d3d8/tests: Make the window client rect match the d3d swapchain size.
[wine.git] / dlls / dbghelp / module.c
blob9df02dfa05fac0fbc991cc7baeb73783d55410f6
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_compare);
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);
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 HeapFree(GetProcessHeap(), 0, module->sources);
683 HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
684 pool_destroy(&module->pool);
685 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
686 * so do we
688 for (p = &pcs->lmodules; *p; p = &(*p)->next)
690 if (*p == module)
692 *p = module->next;
693 HeapFree(GetProcessHeap(), 0, module);
694 return TRUE;
697 FIXME("This shouldn't happen\n");
698 return FALSE;
701 /******************************************************************
702 * SymUnloadModule (DBGHELP.@)
705 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
707 struct process* pcs;
708 struct module* module;
710 pcs = process_find_by_handle(hProcess);
711 if (!pcs) return FALSE;
712 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
713 if (!module) return FALSE;
714 return module_remove(pcs, module);
717 /******************************************************************
718 * SymUnloadModule64 (DBGHELP.@)
721 BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
723 struct process* pcs;
724 struct module* module;
726 pcs = process_find_by_handle(hProcess);
727 if (!pcs) return FALSE;
728 if (!validate_addr64(BaseOfDll)) return FALSE;
729 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
730 if (!module) return FALSE;
731 return module_remove(pcs, module);
734 /******************************************************************
735 * SymEnumerateModules (DBGHELP.@)
738 struct enum_modW64_32
740 PSYM_ENUMMODULES_CALLBACK cb;
741 PVOID user;
742 char module[MAX_PATH];
745 static BOOL CALLBACK enum_modW64_32(PCWSTR name, DWORD64 base, PVOID user)
747 struct enum_modW64_32* x = user;
749 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
750 return x->cb(x->module, (DWORD)base, x->user);
753 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
754 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
755 PVOID UserContext)
757 struct enum_modW64_32 x;
759 x.cb = EnumModulesCallback;
760 x.user = UserContext;
762 return SymEnumerateModulesW64(hProcess, enum_modW64_32, &x);
765 /******************************************************************
766 * SymEnumerateModules64 (DBGHELP.@)
769 struct enum_modW64_64
771 PSYM_ENUMMODULES_CALLBACK64 cb;
772 PVOID user;
773 char module[MAX_PATH];
776 static BOOL CALLBACK enum_modW64_64(PCWSTR name, DWORD64 base, PVOID user)
778 struct enum_modW64_64* x = user;
780 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
781 return x->cb(x->module, base, x->user);
784 BOOL WINAPI SymEnumerateModules64(HANDLE hProcess,
785 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,
786 PVOID UserContext)
788 struct enum_modW64_64 x;
790 x.cb = EnumModulesCallback;
791 x.user = UserContext;
793 return SymEnumerateModulesW64(hProcess, enum_modW64_64, &x);
796 /******************************************************************
797 * SymEnumerateModulesW64 (DBGHELP.@)
800 BOOL WINAPI SymEnumerateModulesW64(HANDLE hProcess,
801 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback,
802 PVOID UserContext)
804 struct process* pcs = process_find_by_handle(hProcess);
805 struct module* module;
807 if (!pcs) return FALSE;
809 for (module = pcs->lmodules; module; module = module->next)
811 if (!(dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES) &&
812 (module->type == DMT_ELF || module->type == DMT_MACHO))
813 continue;
814 if (!EnumModulesCallback(module->module.ModuleName,
815 module->module.BaseOfImage, UserContext))
816 break;
818 return TRUE;
821 /******************************************************************
822 * EnumerateLoadedModules64 (DBGHELP.@)
825 struct enum_load_modW64_64
827 PENUMLOADED_MODULES_CALLBACK64 cb;
828 PVOID user;
829 char module[MAX_PATH];
832 static BOOL CALLBACK enum_load_modW64_64(PCWSTR name, DWORD64 base, ULONG size,
833 PVOID user)
835 struct enum_load_modW64_64* x = user;
837 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
838 return x->cb(x->module, base, size, x->user);
841 BOOL WINAPI EnumerateLoadedModules64(HANDLE hProcess,
842 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
843 PVOID UserContext)
845 struct enum_load_modW64_64 x;
847 x.cb = EnumLoadedModulesCallback;
848 x.user = UserContext;
850 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_64, &x);
853 /******************************************************************
854 * EnumerateLoadedModules (DBGHELP.@)
857 struct enum_load_modW64_32
859 PENUMLOADED_MODULES_CALLBACK cb;
860 PVOID user;
861 char module[MAX_PATH];
864 static BOOL CALLBACK enum_load_modW64_32(PCWSTR name, DWORD64 base, ULONG size,
865 PVOID user)
867 struct enum_load_modW64_32* x = user;
868 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
869 return x->cb(x->module, (DWORD)base, size, x->user);
872 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
873 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
874 PVOID UserContext)
876 struct enum_load_modW64_32 x;
878 x.cb = EnumLoadedModulesCallback;
879 x.user = UserContext;
881 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_32, &x);
884 /******************************************************************
885 * EnumerateLoadedModulesW64 (DBGHELP.@)
888 BOOL WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
889 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback,
890 PVOID UserContext)
892 HMODULE* hMods;
893 WCHAR baseW[256], modW[256];
894 DWORD i, sz;
895 MODULEINFO mi;
897 hMods = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods[0]));
898 if (!hMods) return FALSE;
900 if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
902 /* hProcess should also be a valid process handle !! */
903 FIXME("If this happens, bump the number in mod\n");
904 HeapFree(GetProcessHeap(), 0, hMods);
905 return FALSE;
907 sz /= sizeof(HMODULE);
908 for (i = 0; i < sz; i++)
910 if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
911 !GetModuleBaseNameW(hProcess, hMods[i], baseW, sizeof(baseW) / sizeof(WCHAR)))
912 continue;
913 module_fill_module(baseW, modW, sizeof(modW) / sizeof(CHAR));
914 EnumLoadedModulesCallback(modW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage,
915 UserContext);
917 HeapFree(GetProcessHeap(), 0, hMods);
919 return sz != 0 && i == sz;
922 /******************************************************************
923 * SymGetModuleInfo (DBGHELP.@)
926 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
927 PIMAGEHLP_MODULE ModuleInfo)
929 IMAGEHLP_MODULE mi;
930 IMAGEHLP_MODULEW64 miw64;
932 if (sizeof(mi) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
934 miw64.SizeOfStruct = sizeof(miw64);
935 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
937 mi.SizeOfStruct = miw64.SizeOfStruct;
938 mi.BaseOfImage = miw64.BaseOfImage;
939 mi.ImageSize = miw64.ImageSize;
940 mi.TimeDateStamp = miw64.TimeDateStamp;
941 mi.CheckSum = miw64.CheckSum;
942 mi.NumSyms = miw64.NumSyms;
943 mi.SymType = miw64.SymType;
944 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
945 mi.ModuleName, sizeof(mi.ModuleName), NULL, NULL);
946 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
947 mi.ImageName, sizeof(mi.ImageName), NULL, NULL);
948 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
949 mi.LoadedImageName, sizeof(mi.LoadedImageName), NULL, NULL);
951 memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
953 return TRUE;
956 /******************************************************************
957 * SymGetModuleInfoW (DBGHELP.@)
960 BOOL WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
961 PIMAGEHLP_MODULEW ModuleInfo)
963 IMAGEHLP_MODULEW64 miw64;
964 IMAGEHLP_MODULEW miw;
966 if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
968 miw64.SizeOfStruct = sizeof(miw64);
969 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
971 miw.SizeOfStruct = miw64.SizeOfStruct;
972 miw.BaseOfImage = miw64.BaseOfImage;
973 miw.ImageSize = miw64.ImageSize;
974 miw.TimeDateStamp = miw64.TimeDateStamp;
975 miw.CheckSum = miw64.CheckSum;
976 miw.NumSyms = miw64.NumSyms;
977 miw.SymType = miw64.SymType;
978 strcpyW(miw.ModuleName, miw64.ModuleName);
979 strcpyW(miw.ImageName, miw64.ImageName);
980 strcpyW(miw.LoadedImageName, miw64.LoadedImageName);
981 memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
983 return TRUE;
986 /******************************************************************
987 * SymGetModuleInfo64 (DBGHELP.@)
990 BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
991 PIMAGEHLP_MODULE64 ModuleInfo)
993 IMAGEHLP_MODULE64 mi64;
994 IMAGEHLP_MODULEW64 miw64;
996 if (sizeof(mi64) < ModuleInfo->SizeOfStruct)
998 SetLastError(ERROR_MOD_NOT_FOUND); /* NOTE: native returns this error */
999 WARN("Wrong size %u\n", ModuleInfo->SizeOfStruct);
1000 return FALSE;
1003 miw64.SizeOfStruct = sizeof(miw64);
1004 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
1006 mi64.SizeOfStruct = miw64.SizeOfStruct;
1007 mi64.BaseOfImage = miw64.BaseOfImage;
1008 mi64.ImageSize = miw64.ImageSize;
1009 mi64.TimeDateStamp = miw64.TimeDateStamp;
1010 mi64.CheckSum = miw64.CheckSum;
1011 mi64.NumSyms = miw64.NumSyms;
1012 mi64.SymType = miw64.SymType;
1013 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
1014 mi64.ModuleName, sizeof(mi64.ModuleName), NULL, NULL);
1015 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
1016 mi64.ImageName, sizeof(mi64.ImageName), NULL, NULL);
1017 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
1018 mi64.LoadedImageName, sizeof(mi64.LoadedImageName), NULL, NULL);
1019 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedPdbName, -1,
1020 mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName), NULL, NULL);
1022 mi64.CVSig = miw64.CVSig;
1023 WideCharToMultiByte(CP_ACP, 0, miw64.CVData, -1,
1024 mi64.CVData, sizeof(mi64.CVData), NULL, NULL);
1025 mi64.PdbSig = miw64.PdbSig;
1026 mi64.PdbSig70 = miw64.PdbSig70;
1027 mi64.PdbAge = miw64.PdbAge;
1028 mi64.PdbUnmatched = miw64.PdbUnmatched;
1029 mi64.DbgUnmatched = miw64.DbgUnmatched;
1030 mi64.LineNumbers = miw64.LineNumbers;
1031 mi64.GlobalSymbols = miw64.GlobalSymbols;
1032 mi64.TypeInfo = miw64.TypeInfo;
1033 mi64.SourceIndexed = miw64.SourceIndexed;
1034 mi64.Publics = miw64.Publics;
1036 memcpy(ModuleInfo, &mi64, ModuleInfo->SizeOfStruct);
1038 return TRUE;
1041 /******************************************************************
1042 * SymGetModuleInfoW64 (DBGHELP.@)
1045 BOOL WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr,
1046 PIMAGEHLP_MODULEW64 ModuleInfo)
1048 struct process* pcs = process_find_by_handle(hProcess);
1049 struct module* module;
1050 IMAGEHLP_MODULEW64 miw64;
1052 TRACE("%p %s %p\n", hProcess, wine_dbgstr_longlong(dwAddr), ModuleInfo);
1054 if (!pcs) return FALSE;
1055 if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
1056 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1057 if (!module) return FALSE;
1059 miw64 = module->module;
1061 /* update debug information from container if any */
1062 if (module->module.SymType == SymNone)
1064 module = module_get_container(pcs, module);
1065 if (module && module->module.SymType != SymNone)
1067 miw64.SymType = module->module.SymType;
1068 miw64.NumSyms = module->module.NumSyms;
1071 memcpy(ModuleInfo, &miw64, ModuleInfo->SizeOfStruct);
1072 return TRUE;
1075 /***********************************************************************
1076 * SymGetModuleBase (DBGHELP.@)
1078 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
1080 DWORD64 ret;
1082 ret = SymGetModuleBase64(hProcess, dwAddr);
1083 return validate_addr64(ret) ? ret : 0;
1086 /***********************************************************************
1087 * SymGetModuleBase64 (DBGHELP.@)
1089 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
1091 struct process* pcs = process_find_by_handle(hProcess);
1092 struct module* module;
1094 if (!pcs) return 0;
1095 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1096 if (!module) return 0;
1097 return module->module.BaseOfImage;
1100 /******************************************************************
1101 * module_reset_debug_info
1102 * Removes any debug information linked to a given module.
1104 void module_reset_debug_info(struct module* module)
1106 module->sortlist_valid = TRUE;
1107 module->sorttab_size = 0;
1108 module->addr_sorttab = NULL;
1109 module->num_sorttab = module->num_symbols = 0;
1110 hash_table_destroy(&module->ht_symbols);
1111 module->ht_symbols.num_buckets = 0;
1112 module->ht_symbols.buckets = NULL;
1113 hash_table_destroy(&module->ht_types);
1114 module->ht_types.num_buckets = 0;
1115 module->ht_types.buckets = NULL;
1116 module->vtypes.num_elts = 0;
1117 hash_table_destroy(&module->ht_symbols);
1118 module->sources_used = module->sources_alloc = 0;
1119 module->sources = NULL;
1122 /******************************************************************
1123 * SymRefreshModuleList (DBGHELP.@)
1125 BOOL WINAPI SymRefreshModuleList(HANDLE hProcess)
1127 struct process* pcs;
1129 TRACE("(%p)\n", hProcess);
1131 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1133 return refresh_module_list(pcs);
1136 /***********************************************************************
1137 * SymFunctionTableAccess (DBGHELP.@)
1139 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1141 return SymFunctionTableAccess64(hProcess, AddrBase);
1144 /***********************************************************************
1145 * SymFunctionTableAccess64 (DBGHELP.@)
1147 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1149 struct process* pcs = process_find_by_handle(hProcess);
1150 struct module* module;
1152 if (!pcs || !dbghelp_current_cpu->find_runtime_function) return NULL;
1153 module = module_find_by_addr(pcs, AddrBase, DMT_UNKNOWN);
1154 if (!module) return NULL;
1156 return dbghelp_current_cpu->find_runtime_function(module, AddrBase);