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