/********************************************************************\ Name: mexception.hxx Created by: Stefan Ritt Contents: Midas exception with stack trace \********************************************************************/ #ifndef _MEXCEPT_HXX #define _MEXCEPT_HXX #include #include #include #include #include #include #if defined(__linux__) || defined(__APPLE__) #define HAVE_BACKTRACE #endif #ifdef HAVE_BACKTRACE #include #endif /*------------------------------------------------------------------*/ // exceptions with line number and file name #define mthrow(arg) (throw mexception(arg, __FILE__, __LINE__)) class mexception : public std::runtime_error { std::string msg; public: mexception(const std::string &arg, const char *file, int line) : std::runtime_error(arg) { std::stringstream trace_buf; trace_buf << "\n" << file << ":" << line << ": " << arg << std::endl; // create strack trace and add it to message #ifdef HAVE_BACKTRACE trace_buf << "Stack trace:" << std::endl; void *callstack[128]; char buf[1024]; int n = backtrace(callstack, sizeof(callstack)/sizeof(callstack[0])); char **symbols = backtrace_symbols(callstack, n); for (int i=1 ; i