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_backtrace_display(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=1; i<e.used; i++) /* no need to display "xbt_display_backtrace" */
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 /* size (in char) of pointers on this arch */
91 /* build the commandline */
92 curr += sprintf(curr,"%s -f -e %s ",ADDR2LINE,xbt_binary_name);
93 for (i=0; i<e->used;i++) {
94 /* retrieve this address */
95 snprintf(buff,256,"%s",strchr(backtrace[i],'[')+1);
98 addrs[i]=bprintf("%s",buff);
100 /* Add it to the command line args */
101 curr+=sprintf(curr,"%s ",addrs[i]);
103 addr_len = strlen(addrs[0]);
105 /* parse the output and build a new backtrace */
106 e->bt_strings = xbt_new(char*,e->used);
108 pipe = popen(cmd, "r");
109 // xbt_assert(pipe);//,"Cannot fork addr2line to display the backtrace");
110 for (i=0; i<e->used; i++) {
111 fgets(line_func,1024,pipe);
112 line_func[strlen(line_func)-1]='\0';
113 fgets(line_pos,1024,pipe);
114 line_pos[strlen(line_pos)-1]='\0';
116 if (strcmp("??",line_func)) {
117 e->bt_strings[i] = bprintf("** At %s: %s (%s)",
118 addrs[i], line_func,line_pos);
120 /* Damn. The symbol is in a dynamic library. Let's get wild */
125 long int addr,offset,offset_save;
132 /* let's look for the offset of this library in our addressing space */
133 maps_name=bprintf("/proc/%d/maps",(int)getpid());
134 maps=fopen(maps_name,"r");
136 addr=strtol(addrs[i]+2, NULL,16);
139 if (fgets(maps_buff,512,maps) == NULL)
141 offset_save = offset;
142 offset=strtol(maps_buff,NULL,16);
150 /* Ok, the maps line we just read is beyond the searched symbol. Got it,
151 the symbol is in previous map, which offset is in offset_save.
152 We now need to substract this from the address we got from backtrace.
157 addrs[i] = bprintf("0x%0*lx",addr_len-2,addr-offset);
159 /* Got it. We have our new address. Let's get the library path and we
161 p = xbt_strdup(backtrace[i]);
167 /* Here we go, fire an addr2line up */
168 subcmd = bprintf("%s -f -e %s %s",ADDR2LINE,p, addrs[i]);
170 subpipe = popen(subcmd,"r");
171 fgets(line_func,1024,subpipe);
172 line_func[strlen(line_func)-1]='\0';
173 fgets(line_pos,1024,subpipe);
174 line_pos[strlen(line_pos)-1]='\0';
178 /* check whether the trick worked */
179 if (strcmp("??",line_func)) {
180 e->bt_strings[i] = bprintf("** At %s: %s (%s)",
181 addrs[i], line_func,line_pos);
183 /* damn, nothing to do here. Let's print the raw address */
184 p = bprintf("%s",backtrace[i]);
187 e->bt_strings[i] = bprintf("** At %s: ?? (%s)", addrs[i], p);
200 /** @brief shows an exception content and the associated stack if available */
201 void xbt_ex_display(xbt_ex_t *e) {
204 "** SimGrid: UNCAUGHT EXCEPTION on %s: category: %s; value: %d\n"
206 "** Thrown by %s%s%s",
208 xbt_ex_catname(e->category), e->value, e->msg,
209 e->procname, (e->host?"@":""),(e->host?e->host:""));
211 if (!e->remote && !e->bt_strings)
212 xbt_ex_setup_backtrace(e);
214 #if defined(HAVE_EXECINFO_H) && defined(HAVE_POPEN) && defined(ADDR2LINE)
215 /* We have everything to build neat backtraces */
219 fprintf(stderr,"\n");
220 for (i=0; i<e->used; i++)
221 fprintf(stderr,"%s\n",e->bt_strings[i]);
225 fprintf(stderr," at %s:%d:%s (no backtrace available on that arch)\n",
226 e->file,e->line,e->func);
232 /* default __ex_terminate callback function */
233 void __xbt_ex_terminate_default(xbt_ex_t *e) {
239 /* the externally visible API */
240 ex_ctx_cb_t __xbt_ex_ctx = &__xbt_ex_ctx_default;
241 ex_term_cb_t __xbt_ex_terminate = &__xbt_ex_terminate_default;
243 void xbt_ex_free(xbt_ex_t e) {
246 if (e.msg) free(e.msg);
255 for (i=0; i<e.used; i++)
256 free((char*)e.bt_strings[i]);
257 free((char **)e.bt_strings);
259 // memset(e,0,sizeof(xbt_ex_t));
262 /** \brief returns a short name for the given exception category */
263 const char * xbt_ex_catname(xbt_errcat_t cat) {
265 case unknown_error: return "unknown_err";
266 case arg_error: return "invalid_arg";
267 case mismatch_error: return "mismatch";
268 case not_found_error: return "not found";
269 case system_error: return "system_err";
270 case network_error: return "network_err";
271 case timeout_error: return "timeout";
272 case thread_error: return "thread_err";
273 default: return "INVALID_ERR";
277 #ifndef HAVE_EXECINFO_H
278 /* dummy implementation. We won't use the result, but ex.h needs it to be defined */
279 int backtrace (void **__array, int __size) {
288 XBT_TEST_SUITE("xbt_ex","Exception Handling");
290 XBT_TEST_UNIT("controlflow",test_controlflow, "basic nested control flow") {
294 xbt_test_add0("basic nested control flow");
298 xbt_test_fail1("M1: n=%d (!= 1)", n);
302 xbt_test_fail1("M2: n=%d (!= 2)", n);
304 THROW0(unknown_error,0,"something");
307 xbt_test_fail1("M3: n=%d (!= 3)", n);
314 xbt_test_fail1("M2: n=%d (!= 5)", n);
316 THROW0(unknown_error,0,"something");
319 xbt_test_fail1("M3: n=%d (!= 6)", n);
324 xbt_test_fail1("MX: n=%d (shouldn't reach this point)", n);
328 xbt_test_fail1("M4: n=%d (!= 7)", n);
333 xbt_test_fail1("M5: n=%d (!= 8)", n);
336 XBT_TEST_UNIT("value",test_value,"exception value passing") {
340 THROW0(unknown_error, 2, "toto");
342 xbt_test_add0("exception value passing");
343 if (ex.category != unknown_error)
344 xbt_test_fail1("category=%d (!= 1)", ex.category);
346 xbt_test_fail1("value=%d (!= 2)", ex.value);
347 if (strcmp(ex.msg,"toto"))
348 xbt_test_fail1("message=%s (!= toto)", ex.msg);
353 XBT_TEST_UNIT("variables",test_variables,"variable value preservation") {
358 r1 = r2 = v1 = v2 = 1234;
362 THROW0(unknown_error, 0, "toto");
364 xbt_test_add0("variable preservation");
366 xbt_test_fail1("r1=%d (!= 1234)", r1);
368 xbt_test_fail1("v1=%d (!= 1234)", v1);
369 /* r2 is allowed to be destroyed because not volatile */
371 xbt_test_fail1("v2=%d (!= 5678)", v2);
376 XBT_TEST_UNIT("cleanup",test_cleanup,"cleanup handling") {
381 xbt_test_add0("cleanup handling");
387 THROW0(1, 2, "blah");
390 xbt_test_fail1("v1 = %d (!= 5678)", v1);
394 xbt_test_fail1("v1 = %d (!= 5678)", v1);
395 if (!(ex.category == 1 && ex.value == 2 && !strcmp(ex.msg,"blah")))
396 xbt_test_fail0("unexpected exception contents");
400 xbt_test_fail0("xbt_ex_free not executed");
405 * The following is the example included in the documentation. It's a good
406 * idea to check its syntax even if we don't try to run it.
407 * And actually, it allows to put comments in the code despite doxygen.
409 static char *mallocex(int size) {
412 #define SMALLAMOUNT 10
413 #define TOOBIG 100000000
415 #if 0 /* this contains syntax errors, actually */
416 static void bad_example(void) {
417 struct {char*first;} *globalcontext;
422 char *cp1, *cp2, *cp3;
424 cp1 = mallocex(SMALLAMOUNT);
425 globalcontext->first = cp1;
426 cp2 = mallocex(TOOBIG);
427 cp3 = mallocex(SMALLAMOUNT);
431 if (cp3 != NULL) free(cp3);
432 if (cp2 != NULL) free(cp2);
433 if (cp1 != NULL) free(cp1);
435 printf("cp3=%s", cp3);
438 /* end_of_bad_example */
442 static void good_example(void) {
443 struct {char*first;} *globalcontext;
448 char * volatile /*03*/ cp1 = NULL /*02*/;
449 char * volatile /*03*/ cp2 = NULL /*02*/;
450 char * volatile /*03*/ cp3 = NULL /*02*/;
452 cp1 = mallocex(SMALLAMOUNT);
453 globalcontext->first = cp1;
454 cp1 = NULL /*05 give away*/;
455 cp2 = mallocex(TOOBIG);
456 cp3 = mallocex(SMALLAMOUNT);
460 printf("cp3=%s", cp3 == NULL /*02*/ ? "" : cp3);
465 /*05 cp1 was given away */
467 /*05 global context untouched */
471 /* end_of_good_example */
473 #endif /* SIMGRID_TEST */