trace_uprobe.c 23 KB

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