18 #define WIN32_LEAN_AND_MEAN 35 const unsigned int framesToSkip =
37 const unsigned int framesToCapture = 64;
40 void* backTrace[framesToCapture]{};
42 SymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS);
43 const HANDLE hProcess = GetCurrentProcess();
45 hProcess,
nullptr , TRUE ))
48 <<
"[mrpt::system::getCallStackBackTrace] Error in SymInitialize()!" 53 char buffer[
sizeof(SYMBOL_INFO) + MAX_SYM_NAME *
sizeof(TCHAR)];
54 PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;
55 pSymbol->SizeOfStruct =
sizeof(SYMBOL_INFO);
56 pSymbol->MaxNameLen = MAX_SYM_NAME;
58 const USHORT nFrames = CaptureStackBackTrace(
59 framesToSkip, framesToCapture, backTrace,
nullptr);
60 for (
unsigned int i = 0; i < nFrames; i++)
65 if (!SymFromAddr(hProcess, (DWORD64)cse.
address,
nullptr, pSymbol))
72 SYMBOL_INFO& si = *pSymbol;
75 char undecorated_name[1024];
76 if (!UnDecorateSymbolName(
77 si.Name, undecorated_name,
sizeof(undecorated_name),
84 cse.
symbolName = std::string(undecorated_name);
91 void* callstack[framesToCapture];
92 const int nMaxFrames =
sizeof(callstack) /
sizeof(callstack[0]);
93 int nFrames = ::backtrace(callstack, nMaxFrames);
94 char** symbols = ::backtrace_symbols(callstack, nFrames);
96 for (
int i = (
int)framesToSkip; i < nFrames; i++)
102 if (dladdr(callstack[i], &info) && info.dli_sname)
104 char* demangled =
nullptr;
106 if (info.dli_sname[0] ==
'_')
108 demangled = abi::__cxa_demangle(
109 info.dli_sname,
nullptr,
nullptr, &status);
112 info.dli_sname ==
nullptr ? symbols[i] : info.dli_sname;
126 std::ostringstream trace_buf;
127 trace_buf <<
"Callstack backtrace:" << std::endl;
131 "[%-2d] %*p %s", static_cast<int>(i),
132 int(2 +
sizeof(
void*) * 2),
137 return trace_buf.str();
Used in getCallStackBackTrace()
std::string std::string format(std::string_view fmt, ARGS &&... args)
void getCallStackBackTrace(TCallStackBackTrace &out_bt)
Returns a list of strings representing the current call stack backtrace.
void * address
Address of the calling point.
std::string symbolName
Demangled symbol name.
std::vector< TCallStackEntry > backtrace_levels
std::string symbolNameOriginal
Original (before demangle) symbol name.
See: getCallStackBackTrace()
std::string asString() const
Prints all backtrace entries, one per line in a human-readable format.