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_btf.h"
18 #include "trace_probe.h"
23 static const char *trace_probe_err_text
[] = { ERRORS
};
25 static const char *reserved_field_names
[] = {
28 "common_preempt_count",
36 /* Printing in basic type function template */
37 #define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt) \
38 int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, void *data, void *ent)\
40 trace_seq_printf(s, fmt, *(type *)data); \
41 return !trace_seq_has_overflowed(s); \
43 const char PRINT_TYPE_FMT_NAME(tname)[] = fmt;
45 DEFINE_BASIC_PRINT_TYPE_FUNC(u8
, u8
, "%u")
46 DEFINE_BASIC_PRINT_TYPE_FUNC(u16
, u16
, "%u")
47 DEFINE_BASIC_PRINT_TYPE_FUNC(u32
, u32
, "%u")
48 DEFINE_BASIC_PRINT_TYPE_FUNC(u64
, u64
, "%Lu")
49 DEFINE_BASIC_PRINT_TYPE_FUNC(s8
, s8
, "%d")
50 DEFINE_BASIC_PRINT_TYPE_FUNC(s16
, s16
, "%d")
51 DEFINE_BASIC_PRINT_TYPE_FUNC(s32
, s32
, "%d")
52 DEFINE_BASIC_PRINT_TYPE_FUNC(s64
, s64
, "%Ld")
53 DEFINE_BASIC_PRINT_TYPE_FUNC(x8
, u8
, "0x%x")
54 DEFINE_BASIC_PRINT_TYPE_FUNC(x16
, u16
, "0x%x")
55 DEFINE_BASIC_PRINT_TYPE_FUNC(x32
, u32
, "0x%x")
56 DEFINE_BASIC_PRINT_TYPE_FUNC(x64
, u64
, "0x%Lx")
57 DEFINE_BASIC_PRINT_TYPE_FUNC(char, u8
, "'%c'")
59 int PRINT_TYPE_FUNC_NAME(symbol
)(struct trace_seq
*s
, void *data
, void *ent
)
61 trace_seq_printf(s
, "%pS", (void *)*(unsigned long *)data
);
62 return !trace_seq_has_overflowed(s
);
64 const char PRINT_TYPE_FMT_NAME(symbol
)[] = "%pS";
66 /* Print type function for string type */
67 int PRINT_TYPE_FUNC_NAME(string
)(struct trace_seq
*s
, void *data
, void *ent
)
69 int len
= *(u32
*)data
>> 16;
72 trace_seq_puts(s
, FAULT_STRING
);
74 trace_seq_printf(s
, "\"%s\"",
75 (const char *)get_loc_data(data
, ent
));
76 return !trace_seq_has_overflowed(s
);
79 const char PRINT_TYPE_FMT_NAME(string
)[] = "\\\"%s\\\"";
81 /* Fetch type information table */
82 static const struct fetch_type probe_fetch_types
[] = {
84 __ASSIGN_FETCH_TYPE("string", string
, string
, sizeof(u32
), 1, 1,
86 __ASSIGN_FETCH_TYPE("ustring", string
, string
, sizeof(u32
), 1, 1,
88 __ASSIGN_FETCH_TYPE("symstr", string
, string
, sizeof(u32
), 1, 1,
91 ASSIGN_FETCH_TYPE(u8
, u8
, 0),
92 ASSIGN_FETCH_TYPE(u16
, u16
, 0),
93 ASSIGN_FETCH_TYPE(u32
, u32
, 0),
94 ASSIGN_FETCH_TYPE(u64
, u64
, 0),
95 ASSIGN_FETCH_TYPE(s8
, u8
, 1),
96 ASSIGN_FETCH_TYPE(s16
, u16
, 1),
97 ASSIGN_FETCH_TYPE(s32
, u32
, 1),
98 ASSIGN_FETCH_TYPE(s64
, u64
, 1),
99 ASSIGN_FETCH_TYPE_ALIAS(x8
, u8
, u8
, 0),
100 ASSIGN_FETCH_TYPE_ALIAS(x16
, u16
, u16
, 0),
101 ASSIGN_FETCH_TYPE_ALIAS(x32
, u32
, u32
, 0),
102 ASSIGN_FETCH_TYPE_ALIAS(x64
, u64
, u64
, 0),
103 ASSIGN_FETCH_TYPE_ALIAS(char, u8
, u8
, 0),
104 ASSIGN_FETCH_TYPE_ALIAS(symbol
, ADDR_FETCH_TYPE
, ADDR_FETCH_TYPE
, 0),
106 ASSIGN_FETCH_TYPE_END
109 static const struct fetch_type
*find_fetch_type(const char *type
, unsigned long flags
)
113 /* Reject the symbol/symstr for uprobes */
114 if (type
&& (flags
& TPARG_FL_USER
) &&
115 (!strcmp(type
, "symbol") || !strcmp(type
, "symstr")))
119 type
= DEFAULT_FETCH_TYPE_STR
;
121 /* Special case: bitfield */
125 type
= strchr(type
, '/');
130 if (kstrtoul(type
, 0, &bs
))
135 return find_fetch_type("u8", flags
);
137 return find_fetch_type("u16", flags
);
139 return find_fetch_type("u32", flags
);
141 return find_fetch_type("u64", flags
);
147 for (i
= 0; probe_fetch_types
[i
].name
; i
++) {
148 if (strcmp(type
, probe_fetch_types
[i
].name
) == 0)
149 return &probe_fetch_types
[i
];
156 static struct trace_probe_log trace_probe_log
;
158 void trace_probe_log_init(const char *subsystem
, int argc
, const char **argv
)
160 trace_probe_log
.subsystem
= subsystem
;
161 trace_probe_log
.argc
= argc
;
162 trace_probe_log
.argv
= argv
;
163 trace_probe_log
.index
= 0;
166 void trace_probe_log_clear(void)
168 memset(&trace_probe_log
, 0, sizeof(trace_probe_log
));
171 void trace_probe_log_set_index(int index
)
173 trace_probe_log
.index
= index
;
176 void __trace_probe_log_err(int offset
, int err_type
)
179 int i
, len
= 0, pos
= 0;
181 if (!trace_probe_log
.argv
)
184 /* Recalculate the length and allocate buffer */
185 for (i
= 0; i
< trace_probe_log
.argc
; i
++) {
186 if (i
== trace_probe_log
.index
)
188 len
+= strlen(trace_probe_log
.argv
[i
]) + 1;
190 command
= kzalloc(len
, GFP_KERNEL
);
194 if (trace_probe_log
.index
>= trace_probe_log
.argc
) {
196 * Set the error position is next to the last arg + space.
197 * Note that len includes the terminal null and the cursor
198 * appears at pos + 1.
204 /* And make a command string from argv array */
206 for (i
= 0; i
< trace_probe_log
.argc
; i
++) {
207 len
= strlen(trace_probe_log
.argv
[i
]);
208 strcpy(p
, trace_probe_log
.argv
[i
]);
214 tracing_log_err(NULL
, trace_probe_log
.subsystem
, command
,
215 trace_probe_err_text
, err_type
, pos
+ offset
);
220 /* Split symbol and offset. */
221 int traceprobe_split_symbol_offset(char *symbol
, long *offset
)
229 tmp
= strpbrk(symbol
, "+-");
231 ret
= kstrtol(tmp
, 0, offset
);
241 /* @buf must has MAX_EVENT_NAME_LEN size */
242 int traceprobe_parse_event_name(const char **pevent
, const char **pgroup
,
243 char *buf
, int offset
)
245 const char *slash
, *event
= *pevent
;
248 slash
= strchr(event
, '/');
250 slash
= strchr(event
, '.');
253 if (slash
== event
) {
254 trace_probe_log_err(offset
, NO_GROUP_NAME
);
257 if (slash
- event
+ 1 > MAX_EVENT_NAME_LEN
) {
258 trace_probe_log_err(offset
, GROUP_TOO_LONG
);
261 strscpy(buf
, event
, slash
- event
+ 1);
262 if (!is_good_system_name(buf
)) {
263 trace_probe_log_err(offset
, BAD_GROUP_NAME
);
268 offset
+= slash
- event
+ 1;
277 trace_probe_log_err(offset
, NO_EVENT_NAME
);
279 } else if (len
>= MAX_EVENT_NAME_LEN
) {
280 trace_probe_log_err(offset
, EVENT_TOO_LONG
);
283 if (!is_good_name(event
)) {
284 trace_probe_log_err(offset
, BAD_EVENT_NAME
);
290 static int parse_trace_event_arg(char *arg
, struct fetch_insn
*code
,
291 struct traceprobe_parse_context
*ctx
)
293 struct ftrace_event_field
*field
;
294 struct list_head
*head
;
296 head
= trace_get_fields(ctx
->event
);
297 list_for_each_entry(field
, head
, link
) {
298 if (!strcmp(arg
, field
->name
)) {
299 code
->op
= FETCH_OP_TP_ARG
;
307 #ifdef CONFIG_PROBE_EVENTS_BTF_ARGS
309 static u32
btf_type_int(const struct btf_type
*t
)
311 return *(u32
*)(t
+ 1);
314 static bool btf_type_is_char_ptr(struct btf
*btf
, const struct btf_type
*type
)
316 const struct btf_type
*real_type
;
320 real_type
= btf_type_skip_modifiers(btf
, type
->type
, &tid
);
324 if (BTF_INFO_KIND(real_type
->info
) != BTF_KIND_INT
)
327 intdata
= btf_type_int(real_type
);
328 return !(BTF_INT_ENCODING(intdata
) & BTF_INT_SIGNED
)
329 && BTF_INT_BITS(intdata
) == 8;
332 static bool btf_type_is_char_array(struct btf
*btf
, const struct btf_type
*type
)
334 const struct btf_type
*real_type
;
335 const struct btf_array
*array
;
339 if (BTF_INFO_KIND(type
->info
) != BTF_KIND_ARRAY
)
342 array
= (const struct btf_array
*)(type
+ 1);
344 real_type
= btf_type_skip_modifiers(btf
, array
->type
, &tid
);
346 intdata
= btf_type_int(real_type
);
347 return !(BTF_INT_ENCODING(intdata
) & BTF_INT_SIGNED
)
348 && BTF_INT_BITS(intdata
) == 8;
351 static int check_prepare_btf_string_fetch(char *typename
,
352 struct fetch_insn
**pcode
,
353 struct traceprobe_parse_context
*ctx
)
355 struct btf
*btf
= ctx
->btf
;
357 if (!btf
|| !ctx
->last_type
)
360 /* char [] does not need any change. */
361 if (btf_type_is_char_array(btf
, ctx
->last_type
))
364 /* char * requires dereference the pointer. */
365 if (btf_type_is_char_ptr(btf
, ctx
->last_type
)) {
366 struct fetch_insn
*code
= *pcode
+ 1;
368 if (code
->op
== FETCH_OP_END
) {
369 trace_probe_log_err(ctx
->offset
, TOO_MANY_OPS
);
372 if (typename
[0] == 'u')
373 code
->op
= FETCH_OP_UDEREF
;
375 code
->op
= FETCH_OP_DEREF
;
380 /* Other types are not available for string */
381 trace_probe_log_err(ctx
->offset
, BAD_TYPE4STR
);
385 static const char *fetch_type_from_btf_type(struct btf
*btf
,
386 const struct btf_type
*type
,
387 struct traceprobe_parse_context
*ctx
)
391 /* TODO: const char * could be converted as a string */
392 switch (BTF_INFO_KIND(type
->info
)) {
394 /* enum is "int", so convert to "s32" */
396 case BTF_KIND_ENUM64
:
399 /* pointer will be converted to "x??" */
400 if (IS_ENABLED(CONFIG_64BIT
))
405 intdata
= btf_type_int(type
);
406 if (BTF_INT_ENCODING(intdata
) & BTF_INT_SIGNED
) {
407 switch (BTF_INT_BITS(intdata
)) {
417 } else { /* unsigned */
418 switch (BTF_INT_BITS(intdata
)) {
428 /* bitfield, size is encoded in the type */
429 ctx
->last_bitsize
= BTF_INT_BITS(intdata
);
430 ctx
->last_bitoffs
+= BTF_INT_OFFSET(intdata
);
434 /* TODO: support other types */
439 static int query_btf_context(struct traceprobe_parse_context
*ctx
)
441 const struct btf_param
*param
;
442 const struct btf_type
*type
;
452 type
= btf_find_func_proto(ctx
->funcname
, &btf
);
459 /* ctx->params is optional, since func(void) will not have params. */
461 param
= btf_get_func_param(type
, &nr
);
462 if (!IS_ERR_OR_NULL(param
)) {
463 /* Hide the first 'data' argument of tracepoint */
464 if (ctx
->flags
& TPARG_FL_TPOINT
) {
481 static void clear_btf_context(struct traceprobe_parse_context
*ctx
)
492 /* Return 1 if the field separater is arrow operator ('->') */
493 static int split_next_field(char *varname
, char **next_field
,
494 struct traceprobe_parse_context
*ctx
)
499 field
= strpbrk(varname
, ".-");
501 if (field
[0] == '-' && field
[1] == '>') {
505 } else if (field
[0] == '.') {
509 trace_probe_log_err(ctx
->offset
+ field
- varname
, BAD_HYPHEN
);
519 * Parse the field of data structure. The @type must be a pointer type
520 * pointing the target data structure type.
522 static int parse_btf_field(char *fieldname
, const struct btf_type
*type
,
523 struct fetch_insn
**pcode
, struct fetch_insn
*end
,
524 struct traceprobe_parse_context
*ctx
)
526 struct fetch_insn
*code
= *pcode
;
527 const struct btf_member
*field
;
528 u32 bitoffs
, anon_offs
;
534 /* Outer loop for solving arrow operator ('->') */
535 if (BTF_INFO_KIND(type
->info
) != BTF_KIND_PTR
) {
536 trace_probe_log_err(ctx
->offset
, NO_PTR_STRCT
);
539 /* Convert a struct pointer type to a struct type */
540 type
= btf_type_skip_modifiers(ctx
->btf
, type
->type
, &tid
);
542 trace_probe_log_err(ctx
->offset
, BAD_BTF_TID
);
548 /* Inner loop for solving dot operator ('.') */
550 is_ptr
= split_next_field(fieldname
, &next
, ctx
);
555 field
= btf_find_struct_member(ctx
->btf
, type
, fieldname
,
558 trace_probe_log_err(ctx
->offset
, BAD_BTF_TID
);
559 return PTR_ERR(field
);
562 trace_probe_log_err(ctx
->offset
, NO_BTF_FIELD
);
565 /* Add anonymous structure/union offset */
566 bitoffs
+= anon_offs
;
568 /* Accumulate the bit-offsets of the dot-connected fields */
569 if (btf_type_kflag(type
)) {
570 bitoffs
+= BTF_MEMBER_BIT_OFFSET(field
->offset
);
571 ctx
->last_bitsize
= BTF_MEMBER_BITFIELD_SIZE(field
->offset
);
573 bitoffs
+= field
->offset
;
574 ctx
->last_bitsize
= 0;
577 type
= btf_type_skip_modifiers(ctx
->btf
, field
->type
, &tid
);
579 trace_probe_log_err(ctx
->offset
, BAD_BTF_TID
);
583 ctx
->offset
+= next
- fieldname
;
585 } while (!is_ptr
&& fieldname
);
588 trace_probe_log_err(ctx
->offset
, TOO_MANY_OPS
);
591 code
->op
= FETCH_OP_DEREF
; /* TODO: user deref support */
592 code
->offset
= bitoffs
/ 8;
595 ctx
->last_bitoffs
= bitoffs
% 8;
596 ctx
->last_type
= type
;
602 static int __store_entry_arg(struct trace_probe
*tp
, int argnum
);
604 static int parse_btf_arg(char *varname
,
605 struct fetch_insn
**pcode
, struct fetch_insn
*end
,
606 struct traceprobe_parse_context
*ctx
)
608 struct fetch_insn
*code
= *pcode
;
609 const struct btf_param
*params
;
610 const struct btf_type
*type
;
615 if (WARN_ON_ONCE(!ctx
->funcname
))
618 is_ptr
= split_next_field(varname
, &field
, ctx
);
621 if (!is_ptr
&& field
) {
622 /* dot-connected field on an argument is not supported. */
623 trace_probe_log_err(ctx
->offset
+ field
- varname
,
628 if (ctx
->flags
& TPARG_FL_RETURN
&& !strcmp(varname
, "$retval")) {
629 code
->op
= FETCH_OP_RETVAL
;
630 /* Check whether the function return type is not void */
631 if (query_btf_context(ctx
) == 0) {
632 if (ctx
->proto
->type
== 0) {
633 trace_probe_log_err(ctx
->offset
, NO_RETVAL
);
636 tid
= ctx
->proto
->type
;
640 trace_probe_log_err(ctx
->offset
+ field
- varname
,
648 ret
= query_btf_context(ctx
);
649 if (ret
< 0 || ctx
->nr_params
== 0) {
650 trace_probe_log_err(ctx
->offset
, NO_BTF_ENTRY
);
651 return PTR_ERR(params
);
654 params
= ctx
->params
;
656 for (i
= 0; i
< ctx
->nr_params
; i
++) {
657 const char *name
= btf_name_by_offset(ctx
->btf
, params
[i
].name_off
);
659 if (name
&& !strcmp(name
, varname
)) {
660 if (tparg_is_function_entry(ctx
->flags
)) {
661 code
->op
= FETCH_OP_ARG
;
662 if (ctx
->flags
& TPARG_FL_TPOINT
)
666 } else if (tparg_is_function_return(ctx
->flags
)) {
667 code
->op
= FETCH_OP_EDATA
;
668 ret
= __store_entry_arg(ctx
->tp
, i
);
675 tid
= params
[i
].type
;
679 trace_probe_log_err(ctx
->offset
, NO_BTFARG
);
683 type
= btf_type_skip_modifiers(ctx
->btf
, tid
, &tid
);
685 trace_probe_log_err(ctx
->offset
, BAD_BTF_TID
);
688 /* Initialize the last type information */
689 ctx
->last_type
= type
;
690 ctx
->last_bitoffs
= 0;
691 ctx
->last_bitsize
= 0;
693 ctx
->offset
+= field
- varname
;
694 return parse_btf_field(field
, type
, pcode
, end
, ctx
);
699 static const struct fetch_type
*find_fetch_type_from_btf_type(
700 struct traceprobe_parse_context
*ctx
)
702 struct btf
*btf
= ctx
->btf
;
703 const char *typestr
= NULL
;
705 if (btf
&& ctx
->last_type
)
706 typestr
= fetch_type_from_btf_type(btf
, ctx
->last_type
, ctx
);
708 return find_fetch_type(typestr
, ctx
->flags
);
711 static int parse_btf_bitfield(struct fetch_insn
**pcode
,
712 struct traceprobe_parse_context
*ctx
)
714 struct fetch_insn
*code
= *pcode
;
716 if ((ctx
->last_bitsize
% 8 == 0) && ctx
->last_bitoffs
== 0)
720 if (code
->op
!= FETCH_OP_NOP
) {
721 trace_probe_log_err(ctx
->offset
, TOO_MANY_OPS
);
726 code
->op
= FETCH_OP_MOD_BF
;
727 code
->lshift
= 64 - (ctx
->last_bitsize
+ ctx
->last_bitoffs
);
728 code
->rshift
= 64 - ctx
->last_bitsize
;
729 code
->basesize
= 64 / 8;
734 static void clear_btf_context(struct traceprobe_parse_context
*ctx
)
739 static int query_btf_context(struct traceprobe_parse_context
*ctx
)
744 static int parse_btf_arg(char *varname
,
745 struct fetch_insn
**pcode
, struct fetch_insn
*end
,
746 struct traceprobe_parse_context
*ctx
)
748 trace_probe_log_err(ctx
->offset
, NOSUP_BTFARG
);
752 static int parse_btf_bitfield(struct fetch_insn
**pcode
,
753 struct traceprobe_parse_context
*ctx
)
755 trace_probe_log_err(ctx
->offset
, NOSUP_BTFARG
);
759 #define find_fetch_type_from_btf_type(ctx) \
760 find_fetch_type(NULL, ctx->flags)
762 static int check_prepare_btf_string_fetch(char *typename
,
763 struct fetch_insn
**pcode
,
764 struct traceprobe_parse_context
*ctx
)
771 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
773 static int __store_entry_arg(struct trace_probe
*tp
, int argnum
)
775 struct probe_entry_arg
*earg
= tp
->entry_arg
;
780 earg
= kzalloc(sizeof(*tp
->entry_arg
), GFP_KERNEL
);
783 earg
->size
= 2 * tp
->nr_args
+ 1;
784 earg
->code
= kcalloc(earg
->size
, sizeof(struct fetch_insn
),
790 /* Fill the code buffer with 'end' to simplify it */
791 for (i
= 0; i
< earg
->size
; i
++)
792 earg
->code
[i
].op
= FETCH_OP_END
;
793 tp
->entry_arg
= earg
;
797 for (i
= 0; i
< earg
->size
- 1; i
++) {
798 switch (earg
->code
[i
].op
) {
800 earg
->code
[i
].op
= FETCH_OP_ARG
;
801 earg
->code
[i
].param
= argnum
;
802 earg
->code
[i
+ 1].op
= FETCH_OP_ST_EDATA
;
803 earg
->code
[i
+ 1].offset
= offset
;
806 match
= (earg
->code
[i
].param
== argnum
);
808 case FETCH_OP_ST_EDATA
:
809 offset
= earg
->code
[i
].offset
;
812 offset
+= sizeof(unsigned long);
821 int traceprobe_get_entry_data_size(struct trace_probe
*tp
)
823 struct probe_entry_arg
*earg
= tp
->entry_arg
;
829 for (i
= 0; i
< earg
->size
; i
++) {
830 switch (earg
->code
[i
].op
) {
833 case FETCH_OP_ST_EDATA
:
834 size
= earg
->code
[i
].offset
+ sizeof(unsigned long);
844 void store_trace_entry_data(void *edata
, struct trace_probe
*tp
, struct pt_regs
*regs
)
846 struct probe_entry_arg
*earg
= tp
->entry_arg
;
847 unsigned long val
= 0;
853 for (i
= 0; i
< earg
->size
; i
++) {
854 struct fetch_insn
*code
= &earg
->code
[i
];
858 val
= regs_get_kernel_argument(regs
, code
->param
);
860 case FETCH_OP_ST_EDATA
:
861 *(unsigned long *)((unsigned long)edata
+ code
->offset
) = val
;
872 NOKPROBE_SYMBOL(store_trace_entry_data
)
875 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
877 /* Parse $vars. @orig_arg points '$', which syncs to @ctx->offset */
878 static int parse_probe_vars(char *orig_arg
, const struct fetch_type
*t
,
879 struct fetch_insn
**pcode
,
880 struct fetch_insn
*end
,
881 struct traceprobe_parse_context
*ctx
)
883 struct fetch_insn
*code
= *pcode
;
884 int err
= TP_ERR_BAD_VAR
;
885 char *arg
= orig_arg
+ 1;
890 if (ctx
->flags
& TPARG_FL_TEVENT
) {
893 ret
= parse_trace_event_arg(arg
, code
, ctx
);
896 if (strcmp(arg
, "comm") == 0 || strcmp(arg
, "COMM") == 0) {
897 code
->op
= FETCH_OP_COMM
;
900 /* backward compatibility */
905 if (str_has_prefix(arg
, "retval")) {
906 if (!(ctx
->flags
& TPARG_FL_RETURN
)) {
907 err
= TP_ERR_RETVAL_ON_PROBE
;
910 if (!(ctx
->flags
& TPARG_FL_KERNEL
) ||
911 !IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS
)) {
912 code
->op
= FETCH_OP_RETVAL
;
915 return parse_btf_arg(orig_arg
, pcode
, end
, ctx
);
918 len
= str_has_prefix(arg
, "stack");
921 if (arg
[len
] == '\0') {
922 code
->op
= FETCH_OP_STACKP
;
926 if (isdigit(arg
[len
])) {
927 ret
= kstrtoul(arg
+ len
, 10, ¶m
);
931 if ((ctx
->flags
& TPARG_FL_KERNEL
) &&
932 param
> PARAM_MAX_STACK
) {
933 err
= TP_ERR_BAD_STACK_NUM
;
936 code
->op
= FETCH_OP_STACK
;
937 code
->param
= (unsigned int)param
;
943 if (strcmp(arg
, "comm") == 0 || strcmp(arg
, "COMM") == 0) {
944 code
->op
= FETCH_OP_COMM
;
948 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
949 len
= str_has_prefix(arg
, "arg");
951 ret
= kstrtoul(arg
+ len
, 10, ¶m
);
955 if (!param
|| param
> PARAM_MAX_STACK
) {
956 err
= TP_ERR_BAD_ARG_NUM
;
959 param
--; /* argN starts from 1, but internal arg[N] starts from 0 */
961 if (tparg_is_function_entry(ctx
->flags
)) {
962 code
->op
= FETCH_OP_ARG
;
963 code
->param
= (unsigned int)param
;
965 * The tracepoint probe will probe a stub function, and the
966 * first parameter of the stub is a dummy and should be ignored.
968 if (ctx
->flags
& TPARG_FL_TPOINT
)
970 } else if (tparg_is_function_return(ctx
->flags
)) {
971 /* function entry argument access from return probe */
972 ret
= __store_entry_arg(ctx
->tp
, param
);
973 if (ret
< 0) /* This error should be an internal error */
976 code
->op
= FETCH_OP_EDATA
;
979 err
= TP_ERR_NOFENTRY_ARGS
;
987 __trace_probe_log_err(ctx
->offset
, err
);
991 static int str_to_immediate(char *str
, unsigned long *imm
)
994 return kstrtoul(str
, 0, imm
);
995 else if (str
[0] == '-')
996 return kstrtol(str
, 0, (long *)imm
);
997 else if (str
[0] == '+')
998 return kstrtol(str
+ 1, 0, (long *)imm
);
1002 static int __parse_imm_string(char *str
, char **pbuf
, int offs
)
1004 size_t len
= strlen(str
);
1006 if (str
[len
- 1] != '"') {
1007 trace_probe_log_err(offs
+ len
, IMMSTR_NO_CLOSE
);
1010 *pbuf
= kstrndup(str
, len
- 1, GFP_KERNEL
);
1016 /* Recursive argument parser */
1018 parse_probe_arg(char *arg
, const struct fetch_type
*type
,
1019 struct fetch_insn
**pcode
, struct fetch_insn
*end
,
1020 struct traceprobe_parse_context
*ctx
)
1022 struct fetch_insn
*code
= *pcode
;
1023 unsigned long param
;
1024 int deref
= FETCH_OP_DEREF
;
1031 ret
= parse_probe_vars(arg
, type
, pcode
, end
, ctx
);
1034 case '%': /* named register */
1035 if (ctx
->flags
& (TPARG_FL_TEVENT
| TPARG_FL_FPROBE
)) {
1036 /* eprobe and fprobe do not handle registers */
1037 trace_probe_log_err(ctx
->offset
, BAD_VAR
);
1040 ret
= regs_query_register_offset(arg
+ 1);
1042 code
->op
= FETCH_OP_REG
;
1043 code
->param
= (unsigned int)ret
;
1046 trace_probe_log_err(ctx
->offset
, BAD_REG_NAME
);
1049 case '@': /* memory, file-offset or symbol */
1050 if (isdigit(arg
[1])) {
1051 ret
= kstrtoul(arg
+ 1, 0, ¶m
);
1053 trace_probe_log_err(ctx
->offset
, BAD_MEM_ADDR
);
1057 code
->op
= FETCH_OP_IMM
;
1058 code
->immediate
= param
;
1059 } else if (arg
[1] == '+') {
1060 /* kprobes don't support file offsets */
1061 if (ctx
->flags
& TPARG_FL_KERNEL
) {
1062 trace_probe_log_err(ctx
->offset
, FILE_ON_KPROBE
);
1065 ret
= kstrtol(arg
+ 2, 0, &offset
);
1067 trace_probe_log_err(ctx
->offset
, BAD_FILE_OFFS
);
1071 code
->op
= FETCH_OP_FOFFS
;
1072 code
->immediate
= (unsigned long)offset
; // imm64?
1074 /* uprobes don't support symbols */
1075 if (!(ctx
->flags
& TPARG_FL_KERNEL
)) {
1076 trace_probe_log_err(ctx
->offset
, SYM_ON_UPROBE
);
1079 /* Preserve symbol for updating */
1080 code
->op
= FETCH_NOP_SYMBOL
;
1081 code
->data
= kstrdup(arg
+ 1, GFP_KERNEL
);
1084 if (++code
== end
) {
1085 trace_probe_log_err(ctx
->offset
, TOO_MANY_OPS
);
1088 code
->op
= FETCH_OP_IMM
;
1089 code
->immediate
= 0;
1091 /* These are fetching from memory */
1092 if (++code
== end
) {
1093 trace_probe_log_err(ctx
->offset
, TOO_MANY_OPS
);
1097 code
->op
= FETCH_OP_DEREF
;
1098 code
->offset
= offset
;
1101 case '+': /* deref memory */
1103 if (arg
[1] == 'u') {
1104 deref
= FETCH_OP_UDEREF
;
1109 arg
++; /* Skip '+', because kstrtol() rejects it. */
1110 tmp
= strchr(arg
, '(');
1112 trace_probe_log_err(ctx
->offset
, DEREF_NEED_BRACE
);
1116 ret
= kstrtol(arg
, 0, &offset
);
1118 trace_probe_log_err(ctx
->offset
, BAD_DEREF_OFFS
);
1121 ctx
->offset
+= (tmp
+ 1 - arg
) + (arg
[0] != '-' ? 1 : 0);
1123 tmp
= strrchr(arg
, ')');
1125 trace_probe_log_err(ctx
->offset
+ strlen(arg
),
1129 const struct fetch_type
*t2
= find_fetch_type(NULL
, ctx
->flags
);
1130 int cur_offs
= ctx
->offset
;
1133 ret
= parse_probe_arg(arg
, t2
, &code
, end
, ctx
);
1136 ctx
->offset
= cur_offs
;
1137 if (code
->op
== FETCH_OP_COMM
||
1138 code
->op
== FETCH_OP_DATA
) {
1139 trace_probe_log_err(ctx
->offset
, COMM_CANT_DEREF
);
1142 if (++code
== end
) {
1143 trace_probe_log_err(ctx
->offset
, TOO_MANY_OPS
);
1149 code
->offset
= offset
;
1150 /* Reset the last type if used */
1151 ctx
->last_type
= NULL
;
1154 case '\\': /* Immediate value */
1155 if (arg
[1] == '"') { /* Immediate string */
1156 ret
= __parse_imm_string(arg
+ 2, &tmp
, ctx
->offset
+ 2);
1159 code
->op
= FETCH_OP_DATA
;
1162 ret
= str_to_immediate(arg
+ 1, &code
->immediate
);
1164 trace_probe_log_err(ctx
->offset
+ 1, BAD_IMM
);
1166 code
->op
= FETCH_OP_IMM
;
1170 if (isalpha(arg
[0]) || arg
[0] == '_') { /* BTF variable */
1171 if (!tparg_is_function_entry(ctx
->flags
) &&
1172 !tparg_is_function_return(ctx
->flags
)) {
1173 trace_probe_log_err(ctx
->offset
, NOSUP_BTFARG
);
1176 ret
= parse_btf_arg(arg
, pcode
, end
, ctx
);
1180 if (!ret
&& code
->op
== FETCH_OP_NOP
) {
1181 /* Parsed, but do not find fetch method */
1182 trace_probe_log_err(ctx
->offset
, BAD_FETCH_ARG
);
1188 /* Bitfield type needs to be parsed into a fetch function */
1189 static int __parse_bitfield_probe_arg(const char *bf
,
1190 const struct fetch_type
*t
,
1191 struct fetch_insn
**pcode
)
1193 struct fetch_insn
*code
= *pcode
;
1194 unsigned long bw
, bo
;
1200 bw
= simple_strtoul(bf
+ 1, &tail
, 0); /* Use simple one */
1202 if (bw
== 0 || *tail
!= '@')
1206 bo
= simple_strtoul(bf
, &tail
, 0);
1208 if (tail
== bf
|| *tail
!= '/')
1211 if (code
->op
!= FETCH_OP_NOP
)
1215 code
->op
= FETCH_OP_MOD_BF
;
1216 code
->lshift
= BYTES_TO_BITS(t
->size
) - (bw
+ bo
);
1217 code
->rshift
= BYTES_TO_BITS(t
->size
) - bw
;
1218 code
->basesize
= t
->size
;
1220 return (BYTES_TO_BITS(t
->size
) < (bw
+ bo
)) ? -EINVAL
: 0;
1223 /* Split type part from @arg and return it. */
1224 static char *parse_probe_arg_type(char *arg
, struct probe_arg
*parg
,
1225 struct traceprobe_parse_context
*ctx
)
1227 char *t
= NULL
, *t2
, *t3
;
1230 t
= strchr(arg
, ':');
1233 t2
= strchr(t
, '[');
1236 t3
= strchr(t2
, ']');
1238 offs
= t2
+ strlen(t2
) - arg
;
1240 trace_probe_log_err(ctx
->offset
+ offs
,
1242 return ERR_PTR(-EINVAL
);
1243 } else if (t3
[1] != '\0') {
1244 trace_probe_log_err(ctx
->offset
+ t3
+ 1 - arg
,
1246 return ERR_PTR(-EINVAL
);
1249 if (kstrtouint(t2
, 0, &parg
->count
) || !parg
->count
) {
1250 trace_probe_log_err(ctx
->offset
+ t2
- arg
,
1252 return ERR_PTR(-EINVAL
);
1254 if (parg
->count
> MAX_ARRAY_LEN
) {
1255 trace_probe_log_err(ctx
->offset
+ t2
- arg
,
1257 return ERR_PTR(-EINVAL
);
1261 offs
= t
? t
- arg
: 0;
1264 * Since $comm and immediate string can not be dereferenced,
1265 * we can find those by strcmp. But ignore for eprobes.
1267 if (!(ctx
->flags
& TPARG_FL_TEVENT
) &&
1268 (strcmp(arg
, "$comm") == 0 || strcmp(arg
, "$COMM") == 0 ||
1269 strncmp(arg
, "\\\"", 2) == 0)) {
1270 /* The type of $comm must be "string", and not an array type. */
1271 if (parg
->count
|| (t
&& strcmp(t
, "string"))) {
1272 trace_probe_log_err(ctx
->offset
+ offs
, NEED_STRING_TYPE
);
1273 return ERR_PTR(-EINVAL
);
1275 parg
->type
= find_fetch_type("string", ctx
->flags
);
1277 parg
->type
= find_fetch_type(t
, ctx
->flags
);
1280 trace_probe_log_err(ctx
->offset
+ offs
, BAD_TYPE
);
1281 return ERR_PTR(-EINVAL
);
1287 /* After parsing, adjust the fetch_insn according to the probe_arg */
1288 static int finalize_fetch_insn(struct fetch_insn
*code
,
1289 struct probe_arg
*parg
,
1292 struct traceprobe_parse_context
*ctx
)
1294 struct fetch_insn
*scode
;
1297 /* Store operation */
1298 if (parg
->type
->is_string
) {
1299 /* Check bad combination of the type and the last fetch_insn. */
1300 if (!strcmp(parg
->type
->name
, "symstr")) {
1301 if (code
->op
!= FETCH_OP_REG
&& code
->op
!= FETCH_OP_STACK
&&
1302 code
->op
!= FETCH_OP_RETVAL
&& code
->op
!= FETCH_OP_ARG
&&
1303 code
->op
!= FETCH_OP_DEREF
&& code
->op
!= FETCH_OP_TP_ARG
) {
1304 trace_probe_log_err(ctx
->offset
+ type_offset
,
1309 if (code
->op
!= FETCH_OP_DEREF
&& code
->op
!= FETCH_OP_UDEREF
&&
1310 code
->op
!= FETCH_OP_IMM
&& code
->op
!= FETCH_OP_COMM
&&
1311 code
->op
!= FETCH_OP_DATA
&& code
->op
!= FETCH_OP_TP_ARG
) {
1312 trace_probe_log_err(ctx
->offset
+ type_offset
,
1318 if (!strcmp(parg
->type
->name
, "symstr") ||
1319 (code
->op
== FETCH_OP_IMM
|| code
->op
== FETCH_OP_COMM
||
1320 code
->op
== FETCH_OP_DATA
) || code
->op
== FETCH_OP_TP_ARG
||
1323 * IMM, DATA and COMM is pointing actual address, those
1324 * must be kept, and if parg->count != 0, this is an
1325 * array of string pointers instead of string address
1327 * For the symstr, it doesn't need to dereference, thus
1328 * it just get the value.
1331 if (code
->op
!= FETCH_OP_NOP
) {
1332 trace_probe_log_err(ctx
->offset
, TOO_MANY_OPS
);
1337 /* If op == DEREF, replace it with STRING */
1338 if (!strcmp(parg
->type
->name
, "ustring") ||
1339 code
->op
== FETCH_OP_UDEREF
)
1340 code
->op
= FETCH_OP_ST_USTRING
;
1341 else if (!strcmp(parg
->type
->name
, "symstr"))
1342 code
->op
= FETCH_OP_ST_SYMSTR
;
1344 code
->op
= FETCH_OP_ST_STRING
;
1345 code
->size
= parg
->type
->size
;
1346 parg
->dynamic
= true;
1347 } else if (code
->op
== FETCH_OP_DEREF
) {
1348 code
->op
= FETCH_OP_ST_MEM
;
1349 code
->size
= parg
->type
->size
;
1350 } else if (code
->op
== FETCH_OP_UDEREF
) {
1351 code
->op
= FETCH_OP_ST_UMEM
;
1352 code
->size
= parg
->type
->size
;
1355 if (code
->op
!= FETCH_OP_NOP
) {
1356 trace_probe_log_err(ctx
->offset
, TOO_MANY_OPS
);
1359 code
->op
= FETCH_OP_ST_RAW
;
1360 code
->size
= parg
->type
->size
;
1363 /* Save storing fetch_insn. */
1366 /* Modify operation */
1368 /* Bitfield needs a special fetch_insn. */
1369 ret
= __parse_bitfield_probe_arg(type
, parg
->type
, &code
);
1371 trace_probe_log_err(ctx
->offset
+ type_offset
, BAD_BITFIELD
);
1374 } else if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS
) &&
1376 /* If user not specified the type, try parsing BTF bitfield. */
1377 ret
= parse_btf_bitfield(&code
, ctx
);
1382 /* Loop(Array) operation */
1384 if (scode
->op
!= FETCH_OP_ST_MEM
&&
1385 scode
->op
!= FETCH_OP_ST_STRING
&&
1386 scode
->op
!= FETCH_OP_ST_USTRING
) {
1387 trace_probe_log_err(ctx
->offset
+ type_offset
, BAD_STRING
);
1391 if (code
->op
!= FETCH_OP_NOP
) {
1392 trace_probe_log_err(ctx
->offset
, TOO_MANY_OPS
);
1395 code
->op
= FETCH_OP_LP_ARRAY
;
1396 code
->param
= parg
->count
;
1399 /* Finalize the fetch_insn array. */
1401 code
->op
= FETCH_OP_END
;
1406 /* String length checking wrapper */
1407 static int traceprobe_parse_probe_arg_body(const char *argv
, ssize_t
*size
,
1408 struct probe_arg
*parg
,
1409 struct traceprobe_parse_context
*ctx
)
1411 struct fetch_insn
*code
, *tmp
= NULL
;
1416 if (len
> MAX_ARGSTR_LEN
) {
1417 trace_probe_log_err(ctx
->offset
, ARG_TOO_LONG
);
1419 } else if (len
== 0) {
1420 trace_probe_log_err(ctx
->offset
, NO_ARG_BODY
);
1424 arg
= kstrdup(argv
, GFP_KERNEL
);
1428 parg
->comm
= kstrdup(arg
, GFP_KERNEL
);
1434 type
= parse_probe_arg_type(arg
, parg
, ctx
);
1436 ret
= PTR_ERR(type
);
1440 code
= tmp
= kcalloc(FETCH_INSN_MAX
, sizeof(*code
), GFP_KERNEL
);
1445 code
[FETCH_INSN_MAX
- 1].op
= FETCH_OP_END
;
1447 ctx
->last_type
= NULL
;
1448 ret
= parse_probe_arg(arg
, parg
->type
, &code
, &code
[FETCH_INSN_MAX
- 1],
1453 /* Update storing type if BTF is available */
1454 if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS
) &&
1457 parg
->type
= find_fetch_type_from_btf_type(ctx
);
1458 } else if (strstr(type
, "string")) {
1459 ret
= check_prepare_btf_string_fetch(type
, &code
, ctx
);
1464 parg
->offset
= *size
;
1465 *size
+= parg
->type
->size
* (parg
->count
?: 1);
1468 len
= strlen(parg
->type
->fmttype
) + 6;
1469 parg
->fmt
= kmalloc(len
, GFP_KERNEL
);
1474 snprintf(parg
->fmt
, len
, "%s[%d]", parg
->type
->fmttype
,
1478 ret
= finalize_fetch_insn(code
, parg
, type
, type
? type
- arg
: 0, ctx
);
1482 for (; code
< tmp
+ FETCH_INSN_MAX
; code
++)
1483 if (code
->op
== FETCH_OP_END
)
1485 /* Shrink down the code buffer */
1486 parg
->code
= kcalloc(code
- tmp
+ 1, sizeof(*code
), GFP_KERNEL
);
1490 memcpy(parg
->code
, tmp
, sizeof(*code
) * (code
- tmp
+ 1));
1494 for (code
= tmp
; code
< tmp
+ FETCH_INSN_MAX
; code
++)
1495 if (code
->op
== FETCH_NOP_SYMBOL
||
1496 code
->op
== FETCH_OP_DATA
)
1506 /* Return 1 if name is reserved or already used by another argument */
1507 static int traceprobe_conflict_field_name(const char *name
,
1508 struct probe_arg
*args
, int narg
)
1512 for (i
= 0; i
< ARRAY_SIZE(reserved_field_names
); i
++)
1513 if (strcmp(reserved_field_names
[i
], name
) == 0)
1516 for (i
= 0; i
< narg
; i
++)
1517 if (strcmp(args
[i
].name
, name
) == 0)
1523 static char *generate_probe_arg_name(const char *arg
, int idx
)
1529 * If argument name is omitted, try arg as a name (BTF variable)
1532 if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS
)) {
1533 end
= strchr(arg
, ':');
1535 end
= arg
+ strlen(arg
);
1537 name
= kmemdup_nul(arg
, end
- arg
, GFP_KERNEL
);
1538 if (!name
|| !is_good_name(name
)) {
1545 name
= kasprintf(GFP_KERNEL
, "arg%d", idx
+ 1);
1550 int traceprobe_parse_probe_arg(struct trace_probe
*tp
, int i
, const char *arg
,
1551 struct traceprobe_parse_context
*ctx
)
1553 struct probe_arg
*parg
= &tp
->args
[i
];
1557 body
= strchr(arg
, '=');
1559 if (body
- arg
> MAX_ARG_NAME_LEN
) {
1560 trace_probe_log_err(0, ARG_NAME_TOO_LONG
);
1562 } else if (body
== arg
) {
1563 trace_probe_log_err(0, NO_ARG_NAME
);
1566 parg
->name
= kmemdup_nul(arg
, body
- arg
, GFP_KERNEL
);
1569 parg
->name
= generate_probe_arg_name(arg
, i
);
1575 if (!is_good_name(parg
->name
)) {
1576 trace_probe_log_err(0, BAD_ARG_NAME
);
1579 if (traceprobe_conflict_field_name(parg
->name
, tp
->args
, i
)) {
1580 trace_probe_log_err(0, USED_ARG_NAME
);
1583 ctx
->offset
= body
- arg
;
1584 /* Parse fetch argument */
1585 return traceprobe_parse_probe_arg_body(body
, &tp
->size
, parg
, ctx
);
1588 void traceprobe_free_probe_arg(struct probe_arg
*arg
)
1590 struct fetch_insn
*code
= arg
->code
;
1592 while (code
&& code
->op
!= FETCH_OP_END
) {
1593 if (code
->op
== FETCH_NOP_SYMBOL
||
1594 code
->op
== FETCH_OP_DATA
)
1604 static int argv_has_var_arg(int argc
, const char *argv
[], int *args_idx
,
1605 struct traceprobe_parse_context
*ctx
)
1609 for (i
= 0; i
< argc
; i
++)
1610 if (str_has_prefix(argv
[i
], "$arg")) {
1611 trace_probe_log_set_index(i
+ 2);
1613 if (!tparg_is_function_entry(ctx
->flags
) &&
1614 !tparg_is_function_return(ctx
->flags
)) {
1615 trace_probe_log_err(0, NOFENTRY_ARGS
);
1619 if (isdigit(argv
[i
][4])) {
1624 if (argv
[i
][4] != '*') {
1625 trace_probe_log_err(0, BAD_VAR
);
1629 if (*args_idx
>= 0 && *args_idx
< argc
) {
1630 trace_probe_log_err(0, DOUBLE_ARGS
);
1640 static int sprint_nth_btf_arg(int idx
, const char *type
,
1641 char *buf
, int bufsize
,
1642 struct traceprobe_parse_context
*ctx
)
1647 if (idx
>= ctx
->nr_params
) {
1648 trace_probe_log_err(0, NO_BTFARG
);
1651 name
= btf_name_by_offset(ctx
->btf
, ctx
->params
[idx
].name_off
);
1653 trace_probe_log_err(0, NO_BTF_ENTRY
);
1656 ret
= snprintf(buf
, bufsize
, "%s%s", name
, type
);
1657 if (ret
>= bufsize
) {
1658 trace_probe_log_err(0, ARGS_2LONG
);
1664 /* Return new_argv which must be freed after use */
1665 const char **traceprobe_expand_meta_args(int argc
, const char *argv
[],
1666 int *new_argc
, char *buf
, int bufsize
,
1667 struct traceprobe_parse_context
*ctx
)
1669 const struct btf_param
*params
= NULL
;
1670 int i
, j
, n
, used
, ret
, args_idx
= -1;
1671 const char **new_argv
= NULL
;
1673 ret
= argv_has_var_arg(argc
, argv
, &args_idx
, ctx
);
1675 return ERR_PTR(ret
);
1682 ret
= query_btf_context(ctx
);
1683 if (ret
< 0 || ctx
->nr_params
== 0) {
1684 if (args_idx
!= -1) {
1685 /* $arg* requires BTF info */
1686 trace_probe_log_err(0, NOSUP_BTFARG
);
1687 return (const char **)params
;
1694 *new_argc
= argc
+ ctx
->nr_params
- 1;
1698 new_argv
= kcalloc(*new_argc
, sizeof(char *), GFP_KERNEL
);
1700 return ERR_PTR(-ENOMEM
);
1703 for (i
= 0, j
= 0; i
< argc
; i
++) {
1704 trace_probe_log_set_index(i
+ 2);
1705 if (i
== args_idx
) {
1706 for (n
= 0; n
< ctx
->nr_params
; n
++) {
1707 ret
= sprint_nth_btf_arg(n
, "", buf
+ used
,
1708 bufsize
- used
, ctx
);
1712 new_argv
[j
++] = buf
+ used
;
1718 if (str_has_prefix(argv
[i
], "$arg")) {
1721 n
= simple_strtoul(argv
[i
] + 4, &type
, 10);
1722 if (type
&& !(*type
== ':' || *type
== '\0')) {
1723 trace_probe_log_err(0, BAD_VAR
);
1727 /* Note: $argN starts from $arg1 */
1728 ret
= sprint_nth_btf_arg(n
- 1, type
, buf
+ used
,
1729 bufsize
- used
, ctx
);
1732 new_argv
[j
++] = buf
+ used
;
1735 new_argv
[j
++] = argv
[i
];
1742 return ERR_PTR(ret
);
1745 /* @buf: *buf must be equal to NULL. Caller must to free *buf */
1746 int traceprobe_expand_dentry_args(int argc
, const char *argv
[], char **buf
)
1749 const int bufsize
= MAX_DENTRY_ARGS_LEN
;
1750 char *tmpbuf
= NULL
;
1756 for (i
= 0; i
< argc
; i
++) {
1761 if (!glob_match("*:%p[dD]", argv
[i
]))
1765 tmpbuf
= kmalloc(bufsize
, GFP_KERNEL
);
1770 tmp
= kstrdup(argv
[i
], GFP_KERNEL
);
1774 equal
= strchr(tmp
, '=');
1777 arg_len
= strlen(argv
[i
]);
1778 tmp
[arg_len
- 4] = '\0';
1779 if (argv
[i
][arg_len
- 1] == 'd')
1780 ret
= snprintf(tmpbuf
+ used
, bufsize
- used
,
1781 "%s%s+0x0(+0x%zx(%s)):string",
1782 equal
? tmp
: "", equal
? "=" : "",
1783 offsetof(struct dentry
, d_name
.name
),
1784 equal
? equal
+ 1 : tmp
);
1786 ret
= snprintf(tmpbuf
+ used
, bufsize
- used
,
1787 "%s%s+0x0(+0x%zx(+0x%zx(%s))):string",
1788 equal
? tmp
: "", equal
? "=" : "",
1789 offsetof(struct dentry
, d_name
.name
),
1790 offsetof(struct file
, f_path
.dentry
),
1791 equal
? equal
+ 1 : tmp
);
1794 if (ret
>= bufsize
- used
)
1796 argv
[i
] = tmpbuf
+ used
;
1807 void traceprobe_finish_parse(struct traceprobe_parse_context
*ctx
)
1809 clear_btf_context(ctx
);
1812 int traceprobe_update_arg(struct probe_arg
*arg
)
1814 struct fetch_insn
*code
= arg
->code
;
1820 while (code
&& code
->op
!= FETCH_OP_END
) {
1821 if (code
->op
== FETCH_NOP_SYMBOL
) {
1822 if (code
[1].op
!= FETCH_OP_IMM
)
1825 tmp
= strpbrk(code
->data
, "+-");
1828 ret
= traceprobe_split_symbol_offset(code
->data
,
1834 (unsigned long)kallsyms_lookup_name(code
->data
);
1837 if (!code
[1].immediate
)
1839 code
[1].immediate
+= offset
;
1846 /* When len=0, we just calculate the needed length */
1847 #define LEN_OR_ZERO (len ? len - pos : 0)
1848 static int __set_print_fmt(struct trace_probe
*tp
, char *buf
, int len
,
1849 enum probe_print_type ptype
)
1851 struct probe_arg
*parg
;
1854 const char *fmt
, *arg
;
1857 case PROBE_PRINT_NORMAL
:
1859 arg
= ", REC->" FIELD_STRING_IP
;
1861 case PROBE_PRINT_RETURN
:
1862 fmt
= "(%lx <- %lx)";
1863 arg
= ", REC->" FIELD_STRING_FUNC
", REC->" FIELD_STRING_RETIP
;
1865 case PROBE_PRINT_EVENT
:
1874 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "\"%s", fmt
);
1876 for (i
= 0; i
< tp
->nr_args
; i
++) {
1877 parg
= tp
->args
+ i
;
1878 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, " %s=", parg
->name
);
1880 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "{%s",
1882 for (j
= 1; j
< parg
->count
; j
++)
1883 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, ",%s",
1885 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "}");
1887 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "%s",
1891 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "\"%s", arg
);
1893 for (i
= 0; i
< tp
->nr_args
; i
++) {
1894 parg
= tp
->args
+ i
;
1896 if (parg
->type
->is_string
)
1897 fmt
= ", __get_str(%s[%d])";
1899 fmt
= ", REC->%s[%d]";
1900 for (j
= 0; j
< parg
->count
; j
++)
1901 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
,
1902 fmt
, parg
->name
, j
);
1904 if (parg
->type
->is_string
)
1905 fmt
= ", __get_str(%s)";
1908 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
,
1913 /* return the length of print_fmt */
1918 int traceprobe_set_print_fmt(struct trace_probe
*tp
, enum probe_print_type ptype
)
1920 struct trace_event_call
*call
= trace_probe_event_call(tp
);
1924 /* First: called with 0 length to calculate the needed length */
1925 len
= __set_print_fmt(tp
, NULL
, 0, ptype
);
1926 print_fmt
= kmalloc(len
+ 1, GFP_KERNEL
);
1930 /* Second: actually write the @print_fmt */
1931 __set_print_fmt(tp
, print_fmt
, len
+ 1, ptype
);
1932 call
->print_fmt
= print_fmt
;
1937 int traceprobe_define_arg_fields(struct trace_event_call
*event_call
,
1938 size_t offset
, struct trace_probe
*tp
)
1942 /* Set argument names as fields */
1943 for (i
= 0; i
< tp
->nr_args
; i
++) {
1944 struct probe_arg
*parg
= &tp
->args
[i
];
1945 const char *fmt
= parg
->type
->fmttype
;
1946 int size
= parg
->type
->size
;
1951 size
*= parg
->count
;
1952 ret
= trace_define_field(event_call
, fmt
, parg
->name
,
1953 offset
+ parg
->offset
, size
,
1954 parg
->type
->is_signed
,
1962 static void trace_probe_event_free(struct trace_probe_event
*tpe
)
1964 kfree(tpe
->class.system
);
1965 kfree(tpe
->call
.name
);
1966 kfree(tpe
->call
.print_fmt
);
1970 int trace_probe_append(struct trace_probe
*tp
, struct trace_probe
*to
)
1972 if (trace_probe_has_sibling(tp
))
1975 list_del_init(&tp
->list
);
1976 trace_probe_event_free(tp
->event
);
1978 tp
->event
= to
->event
;
1979 list_add_tail(&tp
->list
, trace_probe_probe_list(to
));
1984 void trace_probe_unlink(struct trace_probe
*tp
)
1986 list_del_init(&tp
->list
);
1987 if (list_empty(trace_probe_probe_list(tp
)))
1988 trace_probe_event_free(tp
->event
);
1992 void trace_probe_cleanup(struct trace_probe
*tp
)
1996 for (i
= 0; i
< tp
->nr_args
; i
++)
1997 traceprobe_free_probe_arg(&tp
->args
[i
]);
1999 if (tp
->entry_arg
) {
2000 kfree(tp
->entry_arg
->code
);
2001 kfree(tp
->entry_arg
);
2002 tp
->entry_arg
= NULL
;
2006 trace_probe_unlink(tp
);
2009 int trace_probe_init(struct trace_probe
*tp
, const char *event
,
2010 const char *group
, bool alloc_filter
, int nargs
)
2012 struct trace_event_call
*call
;
2013 size_t size
= sizeof(struct trace_probe_event
);
2016 if (!event
|| !group
)
2020 size
+= sizeof(struct trace_uprobe_filter
);
2022 tp
->event
= kzalloc(size
, GFP_KERNEL
);
2026 INIT_LIST_HEAD(&tp
->event
->files
);
2027 INIT_LIST_HEAD(&tp
->event
->class.fields
);
2028 INIT_LIST_HEAD(&tp
->event
->probes
);
2029 INIT_LIST_HEAD(&tp
->list
);
2030 list_add(&tp
->list
, &tp
->event
->probes
);
2032 call
= trace_probe_event_call(tp
);
2033 call
->class = &tp
->event
->class;
2034 call
->name
= kstrdup(event
, GFP_KERNEL
);
2040 tp
->event
->class.system
= kstrdup(group
, GFP_KERNEL
);
2041 if (!tp
->event
->class.system
) {
2046 tp
->nr_args
= nargs
;
2047 /* Make sure pointers in args[] are NULL */
2049 memset(tp
->args
, 0, sizeof(tp
->args
[0]) * nargs
);
2054 trace_probe_cleanup(tp
);
2058 static struct trace_event_call
*
2059 find_trace_event_call(const char *system
, const char *event_name
)
2061 struct trace_event_call
*tp_event
;
2064 list_for_each_entry(tp_event
, &ftrace_events
, list
) {
2065 if (!tp_event
->class->system
||
2066 strcmp(system
, tp_event
->class->system
))
2068 name
= trace_event_name(tp_event
);
2069 if (!name
|| strcmp(event_name
, name
))
2077 int trace_probe_register_event_call(struct trace_probe
*tp
)
2079 struct trace_event_call
*call
= trace_probe_event_call(tp
);
2082 lockdep_assert_held(&event_mutex
);
2084 if (find_trace_event_call(trace_probe_group_name(tp
),
2085 trace_probe_name(tp
)))
2088 ret
= register_trace_event(&call
->event
);
2092 ret
= trace_add_event_call(call
);
2094 unregister_trace_event(&call
->event
);
2099 int trace_probe_add_file(struct trace_probe
*tp
, struct trace_event_file
*file
)
2101 struct event_file_link
*link
;
2103 link
= kmalloc(sizeof(*link
), GFP_KERNEL
);
2108 INIT_LIST_HEAD(&link
->list
);
2109 list_add_tail_rcu(&link
->list
, &tp
->event
->files
);
2110 trace_probe_set_flag(tp
, TP_FLAG_TRACE
);
2114 struct event_file_link
*trace_probe_get_file_link(struct trace_probe
*tp
,
2115 struct trace_event_file
*file
)
2117 struct event_file_link
*link
;
2119 trace_probe_for_each_link(link
, tp
) {
2120 if (link
->file
== file
)
2127 int trace_probe_remove_file(struct trace_probe
*tp
,
2128 struct trace_event_file
*file
)
2130 struct event_file_link
*link
;
2132 link
= trace_probe_get_file_link(tp
, file
);
2136 list_del_rcu(&link
->list
);
2137 kvfree_rcu_mightsleep(link
);
2139 if (list_empty(&tp
->event
->files
))
2140 trace_probe_clear_flag(tp
, TP_FLAG_TRACE
);
2146 * Return the smallest index of different type argument (start from 1).
2147 * If all argument types and name are same, return 0.
2149 int trace_probe_compare_arg_type(struct trace_probe
*a
, struct trace_probe
*b
)
2153 /* In case of more arguments */
2154 if (a
->nr_args
< b
->nr_args
)
2155 return a
->nr_args
+ 1;
2156 if (a
->nr_args
> b
->nr_args
)
2157 return b
->nr_args
+ 1;
2159 for (i
= 0; i
< a
->nr_args
; i
++) {
2160 if ((b
->nr_args
<= i
) ||
2161 ((a
->args
[i
].type
!= b
->args
[i
].type
) ||
2162 (a
->args
[i
].count
!= b
->args
[i
].count
) ||
2163 strcmp(a
->args
[i
].name
, b
->args
[i
].name
)))
2170 bool trace_probe_match_command_args(struct trace_probe
*tp
,
2171 int argc
, const char **argv
)
2173 char buf
[MAX_ARGSTR_LEN
+ 1];
2176 if (tp
->nr_args
< argc
)
2179 for (i
= 0; i
< argc
; i
++) {
2180 snprintf(buf
, sizeof(buf
), "%s=%s",
2181 tp
->args
[i
].name
, tp
->args
[i
].comm
);
2182 if (strcmp(buf
, argv
[i
]))
2188 int trace_probe_create(const char *raw_command
, int (*createfn
)(int, const char **))
2190 int argc
= 0, ret
= 0;
2193 argv
= argv_split(GFP_KERNEL
, raw_command
, &argc
);
2198 ret
= createfn(argc
, (const char **)argv
);
2205 int trace_probe_print_args(struct trace_seq
*s
, struct probe_arg
*args
, int nr_args
,
2206 u8
*data
, void *field
)
2211 for (i
= 0; i
< nr_args
; i
++) {
2212 struct probe_arg
*a
= args
+ i
;
2214 trace_seq_printf(s
, " %s=", a
->name
);
2215 if (likely(!a
->count
)) {
2216 if (!a
->type
->print(s
, data
+ a
->offset
, field
))
2220 trace_seq_putc(s
, '{');
2221 p
= data
+ a
->offset
;
2222 for (j
= 0; j
< a
->count
; j
++) {
2223 if (!a
->type
->print(s
, p
, field
))
2225 trace_seq_putc(s
, j
== a
->count
- 1 ? '}' : ',');