wined3d: Pass a wined3d_state structure to use_ps().
[wine/multimedia.git] / dlls / dbghelp / module.c
blob89422a9fc091b6195fd7f2ea1beb6da898010ceb
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 DWORD64 mod_addr, DWORD64 size,
126 unsigned long stamp, unsigned long checksum)
128 struct module* module;
129 unsigned i;
131 assert(type == DMT_ELF || type == DMT_PE || type == DMT_MACHO);
132 if (!(module = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*module))))
133 return NULL;
135 module->next = pcs->lmodules;
136 pcs->lmodules = module;
138 TRACE("=> %s %s-%s %s\n",
139 get_module_type(type, virtual),
140 wine_dbgstr_longlong(mod_addr), wine_dbgstr_longlong(mod_addr + size),
141 debugstr_w(name));
143 pool_init(&module->pool, 65536);
145 module->process = pcs;
146 module->module.SizeOfStruct = sizeof(module->module);
147 module->module.BaseOfImage = mod_addr;
148 module->module.ImageSize = size;
149 module_set_module(module, name);
150 module->module.ImageName[0] = '\0';
151 lstrcpynW(module->module.LoadedImageName, name, sizeof(module->module.LoadedImageName) / sizeof(WCHAR));
152 module->module.SymType = SymNone;
153 module->module.NumSyms = 0;
154 module->module.TimeDateStamp = stamp;
155 module->module.CheckSum = checksum;
157 memset(module->module.LoadedPdbName, 0, sizeof(module->module.CVData));
158 module->module.CVSig = 0;
159 memset(module->module.CVData, 0, sizeof(module->module.CVData));
160 module->module.PdbSig = 0;
161 memset(&module->module.PdbSig70, 0, sizeof(module->module.PdbSig70));
162 module->module.PdbAge = 0;
163 module->module.PdbUnmatched = FALSE;
164 module->module.DbgUnmatched = FALSE;
165 module->module.LineNumbers = FALSE;
166 module->module.GlobalSymbols = FALSE;
167 module->module.TypeInfo = FALSE;
168 module->module.SourceIndexed = FALSE;
169 module->module.Publics = FALSE;
171 module->reloc_delta = 0;
172 module->type = type;
173 module->is_virtual = virtual ? TRUE : FALSE;
174 for (i = 0; i < DFI_LAST; i++) module->format_info[i] = NULL;
175 module->sortlist_valid = FALSE;
176 module->sorttab_size = 0;
177 module->addr_sorttab = NULL;
178 module->num_sorttab = 0;
179 module->num_symbols = 0;
181 vector_init(&module->vsymt, sizeof(struct symt*), 128);
182 /* FIXME: this seems a bit too high (on a per module basis)
183 * need some statistics about this
185 hash_table_init(&module->pool, &module->ht_symbols, 4096);
186 hash_table_init(&module->pool, &module->ht_types, 4096);
187 vector_init(&module->vtypes, sizeof(struct symt*), 32);
189 module->sources_used = 0;
190 module->sources_alloc = 0;
191 module->sources = 0;
193 return module;
196 /***********************************************************************
197 * module_find_by_name
200 static struct module* module_find_by_name(const struct process* pcs, const WCHAR* name)
202 struct module* module;
204 for (module = pcs->lmodules; module; module = module->next)
206 if (!strcmpiW(name, module->module.ModuleName)) return module;
208 SetLastError(ERROR_INVALID_NAME);
209 return NULL;
212 struct module* module_find_by_nameA(const struct process* pcs, const char* name)
214 WCHAR wname[MAX_PATH];
216 MultiByteToWideChar(CP_ACP, 0, name, -1, wname, sizeof(wname) / sizeof(WCHAR));
217 return module_find_by_name(pcs, wname);
220 /***********************************************************************
221 * module_is_already_loaded
224 struct module* module_is_already_loaded(const struct process* pcs, const WCHAR* name)
226 struct module* module;
227 const WCHAR* filename;
229 /* first compare the loaded image name... */
230 for (module = pcs->lmodules; module; module = module->next)
232 if (!strcmpiW(name, module->module.LoadedImageName))
233 return module;
235 /* then compare the standard filenames (without the path) ... */
236 filename = get_filename(name, NULL);
237 for (module = pcs->lmodules; module; module = module->next)
239 if (!strcmpiW(filename, get_filename(module->module.LoadedImageName, NULL)))
240 return module;
242 SetLastError(ERROR_INVALID_NAME);
243 return NULL;
246 /***********************************************************************
247 * module_get_container
250 static struct module* module_get_container(const struct process* pcs,
251 const struct module* inner)
253 struct module* module;
255 for (module = pcs->lmodules; module; module = module->next)
257 if (module != inner &&
258 module->module.BaseOfImage <= inner->module.BaseOfImage &&
259 module->module.BaseOfImage + module->module.ImageSize >=
260 inner->module.BaseOfImage + inner->module.ImageSize)
261 return module;
263 return NULL;
266 /***********************************************************************
267 * module_get_containee
270 struct module* module_get_containee(const struct process* pcs,
271 const struct module* outter)
273 struct module* module;
275 for (module = pcs->lmodules; module; module = module->next)
277 if (module != outter &&
278 outter->module.BaseOfImage <= module->module.BaseOfImage &&
279 outter->module.BaseOfImage + outter->module.ImageSize >=
280 module->module.BaseOfImage + module->module.ImageSize)
281 return module;
283 return NULL;
286 /******************************************************************
287 * module_get_debug
289 * get the debug information from a module:
290 * - if the module's type is deferred, then force loading of debug info (and return
291 * the module itself)
292 * - if the module has no debug info and has an ELF container, then return the ELF
293 * container (and also force the ELF container's debug info loading if deferred)
294 * - otherwise return the module itself if it has some debug info
296 BOOL module_get_debug(struct module_pair* pair)
298 IMAGEHLP_DEFERRED_SYMBOL_LOADW64 idslW64;
300 if (!pair->requested) return FALSE;
301 /* for a PE builtin, always get info from container */
302 if (!(pair->effective = module_get_container(pair->pcs, pair->requested)))
303 pair->effective = pair->requested;
304 /* if deferred, force loading */
305 if (pair->effective->module.SymType == SymDeferred)
307 BOOL ret;
309 if (pair->effective->is_virtual) ret = FALSE;
310 else switch (pair->effective->type)
312 case DMT_ELF:
313 ret = elf_load_debug_info(pair->effective, NULL);
314 break;
315 case DMT_PE:
316 idslW64.SizeOfStruct = sizeof(idslW64);
317 idslW64.BaseOfImage = pair->effective->module.BaseOfImage;
318 idslW64.CheckSum = pair->effective->module.CheckSum;
319 idslW64.TimeDateStamp = pair->effective->module.TimeDateStamp;
320 memcpy(idslW64.FileName, pair->effective->module.ImageName,
321 sizeof(pair->effective->module.ImageName));
322 idslW64.Reparse = FALSE;
323 idslW64.hFile = INVALID_HANDLE_VALUE;
325 pcs_callback(pair->pcs, CBA_DEFERRED_SYMBOL_LOAD_START, &idslW64);
326 ret = pe_load_debug_info(pair->pcs, pair->effective);
327 pcs_callback(pair->pcs,
328 ret ? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE : CBA_DEFERRED_SYMBOL_LOAD_FAILURE,
329 &idslW64);
330 break;
331 case DMT_MACHO:
332 ret = macho_load_debug_info(pair->effective, NULL);
333 break;
334 default:
335 ret = FALSE;
336 break;
338 if (!ret) pair->effective->module.SymType = SymNone;
339 assert(pair->effective->module.SymType != SymDeferred);
340 pair->effective->module.NumSyms = pair->effective->ht_symbols.num_elts;
342 return pair->effective->module.SymType != SymNone;
345 /***********************************************************************
346 * module_find_by_addr
348 * either the addr where module is loaded, or any address inside the
349 * module
351 struct module* module_find_by_addr(const struct process* pcs, unsigned long addr,
352 enum module_type type)
354 struct module* module;
356 if (type == DMT_UNKNOWN)
358 if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
359 (module = module_find_by_addr(pcs, addr, DMT_ELF)) ||
360 (module = module_find_by_addr(pcs, addr, DMT_MACHO)))
361 return module;
363 else
365 for (module = pcs->lmodules; module; module = module->next)
367 if (type == module->type && addr >= module->module.BaseOfImage &&
368 addr < module->module.BaseOfImage + module->module.ImageSize)
369 return module;
372 SetLastError(ERROR_INVALID_ADDRESS);
373 return module;
376 /******************************************************************
377 * module_is_container_loaded
379 * checks whether the native container, for a (supposed) PE builtin is
380 * already loaded
382 static BOOL module_is_container_loaded(const struct process* pcs,
383 const WCHAR* ImageName, DWORD64 base)
385 size_t len;
386 struct module* module;
387 PCWSTR filename, modname;
389 if (!base) return FALSE;
390 filename = get_filename(ImageName, NULL);
391 len = strlenW(filename);
393 for (module = pcs->lmodules; module; module = module->next)
395 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
396 base >= module->module.BaseOfImage &&
397 base < module->module.BaseOfImage + module->module.ImageSize)
399 modname = get_filename(module->module.LoadedImageName, NULL);
400 if (!strncmpiW(modname, filename, len) &&
401 !memcmp(modname + len, S_DotSoW, 3 * sizeof(WCHAR)))
403 return TRUE;
407 /* likely a native PE module */
408 WARN("Couldn't find container for %s\n", debugstr_w(ImageName));
409 return FALSE;
412 /******************************************************************
413 * module_get_type_by_name
415 * Guesses a filename type from its extension
417 enum module_type module_get_type_by_name(const WCHAR* name)
419 int len = strlenW(name);
421 /* Skip all version extensions (.[digits]) regex: "(\.\d+)*$" */
424 int i = len;
426 while (i && isdigit(name[i - 1])) i--;
428 if (i && name[i - 1] == '.')
429 len = i - 1;
430 else
431 break;
432 } while (len);
434 /* check for terminating .so or .so.[digit] */
435 /* FIXME: Can't rely solely on extension; have to check magic or
436 * stop using .so on Mac OS X. For now, base on platform. */
437 if (len > 3 && !memcmp(name + len - 3, S_DotSoW, 3))
438 #ifdef __APPLE__
439 return DMT_MACHO;
440 #else
441 return DMT_ELF;
442 #endif
444 if (len > 6 && !strncmpiW(name + len - 6, S_DotDylibW, 6))
445 return DMT_MACHO;
447 if (len > 4 && !strncmpiW(name + len - 4, S_DotPdbW, 4))
448 return DMT_PDB;
450 if (len > 4 && !strncmpiW(name + len - 4, S_DotDbgW, 4))
451 return DMT_DBG;
453 /* wine is also a native module (Mach-O on Mac OS X, ELF elsewhere) */
454 if (((len > 4 && name[len - 5] == '/') || len == 4) && !strcmpiW(name + len - 4, S_WineW))
456 #ifdef __APPLE__
457 return DMT_MACHO;
458 #else
459 return DMT_ELF;
460 #endif
462 return DMT_PE;
465 /******************************************************************
466 * refresh_module_list
468 static BOOL refresh_module_list(struct process* pcs)
470 /* force transparent ELF and Mach-O loading / unloading */
471 return elf_synchronize_module_list(pcs) || macho_synchronize_module_list(pcs);
474 /***********************************************************************
475 * SymLoadModule (DBGHELP.@)
477 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
478 PCSTR ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
480 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll,
481 SizeOfDll, NULL, 0);
484 /***********************************************************************
485 * SymLoadModuleEx (DBGHELP.@)
487 DWORD64 WINAPI SymLoadModuleEx(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
488 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD DllSize,
489 PMODLOAD_DATA Data, DWORD Flags)
491 PWSTR wImageName, wModuleName;
492 unsigned len;
493 DWORD64 ret;
495 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
496 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
497 wine_dbgstr_longlong(BaseOfDll), DllSize, Data, Flags);
499 if (ImageName)
501 len = MultiByteToWideChar(CP_ACP, 0, ImageName, -1, NULL, 0);
502 wImageName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
503 MultiByteToWideChar(CP_ACP, 0, ImageName, -1, wImageName, len);
505 else wImageName = NULL;
506 if (ModuleName)
508 len = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
509 wModuleName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
510 MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, wModuleName, len);
512 else wModuleName = NULL;
514 ret = SymLoadModuleExW(hProcess, hFile, wImageName, wModuleName,
515 BaseOfDll, DllSize, Data, Flags);
516 HeapFree(GetProcessHeap(), 0, wImageName);
517 HeapFree(GetProcessHeap(), 0, wModuleName);
518 return ret;
521 /***********************************************************************
522 * SymLoadModuleExW (DBGHELP.@)
524 DWORD64 WINAPI SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageName,
525 PCWSTR wModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll,
526 PMODLOAD_DATA Data, DWORD Flags)
528 struct process* pcs;
529 struct module* module = NULL;
531 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
532 hProcess, hFile, debugstr_w(wImageName), debugstr_w(wModuleName),
533 wine_dbgstr_longlong(BaseOfDll), SizeOfDll, Data, Flags);
535 if (Data)
536 FIXME("Unsupported load data parameter %p for %s\n",
537 Data, debugstr_w(wImageName));
538 if (!validate_addr64(BaseOfDll)) return FALSE;
540 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
542 if (Flags & SLMFLAG_VIRTUAL)
544 if (!wImageName) return FALSE;
545 module = module_new(pcs, wImageName, module_get_type_by_name(wImageName),
546 TRUE, BaseOfDll, SizeOfDll, 0, 0);
547 if (!module) return FALSE;
548 if (wModuleName) module_set_module(module, wModuleName);
549 module->module.SymType = SymVirtual;
551 return TRUE;
553 if (Flags & ~(SLMFLAG_VIRTUAL))
554 FIXME("Unsupported Flags %08x for %s\n", Flags, debugstr_w(wImageName));
556 refresh_module_list(pcs);
558 /* this is a Wine extension to the API just to redo the synchronisation */
559 if (!wImageName && !hFile) return 0;
561 /* check if the module is already loaded, or if it's a builtin PE module with
562 * an containing ELF module
564 if (wImageName)
566 module = module_is_already_loaded(pcs, wImageName);
567 if (!module && module_is_container_loaded(pcs, wImageName, BaseOfDll))
569 /* force the loading of DLL as builtin */
570 module = pe_load_builtin_module(pcs, wImageName, BaseOfDll, SizeOfDll);
573 if (!module)
575 /* otherwise, try a regular PE module */
576 if (!(module = pe_load_native_module(pcs, wImageName, hFile, BaseOfDll, SizeOfDll)) &&
577 wImageName)
579 /* and finally an ELF or Mach-O module */
580 switch (module_get_type_by_name(wImageName))
582 case DMT_ELF:
583 module = elf_load_module(pcs, wImageName, BaseOfDll);
584 break;
585 case DMT_MACHO:
586 module = macho_load_module(pcs, wImageName, BaseOfDll);
587 break;
588 default:
589 /* Ignored */
590 break;
594 if (!module)
596 WARN("Couldn't locate %s\n", debugstr_w(wImageName));
597 return 0;
599 module->module.NumSyms = module->ht_symbols.num_elts;
600 /* by default module_new fills module.ModuleName from a derivation
601 * of LoadedImageName. Overwrite it, if we have better information
603 if (wModuleName)
604 module_set_module(module, wModuleName);
605 if (wImageName)
606 lstrcpynW(module->module.ImageName, wImageName,
607 sizeof(module->module.ImageName) / sizeof(WCHAR));
609 return module->module.BaseOfImage;
612 /***********************************************************************
613 * SymLoadModule64 (DBGHELP.@)
615 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
616 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
618 if (!validate_addr64(BaseOfDll)) return FALSE;
619 return SymLoadModule(hProcess, hFile, ImageName, ModuleName, (DWORD)BaseOfDll, SizeOfDll);
622 /******************************************************************
623 * module_remove
626 BOOL module_remove(struct process* pcs, struct module* module)
628 struct module_format*modfmt;
629 struct module** p;
630 unsigned i;
632 TRACE("%s (%p)\n", debugstr_w(module->module.ModuleName), module);
634 for (i = 0; i < DFI_LAST; i++)
636 if ((modfmt = module->format_info[i]) && modfmt->remove)
637 modfmt->remove(pcs, module->format_info[i]);
639 hash_table_destroy(&module->ht_symbols);
640 hash_table_destroy(&module->ht_types);
641 HeapFree(GetProcessHeap(), 0, module->sources);
642 HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
643 pool_destroy(&module->pool);
644 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
645 * so do we
647 for (p = &pcs->lmodules; *p; p = &(*p)->next)
649 if (*p == module)
651 *p = module->next;
652 HeapFree(GetProcessHeap(), 0, module);
653 return TRUE;
656 FIXME("This shouldn't happen\n");
657 return FALSE;
660 /******************************************************************
661 * SymUnloadModule (DBGHELP.@)
664 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
666 struct process* pcs;
667 struct module* module;
669 pcs = process_find_by_handle(hProcess);
670 if (!pcs) return FALSE;
671 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
672 if (!module) return FALSE;
673 return module_remove(pcs, module);
676 /******************************************************************
677 * SymUnloadModule64 (DBGHELP.@)
680 BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
682 struct process* pcs;
683 struct module* module;
685 pcs = process_find_by_handle(hProcess);
686 if (!pcs) return FALSE;
687 if (!validate_addr64(BaseOfDll)) return FALSE;
688 module = module_find_by_addr(pcs, (DWORD)BaseOfDll, DMT_UNKNOWN);
689 if (!module) return FALSE;
690 return module_remove(pcs, module);
693 /******************************************************************
694 * SymEnumerateModules (DBGHELP.@)
697 struct enum_modW64_32
699 PSYM_ENUMMODULES_CALLBACK cb;
700 PVOID user;
701 char module[MAX_PATH];
704 static BOOL CALLBACK enum_modW64_32(PCWSTR name, DWORD64 base, PVOID user)
706 struct enum_modW64_32* x = user;
708 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
709 return x->cb(x->module, (DWORD)base, x->user);
712 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
713 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
714 PVOID UserContext)
716 struct enum_modW64_32 x;
718 x.cb = EnumModulesCallback;
719 x.user = UserContext;
721 return SymEnumerateModulesW64(hProcess, enum_modW64_32, &x);
724 /******************************************************************
725 * SymEnumerateModules64 (DBGHELP.@)
728 struct enum_modW64_64
730 PSYM_ENUMMODULES_CALLBACK64 cb;
731 PVOID user;
732 char module[MAX_PATH];
735 static BOOL CALLBACK enum_modW64_64(PCWSTR name, DWORD64 base, PVOID user)
737 struct enum_modW64_64* x = user;
739 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
740 return x->cb(x->module, base, x->user);
743 BOOL WINAPI SymEnumerateModules64(HANDLE hProcess,
744 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,
745 PVOID UserContext)
747 struct enum_modW64_64 x;
749 x.cb = EnumModulesCallback;
750 x.user = UserContext;
752 return SymEnumerateModulesW64(hProcess, enum_modW64_64, &x);
755 /******************************************************************
756 * SymEnumerateModulesW64 (DBGHELP.@)
759 BOOL WINAPI SymEnumerateModulesW64(HANDLE hProcess,
760 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback,
761 PVOID UserContext)
763 struct process* pcs = process_find_by_handle(hProcess);
764 struct module* module;
766 if (!pcs) return FALSE;
768 for (module = pcs->lmodules; module; module = module->next)
770 if (!(dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES) &&
771 (module->type == DMT_ELF || module->type == DMT_MACHO))
772 continue;
773 if (!EnumModulesCallback(module->module.ModuleName,
774 module->module.BaseOfImage, UserContext))
775 break;
777 return TRUE;
780 /******************************************************************
781 * EnumerateLoadedModules64 (DBGHELP.@)
784 struct enum_load_modW64_64
786 PENUMLOADED_MODULES_CALLBACK64 cb;
787 PVOID user;
788 char module[MAX_PATH];
791 static BOOL CALLBACK enum_load_modW64_64(PCWSTR name, DWORD64 base, ULONG size,
792 PVOID user)
794 struct enum_load_modW64_64* x = user;
796 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
797 return x->cb(x->module, base, size, x->user);
800 BOOL WINAPI EnumerateLoadedModules64(HANDLE hProcess,
801 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
802 PVOID UserContext)
804 struct enum_load_modW64_64 x;
806 x.cb = EnumLoadedModulesCallback;
807 x.user = UserContext;
809 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_64, &x);
812 /******************************************************************
813 * EnumerateLoadedModules (DBGHELP.@)
816 struct enum_load_modW64_32
818 PENUMLOADED_MODULES_CALLBACK cb;
819 PVOID user;
820 char module[MAX_PATH];
823 static BOOL CALLBACK enum_load_modW64_32(PCWSTR name, DWORD64 base, ULONG size,
824 PVOID user)
826 struct enum_load_modW64_32* x = user;
827 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
828 return x->cb(x->module, (DWORD)base, size, x->user);
831 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
832 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
833 PVOID UserContext)
835 struct enum_load_modW64_32 x;
837 x.cb = EnumLoadedModulesCallback;
838 x.user = UserContext;
840 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_32, &x);
843 /******************************************************************
844 * EnumerateLoadedModulesW64 (DBGHELP.@)
847 BOOL WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
848 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback,
849 PVOID UserContext)
851 HMODULE* hMods;
852 WCHAR baseW[256], modW[256];
853 DWORD i, sz;
854 MODULEINFO mi;
856 hMods = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods[0]));
857 if (!hMods) return FALSE;
859 if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
861 /* hProcess should also be a valid process handle !! */
862 FIXME("If this happens, bump the number in mod\n");
863 HeapFree(GetProcessHeap(), 0, hMods);
864 return FALSE;
866 sz /= sizeof(HMODULE);
867 for (i = 0; i < sz; i++)
869 if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
870 !GetModuleBaseNameW(hProcess, hMods[i], baseW, sizeof(baseW) / sizeof(WCHAR)))
871 continue;
872 module_fill_module(baseW, modW, sizeof(modW) / sizeof(CHAR));
873 EnumLoadedModulesCallback(modW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage,
874 UserContext);
876 HeapFree(GetProcessHeap(), 0, hMods);
878 return sz != 0 && i == sz;
881 /******************************************************************
882 * SymGetModuleInfo (DBGHELP.@)
885 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
886 PIMAGEHLP_MODULE ModuleInfo)
888 IMAGEHLP_MODULE mi;
889 IMAGEHLP_MODULEW64 miw64;
891 if (sizeof(mi) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
893 miw64.SizeOfStruct = sizeof(miw64);
894 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
896 mi.SizeOfStruct = miw64.SizeOfStruct;
897 mi.BaseOfImage = miw64.BaseOfImage;
898 mi.ImageSize = miw64.ImageSize;
899 mi.TimeDateStamp = miw64.TimeDateStamp;
900 mi.CheckSum = miw64.CheckSum;
901 mi.NumSyms = miw64.NumSyms;
902 mi.SymType = miw64.SymType;
903 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
904 mi.ModuleName, sizeof(mi.ModuleName), NULL, NULL);
905 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
906 mi.ImageName, sizeof(mi.ImageName), NULL, NULL);
907 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
908 mi.LoadedImageName, sizeof(mi.LoadedImageName), NULL, NULL);
910 memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
912 return TRUE;
915 /******************************************************************
916 * SymGetModuleInfoW (DBGHELP.@)
919 BOOL WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
920 PIMAGEHLP_MODULEW ModuleInfo)
922 IMAGEHLP_MODULEW64 miw64;
923 IMAGEHLP_MODULEW miw;
925 if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
927 miw64.SizeOfStruct = sizeof(miw64);
928 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
930 miw.SizeOfStruct = miw64.SizeOfStruct;
931 miw.BaseOfImage = miw64.BaseOfImage;
932 miw.ImageSize = miw64.ImageSize;
933 miw.TimeDateStamp = miw64.TimeDateStamp;
934 miw.CheckSum = miw64.CheckSum;
935 miw.NumSyms = miw64.NumSyms;
936 miw.SymType = miw64.SymType;
937 strcpyW(miw.ModuleName, miw64.ModuleName);
938 strcpyW(miw.ImageName, miw64.ImageName);
939 strcpyW(miw.LoadedImageName, miw64.LoadedImageName);
940 memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
942 return TRUE;
945 /******************************************************************
946 * SymGetModuleInfo64 (DBGHELP.@)
949 BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
950 PIMAGEHLP_MODULE64 ModuleInfo)
952 IMAGEHLP_MODULE64 mi64;
953 IMAGEHLP_MODULEW64 miw64;
955 if (sizeof(mi64) < ModuleInfo->SizeOfStruct)
957 SetLastError(ERROR_MOD_NOT_FOUND); /* NOTE: native returns this error */
958 WARN("Wrong size %u\n", ModuleInfo->SizeOfStruct);
959 return FALSE;
962 miw64.SizeOfStruct = sizeof(miw64);
963 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
965 mi64.SizeOfStruct = miw64.SizeOfStruct;
966 mi64.BaseOfImage = miw64.BaseOfImage;
967 mi64.ImageSize = miw64.ImageSize;
968 mi64.TimeDateStamp = miw64.TimeDateStamp;
969 mi64.CheckSum = miw64.CheckSum;
970 mi64.NumSyms = miw64.NumSyms;
971 mi64.SymType = miw64.SymType;
972 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
973 mi64.ModuleName, sizeof(mi64.ModuleName), NULL, NULL);
974 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
975 mi64.ImageName, sizeof(mi64.ImageName), NULL, NULL);
976 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
977 mi64.LoadedImageName, sizeof(mi64.LoadedImageName), NULL, NULL);
978 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedPdbName, -1,
979 mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName), NULL, NULL);
981 mi64.CVSig = miw64.CVSig;
982 WideCharToMultiByte(CP_ACP, 0, miw64.CVData, -1,
983 mi64.CVData, sizeof(mi64.CVData), NULL, NULL);
984 mi64.PdbSig = miw64.PdbSig;
985 mi64.PdbSig70 = miw64.PdbSig70;
986 mi64.PdbAge = miw64.PdbAge;
987 mi64.PdbUnmatched = miw64.PdbUnmatched;
988 mi64.DbgUnmatched = miw64.DbgUnmatched;
989 mi64.LineNumbers = miw64.LineNumbers;
990 mi64.GlobalSymbols = miw64.GlobalSymbols;
991 mi64.TypeInfo = miw64.TypeInfo;
992 mi64.SourceIndexed = miw64.SourceIndexed;
993 mi64.Publics = miw64.Publics;
995 memcpy(ModuleInfo, &mi64, ModuleInfo->SizeOfStruct);
997 return TRUE;
1000 /******************************************************************
1001 * SymGetModuleInfoW64 (DBGHELP.@)
1004 BOOL WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr,
1005 PIMAGEHLP_MODULEW64 ModuleInfo)
1007 struct process* pcs = process_find_by_handle(hProcess);
1008 struct module* module;
1009 IMAGEHLP_MODULEW64 miw64;
1011 TRACE("%p %s %p\n", hProcess, wine_dbgstr_longlong(dwAddr), ModuleInfo);
1013 if (!pcs) return FALSE;
1014 if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
1015 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1016 if (!module) return FALSE;
1018 miw64 = module->module;
1020 /* update debug information from container if any */
1021 if (module->module.SymType == SymNone)
1023 module = module_get_container(pcs, module);
1024 if (module && module->module.SymType != SymNone)
1026 miw64.SymType = module->module.SymType;
1027 miw64.NumSyms = module->module.NumSyms;
1030 memcpy(ModuleInfo, &miw64, ModuleInfo->SizeOfStruct);
1031 return TRUE;
1034 /***********************************************************************
1035 * SymGetModuleBase (DBGHELP.@)
1037 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
1039 DWORD64 ret;
1041 ret = SymGetModuleBase64(hProcess, dwAddr);
1042 return validate_addr64(ret) ? ret : 0;
1045 /***********************************************************************
1046 * SymGetModuleBase64 (DBGHELP.@)
1048 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
1050 struct process* pcs = process_find_by_handle(hProcess);
1051 struct module* module;
1053 if (!pcs) return 0;
1054 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1055 if (!module) return 0;
1056 return module->module.BaseOfImage;
1059 /******************************************************************
1060 * module_reset_debug_info
1061 * Removes any debug information linked to a given module.
1063 void module_reset_debug_info(struct module* module)
1065 module->sortlist_valid = TRUE;
1066 module->sorttab_size = 0;
1067 module->addr_sorttab = NULL;
1068 module->num_sorttab = module->num_symbols = 0;
1069 hash_table_destroy(&module->ht_symbols);
1070 module->ht_symbols.num_buckets = 0;
1071 module->ht_symbols.buckets = NULL;
1072 hash_table_destroy(&module->ht_types);
1073 module->ht_types.num_buckets = 0;
1074 module->ht_types.buckets = NULL;
1075 module->vtypes.num_elts = 0;
1076 hash_table_destroy(&module->ht_symbols);
1077 module->sources_used = module->sources_alloc = 0;
1078 module->sources = NULL;
1081 /******************************************************************
1082 * SymRefreshModuleList (DBGHELP.@)
1084 BOOL WINAPI SymRefreshModuleList(HANDLE hProcess)
1086 struct process* pcs;
1088 TRACE("(%p)\n", hProcess);
1090 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1092 return refresh_module_list(pcs);
1095 /***********************************************************************
1096 * SymFunctionTableAccess (DBGHELP.@)
1098 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1100 return SymFunctionTableAccess64(hProcess, AddrBase);
1103 /***********************************************************************
1104 * SymFunctionTableAccess64 (DBGHELP.@)
1106 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1108 struct process* pcs = process_find_by_handle(hProcess);
1109 struct module* module;
1111 if (!pcs || !dbghelp_current_cpu->find_runtime_function) return NULL;
1112 module = module_find_by_addr(pcs, AddrBase, DMT_UNKNOWN);
1113 if (!module) return NULL;
1115 return dbghelp_current_cpu->find_runtime_function(module, AddrBase);