Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Other try at killing that singleton
[simgrid.git] / src / xbt / backtrace_windows.c
index 16cdd74..a32364a 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: ex.c 5173 2008-01-07 22:10:52Z mquinson $ */
+/* $Id$ */
 
 /* backtrace_windows - backtrace displaying on windows platform             */
 /* This file is included by ex.c on need (windows x86)                      */
 
 /* backtrace_windows - backtrace displaying on windows platform             */
 /* This file is included by ex.c on need (windows x86)                      */
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
 /* 
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
 /* 
- * Win32 (x86) implementation backtrace, backtrace_symbols
- * : support for application self-debugging.
+ * Win32 (x86) implementation backtrace, backtrace_symbols:
+ *  support for application self-debugging.
  */
 
 #include <dbghelp.h>
 
  */
 
 #include <dbghelp.h>
 
+
 /* Pointer function to SymInitialize() */
 /* Pointer function to SymInitialize() */
-typedef BOOL(WINAPI * xbt_pfn_sym_initialize_t) (HANDLE, PSTR, BOOL);
+void *fun_initialize;
+void *fun_cleanup;
+void *fun_function_table_access;
+void *fun_get_line_from_addr;
+void *fun_get_module_base;
+void *fun_get_options;
+void *fun_set_options;
+void *fun_get_sym_from_addr;
+void *fun_stack_walk;
+
+#if 0
+BOOL(WINAPI * fun_initialize) (HANDLE, PSTR, BOOL);
 
 /* Pointer function to SymCleanup() */
 
 /* Pointer function to SymCleanup() */
-typedef BOOL(WINAPI * xbt_pfn_sym_cleanup_t) (HANDLE hProcess);
+static BOOL(WINAPI * fun_cleanup) (HANDLE hProcess);
 
 /* Pointer function to SymFunctionTableAccess() */
 
 /* Pointer function to SymFunctionTableAccess() */
-typedef PVOID(WINAPI * xbt_pfn_sym_function_table_access_t) (HANDLE, DWORD);
+static PVOID(WINAPI * fun_function_table_access) (HANDLE, DWORD);
 
 /* Pointer function to SymGetLineFromAddr() */
 
 /* Pointer function to SymGetLineFromAddr() */
-typedef BOOL(WINAPI * xbt_pfn_sym_get_line_from_addr_t) (HANDLE, DWORD,
-                                                         PDWORD,
-                                                         PIMAGEHLP_LINE);
+static BOOL(WINAPI * fun_get_line_from_addr) (HANDLE, DWORD,
+                                               PDWORD,
+                                               PIMAGEHLP_LINE);
 
 /* Pointer function to SymGetModuleBase() */
 
 /* Pointer function to SymGetModuleBase() */
-typedef DWORD(WINAPI * xbt_pfn_sym_get_module_base_t) (HANDLE, DWORD);
+static DWORD(WINAPI * fun_get_module_base) (HANDLE, DWORD);
 
 /* Pointer function to SymGetOptions() */
 
 /* Pointer function to SymGetOptions() */
-typedef DWORD(WINAPI * xbt_pfn_sym_get_options_t) (VOID);
+static DWORD(WINAPI * fun_get_options) (VOID);
 
 /* Pointer function to SymGetSymFromAddr() */
 
 /* Pointer function to SymGetSymFromAddr() */
-typedef BOOL(WINAPI * xbt_pfn_sym_get_sym_from_addr_t) (HANDLE, DWORD, PDWORD,
-                                                        OUT PIMAGEHLP_SYMBOL);
+static BOOL(WINAPI * fun_get_sym_from_addr) (HANDLE, DWORD, PDWORD,
+                                              OUT PIMAGEHLP_SYMBOL);
 
 /* Pointer function to SymSetOptions() */
 
 /* Pointer function to SymSetOptions() */
-typedef DWORD(WINAPI * xbt_pfn_sym_set_options_t) (DWORD);
+static DWORD(WINAPI * fun_set_options) (DWORD);
 
 /* Pointer function to StackWalk() */
 
 /* Pointer function to StackWalk() */
