trace_uprobe.c 23 KB

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