14 #ifdef MRPT_OS_WINDOWS 15 #define WIN32_LEAN_AND_MEAN 31 const unsigned int framesToSkip = 1;
32 const unsigned int framesToCapture = 64;
34 #ifdef MRPT_OS_WINDOWS 35 void* backTrace[framesToCapture]{};
37 SymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS);
38 const HANDLE hProcess = GetCurrentProcess();
39 if (!SymInitialize(hProcess,
nullptr ,
TRUE ))
41 std::cerr <<
"[mrpt::system::getCallStackBackTrace] Error in SymInitialize()!" << std::endl;
45 char buffer[
sizeof(SYMBOL_INFO) + MAX_SYM_NAME *
sizeof(TCHAR)];
46 PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)
buffer;
47 pSymbol->SizeOfStruct =
sizeof(SYMBOL_INFO);
48 pSymbol->MaxNameLen = MAX_SYM_NAME;
50 const USHORT nFrames = CaptureStackBackTrace(framesToSkip, framesToCapture, backTrace,
nullptr);
51 for (
unsigned int i = 0; i < nFrames; i++)
56 if (!SymFromAddr(hProcess, (DWORD64)cse.
address,
nullptr, pSymbol))
63 SYMBOL_INFO &si = *pSymbol;
66 char undecorated_name[1024];
67 if (!UnDecorateSymbolName(si.Name, undecorated_name,
sizeof(undecorated_name), UNDNAME_COMPLETE))
80 void *callstack[framesToCapture];
81 const int nMaxFrames =
sizeof(callstack) /
sizeof(callstack[0]);
82 int nFrames = ::backtrace(callstack, nMaxFrames);
83 char **symbols = ::backtrace_symbols(callstack, nFrames);
85 for (
int i = (
int)framesToSkip; i < nFrames; i++)
91 if (dladdr(callstack[i], &
info) &&
info.dli_sname)
93 char *demangled = NULL;
95 if (
info.dli_sname[0] ==
'_')
97 demangled = abi::__cxa_demangle(
info.dli_sname, NULL, 0, &
status);
115 std::ostringstream trace_buf;
116 trace_buf <<
"Callstack backtrace:" << std::endl;
117 for (
unsigned int i = 0; i < this->backtrace_levels.size(); i++)
119 trace_buf <<
mrpt::format(
"[%-2d] %*p %s", i,
int(2 +
sizeof(
void*) * 2), backtrace_levels[i].
address, backtrace_levels[i].symbolName.c_str()) << std::endl;
121 return trace_buf.str();
Used in getCallStackBackTrace()
void BASE_IMPEXP 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 BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
GLsizei const GLchar ** string
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.