[DBGHELP] Set LastError for SymGetModuleBase-calls to ERROR_MOD_NOT_FOUND.
[reactos.git] / dll / win32 / dbghelp / module.c
index 3848a45..aaa0ff6 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
-#include <config.h>
-//#include <stdlib.h>
-//#include <stdio.h>
-//#include <string.h>
+#include "config.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
 #include <assert.h>
 
 #include "dbghelp_private.h"
-#include <psapi.h>
-//#include "winternl.h"
-#include <wine/debug.h>
+#ifndef DBGHELP_STATIC_LIB
+#include "psapi.h"
+#include "winternl.h"
+#include "wine/debug.h"
+#endif
 
 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
 
@@ -56,7 +58,7 @@ static int match_ext(const WCHAR* ptr, size_t len)
     for (e = ext; *e; e++)
     {
         l = strlenW(*e);
-        if (l >= len) return FALSE;
+        if (l >= len) return 0;
         if (strncmpiW(&ptr[len - l], *e, l)) continue;
         return l;
     }
@@ -100,15 +102,14 @@ static void module_fill_module(const WCHAR* in, WCHAR* out, size_t size)
 
 void module_set_module(struct module* module, const WCHAR* name)
 {
-    module_fill_module(name, module->module.ModuleName, sizeof(module->module.ModuleName));
-    WideCharToMultiByte(CP_ACP, 0, module->module.ModuleName, -1,
-                        module->module_name, sizeof(module->module_name),
-                        NULL, NULL);
+    module_fill_module(name, module->module.ModuleName,
+            sizeof(module->module.ModuleName) / sizeof(module->module.ModuleName[0]));
+    module_fill_module(name, module->modulename, sizeof(module->modulename) / sizeof(module->modulename[0]));
 }
 
 const WCHAR *get_wine_loader_name(void)
 {
-    static const int is_win64 = sizeof(void *) > sizeof(int); /* FIXME: should depend on target process */
+    static const BOOL is_win64 = sizeof(void *) > sizeof(int); /* FIXME: should depend on target process */
     static const WCHAR wineW[] = {'w','i','n','e',0};
     static const WCHAR suffixW[] = {'6','4',0};
     static const WCHAR *loader;
@@ -209,7 +210,7 @@ struct module* module_new(struct process* pcs, const WCHAR* name,
 
     module->reloc_delta       = 0;
     module->type              = type;
-    module->is_virtual        = virtual ? TRUE : FALSE;
+    module->is_virtual        = virtual;
     for (i = 0; i < DFI_LAST; i++) module->format_info[i] = NULL;
     module->sortlist_valid    = FALSE;
     module->sorttab_size      = 0;
@@ -223,12 +224,15 @@ struct module* module_new(struct process* pcs, const WCHAR* name,
      */
     hash_table_init(&module->pool, &module->ht_symbols, 4096);
     hash_table_init(&module->pool, &module->ht_types,   4096);
+#ifdef __x86_64__
+    hash_table_init(&module->pool, &module->ht_symaddr, 4096);
+#endif
     vector_init(&module->vtypes, sizeof(struct symt*),  32);
 
     module->sources_used      = 0;
     module->sources_alloc     = 0;
     module->sources           = 0;
-    wine_rb_init(&module->sources_offsets_tree, &source_rb_functions);
+    wine_rb_init(&module->sources_offsets_tree, source_rb_compare);
 
     return module;
 }
@@ -349,9 +353,11 @@ BOOL module_get_debug(struct module_pair* pair)
         if (pair->effective->is_virtual) ret = FALSE;
         else switch (pair->effective->type)
         {
+#ifndef DBGHELP_STATIC_LIB
         case DMT_ELF:
             ret = elf_load_debug_info(pair->effective);
             break;
+#endif
         case DMT_PE:
             idslW64.SizeOfStruct = sizeof(idslW64);
             idslW64.BaseOfImage = pair->effective->module.BaseOfImage;
@@ -368,9 +374,11 @@ BOOL module_get_debug(struct module_pair* pair)
                          ret ? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE : CBA_DEFERRED_SYMBOL_LOAD_FAILURE,
                          &idslW64);
             break;
+#ifndef DBGHELP_STATIC_LIB
         case DMT_MACHO:
-            ret = macho_load_debug_info(pair->effective, NULL);
+            ret = macho_load_debug_info(pair->effective);
             break;
+#endif
         default:
             ret = FALSE;
             break;
@@ -388,7 +396,7 @@ BOOL module_get_debug(struct module_pair* pair)
  * either the addr where module is loaded, or any address inside the 
  * module
  */
-struct module* module_find_by_addr(const struct process* pcs, unsigned long addr, 
+struct module* module_find_by_addr(const struct process* pcs, DWORD64 addr,
                                    enum module_type type)
 {
     struct module*      module;
@@ -409,7 +417,7 @@ struct module* module_find_by_addr(const struct process* pcs, unsigned long addr
                 return module;
         }
     }
-    SetLastError(ERROR_INVALID_ADDRESS);
+    SetLastError(ERROR_MOD_NOT_FOUND);
     return module;
 }
 
@@ -464,7 +472,7 @@ enum module_type module_get_type_by_name(const WCHAR* name)
     {
         int i = len;
 
-        while (i && isdigit(name[i - 1])) i--;
+        while (i && name[i - 1] >= '0' && name[i - 1] <= '9') i--;
 
         if (i && name[i - 1] == '.')
             len = i - 1;
@@ -509,11 +517,13 @@ enum module_type module_get_type_by_name(const WCHAR* name)
 /******************************************************************
  *                             refresh_module_list
  */
+#ifndef DBGHELP_STATIC_LIB
 static BOOL refresh_module_list(struct process* pcs)
 {
     /* force transparent ELF and Mach-O loading / unloading */
     return elf_synchronize_module_list(pcs) || macho_synchronize_module_list(pcs);
 }
+#endif
 
 /***********************************************************************
  *                     SymLoadModule (DBGHELP.@)
@@ -597,7 +607,9 @@ DWORD64 WINAPI  SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageNam
     if (Flags & ~(SLMFLAG_VIRTUAL))
         FIXME("Unsupported Flags %08x for %s\n", Flags, debugstr_w(wImageName));
 
+#ifndef DBGHELP_STATIC_LIB
     refresh_module_list(pcs);
+#endif
 
     /* this is a Wine extension to the API just to redo the synchronisation */
     if (!wImageName && !hFile) return 0;
@@ -621,6 +633,7 @@ DWORD64 WINAPI  SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageNam
             wImageName)
         {
             /* and finally an ELF or Mach-O module */
+#ifndef DBGHELP_STATIC_LIB
             switch (module_get_type_by_name(wImageName))
             {
                 case DMT_ELF:
@@ -633,6 +646,7 @@ DWORD64 WINAPI  SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageNam
                     /* Ignored */
                     break;
             }
+#endif
         }
     }
     if (!module)
@@ -659,8 +673,8 @@ DWORD64 WINAPI  SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageNam
 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
                                PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
 {
-    if (!validate_addr64(BaseOfDll)) return FALSE;
-    return SymLoadModule(hProcess, hFile, ImageName, ModuleName, (DWORD)BaseOfDll, SizeOfDll);
+    return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll, SizeOfDll,
+                           NULL, 0);
 }
 
 /******************************************************************
@@ -682,7 +696,6 @@ BOOL module_remove(struct process* pcs, struct module* module)
     }
     hash_table_destroy(&module->ht_symbols);
     hash_table_destroy(&module->ht_types);
-    wine_rb_destroy(&module->sources_offsets_tree, NULL, NULL);
     HeapFree(GetProcessHeap(), 0, module->sources);
     HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
     pool_destroy(&module->pool);
@@ -730,7 +743,7 @@ BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
     pcs = process_find_by_handle(hProcess);
     if (!pcs) return FALSE;
     if (!validate_addr64(BaseOfDll)) return FALSE;
-    module = module_find_by_addr(pcs, (DWORD)BaseOfDll, DMT_UNKNOWN);
+    module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
     if (!module) return FALSE;
     return module_remove(pcs, module);
 }
@@ -815,13 +828,14 @@ BOOL  WINAPI SymEnumerateModulesW64(HANDLE hProcess,
         if (!(dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES) &&
             (module->type == DMT_ELF || module->type == DMT_MACHO))
             continue;
-        if (!EnumModulesCallback(module->module.ModuleName,
+        if (!EnumModulesCallback(module->modulename,
                                  module->module.BaseOfImage, UserContext))
             break;
     }
     return TRUE;
 }
 
+#ifndef DBGHELP_STATIC_LIB
 /******************************************************************
  *             EnumerateLoadedModules64 (DBGHELP.@)
  *
@@ -914,7 +928,7 @@ BOOL  WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
         if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
             !GetModuleBaseNameW(hProcess, hMods[i], baseW, sizeof(baseW) / sizeof(WCHAR)))
             continue;
-        module_fill_module(baseW, modW, sizeof(modW) / sizeof(CHAR));
+        module_fill_module(baseW, modW, sizeof(modW) / sizeof(modW[0]));
         EnumLoadedModulesCallback(modW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage,
                                   UserContext);
     }
@@ -923,6 +937,14 @@ BOOL  WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
     return sz != 0 && i == sz;
 }
 
+#endif /* DBGHELP_STATIC_LIB */
+
+static void dbghelp_str_WtoA(const WCHAR *src, char *dst, int dst_len)
+{
+    WideCharToMultiByte(CP_ACP, 0, src, -1, dst, dst_len - 1, NULL, NULL);
+    dst[dst_len - 1] = 0;
+}
+
 /******************************************************************
  *             SymGetModuleInfo (DBGHELP.@)
  *
@@ -938,19 +960,16 @@ BOOL  WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
     miw64.SizeOfStruct = sizeof(miw64);
     if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
 
-    mi.SizeOfStruct  = miw64.SizeOfStruct;
+    mi.SizeOfStruct  = ModuleInfo->SizeOfStruct;
     mi.BaseOfImage   = miw64.BaseOfImage;
     mi.ImageSize     = miw64.ImageSize;
     mi.TimeDateStamp = miw64.TimeDateStamp;
     mi.CheckSum      = miw64.CheckSum;
     mi.NumSyms       = miw64.NumSyms;
     mi.SymType       = miw64.SymType;
-    WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
-                        mi.ModuleName, sizeof(mi.ModuleName), NULL, NULL);
-    WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
-                        mi.ImageName, sizeof(mi.ImageName), NULL, NULL);
-    WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
-                        mi.LoadedImageName, sizeof(mi.LoadedImageName), NULL, NULL);
+    dbghelp_str_WtoA(miw64.ModuleName, mi.ModuleName, sizeof(mi.ModuleName));
+    dbghelp_str_WtoA(miw64.ImageName, mi.ImageName, sizeof(mi.ImageName));
+    dbghelp_str_WtoA(miw64.LoadedImageName, mi.LoadedImageName, sizeof(mi.LoadedImageName));
 
     memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
 
@@ -972,7 +991,7 @@ BOOL  WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
     miw64.SizeOfStruct = sizeof(miw64);
     if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
 
-    miw.SizeOfStruct  = miw64.SizeOfStruct;
+    miw.SizeOfStruct  = ModuleInfo->SizeOfStruct;
     miw.BaseOfImage   = miw64.BaseOfImage;
     miw.ImageSize     = miw64.ImageSize;
     miw.TimeDateStamp = miw64.TimeDateStamp;
@@ -1007,25 +1026,20 @@ BOOL  WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
     miw64.SizeOfStruct = sizeof(miw64);
     if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
 
-    mi64.SizeOfStruct  = miw64.SizeOfStruct;
+    mi64.SizeOfStruct  = ModuleInfo->SizeOfStruct;
     mi64.BaseOfImage   = miw64.BaseOfImage;
     mi64.ImageSize     = miw64.ImageSize;
     mi64.TimeDateStamp = miw64.TimeDateStamp;
     mi64.CheckSum      = miw64.CheckSum;
     mi64.NumSyms       = miw64.NumSyms;
     mi64.SymType       = miw64.SymType;
-    WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
-                        mi64.ModuleName, sizeof(mi64.ModuleName), NULL, NULL);
-    WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
-                        mi64.ImageName, sizeof(mi64.ImageName), NULL, NULL);
-    WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
-                        mi64.LoadedImageName, sizeof(mi64.LoadedImageName), NULL, NULL);
-    WideCharToMultiByte(CP_ACP, 0, miw64.LoadedPdbName, -1,
-                        mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName), NULL, NULL);
+    dbghelp_str_WtoA(miw64.ModuleName, mi64.ModuleName, sizeof(mi64.ModuleName));
+    dbghelp_str_WtoA(miw64.ImageName, mi64.ImageName, sizeof(mi64.ImageName));
+    dbghelp_str_WtoA(miw64.LoadedImageName, mi64.LoadedImageName, sizeof(mi64.LoadedImageName));
+    dbghelp_str_WtoA(miw64.LoadedPdbName, mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName));
 
     mi64.CVSig         = miw64.CVSig;
-    WideCharToMultiByte(CP_ACP, 0, miw64.CVData, -1,
-                        mi64.CVData, sizeof(mi64.CVData), NULL, NULL);
+    dbghelp_str_WtoA(miw64.CVData, mi64.CVData, sizeof(mi64.CVData));
     mi64.PdbSig        = miw64.PdbSig;
     mi64.PdbSig70      = miw64.PdbSig70;
     mi64.PdbAge        = miw64.PdbAge;
@@ -1134,7 +1148,11 @@ BOOL WINAPI SymRefreshModuleList(HANDLE hProcess)
 
     if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
 
+#ifndef DBGHELP_STATIC_LIB
     return refresh_module_list(pcs);
+#else
+    return TRUE;
+#endif
 }
 
 /***********************************************************************