Create a branch for network fixes.
[reactos.git] / dll / win32 / dbghelp / module.c
1 /*
2 * File module.c - module handling for the wine debugger
3 *
4 * Copyright (C) 1993, Eric Youngdale.
5 * 2000-2007, Eric Pouech
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <assert.h>
27
28 #include "dbghelp_private.h"
29 #include "psapi.h"
30 #include "winternl.h"
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
34
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_DotPdbW[] = {'.','p','d','b','\0'};
39 const WCHAR S_WinePThreadW[] = {'w','i','n','e','-','p','t','h','r','e','a','d','\0'};
40 const WCHAR S_WineKThreadW[] = {'w','i','n','e','-','k','t','h','r','e','a','d','\0'};
41 const WCHAR S_SlashW[] = {'/','\0'};
42
43 static const WCHAR S_AcmW[] = {'.','a','c','m','\0'};
44 static const WCHAR S_DllW[] = {'.','d','l','l','\0'};
45 static const WCHAR S_DrvW[] = {'.','d','r','v','\0'};
46 static const WCHAR S_ExeW[] = {'.','e','x','e','\0'};
47 static const WCHAR S_OcxW[] = {'.','o','c','x','\0'};
48 static const WCHAR S_VxdW[] = {'.','v','x','d','\0'};
49 static const WCHAR * const ext[] = {S_AcmW, S_DllW, S_DrvW, S_ExeW, S_OcxW, S_VxdW, NULL};
50
51 static int match_ext(const WCHAR* ptr, size_t len)
52 {
53 const WCHAR* const *e;
54 size_t l;
55
56 for (e = ext; *e; e++)
57 {
58 l = strlenW(*e);
59 if (l >= len) return FALSE;
60 if (strncmpiW(&ptr[len - l], *e, l)) continue;
61 return l;
62 }
63 return 0;
64 }
65
66 static const WCHAR* get_filename(const WCHAR* name, const WCHAR* endptr)
67 {
68 const WCHAR* ptr;
69
70 if (!endptr) endptr = name + strlenW(name);
71 for (ptr = endptr - 1; ptr >= name; ptr--)
72 {
73 if (*ptr == '/' || *ptr == '\\') break;
74 }
75 return ++ptr;
76 }
77
78 static void module_fill_module(const WCHAR* in, WCHAR* out, size_t size)
79 {
80 const WCHAR *ptr, *endptr;
81 size_t len, l;
82
83 ptr = get_filename(in, endptr = in + strlenW(in));
84 len = min(endptr - ptr, size - 1);
85 memcpy(out, ptr, len * sizeof(WCHAR));
86 out[len] = '\0';
87 if (len > 4 && (l = match_ext(out, len)))
88 out[len - l] = '\0';
89 else if (len > 12 &&
90 (!strcmpiW(out + len - 12, S_WinePThreadW) ||
91 !strcmpiW(out + len - 12, S_WineKThreadW)))
92 lstrcpynW(out, S_WineLoaderW, size);
93 else
94 {
95 if (len > 3 && !strcmpiW(&out[len - 3], S_DotSoW) &&
96 (l = match_ext(out, len - 3)))
97 strcpyW(&out[len - l - 3], S_ElfW);
98 }
99 while ((*out = tolowerW(*out))) out++;
100 }
101
102 void module_set_module(struct module* module, const WCHAR* name)
103 {
104 module_fill_module(name, module->module.ModuleName, sizeof(module->module.ModuleName));
105 WideCharToMultiByte(CP_ACP, 0, module->module.ModuleName, -1,
106 module->module_name, sizeof(module->module_name),
107 NULL, NULL);
108 }
109
110 static const char* get_module_type(enum module_type type, BOOL virtual)
111 {
112 switch (type)
113 {
114 case DMT_ELF: return virtual ? "Virtual ELF" : "ELF";
115 case DMT_PE: return virtual ? "Virtual PE" : "PE";
116 default: return "---";
117 }
118 }
119
120 /***********************************************************************
121 * Creates and links a new module to a process
122 */
123 struct module* module_new(struct process* pcs, const WCHAR* name,
124 enum module_type type, BOOL virtual,
125 unsigned long mod_addr, unsigned long size,
126 unsigned long stamp, unsigned long checksum)
127 {
128 struct module* module;
129
130 assert(type == DMT_ELF || type == DMT_PE);
131 if (!(module = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*module))))
132 return NULL;
133
134 module->next = pcs->lmodules;
135 pcs->lmodules = module;
136
137 TRACE("=> %s %08lx-%08lx %s\n",
138 get_module_type(type, virtual), mod_addr, mod_addr + size,
139 debugstr_w(name));
140
141 pool_init(&module->pool, 65536);
142
143 module->module.SizeOfStruct = sizeof(module->module);
144 module->module.BaseOfImage = mod_addr;
145 module->module.ImageSize = size;
146 module_set_module(module, name);
147 module->module.ImageName[0] = '\0';
148 lstrcpynW(module->module.LoadedImageName, name, sizeof(module->module.LoadedImageName) / sizeof(WCHAR));
149 module->module.SymType = SymNone;
150 module->module.NumSyms = 0;
151 module->module.TimeDateStamp = stamp;
152 module->module.CheckSum = checksum;
153
154 memset(module->module.LoadedPdbName, 0, sizeof(module->module.CVData));
155 module->module.CVSig = 0;
156 memset(module->module.CVData, 0, sizeof(module->module.CVData));
157 module->module.PdbSig = 0;
158 memset(&module->module.PdbSig70, 0, sizeof(module->module.PdbSig70));
159 module->module.PdbAge = 0;
160 module->module.PdbUnmatched = FALSE;
161 module->module.DbgUnmatched = FALSE;
162 module->module.LineNumbers = FALSE;
163 module->module.GlobalSymbols = FALSE;
164 module->module.TypeInfo = FALSE;
165 module->module.SourceIndexed = FALSE;
166 module->module.Publics = FALSE;
167
168 module->type = type;
169 module->is_virtual = virtual ? TRUE : FALSE;
170 module->sortlist_valid = FALSE;
171 module->addr_sorttab = NULL;
172 /* FIXME: this seems a bit too high (on a per module basis)
173 * need some statistics about this
174 */
175 hash_table_init(&module->pool, &module->ht_symbols, 4096);
176 hash_table_init(&module->pool, &module->ht_types, 4096);
177 vector_init(&module->vtypes, sizeof(struct symt*), 32);
178
179 module->sources_used = 0;
180 module->sources_alloc = 0;
181 module->sources = 0;
182
183 return module;
184 }
185
186 /***********************************************************************
187 * module_find_by_name
188 *
189 */
190 struct module* module_find_by_name(const struct process* pcs, const WCHAR* name)
191 {
192 struct module* module;
193
194 for (module = pcs->lmodules; module; module = module->next)
195 {
196 if (!strcmpiW(name, module->module.ModuleName)) return module;
197 }
198 SetLastError(ERROR_INVALID_NAME);
199 return NULL;
200 }
201
202 struct module* module_find_by_nameA(const struct process* pcs, const char* name)
203 {
204 WCHAR wname[MAX_PATH];
205
206 MultiByteToWideChar(CP_ACP, 0, name, -1, wname, sizeof(wname) / sizeof(WCHAR));
207 return module_find_by_name(pcs, wname);
208 }
209
210 /***********************************************************************
211 * module_is_already_loaded
212 *
213 */
214 struct module* module_is_already_loaded(const struct process* pcs, const WCHAR* name)
215 {
216 struct module* module;
217 const WCHAR* filename;
218
219 /* first compare the loaded image name... */
220 for (module = pcs->lmodules; module; module = module->next)
221 {
222 if (!strcmpiW(name, module->module.LoadedImageName))
223 return module;
224 }
225 /* then compare the standard filenames (without the path) ... */
226 filename = get_filename(name, NULL);
227 for (module = pcs->lmodules; module; module = module->next)
228 {
229 if (!strcmpiW(filename, get_filename(module->module.LoadedImageName, NULL)))
230 return module;
231 }
232 SetLastError(ERROR_INVALID_NAME);
233 return NULL;
234 }
235
236 /***********************************************************************
237 * module_get_container
238 *
239 */
240 struct module* module_get_container(const struct process* pcs,
241 const struct module* inner)
242 {
243 struct module* module;
244
245 for (module = pcs->lmodules; module; module = module->next)
246 {
247 if (module != inner &&
248 module->module.BaseOfImage <= inner->module.BaseOfImage &&
249 module->module.BaseOfImage + module->module.ImageSize >=
250 inner->module.BaseOfImage + inner->module.ImageSize)
251 return module;
252 }
253 return NULL;
254 }
255
256 /***********************************************************************
257 * module_get_containee
258 *
259 */
260 struct module* module_get_containee(const struct process* pcs,
261 const struct module* outter)
262 {
263 struct module* module;
264
265 for (module = pcs->lmodules; module; module = module->next)
266 {
267 if (module != outter &&
268 outter->module.BaseOfImage <= module->module.BaseOfImage &&
269 outter->module.BaseOfImage + outter->module.ImageSize >=
270 module->module.BaseOfImage + module->module.ImageSize)
271 return module;
272 }
273 return NULL;
274 }
275
276 /******************************************************************
277 * module_get_debug
278 *
279 * get the debug information from a module:
280 * - if the module's type is deferred, then force loading of debug info (and return
281 * the module itself)
282 * - if the module has no debug info and has an ELF container, then return the ELF
283 * container (and also force the ELF container's debug info loading if deferred)
284 * - otherwise return the module itself if it has some debug info
285 */
286 BOOL module_get_debug(struct module_pair* pair)
287 {
288 IMAGEHLP_DEFERRED_SYMBOL_LOADW64 idslW64;
289
290 if (!pair->requested) return FALSE;
291 /* for a PE builtin, always get info from container */
292 if (!(pair->effective = module_get_container(pair->pcs, pair->requested)))
293 pair->effective = pair->requested;
294 /* if deferred, force loading */
295 if (pair->effective->module.SymType == SymDeferred)
296 {
297 BOOL ret;
298
299 if (pair->effective->is_virtual) ret = FALSE;
300 else switch (pair->effective->type)
301 {
302 case DMT_ELF:
303 ret = elf_load_debug_info(pair->effective, NULL);
304 break;
305 case DMT_PE:
306 idslW64.SizeOfStruct = sizeof(idslW64);
307 idslW64.BaseOfImage = pair->effective->module.BaseOfImage;
308 idslW64.CheckSum = pair->effective->module.CheckSum;
309 idslW64.TimeDateStamp = pair->effective->module.TimeDateStamp;
310 memcpy(idslW64.FileName, pair->effective->module.ImageName,
311 sizeof(idslW64.FileName));
312 idslW64.Reparse = FALSE;
313 idslW64.hFile = INVALID_HANDLE_VALUE;
314
315 pcs_callback(pair->pcs, CBA_DEFERRED_SYMBOL_LOAD_START, &idslW64);
316 ret = pe_load_debug_info(pair->pcs, pair->effective);
317 pcs_callback(pair->pcs,
318 ret ? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE : CBA_DEFERRED_SYMBOL_LOAD_FAILURE,
319 &idslW64);
320 break;
321 default:
322 ret = FALSE;
323 break;
324 }
325 if (!ret) pair->effective->module.SymType = SymNone;
326 assert(pair->effective->module.SymType != SymDeferred);
327 pair->effective->module.NumSyms = pair->effective->ht_symbols.num_elts;
328 }
329 return pair->effective->module.SymType != SymNone;
330 }
331
332 /***********************************************************************
333 * module_find_by_addr
334 *
335 * either the addr where module is loaded, or any address inside the
336 * module
337 */
338 struct module* module_find_by_addr(const struct process* pcs, unsigned long addr,
339 enum module_type type)
340 {
341 struct module* module;
342
343 if (type == DMT_UNKNOWN)
344 {
345 if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
346 (module = module_find_by_addr(pcs, addr, DMT_ELF)))
347 return module;
348 }
349 else
350 {
351 for (module = pcs->lmodules; module; module = module->next)
352 {
353 if (type == module->type && addr >= module->module.BaseOfImage &&
354 addr < module->module.BaseOfImage + module->module.ImageSize)
355 return module;
356 }
357 }
358 SetLastError(ERROR_INVALID_ADDRESS);
359 return module;
360 }
361
362 /******************************************************************
363 * module_is_elf_container_loaded
364 *
365 * checks whether the ELF container, for a (supposed) PE builtin is
366 * already loaded
367 */
368 static BOOL module_is_elf_container_loaded(const struct process* pcs,
369 const WCHAR* ImageName, DWORD base)
370 {
371 size_t len;
372 struct module* module;
373 PCWSTR filename, modname;
374
375 if (!base) return FALSE;
376 filename = get_filename(ImageName, NULL);
377 len = strlenW(filename);
378
379 for (module = pcs->lmodules; module; module = module->next)
380 {
381 if (module->type == DMT_ELF &&
382 base >= module->module.BaseOfImage &&
383 base < module->module.BaseOfImage + module->module.ImageSize)
384 {
385 modname = get_filename(module->module.LoadedImageName, NULL);
386 if (!strncmpiW(modname, filename, len) &&
387 !memcmp(modname + len, S_DotSoW, 3 * sizeof(WCHAR)))
388 {
389 return TRUE;
390 }
391 }
392 }
393 /* likely a native PE module */
394 WARN("Couldn't find container for %s\n", debugstr_w(ImageName));
395 return FALSE;
396 }
397
398 /******************************************************************
399 * module_get_type_by_name
400 *
401 * Guesses a filename type from its extension
402 */
403 enum module_type module_get_type_by_name(const WCHAR* name)
404 {
405 int len = strlenW(name);
406
407 /* Skip all version extensions (.[digits]) regex: "(\.\d+)*$" */
408 do
409 {
410 int i = len;
411
412 while (i && isdigit(name[i - 1])) i--;
413
414 if (i && name[i - 1] == '.')
415 len = i - 1;
416 else
417 break;
418 } while (len);
419
420 /* check for terminating .so or .so.[digit] */
421 if (len > 3 && !memcmp(name + len - 3, S_DotSoW, 3))
422 return DMT_ELF;
423
424 if (len > 4 && !strncmpiW(name + len - 4, S_DotPdbW, 4))
425 return DMT_PDB;
426
427 /* wine-[kp]thread is also an ELF module */
428 if (((len > 12 && name[len - 13] == '/') || len == 12) &&
429 (!strncmpiW(name + len - 12, S_WinePThreadW, 12) ||
430 !strncmpiW(name + len - 12, S_WineKThreadW, 12)))
431 {
432 return DMT_ELF;
433 }
434 return DMT_PE;
435 }
436
437 /***********************************************************************
438 * SymLoadModule (DBGHELP.@)
439 */
440 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
441 PCSTR ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
442 {
443 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll,
444 SizeOfDll, NULL, 0);
445 }
446
447 /***********************************************************************
448 * SymLoadModuleEx (DBGHELP.@)
449 */
450 DWORD64 WINAPI SymLoadModuleEx(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
451 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD DllSize,
452 PMODLOAD_DATA Data, DWORD Flags)
453 {
454 PWSTR wImageName, wModuleName;
455 unsigned len;
456 DWORD64 ret;
457
458 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
459 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
460 wine_dbgstr_longlong(BaseOfDll), DllSize, Data, Flags);
461
462 if (ImageName)
463 {
464 len = MultiByteToWideChar(CP_ACP, 0, ImageName, -1, NULL, 0);
465 wImageName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
466 MultiByteToWideChar(CP_ACP, 0, ImageName, -1, wImageName, len);
467 }
468 else wImageName = NULL;
469 if (ModuleName)
470 {
471 len = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
472 wModuleName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
473 MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, wModuleName, len);
474 }
475 else wModuleName = NULL;
476
477 ret = SymLoadModuleExW(hProcess, hFile, wImageName, wModuleName,
478 BaseOfDll, DllSize, Data, Flags);
479 HeapFree(GetProcessHeap(), 0, wImageName);
480 HeapFree(GetProcessHeap(), 0, wModuleName);
481 return ret;
482 }
483
484 /***********************************************************************
485 * SymLoadModuleExW (DBGHELP.@)
486 */
487 DWORD64 WINAPI SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageName,
488 PCWSTR wModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll,
489 PMODLOAD_DATA Data, DWORD Flags)
490 {
491 struct process* pcs;
492 struct module* module = NULL;
493
494 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
495 hProcess, hFile, debugstr_w(wImageName), debugstr_w(wModuleName),
496 wine_dbgstr_longlong(BaseOfDll), SizeOfDll, Data, Flags);
497
498 if (Data)
499 FIXME("Unsupported load data parameter %p for %s\n",
500 Data, debugstr_w(wImageName));
501 if (!validate_addr64(BaseOfDll)) return FALSE;
502
503 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
504
505 if (Flags & SLMFLAG_VIRTUAL)
506 {
507 module = module_new(pcs, wImageName, module_get_type_by_name(wImageName),
508 TRUE, (DWORD)BaseOfDll, SizeOfDll, 0, 0);
509 if (!module) return FALSE;
510 if (wModuleName) module_set_module(module, wModuleName);
511 module->module.SymType = SymVirtual;
512
513 return TRUE;
514 }
515 if (Flags & ~(SLMFLAG_VIRTUAL))
516 FIXME("Unsupported Flags %08x for %s\n", Flags, debugstr_w(wImageName));
517
518 /* force transparent ELF loading / unloading */
519 elf_synchronize_module_list(pcs);
520
521 /* this is a Wine extension to the API just to redo the synchronisation */
522 if (!wImageName && !hFile) return 0;
523
524 /* check if the module is already loaded, or if it's a builtin PE module with
525 * an containing ELF module
526 */
527 if (wImageName)
528 {
529 module = module_is_already_loaded(pcs, wImageName);
530 if (!module && module_is_elf_container_loaded(pcs, wImageName, BaseOfDll))
531 {
532 /* force the loading of DLL as builtin */
533 module = pe_load_builtin_module(pcs, wImageName, BaseOfDll, SizeOfDll);
534 }
535 }
536 if (!module)
537 {
538 /* otherwise, try a regular PE module */
539 if (!(module = pe_load_native_module(pcs, wImageName, hFile, BaseOfDll, SizeOfDll)))
540 {
541 /* and finally and ELF module */
542 if (module_get_type_by_name(wImageName) == DMT_ELF)
543 module = elf_load_module(pcs, wImageName, BaseOfDll);
544 }
545 }
546 if (!module)
547 {
548 WARN("Couldn't locate %s\n", debugstr_w(wImageName));
549 return 0;
550 }
551 module->module.NumSyms = module->ht_symbols.num_elts;
552 /* by default module_new fills module.ModuleName from a derivation
553 * of LoadedImageName. Overwrite it, if we have better information
554 */
555 if (wModuleName)
556 module_set_module(module, wModuleName);
557 lstrcpynW(module->module.ImageName, wImageName,
558 sizeof(module->module.ImageName) / sizeof(CHAR));
559
560 return module->module.BaseOfImage;
561 }
562
563 /***********************************************************************
564 * SymLoadModule64 (DBGHELP.@)
565 */
566 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
567 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
568 {
569 if (!validate_addr64(BaseOfDll)) return FALSE;
570 return SymLoadModule(hProcess, hFile, ImageName, ModuleName, (DWORD)BaseOfDll, SizeOfDll);
571 }
572
573 /******************************************************************
574 * module_remove
575 *
576 */
577 BOOL module_remove(struct process* pcs, struct module* module)
578 {
579 struct module** p;
580
581 TRACE("%s (%p)\n", debugstr_w(module->module.ModuleName), module);
582 hash_table_destroy(&module->ht_symbols);
583 hash_table_destroy(&module->ht_types);
584 HeapFree(GetProcessHeap(), 0, module->sources);
585 HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
586 HeapFree(GetProcessHeap(), 0, module->dwarf2_info);
587 pool_destroy(&module->pool);
588 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
589 * so do we
590 */
591 for (p = &pcs->lmodules; *p; p = &(*p)->next)
592 {
593 if (*p == module)
594 {
595 *p = module->next;
596 HeapFree(GetProcessHeap(), 0, module);
597 return TRUE;
598 }
599 }
600 FIXME("This shouldn't happen\n");
601 return FALSE;
602 }
603
604 /******************************************************************
605 * SymUnloadModule (DBGHELP.@)
606 *
607 */
608 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
609 {
610 struct process* pcs;
611 struct module* module;
612
613 pcs = process_find_by_handle(hProcess);
614 if (!pcs) return FALSE;
615 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
616 if (!module) return FALSE;
617 return module_remove(pcs, module);
618 }
619
620 /******************************************************************
621 * SymUnloadModule64 (DBGHELP.@)
622 *
623 */
624 BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
625 {
626 struct process* pcs;
627 struct module* module;
628
629 pcs = process_find_by_handle(hProcess);
630 if (!pcs) return FALSE;
631 if (!validate_addr64(BaseOfDll)) return FALSE;
632 module = module_find_by_addr(pcs, (DWORD)BaseOfDll, DMT_UNKNOWN);
633 if (!module) return FALSE;
634 return module_remove(pcs, module);
635 }
636
637 /******************************************************************
638 * SymEnumerateModules (DBGHELP.@)
639 *
640 */
641 struct enum_modW64_32
642 {
643 PSYM_ENUMMODULES_CALLBACK cb;
644 PVOID user;
645 char module[MAX_PATH];
646 };
647
648 static BOOL CALLBACK enum_modW64_32(PCWSTR name, DWORD64 base, PVOID user)
649 {
650 struct enum_modW64_32* x = user;
651
652 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
653 return x->cb(x->module, (DWORD)base, x->user);
654 }
655
656 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
657 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
658 PVOID UserContext)
659 {
660 struct enum_modW64_32 x;
661
662 x.cb = EnumModulesCallback;
663 x.user = UserContext;
664
665 return SymEnumerateModulesW64(hProcess, enum_modW64_32, &x);
666 }
667
668 /******************************************************************
669 * SymEnumerateModules64 (DBGHELP.@)
670 *
671 */
672 struct enum_modW64_64
673 {
674 PSYM_ENUMMODULES_CALLBACK64 cb;
675 PVOID user;
676 char module[MAX_PATH];
677 };
678
679 static BOOL CALLBACK enum_modW64_64(PCWSTR name, DWORD64 base, PVOID user)
680 {
681 struct enum_modW64_64* x = user;
682
683 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
684 return x->cb(x->module, base, x->user);
685 }
686
687 BOOL WINAPI SymEnumerateModules64(HANDLE hProcess,
688 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,
689 PVOID UserContext)
690 {
691 struct enum_modW64_64 x;
692
693 x.cb = EnumModulesCallback;
694 x.user = UserContext;
695
696 return SymEnumerateModulesW64(hProcess, enum_modW64_64, &x);
697 }
698
699 /******************************************************************
700 * SymEnumerateModulesW64 (DBGHELP.@)
701 *
702 */
703 BOOL WINAPI SymEnumerateModulesW64(HANDLE hProcess,
704 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback,
705 PVOID UserContext)
706 {
707 struct process* pcs = process_find_by_handle(hProcess);
708 struct module* module;
709
710 if (!pcs) return FALSE;
711
712 for (module = pcs->lmodules; module; module = module->next)
713 {
714 if (!(dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES) && module->type == DMT_ELF)
715 continue;
716 if (!EnumModulesCallback(module->module.ModuleName,
717 module->module.BaseOfImage, UserContext))
718 break;
719 }
720 return TRUE;
721 }
722
723 /******************************************************************
724 * EnumerateLoadedModules64 (DBGHELP.@)
725 *
726 */
727 struct enum_load_modW64_64
728 {
729 PENUMLOADED_MODULES_CALLBACK64 cb;
730 PVOID user;
731 char module[MAX_PATH];
732 };
733
734 static BOOL CALLBACK enum_load_modW64_64(PCWSTR name, DWORD64 base, ULONG size,
735 PVOID user)
736 {
737 struct enum_load_modW64_64* x = user;
738
739 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
740 return x->cb(x->module, base, size, x->user);
741 }
742
743 BOOL WINAPI EnumerateLoadedModules64(HANDLE hProcess,
744 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
745 PVOID UserContext)
746 {
747 struct enum_load_modW64_64 x;
748
749 x.cb = EnumLoadedModulesCallback;
750 x.user = UserContext;
751
752 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_64, &x);
753 }
754
755 /******************************************************************
756 * EnumerateLoadedModules (DBGHELP.@)
757 *
758 */
759 struct enum_load_modW64_32
760 {
761 PENUMLOADED_MODULES_CALLBACK cb;
762 PVOID user;
763 char module[MAX_PATH];
764 };
765
766 static BOOL CALLBACK enum_load_modW64_32(PCWSTR name, DWORD64 base, ULONG size,
767 PVOID user)
768 {
769 struct enum_load_modW64_32* x = user;
770 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
771 return x->cb(x->module, (DWORD)base, size, x->user);
772 }
773
774 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
775 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
776 PVOID UserContext)
777 {
778 struct enum_load_modW64_32 x;
779
780 x.cb = EnumLoadedModulesCallback;
781 x.user = UserContext;
782
783 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_32, &x);
784 }
785
786 /******************************************************************
787 * EnumerateLoadedModulesW64 (DBGHELP.@)
788 *
789 */
790 BOOL WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
791 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback,
792 PVOID UserContext)
793 {
794 HMODULE* hMods;
795 WCHAR baseW[256], modW[256];
796 DWORD i, sz;
797 MODULEINFO mi;
798
799 hMods = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods[0]));
800 if (!hMods) return FALSE;
801
802 if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
803 {
804 /* hProcess should also be a valid process handle !! */
805 FIXME("If this happens, bump the number in mod\n");
806 HeapFree(GetProcessHeap(), 0, hMods);
807 return FALSE;
808 }
809 sz /= sizeof(HMODULE);
810 for (i = 0; i < sz; i++)
811 {
812 if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
813 !GetModuleBaseNameW(hProcess, hMods[i], baseW, sizeof(baseW) / sizeof(WCHAR)))
814 continue;
815 module_fill_module(baseW, modW, sizeof(modW) / sizeof(CHAR));
816 EnumLoadedModulesCallback(modW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage,
817 UserContext);
818 }
819 HeapFree(GetProcessHeap(), 0, hMods);
820
821 return sz != 0 && i == sz;
822 }
823
824 /******************************************************************
825 * SymGetModuleInfo (DBGHELP.@)
826 *
827 */
828 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
829 PIMAGEHLP_MODULE ModuleInfo)
830 {
831 IMAGEHLP_MODULE mi;
832 IMAGEHLP_MODULEW64 miw64;
833
834 if (sizeof(mi) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
835
836 miw64.SizeOfStruct = sizeof(miw64);
837 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
838
839 mi.SizeOfStruct = miw64.SizeOfStruct;
840 mi.BaseOfImage = miw64.BaseOfImage;
841 mi.ImageSize = miw64.ImageSize;
842 mi.TimeDateStamp = miw64.TimeDateStamp;
843 mi.CheckSum = miw64.CheckSum;
844 mi.NumSyms = miw64.NumSyms;
845 mi.SymType = miw64.SymType;
846 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
847 mi.ModuleName, sizeof(mi.ModuleName), NULL, NULL);
848 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
849 mi.ImageName, sizeof(mi.ImageName), NULL, NULL);
850 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
851 mi.LoadedImageName, sizeof(mi.LoadedImageName), NULL, NULL);
852
853 memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
854
855 return TRUE;
856 }
857
858 /******************************************************************
859 * SymGetModuleInfoW (DBGHELP.@)
860 *
861 */
862 BOOL WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
863 PIMAGEHLP_MODULEW ModuleInfo)
864 {
865 IMAGEHLP_MODULEW64 miw64;
866 IMAGEHLP_MODULEW miw;
867
868 if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
869
870 miw64.SizeOfStruct = sizeof(miw64);
871 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
872
873 miw.SizeOfStruct = miw64.SizeOfStruct;
874 miw.BaseOfImage = miw64.BaseOfImage;
875 miw.ImageSize = miw64.ImageSize;
876 miw.TimeDateStamp = miw64.TimeDateStamp;
877 miw.CheckSum = miw64.CheckSum;
878 miw.NumSyms = miw64.NumSyms;
879 miw.SymType = miw64.SymType;
880 strcpyW(miw.ModuleName, miw64.ModuleName);
881 strcpyW(miw.ImageName, miw64.ImageName);
882 strcpyW(miw.LoadedImageName, miw64.LoadedImageName);
883 memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
884
885 return TRUE;
886 }
887
888 /******************************************************************
889 * SymGetModuleInfo64 (DBGHELP.@)
890 *
891 */
892 BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
893 PIMAGEHLP_MODULE64 ModuleInfo)
894 {
895 IMAGEHLP_MODULE64 mi64;
896 IMAGEHLP_MODULEW64 miw64;
897
898 if (sizeof(mi64) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
899
900 miw64.SizeOfStruct = sizeof(miw64);
901 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
902
903 mi64.SizeOfStruct = miw64.SizeOfStruct;
904 mi64.BaseOfImage = miw64.BaseOfImage;
905 mi64.ImageSize = miw64.ImageSize;
906 mi64.TimeDateStamp = miw64.TimeDateStamp;
907 mi64.CheckSum = miw64.CheckSum;
908 mi64.NumSyms = miw64.NumSyms;
909 mi64.SymType = miw64.SymType;
910 WideCharToMultiByte(CP_ACP, 0, miw64.ModuleName, -1,
911 mi64.ModuleName, sizeof(mi64.ModuleName), NULL, NULL);
912 WideCharToMultiByte(CP_ACP, 0, miw64.ImageName, -1,
913 mi64.ImageName, sizeof(mi64.ImageName), NULL, NULL);
914 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedImageName, -1,
915 mi64.LoadedImageName, sizeof(mi64.LoadedImageName), NULL, NULL);
916 WideCharToMultiByte(CP_ACP, 0, miw64.LoadedPdbName, -1,
917 mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName), NULL, NULL);
918
919 mi64.CVSig = miw64.CVSig;
920 WideCharToMultiByte(CP_ACP, 0, miw64.CVData, -1,
921 mi64.CVData, sizeof(mi64.CVData), NULL, NULL);
922 mi64.PdbSig = miw64.PdbSig;
923 mi64.PdbSig70 = miw64.PdbSig70;
924 mi64.PdbAge = miw64.PdbAge;
925 mi64.PdbUnmatched = miw64.PdbUnmatched;
926 mi64.DbgUnmatched = miw64.DbgUnmatched;
927 mi64.LineNumbers = miw64.LineNumbers;
928 mi64.GlobalSymbols = miw64.GlobalSymbols;
929 mi64.TypeInfo = miw64.TypeInfo;
930 mi64.SourceIndexed = miw64.SourceIndexed;
931 mi64.Publics = miw64.Publics;
932
933 memcpy(ModuleInfo, &mi64, ModuleInfo->SizeOfStruct);
934
935 return TRUE;
936 }
937
938 /******************************************************************
939 * SymGetModuleInfoW64 (DBGHELP.@)
940 *
941 */
942 BOOL WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr,
943 PIMAGEHLP_MODULEW64 ModuleInfo)
944 {
945 struct process* pcs = process_find_by_handle(hProcess);
946 struct module* module;
947 IMAGEHLP_MODULEW64 miw64;
948
949 TRACE("%p %s %p\n", hProcess, wine_dbgstr_longlong(dwAddr), ModuleInfo);
950
951 if (!pcs) return FALSE;
952 if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
953 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
954 if (!module) return FALSE;
955
956 miw64 = module->module;
957
958 /* update debug information from container if any */
959 if (module->module.SymType == SymNone)
960 {
961 module = module_get_container(pcs, module);
962 if (module && module->module.SymType != SymNone)
963 {
964 miw64.SymType = module->module.SymType;
965 miw64.NumSyms = module->module.NumSyms;
966 }
967 }
968 memcpy(ModuleInfo, &miw64, ModuleInfo->SizeOfStruct);
969 return TRUE;
970 }
971
972 /***********************************************************************
973 * SymGetModuleBase (DBGHELP.@)
974 */
975 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
976 {
977 struct process* pcs = process_find_by_handle(hProcess);
978 struct module* module;
979
980 if (!pcs) return 0;
981 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
982 if (!module) return 0;
983 return module->module.BaseOfImage;
984 }
985
986 /***********************************************************************
987 * SymGetModuleBase64 (DBGHELP.@)
988 */
989 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
990 {
991 if (!validate_addr64(dwAddr)) return 0;
992 return SymGetModuleBase(hProcess, (DWORD)dwAddr);
993 }
994
995 /******************************************************************
996 * module_reset_debug_info
997 * Removes any debug information linked to a given module.
998 */
999 void module_reset_debug_info(struct module* module)
1000 {
1001 module->sortlist_valid = TRUE;
1002 module->addr_sorttab = NULL;
1003 hash_table_destroy(&module->ht_symbols);
1004 module->ht_symbols.num_buckets = 0;
1005 module->ht_symbols.buckets = NULL;
1006 hash_table_destroy(&module->ht_types);
1007 module->ht_types.num_buckets = 0;
1008 module->ht_types.buckets = NULL;
1009 module->vtypes.num_elts = 0;
1010 hash_table_destroy(&module->ht_symbols);
1011 module->sources_used = module->sources_alloc = 0;
1012 module->sources = NULL;
1013 }