2018-06-18 23:17:19 +00:00
|
|
|
#include "TracyCallstack.hpp"
|
|
|
|
|
|
|
|
#ifdef TRACY_HAS_CALLSTACK
|
|
|
|
|
2018-06-19 17:49:21 +00:00
|
|
|
#if defined _WIN32 || defined __CYGWIN__
|
2018-06-20 19:01:25 +00:00
|
|
|
# include <windows.h>
|
|
|
|
# include <dbghelp.h>
|
2018-06-20 20:54:42 +00:00
|
|
|
#elif defined _GNU_SOURCE
|
|
|
|
# include <dlfcn.h>
|
2018-06-20 21:01:00 +00:00
|
|
|
# include <cxxabi.h>
|
2018-06-18 23:17:19 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
2018-06-20 19:45:27 +00:00
|
|
|
#if defined _WIN32 || defined __CYGWIN__
|
|
|
|
|
2018-06-18 23:17:19 +00:00
|
|
|
void InitCallstack()
|
|
|
|
{
|
|
|
|
SymInitialize( GetCurrentProcess(), nullptr, true );
|
|
|
|
SymSetOptions( SYMOPT_LOAD_LINES );
|
|
|
|
}
|
|
|
|
|
|
|
|
CallstackEntry DecodeCallstackPtr( uint64_t ptr )
|
|
|
|
{
|
|
|
|
CallstackEntry ret;
|
|
|
|
|
|
|
|
const auto proc = GetCurrentProcess();
|
|
|
|
|
|
|
|
char buf[sizeof( SYMBOL_INFO ) + 255];
|
|
|
|
auto si = (SYMBOL_INFO*)buf;
|
|
|
|
si->SizeOfStruct = sizeof( SYMBOL_INFO );
|
|
|
|
si->MaxNameLen = 255;
|
|
|
|
|
2018-06-19 23:05:44 +00:00
|
|
|
if( SymFromAddr( proc, ptr, nullptr, si ) == 0 )
|
|
|
|
{
|
|
|
|
memcpy( si->Name, "[unknown]", 10 );
|
|
|
|
si->NameLen = 9;
|
|
|
|
}
|
2018-06-18 23:17:19 +00:00
|
|
|
|
|
|
|
auto name = (char*)tracy_malloc( si->NameLen + 1 );
|
|
|
|
memcpy( name, si->Name, si->NameLen );
|
|
|
|
name[si->NameLen] = '\0';
|
|
|
|
|
|
|
|
ret.name = name;
|
|
|
|
|
2018-06-20 18:35:57 +00:00
|
|
|
const char* filename;
|
2018-06-18 23:17:19 +00:00
|
|
|
IMAGEHLP_LINE64 line;
|
2018-06-20 19:01:25 +00:00
|
|
|
DWORD displacement = 0;
|
2018-06-18 23:17:19 +00:00
|
|
|
line.SizeOfStruct = sizeof( IMAGEHLP_LINE64 );
|
2018-06-19 23:05:44 +00:00
|
|
|
if( SymGetLineFromAddr64( proc, ptr, &displacement, &line ) == 0 )
|
|
|
|
{
|
2018-06-20 18:35:57 +00:00
|
|
|
filename = "[unknown]";
|
2018-06-19 23:05:44 +00:00
|
|
|
ret.line = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-20 18:35:57 +00:00
|
|
|
filename = line.FileName;
|
2018-06-19 23:05:44 +00:00
|
|
|
ret.line = line.LineNumber;
|
|
|
|
}
|
2018-06-18 23:17:19 +00:00
|
|
|
|
2018-06-20 18:35:57 +00:00
|
|
|
const auto fsz = strlen( filename );
|
2018-06-19 23:26:05 +00:00
|
|
|
auto file = (char*)tracy_malloc( fsz + 1 );
|
2018-06-20 18:35:57 +00:00
|
|
|
memcpy( file, filename, fsz );
|
2018-06-19 23:26:05 +00:00
|
|
|
file[fsz] = '\0';
|
|
|
|
|
|
|
|
ret.file = file;
|
|
|
|
|
2018-06-18 23:17:19 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-06-21 15:07:21 +00:00
|
|
|
#elif defined __ANDROID__
|
|
|
|
|
|
|
|
CallstackEntry DecodeCallstackPtr( uint64_t ptr )
|
|
|
|
{
|
|
|
|
CallstackEntry ret;
|
|
|
|
ret.line = 0;
|
|
|
|
|
|
|
|
char* demangled = nullptr;
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
symname = "[unknown]";
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-06-20 19:45:27 +00:00
|
|
|
#elif defined _GNU_SOURCE
|
|
|
|
|
|
|
|
CallstackEntry DecodeCallstackPtr( uint64_t ptr )
|
|
|
|
{
|
|
|
|
CallstackEntry ret;
|
2018-06-20 20:54:42 +00:00
|
|
|
ret.line = 0;
|
2018-06-20 19:45:27 +00:00
|
|
|
|
2018-06-20 21:01:00 +00:00
|
|
|
char* demangled = nullptr;
|
2018-06-20 20:54:42 +00:00
|
|
|
const char* symname = nullptr;
|
|
|
|
const char* symloc = nullptr;
|
2018-06-20 19:45:27 +00:00
|
|
|
auto vptr = (void*)ptr;
|
2018-06-20 20:54:42 +00:00
|
|
|
char** sym = nullptr;
|
2018-06-21 11:10:48 +00:00
|
|
|
ptrdiff_t symoff = 0;
|
2018-06-20 20:54:42 +00:00
|
|
|
|
|
|
|
Dl_info dlinfo;
|
|
|
|
if( dladdr( vptr, &dlinfo ) )
|
2018-06-20 19:45:27 +00:00
|
|
|
{
|
2018-06-20 20:54:42 +00:00
|
|
|
symloc = dlinfo.dli_fname;
|
|
|
|
symname = dlinfo.dli_sname;
|
2018-06-21 11:10:48 +00:00
|
|
|
symoff = (char*)ptr - (char*)dlinfo.dli_saddr;
|
2018-06-20 21:01:00 +00:00
|
|
|
|
|
|
|
if( symname && symname[0] == '_' )
|
|
|
|
{
|
|
|
|
size_t len = 0;
|
|
|
|
int status;
|
|
|
|
demangled = abi::__cxa_demangle( symname, nullptr, &len, &status );
|
|
|
|
if( status == 0 )
|
|
|
|
{
|
|
|
|
symname = demangled;
|
|
|
|
}
|
|
|
|
}
|
2018-06-20 19:45:27 +00:00
|
|
|
}
|
2018-06-20 20:54:42 +00:00
|
|
|
|
|
|
|
if( !symname )
|
2018-06-20 19:45:27 +00:00
|
|
|
{
|
2018-06-20 20:54:42 +00:00
|
|
|
sym = backtrace_symbols( &vptr, 1 );
|
|
|
|
if( !sym )
|
|
|
|
{
|
|
|
|
symname = "[unknown]";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
symname = *sym;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( !symloc )
|
|
|
|
{
|
|
|
|
symloc = "[unknown]";
|
2018-06-20 19:45:27 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 11:10:48 +00:00
|
|
|
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;
|
|
|
|
}
|
2018-06-20 19:45:27 +00:00
|
|
|
|
2018-06-21 11:14:13 +00:00
|
|
|
char buf[32];
|
|
|
|
sprintf( buf, " [%p]", (void*)ptr );
|
|
|
|
const auto addrlen = strlen( buf );
|
2018-06-20 20:54:42 +00:00
|
|
|
const auto loclen = strlen( symloc );
|
2018-06-21 11:14:13 +00:00
|
|
|
auto loc = (char*)tracy_malloc( loclen + addrlen + 1 );
|
2018-06-20 20:54:42 +00:00
|
|
|
memcpy( loc, symloc, loclen );
|
2018-06-21 11:14:13 +00:00
|
|
|
memcpy( loc + loclen, buf, addrlen );
|
|
|
|
loc[loclen + addrlen] = '\0';
|
2018-06-20 20:54:42 +00:00
|
|
|
ret.file = loc;
|
2018-06-20 19:45:27 +00:00
|
|
|
|
2018-06-20 20:18:12 +00:00
|
|
|
if( sym ) free( sym );
|
2018-06-20 21:01:00 +00:00
|
|
|
if( demangled ) free( demangled );
|
2018-06-20 20:18:12 +00:00
|
|
|
|
2018-06-20 19:45:27 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2018-06-18 23:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|