powrprof: Add stubs for PowerGetActiveScheme and PowerReadDCValue.
[wine/hacks.git] / dlls / dbghelp / module.c
blob53f56c25129c6952e842921d20683149a9444455
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;
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 %s-%s %s\n",
138 get_module_type(type, virtual),
139 wine_dbgstr_longlong(mod_addr), wine_dbgstr_longlong(mod_addr + size),
140 debugstr_w(name));
142 pool_init(&module->pool, 65536);
144 module->module.SizeOfStruct = sizeof(module->module);
145 module->module.BaseOfImage = mod_addr;
146 module->module.ImageSize = size;
147 module_set_module(module, name);
148 module->module.ImageName[0] = '\0';
149 lstrcpynW(module->module.LoadedImageName, name, sizeof(module->module.LoadedImageName) / sizeof(WCHAR));
150 module->module.SymType = SymNone;
151 module->module.NumSyms = 0;
152 module->module.TimeDateStamp = stamp;
153 module->module.CheckSum = checksum;
155 memset(module->module.LoadedPdbName, 0, sizeof(module->module.CVData));
156 module->module.CVSig = 0;
157 memset(module->module.CVData, 0, sizeof(module->module.CVData));
158 module->module.PdbSig = 0;
159 memset(&module->module.PdbSig70, 0, sizeof(module->module.PdbSig70));
160 module->module.PdbAge = 0;
161 module->module.PdbUnmatched = FALSE;
162 module->module.DbgUnmatched = FALSE;
163 module->module.LineNumbers = FALSE;
164 module->module.GlobalSymbols = FALSE;
165 module->module.TypeInfo = FALSE;
166 module->module.SourceIndexed = FALSE;
167 module->module.Publics = FALSE;
169 module->type = type;
170 module->is_virtual = virtual ? TRUE : FALSE;
171 module->sortlist_valid = FALSE;
172 module->sorttab_size = 0;
173 module->addr_sorttab = NULL;
174 module->num_sorttab = 0;
175 module->num_symbols = 0;
177 vector_init(&module->vsymt, sizeof(struct symt*), 128);
178 /* FIXME: this seems a bit too high (on a per module basis)
179 * need some statistics about this
181 hash_table_init(&module->pool, &module->ht_symbols, 4096);
182 hash_table_init(&module->pool, &module->ht_types, 4096);
183 vector_init(&module->vtypes, sizeof(struct symt*), 32);
185 module->sources_used = 0;
186 module->sources_alloc = 0;
187 module->sources = 0;
189 return module;
192 /***********************************************************************
193 * module_find_by_name
196 static struct module* module_find_by_name(const struct process* pcs, const WCHAR* name)
198 struct module* module;
200 for (module = pcs->lmodules; module; module = module->next)
202 if (!strcmpiW(name, module->module.ModuleName)) return module;
204 SetLastError(ERROR_INVALID_NAME);
205 return NULL;
208 struct module* module_find_by_nameA(const struct process* pcs, const char* name)
210 WCHAR wname[MAX_PATH];
212 MultiByteToWideChar(CP_ACP, 0, name, -1, wname, sizeof(wname) / sizeof(WCHAR));
213 return module_find_by_name(pcs, wname);
216 /***********************************************************************
217 * module_is_already_loaded
220 struct module* module_is_already_loaded(const struct process* pcs, const WCHAR* name)
222 struct module* module;
223 const WCHAR* filename;
225 /* first compare the loaded image name... */
226 for (module = pcs->lmodules; module; module = module->next)
228 if (!strcmpiW(name, module->module.LoadedImageName))
229 return module;
231 /* then compare the standard filenames (without the path) ... */
232 filename = get_filename(name, NULL);
233 for (module = pcs->lmodules; module; module = module->next)
235 if (!strcmpiW(filename, get_filename(module->module.LoadedImageName, NULL)))
236 return module;
238 SetLastError(ERROR_INVALID_NAME);
239 return NULL;
242 /***********************************************************************
243 * module_get_container
246 static struct module* module_get_container(const struct process* pcs,
247 const struct module* inner)
249 struct module* module;
251 for (module = pcs->lmodules; module; module = module->next)
253 if (module != inner &&
254 module->module.BaseOfImage <= inner->module.BaseOfImage &&
255 module->module.BaseOfImage + module->module.ImageSize >=
256 inner->module.BaseOfImage + inner->module.ImageSize)
257 return module;
259 return NULL;
262 /***********************************************************************
263 * module_get_containee
266 struct module* module_get_containee(const struct process* pcs,
267 const struct module* outter)
269 struct module* module;
271 for (module = pcs->lmodules; module; module = module->next)
273 if (module != outter &&
274 outter->module.BaseOfImage <= module->module.BaseOfImage &&
275 outter->module.BaseOfImage + outter->module.ImageSize >=
276 module->module.BaseOfImage + module->module.ImageSize)
277 return module;
279 return NULL;
282 /******************************************************************
283 * module_get_debug
285 * get the debug information from a module:
286 * - if the module's type is deferred, then force loading of debug info (and return
287 * the module itself)
288 * - if the module has no debug info and has an ELF container, then return the ELF
289 * container (and also force the ELF container's debug info loading if deferred)
290 * - otherwise return the module itself if it has some debug info
292 BOOL module_get_debug(struct module_pair* pair)
294 IMAGEHLP_DEFERRED_SYMBOL_LOADW64 idslW64;
296 if (!pair->requested) return FALSE;
297 /* for a PE builtin, always get info from container */
298 if (!(pair->effective = module_get_container(pair->pcs, pair->requested)))
299 pair->effective = pair->requested;
300 /* if deferred, force loading */
301 if (pair->effective->module.SymType == SymDeferred)
303 BOOL ret;
305 if (pair->effective->is_virtual) ret = FALSE;
306 else switch (pair->effective->type)
308 case DMT_ELF:
309 ret = elf_load_debug_info(pair->effective, NULL);
310 break;
311 case DMT_PE:
312 idslW64.SizeOfStruct = sizeof(idslW64);
313 idslW64.BaseOfImage = pair->effective->module.BaseOfImage;
314 idslW64.CheckSum = pair->effective->module.CheckSum;
315 idslW64.TimeDateStamp = pair->effective->module.TimeDateStamp;
316 memcpy(idslW64.FileName, pair->effective->module.ImageName,
317 sizeof(pair->effective->module.ImageName));
318 idslW64.Reparse = FALSE;
319 idslW64.hFile = INVALID_HANDLE_VALUE;
321 pcs_callback(pair->pcs, CBA_DEFERRED_SYMBOL_LOAD_START, &idslW64);
322 ret = pe_load_debug_info(pair->pcs, pair->effective);
323 pcs_callback(pair->pcs,
324 ret ? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE : CBA_DEFERRED_SYMBOL_LOAD_FAILURE,
325 &idslW64);
326 break;
327 case DMT_MACHO:
328 ret = macho_load_debug_info(pair->effective, NULL);
329 break;
330 default:
331 ret = FALSE;
332 break;
334 if (!ret) pair->effective->module.SymType = SymNone;
335 assert(pair->effective->module.SymType != SymDeferred);
336 pair->effective->module.NumSyms = pair->effective->ht_symbols.num_elts;
338 return pair->effective->module.SymType != SymNone;
341 /***********************************************************************
342 * module_find_by_addr
344 * either the addr where module is loaded, or any address inside the
345 * module
347 struct module* module_find_by_addr(const struct process* pcs, unsigned long addr,
348 enum module_type type)
350 struct module* module;
352 if (type == DMT_UNKNOWN)
354 if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
355 (module = module_find_by_addr(pcs, addr, DMT_ELF)) ||
356 (module = module_find_by_addr(pcs, addr, DMT_MACHO)))
357 return module;
359 else
361 for (module = pcs->lmodules; module; module = module->next)
363 if (type == module->type && addr >= module->module.BaseOfImage &&
364 addr < module->module.BaseOfImage + module->module.ImageSize)
365 return module;
368 SetLastError(ERROR_INVALID_ADDRESS);
369 return module;
372 /******************************************************************
373 * module_is_container_loaded
375 * checks whether the native container, for a (supposed) PE builtin is
376 * already loaded
378 static BOOL module_is_container_loaded(const struct process* pcs,
379 const WCHAR* ImageName, DWORD64 base)
381 size_t len;
382 struct module* module;
383 PCWSTR filename, modname;
385 if (!base) return FALSE;
386 filename = get_filename(ImageName, NULL);
387 len = strlenW(filename);
389 for (module = pcs->lmodules; module; module = module->next)
391 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
392 base >= module->module.BaseOfImage &&
393 base < module->module.BaseOfImage + module->module.ImageSize)
395 modname = get_filename(module->module.LoadedImageName, NULL);
396 if (!strncmpiW(modname, filename, len) &&
397 !memcmp(modname + len, S_DotSoW, 3 * sizeof(WCHAR)))
399 return TRUE;
403 /* likely a native PE module */
404 WARN("Couldn't find container for %s\n", debugstr_w(ImageName));
405 return FALSE;
408 /******************************************************************
409 * module_get_type_by_name
411 * Guesses a filename type from its extension
413 enum module_type module_get_type_by_name(const WCHAR* name)
415 int len = strlenW(name);
417 /* Skip all version extensions (.[digits]) regex: "(\.\d+)*$" */
420 int i = len;
422 while (i && isdigit(name[i - 1])) i--;
424 if (i && name[i - 1] == '.')
425 len = i - 1;
426 else
427 break;
428 } while (len);
430 /* check for terminating .so or .so.[digit] */
431 /* FIXME: Can't rely solely on extension; have to check magic or
432 * stop using .so on Mac OS X. For now, base on platform. */
433 if (len > 3 && !memcmp(name + len - 3, S_DotSoW, 3))
434 #ifdef __APPLE__
435 return DMT_MACHO;
436 #else
437 return DMT_ELF;
438 #endif
440 if (len > 6 && !strncmpiW(name + len - 6, S_DotDylibW, 6))
441 return DMT_MACHO;
443 if (len > 4 && !strncmpiW(name + len - 4, S_DotPdbW, 4))
444 return DMT_PDB;
446 if (len > 4 && !strncmpiW(name + len - 4, S_DotDbgW, 4))
447 return DMT_DBG;
449 /* wine is also a native module (Mach-O on Mac OS X, ELF elsewhere) */
450 if (((len > 4 && name[len - 5] == '/') || len == 4) && !strcmpiW(name + len - 4, S_WineW))
452 #ifdef __APPLE__
453 return DMT_MACHO;
454 #else
455 return DMT_ELF;
456 #endif
458 return DMT_PE;
461 /******************************************************************
462 * refresh_module_list
464 static BOOL refresh_module_list(struct process* pcs)
466 /* force transparent ELF and Mach-O loading / unloading */
467 return elf_synchronize_module_list(pcs) || macho_synchronize_module_list(pcs);
470 /***********************************************************************
471 * SymLoadModule (DBGHELP.@)
473 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
474 PCSTR ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
476 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll,
477 SizeOfDll, NULL, 0);
480 /***********************************************************************
481 * SymLoadModuleEx (DBGHELP.@)
483 DWORD64 WINAPI SymLoadModuleEx(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
484 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD DllSize,
485 PMODLOAD_DATA Data, DWORD Flags)
487 PWSTR wImageName, wModuleName;
488 unsigned len;
489 DWORD64 ret;
491 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
492 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
493 wine_dbgstr_longlong(BaseOfDll), DllSize, Data, Flags);
495 if (ImageName)
497 len = MultiByteToWideChar(CP_ACP, 0, ImageName, -1, NULL, 0);
498 wImageName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
499 MultiByteToWideChar(CP_ACP, 0, ImageName, -1, wImageName, len);
501 else wImageName = NULL;
502 if (ModuleName)
504 len = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
505 wModuleName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
506 MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, wModuleName, len);
508 else wModuleName = NULL;
510 ret = SymLoadModuleExW(hProcess, hFile, wImageName, wModuleName,
511 BaseOfDll, DllSize, Data, Flags);
512 HeapFree(GetProcessHeap(), 0, wImageName);
513 HeapFree(GetProcessHeap(), 0, wModuleName);
514 return ret;
517 /***********************************************************************
518 * SymLoadModuleExW (DBGHELP.@)
520 DWORD64 WINAPI SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageName,
521 PCWSTR wModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll,
522 PMODLOAD_DATA Data, DWORD Flags)
524 struct process* pcs;
525 struct module* module = NULL;
527 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
528 hProcess, hFile, debugstr_w(wImageName), debugstr_w(wModuleName),
529 wine_dbgstr_longlong(BaseOfDll), SizeOfDll, Data, Flags);
531 if (Data)
532 FIXME("Unsupported load data parameter %p for %s\n",
533 Data, debugstr_w(wImageName));
534 if (!validate_addr64(BaseOfDll)) return FALSE;
536 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
538 if (Flags & SLMFLAG_VIRTUAL)
540 if (!wImageName) return FALSE;
541 module = module_new(pcs, wImageName, module_get_type_by_name(wImageName),
542 TRUE, BaseOfDll, SizeOfDll, 0, 0);
543 if (!module) return FALSE;
544 if (wModuleName) module_set_module(module, wModuleName);
545 module->module.SymType = SymVirtual;
547 return TRUE;
549 if (Flags & ~(SLMFLAG_VIRTUAL))
550 FIXME("Unsupported Flags %08x for %s\n", Flags, debugstr_w(wImageName));
552 refresh_module_list(pcs);
554 /* this is a Wine extension to the API just to redo the synchronisation */
555 if (!wImageName && !hFile) return 0;
557 /* check if the module is already loaded, or if it's a builtin PE module with
558 * an containing ELF module
560 if (wImageName)
562 module = module_is_already_loaded(pcs, wImageName);
563 if (!module && module_is_container_loaded(pcs, wImageName, BaseOfDll))
565 /* force the loading of DLL as builtin */
566 module = pe_load_builtin_module(pcs, wImageName, BaseOfDll, SizeOfDll);
569 if (!module)
571 /* otherwise, try a regular PE module */
572 if (!(module = pe_load_native_module(pcs, wImageName, hFile, BaseOfDll, SizeOfDll)) &&
573 wImageName)
575 /* and finally an ELF or Mach-O module */
576 switch (module_get_type_by_name(wImageName))
578 case DMT_ELF:
579 module = elf_load_module(pcs, wImageName, BaseOfDll);
580 break;
581 case DMT_MACHO:
582 module = macho_load_module(pcs, wImageName, BaseOfDll);
583 break;
584 default:
585 /* Ignored */
586 break;
590 if (!module)
592 WARN("Couldn't locate %s\n", debugstr_w(wImageName));
593 return 0;
595 module->module.NumSyms = module->ht_symbols.num_elts;
596 /* by default module_new fills module.ModuleName from a derivation
597 * of LoadedImageName. Overwrite it, if we have better information
599 if (wModuleName)
600 module_set_module(module, wModuleName);
601 if (wImageName)
602 lstrcpynW(module->module.ImageName, wImageName,
603 sizeof(module->module.ImageName) / sizeof(WCHAR));
605 return module->module.BaseOfImage;
608 /***********************************************************************
609 * SymLoadModule64 (DBGHELP.@)
611 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
612 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
614 if (!validate_addr64(BaseOfDll)) return FALSE;
615 return SymLoadModule(hProcess, hFile, ImageName, ModuleName, (DWORD)BaseOfDll, SizeOfDll);
618 /******************************************************************
619 * module_remove
622 BOOL module_remove(struct process* pcs, struct module* module)
624 struct module** p;
626 TRACE("%s (%p)\n", debugstr_w(module->module.ModuleName), module);
627 hash_table_destroy(&module->ht_symbols);
628 hash_table_destroy(&module->ht_types);
629 HeapFree(GetProcessHeap(), 0, module->sources);
630 HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
631 HeapFree(GetProcessHeap(), 0, module->dwarf2_info);
632 pool_destroy(&module->pool);
633 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
634 * so do we
636 for (p = &pcs->lmodules; *p; p = &(*p)->next)
638 if (*p == module)
640 *p = module->next;
641 HeapFree(GetProcessHeap(), 0, module);
642 return TRUE;
645 FIXME("This shouldn't happen\n");
646 return FALSE;
649 /******************************************************************
650 * SymUnloadModule (DBGHELP.@)
653 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
655 struct process* pcs;
656 struct module* module;
658 pcs = process_find_by_handle(hProcess);
659 if (!pcs) return FALSE;
660 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
661 if (!module) return FALSE;
662 return module_remove(pcs, module);
665 /******************************************************************
666 * SymUnloadModule64 (DBGHELP.@)
669 BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
671 struct process* pcs;
672 struct module* module;
674 pcs = process_find_by_handle(hProcess);
675 if (!pcs) return FALSE;
676 if (!validate_addr64(BaseOfDll)) return FALSE;
677 module = module_find_by_addr(pcs, (DWORD)BaseOfDll, DMT_UNKNOWN);
678 if (!module) return FALSE;
679 return module_remove(pcs, module);
682 /******************************************************************
683 * SymEnumerateModules (DBGHELP.@)
686 struct enum_modW64_32
688 PSYM_ENUMMODULES_CALLBACK cb;
689 PVOID user;
690 char module[MAX_PATH];
693 static BOOL CALLBACK enum_modW64_32(PCWSTR name, DWORD64 base, PVOID user)
695 struct enum_modW64_32* x = user;
697 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
698 return x->cb(x->module, (DWORD)base, x->user);
701 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
702 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
703 PVOID UserContext)
705 struct enum_modW64_32 x;
707 x.cb = EnumModulesCallback;
708 x.user = UserContext;
710 return SymEnumerateModulesW64(hProcess, enum_modW64_32, &x);
713 /******************************************************************
714 * SymEnumerateModules64 (DBGHELP.@)
717 struct enum_modW64_64
719 PSYM_ENUMMODULES_CALLBACK64 cb;
720 PVOID user;
721 char module[MAX_PATH];
724 static BOOL CALLBACK enum_modW64_64(PCWSTR name, DWORD64 base, PVOID user)
726 struct enum_modW64_64* x = user;
728 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
729 return x->cb(x->module, base, x->user);
732 BOOL WINAPI SymEnumerateModules64(HANDLE hProcess,
733 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,
734 PVOID UserContext)
736 struct enum_modW64_64 x;
738 x.cb = EnumModulesCallback;
739 x.user = UserContext;
741 return SymEnumerateModulesW64(hProcess, enum_modW64_64, &x);
744 /******************************************************************
745 * SymEnumerateModulesW64 (DBGHELP.@)
748 BOOL WINAPI SymEnumerateModulesW64(HANDLE hProcess,
749 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback,
750 PVOID UserContext)
752 struct process* pcs = process_find_by_handle(hProcess);
753 struct module* module;
755 if (!pcs) return FALSE;
757 for (module = pcs->lmodules; module; module = module->next)
759 if (!(dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES) &&
760 (module->type == DMT_ELF || module->type == DMT_MACHO))
761 continue;
762 if (!EnumModulesCallback(module->module.ModuleName,
763 module->module.BaseOfImage, UserContext))
764 break;
766 return TRUE;
769 /******************************************************************
770 * EnumerateLoadedModules64 (DBGHELP.@)
773 struct enum_load_modW64_64
775 PENUMLOADED_MODULES_CALLBACK64 cb;
776 PVOID user;
777 char module[MAX_PATH];
780 static BOOL CALLBACK enum_load_modW64_64(PCWSTR name, DWORD64 base, ULONG size,
781 PVOID user)
783 struct enum_load_modW64_64* x = user;
785 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
786 return x->cb(x->module, base, size, x->user);
789 BOOL WINAPI EnumerateLoadedModules64(HANDLE hProcess,
790 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
791 PVOID UserContext)
793 struct enum_load_modW64_64 x;
795 x.cb = EnumLoadedModulesCallback;
796 x.user = UserContext;
798 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_64, &x);
801 /******************************************************************
802 * EnumerateLoadedModules (DBGHELP.@)
805 struct enum_load_modW64_32
807 PENUMLOADED_MODULES_CALLBACK cb;
808 PVOID user;
809 char module[MAX_PATH];
812 static BOOL CALLBACK enum_load_modW64_32(PCWSTR name, DWORD64 base, ULONG size,
813 PVOID user)
815 struct enum_load_modW64_32* x = user;
816 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
817 return x->cb(x->module, (DWORD)base, size, x->user);
820 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
821 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
822 PVOID UserContext)
824 struct enum_load_modW64_32 x;
826 x.cb = EnumLoadedModulesCallback;
827 x.user = UserContext;
829 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_32, &x);
832 /******************************************************************
833 * EnumerateLoadedModulesW64 (DBGHELP.@)
836 BOOL WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
837 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback,
838 PVOID UserContext)
840 HMODULE* hMods;
841 WCHAR baseW[256], modW[256];
842 DWORD i, sz;
843 MODULEINFO mi;
845 hMods = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods[0]));
846 if (!hMods) return FALSE;
848 if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
850 /* hProcess should also be a valid process handle !! */
851 FIXME("If this happens, bump the number in mod\n");
852 HeapFree(GetProcessHeap(), 0, hMods);
853 return FALSE;
855 sz /= sizeof(HMODULE);
856 for (i = 0; i < sz; i++)
858 if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
859 !GetModuleBaseNameW(hProcess, hMods[i], baseW, sizeof(baseW) / sizeof(WCHAR)))
860 continue;
861 module_fill_module(baseW, modW, sizeof(modW) / sizeof(CHAR));
862 EnumLoadedModulesCallback(modW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage,
863 UserContext);
865 HeapFree(GetProcessHeap(), 0, hMods);
867 return sz != 0 && i == sz;
870 /******************************************************************
871 * SymGetModuleInfo (DBGHELP.@)
874 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
875 PIMAGEHLP_MODULE ModuleInfo)
877 IMAGEHLP_MODULE mi;
878 IMAGEHLP_MODULEW64 miw64;
880 if (sizeof(mi) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
882 miw64.SizeOfStruct = sizeof(miw64);
883 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
885 mi.SizeOfStruct = miw64.SizeOfStruct;
886 mi.BaseOfImage = miw64.BaseOfImage;
887 mi.ImageSize = miw64.ImageSize;
888 mi.TimeDateStamp = miw64.TimeDateStamp;
889 mi.CheckSum = miw64.CheckSum;
890 mi.NumSyms = miw64.NumSyms;
891 mi.SymType = miw64.SymType;
892 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
893 mi.ModuleName, sizeof(mi.ModuleName), NULL, NULL);
894 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
895 mi.ImageName, sizeof(mi.ImageName), NULL, NULL);
896 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
897 mi.LoadedImageName, sizeof(mi.LoadedImageName), NULL, NULL);
899 memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
901 return TRUE;
904 /******************************************************************
905 * SymGetModuleInfoW (DBGHELP.@)
908 BOOL WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
909 PIMAGEHLP_MODULEW ModuleInfo)
911 IMAGEHLP_MODULEW64 miw64;
912 IMAGEHLP_MODULEW miw;
914 if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
916 miw64.SizeOfStruct = sizeof(miw64);
917 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
919 miw.SizeOfStruct = miw64.SizeOfStruct;
920 miw.BaseOfImage = miw64.BaseOfImage;
921 miw.ImageSize = miw64.ImageSize;
922 miw.TimeDateStamp = miw64.TimeDateStamp;
923 miw.CheckSum = miw64.CheckSum;
924 miw.NumSyms = miw64.NumSyms;
925 miw.SymType = miw64.SymType;
926 strcpyW(miw.ModuleName, miw64.ModuleName);
927 strcpyW(miw.ImageName, miw64.ImageName);
928 strcpyW(miw.LoadedImageName, miw64.LoadedImageName);
929 memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
931 return TRUE;
934 /******************************************************************
935 * SymGetModuleInfo64 (DBGHELP.@)
938 BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
939 PIMAGEHLP_MODULE64 ModuleInfo)
941 IMAGEHLP_MODULE64 mi64;
942 IMAGEHLP_MODULEW64 miw64;
944 if (sizeof(mi64) < ModuleInfo->SizeOfStruct)
946 SetLastError(ERROR_MOD_NOT_FOUND); /* NOTE: native returns this error */
947 WARN("Wrong size %u\n", ModuleInfo->SizeOfStruct);
948 return FALSE;
951 miw64.SizeOfStruct = sizeof(miw64);
952 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
954 mi64.SizeOfStruct = miw64.SizeOfStruct;
955 mi64.BaseOfImage = miw64.BaseOfImage;
956 mi64.ImageSize = miw64.ImageSize;
957 mi64.TimeDateStamp = miw64.TimeDateStamp;
958 mi64.CheckSum = miw64.CheckSum;
959 mi64.NumSyms = miw64.NumSyms;
960 mi64.SymType = miw64.SymType;
961 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
962 mi64.ModuleName, sizeof(mi64.ModuleName), NULL, NULL);
963 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
964 mi64.ImageName, sizeof(mi64.ImageName), NULL, NULL);
965 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
966 mi64.LoadedImageName, sizeof(mi64.LoadedImageName), NULL, NULL);
967 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedPdbName, -1,
968 mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName), NULL, NULL);
970 mi64.CVSig = miw64.CVSig;
971 WideCharToMultiByte(CP_ACP, 0, miw64.CVData, -1,
972 mi64.CVData, sizeof(mi64.CVData), NULL, NULL);
973 mi64.PdbSig = miw64.PdbSig;
974 mi64.PdbSig70 = miw64.PdbSig70;
975 mi64.PdbAge = miw64.PdbAge;
976 mi64.PdbUnmatched = miw64.PdbUnmatched;
977 mi64.DbgUnmatched = miw64.DbgUnmatched;
978 mi64.LineNumbers = miw64.LineNumbers;
979 mi64.GlobalSymbols = miw64.GlobalSymbols;
980 mi64.TypeInfo = miw64.TypeInfo;
981 mi64.SourceIndexed = miw64.SourceIndexed;
982 mi64.Publics = miw64.Publics;
984 memcpy(ModuleInfo, &mi64, ModuleInfo->SizeOfStruct);
986 return TRUE;
989 /******************************************************************
990 * SymGetModuleInfoW64 (DBGHELP.@)
993 BOOL WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr,
994 PIMAGEHLP_MODULEW64 ModuleInfo)
996 struct process* pcs = process_find_by_handle(hProcess);
997 struct module* module;
998 IMAGEHLP_MODULEW64 miw64;
1000 TRACE("%p %s %p\n", hProcess, wine_dbgstr_longlong(dwAddr), ModuleInfo);
1002 if (!pcs) return FALSE;
1003 if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
1004 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1005 if (!module) return FALSE;
1007 miw64 = module->module;
1009 /* update debug information from container if any */
1010 if (module->module.SymType == SymNone)
1012 module = module_get_container(pcs, module);
1013 if (module && module->module.SymType != SymNone)
1015 miw64.SymType = module->module.SymType;
1016 miw64.NumSyms = module->module.NumSyms;
1019 memcpy(ModuleInfo, &miw64, ModuleInfo->SizeOfStruct);
1020 return TRUE;
1023 /***********************************************************************
1024 * SymGetModuleBase (DBGHELP.@)
1026 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
1028 struct process* pcs = process_find_by_handle(hProcess);
1029 struct module* module;
1031 if (!pcs) return 0;
1032 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1033 if (!module) return 0;
1034 return module->module.BaseOfImage;
1037 /***********************************************************************
1038 * SymGetModuleBase64 (DBGHELP.@)
1040 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
1042 if (!validate_addr64(dwAddr)) return 0;
1043 return SymGetModuleBase(hProcess, (DWORD)dwAddr);
1046 /******************************************************************
1047 * module_reset_debug_info
1048 * Removes any debug information linked to a given module.
1050 void module_reset_debug_info(struct module* module)
1052 module->sortlist_valid = TRUE;
1053 module->sorttab_size = 0;
1054 module->addr_sorttab = NULL;
1055 module->num_sorttab = module->num_symbols = 0;
1056 hash_table_destroy(&module->ht_symbols);
1057 module->ht_symbols.num_buckets = 0;
1058 module->ht_symbols.buckets = NULL;
1059 hash_table_destroy(&module->ht_types);
1060 module->ht_types.num_buckets = 0;
1061 module->ht_types.buckets = NULL;
1062 module->vtypes.num_elts = 0;
1063 hash_table_destroy(&module->ht_symbols);
1064 module->sources_used = module->sources_alloc = 0;
1065 module->sources = NULL;
1068 /******************************************************************
1069 * SymRefreshModuleList (DBGHELP.@)
1071 BOOL WINAPI SymRefreshModuleList(HANDLE hProcess)
1073 struct process* pcs;
1075 TRACE("(%p)\n", hProcess);
1077 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1079 return refresh_module_list(pcs);