2 * File module.c - module handling for the wine debugger
4 * Copyright (C) 1993, Eric Youngdale.
5 * 2000-2004, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "dbghelp_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
36 static const char* ext
[] = {".acm", ".dll", ".drv", ".exe", ".ocx", ".vxd", NULL
};
38 static int match_ext(const char* ptr
, size_t len
)
43 for (e
= ext
; *e
; e
++)
46 if (l
>= len
) return FALSE
;
47 if (strncasecmp(&ptr
[len
- l
], *e
, l
)) continue;
53 static void module_fill_module(const char* in
, char* out
, size_t size
)
55 const char *ptr
,*endptr
;
58 endptr
= in
+ strlen(in
);
59 for (ptr
= endptr
- 1;
60 ptr
>= in
&& *ptr
!= '/' && *ptr
!= '\\';
63 len
= min(endptr
-ptr
,size
-1);
64 memcpy(out
, ptr
, len
);
66 if (len
> 4 && (l
= match_ext(out
, len
)))
69 (!strcasecmp(out
+ len
- 12, "wine-pthread") ||
70 !strcasecmp(out
+ len
- 12, "wine-kthread")))
71 lstrcpynA(out
, "<wine-loader>", size
);
74 if (len
> 3 && !strcasecmp(&out
[len
- 3], ".so") &&
75 (l
= match_ext(out
, len
- 3)))
76 strcpy(&out
[len
- l
- 3], "<elf>");
78 while ((*out
= tolower(*out
))) out
++;
81 static const char* get_module_type(enum module_type type
)
85 case DMT_ELF
: return "ELF";
86 case DMT_PE
: return "PE";
87 case DMT_VIRTUAL
: return "Virtual";
88 default: return "---";
92 /***********************************************************************
93 * Creates and links a new module to a process
95 struct module
* module_new(struct process
* pcs
, const char* name
,
96 enum module_type type
,
97 unsigned long mod_addr
, unsigned long size
,
98 unsigned long stamp
, unsigned long checksum
)
100 struct module
* module
;
102 assert(type
== DMT_ELF
|| type
== DMT_PE
|| type
== DMT_VIRTUAL
);
103 if (!(module
= HeapAlloc(GetProcessHeap(), 0, sizeof(*module
))))
106 memset(module
, 0, sizeof(*module
));
108 module
->next
= pcs
->lmodules
;
109 pcs
->lmodules
= module
;
111 TRACE("=> %s %08lx-%08lx %s\n",
112 get_module_type(type
), mod_addr
, mod_addr
+ size
, name
);
114 pool_init(&module
->pool
, 65536);
116 module
->module
.SizeOfStruct
= sizeof(module
->module
);
117 module
->module
.BaseOfImage
= mod_addr
;
118 module
->module
.ImageSize
= size
;
119 module_fill_module(name
, module
->module
.ModuleName
,
120 sizeof(module
->module
.ModuleName
));
121 module
->module
.ImageName
[0] = '\0';
122 lstrcpynA(module
->module
.LoadedImageName
, name
, sizeof(module
->module
.LoadedImageName
));
123 module
->module
.SymType
= SymNone
;
124 module
->module
.NumSyms
= 0;
125 module
->module
.TimeDateStamp
= stamp
;
126 module
->module
.CheckSum
= checksum
;
129 module
->sortlist_valid
= FALSE
;
130 module
->addr_sorttab
= NULL
;
131 /* FIXME: this seems a bit too high (on a per module basis)
132 * need some statistics about this
134 hash_table_init(&module
->pool
, &module
->ht_symbols
, 4096);
135 hash_table_init(&module
->pool
, &module
->ht_types
, 4096);
136 vector_init(&module
->vtypes
, sizeof(struct symt
*), 32);
138 module
->sources_used
= 0;
139 module
->sources_alloc
= 0;
145 /***********************************************************************
146 * module_find_by_name
149 struct module
* module_find_by_name(const struct process
* pcs
,
150 const char* name
, enum module_type type
)
152 struct module
* module
;
154 if (type
== DMT_UNKNOWN
)
156 if ((module
= module_find_by_name(pcs
, name
, DMT_PE
)) ||
157 (module
= module_find_by_name(pcs
, name
, DMT_ELF
)) ||
158 (module
= module_find_by_name(pcs
, name
, DMT_VIRTUAL
)))
163 char modname
[MAX_PATH
];
165 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
167 if (type
== module
->type
&&
168 !strcasecmp(name
, module
->module
.LoadedImageName
))
171 module_fill_module(name
, modname
, sizeof(modname
));
172 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
174 if (type
== module
->type
&&
175 !strcasecmp(modname
, module
->module
.ModuleName
))
179 SetLastError(ERROR_INVALID_NAME
);
183 /***********************************************************************
184 * module_get_container
187 struct module
* module_get_container(const struct process
* pcs
,
188 const struct module
* inner
)
190 struct module
* module
;
192 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
194 if (module
!= inner
&&
195 module
->module
.BaseOfImage
<= inner
->module
.BaseOfImage
&&
196 module
->module
.BaseOfImage
+ module
->module
.ImageSize
>=
197 inner
->module
.BaseOfImage
+ inner
->module
.ImageSize
)
203 /***********************************************************************
204 * module_get_containee
207 struct module
* module_get_containee(const struct process
* pcs
,
208 const struct module
* outter
)
210 struct module
* module
;
212 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
214 if (module
!= outter
&&
215 outter
->module
.BaseOfImage
<= module
->module
.BaseOfImage
&&
216 outter
->module
.BaseOfImage
+ outter
->module
.ImageSize
>=
217 module
->module
.BaseOfImage
+ module
->module
.ImageSize
)
223 /******************************************************************
226 * get the debug information from a module:
227 * - if the module's type is deferred, then force loading of debug info (and return
229 * - if the module has no debug info and has an ELF container, then return the ELF
230 * container (and also force the ELF container's debug info loading if deferred)
231 * - otherwise return the module itself if it has some debug info
233 struct module
* module_get_debug(const struct process
* pcs
, struct module
* module
)
235 struct module
* parent
;
236 IMAGEHLP_DEFERRED_SYMBOL_LOAD64 idsl64
;
238 if (!module
) return NULL
;
239 /* for a PE builtin, always get info from parent */
240 if ((parent
= module_get_container(pcs
, module
)))
242 /* if deferred, force loading */
243 if (module
->module
.SymType
== SymDeferred
)
247 switch (module
->type
)
250 ret
= elf_load_debug_info(module
, NULL
);
253 idsl64
.SizeOfStruct
= sizeof(idsl64
);
254 idsl64
.BaseOfImage
= module
->module
.BaseOfImage
;
255 idsl64
.CheckSum
= module
->module
.CheckSum
;
256 idsl64
.TimeDateStamp
= module
->module
.TimeDateStamp
;
257 strcpy(idsl64
.FileName
, module
->module
.ImageName
);
258 idsl64
.Reparse
= FALSE
;
259 idsl64
.hFile
= INVALID_HANDLE_VALUE
;
261 pcs_callback(pcs
, CBA_DEFERRED_SYMBOL_LOAD_START
, &idsl64
);
262 ret
= pe_load_debug_info(pcs
, module
);
264 ret
? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE
: CBA_DEFERRED_SYMBOL_LOAD_FAILURE
,
267 case DMT_VIRTUAL
: /* fall through */
272 if (!ret
) module
->module
.SymType
= SymNone
;
273 assert(module
->module
.SymType
!= SymDeferred
);
275 return (module
&& module
->module
.SymType
!= SymNone
) ? module
: NULL
;
278 /***********************************************************************
279 * module_find_by_addr
281 * either the addr where module is loaded, or any address inside the
284 struct module
* module_find_by_addr(const struct process
* pcs
, unsigned long addr
,
285 enum module_type type
)
287 struct module
* module
;
289 if (type
== DMT_UNKNOWN
)
291 if ((module
= module_find_by_addr(pcs
, addr
, DMT_PE
)) ||
292 (module
= module_find_by_addr(pcs
, addr
, DMT_ELF
)) ||
293 (module
= module_find_by_addr(pcs
, addr
, DMT_VIRTUAL
)))
298 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
300 if (type
== module
->type
&& addr
>= module
->module
.BaseOfImage
&&
301 addr
< module
->module
.BaseOfImage
+ module
->module
.ImageSize
)
305 SetLastError(ERROR_INVALID_ADDRESS
);
309 static BOOL
module_is_elf_container_loaded(struct process
* pcs
, const char* ImageName
,
310 const char* ModuleName
)
312 char buffer
[MAX_PATH
];
314 struct module
* module
;
318 module_fill_module(ImageName
, buffer
, sizeof(buffer
));
321 len
= strlen(ModuleName
);
322 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
324 if (!strncasecmp(module
->module
.ModuleName
, ModuleName
, len
) &&
325 module
->type
== DMT_ELF
&&
326 !strcmp(module
->module
.ModuleName
+ len
, "<elf>"))
332 /******************************************************************
333 * module_get_type_by_name
335 * Guesses a filename type from its extension
337 enum module_type
module_get_type_by_name(const char* name
)
340 int len
= strlen(name
);
342 /* check for terminating .so or .so.[digit] */
343 ptr
= strrchr(name
, '.');
346 if (!strcmp(ptr
, ".so") ||
347 (isdigit(ptr
[1]) && !ptr
[2] && ptr
>= name
+ 3 && !memcmp(ptr
- 3, ".so", 3)))
349 else if (!strcasecmp(ptr
, ".pdb"))
352 /* wine-[kp]thread is also an ELF module */
353 else if (((len
> 12 && name
[len
- 13] == '/') || len
== 12) &&
354 (!strcasecmp(name
+ len
- 12, "wine-pthread") ||
355 !strcasecmp(name
+ len
- 12, "wine-kthread")))
362 /***********************************************************************
363 * SymLoadModule (DBGHELP.@)
365 DWORD WINAPI
SymLoadModule(HANDLE hProcess
, HANDLE hFile
, char* ImageName
,
366 char* ModuleName
, DWORD BaseOfDll
, DWORD SizeOfDll
)
369 struct module
* module
= NULL
;
371 TRACE("(%p %p %s %s %08lx %08lx)\n",
372 hProcess
, hFile
, debugstr_a(ImageName
), debugstr_a(ModuleName
),
373 BaseOfDll
, SizeOfDll
);
375 pcs
= process_find_by_handle(hProcess
);
376 if (!pcs
) return FALSE
;
378 /* force transparent ELF loading / unloading */
379 elf_synchronize_module_list(pcs
);
381 /* this is a Wine extension to the API just to redo the synchronisation */
382 if (!ImageName
&& !hFile
) return 0;
384 if (module_is_elf_container_loaded(pcs
, ImageName
, ModuleName
))
386 /* force the loading of DLL as builtin */
387 if ((module
= pe_load_module_from_pcs(pcs
, ImageName
, ModuleName
,
388 BaseOfDll
, SizeOfDll
)))
390 WARN("Couldn't locate %s\n", ImageName
);
393 TRACE("Assuming %s as native DLL\n", ImageName
);
394 if (!(module
= pe_load_module(pcs
, ImageName
, hFile
, BaseOfDll
, SizeOfDll
)))
396 if (module_get_type_by_name(ImageName
) == DMT_ELF
&&
397 (module
= elf_load_module(pcs
, ImageName
, BaseOfDll
)))
399 FIXME("Should have successfully loaded debug information for image %s\n",
401 if ((module
= pe_load_module_from_pcs(pcs
, ImageName
, ModuleName
,
402 BaseOfDll
, SizeOfDll
)))
404 WARN("Couldn't locate %s\n", ImageName
);
409 /* by default pe_load_module fills module.ModuleName from a derivation
410 * of ImageName. Overwrite it, if we have better information
413 lstrcpynA(module
->module
.ModuleName
, ModuleName
, sizeof(module
->module
.ModuleName
));
414 lstrcpynA(module
->module
.ImageName
, ImageName
, sizeof(module
->module
.ImageName
));
416 return module
->module
.BaseOfImage
;
419 /***********************************************************************
420 * SymLoadModuleEx (DBGHELP.@)
422 DWORD64 WINAPI
SymLoadModuleEx(HANDLE hProcess
, HANDLE hFile
, PCSTR ImageName
,
423 PCSTR ModuleName
, DWORD64 BaseOfDll
, DWORD DllSize
,
424 PMODLOAD_DATA Data
, DWORD Flags
)
427 FIXME("Unsupported load data parameter %p for %s\n", Data
, ImageName
);
428 if (!validate_addr64(BaseOfDll
)) return FALSE
;
429 if (Flags
& SLMFLAG_VIRTUAL
)
431 struct process
* pcs
= process_find_by_handle(hProcess
);
432 struct module
* module
;
433 if (!pcs
) return FALSE
;
435 module
= module_new(pcs
, ImageName
, DMT_VIRTUAL
, (DWORD
)BaseOfDll
, DllSize
, 0, 0);
436 if (!module
) return FALSE
;
438 lstrcpynA(module
->module
.ModuleName
, ModuleName
, sizeof(module
->module
.ModuleName
));
442 if (Flags
& ~(SLMFLAG_VIRTUAL
))
443 FIXME("Unsupported Flags %08lx for %s\n", Flags
, ImageName
);
445 return SymLoadModule(hProcess
, hFile
, (char*)ImageName
, (char*)ModuleName
,
446 (DWORD
)BaseOfDll
, DllSize
);
449 /***********************************************************************
450 * SymLoadModule64 (DBGHELP.@)
452 DWORD64 WINAPI
SymLoadModule64(HANDLE hProcess
, HANDLE hFile
, PSTR ImageName
,
453 PSTR ModuleName
, DWORD64 BaseOfDll
, DWORD SizeOfDll
)
455 if (!validate_addr64(BaseOfDll
)) return FALSE
;
456 return SymLoadModule(hProcess
, hFile
, ImageName
, ModuleName
, (DWORD
)BaseOfDll
, SizeOfDll
);
459 /******************************************************************
463 BOOL
module_remove(struct process
* pcs
, struct module
* module
)
467 TRACE("%s (%p)\n", module
->module
.ModuleName
, module
);
468 hash_table_destroy(&module
->ht_symbols
);
469 hash_table_destroy(&module
->ht_types
);
470 HeapFree(GetProcessHeap(), 0, (char*)module
->sources
);
471 HeapFree(GetProcessHeap(), 0, module
->addr_sorttab
);
472 pool_destroy(&module
->pool
);
473 if (module
->module
.SymType
!= SymNone
)
474 pcs_callback(pcs
, CBA_SYMBOLS_UNLOADED
, NULL
);
476 for (p
= &pcs
->lmodules
; *p
; p
= &(*p
)->next
)
481 HeapFree(GetProcessHeap(), 0, module
);
485 FIXME("This shouldn't happen\n");
489 /******************************************************************
490 * SymUnloadModule (DBGHELP.@)
493 BOOL WINAPI
SymUnloadModule(HANDLE hProcess
, DWORD BaseOfDll
)
496 struct module
* module
;
498 pcs
= process_find_by_handle(hProcess
);
499 if (!pcs
) return FALSE
;
500 module
= module_find_by_addr(pcs
, BaseOfDll
, DMT_UNKNOWN
);
501 if (!module
) return FALSE
;
502 return module_remove(pcs
, module
);
505 /******************************************************************
506 * SymUnloadModule64 (DBGHELP.@)
509 BOOL WINAPI
SymUnloadModule64(HANDLE hProcess
, DWORD64 BaseOfDll
)
512 struct module
* module
;
514 pcs
= process_find_by_handle(hProcess
);
515 if (!pcs
) return FALSE
;
516 if (!validate_addr64(BaseOfDll
)) return FALSE
;
517 module
= module_find_by_addr(pcs
, (DWORD
)BaseOfDll
, DMT_UNKNOWN
);
518 if (!module
) return FALSE
;
519 return module_remove(pcs
, module
);
522 /******************************************************************
523 * SymEnumerateModules (DBGHELP.@)
526 BOOL WINAPI
SymEnumerateModules(HANDLE hProcess
,
527 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback
,
530 struct process
* pcs
= process_find_by_handle(hProcess
);
531 struct module
* module
;
533 if (!pcs
) return FALSE
;
535 for (module
= pcs
->lmodules
; module
; module
= module
->next
)
537 if (!(dbghelp_options
& SYMOPT_WINE_WITH_ELF_MODULES
) && module
->type
== DMT_ELF
)
539 if (!EnumModulesCallback(module
->module
.ModuleName
,
540 module
->module
.BaseOfImage
, UserContext
))
546 /******************************************************************
547 * EnumerateLoadedModules (DBGHELP.@)
550 BOOL WINAPI
EnumerateLoadedModules(HANDLE hProcess
,
551 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback
,
555 char base
[256], mod
[256];
559 hMods
= HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods
[0]));
560 if (!hMods
) return FALSE
;
562 if (!EnumProcessModules(hProcess
, hMods
, 256 * sizeof(hMods
[0]), &sz
))
564 /* hProcess should also be a valid process handle !! */
565 FIXME("If this happens, bump the number in mod\n");
566 HeapFree(GetProcessHeap(), 0, hMods
);
569 sz
/= sizeof(HMODULE
);
570 for (i
= 0; i
< sz
; i
++)
572 if (!GetModuleInformation(hProcess
, hMods
[i
], &mi
, sizeof(mi
)) ||
573 !GetModuleBaseNameA(hProcess
, hMods
[i
], base
, sizeof(base
)))
575 module_fill_module(base
, mod
, sizeof(mod
));
576 EnumLoadedModulesCallback(mod
, (DWORD
)mi
.lpBaseOfDll
, mi
.SizeOfImage
,
579 HeapFree(GetProcessHeap(), 0, hMods
);
581 return sz
!= 0 && i
== sz
;
584 /******************************************************************
585 * SymGetModuleInfo (DBGHELP.@)
588 BOOL WINAPI
SymGetModuleInfo(HANDLE hProcess
, DWORD dwAddr
,
589 PIMAGEHLP_MODULE ModuleInfo
)
591 struct process
* pcs
= process_find_by_handle(hProcess
);
592 struct module
* module
;
594 if (!pcs
) return FALSE
;
595 if (ModuleInfo
->SizeOfStruct
< sizeof(*ModuleInfo
)) return FALSE
;
596 module
= module_find_by_addr(pcs
, dwAddr
, DMT_UNKNOWN
);
597 if (!module
) return FALSE
;
599 *ModuleInfo
= module
->module
;
600 if (module
->module
.SymType
== SymNone
)
602 module
= module_get_container(pcs
, module
);
603 if (module
&& module
->module
.SymType
!= SymNone
)
604 ModuleInfo
->SymType
= module
->module
.SymType
;
610 /******************************************************************
611 * SymGetModuleInfo64 (DBGHELP.@)
614 BOOL WINAPI
SymGetModuleInfo64(HANDLE hProcess
, DWORD64 dwAddr
,
615 PIMAGEHLP_MODULE64 ModuleInfo
)
617 struct process
* pcs
= process_find_by_handle(hProcess
);
618 struct module
* module
;
620 IMAGEHLP_MODULE64 mod
;
622 TRACE("%p %s %p\n", hProcess
, wine_dbgstr_longlong(dwAddr
), ModuleInfo
);
624 if (!pcs
) return FALSE
;
625 if (ModuleInfo
->SizeOfStruct
> sizeof(*ModuleInfo
)) return FALSE
;
626 module
= module_find_by_addr(pcs
, dwAddr
, DMT_UNKNOWN
);
627 if (!module
) return FALSE
;
629 mod
.BaseOfImage
= module
->module
.BaseOfImage
;
630 mod
.ImageSize
= module
->module
.ImageSize
;
631 mod
.TimeDateStamp
= module
->module
.TimeDateStamp
;
632 mod
.CheckSum
= module
->module
.CheckSum
;
633 mod
.NumSyms
= module
->module
.NumSyms
;
634 mod
.SymType
= module
->module
.SymType
;
635 strcpy(mod
.ModuleName
, module
->module
.ModuleName
);
636 strcpy(mod
.ImageName
, module
->module
.ImageName
);
637 strcpy(mod
.LoadedImageName
, module
->module
.LoadedImageName
);
638 /* FIXME: all following attributes need to be set */
639 mod
.LoadedPdbName
[0] = '\0';
641 memset(mod
.CVData
, 0, sizeof(mod
.CVData
));
643 memset(&mod
.PdbSig70
, 0, sizeof(mod
.PdbSig70
));
645 mod
.PdbUnmatched
= 0;
646 mod
.DbgUnmatched
= 0;
648 mod
.GlobalSymbols
= 0;
650 mod
.SourceIndexed
= 0;
653 if (module
->module
.SymType
== SymNone
)
655 module
= module_get_container(pcs
, module
);
656 if (module
&& module
->module
.SymType
!= SymNone
)
657 mod
.SymType
= module
->module
.SymType
;
659 sz
= ModuleInfo
->SizeOfStruct
;
660 memcpy(ModuleInfo
, &mod
, sz
);
661 ModuleInfo
->SizeOfStruct
= sz
;
665 /***********************************************************************
666 * SymGetModuleBase (DBGHELP.@)
668 DWORD WINAPI
SymGetModuleBase(HANDLE hProcess
, DWORD dwAddr
)
670 struct process
* pcs
= process_find_by_handle(hProcess
);
671 struct module
* module
;
674 module
= module_find_by_addr(pcs
, dwAddr
, DMT_UNKNOWN
);
675 if (!module
) return 0;
676 return module
->module
.BaseOfImage
;
679 /***********************************************************************
680 * SymGetModuleBase64 (DBGHELP.@)
682 DWORD64 WINAPI
SymGetModuleBase64(HANDLE hProcess
, DWORD64 dwAddr
)
684 if (!validate_addr64(dwAddr
)) return 0;
685 return SymGetModuleBase(hProcess
, (DWORD
)dwAddr
);
688 /******************************************************************
689 * module_reset_debug_info
690 * Removes any debug information linked to a given module.
692 void module_reset_debug_info(struct module
* module
)
694 module
->sortlist_valid
= TRUE
;
695 module
->addr_sorttab
= NULL
;
696 hash_table_destroy(&module
->ht_symbols
);
697 module
->ht_symbols
.num_buckets
= 0;
698 module
->ht_symbols
.buckets
= NULL
;
699 hash_table_destroy(&module
->ht_types
);
700 module
->ht_types
.num_buckets
= 0;
701 module
->ht_types
.buckets
= NULL
;
702 module
->vtypes
.num_elts
= 0;
703 hash_table_destroy(&module
->ht_symbols
);
704 module
->sources_used
= module
->sources_alloc
= 0;
705 module
->sources
= NULL
;