xref: /linux-6.15/kernel/trace/trace_probe.c (revision fd837de3)
1bcea3f96SSteven Rostedt (VMware) // SPDX-License-Identifier: GPL-2.0
28ab83f56SSrikar Dronamraju /*
38ab83f56SSrikar Dronamraju  * Common code for probe-based Dynamic events.
48ab83f56SSrikar Dronamraju  *
58ab83f56SSrikar Dronamraju  * This code was copied from kernel/trace/trace_kprobe.c written by
68ab83f56SSrikar Dronamraju  * Masami Hiramatsu <[email protected]>
78ab83f56SSrikar Dronamraju  *
88ab83f56SSrikar Dronamraju  * Updates to make this generic:
98ab83f56SSrikar Dronamraju  * Copyright (C) IBM Corporation, 2010-2011
108ab83f56SSrikar Dronamraju  * Author:     Srikar Dronamraju
118ab83f56SSrikar Dronamraju  */
1272576341SMasami Hiramatsu #define pr_fmt(fmt)	"trace_probe: " fmt
138ab83f56SSrikar Dronamraju 
14b576e097SMasami Hiramatsu (Google) #include <linux/bpf.h>
1520fe4d07SYe Bin #include <linux/fs.h>
16ebeed8d4SMasami Hiramatsu (Google) #include "trace_btf.h"
17b576e097SMasami Hiramatsu (Google) 
188ab83f56SSrikar Dronamraju #include "trace_probe.h"
198ab83f56SSrikar Dronamraju 
20ab105a4fSMasami Hiramatsu #undef C
21ab105a4fSMasami Hiramatsu #define C(a, b)		b
22ab105a4fSMasami Hiramatsu 
23ab105a4fSMasami Hiramatsu static const char *trace_probe_err_text[] = { ERRORS };
24ab105a4fSMasami Hiramatsu 
2508416252SValdis Klētnieks static const char *reserved_field_names[] = {
268ab83f56SSrikar Dronamraju 	"common_type",
278ab83f56SSrikar Dronamraju 	"common_flags",
288ab83f56SSrikar Dronamraju 	"common_preempt_count",
298ab83f56SSrikar Dronamraju 	"common_pid",
308ab83f56SSrikar Dronamraju 	"common_tgid",
318ab83f56SSrikar Dronamraju 	FIELD_STRING_IP,
328ab83f56SSrikar Dronamraju 	FIELD_STRING_RETIP,
338ab83f56SSrikar Dronamraju 	FIELD_STRING_FUNC,
348ab83f56SSrikar Dronamraju };
358ab83f56SSrikar Dronamraju 
368ab83f56SSrikar Dronamraju /* Printing  in basic type function template */
3717ce3dc7SMasami Hiramatsu #define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt)			\
3856de7630SMasami Hiramatsu int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, void *data, void *ent)\
398ab83f56SSrikar Dronamraju {									\
4056de7630SMasami Hiramatsu 	trace_seq_printf(s, fmt, *(type *)data);			\
41d2b0191aSSteven Rostedt (Red Hat) 	return !trace_seq_has_overflowed(s);				\
428ab83f56SSrikar Dronamraju }									\
437bfbc63eSMasami Hiramatsu const char PRINT_TYPE_FMT_NAME(tname)[] = fmt;
448ab83f56SSrikar Dronamraju 
45bdca79c2SMasami Hiramatsu DEFINE_BASIC_PRINT_TYPE_FUNC(u8,  u8,  "%u")
46bdca79c2SMasami Hiramatsu DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u")
47bdca79c2SMasami Hiramatsu DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u")
48bdca79c2SMasami Hiramatsu DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu")
4917ce3dc7SMasami Hiramatsu DEFINE_BASIC_PRINT_TYPE_FUNC(s8,  s8,  "%d")
5017ce3dc7SMasami Hiramatsu DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d")
5117ce3dc7SMasami Hiramatsu DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d")
5217ce3dc7SMasami Hiramatsu DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld")
5317ce3dc7SMasami Hiramatsu DEFINE_BASIC_PRINT_TYPE_FUNC(x8,  u8,  "0x%x")
5417ce3dc7SMasami Hiramatsu DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x")
5517ce3dc7SMasami Hiramatsu DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x")
5617ce3dc7SMasami Hiramatsu DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx")
578478cca1SDonglin Peng DEFINE_BASIC_PRINT_TYPE_FUNC(char, u8, "'%c'")
588ab83f56SSrikar Dronamraju 
PRINT_TYPE_FUNC_NAME(symbol)5960c2e0ceSMasami Hiramatsu int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent)
6060c2e0ceSMasami Hiramatsu {
6160c2e0ceSMasami Hiramatsu 	trace_seq_printf(s, "%pS", (void *)*(unsigned long *)data);
6260c2e0ceSMasami Hiramatsu 	return !trace_seq_has_overflowed(s);
6360c2e0ceSMasami Hiramatsu }
6460c2e0ceSMasami Hiramatsu const char PRINT_TYPE_FMT_NAME(symbol)[] = "%pS";
6560c2e0ceSMasami Hiramatsu 
668ab83f56SSrikar Dronamraju /* Print type function for string type */
PRINT_TYPE_FUNC_NAME(string)6756de7630SMasami Hiramatsu int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, void *data, void *ent)
688ab83f56SSrikar Dronamraju {
698ab83f56SSrikar Dronamraju 	int len = *(u32 *)data >> 16;
708ab83f56SSrikar Dronamraju 
718ab83f56SSrikar Dronamraju 	if (!len)
724ed8f337SMasami Hiramatsu (Google) 		trace_seq_puts(s, FAULT_STRING);
738ab83f56SSrikar Dronamraju 	else
7456de7630SMasami Hiramatsu 		trace_seq_printf(s, "\"%s\"",
758ab83f56SSrikar Dronamraju 				 (const char *)get_loc_data(data, ent));
76d2b0191aSSteven Rostedt (Red Hat) 	return !trace_seq_has_overflowed(s);
778ab83f56SSrikar Dronamraju }
788ab83f56SSrikar Dronamraju 
79b26c74e1SNamhyung Kim const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
808ab83f56SSrikar Dronamraju 
81f451bc89SMasami Hiramatsu /* Fetch type information table */
82f451bc89SMasami Hiramatsu static const struct fetch_type probe_fetch_types[] = {
83f451bc89SMasami Hiramatsu 	/* Special types */
84b26a124cSMasami Hiramatsu (Google) 	__ASSIGN_FETCH_TYPE("string", string, string, sizeof(u32), 1, 1,
85f451bc89SMasami Hiramatsu 			    "__data_loc char[]"),
86b26a124cSMasami Hiramatsu (Google) 	__ASSIGN_FETCH_TYPE("ustring", string, string, sizeof(u32), 1, 1,
87b26a124cSMasami Hiramatsu (Google) 			    "__data_loc char[]"),
88b26a124cSMasami Hiramatsu (Google) 	__ASSIGN_FETCH_TYPE("symstr", string, string, sizeof(u32), 1, 1,
8988903c46SMasami Hiramatsu 			    "__data_loc char[]"),
90f451bc89SMasami Hiramatsu 	/* Basic types */
91f451bc89SMasami Hiramatsu 	ASSIGN_FETCH_TYPE(u8,  u8,  0),
92f451bc89SMasami Hiramatsu 	ASSIGN_FETCH_TYPE(u16, u16, 0),
93f451bc89SMasami Hiramatsu 	ASSIGN_FETCH_TYPE(u32, u32, 0),
94f451bc89SMasami Hiramatsu 	ASSIGN_FETCH_TYPE(u64, u64, 0),
95f451bc89SMasami Hiramatsu 	ASSIGN_FETCH_TYPE(s8,  u8,  1),
96f451bc89SMasami Hiramatsu 	ASSIGN_FETCH_TYPE(s16, u16, 1),
97f451bc89SMasami Hiramatsu 	ASSIGN_FETCH_TYPE(s32, u32, 1),
98f451bc89SMasami Hiramatsu 	ASSIGN_FETCH_TYPE(s64, u64, 1),
99f451bc89SMasami Hiramatsu 	ASSIGN_FETCH_TYPE_ALIAS(x8,  u8,  u8,  0),
100f451bc89SMasami Hiramatsu 	ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
101f451bc89SMasami Hiramatsu 	ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
102f451bc89SMasami Hiramatsu 	ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
1038478cca1SDonglin Peng 	ASSIGN_FETCH_TYPE_ALIAS(char, u8, u8,  0),
10460c2e0ceSMasami Hiramatsu 	ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0),
105f451bc89SMasami Hiramatsu 
106f451bc89SMasami Hiramatsu 	ASSIGN_FETCH_TYPE_END
107f451bc89SMasami Hiramatsu };
108f451bc89SMasami Hiramatsu 
find_fetch_type(const char * type,unsigned long flags)109d4505aa6SMasami Hiramatsu (Google) static const struct fetch_type *find_fetch_type(const char *type, unsigned long flags)
1108ab83f56SSrikar Dronamraju {
1118ab83f56SSrikar Dronamraju 	int i;
1128ab83f56SSrikar Dronamraju 
113d4505aa6SMasami Hiramatsu (Google) 	/* Reject the symbol/symstr for uprobes */
114d4505aa6SMasami Hiramatsu (Google) 	if (type && (flags & TPARG_FL_USER) &&
115d4505aa6SMasami Hiramatsu (Google) 	    (!strcmp(type, "symbol") || !strcmp(type, "symstr")))
116d4505aa6SMasami Hiramatsu (Google) 		return NULL;
117d4505aa6SMasami Hiramatsu (Google) 
1188ab83f56SSrikar Dronamraju 	if (!type)
1198ab83f56SSrikar Dronamraju 		type = DEFAULT_FETCH_TYPE_STR;
1208ab83f56SSrikar Dronamraju 
1218ab83f56SSrikar Dronamraju 	/* Special case: bitfield */
1228ab83f56SSrikar Dronamraju 	if (*type == 'b') {
1238ab83f56SSrikar Dronamraju 		unsigned long bs;
1248ab83f56SSrikar Dronamraju 
1258ab83f56SSrikar Dronamraju 		type = strchr(type, '/');
1268ab83f56SSrikar Dronamraju 		if (!type)
1278ab83f56SSrikar Dronamraju 			goto fail;
1288ab83f56SSrikar Dronamraju 
1298ab83f56SSrikar Dronamraju 		type++;
130bcd83ea6SDaniel Walter 		if (kstrtoul(type, 0, &bs))
1318ab83f56SSrikar Dronamraju 			goto fail;
1328ab83f56SSrikar Dronamraju 
1338ab83f56SSrikar Dronamraju 		switch (bs) {
1348ab83f56SSrikar Dronamraju 		case 8:
135d4505aa6SMasami Hiramatsu (Google) 			return find_fetch_type("u8", flags);
1368ab83f56SSrikar Dronamraju 		case 16:
137d4505aa6SMasami Hiramatsu (Google) 			return find_fetch_type("u16", flags);
1388ab83f56SSrikar Dronamraju 		case 32:
139d4505aa6SMasami Hiramatsu (Google) 			return find_fetch_type("u32", flags);
1408ab83f56SSrikar Dronamraju 		case 64:
141d4505aa6SMasami Hiramatsu (Google) 			return find_fetch_type("u64", flags);
1428ab83f56SSrikar Dronamraju 		default:
1438ab83f56SSrikar Dronamraju 			goto fail;
1448ab83f56SSrikar Dronamraju 		}
1458ab83f56SSrikar Dronamraju 	}
1468ab83f56SSrikar Dronamraju 
147f451bc89SMasami Hiramatsu 	for (i = 0; probe_fetch_types[i].name; i++) {
148f451bc89SMasami Hiramatsu 		if (strcmp(type, probe_fetch_types[i].name) == 0)
149f451bc89SMasami Hiramatsu 			return &probe_fetch_types[i];
15034fee3a1SNamhyung Kim 	}
1518ab83f56SSrikar Dronamraju 
1528ab83f56SSrikar Dronamraju fail:
1538ab83f56SSrikar Dronamraju 	return NULL;
1548ab83f56SSrikar Dronamraju }
1558ab83f56SSrikar Dronamraju 
156ab105a4fSMasami Hiramatsu static struct trace_probe_log trace_probe_log;
157*fd837de3SMasami Hiramatsu (Google) extern struct mutex dyn_event_ops_mutex;
158ab105a4fSMasami Hiramatsu 
trace_probe_log_init(const char * subsystem,int argc,const char ** argv)159ab105a4fSMasami Hiramatsu void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
160ab105a4fSMasami Hiramatsu {
161*fd837de3SMasami Hiramatsu (Google) 	lockdep_assert_held(&dyn_event_ops_mutex);
162*fd837de3SMasami Hiramatsu (Google) 
163ab105a4fSMasami Hiramatsu 	trace_probe_log.subsystem = subsystem;
164ab105a4fSMasami Hiramatsu 	trace_probe_log.argc = argc;
165ab105a4fSMasami Hiramatsu 	trace_probe_log.argv = argv;
166ab105a4fSMasami Hiramatsu 	trace_probe_log.index = 0;
167ab105a4fSMasami Hiramatsu }
168ab105a4fSMasami Hiramatsu 
trace_probe_log_clear(void)169ab105a4fSMasami Hiramatsu void trace_probe_log_clear(void)
170ab105a4fSMasami Hiramatsu {
171*fd837de3SMasami Hiramatsu (Google) 	lockdep_assert_held(&dyn_event_ops_mutex);
172*fd837de3SMasami Hiramatsu (Google) 
173ab105a4fSMasami Hiramatsu 	memset(&trace_probe_log, 0, sizeof(trace_probe_log));
174ab105a4fSMasami Hiramatsu }
175ab105a4fSMasami Hiramatsu 
trace_probe_log_set_index(int index)176ab105a4fSMasami Hiramatsu void trace_probe_log_set_index(int index)
177ab105a4fSMasami Hiramatsu {
178*fd837de3SMasami Hiramatsu (Google) 	lockdep_assert_held(&dyn_event_ops_mutex);
179*fd837de3SMasami Hiramatsu (Google) 
180ab105a4fSMasami Hiramatsu 	trace_probe_log.index = index;
181ab105a4fSMasami Hiramatsu }
182ab105a4fSMasami Hiramatsu 
__trace_probe_log_err(int offset,int err_type)183ab105a4fSMasami Hiramatsu void __trace_probe_log_err(int offset, int err_type)
184ab105a4fSMasami Hiramatsu {
185ab105a4fSMasami Hiramatsu 	char *command, *p;
186ab105a4fSMasami Hiramatsu 	int i, len = 0, pos = 0;
187ab105a4fSMasami Hiramatsu 
188*fd837de3SMasami Hiramatsu (Google) 	lockdep_assert_held(&dyn_event_ops_mutex);
189*fd837de3SMasami Hiramatsu (Google) 
190ab105a4fSMasami Hiramatsu 	if (!trace_probe_log.argv)
191ab105a4fSMasami Hiramatsu 		return;
192ab105a4fSMasami Hiramatsu 
193f2cc020dSIngo Molnar 	/* Recalculate the length and allocate buffer */
194ab105a4fSMasami Hiramatsu 	for (i = 0; i < trace_probe_log.argc; i++) {
195ab105a4fSMasami Hiramatsu 		if (i == trace_probe_log.index)
196ab105a4fSMasami Hiramatsu 			pos = len;
197ab105a4fSMasami Hiramatsu 		len += strlen(trace_probe_log.argv[i]) + 1;
198ab105a4fSMasami Hiramatsu 	}
199ab105a4fSMasami Hiramatsu 	command = kzalloc(len, GFP_KERNEL);
200ab105a4fSMasami Hiramatsu 	if (!command)
201ab105a4fSMasami Hiramatsu 		return;
202ab105a4fSMasami Hiramatsu 
203d2aea95aSMasami Hiramatsu 	if (trace_probe_log.index >= trace_probe_log.argc) {
204d2aea95aSMasami Hiramatsu 		/**
205d2aea95aSMasami Hiramatsu 		 * Set the error position is next to the last arg + space.
206d2aea95aSMasami Hiramatsu 		 * Note that len includes the terminal null and the cursor
207f2cc020dSIngo Molnar 		 * appears at pos + 1.
208d2aea95aSMasami Hiramatsu 		 */
209d2aea95aSMasami Hiramatsu 		pos = len;
210d2aea95aSMasami Hiramatsu 		offset = 0;
211d2aea95aSMasami Hiramatsu 	}
212d2aea95aSMasami Hiramatsu 
213ab105a4fSMasami Hiramatsu 	/* And make a command string from argv array */
214ab105a4fSMasami Hiramatsu 	p = command;
215ab105a4fSMasami Hiramatsu 	for (i = 0; i < trace_probe_log.argc; i++) {
216ab105a4fSMasami Hiramatsu 		len = strlen(trace_probe_log.argv[i]);
217ab105a4fSMasami Hiramatsu 		strcpy(p, trace_probe_log.argv[i]);
218ab105a4fSMasami Hiramatsu 		p[len] = ' ';
219ab105a4fSMasami Hiramatsu 		p += len + 1;
220ab105a4fSMasami Hiramatsu 	}
221ab105a4fSMasami Hiramatsu 	*(p - 1) = '\0';
222ab105a4fSMasami Hiramatsu 
2232f754e77SSteven Rostedt (VMware) 	tracing_log_err(NULL, trace_probe_log.subsystem, command,
224ab105a4fSMasami Hiramatsu 			trace_probe_err_text, err_type, pos + offset);
225ab105a4fSMasami Hiramatsu 
226ab105a4fSMasami Hiramatsu 	kfree(command);
227ab105a4fSMasami Hiramatsu }
228ab105a4fSMasami Hiramatsu 
2298ab83f56SSrikar Dronamraju /* Split symbol and offset. */
traceprobe_split_symbol_offset(char * symbol,long * offset)230c5d343b6SMasami Hiramatsu int traceprobe_split_symbol_offset(char *symbol, long *offset)
2318ab83f56SSrikar Dronamraju {
2328ab83f56SSrikar Dronamraju 	char *tmp;
2338ab83f56SSrikar Dronamraju 	int ret;
2348ab83f56SSrikar Dronamraju 
2358ab83f56SSrikar Dronamraju 	if (!offset)
2368ab83f56SSrikar Dronamraju 		return -EINVAL;
2378ab83f56SSrikar Dronamraju 
238c5d343b6SMasami Hiramatsu 	tmp = strpbrk(symbol, "+-");
2398ab83f56SSrikar Dronamraju 	if (tmp) {
240c5d343b6SMasami Hiramatsu 		ret = kstrtol(tmp, 0, offset);
2418ab83f56SSrikar Dronamraju 		if (ret)
2428ab83f56SSrikar Dronamraju 			return ret;
2438ab83f56SSrikar Dronamraju 		*tmp = '\0';
2448ab83f56SSrikar Dronamraju 	} else
2458ab83f56SSrikar Dronamraju 		*offset = 0;
2468ab83f56SSrikar Dronamraju 
2478ab83f56SSrikar Dronamraju 	return 0;
2488ab83f56SSrikar Dronamraju }
2498ab83f56SSrikar Dronamraju 
2506212dd29SMasami Hiramatsu /* @buf must has MAX_EVENT_NAME_LEN size */
traceprobe_parse_event_name(const char ** pevent,const char ** pgroup,char * buf,int offset)2516212dd29SMasami Hiramatsu int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
252ab105a4fSMasami Hiramatsu 				char *buf, int offset)
2536212dd29SMasami Hiramatsu {
2546212dd29SMasami Hiramatsu 	const char *slash, *event = *pevent;
255dec65d79SMasami Hiramatsu 	int len;
2566212dd29SMasami Hiramatsu 
2576212dd29SMasami Hiramatsu 	slash = strchr(event, '/');
258bc1b9734SSteven Rostedt (VMware) 	if (!slash)
259bc1b9734SSteven Rostedt (VMware) 		slash = strchr(event, '.');
260bc1b9734SSteven Rostedt (VMware) 
2616212dd29SMasami Hiramatsu 	if (slash) {
2626212dd29SMasami Hiramatsu 		if (slash == event) {
263ab105a4fSMasami Hiramatsu 			trace_probe_log_err(offset, NO_GROUP_NAME);
2646212dd29SMasami Hiramatsu 			return -EINVAL;
2656212dd29SMasami Hiramatsu 		}
2666212dd29SMasami Hiramatsu 		if (slash - event + 1 > MAX_EVENT_NAME_LEN) {
267ab105a4fSMasami Hiramatsu 			trace_probe_log_err(offset, GROUP_TOO_LONG);
268ab105a4fSMasami Hiramatsu 			return -EINVAL;
2696212dd29SMasami Hiramatsu 		}
270c7dce4c5SAzeem Shaikh 		strscpy(buf, event, slash - event + 1);
271575b76cbSSteven Rostedt (Google) 		if (!is_good_system_name(buf)) {
272ab105a4fSMasami Hiramatsu 			trace_probe_log_err(offset, BAD_GROUP_NAME);
2735b7a9622SMasami Hiramatsu 			return -EINVAL;
2745b7a9622SMasami Hiramatsu 		}
2756212dd29SMasami Hiramatsu 		*pgroup = buf;
2766212dd29SMasami Hiramatsu 		*pevent = slash + 1;
277ab105a4fSMasami Hiramatsu 		offset += slash - event + 1;
278dec65d79SMasami Hiramatsu 		event = *pevent;
2796212dd29SMasami Hiramatsu 	}
280dec65d79SMasami Hiramatsu 	len = strlen(event);
281dec65d79SMasami Hiramatsu 	if (len == 0) {
28295c104c3SLinyu Yuan 		if (slash) {
28395c104c3SLinyu Yuan 			*pevent = NULL;
28495c104c3SLinyu Yuan 			return 0;
28595c104c3SLinyu Yuan 		}
286ab105a4fSMasami Hiramatsu 		trace_probe_log_err(offset, NO_EVENT_NAME);
2876212dd29SMasami Hiramatsu 		return -EINVAL;
2880b6e2e22SLeo Yan 	} else if (len >= MAX_EVENT_NAME_LEN) {
289ab105a4fSMasami Hiramatsu 		trace_probe_log_err(offset, EVENT_TOO_LONG);
290ab105a4fSMasami Hiramatsu 		return -EINVAL;
2916212dd29SMasami Hiramatsu 	}
2925b7a9622SMasami Hiramatsu 	if (!is_good_name(event)) {
293ab105a4fSMasami Hiramatsu 		trace_probe_log_err(offset, BAD_EVENT_NAME);
2945b7a9622SMasami Hiramatsu 		return -EINVAL;
2955b7a9622SMasami Hiramatsu 	}
2966212dd29SMasami Hiramatsu 	return 0;
2976212dd29SMasami Hiramatsu }
2986212dd29SMasami Hiramatsu 
parse_trace_event_arg(char * arg,struct fetch_insn * code,struct traceprobe_parse_context * ctx)2991b8b0cd7SMasami Hiramatsu (Google) static int parse_trace_event_arg(char *arg, struct fetch_insn *code,
3001b8b0cd7SMasami Hiramatsu (Google) 				 struct traceprobe_parse_context *ctx)
3011b8b0cd7SMasami Hiramatsu (Google) {
3021b8b0cd7SMasami Hiramatsu (Google) 	struct ftrace_event_field *field;
3031b8b0cd7SMasami Hiramatsu (Google) 	struct list_head *head;
3041b8b0cd7SMasami Hiramatsu (Google) 
3051b8b0cd7SMasami Hiramatsu (Google) 	head = trace_get_fields(ctx->event);
3061b8b0cd7SMasami Hiramatsu (Google) 	list_for_each_entry(field, head, link) {
3071b8b0cd7SMasami Hiramatsu (Google) 		if (!strcmp(arg, field->name)) {
3081b8b0cd7SMasami Hiramatsu (Google) 			code->op = FETCH_OP_TP_ARG;
3091b8b0cd7SMasami Hiramatsu (Google) 			code->data = field;
3101b8b0cd7SMasami Hiramatsu (Google) 			return 0;
3111b8b0cd7SMasami Hiramatsu (Google) 		}
3121b8b0cd7SMasami Hiramatsu (Google) 	}
3131b8b0cd7SMasami Hiramatsu (Google) 	return -ENOENT;
3141b8b0cd7SMasami Hiramatsu (Google) }
3151b8b0cd7SMasami Hiramatsu (Google) 
316b576e097SMasami Hiramatsu (Google) #ifdef CONFIG_PROBE_EVENTS_BTF_ARGS
317b576e097SMasami Hiramatsu (Google) 
btf_type_int(const struct btf_type * t)318b576e097SMasami Hiramatsu (Google) static u32 btf_type_int(const struct btf_type *t)
319b576e097SMasami Hiramatsu (Google) {
320b576e097SMasami Hiramatsu (Google) 	return *(u32 *)(t + 1);
321b576e097SMasami Hiramatsu (Google) }
322b576e097SMasami Hiramatsu (Google) 
btf_type_is_char_ptr(struct btf * btf,const struct btf_type * type)32327973e5cSMasami Hiramatsu (Google) static bool btf_type_is_char_ptr(struct btf *btf, const struct btf_type *type)
32427973e5cSMasami Hiramatsu (Google) {
32527973e5cSMasami Hiramatsu (Google) 	const struct btf_type *real_type;
32627973e5cSMasami Hiramatsu (Google) 	u32 intdata;
32727973e5cSMasami Hiramatsu (Google) 	s32 tid;
32827973e5cSMasami Hiramatsu (Google) 
32927973e5cSMasami Hiramatsu (Google) 	real_type = btf_type_skip_modifiers(btf, type->type, &tid);
33027973e5cSMasami Hiramatsu (Google) 	if (!real_type)
33127973e5cSMasami Hiramatsu (Google) 		return false;
33227973e5cSMasami Hiramatsu (Google) 
33327973e5cSMasami Hiramatsu (Google) 	if (BTF_INFO_KIND(real_type->info) != BTF_KIND_INT)
33427973e5cSMasami Hiramatsu (Google) 		return false;
33527973e5cSMasami Hiramatsu (Google) 
33627973e5cSMasami Hiramatsu (Google) 	intdata = btf_type_int(real_type);
33727973e5cSMasami Hiramatsu (Google) 	return !(BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED)
33827973e5cSMasami Hiramatsu (Google) 		&& BTF_INT_BITS(intdata) == 8;
33927973e5cSMasami Hiramatsu (Google) }
34027973e5cSMasami Hiramatsu (Google) 
btf_type_is_char_array(struct btf * btf,const struct btf_type * type)34127973e5cSMasami Hiramatsu (Google) static bool btf_type_is_char_array(struct btf *btf, const struct btf_type *type)
34227973e5cSMasami Hiramatsu (Google) {
34327973e5cSMasami Hiramatsu (Google) 	const struct btf_type *real_type;
34427973e5cSMasami Hiramatsu (Google) 	const struct btf_array *array;
34527973e5cSMasami Hiramatsu (Google) 	u32 intdata;
34627973e5cSMasami Hiramatsu (Google) 	s32 tid;
34727973e5cSMasami Hiramatsu (Google) 
34827973e5cSMasami Hiramatsu (Google) 	if (BTF_INFO_KIND(type->info) != BTF_KIND_ARRAY)
34927973e5cSMasami Hiramatsu (Google) 		return false;
35027973e5cSMasami Hiramatsu (Google) 
35127973e5cSMasami Hiramatsu (Google) 	array = (const struct btf_array *)(type + 1);
35227973e5cSMasami Hiramatsu (Google) 
35327973e5cSMasami Hiramatsu (Google) 	real_type = btf_type_skip_modifiers(btf, array->type, &tid);
35427973e5cSMasami Hiramatsu (Google) 
35527973e5cSMasami Hiramatsu (Google) 	intdata = btf_type_int(real_type);
35627973e5cSMasami Hiramatsu (Google) 	return !(BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED)
35727973e5cSMasami Hiramatsu (Google) 		&& BTF_INT_BITS(intdata) == 8;
35827973e5cSMasami Hiramatsu (Google) }
35927973e5cSMasami Hiramatsu (Google) 
check_prepare_btf_string_fetch(char * typename,struct fetch_insn ** pcode,struct traceprobe_parse_context * ctx)36027973e5cSMasami Hiramatsu (Google) static int check_prepare_btf_string_fetch(char *typename,
36127973e5cSMasami Hiramatsu (Google) 				struct fetch_insn **pcode,
36227973e5cSMasami Hiramatsu (Google) 				struct traceprobe_parse_context *ctx)
36327973e5cSMasami Hiramatsu (Google) {
36427973e5cSMasami Hiramatsu (Google) 	struct btf *btf = ctx->btf;
36527973e5cSMasami Hiramatsu (Google) 
36627973e5cSMasami Hiramatsu (Google) 	if (!btf || !ctx->last_type)
36727973e5cSMasami Hiramatsu (Google) 		return 0;
36827973e5cSMasami Hiramatsu (Google) 
36927973e5cSMasami Hiramatsu (Google) 	/* char [] does not need any change. */
37027973e5cSMasami Hiramatsu (Google) 	if (btf_type_is_char_array(btf, ctx->last_type))
37127973e5cSMasami Hiramatsu (Google) 		return 0;
37227973e5cSMasami Hiramatsu (Google) 
37327973e5cSMasami Hiramatsu (Google) 	/* char * requires dereference the pointer. */
37427973e5cSMasami Hiramatsu (Google) 	if (btf_type_is_char_ptr(btf, ctx->last_type)) {
37527973e5cSMasami Hiramatsu (Google) 		struct fetch_insn *code = *pcode + 1;
37627973e5cSMasami Hiramatsu (Google) 
37727973e5cSMasami Hiramatsu (Google) 		if (code->op == FETCH_OP_END) {
37827973e5cSMasami Hiramatsu (Google) 			trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
37927973e5cSMasami Hiramatsu (Google) 			return -E2BIG;
38027973e5cSMasami Hiramatsu (Google) 		}
38127973e5cSMasami Hiramatsu (Google) 		if (typename[0] == 'u')
38227973e5cSMasami Hiramatsu (Google) 			code->op = FETCH_OP_UDEREF;
38327973e5cSMasami Hiramatsu (Google) 		else
38427973e5cSMasami Hiramatsu (Google) 			code->op = FETCH_OP_DEREF;
38527973e5cSMasami Hiramatsu (Google) 		code->offset = 0;
38627973e5cSMasami Hiramatsu (Google) 		*pcode = code;
38727973e5cSMasami Hiramatsu (Google) 		return 0;
38827973e5cSMasami Hiramatsu (Google) 	}
38927973e5cSMasami Hiramatsu (Google) 	/* Other types are not available for string */
39027973e5cSMasami Hiramatsu (Google) 	trace_probe_log_err(ctx->offset, BAD_TYPE4STR);
39127973e5cSMasami Hiramatsu (Google) 	return -EINVAL;
39227973e5cSMasami Hiramatsu (Google) }
39327973e5cSMasami Hiramatsu (Google) 
fetch_type_from_btf_type(struct btf * btf,const struct btf_type * type,struct traceprobe_parse_context * ctx)394c440adfbSMasami Hiramatsu (Google) static const char *fetch_type_from_btf_type(struct btf *btf,
395c440adfbSMasami Hiramatsu (Google) 					const struct btf_type *type,
396c440adfbSMasami Hiramatsu (Google) 					struct traceprobe_parse_context *ctx)
397b576e097SMasami Hiramatsu (Google) {
398b576e097SMasami Hiramatsu (Google) 	u32 intdata;
399b576e097SMasami Hiramatsu (Google) 
400b576e097SMasami Hiramatsu (Google) 	/* TODO: const char * could be converted as a string */
401c440adfbSMasami Hiramatsu (Google) 	switch (BTF_INFO_KIND(type->info)) {
402b576e097SMasami Hiramatsu (Google) 	case BTF_KIND_ENUM:
403b576e097SMasami Hiramatsu (Google) 		/* enum is "int", so convert to "s32" */
404b576e097SMasami Hiramatsu (Google) 		return "s32";
405b576e097SMasami Hiramatsu (Google) 	case BTF_KIND_ENUM64:
406b576e097SMasami Hiramatsu (Google) 		return "s64";
407b576e097SMasami Hiramatsu (Google) 	case BTF_KIND_PTR:
408b576e097SMasami Hiramatsu (Google) 		/* pointer will be converted to "x??" */
409b576e097SMasami Hiramatsu (Google) 		if (IS_ENABLED(CONFIG_64BIT))
410b576e097SMasami Hiramatsu (Google) 			return "x64";
411b576e097SMasami Hiramatsu (Google) 		else
412b576e097SMasami Hiramatsu (Google) 			return "x32";
413b576e097SMasami Hiramatsu (Google) 	case BTF_KIND_INT:
414c440adfbSMasami Hiramatsu (Google) 		intdata = btf_type_int(type);
415b576e097SMasami Hiramatsu (Google) 		if (BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED) {
416b576e097SMasami Hiramatsu (Google) 			switch (BTF_INT_BITS(intdata)) {
417b576e097SMasami Hiramatsu (Google) 			case 8:
418b576e097SMasami Hiramatsu (Google) 				return "s8";
419b576e097SMasami Hiramatsu (Google) 			case 16:
420b576e097SMasami Hiramatsu (Google) 				return "s16";
421b576e097SMasami Hiramatsu (Google) 			case 32:
422b576e097SMasami Hiramatsu (Google) 				return "s32";
423b576e097SMasami Hiramatsu (Google) 			case 64:
424b576e097SMasami Hiramatsu (Google) 				return "s64";
425b576e097SMasami Hiramatsu (Google) 			}
426b576e097SMasami Hiramatsu (Google) 		} else {	/* unsigned */
427b576e097SMasami Hiramatsu (Google) 			switch (BTF_INT_BITS(intdata)) {
428b576e097SMasami Hiramatsu (Google) 			case 8:
429b576e097SMasami Hiramatsu (Google) 				return "u8";
430b576e097SMasami Hiramatsu (Google) 			case 16:
431b576e097SMasami Hiramatsu (Google) 				return "u16";
432b576e097SMasami Hiramatsu (Google) 			case 32:
433b576e097SMasami Hiramatsu (Google) 				return "u32";
434b576e097SMasami Hiramatsu (Google) 			case 64:
435b576e097SMasami Hiramatsu (Google) 				return "u64";
436b576e097SMasami Hiramatsu (Google) 			}
437c440adfbSMasami Hiramatsu (Google) 			/* bitfield, size is encoded in the type */
438c440adfbSMasami Hiramatsu (Google) 			ctx->last_bitsize = BTF_INT_BITS(intdata);
439c440adfbSMasami Hiramatsu (Google) 			ctx->last_bitoffs += BTF_INT_OFFSET(intdata);
440c440adfbSMasami Hiramatsu (Google) 			return "u64";
441b576e097SMasami Hiramatsu (Google) 		}
442b576e097SMasami Hiramatsu (Google) 	}
443b576e097SMasami Hiramatsu (Google) 	/* TODO: support other types */
444b576e097SMasami Hiramatsu (Google) 
445b576e097SMasami Hiramatsu (Google) 	return NULL;
446b576e097SMasami Hiramatsu (Google) }
447b576e097SMasami Hiramatsu (Google) 
query_btf_context(struct traceprobe_parse_context * ctx)448d157d769SMasami Hiramatsu (Google) static int query_btf_context(struct traceprobe_parse_context *ctx)
449fd26290eSMasami Hiramatsu (Google) {
450fd26290eSMasami Hiramatsu (Google) 	const struct btf_param *param;
451d157d769SMasami Hiramatsu (Google) 	const struct btf_type *type;
452b1d1e904SMasami Hiramatsu (Google) 	struct btf *btf;
453d157d769SMasami Hiramatsu (Google) 	s32 nr;
454fd26290eSMasami Hiramatsu (Google) 
455d157d769SMasami Hiramatsu (Google) 	if (ctx->btf)
456d157d769SMasami Hiramatsu (Google) 		return 0;
457fd26290eSMasami Hiramatsu (Google) 
458d157d769SMasami Hiramatsu (Google) 	if (!ctx->funcname)
459d157d769SMasami Hiramatsu (Google) 		return -EINVAL;
460fd26290eSMasami Hiramatsu (Google) 
461d157d769SMasami Hiramatsu (Google) 	type = btf_find_func_proto(ctx->funcname, &btf);
462d157d769SMasami Hiramatsu (Google) 	if (!type)
463d157d769SMasami Hiramatsu (Google) 		return -ENOENT;
464b576e097SMasami Hiramatsu (Google) 
465d157d769SMasami Hiramatsu (Google) 	ctx->btf = btf;
466d157d769SMasami Hiramatsu (Google) 	ctx->proto = type;
467d157d769SMasami Hiramatsu (Google) 
468d157d769SMasami Hiramatsu (Google) 	/* ctx->params is optional, since func(void) will not have params. */
469d157d769SMasami Hiramatsu (Google) 	nr = 0;
470d157d769SMasami Hiramatsu (Google) 	param = btf_get_func_param(type, &nr);
471d157d769SMasami Hiramatsu (Google) 	if (!IS_ERR_OR_NULL(param)) {
47218b1e870SMasami Hiramatsu (Google) 		/* Hide the first 'data' argument of tracepoint */
473d157d769SMasami Hiramatsu (Google) 		if (ctx->flags & TPARG_FL_TPOINT) {
474d157d769SMasami Hiramatsu (Google) 			nr--;
47518b1e870SMasami Hiramatsu (Google) 			param++;
47618b1e870SMasami Hiramatsu (Google) 		}
477b1d1e904SMasami Hiramatsu (Google) 	}
478b1d1e904SMasami Hiramatsu (Google) 
479d157d769SMasami Hiramatsu (Google) 	if (nr > 0) {
480d157d769SMasami Hiramatsu (Google) 		ctx->nr_params = nr;
481d157d769SMasami Hiramatsu (Google) 		ctx->params = param;
482d157d769SMasami Hiramatsu (Google) 	} else {
483d157d769SMasami Hiramatsu (Google) 		ctx->nr_params = 0;
484d157d769SMasami Hiramatsu (Google) 		ctx->params = NULL;
485d157d769SMasami Hiramatsu (Google) 	}
486d157d769SMasami Hiramatsu (Google) 
487d157d769SMasami Hiramatsu (Google) 	return 0;
488b576e097SMasami Hiramatsu (Google) }
489b576e097SMasami Hiramatsu (Google) 
clear_btf_context(struct traceprobe_parse_context * ctx)490b1d1e904SMasami Hiramatsu (Google) static void clear_btf_context(struct traceprobe_parse_context *ctx)
491b1d1e904SMasami Hiramatsu (Google) {
492b1d1e904SMasami Hiramatsu (Google) 	if (ctx->btf) {
493b1d1e904SMasami Hiramatsu (Google) 		btf_put(ctx->btf);
494b1d1e904SMasami Hiramatsu (Google) 		ctx->btf = NULL;
495d157d769SMasami Hiramatsu (Google) 		ctx->proto = NULL;
496b1d1e904SMasami Hiramatsu (Google) 		ctx->params = NULL;
497b1d1e904SMasami Hiramatsu (Google) 		ctx->nr_params = 0;
498b1d1e904SMasami Hiramatsu (Google) 	}
499b1d1e904SMasami Hiramatsu (Google) }
500b1d1e904SMasami Hiramatsu (Google) 
501c440adfbSMasami Hiramatsu (Google) /* Return 1 if the field separater is arrow operator ('->') */
split_next_field(char * varname,char ** next_field,struct traceprobe_parse_context * ctx)502c440adfbSMasami Hiramatsu (Google) static int split_next_field(char *varname, char **next_field,
503b576e097SMasami Hiramatsu (Google) 			    struct traceprobe_parse_context *ctx)
504b576e097SMasami Hiramatsu (Google) {
505c440adfbSMasami Hiramatsu (Google) 	char *field;
506c440adfbSMasami Hiramatsu (Google) 	int ret = 0;
507c440adfbSMasami Hiramatsu (Google) 
508c440adfbSMasami Hiramatsu (Google) 	field = strpbrk(varname, ".-");
509c440adfbSMasami Hiramatsu (Google) 	if (field) {
510c440adfbSMasami Hiramatsu (Google) 		if (field[0] == '-' && field[1] == '>') {
511c440adfbSMasami Hiramatsu (Google) 			field[0] = '\0';
512c440adfbSMasami Hiramatsu (Google) 			field += 2;
513c440adfbSMasami Hiramatsu (Google) 			ret = 1;
514c440adfbSMasami Hiramatsu (Google) 		} else if (field[0] == '.') {
515c440adfbSMasami Hiramatsu (Google) 			field[0] = '\0';
516c440adfbSMasami Hiramatsu (Google) 			field += 1;
517c440adfbSMasami Hiramatsu (Google) 		} else {
518c440adfbSMasami Hiramatsu (Google) 			trace_probe_log_err(ctx->offset + field - varname, BAD_HYPHEN);
519c440adfbSMasami Hiramatsu (Google) 			return -EINVAL;
520c440adfbSMasami Hiramatsu (Google) 		}
521c440adfbSMasami Hiramatsu (Google) 		*next_field = field;
522c440adfbSMasami Hiramatsu (Google) 	}
523c440adfbSMasami Hiramatsu (Google) 
524c440adfbSMasami Hiramatsu (Google) 	return ret;
525c440adfbSMasami Hiramatsu (Google) }
526c440adfbSMasami Hiramatsu (Google) 
527c440adfbSMasami Hiramatsu (Google) /*
528c440adfbSMasami Hiramatsu (Google)  * Parse the field of data structure. The @type must be a pointer type
529c440adfbSMasami Hiramatsu (Google)  * pointing the target data structure type.
530c440adfbSMasami Hiramatsu (Google)  */
parse_btf_field(char * fieldname,const struct btf_type * type,struct fetch_insn ** pcode,struct fetch_insn * end,struct traceprobe_parse_context * ctx)531c440adfbSMasami Hiramatsu (Google) static int parse_btf_field(char *fieldname, const struct btf_type *type,
532c440adfbSMasami Hiramatsu (Google) 			   struct fetch_insn **pcode, struct fetch_insn *end,
533c440adfbSMasami Hiramatsu (Google) 			   struct traceprobe_parse_context *ctx)
534c440adfbSMasami Hiramatsu (Google) {
535c440adfbSMasami Hiramatsu (Google) 	struct fetch_insn *code = *pcode;
536c440adfbSMasami Hiramatsu (Google) 	const struct btf_member *field;
537c440adfbSMasami Hiramatsu (Google) 	u32 bitoffs, anon_offs;
538c440adfbSMasami Hiramatsu (Google) 	char *next;
539c440adfbSMasami Hiramatsu (Google) 	int is_ptr;
540c440adfbSMasami Hiramatsu (Google) 	s32 tid;
541c440adfbSMasami Hiramatsu (Google) 
542c440adfbSMasami Hiramatsu (Google) 	do {
543c440adfbSMasami Hiramatsu (Google) 		/* Outer loop for solving arrow operator ('->') */
544c440adfbSMasami Hiramatsu (Google) 		if (BTF_INFO_KIND(type->info) != BTF_KIND_PTR) {
545c440adfbSMasami Hiramatsu (Google) 			trace_probe_log_err(ctx->offset, NO_PTR_STRCT);
546c440adfbSMasami Hiramatsu (Google) 			return -EINVAL;
547c440adfbSMasami Hiramatsu (Google) 		}
548c440adfbSMasami Hiramatsu (Google) 		/* Convert a struct pointer type to a struct type */
549c440adfbSMasami Hiramatsu (Google) 		type = btf_type_skip_modifiers(ctx->btf, type->type, &tid);
550c440adfbSMasami Hiramatsu (Google) 		if (!type) {
551c440adfbSMasami Hiramatsu (Google) 			trace_probe_log_err(ctx->offset, BAD_BTF_TID);
552c440adfbSMasami Hiramatsu (Google) 			return -EINVAL;
553c440adfbSMasami Hiramatsu (Google) 		}
554c440adfbSMasami Hiramatsu (Google) 
555c440adfbSMasami Hiramatsu (Google) 		bitoffs = 0;
556c440adfbSMasami Hiramatsu (Google) 		do {
557c440adfbSMasami Hiramatsu (Google) 			/* Inner loop for solving dot operator ('.') */
558c440adfbSMasami Hiramatsu (Google) 			next = NULL;
559c440adfbSMasami Hiramatsu (Google) 			is_ptr = split_next_field(fieldname, &next, ctx);
560c440adfbSMasami Hiramatsu (Google) 			if (is_ptr < 0)
561c440adfbSMasami Hiramatsu (Google) 				return is_ptr;
562c440adfbSMasami Hiramatsu (Google) 
563c440adfbSMasami Hiramatsu (Google) 			anon_offs = 0;
564c440adfbSMasami Hiramatsu (Google) 			field = btf_find_struct_member(ctx->btf, type, fieldname,
565c440adfbSMasami Hiramatsu (Google) 						       &anon_offs);
566e569eb34SCarlos López 			if (IS_ERR(field)) {
567e569eb34SCarlos López 				trace_probe_log_err(ctx->offset, BAD_BTF_TID);
568e569eb34SCarlos López 				return PTR_ERR(field);
569e569eb34SCarlos López 			}
570c440adfbSMasami Hiramatsu (Google) 			if (!field) {
571c440adfbSMasami Hiramatsu (Google) 				trace_probe_log_err(ctx->offset, NO_BTF_FIELD);
572c440adfbSMasami Hiramatsu (Google) 				return -ENOENT;
573c440adfbSMasami Hiramatsu (Google) 			}
574c440adfbSMasami Hiramatsu (Google) 			/* Add anonymous structure/union offset */
575c440adfbSMasami Hiramatsu (Google) 			bitoffs += anon_offs;
576c440adfbSMasami Hiramatsu (Google) 
577c440adfbSMasami Hiramatsu (Google) 			/* Accumulate the bit-offsets of the dot-connected fields */
578c440adfbSMasami Hiramatsu (Google) 			if (btf_type_kflag(type)) {
579c440adfbSMasami Hiramatsu (Google) 				bitoffs += BTF_MEMBER_BIT_OFFSET(field->offset);
580c440adfbSMasami Hiramatsu (Google) 				ctx->last_bitsize = BTF_MEMBER_BITFIELD_SIZE(field->offset);
581c440adfbSMasami Hiramatsu (Google) 			} else {
582c440adfbSMasami Hiramatsu (Google) 				bitoffs += field->offset;
583c440adfbSMasami Hiramatsu (Google) 				ctx->last_bitsize = 0;
584c440adfbSMasami Hiramatsu (Google) 			}
585c440adfbSMasami Hiramatsu (Google) 
586c440adfbSMasami Hiramatsu (Google) 			type = btf_type_skip_modifiers(ctx->btf, field->type, &tid);
587c440adfbSMasami Hiramatsu (Google) 			if (!type) {
588c440adfbSMasami Hiramatsu (Google) 				trace_probe_log_err(ctx->offset, BAD_BTF_TID);
589c440adfbSMasami Hiramatsu (Google) 				return -EINVAL;
590c440adfbSMasami Hiramatsu (Google) 			}
591c440adfbSMasami Hiramatsu (Google) 
592c440adfbSMasami Hiramatsu (Google) 			ctx->offset += next - fieldname;
593c440adfbSMasami Hiramatsu (Google) 			fieldname = next;
594c440adfbSMasami Hiramatsu (Google) 		} while (!is_ptr && fieldname);
595c440adfbSMasami Hiramatsu (Google) 
596c440adfbSMasami Hiramatsu (Google) 		if (++code == end) {
597c440adfbSMasami Hiramatsu (Google) 			trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
598c440adfbSMasami Hiramatsu (Google) 			return -EINVAL;
599c440adfbSMasami Hiramatsu (Google) 		}
600c440adfbSMasami Hiramatsu (Google) 		code->op = FETCH_OP_DEREF;	/* TODO: user deref support */
601c440adfbSMasami Hiramatsu (Google) 		code->offset = bitoffs / 8;
602c440adfbSMasami Hiramatsu (Google) 		*pcode = code;
603c440adfbSMasami Hiramatsu (Google) 
604c440adfbSMasami Hiramatsu (Google) 		ctx->last_bitoffs = bitoffs % 8;
605c440adfbSMasami Hiramatsu (Google) 		ctx->last_type = type;
606c440adfbSMasami Hiramatsu (Google) 	} while (fieldname);
607c440adfbSMasami Hiramatsu (Google) 
608c440adfbSMasami Hiramatsu (Google) 	return 0;
609c440adfbSMasami Hiramatsu (Google) }
610c440adfbSMasami Hiramatsu (Google) 
61125f00e40SMasami Hiramatsu (Google) static int __store_entry_arg(struct trace_probe *tp, int argnum);
61225f00e40SMasami Hiramatsu (Google) 
parse_btf_arg(char * varname,struct fetch_insn ** pcode,struct fetch_insn * end,struct traceprobe_parse_context * ctx)613c440adfbSMasami Hiramatsu (Google) static int parse_btf_arg(char *varname,
614c440adfbSMasami Hiramatsu (Google) 			 struct fetch_insn **pcode, struct fetch_insn *end,
615c440adfbSMasami Hiramatsu (Google) 			 struct traceprobe_parse_context *ctx)
616c440adfbSMasami Hiramatsu (Google) {
617c440adfbSMasami Hiramatsu (Google) 	struct fetch_insn *code = *pcode;
618b576e097SMasami Hiramatsu (Google) 	const struct btf_param *params;
619c440adfbSMasami Hiramatsu (Google) 	const struct btf_type *type;
620c440adfbSMasami Hiramatsu (Google) 	char *field = NULL;
621d157d769SMasami Hiramatsu (Google) 	int i, is_ptr, ret;
622c440adfbSMasami Hiramatsu (Google) 	u32 tid;
623b576e097SMasami Hiramatsu (Google) 
624b576e097SMasami Hiramatsu (Google) 	if (WARN_ON_ONCE(!ctx->funcname))
625b576e097SMasami Hiramatsu (Google) 		return -EINVAL;
626b576e097SMasami Hiramatsu (Google) 
627c440adfbSMasami Hiramatsu (Google) 	is_ptr = split_next_field(varname, &field, ctx);
628c440adfbSMasami Hiramatsu (Google) 	if (is_ptr < 0)
629c440adfbSMasami Hiramatsu (Google) 		return is_ptr;
630c440adfbSMasami Hiramatsu (Google) 	if (!is_ptr && field) {
631c440adfbSMasami Hiramatsu (Google) 		/* dot-connected field on an argument is not supported. */
632c440adfbSMasami Hiramatsu (Google) 		trace_probe_log_err(ctx->offset + field - varname,
633c440adfbSMasami Hiramatsu (Google) 				    NOSUP_DAT_ARG);
634c440adfbSMasami Hiramatsu (Google) 		return -EOPNOTSUPP;
635c440adfbSMasami Hiramatsu (Google) 	}
636c440adfbSMasami Hiramatsu (Google) 
63725f00e40SMasami Hiramatsu (Google) 	if (ctx->flags & TPARG_FL_RETURN && !strcmp(varname, "$retval")) {
638d157d769SMasami Hiramatsu (Google) 		code->op = FETCH_OP_RETVAL;
639d157d769SMasami Hiramatsu (Google) 		/* Check whether the function return type is not void */
640d157d769SMasami Hiramatsu (Google) 		if (query_btf_context(ctx) == 0) {
641d157d769SMasami Hiramatsu (Google) 			if (ctx->proto->type == 0) {
642d157d769SMasami Hiramatsu (Google) 				trace_probe_log_err(ctx->offset, NO_RETVAL);
643d157d769SMasami Hiramatsu (Google) 				return -ENOENT;
644d157d769SMasami Hiramatsu (Google) 			}
645d157d769SMasami Hiramatsu (Google) 			tid = ctx->proto->type;
646d157d769SMasami Hiramatsu (Google) 			goto found;
647d157d769SMasami Hiramatsu (Google) 		}
648d157d769SMasami Hiramatsu (Google) 		if (field) {
649d157d769SMasami Hiramatsu (Google) 			trace_probe_log_err(ctx->offset + field - varname,
650d157d769SMasami Hiramatsu (Google) 					    NO_BTF_ENTRY);
651d157d769SMasami Hiramatsu (Google) 			return -ENOENT;
652d157d769SMasami Hiramatsu (Google) 		}
653d157d769SMasami Hiramatsu (Google) 		return 0;
654d157d769SMasami Hiramatsu (Google) 	}
655d157d769SMasami Hiramatsu (Google) 
656d157d769SMasami Hiramatsu (Google) 	if (!ctx->btf) {
657d157d769SMasami Hiramatsu (Google) 		ret = query_btf_context(ctx);
658d157d769SMasami Hiramatsu (Google) 		if (ret < 0 || ctx->nr_params == 0) {
659b576e097SMasami Hiramatsu (Google) 			trace_probe_log_err(ctx->offset, NO_BTF_ENTRY);
660b576e097SMasami Hiramatsu (Google) 			return PTR_ERR(params);
661b576e097SMasami Hiramatsu (Google) 		}
662d157d769SMasami Hiramatsu (Google) 	}
663b576e097SMasami Hiramatsu (Google) 	params = ctx->params;
664b576e097SMasami Hiramatsu (Google) 
665b576e097SMasami Hiramatsu (Google) 	for (i = 0; i < ctx->nr_params; i++) {
666b1d1e904SMasami Hiramatsu (Google) 		const char *name = btf_name_by_offset(ctx->btf, params[i].name_off);
667b576e097SMasami Hiramatsu (Google) 
668b576e097SMasami Hiramatsu (Google) 		if (name && !strcmp(name, varname)) {
66925f00e40SMasami Hiramatsu (Google) 			if (tparg_is_function_entry(ctx->flags)) {
670b576e097SMasami Hiramatsu (Google) 				code->op = FETCH_OP_ARG;
67153431798SMasami Hiramatsu (Google) 				if (ctx->flags & TPARG_FL_TPOINT)
67253431798SMasami Hiramatsu (Google) 					code->param = i + 1;
67353431798SMasami Hiramatsu (Google) 				else
674b576e097SMasami Hiramatsu (Google) 					code->param = i;
67525f00e40SMasami Hiramatsu (Google) 			} else if (tparg_is_function_return(ctx->flags)) {
67625f00e40SMasami Hiramatsu (Google) 				code->op = FETCH_OP_EDATA;
67725f00e40SMasami Hiramatsu (Google) 				ret = __store_entry_arg(ctx->tp, i);
67825f00e40SMasami Hiramatsu (Google) 				if (ret < 0) {
67925f00e40SMasami Hiramatsu (Google) 					/* internal error */
68025f00e40SMasami Hiramatsu (Google) 					return ret;
68125f00e40SMasami Hiramatsu (Google) 				}
68225f00e40SMasami Hiramatsu (Google) 				code->offset = ret;
68325f00e40SMasami Hiramatsu (Google) 			}
684c440adfbSMasami Hiramatsu (Google) 			tid = params[i].type;
685c440adfbSMasami Hiramatsu (Google) 			goto found;
686b576e097SMasami Hiramatsu (Google) 		}
687b576e097SMasami Hiramatsu (Google) 	}
688b576e097SMasami Hiramatsu (Google) 	trace_probe_log_err(ctx->offset, NO_BTFARG);
689b576e097SMasami Hiramatsu (Google) 	return -ENOENT;
690c440adfbSMasami Hiramatsu (Google) 
691c440adfbSMasami Hiramatsu (Google) found:
692c440adfbSMasami Hiramatsu (Google) 	type = btf_type_skip_modifiers(ctx->btf, tid, &tid);
693c440adfbSMasami Hiramatsu (Google) 	if (!type) {
694c440adfbSMasami Hiramatsu (Google) 		trace_probe_log_err(ctx->offset, BAD_BTF_TID);
695c440adfbSMasami Hiramatsu (Google) 		return -EINVAL;
696c440adfbSMasami Hiramatsu (Google) 	}
697c440adfbSMasami Hiramatsu (Google) 	/* Initialize the last type information */
698c440adfbSMasami Hiramatsu (Google) 	ctx->last_type = type;
699c440adfbSMasami Hiramatsu (Google) 	ctx->last_bitoffs = 0;
700c440adfbSMasami Hiramatsu (Google) 	ctx->last_bitsize = 0;
701c440adfbSMasami Hiramatsu (Google) 	if (field) {
702c440adfbSMasami Hiramatsu (Google) 		ctx->offset += field - varname;
703c440adfbSMasami Hiramatsu (Google) 		return parse_btf_field(field, type, pcode, end, ctx);
704c440adfbSMasami Hiramatsu (Google) 	}
705c440adfbSMasami Hiramatsu (Google) 	return 0;
706b576e097SMasami Hiramatsu (Google) }
707b576e097SMasami Hiramatsu (Google) 
find_fetch_type_from_btf_type(struct traceprobe_parse_context * ctx)708d157d769SMasami Hiramatsu (Google) static const struct fetch_type *find_fetch_type_from_btf_type(
709b576e097SMasami Hiramatsu (Google) 					struct traceprobe_parse_context *ctx)
710b576e097SMasami Hiramatsu (Google) {
711b1d1e904SMasami Hiramatsu (Google) 	struct btf *btf = ctx->btf;
712b576e097SMasami Hiramatsu (Google) 	const char *typestr = NULL;
713b576e097SMasami Hiramatsu (Google) 
714c440adfbSMasami Hiramatsu (Google) 	if (btf && ctx->last_type)
715c440adfbSMasami Hiramatsu (Google) 		typestr = fetch_type_from_btf_type(btf, ctx->last_type, ctx);
716b576e097SMasami Hiramatsu (Google) 
717b576e097SMasami Hiramatsu (Google) 	return find_fetch_type(typestr, ctx->flags);
718b576e097SMasami Hiramatsu (Google) }
71918b1e870SMasami Hiramatsu (Google) 
parse_btf_bitfield(struct fetch_insn ** pcode,struct traceprobe_parse_context * ctx)720c440adfbSMasami Hiramatsu (Google) static int parse_btf_bitfield(struct fetch_insn **pcode,
721c440adfbSMasami Hiramatsu (Google) 			      struct traceprobe_parse_context *ctx)
722c440adfbSMasami Hiramatsu (Google) {
723c440adfbSMasami Hiramatsu (Google) 	struct fetch_insn *code = *pcode;
724c440adfbSMasami Hiramatsu (Google) 
725c440adfbSMasami Hiramatsu (Google) 	if ((ctx->last_bitsize % 8 == 0) && ctx->last_bitoffs == 0)
726c440adfbSMasami Hiramatsu (Google) 		return 0;
727c440adfbSMasami Hiramatsu (Google) 
728c440adfbSMasami Hiramatsu (Google) 	code++;
729c440adfbSMasami Hiramatsu (Google) 	if (code->op != FETCH_OP_NOP) {
730c440adfbSMasami Hiramatsu (Google) 		trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
731c440adfbSMasami Hiramatsu (Google) 		return -EINVAL;
732c440adfbSMasami Hiramatsu (Google) 	}
733c440adfbSMasami Hiramatsu (Google) 	*pcode = code;
734c440adfbSMasami Hiramatsu (Google) 
735c440adfbSMasami Hiramatsu (Google) 	code->op = FETCH_OP_MOD_BF;
736c440adfbSMasami Hiramatsu (Google) 	code->lshift = 64 - (ctx->last_bitsize + ctx->last_bitoffs);
737c440adfbSMasami Hiramatsu (Google) 	code->rshift = 64 - ctx->last_bitsize;
738c440adfbSMasami Hiramatsu (Google) 	code->basesize = 64 / 8;
739c440adfbSMasami Hiramatsu (Google) 	return 0;
740c440adfbSMasami Hiramatsu (Google) }
741c440adfbSMasami Hiramatsu (Google) 
742b576e097SMasami Hiramatsu (Google) #else
clear_btf_context(struct traceprobe_parse_context * ctx)743b1d1e904SMasami Hiramatsu (Google) static void clear_btf_context(struct traceprobe_parse_context *ctx)
744b576e097SMasami Hiramatsu (Google) {
745b1d1e904SMasami Hiramatsu (Google) 	ctx->btf = NULL;
746b576e097SMasami Hiramatsu (Google) }
747b576e097SMasami Hiramatsu (Google) 
query_btf_context(struct traceprobe_parse_context * ctx)748d157d769SMasami Hiramatsu (Google) static int query_btf_context(struct traceprobe_parse_context *ctx)
74918b1e870SMasami Hiramatsu (Google) {
750d157d769SMasami Hiramatsu (Google) 	return -EOPNOTSUPP;
75118b1e870SMasami Hiramatsu (Google) }
75218b1e870SMasami Hiramatsu (Google) 
parse_btf_arg(char * varname,struct fetch_insn ** pcode,struct fetch_insn * end,struct traceprobe_parse_context * ctx)753c440adfbSMasami Hiramatsu (Google) static int parse_btf_arg(char *varname,
754c440adfbSMasami Hiramatsu (Google) 			 struct fetch_insn **pcode, struct fetch_insn *end,
755b576e097SMasami Hiramatsu (Google) 			 struct traceprobe_parse_context *ctx)
756b576e097SMasami Hiramatsu (Google) {
757b576e097SMasami Hiramatsu (Google) 	trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
758b576e097SMasami Hiramatsu (Google) 	return -EOPNOTSUPP;
759b576e097SMasami Hiramatsu (Google) }
760fd26290eSMasami Hiramatsu (Google) 
parse_btf_bitfield(struct fetch_insn ** pcode,struct traceprobe_parse_context * ctx)761c440adfbSMasami Hiramatsu (Google) static int parse_btf_bitfield(struct fetch_insn **pcode,
762c440adfbSMasami Hiramatsu (Google) 			      struct traceprobe_parse_context *ctx)
763c440adfbSMasami Hiramatsu (Google) {
764c440adfbSMasami Hiramatsu (Google) 	trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
765c440adfbSMasami Hiramatsu (Google) 	return -EOPNOTSUPP;
766c440adfbSMasami Hiramatsu (Google) }
767c440adfbSMasami Hiramatsu (Google) 
768d157d769SMasami Hiramatsu (Google) #define find_fetch_type_from_btf_type(ctx)		\
769b576e097SMasami Hiramatsu (Google) 	find_fetch_type(NULL, ctx->flags)
770fd26290eSMasami Hiramatsu (Google) 
check_prepare_btf_string_fetch(char * typename,struct fetch_insn ** pcode,struct traceprobe_parse_context * ctx)77127973e5cSMasami Hiramatsu (Google) static int check_prepare_btf_string_fetch(char *typename,
77227973e5cSMasami Hiramatsu (Google) 				struct fetch_insn **pcode,
77327973e5cSMasami Hiramatsu (Google) 				struct traceprobe_parse_context *ctx)
77427973e5cSMasami Hiramatsu (Google) {
77527973e5cSMasami Hiramatsu (Google) 	return 0;
77627973e5cSMasami Hiramatsu (Google) }
77727973e5cSMasami Hiramatsu (Google) 
778b576e097SMasami Hiramatsu (Google) #endif
779b576e097SMasami Hiramatsu (Google) 
78025f00e40SMasami Hiramatsu (Google) #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
78125f00e40SMasami Hiramatsu (Google) 
782bb9c6020SMasami Hiramatsu (Google) /*
783bb9c6020SMasami Hiramatsu (Google)  * Add the entry code to store the 'argnum'th parameter and return the offset
784bb9c6020SMasami Hiramatsu (Google)  * in the entry data buffer where the data will be stored.
785bb9c6020SMasami Hiramatsu (Google)  */
__store_entry_arg(struct trace_probe * tp,int argnum)78625f00e40SMasami Hiramatsu (Google) static int __store_entry_arg(struct trace_probe *tp, int argnum)
78725f00e40SMasami Hiramatsu (Google) {
78825f00e40SMasami Hiramatsu (Google) 	struct probe_entry_arg *earg = tp->entry_arg;
78925f00e40SMasami Hiramatsu (Google) 	bool match = false;
79025f00e40SMasami Hiramatsu (Google) 	int i, offset;
79125f00e40SMasami Hiramatsu (Google) 
79225f00e40SMasami Hiramatsu (Google) 	if (!earg) {
79325f00e40SMasami Hiramatsu (Google) 		earg = kzalloc(sizeof(*tp->entry_arg), GFP_KERNEL);
79425f00e40SMasami Hiramatsu (Google) 		if (!earg)
79525f00e40SMasami Hiramatsu (Google) 			return -ENOMEM;
79625f00e40SMasami Hiramatsu (Google) 		earg->size = 2 * tp->nr_args + 1;
79725f00e40SMasami Hiramatsu (Google) 		earg->code = kcalloc(earg->size, sizeof(struct fetch_insn),
79825f00e40SMasami Hiramatsu (Google) 				     GFP_KERNEL);
79925f00e40SMasami Hiramatsu (Google) 		if (!earg->code) {
80025f00e40SMasami Hiramatsu (Google) 			kfree(earg);
80125f00e40SMasami Hiramatsu (Google) 			return -ENOMEM;
80225f00e40SMasami Hiramatsu (Google) 		}
80325f00e40SMasami Hiramatsu (Google) 		/* Fill the code buffer with 'end' to simplify it */
80425f00e40SMasami Hiramatsu (Google) 		for (i = 0; i < earg->size; i++)
80525f00e40SMasami Hiramatsu (Google) 			earg->code[i].op = FETCH_OP_END;
80625f00e40SMasami Hiramatsu (Google) 		tp->entry_arg = earg;
80725f00e40SMasami Hiramatsu (Google) 	}
80825f00e40SMasami Hiramatsu (Google) 
809bb9c6020SMasami Hiramatsu (Google) 	/*
810bb9c6020SMasami Hiramatsu (Google) 	 * The entry code array is repeating the pair of
811bb9c6020SMasami Hiramatsu (Google) 	 * [FETCH_OP_ARG(argnum)][FETCH_OP_ST_EDATA(offset of entry data buffer)]
812bb9c6020SMasami Hiramatsu (Google) 	 * and the rest of entries are filled with [FETCH_OP_END].
813bb9c6020SMasami Hiramatsu (Google) 	 *
814bb9c6020SMasami Hiramatsu (Google) 	 * To reduce the redundant function parameter fetching, we scan the entry
815bb9c6020SMasami Hiramatsu (Google) 	 * code array to find the FETCH_OP_ARG which already fetches the 'argnum'
816bb9c6020SMasami Hiramatsu (Google) 	 * parameter. If it doesn't match, update 'offset' to find the last
817bb9c6020SMasami Hiramatsu (Google) 	 * offset.
818bb9c6020SMasami Hiramatsu (Google) 	 * If we find the FETCH_OP_END without matching FETCH_OP_ARG entry, we
819bb9c6020SMasami Hiramatsu (Google) 	 * will save the entry with FETCH_OP_ARG and FETCH_OP_ST_EDATA, and
820bb9c6020SMasami Hiramatsu (Google) 	 * return data offset so that caller can find the data offset in the entry
821bb9c6020SMasami Hiramatsu (Google) 	 * data buffer.
822bb9c6020SMasami Hiramatsu (Google) 	 */
82325f00e40SMasami Hiramatsu (Google) 	offset = 0;
82425f00e40SMasami Hiramatsu (Google) 	for (i = 0; i < earg->size - 1; i++) {
82525f00e40SMasami Hiramatsu (Google) 		switch (earg->code[i].op) {
82625f00e40SMasami Hiramatsu (Google) 		case FETCH_OP_END:
82725f00e40SMasami Hiramatsu (Google) 			earg->code[i].op = FETCH_OP_ARG;
82825f00e40SMasami Hiramatsu (Google) 			earg->code[i].param = argnum;
82925f00e40SMasami Hiramatsu (Google) 			earg->code[i + 1].op = FETCH_OP_ST_EDATA;
83025f00e40SMasami Hiramatsu (Google) 			earg->code[i + 1].offset = offset;
83125f00e40SMasami Hiramatsu (Google) 			return offset;
83225f00e40SMasami Hiramatsu (Google) 		case FETCH_OP_ARG:
83325f00e40SMasami Hiramatsu (Google) 			match = (earg->code[i].param == argnum);
83425f00e40SMasami Hiramatsu (Google) 			break;
83525f00e40SMasami Hiramatsu (Google) 		case FETCH_OP_ST_EDATA:
83625f00e40SMasami Hiramatsu (Google) 			offset = earg->code[i].offset;
83725f00e40SMasami Hiramatsu (Google) 			if (match)
83825f00e40SMasami Hiramatsu (Google) 				return offset;
83925f00e40SMasami Hiramatsu (Google) 			offset += sizeof(unsigned long);
84025f00e40SMasami Hiramatsu (Google) 			break;
84125f00e40SMasami Hiramatsu (Google) 		default:
84225f00e40SMasami Hiramatsu (Google) 			break;
84325f00e40SMasami Hiramatsu (Google) 		}
84425f00e40SMasami Hiramatsu (Google) 	}
84525f00e40SMasami Hiramatsu (Google) 	return -ENOSPC;
84625f00e40SMasami Hiramatsu (Google) }
84725f00e40SMasami Hiramatsu (Google) 
traceprobe_get_entry_data_size(struct trace_probe * tp)84825f00e40SMasami Hiramatsu (Google) int traceprobe_get_entry_data_size(struct trace_probe *tp)
84925f00e40SMasami Hiramatsu (Google) {
85025f00e40SMasami Hiramatsu (Google) 	struct probe_entry_arg *earg = tp->entry_arg;
85125f00e40SMasami Hiramatsu (Google) 	int i, size = 0;
85225f00e40SMasami Hiramatsu (Google) 
85325f00e40SMasami Hiramatsu (Google) 	if (!earg)
85425f00e40SMasami Hiramatsu (Google) 		return 0;
85525f00e40SMasami Hiramatsu (Google) 
856bb9c6020SMasami Hiramatsu (Google) 	/*
857bb9c6020SMasami Hiramatsu (Google) 	 * earg->code[] array has an operation sequence which is run in
858bb9c6020SMasami Hiramatsu (Google) 	 * the entry handler.
859bb9c6020SMasami Hiramatsu (Google) 	 * The sequence stopped by FETCH_OP_END and each data stored in
860bb9c6020SMasami Hiramatsu (Google) 	 * the entry data buffer by FETCH_OP_ST_EDATA. The FETCH_OP_ST_EDATA
861bb9c6020SMasami Hiramatsu (Google) 	 * stores the data at the data buffer + its offset, and all data are
862bb9c6020SMasami Hiramatsu (Google) 	 * "unsigned long" size. The offset must be increased when a data is
863bb9c6020SMasami Hiramatsu (Google) 	 * stored. Thus we need to find the last FETCH_OP_ST_EDATA in the
864bb9c6020SMasami Hiramatsu (Google) 	 * code array.
865bb9c6020SMasami Hiramatsu (Google) 	 */
86625f00e40SMasami Hiramatsu (Google) 	for (i = 0; i < earg->size; i++) {
86725f00e40SMasami Hiramatsu (Google) 		switch (earg->code[i].op) {
86825f00e40SMasami Hiramatsu (Google) 		case FETCH_OP_END:
86925f00e40SMasami Hiramatsu (Google) 			goto out;
87025f00e40SMasami Hiramatsu (Google) 		case FETCH_OP_ST_EDATA:
87125f00e40SMasami Hiramatsu (Google) 			size = earg->code[i].offset + sizeof(unsigned long);
87225f00e40SMasami Hiramatsu (Google) 			break;
87325f00e40SMasami Hiramatsu (Google) 		default:
87425f00e40SMasami Hiramatsu (Google) 			break;
87525f00e40SMasami Hiramatsu (Google) 		}
87625f00e40SMasami Hiramatsu (Google) 	}
87725f00e40SMasami Hiramatsu (Google) out:
87825f00e40SMasami Hiramatsu (Google) 	return size;
87925f00e40SMasami Hiramatsu (Google) }
88025f00e40SMasami Hiramatsu (Google) 
store_trace_entry_data(void * edata,struct trace_probe * tp,struct pt_regs * regs)88125f00e40SMasami Hiramatsu (Google) void store_trace_entry_data(void *edata, struct trace_probe *tp, struct pt_regs *regs)
88225f00e40SMasami Hiramatsu (Google) {
88325f00e40SMasami Hiramatsu (Google) 	struct probe_entry_arg *earg = tp->entry_arg;
8840add699aSMasami Hiramatsu (Google) 	unsigned long val = 0;
88525f00e40SMasami Hiramatsu (Google) 	int i;
88625f00e40SMasami Hiramatsu (Google) 
88725f00e40SMasami Hiramatsu (Google) 	if (!earg)
88825f00e40SMasami Hiramatsu (Google) 		return;
88925f00e40SMasami Hiramatsu (Google) 
89025f00e40SMasami Hiramatsu (Google) 	for (i = 0; i < earg->size; i++) {
89125f00e40SMasami Hiramatsu (Google) 		struct fetch_insn *code = &earg->code[i];
89225f00e40SMasami Hiramatsu (Google) 
89325f00e40SMasami Hiramatsu (Google) 		switch (code->op) {
89425f00e40SMasami Hiramatsu (Google) 		case FETCH_OP_ARG:
89525f00e40SMasami Hiramatsu (Google) 			val = regs_get_kernel_argument(regs, code->param);
89625f00e40SMasami Hiramatsu (Google) 			break;
89725f00e40SMasami Hiramatsu (Google) 		case FETCH_OP_ST_EDATA:
89825f00e40SMasami Hiramatsu (Google) 			*(unsigned long *)((unsigned long)edata + code->offset) = val;
89925f00e40SMasami Hiramatsu (Google) 			break;
90025f00e40SMasami Hiramatsu (Google) 		case FETCH_OP_END:
90125f00e40SMasami Hiramatsu (Google) 			goto end;
90225f00e40SMasami Hiramatsu (Google) 		default:
90325f00e40SMasami Hiramatsu (Google) 			break;
90425f00e40SMasami Hiramatsu (Google) 		}
90525f00e40SMasami Hiramatsu (Google) 	}
90625f00e40SMasami Hiramatsu (Google) end:
90725f00e40SMasami Hiramatsu (Google) 	return;
90825f00e40SMasami Hiramatsu (Google) }
NOKPROBE_SYMBOL(store_trace_entry_data)90925f00e40SMasami Hiramatsu (Google) NOKPROBE_SYMBOL(store_trace_entry_data)
91025f00e40SMasami Hiramatsu (Google) #endif
91125f00e40SMasami Hiramatsu (Google) 
9128ab83f56SSrikar Dronamraju #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
9138ab83f56SSrikar Dronamraju 
914d157d769SMasami Hiramatsu (Google) /* Parse $vars. @orig_arg points '$', which syncs to @ctx->offset */
915d157d769SMasami Hiramatsu (Google) static int parse_probe_vars(char *orig_arg, const struct fetch_type *t,
916d157d769SMasami Hiramatsu (Google) 			    struct fetch_insn **pcode,
917d157d769SMasami Hiramatsu (Google) 			    struct fetch_insn *end,
9181b8b0cd7SMasami Hiramatsu (Google) 			    struct traceprobe_parse_context *ctx)
9198ab83f56SSrikar Dronamraju {
920d157d769SMasami Hiramatsu (Google) 	struct fetch_insn *code = *pcode;
9211b8b0cd7SMasami Hiramatsu (Google) 	int err = TP_ERR_BAD_VAR;
922d157d769SMasami Hiramatsu (Google) 	char *arg = orig_arg + 1;
923d157d769SMasami Hiramatsu (Google) 	unsigned long param;
9243d739c1fSSteven Rostedt (VMware) 	int ret = 0;
9253d739c1fSSteven Rostedt (VMware) 	int len;
9268ab83f56SSrikar Dronamraju 
9271b8b0cd7SMasami Hiramatsu (Google) 	if (ctx->flags & TPARG_FL_TEVENT) {
9282673c60eSSteven Rostedt (Google) 		if (code->data)
9292673c60eSSteven Rostedt (Google) 			return -EFAULT;
9301b8b0cd7SMasami Hiramatsu (Google) 		ret = parse_trace_event_arg(arg, code, ctx);
9311b8b0cd7SMasami Hiramatsu (Google) 		if (!ret)
9321b8b0cd7SMasami Hiramatsu (Google) 			return 0;
9331b8b0cd7SMasami Hiramatsu (Google) 		if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
9341b8b0cd7SMasami Hiramatsu (Google) 			code->op = FETCH_OP_COMM;
9351b8b0cd7SMasami Hiramatsu (Google) 			return 0;
936ab105a4fSMasami Hiramatsu 		}
9371b8b0cd7SMasami Hiramatsu (Google) 		/* backward compatibility */
9381b8b0cd7SMasami Hiramatsu (Google) 		ctx->offset = 0;
9391b8b0cd7SMasami Hiramatsu (Google) 		goto inval;
9401b8b0cd7SMasami Hiramatsu (Google) 	}
9411b8b0cd7SMasami Hiramatsu (Google) 
942d157d769SMasami Hiramatsu (Google) 	if (str_has_prefix(arg, "retval")) {
943d157d769SMasami Hiramatsu (Google) 		if (!(ctx->flags & TPARG_FL_RETURN)) {
944d157d769SMasami Hiramatsu (Google) 			err = TP_ERR_RETVAL_ON_PROBE;
945fd26290eSMasami Hiramatsu (Google) 			goto inval;
946fd26290eSMasami Hiramatsu (Google) 		}
947d157d769SMasami Hiramatsu (Google) 		if (!(ctx->flags & TPARG_FL_KERNEL) ||
948d157d769SMasami Hiramatsu (Google) 		    !IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS)) {
9491b8b0cd7SMasami Hiramatsu (Google) 			code->op = FETCH_OP_RETVAL;
9501b8b0cd7SMasami Hiramatsu (Google) 			return 0;
9511b8b0cd7SMasami Hiramatsu (Google) 		}
952d157d769SMasami Hiramatsu (Google) 		return parse_btf_arg(orig_arg, pcode, end, ctx);
9531b8b0cd7SMasami Hiramatsu (Google) 	}
9541b8b0cd7SMasami Hiramatsu (Google) 
9551b8b0cd7SMasami Hiramatsu (Google) 	len = str_has_prefix(arg, "stack");
9561b8b0cd7SMasami Hiramatsu (Google) 	if (len) {
9571b8b0cd7SMasami Hiramatsu (Google) 
9583d739c1fSSteven Rostedt (VMware) 		if (arg[len] == '\0') {
95953305928SMasami Hiramatsu 			code->op = FETCH_OP_STACKP;
9601b8b0cd7SMasami Hiramatsu (Google) 			return 0;
9611b8b0cd7SMasami Hiramatsu (Google) 		}
9621b8b0cd7SMasami Hiramatsu (Google) 
9631b8b0cd7SMasami Hiramatsu (Google) 		if (isdigit(arg[len])) {
9643d739c1fSSteven Rostedt (VMware) 			ret = kstrtoul(arg + len, 10, &param);
9651b8b0cd7SMasami Hiramatsu (Google) 			if (ret)
9661b8b0cd7SMasami Hiramatsu (Google) 				goto inval;
9671b8b0cd7SMasami Hiramatsu (Google) 
9681b8b0cd7SMasami Hiramatsu (Google) 			if ((ctx->flags & TPARG_FL_KERNEL) &&
969ab105a4fSMasami Hiramatsu 			    param > PARAM_MAX_STACK) {
9701b8b0cd7SMasami Hiramatsu (Google) 				err = TP_ERR_BAD_STACK_NUM;
9711b8b0cd7SMasami Hiramatsu (Google) 				goto inval;
9721b8b0cd7SMasami Hiramatsu (Google) 			}
97353305928SMasami Hiramatsu 			code->op = FETCH_OP_STACK;
97453305928SMasami Hiramatsu 			code->param = (unsigned int)param;
9751b8b0cd7SMasami Hiramatsu (Google) 			return 0;
9768ab83f56SSrikar Dronamraju 		}
9771b8b0cd7SMasami Hiramatsu (Google) 		goto inval;
9781b8b0cd7SMasami Hiramatsu (Google) 	}
9791b8b0cd7SMasami Hiramatsu (Google) 
9801b8b0cd7SMasami Hiramatsu (Google) 	if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
98153305928SMasami Hiramatsu 		code->op = FETCH_OP_COMM;
9821b8b0cd7SMasami Hiramatsu (Google) 		return 0;
983ab105a4fSMasami Hiramatsu 	}
9841b8b0cd7SMasami Hiramatsu (Google) 
9851b8b0cd7SMasami Hiramatsu (Google) #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
9861b8b0cd7SMasami Hiramatsu (Google) 	len = str_has_prefix(arg, "arg");
98725f00e40SMasami Hiramatsu (Google) 	if (len) {
9881b8b0cd7SMasami Hiramatsu (Google) 		ret = kstrtoul(arg + len, 10, &param);
9891b8b0cd7SMasami Hiramatsu (Google) 		if (ret)
9901b8b0cd7SMasami Hiramatsu (Google) 			goto inval;
9911b8b0cd7SMasami Hiramatsu (Google) 
9921b8b0cd7SMasami Hiramatsu (Google) 		if (!param || param > PARAM_MAX_STACK) {
9931b8b0cd7SMasami Hiramatsu (Google) 			err = TP_ERR_BAD_ARG_NUM;
9941b8b0cd7SMasami Hiramatsu (Google) 			goto inval;
9951b8b0cd7SMasami Hiramatsu (Google) 		}
99625f00e40SMasami Hiramatsu (Google) 		param--; /* argN starts from 1, but internal arg[N] starts from 0 */
9971b8b0cd7SMasami Hiramatsu (Google) 
99825f00e40SMasami Hiramatsu (Google) 		if (tparg_is_function_entry(ctx->flags)) {
999a1303af5SMasami Hiramatsu 			code->op = FETCH_OP_ARG;
100025f00e40SMasami Hiramatsu (Google) 			code->param = (unsigned int)param;
1001e2d0d7b2SMasami Hiramatsu (Google) 			/*
1002e2d0d7b2SMasami Hiramatsu (Google) 			 * The tracepoint probe will probe a stub function, and the
1003e2d0d7b2SMasami Hiramatsu (Google) 			 * first parameter of the stub is a dummy and should be ignored.
1004e2d0d7b2SMasami Hiramatsu (Google) 			 */
10051b8b0cd7SMasami Hiramatsu (Google) 			if (ctx->flags & TPARG_FL_TPOINT)
1006e2d0d7b2SMasami Hiramatsu (Google) 				code->param++;
100725f00e40SMasami Hiramatsu (Google) 		} else if (tparg_is_function_return(ctx->flags)) {
100825f00e40SMasami Hiramatsu (Google) 			/* function entry argument access from return probe */
100925f00e40SMasami Hiramatsu (Google) 			ret = __store_entry_arg(ctx->tp, param);
101025f00e40SMasami Hiramatsu (Google) 			if (ret < 0)	/* This error should be an internal error */
101125f00e40SMasami Hiramatsu (Google) 				return ret;
101225f00e40SMasami Hiramatsu (Google) 
101325f00e40SMasami Hiramatsu (Google) 			code->op = FETCH_OP_EDATA;
101425f00e40SMasami Hiramatsu (Google) 			code->offset = ret;
101525f00e40SMasami Hiramatsu (Google) 		} else {
101625f00e40SMasami Hiramatsu (Google) 			err = TP_ERR_NOFENTRY_ARGS;
101725f00e40SMasami Hiramatsu (Google) 			goto inval;
101825f00e40SMasami Hiramatsu (Google) 		}
10191b8b0cd7SMasami Hiramatsu (Google) 		return 0;
10201b8b0cd7SMasami Hiramatsu (Google) 	}
1021a1303af5SMasami Hiramatsu #endif
10228ab83f56SSrikar Dronamraju 
10231b8b0cd7SMasami Hiramatsu (Google) inval:
10241b8b0cd7SMasami Hiramatsu (Google) 	__trace_probe_log_err(ctx->offset, err);
1025ab105a4fSMasami Hiramatsu 	return -EINVAL;
10268ab83f56SSrikar Dronamraju }
10278ab83f56SSrikar Dronamraju 
str_to_immediate(char * str,unsigned long * imm)10286218bf9fSMasami Hiramatsu static int str_to_immediate(char *str, unsigned long *imm)
10296218bf9fSMasami Hiramatsu {
10306218bf9fSMasami Hiramatsu 	if (isdigit(str[0]))
10316218bf9fSMasami Hiramatsu 		return kstrtoul(str, 0, imm);
10326218bf9fSMasami Hiramatsu 	else if (str[0] == '-')
10336218bf9fSMasami Hiramatsu 		return kstrtol(str, 0, (long *)imm);
10346218bf9fSMasami Hiramatsu 	else if (str[0] == '+')
10356218bf9fSMasami Hiramatsu 		return kstrtol(str + 1, 0, (long *)imm);
10366218bf9fSMasami Hiramatsu 	return -EINVAL;
10376218bf9fSMasami Hiramatsu }
10386218bf9fSMasami Hiramatsu 
__parse_imm_string(char * str,char ** pbuf,int offs)1039a42e3c4dSMasami Hiramatsu static int __parse_imm_string(char *str, char **pbuf, int offs)
1040a42e3c4dSMasami Hiramatsu {
1041a42e3c4dSMasami Hiramatsu 	size_t len = strlen(str);
1042a42e3c4dSMasami Hiramatsu 
1043a42e3c4dSMasami Hiramatsu 	if (str[len - 1] != '"') {
1044a42e3c4dSMasami Hiramatsu 		trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE);
1045a42e3c4dSMasami Hiramatsu 		return -EINVAL;
1046a42e3c4dSMasami Hiramatsu 	}
1047a42e3c4dSMasami Hiramatsu 	*pbuf = kstrndup(str, len - 1, GFP_KERNEL);
10481c1857d4SXiaoke Wang 	if (!*pbuf)
10491c1857d4SXiaoke Wang 		return -ENOMEM;
1050a42e3c4dSMasami Hiramatsu 	return 0;
1051a42e3c4dSMasami Hiramatsu }
1052a42e3c4dSMasami Hiramatsu 
10538ab83f56SSrikar Dronamraju /* Recursive argument parser */
105453305928SMasami Hiramatsu static int
parse_probe_arg(char * arg,const struct fetch_type * type,struct fetch_insn ** pcode,struct fetch_insn * end,struct traceprobe_parse_context * ctx)105553305928SMasami Hiramatsu parse_probe_arg(char *arg, const struct fetch_type *type,
105653305928SMasami Hiramatsu 		struct fetch_insn **pcode, struct fetch_insn *end,
10571b8b0cd7SMasami Hiramatsu (Google) 		struct traceprobe_parse_context *ctx)
10588ab83f56SSrikar Dronamraju {
105953305928SMasami Hiramatsu 	struct fetch_insn *code = *pcode;
10608ab83f56SSrikar Dronamraju 	unsigned long param;
1061e65f7ae7SMasami Hiramatsu 	int deref = FETCH_OP_DEREF;
1062bf173ca9SSteven Rostedt (VMware) 	long offset = 0;
10638ab83f56SSrikar Dronamraju 	char *tmp;
106434fee3a1SNamhyung Kim 	int ret = 0;
10658ab83f56SSrikar Dronamraju 
10668ab83f56SSrikar Dronamraju 	switch (arg[0]) {
10678ab83f56SSrikar Dronamraju 	case '$':
1068d157d769SMasami Hiramatsu (Google) 		ret = parse_probe_vars(arg, type, pcode, end, ctx);
10698ab83f56SSrikar Dronamraju 		break;
10708ab83f56SSrikar Dronamraju 
10718ab83f56SSrikar Dronamraju 	case '%':	/* named register */
10721b8b0cd7SMasami Hiramatsu (Google) 		if (ctx->flags & (TPARG_FL_TEVENT | TPARG_FL_FPROBE)) {
1073334e5519SMasami Hiramatsu (Google) 			/* eprobe and fprobe do not handle registers */
10741b8b0cd7SMasami Hiramatsu (Google) 			trace_probe_log_err(ctx->offset, BAD_VAR);
10752673c60eSSteven Rostedt (Google) 			break;
10762673c60eSSteven Rostedt (Google) 		}
10778ab83f56SSrikar Dronamraju 		ret = regs_query_register_offset(arg + 1);
10788ab83f56SSrikar Dronamraju 		if (ret >= 0) {
107953305928SMasami Hiramatsu 			code->op = FETCH_OP_REG;
108053305928SMasami Hiramatsu 			code->param = (unsigned int)ret;
10818ab83f56SSrikar Dronamraju 			ret = 0;
1082ab105a4fSMasami Hiramatsu 		} else
10831b8b0cd7SMasami Hiramatsu (Google) 			trace_probe_log_err(ctx->offset, BAD_REG_NAME);
10848ab83f56SSrikar Dronamraju 		break;
10858ab83f56SSrikar Dronamraju 
1086b7e0bf34SNamhyung Kim 	case '@':	/* memory, file-offset or symbol */
10878ab83f56SSrikar Dronamraju 		if (isdigit(arg[1])) {
1088bcd83ea6SDaniel Walter 			ret = kstrtoul(arg + 1, 0, &param);
1089ab105a4fSMasami Hiramatsu 			if (ret) {
10901b8b0cd7SMasami Hiramatsu (Google) 				trace_probe_log_err(ctx->offset, BAD_MEM_ADDR);
10918ab83f56SSrikar Dronamraju 				break;
1092ab105a4fSMasami Hiramatsu 			}
109353305928SMasami Hiramatsu 			/* load address */
109453305928SMasami Hiramatsu 			code->op = FETCH_OP_IMM;
109553305928SMasami Hiramatsu 			code->immediate = param;
1096b7e0bf34SNamhyung Kim 		} else if (arg[1] == '+') {
1097b7e0bf34SNamhyung Kim 			/* kprobes don't support file offsets */
10981b8b0cd7SMasami Hiramatsu (Google) 			if (ctx->flags & TPARG_FL_KERNEL) {
10991b8b0cd7SMasami Hiramatsu (Google) 				trace_probe_log_err(ctx->offset, FILE_ON_KPROBE);
1100b7e0bf34SNamhyung Kim 				return -EINVAL;
1101ab105a4fSMasami Hiramatsu 			}
1102b7e0bf34SNamhyung Kim 			ret = kstrtol(arg + 2, 0, &offset);
1103ab105a4fSMasami Hiramatsu 			if (ret) {
11041b8b0cd7SMasami Hiramatsu (Google) 				trace_probe_log_err(ctx->offset, BAD_FILE_OFFS);
1105b7e0bf34SNamhyung Kim 				break;
1106ab105a4fSMasami Hiramatsu 			}
1107b7e0bf34SNamhyung Kim 
110853305928SMasami Hiramatsu 			code->op = FETCH_OP_FOFFS;
110953305928SMasami Hiramatsu 			code->immediate = (unsigned long)offset;  // imm64?
11108ab83f56SSrikar Dronamraju 		} else {
1111b079d374SNamhyung Kim 			/* uprobes don't support symbols */
11121b8b0cd7SMasami Hiramatsu (Google) 			if (!(ctx->flags & TPARG_FL_KERNEL)) {
11131b8b0cd7SMasami Hiramatsu (Google) 				trace_probe_log_err(ctx->offset, SYM_ON_UPROBE);
1114b079d374SNamhyung Kim 				return -EINVAL;
1115ab105a4fSMasami Hiramatsu 			}
1116a6682814SMasami Hiramatsu 			/* Preserve symbol for updating */
1117a6682814SMasami Hiramatsu 			code->op = FETCH_NOP_SYMBOL;
1118a6682814SMasami Hiramatsu 			code->data = kstrdup(arg + 1, GFP_KERNEL);
1119a6682814SMasami Hiramatsu 			if (!code->data)
1120a6682814SMasami Hiramatsu 				return -ENOMEM;
1121ab105a4fSMasami Hiramatsu 			if (++code == end) {
11221b8b0cd7SMasami Hiramatsu (Google) 				trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1123ab105a4fSMasami Hiramatsu 				return -EINVAL;
1124ab105a4fSMasami Hiramatsu 			}
112553305928SMasami Hiramatsu 			code->op = FETCH_OP_IMM;
1126a6682814SMasami Hiramatsu 			code->immediate = 0;
11278ab83f56SSrikar Dronamraju 		}
112853305928SMasami Hiramatsu 		/* These are fetching from memory */
1129ab105a4fSMasami Hiramatsu 		if (++code == end) {
11301b8b0cd7SMasami Hiramatsu (Google) 			trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1131ab105a4fSMasami Hiramatsu 			return -EINVAL;
1132ab105a4fSMasami Hiramatsu 		}
113353305928SMasami Hiramatsu 		*pcode = code;
113453305928SMasami Hiramatsu 		code->op = FETCH_OP_DEREF;
113553305928SMasami Hiramatsu 		code->offset = offset;
11368ab83f56SSrikar Dronamraju 		break;
11378ab83f56SSrikar Dronamraju 
11388ab83f56SSrikar Dronamraju 	case '+':	/* deref memory */
11398ab83f56SSrikar Dronamraju 	case '-':
1140e65f7ae7SMasami Hiramatsu 		if (arg[1] == 'u') {
1141e65f7ae7SMasami Hiramatsu 			deref = FETCH_OP_UDEREF;
1142e65f7ae7SMasami Hiramatsu 			arg[1] = arg[0];
1143e65f7ae7SMasami Hiramatsu 			arg++;
1144e65f7ae7SMasami Hiramatsu 		}
1145e65f7ae7SMasami Hiramatsu 		if (arg[0] == '+')
1146e65f7ae7SMasami Hiramatsu 			arg++;	/* Skip '+', because kstrtol() rejects it. */
11478ab83f56SSrikar Dronamraju 		tmp = strchr(arg, '(');
1148ab105a4fSMasami Hiramatsu 		if (!tmp) {
11491b8b0cd7SMasami Hiramatsu (Google) 			trace_probe_log_err(ctx->offset, DEREF_NEED_BRACE);
115053305928SMasami Hiramatsu 			return -EINVAL;
1151ab105a4fSMasami Hiramatsu 		}
11528ab83f56SSrikar Dronamraju 		*tmp = '\0';
1153bcd83ea6SDaniel Walter 		ret = kstrtol(arg, 0, &offset);
1154ab105a4fSMasami Hiramatsu 		if (ret) {
11551b8b0cd7SMasami Hiramatsu (Google) 			trace_probe_log_err(ctx->offset, BAD_DEREF_OFFS);
11568ab83f56SSrikar Dronamraju 			break;
1157ab105a4fSMasami Hiramatsu 		}
11581b8b0cd7SMasami Hiramatsu (Google) 		ctx->offset += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0);
11598ab83f56SSrikar Dronamraju 		arg = tmp + 1;
11608ab83f56SSrikar Dronamraju 		tmp = strrchr(arg, ')');
1161ab105a4fSMasami Hiramatsu 		if (!tmp) {
11621b8b0cd7SMasami Hiramatsu (Google) 			trace_probe_log_err(ctx->offset + strlen(arg),
1163ab105a4fSMasami Hiramatsu 					    DEREF_OPEN_BRACE);
1164ab105a4fSMasami Hiramatsu 			return -EINVAL;
1165ab105a4fSMasami Hiramatsu 		} else {
11661b8b0cd7SMasami Hiramatsu (Google) 			const struct fetch_type *t2 = find_fetch_type(NULL, ctx->flags);
11671b8b0cd7SMasami Hiramatsu (Google) 			int cur_offs = ctx->offset;
11688ab83f56SSrikar Dronamraju 
11698ab83f56SSrikar Dronamraju 			*tmp = '\0';
11701b8b0cd7SMasami Hiramatsu (Google) 			ret = parse_probe_arg(arg, t2, &code, end, ctx);
11718ab83f56SSrikar Dronamraju 			if (ret)
117253305928SMasami Hiramatsu 				break;
11731b8b0cd7SMasami Hiramatsu (Google) 			ctx->offset = cur_offs;
1174a42e3c4dSMasami Hiramatsu 			if (code->op == FETCH_OP_COMM ||
1175a42e3c4dSMasami Hiramatsu 			    code->op == FETCH_OP_DATA) {
11761b8b0cd7SMasami Hiramatsu (Google) 				trace_probe_log_err(ctx->offset, COMM_CANT_DEREF);
117753305928SMasami Hiramatsu 				return -EINVAL;
1178ab105a4fSMasami Hiramatsu 			}
1179ab105a4fSMasami Hiramatsu 			if (++code == end) {
11801b8b0cd7SMasami Hiramatsu (Google) 				trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1181ab105a4fSMasami Hiramatsu 				return -EINVAL;
1182ab105a4fSMasami Hiramatsu 			}
118353305928SMasami Hiramatsu 			*pcode = code;
118453305928SMasami Hiramatsu 
1185e65f7ae7SMasami Hiramatsu 			code->op = deref;
118653305928SMasami Hiramatsu 			code->offset = offset;
1187c440adfbSMasami Hiramatsu (Google) 			/* Reset the last type if used */
1188c440adfbSMasami Hiramatsu (Google) 			ctx->last_type = NULL;
11898ab83f56SSrikar Dronamraju 		}
11908ab83f56SSrikar Dronamraju 		break;
11916218bf9fSMasami Hiramatsu 	case '\\':	/* Immediate value */
1192a42e3c4dSMasami Hiramatsu 		if (arg[1] == '"') {	/* Immediate string */
11931b8b0cd7SMasami Hiramatsu (Google) 			ret = __parse_imm_string(arg + 2, &tmp, ctx->offset + 2);
1194a42e3c4dSMasami Hiramatsu 			if (ret)
1195a42e3c4dSMasami Hiramatsu 				break;
1196a42e3c4dSMasami Hiramatsu 			code->op = FETCH_OP_DATA;
1197a42e3c4dSMasami Hiramatsu 			code->data = tmp;
1198a42e3c4dSMasami Hiramatsu 		} else {
11996218bf9fSMasami Hiramatsu 			ret = str_to_immediate(arg + 1, &code->immediate);
12006218bf9fSMasami Hiramatsu 			if (ret)
12011b8b0cd7SMasami Hiramatsu (Google) 				trace_probe_log_err(ctx->offset + 1, BAD_IMM);
12026218bf9fSMasami Hiramatsu 			else
12036218bf9fSMasami Hiramatsu 				code->op = FETCH_OP_IMM;
1204a42e3c4dSMasami Hiramatsu 		}
12056218bf9fSMasami Hiramatsu 		break;
1206b576e097SMasami Hiramatsu (Google) 	default:
1207b576e097SMasami Hiramatsu (Google) 		if (isalpha(arg[0]) || arg[0] == '_') {	/* BTF variable */
120825f00e40SMasami Hiramatsu (Google) 			if (!tparg_is_function_entry(ctx->flags) &&
120925f00e40SMasami Hiramatsu (Google) 			    !tparg_is_function_return(ctx->flags)) {
1210b576e097SMasami Hiramatsu (Google) 				trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
1211b576e097SMasami Hiramatsu (Google) 				return -EINVAL;
1212b576e097SMasami Hiramatsu (Google) 			}
1213c440adfbSMasami Hiramatsu (Google) 			ret = parse_btf_arg(arg, pcode, end, ctx);
1214b576e097SMasami Hiramatsu (Google) 			break;
1215b576e097SMasami Hiramatsu (Google) 		}
12168ab83f56SSrikar Dronamraju 	}
121753305928SMasami Hiramatsu 	if (!ret && code->op == FETCH_OP_NOP) {
121853305928SMasami Hiramatsu 		/* Parsed, but do not find fetch method */
12191b8b0cd7SMasami Hiramatsu (Google) 		trace_probe_log_err(ctx->offset, BAD_FETCH_ARG);
12208ab83f56SSrikar Dronamraju 		ret = -EINVAL;
12218ab83f56SSrikar Dronamraju 	}
12228ab83f56SSrikar Dronamraju 	return ret;
12238ab83f56SSrikar Dronamraju }
12248ab83f56SSrikar Dronamraju 
12258ab83f56SSrikar Dronamraju /* Bitfield type needs to be parsed into a fetch function */
__parse_bitfield_probe_arg(const char * bf,const struct fetch_type * t,struct fetch_insn ** pcode)12268ab83f56SSrikar Dronamraju static int __parse_bitfield_probe_arg(const char *bf,
12278ab83f56SSrikar Dronamraju 				      const struct fetch_type *t,
122853305928SMasami Hiramatsu 				      struct fetch_insn **pcode)
12298ab83f56SSrikar Dronamraju {
123053305928SMasami Hiramatsu 	struct fetch_insn *code = *pcode;
12318ab83f56SSrikar Dronamraju 	unsigned long bw, bo;
12328ab83f56SSrikar Dronamraju 	char *tail;
12338ab83f56SSrikar Dronamraju 
12348ab83f56SSrikar Dronamraju 	if (*bf != 'b')
12358ab83f56SSrikar Dronamraju 		return 0;
12368ab83f56SSrikar Dronamraju 
12378ab83f56SSrikar Dronamraju 	bw = simple_strtoul(bf + 1, &tail, 0);	/* Use simple one */
12388ab83f56SSrikar Dronamraju 
12398ab83f56SSrikar Dronamraju 	if (bw == 0 || *tail != '@')
12408ab83f56SSrikar Dronamraju 		return -EINVAL;
12418ab83f56SSrikar Dronamraju 
12428ab83f56SSrikar Dronamraju 	bf = tail + 1;
12438ab83f56SSrikar Dronamraju 	bo = simple_strtoul(bf, &tail, 0);
12448ab83f56SSrikar Dronamraju 
12458ab83f56SSrikar Dronamraju 	if (tail == bf || *tail != '/')
12468ab83f56SSrikar Dronamraju 		return -EINVAL;
124753305928SMasami Hiramatsu 	code++;
124853305928SMasami Hiramatsu 	if (code->op != FETCH_OP_NOP)
1249ab105a4fSMasami Hiramatsu 		return -EINVAL;
125053305928SMasami Hiramatsu 	*pcode = code;
12518ab83f56SSrikar Dronamraju 
125253305928SMasami Hiramatsu 	code->op = FETCH_OP_MOD_BF;
125353305928SMasami Hiramatsu 	code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
125453305928SMasami Hiramatsu 	code->rshift = BYTES_TO_BITS(t->size) - bw;
125553305928SMasami Hiramatsu 	code->basesize = t->size;
12568ab83f56SSrikar Dronamraju 
12578ab83f56SSrikar Dronamraju 	return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
12588ab83f56SSrikar Dronamraju }
12598ab83f56SSrikar Dronamraju 
1260032330abSMasami Hiramatsu (Google) /* Split type part from @arg and return it. */
parse_probe_arg_type(char * arg,struct probe_arg * parg,struct traceprobe_parse_context * ctx)1261032330abSMasami Hiramatsu (Google) static char *parse_probe_arg_type(char *arg, struct probe_arg *parg,
12621b8b0cd7SMasami Hiramatsu (Google) 				  struct traceprobe_parse_context *ctx)
12638ab83f56SSrikar Dronamraju {
1264032330abSMasami Hiramatsu (Google) 	char *t = NULL, *t2, *t3;
1265032330abSMasami Hiramatsu (Google) 	int offs;
12668ab83f56SSrikar Dronamraju 
126740b53b77SMasami Hiramatsu 	t = strchr(arg, ':');
12688ab83f56SSrikar Dronamraju 	if (t) {
1269032330abSMasami Hiramatsu (Google) 		*t++ = '\0';
1270032330abSMasami Hiramatsu (Google) 		t2 = strchr(t, '[');
127140b53b77SMasami Hiramatsu 		if (t2) {
1272ab105a4fSMasami Hiramatsu 			*t2++ = '\0';
1273ab105a4fSMasami Hiramatsu 			t3 = strchr(t2, ']');
1274ab105a4fSMasami Hiramatsu 			if (!t3) {
1275032330abSMasami Hiramatsu (Google) 				offs = t2 + strlen(t2) - arg;
12761b8b0cd7SMasami Hiramatsu (Google) 
12771b8b0cd7SMasami Hiramatsu (Google) 				trace_probe_log_err(ctx->offset + offs,
1278ab105a4fSMasami Hiramatsu 						    ARRAY_NO_CLOSE);
1279032330abSMasami Hiramatsu (Google) 				return ERR_PTR(-EINVAL);
1280ab105a4fSMasami Hiramatsu 			} else if (t3[1] != '\0') {
12811b8b0cd7SMasami Hiramatsu (Google) 				trace_probe_log_err(ctx->offset + t3 + 1 - arg,
1282ab105a4fSMasami Hiramatsu 						    BAD_ARRAY_SUFFIX);
1283032330abSMasami Hiramatsu (Google) 				return ERR_PTR(-EINVAL);
1284ab105a4fSMasami Hiramatsu 			}
1285ab105a4fSMasami Hiramatsu 			*t3 = '\0';
1286ab105a4fSMasami Hiramatsu 			if (kstrtouint(t2, 0, &parg->count) || !parg->count) {
12871b8b0cd7SMasami Hiramatsu (Google) 				trace_probe_log_err(ctx->offset + t2 - arg,
1288ab105a4fSMasami Hiramatsu 						    BAD_ARRAY_NUM);
1289032330abSMasami Hiramatsu (Google) 				return ERR_PTR(-EINVAL);
1290ab105a4fSMasami Hiramatsu 			}
1291ab105a4fSMasami Hiramatsu 			if (parg->count > MAX_ARRAY_LEN) {
12921b8b0cd7SMasami Hiramatsu (Google) 				trace_probe_log_err(ctx->offset + t2 - arg,
1293ab105a4fSMasami Hiramatsu 						    ARRAY_TOO_BIG);
1294032330abSMasami Hiramatsu (Google) 				return ERR_PTR(-EINVAL);
1295ab105a4fSMasami Hiramatsu 			}
129640b53b77SMasami Hiramatsu 		}
12978ab83f56SSrikar Dronamraju 	}
1298032330abSMasami Hiramatsu (Google) 	offs = t ? t - arg : 0;
12993dd1f7f2SMasami Hiramatsu 
1300a42e3c4dSMasami Hiramatsu 	/*
1301f2cc020dSIngo Molnar 	 * Since $comm and immediate string can not be dereferenced,
130202333de9SSteven Rostedt (Google) 	 * we can find those by strcmp. But ignore for eprobes.
1303a42e3c4dSMasami Hiramatsu 	 */
13041b8b0cd7SMasami Hiramatsu (Google) 	if (!(ctx->flags & TPARG_FL_TEVENT) &&
1305ab838444SSteven Rostedt (Google) 	    (strcmp(arg, "$comm") == 0 || strcmp(arg, "$COMM") == 0 ||
1306ab838444SSteven Rostedt (Google) 	     strncmp(arg, "\\\"", 2) == 0)) {
13078c427cc2SMasami Hiramatsu (Google) 		/* The type of $comm must be "string", and not an array type. */
13088c427cc2SMasami Hiramatsu (Google) 		if (parg->count || (t && strcmp(t, "string"))) {
1309032330abSMasami Hiramatsu (Google) 			trace_probe_log_err(ctx->offset + offs, NEED_STRING_TYPE);
1310032330abSMasami Hiramatsu (Google) 			return ERR_PTR(-EINVAL);
13118c427cc2SMasami Hiramatsu (Google) 		}
13121b8b0cd7SMasami Hiramatsu (Google) 		parg->type = find_fetch_type("string", ctx->flags);
13133dd1f7f2SMasami Hiramatsu 	} else
13141b8b0cd7SMasami Hiramatsu (Google) 		parg->type = find_fetch_type(t, ctx->flags);
1315032330abSMasami Hiramatsu (Google) 
13168ab83f56SSrikar Dronamraju 	if (!parg->type) {
1317032330abSMasami Hiramatsu (Google) 		trace_probe_log_err(ctx->offset + offs, BAD_TYPE);
1318032330abSMasami Hiramatsu (Google) 		return ERR_PTR(-EINVAL);
13198ab83f56SSrikar Dronamraju 	}
13208ab83f56SSrikar Dronamraju 
1321032330abSMasami Hiramatsu (Google) 	return t;
13229a571c1eSMasami Hiramatsu (Google) }
1323b576e097SMasami Hiramatsu (Google) 
1324032330abSMasami Hiramatsu (Google) /* After parsing, adjust the fetch_insn according to the probe_arg */
finalize_fetch_insn(struct fetch_insn * code,struct probe_arg * parg,char * type,int type_offset,struct traceprobe_parse_context * ctx)1325032330abSMasami Hiramatsu (Google) static int finalize_fetch_insn(struct fetch_insn *code,
1326032330abSMasami Hiramatsu (Google) 			       struct probe_arg *parg,
1327032330abSMasami Hiramatsu (Google) 			       char *type,
1328032330abSMasami Hiramatsu (Google) 			       int type_offset,
1329032330abSMasami Hiramatsu (Google) 			       struct traceprobe_parse_context *ctx)
1330032330abSMasami Hiramatsu (Google) {
1331032330abSMasami Hiramatsu (Google) 	struct fetch_insn *scode;
1332032330abSMasami Hiramatsu (Google) 	int ret;
1333032330abSMasami Hiramatsu (Google) 
133453305928SMasami Hiramatsu 	/* Store operation */
1335b26a124cSMasami Hiramatsu (Google) 	if (parg->type->is_string) {
1336032330abSMasami Hiramatsu (Google) 		/* Check bad combination of the type and the last fetch_insn. */
1337b26a124cSMasami Hiramatsu (Google) 		if (!strcmp(parg->type->name, "symstr")) {
1338b26a124cSMasami Hiramatsu (Google) 			if (code->op != FETCH_OP_REG && code->op != FETCH_OP_STACK &&
1339b26a124cSMasami Hiramatsu (Google) 			    code->op != FETCH_OP_RETVAL && code->op != FETCH_OP_ARG &&
1340b26a124cSMasami Hiramatsu (Google) 			    code->op != FETCH_OP_DEREF && code->op != FETCH_OP_TP_ARG) {
1341032330abSMasami Hiramatsu (Google) 				trace_probe_log_err(ctx->offset + type_offset,
1342b26a124cSMasami Hiramatsu (Google) 						    BAD_SYMSTRING);
1343032330abSMasami Hiramatsu (Google) 				return -EINVAL;
1344b26a124cSMasami Hiramatsu (Google) 			}
1345b26a124cSMasami Hiramatsu (Google) 		} else {
1346e65f7ae7SMasami Hiramatsu 			if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF &&
1347a42e3c4dSMasami Hiramatsu 			    code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM &&
13487491e2c4STzvetomir Stoyanov (VMware) 			    code->op != FETCH_OP_DATA && code->op != FETCH_OP_TP_ARG) {
1349032330abSMasami Hiramatsu (Google) 				trace_probe_log_err(ctx->offset + type_offset,
1350ab105a4fSMasami Hiramatsu 						    BAD_STRING);
1351032330abSMasami Hiramatsu (Google) 				return -EINVAL;
13528ab83f56SSrikar Dronamraju 			}
1353b26a124cSMasami Hiramatsu (Google) 		}
1354032330abSMasami Hiramatsu (Google) 
1355b26a124cSMasami Hiramatsu (Google) 		if (!strcmp(parg->type->name, "symstr") ||
1356b26a124cSMasami Hiramatsu (Google) 		    (code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM ||
13577491e2c4STzvetomir Stoyanov (VMware) 		     code->op == FETCH_OP_DATA) || code->op == FETCH_OP_TP_ARG ||
13587491e2c4STzvetomir Stoyanov (VMware) 		     parg->count) {
135940b53b77SMasami Hiramatsu 			/*
1360a42e3c4dSMasami Hiramatsu 			 * IMM, DATA and COMM is pointing actual address, those
1361a42e3c4dSMasami Hiramatsu 			 * must be kept, and if parg->count != 0, this is an
1362a42e3c4dSMasami Hiramatsu 			 * array of string pointers instead of string address
1363a42e3c4dSMasami Hiramatsu 			 * itself.
1364b26a124cSMasami Hiramatsu (Google) 			 * For the symstr, it doesn't need to dereference, thus
1365b26a124cSMasami Hiramatsu (Google) 			 * it just get the value.
136640b53b77SMasami Hiramatsu 			 */
136753305928SMasami Hiramatsu 			code++;
136840b53b77SMasami Hiramatsu 			if (code->op != FETCH_OP_NOP) {
13691b8b0cd7SMasami Hiramatsu (Google) 				trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1370032330abSMasami Hiramatsu (Google) 				return -EINVAL;
137140b53b77SMasami Hiramatsu 			}
137240b53b77SMasami Hiramatsu 		}
1373032330abSMasami Hiramatsu (Google) 
137488903c46SMasami Hiramatsu 		/* If op == DEREF, replace it with STRING */
1375e65f7ae7SMasami Hiramatsu 		if (!strcmp(parg->type->name, "ustring") ||
1376e65f7ae7SMasami Hiramatsu 		    code->op == FETCH_OP_UDEREF)
137788903c46SMasami Hiramatsu 			code->op = FETCH_OP_ST_USTRING;
1378b26a124cSMasami Hiramatsu (Google) 		else if (!strcmp(parg->type->name, "symstr"))
1379b26a124cSMasami Hiramatsu (Google) 			code->op = FETCH_OP_ST_SYMSTR;
138088903c46SMasami Hiramatsu 		else
138188903c46SMasami Hiramatsu 			code->op = FETCH_OP_ST_STRING;
138240b53b77SMasami Hiramatsu 		code->size = parg->type->size;
138353305928SMasami Hiramatsu 		parg->dynamic = true;
138453305928SMasami Hiramatsu 	} else if (code->op == FETCH_OP_DEREF) {
138553305928SMasami Hiramatsu 		code->op = FETCH_OP_ST_MEM;
138653305928SMasami Hiramatsu 		code->size = parg->type->size;
1387e65f7ae7SMasami Hiramatsu 	} else if (code->op == FETCH_OP_UDEREF) {
1388e65f7ae7SMasami Hiramatsu 		code->op = FETCH_OP_ST_UMEM;
1389e65f7ae7SMasami Hiramatsu 		code->size = parg->type->size;
139053305928SMasami Hiramatsu 	} else {
139153305928SMasami Hiramatsu 		code++;
139253305928SMasami Hiramatsu 		if (code->op != FETCH_OP_NOP) {
13931b8b0cd7SMasami Hiramatsu (Google) 			trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1394032330abSMasami Hiramatsu (Google) 			return -E2BIG;
139553305928SMasami Hiramatsu 		}
139653305928SMasami Hiramatsu 		code->op = FETCH_OP_ST_RAW;
139753305928SMasami Hiramatsu 		code->size = parg->type->size;
139853305928SMasami Hiramatsu 	}
1399032330abSMasami Hiramatsu (Google) 
1400032330abSMasami Hiramatsu (Google) 	/* Save storing fetch_insn. */
140140b53b77SMasami Hiramatsu 	scode = code;
1402032330abSMasami Hiramatsu (Google) 
140353305928SMasami Hiramatsu 	/* Modify operation */
1404032330abSMasami Hiramatsu (Google) 	if (type != NULL) {
1405032330abSMasami Hiramatsu (Google) 		/* Bitfield needs a special fetch_insn. */
1406032330abSMasami Hiramatsu (Google) 		ret = __parse_bitfield_probe_arg(type, parg->type, &code);
1407ab105a4fSMasami Hiramatsu 		if (ret) {
1408032330abSMasami Hiramatsu (Google) 			trace_probe_log_err(ctx->offset + type_offset, BAD_BITFIELD);
1409032330abSMasami Hiramatsu (Google) 			return ret;
141053305928SMasami Hiramatsu 		}
1411c440adfbSMasami Hiramatsu (Google) 	} else if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) &&
1412c440adfbSMasami Hiramatsu (Google) 		   ctx->last_type) {
1413032330abSMasami Hiramatsu (Google) 		/* If user not specified the type, try parsing BTF bitfield. */
1414c440adfbSMasami Hiramatsu (Google) 		ret = parse_btf_bitfield(&code, ctx);
1415c440adfbSMasami Hiramatsu (Google) 		if (ret)
1416032330abSMasami Hiramatsu (Google) 			return ret;
1417ab105a4fSMasami Hiramatsu 	}
1418032330abSMasami Hiramatsu (Google) 
141940b53b77SMasami Hiramatsu 	/* Loop(Array) operation */
142040b53b77SMasami Hiramatsu 	if (parg->count) {
142140b53b77SMasami Hiramatsu 		if (scode->op != FETCH_OP_ST_MEM &&
142288903c46SMasami Hiramatsu 		    scode->op != FETCH_OP_ST_STRING &&
142388903c46SMasami Hiramatsu 		    scode->op != FETCH_OP_ST_USTRING) {
1424032330abSMasami Hiramatsu (Google) 			trace_probe_log_err(ctx->offset + type_offset, BAD_STRING);
1425032330abSMasami Hiramatsu (Google) 			return -EINVAL;
142640b53b77SMasami Hiramatsu 		}
142740b53b77SMasami Hiramatsu 		code++;
142840b53b77SMasami Hiramatsu 		if (code->op != FETCH_OP_NOP) {
14291b8b0cd7SMasami Hiramatsu (Google) 			trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1430032330abSMasami Hiramatsu (Google) 			return -E2BIG;
143140b53b77SMasami Hiramatsu 		}
143240b53b77SMasami Hiramatsu 		code->op = FETCH_OP_LP_ARRAY;
143340b53b77SMasami Hiramatsu 		code->param = parg->count;
143440b53b77SMasami Hiramatsu 	}
1435032330abSMasami Hiramatsu (Google) 
1436032330abSMasami Hiramatsu (Google) 	/* Finalize the fetch_insn array. */
143753305928SMasami Hiramatsu 	code++;
143853305928SMasami Hiramatsu 	code->op = FETCH_OP_END;
143953305928SMasami Hiramatsu 
1440032330abSMasami Hiramatsu (Google) 	return 0;
1441032330abSMasami Hiramatsu (Google) }
1442032330abSMasami Hiramatsu (Google) 
1443032330abSMasami Hiramatsu (Google) /* String length checking wrapper */
traceprobe_parse_probe_arg_body(const char * argv,ssize_t * size,struct probe_arg * parg,struct traceprobe_parse_context * ctx)1444032330abSMasami Hiramatsu (Google) static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size,
1445032330abSMasami Hiramatsu (Google) 					   struct probe_arg *parg,
1446032330abSMasami Hiramatsu (Google) 					   struct traceprobe_parse_context *ctx)
1447032330abSMasami Hiramatsu (Google) {
1448032330abSMasami Hiramatsu (Google) 	struct fetch_insn *code, *tmp = NULL;
14494af0532aSMasami Hiramatsu (Google) 	char *type, *arg __free(kfree) = NULL;
1450032330abSMasami Hiramatsu (Google) 	int ret, len;
1451032330abSMasami Hiramatsu (Google) 
1452032330abSMasami Hiramatsu (Google) 	len = strlen(argv);
1453032330abSMasami Hiramatsu (Google) 	if (len > MAX_ARGSTR_LEN) {
1454032330abSMasami Hiramatsu (Google) 		trace_probe_log_err(ctx->offset, ARG_TOO_LONG);
1455032330abSMasami Hiramatsu (Google) 		return -E2BIG;
1456032330abSMasami Hiramatsu (Google) 	} else if (len == 0) {
1457032330abSMasami Hiramatsu (Google) 		trace_probe_log_err(ctx->offset, NO_ARG_BODY);
1458032330abSMasami Hiramatsu (Google) 		return -EINVAL;
1459032330abSMasami Hiramatsu (Google) 	}
1460032330abSMasami Hiramatsu (Google) 
1461032330abSMasami Hiramatsu (Google) 	arg = kstrdup(argv, GFP_KERNEL);
1462032330abSMasami Hiramatsu (Google) 	if (!arg)
1463032330abSMasami Hiramatsu (Google) 		return -ENOMEM;
1464032330abSMasami Hiramatsu (Google) 
1465032330abSMasami Hiramatsu (Google) 	parg->comm = kstrdup(arg, GFP_KERNEL);
14664af0532aSMasami Hiramatsu (Google) 	if (!parg->comm)
14674af0532aSMasami Hiramatsu (Google) 		return -ENOMEM;
1468032330abSMasami Hiramatsu (Google) 
1469032330abSMasami Hiramatsu (Google) 	type = parse_probe_arg_type(arg, parg, ctx);
14704af0532aSMasami Hiramatsu (Google) 	if (IS_ERR(type))
14714af0532aSMasami Hiramatsu (Google) 		return PTR_ERR(type);
1472032330abSMasami Hiramatsu (Google) 
1473032330abSMasami Hiramatsu (Google) 	code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL);
14744af0532aSMasami Hiramatsu (Google) 	if (!code)
14754af0532aSMasami Hiramatsu (Google) 		return -ENOMEM;
1476032330abSMasami Hiramatsu (Google) 	code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
1477032330abSMasami Hiramatsu (Google) 
1478032330abSMasami Hiramatsu (Google) 	ctx->last_type = NULL;
1479032330abSMasami Hiramatsu (Google) 	ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
1480032330abSMasami Hiramatsu (Google) 			      ctx);
1481032330abSMasami Hiramatsu (Google) 	if (ret < 0)
1482032330abSMasami Hiramatsu (Google) 		goto fail;
1483032330abSMasami Hiramatsu (Google) 
1484032330abSMasami Hiramatsu (Google) 	/* Update storing type if BTF is available */
1485032330abSMasami Hiramatsu (Google) 	if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) &&
1486032330abSMasami Hiramatsu (Google) 	    ctx->last_type) {
1487032330abSMasami Hiramatsu (Google) 		if (!type) {
1488032330abSMasami Hiramatsu (Google) 			parg->type = find_fetch_type_from_btf_type(ctx);
1489032330abSMasami Hiramatsu (Google) 		} else if (strstr(type, "string")) {
1490032330abSMasami Hiramatsu (Google) 			ret = check_prepare_btf_string_fetch(type, &code, ctx);
1491032330abSMasami Hiramatsu (Google) 			if (ret)
1492032330abSMasami Hiramatsu (Google) 				goto fail;
1493032330abSMasami Hiramatsu (Google) 		}
1494032330abSMasami Hiramatsu (Google) 	}
1495032330abSMasami Hiramatsu (Google) 	parg->offset = *size;
1496032330abSMasami Hiramatsu (Google) 	*size += parg->type->size * (parg->count ?: 1);
1497032330abSMasami Hiramatsu (Google) 
1498032330abSMasami Hiramatsu (Google) 	if (parg->count) {
1499032330abSMasami Hiramatsu (Google) 		len = strlen(parg->type->fmttype) + 6;
1500032330abSMasami Hiramatsu (Google) 		parg->fmt = kmalloc(len, GFP_KERNEL);
1501032330abSMasami Hiramatsu (Google) 		if (!parg->fmt) {
1502032330abSMasami Hiramatsu (Google) 			ret = -ENOMEM;
1503dce36962SLuMingYin 			goto fail;
1504032330abSMasami Hiramatsu (Google) 		}
1505032330abSMasami Hiramatsu (Google) 		snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
1506032330abSMasami Hiramatsu (Google) 			 parg->count);
1507032330abSMasami Hiramatsu (Google) 	}
1508032330abSMasami Hiramatsu (Google) 
1509032330abSMasami Hiramatsu (Google) 	ret = finalize_fetch_insn(code, parg, type, type ? type - arg : 0, ctx);
1510032330abSMasami Hiramatsu (Google) 	if (ret < 0)
1511032330abSMasami Hiramatsu (Google) 		goto fail;
1512032330abSMasami Hiramatsu (Google) 
1513032330abSMasami Hiramatsu (Google) 	for (; code < tmp + FETCH_INSN_MAX; code++)
1514032330abSMasami Hiramatsu (Google) 		if (code->op == FETCH_OP_END)
1515032330abSMasami Hiramatsu (Google) 			break;
151653305928SMasami Hiramatsu 	/* Shrink down the code buffer */
15178623b006SGustavo A. R. Silva 	parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL);
151853305928SMasami Hiramatsu 	if (!parg->code)
151953305928SMasami Hiramatsu 		ret = -ENOMEM;
152053305928SMasami Hiramatsu 	else
152153305928SMasami Hiramatsu 		memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
152253305928SMasami Hiramatsu 
152353305928SMasami Hiramatsu fail:
1524032330abSMasami Hiramatsu (Google) 	if (ret < 0) {
1525a6682814SMasami Hiramatsu 		for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
1526a42e3c4dSMasami Hiramatsu 			if (code->op == FETCH_NOP_SYMBOL ||
1527a42e3c4dSMasami Hiramatsu 			    code->op == FETCH_OP_DATA)
1528a6682814SMasami Hiramatsu 				kfree(code->data);
1529a6682814SMasami Hiramatsu 	}
153053305928SMasami Hiramatsu 	kfree(tmp);
15318ab83f56SSrikar Dronamraju 
15328ab83f56SSrikar Dronamraju 	return ret;
15338ab83f56SSrikar Dronamraju }
15348ab83f56SSrikar Dronamraju 
15358ab83f56SSrikar Dronamraju /* Return 1 if name is reserved or already used by another argument */
traceprobe_conflict_field_name(const char * name,struct probe_arg * args,int narg)1536d00bbea9SMasami Hiramatsu static int traceprobe_conflict_field_name(const char *name,
15378ab83f56SSrikar Dronamraju 					  struct probe_arg *args, int narg)
15388ab83f56SSrikar Dronamraju {
15398ab83f56SSrikar Dronamraju 	int i;
15408ab83f56SSrikar Dronamraju 
15418ab83f56SSrikar Dronamraju 	for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
15428ab83f56SSrikar Dronamraju 		if (strcmp(reserved_field_names[i], name) == 0)
15438ab83f56SSrikar Dronamraju 			return 1;
15448ab83f56SSrikar Dronamraju 
15458ab83f56SSrikar Dronamraju 	for (i = 0; i < narg; i++)
15468ab83f56SSrikar Dronamraju 		if (strcmp(args[i].name, name) == 0)
15478ab83f56SSrikar Dronamraju 			return 1;
15488ab83f56SSrikar Dronamraju 
15498ab83f56SSrikar Dronamraju 	return 0;
15508ab83f56SSrikar Dronamraju }
15518ab83f56SSrikar Dronamraju 
generate_probe_arg_name(const char * arg,int idx)1552b576e097SMasami Hiramatsu (Google) static char *generate_probe_arg_name(const char *arg, int idx)
1553b576e097SMasami Hiramatsu (Google) {
1554b576e097SMasami Hiramatsu (Google) 	char *name = NULL;
1555b576e097SMasami Hiramatsu (Google) 	const char *end;
1556b576e097SMasami Hiramatsu (Google) 
1557b576e097SMasami Hiramatsu (Google) 	/*
1558b576e097SMasami Hiramatsu (Google) 	 * If argument name is omitted, try arg as a name (BTF variable)
1559b576e097SMasami Hiramatsu (Google) 	 * or "argN".
1560b576e097SMasami Hiramatsu (Google) 	 */
1561b576e097SMasami Hiramatsu (Google) 	if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS)) {
1562b576e097SMasami Hiramatsu (Google) 		end = strchr(arg, ':');
1563b576e097SMasami Hiramatsu (Google) 		if (!end)
1564b576e097SMasami Hiramatsu (Google) 			end = arg + strlen(arg);
1565b576e097SMasami Hiramatsu (Google) 
1566b576e097SMasami Hiramatsu (Google) 		name = kmemdup_nul(arg, end - arg, GFP_KERNEL);
1567b576e097SMasami Hiramatsu (Google) 		if (!name || !is_good_name(name)) {
1568b576e097SMasami Hiramatsu (Google) 			kfree(name);
1569b576e097SMasami Hiramatsu (Google) 			name = NULL;
1570b576e097SMasami Hiramatsu (Google) 		}
1571b576e097SMasami Hiramatsu (Google) 	}
1572b576e097SMasami Hiramatsu (Google) 
1573b576e097SMasami Hiramatsu (Google) 	if (!name)
1574b576e097SMasami Hiramatsu (Google) 		name = kasprintf(GFP_KERNEL, "arg%d", idx + 1);
1575b576e097SMasami Hiramatsu (Google) 
1576b576e097SMasami Hiramatsu (Google) 	return name;
1577b576e097SMasami Hiramatsu (Google) }
1578b576e097SMasami Hiramatsu (Google) 
traceprobe_parse_probe_arg(struct trace_probe * tp,int i,const char * arg,struct traceprobe_parse_context * ctx)1579fcd9db51SSteven Rostedt (VMware) int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg,
15801b8b0cd7SMasami Hiramatsu (Google) 			       struct traceprobe_parse_context *ctx)
1581d00bbea9SMasami Hiramatsu {
1582d00bbea9SMasami Hiramatsu 	struct probe_arg *parg = &tp->args[i];
1583fcd9db51SSteven Rostedt (VMware) 	const char *body;
1584d00bbea9SMasami Hiramatsu 
158525f00e40SMasami Hiramatsu (Google) 	ctx->tp = tp;
1586d00bbea9SMasami Hiramatsu 	body = strchr(arg, '=');
1587d00bbea9SMasami Hiramatsu 	if (body) {
1588ab105a4fSMasami Hiramatsu 		if (body - arg > MAX_ARG_NAME_LEN) {
1589ab105a4fSMasami Hiramatsu 			trace_probe_log_err(0, ARG_NAME_TOO_LONG);
1590b4443c17SMasami Hiramatsu 			return -EINVAL;
1591ab105a4fSMasami Hiramatsu 		} else if (body == arg) {
1592ab105a4fSMasami Hiramatsu 			trace_probe_log_err(0, NO_ARG_NAME);
1593ab105a4fSMasami Hiramatsu 			return -EINVAL;
1594ab105a4fSMasami Hiramatsu 		}
1595d00bbea9SMasami Hiramatsu 		parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
1596d00bbea9SMasami Hiramatsu 		body++;
1597d00bbea9SMasami Hiramatsu 	} else {
1598b576e097SMasami Hiramatsu (Google) 		parg->name = generate_probe_arg_name(arg, i);
1599d00bbea9SMasami Hiramatsu 		body = arg;
1600d00bbea9SMasami Hiramatsu 	}
1601d00bbea9SMasami Hiramatsu 	if (!parg->name)
1602d00bbea9SMasami Hiramatsu 		return -ENOMEM;
1603d00bbea9SMasami Hiramatsu 
1604d00bbea9SMasami Hiramatsu 	if (!is_good_name(parg->name)) {
1605ab105a4fSMasami Hiramatsu 		trace_probe_log_err(0, BAD_ARG_NAME);
1606d00bbea9SMasami Hiramatsu 		return -EINVAL;
1607d00bbea9SMasami Hiramatsu 	}
1608d00bbea9SMasami Hiramatsu 	if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
1609ab105a4fSMasami Hiramatsu 		trace_probe_log_err(0, USED_ARG_NAME);
1610d00bbea9SMasami Hiramatsu 		return -EINVAL;
1611d00bbea9SMasami Hiramatsu 	}
16121b8b0cd7SMasami Hiramatsu (Google) 	ctx->offset = body - arg;
1613d00bbea9SMasami Hiramatsu 	/* Parse fetch argument */
16141b8b0cd7SMasami Hiramatsu (Google) 	return traceprobe_parse_probe_arg_body(body, &tp->size, parg, ctx);
1615d00bbea9SMasami Hiramatsu }
1616d00bbea9SMasami Hiramatsu 
traceprobe_free_probe_arg(struct probe_arg * arg)16178ab83f56SSrikar Dronamraju void traceprobe_free_probe_arg(struct probe_arg *arg)
16188ab83f56SSrikar Dronamraju {
1619a6682814SMasami Hiramatsu 	struct fetch_insn *code = arg->code;
1620a6682814SMasami Hiramatsu 
1621a6682814SMasami Hiramatsu 	while (code && code->op != FETCH_OP_END) {
1622a42e3c4dSMasami Hiramatsu 		if (code->op == FETCH_NOP_SYMBOL ||
1623a42e3c4dSMasami Hiramatsu 		    code->op == FETCH_OP_DATA)
1624a6682814SMasami Hiramatsu 			kfree(code->data);
1625a6682814SMasami Hiramatsu 		code++;
1626a6682814SMasami Hiramatsu 	}
162753305928SMasami Hiramatsu 	kfree(arg->code);
16288ab83f56SSrikar Dronamraju 	kfree(arg->name);
16298ab83f56SSrikar Dronamraju 	kfree(arg->comm);
163040b53b77SMasami Hiramatsu 	kfree(arg->fmt);
16318ab83f56SSrikar Dronamraju }
16328ab83f56SSrikar Dronamraju 
argv_has_var_arg(int argc,const char * argv[],int * args_idx,struct traceprobe_parse_context * ctx)163318b1e870SMasami Hiramatsu (Google) static int argv_has_var_arg(int argc, const char *argv[], int *args_idx,
163418b1e870SMasami Hiramatsu (Google) 			    struct traceprobe_parse_context *ctx)
163518b1e870SMasami Hiramatsu (Google) {
163618b1e870SMasami Hiramatsu (Google) 	int i, found = 0;
163718b1e870SMasami Hiramatsu (Google) 
163818b1e870SMasami Hiramatsu (Google) 	for (i = 0; i < argc; i++)
163918b1e870SMasami Hiramatsu (Google) 		if (str_has_prefix(argv[i], "$arg")) {
164018b1e870SMasami Hiramatsu (Google) 			trace_probe_log_set_index(i + 2);
164118b1e870SMasami Hiramatsu (Google) 
164225f00e40SMasami Hiramatsu (Google) 			if (!tparg_is_function_entry(ctx->flags) &&
164325f00e40SMasami Hiramatsu (Google) 			    !tparg_is_function_return(ctx->flags)) {
164418b1e870SMasami Hiramatsu (Google) 				trace_probe_log_err(0, NOFENTRY_ARGS);
164518b1e870SMasami Hiramatsu (Google) 				return -EINVAL;
164618b1e870SMasami Hiramatsu (Google) 			}
164718b1e870SMasami Hiramatsu (Google) 
164818b1e870SMasami Hiramatsu (Google) 			if (isdigit(argv[i][4])) {
164918b1e870SMasami Hiramatsu (Google) 				found = 1;
165018b1e870SMasami Hiramatsu (Google) 				continue;
165118b1e870SMasami Hiramatsu (Google) 			}
165218b1e870SMasami Hiramatsu (Google) 
165318b1e870SMasami Hiramatsu (Google) 			if (argv[i][4] != '*') {
165418b1e870SMasami Hiramatsu (Google) 				trace_probe_log_err(0, BAD_VAR);
165518b1e870SMasami Hiramatsu (Google) 				return -EINVAL;
165618b1e870SMasami Hiramatsu (Google) 			}
165718b1e870SMasami Hiramatsu (Google) 
165818b1e870SMasami Hiramatsu (Google) 			if (*args_idx >= 0 && *args_idx < argc) {
165918b1e870SMasami Hiramatsu (Google) 				trace_probe_log_err(0, DOUBLE_ARGS);
166018b1e870SMasami Hiramatsu (Google) 				return -EINVAL;
166118b1e870SMasami Hiramatsu (Google) 			}
166218b1e870SMasami Hiramatsu (Google) 			found = 1;
166318b1e870SMasami Hiramatsu (Google) 			*args_idx = i;
166418b1e870SMasami Hiramatsu (Google) 		}
166518b1e870SMasami Hiramatsu (Google) 
166618b1e870SMasami Hiramatsu (Google) 	return found;
166718b1e870SMasami Hiramatsu (Google) }
166818b1e870SMasami Hiramatsu (Google) 
sprint_nth_btf_arg(int idx,const char * type,char * buf,int bufsize,struct traceprobe_parse_context * ctx)166918b1e870SMasami Hiramatsu (Google) static int sprint_nth_btf_arg(int idx, const char *type,
167018b1e870SMasami Hiramatsu (Google) 			      char *buf, int bufsize,
167118b1e870SMasami Hiramatsu (Google) 			      struct traceprobe_parse_context *ctx)
167218b1e870SMasami Hiramatsu (Google) {
167318b1e870SMasami Hiramatsu (Google) 	const char *name;
167418b1e870SMasami Hiramatsu (Google) 	int ret;
167518b1e870SMasami Hiramatsu (Google) 
167618b1e870SMasami Hiramatsu (Google) 	if (idx >= ctx->nr_params) {
167718b1e870SMasami Hiramatsu (Google) 		trace_probe_log_err(0, NO_BTFARG);
167818b1e870SMasami Hiramatsu (Google) 		return -ENOENT;
167918b1e870SMasami Hiramatsu (Google) 	}
1680b1d1e904SMasami Hiramatsu (Google) 	name = btf_name_by_offset(ctx->btf, ctx->params[idx].name_off);
168118b1e870SMasami Hiramatsu (Google) 	if (!name) {
168218b1e870SMasami Hiramatsu (Google) 		trace_probe_log_err(0, NO_BTF_ENTRY);
168318b1e870SMasami Hiramatsu (Google) 		return -ENOENT;
168418b1e870SMasami Hiramatsu (Google) 	}
168518b1e870SMasami Hiramatsu (Google) 	ret = snprintf(buf, bufsize, "%s%s", name, type);
168618b1e870SMasami Hiramatsu (Google) 	if (ret >= bufsize) {
168718b1e870SMasami Hiramatsu (Google) 		trace_probe_log_err(0, ARGS_2LONG);
168818b1e870SMasami Hiramatsu (Google) 		return -E2BIG;
168918b1e870SMasami Hiramatsu (Google) 	}
169018b1e870SMasami Hiramatsu (Google) 	return ret;
169118b1e870SMasami Hiramatsu (Google) }
169218b1e870SMasami Hiramatsu (Google) 
169318b1e870SMasami Hiramatsu (Google) /* Return new_argv which must be freed after use */
traceprobe_expand_meta_args(int argc,const char * argv[],int * new_argc,char * buf,int bufsize,struct traceprobe_parse_context * ctx)169418b1e870SMasami Hiramatsu (Google) const char **traceprobe_expand_meta_args(int argc, const char *argv[],
169518b1e870SMasami Hiramatsu (Google) 					 int *new_argc, char *buf, int bufsize,
169618b1e870SMasami Hiramatsu (Google) 					 struct traceprobe_parse_context *ctx)
169718b1e870SMasami Hiramatsu (Google) {
169818b1e870SMasami Hiramatsu (Google) 	const struct btf_param *params = NULL;
169918b1e870SMasami Hiramatsu (Google) 	int i, j, n, used, ret, args_idx = -1;
17004af0532aSMasami Hiramatsu (Google) 	const char **new_argv __free(kfree) = NULL;
170118b1e870SMasami Hiramatsu (Google) 
170218b1e870SMasami Hiramatsu (Google) 	ret = argv_has_var_arg(argc, argv, &args_idx, ctx);
170318b1e870SMasami Hiramatsu (Google) 	if (ret < 0)
170418b1e870SMasami Hiramatsu (Google) 		return ERR_PTR(ret);
170518b1e870SMasami Hiramatsu (Google) 
170618b1e870SMasami Hiramatsu (Google) 	if (!ret) {
170718b1e870SMasami Hiramatsu (Google) 		*new_argc = argc;
170818b1e870SMasami Hiramatsu (Google) 		return NULL;
170918b1e870SMasami Hiramatsu (Google) 	}
171018b1e870SMasami Hiramatsu (Google) 
1711d157d769SMasami Hiramatsu (Google) 	ret = query_btf_context(ctx);
1712d157d769SMasami Hiramatsu (Google) 	if (ret < 0 || ctx->nr_params == 0) {
171318b1e870SMasami Hiramatsu (Google) 		if (args_idx != -1) {
171418b1e870SMasami Hiramatsu (Google) 			/* $arg* requires BTF info */
171518b1e870SMasami Hiramatsu (Google) 			trace_probe_log_err(0, NOSUP_BTFARG);
171618b1e870SMasami Hiramatsu (Google) 			return (const char **)params;
171718b1e870SMasami Hiramatsu (Google) 		}
1718ed5f2978SMasami Hiramatsu (Google) 		*new_argc = argc;
1719ed5f2978SMasami Hiramatsu (Google) 		return NULL;
172018b1e870SMasami Hiramatsu (Google) 	}
172118b1e870SMasami Hiramatsu (Google) 
172218b1e870SMasami Hiramatsu (Google) 	if (args_idx >= 0)
172318b1e870SMasami Hiramatsu (Google) 		*new_argc = argc + ctx->nr_params - 1;
172418b1e870SMasami Hiramatsu (Google) 	else
172518b1e870SMasami Hiramatsu (Google) 		*new_argc = argc;
172618b1e870SMasami Hiramatsu (Google) 
172718b1e870SMasami Hiramatsu (Google) 	new_argv = kcalloc(*new_argc, sizeof(char *), GFP_KERNEL);
172818b1e870SMasami Hiramatsu (Google) 	if (!new_argv)
172918b1e870SMasami Hiramatsu (Google) 		return ERR_PTR(-ENOMEM);
173018b1e870SMasami Hiramatsu (Google) 
173118b1e870SMasami Hiramatsu (Google) 	used = 0;
173218b1e870SMasami Hiramatsu (Google) 	for (i = 0, j = 0; i < argc; i++) {
173318b1e870SMasami Hiramatsu (Google) 		trace_probe_log_set_index(i + 2);
173418b1e870SMasami Hiramatsu (Google) 		if (i == args_idx) {
1735d157d769SMasami Hiramatsu (Google) 			for (n = 0; n < ctx->nr_params; n++) {
173618b1e870SMasami Hiramatsu (Google) 				ret = sprint_nth_btf_arg(n, "", buf + used,
173718b1e870SMasami Hiramatsu (Google) 							 bufsize - used, ctx);
173818b1e870SMasami Hiramatsu (Google) 				if (ret < 0)
17394af0532aSMasami Hiramatsu (Google) 					return ERR_PTR(ret);
174018b1e870SMasami Hiramatsu (Google) 
174118b1e870SMasami Hiramatsu (Google) 				new_argv[j++] = buf + used;
174218b1e870SMasami Hiramatsu (Google) 				used += ret + 1;
174318b1e870SMasami Hiramatsu (Google) 			}
174418b1e870SMasami Hiramatsu (Google) 			continue;
174518b1e870SMasami Hiramatsu (Google) 		}
174618b1e870SMasami Hiramatsu (Google) 
174718b1e870SMasami Hiramatsu (Google) 		if (str_has_prefix(argv[i], "$arg")) {
174818b1e870SMasami Hiramatsu (Google) 			char *type = NULL;
174918b1e870SMasami Hiramatsu (Google) 
175018b1e870SMasami Hiramatsu (Google) 			n = simple_strtoul(argv[i] + 4, &type, 10);
175118b1e870SMasami Hiramatsu (Google) 			if (type && !(*type == ':' || *type == '\0')) {
175218b1e870SMasami Hiramatsu (Google) 				trace_probe_log_err(0, BAD_VAR);
17534af0532aSMasami Hiramatsu (Google) 				return ERR_PTR(-ENOENT);
175418b1e870SMasami Hiramatsu (Google) 			}
175518b1e870SMasami Hiramatsu (Google) 			/* Note: $argN starts from $arg1 */
175618b1e870SMasami Hiramatsu (Google) 			ret = sprint_nth_btf_arg(n - 1, type, buf + used,
175718b1e870SMasami Hiramatsu (Google) 						 bufsize - used, ctx);
175818b1e870SMasami Hiramatsu (Google) 			if (ret < 0)
17594af0532aSMasami Hiramatsu (Google) 				return ERR_PTR(ret);
176018b1e870SMasami Hiramatsu (Google) 			new_argv[j++] = buf + used;
176118b1e870SMasami Hiramatsu (Google) 			used += ret + 1;
176218b1e870SMasami Hiramatsu (Google) 		} else
176318b1e870SMasami Hiramatsu (Google) 			new_argv[j++] = argv[i];
176418b1e870SMasami Hiramatsu (Google) 	}
176518b1e870SMasami Hiramatsu (Google) 
17664af0532aSMasami Hiramatsu (Google) 	return_ptr(new_argv);
176718b1e870SMasami Hiramatsu (Google) }
176818b1e870SMasami Hiramatsu (Google) 
1769d9b15224SYe Bin /* @buf: *buf must be equal to NULL. Caller must to free *buf */
traceprobe_expand_dentry_args(int argc,const char * argv[],char ** buf)1770d9b15224SYe Bin int traceprobe_expand_dentry_args(int argc, const char *argv[], char **buf)
1771d9b15224SYe Bin {
1772d9b15224SYe Bin 	int i, used, ret;
1773d9b15224SYe Bin 	const int bufsize = MAX_DENTRY_ARGS_LEN;
17744af0532aSMasami Hiramatsu (Google) 	char *tmpbuf __free(kfree) = NULL;
1775d9b15224SYe Bin 
1776d9b15224SYe Bin 	if (*buf)
1777d9b15224SYe Bin 		return -EINVAL;
1778d9b15224SYe Bin 
1779d9b15224SYe Bin 	used = 0;
1780d9b15224SYe Bin 	for (i = 0; i < argc; i++) {
17814af0532aSMasami Hiramatsu (Google) 		char *tmp __free(kfree) = NULL;
1782d9b15224SYe Bin 		char *equal;
178320fe4d07SYe Bin 		size_t arg_len;
178420fe4d07SYe Bin 
178520fe4d07SYe Bin 		if (!glob_match("*:%p[dD]", argv[i]))
178620fe4d07SYe Bin 			continue;
1787d9b15224SYe Bin 
1788d9b15224SYe Bin 		if (!tmpbuf) {
1789d9b15224SYe Bin 			tmpbuf = kmalloc(bufsize, GFP_KERNEL);
1790d9b15224SYe Bin 			if (!tmpbuf)
1791d9b15224SYe Bin 				return -ENOMEM;
1792d9b15224SYe Bin 		}
1793d9b15224SYe Bin 
1794d9b15224SYe Bin 		tmp = kstrdup(argv[i], GFP_KERNEL);
1795d9b15224SYe Bin 		if (!tmp)
17964af0532aSMasami Hiramatsu (Google) 			return -ENOMEM;
1797d9b15224SYe Bin 
1798d9b15224SYe Bin 		equal = strchr(tmp, '=');
1799d9b15224SYe Bin 		if (equal)
1800d9b15224SYe Bin 			*equal = '\0';
180120fe4d07SYe Bin 		arg_len = strlen(argv[i]);
180220fe4d07SYe Bin 		tmp[arg_len - 4] = '\0';
180320fe4d07SYe Bin 		if (argv[i][arg_len - 1] == 'd')
1804d9b15224SYe Bin 			ret = snprintf(tmpbuf + used, bufsize - used,
1805d9b15224SYe Bin 				       "%s%s+0x0(+0x%zx(%s)):string",
1806d9b15224SYe Bin 				       equal ? tmp : "", equal ? "=" : "",
1807d9b15224SYe Bin 				       offsetof(struct dentry, d_name.name),
1808d9b15224SYe Bin 				       equal ? equal + 1 : tmp);
180920fe4d07SYe Bin 		else
181020fe4d07SYe Bin 			ret = snprintf(tmpbuf + used, bufsize - used,
181120fe4d07SYe Bin 				       "%s%s+0x0(+0x%zx(+0x%zx(%s))):string",
181220fe4d07SYe Bin 				       equal ? tmp : "", equal ? "=" : "",
181320fe4d07SYe Bin 				       offsetof(struct dentry, d_name.name),
181420fe4d07SYe Bin 				       offsetof(struct file, f_path.dentry),
181520fe4d07SYe Bin 				       equal ? equal + 1 : tmp);
181620fe4d07SYe Bin 
1817d9b15224SYe Bin 		if (ret >= bufsize - used)
18184af0532aSMasami Hiramatsu (Google) 			return -ENOMEM;
1819d9b15224SYe Bin 		argv[i] = tmpbuf + used;
1820d9b15224SYe Bin 		used += ret + 1;
1821d9b15224SYe Bin 	}
1822d9b15224SYe Bin 
18234af0532aSMasami Hiramatsu (Google) 	*buf = no_free_ptr(tmpbuf);
1824d9b15224SYe Bin 	return 0;
1825d9b15224SYe Bin }
1826d9b15224SYe Bin 
traceprobe_finish_parse(struct traceprobe_parse_context * ctx)1827b1d1e904SMasami Hiramatsu (Google) void traceprobe_finish_parse(struct traceprobe_parse_context *ctx)
1828b1d1e904SMasami Hiramatsu (Google) {
1829b1d1e904SMasami Hiramatsu (Google) 	clear_btf_context(ctx);
1830b1d1e904SMasami Hiramatsu (Google) }
1831b1d1e904SMasami Hiramatsu (Google) 
traceprobe_update_arg(struct probe_arg * arg)1832a6682814SMasami Hiramatsu int traceprobe_update_arg(struct probe_arg *arg)
1833a6682814SMasami Hiramatsu {
1834a6682814SMasami Hiramatsu 	struct fetch_insn *code = arg->code;
1835a6682814SMasami Hiramatsu 	long offset;
1836a6682814SMasami Hiramatsu 	char *tmp;
1837a6682814SMasami Hiramatsu 	char c;
1838a6682814SMasami Hiramatsu 	int ret = 0;
1839a6682814SMasami Hiramatsu 
1840a6682814SMasami Hiramatsu 	while (code && code->op != FETCH_OP_END) {
1841a6682814SMasami Hiramatsu 		if (code->op == FETCH_NOP_SYMBOL) {
1842a6682814SMasami Hiramatsu 			if (code[1].op != FETCH_OP_IMM)
1843a6682814SMasami Hiramatsu 				return -EINVAL;
1844a6682814SMasami Hiramatsu 
1845ee474b81SMasami Hiramatsu 			tmp = strpbrk(code->data, "+-");
1846a6682814SMasami Hiramatsu 			if (tmp)
1847a6682814SMasami Hiramatsu 				c = *tmp;
1848a6682814SMasami Hiramatsu 			ret = traceprobe_split_symbol_offset(code->data,
1849a6682814SMasami Hiramatsu 							     &offset);
1850a6682814SMasami Hiramatsu 			if (ret)
1851a6682814SMasami Hiramatsu 				return ret;
1852a6682814SMasami Hiramatsu 
1853a6682814SMasami Hiramatsu 			code[1].immediate =
1854a6682814SMasami Hiramatsu 				(unsigned long)kallsyms_lookup_name(code->data);
1855a6682814SMasami Hiramatsu 			if (tmp)
1856a6682814SMasami Hiramatsu 				*tmp = c;
1857a6682814SMasami Hiramatsu 			if (!code[1].immediate)
1858a6682814SMasami Hiramatsu 				return -ENOENT;
1859a6682814SMasami Hiramatsu 			code[1].immediate += offset;
1860a6682814SMasami Hiramatsu 		}
1861a6682814SMasami Hiramatsu 		code++;
1862a6682814SMasami Hiramatsu 	}
1863a6682814SMasami Hiramatsu 	return 0;
1864a6682814SMasami Hiramatsu }
1865a6682814SMasami Hiramatsu 
186640b53b77SMasami Hiramatsu /* When len=0, we just calculate the needed length */
186740b53b77SMasami Hiramatsu #define LEN_OR_ZERO (len ? len - pos : 0)
__set_print_fmt(struct trace_probe * tp,char * buf,int len,enum probe_print_type ptype)18685bf652aaSNamhyung Kim static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
1869007517a0SSteven Rostedt (VMware) 			   enum probe_print_type ptype)
18705bf652aaSNamhyung Kim {
187140b53b77SMasami Hiramatsu 	struct probe_arg *parg;
187240b53b77SMasami Hiramatsu 	int i, j;
18735bf652aaSNamhyung Kim 	int pos = 0;
18745bf652aaSNamhyung Kim 	const char *fmt, *arg;
18755bf652aaSNamhyung Kim 
1876007517a0SSteven Rostedt (VMware) 	switch (ptype) {
1877007517a0SSteven Rostedt (VMware) 	case PROBE_PRINT_NORMAL:
18785bf652aaSNamhyung Kim 		fmt = "(%lx)";
1879b61edd57SSteven Rostedt (Google) 		arg = ", REC->" FIELD_STRING_IP;
1880007517a0SSteven Rostedt (VMware) 		break;
1881007517a0SSteven Rostedt (VMware) 	case PROBE_PRINT_RETURN:
18825bf652aaSNamhyung Kim 		fmt = "(%lx <- %lx)";
1883b61edd57SSteven Rostedt (Google) 		arg = ", REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
1884007517a0SSteven Rostedt (VMware) 		break;
18857491e2c4STzvetomir Stoyanov (VMware) 	case PROBE_PRINT_EVENT:
1886b61edd57SSteven Rostedt (Google) 		fmt = "";
1887b61edd57SSteven Rostedt (Google) 		arg = "";
18887491e2c4STzvetomir Stoyanov (VMware) 		break;
1889007517a0SSteven Rostedt (VMware) 	default:
1890007517a0SSteven Rostedt (VMware) 		WARN_ON_ONCE(1);
1891007517a0SSteven Rostedt (VMware) 		return 0;
18925bf652aaSNamhyung Kim 	}
18935bf652aaSNamhyung Kim 
18945bf652aaSNamhyung Kim 	pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
18955bf652aaSNamhyung Kim 
18965bf652aaSNamhyung Kim 	for (i = 0; i < tp->nr_args; i++) {
189740b53b77SMasami Hiramatsu 		parg = tp->args + i;
189840b53b77SMasami Hiramatsu 		pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
189940b53b77SMasami Hiramatsu 		if (parg->count) {
190040b53b77SMasami Hiramatsu 			pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
190140b53b77SMasami Hiramatsu 					parg->type->fmt);
190240b53b77SMasami Hiramatsu 			for (j = 1; j < parg->count; j++)
190340b53b77SMasami Hiramatsu 				pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
190440b53b77SMasami Hiramatsu 						parg->type->fmt);
190540b53b77SMasami Hiramatsu 			pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
190640b53b77SMasami Hiramatsu 		} else
190740b53b77SMasami Hiramatsu 			pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
190840b53b77SMasami Hiramatsu 					parg->type->fmt);
19095bf652aaSNamhyung Kim 	}
19105bf652aaSNamhyung Kim 
1911b61edd57SSteven Rostedt (Google) 	pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", arg);
19125bf652aaSNamhyung Kim 
19135bf652aaSNamhyung Kim 	for (i = 0; i < tp->nr_args; i++) {
191440b53b77SMasami Hiramatsu 		parg = tp->args + i;
191540b53b77SMasami Hiramatsu 		if (parg->count) {
1916b26a124cSMasami Hiramatsu (Google) 			if (parg->type->is_string)
191740b53b77SMasami Hiramatsu 				fmt = ", __get_str(%s[%d])";
19185bf652aaSNamhyung Kim 			else
191940b53b77SMasami Hiramatsu 				fmt = ", REC->%s[%d]";
192040b53b77SMasami Hiramatsu 			for (j = 0; j < parg->count; j++)
192140b53b77SMasami Hiramatsu 				pos += snprintf(buf + pos, LEN_OR_ZERO,
192240b53b77SMasami Hiramatsu 						fmt, parg->name, j);
192340b53b77SMasami Hiramatsu 		} else {
1924b26a124cSMasami Hiramatsu (Google) 			if (parg->type->is_string)
192540b53b77SMasami Hiramatsu 				fmt = ", __get_str(%s)";
192640b53b77SMasami Hiramatsu 			else
192740b53b77SMasami Hiramatsu 				fmt = ", REC->%s";
192840b53b77SMasami Hiramatsu 			pos += snprintf(buf + pos, LEN_OR_ZERO,
192940b53b77SMasami Hiramatsu 					fmt, parg->name);
19305bf652aaSNamhyung Kim 		}
193140b53b77SMasami Hiramatsu 	}
19325bf652aaSNamhyung Kim 
19335bf652aaSNamhyung Kim 	/* return the length of print_fmt */
19345bf652aaSNamhyung Kim 	return pos;
19355bf652aaSNamhyung Kim }
193640b53b77SMasami Hiramatsu #undef LEN_OR_ZERO
19375bf652aaSNamhyung Kim 
traceprobe_set_print_fmt(struct trace_probe * tp,enum probe_print_type ptype)1938007517a0SSteven Rostedt (VMware) int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype)
19395bf652aaSNamhyung Kim {
1940e3dc9f89SMasami Hiramatsu 	struct trace_event_call *call = trace_probe_event_call(tp);
19415bf652aaSNamhyung Kim 	int len;
19425bf652aaSNamhyung Kim 	char *print_fmt;
19435bf652aaSNamhyung Kim 
19445bf652aaSNamhyung Kim 	/* First: called with 0 length to calculate the needed length */
1945007517a0SSteven Rostedt (VMware) 	len = __set_print_fmt(tp, NULL, 0, ptype);
19465bf652aaSNamhyung Kim 	print_fmt = kmalloc(len + 1, GFP_KERNEL);
19475bf652aaSNamhyung Kim 	if (!print_fmt)
19485bf652aaSNamhyung Kim 		return -ENOMEM;
19495bf652aaSNamhyung Kim 
19505bf652aaSNamhyung Kim 	/* Second: actually write the @print_fmt */
1951007517a0SSteven Rostedt (VMware) 	__set_print_fmt(tp, print_fmt, len + 1, ptype);
1952e3dc9f89SMasami Hiramatsu 	call->print_fmt = print_fmt;
19535bf652aaSNamhyung Kim 
19545bf652aaSNamhyung Kim 	return 0;
19555bf652aaSNamhyung Kim }
1956eeb07b06SMasami Hiramatsu 
traceprobe_define_arg_fields(struct trace_event_call * event_call,size_t offset,struct trace_probe * tp)1957eeb07b06SMasami Hiramatsu int traceprobe_define_arg_fields(struct trace_event_call *event_call,
1958eeb07b06SMasami Hiramatsu 				 size_t offset, struct trace_probe *tp)
1959eeb07b06SMasami Hiramatsu {
1960eeb07b06SMasami Hiramatsu 	int ret, i;
1961eeb07b06SMasami Hiramatsu 
1962eeb07b06SMasami Hiramatsu 	/* Set argument names as fields */
1963eeb07b06SMasami Hiramatsu 	for (i = 0; i < tp->nr_args; i++) {
1964eeb07b06SMasami Hiramatsu 		struct probe_arg *parg = &tp->args[i];
196540b53b77SMasami Hiramatsu 		const char *fmt = parg->type->fmttype;
196640b53b77SMasami Hiramatsu 		int size = parg->type->size;
1967eeb07b06SMasami Hiramatsu 
196840b53b77SMasami Hiramatsu 		if (parg->fmt)
196940b53b77SMasami Hiramatsu 			fmt = parg->fmt;
197040b53b77SMasami Hiramatsu 		if (parg->count)
197140b53b77SMasami Hiramatsu 			size *= parg->count;
197240b53b77SMasami Hiramatsu 		ret = trace_define_field(event_call, fmt, parg->name,
197340b53b77SMasami Hiramatsu 					 offset + parg->offset, size,
1974eeb07b06SMasami Hiramatsu 					 parg->type->is_signed,
1975eeb07b06SMasami Hiramatsu 					 FILTER_OTHER);
1976eeb07b06SMasami Hiramatsu 		if (ret)
1977eeb07b06SMasami Hiramatsu 			return ret;
1978eeb07b06SMasami Hiramatsu 	}
1979eeb07b06SMasami Hiramatsu 	return 0;
1980eeb07b06SMasami Hiramatsu }
1981455b2899SMasami Hiramatsu 
trace_probe_event_free(struct trace_probe_event * tpe)1982ca89bc07SMasami Hiramatsu static void trace_probe_event_free(struct trace_probe_event *tpe)
1983ca89bc07SMasami Hiramatsu {
1984ca89bc07SMasami Hiramatsu 	kfree(tpe->class.system);
1985ca89bc07SMasami Hiramatsu 	kfree(tpe->call.name);
1986ca89bc07SMasami Hiramatsu 	kfree(tpe->call.print_fmt);
1987ca89bc07SMasami Hiramatsu 	kfree(tpe);
1988ca89bc07SMasami Hiramatsu }
1989ca89bc07SMasami Hiramatsu 
trace_probe_append(struct trace_probe * tp,struct trace_probe * to)1990ca89bc07SMasami Hiramatsu int trace_probe_append(struct trace_probe *tp, struct trace_probe *to)
1991ca89bc07SMasami Hiramatsu {
1992ca89bc07SMasami Hiramatsu 	if (trace_probe_has_sibling(tp))
1993ca89bc07SMasami Hiramatsu 		return -EBUSY;
1994ca89bc07SMasami Hiramatsu 
1995ca89bc07SMasami Hiramatsu 	list_del_init(&tp->list);
1996ca89bc07SMasami Hiramatsu 	trace_probe_event_free(tp->event);
1997ca89bc07SMasami Hiramatsu 
1998ca89bc07SMasami Hiramatsu 	tp->event = to->event;
1999ca89bc07SMasami Hiramatsu 	list_add_tail(&tp->list, trace_probe_probe_list(to));
2000ca89bc07SMasami Hiramatsu 
2001ca89bc07SMasami Hiramatsu 	return 0;
2002ca89bc07SMasami Hiramatsu }
2003ca89bc07SMasami Hiramatsu 
trace_probe_unlink(struct trace_probe * tp)2004ca89bc07SMasami Hiramatsu void trace_probe_unlink(struct trace_probe *tp)
2005ca89bc07SMasami Hiramatsu {
2006ca89bc07SMasami Hiramatsu 	list_del_init(&tp->list);
2007ca89bc07SMasami Hiramatsu 	if (list_empty(trace_probe_probe_list(tp)))
2008ca89bc07SMasami Hiramatsu 		trace_probe_event_free(tp->event);
2009ca89bc07SMasami Hiramatsu 	tp->event = NULL;
2010ca89bc07SMasami Hiramatsu }
2011455b2899SMasami Hiramatsu 
trace_probe_cleanup(struct trace_probe * tp)2012455b2899SMasami Hiramatsu void trace_probe_cleanup(struct trace_probe *tp)
2013455b2899SMasami Hiramatsu {
2014455b2899SMasami Hiramatsu 	int i;
2015455b2899SMasami Hiramatsu 
2016455b2899SMasami Hiramatsu 	for (i = 0; i < tp->nr_args; i++)
2017455b2899SMasami Hiramatsu 		traceprobe_free_probe_arg(&tp->args[i]);
2018455b2899SMasami Hiramatsu 
201925f00e40SMasami Hiramatsu (Google) 	if (tp->entry_arg) {
202025f00e40SMasami Hiramatsu (Google) 		kfree(tp->entry_arg->code);
202125f00e40SMasami Hiramatsu (Google) 		kfree(tp->entry_arg);
202225f00e40SMasami Hiramatsu (Google) 		tp->entry_arg = NULL;
202325f00e40SMasami Hiramatsu (Google) 	}
202425f00e40SMasami Hiramatsu (Google) 
2025ca89bc07SMasami Hiramatsu 	if (tp->event)
2026ca89bc07SMasami Hiramatsu 		trace_probe_unlink(tp);
2027455b2899SMasami Hiramatsu }
2028455b2899SMasami Hiramatsu 
trace_probe_init(struct trace_probe * tp,const char * event,const char * group,bool alloc_filter,int nargs)2029455b2899SMasami Hiramatsu int trace_probe_init(struct trace_probe *tp, const char *event,
2030035ba760SMasami Hiramatsu (Google) 		     const char *group, bool alloc_filter, int nargs)
2031455b2899SMasami Hiramatsu {
203260d53e2cSMasami Hiramatsu 	struct trace_event_call *call;
2033b61387cbSMasami Hiramatsu 	size_t size = sizeof(struct trace_probe_event);
203460d53e2cSMasami Hiramatsu 	int ret = 0;
2035e3dc9f89SMasami Hiramatsu 
2036455b2899SMasami Hiramatsu 	if (!event || !group)
2037455b2899SMasami Hiramatsu 		return -EINVAL;
2038455b2899SMasami Hiramatsu 
2039b61387cbSMasami Hiramatsu 	if (alloc_filter)
2040b61387cbSMasami Hiramatsu 		size += sizeof(struct trace_uprobe_filter);
2041b61387cbSMasami Hiramatsu 
2042b61387cbSMasami Hiramatsu 	tp->event = kzalloc(size, GFP_KERNEL);
204360d53e2cSMasami Hiramatsu 	if (!tp->event)
2044455b2899SMasami Hiramatsu 		return -ENOMEM;
2045455b2899SMasami Hiramatsu 
2046d59fae6fSMasami Hiramatsu 	INIT_LIST_HEAD(&tp->event->files);
2047d59fae6fSMasami Hiramatsu 	INIT_LIST_HEAD(&tp->event->class.fields);
2048d59fae6fSMasami Hiramatsu 	INIT_LIST_HEAD(&tp->event->probes);
2049d59fae6fSMasami Hiramatsu 	INIT_LIST_HEAD(&tp->list);
2050fc9d276fSJulia Lawall 	list_add(&tp->list, &tp->event->probes);
2051d59fae6fSMasami Hiramatsu 
205260d53e2cSMasami Hiramatsu 	call = trace_probe_event_call(tp);
205360d53e2cSMasami Hiramatsu 	call->class = &tp->event->class;
205460d53e2cSMasami Hiramatsu 	call->name = kstrdup(event, GFP_KERNEL);
205560d53e2cSMasami Hiramatsu 	if (!call->name) {
205660d53e2cSMasami Hiramatsu 		ret = -ENOMEM;
205760d53e2cSMasami Hiramatsu 		goto error;
2058455b2899SMasami Hiramatsu 	}
205960d53e2cSMasami Hiramatsu 
206060d53e2cSMasami Hiramatsu 	tp->event->class.system = kstrdup(group, GFP_KERNEL);
206160d53e2cSMasami Hiramatsu 	if (!tp->event->class.system) {
206260d53e2cSMasami Hiramatsu 		ret = -ENOMEM;
206360d53e2cSMasami Hiramatsu 		goto error;
206460d53e2cSMasami Hiramatsu 	}
2065455b2899SMasami Hiramatsu 
2066035ba760SMasami Hiramatsu (Google) 	tp->nr_args = nargs;
2067035ba760SMasami Hiramatsu (Google) 	/* Make sure pointers in args[] are NULL */
2068035ba760SMasami Hiramatsu (Google) 	if (nargs)
2069035ba760SMasami Hiramatsu (Google) 		memset(tp->args, 0, sizeof(tp->args[0]) * nargs);
2070035ba760SMasami Hiramatsu (Google) 
2071455b2899SMasami Hiramatsu 	return 0;
207260d53e2cSMasami Hiramatsu 
207360d53e2cSMasami Hiramatsu error:
207460d53e2cSMasami Hiramatsu 	trace_probe_cleanup(tp);
207560d53e2cSMasami Hiramatsu 	return ret;
2076455b2899SMasami Hiramatsu }
207746e5376dSMasami Hiramatsu 
20788e242060SMasami Hiramatsu static struct trace_event_call *
find_trace_event_call(const char * system,const char * event_name)20798e242060SMasami Hiramatsu find_trace_event_call(const char *system, const char *event_name)
20808e242060SMasami Hiramatsu {
20818e242060SMasami Hiramatsu 	struct trace_event_call *tp_event;
20828e242060SMasami Hiramatsu 	const char *name;
20838e242060SMasami Hiramatsu 
20848e242060SMasami Hiramatsu 	list_for_each_entry(tp_event, &ftrace_events, list) {
20858e242060SMasami Hiramatsu 		if (!tp_event->class->system ||
20868e242060SMasami Hiramatsu 		    strcmp(system, tp_event->class->system))
20878e242060SMasami Hiramatsu 			continue;
20888e242060SMasami Hiramatsu 		name = trace_event_name(tp_event);
20898e242060SMasami Hiramatsu 		if (!name || strcmp(event_name, name))
20908e242060SMasami Hiramatsu 			continue;
20918e242060SMasami Hiramatsu 		return tp_event;
20928e242060SMasami Hiramatsu 	}
20938e242060SMasami Hiramatsu 
20948e242060SMasami Hiramatsu 	return NULL;
20958e242060SMasami Hiramatsu }
20968e242060SMasami Hiramatsu 
trace_probe_register_event_call(struct trace_probe * tp)209746e5376dSMasami Hiramatsu int trace_probe_register_event_call(struct trace_probe *tp)
209846e5376dSMasami Hiramatsu {
2099e3dc9f89SMasami Hiramatsu 	struct trace_event_call *call = trace_probe_event_call(tp);
210046e5376dSMasami Hiramatsu 	int ret;
210146e5376dSMasami Hiramatsu 
21028e242060SMasami Hiramatsu 	lockdep_assert_held(&event_mutex);
21038e242060SMasami Hiramatsu 
21048e242060SMasami Hiramatsu 	if (find_trace_event_call(trace_probe_group_name(tp),
21058e242060SMasami Hiramatsu 				  trace_probe_name(tp)))
21068e242060SMasami Hiramatsu 		return -EEXIST;
21078e242060SMasami Hiramatsu 
210846e5376dSMasami Hiramatsu 	ret = register_trace_event(&call->event);
210946e5376dSMasami Hiramatsu 	if (!ret)
211046e5376dSMasami Hiramatsu 		return -ENODEV;
211146e5376dSMasami Hiramatsu 
211246e5376dSMasami Hiramatsu 	ret = trace_add_event_call(call);
211346e5376dSMasami Hiramatsu 	if (ret)
211446e5376dSMasami Hiramatsu 		unregister_trace_event(&call->event);
211546e5376dSMasami Hiramatsu 
211646e5376dSMasami Hiramatsu 	return ret;
211746e5376dSMasami Hiramatsu }
2118b5f935eeSMasami Hiramatsu 
trace_probe_add_file(struct trace_probe * tp,struct trace_event_file * file)2119b5f935eeSMasami Hiramatsu int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
2120b5f935eeSMasami Hiramatsu {
2121b5f935eeSMasami Hiramatsu 	struct event_file_link *link;
2122b5f935eeSMasami Hiramatsu 
2123b5f935eeSMasami Hiramatsu 	link = kmalloc(sizeof(*link), GFP_KERNEL);
2124b5f935eeSMasami Hiramatsu 	if (!link)
2125b5f935eeSMasami Hiramatsu 		return -ENOMEM;
2126b5f935eeSMasami Hiramatsu 
2127b5f935eeSMasami Hiramatsu 	link->file = file;
2128b5f935eeSMasami Hiramatsu 	INIT_LIST_HEAD(&link->list);
212960d53e2cSMasami Hiramatsu 	list_add_tail_rcu(&link->list, &tp->event->files);
2130747774d6SMasami Hiramatsu 	trace_probe_set_flag(tp, TP_FLAG_TRACE);
2131b5f935eeSMasami Hiramatsu 	return 0;
2132b5f935eeSMasami Hiramatsu }
2133b5f935eeSMasami Hiramatsu 
trace_probe_get_file_link(struct trace_probe * tp,struct trace_event_file * file)2134b5f935eeSMasami Hiramatsu struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
2135b5f935eeSMasami Hiramatsu 						  struct trace_event_file *file)
2136b5f935eeSMasami Hiramatsu {
2137b5f935eeSMasami Hiramatsu 	struct event_file_link *link;
2138b5f935eeSMasami Hiramatsu 
2139b5f935eeSMasami Hiramatsu 	trace_probe_for_each_link(link, tp) {
2140b5f935eeSMasami Hiramatsu 		if (link->file == file)
2141b5f935eeSMasami Hiramatsu 			return link;
2142b5f935eeSMasami Hiramatsu 	}
2143b5f935eeSMasami Hiramatsu 
2144b5f935eeSMasami Hiramatsu 	return NULL;
2145b5f935eeSMasami Hiramatsu }
2146b5f935eeSMasami Hiramatsu 
trace_probe_remove_file(struct trace_probe * tp,struct trace_event_file * file)2147b5f935eeSMasami Hiramatsu int trace_probe_remove_file(struct trace_probe *tp,
2148b5f935eeSMasami Hiramatsu 			    struct trace_event_file *file)
2149b5f935eeSMasami Hiramatsu {
2150b5f935eeSMasami Hiramatsu 	struct event_file_link *link;
2151b5f935eeSMasami Hiramatsu 
2152b5f935eeSMasami Hiramatsu 	link = trace_probe_get_file_link(tp, file);
2153b5f935eeSMasami Hiramatsu 	if (!link)
2154b5f935eeSMasami Hiramatsu 		return -ENOENT;
2155b5f935eeSMasami Hiramatsu 
2156b5f935eeSMasami Hiramatsu 	list_del_rcu(&link->list);
2157cae16f2cSUladzislau Rezki (Sony) 	kvfree_rcu_mightsleep(link);
2158b5f935eeSMasami Hiramatsu 
215960d53e2cSMasami Hiramatsu 	if (list_empty(&tp->event->files))
2160747774d6SMasami Hiramatsu 		trace_probe_clear_flag(tp, TP_FLAG_TRACE);
2161b5f935eeSMasami Hiramatsu 
2162b5f935eeSMasami Hiramatsu 	return 0;
2163b5f935eeSMasami Hiramatsu }
2164ca89bc07SMasami Hiramatsu 
2165ca89bc07SMasami Hiramatsu /*
2166ca89bc07SMasami Hiramatsu  * Return the smallest index of different type argument (start from 1).
2167ca89bc07SMasami Hiramatsu  * If all argument types and name are same, return 0.
2168ca89bc07SMasami Hiramatsu  */
trace_probe_compare_arg_type(struct trace_probe * a,struct trace_probe * b)2169ca89bc07SMasami Hiramatsu int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b)
2170ca89bc07SMasami Hiramatsu {
2171ca89bc07SMasami Hiramatsu 	int i;
2172ca89bc07SMasami Hiramatsu 
2173d2aea95aSMasami Hiramatsu 	/* In case of more arguments */
2174d2aea95aSMasami Hiramatsu 	if (a->nr_args < b->nr_args)
2175d2aea95aSMasami Hiramatsu 		return a->nr_args + 1;
2176d2aea95aSMasami Hiramatsu 	if (a->nr_args > b->nr_args)
2177d2aea95aSMasami Hiramatsu 		return b->nr_args + 1;
2178d2aea95aSMasami Hiramatsu 
2179ca89bc07SMasami Hiramatsu 	for (i = 0; i < a->nr_args; i++) {
2180ca89bc07SMasami Hiramatsu 		if ((b->nr_args <= i) ||
2181ca89bc07SMasami Hiramatsu 		    ((a->args[i].type != b->args[i].type) ||
2182ca89bc07SMasami Hiramatsu 		     (a->args[i].count != b->args[i].count) ||
2183ca89bc07SMasami Hiramatsu 		     strcmp(a->args[i].name, b->args[i].name)))
2184ca89bc07SMasami Hiramatsu 			return i + 1;
2185ca89bc07SMasami Hiramatsu 	}
2186ca89bc07SMasami Hiramatsu 
2187ca89bc07SMasami Hiramatsu 	return 0;
2188ca89bc07SMasami Hiramatsu }
2189eb5bf813SMasami Hiramatsu 
trace_probe_match_command_args(struct trace_probe * tp,int argc,const char ** argv)2190eb5bf813SMasami Hiramatsu bool trace_probe_match_command_args(struct trace_probe *tp,
2191eb5bf813SMasami Hiramatsu 				    int argc, const char **argv)
2192eb5bf813SMasami Hiramatsu {
2193eb5bf813SMasami Hiramatsu 	char buf[MAX_ARGSTR_LEN + 1];
2194eb5bf813SMasami Hiramatsu 	int i;
2195eb5bf813SMasami Hiramatsu 
2196eb5bf813SMasami Hiramatsu 	if (tp->nr_args < argc)
2197eb5bf813SMasami Hiramatsu 		return false;
2198eb5bf813SMasami Hiramatsu 
2199eb5bf813SMasami Hiramatsu 	for (i = 0; i < argc; i++) {
2200eb5bf813SMasami Hiramatsu 		snprintf(buf, sizeof(buf), "%s=%s",
2201eb5bf813SMasami Hiramatsu 			 tp->args[i].name, tp->args[i].comm);
2202eb5bf813SMasami Hiramatsu 		if (strcmp(buf, argv[i]))
2203eb5bf813SMasami Hiramatsu 			return false;
2204eb5bf813SMasami Hiramatsu 	}
2205eb5bf813SMasami Hiramatsu 	return true;
2206eb5bf813SMasami Hiramatsu }
2207d262271dSMasami Hiramatsu 
trace_probe_create(const char * raw_command,int (* createfn)(int,const char **))2208d262271dSMasami Hiramatsu int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **))
2209d262271dSMasami Hiramatsu {
2210d262271dSMasami Hiramatsu 	int argc = 0, ret = 0;
2211d262271dSMasami Hiramatsu 	char **argv;
2212d262271dSMasami Hiramatsu 
2213d262271dSMasami Hiramatsu 	argv = argv_split(GFP_KERNEL, raw_command, &argc);
2214d262271dSMasami Hiramatsu 	if (!argv)
2215d262271dSMasami Hiramatsu 		return -ENOMEM;
2216d262271dSMasami Hiramatsu 
2217d262271dSMasami Hiramatsu 	if (argc)
2218d262271dSMasami Hiramatsu 		ret = createfn(argc, (const char **)argv);
2219d262271dSMasami Hiramatsu 
2220d262271dSMasami Hiramatsu 	argv_free(argv);
2221d262271dSMasami Hiramatsu 
2222d262271dSMasami Hiramatsu 	return ret;
2223d262271dSMasami Hiramatsu }
2224196b6389SSong Chen 
trace_probe_print_args(struct trace_seq * s,struct probe_arg * args,int nr_args,u8 * data,void * field)2225196b6389SSong Chen int trace_probe_print_args(struct trace_seq *s, struct probe_arg *args, int nr_args,
2226196b6389SSong Chen 		 u8 *data, void *field)
2227196b6389SSong Chen {
2228196b6389SSong Chen 	void *p;
2229196b6389SSong Chen 	int i, j;
2230196b6389SSong Chen 
2231196b6389SSong Chen 	for (i = 0; i < nr_args; i++) {
2232196b6389SSong Chen 		struct probe_arg *a = args + i;
2233196b6389SSong Chen 
2234196b6389SSong Chen 		trace_seq_printf(s, " %s=", a->name);
2235196b6389SSong Chen 		if (likely(!a->count)) {
2236196b6389SSong Chen 			if (!a->type->print(s, data + a->offset, field))
2237196b6389SSong Chen 				return -ENOMEM;
2238196b6389SSong Chen 			continue;
2239196b6389SSong Chen 		}
2240196b6389SSong Chen 		trace_seq_putc(s, '{');
2241196b6389SSong Chen 		p = data + a->offset;
2242196b6389SSong Chen 		for (j = 0; j < a->count; j++) {
2243196b6389SSong Chen 			if (!a->type->print(s, p, field))
2244196b6389SSong Chen 				return -ENOMEM;
2245196b6389SSong Chen 			trace_seq_putc(s, j == a->count - 1 ? '}' : ',');
2246196b6389SSong Chen 			p += a->type->size;
2247196b6389SSong Chen 		}
2248196b6389SSong Chen 	}
2249196b6389SSong Chen 	return 0;
2250196b6389SSong Chen }
2251