trace_kprobe.c 28 KB

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