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