xref: /linux-6.15/kernel/trace/trace_eprobe.c (revision e41b5af4)
17491e2c4STzvetomir Stoyanov (VMware) // SPDX-License-Identifier: GPL-2.0
27491e2c4STzvetomir Stoyanov (VMware) /*
37491e2c4STzvetomir Stoyanov (VMware)  * event probes
47491e2c4STzvetomir Stoyanov (VMware)  *
57491e2c4STzvetomir Stoyanov (VMware)  * Part of this code was copied from kernel/trace/trace_kprobe.c written by
67491e2c4STzvetomir Stoyanov (VMware)  * Masami Hiramatsu <[email protected]>
77491e2c4STzvetomir Stoyanov (VMware)  *
87491e2c4STzvetomir Stoyanov (VMware)  * Copyright (C) 2021, VMware Inc, Steven Rostedt <[email protected]>
97491e2c4STzvetomir Stoyanov (VMware)  * Copyright (C) 2021, VMware Inc, Tzvetomir Stoyanov [email protected]>
107491e2c4STzvetomir Stoyanov (VMware)  *
117491e2c4STzvetomir Stoyanov (VMware)  */
127491e2c4STzvetomir Stoyanov (VMware) #include <linux/module.h>
137491e2c4STzvetomir Stoyanov (VMware) #include <linux/mutex.h>
147491e2c4STzvetomir Stoyanov (VMware) #include <linux/ftrace.h>
157491e2c4STzvetomir Stoyanov (VMware) 
167491e2c4STzvetomir Stoyanov (VMware) #include "trace_dynevent.h"
177491e2c4STzvetomir Stoyanov (VMware) #include "trace_probe.h"
187491e2c4STzvetomir Stoyanov (VMware) #include "trace_probe_tmpl.h"
19f1d3cbfaSSteven Rostedt (Google) #include "trace_probe_kernel.h"
207491e2c4STzvetomir Stoyanov (VMware) 
217491e2c4STzvetomir Stoyanov (VMware) #define EPROBE_EVENT_SYSTEM "eprobes"
227491e2c4STzvetomir Stoyanov (VMware) 
237491e2c4STzvetomir Stoyanov (VMware) struct trace_eprobe {
247491e2c4STzvetomir Stoyanov (VMware) 	/* tracepoint system */
257491e2c4STzvetomir Stoyanov (VMware) 	const char *event_system;
267491e2c4STzvetomir Stoyanov (VMware) 
277491e2c4STzvetomir Stoyanov (VMware) 	/* tracepoint event */
287491e2c4STzvetomir Stoyanov (VMware) 	const char *event_name;
297491e2c4STzvetomir Stoyanov (VMware) 
30752be5c5SMasami Hiramatsu (Google) 	/* filter string for the tracepoint */
31752be5c5SMasami Hiramatsu (Google) 	char *filter_str;
32752be5c5SMasami Hiramatsu (Google) 
337491e2c4STzvetomir Stoyanov (VMware) 	struct trace_event_call *event;
347491e2c4STzvetomir Stoyanov (VMware) 
357491e2c4STzvetomir Stoyanov (VMware) 	struct dyn_event	devent;
367491e2c4STzvetomir Stoyanov (VMware) 	struct trace_probe	tp;
377491e2c4STzvetomir Stoyanov (VMware) };
387491e2c4STzvetomir Stoyanov (VMware) 
397491e2c4STzvetomir Stoyanov (VMware) struct eprobe_data {
407491e2c4STzvetomir Stoyanov (VMware) 	struct trace_event_file	*file;
417491e2c4STzvetomir Stoyanov (VMware) 	struct trace_eprobe	*ep;
427491e2c4STzvetomir Stoyanov (VMware) };
437491e2c4STzvetomir Stoyanov (VMware) 
44f8bbf8b9SChuang Wang 
45f8bbf8b9SChuang Wang #define for_each_trace_eprobe_tp(ep, _tp) \
46f8bbf8b9SChuang Wang 	list_for_each_entry(ep, trace_probe_probe_list(_tp), tp.list)
47f8bbf8b9SChuang Wang 
487491e2c4STzvetomir Stoyanov (VMware) static int __trace_eprobe_create(int argc, const char *argv[]);
497491e2c4STzvetomir Stoyanov (VMware) 
trace_event_probe_cleanup(struct trace_eprobe * ep)507491e2c4STzvetomir Stoyanov (VMware) static void trace_event_probe_cleanup(struct trace_eprobe *ep)
517491e2c4STzvetomir Stoyanov (VMware) {
527491e2c4STzvetomir Stoyanov (VMware) 	if (!ep)
537491e2c4STzvetomir Stoyanov (VMware) 		return;
547491e2c4STzvetomir Stoyanov (VMware) 	trace_probe_cleanup(&ep->tp);
557491e2c4STzvetomir Stoyanov (VMware) 	kfree(ep->event_name);
567491e2c4STzvetomir Stoyanov (VMware) 	kfree(ep->event_system);
577491e2c4STzvetomir Stoyanov (VMware) 	if (ep->event)
587491e2c4STzvetomir Stoyanov (VMware) 		trace_event_put_ref(ep->event);
59d1776c02SRafael Mendonca 	kfree(ep->filter_str);
607491e2c4STzvetomir Stoyanov (VMware) 	kfree(ep);
617491e2c4STzvetomir Stoyanov (VMware) }
627491e2c4STzvetomir Stoyanov (VMware) 
to_trace_eprobe(struct dyn_event * ev)637491e2c4STzvetomir Stoyanov (VMware) static struct trace_eprobe *to_trace_eprobe(struct dyn_event *ev)
647491e2c4STzvetomir Stoyanov (VMware) {
657491e2c4STzvetomir Stoyanov (VMware) 	return container_of(ev, struct trace_eprobe, devent);
667491e2c4STzvetomir Stoyanov (VMware) }
677491e2c4STzvetomir Stoyanov (VMware) 
eprobe_dyn_event_create(const char * raw_command)687491e2c4STzvetomir Stoyanov (VMware) static int eprobe_dyn_event_create(const char *raw_command)
697491e2c4STzvetomir Stoyanov (VMware) {
707491e2c4STzvetomir Stoyanov (VMware) 	return trace_probe_create(raw_command, __trace_eprobe_create);
717491e2c4STzvetomir Stoyanov (VMware) }
727491e2c4STzvetomir Stoyanov (VMware) 
eprobe_dyn_event_show(struct seq_file * m,struct dyn_event * ev)737491e2c4STzvetomir Stoyanov (VMware) static int eprobe_dyn_event_show(struct seq_file *m, struct dyn_event *ev)
747491e2c4STzvetomir Stoyanov (VMware) {
757491e2c4STzvetomir Stoyanov (VMware) 	struct trace_eprobe *ep = to_trace_eprobe(ev);
767491e2c4STzvetomir Stoyanov (VMware) 	int i;
777491e2c4STzvetomir Stoyanov (VMware) 
787491e2c4STzvetomir Stoyanov (VMware) 	seq_printf(m, "e:%s/%s", trace_probe_group_name(&ep->tp),
797491e2c4STzvetomir Stoyanov (VMware) 				trace_probe_name(&ep->tp));
807491e2c4STzvetomir Stoyanov (VMware) 	seq_printf(m, " %s.%s", ep->event_system, ep->event_name);
817491e2c4STzvetomir Stoyanov (VMware) 
827491e2c4STzvetomir Stoyanov (VMware) 	for (i = 0; i < ep->tp.nr_args; i++)
837491e2c4STzvetomir Stoyanov (VMware) 		seq_printf(m, " %s=%s", ep->tp.args[i].name, ep->tp.args[i].comm);
847491e2c4STzvetomir Stoyanov (VMware) 	seq_putc(m, '\n');
857491e2c4STzvetomir Stoyanov (VMware) 
867491e2c4STzvetomir Stoyanov (VMware) 	return 0;
877491e2c4STzvetomir Stoyanov (VMware) }
887491e2c4STzvetomir Stoyanov (VMware) 
unregister_trace_eprobe(struct trace_eprobe * ep)897491e2c4STzvetomir Stoyanov (VMware) static int unregister_trace_eprobe(struct trace_eprobe *ep)
907491e2c4STzvetomir Stoyanov (VMware) {
917491e2c4STzvetomir Stoyanov (VMware) 	/* If other probes are on the event, just unregister eprobe */
927491e2c4STzvetomir Stoyanov (VMware) 	if (trace_probe_has_sibling(&ep->tp))
937491e2c4STzvetomir Stoyanov (VMware) 		goto unreg;
947491e2c4STzvetomir Stoyanov (VMware) 
957491e2c4STzvetomir Stoyanov (VMware) 	/* Enabled event can not be unregistered */
967491e2c4STzvetomir Stoyanov (VMware) 	if (trace_probe_is_enabled(&ep->tp))
977491e2c4STzvetomir Stoyanov (VMware) 		return -EBUSY;
987491e2c4STzvetomir Stoyanov (VMware) 
997491e2c4STzvetomir Stoyanov (VMware) 	/* Will fail if probe is being used by ftrace or perf */
1007491e2c4STzvetomir Stoyanov (VMware) 	if (trace_probe_unregister_event_call(&ep->tp))
1017491e2c4STzvetomir Stoyanov (VMware) 		return -EBUSY;
1027491e2c4STzvetomir Stoyanov (VMware) 
1037491e2c4STzvetomir Stoyanov (VMware) unreg:
1047491e2c4STzvetomir Stoyanov (VMware) 	dyn_event_remove(&ep->devent);
1057491e2c4STzvetomir Stoyanov (VMware) 	trace_probe_unlink(&ep->tp);
1067491e2c4STzvetomir Stoyanov (VMware) 
1077491e2c4STzvetomir Stoyanov (VMware) 	return 0;
1087491e2c4STzvetomir Stoyanov (VMware) }
1097491e2c4STzvetomir Stoyanov (VMware) 
eprobe_dyn_event_release(struct dyn_event * ev)1107491e2c4STzvetomir Stoyanov (VMware) static int eprobe_dyn_event_release(struct dyn_event *ev)
1117491e2c4STzvetomir Stoyanov (VMware) {
1127491e2c4STzvetomir Stoyanov (VMware) 	struct trace_eprobe *ep = to_trace_eprobe(ev);
1137491e2c4STzvetomir Stoyanov (VMware) 	int ret = unregister_trace_eprobe(ep);
1147491e2c4STzvetomir Stoyanov (VMware) 
1157491e2c4STzvetomir Stoyanov (VMware) 	if (!ret)
1167491e2c4STzvetomir Stoyanov (VMware) 		trace_event_probe_cleanup(ep);
1177491e2c4STzvetomir Stoyanov (VMware) 	return ret;
1187491e2c4STzvetomir Stoyanov (VMware) }
1197491e2c4STzvetomir Stoyanov (VMware) 
eprobe_dyn_event_is_busy(struct dyn_event * ev)1207491e2c4STzvetomir Stoyanov (VMware) static bool eprobe_dyn_event_is_busy(struct dyn_event *ev)
1217491e2c4STzvetomir Stoyanov (VMware) {
1227491e2c4STzvetomir Stoyanov (VMware) 	struct trace_eprobe *ep = to_trace_eprobe(ev);
1237491e2c4STzvetomir Stoyanov (VMware) 
1247491e2c4STzvetomir Stoyanov (VMware) 	return trace_probe_is_enabled(&ep->tp);
1257491e2c4STzvetomir Stoyanov (VMware) }
1267491e2c4STzvetomir Stoyanov (VMware) 
eprobe_dyn_event_match(const char * system,const char * event,int argc,const char ** argv,struct dyn_event * ev)1277491e2c4STzvetomir Stoyanov (VMware) static bool eprobe_dyn_event_match(const char *system, const char *event,
1287491e2c4STzvetomir Stoyanov (VMware) 			int argc, const char **argv, struct dyn_event *ev)
1297491e2c4STzvetomir Stoyanov (VMware) {
1307491e2c4STzvetomir Stoyanov (VMware) 	struct trace_eprobe *ep = to_trace_eprobe(ev);
1317d5fda1cSSteven Rostedt (VMware) 	const char *slash;
1327491e2c4STzvetomir Stoyanov (VMware) 
1337d5fda1cSSteven Rostedt (VMware) 	/*
1347d5fda1cSSteven Rostedt (VMware) 	 * We match the following:
1357d5fda1cSSteven Rostedt (VMware) 	 *  event only			- match all eprobes with event name
1367d5fda1cSSteven Rostedt (VMware) 	 *  system and event only	- match all system/event probes
13795c104c3SLinyu Yuan 	 *  system only			- match all system probes
1387d5fda1cSSteven Rostedt (VMware) 	 *
1397d5fda1cSSteven Rostedt (VMware) 	 * The below has the above satisfied with more arguments:
1407d5fda1cSSteven Rostedt (VMware) 	 *
1417d5fda1cSSteven Rostedt (VMware) 	 *  attached system/event	- If the arg has the system and event
1427d5fda1cSSteven Rostedt (VMware) 	 *				  the probe is attached to, match
1437d5fda1cSSteven Rostedt (VMware) 	 *				  probes with the attachment.
1447d5fda1cSSteven Rostedt (VMware) 	 *
1457d5fda1cSSteven Rostedt (VMware) 	 *  If any more args are given, then it requires a full match.
1467d5fda1cSSteven Rostedt (VMware) 	 */
1477d5fda1cSSteven Rostedt (VMware) 
1487d5fda1cSSteven Rostedt (VMware) 	/*
1497d5fda1cSSteven Rostedt (VMware) 	 * If system exists, but this probe is not part of that system
1507d5fda1cSSteven Rostedt (VMware) 	 * do not match.
1517d5fda1cSSteven Rostedt (VMware) 	 */
1527d5fda1cSSteven Rostedt (VMware) 	if (system && strcmp(trace_probe_group_name(&ep->tp), system) != 0)
1537d5fda1cSSteven Rostedt (VMware) 		return false;
1547d5fda1cSSteven Rostedt (VMware) 
1557d5fda1cSSteven Rostedt (VMware) 	/* Must match the event name */
15695c104c3SLinyu Yuan 	if (event[0] != '\0' && strcmp(trace_probe_name(&ep->tp), event) != 0)
1577d5fda1cSSteven Rostedt (VMware) 		return false;
1587d5fda1cSSteven Rostedt (VMware) 
1597d5fda1cSSteven Rostedt (VMware) 	/* No arguments match all */
1607d5fda1cSSteven Rostedt (VMware) 	if (argc < 1)
1617d5fda1cSSteven Rostedt (VMware) 		return true;
1627d5fda1cSSteven Rostedt (VMware) 
1637d5fda1cSSteven Rostedt (VMware) 	/* First argument is the system/event the probe is attached to */
1647d5fda1cSSteven Rostedt (VMware) 
1657d5fda1cSSteven Rostedt (VMware) 	slash = strchr(argv[0], '/');
1667d5fda1cSSteven Rostedt (VMware) 	if (!slash)
1677d5fda1cSSteven Rostedt (VMware) 		slash = strchr(argv[0], '.');
1687d5fda1cSSteven Rostedt (VMware) 	if (!slash)
1697d5fda1cSSteven Rostedt (VMware) 		return false;
1707d5fda1cSSteven Rostedt (VMware) 
1717d5fda1cSSteven Rostedt (VMware) 	if (strncmp(ep->event_system, argv[0], slash - argv[0]))
1727d5fda1cSSteven Rostedt (VMware) 		return false;
1737d5fda1cSSteven Rostedt (VMware) 	if (strcmp(ep->event_name, slash + 1))
1747d5fda1cSSteven Rostedt (VMware) 		return false;
1757d5fda1cSSteven Rostedt (VMware) 
1767d5fda1cSSteven Rostedt (VMware) 	argc--;
1777d5fda1cSSteven Rostedt (VMware) 	argv++;
1787d5fda1cSSteven Rostedt (VMware) 
1797d5fda1cSSteven Rostedt (VMware) 	/* If there are no other args, then match */
1807d5fda1cSSteven Rostedt (VMware) 	if (argc < 1)
1817d5fda1cSSteven Rostedt (VMware) 		return true;
1827d5fda1cSSteven Rostedt (VMware) 
1837d5fda1cSSteven Rostedt (VMware) 	return trace_probe_match_command_args(&ep->tp, argc, argv);
1847491e2c4STzvetomir Stoyanov (VMware) }
1857491e2c4STzvetomir Stoyanov (VMware) 
1867491e2c4STzvetomir Stoyanov (VMware) static struct dyn_event_operations eprobe_dyn_event_ops = {
1877491e2c4STzvetomir Stoyanov (VMware) 	.create = eprobe_dyn_event_create,
1887491e2c4STzvetomir Stoyanov (VMware) 	.show = eprobe_dyn_event_show,
1897491e2c4STzvetomir Stoyanov (VMware) 	.is_busy = eprobe_dyn_event_is_busy,
1907491e2c4STzvetomir Stoyanov (VMware) 	.free = eprobe_dyn_event_release,
1917491e2c4STzvetomir Stoyanov (VMware) 	.match = eprobe_dyn_event_match,
1927491e2c4STzvetomir Stoyanov (VMware) };
1937491e2c4STzvetomir Stoyanov (VMware) 
alloc_event_probe(const char * group,const char * this_event,struct trace_event_call * event,int nargs)1947491e2c4STzvetomir Stoyanov (VMware) static struct trace_eprobe *alloc_event_probe(const char *group,
1957491e2c4STzvetomir Stoyanov (VMware) 					      const char *this_event,
1967491e2c4STzvetomir Stoyanov (VMware) 					      struct trace_event_call *event,
1977491e2c4STzvetomir Stoyanov (VMware) 					      int nargs)
1987491e2c4STzvetomir Stoyanov (VMware) {
1997491e2c4STzvetomir Stoyanov (VMware) 	struct trace_eprobe *ep;
2007491e2c4STzvetomir Stoyanov (VMware) 	const char *event_name;
2017491e2c4STzvetomir Stoyanov (VMware) 	const char *sys_name;
2027491e2c4STzvetomir Stoyanov (VMware) 	int ret = -ENOMEM;
2037491e2c4STzvetomir Stoyanov (VMware) 
2047491e2c4STzvetomir Stoyanov (VMware) 	if (!event)
2057491e2c4STzvetomir Stoyanov (VMware) 		return ERR_PTR(-ENODEV);
2067491e2c4STzvetomir Stoyanov (VMware) 
2077491e2c4STzvetomir Stoyanov (VMware) 	sys_name = event->class->system;
2087491e2c4STzvetomir Stoyanov (VMware) 	event_name = trace_event_name(event);
2097491e2c4STzvetomir Stoyanov (VMware) 
2107491e2c4STzvetomir Stoyanov (VMware) 	ep = kzalloc(struct_size(ep, tp.args, nargs), GFP_KERNEL);
2117491e2c4STzvetomir Stoyanov (VMware) 	if (!ep) {
2125615e088SDan Carpenter 		trace_event_put_ref(event);
2137491e2c4STzvetomir Stoyanov (VMware) 		goto error;
2147491e2c4STzvetomir Stoyanov (VMware) 	}
2157491e2c4STzvetomir Stoyanov (VMware) 	ep->event = event;
2167491e2c4STzvetomir Stoyanov (VMware) 	ep->event_name = kstrdup(event_name, GFP_KERNEL);
2177491e2c4STzvetomir Stoyanov (VMware) 	if (!ep->event_name)
2187491e2c4STzvetomir Stoyanov (VMware) 		goto error;
2197491e2c4STzvetomir Stoyanov (VMware) 	ep->event_system = kstrdup(sys_name, GFP_KERNEL);
2207491e2c4STzvetomir Stoyanov (VMware) 	if (!ep->event_system)
2217491e2c4STzvetomir Stoyanov (VMware) 		goto error;
2227491e2c4STzvetomir Stoyanov (VMware) 
223035ba760SMasami Hiramatsu (Google) 	ret = trace_probe_init(&ep->tp, this_event, group, false, nargs);
2247491e2c4STzvetomir Stoyanov (VMware) 	if (ret < 0)
2257491e2c4STzvetomir Stoyanov (VMware) 		goto error;
2267491e2c4STzvetomir Stoyanov (VMware) 
2277491e2c4STzvetomir Stoyanov (VMware) 	dyn_event_init(&ep->devent, &eprobe_dyn_event_ops);
2287491e2c4STzvetomir Stoyanov (VMware) 	return ep;
2297491e2c4STzvetomir Stoyanov (VMware) error:
2307491e2c4STzvetomir Stoyanov (VMware) 	trace_event_probe_cleanup(ep);
2317491e2c4STzvetomir Stoyanov (VMware) 	return ERR_PTR(ret);
2327491e2c4STzvetomir Stoyanov (VMware) }
2337491e2c4STzvetomir Stoyanov (VMware) 
eprobe_event_define_fields(struct trace_event_call * event_call)2347491e2c4STzvetomir Stoyanov (VMware) static int eprobe_event_define_fields(struct trace_event_call *event_call)
2357491e2c4STzvetomir Stoyanov (VMware) {
2367491e2c4STzvetomir Stoyanov (VMware) 	struct eprobe_trace_entry_head field;
2377491e2c4STzvetomir Stoyanov (VMware) 	struct trace_probe *tp;
2387491e2c4STzvetomir Stoyanov (VMware) 
2397491e2c4STzvetomir Stoyanov (VMware) 	tp = trace_probe_primary_from_call(event_call);
2407491e2c4STzvetomir Stoyanov (VMware) 	if (WARN_ON_ONCE(!tp))
2417491e2c4STzvetomir Stoyanov (VMware) 		return -ENOENT;
2427491e2c4STzvetomir Stoyanov (VMware) 
2437491e2c4STzvetomir Stoyanov (VMware) 	return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
2447491e2c4STzvetomir Stoyanov (VMware) }
2457491e2c4STzvetomir Stoyanov (VMware) 
2467491e2c4STzvetomir Stoyanov (VMware) static struct trace_event_fields eprobe_fields_array[] = {
2477491e2c4STzvetomir Stoyanov (VMware) 	{ .type = TRACE_FUNCTION_TYPE,
2487491e2c4STzvetomir Stoyanov (VMware) 	  .define_fields = eprobe_event_define_fields },
2497491e2c4STzvetomir Stoyanov (VMware) 	{}
2507491e2c4STzvetomir Stoyanov (VMware) };
2517491e2c4STzvetomir Stoyanov (VMware) 
2527491e2c4STzvetomir Stoyanov (VMware) /* Event entry printers */
2537491e2c4STzvetomir Stoyanov (VMware) static enum print_line_t
print_eprobe_event(struct trace_iterator * iter,int flags,struct trace_event * event)2547491e2c4STzvetomir Stoyanov (VMware) print_eprobe_event(struct trace_iterator *iter, int flags,
2557491e2c4STzvetomir Stoyanov (VMware) 		   struct trace_event *event)
2567491e2c4STzvetomir Stoyanov (VMware) {
2577491e2c4STzvetomir Stoyanov (VMware) 	struct eprobe_trace_entry_head *field;
2587491e2c4STzvetomir Stoyanov (VMware) 	struct trace_event_call *pevent;
2597491e2c4STzvetomir Stoyanov (VMware) 	struct trace_event *probed_event;
2607491e2c4STzvetomir Stoyanov (VMware) 	struct trace_seq *s = &iter->seq;
261b61edd57SSteven Rostedt (Google) 	struct trace_eprobe *ep;
2627491e2c4STzvetomir Stoyanov (VMware) 	struct trace_probe *tp;
263b61edd57SSteven Rostedt (Google) 	unsigned int type;
2647491e2c4STzvetomir Stoyanov (VMware) 
2657491e2c4STzvetomir Stoyanov (VMware) 	field = (struct eprobe_trace_entry_head *)iter->ent;
2667491e2c4STzvetomir Stoyanov (VMware) 	tp = trace_probe_primary_from_call(
2677491e2c4STzvetomir Stoyanov (VMware) 		container_of(event, struct trace_event_call, event));
2687491e2c4STzvetomir Stoyanov (VMware) 	if (WARN_ON_ONCE(!tp))
2697491e2c4STzvetomir Stoyanov (VMware) 		goto out;
2707491e2c4STzvetomir Stoyanov (VMware) 
271b61edd57SSteven Rostedt (Google) 	ep = container_of(tp, struct trace_eprobe, tp);
272b61edd57SSteven Rostedt (Google) 	type = ep->event->event.type;
273b61edd57SSteven Rostedt (Google) 
2747491e2c4STzvetomir Stoyanov (VMware) 	trace_seq_printf(s, "%s: (", trace_probe_name(tp));
2757491e2c4STzvetomir Stoyanov (VMware) 
276b61edd57SSteven Rostedt (Google) 	probed_event = ftrace_find_event(type);
2777491e2c4STzvetomir Stoyanov (VMware) 	if (probed_event) {
2787491e2c4STzvetomir Stoyanov (VMware) 		pevent = container_of(probed_event, struct trace_event_call, event);
2797491e2c4STzvetomir Stoyanov (VMware) 		trace_seq_printf(s, "%s.%s", pevent->class->system,
2807491e2c4STzvetomir Stoyanov (VMware) 				 trace_event_name(pevent));
2817491e2c4STzvetomir Stoyanov (VMware) 	} else {
282b61edd57SSteven Rostedt (Google) 		trace_seq_printf(s, "%u", type);
2837491e2c4STzvetomir Stoyanov (VMware) 	}
2847491e2c4STzvetomir Stoyanov (VMware) 
2857491e2c4STzvetomir Stoyanov (VMware) 	trace_seq_putc(s, ')');
2867491e2c4STzvetomir Stoyanov (VMware) 
287196b6389SSong Chen 	if (trace_probe_print_args(s, tp->args, tp->nr_args,
2887491e2c4STzvetomir Stoyanov (VMware) 			     (u8 *)&field[1], field) < 0)
2897491e2c4STzvetomir Stoyanov (VMware) 		goto out;
2907491e2c4STzvetomir Stoyanov (VMware) 
2917491e2c4STzvetomir Stoyanov (VMware) 	trace_seq_putc(s, '\n');
2927491e2c4STzvetomir Stoyanov (VMware)  out:
2937491e2c4STzvetomir Stoyanov (VMware) 	return trace_handle_return(s);
2947491e2c4STzvetomir Stoyanov (VMware) }
2957491e2c4STzvetomir Stoyanov (VMware) 
296672a2bf8SSong Chen static nokprobe_inline unsigned long
get_event_field(struct fetch_insn * code,void * rec)297672a2bf8SSong Chen get_event_field(struct fetch_insn *code, void *rec)
2987491e2c4STzvetomir Stoyanov (VMware) {
2997491e2c4STzvetomir Stoyanov (VMware) 	struct ftrace_event_field *field = code->data;
3007491e2c4STzvetomir Stoyanov (VMware) 	unsigned long val;
3017491e2c4STzvetomir Stoyanov (VMware) 	void *addr;
3027491e2c4STzvetomir Stoyanov (VMware) 
3037491e2c4STzvetomir Stoyanov (VMware) 	addr = rec + field->offset;
3047491e2c4STzvetomir Stoyanov (VMware) 
305f04dec93SSteven Rostedt (Google) 	if (is_string_field(field)) {
306f04dec93SSteven Rostedt (Google) 		switch (field->filter_type) {
307f04dec93SSteven Rostedt (Google) 		case FILTER_DYN_STRING:
308f04dec93SSteven Rostedt (Google) 			val = (unsigned long)(rec + (*(unsigned int *)addr & 0xffff));
309f04dec93SSteven Rostedt (Google) 			break;
310f04dec93SSteven Rostedt (Google) 		case FILTER_RDYN_STRING:
311f04dec93SSteven Rostedt (Google) 			val = (unsigned long)(addr + (*(unsigned int *)addr & 0xffff));
312f04dec93SSteven Rostedt (Google) 			break;
313f04dec93SSteven Rostedt (Google) 		case FILTER_STATIC_STRING:
314f04dec93SSteven Rostedt (Google) 			val = (unsigned long)addr;
315f04dec93SSteven Rostedt (Google) 			break;
316f04dec93SSteven Rostedt (Google) 		case FILTER_PTR_STRING:
317f04dec93SSteven Rostedt (Google) 			val = (unsigned long)(*(char *)addr);
318f04dec93SSteven Rostedt (Google) 			break;
319f04dec93SSteven Rostedt (Google) 		default:
320f04dec93SSteven Rostedt (Google) 			WARN_ON_ONCE(1);
321f04dec93SSteven Rostedt (Google) 			return 0;
322f04dec93SSteven Rostedt (Google) 		}
323f04dec93SSteven Rostedt (Google) 		return val;
324f04dec93SSteven Rostedt (Google) 	}
325f04dec93SSteven Rostedt (Google) 
3267491e2c4STzvetomir Stoyanov (VMware) 	switch (field->size) {
3277491e2c4STzvetomir Stoyanov (VMware) 	case 1:
3287491e2c4STzvetomir Stoyanov (VMware) 		if (field->is_signed)
3297491e2c4STzvetomir Stoyanov (VMware) 			val = *(char *)addr;
3307491e2c4STzvetomir Stoyanov (VMware) 		else
3317491e2c4STzvetomir Stoyanov (VMware) 			val = *(unsigned char *)addr;
3327491e2c4STzvetomir Stoyanov (VMware) 		break;
3337491e2c4STzvetomir Stoyanov (VMware) 	case 2:
3347491e2c4STzvetomir Stoyanov (VMware) 		if (field->is_signed)
3357491e2c4STzvetomir Stoyanov (VMware) 			val = *(short *)addr;
3367491e2c4STzvetomir Stoyanov (VMware) 		else
3377491e2c4STzvetomir Stoyanov (VMware) 			val = *(unsigned short *)addr;
3387491e2c4STzvetomir Stoyanov (VMware) 		break;
3397491e2c4STzvetomir Stoyanov (VMware) 	case 4:
3407491e2c4STzvetomir Stoyanov (VMware) 		if (field->is_signed)
3417491e2c4STzvetomir Stoyanov (VMware) 			val = *(int *)addr;
3427491e2c4STzvetomir Stoyanov (VMware) 		else
3437491e2c4STzvetomir Stoyanov (VMware) 			val = *(unsigned int *)addr;
3447491e2c4STzvetomir Stoyanov (VMware) 		break;
3457491e2c4STzvetomir Stoyanov (VMware) 	default:
3467491e2c4STzvetomir Stoyanov (VMware) 		if (field->is_signed)
3477491e2c4STzvetomir Stoyanov (VMware) 			val = *(long *)addr;
3487491e2c4STzvetomir Stoyanov (VMware) 		else
3497491e2c4STzvetomir Stoyanov (VMware) 			val = *(unsigned long *)addr;
3507491e2c4STzvetomir Stoyanov (VMware) 		break;
3517491e2c4STzvetomir Stoyanov (VMware) 	}
3527491e2c4STzvetomir Stoyanov (VMware) 	return val;
3537491e2c4STzvetomir Stoyanov (VMware) }
3547491e2c4STzvetomir Stoyanov (VMware) 
get_eprobe_size(struct trace_probe * tp,void * rec)3557491e2c4STzvetomir Stoyanov (VMware) static int get_eprobe_size(struct trace_probe *tp, void *rec)
3567491e2c4STzvetomir Stoyanov (VMware) {
3576a832ec3SSteven Rostedt (Google) 	struct fetch_insn *code;
3587491e2c4STzvetomir Stoyanov (VMware) 	struct probe_arg *arg;
3597491e2c4STzvetomir Stoyanov (VMware) 	int i, len, ret = 0;
3607491e2c4STzvetomir Stoyanov (VMware) 
3617491e2c4STzvetomir Stoyanov (VMware) 	for (i = 0; i < tp->nr_args; i++) {
3627491e2c4STzvetomir Stoyanov (VMware) 		arg = tp->args + i;
3636a832ec3SSteven Rostedt (Google) 		if (arg->dynamic) {
3647491e2c4STzvetomir Stoyanov (VMware) 			unsigned long val;
3657491e2c4STzvetomir Stoyanov (VMware) 
3666a832ec3SSteven Rostedt (Google) 			code = arg->code;
3676a832ec3SSteven Rostedt (Google)  retry:
3686a832ec3SSteven Rostedt (Google) 			switch (code->op) {
3696a832ec3SSteven Rostedt (Google) 			case FETCH_OP_TP_ARG:
3706a832ec3SSteven Rostedt (Google) 				val = get_event_field(code, rec);
3716a832ec3SSteven Rostedt (Google) 				break;
3726a832ec3SSteven Rostedt (Google) 			case FETCH_NOP_SYMBOL:	/* Ignore a place holder */
3736a832ec3SSteven Rostedt (Google) 				code++;
3746a832ec3SSteven Rostedt (Google) 				goto retry;
3756a832ec3SSteven Rostedt (Google) 			default:
376bd78acc8SSong Chen 				if (process_common_fetch_insn(code, &val) < 0)
3776a832ec3SSteven Rostedt (Google) 					continue;
3786a832ec3SSteven Rostedt (Google) 			}
3796a832ec3SSteven Rostedt (Google) 			code++;
3806a832ec3SSteven Rostedt (Google) 			len = process_fetch_insn_bottom(code, val, NULL, NULL);
3817491e2c4STzvetomir Stoyanov (VMware) 			if (len > 0)
3827491e2c4STzvetomir Stoyanov (VMware) 				ret += len;
3837491e2c4STzvetomir Stoyanov (VMware) 		}
3847491e2c4STzvetomir Stoyanov (VMware) 	}
3857491e2c4STzvetomir Stoyanov (VMware) 
3867491e2c4STzvetomir Stoyanov (VMware) 	return ret;
3877491e2c4STzvetomir Stoyanov (VMware) }
3887491e2c4STzvetomir Stoyanov (VMware) 
3897491e2c4STzvetomir Stoyanov (VMware) /* Kprobe specific fetch functions */
3907491e2c4STzvetomir Stoyanov (VMware) 
3917491e2c4STzvetomir Stoyanov (VMware) /* Note that we don't verify it, since the code does not come from user space */
3927491e2c4STzvetomir Stoyanov (VMware) static int
process_fetch_insn(struct fetch_insn * code,void * rec,void * edata,void * dest,void * base)39325f00e40SMasami Hiramatsu (Google) process_fetch_insn(struct fetch_insn *code, void *rec, void *edata,
39425f00e40SMasami Hiramatsu (Google) 		   void *dest, void *base)
3957491e2c4STzvetomir Stoyanov (VMware) {
3967491e2c4STzvetomir Stoyanov (VMware) 	unsigned long val;
397bd78acc8SSong Chen 	int ret;
3987491e2c4STzvetomir Stoyanov (VMware) 
3996a832ec3SSteven Rostedt (Google)  retry:
4006a832ec3SSteven Rostedt (Google) 	switch (code->op) {
4016a832ec3SSteven Rostedt (Google) 	case FETCH_OP_TP_ARG:
4027491e2c4STzvetomir Stoyanov (VMware) 		val = get_event_field(code, rec);
4036a832ec3SSteven Rostedt (Google) 		break;
4046a832ec3SSteven Rostedt (Google) 	case FETCH_NOP_SYMBOL:	/* Ignore a place holder */
4056a832ec3SSteven Rostedt (Google) 		code++;
4066a832ec3SSteven Rostedt (Google) 		goto retry;
4076a832ec3SSteven Rostedt (Google) 	default:
408bd78acc8SSong Chen 		ret = process_common_fetch_insn(code, &val);
409bd78acc8SSong Chen 		if (ret < 0)
410bd78acc8SSong Chen 			return ret;
4116a832ec3SSteven Rostedt (Google) 	}
4126a832ec3SSteven Rostedt (Google) 	code++;
4136a832ec3SSteven Rostedt (Google) 	return process_fetch_insn_bottom(code, val, dest, base);
4147491e2c4STzvetomir Stoyanov (VMware) }
NOKPROBE_SYMBOL(process_fetch_insn)4157491e2c4STzvetomir Stoyanov (VMware) NOKPROBE_SYMBOL(process_fetch_insn)
4167491e2c4STzvetomir Stoyanov (VMware) 
4177491e2c4STzvetomir Stoyanov (VMware) /* eprobe handler */
4187491e2c4STzvetomir Stoyanov (VMware) static inline void
4197491e2c4STzvetomir Stoyanov (VMware) __eprobe_trace_func(struct eprobe_data *edata, void *rec)
4207491e2c4STzvetomir Stoyanov (VMware) {
4217491e2c4STzvetomir Stoyanov (VMware) 	struct eprobe_trace_entry_head *entry;
4227491e2c4STzvetomir Stoyanov (VMware) 	struct trace_event_call *call = trace_probe_event_call(&edata->ep->tp);
4237491e2c4STzvetomir Stoyanov (VMware) 	struct trace_event_buffer fbuffer;
4247491e2c4STzvetomir Stoyanov (VMware) 	int dsize;
4257491e2c4STzvetomir Stoyanov (VMware) 
4267491e2c4STzvetomir Stoyanov (VMware) 	if (WARN_ON_ONCE(call != edata->file->event_call))
4277491e2c4STzvetomir Stoyanov (VMware) 		return;
4287491e2c4STzvetomir Stoyanov (VMware) 
4297491e2c4STzvetomir Stoyanov (VMware) 	if (trace_trigger_soft_disabled(edata->file))
4307491e2c4STzvetomir Stoyanov (VMware) 		return;
4317491e2c4STzvetomir Stoyanov (VMware) 
4327491e2c4STzvetomir Stoyanov (VMware) 	dsize = get_eprobe_size(&edata->ep->tp, rec);
4337491e2c4STzvetomir Stoyanov (VMware) 
4343e8b1a29SSteven Rostedt (VMware) 	entry = trace_event_buffer_reserve(&fbuffer, edata->file,
4353e8b1a29SSteven Rostedt (VMware) 					   sizeof(*entry) + edata->ep->tp.size + dsize);
4363e8b1a29SSteven Rostedt (VMware) 
4373e8b1a29SSteven Rostedt (VMware) 	if (!entry)
4387491e2c4STzvetomir Stoyanov (VMware) 		return;
4397491e2c4STzvetomir Stoyanov (VMware) 
4407491e2c4STzvetomir Stoyanov (VMware) 	entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event);
44125f00e40SMasami Hiramatsu (Google) 	store_trace_args(&entry[1], &edata->ep->tp, rec, NULL, sizeof(*entry), dsize);
4427491e2c4STzvetomir Stoyanov (VMware) 
4437491e2c4STzvetomir Stoyanov (VMware) 	trace_event_buffer_commit(&fbuffer);
4447491e2c4STzvetomir Stoyanov (VMware) }
4457491e2c4STzvetomir Stoyanov (VMware) 
4467491e2c4STzvetomir Stoyanov (VMware) /*
4477491e2c4STzvetomir Stoyanov (VMware)  * The event probe implementation uses event triggers to get access to
4487491e2c4STzvetomir Stoyanov (VMware)  * the event it is attached to, but is not an actual trigger. The below
4497491e2c4STzvetomir Stoyanov (VMware)  * functions are just stubs to fulfill what is needed to use the trigger
4507491e2c4STzvetomir Stoyanov (VMware)  * infrastructure.
4517491e2c4STzvetomir Stoyanov (VMware)  */
eprobe_trigger_init(struct event_trigger_data * data)45247670541STom Zanussi static int eprobe_trigger_init(struct event_trigger_data *data)
4537491e2c4STzvetomir Stoyanov (VMware) {
4547491e2c4STzvetomir Stoyanov (VMware) 	return 0;
4557491e2c4STzvetomir Stoyanov (VMware) }
4567491e2c4STzvetomir Stoyanov (VMware) 
eprobe_trigger_free(struct event_trigger_data * data)45747670541STom Zanussi static void eprobe_trigger_free(struct event_trigger_data *data)
4587491e2c4STzvetomir Stoyanov (VMware) {
4597491e2c4STzvetomir Stoyanov (VMware) 
4607491e2c4STzvetomir Stoyanov (VMware) }
4617491e2c4STzvetomir Stoyanov (VMware) 
eprobe_trigger_print(struct seq_file * m,struct event_trigger_data * data)4627491e2c4STzvetomir Stoyanov (VMware) static int eprobe_trigger_print(struct seq_file *m,
4637491e2c4STzvetomir Stoyanov (VMware) 				struct event_trigger_data *data)
4647491e2c4STzvetomir Stoyanov (VMware) {
4657491e2c4STzvetomir Stoyanov (VMware) 	/* Do not print eprobe event triggers */
4667491e2c4STzvetomir Stoyanov (VMware) 	return 0;
4677491e2c4STzvetomir Stoyanov (VMware) }
4687491e2c4STzvetomir Stoyanov (VMware) 
eprobe_trigger_func(struct event_trigger_data * data,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * rbe)4697491e2c4STzvetomir Stoyanov (VMware) static void eprobe_trigger_func(struct event_trigger_data *data,
4707491e2c4STzvetomir Stoyanov (VMware) 				struct trace_buffer *buffer, void *rec,
4717491e2c4STzvetomir Stoyanov (VMware) 				struct ring_buffer_event *rbe)
4727491e2c4STzvetomir Stoyanov (VMware) {
4737491e2c4STzvetomir Stoyanov (VMware) 	struct eprobe_data *edata = data->private_data;
4747491e2c4STzvetomir Stoyanov (VMware) 
47594eedf3dSSteven Rostedt (Google) 	if (unlikely(!rec))
47694eedf3dSSteven Rostedt (Google) 		return;
47794eedf3dSSteven Rostedt (Google) 
4787491e2c4STzvetomir Stoyanov (VMware) 	__eprobe_trace_func(edata, rec);
4797491e2c4STzvetomir Stoyanov (VMware) }
4807491e2c4STzvetomir Stoyanov (VMware) 
481502d2e71SChristophe JAILLET static const struct event_trigger_ops eprobe_trigger_ops = {
482fb339e53STom Zanussi 	.trigger		= eprobe_trigger_func,
4837491e2c4STzvetomir Stoyanov (VMware) 	.print			= eprobe_trigger_print,
4847491e2c4STzvetomir Stoyanov (VMware) 	.init			= eprobe_trigger_init,
4857491e2c4STzvetomir Stoyanov (VMware) 	.free			= eprobe_trigger_free,
4867491e2c4STzvetomir Stoyanov (VMware) };
4877491e2c4STzvetomir Stoyanov (VMware) 
eprobe_trigger_cmd_parse(struct event_command * cmd_ops,struct trace_event_file * file,char * glob,char * cmd,char * param_and_filter)4889ec5a7d1STom Zanussi static int eprobe_trigger_cmd_parse(struct event_command *cmd_ops,
4897491e2c4STzvetomir Stoyanov (VMware) 				    struct trace_event_file *file,
490e1f187d0STom Zanussi 				    char *glob, char *cmd,
491e1f187d0STom Zanussi 				    char *param_and_filter)
4927491e2c4STzvetomir Stoyanov (VMware) {
4937491e2c4STzvetomir Stoyanov (VMware) 	return -1;
4947491e2c4STzvetomir Stoyanov (VMware) }
4957491e2c4STzvetomir Stoyanov (VMware) 
eprobe_trigger_reg_func(char * glob,struct event_trigger_data * data,struct trace_event_file * file)4962378a2d6STom Zanussi static int eprobe_trigger_reg_func(char *glob,
4977491e2c4STzvetomir Stoyanov (VMware) 				   struct event_trigger_data *data,
4987491e2c4STzvetomir Stoyanov (VMware) 				   struct trace_event_file *file)
4997491e2c4STzvetomir Stoyanov (VMware) {
5007491e2c4STzvetomir Stoyanov (VMware) 	return -1;
5017491e2c4STzvetomir Stoyanov (VMware) }
5027491e2c4STzvetomir Stoyanov (VMware) 
eprobe_trigger_unreg_func(char * glob,struct event_trigger_data * data,struct trace_event_file * file)5032378a2d6STom Zanussi static void eprobe_trigger_unreg_func(char *glob,
5047491e2c4STzvetomir Stoyanov (VMware) 				      struct event_trigger_data *data,
5057491e2c4STzvetomir Stoyanov (VMware) 				      struct trace_event_file *file)
5067491e2c4STzvetomir Stoyanov (VMware) {
5077491e2c4STzvetomir Stoyanov (VMware) 
5087491e2c4STzvetomir Stoyanov (VMware) }
5097491e2c4STzvetomir Stoyanov (VMware) 
eprobe_trigger_get_ops(char * cmd,char * param)510502d2e71SChristophe JAILLET static const struct event_trigger_ops *eprobe_trigger_get_ops(char *cmd,
5117491e2c4STzvetomir Stoyanov (VMware) 							      char *param)
5127491e2c4STzvetomir Stoyanov (VMware) {
5137491e2c4STzvetomir Stoyanov (VMware) 	return &eprobe_trigger_ops;
5147491e2c4STzvetomir Stoyanov (VMware) }
5157491e2c4STzvetomir Stoyanov (VMware) 
5167491e2c4STzvetomir Stoyanov (VMware) static struct event_command event_trigger_cmd = {
5177491e2c4STzvetomir Stoyanov (VMware) 	.name			= "eprobe",
5187491e2c4STzvetomir Stoyanov (VMware) 	.trigger_type		= ETT_EVENT_EPROBE,
5197491e2c4STzvetomir Stoyanov (VMware) 	.flags			= EVENT_CMD_FL_NEEDS_REC,
5209ec5a7d1STom Zanussi 	.parse			= eprobe_trigger_cmd_parse,
5217491e2c4STzvetomir Stoyanov (VMware) 	.reg			= eprobe_trigger_reg_func,
5227491e2c4STzvetomir Stoyanov (VMware) 	.unreg			= eprobe_trigger_unreg_func,
5237491e2c4STzvetomir Stoyanov (VMware) 	.unreg_all		= NULL,
5247491e2c4STzvetomir Stoyanov (VMware) 	.get_trigger_ops	= eprobe_trigger_get_ops,
5257491e2c4STzvetomir Stoyanov (VMware) 	.set_filter		= NULL,
5267491e2c4STzvetomir Stoyanov (VMware) };
5277491e2c4STzvetomir Stoyanov (VMware) 
5287491e2c4STzvetomir Stoyanov (VMware) static struct event_trigger_data *
new_eprobe_trigger(struct trace_eprobe * ep,struct trace_event_file * file)5297491e2c4STzvetomir Stoyanov (VMware) new_eprobe_trigger(struct trace_eprobe *ep, struct trace_event_file *file)
5307491e2c4STzvetomir Stoyanov (VMware) {
5317491e2c4STzvetomir Stoyanov (VMware) 	struct event_trigger_data *trigger;
532752be5c5SMasami Hiramatsu (Google) 	struct event_filter *filter = NULL;
5337491e2c4STzvetomir Stoyanov (VMware) 	struct eprobe_data *edata;
534752be5c5SMasami Hiramatsu (Google) 	int ret;
5357491e2c4STzvetomir Stoyanov (VMware) 
5367491e2c4STzvetomir Stoyanov (VMware) 	edata = kzalloc(sizeof(*edata), GFP_KERNEL);
5377491e2c4STzvetomir Stoyanov (VMware) 	trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
5387491e2c4STzvetomir Stoyanov (VMware) 	if (!trigger || !edata) {
539752be5c5SMasami Hiramatsu (Google) 		ret = -ENOMEM;
540752be5c5SMasami Hiramatsu (Google) 		goto error;
5417491e2c4STzvetomir Stoyanov (VMware) 	}
5427491e2c4STzvetomir Stoyanov (VMware) 
5437491e2c4STzvetomir Stoyanov (VMware) 	trigger->flags = EVENT_TRIGGER_FL_PROBE;
5447491e2c4STzvetomir Stoyanov (VMware) 	trigger->count = -1;
5457491e2c4STzvetomir Stoyanov (VMware) 	trigger->ops = &eprobe_trigger_ops;
5467491e2c4STzvetomir Stoyanov (VMware) 
5477491e2c4STzvetomir Stoyanov (VMware) 	/*
5487491e2c4STzvetomir Stoyanov (VMware) 	 * EVENT PROBE triggers are not registered as commands with
5497491e2c4STzvetomir Stoyanov (VMware) 	 * register_event_command(), as they are not controlled by the user
5507491e2c4STzvetomir Stoyanov (VMware) 	 * from the trigger file
5517491e2c4STzvetomir Stoyanov (VMware) 	 */
5527491e2c4STzvetomir Stoyanov (VMware) 	trigger->cmd_ops = &event_trigger_cmd;
5537491e2c4STzvetomir Stoyanov (VMware) 
5547491e2c4STzvetomir Stoyanov (VMware) 	INIT_LIST_HEAD(&trigger->list);
555752be5c5SMasami Hiramatsu (Google) 
556752be5c5SMasami Hiramatsu (Google) 	if (ep->filter_str) {
55740adaf51SMasami Hiramatsu (Google) 		ret = create_event_filter(file->tr, ep->event,
558752be5c5SMasami Hiramatsu (Google) 					ep->filter_str, false, &filter);
559752be5c5SMasami Hiramatsu (Google) 		if (ret)
560752be5c5SMasami Hiramatsu (Google) 			goto error;
561752be5c5SMasami Hiramatsu (Google) 	}
562752be5c5SMasami Hiramatsu (Google) 	RCU_INIT_POINTER(trigger->filter, filter);
5637491e2c4STzvetomir Stoyanov (VMware) 
5647491e2c4STzvetomir Stoyanov (VMware) 	edata->file = file;
5657491e2c4STzvetomir Stoyanov (VMware) 	edata->ep = ep;
5667491e2c4STzvetomir Stoyanov (VMware) 	trigger->private_data = edata;
5677491e2c4STzvetomir Stoyanov (VMware) 
5687491e2c4STzvetomir Stoyanov (VMware) 	return trigger;
569752be5c5SMasami Hiramatsu (Google) error:
570752be5c5SMasami Hiramatsu (Google) 	free_event_filter(filter);
571752be5c5SMasami Hiramatsu (Google) 	kfree(edata);
572752be5c5SMasami Hiramatsu (Google) 	kfree(trigger);
573752be5c5SMasami Hiramatsu (Google) 	return ERR_PTR(ret);
5747491e2c4STzvetomir Stoyanov (VMware) }
5757491e2c4STzvetomir Stoyanov (VMware) 
enable_eprobe(struct trace_eprobe * ep,struct trace_event_file * eprobe_file)5767491e2c4STzvetomir Stoyanov (VMware) static int enable_eprobe(struct trace_eprobe *ep,
5777491e2c4STzvetomir Stoyanov (VMware) 			 struct trace_event_file *eprobe_file)
5787491e2c4STzvetomir Stoyanov (VMware) {
5797491e2c4STzvetomir Stoyanov (VMware) 	struct event_trigger_data *trigger;
5807491e2c4STzvetomir Stoyanov (VMware) 	struct trace_event_file *file;
5817491e2c4STzvetomir Stoyanov (VMware) 	struct trace_array *tr = eprobe_file->tr;
5827491e2c4STzvetomir Stoyanov (VMware) 
5837491e2c4STzvetomir Stoyanov (VMware) 	file = find_event_file(tr, ep->event_system, ep->event_name);
5847491e2c4STzvetomir Stoyanov (VMware) 	if (!file)
5857491e2c4STzvetomir Stoyanov (VMware) 		return -ENOENT;
5867491e2c4STzvetomir Stoyanov (VMware) 	trigger = new_eprobe_trigger(ep, eprobe_file);
5877491e2c4STzvetomir Stoyanov (VMware) 	if (IS_ERR(trigger))
5887491e2c4STzvetomir Stoyanov (VMware) 		return PTR_ERR(trigger);
5897491e2c4STzvetomir Stoyanov (VMware) 
5907491e2c4STzvetomir Stoyanov (VMware) 	list_add_tail_rcu(&trigger->list, &file->triggers);
5917491e2c4STzvetomir Stoyanov (VMware) 
5927491e2c4STzvetomir Stoyanov (VMware) 	trace_event_trigger_enable_disable(file, 1);
5937491e2c4STzvetomir Stoyanov (VMware) 	update_cond_flag(file);
5947491e2c4STzvetomir Stoyanov (VMware) 
5957491e2c4STzvetomir Stoyanov (VMware) 	return 0;
5967491e2c4STzvetomir Stoyanov (VMware) }
5977491e2c4STzvetomir Stoyanov (VMware) 
5987491e2c4STzvetomir Stoyanov (VMware) static struct trace_event_functions eprobe_funcs = {
5997491e2c4STzvetomir Stoyanov (VMware) 	.trace		= print_eprobe_event
6007491e2c4STzvetomir Stoyanov (VMware) };
6017491e2c4STzvetomir Stoyanov (VMware) 
disable_eprobe(struct trace_eprobe * ep,struct trace_array * tr)6027491e2c4STzvetomir Stoyanov (VMware) static int disable_eprobe(struct trace_eprobe *ep,
6037491e2c4STzvetomir Stoyanov (VMware) 			  struct trace_array *tr)
6047491e2c4STzvetomir Stoyanov (VMware) {
605ba27d855SJakob Koschel 	struct event_trigger_data *trigger = NULL, *iter;
6067491e2c4STzvetomir Stoyanov (VMware) 	struct trace_event_file *file;
607752be5c5SMasami Hiramatsu (Google) 	struct event_filter *filter;
6087491e2c4STzvetomir Stoyanov (VMware) 	struct eprobe_data *edata;
6097491e2c4STzvetomir Stoyanov (VMware) 
6107491e2c4STzvetomir Stoyanov (VMware) 	file = find_event_file(tr, ep->event_system, ep->event_name);
6117491e2c4STzvetomir Stoyanov (VMware) 	if (!file)
6127491e2c4STzvetomir Stoyanov (VMware) 		return -ENOENT;
6137491e2c4STzvetomir Stoyanov (VMware) 
614ba27d855SJakob Koschel 	list_for_each_entry(iter, &file->triggers, list) {
615ba27d855SJakob Koschel 		if (!(iter->flags & EVENT_TRIGGER_FL_PROBE))
6167491e2c4STzvetomir Stoyanov (VMware) 			continue;
617ba27d855SJakob Koschel 		edata = iter->private_data;
618ba27d855SJakob Koschel 		if (edata->ep == ep) {
619ba27d855SJakob Koschel 			trigger = iter;
6207491e2c4STzvetomir Stoyanov (VMware) 			break;
6217491e2c4STzvetomir Stoyanov (VMware) 		}
622ba27d855SJakob Koschel 	}
623ba27d855SJakob Koschel 	if (!trigger)
6247491e2c4STzvetomir Stoyanov (VMware) 		return -ENODEV;
6257491e2c4STzvetomir Stoyanov (VMware) 
6267491e2c4STzvetomir Stoyanov (VMware) 	list_del_rcu(&trigger->list);
6277491e2c4STzvetomir Stoyanov (VMware) 
6287491e2c4STzvetomir Stoyanov (VMware) 	trace_event_trigger_enable_disable(file, 0);
6297491e2c4STzvetomir Stoyanov (VMware) 	update_cond_flag(file);
6306675880fSVamshi K Sthambamkadi 
6316675880fSVamshi K Sthambamkadi 	/* Make sure nothing is using the edata or trigger */
6326675880fSVamshi K Sthambamkadi 	tracepoint_synchronize_unregister();
6336675880fSVamshi K Sthambamkadi 
634752be5c5SMasami Hiramatsu (Google) 	filter = rcu_access_pointer(trigger->filter);
635752be5c5SMasami Hiramatsu (Google) 
636752be5c5SMasami Hiramatsu (Google) 	if (filter)
637752be5c5SMasami Hiramatsu (Google) 		free_event_filter(filter);
6386675880fSVamshi K Sthambamkadi 	kfree(edata);
6396675880fSVamshi K Sthambamkadi 	kfree(trigger);
6406675880fSVamshi K Sthambamkadi 
6417491e2c4STzvetomir Stoyanov (VMware) 	return 0;
6427491e2c4STzvetomir Stoyanov (VMware) }
6437491e2c4STzvetomir Stoyanov (VMware) 
enable_trace_eprobe(struct trace_event_call * call,struct trace_event_file * file)6447491e2c4STzvetomir Stoyanov (VMware) static int enable_trace_eprobe(struct trace_event_call *call,
6457491e2c4STzvetomir Stoyanov (VMware) 			       struct trace_event_file *file)
6467491e2c4STzvetomir Stoyanov (VMware) {
647f8bbf8b9SChuang Wang 	struct trace_probe *tp;
6487491e2c4STzvetomir Stoyanov (VMware) 	struct trace_eprobe *ep;
6497491e2c4STzvetomir Stoyanov (VMware) 	bool enabled;
6507491e2c4STzvetomir Stoyanov (VMware) 	int ret = 0;
651cf0a624dSTzvetomir Stoyanov (VMware) 	int cnt = 0;
6527491e2c4STzvetomir Stoyanov (VMware) 
6537491e2c4STzvetomir Stoyanov (VMware) 	tp = trace_probe_primary_from_call(call);
6547491e2c4STzvetomir Stoyanov (VMware) 	if (WARN_ON_ONCE(!tp))
6557491e2c4STzvetomir Stoyanov (VMware) 		return -ENODEV;
6567491e2c4STzvetomir Stoyanov (VMware) 	enabled = trace_probe_is_enabled(tp);
6577491e2c4STzvetomir Stoyanov (VMware) 
6587491e2c4STzvetomir Stoyanov (VMware) 	/* This also changes "enabled" state */
6597491e2c4STzvetomir Stoyanov (VMware) 	if (file) {
6607491e2c4STzvetomir Stoyanov (VMware) 		ret = trace_probe_add_file(tp, file);
6617491e2c4STzvetomir Stoyanov (VMware) 		if (ret)
6627491e2c4STzvetomir Stoyanov (VMware) 			return ret;
6637491e2c4STzvetomir Stoyanov (VMware) 	} else
6647491e2c4STzvetomir Stoyanov (VMware) 		trace_probe_set_flag(tp, TP_FLAG_PROFILE);
6657491e2c4STzvetomir Stoyanov (VMware) 
6667491e2c4STzvetomir Stoyanov (VMware) 	if (enabled)
6677491e2c4STzvetomir Stoyanov (VMware) 		return 0;
6687491e2c4STzvetomir Stoyanov (VMware) 
669f8bbf8b9SChuang Wang 	for_each_trace_eprobe_tp(ep, tp) {
6707491e2c4STzvetomir Stoyanov (VMware) 		ret = enable_eprobe(ep, file);
6717491e2c4STzvetomir Stoyanov (VMware) 		if (ret)
6727491e2c4STzvetomir Stoyanov (VMware) 			break;
6737491e2c4STzvetomir Stoyanov (VMware) 		enabled = true;
674cf0a624dSTzvetomir Stoyanov (VMware) 		cnt++;
6757491e2c4STzvetomir Stoyanov (VMware) 	}
6767491e2c4STzvetomir Stoyanov (VMware) 
6777491e2c4STzvetomir Stoyanov (VMware) 	if (ret) {
6787491e2c4STzvetomir Stoyanov (VMware) 		/* Failed to enable one of them. Roll back all */
679cf0a624dSTzvetomir Stoyanov (VMware) 		if (enabled) {
680cf0a624dSTzvetomir Stoyanov (VMware) 			/*
681cf0a624dSTzvetomir Stoyanov (VMware) 			 * It's a bug if one failed for something other than memory
682cf0a624dSTzvetomir Stoyanov (VMware) 			 * not being available but another eprobe succeeded.
683cf0a624dSTzvetomir Stoyanov (VMware) 			 */
684cf0a624dSTzvetomir Stoyanov (VMware) 			WARN_ON_ONCE(ret != -ENOMEM);
685cf0a624dSTzvetomir Stoyanov (VMware) 
686f8bbf8b9SChuang Wang 			for_each_trace_eprobe_tp(ep, tp) {
6877491e2c4STzvetomir Stoyanov (VMware) 				disable_eprobe(ep, file->tr);
688cf0a624dSTzvetomir Stoyanov (VMware) 				if (!--cnt)
689cf0a624dSTzvetomir Stoyanov (VMware) 					break;
690cf0a624dSTzvetomir Stoyanov (VMware) 			}
691cf0a624dSTzvetomir Stoyanov (VMware) 		}
6927491e2c4STzvetomir Stoyanov (VMware) 		if (file)
6937491e2c4STzvetomir Stoyanov (VMware) 			trace_probe_remove_file(tp, file);
6947491e2c4STzvetomir Stoyanov (VMware) 		else
6957491e2c4STzvetomir Stoyanov (VMware) 			trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
6967491e2c4STzvetomir Stoyanov (VMware) 	}
6977491e2c4STzvetomir Stoyanov (VMware) 
6987491e2c4STzvetomir Stoyanov (VMware) 	return ret;
6997491e2c4STzvetomir Stoyanov (VMware) }
7007491e2c4STzvetomir Stoyanov (VMware) 
disable_trace_eprobe(struct trace_event_call * call,struct trace_event_file * file)7017491e2c4STzvetomir Stoyanov (VMware) static int disable_trace_eprobe(struct trace_event_call *call,
7027491e2c4STzvetomir Stoyanov (VMware) 				struct trace_event_file *file)
7037491e2c4STzvetomir Stoyanov (VMware) {
704f8bbf8b9SChuang Wang 	struct trace_probe *tp;
7057491e2c4STzvetomir Stoyanov (VMware) 	struct trace_eprobe *ep;
7067491e2c4STzvetomir Stoyanov (VMware) 
7077491e2c4STzvetomir Stoyanov (VMware) 	tp = trace_probe_primary_from_call(call);
7087491e2c4STzvetomir Stoyanov (VMware) 	if (WARN_ON_ONCE(!tp))
7097491e2c4STzvetomir Stoyanov (VMware) 		return -ENODEV;
7107491e2c4STzvetomir Stoyanov (VMware) 
7117491e2c4STzvetomir Stoyanov (VMware) 	if (file) {
7127491e2c4STzvetomir Stoyanov (VMware) 		if (!trace_probe_get_file_link(tp, file))
7137491e2c4STzvetomir Stoyanov (VMware) 			return -ENOENT;
7147491e2c4STzvetomir Stoyanov (VMware) 		if (!trace_probe_has_single_file(tp))
7157491e2c4STzvetomir Stoyanov (VMware) 			goto out;
7167491e2c4STzvetomir Stoyanov (VMware) 		trace_probe_clear_flag(tp, TP_FLAG_TRACE);
7177491e2c4STzvetomir Stoyanov (VMware) 	} else
7187491e2c4STzvetomir Stoyanov (VMware) 		trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
7197491e2c4STzvetomir Stoyanov (VMware) 
7207491e2c4STzvetomir Stoyanov (VMware) 	if (!trace_probe_is_enabled(tp)) {
721f8bbf8b9SChuang Wang 		for_each_trace_eprobe_tp(ep, tp)
7227491e2c4STzvetomir Stoyanov (VMware) 			disable_eprobe(ep, file->tr);
7237491e2c4STzvetomir Stoyanov (VMware) 	}
7247491e2c4STzvetomir Stoyanov (VMware) 
7257491e2c4STzvetomir Stoyanov (VMware)  out:
7267491e2c4STzvetomir Stoyanov (VMware) 	if (file)
7277491e2c4STzvetomir Stoyanov (VMware) 		/*
7287491e2c4STzvetomir Stoyanov (VMware) 		 * Synchronization is done in below function. For perf event,
7297491e2c4STzvetomir Stoyanov (VMware) 		 * file == NULL and perf_trace_event_unreg() calls
7307491e2c4STzvetomir Stoyanov (VMware) 		 * tracepoint_synchronize_unregister() to ensure synchronize
7317491e2c4STzvetomir Stoyanov (VMware) 		 * event. We don't need to care about it.
7327491e2c4STzvetomir Stoyanov (VMware) 		 */
7337491e2c4STzvetomir Stoyanov (VMware) 		trace_probe_remove_file(tp, file);
7347491e2c4STzvetomir Stoyanov (VMware) 
7357491e2c4STzvetomir Stoyanov (VMware) 	return 0;
7367491e2c4STzvetomir Stoyanov (VMware) }
7377491e2c4STzvetomir Stoyanov (VMware) 
eprobe_register(struct trace_event_call * event,enum trace_reg type,void * data)7387491e2c4STzvetomir Stoyanov (VMware) static int eprobe_register(struct trace_event_call *event,
7397491e2c4STzvetomir Stoyanov (VMware) 			   enum trace_reg type, void *data)
7407491e2c4STzvetomir Stoyanov (VMware) {
7417491e2c4STzvetomir Stoyanov (VMware) 	struct trace_event_file *file = data;
7427491e2c4STzvetomir Stoyanov (VMware) 
7437491e2c4STzvetomir Stoyanov (VMware) 	switch (type) {
7447491e2c4STzvetomir Stoyanov (VMware) 	case TRACE_REG_REGISTER:
7457491e2c4STzvetomir Stoyanov (VMware) 		return enable_trace_eprobe(event, file);
7467491e2c4STzvetomir Stoyanov (VMware) 	case TRACE_REG_UNREGISTER:
7477491e2c4STzvetomir Stoyanov (VMware) 		return disable_trace_eprobe(event, file);
7487491e2c4STzvetomir Stoyanov (VMware) #ifdef CONFIG_PERF_EVENTS
7497491e2c4STzvetomir Stoyanov (VMware) 	case TRACE_REG_PERF_REGISTER:
7507491e2c4STzvetomir Stoyanov (VMware) 	case TRACE_REG_PERF_UNREGISTER:
7517491e2c4STzvetomir Stoyanov (VMware) 	case TRACE_REG_PERF_OPEN:
7527491e2c4STzvetomir Stoyanov (VMware) 	case TRACE_REG_PERF_CLOSE:
7537491e2c4STzvetomir Stoyanov (VMware) 	case TRACE_REG_PERF_ADD:
7547491e2c4STzvetomir Stoyanov (VMware) 	case TRACE_REG_PERF_DEL:
7557491e2c4STzvetomir Stoyanov (VMware) 		return 0;
7567491e2c4STzvetomir Stoyanov (VMware) #endif
7577491e2c4STzvetomir Stoyanov (VMware) 	}
7587491e2c4STzvetomir Stoyanov (VMware) 	return 0;
7597491e2c4STzvetomir Stoyanov (VMware) }
7607491e2c4STzvetomir Stoyanov (VMware) 
init_trace_eprobe_call(struct trace_eprobe * ep)7617491e2c4STzvetomir Stoyanov (VMware) static inline void init_trace_eprobe_call(struct trace_eprobe *ep)
7627491e2c4STzvetomir Stoyanov (VMware) {
7637491e2c4STzvetomir Stoyanov (VMware) 	struct trace_event_call *call = trace_probe_event_call(&ep->tp);
7647491e2c4STzvetomir Stoyanov (VMware) 
7657491e2c4STzvetomir Stoyanov (VMware) 	call->flags = TRACE_EVENT_FL_EPROBE;
7667491e2c4STzvetomir Stoyanov (VMware) 	call->event.funcs = &eprobe_funcs;
7677491e2c4STzvetomir Stoyanov (VMware) 	call->class->fields_array = eprobe_fields_array;
7687491e2c4STzvetomir Stoyanov (VMware) 	call->class->reg = eprobe_register;
7697491e2c4STzvetomir Stoyanov (VMware) }
7707491e2c4STzvetomir Stoyanov (VMware) 
7717491e2c4STzvetomir Stoyanov (VMware) static struct trace_event_call *
find_and_get_event(const char * system,const char * event_name)7727491e2c4STzvetomir Stoyanov (VMware) find_and_get_event(const char *system, const char *event_name)
7737491e2c4STzvetomir Stoyanov (VMware) {
7747491e2c4STzvetomir Stoyanov (VMware) 	struct trace_event_call *tp_event;
7757491e2c4STzvetomir Stoyanov (VMware) 	const char *name;
7767491e2c4STzvetomir Stoyanov (VMware) 
7777491e2c4STzvetomir Stoyanov (VMware) 	list_for_each_entry(tp_event, &ftrace_events, list) {
7787491e2c4STzvetomir Stoyanov (VMware) 		/* Skip other probes and ftrace events */
7797491e2c4STzvetomir Stoyanov (VMware) 		if (tp_event->flags &
7807491e2c4STzvetomir Stoyanov (VMware) 		    (TRACE_EVENT_FL_IGNORE_ENABLE |
7817491e2c4STzvetomir Stoyanov (VMware) 		     TRACE_EVENT_FL_KPROBE |
7827491e2c4STzvetomir Stoyanov (VMware) 		     TRACE_EVENT_FL_UPROBE |
7837491e2c4STzvetomir Stoyanov (VMware) 		     TRACE_EVENT_FL_EPROBE))
7847491e2c4STzvetomir Stoyanov (VMware) 			continue;
7857491e2c4STzvetomir Stoyanov (VMware) 		if (!tp_event->class->system ||
7867491e2c4STzvetomir Stoyanov (VMware) 		    strcmp(system, tp_event->class->system))
7877491e2c4STzvetomir Stoyanov (VMware) 			continue;
7887491e2c4STzvetomir Stoyanov (VMware) 		name = trace_event_name(tp_event);
7897491e2c4STzvetomir Stoyanov (VMware) 		if (!name || strcmp(event_name, name))
7907491e2c4STzvetomir Stoyanov (VMware) 			continue;
791f843249cSJulia Lawall 		if (!trace_event_try_get_ref(tp_event))
7927491e2c4STzvetomir Stoyanov (VMware) 			return NULL;
7937491e2c4STzvetomir Stoyanov (VMware) 		return tp_event;
7947491e2c4STzvetomir Stoyanov (VMware) 	}
7957491e2c4STzvetomir Stoyanov (VMware) 	return NULL;
7967491e2c4STzvetomir Stoyanov (VMware) }
7977491e2c4STzvetomir Stoyanov (VMware) 
trace_eprobe_tp_update_arg(struct trace_eprobe * ep,const char * argv[],int i)7987491e2c4STzvetomir Stoyanov (VMware) static int trace_eprobe_tp_update_arg(struct trace_eprobe *ep, const char *argv[], int i)
7997491e2c4STzvetomir Stoyanov (VMware) {
8001b8b0cd7SMasami Hiramatsu (Google) 	struct traceprobe_parse_context ctx = {
8011b8b0cd7SMasami Hiramatsu (Google) 		.event = ep->event,
8021b8b0cd7SMasami Hiramatsu (Google) 		.flags = TPARG_FL_KERNEL | TPARG_FL_TEVENT,
8031b8b0cd7SMasami Hiramatsu (Google) 	};
8047491e2c4STzvetomir Stoyanov (VMware) 	int ret;
8057491e2c4STzvetomir Stoyanov (VMware) 
8061b8b0cd7SMasami Hiramatsu (Google) 	ret = traceprobe_parse_probe_arg(&ep->tp, i, argv[i], &ctx);
8076a832ec3SSteven Rostedt (Google) 	/* Handle symbols "@" */
8086a832ec3SSteven Rostedt (Google) 	if (!ret)
8096a832ec3SSteven Rostedt (Google) 		ret = traceprobe_update_arg(&ep->tp.args[i]);
8106a832ec3SSteven Rostedt (Google) 
811b1d1e904SMasami Hiramatsu (Google) 	traceprobe_finish_parse(&ctx);
8127491e2c4STzvetomir Stoyanov (VMware) 	return ret;
8137491e2c4STzvetomir Stoyanov (VMware) }
8147491e2c4STzvetomir Stoyanov (VMware) 
trace_eprobe_parse_filter(struct trace_eprobe * ep,int argc,const char * argv[])815752be5c5SMasami Hiramatsu (Google) static int trace_eprobe_parse_filter(struct trace_eprobe *ep, int argc, const char *argv[])
816752be5c5SMasami Hiramatsu (Google) {
817342a4a2fSRafael Mendonca 	struct event_filter *dummy = NULL;
818752be5c5SMasami Hiramatsu (Google) 	int i, ret, len = 0;
819752be5c5SMasami Hiramatsu (Google) 	char *p;
820752be5c5SMasami Hiramatsu (Google) 
821752be5c5SMasami Hiramatsu (Google) 	if (argc == 0) {
822752be5c5SMasami Hiramatsu (Google) 		trace_probe_log_err(0, NO_EP_FILTER);
823752be5c5SMasami Hiramatsu (Google) 		return -EINVAL;
824752be5c5SMasami Hiramatsu (Google) 	}
825752be5c5SMasami Hiramatsu (Google) 
826752be5c5SMasami Hiramatsu (Google) 	/* Recover the filter string */
827752be5c5SMasami Hiramatsu (Google) 	for (i = 0; i < argc; i++)
828752be5c5SMasami Hiramatsu (Google) 		len += strlen(argv[i]) + 1;
829752be5c5SMasami Hiramatsu (Google) 
830752be5c5SMasami Hiramatsu (Google) 	ep->filter_str = kzalloc(len, GFP_KERNEL);
831752be5c5SMasami Hiramatsu (Google) 	if (!ep->filter_str)
832752be5c5SMasami Hiramatsu (Google) 		return -ENOMEM;
833752be5c5SMasami Hiramatsu (Google) 
834752be5c5SMasami Hiramatsu (Google) 	p = ep->filter_str;
835752be5c5SMasami Hiramatsu (Google) 	for (i = 0; i < argc; i++) {
836c96abaecSQuanfa Fu 		if (i)
837752be5c5SMasami Hiramatsu (Google) 			ret = snprintf(p, len, " %s", argv[i]);
838c96abaecSQuanfa Fu 		else
839c96abaecSQuanfa Fu 			ret = snprintf(p, len, "%s", argv[i]);
840752be5c5SMasami Hiramatsu (Google) 		p += ret;
841752be5c5SMasami Hiramatsu (Google) 		len -= ret;
842752be5c5SMasami Hiramatsu (Google) 	}
843752be5c5SMasami Hiramatsu (Google) 
844752be5c5SMasami Hiramatsu (Google) 	/*
845752be5c5SMasami Hiramatsu (Google) 	 * Ensure the filter string can be parsed correctly. Note, this
846752be5c5SMasami Hiramatsu (Google) 	 * filter string is for the original event, not for the eprobe.
847752be5c5SMasami Hiramatsu (Google) 	 */
848752be5c5SMasami Hiramatsu (Google) 	ret = create_event_filter(top_trace_array(), ep->event, ep->filter_str,
849752be5c5SMasami Hiramatsu (Google) 				  true, &dummy);
850752be5c5SMasami Hiramatsu (Google) 	free_event_filter(dummy);
851752be5c5SMasami Hiramatsu (Google) 	if (ret)
852752be5c5SMasami Hiramatsu (Google) 		goto error;
853752be5c5SMasami Hiramatsu (Google) 
854752be5c5SMasami Hiramatsu (Google) 	return 0;
855752be5c5SMasami Hiramatsu (Google) error:
856752be5c5SMasami Hiramatsu (Google) 	kfree(ep->filter_str);
857752be5c5SMasami Hiramatsu (Google) 	ep->filter_str = NULL;
858752be5c5SMasami Hiramatsu (Google) 	return ret;
859752be5c5SMasami Hiramatsu (Google) }
860752be5c5SMasami Hiramatsu (Google) 
__trace_eprobe_create(int argc,const char * argv[])8617491e2c4STzvetomir Stoyanov (VMware) static int __trace_eprobe_create(int argc, const char *argv[])
8627491e2c4STzvetomir Stoyanov (VMware) {
8637491e2c4STzvetomir Stoyanov (VMware) 	/*
8647491e2c4STzvetomir Stoyanov (VMware) 	 * Argument syntax:
865752be5c5SMasami Hiramatsu (Google) 	 *      e[:[GRP/][ENAME]] SYSTEM.EVENT [FETCHARGS] [if FILTER]
866752be5c5SMasami Hiramatsu (Google) 	 * Fetch args (no space):
8677491e2c4STzvetomir Stoyanov (VMware) 	 *  <name>=$<field>[:TYPE]
8687491e2c4STzvetomir Stoyanov (VMware) 	 */
8697491e2c4STzvetomir Stoyanov (VMware) 	const char *event = NULL, *group = EPROBE_EVENT_SYSTEM;
8707491e2c4STzvetomir Stoyanov (VMware) 	const char *sys_event = NULL, *sys_name = NULL;
8717491e2c4STzvetomir Stoyanov (VMware) 	struct trace_event_call *event_call;
8727491e2c4STzvetomir Stoyanov (VMware) 	struct trace_eprobe *ep = NULL;
8737491e2c4STzvetomir Stoyanov (VMware) 	char buf1[MAX_EVENT_NAME_LEN];
8747491e2c4STzvetomir Stoyanov (VMware) 	char buf2[MAX_EVENT_NAME_LEN];
87595c104c3SLinyu Yuan 	char gbuf[MAX_EVENT_NAME_LEN];
876752be5c5SMasami Hiramatsu (Google) 	int ret = 0, filter_idx = 0;
877752be5c5SMasami Hiramatsu (Google) 	int i, filter_cnt;
8787491e2c4STzvetomir Stoyanov (VMware) 
8797491e2c4STzvetomir Stoyanov (VMware) 	if (argc < 2 || argv[0][0] != 'e')
8807491e2c4STzvetomir Stoyanov (VMware) 		return -ECANCELED;
8817491e2c4STzvetomir Stoyanov (VMware) 
8827491e2c4STzvetomir Stoyanov (VMware) 	trace_probe_log_init("event_probe", argc, argv);
8837491e2c4STzvetomir Stoyanov (VMware) 
8847491e2c4STzvetomir Stoyanov (VMware) 	event = strchr(&argv[0][1], ':');
8857491e2c4STzvetomir Stoyanov (VMware) 	if (event) {
8867491e2c4STzvetomir Stoyanov (VMware) 		event++;
88795c104c3SLinyu Yuan 		ret = traceprobe_parse_event_name(&event, &group, gbuf,
8887491e2c4STzvetomir Stoyanov (VMware) 						  event - argv[0]);
8897491e2c4STzvetomir Stoyanov (VMware) 		if (ret)
8907491e2c4STzvetomir Stoyanov (VMware) 			goto parse_error;
89195c104c3SLinyu Yuan 	}
89295c104c3SLinyu Yuan 
89395c104c3SLinyu Yuan 	trace_probe_log_set_index(1);
89495c104c3SLinyu Yuan 	sys_event = argv[1];
89595c104c3SLinyu Yuan 	ret = traceprobe_parse_event_name(&sys_event, &sys_name, buf2, 0);
896d8a64313SLukas Bulwahn 	if (ret || !sys_event || !sys_name) {
8972f63e5d2SMasami Hiramatsu (Google) 		trace_probe_log_err(0, NO_EVENT_INFO);
89895c104c3SLinyu Yuan 		goto parse_error;
8992f63e5d2SMasami Hiramatsu (Google) 	}
90095c104c3SLinyu Yuan 
90195c104c3SLinyu Yuan 	if (!event) {
902dc399adeSTao Chen 		strscpy(buf1, sys_event, MAX_EVENT_NAME_LEN);
9037491e2c4STzvetomir Stoyanov (VMware) 		event = buf1;
9047491e2c4STzvetomir Stoyanov (VMware) 	}
9057491e2c4STzvetomir Stoyanov (VMware) 
906752be5c5SMasami Hiramatsu (Google) 	for (i = 2; i < argc; i++) {
907752be5c5SMasami Hiramatsu (Google) 		if (!strcmp(argv[i], "if")) {
908752be5c5SMasami Hiramatsu (Google) 			filter_idx = i + 1;
909752be5c5SMasami Hiramatsu (Google) 			filter_cnt = argc - filter_idx;
910752be5c5SMasami Hiramatsu (Google) 			argc = i;
911752be5c5SMasami Hiramatsu (Google) 			break;
912752be5c5SMasami Hiramatsu (Google) 		}
913752be5c5SMasami Hiramatsu (Google) 	}
914752be5c5SMasami Hiramatsu (Google) 
91573f35080SMikel Rychliski 	if (argc - 2 > MAX_TRACE_ARGS) {
91657faaa04SMasami Hiramatsu (Google) 		trace_probe_log_set_index(2);
91757faaa04SMasami Hiramatsu (Google) 		trace_probe_log_err(0, TOO_MANY_ARGS);
91873f35080SMikel Rychliski 		ret = -E2BIG;
91973f35080SMikel Rychliski 		goto error;
92073f35080SMikel Rychliski 	}
92173f35080SMikel Rychliski 
9224e83017eSMasami Hiramatsu (Google) 	scoped_guard(mutex, &event_mutex) {
9237491e2c4STzvetomir Stoyanov (VMware) 		event_call = find_and_get_event(sys_name, sys_event);
9247491e2c4STzvetomir Stoyanov (VMware) 		ep = alloc_event_probe(group, event, event_call, argc - 2);
9254e83017eSMasami Hiramatsu (Google) 	}
9267491e2c4STzvetomir Stoyanov (VMware) 
9277491e2c4STzvetomir Stoyanov (VMware) 	if (IS_ERR(ep)) {
9287491e2c4STzvetomir Stoyanov (VMware) 		ret = PTR_ERR(ep);
9292f63e5d2SMasami Hiramatsu (Google) 		if (ret == -ENODEV)
9302f63e5d2SMasami Hiramatsu (Google) 			trace_probe_log_err(0, BAD_ATTACH_EVENT);
931ddcf906fSSteven Rostedt (VMware) 		/* This must return -ENOMEM or missing event, else there is a bug */
9327fa598f9SSteven Rostedt (VMware) 		WARN_ON_ONCE(ret != -ENOMEM && ret != -ENODEV);
9335615e088SDan Carpenter 		ep = NULL;
9345615e088SDan Carpenter 		goto error;
9357491e2c4STzvetomir Stoyanov (VMware) 	}
9367491e2c4STzvetomir Stoyanov (VMware) 
937752be5c5SMasami Hiramatsu (Google) 	if (filter_idx) {
938752be5c5SMasami Hiramatsu (Google) 		trace_probe_log_set_index(filter_idx);
939752be5c5SMasami Hiramatsu (Google) 		ret = trace_eprobe_parse_filter(ep, filter_cnt, argv + filter_idx);
940752be5c5SMasami Hiramatsu (Google) 		if (ret)
941752be5c5SMasami Hiramatsu (Google) 			goto parse_error;
942752be5c5SMasami Hiramatsu (Google) 	} else
943752be5c5SMasami Hiramatsu (Google) 		ep->filter_str = NULL;
944752be5c5SMasami Hiramatsu (Google) 
9457491e2c4STzvetomir Stoyanov (VMware) 	argc -= 2; argv += 2;
9467491e2c4STzvetomir Stoyanov (VMware) 	/* parse arguments */
94773f35080SMikel Rychliski 	for (i = 0; i < argc; i++) {
9487491e2c4STzvetomir Stoyanov (VMware) 		trace_probe_log_set_index(i + 2);
9497491e2c4STzvetomir Stoyanov (VMware) 		ret = trace_eprobe_tp_update_arg(ep, argv, i);
9507491e2c4STzvetomir Stoyanov (VMware) 		if (ret)
9517491e2c4STzvetomir Stoyanov (VMware) 			goto error;
9527491e2c4STzvetomir Stoyanov (VMware) 	}
9537491e2c4STzvetomir Stoyanov (VMware) 	ret = traceprobe_set_print_fmt(&ep->tp, PROBE_PRINT_EVENT);
9547491e2c4STzvetomir Stoyanov (VMware) 	if (ret < 0)
9557491e2c4STzvetomir Stoyanov (VMware) 		goto error;
9567491e2c4STzvetomir Stoyanov (VMware) 	init_trace_eprobe_call(ep);
9574e83017eSMasami Hiramatsu (Google) 	scoped_guard(mutex, &event_mutex) {
9587491e2c4STzvetomir Stoyanov (VMware) 		ret = trace_probe_register_event_call(&ep->tp);
9597491e2c4STzvetomir Stoyanov (VMware) 		if (ret) {
9607491e2c4STzvetomir Stoyanov (VMware) 			if (ret == -EEXIST) {
9617491e2c4STzvetomir Stoyanov (VMware) 				trace_probe_log_set_index(0);
9627491e2c4STzvetomir Stoyanov (VMware) 				trace_probe_log_err(0, EVENT_EXIST);
9637491e2c4STzvetomir Stoyanov (VMware) 			}
9647491e2c4STzvetomir Stoyanov (VMware) 			goto error;
9657491e2c4STzvetomir Stoyanov (VMware) 		}
9667491e2c4STzvetomir Stoyanov (VMware) 		ret = dyn_event_add(&ep->devent, &ep->tp.event->call);
967494b3320SMasami Hiramatsu (Google) 		if (ret < 0) {
968494b3320SMasami Hiramatsu (Google) 			trace_probe_unregister_event_call(&ep->tp);
969494b3320SMasami Hiramatsu (Google) 			goto error;
970494b3320SMasami Hiramatsu (Google) 		}
9714e83017eSMasami Hiramatsu (Google) 	}
972*e41b5af4SPaul Cacheux 	trace_probe_log_clear();
9737491e2c4STzvetomir Stoyanov (VMware) 	return ret;
974*e41b5af4SPaul Cacheux 
9757491e2c4STzvetomir Stoyanov (VMware) parse_error:
9767491e2c4STzvetomir Stoyanov (VMware) 	ret = -EINVAL;
9777491e2c4STzvetomir Stoyanov (VMware) error:
978*e41b5af4SPaul Cacheux 	trace_probe_log_clear();
9797491e2c4STzvetomir Stoyanov (VMware) 	trace_event_probe_cleanup(ep);
9807491e2c4STzvetomir Stoyanov (VMware) 	return ret;
9817491e2c4STzvetomir Stoyanov (VMware) }
9827491e2c4STzvetomir Stoyanov (VMware) 
9837491e2c4STzvetomir Stoyanov (VMware) /*
9847491e2c4STzvetomir Stoyanov (VMware)  * Register dynevent at core_initcall. This allows kernel to setup eprobe
9857491e2c4STzvetomir Stoyanov (VMware)  * events in postcore_initcall without tracefs.
9867491e2c4STzvetomir Stoyanov (VMware)  */
trace_events_eprobe_init_early(void)9877491e2c4STzvetomir Stoyanov (VMware) static __init int trace_events_eprobe_init_early(void)
9887491e2c4STzvetomir Stoyanov (VMware) {
9897491e2c4STzvetomir Stoyanov (VMware) 	int err = 0;
9907491e2c4STzvetomir Stoyanov (VMware) 
9917491e2c4STzvetomir Stoyanov (VMware) 	err = dyn_event_register(&eprobe_dyn_event_ops);
9927491e2c4STzvetomir Stoyanov (VMware) 	if (err)
9937491e2c4STzvetomir Stoyanov (VMware) 		pr_warn("Could not register eprobe_dyn_event_ops\n");
9947491e2c4STzvetomir Stoyanov (VMware) 
9957491e2c4STzvetomir Stoyanov (VMware) 	return err;
9967491e2c4STzvetomir Stoyanov (VMware) }
9977491e2c4STzvetomir Stoyanov (VMware) core_initcall(trace_events_eprobe_init_early);
998