Skip to content

Commit c9a83e7

Browse files
guidosarduccianakryiko
authored andcommitted
selftests/bpf: Fix compile if backtrace support missing in libc
Include GNU <execinfo.h> header only with glibc and provide weak, stubbed backtrace functions as a fallback in test_progs.c. This allows for non-GNU replacements while avoiding compile errors (e.g. with musl libc) like: test_progs.c:13:10: fatal error: execinfo.h: No such file or directory 13 | #include <execinfo.h> /* backtrace */ | ^~~~~~~~~~~~ test_progs.c: In function 'crash_handler': test_progs.c:1034:14: error: implicit declaration of function 'backtrace' [-Werror=implicit-function-declaration] 1034 | sz = backtrace(bt, ARRAY_SIZE(bt)); | ^~~~~~~~~ test_progs.c:1045:9: error: implicit declaration of function 'backtrace_symbols_fd' [-Werror=implicit-function-declaration] 1045 | backtrace_symbols_fd(bt, sz, STDERR_FILENO); | ^~~~~~~~~~~~~~~~~~~~ Fixes: 9fb156b ("selftests/bpf: Print backtrace on SIGSEGV in test_progs") Signed-off-by: Tony Ambardar <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/aa6dc8e23710cb457b278039d0081de7e7b4847d.1722244708.git.tony.ambardar@gmail.com
1 parent 16b795c commit c9a83e7

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

tools/testing/selftests/bpf/test_progs.c

+15-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <sched.h>
1111
#include <signal.h>
1212
#include <string.h>
13-
#include <execinfo.h> /* backtrace */
1413
#include <sys/sysinfo.h> /* get_nprocs */
1514
#include <netinet/in.h>
1615
#include <sys/select.h>
@@ -19,6 +18,21 @@
1918
#include <bpf/btf.h>
2019
#include "json_writer.h"
2120

21+
#ifdef __GLIBC__
22+
#include <execinfo.h> /* backtrace */
23+
#endif
24+
25+
/* Default backtrace funcs if missing at link */
26+
__weak int backtrace(void **buffer, int size)
27+
{
28+
return 0;
29+
}
30+
31+
__weak void backtrace_symbols_fd(void *const *buffer, int size, int fd)
32+
{
33+
dprintf(fd, "<backtrace not supported>\n");
34+
}
35+
2236
static bool verbose(void)
2337
{
2438
return env.verbosity > VERBOSE_NONE;

0 commit comments

Comments
 (0)