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