backtrace: Strip the source tree prefix from filenames

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.

Series-to: concept
Series-cc: heinrich
Cover-letter:
backtrace: Add runtime support for looking at the backtrace
In some cases the backtrace contains useful information, such as whether
a particular function was called earlier in the stack.

This series provides a very simple backtrace library, along with some
sandbox-specific functions to allow it to work. It is designed such that
another arch could implement it.

A new 'backtrace' command provides access to the backtrace.
END

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
This commit is contained in:
Simon Glass
2025-11-28 06:51:08 -07:00
committed by Simon Glass
parent eb711c45ce
commit cee822e941
4 changed files with 32 additions and 11 deletions

View File

@@ -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

View File

@@ -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]);
}