1

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.

pts
  • 80,836
  • 20
  • 110
  • 183
  • 1
    This is somewhat hackish but it should work with GCC(-compatible compilers): [execinfo.c](https://www.varnish-cache.org/trac/browser/lib/libvarnishcompat/execinfo.c?rev=6ea89fd1ed370ffab7af3ac871bba20f4bbfac7e) – The Paramagnetic Croissant Apr 27 '14 at 10:36
  • @user3477950: Thx. It looks like __builtin_return_address(i) just blindly walks up the ebp chain. I've updated the question. – pts Apr 27 '14 at 11:02

0 Answers0