1 /* ex - Exception Handling */
3 /* Copyright (c) 2005-2015. The SimGrid Team.
4 * All rights reserved. */
6 /* Copyright (c) 2002-2004 Ralf S. Engelschall <rse@engelschall.com> */
7 /* Copyright (c) 2002-2004 The OSSP Project <http://www.ossp.org/> */
8 /* Copyright (c) 2002-2004 Cable & Wireless <http://www.cw.com/> */
9 /* All rights reserved. */
11 /* This code is inspirated from the OSSP version (as retrieved back in 2004)*/
12 /* It was heavily modified to fit the SimGrid framework. */
14 /* The OSSP version has the following copyright notice:
15 ** OSSP ex - Exception Handling
16 ** Copyright (c) 2002-2004 Ralf S. Engelschall <rse@engelschall.com>
17 ** Copyright (c) 2002-2004 The OSSP Project <http://www.ossp.org/>
18 ** Copyright (c) 2002-2004 Cable & Wireless <http://www.cw.com/>
20 ** This file is part of OSSP ex, an exception handling library
21 ** which can be found at http://www.ossp.org/pkg/lib/ex/.
23 ** Permission to use, copy, modify, and distribute this software for
24 ** any purpose with or without fee is hereby granted, provided that
25 ** the above copyright notice and this permission notice appear in all
28 ** THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESSED OR IMPLIED
29 ** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30 ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 ** IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
32 ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
35 ** USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36 ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
37 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
38 ** OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 /* The extensions made for the SimGrid project can either be distributed */
43 /* under the same license, or under the LGPL v2.1 */
57 #include "xbt/sysdep.h"
59 #include "xbt/virtu.h"
61 /*-*-* Emergency debuging: define this when the exceptions get crazy *-*-*/
66 # define MAYDAY_SAVE(m) printf("%d %s:%d save %p\n", \
67 xbt_getpid(), __FILE__, __LINE__, \
70 # define MAYDAY_RESTORE(m) printf("%d %s:%d restore %p\n", \
71 xbt_getpid(), __FILE__, __LINE__, \
74 # define MAYDAY_CATCH(e) printf("%d %s:%d Catched '%s'\n", \
75 xbt_getpid(), __FILE__, __LINE__, \
79 # define MAYDAY_SAVE(m)
80 # define MAYDAY_RESTORE(m)
81 # define MAYDAY_CATCH(e)
83 /*-*-* end of debugging stuff *-*-*/
85 /** @brief different kind of errors */
87 unknown_error = 0, /**< unknown error */
88 arg_error, /**< Invalid argument */
89 bound_error, /**< Out of bounds argument */
90 mismatch_error, /**< The provided ID does not match */
91 not_found_error, /**< The searched element was not found */
92 system_error, /**< a syscall did fail */
93 network_error, /**< error while sending/receiving data */
94 timeout_error, /**< not quick enough, dude */
95 cancel_error, /**< an action was canceled */
96 thread_error, /**< error while [un]locking */
97 host_error, /**< host failed */
98 tracing_error, /**< error during the simulation tracing */
99 io_error, /**< disk or file error */
100 vm_error /**< vm error */
104 XBT_PUBLIC_CLASS xbt_ex : public std::runtime_error {
106 xbt_ex() : std::runtime_error("") {}
107 xbt_ex(const char* message) : std::runtime_error(message) {}
110 xbt_errcat_t category; /**< category like HTTP (what went wrong) */
111 int value; /**< like errno (why did it went wrong) */
113 std::string procname; /**< Name of the process who thrown this */
114 int pid; /**< PID of the process who thrown this */
115 const char *file; /**< Thrown point */
116 int line; /**< Thrown point */
117 const char *func; /**< Thrown point */
119 std::vector<std::string> bt_strings;
120 std::vector<void*> bt;
126 XBT_PUBLIC(const char *) xbt_ex_catname(xbt_errcat_t cat);
128 typedef struct xbt_ex xbt_ex_t;
130 XBT_PUBLIC(void) xbt_throw(char* message, xbt_errcat_t errcat, int value, const char* file, int line, const char* func) XBT_ATTRIB_NORETURN;
132 /** @brief Builds and throws an exception
134 #define THROW(c, v) { xbt_throw(NULL, (xbt_errcat_t) c, v, __FILE__, __LINE__, __func__); }
136 /** @brief Builds and throws an exception with a printf-like formatted message
138 #define THROWF(c, v, ...) xbt_throw(bprintf(__VA_ARGS__), (xbt_errcat_t) c, v, __FILE__, __LINE__, __func__)
140 #define THROW_IMPOSSIBLE \
141 THROWF(unknown_error, 0, "The Impossible Did Happen (yet again)")
142 #define THROW_UNIMPLEMENTED \
143 THROWF(unknown_error, 0, "Function %s unimplemented",__func__)
144 #define THROW_DEADCODE \
145 THROWF(unknown_error, 0, "Function %s was supposed to be DEADCODE, but it's not",__func__)
147 #define DIE_IMPOSSIBLE xbt_die("The Impossible Did Happen (yet again)")
149 /** @brief The display made by an exception that is not catched */
150 XBT_PUBLIC(void) xbt_ex_display(xbt_ex_t * e);
152 /** @brief Shows a backtrace of the current location */
153 XBT_PUBLIC(void) xbt_backtrace_display_current(void);
154 /** @brief reimplementation of glibc backtrace based directly on gcc library, without implicit malloc */
155 XBT_PUBLIC(int) xbt_backtrace_no_malloc(void**bt, int size);
156 /** @brief Captures a backtrace for further use */
157 XBT_PUBLIC(void) xbt_backtrace_current(xbt_ex_t * e);
158 /** @brief Display a previously captured backtrace */
159 XBT_PUBLIC(void) xbt_backtrace_display(xbt_ex_t * e);
160 /** @brief Get current backtrace with libunwind */
161 XBT_PUBLIC(int) xbt_libunwind_backtrace(void *bt[XBT_BACKTRACE_SIZE], int size);
166 #endif /* __XBT_EX_H__ */