Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Avoid (almost) duplicated code in xbt_log_layout_*.
[simgrid.git] / src / xbt / backtrace_linux.c
index 77f856d..e4eda28 100644 (file)
@@ -28,6 +28,12 @@ void xbt_backtrace_postexit(void)
 void xbt_backtrace_current(xbt_ex_t * e)
 {
   e->used = backtrace((void **) e->bt, XBT_BACKTRACE_SIZE);
+  if (e->used == 0) {
+    fprintf(stderr, "The backtrace() function failed, which probably means that the memory is exhausted. Here is a crude dump of the exception that I was trying to build:");
+    fprintf(stderr, "%s:%s(%d) [%s:%d] %s", e->host,e->procname,e->pid, e->file,e->line,e->msg);
+    fprintf(stderr, "Bailing out now since there is nothing I can do without a decent amount of memory. Please go fix the memleaks\n");
+    exit(1);
+  }
 }
 
 
@@ -56,7 +62,7 @@ void xbt_ex_setup_backtrace(xbt_ex_t * e)
   struct stat stat_buf;
   char *binary_name = NULL;
 
-  xbt_assert0(e
+  xbt_assert(e
               && e->used,
               "Backtrace not setup yet, cannot set it up for display");
 
@@ -128,9 +134,9 @@ void xbt_ex_setup_backtrace(xbt_ex_t * e)
     p = strchr(buff, ']');
     *p = '\0';
     if (strcmp(buff, "(nil)"))
-      addrs[i] = bprintf("%s", buff);
+      addrs[i] = xbt_strdup(buff);
     else
-      addrs[i] = bprintf("0x0");
+      addrs[i] = xbt_strdup("0x0");
     XBT_DEBUG("Set up a new address: %d, '%s'(%p)", i, addrs[i], addrs[i]);
 
     /* Add it to the command line args */
@@ -153,13 +159,13 @@ void xbt_ex_setup_backtrace(xbt_ex_t * e)
     XBT_DEBUG("Looking for symbol %d, addr = '%s'", i, addrs[i]);
     fgets_res = fgets(line_func, 1024, pipe);
     if (fgets_res == NULL)
-      THROW2(system_error, 0,
+      THROWF(system_error, 0,
              "Cannot run fgets to look for symbol %d, addr %s", i,
              addrs[i]);
     line_func[strlen(line_func) - 1] = '\0';
     fgets_res = fgets(line_pos, 1024, pipe);
     if (fgets_res == NULL)
-      THROW2(system_error, 0,
+      THROWF(system_error, 0,
              "Cannot run fgets to look for symbol %d, addr %s", i,
              addrs[i]);
     line_pos[strlen(line_pos) - 1] = '\0';
@@ -185,10 +191,8 @@ void xbt_ex_setup_backtrace(xbt_ex_t * e)
       maps_name = bprintf("/proc/%d/maps", (int) getpid());
       maps = fopen(maps_name, "r");
 
-      sscanf(addrs[i], "%lx", &addr);
-      sprintf(maps_buff, "%#lx", addr);
-
-      if (strcmp(addrs[i], maps_buff)) {
+      addr = strtol(addrs[i], &p, 16);
+      if (*p != '\0') {
         XBT_CRITICAL("Cannot parse backtrace address '%s' (addr=%#lx)",
                   addrs[i], addr);
       }
@@ -261,12 +265,12 @@ void xbt_ex_setup_backtrace(xbt_ex_t * e)
         }
         fgets_res = fgets(line_func, 1024, subpipe);
         if (fgets_res == NULL)
-          THROW1(system_error, 0, "Cannot read result of subcommand %s",
+          THROWF(system_error, 0, "Cannot read result of subcommand %s",
                  subcmd);
         line_func[strlen(line_func) - 1] = '\0';
         fgets_res = fgets(line_pos, 1024, subpipe);
         if (fgets_res == NULL)
-          THROW1(system_error, 0, "Cannot read result of subcommand %s",
+          THROWF(system_error, 0, "Cannot read result of subcommand %s",
                  subcmd);
         line_pos[strlen(line_pos) - 1] = '\0';
         pclose(subpipe);
@@ -304,7 +308,7 @@ void xbt_ex_setup_backtrace(xbt_ex_t * e)
           ("xbt_thread_context_wrapper", line_func,
            strlen("xbt_thread_context_wrapper"))) {
         free(e->bt_strings[i]);
-        e->bt_strings[i] = bprintf("**   (in a separate thread)");
+        e->bt_strings[i] = xbt_strdup("**   (in a separate thread)");
       }
     }