trace_uprobe.c 23 KB

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