Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Tweak things here and there so that we can propagate exceptions across the network...
[simgrid.git] / src / xbt / ex.c
index 31209f5..18de35e 100644 (file)
@@ -35,6 +35,8 @@
 #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 *__xbt_ex_ctx_default(void) {
     static ex_ctx_t ctx = XBT_CTX_INITIALIZER;
@@ -46,25 +48,24 @@ ex_ctx_t *__xbt_ex_ctx_default(void) {
 void xbt_ex_display(xbt_ex_t *e)  {
 
   fprintf(stderr,
-         "** SimGrid: UNCAUGHT EXCEPTION: category: %s; value: %d\n"
+         "** 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->procname, (e->host?"@":""),(e->host?e->host:""),//localhost"),
          e->file,e->line,e->func);
 
 #ifdef HAVE_EXECINFO_H
  {
-  char **strings;
   int i;
 
   fprintf(stderr,"** Backtrace:\n");
-  strings = backtrace_symbols (e->bt, e->used);
+  if (!e->bt_strings)
+    e->bt_strings = backtrace_symbols (e->bt, e->used);
 
   for (i = 0; i < e->used; i++)
-     printf ("   %s\n", strings[i]);
-
-  free (strings);
+     printf ("   %s\n", e->bt_strings[i]);
  }
 #endif
 }
@@ -82,8 +83,22 @@ 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; i<e.used; i++) 
+      free(e.bt_strings[i]);
+    free(e.bt_strings);
+    e.bt_strings=NULL;
+  }
+  /* locally, only one chunk of memory is allocated by the libc */
+  if (e.bt_strings)
+    free(e.bt_strings);
+
 }
 
 /** \brief returns a short name for the given exception category */
@@ -213,4 +228,75 @@ XBT_TEST_UNIT("cleanup",test_cleanup,"cleanup handling") {
     if (!c)
         xbt_test_fail0("xbt_ex_free not executed");
 }
+
+
+/*
+ * The following is the example included in the documentation. It's a good 
+ * idea to check its syntax even if we don't try to run it.
+ * And actually, it allows to put comments in the code despite doxygen.
+ */ 
+static char *mallocex(int size) {
+  return NULL;
+}
+#define SMALLAMOUNT 10
+#define TOOBIG 100000000
+
+#if 0 /* this contains syntax errors, actually */
+static void bad_example(void) {
+  struct {char*first;} *globalcontext;
+  ex_t ex;
+
+  /* BAD_EXAMPLE */
+  TRY {
+    char *cp1, *cp2, *cp3;
+    
+    cp1 = mallocex(SMALLAMOUNT);
+    globalcontext->first = 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 */