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