Skip to content

Commit

Permalink
Adding initial support for obtaining stack trace.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Kenyon committed Sep 8, 2016
1 parent 6b9ee03 commit 07097d7
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 10 deletions.
6 changes: 5 additions & 1 deletion cmake/defaults/CXXDefaults.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ elseif(MSVC)
include(msvcdefaults)
endif()

_add_define(BUILD_COMPONENT_SRC_PREFIX="pxr/")
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
_add_define(BUILD_COMPONENT_SRC_PREFIX="pxr/")
else()
_add_define(BUILD_COMPONENT_SRC_PREFIX="")
endif()

_add_define(GL_GLEXT_PROTOTYPES)
_add_define(GLX_GLXEXT_PROTOTYPES)
Expand Down
28 changes: 21 additions & 7 deletions pxr/base/lib/arch/stackTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <ciso646>
#include <process.h>
#include <Winsock2.h>
#include <DbgHelp.h>
#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 64
#endif
Expand Down Expand Up @@ -59,6 +60,7 @@
#include <cstdio>
#include <cstring>
#include <mutex>
#include <algorithm>

/* Darwin/ppc did not do stack traces. Darwin/i386 still
needs some work, this has been stubbed out for now. */
Expand Down Expand Up @@ -1231,6 +1233,21 @@ ArchGetStackFrames(size_t maxdepth, size_t skip, vector<uintptr_t> *frames)
_Unwind_Backtrace(Arch_unwindcb, (void*)&context);
}

#elif defined(ARCH_OS_WINDOWS)

void
ArchGetStackFrames(size_t maxdepth, size_t skip, vector<uintptr_t> *frames)
{
void* stack[MAX_STACK_DEPTH];
size_t frameCount = CaptureStackBackTrace(0, MAX_STACK_DEPTH, stack, NULL);
frameCount = std::min(frameCount, maxdepth);
for (size_t frame = skip; frame < frameCount; ++frame)
{
DWORD64 address = (DWORD64)(stack[frame]);
frames->push_back(address);
}
}

#else

void
Expand Down Expand Up @@ -1312,13 +1329,7 @@ Arch_GetStackTrace(const vector<uintptr_t> &frames)
{
vector<string> rv;

#if !defined(ARCH_OS_LINUX)

rv.push_back("No frames saved, stack traces not supported on this "
"architecture.");
return rv;

#else
#if defined(ARCH_OS_LINUX) || defined(ARCH_OS_WINDOWS)

if (frames.empty()) {
rv.push_back("No frames saved, stack traces probably not supported.");
Expand All @@ -1334,7 +1345,10 @@ Arch_GetStackTrace(const vector<uintptr_t> &frames)
rv.push_back(ArchStringPrintf(" #%-3i 0x%016lx in %s",
(int)i, frames[i], symbolic.c_str()));
}
#else

rv.push_back("No frames saved, stack traces not supported on this "
"architecture.");
#endif

return rv;
Expand Down
50 changes: 48 additions & 2 deletions pxr/base/lib/arch/symbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <dlfcn.h>
#elif defined(ARCH_OS_WINDOWS)
#include <Windows.h>
#include <DbgHelp.h>
#include <Psapi.h>
#endif

bool
Expand Down Expand Up @@ -68,12 +70,56 @@ ArchGetAddressInfo(
if(GetModuleFileName(module, modName, ARCH_PATH_MAX))
{
objectPath->assign(modName);
return true;
}
}
else
ArchStrSysError(::GetLastError());
}

if (baseAddress || symbolName || symbolAddress)
{
DWORD displacement;
HANDLE process = GetCurrentProcess();
SymInitialize(process, NULL, TRUE);

// Symbol
ULONG64 symBuffer[(sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR) +
sizeof(ULONG64) - 1) / sizeof(ULONG64)];
SYMBOL_INFO *symbol = (SYMBOL_INFO*)symBuffer;
symbol->MaxNameLen = MAX_SYM_NAME;
symbol->SizeOfStruct = sizeof(SYMBOL_INFO);

// Line
IMAGEHLP_LINE64 line = {0};
line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);

DWORD64 dwAddress = (DWORD64)address;
SymFromAddr(process, dwAddress, NULL, symbol);
if (SymGetLineFromAddr64(process, dwAddress, &displacement, &line))
{
if (baseAddress)
{
MODULEINFO moduleInfo = {0};
if (GetModuleInformation(process, module, &moduleInfo,
sizeof(moduleInfo)))
{
*baseAddress = moduleInfo.lpBaseOfDll;
}
}

if (symbolName)
{
*symbolName = symbol->Name ? symbol->Name : "";
}

if (symbolAddress)
{
*symbolAddress = (void*)symbol->Address;
}
}
else
return false;
}
#endif
return false;
return true;
}

0 comments on commit 07097d7

Please sign in to comment.