X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/9f0e00fec83a9a1f7053f504e48173afb146331e..5c12f278e4acdb6745858a4b841c8a4dedd09fea:/src/xbt/ex.c?ds=sidebyside diff --git a/src/xbt/ex.c b/src/xbt/ex.c index c157964312..9bd1bfcb5c 100644 --- a/src/xbt/ex.c +++ b/src/xbt/ex.c @@ -1,5 +1,6 @@ /* -** OSSP ex - Exception Handling +** OSSP ex - Exception Handling (modified to fit into SimGrid) +** Copyright (c) 2005 Martin Quinson ** Copyright (c) 2002-2004 Ralf S. Engelschall ** Copyright (c) 2002-2004 The OSSP Project ** Copyright (c) 2002-2004 Cable & Wireless @@ -31,28 +32,272 @@ #include #include +#include "portable.h" /* execinfo when available */ #include "xbt/ex.h" +#include "gras/Virtu/virtu_interface.h" /* gras_os_myname */ + /* default __ex_ctx callback function */ -ex_ctx_t *__ex_ctx_default(void) -{ - static ex_ctx_t ctx = EX_CTX_INITIALIZER; +ex_ctx_t *__xbt_ex_ctx_default(void) { + static ex_ctx_t ctx = XBT_CTX_INITIALIZER; return &ctx; } +/** @brief shows an exception content and the associated stack if available */ +void xbt_ex_display(xbt_ex_t *e) { + + fprintf(stderr, + "** SimGrid: UNCAUGHT EXCEPTION on %s: category: %s; value: %d\n" + "** %s\n" + "** Thrown by %s%s%s at %s:%d:%s\n", + gras_os_myname(), + xbt_ex_catname(e->category), e->value, e->msg, + e->procname, (e->host?"@":""),(e->host?e->host:""),//localhost"), + e->file,e->line,e->func); + +#ifdef HAVE_EXECINFO_H + { + int i; + + fprintf(stderr,"** Backtrace:\n"); + if (!e->bt_strings) + e->bt_strings = backtrace_symbols (e->bt, e->used); + + for (i = 0; i < e->used; i++) + printf (" %s\n", e->bt_strings[i]); + } +#endif +} + + /* default __ex_terminate callback function */ -void __ex_terminate_default(ex_t *e) -{ - fprintf(stderr, - "**EX: UNCAUGHT EXCEPTION: " - "class=0x%lx object=0x%lx value=0x%lx [%s:%d@%s]\n", - (long)((e)->ex_class), (long)((e)->ex_object), (long)((e)->ex_value), - (e)->ex_file, (e)->ex_line, (e)->ex_func); - abort(); +void __xbt_ex_terminate_default(xbt_ex_t *e) { + xbt_ex_display(e); + + abort(); } /* the externally visible API */ -ex_ctx_cb_t __ex_ctx = &__ex_ctx_default; -ex_term_cb_t __ex_terminate = &__ex_terminate_default; +ex_ctx_cb_t __xbt_ex_ctx = &__xbt_ex_ctx_default; +ex_term_cb_t __xbt_ex_terminate = &__xbt_ex_terminate_default; + +void xbt_ex_free(xbt_ex_t e) { + int i; + + if (e.msg) free(e.msg); + free(e.procname); + if (e.host) { + free(e.file); + free(e.func); + for (i=0; ifirst = cp1; + cp2 = mallocex(TOOBIG); + cp3 = mallocex(SMALLAMOUNT); + strcpy(cp1, "foo"); + strcpy(cp2, "bar"); + } CLEANUP { + if (cp3 != NULL) free(cp3); + if (cp2 != NULL) free(cp2); + if (cp1 != NULL) free(cp1); + } CATCH(ex) { + printf("cp3=%s", cp3); + RETHROW; + } + /* end_of_bad_example */ +} +#endif + +static void good_example(void) { + struct {char*first;} *globalcontext; + xbt_ex_t ex; + + /* GOOD_EXAMPLE */ + { /*01*/ + char * volatile /*03*/ cp1 = NULL /*02*/; + char * volatile /*03*/ cp2 = NULL /*02*/; + char * volatile /*03*/ cp3 = NULL /*02*/; + TRY { + cp1 = mallocex(SMALLAMOUNT); + globalcontext->first = cp1; + cp1 = NULL /*05 give away*/; + cp2 = mallocex(TOOBIG); + cp3 = mallocex(SMALLAMOUNT); + strcpy(cp1, "foo"); + strcpy(cp2, "bar"); + } CLEANUP { /*04*/ + printf("cp3=%s", cp3 == NULL /*02*/ ? "" : cp3); + if (cp3 != NULL) + free(cp3); + if (cp2 != NULL) + free(cp2); + /*05 cp1 was given away */ + } CATCH(ex) { + /*05 global context untouched */ + RETHROW; + } + } + /* end_of_good_example */ +} +#endif /* SIMGRID_TEST */