trace_kprobe.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  1. /*
  2. * Kprobes-based tracing events
  3. *
  4. * Created by Masami Hiramatsu <mhiramat@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/module.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/kprobes.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/slab.h>
  24. #include <linux/smp.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/types.h>
  27. #include <linux/string.h>
  28. #include <linux/ctype.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/perf_event.h>
  31. #include "trace.h"
  32. #include "trace_output.h"
  33. #define MAX_TRACE_ARGS 128
  34. #define MAX_ARGSTR_LEN 63
  35. #define MAX_EVENT_NAME_LEN 64
  36. #define KPROBE_EVENT_SYSTEM "kprobes"
  37. /* Reserved field names */
  38. #define FIELD_STRING_IP "__probe_ip"
  39. #define FIELD_STRING_NARGS "__probe_nargs"
  40. #define FIELD_STRING_RETIP "__probe_ret_ip"
  41. #define FIELD_STRING_FUNC "__probe_func"
  42. const char *reserved_field_names[] = {
  43. "common_type",
  44. "common_flags",
  45. "common_preempt_count",
  46. "common_pid",
  47. "common_tgid",
  48. "common_lock_depth",
  49. FIELD_STRING_IP,
  50. FIELD_STRING_NARGS,
  51. FIELD_STRING_RETIP,
  52. FIELD_STRING_FUNC,
  53. };
  54. struct fetch_func {
  55. unsigned long (*func)(struct pt_regs *, void *);
  56. void *data;
  57. };
  58. static __kprobes unsigned long call_fetch(struct fetch_func *f,
  59. struct pt_regs *regs)
  60. {
  61. return f->func(regs, f->data);
  62. }
  63. /* fetch handlers */
  64. static __kprobes unsigned long fetch_register(struct pt_regs *regs,
  65. void *offset)
  66. {
  67. return regs_get_register(regs, (unsigned int)((unsigned long)offset));
  68. }
  69. static __kprobes unsigned long fetch_stack(struct pt_regs *regs,
  70. void *num)
  71. {
  72. return regs_get_kernel_stack_nth(regs,
  73. (unsigned int)((unsigned long)num));
  74. }
  75. static __kprobes unsigned long fetch_memory(struct pt_regs *regs, void *addr)
  76. {
  77. unsigned long retval;
  78. if (probe_kernel_address(addr, retval))
  79. return 0;
  80. return retval;
  81. }
  82. static __kprobes unsigned long fetch_argument(struct pt_regs *regs, void *num)
  83. {
  84. return regs_get_argument_nth(regs, (unsigned int)((unsigned long)num));
  85. }
  86. static __kprobes unsigned long fetch_retvalue(struct pt_regs *regs,
  87. void *dummy)
  88. {
  89. return regs_return_value(regs);
  90. }
  91. static __kprobes unsigned long fetch_stack_address(struct pt_regs *regs,
  92. void *dummy)
  93. {
  94. return kernel_stack_pointer(regs);
  95. }
  96. /* Memory fetching by symbol */
  97. struct symbol_cache {
  98. char *symbol;
  99. long offset;
  100. unsigned long addr;
  101. };
  102. static unsigned long update_symbol_cache(struct symbol_cache *sc)
  103. {
  104. sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
  105. if (sc->addr)
  106. sc->addr += sc->offset;
  107. return sc->addr;
  108. }
  109. static void free_symbol_cache(struct symbol_cache *sc)
  110. {
  111. kfree(sc->symbol);
  112. kfree(sc);
  113. }
  114. static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
  115. {
  116. struct symbol_cache *sc;
  117. if (!sym || strlen(sym) == 0)
  118. return NULL;
  119. sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
  120. if (!sc)
  121. return NULL;
  122. sc->symbol = kstrdup(sym, GFP_KERNEL);
  123. if (!sc->symbol) {
  124. kfree(sc);
  125. return NULL;
  126. }
  127. sc->offset = offset;
  128. update_symbol_cache(sc);
  129. return sc;
  130. }
  131. static __kprobes unsigned long fetch_symbol(struct pt_regs *regs, void *data)
  132. {
  133. struct symbol_cache *sc = data;
  134. if (sc->addr)
  135. return fetch_memory(regs, (void *)sc->addr);
  136. else
  137. return 0;
  138. }
  139. /* Special indirect memory access interface */
  140. struct indirect_fetch_data {
  141. struct fetch_func orig;
  142. long offset;
  143. };
  144. static __kprobes unsigned long fetch_indirect(struct pt_regs *regs, void *data)
  145. {
  146. struct indirect_fetch_data *ind = data;
  147. unsigned long addr;
  148. addr = call_fetch(&ind->orig, regs);
  149. if (addr) {
  150. addr += ind->offset;
  151. return fetch_memory(regs, (void *)addr);
  152. } else
  153. return 0;
  154. }
  155. static __kprobes void free_indirect_fetch_data(struct indirect_fetch_data *data)
  156. {
  157. if (data->orig.func == fetch_indirect)
  158. free_indirect_fetch_data(data->orig.data);
  159. else if (data->orig.func == fetch_symbol)
  160. free_symbol_cache(data->orig.data);
  161. kfree(data);
  162. }
  163. /**
  164. * Kprobe event core functions
  165. */
  166. struct probe_arg {
  167. struct fetch_func fetch;
  168. const char *name;
  169. };
  170. /* Flags for trace_probe */
  171. #define TP_FLAG_TRACE 1
  172. #define TP_FLAG_PROFILE 2
  173. struct trace_probe {
  174. struct list_head list;
  175. struct kretprobe rp; /* Use rp.kp for kprobe use */
  176. unsigned long nhit;
  177. unsigned int flags; /* For TP_FLAG_* */
  178. const char *symbol; /* symbol name */
  179. struct ftrace_event_call call;
  180. struct trace_event event;
  181. unsigned int nr_args;
  182. struct probe_arg args[];
  183. };
  184. #define SIZEOF_TRACE_PROBE(n) \
  185. (offsetof(struct trace_probe, args) + \
  186. (sizeof(struct probe_arg) * (n)))
  187. static __kprobes int probe_is_return(struct trace_probe *tp)
  188. {
  189. return tp->rp.handler != NULL;
  190. }
  191. static __kprobes const char *probe_symbol(struct trace_probe *tp)
  192. {
  193. return tp->symbol ? tp->symbol : "unknown";
  194. }
  195. static int probe_arg_string(char *buf, size_t n, struct fetch_func *ff)
  196. {
  197. int ret = -EINVAL;
  198. if (ff->func == fetch_argument)
  199. ret = snprintf(buf, n, "$arg%lu", (unsigned long)ff->data);
  200. else if (ff->func == fetch_register) {
  201. const char *name;
  202. name = regs_query_register_name((unsigned int)((long)ff->data));
  203. ret = snprintf(buf, n, "%%%s", name);
  204. } else if (ff->func == fetch_stack)
  205. ret = snprintf(buf, n, "$stack%lu", (unsigned long)ff->data);
  206. else if (ff->func == fetch_memory)
  207. ret = snprintf(buf, n, "@0x%p", ff->data);
  208. else if (ff->func == fetch_symbol) {
  209. struct symbol_cache *sc = ff->data;
  210. ret = snprintf(buf, n, "@%s%+ld", sc->symbol, sc->offset);
  211. } else if (ff->func == fetch_retvalue)
  212. ret = snprintf(buf, n, "$retval");
  213. else if (ff->func == fetch_stack_address)
  214. ret = snprintf(buf, n, "$stack");
  215. else if (ff->func == fetch_indirect) {
  216. struct indirect_fetch_data *id = ff->data;
  217. size_t l = 0;
  218. ret = snprintf(buf, n, "%+ld(", id->offset);
  219. if (ret >= n)
  220. goto end;
  221. l += ret;
  222. ret = probe_arg_string(buf + l, n - l, &id->orig);
  223. if (ret < 0)
  224. goto end;
  225. l += ret;
  226. ret = snprintf(buf + l, n - l, ")");
  227. ret += l;
  228. }
  229. end:
  230. if (ret >= n)
  231. return -ENOSPC;
  232. return ret;
  233. }
  234. static int register_probe_event(struct trace_probe *tp);
  235. static void unregister_probe_event(struct trace_probe *tp);
  236. static DEFINE_MUTEX(probe_lock);
  237. static LIST_HEAD(probe_list);
  238. static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
  239. static int kretprobe_dispatcher(struct kretprobe_instance *ri,
  240. struct pt_regs *regs);
  241. /*
  242. * Allocate new trace_probe and initialize it (including kprobes).
  243. */
  244. static struct trace_probe *alloc_trace_probe(const char *group,
  245. const char *event,
  246. void *addr,
  247. const char *symbol,
  248. unsigned long offs,
  249. int nargs, int is_return)
  250. {
  251. struct trace_probe *tp;
  252. tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
  253. if (!tp)
  254. return ERR_PTR(-ENOMEM);
  255. if (symbol) {
  256. tp->symbol = kstrdup(symbol, GFP_KERNEL);
  257. if (!tp->symbol)
  258. goto error;
  259. tp->rp.kp.symbol_name = tp->symbol;
  260. tp->rp.kp.offset = offs;
  261. } else
  262. tp->rp.kp.addr = addr;
  263. if (is_return)
  264. tp->rp.handler = kretprobe_dispatcher;
  265. else
  266. tp->rp.kp.pre_handler = kprobe_dispatcher;
  267. if (!event)
  268. goto error;
  269. tp->call.name = kstrdup(event, GFP_KERNEL);
  270. if (!tp->call.name)
  271. goto error;
  272. if (!group)
  273. goto error;
  274. tp->call.system = kstrdup(group, GFP_KERNEL);
  275. if (!tp->call.system)
  276. goto error;
  277. INIT_LIST_HEAD(&tp->list);
  278. return tp;
  279. error:
  280. kfree(tp->call.name);
  281. kfree(tp->symbol);
  282. kfree(tp);
  283. return ERR_PTR(-ENOMEM);
  284. }
  285. static void free_probe_arg(struct probe_arg *arg)
  286. {
  287. if (arg->fetch.func == fetch_symbol)
  288. free_symbol_cache(arg->fetch.data);
  289. else if (arg->fetch.func == fetch_indirect)
  290. free_indirect_fetch_data(arg->fetch.data);
  291. kfree(arg->name);
  292. }
  293. static void free_trace_probe(struct trace_probe *tp)
  294. {
  295. int i;
  296. for (i = 0; i < tp->nr_args; i++)
  297. free_probe_arg(&tp->args[i]);
  298. kfree(tp->call.system);
  299. kfree(tp->call.name);
  300. kfree(tp->symbol);
  301. kfree(tp);
  302. }
  303. static struct trace_probe *find_probe_event(const char *event,
  304. const char *group)
  305. {
  306. struct trace_probe *tp;
  307. list_for_each_entry(tp, &probe_list, list)
  308. if (strcmp(tp->call.name, event) == 0 &&
  309. strcmp(tp->call.system, group) == 0)
  310. return tp;
  311. return NULL;
  312. }
  313. /* Unregister a trace_probe and probe_event: call with locking probe_lock */
  314. static void unregister_trace_probe(struct trace_probe *tp)
  315. {
  316. if (probe_is_return(tp))
  317. unregister_kretprobe(&tp->rp);
  318. else
  319. unregister_kprobe(&tp->rp.kp);
  320. list_del(&tp->list);
  321. unregister_probe_event(tp);
  322. }
  323. /* Register a trace_probe and probe_event */
  324. static int register_trace_probe(struct trace_probe *tp)
  325. {
  326. struct trace_probe *old_tp;
  327. int ret;
  328. mutex_lock(&probe_lock);
  329. /* register as an event */
  330. old_tp = find_probe_event(tp->call.name, tp->call.system);
  331. if (old_tp) {
  332. /* delete old event */
  333. unregister_trace_probe(old_tp);
  334. free_trace_probe(old_tp);
  335. }
  336. ret = register_probe_event(tp);
  337. if (ret) {
  338. pr_warning("Faild to register probe event(%d)\n", ret);
  339. goto end;
  340. }
  341. tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
  342. if (probe_is_return(tp))
  343. ret = register_kretprobe(&tp->rp);
  344. else
  345. ret = register_kprobe(&tp->rp.kp);
  346. if (ret) {
  347. pr_warning("Could not insert probe(%d)\n", ret);
  348. if (ret == -EILSEQ) {
  349. pr_warning("Probing address(0x%p) is not an "
  350. "instruction boundary.\n",
  351. tp->rp.kp.addr);
  352. ret = -EINVAL;
  353. }
  354. unregister_probe_event(tp);
  355. } else
  356. list_add_tail(&tp->list, &probe_list);
  357. end:
  358. mutex_unlock(&probe_lock);
  359. return ret;
  360. }
  361. /* Split symbol and offset. */
  362. static int split_symbol_offset(char *symbol, unsigned long *offset)
  363. {
  364. char *tmp;
  365. int ret;
  366. if (!offset)
  367. return -EINVAL;
  368. tmp = strchr(symbol, '+');
  369. if (tmp) {
  370. /* skip sign because strict_strtol doesn't accept '+' */
  371. ret = strict_strtoul(tmp + 1, 0, offset);
  372. if (ret)
  373. return ret;
  374. *tmp = '\0';
  375. } else
  376. *offset = 0;
  377. return 0;
  378. }
  379. #define PARAM_MAX_ARGS 16
  380. #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
  381. static int parse_probe_vars(char *arg, struct fetch_func *ff, int is_return)
  382. {
  383. int ret = 0;
  384. unsigned long param;
  385. if (strcmp(arg, "retval") == 0) {
  386. if (is_return) {
  387. ff->func = fetch_retvalue;
  388. ff->data = NULL;
  389. } else
  390. ret = -EINVAL;
  391. } else if (strncmp(arg, "stack", 5) == 0) {
  392. if (arg[5] == '\0') {
  393. ff->func = fetch_stack_address;
  394. ff->data = NULL;
  395. } else if (isdigit(arg[5])) {
  396. ret = strict_strtoul(arg + 5, 10, &param);
  397. if (ret || param > PARAM_MAX_STACK)
  398. ret = -EINVAL;
  399. else {
  400. ff->func = fetch_stack;
  401. ff->data = (void *)param;
  402. }
  403. } else
  404. ret = -EINVAL;
  405. } else if (strncmp(arg, "arg", 3) == 0 && isdigit(arg[3])) {
  406. ret = strict_strtoul(arg + 3, 10, &param);
  407. if (ret || param > PARAM_MAX_ARGS)
  408. ret = -EINVAL;
  409. else {
  410. ff->func = fetch_argument;
  411. ff->data = (void *)param;
  412. }
  413. } else
  414. ret = -EINVAL;
  415. return ret;
  416. }
  417. static int parse_probe_arg(char *arg, struct fetch_func *ff, int is_return)
  418. {
  419. int ret = 0;
  420. unsigned long param;
  421. long offset;
  422. char *tmp;
  423. switch (arg[0]) {
  424. case '$':
  425. ret = parse_probe_vars(arg + 1, ff, is_return);
  426. break;
  427. case '%': /* named register */
  428. ret = regs_query_register_offset(arg + 1);
  429. if (ret >= 0) {
  430. ff->func = fetch_register;
  431. ff->data = (void *)(unsigned long)ret;
  432. ret = 0;
  433. }
  434. break;
  435. case '@': /* memory or symbol */
  436. if (isdigit(arg[1])) {
  437. ret = strict_strtoul(arg + 1, 0, &param);
  438. if (ret)
  439. break;
  440. ff->func = fetch_memory;
  441. ff->data = (void *)param;
  442. } else {
  443. ret = split_symbol_offset(arg + 1, &offset);
  444. if (ret)
  445. break;
  446. ff->data = alloc_symbol_cache(arg + 1, offset);
  447. if (ff->data)
  448. ff->func = fetch_symbol;
  449. else
  450. ret = -EINVAL;
  451. }
  452. break;
  453. case '+': /* indirect memory */
  454. case '-':
  455. tmp = strchr(arg, '(');
  456. if (!tmp) {
  457. ret = -EINVAL;
  458. break;
  459. }
  460. *tmp = '\0';
  461. ret = strict_strtol(arg + 1, 0, &offset);
  462. if (ret)
  463. break;
  464. if (arg[0] == '-')
  465. offset = -offset;
  466. arg = tmp + 1;
  467. tmp = strrchr(arg, ')');
  468. if (tmp) {
  469. struct indirect_fetch_data *id;
  470. *tmp = '\0';
  471. id = kzalloc(sizeof(struct indirect_fetch_data),
  472. GFP_KERNEL);
  473. if (!id)
  474. return -ENOMEM;
  475. id->offset = offset;
  476. ret = parse_probe_arg(arg, &id->orig, is_return);
  477. if (ret)
  478. kfree(id);
  479. else {
  480. ff->func = fetch_indirect;
  481. ff->data = (void *)id;
  482. }
  483. } else
  484. ret = -EINVAL;
  485. break;
  486. default:
  487. /* TODO: support custom handler */
  488. ret = -EINVAL;
  489. }
  490. return ret;
  491. }
  492. /* Return 1 if name is reserved or already used by another argument */
  493. static int conflict_field_name(const char *name,
  494. struct probe_arg *args, int narg)
  495. {
  496. int i;
  497. for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
  498. if (strcmp(reserved_field_names[i], name) == 0)
  499. return 1;
  500. for (i = 0; i < narg; i++)
  501. if (strcmp(args[i].name, name) == 0)
  502. return 1;
  503. return 0;
  504. }
  505. static int create_trace_probe(int argc, char **argv)
  506. {
  507. /*
  508. * Argument syntax:
  509. * - Add kprobe: p[:[GRP/]EVENT] KSYM[+OFFS]|KADDR [FETCHARGS]
  510. * - Add kretprobe: r[:[GRP/]EVENT] KSYM[+0] [FETCHARGS]
  511. * Fetch args:
  512. * $argN : fetch Nth of function argument. (N:0-)
  513. * $retval : fetch return value
  514. * $stack : fetch stack address
  515. * $stackN : fetch Nth of stack (N:0-)
  516. * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
  517. * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
  518. * %REG : fetch register REG
  519. * Indirect memory fetch:
  520. * +|-offs(ARG) : fetch memory at ARG +|- offs address.
  521. * Alias name of args:
  522. * NAME=FETCHARG : set NAME as alias of FETCHARG.
  523. */
  524. struct trace_probe *tp;
  525. int i, ret = 0;
  526. int is_return = 0;
  527. char *symbol = NULL, *event = NULL, *arg = NULL, *group = NULL;
  528. unsigned long offset = 0;
  529. void *addr = NULL;
  530. char buf[MAX_EVENT_NAME_LEN];
  531. if (argc < 2) {
  532. pr_info("Probe point is not specified.\n");
  533. return -EINVAL;
  534. }
  535. if (argv[0][0] == 'p')
  536. is_return = 0;
  537. else if (argv[0][0] == 'r')
  538. is_return = 1;
  539. else {
  540. pr_info("Probe definition must be started with 'p' or 'r'.\n");
  541. return -EINVAL;
  542. }
  543. if (argv[0][1] == ':') {
  544. event = &argv[0][2];
  545. if (strchr(event, '/')) {
  546. group = event;
  547. event = strchr(group, '/') + 1;
  548. event[-1] = '\0';
  549. if (strlen(group) == 0) {
  550. pr_info("Group name is not specifiled\n");
  551. return -EINVAL;
  552. }
  553. }
  554. if (strlen(event) == 0) {
  555. pr_info("Event name is not specifiled\n");
  556. return -EINVAL;
  557. }
  558. }
  559. if (isdigit(argv[1][0])) {
  560. if (is_return) {
  561. pr_info("Return probe point must be a symbol.\n");
  562. return -EINVAL;
  563. }
  564. /* an address specified */
  565. ret = strict_strtoul(&argv[0][2], 0, (unsigned long *)&addr);
  566. if (ret) {
  567. pr_info("Failed to parse address.\n");
  568. return ret;
  569. }
  570. } else {
  571. /* a symbol specified */
  572. symbol = argv[1];
  573. /* TODO: support .init module functions */
  574. ret = split_symbol_offset(symbol, &offset);
  575. if (ret) {
  576. pr_info("Failed to parse symbol.\n");
  577. return ret;
  578. }
  579. if (offset && is_return) {
  580. pr_info("Return probe must be used without offset.\n");
  581. return -EINVAL;
  582. }
  583. }
  584. argc -= 2; argv += 2;
  585. /* setup a probe */
  586. if (!group)
  587. group = KPROBE_EVENT_SYSTEM;
  588. if (!event) {
  589. /* Make a new event name */
  590. if (symbol)
  591. snprintf(buf, MAX_EVENT_NAME_LEN, "%c@%s%+ld",
  592. is_return ? 'r' : 'p', symbol, offset);
  593. else
  594. snprintf(buf, MAX_EVENT_NAME_LEN, "%c@0x%p",
  595. is_return ? 'r' : 'p', addr);
  596. event = buf;
  597. }
  598. tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
  599. is_return);
  600. if (IS_ERR(tp)) {
  601. pr_info("Failed to allocate trace_probe.(%d)\n",
  602. (int)PTR_ERR(tp));
  603. return PTR_ERR(tp);
  604. }
  605. /* parse arguments */
  606. ret = 0;
  607. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  608. /* Parse argument name */
  609. arg = strchr(argv[i], '=');
  610. if (arg)
  611. *arg++ = '\0';
  612. else
  613. arg = argv[i];
  614. if (conflict_field_name(argv[i], tp->args, i)) {
  615. pr_info("Argument%d name '%s' conflicts with "
  616. "another field.\n", i, argv[i]);
  617. ret = -EINVAL;
  618. goto error;
  619. }
  620. tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
  621. /* Parse fetch argument */
  622. if (strlen(arg) > MAX_ARGSTR_LEN) {
  623. pr_info("Argument%d(%s) is too long.\n", i, arg);
  624. ret = -ENOSPC;
  625. goto error;
  626. }
  627. ret = parse_probe_arg(arg, &tp->args[i].fetch, is_return);
  628. if (ret) {
  629. pr_info("Parse error at argument%d. (%d)\n", i, ret);
  630. kfree(tp->args[i].name);
  631. goto error;
  632. }
  633. tp->nr_args++;
  634. }
  635. ret = register_trace_probe(tp);
  636. if (ret)
  637. goto error;
  638. return 0;
  639. error:
  640. free_trace_probe(tp);
  641. return ret;
  642. }
  643. static void cleanup_all_probes(void)
  644. {
  645. struct trace_probe *tp;
  646. mutex_lock(&probe_lock);
  647. /* TODO: Use batch unregistration */
  648. while (!list_empty(&probe_list)) {
  649. tp = list_entry(probe_list.next, struct trace_probe, list);
  650. unregister_trace_probe(tp);
  651. free_trace_probe(tp);
  652. }
  653. mutex_unlock(&probe_lock);
  654. }
  655. /* Probes listing interfaces */
  656. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  657. {
  658. mutex_lock(&probe_lock);
  659. return seq_list_start(&probe_list, *pos);
  660. }
  661. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  662. {
  663. return seq_list_next(v, &probe_list, pos);
  664. }
  665. static void probes_seq_stop(struct seq_file *m, void *v)
  666. {
  667. mutex_unlock(&probe_lock);
  668. }
  669. static int probes_seq_show(struct seq_file *m, void *v)
  670. {
  671. struct trace_probe *tp = v;
  672. int i, ret;
  673. char buf[MAX_ARGSTR_LEN + 1];
  674. seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
  675. seq_printf(m, ":%s/%s", tp->call.system, tp->call.name);
  676. if (tp->symbol)
  677. seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
  678. else
  679. seq_printf(m, " 0x%p", tp->rp.kp.addr);
  680. for (i = 0; i < tp->nr_args; i++) {
  681. ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i].fetch);
  682. if (ret < 0) {
  683. pr_warning("Argument%d decoding error(%d).\n", i, ret);
  684. return ret;
  685. }
  686. seq_printf(m, " %s=%s", tp->args[i].name, buf);
  687. }
  688. seq_printf(m, "\n");
  689. return 0;
  690. }
  691. static const struct seq_operations probes_seq_op = {
  692. .start = probes_seq_start,
  693. .next = probes_seq_next,
  694. .stop = probes_seq_stop,
  695. .show = probes_seq_show
  696. };
  697. static int probes_open(struct inode *inode, struct file *file)
  698. {
  699. if ((file->f_mode & FMODE_WRITE) &&
  700. (file->f_flags & O_TRUNC))
  701. cleanup_all_probes();
  702. return seq_open(file, &probes_seq_op);
  703. }
  704. static int command_trace_probe(const char *buf)
  705. {
  706. char **argv;
  707. int argc = 0, ret = 0;
  708. argv = argv_split(GFP_KERNEL, buf, &argc);
  709. if (!argv)
  710. return -ENOMEM;
  711. if (argc)
  712. ret = create_trace_probe(argc, argv);
  713. argv_free(argv);
  714. return ret;
  715. }
  716. #define WRITE_BUFSIZE 128
  717. static ssize_t probes_write(struct file *file, const char __user *buffer,
  718. size_t count, loff_t *ppos)
  719. {
  720. char *kbuf, *tmp;
  721. int ret;
  722. size_t done;
  723. size_t size;
  724. kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
  725. if (!kbuf)
  726. return -ENOMEM;
  727. ret = done = 0;
  728. while (done < count) {
  729. size = count - done;
  730. if (size >= WRITE_BUFSIZE)
  731. size = WRITE_BUFSIZE - 1;
  732. if (copy_from_user(kbuf, buffer + done, size)) {
  733. ret = -EFAULT;
  734. goto out;
  735. }
  736. kbuf[size] = '\0';
  737. tmp = strchr(kbuf, '\n');
  738. if (tmp) {
  739. *tmp = '\0';
  740. size = tmp - kbuf + 1;
  741. } else if (done + size < count) {
  742. pr_warning("Line length is too long: "
  743. "Should be less than %d.", WRITE_BUFSIZE);
  744. ret = -EINVAL;
  745. goto out;
  746. }
  747. done += size;
  748. /* Remove comments */
  749. tmp = strchr(kbuf, '#');
  750. if (tmp)
  751. *tmp = '\0';
  752. ret = command_trace_probe(kbuf);
  753. if (ret)
  754. goto out;
  755. }
  756. ret = done;
  757. out:
  758. kfree(kbuf);
  759. return ret;
  760. }
  761. static const struct file_operations kprobe_events_ops = {
  762. .owner = THIS_MODULE,
  763. .open = probes_open,
  764. .read = seq_read,
  765. .llseek = seq_lseek,
  766. .release = seq_release,
  767. .write = probes_write,
  768. };
  769. /* Probes profiling interfaces */
  770. static int probes_profile_seq_show(struct seq_file *m, void *v)
  771. {
  772. struct trace_probe *tp = v;
  773. seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
  774. tp->rp.kp.nmissed);
  775. return 0;
  776. }
  777. static const struct seq_operations profile_seq_op = {
  778. .start = probes_seq_start,
  779. .next = probes_seq_next,
  780. .stop = probes_seq_stop,
  781. .show = probes_profile_seq_show
  782. };
  783. static int profile_open(struct inode *inode, struct file *file)
  784. {
  785. return seq_open(file, &profile_seq_op);
  786. }
  787. static const struct file_operations kprobe_profile_ops = {
  788. .owner = THIS_MODULE,
  789. .open = profile_open,
  790. .read = seq_read,
  791. .llseek = seq_lseek,
  792. .release = seq_release,
  793. };
  794. /* Kprobe handler */
  795. static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
  796. {
  797. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  798. struct kprobe_trace_entry *entry;
  799. struct ring_buffer_event *event;
  800. struct ring_buffer *buffer;
  801. int size, i, pc;
  802. unsigned long irq_flags;
  803. struct ftrace_event_call *call = &tp->call;
  804. tp->nhit++;
  805. local_save_flags(irq_flags);
  806. pc = preempt_count();
  807. size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
  808. event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
  809. irq_flags, pc);
  810. if (!event)
  811. return 0;
  812. entry = ring_buffer_event_data(event);
  813. entry->nargs = tp->nr_args;
  814. entry->ip = (unsigned long)kp->addr;
  815. for (i = 0; i < tp->nr_args; i++)
  816. entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
  817. if (!filter_current_check_discard(buffer, call, entry, event))
  818. trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
  819. return 0;
  820. }
  821. /* Kretprobe handler */
  822. static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri,
  823. struct pt_regs *regs)
  824. {
  825. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  826. struct kretprobe_trace_entry *entry;
  827. struct ring_buffer_event *event;
  828. struct ring_buffer *buffer;
  829. int size, i, pc;
  830. unsigned long irq_flags;
  831. struct ftrace_event_call *call = &tp->call;
  832. local_save_flags(irq_flags);
  833. pc = preempt_count();
  834. size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
  835. event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
  836. irq_flags, pc);
  837. if (!event)
  838. return 0;
  839. entry = ring_buffer_event_data(event);
  840. entry->nargs = tp->nr_args;
  841. entry->func = (unsigned long)tp->rp.kp.addr;
  842. entry->ret_ip = (unsigned long)ri->ret_addr;
  843. for (i = 0; i < tp->nr_args; i++)
  844. entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
  845. if (!filter_current_check_discard(buffer, call, entry, event))
  846. trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
  847. return 0;
  848. }
  849. /* Event entry printers */
  850. enum print_line_t
  851. print_kprobe_event(struct trace_iterator *iter, int flags)
  852. {
  853. struct kprobe_trace_entry *field;
  854. struct trace_seq *s = &iter->seq;
  855. struct trace_event *event;
  856. struct trace_probe *tp;
  857. int i;
  858. field = (struct kprobe_trace_entry *)iter->ent;
  859. event = ftrace_find_event(field->ent.type);
  860. tp = container_of(event, struct trace_probe, event);
  861. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  862. goto partial;
  863. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  864. goto partial;
  865. if (!trace_seq_puts(s, ")"))
  866. goto partial;
  867. for (i = 0; i < field->nargs; i++)
  868. if (!trace_seq_printf(s, " %s=%lx",
  869. tp->args[i].name, field->args[i]))
  870. goto partial;
  871. if (!trace_seq_puts(s, "\n"))
  872. goto partial;
  873. return TRACE_TYPE_HANDLED;
  874. partial:
  875. return TRACE_TYPE_PARTIAL_LINE;
  876. }
  877. enum print_line_t
  878. print_kretprobe_event(struct trace_iterator *iter, int flags)
  879. {
  880. struct kretprobe_trace_entry *field;
  881. struct trace_seq *s = &iter->seq;
  882. struct trace_event *event;
  883. struct trace_probe *tp;
  884. int i;
  885. field = (struct kretprobe_trace_entry *)iter->ent;
  886. event = ftrace_find_event(field->ent.type);
  887. tp = container_of(event, struct trace_probe, event);
  888. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  889. goto partial;
  890. if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
  891. goto partial;
  892. if (!trace_seq_puts(s, " <- "))
  893. goto partial;
  894. if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
  895. goto partial;
  896. if (!trace_seq_puts(s, ")"))
  897. goto partial;
  898. for (i = 0; i < field->nargs; i++)
  899. if (!trace_seq_printf(s, " %s=%lx",
  900. tp->args[i].name, field->args[i]))
  901. goto partial;
  902. if (!trace_seq_puts(s, "\n"))
  903. goto partial;
  904. return TRACE_TYPE_HANDLED;
  905. partial:
  906. return TRACE_TYPE_PARTIAL_LINE;
  907. }
  908. static int probe_event_enable(struct ftrace_event_call *call)
  909. {
  910. struct trace_probe *tp = (struct trace_probe *)call->data;
  911. tp->flags |= TP_FLAG_TRACE;
  912. if (probe_is_return(tp))
  913. return enable_kretprobe(&tp->rp);
  914. else
  915. return enable_kprobe(&tp->rp.kp);
  916. }
  917. static void probe_event_disable(struct ftrace_event_call *call)
  918. {
  919. struct trace_probe *tp = (struct trace_probe *)call->data;
  920. tp->flags &= ~TP_FLAG_TRACE;
  921. if (!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE))) {
  922. if (probe_is_return(tp))
  923. disable_kretprobe(&tp->rp);
  924. else
  925. disable_kprobe(&tp->rp.kp);
  926. }
  927. }
  928. static int probe_event_raw_init(struct ftrace_event_call *event_call)
  929. {
  930. INIT_LIST_HEAD(&event_call->fields);
  931. return 0;
  932. }
  933. #undef DEFINE_FIELD
  934. #define DEFINE_FIELD(type, item, name, is_signed) \
  935. do { \
  936. ret = trace_define_field(event_call, #type, name, \
  937. offsetof(typeof(field), item), \
  938. sizeof(field.item), is_signed, \
  939. FILTER_OTHER); \
  940. if (ret) \
  941. return ret; \
  942. } while (0)
  943. static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
  944. {
  945. int ret, i;
  946. struct kprobe_trace_entry field;
  947. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  948. ret = trace_define_common_fields(event_call);
  949. if (!ret)
  950. return ret;
  951. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  952. DEFINE_FIELD(int, nargs, FIELD_STRING_NARGS, 1);
  953. /* Set argument names as fields */
  954. for (i = 0; i < tp->nr_args; i++)
  955. DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
  956. return 0;
  957. }
  958. static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
  959. {
  960. int ret, i;
  961. struct kretprobe_trace_entry field;
  962. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  963. ret = trace_define_common_fields(event_call);
  964. if (!ret)
  965. return ret;
  966. DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
  967. DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
  968. DEFINE_FIELD(int, nargs, FIELD_STRING_NARGS, 1);
  969. /* Set argument names as fields */
  970. for (i = 0; i < tp->nr_args; i++)
  971. DEFINE_FIELD(unsigned long, args[i], tp->args[i].name, 0);
  972. return 0;
  973. }
  974. static int __probe_event_show_format(struct trace_seq *s,
  975. struct trace_probe *tp, const char *fmt,
  976. const char *arg)
  977. {
  978. int i;
  979. /* Show format */
  980. if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt))
  981. return 0;
  982. for (i = 0; i < tp->nr_args; i++)
  983. if (!trace_seq_printf(s, " %s=%%lx", tp->args[i].name))
  984. return 0;
  985. if (!trace_seq_printf(s, "\", %s", arg))
  986. return 0;
  987. for (i = 0; i < tp->nr_args; i++)
  988. if (!trace_seq_printf(s, ", REC->%s", tp->args[i].name))
  989. return 0;
  990. return trace_seq_puts(s, "\n");
  991. }
  992. #undef SHOW_FIELD
  993. #define SHOW_FIELD(type, item, name) \
  994. do { \
  995. ret = trace_seq_printf(s, "\tfield: " #type " %s;\t" \
  996. "offset:%u;\tsize:%u;\n", name, \
  997. (unsigned int)offsetof(typeof(field), item),\
  998. (unsigned int)sizeof(type)); \
  999. if (!ret) \
  1000. return 0; \
  1001. } while (0)
  1002. static int kprobe_event_show_format(struct ftrace_event_call *call,
  1003. struct trace_seq *s)
  1004. {
  1005. struct kprobe_trace_entry field __attribute__((unused));
  1006. int ret, i;
  1007. struct trace_probe *tp = (struct trace_probe *)call->data;
  1008. SHOW_FIELD(unsigned long, ip, FIELD_STRING_IP);
  1009. SHOW_FIELD(int, nargs, FIELD_STRING_NARGS);
  1010. /* Show fields */
  1011. for (i = 0; i < tp->nr_args; i++)
  1012. SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
  1013. trace_seq_puts(s, "\n");
  1014. return __probe_event_show_format(s, tp, "(%lx)",
  1015. "REC->" FIELD_STRING_IP);
  1016. }
  1017. static int kretprobe_event_show_format(struct ftrace_event_call *call,
  1018. struct trace_seq *s)
  1019. {
  1020. struct kretprobe_trace_entry field __attribute__((unused));
  1021. int ret, i;
  1022. struct trace_probe *tp = (struct trace_probe *)call->data;
  1023. SHOW_FIELD(unsigned long, func, FIELD_STRING_FUNC);
  1024. SHOW_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP);
  1025. SHOW_FIELD(int, nargs, FIELD_STRING_NARGS);
  1026. /* Show fields */
  1027. for (i = 0; i < tp->nr_args; i++)
  1028. SHOW_FIELD(unsigned long, args[i], tp->args[i].name);
  1029. trace_seq_puts(s, "\n");
  1030. return __probe_event_show_format(s, tp, "(%lx <- %lx)",
  1031. "REC->" FIELD_STRING_FUNC
  1032. ", REC->" FIELD_STRING_RETIP);
  1033. }
  1034. #ifdef CONFIG_EVENT_PROFILE
  1035. /* Kprobe profile handler */
  1036. static __kprobes int kprobe_profile_func(struct kprobe *kp,
  1037. struct pt_regs *regs)
  1038. {
  1039. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1040. struct ftrace_event_call *call = &tp->call;
  1041. struct kprobe_trace_entry *entry;
  1042. struct trace_entry *ent;
  1043. int size, __size, i, pc, __cpu;
  1044. unsigned long irq_flags;
  1045. char *trace_buf;
  1046. char *raw_data;
  1047. int rctx;
  1048. pc = preempt_count();
  1049. __size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
  1050. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1051. size -= sizeof(u32);
  1052. if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
  1053. "profile buffer not large enough"))
  1054. return 0;
  1055. /*
  1056. * Protect the non nmi buffer
  1057. * This also protects the rcu read side
  1058. */
  1059. local_irq_save(irq_flags);
  1060. rctx = perf_swevent_get_recursion_context();
  1061. if (rctx < 0)
  1062. goto end_recursion;
  1063. __cpu = smp_processor_id();
  1064. if (in_nmi())
  1065. trace_buf = rcu_dereference(perf_trace_buf_nmi);
  1066. else
  1067. trace_buf = rcu_dereference(perf_trace_buf);
  1068. if (!trace_buf)
  1069. goto end;
  1070. raw_data = per_cpu_ptr(trace_buf, __cpu);
  1071. /* Zero dead bytes from alignment to avoid buffer leak to userspace */
  1072. *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
  1073. entry = (struct kprobe_trace_entry *)raw_data;
  1074. ent = &entry->ent;
  1075. tracing_generic_entry_update(ent, irq_flags, pc);
  1076. ent->type = call->id;
  1077. entry->nargs = tp->nr_args;
  1078. entry->ip = (unsigned long)kp->addr;
  1079. for (i = 0; i < tp->nr_args; i++)
  1080. entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
  1081. perf_tp_event(call->id, entry->ip, 1, entry, size);
  1082. end:
  1083. perf_swevent_put_recursion_context(rctx);
  1084. end_recursion:
  1085. local_irq_restore(irq_flags);
  1086. return 0;
  1087. }
  1088. /* Kretprobe profile handler */
  1089. static __kprobes int kretprobe_profile_func(struct kretprobe_instance *ri,
  1090. struct pt_regs *regs)
  1091. {
  1092. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1093. struct ftrace_event_call *call = &tp->call;
  1094. struct kretprobe_trace_entry *entry;
  1095. struct trace_entry *ent;
  1096. int size, __size, i, pc, __cpu;
  1097. unsigned long irq_flags;
  1098. char *trace_buf;
  1099. char *raw_data;
  1100. int rctx;
  1101. pc = preempt_count();
  1102. __size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
  1103. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  1104. size -= sizeof(u32);
  1105. if (WARN_ONCE(size > FTRACE_MAX_PROFILE_SIZE,
  1106. "profile buffer not large enough"))
  1107. return 0;
  1108. /*
  1109. * Protect the non nmi buffer
  1110. * This also protects the rcu read side
  1111. */
  1112. local_irq_save(irq_flags);
  1113. rctx = perf_swevent_get_recursion_context();
  1114. if (rctx < 0)
  1115. goto end_recursion;
  1116. __cpu = smp_processor_id();
  1117. if (in_nmi())
  1118. trace_buf = rcu_dereference(perf_trace_buf_nmi);
  1119. else
  1120. trace_buf = rcu_dereference(perf_trace_buf);
  1121. if (!trace_buf)
  1122. goto end;
  1123. raw_data = per_cpu_ptr(trace_buf, __cpu);
  1124. /* Zero dead bytes from alignment to avoid buffer leak to userspace */
  1125. *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
  1126. entry = (struct kretprobe_trace_entry *)raw_data;
  1127. ent = &entry->ent;
  1128. tracing_generic_entry_update(ent, irq_flags, pc);
  1129. ent->type = call->id;
  1130. entry->nargs = tp->nr_args;
  1131. entry->func = (unsigned long)tp->rp.kp.addr;
  1132. entry->ret_ip = (unsigned long)ri->ret_addr;
  1133. for (i = 0; i < tp->nr_args; i++)
  1134. entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
  1135. perf_tp_event(call->id, entry->ret_ip, 1, entry, size);
  1136. end:
  1137. perf_swevent_put_recursion_context(rctx);
  1138. end_recursion:
  1139. local_irq_restore(irq_flags);
  1140. return 0;
  1141. }
  1142. static int probe_profile_enable(struct ftrace_event_call *call)
  1143. {
  1144. struct trace_probe *tp = (struct trace_probe *)call->data;
  1145. tp->flags |= TP_FLAG_PROFILE;
  1146. if (probe_is_return(tp))
  1147. return enable_kretprobe(&tp->rp);
  1148. else
  1149. return enable_kprobe(&tp->rp.kp);
  1150. }
  1151. static void probe_profile_disable(struct ftrace_event_call *call)
  1152. {
  1153. struct trace_probe *tp = (struct trace_probe *)call->data;
  1154. tp->flags &= ~TP_FLAG_PROFILE;
  1155. if (!(tp->flags & TP_FLAG_TRACE)) {
  1156. if (probe_is_return(tp))
  1157. disable_kretprobe(&tp->rp);
  1158. else
  1159. disable_kprobe(&tp->rp.kp);
  1160. }
  1161. }
  1162. #endif /* CONFIG_EVENT_PROFILE */
  1163. static __kprobes
  1164. int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
  1165. {
  1166. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  1167. if (tp->flags & TP_FLAG_TRACE)
  1168. kprobe_trace_func(kp, regs);
  1169. #ifdef CONFIG_EVENT_PROFILE
  1170. if (tp->flags & TP_FLAG_PROFILE)
  1171. kprobe_profile_func(kp, regs);
  1172. #endif /* CONFIG_EVENT_PROFILE */
  1173. return 0; /* We don't tweek kernel, so just return 0 */
  1174. }
  1175. static __kprobes
  1176. int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
  1177. {
  1178. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  1179. if (tp->flags & TP_FLAG_TRACE)
  1180. kretprobe_trace_func(ri, regs);
  1181. #ifdef CONFIG_EVENT_PROFILE
  1182. if (tp->flags & TP_FLAG_PROFILE)
  1183. kretprobe_profile_func(ri, regs);
  1184. #endif /* CONFIG_EVENT_PROFILE */
  1185. return 0; /* We don't tweek kernel, so just return 0 */
  1186. }
  1187. static int register_probe_event(struct trace_probe *tp)
  1188. {
  1189. struct ftrace_event_call *call = &tp->call;
  1190. int ret;
  1191. /* Initialize ftrace_event_call */
  1192. if (probe_is_return(tp)) {
  1193. tp->event.trace = print_kretprobe_event;
  1194. call->raw_init = probe_event_raw_init;
  1195. call->show_format = kretprobe_event_show_format;
  1196. call->define_fields = kretprobe_event_define_fields;
  1197. } else {
  1198. tp->event.trace = print_kprobe_event;
  1199. call->raw_init = probe_event_raw_init;
  1200. call->show_format = kprobe_event_show_format;
  1201. call->define_fields = kprobe_event_define_fields;
  1202. }
  1203. call->event = &tp->event;
  1204. call->id = register_ftrace_event(&tp->event);
  1205. if (!call->id)
  1206. return -ENODEV;
  1207. call->enabled = 0;
  1208. call->regfunc = probe_event_enable;
  1209. call->unregfunc = probe_event_disable;
  1210. #ifdef CONFIG_EVENT_PROFILE
  1211. atomic_set(&call->profile_count, -1);
  1212. call->profile_enable = probe_profile_enable;
  1213. call->profile_disable = probe_profile_disable;
  1214. #endif
  1215. call->data = tp;
  1216. ret = trace_add_event_call(call);
  1217. if (ret) {
  1218. pr_info("Failed to register kprobe event: %s\n", call->name);
  1219. unregister_ftrace_event(&tp->event);
  1220. }
  1221. return ret;
  1222. }
  1223. static void unregister_probe_event(struct trace_probe *tp)
  1224. {
  1225. /* tp->event is unregistered in trace_remove_event_call() */
  1226. trace_remove_event_call(&tp->call);
  1227. }
  1228. /* Make a debugfs interface for controling probe points */
  1229. static __init int init_kprobe_trace(void)
  1230. {
  1231. struct dentry *d_tracer;
  1232. struct dentry *entry;
  1233. d_tracer = tracing_init_dentry();
  1234. if (!d_tracer)
  1235. return 0;
  1236. entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
  1237. NULL, &kprobe_events_ops);
  1238. /* Event list interface */
  1239. if (!entry)
  1240. pr_warning("Could not create debugfs "
  1241. "'kprobe_events' entry\n");
  1242. /* Profile interface */
  1243. entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
  1244. NULL, &kprobe_profile_ops);
  1245. if (!entry)
  1246. pr_warning("Could not create debugfs "
  1247. "'kprobe_profile' entry\n");
  1248. return 0;
  1249. }
  1250. fs_initcall(init_kprobe_trace);
  1251. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1252. static int kprobe_trace_selftest_target(int a1, int a2, int a3,
  1253. int a4, int a5, int a6)
  1254. {
  1255. return a1 + a2 + a3 + a4 + a5 + a6;
  1256. }
  1257. static __init int kprobe_trace_self_tests_init(void)
  1258. {
  1259. int ret;
  1260. int (*target)(int, int, int, int, int, int);
  1261. target = kprobe_trace_selftest_target;
  1262. pr_info("Testing kprobe tracing: ");
  1263. ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
  1264. "$arg1 $arg2 $arg3 $arg4 $stack $stack0");
  1265. if (WARN_ON_ONCE(ret))
  1266. pr_warning("error enabling function entry\n");
  1267. ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
  1268. "$retval");
  1269. if (WARN_ON_ONCE(ret))
  1270. pr_warning("error enabling function return\n");
  1271. ret = target(1, 2, 3, 4, 5, 6);
  1272. cleanup_all_probes();
  1273. pr_cont("OK\n");
  1274. return 0;
  1275. }
  1276. late_initcall(kprobe_trace_self_tests_init);
  1277. #endif