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"
29 #ifndef DBGHELP_STATIC_LIB
32 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
37 const WCHAR S_ElfW
[] = {'<','e','l','f','>','\0'};
38 const WCHAR S_WineLoaderW
[] = {'<','w','i','n','e','-','l','o','a','d','e','r','>','\0'};
39 static const WCHAR S_DotSoW
[] = {'.','s','o','\0'};
40 static const WCHAR S_DotDylibW
[] = {'.','d','y','l','i','b','\0'};
41 static const WCHAR S_DotPdbW
[] = {'.','p','d','b','\0'};
42 static const WCHAR S_DotDbgW
[] = {'.','d','b','g','\0'};
43 const WCHAR S_SlashW
[] = {'/','\0'};
45 static const WCHAR S_AcmW
[] = {'.','a','c','m','\0'};
46 static const WCHAR S_DllW
[] = {'.','d','l','l','\0'};
47 static const WCHAR S_DrvW
[] = {'.','d','r','v','\0'};
48 static const WCHAR S_ExeW
[] = {'.','e','x','e','\0'};
49 static const WCHAR S_OcxW
[] = {'.','o','c','x','\0'};
50 static const WCHAR S_VxdW
[] = {'.','v','x','d','\0'};
51 static const WCHAR
* const ext
[] = {S_AcmW
, S_DllW
, S_DrvW
, S_ExeW
, S_OcxW
, S_VxdW
, NULL
};
53 static int match_ext(const WCHAR
* ptr
, size_t len
)
55 const WCHAR
* const *e
;
58 for (e
= ext
; *e
; e
++)
61 if (l
>= len
) return 0;
62 if (strncmpiW(&ptr
[len
- l
], *e
, l
)) continue;
68 static const WCHAR
* get_filename(const WCHAR
* name
, const WCHAR
* endptr
)
72 if (!endptr
) endptr
= name
+ strlenW(name
);
73 for (ptr
= endptr
- 1; ptr
>= name
; ptr
--)
75 if (*ptr
== '/' || *ptr
== '\\') break;
80 static void module_fill_module(const WCHAR
* in
, WCHAR
* out
, size_t size
)
82 const WCHAR
*loader
= get_wine_loader_name();
83 const WCHAR
*ptr
, *endptr
;
86 ptr
= get_filename(in
, endptr
= in
+ strlenW(in
));
87 len
= min(endptr
- ptr
, size
- 1);
88 memcpy(out
, ptr
, len
* sizeof(WCHAR
));
90 if (len
> 4 && (l
= match_ext(out
, len
)))
92 else if (len
> strlenW(loader
) && !strcmpiW(out
+ len
- strlenW(loader
), loader
))
93 lstrcpynW(out
, S_WineLoaderW
, size
);
96 if (len
> 3 && !strcmpiW(&out
[len
- 3], S_DotSoW
) &&
97 (l
= match_ext(out
, len
- 3)))
98 strcpyW(&out
[len
- l
- 3], S_ElfW
);
100 while ((*out
= tolowerW(*out
))) out
++;
103 void module_set_module(struct module
* module
, const WCHAR
* name
)
105 module_fill_module(name
, module
->module
.ModuleName
,
106 sizeof(module
->module
.ModuleName
) / sizeof(module
->module
.ModuleName
[0]));
107 module_fill_module(name
, module
->modulename
, sizeof(module
->modulename
) / sizeof(module
->modulename
[0]));
110 const WCHAR
*get_wine_loader_name(void)
112 static const BOOL is_win64
= sizeof(void *) > sizeof(int); /* FIXME: should depend on target process */
113 static const WCHAR wineW
[] = {'w','i','n','e',0};
114 static const WCHAR suffixW
[] = {'6','4',0};
115 static const WCHAR
*loader
;
122 /* All binaries are loaded with WINELOADER (if run from tree) or by the
125 if ((ptr
= getenv("WINELOADER")))
127 DWORD len
= 2 + MultiByteToWideChar( CP_UNIXCP
, 0, ptr
, -1, NULL
, 0 );
128 buffer
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
129 MultiByteToWideChar( CP_UNIXCP
, 0, ptr
, -1, buffer
, len
);
133 buffer
= HeapAlloc( GetProcessHeap(), 0, sizeof(wineW
) + 2 * sizeof(WCHAR
) );
134 strcpyW( buffer
, wineW
);
136 p
= buffer
+ strlenW( buffer
) - strlenW( suffixW
);
137 if (p
> buffer
&& !strcmpW( p
, suffixW
))
139 if (!is_win64
) *p
= 0;
141 else if (is_win64
) strcatW( buffer
, suffixW
);
143 TRACE( "returning %s\n", debugstr_w(buffer
) );
149 static const char* get_module_type(enum module_type type
, BOOL
virtual)
153 case DMT_ELF
: return virtual ? "Virtual ELF" : "ELF";
154 case DMT_PE
: return virtual ? "Virtual PE" : "PE";
155 case DMT_MACHO
: return virtual ? "Virtual Mach-O" : "Mach-O";
156 default: return "---";
160 /***********************************************************************
161 * Creates and links a new module to a process
163 struct module
* module_new(struct process
* pcs
, const WCHAR
* name
,
164 enum module_type type
, BOOL
virtual,
165 DWORD64 mod_addr
, DWORD64 size
,
166 unsigned long stamp
, unsigned long checksum
)
168 struct module
* module
;
171 assert(type
== DMT_ELF
|| type
== DMT_PE
|| type
== DMT_MACHO
);
172 if (!(module
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*module
))))
175 module
->next
= pcs
->lmodules
;
176 pcs
->lmodules
= module
;
178 TRACE("=> %s %s-%s %s\n",
179 get_module_type(type
, virtual),
180 wine_dbgstr_longlong(mod_addr
), wine_dbgstr_longlong(mod_addr
+ size
),
183 pool_init(&module
->pool
, 65536);
185 module
->process
= pcs
;
186 module
->module
.SizeOfStruct
= sizeof(module
->module
);
187 module
->module
.BaseOfImage
= mod_addr
;
188 module
->module
.ImageSize
= size
;
189 module_set_module(module
, name
);
190 module
->module
.ImageName
[0] = '\0';
191 lstrcpynW(module
->module
.LoadedImageName
, name
, sizeof(module
->module
.LoadedImageName
) / sizeof(WCHAR
));
192 module
->module
.SymType
= SymNone
;
193 module
->module
.NumSyms
= 0;
194 module
->module
.TimeDateStamp
= stamp
;
195 module
->module
.CheckSum
= checksum
;
197 memset(module
->module
.LoadedPdbName
, 0, sizeof(module
->module
.LoadedPdbName
));
198 module
->module
.CVSig
= 0;
199 memset(module
->module
.CVData
, 0, sizeof(module
->module
.CVData
));
200 module
->module
.PdbSig
= 0;
201 memset(&module
->module
.PdbSig70
, 0, sizeof(module
->module
.PdbSig70
));
202 module
->module
.PdbAge
= 0;
203 module
->module
.PdbUnmatched
= FALSE
;
204 module
->module
.DbgUnmatched
= FALSE
;
205 module
->module
.LineNumbers
= FALSE
;
206 module
->module
.GlobalSymbols
= FALSE
;
207 module
->module
.TypeInfo
= FALSE
;
208 module
->module
.SourceIndexed
= FALSE
;
209 module
->module
.Publics
= FALSE
;
211 module
->reloc_delta
= 0;
213 module
->is_virtual
= virtual;
214 for (i
= 0; i
< DFI_LAST
; i
++) module
->format_info
[i
] = NULL
;
215 module
->sortlist_valid
= FALSE
;
216 module
->sorttab_size
= 0;
217 module
->addr_sorttab
= NULL
;
218 module
->num_sorttab
= 0;
219 module
->num_symbols
= 0;
221 vector_init(&module
->vsymt
, sizeof(struct symt
*), 128);
222 /* FIXME: this seems a bit too high (on a per module basis)
223 * need some statistics about this
225 hash_table_init(&module
->pool
, &module
->ht_symbols
, 4096);
226 hash_table_init(&module
->pool
, &module
->ht_types
, 4096);
228 hash_table_init(&module
->pool
, &module
->ht_symaddr
, 4096);
230 vector_init(&module
->vtypes
, sizeof(struct symt
*), 32);
232 module
->sources_used
= 0;
233 module
->sources_alloc
= 0;
235 wine_rb_init(&module
->sources_offsets_tree
, source_rb_compare
);
240 /***********************************************************************
241 * module_find_by_nameW
244 struct module
* module_find_by_nameW(const struct process
* pcs
, const WCHAR
* name
)
246 struct module
* module
;
248 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
250 if (!strcmpiW(name
, module
->module
.ModuleName
)) return module
;
252 SetLastError(ERROR_INVALID_NAME
);
256 struct module
* module_find_by_nameA(const struct process
* pcs
, const char* name
)
258 WCHAR wname
[MAX_PATH
];
260 MultiByteToWideChar(CP_ACP
, 0, name
, -1, wname
, sizeof(wname
) / sizeof(WCHAR
));
261 return module_find_by_nameW(pcs
, wname
);
264 /***********************************************************************
265 * module_is_already_loaded
268 struct module
* module_is_already_loaded(const struct process
* pcs
, const WCHAR
* name
)
270 struct module
* module
;
271 const WCHAR
* filename
;
273 /* first compare the loaded image name... */
274 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
276 if (!strcmpiW(name
, module
->module
.LoadedImageName
))
279 /* then compare the standard filenames (without the path) ... */
280 filename
= get_filename(name
, NULL
);
281 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
283 if (!strcmpiW(filename
, get_filename(module
->module
.LoadedImageName
, NULL
)))
286 SetLastError(ERROR_INVALID_NAME
);
290 /***********************************************************************
291 * module_get_container
294 static struct module
* module_get_container(const struct process
* pcs
,
295 const struct module
* inner
)
297 struct module
* module
;
299 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
301 if (module
!= inner
&&
302 module
->module
.BaseOfImage
<= inner
->module
.BaseOfImage
&&
303 module
->module
.BaseOfImage
+ module
->module
.ImageSize
>=
304 inner
->module
.BaseOfImage
+ inner
->module
.ImageSize
)
310 /***********************************************************************
311 * module_get_containee
314 struct module
* module_get_containee(const struct process
* pcs
,
315 const struct module
* outter
)
317 struct module
* module
;
319 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
321 if (module
!= outter
&&
322 outter
->module
.BaseOfImage
<= module
->module
.BaseOfImage
&&
323 outter
->module
.BaseOfImage
+ outter
->module
.ImageSize
>=
324 module
->module
.BaseOfImage
+ module
->module
.ImageSize
)
330 /******************************************************************
333 * get the debug information from a module:
334 * - if the module's type is deferred, then force loading of debug info (and return
336 * - if the module has no debug info and has an ELF container, then return the ELF
337 * container (and also force the ELF container's debug info loading if deferred)
338 * - otherwise return the module itself if it has some debug info
340 BOOL
module_get_debug(struct module_pair
* pair
)
342 IMAGEHLP_DEFERRED_SYMBOL_LOADW64 idslW64
;
344 if (!pair
->requested
) return FALSE
;
345 /* for a PE builtin, always get info from container */
346 if (!(pair
->effective
= module_get_container(pair
->pcs
, pair
->requested
)))
347 pair
->effective
= pair
->requested
;
348 /* if deferred, force loading */
349 if (pair
->effective
->module
.SymType
== SymDeferred
)
353 if (pair
->effective
->is_virtual
) ret
= FALSE
;
354 else switch (pair
->effective
->type
)
356 #ifndef DBGHELP_STATIC_LIB
358 ret
= elf_load_debug_info(pair
->effective
);
362 idslW64
.SizeOfStruct
= sizeof(idslW64
);
363 idslW64
.BaseOfImage
= pair
->effective
->module
.BaseOfImage
;
364 idslW64
.CheckSum
= pair
->effective
->module
.CheckSum
;
365 idslW64
.TimeDateStamp
= pair
->effective
->module
.TimeDateStamp
;
366 memcpy(idslW64
.FileName
, pair
->effective
->module
.ImageName
,
367 sizeof(pair
->effective
->module
.ImageName
));
368 idslW64
.Reparse
= FALSE
;
369 idslW64
.hFile
= INVALID_HANDLE_VALUE
;
371 pcs_callback(pair
->pcs
, CBA_DEFERRED_SYMBOL_LOAD_START
, &idslW64
);
372 ret
= pe_load_debug_info(pair
->pcs
, pair
->effective
);
373 pcs_callback(pair
->pcs
,
374 ret
? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE
: CBA_DEFERRED_SYMBOL_LOAD_FAILURE
,
377 #ifndef DBGHELP_STATIC_LIB
379 ret
= macho_load_debug_info(pair
->effective
);
386 if (!ret
) pair
->effective
->module
.SymType
= SymNone
;
387 assert(pair
->effective
->module
.SymType
!= SymDeferred
);
388 pair
->effective
->module
.NumSyms
= pair
->effective
->ht_symbols
.num_elts
;
390 return pair
->effective
->module
.SymType
!= SymNone
;
393 /***********************************************************************
394 * module_find_by_addr
396 * either the addr where module is loaded, or any address inside the
399 struct module
* module_find_by_addr(const struct process
* pcs
, DWORD64 addr
,
400 enum module_type type
)
402 struct module
* module
;
404 if (type
== DMT_UNKNOWN
)
406 if ((module
= module_find_by_addr(pcs
, addr
, DMT_PE
)) ||
407 (module
= module_find_by_addr(pcs
, addr
, DMT_ELF
)) ||
408 (module
= module_find_by_addr(pcs
, addr
, DMT_MACHO
)))
413 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
415 if (type
== module
->type
&& addr
>= module
->module
.BaseOfImage
&&
416 addr
< module
->module
.BaseOfImage
+ module
->module
.ImageSize
)
420 SetLastError(ERROR_INVALID_ADDRESS
);
424 /******************************************************************
425 * module_is_container_loaded
427 * checks whether the native container, for a (supposed) PE builtin is
430 static BOOL
module_is_container_loaded(const struct process
* pcs
,
431 const WCHAR
* ImageName
, DWORD64 base
)
434 struct module
* module
;
435 PCWSTR filename
, modname
;
437 if (!base
) return FALSE
;
438 filename
= get_filename(ImageName
, NULL
);
439 len
= strlenW(filename
);
441 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
443 if ((module
->type
== DMT_ELF
|| module
->type
== DMT_MACHO
) &&
444 base
>= module
->module
.BaseOfImage
&&
445 base
< module
->module
.BaseOfImage
+ module
->module
.ImageSize
)
447 modname
= get_filename(module
->module
.LoadedImageName
, NULL
);
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
&& name
[i
- 1] >= '0' && name
[i
- 1] <= '9') 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 HeapFree(GetProcessHeap(), 0, module
->sources
);
700 HeapFree(GetProcessHeap(), 0, module
->addr_sorttab
);
701 pool_destroy(&module
->pool
);
702 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
705 for (p
= &pcs
->lmodules
; *p
; p
= &(*p
)->next
)
710 HeapFree(GetProcessHeap(), 0, module
);
714 FIXME("This shouldn't happen\n");
718 /******************************************************************
719 * SymUnloadModule (DBGHELP.@)
722 BOOL WINAPI
SymUnloadModule(HANDLE hProcess
, DWORD BaseOfDll
)
725 struct module
* module
;
727 pcs
= process_find_by_handle(hProcess
);
728 if (!pcs
) return FALSE
;
729 module
= module_find_by_addr(pcs
, BaseOfDll
, DMT_UNKNOWN
);
730 if (!module
) return FALSE
;
731 return module_remove(pcs
, module
);
734 /******************************************************************
735 * SymUnloadModule64 (DBGHELP.@)
738 BOOL WINAPI
SymUnloadModule64(HANDLE hProcess
, DWORD64 BaseOfDll
)
741 struct module
* module
;
743 pcs
= process_find_by_handle(hProcess
);
744 if (!pcs
) return FALSE
;
745 if (!validate_addr64(BaseOfDll
)) return FALSE
;
746 module
= module_find_by_addr(pcs
, BaseOfDll
, DMT_UNKNOWN
);
747 if (!module
) return FALSE
;
748 return module_remove(pcs
, module
);
751 /******************************************************************
752 * SymEnumerateModules (DBGHELP.@)
755 struct enum_modW64_32
757 PSYM_ENUMMODULES_CALLBACK cb
;
759 char module
[MAX_PATH
];
762 static BOOL CALLBACK
enum_modW64_32(PCWSTR name
, DWORD64 base
, PVOID user
)
764 struct enum_modW64_32
* x
= user
;
766 WideCharToMultiByte(CP_ACP
, 0, name
, -1, x
->module
, sizeof(x
->module
), NULL
, NULL
);
767 return x
->cb(x
->module
, (DWORD
)base
, x
->user
);
770 BOOL WINAPI
SymEnumerateModules(HANDLE hProcess
,
771 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback
,
774 struct enum_modW64_32 x
;
776 x
.cb
= EnumModulesCallback
;
777 x
.user
= UserContext
;
779 return SymEnumerateModulesW64(hProcess
, enum_modW64_32
, &x
);
782 /******************************************************************
783 * SymEnumerateModules64 (DBGHELP.@)
786 struct enum_modW64_64
788 PSYM_ENUMMODULES_CALLBACK64 cb
;
790 char module
[MAX_PATH
];
793 static BOOL CALLBACK
enum_modW64_64(PCWSTR name
, DWORD64 base
, PVOID user
)
795 struct enum_modW64_64
* x
= user
;
797 WideCharToMultiByte(CP_ACP
, 0, name
, -1, x
->module
, sizeof(x
->module
), NULL
, NULL
);
798 return x
->cb(x
->module
, base
, x
->user
);
801 BOOL WINAPI
SymEnumerateModules64(HANDLE hProcess
,
802 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback
,
805 struct enum_modW64_64 x
;
807 x
.cb
= EnumModulesCallback
;
808 x
.user
= UserContext
;
810 return SymEnumerateModulesW64(hProcess
, enum_modW64_64
, &x
);
813 /******************************************************************
814 * SymEnumerateModulesW64 (DBGHELP.@)
817 BOOL WINAPI
SymEnumerateModulesW64(HANDLE hProcess
,
818 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback
,
821 struct process
* pcs
= process_find_by_handle(hProcess
);
822 struct module
* module
;
824 if (!pcs
) return FALSE
;
826 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
828 if (!(dbghelp_options
& SYMOPT_WINE_WITH_NATIVE_MODULES
) &&
829 (module
->type
== DMT_ELF
|| module
->type
== DMT_MACHO
))
831 if (!EnumModulesCallback(module
->modulename
,
832 module
->module
.BaseOfImage
, UserContext
))
838 #ifndef DBGHELP_STATIC_LIB
839 /******************************************************************
840 * EnumerateLoadedModules64 (DBGHELP.@)
843 struct enum_load_modW64_64
845 PENUMLOADED_MODULES_CALLBACK64 cb
;
847 char module
[MAX_PATH
];
850 static BOOL CALLBACK
enum_load_modW64_64(PCWSTR name
, DWORD64 base
, ULONG size
,
853 struct enum_load_modW64_64
* x
= user
;
855 WideCharToMultiByte(CP_ACP
, 0, name
, -1, x
->module
, sizeof(x
->module
), NULL
, NULL
);
856 return x
->cb(x
->module
, base
, size
, x
->user
);
859 BOOL WINAPI
EnumerateLoadedModules64(HANDLE hProcess
,
860 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback
,
863 struct enum_load_modW64_64 x
;
865 x
.cb
= EnumLoadedModulesCallback
;
866 x
.user
= UserContext
;
868 return EnumerateLoadedModulesW64(hProcess
, enum_load_modW64_64
, &x
);
871 /******************************************************************
872 * EnumerateLoadedModules (DBGHELP.@)
875 struct enum_load_modW64_32
877 PENUMLOADED_MODULES_CALLBACK cb
;
879 char module
[MAX_PATH
];
882 static BOOL CALLBACK
enum_load_modW64_32(PCWSTR name
, DWORD64 base
, ULONG size
,
885 struct enum_load_modW64_32
* x
= user
;
886 WideCharToMultiByte(CP_ACP
, 0, name
, -1, x
->module
, sizeof(x
->module
), NULL
, NULL
);
887 return x
->cb(x
->module
, (DWORD
)base
, size
, x
->user
);
890 BOOL WINAPI
EnumerateLoadedModules(HANDLE hProcess
,
891 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback
,
894 struct enum_load_modW64_32 x
;
896 x
.cb
= EnumLoadedModulesCallback
;
897 x
.user
= UserContext
;
899 return EnumerateLoadedModulesW64(hProcess
, enum_load_modW64_32
, &x
);
902 /******************************************************************
903 * EnumerateLoadedModulesW64 (DBGHELP.@)
906 BOOL WINAPI
EnumerateLoadedModulesW64(HANDLE hProcess
,
907 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback
,
911 WCHAR baseW
[256], modW
[256];
915 hMods
= HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods
[0]));
916 if (!hMods
) return FALSE
;
918 if (!EnumProcessModules(hProcess
, hMods
, 256 * sizeof(hMods
[0]), &sz
))
920 /* hProcess should also be a valid process handle !! */
921 FIXME("If this happens, bump the number in mod\n");
922 HeapFree(GetProcessHeap(), 0, hMods
);
925 sz
/= sizeof(HMODULE
);
926 for (i
= 0; i
< sz
; i
++)
928 if (!GetModuleInformation(hProcess
, hMods
[i
], &mi
, sizeof(mi
)) ||
929 !GetModuleBaseNameW(hProcess
, hMods
[i
], baseW
, sizeof(baseW
) / sizeof(WCHAR
)))
931 module_fill_module(baseW
, modW
, sizeof(modW
) / sizeof(modW
[0]));
932 EnumLoadedModulesCallback(modW
, (DWORD_PTR
)mi
.lpBaseOfDll
, mi
.SizeOfImage
,
935 HeapFree(GetProcessHeap(), 0, hMods
);
937 return sz
!= 0 && i
== sz
;
940 #endif /* DBGHELP_STATIC_LIB */
942 static void dbghelp_str_WtoA(const WCHAR
*src
, char *dst
, int dst_len
)
944 WideCharToMultiByte(CP_ACP
, 0, src
, -1, dst
, dst_len
- 1, NULL
, NULL
);
945 dst
[dst_len
- 1] = 0;
948 /******************************************************************
949 * SymGetModuleInfo (DBGHELP.@)
952 BOOL WINAPI
SymGetModuleInfo(HANDLE hProcess
, DWORD dwAddr
,
953 PIMAGEHLP_MODULE ModuleInfo
)
956 IMAGEHLP_MODULEW64 miw64
;
958 if (sizeof(mi
) < ModuleInfo
->SizeOfStruct
) FIXME("Wrong size\n");
960 miw64
.SizeOfStruct
= sizeof(miw64
);
961 if (!SymGetModuleInfoW64(hProcess
, dwAddr
, &miw64
)) return FALSE
;
963 mi
.SizeOfStruct
= ModuleInfo
->SizeOfStruct
;
964 mi
.BaseOfImage
= miw64
.BaseOfImage
;
965 mi
.ImageSize
= miw64
.ImageSize
;
966 mi
.TimeDateStamp
= miw64
.TimeDateStamp
;
967 mi
.CheckSum
= miw64
.CheckSum
;
968 mi
.NumSyms
= miw64
.NumSyms
;
969 mi
.SymType
= miw64
.SymType
;
970 dbghelp_str_WtoA(miw64
.ModuleName
, mi
.ModuleName
, sizeof(mi
.ModuleName
));
971 dbghelp_str_WtoA(miw64
.ImageName
, mi
.ImageName
, sizeof(mi
.ImageName
));
972 dbghelp_str_WtoA(miw64
.LoadedImageName
, mi
.LoadedImageName
, sizeof(mi
.LoadedImageName
));
974 memcpy(ModuleInfo
, &mi
, ModuleInfo
->SizeOfStruct
);
979 /******************************************************************
980 * SymGetModuleInfoW (DBGHELP.@)
983 BOOL WINAPI
SymGetModuleInfoW(HANDLE hProcess
, DWORD dwAddr
,
984 PIMAGEHLP_MODULEW ModuleInfo
)
986 IMAGEHLP_MODULEW64 miw64
;
987 IMAGEHLP_MODULEW miw
;
989 if (sizeof(miw
) < ModuleInfo
->SizeOfStruct
) FIXME("Wrong size\n");
991 miw64
.SizeOfStruct
= sizeof(miw64
);
992 if (!SymGetModuleInfoW64(hProcess
, dwAddr
, &miw64
)) return FALSE
;
994 miw
.SizeOfStruct
= ModuleInfo
->SizeOfStruct
;
995 miw
.BaseOfImage
= miw64
.BaseOfImage
;
996 miw
.ImageSize
= miw64
.ImageSize
;
997 miw
.TimeDateStamp
= miw64
.TimeDateStamp
;
998 miw
.CheckSum
= miw64
.CheckSum
;
999 miw
.NumSyms
= miw64
.NumSyms
;
1000 miw
.SymType
= miw64
.SymType
;
1001 strcpyW(miw
.ModuleName
, miw64
.ModuleName
);
1002 strcpyW(miw
.ImageName
, miw64
.ImageName
);
1003 strcpyW(miw
.LoadedImageName
, miw64
.LoadedImageName
);
1004 memcpy(ModuleInfo
, &miw
, ModuleInfo
->SizeOfStruct
);
1009 /******************************************************************
1010 * SymGetModuleInfo64 (DBGHELP.@)
1013 BOOL WINAPI
SymGetModuleInfo64(HANDLE hProcess
, DWORD64 dwAddr
,
1014 PIMAGEHLP_MODULE64 ModuleInfo
)
1016 IMAGEHLP_MODULE64 mi64
;
1017 IMAGEHLP_MODULEW64 miw64
;
1019 if (sizeof(mi64
) < ModuleInfo
->SizeOfStruct
)
1021 SetLastError(ERROR_MOD_NOT_FOUND
); /* NOTE: native returns this error */
1022 WARN("Wrong size %u\n", ModuleInfo
->SizeOfStruct
);
1026 miw64
.SizeOfStruct
= sizeof(miw64
);
1027 if (!SymGetModuleInfoW64(hProcess
, dwAddr
, &miw64
)) return FALSE
;
1029 mi64
.SizeOfStruct
= ModuleInfo
->SizeOfStruct
;
1030 mi64
.BaseOfImage
= miw64
.BaseOfImage
;
1031 mi64
.ImageSize
= miw64
.ImageSize
;
1032 mi64
.TimeDateStamp
= miw64
.TimeDateStamp
;
1033 mi64
.CheckSum
= miw64
.CheckSum
;
1034 mi64
.NumSyms
= miw64
.NumSyms
;
1035 mi64
.SymType
= miw64
.SymType
;
1036 dbghelp_str_WtoA(miw64
.ModuleName
, mi64
.ModuleName
, sizeof(mi64
.ModuleName
));
1037 dbghelp_str_WtoA(miw64
.ImageName
, mi64
.ImageName
, sizeof(mi64
.ImageName
));
1038 dbghelp_str_WtoA(miw64
.LoadedImageName
, mi64
.LoadedImageName
, sizeof(mi64
.LoadedImageName
));
1039 dbghelp_str_WtoA(miw64
.LoadedPdbName
, mi64
.LoadedPdbName
, sizeof(mi64
.LoadedPdbName
));
1041 mi64
.CVSig
= miw64
.CVSig
;
1042 dbghelp_str_WtoA(miw64
.CVData
, mi64
.CVData
, sizeof(mi64
.CVData
));
1043 mi64
.PdbSig
= miw64
.PdbSig
;
1044 mi64
.PdbSig70
= miw64
.PdbSig70
;
1045 mi64
.PdbAge
= miw64
.PdbAge
;
1046 mi64
.PdbUnmatched
= miw64
.PdbUnmatched
;
1047 mi64
.DbgUnmatched
= miw64
.DbgUnmatched
;
1048 mi64
.LineNumbers
= miw64
.LineNumbers
;
1049 mi64
.GlobalSymbols
= miw64
.GlobalSymbols
;
1050 mi64
.TypeInfo
= miw64
.TypeInfo
;
1051 mi64
.SourceIndexed
= miw64
.SourceIndexed
;
1052 mi64
.Publics
= miw64
.Publics
;
1054 memcpy(ModuleInfo
, &mi64
, ModuleInfo
->SizeOfStruct
);
1059 /******************************************************************
1060 * SymGetModuleInfoW64 (DBGHELP.@)
1063 BOOL WINAPI
SymGetModuleInfoW64(HANDLE hProcess
, DWORD64 dwAddr
,
1064 PIMAGEHLP_MODULEW64 ModuleInfo
)
1066 struct process
* pcs
= process_find_by_handle(hProcess
);
1067 struct module
* module
;
1068 IMAGEHLP_MODULEW64 miw64
;
1070 TRACE("%p %s %p\n", hProcess
, wine_dbgstr_longlong(dwAddr
), ModuleInfo
);
1072 if (!pcs
) return FALSE
;
1073 if (ModuleInfo
->SizeOfStruct
> sizeof(*ModuleInfo
)) return FALSE
;
1074 module
= module_find_by_addr(pcs
, dwAddr
, DMT_UNKNOWN
);
1075 if (!module
) return FALSE
;
1077 miw64
= module
->module
;
1079 /* update debug information from container if any */
1080 if (module
->module
.SymType
== SymNone
)
1082 module
= module_get_container(pcs
, module
);
1083 if (module
&& module
->module
.SymType
!= SymNone
)
1085 miw64
.SymType
= module
->module
.SymType
;
1086 miw64
.NumSyms
= module
->module
.NumSyms
;
1089 memcpy(ModuleInfo
, &miw64
, ModuleInfo
->SizeOfStruct
);
1093 /***********************************************************************
1094 * SymGetModuleBase (DBGHELP.@)
1096 DWORD WINAPI
SymGetModuleBase(HANDLE hProcess
, DWORD dwAddr
)
1100 ret
= SymGetModuleBase64(hProcess
, dwAddr
);
1101 return validate_addr64(ret
) ? ret
: 0;
1104 /***********************************************************************
1105 * SymGetModuleBase64 (DBGHELP.@)
1107 DWORD64 WINAPI
SymGetModuleBase64(HANDLE hProcess
, DWORD64 dwAddr
)
1109 struct process
* pcs
= process_find_by_handle(hProcess
);
1110 struct module
* module
;
1113 module
= module_find_by_addr(pcs
, dwAddr
, DMT_UNKNOWN
);
1114 if (!module
) return 0;
1115 return module
->module
.BaseOfImage
;
1118 /******************************************************************
1119 * module_reset_debug_info
1120 * Removes any debug information linked to a given module.
1122 void module_reset_debug_info(struct module
* module
)
1124 module
->sortlist_valid
= TRUE
;
1125 module
->sorttab_size
= 0;
1126 module
->addr_sorttab
= NULL
;
1127 module
->num_sorttab
= module
->num_symbols
= 0;
1128 hash_table_destroy(&module
->ht_symbols
);
1129 module
->ht_symbols
.num_buckets
= 0;
1130 module
->ht_symbols
.buckets
= NULL
;
1131 hash_table_destroy(&module
->ht_types
);
1132 module
->ht_types
.num_buckets
= 0;
1133 module
->ht_types
.buckets
= NULL
;
1134 module
->vtypes
.num_elts
= 0;
1135 hash_table_destroy(&module
->ht_symbols
);
1136 module
->sources_used
= module
->sources_alloc
= 0;
1137 module
->sources
= NULL
;
1140 /******************************************************************
1141 * SymRefreshModuleList (DBGHELP.@)
1143 BOOL WINAPI
SymRefreshModuleList(HANDLE hProcess
)
1145 struct process
* pcs
;
1147 TRACE("(%p)\n", hProcess
);
1149 if (!(pcs
= process_find_by_handle(hProcess
))) return FALSE
;
1151 #ifndef DBGHELP_STATIC_LIB
1152 return refresh_module_list(pcs
);
1158 /***********************************************************************
1159 * SymFunctionTableAccess (DBGHELP.@)
1161 PVOID WINAPI
SymFunctionTableAccess(HANDLE hProcess
, DWORD AddrBase
)
1163 return SymFunctionTableAccess64(hProcess
, AddrBase
);
1166 /***********************************************************************
1167 * SymFunctionTableAccess64 (DBGHELP.@)
1169 PVOID WINAPI
SymFunctionTableAccess64(HANDLE hProcess
, DWORD64 AddrBase
)
1171 struct process
* pcs
= process_find_by_handle(hProcess
);
1172 struct module
* module
;
1174 if (!pcs
|| !dbghelp_current_cpu
->find_runtime_function
) return NULL
;
1175 module
= module_find_by_addr(pcs
, AddrBase
, DMT_UNKNOWN
);
1176 if (!module
) return NULL
;
1178 return dbghelp_current_cpu
->find_runtime_function(module
, AddrBase
);