Commit 8bd5d74b authored by Mickaël Salaün's avatar Mickaël Salaün Committed by Shuah Khan

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: default avatarKees Cook <keescook@chromium.org>
Signed-off-by: default avatarMickaël Salaün <mic@digikod.net>
Link: https://lore.kernel.org/r/20240408074625.65017-7-mic@digikod.netSigned-off-by: default avatarShuah Khan <skhan@linuxfoundation.org>
parent 70585f05
...@@ -301,6 +301,8 @@ struct kunit { ...@@ -301,6 +301,8 @@ struct kunit {
struct list_head resources; /* Protected by lock. */ struct list_head resources; /* Protected by lock. */
char status_comment[KUNIT_STATUS_COMMENT_SIZE]; char status_comment[KUNIT_STATUS_COMMENT_SIZE];
/* Saves the last seen test. Useful to help with faults. */
struct kunit_loc last_seen;
}; };
static inline void kunit_set_failure(struct kunit *test) 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, ...@@ -567,6 +569,15 @@ void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt,
#define kunit_err(test, fmt, ...) \ #define kunit_err(test, fmt, ...) \
kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__) kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
/*
* Must be called at the beginning of each KUNIT_*_ASSERTION().
* Cf. KUNIT_CURRENT_LOC.
*/
#define _KUNIT_SAVE_LOC(test) do { \
WRITE_ONCE(test->last_seen.file, __FILE__); \
WRITE_ONCE(test->last_seen.line, __LINE__); \
} while (0)
/** /**
* KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity. * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
* @test: The test context object. * @test: The test context object.
...@@ -575,7 +586,7 @@ void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt, ...@@ -575,7 +586,7 @@ void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt,
* words, it does nothing and only exists for code clarity. See * words, it does nothing and only exists for code clarity. See
* KUNIT_EXPECT_TRUE() for more information. * KUNIT_EXPECT_TRUE() for more information.
*/ */
#define KUNIT_SUCCEED(test) do {} while (0) #define KUNIT_SUCCEED(test) _KUNIT_SAVE_LOC(test)
void __noreturn __kunit_abort(struct kunit *test); void __noreturn __kunit_abort(struct kunit *test);
...@@ -601,14 +612,16 @@ void __printf(6, 7) __kunit_do_failed_assertion(struct kunit *test, ...@@ -601,14 +612,16 @@ void __printf(6, 7) __kunit_do_failed_assertion(struct kunit *test,
} while (0) } while (0)
#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \ #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) do { \
_KUNIT_SAVE_LOC(test); \
_KUNIT_FAILED(test, \ _KUNIT_FAILED(test, \
assert_type, \ assert_type, \
kunit_fail_assert, \ kunit_fail_assert, \
kunit_fail_assert_format, \ kunit_fail_assert_format, \
{}, \ {}, \
fmt, \ fmt, \
##__VA_ARGS__) ##__VA_ARGS__); \
} while (0)
/** /**
* KUNIT_FAIL() - Always causes a test to fail when evaluated. * 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, ...@@ -637,6 +650,7 @@ void __printf(6, 7) __kunit_do_failed_assertion(struct kunit *test,
fmt, \ fmt, \
...) \ ...) \
do { \ do { \
_KUNIT_SAVE_LOC(test); \
if (likely(!!(condition_) == !!expected_true_)) \ if (likely(!!(condition_) == !!expected_true_)) \
break; \ break; \
\ \
...@@ -698,6 +712,7 @@ do { \ ...@@ -698,6 +712,7 @@ do { \
.right_text = #right, \ .right_text = #right, \
}; \ }; \
\ \
_KUNIT_SAVE_LOC(test); \
if (likely(__left op __right)) \ if (likely(__left op __right)) \
break; \ break; \
\ \
...@@ -758,6 +773,7 @@ do { \ ...@@ -758,6 +773,7 @@ do { \
.right_text = #right, \ .right_text = #right, \
}; \ }; \
\ \
_KUNIT_SAVE_LOC(test); \
if (likely((__left) && (__right) && (strcmp(__left, __right) op 0))) \ if (likely((__left) && (__right) && (strcmp(__left, __right) op 0))) \
break; \ break; \
\ \
...@@ -791,6 +807,7 @@ do { \ ...@@ -791,6 +807,7 @@ do { \
.right_text = #right, \ .right_text = #right, \
}; \ }; \
\ \
_KUNIT_SAVE_LOC(test); \
if (likely(__left && __right)) \ if (likely(__left && __right)) \
if (likely(memcmp(__left, __right, __size) op 0)) \ if (likely(memcmp(__left, __right, __size) op 0)) \
break; \ break; \
...@@ -815,6 +832,7 @@ do { \ ...@@ -815,6 +832,7 @@ do { \
do { \ do { \
const typeof(ptr) __ptr = (ptr); \ const typeof(ptr) __ptr = (ptr); \
\ \
_KUNIT_SAVE_LOC(test); \
if (!IS_ERR_OR_NULL(__ptr)) \ if (!IS_ERR_OR_NULL(__ptr)) \
break; \ break; \
\ \
......
...@@ -96,9 +96,13 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) ...@@ -96,9 +96,13 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)
if (exit_code == -EFAULT) if (exit_code == -EFAULT)
try_catch->try_result = 0; try_catch->try_result = 0;
else if (exit_code == -EINTR) else if (exit_code == -EINTR) {
kunit_err(test, "try faulted\n"); if (test->last_seen.file)
else if (exit_code == -ETIMEDOUT) kunit_err(test, "try faulted: last line seen %s:%d\n",
test->last_seen.file, test->last_seen.line);
else
kunit_err(test, "try faulted\n");
} else if (exit_code == -ETIMEDOUT)
kunit_err(test, "try timed out\n"); kunit_err(test, "try timed out\n");
else if (exit_code) else if (exit_code)
kunit_err(test, "Unknown error: %d\n", exit_code); kunit_err(test, "Unknown error: %d\n", exit_code);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment