App: add a CMake test to check if C runtime provides backtrace symbols

This commit is contained in:
wmayer
2021-11-09 14:16:12 +01:00
parent 5ad8bed9c4
commit 84182deff0
3 changed files with 32 additions and 0 deletions

View File

@@ -63,3 +63,23 @@ SET(HAVE_QT_KEYPAD_DEFINE 1)
SET(HAVE_QWIDGET_SHOWFULLSCREEN 1)
file(WRITE ${CMAKE_BINARY_DIR}/backtrace.cpp
"#include <cstddef>\n"
"#include <execinfo.h>\n\n"
"int main() {\n"
" void *callstack[128];\n"
" size_t nMaxFrames = sizeof(callstack) / sizeof(callstack[0]);\n"
" size_t nFrames = backtrace(callstack, nMaxFrames);\n"
" char **symbols = backtrace_symbols(callstack, nFrames);\n"
" return 0;\n"
"}"
)
try_compile(
RESULT_VAR
${CMAKE_BINARY_DIR}
SOURCES
${CMAKE_BINARY_DIR}/backtrace.cpp
)
SET(HAVE_BACKTRACE_SYMBOLS ${RESULT_VAR})

View File

@@ -114,6 +114,9 @@
/* Define to 1 if you have the <values.h> header file. */
#cmakedefine HAVE_VALUES_H
/* Define to 1 if you have a C runtime with backtrace symbols. */
#cmakedefine HAVE_BACKTRACE_SYMBOLS
/* Name of package */
#define PACKAGE "FreeCAD"

View File

@@ -1679,9 +1679,14 @@ static void freecadNewHandler ()
#include <string>
#include <sstream>
#if HAVE_CONFIG_H
#include <config.h>
#endif // HAVE_CONFIG_H
// This function produces a stack backtrace with demangled function & method names.
void printBacktrace(size_t skip=0)
{
#if defined HAVE_BACKTRACE_SYMBOLS
void *callstack[128];
size_t nMaxFrames = sizeof(callstack) / sizeof(callstack[0]);
size_t nFrames = backtrace(callstack, nMaxFrames);
@@ -1712,6 +1717,10 @@ void printBacktrace(size_t skip=0)
}
free(symbols);
#else //HAVE_BACKTRACE_SYMBOLS
(void)skip;
std::cerr << "Cannot print the stacktrace because the C runtime library doesn't provide backtrace or backtrace_symbols\n";
#endif
}
#endif