From bcb467da4c3b91e88f8f7c58e821e6cf594280dd Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Fri, 14 Mar 2025 09:51:01 +1000 Subject: [PATCH] test: add extra highlighting for the backtrace Pass through the function name where the condition failed so we can highlight that line in the backtrace. Part-of: --- src/util-backtrace.h | 15 +++++++++++++-- test/litest-runner.c | 2 +- test/litest.c | 25 ++++++++++++++++--------- test/litest.h | 2 +- 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/util-backtrace.h b/src/util-backtrace.h index b9c48f70..e47ca97b 100644 --- a/src/util-backtrace.h +++ b/src/util-backtrace.h @@ -43,12 +43,16 @@ * If use_colors is true, highlight_before may specify * a substring for a line before which the backtrace is * colored. + * + * If use_colors is true, highlight_extra may specify + * a substring for a line that has extra highlighting. */ static inline void backtrace_print(FILE *fp, bool use_colors, const char *highlight_after, - const char *highlight_before) + const char *highlight_before, + const char *highlight_extra) { #if HAVE_GSTACK pid_t parent, child; @@ -98,8 +102,15 @@ backtrace_print(FILE *fp, while (line && *line) { if (highlight && highlight_before && strstr(*line, highlight_before)) highlight = false; + + const char *hlcolor = highlight ? ANSI_BRIGHT_CYAN : ""; + + if (highlight && highlight_extra && + strstr(*line, highlight_extra)) + hlcolor = ANSI_BRIGHT_MAGENTA; + fprintf(fp, "%s%s%s\n", - highlight ? ANSI_BRIGHT_CYAN : "", + hlcolor, *line, highlight ? ANSI_NORMAL : ""); if (!highlight && highlight_after && strstr(*line, highlight_after)) diff --git a/test/litest-runner.c b/test/litest-runner.c index 2931bb76..bb4f7c0b 100644 --- a/test/litest-runner.c +++ b/test/litest-runner.c @@ -283,7 +283,7 @@ sighandler_forked_child(int signal) * the backtrace anyway - we only need to backtrace the other signals */ if (signal != SIGABRT) - litest_backtrace(); + litest_backtrace(NULL); raise(signal); } diff --git a/test/litest.c b/test/litest.c index 6ebb7cd9..848ac3b7 100644 --- a/test/litest.c +++ b/test/litest.c @@ -168,15 +168,22 @@ _litest_checkpoint(const char *func, } void -litest_backtrace(void) +litest_backtrace(const char *func) { #ifndef LITEST_DISABLE_BACKTRACE_LOGGING if (RUNNING_ON_VALGRIND) { fprintf(stderr, "Using valgrind, omitting backtrace\n"); return; } + char buf[256]; - backtrace_print(stderr, use_colors, "in litest_backtrace", "in litest_runner_test_run"); + snprintf(buf, sizeof(buf), "in %s", func); + + backtrace_print(stderr, + use_colors, + "in litest_backtrace", + "in litest_runner_test_run", + func ? buf : NULL); #endif } @@ -202,7 +209,7 @@ litest_fail_condition(const char *file, } litest_log("in %s() (%s:%d)\n", func, file, line); - litest_backtrace(); + litest_backtrace(func); litest_runner_abort(); } @@ -220,7 +227,7 @@ litest_fail_comparison_int(const char *file, litest_log("FAILED COMPARISON: %s %s %s\n", astr, operator, bstr); litest_log("Resolved to: %d %s %d\n", a, operator, b); litest_log("in %s() (%s:%d)\n", func, file, line); - litest_backtrace(); + litest_backtrace(func); litest_runner_abort(); } @@ -238,7 +245,7 @@ litest_fail_comparison_double(const char *file, litest_log("FAILED COMPARISON: %s %s %s\n", astr, operator, bstr); litest_log("Resolved to: %.3f %s %.3f\n", a, operator, b); litest_log("in %s() (%s:%d)\n", func, file, line); - litest_backtrace(); + litest_backtrace(func); litest_runner_abort(); } @@ -251,7 +258,7 @@ litest_fail_comparison_ptr(const char *file, { litest_log("FAILED COMPARISON: %s\n", comparison); litest_log("in %s() (%s:%d)\n", func, file, line); - litest_backtrace(); + litest_backtrace(func); litest_runner_abort(); } @@ -268,7 +275,7 @@ litest_fail_comparison_str(const char *file, litest_log("FAILED COMPARISON: %s %s %s\n", astr, operator, bstr); litest_log("Resolved to: %s %s %s\n", astr, operator, bstr); litest_log("in %s() (%s:%d)\n", func, file, line); - litest_backtrace(); + litest_backtrace(func); litest_runner_abort(); } @@ -3708,7 +3715,7 @@ _litest_assert_event_type_is_one_of(struct libinput_event *event, ...) fprintf(stderr, "\n"); litest_print_event(event, "Wrong event is:"); - litest_backtrace(); + litest_backtrace(__func__); litest_runner_abort(); } @@ -3748,7 +3755,7 @@ _litest_assert_event_type_not_one_of(struct libinput_event *event, ...) libinput_event_get_type(event)); litest_print_event(event,"\nWrong event is: "); - litest_backtrace(); + litest_backtrace(__func__); litest_runner_abort(); } diff --git a/test/litest.h b/test/litest.h index bf494074..3f342ee8 100644 --- a/test/litest.h +++ b/test/litest.h @@ -359,7 +359,7 @@ litest_fail_comparison_str(const char *file, #define litest_assert_double_ge(a_, b_)\ litest_assert_double_ge_epsilon((a_), (b_),LITEST_DEFAULT_EPSILON) -void litest_backtrace(void); +void litest_backtrace(const char *func); enum litest_device_type { LITEST_NO_DEVICE = -1,