trace_uprobe.c 21 KB

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