Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

StackTrace.h

Go to the documentation of this file.
00001 00002 #ifndef STACK_TRACE_H 00003 #define STACK_TRACE_H 00004 00005 #include "common.h" 00006 #include "nmstl_util.h" 00007 #include "execinfo.h" 00008 00009 #include <unistd.h> 00010 #include <sys/types.h> 00011 #include <sys/wait.h> 00012 00013 // A very inefficient, but accurate, stack tracer. 00014 // 00015 // Steps: 00016 // 00017 // 1. Call the glibc backtrace() function to get the list of 00018 // return addresses on the stack 00019 // 2. For each backtrace address, 00020 // 00021 // - use dladdr1 to determine which shared library (or the main 00022 // executable) contains the address. If a shared library, 00023 // subtract the .so's base address from the backtrace address. 00024 // - call addr2line to get the function, file name, and line 00025 // number of the invocation 00026 // 00027 // This all so Linux-and-glibc-dependent it's not funny. 00028 00029 struct sigcontext; 00030 00031 BOREALIS_NAMESPACE_BEGIN; 00032 00033 class StackTrace { 00034 public: 00035 static StackTrace *save(unsigned int skip_frames = 0); 00036 static string get(unsigned int skip_frames = 0); 00037 00038 static StackTrace *save(const sigcontext& ctx); 00039 static string get(const sigcontext& ctx); 00040 00041 ~StackTrace(); 00042 00043 string as_string() const { 00044 return "StackTrace(" + to_string(_frames.size()) + " frames)"; 00045 } 00046 00047 string repr() const; 00048 00049 private: 00050 static string exec(const char *const argv[]); 00051 static string basename(string path); 00052 00053 StackTrace(unsigned int skip_frames, const sigcontext *); 00054 00055 vector<void*> _frames; 00056 mutable string _trace; 00057 }; 00058 00059 BOREALIS_NAMESPACE_END; 00060 #endif

Generated on Fri Nov 12 15:15:22 2004 for Borealis by doxygen 1.3.8