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