trace_kprobe.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  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 int 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 = strict_strtoul(&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. tp->nhit++;
  634. local_save_flags(irq_flags);
  635. pc = preempt_count();
  636. dsize = __get_data_size(tp, regs);
  637. size = sizeof(*entry) + tp->size + dsize;
  638. event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
  639. size, irq_flags, pc);
  640. if (!event)
  641. return;
  642. entry = ring_buffer_event_data(event);
  643. entry->ip = (unsigned long)kp->addr;
  644. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  645. if (!filter_current_check_discard(buffer, call, entry, event))
  646. trace_nowake_buffer_unlock_commit_regs(buffer, event,
  647. irq_flags, pc, regs);
  648. }
  649. /* Kretprobe handler */
  650. static __kprobes void kretprobe_trace_func(struct kretprobe_instance *ri,
  651. struct pt_regs *regs)
  652. {
  653. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  654. struct kretprobe_trace_entry_head *entry;
  655. struct ring_buffer_event *event;
  656. struct ring_buffer *buffer;
  657. int size, pc, dsize;
  658. unsigned long irq_flags;
  659. struct ftrace_event_call *call = &tp->call;
  660. local_save_flags(irq_flags);
  661. pc = preempt_count();
  662. dsize = __get_data_size(tp, regs);
  663. size = sizeof(*entry) + tp->size + dsize;
  664. event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
  665. size, irq_flags, pc);
  666. if (!event)
  667. return;
  668. entry = ring_buffer_event_data(event);
  669. entry->func = (unsigned long)tp->rp.kp.addr;
  670. entry->ret_ip = (unsigned long)ri->ret_addr;
  671. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  672. if (!filter_current_check_discard(buffer, call, entry, event))
  673. trace_nowake_buffer_unlock_commit_regs(buffer, event,
  674. irq_flags, pc, regs);
  675. }
  676. /* Event entry printers */
  677. enum print_line_t
  678. print_kprobe_event(struct trace_iterator *iter, int flags,
  679. struct trace_event *event)
  680. {
  681. struct kprobe_trace_entry_head *field;
  682. struct trace_seq *s = &iter->seq;
  683. struct trace_probe *tp;
  684. u8 *data;
  685. int i;
  686. field = (struct kprobe_trace_entry_head *)iter->ent;
  687. tp = container_of(event, struct trace_probe, call.event);
  688. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  689. goto partial;
  690. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  691. goto partial;
  692. if (!trace_seq_puts(s, ")"))
  693. goto partial;
  694. data = (u8 *)&field[1];
  695. for (i = 0; i < tp->nr_args; i++)
  696. if (!tp->args[i].type->print(s, tp->args[i].name,
  697. data + tp->args[i].offset, field))
  698. goto partial;
  699. if (!trace_seq_puts(s, "\n"))
  700. goto partial;
  701. return TRACE_TYPE_HANDLED;
  702. partial:
  703. return TRACE_TYPE_PARTIAL_LINE;
  704. }
  705. enum print_line_t
  706. print_kretprobe_event(struct trace_iterator *iter, int flags,
  707. struct trace_event *event)
  708. {
  709. struct kretprobe_trace_entry_head *field;
  710. struct trace_seq *s = &iter->seq;
  711. struct trace_probe *tp;
  712. u8 *data;
  713. int i;
  714. field = (struct kretprobe_trace_entry_head *)iter->ent;
  715. tp = container_of(event, struct trace_probe, call.event);
  716. if (!trace_seq_printf(s, "%s: (", tp->call.name))
  717. goto partial;
  718. if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
  719. goto partial;
  720. if (!trace_seq_puts(s, " <- "))
  721. goto partial;
  722. if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
  723. goto partial;
  724. if (!trace_seq_puts(s, ")"))
  725. goto partial;
  726. data = (u8 *)&field[1];
  727. for (i = 0; i < tp->nr_args; i++)
  728. if (!tp->args[i].type->print(s, tp->args[i].name,
  729. data + tp->args[i].offset, field))
  730. goto partial;
  731. if (!trace_seq_puts(s, "\n"))
  732. goto partial;
  733. return TRACE_TYPE_HANDLED;
  734. partial:
  735. return TRACE_TYPE_PARTIAL_LINE;
  736. }
  737. static int kprobe_event_define_fields(struct ftrace_event_call *event_call)
  738. {
  739. int ret, i;
  740. struct kprobe_trace_entry_head field;
  741. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  742. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  743. /* Set argument names as fields */
  744. for (i = 0; i < tp->nr_args; i++) {
  745. ret = trace_define_field(event_call, tp->args[i].type->fmttype,
  746. tp->args[i].name,
  747. sizeof(field) + tp->args[i].offset,
  748. tp->args[i].type->size,
  749. tp->args[i].type->is_signed,
  750. FILTER_OTHER);
  751. if (ret)
  752. return ret;
  753. }
  754. return 0;
  755. }
  756. static int kretprobe_event_define_fields(struct ftrace_event_call *event_call)
  757. {
  758. int ret, i;
  759. struct kretprobe_trace_entry_head field;
  760. struct trace_probe *tp = (struct trace_probe *)event_call->data;
  761. DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
  762. DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
  763. /* Set argument names as fields */
  764. for (i = 0; i < tp->nr_args; i++) {
  765. ret = trace_define_field(event_call, tp->args[i].type->fmttype,
  766. tp->args[i].name,
  767. sizeof(field) + tp->args[i].offset,
  768. tp->args[i].type->size,
  769. tp->args[i].type->is_signed,
  770. FILTER_OTHER);
  771. if (ret)
  772. return ret;
  773. }
  774. return 0;
  775. }
  776. static int __set_print_fmt(struct trace_probe *tp, char *buf, int len)
  777. {
  778. int i;
  779. int pos = 0;
  780. const char *fmt, *arg;
  781. if (!trace_probe_is_return(tp)) {
  782. fmt = "(%lx)";
  783. arg = "REC->" FIELD_STRING_IP;
  784. } else {
  785. fmt = "(%lx <- %lx)";
  786. arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
  787. }
  788. /* When len=0, we just calculate the needed length */
  789. #define LEN_OR_ZERO (len ? len - pos : 0)
  790. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
  791. for (i = 0; i < tp->nr_args; i++) {
  792. pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
  793. tp->args[i].name, tp->args[i].type->fmt);
  794. }
  795. pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
  796. for (i = 0; i < tp->nr_args; i++) {
  797. if (strcmp(tp->args[i].type->name, "string") == 0)
  798. pos += snprintf(buf + pos, LEN_OR_ZERO,
  799. ", __get_str(%s)",
  800. tp->args[i].name);
  801. else
  802. pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
  803. tp->args[i].name);
  804. }
  805. #undef LEN_OR_ZERO
  806. /* return the length of print_fmt */
  807. return pos;
  808. }
  809. static int set_print_fmt(struct trace_probe *tp)
  810. {
  811. int len;
  812. char *print_fmt;
  813. /* First: called with 0 length to calculate the needed length */
  814. len = __set_print_fmt(tp, NULL, 0);
  815. print_fmt = kmalloc(len + 1, GFP_KERNEL);
  816. if (!print_fmt)
  817. return -ENOMEM;
  818. /* Second: actually write the @print_fmt */
  819. __set_print_fmt(tp, print_fmt, len + 1);
  820. tp->call.print_fmt = print_fmt;
  821. return 0;
  822. }
  823. #ifdef CONFIG_PERF_EVENTS
  824. /* Kprobe profile handler */
  825. static __kprobes void kprobe_perf_func(struct kprobe *kp,
  826. struct pt_regs *regs)
  827. {
  828. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  829. struct ftrace_event_call *call = &tp->call;
  830. struct kprobe_trace_entry_head *entry;
  831. struct hlist_head *head;
  832. int size, __size, dsize;
  833. int rctx;
  834. dsize = __get_data_size(tp, regs);
  835. __size = sizeof(*entry) + tp->size + dsize;
  836. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  837. size -= sizeof(u32);
  838. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  839. "profile buffer not large enough"))
  840. return;
  841. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  842. if (!entry)
  843. return;
  844. entry->ip = (unsigned long)kp->addr;
  845. memset(&entry[1], 0, dsize);
  846. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  847. head = this_cpu_ptr(call->perf_events);
  848. perf_trace_buf_submit(entry, size, rctx,
  849. entry->ip, 1, regs, head, NULL);
  850. }
  851. /* Kretprobe profile handler */
  852. static __kprobes void kretprobe_perf_func(struct kretprobe_instance *ri,
  853. struct pt_regs *regs)
  854. {
  855. struct trace_probe *tp = container_of(ri->rp, struct trace_probe, rp);
  856. struct ftrace_event_call *call = &tp->call;
  857. struct kretprobe_trace_entry_head *entry;
  858. struct hlist_head *head;
  859. int size, __size, dsize;
  860. int rctx;
  861. dsize = __get_data_size(tp, regs);
  862. __size = sizeof(*entry) + tp->size + dsize;
  863. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  864. size -= sizeof(u32);
  865. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  866. "profile buffer not large enough"))
  867. return;
  868. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  869. if (!entry)
  870. return;
  871. entry->func = (unsigned long)tp->rp.kp.addr;
  872. entry->ret_ip = (unsigned long)ri->ret_addr;
  873. store_trace_args(sizeof(*entry), tp, regs, (u8 *)&entry[1], dsize);
  874. head = this_cpu_ptr(call->perf_events);
  875. perf_trace_buf_submit(entry, size, rctx,
  876. entry->ret_ip, 1, regs, head, NULL);
  877. }
  878. #endif /* CONFIG_PERF_EVENTS */
  879. static __kprobes
  880. int kprobe_register(struct ftrace_event_call *event,
  881. enum trace_reg type, void *data)
  882. {
  883. struct trace_probe *tp = (struct trace_probe *)event->data;
  884. switch (type) {
  885. case TRACE_REG_REGISTER:
  886. return enable_trace_probe(tp, TP_FLAG_TRACE);
  887. case TRACE_REG_UNREGISTER:
  888. disable_trace_probe(tp, TP_FLAG_TRACE);
  889. return 0;
  890. #ifdef CONFIG_PERF_EVENTS
  891. case TRACE_REG_PERF_REGISTER:
  892. return enable_trace_probe(tp, TP_FLAG_PROFILE);
  893. case TRACE_REG_PERF_UNREGISTER:
  894. disable_trace_probe(tp, TP_FLAG_PROFILE);
  895. return 0;
  896. case TRACE_REG_PERF_OPEN:
  897. case TRACE_REG_PERF_CLOSE:
  898. case TRACE_REG_PERF_ADD:
  899. case TRACE_REG_PERF_DEL:
  900. return 0;
  901. #endif
  902. }
  903. return 0;
  904. }
  905. static __kprobes
  906. int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
  907. {
  908. struct trace_probe *tp = container_of(kp, struct trace_probe, rp.kp);
  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. if (tp->flags & TP_FLAG_TRACE)
  922. kretprobe_trace_func(ri, regs);
  923. #ifdef CONFIG_PERF_EVENTS
  924. if (tp->flags & TP_FLAG_PROFILE)
  925. kretprobe_perf_func(ri, regs);
  926. #endif
  927. return 0; /* We don't tweek kernel, so just return 0 */
  928. }
  929. static struct trace_event_functions kretprobe_funcs = {
  930. .trace = print_kretprobe_event
  931. };
  932. static struct trace_event_functions kprobe_funcs = {
  933. .trace = print_kprobe_event
  934. };
  935. static int register_probe_event(struct trace_probe *tp)
  936. {
  937. struct ftrace_event_call *call = &tp->call;
  938. int ret;
  939. /* Initialize ftrace_event_call */
  940. INIT_LIST_HEAD(&call->class->fields);
  941. if (trace_probe_is_return(tp)) {
  942. call->event.funcs = &kretprobe_funcs;
  943. call->class->define_fields = kretprobe_event_define_fields;
  944. } else {
  945. call->event.funcs = &kprobe_funcs;
  946. call->class->define_fields = kprobe_event_define_fields;
  947. }
  948. if (set_print_fmt(tp) < 0)
  949. return -ENOMEM;
  950. ret = register_ftrace_event(&call->event);
  951. if (!ret) {
  952. kfree(call->print_fmt);
  953. return -ENODEV;
  954. }
  955. call->flags = 0;
  956. call->class->reg = kprobe_register;
  957. call->data = tp;
  958. ret = trace_add_event_call(call);
  959. if (ret) {
  960. pr_info("Failed to register kprobe event: %s\n", call->name);
  961. kfree(call->print_fmt);
  962. unregister_ftrace_event(&call->event);
  963. }
  964. return ret;
  965. }
  966. static void unregister_probe_event(struct trace_probe *tp)
  967. {
  968. /* tp->event is unregistered in trace_remove_event_call() */
  969. trace_remove_event_call(&tp->call);
  970. kfree(tp->call.print_fmt);
  971. }
  972. /* Make a debugfs interface for controlling probe points */
  973. static __init int init_kprobe_trace(void)
  974. {
  975. struct dentry *d_tracer;
  976. struct dentry *entry;
  977. if (register_module_notifier(&trace_probe_module_nb))
  978. return -EINVAL;
  979. d_tracer = tracing_init_dentry();
  980. if (!d_tracer)
  981. return 0;
  982. entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
  983. NULL, &kprobe_events_ops);
  984. /* Event list interface */
  985. if (!entry)
  986. pr_warning("Could not create debugfs "
  987. "'kprobe_events' entry\n");
  988. /* Profile interface */
  989. entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
  990. NULL, &kprobe_profile_ops);
  991. if (!entry)
  992. pr_warning("Could not create debugfs "
  993. "'kprobe_profile' entry\n");
  994. return 0;
  995. }
  996. fs_initcall(init_kprobe_trace);
  997. #ifdef CONFIG_FTRACE_STARTUP_TEST
  998. /*
  999. * The "__used" keeps gcc from removing the function symbol
  1000. * from the kallsyms table.
  1001. */
  1002. static __used int kprobe_trace_selftest_target(int a1, int a2, int a3,
  1003. int a4, int a5, int a6)
  1004. {
  1005. return a1 + a2 + a3 + a4 + a5 + a6;
  1006. }
  1007. static __init int kprobe_trace_self_tests_init(void)
  1008. {
  1009. int ret, warn = 0;
  1010. int (*target)(int, int, int, int, int, int);
  1011. struct trace_probe *tp;
  1012. target = kprobe_trace_selftest_target;
  1013. pr_info("Testing kprobe tracing: ");
  1014. ret = traceprobe_command("p:testprobe kprobe_trace_selftest_target "
  1015. "$stack $stack0 +0($stack)",
  1016. create_trace_probe);
  1017. if (WARN_ON_ONCE(ret)) {
  1018. pr_warning("error on probing function entry.\n");
  1019. warn++;
  1020. } else {
  1021. /* Enable trace point */
  1022. tp = find_trace_probe("testprobe", KPROBE_EVENT_SYSTEM);
  1023. if (WARN_ON_ONCE(tp == NULL)) {
  1024. pr_warning("error on getting new probe.\n");
  1025. warn++;
  1026. } else
  1027. enable_trace_probe(tp, TP_FLAG_TRACE);
  1028. }
  1029. ret = traceprobe_command("r:testprobe2 kprobe_trace_selftest_target "
  1030. "$retval", create_trace_probe);
  1031. if (WARN_ON_ONCE(ret)) {
  1032. pr_warning("error on probing function return.\n");
  1033. warn++;
  1034. } else {
  1035. /* Enable trace point */
  1036. tp = find_trace_probe("testprobe2", KPROBE_EVENT_SYSTEM);
  1037. if (WARN_ON_ONCE(tp == NULL)) {
  1038. pr_warning("error on getting new probe.\n");
  1039. warn++;
  1040. } else
  1041. enable_trace_probe(tp, TP_FLAG_TRACE);
  1042. }
  1043. if (warn)
  1044. goto end;
  1045. ret = target(1, 2, 3, 4, 5, 6);
  1046. /* Disable trace points before removing it */
  1047. tp = find_trace_probe("testprobe", KPROBE_EVENT_SYSTEM);
  1048. if (WARN_ON_ONCE(tp == NULL)) {
  1049. pr_warning("error on getting test probe.\n");
  1050. warn++;
  1051. } else
  1052. disable_trace_probe(tp, TP_FLAG_TRACE);
  1053. tp = find_trace_probe("testprobe2", KPROBE_EVENT_SYSTEM);
  1054. if (WARN_ON_ONCE(tp == NULL)) {
  1055. pr_warning("error on getting 2nd test probe.\n");
  1056. warn++;
  1057. } else
  1058. disable_trace_probe(tp, TP_FLAG_TRACE);
  1059. ret = traceprobe_command("-:testprobe", create_trace_probe);
  1060. if (WARN_ON_ONCE(ret)) {
  1061. pr_warning("error on deleting a probe.\n");
  1062. warn++;
  1063. }
  1064. ret = traceprobe_command("-:testprobe2", create_trace_probe);
  1065. if (WARN_ON_ONCE(ret)) {
  1066. pr_warning("error on deleting a probe.\n");
  1067. warn++;
  1068. }
  1069. end:
  1070. release_all_trace_probes();
  1071. if (warn)
  1072. pr_cont("NG: Some tests are failed. Please check them.\n");
  1073. else
  1074. pr_cont("OK\n");
  1075. return 0;
  1076. }
  1077. late_initcall(kprobe_trace_self_tests_init);
  1078. #endif