L4Re - L4 Runtime Environment
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
exceptions
1 // vi:ft=cpp
7 /*
8  * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
9  * Alexander Warg <warg@os.inf.tu-dresden.de>
10  * economic rights: Technische Universität Dresden (Germany)
11  *
12  * This file is part of TUD:OS and distributed under the terms of the
13  * GNU General Public License 2.
14  * Please see the COPYING-GPL-2 file for details.
15  *
16  * As a special exception, you may use this file as part of a free software
17  * library without restriction. Specifically, if other files instantiate
18  * templates or use macros or inline functions from this file, or you compile
19  * this file and link it with other files to produce an executable, this
20  * file does not by itself cause the resulting executable to be covered by
21  * the GNU General Public License. This exception does not however
22  * invalidate any other reasons why the executable file might be covered by
23  * the GNU General Public License.
24  */
25 
26 #pragma once
27 
28 #include <l4/cxx/l4types.h>
29 #include <l4/cxx/basic_ostream>
30 #include <l4/sys/err.h>
31 #include <l4/sys/capability>
32 
33 
39 
40 #ifndef L4_CXX_NO_EXCEPTION_BACKTRACE
41 # define L4_CXX_EXCEPTION_BACKTRACE 20
42 #endif
43 
44 #if defined(L4_CXX_EXCEPTION_BACKTRACE)
45 #include <l4/util/backtrace.h>
46 #endif
47 
49 namespace L4
50 {
51 
65  {
66 #if defined(L4_CXX_EXCEPTION_BACKTRACE)
67  private:
68  void *_pc_array[L4_CXX_EXCEPTION_BACKTRACE];
69  int _frame_cnt;
70 
71  protected:
75 #if defined(__PIC__)
76  Exception_tracer() throw() : _frame_cnt(0) {}
77 #else
78  Exception_tracer() throw() : _frame_cnt(l4util_backtrace(_pc_array, L4_CXX_EXCEPTION_BACKTRACE)) {}
79 #endif
80 
81  public:
85  void const *const *pc_array() const throw() { return _pc_array; }
89  int frame_count() const throw() { return _frame_cnt; }
90 #else
91  protected:
95  Exception_tracer() throw() {}
96 
97  public:
101  void const *const *pc_array() const throw() { return 0; }
105  int frame_count() const throw() { return 0; }
106 #endif
107  };
108 
118  {
119  protected:
121  Base_exception() throw() {}
122 
123  public:
127  virtual char const *str() const throw () = 0;
128 
130  virtual ~Base_exception() throw () {}
131  };
132 
141  {
142  private:
143  long _errno;
144  char _extra[80];
145 
146  public:
147  explicit Runtime_error(long err_no, char const *extra = 0) throw ()
148  : _errno(err_no)
149  {
150  if (!extra)
151  _extra[0] = 0;
152  else
153  {
154  for (unsigned i = 0; i < sizeof(_extra) && extra[i]; ++i)
155  _extra[i] = extra[i];
156  _extra[sizeof(_extra) - 1] = 0;
157  }
158  }
159  char const *str() const throw ()
160  { return l4sys_errtostr(_errno); }
161  char const *extra_str() const { return _extra; }
162  ~Runtime_error() throw () {}
163 
164  long err_no() const throw() { return _errno; }
165  };
166 
172  {
173  public:
175  explicit Out_of_memory(char const *extra = "") throw()
176  : Runtime_error(-L4_ENOMEM, extra) {}
178  ~Out_of_memory() throw() {}
179  };
180 
181 
187  {
188  public:
189  explicit Element_already_exists(char const *e = "") throw()
190  : Runtime_error(-L4_EEXIST, e) {}
191  ~Element_already_exists() throw() {}
192  };
193 
203  {
204  public:
205  Unknown_error() throw() {}
206  char const *str() const throw() { return "unknown error"; }
207  ~Unknown_error() throw() {}
208  };
209 
210 
216  {
217  public:
218  explicit Element_not_found(char const *e = "") throw()
219  : Runtime_error(-L4_ENOENT, e) {}
220  };
221 
230  {
231  private:
232  Cap<void> const _o;
233 
234  public:
239  explicit Invalid_capability(Cap<void> const &o) throw() : _o(o) {}
240  template< typename T>
241  explicit Invalid_capability(Cap<T> const &o) throw() : _o(o.cap()) {}
242  char const *str() const throw() { return "invalid object"; }
243 
248  Cap<void> const &cap() const throw() { return _o; }
249  ~Invalid_capability() throw() {}
250  };
251 
258  class Com_error : public Runtime_error
259  {
260  public:
265  explicit Com_error(long err) throw() : Runtime_error(err) {}
266 
267  ~Com_error() throw() {}
268  };
269 
274  {
275  public:
276  explicit Bounds_error(char const *e = "") throw()
277  : Runtime_error(-L4_ERANGE, e) {}
278  ~Bounds_error() throw() {}
279  };
281 };
282 
283 inline
284 L4::BasicOStream &
285 operator << (L4::BasicOStream &o, L4::Base_exception const &e)
286 {
287  o << "Exception: " << e.str() << ", backtrace ...\n";
288  for (int i = 0; i < e.frame_count(); ++i)
289  o << L4::n_hex(l4_addr_t(e.pc_array()[i])) << '\n';
290 
291  return o;
292 }
293 
294 inline
295 L4::BasicOStream &
296 operator << (L4::BasicOStream &o, L4::Runtime_error const &e)
297 {
298  o << "Exception: " << e.str() << ": ";
299  if (e.extra_str())
300  o << e.extra_str() << ": ";
301  o << "backtrace ...\n";
302  for (int i = 0; i < e.frame_count(); ++i)
303  o << L4::n_hex(l4_addr_t(e.pc_array()[i])) << '\n';
304 
305  return o;
306 }
307 
L4Re - L4 Runtime Environment