123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- #ifndef _STACKTRACE_H_
- #define _STACKTRACE_H_
- #include <stdio.h>
- #include <stdlib.h>
- #include <execinfo.h>
- #include <cxxabi.h>
- static inline void print_stacktrace( FILE *out = stdout, unsigned int max_frames = 3 )
- {
- fprintf( out, "stack trace:\n" );
-
- void* addrlist[ max_frames + 1 ];
-
- int addrlen = backtrace( addrlist, sizeof( addrlist ) / sizeof( void* ) );
- if( addrlen == 0 )
- {
- fprintf( out, " <empty, possibly corrupt>\n" );
- return;
- }
-
-
- char** symbollist = backtrace_symbols( addrlist, addrlen );
-
- size_t funcnamesize = 256;
- char* funcname = (char*)malloc( funcnamesize );
-
-
- for( int i = 1; i < addrlen; i++ )
- {
- char *begin_name = 0, *begin_offset = 0, *end_offset = 0;
-
-
- for( char *p = symbollist[ i ]; *p; ++p )
- {
- if( *p == '(' )
- begin_name = p;
- else if( *p == '+' )
- begin_offset = p;
- else if( *p == ')' && begin_offset )
- {
- end_offset = p;
- break;
- }
- }
- if( begin_name && begin_offset && end_offset
- && begin_name < begin_offset )
- {
- *begin_name++ = '\0';
- *begin_offset++ = '\0';
- *end_offset = '\0';
-
-
-
- int status;
- char* ret = abi::__cxa_demangle( begin_name,
- funcname, &funcnamesize, &status );
- if( status == 0 )
- {
- funcname = ret;
- char line[ 256 ];
- sprintf( line, " %s : %s+%s\n",
- symbollist[ i ], funcname, begin_offset );
- int y = 0;
- }
- else
- {
-
-
- char line[ 256 ];
- sprintf( line, " %s : %s()+%s\n",
- symbollist[ i ], begin_name, begin_offset );
- int x = 0;
- }
- }
- else
- {
-
- char line[ 256 ];
- sprintf( line, " %s\n", symbollist[ i ] );
- int x = 0;
- }
- }
- free( funcname );
- free( symbollist );
- }
- #endif
|