xref: /linux-6.15/kernel/trace/trace_probe.c (revision fd26290e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Common code for probe-based Dynamic events.
4  *
5  * This code was copied from kernel/trace/trace_kprobe.c written by
6  * Masami Hiramatsu <[email protected]>
7  *
8  * Updates to make this generic:
9  * Copyright (C) IBM Corporation, 2010-2011
10  * Author:     Srikar Dronamraju
11  */
12 #define pr_fmt(fmt)	"trace_probe: " fmt
13 
14 #include <linux/bpf.h>
15 
16 #include "trace_probe.h"
17 
18 #undef C
19 #define C(a, b)		b
20 
21 static const char *trace_probe_err_text[] = { ERRORS };
22 
23 static const char *reserved_field_names[] = {
24 	"common_type",
25 	"common_flags",
26 	"common_preempt_count",
27 	"common_pid",
28 	"common_tgid",
29 	FIELD_STRING_IP,
30 	FIELD_STRING_RETIP,
31 	FIELD_STRING_FUNC,
32 };
33 
34 /* Printing  in basic type function template */
35 #define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt)			\
36 int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, void *data, void *ent)\
37 {									\
38 	trace_seq_printf(s, fmt, *(type *)data);			\
39 	return !trace_seq_has_overflowed(s);				\
40 }									\
41 const char PRINT_TYPE_FMT_NAME(tname)[] = fmt;
42 
43 DEFINE_BASIC_PRINT_TYPE_FUNC(u8,  u8,  "%u")
44 DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u")
45 DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u")
46 DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu")
47 DEFINE_BASIC_PRINT_TYPE_FUNC(s8,  s8,  "%d")
48 DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d")
49 DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d")
50 DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld")
51 DEFINE_BASIC_PRINT_TYPE_FUNC(x8,  u8,  "0x%x")
52 DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x")
53 DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x")
54 DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx")
55 DEFINE_BASIC_PRINT_TYPE_FUNC(char, u8, "'%c'")
56 
57 int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent)
58 {
59 	trace_seq_printf(s, "%pS", (void *)*(unsigned long *)data);
60 	return !trace_seq_has_overflowed(s);
61 }
62 const char PRINT_TYPE_FMT_NAME(symbol)[] = "%pS";
63 
64 /* Print type function for string type */
65 int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, void *data, void *ent)
66 {
67 	int len = *(u32 *)data >> 16;
68 
69 	if (!len)
70 		trace_seq_puts(s, "(fault)");
71 	else
72 		trace_seq_printf(s, "\"%s\"",
73 				 (const char *)get_loc_data(data, ent));
74 	return !trace_seq_has_overflowed(s);
75 }
76 
77 const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
78 
79 /* Fetch type information table */
80 static const struct fetch_type probe_fetch_types[] = {
81 	/* Special types */
82 	__ASSIGN_FETCH_TYPE("string", string, string, sizeof(u32), 1, 1,
83 			    "__data_loc char[]"),
84 	__ASSIGN_FETCH_TYPE("ustring", string, string, sizeof(u32), 1, 1,
85 			    "__data_loc char[]"),
86 	__ASSIGN_FETCH_TYPE("symstr", string, string, sizeof(u32), 1, 1,
87 			    "__data_loc char[]"),
88 	/* Basic types */
89 	ASSIGN_FETCH_TYPE(u8,  u8,  0),
90 	ASSIGN_FETCH_TYPE(u16, u16, 0),
91 	ASSIGN_FETCH_TYPE(u32, u32, 0),
92 	ASSIGN_FETCH_TYPE(u64, u64, 0),
93 	ASSIGN_FETCH_TYPE(s8,  u8,  1),
94 	ASSIGN_FETCH_TYPE(s16, u16, 1),
95 	ASSIGN_FETCH_TYPE(s32, u32, 1),
96 	ASSIGN_FETCH_TYPE(s64, u64, 1),
97 	ASSIGN_FETCH_TYPE_ALIAS(x8,  u8,  u8,  0),
98 	ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
99 	ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
100 	ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
101 	ASSIGN_FETCH_TYPE_ALIAS(char, u8, u8,  0),
102 	ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0),
103 
104 	ASSIGN_FETCH_TYPE_END
105 };
106 
107 static const struct fetch_type *find_fetch_type(const char *type, unsigned long flags)
108 {
109 	int i;
110 
111 	/* Reject the symbol/symstr for uprobes */
112 	if (type && (flags & TPARG_FL_USER) &&
113 	    (!strcmp(type, "symbol") || !strcmp(type, "symstr")))
114 		return NULL;
115 
116 	if (!type)
117 		type = DEFAULT_FETCH_TYPE_STR;
118 
119 	/* Special case: bitfield */
120 	if (*type == 'b') {
121 		unsigned long bs;
122 
123 		type = strchr(type, '/');
124 		if (!type)
125 			goto fail;
126 
127 		type++;
128 		if (kstrtoul(type, 0, &bs))
129 			goto fail;
130 
131 		switch (bs) {
132 		case 8:
133 			return find_fetch_type("u8", flags);
134 		case 16:
135 			return find_fetch_type("u16", flags);
136 		case 32:
137 			return find_fetch_type("u32", flags);
138 		case 64:
139 			return find_fetch_type("u64", flags);
140 		default:
141 			goto fail;
142 		}
143 	}
144 
145 	for (i = 0; probe_fetch_types[i].name; i++) {
146 		if (strcmp(type, probe_fetch_types[i].name) == 0)
147 			return &probe_fetch_types[i];
148 	}
149 
150 fail:
151 	return NULL;
152 }
153 
154 static struct trace_probe_log trace_probe_log;
155 
156 void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
157 {
158 	trace_probe_log.subsystem = subsystem;
159 	trace_probe_log.argc = argc;
160 	trace_probe_log.argv = argv;
161 	trace_probe_log.index = 0;
162 }
163 
164 void trace_probe_log_clear(void)
165 {
166 	memset(&trace_probe_log, 0, sizeof(trace_probe_log));
167 }
168 
169 void trace_probe_log_set_index(int index)
170 {
171 	trace_probe_log.index = index;
172 }
173 
174 void __trace_probe_log_err(int offset, int err_type)
175 {
176 	char *command, *p;
177 	int i, len = 0, pos = 0;
178 
179 	if (!trace_probe_log.argv)
180 		return;
181 
182 	/* Recalculate the length and allocate buffer */
183 	for (i = 0; i < trace_probe_log.argc; i++) {
184 		if (i == trace_probe_log.index)
185 			pos = len;
186 		len += strlen(trace_probe_log.argv[i]) + 1;
187 	}
188 	command = kzalloc(len, GFP_KERNEL);
189 	if (!command)
190 		return;
191 
192 	if (trace_probe_log.index >= trace_probe_log.argc) {
193 		/**
194 		 * Set the error position is next to the last arg + space.
195 		 * Note that len includes the terminal null and the cursor
196 		 * appears at pos + 1.
197 		 */
198 		pos = len;
199 		offset = 0;
200 	}
201 
202 	/* And make a command string from argv array */
203 	p = command;
204 	for (i = 0; i < trace_probe_log.argc; i++) {
205 		len = strlen(trace_probe_log.argv[i]);
206 		strcpy(p, trace_probe_log.argv[i]);
207 		p[len] = ' ';
208 		p += len + 1;
209 	}
210 	*(p - 1) = '\0';
211 
212 	tracing_log_err(NULL, trace_probe_log.subsystem, command,
213 			trace_probe_err_text, err_type, pos + offset);
214 
215 	kfree(command);
216 }
217 
218 /* Split symbol and offset. */
219 int traceprobe_split_symbol_offset(char *symbol, long *offset)
220 {
221 	char *tmp;
222 	int ret;
223 
224 	if (!offset)
225 		return -EINVAL;
226 
227 	tmp = strpbrk(symbol, "+-");
228 	if (tmp) {
229 		ret = kstrtol(tmp, 0, offset);
230 		if (ret)
231 			return ret;
232 		*tmp = '\0';
233 	} else
234 		*offset = 0;
235 
236 	return 0;
237 }
238 
239 /* @buf must has MAX_EVENT_NAME_LEN size */
240 int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
241 				char *buf, int offset)
242 {
243 	const char *slash, *event = *pevent;
244 	int len;
245 
246 	slash = strchr(event, '/');
247 	if (!slash)
248 		slash = strchr(event, '.');
249 
250 	if (slash) {
251 		if (slash == event) {
252 			trace_probe_log_err(offset, NO_GROUP_NAME);
253 			return -EINVAL;
254 		}
255 		if (slash - event + 1 > MAX_EVENT_NAME_LEN) {
256 			trace_probe_log_err(offset, GROUP_TOO_LONG);
257 			return -EINVAL;
258 		}
259 		strlcpy(buf, event, slash - event + 1);
260 		if (!is_good_system_name(buf)) {
261 			trace_probe_log_err(offset, BAD_GROUP_NAME);
262 			return -EINVAL;
263 		}
264 		*pgroup = buf;
265 		*pevent = slash + 1;
266 		offset += slash - event + 1;
267 		event = *pevent;
268 	}
269 	len = strlen(event);
270 	if (len == 0) {
271 		if (slash) {
272 			*pevent = NULL;
273 			return 0;
274 		}
275 		trace_probe_log_err(offset, NO_EVENT_NAME);
276 		return -EINVAL;
277 	} else if (len > MAX_EVENT_NAME_LEN) {
278 		trace_probe_log_err(offset, EVENT_TOO_LONG);
279 		return -EINVAL;
280 	}
281 	if (!is_good_name(event)) {
282 		trace_probe_log_err(offset, BAD_EVENT_NAME);
283 		return -EINVAL;
284 	}
285 	return 0;
286 }
287 
288 static int parse_trace_event_arg(char *arg, struct fetch_insn *code,
289 				 struct traceprobe_parse_context *ctx)
290 {
291 	struct ftrace_event_field *field;
292 	struct list_head *head;
293 
294 	head = trace_get_fields(ctx->event);
295 	list_for_each_entry(field, head, link) {
296 		if (!strcmp(arg, field->name)) {
297 			code->op = FETCH_OP_TP_ARG;
298 			code->data = field;
299 			return 0;
300 		}
301 	}
302 	return -ENOENT;
303 }
304 
305 #ifdef CONFIG_PROBE_EVENTS_BTF_ARGS
306 
307 static struct btf *traceprobe_get_btf(void)
308 {
309 	struct btf *btf = bpf_get_btf_vmlinux();
310 
311 	if (IS_ERR_OR_NULL(btf))
312 		return NULL;
313 
314 	return btf;
315 }
316 
317 static u32 btf_type_int(const struct btf_type *t)
318 {
319 	return *(u32 *)(t + 1);
320 }
321 
322 static const char *type_from_btf_id(struct btf *btf, s32 id)
323 {
324 	const struct btf_type *t;
325 	u32 intdata;
326 	s32 tid;
327 
328 	/* TODO: const char * could be converted as a string */
329 	t = btf_type_skip_modifiers(btf, id, &tid);
330 
331 	switch (BTF_INFO_KIND(t->info)) {
332 	case BTF_KIND_ENUM:
333 		/* enum is "int", so convert to "s32" */
334 		return "s32";
335 	case BTF_KIND_ENUM64:
336 		return "s64";
337 	case BTF_KIND_PTR:
338 		/* pointer will be converted to "x??" */
339 		if (IS_ENABLED(CONFIG_64BIT))
340 			return "x64";
341 		else
342 			return "x32";
343 	case BTF_KIND_INT:
344 		intdata = btf_type_int(t);
345 		if (BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED) {
346 			switch (BTF_INT_BITS(intdata)) {
347 			case 8:
348 				return "s8";
349 			case 16:
350 				return "s16";
351 			case 32:
352 				return "s32";
353 			case 64:
354 				return "s64";
355 			}
356 		} else {	/* unsigned */
357 			switch (BTF_INT_BITS(intdata)) {
358 			case 8:
359 				return "u8";
360 			case 16:
361 				return "u16";
362 			case 32:
363 				return "u32";
364 			case 64:
365 				return "u64";
366 			}
367 		}
368 	}
369 	/* TODO: support other types */
370 
371 	return NULL;
372 }
373 
374 static const struct btf_type *find_btf_func_proto(const char *funcname)
375 {
376 	struct btf *btf = traceprobe_get_btf();
377 	const struct btf_type *t;
378 	s32 id;
379 
380 	if (!btf || !funcname)
381 		return ERR_PTR(-EINVAL);
382 
383 	id = btf_find_by_name_kind(btf, funcname, BTF_KIND_FUNC);
384 	if (id <= 0)
385 		return ERR_PTR(-ENOENT);
386 
387 	/* Get BTF_KIND_FUNC type */
388 	t = btf_type_by_id(btf, id);
389 	if (!btf_type_is_func(t))
390 		return ERR_PTR(-ENOENT);
391 
392 	/* The type of BTF_KIND_FUNC is BTF_KIND_FUNC_PROTO */
393 	t = btf_type_by_id(btf, t->type);
394 	if (!btf_type_is_func_proto(t))
395 		return ERR_PTR(-ENOENT);
396 
397 	return t;
398 }
399 
400 static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr,
401 						   bool tracepoint)
402 {
403 	const struct btf_param *param;
404 	const struct btf_type *t;
405 
406 	if (!funcname || !nr)
407 		return ERR_PTR(-EINVAL);
408 
409 	t = find_btf_func_proto(funcname);
410 	if (IS_ERR(t))
411 		return (const struct btf_param *)t;
412 
413 	*nr = btf_type_vlen(t);
414 	param = (const struct btf_param *)(t + 1);
415 
416 	/* Hide the first 'data' argument of tracepoint */
417 	if (tracepoint) {
418 		(*nr)--;
419 		param++;
420 	}
421 
422 	if (*nr > 0)
423 		return param;
424 	else
425 		return NULL;
426 }
427 
428 static int parse_btf_arg(const char *varname, struct fetch_insn *code,
429 			 struct traceprobe_parse_context *ctx)
430 {
431 	struct btf *btf = traceprobe_get_btf();
432 	const struct btf_param *params;
433 	int i;
434 
435 	if (!btf) {
436 		trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
437 		return -EOPNOTSUPP;
438 	}
439 
440 	if (WARN_ON_ONCE(!ctx->funcname))
441 		return -EINVAL;
442 
443 	if (!ctx->params) {
444 		params = find_btf_func_param(ctx->funcname, &ctx->nr_params,
445 					     ctx->flags & TPARG_FL_TPOINT);
446 		if (IS_ERR(params)) {
447 			trace_probe_log_err(ctx->offset, NO_BTF_ENTRY);
448 			return PTR_ERR(params);
449 		}
450 		ctx->params = params;
451 	} else
452 		params = ctx->params;
453 
454 	for (i = 0; i < ctx->nr_params; i++) {
455 		const char *name = btf_name_by_offset(btf, params[i].name_off);
456 
457 		if (name && !strcmp(name, varname)) {
458 			code->op = FETCH_OP_ARG;
459 			code->param = i;
460 			return 0;
461 		}
462 	}
463 	trace_probe_log_err(ctx->offset, NO_BTFARG);
464 	return -ENOENT;
465 }
466 
467 static const struct fetch_type *parse_btf_arg_type(int arg_idx,
468 					struct traceprobe_parse_context *ctx)
469 {
470 	struct btf *btf = traceprobe_get_btf();
471 	const char *typestr = NULL;
472 
473 	if (btf && ctx->params)
474 		typestr = type_from_btf_id(btf, ctx->params[arg_idx].type);
475 
476 	return find_fetch_type(typestr, ctx->flags);
477 }
478 
479 static const struct fetch_type *parse_btf_retval_type(
480 					struct traceprobe_parse_context *ctx)
481 {
482 	struct btf *btf = traceprobe_get_btf();
483 	const char *typestr = NULL;
484 	const struct btf_type *t;
485 
486 	if (btf && ctx->funcname) {
487 		t = find_btf_func_proto(ctx->funcname);
488 		if (!IS_ERR(t))
489 			typestr = type_from_btf_id(btf, t->type);
490 	}
491 
492 	return find_fetch_type(typestr, ctx->flags);
493 }
494 
495 static bool is_btf_retval_void(const char *funcname)
496 {
497 	const struct btf_type *t;
498 
499 	t = find_btf_func_proto(funcname);
500 	if (IS_ERR(t))
501 		return false;
502 
503 	return t->type == 0;
504 }
505 #else
506 static struct btf *traceprobe_get_btf(void)
507 {
508 	return NULL;
509 }
510 
511 static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr,
512 						   bool tracepoint)
513 {
514 	return ERR_PTR(-EOPNOTSUPP);
515 }
516 
517 static int parse_btf_arg(const char *varname, struct fetch_insn *code,
518 			 struct traceprobe_parse_context *ctx)
519 {
520 	trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
521 	return -EOPNOTSUPP;
522 }
523 
524 #define parse_btf_arg_type(idx, ctx)		\
525 	find_fetch_type(NULL, ctx->flags)
526 
527 #define parse_btf_retval_type(ctx)		\
528 	find_fetch_type(NULL, ctx->flags)
529 
530 #define is_btf_retval_void(funcname)	(false)
531 
532 #endif
533 
534 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
535 
536 static int parse_probe_vars(char *arg, const struct fetch_type *t,
537 			    struct fetch_insn *code,
538 			    struct traceprobe_parse_context *ctx)
539 {
540 	unsigned long param;
541 	int err = TP_ERR_BAD_VAR;
542 	int ret = 0;
543 	int len;
544 
545 	if (ctx->flags & TPARG_FL_TEVENT) {
546 		if (code->data)
547 			return -EFAULT;
548 		ret = parse_trace_event_arg(arg, code, ctx);
549 		if (!ret)
550 			return 0;
551 		if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
552 			code->op = FETCH_OP_COMM;
553 			return 0;
554 		}
555 		/* backward compatibility */
556 		ctx->offset = 0;
557 		goto inval;
558 	}
559 
560 	if (strcmp(arg, "retval") == 0) {
561 		if (ctx->flags & TPARG_FL_RETURN) {
562 			if ((ctx->flags & TPARG_FL_KERNEL) &&
563 			    is_btf_retval_void(ctx->funcname)) {
564 				err = TP_ERR_NO_RETVAL;
565 				goto inval;
566 			}
567 			code->op = FETCH_OP_RETVAL;
568 			return 0;
569 		}
570 		err = TP_ERR_RETVAL_ON_PROBE;
571 		goto inval;
572 	}
573 
574 	len = str_has_prefix(arg, "stack");
575 	if (len) {
576 
577 		if (arg[len] == '\0') {
578 			code->op = FETCH_OP_STACKP;
579 			return 0;
580 		}
581 
582 		if (isdigit(arg[len])) {
583 			ret = kstrtoul(arg + len, 10, &param);
584 			if (ret)
585 				goto inval;
586 
587 			if ((ctx->flags & TPARG_FL_KERNEL) &&
588 			    param > PARAM_MAX_STACK) {
589 				err = TP_ERR_BAD_STACK_NUM;
590 				goto inval;
591 			}
592 			code->op = FETCH_OP_STACK;
593 			code->param = (unsigned int)param;
594 			return 0;
595 		}
596 		goto inval;
597 	}
598 
599 	if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
600 		code->op = FETCH_OP_COMM;
601 		return 0;
602 	}
603 
604 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
605 	len = str_has_prefix(arg, "arg");
606 	if (len && tparg_is_function_entry(ctx->flags)) {
607 		ret = kstrtoul(arg + len, 10, &param);
608 		if (ret)
609 			goto inval;
610 
611 		if (!param || param > PARAM_MAX_STACK) {
612 			err = TP_ERR_BAD_ARG_NUM;
613 			goto inval;
614 		}
615 
616 		code->op = FETCH_OP_ARG;
617 		code->param = (unsigned int)param - 1;
618 		/*
619 		 * The tracepoint probe will probe a stub function, and the
620 		 * first parameter of the stub is a dummy and should be ignored.
621 		 */
622 		if (ctx->flags & TPARG_FL_TPOINT)
623 			code->param++;
624 		return 0;
625 	}
626 #endif
627 
628 inval:
629 	__trace_probe_log_err(ctx->offset, err);
630 	return -EINVAL;
631 }
632 
633 static int str_to_immediate(char *str, unsigned long *imm)
634 {
635 	if (isdigit(str[0]))
636 		return kstrtoul(str, 0, imm);
637 	else if (str[0] == '-')
638 		return kstrtol(str, 0, (long *)imm);
639 	else if (str[0] == '+')
640 		return kstrtol(str + 1, 0, (long *)imm);
641 	return -EINVAL;
642 }
643 
644 static int __parse_imm_string(char *str, char **pbuf, int offs)
645 {
646 	size_t len = strlen(str);
647 
648 	if (str[len - 1] != '"') {
649 		trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE);
650 		return -EINVAL;
651 	}
652 	*pbuf = kstrndup(str, len - 1, GFP_KERNEL);
653 	if (!*pbuf)
654 		return -ENOMEM;
655 	return 0;
656 }
657 
658 /* Recursive argument parser */
659 static int
660 parse_probe_arg(char *arg, const struct fetch_type *type,
661 		struct fetch_insn **pcode, struct fetch_insn *end,
662 		struct traceprobe_parse_context *ctx)
663 {
664 	struct fetch_insn *code = *pcode;
665 	unsigned long param;
666 	int deref = FETCH_OP_DEREF;
667 	long offset = 0;
668 	char *tmp;
669 	int ret = 0;
670 
671 	switch (arg[0]) {
672 	case '$':
673 		ret = parse_probe_vars(arg + 1, type, code, ctx);
674 		break;
675 
676 	case '%':	/* named register */
677 		if (ctx->flags & (TPARG_FL_TEVENT | TPARG_FL_FPROBE)) {
678 			/* eprobe and fprobe do not handle registers */
679 			trace_probe_log_err(ctx->offset, BAD_VAR);
680 			break;
681 		}
682 		ret = regs_query_register_offset(arg + 1);
683 		if (ret >= 0) {
684 			code->op = FETCH_OP_REG;
685 			code->param = (unsigned int)ret;
686 			ret = 0;
687 		} else
688 			trace_probe_log_err(ctx->offset, BAD_REG_NAME);
689 		break;
690 
691 	case '@':	/* memory, file-offset or symbol */
692 		if (isdigit(arg[1])) {
693 			ret = kstrtoul(arg + 1, 0, &param);
694 			if (ret) {
695 				trace_probe_log_err(ctx->offset, BAD_MEM_ADDR);
696 				break;
697 			}
698 			/* load address */
699 			code->op = FETCH_OP_IMM;
700 			code->immediate = param;
701 		} else if (arg[1] == '+') {
702 			/* kprobes don't support file offsets */
703 			if (ctx->flags & TPARG_FL_KERNEL) {
704 				trace_probe_log_err(ctx->offset, FILE_ON_KPROBE);
705 				return -EINVAL;
706 			}
707 			ret = kstrtol(arg + 2, 0, &offset);
708 			if (ret) {
709 				trace_probe_log_err(ctx->offset, BAD_FILE_OFFS);
710 				break;
711 			}
712 
713 			code->op = FETCH_OP_FOFFS;
714 			code->immediate = (unsigned long)offset;  // imm64?
715 		} else {
716 			/* uprobes don't support symbols */
717 			if (!(ctx->flags & TPARG_FL_KERNEL)) {
718 				trace_probe_log_err(ctx->offset, SYM_ON_UPROBE);
719 				return -EINVAL;
720 			}
721 			/* Preserve symbol for updating */
722 			code->op = FETCH_NOP_SYMBOL;
723 			code->data = kstrdup(arg + 1, GFP_KERNEL);
724 			if (!code->data)
725 				return -ENOMEM;
726 			if (++code == end) {
727 				trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
728 				return -EINVAL;
729 			}
730 			code->op = FETCH_OP_IMM;
731 			code->immediate = 0;
732 		}
733 		/* These are fetching from memory */
734 		if (++code == end) {
735 			trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
736 			return -EINVAL;
737 		}
738 		*pcode = code;
739 		code->op = FETCH_OP_DEREF;
740 		code->offset = offset;
741 		break;
742 
743 	case '+':	/* deref memory */
744 	case '-':
745 		if (arg[1] == 'u') {
746 			deref = FETCH_OP_UDEREF;
747 			arg[1] = arg[0];
748 			arg++;
749 		}
750 		if (arg[0] == '+')
751 			arg++;	/* Skip '+', because kstrtol() rejects it. */
752 		tmp = strchr(arg, '(');
753 		if (!tmp) {
754 			trace_probe_log_err(ctx->offset, DEREF_NEED_BRACE);
755 			return -EINVAL;
756 		}
757 		*tmp = '\0';
758 		ret = kstrtol(arg, 0, &offset);
759 		if (ret) {
760 			trace_probe_log_err(ctx->offset, BAD_DEREF_OFFS);
761 			break;
762 		}
763 		ctx->offset += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0);
764 		arg = tmp + 1;
765 		tmp = strrchr(arg, ')');
766 		if (!tmp) {
767 			trace_probe_log_err(ctx->offset + strlen(arg),
768 					    DEREF_OPEN_BRACE);
769 			return -EINVAL;
770 		} else {
771 			const struct fetch_type *t2 = find_fetch_type(NULL, ctx->flags);
772 			int cur_offs = ctx->offset;
773 
774 			*tmp = '\0';
775 			ret = parse_probe_arg(arg, t2, &code, end, ctx);
776 			if (ret)
777 				break;
778 			ctx->offset = cur_offs;
779 			if (code->op == FETCH_OP_COMM ||
780 			    code->op == FETCH_OP_DATA) {
781 				trace_probe_log_err(ctx->offset, COMM_CANT_DEREF);
782 				return -EINVAL;
783 			}
784 			if (++code == end) {
785 				trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
786 				return -EINVAL;
787 			}
788 			*pcode = code;
789 
790 			code->op = deref;
791 			code->offset = offset;
792 		}
793 		break;
794 	case '\\':	/* Immediate value */
795 		if (arg[1] == '"') {	/* Immediate string */
796 			ret = __parse_imm_string(arg + 2, &tmp, ctx->offset + 2);
797 			if (ret)
798 				break;
799 			code->op = FETCH_OP_DATA;
800 			code->data = tmp;
801 		} else {
802 			ret = str_to_immediate(arg + 1, &code->immediate);
803 			if (ret)
804 				trace_probe_log_err(ctx->offset + 1, BAD_IMM);
805 			else
806 				code->op = FETCH_OP_IMM;
807 		}
808 		break;
809 	default:
810 		if (isalpha(arg[0]) || arg[0] == '_') {	/* BTF variable */
811 			if (!tparg_is_function_entry(ctx->flags)) {
812 				trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
813 				return -EINVAL;
814 			}
815 			ret = parse_btf_arg(arg, code, ctx);
816 			break;
817 		}
818 	}
819 	if (!ret && code->op == FETCH_OP_NOP) {
820 		/* Parsed, but do not find fetch method */
821 		trace_probe_log_err(ctx->offset, BAD_FETCH_ARG);
822 		ret = -EINVAL;
823 	}
824 	return ret;
825 }
826 
827 #define BYTES_TO_BITS(nb)	((BITS_PER_LONG * (nb)) / sizeof(long))
828 
829 /* Bitfield type needs to be parsed into a fetch function */
830 static int __parse_bitfield_probe_arg(const char *bf,
831 				      const struct fetch_type *t,
832 				      struct fetch_insn **pcode)
833 {
834 	struct fetch_insn *code = *pcode;
835 	unsigned long bw, bo;
836 	char *tail;
837 
838 	if (*bf != 'b')
839 		return 0;
840 
841 	bw = simple_strtoul(bf + 1, &tail, 0);	/* Use simple one */
842 
843 	if (bw == 0 || *tail != '@')
844 		return -EINVAL;
845 
846 	bf = tail + 1;
847 	bo = simple_strtoul(bf, &tail, 0);
848 
849 	if (tail == bf || *tail != '/')
850 		return -EINVAL;
851 	code++;
852 	if (code->op != FETCH_OP_NOP)
853 		return -EINVAL;
854 	*pcode = code;
855 
856 	code->op = FETCH_OP_MOD_BF;
857 	code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
858 	code->rshift = BYTES_TO_BITS(t->size) - bw;
859 	code->basesize = t->size;
860 
861 	return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
862 }
863 
864 /* String length checking wrapper */
865 static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size,
866 					   struct probe_arg *parg,
867 					   struct traceprobe_parse_context *ctx)
868 {
869 	struct fetch_insn *code, *scode, *tmp = NULL;
870 	char *t, *t2, *t3;
871 	int ret, len;
872 	char *arg;
873 
874 	arg = kstrdup(argv, GFP_KERNEL);
875 	if (!arg)
876 		return -ENOMEM;
877 
878 	ret = -EINVAL;
879 	len = strlen(arg);
880 	if (len > MAX_ARGSTR_LEN) {
881 		trace_probe_log_err(ctx->offset, ARG_TOO_LONG);
882 		goto out;
883 	} else if (len == 0) {
884 		trace_probe_log_err(ctx->offset, NO_ARG_BODY);
885 		goto out;
886 	}
887 
888 	ret = -ENOMEM;
889 	parg->comm = kstrdup(arg, GFP_KERNEL);
890 	if (!parg->comm)
891 		goto out;
892 
893 	ret = -EINVAL;
894 	t = strchr(arg, ':');
895 	if (t) {
896 		*t = '\0';
897 		t2 = strchr(++t, '[');
898 		if (t2) {
899 			*t2++ = '\0';
900 			t3 = strchr(t2, ']');
901 			if (!t3) {
902 				int offs = t2 + strlen(t2) - arg;
903 
904 				trace_probe_log_err(ctx->offset + offs,
905 						    ARRAY_NO_CLOSE);
906 				goto out;
907 			} else if (t3[1] != '\0') {
908 				trace_probe_log_err(ctx->offset + t3 + 1 - arg,
909 						    BAD_ARRAY_SUFFIX);
910 				goto out;
911 			}
912 			*t3 = '\0';
913 			if (kstrtouint(t2, 0, &parg->count) || !parg->count) {
914 				trace_probe_log_err(ctx->offset + t2 - arg,
915 						    BAD_ARRAY_NUM);
916 				goto out;
917 			}
918 			if (parg->count > MAX_ARRAY_LEN) {
919 				trace_probe_log_err(ctx->offset + t2 - arg,
920 						    ARRAY_TOO_BIG);
921 				goto out;
922 			}
923 		}
924 	}
925 
926 	/*
927 	 * Since $comm and immediate string can not be dereferenced,
928 	 * we can find those by strcmp. But ignore for eprobes.
929 	 */
930 	if (!(ctx->flags & TPARG_FL_TEVENT) &&
931 	    (strcmp(arg, "$comm") == 0 || strcmp(arg, "$COMM") == 0 ||
932 	     strncmp(arg, "\\\"", 2) == 0)) {
933 		/* The type of $comm must be "string", and not an array. */
934 		if (parg->count || (t && strcmp(t, "string")))
935 			goto out;
936 		parg->type = find_fetch_type("string", ctx->flags);
937 	} else
938 		parg->type = find_fetch_type(t, ctx->flags);
939 	if (!parg->type) {
940 		trace_probe_log_err(ctx->offset + (t ? (t - arg) : 0), BAD_TYPE);
941 		goto out;
942 	}
943 	parg->offset = *size;
944 	*size += parg->type->size * (parg->count ?: 1);
945 
946 	ret = -ENOMEM;
947 	if (parg->count) {
948 		len = strlen(parg->type->fmttype) + 6;
949 		parg->fmt = kmalloc(len, GFP_KERNEL);
950 		if (!parg->fmt)
951 			goto out;
952 		snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
953 			 parg->count);
954 	}
955 
956 	code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL);
957 	if (!code)
958 		goto out;
959 	code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
960 
961 	ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
962 			      ctx);
963 	if (ret)
964 		goto fail;
965 
966 	/* Update storing type if BTF is available */
967 	if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) && !t) {
968 		if (code->op == FETCH_OP_ARG)
969 			parg->type = parse_btf_arg_type(code->param, ctx);
970 		else if (code->op == FETCH_OP_RETVAL)
971 			parg->type = parse_btf_retval_type(ctx);
972 	}
973 
974 	ret = -EINVAL;
975 	/* Store operation */
976 	if (parg->type->is_string) {
977 		if (!strcmp(parg->type->name, "symstr")) {
978 			if (code->op != FETCH_OP_REG && code->op != FETCH_OP_STACK &&
979 			    code->op != FETCH_OP_RETVAL && code->op != FETCH_OP_ARG &&
980 			    code->op != FETCH_OP_DEREF && code->op != FETCH_OP_TP_ARG) {
981 				trace_probe_log_err(ctx->offset + (t ? (t - arg) : 0),
982 						    BAD_SYMSTRING);
983 				goto fail;
984 			}
985 		} else {
986 			if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF &&
987 			    code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM &&
988 			    code->op != FETCH_OP_DATA && code->op != FETCH_OP_TP_ARG) {
989 				trace_probe_log_err(ctx->offset + (t ? (t - arg) : 0),
990 						    BAD_STRING);
991 				goto fail;
992 			}
993 		}
994 		if (!strcmp(parg->type->name, "symstr") ||
995 		    (code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM ||
996 		     code->op == FETCH_OP_DATA) || code->op == FETCH_OP_TP_ARG ||
997 		     parg->count) {
998 			/*
999 			 * IMM, DATA and COMM is pointing actual address, those
1000 			 * must be kept, and if parg->count != 0, this is an
1001 			 * array of string pointers instead of string address
1002 			 * itself.
1003 			 * For the symstr, it doesn't need to dereference, thus
1004 			 * it just get the value.
1005 			 */
1006 			code++;
1007 			if (code->op != FETCH_OP_NOP) {
1008 				trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1009 				goto fail;
1010 			}
1011 		}
1012 		/* If op == DEREF, replace it with STRING */
1013 		if (!strcmp(parg->type->name, "ustring") ||
1014 		    code->op == FETCH_OP_UDEREF)
1015 			code->op = FETCH_OP_ST_USTRING;
1016 		else if (!strcmp(parg->type->name, "symstr"))
1017 			code->op = FETCH_OP_ST_SYMSTR;
1018 		else
1019 			code->op = FETCH_OP_ST_STRING;
1020 		code->size = parg->type->size;
1021 		parg->dynamic = true;
1022 	} else if (code->op == FETCH_OP_DEREF) {
1023 		code->op = FETCH_OP_ST_MEM;
1024 		code->size = parg->type->size;
1025 	} else if (code->op == FETCH_OP_UDEREF) {
1026 		code->op = FETCH_OP_ST_UMEM;
1027 		code->size = parg->type->size;
1028 	} else {
1029 		code++;
1030 		if (code->op != FETCH_OP_NOP) {
1031 			trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1032 			goto fail;
1033 		}
1034 		code->op = FETCH_OP_ST_RAW;
1035 		code->size = parg->type->size;
1036 	}
1037 	scode = code;
1038 	/* Modify operation */
1039 	if (t != NULL) {
1040 		ret = __parse_bitfield_probe_arg(t, parg->type, &code);
1041 		if (ret) {
1042 			trace_probe_log_err(ctx->offset + t - arg, BAD_BITFIELD);
1043 			goto fail;
1044 		}
1045 	}
1046 	ret = -EINVAL;
1047 	/* Loop(Array) operation */
1048 	if (parg->count) {
1049 		if (scode->op != FETCH_OP_ST_MEM &&
1050 		    scode->op != FETCH_OP_ST_STRING &&
1051 		    scode->op != FETCH_OP_ST_USTRING) {
1052 			trace_probe_log_err(ctx->offset + (t ? (t - arg) : 0),
1053 					    BAD_STRING);
1054 			goto fail;
1055 		}
1056 		code++;
1057 		if (code->op != FETCH_OP_NOP) {
1058 			trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1059 			goto fail;
1060 		}
1061 		code->op = FETCH_OP_LP_ARRAY;
1062 		code->param = parg->count;
1063 	}
1064 	code++;
1065 	code->op = FETCH_OP_END;
1066 
1067 	ret = 0;
1068 	/* Shrink down the code buffer */
1069 	parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL);
1070 	if (!parg->code)
1071 		ret = -ENOMEM;
1072 	else
1073 		memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
1074 
1075 fail:
1076 	if (ret) {
1077 		for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
1078 			if (code->op == FETCH_NOP_SYMBOL ||
1079 			    code->op == FETCH_OP_DATA)
1080 				kfree(code->data);
1081 	}
1082 	kfree(tmp);
1083 out:
1084 	kfree(arg);
1085 
1086 	return ret;
1087 }
1088 
1089 /* Return 1 if name is reserved or already used by another argument */
1090 static int traceprobe_conflict_field_name(const char *name,
1091 					  struct probe_arg *args, int narg)
1092 {
1093 	int i;
1094 
1095 	for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
1096 		if (strcmp(reserved_field_names[i], name) == 0)
1097 			return 1;
1098 
1099 	for (i = 0; i < narg; i++)
1100 		if (strcmp(args[i].name, name) == 0)
1101 			return 1;
1102 
1103 	return 0;
1104 }
1105 
1106 static char *generate_probe_arg_name(const char *arg, int idx)
1107 {
1108 	char *name = NULL;
1109 	const char *end;
1110 
1111 	/*
1112 	 * If argument name is omitted, try arg as a name (BTF variable)
1113 	 * or "argN".
1114 	 */
1115 	if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS)) {
1116 		end = strchr(arg, ':');
1117 		if (!end)
1118 			end = arg + strlen(arg);
1119 
1120 		name = kmemdup_nul(arg, end - arg, GFP_KERNEL);
1121 		if (!name || !is_good_name(name)) {
1122 			kfree(name);
1123 			name = NULL;
1124 		}
1125 	}
1126 
1127 	if (!name)
1128 		name = kasprintf(GFP_KERNEL, "arg%d", idx + 1);
1129 
1130 	return name;
1131 }
1132 
1133 int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg,
1134 			       struct traceprobe_parse_context *ctx)
1135 {
1136 	struct probe_arg *parg = &tp->args[i];
1137 	const char *body;
1138 
1139 	/* Increment count for freeing args in error case */
1140 	tp->nr_args++;
1141 
1142 	body = strchr(arg, '=');
1143 	if (body) {
1144 		if (body - arg > MAX_ARG_NAME_LEN) {
1145 			trace_probe_log_err(0, ARG_NAME_TOO_LONG);
1146 			return -EINVAL;
1147 		} else if (body == arg) {
1148 			trace_probe_log_err(0, NO_ARG_NAME);
1149 			return -EINVAL;
1150 		}
1151 		parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
1152 		body++;
1153 	} else {
1154 		parg->name = generate_probe_arg_name(arg, i);
1155 		body = arg;
1156 	}
1157 	if (!parg->name)
1158 		return -ENOMEM;
1159 
1160 	if (!is_good_name(parg->name)) {
1161 		trace_probe_log_err(0, BAD_ARG_NAME);
1162 		return -EINVAL;
1163 	}
1164 	if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
1165 		trace_probe_log_err(0, USED_ARG_NAME);
1166 		return -EINVAL;
1167 	}
1168 	ctx->offset = body - arg;
1169 	/* Parse fetch argument */
1170 	return traceprobe_parse_probe_arg_body(body, &tp->size, parg, ctx);
1171 }
1172 
1173 void traceprobe_free_probe_arg(struct probe_arg *arg)
1174 {
1175 	struct fetch_insn *code = arg->code;
1176 
1177 	while (code && code->op != FETCH_OP_END) {
1178 		if (code->op == FETCH_NOP_SYMBOL ||
1179 		    code->op == FETCH_OP_DATA)
1180 			kfree(code->data);
1181 		code++;
1182 	}
1183 	kfree(arg->code);
1184 	kfree(arg->name);
1185 	kfree(arg->comm);
1186 	kfree(arg->fmt);
1187 }
1188 
1189 static int argv_has_var_arg(int argc, const char *argv[], int *args_idx,
1190 			    struct traceprobe_parse_context *ctx)
1191 {
1192 	int i, found = 0;
1193 
1194 	for (i = 0; i < argc; i++)
1195 		if (str_has_prefix(argv[i], "$arg")) {
1196 			trace_probe_log_set_index(i + 2);
1197 
1198 			if (!tparg_is_function_entry(ctx->flags)) {
1199 				trace_probe_log_err(0, NOFENTRY_ARGS);
1200 				return -EINVAL;
1201 			}
1202 
1203 			if (isdigit(argv[i][4])) {
1204 				found = 1;
1205 				continue;
1206 			}
1207 
1208 			if (argv[i][4] != '*') {
1209 				trace_probe_log_err(0, BAD_VAR);
1210 				return -EINVAL;
1211 			}
1212 
1213 			if (*args_idx >= 0 && *args_idx < argc) {
1214 				trace_probe_log_err(0, DOUBLE_ARGS);
1215 				return -EINVAL;
1216 			}
1217 			found = 1;
1218 			*args_idx = i;
1219 		}
1220 
1221 	return found;
1222 }
1223 
1224 static int sprint_nth_btf_arg(int idx, const char *type,
1225 			      char *buf, int bufsize,
1226 			      struct traceprobe_parse_context *ctx)
1227 {
1228 	struct btf *btf = traceprobe_get_btf();
1229 	const char *name;
1230 	int ret;
1231 
1232 	if (idx >= ctx->nr_params) {
1233 		trace_probe_log_err(0, NO_BTFARG);
1234 		return -ENOENT;
1235 	}
1236 	name = btf_name_by_offset(btf, ctx->params[idx].name_off);
1237 	if (!name) {
1238 		trace_probe_log_err(0, NO_BTF_ENTRY);
1239 		return -ENOENT;
1240 	}
1241 	ret = snprintf(buf, bufsize, "%s%s", name, type);
1242 	if (ret >= bufsize) {
1243 		trace_probe_log_err(0, ARGS_2LONG);
1244 		return -E2BIG;
1245 	}
1246 	return ret;
1247 }
1248 
1249 /* Return new_argv which must be freed after use */
1250 const char **traceprobe_expand_meta_args(int argc, const char *argv[],
1251 					 int *new_argc, char *buf, int bufsize,
1252 					 struct traceprobe_parse_context *ctx)
1253 {
1254 	const struct btf_param *params = NULL;
1255 	int i, j, n, used, ret, args_idx = -1;
1256 	const char **new_argv = NULL;
1257 	int nr_params;
1258 
1259 	ret = argv_has_var_arg(argc, argv, &args_idx, ctx);
1260 	if (ret < 0)
1261 		return ERR_PTR(ret);
1262 
1263 	if (!ret) {
1264 		*new_argc = argc;
1265 		return NULL;
1266 	}
1267 
1268 	params = find_btf_func_param(ctx->funcname, &nr_params,
1269 				     ctx->flags & TPARG_FL_TPOINT);
1270 	if (IS_ERR(params)) {
1271 		if (args_idx != -1) {
1272 			/* $arg* requires BTF info */
1273 			trace_probe_log_err(0, NOSUP_BTFARG);
1274 			return (const char **)params;
1275 		}
1276 		return 0;
1277 	}
1278 	ctx->params = params;
1279 	ctx->nr_params = nr_params;
1280 
1281 	if (args_idx >= 0)
1282 		*new_argc = argc + ctx->nr_params - 1;
1283 	else
1284 		*new_argc = argc;
1285 
1286 	new_argv = kcalloc(*new_argc, sizeof(char *), GFP_KERNEL);
1287 	if (!new_argv)
1288 		return ERR_PTR(-ENOMEM);
1289 
1290 	used = 0;
1291 	for (i = 0, j = 0; i < argc; i++) {
1292 		trace_probe_log_set_index(i + 2);
1293 		if (i == args_idx) {
1294 			for (n = 0; n < nr_params; n++) {
1295 				ret = sprint_nth_btf_arg(n, "", buf + used,
1296 							 bufsize - used, ctx);
1297 				if (ret < 0)
1298 					goto error;
1299 
1300 				new_argv[j++] = buf + used;
1301 				used += ret + 1;
1302 			}
1303 			continue;
1304 		}
1305 
1306 		if (str_has_prefix(argv[i], "$arg")) {
1307 			char *type = NULL;
1308 
1309 			n = simple_strtoul(argv[i] + 4, &type, 10);
1310 			if (type && !(*type == ':' || *type == '\0')) {
1311 				trace_probe_log_err(0, BAD_VAR);
1312 				ret = -ENOENT;
1313 				goto error;
1314 			}
1315 			/* Note: $argN starts from $arg1 */
1316 			ret = sprint_nth_btf_arg(n - 1, type, buf + used,
1317 						 bufsize - used, ctx);
1318 			if (ret < 0)
1319 				goto error;
1320 			new_argv[j++] = buf + used;
1321 			used += ret + 1;
1322 		} else
1323 			new_argv[j++] = argv[i];
1324 	}
1325 
1326 	return new_argv;
1327 
1328 error:
1329 	kfree(new_argv);
1330 	return ERR_PTR(ret);
1331 }
1332 
1333 int traceprobe_update_arg(struct probe_arg *arg)
1334 {
1335 	struct fetch_insn *code = arg->code;
1336 	long offset;
1337 	char *tmp;
1338 	char c;
1339 	int ret = 0;
1340 
1341 	while (code && code->op != FETCH_OP_END) {
1342 		if (code->op == FETCH_NOP_SYMBOL) {
1343 			if (code[1].op != FETCH_OP_IMM)
1344 				return -EINVAL;
1345 
1346 			tmp = strpbrk(code->data, "+-");
1347 			if (tmp)
1348 				c = *tmp;
1349 			ret = traceprobe_split_symbol_offset(code->data,
1350 							     &offset);
1351 			if (ret)
1352 				return ret;
1353 
1354 			code[1].immediate =
1355 				(unsigned long)kallsyms_lookup_name(code->data);
1356 			if (tmp)
1357 				*tmp = c;
1358 			if (!code[1].immediate)
1359 				return -ENOENT;
1360 			code[1].immediate += offset;
1361 		}
1362 		code++;
1363 	}
1364 	return 0;
1365 }
1366 
1367 /* When len=0, we just calculate the needed length */
1368 #define LEN_OR_ZERO (len ? len - pos : 0)
1369 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
1370 			   enum probe_print_type ptype)
1371 {
1372 	struct probe_arg *parg;
1373 	int i, j;
1374 	int pos = 0;
1375 	const char *fmt, *arg;
1376 
1377 	switch (ptype) {
1378 	case PROBE_PRINT_NORMAL:
1379 		fmt = "(%lx)";
1380 		arg = ", REC->" FIELD_STRING_IP;
1381 		break;
1382 	case PROBE_PRINT_RETURN:
1383 		fmt = "(%lx <- %lx)";
1384 		arg = ", REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
1385 		break;
1386 	case PROBE_PRINT_EVENT:
1387 		fmt = "";
1388 		arg = "";
1389 		break;
1390 	default:
1391 		WARN_ON_ONCE(1);
1392 		return 0;
1393 	}
1394 
1395 	pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
1396 
1397 	for (i = 0; i < tp->nr_args; i++) {
1398 		parg = tp->args + i;
1399 		pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
1400 		if (parg->count) {
1401 			pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
1402 					parg->type->fmt);
1403 			for (j = 1; j < parg->count; j++)
1404 				pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
1405 						parg->type->fmt);
1406 			pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
1407 		} else
1408 			pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
1409 					parg->type->fmt);
1410 	}
1411 
1412 	pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", arg);
1413 
1414 	for (i = 0; i < tp->nr_args; i++) {
1415 		parg = tp->args + i;
1416 		if (parg->count) {
1417 			if (parg->type->is_string)
1418 				fmt = ", __get_str(%s[%d])";
1419 			else
1420 				fmt = ", REC->%s[%d]";
1421 			for (j = 0; j < parg->count; j++)
1422 				pos += snprintf(buf + pos, LEN_OR_ZERO,
1423 						fmt, parg->name, j);
1424 		} else {
1425 			if (parg->type->is_string)
1426 				fmt = ", __get_str(%s)";
1427 			else
1428 				fmt = ", REC->%s";
1429 			pos += snprintf(buf + pos, LEN_OR_ZERO,
1430 					fmt, parg->name);
1431 		}
1432 	}
1433 
1434 	/* return the length of print_fmt */
1435 	return pos;
1436 }
1437 #undef LEN_OR_ZERO
1438 
1439 int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype)
1440 {
1441 	struct trace_event_call *call = trace_probe_event_call(tp);
1442 	int len;
1443 	char *print_fmt;
1444 
1445 	/* First: called with 0 length to calculate the needed length */
1446 	len = __set_print_fmt(tp, NULL, 0, ptype);
1447 	print_fmt = kmalloc(len + 1, GFP_KERNEL);
1448 	if (!print_fmt)
1449 		return -ENOMEM;
1450 
1451 	/* Second: actually write the @print_fmt */
1452 	__set_print_fmt(tp, print_fmt, len + 1, ptype);
1453 	call->print_fmt = print_fmt;
1454 
1455 	return 0;
1456 }
1457 
1458 int traceprobe_define_arg_fields(struct trace_event_call *event_call,
1459 				 size_t offset, struct trace_probe *tp)
1460 {
1461 	int ret, i;
1462 
1463 	/* Set argument names as fields */
1464 	for (i = 0; i < tp->nr_args; i++) {
1465 		struct probe_arg *parg = &tp->args[i];
1466 		const char *fmt = parg->type->fmttype;
1467 		int size = parg->type->size;
1468 
1469 		if (parg->fmt)
1470 			fmt = parg->fmt;
1471 		if (parg->count)
1472 			size *= parg->count;
1473 		ret = trace_define_field(event_call, fmt, parg->name,
1474 					 offset + parg->offset, size,
1475 					 parg->type->is_signed,
1476 					 FILTER_OTHER);
1477 		if (ret)
1478 			return ret;
1479 	}
1480 	return 0;
1481 }
1482 
1483 static void trace_probe_event_free(struct trace_probe_event *tpe)
1484 {
1485 	kfree(tpe->class.system);
1486 	kfree(tpe->call.name);
1487 	kfree(tpe->call.print_fmt);
1488 	kfree(tpe);
1489 }
1490 
1491 int trace_probe_append(struct trace_probe *tp, struct trace_probe *to)
1492 {
1493 	if (trace_probe_has_sibling(tp))
1494 		return -EBUSY;
1495 
1496 	list_del_init(&tp->list);
1497 	trace_probe_event_free(tp->event);
1498 
1499 	tp->event = to->event;
1500 	list_add_tail(&tp->list, trace_probe_probe_list(to));
1501 
1502 	return 0;
1503 }
1504 
1505 void trace_probe_unlink(struct trace_probe *tp)
1506 {
1507 	list_del_init(&tp->list);
1508 	if (list_empty(trace_probe_probe_list(tp)))
1509 		trace_probe_event_free(tp->event);
1510 	tp->event = NULL;
1511 }
1512 
1513 void trace_probe_cleanup(struct trace_probe *tp)
1514 {
1515 	int i;
1516 
1517 	for (i = 0; i < tp->nr_args; i++)
1518 		traceprobe_free_probe_arg(&tp->args[i]);
1519 
1520 	if (tp->event)
1521 		trace_probe_unlink(tp);
1522 }
1523 
1524 int trace_probe_init(struct trace_probe *tp, const char *event,
1525 		     const char *group, bool alloc_filter)
1526 {
1527 	struct trace_event_call *call;
1528 	size_t size = sizeof(struct trace_probe_event);
1529 	int ret = 0;
1530 
1531 	if (!event || !group)
1532 		return -EINVAL;
1533 
1534 	if (alloc_filter)
1535 		size += sizeof(struct trace_uprobe_filter);
1536 
1537 	tp->event = kzalloc(size, GFP_KERNEL);
1538 	if (!tp->event)
1539 		return -ENOMEM;
1540 
1541 	INIT_LIST_HEAD(&tp->event->files);
1542 	INIT_LIST_HEAD(&tp->event->class.fields);
1543 	INIT_LIST_HEAD(&tp->event->probes);
1544 	INIT_LIST_HEAD(&tp->list);
1545 	list_add(&tp->list, &tp->event->probes);
1546 
1547 	call = trace_probe_event_call(tp);
1548 	call->class = &tp->event->class;
1549 	call->name = kstrdup(event, GFP_KERNEL);
1550 	if (!call->name) {
1551 		ret = -ENOMEM;
1552 		goto error;
1553 	}
1554 
1555 	tp->event->class.system = kstrdup(group, GFP_KERNEL);
1556 	if (!tp->event->class.system) {
1557 		ret = -ENOMEM;
1558 		goto error;
1559 	}
1560 
1561 	return 0;
1562 
1563 error:
1564 	trace_probe_cleanup(tp);
1565 	return ret;
1566 }
1567 
1568 static struct trace_event_call *
1569 find_trace_event_call(const char *system, const char *event_name)
1570 {
1571 	struct trace_event_call *tp_event;
1572 	const char *name;
1573 
1574 	list_for_each_entry(tp_event, &ftrace_events, list) {
1575 		if (!tp_event->class->system ||
1576 		    strcmp(system, tp_event->class->system))
1577 			continue;
1578 		name = trace_event_name(tp_event);
1579 		if (!name || strcmp(event_name, name))
1580 			continue;
1581 		return tp_event;
1582 	}
1583 
1584 	return NULL;
1585 }
1586 
1587 int trace_probe_register_event_call(struct trace_probe *tp)
1588 {
1589 	struct trace_event_call *call = trace_probe_event_call(tp);
1590 	int ret;
1591 
1592 	lockdep_assert_held(&event_mutex);
1593 
1594 	if (find_trace_event_call(trace_probe_group_name(tp),
1595 				  trace_probe_name(tp)))
1596 		return -EEXIST;
1597 
1598 	ret = register_trace_event(&call->event);
1599 	if (!ret)
1600 		return -ENODEV;
1601 
1602 	ret = trace_add_event_call(call);
1603 	if (ret)
1604 		unregister_trace_event(&call->event);
1605 
1606 	return ret;
1607 }
1608 
1609 int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
1610 {
1611 	struct event_file_link *link;
1612 
1613 	link = kmalloc(sizeof(*link), GFP_KERNEL);
1614 	if (!link)
1615 		return -ENOMEM;
1616 
1617 	link->file = file;
1618 	INIT_LIST_HEAD(&link->list);
1619 	list_add_tail_rcu(&link->list, &tp->event->files);
1620 	trace_probe_set_flag(tp, TP_FLAG_TRACE);
1621 	return 0;
1622 }
1623 
1624 struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
1625 						  struct trace_event_file *file)
1626 {
1627 	struct event_file_link *link;
1628 
1629 	trace_probe_for_each_link(link, tp) {
1630 		if (link->file == file)
1631 			return link;
1632 	}
1633 
1634 	return NULL;
1635 }
1636 
1637 int trace_probe_remove_file(struct trace_probe *tp,
1638 			    struct trace_event_file *file)
1639 {
1640 	struct event_file_link *link;
1641 
1642 	link = trace_probe_get_file_link(tp, file);
1643 	if (!link)
1644 		return -ENOENT;
1645 
1646 	list_del_rcu(&link->list);
1647 	kvfree_rcu_mightsleep(link);
1648 
1649 	if (list_empty(&tp->event->files))
1650 		trace_probe_clear_flag(tp, TP_FLAG_TRACE);
1651 
1652 	return 0;
1653 }
1654 
1655 /*
1656  * Return the smallest index of different type argument (start from 1).
1657  * If all argument types and name are same, return 0.
1658  */
1659 int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b)
1660 {
1661 	int i;
1662 
1663 	/* In case of more arguments */
1664 	if (a->nr_args < b->nr_args)
1665 		return a->nr_args + 1;
1666 	if (a->nr_args > b->nr_args)
1667 		return b->nr_args + 1;
1668 
1669 	for (i = 0; i < a->nr_args; i++) {
1670 		if ((b->nr_args <= i) ||
1671 		    ((a->args[i].type != b->args[i].type) ||
1672 		     (a->args[i].count != b->args[i].count) ||
1673 		     strcmp(a->args[i].name, b->args[i].name)))
1674 			return i + 1;
1675 	}
1676 
1677 	return 0;
1678 }
1679 
1680 bool trace_probe_match_command_args(struct trace_probe *tp,
1681 				    int argc, const char **argv)
1682 {
1683 	char buf[MAX_ARGSTR_LEN + 1];
1684 	int i;
1685 
1686 	if (tp->nr_args < argc)
1687 		return false;
1688 
1689 	for (i = 0; i < argc; i++) {
1690 		snprintf(buf, sizeof(buf), "%s=%s",
1691 			 tp->args[i].name, tp->args[i].comm);
1692 		if (strcmp(buf, argv[i]))
1693 			return false;
1694 	}
1695 	return true;
1696 }
1697 
1698 int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **))
1699 {
1700 	int argc = 0, ret = 0;
1701 	char **argv;
1702 
1703 	argv = argv_split(GFP_KERNEL, raw_command, &argc);
1704 	if (!argv)
1705 		return -ENOMEM;
1706 
1707 	if (argc)
1708 		ret = createfn(argc, (const char **)argv);
1709 
1710 	argv_free(argv);
1711 
1712 	return ret;
1713 }
1714 
1715 int trace_probe_print_args(struct trace_seq *s, struct probe_arg *args, int nr_args,
1716 		 u8 *data, void *field)
1717 {
1718 	void *p;
1719 	int i, j;
1720 
1721 	for (i = 0; i < nr_args; i++) {
1722 		struct probe_arg *a = args + i;
1723 
1724 		trace_seq_printf(s, " %s=", a->name);
1725 		if (likely(!a->count)) {
1726 			if (!a->type->print(s, data + a->offset, field))
1727 				return -ENOMEM;
1728 			continue;
1729 		}
1730 		trace_seq_putc(s, '{');
1731 		p = data + a->offset;
1732 		for (j = 0; j < a->count; j++) {
1733 			if (!a->type->print(s, p, field))
1734 				return -ENOMEM;
1735 			trace_seq_putc(s, j == a->count - 1 ? '}' : ',');
1736 			p += a->type->size;
1737 		}
1738 	}
1739 	return 0;
1740 }
1741