trace_uprobe.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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(regs);
  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: (0x%lx)", tu->call.name, field->ip))
  441. goto partial;
  442. data = (u8 *)&field[1];
  443. for (i = 0; i < tu->nr_args; i++) {
  444. if (!tu->args[i].type->print(s, tu->args[i].name,
  445. data + tu->args[i].offset, field))
  446. goto partial;
  447. }
  448. if (trace_seq_puts(s, "\n"))
  449. return TRACE_TYPE_HANDLED;
  450. partial:
  451. return TRACE_TYPE_PARTIAL_LINE;
  452. }
  453. static inline bool is_trace_uprobe_enabled(struct trace_uprobe *tu)
  454. {
  455. return tu->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE);
  456. }
  457. typedef bool (*filter_func_t)(struct uprobe_consumer *self,
  458. enum uprobe_filter_ctx ctx,
  459. struct mm_struct *mm);
  460. static int
  461. probe_event_enable(struct trace_uprobe *tu, int flag, filter_func_t filter)
  462. {
  463. int ret = 0;
  464. if (is_trace_uprobe_enabled(tu))
  465. return -EINTR;
  466. WARN_ON(!uprobe_filter_is_empty(&tu->filter));
  467. tu->flags |= flag;
  468. tu->consumer.filter = filter;
  469. ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
  470. if (ret)
  471. tu->flags &= ~flag;
  472. return ret;
  473. }
  474. static void probe_event_disable(struct trace_uprobe *tu, int flag)
  475. {
  476. if (!is_trace_uprobe_enabled(tu))
  477. return;
  478. WARN_ON(!uprobe_filter_is_empty(&tu->filter));
  479. uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
  480. tu->flags &= ~flag;
  481. }
  482. static int uprobe_event_define_fields(struct ftrace_event_call *event_call)
  483. {
  484. int ret, i;
  485. struct uprobe_trace_entry_head field;
  486. struct trace_uprobe *tu = (struct trace_uprobe *)event_call->data;
  487. DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
  488. /* Set argument names as fields */
  489. for (i = 0; i < tu->nr_args; i++) {
  490. ret = trace_define_field(event_call, tu->args[i].type->fmttype,
  491. tu->args[i].name,
  492. sizeof(field) + tu->args[i].offset,
  493. tu->args[i].type->size,
  494. tu->args[i].type->is_signed,
  495. FILTER_OTHER);
  496. if (ret)
  497. return ret;
  498. }
  499. return 0;
  500. }
  501. #define LEN_OR_ZERO (len ? len - pos : 0)
  502. static int __set_print_fmt(struct trace_uprobe *tu, char *buf, int len)
  503. {
  504. const char *fmt, *arg;
  505. int i;
  506. int pos = 0;
  507. fmt = "(%lx)";
  508. arg = "REC->" FIELD_STRING_IP;
  509. /* When len=0, we just calculate the needed length */
  510. pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
  511. for (i = 0; i < tu->nr_args; i++) {
  512. pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
  513. tu->args[i].name, tu->args[i].type->fmt);
  514. }
  515. pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
  516. for (i = 0; i < tu->nr_args; i++) {
  517. pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
  518. tu->args[i].name);
  519. }
  520. return pos; /* return the length of print_fmt */
  521. }
  522. #undef LEN_OR_ZERO
  523. static int set_print_fmt(struct trace_uprobe *tu)
  524. {
  525. char *print_fmt;
  526. int len;
  527. /* First: called with 0 length to calculate the needed length */
  528. len = __set_print_fmt(tu, NULL, 0);
  529. print_fmt = kmalloc(len + 1, GFP_KERNEL);
  530. if (!print_fmt)
  531. return -ENOMEM;
  532. /* Second: actually write the @print_fmt */
  533. __set_print_fmt(tu, print_fmt, len + 1);
  534. tu->call.print_fmt = print_fmt;
  535. return 0;
  536. }
  537. #ifdef CONFIG_PERF_EVENTS
  538. static bool
  539. __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
  540. {
  541. struct perf_event *event;
  542. if (filter->nr_systemwide)
  543. return true;
  544. list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
  545. if (event->hw.tp_target->mm == mm)
  546. return true;
  547. }
  548. return false;
  549. }
  550. static inline bool
  551. uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
  552. {
  553. return __uprobe_perf_filter(&tu->filter, event->hw.tp_target->mm);
  554. }
  555. static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
  556. {
  557. bool done;
  558. write_lock(&tu->filter.rwlock);
  559. if (event->hw.tp_target) {
  560. /*
  561. * event->parent != NULL means copy_process(), we can avoid
  562. * uprobe_apply(). current->mm must be probed and we can rely
  563. * on dup_mmap() which preserves the already installed bp's.
  564. *
  565. * attr.enable_on_exec means that exec/mmap will install the
  566. * breakpoints we need.
  567. */
  568. done = tu->filter.nr_systemwide ||
  569. event->parent || event->attr.enable_on_exec ||
  570. uprobe_filter_event(tu, event);
  571. list_add(&event->hw.tp_list, &tu->filter.perf_events);
  572. } else {
  573. done = tu->filter.nr_systemwide;
  574. tu->filter.nr_systemwide++;
  575. }
  576. write_unlock(&tu->filter.rwlock);
  577. if (!done)
  578. uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
  579. return 0;
  580. }
  581. static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
  582. {
  583. bool done;
  584. write_lock(&tu->filter.rwlock);
  585. if (event->hw.tp_target) {
  586. list_del(&event->hw.tp_list);
  587. done = tu->filter.nr_systemwide ||
  588. (event->hw.tp_target->flags & PF_EXITING) ||
  589. uprobe_filter_event(tu, event);
  590. } else {
  591. tu->filter.nr_systemwide--;
  592. done = tu->filter.nr_systemwide;
  593. }
  594. write_unlock(&tu->filter.rwlock);
  595. if (!done)
  596. uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
  597. return 0;
  598. }
  599. static bool uprobe_perf_filter(struct uprobe_consumer *uc,
  600. enum uprobe_filter_ctx ctx, struct mm_struct *mm)
  601. {
  602. struct trace_uprobe *tu;
  603. int ret;
  604. tu = container_of(uc, struct trace_uprobe, consumer);
  605. read_lock(&tu->filter.rwlock);
  606. ret = __uprobe_perf_filter(&tu->filter, mm);
  607. read_unlock(&tu->filter.rwlock);
  608. return ret;
  609. }
  610. /* uprobe profile handler */
  611. static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs)
  612. {
  613. struct ftrace_event_call *call = &tu->call;
  614. struct uprobe_trace_entry_head *entry;
  615. struct hlist_head *head;
  616. u8 *data;
  617. int size, __size, i;
  618. int rctx;
  619. if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
  620. return UPROBE_HANDLER_REMOVE;
  621. __size = sizeof(*entry) + tu->size;
  622. size = ALIGN(__size + sizeof(u32), sizeof(u64));
  623. size -= sizeof(u32);
  624. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
  625. return 0;
  626. preempt_disable();
  627. entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
  628. if (!entry)
  629. goto out;
  630. entry->ip = instruction_pointer(regs);
  631. data = (u8 *)&entry[1];
  632. for (i = 0; i < tu->nr_args; i++)
  633. call_fetch(&tu->args[i].fetch, regs, data + tu->args[i].offset);
  634. head = this_cpu_ptr(call->perf_events);
  635. perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, regs, head, NULL);
  636. out:
  637. preempt_enable();
  638. return 0;
  639. }
  640. #endif /* CONFIG_PERF_EVENTS */
  641. static
  642. int trace_uprobe_register(struct ftrace_event_call *event, enum trace_reg type, void *data)
  643. {
  644. struct trace_uprobe *tu = (struct trace_uprobe *)event->data;
  645. switch (type) {
  646. case TRACE_REG_REGISTER:
  647. return probe_event_enable(tu, TP_FLAG_TRACE, NULL);
  648. case TRACE_REG_UNREGISTER:
  649. probe_event_disable(tu, TP_FLAG_TRACE);
  650. return 0;
  651. #ifdef CONFIG_PERF_EVENTS
  652. case TRACE_REG_PERF_REGISTER:
  653. return probe_event_enable(tu, TP_FLAG_PROFILE, uprobe_perf_filter);
  654. case TRACE_REG_PERF_UNREGISTER:
  655. probe_event_disable(tu, TP_FLAG_PROFILE);
  656. return 0;
  657. case TRACE_REG_PERF_OPEN:
  658. return uprobe_perf_open(tu, data);
  659. case TRACE_REG_PERF_CLOSE:
  660. return uprobe_perf_close(tu, data);
  661. #endif
  662. default:
  663. return 0;
  664. }
  665. return 0;
  666. }
  667. static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
  668. {
  669. struct trace_uprobe *tu;
  670. int ret = 0;
  671. tu = container_of(con, struct trace_uprobe, consumer);
  672. tu->nhit++;
  673. if (tu->flags & TP_FLAG_TRACE)
  674. ret |= uprobe_trace_func(tu, regs);
  675. #ifdef CONFIG_PERF_EVENTS
  676. if (tu->flags & TP_FLAG_PROFILE)
  677. ret |= uprobe_perf_func(tu, regs);
  678. #endif
  679. return ret;
  680. }
  681. static struct trace_event_functions uprobe_funcs = {
  682. .trace = print_uprobe_event
  683. };
  684. static int register_uprobe_event(struct trace_uprobe *tu)
  685. {
  686. struct ftrace_event_call *call = &tu->call;
  687. int ret;
  688. /* Initialize ftrace_event_call */
  689. INIT_LIST_HEAD(&call->class->fields);
  690. call->event.funcs = &uprobe_funcs;
  691. call->class->define_fields = uprobe_event_define_fields;
  692. if (set_print_fmt(tu) < 0)
  693. return -ENOMEM;
  694. ret = register_ftrace_event(&call->event);
  695. if (!ret) {
  696. kfree(call->print_fmt);
  697. return -ENODEV;
  698. }
  699. call->flags = 0;
  700. call->class->reg = trace_uprobe_register;
  701. call->data = tu;
  702. ret = trace_add_event_call(call);
  703. if (ret) {
  704. pr_info("Failed to register uprobe event: %s\n", call->name);
  705. kfree(call->print_fmt);
  706. unregister_ftrace_event(&call->event);
  707. }
  708. return ret;
  709. }
  710. static void unregister_uprobe_event(struct trace_uprobe *tu)
  711. {
  712. /* tu->event is unregistered in trace_remove_event_call() */
  713. trace_remove_event_call(&tu->call);
  714. kfree(tu->call.print_fmt);
  715. tu->call.print_fmt = NULL;
  716. }
  717. /* Make a trace interface for controling probe points */
  718. static __init int init_uprobe_trace(void)
  719. {
  720. struct dentry *d_tracer;
  721. d_tracer = tracing_init_dentry();
  722. if (!d_tracer)
  723. return 0;
  724. trace_create_file("uprobe_events", 0644, d_tracer,
  725. NULL, &uprobe_events_ops);
  726. /* Profile interface */
  727. trace_create_file("uprobe_profile", 0444, d_tracer,
  728. NULL, &uprobe_profile_ops);
  729. return 0;
  730. }
  731. fs_initcall(init_uprobe_trace);