https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123265
Bug ID: 123265
Summary: std::stacktrace reports broken stacktraces when using
separate debug symbols
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: moncef.mechri at gmail dot com
Target Milestone: ---
On Linux x86_64, using gcc 14.2.0, the following program prints a reasonable
stacktrace:
#include <print>
#include <stacktrace>
void foo()
{
std::println("{}", std::stacktrace::current());
}
int main()
{
foo();
}
$ g++ -std=c++23 -O2 -g -o test_stacktrace test_stacktrace.cpp -lstdc++exp
$ ./test_backtrace
0# foo() at /home/mmechri/test_stacktrace.cpp:6
1# main at /home/mmechri/test_stacktrace.cpp:11
2# __libc_start_main at ../csu/libc-start.c:308
3# _start at :0
4#
However, when moving the debug symbols to a separate file, the stacktrace is
broken:
$ objcopy --only-keep-debug test_stacktrace test_stacktrace.debug
$ objcopy --strip-unneeded --add-gnu-debuglink=test_stacktrace.debug
test_stacktrace test_stacktrace
$ ./test_stacktrace
0# at :0
1# at :0
2# __libc_start_main at ../csu/libc-start.c:308
3# at :0
4#
After debugging, I noticed that the call to elf_is_symlink() (and the following
while() loop) in elf_find_debugfile_by_debuglink() (from elf.c) are not present
in libstdc++exp.a. I believe this is because HAVE_LSTAT is not defined in
BACKTRACE_CPPFLAGS, which causes lstat() to always return -1, and thus
elf_is_symlink() to be a no-op that gets compiled out.
I believe a similar issue may exist with HAVE_READLINK / elf_readlink()