Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On Raspberry Pi, backtrace() returns 0 frames

I'm playing around with glibc's backtrace() and I can't seem to get it to work right on my Raspberry Pi. Everything compiles with no warnings, but backtrace() returns 0 as the number of frames stored. The exact same code does produce a backtrace on x86_64 (Debian stable), with backtrace() returning 6 frames:


/*test.c*/
#include <stdio.h>
#include <execinfo.h>
#include <unistd.h>

void foo(void)
{
    void *stack[10];
    int n = backtrace(stack, 10);
    fprintf(stderr, "Last %d frames:\n", n);
    backtrace_symbols_fd(stack, n, STDERR_FILENO);
}

void bar(void)
{
    foo();
}

void baz(void)
{
    bar();
}

int main(void)
{
    printf("Hello, Backtrace\n");
    baz();
    return 0;
}

#Makefile
CFLAGS=-Wall -Wextra -g
LDFLAGS=-rdynamic

Output on x86_64 (gcc (Debian 4.7.2-5) 4.7.2 with GNU libc6:amd64 2.13-38+deb7u1):

Hello, Backtrace
Last 6 frames:
./test(foo+0x19)[0x4009a5]
./test(bar+0x9)[0x4009e5]
./test(baz+0x9)[0x4009f0]
./test(main+0x13)[0x400a05]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xfd)[0x7f9286ddbead]
./test[0x4008a9]

Output on Raspberry Pi (gcc (Debian 4.6.3-14+rpi1) 4.6.3 with GNU libc6:armhf (2.13-38+rpi2+deb7u1)):

Hello, Backtrace
Last 0 frames:

I have verified that the compiled executable on Raspberry Pi is storing the frame pointer and link register to the stack. objdump -d test excerpt:

0000882c <bar>:
    882c:       e92d4800        push    {fp, lr}
    8830:       e28db004        add     fp, sp, #4
    8834:       ebffffe3        bl      87c8 <foo>
    8838:       e8bd8800        pop     {fp, pc}

0000883c <baz>:
    883c:       e92d4800        push    {fp, lr}
    8840:       e28db004        add     fp, sp, #4
    8844:       ebfffff8        bl      882c <bar>
    8848:       e8bd8800        pop     {fp, pc}

I haven't found anything like this on the forums or Stack Overflow. Am I doing something wrong? What haven't I checked?

like image 800
laindir Avatar asked Jul 11 '14 14:07

laindir


1 Answers

The application must be compiled with -funwind-tables to make backtrace() work on ARM.

like image 53
nos Avatar answered Oct 05 '22 23:10

nos