push cc8bc80451cc24f4d7cf75168b569f0ebfe19547
[wine/hacks.git] / dlls / dbghelp / module.c
blob1a3d94e316d338ca70b6d1ed15247bde6cb6ef79
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_DotDylibW[] = {'.','d','y','l','i','b','\0'};
39 static const WCHAR S_DotPdbW[] = {'.','p','d','b','\0'};
40 static const WCHAR S_DotDbgW[] = {'.','d','b','g','\0'};
41 const WCHAR S_WineW[] = {'w','i','n','e',0};
42 const WCHAR S_SlashW[] = {'/','\0'};
44 static const WCHAR S_AcmW[] = {'.','a','c','m','\0'};
45 static const WCHAR S_DllW[] = {'.','d','l','l','\0'};
46 static const WCHAR S_DrvW[] = {'.','d','r','v','\0'};
47 static const WCHAR S_ExeW[] = {'.','e','x','e','\0'};
48 static const WCHAR S_OcxW[] = {'.','o','c','x','\0'};
49 static const WCHAR S_VxdW[] = {'.','v','x','d','\0'};
50 static const WCHAR * const ext[] = {S_AcmW, S_DllW, S_DrvW, S_ExeW, S_OcxW, S_VxdW, NULL};
52 static int match_ext(const WCHAR* ptr, size_t len)
54 const WCHAR* const *e;
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 > 4 && !strcmpiW(out + len - 4, S_WineW))
91 lstrcpynW(out, S_WineLoaderW, size);
92 else
94 if (len > 3 && !strcmpiW(&out[len - 3], S_DotSoW) &&
95 (l = match_ext(out, len - 3)))
96 strcpyW(&out[len - l - 3], S_ElfW);
98 while ((*out = tolowerW(*out))) out++;
101 void module_set_module(struct module* module, const WCHAR* name)
103 module_fill_module(name, module->module.ModuleName, sizeof(module->module.ModuleName));
104 WideCharToMultiByte(CP_ACP, 0, module->module.ModuleName, -1,
105 module->module_name, sizeof(module->module_name),
106 NULL, NULL);
109 static const char* get_module_type(enum module_type type, BOOL virtual)
111 switch (type)
113 case DMT_ELF: return virtual ? "Virtual ELF" : "ELF";
114 case DMT_PE: return virtual ? "Virtual PE" : "PE";
115 case DMT_MACHO: return virtual ? "Virtual Mach-O" : "Mach-O";
116 default: return "---";
120 /***********************************************************************
121 * Creates and links a new module to a process
123 struct module* module_new(struct process* pcs, const WCHAR* name,
124 enum module_type type, BOOL virtual,
125 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 || type == DMT_MACHO);
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 static 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 static 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(pair->effective->module.ImageName));
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 case DMT_MACHO:
322 ret = macho_load_debug_info(pair->effective, NULL);
323 break;
324 default:
325 ret = FALSE;
326 break;
328 if (!ret) pair->effective->module.SymType = SymNone;
329 assert(pair->effective->module.SymType != SymDeferred);
330 pair->effective->module.NumSyms = pair->effective->ht_symbols.num_elts;
332 return pair->effective->module.SymType != SymNone;
335 /***********************************************************************
336 * module_find_by_addr
338 * either the addr where module is loaded, or any address inside the
339 * module
341 struct module* module_find_by_addr(const struct process* pcs, unsigned long addr,
342 enum module_type type)
344 struct module* module;
346 if (type == DMT_UNKNOWN)
348 if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
349 (module = module_find_by_addr(pcs, addr, DMT_ELF)) ||
350 (module = module_find_by_addr(pcs, addr, DMT_MACHO)))
351 return module;
353 else
355 for (module = pcs->lmodules; module; module = module->next)
357 if (type == module->type && addr >= module->module.BaseOfImage &&
358 addr < module->module.BaseOfImage + module->module.ImageSize)
359 return module;
362 SetLastError(ERROR_INVALID_ADDRESS);
363 return module;
366 /******************************************************************
367 * module_is_container_loaded
369 * checks whether the native container, for a (supposed) PE builtin is
370 * already loaded
372 static BOOL module_is_container_loaded(const struct process* pcs,
373 const WCHAR* ImageName, DWORD base)
375 size_t len;
376 struct module* module;
377 PCWSTR filename, modname;
379 if (!base) return FALSE;
380 filename = get_filename(ImageName, NULL);
381 len = strlenW(filename);
383 for (module = pcs->lmodules; module; module = module->next)
385 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
386 base >= module->module.BaseOfImage &&
387 base < module->module.BaseOfImage + module->module.ImageSize)
389 modname = get_filename(module->module.LoadedImageName, NULL);
390 if (!strncmpiW(modname, filename, len) &&
391 !memcmp(modname + len, S_DotSoW, 3 * sizeof(WCHAR)))
393 return TRUE;
397 /* likely a native PE module */
398 WARN("Couldn't find container for %s\n", debugstr_w(ImageName));
399 return FALSE;
402 /******************************************************************
403 * module_get_type_by_name
405 * Guesses a filename type from its extension
407 enum module_type module_get_type_by_name(const WCHAR* name)
409 int len = strlenW(name);
411 /* Skip all version extensions (.[digits]) regex: "(\.\d+)*$" */
414 int i = len;
416 while (i && isdigit(name[i - 1])) i--;
418 if (i && name[i - 1] == '.')
419 len = i - 1;
420 else
421 break;
422 } while (len);
424 /* check for terminating .so or .so.[digit] */
425 /* FIXME: Can't rely solely on extension; have to check magic or
426 * stop using .so on Mac OS X. For now, base on platform. */
427 if (len > 3 && !memcmp(name + len - 3, S_DotSoW, 3))
428 #ifdef __APPLE__
429 return DMT_MACHO;
430 #else
431 return DMT_ELF;
432 #endif
434 if (len > 6 && !strncmpiW(name + len - 6, S_DotDylibW, 6))
435 return DMT_MACHO;
437 if (len > 4 && !strncmpiW(name + len - 4, S_DotPdbW, 4))
438 return DMT_PDB;
440 if (len > 4 && !strncmpiW(name + len - 4, S_DotDbgW, 4))
441 return DMT_DBG;
443 /* wine is also a native module (Mach-O on Mac OS X, ELF elsewhere) */
444 if (((len > 4 && name[len - 5] == '/') || len == 4) && !strcmpiW(name + len - 4, S_WineW))
446 #ifdef __APPLE__
447 return DMT_MACHO;
448 #else
449 return DMT_ELF;
450 #endif
452 return DMT_PE;
455 /***********************************************************************
456 * SymLoadModule (DBGHELP.@)
458 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
459 PCSTR ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
461 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll,
462 SizeOfDll, NULL, 0);
465 /***********************************************************************
466 * SymLoadModuleEx (DBGHELP.@)
468 DWORD64 WINAPI SymLoadModuleEx(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
469 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD DllSize,
470 PMODLOAD_DATA Data, DWORD Flags)
472 PWSTR wImageName, wModuleName;
473 unsigned len;
474 DWORD64 ret;
476 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
477 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
478 wine_dbgstr_longlong(BaseOfDll), DllSize, Data, Flags);
480 if (ImageName)
482 len = MultiByteToWideChar(CP_ACP, 0, ImageName, -1, NULL, 0);
483 wImageName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
484 MultiByteToWideChar(CP_ACP, 0, ImageName, -1, wImageName, len);
486 else wImageName = NULL;
487 if (ModuleName)
489 len = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
490 wModuleName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
491 MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, wModuleName, len);
493 else wModuleName = NULL;
495 ret = SymLoadModuleExW(hProcess, hFile, wImageName, wModuleName,
496 BaseOfDll, DllSize, Data, Flags);
497 HeapFree(GetProcessHeap(), 0, wImageName);
498 HeapFree(GetProcessHeap(), 0, wModuleName);
499 return ret;
502 /***********************************************************************
503 * SymLoadModuleExW (DBGHELP.@)
505 DWORD64 WINAPI SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageName,
506 PCWSTR wModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll,
507 PMODLOAD_DATA Data, DWORD Flags)
509 struct process* pcs;
510 struct module* module = NULL;
512 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
513 hProcess, hFile, debugstr_w(wImageName), debugstr_w(wModuleName),
514 wine_dbgstr_longlong(BaseOfDll), SizeOfDll, Data, Flags);
516 if (Data)
517 FIXME("Unsupported load data parameter %p for %s\n",
518 Data, debugstr_w(wImageName));
519 if (!validate_addr64(BaseOfDll)) return FALSE;
521 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
523 if (Flags & SLMFLAG_VIRTUAL)
525 module = module_new(pcs, wImageName, module_get_type_by_name(wImageName),
526 TRUE, (DWORD)BaseOfDll, SizeOfDll, 0, 0);
527 if (!module) return FALSE;
528 if (wModuleName) module_set_module(module, wModuleName);
529 module->module.SymType = SymVirtual;
531 return TRUE;
533 if (Flags & ~(SLMFLAG_VIRTUAL))
534 FIXME("Unsupported Flags %08x for %s\n", Flags, debugstr_w(wImageName));
536 /* force transparent ELF and Mach-O loading / unloading */
537 elf_synchronize_module_list(pcs);
538 macho_synchronize_module_list(pcs);
540 /* this is a Wine extension to the API just to redo the synchronisation */
541 if (!wImageName && !hFile) return 0;
543 /* check if the module is already loaded, or if it's a builtin PE module with
544 * an containing ELF module
546 if (wImageName)
548 module = module_is_already_loaded(pcs, wImageName);
549 if (!module && module_is_container_loaded(pcs, wImageName, BaseOfDll))
551 /* force the loading of DLL as builtin */
552 module = pe_load_builtin_module(pcs, wImageName, BaseOfDll, SizeOfDll);
555 if (!module)
557 /* otherwise, try a regular PE module */
558 if (!(module = pe_load_native_module(pcs, wImageName, hFile, BaseOfDll, SizeOfDll)) &&
559 wImageName)
561 /* and finally an ELF or Mach-O module */
562 switch (module_get_type_by_name(wImageName))
564 case DMT_ELF:
565 module = elf_load_module(pcs, wImageName, BaseOfDll);
566 break;
567 case DMT_MACHO:
568 module = macho_load_module(pcs, wImageName, BaseOfDll);
569 break;
570 default:
571 /* Ignored */
572 break;
576 if (!module)
578 WARN("Couldn't locate %s\n", debugstr_w(wImageName));
579 return 0;
581 module->module.NumSyms = module->ht_symbols.num_elts;
582 /* by default module_new fills module.ModuleName from a derivation
583 * of LoadedImageName. Overwrite it, if we have better information
585 if (wModuleName)
586 module_set_module(module, wModuleName);
587 lstrcpynW(module->module.ImageName, wImageName,
588 sizeof(module->module.ImageName) / sizeof(WCHAR));
590 return module->module.BaseOfImage;
593 /***********************************************************************
594 * SymLoadModule64 (DBGHELP.@)
596 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
597 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
599 if (!validate_addr64(BaseOfDll)) return FALSE;
600 return SymLoadModule(hProcess, hFile, ImageName, ModuleName, (DWORD)BaseOfDll, SizeOfDll);
603 /******************************************************************
604 * module_remove
607 BOOL module_remove(struct process* pcs, struct module* module)
609 struct module** p;
611 TRACE("%s (%p)\n", debugstr_w(module->module.ModuleName), module);
612 hash_table_destroy(&module->ht_symbols);
613 hash_table_destroy(&module->ht_types);
614 HeapFree(GetProcessHeap(), 0, module->sources);
615 HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
616 HeapFree(GetProcessHeap(), 0, module->dwarf2_info);
617 pool_destroy(&module->pool);
618 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
619 * so do we
621 for (p = &pcs->lmodules; *p; p = &(*p)->next)
623 if (*p == module)
625 *p = module->next;
626 HeapFree(GetProcessHeap(), 0, module);
627 return TRUE;
630 FIXME("This shouldn't happen\n");
631 return FALSE;
634 /******************************************************************
635 * SymUnloadModule (DBGHELP.@)
638 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
640 struct process* pcs;
641 struct module* module;
643 pcs = process_find_by_handle(hProcess);
644 if (!pcs) return FALSE;
645 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
646 if (!module) return FALSE;
647 return module_remove(pcs, module);
650 /******************************************************************
651 * SymUnloadModule64 (DBGHELP.@)
654 BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
656 struct process* pcs;
657 struct module* module;
659 pcs = process_find_by_handle(hProcess);
660 if (!pcs) return FALSE;
661 if (!validate_addr64(BaseOfDll)) return FALSE;
662 module = module_find_by_addr(pcs, (DWORD)BaseOfDll, DMT_UNKNOWN);
663 if (!module) return FALSE;
664 return module_remove(pcs, module);
667 /******************************************************************
668 * SymEnumerateModules (DBGHELP.@)
671 struct enum_modW64_32
673 PSYM_ENUMMODULES_CALLBACK cb;
674 PVOID user;
675 char module[MAX_PATH];
678 static BOOL CALLBACK enum_modW64_32(PCWSTR name, DWORD64 base, PVOID user)
680 struct enum_modW64_32* x = user;
682 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
683 return x->cb(x->module, (DWORD)base, x->user);
686 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
687 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
688 PVOID UserContext)
690 struct enum_modW64_32 x;
692 x.cb = EnumModulesCallback;
693 x.user = UserContext;
695 return SymEnumerateModulesW64(hProcess, enum_modW64_32, &x);
698 /******************************************************************
699 * SymEnumerateModules64 (DBGHELP.@)
702 struct enum_modW64_64
704 PSYM_ENUMMODULES_CALLBACK64 cb;
705 PVOID user;
706 char module[MAX_PATH];
709 static BOOL CALLBACK enum_modW64_64(PCWSTR name, DWORD64 base, PVOID user)
711 struct enum_modW64_64* x = user;
713 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
714 return x->cb(x->module, base, x->user);
717 BOOL WINAPI SymEnumerateModules64(HANDLE hProcess,
718 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,
719 PVOID UserContext)
721 struct enum_modW64_64 x;
723 x.cb = EnumModulesCallback;
724 x.user = UserContext;
726 return SymEnumerateModulesW64(hProcess, enum_modW64_64, &x);
729 /******************************************************************
730 * SymEnumerateModulesW64 (DBGHELP.@)
733 BOOL WINAPI SymEnumerateModulesW64(HANDLE hProcess,
734 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback,
735 PVOID UserContext)
737 struct process* pcs = process_find_by_handle(hProcess);
738 struct module* module;
740 if (!pcs) return FALSE;
742 for (module = pcs->lmodules; module; module = module->next)
744 if (!(dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES) &&
745 (module->type == DMT_ELF || module->type == DMT_MACHO))
746 continue;
747 if (!EnumModulesCallback(module->module.ModuleName,
748 module->module.BaseOfImage, UserContext))
749 break;
751 return TRUE;
754 /******************************************************************
755 * EnumerateLoadedModules64 (DBGHELP.@)
758 struct enum_load_modW64_64
760 PENUMLOADED_MODULES_CALLBACK64 cb;
761 PVOID user;
762 char module[MAX_PATH];
765 static BOOL CALLBACK enum_load_modW64_64(PCWSTR name, DWORD64 base, ULONG size,
766 PVOID user)
768 struct enum_load_modW64_64* x = user;
770 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
771 return x->cb(x->module, base, size, x->user);
774 BOOL WINAPI EnumerateLoadedModules64(HANDLE hProcess,
775 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
776 PVOID UserContext)
778 struct enum_load_modW64_64 x;
780 x.cb = EnumLoadedModulesCallback;
781 x.user = UserContext;
783 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_64, &x);
786 /******************************************************************
787 * EnumerateLoadedModules (DBGHELP.@)
790 struct enum_load_modW64_32
792 PENUMLOADED_MODULES_CALLBACK cb;
793 PVOID user;
794 char module[MAX_PATH];
797 static BOOL CALLBACK enum_load_modW64_32(PCWSTR name, DWORD64 base, ULONG size,
798 PVOID user)
800 struct enum_load_modW64_32* x = user;
801 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
802 return x->cb(x->module, (DWORD)base, size, x->user);
805 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
806 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
807 PVOID UserContext)
809 struct enum_load_modW64_32 x;
811 x.cb = EnumLoadedModulesCallback;
812 x.user = UserContext;
814 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_32, &x);
817 /******************************************************************
818 * EnumerateLoadedModulesW64 (DBGHELP.@)
821 BOOL WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
822 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback,
823 PVOID UserContext)
825 HMODULE* hMods;
826 WCHAR baseW[256], modW[256];
827 DWORD i, sz;
828 MODULEINFO mi;
830 hMods = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods[0]));
831 if (!hMods) return FALSE;
833 if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
835 /* hProcess should also be a valid process handle !! */
836 FIXME("If this happens, bump the number in mod\n");
837 HeapFree(GetProcessHeap(), 0, hMods);
838 return FALSE;
840 sz /= sizeof(HMODULE);
841 for (i = 0; i < sz; i++)
843 if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
844 !GetModuleBaseNameW(hProcess, hMods[i], baseW, sizeof(baseW) / sizeof(WCHAR)))
845 continue;
846 module_fill_module(baseW, modW, sizeof(modW) / sizeof(CHAR));
847 EnumLoadedModulesCallback(modW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage,
848 UserContext);
850 HeapFree(GetProcessHeap(), 0, hMods);
852 return sz != 0 && i == sz;
855 /******************************************************************
856 * SymGetModuleInfo (DBGHELP.@)
859 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
860 PIMAGEHLP_MODULE ModuleInfo)
862 IMAGEHLP_MODULE mi;
863 IMAGEHLP_MODULEW64 miw64;
865 if (sizeof(mi) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
867 miw64.SizeOfStruct = sizeof(miw64);
868 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
870 mi.SizeOfStruct = miw64.SizeOfStruct;
871 mi.BaseOfImage = miw64.BaseOfImage;
872 mi.ImageSize = miw64.ImageSize;
873 mi.TimeDateStamp = miw64.TimeDateStamp;
874 mi.CheckSum = miw64.CheckSum;
875 mi.NumSyms = miw64.NumSyms;
876 mi.SymType = miw64.SymType;
877 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
878 mi.ModuleName, sizeof(mi.ModuleName), NULL, NULL);
879 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
880 mi.ImageName, sizeof(mi.ImageName), NULL, NULL);
881 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
882 mi.LoadedImageName, sizeof(mi.LoadedImageName), NULL, NULL);
884 memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
886 return TRUE;
889 /******************************************************************
890 * SymGetModuleInfoW (DBGHELP.@)
893 BOOL WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
894 PIMAGEHLP_MODULEW ModuleInfo)
896 IMAGEHLP_MODULEW64 miw64;
897 IMAGEHLP_MODULEW miw;
899 if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
901 miw64.SizeOfStruct = sizeof(miw64);
902 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
904 miw.SizeOfStruct = miw64.SizeOfStruct;
905 miw.BaseOfImage = miw64.BaseOfImage;
906 miw.ImageSize = miw64.ImageSize;
907 miw.TimeDateStamp = miw64.TimeDateStamp;
908 miw.CheckSum = miw64.CheckSum;
909 miw.NumSyms = miw64.NumSyms;
910 miw.SymType = miw64.SymType;
911 strcpyW(miw.ModuleName, miw64.ModuleName);
912 strcpyW(miw.ImageName, miw64.ImageName);
913 strcpyW(miw.LoadedImageName, miw64.LoadedImageName);
914 memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
916 return TRUE;
919 /******************************************************************
920 * SymGetModuleInfo64 (DBGHELP.@)
923 BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
924 PIMAGEHLP_MODULE64 ModuleInfo)
926 IMAGEHLP_MODULE64 mi64;
927 IMAGEHLP_MODULEW64 miw64;
929 if (sizeof(mi64) < ModuleInfo->SizeOfStruct)
931 SetLastError(ERROR_MOD_NOT_FOUND); /* NOTE: native returns this error */
932 WARN("Wrong size %u\n", ModuleInfo->SizeOfStruct);
933 return FALSE;
936 miw64.SizeOfStruct = sizeof(miw64);
937 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
939 mi64.SizeOfStruct = miw64.SizeOfStruct;
940 mi64.BaseOfImage = miw64.BaseOfImage;
941 mi64.ImageSize = miw64.ImageSize;
942 mi64.TimeDateStamp = miw64.TimeDateStamp;
943 mi64.CheckSum = miw64.CheckSum;
944 mi64.NumSyms = miw64.NumSyms;
945 mi64.SymType = miw64.SymType;
946 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
947 mi64.ModuleName, sizeof(mi64.ModuleName), NULL, NULL);
948 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
949 mi64.ImageName, sizeof(mi64.ImageName), NULL, NULL);
950 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
951 mi64.LoadedImageName, sizeof(mi64.LoadedImageName), NULL, NULL);
952 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedPdbName, -1,
953 mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName), NULL, NULL);
955 mi64.CVSig = miw64.CVSig;
956 WideCharToMultiByte(CP_ACP, 0, miw64.CVData, -1,
957 mi64.CVData, sizeof(mi64.CVData), NULL, NULL);
958 mi64.PdbSig = miw64.PdbSig;
959 mi64.PdbSig70 = miw64.PdbSig70;
960 mi64.PdbAge = miw64.PdbAge;
961 mi64.PdbUnmatched = miw64.PdbUnmatched;
962 mi64.DbgUnmatched = miw64.DbgUnmatched;
963 mi64.LineNumbers = miw64.LineNumbers;
964 mi64.GlobalSymbols = miw64.GlobalSymbols;
965 mi64.TypeInfo = miw64.TypeInfo;
966 mi64.SourceIndexed = miw64.SourceIndexed;
967 mi64.Publics = miw64.Publics;
969 memcpy(ModuleInfo, &mi64, ModuleInfo->SizeOfStruct);
971 return TRUE;
974 /******************************************************************
975 * SymGetModuleInfoW64 (DBGHELP.@)
978 BOOL WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr,
979 PIMAGEHLP_MODULEW64 ModuleInfo)
981 struct process* pcs = process_find_by_handle(hProcess);
982 struct module* module;
983 IMAGEHLP_MODULEW64 miw64;
985 TRACE("%p %s %p\n", hProcess, wine_dbgstr_longlong(dwAddr), ModuleInfo);
987 if (!pcs) return FALSE;
988 if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
989 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
990 if (!module) return FALSE;
992 miw64 = module->module;
994 /* update debug information from container if any */
995 if (module->module.SymType == SymNone)
997 module = module_get_container(pcs, module);
998 if (module && module->module.SymType != SymNone)
1000 miw64.SymType = module->module.SymType;
1001 miw64.NumSyms = module->module.NumSyms;
1004 memcpy(ModuleInfo, &miw64, ModuleInfo->SizeOfStruct);
1005 return TRUE;
1008 /***********************************************************************
1009 * SymGetModuleBase (DBGHELP.@)
1011 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
1013 struct process* pcs = process_find_by_handle(hProcess);
1014 struct module* module;
1016 if (!pcs) return 0;
1017 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1018 if (!module) return 0;
1019 return module->module.BaseOfImage;
1022 /***********************************************************************
1023 * SymGetModuleBase64 (DBGHELP.@)
1025 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
1027 if (!validate_addr64(dwAddr)) return 0;
1028 return SymGetModuleBase(hProcess, (DWORD)dwAddr);
1031 /******************************************************************
1032 * module_reset_debug_info
1033 * Removes any debug information linked to a given module.
1035 void module_reset_debug_info(struct module* module)
1037 module->sortlist_valid = TRUE;
1038 module->addr_sorttab = NULL;
1039 hash_table_destroy(&module->ht_symbols);
1040 module->ht_symbols.num_buckets = 0;
1041 module->ht_symbols.buckets = NULL;
1042 hash_table_destroy(&module->ht_types);
1043 module->ht_types.num_buckets = 0;
1044 module->ht_types.buckets = NULL;
1045 module->vtypes.num_elts = 0;
1046 hash_table_destroy(&module->ht_symbols);
1047 module->sources_used = module->sources_alloc = 0;
1048 module->sources = NULL;