trace_uprobe.c 23 KB

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