Linux callstack retrieval.

This commit is contained in:
Bartosz Taudul 2018-06-20 21:45:27 +02:00
parent dc20742b5b
commit 5541cd6c97
2 changed files with 61 additions and 1 deletions

View File

@ -10,6 +10,8 @@
namespace tracy
{
#if defined _WIN32 || defined __CYGWIN__
void InitCallstack()
{
SymInitialize( GetCurrentProcess(), nullptr, true );
@ -64,6 +66,44 @@ CallstackEntry DecodeCallstackPtr( uint64_t ptr )
return ret;
}
#elif defined _GNU_SOURCE
CallstackEntry DecodeCallstackPtr( uint64_t ptr )
{
CallstackEntry ret;
const char* symname;
auto vptr = (void*)ptr;
auto sym = backtrace_symbols( &vptr, 1 );
if( !sym )
{
symname = "[unknown]";
}
else
{
symname = *sym;
free( sym );
}
const auto namelen = strlen( symname );
auto name = (char*)tracy_malloc( namelen + 1 );
memcpy( name, symname, namelen );
name[namelen] = '\0';
ret.name = name;
auto unknown = (char*)tracy_malloc( 10 );
memcpy( unknown, "[unknown]", 9 );
unknown[9] = '\0';
ret.file = unknown;
ret.line = 0;
return ret;
}
#endif
}
#endif

View File

@ -10,8 +10,12 @@ extern "C" __declspec(dllimport) unsigned short __stdcall RtlCaptureStackBackTra
extern "C" __declspec(dllimport) unsigned short __stdcall RtlCaptureStackBackTrace( unsigned long, unsigned long, void**, unsigned long* );
# endif
# endif
#elif defined _GNU_SOURCE
# define TRACY_HAS_CALLSTACK
# include <execinfo.h>
#endif
#ifdef TRACY_HAS_CALLSTACK
#include <assert.h>
@ -31,10 +35,11 @@ struct CallstackEntry
uint32_t line;
};
CallstackEntry DecodeCallstackPtr( uint64_t ptr );
#if defined _WIN32 || defined __CYGWIN__
void InitCallstack();
CallstackEntry DecodeCallstackPtr( uint64_t ptr );
static tracy_force_inline void* Callstack( int depth )
{
@ -47,6 +52,21 @@ static tracy_force_inline void* Callstack( int depth )
return trace;
}
#elif defined _GNU_SOURCE
static tracy_force_inline void InitCallstack() {}
static tracy_force_inline void* Callstack( int depth )
{
assert( depth >= 1 );
auto trace = (uintptr_t*)tracy_malloc( ( 1 + depth ) * sizeof( uintptr_t ) );
const auto num = backtrace( (void**)(trace+1), depth );
*trace = num;
return trace;
}
#endif
}