2024-01-04 00:37:56 +01:00
|
|
|
#include <stacktrace.hpp>
|
2023-06-20 11:55:56 +02:00
|
|
|
#include <hex/helpers/fmt.hpp>
|
2023-01-17 21:38:56 +01:00
|
|
|
|
2023-07-22 22:47:35 +02:00
|
|
|
#include <array>
|
|
|
|
|
2023-01-17 21:38:56 +01:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <dbghelp.h>
|
|
|
|
#include <llvm/Demangle/Demangle.h>
|
|
|
|
|
|
|
|
namespace hex::stacktrace {
|
|
|
|
|
|
|
|
void initialize() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<StackFrame> getStackTrace() {
|
|
|
|
std::vector<StackFrame> stackTrace;
|
|
|
|
|
|
|
|
HANDLE process = GetCurrentProcess();
|
|
|
|
HANDLE thread = GetCurrentThread();
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
CONTEXT context = {};
|
2023-01-17 21:38:56 +01:00
|
|
|
context.ContextFlags = CONTEXT_FULL;
|
|
|
|
RtlCaptureContext(&context);
|
|
|
|
|
2023-06-20 11:55:56 +02:00
|
|
|
SymSetOptions(SYMOPT_CASE_INSENSITIVE | SYMOPT_UNDNAME | SYMOPT_LOAD_LINES | SYMOPT_LOAD_ANYTHING);
|
|
|
|
SymInitialize(process, nullptr, TRUE);
|
2023-01-17 21:38:56 +01:00
|
|
|
|
|
|
|
DWORD image;
|
|
|
|
STACKFRAME64 stackFrame;
|
|
|
|
ZeroMemory(&stackFrame, sizeof(STACKFRAME64));
|
|
|
|
|
|
|
|
image = IMAGE_FILE_MACHINE_AMD64;
|
|
|
|
stackFrame.AddrPC.Offset = context.Rip;
|
|
|
|
stackFrame.AddrPC.Mode = AddrModeFlat;
|
|
|
|
stackFrame.AddrFrame.Offset = context.Rsp;
|
|
|
|
stackFrame.AddrFrame.Mode = AddrModeFlat;
|
|
|
|
stackFrame.AddrStack.Offset = context.Rsp;
|
|
|
|
stackFrame.AddrStack.Mode = AddrModeFlat;
|
|
|
|
|
|
|
|
while (true) {
|
2023-06-22 15:15:06 +02:00
|
|
|
if (StackWalk64(
|
2023-01-17 21:38:56 +01:00
|
|
|
image, process, thread,
|
|
|
|
&stackFrame, &context, nullptr,
|
2023-06-22 15:15:06 +02:00
|
|
|
SymFunctionTableAccess64, SymGetModuleBase64, nullptr) == FALSE)
|
2023-01-17 21:38:56 +01:00
|
|
|
break;
|
|
|
|
|
2023-06-20 11:55:56 +02:00
|
|
|
if (stackFrame.AddrReturn.Offset == stackFrame.AddrPC.Offset)
|
|
|
|
break;
|
|
|
|
|
2023-06-22 15:15:06 +02:00
|
|
|
std::array<char, sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)> buffer = {};
|
2023-11-10 20:47:08 +01:00
|
|
|
auto symbol = reinterpret_cast<PSYMBOL_INFO>(buffer.data());
|
2023-01-17 21:38:56 +01:00
|
|
|
symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
|
|
|
|
symbol->MaxNameLen = MAX_SYM_NAME;
|
|
|
|
|
|
|
|
DWORD64 displacementSymbol = 0;
|
2023-06-22 15:15:06 +02:00
|
|
|
const char *symbolName;
|
2023-06-20 11:55:56 +02:00
|
|
|
if (SymFromAddr(process, stackFrame.AddrPC.Offset, &displacementSymbol, symbol) == TRUE) {
|
2023-06-22 15:15:06 +02:00
|
|
|
symbolName = symbol->Name;
|
2023-01-17 21:38:56 +01:00
|
|
|
} else {
|
|
|
|
symbolName = "??";
|
|
|
|
}
|
|
|
|
|
2023-06-20 11:55:56 +02:00
|
|
|
SymSetOptions(SYMOPT_LOAD_LINES);
|
|
|
|
|
2023-01-17 21:38:56 +01:00
|
|
|
IMAGEHLP_LINE64 line;
|
|
|
|
line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
|
|
|
|
|
|
|
|
DWORD displacementLine = 0;
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
u32 lineNumber = 0;
|
2023-06-22 15:15:06 +02:00
|
|
|
const char *fileName;
|
2023-06-20 11:55:56 +02:00
|
|
|
if (SymGetLineFromAddr64(process, stackFrame.AddrPC.Offset, &displacementLine, &line) == TRUE) {
|
2023-01-17 21:38:56 +01:00
|
|
|
lineNumber = line.LineNumber;
|
|
|
|
fileName = line.FileName;
|
|
|
|
} else {
|
|
|
|
lineNumber = 0;
|
|
|
|
fileName = "??";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string demangledName;
|
|
|
|
if (auto variant1 = llvm::demangle(symbolName); variant1 != symbolName)
|
|
|
|
demangledName = variant1;
|
|
|
|
else if (auto variant2 = llvm::demangle(std::string("_") + symbolName); variant2 != std::string("_") + symbolName)
|
|
|
|
demangledName = variant2;
|
|
|
|
else
|
|
|
|
demangledName = symbolName;
|
|
|
|
|
|
|
|
stackTrace.push_back(StackFrame { fileName, demangledName, lineNumber });
|
|
|
|
}
|
|
|
|
|
|
|
|
SymCleanup(process);
|
|
|
|
|
|
|
|
return stackTrace;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-02-13 08:27:08 +01:00
|
|
|
#elif defined(HEX_HAS_EXECINFO)
|
2023-01-17 21:38:56 +01:00
|
|
|
|
2023-02-13 08:27:08 +01:00
|
|
|
#if __has_include(BACKTRACE_HEADER)
|
2023-01-17 21:38:56 +01:00
|
|
|
|
2023-02-13 08:27:08 +01:00
|
|
|
#include BACKTRACE_HEADER
|
|
|
|
#include <llvm/Demangle/Demangle.h>
|
|
|
|
#include <hex/helpers/logger.hpp>
|
|
|
|
#include <hex/helpers/utils.hpp>
|
2023-01-17 21:38:56 +01:00
|
|
|
|
2023-02-13 08:27:08 +01:00
|
|
|
namespace hex::stacktrace {
|
2023-01-17 21:38:56 +01:00
|
|
|
|
2023-02-13 08:27:08 +01:00
|
|
|
void initialize() {
|
2023-01-17 21:38:56 +01:00
|
|
|
|
2023-02-13 08:27:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<StackFrame> getStackTrace() {
|
|
|
|
static std::vector<StackFrame> result;
|
2023-01-17 21:38:56 +01:00
|
|
|
|
2023-02-13 08:27:08 +01:00
|
|
|
std::array<void*, 128> addresses;
|
2023-07-11 09:21:13 +02:00
|
|
|
size_t count = backtrace(addresses.data(), addresses.size());
|
2023-02-13 08:27:08 +01:00
|
|
|
auto functions = backtrace_symbols(addresses.data(), count);
|
2023-01-17 21:38:56 +01:00
|
|
|
|
2023-07-11 09:21:13 +02:00
|
|
|
for (size_t i = 0; i < count; i++)
|
2023-02-13 08:27:08 +01:00
|
|
|
result.push_back(StackFrame { "", functions[i], 0 });
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2023-01-17 21:38:56 +01:00
|
|
|
|
|
|
|
}
|
2023-02-13 08:27:08 +01:00
|
|
|
#endif
|
2023-01-17 21:38:56 +01:00
|
|
|
|
2023-02-13 08:27:08 +01:00
|
|
|
#elif defined(HEX_HAS_BACKTRACE)
|
2023-01-17 21:38:56 +01:00
|
|
|
|
2023-02-13 08:27:08 +01:00
|
|
|
#if __has_include(BACKTRACE_HEADER)
|
2023-01-17 21:38:56 +01:00
|
|
|
|
2023-02-13 08:27:08 +01:00
|
|
|
#include BACKTRACE_HEADER
|
|
|
|
#include <llvm/Demangle/Demangle.h>
|
|
|
|
#include <hex/helpers/logger.hpp>
|
|
|
|
#include <hex/helpers/utils.hpp>
|
2023-01-17 21:38:56 +01:00
|
|
|
|
2023-02-13 08:27:08 +01:00
|
|
|
namespace hex::stacktrace {
|
2023-01-17 21:38:56 +01:00
|
|
|
|
2023-02-13 08:27:08 +01:00
|
|
|
static struct backtrace_state *s_backtraceState;
|
2023-01-17 21:38:56 +01:00
|
|
|
|
|
|
|
|
2023-02-13 08:27:08 +01:00
|
|
|
void initialize() {
|
2023-03-13 09:25:07 +01:00
|
|
|
if (auto executablePath = wolv::io::fs::getExecutablePath(); executablePath.has_value()) {
|
2023-02-13 08:27:08 +01:00
|
|
|
static std::string path = executablePath->string();
|
|
|
|
s_backtraceState = backtrace_create_state(path.c_str(), 1, [](void *, const char *msg, int) { log::error("{}", msg); }, nullptr);
|
|
|
|
}
|
2023-01-17 21:38:56 +01:00
|
|
|
}
|
|
|
|
|
2023-02-13 08:27:08 +01:00
|
|
|
std::vector<StackFrame> getStackTrace() {
|
|
|
|
static std::vector<StackFrame> result;
|
|
|
|
|
|
|
|
result.clear();
|
|
|
|
if (s_backtraceState != nullptr) {
|
|
|
|
backtrace_full(s_backtraceState, 0, [](void *, uintptr_t, const char *fileName, int lineNumber, const char *function) -> int {
|
|
|
|
if (fileName == nullptr)
|
|
|
|
fileName = "??";
|
|
|
|
if (function == nullptr)
|
|
|
|
function = "??";
|
2023-01-17 21:38:56 +01:00
|
|
|
|
2023-02-13 08:27:08 +01:00
|
|
|
result.push_back(StackFrame { std::fs::path(fileName).filename().string(), llvm::demangle(function), u32(lineNumber) });
|
2023-01-17 21:38:56 +01:00
|
|
|
|
2023-02-13 08:27:08 +01:00
|
|
|
return 0;
|
|
|
|
}, nullptr, nullptr);
|
2023-01-17 21:38:56 +01:00
|
|
|
|
2023-02-13 08:27:08 +01:00
|
|
|
}
|
2023-01-17 21:38:56 +01:00
|
|
|
|
2023-02-13 08:27:08 +01:00
|
|
|
return result;
|
2023-01-17 21:38:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-02-13 08:27:08 +01:00
|
|
|
#endif
|
2023-01-17 21:38:56 +01:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
namespace hex::stacktrace {
|
|
|
|
|
|
|
|
void initialize() { }
|
2023-07-20 21:41:22 +02:00
|
|
|
std::vector<StackFrame> getStackTrace() { return { StackFrame { "??", "Stacktrace collecting not available!", 0 } }; }
|
2023-01-17 21:38:56 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|