xref: /openbmc/linux/kernel/trace/trace_probe.c (revision b830f94f)
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 	/* Recalcurate 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 	/* And make a command string from argv array */
182 	p = command;
183 	for (i = 0; i < trace_probe_log.argc; i++) {
184 		len = strlen(trace_probe_log.argv[i]);
185 		strcpy(p, trace_probe_log.argv[i]);
186 		p[len] = ' ';
187 		p += len + 1;
188 	}
189 	*(p - 1) = '\0';
190 
191 	tracing_log_err(NULL, trace_probe_log.subsystem, command,
192 			trace_probe_err_text, err_type, pos + offset);
193 
194 	kfree(command);
195 }
196 
197 /* Split symbol and offset. */
198 int traceprobe_split_symbol_offset(char *symbol, long *offset)
199 {
200 	char *tmp;
201 	int ret;
202 
203 	if (!offset)
204 		return -EINVAL;
205 
206 	tmp = strpbrk(symbol, "+-");
207 	if (tmp) {
208 		ret = kstrtol(tmp, 0, offset);
209 		if (ret)
210 			return ret;
211 		*tmp = '\0';
212 	} else
213 		*offset = 0;
214 
215 	return 0;
216 }
217 
218 /* @buf must has MAX_EVENT_NAME_LEN size */
219 int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
220 				char *buf, int offset)
221 {
222 	const char *slash, *event = *pevent;
223 	int len;
224 
225 	slash = strchr(event, '/');
226 	if (slash) {
227 		if (slash == event) {
228 			trace_probe_log_err(offset, NO_GROUP_NAME);
229 			return -EINVAL;
230 		}
231 		if (slash - event + 1 > MAX_EVENT_NAME_LEN) {
232 			trace_probe_log_err(offset, GROUP_TOO_LONG);
233 			return -EINVAL;
234 		}
235 		strlcpy(buf, event, slash - event + 1);
236 		if (!is_good_name(buf)) {
237 			trace_probe_log_err(offset, BAD_GROUP_NAME);
238 			return -EINVAL;
239 		}
240 		*pgroup = buf;
241 		*pevent = slash + 1;
242 		offset += slash - event + 1;
243 		event = *pevent;
244 	}
245 	len = strlen(event);
246 	if (len == 0) {
247 		trace_probe_log_err(offset, NO_EVENT_NAME);
248 		return -EINVAL;
249 	} else if (len > MAX_EVENT_NAME_LEN) {
250 		trace_probe_log_err(offset, EVENT_TOO_LONG);
251 		return -EINVAL;
252 	}
253 	if (!is_good_name(event)) {
254 		trace_probe_log_err(offset, BAD_EVENT_NAME);
255 		return -EINVAL;
256 	}
257 	return 0;
258 }
259 
260 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
261 
262 static int parse_probe_vars(char *arg, const struct fetch_type *t,
263 			struct fetch_insn *code, unsigned int flags, int offs)
264 {
265 	unsigned long param;
266 	int ret = 0;
267 	int len;
268 
269 	if (strcmp(arg, "retval") == 0) {
270 		if (flags & TPARG_FL_RETURN) {
271 			code->op = FETCH_OP_RETVAL;
272 		} else {
273 			trace_probe_log_err(offs, RETVAL_ON_PROBE);
274 			ret = -EINVAL;
275 		}
276 	} else if ((len = str_has_prefix(arg, "stack"))) {
277 		if (arg[len] == '\0') {
278 			code->op = FETCH_OP_STACKP;
279 		} else if (isdigit(arg[len])) {
280 			ret = kstrtoul(arg + len, 10, &param);
281 			if (ret) {
282 				goto inval_var;
283 			} else if ((flags & TPARG_FL_KERNEL) &&
284 				    param > PARAM_MAX_STACK) {
285 				trace_probe_log_err(offs, BAD_STACK_NUM);
286 				ret = -EINVAL;
287 			} else {
288 				code->op = FETCH_OP_STACK;
289 				code->param = (unsigned int)param;
290 			}
291 		} else
292 			goto inval_var;
293 	} else if (strcmp(arg, "comm") == 0) {
294 		code->op = FETCH_OP_COMM;
295 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
296 	} else if (((flags & TPARG_FL_MASK) ==
297 		    (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) &&
298 		   (len = str_has_prefix(arg, "arg"))) {
299 		ret = kstrtoul(arg + len, 10, &param);
300 		if (ret) {
301 			goto inval_var;
302 		} else if (!param || param > PARAM_MAX_STACK) {
303 			trace_probe_log_err(offs, BAD_ARG_NUM);
304 			return -EINVAL;
305 		}
306 		code->op = FETCH_OP_ARG;
307 		code->param = (unsigned int)param - 1;
308 #endif
309 	} else
310 		goto inval_var;
311 
312 	return ret;
313 
314 inval_var:
315 	trace_probe_log_err(offs, BAD_VAR);
316 	return -EINVAL;
317 }
318 
319 /* Recursive argument parser */
320 static int
321 parse_probe_arg(char *arg, const struct fetch_type *type,
322 		struct fetch_insn **pcode, struct fetch_insn *end,
323 		unsigned int flags, int offs)
324 {
325 	struct fetch_insn *code = *pcode;
326 	unsigned long param;
327 	int deref = FETCH_OP_DEREF;
328 	long offset = 0;
329 	char *tmp;
330 	int ret = 0;
331 
332 	switch (arg[0]) {
333 	case '$':
334 		ret = parse_probe_vars(arg + 1, type, code, flags, offs);
335 		break;
336 
337 	case '%':	/* named register */
338 		ret = regs_query_register_offset(arg + 1);
339 		if (ret >= 0) {
340 			code->op = FETCH_OP_REG;
341 			code->param = (unsigned int)ret;
342 			ret = 0;
343 		} else
344 			trace_probe_log_err(offs, BAD_REG_NAME);
345 		break;
346 
347 	case '@':	/* memory, file-offset or symbol */
348 		if (isdigit(arg[1])) {
349 			ret = kstrtoul(arg + 1, 0, &param);
350 			if (ret) {
351 				trace_probe_log_err(offs, BAD_MEM_ADDR);
352 				break;
353 			}
354 			/* load address */
355 			code->op = FETCH_OP_IMM;
356 			code->immediate = param;
357 		} else if (arg[1] == '+') {
358 			/* kprobes don't support file offsets */
359 			if (flags & TPARG_FL_KERNEL) {
360 				trace_probe_log_err(offs, FILE_ON_KPROBE);
361 				return -EINVAL;
362 			}
363 			ret = kstrtol(arg + 2, 0, &offset);
364 			if (ret) {
365 				trace_probe_log_err(offs, BAD_FILE_OFFS);
366 				break;
367 			}
368 
369 			code->op = FETCH_OP_FOFFS;
370 			code->immediate = (unsigned long)offset;  // imm64?
371 		} else {
372 			/* uprobes don't support symbols */
373 			if (!(flags & TPARG_FL_KERNEL)) {
374 				trace_probe_log_err(offs, SYM_ON_UPROBE);
375 				return -EINVAL;
376 			}
377 			/* Preserve symbol for updating */
378 			code->op = FETCH_NOP_SYMBOL;
379 			code->data = kstrdup(arg + 1, GFP_KERNEL);
380 			if (!code->data)
381 				return -ENOMEM;
382 			if (++code == end) {
383 				trace_probe_log_err(offs, TOO_MANY_OPS);
384 				return -EINVAL;
385 			}
386 			code->op = FETCH_OP_IMM;
387 			code->immediate = 0;
388 		}
389 		/* These are fetching from memory */
390 		if (++code == end) {
391 			trace_probe_log_err(offs, TOO_MANY_OPS);
392 			return -EINVAL;
393 		}
394 		*pcode = code;
395 		code->op = FETCH_OP_DEREF;
396 		code->offset = offset;
397 		break;
398 
399 	case '+':	/* deref memory */
400 	case '-':
401 		if (arg[1] == 'u') {
402 			deref = FETCH_OP_UDEREF;
403 			arg[1] = arg[0];
404 			arg++;
405 		}
406 		if (arg[0] == '+')
407 			arg++;	/* Skip '+', because kstrtol() rejects it. */
408 		tmp = strchr(arg, '(');
409 		if (!tmp) {
410 			trace_probe_log_err(offs, DEREF_NEED_BRACE);
411 			return -EINVAL;
412 		}
413 		*tmp = '\0';
414 		ret = kstrtol(arg, 0, &offset);
415 		if (ret) {
416 			trace_probe_log_err(offs, BAD_DEREF_OFFS);
417 			break;
418 		}
419 		offs += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0);
420 		arg = tmp + 1;
421 		tmp = strrchr(arg, ')');
422 		if (!tmp) {
423 			trace_probe_log_err(offs + strlen(arg),
424 					    DEREF_OPEN_BRACE);
425 			return -EINVAL;
426 		} else {
427 			const struct fetch_type *t2 = find_fetch_type(NULL);
428 
429 			*tmp = '\0';
430 			ret = parse_probe_arg(arg, t2, &code, end, flags, offs);
431 			if (ret)
432 				break;
433 			if (code->op == FETCH_OP_COMM) {
434 				trace_probe_log_err(offs, COMM_CANT_DEREF);
435 				return -EINVAL;
436 			}
437 			if (++code == end) {
438 				trace_probe_log_err(offs, TOO_MANY_OPS);
439 				return -EINVAL;
440 			}
441 			*pcode = code;
442 
443 			code->op = deref;
444 			code->offset = offset;
445 		}
446 		break;
447 	}
448 	if (!ret && code->op == FETCH_OP_NOP) {
449 		/* Parsed, but do not find fetch method */
450 		trace_probe_log_err(offs, BAD_FETCH_ARG);
451 		ret = -EINVAL;
452 	}
453 	return ret;
454 }
455 
456 #define BYTES_TO_BITS(nb)	((BITS_PER_LONG * (nb)) / sizeof(long))
457 
458 /* Bitfield type needs to be parsed into a fetch function */
459 static int __parse_bitfield_probe_arg(const char *bf,
460 				      const struct fetch_type *t,
461 				      struct fetch_insn **pcode)
462 {
463 	struct fetch_insn *code = *pcode;
464 	unsigned long bw, bo;
465 	char *tail;
466 
467 	if (*bf != 'b')
468 		return 0;
469 
470 	bw = simple_strtoul(bf + 1, &tail, 0);	/* Use simple one */
471 
472 	if (bw == 0 || *tail != '@')
473 		return -EINVAL;
474 
475 	bf = tail + 1;
476 	bo = simple_strtoul(bf, &tail, 0);
477 
478 	if (tail == bf || *tail != '/')
479 		return -EINVAL;
480 	code++;
481 	if (code->op != FETCH_OP_NOP)
482 		return -EINVAL;
483 	*pcode = code;
484 
485 	code->op = FETCH_OP_MOD_BF;
486 	code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
487 	code->rshift = BYTES_TO_BITS(t->size) - bw;
488 	code->basesize = t->size;
489 
490 	return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
491 }
492 
493 /* String length checking wrapper */
494 static int traceprobe_parse_probe_arg_body(char *arg, ssize_t *size,
495 		struct probe_arg *parg, unsigned int flags, int offset)
496 {
497 	struct fetch_insn *code, *scode, *tmp = NULL;
498 	char *t, *t2, *t3;
499 	int ret, len;
500 
501 	len = strlen(arg);
502 	if (len > MAX_ARGSTR_LEN) {
503 		trace_probe_log_err(offset, ARG_TOO_LONG);
504 		return -EINVAL;
505 	} else if (len == 0) {
506 		trace_probe_log_err(offset, NO_ARG_BODY);
507 		return -EINVAL;
508 	}
509 
510 	parg->comm = kstrdup(arg, GFP_KERNEL);
511 	if (!parg->comm)
512 		return -ENOMEM;
513 
514 	t = strchr(arg, ':');
515 	if (t) {
516 		*t = '\0';
517 		t2 = strchr(++t, '[');
518 		if (t2) {
519 			*t2++ = '\0';
520 			t3 = strchr(t2, ']');
521 			if (!t3) {
522 				offset += t2 + strlen(t2) - arg;
523 				trace_probe_log_err(offset,
524 						    ARRAY_NO_CLOSE);
525 				return -EINVAL;
526 			} else if (t3[1] != '\0') {
527 				trace_probe_log_err(offset + t3 + 1 - arg,
528 						    BAD_ARRAY_SUFFIX);
529 				return -EINVAL;
530 			}
531 			*t3 = '\0';
532 			if (kstrtouint(t2, 0, &parg->count) || !parg->count) {
533 				trace_probe_log_err(offset + t2 - arg,
534 						    BAD_ARRAY_NUM);
535 				return -EINVAL;
536 			}
537 			if (parg->count > MAX_ARRAY_LEN) {
538 				trace_probe_log_err(offset + t2 - arg,
539 						    ARRAY_TOO_BIG);
540 				return -EINVAL;
541 			}
542 		}
543 	}
544 
545 	/* Since $comm can not be dereferred, we can find $comm by strcmp */
546 	if (strcmp(arg, "$comm") == 0) {
547 		/* The type of $comm must be "string", and not an array. */
548 		if (parg->count || (t && strcmp(t, "string")))
549 			return -EINVAL;
550 		parg->type = find_fetch_type("string");
551 	} else
552 		parg->type = find_fetch_type(t);
553 	if (!parg->type) {
554 		trace_probe_log_err(offset + (t ? (t - arg) : 0), BAD_TYPE);
555 		return -EINVAL;
556 	}
557 	parg->offset = *size;
558 	*size += parg->type->size * (parg->count ?: 1);
559 
560 	if (parg->count) {
561 		len = strlen(parg->type->fmttype) + 6;
562 		parg->fmt = kmalloc(len, GFP_KERNEL);
563 		if (!parg->fmt)
564 			return -ENOMEM;
565 		snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
566 			 parg->count);
567 	}
568 
569 	code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL);
570 	if (!code)
571 		return -ENOMEM;
572 	code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
573 
574 	ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
575 			      flags, offset);
576 	if (ret)
577 		goto fail;
578 
579 	/* Store operation */
580 	if (!strcmp(parg->type->name, "string") ||
581 	    !strcmp(parg->type->name, "ustring")) {
582 		if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF &&
583 		    code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM) {
584 			trace_probe_log_err(offset + (t ? (t - arg) : 0),
585 					    BAD_STRING);
586 			ret = -EINVAL;
587 			goto fail;
588 		}
589 		if ((code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM) ||
590 		     parg->count) {
591 			/*
592 			 * IMM and COMM is pointing actual address, those must
593 			 * be kept, and if parg->count != 0, this is an array
594 			 * of string pointers instead of string address itself.
595 			 */
596 			code++;
597 			if (code->op != FETCH_OP_NOP) {
598 				trace_probe_log_err(offset, TOO_MANY_OPS);
599 				ret = -EINVAL;
600 				goto fail;
601 			}
602 		}
603 		/* If op == DEREF, replace it with STRING */
604 		if (!strcmp(parg->type->name, "ustring") ||
605 		    code->op == FETCH_OP_UDEREF)
606 			code->op = FETCH_OP_ST_USTRING;
607 		else
608 			code->op = FETCH_OP_ST_STRING;
609 		code->size = parg->type->size;
610 		parg->dynamic = true;
611 	} else if (code->op == FETCH_OP_DEREF) {
612 		code->op = FETCH_OP_ST_MEM;
613 		code->size = parg->type->size;
614 	} else if (code->op == FETCH_OP_UDEREF) {
615 		code->op = FETCH_OP_ST_UMEM;
616 		code->size = parg->type->size;
617 	} else {
618 		code++;
619 		if (code->op != FETCH_OP_NOP) {
620 			trace_probe_log_err(offset, TOO_MANY_OPS);
621 			ret = -EINVAL;
622 			goto fail;
623 		}
624 		code->op = FETCH_OP_ST_RAW;
625 		code->size = parg->type->size;
626 	}
627 	scode = code;
628 	/* Modify operation */
629 	if (t != NULL) {
630 		ret = __parse_bitfield_probe_arg(t, parg->type, &code);
631 		if (ret) {
632 			trace_probe_log_err(offset + t - arg, BAD_BITFIELD);
633 			goto fail;
634 		}
635 	}
636 	/* Loop(Array) operation */
637 	if (parg->count) {
638 		if (scode->op != FETCH_OP_ST_MEM &&
639 		    scode->op != FETCH_OP_ST_STRING &&
640 		    scode->op != FETCH_OP_ST_USTRING) {
641 			trace_probe_log_err(offset + (t ? (t - arg) : 0),
642 					    BAD_STRING);
643 			ret = -EINVAL;
644 			goto fail;
645 		}
646 		code++;
647 		if (code->op != FETCH_OP_NOP) {
648 			trace_probe_log_err(offset, TOO_MANY_OPS);
649 			ret = -EINVAL;
650 			goto fail;
651 		}
652 		code->op = FETCH_OP_LP_ARRAY;
653 		code->param = parg->count;
654 	}
655 	code++;
656 	code->op = FETCH_OP_END;
657 
658 	/* Shrink down the code buffer */
659 	parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL);
660 	if (!parg->code)
661 		ret = -ENOMEM;
662 	else
663 		memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
664 
665 fail:
666 	if (ret) {
667 		for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
668 			if (code->op == FETCH_NOP_SYMBOL)
669 				kfree(code->data);
670 	}
671 	kfree(tmp);
672 
673 	return ret;
674 }
675 
676 /* Return 1 if name is reserved or already used by another argument */
677 static int traceprobe_conflict_field_name(const char *name,
678 					  struct probe_arg *args, int narg)
679 {
680 	int i;
681 
682 	for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
683 		if (strcmp(reserved_field_names[i], name) == 0)
684 			return 1;
685 
686 	for (i = 0; i < narg; i++)
687 		if (strcmp(args[i].name, name) == 0)
688 			return 1;
689 
690 	return 0;
691 }
692 
693 int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, char *arg,
694 				unsigned int flags)
695 {
696 	struct probe_arg *parg = &tp->args[i];
697 	char *body;
698 
699 	/* Increment count for freeing args in error case */
700 	tp->nr_args++;
701 
702 	body = strchr(arg, '=');
703 	if (body) {
704 		if (body - arg > MAX_ARG_NAME_LEN) {
705 			trace_probe_log_err(0, ARG_NAME_TOO_LONG);
706 			return -EINVAL;
707 		} else if (body == arg) {
708 			trace_probe_log_err(0, NO_ARG_NAME);
709 			return -EINVAL;
710 		}
711 		parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
712 		body++;
713 	} else {
714 		/* If argument name is omitted, set "argN" */
715 		parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1);
716 		body = arg;
717 	}
718 	if (!parg->name)
719 		return -ENOMEM;
720 
721 	if (!is_good_name(parg->name)) {
722 		trace_probe_log_err(0, BAD_ARG_NAME);
723 		return -EINVAL;
724 	}
725 	if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
726 		trace_probe_log_err(0, USED_ARG_NAME);
727 		return -EINVAL;
728 	}
729 	/* Parse fetch argument */
730 	return traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags,
731 					       body - arg);
732 }
733 
734 void traceprobe_free_probe_arg(struct probe_arg *arg)
735 {
736 	struct fetch_insn *code = arg->code;
737 
738 	while (code && code->op != FETCH_OP_END) {
739 		if (code->op == FETCH_NOP_SYMBOL)
740 			kfree(code->data);
741 		code++;
742 	}
743 	kfree(arg->code);
744 	kfree(arg->name);
745 	kfree(arg->comm);
746 	kfree(arg->fmt);
747 }
748 
749 int traceprobe_update_arg(struct probe_arg *arg)
750 {
751 	struct fetch_insn *code = arg->code;
752 	long offset;
753 	char *tmp;
754 	char c;
755 	int ret = 0;
756 
757 	while (code && code->op != FETCH_OP_END) {
758 		if (code->op == FETCH_NOP_SYMBOL) {
759 			if (code[1].op != FETCH_OP_IMM)
760 				return -EINVAL;
761 
762 			tmp = strpbrk(code->data, "+-");
763 			if (tmp)
764 				c = *tmp;
765 			ret = traceprobe_split_symbol_offset(code->data,
766 							     &offset);
767 			if (ret)
768 				return ret;
769 
770 			code[1].immediate =
771 				(unsigned long)kallsyms_lookup_name(code->data);
772 			if (tmp)
773 				*tmp = c;
774 			if (!code[1].immediate)
775 				return -ENOENT;
776 			code[1].immediate += offset;
777 		}
778 		code++;
779 	}
780 	return 0;
781 }
782 
783 /* When len=0, we just calculate the needed length */
784 #define LEN_OR_ZERO (len ? len - pos : 0)
785 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
786 			   bool is_return)
787 {
788 	struct probe_arg *parg;
789 	int i, j;
790 	int pos = 0;
791 	const char *fmt, *arg;
792 
793 	if (!is_return) {
794 		fmt = "(%lx)";
795 		arg = "REC->" FIELD_STRING_IP;
796 	} else {
797 		fmt = "(%lx <- %lx)";
798 		arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
799 	}
800 
801 	pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
802 
803 	for (i = 0; i < tp->nr_args; i++) {
804 		parg = tp->args + i;
805 		pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
806 		if (parg->count) {
807 			pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
808 					parg->type->fmt);
809 			for (j = 1; j < parg->count; j++)
810 				pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
811 						parg->type->fmt);
812 			pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
813 		} else
814 			pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
815 					parg->type->fmt);
816 	}
817 
818 	pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
819 
820 	for (i = 0; i < tp->nr_args; i++) {
821 		parg = tp->args + i;
822 		if (parg->count) {
823 			if (strcmp(parg->type->name, "string") == 0)
824 				fmt = ", __get_str(%s[%d])";
825 			else
826 				fmt = ", REC->%s[%d]";
827 			for (j = 0; j < parg->count; j++)
828 				pos += snprintf(buf + pos, LEN_OR_ZERO,
829 						fmt, parg->name, j);
830 		} else {
831 			if (strcmp(parg->type->name, "string") == 0)
832 				fmt = ", __get_str(%s)";
833 			else
834 				fmt = ", REC->%s";
835 			pos += snprintf(buf + pos, LEN_OR_ZERO,
836 					fmt, parg->name);
837 		}
838 	}
839 
840 	/* return the length of print_fmt */
841 	return pos;
842 }
843 #undef LEN_OR_ZERO
844 
845 int traceprobe_set_print_fmt(struct trace_probe *tp, bool is_return)
846 {
847 	struct trace_event_call *call = trace_probe_event_call(tp);
848 	int len;
849 	char *print_fmt;
850 
851 	/* First: called with 0 length to calculate the needed length */
852 	len = __set_print_fmt(tp, NULL, 0, is_return);
853 	print_fmt = kmalloc(len + 1, GFP_KERNEL);
854 	if (!print_fmt)
855 		return -ENOMEM;
856 
857 	/* Second: actually write the @print_fmt */
858 	__set_print_fmt(tp, print_fmt, len + 1, is_return);
859 	call->print_fmt = print_fmt;
860 
861 	return 0;
862 }
863 
864 int traceprobe_define_arg_fields(struct trace_event_call *event_call,
865 				 size_t offset, struct trace_probe *tp)
866 {
867 	int ret, i;
868 
869 	/* Set argument names as fields */
870 	for (i = 0; i < tp->nr_args; i++) {
871 		struct probe_arg *parg = &tp->args[i];
872 		const char *fmt = parg->type->fmttype;
873 		int size = parg->type->size;
874 
875 		if (parg->fmt)
876 			fmt = parg->fmt;
877 		if (parg->count)
878 			size *= parg->count;
879 		ret = trace_define_field(event_call, fmt, parg->name,
880 					 offset + parg->offset, size,
881 					 parg->type->is_signed,
882 					 FILTER_OTHER);
883 		if (ret)
884 			return ret;
885 	}
886 	return 0;
887 }
888 
889 
890 void trace_probe_cleanup(struct trace_probe *tp)
891 {
892 	struct trace_event_call *call = trace_probe_event_call(tp);
893 	int i;
894 
895 	for (i = 0; i < tp->nr_args; i++)
896 		traceprobe_free_probe_arg(&tp->args[i]);
897 
898 	kfree(call->class->system);
899 	kfree(call->name);
900 	kfree(call->print_fmt);
901 }
902 
903 int trace_probe_init(struct trace_probe *tp, const char *event,
904 		     const char *group)
905 {
906 	struct trace_event_call *call = trace_probe_event_call(tp);
907 
908 	if (!event || !group)
909 		return -EINVAL;
910 
911 	call->class = &tp->class;
912 	call->name = kstrdup(event, GFP_KERNEL);
913 	if (!call->name)
914 		return -ENOMEM;
915 
916 	tp->class.system = kstrdup(group, GFP_KERNEL);
917 	if (!tp->class.system) {
918 		kfree(call->name);
919 		call->name = NULL;
920 		return -ENOMEM;
921 	}
922 	INIT_LIST_HEAD(&tp->files);
923 	INIT_LIST_HEAD(&tp->class.fields);
924 
925 	return 0;
926 }
927 
928 int trace_probe_register_event_call(struct trace_probe *tp)
929 {
930 	struct trace_event_call *call = trace_probe_event_call(tp);
931 	int ret;
932 
933 	ret = register_trace_event(&call->event);
934 	if (!ret)
935 		return -ENODEV;
936 
937 	ret = trace_add_event_call(call);
938 	if (ret)
939 		unregister_trace_event(&call->event);
940 
941 	return ret;
942 }
943 
944 int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
945 {
946 	struct event_file_link *link;
947 
948 	link = kmalloc(sizeof(*link), GFP_KERNEL);
949 	if (!link)
950 		return -ENOMEM;
951 
952 	link->file = file;
953 	INIT_LIST_HEAD(&link->list);
954 	list_add_tail_rcu(&link->list, &tp->files);
955 	trace_probe_set_flag(tp, TP_FLAG_TRACE);
956 	return 0;
957 }
958 
959 struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
960 						  struct trace_event_file *file)
961 {
962 	struct event_file_link *link;
963 
964 	trace_probe_for_each_link(link, tp) {
965 		if (link->file == file)
966 			return link;
967 	}
968 
969 	return NULL;
970 }
971 
972 int trace_probe_remove_file(struct trace_probe *tp,
973 			    struct trace_event_file *file)
974 {
975 	struct event_file_link *link;
976 
977 	link = trace_probe_get_file_link(tp, file);
978 	if (!link)
979 		return -ENOENT;
980 
981 	list_del_rcu(&link->list);
982 	synchronize_rcu();
983 	kfree(link);
984 
985 	if (list_empty(&tp->files))
986 		trace_probe_clear_flag(tp, TP_FLAG_TRACE);
987 
988 	return 0;
989 }
990