2018-06-30 14:00:51 +00:00
|
|
|
#include <stdio.h>
|
2019-11-05 19:09:40 +00:00
|
|
|
#include <string.h>
|
2018-06-18 23:17:19 +00:00
|
|
|
#include "TracyCallstack.hpp"
|
2020-02-24 20:54:19 +00:00
|
|
|
#include "TracyFastVector.hpp"
|
2018-06-18 23:17:19 +00:00
|
|
|
|
|
|
|
#ifdef TRACY_HAS_CALLSTACK
|
|
|
|
|
2018-07-14 09:08:17 +00:00
|
|
|
#if TRACY_HAS_CALLSTACK == 1
|
2019-10-10 18:29:06 +00:00
|
|
|
# ifndef NOMINMAX
|
|
|
|
# define NOMINMAX
|
|
|
|
# endif
|
2018-06-20 19:01:25 +00:00
|
|
|
# include <windows.h>
|
2020-02-22 20:06:32 +00:00
|
|
|
# include <psapi.h>
|
2018-12-28 23:42:38 +00:00
|
|
|
# ifdef _MSC_VER
|
|
|
|
# pragma warning( push )
|
|
|
|
# pragma warning( disable : 4091 )
|
|
|
|
# endif
|
2018-06-20 19:01:25 +00:00
|
|
|
# include <dbghelp.h>
|
2018-12-28 23:42:38 +00:00
|
|
|
# ifdef _MSC_VER
|
|
|
|
# pragma warning( pop )
|
|
|
|
# endif
|
2019-11-29 11:04:47 +00:00
|
|
|
#elif TRACY_HAS_CALLSTACK == 2 || TRACY_HAS_CALLSTACK == 3 || TRACY_HAS_CALLSTACK == 4 || TRACY_HAS_CALLSTACK == 6
|
2019-01-20 15:55:59 +00:00
|
|
|
# include "../libbacktrace/backtrace.hpp"
|
2018-06-20 20:54:42 +00:00
|
|
|
# include <dlfcn.h>
|
2018-06-20 21:01:00 +00:00
|
|
|
# include <cxxabi.h>
|
2019-11-29 11:04:47 +00:00
|
|
|
#elif TRACY_HAS_CALLSTACK == 5
|
2019-05-22 12:14:30 +00:00
|
|
|
# include <dlfcn.h>
|
|
|
|
# include <cxxabi.h>
|
2018-06-18 23:17:19 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
2020-02-25 23:02:30 +00:00
|
|
|
static inline char* CopyString( const char* src, size_t sz )
|
|
|
|
{
|
2020-02-26 21:27:10 +00:00
|
|
|
assert( strlen( src ) == sz );
|
2020-02-25 23:02:30 +00:00
|
|
|
auto dst = (char*)tracy_malloc( sz + 1 );
|
|
|
|
memcpy( dst, src, sz );
|
|
|
|
dst[sz] = '\0';
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline char* CopyString( const char* src )
|
|
|
|
{
|
|
|
|
return CopyString( src, strlen( src ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-14 09:08:17 +00:00
|
|
|
#if TRACY_HAS_CALLSTACK == 1
|
2018-06-20 19:45:27 +00:00
|
|
|
|
2019-01-26 22:50:58 +00:00
|
|
|
enum { MaxCbTrace = 16 };
|
2020-02-26 21:32:09 +00:00
|
|
|
enum { MaxNameSize = 8*1024 };
|
2019-01-26 22:50:58 +00:00
|
|
|
|
|
|
|
int cb_num;
|
|
|
|
CallstackEntry cb_data[MaxCbTrace];
|
|
|
|
|
2020-01-25 15:49:29 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
typedef unsigned long (__stdcall *t_RtlWalkFrameChain)( void**, unsigned long, unsigned long );
|
|
|
|
t_RtlWalkFrameChain RtlWalkFrameChain = 0;
|
|
|
|
}
|
2018-06-23 00:07:47 +00:00
|
|
|
|
2019-08-04 09:00:38 +00:00
|
|
|
#if defined __MINGW32__ && API_VERSION_NUMBER < 12
|
|
|
|
extern "C" {
|
|
|
|
// Actual required API_VERSION_NUMBER is unknown because it is undocumented. These functions are not present in at least v11.
|
|
|
|
DWORD IMAGEAPI SymAddrIncludeInlineTrace(HANDLE hProcess, DWORD64 Address);
|
|
|
|
BOOL IMAGEAPI SymQueryInlineTrace(HANDLE hProcess, DWORD64 StartAddress, DWORD StartContext, DWORD64 StartRetAddress,
|
|
|
|
DWORD64 CurAddress, LPDWORD CurContext, LPDWORD CurFrameIndex);
|
|
|
|
BOOL IMAGEAPI SymFromInlineContext(HANDLE hProcess, DWORD64 Address, ULONG InlineContext, PDWORD64 Displacement,
|
|
|
|
PSYMBOL_INFO Symbol);
|
|
|
|
BOOL IMAGEAPI SymGetLineFromInlineContext(HANDLE hProcess, DWORD64 qwAddr, ULONG InlineContext,
|
|
|
|
DWORD64 qwModuleBaseAddress, PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line64);
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2020-02-22 20:32:10 +00:00
|
|
|
struct ModuleCache
|
|
|
|
{
|
|
|
|
uint64_t start;
|
|
|
|
uint64_t end;
|
|
|
|
char* name;
|
|
|
|
};
|
|
|
|
|
2020-02-24 20:54:19 +00:00
|
|
|
static FastVector<ModuleCache>* s_modCache;
|
2020-02-22 20:32:10 +00:00
|
|
|
|
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
|
2018-06-23 00:07:47 +00:00
|
|
|
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 );
|
2020-02-22 20:32:18 +00:00
|
|
|
|
|
|
|
HMODULE mod[1024];
|
|
|
|
DWORD needed;
|
|
|
|
HANDLE proc = GetCurrentProcess();
|
|
|
|
|
|
|
|
#ifndef __CYGWIN__
|
2020-02-24 20:54:19 +00:00
|
|
|
s_modCache = new FastVector<ModuleCache>( 512 );
|
|
|
|
|
2020-02-22 20:32:18 +00:00
|
|
|
if( EnumProcessModules( proc, mod, sizeof( mod ), &needed ) != 0 )
|
|
|
|
{
|
|
|
|
const auto sz = needed / sizeof( HMODULE );
|
|
|
|
for( size_t i=0; i<sz; i++ )
|
|
|
|
{
|
|
|
|
MODULEINFO info;
|
|
|
|
if( GetModuleInformation( proc, mod[i], &info, sizeof( info ) ) != 0 )
|
|
|
|
{
|
|
|
|
const auto base = uint64_t( info.lpBaseOfDll );
|
|
|
|
char name[1024];
|
|
|
|
const auto res = GetModuleFileNameA( mod[i], name, 1021 );
|
|
|
|
if( res > 0 )
|
|
|
|
{
|
|
|
|
auto ptr = name + res;
|
|
|
|
while( ptr > name && *ptr != '\\' && *ptr != '/' ) ptr--;
|
|
|
|
if( ptr > name ) ptr++;
|
|
|
|
const auto namelen = name + res - ptr;
|
2020-02-24 20:54:19 +00:00
|
|
|
auto cache = s_modCache->push_next();
|
2020-02-22 20:32:18 +00:00
|
|
|
cache->start = base;
|
|
|
|
cache->end = base + info.SizeOfImage;
|
|
|
|
cache->name = (char*)tracy_malloc( namelen+3 );
|
|
|
|
cache->name[0] = '[';
|
|
|
|
memcpy( cache->name+1, ptr, namelen );
|
|
|
|
cache->name[namelen+1] = ']';
|
|
|
|
cache->name[namelen+2] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2018-06-18 23:17:19 +00:00
|
|
|
}
|
|
|
|
|
2020-01-25 15:49:29 +00:00
|
|
|
TRACY_API tracy_force_inline uintptr_t* CallTrace( int depth )
|
|
|
|
{
|
|
|
|
auto trace = (uintptr_t*)tracy_malloc( ( 1 + depth ) * sizeof( uintptr_t ) );
|
|
|
|
const auto num = RtlWalkFrameChain( (void**)( trace + 1 ), depth, 0 );
|
|
|
|
*trace = num;
|
|
|
|
return trace;
|
|
|
|
}
|
|
|
|
|
2019-03-05 01:34:35 +00:00
|
|
|
const char* DecodeCallstackPtrFast( uint64_t ptr )
|
|
|
|
{
|
2020-02-26 21:30:11 +00:00
|
|
|
static char ret[MaxNameSize];
|
2019-03-05 01:34:35 +00:00
|
|
|
const auto proc = GetCurrentProcess();
|
|
|
|
|
2020-02-26 21:30:11 +00:00
|
|
|
char buf[sizeof( SYMBOL_INFO ) + MaxNameSize];
|
2019-03-05 01:34:35 +00:00
|
|
|
auto si = (SYMBOL_INFO*)buf;
|
|
|
|
si->SizeOfStruct = sizeof( SYMBOL_INFO );
|
2020-02-26 21:30:11 +00:00
|
|
|
si->MaxNameLen = MaxNameSize;
|
2019-03-05 01:34:35 +00:00
|
|
|
|
|
|
|
if( SymFromAddr( proc, ptr, nullptr, si ) == 0 )
|
|
|
|
{
|
|
|
|
*ret = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy( ret, si->Name, si->NameLen );
|
|
|
|
ret[si->NameLen] = '\0';
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-02-27 12:24:36 +00:00
|
|
|
static const char* GetModuleName( uint64_t addr )
|
2020-02-22 20:06:32 +00:00
|
|
|
{
|
2020-02-27 12:24:36 +00:00
|
|
|
if( ( addr & 0x8000000000000000 ) != 0 ) return "[kernel]";
|
2020-02-24 22:04:10 +00:00
|
|
|
|
2020-02-22 20:06:32 +00:00
|
|
|
#ifndef __CYGWIN__
|
2020-02-24 20:54:19 +00:00
|
|
|
for( auto& v : *s_modCache )
|
2020-02-22 20:32:10 +00:00
|
|
|
{
|
2020-02-24 20:54:19 +00:00
|
|
|
if( addr >= v.start && addr < v.end )
|
2020-02-22 20:32:10 +00:00
|
|
|
{
|
2020-02-27 12:24:36 +00:00
|
|
|
return v.name;
|
2020-02-22 20:32:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-22 20:06:32 +00:00
|
|
|
HMODULE mod[1024];
|
|
|
|
DWORD needed;
|
|
|
|
HANDLE proc = GetCurrentProcess();
|
|
|
|
|
|
|
|
if( EnumProcessModules( proc, mod, sizeof( mod ), &needed ) != 0 )
|
|
|
|
{
|
|
|
|
const auto sz = needed / sizeof( HMODULE );
|
|
|
|
for( size_t i=0; i<sz; i++ )
|
|
|
|
{
|
|
|
|
MODULEINFO info;
|
|
|
|
if( GetModuleInformation( proc, mod[i], &info, sizeof( info ) ) != 0 )
|
|
|
|
{
|
|
|
|
const auto base = uint64_t( info.lpBaseOfDll );
|
|
|
|
if( addr >= base && addr < base + info.SizeOfImage )
|
|
|
|
{
|
|
|
|
char name[1024];
|
|
|
|
const auto res = GetModuleFileNameA( mod[i], name, 1021 );
|
|
|
|
if( res > 0 )
|
|
|
|
{
|
|
|
|
auto ptr = name + res;
|
|
|
|
while( ptr > name && *ptr != '\\' && *ptr != '/' ) ptr--;
|
|
|
|
if( ptr > name ) ptr++;
|
|
|
|
const auto namelen = name + res - ptr;
|
2020-02-24 20:54:19 +00:00
|
|
|
auto cache = s_modCache->push_next();
|
2020-02-22 20:32:10 +00:00
|
|
|
cache->start = base;
|
|
|
|
cache->end = base + info.SizeOfImage;
|
|
|
|
cache->name = (char*)tracy_malloc( namelen+3 );
|
2020-02-27 12:24:36 +00:00
|
|
|
cache->name[0] = '[';
|
|
|
|
memcpy( cache->name+1, ptr, namelen );
|
|
|
|
cache->name[namelen+1] = ']';
|
|
|
|
cache->name[namelen+2] = '\0';
|
|
|
|
return cache->name;
|
2020-02-22 20:06:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-02-27 12:24:36 +00:00
|
|
|
return "[unknown]";
|
2020-02-22 20:06:32 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 21:32:42 +00:00
|
|
|
SymbolData DecodeSymbolAddress( uint64_t ptr )
|
|
|
|
{
|
|
|
|
SymbolData sym;
|
|
|
|
IMAGEHLP_LINE64 line;
|
|
|
|
DWORD displacement = 0;
|
|
|
|
line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
|
2020-02-27 11:58:01 +00:00
|
|
|
if( SymGetLineFromAddr64( GetCurrentProcess(), ptr, &displacement, &line ) == 0 )
|
2020-02-26 21:32:42 +00:00
|
|
|
{
|
2020-02-27 12:17:26 +00:00
|
|
|
sym.file = "[unknown]";
|
2020-02-26 21:32:42 +00:00
|
|
|
sym.line = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-27 12:17:26 +00:00
|
|
|
sym.file = line.FileName;
|
2020-02-26 21:32:42 +00:00
|
|
|
sym.line = line.LineNumber;
|
|
|
|
}
|
2020-02-27 12:17:26 +00:00
|
|
|
sym.needFree = false;
|
2020-02-26 21:32:42 +00:00
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
|
2019-01-20 18:11:48 +00:00
|
|
|
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__
|
2019-11-05 19:09:40 +00:00
|
|
|
DWORD inlineNum = SymAddrIncludeInlineTrace( proc, ptr );
|
|
|
|
if( inlineNum > MaxCbTrace - 1 ) inlineNum = MaxCbTrace - 1;
|
2019-02-20 15:28:27 +00:00
|
|
|
DWORD ctx = 0;
|
|
|
|
DWORD idx;
|
2019-01-26 22:50:58 +00:00
|
|
|
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
|
2019-01-26 22:50:58 +00:00
|
|
|
{
|
|
|
|
write = 0;
|
|
|
|
cb_num = 1;
|
|
|
|
}
|
2018-06-18 23:17:19 +00:00
|
|
|
|
2020-02-26 21:30:11 +00:00
|
|
|
char buf[sizeof( SYMBOL_INFO ) + MaxNameSize];
|
2018-06-18 23:17:19 +00:00
|
|
|
auto si = (SYMBOL_INFO*)buf;
|
|
|
|
si->SizeOfStruct = sizeof( SYMBOL_INFO );
|
2020-02-26 21:30:11 +00:00
|
|
|
si->MaxNameLen = MaxNameSize;
|
2018-06-18 23:17:19 +00:00
|
|
|
|
2020-02-27 12:24:36 +00:00
|
|
|
const auto moduleName = GetModuleName( ptr );
|
2020-02-25 22:03:40 +00:00
|
|
|
const auto symValid = SymFromAddr( proc, ptr, nullptr, si ) != 0;
|
2018-06-18 23:17:19 +00:00
|
|
|
|
|
|
|
IMAGEHLP_LINE64 line;
|
2018-06-20 19:01:25 +00:00
|
|
|
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
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-02-27 12:24:36 +00:00
|
|
|
cb_data[write].name = symValid ? CopyString( si->Name, si->NameLen ) : CopyString( moduleName );
|
2020-02-25 23:02:30 +00:00
|
|
|
cb_data[write].file = CopyString( filename );
|
2020-03-25 17:05:00 +00:00
|
|
|
if( symValid )
|
|
|
|
{
|
|
|
|
cb_data[write].symLen = si->Size;
|
|
|
|
cb_data[write].symAddr = si->Address;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cb_data[write].symLen = 0;
|
|
|
|
cb_data[write].symAddr = 0;
|
|
|
|
}
|
2019-02-20 15:28:27 +00:00
|
|
|
}
|
2019-01-26 22:50:58 +00:00
|
|
|
|
2019-02-02 12:58:17 +00:00
|
|
|
#ifndef __CYGWIN__
|
2019-01-26 22:50:58 +00:00
|
|
|
if( doInline )
|
|
|
|
{
|
|
|
|
for( DWORD i=0; i<inlineNum; i++ )
|
|
|
|
{
|
|
|
|
auto& cb = cb_data[i];
|
2020-02-25 22:03:40 +00:00
|
|
|
const auto symInlineValid = SymFromInlineContext( proc, ptr, ctx, nullptr, si ) != 0;
|
2019-02-20 15:28:27 +00:00
|
|
|
const char* filename;
|
2019-01-26 22:50:58 +00:00
|
|
|
if( SymGetLineFromInlineContext( proc, ptr, ctx, 0, &displacement, &line ) == 0 )
|
|
|
|
{
|
|
|
|
filename = "[unknown]";
|
|
|
|
cb.line = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
filename = line.FileName;
|
|
|
|
cb.line = line.LineNumber;
|
|
|
|
}
|
|
|
|
|
2020-02-27 12:24:36 +00:00
|
|
|
cb.name = symInlineValid ? CopyString( si->Name, si->NameLen ) : CopyString( moduleName );
|
2020-02-25 23:02:30 +00:00
|
|
|
cb.file = CopyString( filename );
|
2020-03-25 17:05:00 +00:00
|
|
|
if( symInlineValid )
|
|
|
|
{
|
|
|
|
cb.symLen = si->Size;
|
|
|
|
cb.symAddr = si->Address;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cb.symLen = 0;
|
|
|
|
cb.symAddr = 0;
|
|
|
|
}
|
2020-02-25 22:03:40 +00:00
|
|
|
|
2019-01-26 22:50:58 +00:00
|
|
|
ctx++;
|
|
|
|
}
|
|
|
|
}
|
2019-02-02 12:58:17 +00:00
|
|
|
#endif
|
2018-06-19 23:26:05 +00:00
|
|
|
|
2020-02-27 12:45:39 +00:00
|
|
|
return { cb_data, uint8_t( cb_num ), moduleName };
|
2018-06-18 23:17:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-29 11:04:47 +00:00
|
|
|
#elif TRACY_HAS_CALLSTACK == 2 || TRACY_HAS_CALLSTACK == 3 || TRACY_HAS_CALLSTACK == 4 || TRACY_HAS_CALLSTACK == 6
|
2018-06-21 15:07:21 +00:00
|
|
|
|
2019-01-20 18:33:37 +00:00
|
|
|
enum { MaxCbTrace = 16 };
|
2018-06-21 15:07:21 +00:00
|
|
|
|
2019-01-20 15:55:59 +00:00
|
|
|
struct backtrace_state* cb_bts;
|
|
|
|
int cb_num;
|
|
|
|
CallstackEntry cb_data[MaxCbTrace];
|
2020-02-26 01:25:45 +00:00
|
|
|
int cb_fixup;
|
2018-06-21 15:07:21 +00:00
|
|
|
|
2019-01-20 15:55:59 +00:00
|
|
|
void InitCallstack()
|
|
|
|
{
|
|
|
|
cb_bts = backtrace_create_state( nullptr, 0, nullptr, nullptr );
|
2018-06-21 15:07:21 +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 01:34:35 +00:00
|
|
|
{
|
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
|
|
|
|
{
|
2019-03-05 19:43:47 +00:00
|
|
|
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 01:34:35 +00:00
|
|
|
|
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 01:34:35 +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 );
|
2019-03-05 01:34:35 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-02-26 22:38:04 +00:00
|
|
|
static int SymbolAddressDataCb( void* data, uintptr_t pc, const char* fn, int lineno, const char* function )
|
|
|
|
{
|
|
|
|
auto& sym = *(SymbolData*)data;
|
2020-02-27 11:58:01 +00:00
|
|
|
if( !fn )
|
2020-02-26 22:38:04 +00:00
|
|
|
{
|
|
|
|
const char* symloc = nullptr;
|
2020-02-27 12:28:45 +00:00
|
|
|
Dl_info dlinfo;
|
2020-02-27 13:08:56 +00:00
|
|
|
if( dladdr( (void*)pc, &dlinfo ) ) symloc = dlinfo.dli_fname;
|
2020-02-26 22:38:04 +00:00
|
|
|
if( !symloc ) symloc = "[unknown]";
|
2020-02-27 12:17:26 +00:00
|
|
|
sym.file = symloc;
|
2020-02-26 22:38:04 +00:00
|
|
|
sym.line = 0;
|
2020-02-27 12:17:26 +00:00
|
|
|
sym.needFree = false;
|
2020-02-26 22:38:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sym.file = CopyString( fn );
|
|
|
|
sym.line = lineno;
|
2020-02-27 12:17:26 +00:00
|
|
|
sym.needFree = true;
|
2020-02-26 22:38:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SymbolAddressErrorCb( void* data, const char* /*msg*/, int /*errnum*/ )
|
|
|
|
{
|
|
|
|
memset( data, 0, sizeof( SymbolData ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
SymbolData DecodeSymbolAddress( uint64_t ptr )
|
|
|
|
{
|
|
|
|
SymbolData sym;
|
|
|
|
backtrace_pcinfo( cb_bts, ptr, SymbolAddressDataCb, SymbolAddressErrorCb, &sym );
|
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
|
2019-02-20 15:28:27 +00:00
|
|
|
static int CallstackDataCb( void* /*data*/, uintptr_t pc, const char* fn, int lineno, const char* function )
|
2019-01-20 15:55:59 +00:00
|
|
|
{
|
|
|
|
enum { DemangleBufLen = 64*1024 };
|
|
|
|
char demangled[DemangleBufLen];
|
2018-06-20 20:54:42 +00:00
|
|
|
|
2020-03-25 17:05:00 +00:00
|
|
|
cb_data[cb_num].symLen = 0;
|
2020-02-26 01:25:45 +00:00
|
|
|
cb_data[cb_num].symAddr = (uint64_t)pc;
|
|
|
|
|
2019-01-20 15:55:59 +00:00
|
|
|
if( !fn && !function )
|
2018-06-20 19:45:27 +00:00
|
|
|
{
|
2019-01-20 15:55:59 +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
|
|
|
|
2019-01-20 15:55:59 +00:00
|
|
|
Dl_info dlinfo;
|
|
|
|
if( dladdr( vptr, &dlinfo ) )
|
2018-06-20 21:01:00 +00:00
|
|
|
{
|
2019-01-20 15:55:59 +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
|
|
|
{
|
2019-01-20 15:55:59 +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
|
|
|
}
|
|
|
|
}
|
2018-06-20 20:54:42 +00:00
|
|
|
|
2019-01-20 15:55:59 +00:00
|
|
|
if( !symname ) symname = "[unknown]";
|
|
|
|
if( !symloc ) symloc = "[unknown]";
|
|
|
|
|
|
|
|
if( symoff == 0 )
|
2018-06-20 20:54:42 +00:00
|
|
|
{
|
2019-01-20 15:55:59 +00:00
|
|
|
cb_data[cb_num].name = CopyString( symname );
|
2018-06-20 20:54:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-20 15:55:59 +00:00
|
|
|
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;
|
2018-06-20 20:54:42 +00:00
|
|
|
}
|
2019-01-20 15:55:59 +00:00
|
|
|
|
|
|
|
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;
|
2018-06-20 20:54:42 +00:00
|
|
|
}
|
2019-01-20 15:55:59 +00:00
|
|
|
else
|
2018-06-20 20:54:42 +00:00
|
|
|
{
|
2019-01-20 15:55:59 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-01-20 15:55:59 +00:00
|
|
|
if( ++cb_num >= MaxCbTrace )
|
2018-06-21 11:10:48 +00:00
|
|
|
{
|
2019-01-20 15:55:59 +00:00
|
|
|
return 1;
|
2018-06-21 11:10:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-20 15:55:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-20 15:28:27 +00:00
|
|
|
static void CallstackErrorCb( void* /*data*/, const char* /*msg*/, int /*errnum*/ )
|
2019-01-20 15:55:59 +00:00
|
|
|
{
|
|
|
|
for( int i=0; i<cb_num; i++ )
|
|
|
|
{
|
|
|
|
tracy_free( (void*)cb_data[i].name );
|
|
|
|
tracy_free( (void*)cb_data[i].file );
|
2018-06-21 11:10:48 +00:00
|
|
|
}
|
2018-06-20 19:45:27 +00:00
|
|
|
|
2019-01-20 15:55:59 +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
|
|
|
|
2019-01-20 15:55:59 +00:00
|
|
|
cb_num = 1;
|
|
|
|
}
|
2018-06-20 20:18:12 +00:00
|
|
|
|
2020-02-26 01:25:45 +00:00
|
|
|
void SymInfoCallback( void* /*data*/, uintptr_t pc, const char* symname, uintptr_t symval, uintptr_t symsize )
|
|
|
|
{
|
2020-03-25 17:05:00 +00:00
|
|
|
cb_data[cb_fixup].symLen = (uint32_t)symsize;
|
2020-02-26 01:25:45 +00:00
|
|
|
cb_data[cb_fixup].symAddr = (uint64_t)symval;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SymInfoError( void* /*data*/, const char* /*msg*/, int /*errnum*/ )
|
|
|
|
{
|
2020-03-25 17:05:00 +00:00
|
|
|
cb_data[cb_fixup].symLen = 0;
|
2020-02-26 01:25:45 +00:00
|
|
|
cb_data[cb_fixup].symAddr = 0;
|
|
|
|
}
|
|
|
|
|
2019-01-20 18:11:48 +00:00
|
|
|
CallstackEntryData DecodeCallstackPtr( uint64_t ptr )
|
2019-01-20 15:55:59 +00:00
|
|
|
{
|
|
|
|
cb_num = 0;
|
|
|
|
backtrace_pcinfo( cb_bts, ptr, CallstackDataCb, CallstackErrorCb, nullptr );
|
2019-01-20 18:33:37 +00:00
|
|
|
assert( cb_num > 0 );
|
2020-02-26 01:11:51 +00:00
|
|
|
|
2020-02-26 01:25:45 +00:00
|
|
|
for( int i=0; i<cb_num; i++ )
|
|
|
|
{
|
|
|
|
cb_fixup = i;
|
|
|
|
backtrace_syminfo( cb_bts, cb_data[i].symAddr, SymInfoCallback, SymInfoError, nullptr );
|
|
|
|
}
|
|
|
|
|
2020-02-26 01:11:51 +00:00
|
|
|
const char* symloc = nullptr;
|
|
|
|
Dl_info dlinfo;
|
|
|
|
if( dladdr( (void*)ptr, &dlinfo ) ) symloc = dlinfo.dli_fname;
|
|
|
|
|
2020-02-27 12:45:39 +00:00
|
|
|
return { cb_data, uint8_t( cb_num ), symloc ? symloc : "[unknown]" };
|
2018-06-20 19:45:27 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 12:14:30 +00:00
|
|
|
#elif TRACY_HAS_CALLSTACK == 5
|
|
|
|
|
|
|
|
void InitCallstack()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* DecodeCallstackPtrFast( uint64_t ptr )
|
|
|
|
{
|
|
|
|
static char ret[1024];
|
|
|
|
auto vptr = (void*)ptr;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-02-26 22:24:18 +00:00
|
|
|
SymbolData DecodeSymbolAddress( uint64_t ptr )
|
|
|
|
{
|
|
|
|
const char* symloc = nullptr;
|
2020-02-27 12:28:45 +00:00
|
|
|
Dl_info dlinfo;
|
2020-02-27 11:58:01 +00:00
|
|
|
if( dladdr( (void*)ptr, &dlinfo ) ) symloc = dlinfo.dli_fname;
|
|
|
|
if( !symloc ) symloc = "[unknown]";
|
2020-02-27 12:17:26 +00:00
|
|
|
return SymbolData { symloc, 0, false };
|
2020-02-26 22:24:18 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 12:14:30 +00:00
|
|
|
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;
|
|
|
|
ptrdiff_t symoff = 0;
|
2020-02-26 01:04:29 +00:00
|
|
|
void* symaddr = nullptr;
|
2019-05-22 12:14:30 +00:00
|
|
|
|
|
|
|
Dl_info dlinfo;
|
|
|
|
if( dladdr( vptr, &dlinfo ) )
|
|
|
|
{
|
|
|
|
symloc = dlinfo.dli_fname;
|
|
|
|
symname = dlinfo.dli_sname;
|
|
|
|
symoff = (char*)ptr - (char*)dlinfo.dli_saddr;
|
2020-02-26 01:04:29 +00:00
|
|
|
symaddr = dlinfo.dli_saddr;
|
2019-05-22 12:14:30 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-27 12:40:41 +00:00
|
|
|
if( !symname ) symname = "[unknown]";
|
|
|
|
if( !symloc ) symloc = "[unknown]";
|
2019-05-22 12:14:30 +00:00
|
|
|
|
|
|
|
if( symoff == 0 )
|
|
|
|
{
|
2020-02-25 23:02:30 +00:00
|
|
|
cb.name = CopyString( symname );
|
2019-05-22 12:14:30 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
2020-03-25 17:05:00 +00:00
|
|
|
cb.symLen = 0;
|
2020-02-26 01:04:29 +00:00
|
|
|
cb.symAddr = (uint64_t)symaddr;
|
|
|
|
|
2019-05-22 12:14:30 +00:00
|
|
|
if( demangled ) free( demangled );
|
|
|
|
|
2020-02-27 12:45:39 +00:00
|
|
|
return { &cb, 1, symloc };
|
2019-05-22 12:14:30 +00:00
|
|
|
}
|
|
|
|
|
2018-06-20 19:45:27 +00:00
|
|
|
#endif
|
|
|
|
|
2018-06-18 23:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|