trace_kprobe.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  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 long 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 trace_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 = trace_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, 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. if (!event) {
  517. /* Make a new event name */
  518. char buf[MAX_EVENT_NAME_LEN];
  519. if (symbol)
  520. snprintf(buf, MAX_EVENT_NAME_LEN, "%c@%s%+ld",
  521. is_return ? 'r' : 'p', symbol, offset);
  522. else
  523. snprintf(buf, MAX_EVENT_NAME_LEN, "%c@0x%p",
  524. is_return ? 'r' : 'p', addr);
  525. tp = alloc_trace_probe(symbol, buf, argc);
  526. } else
  527. tp = alloc_trace_probe(symbol, event, argc);
  528. if (IS_ERR(tp))
  529. return PTR_ERR(tp);
  530. if (is_return) {
  531. kp = &tp->rp.kp;
  532. tp->rp.handler = kretprobe_trace_func;
  533. } else {
  534. kp = &tp->kp;
  535. tp->kp.pre_handler = kprobe_trace_func;
  536. }
  537. if (tp->symbol) {
  538. kp->symbol_name = tp->symbol;
  539. kp->offset = offset;
  540. } else
  541. kp->addr = addr;
  542. /* parse arguments */
  543. ret = 0;
  544. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  545. if (strlen(argv[i]) > MAX_ARGSTR_LEN) {
  546. pr_info("Argument%d(%s) is too long.\n", i, argv[i]);
  547. ret = -ENOSPC;
  548. goto error;
  549. }
  550. ret = parse_trace_arg(argv[i], &tp->args[i], is_return);
  551. if (ret)
  552. goto error;
  553. }
  554. tp->nr_args = i;
  555. ret = register_trace_probe(tp);
  556. if (ret)
  557. goto error;
  558. return 0;
  559. error:
  560. free_trace_probe(tp);
  561. return ret;
  562. }
  563. static void cleanup_all_probes(void)
  564. {
  565. struct trace_probe *tp;
  566. mutex_lock(&probe_lock);
  567. /* TODO: Use batch unregistration */
  568. while (!list_empty(&probe_list)) {
  569. tp = list_entry(probe_list.next, struct trace_probe, list);
  570. unregister_trace_probe(tp);
  571. free_trace_probe(tp);
  572. }
  573. mutex_unlock(&probe_lock);
  574. }
  575. /* Probes listing interfaces */
  576. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  577. {
  578. mutex_lock(&probe_lock);
  579. return seq_list_start(&probe_list, *pos);
  580. }
  581. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  582. {
  583. return seq_list_next(v, &probe_list, pos);
  584. }
  585. static void probes_seq_stop(struct seq_file *m, void *v)
  586. {
  587. mutex_unlock(&probe_lock);
  588. }
  589. static int probes_seq_show(struct seq_file *m, void *v)
  590. {
  591. struct trace_probe *tp = v;
  592. int i, ret;
  593. char buf[MAX_ARGSTR_LEN + 1];
  594. seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
  595. seq_printf(m, ":%s", tp->call.name);
  596. if (tp->symbol)
  597. seq_printf(m, " %s%+ld", probe_symbol(tp), probe_offset(tp));
  598. else
  599. seq_printf(m, " 0x%p", probe_address(tp));
  600. for (i = 0; i < tp->nr_args; i++) {
  601. ret = trace_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
  602. if (ret < 0) {
  603. pr_warning("Argument%d decoding error(%d).\n", i, ret);
  604. return ret;
  605. }
  606. seq_printf(m, " %s", buf);
  607. }
  608. seq_printf(m, "\n");
  609. return 0;
  610. }
  611. static const struct seq_operations probes_seq_op = {
  612. .start = probes_seq_start,
  613. .next = probes_seq_next,
  614. .stop = probes_seq_stop,
  615. .show = probes_seq_show
  616. };
  617. static int probes_open(struct inode *inode, struct file *file)
  618. {
  619. if ((file->f_mode & FMODE_WRITE) &&
  620. (file->f_flags & O_TRUNC))
  621. cleanup_all_probes();
  622. return seq_open(file, &probes_seq_op);
  623. }
  624. static int command_trace_probe(const char *buf)
  625. {
  626. char **argv;
  627. int argc = 0, ret = 0;
  628. argv = argv_split(GFP_KERNEL, buf, &argc);
  629. if (!argv)
  630. return -ENOMEM;
  631. if (argc)
  632. ret = create_trace_probe(argc, argv);
  633. argv_free(argv);
  634. return ret;
  635. }
  636. #define WRITE_BUFSIZE 128
  637. static ssize_t probes_write(struct file *file, const char __user *buffer,
  638. size_t count, loff_t *ppos)
  639. {
  640. char *kbuf, *tmp;
  641. int ret;
  642. size_t done;
  643. size_t size;
  644. kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
  645. if (!kbuf)
  646. return -ENOMEM;
  647. ret = done = 0;
  648. while (done < count) {
  649. size = count - done;
  650. if (size >= WRITE_BUFSIZE)
  651. size = WRITE_BUFSIZE - 1;
  652. if (copy_from_user(kbuf, buffer + done, size)) {
  653. ret = -EFAULT;
  654. goto out;
  655. }
  656. kbuf[size] = '\0';
  657. tmp = strchr(kbuf, '\n');
  658. if (tmp) {
  659. *tmp = '\0';
  660. size = tmp - kbuf + 1;
  661. } else if (done + size < count) {
  662. pr_warning("Line length is too long: "
  663. "Should be less than %d.", WRITE_BUFSIZE);
  664. ret = -EINVAL;
  665. goto out;
  666. }
  667. done += size;
  668. /* Remove comments */
  669. tmp = strchr(kbuf, '#');
  670. if (tmp)
  671. *tmp = '\0';
  672. ret = command_trace_probe(kbuf);
  673. if (ret)
  674. goto out;
  675. }
  676. ret = done;
  677. out:
  678. kfree(kbuf);
  679. return ret;
  680. }
  681. static const struct file_operations kprobe_events_ops = {
  682. .owner = THIS_MODULE,
  683. .open = probes_open,
  684. .read = seq_read,
  685. .llseek = seq_lseek,
  686. .release = seq_release,
  687. .write = probes_write,
  688. };
  689. /* Probes profiling interfaces */
  690. static int probes_profile_seq_show(struct seq_file *m, void *v)
  691. {
  692. struct trace_probe *tp = v;
  693. seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
  694. probe_is_return(tp) ? tp->rp.kp.nmissed : tp->kp.nmissed);
  695. return 0;
  696. }
  697. static const struct seq_operations profile_seq_op = {
  698. .start = probes_seq_start,
  699. .next = probes_seq_next,
  700. .stop = probes_seq_stop,
  701. .show = probes_profile_seq_show
  702. };
  703. static int profile_open(struct inode *inode, struct file *file)
  704. {
  705. return seq_open(file, &profile_seq_op);
  706. }
  707. static const struct file_operations kprobe_profile_ops = {
  708. .owner = THIS_MODULE,
  709. .open = profile_open,
  710. .read = seq_read,
  711. .llseek = seq_lseek,
  712. .release = seq_release,
  713. };
  714. /* Kprobe handler */
  715. static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
  716. {
  717. struct trace_probe *tp = container_of(kp, struct trace_probe, kp);
  718. struct kprobe_trace_entry *entry;
  719. struct ring_buffer_event *event;
  720. int size, i, pc;
  721. unsigned long irq_flags;
  722. struct ftrace_event_call *call = &tp->call;
  723. tp->nhit++;
  724. local_save_flags(irq_flags);
  725. pc = preempt_count();
  726. size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
  727. event = trace_current_buffer_lock_reserve(call->id, size,
  728. irq_flags, pc);
  729. if (!event)
  730. return 0;
  731. entry = ring_buffer_event_data(event);
  732. entry->nargs = tp->nr_args;
  733. entry->ip = (unsigned long)kp->addr;
  734. for (i = 0; i < tp->nr_args; i++)
  735. entry->args[i] = call_fetch(&tp->args[i], regs);
  736. if (!filter_current_check_discard(call, entry, event))
  737. trace_nowake_buffer_unlock_commit(event, irq_flags, pc);
  738. return 0;
  739. }
  740. /* Kretprobe handler */
  741. static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri,
  742. struct pt_regs *regs)
  743. {
  744. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  745. struct kretprobe_trace_entry *entry;
  746. struct ring_buffer_event *event;
  747. int size, i, pc;
  748. unsigned long irq_flags;
  749. struct ftrace_event_call *call = &tp->call;
  750. local_save_flags(irq_flags);
  751. pc = preempt_count();
  752. size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
  753. event = trace_current_buffer_lock_reserve(call->id, size,
  754. irq_flags, pc);
  755. if (!event)
  756. return 0;
  757. entry = ring_buffer_event_data(event);
  758. entry->nargs = tp->nr_args;
  759. entry->func = (unsigned long)probe_address(tp);
  760. entry->ret_ip = (unsigned long)ri->ret_addr;
  761. for (i = 0; i < tp->nr_args; i++)
  762. entry->args[i] = call_fetch(&tp->args[i], regs);
  763. if (!filter_current_check_discard(call, entry, event))
  764. trace_nowake_buffer_unlock_commit(event, irq_flags, pc);
  765. return 0;
  766. }
  767. /* Event entry printers */
  768. enum print_line_t
  769. print_kprobe_event(struct trace_iterator *iter, int flags)
  770. {
  771. struct kprobe_trace_entry *field;
  772. struct trace_seq *s = &iter->seq;
  773. int i;
  774. field = (struct kprobe_trace_entry *)iter->ent;
  775. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  776. goto partial;
  777. if (!trace_seq_puts(s, ":"))
  778. goto partial;
  779. for (i = 0; i < field->nargs; i++)
  780. if (!trace_seq_printf(s, " 0x%lx", field->args[i]))
  781. goto partial;
  782. if (!trace_seq_puts(s, "\n"))
  783. goto partial;
  784. return TRACE_TYPE_HANDLED;
  785. partial:
  786. return TRACE_TYPE_PARTIAL_LINE;
  787. }
  788. enum print_line_t
  789. print_kretprobe_event(struct trace_iterator *iter, int flags)
  790. {
  791. struct kretprobe_trace_entry *field;
  792. struct trace_seq *s = &iter->seq;
  793. int i;
  794. field = (struct kretprobe_trace_entry *)iter->ent;
  795. if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
  796. goto partial;
  797. if (!trace_seq_puts(s, " <- "))
  798. goto partial;
  799. if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
  800. goto partial;
  801. if (!trace_seq_puts(s, ":"))
  802. goto partial;
  803. for (i = 0; i < field->nargs; i++)
  804. if (!trace_seq_printf(s, " 0x%lx", field->args[i]))
  805. goto partial;
  806. if (!trace_seq_puts(s, "\n"))
  807. goto partial;
  808. return TRACE_TYPE_HANDLED;
  809. partial:
  810. return TRACE_TYPE_PARTIAL_LINE;
  811. }
  812. static int probe_event_enable(struct ftrace_event_call *call)
  813. {
  814. struct trace_probe *tp = (struct trace_probe *)call->data;
  815. if (probe_is_return(tp))
  816. return enable_kretprobe(&tp->rp);
  817. else
  818. return enable_kprobe(&tp->kp);
  819. }
  820. static void probe_event_disable(struct ftrace_event_call *call)
  821. {
  822. struct trace_probe *tp = (struct trace_probe *)call->data;
  823. if (probe_is_return(tp))
  824. disable_kretprobe(&tp->rp);
  825. else
  826. disable_kprobe(&tp->kp);
  827. }
  828. static int probe_event_raw_init(struct ftrace_event_call *event_call)
  829. {
  830. INIT_LIST_HEAD(&event_call->fields);
  831. init_preds(event_call);
  832. return 0;
  833. }
  834. #undef DEFINE_FIELD
  835. #define DEFINE_FIELD(type, item, name, is_signed) \
  836. do { \
  837. ret = trace_define_field(event_call, #type, name, \
  838. offsetof(typeof(field), item), \
  839. sizeof(field.item), is_signed, \
  840. FILTER_OTHER); \
  841. if (ret) \
  842. return ret; \
  843. } while (0)
  844. static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
  845. {
  846. int ret, i;
  847. struct kprobe_trace_entry field;
  848. char buf[MAX_ARGSTR_LEN + 1];
  849. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  850. ret = trace_define_common_fields(event_call);
  851. if (!ret)
  852. return ret;
  853. DEFINE_FIELD(unsigned long, ip, "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 kretprobe_event_define_fields(struct ftrace_event_call *event_call)
  868. {
  869. int ret, i;
  870. struct kretprobe_trace_entry field;
  871. char buf[MAX_ARGSTR_LEN + 1];
  872. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  873. ret = trace_define_common_fields(event_call);
  874. if (!ret)
  875. return ret;
  876. DEFINE_FIELD(unsigned long, func, "func", 0);
  877. DEFINE_FIELD(unsigned long, ret_ip, "ret_ip", 0);
  878. DEFINE_FIELD(int, nargs, "nargs", 1);
  879. for (i = 0; i < tp->nr_args; i++) {
  880. /* Set argN as a field */
  881. sprintf(buf, "arg%d", i);
  882. DEFINE_FIELD(unsigned long, args[i], buf, 0);
  883. /* Set argument string as an alias field */
  884. ret = trace_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
  885. if (ret < 0)
  886. return ret;
  887. DEFINE_FIELD(unsigned long, args[i], buf, 0);
  888. }
  889. return 0;
  890. }
  891. static int __probe_event_show_format(struct trace_seq *s,
  892. struct trace_probe *tp, const char *fmt,
  893. const char *arg)
  894. {
  895. int i, ret;
  896. char buf[MAX_ARGSTR_LEN + 1];
  897. /* Show aliases */
  898. for (i = 0; i < tp->nr_args; i++) {
  899. ret = trace_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
  900. if (ret < 0)
  901. return ret;
  902. if (!trace_seq_printf(s, "\talias: %s;\toriginal: arg%d;\n",
  903. buf, i))
  904. return 0;
  905. }
  906. /* Show format */
  907. if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt))
  908. return 0;
  909. for (i = 0; i < tp->nr_args; i++)
  910. if (!trace_seq_puts(s, " 0x%lx"))
  911. return 0;
  912. if (!trace_seq_printf(s, "\", %s", arg))
  913. return 0;
  914. for (i = 0; i < tp->nr_args; i++)
  915. if (!trace_seq_printf(s, ", arg%d", i))
  916. return 0;
  917. return trace_seq_puts(s, "\n");
  918. }
  919. #undef SHOW_FIELD
  920. #define SHOW_FIELD(type, item, name) \
  921. do { \
  922. ret = trace_seq_printf(s, "\tfield: " #type " %s;\t" \
  923. "offset:%u;tsize:%u;\n", name, \
  924. (unsigned int)offsetof(typeof(field), item),\
  925. (unsigned int)sizeof(type)); \
  926. if (!ret) \
  927. return 0; \
  928. } while (0)
  929. static int kprobe_event_show_format(struct ftrace_event_call *call,
  930. struct trace_seq *s)
  931. {
  932. struct kprobe_trace_entry field __attribute__((unused));
  933. int ret, i;
  934. char buf[8];
  935. struct trace_probe *tp = (struct trace_probe *)call->data;
  936. SHOW_FIELD(unsigned long, ip, "ip");
  937. SHOW_FIELD(int, nargs, "nargs");
  938. /* Show fields */
  939. for (i = 0; i < tp->nr_args; i++) {
  940. sprintf(buf, "arg%d", i);
  941. SHOW_FIELD(unsigned long, args[i], buf);
  942. }
  943. trace_seq_puts(s, "\n");
  944. return __probe_event_show_format(s, tp, "%lx:", "ip");
  945. }
  946. static int kretprobe_event_show_format(struct ftrace_event_call *call,
  947. struct trace_seq *s)
  948. {
  949. struct kretprobe_trace_entry field __attribute__((unused));
  950. int ret, i;
  951. char buf[8];
  952. struct trace_probe *tp = (struct trace_probe *)call->data;
  953. SHOW_FIELD(unsigned long, func, "func");
  954. SHOW_FIELD(unsigned long, ret_ip, "ret_ip");
  955. SHOW_FIELD(int, nargs, "nargs");
  956. /* Show fields */
  957. for (i = 0; i < tp->nr_args; i++) {
  958. sprintf(buf, "arg%d", i);
  959. SHOW_FIELD(unsigned long, args[i], buf);
  960. }
  961. trace_seq_puts(s, "\n");
  962. return __probe_event_show_format(s, tp, "%lx <- %lx:",
  963. "func, ret_ip");
  964. }
  965. static int register_probe_event(struct trace_probe *tp)
  966. {
  967. struct ftrace_event_call *call = &tp->call;
  968. int ret;
  969. /* Initialize ftrace_event_call */
  970. call->system = "kprobes";
  971. if (probe_is_return(tp)) {
  972. tp->event.trace = print_kretprobe_event;
  973. call->raw_init = probe_event_raw_init;
  974. call->show_format = kretprobe_event_show_format;
  975. call->define_fields = kretprobe_event_define_fields;
  976. } else {
  977. tp->event.trace = print_kprobe_event;
  978. call->raw_init = probe_event_raw_init;
  979. call->show_format = kprobe_event_show_format;
  980. call->define_fields = kprobe_event_define_fields;
  981. }
  982. call->event = &tp->event;
  983. call->id = register_ftrace_event(&tp->event);
  984. if (!call->id)
  985. return -ENODEV;
  986. call->enabled = 1;
  987. call->regfunc = probe_event_enable;
  988. call->unregfunc = probe_event_disable;
  989. call->data = tp;
  990. ret = trace_add_event_call(call);
  991. if (ret) {
  992. pr_info("Failed to register kprobe event: %s\n", call->name);
  993. unregister_ftrace_event(&tp->event);
  994. }
  995. return ret;
  996. }
  997. static void unregister_probe_event(struct trace_probe *tp)
  998. {
  999. /* tp->event is unregistered in trace_remove_event_call() */
  1000. trace_remove_event_call(&tp->call);
  1001. }
  1002. /* Make a debugfs interface for controling probe points */
  1003. static __init int init_kprobe_trace(void)
  1004. {
  1005. struct dentry *d_tracer;
  1006. struct dentry *entry;
  1007. d_tracer = tracing_init_dentry();
  1008. if (!d_tracer)
  1009. return 0;
  1010. entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
  1011. NULL, &kprobe_events_ops);
  1012. /* Event list interface */
  1013. if (!entry)
  1014. pr_warning("Could not create debugfs "
  1015. "'kprobe_events' entry\n");
  1016. /* Profile interface */
  1017. entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
  1018. NULL, &kprobe_profile_ops);
  1019. if (!entry)
  1020. pr_warning("Could not create debugfs "
  1021. "'kprobe_profile' entry\n");
  1022. return 0;
  1023. }
  1024. fs_initcall(init_kprobe_trace);
  1025. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1026. static int kprobe_trace_selftest_target(int a1, int a2, int a3,
  1027. int a4, int a5, int a6)
  1028. {
  1029. return a1 + a2 + a3 + a4 + a5 + a6;
  1030. }
  1031. static __init int kprobe_trace_self_tests_init(void)
  1032. {
  1033. int ret;
  1034. int (*target)(int, int, int, int, int, int);
  1035. target = kprobe_trace_selftest_target;
  1036. pr_info("Testing kprobe tracing: ");
  1037. ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
  1038. "a1 a2 a3 a4 a5 a6");
  1039. if (WARN_ON_ONCE(ret))
  1040. pr_warning("error enabling function entry\n");
  1041. ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
  1042. "ra rv");
  1043. if (WARN_ON_ONCE(ret))
  1044. pr_warning("error enabling function return\n");
  1045. ret = target(1, 2, 3, 4, 5, 6);
  1046. cleanup_all_probes();
  1047. pr_cont("OK\n");
  1048. return 0;
  1049. }
  1050. late_initcall(kprobe_trace_self_tests_init);
  1051. #endif