18 #define WIN32_LEAN_AND_MEAN
34 const unsigned int framesToSkip =
36 const unsigned int framesToCapture = 64;
39 void* backTrace[framesToCapture]{};
41 SymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS);
42 const HANDLE hProcess = GetCurrentProcess();
44 hProcess,
nullptr ,
TRUE ))
47 <<
"[mrpt::system::getCallStackBackTrace] Error in SymInitialize()!"
52 char buffer[
sizeof(SYMBOL_INFO) + MAX_SYM_NAME *
sizeof(TCHAR)];
53 PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)
buffer;
54 pSymbol->SizeOfStruct =
sizeof(SYMBOL_INFO);
55 pSymbol->MaxNameLen = MAX_SYM_NAME;
57 const USHORT nFrames = CaptureStackBackTrace(
58 framesToSkip, framesToCapture, backTrace,
nullptr);
59 for (
unsigned int i = 0; i < nFrames; i++)
64 if (!SymFromAddr(hProcess, (DWORD64)cse.
address,
nullptr, pSymbol))
71 SYMBOL_INFO& si = *pSymbol;
74 char undecorated_name[1024];
75 if (!UnDecorateSymbolName(
76 si.Name, undecorated_name,
sizeof(undecorated_name),
90 void* callstack[framesToCapture];
91 const int nMaxFrames =
sizeof(callstack) /
sizeof(callstack[0]);
92 int nFrames = ::backtrace(callstack, nMaxFrames);
93 char** symbols = ::backtrace_symbols(callstack, nFrames);
95 for (
int i = (
int)framesToSkip; i < nFrames; i++)
101 if (dladdr(callstack[i], &info) && info.dli_sname)
103 char* demangled = NULL;
105 if (info.dli_sname[0] ==
'_')
108 abi::__cxa_demangle(info.dli_sname, NULL, 0, &
status);
111 info.dli_sname == 0 ? symbols[i] : info.dli_sname;
125 std::ostringstream trace_buf;
126 trace_buf <<
"Callstack backtrace:" << std::endl;
127 for (
unsigned int i = 0; i < this->backtrace_levels.size(); i++)
130 "[%-2d] %*p %s", i,
int(2 +
sizeof(
void*) * 2),
132 backtrace_levels[i].symbolName.c_str())
135 return trace_buf.str();