This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: why no stacktrace?


> 
> If you're using glibc 2.1 (e.g. in newer Linux distributions) you can use
> its builtin backtrace() function. GNU binutils' addr2line converts it
> to symbol addresses.
> 
I didn't know about backtrace(); thanks a lot.

> % cat tbt.c
> #include <stdlib.h>
> #include <signal.h>
> #include <execinfo.h>
> 
> void crash()
> {
>         void *bt[10];
>         int n,c;
>         n = backtrace(bt,10);
>         for (c=0; c<n; c++)
>                 printf("%#x\n",bt[c]);
>         abort();  // generate core dump anyways
> }
> 
> main()
> {
>         signal(SIGSEGV, crash);
>         *((int *)0) = 0;
> }
> % cc -g tbt.c
> % ./a.out  > X
> zsh: abort (core dumped)  ./a.out > X
> % addr2line < X

Better than printing to stdout (as in your example) would be to pipe
directly into addr2line within the crash procedure. Thus you don't need to start
the executable again.

	-ulrich

----------------------------------------------------------------------------
Ulrich Lauther          ph: +49 89 636 48834 fx: ... 636 42284
Siemens ZT SE 4         Internet: Ulrich.Lauther@mchp.siemens.de

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]