Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
updating the doc
[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 /**\brief Error types
27    \ingroup XBT_error
28 */
29 typedef enum {
30   no_error=0,       /**< succes */
31   mismatch_error=1, /**< The provided ID does not match */
32   system_error=2,   /**< a syscall did fail */
33   network_error=3,  /**< error while sending/receiving data */
34   timeout_error=4,  /**< not quick enough, dude */
35   thread_error=5,   /**< error while [un]locking */
36   unknown_error=6,  /**< unknown error */
37      
38   /* remote errors: result of a RMI/RPC.
39      no_error(=0) is the same for both */   
40   remote_mismatch_error=129,
41   remote_system_error,
42   remote_network_error,
43   remote_timeout_error,
44   remote_thread_error,
45   remote_unknown_error
46 } xbt_error_t;
47
48 const char *xbt_error_name(xbt_error_t errcode);
49
50 #define TRY(a) do {                                     \
51   if ((errcode=a) != no_error) {                        \
52      fprintf (stderr, "%s:%d: '%s' error raising...\n", \
53              __FILE__,__LINE__,                         \
54              xbt_error_name(errcode));                 \
55      return errcode;                                    \
56   } } while (0)
57    
58 #define TRYCATCH(a,b) if ((errcode=a) != no_error && errcode !=b) return errcode
59 #define TRYFAIL(a) do {                                        \
60   if ((errcode=a) != no_error) {                               \
61      fprintf(stderr,"%s:%d: Got '%s' error !\n",               \
62              __FILE__,__LINE__,                                \
63              xbt_error_name(errcode));                        \
64      fflush(stdout);                                           \
65      xbt_abort();                                             \
66   } } while(0)
67
68 #define TRYEXPECT(action,expected_error)  do {                 \
69   errcode=action;                                              \
70   if (errcode != expected_error) {                             \
71     fprintf(stderr,"Got error %s (instead of %s expected)\n",  \
72             xbt_error_name(errcode),                          \
73             xbt_error_name(expected_error));                  \
74     xbt_abort();                                              \
75   }                                                            \
76 } while(0)
77
78 /* FIXME TRYCLEAN should be avoided for readability */
79 #define TRYCLEAN(action,cleanup) do {                          \
80   if ((errcode=action) != no_error) {                          \
81     cleanup;                                                   \
82     return errcode;                                            \
83   }                                                            \
84 } while(0)
85
86 #if 0 /* FIXME: We don't use backtrace. Drop it? */
87 #define _XBT_ERR_PRE do {                                     \
88  void *_gs_array[30];                                          \
89   size_t _gs_size= backtrace (_gs_array, 30);                  \
90   char **_gs_strings= backtrace_symbols (_gs_array, _gs_size); \
91   size_t _gs_i;
92
93 #define _XBT_ERR_POST(code)                                   \
94   fprintf(stderr,"Backtrace follows\n");                       \
95   for (_gs_i = 0; _gs_i < _gs_size; _gs_i++)                   \
96      fprintf (stderr,"   %s\n", _gs_strings[_gs_i]);           \
97   return code;                                                 \
98 } while (0)
99
100 #else /* if 0 */
101 #define _XBT_ERR_PRE do {
102 #define _XBT_ERR_POST(code)                                   \
103   return code;                                                 \
104 } while (0)
105 #endif
106
107 #define RAISE0(code,fmt) _XBT_ERR_PRE     \
108   fprintf(stderr,"%s:%d:%s: " fmt "\n",    \
109           __FILE__,__LINE__,__FUNCTION__); \
110   _XBT_ERR_POST(code)
111 #define RAISE1(code,fmt,a1) _XBT_ERR_PRE     \
112   fprintf(stderr,"%s:%d:%s: " fmt "\n",       \
113           __FILE__,__LINE__,__FUNCTION__,a1); \
114   _XBT_ERR_POST(code)
115 #define RAISE2(code,fmt,a1,a2) _XBT_ERR_PRE     \
116   fprintf(stderr,"%s:%d:%s: " fmt "\n",          \
117           __FILE__,__LINE__,__FUNCTION__,a1,a2); \
118   _XBT_ERR_POST(code)
119 #define RAISE3(code,fmt,a1,a2,a3) _XBT_ERR_PRE     \
120   fprintf(stderr,"%s:%d:%s: " fmt "\n",             \
121           __FILE__,__LINE__,__FUNCTION__,a1,a2,a3); \
122   _XBT_ERR_POST(code)
123 #define RAISE4(code,fmt,a1,a2,a3,a4) _XBT_ERR_PRE     \
124   fprintf(stderr,"%s:%d:%s: " fmt "\n",                \
125           __FILE__,__LINE__,__FUNCTION__,a1,a2,a3,a4); \
126   _XBT_ERR_POST(code)
127 #define RAISE5(code,fmt,a1,a2,a3,a4,a5) _XBT_ERR_PRE     \
128   fprintf(stderr,"%s:%d:%s: " fmt "\n",                   \
129           __FILE__,__LINE__,__FUNCTION__,a1,a2,a3,a4,a5); \
130   _XBT_ERR_POST(code)
131 #define RAISE6(code,fmt,a1,a2,a3,a4,a5,a6) _XBT_ERR_PRE     \
132   fprintf(stderr,"%s:%d:%s: " fmt "\n",                      \
133           __FILE__,__LINE__,__FUNCTION__,a1,a2,a3,a4,a5,a6); \
134   _XBT_ERR_POST(code)
135
136 /* #define RAISE_MALLOC     RAISE0(malloc_error,"Malloc error") */
137 #define RAISE_IMPOSSIBLE RAISE0(unknown_error,"The Impossible did happen")
138 #define RAISE_UNIMPLEMENTED RAISE1(unknown_error,"Function %s unimplemented",__FUNCTION__)
139
140 /** 
141  * \name Asserts
142  * \ingroup XBT_error
143  * asserts and stuff
144  */
145 /*@{*/
146 #ifdef NDEBUG
147 #define xbt_assert(cond)
148 #define xbt_assert0(cond,msg)
149 #define xbt_assert1(cond,msg,a)
150 #define xbt_assert2(cond,msg,a,b)
151 #define xbt_assert3(cond,msg,a,b,c)
152 #define xbt_assert4(cond,msg,a,b,c,d)
153 #define xbt_assert5(cond,msg,a,b,c,d,e)
154 #define xbt_assert6(cond,msg,a,b,c,d,e,f)
155 #else
156 #define xbt_assert(cond)                  if (!(cond)) { CRITICAL1("Assertion %s failed", #cond); xbt_abort(); }
157 #define xbt_assert0(cond,msg)             if (!(cond)) { CRITICAL0(msg); xbt_abort(); }
158 #define xbt_assert1(cond,msg,a)           if (!(cond)) { CRITICAL1(msg,a); xbt_abort(); }
159 #define xbt_assert2(cond,msg,a,b)         if (!(cond)) { CRITICAL2(msg,a,b); xbt_abort(); }
160 #define xbt_assert3(cond,msg,a,b,c)       if (!(cond)) { CRITICAL3(msg,a,b,c); xbt_abort(); }
161 #define xbt_assert4(cond,msg,a,b,c,d)     if (!(cond)) { CRITICAL4(msg,a,b,c,d); xbt_abort(); }
162 #define xbt_assert5(cond,msg,a,b,c,d,e)   if (!(cond)) { CRITICAL5(msg,a,b,c,d,e); xbt_abort(); }
163 #define xbt_assert6(cond,msg,a,b,c,d,e,f) if (!(cond)) { CRITICAL6(msg,a,b,c,d,e,f); xbt_abort(); }
164 #endif
165 /*@}*/
166
167 /** 
168  * \brief How to abort with an error message
169  * \ingroup XBT_error
170  */
171 void xbt_die(const char *msg) _XBT_GNUC_NORETURN;
172
173 #define DIE_IMPOSSIBLE xbt_assert0(0,"The Impossible did happen (yet again)")
174 #define xbt_assert_error(a) xbt_assert1(errcode == (a), "Error %s unexpected",xbt_error_name(errcode))
175
176 END_DECL()
177
178 #endif /* XBT_ERROR_H */