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