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
28 #include "dbghelp_private.h"
30 #ifndef DBGHELP_STATIC_LIB
32 #include <wine/debug.h>
34 //#include "winternl.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
38 const WCHAR S_ElfW
[] = {'<','e','l','f','>','\0'};
39 const WCHAR S_WineLoaderW
[] = {'<','w','i','n','e','-','l','o','a','d','e','r','>','\0'};
40 static const WCHAR S_DotSoW
[] = {'.','s','o','\0'};
41 static const WCHAR S_DotDylibW
[] = {'.','d','y','l','i','b','\0'};
42 static const WCHAR S_DotPdbW
[] = {'.','p','d','b','\0'};
43 static const WCHAR S_DotDbgW
[] = {'.','d','b','g','\0'};
44 const WCHAR S_SlashW
[] = {'/','\0'};
46 static const WCHAR S_AcmW
[] = {'.','a','c','m','\0'};
47 static const WCHAR S_DllW
[] = {'.','d','l','l','\0'};
48 static const WCHAR S_DrvW
[] = {'.','d','r','v','\0'};
49 static const WCHAR S_ExeW
[] = {'.','e','x','e','\0'};
50 static const WCHAR S_OcxW
[] = {'.','o','c','x','\0'};
51 static const WCHAR S_VxdW
[] = {'.','v','x','d','\0'};
52 static const WCHAR
* const ext
[] = {S_AcmW
, S_DllW
, S_DrvW
, S_ExeW
, S_OcxW
, S_VxdW
, NULL
};
54 static int match_ext(const WCHAR
* ptr
, size_t len
)
56 const WCHAR
* const *e
;
59 for (e
= ext
; *e
; e
++)
62 if (l
>= len
) return FALSE
;
63 if (strncmpiW(&ptr
[len
- l
], *e
, l
)) continue;
69 static const WCHAR
* get_filename(const WCHAR
* name
, const WCHAR
* endptr
)
73 if (!endptr
) endptr
= name
+ strlenW(name
);
74 for (ptr
= endptr
- 1; ptr
>= name
; ptr
--)
76 if (*ptr
== '/' || *ptr
== '\\') break;
81 static void module_fill_module(const WCHAR
* in
, WCHAR
* out
, size_t size
)
83 const WCHAR
*loader
= get_wine_loader_name();
84 const WCHAR
*ptr
, *endptr
;
87 ptr
= get_filename(in
, endptr
= in
+ strlenW(in
));
88 len
= min(endptr
- ptr
, size
- 1);
89 memcpy(out
, ptr
, len
* sizeof(WCHAR
));
91 if (len
> 4 && (l
= match_ext(out
, len
)))
93 else if (len
> strlenW(loader
) && !strcmpiW(out
+ len
- strlenW(loader
), loader
))
94 lstrcpynW(out
, S_WineLoaderW
, size
);
97 if (len
> 3 && !strcmpiW(&out
[len
- 3], S_DotSoW
) &&
98 (l
= match_ext(out
, len
- 3)))
99 strcpyW(&out
[len
- l
- 3], S_ElfW
);
101 while ((*out
= tolowerW(*out
))) out
++;
104 void module_set_module(struct module
* module
, const WCHAR
* name
)
106 module_fill_module(name
, module
->module
.ModuleName
, sizeof(module
->module
.ModuleName
));
109 const WCHAR
*get_wine_loader_name(void)
111 static const int is_win64
= sizeof(void *) > sizeof(int); /* FIXME: should depend on target process */
112 static const WCHAR wineW
[] = {'w','i','n','e',0};
113 static const WCHAR suffixW
[] = {'6','4',0};
114 static const WCHAR
*loader
;
121 /* All binaries are loaded with WINELOADER (if run from tree) or by the
124 if ((ptr
= getenv("WINELOADER")))
126 DWORD len
= 2 + MultiByteToWideChar( CP_UNIXCP
, 0, ptr
, -1, NULL
, 0 );
127 buffer
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
128 MultiByteToWideChar( CP_UNIXCP
, 0, ptr
, -1, buffer
, len
);
132 buffer
= HeapAlloc( GetProcessHeap(), 0, sizeof(wineW
) + 2 * sizeof(WCHAR
) );
133 strcpyW( buffer
, wineW
);
135 p
= buffer
+ strlenW( buffer
) - strlenW( suffixW
);
136 if (p
> buffer
&& !strcmpW( p
, suffixW
))
138 if (!is_win64
) *p
= 0;
140 else if (is_win64
) strcatW( buffer
, suffixW
);
142 TRACE( "returning %s\n", debugstr_w(buffer
) );
148 static const char* get_module_type(enum module_type type
, BOOL
virtual)
152 case DMT_ELF
: return virtual ? "Virtual ELF" : "ELF";
153 case DMT_PE
: return virtual ? "Virtual PE" : "PE";
154 case DMT_MACHO
: return virtual ? "Virtual Mach-O" : "Mach-O";
155 default: return "---";
159 /***********************************************************************
160 * Creates and links a new module to a process
162 struct module
* module_new(struct process
* pcs
, const WCHAR
* name
,
163 enum module_type type
, BOOL
virtual,
164 DWORD64 mod_addr
, DWORD64 size
,
165 unsigned long stamp
, unsigned long checksum
)
167 struct module
* module
;
170 assert(type
== DMT_ELF
|| type
== DMT_PE
|| type
== DMT_MACHO
);
171 if (!(module
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*module
))))
174 module
->next
= pcs
->lmodules
;
175 pcs
->lmodules
= module
;
177 TRACE("=> %s %s-%s %s\n",
178 get_module_type(type
, virtual),
179 wine_dbgstr_longlong(mod_addr
), wine_dbgstr_longlong(mod_addr
+ size
),
182 pool_init(&module
->pool
, 65536);
184 module
->process
= pcs
;
185 module
->module
.SizeOfStruct
= sizeof(module
->module
);
186 module
->module
.BaseOfImage
= mod_addr
;
187 module
->module
.ImageSize
= size
;
188 module_set_module(module
, name
);
189 module
->module
.ImageName
[0] = '\0';
190 lstrcpynW(module
->module
.LoadedImageName
, name
, sizeof(module
->module
.LoadedImageName
) / sizeof(WCHAR
));
191 module
->module
.SymType
= SymNone
;
192 module
->module
.NumSyms
= 0;
193 module
->module
.TimeDateStamp
= stamp
;
194 module
->module
.CheckSum
= checksum
;
196 memset(module
->module
.LoadedPdbName
, 0, sizeof(module
->module
.LoadedPdbName
));
197 module
->module
.CVSig
= 0;
198 memset(module
->module
.CVData
, 0, sizeof(module
->module
.CVData
));
199 module
->module
.PdbSig
= 0;
200 memset(&module
->module
.PdbSig70
, 0, sizeof(module
->module
.PdbSig70
));
201 module
->module
.PdbAge
= 0;
202 module
->module
.PdbUnmatched
= FALSE
;
203 module
->module
.DbgUnmatched
= FALSE
;
204 module
->module
.LineNumbers
= FALSE
;
205 module
->module
.GlobalSymbols
= FALSE
;
206 module
->module
.TypeInfo
= FALSE
;
207 module
->module
.SourceIndexed
= FALSE
;
208 module
->module
.Publics
= FALSE
;
210 module
->reloc_delta
= 0;
212 module
->is_virtual
= virtual;
213 for (i
= 0; i
< DFI_LAST
; i
++) module
->format_info
[i
] = NULL
;
214 module
->sortlist_valid
= FALSE
;
215 module
->sorttab_size
= 0;
216 module
->addr_sorttab
= NULL
;
217 module
->num_sorttab
= 0;
218 module
->num_symbols
= 0;
220 vector_init(&module
->vsymt
, sizeof(struct symt
*), 128);
221 /* FIXME: this seems a bit too high (on a per module basis)
222 * need some statistics about this
224 hash_table_init(&module
->pool
, &module
->ht_symbols
, 4096);
225 hash_table_init(&module
->pool
, &module
->ht_types
, 4096);
226 vector_init(&module
->vtypes
, sizeof(struct symt
*), 32);
228 module
->sources_used
= 0;
229 module
->sources_alloc
= 0;
231 wine_rb_init(&module
->sources_offsets_tree
, &source_rb_functions
);
236 /***********************************************************************
237 * module_find_by_nameW
240 struct module
* module_find_by_nameW(const struct process
* pcs
, const WCHAR
* name
)
242 struct module
* module
;
244 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
246 if (!strcmpiW(name
, module
->module
.ModuleName
)) return module
;
248 SetLastError(ERROR_INVALID_NAME
);
252 struct module
* module_find_by_nameA(const struct process
* pcs
, const char* name
)
254 WCHAR wname
[MAX_PATH
];
256 MultiByteToWideChar(CP_ACP
, 0, name
, -1, wname
, sizeof(wname
) / sizeof(WCHAR
));
257 return module_find_by_nameW(pcs
, wname
);
260 /***********************************************************************
261 * module_is_already_loaded
264 struct module
* module_is_already_loaded(const struct process
* pcs
, const WCHAR
* name
)
266 struct module
* module
;
267 const WCHAR
* filename
;
269 /* first compare the loaded image name... */
270 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
272 if (!strcmpiW(name
, module
->module
.LoadedImageName
))
275 /* then compare the standard filenames (without the path) ... */
276 filename
= get_filename(name
, NULL
);
277 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
279 if (!strcmpiW(filename
, get_filename(module
->module
.LoadedImageName
, NULL
)))
282 SetLastError(ERROR_INVALID_NAME
);
286 /***********************************************************************
287 * module_get_container
290 static struct module
* module_get_container(const struct process
* pcs
,
291 const struct module
* inner
)
293 struct module
* module
;
295 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
297 if (module
!= inner
&&
298 module
->module
.BaseOfImage
<= inner
->module
.BaseOfImage
&&
299 module
->module
.BaseOfImage
+ module
->module
.ImageSize
>=
300 inner
->module
.BaseOfImage
+ inner
->module
.ImageSize
)
306 /***********************************************************************
307 * module_get_containee
310 struct module
* module_get_containee(const struct process
* pcs
,
311 const struct module
* outter
)
313 struct module
* module
;
315 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
317 if (module
!= outter
&&
318 outter
->module
.BaseOfImage
<= module
->module
.BaseOfImage
&&
319 outter
->module
.BaseOfImage
+ outter
->module
.ImageSize
>=
320 module
->module
.BaseOfImage
+ module
->module
.ImageSize
)
326 /******************************************************************
329 * get the debug information from a module:
330 * - if the module's type is deferred, then force loading of debug info (and return
332 * - if the module has no debug info and has an ELF container, then return the ELF
333 * container (and also force the ELF container's debug info loading if deferred)
334 * - otherwise return the module itself if it has some debug info
336 BOOL
module_get_debug(struct module_pair
* pair
)
338 IMAGEHLP_DEFERRED_SYMBOL_LOADW64 idslW64
;
340 if (!pair
->requested
) return FALSE
;
341 /* for a PE builtin, always get info from container */
342 if (!(pair
->effective
= module_get_container(pair
->pcs
, pair
->requested
)))
343 pair
->effective
= pair
->requested
;
344 /* if deferred, force loading */
345 if (pair
->effective
->module
.SymType
== SymDeferred
)
349 if (pair
->effective
->is_virtual
) ret
= FALSE
;
350 else switch (pair
->effective
->type
)
352 #ifndef DBGHELP_STATIC_LIB
354 ret
= elf_load_debug_info(pair
->effective
);
358 idslW64
.SizeOfStruct
= sizeof(idslW64
);
359 idslW64
.BaseOfImage
= pair
->effective
->module
.BaseOfImage
;
360 idslW64
.CheckSum
= pair
->effective
->module
.CheckSum
;
361 idslW64
.TimeDateStamp
= pair
->effective
->module
.TimeDateStamp
;
362 memcpy(idslW64
.FileName
, pair
->effective
->module
.ImageName
,
363 sizeof(pair
->effective
->module
.ImageName
));
364 idslW64
.Reparse
= FALSE
;
365 idslW64
.hFile
= INVALID_HANDLE_VALUE
;
367 pcs_callback(pair
->pcs
, CBA_DEFERRED_SYMBOL_LOAD_START
, &idslW64
);
368 ret
= pe_load_debug_info(pair
->pcs
, pair
->effective
);
369 pcs_callback(pair
->pcs
,
370 ret
? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE
: CBA_DEFERRED_SYMBOL_LOAD_FAILURE
,
373 #ifndef DBGHELP_STATIC_LIB
375 ret
= macho_load_debug_info(pair
->effective
, NULL
);
382 if (!ret
) pair
->effective
->module
.SymType
= SymNone
;
383 assert(pair
->effective
->module
.SymType
!= SymDeferred
);
384 pair
->effective
->module
.NumSyms
= pair
->effective
->ht_symbols
.num_elts
;
386 return pair
->effective
->module
.SymType
!= SymNone
;
389 /***********************************************************************
390 * module_find_by_addr
392 * either the addr where module is loaded, or any address inside the
395 struct module
* module_find_by_addr(const struct process
* pcs
, DWORD64 addr
,
396 enum module_type type
)
398 struct module
* module
;
400 if (type
== DMT_UNKNOWN
)
402 if ((module
= module_find_by_addr(pcs
, addr
, DMT_PE
)) ||
403 (module
= module_find_by_addr(pcs
, addr
, DMT_ELF
)) ||
404 (module
= module_find_by_addr(pcs
, addr
, DMT_MACHO
)))
409 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
411 if (type
== module
->type
&& addr
>= module
->module
.BaseOfImage
&&
412 addr
< module
->module
.BaseOfImage
+ module
->module
.ImageSize
)
416 SetLastError(ERROR_INVALID_ADDRESS
);
420 /******************************************************************
421 * module_is_container_loaded
423 * checks whether the native container, for a (supposed) PE builtin is
426 static BOOL
module_is_container_loaded(const struct process
* pcs
,
427 const WCHAR
* ImageName
, DWORD64 base
)
430 struct module
* module
;
431 PCWSTR filename
, modname
;
433 if (!base
) return FALSE
;
434 filename
= get_filename(ImageName
, NULL
);
435 len
= strlenW(filename
);
437 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
439 if ((module
->type
== DMT_ELF
|| module
->type
== DMT_MACHO
) &&
440 base
>= module
->module
.BaseOfImage
&&
441 base
< module
->module
.BaseOfImage
+ module
->module
.ImageSize
)
443 modname
= get_filename(module
->module
.LoadedImageName
, NULL
);
444 if (!strncmpiW(modname
, filename
, len
) &&
445 !memcmp(modname
+ len
, S_DotSoW
, 3 * sizeof(WCHAR
)))
451 /* likely a native PE module */
452 WARN("Couldn't find container for %s\n", debugstr_w(ImageName
));
456 /******************************************************************
457 * module_get_type_by_name
459 * Guesses a filename type from its extension
461 enum module_type
module_get_type_by_name(const WCHAR
* name
)
463 int loader_len
, len
= strlenW(name
);
466 /* Skip all version extensions (.[digits]) regex: "(\.\d+)*$" */
471 while (i
&& isdigit(name
[i
- 1])) i
--;
473 if (i
&& name
[i
- 1] == '.')
479 /* check for terminating .so or .so.[digit] */
480 /* FIXME: Can't rely solely on extension; have to check magic or
481 * stop using .so on Mac OS X. For now, base on platform. */
482 if (len
> 3 && !memcmp(name
+ len
- 3, S_DotSoW
, 3))
489 if (len
> 6 && !strncmpiW(name
+ len
- 6, S_DotDylibW
, 6))
492 if (len
> 4 && !strncmpiW(name
+ len
- 4, S_DotPdbW
, 4))
495 if (len
> 4 && !strncmpiW(name
+ len
- 4, S_DotDbgW
, 4))
498 /* wine is also a native module (Mach-O on Mac OS X, ELF elsewhere) */
499 loader
= get_wine_loader_name();
500 loader_len
= strlenW( loader
);
501 if ((len
== loader_len
|| (len
> loader_len
&& name
[len
- loader_len
- 1] == '/')) &&
502 !strcmpiW(name
+ len
- loader_len
, loader
))
513 /******************************************************************
514 * refresh_module_list
516 #ifndef DBGHELP_STATIC_LIB
517 static BOOL
refresh_module_list(struct process
* pcs
)
519 /* force transparent ELF and Mach-O loading / unloading */
520 return elf_synchronize_module_list(pcs
) || macho_synchronize_module_list(pcs
);
524 /***********************************************************************
525 * SymLoadModule (DBGHELP.@)
527 DWORD WINAPI
SymLoadModule(HANDLE hProcess
, HANDLE hFile
, PCSTR ImageName
,
528 PCSTR ModuleName
, DWORD BaseOfDll
, DWORD SizeOfDll
)
530 return SymLoadModuleEx(hProcess
, hFile
, ImageName
, ModuleName
, BaseOfDll
,
534 /***********************************************************************
535 * SymLoadModuleEx (DBGHELP.@)
537 DWORD64 WINAPI
SymLoadModuleEx(HANDLE hProcess
, HANDLE hFile
, PCSTR ImageName
,
538 PCSTR ModuleName
, DWORD64 BaseOfDll
, DWORD DllSize
,
539 PMODLOAD_DATA Data
, DWORD Flags
)
541 PWSTR wImageName
, wModuleName
;
545 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
546 hProcess
, hFile
, debugstr_a(ImageName
), debugstr_a(ModuleName
),
547 wine_dbgstr_longlong(BaseOfDll
), DllSize
, Data
, Flags
);
551 len
= MultiByteToWideChar(CP_ACP
, 0, ImageName
, -1, NULL
, 0);
552 wImageName
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
553 MultiByteToWideChar(CP_ACP
, 0, ImageName
, -1, wImageName
, len
);
555 else wImageName
= NULL
;
558 len
= MultiByteToWideChar(CP_ACP
, 0, ModuleName
, -1, NULL
, 0);
559 wModuleName
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
560 MultiByteToWideChar(CP_ACP
, 0, ModuleName
, -1, wModuleName
, len
);
562 else wModuleName
= NULL
;
564 ret
= SymLoadModuleExW(hProcess
, hFile
, wImageName
, wModuleName
,
565 BaseOfDll
, DllSize
, Data
, Flags
);
566 HeapFree(GetProcessHeap(), 0, wImageName
);
567 HeapFree(GetProcessHeap(), 0, wModuleName
);
571 /***********************************************************************
572 * SymLoadModuleExW (DBGHELP.@)
574 DWORD64 WINAPI
SymLoadModuleExW(HANDLE hProcess
, HANDLE hFile
, PCWSTR wImageName
,
575 PCWSTR wModuleName
, DWORD64 BaseOfDll
, DWORD SizeOfDll
,
576 PMODLOAD_DATA Data
, DWORD Flags
)
579 struct module
* module
= NULL
;
581 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
582 hProcess
, hFile
, debugstr_w(wImageName
), debugstr_w(wModuleName
),
583 wine_dbgstr_longlong(BaseOfDll
), SizeOfDll
, Data
, Flags
);
586 FIXME("Unsupported load data parameter %p for %s\n",
587 Data
, debugstr_w(wImageName
));
588 if (!validate_addr64(BaseOfDll
)) return FALSE
;
590 if (!(pcs
= process_find_by_handle(hProcess
))) return FALSE
;
592 if (Flags
& SLMFLAG_VIRTUAL
)
594 if (!wImageName
) return FALSE
;
595 module
= module_new(pcs
, wImageName
, module_get_type_by_name(wImageName
),
596 TRUE
, BaseOfDll
, SizeOfDll
, 0, 0);
597 if (!module
) return FALSE
;
598 if (wModuleName
) module_set_module(module
, wModuleName
);
599 module
->module
.SymType
= SymVirtual
;
603 if (Flags
& ~(SLMFLAG_VIRTUAL
))
604 FIXME("Unsupported Flags %08x for %s\n", Flags
, debugstr_w(wImageName
));
606 #ifndef DBGHELP_STATIC_LIB
607 refresh_module_list(pcs
);
610 /* this is a Wine extension to the API just to redo the synchronisation */
611 if (!wImageName
&& !hFile
) return 0;
613 /* check if the module is already loaded, or if it's a builtin PE module with
614 * an containing ELF module
618 module
= module_is_already_loaded(pcs
, wImageName
);
619 if (!module
&& module_is_container_loaded(pcs
, wImageName
, BaseOfDll
))
621 /* force the loading of DLL as builtin */
622 module
= pe_load_builtin_module(pcs
, wImageName
, BaseOfDll
, SizeOfDll
);
627 /* otherwise, try a regular PE module */
628 if (!(module
= pe_load_native_module(pcs
, wImageName
, hFile
, BaseOfDll
, SizeOfDll
)) &&
631 /* and finally an ELF or Mach-O module */
632 #ifndef DBGHELP_STATIC_LIB
633 switch (module_get_type_by_name(wImageName
))
636 module
= elf_load_module(pcs
, wImageName
, BaseOfDll
);
639 module
= macho_load_module(pcs
, wImageName
, BaseOfDll
);
650 WARN("Couldn't locate %s\n", debugstr_w(wImageName
));
653 module
->module
.NumSyms
= module
->ht_symbols
.num_elts
;
654 /* by default module_new fills module.ModuleName from a derivation
655 * of LoadedImageName. Overwrite it, if we have better information
658 module_set_module(module
, wModuleName
);
660 lstrcpynW(module
->module
.ImageName
, wImageName
,
661 sizeof(module
->module
.ImageName
) / sizeof(WCHAR
));
663 return module
->module
.BaseOfImage
;
666 /***********************************************************************
667 * SymLoadModule64 (DBGHELP.@)
669 DWORD64 WINAPI
SymLoadModule64(HANDLE hProcess
, HANDLE hFile
, PCSTR ImageName
,
670 PCSTR ModuleName
, DWORD64 BaseOfDll
, DWORD SizeOfDll
)
672 return SymLoadModuleEx(hProcess
, hFile
, ImageName
, ModuleName
, BaseOfDll
, SizeOfDll
,
676 /******************************************************************
680 BOOL
module_remove(struct process
* pcs
, struct module
* module
)
682 struct module_format
*modfmt
;
686 TRACE("%s (%p)\n", debugstr_w(module
->module
.ModuleName
), module
);
688 for (i
= 0; i
< DFI_LAST
; i
++)
690 if ((modfmt
= module
->format_info
[i
]) && modfmt
->remove
)
691 modfmt
->remove(pcs
, module
->format_info
[i
]);
693 hash_table_destroy(&module
->ht_symbols
);
694 hash_table_destroy(&module
->ht_types
);
695 wine_rb_destroy(&module
->sources_offsets_tree
, NULL
, NULL
);
696 HeapFree(GetProcessHeap(), 0, module
->sources
);
697 HeapFree(GetProcessHeap(), 0, module
->addr_sorttab
);
698 pool_destroy(&module
->pool
);
699 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
702 for (p
= &pcs
->lmodules
; *p
; p
= &(*p
)->next
)
707 HeapFree(GetProcessHeap(), 0, module
);
711 FIXME("This shouldn't happen\n");
715 /******************************************************************
716 * SymUnloadModule (DBGHELP.@)
719 BOOL WINAPI
SymUnloadModule(HANDLE hProcess
, DWORD BaseOfDll
)
722 struct module
* module
;
724 pcs
= process_find_by_handle(hProcess
);
725 if (!pcs
) return FALSE
;
726 module
= module_find_by_addr(pcs
, BaseOfDll
, DMT_UNKNOWN
);
727 if (!module
) return FALSE
;
728 return module_remove(pcs
, module
);
731 /******************************************************************
732 * SymUnloadModule64 (DBGHELP.@)
735 BOOL WINAPI
SymUnloadModule64(HANDLE hProcess
, DWORD64 BaseOfDll
)
738 struct module
* module
;
740 pcs
= process_find_by_handle(hProcess
);
741 if (!pcs
) return FALSE
;
742 if (!validate_addr64(BaseOfDll
)) return FALSE
;
743 module
= module_find_by_addr(pcs
, BaseOfDll
, DMT_UNKNOWN
);
744 if (!module
) return FALSE
;
745 return module_remove(pcs
, module
);
748 /******************************************************************
749 * SymEnumerateModules (DBGHELP.@)
752 struct enum_modW64_32
754 PSYM_ENUMMODULES_CALLBACK cb
;
756 char module
[MAX_PATH
];
759 static BOOL CALLBACK
enum_modW64_32(PCWSTR name
, DWORD64 base
, PVOID user
)
761 struct enum_modW64_32
* x
= user
;
763 WideCharToMultiByte(CP_ACP
, 0, name
, -1, x
->module
, sizeof(x
->module
), NULL
, NULL
);
764 return x
->cb(x
->module
, (DWORD
)base
, x
->user
);
767 BOOL WINAPI
SymEnumerateModules(HANDLE hProcess
,
768 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback
,
771 struct enum_modW64_32 x
;
773 x
.cb
= EnumModulesCallback
;
774 x
.user
= UserContext
;
776 return SymEnumerateModulesW64(hProcess
, enum_modW64_32
, &x
);
779 /******************************************************************
780 * SymEnumerateModules64 (DBGHELP.@)
783 struct enum_modW64_64
785 PSYM_ENUMMODULES_CALLBACK64 cb
;
787 char module
[MAX_PATH
];
790 static BOOL CALLBACK
enum_modW64_64(PCWSTR name
, DWORD64 base
, PVOID user
)
792 struct enum_modW64_64
* x
= user
;
794 WideCharToMultiByte(CP_ACP
, 0, name
, -1, x
->module
, sizeof(x
->module
), NULL
, NULL
);
795 return x
->cb(x
->module
, base
, x
->user
);
798 BOOL WINAPI
SymEnumerateModules64(HANDLE hProcess
,
799 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback
,
802 struct enum_modW64_64 x
;
804 x
.cb
= EnumModulesCallback
;
805 x
.user
= UserContext
;
807 return SymEnumerateModulesW64(hProcess
, enum_modW64_64
, &x
);
810 /******************************************************************
811 * SymEnumerateModulesW64 (DBGHELP.@)
814 BOOL WINAPI
SymEnumerateModulesW64(HANDLE hProcess
,
815 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback
,
818 struct process
* pcs
= process_find_by_handle(hProcess
);
819 struct module
* module
;
821 if (!pcs
) return FALSE
;
823 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
825 if (!(dbghelp_options
& SYMOPT_WINE_WITH_NATIVE_MODULES
) &&
826 (module
->type
== DMT_ELF
|| module
->type
== DMT_MACHO
))
828 if (!EnumModulesCallback(module
->module
.ModuleName
,
829 module
->module
.BaseOfImage
, UserContext
))
835 #ifndef DBGHELP_STATIC_LIB
836 /******************************************************************
837 * EnumerateLoadedModules64 (DBGHELP.@)
840 struct enum_load_modW64_64
842 PENUMLOADED_MODULES_CALLBACK64 cb
;
844 char module
[MAX_PATH
];
847 static BOOL CALLBACK
enum_load_modW64_64(PCWSTR name
, DWORD64 base
, ULONG size
,
850 struct enum_load_modW64_64
* x
= user
;
852 WideCharToMultiByte(CP_ACP
, 0, name
, -1, x
->module
, sizeof(x
->module
), NULL
, NULL
);
853 return x
->cb(x
->module
, base
, size
, x
->user
);
856 BOOL WINAPI
EnumerateLoadedModules64(HANDLE hProcess
,
857 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback
,
860 struct enum_load_modW64_64 x
;
862 x
.cb
= EnumLoadedModulesCallback
;
863 x
.user
= UserContext
;
865 return EnumerateLoadedModulesW64(hProcess
, enum_load_modW64_64
, &x
);
868 /******************************************************************
869 * EnumerateLoadedModules (DBGHELP.@)
872 struct enum_load_modW64_32
874 PENUMLOADED_MODULES_CALLBACK cb
;
876 char module
[MAX_PATH
];
879 static BOOL CALLBACK
enum_load_modW64_32(PCWSTR name
, DWORD64 base
, ULONG size
,
882 struct enum_load_modW64_32
* x
= user
;
883 WideCharToMultiByte(CP_ACP
, 0, name
, -1, x
->module
, sizeof(x
->module
), NULL
, NULL
);
884 return x
->cb(x
->module
, (DWORD
)base
, size
, x
->user
);
887 BOOL WINAPI
EnumerateLoadedModules(HANDLE hProcess
,
888 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback
,
891 struct enum_load_modW64_32 x
;
893 x
.cb
= EnumLoadedModulesCallback
;
894 x
.user
= UserContext
;
896 return EnumerateLoadedModulesW64(hProcess
, enum_load_modW64_32
, &x
);
899 /******************************************************************
900 * EnumerateLoadedModulesW64 (DBGHELP.@)
903 BOOL WINAPI
EnumerateLoadedModulesW64(HANDLE hProcess
,
904 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback
,
908 WCHAR baseW
[256], modW
[256];
912 hMods
= HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods
[0]));
913 if (!hMods
) return FALSE
;
915 if (!EnumProcessModules(hProcess
, hMods
, 256 * sizeof(hMods
[0]), &sz
))
917 /* hProcess should also be a valid process handle !! */
918 FIXME("If this happens, bump the number in mod\n");
919 HeapFree(GetProcessHeap(), 0, hMods
);
922 sz
/= sizeof(HMODULE
);
923 for (i
= 0; i
< sz
; i
++)
925 if (!GetModuleInformation(hProcess
, hMods
[i
], &mi
, sizeof(mi
)) ||
926 !GetModuleBaseNameW(hProcess
, hMods
[i
], baseW
, sizeof(baseW
) / sizeof(WCHAR
)))
928 module_fill_module(baseW
, modW
, sizeof(modW
) / sizeof(CHAR
));
929 EnumLoadedModulesCallback(modW
, (DWORD_PTR
)mi
.lpBaseOfDll
, mi
.SizeOfImage
,
932 HeapFree(GetProcessHeap(), 0, hMods
);
934 return sz
!= 0 && i
== sz
;
936 #endif /* DBGHELP_STATIC_LIB */
938 /******************************************************************
939 * SymGetModuleInfo (DBGHELP.@)
942 BOOL WINAPI
SymGetModuleInfo(HANDLE hProcess
, DWORD dwAddr
,
943 PIMAGEHLP_MODULE ModuleInfo
)
946 IMAGEHLP_MODULEW64 miw64
;
948 if (sizeof(mi
) < ModuleInfo
->SizeOfStruct
) FIXME("Wrong size\n");
950 miw64
.SizeOfStruct
= sizeof(miw64
);
951 if (!SymGetModuleInfoW64(hProcess
, dwAddr
, &miw64
)) return FALSE
;
953 mi
.SizeOfStruct
= miw64
.SizeOfStruct
;
954 mi
.BaseOfImage
= miw64
.BaseOfImage
;
955 mi
.ImageSize
= miw64
.ImageSize
;
956 mi
.TimeDateStamp
= miw64
.TimeDateStamp
;
957 mi
.CheckSum
= miw64
.CheckSum
;
958 mi
.NumSyms
= miw64
.NumSyms
;
959 mi
.SymType
= miw64
.SymType
;
960 WideCharToMultiByte(CP_ACP
, 0, miw64
.ModuleName
, -1,
961 mi
.ModuleName
, sizeof(mi
.ModuleName
), NULL
, NULL
);
962 WideCharToMultiByte(CP_ACP
, 0, miw64
.ImageName
, -1,
963 mi
.ImageName
, sizeof(mi
.ImageName
), NULL
, NULL
);
964 WideCharToMultiByte(CP_ACP
, 0, miw64
.LoadedImageName
, -1,
965 mi
.LoadedImageName
, sizeof(mi
.LoadedImageName
), NULL
, NULL
);
967 memcpy(ModuleInfo
, &mi
, ModuleInfo
->SizeOfStruct
);
972 /******************************************************************
973 * SymGetModuleInfoW (DBGHELP.@)
976 BOOL WINAPI
SymGetModuleInfoW(HANDLE hProcess
, DWORD dwAddr
,
977 PIMAGEHLP_MODULEW ModuleInfo
)
979 IMAGEHLP_MODULEW64 miw64
;
980 IMAGEHLP_MODULEW miw
;
982 if (sizeof(miw
) < ModuleInfo
->SizeOfStruct
) FIXME("Wrong size\n");
984 miw64
.SizeOfStruct
= sizeof(miw64
);
985 if (!SymGetModuleInfoW64(hProcess
, dwAddr
, &miw64
)) return FALSE
;
987 miw
.SizeOfStruct
= miw64
.SizeOfStruct
;
988 miw
.BaseOfImage
= miw64
.BaseOfImage
;
989 miw
.ImageSize
= miw64
.ImageSize
;
990 miw
.TimeDateStamp
= miw64
.TimeDateStamp
;
991 miw
.CheckSum
= miw64
.CheckSum
;
992 miw
.NumSyms
= miw64
.NumSyms
;
993 miw
.SymType
= miw64
.SymType
;
994 strcpyW(miw
.ModuleName
, miw64
.ModuleName
);
995 strcpyW(miw
.ImageName
, miw64
.ImageName
);
996 strcpyW(miw
.LoadedImageName
, miw64
.LoadedImageName
);
997 memcpy(ModuleInfo
, &miw
, ModuleInfo
->SizeOfStruct
);
1002 /******************************************************************
1003 * SymGetModuleInfo64 (DBGHELP.@)
1006 BOOL WINAPI
SymGetModuleInfo64(HANDLE hProcess
, DWORD64 dwAddr
,
1007 PIMAGEHLP_MODULE64 ModuleInfo
)
1009 IMAGEHLP_MODULE64 mi64
;
1010 IMAGEHLP_MODULEW64 miw64
;
1012 if (sizeof(mi64
) < ModuleInfo
->SizeOfStruct
)
1014 SetLastError(ERROR_MOD_NOT_FOUND
); /* NOTE: native returns this error */
1015 WARN("Wrong size %u\n", ModuleInfo
->SizeOfStruct
);
1019 miw64
.SizeOfStruct
= sizeof(miw64
);
1020 if (!SymGetModuleInfoW64(hProcess
, dwAddr
, &miw64
)) return FALSE
;
1022 mi64
.SizeOfStruct
= miw64
.SizeOfStruct
;
1023 mi64
.BaseOfImage
= miw64
.BaseOfImage
;
1024 mi64
.ImageSize
= miw64
.ImageSize
;
1025 mi64
.TimeDateStamp
= miw64
.TimeDateStamp
;
1026 mi64
.CheckSum
= miw64
.CheckSum
;
1027 mi64
.NumSyms
= miw64
.NumSyms
;
1028 mi64
.SymType
= miw64
.SymType
;
1029 WideCharToMultiByte(CP_ACP
, 0, miw64
.ModuleName
, -1,
1030 mi64
.ModuleName
, sizeof(mi64
.ModuleName
), NULL
, NULL
);
1031 WideCharToMultiByte(CP_ACP
, 0, miw64
.ImageName
, -1,
1032 mi64
.ImageName
, sizeof(mi64
.ImageName
), NULL
, NULL
);
1033 WideCharToMultiByte(CP_ACP
, 0, miw64
.LoadedImageName
, -1,
1034 mi64
.LoadedImageName
, sizeof(mi64
.LoadedImageName
), NULL
, NULL
);
1035 WideCharToMultiByte(CP_ACP
, 0, miw64
.LoadedPdbName
, -1,
1036 mi64
.LoadedPdbName
, sizeof(mi64
.LoadedPdbName
), NULL
, NULL
);
1038 mi64
.CVSig
= miw64
.CVSig
;
1039 WideCharToMultiByte(CP_ACP
, 0, miw64
.CVData
, -1,
1040 mi64
.CVData
, sizeof(mi64
.CVData
), NULL
, NULL
);
1041 mi64
.PdbSig
= miw64
.PdbSig
;
1042 mi64
.PdbSig70
= miw64
.PdbSig70
;
1043 mi64
.PdbAge
= miw64
.PdbAge
;
1044 mi64
.PdbUnmatched
= miw64
.PdbUnmatched
;
1045 mi64
.DbgUnmatched
= miw64
.DbgUnmatched
;
1046 mi64
.LineNumbers
= miw64
.LineNumbers
;
1047 mi64
.GlobalSymbols
= miw64
.GlobalSymbols
;
1048 mi64
.TypeInfo
= miw64
.TypeInfo
;
1049 mi64
.SourceIndexed
= miw64
.SourceIndexed
;
1050 mi64
.Publics
= miw64
.Publics
;
1052 memcpy(ModuleInfo
, &mi64
, ModuleInfo
->SizeOfStruct
);
1057 /******************************************************************
1058 * SymGetModuleInfoW64 (DBGHELP.@)
1061 BOOL WINAPI
SymGetModuleInfoW64(HANDLE hProcess
, DWORD64 dwAddr
,
1062 PIMAGEHLP_MODULEW64 ModuleInfo
)
1064 struct process
* pcs
= process_find_by_handle(hProcess
);
1065 struct module
* module
;
1066 IMAGEHLP_MODULEW64 miw64
;
1068 TRACE("%p %s %p\n", hProcess
, wine_dbgstr_longlong(dwAddr
), ModuleInfo
);
1070 if (!pcs
) return FALSE
;
1071 if (ModuleInfo
->SizeOfStruct
> sizeof(*ModuleInfo
)) return FALSE
;
1072 module
= module_find_by_addr(pcs
, dwAddr
, DMT_UNKNOWN
);
1073 if (!module
) return FALSE
;
1075 miw64
= module
->module
;
1077 /* update debug information from container if any */
1078 if (module
->module
.SymType
== SymNone
)
1080 module
= module_get_container(pcs
, module
);
1081 if (module
&& module
->module
.SymType
!= SymNone
)
1083 miw64
.SymType
= module
->module
.SymType
;
1084 miw64
.NumSyms
= module
->module
.NumSyms
;
1087 memcpy(ModuleInfo
, &miw64
, ModuleInfo
->SizeOfStruct
);
1091 /***********************************************************************
1092 * SymGetModuleBase (DBGHELP.@)
1094 DWORD WINAPI
SymGetModuleBase(HANDLE hProcess
, DWORD dwAddr
)
1098 ret
= SymGetModuleBase64(hProcess
, dwAddr
);
1099 return validate_addr64(ret
) ? ret
: 0;
1102 /***********************************************************************
1103 * SymGetModuleBase64 (DBGHELP.@)
1105 DWORD64 WINAPI
SymGetModuleBase64(HANDLE hProcess
, DWORD64 dwAddr
)
1107 struct process
* pcs
= process_find_by_handle(hProcess
);
1108 struct module
* module
;
1111 module
= module_find_by_addr(pcs
, dwAddr
, DMT_UNKNOWN
);
1112 if (!module
) return 0;
1113 return module
->module
.BaseOfImage
;
1116 /******************************************************************
1117 * module_reset_debug_info
1118 * Removes any debug information linked to a given module.
1120 void module_reset_debug_info(struct module
* module
)
1122 module
->sortlist_valid
= TRUE
;
1123 module
->sorttab_size
= 0;
1124 module
->addr_sorttab
= NULL
;
1125 module
->num_sorttab
= module
->num_symbols
= 0;
1126 hash_table_destroy(&module
->ht_symbols
);
1127 module
->ht_symbols
.num_buckets
= 0;
1128 module
->ht_symbols
.buckets
= NULL
;
1129 hash_table_destroy(&module
->ht_types
);
1130 module
->ht_types
.num_buckets
= 0;
1131 module
->ht_types
.buckets
= NULL
;
1132 module
->vtypes
.num_elts
= 0;
1133 hash_table_destroy(&module
->ht_symbols
);
1134 module
->sources_used
= module
->sources_alloc
= 0;
1135 module
->sources
= NULL
;
1138 /******************************************************************
1139 * SymRefreshModuleList (DBGHELP.@)
1141 BOOL WINAPI
SymRefreshModuleList(HANDLE hProcess
)
1143 struct process
* pcs
;
1145 TRACE("(%p)\n", hProcess
);
1147 if (!(pcs
= process_find_by_handle(hProcess
))) return FALSE
;
1149 #ifndef DBGHELP_STATIC_LIB
1150 return refresh_module_list(pcs
);
1156 /***********************************************************************
1157 * SymFunctionTableAccess (DBGHELP.@)
1159 PVOID WINAPI
SymFunctionTableAccess(HANDLE hProcess
, DWORD AddrBase
)
1161 return SymFunctionTableAccess64(hProcess
, AddrBase
);
1164 /***********************************************************************
1165 * SymFunctionTableAccess64 (DBGHELP.@)
1167 PVOID WINAPI
SymFunctionTableAccess64(HANDLE hProcess
, DWORD64 AddrBase
)
1169 struct process
* pcs
= process_find_by_handle(hProcess
);
1170 struct module
* module
;
1172 if (!pcs
|| !dbghelp_current_cpu
->find_runtime_function
) return NULL
;
1173 module
= module_find_by_addr(pcs
, AddrBase
, DMT_UNKNOWN
);
1174 if (!module
) return NULL
;
1176 return dbghelp_current_cpu
->find_runtime_function(module
, AddrBase
);