MIDAS
Loading...
Searching...
No Matches
mexcept.h
Go to the documentation of this file.
1/********************************************************************\
2
3 Name: mexception.hxx
4 Created by: Stefan Ritt
5
6 Contents: Midas exception with stack trace
7
8\********************************************************************/
9
10#ifndef _MEXCEPT_HXX
11#define _MEXCEPT_HXX
12
13#include <string>
14#include <iostream>
15#include <sstream>
16#include <stdexcept>
17#include <execinfo.h>
18#include <dlfcn.h>
19#include <cxxabi.h>
20
21/*------------------------------------------------------------------*/
22
23// throw exception with full stack trace
24#define mthrow(arg) (throw mexception(arg, true, __FILE__, __LINE__))
25
26// throw exception with line number and file name
27#define mthrow1(arg) (throw mexception(arg, false, __FILE__, __LINE__))
28
29// throw exception without file and line number
30#define mthrow2(arg) (throw mexception(arg, false, "", 0))
31
32class mexception : public std::runtime_error {
33 std::string msg;
34public:
35 mexception(const std::string &arg, bool stack_trace, const char *file, int line) :
36 std::runtime_error(arg) {
37 std::stringstream trace_buf;
38 if (file[0])
39 trace_buf << file << ":" << line << ": " << arg;
40 else
41 trace_buf << arg;
42
43 if (stack_trace) {
44 // create strack trace and add it to message
45 trace_buf << std::endl << "Stack trace:" << std::endl;
46 void *callstack[128];
47 char buf[1024];
48 int n = backtrace(callstack, sizeof(callstack) / sizeof(callstack[0]));
50 for (int i = 1; i < n; i++) {
52 if (dladdr(callstack[i], &info)) {
53 char *demangled = nullptr;
54 int status = 0;
55 demangled = abi::__cxa_demangle(info.dli_sname, nullptr, 0, &status);
56 snprintf(buf, sizeof(buf), "%-3d 0x%018lX %s + %zd\n",
57 i, (long unsigned int) callstack[i],
58 status == 0 ? demangled : info.dli_sname,
59 (char *) callstack[i] - (char *) info.dli_saddr);
60 free(demangled);
61 } else {
62 snprintf(buf, sizeof(buf), "%-3d 0x%018lX\n",
63 i, (long unsigned int) callstack[i]);
64 }
65 trace_buf << buf;
66 }
67 free(symbols);
68 }
69
70 msg = trace_buf.str();
71 }
72
74
75 const char *what() const throw() {
76 return msg.c_str();
77 }
78
79 friend std::ostream &operator<<(std::ostream &output, mexception &m) {
80 output << m.what();
81 return output;
82 };
83
84 mexception(const mexception &ex) noexcept : std::runtime_error(ex.msg),msg(ex.msg) {};
85};
86
87static_assert(std::is_nothrow_copy_constructible<mexception>::value,
88 "mexception must be nothrow copy constructible");
89
90#endif // _MEXCEPT_HXX
const char * what() const
Definition mexcept.h:75
mexception(const std::string &arg, bool stack_trace, const char *file, int line)
Definition mexcept.h:35
friend std::ostream & operator<<(std::ostream &output, mexception &m)
Definition mexcept.h:79
~mexception()
Definition mexcept.h:73
std::string msg
Definition mexcept.h:33
mexception(const mexception &ex) noexcept
Definition mexcept.h:84
void ** info
Definition fesimdaq.cxx:41
DWORD n[4]
Definition mana.cxx:247
INT i
Definition mdump.cxx:32
static void output(code_int code)
Definition mgd.cxx:1647
DWORD status
Definition odbhist.cxx:39
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
Definition rmidas.h:24