Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
50a4ef737dbf01f07c073cbf447cf6c88ad23ba4
[simgrid.git] / include / xbt / ex.h
1 /* ex - Exception Handling                                                  */
2
3 /*  Copyright (c) 2005-2010 The SimGrid team                                */
4 /*  Copyright (c) 2002-2004 Ralf S. Engelschall <rse@engelschall.com>       */
5 /*  Copyright (c) 2002-2004 The OSSP Project <http://www.ossp.org/>         */
6 /*  Copyright (c) 2002-2004 Cable & Wireless <http://www.cw.com/>           */
7 /*  All rights reserved.                                                    */
8
9 /* This code is inspirated from the OSSP version (as retrieved back in 2004)*/
10 /* It was heavily modified to fit the SimGrid framework.                    */
11
12 /* The OSSP version has the following copyright notice:
13 **  OSSP ex - Exception Handling
14 **  Copyright (c) 2002-2004 Ralf S. Engelschall <rse@engelschall.com>
15 **  Copyright (c) 2002-2004 The OSSP Project <http://www.ossp.org/>
16 **  Copyright (c) 2002-2004 Cable & Wireless <http://www.cw.com/>
17 **
18 **  This file is part of OSSP ex, an exception handling library
19 **  which can be found at http://www.ossp.org/pkg/lib/ex/.
20 **
21 **  Permission to use, copy, modify, and distribute this software for
22 **  any purpose with or without fee is hereby granted, provided that
23 **  the above copyright notice and this permission notice appear in all
24 **  copies.
25 **
26 **  THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESSED OR IMPLIED
27 **  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28 **  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 **  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
30 **  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 **  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 **  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
33 **  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34 **  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35 **  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
36 **  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 **  SUCH DAMAGE.
38  */
39
40 /* The extensions made for the SimGrid project can either be distributed    */
41 /* under the same license, or under the LGPL v2.1                           */
42
43 #ifndef __XBT_EX_H__
44 #define __XBT_EX_H__
45
46 #include "xbt/sysdep.h"
47 #include "xbt/misc.h"
48 #include "xbt/virtu.h"
49
50 SG_BEGIN_DECL()
51
52 /*-*-* Emergency debuging: define this when the exceptions get crazy *-*-*/
53 #undef __EX_MAYDAY
54 #ifdef __EX_MAYDAY
55 # include <stdio.h>
56 #include <errno.h>
57 #  define MAYDAY_SAVE(m)    printf("%d %s:%d save %p\n",                \
58                                    (*xbt_getpid)(),__FILE__,__LINE__,  \
59                                    (m)->jb                              \
60                                   ),
61 #  define MAYDAY_RESTORE(m) printf("%d %s:%d restore %p\n",             \
62                                    (*xbt_getpid)(),__FILE__,__LINE__,  \
63                                    (m)->jb                              \
64                                   ),
65 #  define MAYDAY_CATCH(e)   printf("%d %s:%d Catched '%s'\n",           \
66                                    (*xbt_getpid)(),__FILE__,__LINE__,  \
67                                    e.msg                                \
68                                   ),
69 #else
70 #  define MAYDAY_SAVE(m)
71 #  define MAYDAY_RESTORE(m)
72 #  define MAYDAY_CATCH(e)
73 #endif
74 /*-*-* end of debugging stuff *-*-*/
75 #if defined(__EX_MCTX_MCSC__)
76 #include <ucontext.h>           /* POSIX.1 ucontext(3) */
77 #define __ex_mctx_struct         ucontext_t uc;
78 #define __ex_mctx_save(mctx)     (getcontext(&(mctx)->uc) == 0)
79 #define __ex_mctx_restored(mctx)        /* noop */
80 #define __ex_mctx_restore(mctx)  (void)setcontext(&(mctx)->uc)
81 #elif defined(__EX_MCTX_SSJLJ__)
82 #include <setjmp.h>             /* POSIX.1 sigjmp_buf(3) */
83 #define __ex_mctx_struct         sigjmp_buf jb;
84 #define __ex_mctx_save(mctx)     (sigsetjmp((mctx)->jb, 1) == 0)
85 #define __ex_mctx_restored(mctx)        /* noop */
86 #define __ex_mctx_restore(mctx)  (void)siglongjmp((mctx)->jb, 1)
87 #elif defined(__EX_MCTX_SJLJ__) || !defined(__EX_MCTX_CUSTOM__) || defined(__EX_MAYDAY)
88 #include <setjmp.h>             /* ISO-C jmp_buf(3) */
89 #define __ex_mctx_struct         jmp_buf jb;
90 #define __ex_mctx_save(mctx)     ( MAYDAY_SAVE(mctx) setjmp((mctx)->jb) == 0)
91 #define __ex_mctx_restored(mctx)        /* noop */
92 #define __ex_mctx_restore(mctx)  ( MAYDAY_RESTORE(mctx) (void)longjmp((mctx)->jb, 1))
93 #endif
94 /* declare the machine context type */
95 typedef struct {
96 __ex_mctx_struct} __ex_mctx_t;
97
98 /** @addtogroup XBT_ex
99  *  @brief A set of macros providing exception a la C++ in ANSI C (grounding feature)
100  *
101  * This module is a small ISO-C++ style exception handling library
102  * for use in the ISO-C language. It allows you to use the paradigm 
103  * of throwing and catching exceptions in order to reduce the amount
104  * of error handling code without hindering program robustness.
105  *               
106  * This is achieved by directly transferring exceptional return codes
107  * (and the program control flow) from the location where the exception
108  * is raised (throw point) to the location where it is handled (catch
109  * point) -- usually from a deeply nested sub-routine to a parent 
110  * routine. All intermediate routines no longer have to make sure that 
111  * the exceptional return codes from sub-routines are correctly passed 
112  * back to the parent.
113  *
114  * These features are brought to you by a modified version of the libex 
115  * library, one of the numerous masterpiece of Ralf S. Engelschall.
116  *
117  * \htmlonly <div class="toc">\endhtmlonly
118  *
119  * @section XBT_ex_toc TABLE OF CONTENTS
120  *
121  *  - \ref XBT_ex_intro
122  *  - \ref XBT_ex_base
123  *  - \ref XBT_ex_pitfalls
124  *
125  * \htmlonly </div> \endhtmlonly
126  *
127  * @section XBT_ex_intro DESCRIPTION
128  * 
129  * In SimGrid, an exception is a triple <\a msg , \a category , \a value> 
130  * where \a msg is a human-readable text describing the exceptional 
131  * condition, \a code an integer describing what went wrong and \a value
132  * providing a sort of sub-category. (this is different in the original libex).
133  *
134  * @section XBT_ex_base BASIC USAGE
135  *
136  * \em TRY \b TRIED_BLOCK [\em TRY_CLEANUP \b CLEANUP_BLOCK] \em CATCH (variable) \b CATCH_BLOCK
137  *
138  * This is the primary syntactical construct provided. It is modeled after the
139  * ISO-C++ try-catch clause and should sound familiar to most of you.
140  *
141  * Any exception thrown directly from the TRIED_BLOCK block or from called
142  * subroutines is caught. Cleanups which must be done after this block
143  * (whenever an exception arised or not) should be placed into the optionnal
144  * CLEANUP_BLOCK. The code dealing with the exceptions when they arise should
145  * be placed into the (mandatory) CATCH_BLOCK.
146  *
147  * 
148  * In absence of exception, the control flow goes into the blocks TRIED_BLOCK
149  * and CLEANUP_BLOCK (if present); The CATCH_BLOCK block is then ignored.
150  *
151  * When an exception is thrown, the control flow goes through the following
152  * blocks: TRIED_BLOCK (up to the statement throwing the exception),
153  * CLEANUP_BLOCK (if any) and CATCH_BLOCK. The exception is stored in a
154  * variable for inspection inside the CATCH_BLOCK. This variable must be
155  * declared in the outter scope, but its value is only valid within the
156  * CATCH_BLOCK block. 
157  *
158  * Some notes:
159  *  - TRY, CLEANUP and CATCH cannot be used separately, they work
160  *    only in combination and form a language clause as a whole.
161  *  - In contrast to the syntax of other languages (such as C++ or Jave) there
162  *    is only one CATCH block and not multiple ones (all exceptions are
163  *    of the same \em xbt_ex_t C type). 
164  *  - the variable of CATCH can naturally be reused in subsequent 
165  *    CATCH clauses.
166  *  - it is possible to nest TRY clauses.
167  *
168  * The TRY block is a regular ISO-C language statement block, but
169  * 
170  * <center><b>it is not
171  * allowed to jump into it via "goto" or longjmp(3) or out of it via "break",
172  * "return", "goto" or longjmp(3)</b>.</center>
173  *
174  * This is because there is some hidden setup and
175  * cleanup that needs to be done regardless of whether an exception is
176  * caught. Bypassing these steps will break the exception handling facility.
177  * The symptom are likely to be a segfault at the next exception raising point,  
178  * ie far away from the point where you did the mistake. If you suspect
179  * that kind of error in your code, have a look at the little script
180  * <tt>tools/xbt_exception_checker</tt> in the CVS. It extracts all the TRY
181  * blocks from a set of C files you give it and display them (and only
182  * them) on the standard output. You can then grep for the forbidden 
183  * keywords on that output.
184  *   
185  * The CLEANUP and CATCH blocks are regular ISO-C language statement
186  * blocks without any restrictions. You are even allowed to throw (and, in the
187  * CATCH block, to re-throw) exceptions.
188  *
189  * There is one subtle detail you should remember about TRY blocks:
190  * Variables used in the CLEANUP or CATCH clauses must be declared with
191  * the storage class "volatile", otherwise they might contain outdated
192  * information if an exception is thrown.
193  *
194  *
195  * This is because you usually do not know which commands in the TRY
196  * were already successful before the exception was thrown (logically speaking)
197  * and because the underlying ISO-C setjmp(3) facility applies those
198  * restrictions (technically speaking). As a matter of fact, value changes
199  * between the TRY and the THROW may be discarded if you forget the
200  * "volatile" keyword. 
201  * 
202  * \section XBT_ex_pitfalls PROGRAMMING PITFALLS 
203  *
204  * Exception handling is a very elegant and efficient way of dealing with
205  * exceptional situation. Nevertheless it requires additional discipline in
206  * programming and there are a few pitfalls one must be aware of. Look the
207  * following code which shows some pitfalls and contains many errors (assuming
208  * a mallocex() function which throws an exception if malloc(3) fails):
209  *
210  * \dontinclude ex.c
211  * \skip BAD_EXAMPLE
212  * \until end_of_bad_example
213  *
214  * This example raises a few issues:
215  *  -# \b variable \b scope \n
216  *     Variables which are used in the CLEANUP or CATCH clauses must be
217  *     declared before the TRY clause, otherwise they only exist inside the
218  *     TRY block. In the example above, cp1, cp2 and cp3 only exist in the
219  *     TRY block and are invisible from the CLEANUP and CATCH
220  *     blocks.
221  *  -# \b variable \b initialization \n
222  *     Variables which are used in the CLEANUP or CATCH clauses must
223  *     be initialized before the point of the first possible THROW is
224  *     reached. In the example above, CLEANUP would have trouble using cp3
225  *     if mallocex() throws a exception when allocating a TOOBIG buffer.
226  *  -# \b volatile \b variable \n
227  *     Variables which are used in the CLEANUP or CATCH clauses MUST BE
228  *     DECLARED AS "volatile", otherwise they might contain outdated
229  *     information when an exception is thrown. 
230  *  -# \b clean \b before \b catch \n
231  *     The CLEANUP clause is not only place before the CATCH clause in
232  *     the source code, it also occures before in the control flow. So,
233  *     resources being cleaned up cannot be used in the CATCH block. In the
234  *     example, c3 gets freed before the printf placed in CATCH.
235  *  -# \b variable \b uninitialization \n
236  *     If resources are passed out of the scope of the
237  *     TRY/CLEANUP/CATCH construct, they naturally shouldn't get
238  *     cleaned up. The example above does free(3) cp1 in CLEANUP although
239  *     its value was affected to globalcontext->first, invalidating this
240  *     pointer.
241
242  * The following is fixed version of the code (annotated with the pitfall items
243  * for reference): 
244  *
245  * \skip GOOD_EXAMPLE
246  * \until end_of_good_example
247  *
248  * @{
249  */
250
251 /** @brief different kind of errors */
252 typedef enum {
253   unknown_error = 0,/**< unknown error */
254   arg_error,        /**< Invalid argument */
255   bound_error,      /**< Out of bounds argument */
256   mismatch_error,   /**< The provided ID does not match */
257   not_found_error,  /**< The searched element was not found */
258
259   system_error,   /**< a syscall did fail */
260   network_error,  /**< error while sending/receiving data */
261   timeout_error,  /**< not quick enough, dude */
262   cancel_error,   /**< an action was canceled */
263   thread_error,    /**< error while [un]locking */
264   host_error,                            /**< host failed */
265   tracing_error   /**< error during the simulation tracing */
266 } xbt_errcat_t;
267
268 XBT_PUBLIC(const char *) xbt_ex_catname(xbt_errcat_t cat);
269
270 /** @brief Structure describing an exception */
271 typedef struct {
272   char *msg;             /**< human readable message */
273   xbt_errcat_t category;
274                          /**< category like HTTP (what went wrong) */
275   int value;             /**< like errno (why did it went wrong) */
276   /* throw point */
277   short int remote;
278                     /**< whether it was raised remotely */
279   char *host;     /**< NULL locally thrown exceptions; full hostname if remote ones */
280   /* FIXME: host should be hostname:port[#thread] */
281   char *procname;
282                   /**< Name of the process who thrown this */
283   int pid;        /**< PID of the process who thrown this */
284   char *file;     /**< Thrown point */
285   int line;       /**< Thrown point */
286   char *func;     /**< Thrown point */
287   /* Backtrace */
288   int used;
289   char **bt_strings;            /* only filed on display (or before the network propagation) */
290   void *bt[XBT_BACKTRACE_SIZE];
291 } xbt_ex_t;
292
293 /* declare the running context type
294  * (that's where we get the process name for the logs and the exception storage)
295  *  -- do not mess with it --
296  */
297 typedef struct {
298   __ex_mctx_t *ctx_mctx;        /* permanent machine context of enclosing try/catch */
299   volatile int ctx_caught;      /* temporary flag whether exception was caught */
300   volatile xbt_ex_t exception;  /* temporary exception storage */
301 } xbt_running_ctx_t;
302
303 /* the static and dynamic initializers for a context structure */
304 #define XBT_RUNNING_CTX_INITIALIZER \
305     { NULL, 0, { /* content */ NULL, unknown_error, 0, \
306                  /* throw point*/ 0,NULL, NULL,0, NULL, 0, NULL,\
307                  /* backtrace */ 0,NULL,{NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL} } }
308 #define XBT_RUNNING_CTX_INITIALIZE(ctx) \
309     do { \
310         (ctx)->ctx_mctx          = NULL; \
311         (ctx)->ctx_caught        = 0;    \
312         (ctx)->exception.msg        = NULL; \
313         (ctx)->exception.category   = 0;    \
314         (ctx)->exception.value      = 0;    \
315         (ctx)->exception.remote     = 0;    \
316         (ctx)->exception.host       = NULL; \
317         (ctx)->exception.procname   = NULL; \
318         (ctx)->exception.pid        = 0;    \
319         (ctx)->exception.file       = NULL; \
320         (ctx)->exception.line       = 0;    \
321         (ctx)->exception.func       = NULL; \
322         (ctx)->exception.bt[0]      = NULL; \
323         (ctx)->exception.bt[1]      = NULL; \
324         (ctx)->exception.bt[2]      = NULL; \
325         (ctx)->exception.bt[3]      = NULL; \
326         (ctx)->exception.bt[4]      = NULL; \
327         (ctx)->exception.bt[5]      = NULL; \
328         (ctx)->exception.bt[6]      = NULL; \
329         (ctx)->exception.bt[7]      = NULL; \
330         (ctx)->exception.bt[8]      = NULL; \
331         (ctx)->exception.bt[9]      = NULL; \
332         (ctx)->exception.used       = 0; \
333         (ctx)->exception.bt_strings = NULL; \
334     } while (0)
335
336 /* the exception context */
337 typedef xbt_running_ctx_t *(*xbt_running_ctx_fetcher_t) (void);
338 XBT_PUBLIC_DATA(xbt_running_ctx_fetcher_t) __xbt_running_ctx_fetch;
339 extern xbt_running_ctx_t *__xbt_ex_ctx_default(void);
340
341 /* the termination handler */
342 typedef void (*ex_term_cb_t) (xbt_ex_t *);
343 XBT_PUBLIC_DATA(ex_term_cb_t) __xbt_ex_terminate;
344 extern void __xbt_ex_terminate_default(xbt_ex_t * e);
345
346 /** @brief Introduce a block where exception may be dealed with 
347  *  @hideinitializer
348  */
349 #define TRY \
350     { \
351         xbt_running_ctx_t *__xbt_ex_ctx_ptr = __xbt_running_ctx_fetch(); \
352         int __ex_cleanup = 0; \
353         __ex_mctx_t *__ex_mctx_en; \
354         __ex_mctx_t __ex_mctx_me; \
355         __ex_mctx_en = __xbt_ex_ctx_ptr->ctx_mctx; \
356         __xbt_ex_ctx_ptr->ctx_mctx = &__ex_mctx_me; \
357         if (__ex_mctx_save(&__ex_mctx_me)) { \
358             if (1)
359
360 /** @brief optional(!) block for cleanup
361  *  @hideinitializer
362  */
363 #define TRY_CLEANUP \
364             else { \
365             } \
366             __xbt_ex_ctx_ptr->ctx_caught = 0; \
367         } else { \
368             __ex_mctx_restored(&__ex_mctx_me); \
369             __xbt_ex_ctx_ptr->ctx_caught = 1; \
370         } \
371         __xbt_ex_ctx_ptr->ctx_mctx = __ex_mctx_en; \
372         __ex_cleanup = 1; \
373         if (1) { \
374             if (1)
375
376 #ifndef DOXYGEN_SKIP
377 #  ifdef __cplusplus
378 #    define XBT_EX_T_CPLUSPLUSCAST (xbt_ex_t&)
379 #  else
380 #    define XBT_EX_T_CPLUSPLUSCAST
381 #  endif
382 #endif
383
384 /** @brief the block for catching (ie, deal with) an exception 
385  *  @hideinitializer
386  */
387 #define CATCH(e) \
388             else { \
389             } \
390             if (!(__ex_cleanup)) \
391                 __xbt_ex_ctx_ptr->ctx_caught = 0; \
392         } else { \
393             if (!(__ex_cleanup)) { \
394                 __ex_mctx_restored(&__ex_mctx_me); \
395                 __xbt_ex_ctx_ptr->ctx_caught = 1; \
396             } \
397         } \
398         __xbt_ex_ctx_ptr->ctx_mctx = __ex_mctx_en; \
399     } \
400     if (   !(__xbt_running_ctx_fetch()->ctx_caught) \
401         || ((e) = XBT_EX_T_CPLUSPLUSCAST __xbt_running_ctx_fetch()->exception, MAYDAY_CATCH(e) 0)) { \
402     } \
403     else
404
405 #define DO_THROW(running_ctx) \
406      /* deal with the exception */                                                     \
407      if (running_ctx->ctx_mctx == NULL)                                                \
408        __xbt_ex_terminate((xbt_ex_t*)&(running_ctx->exception)); /* not catched */     \
409      else                                                                              \
410        __ex_mctx_restore(running_ctx->ctx_mctx); /* catched somewhere */               \
411      abort()  /* nope, stupid GCC, we won't survive a THROW (this won't be reached) */
412
413 /** @brief Helper macro for THROWS0-6
414  *  @hideinitializer
415  *
416  *  @param c: category code (integer)
417  *  @param v: value (integer)
418  *  @param m: message text
419  *
420  * If called from within a TRY/CATCH construct, this exception 
421  * is copied into the CATCH relevant variable program control flow 
422  * is derouted to the CATCH (after the optional sg_cleanup).
423  *
424  * If no TRY/CATCH construct embeds this call, the program calls
425  * abort(3). 
426  *
427  * The THROW can be performed everywhere, including inside TRY, 
428  * CLEANUP and CATCH blocks.
429  */
430
431 #define _THROW(c,v,m) \
432   do { /* change this sequence into one block */                             \
433      xbt_running_ctx_t *_throw_ctx = __xbt_running_ctx_fetch();              \
434      /* build the exception */                                               \
435      _throw_ctx->exception.msg      = (m);                                   \
436      _throw_ctx->exception.category = (xbt_errcat_t)(c);                     \
437      _throw_ctx->exception.value    = (v);                                   \
438      _throw_ctx->exception.remote   = 0;                                     \
439      _throw_ctx->exception.host     = (char*)NULL;                           \
440      _throw_ctx->exception.procname = (char*)xbt_procname();                 \
441      _throw_ctx->exception.pid      = (*xbt_getpid)();                       \
442      _throw_ctx->exception.file     = (char*)__FILE__;                       \
443      _throw_ctx->exception.line     = __LINE__;                              \
444      _throw_ctx->exception.func     = (char*)_XBT_FUNCTION;                  \
445      _throw_ctx->exception.bt_strings = NULL;                                \
446      xbt_backtrace_current( (xbt_ex_t *) &(_throw_ctx->exception) );         \
447      DO_THROW(_throw_ctx);                                                   \
448   } while (0)
449
450 /** @brief Builds and throws an exception with a string taking no arguments
451     @hideinitializer */
452 #define THROW0(c,v,m)                   _THROW(c,v,(m?bprintf(m):NULL))
453 /** @brief Builds and throws an exception with a string taking one argument
454     @hideinitializer */
455 #define THROW1(c,v,m,a1)                _THROW(c,v,bprintf(m,a1))
456 /** @brief Builds and throws an exception with a string taking two arguments
457     @hideinitializer */
458 #define THROW2(c,v,m,a1,a2)             _THROW(c,v,bprintf(m,a1,a2))
459 /** @brief Builds and throws an exception with a string taking three arguments
460     @hideinitializer */
461 #define THROW3(c,v,m,a1,a2,a3)          _THROW(c,v,bprintf(m,a1,a2,a3))
462 /** @brief Builds and throws an exception with a string taking four arguments
463     @hideinitializer */
464 #define THROW4(c,v,m,a1,a2,a3,a4)       _THROW(c,v,bprintf(m,a1,a2,a3,a4))
465 /** @brief Builds and throws an exception with a string taking five arguments
466     @hideinitializer */
467 #define THROW5(c,v,m,a1,a2,a3,a4,a5)    _THROW(c,v,bprintf(m,a1,a2,a3,a4,a5))
468 /** @brief Builds and throws an exception with a string taking six arguments
469     @hideinitializer */
470 #define THROW6(c,v,m,a1,a2,a3,a4,a5,a6) _THROW(c,v,bprintf(m,a1,a2,a3,a4,a5,a6))
471 /** @brief Builds and throws an exception with a string taking seven arguments
472     @hideinitializer */
473 #define THROW7(c,v,m,a1,a2,a3,a4,a5,a6,a7) _THROW(c,v,bprintf(m,a1,a2,a3,a4,a5,a6,a7))
474
475 #define THROW_IMPOSSIBLE     THROW0(unknown_error,0,"The Impossible Did Happen (yet again)")
476 #define THROW_UNIMPLEMENTED  THROW1(unknown_error,0,"Function %s unimplemented",_XBT_FUNCTION)
477
478 #ifndef NDEBUG
479 #  define DIE_IMPOSSIBLE       xbt_assert0(0,"The Impossible Did Happen (yet again)")
480 #else
481 #  define DIE_IMPOSSIBLE       exit(1);
482 #endif
483
484 /** @brief re-throwing of an already caught exception (ie, pass it to the upper catch block) 
485  *  @hideinitializer
486  */
487 #define RETHROW \
488   do { \
489    xbt_running_ctx_t *ctx = __xbt_running_ctx_fetch(); \
490    if (ctx->ctx_mctx == NULL) \
491      __xbt_ex_terminate((xbt_ex_t*)&(ctx->exception)); \
492    else \
493      __ex_mctx_restore(ctx->ctx_mctx); \
494    abort();\
495   } while(0)
496
497
498 #ifndef DOXYGEN_SKIP
499 #define _XBT_PRE_RETHROW \
500   do {                                                               \
501     char *_xbt_ex_internal_msg = __xbt_running_ctx_fetch()->exception.msg;         \
502     __xbt_running_ctx_fetch()->exception.msg = bprintf(
503 #define _XBT_POST_RETHROW \
504  _xbt_ex_internal_msg); \
505     free(_xbt_ex_internal_msg);                                      \
506     RETHROW;                                                         \
507   } while (0)
508 #endif
509
510 /** @brief like THROW0, but adding some details to the message of an existing exception
511  *  @hideinitializer
512  */
513 #define RETHROW0(msg)           _XBT_PRE_RETHROW msg,          _XBT_POST_RETHROW
514 /** @brief like THROW1, but adding some details to the message of an existing exception
515  *  @hideinitializer
516  */
517 #define RETHROW1(msg,a)         _XBT_PRE_RETHROW msg,a,        _XBT_POST_RETHROW
518 /** @brief like THROW2, but adding some details to the message of an existing exception
519  *  @hideinitializer
520  */
521 #define RETHROW2(msg,a,b)       _XBT_PRE_RETHROW msg,a,b,      _XBT_POST_RETHROW
522 /** @brief like THROW3, but adding some details to the message of an existing exception
523  *  @hideinitializer
524  */
525 #define RETHROW3(msg,a,b,c)     _XBT_PRE_RETHROW msg,a,b,c,    _XBT_POST_RETHROW
526 /** @brief like THROW4, but adding some details to the message of an existing exception
527  *  @hideinitializer
528  */
529 #define RETHROW4(msg,a,b,c,d)   _XBT_PRE_RETHROW msg,a,b,c,d,  _XBT_POST_RETHROW
530 /** @brief like THROW5, but adding some details to the message of an existing exception
531  *  @hideinitializer
532  */
533 #define RETHROW5(msg,a,b,c,d,e) _XBT_PRE_RETHROW msg,a,b,c,d,e, _XBT_POST_RETHROW
534
535 /** @brief Exception destructor */
536 XBT_PUBLIC(void) xbt_ex_free(xbt_ex_t e);
537
538 /** @brief Shows a backtrace of the current location */
539 XBT_PUBLIC(void) xbt_backtrace_display_current(void);
540 /** @brief Captures a backtrace for further use */
541 XBT_PUBLIC(void) xbt_backtrace_current(xbt_ex_t * e);
542 /** @brief Display a previously captured backtrace */
543 XBT_PUBLIC(void) xbt_backtrace_display(xbt_ex_t * e);
544
545 SG_END_DECL()
546
547 /** @} */
548 #endif                          /* __XBT_EX_H__ */