uprobes/tracing: Introduce uprobe_{trace,perf}_print() helpers
[deliverable/linux.git] / kernel / trace / trace_uprobe.c
CommitLineData
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
31struct 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
43struct 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
52struct 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
72static int register_uprobe_event(struct trace_uprobe *tu);
73static void unregister_uprobe_event(struct trace_uprobe *tu);
74
75static DEFINE_MUTEX(uprobe_lock);
76static LIST_HEAD(uprobe_list);
77
78static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs);
79
736288ba
ON
80static 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
87static 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 */
95static struct trace_uprobe *
96alloc_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
124error:
125 kfree(tu->call.name);
126 kfree(tu);
127
128 return ERR_PTR(-ENOMEM);
129}
130
131static 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
145static 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 */
158static 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 */
166static 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
187end:
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 */
199static 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
384error:
385 free_trace_uprobe(tu);
386 return ret;
387
388fail_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
397static 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 */
410static 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
416static 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
421static void probes_seq_stop(struct seq_file *m, void *v)
422{
423 mutex_unlock(&uprobe_lock);
424}
425
426static 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
441static 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
448static 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
456static 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
462static 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 */
472static 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
480static 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
487static int profile_open(struct inode *inode, struct file *file)
488{
489 return seq_open(file, &profile_seq_op);
490}
491
492static 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
a51cc604
ON
500static void uprobe_trace_print(struct trace_uprobe *tu,
501 unsigned long func, 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)
a51cc604 514 return;
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);
a51cc604 524}
f42d24a1 525
a51cc604
ON
526/* uprobe handler */
527static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs)
528{
529 uprobe_trace_print(tu, 0, regs);
f42d24a1 530 return 0;
f3f096cf
SD
531}
532
533/* Event entry printers */
534static enum print_line_t
535print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
536{
457d1772 537 struct uprobe_trace_entry_head *entry;
f3f096cf
SD
538 struct trace_seq *s = &iter->seq;
539 struct trace_uprobe *tu;
540 u8 *data;
541 int i;
542
457d1772 543 entry = (struct uprobe_trace_entry_head *)iter->ent;
f3f096cf
SD
544 tu = container_of(event, struct trace_uprobe, call.event);
545
457d1772 546 if (!trace_seq_printf(s, "%s: (0x%lx)", tu->call.name, entry->vaddr[0]))
f3f096cf
SD
547 goto partial;
548
457d1772 549 data = DATAOF_TRACE_ENTRY(entry, false);
f3f096cf
SD
550 for (i = 0; i < tu->nr_args; i++) {
551 if (!tu->args[i].type->print(s, tu->args[i].name,
457d1772 552 data + tu->args[i].offset, entry))
f3f096cf
SD
553 goto partial;
554 }
555
556 if (trace_seq_puts(s, "\n"))
557 return TRACE_TYPE_HANDLED;
558
559partial:
560 return TRACE_TYPE_PARTIAL_LINE;
561}
562
b64b0077
ON
563static inline bool is_trace_uprobe_enabled(struct trace_uprobe *tu)
564{
565 return tu->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE);
566}
567
31ba3348
ON
568typedef bool (*filter_func_t)(struct uprobe_consumer *self,
569 enum uprobe_filter_ctx ctx,
570 struct mm_struct *mm);
571
572static int
573probe_event_enable(struct trace_uprobe *tu, int flag, filter_func_t filter)
f3f096cf 574{
f3f096cf
SD
575 int ret = 0;
576
b64b0077 577 if (is_trace_uprobe_enabled(tu))
f3f096cf
SD
578 return -EINTR;
579
736288ba
ON
580 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
581
4161824f 582 tu->flags |= flag;
31ba3348 583 tu->consumer.filter = filter;
a932b738
ON
584 ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
585 if (ret)
4161824f 586 tu->flags &= ~flag;
f3f096cf 587
4161824f 588 return ret;
f3f096cf
SD
589}
590
591static void probe_event_disable(struct trace_uprobe *tu, int flag)
592{
b64b0077 593 if (!is_trace_uprobe_enabled(tu))
f3f096cf
SD
594 return;
595
736288ba
ON
596 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
597
a932b738 598 uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
f3f096cf 599 tu->flags &= ~flag;
f3f096cf
SD
600}
601
602static int uprobe_event_define_fields(struct ftrace_event_call *event_call)
603{
457d1772 604 int ret, i, size;
f3f096cf 605 struct uprobe_trace_entry_head field;
457d1772 606 struct trace_uprobe *tu = event_call->data;
f3f096cf 607
457d1772
ON
608 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
609 size = SIZEOF_TRACE_ENTRY(false);
f3f096cf
SD
610 /* Set argument names as fields */
611 for (i = 0; i < tu->nr_args; i++) {
612 ret = trace_define_field(event_call, tu->args[i].type->fmttype,
613 tu->args[i].name,
457d1772 614 size + tu->args[i].offset,
f3f096cf
SD
615 tu->args[i].type->size,
616 tu->args[i].type->is_signed,
617 FILTER_OTHER);
618
619 if (ret)
620 return ret;
621 }
622 return 0;
623}
624
625#define LEN_OR_ZERO (len ? len - pos : 0)
626static int __set_print_fmt(struct trace_uprobe *tu, char *buf, int len)
627{
628 const char *fmt, *arg;
629 int i;
630 int pos = 0;
631
632 fmt = "(%lx)";
633 arg = "REC->" FIELD_STRING_IP;
634
635 /* When len=0, we just calculate the needed length */
636
637 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
638
639 for (i = 0; i < tu->nr_args; i++) {
640 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
641 tu->args[i].name, tu->args[i].type->fmt);
642 }
643
644 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
645
646 for (i = 0; i < tu->nr_args; i++) {
647 pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
648 tu->args[i].name);
649 }
650
651 return pos; /* return the length of print_fmt */
652}
653#undef LEN_OR_ZERO
654
655static int set_print_fmt(struct trace_uprobe *tu)
656{
657 char *print_fmt;
658 int len;
659
660 /* First: called with 0 length to calculate the needed length */
661 len = __set_print_fmt(tu, NULL, 0);
662 print_fmt = kmalloc(len + 1, GFP_KERNEL);
663 if (!print_fmt)
664 return -ENOMEM;
665
666 /* Second: actually write the @print_fmt */
667 __set_print_fmt(tu, print_fmt, len + 1);
668 tu->call.print_fmt = print_fmt;
669
670 return 0;
671}
672
673#ifdef CONFIG_PERF_EVENTS
31ba3348
ON
674static bool
675__uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
676{
677 struct perf_event *event;
678
679 if (filter->nr_systemwide)
680 return true;
681
682 list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
683 if (event->hw.tp_target->mm == mm)
684 return true;
685 }
686
687 return false;
688}
689
b2fe8ba6
ON
690static inline bool
691uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
692{
693 return __uprobe_perf_filter(&tu->filter, event->hw.tp_target->mm);
694}
695
736288ba
ON
696static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
697{
b2fe8ba6
ON
698 bool done;
699
736288ba 700 write_lock(&tu->filter.rwlock);
b2fe8ba6
ON
701 if (event->hw.tp_target) {
702 /*
703 * event->parent != NULL means copy_process(), we can avoid
704 * uprobe_apply(). current->mm must be probed and we can rely
705 * on dup_mmap() which preserves the already installed bp's.
706 *
707 * attr.enable_on_exec means that exec/mmap will install the
708 * breakpoints we need.
709 */
710 done = tu->filter.nr_systemwide ||
711 event->parent || event->attr.enable_on_exec ||
712 uprobe_filter_event(tu, event);
736288ba 713 list_add(&event->hw.tp_list, &tu->filter.perf_events);
b2fe8ba6
ON
714 } else {
715 done = tu->filter.nr_systemwide;
736288ba 716 tu->filter.nr_systemwide++;
b2fe8ba6 717 }
736288ba
ON
718 write_unlock(&tu->filter.rwlock);
719
b2fe8ba6
ON
720 if (!done)
721 uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
31ba3348 722
736288ba
ON
723 return 0;
724}
725
726static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
727{
b2fe8ba6
ON
728 bool done;
729
736288ba 730 write_lock(&tu->filter.rwlock);
b2fe8ba6 731 if (event->hw.tp_target) {
736288ba 732 list_del(&event->hw.tp_list);
b2fe8ba6
ON
733 done = tu->filter.nr_systemwide ||
734 (event->hw.tp_target->flags & PF_EXITING) ||
735 uprobe_filter_event(tu, event);
736 } else {
736288ba 737 tu->filter.nr_systemwide--;
b2fe8ba6
ON
738 done = tu->filter.nr_systemwide;
739 }
736288ba
ON
740 write_unlock(&tu->filter.rwlock);
741
b2fe8ba6
ON
742 if (!done)
743 uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
31ba3348 744
736288ba
ON
745 return 0;
746}
747
31ba3348
ON
748static bool uprobe_perf_filter(struct uprobe_consumer *uc,
749 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
750{
751 struct trace_uprobe *tu;
752 int ret;
753
754 tu = container_of(uc, struct trace_uprobe, consumer);
755 read_lock(&tu->filter.rwlock);
756 ret = __uprobe_perf_filter(&tu->filter, mm);
757 read_unlock(&tu->filter.rwlock);
758
759 return ret;
760}
761
a51cc604
ON
762static void uprobe_perf_print(struct trace_uprobe *tu,
763 unsigned long func, struct pt_regs *regs)
f3f096cf
SD
764{
765 struct ftrace_event_call *call = &tu->call;
766 struct uprobe_trace_entry_head *entry;
767 struct hlist_head *head;
457d1772
ON
768 unsigned long ip;
769 void *data;
770 int size, rctx, i;
f3f096cf 771
457d1772
ON
772 size = SIZEOF_TRACE_ENTRY(false);
773 size = ALIGN(size + tu->size + sizeof(u32), sizeof(u64)) - sizeof(u32);
f3f096cf 774 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
a51cc604 775 return;
f3f096cf
SD
776
777 preempt_disable();
f3f096cf
SD
778 entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
779 if (!entry)
780 goto out;
781
457d1772
ON
782 ip = instruction_pointer(regs);
783 entry->vaddr[0] = ip;
784 data = DATAOF_TRACE_ENTRY(entry, false);
f3f096cf
SD
785 for (i = 0; i < tu->nr_args; i++)
786 call_fetch(&tu->args[i].fetch, regs, data + tu->args[i].offset);
787
788 head = this_cpu_ptr(call->perf_events);
457d1772 789 perf_trace_buf_submit(entry, size, rctx, ip, 1, regs, head, NULL);
f3f096cf
SD
790 out:
791 preempt_enable();
a51cc604
ON
792}
793
794/* uprobe profile handler */
795static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs)
796{
797 if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
798 return UPROBE_HANDLER_REMOVE;
799
800 uprobe_perf_print(tu, 0, regs);
f42d24a1 801 return 0;
f3f096cf
SD
802}
803#endif /* CONFIG_PERF_EVENTS */
804
805static
806int trace_uprobe_register(struct ftrace_event_call *event, enum trace_reg type, void *data)
807{
457d1772 808 struct trace_uprobe *tu = event->data;
f3f096cf
SD
809
810 switch (type) {
811 case TRACE_REG_REGISTER:
31ba3348 812 return probe_event_enable(tu, TP_FLAG_TRACE, NULL);
f3f096cf
SD
813
814 case TRACE_REG_UNREGISTER:
815 probe_event_disable(tu, TP_FLAG_TRACE);
816 return 0;
817
818#ifdef CONFIG_PERF_EVENTS
819 case TRACE_REG_PERF_REGISTER:
31ba3348 820 return probe_event_enable(tu, TP_FLAG_PROFILE, uprobe_perf_filter);
f3f096cf
SD
821
822 case TRACE_REG_PERF_UNREGISTER:
823 probe_event_disable(tu, TP_FLAG_PROFILE);
824 return 0;
736288ba
ON
825
826 case TRACE_REG_PERF_OPEN:
827 return uprobe_perf_open(tu, data);
828
829 case TRACE_REG_PERF_CLOSE:
830 return uprobe_perf_close(tu, data);
831
f3f096cf
SD
832#endif
833 default:
834 return 0;
835 }
836 return 0;
837}
838
839static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
840{
f3f096cf 841 struct trace_uprobe *tu;
f42d24a1 842 int ret = 0;
f3f096cf 843
a932b738 844 tu = container_of(con, struct trace_uprobe, consumer);
1b47aefd 845 tu->nhit++;
f3f096cf
SD
846
847 if (tu->flags & TP_FLAG_TRACE)
f42d24a1 848 ret |= uprobe_trace_func(tu, regs);
f3f096cf
SD
849
850#ifdef CONFIG_PERF_EVENTS
851 if (tu->flags & TP_FLAG_PROFILE)
f42d24a1 852 ret |= uprobe_perf_func(tu, regs);
f3f096cf 853#endif
f42d24a1 854 return ret;
f3f096cf
SD
855}
856
857static struct trace_event_functions uprobe_funcs = {
858 .trace = print_uprobe_event
859};
860
861static int register_uprobe_event(struct trace_uprobe *tu)
862{
863 struct ftrace_event_call *call = &tu->call;
864 int ret;
865
866 /* Initialize ftrace_event_call */
867 INIT_LIST_HEAD(&call->class->fields);
868 call->event.funcs = &uprobe_funcs;
869 call->class->define_fields = uprobe_event_define_fields;
870
871 if (set_print_fmt(tu) < 0)
872 return -ENOMEM;
873
874 ret = register_ftrace_event(&call->event);
875 if (!ret) {
876 kfree(call->print_fmt);
877 return -ENODEV;
878 }
879 call->flags = 0;
880 call->class->reg = trace_uprobe_register;
881 call->data = tu;
882 ret = trace_add_event_call(call);
883
884 if (ret) {
885 pr_info("Failed to register uprobe event: %s\n", call->name);
886 kfree(call->print_fmt);
887 unregister_ftrace_event(&call->event);
888 }
889
890 return ret;
891}
892
893static void unregister_uprobe_event(struct trace_uprobe *tu)
894{
895 /* tu->event is unregistered in trace_remove_event_call() */
896 trace_remove_event_call(&tu->call);
897 kfree(tu->call.print_fmt);
898 tu->call.print_fmt = NULL;
899}
900
901/* Make a trace interface for controling probe points */
902static __init int init_uprobe_trace(void)
903{
904 struct dentry *d_tracer;
905
906 d_tracer = tracing_init_dentry();
907 if (!d_tracer)
908 return 0;
909
910 trace_create_file("uprobe_events", 0644, d_tracer,
911 NULL, &uprobe_events_ops);
912 /* Profile interface */
913 trace_create_file("uprobe_profile", 0444, d_tracer,
914 NULL, &uprobe_profile_ops);
915 return 0;
916}
917
918fs_initcall(init_uprobe_trace);
This page took 0.123836 seconds and 5 git commands to generate.