trace_uprobe.c 18 KB

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