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