trace_uprobe.c 23 KB

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