trace_uprobe.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. /*
  2. * uprobes-based tracing events
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. *
  17. * Copyright (C) IBM Corporation, 2010-2012
  18. * Author: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
  19. */
  20. #include <linux/module.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/uprobes.h>
  23. #include <linux/namei.h>
  24. #include <linux/string.h>
  25. #include "trace_probe.h"
  26. #define UPROBE_EVENT_SYSTEM "uprobes"
  27. /*
  28. * uprobe event core functions
  29. */
  30. struct trace_uprobe {
  31. struct list_head list;
  32. struct ftrace_event_class class;
  33. struct ftrace_event_call call;
  34. struct uprobe_consumer consumer;
  35. struct inode *inode;
  36. char *filename;
  37. unsigned long offset;
  38. unsigned long nhit;
  39. unsigned int flags; /* For TP_FLAG_* */
  40. ssize_t size; /* trace entry size */
  41. unsigned int nr_args;
  42. struct probe_arg args[];
  43. };
  44. #define SIZEOF_TRACE_UPROBE(n) \
  45. (offsetof(struct trace_uprobe, args) + \
  46. (sizeof(struct probe_arg) * (n)))
  47. static int register_uprobe_event(struct trace_uprobe *tu);
  48. static void unregister_uprobe_event(struct trace_uprobe *tu);
  49. static DEFINE_MUTEX(uprobe_lock);
  50. static LIST_HEAD(uprobe_list);
  51. static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs);
  52. /*
  53. * Allocate new trace_uprobe and initialize it (including uprobes).
  54. */
  55. static struct trace_uprobe *
  56. alloc_trace_uprobe(const char *group, const char *event, int nargs)
  57. {
  58. struct trace_uprobe *tu;
  59. if (!event || !is_good_name(event))
  60. return ERR_PTR(-EINVAL);
  61. if (!group || !is_good_name(group))
  62. return ERR_PTR(-EINVAL);
  63. tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
  64. if (!tu)
  65. return ERR_PTR(-ENOMEM);
  66. tu->call.class = &tu->class;
  67. tu->call.name = kstrdup(event, GFP_KERNEL);
  68. if (!tu->call.name)
  69. goto error;
  70. tu->class.system = kstrdup(group, GFP_KERNEL);
  71. if (!tu->class.system)
  72. goto error;
  73. INIT_LIST_HEAD(&tu->list);
  74. tu->consumer.handler = uprobe_dispatcher;
  75. return tu;
  76. error:
  77. kfree(tu->call.name);
  78. kfree(tu);
  79. return ERR_PTR(-ENOMEM);
  80. }
  81. static void free_trace_uprobe(struct trace_uprobe *tu)
  82. {
  83. int i;
  84. for (i = 0; i < tu->nr_args; i++)
  85. traceprobe_free_probe_arg(&tu->args[i]);
  86. iput(tu->inode);
  87. kfree(tu->call.class->system);
  88. kfree(tu->call.name);
  89. kfree(tu->filename);
  90. kfree(tu);
  91. }
  92. static struct trace_uprobe *find_probe_event(const char *event, const char *group)
  93. {
  94. struct trace_uprobe *tu;
  95. list_for_each_entry(tu, &uprobe_list, list)
  96. if (strcmp(tu->call.name, event) == 0 &&
  97. strcmp(tu->call.class->system, group) == 0)
  98. return tu;
  99. return NULL;
  100. }
  101. /* Unregister a trace_uprobe and probe_event: call with locking uprobe_lock */
  102. static void unregister_trace_uprobe(struct trace_uprobe *tu)
  103. {
  104. list_del(&tu->list);
  105. unregister_uprobe_event(tu);
  106. free_trace_uprobe(tu);
  107. }
  108. /* Register a trace_uprobe and probe_event */
  109. static int register_trace_uprobe(struct trace_uprobe *tu)
  110. {
  111. struct trace_uprobe *old_tp;
  112. int ret;
  113. mutex_lock(&uprobe_lock);
  114. /* register as an event */
  115. old_tp = find_probe_event(tu->call.name, tu->call.class->system);
  116. if (old_tp)
  117. /* delete old event */
  118. unregister_trace_uprobe(old_tp);
  119. ret = register_uprobe_event(tu);
  120. if (ret) {
  121. pr_warning("Failed to register probe event(%d)\n", ret);
  122. goto end;
  123. }
  124. list_add_tail(&tu->list, &uprobe_list);
  125. end:
  126. mutex_unlock(&uprobe_lock);
  127. return ret;
  128. }
  129. /*
  130. * Argument syntax:
  131. * - Add uprobe: p[:[GRP/]EVENT] PATH:SYMBOL[+offs] [FETCHARGS]
  132. *
  133. * - Remove uprobe: -:[GRP/]EVENT
  134. */
  135. static int create_trace_uprobe(int argc, char **argv)
  136. {
  137. struct trace_uprobe *tu;
  138. struct inode *inode;
  139. char *arg, *event, *group, *filename;
  140. char buf[MAX_EVENT_NAME_LEN];
  141. struct path path;
  142. unsigned long offset;
  143. bool is_delete;
  144. int i, ret;
  145. inode = NULL;
  146. ret = 0;
  147. is_delete = false;
  148. event = NULL;
  149. group = NULL;
  150. /* argc must be >= 1 */
  151. if (argv[0][0] == '-')
  152. is_delete = true;
  153. else if (argv[0][0] != 'p') {
  154. pr_info("Probe definition must be started with 'p' or '-'.\n");
  155. return -EINVAL;
  156. }
  157. if (argv[0][1] == ':') {
  158. event = &argv[0][2];
  159. arg = strchr(event, '/');
  160. if (arg) {
  161. group = event;
  162. event = arg + 1;
  163. event[-1] = '\0';
  164. if (strlen(group) == 0) {
  165. pr_info("Group name is not specified\n");
  166. return -EINVAL;
  167. }
  168. }
  169. if (strlen(event) == 0) {
  170. pr_info("Event name is not specified\n");
  171. return -EINVAL;
  172. }
  173. }
  174. if (!group)
  175. group = UPROBE_EVENT_SYSTEM;
  176. if (is_delete) {
  177. if (!event) {
  178. pr_info("Delete command needs an event name.\n");
  179. return -EINVAL;
  180. }
  181. mutex_lock(&uprobe_lock);
  182. tu = find_probe_event(event, group);
  183. if (!tu) {
  184. mutex_unlock(&uprobe_lock);
  185. pr_info("Event %s/%s doesn't exist.\n", group, event);
  186. return -ENOENT;
  187. }
  188. /* delete an event */
  189. unregister_trace_uprobe(tu);
  190. mutex_unlock(&uprobe_lock);
  191. return 0;
  192. }
  193. if (argc < 2) {
  194. pr_info("Probe point is not specified.\n");
  195. return -EINVAL;
  196. }
  197. if (isdigit(argv[1][0])) {
  198. pr_info("probe point must be have a filename.\n");
  199. return -EINVAL;
  200. }
  201. arg = strchr(argv[1], ':');
  202. if (!arg)
  203. goto fail_address_parse;
  204. *arg++ = '\0';
  205. filename = argv[1];
  206. ret = kern_path(filename, LOOKUP_FOLLOW, &path);
  207. if (ret)
  208. goto fail_address_parse;
  209. inode = igrab(path.dentry->d_inode);
  210. path_put(&path);
  211. if (!inode || !S_ISREG(inode->i_mode)) {
  212. ret = -EINVAL;
  213. goto fail_address_parse;
  214. }
  215. ret = kstrtoul(arg, 0, &offset);
  216. if (ret)
  217. goto fail_address_parse;
  218. argc -= 2;
  219. argv += 2;
  220. /* setup a probe */
  221. if (!event) {
  222. char *tail;
  223. char *ptr;
  224. tail = kstrdup(kbasename(filename), GFP_KERNEL);
  225. if (!tail) {
  226. ret = -ENOMEM;
  227. goto fail_address_parse;
  228. }
  229. ptr = strpbrk(tail, ".-_");
  230. if (ptr)
  231. *ptr = '\0';
  232. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
  233. event = buf;
  234. kfree(tail);
  235. }
  236. tu = alloc_trace_uprobe(group, event, argc);
  237. if (IS_ERR(tu)) {
  238. pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu));
  239. ret = PTR_ERR(tu);
  240. goto fail_address_parse;
  241. }
  242. tu->offset = offset;
  243. tu->inode = inode;
  244. tu->filename = kstrdup(filename, GFP_KERNEL);
  245. if (!tu->filename) {
  246. pr_info("Failed to allocate filename.\n");
  247. ret = -ENOMEM;
  248. goto error;
  249. }
  250. /* parse arguments */
  251. ret = 0;
  252. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  253. /* Increment count for freeing args in error case */
  254. tu->nr_args++;
  255. /* Parse argument name */
  256. arg = strchr(argv[i], '=');
  257. if (arg) {
  258. *arg++ = '\0';
  259. tu->args[i].name = kstrdup(argv[i], GFP_KERNEL);
  260. } else {
  261. arg = argv[i];
  262. /* If argument name is omitted, set "argN" */
  263. snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
  264. tu->args[i].name = kstrdup(buf, GFP_KERNEL);
  265. }
  266. if (!tu->args[i].name) {
  267. pr_info("Failed to allocate argument[%d] name.\n", i);
  268. ret = -ENOMEM;
  269. goto error;
  270. }
  271. if (!is_good_name(tu->args[i].name)) {
  272. pr_info("Invalid argument[%d] name: %s\n", i, tu->args[i].name);
  273. ret = -EINVAL;
  274. goto error;
  275. }
  276. if (traceprobe_conflict_field_name(tu->args[i].name, tu->args, i)) {
  277. pr_info("Argument[%d] name '%s' conflicts with "
  278. "another field.\n", i, argv[i]);
  279. ret = -EINVAL;
  280. goto error;
  281. }
  282. /* Parse fetch argument */
  283. ret = traceprobe_parse_probe_arg(arg, &tu->size, &tu->args[i], false, false);
  284. if (ret) {
  285. pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
  286. goto error;
  287. }
  288. }
  289. ret = register_trace_uprobe(tu);
  290. if (ret)
  291. goto error;
  292. return 0;
  293. error:
  294. free_trace_uprobe(tu);
  295. return ret;
  296. fail_address_parse:
  297. if (inode)
  298. iput(inode);
  299. pr_info("Failed to parse address or file.\n");
  300. return ret;
  301. }
  302. static void cleanup_all_probes(void)
  303. {
  304. struct trace_uprobe *tu;
  305. mutex_lock(&uprobe_lock);
  306. while (!list_empty(&uprobe_list)) {
  307. tu = list_entry(uprobe_list.next, struct trace_uprobe, list);
  308. unregister_trace_uprobe(tu);
  309. }
  310. mutex_unlock(&uprobe_lock);
  311. }
  312. /* Probes listing interfaces */
  313. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  314. {
  315. mutex_lock(&uprobe_lock);
  316. return seq_list_start(&uprobe_list, *pos);
  317. }
  318. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  319. {
  320. return seq_list_next(v, &uprobe_list, pos);
  321. }
  322. static void probes_seq_stop(struct seq_file *m, void *v)
  323. {
  324. mutex_unlock(&uprobe_lock);
  325. }
  326. static int probes_seq_show(struct seq_file *m, void *v)
  327. {
  328. struct trace_uprobe *tu = v;
  329. int i;
  330. seq_printf(m, "p:%s/%s", tu->call.class->system, tu->call.name);
  331. seq_printf(m, " %s:0x%p", tu->filename, (void *)tu->offset);
  332. for (i = 0; i < tu->nr_args; i++)
  333. seq_printf(m, " %s=%s", tu->args[i].name, tu->args[i].comm);
  334. seq_printf(m, "\n");
  335. return 0;
  336. }
  337. static const struct seq_operations probes_seq_op = {
  338. .start = probes_seq_start,
  339. .next = probes_seq_next,
  340. .stop = probes_seq_stop,
  341. .show = probes_seq_show
  342. };
  343. static int probes_open(struct inode *inode, struct file *file)
  344. {
  345. if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC))
  346. cleanup_all_probes();
  347. return seq_open(file, &probes_seq_op);
  348. }
  349. static ssize_t probes_write(struct file *file, const char __user *buffer,
  350. size_t count, loff_t *ppos)
  351. {
  352. return traceprobe_probes_write(file, buffer, count, ppos, create_trace_uprobe);
  353. }
  354. static const struct file_operations uprobe_events_ops = {
  355. .owner = THIS_MODULE,
  356. .open = probes_open,
  357. .read = seq_read,
  358. .llseek = seq_lseek,
  359. .release = seq_release,
  360. .write = probes_write,
  361. };
  362. /* Probes profiling interfaces */
  363. static int probes_profile_seq_show(struct seq_file *m, void *v)
  364. {
  365. struct trace_uprobe *tu = v;
  366. seq_printf(m, " %s %-44s %15lu\n", tu->filename, tu->call.name, tu->nhit);
  367. return 0;
  368. }
  369. static const struct seq_operations profile_seq_op = {
  370. .start = probes_seq_start,
  371. .next = probes_seq_next,
  372. .stop = probes_seq_stop,
  373. .show = probes_profile_seq_show
  374. };
  375. static int profile_open(struct inode *inode, struct file *file)
  376. {
  377. return seq_open(file, &profile_seq_op);
  378. }
  379. static const struct file_operations uprobe_profile_ops = {
  380. .owner = THIS_MODULE,
  381. .open = profile_open,
  382. .read = seq_read,
  383. .llseek = seq_lseek,
  384. .release = seq_release,
  385. };
  386. /* uprobe handler */
  387. static void uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs)
  388. {
  389. struct uprobe_trace_entry_head *entry;
  390. struct ring_buffer_event *event;
  391. struct ring_buffer *buffer;
  392. u8 *data;
  393. int size, i, pc;
  394. unsigned long irq_flags;
  395. struct ftrace_event_call *call = &tu->call;
  396. tu->nhit++;
  397. local_save_flags(irq_flags);
  398. pc = preempt_count();
  399. size = sizeof(*entry) + tu->size;
  400. event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
  401. size, irq_flags, pc);
  402. if (!event)
  403. return;
  404. entry = ring_buffer_event_data(event);
  405. entry->ip = instruction_pointer(task_pt_regs(current));
  406. data = (u8 *)&entry[1];
  407. for (i = 0; i < tu->nr_args; i++)
  408. call_fetch(&tu->args[i].fetch, regs, data + tu->args[i].offset);
  409. if (!filter_current_check_discard(buffer, call, entry, event))
  410. trace_buffer_unlock_commit(buffer, event, irq_flags, pc);
  411. }
  412. /* Event entry printers */
  413. static enum print_line_t
  414. print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
  415. {
  416. struct uprobe_trace_entry_head *field;
  417. struct trace_seq *s = &iter->seq;
  418. struct trace_uprobe *tu;
  419. u8 *data;
  420. int i;
  421. field = (struct uprobe_trace_entry_head *)iter->ent;
  422. tu = container_of(event, struct trace_uprobe, call.event);
  423. if (!trace_seq_printf(s, "%s: (", tu->call.name))
  424. goto partial;
  425. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  426. goto partial;
  427. if (!trace_seq_puts(s, ")"))
  428. goto partial;
  429. data = (u8 *)&field[1];
  430. for (i = 0; i < tu->nr_args; i++) {
  431. if (!tu->args[i].type->print(s, tu->args[i].name,
  432. data + tu->args[i].offset, field))
  433. goto partial;
  434. }
  435. if (trace_seq_puts(s, "\n"))
  436. return TRACE_TYPE_HANDLED;
  437. partial:
  438. return TRACE_TYPE_PARTIAL_LINE;
  439. }
  440. static inline bool is_trace_uprobe_enabled(struct trace_uprobe *tu)
  441. {
  442. return tu->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE);
  443. }
  444. static int probe_event_enable(struct trace_uprobe *tu, int flag)
  445. {
  446. int ret = 0;
  447. if (is_trace_uprobe_enabled(tu))
  448. return -EINTR;
  449. tu->flags |= flag;
  450. ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
  451. if (ret)
  452. tu->flags &= ~flag;
  453. return ret;
  454. }
  455. static void probe_event_disable(struct trace_uprobe *tu, int flag)
  456. {
  457. if (!is_trace_uprobe_enabled(tu))
  458. return;
  459. uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
  460. tu->flags &= ~flag;
  461. }
  462. static int uprobe_event_define_fields(struct ftrace_event_call *event_call)
  463. {
  464. int ret, i;
  465. struct uprobe_trace_entry_head field;
  466. struct trace_uprobe *tu = (struct trace_uprobe *)event_call->data;
  467. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  468. /* Set argument names as fields */
  469. for (i = 0; i < tu->nr_args; i++) {
  470. ret = trace_define_field(event_call, tu->args[i].type->fmttype,
  471. tu->args[i].name,
  472. sizeof(field) + tu->args[i].offset,
  473. tu->args[i].type->size,
  474. tu->args[i].type->is_signed,
  475. FILTER_OTHER);
  476. if (ret)
  477. return ret;
  478. }
  479. return 0;
  480. }
  481. #define LEN_OR_ZERO (len ? len - pos : 0)
  482. static int __set_print_fmt(struct trace_uprobe *tu, char *buf, int len)
  483. {
  484. const char *fmt, *arg;
  485. int i;
  486. int pos = 0;
  487. fmt = "(%lx)";
  488. arg = "REC->" FIELD_STRING_IP;
  489. /* When len=0, we just calculate the needed length */
  490. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
  491. for (i = 0; i < tu->nr_args; i++) {
  492. pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
  493. tu->args[i].name, tu->args[i].type->fmt);
  494. }
  495. pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
  496. for (i = 0; i < tu->nr_args; i++) {
  497. pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
  498. tu->args[i].name);
  499. }
  500. return pos; /* return the length of print_fmt */
  501. }
  502. #undef LEN_OR_ZERO
  503. static int set_print_fmt(struct trace_uprobe *tu)
  504. {
  505. char *print_fmt;
  506. int len;
  507. /* First: called with 0 length to calculate the needed length */
  508. len = __set_print_fmt(tu, NULL, 0);
  509. print_fmt = kmalloc(len + 1, GFP_KERNEL);
  510. if (!print_fmt)
  511. return -ENOMEM;
  512. /* Second: actually write the @print_fmt */
  513. __set_print_fmt(tu, print_fmt, len + 1);
  514. tu->call.print_fmt = print_fmt;
  515. return 0;
  516. }
  517. #ifdef CONFIG_PERF_EVENTS
  518. /* uprobe profile handler */
  519. static void uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs)
  520. {
  521. struct ftrace_event_call *call = &tu->call;
  522. struct uprobe_trace_entry_head *entry;
  523. struct hlist_head *head;
  524. u8 *data;
  525. int size, __size, i;
  526. int rctx;
  527. __size = sizeof(*entry) + tu->size;
  528. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  529. size -= sizeof(u32);
  530. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
  531. return;
  532. preempt_disable();
  533. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  534. if (!entry)
  535. goto out;
  536. entry->ip = instruction_pointer(task_pt_regs(current));
  537. data = (u8 *)&entry[1];
  538. for (i = 0; i < tu->nr_args; i++)
  539. call_fetch(&tu->args[i].fetch, regs, data + tu->args[i].offset);
  540. head = this_cpu_ptr(call->perf_events);
  541. perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, regs, head, NULL);
  542. out:
  543. preempt_enable();
  544. }
  545. #endif /* CONFIG_PERF_EVENTS */
  546. static
  547. int trace_uprobe_register(struct ftrace_event_call *event, enum trace_reg type, void *data)
  548. {
  549. struct trace_uprobe *tu = (struct trace_uprobe *)event->data;
  550. switch (type) {
  551. case TRACE_REG_REGISTER:
  552. return probe_event_enable(tu, TP_FLAG_TRACE);
  553. case TRACE_REG_UNREGISTER:
  554. probe_event_disable(tu, TP_FLAG_TRACE);
  555. return 0;
  556. #ifdef CONFIG_PERF_EVENTS
  557. case TRACE_REG_PERF_REGISTER:
  558. return probe_event_enable(tu, TP_FLAG_PROFILE);
  559. case TRACE_REG_PERF_UNREGISTER:
  560. probe_event_disable(tu, TP_FLAG_PROFILE);
  561. return 0;
  562. #endif
  563. default:
  564. return 0;
  565. }
  566. return 0;
  567. }
  568. static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
  569. {
  570. struct trace_uprobe *tu;
  571. tu = container_of(con, struct trace_uprobe, consumer);
  572. if (tu->flags & TP_FLAG_TRACE)
  573. uprobe_trace_func(tu, regs);
  574. #ifdef CONFIG_PERF_EVENTS
  575. if (tu->flags & TP_FLAG_PROFILE)
  576. uprobe_perf_func(tu, regs);
  577. #endif
  578. return 0;
  579. }
  580. static struct trace_event_functions uprobe_funcs = {
  581. .trace = print_uprobe_event
  582. };
  583. static int register_uprobe_event(struct trace_uprobe *tu)
  584. {
  585. struct ftrace_event_call *call = &tu->call;
  586. int ret;
  587. /* Initialize ftrace_event_call */
  588. INIT_LIST_HEAD(&call->class->fields);
  589. call->event.funcs = &uprobe_funcs;
  590. call->class->define_fields = uprobe_event_define_fields;
  591. if (set_print_fmt(tu) < 0)
  592. return -ENOMEM;
  593. ret = register_ftrace_event(&call->event);
  594. if (!ret) {
  595. kfree(call->print_fmt);
  596. return -ENODEV;
  597. }
  598. call->flags = 0;
  599. call->class->reg = trace_uprobe_register;
  600. call->data = tu;
  601. ret = trace_add_event_call(call);
  602. if (ret) {
  603. pr_info("Failed to register uprobe event: %s\n", call->name);
  604. kfree(call->print_fmt);
  605. unregister_ftrace_event(&call->event);
  606. }
  607. return ret;
  608. }
  609. static void unregister_uprobe_event(struct trace_uprobe *tu)
  610. {
  611. /* tu->event is unregistered in trace_remove_event_call() */
  612. trace_remove_event_call(&tu->call);
  613. kfree(tu->call.print_fmt);
  614. tu->call.print_fmt = NULL;
  615. }
  616. /* Make a trace interface for controling probe points */
  617. static __init int init_uprobe_trace(void)
  618. {
  619. struct dentry *d_tracer;
  620. d_tracer = tracing_init_dentry();
  621. if (!d_tracer)
  622. return 0;
  623. trace_create_file("uprobe_events", 0644, d_tracer,
  624. NULL, &uprobe_events_ops);
  625. /* Profile interface */
  626. trace_create_file("uprobe_profile", 0444, d_tracer,
  627. NULL, &uprobe_profile_ops);
  628. return 0;
  629. }
  630. fs_initcall(init_uprobe_trace);