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 "trace_probe.h"
15 
16 #undef C
17 #define C(a, b)		b
18 
19 static const char *trace_probe_err_text[] = { ERRORS };
20 
21 static const char *reserved_field_names[] = {
22 	"common_type",
23 	"common_flags",
24 	"common_preempt_count",
25 	"common_pid",
26 	"common_tgid",
27 	FIELD_STRING_IP,
28 	FIELD_STRING_RETIP,
29 	FIELD_STRING_FUNC,
30 };
31 
32 /* Printing  in basic type function template */
33 #define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt)			\
34 int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, void *data, void *ent)\
35 {									\
36 	trace_seq_printf(s, fmt, *(type *)data);			\
37 	return !trace_seq_has_overflowed(s);				\
38 }									\
39 const char PRINT_TYPE_FMT_NAME(tname)[] = fmt;
40 
41 DEFINE_BASIC_PRINT_TYPE_FUNC(u8,  u8,  "%u")
42 DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u")
43 DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u")
44 DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu")
45 DEFINE_BASIC_PRINT_TYPE_FUNC(s8,  s8,  "%d")
46 DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d")
47 DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d")
48 DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld")
49 DEFINE_BASIC_PRINT_TYPE_FUNC(x8,  u8,  "0x%x")
50 DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x")
51 DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x")
52 DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx")
53 
PRINT_TYPE_FUNC_NAME(symbol)54 int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent)
55 {
56 	trace_seq_printf(s, "%pS", (void *)*(unsigned long *)data);
57 	return !trace_seq_has_overflowed(s);
58 }
59 const char PRINT_TYPE_FMT_NAME(symbol)[] = "%pS";
60 
61 /* Print type function for string type */
PRINT_TYPE_FUNC_NAME(string)62 int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, void *data, void *ent)
63 {
64 	int len = *(u32 *)data >> 16;
65 
66 	if (!len)
67 		trace_seq_puts(s, "(fault)");
68 	else
69 		trace_seq_printf(s, "\"%s\"",
70 				 (const char *)get_loc_data(data, ent));
71 	return !trace_seq_has_overflowed(s);
72 }
73 
74 const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
75 
76 /* Fetch type information table */
77 static const struct fetch_type probe_fetch_types[] = {
78 	/* Special types */
79 	__ASSIGN_FETCH_TYPE("string", string, string, sizeof(u32), 1,
80 			    "__data_loc char[]"),
81 	__ASSIGN_FETCH_TYPE("ustring", string, string, sizeof(u32), 1,
82 			    "__data_loc char[]"),
83 	/* Basic types */
84 	ASSIGN_FETCH_TYPE(u8,  u8,  0),
85 	ASSIGN_FETCH_TYPE(u16, u16, 0),
86 	ASSIGN_FETCH_TYPE(u32, u32, 0),
87 	ASSIGN_FETCH_TYPE(u64, u64, 0),
88 	ASSIGN_FETCH_TYPE(s8,  u8,  1),
89 	ASSIGN_FETCH_TYPE(s16, u16, 1),
90 	ASSIGN_FETCH_TYPE(s32, u32, 1),
91 	ASSIGN_FETCH_TYPE(s64, u64, 1),
92 	ASSIGN_FETCH_TYPE_ALIAS(x8,  u8,  u8,  0),
93 	ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
94 	ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
95 	ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
96 	ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0),
97 
98 	ASSIGN_FETCH_TYPE_END
99 };
100 
find_fetch_type(const char * type)101 static const struct fetch_type *find_fetch_type(const char *type)
102 {
103 	int i;
104 
105 	if (!type)
106 		type = DEFAULT_FETCH_TYPE_STR;
107 
108 	/* Special case: bitfield */
109 	if (*type == 'b') {
110 		unsigned long bs;
111 
112 		type = strchr(type, '/');
113 		if (!type)
114 			goto fail;
115 
116 		type++;
117 		if (kstrtoul(type, 0, &bs))
118 			goto fail;
119 
120 		switch (bs) {
121 		case 8:
122 			return find_fetch_type("u8");
123 		case 16:
124 			return find_fetch_type("u16");
125 		case 32:
126 			return find_fetch_type("u32");
127 		case 64:
128 			return find_fetch_type("u64");
129 		default:
130 			goto fail;
131 		}
132 	}
133 
134 	for (i = 0; probe_fetch_types[i].name; i++) {
135 		if (strcmp(type, probe_fetch_types[i].name) == 0)
136 			return &probe_fetch_types[i];
137 	}
138 
139 fail:
140 	return NULL;
141 }
142 
143 static struct trace_probe_log trace_probe_log;
144 
trace_probe_log_init(const char * subsystem,int argc,const char ** argv)145 void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
146 {
147 	trace_probe_log.subsystem = subsystem;
148 	trace_probe_log.argc = argc;
149 	trace_probe_log.argv = argv;
150 	trace_probe_log.index = 0;
151 }
152 
trace_probe_log_clear(void)153 void trace_probe_log_clear(void)
154 {
155 	memset(&trace_probe_log, 0, sizeof(trace_probe_log));
156 }
157 
trace_probe_log_set_index(int index)158 void trace_probe_log_set_index(int index)
159 {
160 	trace_probe_log.index = index;
161 }
162 
__trace_probe_log_err(int offset,int err_type)163 void __trace_probe_log_err(int offset, int err_type)
164 {
165 	char *command, *p;
166 	int i, len = 0, pos = 0;
167 
168 	if (!trace_probe_log.argv)
169 		return;
170 
171 	/* Recalculate the length and allocate buffer */
172 	for (i = 0; i < trace_probe_log.argc; i++) {
173 		if (i == trace_probe_log.index)
174 			pos = len;
175 		len += strlen(trace_probe_log.argv[i]) + 1;
176 	}
177 	command = kzalloc(len, GFP_KERNEL);
178 	if (!command)
179 		return;
180 
181 	if (trace_probe_log.index >= trace_probe_log.argc) {
182 		/**
183 		 * Set the error position is next to the last arg + space.
184 		 * Note that len includes the terminal null and the cursor
185 		 * appears at pos + 1.
186 		 */
187 		pos = len;
188 		offset = 0;
189 	}
190 
191 	/* And make a command string from argv array */
192 	p = command;
193 	for (i = 0; i < trace_probe_log.argc; i++) {
194 		len = strlen(trace_probe_log.argv[i]);
195 		strcpy(p, trace_probe_log.argv[i]);
196 		p[len] = ' ';
197 		p += len + 1;
198 	}
199 	*(p - 1) = '\0';
200 
201 	tracing_log_err(NULL, trace_probe_log.subsystem, command,
202 			trace_probe_err_text, err_type, pos + offset);
203 
204 	kfree(command);
205 }
206 
207 /* Split symbol and offset. */
traceprobe_split_symbol_offset(char * symbol,long * offset)208 int traceprobe_split_symbol_offset(char *symbol, long *offset)
209 {
210 	char *tmp;
211 	int ret;
212 
213 	if (!offset)
214 		return -EINVAL;
215 
216 	tmp = strpbrk(symbol, "+-");
217 	if (tmp) {
218 		ret = kstrtol(tmp, 0, offset);
219 		if (ret)
220 			return ret;
221 		*tmp = '\0';
222 	} else
223 		*offset = 0;
224 
225 	return 0;
226 }
227 
228 /* @buf must has MAX_EVENT_NAME_LEN size */
traceprobe_parse_event_name(const char ** pevent,const char ** pgroup,char * buf,int offset)229 int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
230 				char *buf, int offset)
231 {
232 	const char *slash, *event = *pevent;
233 	int len;
234 
235 	slash = strchr(event, '/');
236 	if (!slash)
237 		slash = strchr(event, '.');
238 
239 	if (slash) {
240 		if (slash == event) {
241 			trace_probe_log_err(offset, NO_GROUP_NAME);
242 			return -EINVAL;
243 		}
244 		if (slash - event + 1 > MAX_EVENT_NAME_LEN) {
245 			trace_probe_log_err(offset, GROUP_TOO_LONG);
246 			return -EINVAL;
247 		}
248 		strlcpy(buf, event, slash - event + 1);
249 		if (!is_good_name(buf)) {
250 			trace_probe_log_err(offset, BAD_GROUP_NAME);
251 			return -EINVAL;
252 		}
253 		*pgroup = buf;
254 		*pevent = slash + 1;
255 		offset += slash - event + 1;
256 		event = *pevent;
257 	}
258 	len = strlen(event);
259 	if (len == 0) {
260 		trace_probe_log_err(offset, NO_EVENT_NAME);
261 		return -EINVAL;
262 	} else if (len > MAX_EVENT_NAME_LEN) {
263 		trace_probe_log_err(offset, EVENT_TOO_LONG);
264 		return -EINVAL;
265 	}
266 	if (!is_good_name(event)) {
267 		trace_probe_log_err(offset, BAD_EVENT_NAME);
268 		return -EINVAL;
269 	}
270 	return 0;
271 }
272 
273 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
274 
parse_probe_vars(char * arg,const struct fetch_type * t,struct fetch_insn * code,unsigned int flags,int offs)275 static int parse_probe_vars(char *arg, const struct fetch_type *t,
276 			struct fetch_insn *code, unsigned int flags, int offs)
277 {
278 	unsigned long param;
279 	int ret = 0;
280 	int len;
281 
282 	if (flags & TPARG_FL_TPOINT) {
283 		if (code->data)
284 			return -EFAULT;
285 		code->data = kstrdup(arg, GFP_KERNEL);
286 		if (!code->data)
287 			return -ENOMEM;
288 		code->op = FETCH_OP_TP_ARG;
289 	} else if (strcmp(arg, "retval") == 0) {
290 		if (flags & TPARG_FL_RETURN) {
291 			code->op = FETCH_OP_RETVAL;
292 		} else {
293 			trace_probe_log_err(offs, RETVAL_ON_PROBE);
294 			ret = -EINVAL;
295 		}
296 	} else if ((len = str_has_prefix(arg, "stack"))) {
297 		if (arg[len] == '\0') {
298 			code->op = FETCH_OP_STACKP;
299 		} else if (isdigit(arg[len])) {
300 			ret = kstrtoul(arg + len, 10, &param);
301 			if (ret) {
302 				goto inval_var;
303 			} else if ((flags & TPARG_FL_KERNEL) &&
304 				    param > PARAM_MAX_STACK) {
305 				trace_probe_log_err(offs, BAD_STACK_NUM);
306 				ret = -EINVAL;
307 			} else {
308 				code->op = FETCH_OP_STACK;
309 				code->param = (unsigned int)param;
310 			}
311 		} else
312 			goto inval_var;
313 	} else if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
314 		code->op = FETCH_OP_COMM;
315 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
316 	} else if (((flags & TPARG_FL_MASK) ==
317 		    (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) &&
318 		   (len = str_has_prefix(arg, "arg"))) {
319 		ret = kstrtoul(arg + len, 10, &param);
320 		if (ret) {
321 			goto inval_var;
322 		} else if (!param || param > PARAM_MAX_STACK) {
323 			trace_probe_log_err(offs, BAD_ARG_NUM);
324 			return -EINVAL;
325 		}
326 		code->op = FETCH_OP_ARG;
327 		code->param = (unsigned int)param - 1;
328 #endif
329 	} else
330 		goto inval_var;
331 
332 	return ret;
333 
334 inval_var:
335 	trace_probe_log_err(offs, BAD_VAR);
336 	return -EINVAL;
337 }
338 
str_to_immediate(char * str,unsigned long * imm)339 static int str_to_immediate(char *str, unsigned long *imm)
340 {
341 	if (isdigit(str[0]))
342 		return kstrtoul(str, 0, imm);
343 	else if (str[0] == '-')
344 		return kstrtol(str, 0, (long *)imm);
345 	else if (str[0] == '+')
346 		return kstrtol(str + 1, 0, (long *)imm);
347 	return -EINVAL;
348 }
349 
__parse_imm_string(char * str,char ** pbuf,int offs)350 static int __parse_imm_string(char *str, char **pbuf, int offs)
351 {
352 	size_t len = strlen(str);
353 
354 	if (str[len - 1] != '"') {
355 		trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE);
356 		return -EINVAL;
357 	}
358 	*pbuf = kstrndup(str, len - 1, GFP_KERNEL);
359 	if (!*pbuf)
360 		return -ENOMEM;
361 	return 0;
362 }
363 
364 /* Recursive argument parser */
365 static int
parse_probe_arg(char * arg,const struct fetch_type * type,struct fetch_insn ** pcode,struct fetch_insn * end,unsigned int flags,int offs)366 parse_probe_arg(char *arg, const struct fetch_type *type,
367 		struct fetch_insn **pcode, struct fetch_insn *end,
368 		unsigned int flags, int offs)
369 {
370 	struct fetch_insn *code = *pcode;
371 	unsigned long param;
372 	int deref = FETCH_OP_DEREF;
373 	long offset = 0;
374 	char *tmp;
375 	int ret = 0;
376 
377 	switch (arg[0]) {
378 	case '$':
379 		ret = parse_probe_vars(arg + 1, type, code, flags, offs);
380 		break;
381 
382 	case '%':	/* named register */
383 		if (flags & TPARG_FL_TPOINT) {
384 			/* eprobes do not handle registers */
385 			trace_probe_log_err(offs, BAD_VAR);
386 			break;
387 		}
388 		ret = regs_query_register_offset(arg + 1);
389 		if (ret >= 0) {
390 			code->op = FETCH_OP_REG;
391 			code->param = (unsigned int)ret;
392 			ret = 0;
393 		} else
394 			trace_probe_log_err(offs, BAD_REG_NAME);
395 		break;
396 
397 	case '@':	/* memory, file-offset or symbol */
398 		if (isdigit(arg[1])) {
399 			ret = kstrtoul(arg + 1, 0, &param);
400 			if (ret) {
401 				trace_probe_log_err(offs, BAD_MEM_ADDR);
402 				break;
403 			}
404 			/* load address */
405 			code->op = FETCH_OP_IMM;
406 			code->immediate = param;
407 		} else if (arg[1] == '+') {
408 			/* kprobes don't support file offsets */
409 			if (flags & TPARG_FL_KERNEL) {
410 				trace_probe_log_err(offs, FILE_ON_KPROBE);
411 				return -EINVAL;
412 			}
413 			ret = kstrtol(arg + 2, 0, &offset);
414 			if (ret) {
415 				trace_probe_log_err(offs, BAD_FILE_OFFS);
416 				break;
417 			}
418 
419 			code->op = FETCH_OP_FOFFS;
420 			code->immediate = (unsigned long)offset;  // imm64?
421 		} else {
422 			/* uprobes don't support symbols */
423 			if (!(flags & TPARG_FL_KERNEL)) {
424 				trace_probe_log_err(offs, SYM_ON_UPROBE);
425 				return -EINVAL;
426 			}
427 			/* Preserve symbol for updating */
428 			code->op = FETCH_NOP_SYMBOL;
429 			code->data = kstrdup(arg + 1, GFP_KERNEL);
430 			if (!code->data)
431 				return -ENOMEM;
432 			if (++code == end) {
433 				trace_probe_log_err(offs, TOO_MANY_OPS);
434 				return -EINVAL;
435 			}
436 			code->op = FETCH_OP_IMM;
437 			code->immediate = 0;
438 		}
439 		/* These are fetching from memory */
440 		if (++code == end) {
441 			trace_probe_log_err(offs, TOO_MANY_OPS);
442 			return -EINVAL;
443 		}
444 		*pcode = code;
445 		code->op = FETCH_OP_DEREF;
446 		code->offset = offset;
447 		break;
448 
449 	case '+':	/* deref memory */
450 	case '-':
451 		if (arg[1] == 'u') {
452 			deref = FETCH_OP_UDEREF;
453 			arg[1] = arg[0];
454 			arg++;
455 		}
456 		if (arg[0] == '+')
457 			arg++;	/* Skip '+', because kstrtol() rejects it. */
458 		tmp = strchr(arg, '(');
459 		if (!tmp) {
460 			trace_probe_log_err(offs, DEREF_NEED_BRACE);
461 			return -EINVAL;
462 		}
463 		*tmp = '\0';
464 		ret = kstrtol(arg, 0, &offset);
465 		if (ret) {
466 			trace_probe_log_err(offs, BAD_DEREF_OFFS);
467 			break;
468 		}
469 		offs += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0);
470 		arg = tmp + 1;
471 		tmp = strrchr(arg, ')');
472 		if (!tmp) {
473 			trace_probe_log_err(offs + strlen(arg),
474 					    DEREF_OPEN_BRACE);
475 			return -EINVAL;
476 		} else {
477 			const struct fetch_type *t2 = find_fetch_type(NULL);
478 
479 			*tmp = '\0';
480 			ret = parse_probe_arg(arg, t2, &code, end, flags, offs);
481 			if (ret)
482 				break;
483 			if (code->op == FETCH_OP_COMM ||
484 			    code->op == FETCH_OP_DATA) {
485 				trace_probe_log_err(offs, COMM_CANT_DEREF);
486 				return -EINVAL;
487 			}
488 			if (++code == end) {
489 				trace_probe_log_err(offs, TOO_MANY_OPS);
490 				return -EINVAL;
491 			}
492 			*pcode = code;
493 
494 			code->op = deref;
495 			code->offset = offset;
496 		}
497 		break;
498 	case '\\':	/* Immediate value */
499 		if (arg[1] == '"') {	/* Immediate string */
500 			ret = __parse_imm_string(arg + 2, &tmp, offs + 2);
501 			if (ret)
502 				break;
503 			code->op = FETCH_OP_DATA;
504 			code->data = tmp;
505 		} else {
506 			ret = str_to_immediate(arg + 1, &code->immediate);
507 			if (ret)
508 				trace_probe_log_err(offs + 1, BAD_IMM);
509 			else
510 				code->op = FETCH_OP_IMM;
511 		}
512 		break;
513 	}
514 	if (!ret && code->op == FETCH_OP_NOP) {
515 		/* Parsed, but do not find fetch method */
516 		trace_probe_log_err(offs, BAD_FETCH_ARG);
517 		ret = -EINVAL;
518 	}
519 	return ret;
520 }
521 
522 #define BYTES_TO_BITS(nb)	((BITS_PER_LONG * (nb)) / sizeof(long))
523 
524 /* 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)525 static int __parse_bitfield_probe_arg(const char *bf,
526 				      const struct fetch_type *t,
527 				      struct fetch_insn **pcode)
528 {
529 	struct fetch_insn *code = *pcode;
530 	unsigned long bw, bo;
531 	char *tail;
532 
533 	if (*bf != 'b')
534 		return 0;
535 
536 	bw = simple_strtoul(bf + 1, &tail, 0);	/* Use simple one */
537 
538 	if (bw == 0 || *tail != '@')
539 		return -EINVAL;
540 
541 	bf = tail + 1;
542 	bo = simple_strtoul(bf, &tail, 0);
543 
544 	if (tail == bf || *tail != '/')
545 		return -EINVAL;
546 	code++;
547 	if (code->op != FETCH_OP_NOP)
548 		return -EINVAL;
549 	*pcode = code;
550 
551 	code->op = FETCH_OP_MOD_BF;
552 	code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
553 	code->rshift = BYTES_TO_BITS(t->size) - bw;
554 	code->basesize = t->size;
555 
556 	return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
557 }
558 
559 /* String length checking wrapper */
traceprobe_parse_probe_arg_body(const char * argv,ssize_t * size,struct probe_arg * parg,unsigned int flags,int offset)560 static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size,
561 		struct probe_arg *parg, unsigned int flags, int offset)
562 {
563 	struct fetch_insn *code, *scode, *tmp = NULL;
564 	char *t, *t2, *t3;
565 	char *arg;
566 	int ret, len;
567 
568 	arg = kstrdup(argv, GFP_KERNEL);
569 	if (!arg)
570 		return -ENOMEM;
571 
572 	ret = -EINVAL;
573 	len = strlen(arg);
574 	if (len > MAX_ARGSTR_LEN) {
575 		trace_probe_log_err(offset, ARG_TOO_LONG);
576 		goto out;
577 	} else if (len == 0) {
578 		trace_probe_log_err(offset, NO_ARG_BODY);
579 		goto out;
580 	}
581 
582 	ret = -ENOMEM;
583 	parg->comm = kstrdup(arg, GFP_KERNEL);
584 	if (!parg->comm)
585 		goto out;
586 
587 	ret = -EINVAL;
588 	t = strchr(arg, ':');
589 	if (t) {
590 		*t = '\0';
591 		t2 = strchr(++t, '[');
592 		if (t2) {
593 			*t2++ = '\0';
594 			t3 = strchr(t2, ']');
595 			if (!t3) {
596 				offset += t2 + strlen(t2) - arg;
597 				trace_probe_log_err(offset,
598 						    ARRAY_NO_CLOSE);
599 				goto out;
600 			} else if (t3[1] != '\0') {
601 				trace_probe_log_err(offset + t3 + 1 - arg,
602 						    BAD_ARRAY_SUFFIX);
603 				goto out;
604 			}
605 			*t3 = '\0';
606 			if (kstrtouint(t2, 0, &parg->count) || !parg->count) {
607 				trace_probe_log_err(offset + t2 - arg,
608 						    BAD_ARRAY_NUM);
609 				goto out;
610 			}
611 			if (parg->count > MAX_ARRAY_LEN) {
612 				trace_probe_log_err(offset + t2 - arg,
613 						    ARRAY_TOO_BIG);
614 				goto out;
615 			}
616 		}
617 	}
618 
619 	/*
620 	 * Since $comm and immediate string can not be dereferenced,
621 	 * we can find those by strcmp. But ignore for eprobes.
622 	 */
623 	if (!(flags & TPARG_FL_TPOINT) &&
624 	    (strcmp(arg, "$comm") == 0 || strcmp(arg, "$COMM") == 0 ||
625 	     strncmp(arg, "\\\"", 2) == 0)) {
626 		/* The type of $comm must be "string", and not an array. */
627 		if (parg->count || (t && strcmp(t, "string")))
628 			goto out;
629 		parg->type = find_fetch_type("string");
630 	} else
631 		parg->type = find_fetch_type(t);
632 	if (!parg->type) {
633 		trace_probe_log_err(offset + (t ? (t - arg) : 0), BAD_TYPE);
634 		goto out;
635 	}
636 	parg->offset = *size;
637 	*size += parg->type->size * (parg->count ?: 1);
638 
639 	ret = -ENOMEM;
640 	if (parg->count) {
641 		len = strlen(parg->type->fmttype) + 6;
642 		parg->fmt = kmalloc(len, GFP_KERNEL);
643 		if (!parg->fmt)
644 			goto out;
645 		snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
646 			 parg->count);
647 	}
648 
649 	code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL);
650 	if (!code)
651 		goto out;
652 	code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
653 
654 	ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
655 			      flags, offset);
656 	if (ret)
657 		goto fail;
658 
659 	ret = -EINVAL;
660 	/* Store operation */
661 	if (!strcmp(parg->type->name, "string") ||
662 	    !strcmp(parg->type->name, "ustring")) {
663 		if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF &&
664 		    code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM &&
665 		    code->op != FETCH_OP_DATA && code->op != FETCH_OP_TP_ARG) {
666 			trace_probe_log_err(offset + (t ? (t - arg) : 0),
667 					    BAD_STRING);
668 			goto fail;
669 		}
670 		if ((code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM ||
671 		     code->op == FETCH_OP_DATA) || code->op == FETCH_OP_TP_ARG ||
672 		     parg->count) {
673 			/*
674 			 * IMM, DATA and COMM is pointing actual address, those
675 			 * must be kept, and if parg->count != 0, this is an
676 			 * array of string pointers instead of string address
677 			 * itself.
678 			 */
679 			code++;
680 			if (code->op != FETCH_OP_NOP) {
681 				trace_probe_log_err(offset, TOO_MANY_OPS);
682 				goto fail;
683 			}
684 		}
685 		/* If op == DEREF, replace it with STRING */
686 		if (!strcmp(parg->type->name, "ustring") ||
687 		    code->op == FETCH_OP_UDEREF)
688 			code->op = FETCH_OP_ST_USTRING;
689 		else
690 			code->op = FETCH_OP_ST_STRING;
691 		code->size = parg->type->size;
692 		parg->dynamic = true;
693 	} else if (code->op == FETCH_OP_DEREF) {
694 		code->op = FETCH_OP_ST_MEM;
695 		code->size = parg->type->size;
696 	} else if (code->op == FETCH_OP_UDEREF) {
697 		code->op = FETCH_OP_ST_UMEM;
698 		code->size = parg->type->size;
699 	} else {
700 		code++;
701 		if (code->op != FETCH_OP_NOP) {
702 			trace_probe_log_err(offset, TOO_MANY_OPS);
703 			goto fail;
704 		}
705 		code->op = FETCH_OP_ST_RAW;
706 		code->size = parg->type->size;
707 	}
708 	scode = code;
709 	/* Modify operation */
710 	if (t != NULL) {
711 		ret = __parse_bitfield_probe_arg(t, parg->type, &code);
712 		if (ret) {
713 			trace_probe_log_err(offset + t - arg, BAD_BITFIELD);
714 			goto fail;
715 		}
716 	}
717 	ret = -EINVAL;
718 	/* Loop(Array) operation */
719 	if (parg->count) {
720 		if (scode->op != FETCH_OP_ST_MEM &&
721 		    scode->op != FETCH_OP_ST_STRING &&
722 		    scode->op != FETCH_OP_ST_USTRING) {
723 			trace_probe_log_err(offset + (t ? (t - arg) : 0),
724 					    BAD_STRING);
725 			goto fail;
726 		}
727 		code++;
728 		if (code->op != FETCH_OP_NOP) {
729 			trace_probe_log_err(offset, TOO_MANY_OPS);
730 			goto fail;
731 		}
732 		code->op = FETCH_OP_LP_ARRAY;
733 		code->param = parg->count;
734 	}
735 	code++;
736 	code->op = FETCH_OP_END;
737 
738 	ret = 0;
739 	/* Shrink down the code buffer */
740 	parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL);
741 	if (!parg->code)
742 		ret = -ENOMEM;
743 	else
744 		memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
745 
746 fail:
747 	if (ret) {
748 		for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
749 			if (code->op == FETCH_NOP_SYMBOL ||
750 			    code->op == FETCH_OP_DATA)
751 				kfree(code->data);
752 	}
753 	kfree(tmp);
754 out:
755 	kfree(arg);
756 
757 	return ret;
758 }
759 
760 /* 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)761 static int traceprobe_conflict_field_name(const char *name,
762 					  struct probe_arg *args, int narg)
763 {
764 	int i;
765 
766 	for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
767 		if (strcmp(reserved_field_names[i], name) == 0)
768 			return 1;
769 
770 	for (i = 0; i < narg; i++)
771 		if (strcmp(args[i].name, name) == 0)
772 			return 1;
773 
774 	return 0;
775 }
776 
traceprobe_parse_probe_arg(struct trace_probe * tp,int i,const char * arg,unsigned int flags)777 int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg,
778 				unsigned int flags)
779 {
780 	struct probe_arg *parg = &tp->args[i];
781 	const char *body;
782 
783 	/* Increment count for freeing args in error case */
784 	tp->nr_args++;
785 
786 	body = strchr(arg, '=');
787 	if (body) {
788 		if (body - arg > MAX_ARG_NAME_LEN) {
789 			trace_probe_log_err(0, ARG_NAME_TOO_LONG);
790 			return -EINVAL;
791 		} else if (body == arg) {
792 			trace_probe_log_err(0, NO_ARG_NAME);
793 			return -EINVAL;
794 		}
795 		parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
796 		body++;
797 	} else {
798 		/* If argument name is omitted, set "argN" */
799 		parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1);
800 		body = arg;
801 	}
802 	if (!parg->name)
803 		return -ENOMEM;
804 
805 	if (!is_good_name(parg->name)) {
806 		trace_probe_log_err(0, BAD_ARG_NAME);
807 		return -EINVAL;
808 	}
809 	if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
810 		trace_probe_log_err(0, USED_ARG_NAME);
811 		return -EINVAL;
812 	}
813 	/* Parse fetch argument */
814 	return traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags,
815 					       body - arg);
816 }
817 
traceprobe_free_probe_arg(struct probe_arg * arg)818 void traceprobe_free_probe_arg(struct probe_arg *arg)
819 {
820 	struct fetch_insn *code = arg->code;
821 
822 	while (code && code->op != FETCH_OP_END) {
823 		if (code->op == FETCH_NOP_SYMBOL ||
824 		    code->op == FETCH_OP_DATA)
825 			kfree(code->data);
826 		code++;
827 	}
828 	kfree(arg->code);
829 	kfree(arg->name);
830 	kfree(arg->comm);
831 	kfree(arg->fmt);
832 }
833 
traceprobe_update_arg(struct probe_arg * arg)834 int traceprobe_update_arg(struct probe_arg *arg)
835 {
836 	struct fetch_insn *code = arg->code;
837 	long offset;
838 	char *tmp;
839 	char c;
840 	int ret = 0;
841 
842 	while (code && code->op != FETCH_OP_END) {
843 		if (code->op == FETCH_NOP_SYMBOL) {
844 			if (code[1].op != FETCH_OP_IMM)
845 				return -EINVAL;
846 
847 			tmp = strpbrk(code->data, "+-");
848 			if (tmp)
849 				c = *tmp;
850 			ret = traceprobe_split_symbol_offset(code->data,
851 							     &offset);
852 			if (ret)
853 				return ret;
854 
855 			code[1].immediate =
856 				(unsigned long)kallsyms_lookup_name(code->data);
857 			if (tmp)
858 				*tmp = c;
859 			if (!code[1].immediate)
860 				return -ENOENT;
861 			code[1].immediate += offset;
862 		}
863 		code++;
864 	}
865 	return 0;
866 }
867 
868 /* When len=0, we just calculate the needed length */
869 #define LEN_OR_ZERO (len ? len - pos : 0)
__set_print_fmt(struct trace_probe * tp,char * buf,int len,enum probe_print_type ptype)870 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
871 			   enum probe_print_type ptype)
872 {
873 	struct probe_arg *parg;
874 	int i, j;
875 	int pos = 0;
876 	const char *fmt, *arg;
877 
878 	switch (ptype) {
879 	case PROBE_PRINT_NORMAL:
880 		fmt = "(%lx)";
881 		arg = ", REC->" FIELD_STRING_IP;
882 		break;
883 	case PROBE_PRINT_RETURN:
884 		fmt = "(%lx <- %lx)";
885 		arg = ", REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
886 		break;
887 	case PROBE_PRINT_EVENT:
888 		fmt = "";
889 		arg = "";
890 		break;
891 	default:
892 		WARN_ON_ONCE(1);
893 		return 0;
894 	}
895 
896 	pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
897 
898 	for (i = 0; i < tp->nr_args; i++) {
899 		parg = tp->args + i;
900 		pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
901 		if (parg->count) {
902 			pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
903 					parg->type->fmt);
904 			for (j = 1; j < parg->count; j++)
905 				pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
906 						parg->type->fmt);
907 			pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
908 		} else
909 			pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
910 					parg->type->fmt);
911 	}
912 
913 	pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", arg);
914 
915 	for (i = 0; i < tp->nr_args; i++) {
916 		parg = tp->args + i;
917 		if (parg->count) {
918 			if ((strcmp(parg->type->name, "string") == 0) ||
919 			    (strcmp(parg->type->name, "ustring") == 0))
920 				fmt = ", __get_str(%s[%d])";
921 			else
922 				fmt = ", REC->%s[%d]";
923 			for (j = 0; j < parg->count; j++)
924 				pos += snprintf(buf + pos, LEN_OR_ZERO,
925 						fmt, parg->name, j);
926 		} else {
927 			if ((strcmp(parg->type->name, "string") == 0) ||
928 			    (strcmp(parg->type->name, "ustring") == 0))
929 				fmt = ", __get_str(%s)";
930 			else
931 				fmt = ", REC->%s";
932 			pos += snprintf(buf + pos, LEN_OR_ZERO,
933 					fmt, parg->name);
934 		}
935 	}
936 
937 	/* return the length of print_fmt */
938 	return pos;
939 }
940 #undef LEN_OR_ZERO
941 
traceprobe_set_print_fmt(struct trace_probe * tp,enum probe_print_type ptype)942 int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype)
943 {
944 	struct trace_event_call *call = trace_probe_event_call(tp);
945 	int len;
946 	char *print_fmt;
947 
948 	/* First: called with 0 length to calculate the needed length */
949 	len = __set_print_fmt(tp, NULL, 0, ptype);
950 	print_fmt = kmalloc(len + 1, GFP_KERNEL);
951 	if (!print_fmt)
952 		return -ENOMEM;
953 
954 	/* Second: actually write the @print_fmt */
955 	__set_print_fmt(tp, print_fmt, len + 1, ptype);
956 	call->print_fmt = print_fmt;
957 
958 	return 0;
959 }
960 
traceprobe_define_arg_fields(struct trace_event_call * event_call,size_t offset,struct trace_probe * tp)961 int traceprobe_define_arg_fields(struct trace_event_call *event_call,
962 				 size_t offset, struct trace_probe *tp)
963 {
964 	int ret, i;
965 
966 	/* Set argument names as fields */
967 	for (i = 0; i < tp->nr_args; i++) {
968 		struct probe_arg *parg = &tp->args[i];
969 		const char *fmt = parg->type->fmttype;
970 		int size = parg->type->size;
971 
972 		if (parg->fmt)
973 			fmt = parg->fmt;
974 		if (parg->count)
975 			size *= parg->count;
976 		ret = trace_define_field(event_call, fmt, parg->name,
977 					 offset + parg->offset, size,
978 					 parg->type->is_signed,
979 					 FILTER_OTHER);
980 		if (ret)
981 			return ret;
982 	}
983 	return 0;
984 }
985 
trace_probe_event_free(struct trace_probe_event * tpe)986 static void trace_probe_event_free(struct trace_probe_event *tpe)
987 {
988 	kfree(tpe->class.system);
989 	kfree(tpe->call.name);
990 	kfree(tpe->call.print_fmt);
991 	kfree(tpe);
992 }
993 
trace_probe_append(struct trace_probe * tp,struct trace_probe * to)994 int trace_probe_append(struct trace_probe *tp, struct trace_probe *to)
995 {
996 	if (trace_probe_has_sibling(tp))
997 		return -EBUSY;
998 
999 	list_del_init(&tp->list);
1000 	trace_probe_event_free(tp->event);
1001 
1002 	tp->event = to->event;
1003 	list_add_tail(&tp->list, trace_probe_probe_list(to));
1004 
1005 	return 0;
1006 }
1007 
trace_probe_unlink(struct trace_probe * tp)1008 void trace_probe_unlink(struct trace_probe *tp)
1009 {
1010 	list_del_init(&tp->list);
1011 	if (list_empty(trace_probe_probe_list(tp)))
1012 		trace_probe_event_free(tp->event);
1013 	tp->event = NULL;
1014 }
1015 
trace_probe_cleanup(struct trace_probe * tp)1016 void trace_probe_cleanup(struct trace_probe *tp)
1017 {
1018 	int i;
1019 
1020 	for (i = 0; i < tp->nr_args; i++)
1021 		traceprobe_free_probe_arg(&tp->args[i]);
1022 
1023 	if (tp->event)
1024 		trace_probe_unlink(tp);
1025 }
1026 
trace_probe_init(struct trace_probe * tp,const char * event,const char * group,bool alloc_filter)1027 int trace_probe_init(struct trace_probe *tp, const char *event,
1028 		     const char *group, bool alloc_filter)
1029 {
1030 	struct trace_event_call *call;
1031 	size_t size = sizeof(struct trace_probe_event);
1032 	int ret = 0;
1033 
1034 	if (!event || !group)
1035 		return -EINVAL;
1036 
1037 	if (alloc_filter)
1038 		size += sizeof(struct trace_uprobe_filter);
1039 
1040 	tp->event = kzalloc(size, GFP_KERNEL);
1041 	if (!tp->event)
1042 		return -ENOMEM;
1043 
1044 	INIT_LIST_HEAD(&tp->event->files);
1045 	INIT_LIST_HEAD(&tp->event->class.fields);
1046 	INIT_LIST_HEAD(&tp->event->probes);
1047 	INIT_LIST_HEAD(&tp->list);
1048 	list_add(&tp->list, &tp->event->probes);
1049 
1050 	call = trace_probe_event_call(tp);
1051 	call->class = &tp->event->class;
1052 	call->name = kstrdup(event, GFP_KERNEL);
1053 	if (!call->name) {
1054 		ret = -ENOMEM;
1055 		goto error;
1056 	}
1057 
1058 	tp->event->class.system = kstrdup(group, GFP_KERNEL);
1059 	if (!tp->event->class.system) {
1060 		ret = -ENOMEM;
1061 		goto error;
1062 	}
1063 
1064 	return 0;
1065 
1066 error:
1067 	trace_probe_cleanup(tp);
1068 	return ret;
1069 }
1070 
1071 static struct trace_event_call *
find_trace_event_call(const char * system,const char * event_name)1072 find_trace_event_call(const char *system, const char *event_name)
1073 {
1074 	struct trace_event_call *tp_event;
1075 	const char *name;
1076 
1077 	list_for_each_entry(tp_event, &ftrace_events, list) {
1078 		if (!tp_event->class->system ||
1079 		    strcmp(system, tp_event->class->system))
1080 			continue;
1081 		name = trace_event_name(tp_event);
1082 		if (!name || strcmp(event_name, name))
1083 			continue;
1084 		return tp_event;
1085 	}
1086 
1087 	return NULL;
1088 }
1089 
trace_probe_register_event_call(struct trace_probe * tp)1090 int trace_probe_register_event_call(struct trace_probe *tp)
1091 {
1092 	struct trace_event_call *call = trace_probe_event_call(tp);
1093 	int ret;
1094 
1095 	lockdep_assert_held(&event_mutex);
1096 
1097 	if (find_trace_event_call(trace_probe_group_name(tp),
1098 				  trace_probe_name(tp)))
1099 		return -EEXIST;
1100 
1101 	ret = register_trace_event(&call->event);
1102 	if (!ret)
1103 		return -ENODEV;
1104 
1105 	ret = trace_add_event_call(call);
1106 	if (ret)
1107 		unregister_trace_event(&call->event);
1108 
1109 	return ret;
1110 }
1111 
trace_probe_add_file(struct trace_probe * tp,struct trace_event_file * file)1112 int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
1113 {
1114 	struct event_file_link *link;
1115 
1116 	link = kmalloc(sizeof(*link), GFP_KERNEL);
1117 	if (!link)
1118 		return -ENOMEM;
1119 
1120 	link->file = file;
1121 	INIT_LIST_HEAD(&link->list);
1122 	list_add_tail_rcu(&link->list, &tp->event->files);
1123 	trace_probe_set_flag(tp, TP_FLAG_TRACE);
1124 	return 0;
1125 }
1126 
trace_probe_get_file_link(struct trace_probe * tp,struct trace_event_file * file)1127 struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
1128 						  struct trace_event_file *file)
1129 {
1130 	struct event_file_link *link;
1131 
1132 	trace_probe_for_each_link(link, tp) {
1133 		if (link->file == file)
1134 			return link;
1135 	}
1136 
1137 	return NULL;
1138 }
1139 
trace_probe_remove_file(struct trace_probe * tp,struct trace_event_file * file)1140 int trace_probe_remove_file(struct trace_probe *tp,
1141 			    struct trace_event_file *file)
1142 {
1143 	struct event_file_link *link;
1144 
1145 	link = trace_probe_get_file_link(tp, file);
1146 	if (!link)
1147 		return -ENOENT;
1148 
1149 	list_del_rcu(&link->list);
1150 	kvfree_rcu(link);
1151 
1152 	if (list_empty(&tp->event->files))
1153 		trace_probe_clear_flag(tp, TP_FLAG_TRACE);
1154 
1155 	return 0;
1156 }
1157 
1158 /*
1159  * Return the smallest index of different type argument (start from 1).
1160  * If all argument types and name are same, return 0.
1161  */
trace_probe_compare_arg_type(struct trace_probe * a,struct trace_probe * b)1162 int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b)
1163 {
1164 	int i;
1165 
1166 	/* In case of more arguments */
1167 	if (a->nr_args < b->nr_args)
1168 		return a->nr_args + 1;
1169 	if (a->nr_args > b->nr_args)
1170 		return b->nr_args + 1;
1171 
1172 	for (i = 0; i < a->nr_args; i++) {
1173 		if ((b->nr_args <= i) ||
1174 		    ((a->args[i].type != b->args[i].type) ||
1175 		     (a->args[i].count != b->args[i].count) ||
1176 		     strcmp(a->args[i].name, b->args[i].name)))
1177 			return i + 1;
1178 	}
1179 
1180 	return 0;
1181 }
1182 
trace_probe_match_command_args(struct trace_probe * tp,int argc,const char ** argv)1183 bool trace_probe_match_command_args(struct trace_probe *tp,
1184 				    int argc, const char **argv)
1185 {
1186 	char buf[MAX_ARGSTR_LEN + 1];
1187 	int i;
1188 
1189 	if (tp->nr_args < argc)
1190 		return false;
1191 
1192 	for (i = 0; i < argc; i++) {
1193 		snprintf(buf, sizeof(buf), "%s=%s",
1194 			 tp->args[i].name, tp->args[i].comm);
1195 		if (strcmp(buf, argv[i]))
1196 			return false;
1197 	}
1198 	return true;
1199 }
1200 
trace_probe_create(const char * raw_command,int (* createfn)(int,const char **))1201 int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **))
1202 {
1203 	int argc = 0, ret = 0;
1204 	char **argv;
1205 
1206 	argv = argv_split(GFP_KERNEL, raw_command, &argc);
1207 	if (!argv)
1208 		return -ENOMEM;
1209 
1210 	if (argc)
1211 		ret = createfn(argc, (const char **)argv);
1212 
1213 	argv_free(argv);
1214 
1215 	return ret;
1216 }
1217