dmusic: Don't use method implementation directly. Pass through vtable with related...
[wine.git] / dlls / dbghelp / module.c
blob136ec4e036ad2e1305a0c7cacf152a7fcbb07153
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_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 *loader = get_wine_loader_name();
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 > strlenW(loader) && !strcmpiW(out + len - strlenW(loader), loader))
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));
106 const WCHAR *get_wine_loader_name(void)
108 static const int is_win64 = sizeof(void *) > sizeof(int); /* FIXME: should depend on target process */
109 static const WCHAR wineW[] = {'w','i','n','e',0};
110 static const WCHAR suffixW[] = {'6','4',0};
111 static const WCHAR *loader;
113 if (!loader)
115 WCHAR *p, *buffer;
116 const char *ptr;
118 /* All binaries are loaded with WINELOADER (if run from tree) or by the
119 * main executable
121 if ((ptr = getenv("WINELOADER")))
123 DWORD len = 2 + MultiByteToWideChar( CP_UNIXCP, 0, ptr, -1, NULL, 0 );
124 buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
125 MultiByteToWideChar( CP_UNIXCP, 0, ptr, -1, buffer, len );
127 else
129 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(wineW) + 2 * sizeof(WCHAR) );
130 strcpyW( buffer, wineW );
132 p = buffer + strlenW( buffer ) - strlenW( suffixW );
133 if (p > buffer && !strcmpW( p, suffixW ))
135 if (!is_win64) *p = 0;
137 else if (is_win64) strcatW( buffer, suffixW );
139 TRACE( "returning %s\n", debugstr_w(buffer) );
140 loader = buffer;
142 return loader;
145 static const char* get_module_type(enum module_type type, BOOL virtual)
147 switch (type)
149 case DMT_ELF: return virtual ? "Virtual ELF" : "ELF";
150 case DMT_PE: return virtual ? "Virtual PE" : "PE";
151 case DMT_MACHO: return virtual ? "Virtual Mach-O" : "Mach-O";
152 default: return "---";
156 /***********************************************************************
157 * Creates and links a new module to a process
159 struct module* module_new(struct process* pcs, const WCHAR* name,
160 enum module_type type, BOOL virtual,
161 DWORD64 mod_addr, DWORD64 size,
162 unsigned long stamp, unsigned long checksum)
164 struct module* module;
165 unsigned i;
167 assert(type == DMT_ELF || type == DMT_PE || type == DMT_MACHO);
168 if (!(module = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*module))))
169 return NULL;
171 module->next = pcs->lmodules;
172 pcs->lmodules = module;
174 TRACE("=> %s %s-%s %s\n",
175 get_module_type(type, virtual),
176 wine_dbgstr_longlong(mod_addr), wine_dbgstr_longlong(mod_addr + size),
177 debugstr_w(name));
179 pool_init(&module->pool, 65536);
181 module->process = pcs;
182 module->module.SizeOfStruct = sizeof(module->module);
183 module->module.BaseOfImage = mod_addr;
184 module->module.ImageSize = size;
185 module_set_module(module, name);
186 module->module.ImageName[0] = '\0';
187 lstrcpynW(module->module.LoadedImageName, name, sizeof(module->module.LoadedImageName) / sizeof(WCHAR));
188 module->module.SymType = SymNone;
189 module->module.NumSyms = 0;
190 module->module.TimeDateStamp = stamp;
191 module->module.CheckSum = checksum;
193 memset(module->module.LoadedPdbName, 0, sizeof(module->module.LoadedPdbName));
194 module->module.CVSig = 0;
195 memset(module->module.CVData, 0, sizeof(module->module.CVData));
196 module->module.PdbSig = 0;
197 memset(&module->module.PdbSig70, 0, sizeof(module->module.PdbSig70));
198 module->module.PdbAge = 0;
199 module->module.PdbUnmatched = FALSE;
200 module->module.DbgUnmatched = FALSE;
201 module->module.LineNumbers = FALSE;
202 module->module.GlobalSymbols = FALSE;
203 module->module.TypeInfo = FALSE;
204 module->module.SourceIndexed = FALSE;
205 module->module.Publics = FALSE;
207 module->reloc_delta = 0;
208 module->type = type;
209 module->is_virtual = virtual ? TRUE : FALSE;
210 for (i = 0; i < DFI_LAST; i++) module->format_info[i] = NULL;
211 module->sortlist_valid = FALSE;
212 module->sorttab_size = 0;
213 module->addr_sorttab = NULL;
214 module->num_sorttab = 0;
215 module->num_symbols = 0;
217 vector_init(&module->vsymt, sizeof(struct symt*), 128);
218 /* FIXME: this seems a bit too high (on a per module basis)
219 * need some statistics about this
221 hash_table_init(&module->pool, &module->ht_symbols, 4096);
222 hash_table_init(&module->pool, &module->ht_types, 4096);
223 vector_init(&module->vtypes, sizeof(struct symt*), 32);
225 module->sources_used = 0;
226 module->sources_alloc = 0;
227 module->sources = 0;
228 wine_rb_init(&module->sources_offsets_tree, &source_rb_functions);
230 return module;
233 /***********************************************************************
234 * module_find_by_nameW
237 struct module* module_find_by_nameW(const struct process* pcs, const WCHAR* name)
239 struct module* module;
241 for (module = pcs->lmodules; module; module = module->next)
243 if (!strcmpiW(name, module->module.ModuleName)) return module;
245 SetLastError(ERROR_INVALID_NAME);
246 return NULL;
249 struct module* module_find_by_nameA(const struct process* pcs, const char* name)
251 WCHAR wname[MAX_PATH];
253 MultiByteToWideChar(CP_ACP, 0, name, -1, wname, sizeof(wname) / sizeof(WCHAR));
254 return module_find_by_nameW(pcs, wname);
257 /***********************************************************************
258 * module_is_already_loaded
261 struct module* module_is_already_loaded(const struct process* pcs, const WCHAR* name)
263 struct module* module;
264 const WCHAR* filename;
266 /* first compare the loaded image name... */
267 for (module = pcs->lmodules; module; module = module->next)
269 if (!strcmpiW(name, module->module.LoadedImageName))
270 return module;
272 /* then compare the standard filenames (without the path) ... */
273 filename = get_filename(name, NULL);
274 for (module = pcs->lmodules; module; module = module->next)
276 if (!strcmpiW(filename, get_filename(module->module.LoadedImageName, NULL)))
277 return module;
279 SetLastError(ERROR_INVALID_NAME);
280 return NULL;
283 /***********************************************************************
284 * module_get_container
287 static struct module* module_get_container(const struct process* pcs,
288 const struct module* inner)
290 struct module* module;
292 for (module = pcs->lmodules; module; module = module->next)
294 if (module != inner &&
295 module->module.BaseOfImage <= inner->module.BaseOfImage &&
296 module->module.BaseOfImage + module->module.ImageSize >=
297 inner->module.BaseOfImage + inner->module.ImageSize)
298 return module;
300 return NULL;
303 /***********************************************************************
304 * module_get_containee
307 struct module* module_get_containee(const struct process* pcs,
308 const struct module* outter)
310 struct module* module;
312 for (module = pcs->lmodules; module; module = module->next)
314 if (module != outter &&
315 outter->module.BaseOfImage <= module->module.BaseOfImage &&
316 outter->module.BaseOfImage + outter->module.ImageSize >=
317 module->module.BaseOfImage + module->module.ImageSize)
318 return module;
320 return NULL;
323 /******************************************************************
324 * module_get_debug
326 * get the debug information from a module:
327 * - if the module's type is deferred, then force loading of debug info (and return
328 * the module itself)
329 * - if the module has no debug info and has an ELF container, then return the ELF
330 * container (and also force the ELF container's debug info loading if deferred)
331 * - otherwise return the module itself if it has some debug info
333 BOOL module_get_debug(struct module_pair* pair)
335 IMAGEHLP_DEFERRED_SYMBOL_LOADW64 idslW64;
337 if (!pair->requested) return FALSE;
338 /* for a PE builtin, always get info from container */
339 if (!(pair->effective = module_get_container(pair->pcs, pair->requested)))
340 pair->effective = pair->requested;
341 /* if deferred, force loading */
342 if (pair->effective->module.SymType == SymDeferred)
344 BOOL ret;
346 if (pair->effective->is_virtual) ret = FALSE;
347 else switch (pair->effective->type)
349 case DMT_ELF:
350 ret = elf_load_debug_info(pair->effective);
351 break;
352 case DMT_PE:
353 idslW64.SizeOfStruct = sizeof(idslW64);
354 idslW64.BaseOfImage = pair->effective->module.BaseOfImage;
355 idslW64.CheckSum = pair->effective->module.CheckSum;
356 idslW64.TimeDateStamp = pair->effective->module.TimeDateStamp;
357 memcpy(idslW64.FileName, pair->effective->module.ImageName,
358 sizeof(pair->effective->module.ImageName));
359 idslW64.Reparse = FALSE;
360 idslW64.hFile = INVALID_HANDLE_VALUE;
362 pcs_callback(pair->pcs, CBA_DEFERRED_SYMBOL_LOAD_START, &idslW64);
363 ret = pe_load_debug_info(pair->pcs, pair->effective);
364 pcs_callback(pair->pcs,
365 ret ? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE : CBA_DEFERRED_SYMBOL_LOAD_FAILURE,
366 &idslW64);
367 break;
368 case DMT_MACHO:
369 ret = macho_load_debug_info(pair->effective, NULL);
370 break;
371 default:
372 ret = FALSE;
373 break;
375 if (!ret) pair->effective->module.SymType = SymNone;
376 assert(pair->effective->module.SymType != SymDeferred);
377 pair->effective->module.NumSyms = pair->effective->ht_symbols.num_elts;
379 return pair->effective->module.SymType != SymNone;
382 /***********************************************************************
383 * module_find_by_addr
385 * either the addr where module is loaded, or any address inside the
386 * module
388 struct module* module_find_by_addr(const struct process* pcs, DWORD64 addr,
389 enum module_type type)
391 struct module* module;
393 if (type == DMT_UNKNOWN)
395 if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
396 (module = module_find_by_addr(pcs, addr, DMT_ELF)) ||
397 (module = module_find_by_addr(pcs, addr, DMT_MACHO)))
398 return module;
400 else
402 for (module = pcs->lmodules; module; module = module->next)
404 if (type == module->type && addr >= module->module.BaseOfImage &&
405 addr < module->module.BaseOfImage + module->module.ImageSize)
406 return module;
409 SetLastError(ERROR_INVALID_ADDRESS);
410 return module;
413 /******************************************************************
414 * module_is_container_loaded
416 * checks whether the native container, for a (supposed) PE builtin is
417 * already loaded
419 static BOOL module_is_container_loaded(const struct process* pcs,
420 const WCHAR* ImageName, DWORD64 base)
422 size_t len;
423 struct module* module;
424 PCWSTR filename, modname;
426 if (!base) return FALSE;
427 filename = get_filename(ImageName, NULL);
428 len = strlenW(filename);
430 for (module = pcs->lmodules; module; module = module->next)
432 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
433 base >= module->module.BaseOfImage &&
434 base < module->module.BaseOfImage + module->module.ImageSize)
436 modname = get_filename(module->module.LoadedImageName, NULL);
437 if (!strncmpiW(modname, filename, len) &&
438 !memcmp(modname + len, S_DotSoW, 3 * sizeof(WCHAR)))
440 return TRUE;
444 /* likely a native PE module */
445 WARN("Couldn't find container for %s\n", debugstr_w(ImageName));
446 return FALSE;
449 /******************************************************************
450 * module_get_type_by_name
452 * Guesses a filename type from its extension
454 enum module_type module_get_type_by_name(const WCHAR* name)
456 int loader_len, len = strlenW(name);
457 const WCHAR *loader;
459 /* Skip all version extensions (.[digits]) regex: "(\.\d+)*$" */
462 int i = len;
464 while (i && isdigit(name[i - 1])) i--;
466 if (i && name[i - 1] == '.')
467 len = i - 1;
468 else
469 break;
470 } while (len);
472 /* check for terminating .so or .so.[digit] */
473 /* FIXME: Can't rely solely on extension; have to check magic or
474 * stop using .so on Mac OS X. For now, base on platform. */
475 if (len > 3 && !memcmp(name + len - 3, S_DotSoW, 3))
476 #ifdef __APPLE__
477 return DMT_MACHO;
478 #else
479 return DMT_ELF;
480 #endif
482 if (len > 6 && !strncmpiW(name + len - 6, S_DotDylibW, 6))
483 return DMT_MACHO;
485 if (len > 4 && !strncmpiW(name + len - 4, S_DotPdbW, 4))
486 return DMT_PDB;
488 if (len > 4 && !strncmpiW(name + len - 4, S_DotDbgW, 4))
489 return DMT_DBG;
491 /* wine is also a native module (Mach-O on Mac OS X, ELF elsewhere) */
492 loader = get_wine_loader_name();
493 loader_len = strlenW( loader );
494 if ((len == loader_len || (len > loader_len && name[len - loader_len - 1] == '/')) &&
495 !strcmpiW(name + len - loader_len, loader))
497 #ifdef __APPLE__
498 return DMT_MACHO;
499 #else
500 return DMT_ELF;
501 #endif
503 return DMT_PE;
506 /******************************************************************
507 * refresh_module_list
509 static BOOL refresh_module_list(struct process* pcs)
511 /* force transparent ELF and Mach-O loading / unloading */
512 return elf_synchronize_module_list(pcs) || macho_synchronize_module_list(pcs);
515 /***********************************************************************
516 * SymLoadModule (DBGHELP.@)
518 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
519 PCSTR ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
521 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll,
522 SizeOfDll, NULL, 0);
525 /***********************************************************************
526 * SymLoadModuleEx (DBGHELP.@)
528 DWORD64 WINAPI SymLoadModuleEx(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
529 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD DllSize,
530 PMODLOAD_DATA Data, DWORD Flags)
532 PWSTR wImageName, wModuleName;
533 unsigned len;
534 DWORD64 ret;
536 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
537 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
538 wine_dbgstr_longlong(BaseOfDll), DllSize, Data, Flags);
540 if (ImageName)
542 len = MultiByteToWideChar(CP_ACP, 0, ImageName, -1, NULL, 0);
543 wImageName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
544 MultiByteToWideChar(CP_ACP, 0, ImageName, -1, wImageName, len);
546 else wImageName = NULL;
547 if (ModuleName)
549 len = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
550 wModuleName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
551 MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, wModuleName, len);
553 else wModuleName = NULL;
555 ret = SymLoadModuleExW(hProcess, hFile, wImageName, wModuleName,
556 BaseOfDll, DllSize, Data, Flags);
557 HeapFree(GetProcessHeap(), 0, wImageName);
558 HeapFree(GetProcessHeap(), 0, wModuleName);
559 return ret;
562 /***********************************************************************
563 * SymLoadModuleExW (DBGHELP.@)
565 DWORD64 WINAPI SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageName,
566 PCWSTR wModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll,
567 PMODLOAD_DATA Data, DWORD Flags)
569 struct process* pcs;
570 struct module* module = NULL;
572 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
573 hProcess, hFile, debugstr_w(wImageName), debugstr_w(wModuleName),
574 wine_dbgstr_longlong(BaseOfDll), SizeOfDll, Data, Flags);
576 if (Data)
577 FIXME("Unsupported load data parameter %p for %s\n",
578 Data, debugstr_w(wImageName));
579 if (!validate_addr64(BaseOfDll)) return FALSE;
581 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
583 if (Flags & SLMFLAG_VIRTUAL)
585 if (!wImageName) return FALSE;
586 module = module_new(pcs, wImageName, module_get_type_by_name(wImageName),
587 TRUE, BaseOfDll, SizeOfDll, 0, 0);
588 if (!module) return FALSE;
589 if (wModuleName) module_set_module(module, wModuleName);
590 module->module.SymType = SymVirtual;
592 return TRUE;
594 if (Flags & ~(SLMFLAG_VIRTUAL))
595 FIXME("Unsupported Flags %08x for %s\n", Flags, debugstr_w(wImageName));
597 refresh_module_list(pcs);
599 /* this is a Wine extension to the API just to redo the synchronisation */
600 if (!wImageName && !hFile) return 0;
602 /* check if the module is already loaded, or if it's a builtin PE module with
603 * an containing ELF module
605 if (wImageName)
607 module = module_is_already_loaded(pcs, wImageName);
608 if (!module && module_is_container_loaded(pcs, wImageName, BaseOfDll))
610 /* force the loading of DLL as builtin */
611 module = pe_load_builtin_module(pcs, wImageName, BaseOfDll, SizeOfDll);
614 if (!module)
616 /* otherwise, try a regular PE module */
617 if (!(module = pe_load_native_module(pcs, wImageName, hFile, BaseOfDll, SizeOfDll)) &&
618 wImageName)
620 /* and finally an ELF or Mach-O module */
621 switch (module_get_type_by_name(wImageName))
623 case DMT_ELF:
624 module = elf_load_module(pcs, wImageName, BaseOfDll);
625 break;
626 case DMT_MACHO:
627 module = macho_load_module(pcs, wImageName, BaseOfDll);
628 break;
629 default:
630 /* Ignored */
631 break;
635 if (!module)
637 WARN("Couldn't locate %s\n", debugstr_w(wImageName));
638 return 0;
640 module->module.NumSyms = module->ht_symbols.num_elts;
641 /* by default module_new fills module.ModuleName from a derivation
642 * of LoadedImageName. Overwrite it, if we have better information
644 if (wModuleName)
645 module_set_module(module, wModuleName);
646 if (wImageName)
647 lstrcpynW(module->module.ImageName, wImageName,
648 sizeof(module->module.ImageName) / sizeof(WCHAR));
650 return module->module.BaseOfImage;
653 /***********************************************************************
654 * SymLoadModule64 (DBGHELP.@)
656 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
657 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
659 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll, SizeOfDll,
660 NULL, 0);
663 /******************************************************************
664 * module_remove
667 BOOL module_remove(struct process* pcs, struct module* module)
669 struct module_format*modfmt;
670 struct module** p;
671 unsigned i;
673 TRACE("%s (%p)\n", debugstr_w(module->module.ModuleName), module);
675 for (i = 0; i < DFI_LAST; i++)
677 if ((modfmt = module->format_info[i]) && modfmt->remove)
678 modfmt->remove(pcs, module->format_info[i]);
680 hash_table_destroy(&module->ht_symbols);
681 hash_table_destroy(&module->ht_types);
682 wine_rb_destroy(&module->sources_offsets_tree, NULL, NULL);
683 HeapFree(GetProcessHeap(), 0, module->sources);
684 HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
685 pool_destroy(&module->pool);
686 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
687 * so do we
689 for (p = &pcs->lmodules; *p; p = &(*p)->next)
691 if (*p == module)
693 *p = module->next;
694 HeapFree(GetProcessHeap(), 0, module);
695 return TRUE;
698 FIXME("This shouldn't happen\n");
699 return FALSE;
702 /******************************************************************
703 * SymUnloadModule (DBGHELP.@)
706 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
708 struct process* pcs;
709 struct module* module;
711 pcs = process_find_by_handle(hProcess);
712 if (!pcs) return FALSE;
713 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
714 if (!module) return FALSE;
715 return module_remove(pcs, module);
718 /******************************************************************
719 * SymUnloadModule64 (DBGHELP.@)
722 BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
724 struct process* pcs;
725 struct module* module;
727 pcs = process_find_by_handle(hProcess);
728 if (!pcs) return FALSE;
729 if (!validate_addr64(BaseOfDll)) return FALSE;
730 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
731 if (!module) return FALSE;
732 return module_remove(pcs, module);
735 /******************************************************************
736 * SymEnumerateModules (DBGHELP.@)
739 struct enum_modW64_32
741 PSYM_ENUMMODULES_CALLBACK cb;
742 PVOID user;
743 char module[MAX_PATH];
746 static BOOL CALLBACK enum_modW64_32(PCWSTR name, DWORD64 base, PVOID user)
748 struct enum_modW64_32* x = user;
750 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
751 return x->cb(x->module, (DWORD)base, x->user);
754 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
755 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
756 PVOID UserContext)
758 struct enum_modW64_32 x;
760 x.cb = EnumModulesCallback;
761 x.user = UserContext;
763 return SymEnumerateModulesW64(hProcess, enum_modW64_32, &x);
766 /******************************************************************
767 * SymEnumerateModules64 (DBGHELP.@)
770 struct enum_modW64_64
772 PSYM_ENUMMODULES_CALLBACK64 cb;
773 PVOID user;
774 char module[MAX_PATH];
777 static BOOL CALLBACK enum_modW64_64(PCWSTR name, DWORD64 base, PVOID user)
779 struct enum_modW64_64* x = user;
781 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
782 return x->cb(x->module, base, x->user);
785 BOOL WINAPI SymEnumerateModules64(HANDLE hProcess,
786 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,
787 PVOID UserContext)
789 struct enum_modW64_64 x;
791 x.cb = EnumModulesCallback;
792 x.user = UserContext;
794 return SymEnumerateModulesW64(hProcess, enum_modW64_64, &x);
797 /******************************************************************
798 * SymEnumerateModulesW64 (DBGHELP.@)
801 BOOL WINAPI SymEnumerateModulesW64(HANDLE hProcess,
802 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback,
803 PVOID UserContext)
805 struct process* pcs = process_find_by_handle(hProcess);
806 struct module* module;
808 if (!pcs) return FALSE;
810 for (module = pcs->lmodules; module; module = module->next)
812 if (!(dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES) &&
813 (module->type == DMT_ELF || module->type == DMT_MACHO))
814 continue;
815 if (!EnumModulesCallback(module->module.ModuleName,
816 module->module.BaseOfImage, UserContext))
817 break;
819 return TRUE;
822 /******************************************************************
823 * EnumerateLoadedModules64 (DBGHELP.@)
826 struct enum_load_modW64_64
828 PENUMLOADED_MODULES_CALLBACK64 cb;
829 PVOID user;
830 char module[MAX_PATH];
833 static BOOL CALLBACK enum_load_modW64_64(PCWSTR name, DWORD64 base, ULONG size,
834 PVOID user)
836 struct enum_load_modW64_64* x = user;
838 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
839 return x->cb(x->module, base, size, x->user);
842 BOOL WINAPI EnumerateLoadedModules64(HANDLE hProcess,
843 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
844 PVOID UserContext)
846 struct enum_load_modW64_64 x;
848 x.cb = EnumLoadedModulesCallback;
849 x.user = UserContext;
851 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_64, &x);
854 /******************************************************************
855 * EnumerateLoadedModules (DBGHELP.@)
858 struct enum_load_modW64_32
860 PENUMLOADED_MODULES_CALLBACK cb;
861 PVOID user;
862 char module[MAX_PATH];
865 static BOOL CALLBACK enum_load_modW64_32(PCWSTR name, DWORD64 base, ULONG size,
866 PVOID user)
868 struct enum_load_modW64_32* x = user;
869 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
870 return x->cb(x->module, (DWORD)base, size, x->user);
873 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
874 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
875 PVOID UserContext)
877 struct enum_load_modW64_32 x;
879 x.cb = EnumLoadedModulesCallback;
880 x.user = UserContext;
882 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_32, &x);
885 /******************************************************************
886 * EnumerateLoadedModulesW64 (DBGHELP.@)
889 BOOL WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
890 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback,
891 PVOID UserContext)
893 HMODULE* hMods;
894 WCHAR baseW[256], modW[256];
895 DWORD i, sz;
896 MODULEINFO mi;
898 hMods = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods[0]));
899 if (!hMods) return FALSE;
901 if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
903 /* hProcess should also be a valid process handle !! */
904 FIXME("If this happens, bump the number in mod\n");
905 HeapFree(GetProcessHeap(), 0, hMods);
906 return FALSE;
908 sz /= sizeof(HMODULE);
909 for (i = 0; i < sz; i++)
911 if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
912 !GetModuleBaseNameW(hProcess, hMods[i], baseW, sizeof(baseW) / sizeof(WCHAR)))
913 continue;
914 module_fill_module(baseW, modW, sizeof(modW) / sizeof(CHAR));
915 EnumLoadedModulesCallback(modW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage,
916 UserContext);
918 HeapFree(GetProcessHeap(), 0, hMods);
920 return sz != 0 && i == sz;
923 /******************************************************************
924 * SymGetModuleInfo (DBGHELP.@)
927 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
928 PIMAGEHLP_MODULE ModuleInfo)
930 IMAGEHLP_MODULE mi;
931 IMAGEHLP_MODULEW64 miw64;
933 if (sizeof(mi) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
935 miw64.SizeOfStruct = sizeof(miw64);
936 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
938 mi.SizeOfStruct = miw64.SizeOfStruct;
939 mi.BaseOfImage = miw64.BaseOfImage;
940 mi.ImageSize = miw64.ImageSize;
941 mi.TimeDateStamp = miw64.TimeDateStamp;
942 mi.CheckSum = miw64.CheckSum;
943 mi.NumSyms = miw64.NumSyms;
944 mi.SymType = miw64.SymType;
945 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
946 mi.ModuleName, sizeof(mi.ModuleName), NULL, NULL);
947 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
948 mi.ImageName, sizeof(mi.ImageName), NULL, NULL);
949 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
950 mi.LoadedImageName, sizeof(mi.LoadedImageName), NULL, NULL);
952 memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
954 return TRUE;
957 /******************************************************************
958 * SymGetModuleInfoW (DBGHELP.@)
961 BOOL WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
962 PIMAGEHLP_MODULEW ModuleInfo)
964 IMAGEHLP_MODULEW64 miw64;
965 IMAGEHLP_MODULEW miw;
967 if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
969 miw64.SizeOfStruct = sizeof(miw64);
970 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
972 miw.SizeOfStruct = miw64.SizeOfStruct;
973 miw.BaseOfImage = miw64.BaseOfImage;
974 miw.ImageSize = miw64.ImageSize;
975 miw.TimeDateStamp = miw64.TimeDateStamp;
976 miw.CheckSum = miw64.CheckSum;
977 miw.NumSyms = miw64.NumSyms;
978 miw.SymType = miw64.SymType;
979 strcpyW(miw.ModuleName, miw64.ModuleName);
980 strcpyW(miw.ImageName, miw64.ImageName);
981 strcpyW(miw.LoadedImageName, miw64.LoadedImageName);
982 memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
984 return TRUE;
987 /******************************************************************
988 * SymGetModuleInfo64 (DBGHELP.@)
991 BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
992 PIMAGEHLP_MODULE64 ModuleInfo)
994 IMAGEHLP_MODULE64 mi64;
995 IMAGEHLP_MODULEW64 miw64;
997 if (sizeof(mi64) < ModuleInfo->SizeOfStruct)
999 SetLastError(ERROR_MOD_NOT_FOUND); /* NOTE: native returns this error */
1000 WARN("Wrong size %u\n", ModuleInfo->SizeOfStruct);
1001 return FALSE;
1004 miw64.SizeOfStruct = sizeof(miw64);
1005 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
1007 mi64.SizeOfStruct = miw64.SizeOfStruct;
1008 mi64.BaseOfImage = miw64.BaseOfImage;
1009 mi64.ImageSize = miw64.ImageSize;
1010 mi64.TimeDateStamp = miw64.TimeDateStamp;
1011 mi64.CheckSum = miw64.CheckSum;
1012 mi64.NumSyms = miw64.NumSyms;
1013 mi64.SymType = miw64.SymType;
1014 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
1015 mi64.ModuleName, sizeof(mi64.ModuleName), NULL, NULL);
1016 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
1017 mi64.ImageName, sizeof(mi64.ImageName), NULL, NULL);
1018 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
1019 mi64.LoadedImageName, sizeof(mi64.LoadedImageName), NULL, NULL);
1020 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedPdbName, -1,
1021 mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName), NULL, NULL);
1023 mi64.CVSig = miw64.CVSig;
1024 WideCharToMultiByte(CP_ACP, 0, miw64.CVData, -1,
1025 mi64.CVData, sizeof(mi64.CVData), NULL, NULL);
1026 mi64.PdbSig = miw64.PdbSig;
1027 mi64.PdbSig70 = miw64.PdbSig70;
1028 mi64.PdbAge = miw64.PdbAge;
1029 mi64.PdbUnmatched = miw64.PdbUnmatched;
1030 mi64.DbgUnmatched = miw64.DbgUnmatched;
1031 mi64.LineNumbers = miw64.LineNumbers;
1032 mi64.GlobalSymbols = miw64.GlobalSymbols;
1033 mi64.TypeInfo = miw64.TypeInfo;
1034 mi64.SourceIndexed = miw64.SourceIndexed;
1035 mi64.Publics = miw64.Publics;
1037 memcpy(ModuleInfo, &mi64, ModuleInfo->SizeOfStruct);
1039 return TRUE;
1042 /******************************************************************
1043 * SymGetModuleInfoW64 (DBGHELP.@)
1046 BOOL WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr,
1047 PIMAGEHLP_MODULEW64 ModuleInfo)
1049 struct process* pcs = process_find_by_handle(hProcess);
1050 struct module* module;
1051 IMAGEHLP_MODULEW64 miw64;
1053 TRACE("%p %s %p\n", hProcess, wine_dbgstr_longlong(dwAddr), ModuleInfo);
1055 if (!pcs) return FALSE;
1056 if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
1057 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1058 if (!module) return FALSE;
1060 miw64 = module->module;
1062 /* update debug information from container if any */
1063 if (module->module.SymType == SymNone)
1065 module = module_get_container(pcs, module);
1066 if (module && module->module.SymType != SymNone)
1068 miw64.SymType = module->module.SymType;
1069 miw64.NumSyms = module->module.NumSyms;
1072 memcpy(ModuleInfo, &miw64, ModuleInfo->SizeOfStruct);
1073 return TRUE;
1076 /***********************************************************************
1077 * SymGetModuleBase (DBGHELP.@)
1079 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
1081 DWORD64 ret;
1083 ret = SymGetModuleBase64(hProcess, dwAddr);
1084 return validate_addr64(ret) ? ret : 0;
1087 /***********************************************************************
1088 * SymGetModuleBase64 (DBGHELP.@)
1090 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
1092 struct process* pcs = process_find_by_handle(hProcess);
1093 struct module* module;
1095 if (!pcs) return 0;
1096 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1097 if (!module) return 0;
1098 return module->module.BaseOfImage;
1101 /******************************************************************
1102 * module_reset_debug_info
1103 * Removes any debug information linked to a given module.
1105 void module_reset_debug_info(struct module* module)
1107 module->sortlist_valid = TRUE;
1108 module->sorttab_size = 0;
1109 module->addr_sorttab = NULL;
1110 module->num_sorttab = module->num_symbols = 0;
1111 hash_table_destroy(&module->ht_symbols);
1112 module->ht_symbols.num_buckets = 0;
1113 module->ht_symbols.buckets = NULL;
1114 hash_table_destroy(&module->ht_types);
1115 module->ht_types.num_buckets = 0;
1116 module->ht_types.buckets = NULL;
1117 module->vtypes.num_elts = 0;
1118 hash_table_destroy(&module->ht_symbols);
1119 module->sources_used = module->sources_alloc = 0;
1120 module->sources = NULL;
1123 /******************************************************************
1124 * SymRefreshModuleList (DBGHELP.@)
1126 BOOL WINAPI SymRefreshModuleList(HANDLE hProcess)
1128 struct process* pcs;
1130 TRACE("(%p)\n", hProcess);
1132 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1134 return refresh_module_list(pcs);
1137 /***********************************************************************
1138 * SymFunctionTableAccess (DBGHELP.@)
1140 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1142 return SymFunctionTableAccess64(hProcess, AddrBase);
1145 /***********************************************************************
1146 * SymFunctionTableAccess64 (DBGHELP.@)
1148 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1150 struct process* pcs = process_find_by_handle(hProcess);
1151 struct module* module;
1153 if (!pcs || !dbghelp_current_cpu->find_runtime_function) return NULL;
1154 module = module_find_by_addr(pcs, AddrBase, DMT_UNKNOWN);
1155 if (!module) return NULL;
1157 return dbghelp_current_cpu->find_runtime_function(module, AddrBase);