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