Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cleanups: kill code dupplication
[simgrid.git] / src / xbt / backtrace_windows.c
index 16cdd74a4b511bd1f7e99238ffcd6f795c995d03..3d066974470e7aeffacbe90906f23e7154d90438 100644 (file)
@@ -10,8 +10,8 @@
  * 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>
@@ -139,11 +139,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
-    _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 
+
+  dbg_hlp_init(GetCurrentProcess());
 
   if ((NULL == dbg_hlp) || (size <= 0) || (NULL == buffer)) {
     errno = EINVAL;
@@ -167,11 +169,6 @@ int backtrace(void **buffer, int size)
   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;
 
@@ -238,11 +235,6 @@ char **backtrace_symbols(void *const *buffer, int size)
 
   strings = xbt_new0(char *, size);
 
-  if (NULL == strings) {
-    errno = ENOMEM;
-    return NULL;
-  }
-
   pSym = (IMAGEHLP_SYMBOL *) __buffer;
 
   pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
@@ -299,107 +291,54 @@ static int dbg_hlp_init(HANDLE process_handle)
   /* 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)) {
+  if (!dbg_hlp->instance) {
     free(dbg_hlp);
     dbg_hlp = NULL;
     return (int) GetLastError();
   }
-
   /* get the pointers to debug help library exported functions */
+  dbg_hlp->sym_initialize =
+    (xbt_pfn_sym_initialize_t) GetProcAddress(dbg_hlp->instance, "SymInitialize");
 
-  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();
-  }
+  dbg_hlp->sym_cleanup = 
+    (xbt_pfn_sym_cleanup_t) GetProcAddress(dbg_hlp->instance, "SymCleanup");
 
-  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();
-  }
+  dbg_hlp->sym_function_table_access =
+    (xbt_pfn_sym_function_table_access_t) GetProcAddress(dbg_hlp->instance, "SymFunctionTableAccess");
 
-  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();
-  }
+  dbg_hlp->sym_get_line_from_addr =
+    (xbt_pfn_sym_get_line_from_addr_t) GetProcAddress(dbg_hlp->instance, "SymGetLineFromAddr");
 
-  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();
-  }
+  dbg_hlp->sym_get_module_base =
+    (xbt_pfn_sym_get_module_base_t) GetProcAddress(dbg_hlp->instance, "SymGetModuleBase");
 
-  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();
-  }
+  dbg_hlp->sym_get_options =
+    (xbt_pfn_sym_get_options_t) GetProcAddress(dbg_hlp->instance, "SymGetOptions");
 
-  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();
-  }
+  dbg_hlp->sym_get_sym_from_addr =
+    (xbt_pfn_sym_get_sym_from_addr_t) GetProcAddress(dbg_hlp->instance, "SymGetSymFromAddr");
 
-  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();
-  }
+  dbg_hlp->sym_set_options =
+    (xbt_pfn_sym_set_options_t) GetProcAddress(dbg_hlp->instance, "SymSetOptions");
 
-  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();
-  }
+  dbg_hlp->stack_walk =
+    (xbt_pfn_stack_walk_t) GetProcAddress(dbg_hlp->instance, "StackWalk");
 
-  if (!
-      ((dbg_hlp->stack_walk) =
-       (xbt_pfn_stack_walk_t) GetProcAddress(dbg_hlp->instance,
-                                             "StackWalk"))) {
+  /* Check that everything worked well */
+  if (!dbg_hlp->sym_initialize ||
+      !dbg_hlp->sym_cleanup ||
+      !dbg_hlp->sym_function_table_access || 
+      !dbg_hlp->sym_get_line_from_addr ||
+      !dbg_hlp->sym_get_module_base ||
+      !dbg_hlp->sym_get_options ||
+      !dbg_hlp->sym_get_sym_from_addr ||
+      !dbg_hlp->sym_set_options ||
+      !dbg_hlp->stack_walk
+      ) {
     FreeLibrary(dbg_hlp->instance);
     free(dbg_hlp);
     dbg_hlp = NULL;