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