-typedef BOOL(WINAPI * xbt_pfn_stack_walk_t) (DWORD, HANDLE, HANDLE,
-                                             LPSTACKFRAME, PVOID,
-                                             PREAD_PROCESS_MEMORY_ROUTINE,
-                                             PFUNCTION_TABLE_ACCESS_ROUTINE,
-                                             PGET_MODULE_BASE_ROUTINE,
-                                             PTRANSLATE_ADDRESS_ROUTINE);
-
-/* This structure represents the debug_help library used interface */
-typedef struct s_xbt_debug_help {
-  HINSTANCE instance;
-  HANDLE process_handle;
-  xbt_pfn_sym_initialize_t sym_initialize;
-  xbt_pfn_sym_cleanup_t sym_cleanup;
-  xbt_pfn_sym_function_table_access_t sym_function_table_access;
-  xbt_pfn_sym_get_line_from_addr_t sym_get_line_from_addr;
-  xbt_pfn_sym_get_module_base_t sym_get_module_base;
-  xbt_pfn_sym_get_options_t sym_get_options;
-  xbt_pfn_sym_get_sym_from_addr_t sym_get_sym_from_addr;
-  xbt_pfn_sym_set_options_t sym_set_options;
-  xbt_pfn_stack_walk_t stack_walk;
-} s_xbt_debug_hlp_t, *xbt_debug_hlp_t;
-
-
-/* the address to the unique reference to the debug help library interface */
-static xbt_debug_hlp_t dbg_hlp = NULL;
-
-/* initialize the debug help library */
-static int dbg_hlp_init(HANDLE process_handle);
-
-/* finalize the debug help library */
-static int dbg_hlp_finalize(void);
+static BOOL(WINAPI * fun_stack_walk) (DWORD, HANDLE, HANDLE,
+                                           LPSTACKFRAME, PVOID,
+                                           PREAD_PROCESS_MEMORY_ROUTINE,
+                                           PFUNCTION_TABLE_ACCESS_ROUTINE,
+                                           PGET_MODULE_BASE_ROUTINE,
+                                           PTRANSLATE_ADDRESS_ROUTINE);
+#endif
+
+static HINSTANCE hlp_dbg_instance = NULL;
+static HANDLE process_handle = NULL;
+
+
+/* Module creation/destruction: nothing to do on linux */
+void xbt_backtrace_init(void) { 
+  process_handle = GetCurrentProcess();
+
+  if (hlp_dbg_instance) {
+    /* debug help is already loaded */
+    return;
+  }
+
+  /* load the library */
+  hlp_dbg_instance = LoadLibraryA("Dbghelp.dll");
+
+  if (!hlp_dbg_instance)
+    return;
+  /* get the pointers to debug help library exported functions */
+  fun_initialize = GetProcAddress(hlp_dbg_instance, "SymInitialize");
+  fun_cleanup = GetProcAddress(hlp_dbg_instance, "SymCleanup");
+  fun_function_table_access = GetProcAddress(hlp_dbg_instance, "SymFunctionTableAccess");
+  fun_get_line_from_addr = GetProcAddress(hlp_dbg_instance, "SymGetLineFromAddr");
+  fun_get_module_base = GetProcAddress(hlp_dbg_instance, "SymGetModuleBase");
+  fun_get_options = GetProcAddress(hlp_dbg_instance, "SymGetOptions");
+  fun_get_sym_from_addr = GetProcAddress(hlp_dbg_instance, "SymGetSymFromAddr");
+  fun_set_options = GetProcAddress(hlp_dbg_instance, "SymSetOptions");
+  fun_stack_walk = GetProcAddress(hlp_dbg_instance, "StackWalk");
+
+  /* Check that everything worked well */
+  if (!fun_initialize ||
+      !fun_cleanup ||
+      !fun_function_table_access || 
+      !fun_get_line_from_addr ||
+      !fun_get_module_base ||
+      !fun_get_options ||
+      !fun_get_sym_from_addr ||
+      !fun_set_options ||
+      !fun_stack_walk
+      ) {
+    FreeLibrary(hlp_dbg_instance);
+    hlp_dbg_instance = NULL;
+    return;
+  }
+
+  (*fun_set_options) ((*fun_get_options) () |
+                       SYMOPT_LOAD_LINES | SYMOPT_DEFERRED_LOADS);
+
+  if (!(*fun_initialize) (process_handle, 0, 1)) {
+    FreeLibrary(hlp_dbg_instance);
+    hlp_dbg_instance = NULL;
+  }
+}
+void xbt_backtrace_exit(void) { 
+  if (!hlp_dbg_instance)
+    return;
+
+  if ((*fun_cleanup) (process_handle))
+    FreeLibrary(hlp_dbg_instance);
+
+  hlp_dbg_instance = NULL;
+}
 
 /*
  * backtrace() function.
 
 /*
  * backtrace() function.
@@ -112,10 +164,8 @@ void xbt_ex_setup_backtrace(xbt_ex_t * e)
   e->bt_strings = xbt_new(char *, e->used);
 
 
   e->bt_strings = xbt_new(char *, e->used);
 
 
-  for (i = 0; i < e->used; i++) {
-    e->bt_strings[i] = xbt_strdup(backtrace_syms[i]);
-    free(backtrace_syms[i]);
-  }
+  for (i = 0; i < e->used; i++)
+    e->bt_strings[i] = backtrace_syms[i];
 
   free(backtrace_syms);
 }
 
   free(backtrace_syms);
 }
@@ -139,13 +189,13 @@ int backtrace(void **buffer, int size)
   /* ebp points on stack base */
   /* esp points on stack pointer, ie on last stacked element (current element) */
   _asm call $ + 5
   /* ebp points on stack base */
   /* esp points on stack pointer, ie on last stacked element (current element) */
   _asm call $ + 5
