I'm compiling a statically linked i386 binary for Linux, using uClibc. The backtrace library function to generate a stack trace of addresses is not available. I need a replacement.
Both http://code.metager.de/source/xref/lib/eglibc/libc/sysdeps/i386/backtrace.c and https://github.com/hwoarang/uClibc/blob/master-metag/libubacktrace/backtrace.c seem to load the function _Unwind_Backtrace from libgcc_c.so.1
. However, libgcc_so.1
is not available in my toolchain, and when I try to use _Unwind_backtrace directly, I get a stack trace of only 1 frame (the topmost one, from the function calling _Unwind_Backtrace.
I need a working replacement for backtrace or _Unwind_Backtrace in my statically linked i386 Linux executable, linked against uClibc, without loading any .so files (such as libgcc_c.so.1
), which can generate a full stack trace (only the instruction pointers). Where is such an implementation available?
FYI for gcc __builtin_return_address
generates code like this:
movl 0(%ebp), %eax
movl (%eax), %eax
movl (%eax), %eax
movl (%eax), %eax
movl (%eax), %eax
movl (%eax), %eax
movl (%eax), %eax
movl (%eax), %eax
movl 4(%eax), %eax
This just blindly walks up the frame chain, without any bounds checking or sanity checking. I'd like to use something safer than that.
Please note that even glibc backtrace(3) doesn't display frames in functions compiled with gcc -fomit-frame-pointer, I've decided that I don't need that.