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