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"
33 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
35 const WCHAR S_ElfW
[] = {'<','e','l','f','>','\0'};
36 const WCHAR S_WineLoaderW
[] = {'<','w','i','n','e','-','l','o','a','d','e','r','>','\0'};
37 static const WCHAR S_DotSoW
[] = {'.','s','o','\0'};
38 static const WCHAR S_DotDylibW
[] = {'.','d','y','l','i','b','\0'};
39 static const WCHAR S_DotPdbW
[] = {'.','p','d','b','\0'};
40 static const WCHAR S_DotDbgW
[] = {'.','d','b','g','\0'};
41 const WCHAR S_WineW
[] = {'w','i','n','e',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 FALSE
;
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 void module_fill_module(const WCHAR
* in
, WCHAR
* out
, size_t size
)
81 const WCHAR
*ptr
, *endptr
;
84 ptr
= get_filename(in
, endptr
= in
+ strlenW(in
));
85 len
= min(endptr
- ptr
, size
- 1);
86 memcpy(out
, ptr
, len
* sizeof(WCHAR
));
88 if (len
> 4 && (l
= match_ext(out
, len
)))
90 else if (len
> 4 && !strcmpiW(out
+ len
- 4, S_WineW
))
91 lstrcpynW(out
, S_WineLoaderW
, size
);
94 if (len
> 3 && !strcmpiW(&out
[len
- 3], S_DotSoW
) &&
95 (l
= match_ext(out
, len
- 3)))
96 strcpyW(&out
[len
- l
- 3], S_ElfW
);
98 while ((*out
= tolowerW(*out
))) out
++;
101 void module_set_module(struct module
* module
, const WCHAR
* name
)
103 module_fill_module(name
, module
->module
.ModuleName
, sizeof(module
->module
.ModuleName
));
104 WideCharToMultiByte(CP_ACP
, 0, module
->module
.ModuleName
, -1,
105 module
->module_name
, sizeof(module
->module_name
),
109 static const char* get_module_type(enum module_type type
, BOOL
virtual)
113 case DMT_ELF
: return virtual ? "Virtual ELF" : "ELF";
114 case DMT_PE
: return virtual ? "Virtual PE" : "PE";
115 case DMT_MACHO
: return virtual ? "Virtual Mach-O" : "Mach-O";
116 default: return "---";
120 /***********************************************************************
121 * Creates and links a new module to a process
123 struct module
* module_new(struct process
* pcs
, const WCHAR
* name
,
124 enum module_type type
, BOOL
virtual,
125 DWORD64 mod_addr
, DWORD64 size
,
126 unsigned long stamp
, unsigned long checksum
)
128 struct module
* module
;
131 assert(type
== DMT_ELF
|| type
== DMT_PE
|| type
== DMT_MACHO
);
132 if (!(module
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*module
))))
135 module
->next
= pcs
->lmodules
;
136 pcs
->lmodules
= module
;
138 TRACE("=> %s %s-%s %s\n",
139 get_module_type(type
, virtual),
140 wine_dbgstr_longlong(mod_addr
), wine_dbgstr_longlong(mod_addr
+ size
),
143 pool_init(&module
->pool
, 65536);
145 module
->module
.SizeOfStruct
= sizeof(module
->module
);
146 module
->module
.BaseOfImage
= mod_addr
;
147 module
->module
.ImageSize
= size
;
148 module_set_module(module
, name
);
149 module
->module
.ImageName
[0] = '\0';
150 lstrcpynW(module
->module
.LoadedImageName
, name
, sizeof(module
->module
.LoadedImageName
) / sizeof(WCHAR
));
151 module
->module
.SymType
= SymNone
;
152 module
->module
.NumSyms
= 0;
153 module
->module
.TimeDateStamp
= stamp
;
154 module
->module
.CheckSum
= checksum
;
156 memset(module
->module
.LoadedPdbName
, 0, sizeof(module
->module
.CVData
));
157 module
->module
.CVSig
= 0;
158 memset(module
->module
.CVData
, 0, sizeof(module
->module
.CVData
));
159 module
->module
.PdbSig
= 0;
160 memset(&module
->module
.PdbSig70
, 0, sizeof(module
->module
.PdbSig70
));
161 module
->module
.PdbAge
= 0;
162 module
->module
.PdbUnmatched
= FALSE
;
163 module
->module
.DbgUnmatched
= FALSE
;
164 module
->module
.LineNumbers
= FALSE
;
165 module
->module
.GlobalSymbols
= FALSE
;
166 module
->module
.TypeInfo
= FALSE
;
167 module
->module
.SourceIndexed
= FALSE
;
168 module
->module
.Publics
= FALSE
;
171 module
->is_virtual
= virtual ? TRUE
: FALSE
;
172 for (i
= 0; i
< DFI_LAST
; i
++) module
->format_info
[i
] = NULL
;
173 module
->sortlist_valid
= FALSE
;
174 module
->sorttab_size
= 0;
175 module
->addr_sorttab
= NULL
;
176 module
->num_sorttab
= 0;
177 module
->num_symbols
= 0;
179 vector_init(&module
->vsymt
, sizeof(struct symt
*), 128);
180 /* FIXME: this seems a bit too high (on a per module basis)
181 * need some statistics about this
183 hash_table_init(&module
->pool
, &module
->ht_symbols
, 4096);
184 hash_table_init(&module
->pool
, &module
->ht_types
, 4096);
185 vector_init(&module
->vtypes
, sizeof(struct symt
*), 32);
187 module
->sources_used
= 0;
188 module
->sources_alloc
= 0;
194 /***********************************************************************
195 * module_find_by_name
198 static struct module
* module_find_by_name(const struct process
* pcs
, const WCHAR
* name
)
200 struct module
* module
;
202 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
204 if (!strcmpiW(name
, module
->module
.ModuleName
)) return module
;
206 SetLastError(ERROR_INVALID_NAME
);
210 struct module
* module_find_by_nameA(const struct process
* pcs
, const char* name
)
212 WCHAR wname
[MAX_PATH
];
214 MultiByteToWideChar(CP_ACP
, 0, name
, -1, wname
, sizeof(wname
) / sizeof(WCHAR
));
215 return module_find_by_name(pcs
, wname
);
218 /***********************************************************************
219 * module_is_already_loaded
222 struct module
* module_is_already_loaded(const struct process
* pcs
, const WCHAR
* name
)
224 struct module
* module
;
225 const WCHAR
* filename
;
227 /* first compare the loaded image name... */
228 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
230 if (!strcmpiW(name
, module
->module
.LoadedImageName
))
233 /* then compare the standard filenames (without the path) ... */
234 filename
= get_filename(name
, NULL
);
235 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
237 if (!strcmpiW(filename
, get_filename(module
->module
.LoadedImageName
, NULL
)))
240 SetLastError(ERROR_INVALID_NAME
);
244 /***********************************************************************
245 * module_get_container
248 static struct module
* module_get_container(const struct process
* pcs
,
249 const struct module
* inner
)
251 struct module
* module
;
253 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
255 if (module
!= inner
&&
256 module
->module
.BaseOfImage
<= inner
->module
.BaseOfImage
&&
257 module
->module
.BaseOfImage
+ module
->module
.ImageSize
>=
258 inner
->module
.BaseOfImage
+ inner
->module
.ImageSize
)
264 /***********************************************************************
265 * module_get_containee
268 struct module
* module_get_containee(const struct process
* pcs
,
269 const struct module
* outter
)
271 struct module
* module
;
273 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
275 if (module
!= outter
&&
276 outter
->module
.BaseOfImage
<= module
->module
.BaseOfImage
&&
277 outter
->module
.BaseOfImage
+ outter
->module
.ImageSize
>=
278 module
->module
.BaseOfImage
+ module
->module
.ImageSize
)
284 /******************************************************************
287 * get the debug information from a module:
288 * - if the module's type is deferred, then force loading of debug info (and return
290 * - if the module has no debug info and has an ELF container, then return the ELF
291 * container (and also force the ELF container's debug info loading if deferred)
292 * - otherwise return the module itself if it has some debug info
294 BOOL
module_get_debug(struct module_pair
* pair
)
296 IMAGEHLP_DEFERRED_SYMBOL_LOADW64 idslW64
;
298 if (!pair
->requested
) return FALSE
;
299 /* for a PE builtin, always get info from container */
300 if (!(pair
->effective
= module_get_container(pair
->pcs
, pair
->requested
)))
301 pair
->effective
= pair
->requested
;
302 /* if deferred, force loading */
303 if (pair
->effective
->module
.SymType
== SymDeferred
)
307 if (pair
->effective
->is_virtual
) ret
= FALSE
;
308 else switch (pair
->effective
->type
)
311 ret
= elf_load_debug_info(pair
->effective
, NULL
);
314 idslW64
.SizeOfStruct
= sizeof(idslW64
);
315 idslW64
.BaseOfImage
= pair
->effective
->module
.BaseOfImage
;
316 idslW64
.CheckSum
= pair
->effective
->module
.CheckSum
;
317 idslW64
.TimeDateStamp
= pair
->effective
->module
.TimeDateStamp
;
318 memcpy(idslW64
.FileName
, pair
->effective
->module
.ImageName
,
319 sizeof(pair
->effective
->module
.ImageName
));
320 idslW64
.Reparse
= FALSE
;
321 idslW64
.hFile
= INVALID_HANDLE_VALUE
;
323 pcs_callback(pair
->pcs
, CBA_DEFERRED_SYMBOL_LOAD_START
, &idslW64
);
324 ret
= pe_load_debug_info(pair
->pcs
, pair
->effective
);
325 pcs_callback(pair
->pcs
,
326 ret
? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE
: CBA_DEFERRED_SYMBOL_LOAD_FAILURE
,
330 ret
= macho_load_debug_info(pair
->effective
, NULL
);
336 if (!ret
) pair
->effective
->module
.SymType
= SymNone
;
337 assert(pair
->effective
->module
.SymType
!= SymDeferred
);
338 pair
->effective
->module
.NumSyms
= pair
->effective
->ht_symbols
.num_elts
;
340 return pair
->effective
->module
.SymType
!= SymNone
;
343 /***********************************************************************
344 * module_find_by_addr
346 * either the addr where module is loaded, or any address inside the
349 struct module
* module_find_by_addr(const struct process
* pcs
, unsigned long addr
,
350 enum module_type type
)
352 struct module
* module
;
354 if (type
== DMT_UNKNOWN
)
356 if ((module
= module_find_by_addr(pcs
, addr
, DMT_PE
)) ||
357 (module
= module_find_by_addr(pcs
, addr
, DMT_ELF
)) ||
358 (module
= module_find_by_addr(pcs
, addr
, DMT_MACHO
)))
363 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
365 if (type
== module
->type
&& addr
>= module
->module
.BaseOfImage
&&
366 addr
< module
->module
.BaseOfImage
+ module
->module
.ImageSize
)
370 SetLastError(ERROR_INVALID_ADDRESS
);
374 /******************************************************************
375 * module_is_container_loaded
377 * checks whether the native container, for a (supposed) PE builtin is
380 static BOOL
module_is_container_loaded(const struct process
* pcs
,
381 const WCHAR
* ImageName
, DWORD64 base
)
384 struct module
* module
;
385 PCWSTR filename
, modname
;
387 if (!base
) return FALSE
;
388 filename
= get_filename(ImageName
, NULL
);
389 len
= strlenW(filename
);
391 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
393 if ((module
->type
== DMT_ELF
|| module
->type
== DMT_MACHO
) &&
394 base
>= module
->module
.BaseOfImage
&&
395 base
< module
->module
.BaseOfImage
+ module
->module
.ImageSize
)
397 modname
= get_filename(module
->module
.LoadedImageName
, NULL
);
398 if (!strncmpiW(modname
, filename
, len
) &&
399 !memcmp(modname
+ len
, S_DotSoW
, 3 * sizeof(WCHAR
)))
405 /* likely a native PE module */
406 WARN("Couldn't find container for %s\n", debugstr_w(ImageName
));
410 /******************************************************************
411 * module_get_type_by_name
413 * Guesses a filename type from its extension
415 enum module_type
module_get_type_by_name(const WCHAR
* name
)
417 int len
= strlenW(name
);
419 /* Skip all version extensions (.[digits]) regex: "(\.\d+)*$" */
424 while (i
&& isdigit(name
[i
- 1])) i
--;
426 if (i
&& name
[i
- 1] == '.')
432 /* check for terminating .so or .so.[digit] */
433 /* FIXME: Can't rely solely on extension; have to check magic or
434 * stop using .so on Mac OS X. For now, base on platform. */
435 if (len
> 3 && !memcmp(name
+ len
- 3, S_DotSoW
, 3))
442 if (len
> 6 && !strncmpiW(name
+ len
- 6, S_DotDylibW
, 6))
445 if (len
> 4 && !strncmpiW(name
+ len
- 4, S_DotPdbW
, 4))
448 if (len
> 4 && !strncmpiW(name
+ len
- 4, S_DotDbgW
, 4))
451 /* wine is also a native module (Mach-O on Mac OS X, ELF elsewhere) */
452 if (((len
> 4 && name
[len
- 5] == '/') || len
== 4) && !strcmpiW(name
+ len
- 4, S_WineW
))
463 /******************************************************************
464 * refresh_module_list
466 static BOOL
refresh_module_list(struct process
* pcs
)
468 /* force transparent ELF and Mach-O loading / unloading */
469 return elf_synchronize_module_list(pcs
) || macho_synchronize_module_list(pcs
);
472 /***********************************************************************
473 * SymLoadModule (DBGHELP.@)
475 DWORD WINAPI
SymLoadModule(HANDLE hProcess
, HANDLE hFile
, PCSTR ImageName
,
476 PCSTR ModuleName
, DWORD BaseOfDll
, DWORD SizeOfDll
)
478 return SymLoadModuleEx(hProcess
, hFile
, ImageName
, ModuleName
, BaseOfDll
,
482 /***********************************************************************
483 * SymLoadModuleEx (DBGHELP.@)
485 DWORD64 WINAPI
SymLoadModuleEx(HANDLE hProcess
, HANDLE hFile
, PCSTR ImageName
,
486 PCSTR ModuleName
, DWORD64 BaseOfDll
, DWORD DllSize
,
487 PMODLOAD_DATA Data
, DWORD Flags
)
489 PWSTR wImageName
, wModuleName
;
493 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
494 hProcess
, hFile
, debugstr_a(ImageName
), debugstr_a(ModuleName
),
495 wine_dbgstr_longlong(BaseOfDll
), DllSize
, Data
, Flags
);
499 len
= MultiByteToWideChar(CP_ACP
, 0, ImageName
, -1, NULL
, 0);
500 wImageName
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
501 MultiByteToWideChar(CP_ACP
, 0, ImageName
, -1, wImageName
, len
);
503 else wImageName
= NULL
;
506 len
= MultiByteToWideChar(CP_ACP
, 0, ModuleName
, -1, NULL
, 0);
507 wModuleName
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
508 MultiByteToWideChar(CP_ACP
, 0, ModuleName
, -1, wModuleName
, len
);
510 else wModuleName
= NULL
;
512 ret
= SymLoadModuleExW(hProcess
, hFile
, wImageName
, wModuleName
,
513 BaseOfDll
, DllSize
, Data
, Flags
);
514 HeapFree(GetProcessHeap(), 0, wImageName
);
515 HeapFree(GetProcessHeap(), 0, wModuleName
);
519 /***********************************************************************
520 * SymLoadModuleExW (DBGHELP.@)
522 DWORD64 WINAPI
SymLoadModuleExW(HANDLE hProcess
, HANDLE hFile
, PCWSTR wImageName
,
523 PCWSTR wModuleName
, DWORD64 BaseOfDll
, DWORD SizeOfDll
,
524 PMODLOAD_DATA Data
, DWORD Flags
)
527 struct module
* module
= NULL
;
529 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
530 hProcess
, hFile
, debugstr_w(wImageName
), debugstr_w(wModuleName
),
531 wine_dbgstr_longlong(BaseOfDll
), SizeOfDll
, Data
, Flags
);
534 FIXME("Unsupported load data parameter %p for %s\n",
535 Data
, debugstr_w(wImageName
));
536 if (!validate_addr64(BaseOfDll
)) return FALSE
;
538 if (!(pcs
= process_find_by_handle(hProcess
))) return FALSE
;
540 if (Flags
& SLMFLAG_VIRTUAL
)
542 if (!wImageName
) return FALSE
;
543 module
= module_new(pcs
, wImageName
, module_get_type_by_name(wImageName
),
544 TRUE
, BaseOfDll
, SizeOfDll
, 0, 0);
545 if (!module
) return FALSE
;
546 if (wModuleName
) module_set_module(module
, wModuleName
);
547 module
->module
.SymType
= SymVirtual
;
551 if (Flags
& ~(SLMFLAG_VIRTUAL
))
552 FIXME("Unsupported Flags %08x for %s\n", Flags
, debugstr_w(wImageName
));
554 refresh_module_list(pcs
);
556 /* this is a Wine extension to the API just to redo the synchronisation */
557 if (!wImageName
&& !hFile
) return 0;
559 /* check if the module is already loaded, or if it's a builtin PE module with
560 * an containing ELF module
564 module
= module_is_already_loaded(pcs
, wImageName
);
565 if (!module
&& module_is_container_loaded(pcs
, wImageName
, BaseOfDll
))
567 /* force the loading of DLL as builtin */
568 module
= pe_load_builtin_module(pcs
, wImageName
, BaseOfDll
, SizeOfDll
);
573 /* otherwise, try a regular PE module */
574 if (!(module
= pe_load_native_module(pcs
, wImageName
, hFile
, BaseOfDll
, SizeOfDll
)) &&
577 /* and finally an ELF or Mach-O module */
578 switch (module_get_type_by_name(wImageName
))
581 module
= elf_load_module(pcs
, wImageName
, BaseOfDll
);
584 module
= macho_load_module(pcs
, wImageName
, BaseOfDll
);
594 WARN("Couldn't locate %s\n", debugstr_w(wImageName
));
597 module
->module
.NumSyms
= module
->ht_symbols
.num_elts
;
598 /* by default module_new fills module.ModuleName from a derivation
599 * of LoadedImageName. Overwrite it, if we have better information
602 module_set_module(module
, wModuleName
);
604 lstrcpynW(module
->module
.ImageName
, wImageName
,
605 sizeof(module
->module
.ImageName
) / sizeof(WCHAR
));
607 return module
->module
.BaseOfImage
;
610 /***********************************************************************
611 * SymLoadModule64 (DBGHELP.@)
613 DWORD64 WINAPI
SymLoadModule64(HANDLE hProcess
, HANDLE hFile
, PCSTR ImageName
,
614 PCSTR ModuleName
, DWORD64 BaseOfDll
, DWORD SizeOfDll
)
616 if (!validate_addr64(BaseOfDll
)) return FALSE
;
617 return SymLoadModule(hProcess
, hFile
, ImageName
, ModuleName
, (DWORD
)BaseOfDll
, SizeOfDll
);
620 /******************************************************************
624 BOOL
module_remove(struct process
* pcs
, struct module
* module
)
626 struct module_format
*modfmt
;
630 TRACE("%s (%p)\n", debugstr_w(module
->module
.ModuleName
), module
);
632 for (i
= 0; i
< DFI_LAST
; i
++)
634 if ((modfmt
= module
->format_info
[i
]) && modfmt
->remove
)
635 modfmt
->remove(pcs
, module
->format_info
[i
]);
637 hash_table_destroy(&module
->ht_symbols
);
638 hash_table_destroy(&module
->ht_types
);
639 HeapFree(GetProcessHeap(), 0, module
->sources
);
640 HeapFree(GetProcessHeap(), 0, module
->addr_sorttab
);
641 pool_destroy(&module
->pool
);
642 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
645 for (p
= &pcs
->lmodules
; *p
; p
= &(*p
)->next
)
650 HeapFree(GetProcessHeap(), 0, module
);
654 FIXME("This shouldn't happen\n");
658 /******************************************************************
659 * SymUnloadModule (DBGHELP.@)
662 BOOL WINAPI
SymUnloadModule(HANDLE hProcess
, DWORD BaseOfDll
)
665 struct module
* module
;
667 pcs
= process_find_by_handle(hProcess
);
668 if (!pcs
) return FALSE
;
669 module
= module_find_by_addr(pcs
, BaseOfDll
, DMT_UNKNOWN
);
670 if (!module
) return FALSE
;
671 return module_remove(pcs
, module
);
674 /******************************************************************
675 * SymUnloadModule64 (DBGHELP.@)
678 BOOL WINAPI
SymUnloadModule64(HANDLE hProcess
, DWORD64 BaseOfDll
)
681 struct module
* module
;
683 pcs
= process_find_by_handle(hProcess
);
684 if (!pcs
) return FALSE
;
685 if (!validate_addr64(BaseOfDll
)) return FALSE
;
686 module
= module_find_by_addr(pcs
, (DWORD
)BaseOfDll
, DMT_UNKNOWN
);
687 if (!module
) return FALSE
;
688 return module_remove(pcs
, module
);
691 /******************************************************************
692 * SymEnumerateModules (DBGHELP.@)
695 struct enum_modW64_32
697 PSYM_ENUMMODULES_CALLBACK cb
;
699 char module
[MAX_PATH
];
702 static BOOL CALLBACK
enum_modW64_32(PCWSTR name
, DWORD64 base
, PVOID user
)
704 struct enum_modW64_32
* x
= user
;
706 WideCharToMultiByte(CP_ACP
, 0, name
, -1, x
->module
, sizeof(x
->module
), NULL
, NULL
);
707 return x
->cb(x
->module
, (DWORD
)base
, x
->user
);
710 BOOL WINAPI
SymEnumerateModules(HANDLE hProcess
,
711 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback
,
714 struct enum_modW64_32 x
;
716 x
.cb
= EnumModulesCallback
;
717 x
.user
= UserContext
;
719 return SymEnumerateModulesW64(hProcess
, enum_modW64_32
, &x
);
722 /******************************************************************
723 * SymEnumerateModules64 (DBGHELP.@)
726 struct enum_modW64_64
728 PSYM_ENUMMODULES_CALLBACK64 cb
;
730 char module
[MAX_PATH
];
733 static BOOL CALLBACK
enum_modW64_64(PCWSTR name
, DWORD64 base
, PVOID user
)
735 struct enum_modW64_64
* x
= user
;
737 WideCharToMultiByte(CP_ACP
, 0, name
, -1, x
->module
, sizeof(x
->module
), NULL
, NULL
);
738 return x
->cb(x
->module
, base
, x
->user
);
741 BOOL WINAPI
SymEnumerateModules64(HANDLE hProcess
,
742 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback
,
745 struct enum_modW64_64 x
;
747 x
.cb
= EnumModulesCallback
;
748 x
.user
= UserContext
;
750 return SymEnumerateModulesW64(hProcess
, enum_modW64_64
, &x
);
753 /******************************************************************
754 * SymEnumerateModulesW64 (DBGHELP.@)
757 BOOL WINAPI
SymEnumerateModulesW64(HANDLE hProcess
,
758 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback
,
761 struct process
* pcs
= process_find_by_handle(hProcess
);
762 struct module
* module
;
764 if (!pcs
) return FALSE
;
766 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
768 if (!(dbghelp_options
& SYMOPT_WINE_WITH_NATIVE_MODULES
) &&
769 (module
->type
== DMT_ELF
|| module
->type
== DMT_MACHO
))
771 if (!EnumModulesCallback(module
->module
.ModuleName
,
772 module
->module
.BaseOfImage
, UserContext
))
778 /******************************************************************
779 * EnumerateLoadedModules64 (DBGHELP.@)
782 struct enum_load_modW64_64
784 PENUMLOADED_MODULES_CALLBACK64 cb
;
786 char module
[MAX_PATH
];
789 static BOOL CALLBACK
enum_load_modW64_64(PCWSTR name
, DWORD64 base
, ULONG size
,
792 struct enum_load_modW64_64
* x
= user
;
794 WideCharToMultiByte(CP_ACP
, 0, name
, -1, x
->module
, sizeof(x
->module
), NULL
, NULL
);
795 return x
->cb(x
->module
, base
, size
, x
->user
);
798 BOOL WINAPI
EnumerateLoadedModules64(HANDLE hProcess
,
799 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback
,
802 struct enum_load_modW64_64 x
;
804 x
.cb
= EnumLoadedModulesCallback
;
805 x
.user
= UserContext
;
807 return EnumerateLoadedModulesW64(hProcess
, enum_load_modW64_64
, &x
);
810 /******************************************************************
811 * EnumerateLoadedModules (DBGHELP.@)
814 struct enum_load_modW64_32
816 PENUMLOADED_MODULES_CALLBACK cb
;
818 char module
[MAX_PATH
];
821 static BOOL CALLBACK
enum_load_modW64_32(PCWSTR name
, DWORD64 base
, ULONG size
,
824 struct enum_load_modW64_32
* x
= user
;
825 WideCharToMultiByte(CP_ACP
, 0, name
, -1, x
->module
, sizeof(x
->module
), NULL
, NULL
);
826 return x
->cb(x
->module
, (DWORD
)base
, size
, x
->user
);
829 BOOL WINAPI
EnumerateLoadedModules(HANDLE hProcess
,
830 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback
,
833 struct enum_load_modW64_32 x
;
835 x
.cb
= EnumLoadedModulesCallback
;
836 x
.user
= UserContext
;
838 return EnumerateLoadedModulesW64(hProcess
, enum_load_modW64_32
, &x
);
841 /******************************************************************
842 * EnumerateLoadedModulesW64 (DBGHELP.@)
845 BOOL WINAPI
EnumerateLoadedModulesW64(HANDLE hProcess
,
846 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback
,
850 WCHAR baseW
[256], modW
[256];
854 hMods
= HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods
[0]));
855 if (!hMods
) return FALSE
;
857 if (!EnumProcessModules(hProcess
, hMods
, 256 * sizeof(hMods
[0]), &sz
))
859 /* hProcess should also be a valid process handle !! */
860 FIXME("If this happens, bump the number in mod\n");
861 HeapFree(GetProcessHeap(), 0, hMods
);
864 sz
/= sizeof(HMODULE
);
865 for (i
= 0; i
< sz
; i
++)
867 if (!GetModuleInformation(hProcess
, hMods
[i
], &mi
, sizeof(mi
)) ||
868 !GetModuleBaseNameW(hProcess
, hMods
[i
], baseW
, sizeof(baseW
) / sizeof(WCHAR
)))
870 module_fill_module(baseW
, modW
, sizeof(modW
) / sizeof(CHAR
));
871 EnumLoadedModulesCallback(modW
, (DWORD_PTR
)mi
.lpBaseOfDll
, mi
.SizeOfImage
,
874 HeapFree(GetProcessHeap(), 0, hMods
);
876 return sz
!= 0 && i
== sz
;
879 /******************************************************************
880 * SymGetModuleInfo (DBGHELP.@)
883 BOOL WINAPI
SymGetModuleInfo(HANDLE hProcess
, DWORD dwAddr
,
884 PIMAGEHLP_MODULE ModuleInfo
)
887 IMAGEHLP_MODULEW64 miw64
;
889 if (sizeof(mi
) < ModuleInfo
->SizeOfStruct
) FIXME("Wrong size\n");
891 miw64
.SizeOfStruct
= sizeof(miw64
);
892 if (!SymGetModuleInfoW64(hProcess
, dwAddr
, &miw64
)) return FALSE
;
894 mi
.SizeOfStruct
= miw64
.SizeOfStruct
;
895 mi
.BaseOfImage
= miw64
.BaseOfImage
;
896 mi
.ImageSize
= miw64
.ImageSize
;
897 mi
.TimeDateStamp
= miw64
.TimeDateStamp
;
898 mi
.CheckSum
= miw64
.CheckSum
;
899 mi
.NumSyms
= miw64
.NumSyms
;
900 mi
.SymType
= miw64
.SymType
;
901 WideCharToMultiByte(CP_ACP
, 0, miw64
.ModuleName
, -1,
902 mi
.ModuleName
, sizeof(mi
.ModuleName
), NULL
, NULL
);
903 WideCharToMultiByte(CP_ACP
, 0, miw64
.ImageName
, -1,
904 mi
.ImageName
, sizeof(mi
.ImageName
), NULL
, NULL
);
905 WideCharToMultiByte(CP_ACP
, 0, miw64
.LoadedImageName
, -1,
906 mi
.LoadedImageName
, sizeof(mi
.LoadedImageName
), NULL
, NULL
);
908 memcpy(ModuleInfo
, &mi
, ModuleInfo
->SizeOfStruct
);
913 /******************************************************************
914 * SymGetModuleInfoW (DBGHELP.@)
917 BOOL WINAPI
SymGetModuleInfoW(HANDLE hProcess
, DWORD dwAddr
,
918 PIMAGEHLP_MODULEW ModuleInfo
)
920 IMAGEHLP_MODULEW64 miw64
;
921 IMAGEHLP_MODULEW miw
;
923 if (sizeof(miw
) < ModuleInfo
->SizeOfStruct
) FIXME("Wrong size\n");
925 miw64
.SizeOfStruct
= sizeof(miw64
);
926 if (!SymGetModuleInfoW64(hProcess
, dwAddr
, &miw64
)) return FALSE
;
928 miw
.SizeOfStruct
= miw64
.SizeOfStruct
;
929 miw
.BaseOfImage
= miw64
.BaseOfImage
;
930 miw
.ImageSize
= miw64
.ImageSize
;
931 miw
.TimeDateStamp
= miw64
.TimeDateStamp
;
932 miw
.CheckSum
= miw64
.CheckSum
;
933 miw
.NumSyms
= miw64
.NumSyms
;
934 miw
.SymType
= miw64
.SymType
;
935 strcpyW(miw
.ModuleName
, miw64
.ModuleName
);
936 strcpyW(miw
.ImageName
, miw64
.ImageName
);
937 strcpyW(miw
.LoadedImageName
, miw64
.LoadedImageName
);
938 memcpy(ModuleInfo
, &miw
, ModuleInfo
->SizeOfStruct
);
943 /******************************************************************
944 * SymGetModuleInfo64 (DBGHELP.@)
947 BOOL WINAPI
SymGetModuleInfo64(HANDLE hProcess
, DWORD64 dwAddr
,
948 PIMAGEHLP_MODULE64 ModuleInfo
)
950 IMAGEHLP_MODULE64 mi64
;
951 IMAGEHLP_MODULEW64 miw64
;
953 if (sizeof(mi64
) < ModuleInfo
->SizeOfStruct
)
955 SetLastError(ERROR_MOD_NOT_FOUND
); /* NOTE: native returns this error */
956 WARN("Wrong size %u\n", ModuleInfo
->SizeOfStruct
);
960 miw64
.SizeOfStruct
= sizeof(miw64
);
961 if (!SymGetModuleInfoW64(hProcess
, dwAddr
, &miw64
)) return FALSE
;
963 mi64
.SizeOfStruct
= miw64
.SizeOfStruct
;
964 mi64
.BaseOfImage
= miw64
.BaseOfImage
;
965 mi64
.ImageSize
= miw64
.ImageSize
;
966 mi64
.TimeDateStamp
= miw64
.TimeDateStamp
;
967 mi64
.CheckSum
= miw64
.CheckSum
;
968 mi64
.NumSyms
= miw64
.NumSyms
;
969 mi64
.SymType
= miw64
.SymType
;
970 WideCharToMultiByte(CP_ACP
, 0, miw64
.ModuleName
, -1,
971 mi64
.ModuleName
, sizeof(mi64
.ModuleName
), NULL
, NULL
);
972 WideCharToMultiByte(CP_ACP
, 0, miw64
.ImageName
, -1,
973 mi64
.ImageName
, sizeof(mi64
.ImageName
), NULL
, NULL
);
974 WideCharToMultiByte(CP_ACP
, 0, miw64
.LoadedImageName
, -1,
975 mi64
.LoadedImageName
, sizeof(mi64
.LoadedImageName
), NULL
, NULL
);
976 WideCharToMultiByte(CP_ACP
, 0, miw64
.LoadedPdbName
, -1,
977 mi64
.LoadedPdbName
, sizeof(mi64
.LoadedPdbName
), NULL
, NULL
);
979 mi64
.CVSig
= miw64
.CVSig
;
980 WideCharToMultiByte(CP_ACP
, 0, miw64
.CVData
, -1,
981 mi64
.CVData
, sizeof(mi64
.CVData
), NULL
, NULL
);
982 mi64
.PdbSig
= miw64
.PdbSig
;
983 mi64
.PdbSig70
= miw64
.PdbSig70
;
984 mi64
.PdbAge
= miw64
.PdbAge
;
985 mi64
.PdbUnmatched
= miw64
.PdbUnmatched
;
986 mi64
.DbgUnmatched
= miw64
.DbgUnmatched
;
987 mi64
.LineNumbers
= miw64
.LineNumbers
;
988 mi64
.GlobalSymbols
= miw64
.GlobalSymbols
;
989 mi64
.TypeInfo
= miw64
.TypeInfo
;
990 mi64
.SourceIndexed
= miw64
.SourceIndexed
;
991 mi64
.Publics
= miw64
.Publics
;
993 memcpy(ModuleInfo
, &mi64
, ModuleInfo
->SizeOfStruct
);
998 /******************************************************************
999 * SymGetModuleInfoW64 (DBGHELP.@)
1002 BOOL WINAPI
SymGetModuleInfoW64(HANDLE hProcess
, DWORD64 dwAddr
,
1003 PIMAGEHLP_MODULEW64 ModuleInfo
)
1005 struct process
* pcs
= process_find_by_handle(hProcess
);
1006 struct module
* module
;
1007 IMAGEHLP_MODULEW64 miw64
;
1009 TRACE("%p %s %p\n", hProcess
, wine_dbgstr_longlong(dwAddr
), ModuleInfo
);
1011 if (!pcs
) return FALSE
;
1012 if (ModuleInfo
->SizeOfStruct
> sizeof(*ModuleInfo
)) return FALSE
;
1013 module
= module_find_by_addr(pcs
, dwAddr
, DMT_UNKNOWN
);
1014 if (!module
) return FALSE
;
1016 miw64
= module
->module
;
1018 /* update debug information from container if any */
1019 if (module
->module
.SymType
== SymNone
)
1021 module
= module_get_container(pcs
, module
);
1022 if (module
&& module
->module
.SymType
!= SymNone
)
1024 miw64
.SymType
= module
->module
.SymType
;
1025 miw64
.NumSyms
= module
->module
.NumSyms
;
1028 memcpy(ModuleInfo
, &miw64
, ModuleInfo
->SizeOfStruct
);
1032 /***********************************************************************
1033 * SymGetModuleBase (DBGHELP.@)
1035 DWORD WINAPI
SymGetModuleBase(HANDLE hProcess
, DWORD dwAddr
)
1039 ret
= SymGetModuleBase64(hProcess
, dwAddr
);
1040 return validate_addr64(ret
) ? ret
: 0;
1043 /***********************************************************************
1044 * SymGetModuleBase64 (DBGHELP.@)
1046 DWORD64 WINAPI
SymGetModuleBase64(HANDLE hProcess
, DWORD64 dwAddr
)
1048 struct process
* pcs
= process_find_by_handle(hProcess
);
1049 struct module
* module
;
1052 module
= module_find_by_addr(pcs
, dwAddr
, DMT_UNKNOWN
);
1053 if (!module
) return 0;
1054 return module
->module
.BaseOfImage
;
1057 /******************************************************************
1058 * module_reset_debug_info
1059 * Removes any debug information linked to a given module.
1061 void module_reset_debug_info(struct module
* module
)
1063 module
->sortlist_valid
= TRUE
;
1064 module
->sorttab_size
= 0;
1065 module
->addr_sorttab
= NULL
;
1066 module
->num_sorttab
= module
->num_symbols
= 0;
1067 hash_table_destroy(&module
->ht_symbols
);
1068 module
->ht_symbols
.num_buckets
= 0;
1069 module
->ht_symbols
.buckets
= NULL
;
1070 hash_table_destroy(&module
->ht_types
);
1071 module
->ht_types
.num_buckets
= 0;
1072 module
->ht_types
.buckets
= NULL
;
1073 module
->vtypes
.num_elts
= 0;
1074 hash_table_destroy(&module
->ht_symbols
);
1075 module
->sources_used
= module
->sources_alloc
= 0;
1076 module
->sources
= NULL
;
1079 /******************************************************************
1080 * SymRefreshModuleList (DBGHELP.@)
1082 BOOL WINAPI
SymRefreshModuleList(HANDLE hProcess
)
1084 struct process
* pcs
;
1086 TRACE("(%p)\n", hProcess
);
1088 if (!(pcs
= process_find_by_handle(hProcess
))) return FALSE
;
1090 return refresh_module_list(pcs
);
1093 /***********************************************************************
1094 * SymFunctionTableAccess (DBGHELP.@)
1096 PVOID WINAPI
SymFunctionTableAccess(HANDLE hProcess
, DWORD AddrBase
)
1098 return SymFunctionTableAccess64(hProcess
, AddrBase
);
1101 /***********************************************************************
1102 * SymFunctionTableAccess64 (DBGHELP.@)
1104 PVOID WINAPI
SymFunctionTableAccess64(HANDLE hProcess
, DWORD64 AddrBase
)
1106 struct process
* pcs
= process_find_by_handle(hProcess
);
1107 struct module
* module
;
1109 if (!pcs
|| !dbghelp_current_cpu
->find_runtime_function
) return NULL
;
1110 module
= module_find_by_addr(pcs
, AddrBase
, DMT_UNKNOWN
);
1111 if (!module
) return NULL
;
1113 return dbghelp_current_cpu
->find_runtime_function(module
, AddrBase
);