dbghelp: Now storing module information in Unicode form.
[wine/wine-gecko.git] / dlls / dbghelp / module.c
blob9d56adbc6f47ec654435b79a2e8cb49c5bc3ceff
1 /*
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
22 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <assert.h>
28 #include "dbghelp_private.h"
29 #include "psapi.h"
30 #include "winreg.h"
31 #include "winternl.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
36 const WCHAR S_ElfW[] = {'<','e','l','f','>','\0'};
37 const WCHAR S_WineLoaderW[] = {'<','w','i','n','e','-','l','o','a','d','e','r','>','\0'};
38 static const WCHAR S_DotSoW[] = {'.','s','o','\0'};
39 static const WCHAR S_PdbW[] = {'.','p','d','b','\0'};
40 static const WCHAR S_WinePThreadW[] = {'w','i','n','e','-','p','t','h','r','e','a','d','\0'};
41 static const WCHAR S_WineKThreadW[] = {'w','i','n','e','-','k','t','h','r','e','a','d','\0'};
43 static const WCHAR S_AcmW[] = {'.','a','c','m','\0'};
44 static const WCHAR S_DllW[] = {'.','d','l','l','\0'};
45 static const WCHAR S_DrvW[] = {'.','d','r','v','\0'};
46 static const WCHAR S_ExeW[] = {'.','e','x','e','\0'};
47 static const WCHAR S_OcxW[] = {'.','o','c','x','\0'};
48 static const WCHAR S_VxdW[] = {'.','v','x','d','\0'};
49 static const WCHAR * const ext[] = {S_AcmW, S_DllW, S_DrvW, S_ExeW, S_OcxW, S_VxdW, NULL};
51 static int match_ext(const WCHAR* ptr, size_t len)
53 const WCHAR* const *e;
54 size_t l;
56 for (e = ext; *e; e++)
58 l = strlenW(*e);
59 if (l >= len) return FALSE;
60 if (strncmpiW(&ptr[len - l], *e, l)) continue;
61 return l;
63 return 0;
66 static void module_fill_module(const WCHAR* in, WCHAR* out, size_t size)
68 const WCHAR *ptr,*endptr;
69 size_t len, l;
71 endptr = in + strlenW(in);
72 for (ptr = endptr - 1;
73 ptr >= in && *ptr != '/' && *ptr != '\\';
74 ptr--);
75 ptr++;
76 len = min(endptr-ptr,size-1);
77 memcpy(out, ptr, len * sizeof(WCHAR));
78 out[len] = '\0';
79 if (len > 4 && (l = match_ext(out, len)))
80 out[len - l] = '\0';
81 else if (len > 12 &&
82 (!strcmpiW(out + len - 12, S_WinePThreadW) ||
83 !strcmpiW(out + len - 12, S_WineKThreadW)))
84 lstrcpynW(out, S_WineLoaderW, size);
85 else
87 if (len > 3 && !strcmpiW(&out[len - 3], S_DotSoW) &&
88 (l = match_ext(out, len - 3)))
89 strcpyW(&out[len - l - 3], S_ElfW);
91 while ((*out = tolowerW(*out))) out++;
94 void module_set_module(struct module* module, const WCHAR* name)
96 module_fill_module(name, module->module.ModuleName, sizeof(module->module.ModuleName));
97 WideCharToMultiByte(CP_ACP, 0, module->module.ModuleName, -1,
98 module->module_name, sizeof(module->module_name),
99 NULL, NULL);
102 static const char* get_module_type(enum module_type type, BOOL virtual)
104 switch (type)
106 case DMT_ELF: return virtual ? "Virtual ELF" : "ELF";
107 case DMT_PE: return virtual ? "Virtual PE" : "PE";
108 default: return "---";
112 /***********************************************************************
113 * Creates and links a new module to a process
115 struct module* module_new(struct process* pcs, const WCHAR* name,
116 enum module_type type, BOOL virtual,
117 unsigned long mod_addr, unsigned long size,
118 unsigned long stamp, unsigned long checksum)
120 struct module* module;
122 assert(type == DMT_ELF || type == DMT_PE);
123 if (!(module = HeapAlloc(GetProcessHeap(), 0, sizeof(*module))))
124 return NULL;
126 memset(module, 0, sizeof(*module));
128 module->next = pcs->lmodules;
129 pcs->lmodules = module;
131 TRACE("=> %s %08lx-%08lx %s\n",
132 get_module_type(type, virtual), mod_addr, mod_addr + size,
133 debugstr_w(name));
135 pool_init(&module->pool, 65536);
137 module->module.SizeOfStruct = sizeof(module->module);
138 module->module.BaseOfImage = mod_addr;
139 module->module.ImageSize = size;
140 module_set_module(module, name);
141 module->module.ImageName[0] = '\0';
142 lstrcpynW(module->module.LoadedImageName, name, sizeof(module->module.LoadedImageName) / sizeof(WCHAR));
143 module->module.SymType = SymNone;
144 module->module.NumSyms = 0;
145 module->module.TimeDateStamp = stamp;
146 module->module.CheckSum = checksum;
148 memset(module->module.LoadedPdbName, 0, sizeof(module->module.CVData));
149 module->module.CVSig = 0;
150 memset(module->module.CVData, 0, sizeof(module->module.CVData));
151 module->module.PdbSig = 0;
152 memset(&module->module.PdbSig70, 0, sizeof(module->module.PdbSig70));
153 module->module.PdbAge = 0;
154 module->module.PdbUnmatched = FALSE;
155 module->module.DbgUnmatched = FALSE;
156 module->module.LineNumbers = FALSE;
157 module->module.GlobalSymbols = FALSE;
158 module->module.TypeInfo = FALSE;
159 module->module.SourceIndexed = FALSE;
160 module->module.Publics = FALSE;
162 module->type = type;
163 module->is_virtual = virtual ? TRUE : FALSE;
164 module->sortlist_valid = FALSE;
165 module->addr_sorttab = NULL;
166 /* FIXME: this seems a bit too high (on a per module basis)
167 * need some statistics about this
169 hash_table_init(&module->pool, &module->ht_symbols, 4096);
170 hash_table_init(&module->pool, &module->ht_types, 4096);
171 vector_init(&module->vtypes, sizeof(struct symt*), 32);
173 module->sources_used = 0;
174 module->sources_alloc = 0;
175 module->sources = 0;
177 return module;
180 struct module* module_newA(struct process* pcs, const char* name,
181 enum module_type type, BOOL virtual,
182 unsigned long mod_addr, unsigned long size,
183 unsigned long stamp, unsigned long checksum)
185 WCHAR wname[MAX_PATH];
187 MultiByteToWideChar(CP_ACP, 0, name, -1, wname, sizeof(wname) / sizeof(WCHAR));
188 return module_new(pcs, wname, type, virtual, mod_addr, size, stamp, checksum);
191 /***********************************************************************
192 * module_find_by_name
195 struct module* module_find_by_name(const struct process* pcs,
196 const WCHAR* name, enum module_type type)
198 struct module* module;
200 if (type == DMT_UNKNOWN)
202 if ((module = module_find_by_name(pcs, name, DMT_PE)) ||
203 (module = module_find_by_name(pcs, name, DMT_ELF)))
204 return module;
206 else
208 WCHAR modname[MAX_PATH];
210 for (module = pcs->lmodules; module; module = module->next)
212 if (type == module->type &&
213 !strcmpiW(name, module->module.LoadedImageName))
214 return module;
216 module_fill_module(name, modname, sizeof(modname));
217 for (module = pcs->lmodules; module; module = module->next)
219 if (type == module->type &&
220 !strcmpiW(modname, module->module.ModuleName))
221 return module;
224 SetLastError(ERROR_INVALID_NAME);
225 return NULL;
228 struct module* module_find_by_nameA(const struct process* pcs,
229 const char* name, enum module_type type)
231 WCHAR wname[MAX_PATH];
233 MultiByteToWideChar(CP_ACP, 0, name, -1, wname, sizeof(wname) / sizeof(WCHAR));
234 return module_find_by_name(pcs, wname, type);
237 /***********************************************************************
238 * module_get_container
241 struct module* module_get_container(const struct process* pcs,
242 const struct module* inner)
244 struct module* module;
246 for (module = pcs->lmodules; module; module = module->next)
248 if (module != inner &&
249 module->module.BaseOfImage <= inner->module.BaseOfImage &&
250 module->module.BaseOfImage + module->module.ImageSize >=
251 inner->module.BaseOfImage + inner->module.ImageSize)
252 return module;
254 return NULL;
257 /***********************************************************************
258 * module_get_containee
261 struct module* module_get_containee(const struct process* pcs,
262 const struct module* outter)
264 struct module* module;
266 for (module = pcs->lmodules; module; module = module->next)
268 if (module != outter &&
269 outter->module.BaseOfImage <= module->module.BaseOfImage &&
270 outter->module.BaseOfImage + outter->module.ImageSize >=
271 module->module.BaseOfImage + module->module.ImageSize)
272 return module;
274 return NULL;
277 /******************************************************************
278 * module_get_debug
280 * get the debug information from a module:
281 * - if the module's type is deferred, then force loading of debug info (and return
282 * the module itself)
283 * - if the module has no debug info and has an ELF container, then return the ELF
284 * container (and also force the ELF container's debug info loading if deferred)
285 * - otherwise return the module itself if it has some debug info
287 BOOL module_get_debug(struct module_pair* pair)
289 IMAGEHLP_DEFERRED_SYMBOL_LOAD64 idsl64;
291 if (!pair->requested) return FALSE;
292 /* for a PE builtin, always get info from container */
293 if (!(pair->effective = module_get_container(pair->pcs, pair->requested)))
294 pair->effective = pair->requested;
295 /* if deferred, force loading */
296 if (pair->effective->module.SymType == SymDeferred)
298 BOOL ret;
300 if (pair->effective->is_virtual) ret = FALSE;
301 else switch (pair->effective->type)
303 case DMT_ELF:
304 ret = elf_load_debug_info(pair->effective, NULL);
305 break;
306 case DMT_PE:
307 idsl64.SizeOfStruct = sizeof(idsl64);
308 idsl64.BaseOfImage = pair->effective->module.BaseOfImage;
309 idsl64.CheckSum = pair->effective->module.CheckSum;
310 idsl64.TimeDateStamp = pair->effective->module.TimeDateStamp;
311 WideCharToMultiByte(CP_ACP, 0, pair->effective->module.ImageName, -1,
312 idsl64.FileName, sizeof(idsl64.FileName), NULL, NULL);
313 idsl64.Reparse = FALSE;
314 idsl64.hFile = INVALID_HANDLE_VALUE;
316 pcs_callback(pair->pcs, CBA_DEFERRED_SYMBOL_LOAD_START, &idsl64);
317 ret = pe_load_debug_info(pair->pcs, pair->effective);
318 pcs_callback(pair->pcs,
319 ret ? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE : CBA_DEFERRED_SYMBOL_LOAD_FAILURE,
320 &idsl64);
321 break;
322 default:
323 ret = FALSE;
324 break;
326 if (!ret) pair->effective->module.SymType = SymNone;
327 assert(pair->effective->module.SymType != SymDeferred);
328 pair->effective->module.NumSyms = pair->effective->ht_symbols.num_elts;
330 return pair->effective->module.SymType != SymNone;
333 /***********************************************************************
334 * module_find_by_addr
336 * either the addr where module is loaded, or any address inside the
337 * module
339 struct module* module_find_by_addr(const struct process* pcs, unsigned long addr,
340 enum module_type type)
342 struct module* module;
344 if (type == DMT_UNKNOWN)
346 if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
347 (module = module_find_by_addr(pcs, addr, DMT_ELF)))
348 return module;
350 else
352 for (module = pcs->lmodules; module; module = module->next)
354 if (type == module->type && addr >= module->module.BaseOfImage &&
355 addr < module->module.BaseOfImage + module->module.ImageSize)
356 return module;
359 SetLastError(ERROR_INVALID_ADDRESS);
360 return module;
363 static BOOL module_is_elf_container_loaded(struct process* pcs,
364 const WCHAR* ImageName,
365 const WCHAR* ModuleName)
367 WCHAR buffer[MAX_PATH];
368 size_t len;
369 struct module* module;
371 if (!ModuleName)
373 module_fill_module(ImageName, buffer, sizeof(buffer));
374 ModuleName = buffer;
376 len = strlenW(ModuleName);
377 for (module = pcs->lmodules; module; module = module->next)
379 if (!strncmpiW(module->module.ModuleName, ModuleName, len) &&
380 module->type == DMT_ELF &&
381 !strcmpW(module->module.ModuleName + len, S_ElfW))
382 return TRUE;
384 return FALSE;
387 /******************************************************************
388 * module_get_type_by_name
390 * Guesses a filename type from its extension
392 enum module_type module_get_type_by_name(const char* name)
394 const char* ptr;
395 int len = strlen(name);
397 /* check for terminating .so or .so.[digit] */
398 ptr = strrchr(name, '.');
399 if (ptr)
401 if (!strcmp(ptr, ".so") ||
402 (isdigit(ptr[1]) && !ptr[2] && ptr >= name + 3 && !memcmp(ptr - 3, ".so", 3)))
403 return DMT_ELF;
404 else if (!strcasecmp(ptr, ".pdb"))
405 return DMT_PDB;
407 /* wine-[kp]thread is also an ELF module */
408 else if (((len > 12 && name[len - 13] == '/') || len == 12) &&
409 (!strcasecmp(name + len - 12, "wine-pthread") ||
410 !strcasecmp(name + len - 12, "wine-kthread")))
412 return DMT_ELF;
414 return DMT_PE;
417 /***********************************************************************
418 * SymLoadModule (DBGHELP.@)
420 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, const char* ImageName,
421 const char* ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
423 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll,
424 SizeOfDll, NULL, 0);
427 /***********************************************************************
428 * SymLoadModuleEx (DBGHELP.@)
430 DWORD64 WINAPI SymLoadModuleEx(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
431 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD DllSize,
432 PMODLOAD_DATA Data, DWORD Flags)
434 LPWSTR wImageName, wModuleName;
435 unsigned len;
436 DWORD64 ret;
438 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
439 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
440 wine_dbgstr_longlong(BaseOfDll), DllSize, Data, Flags);
442 if (ImageName)
444 len = MultiByteToWideChar(CP_ACP, 0, ImageName, -1, NULL, 0);
445 wImageName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
446 MultiByteToWideChar(CP_ACP, 0, ImageName, -1, wImageName, len);
448 else wImageName = NULL;
449 if (ModuleName)
451 len = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
452 wModuleName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
453 MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, wModuleName, len);
455 else wModuleName = NULL;
457 ret = SymLoadModuleExW(hProcess, hFile, wImageName, wModuleName,
458 BaseOfDll, DllSize, Data, Flags);
459 HeapFree(GetProcessHeap(), 0, wImageName);
460 HeapFree(GetProcessHeap(), 0, wModuleName);
461 return ret;
464 /***********************************************************************
465 * SymLoadModuleExW (DBGHELP.@)
467 DWORD64 WINAPI SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageName,
468 PCWSTR wModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll,
469 PMODLOAD_DATA Data, DWORD Flags)
471 struct process* pcs;
472 struct module* module = NULL;
473 char ImageName[MAX_PATH], amodname[MAX_PATH], *ModuleName;
476 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
477 hProcess, hFile, debugstr_w(wImageName), debugstr_w(wModuleName),
478 wine_dbgstr_longlong(BaseOfDll), SizeOfDll, Data, Flags);
480 if (Data)
481 FIXME("Unsupported load data parameter %p for %s\n",
482 Data, debugstr_w(wImageName));
483 if (!validate_addr64(BaseOfDll)) return FALSE;
485 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
487 if (Flags & SLMFLAG_VIRTUAL)
489 WideCharToMultiByte(CP_ACP,0, wImageName, -1, ImageName, MAX_PATH,
490 NULL, NULL);
491 module = module_new(pcs, wImageName, module_get_type_by_name(ImageName),
492 TRUE, (DWORD)BaseOfDll, SizeOfDll, 0, 0);
493 if (!module) return FALSE;
494 if (wModuleName) module_set_module(module, wModuleName);
495 module->module.SymType = SymVirtual;
497 return TRUE;
499 if (Flags & ~(SLMFLAG_VIRTUAL))
500 FIXME("Unsupported Flags %08x for %s\n", Flags, debugstr_w(wImageName));
502 /* force transparent ELF loading / unloading */
503 elf_synchronize_module_list(pcs);
505 /* this is a Wine extension to the API just to redo the synchronisation */
506 if (!wImageName && !hFile) return 0;
508 WideCharToMultiByte(CP_ACP,0, wImageName, -1, ImageName, MAX_PATH,
509 NULL, NULL);
511 if (wModuleName)
512 WideCharToMultiByte(CP_ACP,0, wModuleName, -1, ModuleName = amodname, MAX_PATH,
513 NULL, NULL);
514 else
515 ModuleName = NULL;
517 if (module_is_elf_container_loaded(pcs, wImageName, wModuleName))
519 /* force the loading of DLL as builtin */
520 if ((module = pe_load_module_from_pcs(pcs, ImageName, ModuleName,
521 BaseOfDll, SizeOfDll)))
522 goto done;
523 WARN("Couldn't locate %s\n", debugstr_w(wImageName));
524 return 0;
526 TRACE("Assuming %s as native DLL\n", debugstr_w(wImageName));
527 if (!(module = pe_load_module(pcs, ImageName, hFile, BaseOfDll, SizeOfDll)))
529 if (module_get_type_by_name(ImageName) == DMT_ELF &&
530 (module = elf_load_module(pcs, ImageName, BaseOfDll)))
531 goto done;
532 FIXME("Should have successfully loaded debug information for image %s\n",
533 debugstr_w(wImageName));
534 if ((module = pe_load_module_from_pcs(pcs, ImageName, ModuleName,
535 BaseOfDll, SizeOfDll)))
536 goto done;
537 WARN("Couldn't locate %s\n", debugstr_w(wImageName));
538 return 0;
540 module->module.NumSyms = module->ht_symbols.num_elts;
541 done:
542 /* by default pe_load_module fills module.ModuleName from a derivation
543 * of ImageName. Overwrite it, if we have better information
545 if (wModuleName)
546 module_set_module(module, wModuleName);
547 lstrcpynW(module->module.ImageName, wImageName,
548 sizeof(module->module.ImageName) / sizeof(CHAR));
550 return module->module.BaseOfImage;
553 /***********************************************************************
554 * SymLoadModule64 (DBGHELP.@)
556 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
557 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
559 if (!validate_addr64(BaseOfDll)) return FALSE;
560 return SymLoadModule(hProcess, hFile, ImageName, ModuleName, (DWORD)BaseOfDll, SizeOfDll);
563 /******************************************************************
564 * module_remove
567 BOOL module_remove(struct process* pcs, struct module* module)
569 struct module** p;
571 TRACE("%s (%p)\n", module->module_name, module);
572 hash_table_destroy(&module->ht_symbols);
573 hash_table_destroy(&module->ht_types);
574 HeapFree(GetProcessHeap(), 0, (char*)module->sources);
575 HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
576 HeapFree(GetProcessHeap(), 0, module->dwarf2_info);
577 pool_destroy(&module->pool);
578 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
579 * so do we
581 for (p = &pcs->lmodules; *p; p = &(*p)->next)
583 if (*p == module)
585 *p = module->next;
586 HeapFree(GetProcessHeap(), 0, module);
587 return TRUE;
590 FIXME("This shouldn't happen\n");
591 return FALSE;
594 /******************************************************************
595 * SymUnloadModule (DBGHELP.@)
598 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
600 struct process* pcs;
601 struct module* module;
603 pcs = process_find_by_handle(hProcess);
604 if (!pcs) return FALSE;
605 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
606 if (!module) return FALSE;
607 return module_remove(pcs, module);
610 /******************************************************************
611 * SymUnloadModule64 (DBGHELP.@)
614 BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
616 struct process* pcs;
617 struct module* module;
619 pcs = process_find_by_handle(hProcess);
620 if (!pcs) return FALSE;
621 if (!validate_addr64(BaseOfDll)) return FALSE;
622 module = module_find_by_addr(pcs, (DWORD)BaseOfDll, DMT_UNKNOWN);
623 if (!module) return FALSE;
624 return module_remove(pcs, module);
627 /******************************************************************
628 * SymEnumerateModules (DBGHELP.@)
631 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
632 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
633 PVOID UserContext)
635 struct process* pcs = process_find_by_handle(hProcess);
636 struct module* module;
638 if (!pcs) return FALSE;
640 for (module = pcs->lmodules; module; module = module->next)
642 if (!(dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES) && module->type == DMT_ELF)
643 continue;
644 if (!EnumModulesCallback(module->module_name,
645 module->module.BaseOfImage, UserContext))
646 break;
648 return TRUE;
651 /******************************************************************
652 * SymEnumerateModules64 (DBGHELP.@)
655 BOOL WINAPI SymEnumerateModules64(HANDLE hProcess,
656 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,
657 PVOID UserContext)
659 struct process* pcs = process_find_by_handle(hProcess);
660 struct module* module;
662 if (!pcs) return FALSE;
664 for (module = pcs->lmodules; module; module = module->next)
666 if (!(dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES) && module->type == DMT_ELF)
667 continue;
668 if (!EnumModulesCallback(module->module_name,
669 module->module.BaseOfImage, UserContext))
670 break;
672 return TRUE;
675 /******************************************************************
676 * EnumerateLoadedModules64 (DBGHELP.@)
679 struct enum_load_modW64_64
681 PENUMLOADED_MODULES_CALLBACK64 cb;
682 PVOID user;
683 char module[MAX_PATH];
686 static BOOL CALLBACK enum_load_modW64_64(PWSTR name, DWORD64 base, ULONG size,
687 PVOID user)
689 struct enum_load_modW64_64* x = user;
691 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
692 return x->cb(x->module, base, size, x->user);
695 BOOL WINAPI EnumerateLoadedModules64(HANDLE hProcess,
696 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
697 PVOID UserContext)
699 struct enum_load_modW64_64 x;
701 x.cb = EnumLoadedModulesCallback;
702 x.user = UserContext;
704 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_64, &x);
707 /******************************************************************
708 * EnumerateLoadedModules (DBGHELP.@)
711 struct enum_load_modW64_32
713 PENUMLOADED_MODULES_CALLBACK cb;
714 PVOID user;
715 char module[MAX_PATH];
718 static BOOL CALLBACK enum_load_modW64_32(PWSTR name, DWORD64 base, ULONG size,
719 PVOID user)
721 struct enum_load_modW64_32* x = user;
722 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
723 return x->cb(x->module, (DWORD)base, size, x->user);
726 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
727 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
728 PVOID UserContext)
730 struct enum_load_modW64_32 x;
732 x.cb = EnumLoadedModulesCallback;
733 x.user = UserContext;
735 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_32, &x);
738 /******************************************************************
739 * EnumerateLoadedModulesW64 (DBGHELP.@)
742 BOOL WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
743 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback,
744 PVOID UserContext)
746 HMODULE* hMods;
747 WCHAR baseW[256], modW[256];
748 DWORD i, sz;
749 MODULEINFO mi;
751 hMods = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods[0]));
752 if (!hMods) return FALSE;
754 if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
756 /* hProcess should also be a valid process handle !! */
757 FIXME("If this happens, bump the number in mod\n");
758 HeapFree(GetProcessHeap(), 0, hMods);
759 return FALSE;
761 sz /= sizeof(HMODULE);
762 for (i = 0; i < sz; i++)
764 if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
765 !GetModuleBaseNameW(hProcess, hMods[i], baseW, sizeof(baseW) / sizeof(WCHAR)))
766 continue;
767 module_fill_module(baseW, modW, sizeof(modW) / sizeof(CHAR));
768 EnumLoadedModulesCallback(modW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage,
769 UserContext);
771 HeapFree(GetProcessHeap(), 0, hMods);
773 return sz != 0 && i == sz;
776 /******************************************************************
777 * SymGetModuleInfo (DBGHELP.@)
780 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
781 PIMAGEHLP_MODULE ModuleInfo)
783 IMAGEHLP_MODULE mi;
784 IMAGEHLP_MODULEW64 miw64;
786 if (sizeof(mi) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
788 miw64.SizeOfStruct = sizeof(miw64);
789 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
791 mi.SizeOfStruct = miw64.SizeOfStruct;
792 mi.BaseOfImage = miw64.BaseOfImage;
793 mi.ImageSize = miw64.ImageSize;
794 mi.TimeDateStamp = miw64.TimeDateStamp;
795 mi.CheckSum = miw64.CheckSum;
796 mi.NumSyms = miw64.NumSyms;
797 mi.SymType = miw64.SymType;
798 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
799 mi.ModuleName, sizeof(mi.ModuleName), NULL, NULL);
800 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
801 mi.ImageName, sizeof(mi.ImageName), NULL, NULL);
802 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
803 mi.LoadedImageName, sizeof(mi.LoadedImageName), NULL, NULL);
805 memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
807 return TRUE;
810 /******************************************************************
811 * SymGetModuleInfoW (DBGHELP.@)
814 BOOL WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
815 PIMAGEHLP_MODULEW ModuleInfo)
817 IMAGEHLP_MODULEW64 miw64;
818 IMAGEHLP_MODULEW miw;
820 if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
822 miw64.SizeOfStruct = sizeof(miw64);
823 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
825 miw.SizeOfStruct = miw64.SizeOfStruct;
826 miw.BaseOfImage = miw64.BaseOfImage;
827 miw.ImageSize = miw64.ImageSize;
828 miw.TimeDateStamp = miw64.TimeDateStamp;
829 miw.CheckSum = miw64.CheckSum;
830 miw.NumSyms = miw64.NumSyms;
831 miw.SymType = miw64.SymType;
832 strcpyW(miw.ModuleName, miw64.ModuleName);
833 strcpyW(miw.ImageName, miw64.ImageName);
834 strcpyW(miw.LoadedImageName, miw64.LoadedImageName);
835 memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
837 return TRUE;
840 /******************************************************************
841 * SymGetModuleInfo64 (DBGHELP.@)
844 BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
845 PIMAGEHLP_MODULE64 ModuleInfo)
847 IMAGEHLP_MODULE64 mi64;
848 IMAGEHLP_MODULEW64 miw64;
850 if (sizeof(mi64) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
852 miw64.SizeOfStruct = sizeof(miw64);
853 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
855 mi64.SizeOfStruct = miw64.SizeOfStruct;
856 mi64.BaseOfImage = miw64.BaseOfImage;
857 mi64.ImageSize = miw64.ImageSize;
858 mi64.TimeDateStamp = miw64.TimeDateStamp;
859 mi64.CheckSum = miw64.CheckSum;
860 mi64.NumSyms = miw64.NumSyms;
861 mi64.SymType = miw64.SymType;
862 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
863 mi64.ModuleName, sizeof(mi64.ModuleName), NULL, NULL);
864 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
865 mi64.ImageName, sizeof(mi64.ImageName), NULL, NULL);
866 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
867 mi64.LoadedImageName, sizeof(mi64.LoadedImageName), NULL, NULL);
868 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedPdbName, -1,
869 mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName), NULL, NULL);
871 mi64.CVSig = miw64.CVSig;
872 WideCharToMultiByte(CP_ACP, 0, miw64.CVData, -1,
873 mi64.CVData, sizeof(mi64.CVData), NULL, NULL);
874 mi64.PdbSig = miw64.PdbSig;
875 mi64.PdbSig70 = miw64.PdbSig70;
876 mi64.PdbAge = miw64.PdbAge;
877 mi64.PdbUnmatched = miw64.PdbUnmatched;
878 mi64.DbgUnmatched = miw64.DbgUnmatched;
879 mi64.LineNumbers = miw64.LineNumbers;
880 mi64.GlobalSymbols = miw64.GlobalSymbols;
881 mi64.TypeInfo = miw64.TypeInfo;
882 mi64.SourceIndexed = miw64.SourceIndexed;
883 mi64.Publics = miw64.Publics;
885 memcpy(ModuleInfo, &mi64, ModuleInfo->SizeOfStruct);
887 return TRUE;
890 /******************************************************************
891 * SymGetModuleInfoW64 (DBGHELP.@)
894 BOOL WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr,
895 PIMAGEHLP_MODULEW64 ModuleInfo)
897 struct process* pcs = process_find_by_handle(hProcess);
898 struct module* module;
899 IMAGEHLP_MODULEW64 miw64;
901 TRACE("%p %s %p\n", hProcess, wine_dbgstr_longlong(dwAddr), ModuleInfo);
903 if (!pcs) return FALSE;
904 if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
905 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
906 if (!module) return FALSE;
908 miw64 = module->module;
910 /* update debug information from container if any */
911 if (module->module.SymType == SymNone)
913 module = module_get_container(pcs, module);
914 if (module && module->module.SymType != SymNone)
916 miw64.SymType = module->module.SymType;
917 miw64.NumSyms = module->module.NumSyms;
920 memcpy(ModuleInfo, &miw64, ModuleInfo->SizeOfStruct);
921 return TRUE;
924 /***********************************************************************
925 * SymGetModuleBase (DBGHELP.@)
927 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
929 struct process* pcs = process_find_by_handle(hProcess);
930 struct module* module;
932 if (!pcs) return 0;
933 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
934 if (!module) return 0;
935 return module->module.BaseOfImage;
938 /***********************************************************************
939 * SymGetModuleBase64 (DBGHELP.@)
941 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
943 if (!validate_addr64(dwAddr)) return 0;
944 return SymGetModuleBase(hProcess, (DWORD)dwAddr);
947 /******************************************************************
948 * module_reset_debug_info
949 * Removes any debug information linked to a given module.
951 void module_reset_debug_info(struct module* module)
953 module->sortlist_valid = TRUE;
954 module->addr_sorttab = NULL;
955 hash_table_destroy(&module->ht_symbols);
956 module->ht_symbols.num_buckets = 0;
957 module->ht_symbols.buckets = NULL;
958 hash_table_destroy(&module->ht_types);
959 module->ht_types.num_buckets = 0;
960 module->ht_types.buckets = NULL;
961 module->vtypes.num_elts = 0;
962 hash_table_destroy(&module->ht_symbols);
963 module->sources_used = module->sources_alloc = 0;
964 module->sources = NULL;