MAIA bb96820c
Multiphysics at AIA
Loading...
Searching...
No Matches
maia::debug Namespace Reference

Classes

struct  Tracer
 

Functions

void backtrace ()
 Prints a backtrace of the function call path if possible. More...
 
void backtrace_gcc (const MString &fileName, const MInt noFramesOmitted=1)
 Prints a backtrace of the function call path if possible. More...
 
void backtrace_gcc (const MString &NotUsed(fileName), MInt NotUsed(noFramesOmitted))
 

Function Documentation

◆ backtrace()

void maia::debug::backtrace ( )
inline
Author
Fabian Klemp f.kle.nosp@m.mp@a.nosp@m.ia.rw.nosp@m.th-a.nosp@m.achen.nosp@m..de
Date
2015-09-16

Uses the LLVMSupport library to print a stacktrace Note: use BACKTRACE(...) instead of calling this method directly

Definition at line 58 of file backtrace.h.

58 {
59 llvm::errs() << "Backtrace (line numbers may be too large by 1-3 lines):\n";
60 llvm::sys::PrintStackTrace(llvm::errs());
61}

◆ backtrace_gcc() [1/2]

void maia::debug::backtrace_gcc ( const MString fileName,
const MInt  noFramesOmitted = 1 
)
inline
Author
Michael Schlottke (mic) mic@a.nosp@m.ia.r.nosp@m.wth-a.nosp@m.ache.nosp@m.n.de
Date
2013-10-09
Parameters
[in]noFramesOmittedSet number of omitted frames (e.g. to hide backtrace() itself).

Works only with GCC. Needs command line tool 'addr2line' to be callable from within MAIA.

Definition at line 77 of file backtrace.h.

77 {
78 // Get trace
79 const int maxNoTraces = 128;
80 void* trace[maxNoTraces];
81 int noTraces = ::backtrace(trace, maxNoTraces);
82
83 // Get messages (i.e. function names)
84 char** messages = backtrace_symbols(trace, noTraces);
85
86 // Create stream and char buffer for single-flush printing
87 std::stringstream s;
88 const MInt maxLineLength = 1024;
89 MChar line[maxLineLength];
90
91 // Print out messages one by one
92 const MInt maxCmdSize = 1024;
93 MInt frameId = 0;
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 "
97 "lines):\n";
98 for(MInt i = noFramesOmitted; i < noTraces; i++) {
99 // Get demangled function name
100 MString mangled(messages[i]);
101 mangled = mangled.substr(mangled.find("(") + 1);
102 mangled = mangled.substr(0, mangled.find("+"));
103 int status;
104 char* demangledChar = abi::__cxa_demangle(mangled.c_str(), 0, 0, &status);
105
106 // Only use demangled name if demangling worked
107 const MString demangled = (status == 0) ? demangledChar : messages[i];
108 free(demangledChar);
109
110 // Get filename/line number using addr2line
111 char cmd[maxCmdSize];
112 std::sprintf(cmd, "addr2line %p -s -e %s", trace[i], fileName.c_str());
113 MString fileline;
114 FILE* pipe = popen(cmd, "r");
115 if(!pipe) {
116 fileline = "n/a";
117 } else {
118 char buffer[128];
119 while(!feof(pipe)) {
120 if(fgets(buffer, 128, pipe) != nullptr) {
121 fileline += buffer;
122 }
123 }
124 }
125 pclose(pipe);
126
127 // Parse output of addr2line to skip newline and possible discriminator
128 // information
129 fileline = fileline.substr(0, fileline.length() - 1);
130 fileline = fileline.substr(0, fileline.find(" "));
131
132 // Print function names + filename:lineno
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';
136 s << line;
137 }
138 std::cout << s.str() << std::endl;
139}
int32_t MInt
Definition: maiatypes.h:62
std::basic_string< char > MString
Definition: maiatypes.h:55
char MChar
Definition: maiatypes.h:56
void free(void *)
Definition: nvhpcbugfix.cpp:11

◆ backtrace_gcc() [2/2]

void maia::debug::backtrace_gcc ( const MString NotUsedfileName,
MInt   NotUsednoFramesOmitted 
)
inline

Definition at line 141 of file backtrace.h.

141 {
142 std::cout << "Not using GCC - backtrace disabled." << std::endl;
143}