Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a442b01d37b78e7c4db8e31dac4d56d867fe15d5
[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     for (i=0; i<e.used; i++) 
94       free(e.bt_strings[i]);
95     free(e.bt_strings);
96     e.bt_strings=NULL;
97     free(e.host);
98   }
99   /* locally, only one chunk of memory is allocated by the libc */
100   if (e.bt_strings)
101     free(e.bt_strings);
102
103 }
104
105 /** \brief returns a short name for the given exception category */
106 const char * xbt_ex_catname(xbt_errcat_t cat) {
107   switch (cat) {
108   case unknown_error:   return  "unknown_err";
109   case arg_error:       return "invalid_arg";
110   case mismatch_error:  return "mismatch";
111   case not_found_error: return "not found";
112   case system_error:    return "system_err";
113   case network_error:   return "network_err";
114   case timeout_error:   return "timeout";
115   case thread_error:    return "thread_err";
116   default:              return "INVALID_ERR";
117   }
118 }
119
120 #ifndef HAVE_EXECINFO_H
121 /* dummy implementation. We won't use the result, but ex.h needs it to be defined */
122 int backtrace (void **__array, int __size) {
123   return 0;
124 }
125
126 #endif
127
128 #ifdef SIMGRID_TEST
129 #include "xbt/ex.h"
130
131 XBT_TEST_SUITE("xbt_ex","Exception Handling");
132
133 XBT_TEST_UNIT("controlflow",test_controlflow, "basic nested control flow") {
134     xbt_ex_t ex;
135     volatile int n=1;
136
137     xbt_test_add0("basic nested control flow");
138
139     TRY {
140         if (n != 1)
141             xbt_test_fail1("M1: n=%d (!= 1)", n);
142         n++;
143         TRY {
144             if (n != 2)
145                 xbt_test_fail1("M2: n=%d (!= 2)", n);
146             n++;
147             THROW0(unknown_error,0,"something");
148         } CATCH (ex) {
149             if (n != 3)
150                 xbt_test_fail1("M3: n=%d (!= 1)", n);
151             n++;
152             RETHROW;
153         }
154         xbt_test_fail1("MX: n=%d (shouldn't reach this point)", n);
155     }
156     CATCH(ex) {
157         if (n != 4)
158             xbt_test_fail1("M4: n=%d (!= 4)", n);
159         n++;
160         xbt_ex_free(ex);
161     }
162     if (n != 5)
163         xbt_test_fail1("M5: n=%d (!= 5)", n);
164 }
165
166 XBT_TEST_UNIT("value",test_value,"exception value passing") {
167     xbt_ex_t ex;
168
169     TRY {
170         THROW0(unknown_error, 2, "toto");
171     } CATCH(ex) {
172         xbt_test_add0("exception value passing");
173         if (ex.category != unknown_error)
174             xbt_test_fail1("category=%d (!= 1)", ex.category);
175         if (ex.value != 2)
176             xbt_test_fail1("value=%d (!= 2)", ex.value);
177         if (strcmp(ex.msg,"toto"))
178             xbt_test_fail1("message=%s (!= toto)", ex.msg);
179         xbt_ex_free(ex);
180     }
181 }
182
183 XBT_TEST_UNIT("variables",test_variables,"variable value preservation") {
184     xbt_ex_t ex;
185     int r1, r2;
186     volatile int v1, v2;
187
188     r1 = r2 = v1 = v2 = 1234;
189     TRY {
190         r2 = 5678;
191         v2 = 5678;
192         THROW0(unknown_error, 0, "toto");
193     } CATCH(ex) {
194         xbt_test_add0("variable preservation");
195         if (r1 != 1234)
196             xbt_test_fail1("r1=%d (!= 1234)", r1);
197         if (v1 != 1234)
198             xbt_test_fail1("v1=%d (!= 1234)", v1);
199         /* r2 is allowed to be destroyed because not volatile */
200         if (v2 != 5678)
201             xbt_test_fail1("v2=%d (!= 5678)", v2);
202         xbt_ex_free(ex);
203     }
204 }
205
206 XBT_TEST_UNIT("cleanup",test_cleanup,"cleanup handling") {
207     xbt_ex_t ex;
208     volatile int v1;
209     int c;
210
211     xbt_test_add0("cleanup handling");
212
213     v1 = 1234;
214     c = 0;
215     TRY {
216         v1 = 5678;
217         THROW0(1, 2, "blah");
218     } CLEANUP {
219         if (v1 != 5678)
220             xbt_test_fail1("v1 = %d (!= 5678)", v1);
221         c = 1;
222     } CATCH(ex) {
223         if (v1 != 5678)
224             xbt_test_fail1("v1 = %d (!= 5678)", v1);
225         if (!(ex.category == 1 && ex.value == 2 && !strcmp(ex.msg,"blah")))
226             xbt_test_fail0("unexpected exception contents");
227         xbt_ex_free(ex);
228     }
229     if (!c)
230         xbt_test_fail0("xbt_ex_free not executed");
231 }
232
233
234 /*
235  * The following is the example included in the documentation. It's a good 
236  * idea to check its syntax even if we don't try to run it.
237  * And actually, it allows to put comments in the code despite doxygen.
238  */ 
239 static char *mallocex(int size) {
240   return NULL;
241 }
242 #define SMALLAMOUNT 10
243 #define TOOBIG 100000000
244
245 #if 0 /* this contains syntax errors, actually */
246 static void bad_example(void) {
247   struct {char*first;} *globalcontext;
248   ex_t ex;
249
250   /* BAD_EXAMPLE */
251   TRY {
252     char *cp1, *cp2, *cp3;
253     
254     cp1 = mallocex(SMALLAMOUNT);
255     globalcontext->first = cp1;
256     cp2 = mallocex(TOOBIG);
257     cp3 = mallocex(SMALLAMOUNT);
258     strcpy(cp1, "foo");
259     strcpy(cp2, "bar");
260   } CLEANUP {
261     if (cp3 != NULL) free(cp3);
262     if (cp2 != NULL) free(cp2);
263     if (cp1 != NULL) free(cp1);
264   } CATCH(ex) {
265     printf("cp3=%s", cp3);
266     RETHROW;
267   }
268   /* end_of_bad_example */
269 }
270 #endif
271
272 static void good_example(void) {
273   struct {char*first;} *globalcontext;
274   xbt_ex_t ex;
275
276   /* GOOD_EXAMPLE */
277   { /*01*/
278     char * volatile /*03*/ cp1 = NULL /*02*/;
279     char * volatile /*03*/ cp2 = NULL /*02*/;
280     char * volatile /*03*/ cp3 = NULL /*02*/;
281     TRY {
282       cp1 = mallocex(SMALLAMOUNT);
283       globalcontext->first = cp1;
284       cp1 = NULL /*05 give away*/;
285       cp2 = mallocex(TOOBIG);
286       cp3 = mallocex(SMALLAMOUNT);
287       strcpy(cp1, "foo");
288       strcpy(cp2, "bar");
289     } CLEANUP { /*04*/
290       printf("cp3=%s", cp3 == NULL /*02*/ ? "" : cp3);
291       if (cp3 != NULL)
292         free(cp3);
293       if (cp2 != NULL)
294         free(cp2);
295       /*05 cp1 was given away */
296     } CATCH(ex) {
297       /*05 global context untouched */
298       RETHROW;
299     }
300   }
301   /* end_of_good_example */
302 }
303 #endif /* SIMGRID_TEST */