dbghelp: Add support for debugging Mach-O modules.
[wine/wine-gecko.git] / dlls / dbghelp / module.c
blob3e32a82ca693bf03ece3569d93b9abd3e0e53812
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 case DMT_MACHO: return virtual ? "Virtual Mach-O" : "Mach-O";
115 default: return "---";
119 /***********************************************************************
120 * Creates and links a new module to a process
122 struct module* module_new(struct process* pcs, const WCHAR* name,
123 enum module_type type, BOOL virtual,
124 unsigned long mod_addr, unsigned long size,
125 unsigned long stamp, unsigned long checksum)
127 struct module* module;
129 assert(type == DMT_ELF || type == DMT_PE || type == DMT_MACHO);
130 if (!(module = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*module))))
131 return NULL;
133 module->next = pcs->lmodules;
134 pcs->lmodules = module;
136 TRACE("=> %s %08lx-%08lx %s\n",
137 get_module_type(type, virtual), mod_addr, mod_addr + size,
138 debugstr_w(name));
140 pool_init(&module->pool, 65536);
142 module->module.SizeOfStruct = sizeof(module->module);
143 module->module.BaseOfImage = mod_addr;
144 module->module.ImageSize = size;
145 module_set_module(module, name);
146 module->module.ImageName[0] = '\0';
147 lstrcpynW(module->module.LoadedImageName, name, sizeof(module->module.LoadedImageName) / sizeof(WCHAR));
148 module->module.SymType = SymNone;
149 module->module.NumSyms = 0;
150 module->module.TimeDateStamp = stamp;
151 module->module.CheckSum = checksum;
153 memset(module->module.LoadedPdbName, 0, sizeof(module->module.CVData));
154 module->module.CVSig = 0;
155 memset(module->module.CVData, 0, sizeof(module->module.CVData));
156 module->module.PdbSig = 0;
157 memset(&module->module.PdbSig70, 0, sizeof(module->module.PdbSig70));
158 module->module.PdbAge = 0;
159 module->module.PdbUnmatched = FALSE;
160 module->module.DbgUnmatched = FALSE;
161 module->module.LineNumbers = FALSE;
162 module->module.GlobalSymbols = FALSE;
163 module->module.TypeInfo = FALSE;
164 module->module.SourceIndexed = FALSE;
165 module->module.Publics = FALSE;
167 module->type = type;
168 module->is_virtual = virtual ? TRUE : FALSE;
169 module->sortlist_valid = FALSE;
170 module->addr_sorttab = NULL;
171 /* FIXME: this seems a bit too high (on a per module basis)
172 * need some statistics about this
174 hash_table_init(&module->pool, &module->ht_symbols, 4096);
175 hash_table_init(&module->pool, &module->ht_types, 4096);
176 vector_init(&module->vtypes, sizeof(struct symt*), 32);
178 module->sources_used = 0;
179 module->sources_alloc = 0;
180 module->sources = 0;
182 return module;
185 /***********************************************************************
186 * module_find_by_name
189 static struct module* module_find_by_name(const struct process* pcs, const WCHAR* name)
191 struct module* module;
193 for (module = pcs->lmodules; module; module = module->next)
195 if (!strcmpiW(name, module->module.ModuleName)) return module;
197 SetLastError(ERROR_INVALID_NAME);
198 return NULL;
201 struct module* module_find_by_nameA(const struct process* pcs, const char* name)
203 WCHAR wname[MAX_PATH];
205 MultiByteToWideChar(CP_ACP, 0, name, -1, wname, sizeof(wname) / sizeof(WCHAR));
206 return module_find_by_name(pcs, wname);
209 /***********************************************************************
210 * module_is_already_loaded
213 struct module* module_is_already_loaded(const struct process* pcs, const WCHAR* name)
215 struct module* module;
216 const WCHAR* filename;
218 /* first compare the loaded image name... */
219 for (module = pcs->lmodules; module; module = module->next)
221 if (!strcmpiW(name, module->module.LoadedImageName))
222 return module;
224 /* then compare the standard filenames (without the path) ... */
225 filename = get_filename(name, NULL);
226 for (module = pcs->lmodules; module; module = module->next)
228 if (!strcmpiW(filename, get_filename(module->module.LoadedImageName, NULL)))
229 return module;
231 SetLastError(ERROR_INVALID_NAME);
232 return NULL;
235 /***********************************************************************
236 * module_get_container
239 static struct module* module_get_container(const struct process* pcs,
240 const struct module* inner)
242 struct module* module;
244 for (module = pcs->lmodules; module; module = module->next)
246 if (module != inner &&
247 module->module.BaseOfImage <= inner->module.BaseOfImage &&
248 module->module.BaseOfImage + module->module.ImageSize >=
249 inner->module.BaseOfImage + inner->module.ImageSize)
250 return module;
252 return NULL;
255 /***********************************************************************
256 * module_get_containee
259 struct module* module_get_containee(const struct process* pcs,
260 const struct module* outter)
262 struct module* module;
264 for (module = pcs->lmodules; module; module = module->next)
266 if (module != outter &&
267 outter->module.BaseOfImage <= module->module.BaseOfImage &&
268 outter->module.BaseOfImage + outter->module.ImageSize >=
269 module->module.BaseOfImage + module->module.ImageSize)
270 return module;
272 return NULL;
275 /******************************************************************
276 * module_get_debug
278 * get the debug information from a module:
279 * - if the module's type is deferred, then force loading of debug info (and return
280 * the module itself)
281 * - if the module has no debug info and has an ELF container, then return the ELF
282 * container (and also force the ELF container's debug info loading if deferred)
283 * - otherwise return the module itself if it has some debug info
285 BOOL module_get_debug(struct module_pair* pair)
287 IMAGEHLP_DEFERRED_SYMBOL_LOADW64 idslW64;
289 if (!pair->requested) return FALSE;
290 /* for a PE builtin, always get info from container */
291 if (!(pair->effective = module_get_container(pair->pcs, pair->requested)))
292 pair->effective = pair->requested;
293 /* if deferred, force loading */
294 if (pair->effective->module.SymType == SymDeferred)
296 BOOL ret;
298 if (pair->effective->is_virtual) ret = FALSE;
299 else switch (pair->effective->type)
301 case DMT_ELF:
302 ret = elf_load_debug_info(pair->effective, NULL);
303 break;
304 case DMT_PE:
305 idslW64.SizeOfStruct = sizeof(idslW64);
306 idslW64.BaseOfImage = pair->effective->module.BaseOfImage;
307 idslW64.CheckSum = pair->effective->module.CheckSum;
308 idslW64.TimeDateStamp = pair->effective->module.TimeDateStamp;
309 memcpy(idslW64.FileName, pair->effective->module.ImageName,
310 sizeof(pair->effective->module.ImageName));
311 idslW64.Reparse = FALSE;
312 idslW64.hFile = INVALID_HANDLE_VALUE;
314 pcs_callback(pair->pcs, CBA_DEFERRED_SYMBOL_LOAD_START, &idslW64);
315 ret = pe_load_debug_info(pair->pcs, pair->effective);
316 pcs_callback(pair->pcs,
317 ret ? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE : CBA_DEFERRED_SYMBOL_LOAD_FAILURE,
318 &idslW64);
319 break;
320 case DMT_MACHO:
321 ret = macho_load_debug_info(pair->effective, NULL);
322 break;
323 default:
324 ret = FALSE;
325 break;
327 if (!ret) pair->effective->module.SymType = SymNone;
328 assert(pair->effective->module.SymType != SymDeferred);
329 pair->effective->module.NumSyms = pair->effective->ht_symbols.num_elts;
331 return pair->effective->module.SymType != SymNone;
334 /***********************************************************************
335 * module_find_by_addr
337 * either the addr where module is loaded, or any address inside the
338 * module
340 struct module* module_find_by_addr(const struct process* pcs, unsigned long addr,
341 enum module_type type)
343 struct module* module;
345 if (type == DMT_UNKNOWN)
347 if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
348 (module = module_find_by_addr(pcs, addr, DMT_ELF)) ||
349 (module = module_find_by_addr(pcs, addr, DMT_MACHO)))
350 return module;
352 else
354 for (module = pcs->lmodules; module; module = module->next)
356 if (type == module->type && addr >= module->module.BaseOfImage &&
357 addr < module->module.BaseOfImage + module->module.ImageSize)
358 return module;
361 SetLastError(ERROR_INVALID_ADDRESS);
362 return module;
365 /******************************************************************
366 * module_is_container_loaded
368 * checks whether the native container, for a (supposed) PE builtin is
369 * already loaded
371 static BOOL module_is_container_loaded(const struct process* pcs,
372 const WCHAR* ImageName, DWORD base)
374 size_t len;
375 struct module* module;
376 PCWSTR filename, modname;
378 if (!base) return FALSE;
379 filename = get_filename(ImageName, NULL);
380 len = strlenW(filename);
382 for (module = pcs->lmodules; module; module = module->next)
384 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
385 base >= module->module.BaseOfImage &&
386 base < module->module.BaseOfImage + module->module.ImageSize)
388 modname = get_filename(module->module.LoadedImageName, NULL);
389 if (!strncmpiW(modname, filename, len) &&
390 !memcmp(modname + len, S_DotSoW, 3 * sizeof(WCHAR)))
392 return TRUE;
396 /* likely a native PE module */
397 WARN("Couldn't find container for %s\n", debugstr_w(ImageName));
398 return FALSE;
401 /******************************************************************
402 * module_get_type_by_name
404 * Guesses a filename type from its extension
406 enum module_type module_get_type_by_name(const WCHAR* name)
408 int len = strlenW(name);
410 /* Skip all version extensions (.[digits]) regex: "(\.\d+)*$" */
413 int i = len;
415 while (i && isdigit(name[i - 1])) i--;
417 if (i && name[i - 1] == '.')
418 len = i - 1;
419 else
420 break;
421 } while (len);
423 /* check for terminating .so or .so.[digit] */
424 /* FIXME: Can't rely solely on extension; have to check magic or
425 * stop using .so on Mac OS X. For now, base on platform. */
426 if (len > 3 && !memcmp(name + len - 3, S_DotSoW, 3))
427 #ifdef __APPLE__
428 return DMT_MACHO;
429 #else
430 return DMT_ELF;
431 #endif
433 if (len > 4 && !strncmpiW(name + len - 4, S_DotPdbW, 4))
434 return DMT_PDB;
436 if (len > 4 && !strncmpiW(name + len - 4, S_DotDbgW, 4))
437 return DMT_DBG;
439 /* wine is also a native module (Mach-O on Mac OS X, ELF elsewhere) */
440 if (((len > 4 && name[len - 5] == '/') || len == 4) && !strcmpiW(name + len - 4, S_WineW))
442 #ifdef __APPLE__
443 return DMT_MACHO;
444 #else
445 return DMT_ELF;
446 #endif
448 return DMT_PE;
451 /***********************************************************************
452 * SymLoadModule (DBGHELP.@)
454 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
455 PCSTR ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
457 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll,
458 SizeOfDll, NULL, 0);
461 /***********************************************************************
462 * SymLoadModuleEx (DBGHELP.@)
464 DWORD64 WINAPI SymLoadModuleEx(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
465 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD DllSize,
466 PMODLOAD_DATA Data, DWORD Flags)
468 PWSTR wImageName, wModuleName;
469 unsigned len;
470 DWORD64 ret;
472 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
473 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
474 wine_dbgstr_longlong(BaseOfDll), DllSize, Data, Flags);
476 if (ImageName)
478 len = MultiByteToWideChar(CP_ACP, 0, ImageName, -1, NULL, 0);
479 wImageName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
480 MultiByteToWideChar(CP_ACP, 0, ImageName, -1, wImageName, len);
482 else wImageName = NULL;
483 if (ModuleName)
485 len = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
486 wModuleName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
487 MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, wModuleName, len);
489 else wModuleName = NULL;
491 ret = SymLoadModuleExW(hProcess, hFile, wImageName, wModuleName,
492 BaseOfDll, DllSize, Data, Flags);
493 HeapFree(GetProcessHeap(), 0, wImageName);
494 HeapFree(GetProcessHeap(), 0, wModuleName);
495 return ret;
498 /***********************************************************************
499 * SymLoadModuleExW (DBGHELP.@)
501 DWORD64 WINAPI SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageName,
502 PCWSTR wModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll,
503 PMODLOAD_DATA Data, DWORD Flags)
505 struct process* pcs;
506 struct module* module = NULL;
508 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
509 hProcess, hFile, debugstr_w(wImageName), debugstr_w(wModuleName),
510 wine_dbgstr_longlong(BaseOfDll), SizeOfDll, Data, Flags);
512 if (Data)
513 FIXME("Unsupported load data parameter %p for %s\n",
514 Data, debugstr_w(wImageName));
515 if (!validate_addr64(BaseOfDll)) return FALSE;
517 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
519 if (Flags & SLMFLAG_VIRTUAL)
521 module = module_new(pcs, wImageName, module_get_type_by_name(wImageName),
522 TRUE, (DWORD)BaseOfDll, SizeOfDll, 0, 0);
523 if (!module) return FALSE;
524 if (wModuleName) module_set_module(module, wModuleName);
525 module->module.SymType = SymVirtual;
527 return TRUE;
529 if (Flags & ~(SLMFLAG_VIRTUAL))
530 FIXME("Unsupported Flags %08x for %s\n", Flags, debugstr_w(wImageName));
532 /* force transparent ELF and Mach-O loading / unloading */
533 elf_synchronize_module_list(pcs);
534 macho_synchronize_module_list(pcs);
536 /* this is a Wine extension to the API just to redo the synchronisation */
537 if (!wImageName && !hFile) return 0;
539 /* check if the module is already loaded, or if it's a builtin PE module with
540 * an containing ELF module
542 if (wImageName)
544 module = module_is_already_loaded(pcs, wImageName);
545 if (!module && module_is_container_loaded(pcs, wImageName, BaseOfDll))
547 /* force the loading of DLL as builtin */
548 module = pe_load_builtin_module(pcs, wImageName, BaseOfDll, SizeOfDll);
551 if (!module)
553 /* otherwise, try a regular PE module */
554 if (!(module = pe_load_native_module(pcs, wImageName, hFile, BaseOfDll, SizeOfDll)) &&
555 wImageName)
557 /* and finally an ELF or Mach-O module */
558 switch (module_get_type_by_name(wImageName))
560 case DMT_ELF:
561 module = elf_load_module(pcs, wImageName, BaseOfDll);
562 break;
563 case DMT_MACHO:
564 module = macho_load_module(pcs, wImageName, BaseOfDll);
565 break;
566 default:
567 /* Ignored */
568 break;
572 if (!module)
574 WARN("Couldn't locate %s\n", debugstr_w(wImageName));
575 return 0;
577 module->module.NumSyms = module->ht_symbols.num_elts;
578 /* by default module_new fills module.ModuleName from a derivation
579 * of LoadedImageName. Overwrite it, if we have better information
581 if (wModuleName)
582 module_set_module(module, wModuleName);
583 lstrcpynW(module->module.ImageName, wImageName,
584 sizeof(module->module.ImageName) / sizeof(WCHAR));
586 return module->module.BaseOfImage;
589 /***********************************************************************
590 * SymLoadModule64 (DBGHELP.@)
592 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
593 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
595 if (!validate_addr64(BaseOfDll)) return FALSE;
596 return SymLoadModule(hProcess, hFile, ImageName, ModuleName, (DWORD)BaseOfDll, SizeOfDll);
599 /******************************************************************
600 * module_remove
603 BOOL module_remove(struct process* pcs, struct module* module)
605 struct module** p;
607 TRACE("%s (%p)\n", debugstr_w(module->module.ModuleName), module);
608 hash_table_destroy(&module->ht_symbols);
609 hash_table_destroy(&module->ht_types);
610 HeapFree(GetProcessHeap(), 0, module->sources);
611 HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
612 HeapFree(GetProcessHeap(), 0, module->dwarf2_info);
613 pool_destroy(&module->pool);
614 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
615 * so do we
617 for (p = &pcs->lmodules; *p; p = &(*p)->next)
619 if (*p == module)
621 *p = module->next;
622 HeapFree(GetProcessHeap(), 0, module);
623 return TRUE;
626 FIXME("This shouldn't happen\n");
627 return FALSE;
630 /******************************************************************
631 * SymUnloadModule (DBGHELP.@)
634 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
636 struct process* pcs;
637 struct module* module;
639 pcs = process_find_by_handle(hProcess);
640 if (!pcs) return FALSE;
641 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
642 if (!module) return FALSE;
643 return module_remove(pcs, module);
646 /******************************************************************
647 * SymUnloadModule64 (DBGHELP.@)
650 BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
652 struct process* pcs;
653 struct module* module;
655 pcs = process_find_by_handle(hProcess);
656 if (!pcs) return FALSE;
657 if (!validate_addr64(BaseOfDll)) return FALSE;
658 module = module_find_by_addr(pcs, (DWORD)BaseOfDll, DMT_UNKNOWN);
659 if (!module) return FALSE;
660 return module_remove(pcs, module);
663 /******************************************************************
664 * SymEnumerateModules (DBGHELP.@)
667 struct enum_modW64_32
669 PSYM_ENUMMODULES_CALLBACK cb;
670 PVOID user;
671 char module[MAX_PATH];
674 static BOOL CALLBACK enum_modW64_32(PCWSTR name, DWORD64 base, PVOID user)
676 struct enum_modW64_32* x = user;
678 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
679 return x->cb(x->module, (DWORD)base, x->user);
682 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
683 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
684 PVOID UserContext)
686 struct enum_modW64_32 x;
688 x.cb = EnumModulesCallback;
689 x.user = UserContext;
691 return SymEnumerateModulesW64(hProcess, enum_modW64_32, &x);
694 /******************************************************************
695 * SymEnumerateModules64 (DBGHELP.@)
698 struct enum_modW64_64
700 PSYM_ENUMMODULES_CALLBACK64 cb;
701 PVOID user;
702 char module[MAX_PATH];
705 static BOOL CALLBACK enum_modW64_64(PCWSTR name, DWORD64 base, PVOID user)
707 struct enum_modW64_64* x = user;
709 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
710 return x->cb(x->module, base, x->user);
713 BOOL WINAPI SymEnumerateModules64(HANDLE hProcess,
714 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,
715 PVOID UserContext)
717 struct enum_modW64_64 x;
719 x.cb = EnumModulesCallback;
720 x.user = UserContext;
722 return SymEnumerateModulesW64(hProcess, enum_modW64_64, &x);
725 /******************************************************************
726 * SymEnumerateModulesW64 (DBGHELP.@)
729 BOOL WINAPI SymEnumerateModulesW64(HANDLE hProcess,
730 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback,
731 PVOID UserContext)
733 struct process* pcs = process_find_by_handle(hProcess);
734 struct module* module;
736 if (!pcs) return FALSE;
738 for (module = pcs->lmodules; module; module = module->next)
740 if (!(dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES) &&
741 (module->type == DMT_ELF || module->type == DMT_MACHO))
742 continue;
743 if (!EnumModulesCallback(module->module.ModuleName,
744 module->module.BaseOfImage, UserContext))
745 break;
747 return TRUE;
750 /******************************************************************
751 * EnumerateLoadedModules64 (DBGHELP.@)
754 struct enum_load_modW64_64
756 PENUMLOADED_MODULES_CALLBACK64 cb;
757 PVOID user;
758 char module[MAX_PATH];
761 static BOOL CALLBACK enum_load_modW64_64(PCWSTR name, DWORD64 base, ULONG size,
762 PVOID user)
764 struct enum_load_modW64_64* x = user;
766 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
767 return x->cb(x->module, base, size, x->user);
770 BOOL WINAPI EnumerateLoadedModules64(HANDLE hProcess,
771 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
772 PVOID UserContext)
774 struct enum_load_modW64_64 x;
776 x.cb = EnumLoadedModulesCallback;
777 x.user = UserContext;
779 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_64, &x);
782 /******************************************************************
783 * EnumerateLoadedModules (DBGHELP.@)
786 struct enum_load_modW64_32
788 PENUMLOADED_MODULES_CALLBACK cb;
789 PVOID user;
790 char module[MAX_PATH];
793 static BOOL CALLBACK enum_load_modW64_32(PCWSTR name, DWORD64 base, ULONG size,
794 PVOID user)
796 struct enum_load_modW64_32* x = user;
797 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
798 return x->cb(x->module, (DWORD)base, size, x->user);
801 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
802 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
803 PVOID UserContext)
805 struct enum_load_modW64_32 x;
807 x.cb = EnumLoadedModulesCallback;
808 x.user = UserContext;
810 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_32, &x);
813 /******************************************************************
814 * EnumerateLoadedModulesW64 (DBGHELP.@)
817 BOOL WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
818 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback,
819 PVOID UserContext)
821 HMODULE* hMods;
822 WCHAR baseW[256], modW[256];
823 DWORD i, sz;
824 MODULEINFO mi;
826 hMods = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods[0]));
827 if (!hMods) return FALSE;
829 if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
831 /* hProcess should also be a valid process handle !! */
832 FIXME("If this happens, bump the number in mod\n");
833 HeapFree(GetProcessHeap(), 0, hMods);
834 return FALSE;
836 sz /= sizeof(HMODULE);
837 for (i = 0; i < sz; i++)
839 if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
840 !GetModuleBaseNameW(hProcess, hMods[i], baseW, sizeof(baseW) / sizeof(WCHAR)))
841 continue;
842 module_fill_module(baseW, modW, sizeof(modW) / sizeof(CHAR));
843 EnumLoadedModulesCallback(modW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage,
844 UserContext);
846 HeapFree(GetProcessHeap(), 0, hMods);
848 return sz != 0 && i == sz;
851 /******************************************************************
852 * SymGetModuleInfo (DBGHELP.@)
855 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
856 PIMAGEHLP_MODULE ModuleInfo)
858 IMAGEHLP_MODULE mi;
859 IMAGEHLP_MODULEW64 miw64;
861 if (sizeof(mi) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
863 miw64.SizeOfStruct = sizeof(miw64);
864 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
866 mi.SizeOfStruct = miw64.SizeOfStruct;
867 mi.BaseOfImage = miw64.BaseOfImage;
868 mi.ImageSize = miw64.ImageSize;
869 mi.TimeDateStamp = miw64.TimeDateStamp;
870 mi.CheckSum = miw64.CheckSum;
871 mi.NumSyms = miw64.NumSyms;
872 mi.SymType = miw64.SymType;
873 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
874 mi.ModuleName, sizeof(mi.ModuleName), NULL, NULL);
875 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
876 mi.ImageName, sizeof(mi.ImageName), NULL, NULL);
877 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
878 mi.LoadedImageName, sizeof(mi.LoadedImageName), NULL, NULL);
880 memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
882 return TRUE;
885 /******************************************************************
886 * SymGetModuleInfoW (DBGHELP.@)
889 BOOL WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
890 PIMAGEHLP_MODULEW ModuleInfo)
892 IMAGEHLP_MODULEW64 miw64;
893 IMAGEHLP_MODULEW miw;
895 if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
897 miw64.SizeOfStruct = sizeof(miw64);
898 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
900 miw.SizeOfStruct = miw64.SizeOfStruct;
901 miw.BaseOfImage = miw64.BaseOfImage;
902 miw.ImageSize = miw64.ImageSize;
903 miw.TimeDateStamp = miw64.TimeDateStamp;
904 miw.CheckSum = miw64.CheckSum;
905 miw.NumSyms = miw64.NumSyms;
906 miw.SymType = miw64.SymType;
907 strcpyW(miw.ModuleName, miw64.ModuleName);
908 strcpyW(miw.ImageName, miw64.ImageName);
909 strcpyW(miw.LoadedImageName, miw64.LoadedImageName);
910 memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
912 return TRUE;
915 /******************************************************************
916 * SymGetModuleInfo64 (DBGHELP.@)
919 BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
920 PIMAGEHLP_MODULE64 ModuleInfo)
922 IMAGEHLP_MODULE64 mi64;
923 IMAGEHLP_MODULEW64 miw64;
925 if (sizeof(mi64) < ModuleInfo->SizeOfStruct)
927 SetLastError(ERROR_MOD_NOT_FOUND); /* NOTE: native returns this error */
928 WARN("Wrong size %u\n", ModuleInfo->SizeOfStruct);
929 return FALSE;
932 miw64.SizeOfStruct = sizeof(miw64);
933 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
935 mi64.SizeOfStruct = miw64.SizeOfStruct;
936 mi64.BaseOfImage = miw64.BaseOfImage;
937 mi64.ImageSize = miw64.ImageSize;
938 mi64.TimeDateStamp = miw64.TimeDateStamp;
939 mi64.CheckSum = miw64.CheckSum;
940 mi64.NumSyms = miw64.NumSyms;
941 mi64.SymType = miw64.SymType;
942 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
943 mi64.ModuleName, sizeof(mi64.ModuleName), NULL, NULL);
944 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
945 mi64.ImageName, sizeof(mi64.ImageName), NULL, NULL);
946 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
947 mi64.LoadedImageName, sizeof(mi64.LoadedImageName), NULL, NULL);
948 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedPdbName, -1,
949 mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName), NULL, NULL);
951 mi64.CVSig = miw64.CVSig;
952 WideCharToMultiByte(CP_ACP, 0, miw64.CVData, -1,
953 mi64.CVData, sizeof(mi64.CVData), NULL, NULL);
954 mi64.PdbSig = miw64.PdbSig;
955 mi64.PdbSig70 = miw64.PdbSig70;
956 mi64.PdbAge = miw64.PdbAge;
957 mi64.PdbUnmatched = miw64.PdbUnmatched;
958 mi64.DbgUnmatched = miw64.DbgUnmatched;
959 mi64.LineNumbers = miw64.LineNumbers;
960 mi64.GlobalSymbols = miw64.GlobalSymbols;
961 mi64.TypeInfo = miw64.TypeInfo;
962 mi64.SourceIndexed = miw64.SourceIndexed;
963 mi64.Publics = miw64.Publics;
965 memcpy(ModuleInfo, &mi64, ModuleInfo->SizeOfStruct);
967 return TRUE;
970 /******************************************************************
971 * SymGetModuleInfoW64 (DBGHELP.@)
974 BOOL WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr,
975 PIMAGEHLP_MODULEW64 ModuleInfo)
977 struct process* pcs = process_find_by_handle(hProcess);
978 struct module* module;
979 IMAGEHLP_MODULEW64 miw64;
981 TRACE("%p %s %p\n", hProcess, wine_dbgstr_longlong(dwAddr), ModuleInfo);
983 if (!pcs) return FALSE;
984 if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
985 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
986 if (!module) return FALSE;
988 miw64 = module->module;
990 /* update debug information from container if any */
991 if (module->module.SymType == SymNone)
993 module = module_get_container(pcs, module);
994 if (module && module->module.SymType != SymNone)
996 miw64.SymType = module->module.SymType;
997 miw64.NumSyms = module->module.NumSyms;
1000 memcpy(ModuleInfo, &miw64, ModuleInfo->SizeOfStruct);
1001 return TRUE;
1004 /***********************************************************************
1005 * SymGetModuleBase (DBGHELP.@)
1007 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
1009 struct process* pcs = process_find_by_handle(hProcess);
1010 struct module* module;
1012 if (!pcs) return 0;
1013 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1014 if (!module) return 0;
1015 return module->module.BaseOfImage;
1018 /***********************************************************************
1019 * SymGetModuleBase64 (DBGHELP.@)
1021 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
1023 if (!validate_addr64(dwAddr)) return 0;
1024 return SymGetModuleBase(hProcess, (DWORD)dwAddr);
1027 /******************************************************************
1028 * module_reset_debug_info
1029 * Removes any debug information linked to a given module.
1031 void module_reset_debug_info(struct module* module)
1033 module->sortlist_valid = TRUE;
1034 module->addr_sorttab = NULL;
1035 hash_table_destroy(&module->ht_symbols);
1036 module->ht_symbols.num_buckets = 0;
1037 module->ht_symbols.buckets = NULL;
1038 hash_table_destroy(&module->ht_types);
1039 module->ht_types.num_buckets = 0;
1040 module->ht_types.buckets = NULL;
1041 module->vtypes.num_elts = 0;
1042 hash_table_destroy(&module->ht_symbols);
1043 module->sources_used = module->sources_alloc = 0;
1044 module->sources = NULL;