trace_kprobe.c 31 KB

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