trace_uprobe.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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. inode = igrab(path.dentry->d_inode);
  214. path_put(&path);
  215. if (!S_ISREG(inode->i_mode)) {
  216. ret = -EINVAL;
  217. goto fail_address_parse;
  218. }
  219. ret = kstrtoul(arg, 0, &offset);
  220. if (ret)
  221. goto fail_address_parse;
  222. argc -= 2;
  223. argv += 2;
  224. /* setup a probe */
  225. if (!event) {
  226. char *tail;
  227. char *ptr;
  228. tail = kstrdup(kbasename(filename), GFP_KERNEL);
  229. if (!tail) {
  230. ret = -ENOMEM;
  231. goto fail_address_parse;
  232. }
  233. ptr = strpbrk(tail, ".-_");
  234. if (ptr)
  235. *ptr = '\0';
  236. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
  237. event = buf;
  238. kfree(tail);
  239. }
  240. tu = alloc_trace_uprobe(group, event, argc);
  241. if (IS_ERR(tu)) {
  242. pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu));
  243. ret = PTR_ERR(tu);
  244. goto fail_address_parse;
  245. }
  246. tu->offset = offset;
  247. tu->inode = inode;
  248. tu->filename = kstrdup(filename, GFP_KERNEL);
  249. if (!tu->filename) {
  250. pr_info("Failed to allocate filename.\n");
  251. ret = -ENOMEM;
  252. goto error;
  253. }
  254. /* parse arguments */
  255. ret = 0;
  256. for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
  257. /* Increment count for freeing args in error case */
  258. tu->nr_args++;
  259. /* Parse argument name */
  260. arg = strchr(argv[i], '=');
  261. if (arg) {
  262. *arg++ = '\0';
  263. tu->args[i].name = kstrdup(argv[i], GFP_KERNEL);
  264. } else {
  265. arg = argv[i];
  266. /* If argument name is omitted, set "argN" */
  267. snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
  268. tu->args[i].name = kstrdup(buf, GFP_KERNEL);
  269. }
  270. if (!tu->args[i].name) {
  271. pr_info("Failed to allocate argument[%d] name.\n", i);
  272. ret = -ENOMEM;
  273. goto error;
  274. }
  275. if (!is_good_name(tu->args[i].name)) {
  276. pr_info("Invalid argument[%d] name: %s\n", i, tu->args[i].name);
  277. ret = -EINVAL;
  278. goto error;
  279. }
  280. if (traceprobe_conflict_field_name(tu->args[i].name, tu->args, i)) {
  281. pr_info("Argument[%d] name '%s' conflicts with "
  282. "another field.\n", i, argv[i]);
  283. ret = -EINVAL;
  284. goto error;
  285. }
  286. /* Parse fetch argument */
  287. ret = traceprobe_parse_probe_arg(arg, &tu->size, &tu->args[i], false, false);
  288. if (ret) {
  289. pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
  290. goto error;
  291. }
  292. }
  293. ret = register_trace_uprobe(tu);
  294. if (ret)
  295. goto error;
  296. return 0;
  297. error:
  298. free_trace_uprobe(tu);
  299. return ret;
  300. fail_address_parse:
  301. if (inode)
  302. iput(inode);
  303. pr_info("Failed to parse address or file.\n");
  304. return ret;
  305. }
  306. static void cleanup_all_probes(void)
  307. {
  308. struct trace_uprobe *tu;
  309. mutex_lock(&uprobe_lock);
  310. while (!list_empty(&uprobe_list)) {
  311. tu = list_entry(uprobe_list.next, struct trace_uprobe, list);
  312. unregister_trace_uprobe(tu);
  313. }
  314. mutex_unlock(&uprobe_lock);
  315. }
  316. /* Probes listing interfaces */
  317. static void *probes_seq_start(struct seq_file *m, loff_t *pos)
  318. {
  319. mutex_lock(&uprobe_lock);
  320. return seq_list_start(&uprobe_list, *pos);
  321. }
  322. static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
  323. {
  324. return seq_list_next(v, &uprobe_list, pos);
  325. }
  326. static void probes_seq_stop(struct seq_file *m, void *v)
  327. {
  328. mutex_unlock(&uprobe_lock);
  329. }
  330. static int probes_seq_show(struct seq_file *m, void *v)
  331. {
  332. struct trace_uprobe *tu = v;
  333. int i;
  334. seq_printf(m, "p:%s/%s", tu->call.class->system, tu->call.name);
  335. seq_printf(m, " %s:0x%p", tu->filename, (void *)tu->offset);
  336. for (i = 0; i < tu->nr_args; i++)
  337. seq_printf(m, " %s=%s", tu->args[i].name, tu->args[i].comm);
  338. seq_printf(m, "\n");
  339. return 0;
  340. }
  341. static const struct seq_operations probes_seq_op = {
  342. .start = probes_seq_start,
  343. .next = probes_seq_next,
  344. .stop = probes_seq_stop,
  345. .show = probes_seq_show
  346. };
  347. static int probes_open(struct inode *inode, struct file *file)
  348. {
  349. if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC))
  350. cleanup_all_probes();
  351. return seq_open(file, &probes_seq_op);
  352. }
  353. static ssize_t probes_write(struct file *file, const char __user *buffer,
  354. size_t count, loff_t *ppos)
  355. {
  356. return traceprobe_probes_write(file, buffer, count, ppos, create_trace_uprobe);
  357. }
  358. static const struct file_operations uprobe_events_ops = {
  359. .owner = THIS_MODULE,
  360. .open = probes_open,
  361. .read = seq_read,
  362. .llseek = seq_lseek,
  363. .release = seq_release,
  364. .write = probes_write,
  365. };
  366. /* Probes profiling interfaces */
  367. static int probes_profile_seq_show(struct seq_file *m, void *v)
  368. {
  369. struct trace_uprobe *tu = v;
  370. seq_printf(m, " %s %-44s %15lu\n", tu->filename, tu->call.name, tu->nhit);
  371. return 0;
  372. }
  373. static const struct seq_operations profile_seq_op = {
  374. .start = probes_seq_start,
  375. .next = probes_seq_next,
  376. .stop = probes_seq_stop,
  377. .show = probes_profile_seq_show
  378. };
  379. static int profile_open(struct inode *inode, struct file *file)
  380. {
  381. return seq_open(file, &profile_seq_op);
  382. }
  383. static const struct file_operations uprobe_profile_ops = {
  384. .owner = THIS_MODULE,
  385. .open = profile_open,
  386. .read = seq_read,
  387. .llseek = seq_lseek,
  388. .release = seq_release,
  389. };
  390. /* uprobe handler */
  391. static void uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs)
  392. {
  393. struct uprobe_trace_entry_head *entry;
  394. struct ring_buffer_event *event;
  395. struct ring_buffer *buffer;
  396. u8 *data;
  397. int size, i, pc;
  398. unsigned long irq_flags;
  399. struct ftrace_event_call *call = &tu->call;
  400. tu->nhit++;
  401. local_save_flags(irq_flags);
  402. pc = preempt_count();
  403. size = sizeof(*entry) + tu->size;
  404. event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
  405. size, irq_flags, pc);
  406. if (!event)
  407. return;
  408. entry = ring_buffer_event_data(event);
  409. entry->ip = instruction_pointer(task_pt_regs(current));
  410. data = (u8 *)&entry[1];
  411. for (i = 0; i < tu->nr_args; i++)
  412. call_fetch(&tu->args[i].fetch, regs, data + tu->args[i].offset);
  413. if (!filter_current_check_discard(buffer, call, entry, event))
  414. trace_buffer_unlock_commit(buffer, event, irq_flags, pc);
  415. }
  416. /* Event entry printers */
  417. static enum print_line_t
  418. print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
  419. {
  420. struct uprobe_trace_entry_head *field;
  421. struct trace_seq *s = &iter->seq;
  422. struct trace_uprobe *tu;
  423. u8 *data;
  424. int i;
  425. field = (struct uprobe_trace_entry_head *)iter->ent;
  426. tu = container_of(event, struct trace_uprobe, call.event);
  427. if (!trace_seq_printf(s, "%s: (", tu->call.name))
  428. goto partial;
  429. if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
  430. goto partial;
  431. if (!trace_seq_puts(s, ")"))
  432. goto partial;
  433. data = (u8 *)&field[1];
  434. for (i = 0; i < tu->nr_args; i++) {
  435. if (!tu->args[i].type->print(s, tu->args[i].name,
  436. data + tu->args[i].offset, field))
  437. goto partial;
  438. }
  439. if (trace_seq_puts(s, "\n"))
  440. return TRACE_TYPE_HANDLED;
  441. partial:
  442. return TRACE_TYPE_PARTIAL_LINE;
  443. }
  444. static int probe_event_enable(struct trace_uprobe *tu, int flag)
  445. {
  446. struct uprobe_trace_consumer *utc;
  447. int ret = 0;
  448. if (!tu->inode || tu->consumer)
  449. return -EINTR;
  450. utc = kzalloc(sizeof(struct uprobe_trace_consumer), GFP_KERNEL);
  451. if (!utc)
  452. return -EINTR;
  453. utc->cons.handler = uprobe_dispatcher;
  454. utc->tu = tu;
  455. tu->consumer = utc;
  456. tu->flags |= flag;
  457. ret = uprobe_register(tu->inode, tu->offset, &utc->cons);
  458. if (ret) {
  459. tu->consumer = NULL;
  460. tu->flags &= ~flag;
  461. kfree(utc);
  462. }
  463. return ret;
  464. }
  465. static void probe_event_disable(struct trace_uprobe *tu, int flag)
  466. {
  467. if (!tu->inode || !tu->consumer)
  468. return;
  469. uprobe_unregister(tu->inode, tu->offset, &tu->consumer->cons);
  470. tu->flags &= ~flag;
  471. kfree(tu->consumer);
  472. tu->consumer = NULL;
  473. }
  474. static int uprobe_event_define_fields(struct ftrace_event_call *event_call)
  475. {
  476. int ret, i;
  477. struct uprobe_trace_entry_head field;
  478. struct trace_uprobe *tu = (struct trace_uprobe *)event_call->data;
  479. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  480. /* Set argument names as fields */
  481. for (i = 0; i < tu->nr_args; i++) {
  482. ret = trace_define_field(event_call, tu->args[i].type->fmttype,
  483. tu->args[i].name,
  484. sizeof(field) + tu->args[i].offset,
  485. tu->args[i].type->size,
  486. tu->args[i].type->is_signed,
  487. FILTER_OTHER);
  488. if (ret)
  489. return ret;
  490. }
  491. return 0;
  492. }
  493. #define LEN_OR_ZERO (len ? len - pos : 0)
  494. static int __set_print_fmt(struct trace_uprobe *tu, char *buf, int len)
  495. {
  496. const char *fmt, *arg;
  497. int i;
  498. int pos = 0;
  499. fmt = "(%lx)";
  500. arg = "REC->" FIELD_STRING_IP;
  501. /* When len=0, we just calculate the needed length */
  502. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
  503. for (i = 0; i < tu->nr_args; i++) {
  504. pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
  505. tu->args[i].name, tu->args[i].type->fmt);
  506. }
  507. pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
  508. for (i = 0; i < tu->nr_args; i++) {
  509. pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
  510. tu->args[i].name);
  511. }
  512. return pos; /* return the length of print_fmt */
  513. }
  514. #undef LEN_OR_ZERO
  515. static int set_print_fmt(struct trace_uprobe *tu)
  516. {
  517. char *print_fmt;
  518. int len;
  519. /* First: called with 0 length to calculate the needed length */
  520. len = __set_print_fmt(tu, NULL, 0);
  521. print_fmt = kmalloc(len + 1, GFP_KERNEL);
  522. if (!print_fmt)
  523. return -ENOMEM;
  524. /* Second: actually write the @print_fmt */
  525. __set_print_fmt(tu, print_fmt, len + 1);
  526. tu->call.print_fmt = print_fmt;
  527. return 0;
  528. }
  529. #ifdef CONFIG_PERF_EVENTS
  530. /* uprobe profile handler */
  531. static void uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs)
  532. {
  533. struct ftrace_event_call *call = &tu->call;
  534. struct uprobe_trace_entry_head *entry;
  535. struct hlist_head *head;
  536. u8 *data;
  537. int size, __size, i;
  538. int rctx;
  539. __size = sizeof(*entry) + tu->size;
  540. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  541. size -= sizeof(u32);
  542. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
  543. return;
  544. preempt_disable();
  545. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  546. if (!entry)
  547. goto out;
  548. entry->ip = instruction_pointer(task_pt_regs(current));
  549. data = (u8 *)&entry[1];
  550. for (i = 0; i < tu->nr_args; i++)
  551. call_fetch(&tu->args[i].fetch, regs, data + tu->args[i].offset);
  552. head = this_cpu_ptr(call->perf_events);
  553. perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, regs, head, NULL);
  554. out:
  555. preempt_enable();
  556. }
  557. #endif /* CONFIG_PERF_EVENTS */
  558. static
  559. int trace_uprobe_register(struct ftrace_event_call *event, enum trace_reg type, void *data)
  560. {
  561. struct trace_uprobe *tu = (struct trace_uprobe *)event->data;
  562. switch (type) {
  563. case TRACE_REG_REGISTER:
  564. return probe_event_enable(tu, TP_FLAG_TRACE);
  565. case TRACE_REG_UNREGISTER:
  566. probe_event_disable(tu, TP_FLAG_TRACE);
  567. return 0;
  568. #ifdef CONFIG_PERF_EVENTS
  569. case TRACE_REG_PERF_REGISTER:
  570. return probe_event_enable(tu, TP_FLAG_PROFILE);
  571. case TRACE_REG_PERF_UNREGISTER:
  572. probe_event_disable(tu, TP_FLAG_PROFILE);
  573. return 0;
  574. #endif
  575. default:
  576. return 0;
  577. }
  578. return 0;
  579. }
  580. static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
  581. {
  582. struct uprobe_trace_consumer *utc;
  583. struct trace_uprobe *tu;
  584. utc = container_of(con, struct uprobe_trace_consumer, cons);
  585. tu = utc->tu;
  586. if (!tu || tu->consumer != utc)
  587. return 0;
  588. if (tu->flags & TP_FLAG_TRACE)
  589. uprobe_trace_func(tu, regs);
  590. #ifdef CONFIG_PERF_EVENTS
  591. if (tu->flags & TP_FLAG_PROFILE)
  592. uprobe_perf_func(tu, regs);
  593. #endif
  594. return 0;
  595. }
  596. static struct trace_event_functions uprobe_funcs = {
  597. .trace = print_uprobe_event
  598. };
  599. static int register_uprobe_event(struct trace_uprobe *tu)
  600. {
  601. struct ftrace_event_call *call = &tu->call;
  602. int ret;
  603. /* Initialize ftrace_event_call */
  604. INIT_LIST_HEAD(&call->class->fields);
  605. call->event.funcs = &uprobe_funcs;
  606. call->class->define_fields = uprobe_event_define_fields;
  607. if (set_print_fmt(tu) < 0)
  608. return -ENOMEM;
  609. ret = register_ftrace_event(&call->event);
  610. if (!ret) {
  611. kfree(call->print_fmt);
  612. return -ENODEV;
  613. }
  614. call->flags = 0;
  615. call->class->reg = trace_uprobe_register;
  616. call->data = tu;
  617. ret = trace_add_event_call(call);
  618. if (ret) {
  619. pr_info("Failed to register uprobe event: %s\n", call->name);
  620. kfree(call->print_fmt);
  621. unregister_ftrace_event(&call->event);
  622. }
  623. return ret;
  624. }
  625. static void unregister_uprobe_event(struct trace_uprobe *tu)
  626. {
  627. /* tu->event is unregistered in trace_remove_event_call() */
  628. trace_remove_event_call(&tu->call);
  629. kfree(tu->call.print_fmt);
  630. tu->call.print_fmt = NULL;
  631. }
  632. /* Make a trace interface for controling probe points */
  633. static __init int init_uprobe_trace(void)
  634. {
  635. struct dentry *d_tracer;
  636. d_tracer = tracing_init_dentry();
  637. if (!d_tracer)
  638. return 0;
  639. trace_create_file("uprobe_events", 0644, d_tracer,
  640. NULL, &uprobe_events_ops);
  641. /* Profile interface */
  642. trace_create_file("uprobe_profile", 0444, d_tracer,
  643. NULL, &uprobe_profile_ops);
  644. return 0;
  645. }
  646. fs_initcall(init_uprobe_trace);