Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Improve detection of remote exceptions, plus workaround a bug somewhere (backtrace...
[simgrid.git] / src / xbt / ex.c
1 /*
2 **  OSSP ex - Exception Handling (modified to fit into SimGrid)
3 **  Copyright (c) 2005 Martin Quinson
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 **
8 **  This file is part of OSSP ex, an exception handling library
9 **  which can be found at http://www.ossp.org/pkg/lib/ex/.
10 **
11 **  Permission to use, copy, modify, and distribute this software for
12 **  any purpose with or without fee is hereby granted, provided that
13 **  the above copyright notice and this permission notice appear in all
14 **  copies.
15 **
16 **  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
17 **  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 **  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 **  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
20 **  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 **  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 **  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 **  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 **  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 **  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 **  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 **  SUCH DAMAGE.
28 **
29 **  ex.c: exception handling (compiler part)
30 */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #include "portable.h" /* execinfo when available */
36 #include "xbt/ex.h"
37
38 #include "gras/Virtu/virtu_interface.h" /* gras_os_myname */
39
40 /* default __ex_ctx callback function */
41 ex_ctx_t *__xbt_ex_ctx_default(void) {
42     static ex_ctx_t ctx = XBT_CTX_INITIALIZER;
43
44     return &ctx;
45 }
46
47 /** @brief shows an exception content and the associated stack if available */
48 void xbt_ex_display(xbt_ex_t *e)  {
49
50   fprintf(stderr,
51           "** SimGrid: UNCAUGHT EXCEPTION on %s: category: %s; value: %d\n"
52           "** %s\n"
53           "** Thrown by %s%s%s at %s:%d:%s\n",
54           gras_os_myname(),
55           xbt_ex_catname(e->category), e->value, e->msg,
56           e->procname, (e->host?"@":""),(e->host?e->host:""),//localhost"),
57           e->file,e->line,e->func);
58
59 #ifdef HAVE_EXECINFO_H
60  {
61   int i;
62
63   fprintf(stderr,"** Backtrace:\n");
64   if (!e->bt_strings)
65     e->bt_strings = backtrace_symbols (e->bt, e->used);
66
67   for (i = 0; i < e->used; i++)
68      printf ("   %s\n", e->bt_strings[i]);
69  }
70 #endif
71 }
72
73
74 /* default __ex_terminate callback function */
75 void __xbt_ex_terminate_default(xbt_ex_t *e)  {
76   xbt_ex_display(e);
77
78   abort();
79 }
80
81 /* the externally visible API */
82 ex_ctx_cb_t  __xbt_ex_ctx       = &__xbt_ex_ctx_default;
83 ex_term_cb_t __xbt_ex_terminate = &__xbt_ex_terminate_default;
84
85 void xbt_ex_free(xbt_ex_t e) {
86   int i;
87
88   if (e.msg) free(e.msg);
89   free(e.procname);
90   if (e.remote) {
91     free(e.file);
92     free(e.func);
93     if (e.bt_strings) /* FIXME: should never be NULL... */
94       for (i=0; i<e.used; i++) 
95         free(e.bt_strings[i]);
96     free(e.bt_strings);
97     e.bt_strings=NULL;
98     free(e.host);
99   }
100   /* locally, only one chunk of memory is allocated by the libc */
101   if (e.bt_strings)
102     free(e.bt_strings);
103
104 }
105
106 /** \brief returns a short name for the given exception category */
107 const char * xbt_ex_catname(xbt_errcat_t cat) {
108   switch (cat) {
109   case unknown_error:   return  "unknown_err";
110   case arg_error:       return "invalid_arg";
111   case mismatch_error:  return "mismatch";
112   case not_found_error: return "not found";
113   case system_error:    return "system_err";
114   case network_error:   return "network_err";
115   case timeout_error:   return "timeout";
116   case thread_error:    return "thread_err";
117   default:              return "INVALID_ERR";
118   }
119 }
120
121 #ifndef HAVE_EXECINFO_H
122 /* dummy implementation. We won't use the result, but ex.h needs it to be defined */
123 int backtrace (void **__array, int __size) {
124   return 0;
125 }
126
127 #endif
128
129 #ifdef SIMGRID_TEST
130 #include "xbt/ex.h"
131
132 XBT_TEST_SUITE("xbt_ex","Exception Handling");
133
134 XBT_TEST_UNIT("controlflow",test_controlflow, "basic nested control flow") {
135     xbt_ex_t ex;
136     volatile int n=1;
137
138     xbt_test_add0("basic nested control flow");
139
140     TRY {
141         if (n != 1)
142             xbt_test_fail1("M1: n=%d (!= 1)", n);
143         n++;
144         TRY {
145             if (n != 2)
146                 xbt_test_fail1("M2: n=%d (!= 2)", n);
147             n++;
148             THROW0(unknown_error,0,"something");
149         } CATCH (ex) {
150             if (n != 3)
151                 xbt_test_fail1("M3: n=%d (!= 1)", n);
152             n++;
153             RETHROW;
154         }
155         xbt_test_fail1("MX: n=%d (shouldn't reach this point)", n);
156     }
157     CATCH(ex) {
158         if (n != 4)
159             xbt_test_fail1("M4: n=%d (!= 4)", n);
160         n++;
161         xbt_ex_free(ex);
162     }
163     if (n != 5)
164         xbt_test_fail1("M5: n=%d (!= 5)", n);
165 }
166
167 XBT_TEST_UNIT("value",test_value,"exception value passing") {
168     xbt_ex_t ex;
169
170     TRY {
171         THROW0(unknown_error, 2, "toto");
172     } CATCH(ex) {
173         xbt_test_add0("exception value passing");
174         if (ex.category != unknown_error)
175             xbt_test_fail1("category=%d (!= 1)", ex.category);
176         if (ex.value != 2)
177             xbt_test_fail1("value=%d (!= 2)", ex.value);
178         if (strcmp(ex.msg,"toto"))
179             xbt_test_fail1("message=%s (!= toto)", ex.msg);
180         xbt_ex_free(ex);
181     }
182 }
183
184 XBT_TEST_UNIT("variables",test_variables,"variable value preservation") {
185     xbt_ex_t ex;
186     int r1, r2;
187     volatile int v1, v2;
188
189     r1 = r2 = v1 = v2 = 1234;
190     TRY {
191         r2 = 5678;
192         v2 = 5678;
193         THROW0(unknown_error, 0, "toto");
194     } CATCH(ex) {
195         xbt_test_add0("variable preservation");
196         if (r1 != 1234)
197             xbt_test_fail1("r1=%d (!= 1234)", r1);
198         if (v1 != 1234)
199             xbt_test_fail1("v1=%d (!= 1234)", v1);
200         /* r2 is allowed to be destroyed because not volatile */
201         if (v2 != 5678)
202             xbt_test_fail1("v2=%d (!= 5678)", v2);
203         xbt_ex_free(ex);
204     }
205 }
206
207 XBT_TEST_UNIT("cleanup",test_cleanup,"cleanup handling") {
208     xbt_ex_t ex;
209     volatile int v1;
210     int c;
211
212     xbt_test_add0("cleanup handling");
213
214     v1 = 1234;
215     c = 0;
216     TRY {
217         v1 = 5678;
218         THROW0(1, 2, "blah");
219     } CLEANUP {
220         if (v1 != 5678)
221             xbt_test_fail1("v1 = %d (!= 5678)", v1);
222         c = 1;
223     } CATCH(ex) {
224         if (v1 != 5678)
225             xbt_test_fail1("v1 = %d (!= 5678)", v1);
226         if (!(ex.category == 1 && ex.value == 2 && !strcmp(ex.msg,"blah")))
227             xbt_test_fail0("unexpected exception contents");
228         xbt_ex_free(ex);
229     }
230     if (!c)
231         xbt_test_fail0("xbt_ex_free not executed");
232 }
233
234
235 /*
236  * The following is the example included in the documentation. It's a good 
237  * idea to check its syntax even if we don't try to run it.
238  * And actually, it allows to put comments in the code despite doxygen.
239  */ 
240 static char *mallocex(int size) {
241   return NULL;
242 }
243 #define SMALLAMOUNT 10
244 #define TOOBIG 100000000
245
246 #if 0 /* this contains syntax errors, actually */
247 static void bad_example(void) {
248   struct {char*first;} *globalcontext;
249   ex_t ex;
250
251   /* BAD_EXAMPLE */
252   TRY {
253     char *cp1, *cp2, *cp3;
254     
255     cp1 = mallocex(SMALLAMOUNT);
256     globalcontext->first = cp1;
257     cp2 = mallocex(TOOBIG);
258     cp3 = mallocex(SMALLAMOUNT);
259     strcpy(cp1, "foo");
260     strcpy(cp2, "bar");
261   } CLEANUP {
262     if (cp3 != NULL) free(cp3);
263     if (cp2 != NULL) free(cp2);
264     if (cp1 != NULL) free(cp1);
265   } CATCH(ex) {
266     printf("cp3=%s", cp3);
267     RETHROW;
268   }
269   /* end_of_bad_example */
270 }
271 #endif
272
273 static void good_example(void) {
274   struct {char*first;} *globalcontext;
275   xbt_ex_t ex;
276
277   /* GOOD_EXAMPLE */
278   { /*01*/
279     char * volatile /*03*/ cp1 = NULL /*02*/;
280     char * volatile /*03*/ cp2 = NULL /*02*/;
281     char * volatile /*03*/ cp3 = NULL /*02*/;
282     TRY {
283       cp1 = mallocex(SMALLAMOUNT);
284       globalcontext->first = cp1;
285       cp1 = NULL /*05 give away*/;
286       cp2 = mallocex(TOOBIG);
287       cp3 = mallocex(SMALLAMOUNT);
288       strcpy(cp1, "foo");
289       strcpy(cp2, "bar");
290     } CLEANUP { /*04*/
291       printf("cp3=%s", cp3 == NULL /*02*/ ? "" : cp3);
292       if (cp3 != NULL)
293         free(cp3);
294       if (cp2 != NULL)
295         free(cp2);
296       /*05 cp1 was given away */
297     } CATCH(ex) {
298       /*05 global context untouched */
299       RETHROW;
300     }
301   }
302   /* end_of_good_example */
303 }
304 #endif /* SIMGRID_TEST */