trace_uprobe.c 23 KB

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