mtd: rawnand: sunxi: Add A23/A33 DMA support
[linux/fpc-iii.git] / kernel / trace / trace_probe.c
blob8f8411e7835fdc0e138924c8ab5c35683894774e
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 static 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;
162 int len;
164 slash = strchr(event, '/');
165 if (slash) {
166 if (slash == event) {
167 pr_info("Group name is not specified\n");
168 return -EINVAL;
170 if (slash - event + 1 > MAX_EVENT_NAME_LEN) {
171 pr_info("Group name is too long\n");
172 return -E2BIG;
174 strlcpy(buf, event, slash - event + 1);
175 if (!is_good_name(buf)) {
176 pr_info("Group name must follow the same rules as C identifiers\n");
177 return -EINVAL;
179 *pgroup = buf;
180 *pevent = slash + 1;
181 event = *pevent;
183 len = strlen(event);
184 if (len == 0) {
185 pr_info("Event name is not specified\n");
186 return -EINVAL;
187 } else if (len > MAX_EVENT_NAME_LEN) {
188 pr_info("Event name is too long\n");
189 return -E2BIG;
191 if (!is_good_name(event)) {
192 pr_info("Event name must follow the same rules as C identifiers\n");
193 return -EINVAL;
195 return 0;
198 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
200 static int parse_probe_vars(char *arg, const struct fetch_type *t,
201 struct fetch_insn *code, unsigned int flags)
203 unsigned long param;
204 int ret = 0;
205 int len;
207 if (strcmp(arg, "retval") == 0) {
208 if (flags & TPARG_FL_RETURN)
209 code->op = FETCH_OP_RETVAL;
210 else
211 ret = -EINVAL;
212 } else if ((len = str_has_prefix(arg, "stack"))) {
213 if (arg[len] == '\0') {
214 code->op = FETCH_OP_STACKP;
215 } else if (isdigit(arg[len])) {
216 ret = kstrtoul(arg + len, 10, &param);
217 if (ret || ((flags & TPARG_FL_KERNEL) &&
218 param > PARAM_MAX_STACK))
219 ret = -EINVAL;
220 else {
221 code->op = FETCH_OP_STACK;
222 code->param = (unsigned int)param;
224 } else
225 ret = -EINVAL;
226 } else if (strcmp(arg, "comm") == 0) {
227 code->op = FETCH_OP_COMM;
228 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
229 } else if (((flags & TPARG_FL_MASK) ==
230 (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) &&
231 (len = str_has_prefix(arg, "arg"))) {
232 if (!isdigit(arg[len]))
233 return -EINVAL;
234 ret = kstrtoul(arg + len, 10, &param);
235 if (ret || !param || param > PARAM_MAX_STACK)
236 return -EINVAL;
237 code->op = FETCH_OP_ARG;
238 code->param = (unsigned int)param - 1;
239 #endif
240 } else
241 ret = -EINVAL;
243 return ret;
246 /* Recursive argument parser */
247 static int
248 parse_probe_arg(char *arg, const struct fetch_type *type,
249 struct fetch_insn **pcode, struct fetch_insn *end,
250 unsigned int flags)
252 struct fetch_insn *code = *pcode;
253 unsigned long param;
254 long offset = 0;
255 char *tmp;
256 int ret = 0;
258 switch (arg[0]) {
259 case '$':
260 ret = parse_probe_vars(arg + 1, type, code, flags);
261 break;
263 case '%': /* named register */
264 ret = regs_query_register_offset(arg + 1);
265 if (ret >= 0) {
266 code->op = FETCH_OP_REG;
267 code->param = (unsigned int)ret;
268 ret = 0;
270 break;
272 case '@': /* memory, file-offset or symbol */
273 if (isdigit(arg[1])) {
274 ret = kstrtoul(arg + 1, 0, &param);
275 if (ret)
276 break;
277 /* load address */
278 code->op = FETCH_OP_IMM;
279 code->immediate = param;
280 } else if (arg[1] == '+') {
281 /* kprobes don't support file offsets */
282 if (flags & TPARG_FL_KERNEL)
283 return -EINVAL;
285 ret = kstrtol(arg + 2, 0, &offset);
286 if (ret)
287 break;
289 code->op = FETCH_OP_FOFFS;
290 code->immediate = (unsigned long)offset; // imm64?
291 } else {
292 /* uprobes don't support symbols */
293 if (!(flags & TPARG_FL_KERNEL))
294 return -EINVAL;
296 /* Preserve symbol for updating */
297 code->op = FETCH_NOP_SYMBOL;
298 code->data = kstrdup(arg + 1, GFP_KERNEL);
299 if (!code->data)
300 return -ENOMEM;
301 if (++code == end)
302 return -E2BIG;
304 code->op = FETCH_OP_IMM;
305 code->immediate = 0;
307 /* These are fetching from memory */
308 if (++code == end)
309 return -E2BIG;
310 *pcode = code;
311 code->op = FETCH_OP_DEREF;
312 code->offset = offset;
313 break;
315 case '+': /* deref memory */
316 arg++; /* Skip '+', because kstrtol() rejects it. */
317 /* fall through */
318 case '-':
319 tmp = strchr(arg, '(');
320 if (!tmp)
321 return -EINVAL;
323 *tmp = '\0';
324 ret = kstrtol(arg, 0, &offset);
325 if (ret)
326 break;
328 arg = tmp + 1;
329 tmp = strrchr(arg, ')');
331 if (tmp) {
332 const struct fetch_type *t2 = find_fetch_type(NULL);
334 *tmp = '\0';
335 ret = parse_probe_arg(arg, t2, &code, end, flags);
336 if (ret)
337 break;
338 if (code->op == FETCH_OP_COMM)
339 return -EINVAL;
340 if (++code == end)
341 return -E2BIG;
342 *pcode = code;
344 code->op = FETCH_OP_DEREF;
345 code->offset = offset;
347 break;
349 if (!ret && code->op == FETCH_OP_NOP) {
350 /* Parsed, but do not find fetch method */
351 ret = -EINVAL;
353 return ret;
356 #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
358 /* Bitfield type needs to be parsed into a fetch function */
359 static int __parse_bitfield_probe_arg(const char *bf,
360 const struct fetch_type *t,
361 struct fetch_insn **pcode)
363 struct fetch_insn *code = *pcode;
364 unsigned long bw, bo;
365 char *tail;
367 if (*bf != 'b')
368 return 0;
370 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
372 if (bw == 0 || *tail != '@')
373 return -EINVAL;
375 bf = tail + 1;
376 bo = simple_strtoul(bf, &tail, 0);
378 if (tail == bf || *tail != '/')
379 return -EINVAL;
380 code++;
381 if (code->op != FETCH_OP_NOP)
382 return -E2BIG;
383 *pcode = code;
385 code->op = FETCH_OP_MOD_BF;
386 code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
387 code->rshift = BYTES_TO_BITS(t->size) - bw;
388 code->basesize = t->size;
390 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
393 /* String length checking wrapper */
394 static int traceprobe_parse_probe_arg_body(char *arg, ssize_t *size,
395 struct probe_arg *parg, unsigned int flags)
397 struct fetch_insn *code, *scode, *tmp = NULL;
398 char *t, *t2;
399 int ret, len;
401 if (strlen(arg) > MAX_ARGSTR_LEN) {
402 pr_info("Argument is too long.: %s\n", arg);
403 return -ENOSPC;
405 parg->comm = kstrdup(arg, GFP_KERNEL);
406 if (!parg->comm) {
407 pr_info("Failed to allocate memory for command '%s'.\n", arg);
408 return -ENOMEM;
410 t = strchr(arg, ':');
411 if (t) {
412 *t = '\0';
413 t2 = strchr(++t, '[');
414 if (t2) {
415 *t2 = '\0';
416 parg->count = simple_strtoul(t2 + 1, &t2, 0);
417 if (strcmp(t2, "]") || parg->count == 0)
418 return -EINVAL;
419 if (parg->count > MAX_ARRAY_LEN)
420 return -E2BIG;
424 * The default type of $comm should be "string", and it can't be
425 * dereferenced.
427 if (!t && strcmp(arg, "$comm") == 0)
428 parg->type = find_fetch_type("string");
429 else
430 parg->type = find_fetch_type(t);
431 if (!parg->type) {
432 pr_info("Unsupported type: %s\n", t);
433 return -EINVAL;
435 parg->offset = *size;
436 *size += parg->type->size * (parg->count ?: 1);
438 if (parg->count) {
439 len = strlen(parg->type->fmttype) + 6;
440 parg->fmt = kmalloc(len, GFP_KERNEL);
441 if (!parg->fmt)
442 return -ENOMEM;
443 snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
444 parg->count);
447 code = tmp = kzalloc(sizeof(*code) * FETCH_INSN_MAX, GFP_KERNEL);
448 if (!code)
449 return -ENOMEM;
450 code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
452 ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
453 flags);
454 if (ret)
455 goto fail;
457 /* Store operation */
458 if (!strcmp(parg->type->name, "string")) {
459 if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_IMM &&
460 code->op != FETCH_OP_COMM) {
461 pr_info("string only accepts memory or address.\n");
462 ret = -EINVAL;
463 goto fail;
465 if (code->op != FETCH_OP_DEREF || parg->count) {
467 * IMM and COMM is pointing actual address, those must
468 * be kept, and if parg->count != 0, this is an array
469 * of string pointers instead of string address itself.
471 code++;
472 if (code->op != FETCH_OP_NOP) {
473 ret = -E2BIG;
474 goto fail;
477 code->op = FETCH_OP_ST_STRING; /* In DEREF case, replace it */
478 code->size = parg->type->size;
479 parg->dynamic = true;
480 } else if (code->op == FETCH_OP_DEREF) {
481 code->op = FETCH_OP_ST_MEM;
482 code->size = parg->type->size;
483 } else {
484 code++;
485 if (code->op != FETCH_OP_NOP) {
486 ret = -E2BIG;
487 goto fail;
489 code->op = FETCH_OP_ST_RAW;
490 code->size = parg->type->size;
492 scode = code;
493 /* Modify operation */
494 if (t != NULL) {
495 ret = __parse_bitfield_probe_arg(t, parg->type, &code);
496 if (ret)
497 goto fail;
499 /* Loop(Array) operation */
500 if (parg->count) {
501 if (scode->op != FETCH_OP_ST_MEM &&
502 scode->op != FETCH_OP_ST_STRING) {
503 pr_info("array only accepts memory or address\n");
504 ret = -EINVAL;
505 goto fail;
507 code++;
508 if (code->op != FETCH_OP_NOP) {
509 ret = -E2BIG;
510 goto fail;
512 code->op = FETCH_OP_LP_ARRAY;
513 code->param = parg->count;
515 code++;
516 code->op = FETCH_OP_END;
518 /* Shrink down the code buffer */
519 parg->code = kzalloc(sizeof(*code) * (code - tmp + 1), GFP_KERNEL);
520 if (!parg->code)
521 ret = -ENOMEM;
522 else
523 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
525 fail:
526 if (ret) {
527 for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
528 if (code->op == FETCH_NOP_SYMBOL)
529 kfree(code->data);
531 kfree(tmp);
533 return ret;
536 /* Return 1 if name is reserved or already used by another argument */
537 static int traceprobe_conflict_field_name(const char *name,
538 struct probe_arg *args, int narg)
540 int i;
542 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
543 if (strcmp(reserved_field_names[i], name) == 0)
544 return 1;
546 for (i = 0; i < narg; i++)
547 if (strcmp(args[i].name, name) == 0)
548 return 1;
550 return 0;
553 int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, char *arg,
554 unsigned int flags)
556 struct probe_arg *parg = &tp->args[i];
557 char *body;
558 int ret;
560 /* Increment count for freeing args in error case */
561 tp->nr_args++;
563 body = strchr(arg, '=');
564 if (body) {
565 if (body - arg > MAX_ARG_NAME_LEN || body == arg)
566 return -EINVAL;
567 parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
568 body++;
569 } else {
570 /* If argument name is omitted, set "argN" */
571 parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1);
572 body = arg;
574 if (!parg->name)
575 return -ENOMEM;
577 if (!is_good_name(parg->name)) {
578 pr_info("Invalid argument[%d] name: %s\n",
579 i, parg->name);
580 return -EINVAL;
583 if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
584 pr_info("Argument[%d]: '%s' conflicts with another field.\n",
585 i, parg->name);
586 return -EINVAL;
589 /* Parse fetch argument */
590 ret = traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags);
591 if (ret)
592 pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
593 return ret;
596 void traceprobe_free_probe_arg(struct probe_arg *arg)
598 struct fetch_insn *code = arg->code;
600 while (code && code->op != FETCH_OP_END) {
601 if (code->op == FETCH_NOP_SYMBOL)
602 kfree(code->data);
603 code++;
605 kfree(arg->code);
606 kfree(arg->name);
607 kfree(arg->comm);
608 kfree(arg->fmt);
611 int traceprobe_update_arg(struct probe_arg *arg)
613 struct fetch_insn *code = arg->code;
614 long offset;
615 char *tmp;
616 char c;
617 int ret = 0;
619 while (code && code->op != FETCH_OP_END) {
620 if (code->op == FETCH_NOP_SYMBOL) {
621 if (code[1].op != FETCH_OP_IMM)
622 return -EINVAL;
624 tmp = strpbrk(code->data, "+-");
625 if (tmp)
626 c = *tmp;
627 ret = traceprobe_split_symbol_offset(code->data,
628 &offset);
629 if (ret)
630 return ret;
632 code[1].immediate =
633 (unsigned long)kallsyms_lookup_name(code->data);
634 if (tmp)
635 *tmp = c;
636 if (!code[1].immediate)
637 return -ENOENT;
638 code[1].immediate += offset;
640 code++;
642 return 0;
645 /* When len=0, we just calculate the needed length */
646 #define LEN_OR_ZERO (len ? len - pos : 0)
647 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
648 bool is_return)
650 struct probe_arg *parg;
651 int i, j;
652 int pos = 0;
653 const char *fmt, *arg;
655 if (!is_return) {
656 fmt = "(%lx)";
657 arg = "REC->" FIELD_STRING_IP;
658 } else {
659 fmt = "(%lx <- %lx)";
660 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
663 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
665 for (i = 0; i < tp->nr_args; i++) {
666 parg = tp->args + i;
667 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
668 if (parg->count) {
669 pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
670 parg->type->fmt);
671 for (j = 1; j < parg->count; j++)
672 pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
673 parg->type->fmt);
674 pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
675 } else
676 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
677 parg->type->fmt);
680 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
682 for (i = 0; i < tp->nr_args; i++) {
683 parg = tp->args + i;
684 if (parg->count) {
685 if (strcmp(parg->type->name, "string") == 0)
686 fmt = ", __get_str(%s[%d])";
687 else
688 fmt = ", REC->%s[%d]";
689 for (j = 0; j < parg->count; j++)
690 pos += snprintf(buf + pos, LEN_OR_ZERO,
691 fmt, parg->name, j);
692 } else {
693 if (strcmp(parg->type->name, "string") == 0)
694 fmt = ", __get_str(%s)";
695 else
696 fmt = ", REC->%s";
697 pos += snprintf(buf + pos, LEN_OR_ZERO,
698 fmt, parg->name);
702 /* return the length of print_fmt */
703 return pos;
705 #undef LEN_OR_ZERO
707 int traceprobe_set_print_fmt(struct trace_probe *tp, bool is_return)
709 int len;
710 char *print_fmt;
712 /* First: called with 0 length to calculate the needed length */
713 len = __set_print_fmt(tp, NULL, 0, is_return);
714 print_fmt = kmalloc(len + 1, GFP_KERNEL);
715 if (!print_fmt)
716 return -ENOMEM;
718 /* Second: actually write the @print_fmt */
719 __set_print_fmt(tp, print_fmt, len + 1, is_return);
720 tp->call.print_fmt = print_fmt;
722 return 0;
725 int traceprobe_define_arg_fields(struct trace_event_call *event_call,
726 size_t offset, struct trace_probe *tp)
728 int ret, i;
730 /* Set argument names as fields */
731 for (i = 0; i < tp->nr_args; i++) {
732 struct probe_arg *parg = &tp->args[i];
733 const char *fmt = parg->type->fmttype;
734 int size = parg->type->size;
736 if (parg->fmt)
737 fmt = parg->fmt;
738 if (parg->count)
739 size *= parg->count;
740 ret = trace_define_field(event_call, fmt, parg->name,
741 offset + parg->offset, size,
742 parg->type->is_signed,
743 FILTER_OTHER);
744 if (ret)
745 return ret;
747 return 0;