trace_kprobe.c 33 KB

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