tracing: Add "within" filter for call-stack-based event filtering
Subsystems (2)
PATCH 1/1 tracing: Add "within" filter for call-stack-based event filtering
Low-level kernel functions are called from many different paths. When debugging, it is often useful to filter trace events to only those occurring within a specific call chain.
Add a "within" filter predicate that tests whether a given function appears in the current call stack at event time. The function name is resolved to its address range via kallsyms during filter setup; at runtime, stack_trace_save() captures the call stack and compares each return address against the stored range.
Example:
echo 'within == "vfs_read"' > events/sched/sched_switch/filter
Only "==" and "!=" operators are supported. The filter depends on CONFIG_STACKTRACE.
--- Documentation/trace/events.rst | 12 +++++++++ include/linux/trace_events.h | 1 + kernel/trace/trace.h | 3 ++- kernel/trace/trace_events.c | 3 +++ kernel/trace/trace_events_filter.c | 41 ++++++++++++++++++++++++++++-- 5 files changed, 57 insertions(+), 3 deletions(-)
Documentation/trace/events.rst +12 -0
@@ -243,6 +243,18 @@ the function "security_prepare_creds" and less than the end of that function. The ".function" postfix can only be attached to values of size long, and can only be compared with "==" or "!=". +The special field "within" can be used to filter events based on whether +a specific function appears in the current call stack:: + + within == "function_name" + within != "function_name" + +For example, to only trace events where "vfs_read" is in the call stack:: + + # echo 'within == "vfs_read"' > events/sched/sched_switch/filter + +The within field supports only the "==" and "!=" operators. + Cpumask fields or scalar fields that encode a CPU number can be filtered using a user-provided cpumask in cpulist format. The format is as follows::
include/linux/trace_events.h +1 -0
@@ -851,6 +851,7 @@ enum {FILTER_COMM ,FILTER_CPU ,FILTER_STACKTRACE , + FILTER_WITHIN, };extern int trace_event_raw_init (struct trace_event_call *call);
kernel/trace/trace.h +2 -1
@@ -1825,7 +1825,8 @@ static inline bool is_string_field(struct ftrace_event_field *field) field->filter_type ==FILTER_RDYN_STRING || field->filter_type ==FILTER_STATIC_STRING || field->filter_type ==FILTER_PTR_STRING || - field->filter_type ==FILTER_COMM ; + field->filter_type ==FILTER_COMM || + field->filter_type ==FILTER_WITHIN ; }static inline bool is_function_field (struct ftrace_event_field *field)
kernel/trace/trace_events.c +3 -0
@@ -199,6 +199,9 @@ static int trace_define_generic_fields(void) __generic_field(char *, comm, FILTER_COMM); __generic_field(char *, stacktrace, FILTER_STACKTRACE); __generic_field(char *, STACKTRACE, FILTER_STACKTRACE); +#ifdef CONFIG_STACKTRACE + __generic_field(char *, within, FILTER_WITHIN); +#endif return ret; }
kernel/trace/trace_events_filter.c +39 -2
67 lines not on this page · raw message
Signature · 1 line
2.43.0
Steven Rostedt
maintainerFrom: https://sashiko.dev/#/patchset/20260608145556.94931-1-chenjun102%40huawei.com
Quoted 33 lines
commit
dfbe6e67884d4ff73b44c46b51995f1945758a89
Author: Chen Jun <[email protected]>
tracing: Add "within" filter for call-stack-based event filtering This commit introduces a "within" filter predicate for trace events, allowing users to filter events based on whether a specific function appears in the current call stack. It useskallsyms_lookup_name()to resolve the function to an address range during setup, andstack_trace_save()to capture the stack at runtime to evaluate the filter condition.Quoted 4 lines
kernel/trace/trace_events_filter.c +0
-0[ ... ]
kernel/trace/trace_events_filter.c static int filter_pred_function(struct filter_pred *pred, void *event) · line 1021
Quoted 13 lines
kernel/trace/trace_events_filter.c +9
-0@@ -1009,6 +1010,22 @@ static int filter_pred_function(struct filter_pred *pred, void *event)return pred->op ==OP_EQ ? ret : !ret; } +/* Filter predicate for within. */ +static int filter_pred_within (struct filter_pred *pred,void *event) +{ +#ifdef CONFIG_STACKTRACE +unsigned long entries[16 ]; +unsigned int nr_entries; +int i; + + nr_entries =stack_trace_save (entries,ARRAY_SIZE (entries),0 );Is an array size of 16 entries sufficient here? Since the skipnr parameter is 0, several of these frames will be consumed by internal tracing and filter evaluation functions (such as
filter_pred_within()andtrace_event_buffer_commit()).
Quoted 5 lines
This leaves very few slots for the actual call chain, meaning higher-level functions like "
vfs_read" will likely be truncated from the stack trace. Could this cause the filter condition to evaluate to false incorrectly, missing the events we are trying to capture?
This is a current limitation that may be dealt with in the future.
Quoted 10 lines
Quoted 6 lines
kernel/trace/trace_events_filter.c +6
-0+for (i =0 ; i < nr_entries; i++) +if (pred->val <= entries[i] && entries[i] < pred->val2) +return !pred->not; +#endif +return pred->not; +}[ ... ]
Quoted 2 lines
kernel/trace/trace_events_filter.c +0
-0@@ -2002,10 +2021,28 @@ static int parse_pred(const char *str, void *data,
Quoted 16 lines
kernel/trace/trace_events_filter.c static int parse_pred(const char *str, void *data, · line 2030
Quoted 11 lines
kernel/trace/trace_events_filter.c +6
-2}else if (field->filter_type ==FILTER_DYN_STRING ) { pred->fn_num =FILTER_PRED_FN_STRLOC ; - }else if (field->filter_type ==FILTER_RDYN_STRING ) + }else if (field->filter_type ==FILTER_RDYN_STRING ) { pred->fn_num =FILTER_PRED_FN_STRRELLOC ; -else { + }else if (field->filter_type ==FILTER_WITHIN ) { +unsigned long func; + +if (op ==OP_GLOB ) + goto err_free;Does this goto need a preceding call to
parse_error()? Other error paths in this function initialize the error state before jumping toerr_free. Without it, users might see an unhelpful generic error message instead of a syntax error indicating that the glob operator isn't supported here.
This is a legitimate issue.
Chen, can you send a v2 and add a proper error message here?
Thanks,
-- Steve
Quoted 3 lines
Quoted 3 lines
kernel/trace/trace_events_filter.c +3
-0+ + pred->fn_num =FILTER_PRED_FN_WITHIN ; + func =kallsyms_lookup_name (pred->regex->pattern);
Steven Rostedt
maintainerQuoted 8 lines · On Tue, 7 Jul 2026 11:20:11 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 4 lines
Does this goto need a preceding call to
parse_error()? Other error paths in this function initialize the error state before jumping toerr_free. Without it, users might see an unhelpful generic error message instead of a syntax error indicating that the glob operator isn't supported here.This is a legitimate issue.
Chen, can you send a v2 and add a proper error message here?
Hi Chen,
Do you plan on sending a v2?
Thanks,
-- Steve