-    _asm pop eax
-    _asm mov context.Eip, eax
-    _asm mov eax, esp
-    _asm mov context.Esp, eax
-    _asm mov context.Ebp, ebp dbg_hlp_init(GetCurrentProcess());
+  _asm pop eax
+  _asm mov context.Eip, eax
+  _asm mov eax, esp
+  _asm mov context.Esp, eax
+  _asm mov context.Ebp, ebp 
 
 
-  if ((NULL == dbg_hlp) || (size <= 0) || (NULL == buffer)) {
+  if ((NULL == hlp_dbg_instance) || (size <= 0) || (NULL == buffer)) {
     errno = EINVAL;
     return 0;
   }
     errno = EINVAL;
     return 0;
   }
@@ -167,11 +217,6 @@ int backtrace(void **buffer, int size)
   while (pos < size) {
     stack_frame = (void *) xbt_new0(STACKFRAME, 1);
 
   while (pos < size) {
     stack_frame = (void *) xbt_new0(STACKFRAME, 1);
 
-    if (!stack_frame) {
-      errno = ENOMEM;
-      break;
-    }
-
     stack_frame->AddrPC.Offset = context.Eip;
     stack_frame->AddrPC.Mode = AddrModeFlat;
 
     stack_frame->AddrPC.Offset = context.Eip;
     stack_frame->AddrPC.Mode = AddrModeFlat;
 
@@ -181,23 +226,19 @@ int backtrace(void **buffer, int size)
     stack_frame->AddrStack.Offset = context.Esp;
     stack_frame->AddrStack.Mode = AddrModeFlat;
 
     stack_frame->AddrStack.Offset = context.Esp;
     stack_frame->AddrStack.Mode = AddrModeFlat;
 
-    if ((*(dbg_hlp->stack_walk)) (IMAGE_FILE_MACHINE_I386,
-                                  dbg_hlp->process_handle,
-                                  GetCurrentThread(),
-                                  stack_frame,
-                                  &context,
-                                  NULL,
-                                  dbg_hlp->sym_function_table_access,
-                                  dbg_hlp->sym_get_module_base, NULL)
+    if ((*fun_stack_walk) (IMAGE_FILE_MACHINE_I386,
+                          process_handle,
+                          GetCurrentThread(),
+                          stack_frame,
+                          &context,
+                          NULL,
+                          fun_function_table_access,
+                          fun_get_module_base, NULL)
         && !first) {
       if (stack_frame->AddrReturn.Offset) {
 
         && !first) {
       if (stack_frame->AddrReturn.Offset) {
 
-        if ((*(dbg_hlp->sym_get_sym_from_addr))
-            (dbg_hlp->process_handle, stack_frame->AddrPC.Offset, &offset,
-             pSym)) {
-          if ((*(dbg_hlp->sym_get_line_from_addr))
-              (dbg_hlp->process_handle, stack_frame->AddrPC.Offset, &offset,
-               &line_info))
+        if ((*fun_get_sym_from_addr) (process_handle, stack_frame->AddrPC.Offset, &offset, pSym)) {
+          if ((*fun_get_line_from_addr) (process_handle, stack_frame->AddrPC.Offset, &offset, &line_info))
             buffer[pos++] = (void *) stack_frame;
         }
       } else {
             buffer[pos++] = (void *) stack_frame;
         }
       } else {
@@ -231,18 +272,13 @@ char **backtrace_symbols(void *const *buffer, int size)
     __buffer[(sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR) +
               sizeof(ULONG64) - 1) / sizeof(ULONG64)];
 
     __buffer[(sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR) +
               sizeof(ULONG64) - 1) / sizeof(ULONG64)];
 
-  if ((NULL == dbg_hlp) || (size <= 0) || (NULL == buffer)) {
+  if ((NULL == hlp_dbg_instance) || (size <= 0) || (NULL == buffer)) {
     errno = EINVAL;
     return NULL;
   }
 
   strings = xbt_new0(char *, size);
 
     errno = EINVAL;
     return NULL;
   }
 
   strings = xbt_new0(char *, size);
 
-  if (NULL == strings) {
-    errno = ENOMEM;
-    return NULL;
-  }
-
   pSym = (IMAGEHLP_SYMBOL *) __buffer;
 
   pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
   pSym = (IMAGEHLP_SYMBOL *) __buffer;
 
   pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
@@ -257,12 +293,8 @@ char **backtrace_symbols(void *const *buffer, int size)
 
     if (NULL != stack_frame) {
 
 
     if (NULL != stack_frame) {
 
-      if ((*(dbg_hlp->sym_get_sym_from_addr))
-          (dbg_hlp->process_handle, stack_frame->AddrPC.Offset, &offset,
-           pSym)) {
-        if ((*(dbg_hlp->sym_get_line_from_addr))
-            (dbg_hlp->process_handle, stack_frame->AddrPC.Offset, &offset,
-             &line_info)) {
+      if ((*fun_get_sym_from_addr) (process_handle, stack_frame->AddrPC.Offset, &offset, pSym)) {
+        if ((*fun_get_line_from_addr) (process_handle, stack_frame->AddrPC.Offset, &offset, &line_info)) {
           strings[pos] =
             bprintf("**   In %s() at %s:%d", pSym->Name, line_info.FileName,
                     line_info.LineNumber);
           strings[pos] =
             bprintf("**   In %s() at %s:%d", pSym->Name, line_info.FileName,
                     line_info.LineNumber);
@@ -284,157 +316,5 @@ char **backtrace_symbols(void *const *buffer, int size)
     strings = NULL;
   }
 
     strings = NULL;
   }
 
-  dbg_hlp_finalize();
-
   return strings;
 }
   return strings;
 }
-
-static int dbg_hlp_init(HANDLE process_handle)
-{
-  if (dbg_hlp) {
-    /* debug help is already loaded */
-    return 0;
-  }
-
-  /* allocation */
-  dbg_hlp = xbt_new0(s_xbt_debug_hlp_t, 1);
-
-  if (!dbg_hlp)
-    return ENOMEM;
-
-  /* load the library */
-  dbg_hlp->instance = LoadLibraryA("Dbghelp.dll");
-
-  if (!(dbg_hlp->instance)) {
-    free(dbg_hlp);
-    dbg_hlp = NULL;
-    return (int) GetLastError();
-  }
-
-  /* get the pointers to debug help library exported functions */
-
-  if (!
-      ((dbg_hlp->sym_initialize) =
-       (xbt_pfn_sym_initialize_t) GetProcAddress(dbg_hlp->instance,
-                                                 "SymInitialize"))) {
-    FreeLibrary(dbg_hlp->instance);
-    free(dbg_hlp);
-    dbg_hlp = NULL;
-    return (int) GetLastError();
-  }
-
-  if (!
-      ((dbg_hlp->sym_cleanup) =
-       (xbt_pfn_sym_cleanup_t) GetProcAddress(dbg_hlp->instance,
-                                              "SymCleanup"))) {
-    FreeLibrary(dbg_hlp->instance);
-    free(dbg_hlp);
-    dbg_hlp = NULL;
-    return (int) GetLastError();
-  }
-
-  if (!
-      ((dbg_hlp->sym_function_table_access) =
-       (xbt_pfn_sym_function_table_access_t) GetProcAddress(dbg_hlp->instance,
-                                                            "SymFunctionTableAccess")))
-  {
-    FreeLibrary(dbg_hlp->instance);
-    free(dbg_hlp);
-    dbg_hlp = NULL;
-    return (int) GetLastError();
-  }
-
-  if (!
-      ((dbg_hlp->sym_get_line_from_addr) =
-       (xbt_pfn_sym_get_line_from_addr_t) GetProcAddress(dbg_hlp->instance,
-                                                         "SymGetLineFromAddr")))
-  {
-    FreeLibrary(dbg_hlp->instance);
-    free(dbg_hlp);
-    dbg_hlp = NULL;
-    return (int) GetLastError();
-  }
-
-  if (!
-      ((dbg_hlp->sym_get_module_base) =
-       (xbt_pfn_sym_get_module_base_t) GetProcAddress(dbg_hlp->instance,
-                                                      "SymGetModuleBase"))) {
-    FreeLibrary(dbg_hlp->instance);
-    free(dbg_hlp);
-    dbg_hlp = NULL;
-    return (int) GetLastError();
-  }
-
-  if (!
-      ((dbg_hlp->sym_get_options) =
-       (xbt_pfn_sym_get_options_t) GetProcAddress(dbg_hlp->instance,
-                                                  "SymGetOptions"))) {
-    FreeLibrary(dbg_hlp->instance);
-    free(dbg_hlp);
-    dbg_hlp = NULL;
-    return (int) GetLastError();
-  }
-
-  if (!
-      ((dbg_hlp->sym_get_sym_from_addr) =
-       (xbt_pfn_sym_get_sym_from_addr_t) GetProcAddress(dbg_hlp->instance,
-                                                        "SymGetSymFromAddr")))
-  {
-    FreeLibrary(dbg_hlp->instance);
-    free(dbg_hlp);
-    dbg_hlp = NULL;
-    return (int) GetLastError();
-  }
-
-  if (!
-      ((dbg_hlp->sym_set_options) =
-       (xbt_pfn_sym_set_options_t) GetProcAddress(dbg_hlp->instance,
-                                                  "SymSetOptions"))) {
-    FreeLibrary(dbg_hlp->instance);
-    free(dbg_hlp);
-    dbg_hlp = NULL;
-    return (int) GetLastError();
-  }
-
-  if (!
-      ((dbg_hlp->stack_walk) =
-       (xbt_pfn_stack_walk_t) GetProcAddress(dbg_hlp->instance,
-                                             "StackWalk"))) {
-    FreeLibrary(dbg_hlp->instance);
-    free(dbg_hlp);
-    dbg_hlp = NULL;
-    return (int) GetLastError();
-  }
-
-  dbg_hlp->process_handle = process_handle;
-
-  (*(dbg_hlp->sym_set_options)) ((*(dbg_hlp->sym_get_options)) () |
-                                 SYMOPT_LOAD_LINES | SYMOPT_DEFERRED_LOADS);
-
-  if (!(*(dbg_hlp->sym_initialize)) (dbg_hlp->process_handle, 0, 1)) {
-    FreeLibrary(dbg_hlp->instance);
-    free(dbg_hlp);
-    dbg_hlp = NULL;
-    return (int) GetLastError();
-  }
-
-
-  return 0;
-}
-
-static int dbg_hlp_finalize(void)
-{
-  if (!dbg_hlp)
-    return EINVAL;
-
-  if (!(*(dbg_hlp->sym_cleanup)) (dbg_hlp->process_handle))
-    return (int) GetLastError();
-
-  if (!FreeLibrary(dbg_hlp->instance))
-    return (int) GetLastError();
-
-  free(dbg_hlp);
-  dbg_hlp = NULL;
-
-  return 0;
-}