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 "dbghelp_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
28 const WCHAR S_ElfW
[] = {'<','e','l','f','>','\0'};
29 const WCHAR S_WineLoaderW
[] = {'<','w','i','n','e','-','l','o','a','d','e','r','>','\0'};
30 static const WCHAR S_DotSoW
[] = {'.','s','o','\0'};
31 static const WCHAR S_DotDylibW
[] = {'.','d','y','l','i','b','\0'};
32 static const WCHAR S_DotPdbW
[] = {'.','p','d','b','\0'};
33 static const WCHAR S_DotDbgW
[] = {'.','d','b','g','\0'};
34 const WCHAR S_SlashW
[] = {'/','\0'};
36 static const WCHAR S_AcmW
[] = {'.','a','c','m','\0'};
37 static const WCHAR S_DllW
[] = {'.','d','l','l','\0'};
38 static const WCHAR S_DrvW
[] = {'.','d','r','v','\0'};
39 static const WCHAR S_ExeW
[] = {'.','e','x','e','\0'};
40 static const WCHAR S_OcxW
[] = {'.','o','c','x','\0'};
41 static const WCHAR S_VxdW
[] = {'.','v','x','d','\0'};
42 static const WCHAR
* const ext
[] = {S_AcmW
, S_DllW
, S_DrvW
, S_ExeW
, S_OcxW
, S_VxdW
, NULL
};
44 static int match_ext(const WCHAR
* ptr
, size_t len
)
46 const WCHAR
* const *e
;
49 for (e
= ext
; *e
; e
++)
52 if (l
>= len
) return 0;
53 if (strncmpiW(&ptr
[len
- l
], *e
, l
)) continue;
59 static const WCHAR
* get_filename(const WCHAR
* name
, const WCHAR
* endptr
)
63 if (!endptr
) endptr
= name
+ strlenW(name
);
64 for (ptr
= endptr
- 1; ptr
>= name
; ptr
--)
66 if (*ptr
== '/' || *ptr
== '\\') break;
71 static void module_fill_module(const WCHAR
* in
, WCHAR
* out
, size_t size
)
73 const WCHAR
*loader
= get_wine_loader_name();
74 const WCHAR
*ptr
, *endptr
;
77 ptr
= get_filename(in
, endptr
= in
+ strlenW(in
));
78 len
= min(endptr
- ptr
, size
- 1);
79 memcpy(out
, ptr
, len
* sizeof(WCHAR
));
81 if (len
> 4 && (l
= match_ext(out
, len
)))
83 else if (len
> strlenW(loader
) && !strcmpiW(out
+ len
- strlenW(loader
), loader
))
84 lstrcpynW(out
, S_WineLoaderW
, size
);
87 if (len
> 3 && !strcmpiW(&out
[len
- 3], S_DotSoW
) &&
88 (l
= match_ext(out
, len
- 3)))
89 strcpyW(&out
[len
- l
- 3], S_ElfW
);
91 while ((*out
= tolowerW(*out
))) out
++;
94 void module_set_module(struct module
* module
, const WCHAR
* name
)
96 module_fill_module(name
, module
->module
.ModuleName
, sizeof(module
->module
.ModuleName
));
99 const WCHAR
*get_wine_loader_name(void)
101 static const BOOL is_win64
= sizeof(void *) > sizeof(int); /* FIXME: should depend on target process */
102 static const WCHAR wineW
[] = {'w','i','n','e',0};
103 static const WCHAR suffixW
[] = {'6','4',0};
104 static const WCHAR
*loader
;
111 /* All binaries are loaded with WINELOADER (if run from tree) or by the
114 if ((ptr
= getenv("WINELOADER")))
116 DWORD len
= 2 + MultiByteToWideChar( CP_UNIXCP
, 0, ptr
, -1, NULL
, 0 );
117 buffer
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
118 MultiByteToWideChar( CP_UNIXCP
, 0, ptr
, -1, buffer
, len
);
122 buffer
= HeapAlloc( GetProcessHeap(), 0, sizeof(wineW
) + 2 * sizeof(WCHAR
) );
123 strcpyW( buffer
, wineW
);
125 p
= buffer
+ strlenW( buffer
) - strlenW( suffixW
);
126 if (p
> buffer
&& !strcmpW( p
, suffixW
))
128 if (!is_win64
) *p
= 0;
130 else if (is_win64
) strcatW( buffer
, suffixW
);
132 TRACE( "returning %s\n", debugstr_w(buffer
) );
138 static const char* get_module_type(enum module_type type
, BOOL
virtual)
142 case DMT_ELF
: return virtual ? "Virtual ELF" : "ELF";
143 case DMT_PE
: return virtual ? "Virtual PE" : "PE";
144 case DMT_MACHO
: return virtual ? "Virtual Mach-O" : "Mach-O";
145 default: return "---";
149 /***********************************************************************
150 * Creates and links a new module to a process
152 struct module
* module_new(struct process
* pcs
, const WCHAR
* name
,
153 enum module_type type
, BOOL
virtual,
154 DWORD64 mod_addr
, DWORD64 size
,
155 unsigned long stamp
, unsigned long checksum
)
157 struct module
* module
;
160 assert(type
== DMT_ELF
|| type
== DMT_PE
|| type
== DMT_MACHO
);
161 if (!(module
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*module
))))
164 module
->next
= pcs
->lmodules
;
165 pcs
->lmodules
= module
;
167 TRACE("=> %s %s-%s %s\n",
168 get_module_type(type
, virtual),
169 wine_dbgstr_longlong(mod_addr
), wine_dbgstr_longlong(mod_addr
+ size
),
172 pool_init(&module
->pool
, 65536);
174 module
->process
= pcs
;
175 module
->module
.SizeOfStruct
= sizeof(module
->module
);
176 module
->module
.BaseOfImage
= mod_addr
;
177 module
->module
.ImageSize
= size
;
178 module_set_module(module
, name
);
179 module
->module
.ImageName
[0] = '\0';
180 lstrcpynW(module
->module
.LoadedImageName
, name
, sizeof(module
->module
.LoadedImageName
) / sizeof(WCHAR
));
181 module
->module
.SymType
= SymNone
;
182 module
->module
.NumSyms
= 0;
183 module
->module
.TimeDateStamp
= stamp
;
184 module
->module
.CheckSum
= checksum
;
186 memset(module
->module
.LoadedPdbName
, 0, sizeof(module
->module
.LoadedPdbName
));
187 module
->module
.CVSig
= 0;
188 memset(module
->module
.CVData
, 0, sizeof(module
->module
.CVData
));
189 module
->module
.PdbSig
= 0;
190 memset(&module
->module
.PdbSig70
, 0, sizeof(module
->module
.PdbSig70
));
191 module
->module
.PdbAge
= 0;
192 module
->module
.PdbUnmatched
= FALSE
;
193 module
->module
.DbgUnmatched
= FALSE
;
194 module
->module
.LineNumbers
= FALSE
;
195 module
->module
.GlobalSymbols
= FALSE
;
196 module
->module
.TypeInfo
= FALSE
;
197 module
->module
.SourceIndexed
= FALSE
;
198 module
->module
.Publics
= FALSE
;
200 module
->reloc_delta
= 0;
202 module
->is_virtual
= virtual;
203 for (i
= 0; i
< DFI_LAST
; i
++) module
->format_info
[i
] = NULL
;
204 module
->sortlist_valid
= FALSE
;
205 module
->sorttab_size
= 0;
206 module
->addr_sorttab
= NULL
;
207 module
->num_sorttab
= 0;
208 module
->num_symbols
= 0;
210 vector_init(&module
->vsymt
, sizeof(struct symt
*), 128);
211 /* FIXME: this seems a bit too high (on a per module basis)
212 * need some statistics about this
214 hash_table_init(&module
->pool
, &module
->ht_symbols
, 4096);
215 hash_table_init(&module
->pool
, &module
->ht_types
, 4096);
217 hash_table_init(&module
->pool
, &module
->ht_symaddr
, 4096);
219 vector_init(&module
->vtypes
, sizeof(struct symt
*), 32);
221 module
->sources_used
= 0;
222 module
->sources_alloc
= 0;
224 wine_rb_init(&module
->sources_offsets_tree
, &source_rb_functions
);
229 /***********************************************************************
230 * module_find_by_nameW
233 struct module
* module_find_by_nameW(const struct process
* pcs
, const WCHAR
* name
)
235 struct module
* module
;
237 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
239 if (!strcmpiW(name
, module
->module
.ModuleName
)) return module
;
241 SetLastError(ERROR_INVALID_NAME
);
245 struct module
* module_find_by_nameA(const struct process
* pcs
, const char* name
)
247 WCHAR wname
[MAX_PATH
];
249 MultiByteToWideChar(CP_ACP
, 0, name
, -1, wname
, sizeof(wname
) / sizeof(WCHAR
));
250 return module_find_by_nameW(pcs
, wname
);
253 /***********************************************************************
254 * module_is_already_loaded
257 struct module
* module_is_already_loaded(const struct process
* pcs
, const WCHAR
* name
)
259 struct module
* module
;
260 const WCHAR
* filename
;
262 /* first compare the loaded image name... */
263 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
265 if (!strcmpiW(name
, module
->module
.LoadedImageName
))
268 /* then compare the standard filenames (without the path) ... */
269 filename
= get_filename(name
, NULL
);
270 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
272 if (!strcmpiW(filename
, get_filename(module
->module
.LoadedImageName
, NULL
)))
275 SetLastError(ERROR_INVALID_NAME
);
279 /***********************************************************************
280 * module_get_container
283 static struct module
* module_get_container(const struct process
* pcs
,
284 const struct module
* inner
)
286 struct module
* module
;
288 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
290 if (module
!= inner
&&
291 module
->module
.BaseOfImage
<= inner
->module
.BaseOfImage
&&
292 module
->module
.BaseOfImage
+ module
->module
.ImageSize
>=
293 inner
->module
.BaseOfImage
+ inner
->module
.ImageSize
)
299 /***********************************************************************
300 * module_get_containee
303 struct module
* module_get_containee(const struct process
* pcs
,
304 const struct module
* outter
)
306 struct module
* module
;
308 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
310 if (module
!= outter
&&
311 outter
->module
.BaseOfImage
<= module
->module
.BaseOfImage
&&
312 outter
->module
.BaseOfImage
+ outter
->module
.ImageSize
>=
313 module
->module
.BaseOfImage
+ module
->module
.ImageSize
)
319 /******************************************************************
322 * get the debug information from a module:
323 * - if the module's type is deferred, then force loading of debug info (and return
325 * - if the module has no debug info and has an ELF container, then return the ELF
326 * container (and also force the ELF container's debug info loading if deferred)
327 * - otherwise return the module itself if it has some debug info
329 BOOL
module_get_debug(struct module_pair
* pair
)
331 IMAGEHLP_DEFERRED_SYMBOL_LOADW64 idslW64
;
333 if (!pair
->requested
) return FALSE
;
334 /* for a PE builtin, always get info from container */
335 if (!(pair
->effective
= module_get_container(pair
->pcs
, pair
->requested
)))
336 pair
->effective
= pair
->requested
;
337 /* if deferred, force loading */
338 if (pair
->effective
->module
.SymType
== SymDeferred
)
342 if (pair
->effective
->is_virtual
) ret
= FALSE
;
343 else switch (pair
->effective
->type
)
345 #ifndef DBGHELP_STATIC_LIB
347 ret
= elf_load_debug_info(pair
->effective
);
351 idslW64
.SizeOfStruct
= sizeof(idslW64
);
352 idslW64
.BaseOfImage
= pair
->effective
->module
.BaseOfImage
;
353 idslW64
.CheckSum
= pair
->effective
->module
.CheckSum
;
354 idslW64
.TimeDateStamp
= pair
->effective
->module
.TimeDateStamp
;
355 memcpy(idslW64
.FileName
, pair
->effective
->module
.ImageName
,
356 sizeof(pair
->effective
->module
.ImageName
));
357 idslW64
.Reparse
= FALSE
;
358 idslW64
.hFile
= INVALID_HANDLE_VALUE
;
360 pcs_callback(pair
->pcs
, CBA_DEFERRED_SYMBOL_LOAD_START
, &idslW64
);
361 ret
= pe_load_debug_info(pair
->pcs
, pair
->effective
);
362 pcs_callback(pair
->pcs
,
363 ret
? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE
: CBA_DEFERRED_SYMBOL_LOAD_FAILURE
,
366 #ifndef DBGHELP_STATIC_LIB
368 ret
= macho_load_debug_info(pair
->effective
, NULL
);
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
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
)))
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
)
409 SetLastError(ERROR_INVALID_ADDRESS
);
413 /******************************************************************
414 * module_is_container_loaded
416 * checks whether the native container, for a (supposed) PE builtin is
419 static BOOL
module_is_container_loaded(const struct process
* pcs
,
420 const WCHAR
* ImageName
, DWORD64 base
)
423 struct module
* module
;
424 PCWSTR filename
, modname
;
425 static WCHAR
* dll_prefix
;
426 static int dll_prefix_len
;
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
);
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
)))
455 /* likely a native PE module */
456 WARN("Couldn't find container for %s\n", debugstr_w(ImageName
));
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
);
470 /* Skip all version extensions (.[digits]) regex: "(\.\d+)*$" */
475 while (i
&& isdigit(name
[i
- 1])) i
--;
477 if (i
&& name
[i
- 1] == '.')
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))
493 if (len
> 6 && !strncmpiW(name
+ len
- 6, S_DotDylibW
, 6))
496 if (len
> 4 && !strncmpiW(name
+ len
- 4, S_DotPdbW
, 4))
499 if (len
> 4 && !strncmpiW(name
+ len
- 4, S_DotDbgW
, 4))
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
))
517 /******************************************************************
518 * refresh_module_list
520 #ifndef DBGHELP_STATIC_LIB
521 static BOOL
refresh_module_list(struct process
* pcs
)
523 /* force transparent ELF and Mach-O loading / unloading */
524 return elf_synchronize_module_list(pcs
) || macho_synchronize_module_list(pcs
);
528 /***********************************************************************
529 * SymLoadModule (DBGHELP.@)
531 DWORD WINAPI
SymLoadModule(HANDLE hProcess
, HANDLE hFile
, PCSTR ImageName
,
532 PCSTR ModuleName
, DWORD BaseOfDll
, DWORD SizeOfDll
)
534 return SymLoadModuleEx(hProcess
, hFile
, ImageName
, ModuleName
, BaseOfDll
,
538 /***********************************************************************
539 * SymLoadModuleEx (DBGHELP.@)
541 DWORD64 WINAPI
SymLoadModuleEx(HANDLE hProcess
, HANDLE hFile
, PCSTR ImageName
,
542 PCSTR ModuleName
, DWORD64 BaseOfDll
, DWORD DllSize
,
543 PMODLOAD_DATA Data
, DWORD Flags
)
545 PWSTR wImageName
, wModuleName
;
549 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
550 hProcess
, hFile
, debugstr_a(ImageName
), debugstr_a(ModuleName
),
551 wine_dbgstr_longlong(BaseOfDll
), DllSize
, Data
, Flags
);
555 len
= MultiByteToWideChar(CP_ACP
, 0, ImageName
, -1, NULL
, 0);
556 wImageName
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
557 MultiByteToWideChar(CP_ACP
, 0, ImageName
, -1, wImageName
, len
);
559 else wImageName
= NULL
;
562 len
= MultiByteToWideChar(CP_ACP
, 0, ModuleName
, -1, NULL
, 0);
563 wModuleName
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
564 MultiByteToWideChar(CP_ACP
, 0, ModuleName
, -1, wModuleName
, len
);
566 else wModuleName
= NULL
;
568 ret
= SymLoadModuleExW(hProcess
, hFile
, wImageName
, wModuleName
,
569 BaseOfDll
, DllSize
, Data
, Flags
);
570 HeapFree(GetProcessHeap(), 0, wImageName
);
571 HeapFree(GetProcessHeap(), 0, wModuleName
);
575 /***********************************************************************
576 * SymLoadModuleExW (DBGHELP.@)
578 DWORD64 WINAPI
SymLoadModuleExW(HANDLE hProcess
, HANDLE hFile
, PCWSTR wImageName
,
579 PCWSTR wModuleName
, DWORD64 BaseOfDll
, DWORD SizeOfDll
,
580 PMODLOAD_DATA Data
, DWORD Flags
)
583 struct module
* module
= NULL
;
585 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
586 hProcess
, hFile
, debugstr_w(wImageName
), debugstr_w(wModuleName
),
587 wine_dbgstr_longlong(BaseOfDll
), SizeOfDll
, Data
, Flags
);
590 FIXME("Unsupported load data parameter %p for %s\n",
591 Data
, debugstr_w(wImageName
));
592 if (!validate_addr64(BaseOfDll
)) return FALSE
;
594 if (!(pcs
= process_find_by_handle(hProcess
))) return FALSE
;
596 if (Flags
& SLMFLAG_VIRTUAL
)
598 if (!wImageName
) return FALSE
;
599 module
= module_new(pcs
, wImageName
, module_get_type_by_name(wImageName
),
600 TRUE
, BaseOfDll
, SizeOfDll
, 0, 0);
601 if (!module
) return FALSE
;
602 if (wModuleName
) module_set_module(module
, wModuleName
);
603 module
->module
.SymType
= SymVirtual
;
607 if (Flags
& ~(SLMFLAG_VIRTUAL
))
608 FIXME("Unsupported Flags %08x for %s\n", Flags
, debugstr_w(wImageName
));
610 #ifndef DBGHELP_STATIC_LIB
611 refresh_module_list(pcs
);
614 /* this is a Wine extension to the API just to redo the synchronisation */
615 if (!wImageName
&& !hFile
) return 0;
617 /* check if the module is already loaded, or if it's a builtin PE module with
618 * an containing ELF module
622 module
= module_is_already_loaded(pcs
, wImageName
);
623 if (!module
&& module_is_container_loaded(pcs
, wImageName
, BaseOfDll
))
625 /* force the loading of DLL as builtin */
626 module
= pe_load_builtin_module(pcs
, wImageName
, BaseOfDll
, SizeOfDll
);
631 /* otherwise, try a regular PE module */
632 if (!(module
= pe_load_native_module(pcs
, wImageName
, hFile
, BaseOfDll
, SizeOfDll
)) &&
635 /* and finally an ELF or Mach-O module */
636 #ifndef DBGHELP_STATIC_LIB
637 switch (module_get_type_by_name(wImageName
))
640 module
= elf_load_module(pcs
, wImageName
, BaseOfDll
);
643 module
= macho_load_module(pcs
, wImageName
, BaseOfDll
);
654 WARN("Couldn't locate %s\n", debugstr_w(wImageName
));
657 module
->module
.NumSyms
= module
->ht_symbols
.num_elts
;
658 /* by default module_new fills module.ModuleName from a derivation
659 * of LoadedImageName. Overwrite it, if we have better information
662 module_set_module(module
, wModuleName
);
664 lstrcpynW(module
->module
.ImageName
, wImageName
,
665 sizeof(module
->module
.ImageName
) / sizeof(WCHAR
));
667 return module
->module
.BaseOfImage
;
670 /***********************************************************************
671 * SymLoadModule64 (DBGHELP.@)
673 DWORD64 WINAPI
SymLoadModule64(HANDLE hProcess
, HANDLE hFile
, PCSTR ImageName
,
674 PCSTR ModuleName
, DWORD64 BaseOfDll
, DWORD SizeOfDll
)
676 return SymLoadModuleEx(hProcess
, hFile
, ImageName
, ModuleName
, BaseOfDll
, SizeOfDll
,
680 /******************************************************************
684 BOOL
module_remove(struct process
* pcs
, struct module
* module
)
686 struct module_format
*modfmt
;
690 TRACE("%s (%p)\n", debugstr_w(module
->module
.ModuleName
), module
);
692 for (i
= 0; i
< DFI_LAST
; i
++)
694 if ((modfmt
= module
->format_info
[i
]) && modfmt
->remove
)
695 modfmt
->remove(pcs
, module
->format_info
[i
]);
697 hash_table_destroy(&module
->ht_symbols
);
698 hash_table_destroy(&module
->ht_types
);
699 wine_rb_destroy(&module
->sources_offsets_tree
, NULL
, NULL
);
700 HeapFree(GetProcessHeap(), 0, module
->sources
);
701 HeapFree(GetProcessHeap(), 0, module
->addr_sorttab
);
702 pool_destroy(&module
->pool
);
703 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
706 for (p
= &pcs
->lmodules
; *p
; p
= &(*p
)->next
)
711 HeapFree(GetProcessHeap(), 0, module
);
715 FIXME("This shouldn't happen\n");
719 /******************************************************************
720 * SymUnloadModule (DBGHELP.@)
723 BOOL WINAPI
SymUnloadModule(HANDLE hProcess
, DWORD BaseOfDll
)
726 struct module
* module
;
728 pcs
= process_find_by_handle(hProcess
);
729 if (!pcs
) return FALSE
;
730 module
= module_find_by_addr(pcs
, BaseOfDll
, DMT_UNKNOWN
);
731 if (!module
) return FALSE
;
732 return module_remove(pcs
, module
);
735 /******************************************************************
736 * SymUnloadModule64 (DBGHELP.@)
739 BOOL WINAPI
SymUnloadModule64(HANDLE hProcess
, DWORD64 BaseOfDll
)
742 struct module
* module
;
744 pcs
= process_find_by_handle(hProcess
);
745 if (!pcs
) return FALSE
;
746 if (!validate_addr64(BaseOfDll
)) return FALSE
;
747 module
= module_find_by_addr(pcs
, BaseOfDll
, DMT_UNKNOWN
);
748 if (!module
) return FALSE
;
749 return module_remove(pcs
, module
);
752 /******************************************************************
753 * SymEnumerateModules (DBGHELP.@)
756 struct enum_modW64_32
758 PSYM_ENUMMODULES_CALLBACK cb
;
760 char module
[MAX_PATH
];
763 static BOOL CALLBACK
enum_modW64_32(PCWSTR name
, DWORD64 base
, PVOID user
)
765 struct enum_modW64_32
* x
= user
;
767 WideCharToMultiByte(CP_ACP
, 0, name
, -1, x
->module
, sizeof(x
->module
), NULL
, NULL
);
768 return x
->cb(x
->module
, (DWORD
)base
, x
->user
);
771 BOOL WINAPI
SymEnumerateModules(HANDLE hProcess
,
772 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback
,
775 struct enum_modW64_32 x
;
777 x
.cb
= EnumModulesCallback
;
778 x
.user
= UserContext
;
780 return SymEnumerateModulesW64(hProcess
, enum_modW64_32
, &x
);
783 /******************************************************************
784 * SymEnumerateModules64 (DBGHELP.@)
787 struct enum_modW64_64
789 PSYM_ENUMMODULES_CALLBACK64 cb
;
791 char module
[MAX_PATH
];
794 static BOOL CALLBACK
enum_modW64_64(PCWSTR name
, DWORD64 base
, PVOID user
)
796 struct enum_modW64_64
* x
= user
;
798 WideCharToMultiByte(CP_ACP
, 0, name
, -1, x
->module
, sizeof(x
->module
), NULL
, NULL
);
799 return x
->cb(x
->module
, base
, x
->user
);
802 BOOL WINAPI
SymEnumerateModules64(HANDLE hProcess
,
803 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback
,
806 struct enum_modW64_64 x
;
808 x
.cb
= EnumModulesCallback
;
809 x
.user
= UserContext
;
811 return SymEnumerateModulesW64(hProcess
, enum_modW64_64
, &x
);
814 /******************************************************************
815 * SymEnumerateModulesW64 (DBGHELP.@)
818 BOOL WINAPI
SymEnumerateModulesW64(HANDLE hProcess
,
819 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback
,
822 struct process
* pcs
= process_find_by_handle(hProcess
);
823 struct module
* module
;
825 if (!pcs
) return FALSE
;
827 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
829 if (!(dbghelp_options
& SYMOPT_WINE_WITH_NATIVE_MODULES
) &&
830 (module
->type
== DMT_ELF
|| module
->type
== DMT_MACHO
))
832 if (!EnumModulesCallback(module
->module
.ModuleName
,
833 module
->module
.BaseOfImage
, UserContext
))
839 #ifndef DBGHELP_STATIC_LIB
840 /******************************************************************
841 * EnumerateLoadedModules64 (DBGHELP.@)
844 struct enum_load_modW64_64
846 PENUMLOADED_MODULES_CALLBACK64 cb
;
848 char module
[MAX_PATH
];
851 static BOOL CALLBACK
enum_load_modW64_64(PCWSTR name
, DWORD64 base
, ULONG size
,
854 struct enum_load_modW64_64
* x
= user
;
856 WideCharToMultiByte(CP_ACP
, 0, name
, -1, x
->module
, sizeof(x
->module
), NULL
, NULL
);
857 return x
->cb(x
->module
, base
, size
, x
->user
);
860 BOOL WINAPI
EnumerateLoadedModules64(HANDLE hProcess
,
861 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback
,
864 struct enum_load_modW64_64 x
;
866 x
.cb
= EnumLoadedModulesCallback
;
867 x
.user
= UserContext
;
869 return EnumerateLoadedModulesW64(hProcess
, enum_load_modW64_64
, &x
);
872 /******************************************************************
873 * EnumerateLoadedModules (DBGHELP.@)
876 struct enum_load_modW64_32
878 PENUMLOADED_MODULES_CALLBACK cb
;
880 char module
[MAX_PATH
];
883 static BOOL CALLBACK
enum_load_modW64_32(PCWSTR name
, DWORD64 base
, ULONG size
,
886 struct enum_load_modW64_32
* x
= user
;
887 WideCharToMultiByte(CP_ACP
, 0, name
, -1, x
->module
, sizeof(x
->module
), NULL
, NULL
);
888 return x
->cb(x
->module
, (DWORD
)base
, size
, x
->user
);
891 BOOL WINAPI
EnumerateLoadedModules(HANDLE hProcess
,
892 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback
,
895 struct enum_load_modW64_32 x
;
897 x
.cb
= EnumLoadedModulesCallback
;
898 x
.user
= UserContext
;
900 return EnumerateLoadedModulesW64(hProcess
, enum_load_modW64_32
, &x
);
903 /******************************************************************
904 * EnumerateLoadedModulesW64 (DBGHELP.@)
907 BOOL WINAPI
EnumerateLoadedModulesW64(HANDLE hProcess
,
908 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback
,
912 WCHAR baseW
[256], modW
[256];
916 hMods
= HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods
[0]));
917 if (!hMods
) return FALSE
;
919 if (!EnumProcessModules(hProcess
, hMods
, 256 * sizeof(hMods
[0]), &sz
))
921 /* hProcess should also be a valid process handle !! */
922 FIXME("If this happens, bump the number in mod\n");
923 HeapFree(GetProcessHeap(), 0, hMods
);
926 sz
/= sizeof(HMODULE
);
927 for (i
= 0; i
< sz
; i
++)
929 if (!GetModuleInformation(hProcess
, hMods
[i
], &mi
, sizeof(mi
)) ||
930 !GetModuleBaseNameW(hProcess
, hMods
[i
], baseW
, sizeof(baseW
) / sizeof(WCHAR
)))
932 module_fill_module(baseW
, modW
, sizeof(modW
) / sizeof(CHAR
));
933 EnumLoadedModulesCallback(modW
, (DWORD_PTR
)mi
.lpBaseOfDll
, mi
.SizeOfImage
,
936 HeapFree(GetProcessHeap(), 0, hMods
);
938 return sz
!= 0 && i
== sz
;
940 #endif /* DBGHELP_STATIC_LIB */
942 /******************************************************************
943 * SymGetModuleInfo (DBGHELP.@)
946 BOOL WINAPI
SymGetModuleInfo(HANDLE hProcess
, DWORD dwAddr
,
947 PIMAGEHLP_MODULE ModuleInfo
)
950 IMAGEHLP_MODULEW64 miw64
;
952 if (sizeof(mi
) < ModuleInfo
->SizeOfStruct
) FIXME("Wrong size\n");
954 miw64
.SizeOfStruct
= sizeof(miw64
);
955 if (!SymGetModuleInfoW64(hProcess
, dwAddr
, &miw64
)) return FALSE
;
957 mi
.SizeOfStruct
= miw64
.SizeOfStruct
;
958 mi
.BaseOfImage
= miw64
.BaseOfImage
;
959 mi
.ImageSize
= miw64
.ImageSize
;
960 mi
.TimeDateStamp
= miw64
.TimeDateStamp
;
961 mi
.CheckSum
= miw64
.CheckSum
;
962 mi
.NumSyms
= miw64
.NumSyms
;
963 mi
.SymType
= miw64
.SymType
;
964 WideCharToMultiByte(CP_ACP
, 0, miw64
.ModuleName
, -1,
965 mi
.ModuleName
, sizeof(mi
.ModuleName
), NULL
, NULL
);
966 WideCharToMultiByte(CP_ACP
, 0, miw64
.ImageName
, -1,
967 mi
.ImageName
, sizeof(mi
.ImageName
), NULL
, NULL
);
968 WideCharToMultiByte(CP_ACP
, 0, miw64
.LoadedImageName
, -1,
969 mi
.LoadedImageName
, sizeof(mi
.LoadedImageName
), NULL
, NULL
);
971 memcpy(ModuleInfo
, &mi
, ModuleInfo
->SizeOfStruct
);
976 /******************************************************************
977 * SymGetModuleInfoW (DBGHELP.@)
980 BOOL WINAPI
SymGetModuleInfoW(HANDLE hProcess
, DWORD dwAddr
,
981 PIMAGEHLP_MODULEW ModuleInfo
)
983 IMAGEHLP_MODULEW64 miw64
;
984 IMAGEHLP_MODULEW miw
;
986 if (sizeof(miw
) < ModuleInfo
->SizeOfStruct
) FIXME("Wrong size\n");
988 miw64
.SizeOfStruct
= sizeof(miw64
);
989 if (!SymGetModuleInfoW64(hProcess
, dwAddr
, &miw64
)) return FALSE
;
991 miw
.SizeOfStruct
= miw64
.SizeOfStruct
;
992 miw
.BaseOfImage
= miw64
.BaseOfImage
;
993 miw
.ImageSize
= miw64
.ImageSize
;
994 miw
.TimeDateStamp
= miw64
.TimeDateStamp
;
995 miw
.CheckSum
= miw64
.CheckSum
;
996 miw
.NumSyms
= miw64
.NumSyms
;
997 miw
.SymType
= miw64
.SymType
;
998 strcpyW(miw
.ModuleName
, miw64
.ModuleName
);
999 strcpyW(miw
.ImageName
, miw64
.ImageName
);
1000 strcpyW(miw
.LoadedImageName
, miw64
.LoadedImageName
);
1001 memcpy(ModuleInfo
, &miw
, ModuleInfo
->SizeOfStruct
);
1006 /******************************************************************
1007 * SymGetModuleInfo64 (DBGHELP.@)
1010 BOOL WINAPI
SymGetModuleInfo64(HANDLE hProcess
, DWORD64 dwAddr
,
1011 PIMAGEHLP_MODULE64 ModuleInfo
)
1013 IMAGEHLP_MODULE64 mi64
;
1014 IMAGEHLP_MODULEW64 miw64
;
1016 if (sizeof(mi64
) < ModuleInfo
->SizeOfStruct
)
1018 SetLastError(ERROR_MOD_NOT_FOUND
); /* NOTE: native returns this error */
1019 WARN("Wrong size %u\n", ModuleInfo
->SizeOfStruct
);
1023 miw64
.SizeOfStruct
= sizeof(miw64
);
1024 if (!SymGetModuleInfoW64(hProcess
, dwAddr
, &miw64
)) return FALSE
;
1026 mi64
.SizeOfStruct
= miw64
.SizeOfStruct
;
1027 mi64
.BaseOfImage
= miw64
.BaseOfImage
;
1028 mi64
.ImageSize
= miw64
.ImageSize
;
1029 mi64
.TimeDateStamp
= miw64
.TimeDateStamp
;
1030 mi64
.CheckSum
= miw64
.CheckSum
;
1031 mi64
.NumSyms
= miw64
.NumSyms
;
1032 mi64
.SymType
= miw64
.SymType
;
1033 WideCharToMultiByte(CP_ACP
, 0, miw64
.ModuleName
, -1,
1034 mi64
.ModuleName
, sizeof(mi64
.ModuleName
), NULL
, NULL
);
1035 WideCharToMultiByte(CP_ACP
, 0, miw64
.ImageName
, -1,
1036 mi64
.ImageName
, sizeof(mi64
.ImageName
), NULL
, NULL
);
1037 WideCharToMultiByte(CP_ACP
, 0, miw64
.LoadedImageName
, -1,
1038 mi64
.LoadedImageName
, sizeof(mi64
.LoadedImageName
), NULL
, NULL
);
1039 WideCharToMultiByte(CP_ACP
, 0, miw64
.LoadedPdbName
, -1,
1040 mi64
.LoadedPdbName
, sizeof(mi64
.LoadedPdbName
), NULL
, NULL
);
1042 mi64
.CVSig
= miw64
.CVSig
;
1043 WideCharToMultiByte(CP_ACP
, 0, miw64
.CVData
, -1,
1044 mi64
.CVData
, sizeof(mi64
.CVData
), NULL
, NULL
);
1045 mi64
.PdbSig
= miw64
.PdbSig
;
1046 mi64
.PdbSig70
= miw64
.PdbSig70
;
1047 mi64
.PdbAge
= miw64
.PdbAge
;
1048 mi64
.PdbUnmatched
= miw64
.PdbUnmatched
;
1049 mi64
.DbgUnmatched
= miw64
.DbgUnmatched
;
1050 mi64
.LineNumbers
= miw64
.LineNumbers
;
1051 mi64
.GlobalSymbols
= miw64
.GlobalSymbols
;
1052 mi64
.TypeInfo
= miw64
.TypeInfo
;
1053 mi64
.SourceIndexed
= miw64
.SourceIndexed
;
1054 mi64
.Publics
= miw64
.Publics
;
1056 memcpy(ModuleInfo
, &mi64
, ModuleInfo
->SizeOfStruct
);
1061 /******************************************************************
1062 * SymGetModuleInfoW64 (DBGHELP.@)
1065 BOOL WINAPI
SymGetModuleInfoW64(HANDLE hProcess
, DWORD64 dwAddr
,
1066 PIMAGEHLP_MODULEW64 ModuleInfo
)
1068 struct process
* pcs
= process_find_by_handle(hProcess
);
1069 struct module
* module
;
1070 IMAGEHLP_MODULEW64 miw64
;
1072 TRACE("%p %s %p\n", hProcess
, wine_dbgstr_longlong(dwAddr
), ModuleInfo
);
1074 if (!pcs
) return FALSE
;
1075 if (ModuleInfo
->SizeOfStruct
> sizeof(*ModuleInfo
)) return FALSE
;
1076 module
= module_find_by_addr(pcs
, dwAddr
, DMT_UNKNOWN
);
1077 if (!module
) return FALSE
;
1079 miw64
= module
->module
;
1081 /* update debug information from container if any */
1082 if (module
->module
.SymType
== SymNone
)
1084 module
= module_get_container(pcs
, module
);
1085 if (module
&& module
->module
.SymType
!= SymNone
)
1087 miw64
.SymType
= module
->module
.SymType
;
1088 miw64
.NumSyms
= module
->module
.NumSyms
;
1091 memcpy(ModuleInfo
, &miw64
, ModuleInfo
->SizeOfStruct
);
1095 /***********************************************************************
1096 * SymGetModuleBase (DBGHELP.@)
1098 DWORD WINAPI
SymGetModuleBase(HANDLE hProcess
, DWORD dwAddr
)
1102 ret
= SymGetModuleBase64(hProcess
, dwAddr
);
1103 return validate_addr64(ret
) ? ret
: 0;
1106 /***********************************************************************
1107 * SymGetModuleBase64 (DBGHELP.@)
1109 DWORD64 WINAPI
SymGetModuleBase64(HANDLE hProcess
, DWORD64 dwAddr
)
1111 struct process
* pcs
= process_find_by_handle(hProcess
);
1112 struct module
* module
;
1115 module
= module_find_by_addr(pcs
, dwAddr
, DMT_UNKNOWN
);
1116 if (!module
) return 0;
1117 return module
->module
.BaseOfImage
;
1120 /******************************************************************
1121 * module_reset_debug_info
1122 * Removes any debug information linked to a given module.
1124 void module_reset_debug_info(struct module
* module
)
1126 module
->sortlist_valid
= TRUE
;
1127 module
->sorttab_size
= 0;
1128 module
->addr_sorttab
= NULL
;
1129 module
->num_sorttab
= module
->num_symbols
= 0;
1130 hash_table_destroy(&module
->ht_symbols
);
1131 module
->ht_symbols
.num_buckets
= 0;
1132 module
->ht_symbols
.buckets
= NULL
;
1133 hash_table_destroy(&module
->ht_types
);
1134 module
->ht_types
.num_buckets
= 0;
1135 module
->ht_types
.buckets
= NULL
;
1136 module
->vtypes
.num_elts
= 0;
1137 hash_table_destroy(&module
->ht_symbols
);
1138 module
->sources_used
= module
->sources_alloc
= 0;
1139 module
->sources
= NULL
;
1142 /******************************************************************
1143 * SymRefreshModuleList (DBGHELP.@)
1145 BOOL WINAPI
SymRefreshModuleList(HANDLE hProcess
)
1147 struct process
* pcs
;
1149 TRACE("(%p)\n", hProcess
);
1151 if (!(pcs
= process_find_by_handle(hProcess
))) return FALSE
;
1153 #ifndef DBGHELP_STATIC_LIB
1154 return refresh_module_list(pcs
);
1160 /***********************************************************************
1161 * SymFunctionTableAccess (DBGHELP.@)
1163 PVOID WINAPI
SymFunctionTableAccess(HANDLE hProcess
, DWORD AddrBase
)
1165 return SymFunctionTableAccess64(hProcess
, AddrBase
);
1168 /***********************************************************************
1169 * SymFunctionTableAccess64 (DBGHELP.@)
1171 PVOID WINAPI
SymFunctionTableAccess64(HANDLE hProcess
, DWORD64 AddrBase
)
1173 struct process
* pcs
= process_find_by_handle(hProcess
);
1174 struct module
* module
;
1176 if (!pcs
|| !dbghelp_current_cpu
->find_runtime_function
) return NULL
;
1177 module
= module_find_by_addr(pcs
, AddrBase
, DMT_UNKNOWN
);
1178 if (!module
) return NULL
;
1180 return dbghelp_current_cpu
->find_runtime_function(module
, AddrBase
);