3 /* xbt/error.h - Error tracking support */
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
13 #include <stdio.h> /* FIXME: Get rid of it */
15 #include "xbt/misc.h" /* BEGIN_DECL */
18 #ifdef HAVE_EXECINFO_H
19 #include <execinfo.h> /* to print the backtrace */
24 void xbt_abort(void) _XBT_GNUC_NORETURN;
27 no_error=0, /* succes */
28 mismatch_error=1, /* The provided ID does not match */
29 system_error=2, /* a syscall did fail */
30 network_error=3, /* error while sending/receiving data */
31 timeout_error=4, /* not quick enough, dude */
32 thread_error=5, /* error while [un]locking */
35 /* remote errors: result of a RMI/RPC.
36 no_error(=0) is the same for both */
37 remote_mismatch_error=129,
45 /*@observer@*/ const char *xbt_error_name(xbt_error_t errcode);
48 if ((errcode=a) != no_error) { \
49 fprintf (stderr, "%s:%d: '%s' error raising...\n", \
51 xbt_error_name(errcode)); \
55 #define TRYCATCH(a,b) if ((errcode=a) != no_error && errcode !=b) return errcode
56 #define TRYFAIL(a) do { \
57 if ((errcode=a) != no_error) { \
58 fprintf(stderr,"%s:%d: Got '%s' error !\n", \
60 xbt_error_name(errcode)); \
65 #define TRYEXPECT(action,expected_error) do { \
67 if (errcode != expected_error) { \
68 fprintf(stderr,"Got error %s (instead of %s expected)\n", \
69 xbt_error_name(errcode), \
70 xbt_error_name(expected_error)); \
75 /* FIXME TRYCLEAN should be avoided for readability */
76 #define TRYCLEAN(action,cleanup) do { \
77 if ((errcode=action) != no_error) { \
83 #if 0 /* FIXME: We don't use backtrace. Drop it? */
84 #define _XBT_ERR_PRE do { \
85 void *_gs_array[30]; \
86 size_t _gs_size= backtrace (_gs_array, 30); \
87 char **_gs_strings= backtrace_symbols (_gs_array, _gs_size); \
90 #define _XBT_ERR_POST(code) \
91 fprintf(stderr,"Backtrace follows\n"); \
92 for (_gs_i = 0; _gs_i < _gs_size; _gs_i++) \
93 fprintf (stderr," %s\n", _gs_strings[_gs_i]); \
98 #define _XBT_ERR_PRE do {
99 #define _XBT_ERR_POST(code) \
104 #define RAISE0(code,fmt) _XBT_ERR_PRE \
105 fprintf(stderr,"%s:%d:%s: " fmt "\n", \
106 __FILE__,__LINE__,__FUNCTION__); \
108 #define RAISE1(code,fmt,a1) _XBT_ERR_PRE \
109 fprintf(stderr,"%s:%d:%s: " fmt "\n", \
110 __FILE__,__LINE__,__FUNCTION__,a1); \
112 #define RAISE2(code,fmt,a1,a2) _XBT_ERR_PRE \
113 fprintf(stderr,"%s:%d:%s: " fmt "\n", \
114 __FILE__,__LINE__,__FUNCTION__,a1,a2); \
116 #define RAISE3(code,fmt,a1,a2,a3) _XBT_ERR_PRE \
117 fprintf(stderr,"%s:%d:%s: " fmt "\n", \
118 __FILE__,__LINE__,__FUNCTION__,a1,a2,a3); \
120 #define RAISE4(code,fmt,a1,a2,a3,a4) _XBT_ERR_PRE \
121 fprintf(stderr,"%s:%d:%s: " fmt "\n", \
122 __FILE__,__LINE__,__FUNCTION__,a1,a2,a3,a4); \
124 #define RAISE5(code,fmt,a1,a2,a3,a4,a5) _XBT_ERR_PRE \
125 fprintf(stderr,"%s:%d:%s: " fmt "\n", \
126 __FILE__,__LINE__,__FUNCTION__,a1,a2,a3,a4,a5); \
128 #define RAISE6(code,fmt,a1,a2,a3,a4,a5,a6) _XBT_ERR_PRE \
129 fprintf(stderr,"%s:%d:%s: " fmt "\n", \
130 __FILE__,__LINE__,__FUNCTION__,a1,a2,a3,a4,a5,a6); \
133 /* #define RAISE_MALLOC RAISE0(malloc_error,"Malloc error") */
134 #define RAISE_IMPOSSIBLE RAISE0(unknown_error,"The Impossible did happen")
135 #define RAISE_UNIMPLEMENTED RAISE1(unknown_error,"Function %s unimplemented",__FUNCTION__)
138 #define xbt_assert(cond)
139 #define xbt_assert0(cond,msg)
140 #define xbt_assert1(cond,msg,a)
141 #define xbt_assert2(cond,msg,a,b)
142 #define xbt_assert3(cond,msg,a,b,c)
143 #define xbt_assert4(cond,msg,a,b,c,d)
144 #define xbt_assert5(cond,msg,a,b,c,d,e)
145 #define xbt_assert6(cond,msg,a,b,c,d,e,f)
147 #define xbt_assert(cond) if (!(cond)) { CRITICAL1("Assertion %s failed", #cond); xbt_abort(); }
148 #define xbt_assert0(cond,msg) if (!(cond)) { CRITICAL0(msg); xbt_abort(); }
149 #define xbt_assert1(cond,msg,a) if (!(cond)) { CRITICAL1(msg,a); xbt_abort(); }
150 #define xbt_assert2(cond,msg,a,b) if (!(cond)) { CRITICAL2(msg,a,b); xbt_abort(); }
151 #define xbt_assert3(cond,msg,a,b,c) if (!(cond)) { CRITICAL3(msg,a,b,c); xbt_abort(); }
152 #define xbt_assert4(cond,msg,a,b,c,d) if (!(cond)) { CRITICAL4(msg,a,b,c,d); xbt_abort(); }
153 #define xbt_assert5(cond,msg,a,b,c,d,e) if (!(cond)) { CRITICAL5(msg,a,b,c,d,e); xbt_abort(); }
154 #define xbt_assert6(cond,msg,a,b,c,d,e,f) if (!(cond)) { CRITICAL6(msg,a,b,c,d,e,f); xbt_abort(); }
157 void xbt_die(const char *msg) _XBT_GNUC_NORETURN;
159 #define DIE_IMPOSSIBLE xbt_assert0(0,"The Impossible did happen (yet again)")
160 #define xbt_assert_error(a) xbt_assert1(errcode == (a), "Error %s unexpected",xbt_error_name(errcode))
164 #endif /* XBT_ERROR_H */