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