perf trace: Support --bitmask-list with --libtraceevent
PATCH 1/1 perf trace: Support --bitmask-list with --libtraceevent
Commit ab74d1fbe061 ("perf trace: Add --bitmask-list command-line option") introduced the --bitmask-list option to display cpumasks and bitmasks as condensed, human-readable range lists (e.g. "0,2-5,7") instead of the default hexadecimal format.
However, that implementation only augmented trace__fprintf_tp_fields(). When --libtraceevent is specified, event formatting is delegated to event_format__fprintf(), which bypasses trace__fprintf_tp_fields() and relies directly on libtraceevent.
In libtraceevent, tracepoint bitmask fields defined using __get_bitmask() are parsed into TEP_PRINT_BITMASK AST nodes and unconditionally formatted as raw 32-bit hex chunks (e.g. "00000000,00000005"), ignoring the --bitmask-list command-line option.
While a dedicated configuration API in libtraceevent would be the ideal long-term solution, that requires a new upstream libtraceevent release and conditional version checks in perf. To provide immediate support for existing installations, traverse the event format's print argument AST in perf trace and rewrite any TEP_PRINT_BITMASK nodes to TEP_PRINT_CPUMASK when --bitmask-list is enabled. Both types share the identical union payload (arg->bitmask.field), but TEP_PRINT_CPUMASK instructs libtraceevent's print engine (print_cpumask_to_seq()) to render the mask as a condensed range list.
To avoid any per-sample overhead in the event handling fast path, perform this transformation once during tracepoint setup via trace__init_tp_bitmask_list() for both live tracing (cmd_trace()) and recording replay (trace__replay()), leaving trace__event_handler() untouched. All enum values of enum tep_print_arg_type are explicitly covered in the switch statement to satisfy -Wswitch-enum under -Werror.
--- tools/perf/builtin-trace.c | 75 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+)
tools/perf/builtin-trace.c +75 -0
@@ -4857,6 +4857,77 @@ static int trace__run(struct trace *trace, int argc, const char **argv) goto out_put_evlist; } +#ifdef HAVE_LIBTRACEEVENT +static void tep_print_arg__set_bitmask_list (struct tep_print_arg *arg) +{ +for (; arg; arg = arg->next) { +switch (arg->type) { +case TEP_PRINT_BITMASK : + arg->type =TEP_PRINT_CPUMASK ; +break ; +case TEP_PRINT_OP : +tep_print_arg__set_bitmask_list (arg->op.left); +tep_print_arg__set_bitmask_list (arg->op.right); +break ; +case TEP_PRINT_FUNC : +tep_print_arg__set_bitmask_list (arg->func.args); +break ; +case TEP_PRINT_FLAGS : +tep_print_arg__set_bitmask_list (arg->flags.field); +break ; +case TEP_PRINT_SYMBOL : +tep_print_arg__set_bitmask_list (arg->symbol.field); +break ; +case TEP_PRINT_HEX : +tep_print_arg__set_bitmask_list (arg->hex.field); +tep_print_arg__set_bitmask_list (arg->hex.size); +break ; +case TEP_PRINT_INT_ARRAY : +tep_print_arg__set_bitmask_list (arg->int_array.field); +tep_print_arg__set_bitmask_list (arg->int_array.count); +tep_print_arg__set_bitmask_list (arg->int_array.el_size); +break ; +case TEP_PRINT_TYPE : +tep_print_arg__set_bitmask_list (arg->typecast.item); +break ; +case TEP_PRINT_DYNAMIC_ARRAY : +tep_print_arg__set_bitmask_list (arg->dynarray.index); +break ; +case TEP_PRINT_NULL : +case TEP_PRINT_ATOM : +case TEP_PRINT_FIELD : +case TEP_PRINT_STRING : +case TEP_PRINT_BSTRING : +case TEP_PRINT_DYNAMIC_ARRAY_LEN : +case TEP_PRINT_HEX_STR : +case TEP_PRINT_CPUMASK : +default : +break ; + } + } +} + +static void trace__init_tp_bitmask_list (struct trace *trace,struct evlist *evlist) +{ +struct evsel *evsel; + +if (!trace->libtraceevent_print || !trace->bitmask_list) +return ; + +evlist__for_each_entry (evlist, evsel) { +struct tep_event *tp_format =evsel__tp_format (evsel); + +if (tp_format) +tep_print_arg__set_bitmask_list (tp_format->print_fmt.args); + } +} +#else +static void trace__init_tp_bitmask_list (struct trace *trace__maybe_unused , +struct evlist *evlist__maybe_unused ) +{ +} +#endif +static int trace__replay (struct trace *trace) {const struct evsel_str_handler handlers[] = {
@@ -4908,6 +4979,8 @@ static int trace__replay(struct trace *trace)if (err) goto out; +trace__init_tp_bitmask_list (trace, session->evlist); + evsel =evlist__find_tracepoint_by_name (session->evlist,"raw_syscalls:sys_enter" ); trace->syscalls.events.sys_enter = evsel;/* older kernels have syscalls tp versus raw_syscalls */
@@ -5814,6 +5887,8 @@ int cmd_trace(int argc, const char **argv)if (use_btf)trace__load_vmlinux_btf (&trace); + +trace__init_tp_bitmask_list (&trace, trace.evlist); }/*
Signature · 1 line
2.55.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Medium] perf trace: incomplete AST traversal for TEP_PRINT_HEX_STR nodes - [Medium] perf trace…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Medium] perf trace: incomplete AST traversal for TEP_PRINT_HEX_STR nodes
- [Medium] perf trace: --bitmask-list broken for pipe mode streams
- [Low] perf trace: build failure on older libtraceevent due to missing TEP_PRINT_CPUMASK
- [Low] perf trace: future build failures due to -Wswitch-enum on external libtraceevent enum --
commit f1dd0cf882822a7f067e6b6c6825d55f9ca3811c
Author: Aaron Tomlin <[email protected]>
perf trace: Support --bitmask-list with --libtraceevent
Commit ab74d1fbe061 ("perf trace: Add --bitmask-list command-line option") introduced the --bitmask-list option to display cpumasks and bitmasks as condensed, human-readable range lists (e.g. "0,2-5,7") instead of the default hexadecimal format.
However, that implementation only augmented trace__fprintf_tp_fields(). When --libtraceevent is specified, event formatting is delegated to event_format__fprintf(), which bypasses trace__fprintf_tp_fields() and relies directly on libtraceevent.
tools/perf/builtin-trace.c static int trace__run(struct trace *trace, int argc, const char **argv) · line 4864
Quoted 13 lines
tools/perf/builtin-trace.c +5
-0@@ -4857,6 +4857,77 @@ static int trace__run(struct trace *trace, int argc, const char **argv) goto out_put_evlist; } +#ifdef HAVE_LIBTRACEEVENT +static void tep_print_arg__set_bitmask_list (struct tep_print_arg *arg) +{ +for (; arg; arg = arg->next) { +switch (arg->type) {
[Severity: Low]
Will this cause build failures on future libtraceevent updates?
The commit message mentions adding all enum values of tep_print_arg_type to satisfy -Wswitch-enum. However, because tep_print_arg_type is an external enum from libtraceevent, any future updates to libtraceevent that add new values to this enum will break the perf build due to -Werror.
tools/perf/builtin-trace.c static int trace__run(struct trace *trace, int argc, const char **argv) · line 4866
Quoted 2 lines
tools/perf/builtin-trace.c +2
-0+case TEP_PRINT_BITMASK : + arg->type =TEP_PRINT_CPUMASK ;
[Severity: Low]
Can this cause a build failure on older libtraceevent versions?
If TEP_PRINT_CPUMASK is missing from the installed system libtraceevent headers, this unconditional assignment will fail to compile.
Quoted 26 lines
tools/perf/builtin-trace.c +26
-0+break ; +case TEP_PRINT_OP : +tep_print_arg__set_bitmask_list (arg->op.left); +tep_print_arg__set_bitmask_list (arg->op.right); +break ; +case TEP_PRINT_FUNC : +tep_print_arg__set_bitmask_list (arg->func.args); +break ; +case TEP_PRINT_FLAGS : +tep_print_arg__set_bitmask_list (arg->flags.field); +break ; +case TEP_PRINT_SYMBOL : +tep_print_arg__set_bitmask_list (arg->symbol.field); +break ; +case TEP_PRINT_HEX : +tep_print_arg__set_bitmask_list (arg->hex.field); +tep_print_arg__set_bitmask_list (arg->hex.size); +break ; +case TEP_PRINT_INT_ARRAY : +tep_print_arg__set_bitmask_list (arg->int_array.field); +tep_print_arg__set_bitmask_list (arg->int_array.count); +tep_print_arg__set_bitmask_list (arg->int_array.el_size); +break ; +case TEP_PRINT_TYPE : +tep_print_arg__set_bitmask_list (arg->typecast.item); +break ;
tools/perf/builtin-trace.c static int trace__run(struct trace *trace, int argc, const char **argv) · line 4902
Quoted 10 lines
tools/perf/builtin-trace.c +10
-0+case TEP_PRINT_DYNAMIC_ARRAY : +tep_print_arg__set_bitmask_list (arg->dynarray.index); +break ; +case TEP_PRINT_NULL : +case TEP_PRINT_ATOM : +case TEP_PRINT_FIELD : +case TEP_PRINT_STRING : +case TEP_PRINT_BSTRING : +case TEP_PRINT_DYNAMIC_ARRAY_LEN : +case TEP_PRINT_HEX_STR :
[Severity: Medium]
Does this skip the AST traversal for TEP_PRINT_HEX_STR nodes?
The fallthrough here prevents the recursive traversal of arg->hex.field and arg->hex.size for TEP_PRINT_HEX_STR nodes, which share the same structural payload as TEP_PRINT_HEX. Nested bitmask evaluations inside a hex string length or field argument could be silently ignored.
Quoted 7 lines
tools/perf/builtin-trace.c +7
-0+case TEP_PRINT_CPUMASK : +default : +break ; + } + } +} +
[ ... ]
tools/perf/builtin-trace.c static int trace__replay(struct trace *trace) · line 4982
Quoted 5 lines
tools/perf/builtin-trace.c +1
-0@@ -4908,6 +4979,8 @@ static int trace__replay(struct trace *trace)if (err) goto out; +trace__init_tp_bitmask_list (trace, session->evlist);
[Severity: Medium]
Is --bitmask-list broken for pipe mode streams?
In pipe mode, the event list is populated dynamically from synthesized events during perf_session__process_events(), which happens later in this function. By calling trace__init_tp_bitmask_list() here, the initialization operates on an empty evlist, skipping the AST transformation.
Quoted 4 lines
tools/perf/builtin-trace.c +1
-0+ evsel =evlist__find_tracepoint_by_name (session->evlist,"raw_syscalls:sys_enter" ); trace->syscalls.events.sys_enter = evsel;/* older kernels have syscalls tp versus raw_syscalls */
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 7 lines · On Sat, Oct 03, 2026 at 01:34:01AM +0000, [email protected] wrote:
[Severity: Low]
Will this cause build failures on future libtraceevent updates?The commit message mentions adding all enum values of
tep_print_arg_typeto satisfy -Wswitch-enum. However, becausetep_print_arg_typeis an external enum from libtraceevent, any future updates to libtraceevent that add new values to this enum will break the perf build due to -Werror.
Good point. Explicitly enumerating all external enum variants is fragile against future upstream additions. In the next iteration, this is resolved by casting arg->type explicitly to (int) in the switch evaluation
Quoted 9 lines
tools/perf/builtin-trace.c static int trace__run(struct trace *trace, int argc, const char **argv) · line 4866
Quoted 3 lines
tools/perf/builtin-trace.c +2
-0+case TEP_PRINT_BITMASK : + arg->type =TEP_PRINT_CPUMASK ;[Severity: Low]
Can this cause a build failure on older libtraceevent versions?If
TEP_PRINT_CPUMASKis missing from the installed system libtraceevent headers, this unconditional assignment will fail to compile.
TEP_PRINT_CPUMASK was introduced in libtraceevent 1.7.0, so building against older versions without a version check breaks compilation. In the next iteration, the transformation and helper are guarded with:
#if defined(HAVE_LIBTRACEEVENT) && \
defined(LIBTRACEEVENT_VERSION) && \
(MAKE_LIBTRACEEVENT_VERSION(1, 7, 0) <= LIBTRACEEVENT_VERSION)Quoted 41 lines
Quoted 27 lines
tools/perf/builtin-trace.c +26
-0+break ; +case TEP_PRINT_OP : +tep_print_arg__set_bitmask_list (arg->op.left); +tep_print_arg__set_bitmask_list (arg->op.right); +break ; +case TEP_PRINT_FUNC : +tep_print_arg__set_bitmask_list (arg->func.args); +break ; +case TEP_PRINT_FLAGS : +tep_print_arg__set_bitmask_list (arg->flags.field); +break ; +case TEP_PRINT_SYMBOL : +tep_print_arg__set_bitmask_list (arg->symbol.field); +break ; +case TEP_PRINT_HEX : +tep_print_arg__set_bitmask_list (arg->hex.field); +tep_print_arg__set_bitmask_list (arg->hex.size); +break ; +case TEP_PRINT_INT_ARRAY : +tep_print_arg__set_bitmask_list (arg->int_array.field); +tep_print_arg__set_bitmask_list (arg->int_array.count); +tep_print_arg__set_bitmask_list (arg->int_array.el_size); +break ; +case TEP_PRINT_TYPE : +tep_print_arg__set_bitmask_list (arg->typecast.item); +break ;tools/perf/builtin-trace.c static int trace__run(struct trace *trace, int argc, const char **argv) · line 4902
Quoted 10 lines
tools/perf/builtin-trace.c +10
-0+case TEP_PRINT_DYNAMIC_ARRAY : +tep_print_arg__set_bitmask_list (arg->dynarray.index); +break ; +case TEP_PRINT_NULL : +case TEP_PRINT_ATOM : +case TEP_PRINT_FIELD : +case TEP_PRINT_STRING : +case TEP_PRINT_BSTRING : +case TEP_PRINT_DYNAMIC_ARRAY_LEN : +case TEP_PRINT_HEX_STR :[Severity: Medium]
Does this skip the AST traversal forTEP_PRINT_HEX_STRnodes?
Quoted 4 lines
The fallthrough here prevents the recursive traversal of
arg->hex.fieldandarg->hex.sizeforTEP_PRINT_HEX_STRnodes, which share the same structural payload asTEP_PRINT_HEX. Nested bitmask evaluations inside a hex string length or field argument could be silently ignored.
TEP_PRINT_HEX_STR shares the identical struct tep_print_arg_hex payload (arg->hex.field and arg->hex.size) with TEP_PRINT_HEX. The next iteration, TEP_PRINT_HEX_STR is grouped together with TEP_PRINT_HEX:
case TEP_PRINT_HEX:
case TEP_PRINT_HEX_STR:
tep_print_arg__set_bitmask_list(arg->hex.field);
tep_print_arg__set_bitmask_list(arg->hex.size);
break;Quoted 20 lines
Quoted 8 lines
tools/perf/builtin-trace.c +7
-0+case TEP_PRINT_CPUMASK : +default : +break ; + } + } +} +[ ... ]
tools/perf/builtin-trace.c static int trace__replay(struct trace *trace) · line 4982
Quoted 5 lines
tools/perf/builtin-trace.c +1
-0@@ -4908,6 +4979,8 @@ static int trace__replay(struct trace *trace)if (err) goto out; +trace__init_tp_bitmask_list (trace, session->evlist);[Severity: Medium]
Is --bitmask-list broken for pipe mode streams?
Quoted 4 lines
In pipe mode, the event list is populated dynamically from synthesized events during
perf_session__process_events(), which happens later in this function. By callingtrace__init_tp_bitmask_list()here, the initialization operates on an empty evlist, skipping the AST transformation.
In perf trace, file replay (trace__replay()) expects to read from a recorded perf.data file where the session header (including event descriptions and tracepoint formats) is read upfront by perf_session__new(). Existing tracepoint setup in trace__replay() already relies on session->evlist being populated prior to perf_session__process_events() e.g. evlist__find_tracepoint_by_name() for raw_syscalls and syscalls.
Doing this transformation once during tracepoint setup avoids adding per-event checks or lookups to trace__event_handler() in the sample delivery hot path. The commit message has been updated to explicitly state that, as with existing tracepoint replay in perf trace, this expects the evlist to be populated from the session header.
Kind regards,
Signature · 1 line
Aaron Tomlin