genirq/debugfs: No need to check return value of debugfs_create functions
[linux/fpc-iii.git] / kernel / trace / trace_probe.c
blob9962cb5da8acdebb43e247c1e55df86a19038a51
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Common code for probe-based Dynamic events.
5 * This code was copied from kernel/trace/trace_kprobe.c written by
6 * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
8 * Updates to make this generic:
9 * Copyright (C) IBM Corporation, 2010-2011
10 * Author: Srikar Dronamraju
12 #define pr_fmt(fmt) "trace_probe: " fmt
14 #include "trace_probe.h"
16 const char *reserved_field_names[] = {
17 "common_type",
18 "common_flags",
19 "common_preempt_count",
20 "common_pid",
21 "common_tgid",
22 FIELD_STRING_IP,
23 FIELD_STRING_RETIP,
24 FIELD_STRING_FUNC,
27 /* Printing in basic type function template */
28 #define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt) \
29 int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, void *data, void *ent)\
30 { \
31 trace_seq_printf(s, fmt, *(type *)data); \
32 return !trace_seq_has_overflowed(s); \
33 } \
34 const char PRINT_TYPE_FMT_NAME(tname)[] = fmt;
36 DEFINE_BASIC_PRINT_TYPE_FUNC(u8, u8, "%u")
37 DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u")
38 DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u")
39 DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu")
40 DEFINE_BASIC_PRINT_TYPE_FUNC(s8, s8, "%d")
41 DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d")
42 DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d")
43 DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld")
44 DEFINE_BASIC_PRINT_TYPE_FUNC(x8, u8, "0x%x")
45 DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x")
46 DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x")
47 DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx")
49 int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent)
51 trace_seq_printf(s, "%pS", (void *)*(unsigned long *)data);
52 return !trace_seq_has_overflowed(s);
54 const char PRINT_TYPE_FMT_NAME(symbol)[] = "%pS";
56 /* Print type function for string type */
57 int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, void *data, void *ent)
59 int len = *(u32 *)data >> 16;
61 if (!len)
62 trace_seq_puts(s, "(fault)");
63 else
64 trace_seq_printf(s, "\"%s\"",
65 (const char *)get_loc_data(data, ent));
66 return !trace_seq_has_overflowed(s);
69 const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
71 /* Fetch type information table */
72 static const struct fetch_type probe_fetch_types[] = {
73 /* Special types */
74 __ASSIGN_FETCH_TYPE("string", string, string, sizeof(u32), 1,
75 "__data_loc char[]"),
76 /* Basic types */
77 ASSIGN_FETCH_TYPE(u8, u8, 0),
78 ASSIGN_FETCH_TYPE(u16, u16, 0),
79 ASSIGN_FETCH_TYPE(u32, u32, 0),
80 ASSIGN_FETCH_TYPE(u64, u64, 0),
81 ASSIGN_FETCH_TYPE(s8, u8, 1),
82 ASSIGN_FETCH_TYPE(s16, u16, 1),
83 ASSIGN_FETCH_TYPE(s32, u32, 1),
84 ASSIGN_FETCH_TYPE(s64, u64, 1),
85 ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0),
86 ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
87 ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
88 ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
89 ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0),
91 ASSIGN_FETCH_TYPE_END
94 static const struct fetch_type *find_fetch_type(const char *type)
96 int i;
98 if (!type)
99 type = DEFAULT_FETCH_TYPE_STR;
101 /* Special case: bitfield */
102 if (*type == 'b') {
103 unsigned long bs;
105 type = strchr(type, '/');
106 if (!type)
107 goto fail;
109 type++;
110 if (kstrtoul(type, 0, &bs))
111 goto fail;
113 switch (bs) {
114 case 8:
115 return find_fetch_type("u8");
116 case 16:
117 return find_fetch_type("u16");
118 case 32:
119 return find_fetch_type("u32");
120 case 64:
121 return find_fetch_type("u64");
122 default:
123 goto fail;
127 for (i = 0; probe_fetch_types[i].name; i++) {
128 if (strcmp(type, probe_fetch_types[i].name) == 0)
129 return &probe_fetch_types[i];
132 fail:
133 return NULL;
136 /* Split symbol and offset. */
137 int traceprobe_split_symbol_offset(char *symbol, long *offset)
139 char *tmp;
140 int ret;
142 if (!offset)
143 return -EINVAL;
145 tmp = strpbrk(symbol, "+-");
146 if (tmp) {
147 ret = kstrtol(tmp, 0, offset);
148 if (ret)
149 return ret;
150 *tmp = '\0';
151 } else
152 *offset = 0;
154 return 0;
157 /* @buf must has MAX_EVENT_NAME_LEN size */
158 int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
159 char *buf)
161 const char *slash, *event = *pevent;
163 slash = strchr(event, '/');
164 if (slash) {
165 if (slash == event) {
166 pr_info("Group name is not specified\n");
167 return -EINVAL;
169 if (slash - event + 1 > MAX_EVENT_NAME_LEN) {
170 pr_info("Group name is too long\n");
171 return -E2BIG;
173 strlcpy(buf, event, slash - event + 1);
174 *pgroup = buf;
175 *pevent = slash + 1;
177 if (strlen(event) == 0) {
178 pr_info("Event name is not specified\n");
179 return -EINVAL;
181 return 0;
184 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
186 static int parse_probe_vars(char *arg, const struct fetch_type *t,
187 struct fetch_insn *code, unsigned int flags)
189 unsigned long param;
190 int ret = 0;
191 int len;
193 if (strcmp(arg, "retval") == 0) {
194 if (flags & TPARG_FL_RETURN)
195 code->op = FETCH_OP_RETVAL;
196 else
197 ret = -EINVAL;
198 } else if ((len = str_has_prefix(arg, "stack"))) {
199 if (arg[len] == '\0') {
200 code->op = FETCH_OP_STACKP;
201 } else if (isdigit(arg[len])) {
202 ret = kstrtoul(arg + len, 10, &param);
203 if (ret || ((flags & TPARG_FL_KERNEL) &&
204 param > PARAM_MAX_STACK))
205 ret = -EINVAL;
206 else {
207 code->op = FETCH_OP_STACK;
208 code->param = (unsigned int)param;
210 } else
211 ret = -EINVAL;
212 } else if (strcmp(arg, "comm") == 0) {
213 code->op = FETCH_OP_COMM;
214 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
215 } else if (((flags & TPARG_FL_MASK) ==
216 (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) &&
217 (len = str_has_prefix(arg, "arg"))) {
218 if (!isdigit(arg[len]))
219 return -EINVAL;
220 ret = kstrtoul(arg + len, 10, &param);
221 if (ret || !param || param > PARAM_MAX_STACK)
222 return -EINVAL;
223 code->op = FETCH_OP_ARG;
224 code->param = (unsigned int)param - 1;
225 #endif
226 } else
227 ret = -EINVAL;
229 return ret;
232 /* Recursive argument parser */
233 static int
234 parse_probe_arg(char *arg, const struct fetch_type *type,
235 struct fetch_insn **pcode, struct fetch_insn *end,
236 unsigned int flags)
238 struct fetch_insn *code = *pcode;
239 unsigned long param;
240 long offset = 0;
241 char *tmp;
242 int ret = 0;
244 switch (arg[0]) {
245 case '$':
246 ret = parse_probe_vars(arg + 1, type, code, flags);
247 break;
249 case '%': /* named register */
250 ret = regs_query_register_offset(arg + 1);
251 if (ret >= 0) {
252 code->op = FETCH_OP_REG;
253 code->param = (unsigned int)ret;
254 ret = 0;
256 break;
258 case '@': /* memory, file-offset or symbol */
259 if (isdigit(arg[1])) {
260 ret = kstrtoul(arg + 1, 0, &param);
261 if (ret)
262 break;
263 /* load address */
264 code->op = FETCH_OP_IMM;
265 code->immediate = param;
266 } else if (arg[1] == '+') {
267 /* kprobes don't support file offsets */
268 if (flags & TPARG_FL_KERNEL)
269 return -EINVAL;
271 ret = kstrtol(arg + 2, 0, &offset);
272 if (ret)
273 break;
275 code->op = FETCH_OP_FOFFS;
276 code->immediate = (unsigned long)offset; // imm64?
277 } else {
278 /* uprobes don't support symbols */
279 if (!(flags & TPARG_FL_KERNEL))
280 return -EINVAL;
282 /* Preserve symbol for updating */
283 code->op = FETCH_NOP_SYMBOL;
284 code->data = kstrdup(arg + 1, GFP_KERNEL);
285 if (!code->data)
286 return -ENOMEM;
287 if (++code == end)
288 return -E2BIG;
290 code->op = FETCH_OP_IMM;
291 code->immediate = 0;
293 /* These are fetching from memory */
294 if (++code == end)
295 return -E2BIG;
296 *pcode = code;
297 code->op = FETCH_OP_DEREF;
298 code->offset = offset;
299 break;
301 case '+': /* deref memory */
302 arg++; /* Skip '+', because kstrtol() rejects it. */
303 case '-':
304 tmp = strchr(arg, '(');
305 if (!tmp)
306 return -EINVAL;
308 *tmp = '\0';
309 ret = kstrtol(arg, 0, &offset);
310 if (ret)
311 break;
313 arg = tmp + 1;
314 tmp = strrchr(arg, ')');
316 if (tmp) {
317 const struct fetch_type *t2 = find_fetch_type(NULL);
319 *tmp = '\0';
320 ret = parse_probe_arg(arg, t2, &code, end, flags);
321 if (ret)
322 break;
323 if (code->op == FETCH_OP_COMM)
324 return -EINVAL;
325 if (++code == end)
326 return -E2BIG;
327 *pcode = code;
329 code->op = FETCH_OP_DEREF;
330 code->offset = offset;
332 break;
334 if (!ret && code->op == FETCH_OP_NOP) {
335 /* Parsed, but do not find fetch method */
336 ret = -EINVAL;
338 return ret;
341 #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
343 /* Bitfield type needs to be parsed into a fetch function */
344 static int __parse_bitfield_probe_arg(const char *bf,
345 const struct fetch_type *t,
346 struct fetch_insn **pcode)
348 struct fetch_insn *code = *pcode;
349 unsigned long bw, bo;
350 char *tail;
352 if (*bf != 'b')
353 return 0;
355 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
357 if (bw == 0 || *tail != '@')
358 return -EINVAL;
360 bf = tail + 1;
361 bo = simple_strtoul(bf, &tail, 0);
363 if (tail == bf || *tail != '/')
364 return -EINVAL;
365 code++;
366 if (code->op != FETCH_OP_NOP)
367 return -E2BIG;
368 *pcode = code;
370 code->op = FETCH_OP_MOD_BF;
371 code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
372 code->rshift = BYTES_TO_BITS(t->size) - bw;
373 code->basesize = t->size;
375 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
378 /* String length checking wrapper */
379 static int traceprobe_parse_probe_arg_body(char *arg, ssize_t *size,
380 struct probe_arg *parg, unsigned int flags)
382 struct fetch_insn *code, *scode, *tmp = NULL;
383 char *t, *t2;
384 int ret, len;
386 if (strlen(arg) > MAX_ARGSTR_LEN) {
387 pr_info("Argument is too long.: %s\n", arg);
388 return -ENOSPC;
390 parg->comm = kstrdup(arg, GFP_KERNEL);
391 if (!parg->comm) {
392 pr_info("Failed to allocate memory for command '%s'.\n", arg);
393 return -ENOMEM;
395 t = strchr(arg, ':');
396 if (t) {
397 *t = '\0';
398 t2 = strchr(++t, '[');
399 if (t2) {
400 *t2 = '\0';
401 parg->count = simple_strtoul(t2 + 1, &t2, 0);
402 if (strcmp(t2, "]") || parg->count == 0)
403 return -EINVAL;
404 if (parg->count > MAX_ARRAY_LEN)
405 return -E2BIG;
409 * The default type of $comm should be "string", and it can't be
410 * dereferenced.
412 if (!t && strcmp(arg, "$comm") == 0)
413 parg->type = find_fetch_type("string");
414 else
415 parg->type = find_fetch_type(t);
416 if (!parg->type) {
417 pr_info("Unsupported type: %s\n", t);
418 return -EINVAL;
420 parg->offset = *size;
421 *size += parg->type->size * (parg->count ?: 1);
423 if (parg->count) {
424 len = strlen(parg->type->fmttype) + 6;
425 parg->fmt = kmalloc(len, GFP_KERNEL);
426 if (!parg->fmt)
427 return -ENOMEM;
428 snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
429 parg->count);
432 code = tmp = kzalloc(sizeof(*code) * FETCH_INSN_MAX, GFP_KERNEL);
433 if (!code)
434 return -ENOMEM;
435 code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
437 ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
438 flags);
439 if (ret)
440 goto fail;
442 /* Store operation */
443 if (!strcmp(parg->type->name, "string")) {
444 if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_IMM &&
445 code->op != FETCH_OP_COMM) {
446 pr_info("string only accepts memory or address.\n");
447 ret = -EINVAL;
448 goto fail;
450 if (code->op != FETCH_OP_DEREF || parg->count) {
452 * IMM and COMM is pointing actual address, those must
453 * be kept, and if parg->count != 0, this is an array
454 * of string pointers instead of string address itself.
456 code++;
457 if (code->op != FETCH_OP_NOP) {
458 ret = -E2BIG;
459 goto fail;
462 code->op = FETCH_OP_ST_STRING; /* In DEREF case, replace it */
463 code->size = parg->type->size;
464 parg->dynamic = true;
465 } else if (code->op == FETCH_OP_DEREF) {
466 code->op = FETCH_OP_ST_MEM;
467 code->size = parg->type->size;
468 } else {
469 code++;
470 if (code->op != FETCH_OP_NOP) {
471 ret = -E2BIG;
472 goto fail;
474 code->op = FETCH_OP_ST_RAW;
475 code->size = parg->type->size;
477 scode = code;
478 /* Modify operation */
479 if (t != NULL) {
480 ret = __parse_bitfield_probe_arg(t, parg->type, &code);
481 if (ret)
482 goto fail;
484 /* Loop(Array) operation */
485 if (parg->count) {
486 if (scode->op != FETCH_OP_ST_MEM &&
487 scode->op != FETCH_OP_ST_STRING) {
488 pr_info("array only accepts memory or address\n");
489 ret = -EINVAL;
490 goto fail;
492 code++;
493 if (code->op != FETCH_OP_NOP) {
494 ret = -E2BIG;
495 goto fail;
497 code->op = FETCH_OP_LP_ARRAY;
498 code->param = parg->count;
500 code++;
501 code->op = FETCH_OP_END;
503 /* Shrink down the code buffer */
504 parg->code = kzalloc(sizeof(*code) * (code - tmp + 1), GFP_KERNEL);
505 if (!parg->code)
506 ret = -ENOMEM;
507 else
508 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
510 fail:
511 if (ret) {
512 for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
513 if (code->op == FETCH_NOP_SYMBOL)
514 kfree(code->data);
516 kfree(tmp);
518 return ret;
521 /* Return 1 if name is reserved or already used by another argument */
522 static int traceprobe_conflict_field_name(const char *name,
523 struct probe_arg *args, int narg)
525 int i;
527 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
528 if (strcmp(reserved_field_names[i], name) == 0)
529 return 1;
531 for (i = 0; i < narg; i++)
532 if (strcmp(args[i].name, name) == 0)
533 return 1;
535 return 0;
538 int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, char *arg,
539 unsigned int flags)
541 struct probe_arg *parg = &tp->args[i];
542 char *body;
543 int ret;
545 /* Increment count for freeing args in error case */
546 tp->nr_args++;
548 body = strchr(arg, '=');
549 if (body) {
550 parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
551 body++;
552 } else {
553 /* If argument name is omitted, set "argN" */
554 parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1);
555 body = arg;
557 if (!parg->name)
558 return -ENOMEM;
560 if (!is_good_name(parg->name)) {
561 pr_info("Invalid argument[%d] name: %s\n",
562 i, parg->name);
563 return -EINVAL;
566 if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
567 pr_info("Argument[%d]: '%s' conflicts with another field.\n",
568 i, parg->name);
569 return -EINVAL;
572 /* Parse fetch argument */
573 ret = traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags);
574 if (ret)
575 pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
576 return ret;
579 void traceprobe_free_probe_arg(struct probe_arg *arg)
581 struct fetch_insn *code = arg->code;
583 while (code && code->op != FETCH_OP_END) {
584 if (code->op == FETCH_NOP_SYMBOL)
585 kfree(code->data);
586 code++;
588 kfree(arg->code);
589 kfree(arg->name);
590 kfree(arg->comm);
591 kfree(arg->fmt);
594 int traceprobe_update_arg(struct probe_arg *arg)
596 struct fetch_insn *code = arg->code;
597 long offset;
598 char *tmp;
599 char c;
600 int ret = 0;
602 while (code && code->op != FETCH_OP_END) {
603 if (code->op == FETCH_NOP_SYMBOL) {
604 if (code[1].op != FETCH_OP_IMM)
605 return -EINVAL;
607 tmp = strpbrk(code->data, "+-");
608 if (tmp)
609 c = *tmp;
610 ret = traceprobe_split_symbol_offset(code->data,
611 &offset);
612 if (ret)
613 return ret;
615 code[1].immediate =
616 (unsigned long)kallsyms_lookup_name(code->data);
617 if (tmp)
618 *tmp = c;
619 if (!code[1].immediate)
620 return -ENOENT;
621 code[1].immediate += offset;
623 code++;
625 return 0;
628 /* When len=0, we just calculate the needed length */
629 #define LEN_OR_ZERO (len ? len - pos : 0)
630 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
631 bool is_return)
633 struct probe_arg *parg;
634 int i, j;
635 int pos = 0;
636 const char *fmt, *arg;
638 if (!is_return) {
639 fmt = "(%lx)";
640 arg = "REC->" FIELD_STRING_IP;
641 } else {
642 fmt = "(%lx <- %lx)";
643 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
646 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
648 for (i = 0; i < tp->nr_args; i++) {
649 parg = tp->args + i;
650 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
651 if (parg->count) {
652 pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
653 parg->type->fmt);
654 for (j = 1; j < parg->count; j++)
655 pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
656 parg->type->fmt);
657 pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
658 } else
659 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
660 parg->type->fmt);
663 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
665 for (i = 0; i < tp->nr_args; i++) {
666 parg = tp->args + i;
667 if (parg->count) {
668 if (strcmp(parg->type->name, "string") == 0)
669 fmt = ", __get_str(%s[%d])";
670 else
671 fmt = ", REC->%s[%d]";
672 for (j = 0; j < parg->count; j++)
673 pos += snprintf(buf + pos, LEN_OR_ZERO,
674 fmt, parg->name, j);
675 } else {
676 if (strcmp(parg->type->name, "string") == 0)
677 fmt = ", __get_str(%s)";
678 else
679 fmt = ", REC->%s";
680 pos += snprintf(buf + pos, LEN_OR_ZERO,
681 fmt, parg->name);
685 /* return the length of print_fmt */
686 return pos;
688 #undef LEN_OR_ZERO
690 int traceprobe_set_print_fmt(struct trace_probe *tp, bool is_return)
692 int len;
693 char *print_fmt;
695 /* First: called with 0 length to calculate the needed length */
696 len = __set_print_fmt(tp, NULL, 0, is_return);
697 print_fmt = kmalloc(len + 1, GFP_KERNEL);
698 if (!print_fmt)
699 return -ENOMEM;
701 /* Second: actually write the @print_fmt */
702 __set_print_fmt(tp, print_fmt, len + 1, is_return);
703 tp->call.print_fmt = print_fmt;
705 return 0;
708 int traceprobe_define_arg_fields(struct trace_event_call *event_call,
709 size_t offset, struct trace_probe *tp)
711 int ret, i;
713 /* Set argument names as fields */
714 for (i = 0; i < tp->nr_args; i++) {
715 struct probe_arg *parg = &tp->args[i];
716 const char *fmt = parg->type->fmttype;
717 int size = parg->type->size;
719 if (parg->fmt)
720 fmt = parg->fmt;
721 if (parg->count)
722 size *= parg->count;
723 ret = trace_define_field(event_call, fmt, parg->name,
724 offset + parg->offset, size,
725 parg->type->is_signed,
726 FILTER_OTHER);
727 if (ret)
728 return ret;
730 return 0;