[POC] kunit/scanf_knunit: Add kunit_debug() to allow using the dynamic debug facility
From: Petr Mladek
Date: Tue Feb 11 2025 - 06:24:40 EST
Do not fill the kernel logbuffer with debug messages by default.
Allow to enable them via /sys/kernel/debug/dynamic_debug/control
The question is whether we want to print them into the kunit log.
FIXME: This use just a POC. The right solution should reduce
cut&paste.
---
include/kunit/test.h | 30 ++++++++++++++++++++++++++++++
lib/scanf_kunit.c | 6 +++---
2 files changed, 33 insertions(+), 3 deletions(-)
diff --git a/include/kunit/test.h b/include/kunit/test.h
index 58dbab60f853..23fa3d6b3735 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -637,6 +637,36 @@ void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt,
#define kunit_err(test, fmt, ...) \
kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
+/*
+ * pr_debug and log to per-test or per-suite log buffer. Logging only done
+ * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
+ *
+ * The special variant is needed to allow using the printk dynamic debug
+ * infrastructure, see CONFIG_DYNAMIC_DEBUG.
+ */
+#define kunit_log_debug(test_or_suite, fmt, ...) \
+ do { \
+ pr_debug(fmt, ##__VA_ARGS__); \
+ kunit_log_append((test_or_suite)->log, fmt, \
+ ##__VA_ARGS__); \
+ } while (0)
+
+#define kunit_printk_debug(test, fmt, ...) \
+ kunit_log_debug(test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
+ (test)->name, ##__VA_ARGS__)
+
+/**
+ * kunit_debug() - Prints an DEBUG level message associated with @test.
+ *
+ * @test: The test context object.
+ * @fmt: A printk() style format string.
+ *
+ * Prints an error level message.
+ */
+#define kunit_debug(test, fmt, ...) \
+ kunit_printk_debug(test, fmt, ##__VA_ARGS__)
+
+
/*
* Must be called at the beginning of each KUNIT_*_ASSERTION().
* Cf. KUNIT_CURRENT_LOC.
diff --git a/lib/scanf_kunit.c b/lib/scanf_kunit.c
index 7e2e7d891e41..e45f3c4f0437 100644
--- a/lib/scanf_kunit.c
+++ b/lib/scanf_kunit.c
@@ -42,10 +42,10 @@ _test(struct kunit *test, check_fn fn, const void *check_data, const char *strin
#define _check_numbers_template(arg_fmt, expect, str, fmt, n_args, ap) \
do { \
- kunit_printk(KERN_DEBUG, test, "\"%s\", \"%s\" ->", str, fmt); \
+ kunit_debug(test, "\"%s\", \"%s\" ->", str, fmt); \
for (; n_args > 0; n_args--, expect++) { \
typeof(*expect) got = *va_arg(ap, typeof(expect)); \
- kunit_printk(KERN_DEBUG, test, "\t" arg_fmt, got); \
+ kunit_debug(test, "\t" arg_fmt, got); \
KUNIT_EXPECT_EQ_MSG(test, got, *expect, \
"vsscanf(\"%s\", \"%s\", ...)", str, fmt); \
} \
@@ -677,7 +677,7 @@ do { \
\
len = snprintf(test_buffer, BUF_SIZE, gen_fmt, expect); \
got = (fn)(test_buffer, &endp, base); \
- kunit_printk(KERN_DEBUG, test, #fn "(\"%s\", %d) -> " gen_fmt, test_buffer, base, got); \
+ kunit_debug(test, #fn "(\"%s\", %d) -> " gen_fmt, test_buffer, base, got); \
if (got != (expect)) { \
KUNIT_FAIL(test, #fn "(\"%s\", %d): got " gen_fmt " expected " gen_fmt, \
test_buffer, base, got, expect); \
--
2.48.1
But when thinking more about it. I think that even pr_debug() is not
the right solution.
IMHO, we really want to print these details only when the test fails.
Best Regards,
Petr