Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Readd the example we need in the documentation, they used to live in testsuite/xbt...
[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 /* default __ex_ctx callback function */
39 ex_ctx_t *__xbt_ex_ctx_default(void) {
40     static ex_ctx_t ctx = XBT_CTX_INITIALIZER;
41
42     return &ctx;
43 }
44
45 /** @brief shows an exception content and the associated stack if available */
46 void xbt_ex_display(xbt_ex_t *e)  {
47
48   fprintf(stderr,
49           "** SimGrid: UNCAUGHT EXCEPTION: category: %s; value: %d\n"
50           "** %s\n"
51           "** Thrown by %s%s%s at %s:%d:%s\n",
52           xbt_ex_catname(e->category), e->value, e->msg,
53           e->procname, (e->host?"@":""),(e->host?e->host:"localhost"),
54           e->file,e->line,e->func);
55
56 #ifdef HAVE_EXECINFO_H
57  {
58   char **strings;
59   int i;
60
61   fprintf(stderr,"** Backtrace:\n");
62   strings = backtrace_symbols (e->bt, e->used);
63
64   for (i = 0; i < e->used; i++)
65      printf ("   %s\n", strings[i]);
66
67   free (strings);
68  }
69 #endif
70 }
71
72
73 /* default __ex_terminate callback function */
74 void __xbt_ex_terminate_default(xbt_ex_t *e)  {
75   xbt_ex_display(e);
76
77   abort();
78 }
79
80 /* the externally visible API */
81 ex_ctx_cb_t  __xbt_ex_ctx       = &__xbt_ex_ctx_default;
82 ex_term_cb_t __xbt_ex_terminate = &__xbt_ex_terminate_default;
83
84 void xbt_ex_free(xbt_ex_t e) {
85   if (e.msg) free(e.msg);
86   free(e.procname);
87 }
88
89 /** \brief returns a short name for the given exception category */
90 const char * xbt_ex_catname(xbt_errcat_t cat) {
91   switch (cat) {
92   case unknown_error:   return  "unknown_err";
93   case arg_error:       return "invalid_arg";
94   case mismatch_error:  return "mismatch";
95   case not_found_error: return "not found";
96   case system_error:    return "system_err";
97   case network_error:   return "network_err";
98   case timeout_error:   return "timeout";
99   case thread_error:    return "thread_err";
100   default:              return "INVALID_ERR";
101   }
102 }
103
104 #ifndef HAVE_EXECINFO_H
105 /* dummy implementation. We won't use the result, but ex.h needs it to be defined */
106 int backtrace (void **__array, int __size) {
107   return 0;
108 }
109
110 #endif
111
112 #ifdef SIMGRID_TEST
113 #include "xbt/ex.h"
114
115 XBT_TEST_SUITE("xbt_ex","Exception Handling");
116
117 XBT_TEST_UNIT("controlflow",test_controlflow, "basic nested control flow") {
118     xbt_ex_t ex;
119     volatile int n=1;
120
121     xbt_test_add0("basic nested control flow");
122
123     TRY {
124         if (n != 1)
125             xbt_test_fail1("M1: n=%d (!= 1)", n);
126         n++;
127         TRY {
128             if (n != 2)
129                 xbt_test_fail1("M2: n=%d (!= 2)", n);
130             n++;
131             THROW0(unknown_error,0,"something");
132         } CATCH (ex) {
133             if (n != 3)
134                 xbt_test_fail1("M3: n=%d (!= 1)", n);
135             n++;
136             RETHROW;
137         }
138         xbt_test_fail1("MX: n=%d (shouldn't reach this point)", n);
139     }
140     CATCH(ex) {
141         if (n != 4)
142             xbt_test_fail1("M4: n=%d (!= 4)", n);
143         n++;
144         xbt_ex_free(ex);
145     }
146     if (n != 5)
147         xbt_test_fail1("M5: n=%d (!= 5)", n);
148 }
149
150 XBT_TEST_UNIT("value",test_value,"exception value passing") {
151     xbt_ex_t ex;
152
153     TRY {
154         THROW0(unknown_error, 2, "toto");
155     } CATCH(ex) {
156         xbt_test_add0("exception value passing");
157         if (ex.category != unknown_error)
158             xbt_test_fail1("category=%d (!= 1)", ex.category);
159         if (ex.value != 2)
160             xbt_test_fail1("value=%d (!= 2)", ex.value);
161         if (strcmp(ex.msg,"toto"))
162             xbt_test_fail1("message=%s (!= toto)", ex.msg);
163         xbt_ex_free(ex);
164     }
165 }
166
167 XBT_TEST_UNIT("variables",test_variables,"variable value preservation") {
168     xbt_ex_t ex;
169     int r1, r2;
170     volatile int v1, v2;
171
172     r1 = r2 = v1 = v2 = 1234;
173     TRY {
174         r2 = 5678;
175         v2 = 5678;
176         THROW0(unknown_error, 0, "toto");
177     } CATCH(ex) {
178         xbt_test_add0("variable preservation");
179         if (r1 != 1234)
180             xbt_test_fail1("r1=%d (!= 1234)", r1);
181         if (v1 != 1234)
182             xbt_test_fail1("v1=%d (!= 1234)", v1);
183         /* r2 is allowed to be destroyed because not volatile */
184         if (v2 != 5678)
185             xbt_test_fail1("v2=%d (!= 5678)", v2);
186         xbt_ex_free(ex);
187     }
188 }
189
190 XBT_TEST_UNIT("cleanup",test_cleanup,"cleanup handling") {
191     xbt_ex_t ex;
192     volatile int v1;
193     int c;
194
195     xbt_test_add0("cleanup handling");
196
197     v1 = 1234;
198     c = 0;
199     TRY {
200         v1 = 5678;
201         THROW0(1, 2, "blah");
202     } CLEANUP {
203         if (v1 != 5678)
204             xbt_test_fail1("v1 = %d (!= 5678)", v1);
205         c = 1;
206     } CATCH(ex) {
207         if (v1 != 5678)
208             xbt_test_fail1("v1 = %d (!= 5678)", v1);
209         if (!(ex.category == 1 && ex.value == 2 && !strcmp(ex.msg,"blah")))
210             xbt_test_fail0("unexpected exception contents");
211         xbt_ex_free(ex);
212     }
213     if (!c)
214         xbt_test_fail0("xbt_ex_free not executed");
215 }
216
217
218 /*
219  * The following is the example included in the documentation. It's a good 
220  * idea to check its syntax even if we don't try to run it.
221  * And actually, it allows to put comments in the code despite doxygen.
222  */ 
223 static char *mallocex(int size) {
224   return NULL;
225 }
226 #define SMALLAMOUNT 10
227 #define TOOBIG 100000000
228
229 #if 0 /* this contains syntax errors, actually */
230 static void bad_example(void) {
231   struct {char*first;} *globalcontext;
232   ex_t ex;
233
234   /* BAD_EXAMPLE */
235   TRY {
236     char *cp1, *cp2, *cp3;
237     
238     cp1 = mallocex(SMALLAMOUNT);
239     globalcontext->first = cp1;
240     cp2 = mallocex(TOOBIG);
241     cp3 = mallocex(SMALLAMOUNT);
242     strcpy(cp1, "foo");
243     strcpy(cp2, "bar");
244   } CLEANUP {
245     if (cp3 != NULL) free(cp3);
246     if (cp2 != NULL) free(cp2);
247     if (cp1 != NULL) free(cp1);
248   } CATCH(ex) {
249     printf("cp3=%s", cp3);
250     RETHROW;
251   }
252   /* end_of_bad_example */
253 }
254 #endif
255
256 static void good_example(void) {
257   struct {char*first;} *globalcontext;
258   xbt_ex_t ex;
259
260   /* GOOD_EXAMPLE */
261   { /*01*/
262     char * volatile /*03*/ cp1 = NULL /*02*/;
263     char * volatile /*03*/ cp2 = NULL /*02*/;
264     char * volatile /*03*/ cp3 = NULL /*02*/;
265     TRY {
266       cp1 = mallocex(SMALLAMOUNT);
267       globalcontext->first = cp1;
268       cp1 = NULL /*05 give away*/;
269       cp2 = mallocex(TOOBIG);
270       cp3 = mallocex(SMALLAMOUNT);
271       strcpy(cp1, "foo");
272       strcpy(cp2, "bar");
273     } CLEANUP { /*04*/
274       printf("cp3=%s", cp3 == NULL /*02*/ ? "" : cp3);
275       if (cp3 != NULL)
276         free(cp3);
277       if (cp2 != NULL)
278         free(cp2);
279       /*05 cp1 was given away */
280     } CATCH(ex) {
281       /*05 global context untouched */
282       RETHROW;
283     }
284   }
285   /* end_of_good_example */
286 }
287 #endif /* SIMGRID_TEST */