trace_kprobe.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. /*
  2. * kprobe based kernel tracer
  3. *
  4. * Created by Masami Hiramatsu <mhiramat@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/module.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/kprobes.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/slab.h>
  24. #include <linux/smp.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/types.h>
  27. #include <linux/string.h>
  28. #include <linux/ctype.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/perf_counter.h>
  31. #include "trace.h"
  32. #include "trace_output.h"
  33. #define MAX_TRACE_ARGS 128
  34. #define MAX_ARGSTR_LEN 63
  35. #define MAX_EVENT_NAME_LEN 64
  36. /* currently, trace_kprobe only supports X86. */
  37. struct fetch_func {
  38. unsigned long (*func)(struct pt_regs *, void *);
  39. void *data;
  40. };
  41. static __kprobes unsigned long call_fetch(struct fetch_func *f,
  42. struct pt_regs *regs)
  43. {
  44. return f->func(regs, f->data);
  45. }
  46. /* fetch handlers */
  47. static __kprobes unsigned long fetch_register(struct pt_regs *regs,
  48. void *offset)
  49. {
  50. return regs_get_register(regs, (unsigned int)((unsigned long)offset));
  51. }
  52. static __kprobes unsigned long fetch_stack(struct pt_regs *regs,
  53. void *num)
  54. {
  55. return regs_get_kernel_stack_nth(regs,
  56. (unsigned int)((unsigned long)num));
  57. }
  58. static __kprobes unsigned long fetch_memory(struct pt_regs *regs, void *addr)
  59. {
  60. unsigned long retval;
  61. if (probe_kernel_address(addr, retval))
  62. return 0;
  63. return retval;
  64. }
  65. static __kprobes unsigned long fetch_argument(struct pt_regs *regs, void *num)
  66. {
  67. return regs_get_argument_nth(regs, (unsigned int)((unsigned long)num));
  68. }
  69. static __kprobes unsigned long fetch_retvalue(struct pt_regs *regs,
  70. void *dummy)
  71. {
  72. return regs_return_value(regs);
  73. }
  74. static __kprobes unsigned long fetch_ip(struct pt_regs *regs, void *dummy)
  75. {
  76. return instruction_pointer(regs);
  77. }
  78. static __kprobes unsigned long fetch_stack_address(struct pt_regs *regs,
  79. void *dummy)
  80. {
  81. return kernel_stack_pointer(regs);
  82. }
  83. /* Memory fetching by symbol */
  84. struct symbol_cache {
  85. char *symbol;
  86. long offset;
  87. unsigned long addr;
  88. };
  89. static unsigned long update_symbol_cache(struct symbol_cache *sc)
  90. {
  91. sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
  92. if (sc->addr)
  93. sc->addr += sc->offset;
  94. return sc->addr;
  95. }
  96. static void free_symbol_cache(struct symbol_cache *sc)
  97. {
  98. kfree(sc->symbol);
  99. kfree(sc);
  100. }
  101. static struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
  102. {
  103. struct symbol_cache *sc;
  104. if (!sym || strlen(sym) == 0)
  105. return NULL;
  106. sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
  107. if (!sc)
  108. return NULL;
  109. sc->symbol = kstrdup(sym, GFP_KERNEL);
  110. if (!sc->symbol) {
  111. kfree(sc);
  112. return NULL;
  113. }
  114. sc->offset = offset;
  115. update_symbol_cache(sc);
  116. return sc;
  117. }
  118. static __kprobes unsigned long fetch_symbol(struct pt_regs *regs, void *data)
  119. {
  120. struct symbol_cache *sc = data;
  121. if (sc->addr)
  122. return fetch_memory(regs, (void *)sc->addr);
  123. else
  124. return 0;
  125. }
  126. /* Special indirect memory access interface */
  127. struct indirect_fetch_data {
  128. struct fetch_func orig;
  129. long offset;
  130. };
  131. static __kprobes unsigned long fetch_indirect(struct pt_regs *regs, void *data)
  132. {
  133. struct indirect_fetch_data *ind = data;
  134. unsigned long addr;
  135. addr = call_fetch(&ind->orig, regs);
  136. if (addr) {
  137. addr += ind->offset;
  138. return fetch_memory(regs, (void *)addr);
  139. } else
  140. return 0;
  141. }
  142. static __kprobes void free_indirect_fetch_data(struct indirect_fetch_data *data)
  143. {
  144. if (data->orig.func == fetch_indirect)
  145. free_indirect_fetch_data(data->orig.data);
  146. else if (data->orig.func == fetch_symbol)
  147. free_symbol_cache(data->orig.data);
  148. kfree(data);
  149. }
  150. /**
  151. * kprobe_trace_core
  152. */
  153. struct trace_probe {
  154. struct list_head list;
  155. struct kretprobe rp; /* Use rp.kp for kprobe use */
  156. unsigned long nhit;
  157. const char *symbol; /* symbol name */
  158. struct ftrace_event_call call;
  159. struct trace_event event;
  160. unsigned int nr_args;
  161. struct fetch_func args[];
  162. };
  163. #define SIZEOF_TRACE_PROBE(n) \
  164. (offsetof(struct trace_probe, args) + \
  165. (sizeof(struct fetch_func) * (n)))
  166. static int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs);
  167. static int kretprobe_trace_func(struct kretprobe_instance *ri,
  168. struct pt_regs *regs);
  169. static __kprobes int probe_is_return(struct trace_probe *tp)
  170. {
  171. return tp->rp.handler != NULL;
  172. }
  173. static __kprobes const char *probe_symbol(struct trace_probe *tp)
  174. {
  175. return tp->symbol ? tp->symbol : "unknown";
  176. }
  177. static int probe_arg_string(char *buf, size_t n, struct fetch_func *ff)
  178. {
  179. int ret = -EINVAL;
  180. if (ff->func == fetch_argument)
  181. ret = snprintf(buf, n, "a%lu", (unsigned long)ff->data);
  182. else if (ff->func == fetch_register) {
  183. const char *name;
  184. name = regs_query_register_name((unsigned int)((long)ff->data));
  185. ret = snprintf(buf, n, "%%%s", name);
  186. } else if (ff->func == fetch_stack)
  187. ret = snprintf(buf, n, "s%lu", (unsigned long)ff->data);
  188. else if (ff->func == fetch_memory)
  189. ret = snprintf(buf, n, "@0x%p", ff->data);
  190. else if (ff->func == fetch_symbol) {
  191. struct symbol_cache *sc = ff->data;
  192. ret = snprintf(buf, n, "@%s%+ld", sc->symbol, sc->offset);
  193. } else if (ff->func == fetch_retvalue)
  194. ret = snprintf(buf, n, "rv");
  195. else if (ff->func == fetch_ip)
  196. ret = snprintf(buf, n, "ra");
  197. else if (ff->func == fetch_stack_address)
  198. ret = snprintf(buf, n, "sa");
  199. else if (ff->func == fetch_indirect) {
  200. struct indirect_fetch_data *id = ff->data;
  201. size_t l = 0;
  202. ret = snprintf(buf, n, "%+ld(", id->offset);
  203. if (ret >= n)
  204. goto end;
  205. l += ret;
  206. ret = probe_arg_string(buf + l, n - l, &id->orig);
  207. if (ret < 0)
  208. goto end;
  209. l += ret;
  210. ret = snprintf(buf + l, n - l, ")");
  211. ret += l;
  212. }
  213. end:
  214. if (ret >= n)
  215. return -ENOSPC;
  216. return ret;
  217. }
  218. static int register_probe_event(struct trace_probe *tp);
  219. static void unregister_probe_event(struct trace_probe *tp);
  220. static DEFINE_MUTEX(probe_lock);
  221. static LIST_HEAD(probe_list);
  222. /*
  223. * Allocate new trace_probe and initialize it (including kprobes).
  224. */
  225. static struct trace_probe *alloc_trace_probe(const char *event,
  226. void *addr,
  227. const char *symbol,
  228. unsigned long offs,
  229. int nargs, int is_return)
  230. {
  231. struct trace_probe *tp;
  232. tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
  233. if (!tp)
  234. return ERR_PTR(-ENOMEM);
  235. if (symbol) {
  236. tp->symbol = kstrdup(symbol, GFP_KERNEL);
  237. if (!tp->symbol)
  238. goto error;
  239. tp->rp.kp.symbol_name = tp->symbol;
  240. tp->rp.kp.offset = offs;
  241. } else
  242. tp->rp.kp.addr = addr;
  243. /* Set handler here for checking whether this probe is return or not. */
  244. if (is_return)
  245. tp->rp.handler = kretprobe_trace_func;
  246. else
  247. tp->rp.kp.pre_handler = kprobe_trace_func;
  248. if (!event)
  249. goto error;
  250. tp->call.name = kstrdup(event, GFP_KERNEL);
  251. if (!tp->call.name)
  252. goto error;
  253. INIT_LIST_HEAD(&tp->list);
  254. return tp;
  255. error:
  256. kfree(tp->symbol);
  257. kfree(tp);
  258. return ERR_PTR(-ENOMEM);
  259. }
  260. static void free_trace_probe(struct trace_probe *tp)
  261. {
  262. int i;
  263. for (i = 0; i < tp->nr_args; i++)
  264. if (tp->args[i].func == fetch_symbol)
  265. free_symbol_cache(tp->args[i].data);
  266. else if (tp->args[i].func == fetch_indirect)
  267. free_indirect_fetch_data(tp->args[i].data);
  268. kfree(tp->call.name);
  269. kfree(tp->symbol);
  270. kfree(tp);
  271. }
  272. static struct trace_probe *find_probe_event(const char *event)
  273. {
  274. struct trace_probe *tp;
  275. list_for_each_entry(tp, &probe_list, list)
  276. if (!strcmp(tp->call.name, event))
  277. return tp;
  278. return NULL;
  279. }
  280. static void __unregister_trace_probe(struct trace_probe *tp)
  281. {
  282. if (probe_is_return(tp))
  283. unregister_kretprobe(&tp->rp);
  284. else
  285. unregister_kprobe(&tp->rp.kp);
  286. }
  287. /* Unregister a trace_probe and probe_event: call with locking probe_lock */
  288. static void unregister_trace_probe(struct trace_probe *tp)
  289. {
  290. unregister_probe_event(tp);
  291. __unregister_trace_probe(tp);
  292. list_del(&tp->list);
  293. }
  294. /* Register a trace_probe and probe_event */
  295. static int register_trace_probe(struct trace_probe *tp)
  296. {
  297. struct trace_probe *old_tp;
  298. int ret;
  299. mutex_lock(&probe_lock);
  300. if (probe_is_return(tp))
  301. ret = register_kretprobe(&tp->rp);
  302. else
  303. ret = register_kprobe(&tp->rp.kp);
  304. if (ret) {
  305. pr_warning("Could not insert probe(%d)\n", ret);
  306. if (ret == -EILSEQ) {
  307. pr_warning("Probing address(0x%p) is not an "
  308. "instruction boundary.\n",
  309. tp->rp.kp.addr);
  310. ret = -EINVAL;
  311. }
  312. goto end;
  313. }
  314. /* register as an event */
  315. old_tp = find_probe_event(tp->call.name);
  316. if (old_tp) {
  317. /* delete old event */
  318. unregister_trace_probe(old_tp);
  319. free_trace_probe(old_tp);
  320. }
  321. ret = register_probe_event(tp);
  322. if (ret) {
  323. pr_warning("Faild to register probe event(%d)\n", ret);
  324. __unregister_trace_probe(tp);
  325. }
  326. list_add_tail(&tp->list, &probe_list);
  327. end:
  328. mutex_unlock(&probe_lock);
  329. return ret;
  330. }
  331. /* Split symbol and offset. */
  332. static int split_symbol_offset(char *symbol, unsigned long *offset)
  333. {
  334. char *tmp;
  335. int ret;
  336. if (!offset)
  337. return -EINVAL;
  338. tmp = strchr(symbol, '+');
  339. if (tmp) {
  340. /* skip sign because strict_strtol doesn't accept '+' */
  341. ret = strict_strtoul(tmp + 1, 0, offset);
  342. if (ret)
  343. return ret;
  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_probe_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_probe_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]|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. int i, ret = 0;
  477. int is_return = 0;
  478. char *symbol = NULL, *event = NULL;
  479. unsigned long offset = 0;
  480. void *addr = NULL;
  481. char buf[MAX_EVENT_NAME_LEN];
  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. if (symbol)
  519. snprintf(buf, MAX_EVENT_NAME_LEN, "%c@%s%+ld",
  520. is_return ? 'r' : 'p', symbol, offset);
  521. else
  522. snprintf(buf, MAX_EVENT_NAME_LEN, "%c@0x%p",
  523. is_return ? 'r' : 'p', addr);
  524. event = buf;
  525. }
  526. tp = alloc_trace_probe(event, addr, symbol, offset, argc, is_return);
  527. if (IS_ERR(tp))
  528. return PTR_ERR(tp);
  529. /* parse arguments */
  530. ret = 0;
  531. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  532. if (strlen(argv[i]) > MAX_ARGSTR_LEN) {
  533. pr_info("Argument%d(%s) is too long.\n", i, argv[i]);
  534. ret = -ENOSPC;
  535. goto error;
  536. }
  537. ret = parse_probe_arg(argv[i], &tp->args[i], is_return);
  538. if (ret)
  539. goto error;
  540. }
  541. tp->nr_args = i;
  542. ret = register_trace_probe(tp);
  543. if (ret)
  544. goto error;
  545. return 0;
  546. error:
  547. free_trace_probe(tp);
  548. return ret;
  549. }
  550. static void cleanup_all_probes(void)
  551. {
  552. struct trace_probe *tp;
  553. mutex_lock(&probe_lock);
  554. /* TODO: Use batch unregistration */
  555. while (!list_empty(&probe_list)) {
  556. tp = list_entry(probe_list.next, struct trace_probe, list);
  557. unregister_trace_probe(tp);
  558. free_trace_probe(tp);
  559. }
  560. mutex_unlock(&probe_lock);
  561. }
  562. /* Probes listing interfaces */
  563. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  564. {
  565. mutex_lock(&probe_lock);
  566. return seq_list_start(&probe_list, *pos);
  567. }
  568. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  569. {
  570. return seq_list_next(v, &probe_list, pos);
  571. }
  572. static void probes_seq_stop(struct seq_file *m, void *v)
  573. {
  574. mutex_unlock(&probe_lock);
  575. }
  576. static int probes_seq_show(struct seq_file *m, void *v)
  577. {
  578. struct trace_probe *tp = v;
  579. int i, ret;
  580. char buf[MAX_ARGSTR_LEN + 1];
  581. seq_printf(m, "%c", probe_is_return(tp) ? 'r' : 'p');
  582. seq_printf(m, ":%s", tp->call.name);
  583. if (tp->symbol)
  584. seq_printf(m, " %s+%u", probe_symbol(tp), tp->rp.kp.offset);
  585. else
  586. seq_printf(m, " 0x%p", tp->rp.kp.addr);
  587. for (i = 0; i < tp->nr_args; i++) {
  588. ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
  589. if (ret < 0) {
  590. pr_warning("Argument%d decoding error(%d).\n", i, ret);
  591. return ret;
  592. }
  593. seq_printf(m, " %s", buf);
  594. }
  595. seq_printf(m, "\n");
  596. return 0;
  597. }
  598. static const struct seq_operations probes_seq_op = {
  599. .start = probes_seq_start,
  600. .next = probes_seq_next,
  601. .stop = probes_seq_stop,
  602. .show = probes_seq_show
  603. };
  604. static int probes_open(struct inode *inode, struct file *file)
  605. {
  606. if ((file->f_mode & FMODE_WRITE) &&
  607. (file->f_flags & O_TRUNC))
  608. cleanup_all_probes();
  609. return seq_open(file, &probes_seq_op);
  610. }
  611. static int command_trace_probe(const char *buf)
  612. {
  613. char **argv;
  614. int argc = 0, ret = 0;
  615. argv = argv_split(GFP_KERNEL, buf, &argc);
  616. if (!argv)
  617. return -ENOMEM;
  618. if (argc)
  619. ret = create_trace_probe(argc, argv);
  620. argv_free(argv);
  621. return ret;
  622. }
  623. #define WRITE_BUFSIZE 128
  624. static ssize_t probes_write(struct file *file, const char __user *buffer,
  625. size_t count, loff_t *ppos)
  626. {
  627. char *kbuf, *tmp;
  628. int ret;
  629. size_t done;
  630. size_t size;
  631. kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
  632. if (!kbuf)
  633. return -ENOMEM;
  634. ret = done = 0;
  635. while (done < count) {
  636. size = count - done;
  637. if (size >= WRITE_BUFSIZE)
  638. size = WRITE_BUFSIZE - 1;
  639. if (copy_from_user(kbuf, buffer + done, size)) {
  640. ret = -EFAULT;
  641. goto out;
  642. }
  643. kbuf[size] = '\0';
  644. tmp = strchr(kbuf, '\n');
  645. if (tmp) {
  646. *tmp = '\0';
  647. size = tmp - kbuf + 1;
  648. } else if (done + size < count) {
  649. pr_warning("Line length is too long: "
  650. "Should be less than %d.", WRITE_BUFSIZE);
  651. ret = -EINVAL;
  652. goto out;
  653. }
  654. done += size;
  655. /* Remove comments */
  656. tmp = strchr(kbuf, '#');
  657. if (tmp)
  658. *tmp = '\0';
  659. ret = command_trace_probe(kbuf);
  660. if (ret)
  661. goto out;
  662. }
  663. ret = done;
  664. out:
  665. kfree(kbuf);
  666. return ret;
  667. }
  668. static const struct file_operations kprobe_events_ops = {
  669. .owner = THIS_MODULE,
  670. .open = probes_open,
  671. .read = seq_read,
  672. .llseek = seq_lseek,
  673. .release = seq_release,
  674. .write = probes_write,
  675. };
  676. /* Probes profiling interfaces */
  677. static int probes_profile_seq_show(struct seq_file *m, void *v)
  678. {
  679. struct trace_probe *tp = v;
  680. seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
  681. tp->rp.kp.nmissed);
  682. return 0;
  683. }
  684. static const struct seq_operations profile_seq_op = {
  685. .start = probes_seq_start,
  686. .next = probes_seq_next,
  687. .stop = probes_seq_stop,
  688. .show = probes_profile_seq_show
  689. };
  690. static int profile_open(struct inode *inode, struct file *file)
  691. {
  692. return seq_open(file, &profile_seq_op);
  693. }
  694. static const struct file_operations kprobe_profile_ops = {
  695. .owner = THIS_MODULE,
  696. .open = profile_open,
  697. .read = seq_read,
  698. .llseek = seq_lseek,
  699. .release = seq_release,
  700. };
  701. /* Kprobe handler */
  702. static __kprobes int kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
  703. {
  704. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  705. struct kprobe_trace_entry *entry;
  706. struct ring_buffer_event *event;
  707. struct ring_buffer *buffer;
  708. int size, i, pc;
  709. unsigned long irq_flags;
  710. struct ftrace_event_call *call = &tp->call;
  711. tp->nhit++;
  712. local_save_flags(irq_flags);
  713. pc = preempt_count();
  714. size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
  715. event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
  716. irq_flags, pc);
  717. if (!event)
  718. return 0;
  719. entry = ring_buffer_event_data(event);
  720. entry->nargs = tp->nr_args;
  721. entry->ip = (unsigned long)kp->addr;
  722. for (i = 0; i < tp->nr_args; i++)
  723. entry->args[i] = call_fetch(&tp->args[i], regs);
  724. if (!filter_current_check_discard(buffer, call, entry, event))
  725. trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
  726. return 0;
  727. }
  728. /* Kretprobe handler */
  729. static __kprobes int kretprobe_trace_func(struct kretprobe_instance *ri,
  730. struct pt_regs *regs)
  731. {
  732. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  733. struct kretprobe_trace_entry *entry;
  734. struct ring_buffer_event *event;
  735. struct ring_buffer *buffer;
  736. int size, i, pc;
  737. unsigned long irq_flags;
  738. struct ftrace_event_call *call = &tp->call;
  739. local_save_flags(irq_flags);
  740. pc = preempt_count();
  741. size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
  742. event = trace_current_buffer_lock_reserve(&buffer, call->id, size,
  743. irq_flags, pc);
  744. if (!event)
  745. return 0;
  746. entry = ring_buffer_event_data(event);
  747. entry->nargs = tp->nr_args;
  748. entry->func = (unsigned long)tp->rp.kp.addr;
  749. entry->ret_ip = (unsigned long)ri->ret_addr;
  750. for (i = 0; i < tp->nr_args; i++)
  751. entry->args[i] = call_fetch(&tp->args[i], regs);
  752. if (!filter_current_check_discard(buffer, call, entry, event))
  753. trace_nowake_buffer_unlock_commit(buffer, event, irq_flags, pc);
  754. return 0;
  755. }
  756. /* Event entry printers */
  757. enum print_line_t
  758. print_kprobe_event(struct trace_iterator *iter, int flags)
  759. {
  760. struct kprobe_trace_entry *field;
  761. struct trace_seq *s = &iter->seq;
  762. int i;
  763. field = (struct kprobe_trace_entry *)iter->ent;
  764. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  765. goto partial;
  766. if (!trace_seq_puts(s, ":"))
  767. goto partial;
  768. for (i = 0; i < field->nargs; i++)
  769. if (!trace_seq_printf(s, " 0x%lx", field->args[i]))
  770. goto partial;
  771. if (!trace_seq_puts(s, "\n"))
  772. goto partial;
  773. return TRACE_TYPE_HANDLED;
  774. partial:
  775. return TRACE_TYPE_PARTIAL_LINE;
  776. }
  777. enum print_line_t
  778. print_kretprobe_event(struct trace_iterator *iter, int flags)
  779. {
  780. struct kretprobe_trace_entry *field;
  781. struct trace_seq *s = &iter->seq;
  782. int i;
  783. field = (struct kretprobe_trace_entry *)iter->ent;
  784. if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
  785. goto partial;
  786. if (!trace_seq_puts(s, " <- "))
  787. goto partial;
  788. if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
  789. goto partial;
  790. if (!trace_seq_puts(s, ":"))
  791. goto partial;
  792. for (i = 0; i < field->nargs; i++)
  793. if (!trace_seq_printf(s, " 0x%lx", field->args[i]))
  794. goto partial;
  795. if (!trace_seq_puts(s, "\n"))
  796. goto partial;
  797. return TRACE_TYPE_HANDLED;
  798. partial:
  799. return TRACE_TYPE_PARTIAL_LINE;
  800. }
  801. static int probe_event_enable(struct ftrace_event_call *call)
  802. {
  803. struct trace_probe *tp = (struct trace_probe *)call->data;
  804. if (probe_is_return(tp)) {
  805. tp->rp.handler = kretprobe_trace_func;
  806. return enable_kretprobe(&tp->rp);
  807. } else {
  808. tp->rp.kp.pre_handler = kprobe_trace_func;
  809. return enable_kprobe(&tp->rp.kp);
  810. }
  811. }
  812. static void probe_event_disable(struct ftrace_event_call *call)
  813. {
  814. struct trace_probe *tp = (struct trace_probe *)call->data;
  815. if (probe_is_return(tp))
  816. disable_kretprobe(&tp->rp);
  817. else
  818. disable_kprobe(&tp->rp.kp);
  819. }
  820. static int probe_event_raw_init(struct ftrace_event_call *event_call)
  821. {
  822. INIT_LIST_HEAD(&event_call->fields);
  823. return 0;
  824. }
  825. #undef DEFINE_FIELD
  826. #define DEFINE_FIELD(type, item, name, is_signed) \
  827. do { \
  828. ret = trace_define_field(event_call, #type, name, \
  829. offsetof(typeof(field), item), \
  830. sizeof(field.item), is_signed, \
  831. FILTER_OTHER); \
  832. if (ret) \
  833. return ret; \
  834. } while (0)
  835. static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
  836. {
  837. int ret, i;
  838. struct kprobe_trace_entry field;
  839. char buf[MAX_ARGSTR_LEN + 1];
  840. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  841. ret = trace_define_common_fields(event_call);
  842. if (!ret)
  843. return ret;
  844. DEFINE_FIELD(unsigned long, ip, "ip", 0);
  845. DEFINE_FIELD(int, nargs, "nargs", 1);
  846. for (i = 0; i < tp->nr_args; i++) {
  847. /* Set argN as a field */
  848. sprintf(buf, "arg%d", i);
  849. DEFINE_FIELD(unsigned long, args[i], buf, 0);
  850. /* Set argument string as an alias field */
  851. ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
  852. if (ret < 0)
  853. return ret;
  854. DEFINE_FIELD(unsigned long, args[i], buf, 0);
  855. }
  856. return 0;
  857. }
  858. static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
  859. {
  860. int ret, i;
  861. struct kretprobe_trace_entry field;
  862. char buf[MAX_ARGSTR_LEN + 1];
  863. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  864. ret = trace_define_common_fields(event_call);
  865. if (!ret)
  866. return ret;
  867. DEFINE_FIELD(unsigned long, func, "func", 0);
  868. DEFINE_FIELD(unsigned long, ret_ip, "ret_ip", 0);
  869. DEFINE_FIELD(int, nargs, "nargs", 1);
  870. for (i = 0; i < tp->nr_args; i++) {
  871. /* Set argN as a field */
  872. sprintf(buf, "arg%d", i);
  873. DEFINE_FIELD(unsigned long, args[i], buf, 0);
  874. /* Set argument string as an alias field */
  875. ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
  876. if (ret < 0)
  877. return ret;
  878. DEFINE_FIELD(unsigned long, args[i], buf, 0);
  879. }
  880. return 0;
  881. }
  882. static int __probe_event_show_format(struct trace_seq *s,
  883. struct trace_probe *tp, const char *fmt,
  884. const char *arg)
  885. {
  886. int i, ret;
  887. char buf[MAX_ARGSTR_LEN + 1];
  888. /* Show aliases */
  889. for (i = 0; i < tp->nr_args; i++) {
  890. ret = probe_arg_string(buf, MAX_ARGSTR_LEN, &tp->args[i]);
  891. if (ret < 0)
  892. return ret;
  893. if (!trace_seq_printf(s, "\talias: %s;\toriginal: arg%d;\n",
  894. buf, i))
  895. return 0;
  896. }
  897. /* Show format */
  898. if (!trace_seq_printf(s, "\nprint fmt: \"%s", fmt))
  899. return 0;
  900. for (i = 0; i < tp->nr_args; i++)
  901. if (!trace_seq_puts(s, " 0x%lx"))
  902. return 0;
  903. if (!trace_seq_printf(s, "\", %s", arg))
  904. return 0;
  905. for (i = 0; i < tp->nr_args; i++)
  906. if (!trace_seq_printf(s, ", arg%d", i))
  907. return 0;
  908. return trace_seq_puts(s, "\n");
  909. }
  910. #undef SHOW_FIELD
  911. #define SHOW_FIELD(type, item, name) \
  912. do { \
  913. ret = trace_seq_printf(s, "\tfield: " #type " %s;\t" \
  914. "offset:%u;\tsize:%u;\n", name, \
  915. (unsigned int)offsetof(typeof(field), item),\
  916. (unsigned int)sizeof(type)); \
  917. if (!ret) \
  918. return 0; \
  919. } while (0)
  920. static int kprobe_event_show_format(struct ftrace_event_call *call,
  921. struct trace_seq *s)
  922. {
  923. struct kprobe_trace_entry field __attribute__((unused));
  924. int ret, i;
  925. char buf[8];
  926. struct trace_probe *tp = (struct trace_probe *)call->data;
  927. SHOW_FIELD(unsigned long, ip, "ip");
  928. SHOW_FIELD(int, nargs, "nargs");
  929. /* Show fields */
  930. for (i = 0; i < tp->nr_args; i++) {
  931. sprintf(buf, "arg%d", i);
  932. SHOW_FIELD(unsigned long, args[i], buf);
  933. }
  934. trace_seq_puts(s, "\n");
  935. return __probe_event_show_format(s, tp, "%lx:", "ip");
  936. }
  937. static int kretprobe_event_show_format(struct ftrace_event_call *call,
  938. struct trace_seq *s)
  939. {
  940. struct kretprobe_trace_entry field __attribute__((unused));
  941. int ret, i;
  942. char buf[8];
  943. struct trace_probe *tp = (struct trace_probe *)call->data;
  944. SHOW_FIELD(unsigned long, func, "func");
  945. SHOW_FIELD(unsigned long, ret_ip, "ret_ip");
  946. SHOW_FIELD(int, nargs, "nargs");
  947. /* Show fields */
  948. for (i = 0; i < tp->nr_args; i++) {
  949. sprintf(buf, "arg%d", i);
  950. SHOW_FIELD(unsigned long, args[i], buf);
  951. }
  952. trace_seq_puts(s, "\n");
  953. return __probe_event_show_format(s, tp, "%lx <- %lx:",
  954. "func, ret_ip");
  955. }
  956. #ifdef CONFIG_EVENT_PROFILE
  957. /* Kprobe profile handler */
  958. static __kprobes int kprobe_profile_func(struct kprobe *kp,
  959. struct pt_regs *regs)
  960. {
  961. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  962. struct ftrace_event_call *call = &tp->call;
  963. struct kprobe_trace_entry *entry;
  964. int size, i, pc;
  965. unsigned long irq_flags;
  966. local_save_flags(irq_flags);
  967. pc = preempt_count();
  968. size = SIZEOF_KPROBE_TRACE_ENTRY(tp->nr_args);
  969. do {
  970. char raw_data[size];
  971. struct trace_entry *ent;
  972. *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
  973. entry = (struct kprobe_trace_entry *)raw_data;
  974. ent = &entry->ent;
  975. tracing_generic_entry_update(ent, irq_flags, pc);
  976. ent->type = call->id;
  977. entry->nargs = tp->nr_args;
  978. entry->ip = (unsigned long)kp->addr;
  979. for (i = 0; i < tp->nr_args; i++)
  980. entry->args[i] = call_fetch(&tp->args[i], regs);
  981. perf_tpcounter_event(call->id, entry->ip, 1, entry, size);
  982. } while (0);
  983. return 0;
  984. }
  985. /* Kretprobe profile handler */
  986. static __kprobes int kretprobe_profile_func(struct kretprobe_instance *ri,
  987. struct pt_regs *regs)
  988. {
  989. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  990. struct ftrace_event_call *call = &tp->call;
  991. struct kretprobe_trace_entry *entry;
  992. int size, i, pc;
  993. unsigned long irq_flags;
  994. local_save_flags(irq_flags);
  995. pc = preempt_count();
  996. size = SIZEOF_KRETPROBE_TRACE_ENTRY(tp->nr_args);
  997. do {
  998. char raw_data[size];
  999. struct trace_entry *ent;
  1000. *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
  1001. entry = (struct kretprobe_trace_entry *)raw_data;
  1002. ent = &entry->ent;
  1003. tracing_generic_entry_update(ent, irq_flags, pc);
  1004. ent->type = call->id;
  1005. entry->nargs = tp->nr_args;
  1006. entry->func = (unsigned long)tp->rp.kp.addr;
  1007. entry->ret_ip = (unsigned long)ri->ret_addr;
  1008. for (i = 0; i < tp->nr_args; i++)
  1009. entry->args[i] = call_fetch(&tp->args[i], regs);
  1010. perf_tpcounter_event(call->id, entry->ret_ip, 1, entry, size);
  1011. } while (0);
  1012. return 0;
  1013. }
  1014. static int probe_profile_enable(struct ftrace_event_call *call)
  1015. {
  1016. struct trace_probe *tp = (struct trace_probe *)call->data;
  1017. if (atomic_inc_return(&call->profile_count))
  1018. return 0;
  1019. if (probe_is_return(tp)) {
  1020. tp->rp.handler = kretprobe_profile_func;
  1021. return enable_kretprobe(&tp->rp);
  1022. } else {
  1023. tp->rp.kp.pre_handler = kprobe_profile_func;
  1024. return enable_kprobe(&tp->rp.kp);
  1025. }
  1026. }
  1027. static void probe_profile_disable(struct ftrace_event_call *call)
  1028. {
  1029. if (atomic_add_negative(-1, &call->profile_count))
  1030. probe_event_disable(call);
  1031. }
  1032. #endif /* CONFIG_EVENT_PROFILE */
  1033. static int register_probe_event(struct trace_probe *tp)
  1034. {
  1035. struct ftrace_event_call *call = &tp->call;
  1036. int ret;
  1037. /* Initialize ftrace_event_call */
  1038. call->system = "kprobes";
  1039. if (probe_is_return(tp)) {
  1040. tp->event.trace = print_kretprobe_event;
  1041. call->raw_init = probe_event_raw_init;
  1042. call->show_format = kretprobe_event_show_format;
  1043. call->define_fields = kretprobe_event_define_fields;
  1044. } else {
  1045. tp->event.trace = print_kprobe_event;
  1046. call->raw_init = probe_event_raw_init;
  1047. call->show_format = kprobe_event_show_format;
  1048. call->define_fields = kprobe_event_define_fields;
  1049. }
  1050. call->event = &tp->event;
  1051. call->id = register_ftrace_event(&tp->event);
  1052. if (!call->id)
  1053. return -ENODEV;
  1054. call->enabled = 1;
  1055. call->regfunc = probe_event_enable;
  1056. call->unregfunc = probe_event_disable;
  1057. #ifdef CONFIG_EVENT_PROFILE
  1058. atomic_set(&call->profile_count, -1);
  1059. call->profile_enable = probe_profile_enable;
  1060. call->profile_disable = probe_profile_disable;
  1061. #endif
  1062. call->data = tp;
  1063. ret = trace_add_event_call(call);
  1064. if (ret) {
  1065. pr_info("Failed to register kprobe event: %s\n", call->name);
  1066. unregister_ftrace_event(&tp->event);
  1067. }
  1068. return ret;
  1069. }
  1070. static void unregister_probe_event(struct trace_probe *tp)
  1071. {
  1072. /* tp->event is unregistered in trace_remove_event_call() */
  1073. trace_remove_event_call(&tp->call);
  1074. }
  1075. /* Make a debugfs interface for controling probe points */
  1076. static __init int init_kprobe_trace(void)
  1077. {
  1078. struct dentry *d_tracer;
  1079. struct dentry *entry;
  1080. d_tracer = tracing_init_dentry();
  1081. if (!d_tracer)
  1082. return 0;
  1083. entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
  1084. NULL, &kprobe_events_ops);
  1085. /* Event list interface */
  1086. if (!entry)
  1087. pr_warning("Could not create debugfs "
  1088. "'kprobe_events' entry\n");
  1089. /* Profile interface */
  1090. entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
  1091. NULL, &kprobe_profile_ops);
  1092. if (!entry)
  1093. pr_warning("Could not create debugfs "
  1094. "'kprobe_profile' entry\n");
  1095. return 0;
  1096. }
  1097. fs_initcall(init_kprobe_trace);
  1098. #ifdef CONFIG_FTRACE_STARTUP_TEST
  1099. static int kprobe_trace_selftest_target(int a1, int a2, int a3,
  1100. int a4, int a5, int a6)
  1101. {
  1102. return a1 + a2 + a3 + a4 + a5 + a6;
  1103. }
  1104. static __init int kprobe_trace_self_tests_init(void)
  1105. {
  1106. int ret;
  1107. int (*target)(int, int, int, int, int, int);
  1108. target = kprobe_trace_selftest_target;
  1109. pr_info("Testing kprobe tracing: ");
  1110. ret = command_trace_probe("p:testprobe kprobe_trace_selftest_target "
  1111. "a1 a2 a3 a4 a5 a6");
  1112. if (WARN_ON_ONCE(ret))
  1113. pr_warning("error enabling function entry\n");
  1114. ret = command_trace_probe("r:testprobe2 kprobe_trace_selftest_target "
  1115. "ra rv");
  1116. if (WARN_ON_ONCE(ret))
  1117. pr_warning("error enabling function return\n");
  1118. ret = target(1, 2, 3, 4, 5, 6);
  1119. cleanup_all_probes();
  1120. pr_cont("OK\n");
  1121. return 0;
  1122. }
  1123. late_initcall(kprobe_trace_self_tests_init);
  1124. #endif