oleaut32: Remove unnecessary consts.
[wine.git] / dlls / dbghelp / module.c
blob98fc10c0292ae874f3df767a5f9f2eb6a1ad7326
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"
32 #include "wine/heap.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
36 const WCHAR S_ElfW[] = {'<','e','l','f','>','\0'};
37 const WCHAR S_WineLoaderW[] = {'<','w','i','n','e','-','l','o','a','d','e','r','>','\0'};
38 static const WCHAR S_DotSoW[] = {'.','s','o','\0'};
39 static const WCHAR S_DotDylibW[] = {'.','d','y','l','i','b','\0'};
40 static const WCHAR S_DotPdbW[] = {'.','p','d','b','\0'};
41 static const WCHAR S_DotDbgW[] = {'.','d','b','g','\0'};
42 const WCHAR S_SlashW[] = {'/','\0'};
44 static const WCHAR S_AcmW[] = {'.','a','c','m','\0'};
45 static const WCHAR S_DllW[] = {'.','d','l','l','\0'};
46 static const WCHAR S_DrvW[] = {'.','d','r','v','\0'};
47 static const WCHAR S_ExeW[] = {'.','e','x','e','\0'};
48 static const WCHAR S_OcxW[] = {'.','o','c','x','\0'};
49 static const WCHAR S_VxdW[] = {'.','v','x','d','\0'};
50 static const WCHAR * const ext[] = {S_AcmW, S_DllW, S_DrvW, S_ExeW, S_OcxW, S_VxdW, NULL};
52 static int match_ext(const WCHAR* ptr, size_t len)
54 const WCHAR* const *e;
55 size_t l;
57 for (e = ext; *e; e++)
59 l = strlenW(*e);
60 if (l >= len) return 0;
61 if (strncmpiW(&ptr[len - l], *e, l)) continue;
62 return l;
64 return 0;
67 static const WCHAR* get_filename(const WCHAR* name, const WCHAR* endptr)
69 const WCHAR* ptr;
71 if (!endptr) endptr = name + strlenW(name);
72 for (ptr = endptr - 1; ptr >= name; ptr--)
74 if (*ptr == '/' || *ptr == '\\') break;
76 return ++ptr;
79 static BOOL is_wine_loader(const WCHAR *module)
81 static const WCHAR wineW[] = {'w','i','n','e',0};
82 static const WCHAR suffixW[] = {'6','4',0};
83 const WCHAR *filename = get_filename(module, NULL);
84 const char *ptr, *p;
85 BOOL ret = FALSE;
86 WCHAR *buffer;
87 DWORD len;
89 if ((ptr = getenv("WINELOADER")))
91 if ((p = strrchr(ptr, '/'))) ptr = p + 1;
92 len = 2 + MultiByteToWideChar( CP_UNIXCP, 0, ptr, -1, NULL, 0 );
93 buffer = heap_alloc( len * sizeof(WCHAR) );
94 MultiByteToWideChar( CP_UNIXCP, 0, ptr, -1, buffer, len );
96 else
98 buffer = heap_alloc( sizeof(wineW) + 2 * sizeof(WCHAR) );
99 strcpyW( buffer, wineW );
102 if (!strcmpW( filename, buffer ))
103 ret = TRUE;
105 strcatW( buffer, suffixW );
106 if (!strcmpW( filename, buffer ))
107 ret = TRUE;
109 heap_free( buffer );
110 return ret;
113 static void module_fill_module(const WCHAR* in, WCHAR* out, size_t size)
115 const WCHAR *ptr, *endptr;
116 size_t len, l;
118 ptr = get_filename(in, endptr = in + strlenW(in));
119 len = min(endptr - ptr, size - 1);
120 memcpy(out, ptr, len * sizeof(WCHAR));
121 out[len] = '\0';
122 if (len > 4 && (l = match_ext(out, len)))
123 out[len - l] = '\0';
124 else if (is_wine_loader(out))
125 lstrcpynW(out, S_WineLoaderW, size);
126 else
128 if (len > 3 && !strcmpiW(&out[len - 3], S_DotSoW) &&
129 (l = match_ext(out, len - 3)))
130 strcpyW(&out[len - l - 3], S_ElfW);
132 while ((*out = tolowerW(*out))) out++;
135 void module_set_module(struct module* module, const WCHAR* name)
137 module_fill_module(name, module->module.ModuleName, ARRAY_SIZE(module->module.ModuleName));
138 module_fill_module(name, module->modulename, ARRAY_SIZE(module->modulename));
141 /* Returned string must be freed by caller */
142 WCHAR *get_wine_loader_name(struct process *pcs)
144 static const WCHAR wineW[] = {'w','i','n','e',0};
145 static const WCHAR suffixW[] = {'6','4',0};
146 WCHAR *buffer, *p;
147 const char *env;
149 /* All binaries are loaded with WINELOADER (if run from tree) or by the
150 * main executable
152 if ((env = getenv("WINELOADER")))
154 DWORD len = 2 + MultiByteToWideChar( CP_UNIXCP, 0, env, -1, NULL, 0 );
155 buffer = heap_alloc( len * sizeof(WCHAR) );
156 MultiByteToWideChar( CP_UNIXCP, 0, env, -1, buffer, len );
158 else
160 buffer = heap_alloc( sizeof(wineW) + 2 * sizeof(WCHAR) );
161 strcpyW( buffer, wineW );
164 p = buffer + strlenW( buffer ) - strlenW( suffixW );
165 if (p > buffer && !strcmpW( p, suffixW ))
166 *p = 0;
168 if (pcs->is_64bit)
169 strcatW(buffer, suffixW);
171 TRACE( "returning %s\n", debugstr_w(buffer) );
172 return buffer;
175 static const char* get_module_type(enum module_type type, BOOL virtual)
177 switch (type)
179 case DMT_ELF: return virtual ? "Virtual ELF" : "ELF";
180 case DMT_PE: return virtual ? "Virtual PE" : "PE";
181 case DMT_MACHO: return virtual ? "Virtual Mach-O" : "Mach-O";
182 default: return "---";
186 /***********************************************************************
187 * Creates and links a new module to a process
189 struct module* module_new(struct process* pcs, const WCHAR* name,
190 enum module_type type, BOOL virtual,
191 DWORD64 mod_addr, DWORD64 size,
192 unsigned long stamp, unsigned long checksum)
194 struct module* module;
195 unsigned i;
197 assert(type == DMT_ELF || type == DMT_PE || type == DMT_MACHO);
198 if (!(module = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*module))))
199 return NULL;
201 module->next = pcs->lmodules;
202 pcs->lmodules = module;
204 TRACE("=> %s %s-%s %s\n",
205 get_module_type(type, virtual),
206 wine_dbgstr_longlong(mod_addr), wine_dbgstr_longlong(mod_addr + size),
207 debugstr_w(name));
209 pool_init(&module->pool, 65536);
211 module->process = pcs;
212 module->module.SizeOfStruct = sizeof(module->module);
213 module->module.BaseOfImage = mod_addr;
214 module->module.ImageSize = size;
215 module_set_module(module, name);
216 module->module.ImageName[0] = '\0';
217 lstrcpynW(module->module.LoadedImageName, name, ARRAY_SIZE(module->module.LoadedImageName));
218 module->module.SymType = SymNone;
219 module->module.NumSyms = 0;
220 module->module.TimeDateStamp = stamp;
221 module->module.CheckSum = checksum;
223 memset(module->module.LoadedPdbName, 0, sizeof(module->module.LoadedPdbName));
224 module->module.CVSig = 0;
225 memset(module->module.CVData, 0, sizeof(module->module.CVData));
226 module->module.PdbSig = 0;
227 memset(&module->module.PdbSig70, 0, sizeof(module->module.PdbSig70));
228 module->module.PdbAge = 0;
229 module->module.PdbUnmatched = FALSE;
230 module->module.DbgUnmatched = FALSE;
231 module->module.LineNumbers = FALSE;
232 module->module.GlobalSymbols = FALSE;
233 module->module.TypeInfo = FALSE;
234 module->module.SourceIndexed = FALSE;
235 module->module.Publics = FALSE;
237 module->reloc_delta = 0;
238 module->type = type;
239 module->is_virtual = virtual;
240 for (i = 0; i < DFI_LAST; i++) module->format_info[i] = NULL;
241 module->sortlist_valid = FALSE;
242 module->sorttab_size = 0;
243 module->addr_sorttab = NULL;
244 module->num_sorttab = 0;
245 module->num_symbols = 0;
247 vector_init(&module->vsymt, sizeof(struct symt*), 128);
248 /* FIXME: this seems a bit too high (on a per module basis)
249 * need some statistics about this
251 hash_table_init(&module->pool, &module->ht_symbols, 4096);
252 hash_table_init(&module->pool, &module->ht_types, 4096);
253 vector_init(&module->vtypes, sizeof(struct symt*), 32);
255 module->sources_used = 0;
256 module->sources_alloc = 0;
257 module->sources = 0;
258 wine_rb_init(&module->sources_offsets_tree, source_rb_compare);
260 return module;
263 /***********************************************************************
264 * module_find_by_nameW
267 struct module* module_find_by_nameW(const struct process* pcs, const WCHAR* name)
269 struct module* module;
271 for (module = pcs->lmodules; module; module = module->next)
273 if (!strcmpiW(name, module->module.ModuleName)) return module;
275 SetLastError(ERROR_INVALID_NAME);
276 return NULL;
279 struct module* module_find_by_nameA(const struct process* pcs, const char* name)
281 WCHAR wname[MAX_PATH];
283 MultiByteToWideChar(CP_ACP, 0, name, -1, wname, ARRAY_SIZE(wname));
284 return module_find_by_nameW(pcs, wname);
287 /***********************************************************************
288 * module_is_already_loaded
291 struct module* module_is_already_loaded(const struct process* pcs, const WCHAR* name)
293 struct module* module;
294 const WCHAR* filename;
296 /* first compare the loaded image name... */
297 for (module = pcs->lmodules; module; module = module->next)
299 if (!strcmpiW(name, module->module.LoadedImageName))
300 return module;
302 /* then compare the standard filenames (without the path) ... */
303 filename = get_filename(name, NULL);
304 for (module = pcs->lmodules; module; module = module->next)
306 if (!strcmpiW(filename, get_filename(module->module.LoadedImageName, NULL)))
307 return module;
309 SetLastError(ERROR_INVALID_NAME);
310 return NULL;
313 /***********************************************************************
314 * module_get_container
317 static struct module* module_get_container(const struct process* pcs,
318 const struct module* inner)
320 struct module* module;
322 for (module = pcs->lmodules; module; module = module->next)
324 if (module != inner &&
325 module->module.BaseOfImage <= inner->module.BaseOfImage &&
326 module->module.BaseOfImage + module->module.ImageSize >=
327 inner->module.BaseOfImage + inner->module.ImageSize)
328 return module;
330 return NULL;
333 /***********************************************************************
334 * module_get_containee
337 struct module* module_get_containee(const struct process* pcs,
338 const struct module* outter)
340 struct module* module;
342 for (module = pcs->lmodules; module; module = module->next)
344 if (module != outter &&
345 outter->module.BaseOfImage <= module->module.BaseOfImage &&
346 outter->module.BaseOfImage + outter->module.ImageSize >=
347 module->module.BaseOfImage + module->module.ImageSize)
348 return module;
350 return NULL;
353 /******************************************************************
354 * module_get_debug
356 * get the debug information from a module:
357 * - if the module's type is deferred, then force loading of debug info (and return
358 * the module itself)
359 * - if the module has no debug info and has an ELF container, then return the ELF
360 * container (and also force the ELF container's debug info loading if deferred)
361 * - otherwise return the module itself if it has some debug info
363 BOOL module_get_debug(struct module_pair* pair)
365 IMAGEHLP_DEFERRED_SYMBOL_LOADW64 idslW64;
367 if (!pair->requested) return FALSE;
368 /* for a PE builtin, always get info from container */
369 if (!(pair->effective = module_get_container(pair->pcs, pair->requested)))
370 pair->effective = pair->requested;
371 /* if deferred, force loading */
372 if (pair->effective->module.SymType == SymDeferred)
374 BOOL ret;
376 if (pair->effective->is_virtual) ret = FALSE;
377 else switch (pair->effective->type)
379 case DMT_ELF:
380 ret = elf_load_debug_info(pair->effective);
381 break;
382 case DMT_PE:
383 idslW64.SizeOfStruct = sizeof(idslW64);
384 idslW64.BaseOfImage = pair->effective->module.BaseOfImage;
385 idslW64.CheckSum = pair->effective->module.CheckSum;
386 idslW64.TimeDateStamp = pair->effective->module.TimeDateStamp;
387 memcpy(idslW64.FileName, pair->effective->module.ImageName,
388 sizeof(pair->effective->module.ImageName));
389 idslW64.Reparse = FALSE;
390 idslW64.hFile = INVALID_HANDLE_VALUE;
392 pcs_callback(pair->pcs, CBA_DEFERRED_SYMBOL_LOAD_START, &idslW64);
393 ret = pe_load_debug_info(pair->pcs, pair->effective);
394 pcs_callback(pair->pcs,
395 ret ? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE : CBA_DEFERRED_SYMBOL_LOAD_FAILURE,
396 &idslW64);
397 break;
398 case DMT_MACHO:
399 ret = macho_load_debug_info(pair->pcs, pair->effective);
400 break;
401 default:
402 ret = FALSE;
403 break;
405 if (!ret) pair->effective->module.SymType = SymNone;
406 assert(pair->effective->module.SymType != SymDeferred);
407 pair->effective->module.NumSyms = pair->effective->ht_symbols.num_elts;
409 return pair->effective->module.SymType != SymNone;
412 /***********************************************************************
413 * module_find_by_addr
415 * either the addr where module is loaded, or any address inside the
416 * module
418 struct module* module_find_by_addr(const struct process* pcs, DWORD64 addr,
419 enum module_type type)
421 struct module* module;
423 if (type == DMT_UNKNOWN)
425 if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
426 (module = module_find_by_addr(pcs, addr, DMT_ELF)) ||
427 (module = module_find_by_addr(pcs, addr, DMT_MACHO)))
428 return module;
430 else
432 for (module = pcs->lmodules; module; module = module->next)
434 if (type == module->type && addr >= module->module.BaseOfImage &&
435 addr < module->module.BaseOfImage + module->module.ImageSize)
436 return module;
439 SetLastError(ERROR_INVALID_ADDRESS);
440 return module;
443 /******************************************************************
444 * module_is_container_loaded
446 * checks whether the native container, for a (supposed) PE builtin is
447 * already loaded
449 static BOOL module_is_container_loaded(const struct process* pcs,
450 const WCHAR* ImageName, DWORD64 base)
452 size_t len;
453 struct module* module;
454 PCWSTR filename, modname;
456 if (!base) return FALSE;
457 filename = get_filename(ImageName, NULL);
458 len = strlenW(filename);
460 for (module = pcs->lmodules; module; module = module->next)
462 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
463 base >= module->module.BaseOfImage &&
464 base < module->module.BaseOfImage + module->module.ImageSize)
466 modname = get_filename(module->module.LoadedImageName, NULL);
467 if (!strncmpiW(modname, filename, len) &&
468 !memcmp(modname + len, S_DotSoW, 3 * sizeof(WCHAR)))
470 return TRUE;
474 /* likely a native PE module */
475 WARN("Couldn't find container for %s\n", debugstr_w(ImageName));
476 return FALSE;
479 /******************************************************************
480 * module_get_type_by_name
482 * Guesses a filename type from its extension
484 enum module_type module_get_type_by_name(const WCHAR* name)
486 int len = strlenW(name);
488 /* Skip all version extensions (.[digits]) regex: "(\.\d+)*$" */
491 int i = len;
493 while (i && name[i - 1] >= '0' && name[i - 1] <= '9') i--;
495 if (i && name[i - 1] == '.')
496 len = i - 1;
497 else
498 break;
499 } while (len);
501 /* check for terminating .so or .so.[digit] */
502 /* FIXME: Can't rely solely on extension; have to check magic or
503 * stop using .so on Mac OS X. For now, base on platform. */
504 if (len > 3 && !memcmp(name + len - 3, S_DotSoW, 3))
505 #ifdef __APPLE__
506 return DMT_MACHO;
507 #else
508 return DMT_ELF;
509 #endif
511 if (len > 6 && !strncmpiW(name + len - 6, S_DotDylibW, 6))
512 return DMT_MACHO;
514 if (len > 4 && !strncmpiW(name + len - 4, S_DotPdbW, 4))
515 return DMT_PDB;
517 if (len > 4 && !strncmpiW(name + len - 4, S_DotDbgW, 4))
518 return DMT_DBG;
520 /* wine is also a native module (Mach-O on Mac OS X, ELF elsewhere) */
521 if (is_wine_loader(name))
523 #ifdef __APPLE__
524 return DMT_MACHO;
525 #else
526 return DMT_ELF;
527 #endif
529 return DMT_PE;
532 /******************************************************************
533 * refresh_module_list
535 static BOOL refresh_module_list(struct process* pcs)
537 /* force transparent ELF and Mach-O loading / unloading */
538 return elf_synchronize_module_list(pcs) || macho_synchronize_module_list(pcs);
541 /***********************************************************************
542 * SymLoadModule (DBGHELP.@)
544 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
545 PCSTR ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
547 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll,
548 SizeOfDll, NULL, 0);
551 /***********************************************************************
552 * SymLoadModuleEx (DBGHELP.@)
554 DWORD64 WINAPI SymLoadModuleEx(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
555 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD DllSize,
556 PMODLOAD_DATA Data, DWORD Flags)
558 PWSTR wImageName, wModuleName;
559 unsigned len;
560 DWORD64 ret;
562 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
563 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
564 wine_dbgstr_longlong(BaseOfDll), DllSize, Data, Flags);
566 if (ImageName)
568 len = MultiByteToWideChar(CP_ACP, 0, ImageName, -1, NULL, 0);
569 wImageName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
570 MultiByteToWideChar(CP_ACP, 0, ImageName, -1, wImageName, len);
572 else wImageName = NULL;
573 if (ModuleName)
575 len = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
576 wModuleName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
577 MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, wModuleName, len);
579 else wModuleName = NULL;
581 ret = SymLoadModuleExW(hProcess, hFile, wImageName, wModuleName,
582 BaseOfDll, DllSize, Data, Flags);
583 HeapFree(GetProcessHeap(), 0, wImageName);
584 HeapFree(GetProcessHeap(), 0, wModuleName);
585 return ret;
588 /***********************************************************************
589 * SymLoadModuleExW (DBGHELP.@)
591 DWORD64 WINAPI SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageName,
592 PCWSTR wModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll,
593 PMODLOAD_DATA Data, DWORD Flags)
595 struct process* pcs;
596 struct module* module = NULL;
598 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
599 hProcess, hFile, debugstr_w(wImageName), debugstr_w(wModuleName),
600 wine_dbgstr_longlong(BaseOfDll), SizeOfDll, Data, Flags);
602 if (Data)
603 FIXME("Unsupported load data parameter %p for %s\n",
604 Data, debugstr_w(wImageName));
605 if (!validate_addr64(BaseOfDll)) return FALSE;
607 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
609 if (Flags & SLMFLAG_VIRTUAL)
611 if (!wImageName) return FALSE;
612 module = module_new(pcs, wImageName, module_get_type_by_name(wImageName),
613 TRUE, BaseOfDll, SizeOfDll, 0, 0);
614 if (!module) return FALSE;
615 if (wModuleName) module_set_module(module, wModuleName);
616 module->module.SymType = SymVirtual;
618 return TRUE;
620 if (Flags & ~(SLMFLAG_VIRTUAL))
621 FIXME("Unsupported Flags %08x for %s\n", Flags, debugstr_w(wImageName));
623 refresh_module_list(pcs);
625 /* this is a Wine extension to the API just to redo the synchronisation */
626 if (!wImageName && !hFile) return 0;
628 /* check if the module is already loaded, or if it's a builtin PE module with
629 * an containing ELF module
631 if (wImageName)
633 module = module_is_already_loaded(pcs, wImageName);
634 if (!module && module_is_container_loaded(pcs, wImageName, BaseOfDll))
636 /* force the loading of DLL as builtin */
637 module = pe_load_builtin_module(pcs, wImageName, BaseOfDll, SizeOfDll);
640 if (!module)
642 /* otherwise, try a regular PE module */
643 if (!(module = pe_load_native_module(pcs, wImageName, hFile, BaseOfDll, SizeOfDll)) &&
644 wImageName)
646 /* and finally an ELF or Mach-O module */
647 switch (module_get_type_by_name(wImageName))
649 case DMT_ELF:
650 module = elf_load_module(pcs, wImageName, BaseOfDll);
651 break;
652 case DMT_MACHO:
653 module = macho_load_module(pcs, wImageName, BaseOfDll);
654 break;
655 default:
656 /* Ignored */
657 break;
661 if (!module)
663 WARN("Couldn't locate %s\n", debugstr_w(wImageName));
664 return 0;
666 module->module.NumSyms = module->ht_symbols.num_elts;
667 /* by default module_new fills module.ModuleName from a derivation
668 * of LoadedImageName. Overwrite it, if we have better information
670 if (wModuleName)
671 module_set_module(module, wModuleName);
672 if (wImageName)
673 lstrcpynW(module->module.ImageName, wImageName, ARRAY_SIZE(module->module.ImageName));
675 return module->module.BaseOfImage;
678 /***********************************************************************
679 * SymLoadModule64 (DBGHELP.@)
681 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
682 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
684 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll, SizeOfDll,
685 NULL, 0);
688 /******************************************************************
689 * module_remove
692 BOOL module_remove(struct process* pcs, struct module* module)
694 struct module_format*modfmt;
695 struct module** p;
696 unsigned i;
698 TRACE("%s (%p)\n", debugstr_w(module->module.ModuleName), module);
700 for (i = 0; i < DFI_LAST; i++)
702 if ((modfmt = module->format_info[i]) && modfmt->remove)
703 modfmt->remove(pcs, module->format_info[i]);
705 hash_table_destroy(&module->ht_symbols);
706 hash_table_destroy(&module->ht_types);
707 HeapFree(GetProcessHeap(), 0, module->sources);
708 HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
709 pool_destroy(&module->pool);
710 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
711 * so do we
713 for (p = &pcs->lmodules; *p; p = &(*p)->next)
715 if (*p == module)
717 *p = module->next;
718 HeapFree(GetProcessHeap(), 0, module);
719 return TRUE;
722 FIXME("This shouldn't happen\n");
723 return FALSE;
726 /******************************************************************
727 * SymUnloadModule (DBGHELP.@)
730 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
732 struct process* pcs;
733 struct module* module;
735 pcs = process_find_by_handle(hProcess);
736 if (!pcs) return FALSE;
737 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
738 if (!module) return FALSE;
739 return module_remove(pcs, module);
742 /******************************************************************
743 * SymUnloadModule64 (DBGHELP.@)
746 BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
748 struct process* pcs;
749 struct module* module;
751 pcs = process_find_by_handle(hProcess);
752 if (!pcs) return FALSE;
753 if (!validate_addr64(BaseOfDll)) return FALSE;
754 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
755 if (!module) return FALSE;
756 return module_remove(pcs, module);
759 /******************************************************************
760 * SymEnumerateModules (DBGHELP.@)
763 struct enum_modW64_32
765 PSYM_ENUMMODULES_CALLBACK cb;
766 PVOID user;
767 char module[MAX_PATH];
770 static BOOL CALLBACK enum_modW64_32(PCWSTR name, DWORD64 base, PVOID user)
772 struct enum_modW64_32* x = user;
774 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
775 return x->cb(x->module, (DWORD)base, x->user);
778 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
779 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
780 PVOID UserContext)
782 struct enum_modW64_32 x;
784 x.cb = EnumModulesCallback;
785 x.user = UserContext;
787 return SymEnumerateModulesW64(hProcess, enum_modW64_32, &x);
790 /******************************************************************
791 * SymEnumerateModules64 (DBGHELP.@)
794 struct enum_modW64_64
796 PSYM_ENUMMODULES_CALLBACK64 cb;
797 PVOID user;
798 char module[MAX_PATH];
801 static BOOL CALLBACK enum_modW64_64(PCWSTR name, DWORD64 base, PVOID user)
803 struct enum_modW64_64* x = user;
805 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
806 return x->cb(x->module, base, x->user);
809 BOOL WINAPI SymEnumerateModules64(HANDLE hProcess,
810 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,
811 PVOID UserContext)
813 struct enum_modW64_64 x;
815 x.cb = EnumModulesCallback;
816 x.user = UserContext;
818 return SymEnumerateModulesW64(hProcess, enum_modW64_64, &x);
821 /******************************************************************
822 * SymEnumerateModulesW64 (DBGHELP.@)
825 BOOL WINAPI SymEnumerateModulesW64(HANDLE hProcess,
826 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback,
827 PVOID UserContext)
829 struct process* pcs = process_find_by_handle(hProcess);
830 struct module* module;
832 if (!pcs) return FALSE;
834 for (module = pcs->lmodules; module; module = module->next)
836 if (!(dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES) &&
837 (module->type == DMT_ELF || module->type == DMT_MACHO))
838 continue;
839 if (!EnumModulesCallback(module->modulename,
840 module->module.BaseOfImage, UserContext))
841 break;
843 return TRUE;
846 /******************************************************************
847 * EnumerateLoadedModules64 (DBGHELP.@)
850 struct enum_load_modW64_64
852 PENUMLOADED_MODULES_CALLBACK64 cb;
853 PVOID user;
854 char module[MAX_PATH];
857 static BOOL CALLBACK enum_load_modW64_64(PCWSTR name, DWORD64 base, ULONG size,
858 PVOID user)
860 struct enum_load_modW64_64* x = user;
862 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
863 return x->cb(x->module, base, size, x->user);
866 BOOL WINAPI EnumerateLoadedModules64(HANDLE hProcess,
867 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
868 PVOID UserContext)
870 struct enum_load_modW64_64 x;
872 x.cb = EnumLoadedModulesCallback;
873 x.user = UserContext;
875 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_64, &x);
878 /******************************************************************
879 * EnumerateLoadedModules (DBGHELP.@)
882 struct enum_load_modW64_32
884 PENUMLOADED_MODULES_CALLBACK cb;
885 PVOID user;
886 char module[MAX_PATH];
889 static BOOL CALLBACK enum_load_modW64_32(PCWSTR name, DWORD64 base, ULONG size,
890 PVOID user)
892 struct enum_load_modW64_32* x = user;
893 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
894 return x->cb(x->module, (DWORD)base, size, x->user);
897 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
898 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
899 PVOID UserContext)
901 struct enum_load_modW64_32 x;
903 x.cb = EnumLoadedModulesCallback;
904 x.user = UserContext;
906 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_32, &x);
909 /******************************************************************
910 * EnumerateLoadedModulesW64 (DBGHELP.@)
913 BOOL WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
914 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback,
915 PVOID UserContext)
917 HMODULE* hMods;
918 WCHAR baseW[256], modW[256];
919 DWORD i, sz;
920 MODULEINFO mi;
922 hMods = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods[0]));
923 if (!hMods) return FALSE;
925 if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
927 /* hProcess should also be a valid process handle !! */
928 FIXME("If this happens, bump the number in mod\n");
929 HeapFree(GetProcessHeap(), 0, hMods);
930 return FALSE;
932 sz /= sizeof(HMODULE);
933 for (i = 0; i < sz; i++)
935 if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
936 !GetModuleBaseNameW(hProcess, hMods[i], baseW, ARRAY_SIZE(baseW)))
937 continue;
938 module_fill_module(baseW, modW, ARRAY_SIZE(modW));
939 EnumLoadedModulesCallback(modW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage,
940 UserContext);
942 HeapFree(GetProcessHeap(), 0, hMods);
944 return sz != 0 && i == sz;
947 static void dbghelp_str_WtoA(const WCHAR *src, char *dst, int dst_len)
949 WideCharToMultiByte(CP_ACP, 0, src, -1, dst, dst_len - 1, NULL, NULL);
950 dst[dst_len - 1] = 0;
953 /******************************************************************
954 * SymGetModuleInfo (DBGHELP.@)
957 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
958 PIMAGEHLP_MODULE ModuleInfo)
960 IMAGEHLP_MODULE mi;
961 IMAGEHLP_MODULEW64 miw64;
963 if (sizeof(mi) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
965 miw64.SizeOfStruct = sizeof(miw64);
966 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
968 mi.SizeOfStruct = ModuleInfo->SizeOfStruct;
969 mi.BaseOfImage = miw64.BaseOfImage;
970 mi.ImageSize = miw64.ImageSize;
971 mi.TimeDateStamp = miw64.TimeDateStamp;
972 mi.CheckSum = miw64.CheckSum;
973 mi.NumSyms = miw64.NumSyms;
974 mi.SymType = miw64.SymType;
975 dbghelp_str_WtoA(miw64.ModuleName, mi.ModuleName, sizeof(mi.ModuleName));
976 dbghelp_str_WtoA(miw64.ImageName, mi.ImageName, sizeof(mi.ImageName));
977 dbghelp_str_WtoA(miw64.LoadedImageName, mi.LoadedImageName, sizeof(mi.LoadedImageName));
979 memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
981 return TRUE;
984 /******************************************************************
985 * SymGetModuleInfoW (DBGHELP.@)
988 BOOL WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
989 PIMAGEHLP_MODULEW ModuleInfo)
991 IMAGEHLP_MODULEW64 miw64;
992 IMAGEHLP_MODULEW miw;
994 if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
996 miw64.SizeOfStruct = sizeof(miw64);
997 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
999 miw.SizeOfStruct = ModuleInfo->SizeOfStruct;
1000 miw.BaseOfImage = miw64.BaseOfImage;
1001 miw.ImageSize = miw64.ImageSize;
1002 miw.TimeDateStamp = miw64.TimeDateStamp;
1003 miw.CheckSum = miw64.CheckSum;
1004 miw.NumSyms = miw64.NumSyms;
1005 miw.SymType = miw64.SymType;
1006 strcpyW(miw.ModuleName, miw64.ModuleName);
1007 strcpyW(miw.ImageName, miw64.ImageName);
1008 strcpyW(miw.LoadedImageName, miw64.LoadedImageName);
1009 memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
1011 return TRUE;
1014 /******************************************************************
1015 * SymGetModuleInfo64 (DBGHELP.@)
1018 BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
1019 PIMAGEHLP_MODULE64 ModuleInfo)
1021 IMAGEHLP_MODULE64 mi64;
1022 IMAGEHLP_MODULEW64 miw64;
1024 if (sizeof(mi64) < ModuleInfo->SizeOfStruct)
1026 SetLastError(ERROR_MOD_NOT_FOUND); /* NOTE: native returns this error */
1027 WARN("Wrong size %u\n", ModuleInfo->SizeOfStruct);
1028 return FALSE;
1031 miw64.SizeOfStruct = sizeof(miw64);
1032 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
1034 mi64.SizeOfStruct = ModuleInfo->SizeOfStruct;
1035 mi64.BaseOfImage = miw64.BaseOfImage;
1036 mi64.ImageSize = miw64.ImageSize;
1037 mi64.TimeDateStamp = miw64.TimeDateStamp;
1038 mi64.CheckSum = miw64.CheckSum;
1039 mi64.NumSyms = miw64.NumSyms;
1040 mi64.SymType = miw64.SymType;
1041 dbghelp_str_WtoA(miw64.ModuleName, mi64.ModuleName, sizeof(mi64.ModuleName));
1042 dbghelp_str_WtoA(miw64.ImageName, mi64.ImageName, sizeof(mi64.ImageName));
1043 dbghelp_str_WtoA(miw64.LoadedImageName, mi64.LoadedImageName, sizeof(mi64.LoadedImageName));
1044 dbghelp_str_WtoA(miw64.LoadedPdbName, mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName));
1046 mi64.CVSig = miw64.CVSig;
1047 dbghelp_str_WtoA(miw64.CVData, mi64.CVData, sizeof(mi64.CVData));
1048 mi64.PdbSig = miw64.PdbSig;
1049 mi64.PdbSig70 = miw64.PdbSig70;
1050 mi64.PdbAge = miw64.PdbAge;
1051 mi64.PdbUnmatched = miw64.PdbUnmatched;
1052 mi64.DbgUnmatched = miw64.DbgUnmatched;
1053 mi64.LineNumbers = miw64.LineNumbers;
1054 mi64.GlobalSymbols = miw64.GlobalSymbols;
1055 mi64.TypeInfo = miw64.TypeInfo;
1056 mi64.SourceIndexed = miw64.SourceIndexed;
1057 mi64.Publics = miw64.Publics;
1059 memcpy(ModuleInfo, &mi64, ModuleInfo->SizeOfStruct);
1061 return TRUE;
1064 /******************************************************************
1065 * SymGetModuleInfoW64 (DBGHELP.@)
1068 BOOL WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr,
1069 PIMAGEHLP_MODULEW64 ModuleInfo)
1071 struct process* pcs = process_find_by_handle(hProcess);
1072 struct module* module;
1073 IMAGEHLP_MODULEW64 miw64;
1075 TRACE("%p %s %p\n", hProcess, wine_dbgstr_longlong(dwAddr), ModuleInfo);
1077 if (!pcs) return FALSE;
1078 if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
1079 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1080 if (!module) return FALSE;
1082 miw64 = module->module;
1084 /* update debug information from container if any */
1085 if (module->module.SymType == SymNone)
1087 module = module_get_container(pcs, module);
1088 if (module && module->module.SymType != SymNone)
1090 miw64.SymType = module->module.SymType;
1091 miw64.NumSyms = module->module.NumSyms;
1094 memcpy(ModuleInfo, &miw64, ModuleInfo->SizeOfStruct);
1095 return TRUE;
1098 /***********************************************************************
1099 * SymGetModuleBase (DBGHELP.@)
1101 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
1103 DWORD64 ret;
1105 ret = SymGetModuleBase64(hProcess, dwAddr);
1106 return validate_addr64(ret) ? ret : 0;
1109 /***********************************************************************
1110 * SymGetModuleBase64 (DBGHELP.@)
1112 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
1114 struct process* pcs = process_find_by_handle(hProcess);
1115 struct module* module;
1117 if (!pcs) return 0;
1118 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1119 if (!module) return 0;
1120 return module->module.BaseOfImage;
1123 /******************************************************************
1124 * module_reset_debug_info
1125 * Removes any debug information linked to a given module.
1127 void module_reset_debug_info(struct module* module)
1129 module->sortlist_valid = TRUE;
1130 module->sorttab_size = 0;
1131 module->addr_sorttab = NULL;
1132 module->num_sorttab = module->num_symbols = 0;
1133 hash_table_destroy(&module->ht_symbols);
1134 module->ht_symbols.num_buckets = 0;
1135 module->ht_symbols.buckets = NULL;
1136 hash_table_destroy(&module->ht_types);
1137 module->ht_types.num_buckets = 0;
1138 module->ht_types.buckets = NULL;
1139 module->vtypes.num_elts = 0;
1140 hash_table_destroy(&module->ht_symbols);
1141 module->sources_used = module->sources_alloc = 0;
1142 module->sources = NULL;
1145 /******************************************************************
1146 * SymRefreshModuleList (DBGHELP.@)
1148 BOOL WINAPI SymRefreshModuleList(HANDLE hProcess)
1150 struct process* pcs;
1152 TRACE("(%p)\n", hProcess);
1154 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1156 return refresh_module_list(pcs);
1159 /***********************************************************************
1160 * SymFunctionTableAccess (DBGHELP.@)
1162 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1164 return SymFunctionTableAccess64(hProcess, AddrBase);
1167 /***********************************************************************
1168 * SymFunctionTableAccess64 (DBGHELP.@)
1170 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1172 struct process* pcs = process_find_by_handle(hProcess);
1173 struct module* module;
1175 if (!pcs || !dbghelp_current_cpu->find_runtime_function) return NULL;
1176 module = module_find_by_addr(pcs, AddrBase, DMT_UNKNOWN);
1177 if (!module) return NULL;
1179 return dbghelp_current_cpu->find_runtime_function(module, AddrBase);