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?


ulrich.lauther@mchp.siemens.de (Ulrich Lauther) writes:

> I am a rather old programmer starting with Algol in the 60ies, then Fortran,
> Pascal ...
> All these languages, i.e., its compilers, (even Fortran) provided a stack
> trace when a program crashed. Not so gcc/g++. Why not?
> Yes, I know, there are debuggers these days, but it is very convenient
> to have a stack trace when a program crashes at a customer to get a rough
> first idea about the problem.

In the unix world you enable core dumps for this case (using ulimit -c).
Often they are enabled by default. Then you have the complete program image,
not only the backtrace.

> 
> I wrote my own stack trace procedure based on  __builtin_return_address()
> and using the addr2line utility from binutils (which I wrote a few years ago)
> to translate addresses into line numbers.
> 
> However, to have a builtin stack trace, called on unnormal termination would be
> much better and I guess it would be quite easy to implement by someone who knows
> the inner workings of the compiler.

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.

% 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
tbt.c:9
tbt.c:20
tbt.c:20
(null):0
%

-Andi

-- 
This is like TV. I don't like TV.

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