trace_kprobe.c 28 KB

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