FAQ: "Backtrace" causes a seg fault when compiled

Hi All,

I wanted to share the following report sent in to PGI customer support. The user was trying to use the “backtrace” function in a static binary causing the code to seg fault. For example:

bicycle:/tmp% cat back.c
#include <stdio.h>
#include <execinfo.h>
int
main ( int argc, char *argv [] )
{
    int num_pcs;
    void *pcs [ 32 ];
    setvbuf ( stdout, 0, _IONBF, 0 );
    printf ( "calling backtrace\n" );
    num_pcs = backtrace ( pcs, sizeof ( pcs ) / sizeof ( pcs [ 0 ] ) );
    printf ( "back\n" );
    return 0;
}
bicycle:/tmp% pgcc -Bstatic back.c
bicycle:/tmp% a.out
calling backtrace
Segmentation fault (core dumped)

There are two problems here. The first was the we (PGI) weren’t linking the correct crtbegin.o object which included static traceback (crtbeginT.o). While this was fixed with the 7.2 release, the same code will still seg fault on systems that use GCC 3.3.3. This is a problem with crtbeginT.o itself which was fixed with later versions of GCC.

If you encounter this problem either don’t statically link system files when using backtrace (i.e. link with -Bstatic_pgi which statically links the PGI libraries but not the system libraries.) or update your PGI and GCC versions.

Hope this helps,

  • Mat