Skip to content

Commit 8bd5d74

Browse files
l0kodshuahkh
authored andcommitted
kunit: Print last test location on fault
This helps identify the location of test faults with opportunistic calls to _KUNIT_SAVE_LOC(). This can be useful while writing tests or debugging them. It is possible to call KUNIT_SUCCESS() to explicit save last location. Cc: Brendan Higgins <brendanhiggins@google.com> Cc: David Gow <davidgow@google.com> Cc: Rae Moar <rmoar@google.com> Cc: Shuah Khan <skhan@linuxfoundation.org> Reviewed-by: Kees Cook <keescook@chromium.org> Signed-off-by: Mickaël Salaün <mic@digikod.net> Link: https://lore.kernel.org/r/20240408074625.65017-7-mic@digikod.net Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent 70585f0 commit 8bd5d74

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

include/kunit/test.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ struct kunit {
301301
struct list_head resources; /* Protected by lock. */
302302

303303
char status_comment[KUNIT_STATUS_COMMENT_SIZE];
304+
/* Saves the last seen test. Useful to help with faults. */
305+
struct kunit_loc last_seen;
304306
};
305307

306308
static inline void kunit_set_failure(struct kunit *test)
@@ -567,6 +569,15 @@ void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt,
567569
#define kunit_err(test, fmt, ...) \
568570
kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
569571

572+
/*
573+
* Must be called at the beginning of each KUNIT_*_ASSERTION().
574+
* Cf. KUNIT_CURRENT_LOC.
575+
*/
576+
#define _KUNIT_SAVE_LOC(test) do { \
577+
WRITE_ONCE(test->last_seen.file, __FILE__); \
578+
WRITE_ONCE(test->last_seen.line, __LINE__); \
579+
} while (0)
580+
570581
/**
571582
* KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
572583
* @test: The test context object.
@@ -575,7 +586,7 @@ void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt,
575586
* words, it does nothing and only exists for code clarity. See
576587
* KUNIT_EXPECT_TRUE() for more information.
577588
*/
578-
#define KUNIT_SUCCEED(test) do {} while (0)
589+
#define KUNIT_SUCCEED(test) _KUNIT_SAVE_LOC(test)
579590

580591
void __noreturn __kunit_abort(struct kunit *test);
581592

@@ -601,14 +612,16 @@ void __printf(6, 7) __kunit_do_failed_assertion(struct kunit *test,
601612
} while (0)
602613

603614

604-
#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
615+
#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) do { \
616+
_KUNIT_SAVE_LOC(test); \
605617
_KUNIT_FAILED(test, \
606618
assert_type, \
607619
kunit_fail_assert, \
608620
kunit_fail_assert_format, \
609621
{}, \
610622
fmt, \
611-
##__VA_ARGS__)
623+
##__VA_ARGS__); \
624+
} while (0)
612625

613626
/**
614627
* KUNIT_FAIL() - Always causes a test to fail when evaluated.
@@ -637,6 +650,7 @@ void __printf(6, 7) __kunit_do_failed_assertion(struct kunit *test,
637650
fmt, \
638651
...) \
639652
do { \
653+
_KUNIT_SAVE_LOC(test); \
640654
if (likely(!!(condition_) == !!expected_true_)) \
641655
break; \
642656
\
@@ -698,6 +712,7 @@ do { \
698712
.right_text = #right, \
699713
}; \
700714
\
715+
_KUNIT_SAVE_LOC(test); \
701716
if (likely(__left op __right)) \
702717
break; \
703718
\
@@ -758,6 +773,7 @@ do { \
758773
.right_text = #right, \
759774
}; \
760775
\
776+
_KUNIT_SAVE_LOC(test); \
761777
if (likely((__left) && (__right) && (strcmp(__left, __right) op 0))) \
762778
break; \
763779
\
@@ -791,6 +807,7 @@ do { \
791807
.right_text = #right, \
792808
}; \
793809
\
810+
_KUNIT_SAVE_LOC(test); \
794811
if (likely(__left && __right)) \
795812
if (likely(memcmp(__left, __right, __size) op 0)) \
796813
break; \
@@ -815,6 +832,7 @@ do { \
815832
do { \
816833
const typeof(ptr) __ptr = (ptr); \
817834
\
835+
_KUNIT_SAVE_LOC(test); \
818836
if (!IS_ERR_OR_NULL(__ptr)) \
819837
break; \
820838
\

lib/kunit/try-catch.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,13 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)
9696

9797
if (exit_code == -EFAULT)
9898
try_catch->try_result = 0;
99-
else if (exit_code == -EINTR)
100-
kunit_err(test, "try faulted\n");
101-
else if (exit_code == -ETIMEDOUT)
99+
else if (exit_code == -EINTR) {
100+
if (test->last_seen.file)
101+
kunit_err(test, "try faulted: last line seen %s:%d\n",
102+
test->last_seen.file, test->last_seen.line);
103+
else
104+
kunit_err(test, "try faulted\n");
105+
} else if (exit_code == -ETIMEDOUT)
102106
kunit_err(test, "try timed out\n");
103107
else if (exit_code)
104108
kunit_err(test, "Unknown error: %d\n", exit_code);

0 commit comments

Comments
 (0)