tracing/probes: Add more typecast features
Subsystems (3)
Masami Hiramatsu (Google)
maintainerv6 cover tracing/probes: Add more typecast features
Hi,
Here is the 6th version of series to introduce more typecast features to probe events. The previous version is here:
In this version, I fixed some issues found by Sashiko reviews (again), drop the first bugfix which is merged to probes/core, add new fetcharg dump patch[1/8] and make typecast always nested[3/8]. This version also allows all probe events to use $current.
Steve introduced BTF typecast feature for eprobe[1]. This series extends it and add more options:
1. Expanding BTF typecast to kprobe and fprobe.
(currently only function entry/exit)
2. Introduce container_of like typecast. This adds a "assigned member" option to the typecast.
(STRUCT,MEMBER)VAR->ANOTHER_MEMBER
This casts VAR to STRUCT type but the VAR is as the address of STRUCT.MEMBER. In C, it is:
container_of(VAR, STRUCT, MEMBER)->ANOTHER_MEMBER
3. Support nested typecast, e.g.
(STRUCT)((STRUCT2)VAR->MEMBER2)->MEMBER
the nest level must be smaller than 3.
4. Add $current variable to point "current" task_struct. This is useful with typecast, e.g.
(task_struct)$current->pid
5. per-cpu dereference support.
Intrdouce this_cpu_read(VAR) and this_cpu_ptr(VAR) to access per-cpu data on the current CPU (accessing other CPU data is not stable, because it can be changed.)
You can access the member of per-cpu data structure using typecast like:
(STRUCT)this_cpu_ptr(VAR)->MEMBER
And added fetcharg dump feature (for debug) and updated test scripts to test part of them.
Thanks,
---
base-id: cfae4cb90fc8113d52fea1f0a62cd6a36d9df149
Masami Hiramatsu (Google) (8):
tracing/probes: Support dumping fetcharg program for debugging dynamic events tracing/probes: Support typecast for various probe events tracing/probes: Support nested typecast
tracing/probes: Type casting always involves nested calls tracing/probes: Support field specifier option for typecast tracing/probes: Add $current variable support
tracing/probes: Add this_cpu_read() and this_cpu_ptr() dereference method to fetcharg tracing/probes: Add a new testcase for BTF typecasts
Documentation/trace/eprobetrace.rst | 9 Documentation/trace/fprobetrace.rst | 10 Documentation/trace/kprobetrace.rst | 11 kernel/trace/Kconfig | 11 kernel/trace/trace.c | 8 kernel/trace/trace_eprobe.c | 2 kernel/trace/trace_fprobe.c | 2 kernel/trace/trace_kprobe.c | 2 kernel/trace/trace_probe.c | 571 ++++++++++++++++---- kernel/trace/trace_probe.h | 95 ++- kernel/trace/trace_probe_tmpl.h | 25 + kernel/trace/trace_uprobe.c | 3 samples/trace_events/trace-events-sample.c | 40 + samples/trace_events/trace-events-sample.h | 34 + .../ftrace/test.d/dynevent/btf_probe_event.tc | 51 ++ .../ftrace/test.d/dynevent/fprobe_syntax_errors.tc | 11 .../ftrace/test.d/kprobe/kprobe_syntax_errors.tc | 11 .../ftrace/test.d/kprobe/uprobe_syntax_errors.tc | 5 18 files changed, 747 insertions(+), 154 deletions(-) create mode 100644 tools/testing/selftests/ftrace/test.d/dynevent/btf_probe_event.tc
--
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerPATCH v6 1/8 tracing/probes: Support dumping fetcharg program for debugging dynamic events
From: Masami Hiramatsu (Google) <[email protected]>
For debugging probe events, it is helpful to verify the compiled fetch instructions for each probe argument. This introduces a new kernel config CONFIG_PROBE_EVENTS_DUMP_FETCHARG to decode the instruction sequence of each argument and display it under a commented line starting with '#' immediately following the dynamic event definition (such as in dynamic_events, kprobe_events, uprobe_events, etc.).
For example:/sys/kernel/tracing # cat dynamic_events p:kprobes/p_vfs_read_0 vfs_read arg1=+0(file):ustring arg2=%ax:x16 # arg1: ARG(0) -> ST_USTRING(offset=0,size=4) -> END
# arg2: REG(80) -> ST_RAW(size=2) -> END
---
Changes in v6:
- Newly added.
--- kernel/trace/Kconfig | 11 +++++ kernel/trace/trace_eprobe.c | 2 + kernel/trace/trace_fprobe.c | 2 + kernel/trace/trace_kprobe.c | 2 + kernel/trace/trace_probe.c | 90 +++++++++++++++++++++++++++++++++++++++++++ kernel/trace/trace_probe.h | 77 ++++++++++++++++++++++--------------- kernel/trace/trace_uprobe.c | 3 + 7 files changed, 157 insertions(+), 30 deletions(-)
kernel/trace/Kconfig +11 -0
@@ -779,6 +779,17 @@ config PROBE_EVENTS_BTF_ARGS kernel function entry or a tracepoint. This is available only if BTF (BPF Type Format) support is enabled. +config PROBE_EVENTS_DUMP_FETCHARG + depends on PROBE_EVENTS + bool "Dump of dynamic probe event fetch-arguments" + default n + help + This shows the dump of fetch-arguments of dynamic probe events + alongside their event definitions in the dynamic_events file + as comment lines. This is useful to debug the probe events. + + If unsure, say N. + config KPROBE_EVENTS depends on KPROBES depends on HAVE_REGS_AND_STACK_ACCESS_API
kernel/trace/trace_eprobe.c +2 -0
@@ -87,6 +87,8 @@ static int eprobe_dyn_event_show(struct seq_file *m, struct dyn_event *ev)seq_printf (m," %s=%s" , ep->tp.args[i].name, ep->tp.args[i].comm);seq_putc (m,'\n' ); +trace_probe_dump_args (m, &ep->tp); +return 0 ; }
kernel/trace/trace_fprobe.c +2 -0
@@ -1449,6 +1449,8 @@ static int trace_fprobe_show(struct seq_file *m, struct dyn_event *ev)seq_printf (m," %s=%s" , tf->tp.args[i].name, tf->tp.args[i].comm);seq_putc (m,'\n' ); +trace_probe_dump_args (m, &tf->tp); +return 0 ; }
kernel/trace/trace_kprobe.c +2 -0
@@ -1320,6 +1320,8 @@ static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev)seq_printf (m," %s=%s" , tk->tp.args[i].name, tk->tp.args[i].comm);seq_putc (m,'\n' ); +trace_probe_dump_args (m, &tk->tp); +return 0 ; }
kernel/trace/trace_probe.c +90 -0
93 lines not on this page · raw message
kernel/trace/trace_probe.h +47 -30
91 lines not on this page · raw message
kernel/trace/trace_uprobe.c +3 -0
@@ -765,6 +765,9 @@ static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev)seq_printf (m," %s=%s" , tu->tp.args[i].name, tu->tp.args[i].comm);seq_putc (m,'\n' ); + +trace_probe_dump_args (m, &tu->tp); +return 0 ; }
Masami Hiramatsu (Google)
maintainerPATCH v6 2/8 tracing/probes: Support typecast for various probe events
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Support BTF typecast feature on other probe events, but only if it is kernel function entry or return, and must use function parameter name or $retval. This means you can do:
(STRUCT)PARAM->MEMBER
Note: you can not use other variables like $stackN, %reg etc. That needs nesting support.
To support other probe events, we just need to use last_struct type when we find a function parameter in parse_btf_arg().
This also updates <tracefs>/README file to show struct typecast.
---
Changes in v5:
- Add comments about $retval with typecast.
- Even if the type of retvalue is not known, if user specifies typecast, use it for its type.
Changes in v3:
- Clarify the limitation.
Changes in v2:
- Fix to re-enable typecast on eprobe.
--- Documentation/trace/fprobetrace.rst | 3 +++ Documentation/trace/kprobetrace.rst | 4 ++++ kernel/trace/trace.c | 2 +- kernel/trace/trace_probe.c | 23 +++++++++++++++++------ kernel/trace/trace_probe.h | 5 +++++ 5 files changed, 30 insertions(+), 7 deletions(-)
Documentation/trace/fprobetrace.rst +3 -0
@@ -57,6 +57,9 @@ Synopsis of fprobe-events (u8/u16/u32/u64/s8/s16/s32/s64), hexadecimal types (x8/x16/x32/x64), "char", "string", "ustring", "symbol", "symstr" and bitfield are supported. + (STRUCT)FIELD->MEMBER[->MEMBER] : If BTF is supported, typecast FIELD to + a pointer to STRUCT and then derference the pointer defined by + ->MEMBER. (\*1) This is available only when BTF is enabled. (\*2) only for the probe on function entry (offs == 0). Note, this argument access
Documentation/trace/kprobetrace.rst +4 -0
@@ -61,6 +61,10 @@ Synopsis of kprobe_events (x8/x16/x32/x64), VFS layer common type(%pd/%pD), "char", "string", "ustring", "symbol", "symstr" and bitfield are supported. + (STRUCT)FIELD->MEMBER[->MEMBER] : If BTF is supported, typecast FIELD to + a pointer to STRUCT and then derference the pointer defined by + ->MEMBER. Note that this is available only when the probe is + on function entry. (\*1) only for the probe on function entry (offs == 0). Note, this argument access is best effort, because depending on the argument type, it may be passed on
kernel/trace/trace.c +1 -1
@@ -4325,7 +4325,7 @@ static const char readme_msg[] =#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API "\t $stack<index>, $stack, $retval, $comm, $arg<N>,\n" #ifdef CONFIG_PROBE_EVENTS_BTF_ARGS - "\t <argname>[->field[->field|.field...]],\n" + "\t [(structname)]<argname>[->field[->field|.field...]],\n"#endif #else "\t $stack<index>, $stack, $retval, $comm,\n"
kernel/trace/trace_probe.c +17 -6
@@ -706,7 +706,7 @@ static int parse_btf_arg(char *varname,if (ctx->flags &TPARG_FL_RETURN && !strcmp (varname,"$retval" )) { code->op =FETCH_OP_RETVAL ; -/* Check whether the function return type is not void */ +/* Check whether the function return type is not void, even with typecast. */ if (query_btf_context (ctx) ==0 ) {if (ctx->proto->type ==0 ) {trace_probe_log_err (ctx->offset,NO_RETVAL );
@@ -715,6 +715,13 @@ static int parse_btf_arg(char *varname, tid = ctx->proto->type; goto found; } +/* +* Even if we can not find appropriate BTF info, we can still access +* the field via typecast. +*/ +if (ctx->struct_btf) + goto found; +if (field) {trace_probe_log_err (ctx->offset + field - varname,NO_BTF_ENTRY );
@@ -759,7 +766,10 @@ static int parse_btf_arg(char *varname,return -ENOENT ; found: - type =btf_type_skip_modifiers (ctx->btf, tid, &tid); +if (ctx->struct_btf) + type = ctx->last_struct; +else + type =btf_type_skip_modifiers (ctx->btf, tid, &tid); found_type:if (!type) {trace_probe_log_err (ctx->offset,BAD_BTF_TID );
@@ -836,10 +846,11 @@ static int handle_typecast(char *arg, struct fetch_insn **pcode,char *tmp;int ret; -/* Currently this only works for eprobes */ -if (!(ctx->flags &TPARG_FL_TEVENT )) { -trace_probe_log_err (ctx->offset,TYPECAST_NOT_EVENT ); -return -EINVAL ; +if (!(tparg_is_event_probe (ctx->flags) || +tparg_is_function_entry (ctx->flags) || +tparg_is_function_return (ctx->flags))) { +trace_probe_log_err (ctx->offset,NOSUP_BTFARG ); +return -EOPNOTSUPP ; } tmp =strchr (arg,')' );
kernel/trace/trace_probe.h +5 -0
@@ -431,6 +431,11 @@ static inline bool tparg_is_function_return(unsigned int flags)return (flags &TPARG_FL_LOC_MASK ) == (TPARG_FL_KERNEL |TPARG_FL_RETURN ); } +static inline bool tparg_is_event_probe (unsigned int flags) +{ +return !!(flags &TPARG_FL_TEVENT ); +} +struct traceprobe_parse_context {struct trace_event_call *event;/* BTF related parameters */
Masami Hiramatsu (Google)
maintainerPATCH v6 3/8 tracing/probes: Support nested typecast
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
When we hit an open parenthesis right after typecast closing parenthesis, it means we have nested typecast. This allows us to typecast a generic data member in a structure to a pointer to another structure.
For example, to cast a DATA_MEMBER of VAR structure to STRUCT pointer and get MEMBER value.
(STRUCT)(VAR->DATA_MEMBER)->MEMBER
Also, we can nest typecast.
(STRUCT1)((STRUCT2)$ARG->FIELD2)->FIELD1
Currently the max nest level is limited to 3.
This also allows user to use typecasting for registers or stacks on kprobe events. e.g.
(STRUCT)(%ax)->MEMBER
(STRUCT)($stack0)->MEMBER
---
Changes in v6:
- Add a WARN_ON_ONCE check for leaking nested_level (it must not happen.)
Changes in v4:
- Use orig_offset for reporting NO_PTR_STRCT error.
Changes in v2:
- Fix to skip "->" after closing parenthetsis.
--- Documentation/trace/eprobetrace.rst | 2 + Documentation/trace/fprobetrace.rst | 2 + Documentation/trace/kprobetrace.rst | 2 + kernel/trace/trace.c | 1 kernel/trace/trace_probe.c | 81 ++++++++++++++++++++++++++++++++--- kernel/trace/trace_probe.h | 7 +++ 6 files changed, 86 insertions(+), 9 deletions(-)
Documentation/trace/eprobetrace.rst +2 -0
@@ -50,6 +50,8 @@ Synopsis of eprobe_events a pointer to STRUCT and then derference the pointer defined by ->MEMBER. Note that when this is used, the FIELD name does not need to be prefixed with a '$'. + (STRUCT)(FETCHARG)->MEMBER[->MEMBER] : typecast can nest, so the above can + also be used with another FETCHARG instead of FIELD. Types -----
Documentation/trace/fprobetrace.rst +2 -0
@@ -60,6 +60,8 @@ Synopsis of fprobe-events (STRUCT)FIELD->MEMBER[->MEMBER] : If BTF is supported, typecast FIELD to a pointer to STRUCT and then derference the pointer defined by ->MEMBER. + (STRUCT)(FETCHARG)->MEMBER[->MEMBER] : typecast can nest, so the above can + also be used with another FETCHARG instead of FIELD. (\*1) This is available only when BTF is enabled. (\*2) only for the probe on function entry (offs == 0). Note, this argument access
Documentation/trace/kprobetrace.rst +2 -0
@@ -65,6 +65,8 @@ Synopsis of kprobe_events a pointer to STRUCT and then derference the pointer defined by ->MEMBER. Note that this is available only when the probe is on function entry. + (STRUCT)(FETCHARG)->MEMBER[->MEMBER] : typecast can nest, so the above can + also be used with another FETCHARG instead of FIELD. (\*1) only for the probe on function entry (offs == 0). Note, this argument access is best effort, because depending on the argument type, it may be passed on
kernel/trace/trace.c +1 -0
@@ -4326,6 +4326,7 @@ static const char readme_msg[] ="\t $stack<index>, $stack, $retval, $comm, $arg<N>,\n" #ifdef CONFIG_PROBE_EVENTS_BTF_ARGS"\t [(structname)]<argname>[->field[->field|.field...]],\n" + "\t [(structname)](fetcharg)->field[->field|.field...],\n"#endif #else "\t $stack<index>, $stack, $retval, $comm,\n"
kernel/trace/trace_probe.c +73 -8
108 lines not on this page · raw message
kernel/trace/trace_probe.h +6 -1
@@ -452,8 +452,11 @@ struct traceprobe_parse_context {struct trace_probe *tp;unsigned int flags;int offset; +int nested_level; }; +#define TRACEPROBE_MAX_NESTED_LEVEL 3 +extern int traceprobe_parse_probe_arg (struct trace_probe *tp,int i,const char *argv,struct traceprobe_parse_context *ctx);
@@ -588,7 +591,9 @@ extern int traceprobe_define_arg_fields(struct trace_event_call *event_call,C (TOO_MANY_ARGS ,"Too many arguments are specified" ), \C (TOO_MANY_EARGS ,"Too many entry arguments specified" ), \C (EVENT_TOO_BIG ,"Event too big (too many fields?)" ), \ -C (TYPECAST_NOT_EVENT ,"Typecasts are only for eprobe fields" ), +C (TYPECAST_NOT_EVENT ,"Typecasts are only for eprobe fields" ), \ +C (TYPECAST_REQ_FIELD ,"Typecast requires a field access" ), \ +C (TOO_MANY_NESTED ,"Too many nested typecasts/dereferences" ), #undef C #define C(a, b) TP_ERR_##a
Masami Hiramatsu (Google)
maintainerPATCH v6 4/8 tracing/probes: Type casting always involves nested calls
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
This allows type casting to various fetchargs without parentheses by recursively calling parse_probe_arg on the target when type casting is used.
For example, this allows the following expressions:
- (STRUCT)%REG->FIELD
- (STRUCT)$stackN->FIELD
- (STRUCT)@SYM->FIELD
---
Changes in v6:
- Newly added.
--- kernel/trace/trace_probe.c | 101 +++++++++++++++++++++++++++----------------- kernel/trace/trace_probe.h | 1 2 files changed, 63 insertions(+), 39 deletions(-)
kernel/trace/trace_probe.c +62 -39
@@ -691,19 +691,6 @@ static int parse_btf_arg(char *varname,return -EOPNOTSUPP ; } -if (ctx->flags &TPARG_FL_TEVENT ) { - ret =parse_trace_event (varname, code, ctx); -if (ret <0 ) { -trace_probe_log_err (ctx->offset,BAD_ATTACH_ARG ); -return ret; - } -/* TEVENT is only here via a typecast */ -if (WARN_ON_ONCE (ctx->struct_btf ==NULL )) -return -EINVAL ; - type = ctx->last_struct; - goto found_type; - } -if (ctx->flags &TPARG_FL_RETURN && !strcmp (varname,"$retval" )) { code->op =FETCH_OP_RETVAL ;/* Check whether the function return type is not void, even with typecast. */
@@ -867,7 +854,7 @@ static int handle_typecast(char *arg, struct fetch_insn **pcode, struct traceprobe_parse_context *ctx) {int orig_offset = ctx->offset; -bool nested = false; +char *close;char *tmp;int ret;
@@ -878,6 +865,17 @@ static int handle_typecast(char *arg, struct fetch_insn **pcode,return -EOPNOTSUPP ; } +/* +* Always consider the token after typecast as a nested call +* For example: (STRUCT)VAR->FIELD and (STRUCT)(VAR)->FIELD are same. +* VAR is solved in the nested call. +*/ + ctx->nested_level++; +if (ctx->nested_level >TRACEPROBE_MAX_NESTED_LEVEL ) { +trace_probe_log_err (ctx->offset,TOO_MANY_NESTED ); +return -E2BIG ; + } + tmp =strchr (arg,')' );if (!tmp) { trace_probe_log_err(ctx->offset + strlen(arg),
@@ -886,11 +884,10 @@ static int handle_typecast(char *arg, struct fetch_insn **pcode, } *tmp++ ='\0' ; -/* Handle the nested structure like (STRUCT)(VAR->FIELD)->... */ + ctx->offset += tmp - arg;if (*tmp =='(' ) { -char *close =find_matched_close_paren (tmp); + close =find_matched_close_paren (tmp); - ctx->offset += tmp - arg;if (!close) {trace_probe_log_err (ctx->offset,DEREF_OPEN_BRACE );return -EINVAL ;
@@ -901,27 +898,57 @@ static int handle_typecast(char *arg, struct fetch_insn **pcode, TYPECAST_REQ_FIELD);return -EINVAL ; } - - ctx->nested_level++; -if (ctx->nested_level >TRACEPROBE_MAX_NESTED_LEVEL ) { -trace_probe_log_err (ctx->offset,TOO_MANY_NESTED ); -return -E2BIG ; +/* Skip '(' */ + ctx->offset +=1 ; + tmp++; + }else if (*tmp =='+' || *tmp =='-' ) { +/* Dereference can have another field access inside it. */ +char *open =strchr (tmp +1 ,'(' ); + +if (!open) { +trace_probe_log_err (ctx->offset, +DEREF_NEED_BRACE ); +return -EINVAL ; } - *close ='\0' ; + close =find_matched_close_paren (open); +if (!close) { +trace_probe_log_err (ctx->offset +strlen (tmp), +DEREF_OPEN_BRACE ); +return -EINVAL ; + } + close++; +/* We expect a field access for typecast */ +if (close[0 ] !='-' || close[1 ] !='>' ) { +trace_probe_log_err (ctx->offset + close - tmp +1 , +TYPECAST_REQ_FIELD ); +return -EINVAL ; + } + }else { +/* Inner variable name */ + close =strchr (tmp,'-' ); +if (!close || close[1 ] !='>' ) { +trace_probe_log_err (ctx->offset +strlen (tmp), +TYPECAST_REQ_FIELD ); +return -EINVAL ; + } + } + *close ='\0' ; - ctx->offset +=1 ;/* for the '(' */ -/* We need to parse the nested one */ - ret =parse_probe_arg (tmp +1 ,find_fetch_type (NULL , ctx->flags), - pcode, end, ctx); -if (ret <0 ) -return ret; - ctx->nested_level--; -clear_struct_btf (ctx); +/* We need to parse the nested one */ + ret =parse_probe_arg (tmp,find_fetch_type (NULL , ctx->flags), + pcode, end, ctx); +if (ret <0 ) +return ret; + ctx->nested_level--; +clear_struct_btf (ctx); - tmp = close +3 ;/* Skip "->" after closing parenthesis */ - nested = true; - } +/* Let tmp point the field name. */ +if (close[1 ] =='-' ) + tmp = close +3 ;/* Skip "->" after closing parenthesis */ +else + tmp = close +2 ;/* Skip ">" after inner variable name */ +/* resolve the typecast struct name */ ret =query_btf_struct (arg +1 , ctx);if (ret <0 ) {trace_probe_log_err (orig_offset +1 ,NO_PTR_STRCT );
@@ -929,11 +956,7 @@ static int handle_typecast(char *arg, struct fetch_insn **pcode, } ctx->offset = orig_offset + tmp - arg; -/* If it is nested, tmp points to the field name. */ -if (nested) - ret =parse_btf_field (tmp, ctx->last_struct, pcode, end, ctx); -else - ret =parse_btf_arg (tmp, pcode, end, ctx); + ret =parse_btf_field (tmp, ctx->last_struct, pcode, end, ctx);return ret; }
kernel/trace/trace_probe.h +1 -0
@@ -455,6 +455,7 @@ struct traceprobe_parse_context {int nested_level; }; +/* Each typecast consumes nested level. So the max number of typecast is 3. */ #define TRACEPROBE_MAX_NESTED_LEVEL 3 extern int traceprobe_parse_probe_arg(struct trace_probe *tp, int i,
Masami Hiramatsu (Google)
maintainerPATCH v6 5/8 tracing/probes: Support field specifier option for typecast
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Add a field specifier option for the typecast. This works like container_of() macro.
(STRUCT[,FIELD[.FIELD2...]])VAR
This is equivalent to :
container_of(VAR, struct STRUCT, FIELD[.FIELD2...])
For example:
echo "f tick_nohz_handler next_tick=(tick_sched,sched_timer)timer->next_tick" >> dynamic_events
This will trace tick_nohz_handler() with its tick_sched::next_tick which is converted from @timer by contianer_of(tick, struct tick_sched, sched_timer). So, if you enabkle both fprobes:tick_nohz_handler__entry and timer:hrtimer_expire_entry events, we will see something like:
<idle>-0 [002] d.h1. 3778.087272: hrtimer_expire_entry: hrtimer=00000000d63db328 f
unction=tick_nohz_handler now=3777450051040
<idle>-0 [002] d.h1. 3778.087281: tick_nohz_handler__entry: (tick_nohz_handler+0x4/0x140) next_tick=3777450000000
---
Changes in v6:
- Update according to the allways nested patch.
Changes in v3:
- Fix error caret position.
Changes in v2:
- Use byteoffset for typecast field offset instead of bitoffset. This fixes negative modulo calculation.
- Check whether a field is specified after typecast.
- Reject if typecast field option has arrow operator.
--- Documentation/trace/eprobetrace.rst | 5 + Documentation/trace/fprobetrace.rst | 8 +- Documentation/trace/kprobetrace.rst | 8 +- kernel/trace/trace.c | 4 - kernel/trace/trace_probe.c | 171 ++++++++++++++++++++++++----------- kernel/trace/trace_probe.h | 5 + 6 files changed, 136 insertions(+), 65 deletions(-)
Documentation/trace/eprobetrace.rst +4 -1
@@ -49,7 +49,10 @@ Synopsis of eprobe_events (STRUCT)FIELD->MEMBER[->MEMBER] : If BTF is supported, typecast FIELD to a pointer to STRUCT and then derference the pointer defined by ->MEMBER. Note that when this is used, the FIELD name does not - need to be prefixed with a '$'. + need to be prefixed with a '$'. ASGN can be specified optionally. + If ASGN is specified, FIELD will be cast to the same offset + position as the ASGN member, rather than to the beginning of + the STRUCT. (STRUCT)(FETCHARG)->MEMBER[->MEMBER] : typecast can nest, so the above can also be used with another FETCHARG instead of FIELD.
Documentation/trace/fprobetrace.rst +5 -3
@@ -57,10 +57,12 @@ Synopsis of fprobe-events (u8/u16/u32/u64/s8/s16/s32/s64), hexadecimal types (x8/x16/x32/x64), "char", "string", "ustring", "symbol", "symstr" and bitfield are supported. - (STRUCT)FIELD->MEMBER[->MEMBER] : If BTF is supported, typecast FIELD to + (STRUCT[,ASGN])FIELD->MEMBER[->MEMBER] : If BTF is supported, typecast FIELD to a pointer to STRUCT and then derference the pointer defined by - ->MEMBER. - (STRUCT)(FETCHARG)->MEMBER[->MEMBER] : typecast can nest, so the above can + ->MEMBER. ASGN can be specified optionally. If ASGN is specified, + FIELD will be cast to the same offset position as the ASGN member, + rather than to the beginning of the STRUCT. + (STRUCT[,ASGN])(FETCHARG)->MEMBER[->MEMBER] : typecast can nest, so the above can also be used with another FETCHARG instead of FIELD. (\*1) This is available only when BTF is enabled.
Documentation/trace/kprobetrace.rst +5 -3
@@ -61,11 +61,13 @@ Synopsis of kprobe_events (x8/x16/x32/x64), VFS layer common type(%pd/%pD), "char", "string", "ustring", "symbol", "symstr" and bitfield are supported. - (STRUCT)FIELD->MEMBER[->MEMBER] : If BTF is supported, typecast FIELD to + (STRUCT[,ASGN])FIELD->MEMBER[->MEMBER] : If BTF is supported, typecast FIELD to a pointer to STRUCT and then derference the pointer defined by ->MEMBER. Note that this is available only when the probe is - on function entry. - (STRUCT)(FETCHARG)->MEMBER[->MEMBER] : typecast can nest, so the above can + on function entry. ASGN can be specified optionally. If ASGN + is specified, FIELD will be cast to the same offset position + as the ASGN member, rather than to the beginning of the STRUCT. + (STRUCT[,ASGN])(FETCHARG)->MEMBER[->MEMBER] : typecast can nest, so the above can also be used with another FETCHARG instead of FIELD. (\*1) only for the probe on function entry (offs == 0). Note, this argument access
kernel/trace/trace.c +2 -2
@@ -4325,8 +4325,8 @@ static const char readme_msg[] =#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API "\t $stack<index>, $stack, $retval, $comm, $arg<N>,\n" #ifdef CONFIG_PROBE_EVENTS_BTF_ARGS -"\t [(structname)]<argname>[->field[->field|.field...]],\n" - "\t [(structname)](fetcharg)->field[->field|.field...],\n" +"\t [(structname[,field])]<argname>[->field[->field|.field...]],\n" + "\t [(structname[,field])](fetcharg)->field[->field|.field...],\n"#endif #else "\t $stack<index>, $stack, $retval, $comm,\n"
kernel/trace/trace_probe.c +116 -55
225 lines not on this page · raw message
kernel/trace/trace_probe.h +4 -1
@@ -453,6 +453,7 @@ struct traceprobe_parse_context {unsigned int flags;int offset;int nested_level; +int prefix_byteoffs;/* The byte offset of the prefix field of typecast */ };/* Each typecast consumes nested level. So the max number of typecast is 3. */
@@ -594,7 +595,9 @@ extern int traceprobe_define_arg_fields(struct trace_event_call *event_call,C (EVENT_TOO_BIG ,"Event too big (too many fields?)" ), \C (TYPECAST_NOT_EVENT ,"Typecasts are only for eprobe fields" ), \C (TYPECAST_REQ_FIELD ,"Typecast requires a field access" ), \ -C (TOO_MANY_NESTED ,"Too many nested typecasts/dereferences" ), +C (TOO_MANY_NESTED ,"Too many nested typecasts/dereferences" ), \ +C (TYPECAST_NOT_ALIGNED ,"Typecast field option is not byte-aligned" ), \ +C (TYPECAST_BAD_ARROW ,"Typecast field option does not support -> operator" ), #undef C #define C(a, b) TP_ERR_##a
Masami Hiramatsu (Google)
maintainerPATCH v6 6/8 tracing/probes: Add $current variable support
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Since we can use the BTF to cast value to a structure pointer type, it is useful to introduce "$current" special variable support to fetcharg.
User can define a fetcharg to access current task_struct properties using BTF info. e.g.
$current->cpus_ptr
---
Changes in v6:
- Rebased on dump fetcharg patch.
- Remove function name/eprobe requirement for $current.
Changes in v5:
- Use s32 for bof_find_btf_id().
Changes in v4:
- Add $current in README when CONFIG_HAVE_FUNCTION_ARG_ACCESS_API=y case.
- Fix to prohibit using $current in eprobes and address based kprobes.
Changes in v3:
- Remove $current support from eprobes (because eprobes is only for event)
- Prohibit uprobes to use $current.
Changes in v2:
- Support to parse $current in parse_btf_arg().
- If no typecast on $current, it automatically casted to task_struct.
- Check error case if $current follows something except for "-".
--- Documentation/trace/fprobetrace.rst | 1 + Documentation/trace/kprobetrace.rst | 1 + kernel/trace/trace.c | 4 ++-- kernel/trace/trace_probe.c | 40 ++++++++++++++++++++++++++++++++++- kernel/trace/trace_probe.h | 1 + kernel/trace/trace_probe_tmpl.h | 3 +++ 6 files changed, 47 insertions(+), 3 deletions(-)
Documentation/trace/fprobetrace.rst +1 -0
@@ -50,6 +50,7 @@ Synopsis of fprobe-events $argN : Fetch the Nth function argument. (N >= 1) (\*2) $retval : Fetch return value.(\*3) $comm : Fetch current task comm. + $current : Fetch the address of the current task_struct. +|-[u]OFFS(FETCHARG) : Fetch memory at FETCHARG +|- OFFS address.(\*4)(\*5) \IMM : Store an immediate value to the argument. NAME=FETCHARG : Set NAME as the argument name of FETCHARG.
Documentation/trace/kprobetrace.rst +1 -0
@@ -53,6 +53,7 @@ Synopsis of kprobe_events $argN : Fetch the Nth function argument. (N >= 1) (\*1) $retval : Fetch return value.(\*2) $comm : Fetch current task comm. + $current : Fetch the address of the current task_struct. +|-[u]OFFS(FETCHARG) : Fetch memory at FETCHARG +|- OFFS address.(\*3)(\*4) \IMM : Store an immediate value to the argument. NAME=FETCHARG : Set NAME as the argument name of FETCHARG.
kernel/trace/trace.c +2 -2
@@ -4323,13 +4323,13 @@ static const char readme_msg[] = "\t args: <name>=fetcharg[:type]\n" "\t fetcharg: (%<register>|$<efield>), @<address>, @<symbol>[+|-<offset>],\n" #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API - "\t $stack<index>, $stack, $retval, $comm, $arg<N>,\n" + "\t $stack<index>, $stack, $retval, $comm, $arg<N>, $current\n" #ifdef CONFIG_PROBE_EVENTS_BTF_ARGS "\t [(structname[,field])]<argname>[->field[->field|.field...]],\n" "\t [(structname[,field])](fetcharg)->field[->field|.field...],\n" #endif #else -"\t $stack<index>, $stack, $retval, $comm,\n" +"\t $stack<index>, $stack, $retval, $comm, $current\n" #endif"\t +|-[u]<offset>(<fetcharg>), \\imm-value, \\\"imm-string\"\n" "\t kernel return probes support: $retval, $arg<N>, $comm\n"
kernel/trace/trace_probe.c +39 -1
@@ -700,7 +700,9 @@ static int parse_btf_arg(char *varname,int i, is_ptr, ret;u32 tid; -if (!ctx->funcname && !(ctx->flags &TPARG_FL_TEVENT )) +/* Note: field is not separated at this point, so check prefix. */ +if (!str_has_prefix (varname,"$current" ) && + !ctx->funcname && !(ctx->flags &TPARG_FL_TEVENT ))return -EINVAL ; is_ptr =split_next_field (varname, &field, ctx);
@@ -713,6 +715,24 @@ static int parse_btf_arg(char *varname,return -EOPNOTSUPP ; } +if (!strcmp (varname,"$current" )) { + code->op =FETCH_OP_CURRENT ; +/* If no typecast is specified for $current, use task_struct by default */ +if (!ctx->struct_btf) { +s32 ttid =bpf_find_btf_id ("task_struct" ,BTF_KIND_STRUCT , + &ctx->struct_btf); + +if (ttid <0 ) { +trace_probe_log_err (ctx->offset,NO_BTF_ENTRY ); +return -ENOENT ; + } +/* btf_type_skip_modifier() requires u32 for type id. */ + tid = ttid; + ctx->last_struct =btf_type_skip_modifiers (ctx->struct_btf, tid, &tid); + } + goto found; + } +if (ctx->flags &TPARG_FL_RETURN && !strcmp (varname,"$retval" )) { code->op =FETCH_OP_RETVAL ;/* Check whether the function return type is not void, even with typecast. */
@@ -1273,6 +1293,24 @@ static int parse_probe_vars(char *orig_arg, const struct fetch_type *t,return 0 ; } +/* $current returns the address of the current task_struct. */ +if (str_has_prefix (arg,"current" )) { +/* $current is only supported by kernel probe. */ +if (!(ctx->flags &TPARG_FL_KERNEL )) { + err =TP_ERR_BAD_VAR ; + goto inval; + } + arg +=strlen ("current" ); +if (*arg =='-' &&IS_ENABLED (CONFIG_PROBE_EVENTS_BTF_ARGS )) +return parse_btf_arg (orig_arg, pcode, end, ctx); + +if (*arg !='\0' ) + goto inval; + + code->op =FETCH_OP_CURRENT ; +return 0 ; + } +#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API len =str_has_prefix (arg,"arg" );if (len) {
kernel/trace/trace_probe.h +1 -0
@@ -92,6 +92,7 @@ typedef int (*print_type_func_t)(struct trace_seq *, void *, void *); FETCH_OP(RETVAL, none) /* Return value */ \ FETCH_OP(IMM, imm) /* Immediate: .immediate */ \ FETCH_OP(COMM, none) /* Current comm */ \ + FETCH_OP(CURRENT, none) /* Current task_struct address */\ FETCH_OP(ARG, param) /* Argument: .param = index */ \ FETCH_OP(FOFFS, imm) /* File offset: .immediate */ \FETCH_OP (DATA , ptr)/* Allocated data: .data */ \
kernel/trace/trace_probe_tmpl.h +3 -0
@@ -112,6 +112,9 @@ process_common_fetch_insn(struct fetch_insn *code, unsigned long *val)case FETCH_OP_DATA : *val = (unsigned long )code->data;break ; +case FETCH_OP_CURRENT : + *val = (unsigned long )current; +break ;default :return -EILSEQ ; }
Masami Hiramatsu (Google)
maintainerPATCH v6 7/8 tracing/probes: Add this_cpu_read() and this_cpu_ptr() dereference method to fetcharg
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
When tracing the kernel local variables, sometimes we need to get the CPU local variables. To access it, current simple dereference is not enough.
Thus, introduce a special this_cpu_read() dereference to access per-cpu variable for the current CPU (accessing other CPU variable may race with updates on other CPUs). Also this_cpu_ptr() is for accessing per-cpu pointer.
Those are working as same as the kernel percpu macro.
---
Changes in v6:
- Rebased on dump fetcharg patch.
- Fix to fetch static percpu variable with @SYM correctly.
Changes in v5:
- Simplify this_cpu_read() into +0(this_cpu_ptr()).
Changes in v3:
- Remove NULL check for percpu var because it is just an offset, could be 0.
- Simplify process_fetch_insn_bottom() code.
- If the last operation is this_cpu_read(), read only memory of the specific size (of type).
Changes in v2:
- Drop +CPU/+PCPU and introduce this_cpu_read() and this_cpu_ptr().
- Support these method with BTF typecast.
- Just check the base address is NOT NULL instead of is_kernel_percpu_address().
--- Documentation/trace/eprobetrace.rst | 2 Documentation/trace/fprobetrace.rst | 2 Documentation/trace/kprobetrace.rst | 2 kernel/trace/trace.c | 1 kernel/trace/trace_probe.c | 143 ++++++++++++++++++++++++++--------- kernel/trace/trace_probe.h | 1 kernel/trace/trace_probe_tmpl.h | 22 ++++- 7 files changed, 129 insertions(+), 44 deletions(-)
Documentation/trace/eprobetrace.rst +2 -0
@@ -39,6 +39,8 @@ Synopsis of eprobe_events @SYM[+|-offs] : Fetch memory at SYM +|- offs (SYM should be a data symbol) $comm : Fetch current task comm. +|-[u]OFFS(FETCHARG) : Fetch memory at FETCHARG +|- OFFS address.(\*3)(\*4) + this_cpu_read(FETCHARG) : Read the value of the per-CPU variable FETCHARG on the current CPU. + this_cpu_ptr(FETCHARG) : Get the address of the per-CPU variable FETCHARG on the current CPU. \IMM : Store an immediate value to the argument. NAME=FETCHARG : Set NAME as the argument name of FETCHARG. FETCHARG:TYPE : Set TYPE as the type of FETCHARG. Currently, basic types
Documentation/trace/fprobetrace.rst +2 -0
@@ -52,6 +52,8 @@ Synopsis of fprobe-events $comm : Fetch current task comm. $current : Fetch the address of the current task_struct. +|-[u]OFFS(FETCHARG) : Fetch memory at FETCHARG +|- OFFS address.(\*4)(\*5) + this_cpu_read(FETCHARG) : Read the value of the per-CPU variable FETCHARG on the current CPU. + this_cpu_ptr(FETCHARG) : Get the address of the per-CPU variable FETCHARG on the current CPU. \IMM : Store an immediate value to the argument. NAME=FETCHARG : Set NAME as the argument name of FETCHARG. FETCHARG:TYPE : Set TYPE as the type of FETCHARG. Currently, basic types
Documentation/trace/kprobetrace.rst +2 -0
@@ -55,6 +55,8 @@ Synopsis of kprobe_events $comm : Fetch current task comm. $current : Fetch the address of the current task_struct. +|-[u]OFFS(FETCHARG) : Fetch memory at FETCHARG +|- OFFS address.(\*3)(\*4) + this_cpu_read(FETCHARG) : Read the value of the per-CPU variable FETCHARG on the current CPU. + this_cpu_ptr(FETCHARG) : Get the address of the per-CPU variable FETCHARG on the current CPU. \IMM : Store an immediate value to the argument. NAME=FETCHARG : Set NAME as the argument name of FETCHARG. FETCHARG:TYPE : Set TYPE as the type of FETCHARG. Currently, basic types
kernel/trace/trace.c +1 -0
@@ -4332,6 +4332,7 @@ static const char readme_msg[] = "\t $stack<index>, $stack, $retval, $comm, $current\n"#endif "\t +|-[u]<offset>(<fetcharg>), \\imm-value, \\\"imm-string\"\n" +"\t this_cpu_read(<fetcharg>), this_cpu_ptr(<fetcharg>)\n" "\t kernel return probes support: $retval, $arg<N>, $comm\n" "\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, char, string, symbol,\n" "\t b<bit-width>@<bit-offset>/<container-size>, ustring,\n"
kernel/trace/trace_probe.c +105 -38
180 lines not on this page · raw message
kernel/trace/trace_probe.h +1 -0
@@ -101,6 +101,7 @@ typedef int (*print_type_func_t)(struct trace_seq *, void *, void *);/* Stage 2 (dereference) ops */ \ FETCH_OP(DEREF, offset) /* Dereference: .offset */ \ FETCH_OP(UDEREF, offset) /* User-space dereference: .offset */\ +FETCH_OP (CPU_PTR , none)/* Per-CPU pointer: .offset */ \ /* Stage 3 (store) ops */ \ FETCH_OP(ST_RAW, store) /* Raw value: .size */ \FETCH_OP (ST_MEM , store)/* Memory: .offset, .size */ \
kernel/trace/trace_probe_tmpl.h +16 -6
@@ -129,25 +129,35 @@ process_fetch_insn_bottom(struct fetch_insn *code, unsigned long val,struct fetch_insn *s3 =NULL ;int total =0 , ret =0 , i =0 ;u32 loc =0 ; -unsigned long lval = val; +unsigned long lval, llval = val; stage2:/* 2nd stage: dereference memory if needed */ do { -if (code->op ==FETCH_OP_DEREF ) { - lval = val; + lval = val; +switch (code->op) { +case FETCH_OP_DEREF : ret =probe_mem_read (&val, (void *)val + code->offset,sizeof (val)); - }else if (code->op ==FETCH_OP_UDEREF ) { - lval = val; +break ; +case FETCH_OP_UDEREF : ret =probe_mem_read_user (&val, (void *)val + code->offset,sizeof (val)); - }else break ; +case FETCH_OP_CPU_PTR : + val = (unsigned long )this_cpu_ptr ((void __percpu *)val); + ret =0 ; +break ; +default : + lval = llval; + goto out; + }if (ret)return ret; + llval = lval; code++; }while (1 ); +out: s3 = code; stage3:
Masami Hiramatsu (Google)
maintainerPATCH v6 8/8 tracing/probes: Add a new testcase for BTF typecasts
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
With the introduction of container_of-style BTF typecasting and per-CPU variable access support in trace probes, we need a way to verify their functionality and prevent regressions.
Add a new ftrace kselftest and update the trace event sample module to test and validate these features.
Specifically, update the trace-events-sample module to set up a periodic timer whose callback accesses a per-CPU counter. Introduce a new sample trace event, foo_timer_fn, to trace this callback and log the current counter value.
Then, add a new test case, btf_probe_event.tc, which defines a dynamic probe on the timer callback. The probe uses BTF typecasting to recover the parent structure from the timer argument and this_cpu_read() to fetch the per-CPU counter. The test verifies the integrity of the implementation by ensuring the values recorded by the dynamic probe match those from the static tracepoint.
---
Changes in v6:
- Update testcase according to changes.
Changes in v5:
- Add more syntax test cases.
Changes in v4:
- Fix uprobe $current test.
Changes in v3:
- Add syntax test case.
- Update testcase to use this_cpu_read()
Changes in v2:
- Use timer_shutdown_sync() instead of timer_delete_sync() for teardown.
--- samples/trace_events/trace-events-sample.c | 40 +++++++++++++++- samples/trace_events/trace-events-sample.h | 34 ++++++++++++- .../ftrace/test.d/dynevent/btf_probe_event.tc | 51 ++++++++++++++++++++ .../ftrace/test.d/dynevent/fprobe_syntax_errors.tc | 11 ++++ .../ftrace/test.d/kprobe/kprobe_syntax_errors.tc | 11 ++++ .../ftrace/test.d/kprobe/uprobe_syntax_errors.tc | 5 ++ 6 files changed, 147 insertions(+), 5 deletions(-) create mode 100644 tools/testing/selftests/ftrace/test.d/dynevent/btf_probe_event.tc
samples/trace_events/trace-events-sample.c +38 -2
@@ -94,6 +94,20 @@ static int simple_thread_fn(void *arg)static DEFINE_MUTEX (thread_mutex);static int simple_thread_cnt; +static struct foo_timer_data *foo_timer_data; + +static void sample_timer_cb (struct timer_list *t) +{ + struct foo_timer_data *data = container_of(t, struct foo_timer_data, timer); + +get_cpu (); +trace_foo_timer_fn (data); + (*this_cpu_ptr (data->counter))++; +put_cpu (); + +mod_timer (t, jiffies +HZ ); +} +int foo_bar_reg (void ) {mutex_lock (&thread_mutex);
@@ -132,9 +146,27 @@ void foo_bar_unreg(void)static int __init trace_event_init (void ) { + foo_timer_data =kzalloc_obj (*foo_timer_data,GFP_KERNEL ); +if (!foo_timer_data) +return -ENOMEM ; + + foo_timer_data->name ="sample_timer_counter" ; + foo_timer_data->counter =alloc_percpu (int); +if (!foo_timer_data->counter) { +kfree (foo_timer_data); +return -ENOMEM ; + } + +timer_setup (&foo_timer_data->timer, sample_timer_cb,0 ); +mod_timer (&foo_timer_data->timer, jiffies +HZ ); + simple_tsk =kthread_run (simple_thread,NULL ,"event-sample" ); -if (IS_ERR (simple_tsk)) -return -1 ; +if (IS_ERR (simple_tsk)) { +timer_shutdown_sync (&foo_timer_data->timer); +free_percpu (foo_timer_data->counter); +kfree (foo_timer_data); +return PTR_ERR (simple_tsk); + }return 0 ; }
@@ -147,6 +179,10 @@ static void __exit trace_event_exit(void)kthread_stop (simple_tsk_fn); simple_tsk_fn =NULL ;mutex_unlock (&thread_mutex); + +timer_shutdown_sync (&foo_timer_data->timer); +free_percpu (foo_timer_data->counter); +kfree (foo_timer_data); }module_init (trace_event_init);
samples/trace_events/trace-events-sample.h +31 -3
@@ -247,12 +247,14 @@*/ /* -* It is OK to have helper functions in the file, but they need to be protected -* from being defined more than once. Remember, this file gets included more -* than once. +* It is OK to have helper functions and data structures in the file, but they +* need to be protected from being defined more than once. Remember, this file +* gets included more than once. */ #ifndef __TRACE_EVENT_SAMPLE_HELPER_FUNCTIONS#define __TRACE_EVENT_SAMPLE_HELPER_FUNCTIONS +#include <linux/timer.h> +static inline int __length_of (const int *list) {int i;
@@ -270,6 +272,13 @@ enum {TRACE_SAMPLE_BAR =4 , TRACE_SAMPLE_ZOO = 8, }; + +struct foo_timer_data { +const char *name; +struct timer_list timer; +int __percpu *counter; +}; +#endif /*
@@ -595,6 +604,25 @@ TRACE_EVENT(foo_rel_loc,__get_rel_bitmask (bitmask), __get_rel_cpumask(cpumask)) ); + +TRACE_EVENT (foo_timer_fn, + + TP_PROTO(struct foo_timer_data *data), + +TP_ARGS (data), + +TP_STRUCT__entry ( + __string( name, data->name ) +__field ( int, count ) + ), + +TP_fast_assign ( + __assign_str(name); + __entry->count = *this_cpu_ptr(data->counter); + ), + +TP_printk ("name=%s count=%d" ,__get_str (name), __entry->count) +);#endif /***** NOTICE! The #if protection ends here. *****/
tools/testing/selftests/ftrace/test.d/dynevent/btf_probe_event.tc new file +51 -0
51 lines not on this page · raw message
tools/testing/selftests/ftrace/test.d/dynevent/fprobe_syntax_errors.tc +11 -0
@@ -112,6 +112,17 @@ check_error 'f vfs_read%return $retval->^foo' # NO_PTR_STRCT check_error 'f vfs_read file->^foo' # NO_BTF_FIELD check_error 'f vfs_read file^-.foo' # BAD_HYPHEN check_error 'f vfs_read ^file:string' # BAD_TYPE4STR +if grep -qF "[(structname" README ; then +check_error 'f vfs_read arg1=(task_struct)file^' # TYPECAST_REQ_FIELD +check_error 'f vfs_read arg1=(a)((b)((c)(^(d)file->d)->c)->b)->a' # TOO_MANY_NESTED +check_error 'f vfs_read arg1=(task_struct,^in_execve)file->comm' # TYPECAST_NOT_ALIGNED +check_error 'f vfs_read arg1=(task_struct,^foo_bar)file->pid' # NO_BTF_FIELD +check_error 'f vfs_read arg1=(^task_struct1234)file->pid' # NO_PTR_STRCT +check_error 'f vfs_read arg1=(task_struct,se^->group_node)file->comm' # TYPECAST_BAD_ARROW +check_error 'f vfs_read arg1=(task_struct,^->pid)file->comm' # NO_BTF_FIELD +check_error 'f vfs_read arg1=(task_struct,^.pid)file->comm' # NO_BTF_FIELD +check_error 'f vfs_read arg1=(task_struct,^.)file->comm' # NO_BTF_FIELD +fi fi else
tools/testing/selftests/ftrace/test.d/kprobe/kprobe_syntax_errors.tc +11 -0
@@ -115,6 +115,17 @@ check_error 'p vfs_read+20 ^$arg*' # NOFENTRY_ARGS check_error 'p vfs_read ^hoge' # NO_BTFARG check_error 'p kfree ^$arg10' # NO_BTFARG (exceed the number of parameters) check_error 'r kfree ^$retval' # NO_RETVAL +if grep -qF "[(structname" README ; then +check_error 'p vfs_read arg1=(task_struct)file^' # TYPECAST_REQ_FIELD +check_error 'p vfs_read arg1=(a)((b)((c)(^(d)file->d)->c)->b)->a' # TOO_MANY_NESTED +check_error 'p vfs_read arg1=(task_struct,^in_execve)file->comm' # TYPECAST_NOT_ALIGNED +check_error 'p vfs_read arg1=(task_struct,^foo_bar)file->pid' # NO_BTF_FIELD +check_error 'p vfs_read arg1=(^task_struct1234)file->pid' # NO_PTR_STRCT +check_error 'p vfs_read arg1=(task_struct,se^->group_node)file->comm' # TYPECAST_BAD_ARROW +check_error 'p vfs_read arg1=(task_struct,^->pid)file->comm' # NO_BTF_FIELD +check_error 'p vfs_read arg1=(task_struct,^.pid)file->comm' # NO_BTF_FIELD +check_error 'p vfs_read arg1=(task_struct,^.)file->comm' # NO_BTF_FIELD +fi else check_error 'p vfs_read ^$arg*' # NOSUP_BTFARG fi
tools/testing/selftests/ftrace/test.d/kprobe/uprobe_syntax_errors.tc +5 -0
@@ -28,4 +28,9 @@ if grep -q ".*symstr.*" README; then check_error 'p /bin/sh:10 $stack0:^symstr' # BAD_TYPE fi +# $current is not supported by uprobe +if grep -q "\$current.*" README; then +check_error 'p /bin/sh:10 ^$current:u8' # BAD_VAR +fi + exit 0
Masami Hiramatsu (Google)
maintainerRe: [PATCH v6 1/8] tracing/probes: Support dumping fetcharg program for debugging dynamic events
In reply to Masami Hiramatsu (Google)Hi
Sashiko found another bug about FETCH_OP_TP_ARG. which seems using fetch_insn parameter wrongly (using different parameters). I need to fix it first.
We also may need to rename FETCH_OP_DATA to FETCH_OP_IMMSTR.
Thanks,
Quoted 302 lines · On Sun, 21 Jun 2026 00:17:13 +0900 "Masami Hiramatsu (Google)"…
From: Masami Hiramatsu (Google) <[email protected]>
For debugging probe events, it is helpful to verify the compiled fetch instructions for each probe argument. This introduces a new kernel config
CONFIG_PROBE_EVENTS_DUMP_FETCHARGto decode the instruction sequence of each argument and display it under a commented line starting with '#' immediately following the dynamic event definition (such as indynamic_events,kprobe_events,uprobe_events, etc.).For example:
/sys/kernel/tracing# catdynamic_eventsp:kprobes/p_vfs_read_0vfs_readarg1=+0(file):ustring arg2=%ax:x16 # arg1: ARG(0) ->ST_USTRING(offset=0,size=4)-> END
# arg2: REG(80) ->ST_RAW(size=2)-> ENDAssisted-by Antigravity:gemini-3.5-flashSigned-off-by Masami Hiramatsu (Google) <[email protected]>---
Changes in v6:
- Newly added.--- kernel/trace/Kconfig | 11 +++++ kernel/trace/trace_eprobe.c | 2 + kernel/trace/trace_fprobe.c | 2 + kernel/trace/trace_kprobe.c | 2 + kernel/trace/trace_probe.c | 90 +++++++++++++++++++++++++++++++++++++++++++ kernel/trace/trace_probe.h | 77 ++++++++++++++++++++++--------------- kernel/trace/trace_uprobe.c | 3 + 7 files changed, 157 insertions(+), 30 deletions(-)kernel/trace/Kconfig +11
-0@@ -779,6 +779,17 @@ config PROBE_EVENTS_BTF_ARGS kernel function entry or a tracepoint. This is available only if BTF (BPF Type Format) support is enabled. +config PROBE_EVENTS_DUMP_FETCHARG + depends on PROBE_EVENTS + bool "Dump of dynamic probe event fetch-arguments" + default n + help + This shows the dump of fetch-arguments of dynamic probe events + alongside their event definitions in the dynamic_events file + as comment lines. This is useful to debug the probe events. + + If unsure, say N. + config KPROBE_EVENTS depends on KPROBES depends on HAVE_REGS_AND_STACK_ACCESS_APIkernel/trace/trace_eprobe.c +2
-0@@ -87,6 +87,8 @@ static int eprobe_dyn_event_show(struct seq_file *m, struct dyn_event *ev)seq_printf (m," %s=%s" , ep->tp.args[i].name, ep->tp.args[i].comm);seq_putc (m,'\n' ); +trace_probe_dump_args (m, &ep->tp); +return 0 ; }kernel/trace/trace_fprobe.c +2
-0@@ -1449,6 +1449,8 @@ static int trace_fprobe_show(struct seq_file *m, struct dyn_event *ev)seq_printf (m," %s=%s" , tf->tp.args[i].name, tf->tp.args[i].comm);seq_putc (m,'\n' ); +trace_probe_dump_args (m, &tf->tp); +return 0 ; }kernel/trace/trace_kprobe.c +2
-0@@ -1320,6 +1320,8 @@ static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev)seq_printf (m," %s=%s" , tk->tp.args[i].name, tk->tp.args[i].comm);seq_putc (m,'\n' ); +trace_probe_dump_args (m, &tk->tp); +return 0 ; }kernel/trace/trace_probe.c +90
-0@@ -2393,3 +2393,93 @@ int trace_probe_print_args(struct trace_seq *s, struct probe_arg *args, int nr_a }return 0 ; } + +#ifdef CONFIG_PROBE_EVENTS_DUMP_FETCHARG + +struct fetch_op_decode { +const char *name; +void (*decode)(struct seq_file *m,struct fetch_insn *insn); +}; + +static const struct fetch_op_decode fetch_op_decode[]; + +static void fetcharg_decode_none (struct seq_file *m,struct fetch_insn *insn) +{ +seq_puts (m, fetch_op_decode[insn->op].name); +} + +static void fetcharg_decode_param (struct seq_file *m,struct fetch_insn *insn) +{ +seq_printf (m,"%s(%u)" , fetch_op_decode[insn->op].name, insn->param); +} + +static void fetcharg_decode_imm (struct seq_file *m,struct fetch_insn *insn) +{ +seq_printf (m,"%s(0x%lx)" , fetch_op_decode[insn->op].name, insn->immediate); +} + +static void fetcharg_decode_ptr (struct seq_file *m,struct fetch_insn *insn) +{ +seq_printf (m,"%s(%p)" , fetch_op_decode[insn->op].name, insn->data); +} + +static void fetcharg_decode_symbol (struct seq_file *m,struct fetch_insn *insn) +{ +seq_printf (m,"%s(%s)" , fetch_op_decode[insn->op].name, (char *)insn->data); +} + +static void fetcharg_decode_offset (struct seq_file *m,struct fetch_insn *insn) +{ +seq_printf (m,"%s(offset=%d)" , fetch_op_decode[insn->op].name, insn->offset); +} + +static void fetcharg_decode_store (struct seq_file *m,struct fetch_insn *insn) +{ +if (insn->op ==FETCH_OP_ST_RAW ) +seq_printf (m,"%s(size=%u)" , fetch_op_decode[insn->op].name, insn->size); +else +seq_printf (m,"%s(offset=%d,size=%u)" , fetch_op_decode[insn->op].name, insn->offset, insn->size); +} + +static void fetcharg_decode_bf (struct seq_file *m,struct fetch_insn *insn) +{ +seq_printf (m,"%s(basesize=%u,lshift=%u,rshift=%u)" , + fetch_op_decode[insn->op].name, insn->basesize, insn->lshift, insn->rshift); +} + +#define FETCH_OP (opname, decode_fn) \ + [FETCH_OP_##opname] = { .name = #opname, .decode = fetcharg_decode_##decode_fn }, + +static const struct fetch_op_decode fetch_op_decode[] = { +FETCH_OP_LIST +}; +#undef FETCH_OP + +static void trace_probe_dump_arg (struct seq_file *m,struct probe_arg *parg) +{ +int i; + +seq_printf (m,"# %s: " , parg->name); +for (i =0 ; i <FETCH_INSN_MAX ; i++) { +struct fetch_insn *insn = parg->code + i; + +if (insn->op >=ARRAY_SIZE (fetch_op_decode) || !fetch_op_decode[insn->op].decode) +seq_printf (m,"unknown(%d)" , insn->op); +else + fetch_op_decode[insn->op].decode (m, insn); + +if (insn->op ==FETCH_OP_END ) +break ; +seq_puts (m," -> " ); + } +seq_putc (m,'\n' ); +} + +void trace_probe_dump_args (struct seq_file *m,struct trace_probe *tp) +{ +int i; + +for (i =0 ; i < tp->nr_args; i++) +trace_probe_dump_arg (m, &tp->args[i]); +} +#endif /* CONFIG_PROBE_EVENTS_DUMP_FETCHARG */ kernel/trace/trace_probe.h +47
-30@@ -83,38 +83,47 @@ static nokprobe_inline u32 update_data_loc(u32 loc, int consumed)/* Printing function type */ typedef int (*print_type_func_t )(struct trace_seq *,void *,void *); +#define FETCH_OP_LIST \ +/* Stage 1 (load) ops */ \ + FETCH_OP(NOP, none) /* NOP */ \ + FETCH_OP(REG, param) /* Register: .param = offset */ \ + FETCH_OP(STACK, param) /* Stack: .param = index */ \ + FETCH_OP(STACKP, none) /* Stack pointer */ \ + FETCH_OP(RETVAL, none) /* Return value */ \ + FETCH_OP(IMM, imm) /* Immediate: .immediate */ \ + FETCH_OP(COMM, none) /* Current comm */ \ + FETCH_OP(ARG, param) /* Argument: .param = index */ \ + FETCH_OP(FOFFS, imm) /* File offset: .immediate */ \ + FETCH_OP(DATA, ptr) /* Allocated data: .data */ \ + FETCH_OP(EDATA, offset) /* Entry data: .offset */ \ + FETCH_OP(TP_ARG, param) /* Tracepoint argument: .data */\ + /* Stage 2 (dereference) ops */ \ + FETCH_OP(DEREF, offset) /* Dereference: .offset */ \ + FETCH_OP(UDEREF, offset) /* User-space dereference: .offset */\ + /* Stage 3 (store) ops */ \ + FETCH_OP(ST_RAW, store) /* Raw value: .size */ \ + FETCH_OP(ST_MEM, store) /* Memory: .offset, .size */ \ + FETCH_OP(ST_UMEM, store) /* User memory: .offset, .size */\ + FETCH_OP(ST_STRING, store) /* String: .offset, .size */ \ + FETCH_OP(ST_USTRING, store) /* User string: .offset, .size */\ + FETCH_OP(ST_SYMSTR, store) /* Symbol name: .offset, .size */\ + FETCH_OP(ST_EDATA, offset) /* Entry data: .offset */ \ + /* Stage 4 (modify) op */ \ + FETCH_OP(MOD_BF, bf) /* Bitfield: .basesize, .lshift, .rshift*/\ + /* Stage 5 (loop) op */ \ + FETCH_OP(LP_ARRAY, param) /* Loop array: .param = count */\ + /* End */ \ + FETCH_OP(END, none) \ +/* Unresolved Symbol holder */ \ + FETCH_OP(NOP_SYMBOL, symbol)/* Non loaded symbol: .data = symbol name */ + +#define FETCH_OP (opname, decode_fn) FETCH_OP_##opname,enum fetch_op { -FETCH_OP_NOP =0 , -// Stage 1 (load) ops -FETCH_OP_REG ,/* Register : .param = offset */ -FETCH_OP_STACK ,/* Stack : .param = index */ -FETCH_OP_STACKP ,/* Stack pointer */ -FETCH_OP_RETVAL ,/* Return value */ -FETCH_OP_IMM ,/* Immediate : .immediate */ -FETCH_OP_COMM ,/* Current comm */ -FETCH_OP_ARG ,/* Function argument : .param */ -FETCH_OP_FOFFS ,/* File offset: .immediate */ -FETCH_OP_DATA ,/* Allocated data: .data */ -FETCH_OP_EDATA ,/* Entry data: .offset */ -// Stage 2 (dereference) op -FETCH_OP_DEREF ,/* Dereference: .offset */ -FETCH_OP_UDEREF ,/* User-space Dereference: .offset */ -// Stage 3 (store) ops -FETCH_OP_ST_RAW ,/* Raw: .size */ -FETCH_OP_ST_MEM ,/* Mem: .offset, .size */ -FETCH_OP_ST_UMEM ,/* Mem: .offset, .size */ -FETCH_OP_ST_STRING ,/* String: .offset, .size */ -FETCH_OP_ST_USTRING ,/* User String: .offset, .size */ -FETCH_OP_ST_SYMSTR ,/* Kernel Symbol String: .offset, .size */ -FETCH_OP_ST_EDATA ,/* Store Entry Data: .offset */ -// Stage 4 (modify) op -FETCH_OP_MOD_BF ,/* Bitfield: .basesize, .lshift, .rshift */ -// Stage 5 (loop) op -FETCH_OP_LP_ARRAY ,/* Array: .param = loop count */ -FETCH_OP_TP_ARG ,/* Trace Point argument */ -FETCH_OP_END , -FETCH_NOP_SYMBOL ,/* Unresolved Symbol holder */ +FETCH_OP_LIST }; +#undef FETCH_OP + +#define FETCH_NOP_SYMBOL FETCH_OP_NOP_SYMBOLstruct fetch_insn {enum fetch_op op;@@ -370,6 +379,14 @@ bool trace_probe_match_command_args(struct trace_probe *tp,int trace_probe_create (const char *raw_command,int (*createfn)(int ,const char **));int trace_probe_print_args (struct trace_seq *s,struct probe_arg *args,int nr_args,u8 *data,void *field); +#ifdef CONFIG_PROBE_EVENTS_DUMP_FETCHARG +void trace_probe_dump_args (struct seq_file *m,struct trace_probe *tp); +#else +static inline void trace_probe_dump_args (struct seq_file *m,struct trace_probe *tp) +{ +return ; +} +#endif #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API int traceprobe_get_entry_data_size (struct trace_probe *tp);kernel/trace/trace_uprobe.c +3
-0@@ -765,6 +765,9 @@ static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev)seq_printf (m," %s=%s" , tu->tp.args[i].name, tu->tp.args[i].comm);seq_putc (m,'\n' ); + +trace_probe_dump_args (m, &tu->tp); +return 0 ; }
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>