1 // SPDX-License-Identifier: GPL-2.0
5 * Part of this code was copied from kernel/trace/trace_kprobe.c written by
6 * Masami Hiramatsu <mhiramat@kernel.org>
8 * Copyright (C) 2021, VMware Inc, Steven Rostedt <rostedt@goodmis.org>
9 * Copyright (C) 2021, VMware Inc, Tzvetomir Stoyanov tz.stoyanov@gmail.com>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/ftrace.h>
16 #include "trace_dynevent.h"
17 #include "trace_probe.h"
18 #include "trace_probe_tmpl.h"
19 #include "trace_probe_kernel.h"
21 #define EPROBE_EVENT_SYSTEM "eprobes"
24 /* tracepoint system */
25 const char *event_system;
27 /* tracepoint event */
28 const char *event_name;
30 /* filter string for the tracepoint */
33 struct trace_event_call *event;
35 struct dyn_event devent;
36 struct trace_probe tp;
40 struct trace_event_file *file;
41 struct trace_eprobe *ep;
45 #define for_each_trace_eprobe_tp(ep, _tp) \
46 list_for_each_entry(ep, trace_probe_probe_list(_tp), tp.list)
48 static int __trace_eprobe_create(int argc, const char *argv[]);
50 static void trace_event_probe_cleanup(struct trace_eprobe *ep)
54 trace_probe_cleanup(&ep->tp);
55 kfree(ep->event_name);
56 kfree(ep->event_system);
58 trace_event_put_ref(ep->event);
59 kfree(ep->filter_str);
63 static struct trace_eprobe *to_trace_eprobe(struct dyn_event *ev)
65 return container_of(ev, struct trace_eprobe, devent);
68 static int eprobe_dyn_event_create(const char *raw_command)
70 return trace_probe_create(raw_command, __trace_eprobe_create);
73 static int eprobe_dyn_event_show(struct seq_file *m, struct dyn_event *ev)
75 struct trace_eprobe *ep = to_trace_eprobe(ev);
78 seq_printf(m, "e:%s/%s", trace_probe_group_name(&ep->tp),
79 trace_probe_name(&ep->tp));
80 seq_printf(m, " %s.%s", ep->event_system, ep->event_name);
82 for (i = 0; i < ep->tp.nr_args; i++)
83 seq_printf(m, " %s=%s", ep->tp.args[i].name, ep->tp.args[i].comm);
89 static int unregister_trace_eprobe(struct trace_eprobe *ep)
91 /* If other probes are on the event, just unregister eprobe */
92 if (trace_probe_has_sibling(&ep->tp))
95 /* Enabled event can not be unregistered */
96 if (trace_probe_is_enabled(&ep->tp))
99 /* Will fail if probe is being used by ftrace or perf */
100 if (trace_probe_unregister_event_call(&ep->tp))
104 dyn_event_remove(&ep->devent);
105 trace_probe_unlink(&ep->tp);
110 static int eprobe_dyn_event_release(struct dyn_event *ev)
112 struct trace_eprobe *ep = to_trace_eprobe(ev);
113 int ret = unregister_trace_eprobe(ep);
116 trace_event_probe_cleanup(ep);
120 static bool eprobe_dyn_event_is_busy(struct dyn_event *ev)
122 struct trace_eprobe *ep = to_trace_eprobe(ev);
124 return trace_probe_is_enabled(&ep->tp);
127 static bool eprobe_dyn_event_match(const char *system, const char *event,
128 int argc, const char **argv, struct dyn_event *ev)
130 struct trace_eprobe *ep = to_trace_eprobe(ev);
134 * We match the following:
135 * event only - match all eprobes with event name
136 * system and event only - match all system/event probes
137 * system only - match all system probes
139 * The below has the above satisfied with more arguments:
141 * attached system/event - If the arg has the system and event
142 * the probe is attached to, match
143 * probes with the attachment.
145 * If any more args are given, then it requires a full match.
149 * If system exists, but this probe is not part of that system
152 if (system && strcmp(trace_probe_group_name(&ep->tp), system) != 0)
155 /* Must match the event name */
156 if (event[0] != '\0' && strcmp(trace_probe_name(&ep->tp), event) != 0)
159 /* No arguments match all */
163 /* First argument is the system/event the probe is attached to */
165 slash = strchr(argv[0], '/');
167 slash = strchr(argv[0], '.');
171 if (strncmp(ep->event_system, argv[0], slash - argv[0]))
173 if (strcmp(ep->event_name, slash + 1))
179 /* If there are no other args, then match */
183 return trace_probe_match_command_args(&ep->tp, argc, argv);
186 static struct dyn_event_operations eprobe_dyn_event_ops = {
187 .create = eprobe_dyn_event_create,
188 .show = eprobe_dyn_event_show,
189 .is_busy = eprobe_dyn_event_is_busy,
190 .free = eprobe_dyn_event_release,
191 .match = eprobe_dyn_event_match,
194 static struct trace_eprobe *alloc_event_probe(const char *group,
195 const char *this_event,
196 struct trace_event_call *event,
199 struct trace_eprobe *ep;
200 const char *event_name;
201 const char *sys_name;
205 return ERR_PTR(-ENODEV);
207 sys_name = event->class->system;
208 event_name = trace_event_name(event);
210 ep = kzalloc(struct_size(ep, tp.args, nargs), GFP_KERNEL);
212 trace_event_put_ref(event);
216 ep->event_name = kstrdup(event_name, GFP_KERNEL);
219 ep->event_system = kstrdup(sys_name, GFP_KERNEL);
220 if (!ep->event_system)
223 ret = trace_probe_init(&ep->tp, this_event, group, false);
227 dyn_event_init(&ep->devent, &eprobe_dyn_event_ops);
230 trace_event_probe_cleanup(ep);
234 static int eprobe_event_define_fields(struct trace_event_call *event_call)
236 struct eprobe_trace_entry_head field;
237 struct trace_probe *tp;
239 tp = trace_probe_primary_from_call(event_call);
240 if (WARN_ON_ONCE(!tp))
243 return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
246 static struct trace_event_fields eprobe_fields_array[] = {
247 { .type = TRACE_FUNCTION_TYPE,
248 .define_fields = eprobe_event_define_fields },
252 /* Event entry printers */
253 static enum print_line_t
254 print_eprobe_event(struct trace_iterator *iter, int flags,
255 struct trace_event *event)
257 struct eprobe_trace_entry_head *field;
258 struct trace_event_call *pevent;
259 struct trace_event *probed_event;
260 struct trace_seq *s = &iter->seq;
261 struct trace_eprobe *ep;
262 struct trace_probe *tp;
265 field = (struct eprobe_trace_entry_head *)iter->ent;
266 tp = trace_probe_primary_from_call(
267 container_of(event, struct trace_event_call, event));
268 if (WARN_ON_ONCE(!tp))
271 ep = container_of(tp, struct trace_eprobe, tp);
272 type = ep->event->event.type;
274 trace_seq_printf(s, "%s: (", trace_probe_name(tp));
276 probed_event = ftrace_find_event(type);
278 pevent = container_of(probed_event, struct trace_event_call, event);
279 trace_seq_printf(s, "%s.%s", pevent->class->system,
280 trace_event_name(pevent));
282 trace_seq_printf(s, "%u", type);
285 trace_seq_putc(s, ')');
287 if (trace_probe_print_args(s, tp->args, tp->nr_args,
288 (u8 *)&field[1], field) < 0)
291 trace_seq_putc(s, '\n');
293 return trace_handle_return(s);
296 static nokprobe_inline unsigned long
297 get_event_field(struct fetch_insn *code, void *rec)
299 struct ftrace_event_field *field = code->data;
303 addr = rec + field->offset;
305 if (is_string_field(field)) {
306 switch (field->filter_type) {
307 case FILTER_DYN_STRING:
308 val = (unsigned long)(rec + (*(unsigned int *)addr & 0xffff));
310 case FILTER_RDYN_STRING:
311 val = (unsigned long)(addr + (*(unsigned int *)addr & 0xffff));
313 case FILTER_STATIC_STRING:
314 val = (unsigned long)addr;
316 case FILTER_PTR_STRING:
317 val = (unsigned long)(*(char *)addr);
326 switch (field->size) {
328 if (field->is_signed)
331 val = *(unsigned char *)addr;
334 if (field->is_signed)
335 val = *(short *)addr;
337 val = *(unsigned short *)addr;
340 if (field->is_signed)
343 val = *(unsigned int *)addr;
346 if (field->is_signed)
349 val = *(unsigned long *)addr;
355 static int get_eprobe_size(struct trace_probe *tp, void *rec)
357 struct fetch_insn *code;
358 struct probe_arg *arg;
361 for (i = 0; i < tp->nr_args; i++) {
369 case FETCH_OP_TP_ARG:
370 val = get_event_field(code, rec);
372 case FETCH_NOP_SYMBOL: /* Ignore a place holder */
376 if (process_common_fetch_insn(code, &val) < 0)
380 len = process_fetch_insn_bottom(code, val, NULL, NULL);
389 /* Kprobe specific fetch functions */
391 /* Note that we don't verify it, since the code does not come from user space */
393 process_fetch_insn(struct fetch_insn *code, void *rec, void *dest,
401 case FETCH_OP_TP_ARG:
402 val = get_event_field(code, rec);
404 case FETCH_NOP_SYMBOL: /* Ignore a place holder */
408 ret = process_common_fetch_insn(code, &val);
413 return process_fetch_insn_bottom(code, val, dest, base);
415 NOKPROBE_SYMBOL(process_fetch_insn)
419 __eprobe_trace_func(struct eprobe_data *edata, void *rec)
421 struct eprobe_trace_entry_head *entry;
422 struct trace_event_call *call = trace_probe_event_call(&edata->ep->tp);
423 struct trace_event_buffer fbuffer;
426 if (WARN_ON_ONCE(call != edata->file->event_call))
429 if (trace_trigger_soft_disabled(edata->file))
432 dsize = get_eprobe_size(&edata->ep->tp, rec);
434 entry = trace_event_buffer_reserve(&fbuffer, edata->file,
435 sizeof(*entry) + edata->ep->tp.size + dsize);
440 entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event);
441 store_trace_args(&entry[1], &edata->ep->tp, rec, sizeof(*entry), dsize);
443 trace_event_buffer_commit(&fbuffer);
447 * The event probe implementation uses event triggers to get access to
448 * the event it is attached to, but is not an actual trigger. The below
449 * functions are just stubs to fulfill what is needed to use the trigger
452 static int eprobe_trigger_init(struct event_trigger_data *data)
457 static void eprobe_trigger_free(struct event_trigger_data *data)
462 static int eprobe_trigger_print(struct seq_file *m,
463 struct event_trigger_data *data)
465 /* Do not print eprobe event triggers */
469 static void eprobe_trigger_func(struct event_trigger_data *data,
470 struct trace_buffer *buffer, void *rec,
471 struct ring_buffer_event *rbe)
473 struct eprobe_data *edata = data->private_data;
478 __eprobe_trace_func(edata, rec);
481 static struct event_trigger_ops eprobe_trigger_ops = {
482 .trigger = eprobe_trigger_func,
483 .print = eprobe_trigger_print,
484 .init = eprobe_trigger_init,
485 .free = eprobe_trigger_free,
488 static int eprobe_trigger_cmd_parse(struct event_command *cmd_ops,
489 struct trace_event_file *file,
490 char *glob, char *cmd,
491 char *param_and_filter)
496 static int eprobe_trigger_reg_func(char *glob,
497 struct event_trigger_data *data,
498 struct trace_event_file *file)
503 static void eprobe_trigger_unreg_func(char *glob,
504 struct event_trigger_data *data,
505 struct trace_event_file *file)
510 static struct event_trigger_ops *eprobe_trigger_get_ops(char *cmd,
513 return &eprobe_trigger_ops;
516 static struct event_command event_trigger_cmd = {
518 .trigger_type = ETT_EVENT_EPROBE,
519 .flags = EVENT_CMD_FL_NEEDS_REC,
520 .parse = eprobe_trigger_cmd_parse,
521 .reg = eprobe_trigger_reg_func,
522 .unreg = eprobe_trigger_unreg_func,
524 .get_trigger_ops = eprobe_trigger_get_ops,
528 static struct event_trigger_data *
529 new_eprobe_trigger(struct trace_eprobe *ep, struct trace_event_file *file)
531 struct event_trigger_data *trigger;
532 struct event_filter *filter = NULL;
533 struct eprobe_data *edata;
536 edata = kzalloc(sizeof(*edata), GFP_KERNEL);
537 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
538 if (!trigger || !edata) {
543 trigger->flags = EVENT_TRIGGER_FL_PROBE;
545 trigger->ops = &eprobe_trigger_ops;
548 * EVENT PROBE triggers are not registered as commands with
549 * register_event_command(), as they are not controlled by the user
550 * from the trigger file
552 trigger->cmd_ops = &event_trigger_cmd;
554 INIT_LIST_HEAD(&trigger->list);
556 if (ep->filter_str) {
557 ret = create_event_filter(file->tr, ep->event,
558 ep->filter_str, false, &filter);
562 RCU_INIT_POINTER(trigger->filter, filter);
566 trigger->private_data = edata;
570 free_event_filter(filter);
576 static int enable_eprobe(struct trace_eprobe *ep,
577 struct trace_event_file *eprobe_file)
579 struct event_trigger_data *trigger;
580 struct trace_event_file *file;
581 struct trace_array *tr = eprobe_file->tr;
583 file = find_event_file(tr, ep->event_system, ep->event_name);
586 trigger = new_eprobe_trigger(ep, eprobe_file);
588 return PTR_ERR(trigger);
590 list_add_tail_rcu(&trigger->list, &file->triggers);
592 trace_event_trigger_enable_disable(file, 1);
593 update_cond_flag(file);
598 static struct trace_event_functions eprobe_funcs = {
599 .trace = print_eprobe_event
602 static int disable_eprobe(struct trace_eprobe *ep,
603 struct trace_array *tr)
605 struct event_trigger_data *trigger = NULL, *iter;
606 struct trace_event_file *file;
607 struct event_filter *filter;
608 struct eprobe_data *edata;
610 file = find_event_file(tr, ep->event_system, ep->event_name);
614 list_for_each_entry(iter, &file->triggers, list) {
615 if (!(iter->flags & EVENT_TRIGGER_FL_PROBE))
617 edata = iter->private_data;
618 if (edata->ep == ep) {
626 list_del_rcu(&trigger->list);
628 trace_event_trigger_enable_disable(file, 0);
629 update_cond_flag(file);
631 /* Make sure nothing is using the edata or trigger */
632 tracepoint_synchronize_unregister();
634 filter = rcu_access_pointer(trigger->filter);
637 free_event_filter(filter);
644 static int enable_trace_eprobe(struct trace_event_call *call,
645 struct trace_event_file *file)
647 struct trace_probe *tp;
648 struct trace_eprobe *ep;
653 tp = trace_probe_primary_from_call(call);
654 if (WARN_ON_ONCE(!tp))
656 enabled = trace_probe_is_enabled(tp);
658 /* This also changes "enabled" state */
660 ret = trace_probe_add_file(tp, file);
664 trace_probe_set_flag(tp, TP_FLAG_PROFILE);
669 for_each_trace_eprobe_tp(ep, tp) {
670 ret = enable_eprobe(ep, file);
678 /* Failed to enable one of them. Roll back all */
681 * It's a bug if one failed for something other than memory
682 * not being available but another eprobe succeeded.
684 WARN_ON_ONCE(ret != -ENOMEM);
686 for_each_trace_eprobe_tp(ep, tp) {
687 disable_eprobe(ep, file->tr);
693 trace_probe_remove_file(tp, file);
695 trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
701 static int disable_trace_eprobe(struct trace_event_call *call,
702 struct trace_event_file *file)
704 struct trace_probe *tp;
705 struct trace_eprobe *ep;
707 tp = trace_probe_primary_from_call(call);
708 if (WARN_ON_ONCE(!tp))
712 if (!trace_probe_get_file_link(tp, file))
714 if (!trace_probe_has_single_file(tp))
716 trace_probe_clear_flag(tp, TP_FLAG_TRACE);
718 trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
720 if (!trace_probe_is_enabled(tp)) {
721 for_each_trace_eprobe_tp(ep, tp)
722 disable_eprobe(ep, file->tr);
728 * Synchronization is done in below function. For perf event,
729 * file == NULL and perf_trace_event_unreg() calls
730 * tracepoint_synchronize_unregister() to ensure synchronize
731 * event. We don't need to care about it.
733 trace_probe_remove_file(tp, file);
738 static int eprobe_register(struct trace_event_call *event,
739 enum trace_reg type, void *data)
741 struct trace_event_file *file = data;
744 case TRACE_REG_REGISTER:
745 return enable_trace_eprobe(event, file);
746 case TRACE_REG_UNREGISTER:
747 return disable_trace_eprobe(event, file);
748 #ifdef CONFIG_PERF_EVENTS
749 case TRACE_REG_PERF_REGISTER:
750 case TRACE_REG_PERF_UNREGISTER:
751 case TRACE_REG_PERF_OPEN:
752 case TRACE_REG_PERF_CLOSE:
753 case TRACE_REG_PERF_ADD:
754 case TRACE_REG_PERF_DEL:
761 static inline void init_trace_eprobe_call(struct trace_eprobe *ep)
763 struct trace_event_call *call = trace_probe_event_call(&ep->tp);
765 call->flags = TRACE_EVENT_FL_EPROBE;
766 call->event.funcs = &eprobe_funcs;
767 call->class->fields_array = eprobe_fields_array;
768 call->class->reg = eprobe_register;
771 static struct trace_event_call *
772 find_and_get_event(const char *system, const char *event_name)
774 struct trace_event_call *tp_event;
777 list_for_each_entry(tp_event, &ftrace_events, list) {
778 /* Skip other probes and ftrace events */
779 if (tp_event->flags &
780 (TRACE_EVENT_FL_IGNORE_ENABLE |
781 TRACE_EVENT_FL_KPROBE |
782 TRACE_EVENT_FL_UPROBE |
783 TRACE_EVENT_FL_EPROBE))
785 if (!tp_event->class->system ||
786 strcmp(system, tp_event->class->system))
788 name = trace_event_name(tp_event);
789 if (!name || strcmp(event_name, name))
791 if (!trace_event_try_get_ref(tp_event)) {
801 static int trace_eprobe_tp_update_arg(struct trace_eprobe *ep, const char *argv[], int i)
803 struct traceprobe_parse_context ctx = {
805 .flags = TPARG_FL_KERNEL | TPARG_FL_TEVENT,
809 ret = traceprobe_parse_probe_arg(&ep->tp, i, argv[i], &ctx);
810 /* Handle symbols "@" */
812 ret = traceprobe_update_arg(&ep->tp.args[i]);
814 traceprobe_finish_parse(&ctx);
818 static int trace_eprobe_parse_filter(struct trace_eprobe *ep, int argc, const char *argv[])
820 struct event_filter *dummy = NULL;
825 trace_probe_log_err(0, NO_EP_FILTER);
829 /* Recover the filter string */
830 for (i = 0; i < argc; i++)
831 len += strlen(argv[i]) + 1;
833 ep->filter_str = kzalloc(len, GFP_KERNEL);
838 for (i = 0; i < argc; i++) {
840 ret = snprintf(p, len, " %s", argv[i]);
842 ret = snprintf(p, len, "%s", argv[i]);
848 * Ensure the filter string can be parsed correctly. Note, this
849 * filter string is for the original event, not for the eprobe.
851 ret = create_event_filter(top_trace_array(), ep->event, ep->filter_str,
853 free_event_filter(dummy);
859 kfree(ep->filter_str);
860 ep->filter_str = NULL;
864 static int __trace_eprobe_create(int argc, const char *argv[])
868 * e[:[GRP/][ENAME]] SYSTEM.EVENT [FETCHARGS] [if FILTER]
869 * Fetch args (no space):
870 * <name>=$<field>[:TYPE]
872 const char *event = NULL, *group = EPROBE_EVENT_SYSTEM;
873 const char *sys_event = NULL, *sys_name = NULL;
874 struct trace_event_call *event_call;
875 struct trace_eprobe *ep = NULL;
876 char buf1[MAX_EVENT_NAME_LEN];
877 char buf2[MAX_EVENT_NAME_LEN];
878 char gbuf[MAX_EVENT_NAME_LEN];
879 int ret = 0, filter_idx = 0;
882 if (argc < 2 || argv[0][0] != 'e')
885 trace_probe_log_init("event_probe", argc, argv);
887 event = strchr(&argv[0][1], ':');
890 ret = traceprobe_parse_event_name(&event, &group, gbuf,
896 trace_probe_log_set_index(1);
898 ret = traceprobe_parse_event_name(&sys_event, &sys_name, buf2, 0);
899 if (ret || !sys_event || !sys_name) {
900 trace_probe_log_err(0, NO_EVENT_INFO);
905 strscpy(buf1, sys_event, MAX_EVENT_NAME_LEN);
909 for (i = 2; i < argc; i++) {
910 if (!strcmp(argv[i], "if")) {
912 filter_cnt = argc - filter_idx;
918 mutex_lock(&event_mutex);
919 event_call = find_and_get_event(sys_name, sys_event);
920 ep = alloc_event_probe(group, event, event_call, argc - 2);
921 mutex_unlock(&event_mutex);
926 trace_probe_log_err(0, BAD_ATTACH_EVENT);
927 /* This must return -ENOMEM or missing event, else there is a bug */
928 WARN_ON_ONCE(ret != -ENOMEM && ret != -ENODEV);
934 trace_probe_log_set_index(filter_idx);
935 ret = trace_eprobe_parse_filter(ep, filter_cnt, argv + filter_idx);
939 ep->filter_str = NULL;
941 argc -= 2; argv += 2;
942 /* parse arguments */
943 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
944 trace_probe_log_set_index(i + 2);
945 ret = trace_eprobe_tp_update_arg(ep, argv, i);
949 ret = traceprobe_set_print_fmt(&ep->tp, PROBE_PRINT_EVENT);
952 init_trace_eprobe_call(ep);
953 mutex_lock(&event_mutex);
954 ret = trace_probe_register_event_call(&ep->tp);
956 if (ret == -EEXIST) {
957 trace_probe_log_set_index(0);
958 trace_probe_log_err(0, EVENT_EXIST);
960 mutex_unlock(&event_mutex);
963 ret = dyn_event_add(&ep->devent, &ep->tp.event->call);
964 mutex_unlock(&event_mutex);
969 trace_event_probe_cleanup(ep);
974 * Register dynevent at core_initcall. This allows kernel to setup eprobe
975 * events in postcore_initcall without tracefs.
977 static __init int trace_events_eprobe_init_early(void)
981 err = dyn_event_register(&eprobe_dyn_event_ops);
983 pr_warn("Could not register eprobe_dyn_event_ops\n");
987 core_initcall(trace_events_eprobe_init_early);