[Concept,10/10] backtrace: Strip the source tree prefix from filenames
Commit Message
From: Simon Glass <simon.glass@canonical.com>
Display relative paths instead of absolute paths in backtrace output,
making the output cleaner and more portable across different build
environments.
This works by adding a SRCTREE define to lib/backtrace.c and stripping
it from filenames when printing.
Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---
doc/usage/cmd/backtrace.rst | 18 +++++++++---------
lib/Makefile | 1 +
lib/backtrace.c | 17 ++++++++++++++++-
test/cmd/backtrace.c | 7 ++++++-
4 files changed, 32 insertions(+), 11 deletions(-)
@@ -29,15 +29,15 @@ Example
=> backtrace
backtrace: 14 addresses
- backtrace_show() at /home/user/u-boot/lib/backtrace.c:17
- do_backtrace() at /home/user/u-boot/cmd/backtrace.c:18
- cmd_process() at /home/user/u-boot/common/command.c:637
- run_list_real() at /home/user/u-boot/common/cli_hush.c:1868
- parse_stream_outer() at /home/user/u-boot/common/cli_hush.c:3207
- parse_string_outer() at /home/user/u-boot/common/cli_hush.c:3257
- run_command_list() at /home/user/u-boot/common/cli.c:168
- sandbox_main_loop_init() at /home/user/u-boot/arch/sandbox/cpu/start.c:153
- board_init_r() at /home/user/u-boot/common/board_r.c:774
+ backtrace_show() at lib/backtrace.c:18
+ do_backtrace() at cmd/backtrace.c:17
+ cmd_process() at common/command.c:637
+ run_list_real() at common/cli_hush.c:1868
+ parse_stream_outer() at common/cli_hush.c:3207
+ parse_string_outer() at common/cli_hush.c:3257
+ run_command_list() at common/cli.c:168
+ sandbox_main_loop_init() at arch/sandbox/cpu/start.c:153
+ board_init_r() at common/board_r.c:774
...
Configuration
@@ -148,6 +148,7 @@ obj-$(CONFIG_LIB_UUID) += uuid.o
obj-$(CONFIG_LIB_RAND) += rand.o
obj-y += panic.o
obj-$(CONFIG_BACKTRACE) += backtrace.o
+CFLAGS_backtrace.o += -DSRCTREE='"$(srctree)/"'
ifeq ($(CONFIG_XPL_BUILD),y)
# SPL U-Boot may use full-printf, tiny-printf or none at all
@@ -8,6 +8,21 @@
#include <backtrace.h>
#include <stdio.h>
+#include <string.h>
+
+static void print_sym(const char *sym)
+{
+ const char *p;
+
+ /* Look for SRCTREE prefix in the string and skip it */
+ p = strstr(sym, SRCTREE);
+ if (p) {
+ /* Print part before SRCTREE, then the rest after SRCTREE */
+ printf(" %.*s%s\n", (int)(p - sym), sym, p + strlen(SRCTREE));
+ } else {
+ printf(" %s\n", sym);
+ }
+}
int backtrace_show(void)
{
@@ -29,7 +44,7 @@ int backtrace_show(void)
printf("backtrace: %d addresses\n", ctx.count);
for (i = 0; i < ctx.count; i++) {
if (ctx.syms[i])
- printf(" %s\n", ctx.syms[i]);
+ print_sym(ctx.syms[i]);
else
printf(" %p\n", ctx.addrs[i]);
}
@@ -14,9 +14,14 @@
/* Test 'backtrace' command */
static int cmd_test_backtrace(struct unit_test_state *uts)
{
- /* for now, just run the command */
ut_assertok(run_command("backtrace", 0));
+ ut_assert_nextlinen("backtrace:");
+ ut_assert_nextlinen(" backtrace_show() at lib/backtrace.c:");
+ ut_assert_nextlinen(" do_backtrace() at cmd/backtrace.c:");
+ ut_assert_nextlinen(" cmd_process() at common/command.c:");
+ ut_assert_skip_to_linen(" cmd_test_backtrace() at test/cmd/backtrace.c:");
+
return 0;
}
DM_TEST(cmd_test_backtrace, UTF_SCAN_FDT);