Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / include / xbt / ex.h
1 /* ex - Exception Handling                                                  */
2
3 /* Copyright (c) 2005-2015. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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.                                                    */
10
11 /* This code is inspirated from the OSSP version (as retrieved back in 2004)*/
12 /* It was heavily modified to fit the SimGrid framework.                    */
13
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/>
19 **
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/.
22 **
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
26 **  copies.
27 **
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
39 **  SUCH DAMAGE.
40  */
41
42 /* The extensions made for the SimGrid project can either be distributed    */
43 /* under the same license, or under the LGPL v2.1                           */
44
45 #ifndef __XBT_EX_H__
46 #define __XBT_EX_H__
47
48 #include <stdlib.h>
49
50 #ifdef __cplusplus
51 #include <string>
52 #include <vector>
53 #include <stdexcept>
54 #endif
55
56 #include "xbt/base.h"
57 #include "xbt/sysdep.h"
58 #include "xbt/misc.h"
59 #include "xbt/virtu.h"
60
61 /*-*-* Emergency debuging: define this when the exceptions get crazy *-*-*/
62 #undef __EX_MAYDAY
63 #ifdef __EX_MAYDAY
64 # include <stdio.h>
65 #include <errno.h>
66 #  define MAYDAY_SAVE(m)    printf("%d %s:%d save %p\n",                \
67                                    xbt_getpid(), __FILE__, __LINE__,    \
68                                    (m)->jb                              \
69                                   ),
70 #  define MAYDAY_RESTORE(m) printf("%d %s:%d restore %p\n",             \
71                                    xbt_getpid(), __FILE__, __LINE__,    \
72                                    (m)->jb                              \
73                                   ),
74 #  define MAYDAY_CATCH(e)   printf("%d %s:%d Catched '%s'\n",           \
75                                    xbt_getpid(), __FILE__, __LINE__,    \
76                                    (e).msg                              \
77           ),
78 #else
79 #  define MAYDAY_SAVE(m)
80 #  define MAYDAY_RESTORE(m)
81 #  define MAYDAY_CATCH(e)
82 #endif
83 /*-*-* end of debugging stuff *-*-*/
84
85 /** @brief different kind of errors */
86 typedef enum {
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 */
101 } xbt_errcat_t;
102
103 #ifdef __cplusplus
104 XBT_PUBLIC_CLASS xbt_ex : public std::runtime_error {
105 public:
106   xbt_ex() : std::runtime_error("") {}
107   xbt_ex(const char* message) : std::runtime_error(message) {}
108   ~xbt_ex() override;
109
110   xbt_errcat_t category;        /**< category like HTTP (what went wrong) */
111   int value;                    /**< like errno (why did it went wrong) */
112   /* throw point */
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 */
118   /* Backtrace */
119   std::vector<std::string>      bt_strings;
120   std::vector<void*>            bt;
121 };
122 #endif
123
124 SG_BEGIN_DECL()
125
126 XBT_PUBLIC(const char *) xbt_ex_catname(xbt_errcat_t cat);
127
128 typedef struct xbt_ex xbt_ex_t;
129
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;
131
132 /** @brief Builds and throws an exception
133     @hideinitializer */
134 #define THROW(c, v)             { xbt_throw(NULL, (xbt_errcat_t) c, v, __FILE__, __LINE__, __func__); }
135
136 /** @brief Builds and throws an exception with a printf-like formatted message
137     @hideinitializer */
138 #define THROWF(c, v, ...)       xbt_throw(bprintf(__VA_ARGS__), (xbt_errcat_t) c, v, __FILE__, __LINE__, __func__)
139
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__)
146
147 #define DIE_IMPOSSIBLE xbt_die("The Impossible Did Happen (yet again)")
148
149 /** @brief The display made by an exception that is not catched */
150 XBT_PUBLIC(void) xbt_ex_display(xbt_ex_t * e);
151
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);
162
163 SG_END_DECL()
164
165 /** @} */
166 #endif                          /* __XBT_EX_H__ */