SUNphi  1.0
BackTracing.hpp
Go to the documentation of this file.
1 #ifndef _BACKTRACING_HPP
2 #define _BACKTRACING_HPP
3 
4 /// \file BackTracing.hpp
5 ///
6 /// \brief Defines the backtracing function
7 
8 #include <containers/Vector.hpp>
9 #include <debug/Demangle.hpp>
10 #include <utility/String.hpp>
11 
12 namespace SUNphi
13 {
14  /// Decompose the backtrace symbol
16  {
17  public:
18 
19  /// Compilation unit
20  const std::string compilUnit;
21 
22  /// Symbol name
23  const std::string symbol;
24 
25  /// Offset
26  const std::string offset;
27 
28  /// Address
29  const std::string address;
30 
31  /// Parse the string
32  explicit BackTraceSymbol(const char* str) :
33  compilUnit(substrBetweenPos(str,0,std::string(str).find('('))),
34  symbol(substrBetween(str,'(','+')),
35  offset(substrBetween(str,'+',')')),
36  address(substrBetween(str,'[',']'))
37  {
38  }
39  };
40 
41  /// Gets the backtracing symbols list
42  template <int NMAX_REW=128>
44  {
45  /// Rewinded stack
46  void* callstack[NMAX_REW];
47 
48  /// Gets the stack and number of lines to be rewinded
49  int nRew=
50  backtrace(callstack,NMAX_REW);
51 
52  /// Gets the symbols list
53  char** strs=
54  backtrace_symbols(callstack,nRew);
55 
56  /// Result
58  res.reserve(nRew);
59 
60  for(int i=0;i<nRew;i++)
61  res.emplace_back(strs[i]);
62 
63  free(strs);
64 
65  return
66  res;
67  }
68 
69  /// Print a symbol to a stream
70  template <typename T> // Type of the stream
71  T& operator<<(T&& os, ///< Stream
72  const BackTraceSymbol& s) ///< Symbol to print
73  {
74  return
75  os<<s.compilUnit<<
76  ", symbol: "<<
77  ((s.symbol!="")?
78 #ifdef CAN_DEMANGLE
79  demangle(s.symbol)
80 #else
81  s.symbol
82 #endif
83  :"n.a")<<
84  ", offset: "<<((s.offset!="")?s.offset:"n.a")<<
85  ", address: "<<s.address;
86  }
87 
88  /// Write the list of called routines
89  void printBacktraceList();
90 }
91 
92 #endif
void printBacktraceList()
Write the list of called routines.
Definition: BackTracing.cpp:16
Decompose the backtrace symbol.
Definition: BackTracing.hpp:15
Vector< BackTraceSymbol > getBackTraceList()
Gets the backtracing symbols list.
Definition: BackTracing.hpp:43
void divWithMod(Vector< TOut > &quotient, Vector< TOut > &remainder, const Vector &divisor) const
Returns the result and remainder of the division.
Definition: Vector.hpp:310
const std::string offset
Offset.
Definition: BackTracing.hpp:26
const std::string symbol
Symbol name.
Definition: BackTracing.hpp:23
const std::string address
Address.
Definition: BackTracing.hpp:29
const std::string compilUnit
Compilation unit.
Definition: BackTracing.hpp:20