15#ifdef MAIA_GCC_COMPILER
22#if defined(ENABLE_BACKTRACE)
23#include "llvm/Support/Signals.h"
24#ifndef __STDC_LIMIT_MACROS
25#define __STDC_LIMIT_MACROS
27#ifndef __STDC_CONSTANT_MACROS
28#define __STDC_CONSTANT_MACROS
30#include "llvm/Support/raw_ostream.h"
34 maia::debug::backtrace(); \
57#if defined(ENABLE_BACKTRACE)
59 llvm::errs() <<
"Backtrace (line numbers may be too large by 1-3 lines):\n";
60 llvm::sys::PrintStackTrace(llvm::errs());
76#ifdef MAIA_GCC_COMPILER
79 const int maxNoTraces = 128;
80 void* trace[maxNoTraces];
84 char** messages = backtrace_symbols(trace, noTraces);
88 const MInt maxLineLength = 1024;
89 MChar line[maxLineLength];
92 const MInt maxCmdSize = 1024;
94 const MInt noFrames = noTraces - noFramesOmitted;
95 const MInt width = noFrames ? floor(log10(noFrames)) + 1 : 1;
96 s <<
"\nBacktrace (line numbers are usually too large, typically by 1-3 "
98 for(
MInt i = noFramesOmitted; i < noTraces; i++) {
101 mangled = mangled.substr(mangled.find(
"(") + 1);
102 mangled = mangled.substr(0, mangled.find(
"+"));
104 char* demangledChar = abi::__cxa_demangle(mangled.c_str(), 0, 0, &status);
107 const MString demangled = (status == 0) ? demangledChar : messages[i];
111 char cmd[maxCmdSize];
112 std::sprintf(cmd,
"addr2line %p -s -e %s", trace[i], fileName.c_str());
114 FILE* pipe = popen(cmd,
"r");
120 if(fgets(buffer, 128, pipe) !=
nullptr) {
129 fileline = fileline.substr(0, fileline.length() - 1);
130 fileline = fileline.substr(0, fileline.find(
" "));
133 std::sprintf(line,
"#%-*d 0x%016lx in %s at %s\n", width, frameId++,
reinterpret_cast<uintptr_t
>(trace[i]),
134 demangled.c_str(), fileline.c_str());
135 line[maxLineLength - 1] =
'\0';
138 std::cout << s.str() << std::endl;
142 std::cout <<
"Not using GCC - backtrace disabled." << std::endl;
std::basic_string< char > MString
void backtrace_gcc(const MString &fileName, const MInt noFramesOmitted=1)
Prints a backtrace of the function call path if possible.
void backtrace()
Prints a backtrace of the function call path if possible.
Namespace for auxiliary functions/classes.