ReFRACtor
backtrace.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2010 Artyom Beilis (Tonkikh)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 #ifndef BOOST_BACKTRACE_HPP
9 #define BOOST_BACKTRACE_HPP
10 
11 #include <boost/config.hpp>
12 #include <stdexcept>
13 #include <typeinfo>
14 #include <vector>
15 #include <iosfwd>
16 #include <string>
17 
18 namespace boost {
19 
20  namespace stack_trace {
21  int trace(void **addresses,int size);
22  void write_symbols(void *const *addresses,int size,std::ostream &);
23  std::string get_symbol(void *address);
24  std::string get_symbols(void * const *address,int size);
25  } // stack_trace
26 
27  class backtrace {
28  public:
29 
30  static size_t const default_stack_size = 32;
31 
32  backtrace(size_t frames_no = default_stack_size)
33  {
34  if(frames_no == 0)
35  return;
36  frames_.resize(frames_no,0);
37  int size = stack_trace::trace(&frames_.front(),frames_no);
38  frames_.resize(size);
39  }
40 
41  virtual ~backtrace() throw()
42  {
43  }
44 
45  size_t stack_size() const
46  {
47  return frames_.size();
48  }
49 
50  void *return_address(unsigned frame_no) const
51  {
52  if(frame_no < stack_size())
53  return frames_[frame_no];
54  return 0;
55  }
56 
57  void trace_line(unsigned frame_no,std::ostream &out) const
58  {
59  if(frame_no < frames_.size())
60  stack_trace::write_symbols(&frames_[frame_no],1,out);
61  }
62 
63  std::string trace_line(unsigned frame_no) const
64  {
65  if(frame_no < frames_.size())
66  return stack_trace::get_symbol(frames_[frame_no]);
67  return std::string();
68  }
69 
70  std::string trace() const
71  {
72  if(frames_.empty())
73  return std::string();
74  return stack_trace::get_symbols(&frames_.front(),frames_.size());
75  }
76 
77  void trace(std::ostream &out) const
78  {
79  if(frames_.empty())
80  return;
81  stack_trace::write_symbols(&frames_.front(),frames_.size(),out);
82  }
83 
84  private:
85  std::vector<void *> frames_;
86  };
87 
88  // This isn't actually in boost yet, so don't define these.
89  // class exception : public std::exception, public backtrace {
90  // public:
91  // };
92 
93  // class bad_cast : public std::bad_cast, public backtrace {
94  // public:
95  // };
96 
97  // class runtime_error: public std::runtime_error, public backtrace {
98  // public:
99  // explicit runtime_error(std::string const &s) : std::runtime_error(s)
100  // {
101  // }
102  // };
103 
104  // class range_error: public std::range_error, public backtrace {
105  // public:
106  // explicit range_error(std::string const &s) : std::range_error(s)
107  // {
108  // }
109  // };
110 
111  // class overflow_error: public std::overflow_error, public backtrace {
112  // public:
113  // explicit overflow_error(std::string const &s) : std::overflow_error(s)
114  // {
115  // }
116  // };
117 
118  // class underflow_error: public std::underflow_error, public backtrace {
119  // public:
120  // explicit underflow_error(std::string const &s) : std::underflow_error(s)
121  // {
122  // }
123  // };
124 
125  // class logic_error: public std::logic_error, public backtrace {
126  // public:
127  // explicit logic_error(std::string const &s) : std::logic_error(s)
128  // {
129  // }
130  // };
131 
132  // class domain_error: public std::domain_error, public backtrace {
133  // public:
134  // explicit domain_error(std::string const &s) : std::domain_error(s)
135  // {
136  // }
137  // };
138 
139  // class length_error: public std::length_error, public backtrace {
140  // public:
141  // explicit length_error(std::string const &s) : std::length_error(s)
142  // {
143  // }
144  // };
145 
146  // class invalid_argument : public std::invalid_argument, public backtrace {
147  // public:
148  // explicit invalid_argument(std::string const &s) : std::invalid_argument(s)
149  // {
150  // }
151  // };
152 
153  // class out_of_range : public std::out_of_range, public backtrace {
154  // public:
155  // explicit out_of_range(std::string const &s) : std::out_of_range(s)
156  // {
157  // }
158  // };
159 
160  namespace details {
161  class trace_manip {
162  public:
163  trace_manip(backtrace const *tr) :
164  tr_(tr)
165  {
166  }
167  std::ostream &write(std::ostream &out) const
168  {
169  if(tr_)
170  tr_->trace(out);
171  return out;
172  }
173  private:
174  backtrace const *tr_;
175  };
176 
177  inline std::ostream &operator<<(std::ostream &out,details::trace_manip const &t)
178  {
179  return t.write(out);
180  }
181  }
182 
183  template<typename E>
185  {
186  backtrace const *tr = dynamic_cast<backtrace const *>(&e);
187  return details::trace_manip(tr);
188  }
189 
190 
191 } // boost
192 
193 #endif
194 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
195 
trace_manip(backtrace const *tr)
Definition: backtrace.hpp:163
std::string trace() const
Definition: backtrace.hpp:70
Definition: doxygen.h:52
void trace(std::ostream &out) const
Definition: backtrace.hpp:77
size_t stack_size() const
Definition: backtrace.hpp:45
backtrace(size_t frames_no=default_stack_size)
Definition: backtrace.hpp:32
std::string get_symbols(void *const *ptrs, int size)
Definition: backtrace.cpp:276
void write_symbols(void *const *addresses, int size, std::ostream &out)
Definition: backtrace.cpp:286
int trace(void **, int)
Definition: backtrace.cpp:66
virtual ~backtrace()
Definition: backtrace.hpp:41
std::ostream & operator<<(std::ostream &out, details::trace_manip const &t)
Definition: backtrace.hpp:177
void * return_address(unsigned frame_no) const
Definition: backtrace.hpp:50
std::string get_symbol(void *ptr)
Definition: backtrace.cpp:266
std::string trace_line(unsigned frame_no) const
Definition: backtrace.hpp:63
std::ostream & write(std::ostream &out) const
Definition: backtrace.hpp:167
void trace_line(unsigned frame_no, std::ostream &out) const
Definition: backtrace.hpp:57

Copyright © 2017, California Institute of Technology.
ALL RIGHTS RESERVED.
U.S. Government Sponsorship acknowledged.
Generated Fri Aug 24 2018 15:44:11