[PATCH 2/2] tracing: Add tests for globbing number-based strings
From: Brian Norris
Date: Mon Nov 16 2026 - 21:28:49 EST
In "tracing: Properly match strings that start with numbers", we fixed
bugs related to string fields that start with a number and also use
globs instead of full matches.
Add some additional startup test cases for this.
For easy reproducible testing, I abused the kunit.py script to boot a
minimal kernel with FTRACE_STARTUP_TEST enabled:
./tools/testing/kunit/kunit.py run --arch=x86_64 \
--kconfig_add=CONFIG_FTRACE=y \
--kconfig_add=CONFIG_FUNCTION_TRACER=y \
--kconfig_add=CONFIG_DYNAMIC_FTRACE=y \
--kconfig_add=CONFIG_FTRACE_STARTUP_TEST=y \
--kconfig_add=CONFIG_EVENT_TRACE_STARTUP_TEST=n \
--raw_output=all deadbeef
Relevant line:
Testing ftrace filter: OK
Without "tracing: Properly match strings that start with numbers", the
test fails with:
Testing ftrace filter:
Failed to match filter 'str ~ "0001:*"', expected 1
Signed-off-by: Brian Norris <briannorris@xxxxxxxxxxxx>
---
I was torn on whether to add to CONFIG_FTRACE_STARTUP_TEST, or to add a
KUnit test (such as in kernel/trace/trace_events_test.c).
The former has a bit of precedent, since the existing match tests are
very similar, except they only operate on integers. But the latter
generally has better tooling, and is more consistent with how most
subsystems write their unit tests these days.
I chose the former for now, but let me know if you'd prefer it
differently.
kernel/trace/trace_events_filter.c | 74 ++++++++++++++++++++++++-
kernel/trace/trace_events_filter_test.h | 12 ++--
2 files changed, 80 insertions(+), 6 deletions(-)
diff --git a/kernel/trace/trace_events_filter.c b/kernel/trace/trace_events_filter.c
index 07ce75244233..37985168290a 100644
--- a/kernel/trace/trace_events_filter.c
+++ b/kernel/trace/trace_events_filter.c
@@ -2784,6 +2784,8 @@ int ftrace_profile_set_filter(struct perf_event *event, int event_id,
#include <linux/types.h>
#include <linux/tracepoint.h>
+#include "trace_probe.h"
+
#define CREATE_TRACE_POINTS
#include "trace_events_filter_test.h"
@@ -2795,12 +2797,22 @@ int ftrace_profile_set_filter(struct perf_event *event, int event_id,
.match = m, \
.not_visited = nvisit, \
}
+
+#define DATA_STR(m, s) \
+{ \
+ .filter = FILTER, \
+ .str = s, \
+ .match = m, \
+ .not_visited = "", \
+}
+
#define YES 1
#define NO 0
static struct test_filter_data_t {
char *filter;
struct trace_event_raw_ftrace_test_filter rec;
+ const char *str;
int match;
char *not_visited;
} test_filter_data[] = {
@@ -2852,9 +2864,46 @@ static struct test_filter_data_t {
DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"),
DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
DATA_REC(YES, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"),
+#undef FILTER
+#define FILTER "str ~ \"0001:*\""
+ DATA_STR(YES, "0001:01:00.0"),
+ DATA_STR(YES, "0001:"),
+ DATA_STR(NO, "0002:01:00.0"),
+ DATA_STR(NO, "pci:0001:01:00.0"),
+ DATA_STR(NO, "0001"),
+#undef FILTER
+#define FILTER "str ~ \"!0001:*\""
+ DATA_STR(NO, "0001:01:00.0"),
+ DATA_STR(YES, "0002:01:00.0"),
+#undef FILTER
+#define FILTER "str ~ \"0001:*:00.[0-9]\""
+ DATA_STR(YES, "0001:01:00.0"),
+ DATA_STR(YES, "0001:ff:00.7"),
+ DATA_STR(NO, "0001:01:00.a"),
+ DATA_STR(NO, "0002:01:00.0"),
+#undef FILTER
+#define FILTER "str ~ \"0001:01:00.0\""
+ DATA_STR(YES, "0001:01:00.0"),
+ DATA_STR(NO, "0001:01:00.1"),
+ DATA_STR(NO, "0001:01:00.00"),
+ DATA_STR(NO, "0001:01:00"),
+#undef FILTER
+#define FILTER "str == \"0001:01:00.0\""
+ DATA_STR(YES, "0001:01:00.0"),
+ DATA_STR(NO, "0001:01:00.1"),
+#undef FILTER
+#define FILTER "str ~ \"*0001:*\""
+ DATA_STR(YES, "0001:01:00.0"),
+ DATA_STR(YES, "pci:0001:01:00.0"),
+ DATA_STR(NO, "0002:01:00.0"),
+#undef FILTER
+#define FILTER "str ~ \"*:00.0\""
+ DATA_STR(YES, "0001:01:00.0"),
+ DATA_STR(NO, "0001:01:00.1"),
};
#undef DATA_REC
+#undef DATA_STR
#undef FILTER
#undef YES
#undef NO
@@ -2905,8 +2954,29 @@ static __init int ftrace_test_event_filter(void)
for (i = 0; i < DATA_CNT; i++) {
struct event_filter *filter = NULL;
struct test_filter_data_t *d = &test_filter_data[i];
+ union {
+ struct trace_event_raw_ftrace_test_filter rec;
+ /* Add some spare, to append the string contents. */
+ char buf[sizeof(struct trace_event_raw_ftrace_test_filter) + 32];
+ } event = { .rec = d->rec };
int err;
+ if (d->str) {
+ struct ftrace_event_field *field;
+ u32 offset = offsetof(typeof(event.rec), __data);
+ u32 len = strlen(d->str) + 1;
+ u32 *str_item;
+
+ field = trace_find_event_field(&event_ftrace_test_filter, "str");
+ if (WARN_ON_ONCE(!field))
+ break;
+ if (WARN_ON_ONCE(offset + len > sizeof(event.buf)))
+ break;
+ str_item = (u32 *)&event.buf[field->offset];
+ *str_item = make_data_loc(len, offset);
+ memcpy(&event.buf[offset], d->str, len);
+ }
+
err = create_filter(NULL, &event_ftrace_test_filter,
d->filter, false, &filter);
if (err) {
@@ -2928,7 +2998,7 @@ static __init int ftrace_test_event_filter(void)
update_pred_fn(filter, d->not_visited);
test_pred_visited = 0;
- err = filter_match_preds(filter, &d->rec);
+ err = filter_match_preds(filter, &event.rec);
preempt_enable();
mutex_unlock(&event_mutex);
@@ -2955,7 +3025,7 @@ static __init int ftrace_test_event_filter(void)
/* Need to call ftrace_test_filter to prevent a warning */
if (!trace_ftrace_test_filter_enabled())
- trace_ftrace_test_filter(1, 2, 3, 4, 5, 6, 7, 8);
+ trace_ftrace_test_filter(1, 2, 3, 4, 5, 6, 7, 8, "");
return 0;
}
diff --git a/kernel/trace/trace_events_filter_test.h b/kernel/trace/trace_events_filter_test.h
index e651dfbd345e..74a9dba91545 100644
--- a/kernel/trace/trace_events_filter_test.h
+++ b/kernel/trace/trace_events_filter_test.h
@@ -9,9 +9,10 @@
TRACE_EVENT(ftrace_test_filter,
- TP_PROTO(int a, int b, int c, int d, int e, int f, int g, int h),
+ TP_PROTO(int a, int b, int c, int d, int e, int f, int g, int h,
+ const char *str),
- TP_ARGS(a, b, c, d, e, f, g, h),
+ TP_ARGS(a, b, c, d, e, f, g, h, str),
TP_STRUCT__entry(
__field(int, a)
@@ -22,6 +23,7 @@ TRACE_EVENT(ftrace_test_filter,
__field(int, f)
__field(int, g)
__field(int, h)
+ __string(str, str)
),
TP_fast_assign(
@@ -33,11 +35,13 @@ TRACE_EVENT(ftrace_test_filter,
__entry->f = f;
__entry->g = g;
__entry->h = h;
+ __assign_str(str);
),
- TP_printk("a %d, b %d, c %d, d %d, e %d, f %d, g %d, h %d",
+ TP_printk("a %d, b %d, c %d, d %d, e %d, f %d, g %d, h %d, str %s",
__entry->a, __entry->b, __entry->c, __entry->d,
- __entry->e, __entry->f, __entry->g, __entry->h)
+ __entry->e, __entry->f, __entry->g, __entry->h,
+ __get_str(str))
);
#endif /* _TRACE_TEST_H || TRACE_HEADER_MULTI_READ */
--
2.56.1.703.gf848ed807c-goog