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
);
26 const WCHAR S_ElfW
[] = {'<','e','l','f','>','\0'};
27 const WCHAR S_WineLoaderW
[] = {'<','w','i','n','e','-','l','o','a','d','e','r','>','\0'};
28 static const WCHAR S_DotSoW
[] = {'.','s','o','\0'};
29 static const WCHAR S_DotDylibW
[] = {'.','d','y','l','i','b','\0'};
30 static const WCHAR S_DotPdbW
[] = {'.','p','d','b','\0'};
31 static const WCHAR S_DotDbgW
[] = {'.','d','b','g','\0'};
32 const WCHAR S_SlashW
[] = {'/','\0'};
34 static const WCHAR S_AcmW
[] = {'.','a','c','m','\0'};
35 static const WCHAR S_DllW
[] = {'.','d','l','l','\0'};
36 static const WCHAR S_DrvW
[] = {'.','d','r','v','\0'};
37 static const WCHAR S_ExeW
[] = {'.','e','x','e','\0'};
38 static const WCHAR S_OcxW
[] = {'.','o','c','x','\0'};
39 static const WCHAR S_VxdW
[] = {'.','v','x','d','\0'};
40 static const WCHAR
* const ext
[] = {S_AcmW
, S_DllW
, S_DrvW
, S_ExeW
, S_OcxW
, S_VxdW
, NULL
};
42 static int match_ext(const WCHAR
* ptr
, size_t len
)
44 const WCHAR
* const *e
;
47 for (e
= ext
; *e
; e
++)
50 if (l
>= len
) return FALSE
;
51 if (strncmpiW(&ptr
[len
- l
], *e
, l
)) continue;
57 static const WCHAR
* get_filename(const WCHAR
* name
, const WCHAR
* endptr
)
61 if (!endptr
) endptr
= name
+ strlenW(name
);
62 for (ptr
= endptr
- 1; ptr
>= name
; ptr
--)
64 if (*ptr
== '/' || *ptr
== '\\') break;
69 static void module_fill_module(const WCHAR
* in
, WCHAR
* out
, size_t size
)
71 const WCHAR
*loader
= get_wine_loader_name();
72 const WCHAR
*ptr
, *endptr
;
75 ptr
= get_filename(in
, endptr
= in
+ strlenW(in
));
76 len
= min(endptr
- ptr
, size
- 1);
77 memcpy(out
, ptr
, len
* sizeof(WCHAR
));
79 if (len
> 4 && (l
= match_ext(out
, len
)))
81 else if (len
> strlenW(loader
) && !strcmpiW(out
+ len
- strlenW(loader
), loader
))
82 lstrcpynW(out
, S_WineLoaderW
, size
);
85 if (len
> 3 && !strcmpiW(&out
[len
- 3], S_DotSoW
) &&
86 (l
= match_ext(out
, len
- 3)))
87 strcpyW(&out
[len
- l
- 3], S_ElfW
);
89 while ((*out
= tolowerW(*out
))) out
++;
92 void module_set_module(struct module
* module
, const WCHAR
* name
)
94 module_fill_module(name
, module
->module
.ModuleName
, sizeof(module
->module
.ModuleName
));
97 const WCHAR
*get_wine_loader_name(void)
99 static const int is_win64
= sizeof(void *) > sizeof(int); /* FIXME: should depend on target process */
100 static const WCHAR wineW
[] = {'w','i','n','e',0};
101 static const WCHAR suffixW
[] = {'6','4',0};
102 static const WCHAR
*loader
;
109 /* All binaries are loaded with WINELOADER (if run from tree) or by the
112 if ((ptr
= getenv("WINELOADER")))
114 DWORD len
= 2 + MultiByteToWideChar( CP_UNIXCP
, 0, ptr
, -1, NULL
, 0 );
115 buffer
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
116 MultiByteToWideChar( CP_UNIXCP
, 0, ptr
, -1, buffer
, len
);
120 buffer
= HeapAlloc( GetProcessHeap(), 0, sizeof(wineW
) + 2 * sizeof(WCHAR
) );
121 strcpyW( buffer
, wineW
);
123 p
= buffer
+ strlenW( buffer
) - strlenW( suffixW
);
124 if (p
> buffer
&& !strcmpW( p
, suffixW
))
126 if (!is_win64
) *p
= 0;
128 else if (is_win64
) strcatW( buffer
, suffixW
);
130 TRACE( "returning %s\n", debugstr_w(buffer
) );
136 static const char* get_module_type(enum module_type type
, BOOL
virtual)
140 case DMT_ELF
: return virtual ? "Virtual ELF" : "ELF";
141 case DMT_PE
: return virtual ? "Virtual PE" : "PE";
142 case DMT_MACHO
: return virtual ? "Virtual Mach-O" : "Mach-O";
143 default: return "---";
147 /***********************************************************************
148 * Creates and links a new module to a process
150 struct module
* module_new(struct process
* pcs
, const WCHAR
* name
,
151 enum module_type type
, BOOL
virtual,
152 DWORD64 mod_addr
, DWORD64 size
,
153 unsigned long stamp
, unsigned long checksum
)
155 struct module
* module
;
158 assert(type
== DMT_ELF
|| type
== DMT_PE
|| type
== DMT_MACHO
);
159 if (!(module
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*module
))))
162 module
->next
= pcs
->lmodules
;
163 pcs
->lmodules
= module
;
165 TRACE("=> %s %s-%s %s\n",
166 get_module_type(type
, virtual),
167 wine_dbgstr_longlong(mod_addr
), wine_dbgstr_longlong(mod_addr
+ size
),
170 pool_init(&module
->pool
, 65536);
172 module
->process
= pcs
;
173 module
->module
.SizeOfStruct
= sizeof(module
->module
);
174 module
->module
.BaseOfImage
= mod_addr
;
175 module
->module
.ImageSize
= size
;
176 module_set_module(module
, name
);
177 module
->module
.ImageName
[0] = '\0';
178 lstrcpynW(module
->module
.LoadedImageName
, name
, sizeof(module
->module
.LoadedImageName
) / sizeof(WCHAR
));
179 module
->module
.SymType
= SymNone
;
180 module
->module
.NumSyms
= 0;
181 module
->module
.TimeDateStamp
= stamp
;
182 module
->module
.CheckSum
= checksum
;
184 memset(module
->module
.LoadedPdbName
, 0, sizeof(module
->module
.LoadedPdbName
));
185 module
->module
.CVSig
= 0;
186 memset(module
->module
.CVData
, 0, sizeof(module
->module
.CVData
));
187 module
->module
.PdbSig
= 0;
188 memset(&module
->module
.PdbSig70
, 0, sizeof(module
->module
.PdbSig70
));
189 module
->module
.PdbAge
= 0;
190 module
->module
.PdbUnmatched
= FALSE
;
191 module
->module
.DbgUnmatched
= FALSE
;
192 module
->module
.LineNumbers
= FALSE
;
193 module
->module
.GlobalSymbols
= FALSE
;
194 module
->module
.TypeInfo
= FALSE
;
195 module
->module
.SourceIndexed
= FALSE
;
196 module
->module
.Publics
= FALSE
;
198 module
->reloc_delta
= 0;
200 module
->is_virtual
= virtual;
201 for (i
= 0; i
< DFI_LAST
; i
++) module
->format_info
[i
] = NULL
;
202 module
->sortlist_valid
= FALSE
;
203 module
->sorttab_size
= 0;
204 module
->addr_sorttab
= NULL
;
205 module
->num_sorttab
= 0;
206 module
->num_symbols
= 0;
208 vector_init(&module
->vsymt
, sizeof(struct symt
*), 128);
209 /* FIXME: this seems a bit too high (on a per module basis)
210 * need some statistics about this
212 hash_table_init(&module
->pool
, &module
->ht_symbols
, 4096);
213 hash_table_init(&module
->pool
, &module
->ht_types
, 4096);
215 hash_table_init(&module
->pool
, &module
->ht_symaddr
, 4096);
217 vector_init(&module
->vtypes
, sizeof(struct symt
*), 32);
219 module
->sources_used
= 0;
220 module
->sources_alloc
= 0;
222 wine_rb_init(&module
->sources_offsets_tree
, &source_rb_functions
);
227 /***********************************************************************
228 * module_find_by_nameW
231 struct module
* module_find_by_nameW(const struct process
* pcs
, const WCHAR
* name
)
233 struct module
* module
;
235 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
237 if (!strcmpiW(name
, module
->module
.ModuleName
)) return module
;
239 SetLastError(ERROR_INVALID_NAME
);
243 struct module
* module_find_by_nameA(const struct process
* pcs
, const char* name
)
245 WCHAR wname
[MAX_PATH
];
247 MultiByteToWideChar(CP_ACP
, 0, name
, -1, wname
, sizeof(wname
) / sizeof(WCHAR
));
248 return module_find_by_nameW(pcs
, wname
);
251 /***********************************************************************
252 * module_is_already_loaded
255 struct module
* module_is_already_loaded(const struct process
* pcs
, const WCHAR
* name
)
257 struct module
* module
;
258 const WCHAR
* filename
;
260 /* first compare the loaded image name... */
261 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
263 if (!strcmpiW(name
, module
->module
.LoadedImageName
))
266 /* then compare the standard filenames (without the path) ... */
267 filename
= get_filename(name
, NULL
);
268 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
270 if (!strcmpiW(filename
, get_filename(module
->module
.LoadedImageName
, NULL
)))
273 SetLastError(ERROR_INVALID_NAME
);
277 /***********************************************************************
278 * module_get_container
281 static struct module
* module_get_container(const struct process
* pcs
,
282 const struct module
* inner
)
284 struct module
* module
;
286 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
288 if (module
!= inner
&&
289 module
->module
.BaseOfImage
<= inner
->module
.BaseOfImage
&&
290 module
->module
.BaseOfImage
+ module
->module
.ImageSize
>=
291 inner
->module
.BaseOfImage
+ inner
->module
.ImageSize
)
297 /***********************************************************************
298 * module_get_containee
301 struct module
* module_get_containee(const struct process
* pcs
,
302 const struct module
* outter
)
304 struct module
* module
;
306 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
308 if (module
!= outter
&&
309 outter
->module
.BaseOfImage
<= module
->module
.BaseOfImage
&&
310 outter
->module
.BaseOfImage
+ outter
->module
.ImageSize
>=
311 module
->module
.BaseOfImage
+ module
->module
.ImageSize
)
317 /******************************************************************
320 * get the debug information from a module:
321 * - if the module's type is deferred, then force loading of debug info (and return
323 * - if the module has no debug info and has an ELF container, then return the ELF
324 * container (and also force the ELF container's debug info loading if deferred)
325 * - otherwise return the module itself if it has some debug info
327 BOOL
module_get_debug(struct module_pair
* pair
)
329 IMAGEHLP_DEFERRED_SYMBOL_LOADW64 idslW64
;
331 if (!pair
->requested
) return FALSE
;
332 /* for a PE builtin, always get info from container */
333 if (!(pair
->effective
= module_get_container(pair
->pcs
, pair
->requested
)))
334 pair
->effective
= pair
->requested
;
335 /* if deferred, force loading */
336 if (pair
->effective
->module
.SymType
== SymDeferred
)
340 if (pair
->effective
->is_virtual
) ret
= FALSE
;
341 else switch (pair
->effective
->type
)
343 #ifndef DBGHELP_STATIC_LIB
345 ret
= elf_load_debug_info(pair
->effective
);
349 idslW64
.SizeOfStruct
= sizeof(idslW64
);
350 idslW64
.BaseOfImage
= pair
->effective
->module
.BaseOfImage
;
351 idslW64
.CheckSum
= pair
->effective
->module
.CheckSum
;
352 idslW64
.TimeDateStamp
= pair
->effective
->module
.TimeDateStamp
;
353 memcpy(idslW64
.FileName
, pair
->effective
->module
.ImageName
,
354 sizeof(pair
->effective
->module
.ImageName
));
355 idslW64
.Reparse
= FALSE
;
356 idslW64
.hFile
= INVALID_HANDLE_VALUE
;
358 pcs_callback(pair
->pcs
, CBA_DEFERRED_SYMBOL_LOAD_START
, &idslW64
);
359 ret
= pe_load_debug_info(pair
->pcs
, pair
->effective
);
360 pcs_callback(pair
->pcs
,
361 ret
? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE
: CBA_DEFERRED_SYMBOL_LOAD_FAILURE
,
364 #ifndef DBGHELP_STATIC_LIB
366 ret
= macho_load_debug_info(pair
->effective
, NULL
);
373 if (!ret
) pair
->effective
->module
.SymType
= SymNone
;
374 assert(pair
->effective
->module
.SymType
!= SymDeferred
);
375 pair
->effective
->module
.NumSyms
= pair
->effective
->ht_symbols
.num_elts
;
377 return pair
->effective
->module
.SymType
!= SymNone
;
380 /***********************************************************************
381 * module_find_by_addr
383 * either the addr where module is loaded, or any address inside the
386 struct module
* module_find_by_addr(const struct process
* pcs
, DWORD64 addr
,
387 enum module_type type
)
389 struct module
* module
;
391 if (type
== DMT_UNKNOWN
)
393 if ((module
= module_find_by_addr(pcs
, addr
, DMT_PE
)) ||
394 (module
= module_find_by_addr(pcs
, addr
, DMT_ELF
)) ||
395 (module
= module_find_by_addr(pcs
, addr
, DMT_MACHO
)))
400 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
402 if (type
== module
->type
&& addr
>= module
->module
.BaseOfImage
&&
403 addr
< module
->module
.BaseOfImage
+ module
->module
.ImageSize
)
407 SetLastError(ERROR_INVALID_ADDRESS
);
411 /******************************************************************
412 * module_is_container_loaded
414 * checks whether the native container, for a (supposed) PE builtin is
417 static BOOL
module_is_container_loaded(const struct process
* pcs
,
418 const WCHAR
* ImageName
, DWORD64 base
)
421 struct module
* module
;
422 PCWSTR filename
, modname
;
424 if (!base
) return FALSE
;
425 filename
= get_filename(ImageName
, NULL
);
426 len
= strlenW(filename
);
428 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
430 if ((module
->type
== DMT_ELF
|| module
->type
== DMT_MACHO
) &&
431 base
>= module
->module
.BaseOfImage
&&
432 base
< module
->module
.BaseOfImage
+ module
->module
.ImageSize
)
434 modname
= get_filename(module
->module
.LoadedImageName
, NULL
);
435 if (!strncmpiW(modname
, filename
, len
) &&
436 !memcmp(modname
+ len
, S_DotSoW
, 3 * sizeof(WCHAR
)))
442 /* likely a native PE module */
443 WARN("Couldn't find container for %s\n", debugstr_w(ImageName
));
447 /******************************************************************
448 * module_get_type_by_name
450 * Guesses a filename type from its extension
452 enum module_type
module_get_type_by_name(const WCHAR
* name
)
454 int loader_len
, len
= strlenW(name
);
457 /* Skip all version extensions (.[digits]) regex: "(\.\d+)*$" */
462 while (i
&& isdigit(name
[i
- 1])) i
--;
464 if (i
&& name
[i
- 1] == '.')
470 /* check for terminating .so or .so.[digit] */
471 /* FIXME: Can't rely solely on extension; have to check magic or
472 * stop using .so on Mac OS X. For now, base on platform. */
473 if (len
> 3 && !memcmp(name
+ len
- 3, S_DotSoW
, 3))
480 if (len
> 6 && !strncmpiW(name
+ len
- 6, S_DotDylibW
, 6))
483 if (len
> 4 && !strncmpiW(name
+ len
- 4, S_DotPdbW
, 4))
486 if (len
> 4 && !strncmpiW(name
+ len
- 4, S_DotDbgW
, 4))
489 /* wine is also a native module (Mach-O on Mac OS X, ELF elsewhere) */
490 loader
= get_wine_loader_name();
491 loader_len
= strlenW( loader
);
492 if ((len
== loader_len
|| (len
> loader_len
&& name
[len
- loader_len
- 1] == '/')) &&
493 !strcmpiW(name
+ len
- loader_len
, loader
))
504 /******************************************************************
505 * refresh_module_list
507 #ifndef DBGHELP_STATIC_LIB
508 static BOOL
refresh_module_list(struct process
* pcs
)
510 /* force transparent ELF and Mach-O loading / unloading */
511 return elf_synchronize_module_list(pcs
) || macho_synchronize_module_list(pcs
);
515 /***********************************************************************
516 * SymLoadModule (DBGHELP.@)
518 DWORD WINAPI
SymLoadModule(HANDLE hProcess
, HANDLE hFile
, PCSTR ImageName
,
519 PCSTR ModuleName
, DWORD BaseOfDll
, DWORD SizeOfDll
)
521 return SymLoadModuleEx(hProcess
, hFile
, ImageName
, ModuleName
, BaseOfDll
,
525 /***********************************************************************
526 * SymLoadModuleEx (DBGHELP.@)
528 DWORD64 WINAPI
SymLoadModuleEx(HANDLE hProcess
, HANDLE hFile
, PCSTR ImageName
,
529 PCSTR ModuleName
, DWORD64 BaseOfDll
, DWORD DllSize
,
530 PMODLOAD_DATA Data
, DWORD Flags
)
532 PWSTR wImageName
, wModuleName
;
536 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
537 hProcess
, hFile
, debugstr_a(ImageName
), debugstr_a(ModuleName
),
538 wine_dbgstr_longlong(BaseOfDll
), DllSize
, Data
, Flags
);
542 len
= MultiByteToWideChar(CP_ACP
, 0, ImageName
, -1, NULL
, 0);
543 wImageName
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
544 MultiByteToWideChar(CP_ACP
, 0, ImageName
, -1, wImageName
, len
);
546 else wImageName
= NULL
;
549 len
= MultiByteToWideChar(CP_ACP
, 0, ModuleName
, -1, NULL
, 0);
550 wModuleName
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
551 MultiByteToWideChar(CP_ACP
, 0, ModuleName
, -1, wModuleName
, len
);
553 else wModuleName
= NULL
;
555 ret
= SymLoadModuleExW(hProcess
, hFile
, wImageName
, wModuleName
,
556 BaseOfDll
, DllSize
, Data
, Flags
);
557 HeapFree(GetProcessHeap(), 0, wImageName
);
558 HeapFree(GetProcessHeap(), 0, wModuleName
);
562 /***********************************************************************
563 * SymLoadModuleExW (DBGHELP.@)
565 DWORD64 WINAPI
SymLoadModuleExW(HANDLE hProcess
, HANDLE hFile
, PCWSTR wImageName
,
566 PCWSTR wModuleName
, DWORD64 BaseOfDll
, DWORD SizeOfDll
,
567 PMODLOAD_DATA Data
, DWORD Flags
)
570 struct module
* module
= NULL
;
572 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
573 hProcess
, hFile
, debugstr_w(wImageName
), debugstr_w(wModuleName
),
574 wine_dbgstr_longlong(BaseOfDll
), SizeOfDll
, Data
, Flags
);
577 FIXME("Unsupported load data parameter %p for %s\n",
578 Data
, debugstr_w(wImageName
));
579 if (!validate_addr64(BaseOfDll
)) return FALSE
;
581 if (!(pcs
= process_find_by_handle(hProcess
))) return FALSE
;
583 if (Flags
& SLMFLAG_VIRTUAL
)
585 if (!wImageName
) return FALSE
;
586 module
= module_new(pcs
, wImageName
, module_get_type_by_name(wImageName
),
587 TRUE
, BaseOfDll
, SizeOfDll
, 0, 0);
588 if (!module
) return FALSE
;
589 if (wModuleName
) module_set_module(module
, wModuleName
);
590 module
->module
.SymType
= SymVirtual
;
594 if (Flags
& ~(SLMFLAG_VIRTUAL
))
595 FIXME("Unsupported Flags %08x for %s\n", Flags
, debugstr_w(wImageName
));
597 #ifndef DBGHELP_STATIC_LIB
598 refresh_module_list(pcs
);
601 /* this is a Wine extension to the API just to redo the synchronisation */
602 if (!wImageName
&& !hFile
) return 0;
604 /* check if the module is already loaded, or if it's a builtin PE module with
605 * an containing ELF module
609 module
= module_is_already_loaded(pcs
, wImageName
);
610 if (!module
&& module_is_container_loaded(pcs
, wImageName
, BaseOfDll
))
612 /* force the loading of DLL as builtin */
613 module
= pe_load_builtin_module(pcs
, wImageName
, BaseOfDll
, SizeOfDll
);
618 /* otherwise, try a regular PE module */
619 if (!(module
= pe_load_native_module(pcs
, wImageName
, hFile
, BaseOfDll
, SizeOfDll
)) &&
622 /* and finally an ELF or Mach-O module */
623 #ifndef DBGHELP_STATIC_LIB
624 switch (module_get_type_by_name(wImageName
))
627 module
= elf_load_module(pcs
, wImageName
, BaseOfDll
);
630 module
= macho_load_module(pcs
, wImageName
, BaseOfDll
);
641 WARN("Couldn't locate %s\n", debugstr_w(wImageName
));
644 module
->module
.NumSyms
= module
->ht_symbols
.num_elts
;
645 /* by default module_new fills module.ModuleName from a derivation
646 * of LoadedImageName. Overwrite it, if we have better information
649 module_set_module(module
, wModuleName
);
651 lstrcpynW(module
->module
.ImageName
, wImageName
,
652 sizeof(module
->module
.ImageName
) / sizeof(WCHAR
));
654 return module
->module
.BaseOfImage
;
657 /***********************************************************************
658 * SymLoadModule64 (DBGHELP.@)
660 DWORD64 WINAPI
SymLoadModule64(HANDLE hProcess
, HANDLE hFile
, PCSTR ImageName
,
661 PCSTR ModuleName
, DWORD64 BaseOfDll
, DWORD SizeOfDll
)
663 return SymLoadModuleEx(hProcess
, hFile
, ImageName
, ModuleName
, BaseOfDll
, SizeOfDll
,
667 /******************************************************************
671 BOOL
module_remove(struct process
* pcs
, struct module
* module
)
673 struct module_format
*modfmt
;
677 TRACE("%s (%p)\n", debugstr_w(module
->module
.ModuleName
), module
);
679 for (i
= 0; i
< DFI_LAST
; i
++)
681 if ((modfmt
= module
->format_info
[i
]) && modfmt
->remove
)
682 modfmt
->remove(pcs
, module
->format_info
[i
]);
684 hash_table_destroy(&module
->ht_symbols
);
685 hash_table_destroy(&module
->ht_types
);
686 wine_rb_destroy(&module
->sources_offsets_tree
, NULL
, NULL
);
687 HeapFree(GetProcessHeap(), 0, module
->sources
);
688 HeapFree(GetProcessHeap(), 0, module
->addr_sorttab
);
689 pool_destroy(&module
->pool
);
690 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
693 for (p
= &pcs
->lmodules
; *p
; p
= &(*p
)->next
)
698 HeapFree(GetProcessHeap(), 0, module
);
702 FIXME("This shouldn't happen\n");
706 /******************************************************************
707 * SymUnloadModule (DBGHELP.@)
710 BOOL WINAPI
SymUnloadModule(HANDLE hProcess
, DWORD BaseOfDll
)
713 struct module
* module
;
715 pcs
= process_find_by_handle(hProcess
);
716 if (!pcs
) return FALSE
;
717 module
= module_find_by_addr(pcs
, BaseOfDll
, DMT_UNKNOWN
);
718 if (!module
) return FALSE
;
719 return module_remove(pcs
, module
);
722 /******************************************************************
723 * SymUnloadModule64 (DBGHELP.@)
726 BOOL WINAPI
SymUnloadModule64(HANDLE hProcess
, DWORD64 BaseOfDll
)
729 struct module
* module
;
731 pcs
= process_find_by_handle(hProcess
);
732 if (!pcs
) return FALSE
;
733 if (!validate_addr64(BaseOfDll
)) return FALSE
;
734 module
= module_find_by_addr(pcs
, BaseOfDll
, DMT_UNKNOWN
);
735 if (!module
) return FALSE
;
736 return module_remove(pcs
, module
);
739 /******************************************************************
740 * SymEnumerateModules (DBGHELP.@)
743 struct enum_modW64_32
745 PSYM_ENUMMODULES_CALLBACK cb
;
747 char module
[MAX_PATH
];
750 static BOOL CALLBACK
enum_modW64_32(PCWSTR name
, DWORD64 base
, PVOID user
)
752 struct enum_modW64_32
* x
= user
;
754 WideCharToMultiByte(CP_ACP
, 0, name
, -1, x
->module
, sizeof(x
->module
), NULL
, NULL
);
755 return x
->cb(x
->module
, (DWORD
)base
, x
->user
);
758 BOOL WINAPI
SymEnumerateModules(HANDLE hProcess
,
759 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback
,
762 struct enum_modW64_32 x
;
764 x
.cb
= EnumModulesCallback
;
765 x
.user
= UserContext
;
767 return SymEnumerateModulesW64(hProcess
, enum_modW64_32
, &x
);
770 /******************************************************************
771 * SymEnumerateModules64 (DBGHELP.@)
774 struct enum_modW64_64
776 PSYM_ENUMMODULES_CALLBACK64 cb
;
778 char module
[MAX_PATH
];
781 static BOOL CALLBACK
enum_modW64_64(PCWSTR name
, DWORD64 base
, PVOID user
)
783 struct enum_modW64_64
* x
= user
;
785 WideCharToMultiByte(CP_ACP
, 0, name
, -1, x
->module
, sizeof(x
->module
), NULL
, NULL
);
786 return x
->cb(x
->module
, base
, x
->user
);
789 BOOL WINAPI
SymEnumerateModules64(HANDLE hProcess
,
790 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback
,
793 struct enum_modW64_64 x
;
795 x
.cb
= EnumModulesCallback
;
796 x
.user
= UserContext
;
798 return SymEnumerateModulesW64(hProcess
, enum_modW64_64
, &x
);
801 /******************************************************************
802 * SymEnumerateModulesW64 (DBGHELP.@)
805 BOOL WINAPI
SymEnumerateModulesW64(HANDLE hProcess
,
806 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback
,
809 struct process
* pcs
= process_find_by_handle(hProcess
);
810 struct module
* module
;
812 if (!pcs
) return FALSE
;
814 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
816 if (!(dbghelp_options
& SYMOPT_WINE_WITH_NATIVE_MODULES
) &&
817 (module
->type
== DMT_ELF
|| module
->type
== DMT_MACHO
))
819 if (!EnumModulesCallback(module
->module
.ModuleName
,
820 module
->module
.BaseOfImage
, UserContext
))
826 #ifndef DBGHELP_STATIC_LIB
827 /******************************************************************
828 * EnumerateLoadedModules64 (DBGHELP.@)
831 struct enum_load_modW64_64
833 PENUMLOADED_MODULES_CALLBACK64 cb
;
835 char module
[MAX_PATH
];
838 static BOOL CALLBACK
enum_load_modW64_64(PCWSTR name
, DWORD64 base
, ULONG size
,
841 struct enum_load_modW64_64
* x
= user
;
843 WideCharToMultiByte(CP_ACP
, 0, name
, -1, x
->module
, sizeof(x
->module
), NULL
, NULL
);
844 return x
->cb(x
->module
, base
, size
, x
->user
);
847 BOOL WINAPI
EnumerateLoadedModules64(HANDLE hProcess
,
848 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback
,
851 struct enum_load_modW64_64 x
;
853 x
.cb
= EnumLoadedModulesCallback
;
854 x
.user
= UserContext
;
856 return EnumerateLoadedModulesW64(hProcess
, enum_load_modW64_64
, &x
);
859 /******************************************************************
860 * EnumerateLoadedModules (DBGHELP.@)
863 struct enum_load_modW64_32
865 PENUMLOADED_MODULES_CALLBACK cb
;
867 char module
[MAX_PATH
];
870 static BOOL CALLBACK
enum_load_modW64_32(PCWSTR name
, DWORD64 base
, ULONG size
,
873 struct enum_load_modW64_32
* x
= user
;
874 WideCharToMultiByte(CP_ACP
, 0, name
, -1, x
->module
, sizeof(x
->module
), NULL
, NULL
);
875 return x
->cb(x
->module
, (DWORD
)base
, size
, x
->user
);
878 BOOL WINAPI
EnumerateLoadedModules(HANDLE hProcess
,
879 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback
,
882 struct enum_load_modW64_32 x
;
884 x
.cb
= EnumLoadedModulesCallback
;
885 x
.user
= UserContext
;
887 return EnumerateLoadedModulesW64(hProcess
, enum_load_modW64_32
, &x
);
890 /******************************************************************
891 * EnumerateLoadedModulesW64 (DBGHELP.@)
894 BOOL WINAPI
EnumerateLoadedModulesW64(HANDLE hProcess
,
895 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback
,
899 WCHAR baseW
[256], modW
[256];
903 hMods
= HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods
[0]));
904 if (!hMods
) return FALSE
;
906 if (!EnumProcessModules(hProcess
, hMods
, 256 * sizeof(hMods
[0]), &sz
))
908 /* hProcess should also be a valid process handle !! */
909 FIXME("If this happens, bump the number in mod\n");
910 HeapFree(GetProcessHeap(), 0, hMods
);
913 sz
/= sizeof(HMODULE
);
914 for (i
= 0; i
< sz
; i
++)
916 if (!GetModuleInformation(hProcess
, hMods
[i
], &mi
, sizeof(mi
)) ||
917 !GetModuleBaseNameW(hProcess
, hMods
[i
], baseW
, sizeof(baseW
) / sizeof(WCHAR
)))
919 module_fill_module(baseW
, modW
, sizeof(modW
) / sizeof(CHAR
));
920 EnumLoadedModulesCallback(modW
, (DWORD_PTR
)mi
.lpBaseOfDll
, mi
.SizeOfImage
,
923 HeapFree(GetProcessHeap(), 0, hMods
);
925 return sz
!= 0 && i
== sz
;
927 #endif /* DBGHELP_STATIC_LIB */
929 /******************************************************************
930 * SymGetModuleInfo (DBGHELP.@)
933 BOOL WINAPI
SymGetModuleInfo(HANDLE hProcess
, DWORD dwAddr
,
934 PIMAGEHLP_MODULE ModuleInfo
)
937 IMAGEHLP_MODULEW64 miw64
;
939 if (sizeof(mi
) < ModuleInfo
->SizeOfStruct
) FIXME("Wrong size\n");
941 miw64
.SizeOfStruct
= sizeof(miw64
);
942 if (!SymGetModuleInfoW64(hProcess
, dwAddr
, &miw64
)) return FALSE
;
944 mi
.SizeOfStruct
= miw64
.SizeOfStruct
;
945 mi
.BaseOfImage
= miw64
.BaseOfImage
;
946 mi
.ImageSize
= miw64
.ImageSize
;
947 mi
.TimeDateStamp
= miw64
.TimeDateStamp
;
948 mi
.CheckSum
= miw64
.CheckSum
;
949 mi
.NumSyms
= miw64
.NumSyms
;
950 mi
.SymType
= miw64
.SymType
;
951 WideCharToMultiByte(CP_ACP
, 0, miw64
.ModuleName
, -1,
952 mi
.ModuleName
, sizeof(mi
.ModuleName
), NULL
, NULL
);
953 WideCharToMultiByte(CP_ACP
, 0, miw64
.ImageName
, -1,
954 mi
.ImageName
, sizeof(mi
.ImageName
), NULL
, NULL
);
955 WideCharToMultiByte(CP_ACP
, 0, miw64
.LoadedImageName
, -1,
956 mi
.LoadedImageName
, sizeof(mi
.LoadedImageName
), NULL
, NULL
);
958 memcpy(ModuleInfo
, &mi
, ModuleInfo
->SizeOfStruct
);
963 /******************************************************************
964 * SymGetModuleInfoW (DBGHELP.@)
967 BOOL WINAPI
SymGetModuleInfoW(HANDLE hProcess
, DWORD dwAddr
,
968 PIMAGEHLP_MODULEW ModuleInfo
)
970 IMAGEHLP_MODULEW64 miw64
;
971 IMAGEHLP_MODULEW miw
;
973 if (sizeof(miw
) < ModuleInfo
->SizeOfStruct
) FIXME("Wrong size\n");
975 miw64
.SizeOfStruct
= sizeof(miw64
);
976 if (!SymGetModuleInfoW64(hProcess
, dwAddr
, &miw64
)) return FALSE
;
978 miw
.SizeOfStruct
= miw64
.SizeOfStruct
;
979 miw
.BaseOfImage
= miw64
.BaseOfImage
;
980 miw
.ImageSize
= miw64
.ImageSize
;
981 miw
.TimeDateStamp
= miw64
.TimeDateStamp
;
982 miw
.CheckSum
= miw64
.CheckSum
;
983 miw
.NumSyms
= miw64
.NumSyms
;
984 miw
.SymType
= miw64
.SymType
;
985 strcpyW(miw
.ModuleName
, miw64
.ModuleName
);
986 strcpyW(miw
.ImageName
, miw64
.ImageName
);
987 strcpyW(miw
.LoadedImageName
, miw64
.LoadedImageName
);
988 memcpy(ModuleInfo
, &miw
, ModuleInfo
->SizeOfStruct
);
993 /******************************************************************
994 * SymGetModuleInfo64 (DBGHELP.@)
997 BOOL WINAPI
SymGetModuleInfo64(HANDLE hProcess
, DWORD64 dwAddr
,
998 PIMAGEHLP_MODULE64 ModuleInfo
)
1000 IMAGEHLP_MODULE64 mi64
;
1001 IMAGEHLP_MODULEW64 miw64
;
1003 if (sizeof(mi64
) < ModuleInfo
->SizeOfStruct
)
1005 SetLastError(ERROR_MOD_NOT_FOUND
); /* NOTE: native returns this error */
1006 WARN("Wrong size %u\n", ModuleInfo
->SizeOfStruct
);
1010 miw64
.SizeOfStruct
= sizeof(miw64
);
1011 if (!SymGetModuleInfoW64(hProcess
, dwAddr
, &miw64
)) return FALSE
;
1013 mi64
.SizeOfStruct
= miw64
.SizeOfStruct
;
1014 mi64
.BaseOfImage
= miw64
.BaseOfImage
;
1015 mi64
.ImageSize
= miw64
.ImageSize
;
1016 mi64
.TimeDateStamp
= miw64
.TimeDateStamp
;
1017 mi64
.CheckSum
= miw64
.CheckSum
;
1018 mi64
.NumSyms
= miw64
.NumSyms
;
1019 mi64
.SymType
= miw64
.SymType
;
1020 WideCharToMultiByte(CP_ACP
, 0, miw64
.ModuleName
, -1,
1021 mi64
.ModuleName
, sizeof(mi64
.ModuleName
), NULL
, NULL
);
1022 WideCharToMultiByte(CP_ACP
, 0, miw64
.ImageName
, -1,
1023 mi64
.ImageName
, sizeof(mi64
.ImageName
), NULL
, NULL
);
1024 WideCharToMultiByte(CP_ACP
, 0, miw64
.LoadedImageName
, -1,
1025 mi64
.LoadedImageName
, sizeof(mi64
.LoadedImageName
), NULL
, NULL
);
1026 WideCharToMultiByte(CP_ACP
, 0, miw64
.LoadedPdbName
, -1,
1027 mi64
.LoadedPdbName
, sizeof(mi64
.LoadedPdbName
), NULL
, NULL
);
1029 mi64
.CVSig
= miw64
.CVSig
;
1030 WideCharToMultiByte(CP_ACP
, 0, miw64
.CVData
, -1,
1031 mi64
.CVData
, sizeof(mi64
.CVData
), NULL
, NULL
);
1032 mi64
.PdbSig
= miw64
.PdbSig
;
1033 mi64
.PdbSig70
= miw64
.PdbSig70
;
1034 mi64
.PdbAge
= miw64
.PdbAge
;
1035 mi64
.PdbUnmatched
= miw64
.PdbUnmatched
;
1036 mi64
.DbgUnmatched
= miw64
.DbgUnmatched
;
1037 mi64
.LineNumbers
= miw64
.LineNumbers
;
1038 mi64
.GlobalSymbols
= miw64
.GlobalSymbols
;
1039 mi64
.TypeInfo
= miw64
.TypeInfo
;
1040 mi64
.SourceIndexed
= miw64
.SourceIndexed
;
1041 mi64
.Publics
= miw64
.Publics
;
1043 memcpy(ModuleInfo
, &mi64
, ModuleInfo
->SizeOfStruct
);
1048 /******************************************************************
1049 * SymGetModuleInfoW64 (DBGHELP.@)
1052 BOOL WINAPI
SymGetModuleInfoW64(HANDLE hProcess
, DWORD64 dwAddr
,
1053 PIMAGEHLP_MODULEW64 ModuleInfo
)
1055 struct process
* pcs
= process_find_by_handle(hProcess
);
1056 struct module
* module
;
1057 IMAGEHLP_MODULEW64 miw64
;
1059 TRACE("%p %s %p\n", hProcess
, wine_dbgstr_longlong(dwAddr
), ModuleInfo
);
1061 if (!pcs
) return FALSE
;
1062 if (ModuleInfo
->SizeOfStruct
> sizeof(*ModuleInfo
)) return FALSE
;
1063 module
= module_find_by_addr(pcs
, dwAddr
, DMT_UNKNOWN
);
1064 if (!module
) return FALSE
;
1066 miw64
= module
->module
;
1068 /* update debug information from container if any */
1069 if (module
->module
.SymType
== SymNone
)
1071 module
= module_get_container(pcs
, module
);
1072 if (module
&& module
->module
.SymType
!= SymNone
)
1074 miw64
.SymType
= module
->module
.SymType
;
1075 miw64
.NumSyms
= module
->module
.NumSyms
;
1078 memcpy(ModuleInfo
, &miw64
, ModuleInfo
->SizeOfStruct
);
1082 /***********************************************************************
1083 * SymGetModuleBase (DBGHELP.@)
1085 DWORD WINAPI
SymGetModuleBase(HANDLE hProcess
, DWORD dwAddr
)
1089 ret
= SymGetModuleBase64(hProcess
, dwAddr
);
1090 return validate_addr64(ret
) ? ret
: 0;
1093 /***********************************************************************
1094 * SymGetModuleBase64 (DBGHELP.@)
1096 DWORD64 WINAPI
SymGetModuleBase64(HANDLE hProcess
, DWORD64 dwAddr
)
1098 struct process
* pcs
= process_find_by_handle(hProcess
);
1099 struct module
* module
;
1102 module
= module_find_by_addr(pcs
, dwAddr
, DMT_UNKNOWN
);
1103 if (!module
) return 0;
1104 return module
->module
.BaseOfImage
;
1107 /******************************************************************
1108 * module_reset_debug_info
1109 * Removes any debug information linked to a given module.
1111 void module_reset_debug_info(struct module
* module
)
1113 module
->sortlist_valid
= TRUE
;
1114 module
->sorttab_size
= 0;
1115 module
->addr_sorttab
= NULL
;
1116 module
->num_sorttab
= module
->num_symbols
= 0;
1117 hash_table_destroy(&module
->ht_symbols
);
1118 module
->ht_symbols
.num_buckets
= 0;
1119 module
->ht_symbols
.buckets
= NULL
;
1120 hash_table_destroy(&module
->ht_types
);
1121 module
->ht_types
.num_buckets
= 0;
1122 module
->ht_types
.buckets
= NULL
;
1123 module
->vtypes
.num_elts
= 0;
1124 hash_table_destroy(&module
->ht_symbols
);
1125 module
->sources_used
= module
->sources_alloc
= 0;
1126 module
->sources
= NULL
;
1129 /******************************************************************
1130 * SymRefreshModuleList (DBGHELP.@)
1132 BOOL WINAPI
SymRefreshModuleList(HANDLE hProcess
)
1134 struct process
* pcs
;
1136 TRACE("(%p)\n", hProcess
);
1138 if (!(pcs
= process_find_by_handle(hProcess
))) return FALSE
;
1140 #ifndef DBGHELP_STATIC_LIB
1141 return refresh_module_list(pcs
);
1147 /***********************************************************************
1148 * SymFunctionTableAccess (DBGHELP.@)
1150 PVOID WINAPI
SymFunctionTableAccess(HANDLE hProcess
, DWORD AddrBase
)
1152 return SymFunctionTableAccess64(hProcess
, AddrBase
);
1155 /***********************************************************************
1156 * SymFunctionTableAccess64 (DBGHELP.@)
1158 PVOID WINAPI
SymFunctionTableAccess64(HANDLE hProcess
, DWORD64 AddrBase
)
1160 struct process
* pcs
= process_find_by_handle(hProcess
);
1161 struct module
* module
;
1163 if (!pcs
|| !dbghelp_current_cpu
->find_runtime_function
) return NULL
;
1164 module
= module_find_by_addr(pcs
, AddrBase
, DMT_UNKNOWN
);
1165 if (!module
) return NULL
;
1167 return dbghelp_current_cpu
->find_runtime_function(module
, AddrBase
);