mirror of
https://github.com/wolfpld/tracy.git
synced 2024-11-30 01:04:36 +00:00
Decode callstack frames using libbacktrace.
This commit is contained in:
parent
79a5a860a5
commit
9e7714c45a
@ -14,6 +14,7 @@
|
|||||||
# pragma warning( pop )
|
# pragma warning( pop )
|
||||||
# endif
|
# endif
|
||||||
#elif TRACY_HAS_CALLSTACK >= 2
|
#elif TRACY_HAS_CALLSTACK >= 2
|
||||||
|
# include "../libbacktrace/backtrace.hpp"
|
||||||
# include <dlfcn.h>
|
# include <dlfcn.h>
|
||||||
# include <cxxabi.h>
|
# include <cxxabi.h>
|
||||||
#endif
|
#endif
|
||||||
@ -84,18 +85,38 @@ CallstackEntry DecodeCallstackPtr( uint64_t ptr )
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
#elif TRACY_HAS_CALLSTACK == 2
|
#elif TRACY_HAS_CALLSTACK >= 2
|
||||||
|
|
||||||
CallstackEntry DecodeCallstackPtr( uint64_t ptr )
|
enum { MaxCbTrace = 1 };
|
||||||
|
|
||||||
|
struct backtrace_state* cb_bts;
|
||||||
|
int cb_num;
|
||||||
|
CallstackEntry cb_data[MaxCbTrace];
|
||||||
|
|
||||||
|
void InitCallstack()
|
||||||
{
|
{
|
||||||
CallstackEntry ret;
|
cb_bts = backtrace_create_state( nullptr, 0, nullptr, nullptr );
|
||||||
ret.line = 0;
|
}
|
||||||
|
|
||||||
char* demangled = nullptr;
|
static inline char* CopyString( const char* src )
|
||||||
|
{
|
||||||
|
const auto sz = strlen( src );
|
||||||
|
auto dst = (char*)tracy_malloc( sz + 1 );
|
||||||
|
memcpy( dst, src, sz );
|
||||||
|
dst[sz] = '\0';
|
||||||
|
return dst;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int CallstackDataCb( void* data, uintptr_t pc, const char* fn, int lineno, const char* function )
|
||||||
|
{
|
||||||
|
enum { DemangleBufLen = 64*1024 };
|
||||||
|
char demangled[DemangleBufLen];
|
||||||
|
|
||||||
|
if( !fn && !function )
|
||||||
|
{
|
||||||
const char* symname = nullptr;
|
const char* symname = nullptr;
|
||||||
const char* symloc = nullptr;
|
const char* symloc = nullptr;
|
||||||
auto vptr = (void*)ptr;
|
auto vptr = (void*)pc;
|
||||||
char** sym = nullptr;
|
|
||||||
ptrdiff_t symoff = 0;
|
ptrdiff_t symoff = 0;
|
||||||
|
|
||||||
Dl_info dlinfo;
|
Dl_info dlinfo;
|
||||||
@ -103,13 +124,13 @@ CallstackEntry DecodeCallstackPtr( uint64_t ptr )
|
|||||||
{
|
{
|
||||||
symloc = dlinfo.dli_fname;
|
symloc = dlinfo.dli_fname;
|
||||||
symname = dlinfo.dli_sname;
|
symname = dlinfo.dli_sname;
|
||||||
symoff = (char*)ptr - (char*)dlinfo.dli_saddr;
|
symoff = (char*)pc - (char*)dlinfo.dli_saddr;
|
||||||
|
|
||||||
if( symname && symname[0] == '_' )
|
if( symname && symname[0] == '_' )
|
||||||
{
|
{
|
||||||
size_t len = 0;
|
size_t len = DemangleBufLen;
|
||||||
int status;
|
int status;
|
||||||
demangled = abi::__cxa_demangle( symname, nullptr, &len, &status );
|
abi::__cxa_demangle( symname, demangled, &len, &status );
|
||||||
if( status == 0 )
|
if( status == 0 )
|
||||||
{
|
{
|
||||||
symname = demangled;
|
symname = demangled;
|
||||||
@ -117,137 +138,93 @@ CallstackEntry DecodeCallstackPtr( uint64_t ptr )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if( !symname )
|
if( !symname ) symname = "[unknown]";
|
||||||
{
|
if( !symloc ) symloc = "[unknown]";
|
||||||
symname = "[unknown]";
|
|
||||||
}
|
|
||||||
if( !symloc )
|
|
||||||
{
|
|
||||||
symloc = "[unknown]";
|
|
||||||
}
|
|
||||||
|
|
||||||
if( symoff == 0 )
|
if( symoff == 0 )
|
||||||
{
|
{
|
||||||
const auto namelen = strlen( symname );
|
cb_data[cb_num].name = CopyString( symname );
|
||||||
auto name = (char*)tracy_malloc( namelen + 1 );
|
|
||||||
memcpy( name, symname, namelen );
|
|
||||||
name[namelen] = '\0';
|
|
||||||
ret.name = name;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
char buf[32];
|
char buf[32];
|
||||||
sprintf( buf, " + %td", symoff );
|
const auto offlen = sprintf( buf, " + %td", symoff );
|
||||||
const auto offlen = strlen( buf );
|
|
||||||
const auto namelen = strlen( symname );
|
const auto namelen = strlen( symname );
|
||||||
auto name = (char*)tracy_malloc( namelen + offlen + 1 );
|
auto name = (char*)tracy_malloc( namelen + offlen + 1 );
|
||||||
memcpy( name, symname, namelen );
|
memcpy( name, symname, namelen );
|
||||||
memcpy( name + namelen, buf, offlen );
|
memcpy( name + namelen, buf, offlen );
|
||||||
name[namelen + offlen] = '\0';
|
name[namelen + offlen] = '\0';
|
||||||
ret.name = name;
|
cb_data[cb_num].name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
char buf[32];
|
char buf[32];
|
||||||
sprintf( buf, " [%p]", (void*)ptr );
|
const auto addrlen = sprintf( buf, " [%p]", (void*)pc );
|
||||||
const auto addrlen = strlen( buf );
|
|
||||||
const auto loclen = strlen( symloc );
|
const auto loclen = strlen( symloc );
|
||||||
auto loc = (char*)tracy_malloc( loclen + addrlen + 1 );
|
auto loc = (char*)tracy_malloc( loclen + addrlen + 1 );
|
||||||
memcpy( loc, symloc, loclen );
|
memcpy( loc, symloc, loclen );
|
||||||
memcpy( loc + loclen, buf, addrlen );
|
memcpy( loc + loclen, buf, addrlen );
|
||||||
loc[loclen + addrlen] = '\0';
|
loc[loclen + addrlen] = '\0';
|
||||||
ret.file = loc;
|
cb_data[cb_num].file = loc;
|
||||||
|
|
||||||
if( sym ) free( sym );
|
cb_data[cb_num].line = 0;
|
||||||
if( demangled ) free( demangled );
|
}
|
||||||
|
else
|
||||||
return ret;
|
{
|
||||||
|
if( !fn ) fn = "[unknown]";
|
||||||
|
if( !function )
|
||||||
|
{
|
||||||
|
function = "[unknown]";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if( function[0] == '_' )
|
||||||
|
{
|
||||||
|
size_t len = DemangleBufLen;
|
||||||
|
int status;
|
||||||
|
abi::__cxa_demangle( function, demangled, &len, &status );
|
||||||
|
if( status == 0 )
|
||||||
|
{
|
||||||
|
function = demangled;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#elif TRACY_HAS_CALLSTACK == 3
|
cb_data[cb_num].name = CopyString( function );
|
||||||
|
cb_data[cb_num].file = CopyString( fn );
|
||||||
|
cb_data[cb_num].line = lineno;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( ++cb_num >= MaxCbTrace )
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void CallstackErrorCb( void* data, const char* msg, int errnum )
|
||||||
|
{
|
||||||
|
for( int i=0; i<cb_num; i++ )
|
||||||
|
{
|
||||||
|
tracy_free( (void*)cb_data[i].name );
|
||||||
|
tracy_free( (void*)cb_data[i].file );
|
||||||
|
}
|
||||||
|
|
||||||
|
cb_data[0].name = CopyString( "[error]" );
|
||||||
|
cb_data[0].file = CopyString( "[error]" );
|
||||||
|
cb_data[0].line = 0;
|
||||||
|
|
||||||
|
cb_num = 1;
|
||||||
|
}
|
||||||
|
|
||||||
CallstackEntry DecodeCallstackPtr( uint64_t ptr )
|
CallstackEntry DecodeCallstackPtr( uint64_t ptr )
|
||||||
{
|
{
|
||||||
CallstackEntry ret;
|
cb_num = 0;
|
||||||
ret.line = 0;
|
backtrace_pcinfo( cb_bts, ptr, CallstackDataCb, CallstackErrorCb, nullptr );
|
||||||
|
assert( cb_num == 1 );
|
||||||
char* demangled = nullptr;
|
return cb_data[0];
|
||||||
const char* symname = nullptr;
|
|
||||||
const char* symloc = nullptr;
|
|
||||||
auto vptr = (void*)ptr;
|
|
||||||
char** sym = nullptr;
|
|
||||||
ptrdiff_t symoff = 0;
|
|
||||||
|
|
||||||
Dl_info dlinfo;
|
|
||||||
if( dladdr( vptr, &dlinfo ) )
|
|
||||||
{
|
|
||||||
symloc = dlinfo.dli_fname;
|
|
||||||
symname = dlinfo.dli_sname;
|
|
||||||
symoff = (char*)ptr - (char*)dlinfo.dli_saddr;
|
|
||||||
|
|
||||||
if( symname && symname[0] == '_' )
|
|
||||||
{
|
|
||||||
size_t len = 0;
|
|
||||||
int status;
|
|
||||||
demangled = abi::__cxa_demangle( symname, nullptr, &len, &status );
|
|
||||||
if( status == 0 )
|
|
||||||
{
|
|
||||||
symname = demangled;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if( !symname )
|
|
||||||
{
|
|
||||||
sym = backtrace_symbols( &vptr, 1 );
|
|
||||||
if( !sym )
|
|
||||||
{
|
|
||||||
symname = "[unknown]";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
symname = *sym;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if( !symloc )
|
|
||||||
{
|
|
||||||
symloc = "[unknown]";
|
|
||||||
}
|
|
||||||
|
|
||||||
if( symoff == 0 )
|
|
||||||
{
|
|
||||||
const auto namelen = strlen( symname );
|
|
||||||
auto name = (char*)tracy_malloc( namelen + 1 );
|
|
||||||
memcpy( name, symname, namelen );
|
|
||||||
name[namelen] = '\0';
|
|
||||||
ret.name = name;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
char buf[32];
|
|
||||||
sprintf( buf, " + %td", symoff );
|
|
||||||
const auto offlen = strlen( buf );
|
|
||||||
const auto namelen = strlen( symname );
|
|
||||||
auto name = (char*)tracy_malloc( namelen + offlen + 1 );
|
|
||||||
memcpy( name, symname, namelen );
|
|
||||||
memcpy( name + namelen, buf, offlen );
|
|
||||||
name[namelen + offlen] = '\0';
|
|
||||||
ret.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
char buf[32];
|
|
||||||
sprintf( buf, " [%p]", (void*)ptr );
|
|
||||||
const auto addrlen = strlen( buf );
|
|
||||||
const auto loclen = strlen( symloc );
|
|
||||||
auto loc = (char*)tracy_malloc( loclen + addrlen + 1 );
|
|
||||||
memcpy( loc, symloc, loclen );
|
|
||||||
memcpy( loc + loclen, buf, addrlen );
|
|
||||||
loc[loclen + addrlen] = '\0';
|
|
||||||
ret.file = loc;
|
|
||||||
|
|
||||||
if( sym ) free( sym );
|
|
||||||
if( demangled ) free( demangled );
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -42,11 +42,10 @@ struct CallstackEntry
|
|||||||
};
|
};
|
||||||
|
|
||||||
CallstackEntry DecodeCallstackPtr( uint64_t ptr );
|
CallstackEntry DecodeCallstackPtr( uint64_t ptr );
|
||||||
|
void InitCallstack();
|
||||||
|
|
||||||
#if TRACY_HAS_CALLSTACK == 1
|
#if TRACY_HAS_CALLSTACK == 1
|
||||||
|
|
||||||
void InitCallstack();
|
|
||||||
|
|
||||||
static tracy_force_inline void* Callstack( int depth )
|
static tracy_force_inline void* Callstack( int depth )
|
||||||
{
|
{
|
||||||
assert( depth >= 1 && depth < 63 );
|
assert( depth >= 1 && depth < 63 );
|
||||||
@ -60,8 +59,6 @@ static tracy_force_inline void* Callstack( int depth )
|
|||||||
|
|
||||||
#elif TRACY_HAS_CALLSTACK == 2
|
#elif TRACY_HAS_CALLSTACK == 2
|
||||||
|
|
||||||
static tracy_force_inline void InitCallstack() {}
|
|
||||||
|
|
||||||
struct BacktraceState
|
struct BacktraceState
|
||||||
{
|
{
|
||||||
void** current;
|
void** current;
|
||||||
@ -95,8 +92,6 @@ static tracy_force_inline void* Callstack( int depth )
|
|||||||
|
|
||||||
#elif TRACY_HAS_CALLSTACK == 3
|
#elif TRACY_HAS_CALLSTACK == 3
|
||||||
|
|
||||||
static tracy_force_inline void InitCallstack() {}
|
|
||||||
|
|
||||||
static tracy_force_inline void* Callstack( int depth )
|
static tracy_force_inline void* Callstack( int depth )
|
||||||
{
|
{
|
||||||
assert( depth >= 1 );
|
assert( depth >= 1 );
|
||||||
|
Loading…
Reference in New Issue
Block a user