trace_kprobe.c 27 KB

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