GRSISort
Created by P.C. Bender
Developement Team: P.C. Bender, R. Dunlop, V. Bildstein
An extension of the ROOT analysis Framework
Globals.h
Go to the documentation of this file.
1 #ifndef GLOBALS_H
2 #define GLOBALS_H
3 
4 #define RESET_COLOR "\033[m"
5 #define BLUE "\033[1;34m"
6 #define YELLOW "\033[1;33m"
7 #define GREEN "\033[1;32m"
8 #define RED "\033[1;31m"
9 #define BLACK "\033[1;30m"
10 #define MAGENTA "\033[1;35m"
11 #define CYAN "\033[1;36m"
12 #define WHITE "\033[1;37m"
13 
14 #define DBLUE "\033[0;34m"
15 #define DYELLOW "\033[0;33m"
16 #define DGREEN "\033[0;32m"
17 #define DRED "\033[0;31m"
18 #define DBLACK "\033[0;30m"
19 #define DMAGENTA "\033[0;35m"
20 #define DCYAN "\033[0;36m"
21 #define DWHITE "\033[0;37m"
22 
23 #define BG_WHITE "\033[47m"
24 #define BG_RED "\033[41m"
25 #define BG_GREEN "\033[42m"
26 #define BG_YELLOW "\033[43m"
27 #define BG_BLUE "\033[44m"
28 #define BG_MAGENTA "\033[45m"
29 #define BG_CYAN "\033[46m"
30 
31 #define HIDE_CURSOR "\033[?25l"
32 #define SHOW_CURSOR "\033[?25h"
33 
34 #define ALERTTEXT "\033[47m\033[0;31m"
35 
36 #define NUM_SIS_CHAN 8
37 
38 #define MAXSAMPLESIZE 8192
39 
40 #define FRAGMENTBUFFERSIZE 1000
41 
42 #define BUILDINGTIMECONDITION 2
43 #define BUILDINGTRIGGERCONDITION 999
44 
45 #if __APPLE__
46 #ifdef __CINT__
47 #undef __GNUC__
48 typedef char __signed;
49 typedef char int8_t;
50 #endif
51 #endif
52 
53 #if __APPLE__
54 //#include <_types/_uint8_t.h>
55 #include <_types/_uint16_t.h>
56 #include <_types/_uint32_t.h>
57 #include <_types/_uint64_t.h>
58 #include <sys/_types/_int16_t.h>
59 #else
60 #include <cstdint>
61 #endif
62 
63 #include <iostream>
64 #include <iomanip>
65 #include <stdexcept>
66 #include <string>
67 #include <cstdio>
68 #include <cstdlib>
69 #include <execinfo.h>
70 #include <cxxabi.h>
71 #include <sstream>
72 #include <array>
73 #include <memory>
74 #include <unistd.h>
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <sys/wait.h>
78 #include <sys/prctl.h>
79 
80 const std::string& ProgramName();
81 
82 namespace grsi {
83 struct exit_exception : public std::exception {
84 public:
85  exit_exception(int c, const char* msg = "") : code(c), message(msg) {}
86  ~exit_exception() throw() override = default;
87  /* virtual const char* what() const throw {
88  // LOG(what); // write to log file
89  return what.c_str();
90  }*/
91 
92  const int code;
93  const char* message;
94 };
95 
96 //-------------------- three function templates that print all arguments into a string
97 //this template uses existing stream and appends the last argument to it
98 template <typename T>
99 void Append(std::stringstream& stream, const T& tail) {
100  // append last argument
101  stream<<tail;
102 }
103 
104 //this template uses existing stream and appends to it
105 template <typename T, typename... U>
106 void Append(std::stringstream& stream, const T& head, const U&... tail) {
107  // append first argument
108  stream<<head;
109 
110  // reversely call this template (or the one with the last argument)
111  Append(stream,tail...);
112 }
113 
114 //this function typically gets called by user
115 template <typename T, typename... U>
116 std::string Stringify(const T& head, const U&... tail) {
117  // print first arguments to string
118  std::stringstream stream;
119  stream<<head;
120 
121  // call the second template (or the third if tail is just one argument)
122  Append(stream,tail...);
123 
124  // append a newline
125  stream<<std::endl;
126 
127  // return resulting string
128  return stream.str();
129 }
130 
131 } // end of namespace grsi
132 
133 template <typename T>
134 inline std::string hex(T val, int width = -1)
135 {
136  std::ostringstream str;
137  str<<"0x"<<std::hex;
138  if(width > 0) {
139  str<<std::setfill('0')<<std::setw(width);
140  }
141  str<<val;
142  if(width > 0) {
143  str<<std::setfill(' ');
144  }
145  return str.str();
146 }
147 
148 static inline std::string getexepath() {
149  char result[1024];
150  ssize_t count = readlink("/proc/self/exe", result, sizeof(result)-1);
151  return std::string(result, (count > 0) ? count : 0);
152 }
153 
154 static inline std::string sh(std::string cmd) {
155  std::array<char, 128> buffer;
156  std::string result;
157  std::shared_ptr<FILE> pipe(popen(cmd.c_str(), "r"), pclose);
158  if(!pipe) throw std::runtime_error("popen() failed!");
159  while(!feof(pipe.get())) {
160  if(fgets(buffer.data(), 128, pipe.get()) != nullptr) {
161  result += buffer.data();
162  }
163  }
164  return result;
165 }
166 
167 // print a demangled stack backtrace of the caller function (copied from https://panthema.net/2008/0901-stacktrace-demangled/)
168 static inline void PrintStacktrace(std::ostream& out = std::cout, unsigned int maxFrames = 63)
169 {
170  std::stringstream str;
171  str<<"stack trace:"<<std::endl;
172 
173  // storage array for stack trace address data
174  void** addrlist = new void*[maxFrames+1];
175 
176  // retrieve current stack addresses
177  int addrlen = backtrace(addrlist, maxFrames+1);
178 
179  if(addrlen == 0) {
180  str<<" <empty, possibly corrupt>"<<std::endl;
181  out<<str.str();
182  return;
183  }
184 
185  // resolve addresses into strings containing "filename(function+address)",
186  // this array must be free()-ed
187  char** symbollist = backtrace_symbols(addrlist, addrlen);
188 
189  // allocate string which will be filled with the demangled function name
190  size_t funcnamesize = 256;
191  char* funcname = new char[funcnamesize];
192 
193  // iterate over the returned symbol lines. skip the first, it is the
194  // address of this function.
195  for(int i = 2; i < addrlen; i++) {
196  char* begin_name = nullptr;
197  char* begin_offset = nullptr;
198  char* end_offset = nullptr;
199 
200  // find parentheses and +address offset surrounding the mangled name:
201  // ./module(function+0x15c) [0x8048a6d]
202  for(char* p = symbollist[i]; *p; ++p) {
203  if(*p == '(') {
204  begin_name = p;
205  } else if (*p == '+') {
206  begin_offset = p;
207  } else if(*p == ')' && begin_offset) {
208  end_offset = p;
209  break;
210  }
211  }
212 
213  // try and decode file and line number (only if we have an absolute path)
214  //std::string line;
215  //if(symbollist[i][0] == '/') {
216  // std::stringstream command;
217  // std::string filename = symbollist[i];
218  // command<<"addr2line "<<addrlist[i]<<" -e "<<filename.substr(0, filename.find_first_of('('));
219  // //std::cout<<symbollist[i]<<": executing command "<<command.str()<<std::endl;
220  // line = sh(command.str());
221  //}
222 
223  if(begin_name && begin_offset && end_offset && begin_name < begin_offset) {
224  *begin_name++ = '\0';
225  *begin_offset++ = '\0';
226  *end_offset = '\0';
227 
228  // mangled name is now in [begin_name, begin_offset) and caller
229  // offset in [begin_offset, end_offset). now apply
230  // __cxa_demangle():
231 
232  int status;
233  char* ret = abi::__cxa_demangle(begin_name, funcname, &funcnamesize, &status);
234  if(status == 0) {
235  funcname = ret; // use possibly realloc()-ed string
236  str<<" "<<symbollist[i]<<": "<<funcname<<"+"<<begin_offset<<std::endl;
237  } else {
238  // demangling failed. Output function name as a C function with
239  // no arguments.
240  str<<" "<<symbollist[i]<<": "<<begin_name<<"()+"<<begin_offset<<std::endl;
241  }
242  } else {
243  // couldn't parse the line? print the whole line.
244  str<<" "<<symbollist[i]<<std::endl;
245  }
246  //str<<line;
247  }
248 
249  free(funcname);
250  free(symbollist);
251  out<<str.str();
252 }
253 
254 static inline void PrintGdbStacktrace() {
255  char pid_buf[30];
256  sprintf(pid_buf, "%d", getpid());
257  char name_buf[512];
258  name_buf[readlink("/proc/self/exe", name_buf, 511)] = 0;
259  prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0);
260  int child_pid = fork();
261  if (!child_pid) {
262  dup2(2,1); // redirect output to stderr - edit: unnecessary?
263  execl("/usr/bin/gdb", "gdb", "--batch", "-n", "-ex", "thread", "-ex", "bt", name_buf, pid_buf, NULL);
264  abort(); /* If gdb failed to start */
265  } else {
266  waitpid(child_pid,NULL,0);
267  }
268 }
269 
270 #endif
void Append(std::stringstream &stream, const T &tail)
Definition: Globals.h:99
STL namespace.
static std::string sh(std::string cmd)
Definition: Globals.h:154
std::string hex(T val, int width=-1)
Definition: Globals.h:134
static std::string getexepath()
Definition: Globals.h:148
static void PrintStacktrace(std::ostream &out=std::cout, unsigned int maxFrames=63)
Definition: Globals.h:168
static void PrintGdbStacktrace()
Definition: Globals.h:254
Definition: Globals.h:82
std::string Stringify(const T &head, const U &... tail)
Definition: Globals.h:116
~exit_exception() override=default
exit_exception(int c, const char *msg="")
Definition: Globals.h:85
const std::string & ProgramName()
const int code
Definition: Globals.h:92
const char * message
Definition: Globals.h:93