dbghelp: Rewrote module_is_elf_container_loaded so that it no longer
[wine/multimedia.git] / dlls / dbghelp / module.c
blob8dce6716eb6fbb87ff0a080d159786abc7e9cd06
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_DotPdbW[] = {'.','p','d','b','\0'};
40 const WCHAR S_WinePThreadW[] = {'w','i','n','e','-','p','t','h','r','e','a','d','\0'};
41 const WCHAR S_WineKThreadW[] = {'w','i','n','e','-','k','t','h','r','e','a','d','\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;
55 size_t l;
57 for (e = ext; *e; e++)
59 l = strlenW(*e);
60 if (l >= len) return FALSE;
61 if (strncmpiW(&ptr[len - l], *e, l)) continue;
62 return l;
64 return 0;
67 static const WCHAR* get_filename(const WCHAR* name, const WCHAR* endptr)
69 const WCHAR* ptr;
71 if (!endptr) endptr = name + strlenW(name);
72 for (ptr = endptr - 1; ptr >= name; ptr--)
74 if (*ptr == '/' || *ptr == '\\') break;
76 return ++ptr;
79 static void module_fill_module(const WCHAR* in, WCHAR* out, size_t size)
81 const WCHAR *ptr, *endptr;
82 size_t len, l;
84 ptr = get_filename(in, endptr = in + strlenW(in));
85 len = min(endptr - ptr, size - 1);
86 memcpy(out, ptr, len * sizeof(WCHAR));
87 out[len] = '\0';
88 if (len > 4 && (l = match_ext(out, len)))
89 out[len - l] = '\0';
90 else if (len > 12 &&
91 (!strcmpiW(out + len - 12, S_WinePThreadW) ||
92 !strcmpiW(out + len - 12, S_WineKThreadW)))
93 lstrcpynW(out, S_WineLoaderW, size);
94 else
96 if (len > 3 && !strcmpiW(&out[len - 3], S_DotSoW) &&
97 (l = match_ext(out, len - 3)))
98 strcpyW(&out[len - l - 3], S_ElfW);
100 while ((*out = tolowerW(*out))) out++;
103 void module_set_module(struct module* module, const WCHAR* name)
105 module_fill_module(name, module->module.ModuleName, sizeof(module->module.ModuleName));
106 WideCharToMultiByte(CP_ACP, 0, module->module.ModuleName, -1,
107 module->module_name, sizeof(module->module_name),
108 NULL, NULL);
111 static const char* get_module_type(enum module_type type, BOOL virtual)
113 switch (type)
115 case DMT_ELF: return virtual ? "Virtual ELF" : "ELF";
116 case DMT_PE: return virtual ? "Virtual PE" : "PE";
117 default: return "---";
121 /***********************************************************************
122 * Creates and links a new module to a process
124 struct module* module_new(struct process* pcs, const WCHAR* name,
125 enum module_type type, BOOL virtual,
126 unsigned long mod_addr, unsigned long size,
127 unsigned long stamp, unsigned long checksum)
129 struct module* module;
131 assert(type == DMT_ELF || type == DMT_PE);
132 if (!(module = HeapAlloc(GetProcessHeap(), 0, sizeof(*module))))
133 return NULL;
135 memset(module, 0, sizeof(*module));
137 module->next = pcs->lmodules;
138 pcs->lmodules = module;
140 TRACE("=> %s %08lx-%08lx %s\n",
141 get_module_type(type, virtual), mod_addr, mod_addr + size,
142 debugstr_w(name));
144 pool_init(&module->pool, 65536);
146 module->module.SizeOfStruct = sizeof(module->module);
147 module->module.BaseOfImage = mod_addr;
148 module->module.ImageSize = size;
149 module_set_module(module, name);
150 module->module.ImageName[0] = '\0';
151 lstrcpynW(module->module.LoadedImageName, name, sizeof(module->module.LoadedImageName) / sizeof(WCHAR));
152 module->module.SymType = SymNone;
153 module->module.NumSyms = 0;
154 module->module.TimeDateStamp = stamp;
155 module->module.CheckSum = checksum;
157 memset(module->module.LoadedPdbName, 0, sizeof(module->module.CVData));
158 module->module.CVSig = 0;
159 memset(module->module.CVData, 0, sizeof(module->module.CVData));
160 module->module.PdbSig = 0;
161 memset(&module->module.PdbSig70, 0, sizeof(module->module.PdbSig70));
162 module->module.PdbAge = 0;
163 module->module.PdbUnmatched = FALSE;
164 module->module.DbgUnmatched = FALSE;
165 module->module.LineNumbers = FALSE;
166 module->module.GlobalSymbols = FALSE;
167 module->module.TypeInfo = FALSE;
168 module->module.SourceIndexed = FALSE;
169 module->module.Publics = FALSE;
171 module->type = type;
172 module->is_virtual = virtual ? TRUE : FALSE;
173 module->sortlist_valid = FALSE;
174 module->addr_sorttab = NULL;
175 /* FIXME: this seems a bit too high (on a per module basis)
176 * need some statistics about this
178 hash_table_init(&module->pool, &module->ht_symbols, 4096);
179 hash_table_init(&module->pool, &module->ht_types, 4096);
180 vector_init(&module->vtypes, sizeof(struct symt*), 32);
182 module->sources_used = 0;
183 module->sources_alloc = 0;
184 module->sources = 0;
186 return module;
189 /***********************************************************************
190 * module_find_by_name
193 struct module* module_find_by_name(const struct process* pcs,
194 const WCHAR* name, enum module_type type)
196 struct module* module;
198 if (type == DMT_UNKNOWN)
200 if ((module = module_find_by_name(pcs, name, DMT_PE)) ||
201 (module = module_find_by_name(pcs, name, DMT_ELF)))
202 return module;
204 else
206 WCHAR modname[MAX_PATH];
208 for (module = pcs->lmodules; module; module = module->next)
210 if (type == module->type &&
211 !strcmpiW(name, module->module.LoadedImageName))
212 return module;
214 module_fill_module(name, modname, sizeof(modname));
215 for (module = pcs->lmodules; module; module = module->next)
217 if (type == module->type &&
218 !strcmpiW(modname, module->module.ModuleName))
219 return module;
222 SetLastError(ERROR_INVALID_NAME);
223 return NULL;
226 struct module* module_find_by_nameA(const struct process* pcs,
227 const char* name, enum module_type type)
229 WCHAR wname[MAX_PATH];
231 MultiByteToWideChar(CP_ACP, 0, name, -1, wname, sizeof(wname) / sizeof(WCHAR));
232 return module_find_by_name(pcs, wname, type);
235 /***********************************************************************
236 * module_get_container
239 struct module* module_get_container(const struct process* pcs,
240 const struct module* inner)
242 struct module* module;
244 for (module = pcs->lmodules; module; module = module->next)
246 if (module != inner &&
247 module->module.BaseOfImage <= inner->module.BaseOfImage &&
248 module->module.BaseOfImage + module->module.ImageSize >=
249 inner->module.BaseOfImage + inner->module.ImageSize)
250 return module;
252 return NULL;
255 /***********************************************************************
256 * module_get_containee
259 struct module* module_get_containee(const struct process* pcs,
260 const struct module* outter)
262 struct module* module;
264 for (module = pcs->lmodules; module; module = module->next)
266 if (module != outter &&
267 outter->module.BaseOfImage <= module->module.BaseOfImage &&
268 outter->module.BaseOfImage + outter->module.ImageSize >=
269 module->module.BaseOfImage + module->module.ImageSize)
270 return module;
272 return NULL;
275 /******************************************************************
276 * module_get_debug
278 * get the debug information from a module:
279 * - if the module's type is deferred, then force loading of debug info (and return
280 * the module itself)
281 * - if the module has no debug info and has an ELF container, then return the ELF
282 * container (and also force the ELF container's debug info loading if deferred)
283 * - otherwise return the module itself if it has some debug info
285 BOOL module_get_debug(struct module_pair* pair)
287 IMAGEHLP_DEFERRED_SYMBOL_LOADW64 idslW64;
289 if (!pair->requested) return FALSE;
290 /* for a PE builtin, always get info from container */
291 if (!(pair->effective = module_get_container(pair->pcs, pair->requested)))
292 pair->effective = pair->requested;
293 /* if deferred, force loading */
294 if (pair->effective->module.SymType == SymDeferred)
296 BOOL ret;
298 if (pair->effective->is_virtual) ret = FALSE;
299 else switch (pair->effective->type)
301 case DMT_ELF:
302 ret = elf_load_debug_info(pair->effective, NULL);
303 break;
304 case DMT_PE:
305 idslW64.SizeOfStruct = sizeof(idslW64);
306 idslW64.BaseOfImage = pair->effective->module.BaseOfImage;
307 idslW64.CheckSum = pair->effective->module.CheckSum;
308 idslW64.TimeDateStamp = pair->effective->module.TimeDateStamp;
309 memcpy(idslW64.FileName, pair->effective->module.ImageName,
310 sizeof(idslW64.FileName));
311 idslW64.Reparse = FALSE;
312 idslW64.hFile = INVALID_HANDLE_VALUE;
314 pcs_callback(pair->pcs, CBA_DEFERRED_SYMBOL_LOAD_START, &idslW64);
315 ret = pe_load_debug_info(pair->pcs, pair->effective);
316 pcs_callback(pair->pcs,
317 ret ? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE : CBA_DEFERRED_SYMBOL_LOAD_FAILURE,
318 &idslW64);
319 break;
320 default:
321 ret = FALSE;
322 break;
324 if (!ret) pair->effective->module.SymType = SymNone;
325 assert(pair->effective->module.SymType != SymDeferred);
326 pair->effective->module.NumSyms = pair->effective->ht_symbols.num_elts;
328 return pair->effective->module.SymType != SymNone;
331 /***********************************************************************
332 * module_find_by_addr
334 * either the addr where module is loaded, or any address inside the
335 * module
337 struct module* module_find_by_addr(const struct process* pcs, unsigned long addr,
338 enum module_type type)
340 struct module* module;
342 if (type == DMT_UNKNOWN)
344 if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
345 (module = module_find_by_addr(pcs, addr, DMT_ELF)))
346 return module;
348 else
350 for (module = pcs->lmodules; module; module = module->next)
352 if (type == module->type && addr >= module->module.BaseOfImage &&
353 addr < module->module.BaseOfImage + module->module.ImageSize)
354 return module;
357 SetLastError(ERROR_INVALID_ADDRESS);
358 return module;
361 /******************************************************************
362 * module_is_elf_container_loaded
364 * checks whether the ELF container, for a (supposed) PE builtin is
365 * already loaded
367 static BOOL module_is_elf_container_loaded(struct process* pcs,
368 const WCHAR* ImageName, DWORD base)
370 size_t len;
371 struct module* module;
372 LPCWSTR filename, modname;
374 if (!base) return FALSE;
375 filename = get_filename(ImageName, NULL);
376 len = strlenW(filename);
378 for (module = pcs->lmodules; module; module = module->next)
380 if (module->type == DMT_ELF &&
381 base >= module->module.BaseOfImage &&
382 base < module->module.BaseOfImage + module->module.ImageSize)
384 modname = get_filename(module->module.LoadedImageName, NULL);
385 if (!strncmpiW(modname, filename, len) &&
386 !memcmp(modname + len, S_DotSoW, 3 * sizeof(WCHAR)))
388 return TRUE;
392 /* likely a native PE module */
393 WARN("Couldn't find container for %s\n", debugstr_w(ImageName));
394 return FALSE;
397 /******************************************************************
398 * module_get_type_by_name
400 * Guesses a filename type from its extension
402 enum module_type module_get_type_by_name(const WCHAR* name)
404 const WCHAR*ptr;
405 int len = strlenW(name);
407 /* check for terminating .so or .so.[digit] */
408 ptr = strrchrW(name, '.');
409 if (ptr)
411 if (!strcmpW(ptr, S_DotSoW) ||
412 (isdigit(ptr[1]) && !ptr[2] && ptr >= name + 3 && !memcmp(ptr - 3, S_DotSoW, 3)))
413 return DMT_ELF;
414 else if (!strcmpiW(ptr, S_DotPdbW))
415 return DMT_PDB;
417 /* wine-[kp]thread is also an ELF module */
418 else if (((len > 12 && name[len - 13] == '/') || len == 12) &&
419 (!strcmpiW(name + len - 12, S_WinePThreadW) ||
420 !strcmpiW(name + len - 12, S_WineKThreadW)))
422 return DMT_ELF;
424 return DMT_PE;
427 /***********************************************************************
428 * SymLoadModule (DBGHELP.@)
430 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, const char* ImageName,
431 const char* ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
433 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll,
434 SizeOfDll, NULL, 0);
437 /***********************************************************************
438 * SymLoadModuleEx (DBGHELP.@)
440 DWORD64 WINAPI SymLoadModuleEx(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
441 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD DllSize,
442 PMODLOAD_DATA Data, DWORD Flags)
444 LPWSTR wImageName, wModuleName;
445 unsigned len;
446 DWORD64 ret;
448 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
449 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
450 wine_dbgstr_longlong(BaseOfDll), DllSize, Data, Flags);
452 if (ImageName)
454 len = MultiByteToWideChar(CP_ACP, 0, ImageName, -1, NULL, 0);
455 wImageName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
456 MultiByteToWideChar(CP_ACP, 0, ImageName, -1, wImageName, len);
458 else wImageName = NULL;
459 if (ModuleName)
461 len = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
462 wModuleName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
463 MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, wModuleName, len);
465 else wModuleName = NULL;
467 ret = SymLoadModuleExW(hProcess, hFile, wImageName, wModuleName,
468 BaseOfDll, DllSize, Data, Flags);
469 HeapFree(GetProcessHeap(), 0, wImageName);
470 HeapFree(GetProcessHeap(), 0, wModuleName);
471 return ret;
474 /***********************************************************************
475 * SymLoadModuleExW (DBGHELP.@)
477 DWORD64 WINAPI SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageName,
478 PCWSTR wModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll,
479 PMODLOAD_DATA Data, DWORD Flags)
481 struct process* pcs;
482 struct module* module = NULL;
484 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
485 hProcess, hFile, debugstr_w(wImageName), debugstr_w(wModuleName),
486 wine_dbgstr_longlong(BaseOfDll), SizeOfDll, Data, Flags);
488 if (Data)
489 FIXME("Unsupported load data parameter %p for %s\n",
490 Data, debugstr_w(wImageName));
491 if (!validate_addr64(BaseOfDll)) return FALSE;
493 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
495 if (Flags & SLMFLAG_VIRTUAL)
497 module = module_new(pcs, wImageName, module_get_type_by_name(wImageName),
498 TRUE, (DWORD)BaseOfDll, SizeOfDll, 0, 0);
499 if (!module) return FALSE;
500 if (wModuleName) module_set_module(module, wModuleName);
501 module->module.SymType = SymVirtual;
503 return TRUE;
505 if (Flags & ~(SLMFLAG_VIRTUAL))
506 FIXME("Unsupported Flags %08x for %s\n", Flags, debugstr_w(wImageName));
508 /* force transparent ELF loading / unloading */
509 elf_synchronize_module_list(pcs);
511 /* this is a Wine extension to the API just to redo the synchronisation */
512 if (!wImageName && !hFile) return 0;
514 if (module_is_elf_container_loaded(pcs, wImageName, BaseOfDll))
516 /* force the loading of DLL as builtin */
517 if ((module = pe_load_module_from_pcs(pcs, wImageName, wModuleName,
518 BaseOfDll, SizeOfDll)))
519 goto done;
520 WARN("Couldn't locate %s\n", debugstr_w(wImageName));
521 return 0;
523 TRACE("Assuming %s as native DLL\n", debugstr_w(wImageName));
524 if (!(module = pe_load_module(pcs, wImageName, hFile, BaseOfDll, SizeOfDll)))
526 if (module_get_type_by_name(wImageName) == DMT_ELF &&
527 (module = elf_load_module(pcs, wImageName, BaseOfDll)))
528 goto done;
529 FIXME("Should have successfully loaded debug information for image %s\n",
530 debugstr_w(wImageName));
531 if ((module = pe_load_module_from_pcs(pcs, wImageName, wModuleName,
532 BaseOfDll, SizeOfDll)))
533 goto done;
534 WARN("Couldn't locate %s\n", debugstr_w(wImageName));
535 return 0;
537 module->module.NumSyms = module->ht_symbols.num_elts;
538 done:
539 /* by default pe_load_module fills module.ModuleName from a derivation
540 * of ImageName. Overwrite it, if we have better information
542 if (wModuleName)
543 module_set_module(module, wModuleName);
544 lstrcpynW(module->module.ImageName, wImageName,
545 sizeof(module->module.ImageName) / sizeof(CHAR));
547 return module->module.BaseOfImage;
550 /***********************************************************************
551 * SymLoadModule64 (DBGHELP.@)
553 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
554 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
556 if (!validate_addr64(BaseOfDll)) return FALSE;
557 return SymLoadModule(hProcess, hFile, ImageName, ModuleName, (DWORD)BaseOfDll, SizeOfDll);
560 /******************************************************************
561 * module_remove
564 BOOL module_remove(struct process* pcs, struct module* module)
566 struct module** p;
568 TRACE("%s (%p)\n", module->module_name, module);
569 hash_table_destroy(&module->ht_symbols);
570 hash_table_destroy(&module->ht_types);
571 HeapFree(GetProcessHeap(), 0, (char*)module->sources);
572 HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
573 HeapFree(GetProcessHeap(), 0, module->dwarf2_info);
574 pool_destroy(&module->pool);
575 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
576 * so do we
578 for (p = &pcs->lmodules; *p; p = &(*p)->next)
580 if (*p == module)
582 *p = module->next;
583 HeapFree(GetProcessHeap(), 0, module);
584 return TRUE;
587 FIXME("This shouldn't happen\n");
588 return FALSE;
591 /******************************************************************
592 * SymUnloadModule (DBGHELP.@)
595 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
597 struct process* pcs;
598 struct module* module;
600 pcs = process_find_by_handle(hProcess);
601 if (!pcs) return FALSE;
602 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
603 if (!module) return FALSE;
604 return module_remove(pcs, module);
607 /******************************************************************
608 * SymUnloadModule64 (DBGHELP.@)
611 BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
613 struct process* pcs;
614 struct module* module;
616 pcs = process_find_by_handle(hProcess);
617 if (!pcs) return FALSE;
618 if (!validate_addr64(BaseOfDll)) return FALSE;
619 module = module_find_by_addr(pcs, (DWORD)BaseOfDll, DMT_UNKNOWN);
620 if (!module) return FALSE;
621 return module_remove(pcs, module);
624 /******************************************************************
625 * SymEnumerateModules (DBGHELP.@)
628 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
629 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
630 PVOID UserContext)
632 struct process* pcs = process_find_by_handle(hProcess);
633 struct module* module;
635 if (!pcs) return FALSE;
637 for (module = pcs->lmodules; module; module = module->next)
639 if (!(dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES) && module->type == DMT_ELF)
640 continue;
641 if (!EnumModulesCallback(module->module_name,
642 module->module.BaseOfImage, UserContext))
643 break;
645 return TRUE;
648 /******************************************************************
649 * SymEnumerateModules64 (DBGHELP.@)
652 BOOL WINAPI SymEnumerateModules64(HANDLE hProcess,
653 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,
654 PVOID UserContext)
656 struct process* pcs = process_find_by_handle(hProcess);
657 struct module* module;
659 if (!pcs) return FALSE;
661 for (module = pcs->lmodules; module; module = module->next)
663 if (!(dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES) && module->type == DMT_ELF)
664 continue;
665 if (!EnumModulesCallback(module->module_name,
666 module->module.BaseOfImage, UserContext))
667 break;
669 return TRUE;
672 /******************************************************************
673 * EnumerateLoadedModules64 (DBGHELP.@)
676 struct enum_load_modW64_64
678 PENUMLOADED_MODULES_CALLBACK64 cb;
679 PVOID user;
680 char module[MAX_PATH];
683 static BOOL CALLBACK enum_load_modW64_64(PWSTR name, DWORD64 base, ULONG size,
684 PVOID user)
686 struct enum_load_modW64_64* x = user;
688 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
689 return x->cb(x->module, base, size, x->user);
692 BOOL WINAPI EnumerateLoadedModules64(HANDLE hProcess,
693 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
694 PVOID UserContext)
696 struct enum_load_modW64_64 x;
698 x.cb = EnumLoadedModulesCallback;
699 x.user = UserContext;
701 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_64, &x);
704 /******************************************************************
705 * EnumerateLoadedModules (DBGHELP.@)
708 struct enum_load_modW64_32
710 PENUMLOADED_MODULES_CALLBACK cb;
711 PVOID user;
712 char module[MAX_PATH];
715 static BOOL CALLBACK enum_load_modW64_32(PWSTR name, DWORD64 base, ULONG size,
716 PVOID user)
718 struct enum_load_modW64_32* x = user;
719 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
720 return x->cb(x->module, (DWORD)base, size, x->user);
723 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
724 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
725 PVOID UserContext)
727 struct enum_load_modW64_32 x;
729 x.cb = EnumLoadedModulesCallback;
730 x.user = UserContext;
732 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_32, &x);
735 /******************************************************************
736 * EnumerateLoadedModulesW64 (DBGHELP.@)
739 BOOL WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
740 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback,
741 PVOID UserContext)
743 HMODULE* hMods;
744 WCHAR baseW[256], modW[256];
745 DWORD i, sz;
746 MODULEINFO mi;
748 hMods = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods[0]));
749 if (!hMods) return FALSE;
751 if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
753 /* hProcess should also be a valid process handle !! */
754 FIXME("If this happens, bump the number in mod\n");
755 HeapFree(GetProcessHeap(), 0, hMods);
756 return FALSE;
758 sz /= sizeof(HMODULE);
759 for (i = 0; i < sz; i++)
761 if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
762 !GetModuleBaseNameW(hProcess, hMods[i], baseW, sizeof(baseW) / sizeof(WCHAR)))
763 continue;
764 module_fill_module(baseW, modW, sizeof(modW) / sizeof(CHAR));
765 EnumLoadedModulesCallback(modW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage,
766 UserContext);
768 HeapFree(GetProcessHeap(), 0, hMods);
770 return sz != 0 && i == sz;
773 /******************************************************************
774 * SymGetModuleInfo (DBGHELP.@)
777 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
778 PIMAGEHLP_MODULE ModuleInfo)
780 IMAGEHLP_MODULE mi;
781 IMAGEHLP_MODULEW64 miw64;
783 if (sizeof(mi) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
785 miw64.SizeOfStruct = sizeof(miw64);
786 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
788 mi.SizeOfStruct = miw64.SizeOfStruct;
789 mi.BaseOfImage = miw64.BaseOfImage;
790 mi.ImageSize = miw64.ImageSize;
791 mi.TimeDateStamp = miw64.TimeDateStamp;
792 mi.CheckSum = miw64.CheckSum;
793 mi.NumSyms = miw64.NumSyms;
794 mi.SymType = miw64.SymType;
795 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
796 mi.ModuleName, sizeof(mi.ModuleName), NULL, NULL);
797 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
798 mi.ImageName, sizeof(mi.ImageName), NULL, NULL);
799 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
800 mi.LoadedImageName, sizeof(mi.LoadedImageName), NULL, NULL);
802 memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
804 return TRUE;
807 /******************************************************************
808 * SymGetModuleInfoW (DBGHELP.@)
811 BOOL WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
812 PIMAGEHLP_MODULEW ModuleInfo)
814 IMAGEHLP_MODULEW64 miw64;
815 IMAGEHLP_MODULEW miw;
817 if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
819 miw64.SizeOfStruct = sizeof(miw64);
820 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
822 miw.SizeOfStruct = miw64.SizeOfStruct;
823 miw.BaseOfImage = miw64.BaseOfImage;
824 miw.ImageSize = miw64.ImageSize;
825 miw.TimeDateStamp = miw64.TimeDateStamp;
826 miw.CheckSum = miw64.CheckSum;
827 miw.NumSyms = miw64.NumSyms;
828 miw.SymType = miw64.SymType;
829 strcpyW(miw.ModuleName, miw64.ModuleName);
830 strcpyW(miw.ImageName, miw64.ImageName);
831 strcpyW(miw.LoadedImageName, miw64.LoadedImageName);
832 memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
834 return TRUE;
837 /******************************************************************
838 * SymGetModuleInfo64 (DBGHELP.@)
841 BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
842 PIMAGEHLP_MODULE64 ModuleInfo)
844 IMAGEHLP_MODULE64 mi64;
845 IMAGEHLP_MODULEW64 miw64;
847 if (sizeof(mi64) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
849 miw64.SizeOfStruct = sizeof(miw64);
850 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
852 mi64.SizeOfStruct = miw64.SizeOfStruct;
853 mi64.BaseOfImage = miw64.BaseOfImage;
854 mi64.ImageSize = miw64.ImageSize;
855 mi64.TimeDateStamp = miw64.TimeDateStamp;
856 mi64.CheckSum = miw64.CheckSum;
857 mi64.NumSyms = miw64.NumSyms;
858 mi64.SymType = miw64.SymType;
859 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
860 mi64.ModuleName, sizeof(mi64.ModuleName), NULL, NULL);
861 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
862 mi64.ImageName, sizeof(mi64.ImageName), NULL, NULL);
863 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
864 mi64.LoadedImageName, sizeof(mi64.LoadedImageName), NULL, NULL);
865 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedPdbName, -1,
866 mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName), NULL, NULL);
868 mi64.CVSig = miw64.CVSig;
869 WideCharToMultiByte(CP_ACP, 0, miw64.CVData, -1,
870 mi64.CVData, sizeof(mi64.CVData), NULL, NULL);
871 mi64.PdbSig = miw64.PdbSig;
872 mi64.PdbSig70 = miw64.PdbSig70;
873 mi64.PdbAge = miw64.PdbAge;
874 mi64.PdbUnmatched = miw64.PdbUnmatched;
875 mi64.DbgUnmatched = miw64.DbgUnmatched;
876 mi64.LineNumbers = miw64.LineNumbers;
877 mi64.GlobalSymbols = miw64.GlobalSymbols;
878 mi64.TypeInfo = miw64.TypeInfo;
879 mi64.SourceIndexed = miw64.SourceIndexed;
880 mi64.Publics = miw64.Publics;
882 memcpy(ModuleInfo, &mi64, ModuleInfo->SizeOfStruct);
884 return TRUE;
887 /******************************************************************
888 * SymGetModuleInfoW64 (DBGHELP.@)
891 BOOL WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr,
892 PIMAGEHLP_MODULEW64 ModuleInfo)
894 struct process* pcs = process_find_by_handle(hProcess);
895 struct module* module;
896 IMAGEHLP_MODULEW64 miw64;
898 TRACE("%p %s %p\n", hProcess, wine_dbgstr_longlong(dwAddr), ModuleInfo);
900 if (!pcs) return FALSE;
901 if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
902 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
903 if (!module) return FALSE;
905 miw64 = module->module;
907 /* update debug information from container if any */
908 if (module->module.SymType == SymNone)
910 module = module_get_container(pcs, module);
911 if (module && module->module.SymType != SymNone)
913 miw64.SymType = module->module.SymType;
914 miw64.NumSyms = module->module.NumSyms;
917 memcpy(ModuleInfo, &miw64, ModuleInfo->SizeOfStruct);
918 return TRUE;
921 /***********************************************************************
922 * SymGetModuleBase (DBGHELP.@)
924 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
926 struct process* pcs = process_find_by_handle(hProcess);
927 struct module* module;
929 if (!pcs) return 0;
930 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
931 if (!module) return 0;
932 return module->module.BaseOfImage;
935 /***********************************************************************
936 * SymGetModuleBase64 (DBGHELP.@)
938 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
940 if (!validate_addr64(dwAddr)) return 0;
941 return SymGetModuleBase(hProcess, (DWORD)dwAddr);
944 /******************************************************************
945 * module_reset_debug_info
946 * Removes any debug information linked to a given module.
948 void module_reset_debug_info(struct module* module)
950 module->sortlist_valid = TRUE;
951 module->addr_sorttab = NULL;
952 hash_table_destroy(&module->ht_symbols);
953 module->ht_symbols.num_buckets = 0;
954 module->ht_symbols.buckets = NULL;
955 hash_table_destroy(&module->ht_types);
956 module->ht_types.num_buckets = 0;
957 module->ht_types.buckets = NULL;
958 module->vtypes.num_elts = 0;
959 hash_table_destroy(&module->ht_symbols);
960 module->sources_used = module->sources_alloc = 0;
961 module->sources = NULL;