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 #ifdef MIRA_LINUX
54 # include <execinfo.h>
55 #endif
56 
57 #ifdef MIRA_WINDOWS
58 # include <DbgHelp.h>
59 #endif
60 
61 #include <stdlib.h>
62 #include <stdio.h>
63 
64 #include <iostream>
65 #include <string>
66 #include <vector>
67 
68 #ifndef Q_MOC_RUN
69 #include <boost/shared_array.hpp>
70 #include <boost/lexical_cast.hpp>
71 #endif
72 
73 namespace mira {
74 
76 
87 {
88 protected:
89 #ifdef MIRA_LINUX
90  CallStack(void** stack, std::size_t stackSize, std::size_t stackStart) :
92  mStack(stack), mStackSize(stackSize),
93  mStackStart(stackStart) {}
94 #endif
95 #ifdef MIRA_WINDOWS
96  CallStack(const std::vector<STACKFRAME64>& stack,
98  std::size_t stackSize, std::size_t stackStart) :
99  mStack(stack), mStackSize(stackSize),
100  mStackStart(stackStart) {}
101 #endif
102 
103 public:
104 
106  CallStack() : mStackSize(0), mStackStart(0) {}
107 
109  CallStack(const CallStack& other) :
110  mStack(other.mStack), mStackSize(other.mStackSize),
111  mStackStart(other.mStackStart), mSymbols(other.mSymbols) {}
112 
114  const CallStack& operator=(const CallStack& other);
115 
116  template<typename Reflector>
117  void reflect(Reflector& r)
118  {
119  // mStack is only required to obtain the actual symbols,
120  // no need to reflect it (luckily, as shared_array is not supported yet)
121  r.property("stacksize", mStackSize, "");
122  r.property("stackstart", mStackStart, "");
123  r.property("symbols",
124  getter(&CallStack::getSymbols, this),
125  setter(&CallStack::setSymbols, this),
126  "");
127  }
128 
129 public:
130 
135  struct Symbol
136  {
137  Symbol() :
138  address(0),
139  offset(0) {}
140 
141  template<typename Reflector>
142  void reflect(Reflector& r)
143  {
144  r.property("address", address, "");
145  r.property("addressStr", addressStr, "");
146  r.property("offset", offset, "");
147  r.property("name", name, "");
148  r.property("nameDemangled", nameDemangled, "");
149  r.property("file", file, "");
150  }
151 
153  int64 address;
155  std::string addressStr;
157  int64 offset;
159  std::string name;
162  std::string nameDemangled;
164  std::string file;
165 
175  std::string sourceLocation() const;
176 
181  friend MIRA_BASE_EXPORT std::ostream& operator<<(std::ostream& os, const Symbol& s);
182  };
183 
185  const Symbol& operator[](std::size_t i) const;
186 
188  std::size_t size() const { return mStackSize-mStackStart; }
189 
191  friend MIRA_BASE_EXPORT std::ostream& operator<<(std::ostream& os, const CallStack& stack);
192 
193 public:
198  static CallStack backtrace(std::size_t maxSize = 10, std::size_t stackStart = 1);
199 
200 private:
202  void obtainSymbols() const;
203 
204  const std::vector<Symbol>& getSymbols();
205  void setSymbols(const std::vector<Symbol>& symbols);
206 
207 private:
208 #ifdef MIRA_LINUX
209  boost::shared_array<void*> mStack;
210 #endif
211 #ifdef MIRA_WINDOWS
212  std::vector<STACKFRAME64> mStack;
213 #endif
214  std::size_t mStackSize;
215  std::size_t mStackStart;
216 
217  mutable std::vector<Symbol> mSymbols; // the symbols of the call stack
218 };
219 
221 
222 }
223 
224 #endif
Typedefs for OS independent basic data types.
int64 offset
The corresponding offset.
Definition: CallStack.h:157
PropertyHint file(const std::string &filters=std::string(), bool save=false)
Tells the property editor that the path is for a file, and that it should show a "File Open"/"File Sa...
Definition: Path.h:247
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
std::ostream & operator<<(std::ostream &s, const LibraryVersion &version)
std::size_t size() const
Returns the size of the call stack.
Definition: CallStack.h:188
CallStack(const CallStack &other)
Copy-Constructor.
Definition: CallStack.h:109
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:117
Encapsulates unix call stack functionality.
Definition: CallStack.h:86
std::string name
The mangled name of the function.
Definition: CallStack.h:159
std::string addressStr
The address of the code portion in a "human readable" format.
Definition: CallStack.h:155
std::string nameDemangled
The demangled name of the function (on Linux only available, if CALLSTACK_LINUX_USE_DEMANGLE is defin...
Definition: CallStack.h:162
#define MIRA_BASE_EXPORT
This is required because on windows there is a macro defined called ERROR.
Definition: Platform.h:153
Symbol()
Definition: CallStack.h:137
int64 address
The address of the code portion as a 64 bit integer.
Definition: CallStack.h:153
Contains all information of a single function symbol in the call stack.
Definition: CallStack.h:135
CallStack()
Constructor that creates an empty call stack.
Definition: CallStack.h:106
std::string file
the binary file
Definition: CallStack.h:164
Provides definition for getters and setters that are used with the serialization framework.
void reflect(Reflector &r)
Definition: CallStack.h:142