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 DWORD64 mod_addr, DWORD64 size,
126 unsigned long stamp, unsigned long checksum)
128 struct module* module;
131 assert(type == DMT_ELF || type == DMT_PE || type == DMT_MACHO);
132 if (!(module = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*module))))
135 module->next = pcs->lmodules;
136 pcs->lmodules = module;
138 TRACE("=> %s %s-%s %s\n",
139 get_module_type(type, virtual),
140 wine_dbgstr_longlong(mod_addr), wine_dbgstr_longlong(mod_addr + size),
143 pool_init(&module->pool, 65536);
145 module->module.SizeOfStruct = sizeof(module->module);
146 module->module.BaseOfImage = mod_addr;
147 module->module.ImageSize = size;
148 module_set_module(module, name);
149 module->module.ImageName[0] = '\0';
150 lstrcpynW(module->module.LoadedImageName, name, sizeof(module->module.LoadedImageName) / sizeof(WCHAR));
151 module->module.SymType = SymNone;
152 module->module.NumSyms = 0;
153 module->module.TimeDateStamp = stamp;
154 module->module.CheckSum = checksum;
156 memset(module->module.LoadedPdbName, 0, sizeof(module->module.CVData));
157 module->module.CVSig = 0;
158 memset(module->module.CVData, 0, sizeof(module->module.CVData));
159 module->module.PdbSig = 0;
160 memset(&module->module.PdbSig70, 0, sizeof(module->module.PdbSig70));
161 module->module.PdbAge = 0;
162 module->module.PdbUnmatched = FALSE;
163 module->module.DbgUnmatched = FALSE;
164 module->module.LineNumbers = FALSE;
165 module->module.GlobalSymbols = FALSE;
166 module->module.TypeInfo = FALSE;
167 module->module.SourceIndexed = FALSE;
168 module->module.Publics = FALSE;
170 module->reloc_delta = 0;
172 module->is_virtual = virtual ? TRUE : FALSE;
173 for (i = 0; i < DFI_LAST; i++) module->format_info[i] = NULL;
174 module->sortlist_valid = FALSE;
175 module->sorttab_size = 0;
176 module->addr_sorttab = NULL;
177 module->num_sorttab = 0;
178 module->num_symbols = 0;
180 vector_init(&module->vsymt, sizeof(struct symt*), 128);
181 /* FIXME: this seems a bit too high (on a per module basis)
182 * need some statistics about this
184 hash_table_init(&module->pool, &module->ht_symbols, 4096);
185 hash_table_init(&module->pool, &module->ht_types, 4096);
186 vector_init(&module->vtypes, sizeof(struct symt*), 32);
188 module->sources_used = 0;
189 module->sources_alloc = 0;
195 /***********************************************************************
196 * module_find_by_name
199 static struct module* module_find_by_name(const struct process* pcs, const WCHAR* name)
201 struct module* module;
203 for (module = pcs->lmodules; module; module = module->next)
205 if (!strcmpiW(name, module->module.ModuleName)) return module;
207 SetLastError(ERROR_INVALID_NAME);
211 struct module* module_find_by_nameA(const struct process* pcs, const char* name)
213 WCHAR wname[MAX_PATH];
215 MultiByteToWideChar(CP_ACP, 0, name, -1, wname, sizeof(wname) / sizeof(WCHAR));
216 return module_find_by_name(pcs, wname);
219 /***********************************************************************
220 * module_is_already_loaded
223 struct module* module_is_already_loaded(const struct process* pcs, const WCHAR* name)
225 struct module* module;
226 const WCHAR* filename;
228 /* first compare the loaded image name... */
229 for (module = pcs->lmodules; module; module = module->next)
231 if (!strcmpiW(name, module->module.LoadedImageName))
234 /* then compare the standard filenames (without the path) ... */
235 filename = get_filename(name, NULL);
236 for (module = pcs->lmodules; module; module = module->next)
238 if (!strcmpiW(filename, get_filename(module->module.LoadedImageName, NULL)))
241 SetLastError(ERROR_INVALID_NAME);
245 /***********************************************************************
246 * module_get_container
249 static struct module* module_get_container(const struct process* pcs,
250 const struct module* inner)
252 struct module* module;
254 for (module = pcs->lmodules; module; module = module->next)
256 if (module != inner &&
257 module->module.BaseOfImage <= inner->module.BaseOfImage &&
258 module->module.BaseOfImage + module->module.ImageSize >=
259 inner->module.BaseOfImage + inner->module.ImageSize)
265 /***********************************************************************
266 * module_get_containee
269 struct module* module_get_containee(const struct process* pcs,
270 const struct module* outter)
272 struct module* module;
274 for (module = pcs->lmodules; module; module = module->next)
276 if (module != outter &&
277 outter->module.BaseOfImage <= module->module.BaseOfImage &&
278 outter->module.BaseOfImage + outter->module.ImageSize >=
279 module->module.BaseOfImage + module->module.ImageSize)
285 /******************************************************************
288 * get the debug information from a module:
289 * - if the module's type is deferred, then force loading of debug info (and return
291 * - if the module has no debug info and has an ELF container, then return the ELF
292 * container (and also force the ELF container's debug info loading if deferred)
293 * - otherwise return the module itself if it has some debug info
295 BOOL module_get_debug(struct module_pair* pair)
297 IMAGEHLP_DEFERRED_SYMBOL_LOADW64 idslW64;
299 if (!pair->requested) return FALSE;
300 /* for a PE builtin, always get info from container */
301 if (!(pair->effective = module_get_container(pair->pcs, pair->requested)))
302 pair->effective = pair->requested;
303 /* if deferred, force loading */
304 if (pair->effective->module.SymType == SymDeferred)
308 if (pair->effective->is_virtual) ret = FALSE;
309 else switch (pair->effective->type)
312 ret = elf_load_debug_info(pair->effective, NULL);
315 idslW64.SizeOfStruct = sizeof(idslW64);
316 idslW64.BaseOfImage = pair->effective->module.BaseOfImage;
317 idslW64.CheckSum = pair->effective->module.CheckSum;
318 idslW64.TimeDateStamp = pair->effective->module.TimeDateStamp;
319 memcpy(idslW64.FileName, pair->effective->module.ImageName,
320 sizeof(pair->effective->module.ImageName));
321 idslW64.Reparse = FALSE;
322 idslW64.hFile = INVALID_HANDLE_VALUE;
324 pcs_callback(pair->pcs, CBA_DEFERRED_SYMBOL_LOAD_START, &idslW64);
325 ret = pe_load_debug_info(pair->pcs, pair->effective);
326 pcs_callback(pair->pcs,
327 ret ? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE : CBA_DEFERRED_SYMBOL_LOAD_FAILURE,
331 ret = macho_load_debug_info(pair->effective, NULL);
337 if (!ret) pair->effective->module.SymType = SymNone;
338 assert(pair->effective->module.SymType != SymDeferred);
339 pair->effective->module.NumSyms = pair->effective->ht_symbols.num_elts;
341 return pair->effective->module.SymType != SymNone;
344 /***********************************************************************
345 * module_find_by_addr
347 * either the addr where module is loaded, or any address inside the
350 struct module* module_find_by_addr(const struct process* pcs, unsigned long addr,
351 enum module_type type)
353 struct module* module;
355 if (type == DMT_UNKNOWN)
357 if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
358 (module = module_find_by_addr(pcs, addr, DMT_ELF)) ||
359 (module = module_find_by_addr(pcs, addr, DMT_MACHO)))
364 for (module = pcs->lmodules; module; module = module->next)
366 if (type == module->type && addr >= module->module.BaseOfImage &&
367 addr < module->module.BaseOfImage + module->module.ImageSize)
371 SetLastError(ERROR_INVALID_ADDRESS);
375 /******************************************************************
376 * module_is_container_loaded
378 * checks whether the native container, for a (supposed) PE builtin is
381 static BOOL module_is_container_loaded(const struct process* pcs,
382 const WCHAR* ImageName, DWORD64 base)
385 struct module* module;
386 PCWSTR filename, modname;
388 if (!base) return FALSE;
389 filename = get_filename(ImageName, NULL);
390 len = strlenW(filename);
392 for (module = pcs->lmodules; module; module = module->next)
394 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
395 base >= module->module.BaseOfImage &&
396 base < module->module.BaseOfImage + module->module.ImageSize)
398 modname = get_filename(module->module.LoadedImageName, NULL);
399 if (!strncmpiW(modname, filename, len) &&
400 !memcmp(modname + len, S_DotSoW, 3 * sizeof(WCHAR)))
406 /* likely a native PE module */
407 WARN("Couldn't find container for %s\n", debugstr_w(ImageName));
411 /******************************************************************
412 * module_get_type_by_name
414 * Guesses a filename type from its extension
416 enum module_type module_get_type_by_name(const WCHAR* name)
418 int len = strlenW(name);
420 /* Skip all version extensions (.[digits]) regex: "(\.\d+)*$" */
425 while (i && isdigit(name[i - 1])) i--;
427 if (i && name[i - 1] == '.')
433 /* check for terminating .so or .so.[digit] */
434 /* FIXME: Can't rely solely on extension; have to check magic or
435 * stop using .so on Mac OS X. For now, base on platform. */
436 if (len > 3 && !memcmp(name + len - 3, S_DotSoW, 3))
443 if (len > 6 && !strncmpiW(name + len - 6, S_DotDylibW, 6))
446 if (len > 4 && !strncmpiW(name + len - 4, S_DotPdbW, 4))
449 if (len > 4 && !strncmpiW(name + len - 4, S_DotDbgW, 4))
452 /* wine is also a native module (Mach-O on Mac OS X, ELF elsewhere) */
453 if (((len > 4 && name[len - 5] == '/') || len == 4) && !strcmpiW(name + len - 4, S_WineW))
464 /******************************************************************
465 * refresh_module_list
467 static BOOL refresh_module_list(struct process* pcs)
469 /* force transparent ELF and Mach-O loading / unloading */
470 return elf_synchronize_module_list(pcs) || macho_synchronize_module_list(pcs);
473 /***********************************************************************
474 * SymLoadModule (DBGHELP.@)
476 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
477 PCSTR ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
479 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll,
483 /***********************************************************************
484 * SymLoadModuleEx (DBGHELP.@)
486 DWORD64 WINAPI SymLoadModuleEx(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
487 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD DllSize,
488 PMODLOAD_DATA Data, DWORD Flags)
490 PWSTR wImageName, wModuleName;
494 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
495 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
496 wine_dbgstr_longlong(BaseOfDll), DllSize, Data, Flags);
500 len = MultiByteToWideChar(CP_ACP, 0, ImageName, -1, NULL, 0);
501 wImageName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
502 MultiByteToWideChar(CP_ACP, 0, ImageName, -1, wImageName, len);
504 else wImageName = NULL;
507 len = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
508 wModuleName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
509 MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, wModuleName, len);
511 else wModuleName = NULL;
513 ret = SymLoadModuleExW(hProcess, hFile, wImageName, wModuleName,
514 BaseOfDll, DllSize, Data, Flags);
515 HeapFree(GetProcessHeap(), 0, wImageName);
516 HeapFree(GetProcessHeap(), 0, wModuleName);
520 /***********************************************************************
521 * SymLoadModuleExW (DBGHELP.@)
523 DWORD64 WINAPI SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageName,
524 PCWSTR wModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll,
525 PMODLOAD_DATA Data, DWORD Flags)
528 struct module* module = NULL;
530 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
531 hProcess, hFile, debugstr_w(wImageName), debugstr_w(wModuleName),
532 wine_dbgstr_longlong(BaseOfDll), SizeOfDll, Data, Flags);
535 FIXME("Unsupported load data parameter %p for %s\n",
536 Data, debugstr_w(wImageName));
537 if (!validate_addr64(BaseOfDll)) return FALSE;
539 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
541 if (Flags & SLMFLAG_VIRTUAL)
543 if (!wImageName) return FALSE;
544 module = module_new(pcs, wImageName, module_get_type_by_name(wImageName),
545 TRUE, BaseOfDll, SizeOfDll, 0, 0);
546 if (!module) return FALSE;
547 if (wModuleName) module_set_module(module, wModuleName);
548 module->module.SymType = SymVirtual;
552 if (Flags & ~(SLMFLAG_VIRTUAL))
553 FIXME("Unsupported Flags %08x for %s\n", Flags, debugstr_w(wImageName));
555 refresh_module_list(pcs);
557 /* this is a Wine extension to the API just to redo the synchronisation */
558 if (!wImageName && !hFile) return 0;
560 /* check if the module is already loaded, or if it's a builtin PE module with
561 * an containing ELF module
565 module = module_is_already_loaded(pcs, wImageName);
566 if (!module && module_is_container_loaded(pcs, wImageName, BaseOfDll))
568 /* force the loading of DLL as builtin */
569 module = pe_load_builtin_module(pcs, wImageName, BaseOfDll, SizeOfDll);
574 /* otherwise, try a regular PE module */
575 if (!(module = pe_load_native_module(pcs, wImageName, hFile, BaseOfDll, SizeOfDll)) &&
578 /* and finally an ELF or Mach-O module */
579 switch (module_get_type_by_name(wImageName))
582 module = elf_load_module(pcs, wImageName, BaseOfDll);
585 module = macho_load_module(pcs, wImageName, BaseOfDll);
595 WARN("Couldn't locate %s\n", debugstr_w(wImageName));
598 module->module.NumSyms = module->ht_symbols.num_elts;
599 /* by default module_new fills module.ModuleName from a derivation
600 * of LoadedImageName. Overwrite it, if we have better information
603 module_set_module(module, wModuleName);
605 lstrcpynW(module->module.ImageName, wImageName,
606 sizeof(module->module.ImageName) / sizeof(WCHAR));
608 return module->module.BaseOfImage;
611 /***********************************************************************
612 * SymLoadModule64 (DBGHELP.@)
614 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
615 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
617 if (!validate_addr64(BaseOfDll)) return FALSE;
618 return SymLoadModule(hProcess, hFile, ImageName, ModuleName, (DWORD)BaseOfDll, SizeOfDll);
621 /******************************************************************
625 BOOL module_remove(struct process* pcs, struct module* module)
627 struct module_format*modfmt;
631 TRACE("%s (%p)\n", debugstr_w(module->module.ModuleName), module);
633 for (i = 0; i < DFI_LAST; i++)
635 if ((modfmt = module->format_info[i]) && modfmt->remove)
636 modfmt->remove(pcs, module->format_info[i]);
638 hash_table_destroy(&module->ht_symbols);
639 hash_table_destroy(&module->ht_types);
640 HeapFree(GetProcessHeap(), 0, module->sources);
641 HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
642 pool_destroy(&module->pool);
643 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
646 for (p = &pcs->lmodules; *p; p = &(*p)->next)
651 HeapFree(GetProcessHeap(), 0, module);
655 FIXME("This shouldn't happen\n");
659 /******************************************************************
660 * SymUnloadModule (DBGHELP.@)
663 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
666 struct module* module;
668 pcs = process_find_by_handle(hProcess);
669 if (!pcs) return FALSE;
670 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
671 if (!module) return FALSE;
672 return module_remove(pcs, module);
675 /******************************************************************
676 * SymUnloadModule64 (DBGHELP.@)
679 BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
682 struct module* module;
684 pcs = process_find_by_handle(hProcess);
685 if (!pcs) return FALSE;
686 if (!validate_addr64(BaseOfDll)) return FALSE;
687 module = module_find_by_addr(pcs, (DWORD)BaseOfDll, DMT_UNKNOWN);
688 if (!module) return FALSE;
689 return module_remove(pcs, module);
692 /******************************************************************
693 * SymEnumerateModules (DBGHELP.@)
696 struct enum_modW64_32
698 PSYM_ENUMMODULES_CALLBACK cb;
700 char module[MAX_PATH];
703 static BOOL CALLBACK enum_modW64_32(PCWSTR name, DWORD64 base, PVOID user)
705 struct enum_modW64_32* x = user;
707 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
708 return x->cb(x->module, (DWORD)base, x->user);
711 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
712 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
715 struct enum_modW64_32 x;
717 x.cb = EnumModulesCallback;
718 x.user = UserContext;
720 return SymEnumerateModulesW64(hProcess, enum_modW64_32, &x);
723 /******************************************************************
724 * SymEnumerateModules64 (DBGHELP.@)
727 struct enum_modW64_64
729 PSYM_ENUMMODULES_CALLBACK64 cb;
731 char module[MAX_PATH];
734 static BOOL CALLBACK enum_modW64_64(PCWSTR name, DWORD64 base, PVOID user)
736 struct enum_modW64_64* x = user;
738 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
739 return x->cb(x->module, base, x->user);
742 BOOL WINAPI SymEnumerateModules64(HANDLE hProcess,
743 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,
746 struct enum_modW64_64 x;
748 x.cb = EnumModulesCallback;
749 x.user = UserContext;
751 return SymEnumerateModulesW64(hProcess, enum_modW64_64, &x);
754 /******************************************************************
755 * SymEnumerateModulesW64 (DBGHELP.@)
758 BOOL WINAPI SymEnumerateModulesW64(HANDLE hProcess,
759 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback,
762 struct process* pcs = process_find_by_handle(hProcess);
763 struct module* module;
765 if (!pcs) return FALSE;
767 for (module = pcs->lmodules; module; module = module->next)
769 if (!(dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES) &&
770 (module->type == DMT_ELF || module->type == DMT_MACHO))
772 if (!EnumModulesCallback(module->module.ModuleName,
773 module->module.BaseOfImage, UserContext))
779 /******************************************************************
780 * EnumerateLoadedModules64 (DBGHELP.@)
783 struct enum_load_modW64_64
785 PENUMLOADED_MODULES_CALLBACK64 cb;
787 char module[MAX_PATH];
790 static BOOL CALLBACK enum_load_modW64_64(PCWSTR name, DWORD64 base, ULONG size,
793 struct enum_load_modW64_64* x = user;
795 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
796 return x->cb(x->module, base, size, x->user);
799 BOOL WINAPI EnumerateLoadedModules64(HANDLE hProcess,
800 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
803 struct enum_load_modW64_64 x;
805 x.cb = EnumLoadedModulesCallback;
806 x.user = UserContext;
808 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_64, &x);
811 /******************************************************************
812 * EnumerateLoadedModules (DBGHELP.@)
815 struct enum_load_modW64_32
817 PENUMLOADED_MODULES_CALLBACK cb;
819 char module[MAX_PATH];
822 static BOOL CALLBACK enum_load_modW64_32(PCWSTR name, DWORD64 base, ULONG size,
825 struct enum_load_modW64_32* x = user;
826 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
827 return x->cb(x->module, (DWORD)base, size, x->user);
830 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
831 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
834 struct enum_load_modW64_32 x;
836 x.cb = EnumLoadedModulesCallback;
837 x.user = UserContext;
839 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_32, &x);
842 /******************************************************************
843 * EnumerateLoadedModulesW64 (DBGHELP.@)
846 BOOL WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
847 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback,
851 WCHAR baseW[256], modW[256];
855 hMods = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods[0]));
856 if (!hMods) return FALSE;
858 if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
860 /* hProcess should also be a valid process handle !! */
861 FIXME("If this happens, bump the number in mod\n");
862 HeapFree(GetProcessHeap(), 0, hMods);
865 sz /= sizeof(HMODULE);
866 for (i = 0; i < sz; i++)
868 if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
869 !GetModuleBaseNameW(hProcess, hMods[i], baseW, sizeof(baseW) / sizeof(WCHAR)))
871 module_fill_module(baseW, modW, sizeof(modW) / sizeof(CHAR));
872 EnumLoadedModulesCallback(modW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage,
875 HeapFree(GetProcessHeap(), 0, hMods);
877 return sz != 0 && i == sz;
880 /******************************************************************
881 * SymGetModuleInfo (DBGHELP.@)
884 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
885 PIMAGEHLP_MODULE ModuleInfo)
888 IMAGEHLP_MODULEW64 miw64;
890 if (sizeof(mi) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
892 miw64.SizeOfStruct = sizeof(miw64);
893 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
895 mi.SizeOfStruct = miw64.SizeOfStruct;
896 mi.BaseOfImage = miw64.BaseOfImage;
897 mi.ImageSize = miw64.ImageSize;
898 mi.TimeDateStamp = miw64.TimeDateStamp;
899 mi.CheckSum = miw64.CheckSum;
900 mi.NumSyms = miw64.NumSyms;
901 mi.SymType = miw64.SymType;
902 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
903 mi.ModuleName, sizeof(mi.ModuleName), NULL, NULL);
904 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
905 mi.ImageName, sizeof(mi.ImageName), NULL, NULL);
906 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
907 mi.LoadedImageName, sizeof(mi.LoadedImageName), NULL, NULL);
909 memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
914 /******************************************************************
915 * SymGetModuleInfoW (DBGHELP.@)
918 BOOL WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
919 PIMAGEHLP_MODULEW ModuleInfo)
921 IMAGEHLP_MODULEW64 miw64;
922 IMAGEHLP_MODULEW miw;
924 if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
926 miw64.SizeOfStruct = sizeof(miw64);
927 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
929 miw.SizeOfStruct = miw64.SizeOfStruct;
930 miw.BaseOfImage = miw64.BaseOfImage;
931 miw.ImageSize = miw64.ImageSize;
932 miw.TimeDateStamp = miw64.TimeDateStamp;
933 miw.CheckSum = miw64.CheckSum;
934 miw.NumSyms = miw64.NumSyms;
935 miw.SymType = miw64.SymType;
936 strcpyW(miw.ModuleName, miw64.ModuleName);
937 strcpyW(miw.ImageName, miw64.ImageName);
938 strcpyW(miw.LoadedImageName, miw64.LoadedImageName);
939 memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
944 /******************************************************************
945 * SymGetModuleInfo64 (DBGHELP.@)
948 BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
949 PIMAGEHLP_MODULE64 ModuleInfo)
951 IMAGEHLP_MODULE64 mi64;
952 IMAGEHLP_MODULEW64 miw64;
954 if (sizeof(mi64) < ModuleInfo->SizeOfStruct)
956 SetLastError(ERROR_MOD_NOT_FOUND); /* NOTE: native returns this error */
957 WARN("Wrong size %u\n", ModuleInfo->SizeOfStruct);
961 miw64.SizeOfStruct = sizeof(miw64);
962 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
964 mi64.SizeOfStruct = miw64.SizeOfStruct;
965 mi64.BaseOfImage = miw64.BaseOfImage;
966 mi64.ImageSize = miw64.ImageSize;
967 mi64.TimeDateStamp = miw64.TimeDateStamp;
968 mi64.CheckSum = miw64.CheckSum;
969 mi64.NumSyms = miw64.NumSyms;
970 mi64.SymType = miw64.SymType;
971 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
972 mi64.ModuleName, sizeof(mi64.ModuleName), NULL, NULL);
973 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
974 mi64.ImageName, sizeof(mi64.ImageName), NULL, NULL);
975 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
976 mi64.LoadedImageName, sizeof(mi64.LoadedImageName), NULL, NULL);
977 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedPdbName, -1,
978 mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName), NULL, NULL);
980 mi64.CVSig = miw64.CVSig;
981 WideCharToMultiByte(CP_ACP, 0, miw64.CVData, -1,
982 mi64.CVData, sizeof(mi64.CVData), NULL, NULL);
983 mi64.PdbSig = miw64.PdbSig;
984 mi64.PdbSig70 = miw64.PdbSig70;
985 mi64.PdbAge = miw64.PdbAge;
986 mi64.PdbUnmatched = miw64.PdbUnmatched;
987 mi64.DbgUnmatched = miw64.DbgUnmatched;
988 mi64.LineNumbers = miw64.LineNumbers;
989 mi64.GlobalSymbols = miw64.GlobalSymbols;
990 mi64.TypeInfo = miw64.TypeInfo;
991 mi64.SourceIndexed = miw64.SourceIndexed;
992 mi64.Publics = miw64.Publics;
994 memcpy(ModuleInfo, &mi64, ModuleInfo->SizeOfStruct);
999 /******************************************************************
1000 * SymGetModuleInfoW64 (DBGHELP.@)
1003 BOOL WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr,
1004 PIMAGEHLP_MODULEW64 ModuleInfo)
1006 struct process* pcs = process_find_by_handle(hProcess);
1007 struct module* module;
1008 IMAGEHLP_MODULEW64 miw64;
1010 TRACE("%p %s %p\n", hProcess, wine_dbgstr_longlong(dwAddr), ModuleInfo);
1012 if (!pcs) return FALSE;
1013 if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
1014 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1015 if (!module) return FALSE;
1017 miw64 = module->module;
1019 /* update debug information from container if any */
1020 if (module->module.SymType == SymNone)
1022 module = module_get_container(pcs, module);
1023 if (module && module->module.SymType != SymNone)
1025 miw64.SymType = module->module.SymType;
1026 miw64.NumSyms = module->module.NumSyms;
1029 memcpy(ModuleInfo, &miw64, ModuleInfo->SizeOfStruct);
1033 /***********************************************************************
1034 * SymGetModuleBase (DBGHELP.@)
1036 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
1040 ret = SymGetModuleBase64(hProcess, dwAddr);
1041 return validate_addr64(ret) ? ret : 0;
1044 /***********************************************************************
1045 * SymGetModuleBase64 (DBGHELP.@)
1047 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
1049 struct process* pcs = process_find_by_handle(hProcess);
1050 struct module* module;
1053 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1054 if (!module) return 0;
1055 return module->module.BaseOfImage;
1058 /******************************************************************
1059 * module_reset_debug_info
1060 * Removes any debug information linked to a given module.
1062 void module_reset_debug_info(struct module* module)
1064 module->sortlist_valid = TRUE;
1065 module->sorttab_size = 0;
1066 module->addr_sorttab = NULL;
1067 module->num_sorttab = module->num_symbols = 0;
1068 hash_table_destroy(&module->ht_symbols);
1069 module->ht_symbols.num_buckets = 0;
1070 module->ht_symbols.buckets = NULL;
1071 hash_table_destroy(&module->ht_types);
1072 module->ht_types.num_buckets = 0;
1073 module->ht_types.buckets = NULL;
1074 module->vtypes.num_elts = 0;
1075 hash_table_destroy(&module->ht_symbols);
1076 module->sources_used = module->sources_alloc = 0;
1077 module->sources = NULL;
1080 /******************************************************************
1081 * SymRefreshModuleList (DBGHELP.@)
1083 BOOL WINAPI SymRefreshModuleList(HANDLE hProcess)
1085 struct process* pcs;
1087 TRACE("(%p)\n", hProcess);
1089 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1091 return refresh_module_list(pcs);
1094 /***********************************************************************
1095 * SymFunctionTableAccess (DBGHELP.@)
1097 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1099 return SymFunctionTableAccess64(hProcess, AddrBase);
1102 /***********************************************************************
1103 * SymFunctionTableAccess64 (DBGHELP.@)
1105 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1107 struct process* pcs = process_find_by_handle(hProcess);
1108 struct module* module;
1110 if (!pcs || !dbghelp_current_cpu->find_runtime_function) return NULL;
1111 module = module_find_by_addr(pcs, AddrBase, DMT_UNKNOWN);
1112 if (!module) return NULL;
1114 return dbghelp_current_cpu->find_runtime_function(module, AddrBase);