tracy/client/TracyCallstack.cpp

587 lines
14 KiB
C++
Raw Normal View History

#include <algorithm>
#include <stdio.h>
2018-06-18 23:17:19 +00:00
#include "TracyCallstack.hpp"
#ifdef TRACY_HAS_CALLSTACK
#if TRACY_HAS_CALLSTACK == 1
# include <windows.h>
# ifdef _MSC_VER
# pragma warning( push )
# pragma warning( disable : 4091 )
# endif
# include <dbghelp.h>
# ifdef _MSC_VER
# pragma warning( pop )
# endif
#elif TRACY_HAS_CALLSTACK == 2 || TRACY_HAS_CALLSTACK == 3
# include "../libbacktrace/backtrace.hpp"
# include <dlfcn.h>
2018-06-20 21:01:00 +00:00
# include <cxxabi.h>
#elif TRACY_HAS_CALLSTACK == 4 || TRACY_HAS_CALLSTACK == 5
# include <dlfcn.h>
# include <cxxabi.h>
2018-06-18 23:17:19 +00:00
#endif
namespace tracy
{
#if TRACY_HAS_CALLSTACK == 1
2018-06-20 19:45:27 +00:00
enum { MaxCbTrace = 16 };
int cb_num;
CallstackEntry cb_data[MaxCbTrace];
2018-12-28 23:50:41 +00:00
extern "C" { t_RtlWalkFrameChain RtlWalkFrameChain = 0; }
2018-06-18 23:17:19 +00:00
void InitCallstack()
{
2018-06-25 08:55:07 +00:00
#ifdef UNICODE
RtlWalkFrameChain = (t_RtlWalkFrameChain)GetProcAddress( GetModuleHandle( L"ntdll.dll" ), "RtlWalkFrameChain" );
#else
RtlWalkFrameChain = (t_RtlWalkFrameChain)GetProcAddress( GetModuleHandle( "ntdll.dll" ), "RtlWalkFrameChain" );
2018-06-25 08:55:07 +00:00
#endif
2018-06-18 23:17:19 +00:00
SymInitialize( GetCurrentProcess(), nullptr, true );
SymSetOptions( SYMOPT_LOAD_LINES );
}
const char* DecodeCallstackPtrFast( uint64_t ptr )
{
static char ret[1024];
const auto proc = GetCurrentProcess();
char buf[sizeof( SYMBOL_INFO ) + 1024];
auto si = (SYMBOL_INFO*)buf;
si->SizeOfStruct = sizeof( SYMBOL_INFO );
si->MaxNameLen = 1024;
if( SymFromAddr( proc, ptr, nullptr, si ) == 0 )
{
*ret = '\0';
}
else
{
memcpy( ret, si->Name, si->NameLen );
ret[si->NameLen] = '\0';
}
return ret;
}
CallstackEntryData DecodeCallstackPtr( uint64_t ptr )
2018-06-18 23:17:19 +00:00
{
2019-02-02 12:58:17 +00:00
int write;
2018-06-18 23:17:19 +00:00
const auto proc = GetCurrentProcess();
2019-02-02 12:58:17 +00:00
#ifndef __CYGWIN__
const auto inlineNum = std::min<DWORD>( MaxCbTrace - 1, SymAddrIncludeInlineTrace( proc, ptr ) );
2019-02-20 15:28:27 +00:00
DWORD ctx = 0;
DWORD idx;
BOOL doInline = FALSE;
if( inlineNum != 0 ) doInline = SymQueryInlineTrace( proc, ptr, 0, ptr, ptr, &ctx, &idx );
if( doInline )
{
write = inlineNum;
cb_num = 1 + inlineNum;
}
else
2019-02-02 12:58:17 +00:00
#endif
{
write = 0;
cb_num = 1;
}
2018-06-18 23:17:19 +00:00
char buf[sizeof( SYMBOL_INFO ) + 1024];
2018-06-18 23:17:19 +00:00
auto si = (SYMBOL_INFO*)buf;
si->SizeOfStruct = sizeof( SYMBOL_INFO );
si->MaxNameLen = 1024;
2018-06-18 23:17:19 +00:00
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
IMAGEHLP_LINE64 line;
DWORD displacement = 0;
2019-02-20 15:28:27 +00:00
line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
2018-06-19 23:05:44 +00:00
{
2019-02-20 15:28:27 +00:00
auto name = (char*)tracy_malloc(si->NameLen + 1);
memcpy(name, si->Name, si->NameLen);
name[si->NameLen] = '\0';
2018-06-18 23:17:19 +00:00
2019-02-20 15:28:27 +00:00
cb_data[write].name = name;
2019-02-20 15:28:27 +00:00
const char* filename;
if (SymGetLineFromAddr64(proc, ptr, &displacement, &line) == 0)
{
filename = "[unknown]";
cb_data[write].line = 0;
}
else
{
filename = line.FileName;
cb_data[write].line = line.LineNumber;
}
const auto fsz = strlen(filename);
auto file = (char*)tracy_malloc(fsz + 1);
memcpy(file, filename, fsz);
file[fsz] = '\0';
cb_data[write].file = file;
}
2019-02-02 12:58:17 +00:00
#ifndef __CYGWIN__
if( doInline )
{
for( DWORD i=0; i<inlineNum; i++ )
{
auto& cb = cb_data[i];
if( SymFromInlineContext( proc, ptr, ctx, nullptr, si ) == 0 )
{
memcpy( si->Name, "[unknown]", 10 );
si->NameLen = 9;
}
auto name = (char*)tracy_malloc( si->NameLen + 1 );
memcpy( name, si->Name, si->NameLen );
name[si->NameLen] = '\0';
cb.name = name;
2019-02-20 15:28:27 +00:00
const char* filename;
if( SymGetLineFromInlineContext( proc, ptr, ctx, 0, &displacement, &line ) == 0 )
{
filename = "[unknown]";
cb.line = 0;
}
else
{
filename = line.FileName;
cb.line = line.LineNumber;
}
const auto fsz = strlen( filename );
auto file = (char*)tracy_malloc( fsz + 1 );
memcpy( file, filename, fsz );
file[fsz] = '\0';
cb.file = file;
ctx++;
}
}
2019-02-02 12:58:17 +00:00
#endif
return { cb_data, uint8_t( cb_num ) };
2018-06-18 23:17:19 +00:00
}
2019-02-20 15:01:41 +00:00
#elif TRACY_HAS_CALLSTACK == 4
void InitCallstack()
{
}
const char* DecodeCallstackPtrFast( uint64_t ptr )
{
static char ret[1024];
auto vptr = (void*)ptr;
char** sym = nullptr;
const char* symname = nullptr;
Dl_info dlinfo;
if( dladdr( vptr, &dlinfo ) && dlinfo.dli_sname )
{
symname = dlinfo.dli_sname;
}
else
{
sym = backtrace_symbols( &vptr, 1 );
if( sym )
{
symname = *sym;
}
}
if( symname )
{
strcpy( ret, symname );
}
else
{
*ret = '\0';
}
return ret;
}
2019-02-20 15:01:41 +00:00
CallstackEntryData DecodeCallstackPtr( uint64_t ptr )
{
static CallstackEntry cb;
cb.line = 0;
2019-02-21 14:04:17 +00:00
char* demangled = nullptr;
2019-02-20 15:01:41 +00:00
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';
cb.name = name;
}
else
{
char buf[32];
const auto offlen = sprintf( buf, " + %td", symoff );
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';
cb.name = name;
}
char buf[32];
const auto addrlen = sprintf( buf, " [%p]", (void*)ptr );
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';
cb.file = loc;
if( sym ) free( sym );
if( demangled ) free( demangled );
return { &cb, 1 };
}
2019-05-22 12:05:44 +00:00
#elif TRACY_HAS_CALLSTACK == 2 || TRACY_HAS_CALLSTACK == 3
enum { MaxCbTrace = 16 };
struct backtrace_state* cb_bts;
int cb_num;
CallstackEntry cb_data[MaxCbTrace];
void InitCallstack()
{
cb_bts = backtrace_create_state( nullptr, 0, nullptr, nullptr );
}
static inline char* CopyString( const char* src )
2018-06-20 19:45:27 +00:00
{
const auto sz = strlen( src );
auto dst = (char*)tracy_malloc( sz + 1 );
memcpy( dst, src, sz );
dst[sz] = '\0';
return dst;
}
2018-06-20 19:45:27 +00:00
2019-03-05 17:56:39 +00:00
static int FastCallstackDataCb( void* data, uintptr_t pc, const char* fn, int lineno, const char* function )
{
2019-03-05 17:56:39 +00:00
if( function )
{
2019-03-05 19:20:55 +00:00
strcpy( (char*)data, function );
2019-03-05 17:56:39 +00:00
}
else
{
const char* symname = nullptr;
auto vptr = (void*)pc;
Dl_info dlinfo;
if( dladdr( vptr, &dlinfo ) )
{
symname = dlinfo.dli_sname;
}
if( symname )
{
strcpy( (char*)data, symname );
}
else
{
*(char*)data = '\0';
}
2019-03-05 17:56:39 +00:00
}
return 1;
}
2019-03-05 17:56:39 +00:00
static void FastCallstackErrorCb( void* data, const char* /*msg*/, int /*errnum*/ )
{
2019-03-05 19:20:55 +00:00
*(char*)data = '\0';
2019-03-05 17:56:39 +00:00
}
2019-03-05 17:56:39 +00:00
const char* DecodeCallstackPtrFast( uint64_t ptr )
{
static char ret[1024];
backtrace_pcinfo( cb_bts, ptr, FastCallstackDataCb, FastCallstackErrorCb, ret );
return ret;
}
2019-02-20 15:28:27 +00:00
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 )
2018-06-20 19:45:27 +00:00
{
const char* symname = nullptr;
const char* symloc = nullptr;
auto vptr = (void*)pc;
ptrdiff_t symoff = 0;
2018-06-20 21:01:00 +00:00
Dl_info dlinfo;
if( dladdr( vptr, &dlinfo ) )
2018-06-20 21:01:00 +00:00
{
symloc = dlinfo.dli_fname;
symname = dlinfo.dli_sname;
symoff = (char*)pc - (char*)dlinfo.dli_saddr;
if( symname && symname[0] == '_' )
2018-06-20 21:01:00 +00:00
{
size_t len = DemangleBufLen;
int status;
abi::__cxa_demangle( symname, demangled, &len, &status );
if( status == 0 )
{
symname = demangled;
}
2018-06-20 21:01:00 +00:00
}
}
if( !symname ) symname = "[unknown]";
if( !symloc ) symloc = "[unknown]";
if( symoff == 0 )
{
cb_data[cb_num].name = CopyString( symname );
}
else
{
char buf[32];
const auto offlen = sprintf( buf, " + %td", symoff );
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';
cb_data[cb_num].name = name;
}
char buf[32];
const auto addrlen = sprintf( buf, " [%p]", (void*)pc );
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';
cb_data[cb_num].file = loc;
cb_data[cb_num].line = 0;
}
else
{
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;
}
}
}
cb_data[cb_num].name = CopyString( function );
cb_data[cb_num].file = CopyString( fn );
cb_data[cb_num].line = lineno;
2018-06-20 19:45:27 +00:00
}
if( ++cb_num >= MaxCbTrace )
{
return 1;
}
else
{
return 0;
}
}
2019-02-20 15:28:27 +00:00
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 );
}
2018-06-20 19:45:27 +00:00
cb_data[0].name = CopyString( "[error]" );
cb_data[0].file = CopyString( "[error]" );
cb_data[0].line = 0;
2018-06-20 19:45:27 +00:00
cb_num = 1;
}
2018-06-20 20:18:12 +00:00
CallstackEntryData DecodeCallstackPtr( uint64_t ptr )
{
cb_num = 0;
backtrace_pcinfo( cb_bts, ptr, CallstackDataCb, CallstackErrorCb, nullptr );
assert( cb_num > 0 );
return { cb_data, uint8_t( cb_num ) };
2018-06-20 19:45:27 +00:00
}
#elif TRACY_HAS_CALLSTACK == 5
void InitCallstack()
{
}
const char* DecodeCallstackPtrFast( uint64_t ptr )
{
static char ret[1024];
auto vptr = (void*)ptr;
char** sym = nullptr;
const char* symname = nullptr;
Dl_info dlinfo;
if( dladdr( vptr, &dlinfo ) && dlinfo.dli_sname )
{
symname = dlinfo.dli_sname;
}
if( symname )
{
strcpy( ret, symname );
}
else
{
*ret = '\0';
}
return ret;
}
CallstackEntryData DecodeCallstackPtr( uint64_t ptr )
{
static CallstackEntry cb;
cb.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';
cb.name = name;
}
else
{
char buf[32];
const auto offlen = sprintf( buf, " + %td", symoff );
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';
cb.name = name;
}
char buf[32];
const auto addrlen = sprintf( buf, " [%p]", (void*)ptr );
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';
cb.file = loc;
if( sym ) free( sym );
if( demangled ) free( demangled );
return { &cb, 1 };
}
2018-06-20 19:45:27 +00:00
#endif
2018-06-18 23:17:19 +00:00
}
#endif