wined3d: Use a separate STATE_VDECL state handler in the GLSL pipeline.
[wine/multimedia.git] / dlls / dbghelp / module.c
blob15769e8cef752825b52ee1c3f88940e818757ece
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 0;
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 BOOL 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;
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;
425 static WCHAR* dll_prefix;
426 static int dll_prefix_len;
428 if (!dll_prefix)
430 dll_prefix_len = MultiByteToWideChar( CP_UNIXCP, 0, DLLPREFIX, -1, NULL, 0 );
431 dll_prefix = HeapAlloc( GetProcessHeap(), 0, dll_prefix_len * sizeof(WCHAR) );
432 MultiByteToWideChar( CP_UNIXCP, 0, DLLPREFIX, -1, dll_prefix, dll_prefix_len );
433 dll_prefix_len--;
436 if (!base) return FALSE;
437 filename = get_filename(ImageName, NULL);
438 len = strlenW(filename);
440 for (module = pcs->lmodules; module; module = module->next)
442 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
443 base >= module->module.BaseOfImage &&
444 base < module->module.BaseOfImage + module->module.ImageSize)
446 modname = get_filename(module->module.LoadedImageName, NULL);
447 if (dll_prefix_len && !strncmpW( modname, dll_prefix, dll_prefix_len )) modname += dll_prefix_len;
448 if (!strncmpiW(modname, filename, len) &&
449 !memcmp(modname + len, S_DotSoW, 3 * sizeof(WCHAR)))
451 return TRUE;
455 /* likely a native PE module */
456 WARN("Couldn't find container for %s\n", debugstr_w(ImageName));
457 return FALSE;
460 /******************************************************************
461 * module_get_type_by_name
463 * Guesses a filename type from its extension
465 enum module_type module_get_type_by_name(const WCHAR* name)
467 int loader_len, len = strlenW(name);
468 const WCHAR *loader;
470 /* Skip all version extensions (.[digits]) regex: "(\.\d+)*$" */
473 int i = len;
475 while (i && isdigit(name[i - 1])) i--;
477 if (i && name[i - 1] == '.')
478 len = i - 1;
479 else
480 break;
481 } while (len);
483 /* check for terminating .so or .so.[digit] */
484 /* FIXME: Can't rely solely on extension; have to check magic or
485 * stop using .so on Mac OS X. For now, base on platform. */
486 if (len > 3 && !memcmp(name + len - 3, S_DotSoW, 3))
487 #ifdef __APPLE__
488 return DMT_MACHO;
489 #else
490 return DMT_ELF;
491 #endif
493 if (len > 6 && !strncmpiW(name + len - 6, S_DotDylibW, 6))
494 return DMT_MACHO;
496 if (len > 4 && !strncmpiW(name + len - 4, S_DotPdbW, 4))
497 return DMT_PDB;
499 if (len > 4 && !strncmpiW(name + len - 4, S_DotDbgW, 4))
500 return DMT_DBG;
502 /* wine is also a native module (Mach-O on Mac OS X, ELF elsewhere) */
503 loader = get_wine_loader_name();
504 loader_len = strlenW( loader );
505 if ((len == loader_len || (len > loader_len && name[len - loader_len - 1] == '/')) &&
506 !strcmpiW(name + len - loader_len, loader))
508 #ifdef __APPLE__
509 return DMT_MACHO;
510 #else
511 return DMT_ELF;
512 #endif
514 return DMT_PE;
517 /******************************************************************
518 * refresh_module_list
520 static BOOL refresh_module_list(struct process* pcs)
522 /* force transparent ELF and Mach-O loading / unloading */
523 return elf_synchronize_module_list(pcs) || macho_synchronize_module_list(pcs);
526 /***********************************************************************
527 * SymLoadModule (DBGHELP.@)
529 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
530 PCSTR ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
532 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll,
533 SizeOfDll, NULL, 0);
536 /***********************************************************************
537 * SymLoadModuleEx (DBGHELP.@)
539 DWORD64 WINAPI SymLoadModuleEx(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
540 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD DllSize,
541 PMODLOAD_DATA Data, DWORD Flags)
543 PWSTR wImageName, wModuleName;
544 unsigned len;
545 DWORD64 ret;
547 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
548 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
549 wine_dbgstr_longlong(BaseOfDll), DllSize, Data, Flags);
551 if (ImageName)
553 len = MultiByteToWideChar(CP_ACP, 0, ImageName, -1, NULL, 0);
554 wImageName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
555 MultiByteToWideChar(CP_ACP, 0, ImageName, -1, wImageName, len);
557 else wImageName = NULL;
558 if (ModuleName)
560 len = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
561 wModuleName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
562 MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, wModuleName, len);
564 else wModuleName = NULL;
566 ret = SymLoadModuleExW(hProcess, hFile, wImageName, wModuleName,
567 BaseOfDll, DllSize, Data, Flags);
568 HeapFree(GetProcessHeap(), 0, wImageName);
569 HeapFree(GetProcessHeap(), 0, wModuleName);
570 return ret;
573 /***********************************************************************
574 * SymLoadModuleExW (DBGHELP.@)
576 DWORD64 WINAPI SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageName,
577 PCWSTR wModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll,
578 PMODLOAD_DATA Data, DWORD Flags)
580 struct process* pcs;
581 struct module* module = NULL;
583 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
584 hProcess, hFile, debugstr_w(wImageName), debugstr_w(wModuleName),
585 wine_dbgstr_longlong(BaseOfDll), SizeOfDll, Data, Flags);
587 if (Data)
588 FIXME("Unsupported load data parameter %p for %s\n",
589 Data, debugstr_w(wImageName));
590 if (!validate_addr64(BaseOfDll)) return FALSE;
592 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
594 if (Flags & SLMFLAG_VIRTUAL)
596 if (!wImageName) return FALSE;
597 module = module_new(pcs, wImageName, module_get_type_by_name(wImageName),
598 TRUE, BaseOfDll, SizeOfDll, 0, 0);
599 if (!module) return FALSE;
600 if (wModuleName) module_set_module(module, wModuleName);
601 module->module.SymType = SymVirtual;
603 return TRUE;
605 if (Flags & ~(SLMFLAG_VIRTUAL))
606 FIXME("Unsupported Flags %08x for %s\n", Flags, debugstr_w(wImageName));
608 refresh_module_list(pcs);
610 /* this is a Wine extension to the API just to redo the synchronisation */
611 if (!wImageName && !hFile) return 0;
613 /* check if the module is already loaded, or if it's a builtin PE module with
614 * an containing ELF module
616 if (wImageName)
618 module = module_is_already_loaded(pcs, wImageName);
619 if (!module && module_is_container_loaded(pcs, wImageName, BaseOfDll))
621 /* force the loading of DLL as builtin */
622 module = pe_load_builtin_module(pcs, wImageName, BaseOfDll, SizeOfDll);
625 if (!module)
627 /* otherwise, try a regular PE module */
628 if (!(module = pe_load_native_module(pcs, wImageName, hFile, BaseOfDll, SizeOfDll)) &&
629 wImageName)
631 /* and finally an ELF or Mach-O module */
632 switch (module_get_type_by_name(wImageName))
634 case DMT_ELF:
635 module = elf_load_module(pcs, wImageName, BaseOfDll);
636 break;
637 case DMT_MACHO:
638 module = macho_load_module(pcs, wImageName, BaseOfDll);
639 break;
640 default:
641 /* Ignored */
642 break;
646 if (!module)
648 WARN("Couldn't locate %s\n", debugstr_w(wImageName));
649 return 0;
651 module->module.NumSyms = module->ht_symbols.num_elts;
652 /* by default module_new fills module.ModuleName from a derivation
653 * of LoadedImageName. Overwrite it, if we have better information
655 if (wModuleName)
656 module_set_module(module, wModuleName);
657 if (wImageName)
658 lstrcpynW(module->module.ImageName, wImageName,
659 sizeof(module->module.ImageName) / sizeof(WCHAR));
661 return module->module.BaseOfImage;
664 /***********************************************************************
665 * SymLoadModule64 (DBGHELP.@)
667 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
668 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
670 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll, SizeOfDll,
671 NULL, 0);
674 /******************************************************************
675 * module_remove
678 BOOL module_remove(struct process* pcs, struct module* module)
680 struct module_format*modfmt;
681 struct module** p;
682 unsigned i;
684 TRACE("%s (%p)\n", debugstr_w(module->module.ModuleName), module);
686 for (i = 0; i < DFI_LAST; i++)
688 if ((modfmt = module->format_info[i]) && modfmt->remove)
689 modfmt->remove(pcs, module->format_info[i]);
691 hash_table_destroy(&module->ht_symbols);
692 hash_table_destroy(&module->ht_types);
693 wine_rb_destroy(&module->sources_offsets_tree, NULL, NULL);
694 HeapFree(GetProcessHeap(), 0, module->sources);
695 HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
696 pool_destroy(&module->pool);
697 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
698 * so do we
700 for (p = &pcs->lmodules; *p; p = &(*p)->next)
702 if (*p == module)
704 *p = module->next;
705 HeapFree(GetProcessHeap(), 0, module);
706 return TRUE;
709 FIXME("This shouldn't happen\n");
710 return FALSE;
713 /******************************************************************
714 * SymUnloadModule (DBGHELP.@)
717 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
719 struct process* pcs;
720 struct module* module;
722 pcs = process_find_by_handle(hProcess);
723 if (!pcs) return FALSE;
724 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
725 if (!module) return FALSE;
726 return module_remove(pcs, module);
729 /******************************************************************
730 * SymUnloadModule64 (DBGHELP.@)
733 BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
735 struct process* pcs;
736 struct module* module;
738 pcs = process_find_by_handle(hProcess);
739 if (!pcs) return FALSE;
740 if (!validate_addr64(BaseOfDll)) return FALSE;
741 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
742 if (!module) return FALSE;
743 return module_remove(pcs, module);
746 /******************************************************************
747 * SymEnumerateModules (DBGHELP.@)
750 struct enum_modW64_32
752 PSYM_ENUMMODULES_CALLBACK cb;
753 PVOID user;
754 char module[MAX_PATH];
757 static BOOL CALLBACK enum_modW64_32(PCWSTR name, DWORD64 base, PVOID user)
759 struct enum_modW64_32* x = user;
761 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
762 return x->cb(x->module, (DWORD)base, x->user);
765 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
766 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
767 PVOID UserContext)
769 struct enum_modW64_32 x;
771 x.cb = EnumModulesCallback;
772 x.user = UserContext;
774 return SymEnumerateModulesW64(hProcess, enum_modW64_32, &x);
777 /******************************************************************
778 * SymEnumerateModules64 (DBGHELP.@)
781 struct enum_modW64_64
783 PSYM_ENUMMODULES_CALLBACK64 cb;
784 PVOID user;
785 char module[MAX_PATH];
788 static BOOL CALLBACK enum_modW64_64(PCWSTR name, DWORD64 base, PVOID user)
790 struct enum_modW64_64* x = user;
792 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
793 return x->cb(x->module, base, x->user);
796 BOOL WINAPI SymEnumerateModules64(HANDLE hProcess,
797 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,
798 PVOID UserContext)
800 struct enum_modW64_64 x;
802 x.cb = EnumModulesCallback;
803 x.user = UserContext;
805 return SymEnumerateModulesW64(hProcess, enum_modW64_64, &x);
808 /******************************************************************
809 * SymEnumerateModulesW64 (DBGHELP.@)
812 BOOL WINAPI SymEnumerateModulesW64(HANDLE hProcess,
813 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback,
814 PVOID UserContext)
816 struct process* pcs = process_find_by_handle(hProcess);
817 struct module* module;
819 if (!pcs) return FALSE;
821 for (module = pcs->lmodules; module; module = module->next)
823 if (!(dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES) &&
824 (module->type == DMT_ELF || module->type == DMT_MACHO))
825 continue;
826 if (!EnumModulesCallback(module->module.ModuleName,
827 module->module.BaseOfImage, UserContext))
828 break;
830 return TRUE;
833 /******************************************************************
834 * EnumerateLoadedModules64 (DBGHELP.@)
837 struct enum_load_modW64_64
839 PENUMLOADED_MODULES_CALLBACK64 cb;
840 PVOID user;
841 char module[MAX_PATH];
844 static BOOL CALLBACK enum_load_modW64_64(PCWSTR name, DWORD64 base, ULONG size,
845 PVOID user)
847 struct enum_load_modW64_64* x = user;
849 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
850 return x->cb(x->module, base, size, x->user);
853 BOOL WINAPI EnumerateLoadedModules64(HANDLE hProcess,
854 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
855 PVOID UserContext)
857 struct enum_load_modW64_64 x;
859 x.cb = EnumLoadedModulesCallback;
860 x.user = UserContext;
862 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_64, &x);
865 /******************************************************************
866 * EnumerateLoadedModules (DBGHELP.@)
869 struct enum_load_modW64_32
871 PENUMLOADED_MODULES_CALLBACK cb;
872 PVOID user;
873 char module[MAX_PATH];
876 static BOOL CALLBACK enum_load_modW64_32(PCWSTR name, DWORD64 base, ULONG size,
877 PVOID user)
879 struct enum_load_modW64_32* x = user;
880 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
881 return x->cb(x->module, (DWORD)base, size, x->user);
884 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
885 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
886 PVOID UserContext)
888 struct enum_load_modW64_32 x;
890 x.cb = EnumLoadedModulesCallback;
891 x.user = UserContext;
893 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_32, &x);
896 /******************************************************************
897 * EnumerateLoadedModulesW64 (DBGHELP.@)
900 BOOL WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
901 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback,
902 PVOID UserContext)
904 HMODULE* hMods;
905 WCHAR baseW[256], modW[256];
906 DWORD i, sz;
907 MODULEINFO mi;
909 hMods = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods[0]));
910 if (!hMods) return FALSE;
912 if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
914 /* hProcess should also be a valid process handle !! */
915 FIXME("If this happens, bump the number in mod\n");
916 HeapFree(GetProcessHeap(), 0, hMods);
917 return FALSE;
919 sz /= sizeof(HMODULE);
920 for (i = 0; i < sz; i++)
922 if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
923 !GetModuleBaseNameW(hProcess, hMods[i], baseW, sizeof(baseW) / sizeof(WCHAR)))
924 continue;
925 module_fill_module(baseW, modW, sizeof(modW) / sizeof(CHAR));
926 EnumLoadedModulesCallback(modW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage,
927 UserContext);
929 HeapFree(GetProcessHeap(), 0, hMods);
931 return sz != 0 && i == sz;
934 /******************************************************************
935 * SymGetModuleInfo (DBGHELP.@)
938 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
939 PIMAGEHLP_MODULE ModuleInfo)
941 IMAGEHLP_MODULE mi;
942 IMAGEHLP_MODULEW64 miw64;
944 if (sizeof(mi) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
946 miw64.SizeOfStruct = sizeof(miw64);
947 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
949 mi.SizeOfStruct = miw64.SizeOfStruct;
950 mi.BaseOfImage = miw64.BaseOfImage;
951 mi.ImageSize = miw64.ImageSize;
952 mi.TimeDateStamp = miw64.TimeDateStamp;
953 mi.CheckSum = miw64.CheckSum;
954 mi.NumSyms = miw64.NumSyms;
955 mi.SymType = miw64.SymType;
956 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
957 mi.ModuleName, sizeof(mi.ModuleName), NULL, NULL);
958 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
959 mi.ImageName, sizeof(mi.ImageName), NULL, NULL);
960 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
961 mi.LoadedImageName, sizeof(mi.LoadedImageName), NULL, NULL);
963 memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
965 return TRUE;
968 /******************************************************************
969 * SymGetModuleInfoW (DBGHELP.@)
972 BOOL WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
973 PIMAGEHLP_MODULEW ModuleInfo)
975 IMAGEHLP_MODULEW64 miw64;
976 IMAGEHLP_MODULEW miw;
978 if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
980 miw64.SizeOfStruct = sizeof(miw64);
981 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
983 miw.SizeOfStruct = miw64.SizeOfStruct;
984 miw.BaseOfImage = miw64.BaseOfImage;
985 miw.ImageSize = miw64.ImageSize;
986 miw.TimeDateStamp = miw64.TimeDateStamp;
987 miw.CheckSum = miw64.CheckSum;
988 miw.NumSyms = miw64.NumSyms;
989 miw.SymType = miw64.SymType;
990 strcpyW(miw.ModuleName, miw64.ModuleName);
991 strcpyW(miw.ImageName, miw64.ImageName);
992 strcpyW(miw.LoadedImageName, miw64.LoadedImageName);
993 memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
995 return TRUE;
998 /******************************************************************
999 * SymGetModuleInfo64 (DBGHELP.@)
1002 BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
1003 PIMAGEHLP_MODULE64 ModuleInfo)
1005 IMAGEHLP_MODULE64 mi64;
1006 IMAGEHLP_MODULEW64 miw64;
1008 if (sizeof(mi64) < ModuleInfo->SizeOfStruct)
1010 SetLastError(ERROR_MOD_NOT_FOUND); /* NOTE: native returns this error */
1011 WARN("Wrong size %u\n", ModuleInfo->SizeOfStruct);
1012 return FALSE;
1015 miw64.SizeOfStruct = sizeof(miw64);
1016 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
1018 mi64.SizeOfStruct = miw64.SizeOfStruct;
1019 mi64.BaseOfImage = miw64.BaseOfImage;
1020 mi64.ImageSize = miw64.ImageSize;
1021 mi64.TimeDateStamp = miw64.TimeDateStamp;
1022 mi64.CheckSum = miw64.CheckSum;
1023 mi64.NumSyms = miw64.NumSyms;
1024 mi64.SymType = miw64.SymType;
1025 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
1026 mi64.ModuleName, sizeof(mi64.ModuleName), NULL, NULL);
1027 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
1028 mi64.ImageName, sizeof(mi64.ImageName), NULL, NULL);
1029 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
1030 mi64.LoadedImageName, sizeof(mi64.LoadedImageName), NULL, NULL);
1031 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedPdbName, -1,
1032 mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName), NULL, NULL);
1034 mi64.CVSig = miw64.CVSig;
1035 WideCharToMultiByte(CP_ACP, 0, miw64.CVData, -1,
1036 mi64.CVData, sizeof(mi64.CVData), NULL, NULL);
1037 mi64.PdbSig = miw64.PdbSig;
1038 mi64.PdbSig70 = miw64.PdbSig70;
1039 mi64.PdbAge = miw64.PdbAge;
1040 mi64.PdbUnmatched = miw64.PdbUnmatched;
1041 mi64.DbgUnmatched = miw64.DbgUnmatched;
1042 mi64.LineNumbers = miw64.LineNumbers;
1043 mi64.GlobalSymbols = miw64.GlobalSymbols;
1044 mi64.TypeInfo = miw64.TypeInfo;
1045 mi64.SourceIndexed = miw64.SourceIndexed;
1046 mi64.Publics = miw64.Publics;
1048 memcpy(ModuleInfo, &mi64, ModuleInfo->SizeOfStruct);
1050 return TRUE;
1053 /******************************************************************
1054 * SymGetModuleInfoW64 (DBGHELP.@)
1057 BOOL WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr,
1058 PIMAGEHLP_MODULEW64 ModuleInfo)
1060 struct process* pcs = process_find_by_handle(hProcess);
1061 struct module* module;
1062 IMAGEHLP_MODULEW64 miw64;
1064 TRACE("%p %s %p\n", hProcess, wine_dbgstr_longlong(dwAddr), ModuleInfo);
1066 if (!pcs) return FALSE;
1067 if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
1068 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1069 if (!module) return FALSE;
1071 miw64 = module->module;
1073 /* update debug information from container if any */
1074 if (module->module.SymType == SymNone)
1076 module = module_get_container(pcs, module);
1077 if (module && module->module.SymType != SymNone)
1079 miw64.SymType = module->module.SymType;
1080 miw64.NumSyms = module->module.NumSyms;
1083 memcpy(ModuleInfo, &miw64, ModuleInfo->SizeOfStruct);
1084 return TRUE;
1087 /***********************************************************************
1088 * SymGetModuleBase (DBGHELP.@)
1090 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
1092 DWORD64 ret;
1094 ret = SymGetModuleBase64(hProcess, dwAddr);
1095 return validate_addr64(ret) ? ret : 0;
1098 /***********************************************************************
1099 * SymGetModuleBase64 (DBGHELP.@)
1101 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
1103 struct process* pcs = process_find_by_handle(hProcess);
1104 struct module* module;
1106 if (!pcs) return 0;
1107 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1108 if (!module) return 0;
1109 return module->module.BaseOfImage;
1112 /******************************************************************
1113 * module_reset_debug_info
1114 * Removes any debug information linked to a given module.
1116 void module_reset_debug_info(struct module* module)
1118 module->sortlist_valid = TRUE;
1119 module->sorttab_size = 0;
1120 module->addr_sorttab = NULL;
1121 module->num_sorttab = module->num_symbols = 0;
1122 hash_table_destroy(&module->ht_symbols);
1123 module->ht_symbols.num_buckets = 0;
1124 module->ht_symbols.buckets = NULL;
1125 hash_table_destroy(&module->ht_types);
1126 module->ht_types.num_buckets = 0;
1127 module->ht_types.buckets = NULL;
1128 module->vtypes.num_elts = 0;
1129 hash_table_destroy(&module->ht_symbols);
1130 module->sources_used = module->sources_alloc = 0;
1131 module->sources = NULL;
1134 /******************************************************************
1135 * SymRefreshModuleList (DBGHELP.@)
1137 BOOL WINAPI SymRefreshModuleList(HANDLE hProcess)
1139 struct process* pcs;
1141 TRACE("(%p)\n", hProcess);
1143 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1145 return refresh_module_list(pcs);
1148 /***********************************************************************
1149 * SymFunctionTableAccess (DBGHELP.@)
1151 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1153 return SymFunctionTableAccess64(hProcess, AddrBase);
1156 /***********************************************************************
1157 * SymFunctionTableAccess64 (DBGHELP.@)
1159 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1161 struct process* pcs = process_find_by_handle(hProcess);
1162 struct module* module;
1164 if (!pcs || !dbghelp_current_cpu->find_runtime_function) return NULL;
1165 module = module_find_by_addr(pcs, AddrBase, DMT_UNKNOWN);
1166 if (!module) return NULL;
1168 return dbghelp_current_cpu->find_runtime_function(module, AddrBase);