CppCMS
backtrace.h
1 //
2 // Copyright (C) 2009-2012 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 BOOSTER_BACKTRACE_H
9 #define BOOSTER_BACKTRACE_H
10 
11 #include <booster/config.h>
12 #include <stdexcept>
13 #include <typeinfo>
14 #include <string>
15 #include <vector>
16 #include <iosfwd>
17 
18 namespace booster {
19 
24  namespace stack_trace {
33  BOOSTER_API int trace(void **addresses,int size);
39  BOOSTER_API void write_symbols(void *const *addresses,int size,std::ostream &);
43  BOOSTER_API std::string get_symbol(void *address);
47  BOOSTER_API std::string get_symbols(void * const *address,int size);
48  } // stack_trace
49 
55 
56  class backtrace {
57  public:
58 
62  static size_t const default_stack_size = 32;
63 
67  backtrace(size_t frames_no = default_stack_size)
68  {
69  if(frames_no == 0)
70  return;
71  frames_.resize(frames_no,0);
72  int size = stack_trace::trace(&frames_.front(),frames_no);
73  frames_.resize(size);
74  }
75 
76  virtual ~backtrace() throw()
77  {
78  }
79 
83  size_t stack_size() const
84  {
85  return frames_.size();
86  }
87 
91  void *return_address(unsigned frame_no) const
92  {
93  if(frame_no < stack_size())
94  return frames_[frame_no];
95  return 0;
96  }
97 
101  void trace_line(unsigned frame_no,std::ostream &out) const
102  {
103  if(frame_no < frames_.size())
104  stack_trace::write_symbols(&frames_[frame_no],1,out);
105  }
106 
110  std::string trace_line(unsigned frame_no) const
111  {
112  if(frame_no < frames_.size())
113  return stack_trace::get_symbol(frames_[frame_no]);
114  return std::string();
115  }
116 
120  std::string trace() const
121  {
122  if(frames_.empty())
123  return std::string();
124  return stack_trace::get_symbols(&frames_.front(),frames_.size());
125  }
126 
130  void trace(std::ostream &out) const
131  {
132  if(frames_.empty())
133  return;
134  stack_trace::write_symbols(&frames_.front(),frames_.size(),out);
135  }
136 
137  private:
138  std::vector<void *> frames_;
139  };
140 
144  class exception : public std::exception, public backtrace {
145  public:
146  };
147 
151  class bad_cast : public std::bad_cast, public backtrace {
152  public:
153  };
154 
158  class runtime_error: public std::runtime_error, public backtrace {
159  public:
160  explicit runtime_error(std::string const &s) : std::runtime_error(s)
161  {
162  }
163  };
164 
168  class range_error: public std::range_error, public backtrace {
169  public:
170  explicit range_error(std::string const &s) : std::range_error(s)
171  {
172  }
173  };
174 
178  class overflow_error: public std::overflow_error, public backtrace {
179  public:
180  explicit overflow_error(std::string const &s) : std::overflow_error(s)
181  {
182  }
183  };
184 
188  class underflow_error: public std::underflow_error, public backtrace {
189  public:
190  explicit underflow_error(std::string const &s) : std::underflow_error(s)
191  {
192  }
193  };
194 
198  class logic_error: public std::logic_error, public backtrace {
199  public:
200  explicit logic_error(std::string const &s) : std::logic_error(s)
201  {
202  }
203  };
204 
208  class domain_error: public std::domain_error, public backtrace {
209  public:
210  explicit domain_error(std::string const &s) : std::domain_error(s)
211  {
212  }
213  };
214 
218  class length_error: public std::length_error, public backtrace {
219  public:
220  explicit length_error(std::string const &s) : std::length_error(s)
221  {
222  }
223  };
224 
228  class invalid_argument : public std::invalid_argument, public backtrace {
229  public:
230  explicit invalid_argument(std::string const &s) : std::invalid_argument(s)
231  {
232  }
233  };
234 
238  class out_of_range : public std::out_of_range, public backtrace {
239  public:
240  explicit out_of_range(std::string const &s) : std::out_of_range(s)
241  {
242  }
243  };
244 
246  namespace details {
247  class trace_manip {
248  public:
249  trace_manip(backtrace const *tr) :
250  tr_(tr)
251  {
252  }
253  std::ostream &write(std::ostream &out) const
254  {
255  if(tr_)
256  tr_->trace(out);
257  return out;
258  }
259  private:
260  backtrace const *tr_;
261  };
262 
263  inline std::ostream &operator<<(std::ostream &out,details::trace_manip const &t)
264  {
265  return t.write(out);
266  }
267  }
269 
284  template<typename E>
285  details::trace_manip trace(E const &e)
286  {
287  backtrace const *tr = dynamic_cast<backtrace const *>(&e);
288  return details::trace_manip(tr);
289  }
290 
291 
292 } // booster
293 
294 #endif
295 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
296 
Same as std::length_error but records stack trace.
Definition: backtrace.h:218
Same as std::runtime_error but records stack trace.
Definition: backtrace.h:158
Same as std::exception but records stack trace.
Definition: backtrace.h:144
BOOSTER_API std::string get_symbols(void *const *address, int size)
Get stack trace information about multiple address recorded.
Same as std::underflow_error but records stack trace.
Definition: backtrace.h:188
Same as std::domain_error but records stack trace.
Definition: backtrace.h:208
Same as std::range_error but records stack trace.
Definition: backtrace.h:168
backtrace(size_t frames_no=default_stack_size)
Definition: backtrace.h:67
void * return_address(unsigned frame_no) const
Definition: backtrace.h:91
BOOSTER_API int trace(void **addresses, int size)
Record stack frame.
std::string trace_line(unsigned frame_no) const
Definition: backtrace.h:110
Same as std::overflow_error but records stack trace.
Definition: backtrace.h:178
Same as std::out_of_range but records stack trace.
Definition: backtrace.h:238
size_t stack_size() const
Definition: backtrace.h:83
Same as std::logic_error but records stack trace.
Definition: backtrace.h:198
BOOSTER_API void write_symbols(void *const *addresses, int size, std::ostream &)
Print stack trace.
the class that records the stack trace when it is created,
Definition: backtrace.h:56
Same as std::bad_cast but records stack trace.
Definition: backtrace.h:151
std::string trace() const
Definition: backtrace.h:120
Same as std::invalid_argument but records stack trace.
Definition: backtrace.h:228
void trace(std::ostream &out) const
Definition: backtrace.h:130
Booster library namespace. The library that implements Boost Like API in ABI backward compatible way...
Definition: application.h:23
BOOSTER_API std::string get_symbol(void *address)
Get stack trace information about a single address recorded.
void trace_line(unsigned frame_no, std::ostream &out) const
Definition: backtrace.h:101