3 /* ex - Exception Handling (modified to fit into SimGrid from OSSP version) */
5 /* Copyright (c) 2005-2006 Martin Quinson */
6 /* Copyright (c) 2002-2004 Ralf S. Engelschall <rse@engelschall.com> */
7 /* Copyright (c) 2002-2004 The OSSP Project <http://www.ossp.org/> */
8 /* Copyright (c) 2002-2004 Cable & Wireless <http://www.cw.com/> */
9 /* All rights reserved. */
11 /* This program is free software; you can redistribute it and/or modify it
12 * under the terms of the license (GNU LGPL) which comes with this package. */
17 #include "portable.h" /* execinfo when available */
19 #include "xbt/module.h" /* xbt_binary_name */
20 #include "xbt/ex_interface.h"
22 #include "gras/Virtu/virtu_interface.h" /* gras_os_myname */
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_ex,xbt,"Exception mecanism");
27 /* default __ex_ctx callback function */
28 ex_ctx_t *__xbt_ex_ctx_default(void) {
29 static ex_ctx_t ctx = XBT_CTX_INITIALIZER;
35 /** \brief show the backtrace of the current point (lovely while debuging) */
36 void xbt_backtrace_display(void) {
37 #if defined(HAVE_EXECINFO_H) && defined(HAVE_POPEN) && defined(ADDR2LINE)
41 e.used = backtrace((void**)e.bt,XBT_BACKTRACE_SIZE);
43 xbt_ex_setup_backtrace(&e);
44 for (i=1; i<e.used; i++) /* no need to display "xbt_display_backtrace" */
45 fprintf(stderr,"%s\n",e.bt_strings[i]);
51 ERROR0("No backtrace on this arch");
55 void xbt_ex_setup_backtrace(xbt_ex_t *e) {
56 #if defined(HAVE_EXECINFO_H) && defined(HAVE_POPEN) && defined(ADDR2LINE)
58 /* to get the backtrace from the libc */
59 char **backtrace = backtrace_symbols (e->bt, e->used);
61 /* To build the commandline of addr2line */
62 char *cmd = xbt_new(char,strlen(ADDR2LINE)+strlen(xbt_binary_name)+20*e->used);
65 /* to extract the addresses from the backtrace */
66 char **addrs=xbt_new(char*,e->used);
69 /* To read the output of addr2line */
71 char line_func[1024],line_pos[1024];
73 /* size (in char) of pointers on this arch */
76 /* build the commandline */
77 curr += sprintf(curr,"%s -f -e %s ",ADDR2LINE,xbt_binary_name);
78 for (i=0; i<e->used;i++) {
79 /* retrieve this address */
80 snprintf(buff,256,"%s",strchr(backtrace[i],'[')+1);
83 addrs[i]=bprintf("%s",buff);
85 /* Add it to the command line args */
86 curr+=sprintf(curr,"%s ",addrs[i]);
88 addr_len = strlen(addrs[0]);
90 /* parse the output and build a new backtrace */
91 e->bt_strings = xbt_new(char*,e->used);
93 pipe = popen(cmd, "r");
95 CRITICAL0("Cannot fork addr2line to display the backtrace");
98 for (i=0; i<e->used; i++) {
99 fgets(line_func,1024,pipe);
100 line_func[strlen(line_func)-1]='\0';
101 fgets(line_pos,1024,pipe);
102 line_pos[strlen(line_pos)-1]='\0';
104 if (strcmp("??",line_func)) {
105 e->bt_strings[i] = bprintf("** In %s() at %s (static symbol)", line_func,line_pos);
107 /* Damn. The symbol is in a dynamic library. Let's get wild */
112 long int addr,offset=0;
119 /* let's look for the offset of this library in our addressing space */
120 maps_name=bprintf("/proc/%d/maps",(int)getpid());
121 maps=fopen(maps_name,"r");
123 sscanf(addrs[i],"%lx",&addr);
124 sprintf(maps_buff,"%#lx",addr);
126 if (strcmp(addrs[i],maps_buff)) {
127 CRITICAL2("Cannot parse backtrace address '%s' (addr=%#lx)",
130 DEBUG2("addr=%s (as string) =%#lx (as number)",addrs[i],addr);
133 long int first, last;
134 if (fgets(maps_buff,512,maps) == NULL)
137 maps_buff[strlen(maps_buff) -1]='\0';
138 DEBUG1("map line: %s", maps_buff);
140 sscanf(maps_buff,"%lx",&first);
141 p=strchr(maps_buff,'-')+1;
142 sscanf(p,"%lx",&last);
143 if (first < addr && addr < last) {
147 DEBUG4("%#lx %s [%#lx-%#lx]",
148 addr, found? "in":"out of",first,last);
151 CRITICAL0("Problem while reading the maps file");
156 /* Ok, Found the offset of the maps line containing the searched symbol.
157 We now need to substract this from the address we got from backtrace.
161 addrs[i] = bprintf("0x%0*lx",addr_len-2,addr-offset);
162 DEBUG2("offset=%#lx new addr=%s",offset,addrs[i]);
164 /* Got it. We have our new address. Let's get the library path and we
166 p = xbt_strdup(backtrace[i]);
172 /* Here we go, fire an addr2line up */
173 subcmd = bprintf("%s -f -e %s %s",ADDR2LINE,p, addrs[i]);
175 VERB1("Fire a new command: '%s'",subcmd);
176 subpipe = popen(subcmd,"r");
178 CRITICAL0("Cannot fork addr2line to display the backtrace");
181 fgets(line_func,1024,subpipe);
182 line_func[strlen(line_func)-1]='\0';
183 fgets(line_pos,1024,subpipe);
184 line_pos[strlen(line_pos)-1]='\0';
188 /* check whether the trick worked */
189 if (strcmp("??",line_func)) {
190 e->bt_strings[i] = bprintf("** In %s() at %s (dynamic symbol)", line_func,line_pos);
192 /* damn, nothing to do here. Let's print the raw address */
193 e->bt_strings[i] = bprintf("** In ?? (%s)", backtrace[i]);
205 /** @brief shows an exception content and the associated stack if available */
206 void xbt_ex_display(xbt_ex_t *e) {
210 bprintf(" on host %s(%ld)",e->host,e->pid);
212 CRITICAL1("%s",e->msg);
214 "** SimGrid: UNCAUGHT EXCEPTION received on %s(%ld): category: %s; value: %d\n"
216 "** Thrown by %s()%s\n",
217 gras_os_myname(),gras_os_getpid(),
218 xbt_ex_catname(e->category), e->value, e->msg,
219 e->procname,thrower?thrower:" in this process");
224 if (!e->remote && !e->bt_strings)
225 xbt_ex_setup_backtrace(e);
227 #if defined(HAVE_EXECINFO_H) && defined(HAVE_POPEN) && defined(ADDR2LINE)
228 /* We have everything to build neat backtraces */
232 fprintf(stderr,"\n");
233 for (i=0; i<e->used; i++)
234 fprintf(stderr,"%s\n",e->bt_strings[i]);
238 fprintf(stderr," at %s:%d:%s (no backtrace available on that arch)\n",
239 e->file,e->line,e->func);
245 /* default __ex_terminate callback function */
246 void __xbt_ex_terminate_default(xbt_ex_t *e) {
252 /* the externally visible API */
253 ex_ctx_cb_t __xbt_ex_ctx = &__xbt_ex_ctx_default;
254 ex_term_cb_t __xbt_ex_terminate = &__xbt_ex_terminate_default;
256 void xbt_ex_free(xbt_ex_t e) {
259 if (e.msg) free(e.msg);
268 for (i=0; i<e.used; i++)
269 free((char*)e.bt_strings[i]);
270 free((char **)e.bt_strings);
272 // memset(e,0,sizeof(xbt_ex_t));
275 /** \brief returns a short name for the given exception category */
276 const char * xbt_ex_catname(xbt_errcat_t cat) {
278 case unknown_error: return "unknown_err";
279 case arg_error: return "invalid_arg";
280 case mismatch_error: return "mismatch";
281 case not_found_error: return "not found";
282 case system_error: return "system_err";
283 case network_error: return "network_err";
284 case timeout_error: return "timeout";
285 case thread_error: return "thread_err";
286 default: return "INVALID_ERR";
290 #ifndef HAVE_EXECINFO_H
291 /* dummy implementation. We won't use the result, but ex.h needs it to be defined */
292 int backtrace (void **__array, int __size) {
301 XBT_TEST_SUITE("xbt_ex","Exception Handling");
303 XBT_TEST_UNIT("controlflow",test_controlflow, "basic nested control flow") {
307 xbt_test_add0("basic nested control flow");
311 xbt_test_fail1("M1: n=%d (!= 1)", n);
315 xbt_test_fail1("M2: n=%d (!= 2)", n);
317 THROW0(unknown_error,0,"something");
320 xbt_test_fail1("M3: n=%d (!= 3)", n);
327 xbt_test_fail1("M2: n=%d (!= 5)", n);
329 THROW0(unknown_error,0,"something");
332 xbt_test_fail1("M3: n=%d (!= 6)", n);
337 xbt_test_fail1("MX: n=%d (shouldn't reach this point)", n);
341 xbt_test_fail1("M4: n=%d (!= 7)", n);
346 xbt_test_fail1("M5: n=%d (!= 8)", n);
349 XBT_TEST_UNIT("value",test_value,"exception value passing") {
353 THROW0(unknown_error, 2, "toto");
355 xbt_test_add0("exception value passing");
356 if (ex.category != unknown_error)
357 xbt_test_fail1("category=%d (!= 1)", ex.category);
359 xbt_test_fail1("value=%d (!= 2)", ex.value);
360 if (strcmp(ex.msg,"toto"))
361 xbt_test_fail1("message=%s (!= toto)", ex.msg);
366 XBT_TEST_UNIT("variables",test_variables,"variable value preservation") {
371 r1 = r2 = v1 = v2 = 1234;
375 THROW0(unknown_error, 0, "toto");
377 xbt_test_add0("variable preservation");
379 xbt_test_fail1("r1=%d (!= 1234)", r1);
381 xbt_test_fail1("v1=%d (!= 1234)", v1);
382 /* r2 is allowed to be destroyed because not volatile */
384 xbt_test_fail1("v2=%d (!= 5678)", v2);
389 XBT_TEST_UNIT("cleanup",test_cleanup,"cleanup handling") {
394 xbt_test_add0("cleanup handling");
400 THROW0(1, 2, "blah");
403 xbt_test_fail1("v1 = %d (!= 5678)", v1);
407 xbt_test_fail1("v1 = %d (!= 5678)", v1);
408 if (!(ex.category == 1 && ex.value == 2 && !strcmp(ex.msg,"blah")))
409 xbt_test_fail0("unexpected exception contents");
413 xbt_test_fail0("xbt_ex_free not executed");
418 * The following is the example included in the documentation. It's a good
419 * idea to check its syntax even if we don't try to run it.
420 * And actually, it allows to put comments in the code despite doxygen.
422 static char *mallocex(int size) {
425 #define SMALLAMOUNT 10
426 #define TOOBIG 100000000
428 #if 0 /* this contains syntax errors, actually */
429 static void bad_example(void) {
430 struct {char*first;} *globalcontext;
435 char *cp1, *cp2, *cp3;
437 cp1 = mallocex(SMALLAMOUNT);
438 globalcontext->first = cp1;
439 cp2 = mallocex(TOOBIG);
440 cp3 = mallocex(SMALLAMOUNT);
444 if (cp3 != NULL) free(cp3);
445 if (cp2 != NULL) free(cp2);
446 if (cp1 != NULL) free(cp1);
448 printf("cp3=%s", cp3);
451 /* end_of_bad_example */
455 static void good_example(void) {
456 struct {char*first;} *globalcontext;
461 char * volatile /*03*/ cp1 = NULL /*02*/;
462 char * volatile /*03*/ cp2 = NULL /*02*/;
463 char * volatile /*03*/ cp3 = NULL /*02*/;
465 cp1 = mallocex(SMALLAMOUNT);
466 globalcontext->first = cp1;
467 cp1 = NULL /*05 give away*/;
468 cp2 = mallocex(TOOBIG);
469 cp3 = mallocex(SMALLAMOUNT);
473 printf("cp3=%s", cp3 == NULL /*02*/ ? "" : cp3);
478 /*05 cp1 was given away */
480 /*05 global context untouched */
484 /* end_of_good_example */
486 #endif /* SIMGRID_TEST */