trace_eprobe.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * event probes
  4. *
  5. * Part of this code was copied from kernel/trace/trace_kprobe.c written by
  6. * Masami Hiramatsu <[email protected]>
  7. *
  8. * Copyright (C) 2021, VMware Inc, Steven Rostedt <[email protected]>
  9. * Copyright (C) 2021, VMware Inc, Tzvetomir Stoyanov [email protected]>
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/mutex.h>
  14. #include <linux/ftrace.h>
  15. #include "trace_dynevent.h"
  16. #include "trace_probe.h"
  17. #include "trace_probe_tmpl.h"
  18. #include "trace_probe_kernel.h"
  19. #define EPROBE_EVENT_SYSTEM "eprobes"
  20. struct trace_eprobe {
  21. /* tracepoint system */
  22. const char *event_system;
  23. /* tracepoint event */
  24. const char *event_name;
  25. /* filter string for the tracepoint */
  26. char *filter_str;
  27. struct trace_event_call *event;
  28. struct dyn_event devent;
  29. struct trace_probe tp;
  30. };
  31. struct eprobe_data {
  32. struct trace_event_file *file;
  33. struct trace_eprobe *ep;
  34. };
  35. static int __trace_eprobe_create(int argc, const char *argv[]);
  36. static void trace_event_probe_cleanup(struct trace_eprobe *ep)
  37. {
  38. if (!ep)
  39. return;
  40. trace_probe_cleanup(&ep->tp);
  41. kfree(ep->event_name);
  42. kfree(ep->event_system);
  43. if (ep->event)
  44. trace_event_put_ref(ep->event);
  45. kfree(ep->filter_str);
  46. kfree(ep);
  47. }
  48. static struct trace_eprobe *to_trace_eprobe(struct dyn_event *ev)
  49. {
  50. return container_of(ev, struct trace_eprobe, devent);
  51. }
  52. static int eprobe_dyn_event_create(const char *raw_command)
  53. {
  54. return trace_probe_create(raw_command, __trace_eprobe_create);
  55. }
  56. static int eprobe_dyn_event_show(struct seq_file *m, struct dyn_event *ev)
  57. {
  58. struct trace_eprobe *ep = to_trace_eprobe(ev);
  59. int i;
  60. seq_printf(m, "e:%s/%s", trace_probe_group_name(&ep->tp),
  61. trace_probe_name(&ep->tp));
  62. seq_printf(m, " %s.%s", ep->event_system, ep->event_name);
  63. for (i = 0; i < ep->tp.nr_args; i++)
  64. seq_printf(m, " %s=%s", ep->tp.args[i].name, ep->tp.args[i].comm);
  65. seq_putc(m, '\n');
  66. return 0;
  67. }
  68. static int unregister_trace_eprobe(struct trace_eprobe *ep)
  69. {
  70. /* If other probes are on the event, just unregister eprobe */
  71. if (trace_probe_has_sibling(&ep->tp))
  72. goto unreg;
  73. /* Enabled event can not be unregistered */
  74. if (trace_probe_is_enabled(&ep->tp))
  75. return -EBUSY;
  76. /* Will fail if probe is being used by ftrace or perf */
  77. if (trace_probe_unregister_event_call(&ep->tp))
  78. return -EBUSY;
  79. unreg:
  80. dyn_event_remove(&ep->devent);
  81. trace_probe_unlink(&ep->tp);
  82. return 0;
  83. }
  84. static int eprobe_dyn_event_release(struct dyn_event *ev)
  85. {
  86. struct trace_eprobe *ep = to_trace_eprobe(ev);
  87. int ret = unregister_trace_eprobe(ep);
  88. if (!ret)
  89. trace_event_probe_cleanup(ep);
  90. return ret;
  91. }
  92. static bool eprobe_dyn_event_is_busy(struct dyn_event *ev)
  93. {
  94. struct trace_eprobe *ep = to_trace_eprobe(ev);
  95. return trace_probe_is_enabled(&ep->tp);
  96. }
  97. static bool eprobe_dyn_event_match(const char *system, const char *event,
  98. int argc, const char **argv, struct dyn_event *ev)
  99. {
  100. struct trace_eprobe *ep = to_trace_eprobe(ev);
  101. const char *slash;
  102. /*
  103. * We match the following:
  104. * event only - match all eprobes with event name
  105. * system and event only - match all system/event probes
  106. * system only - match all system probes
  107. *
  108. * The below has the above satisfied with more arguments:
  109. *
  110. * attached system/event - If the arg has the system and event
  111. * the probe is attached to, match
  112. * probes with the attachment.
  113. *
  114. * If any more args are given, then it requires a full match.
  115. */
  116. /*
  117. * If system exists, but this probe is not part of that system
  118. * do not match.
  119. */
  120. if (system && strcmp(trace_probe_group_name(&ep->tp), system) != 0)
  121. return false;
  122. /* Must match the event name */
  123. if (event[0] != '\0' && strcmp(trace_probe_name(&ep->tp), event) != 0)
  124. return false;
  125. /* No arguments match all */
  126. if (argc < 1)
  127. return true;
  128. /* First argument is the system/event the probe is attached to */
  129. slash = strchr(argv[0], '/');
  130. if (!slash)
  131. slash = strchr(argv[0], '.');
  132. if (!slash)
  133. return false;
  134. if (strncmp(ep->event_system, argv[0], slash - argv[0]))
  135. return false;
  136. if (strcmp(ep->event_name, slash + 1))
  137. return false;
  138. argc--;
  139. argv++;
  140. /* If there are no other args, then match */
  141. if (argc < 1)
  142. return true;
  143. return trace_probe_match_command_args(&ep->tp, argc, argv);
  144. }
  145. static struct dyn_event_operations eprobe_dyn_event_ops = {
  146. .create = eprobe_dyn_event_create,
  147. .show = eprobe_dyn_event_show,
  148. .is_busy = eprobe_dyn_event_is_busy,
  149. .free = eprobe_dyn_event_release,
  150. .match = eprobe_dyn_event_match,
  151. };
  152. static struct trace_eprobe *alloc_event_probe(const char *group,
  153. const char *this_event,
  154. struct trace_event_call *event,
  155. int nargs)
  156. {
  157. struct trace_eprobe *ep;
  158. const char *event_name;
  159. const char *sys_name;
  160. int ret = -ENOMEM;
  161. if (!event)
  162. return ERR_PTR(-ENODEV);
  163. sys_name = event->class->system;
  164. event_name = trace_event_name(event);
  165. ep = kzalloc(struct_size(ep, tp.args, nargs), GFP_KERNEL);
  166. if (!ep) {
  167. trace_event_put_ref(event);
  168. goto error;
  169. }
  170. ep->event = event;
  171. ep->event_name = kstrdup(event_name, GFP_KERNEL);
  172. if (!ep->event_name)
  173. goto error;
  174. ep->event_system = kstrdup(sys_name, GFP_KERNEL);
  175. if (!ep->event_system)
  176. goto error;
  177. ret = trace_probe_init(&ep->tp, this_event, group, false);
  178. if (ret < 0)
  179. goto error;
  180. dyn_event_init(&ep->devent, &eprobe_dyn_event_ops);
  181. return ep;
  182. error:
  183. trace_event_probe_cleanup(ep);
  184. return ERR_PTR(ret);
  185. }
  186. static int trace_eprobe_tp_arg_update(struct trace_eprobe *ep, int i)
  187. {
  188. struct probe_arg *parg = &ep->tp.args[i];
  189. struct ftrace_event_field *field;
  190. struct list_head *head;
  191. int ret = -ENOENT;
  192. head = trace_get_fields(ep->event);
  193. list_for_each_entry(field, head, link) {
  194. if (!strcmp(parg->code->data, field->name)) {
  195. kfree(parg->code->data);
  196. parg->code->data = field;
  197. return 0;
  198. }
  199. }
  200. /*
  201. * Argument not found on event. But allow for comm and COMM
  202. * to be used to get the current->comm.
  203. */
  204. if (strcmp(parg->code->data, "COMM") == 0 ||
  205. strcmp(parg->code->data, "comm") == 0) {
  206. parg->code->op = FETCH_OP_COMM;
  207. ret = 0;
  208. }
  209. kfree(parg->code->data);
  210. parg->code->data = NULL;
  211. return ret;
  212. }
  213. static int eprobe_event_define_fields(struct trace_event_call *event_call)
  214. {
  215. struct eprobe_trace_entry_head field;
  216. struct trace_probe *tp;
  217. tp = trace_probe_primary_from_call(event_call);
  218. if (WARN_ON_ONCE(!tp))
  219. return -ENOENT;
  220. return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
  221. }
  222. static struct trace_event_fields eprobe_fields_array[] = {
  223. { .type = TRACE_FUNCTION_TYPE,
  224. .define_fields = eprobe_event_define_fields },
  225. {}
  226. };
  227. /* Event entry printers */
  228. static enum print_line_t
  229. print_eprobe_event(struct trace_iterator *iter, int flags,
  230. struct trace_event *event)
  231. {
  232. struct eprobe_trace_entry_head *field;
  233. struct trace_event_call *pevent;
  234. struct trace_event *probed_event;
  235. struct trace_seq *s = &iter->seq;
  236. struct trace_eprobe *ep;
  237. struct trace_probe *tp;
  238. unsigned int type;
  239. field = (struct eprobe_trace_entry_head *)iter->ent;
  240. tp = trace_probe_primary_from_call(
  241. container_of(event, struct trace_event_call, event));
  242. if (WARN_ON_ONCE(!tp))
  243. goto out;
  244. ep = container_of(tp, struct trace_eprobe, tp);
  245. type = ep->event->event.type;
  246. trace_seq_printf(s, "%s: (", trace_probe_name(tp));
  247. probed_event = ftrace_find_event(type);
  248. if (probed_event) {
  249. pevent = container_of(probed_event, struct trace_event_call, event);
  250. trace_seq_printf(s, "%s.%s", pevent->class->system,
  251. trace_event_name(pevent));
  252. } else {
  253. trace_seq_printf(s, "%u", type);
  254. }
  255. trace_seq_putc(s, ')');
  256. if (print_probe_args(s, tp->args, tp->nr_args,
  257. (u8 *)&field[1], field) < 0)
  258. goto out;
  259. trace_seq_putc(s, '\n');
  260. out:
  261. return trace_handle_return(s);
  262. }
  263. static unsigned long get_event_field(struct fetch_insn *code, void *rec)
  264. {
  265. struct ftrace_event_field *field = code->data;
  266. unsigned long val;
  267. void *addr;
  268. addr = rec + field->offset;
  269. if (is_string_field(field)) {
  270. switch (field->filter_type) {
  271. case FILTER_DYN_STRING:
  272. val = (unsigned long)(rec + (*(unsigned int *)addr & 0xffff));
  273. break;
  274. case FILTER_RDYN_STRING:
  275. val = (unsigned long)(addr + (*(unsigned int *)addr & 0xffff));
  276. break;
  277. case FILTER_STATIC_STRING:
  278. val = (unsigned long)addr;
  279. break;
  280. case FILTER_PTR_STRING:
  281. val = (unsigned long)(*(char *)addr);
  282. break;
  283. default:
  284. WARN_ON_ONCE(1);
  285. return 0;
  286. }
  287. return val;
  288. }
  289. switch (field->size) {
  290. case 1:
  291. if (field->is_signed)
  292. val = *(char *)addr;
  293. else
  294. val = *(unsigned char *)addr;
  295. break;
  296. case 2:
  297. if (field->is_signed)
  298. val = *(short *)addr;
  299. else
  300. val = *(unsigned short *)addr;
  301. break;
  302. case 4:
  303. if (field->is_signed)
  304. val = *(int *)addr;
  305. else
  306. val = *(unsigned int *)addr;
  307. break;
  308. default:
  309. if (field->is_signed)
  310. val = *(long *)addr;
  311. else
  312. val = *(unsigned long *)addr;
  313. break;
  314. }
  315. return val;
  316. }
  317. static int get_eprobe_size(struct trace_probe *tp, void *rec)
  318. {
  319. struct fetch_insn *code;
  320. struct probe_arg *arg;
  321. int i, len, ret = 0;
  322. for (i = 0; i < tp->nr_args; i++) {
  323. arg = tp->args + i;
  324. if (arg->dynamic) {
  325. unsigned long val;
  326. code = arg->code;
  327. retry:
  328. switch (code->op) {
  329. case FETCH_OP_TP_ARG:
  330. val = get_event_field(code, rec);
  331. break;
  332. case FETCH_OP_IMM:
  333. val = code->immediate;
  334. break;
  335. case FETCH_OP_COMM:
  336. val = (unsigned long)current->comm;
  337. break;
  338. case FETCH_OP_DATA:
  339. val = (unsigned long)code->data;
  340. break;
  341. case FETCH_NOP_SYMBOL: /* Ignore a place holder */
  342. code++;
  343. goto retry;
  344. default:
  345. continue;
  346. }
  347. code++;
  348. len = process_fetch_insn_bottom(code, val, NULL, NULL);
  349. if (len > 0)
  350. ret += len;
  351. }
  352. }
  353. return ret;
  354. }
  355. /* Kprobe specific fetch functions */
  356. /* Note that we don't verify it, since the code does not come from user space */
  357. static int
  358. process_fetch_insn(struct fetch_insn *code, void *rec, void *dest,
  359. void *base)
  360. {
  361. unsigned long val;
  362. retry:
  363. switch (code->op) {
  364. case FETCH_OP_TP_ARG:
  365. val = get_event_field(code, rec);
  366. break;
  367. case FETCH_OP_IMM:
  368. val = code->immediate;
  369. break;
  370. case FETCH_OP_COMM:
  371. val = (unsigned long)current->comm;
  372. break;
  373. case FETCH_OP_DATA:
  374. val = (unsigned long)code->data;
  375. break;
  376. case FETCH_NOP_SYMBOL: /* Ignore a place holder */
  377. code++;
  378. goto retry;
  379. default:
  380. return -EILSEQ;
  381. }
  382. code++;
  383. return process_fetch_insn_bottom(code, val, dest, base);
  384. }
  385. NOKPROBE_SYMBOL(process_fetch_insn)
  386. /* Return the length of string -- including null terminal byte */
  387. static nokprobe_inline int
  388. fetch_store_strlen_user(unsigned long addr)
  389. {
  390. return kern_fetch_store_strlen_user(addr);
  391. }
  392. /* Return the length of string -- including null terminal byte */
  393. static nokprobe_inline int
  394. fetch_store_strlen(unsigned long addr)
  395. {
  396. return kern_fetch_store_strlen(addr);
  397. }
  398. /*
  399. * Fetch a null-terminated string from user. Caller MUST set *(u32 *)buf
  400. * with max length and relative data location.
  401. */
  402. static nokprobe_inline int
  403. fetch_store_string_user(unsigned long addr, void *dest, void *base)
  404. {
  405. return kern_fetch_store_string_user(addr, dest, base);
  406. }
  407. /*
  408. * Fetch a null-terminated string. Caller MUST set *(u32 *)buf with max
  409. * length and relative data location.
  410. */
  411. static nokprobe_inline int
  412. fetch_store_string(unsigned long addr, void *dest, void *base)
  413. {
  414. return kern_fetch_store_string(addr, dest, base);
  415. }
  416. static nokprobe_inline int
  417. probe_mem_read_user(void *dest, void *src, size_t size)
  418. {
  419. const void __user *uaddr = (__force const void __user *)src;
  420. return copy_from_user_nofault(dest, uaddr, size);
  421. }
  422. static nokprobe_inline int
  423. probe_mem_read(void *dest, void *src, size_t size)
  424. {
  425. #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
  426. if ((unsigned long)src < TASK_SIZE)
  427. return probe_mem_read_user(dest, src, size);
  428. #endif
  429. return copy_from_kernel_nofault(dest, src, size);
  430. }
  431. /* eprobe handler */
  432. static inline void
  433. __eprobe_trace_func(struct eprobe_data *edata, void *rec)
  434. {
  435. struct eprobe_trace_entry_head *entry;
  436. struct trace_event_call *call = trace_probe_event_call(&edata->ep->tp);
  437. struct trace_event_buffer fbuffer;
  438. int dsize;
  439. if (WARN_ON_ONCE(call != edata->file->event_call))
  440. return;
  441. if (trace_trigger_soft_disabled(edata->file))
  442. return;
  443. dsize = get_eprobe_size(&edata->ep->tp, rec);
  444. entry = trace_event_buffer_reserve(&fbuffer, edata->file,
  445. sizeof(*entry) + edata->ep->tp.size + dsize);
  446. if (!entry)
  447. return;
  448. entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event);
  449. store_trace_args(&entry[1], &edata->ep->tp, rec, sizeof(*entry), dsize);
  450. trace_event_buffer_commit(&fbuffer);
  451. }
  452. /*
  453. * The event probe implementation uses event triggers to get access to
  454. * the event it is attached to, but is not an actual trigger. The below
  455. * functions are just stubs to fulfill what is needed to use the trigger
  456. * infrastructure.
  457. */
  458. static int eprobe_trigger_init(struct event_trigger_data *data)
  459. {
  460. return 0;
  461. }
  462. static void eprobe_trigger_free(struct event_trigger_data *data)
  463. {
  464. }
  465. static int eprobe_trigger_print(struct seq_file *m,
  466. struct event_trigger_data *data)
  467. {
  468. /* Do not print eprobe event triggers */
  469. return 0;
  470. }
  471. static void eprobe_trigger_func(struct event_trigger_data *data,
  472. struct trace_buffer *buffer, void *rec,
  473. struct ring_buffer_event *rbe)
  474. {
  475. struct eprobe_data *edata = data->private_data;
  476. if (unlikely(!rec))
  477. return;
  478. if (unlikely(!rec))
  479. return;
  480. __eprobe_trace_func(edata, rec);
  481. }
  482. static struct event_trigger_ops eprobe_trigger_ops = {
  483. .trigger = eprobe_trigger_func,
  484. .print = eprobe_trigger_print,
  485. .init = eprobe_trigger_init,
  486. .free = eprobe_trigger_free,
  487. };
  488. static int eprobe_trigger_cmd_parse(struct event_command *cmd_ops,
  489. struct trace_event_file *file,
  490. char *glob, char *cmd,
  491. char *param_and_filter)
  492. {
  493. return -1;
  494. }
  495. static int eprobe_trigger_reg_func(char *glob,
  496. struct event_trigger_data *data,
  497. struct trace_event_file *file)
  498. {
  499. return -1;
  500. }
  501. static void eprobe_trigger_unreg_func(char *glob,
  502. struct event_trigger_data *data,
  503. struct trace_event_file *file)
  504. {
  505. }
  506. static struct event_trigger_ops *eprobe_trigger_get_ops(char *cmd,
  507. char *param)
  508. {
  509. return &eprobe_trigger_ops;
  510. }
  511. static struct event_command event_trigger_cmd = {
  512. .name = "eprobe",
  513. .trigger_type = ETT_EVENT_EPROBE,
  514. .flags = EVENT_CMD_FL_NEEDS_REC,
  515. .parse = eprobe_trigger_cmd_parse,
  516. .reg = eprobe_trigger_reg_func,
  517. .unreg = eprobe_trigger_unreg_func,
  518. .unreg_all = NULL,
  519. .get_trigger_ops = eprobe_trigger_get_ops,
  520. .set_filter = NULL,
  521. };
  522. static struct event_trigger_data *
  523. new_eprobe_trigger(struct trace_eprobe *ep, struct trace_event_file *file)
  524. {
  525. struct event_trigger_data *trigger;
  526. struct event_filter *filter = NULL;
  527. struct eprobe_data *edata;
  528. int ret;
  529. edata = kzalloc(sizeof(*edata), GFP_KERNEL);
  530. trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
  531. if (!trigger || !edata) {
  532. ret = -ENOMEM;
  533. goto error;
  534. }
  535. trigger->flags = EVENT_TRIGGER_FL_PROBE;
  536. trigger->count = -1;
  537. trigger->ops = &eprobe_trigger_ops;
  538. /*
  539. * EVENT PROBE triggers are not registered as commands with
  540. * register_event_command(), as they are not controlled by the user
  541. * from the trigger file
  542. */
  543. trigger->cmd_ops = &event_trigger_cmd;
  544. INIT_LIST_HEAD(&trigger->list);
  545. if (ep->filter_str) {
  546. ret = create_event_filter(file->tr, ep->event,
  547. ep->filter_str, false, &filter);
  548. if (ret)
  549. goto error;
  550. }
  551. RCU_INIT_POINTER(trigger->filter, filter);
  552. edata->file = file;
  553. edata->ep = ep;
  554. trigger->private_data = edata;
  555. return trigger;
  556. error:
  557. free_event_filter(filter);
  558. kfree(edata);
  559. kfree(trigger);
  560. return ERR_PTR(ret);
  561. }
  562. static int enable_eprobe(struct trace_eprobe *ep,
  563. struct trace_event_file *eprobe_file)
  564. {
  565. struct event_trigger_data *trigger;
  566. struct trace_event_file *file;
  567. struct trace_array *tr = eprobe_file->tr;
  568. file = find_event_file(tr, ep->event_system, ep->event_name);
  569. if (!file)
  570. return -ENOENT;
  571. trigger = new_eprobe_trigger(ep, eprobe_file);
  572. if (IS_ERR(trigger))
  573. return PTR_ERR(trigger);
  574. list_add_tail_rcu(&trigger->list, &file->triggers);
  575. trace_event_trigger_enable_disable(file, 1);
  576. update_cond_flag(file);
  577. return 0;
  578. }
  579. static struct trace_event_functions eprobe_funcs = {
  580. .trace = print_eprobe_event
  581. };
  582. static int disable_eprobe(struct trace_eprobe *ep,
  583. struct trace_array *tr)
  584. {
  585. struct event_trigger_data *trigger = NULL, *iter;
  586. struct trace_event_file *file;
  587. struct event_filter *filter;
  588. struct eprobe_data *edata;
  589. file = find_event_file(tr, ep->event_system, ep->event_name);
  590. if (!file)
  591. return -ENOENT;
  592. list_for_each_entry(iter, &file->triggers, list) {
  593. if (!(iter->flags & EVENT_TRIGGER_FL_PROBE))
  594. continue;
  595. edata = iter->private_data;
  596. if (edata->ep == ep) {
  597. trigger = iter;
  598. break;
  599. }
  600. }
  601. if (!trigger)
  602. return -ENODEV;
  603. list_del_rcu(&trigger->list);
  604. trace_event_trigger_enable_disable(file, 0);
  605. update_cond_flag(file);
  606. /* Make sure nothing is using the edata or trigger */
  607. tracepoint_synchronize_unregister();
  608. filter = rcu_access_pointer(trigger->filter);
  609. if (filter)
  610. free_event_filter(filter);
  611. kfree(edata);
  612. kfree(trigger);
  613. return 0;
  614. }
  615. static int enable_trace_eprobe(struct trace_event_call *call,
  616. struct trace_event_file *file)
  617. {
  618. struct trace_probe *pos, *tp;
  619. struct trace_eprobe *ep;
  620. bool enabled;
  621. int ret = 0;
  622. int cnt = 0;
  623. tp = trace_probe_primary_from_call(call);
  624. if (WARN_ON_ONCE(!tp))
  625. return -ENODEV;
  626. enabled = trace_probe_is_enabled(tp);
  627. /* This also changes "enabled" state */
  628. if (file) {
  629. ret = trace_probe_add_file(tp, file);
  630. if (ret)
  631. return ret;
  632. } else
  633. trace_probe_set_flag(tp, TP_FLAG_PROFILE);
  634. if (enabled)
  635. return 0;
  636. list_for_each_entry(pos, trace_probe_probe_list(tp), list) {
  637. ep = container_of(pos, struct trace_eprobe, tp);
  638. ret = enable_eprobe(ep, file);
  639. if (ret)
  640. break;
  641. enabled = true;
  642. cnt++;
  643. }
  644. if (ret) {
  645. /* Failed to enable one of them. Roll back all */
  646. if (enabled) {
  647. /*
  648. * It's a bug if one failed for something other than memory
  649. * not being available but another eprobe succeeded.
  650. */
  651. WARN_ON_ONCE(ret != -ENOMEM);
  652. list_for_each_entry(pos, trace_probe_probe_list(tp), list) {
  653. ep = container_of(pos, struct trace_eprobe, tp);
  654. disable_eprobe(ep, file->tr);
  655. if (!--cnt)
  656. break;
  657. }
  658. }
  659. if (file)
  660. trace_probe_remove_file(tp, file);
  661. else
  662. trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
  663. }
  664. return ret;
  665. }
  666. static int disable_trace_eprobe(struct trace_event_call *call,
  667. struct trace_event_file *file)
  668. {
  669. struct trace_probe *pos, *tp;
  670. struct trace_eprobe *ep;
  671. tp = trace_probe_primary_from_call(call);
  672. if (WARN_ON_ONCE(!tp))
  673. return -ENODEV;
  674. if (file) {
  675. if (!trace_probe_get_file_link(tp, file))
  676. return -ENOENT;
  677. if (!trace_probe_has_single_file(tp))
  678. goto out;
  679. trace_probe_clear_flag(tp, TP_FLAG_TRACE);
  680. } else
  681. trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
  682. if (!trace_probe_is_enabled(tp)) {
  683. list_for_each_entry(pos, trace_probe_probe_list(tp), list) {
  684. ep = container_of(pos, struct trace_eprobe, tp);
  685. disable_eprobe(ep, file->tr);
  686. }
  687. }
  688. out:
  689. if (file)
  690. /*
  691. * Synchronization is done in below function. For perf event,
  692. * file == NULL and perf_trace_event_unreg() calls
  693. * tracepoint_synchronize_unregister() to ensure synchronize
  694. * event. We don't need to care about it.
  695. */
  696. trace_probe_remove_file(tp, file);
  697. return 0;
  698. }
  699. static int eprobe_register(struct trace_event_call *event,
  700. enum trace_reg type, void *data)
  701. {
  702. struct trace_event_file *file = data;
  703. switch (type) {
  704. case TRACE_REG_REGISTER:
  705. return enable_trace_eprobe(event, file);
  706. case TRACE_REG_UNREGISTER:
  707. return disable_trace_eprobe(event, file);
  708. #ifdef CONFIG_PERF_EVENTS
  709. case TRACE_REG_PERF_REGISTER:
  710. case TRACE_REG_PERF_UNREGISTER:
  711. case TRACE_REG_PERF_OPEN:
  712. case TRACE_REG_PERF_CLOSE:
  713. case TRACE_REG_PERF_ADD:
  714. case TRACE_REG_PERF_DEL:
  715. return 0;
  716. #endif
  717. }
  718. return 0;
  719. }
  720. static inline void init_trace_eprobe_call(struct trace_eprobe *ep)
  721. {
  722. struct trace_event_call *call = trace_probe_event_call(&ep->tp);
  723. call->flags = TRACE_EVENT_FL_EPROBE;
  724. call->event.funcs = &eprobe_funcs;
  725. call->class->fields_array = eprobe_fields_array;
  726. call->class->reg = eprobe_register;
  727. }
  728. static struct trace_event_call *
  729. find_and_get_event(const char *system, const char *event_name)
  730. {
  731. struct trace_event_call *tp_event;
  732. const char *name;
  733. list_for_each_entry(tp_event, &ftrace_events, list) {
  734. /* Skip other probes and ftrace events */
  735. if (tp_event->flags &
  736. (TRACE_EVENT_FL_IGNORE_ENABLE |
  737. TRACE_EVENT_FL_KPROBE |
  738. TRACE_EVENT_FL_UPROBE |
  739. TRACE_EVENT_FL_EPROBE))
  740. continue;
  741. if (!tp_event->class->system ||
  742. strcmp(system, tp_event->class->system))
  743. continue;
  744. name = trace_event_name(tp_event);
  745. if (!name || strcmp(event_name, name))
  746. continue;
  747. if (!trace_event_try_get_ref(tp_event)) {
  748. return NULL;
  749. break;
  750. }
  751. return tp_event;
  752. break;
  753. }
  754. return NULL;
  755. }
  756. static int trace_eprobe_tp_update_arg(struct trace_eprobe *ep, const char *argv[], int i)
  757. {
  758. unsigned int flags = TPARG_FL_KERNEL | TPARG_FL_TPOINT;
  759. int ret;
  760. ret = traceprobe_parse_probe_arg(&ep->tp, i, argv[i], flags);
  761. if (ret)
  762. return ret;
  763. if (ep->tp.args[i].code->op == FETCH_OP_TP_ARG) {
  764. ret = trace_eprobe_tp_arg_update(ep, i);
  765. if (ret)
  766. trace_probe_log_err(0, BAD_ATTACH_ARG);
  767. }
  768. /* Handle symbols "@" */
  769. if (!ret)
  770. ret = traceprobe_update_arg(&ep->tp.args[i]);
  771. return ret;
  772. }
  773. static int trace_eprobe_parse_filter(struct trace_eprobe *ep, int argc, const char *argv[])
  774. {
  775. struct event_filter *dummy = NULL;
  776. int i, ret, len = 0;
  777. char *p;
  778. if (argc == 0) {
  779. trace_probe_log_err(0, NO_EP_FILTER);
  780. return -EINVAL;
  781. }
  782. /* Recover the filter string */
  783. for (i = 0; i < argc; i++)
  784. len += strlen(argv[i]) + 1;
  785. ep->filter_str = kzalloc(len, GFP_KERNEL);
  786. if (!ep->filter_str)
  787. return -ENOMEM;
  788. p = ep->filter_str;
  789. for (i = 0; i < argc; i++) {
  790. ret = snprintf(p, len, "%s ", argv[i]);
  791. if (ret < 0)
  792. goto error;
  793. if (ret > len) {
  794. ret = -E2BIG;
  795. goto error;
  796. }
  797. p += ret;
  798. len -= ret;
  799. }
  800. p[-1] = '\0';
  801. /*
  802. * Ensure the filter string can be parsed correctly. Note, this
  803. * filter string is for the original event, not for the eprobe.
  804. */
  805. ret = create_event_filter(top_trace_array(), ep->event, ep->filter_str,
  806. true, &dummy);
  807. free_event_filter(dummy);
  808. if (ret)
  809. goto error;
  810. return 0;
  811. error:
  812. kfree(ep->filter_str);
  813. ep->filter_str = NULL;
  814. return ret;
  815. }
  816. static int __trace_eprobe_create(int argc, const char *argv[])
  817. {
  818. /*
  819. * Argument syntax:
  820. * e[:[GRP/][ENAME]] SYSTEM.EVENT [FETCHARGS] [if FILTER]
  821. * Fetch args (no space):
  822. * <name>=$<field>[:TYPE]
  823. */
  824. const char *event = NULL, *group = EPROBE_EVENT_SYSTEM;
  825. const char *sys_event = NULL, *sys_name = NULL;
  826. struct trace_event_call *event_call;
  827. struct trace_eprobe *ep = NULL;
  828. char buf1[MAX_EVENT_NAME_LEN];
  829. char buf2[MAX_EVENT_NAME_LEN];
  830. char gbuf[MAX_EVENT_NAME_LEN];
  831. int ret = 0, filter_idx = 0;
  832. int i, filter_cnt;
  833. if (argc < 2 || argv[0][0] != 'e')
  834. return -ECANCELED;
  835. trace_probe_log_init("event_probe", argc, argv);
  836. event = strchr(&argv[0][1], ':');
  837. if (event) {
  838. event++;
  839. ret = traceprobe_parse_event_name(&event, &group, gbuf,
  840. event - argv[0]);
  841. if (ret)
  842. goto parse_error;
  843. }
  844. trace_probe_log_set_index(1);
  845. sys_event = argv[1];
  846. ret = traceprobe_parse_event_name(&sys_event, &sys_name, buf2, 0);
  847. if (ret || !sys_event || !sys_name) {
  848. trace_probe_log_err(0, NO_EVENT_INFO);
  849. goto parse_error;
  850. }
  851. if (!event) {
  852. strscpy(buf1, sys_event, MAX_EVENT_NAME_LEN);
  853. event = buf1;
  854. }
  855. for (i = 2; i < argc; i++) {
  856. if (!strcmp(argv[i], "if")) {
  857. filter_idx = i + 1;
  858. filter_cnt = argc - filter_idx;
  859. argc = i;
  860. break;
  861. }
  862. }
  863. mutex_lock(&event_mutex);
  864. event_call = find_and_get_event(sys_name, sys_event);
  865. ep = alloc_event_probe(group, event, event_call, argc - 2);
  866. mutex_unlock(&event_mutex);
  867. if (IS_ERR(ep)) {
  868. ret = PTR_ERR(ep);
  869. if (ret == -ENODEV)
  870. trace_probe_log_err(0, BAD_ATTACH_EVENT);
  871. /* This must return -ENOMEM or missing event, else there is a bug */
  872. WARN_ON_ONCE(ret != -ENOMEM && ret != -ENODEV);
  873. ep = NULL;
  874. goto error;
  875. }
  876. if (filter_idx) {
  877. trace_probe_log_set_index(filter_idx);
  878. ret = trace_eprobe_parse_filter(ep, filter_cnt, argv + filter_idx);
  879. if (ret)
  880. goto parse_error;
  881. } else
  882. ep->filter_str = NULL;
  883. argc -= 2; argv += 2;
  884. /* parse arguments */
  885. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  886. trace_probe_log_set_index(i + 2);
  887. ret = trace_eprobe_tp_update_arg(ep, argv, i);
  888. if (ret)
  889. goto error;
  890. }
  891. ret = traceprobe_set_print_fmt(&ep->tp, PROBE_PRINT_EVENT);
  892. if (ret < 0)
  893. goto error;
  894. init_trace_eprobe_call(ep);
  895. mutex_lock(&event_mutex);
  896. ret = trace_probe_register_event_call(&ep->tp);
  897. if (ret) {
  898. if (ret == -EEXIST) {
  899. trace_probe_log_set_index(0);
  900. trace_probe_log_err(0, EVENT_EXIST);
  901. }
  902. mutex_unlock(&event_mutex);
  903. goto error;
  904. }
  905. ret = dyn_event_add(&ep->devent, &ep->tp.event->call);
  906. mutex_unlock(&event_mutex);
  907. return ret;
  908. parse_error:
  909. ret = -EINVAL;
  910. error:
  911. trace_event_probe_cleanup(ep);
  912. return ret;
  913. }
  914. /*
  915. * Register dynevent at core_initcall. This allows kernel to setup eprobe
  916. * events in postcore_initcall without tracefs.
  917. */
  918. static __init int trace_events_eprobe_init_early(void)
  919. {
  920. int err = 0;
  921. err = dyn_event_register(&eprobe_dyn_event_ops);
  922. if (err)
  923. pr_warn("Could not register eprobe_dyn_event_ops\n");
  924. return err;
  925. }
  926. core_initcall(trace_events_eprobe_init_early);