1 // SPDX-License-Identifier: GPL-2.0
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 <linux/bpf.h>
16 #include "trace_probe.h"
21 static const char *trace_probe_err_text[] = { ERRORS };
23 static const char *reserved_field_names[] = {
26 "common_preempt_count",
34 /* Printing in basic type function template */
35 #define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt) \
36 int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, void *data, void *ent)\
38 trace_seq_printf(s, fmt, *(type *)data); \
39 return !trace_seq_has_overflowed(s); \
41 const char PRINT_TYPE_FMT_NAME(tname)[] = fmt;
43 DEFINE_BASIC_PRINT_TYPE_FUNC(u8, u8, "%u")
44 DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u")
45 DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u")
46 DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu")
47 DEFINE_BASIC_PRINT_TYPE_FUNC(s8, s8, "%d")
48 DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d")
49 DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d")
50 DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld")
51 DEFINE_BASIC_PRINT_TYPE_FUNC(x8, u8, "0x%x")
52 DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x")
53 DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x")
54 DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx")
55 DEFINE_BASIC_PRINT_TYPE_FUNC(char, u8, "'%c'")
57 int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent)
59 trace_seq_printf(s, "%pS", (void *)*(unsigned long *)data);
60 return !trace_seq_has_overflowed(s);
62 const char PRINT_TYPE_FMT_NAME(symbol)[] = "%pS";
64 /* Print type function for string type */
65 int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, void *data, void *ent)
67 int len = *(u32 *)data >> 16;
70 trace_seq_puts(s, "(fault)");
72 trace_seq_printf(s, "\"%s\"",
73 (const char *)get_loc_data(data, ent));
74 return !trace_seq_has_overflowed(s);
77 const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
79 /* Fetch type information table */
80 static const struct fetch_type probe_fetch_types[] = {
82 __ASSIGN_FETCH_TYPE("string", string, string, sizeof(u32), 1, 1,
84 __ASSIGN_FETCH_TYPE("ustring", string, string, sizeof(u32), 1, 1,
86 __ASSIGN_FETCH_TYPE("symstr", string, string, sizeof(u32), 1, 1,
89 ASSIGN_FETCH_TYPE(u8, u8, 0),
90 ASSIGN_FETCH_TYPE(u16, u16, 0),
91 ASSIGN_FETCH_TYPE(u32, u32, 0),
92 ASSIGN_FETCH_TYPE(u64, u64, 0),
93 ASSIGN_FETCH_TYPE(s8, u8, 1),
94 ASSIGN_FETCH_TYPE(s16, u16, 1),
95 ASSIGN_FETCH_TYPE(s32, u32, 1),
96 ASSIGN_FETCH_TYPE(s64, u64, 1),
97 ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0),
98 ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
99 ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
100 ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
101 ASSIGN_FETCH_TYPE_ALIAS(char, u8, u8, 0),
102 ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0),
104 ASSIGN_FETCH_TYPE_END
107 static const struct fetch_type *find_fetch_type(const char *type, unsigned long flags)
111 /* Reject the symbol/symstr for uprobes */
112 if (type && (flags & TPARG_FL_USER) &&
113 (!strcmp(type, "symbol") || !strcmp(type, "symstr")))
117 type = DEFAULT_FETCH_TYPE_STR;
119 /* Special case: bitfield */
123 type = strchr(type, '/');
128 if (kstrtoul(type, 0, &bs))
133 return find_fetch_type("u8", flags);
135 return find_fetch_type("u16", flags);
137 return find_fetch_type("u32", flags);
139 return find_fetch_type("u64", flags);
145 for (i = 0; probe_fetch_types[i].name; i++) {
146 if (strcmp(type, probe_fetch_types[i].name) == 0)
147 return &probe_fetch_types[i];
154 static struct trace_probe_log trace_probe_log;
156 void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
158 trace_probe_log.subsystem = subsystem;
159 trace_probe_log.argc = argc;
160 trace_probe_log.argv = argv;
161 trace_probe_log.index = 0;
164 void trace_probe_log_clear(void)
166 memset(&trace_probe_log, 0, sizeof(trace_probe_log));
169 void trace_probe_log_set_index(int index)
171 trace_probe_log.index = index;
174 void __trace_probe_log_err(int offset, int err_type)
177 int i, len = 0, pos = 0;
179 if (!trace_probe_log.argv)
182 /* Recalculate the length and allocate buffer */
183 for (i = 0; i < trace_probe_log.argc; i++) {
184 if (i == trace_probe_log.index)
186 len += strlen(trace_probe_log.argv[i]) + 1;
188 command = kzalloc(len, GFP_KERNEL);
192 if (trace_probe_log.index >= trace_probe_log.argc) {
194 * Set the error position is next to the last arg + space.
195 * Note that len includes the terminal null and the cursor
196 * appears at pos + 1.
202 /* And make a command string from argv array */
204 for (i = 0; i < trace_probe_log.argc; i++) {
205 len = strlen(trace_probe_log.argv[i]);
206 strcpy(p, trace_probe_log.argv[i]);
212 tracing_log_err(NULL, trace_probe_log.subsystem, command,
213 trace_probe_err_text, err_type, pos + offset);
218 /* Split symbol and offset. */
219 int traceprobe_split_symbol_offset(char *symbol, long *offset)
227 tmp = strpbrk(symbol, "+-");
229 ret = kstrtol(tmp, 0, offset);
239 /* @buf must has MAX_EVENT_NAME_LEN size */
240 int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
241 char *buf, int offset)
243 const char *slash, *event = *pevent;
246 slash = strchr(event, '/');
248 slash = strchr(event, '.');
251 if (slash == event) {
252 trace_probe_log_err(offset, NO_GROUP_NAME);
255 if (slash - event + 1 > MAX_EVENT_NAME_LEN) {
256 trace_probe_log_err(offset, GROUP_TOO_LONG);
259 strscpy(buf, event, slash - event + 1);
260 if (!is_good_system_name(buf)) {
261 trace_probe_log_err(offset, BAD_GROUP_NAME);
266 offset += slash - event + 1;
275 trace_probe_log_err(offset, NO_EVENT_NAME);
277 } else if (len > MAX_EVENT_NAME_LEN) {
278 trace_probe_log_err(offset, EVENT_TOO_LONG);
281 if (!is_good_name(event)) {
282 trace_probe_log_err(offset, BAD_EVENT_NAME);
288 static int parse_trace_event_arg(char *arg, struct fetch_insn *code,
289 struct traceprobe_parse_context *ctx)
291 struct ftrace_event_field *field;
292 struct list_head *head;
294 head = trace_get_fields(ctx->event);
295 list_for_each_entry(field, head, link) {
296 if (!strcmp(arg, field->name)) {
297 code->op = FETCH_OP_TP_ARG;
305 #ifdef CONFIG_PROBE_EVENTS_BTF_ARGS
307 static struct btf *traceprobe_get_btf(void)
309 struct btf *btf = bpf_get_btf_vmlinux();
311 if (IS_ERR_OR_NULL(btf))
317 static u32 btf_type_int(const struct btf_type *t)
319 return *(u32 *)(t + 1);
322 static const char *type_from_btf_id(struct btf *btf, s32 id)
324 const struct btf_type *t;
328 /* TODO: const char * could be converted as a string */
329 t = btf_type_skip_modifiers(btf, id, &tid);
331 switch (BTF_INFO_KIND(t->info)) {
333 /* enum is "int", so convert to "s32" */
335 case BTF_KIND_ENUM64:
338 /* pointer will be converted to "x??" */
339 if (IS_ENABLED(CONFIG_64BIT))
344 intdata = btf_type_int(t);
345 if (BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED) {
346 switch (BTF_INT_BITS(intdata)) {
356 } else { /* unsigned */
357 switch (BTF_INT_BITS(intdata)) {
369 /* TODO: support other types */
374 static const struct btf_type *find_btf_func_proto(const char *funcname)
376 struct btf *btf = traceprobe_get_btf();
377 const struct btf_type *t;
380 if (!btf || !funcname)
381 return ERR_PTR(-EINVAL);
383 id = btf_find_by_name_kind(btf, funcname, BTF_KIND_FUNC);
385 return ERR_PTR(-ENOENT);
387 /* Get BTF_KIND_FUNC type */
388 t = btf_type_by_id(btf, id);
389 if (!btf_type_is_func(t))
390 return ERR_PTR(-ENOENT);
392 /* The type of BTF_KIND_FUNC is BTF_KIND_FUNC_PROTO */
393 t = btf_type_by_id(btf, t->type);
394 if (!btf_type_is_func_proto(t))
395 return ERR_PTR(-ENOENT);
400 static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr,
403 const struct btf_param *param;
404 const struct btf_type *t;
406 if (!funcname || !nr)
407 return ERR_PTR(-EINVAL);
409 t = find_btf_func_proto(funcname);
411 return (const struct btf_param *)t;
413 *nr = btf_type_vlen(t);
414 param = (const struct btf_param *)(t + 1);
416 /* Hide the first 'data' argument of tracepoint */
428 static int parse_btf_arg(const char *varname, struct fetch_insn *code,
429 struct traceprobe_parse_context *ctx)
431 struct btf *btf = traceprobe_get_btf();
432 const struct btf_param *params;
436 trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
440 if (WARN_ON_ONCE(!ctx->funcname))
444 params = find_btf_func_param(ctx->funcname, &ctx->nr_params,
445 ctx->flags & TPARG_FL_TPOINT);
446 if (IS_ERR(params)) {
447 trace_probe_log_err(ctx->offset, NO_BTF_ENTRY);
448 return PTR_ERR(params);
450 ctx->params = params;
452 params = ctx->params;
454 for (i = 0; i < ctx->nr_params; i++) {
455 const char *name = btf_name_by_offset(btf, params[i].name_off);
457 if (name && !strcmp(name, varname)) {
458 code->op = FETCH_OP_ARG;
459 if (ctx->flags & TPARG_FL_TPOINT)
466 trace_probe_log_err(ctx->offset, NO_BTFARG);
470 static const struct fetch_type *parse_btf_arg_type(int arg_idx,
471 struct traceprobe_parse_context *ctx)
473 struct btf *btf = traceprobe_get_btf();
474 const char *typestr = NULL;
476 if (btf && ctx->params) {
477 if (ctx->flags & TPARG_FL_TPOINT)
479 typestr = type_from_btf_id(btf, ctx->params[arg_idx].type);
482 return find_fetch_type(typestr, ctx->flags);
485 static const struct fetch_type *parse_btf_retval_type(
486 struct traceprobe_parse_context *ctx)
488 struct btf *btf = traceprobe_get_btf();
489 const char *typestr = NULL;
490 const struct btf_type *t;
492 if (btf && ctx->funcname) {
493 t = find_btf_func_proto(ctx->funcname);
495 typestr = type_from_btf_id(btf, t->type);
498 return find_fetch_type(typestr, ctx->flags);
501 static bool is_btf_retval_void(const char *funcname)
503 const struct btf_type *t;
505 t = find_btf_func_proto(funcname);
512 static struct btf *traceprobe_get_btf(void)
517 static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr,
520 return ERR_PTR(-EOPNOTSUPP);
523 static int parse_btf_arg(const char *varname, struct fetch_insn *code,
524 struct traceprobe_parse_context *ctx)
526 trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
530 #define parse_btf_arg_type(idx, ctx) \
531 find_fetch_type(NULL, ctx->flags)
533 #define parse_btf_retval_type(ctx) \
534 find_fetch_type(NULL, ctx->flags)
536 #define is_btf_retval_void(funcname) (false)
540 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
542 static int parse_probe_vars(char *arg, const struct fetch_type *t,
543 struct fetch_insn *code,
544 struct traceprobe_parse_context *ctx)
547 int err = TP_ERR_BAD_VAR;
551 if (ctx->flags & TPARG_FL_TEVENT) {
554 ret = parse_trace_event_arg(arg, code, ctx);
557 if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
558 code->op = FETCH_OP_COMM;
561 /* backward compatibility */
566 if (strcmp(arg, "retval") == 0) {
567 if (ctx->flags & TPARG_FL_RETURN) {
568 if ((ctx->flags & TPARG_FL_KERNEL) &&
569 is_btf_retval_void(ctx->funcname)) {
570 err = TP_ERR_NO_RETVAL;
573 code->op = FETCH_OP_RETVAL;
576 err = TP_ERR_RETVAL_ON_PROBE;
580 len = str_has_prefix(arg, "stack");
583 if (arg[len] == '\0') {
584 code->op = FETCH_OP_STACKP;
588 if (isdigit(arg[len])) {
589 ret = kstrtoul(arg + len, 10, ¶m);
593 if ((ctx->flags & TPARG_FL_KERNEL) &&
594 param > PARAM_MAX_STACK) {
595 err = TP_ERR_BAD_STACK_NUM;
598 code->op = FETCH_OP_STACK;
599 code->param = (unsigned int)param;
605 if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
606 code->op = FETCH_OP_COMM;
610 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
611 len = str_has_prefix(arg, "arg");
612 if (len && tparg_is_function_entry(ctx->flags)) {
613 ret = kstrtoul(arg + len, 10, ¶m);
617 if (!param || param > PARAM_MAX_STACK) {
618 err = TP_ERR_BAD_ARG_NUM;
622 code->op = FETCH_OP_ARG;
623 code->param = (unsigned int)param - 1;
625 * The tracepoint probe will probe a stub function, and the
626 * first parameter of the stub is a dummy and should be ignored.
628 if (ctx->flags & TPARG_FL_TPOINT)
635 __trace_probe_log_err(ctx->offset, err);
639 static int str_to_immediate(char *str, unsigned long *imm)
642 return kstrtoul(str, 0, imm);
643 else if (str[0] == '-')
644 return kstrtol(str, 0, (long *)imm);
645 else if (str[0] == '+')
646 return kstrtol(str + 1, 0, (long *)imm);
650 static int __parse_imm_string(char *str, char **pbuf, int offs)
652 size_t len = strlen(str);
654 if (str[len - 1] != '"') {
655 trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE);
658 *pbuf = kstrndup(str, len - 1, GFP_KERNEL);
664 /* Recursive argument parser */
666 parse_probe_arg(char *arg, const struct fetch_type *type,
667 struct fetch_insn **pcode, struct fetch_insn *end,
668 struct traceprobe_parse_context *ctx)
670 struct fetch_insn *code = *pcode;
672 int deref = FETCH_OP_DEREF;
679 ret = parse_probe_vars(arg + 1, type, code, ctx);
682 case '%': /* named register */
683 if (ctx->flags & (TPARG_FL_TEVENT | TPARG_FL_FPROBE)) {
684 /* eprobe and fprobe do not handle registers */
685 trace_probe_log_err(ctx->offset, BAD_VAR);
688 ret = regs_query_register_offset(arg + 1);
690 code->op = FETCH_OP_REG;
691 code->param = (unsigned int)ret;
694 trace_probe_log_err(ctx->offset, BAD_REG_NAME);
697 case '@': /* memory, file-offset or symbol */
698 if (isdigit(arg[1])) {
699 ret = kstrtoul(arg + 1, 0, ¶m);
701 trace_probe_log_err(ctx->offset, BAD_MEM_ADDR);
705 code->op = FETCH_OP_IMM;
706 code->immediate = param;
707 } else if (arg[1] == '+') {
708 /* kprobes don't support file offsets */
709 if (ctx->flags & TPARG_FL_KERNEL) {
710 trace_probe_log_err(ctx->offset, FILE_ON_KPROBE);
713 ret = kstrtol(arg + 2, 0, &offset);
715 trace_probe_log_err(ctx->offset, BAD_FILE_OFFS);
719 code->op = FETCH_OP_FOFFS;
720 code->immediate = (unsigned long)offset; // imm64?
722 /* uprobes don't support symbols */
723 if (!(ctx->flags & TPARG_FL_KERNEL)) {
724 trace_probe_log_err(ctx->offset, SYM_ON_UPROBE);
727 /* Preserve symbol for updating */
728 code->op = FETCH_NOP_SYMBOL;
729 code->data = kstrdup(arg + 1, GFP_KERNEL);
733 trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
736 code->op = FETCH_OP_IMM;
739 /* These are fetching from memory */
741 trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
745 code->op = FETCH_OP_DEREF;
746 code->offset = offset;
749 case '+': /* deref memory */
752 deref = FETCH_OP_UDEREF;
757 arg++; /* Skip '+', because kstrtol() rejects it. */
758 tmp = strchr(arg, '(');
760 trace_probe_log_err(ctx->offset, DEREF_NEED_BRACE);
764 ret = kstrtol(arg, 0, &offset);
766 trace_probe_log_err(ctx->offset, BAD_DEREF_OFFS);
769 ctx->offset += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0);
771 tmp = strrchr(arg, ')');
773 trace_probe_log_err(ctx->offset + strlen(arg),
777 const struct fetch_type *t2 = find_fetch_type(NULL, ctx->flags);
778 int cur_offs = ctx->offset;
781 ret = parse_probe_arg(arg, t2, &code, end, ctx);
784 ctx->offset = cur_offs;
785 if (code->op == FETCH_OP_COMM ||
786 code->op == FETCH_OP_DATA) {
787 trace_probe_log_err(ctx->offset, COMM_CANT_DEREF);
791 trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
797 code->offset = offset;
800 case '\\': /* Immediate value */
801 if (arg[1] == '"') { /* Immediate string */
802 ret = __parse_imm_string(arg + 2, &tmp, ctx->offset + 2);
805 code->op = FETCH_OP_DATA;
808 ret = str_to_immediate(arg + 1, &code->immediate);
810 trace_probe_log_err(ctx->offset + 1, BAD_IMM);
812 code->op = FETCH_OP_IMM;
816 if (isalpha(arg[0]) || arg[0] == '_') { /* BTF variable */
817 if (!tparg_is_function_entry(ctx->flags)) {
818 trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
821 ret = parse_btf_arg(arg, code, ctx);
825 if (!ret && code->op == FETCH_OP_NOP) {
826 /* Parsed, but do not find fetch method */
827 trace_probe_log_err(ctx->offset, BAD_FETCH_ARG);
833 #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
835 /* Bitfield type needs to be parsed into a fetch function */
836 static int __parse_bitfield_probe_arg(const char *bf,
837 const struct fetch_type *t,
838 struct fetch_insn **pcode)
840 struct fetch_insn *code = *pcode;
841 unsigned long bw, bo;
847 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
849 if (bw == 0 || *tail != '@')
853 bo = simple_strtoul(bf, &tail, 0);
855 if (tail == bf || *tail != '/')
858 if (code->op != FETCH_OP_NOP)
862 code->op = FETCH_OP_MOD_BF;
863 code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
864 code->rshift = BYTES_TO_BITS(t->size) - bw;
865 code->basesize = t->size;
867 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
870 /* String length checking wrapper */
871 static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size,
872 struct probe_arg *parg,
873 struct traceprobe_parse_context *ctx)
875 struct fetch_insn *code, *scode, *tmp = NULL;
880 arg = kstrdup(argv, GFP_KERNEL);
886 if (len > MAX_ARGSTR_LEN) {
887 trace_probe_log_err(ctx->offset, ARG_TOO_LONG);
889 } else if (len == 0) {
890 trace_probe_log_err(ctx->offset, NO_ARG_BODY);
895 parg->comm = kstrdup(arg, GFP_KERNEL);
900 t = strchr(arg, ':');
903 t2 = strchr(++t, '[');
906 t3 = strchr(t2, ']');
908 int offs = t2 + strlen(t2) - arg;
910 trace_probe_log_err(ctx->offset + offs,
913 } else if (t3[1] != '\0') {
914 trace_probe_log_err(ctx->offset + t3 + 1 - arg,
919 if (kstrtouint(t2, 0, &parg->count) || !parg->count) {
920 trace_probe_log_err(ctx->offset + t2 - arg,
924 if (parg->count > MAX_ARRAY_LEN) {
925 trace_probe_log_err(ctx->offset + t2 - arg,
933 * Since $comm and immediate string can not be dereferenced,
934 * we can find those by strcmp. But ignore for eprobes.
936 if (!(ctx->flags & TPARG_FL_TEVENT) &&
937 (strcmp(arg, "$comm") == 0 || strcmp(arg, "$COMM") == 0 ||
938 strncmp(arg, "\\\"", 2) == 0)) {
939 /* The type of $comm must be "string", and not an array. */
940 if (parg->count || (t && strcmp(t, "string")))
942 parg->type = find_fetch_type("string", ctx->flags);
944 parg->type = find_fetch_type(t, ctx->flags);
946 trace_probe_log_err(ctx->offset + (t ? (t - arg) : 0), BAD_TYPE);
949 parg->offset = *size;
950 *size += parg->type->size * (parg->count ?: 1);
954 len = strlen(parg->type->fmttype) + 6;
955 parg->fmt = kmalloc(len, GFP_KERNEL);
958 snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
962 code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL);
965 code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
967 ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
972 /* Update storing type if BTF is available */
973 if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) && !t) {
974 if (code->op == FETCH_OP_ARG)
975 parg->type = parse_btf_arg_type(code->param, ctx);
976 else if (code->op == FETCH_OP_RETVAL)
977 parg->type = parse_btf_retval_type(ctx);
981 /* Store operation */
982 if (parg->type->is_string) {
983 if (!strcmp(parg->type->name, "symstr")) {
984 if (code->op != FETCH_OP_REG && code->op != FETCH_OP_STACK &&
985 code->op != FETCH_OP_RETVAL && code->op != FETCH_OP_ARG &&
986 code->op != FETCH_OP_DEREF && code->op != FETCH_OP_TP_ARG) {
987 trace_probe_log_err(ctx->offset + (t ? (t - arg) : 0),
992 if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF &&
993 code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM &&
994 code->op != FETCH_OP_DATA && code->op != FETCH_OP_TP_ARG) {
995 trace_probe_log_err(ctx->offset + (t ? (t - arg) : 0),
1000 if (!strcmp(parg->type->name, "symstr") ||
1001 (code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM ||
1002 code->op == FETCH_OP_DATA) || code->op == FETCH_OP_TP_ARG ||
1005 * IMM, DATA and COMM is pointing actual address, those
1006 * must be kept, and if parg->count != 0, this is an
1007 * array of string pointers instead of string address
1009 * For the symstr, it doesn't need to dereference, thus
1010 * it just get the value.
1013 if (code->op != FETCH_OP_NOP) {
1014 trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1018 /* If op == DEREF, replace it with STRING */
1019 if (!strcmp(parg->type->name, "ustring") ||
1020 code->op == FETCH_OP_UDEREF)
1021 code->op = FETCH_OP_ST_USTRING;
1022 else if (!strcmp(parg->type->name, "symstr"))
1023 code->op = FETCH_OP_ST_SYMSTR;
1025 code->op = FETCH_OP_ST_STRING;
1026 code->size = parg->type->size;
1027 parg->dynamic = true;
1028 } else if (code->op == FETCH_OP_DEREF) {
1029 code->op = FETCH_OP_ST_MEM;
1030 code->size = parg->type->size;
1031 } else if (code->op == FETCH_OP_UDEREF) {
1032 code->op = FETCH_OP_ST_UMEM;
1033 code->size = parg->type->size;
1036 if (code->op != FETCH_OP_NOP) {
1037 trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1040 code->op = FETCH_OP_ST_RAW;
1041 code->size = parg->type->size;
1044 /* Modify operation */
1046 ret = __parse_bitfield_probe_arg(t, parg->type, &code);
1048 trace_probe_log_err(ctx->offset + t - arg, BAD_BITFIELD);
1053 /* Loop(Array) operation */
1055 if (scode->op != FETCH_OP_ST_MEM &&
1056 scode->op != FETCH_OP_ST_STRING &&
1057 scode->op != FETCH_OP_ST_USTRING) {
1058 trace_probe_log_err(ctx->offset + (t ? (t - arg) : 0),
1063 if (code->op != FETCH_OP_NOP) {
1064 trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1067 code->op = FETCH_OP_LP_ARRAY;
1068 code->param = parg->count;
1071 code->op = FETCH_OP_END;
1074 /* Shrink down the code buffer */
1075 parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL);
1079 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
1083 for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
1084 if (code->op == FETCH_NOP_SYMBOL ||
1085 code->op == FETCH_OP_DATA)
1095 /* Return 1 if name is reserved or already used by another argument */
1096 static int traceprobe_conflict_field_name(const char *name,
1097 struct probe_arg *args, int narg)
1101 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
1102 if (strcmp(reserved_field_names[i], name) == 0)
1105 for (i = 0; i < narg; i++)
1106 if (strcmp(args[i].name, name) == 0)
1112 static char *generate_probe_arg_name(const char *arg, int idx)
1118 * If argument name is omitted, try arg as a name (BTF variable)
1121 if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS)) {
1122 end = strchr(arg, ':');
1124 end = arg + strlen(arg);
1126 name = kmemdup_nul(arg, end - arg, GFP_KERNEL);
1127 if (!name || !is_good_name(name)) {
1134 name = kasprintf(GFP_KERNEL, "arg%d", idx + 1);
1139 int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg,
1140 struct traceprobe_parse_context *ctx)
1142 struct probe_arg *parg = &tp->args[i];
1145 /* Increment count for freeing args in error case */
1148 body = strchr(arg, '=');
1150 if (body - arg > MAX_ARG_NAME_LEN) {
1151 trace_probe_log_err(0, ARG_NAME_TOO_LONG);
1153 } else if (body == arg) {
1154 trace_probe_log_err(0, NO_ARG_NAME);
1157 parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
1160 parg->name = generate_probe_arg_name(arg, i);
1166 if (!is_good_name(parg->name)) {
1167 trace_probe_log_err(0, BAD_ARG_NAME);
1170 if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
1171 trace_probe_log_err(0, USED_ARG_NAME);
1174 ctx->offset = body - arg;
1175 /* Parse fetch argument */
1176 return traceprobe_parse_probe_arg_body(body, &tp->size, parg, ctx);
1179 void traceprobe_free_probe_arg(struct probe_arg *arg)
1181 struct fetch_insn *code = arg->code;
1183 while (code && code->op != FETCH_OP_END) {
1184 if (code->op == FETCH_NOP_SYMBOL ||
1185 code->op == FETCH_OP_DATA)
1195 static int argv_has_var_arg(int argc, const char *argv[], int *args_idx,
1196 struct traceprobe_parse_context *ctx)
1200 for (i = 0; i < argc; i++)
1201 if (str_has_prefix(argv[i], "$arg")) {
1202 trace_probe_log_set_index(i + 2);
1204 if (!tparg_is_function_entry(ctx->flags)) {
1205 trace_probe_log_err(0, NOFENTRY_ARGS);
1209 if (isdigit(argv[i][4])) {
1214 if (argv[i][4] != '*') {
1215 trace_probe_log_err(0, BAD_VAR);
1219 if (*args_idx >= 0 && *args_idx < argc) {
1220 trace_probe_log_err(0, DOUBLE_ARGS);
1230 static int sprint_nth_btf_arg(int idx, const char *type,
1231 char *buf, int bufsize,
1232 struct traceprobe_parse_context *ctx)
1234 struct btf *btf = traceprobe_get_btf();
1238 if (idx >= ctx->nr_params) {
1239 trace_probe_log_err(0, NO_BTFARG);
1242 name = btf_name_by_offset(btf, ctx->params[idx].name_off);
1244 trace_probe_log_err(0, NO_BTF_ENTRY);
1247 ret = snprintf(buf, bufsize, "%s%s", name, type);
1248 if (ret >= bufsize) {
1249 trace_probe_log_err(0, ARGS_2LONG);
1255 /* Return new_argv which must be freed after use */
1256 const char **traceprobe_expand_meta_args(int argc, const char *argv[],
1257 int *new_argc, char *buf, int bufsize,
1258 struct traceprobe_parse_context *ctx)
1260 const struct btf_param *params = NULL;
1261 int i, j, n, used, ret, args_idx = -1;
1262 const char **new_argv = NULL;
1265 ret = argv_has_var_arg(argc, argv, &args_idx, ctx);
1267 return ERR_PTR(ret);
1274 params = find_btf_func_param(ctx->funcname, &nr_params,
1275 ctx->flags & TPARG_FL_TPOINT);
1276 if (IS_ERR(params)) {
1277 if (args_idx != -1) {
1278 /* $arg* requires BTF info */
1279 trace_probe_log_err(0, NOSUP_BTFARG);
1280 return (const char **)params;
1285 ctx->params = params;
1286 ctx->nr_params = nr_params;
1289 *new_argc = argc + ctx->nr_params - 1;
1293 new_argv = kcalloc(*new_argc, sizeof(char *), GFP_KERNEL);
1295 return ERR_PTR(-ENOMEM);
1298 for (i = 0, j = 0; i < argc; i++) {
1299 trace_probe_log_set_index(i + 2);
1300 if (i == args_idx) {
1301 for (n = 0; n < nr_params; n++) {
1302 ret = sprint_nth_btf_arg(n, "", buf + used,
1303 bufsize - used, ctx);
1307 new_argv[j++] = buf + used;
1313 if (str_has_prefix(argv[i], "$arg")) {
1316 n = simple_strtoul(argv[i] + 4, &type, 10);
1317 if (type && !(*type == ':' || *type == '\0')) {
1318 trace_probe_log_err(0, BAD_VAR);
1322 /* Note: $argN starts from $arg1 */
1323 ret = sprint_nth_btf_arg(n - 1, type, buf + used,
1324 bufsize - used, ctx);
1327 new_argv[j++] = buf + used;
1330 new_argv[j++] = argv[i];
1337 return ERR_PTR(ret);
1340 int traceprobe_update_arg(struct probe_arg *arg)
1342 struct fetch_insn *code = arg->code;
1348 while (code && code->op != FETCH_OP_END) {
1349 if (code->op == FETCH_NOP_SYMBOL) {
1350 if (code[1].op != FETCH_OP_IMM)
1353 tmp = strpbrk(code->data, "+-");
1356 ret = traceprobe_split_symbol_offset(code->data,
1362 (unsigned long)kallsyms_lookup_name(code->data);
1365 if (!code[1].immediate)
1367 code[1].immediate += offset;
1374 /* When len=0, we just calculate the needed length */
1375 #define LEN_OR_ZERO (len ? len - pos : 0)
1376 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
1377 enum probe_print_type ptype)
1379 struct probe_arg *parg;
1382 const char *fmt, *arg;
1385 case PROBE_PRINT_NORMAL:
1387 arg = ", REC->" FIELD_STRING_IP;
1389 case PROBE_PRINT_RETURN:
1390 fmt = "(%lx <- %lx)";
1391 arg = ", REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
1393 case PROBE_PRINT_EVENT:
1402 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
1404 for (i = 0; i < tp->nr_args; i++) {
1405 parg = tp->args + i;
1406 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
1408 pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
1410 for (j = 1; j < parg->count; j++)
1411 pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
1413 pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
1415 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
1419 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", arg);
1421 for (i = 0; i < tp->nr_args; i++) {
1422 parg = tp->args + i;
1424 if (parg->type->is_string)
1425 fmt = ", __get_str(%s[%d])";
1427 fmt = ", REC->%s[%d]";
1428 for (j = 0; j < parg->count; j++)
1429 pos += snprintf(buf + pos, LEN_OR_ZERO,
1430 fmt, parg->name, j);
1432 if (parg->type->is_string)
1433 fmt = ", __get_str(%s)";
1436 pos += snprintf(buf + pos, LEN_OR_ZERO,
1441 /* return the length of print_fmt */
1446 int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype)
1448 struct trace_event_call *call = trace_probe_event_call(tp);
1452 /* First: called with 0 length to calculate the needed length */
1453 len = __set_print_fmt(tp, NULL, 0, ptype);
1454 print_fmt = kmalloc(len + 1, GFP_KERNEL);
1458 /* Second: actually write the @print_fmt */
1459 __set_print_fmt(tp, print_fmt, len + 1, ptype);
1460 call->print_fmt = print_fmt;
1465 int traceprobe_define_arg_fields(struct trace_event_call *event_call,
1466 size_t offset, struct trace_probe *tp)
1470 /* Set argument names as fields */
1471 for (i = 0; i < tp->nr_args; i++) {
1472 struct probe_arg *parg = &tp->args[i];
1473 const char *fmt = parg->type->fmttype;
1474 int size = parg->type->size;
1479 size *= parg->count;
1480 ret = trace_define_field(event_call, fmt, parg->name,
1481 offset + parg->offset, size,
1482 parg->type->is_signed,
1490 static void trace_probe_event_free(struct trace_probe_event *tpe)
1492 kfree(tpe->class.system);
1493 kfree(tpe->call.name);
1494 kfree(tpe->call.print_fmt);
1498 int trace_probe_append(struct trace_probe *tp, struct trace_probe *to)
1500 if (trace_probe_has_sibling(tp))
1503 list_del_init(&tp->list);
1504 trace_probe_event_free(tp->event);
1506 tp->event = to->event;
1507 list_add_tail(&tp->list, trace_probe_probe_list(to));
1512 void trace_probe_unlink(struct trace_probe *tp)
1514 list_del_init(&tp->list);
1515 if (list_empty(trace_probe_probe_list(tp)))
1516 trace_probe_event_free(tp->event);
1520 void trace_probe_cleanup(struct trace_probe *tp)
1524 for (i = 0; i < tp->nr_args; i++)
1525 traceprobe_free_probe_arg(&tp->args[i]);
1528 trace_probe_unlink(tp);
1531 int trace_probe_init(struct trace_probe *tp, const char *event,
1532 const char *group, bool alloc_filter)
1534 struct trace_event_call *call;
1535 size_t size = sizeof(struct trace_probe_event);
1538 if (!event || !group)
1542 size += sizeof(struct trace_uprobe_filter);
1544 tp->event = kzalloc(size, GFP_KERNEL);
1548 INIT_LIST_HEAD(&tp->event->files);
1549 INIT_LIST_HEAD(&tp->event->class.fields);
1550 INIT_LIST_HEAD(&tp->event->probes);
1551 INIT_LIST_HEAD(&tp->list);
1552 list_add(&tp->list, &tp->event->probes);
1554 call = trace_probe_event_call(tp);
1555 call->class = &tp->event->class;
1556 call->name = kstrdup(event, GFP_KERNEL);
1562 tp->event->class.system = kstrdup(group, GFP_KERNEL);
1563 if (!tp->event->class.system) {
1571 trace_probe_cleanup(tp);
1575 static struct trace_event_call *
1576 find_trace_event_call(const char *system, const char *event_name)
1578 struct trace_event_call *tp_event;
1581 list_for_each_entry(tp_event, &ftrace_events, list) {
1582 if (!tp_event->class->system ||
1583 strcmp(system, tp_event->class->system))
1585 name = trace_event_name(tp_event);
1586 if (!name || strcmp(event_name, name))
1594 int trace_probe_register_event_call(struct trace_probe *tp)
1596 struct trace_event_call *call = trace_probe_event_call(tp);
1599 lockdep_assert_held(&event_mutex);
1601 if (find_trace_event_call(trace_probe_group_name(tp),
1602 trace_probe_name(tp)))
1605 ret = register_trace_event(&call->event);
1609 ret = trace_add_event_call(call);
1611 unregister_trace_event(&call->event);
1616 int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
1618 struct event_file_link *link;
1620 link = kmalloc(sizeof(*link), GFP_KERNEL);
1625 INIT_LIST_HEAD(&link->list);
1626 list_add_tail_rcu(&link->list, &tp->event->files);
1627 trace_probe_set_flag(tp, TP_FLAG_TRACE);
1631 struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
1632 struct trace_event_file *file)
1634 struct event_file_link *link;
1636 trace_probe_for_each_link(link, tp) {
1637 if (link->file == file)
1644 int trace_probe_remove_file(struct trace_probe *tp,
1645 struct trace_event_file *file)
1647 struct event_file_link *link;
1649 link = trace_probe_get_file_link(tp, file);
1653 list_del_rcu(&link->list);
1654 kvfree_rcu_mightsleep(link);
1656 if (list_empty(&tp->event->files))
1657 trace_probe_clear_flag(tp, TP_FLAG_TRACE);
1663 * Return the smallest index of different type argument (start from 1).
1664 * If all argument types and name are same, return 0.
1666 int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b)
1670 /* In case of more arguments */
1671 if (a->nr_args < b->nr_args)
1672 return a->nr_args + 1;
1673 if (a->nr_args > b->nr_args)
1674 return b->nr_args + 1;
1676 for (i = 0; i < a->nr_args; i++) {
1677 if ((b->nr_args <= i) ||
1678 ((a->args[i].type != b->args[i].type) ||
1679 (a->args[i].count != b->args[i].count) ||
1680 strcmp(a->args[i].name, b->args[i].name)))
1687 bool trace_probe_match_command_args(struct trace_probe *tp,
1688 int argc, const char **argv)
1690 char buf[MAX_ARGSTR_LEN + 1];
1693 if (tp->nr_args < argc)
1696 for (i = 0; i < argc; i++) {
1697 snprintf(buf, sizeof(buf), "%s=%s",
1698 tp->args[i].name, tp->args[i].comm);
1699 if (strcmp(buf, argv[i]))
1705 int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **))
1707 int argc = 0, ret = 0;
1710 argv = argv_split(GFP_KERNEL, raw_command, &argc);
1715 ret = createfn(argc, (const char **)argv);
1722 int trace_probe_print_args(struct trace_seq *s, struct probe_arg *args, int nr_args,
1723 u8 *data, void *field)
1728 for (i = 0; i < nr_args; i++) {
1729 struct probe_arg *a = args + i;
1731 trace_seq_printf(s, " %s=", a->name);
1732 if (likely(!a->count)) {
1733 if (!a->type->print(s, data + a->offset, field))
1737 trace_seq_putc(s, '{');
1738 p = data + a->offset;
1739 for (j = 0; j < a->count; j++) {
1740 if (!a->type->print(s, p, field))
1742 trace_seq_putc(s, j == a->count - 1 ? '}' : ',');