Skip to content

Commit 5c1553d

Browse files
groeckshuahkh
authored andcommitted
kunit: Add documentation for warning backtrace suppression API
Document API functions for suppressing warning backtraces. Link: https://lore.kernel.org/r/20260514-kunit_add_support-v11-4-b36a530a6d8f@redhat.com Tested-by: Linux Kernel Functional Testing <lkft@linaro.org> Acked-by: Dan Carpenter <dan.carpenter@linaro.org> Reviewed-by: Kees Cook <keescook@chromium.org> Signed-off-by: Guenter Roeck <linux@roeck-us.net> Signed-off-by: Alessandro Carminati <acarmina@redhat.com> Reviewed-by: David Gow <david@davidgow.net> Signed-off-by: Albert Esteve <aesteve@redhat.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent a5eefd0 commit 5c1553d

1 file changed

Lines changed: 45 additions & 1 deletion

File tree

Documentation/dev-tools/kunit/usage.rst

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,50 @@ Alternatively, one can take full control over the error message by using
157157
if (some_setup_function())
158158
KUNIT_FAIL(test, "Failed to setup thing for testing");
159159
160+
Suppressing warning backtraces
161+
------------------------------
162+
163+
Some unit tests trigger warning backtraces either intentionally or as a side
164+
effect. Such backtraces are normally undesirable since they distract from
165+
the actual test and may result in the impression that there is a problem.
166+
167+
Backtraces can be suppressed with **task-scoped suppression**: while
168+
suppression is active on the current task, the backtrace and stack dump from
169+
``WARN*()``, ``WARN_ON*()``, and related macros on that task are suppressed.
170+
Two API forms are available.
171+
172+
- Scoped suppression is the simplest form. Wrap the code that triggers
173+
warnings in a ``kunit_warning_suppress()`` block:
174+
175+
.. code-block:: c
176+
177+
static void some_test(struct kunit *test)
178+
{
179+
kunit_warning_suppress(test) {
180+
trigger_backtrace();
181+
KUNIT_EXPECT_SUPPRESSED_WARNING_COUNT(test, 1);
182+
}
183+
}
184+
185+
.. note::
186+
The warning count must be checked inside the block; the suppression handle
187+
is not accessible after the block exits.
188+
189+
- Direct functions return an explicit handle pointer. Use them when the handle
190+
needs to be retained or passed across helper functions:
191+
192+
.. code-block:: c
193+
194+
static void some_test(struct kunit *test)
195+
{
196+
struct kunit_suppressed_warning *w;
197+
198+
w = kunit_start_suppress_warning(test);
199+
trigger_backtrace();
200+
kunit_end_suppress_warning(test, w);
201+
202+
KUNIT_EXPECT_EQ(test, kunit_suppressed_warning_count(w), 1);
203+
}
160204
161205
Test Suites
162206
~~~~~~~~~~~
@@ -1211,4 +1255,4 @@ For example:
12111255
dev_managed_string = devm_kstrdup(fake_device, "Hello, World!");
12121256
12131257
// Everything is cleaned up automatically when the test ends.
1214-
}
1258+
}

0 commit comments

Comments
 (0)