push 0c258732cb75565633c2f709ca58b227c9f8ae60
[wine/hacks.git] / dlls / dbghelp / module.c
blob59daa0ddcb350b82a79df05f5e370417dbd090e4
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 "winternl.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_DotPdbW[] = {'.','p','d','b','\0'};
39 const WCHAR S_WinePThreadW[] = {'w','i','n','e','-','p','t','h','r','e','a','d','\0'};
40 const WCHAR S_WineKThreadW[] = {'w','i','n','e','-','k','t','h','r','e','a','d','\0'};
41 const WCHAR S_SlashW[] = {'/','\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 const WCHAR* get_filename(const WCHAR* name, const WCHAR* endptr)
68 const WCHAR* ptr;
70 if (!endptr) endptr = name + strlenW(name);
71 for (ptr = endptr - 1; ptr >= name; ptr--)
73 if (*ptr == '/' || *ptr == '\\') break;
75 return ++ptr;
78 static void module_fill_module(const WCHAR* in, WCHAR* out, size_t size)
80 const WCHAR *ptr, *endptr;
81 size_t len, l;
83 ptr = get_filename(in, endptr = in + strlenW(in));
84 len = min(endptr - ptr, size - 1);
85 memcpy(out, ptr, len * sizeof(WCHAR));
86 out[len] = '\0';
87 if (len > 4 && (l = match_ext(out, len)))
88 out[len - l] = '\0';
89 else if (len > 12 &&
90 (!strcmpiW(out + len - 12, S_WinePThreadW) ||
91 !strcmpiW(out + len - 12, S_WineKThreadW)))
92 lstrcpynW(out, S_WineLoaderW, size);
93 else
95 if (len > 3 && !strcmpiW(&out[len - 3], S_DotSoW) &&
96 (l = match_ext(out, len - 3)))
97 strcpyW(&out[len - l - 3], S_ElfW);
99 while ((*out = tolowerW(*out))) out++;
102 void module_set_module(struct module* module, const WCHAR* name)
104 module_fill_module(name, module->module.ModuleName, sizeof(module->module.ModuleName));
105 WideCharToMultiByte(CP_ACP, 0, module->module.ModuleName, -1,
106 module->module_name, sizeof(module->module_name),
107 NULL, NULL);
110 static const char* get_module_type(enum module_type type, BOOL virtual)
112 switch (type)
114 case DMT_ELF: return virtual ? "Virtual ELF" : "ELF";
115 case DMT_PE: return virtual ? "Virtual PE" : "PE";
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);
131 if (!(module = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*module))))
132 return NULL;
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,
139 debugstr_w(name));
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;
168 module->type = type;
169 module->is_virtual = virtual ? TRUE : FALSE;
170 module->sortlist_valid = FALSE;
171 module->addr_sorttab = NULL;
172 /* FIXME: this seems a bit too high (on a per module basis)
173 * need some statistics about this
175 hash_table_init(&module->pool, &module->ht_symbols, 4096);
176 hash_table_init(&module->pool, &module->ht_types, 4096);
177 vector_init(&module->vtypes, sizeof(struct symt*), 32);
179 module->sources_used = 0;
180 module->sources_alloc = 0;
181 module->sources = 0;
183 return module;
186 /***********************************************************************
187 * module_find_by_name
190 struct module* module_find_by_name(const struct process* pcs, const WCHAR* name)
192 struct module* module;
194 for (module = pcs->lmodules; module; module = module->next)
196 if (!strcmpiW(name, module->module.ModuleName)) return module;
198 SetLastError(ERROR_INVALID_NAME);
199 return NULL;
202 struct module* module_find_by_nameA(const struct process* pcs, const char* name)
204 WCHAR wname[MAX_PATH];
206 MultiByteToWideChar(CP_ACP, 0, name, -1, wname, sizeof(wname) / sizeof(WCHAR));
207 return module_find_by_name(pcs, wname);
210 /***********************************************************************
211 * module_is_already_loaded
214 struct module* module_is_already_loaded(const struct process* pcs, const WCHAR* name)
216 struct module* module;
217 const WCHAR* filename;
219 /* first compare the loaded image name... */
220 for (module = pcs->lmodules; module; module = module->next)
222 if (!strcmpiW(name, module->module.LoadedImageName))
223 return module;
225 /* then compare the standard filenames (without the path) ... */
226 filename = get_filename(name, NULL);
227 for (module = pcs->lmodules; module; module = module->next)
229 if (!strcmpiW(filename, get_filename(module->module.LoadedImageName, NULL)))
230 return module;
232 SetLastError(ERROR_INVALID_NAME);
233 return NULL;
236 /***********************************************************************
237 * module_get_container
240 struct module* module_get_container(const struct process* pcs,
241 const struct module* inner)
243 struct module* module;
245 for (module = pcs->lmodules; module; module = module->next)
247 if (module != inner &&
248 module->module.BaseOfImage <= inner->module.BaseOfImage &&
249 module->module.BaseOfImage + module->module.ImageSize >=
250 inner->module.BaseOfImage + inner->module.ImageSize)
251 return module;
253 return NULL;
256 /***********************************************************************
257 * module_get_containee
260 struct module* module_get_containee(const struct process* pcs,
261 const struct module* outter)
263 struct module* module;
265 for (module = pcs->lmodules; module; module = module->next)
267 if (module != outter &&
268 outter->module.BaseOfImage <= module->module.BaseOfImage &&
269 outter->module.BaseOfImage + outter->module.ImageSize >=
270 module->module.BaseOfImage + module->module.ImageSize)
271 return module;
273 return NULL;
276 /******************************************************************
277 * module_get_debug
279 * get the debug information from a module:
280 * - if the module's type is deferred, then force loading of debug info (and return
281 * the module itself)
282 * - if the module has no debug info and has an ELF container, then return the ELF
283 * container (and also force the ELF container's debug info loading if deferred)
284 * - otherwise return the module itself if it has some debug info
286 BOOL module_get_debug(struct module_pair* pair)
288 IMAGEHLP_DEFERRED_SYMBOL_LOADW64 idslW64;
290 if (!pair->requested) return FALSE;
291 /* for a PE builtin, always get info from container */
292 if (!(pair->effective = module_get_container(pair->pcs, pair->requested)))
293 pair->effective = pair->requested;
294 /* if deferred, force loading */
295 if (pair->effective->module.SymType == SymDeferred)
297 BOOL ret;
299 if (pair->effective->is_virtual) ret = FALSE;
300 else switch (pair->effective->type)
302 case DMT_ELF:
303 ret = elf_load_debug_info(pair->effective, NULL);
304 break;
305 case DMT_PE:
306 idslW64.SizeOfStruct = sizeof(idslW64);
307 idslW64.BaseOfImage = pair->effective->module.BaseOfImage;
308 idslW64.CheckSum = pair->effective->module.CheckSum;
309 idslW64.TimeDateStamp = pair->effective->module.TimeDateStamp;
310 memcpy(idslW64.FileName, pair->effective->module.ImageName,
311 sizeof(idslW64.FileName));
312 idslW64.Reparse = FALSE;
313 idslW64.hFile = INVALID_HANDLE_VALUE;
315 pcs_callback(pair->pcs, CBA_DEFERRED_SYMBOL_LOAD_START, &idslW64);
316 ret = pe_load_debug_info(pair->pcs, pair->effective);
317 pcs_callback(pair->pcs,
318 ret ? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE : CBA_DEFERRED_SYMBOL_LOAD_FAILURE,
319 &idslW64);
320 break;
321 default:
322 ret = FALSE;
323 break;
325 if (!ret) pair->effective->module.SymType = SymNone;
326 assert(pair->effective->module.SymType != SymDeferred);
327 pair->effective->module.NumSyms = pair->effective->ht_symbols.num_elts;
329 return pair->effective->module.SymType != SymNone;
332 /***********************************************************************
333 * module_find_by_addr
335 * either the addr where module is loaded, or any address inside the
336 * module
338 struct module* module_find_by_addr(const struct process* pcs, unsigned long addr,
339 enum module_type type)
341 struct module* module;
343 if (type == DMT_UNKNOWN)
345 if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
346 (module = module_find_by_addr(pcs, addr, DMT_ELF)))
347 return module;
349 else
351 for (module = pcs->lmodules; module; module = module->next)
353 if (type == module->type && addr >= module->module.BaseOfImage &&
354 addr < module->module.BaseOfImage + module->module.ImageSize)
355 return module;
358 SetLastError(ERROR_INVALID_ADDRESS);
359 return module;
362 /******************************************************************
363 * module_is_elf_container_loaded
365 * checks whether the ELF container, for a (supposed) PE builtin is
366 * already loaded
368 static BOOL module_is_elf_container_loaded(const struct process* pcs,
369 const WCHAR* ImageName, DWORD base)
371 size_t len;
372 struct module* module;
373 PCWSTR filename, modname;
375 if (!base) return FALSE;
376 filename = get_filename(ImageName, NULL);
377 len = strlenW(filename);
379 for (module = pcs->lmodules; module; module = module->next)
381 if (module->type == DMT_ELF &&
382 base >= module->module.BaseOfImage &&
383 base < module->module.BaseOfImage + module->module.ImageSize)
385 modname = get_filename(module->module.LoadedImageName, NULL);
386 if (!strncmpiW(modname, filename, len) &&
387 !memcmp(modname + len, S_DotSoW, 3 * sizeof(WCHAR)))
389 return TRUE;
393 /* likely a native PE module */
394 WARN("Couldn't find container for %s\n", debugstr_w(ImageName));
395 return FALSE;
398 /******************************************************************
399 * module_get_type_by_name
401 * Guesses a filename type from its extension
403 enum module_type module_get_type_by_name(const WCHAR* name)
405 const WCHAR*ptr;
406 int len = strlenW(name);
408 /* check for terminating .so or .so.[digit] */
409 ptr = strrchrW(name, '.');
410 if (ptr)
412 if (!strcmpW(ptr, S_DotSoW) ||
413 (isdigit(ptr[1]) && !ptr[2] && ptr >= name + 3 && !memcmp(ptr - 3, S_DotSoW, 3)))
414 return DMT_ELF;
415 else if (!strcmpiW(ptr, S_DotPdbW))
416 return DMT_PDB;
418 /* wine-[kp]thread is also an ELF module */
419 else if (((len > 12 && name[len - 13] == '/') || len == 12) &&
420 (!strcmpiW(name + len - 12, S_WinePThreadW) ||
421 !strcmpiW(name + len - 12, S_WineKThreadW)))
423 return DMT_ELF;
425 return DMT_PE;
428 /***********************************************************************
429 * SymLoadModule (DBGHELP.@)
431 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
432 PCSTR ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
434 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll,
435 SizeOfDll, NULL, 0);
438 /***********************************************************************
439 * SymLoadModuleEx (DBGHELP.@)
441 DWORD64 WINAPI SymLoadModuleEx(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
442 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD DllSize,
443 PMODLOAD_DATA Data, DWORD Flags)
445 PWSTR wImageName, wModuleName;
446 unsigned len;
447 DWORD64 ret;
449 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
450 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
451 wine_dbgstr_longlong(BaseOfDll), DllSize, Data, Flags);
453 if (ImageName)
455 len = MultiByteToWideChar(CP_ACP, 0, ImageName, -1, NULL, 0);
456 wImageName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
457 MultiByteToWideChar(CP_ACP, 0, ImageName, -1, wImageName, len);
459 else wImageName = NULL;
460 if (ModuleName)
462 len = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
463 wModuleName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
464 MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, wModuleName, len);
466 else wModuleName = NULL;
468 ret = SymLoadModuleExW(hProcess, hFile, wImageName, wModuleName,
469 BaseOfDll, DllSize, Data, Flags);
470 HeapFree(GetProcessHeap(), 0, wImageName);
471 HeapFree(GetProcessHeap(), 0, wModuleName);
472 return ret;
475 /***********************************************************************
476 * SymLoadModuleExW (DBGHELP.@)
478 DWORD64 WINAPI SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageName,
479 PCWSTR wModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll,
480 PMODLOAD_DATA Data, DWORD Flags)
482 struct process* pcs;
483 struct module* module = NULL;
485 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
486 hProcess, hFile, debugstr_w(wImageName), debugstr_w(wModuleName),
487 wine_dbgstr_longlong(BaseOfDll), SizeOfDll, Data, Flags);
489 if (Data)
490 FIXME("Unsupported load data parameter %p for %s\n",
491 Data, debugstr_w(wImageName));
492 if (!validate_addr64(BaseOfDll)) return FALSE;
494 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
496 if (Flags & SLMFLAG_VIRTUAL)
498 module = module_new(pcs, wImageName, module_get_type_by_name(wImageName),
499 TRUE, (DWORD)BaseOfDll, SizeOfDll, 0, 0);
500 if (!module) return FALSE;
501 if (wModuleName) module_set_module(module, wModuleName);
502 module->module.SymType = SymVirtual;
504 return TRUE;
506 if (Flags & ~(SLMFLAG_VIRTUAL))
507 FIXME("Unsupported Flags %08x for %s\n", Flags, debugstr_w(wImageName));
509 /* force transparent ELF loading / unloading */
510 elf_synchronize_module_list(pcs);
512 /* this is a Wine extension to the API just to redo the synchronisation */
513 if (!wImageName && !hFile) return 0;
515 /* check if the module is already loaded, or if it's a builtin PE module with
516 * an containing ELF module
518 if (wImageName)
520 module = module_is_already_loaded(pcs, wImageName);
521 if (!module && module_is_elf_container_loaded(pcs, wImageName, BaseOfDll))
523 /* force the loading of DLL as builtin */
524 module = pe_load_builtin_module(pcs, wImageName, BaseOfDll, SizeOfDll);
527 if (!module)
529 /* otherwise, try a regular PE module */
530 if (!(module = pe_load_native_module(pcs, wImageName, hFile, BaseOfDll, SizeOfDll)))
532 /* and finally and ELF module */
533 if (module_get_type_by_name(wImageName) == DMT_ELF)
534 module = elf_load_module(pcs, wImageName, BaseOfDll);
537 if (!module)
539 WARN("Couldn't locate %s\n", debugstr_w(wImageName));
540 return 0;
542 module->module.NumSyms = module->ht_symbols.num_elts;
543 /* by default module_new fills module.ModuleName from a derivation
544 * of LoadedImageName. Overwrite it, if we have better information
546 if (wModuleName)
547 module_set_module(module, wModuleName);
548 lstrcpynW(module->module.ImageName, wImageName,
549 sizeof(module->module.ImageName) / sizeof(CHAR));
551 return module->module.BaseOfImage;
554 /***********************************************************************
555 * SymLoadModule64 (DBGHELP.@)
557 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
558 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
560 if (!validate_addr64(BaseOfDll)) return FALSE;
561 return SymLoadModule(hProcess, hFile, ImageName, ModuleName, (DWORD)BaseOfDll, SizeOfDll);
564 /******************************************************************
565 * module_remove
568 BOOL module_remove(struct process* pcs, struct module* module)
570 struct module** p;
572 TRACE("%s (%p)\n", debugstr_w(module->module.ModuleName), module);
573 hash_table_destroy(&module->ht_symbols);
574 hash_table_destroy(&module->ht_types);
575 HeapFree(GetProcessHeap(), 0, (char*)module->sources);
576 HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
577 HeapFree(GetProcessHeap(), 0, module->dwarf2_info);
578 pool_destroy(&module->pool);
579 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
580 * so do we
582 for (p = &pcs->lmodules; *p; p = &(*p)->next)
584 if (*p == module)
586 *p = module->next;
587 HeapFree(GetProcessHeap(), 0, module);
588 return TRUE;
591 FIXME("This shouldn't happen\n");
592 return FALSE;
595 /******************************************************************
596 * SymUnloadModule (DBGHELP.@)
599 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
601 struct process* pcs;
602 struct module* module;
604 pcs = process_find_by_handle(hProcess);
605 if (!pcs) return FALSE;
606 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
607 if (!module) return FALSE;
608 return module_remove(pcs, module);
611 /******************************************************************
612 * SymUnloadModule64 (DBGHELP.@)
615 BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
617 struct process* pcs;
618 struct module* module;
620 pcs = process_find_by_handle(hProcess);
621 if (!pcs) return FALSE;
622 if (!validate_addr64(BaseOfDll)) return FALSE;
623 module = module_find_by_addr(pcs, (DWORD)BaseOfDll, DMT_UNKNOWN);
624 if (!module) return FALSE;
625 return module_remove(pcs, module);
628 /******************************************************************
629 * SymEnumerateModules (DBGHELP.@)
632 struct enum_modW64_32
634 PSYM_ENUMMODULES_CALLBACK cb;
635 PVOID user;
636 char module[MAX_PATH];
639 static BOOL CALLBACK enum_modW64_32(PCWSTR name, DWORD64 base, PVOID user)
641 struct enum_modW64_32* x = user;
643 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
644 return x->cb(x->module, (DWORD)base, x->user);
647 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
648 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
649 PVOID UserContext)
651 struct enum_modW64_32 x;
653 x.cb = EnumModulesCallback;
654 x.user = UserContext;
656 return SymEnumerateModulesW64(hProcess, enum_modW64_32, &x);
659 /******************************************************************
660 * SymEnumerateModules64 (DBGHELP.@)
663 struct enum_modW64_64
665 PSYM_ENUMMODULES_CALLBACK64 cb;
666 PVOID user;
667 char module[MAX_PATH];
670 static BOOL CALLBACK enum_modW64_64(PCWSTR name, DWORD64 base, PVOID user)
672 struct enum_modW64_64* x = user;
674 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
675 return x->cb(x->module, base, x->user);
678 BOOL WINAPI SymEnumerateModules64(HANDLE hProcess,
679 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,
680 PVOID UserContext)
682 struct enum_modW64_64 x;
684 x.cb = EnumModulesCallback;
685 x.user = UserContext;
687 return SymEnumerateModulesW64(hProcess, enum_modW64_64, &x);
690 /******************************************************************
691 * SymEnumerateModulesW64 (DBGHELP.@)
694 BOOL WINAPI SymEnumerateModulesW64(HANDLE hProcess,
695 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback,
696 PVOID UserContext)
698 struct process* pcs = process_find_by_handle(hProcess);
699 struct module* module;
701 if (!pcs) return FALSE;
703 for (module = pcs->lmodules; module; module = module->next)
705 if (!(dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES) && module->type == DMT_ELF)
706 continue;
707 if (!EnumModulesCallback(module->module.ModuleName,
708 module->module.BaseOfImage, UserContext))
709 break;
711 return TRUE;
714 /******************************************************************
715 * EnumerateLoadedModules64 (DBGHELP.@)
718 struct enum_load_modW64_64
720 PENUMLOADED_MODULES_CALLBACK64 cb;
721 PVOID user;
722 char module[MAX_PATH];
725 static BOOL CALLBACK enum_load_modW64_64(PCWSTR name, DWORD64 base, ULONG size,
726 PVOID user)
728 struct enum_load_modW64_64* x = user;
730 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
731 return x->cb(x->module, base, size, x->user);
734 BOOL WINAPI EnumerateLoadedModules64(HANDLE hProcess,
735 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
736 PVOID UserContext)
738 struct enum_load_modW64_64 x;
740 x.cb = EnumLoadedModulesCallback;
741 x.user = UserContext;
743 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_64, &x);
746 /******************************************************************
747 * EnumerateLoadedModules (DBGHELP.@)
750 struct enum_load_modW64_32
752 PENUMLOADED_MODULES_CALLBACK cb;
753 PVOID user;
754 char module[MAX_PATH];
757 static BOOL CALLBACK enum_load_modW64_32(PCWSTR name, DWORD64 base, ULONG size,
758 PVOID user)
760 struct enum_load_modW64_32* x = user;
761 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
762 return x->cb(x->module, (DWORD)base, size, x->user);
765 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
766 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
767 PVOID UserContext)
769 struct enum_load_modW64_32 x;
771 x.cb = EnumLoadedModulesCallback;
772 x.user = UserContext;
774 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_32, &x);
777 /******************************************************************
778 * EnumerateLoadedModulesW64 (DBGHELP.@)
781 BOOL WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
782 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback,
783 PVOID UserContext)
785 HMODULE* hMods;
786 WCHAR baseW[256], modW[256];
787 DWORD i, sz;
788 MODULEINFO mi;
790 hMods = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods[0]));
791 if (!hMods) return FALSE;
793 if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
795 /* hProcess should also be a valid process handle !! */
796 FIXME("If this happens, bump the number in mod\n");
797 HeapFree(GetProcessHeap(), 0, hMods);
798 return FALSE;
800 sz /= sizeof(HMODULE);
801 for (i = 0; i < sz; i++)
803 if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
804 !GetModuleBaseNameW(hProcess, hMods[i], baseW, sizeof(baseW) / sizeof(WCHAR)))
805 continue;
806 module_fill_module(baseW, modW, sizeof(modW) / sizeof(CHAR));
807 EnumLoadedModulesCallback(modW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage,
808 UserContext);
810 HeapFree(GetProcessHeap(), 0, hMods);
812 return sz != 0 && i == sz;
815 /******************************************************************
816 * SymGetModuleInfo (DBGHELP.@)
819 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
820 PIMAGEHLP_MODULE ModuleInfo)
822 IMAGEHLP_MODULE mi;
823 IMAGEHLP_MODULEW64 miw64;
825 if (sizeof(mi) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
827 miw64.SizeOfStruct = sizeof(miw64);
828 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
830 mi.SizeOfStruct = miw64.SizeOfStruct;
831 mi.BaseOfImage = miw64.BaseOfImage;
832 mi.ImageSize = miw64.ImageSize;
833 mi.TimeDateStamp = miw64.TimeDateStamp;
834 mi.CheckSum = miw64.CheckSum;
835 mi.NumSyms = miw64.NumSyms;
836 mi.SymType = miw64.SymType;
837 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
838 mi.ModuleName, sizeof(mi.ModuleName), NULL, NULL);
839 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
840 mi.ImageName, sizeof(mi.ImageName), NULL, NULL);
841 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
842 mi.LoadedImageName, sizeof(mi.LoadedImageName), NULL, NULL);
844 memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
846 return TRUE;
849 /******************************************************************
850 * SymGetModuleInfoW (DBGHELP.@)
853 BOOL WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
854 PIMAGEHLP_MODULEW ModuleInfo)
856 IMAGEHLP_MODULEW64 miw64;
857 IMAGEHLP_MODULEW miw;
859 if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
861 miw64.SizeOfStruct = sizeof(miw64);
862 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
864 miw.SizeOfStruct = miw64.SizeOfStruct;
865 miw.BaseOfImage = miw64.BaseOfImage;
866 miw.ImageSize = miw64.ImageSize;
867 miw.TimeDateStamp = miw64.TimeDateStamp;
868 miw.CheckSum = miw64.CheckSum;
869 miw.NumSyms = miw64.NumSyms;
870 miw.SymType = miw64.SymType;
871 strcpyW(miw.ModuleName, miw64.ModuleName);
872 strcpyW(miw.ImageName, miw64.ImageName);
873 strcpyW(miw.LoadedImageName, miw64.LoadedImageName);
874 memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
876 return TRUE;
879 /******************************************************************
880 * SymGetModuleInfo64 (DBGHELP.@)
883 BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
884 PIMAGEHLP_MODULE64 ModuleInfo)
886 IMAGEHLP_MODULE64 mi64;
887 IMAGEHLP_MODULEW64 miw64;
889 if (sizeof(mi64) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
891 miw64.SizeOfStruct = sizeof(miw64);
892 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
894 mi64.SizeOfStruct = miw64.SizeOfStruct;
895 mi64.BaseOfImage = miw64.BaseOfImage;
896 mi64.ImageSize = miw64.ImageSize;
897 mi64.TimeDateStamp = miw64.TimeDateStamp;
898 mi64.CheckSum = miw64.CheckSum;
899 mi64.NumSyms = miw64.NumSyms;
900 mi64.SymType = miw64.SymType;
901 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
902 mi64.ModuleName, sizeof(mi64.ModuleName), NULL, NULL);
903 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
904 mi64.ImageName, sizeof(mi64.ImageName), NULL, NULL);
905 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
906 mi64.LoadedImageName, sizeof(mi64.LoadedImageName), NULL, NULL);
907 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedPdbName, -1,
908 mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName), NULL, NULL);
910 mi64.CVSig = miw64.CVSig;
911 WideCharToMultiByte(CP_ACP, 0, miw64.CVData, -1,
912 mi64.CVData, sizeof(mi64.CVData), NULL, NULL);
913 mi64.PdbSig = miw64.PdbSig;
914 mi64.PdbSig70 = miw64.PdbSig70;
915 mi64.PdbAge = miw64.PdbAge;
916 mi64.PdbUnmatched = miw64.PdbUnmatched;
917 mi64.DbgUnmatched = miw64.DbgUnmatched;
918 mi64.LineNumbers = miw64.LineNumbers;
919 mi64.GlobalSymbols = miw64.GlobalSymbols;
920 mi64.TypeInfo = miw64.TypeInfo;
921 mi64.SourceIndexed = miw64.SourceIndexed;
922 mi64.Publics = miw64.Publics;
924 memcpy(ModuleInfo, &mi64, ModuleInfo->SizeOfStruct);
926 return TRUE;
929 /******************************************************************
930 * SymGetModuleInfoW64 (DBGHELP.@)
933 BOOL WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr,
934 PIMAGEHLP_MODULEW64 ModuleInfo)
936 struct process* pcs = process_find_by_handle(hProcess);
937 struct module* module;
938 IMAGEHLP_MODULEW64 miw64;
940 TRACE("%p %s %p\n", hProcess, wine_dbgstr_longlong(dwAddr), ModuleInfo);
942 if (!pcs) return FALSE;
943 if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
944 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
945 if (!module) return FALSE;
947 miw64 = module->module;
949 /* update debug information from container if any */
950 if (module->module.SymType == SymNone)
952 module = module_get_container(pcs, module);
953 if (module && module->module.SymType != SymNone)
955 miw64.SymType = module->module.SymType;
956 miw64.NumSyms = module->module.NumSyms;
959 memcpy(ModuleInfo, &miw64, ModuleInfo->SizeOfStruct);
960 return TRUE;
963 /***********************************************************************
964 * SymGetModuleBase (DBGHELP.@)
966 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
968 struct process* pcs = process_find_by_handle(hProcess);
969 struct module* module;
971 if (!pcs) return 0;
972 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
973 if (!module) return 0;
974 return module->module.BaseOfImage;
977 /***********************************************************************
978 * SymGetModuleBase64 (DBGHELP.@)
980 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
982 if (!validate_addr64(dwAddr)) return 0;
983 return SymGetModuleBase(hProcess, (DWORD)dwAddr);
986 /******************************************************************
987 * module_reset_debug_info
988 * Removes any debug information linked to a given module.
990 void module_reset_debug_info(struct module* module)
992 module->sortlist_valid = TRUE;
993 module->addr_sorttab = NULL;
994 hash_table_destroy(&module->ht_symbols);
995 module->ht_symbols.num_buckets = 0;
996 module->ht_symbols.buckets = NULL;
997 hash_table_destroy(&module->ht_types);
998 module->ht_types.num_buckets = 0;
999 module->ht_types.buckets = NULL;
1000 module->vtypes.num_elts = 0;
1001 hash_table_destroy(&module->ht_symbols);
1002 module->sources_used = module->sources_alloc = 0;
1003 module->sources = NULL;