kernel32: Return a non-empty username from GetNamedPipeHandleState.
[wine.git] / dlls / dbghelp / module.c
blob9f90d5e0ca683a5351d7bb079d4954922509226d
1 /*
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
22 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <assert.h>
28 #include "dbghelp_private.h"
29 #include "psapi.h"
30 #include "winternl.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_SlashW[] = {'/','\0'};
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};
51 static int match_ext(const WCHAR* ptr, size_t len)
53 const WCHAR* const *e;
54 size_t l;
56 for (e = ext; *e; e++)
58 l = strlenW(*e);
59 if (l >= len) return 0;
60 if (strncmpiW(&ptr[len - l], *e, l)) continue;
61 return l;
63 return 0;
66 static const WCHAR* get_filename(const WCHAR* name, const WCHAR* endptr)
68 const WCHAR* ptr;
70 if (!endptr) endptr = name + strlenW(name);
71 for (ptr = endptr - 1; ptr >= name; ptr--)
73 if (*ptr == '/' || *ptr == '\\') break;
75 return ++ptr;
78 static void module_fill_module(const WCHAR* in, WCHAR* out, size_t size)
80 const WCHAR *loader = get_wine_loader_name();
81 const WCHAR *ptr, *endptr;
82 size_t len, l;
84 ptr = get_filename(in, endptr = in + strlenW(in));
85 len = min(endptr - ptr, size - 1);
86 memcpy(out, ptr, len * sizeof(WCHAR));
87 out[len] = '\0';
88 if (len > 4 && (l = match_ext(out, len)))
89 out[len - l] = '\0';
90 else if (len > strlenW(loader) && !strcmpiW(out + len - strlenW(loader), loader))
91 lstrcpynW(out, S_WineLoaderW, size);
92 else
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,
104 sizeof(module->module.ModuleName) / sizeof(module->module.ModuleName[0]));
105 module_fill_module(name, module->modulename, sizeof(module->modulename) / sizeof(module->modulename[0]));
108 const WCHAR *get_wine_loader_name(void)
110 static const BOOL is_win64 = sizeof(void *) > sizeof(int); /* FIXME: should depend on target process */
111 static const WCHAR wineW[] = {'w','i','n','e',0};
112 static const WCHAR suffixW[] = {'6','4',0};
113 static const WCHAR *loader;
115 if (!loader)
117 WCHAR *p, *buffer;
118 const char *ptr;
120 /* All binaries are loaded with WINELOADER (if run from tree) or by the
121 * main executable
123 if ((ptr = getenv("WINELOADER")))
125 DWORD len = 2 + MultiByteToWideChar( CP_UNIXCP, 0, ptr, -1, NULL, 0 );
126 buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
127 MultiByteToWideChar( CP_UNIXCP, 0, ptr, -1, buffer, len );
129 else
131 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(wineW) + 2 * sizeof(WCHAR) );
132 strcpyW( buffer, wineW );
134 p = buffer + strlenW( buffer ) - strlenW( suffixW );
135 if (p > buffer && !strcmpW( p, suffixW ))
137 if (!is_win64) *p = 0;
139 else if (is_win64) strcatW( buffer, suffixW );
141 TRACE( "returning %s\n", debugstr_w(buffer) );
142 loader = buffer;
144 return loader;
147 static const char* get_module_type(enum module_type type, BOOL virtual)
149 switch (type)
151 case DMT_ELF: return virtual ? "Virtual ELF" : "ELF";
152 case DMT_PE: return virtual ? "Virtual PE" : "PE";
153 case DMT_MACHO: return virtual ? "Virtual Mach-O" : "Mach-O";
154 default: return "---";
158 /***********************************************************************
159 * Creates and links a new module to a process
161 struct module* module_new(struct process* pcs, const WCHAR* name,
162 enum module_type type, BOOL virtual,
163 DWORD64 mod_addr, DWORD64 size,
164 unsigned long stamp, unsigned long checksum)
166 struct module* module;
167 unsigned i;
169 assert(type == DMT_ELF || type == DMT_PE || type == DMT_MACHO);
170 if (!(module = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*module))))
171 return NULL;
173 module->next = pcs->lmodules;
174 pcs->lmodules = module;
176 TRACE("=> %s %s-%s %s\n",
177 get_module_type(type, virtual),
178 wine_dbgstr_longlong(mod_addr), wine_dbgstr_longlong(mod_addr + size),
179 debugstr_w(name));
181 pool_init(&module->pool, 65536);
183 module->process = pcs;
184 module->module.SizeOfStruct = sizeof(module->module);
185 module->module.BaseOfImage = mod_addr;
186 module->module.ImageSize = size;
187 module_set_module(module, name);
188 module->module.ImageName[0] = '\0';
189 lstrcpynW(module->module.LoadedImageName, name, sizeof(module->module.LoadedImageName) / sizeof(WCHAR));
190 module->module.SymType = SymNone;
191 module->module.NumSyms = 0;
192 module->module.TimeDateStamp = stamp;
193 module->module.CheckSum = checksum;
195 memset(module->module.LoadedPdbName, 0, sizeof(module->module.LoadedPdbName));
196 module->module.CVSig = 0;
197 memset(module->module.CVData, 0, sizeof(module->module.CVData));
198 module->module.PdbSig = 0;
199 memset(&module->module.PdbSig70, 0, sizeof(module->module.PdbSig70));
200 module->module.PdbAge = 0;
201 module->module.PdbUnmatched = FALSE;
202 module->module.DbgUnmatched = FALSE;
203 module->module.LineNumbers = FALSE;
204 module->module.GlobalSymbols = FALSE;
205 module->module.TypeInfo = FALSE;
206 module->module.SourceIndexed = FALSE;
207 module->module.Publics = FALSE;
209 module->reloc_delta = 0;
210 module->type = type;
211 module->is_virtual = virtual;
212 for (i = 0; i < DFI_LAST; i++) module->format_info[i] = NULL;
213 module->sortlist_valid = FALSE;
214 module->sorttab_size = 0;
215 module->addr_sorttab = NULL;
216 module->num_sorttab = 0;
217 module->num_symbols = 0;
219 vector_init(&module->vsymt, sizeof(struct symt*), 128);
220 /* FIXME: this seems a bit too high (on a per module basis)
221 * need some statistics about this
223 hash_table_init(&module->pool, &module->ht_symbols, 4096);
224 hash_table_init(&module->pool, &module->ht_types, 4096);
225 vector_init(&module->vtypes, sizeof(struct symt*), 32);
227 module->sources_used = 0;
228 module->sources_alloc = 0;
229 module->sources = 0;
230 wine_rb_init(&module->sources_offsets_tree, source_rb_compare);
232 return module;
235 /***********************************************************************
236 * module_find_by_nameW
239 struct module* module_find_by_nameW(const struct process* pcs, const WCHAR* name)
241 struct module* module;
243 for (module = pcs->lmodules; module; module = module->next)
245 if (!strcmpiW(name, module->module.ModuleName)) return module;
247 SetLastError(ERROR_INVALID_NAME);
248 return NULL;
251 struct module* module_find_by_nameA(const struct process* pcs, const char* name)
253 WCHAR wname[MAX_PATH];
255 MultiByteToWideChar(CP_ACP, 0, name, -1, wname, sizeof(wname) / sizeof(WCHAR));
256 return module_find_by_nameW(pcs, wname);
259 /***********************************************************************
260 * module_is_already_loaded
263 struct module* module_is_already_loaded(const struct process* pcs, const WCHAR* name)
265 struct module* module;
266 const WCHAR* filename;
268 /* first compare the loaded image name... */
269 for (module = pcs->lmodules; module; module = module->next)
271 if (!strcmpiW(name, module->module.LoadedImageName))
272 return module;
274 /* then compare the standard filenames (without the path) ... */
275 filename = get_filename(name, NULL);
276 for (module = pcs->lmodules; module; module = module->next)
278 if (!strcmpiW(filename, get_filename(module->module.LoadedImageName, NULL)))
279 return module;
281 SetLastError(ERROR_INVALID_NAME);
282 return NULL;
285 /***********************************************************************
286 * module_get_container
289 static struct module* module_get_container(const struct process* pcs,
290 const struct module* inner)
292 struct module* module;
294 for (module = pcs->lmodules; module; module = module->next)
296 if (module != inner &&
297 module->module.BaseOfImage <= inner->module.BaseOfImage &&
298 module->module.BaseOfImage + module->module.ImageSize >=
299 inner->module.BaseOfImage + inner->module.ImageSize)
300 return module;
302 return NULL;
305 /***********************************************************************
306 * module_get_containee
309 struct module* module_get_containee(const struct process* pcs,
310 const struct module* outter)
312 struct module* module;
314 for (module = pcs->lmodules; module; module = module->next)
316 if (module != outter &&
317 outter->module.BaseOfImage <= module->module.BaseOfImage &&
318 outter->module.BaseOfImage + outter->module.ImageSize >=
319 module->module.BaseOfImage + module->module.ImageSize)
320 return module;
322 return NULL;
325 /******************************************************************
326 * module_get_debug
328 * get the debug information from a module:
329 * - if the module's type is deferred, then force loading of debug info (and return
330 * the module itself)
331 * - if the module has no debug info and has an ELF container, then return the ELF
332 * container (and also force the ELF container's debug info loading if deferred)
333 * - otherwise return the module itself if it has some debug info
335 BOOL module_get_debug(struct module_pair* pair)
337 IMAGEHLP_DEFERRED_SYMBOL_LOADW64 idslW64;
339 if (!pair->requested) return FALSE;
340 /* for a PE builtin, always get info from container */
341 if (!(pair->effective = module_get_container(pair->pcs, pair->requested)))
342 pair->effective = pair->requested;
343 /* if deferred, force loading */
344 if (pair->effective->module.SymType == SymDeferred)
346 BOOL ret;
348 if (pair->effective->is_virtual) ret = FALSE;
349 else switch (pair->effective->type)
351 case DMT_ELF:
352 ret = elf_load_debug_info(pair->effective);
353 break;
354 case DMT_PE:
355 idslW64.SizeOfStruct = sizeof(idslW64);
356 idslW64.BaseOfImage = pair->effective->module.BaseOfImage;
357 idslW64.CheckSum = pair->effective->module.CheckSum;
358 idslW64.TimeDateStamp = pair->effective->module.TimeDateStamp;
359 memcpy(idslW64.FileName, pair->effective->module.ImageName,
360 sizeof(pair->effective->module.ImageName));
361 idslW64.Reparse = FALSE;
362 idslW64.hFile = INVALID_HANDLE_VALUE;
364 pcs_callback(pair->pcs, CBA_DEFERRED_SYMBOL_LOAD_START, &idslW64);
365 ret = pe_load_debug_info(pair->pcs, pair->effective);
366 pcs_callback(pair->pcs,
367 ret ? CBA_DEFERRED_SYMBOL_LOAD_COMPLETE : CBA_DEFERRED_SYMBOL_LOAD_FAILURE,
368 &idslW64);
369 break;
370 case DMT_MACHO:
371 ret = macho_load_debug_info(pair->effective);
372 break;
373 default:
374 ret = FALSE;
375 break;
377 if (!ret) pair->effective->module.SymType = SymNone;
378 assert(pair->effective->module.SymType != SymDeferred);
379 pair->effective->module.NumSyms = pair->effective->ht_symbols.num_elts;
381 return pair->effective->module.SymType != SymNone;
384 /***********************************************************************
385 * module_find_by_addr
387 * either the addr where module is loaded, or any address inside the
388 * module
390 struct module* module_find_by_addr(const struct process* pcs, DWORD64 addr,
391 enum module_type type)
393 struct module* module;
395 if (type == DMT_UNKNOWN)
397 if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
398 (module = module_find_by_addr(pcs, addr, DMT_ELF)) ||
399 (module = module_find_by_addr(pcs, addr, DMT_MACHO)))
400 return module;
402 else
404 for (module = pcs->lmodules; module; module = module->next)
406 if (type == module->type && addr >= module->module.BaseOfImage &&
407 addr < module->module.BaseOfImage + module->module.ImageSize)
408 return module;
411 SetLastError(ERROR_INVALID_ADDRESS);
412 return module;
415 /******************************************************************
416 * module_is_container_loaded
418 * checks whether the native container, for a (supposed) PE builtin is
419 * already loaded
421 static BOOL module_is_container_loaded(const struct process* pcs,
422 const WCHAR* ImageName, DWORD64 base)
424 size_t len;
425 struct module* module;
426 PCWSTR filename, modname;
428 if (!base) return FALSE;
429 filename = get_filename(ImageName, NULL);
430 len = strlenW(filename);
432 for (module = pcs->lmodules; module; module = module->next)
434 if ((module->type == DMT_ELF || module->type == DMT_MACHO) &&
435 base >= module->module.BaseOfImage &&
436 base < module->module.BaseOfImage + module->module.ImageSize)
438 modname = get_filename(module->module.LoadedImageName, NULL);
439 if (!strncmpiW(modname, filename, len) &&
440 !memcmp(modname + len, S_DotSoW, 3 * sizeof(WCHAR)))
442 return TRUE;
446 /* likely a native PE module */
447 WARN("Couldn't find container for %s\n", debugstr_w(ImageName));
448 return FALSE;
451 /******************************************************************
452 * module_get_type_by_name
454 * Guesses a filename type from its extension
456 enum module_type module_get_type_by_name(const WCHAR* name)
458 int loader_len, len = strlenW(name);
459 const WCHAR *loader;
461 /* Skip all version extensions (.[digits]) regex: "(\.\d+)*$" */
464 int i = len;
466 while (i && name[i - 1] >= '0' && name[i - 1] <= '9') i--;
468 if (i && name[i - 1] == '.')
469 len = i - 1;
470 else
471 break;
472 } while (len);
474 /* check for terminating .so or .so.[digit] */
475 /* FIXME: Can't rely solely on extension; have to check magic or
476 * stop using .so on Mac OS X. For now, base on platform. */
477 if (len > 3 && !memcmp(name + len - 3, S_DotSoW, 3))
478 #ifdef __APPLE__
479 return DMT_MACHO;
480 #else
481 return DMT_ELF;
482 #endif
484 if (len > 6 && !strncmpiW(name + len - 6, S_DotDylibW, 6))
485 return DMT_MACHO;
487 if (len > 4 && !strncmpiW(name + len - 4, S_DotPdbW, 4))
488 return DMT_PDB;
490 if (len > 4 && !strncmpiW(name + len - 4, S_DotDbgW, 4))
491 return DMT_DBG;
493 /* wine is also a native module (Mach-O on Mac OS X, ELF elsewhere) */
494 loader = get_wine_loader_name();
495 loader_len = strlenW( loader );
496 if ((len == loader_len || (len > loader_len && name[len - loader_len - 1] == '/')) &&
497 !strcmpiW(name + len - loader_len, loader))
499 #ifdef __APPLE__
500 return DMT_MACHO;
501 #else
502 return DMT_ELF;
503 #endif
505 return DMT_PE;
508 /******************************************************************
509 * refresh_module_list
511 static BOOL refresh_module_list(struct process* pcs)
513 /* force transparent ELF and Mach-O loading / unloading */
514 return elf_synchronize_module_list(pcs) || macho_synchronize_module_list(pcs);
517 /***********************************************************************
518 * SymLoadModule (DBGHELP.@)
520 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
521 PCSTR ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
523 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll,
524 SizeOfDll, NULL, 0);
527 /***********************************************************************
528 * SymLoadModuleEx (DBGHELP.@)
530 DWORD64 WINAPI SymLoadModuleEx(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
531 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD DllSize,
532 PMODLOAD_DATA Data, DWORD Flags)
534 PWSTR wImageName, wModuleName;
535 unsigned len;
536 DWORD64 ret;
538 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
539 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
540 wine_dbgstr_longlong(BaseOfDll), DllSize, Data, Flags);
542 if (ImageName)
544 len = MultiByteToWideChar(CP_ACP, 0, ImageName, -1, NULL, 0);
545 wImageName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
546 MultiByteToWideChar(CP_ACP, 0, ImageName, -1, wImageName, len);
548 else wImageName = NULL;
549 if (ModuleName)
551 len = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
552 wModuleName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
553 MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, wModuleName, len);
555 else wModuleName = NULL;
557 ret = SymLoadModuleExW(hProcess, hFile, wImageName, wModuleName,
558 BaseOfDll, DllSize, Data, Flags);
559 HeapFree(GetProcessHeap(), 0, wImageName);
560 HeapFree(GetProcessHeap(), 0, wModuleName);
561 return ret;
564 /***********************************************************************
565 * SymLoadModuleExW (DBGHELP.@)
567 DWORD64 WINAPI SymLoadModuleExW(HANDLE hProcess, HANDLE hFile, PCWSTR wImageName,
568 PCWSTR wModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll,
569 PMODLOAD_DATA Data, DWORD Flags)
571 struct process* pcs;
572 struct module* module = NULL;
574 TRACE("(%p %p %s %s %s %08x %p %08x)\n",
575 hProcess, hFile, debugstr_w(wImageName), debugstr_w(wModuleName),
576 wine_dbgstr_longlong(BaseOfDll), SizeOfDll, Data, Flags);
578 if (Data)
579 FIXME("Unsupported load data parameter %p for %s\n",
580 Data, debugstr_w(wImageName));
581 if (!validate_addr64(BaseOfDll)) return FALSE;
583 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
585 if (Flags & SLMFLAG_VIRTUAL)
587 if (!wImageName) return FALSE;
588 module = module_new(pcs, wImageName, module_get_type_by_name(wImageName),
589 TRUE, BaseOfDll, SizeOfDll, 0, 0);
590 if (!module) return FALSE;
591 if (wModuleName) module_set_module(module, wModuleName);
592 module->module.SymType = SymVirtual;
594 return TRUE;
596 if (Flags & ~(SLMFLAG_VIRTUAL))
597 FIXME("Unsupported Flags %08x for %s\n", Flags, debugstr_w(wImageName));
599 refresh_module_list(pcs);
601 /* this is a Wine extension to the API just to redo the synchronisation */
602 if (!wImageName && !hFile) return 0;
604 /* check if the module is already loaded, or if it's a builtin PE module with
605 * an containing ELF module
607 if (wImageName)
609 module = module_is_already_loaded(pcs, wImageName);
610 if (!module && module_is_container_loaded(pcs, wImageName, BaseOfDll))
612 /* force the loading of DLL as builtin */
613 module = pe_load_builtin_module(pcs, wImageName, BaseOfDll, SizeOfDll);
616 if (!module)
618 /* otherwise, try a regular PE module */
619 if (!(module = pe_load_native_module(pcs, wImageName, hFile, BaseOfDll, SizeOfDll)) &&
620 wImageName)
622 /* and finally an ELF or Mach-O module */
623 switch (module_get_type_by_name(wImageName))
625 case DMT_ELF:
626 module = elf_load_module(pcs, wImageName, BaseOfDll);
627 break;
628 case DMT_MACHO:
629 module = macho_load_module(pcs, wImageName, BaseOfDll);
630 break;
631 default:
632 /* Ignored */
633 break;
637 if (!module)
639 WARN("Couldn't locate %s\n", debugstr_w(wImageName));
640 return 0;
642 module->module.NumSyms = module->ht_symbols.num_elts;
643 /* by default module_new fills module.ModuleName from a derivation
644 * of LoadedImageName. Overwrite it, if we have better information
646 if (wModuleName)
647 module_set_module(module, wModuleName);
648 if (wImageName)
649 lstrcpynW(module->module.ImageName, wImageName,
650 sizeof(module->module.ImageName) / sizeof(WCHAR));
652 return module->module.BaseOfImage;
655 /***********************************************************************
656 * SymLoadModule64 (DBGHELP.@)
658 DWORD64 WINAPI SymLoadModule64(HANDLE hProcess, HANDLE hFile, PCSTR ImageName,
659 PCSTR ModuleName, DWORD64 BaseOfDll, DWORD SizeOfDll)
661 return SymLoadModuleEx(hProcess, hFile, ImageName, ModuleName, BaseOfDll, SizeOfDll,
662 NULL, 0);
665 /******************************************************************
666 * module_remove
669 BOOL module_remove(struct process* pcs, struct module* module)
671 struct module_format*modfmt;
672 struct module** p;
673 unsigned i;
675 TRACE("%s (%p)\n", debugstr_w(module->module.ModuleName), module);
677 for (i = 0; i < DFI_LAST; i++)
679 if ((modfmt = module->format_info[i]) && modfmt->remove)
680 modfmt->remove(pcs, module->format_info[i]);
682 hash_table_destroy(&module->ht_symbols);
683 hash_table_destroy(&module->ht_types);
684 HeapFree(GetProcessHeap(), 0, module->sources);
685 HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
686 pool_destroy(&module->pool);
687 /* native dbghelp doesn't invoke registered callback(,CBA_SYMBOLS_UNLOADED,) here
688 * so do we
690 for (p = &pcs->lmodules; *p; p = &(*p)->next)
692 if (*p == module)
694 *p = module->next;
695 HeapFree(GetProcessHeap(), 0, module);
696 return TRUE;
699 FIXME("This shouldn't happen\n");
700 return FALSE;
703 /******************************************************************
704 * SymUnloadModule (DBGHELP.@)
707 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
709 struct process* pcs;
710 struct module* module;
712 pcs = process_find_by_handle(hProcess);
713 if (!pcs) return FALSE;
714 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
715 if (!module) return FALSE;
716 return module_remove(pcs, module);
719 /******************************************************************
720 * SymUnloadModule64 (DBGHELP.@)
723 BOOL WINAPI SymUnloadModule64(HANDLE hProcess, DWORD64 BaseOfDll)
725 struct process* pcs;
726 struct module* module;
728 pcs = process_find_by_handle(hProcess);
729 if (!pcs) return FALSE;
730 if (!validate_addr64(BaseOfDll)) return FALSE;
731 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
732 if (!module) return FALSE;
733 return module_remove(pcs, module);
736 /******************************************************************
737 * SymEnumerateModules (DBGHELP.@)
740 struct enum_modW64_32
742 PSYM_ENUMMODULES_CALLBACK cb;
743 PVOID user;
744 char module[MAX_PATH];
747 static BOOL CALLBACK enum_modW64_32(PCWSTR name, DWORD64 base, PVOID user)
749 struct enum_modW64_32* x = user;
751 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
752 return x->cb(x->module, (DWORD)base, x->user);
755 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
756 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
757 PVOID UserContext)
759 struct enum_modW64_32 x;
761 x.cb = EnumModulesCallback;
762 x.user = UserContext;
764 return SymEnumerateModulesW64(hProcess, enum_modW64_32, &x);
767 /******************************************************************
768 * SymEnumerateModules64 (DBGHELP.@)
771 struct enum_modW64_64
773 PSYM_ENUMMODULES_CALLBACK64 cb;
774 PVOID user;
775 char module[MAX_PATH];
778 static BOOL CALLBACK enum_modW64_64(PCWSTR name, DWORD64 base, PVOID user)
780 struct enum_modW64_64* x = user;
782 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
783 return x->cb(x->module, base, x->user);
786 BOOL WINAPI SymEnumerateModules64(HANDLE hProcess,
787 PSYM_ENUMMODULES_CALLBACK64 EnumModulesCallback,
788 PVOID UserContext)
790 struct enum_modW64_64 x;
792 x.cb = EnumModulesCallback;
793 x.user = UserContext;
795 return SymEnumerateModulesW64(hProcess, enum_modW64_64, &x);
798 /******************************************************************
799 * SymEnumerateModulesW64 (DBGHELP.@)
802 BOOL WINAPI SymEnumerateModulesW64(HANDLE hProcess,
803 PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback,
804 PVOID UserContext)
806 struct process* pcs = process_find_by_handle(hProcess);
807 struct module* module;
809 if (!pcs) return FALSE;
811 for (module = pcs->lmodules; module; module = module->next)
813 if (!(dbghelp_options & SYMOPT_WINE_WITH_NATIVE_MODULES) &&
814 (module->type == DMT_ELF || module->type == DMT_MACHO))
815 continue;
816 if (!EnumModulesCallback(module->modulename,
817 module->module.BaseOfImage, UserContext))
818 break;
820 return TRUE;
823 /******************************************************************
824 * EnumerateLoadedModules64 (DBGHELP.@)
827 struct enum_load_modW64_64
829 PENUMLOADED_MODULES_CALLBACK64 cb;
830 PVOID user;
831 char module[MAX_PATH];
834 static BOOL CALLBACK enum_load_modW64_64(PCWSTR name, DWORD64 base, ULONG size,
835 PVOID user)
837 struct enum_load_modW64_64* x = user;
839 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
840 return x->cb(x->module, base, size, x->user);
843 BOOL WINAPI EnumerateLoadedModules64(HANDLE hProcess,
844 PENUMLOADED_MODULES_CALLBACK64 EnumLoadedModulesCallback,
845 PVOID UserContext)
847 struct enum_load_modW64_64 x;
849 x.cb = EnumLoadedModulesCallback;
850 x.user = UserContext;
852 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_64, &x);
855 /******************************************************************
856 * EnumerateLoadedModules (DBGHELP.@)
859 struct enum_load_modW64_32
861 PENUMLOADED_MODULES_CALLBACK cb;
862 PVOID user;
863 char module[MAX_PATH];
866 static BOOL CALLBACK enum_load_modW64_32(PCWSTR name, DWORD64 base, ULONG size,
867 PVOID user)
869 struct enum_load_modW64_32* x = user;
870 WideCharToMultiByte(CP_ACP, 0, name, -1, x->module, sizeof(x->module), NULL, NULL);
871 return x->cb(x->module, (DWORD)base, size, x->user);
874 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
875 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
876 PVOID UserContext)
878 struct enum_load_modW64_32 x;
880 x.cb = EnumLoadedModulesCallback;
881 x.user = UserContext;
883 return EnumerateLoadedModulesW64(hProcess, enum_load_modW64_32, &x);
886 /******************************************************************
887 * EnumerateLoadedModulesW64 (DBGHELP.@)
890 BOOL WINAPI EnumerateLoadedModulesW64(HANDLE hProcess,
891 PENUMLOADED_MODULES_CALLBACKW64 EnumLoadedModulesCallback,
892 PVOID UserContext)
894 HMODULE* hMods;
895 WCHAR baseW[256], modW[256];
896 DWORD i, sz;
897 MODULEINFO mi;
899 hMods = HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(hMods[0]));
900 if (!hMods) return FALSE;
902 if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
904 /* hProcess should also be a valid process handle !! */
905 FIXME("If this happens, bump the number in mod\n");
906 HeapFree(GetProcessHeap(), 0, hMods);
907 return FALSE;
909 sz /= sizeof(HMODULE);
910 for (i = 0; i < sz; i++)
912 if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
913 !GetModuleBaseNameW(hProcess, hMods[i], baseW, sizeof(baseW) / sizeof(WCHAR)))
914 continue;
915 module_fill_module(baseW, modW, sizeof(modW) / sizeof(modW[0]));
916 EnumLoadedModulesCallback(modW, (DWORD_PTR)mi.lpBaseOfDll, mi.SizeOfImage,
917 UserContext);
919 HeapFree(GetProcessHeap(), 0, hMods);
921 return sz != 0 && i == sz;
924 static void dbghelp_str_WtoA(const WCHAR *src, char *dst, int dst_len)
926 WideCharToMultiByte(CP_ACP, 0, src, -1, dst, dst_len - 1, NULL, NULL);
927 dst[dst_len - 1] = 0;
930 /******************************************************************
931 * SymGetModuleInfo (DBGHELP.@)
934 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
935 PIMAGEHLP_MODULE ModuleInfo)
937 IMAGEHLP_MODULE mi;
938 IMAGEHLP_MODULEW64 miw64;
940 if (sizeof(mi) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
942 miw64.SizeOfStruct = sizeof(miw64);
943 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
945 mi.SizeOfStruct = ModuleInfo->SizeOfStruct;
946 mi.BaseOfImage = miw64.BaseOfImage;
947 mi.ImageSize = miw64.ImageSize;
948 mi.TimeDateStamp = miw64.TimeDateStamp;
949 mi.CheckSum = miw64.CheckSum;
950 mi.NumSyms = miw64.NumSyms;
951 mi.SymType = miw64.SymType;
952 dbghelp_str_WtoA(miw64.ModuleName, mi.ModuleName, sizeof(mi.ModuleName));
953 dbghelp_str_WtoA(miw64.ImageName, mi.ImageName, sizeof(mi.ImageName));
954 dbghelp_str_WtoA(miw64.LoadedImageName, mi.LoadedImageName, sizeof(mi.LoadedImageName));
956 memcpy(ModuleInfo, &mi, ModuleInfo->SizeOfStruct);
958 return TRUE;
961 /******************************************************************
962 * SymGetModuleInfoW (DBGHELP.@)
965 BOOL WINAPI SymGetModuleInfoW(HANDLE hProcess, DWORD dwAddr,
966 PIMAGEHLP_MODULEW ModuleInfo)
968 IMAGEHLP_MODULEW64 miw64;
969 IMAGEHLP_MODULEW miw;
971 if (sizeof(miw) < ModuleInfo->SizeOfStruct) FIXME("Wrong size\n");
973 miw64.SizeOfStruct = sizeof(miw64);
974 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
976 miw.SizeOfStruct = ModuleInfo->SizeOfStruct;
977 miw.BaseOfImage = miw64.BaseOfImage;
978 miw.ImageSize = miw64.ImageSize;
979 miw.TimeDateStamp = miw64.TimeDateStamp;
980 miw.CheckSum = miw64.CheckSum;
981 miw.NumSyms = miw64.NumSyms;
982 miw.SymType = miw64.SymType;
983 strcpyW(miw.ModuleName, miw64.ModuleName);
984 strcpyW(miw.ImageName, miw64.ImageName);
985 strcpyW(miw.LoadedImageName, miw64.LoadedImageName);
986 memcpy(ModuleInfo, &miw, ModuleInfo->SizeOfStruct);
988 return TRUE;
991 /******************************************************************
992 * SymGetModuleInfo64 (DBGHELP.@)
995 BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr,
996 PIMAGEHLP_MODULE64 ModuleInfo)
998 IMAGEHLP_MODULE64 mi64;
999 IMAGEHLP_MODULEW64 miw64;
1001 if (sizeof(mi64) < ModuleInfo->SizeOfStruct)
1003 SetLastError(ERROR_MOD_NOT_FOUND); /* NOTE: native returns this error */
1004 WARN("Wrong size %u\n", ModuleInfo->SizeOfStruct);
1005 return FALSE;
1008 miw64.SizeOfStruct = sizeof(miw64);
1009 if (!SymGetModuleInfoW64(hProcess, dwAddr, &miw64)) return FALSE;
1011 mi64.SizeOfStruct = ModuleInfo->SizeOfStruct;
1012 mi64.BaseOfImage = miw64.BaseOfImage;
1013 mi64.ImageSize = miw64.ImageSize;
1014 mi64.TimeDateStamp = miw64.TimeDateStamp;
1015 mi64.CheckSum = miw64.CheckSum;
1016 mi64.NumSyms = miw64.NumSyms;
1017 mi64.SymType = miw64.SymType;
1018 dbghelp_str_WtoA(miw64.ModuleName, mi64.ModuleName, sizeof(mi64.ModuleName));
1019 dbghelp_str_WtoA(miw64.ImageName, mi64.ImageName, sizeof(mi64.ImageName));
1020 dbghelp_str_WtoA(miw64.LoadedImageName, mi64.LoadedImageName, sizeof(mi64.LoadedImageName));
1021 dbghelp_str_WtoA(miw64.LoadedPdbName, mi64.LoadedPdbName, sizeof(mi64.LoadedPdbName));
1023 mi64.CVSig = miw64.CVSig;
1024 dbghelp_str_WtoA(miw64.CVData, mi64.CVData, sizeof(mi64.CVData));
1025 mi64.PdbSig = miw64.PdbSig;
1026 mi64.PdbSig70 = miw64.PdbSig70;
1027 mi64.PdbAge = miw64.PdbAge;
1028 mi64.PdbUnmatched = miw64.PdbUnmatched;
1029 mi64.DbgUnmatched = miw64.DbgUnmatched;
1030 mi64.LineNumbers = miw64.LineNumbers;
1031 mi64.GlobalSymbols = miw64.GlobalSymbols;
1032 mi64.TypeInfo = miw64.TypeInfo;
1033 mi64.SourceIndexed = miw64.SourceIndexed;
1034 mi64.Publics = miw64.Publics;
1036 memcpy(ModuleInfo, &mi64, ModuleInfo->SizeOfStruct);
1038 return TRUE;
1041 /******************************************************************
1042 * SymGetModuleInfoW64 (DBGHELP.@)
1045 BOOL WINAPI SymGetModuleInfoW64(HANDLE hProcess, DWORD64 dwAddr,
1046 PIMAGEHLP_MODULEW64 ModuleInfo)
1048 struct process* pcs = process_find_by_handle(hProcess);
1049 struct module* module;
1050 IMAGEHLP_MODULEW64 miw64;
1052 TRACE("%p %s %p\n", hProcess, wine_dbgstr_longlong(dwAddr), ModuleInfo);
1054 if (!pcs) return FALSE;
1055 if (ModuleInfo->SizeOfStruct > sizeof(*ModuleInfo)) return FALSE;
1056 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1057 if (!module) return FALSE;
1059 miw64 = module->module;
1061 /* update debug information from container if any */
1062 if (module->module.SymType == SymNone)
1064 module = module_get_container(pcs, module);
1065 if (module && module->module.SymType != SymNone)
1067 miw64.SymType = module->module.SymType;
1068 miw64.NumSyms = module->module.NumSyms;
1071 memcpy(ModuleInfo, &miw64, ModuleInfo->SizeOfStruct);
1072 return TRUE;
1075 /***********************************************************************
1076 * SymGetModuleBase (DBGHELP.@)
1078 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
1080 DWORD64 ret;
1082 ret = SymGetModuleBase64(hProcess, dwAddr);
1083 return validate_addr64(ret) ? ret : 0;
1086 /***********************************************************************
1087 * SymGetModuleBase64 (DBGHELP.@)
1089 DWORD64 WINAPI SymGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr)
1091 struct process* pcs = process_find_by_handle(hProcess);
1092 struct module* module;
1094 if (!pcs) return 0;
1095 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
1096 if (!module) return 0;
1097 return module->module.BaseOfImage;
1100 /******************************************************************
1101 * module_reset_debug_info
1102 * Removes any debug information linked to a given module.
1104 void module_reset_debug_info(struct module* module)
1106 module->sortlist_valid = TRUE;
1107 module->sorttab_size = 0;
1108 module->addr_sorttab = NULL;
1109 module->num_sorttab = module->num_symbols = 0;
1110 hash_table_destroy(&module->ht_symbols);
1111 module->ht_symbols.num_buckets = 0;
1112 module->ht_symbols.buckets = NULL;
1113 hash_table_destroy(&module->ht_types);
1114 module->ht_types.num_buckets = 0;
1115 module->ht_types.buckets = NULL;
1116 module->vtypes.num_elts = 0;
1117 hash_table_destroy(&module->ht_symbols);
1118 module->sources_used = module->sources_alloc = 0;
1119 module->sources = NULL;
1122 /******************************************************************
1123 * SymRefreshModuleList (DBGHELP.@)
1125 BOOL WINAPI SymRefreshModuleList(HANDLE hProcess)
1127 struct process* pcs;
1129 TRACE("(%p)\n", hProcess);
1131 if (!(pcs = process_find_by_handle(hProcess))) return FALSE;
1133 return refresh_module_list(pcs);
1136 /***********************************************************************
1137 * SymFunctionTableAccess (DBGHELP.@)
1139 PVOID WINAPI SymFunctionTableAccess(HANDLE hProcess, DWORD AddrBase)
1141 return SymFunctionTableAccess64(hProcess, AddrBase);
1144 /***********************************************************************
1145 * SymFunctionTableAccess64 (DBGHELP.@)
1147 PVOID WINAPI SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase)
1149 struct process* pcs = process_find_by_handle(hProcess);
1150 struct module* module;
1152 if (!pcs || !dbghelp_current_cpu->find_runtime_function) return NULL;
1153 module = module_find_by_addr(pcs, AddrBase, DMT_UNKNOWN);
1154 if (!module) return NULL;
1156 return dbghelp_current_cpu->find_runtime_function(module, AddrBase);