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/>
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/.
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
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
29 ** ex.c: exception handling (compiler part)
35 #include "portable.h" /* execinfo when available */
37 #include "xbt/module.h" /* xbt_binary_name */
38 #include "xbt/ex_interface.h"
40 #include "gras/Virtu/virtu_interface.h" /* gras_os_myname */
42 /* default __ex_ctx callback function */
43 ex_ctx_t *__xbt_ex_ctx_default(void) {
44 static ex_ctx_t ctx = XBT_CTX_INITIALIZER;
50 /** \brief show the backtrace of the current point (lovely while debuging) */
51 void xbt_display_backtrace(void) {
52 #if defined(HAVE_EXECINFO_H) && defined(HAVE_POPEN) && defined(ADDR2LINE)
56 e.used = backtrace((void**)e.bt,XBT_BACKTRACE_SIZE);
58 xbt_ex_setup_backtrace(&e);
59 for (i=0; i<e.used; i++)
60 fprintf(stderr,"%s\n",e.bt_strings[i]);
66 fprintf(stderr,"No backtrace on this arch");
70 void xbt_ex_setup_backtrace(xbt_ex_t *e) {
71 #if defined(HAVE_EXECINFO_H) && defined(HAVE_POPEN) && defined(ADDR2LINE)
73 /* to get the backtrace from the libc */
74 char **backtrace = backtrace_symbols (e->bt, e->used);
76 /* To build the commandline of addr2line */
77 char *cmd = xbt_new(char,strlen(ADDR2LINE)+strlen(xbt_binary_name)+20*e->used);
80 /* to extract the addresses from the backtrace */
81 char **addrs=xbt_new(char*,e->used);
84 /* To read the output of addr2line */
86 char line_func[1024],line_pos[1024];
88 /* build the commandline */
89 curr += sprintf(curr,"%s -f -e %s ",ADDR2LINE,xbt_binary_name);
90 for (i=0; i<e->used;i++) {
91 /* retrieve this address */
92 snprintf(buff,256,"%s",strchr(backtrace[i],'[')+1);
95 addrs[i]=bprintf("%s",buff);
97 /* Add it to the command line args */
98 curr+=sprintf(curr,"%s ",addrs[i]);
101 /* parse the output and build a new backtrace */
102 e->bt_strings = xbt_new(char*,e->used);
104 pipe = popen(cmd, "r");
105 // xbt_assert(pipe);//,"Cannot fork addr2line to display the backtrace");
106 for (i=0; i<e->used; i++) {
107 fgets(line_func,1024,pipe);
108 line_func[strlen(line_func)-1]='\0';
109 fgets(line_pos,1024,pipe);
110 line_pos[strlen(line_pos)-1]='\0';
112 if (strcmp("??",line_func)) {
113 e->bt_strings[i] = bprintf("** At %s: %s (%s)", addrs[i], line_func,line_pos);
115 char *p=bprintf("%s",backtrace[i]);
116 char *pos=strrchr(p,' ');
118 e->bt_strings[i] = bprintf("** At %s: ?? (%s)", addrs[i], p);
129 /** @brief shows an exception content and the associated stack if available */
130 void xbt_ex_display(xbt_ex_t *e) {
133 "** SimGrid: UNCAUGHT EXCEPTION on %s: category: %s; value: %d\n"
135 "** Thrown by %s%s%s",
137 xbt_ex_catname(e->category), e->value, e->msg,
138 e->procname, (e->host?"@":""),(e->host?e->host:""));
140 if (!e->remote && !e->bt_strings)
141 xbt_ex_setup_backtrace(e);
143 #if defined(HAVE_EXECINFO_H) && defined(HAVE_POPEN) && defined(ADDR2LINE)
144 /* We have everything to build neat backtraces */
148 fprintf(stderr,"\n");
149 for (i=0; i<e->used; i++)
150 fprintf(stderr,"%s\n",e->bt_strings[i]);
154 fprintf(stderr," at %s:%d:%s (no backtrace available on that arch)\n",
155 e->file,e->line,e->func);
161 /* default __ex_terminate callback function */
162 void __xbt_ex_terminate_default(xbt_ex_t *e) {
168 /* the externally visible API */
169 ex_ctx_cb_t __xbt_ex_ctx = &__xbt_ex_ctx_default;
170 ex_term_cb_t __xbt_ex_terminate = &__xbt_ex_terminate_default;
172 void xbt_ex_free(xbt_ex_t *e) {
175 if (e->msg) free(e->msg);
184 for (i=0; i<e->used; i++)
185 free((char*)e->bt_strings[i]);
186 free((char **)e->bt_strings);
188 memset(e,0,sizeof(xbt_ex_t));
191 /** \brief returns a short name for the given exception category */
192 const char * xbt_ex_catname(xbt_errcat_t cat) {
194 case unknown_error: return "unknown_err";
195 case arg_error: return "invalid_arg";
196 case mismatch_error: return "mismatch";
197 case not_found_error: return "not found";
198 case system_error: return "system_err";
199 case network_error: return "network_err";
200 case timeout_error: return "timeout";
201 case thread_error: return "thread_err";
202 default: return "INVALID_ERR";
206 #ifndef HAVE_EXECINFO_H
207 /* dummy implementation. We won't use the result, but ex.h needs it to be defined */
208 int backtrace (void **__array, int __size) {
217 XBT_TEST_SUITE("xbt_ex","Exception Handling");
219 XBT_TEST_UNIT("controlflow",test_controlflow, "basic nested control flow") {
223 xbt_test_add0("basic nested control flow");
227 xbt_test_fail1("M1: n=%d (!= 1)", n);
231 xbt_test_fail1("M2: n=%d (!= 2)", n);
233 THROW0(unknown_error,0,"something");
236 xbt_test_fail1("M3: n=%d (!= 3)", n);
243 xbt_test_fail1("M2: n=%d (!= 5)", n);
245 THROW0(unknown_error,0,"something");
248 xbt_test_fail1("M3: n=%d (!= 6)", n);
253 xbt_test_fail1("MX: n=%d (shouldn't reach this point)", n);
257 xbt_test_fail1("M4: n=%d (!= 7)", n);
262 xbt_test_fail1("M5: n=%d (!= 8)", n);
265 XBT_TEST_UNIT("value",test_value,"exception value passing") {
269 THROW0(unknown_error, 2, "toto");
271 xbt_test_add0("exception value passing");
272 if (ex.category != unknown_error)
273 xbt_test_fail1("category=%d (!= 1)", ex.category);
275 xbt_test_fail1("value=%d (!= 2)", ex.value);
276 if (strcmp(ex.msg,"toto"))
277 xbt_test_fail1("message=%s (!= toto)", ex.msg);
282 XBT_TEST_UNIT("variables",test_variables,"variable value preservation") {
287 r1 = r2 = v1 = v2 = 1234;
291 THROW0(unknown_error, 0, "toto");
293 xbt_test_add0("variable preservation");
295 xbt_test_fail1("r1=%d (!= 1234)", r1);
297 xbt_test_fail1("v1=%d (!= 1234)", v1);
298 /* r2 is allowed to be destroyed because not volatile */
300 xbt_test_fail1("v2=%d (!= 5678)", v2);
305 XBT_TEST_UNIT("cleanup",test_cleanup,"cleanup handling") {
310 xbt_test_add0("cleanup handling");
316 THROW0(1, 2, "blah");
319 xbt_test_fail1("v1 = %d (!= 5678)", v1);
323 xbt_test_fail1("v1 = %d (!= 5678)", v1);
324 if (!(ex.category == 1 && ex.value == 2 && !strcmp(ex.msg,"blah")))
325 xbt_test_fail0("unexpected exception contents");
329 xbt_test_fail0("xbt_ex_free not executed");
334 * The following is the example included in the documentation. It's a good
335 * idea to check its syntax even if we don't try to run it.
336 * And actually, it allows to put comments in the code despite doxygen.
338 static char *mallocex(int size) {
341 #define SMALLAMOUNT 10
342 #define TOOBIG 100000000
344 #if 0 /* this contains syntax errors, actually */
345 static void bad_example(void) {
346 struct {char*first;} *globalcontext;
351 char *cp1, *cp2, *cp3;
353 cp1 = mallocex(SMALLAMOUNT);
354 globalcontext->first = cp1;
355 cp2 = mallocex(TOOBIG);
356 cp3 = mallocex(SMALLAMOUNT);
360 if (cp3 != NULL) free(cp3);
361 if (cp2 != NULL) free(cp2);
362 if (cp1 != NULL) free(cp1);
364 printf("cp3=%s", cp3);
367 /* end_of_bad_example */
371 static void good_example(void) {
372 struct {char*first;} *globalcontext;
377 char * volatile /*03*/ cp1 = NULL /*02*/;
378 char * volatile /*03*/ cp2 = NULL /*02*/;
379 char * volatile /*03*/ cp3 = NULL /*02*/;
381 cp1 = mallocex(SMALLAMOUNT);
382 globalcontext->first = cp1;
383 cp1 = NULL /*05 give away*/;
384 cp2 = mallocex(TOOBIG);
385 cp3 = mallocex(SMALLAMOUNT);
389 printf("cp3=%s", cp3 == NULL /*02*/ ? "" : cp3);
394 /*05 cp1 was given away */
396 /*05 global context untouched */
400 /* end_of_good_example */
402 #endif /* SIMGRID_TEST */