MIRA
CallStack.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_CALLSTACK_H_
48 #define _MIRA_CALLSTACK_H_
49 
50 #include <platform/Types.h>
52 
53 #include <execinfo.h>
54 
55 #include <string>
56 #include <vector>
57 
58 #ifndef Q_MOC_RUN
59 #include <boost/shared_array.hpp>
60 #endif
61 
62 namespace mira {
63 
65 
75 class CallStack
76 {
77 protected:
79  CallStack(void** stack, std::size_t stackSize, std::size_t stackStart) :
80  mStack(stack), mStackSize(stackSize),
81  mStackStart(stackStart) {}
82 
83 public:
84 
86  CallStack() : mStackSize(0), mStackStart(0) {}
87 
89  CallStack(const CallStack& other) :
90  mStack(other.mStack), mStackSize(other.mStackSize),
91  mStackStart(other.mStackStart), mSymbols(other.mSymbols) {}
92 
94  const CallStack& operator=(const CallStack& other);
95 
96  template<typename Reflector>
97  void reflect(Reflector& r)
98  {
99  // mStack is only required to obtain the actual symbols,
100  // no need to reflect it (luckily, as shared_array is not supported yet)
101  r.property("stacksize", mStackSize, "");
102  r.property("stackstart", mStackStart, "");
103  r.property("symbols",
104  getter(&CallStack::getSymbols, this),
105  setter(&CallStack::setSymbols, this),
106  "");
107  }
108 
109 public:
110 
115  struct Symbol
116  {
117  Symbol() :
118  address(0),
119  offset(0) {}
120 
121  template<typename Reflector>
122  void reflect(Reflector& r)
123  {
124  r.property("address", address, "");
125  r.property("addressStr", addressStr, "");
126  r.property("offset", offset, "");
127  r.property("name", name, "");
128  r.property("nameDemangled", nameDemangled, "");
129  r.property("file", file, "");
130  }
131 
135  std::string addressStr;
139  std::string name;
142  std::string nameDemangled;
144  std::string file;
145 
153  std::string sourceLocation() const;
154 
159  friend std::ostream& operator<<(std::ostream& os, const Symbol& s);
160  };
161 
163  const Symbol& operator[](std::size_t i) const;
164 
166  std::size_t size() const { return mStackSize-mStackStart; }
167 
169  friend std::ostream& operator<<(std::ostream& os, const CallStack& stack);
170 
171 public:
176  static CallStack backtrace(std::size_t maxSize = 10, std::size_t stackStart = 1);
177 
178 private:
180  void obtainSymbols() const;
181 
182  const std::vector<Symbol>& getSymbols();
183  void setSymbols(const std::vector<Symbol>& symbols);
184 
185 private:
186  boost::shared_array<void*> mStack;
187 
188  std::size_t mStackSize;
189  std::size_t mStackStart;
190 
191  mutable std::vector<Symbol> mSymbols; // the symbols of the call stack
192 };
193 
195 
196 }
197 
198 #endif
int64 offset
The corresponding offset.
Definition: CallStack.h:137
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Setter< T > setter(void(*f)(const T &))
Creates a Setter for global or static class methods taking the argument by const reference.
Definition: GetterSetter.h:443
const CallStack & operator=(const CallStack &other)
Assignment operator with similar behaviour as copy-constructor.
const Symbol & operator[](std::size_t i) const
Returns the i-th symbol of the call stack.
CallStack(void **stack, std::size_t stackSize, std::size_t stackStart)
Constructor for internal use only.
Definition: CallStack.h:79
friend std::ostream & operator<<(std::ostream &os, const Symbol &s)
Prints the symbol in the similar format as the GDB shows function symbols into the stream...
std::size_t size() const
Returns the size of the call stack.
Definition: CallStack.h:166
friend std::ostream & operator<<(std::ostream &os, const CallStack &stack)
Prints the whole stack into a stream in a similar format as the GDB.
CallStack(const CallStack &other)
Copy-Constructor.
Definition: CallStack.h:89
std::string sourceLocation() const
Tries to obtain the source file name and line number for the given symbol using the debug information...
Getter< T > getter(T(*f)())
Creates a Getter for global or static class methods returning the result by value.
Definition: GetterSetter.h:136
void reflect(Reflector &r)
Definition: CallStack.h:97
int64_t int64
Definition: Types.h:61
Encapsulates unix call stack functionality.
Definition: CallStack.h:75
std::string name
The mangled name of the function.
Definition: CallStack.h:139
std::string addressStr
The address of the code portion in a "human readable" format.
Definition: CallStack.h:135
std::string nameDemangled
The demangled name of the function (on Linux only available, if CALLSTACK_LINUX_USE_DEMANGLE is defin...
Definition: CallStack.h:142
Symbol()
Definition: CallStack.h:117
static CallStack backtrace(std::size_t maxSize=10, std::size_t stackStart=1)
Creates the call stack object that contains the callstack starting at the current address of the inst...
int64 address
The address of the code portion as a 64 bit integer.
Definition: CallStack.h:133
Contains all information of a single function symbol in the call stack.
Definition: CallStack.h:115
CallStack()
Constructor that creates an empty call stack.
Definition: CallStack.h:86
std::string file
the binary file
Definition: CallStack.h:144
Provides definition for getters and setters that are used with the serialization framework.
void reflect(Reflector &r)
Definition: CallStack.h:122