2 * File module.c - module handling for the wine debugger
4 * Copyright (C) 1993, Eric Youngdale.
5 * 2000-2007, Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "dbghelp_private.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
35 const WCHAR S_ElfW[] = {'<','e','l','f','>','\0'};
36 const WCHAR S_WineLoaderW[] = {'<','w','i','n','e','-','l','o','a','d','e','r','>','\0'};
37 static const WCHAR S_DotSoW[] = {'.','s','o','\0'};
38 static const WCHAR S_DotDylibW[] = {'.','d','y','l','i','b','\0'};
39 static const WCHAR S_DotPdbW[] = {'.','p','d','b','\0'};
40 static const WCHAR S_DotDbgW[] = {'.','d','b','g','\0'};
41 const WCHAR S_WineW[] = {'w','i','n','e',0};
42 const WCHAR S_SlashW[] = {'/','\0'};
44 static const WCHAR S_AcmW[] = {'.','a','c','m','\0'};
45 static const WCHAR S_DllW[] = {'.','d','l','l','\0'};
46 static const WCHAR S_DrvW[] = {'.','d','r','v','\0'};
47 static const WCHAR S_ExeW[] = {'.','e','x','e','\0'};
48 static const WCHAR S_OcxW[] = {'.','o','c','x','\0'};
49 static const WCHAR S_VxdW[] = {'.','v','x','d','\0'};
50 static const WCHAR * const ext[] = {S_AcmW, S_DllW, S_DrvW, S_ExeW, S_OcxW, S_VxdW, NULL};
52 static int match_ext(const WCHAR* ptr, size_t len)
54 const WCHAR* const *e;
57 for (e = ext; *e; e++)
60 if (l >= len) return FALSE;
61 if (strncmpiW(&ptr[len - l], *e, l)) continue;
67 static const WCHAR* get_filename(const WCHAR* name, const WCHAR* endptr)
71 if (!endptr) endptr = name + strlenW(name);
72 for (ptr = endptr - 1; ptr >= name; ptr--)
74 if (*ptr == '/' || *ptr == '\\') break;
79 static void module_fill_module(const WCHAR* in, WCHAR* out, size_t size)
81 const WCHAR *ptr, *endptr;
84 ptr = get_filename(in, endptr = in + strlenW(in));
85 len = min(endptr - ptr, size - 1);
86 memcpy(out, ptr, len * sizeof(WCHAR));
88 if (len > 4 && (l = match_ext(out, len)))
90 else if (len > 4 && !strcmpiW(out + len - 4, S_WineW))
91 lstrcpynW(out, S_WineLoaderW, size);
94 if (len > 3 && !strcmpiW(&out[len - 3], S_DotSoW) &&
95 (l = match_ext(out, len - 3)))
96 strcpyW(&out[len - l - 3], S_ElfW);
98 while ((*out = tolowerW(*out))) out++;
101 void module_set_module(struct module* module, const WCHAR* name)
103 module_fill_module(name, module->module.ModuleName, sizeof(module->module.ModuleName));
104 WideCharToMultiByte(CP_ACP, 0, module->module.ModuleName, -1,
105 module->module_name, sizeof(module->module_name),
109 static const char* get_module_type(enum module_type type, BOOL virtual)
113 case DMT_ELF: return virtual ? "Virtual ELF" : "ELF";
114 case DMT_PE: return virtual ? "Virtual PE" : "PE";
115 case DMT_MACHO: return virtual ? "Virtual Mach-O" : "Mach-O";
116 default: return "---";
120 /***********************************************************************
121 * Creates and links a new module to a process
123 struct module* module_new(struct process* pcs, const WCHAR* name,
124 enum module_type type, BOOL virtual,
125 unsigned long mod_addr, unsigned long size,
126 unsigned long stamp, unsigned long checksum)
128 struct module* module;
130 assert(type == DMT_ELF || type == DMT_PE || type == DMT_MACHO);
131 if (!(module = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*module))))
134 module->next = pcs->lmodules;
135 pcs->lmodules = module;
137 TRACE("=> %s %08lx-%08lx %s\n",
138 get_module_type(type, virtual), mod_addr, mod_addr + size,
141 pool_init(&module->pool, 65536);
143 module->module.SizeOfStruct = sizeof(module->module);
144 module->module.BaseOfImage = mod_addr;
145 module->module.ImageSize = size;
146 module_set_module(module, name);
147 module->module.ImageName[0] = '\0';
148 lstrcpynW(module->module.LoadedImageName, name, sizeof(module->module.LoadedImageName) / sizeof(WCHAR));
149 module->module.SymType = SymNone;
150 module->module.NumSyms = 0;
151 module->module.TimeDateStamp = stamp;
152 module->module.CheckSum = checksum;
154 memset(module->module.LoadedPdbName, 0, sizeof(module->module.CVData));
155 module->module.CVSig = 0;
156 memset(module->module.CVData, 0, sizeof(module->module.CVData));
157 module->module.PdbSig = 0;
158 memset(&module->module.PdbSig70, 0, sizeof(module->module.PdbSig70));
159 module->module.PdbAge = 0;
160 module->module.PdbUnmatched = FALSE;
161 module->module.DbgUnmatched = FALSE;
162 module->module.LineNumbers = FALSE;
163 module->module.GlobalSymbols = FALSE;
164 module->module.TypeInfo = FALSE;
165 module->module.SourceIndexed = FALSE;
166 module->module.Publics = FALSE;
169 module->is_virtual = virtual ? TRUE : FALSE;
170 module->sortlist_valid = FALSE;
171 module->addr_sorttab = NULL;
172 module->num_sorttab = 0;
173 module->num_symbols = 0;
174 /* FIXME: this seems a bit too high (on a per module basis)
175 * need some statistics about this
177 hash_table_init(&module->pool, &module->ht_symbols, 4096);
178 hash_table_init(&module->pool, &module->ht_types, 4096);
179 vector_init(&module->vtypes, sizeof(struct symt*), 32);
181 module->sources_used = 0;
182 module->sources_alloc = 0;
188 /***********************************************************************
189 * module_find_by_name
192 static struct module* module_find_by_name(const struct process* pcs, const WCHAR* name)
194 struct module* module;
196 for (module = pcs->lmodules; module; module = module->next)
198 if (!strcmpiW(name, module->module.ModuleName)) return module;
200 SetLastError(ERROR_INVALID_NAME);
204 struct module* module_find_by_nameA(const struct process* pcs, const char* name)
206 WCHAR wname[MAX_PATH];
208 MultiByteToWideChar(CP_ACP, 0, name, -1, wname, sizeof(wname) / sizeof(WCHAR));
209 return module_find_by_name(pcs, wname);
212 /***********************************************************************
213 * module_is_already_loaded
216 struct module* module_is_already_loaded(const struct process* pcs, const WCHAR* name)
218 struct module* module;
219 const WCHAR* filename;
221 /* first compare the loaded image name... */
222 for (module = pcs->lmodules; module; module = module->next)
224 if (!strcmpiW(name, module->module.LoadedImageName))
227 /* then compare the standard filenames (without the path) ... */
228 filename = get_filename(name, NULL);
229 for (module = pcs->lmodules; module; module = module->next)
231 if (!strcmpiW(filename, get_filename(module->module.LoadedImageName, NULL)))
234 SetLastError(ERROR_INVALID_NAME);
238 /***********************************************************************
239 * module_get_container
242 static struct module* module_get_container(const struct process* pcs,
243 const struct module* inner)
245 struct module* module;
247 for (module = pcs->lmodules; module; module = module->next)
249 if (module != inner &&
250 module->module.BaseOfImage <= inner->module.BaseOfImage &&
251 module->module.BaseOfImage + module->module.ImageSize >=
252 inner->module.BaseOfImage + inner->module.ImageSize)
258 /***********************************************************************
259 * module_get_containee
262 struct module* module_get_containee(const struct process* pcs,
263 const struct module* outter)
265 struct module* module;
267 for (module = pcs->lmodules; module; module = module->next)
269 if (module != outter &&
270 outter->module.BaseOfImage <= module->module.BaseOfImage &&
271 outter->module.BaseOfImage + outter->module.ImageSize >=
272 module->module.BaseOfImage + module->module.ImageSize)
278 /******************************************************************
281 * get the debug information from a module:
282 * - if the module's type is deferred, then force loading of debug info (and return
284 * - if the module has no debug info and has an ELF container, then return the ELF
285 * container (and also force the ELF container's debug info loading if deferred)
286 * - otherwise return the module itself if it has some debug info
288 BOOL module_get_debug(struct module_pair* pair)
290 IMAGEHLP_DEFERRED_SYMBOL_LOADW64 idslW64;
292 if (!pair->requested) return FALSE;
293 /* for a PE builtin, always get info from container */
294 if (!(pair->effective = module_get_container(pair->pcs, pair->requested)))
295 pair->effective = pair->requested;
296 /* if deferred, force loading */
297 if (pair->effective->module.SymType == SymDeferred)
301 if (pair->effective->is_virtual) ret = FALSE;
302 else switch (pair->effective->type)
305 ret = elf_load_debug_info(pair->effective, NULL);
308 idslW64.SizeOfStruct = sizeof(idslW64);
309 idslW64.BaseOfImage = pair->effective->module.BaseOfImage;
310 idslW64.CheckSum = pair->effective->module.CheckSum;
311 idslW64.TimeDateStamp = pair->effective->module.TimeDateStamp;
312 memcpy(idslW64.FileName, pair->effective->module.ImageName,
313 sizeof(pair->effective->module.ImageName));
314 idslW64.Reparse = FALSE;
315 idslW64.hFile = INVALID_HANDLE_VALUE;
317 pcs_callback(pair->pcs, CBA_DEFERRED_SYMBOL_LOAD_START, &idslW64);
318 ret = pe_load_debug_info(pair->pcs, pair->effective);
319 pcs_callback(pair->pcs,
320 ret ? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE : CBA_DEFERRED_SYMBOL_LOAD_FAILURE,
324 ret = macho_load_debug_info(pair->effective, NULL);
330 if (!ret) pair->effective->module.SymType = SymNone;
331 assert(pair->effective->module.SymType != SymDeferred);
332 pair->effective->module.NumSyms = pair->effective->ht_symbols.num_elts;
334 return pair->effective->module.SymType != SymNone;
337 /***********************************************************************
338 * module_find_by_addr
340 * either the addr where module is loaded, or any address inside the
343 struct module* module_find_by_addr(const struct process* pcs, unsigned long addr,
344 enum module_type type)
346 struct module* module;
348 if (type == DMT_UNKNOWN)
350 if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
351 (module = module_find_by_addr(pcs, addr, DMT_ELF)) ||
352 (module = module_find_by_addr(pcs, addr, DMT_MACHO)))
357 for (module = pcs->lmodules; module; module = module->next)
359 if (type == module->type && addr >= module->module.BaseOfImage &&
360 addr < module->module.BaseOfImage + module->module.ImageSize)
364 SetLastError(ERROR_INVALID_ADDRESS);
368 /******************************************************************
369 * module_is_container_loaded
371 * checks whether the native container, for a (supposed) PE builtin is
374 static BOOL module_is_container_loaded(const struct process* pcs,
375 const WCHAR* ImageName, DWORD base)
378 struct module* module;
379 PCWSTR filename, modname;
381 if (!base) return FALSE;
382 filename = get_filename(ImageName, NULL);
383 len = strlenW(filename);
385 for (module = pcs->lmodules; module; module = module->next)
387 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
388 base >= module->module.BaseOfImage &&
389 base < module->module.BaseOfImage + module->module.ImageSize)
391 modname = get_filename(module->module.LoadedImageName, NULL);
392 if (!strncmpiW(modname, filename, len) &&
393 !memcmp(modname + len, S_DotSoW, 3 * sizeof(WCHAR)))
399 /* likely a native PE module */
400 WARN("Couldn't find container for %s\n", debugstr_w(ImageName));
404 /******************************************************************
405 * module_get_type_by_name
407 * Guesses a filename type from its extension
409 enum module_type module_get_type_by_name(const WCHAR* name)
411 int len = strlenW(name);
413 /* Skip all version extensions (.[digits]) regex: "(\.\d+)*$" */
418 while (i && isdigit(name[i - 1])) i--;
420 if (i && name[i - 1] == '.')
426 /* check for terminating .so or .so.[digit] */
427 /* FIXME: Can't rely solely on extension; have to check magic or
428 * stop using .so on Mac OS X. For now, base on platform. */
429 if (len > 3 && !memcmp(name + len - 3, S_DotSoW, 3))
436 if (len > 6 && !strncmpiW(name + len - 6, S_DotDylibW, 6))
439 if (len > 4 && !strncmpiW(name + len - 4, S_DotPdbW, 4))
442 if (len > 4 && !strncmpiW(name + len - 4, S_DotDbgW, 4))
445 /* wine is also a native module (Mach-O on Mac OS X, ELF elsewhere) */
446 if (((len > 4 && name[len - 5] == '/') || len == 4) && !strcmpiW(name + len - 4, S_WineW))
457 /***********************************************************************
458 * SymLoadModule (DBGHELP.@)
460 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
461 PCSTR ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
463 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll,
467 /***********************************************************************
468 * SymLoadModuleEx (DBGHELP.@)
470 DWORD64 WINAPI SymLoadModuleEx(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
471 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD DllSize,
472 PMODLOAD_DATA Data, DWORD Flags)
474 PWSTR wImageName, wModuleName;
478 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
479 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
480 wine_dbgstr_longlong(BaseOfDll), DllSize, Data, Flags);
484 len = MultiByteToWideChar(CP_ACP, 0, ImageName, -1, NULL, 0);
485 wImageName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
486 MultiByteToWideChar(CP_ACP, 0, ImageName, -1, wImageName, len);
488 else wImageName = NULL;
491 len = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
492 wModuleName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
493 MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, wModuleName, len);
495 else wModuleName = NULL;
497 ret = SymLoadModuleExW(hProcess, hFile, wImageName, wModuleName,
498 BaseOfDll, DllSize, Data, Flags);
499 HeapFree(GetProcessHeap(), 0, wImageName);
500 HeapFree(GetProcessHeap(), 0, wModuleName);
504 /***********************************************************************
505 * SymLoadModuleExW (DBGHELP.@)
507 DWORD64 WINAPI SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageName,
508 PCWSTR wModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll,
509 PMODLOAD_DATA Data, DWORD Flags)
512 struct module* module = NULL;
514 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
515 hProcess, hFile, debugstr_w(wImageName), debugstr_w(wModuleName),
516 wine_dbgstr_longlong(BaseOfDll), SizeOfDll, Data, Flags);
519 FIXME("Unsupported load data parameter %p for %s\n",
520 Data, debugstr_w(wImageName));
521 if (!validate_addr64(BaseOfDll)) return FALSE;
523 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
525 if (Flags & SLMFLAG_VIRTUAL)
527 module = module_new(pcs, wImageName, module_get_type_by_name(wImageName),
528 TRUE, (DWORD)BaseOfDll, SizeOfDll, 0, 0);
529 if (!module) return FALSE;
530 if (wModuleName) module_set_module(module, wModuleName);
531 module->module.SymType = SymVirtual;
535 if (Flags & ~(SLMFLAG_VIRTUAL))
536 FIXME("Unsupported Flags %08x for %s\n", Flags, debugstr_w(wImageName));
538 /* force transparent ELF and Mach-O loading / unloading */
539 elf_synchronize_module_list(pcs);
540 macho_synchronize_module_list(pcs);
542 /* this is a Wine extension to the API just to redo the synchronisation */
543 if (!wImageName && !hFile) return 0;
545 /* check if the module is already loaded, or if it's a builtin PE module with
546 * an containing ELF module
550 module = module_is_already_loaded(pcs, wImageName);
551 if (!module && module_is_container_loaded(pcs, wImageName, BaseOfDll))
553 /* force the loading of DLL as builtin */
554 module = pe_load_builtin_module(pcs, wImageName, BaseOfDll, SizeOfDll);
559 /* otherwise, try a regular PE module */
560 if (!(module = pe_load_native_module(pcs, wImageName, hFile, BaseOfDll, SizeOfDll)) &&
563 /* and finally an ELF or Mach-O module */
564 switch (module_get_type_by_name(wImageName))
567 module = elf_load_module(pcs, wImageName, BaseOfDll);
570 module = macho_load_module(pcs, wImageName, BaseOfDll);
580 WARN("Couldn't locate %s\n", debugstr_w(wImageName));
583 module->module.NumSyms = module->ht_symbols.num_elts;
584 /* by default module_new fills module.ModuleName from a derivation
585 * of LoadedImageName. Overwrite it, if we have better information
588 module_set_module(module, wModuleName);
589 lstrcpynW(module->module.ImageName, wImageName,
590 sizeof(module->module.ImageName) / sizeof(WCHAR));
592 return module->module.BaseOfImage;
595 /***********************************************************************
596 * SymLoadModule64 (DBGHELP.@)
598 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
599 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
601 if (!validate_addr64(BaseOfDll)) return FALSE;
602 return SymLoadModule(hProcess, hFile, ImageName, ModuleName, (DWORD)BaseOfDll, SizeOfDll);
605 /******************************************************************
609 BOOL module_remove(struct process* pcs, struct module* module)
613 TRACE("%s (%p)\n", debugstr_w(module->module.ModuleName), module);
614 hash_table_destroy(&module->ht_symbols);
615 hash_table_destroy(&module->ht_types);
616 HeapFree(GetProcessHeap(), 0, module->sources);
617 HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
618 HeapFree(GetProcessHeap(), 0, module->dwarf2_info);
619 pool_destroy(&module->pool);
620 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
623 for (p = &pcs->lmodules; *p; p = &(*p)->next)
628 HeapFree(GetProcessHeap(), 0, module);
632 FIXME("This shouldn't happen\n");
636 /******************************************************************
637 * SymUnloadModule (DBGHELP.@)
640 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
643 struct module* module;
645 pcs = process_find_by_handle(hProcess);
646 if (!pcs) return FALSE;
647 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
648 if (!module) return FALSE;
649 return module_remove(pcs, module);
652 /******************************************************************
653 * SymUnloadModule64 (DBGHELP.@)
656 BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
659 struct module* module;
661 pcs = process_find_by_handle(hProcess);
662 if (!pcs) return FALSE;
663 if (!validate_addr64(BaseOfDll)) return FALSE;
664 module = module_find_by_addr(pcs, (DWORD)BaseOfDll, DMT_UNKNOWN);
665 if (!module) return FALSE;
666 return module_remove(pcs, module);
669 /******************************************************************
670 * SymEnumerateModules (DBGHELP.@)
673 struct enum_modW64_32
675 PSYM_ENUMMODULES_CALLBACK cb;
677 char module[MAX_PATH];
680 static BOOL CALLBACK enum_modW64_32(PCWSTR name, DWORD64 base, PVOID user)
682 struct enum_modW64_32* x = user;
684 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
685 return x->cb(x->module, (DWORD)base, x->user);
688 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
689 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
692 struct enum_modW64_32 x;
694 x.cb = EnumModulesCallback;
695 x.user = UserContext;
697 return SymEnumerateModulesW64(hProcess, enum_modW64_32, &x);
700 /******************************************************************
701 * SymEnumerateModules64 (DBGHELP.@)
704 struct enum_modW64_64
706 PSYM_ENUMMODULES_CALLBACK64 cb;
708 char module[MAX_PATH];
711 static BOOL CALLBACK enum_modW64_64(PCWSTR name, DWORD64 base, PVOID user)
713 struct enum_modW64_64* x = user;
715 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
716 return x->cb(x->module, base, x->user);
719 BOOL WINAPI SymEnumerateModules64(HANDLE hProcess,
720 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,
723 struct enum_modW64_64 x;
725 x.cb = EnumModulesCallback;
726 x.user = UserContext;
728 return SymEnumerateModulesW64(hProcess, enum_modW64_64, &x);
731 /******************************************************************
732 * SymEnumerateModulesW64 (DBGHELP.@)
735 BOOL WINAPI SymEnumerateModulesW64(HANDLE hProcess,
736 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback,
739 struct process* pcs = process_find_by_handle(hProcess);
740 struct module* module;
742 if (!pcs) return FALSE;
744 for (module = pcs->lmodules; module; module = module->next)
746 if (!(dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES) &&
747 (module->type == DMT_ELF || module->type == DMT_MACHO))
749 if (!EnumModulesCallback(module->module.ModuleName,
750 module->module.BaseOfImage, UserContext))
756 /******************************************************************
757 * EnumerateLoadedModules64 (DBGHELP.@)
760 struct enum_load_modW64_64
762 PENUMLOADED_MODULES_CALLBACK64 cb;
764 char module[MAX_PATH];
767 static BOOL CALLBACK enum_load_modW64_64(PCWSTR name, DWORD64 base, ULONG size,
770 struct enum_load_modW64_64* x = user;
772 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
773 return x->cb(x->module, base, size, x->user);
776 BOOL WINAPI EnumerateLoadedModules64(HANDLE hProcess,
777 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
780 struct enum_load_modW64_64 x;
782 x.cb = EnumLoadedModulesCallback;
783 x.user = UserContext;
785 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_64, &x);
788 /******************************************************************
789 * EnumerateLoadedModules (DBGHELP.@)
792 struct enum_load_modW64_32
794 PENUMLOADED_MODULES_CALLBACK cb;
796 char module[MAX_PATH];
799 static BOOL CALLBACK enum_load_modW64_32(PCWSTR name, DWORD64 base, ULONG size,
802 struct enum_load_modW64_32* x = user;
803 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
804 return x->cb(x->module, (DWORD)base, size, x->user);
807 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
808 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
811 struct enum_load_modW64_32 x;
813 x.cb = EnumLoadedModulesCallback;
814 x.user = UserContext;
816 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_32, &x);
819 /******************************************************************
820 * EnumerateLoadedModulesW64 (DBGHELP.@)
823 BOOL WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
824 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback,
828 WCHAR baseW[256], modW[256];
832 hMods = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods[0]));
833 if (!hMods) return FALSE;
835 if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
837 /* hProcess should also be a valid process handle !! */
838 FIXME("If this happens, bump the number in mod\n");
839 HeapFree(GetProcessHeap(), 0, hMods);
842 sz /= sizeof(HMODULE);
843 for (i = 0; i < sz; i++)
845 if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
846 !GetModuleBaseNameW(hProcess, hMods[i], baseW, sizeof(baseW) / sizeof(WCHAR)))
848 module_fill_module(baseW, modW, sizeof(modW) / sizeof(CHAR));
849 EnumLoadedModulesCallback(modW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage,
852 HeapFree(GetProcessHeap(), 0, hMods);
854 return sz != 0 && i == sz;
857 /******************************************************************
858 * SymGetModuleInfo (DBGHELP.@)
861 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
862 PIMAGEHLP_MODULE ModuleInfo)
865 IMAGEHLP_MODULEW64 miw64;
867 if (sizeof(mi) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
869 miw64.SizeOfStruct = sizeof(miw64);
870 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
872 mi.SizeOfStruct = miw64.SizeOfStruct;
873 mi.BaseOfImage = miw64.BaseOfImage;
874 mi.ImageSize = miw64.ImageSize;
875 mi.TimeDateStamp = miw64.TimeDateStamp;
876 mi.CheckSum = miw64.CheckSum;
877 mi.NumSyms = miw64.NumSyms;
878 mi.SymType = miw64.SymType;
879 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
880 mi.ModuleName, sizeof(mi.ModuleName), NULL, NULL);
881 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
882 mi.ImageName, sizeof(mi.ImageName), NULL, NULL);
883 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
884 mi.LoadedImageName, sizeof(mi.LoadedImageName), NULL, NULL);
886 memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
891 /******************************************************************
892 * SymGetModuleInfoW (DBGHELP.@)
895 BOOL WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
896 PIMAGEHLP_MODULEW ModuleInfo)
898 IMAGEHLP_MODULEW64 miw64;
899 IMAGEHLP_MODULEW miw;
901 if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
903 miw64.SizeOfStruct = sizeof(miw64);
904 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
906 miw.SizeOfStruct = miw64.SizeOfStruct;
907 miw.BaseOfImage = miw64.BaseOfImage;
908 miw.ImageSize = miw64.ImageSize;
909 miw.TimeDateStamp = miw64.TimeDateStamp;
910 miw.CheckSum = miw64.CheckSum;
911 miw.NumSyms = miw64.NumSyms;
912 miw.SymType = miw64.SymType;
913 strcpyW(miw.ModuleName, miw64.ModuleName);
914 strcpyW(miw.ImageName, miw64.ImageName);
915 strcpyW(miw.LoadedImageName, miw64.LoadedImageName);
916 memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
921 /******************************************************************
922 * SymGetModuleInfo64 (DBGHELP.@)
925 BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
926 PIMAGEHLP_MODULE64 ModuleInfo)
928 IMAGEHLP_MODULE64 mi64;
929 IMAGEHLP_MODULEW64 miw64;
931 if (sizeof(mi64) < ModuleInfo->SizeOfStruct)
933 SetLastError(ERROR_MOD_NOT_FOUND); /* NOTE: native returns this error */
934 WARN("Wrong size %u\n", ModuleInfo->SizeOfStruct);
938 miw64.SizeOfStruct = sizeof(miw64);
939 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
941 mi64.SizeOfStruct = miw64.SizeOfStruct;
942 mi64.BaseOfImage = miw64.BaseOfImage;
943 mi64.ImageSize = miw64.ImageSize;
944 mi64.TimeDateStamp = miw64.TimeDateStamp;
945 mi64.CheckSum = miw64.CheckSum;
946 mi64.NumSyms = miw64.NumSyms;
947 mi64.SymType = miw64.SymType;
948 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
949 mi64.ModuleName, sizeof(mi64.ModuleName), NULL, NULL);
950 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
951 mi64.ImageName, sizeof(mi64.ImageName), NULL, NULL);
952 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
953 mi64.LoadedImageName, sizeof(mi64.LoadedImageName), NULL, NULL);
954 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedPdbName, -1,
955 mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName), NULL, NULL);
957 mi64.CVSig = miw64.CVSig;
958 WideCharToMultiByte(CP_ACP, 0, miw64.CVData, -1,
959 mi64.CVData, sizeof(mi64.CVData), NULL, NULL);
960 mi64.PdbSig = miw64.PdbSig;
961 mi64.PdbSig70 = miw64.PdbSig70;
962 mi64.PdbAge = miw64.PdbAge;
963 mi64.PdbUnmatched = miw64.PdbUnmatched;
964 mi64.DbgUnmatched = miw64.DbgUnmatched;
965 mi64.LineNumbers = miw64.LineNumbers;
966 mi64.GlobalSymbols = miw64.GlobalSymbols;
967 mi64.TypeInfo = miw64.TypeInfo;
968 mi64.SourceIndexed = miw64.SourceIndexed;
969 mi64.Publics = miw64.Publics;
971 memcpy(ModuleInfo, &mi64, ModuleInfo->SizeOfStruct);
976 /******************************************************************
977 * SymGetModuleInfoW64 (DBGHELP.@)
980 BOOL WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr,
981 PIMAGEHLP_MODULEW64 ModuleInfo)
983 struct process* pcs = process_find_by_handle(hProcess);
984 struct module* module;
985 IMAGEHLP_MODULEW64 miw64;
987 TRACE("%p %s %p\n", hProcess, wine_dbgstr_longlong(dwAddr), ModuleInfo);
989 if (!pcs) return FALSE;
990 if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
991 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
992 if (!module) return FALSE;
994 miw64 = module->module;
996 /* update debug information from container if any */
997 if (module->module.SymType == SymNone)
999 module = module_get_container(pcs, module);
1000 if (module && module->module.SymType != SymNone)
1002 miw64.SymType = module->module.SymType;
1003 miw64.NumSyms = module->module.NumSyms;
1006 memcpy(ModuleInfo, &miw64, ModuleInfo->SizeOfStruct);
1010 /***********************************************************************
1011 * SymGetModuleBase (DBGHELP.@)
1013 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
1015 struct process* pcs = process_find_by_handle(hProcess);
1016 struct module* module;
1019 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1020 if (!module) return 0;
1021 return module->module.BaseOfImage;
1024 /***********************************************************************
1025 * SymGetModuleBase64 (DBGHELP.@)
1027 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
1029 if (!validate_addr64(dwAddr)) return 0;
1030 return SymGetModuleBase(hProcess, (DWORD)dwAddr);
1033 /******************************************************************
1034 * module_reset_debug_info
1035 * Removes any debug information linked to a given module.
1037 void module_reset_debug_info(struct module* module)
1039 module->sortlist_valid = TRUE;
1040 module->addr_sorttab = NULL;
1041 module->num_sorttab = module->num_symbols = 0;
1042 hash_table_destroy(&module->ht_symbols);
1043 module->ht_symbols.num_buckets = 0;
1044 module->ht_symbols.buckets = NULL;
1045 hash_table_destroy(&module->ht_types);
1046 module->ht_types.num_buckets = 0;
1047 module->ht_types.buckets = NULL;
1048 module->vtypes.num_elts = 0;
1049 hash_table_destroy(&module->ht_symbols);
1050 module->sources_used = module->sources_alloc = 0;
1051 module->sources = NULL;