Unix Programming - printing backtrace

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2005 > printing backtrace





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author printing backtrace
alex goldman

2005-05-27, 5:56 pm

I wonder if the assert macro can be modified in any way that would make the
program (assuming any necessary compile flags for GCC/G++) print the
backtrace whenever the error occurs.

Assert prints the location in the code, where the error took place, but not
what called the code that called the code that called the code that called
the code that had this assert.
Jens.Toerring@physik.fu-berlin.de

2005-05-28, 7:48 am

alex goldman <hello@spamm.er> wrote:
> I wonder if the assert macro can be modified in any way that would make the
> program (assuming any necessary compile flags for GCC/G++) print the
> backtrace whenever the error occurs.


There are some possibilities, but, of course, they depend on
either some support by the libc or the availability of a de-
bugger on the machine.

The GNU libc has some functions that you can use to print out a
backtrace, see the info files for the libc and look below the
menu entry "Debugging Support" for the functions backtrace(),
backtrace_symbols() and backtrace_symbols_fd().

Alternatively, there's a nice method that uses the debugger
itself to do the job - you spawn a child process that execs
the debugger, which in turn attaches to the parent process
and then is used to print out the backtrace. Bjorn Reese
allows you to download his code for doing this on IRIX,
HP/UX and with gcc at

http://home1.stofanet.dk/breese/debug/debug.tar.gz

Please note that this may not work if your program is setuid'ed
since typically such processes can't be attached to for security
reasons.

The rest is more or less trivial, here's a .h and .c file
for replacing the normal assert() and that hopefully will
get you started (include this header file instead of the
normal <assert.h> ):

------- my_assert.h -----------------------------------------

#if ! defined MY_ASSERT_HEADER
#define MY_ASSERT_HEADER

int my_assert( const char * /* expression */,
const char * /* filename */,
int /* line */ );

#ifdef NDEBUG
#define assert( expression ) ( ( void ) 0 )
#else
#define assert( expression ) \
( ( void ) ( ( expression ) ? \
0 : my_assert( #expression, __FILE__, __LINE__ ) ) )
#endif

#endif /* ! MY_ASSERT__HEADER */


------- my_assert.c -----------------------------------------

int my_assert( const char *expression, const char *filename,
int line )
{
fprintf( stderr, "%s:%d: failed assertion: %s\n", filename,
line, expression );

/* Put here everything you need to do the backtrace */

abort( );
return 0;
}
Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com