error_handler: unify stack walking logic
authorSeokYeon Hwang <syeon.hwang@samsung.com>
Wed, 21 Dec 2016 04:27:49 +0000 (13:27 +0900)
committerSeokYeon Hwang <syeon.hwang@samsung.com>
Wed, 21 Dec 2016 04:27:49 +0000 (13:27 +0900)
Change-Id: I5c82ecd08085af25b560f3c57b4abbc974f42e3d
Signed-off-by: SeokYeon Hwang <syeon.hwang@samsung.com>
tizen/src/util/error_handler.c

index 959594e..a6348ac 100644 (file)
@@ -67,29 +67,9 @@ void enable_print_backtrace_at_normal_exit(void)
 #ifdef CONFIG_WIN32
 static LPTOP_LEVEL_EXCEPTION_FILTER prevExceptionFilter;
 
-/* The MSDN says as followed in "Updated Platform Support" page,
-   (https://msdn.microsoft.com/en-us/library/windows/desktop/
-    ms681408(v=vs.85).aspx)
-    "Where necessary, the DbgHelp library has been widened to support both 32-
-     and 64-bit Windows. The original function and structure definitions are
-     still in DbgHelp.h, but there are also updated versions of these
-     definitions that are compatible with 64-bit Windows. If you use the updated
-     functions in your code, it can be compiled for both 32- and 64-bit Windows.
-     Your code will also be more efficient, since the original functions simply
-     call the updated functions to perform the work."
-    However, using the updated functinos does not work on the Windows 32-bit.
-    IMHO, in the MinGW cross compile environment rather than the Visual Studio
-    it does not compile correctly.
-    Thus, use explicitly.
-*/
 static void dump_backtrace(void *ptr)
 {
-#ifdef _WIN64
     STACKFRAME64 frame;
-#else
-    STACKFRAME frame;
-#endif
-    int i;
     DWORD image;
     CONTEXT context;
     HANDLE hProcess = GetCurrentProcess();
@@ -105,8 +85,8 @@ static void dump_backtrace(void *ptr)
 
     SymInitialize(hProcess, NULL, TRUE);
 
-#ifdef _WIN64
     ZeroMemory(&frame, sizeof(STACKFRAME64));
+#ifdef _WIN64
     image = IMAGE_FILE_MACHINE_AMD64;
     frame.AddrPC.Offset = context.Rip;
     frame.AddrPC.Mode = AddrModeFlat;
@@ -115,7 +95,6 @@ static void dump_backtrace(void *ptr)
     frame.AddrStack.Offset = context.Rsp;
     frame.AddrStack.Mode = AddrModeFlat;
 #else
-    ZeroMemory(&frame, sizeof(STACKFRAME));
     image = IMAGE_FILE_MACHINE_I386;
     frame.AddrPC.Offset = context.Eip;
     frame.AddrPC.Mode = AddrModeFlat;
@@ -125,60 +104,41 @@ static void dump_backtrace(void *ptr)
     frame.AddrStack.Mode = AddrModeFlat;
 #endif
 
-    i = 0;
-    while (1) {
-#ifdef _WIN64
-        BOOL result = StackWalk64(image, hProcess, hThread,
+    int i = 0;
+    BOOL result = FALSE;
+    while ((result = StackWalk64(image, hProcess, hThread,
                                   &frame, &context, NULL,
                                   SymFunctionTableAccess64,
-                                  SymGetModuleBase64, NULL);
-#else
-        BOOL result = StackWalk(image, hProcess, hThread,
-                                &frame, &context, NULL,
-                                SymFunctionTableAccess,
-                                SymGetModuleBase, NULL);
-#endif
-        if (!result) {
-            break;
-        }
+                                  SymGetModuleBase64, NULL)) == TRUE) {
         TCHAR buffer[sizeof(SYMBOL_INFO) + (MAX_SYM_NAME - 1) * sizeof(TCHAR)];
         PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;
         pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
         pSymbol->MaxNameLen = MAX_SYM_NAME;
         DWORD64 displacement = 0;
         TCHAR pFileName[MAX_PATH] = {0, };
-#ifdef _WIN64
         DWORD64 dwBase = SymGetModuleBase64(hProcess, frame.AddrPC.Offset);
-#else
-        DWORD dwBase = SymGetModuleBase(hProcess, frame.AddrPC.Offset);
-#endif
         if (dwBase) {
-            HMODULE hModule = (HMODULE)((DWORD_PTR)dwBase);
-            if (!GetModuleFileNameA(hModule, pFileName, MAX_PATH)) {
+            if (!GetModuleFileNameA((HMODULE)(intptr_t)dwBase, pFileName,
+                        MAX_PATH)) {
                 snprintf(pFileName, MAX_PATH, "Unknown Module");
             }
         }
         /* TODO: take the symbols for the static functions
                  without import .pdb */
-        if (SymFromAddr(hProcess,
-                        frame.AddrPC.Offset,
-                        &displacement, pSymbol)) {
-#ifdef _WIN64
-            LOG_INFO("#%04d 0x%016I64x in %s from %s\n",
-#else
-            LOG_INFO("#%04d 0x%08x in %s from %s\n",
-#endif
-                        i, frame.AddrPC.Offset, pSymbol->Name, pFileName);
-        } else {
+        if (!SymFromAddr(hProcess,
+                    frame.AddrPC.Offset,
+                    &displacement, pSymbol)) {
+            g_strlcpy(pSymbol->Name, "????????", MAX_SYM_NAME);
+        }
 #ifdef _WIN64
-            LOG_INFO("#%04d 0x%016I64x in ???????? from %s\n",
+        LOG_INFO("#%04d 0x%016" PRIx64 " in %s from %s\n",
 #else
-            LOG_INFO("#%04d 0x%08x in ???????? from %s\n",
+        LOG_INFO("#%04d 0x%08" PRIx64 " in %s from %s\n",
 #endif
-                        i, frame.AddrPC.Offset, pFileName);
-        }
+                i, frame.AddrPC.Offset, pSymbol->Name, pFileName);
         i++;
     }
+
     SymCleanup(hProcess);
 }