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