dbghelp: The main executable is now always named "wine".
[wine/multimedia.git] / dlls / dbghelp / module.c
bloba7af0f584091aca2ffab1dedda4e8303a0ccd995
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 static const WCHAR S_DotDbgW[] = {'.','d','b','g','\0'};
40 const WCHAR S_WineW[] = {'w','i','n','e',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 > 4 && !strcmpiW(out + len - 4, S_WineW))
90 lstrcpynW(out, S_WineLoaderW, size);
91 else
93 if (len > 3 && !strcmpiW(&out[len - 3], S_DotSoW) &&
94 (l = match_ext(out, len - 3)))
95 strcpyW(&out[len - l - 3], S_ElfW);
97 while ((*out = tolowerW(*out))) out++;
100 void module_set_module(struct module* module, const WCHAR* name)
102 module_fill_module(name, module->module.ModuleName, sizeof(module->module.ModuleName));
103 WideCharToMultiByte(CP_ACP, 0, module->module.ModuleName, -1,
104 module->module_name, sizeof(module->module_name),
105 NULL, NULL);
108 static const char* get_module_type(enum module_type type, BOOL virtual)
110 switch (type)
112 case DMT_ELF: return virtual ? "Virtual ELF" : "ELF";
113 case DMT_PE: return virtual ? "Virtual PE" : "PE";
114 default: return "---";
118 /***********************************************************************
119 * Creates and links a new module to a process
121 struct module* module_new(struct process* pcs, const WCHAR* name,
122 enum module_type type, BOOL virtual,
123 unsigned long mod_addr, unsigned long size,
124 unsigned long stamp, unsigned long checksum)
126 struct module* module;
128 assert(type == DMT_ELF || type == DMT_PE);
129 if (!(module = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*module))))
130 return NULL;
132 module->next = pcs->lmodules;
133 pcs->lmodules = module;
135 TRACE("=> %s %08lx-%08lx %s\n",
136 get_module_type(type, virtual), mod_addr, mod_addr + size,
137 debugstr_w(name));
139 pool_init(&module->pool, 65536);
141 module->module.SizeOfStruct = sizeof(module->module);
142 module->module.BaseOfImage = mod_addr;
143 module->module.ImageSize = size;
144 module_set_module(module, name);
145 module->module.ImageName[0] = '\0';
146 lstrcpynW(module->module.LoadedImageName, name, sizeof(module->module.LoadedImageName) / sizeof(WCHAR));
147 module->module.SymType = SymNone;
148 module->module.NumSyms = 0;
149 module->module.TimeDateStamp = stamp;
150 module->module.CheckSum = checksum;
152 memset(module->module.LoadedPdbName, 0, sizeof(module->module.CVData));
153 module->module.CVSig = 0;
154 memset(module->module.CVData, 0, sizeof(module->module.CVData));
155 module->module.PdbSig = 0;
156 memset(&module->module.PdbSig70, 0, sizeof(module->module.PdbSig70));
157 module->module.PdbAge = 0;
158 module->module.PdbUnmatched = FALSE;
159 module->module.DbgUnmatched = FALSE;
160 module->module.LineNumbers = FALSE;
161 module->module.GlobalSymbols = FALSE;
162 module->module.TypeInfo = FALSE;
163 module->module.SourceIndexed = FALSE;
164 module->module.Publics = FALSE;
166 module->type = type;
167 module->is_virtual = virtual ? TRUE : FALSE;
168 module->sortlist_valid = FALSE;
169 module->addr_sorttab = NULL;
170 /* FIXME: this seems a bit too high (on a per module basis)
171 * need some statistics about this
173 hash_table_init(&module->pool, &module->ht_symbols, 4096);
174 hash_table_init(&module->pool, &module->ht_types, 4096);
175 vector_init(&module->vtypes, sizeof(struct symt*), 32);
177 module->sources_used = 0;
178 module->sources_alloc = 0;
179 module->sources = 0;
181 return module;
184 /***********************************************************************
185 * module_find_by_name
188 static struct module* module_find_by_name(const struct process* pcs, const WCHAR* name)
190 struct module* module;
192 for (module = pcs->lmodules; module; module = module->next)
194 if (!strcmpiW(name, module->module.ModuleName)) return module;
196 SetLastError(ERROR_INVALID_NAME);
197 return NULL;
200 struct module* module_find_by_nameA(const struct process* pcs, const char* name)
202 WCHAR wname[MAX_PATH];
204 MultiByteToWideChar(CP_ACP, 0, name, -1, wname, sizeof(wname) / sizeof(WCHAR));
205 return module_find_by_name(pcs, wname);
208 /***********************************************************************
209 * module_is_already_loaded
212 struct module* module_is_already_loaded(const struct process* pcs, const WCHAR* name)
214 struct module* module;
215 const WCHAR* filename;
217 /* first compare the loaded image name... */
218 for (module = pcs->lmodules; module; module = module->next)
220 if (!strcmpiW(name, module->module.LoadedImageName))
221 return module;
223 /* then compare the standard filenames (without the path) ... */
224 filename = get_filename(name, NULL);
225 for (module = pcs->lmodules; module; module = module->next)
227 if (!strcmpiW(filename, get_filename(module->module.LoadedImageName, NULL)))
228 return module;
230 SetLastError(ERROR_INVALID_NAME);
231 return NULL;
234 /***********************************************************************
235 * module_get_container
238 static struct module* module_get_container(const struct process* pcs,
239 const struct module* inner)
241 struct module* module;
243 for (module = pcs->lmodules; module; module = module->next)
245 if (module != inner &&
246 module->module.BaseOfImage <= inner->module.BaseOfImage &&
247 module->module.BaseOfImage + module->module.ImageSize >=
248 inner->module.BaseOfImage + inner->module.ImageSize)
249 return module;
251 return NULL;
254 /***********************************************************************
255 * module_get_containee
258 struct module* module_get_containee(const struct process* pcs,
259 const struct module* outter)
261 struct module* module;
263 for (module = pcs->lmodules; module; module = module->next)
265 if (module != outter &&
266 outter->module.BaseOfImage <= module->module.BaseOfImage &&
267 outter->module.BaseOfImage + outter->module.ImageSize >=
268 module->module.BaseOfImage + module->module.ImageSize)
269 return module;
271 return NULL;
274 /******************************************************************
275 * module_get_debug
277 * get the debug information from a module:
278 * - if the module's type is deferred, then force loading of debug info (and return
279 * the module itself)
280 * - if the module has no debug info and has an ELF container, then return the ELF
281 * container (and also force the ELF container's debug info loading if deferred)
282 * - otherwise return the module itself if it has some debug info
284 BOOL module_get_debug(struct module_pair* pair)
286 IMAGEHLP_DEFERRED_SYMBOL_LOADW64 idslW64;
288 if (!pair->requested) return FALSE;
289 /* for a PE builtin, always get info from container */
290 if (!(pair->effective = module_get_container(pair->pcs, pair->requested)))
291 pair->effective = pair->requested;
292 /* if deferred, force loading */
293 if (pair->effective->module.SymType == SymDeferred)
295 BOOL ret;
297 if (pair->effective->is_virtual) ret = FALSE;
298 else switch (pair->effective->type)
300 case DMT_ELF:
301 ret = elf_load_debug_info(pair->effective, NULL);
302 break;
303 case DMT_PE:
304 idslW64.SizeOfStruct = sizeof(idslW64);
305 idslW64.BaseOfImage = pair->effective->module.BaseOfImage;
306 idslW64.CheckSum = pair->effective->module.CheckSum;
307 idslW64.TimeDateStamp = pair->effective->module.TimeDateStamp;
308 memcpy(idslW64.FileName, pair->effective->module.ImageName,
309 sizeof(pair->effective->module.ImageName));
310 idslW64.Reparse = FALSE;
311 idslW64.hFile = INVALID_HANDLE_VALUE;
313 pcs_callback(pair->pcs, CBA_DEFERRED_SYMBOL_LOAD_START, &idslW64);
314 ret = pe_load_debug_info(pair->pcs, pair->effective);
315 pcs_callback(pair->pcs,
316 ret ? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE : CBA_DEFERRED_SYMBOL_LOAD_FAILURE,
317 &idslW64);
318 break;
319 default:
320 ret = FALSE;
321 break;
323 if (!ret) pair->effective->module.SymType = SymNone;
324 assert(pair->effective->module.SymType != SymDeferred);
325 pair->effective->module.NumSyms = pair->effective->ht_symbols.num_elts;
327 return pair->effective->module.SymType != SymNone;
330 /***********************************************************************
331 * module_find_by_addr
333 * either the addr where module is loaded, or any address inside the
334 * module
336 struct module* module_find_by_addr(const struct process* pcs, unsigned long addr,
337 enum module_type type)
339 struct module* module;
341 if (type == DMT_UNKNOWN)
343 if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
344 (module = module_find_by_addr(pcs, addr, DMT_ELF)))
345 return module;
347 else
349 for (module = pcs->lmodules; module; module = module->next)
351 if (type == module->type && addr >= module->module.BaseOfImage &&
352 addr < module->module.BaseOfImage + module->module.ImageSize)
353 return module;
356 SetLastError(ERROR_INVALID_ADDRESS);
357 return module;
360 /******************************************************************
361 * module_is_elf_container_loaded
363 * checks whether the ELF container, for a (supposed) PE builtin is
364 * already loaded
366 static BOOL module_is_elf_container_loaded(const struct process* pcs,
367 const WCHAR* ImageName, DWORD base)
369 size_t len;
370 struct module* module;
371 PCWSTR filename, modname;
373 if (!base) return FALSE;
374 filename = get_filename(ImageName, NULL);
375 len = strlenW(filename);
377 for (module = pcs->lmodules; module; module = module->next)
379 if (module->type == DMT_ELF &&
380 base >= module->module.BaseOfImage &&
381 base < module->module.BaseOfImage + module->module.ImageSize)
383 modname = get_filename(module->module.LoadedImageName, NULL);
384 if (!strncmpiW(modname, filename, len) &&
385 !memcmp(modname + len, S_DotSoW, 3 * sizeof(WCHAR)))
387 return TRUE;
391 /* likely a native PE module */
392 WARN("Couldn't find container for %s\n", debugstr_w(ImageName));
393 return FALSE;
396 /******************************************************************
397 * module_get_type_by_name
399 * Guesses a filename type from its extension
401 enum module_type module_get_type_by_name(const WCHAR* name)
403 int len = strlenW(name);
405 /* Skip all version extensions (.[digits]) regex: "(\.\d+)*$" */
408 int i = len;
410 while (i && isdigit(name[i - 1])) i--;
412 if (i && name[i - 1] == '.')
413 len = i - 1;
414 else
415 break;
416 } while (len);
418 /* check for terminating .so or .so.[digit] */
419 if (len > 3 && !memcmp(name + len - 3, S_DotSoW, 3))
420 return DMT_ELF;
422 if (len > 4 && !strncmpiW(name + len - 4, S_DotPdbW, 4))
423 return DMT_PDB;
425 if (len > 4 && !strncmpiW(name + len - 4, S_DotDbgW, 4))
426 return DMT_DBG;
428 /* wine is also an ELF module */
429 if (((len > 4 && name[len - 5] == '/') || len == 4) && !strcmpiW(name + len - 4, S_WineW))
430 return DMT_ELF;
432 return DMT_PE;
435 /***********************************************************************
436 * SymLoadModule (DBGHELP.@)
438 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
439 PCSTR ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
441 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll,
442 SizeOfDll, NULL, 0);
445 /***********************************************************************
446 * SymLoadModuleEx (DBGHELP.@)
448 DWORD64 WINAPI SymLoadModuleEx(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
449 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD DllSize,
450 PMODLOAD_DATA Data, DWORD Flags)
452 PWSTR wImageName, wModuleName;
453 unsigned len;
454 DWORD64 ret;
456 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
457 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
458 wine_dbgstr_longlong(BaseOfDll), DllSize, Data, Flags);
460 if (ImageName)
462 len = MultiByteToWideChar(CP_ACP, 0, ImageName, -1, NULL, 0);
463 wImageName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
464 MultiByteToWideChar(CP_ACP, 0, ImageName, -1, wImageName, len);
466 else wImageName = NULL;
467 if (ModuleName)
469 len = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
470 wModuleName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
471 MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, wModuleName, len);
473 else wModuleName = NULL;
475 ret = SymLoadModuleExW(hProcess, hFile, wImageName, wModuleName,
476 BaseOfDll, DllSize, Data, Flags);
477 HeapFree(GetProcessHeap(), 0, wImageName);
478 HeapFree(GetProcessHeap(), 0, wModuleName);
479 return ret;
482 /***********************************************************************
483 * SymLoadModuleExW (DBGHELP.@)
485 DWORD64 WINAPI SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageName,
486 PCWSTR wModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll,
487 PMODLOAD_DATA Data, DWORD Flags)
489 struct process* pcs;
490 struct module* module = NULL;
492 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
493 hProcess, hFile, debugstr_w(wImageName), debugstr_w(wModuleName),
494 wine_dbgstr_longlong(BaseOfDll), SizeOfDll, Data, Flags);
496 if (Data)
497 FIXME("Unsupported load data parameter %p for %s\n",
498 Data, debugstr_w(wImageName));
499 if (!validate_addr64(BaseOfDll)) return FALSE;
501 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
503 if (Flags & SLMFLAG_VIRTUAL)
505 module = module_new(pcs, wImageName, module_get_type_by_name(wImageName),
506 TRUE, (DWORD)BaseOfDll, SizeOfDll, 0, 0);
507 if (!module) return FALSE;
508 if (wModuleName) module_set_module(module, wModuleName);
509 module->module.SymType = SymVirtual;
511 return TRUE;
513 if (Flags & ~(SLMFLAG_VIRTUAL))
514 FIXME("Unsupported Flags %08x for %s\n", Flags, debugstr_w(wImageName));
516 /* force transparent ELF loading / unloading */
517 elf_synchronize_module_list(pcs);
519 /* this is a Wine extension to the API just to redo the synchronisation */
520 if (!wImageName && !hFile) return 0;
522 /* check if the module is already loaded, or if it's a builtin PE module with
523 * an containing ELF module
525 if (wImageName)
527 module = module_is_already_loaded(pcs, wImageName);
528 if (!module && module_is_elf_container_loaded(pcs, wImageName, BaseOfDll))
530 /* force the loading of DLL as builtin */
531 module = pe_load_builtin_module(pcs, wImageName, BaseOfDll, SizeOfDll);
534 if (!module)
536 /* otherwise, try a regular PE module */
537 if (!(module = pe_load_native_module(pcs, wImageName, hFile, BaseOfDll, SizeOfDll)))
539 /* and finally and ELF module */
540 if (wImageName && (module_get_type_by_name(wImageName) == DMT_ELF))
541 module = elf_load_module(pcs, wImageName, BaseOfDll);
544 if (!module)
546 WARN("Couldn't locate %s\n", debugstr_w(wImageName));
547 return 0;
549 module->module.NumSyms = module->ht_symbols.num_elts;
550 /* by default module_new fills module.ModuleName from a derivation
551 * of LoadedImageName. Overwrite it, if we have better information
553 if (wModuleName)
554 module_set_module(module, wModuleName);
555 lstrcpynW(module->module.ImageName, wImageName,
556 sizeof(module->module.ImageName) / sizeof(WCHAR));
558 return module->module.BaseOfImage;
561 /***********************************************************************
562 * SymLoadModule64 (DBGHELP.@)
564 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
565 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
567 if (!validate_addr64(BaseOfDll)) return FALSE;
568 return SymLoadModule(hProcess, hFile, ImageName, ModuleName, (DWORD)BaseOfDll, SizeOfDll);
571 /******************************************************************
572 * module_remove
575 BOOL module_remove(struct process* pcs, struct module* module)
577 struct module** p;
579 TRACE("%s (%p)\n", debugstr_w(module->module.ModuleName), module);
580 hash_table_destroy(&module->ht_symbols);
581 hash_table_destroy(&module->ht_types);
582 HeapFree(GetProcessHeap(), 0, module->sources);
583 HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
584 HeapFree(GetProcessHeap(), 0, module->dwarf2_info);
585 pool_destroy(&module->pool);
586 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
587 * so do we
589 for (p = &pcs->lmodules; *p; p = &(*p)->next)
591 if (*p == module)
593 *p = module->next;
594 HeapFree(GetProcessHeap(), 0, module);
595 return TRUE;
598 FIXME("This shouldn't happen\n");
599 return FALSE;
602 /******************************************************************
603 * SymUnloadModule (DBGHELP.@)
606 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
608 struct process* pcs;
609 struct module* module;
611 pcs = process_find_by_handle(hProcess);
612 if (!pcs) return FALSE;
613 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
614 if (!module) return FALSE;
615 return module_remove(pcs, module);
618 /******************************************************************
619 * SymUnloadModule64 (DBGHELP.@)
622 BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
624 struct process* pcs;
625 struct module* module;
627 pcs = process_find_by_handle(hProcess);
628 if (!pcs) return FALSE;
629 if (!validate_addr64(BaseOfDll)) return FALSE;
630 module = module_find_by_addr(pcs, (DWORD)BaseOfDll, DMT_UNKNOWN);
631 if (!module) return FALSE;
632 return module_remove(pcs, module);
635 /******************************************************************
636 * SymEnumerateModules (DBGHELP.@)
639 struct enum_modW64_32
641 PSYM_ENUMMODULES_CALLBACK cb;
642 PVOID user;
643 char module[MAX_PATH];
646 static BOOL CALLBACK enum_modW64_32(PCWSTR name, DWORD64 base, PVOID user)
648 struct enum_modW64_32* x = user;
650 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
651 return x->cb(x->module, (DWORD)base, x->user);
654 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
655 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
656 PVOID UserContext)
658 struct enum_modW64_32 x;
660 x.cb = EnumModulesCallback;
661 x.user = UserContext;
663 return SymEnumerateModulesW64(hProcess, enum_modW64_32, &x);
666 /******************************************************************
667 * SymEnumerateModules64 (DBGHELP.@)
670 struct enum_modW64_64
672 PSYM_ENUMMODULES_CALLBACK64 cb;
673 PVOID user;
674 char module[MAX_PATH];
677 static BOOL CALLBACK enum_modW64_64(PCWSTR name, DWORD64 base, PVOID user)
679 struct enum_modW64_64* x = user;
681 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
682 return x->cb(x->module, base, x->user);
685 BOOL WINAPI SymEnumerateModules64(HANDLE hProcess,
686 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,
687 PVOID UserContext)
689 struct enum_modW64_64 x;
691 x.cb = EnumModulesCallback;
692 x.user = UserContext;
694 return SymEnumerateModulesW64(hProcess, enum_modW64_64, &x);
697 /******************************************************************
698 * SymEnumerateModulesW64 (DBGHELP.@)
701 BOOL WINAPI SymEnumerateModulesW64(HANDLE hProcess,
702 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback,
703 PVOID UserContext)
705 struct process* pcs = process_find_by_handle(hProcess);
706 struct module* module;
708 if (!pcs) return FALSE;
710 for (module = pcs->lmodules; module; module = module->next)
712 if (!(dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES) && module->type == DMT_ELF)
713 continue;
714 if (!EnumModulesCallback(module->module.ModuleName,
715 module->module.BaseOfImage, UserContext))
716 break;
718 return TRUE;
721 /******************************************************************
722 * EnumerateLoadedModules64 (DBGHELP.@)
725 struct enum_load_modW64_64
727 PENUMLOADED_MODULES_CALLBACK64 cb;
728 PVOID user;
729 char module[MAX_PATH];
732 static BOOL CALLBACK enum_load_modW64_64(PCWSTR name, DWORD64 base, ULONG size,
733 PVOID user)
735 struct enum_load_modW64_64* x = user;
737 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
738 return x->cb(x->module, base, size, x->user);
741 BOOL WINAPI EnumerateLoadedModules64(HANDLE hProcess,
742 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
743 PVOID UserContext)
745 struct enum_load_modW64_64 x;
747 x.cb = EnumLoadedModulesCallback;
748 x.user = UserContext;
750 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_64, &x);
753 /******************************************************************
754 * EnumerateLoadedModules (DBGHELP.@)
757 struct enum_load_modW64_32
759 PENUMLOADED_MODULES_CALLBACK cb;
760 PVOID user;
761 char module[MAX_PATH];
764 static BOOL CALLBACK enum_load_modW64_32(PCWSTR name, DWORD64 base, ULONG size,
765 PVOID user)
767 struct enum_load_modW64_32* x = user;
768 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
769 return x->cb(x->module, (DWORD)base, size, x->user);
772 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
773 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
774 PVOID UserContext)
776 struct enum_load_modW64_32 x;
778 x.cb = EnumLoadedModulesCallback;
779 x.user = UserContext;
781 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_32, &x);
784 /******************************************************************
785 * EnumerateLoadedModulesW64 (DBGHELP.@)
788 BOOL WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
789 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback,
790 PVOID UserContext)
792 HMODULE* hMods;
793 WCHAR baseW[256], modW[256];
794 DWORD i, sz;
795 MODULEINFO mi;
797 hMods = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods[0]));
798 if (!hMods) return FALSE;
800 if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
802 /* hProcess should also be a valid process handle !! */
803 FIXME("If this happens, bump the number in mod\n");
804 HeapFree(GetProcessHeap(), 0, hMods);
805 return FALSE;
807 sz /= sizeof(HMODULE);
808 for (i = 0; i < sz; i++)
810 if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
811 !GetModuleBaseNameW(hProcess, hMods[i], baseW, sizeof(baseW) / sizeof(WCHAR)))
812 continue;
813 module_fill_module(baseW, modW, sizeof(modW) / sizeof(CHAR));
814 EnumLoadedModulesCallback(modW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage,
815 UserContext);
817 HeapFree(GetProcessHeap(), 0, hMods);
819 return sz != 0 && i == sz;
822 /******************************************************************
823 * SymGetModuleInfo (DBGHELP.@)
826 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
827 PIMAGEHLP_MODULE ModuleInfo)
829 IMAGEHLP_MODULE mi;
830 IMAGEHLP_MODULEW64 miw64;
832 if (sizeof(mi) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
834 miw64.SizeOfStruct = sizeof(miw64);
835 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
837 mi.SizeOfStruct = miw64.SizeOfStruct;
838 mi.BaseOfImage = miw64.BaseOfImage;
839 mi.ImageSize = miw64.ImageSize;
840 mi.TimeDateStamp = miw64.TimeDateStamp;
841 mi.CheckSum = miw64.CheckSum;
842 mi.NumSyms = miw64.NumSyms;
843 mi.SymType = miw64.SymType;
844 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
845 mi.ModuleName, sizeof(mi.ModuleName), NULL, NULL);
846 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
847 mi.ImageName, sizeof(mi.ImageName), NULL, NULL);
848 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
849 mi.LoadedImageName, sizeof(mi.LoadedImageName), NULL, NULL);
851 memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
853 return TRUE;
856 /******************************************************************
857 * SymGetModuleInfoW (DBGHELP.@)
860 BOOL WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
861 PIMAGEHLP_MODULEW ModuleInfo)
863 IMAGEHLP_MODULEW64 miw64;
864 IMAGEHLP_MODULEW miw;
866 if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
868 miw64.SizeOfStruct = sizeof(miw64);
869 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
871 miw.SizeOfStruct = miw64.SizeOfStruct;
872 miw.BaseOfImage = miw64.BaseOfImage;
873 miw.ImageSize = miw64.ImageSize;
874 miw.TimeDateStamp = miw64.TimeDateStamp;
875 miw.CheckSum = miw64.CheckSum;
876 miw.NumSyms = miw64.NumSyms;
877 miw.SymType = miw64.SymType;
878 strcpyW(miw.ModuleName, miw64.ModuleName);
879 strcpyW(miw.ImageName, miw64.ImageName);
880 strcpyW(miw.LoadedImageName, miw64.LoadedImageName);
881 memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
883 return TRUE;
886 /******************************************************************
887 * SymGetModuleInfo64 (DBGHELP.@)
890 BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
891 PIMAGEHLP_MODULE64 ModuleInfo)
893 IMAGEHLP_MODULE64 mi64;
894 IMAGEHLP_MODULEW64 miw64;
896 if (sizeof(mi64) < ModuleInfo->SizeOfStruct)
898 SetLastError(ERROR_MOD_NOT_FOUND); /* NOTE: native returns this error */
899 WARN("Wrong size %u\n", ModuleInfo->SizeOfStruct);
900 return FALSE;
903 miw64.SizeOfStruct = sizeof(miw64);
904 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
906 mi64.SizeOfStruct = miw64.SizeOfStruct;
907 mi64.BaseOfImage = miw64.BaseOfImage;
908 mi64.ImageSize = miw64.ImageSize;
909 mi64.TimeDateStamp = miw64.TimeDateStamp;
910 mi64.CheckSum = miw64.CheckSum;
911 mi64.NumSyms = miw64.NumSyms;
912 mi64.SymType = miw64.SymType;
913 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
914 mi64.ModuleName, sizeof(mi64.ModuleName), NULL, NULL);
915 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
916 mi64.ImageName, sizeof(mi64.ImageName), NULL, NULL);
917 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
918 mi64.LoadedImageName, sizeof(mi64.LoadedImageName), NULL, NULL);
919 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedPdbName, -1,
920 mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName), NULL, NULL);
922 mi64.CVSig = miw64.CVSig;
923 WideCharToMultiByte(CP_ACP, 0, miw64.CVData, -1,
924 mi64.CVData, sizeof(mi64.CVData), NULL, NULL);
925 mi64.PdbSig = miw64.PdbSig;
926 mi64.PdbSig70 = miw64.PdbSig70;
927 mi64.PdbAge = miw64.PdbAge;
928 mi64.PdbUnmatched = miw64.PdbUnmatched;
929 mi64.DbgUnmatched = miw64.DbgUnmatched;
930 mi64.LineNumbers = miw64.LineNumbers;
931 mi64.GlobalSymbols = miw64.GlobalSymbols;
932 mi64.TypeInfo = miw64.TypeInfo;
933 mi64.SourceIndexed = miw64.SourceIndexed;
934 mi64.Publics = miw64.Publics;
936 memcpy(ModuleInfo, &mi64, ModuleInfo->SizeOfStruct);
938 return TRUE;
941 /******************************************************************
942 * SymGetModuleInfoW64 (DBGHELP.@)
945 BOOL WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr,
946 PIMAGEHLP_MODULEW64 ModuleInfo)
948 struct process* pcs = process_find_by_handle(hProcess);
949 struct module* module;
950 IMAGEHLP_MODULEW64 miw64;
952 TRACE("%p %s %p\n", hProcess, wine_dbgstr_longlong(dwAddr), ModuleInfo);
954 if (!pcs) return FALSE;
955 if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
956 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
957 if (!module) return FALSE;
959 miw64 = module->module;
961 /* update debug information from container if any */
962 if (module->module.SymType == SymNone)
964 module = module_get_container(pcs, module);
965 if (module && module->module.SymType != SymNone)
967 miw64.SymType = module->module.SymType;
968 miw64.NumSyms = module->module.NumSyms;
971 memcpy(ModuleInfo, &miw64, ModuleInfo->SizeOfStruct);
972 return TRUE;
975 /***********************************************************************
976 * SymGetModuleBase (DBGHELP.@)
978 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
980 struct process* pcs = process_find_by_handle(hProcess);
981 struct module* module;
983 if (!pcs) return 0;
984 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
985 if (!module) return 0;
986 return module->module.BaseOfImage;
989 /***********************************************************************
990 * SymGetModuleBase64 (DBGHELP.@)
992 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
994 if (!validate_addr64(dwAddr)) return 0;
995 return SymGetModuleBase(hProcess, (DWORD)dwAddr);
998 /******************************************************************
999 * module_reset_debug_info
1000 * Removes any debug information linked to a given module.
1002 void module_reset_debug_info(struct module* module)
1004 module->sortlist_valid = TRUE;
1005 module->addr_sorttab = NULL;
1006 hash_table_destroy(&module->ht_symbols);
1007 module->ht_symbols.num_buckets = 0;
1008 module->ht_symbols.buckets = NULL;
1009 hash_table_destroy(&module->ht_types);
1010 module->ht_types.num_buckets = 0;
1011 module->ht_types.buckets = NULL;
1012 module->vtypes.num_elts = 0;
1013 hash_table_destroy(&module->ht_symbols);
1014 module->sources_used = module->sources_alloc = 0;
1015 module->sources = NULL;