CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
BackTrace.cc
Go to the documentation of this file.
3 #include <iostream>
4 #include <cstring>
5 
7 
8 void BackTrace::trace() const {
10 }
11 
12 
13 #ifdef __linux__
14 
15 #include <execinfo.h>
16 #include <cstdlib>
17 #include <cstdio>
18 #ifndef __USE_GNU
19 #define __USE_GNU
20 #endif
21 
22 #include <dlfcn.h>
23 #include <cxxabi.h>
24 
25 void BackTrace::trace(std::ostream & out) const{
26  static const unsigned int bsize(1024U);
27  char buffer [bsize];
28  void * ltrace [MAX_BACKTRACE_DEPTH];
29  int depth = backtrace (ltrace, MAX_BACKTRACE_DEPTH);
30  if (depth>MAX_BACKTRACE_DEPTH) {
31  out << "Error in backtrace" << std::endl;
32  return;
33  }
34  for (int n = 0; n < depth; ++n) {
35  unsigned long addr = (unsigned long) ltrace[n];
36  Dl_info info;
37 
38  if (dladdr (ltrace[n], &info) && info.dli_fname && info.dli_fname[0]) {
39  const char *libname = info.dli_fname;
40  unsigned long symaddr = (unsigned long) info.dli_saddr;
41  if (info.dli_sname && info.dli_sname[0]) {
42  Demangle ln(info.dli_sname);
43  bool gte = (addr >= symaddr);
44  unsigned long diff = (gte ? addr - symaddr : symaddr - addr);
45  sprintf (buffer, " 0x%08lx %.100s %s 0x%lx [%.100s]\n",
46  addr, ln(), gte ? "+" : "-", diff, libname);
47  } else
48  sprintf (buffer, " 0x%08lx <unknown function> [%.100s]\n", addr, libname);
49  } else {
50  sprintf (buffer, " 0x%08lx <unknown function>\n", addr);
51  }
52  if (::strlen (buffer) > bsize) {
53  out << "Error in backtrace" << std::endl;
54  return;
55  }
56  out.write (buffer, ::strlen (buffer));
57  out.flush();
58  }
59 #ifndef CMS_CHAR_STREAM
60  out << std::ends;
61 #endif
62 }
63 #else
64 void BackTrace::trace(std::ostream & out) const {}
65 #endif
66 
void trace() const
Definition: BackTrace.cc:8
tuple out
Definition: dbtoconf.py:99
perl if(1 lt scalar(@::datatypes))
Definition: edlooper.cc:31
tuple cout
Definition: gather_cfg.py:121
BackTrace()
constructor
Definition: BackTrace.cc:6