trace_kprobe.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. /*
  2. * Kprobes-based tracing events
  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 "trace_probe.h"
  22. #define KPROBE_EVENT_SYSTEM "kprobes"
  23. /**
  24. * Kprobe event core functions
  25. */
  26. struct trace_probe {
  27. struct list_head list;
  28. struct kretprobe rp; /* Use rp.kp for kprobe use */
  29. unsigned long nhit;
  30. unsigned int flags; /* For TP_FLAG_* */
  31. const char *symbol; /* symbol name */
  32. struct ftrace_event_class class;
  33. struct ftrace_event_call call;
  34. ssize_t size; /* trace entry size */
  35. unsigned int nr_args;
  36. struct probe_arg args[];
  37. };
  38. #define SIZEOF_TRACE_PROBE(n) \
  39. (offsetof(struct trace_probe, args) + \
  40. (sizeof(struct probe_arg) * (n)))
  41. static __kprobes bool trace_probe_is_return(struct trace_probe *tp)
  42. {
  43. return tp->rp.handler != NULL;
  44. }
  45. static __kprobes const char *trace_probe_symbol(struct trace_probe *tp)
  46. {
  47. return tp->symbol ? tp->symbol : "unknown";
  48. }
  49. static __kprobes unsigned long trace_probe_offset(struct trace_probe *tp)
  50. {
  51. return tp->rp.kp.offset;
  52. }
  53. static __kprobes bool trace_probe_is_enabled(struct trace_probe *tp)
  54. {
  55. return !!(tp->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE));
  56. }
  57. static __kprobes bool trace_probe_is_registered(struct trace_probe *tp)
  58. {
  59. return !!(tp->flags & TP_FLAG_REGISTERED);
  60. }
  61. static __kprobes bool trace_probe_has_gone(struct trace_probe *tp)
  62. {
  63. return !!(kprobe_gone(&tp->rp.kp));
  64. }
  65. static __kprobes bool trace_probe_within_module(struct trace_probe *tp,
  66. struct module *mod)
  67. {
  68. int len = strlen(mod->name);
  69. const char *name = trace_probe_symbol(tp);
  70. return strncmp(mod->name, name, len) == 0 && name[len] == ':';
  71. }
  72. static __kprobes bool trace_probe_is_on_module(struct trace_probe *tp)
  73. {
  74. return !!strchr(trace_probe_symbol(tp), ':');
  75. }
  76. static int register_probe_event(struct trace_probe *tp);
  77. static void unregister_probe_event(struct trace_probe *tp);
  78. static DEFINE_MUTEX(probe_lock);
  79. static LIST_HEAD(probe_list);
  80. static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
  81. static int kretprobe_dispatcher(struct kretprobe_instance *ri,
  82. struct pt_regs *regs);
  83. /*
  84. * Allocate new trace_probe and initialize it (including kprobes).
  85. */
  86. static struct trace_probe *alloc_trace_probe(const char *group,
  87. const char *event,
  88. void *addr,
  89. const char *symbol,
  90. unsigned long offs,
  91. int nargs, bool is_return)
  92. {
  93. struct trace_probe *tp;
  94. int ret = -ENOMEM;
  95. tp = kzalloc(SIZEOF_TRACE_PROBE(nargs), GFP_KERNEL);
  96. if (!tp)
  97. return ERR_PTR(ret);
  98. if (symbol) {
  99. tp->symbol = kstrdup(symbol, GFP_KERNEL);
  100. if (!tp->symbol)
  101. goto error;
  102. tp->rp.kp.symbol_name = tp->symbol;
  103. tp->rp.kp.offset = offs;
  104. } else
  105. tp->rp.kp.addr = addr;
  106. if (is_return)
  107. tp->rp.handler = kretprobe_dispatcher;
  108. else
  109. tp->rp.kp.pre_handler = kprobe_dispatcher;
  110. if (!event || !is_good_name(event)) {
  111. ret = -EINVAL;
  112. goto error;
  113. }
  114. tp->call.class = &tp->class;
  115. tp->call.name = kstrdup(event, GFP_KERNEL);
  116. if (!tp->call.name)
  117. goto error;
  118. if (!group || !is_good_name(group)) {
  119. ret = -EINVAL;
  120. goto error;
  121. }
  122. tp->class.system = kstrdup(group, GFP_KERNEL);
  123. if (!tp->class.system)
  124. goto error;
  125. INIT_LIST_HEAD(&tp->list);
  126. return tp;
  127. error:
  128. kfree(tp->call.name);
  129. kfree(tp->symbol);
  130. kfree(tp);
  131. return ERR_PTR(ret);
  132. }
  133. static void free_trace_probe(struct trace_probe *tp)
  134. {
  135. int i;
  136. for (i = 0; i < tp->nr_args; i++)
  137. traceprobe_free_probe_arg(&tp->args[i]);
  138. kfree(tp->call.class->system);
  139. kfree(tp->call.name);
  140. kfree(tp->symbol);
  141. kfree(tp);
  142. }
  143. static struct trace_probe *find_trace_probe(const char *event,
  144. const char *group)
  145. {
  146. struct trace_probe *tp;
  147. list_for_each_entry(tp, &probe_list, list)
  148. if (strcmp(tp->call.name, event) == 0 &&
  149. strcmp(tp->call.class->system, group) == 0)
  150. return tp;
  151. return NULL;
  152. }
  153. /* Enable trace_probe - @flag must be TP_FLAG_TRACE or TP_FLAG_PROFILE */
  154. static int enable_trace_probe(struct trace_probe *tp, int flag)
  155. {
  156. int ret = 0;
  157. tp->flags |= flag;
  158. if (trace_probe_is_enabled(tp) && trace_probe_is_registered(tp) &&
  159. !trace_probe_has_gone(tp)) {
  160. if (trace_probe_is_return(tp))
  161. ret = enable_kretprobe(&tp->rp);
  162. else
  163. ret = enable_kprobe(&tp->rp.kp);
  164. }
  165. return ret;
  166. }
  167. /* Disable trace_probe - @flag must be TP_FLAG_TRACE or TP_FLAG_PROFILE */
  168. static void disable_trace_probe(struct trace_probe *tp, int flag)
  169. {
  170. tp->flags &= ~flag;
  171. if (!trace_probe_is_enabled(tp) && trace_probe_is_registered(tp)) {
  172. if (trace_probe_is_return(tp))
  173. disable_kretprobe(&tp->rp);
  174. else
  175. disable_kprobe(&tp->rp.kp);
  176. }
  177. }
  178. /* Internal register function - just handle k*probes and flags */
  179. static int __register_trace_probe(struct trace_probe *tp)
  180. {
  181. int i, ret;
  182. if (trace_probe_is_registered(tp))
  183. return -EINVAL;
  184. for (i = 0; i < tp->nr_args; i++)
  185. traceprobe_update_arg(&tp->args[i]);
  186. /* Set/clear disabled flag according to tp->flag */
  187. if (trace_probe_is_enabled(tp))
  188. tp->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
  189. else
  190. tp->rp.kp.flags |= KPROBE_FLAG_DISABLED;
  191. if (trace_probe_is_return(tp))
  192. ret = register_kretprobe(&tp->rp);
  193. else
  194. ret = register_kprobe(&tp->rp.kp);
  195. if (ret == 0)
  196. tp->flags |= TP_FLAG_REGISTERED;
  197. else {
  198. pr_warning("Could not insert probe at %s+%lu: %d\n",
  199. trace_probe_symbol(tp), trace_probe_offset(tp), ret);
  200. if (ret == -ENOENT && trace_probe_is_on_module(tp)) {
  201. pr_warning("This probe might be able to register after"
  202. "target module is loaded. Continue.\n");
  203. ret = 0;
  204. } else if (ret == -EILSEQ) {
  205. pr_warning("Probing address(0x%p) is not an "
  206. "instruction boundary.\n",
  207. tp->rp.kp.addr);
  208. ret = -EINVAL;
  209. }
  210. }
  211. return ret;
  212. }
  213. /* Internal unregister function - just handle k*probes and flags */
  214. static void __unregister_trace_probe(struct trace_probe *tp)
  215. {
  216. if (trace_probe_is_registered(tp)) {
  217. if (trace_probe_is_return(tp))
  218. unregister_kretprobe(&tp->rp);
  219. else
  220. unregister_kprobe(&tp->rp.kp);
  221. tp->flags &= ~TP_FLAG_REGISTERED;
  222. /* Cleanup kprobe for reuse */
  223. if (tp->rp.kp.symbol_name)
  224. tp->rp.kp.addr = NULL;
  225. }
  226. }
  227. /* Unregister a trace_probe and probe_event: call with locking probe_lock */
  228. static int unregister_trace_probe(struct trace_probe *tp)
  229. {
  230. /* Enabled event can not be unregistered */
  231. if (trace_probe_is_enabled(tp))
  232. return -EBUSY;
  233. __unregister_trace_probe(tp);
  234. list_del(&tp->list);
  235. unregister_probe_event(tp);
  236. return 0;
  237. }
  238. /* Register a trace_probe and probe_event */
  239. static int register_trace_probe(struct trace_probe *tp)
  240. {
  241. struct trace_probe *old_tp;
  242. int ret;
  243. mutex_lock(&probe_lock);
  244. /* Delete old (same name) event if exist */
  245. old_tp = find_trace_probe(tp->call.name, tp->call.class->system);
  246. if (old_tp) {
  247. ret = unregister_trace_probe(old_tp);
  248. if (ret < 0)
  249. goto end;
  250. free_trace_probe(old_tp);
  251. }
  252. /* Register new event */
  253. ret = register_probe_event(tp);
  254. if (ret) {
  255. pr_warning("Failed to register probe event(%d)\n", ret);
  256. goto end;
  257. }
  258. /* Register k*probe */
  259. ret = __register_trace_probe(tp);
  260. if (ret < 0)
  261. unregister_probe_event(tp);
  262. else
  263. list_add_tail(&tp->list, &probe_list);
  264. end:
  265. mutex_unlock(&probe_lock);
  266. return ret;
  267. }
  268. /* Module notifier call back, checking event on the module */
  269. static int trace_probe_module_callback(struct notifier_block *nb,
  270. unsigned long val, void *data)
  271. {
  272. struct module *mod = data;
  273. struct trace_probe *tp;
  274. int ret;
  275. if (val != MODULE_STATE_COMING)
  276. return NOTIFY_DONE;
  277. /* Update probes on coming module */
  278. mutex_lock(&probe_lock);
  279. list_for_each_entry(tp, &probe_list, list) {
  280. if (trace_probe_within_module(tp, mod)) {
  281. /* Don't need to check busy - this should have gone. */
  282. __unregister_trace_probe(tp);
  283. ret = __register_trace_probe(tp);
  284. if (ret)
  285. pr_warning("Failed to re-register probe %s on"
  286. "%s: %d\n",
  287. tp->call.name, mod->name, ret);
  288. }
  289. }
  290. mutex_unlock(&probe_lock);
  291. return NOTIFY_DONE;
  292. }
  293. static struct notifier_block trace_probe_module_nb = {
  294. .notifier_call = trace_probe_module_callback,
  295. .priority = 1 /* Invoked after kprobe module callback */
  296. };
  297. static int create_trace_probe(int argc, char **argv)
  298. {
  299. /*
  300. * Argument syntax:
  301. * - Add kprobe: p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
  302. * - Add kretprobe: r[:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
  303. * Fetch args:
  304. * $retval : fetch return value
  305. * $stack : fetch stack address
  306. * $stackN : fetch Nth of stack (N:0-)
  307. * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
  308. * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
  309. * %REG : fetch register REG
  310. * Dereferencing memory fetch:
  311. * +|-offs(ARG) : fetch memory at ARG +|- offs address.
  312. * Alias name of args:
  313. * NAME=FETCHARG : set NAME as alias of FETCHARG.
  314. * Type of args:
  315. * FETCHARG:TYPE : use TYPE instead of unsigned long.
  316. */
  317. struct trace_probe *tp;
  318. int i, ret = 0;
  319. bool is_return = false, is_delete = false;
  320. char *symbol = NULL, *event = NULL, *group = NULL;
  321. char *arg;
  322. unsigned long offset = 0;
  323. void *addr = NULL;
  324. char buf[MAX_EVENT_NAME_LEN];
  325. /* argc must be >= 1 */
  326. if (argv[0][0] == 'p')
  327. is_return = false;
  328. else if (argv[0][0] == 'r')
  329. is_return = true;
  330. else if (argv[0][0] == '-')
  331. is_delete = true;
  332. else {
  333. pr_info("Probe definition must be started with 'p', 'r' or"
  334. " '-'.\n");
  335. return -EINVAL;
  336. }
  337. if (argv[0][1] == ':') {
  338. event = &argv[0][2];
  339. if (strchr(event, '/')) {
  340. group = event;
  341. event = strchr(group, '/') + 1;
  342. event[-1] = '\0';
  343. if (strlen(group) == 0) {
  344. pr_info("Group name is not specified\n");
  345. return -EINVAL;
  346. }
  347. }
  348. if (strlen(event) == 0) {
  349. pr_info("Event name is not specified\n");
  350. return -EINVAL;
  351. }
  352. }
  353. if (!group)
  354. group = KPROBE_EVENT_SYSTEM;
  355. if (is_delete) {
  356. if (!event) {
  357. pr_info("Delete command needs an event name.\n");
  358. return -EINVAL;
  359. }
  360. mutex_lock(&probe_lock);
  361. tp = find_trace_probe(event, group);
  362. if (!tp) {
  363. mutex_unlock(&probe_lock);
  364. pr_info("Event %s/%s doesn't exist.\n", group, event);
  365. return -ENOENT;
  366. }
  367. /* delete an event */
  368. ret = unregister_trace_probe(tp);
  369. if (ret == 0)
  370. free_trace_probe(tp);
  371. mutex_unlock(&probe_lock);
  372. return ret;
  373. }
  374. if (argc < 2) {
  375. pr_info("Probe point is not specified.\n");
  376. return -EINVAL;
  377. }
  378. if (isdigit(argv[1][0])) {
  379. if (is_return) {
  380. pr_info("Return probe point must be a symbol.\n");
  381. return -EINVAL;
  382. }
  383. /* an address specified */
  384. ret = kstrtoul(&argv[1][0], 0, (unsigned long *)&addr);
  385. if (ret) {
  386. pr_info("Failed to parse address.\n");
  387. return ret;
  388. }
  389. } else {
  390. /* a symbol specified */
  391. symbol = argv[1];
  392. /* TODO: support .init module functions */
  393. ret = traceprobe_split_symbol_offset(symbol, &offset);
  394. if (ret) {
  395. pr_info("Failed to parse symbol.\n");
  396. return ret;
  397. }
  398. if (offset && is_return) {
  399. pr_info("Return probe must be used without offset.\n");
  400. return -EINVAL;
  401. }
  402. }
  403. argc -= 2; argv += 2;
  404. /* setup a probe */
  405. if (!event) {
  406. /* Make a new event name */
  407. if (symbol)
  408. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
  409. is_return ? 'r' : 'p', symbol, offset);
  410. else
  411. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
  412. is_return ? 'r' : 'p', addr);
  413. event = buf;
  414. }
  415. tp = alloc_trace_probe(group, event, addr, symbol, offset, argc,
  416. is_return);
  417. if (IS_ERR(tp)) {
  418. pr_info("Failed to allocate trace_probe.(%d)\n",
  419. (int)PTR_ERR(tp));
  420. return PTR_ERR(tp);
  421. }
  422. /* parse arguments */
  423. ret = 0;
  424. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  425. /* Increment count for freeing args in error case */
  426. tp->nr_args++;
  427. /* Parse argument name */
  428. arg = strchr(argv[i], '=');
  429. if (arg) {
  430. *arg++ = '\0';
  431. tp->args[i].name = kstrdup(argv[i], GFP_KERNEL);
  432. } else {
  433. arg = argv[i];
  434. /* If argument name is omitted, set "argN" */
  435. snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
  436. tp->args[i].name = kstrdup(buf, GFP_KERNEL);
  437. }
  438. if (!tp->args[i].name) {
  439. pr_info("Failed to allocate argument[%d] name.\n", i);
  440. ret = -ENOMEM;
  441. goto error;
  442. }
  443. if (!is_good_name(tp->args[i].name)) {
  444. pr_info("Invalid argument[%d] name: %s\n",
  445. i, tp->args[i].name);
  446. ret = -EINVAL;
  447. goto error;
  448. }
  449. if (traceprobe_conflict_field_name(tp->args[i].name,
  450. tp->args, i)) {
  451. pr_info("Argument[%d] name '%s' conflicts with "
  452. "another field.\n", i, argv[i]);
  453. ret = -EINVAL;
  454. goto error;
  455. }
  456. /* Parse fetch argument */
  457. ret = traceprobe_parse_probe_arg(arg, &tp->size, &tp->args[i],
  458. is_return, true);
  459. if (ret) {
  460. pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
  461. goto error;
  462. }
  463. }
  464. ret = register_trace_probe(tp);
  465. if (ret)
  466. goto error;
  467. return 0;
  468. error:
  469. free_trace_probe(tp);
  470. return ret;
  471. }
  472. static int release_all_trace_probes(void)
  473. {
  474. struct trace_probe *tp;
  475. int ret = 0;
  476. mutex_lock(&probe_lock);
  477. /* Ensure no probe is in use. */
  478. list_for_each_entry(tp, &probe_list, list)
  479. if (trace_probe_is_enabled(tp)) {
  480. ret = -EBUSY;
  481. goto end;
  482. }
  483. /* TODO: Use batch unregistration */
  484. while (!list_empty(&probe_list)) {
  485. tp = list_entry(probe_list.next, struct trace_probe, list);
  486. unregister_trace_probe(tp);
  487. free_trace_probe(tp);
  488. }
  489. end:
  490. mutex_unlock(&probe_lock);
  491. return ret;
  492. }
  493. /* Probes listing interfaces */
  494. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  495. {
  496. mutex_lock(&probe_lock);
  497. return seq_list_start(&probe_list, *pos);
  498. }
  499. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  500. {
  501. return seq_list_next(v, &probe_list, pos);
  502. }
  503. static void probes_seq_stop(struct seq_file *m, void *v)
  504. {
  505. mutex_unlock(&probe_lock);
  506. }
  507. static int probes_seq_show(struct seq_file *m, void *v)
  508. {
  509. struct trace_probe *tp = v;
  510. int i;
  511. seq_printf(m, "%c", trace_probe_is_return(tp) ? 'r' : 'p');
  512. seq_printf(m, ":%s/%s", tp->call.class->system, tp->call.name);
  513. if (!tp->symbol)
  514. seq_printf(m, " 0x%p", tp->rp.kp.addr);
  515. else if (tp->rp.kp.offset)
  516. seq_printf(m, " %s+%u", trace_probe_symbol(tp),
  517. tp->rp.kp.offset);
  518. else
  519. seq_printf(m, " %s", trace_probe_symbol(tp));
  520. for (i = 0; i < tp->nr_args; i++)
  521. seq_printf(m, " %s=%s", tp->args[i].name, tp->args[i].comm);
  522. seq_printf(m, "\n");
  523. return 0;
  524. }
  525. static const struct seq_operations probes_seq_op = {
  526. .start = probes_seq_start,
  527. .next = probes_seq_next,
  528. .stop = probes_seq_stop,
  529. .show = probes_seq_show
  530. };
  531. static int probes_open(struct inode *inode, struct file *file)
  532. {
  533. int ret;
  534. if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
  535. ret = release_all_trace_probes();
  536. if (ret < 0)
  537. return ret;
  538. }
  539. return seq_open(file, &probes_seq_op);
  540. }
  541. static ssize_t probes_write(struct file *file, const char __user *buffer,
  542. size_t count, loff_t *ppos)
  543. {
  544. return traceprobe_probes_write(file, buffer, count, ppos,
  545. create_trace_probe);
  546. }
  547. static const struct file_operations kprobe_events_ops = {
  548. .owner = THIS_MODULE,
  549. .open = probes_open,
  550. .read = seq_read,
  551. .llseek = seq_lseek,
  552. .release = seq_release,
  553. .write = probes_write,
  554. };
  555. /* Probes profiling interfaces */
  556. static int probes_profile_seq_show(struct seq_file *m, void *v)
  557. {
  558. struct trace_probe *tp = v;
  559. seq_printf(m, " %-44s %15lu %15lu\n", tp->call.name, tp->nhit,
  560. tp->rp.kp.nmissed);
  561. return 0;
  562. }
  563. static const struct seq_operations profile_seq_op = {
  564. .start = probes_seq_start,
  565. .next = probes_seq_next,
  566. .stop = probes_seq_stop,
  567. .show = probes_profile_seq_show
  568. };
  569. static int profile_open(struct inode *inode, struct file *file)
  570. {
  571. return seq_open(file, &profile_seq_op);
  572. }
  573. static const struct file_operations kprobe_profile_ops = {
  574. .owner = THIS_MODULE,
  575. .open = profile_open,
  576. .read = seq_read,
  577. .llseek = seq_lseek,
  578. .release = seq_release,
  579. };
  580. /* Sum up total data length for dynamic arraies (strings) */
  581. static __kprobes int __get_data_size(struct trace_probe *tp,
  582. struct pt_regs *regs)
  583. {
  584. int i, ret = 0;
  585. u32 len;
  586. for (i = 0; i < tp->nr_args; i++)
  587. if (unlikely(tp->args[i].fetch_size.fn)) {
  588. call_fetch(&tp->args[i].fetch_size, regs, &len);
  589. ret += len;
  590. }
  591. return ret;
  592. }
  593. /* Store the value of each argument */
  594. static __kprobes void store_trace_args(int ent_size, struct trace_probe *tp,
  595. struct pt_regs *regs,
  596. u8 *data, int maxlen)
  597. {
  598. int i;
  599. u32 end = tp->size;
  600. u32 *dl; /* Data (relative) location */
  601. for (i = 0; i < tp->nr_args; i++) {
  602. if (unlikely(tp->args[i].fetch_size.fn)) {
  603. /*
  604. * First, we set the relative location and
  605. * maximum data length to *dl
  606. */
  607. dl = (u32 *)(data + tp->args[i].offset);
  608. *dl = make_data_rloc(maxlen, end - tp->args[i].offset);
  609. /* Then try to fetch string or dynamic array data */
  610. call_fetch(&tp->args[i].fetch, regs, dl);
  611. /* Reduce maximum length */
  612. end += get_rloc_len(*dl);
  613. maxlen -= get_rloc_len(*dl);
  614. /* Trick here, convert data_rloc to data_loc */
  615. *dl = convert_rloc_to_loc(*dl,
  616. ent_size + tp->args[i].offset);
  617. } else
  618. /* Just fetching data normally */
  619. call_fetch(&tp->args[i].fetch, regs,
  620. data + tp->args[i].offset);
  621. }
  622. }
  623. /* Kprobe handler */
  624. static __kprobes void kprobe_trace_func(struct kprobe *kp, struct pt_regs *regs)
  625. {
  626. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  627. struct kprobe_trace_entry_head *entry;
  628. struct ring_buffer_event *event;
  629. struct ring_buffer *buffer;
  630. int size, dsize, pc;
  631. unsigned long irq_flags;
  632. struct ftrace_event_call *call = &tp->call;
  633. local_save_flags(irq_flags);
  634. pc = preempt_count();
  635. dsize = __get_data_size(tp, regs);
  636. size = sizeof(*entry) + tp->size + dsize;
  637. event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
  638. size, irq_flags, pc);
  639. if (!event)
  640. return;
  641. entry = ring_buffer_event_data(event);
  642. entry->ip = (unsigned long)kp->addr;
  643. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  644. if (!filter_current_check_discard(buffer, call, entry, event))
  645. trace_buffer_unlock_commit_regs(buffer, event,
  646. irq_flags, pc, regs);
  647. }
  648. /* Kretprobe handler */
  649. static __kprobes void kretprobe_trace_func(struct kretprobe_instance *ri,
  650. struct pt_regs *regs)
  651. {
  652. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  653. struct kretprobe_trace_entry_head *entry;
  654. struct ring_buffer_event *event;
  655. struct ring_buffer *buffer;
  656. int size, pc, dsize;
  657. unsigned long irq_flags;
  658. struct ftrace_event_call *call = &tp->call;
  659. local_save_flags(irq_flags);
  660. pc = preempt_count();
  661. dsize = __get_data_size(tp, regs);
  662. size = sizeof(*entry) + tp->size + dsize;
  663. event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
  664. size, irq_flags, pc);
  665. if (!event)
  666. return;
  667. entry = ring_buffer_event_data(event);
  668. entry->func = (unsigned long)tp->rp.kp.addr;
  669. entry->ret_ip = (unsigned long)ri->ret_addr;
  670. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  671. if (!filter_current_check_discard(buffer, call, entry, event))
  672. trace_buffer_unlock_commit_regs(buffer, event,
  673. irq_flags, pc, regs);
  674. }
  675. /* Event entry printers */
  676. enum print_line_t
  677. print_kprobe_event(struct trace_iterator *iter, int flags,
  678. struct trace_event *event)
  679. {
  680. struct kprobe_trace_entry_head *field;
  681. struct trace_seq *s = &iter->seq;
  682. struct trace_probe *tp;
  683. u8 *data;
  684. int i;
  685. field = (struct kprobe_trace_entry_head *)iter->ent;
  686. tp = container_of(event, struct trace_probe, call.event);
  687. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  688. goto partial;
  689. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  690. goto partial;
  691. if (!trace_seq_puts(s, ")"))
  692. goto partial;
  693. data = (u8 *)&field[1];
  694. for (i = 0; i < tp->nr_args; i++)
  695. if (!tp->args[i].type->print(s, tp->args[i].name,
  696. data + tp->args[i].offset, field))
  697. goto partial;
  698. if (!trace_seq_puts(s, "\n"))
  699. goto partial;
  700. return TRACE_TYPE_HANDLED;
  701. partial:
  702. return TRACE_TYPE_PARTIAL_LINE;
  703. }
  704. enum print_line_t
  705. print_kretprobe_event(struct trace_iterator *iter, int flags,
  706. struct trace_event *event)
  707. {
  708. struct kretprobe_trace_entry_head *field;
  709. struct trace_seq *s = &iter->seq;
  710. struct trace_probe *tp;
  711. u8 *data;
  712. int i;
  713. field = (struct kretprobe_trace_entry_head *)iter->ent;
  714. tp = container_of(event, struct trace_probe, call.event);
  715. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  716. goto partial;
  717. if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
  718. goto partial;
  719. if (!trace_seq_puts(s, " <- "))
  720. goto partial;
  721. if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
  722. goto partial;
  723. if (!trace_seq_puts(s, ")"))
  724. goto partial;
  725. data = (u8 *)&field[1];
  726. for (i = 0; i < tp->nr_args; i++)
  727. if (!tp->args[i].type->print(s, tp->args[i].name,
  728. data + tp->args[i].offset, field))
  729. goto partial;
  730. if (!trace_seq_puts(s, "\n"))
  731. goto partial;
  732. return TRACE_TYPE_HANDLED;
  733. partial:
  734. return TRACE_TYPE_PARTIAL_LINE;
  735. }
  736. static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
  737. {
  738. int ret, i;
  739. struct kprobe_trace_entry_head field;
  740. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  741. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  742. /* Set argument names as fields */
  743. for (i = 0; i < tp->nr_args; i++) {
  744. ret = trace_define_field(event_call, tp->args[i].type->fmttype,
  745. tp->args[i].name,
  746. sizeof(field) + tp->args[i].offset,
  747. tp->args[i].type->size,
  748. tp->args[i].type->is_signed,
  749. FILTER_OTHER);
  750. if (ret)
  751. return ret;
  752. }
  753. return 0;
  754. }
  755. static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
  756. {
  757. int ret, i;
  758. struct kretprobe_trace_entry_head field;
  759. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  760. DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
  761. DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
  762. /* Set argument names as fields */
  763. for (i = 0; i < tp->nr_args; i++) {
  764. ret = trace_define_field(event_call, tp->args[i].type->fmttype,
  765. tp->args[i].name,
  766. sizeof(field) + tp->args[i].offset,
  767. tp->args[i].type->size,
  768. tp->args[i].type->is_signed,
  769. FILTER_OTHER);
  770. if (ret)
  771. return ret;
  772. }
  773. return 0;
  774. }
  775. static int __set_print_fmt(struct trace_probe *tp, char *buf, int len)
  776. {
  777. int i;
  778. int pos = 0;
  779. const char *fmt, *arg;
  780. if (!trace_probe_is_return(tp)) {
  781. fmt = "(%lx)";
  782. arg = "REC->" FIELD_STRING_IP;
  783. } else {
  784. fmt = "(%lx <- %lx)";
  785. arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
  786. }
  787. /* When len=0, we just calculate the needed length */
  788. #define LEN_OR_ZERO (len ? len - pos : 0)
  789. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
  790. for (i = 0; i < tp->nr_args; i++) {
  791. pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
  792. tp->args[i].name, tp->args[i].type->fmt);
  793. }
  794. pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
  795. for (i = 0; i < tp->nr_args; i++) {
  796. if (strcmp(tp->args[i].type->name, "string") == 0)
  797. pos += snprintf(buf + pos, LEN_OR_ZERO,
  798. ", __get_str(%s)",
  799. tp->args[i].name);
  800. else
  801. pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
  802. tp->args[i].name);
  803. }
  804. #undef LEN_OR_ZERO
  805. /* return the length of print_fmt */
  806. return pos;
  807. }
  808. static int set_print_fmt(struct trace_probe *tp)
  809. {
  810. int len;
  811. char *print_fmt;
  812. /* First: called with 0 length to calculate the needed length */
  813. len = __set_print_fmt(tp, NULL, 0);
  814. print_fmt = kmalloc(len + 1, GFP_KERNEL);
  815. if (!print_fmt)
  816. return -ENOMEM;
  817. /* Second: actually write the @print_fmt */
  818. __set_print_fmt(tp, print_fmt, len + 1);
  819. tp->call.print_fmt = print_fmt;
  820. return 0;
  821. }
  822. #ifdef CONFIG_PERF_EVENTS
  823. /* Kprobe profile handler */
  824. static __kprobes void kprobe_perf_func(struct kprobe *kp,
  825. struct pt_regs *regs)
  826. {
  827. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  828. struct ftrace_event_call *call = &tp->call;
  829. struct kprobe_trace_entry_head *entry;
  830. struct hlist_head *head;
  831. int size, __size, dsize;
  832. int rctx;
  833. dsize = __get_data_size(tp, regs);
  834. __size = sizeof(*entry) + tp->size + dsize;
  835. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  836. size -= sizeof(u32);
  837. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  838. "profile buffer not large enough"))
  839. return;
  840. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  841. if (!entry)
  842. return;
  843. entry->ip = (unsigned long)kp->addr;
  844. memset(&entry[1], 0, dsize);
  845. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  846. head = this_cpu_ptr(call->perf_events);
  847. perf_trace_buf_submit(entry, size, rctx,
  848. entry->ip, 1, regs, head, NULL);
  849. }
  850. /* Kretprobe profile handler */
  851. static __kprobes void kretprobe_perf_func(struct kretprobe_instance *ri,
  852. struct pt_regs *regs)
  853. {
  854. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  855. struct ftrace_event_call *call = &tp->call;
  856. struct kretprobe_trace_entry_head *entry;
  857. struct hlist_head *head;
  858. int size, __size, dsize;
  859. int rctx;
  860. dsize = __get_data_size(tp, regs);
  861. __size = sizeof(*entry) + tp->size + dsize;
  862. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  863. size -= sizeof(u32);
  864. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  865. "profile buffer not large enough"))
  866. return;
  867. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  868. if (!entry)
  869. return;
  870. entry->func = (unsigned long)tp->rp.kp.addr;
  871. entry->ret_ip = (unsigned long)ri->ret_addr;
  872. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  873. head = this_cpu_ptr(call->perf_events);
  874. perf_trace_buf_submit(entry, size, rctx,
  875. entry->ret_ip, 1, regs, head, NULL);
  876. }
  877. #endif /* CONFIG_PERF_EVENTS */
  878. static __kprobes
  879. int kprobe_register(struct ftrace_event_call *event,
  880. enum trace_reg type, void *data)
  881. {
  882. struct trace_probe *tp = (struct trace_probe *)event->data;
  883. switch (type) {
  884. case TRACE_REG_REGISTER:
  885. return enable_trace_probe(tp, TP_FLAG_TRACE);
  886. case TRACE_REG_UNREGISTER:
  887. disable_trace_probe(tp, TP_FLAG_TRACE);
  888. return 0;
  889. #ifdef CONFIG_PERF_EVENTS
  890. case TRACE_REG_PERF_REGISTER:
  891. return enable_trace_probe(tp, TP_FLAG_PROFILE);
  892. case TRACE_REG_PERF_UNREGISTER:
  893. disable_trace_probe(tp, TP_FLAG_PROFILE);
  894. return 0;
  895. case TRACE_REG_PERF_OPEN:
  896. case TRACE_REG_PERF_CLOSE:
  897. case TRACE_REG_PERF_ADD:
  898. case TRACE_REG_PERF_DEL:
  899. return 0;
  900. #endif
  901. }
  902. return 0;
  903. }
  904. static __kprobes
  905. int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
  906. {
  907. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  908. tp->nhit++;
  909. if (tp->flags & TP_FLAG_TRACE)
  910. kprobe_trace_func(kp, regs);
  911. #ifdef CONFIG_PERF_EVENTS
  912. if (tp->flags & TP_FLAG_PROFILE)
  913. kprobe_perf_func(kp, regs);
  914. #endif
  915. return 0; /* We don't tweek kernel, so just return 0 */
  916. }
  917. static __kprobes
  918. int kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
  919. {
  920. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  921. tp->nhit++;
  922. if (tp->flags & TP_FLAG_TRACE)
  923. kretprobe_trace_func(ri, regs);
  924. #ifdef CONFIG_PERF_EVENTS
  925. if (tp->flags & TP_FLAG_PROFILE)
  926. kretprobe_perf_func(ri, regs);
  927. #endif
  928. return 0; /* We don't tweek kernel, so just return 0 */
  929. }
  930. static struct trace_event_functions kretprobe_funcs = {
  931. .trace = print_kretprobe_event
  932. };
  933. static struct trace_event_functions kprobe_funcs = {
  934. .trace = print_kprobe_event
  935. };
  936. static int register_probe_event(struct trace_probe *tp)
  937. {
  938. struct ftrace_event_call *call = &tp->call;
  939. int ret;
  940. /* Initialize ftrace_event_call */
  941. INIT_LIST_HEAD(&call->class->fields);
  942. if (trace_probe_is_return(tp)) {
  943. call->event.funcs = &kretprobe_funcs;
  944. call->class->define_fields = kretprobe_event_define_fields;
  945. } else {
  946. call->event.funcs = &kprobe_funcs;
  947. call->class->define_fields = kprobe_event_define_fields;
  948. }
  949. if (set_print_fmt(tp) < 0)
  950. return -ENOMEM;
  951. ret = register_ftrace_event(&call->event);
  952. if (!ret) {
  953. kfree(call->print_fmt);
  954. return -ENODEV;
  955. }
  956. call->flags = 0;
  957. call->class->reg = kprobe_register;
  958. call->data = tp;
  959. ret = trace_add_event_call(call);
  960. if (ret) {
  961. pr_info("Failed to register kprobe event: %s\n", call->name);
  962. kfree(call->print_fmt);
  963. unregister_ftrace_event(&call->event);
  964. }
  965. return ret;
  966. }
  967. static void unregister_probe_event(struct trace_probe *tp)
  968. {
  969. /* tp->event is unregistered in trace_remove_event_call() */
  970. trace_remove_event_call(&tp->call);
  971. kfree(tp->call.print_fmt);
  972. }
  973. /* Make a debugfs interface for controlling probe points */
  974. static __init int init_kprobe_trace(void)
  975. {
  976. struct dentry *d_tracer;
  977. struct dentry *entry;
  978. if (register_module_notifier(&trace_probe_module_nb))
  979. return -EINVAL;
  980. d_tracer = tracing_init_dentry();
  981. if (!d_tracer)
  982. return 0;
  983. entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
  984. NULL, &kprobe_events_ops);
  985. /* Event list interface */
  986. if (!entry)
  987. pr_warning("Could not create debugfs "
  988. "'kprobe_events' entry\n");
  989. /* Profile interface */
  990. entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
  991. NULL, &kprobe_profile_ops);
  992. if (!entry)
  993. pr_warning("Could not create debugfs "
  994. "'kprobe_profile' entry\n");
  995. return 0;
  996. }
  997. fs_initcall(init_kprobe_trace);
  998. #ifdef CONFIG_FTRACE_STARTUP_TEST
  999. /*
  1000. * The "__used" keeps gcc from removing the function symbol
  1001. * from the kallsyms table.
  1002. */
  1003. static __used int kprobe_trace_selftest_target(int a1, int a2, int a3,
  1004. int a4, int a5, int a6)
  1005. {
  1006. return a1 + a2 + a3 + a4 + a5 + a6;
  1007. }
  1008. static __init int kprobe_trace_self_tests_init(void)
  1009. {
  1010. int ret, warn = 0;
  1011. int (*target)(int, int, int, int, int, int);
  1012. struct trace_probe *tp;
  1013. target = kprobe_trace_selftest_target;
  1014. pr_info("Testing kprobe tracing: ");
  1015. ret = traceprobe_command("p:testprobe kprobe_trace_selftest_target "
  1016. "$stack $stack0 +0($stack)",
  1017. create_trace_probe);
  1018. if (WARN_ON_ONCE(ret)) {
  1019. pr_warning("error on probing function entry.\n");
  1020. warn++;
  1021. } else {
  1022. /* Enable trace point */
  1023. tp = find_trace_probe("testprobe", KPROBE_EVENT_SYSTEM);
  1024. if (WARN_ON_ONCE(tp == NULL)) {
  1025. pr_warning("error on getting new probe.\n");
  1026. warn++;
  1027. } else
  1028. enable_trace_probe(tp, TP_FLAG_TRACE);
  1029. }
  1030. ret = traceprobe_command("r:testprobe2 kprobe_trace_selftest_target "
  1031. "$retval", create_trace_probe);
  1032. if (WARN_ON_ONCE(ret)) {
  1033. pr_warning("error on probing function return.\n");
  1034. warn++;
  1035. } else {
  1036. /* Enable trace point */
  1037. tp = find_trace_probe("testprobe2", KPROBE_EVENT_SYSTEM);
  1038. if (WARN_ON_ONCE(tp == NULL)) {
  1039. pr_warning("error on getting new probe.\n");
  1040. warn++;
  1041. } else
  1042. enable_trace_probe(tp, TP_FLAG_TRACE);
  1043. }
  1044. if (warn)
  1045. goto end;
  1046. ret = target(1, 2, 3, 4, 5, 6);
  1047. /* Disable trace points before removing it */
  1048. tp = find_trace_probe("testprobe", KPROBE_EVENT_SYSTEM);
  1049. if (WARN_ON_ONCE(tp == NULL)) {
  1050. pr_warning("error on getting test probe.\n");
  1051. warn++;
  1052. } else
  1053. disable_trace_probe(tp, TP_FLAG_TRACE);
  1054. tp = find_trace_probe("testprobe2", KPROBE_EVENT_SYSTEM);
  1055. if (WARN_ON_ONCE(tp == NULL)) {
  1056. pr_warning("error on getting 2nd test probe.\n");
  1057. warn++;
  1058. } else
  1059. disable_trace_probe(tp, TP_FLAG_TRACE);
  1060. ret = traceprobe_command("-:testprobe", create_trace_probe);
  1061. if (WARN_ON_ONCE(ret)) {
  1062. pr_warning("error on deleting a probe.\n");
  1063. warn++;
  1064. }
  1065. ret = traceprobe_command("-:testprobe2", create_trace_probe);
  1066. if (WARN_ON_ONCE(ret)) {
  1067. pr_warning("error on deleting a probe.\n");
  1068. warn++;
  1069. }
  1070. end:
  1071. release_all_trace_probes();
  1072. if (warn)
  1073. pr_cont("NG: Some tests are failed. Please check them.\n");
  1074. else
  1075. pr_cont("OK\n");
  1076. return 0;
  1077. }
  1078. late_initcall(kprobe_trace_self_tests_init);
  1079. #endif