4 * \brief Base exceptions
5 * \ingroup l4cxx_exceptions
8 * (c) 2008-2009 Technische Universität Dresden
9 * This file is part of TUD:OS and distributed under the terms of the
10 * GNU General Public License 2.
11 * Please see the COPYING-GPL-2 file for details.
13 * As a special exception, you may use this file as part of a free software
14 * library without restriction. Specifically, if other files instantiate
15 * templates or use macros or inline functions from this file, or you compile
16 * this file and link it with other files to produce an executable, this
17 * file does not by itself cause the resulting executable to be covered by
18 * the GNU General Public License. This exception does not however
19 * invalidate any other reasons why the executable file might be covered by
20 * the GNU General Public License.
25 #include <l4/cxx/l4types.h>
26 #include <l4/cxx/basic_ostream>
27 #include <l4/sys/err.h>
28 #include <l4/sys/capability>
32 * \defgroup l4cxx_exceptions C++ Exceptions
37 #ifndef L4_CXX_NO_EXCEPTION_BACKTRACE
38 # define L4_CXX_EXCEPTION_BACKTRACE 20
41 #if defined(L4_CXX_EXCEPTION_BACKTRACE)
42 #include <l4/util/backtrace.h>
50 * \addtogroup l4cxx_exceptions
54 * \brief Back-trace support for exceptions.
55 * \headerfile l4/cxx/exceptions
57 * This class holds an array of at most #L4_CXX_EXCEPTION_BACKTRACE
58 * instruction pointers containing the call trace at the instant when an
59 * exception was thrown.
61 class Exception_tracer
63 #if defined(L4_CXX_EXCEPTION_BACKTRACE)
65 void *_pc_array[L4_CXX_EXCEPTION_BACKTRACE];
70 * \brief Create a back trace.
73 Exception_tracer() throw() : _frame_cnt(0) {}
75 Exception_tracer() throw() : _frame_cnt(l4util_backtrace(_pc_array, L4_CXX_EXCEPTION_BACKTRACE)) {}
80 * \brief Get the array containing the call trace.
82 void const *const *pc_array() const throw() { return _pc_array; }
84 * \brief Get the number of entries that are valid in the call trace.
86 int frame_count() const throw() { return _frame_cnt; }
90 * \brief Create a back trace.
92 Exception_tracer() throw() {}
96 * \brief Get the array containing the call trace.
98 void const *const *pc_array() const throw() { return 0; }
100 * \brief Get the number of entries that are valid in the call trace.
102 int frame_count() const throw() { return 0; }
107 * \brief Base class for all exceptions, thrown by the L4Re framework.
108 * \headerfile l4/cxx/exceptions
110 * This is the abstract base of all exceptions thrown within the
111 * L4Re framework. It is basically also a good idea to use it as base of
112 * all user defined exceptions.
114 class Base_exception : public Exception_tracer
117 /// Create a base exception.
118 Base_exception() throw() {}
122 * \brief Should return a human readable string for the exception.
124 virtual char const *str() const throw () = 0;
127 virtual ~Base_exception() throw () {}
131 * \brief Exception for an abstract runtime error.
132 * \headerfile l4/cxx/exceptions
134 * This is the base class for a set of exceptions that cover all errors
135 * that have a C error value (see #l4_error_code_t).
137 class Runtime_error : public Base_exception
144 explicit Runtime_error(long err_no, char const *extra = 0) throw ()
147 for (unsigned i = 0; i < sizeof(_extra) && extra[i]; ++i)
148 _extra[i] = extra[i];
149 _extra[sizeof(_extra) - 1] = 0;
151 char const *str() const throw ()
152 { return l4sys_errtostr(_errno); }
153 char const *extra_str() const { return _extra; }
154 ~Runtime_error() throw () {}
156 long err_no() const throw() { return _errno; }
160 * \brief Exception signalling insufficient memory.
161 * \headerfile l4/cxx/exceptions
163 class Out_of_memory : public Runtime_error
166 /// Create an out-of-memory exception.
167 explicit Out_of_memory(char const *extra = "") throw()
168 : Runtime_error(-L4_ENOMEM, extra) {}
170 ~Out_of_memory() throw() {}
175 * \brief Exception for duplicate element insertions.
176 * \headerfile l4/cxx/exceptions
178 class Element_already_exists : public Runtime_error
181 explicit Element_already_exists(char const *e = "") throw()
182 : Runtime_error(-L4_EEXIST, e) {}
183 ~Element_already_exists() throw() {}
187 * \brief Exception for an unknown condition.
188 * \headerfile l4/cxx/exceptions
190 * This error is usually used when a server returns an unknown return state
191 * to the client, this may indicate incompatible messages used by the client
194 class Unknown_error : public Base_exception
197 Unknown_error() throw() {}
198 char const *str() const throw() { return "unknown error"; }
199 ~Unknown_error() throw() {}
204 * \brief Exception for a failed lookup (element not found).
205 * \headerfile l4/cxx/exceptions
207 class Element_not_found : public Runtime_error
210 explicit Element_not_found(char const *e = "") throw()
211 : Runtime_error(-L4_ENOENT, e) {}
215 * \brief Indicates that an invalid object was invoked.
216 * \headerfile l4/cxx/exceptions
218 * An Object is invalid if it has L4_INVALID_ID as server L4 UID,
219 * or if the server does not know the object ID.
221 class Invalid_capability : public Base_exception
228 * \brief Create an Invalid_obejct exception for the Object o.
229 * \param o The object that caused the server side error.
231 explicit Invalid_capability(Cap<void> const &o) throw() : _o(o) {}
232 template< typename T>
233 explicit Invalid_capability(Cap<T> const &o) throw() : _o(o.cap()) {}
234 char const *str() const throw() { return "invalid object"; }
237 * \brief Get the object that caused the error.
238 * \return The object that caused the error on invocation.
240 Cap<void> const &cap() const throw() { return _o; }
241 ~Invalid_capability() throw() {}
245 * \brief Error conditions during IPC.
246 * \headerfile l4/cxx/exceptions
248 * This exception encapsulates all IPC error conditions of L4 IPC.
250 class Com_error : public Runtime_error
254 * \brief Create a Com_error for the givel L4 IPC error code.
255 * \param err The L4 IPC error code (l4_ipc... return value).
257 explicit Com_error(long err) throw() : Runtime_error(err) {}
259 ~Com_error() throw() {}
263 * \brief Access out of bounds.
265 class Bounds_error : public Runtime_error
268 explicit Bounds_error(char const *e = "") throw()
269 : Runtime_error(-L4_ERANGE, e) {}
270 ~Bounds_error() throw() {}
277 operator << (L4::BasicOStream &o, L4::Base_exception const &e)
279 o << "Exception: " << e.str() << ", backtrace ...\n";
280 for (int i = 0; i < e.frame_count(); ++i)
281 o << L4::n_hex(l4_addr_t(e.pc_array()[i])) << '\n';
288 operator << (L4::BasicOStream &o, L4::Runtime_error const &e)
290 o << "Exception: " << e.str() << ": ";
292 o << e.extra_str() << ": ";
293 o << "backtrace ...\n";
294 for (int i = 0; i < e.frame_count(); ++i)
295 o << L4::n_hex(l4_addr_t(e.pc_array()[i])) << '\n';