Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Modifying the API so as to prevent a use of the context that would make valgrind...
[simgrid.git] / include / xbt / error.h
1 /* $Id$ */
2
3 /* xbt/error.h - Error tracking support                                     */
4
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
6
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. */
9
10 #ifndef XBT_ERROR_H
11 #define XBT_ERROR_H
12
13 #include <stdio.h> /* FIXME: Get rid of it */
14
15 #include "xbt/misc.h" /* BEGIN_DECL */
16 #include "xbt/log.h"
17
18 #ifdef HAVE_EXECINFO_H
19 #include <execinfo.h>  /* to print the backtrace */
20 #endif
21
22 BEGIN_DECL
23
24 void xbt_abort(void) _XBT_GNUC_NORETURN;
25   
26 typedef enum {
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 */
33   unknown_error=6,
34      
35   /* remote errors: result of a RMI/RPC.
36      no_error(=0) is the same for both */   
37   remote_mismatch_error=129,
38   remote_system_error,
39   remote_network_error,
40   remote_timeout_error,
41   remote_thread_error,
42   remote_unknown_error
43 } xbt_error_t;
44
45 /*@observer@*/ const char *xbt_error_name(xbt_error_t errcode);
46
47 #define TRY(a) do {                                     \
48   if ((errcode=a) != no_error) {                        \
49      fprintf (stderr, "%s:%d: '%s' error raising...\n", \
50              __FILE__,__LINE__,                         \
51              xbt_error_name(errcode));                 \
52      return errcode;                                    \
53   } } while (0)
54    
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",               \
59              __FILE__,__LINE__,                                \
60              xbt_error_name(errcode));                        \
61      fflush(stdout);                                           \
62      xbt_abort();                                             \
63   } } while(0)
64
65 #define TRYEXPECT(action,expected_error)  do {                 \
66   errcode=action;                                              \
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));                  \
71     xbt_abort();                                              \
72   }                                                            \
73 } while(0)
74
75 /* FIXME TRYCLEAN should be avoided for readability */
76 #define TRYCLEAN(action,cleanup) do {                          \
77   if ((errcode=action) != no_error) {                          \
78     cleanup;                                                   \
79     return errcode;                                            \
80   }                                                            \
81 } while(0)
82
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); \
88   size_t _gs_i;
89
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]);           \
94   return code;                                                 \
95 } while (0)
96
97 #else /* if 0 */
98 #define _XBT_ERR_PRE do {
99 #define _XBT_ERR_POST(code)                                   \
100   return code;                                                 \
101 } while (0)
102 #endif
103
104 #define RAISE0(code,fmt) _XBT_ERR_PRE     \
105   fprintf(stderr,"%s:%d:%s: " fmt "\n",    \
106           __FILE__,__LINE__,__FUNCTION__); \
107   _XBT_ERR_POST(code)
108 #define RAISE1(code,fmt,a1) _XBT_ERR_PRE     \
109   fprintf(stderr,"%s:%d:%s: " fmt "\n",       \
110           __FILE__,__LINE__,__FUNCTION__,a1); \
111   _XBT_ERR_POST(code)
112 #define RAISE2(code,fmt,a1,a2) _XBT_ERR_PRE     \
113   fprintf(stderr,"%s:%d:%s: " fmt "\n",          \
114           __FILE__,__LINE__,__FUNCTION__,a1,a2); \
115   _XBT_ERR_POST(code)
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); \
119   _XBT_ERR_POST(code)
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); \
123   _XBT_ERR_POST(code)
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); \
127   _XBT_ERR_POST(code)
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); \
131   _XBT_ERR_POST(code)
132
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__)
136
137 #ifdef NDEBUG
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)
146 #else
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(); }
155 #endif
156
157 void xbt_die(const char *msg) _XBT_GNUC_NORETURN;
158
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))
161
162 END_DECL
163
164 #endif /* XBT_ERROR_H */
165