Dump DirectSound capabilities flags.
[wine.git] / dlls / dbghelp / module.c
blob22167ae995ab49d53cbe0b9f7e37000838862fa9
1 /*
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
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 "winreg.h"
31 #include "winternl.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
36 static void module_fill_module(const char* in, char* out, unsigned size)
38 const char* ptr;
39 unsigned len;
41 for (ptr = in + strlen(in) - 1;
42 *ptr != '/' && *ptr != '\\' && ptr >= in;
43 ptr--);
44 if (ptr < in || *ptr == '/' || *ptr == '\\') ptr++;
45 strncpy(out, ptr, size);
46 out[size - 1] = '\0';
47 len = strlen(out);
48 if (len > 4 &&
49 (!strcasecmp(&out[len - 4], ".dll") || !strcasecmp(&out[len - 4], ".exe")))
50 out[len - 4] = '\0';
51 else
53 if (len > 7 &&
54 (!strcasecmp(&out[len - 7], ".dll.so") || !strcasecmp(&out[len - 7], ".exe.so")))
55 strcpy(&out[len - 7], "<elf>");
56 else if (len > 7 &&
57 out[len - 7] == '.' && !strcasecmp(&out[len - 3], ".so"))
59 if (len + 3 < size) strcpy(&out[len - 3], "<elf>");
60 else WARN("Buffer too short: %s\n", out);
63 while ((*out = tolower(*out))) out++;
66 /***********************************************************************
67 * Creates and links a new module to a process
69 struct module* module_new(struct process* pcs, const char* name,
70 enum module_type type,
71 unsigned long mod_addr, unsigned long size,
72 unsigned long stamp, unsigned long checksum)
74 struct module* module;
76 if (!(module = HeapAlloc(GetProcessHeap(), 0, sizeof(*module))))
77 return NULL;
79 memset(module, 0, sizeof(*module));
81 module->next = pcs->lmodules;
82 pcs->lmodules = module;
84 TRACE("=> %s %08lx-%08lx %s\n",
85 type == DMT_ELF ? "ELF" : (type == DMT_PE ? "PE" : "---"),
86 mod_addr, mod_addr + size, name);
88 pool_init(&module->pool, 65536);
90 module->module.SizeOfStruct = sizeof(module->module);
91 module->module.BaseOfImage = mod_addr;
92 module->module.ImageSize = size;
93 module_fill_module(name, module->module.ModuleName, sizeof(module->module.ModuleName));
94 module->module.ImageName[0] = '\0';
95 strncpy(module->module.LoadedImageName, name,
96 sizeof(module->module.LoadedImageName));
97 module->module.LoadedImageName[sizeof(module->module.LoadedImageName) - 1] = '\0';
98 module->module.SymType = SymNone;
99 module->module.NumSyms = 0;
100 module->module.TimeDateStamp = stamp;
101 module->module.CheckSum = checksum;
103 module->type = type;
104 module->sortlist_valid = FALSE;
105 module->addr_sorttab = NULL;
106 /* FIXME: this seems a bit too high (on a per module basis)
107 * need some statistics about this
109 hash_table_init(&module->pool, &module->ht_symbols, 4096);
110 hash_table_init(&module->pool, &module->ht_types, 4096);
112 module->sources_used = 0;
113 module->sources_alloc = 0;
114 module->sources = 0;
116 return module;
119 /***********************************************************************
120 * module_find_by_name
123 struct module* module_find_by_name(const struct process* pcs,
124 const char* name, enum module_type type)
126 struct module* module;
128 if (type == DMT_UNKNOWN)
130 if ((module = module_find_by_name(pcs, name, DMT_PE)) ||
131 (module = module_find_by_name(pcs, name, DMT_ELF)))
132 return module;
134 else
136 for (module = pcs->lmodules; module; module = module->next)
138 if (type == module->type && !strcasecmp(name, module->module.LoadedImageName))
139 return module;
141 for (module = pcs->lmodules; module; module = module->next)
143 if (type == module->type && !strcasecmp(name, module->module.ModuleName))
144 return module;
147 SetLastError(ERROR_INVALID_NAME);
148 return NULL;
151 /***********************************************************************
152 * module_get_container
155 struct module* module_get_container(const struct process* pcs,
156 const struct module* inner)
158 struct module* module;
160 for (module = pcs->lmodules; module; module = module->next)
162 if (module != inner &&
163 module->module.BaseOfImage <= inner->module.BaseOfImage &&
164 module->module.BaseOfImage + module->module.ImageSize >=
165 inner->module.BaseOfImage + inner->module.ImageSize)
166 return module;
168 return NULL;
171 /***********************************************************************
172 * module_get_containee
175 struct module* module_get_containee(const struct process* pcs,
176 const struct module* outter)
178 struct module* module;
180 for (module = pcs->lmodules; module; module = module->next)
182 if (module != outter &&
183 outter->module.BaseOfImage <= module->module.BaseOfImage &&
184 outter->module.BaseOfImage + outter->module.ImageSize >=
185 module->module.BaseOfImage + module->module.ImageSize)
186 return module;
188 return NULL;
191 /******************************************************************
192 * module_get_debug
194 * get the debug information from a module:
195 * - if the module's type is deferred, then force loading of debug info (and return
196 * the module itself)
197 * - if the module has no debug info and has an ELF container, then return the ELF
198 * container (and also force the ELF container's debug info loading if deferred)
199 * - otherwise return the module itself if it has some debug info
201 struct module* module_get_debug(const struct process* pcs, struct module* module)
203 if (!module) return NULL;
204 switch (module->module.SymType)
206 case -1: break;
207 case SymNone:
208 module = module_get_container(pcs, module);
209 if (!module || module->module.SymType != SymDeferred) break;
210 /* fall through */
211 case SymDeferred:
212 switch (module->type)
214 case DMT_ELF:
215 elf_load_debug_info(module);
216 break;
217 case DMT_PE:
218 pe_load_debug_info(pcs, module);
219 break;
220 default: break;
222 break;
223 default: break;
225 return (module && module->module.SymType > SymNone) ? module : NULL;
228 /***********************************************************************
229 * module_find_by_addr
231 * either the addr where module is loaded, or any address inside the
232 * module
234 struct module* module_find_by_addr(const struct process* pcs, unsigned long addr,
235 enum module_type type)
237 struct module* module;
239 if (type == DMT_UNKNOWN)
241 if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
242 (module = module_find_by_addr(pcs, addr, DMT_ELF)))
243 return module;
245 else
247 for (module = pcs->lmodules; module; module = module->next)
249 if (type == module->type && addr >= module->module.BaseOfImage &&
250 addr < module->module.BaseOfImage + module->module.ImageSize)
251 return module;
254 SetLastError(ERROR_INVALID_ADDRESS);
255 return module;
258 static BOOL module_is_elf_container_loaded(struct process* pcs, const char* ImageName,
259 const char* ModuleName)
261 char buffer[MAX_PATH];
262 size_t len;
263 struct module* module;
265 if (!ModuleName)
267 module_fill_module(ImageName, buffer, sizeof(buffer));
268 ModuleName = buffer;
270 len = strlen(ModuleName);
271 for (module = pcs->lmodules; module; module = module->next)
273 if (!strncasecmp(module->module.ModuleName, ModuleName, len) &&
274 module->type == DMT_ELF &&
275 !strcmp(module->module.ModuleName + len, "<elf>"))
276 return TRUE;
278 return FALSE;
281 /***********************************************************************
282 * SymLoadModule (DBGHELP.@)
284 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, char* ImageName,
285 char* ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
287 struct process* pcs;
288 struct module* module = NULL;
290 TRACE("(%p %p %s %s %08lx %08lx)\n",
291 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
292 BaseOfDll, SizeOfDll);
294 pcs = process_find_by_handle(hProcess);
295 if (!pcs) return FALSE;
297 /* force transparent ELF loading / unloading */
298 elf_synchronize_module_list(pcs);
300 /* this is a Wine extension to the API just to redo the synchronisation */
301 if (!ImageName && !hFile) return 0;
303 if (module_is_elf_container_loaded(pcs, ImageName, ModuleName))
305 /* force the loading of DLL as builtin */
306 if ((module = pe_load_module_from_pcs(pcs, ImageName, ModuleName, BaseOfDll, SizeOfDll)))
307 goto done;
308 WARN("Couldn't locate %s\n", ImageName);
309 return 0;
311 TRACE("Assuming %s as native DLL\n", ImageName);
312 if (!(module = pe_load_module(pcs, ImageName, hFile, BaseOfDll, SizeOfDll)))
314 unsigned len = strlen(ImageName);
316 if (!strcmp(ImageName + len - 3, ".so") &&
317 (module = elf_load_module(pcs, ImageName))) goto done;
318 FIXME("should no longer happen\n");
319 if ((module = pe_load_module_from_pcs(pcs, ImageName, ModuleName, BaseOfDll, SizeOfDll)))
320 goto done;
321 WARN("Couldn't locate %s\n", ImageName);
322 return 0;
325 done:
326 /* by default pe_load_module fills module.ModuleName from a derivation
327 * of ImageName. Overwrite it, if we have better information
329 if (ModuleName)
331 strncpy(module->module.ModuleName, ModuleName,
332 sizeof(module->module.ModuleName));
333 module->module.ModuleName[sizeof(module->module.ModuleName) - 1] = '\0';
335 strncpy(module->module.ImageName, ImageName, sizeof(module->module.ImageName));
336 module->module.ImageName[sizeof(module->module.ImageName) - 1] = '\0';
338 return module->module.BaseOfImage;
341 /******************************************************************
342 * module_remove
345 BOOL module_remove(struct process* pcs, struct module* module)
347 struct module** p;
349 TRACE("%s (%p)\n", module->module.ModuleName, module);
350 hash_table_destroy(&module->ht_symbols);
351 hash_table_destroy(&module->ht_types);
352 HeapFree(GetProcessHeap(), 0, (char*)module->sources);
353 HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
354 pool_destroy(&module->pool);
356 for (p = &pcs->lmodules; *p; p = &(*p)->next)
358 if (*p == module)
360 *p = module->next;
361 HeapFree(GetProcessHeap(), 0, module);
362 return TRUE;
365 FIXME("This shouldn't happen\n");
366 return FALSE;
369 /******************************************************************
370 * SymUnloadModule (DBGHELP.@)
373 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
375 struct process* pcs;
376 struct module* module;
378 pcs = process_find_by_handle(hProcess);
379 if (!pcs) return FALSE;
380 module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
381 if (!module) return FALSE;
382 return module_remove(pcs, module);
385 /******************************************************************
386 * SymEnumerateModules (DBGHELP.@)
389 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
390 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
391 PVOID UserContext)
393 struct process* pcs = process_find_by_handle(hProcess);
394 struct module* module;
396 if (!pcs) return FALSE;
398 for (module = pcs->lmodules; module; module = module->next)
400 if (!(dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES) && module->type != DMT_PE)
401 continue;
402 if (!EnumModulesCallback(module->module.ModuleName,
403 module->module.BaseOfImage, UserContext))
404 break;
406 return TRUE;
409 /******************************************************************
410 * EnumerateLoadedModules (DBGHELP.@)
413 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
414 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
415 PVOID UserContext)
417 HMODULE* hMods;
418 char base[256], mod[256];
419 DWORD i, sz;
420 MODULEINFO mi;
422 hMods = HeapAlloc(GetProcessHeap(), 0, sz);
423 if (!hMods) return FALSE;
425 if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
427 /* hProcess should also be a valid process handle !! */
428 FIXME("If this happens, bump the number in mod\n");
429 HeapFree(GetProcessHeap(), 0, hMods);
430 return FALSE;
432 sz /= sizeof(HMODULE);
433 for (i = 0; i < sz; i++)
435 if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
436 !GetModuleBaseNameA(hProcess, hMods[i], base, sizeof(base)))
437 continue;
438 module_fill_module(base, mod, sizeof(mod));
440 EnumLoadedModulesCallback(mod, (DWORD)mi.lpBaseOfDll, mi.SizeOfImage,
441 UserContext);
443 HeapFree(GetProcessHeap(), 0, hMods);
445 return sz != 0 && i == sz;
448 /******************************************************************
449 * SymGetModuleInfo (DBGHELP.@)
452 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
453 PIMAGEHLP_MODULE ModuleInfo)
455 struct process* pcs = process_find_by_handle(hProcess);
456 struct module* module;
458 if (!pcs) return FALSE;
459 if (ModuleInfo->SizeOfStruct < sizeof(*ModuleInfo)) return FALSE;
460 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
461 if (!module) return FALSE;
463 *ModuleInfo = module->module;
464 if (module->module.SymType <= SymNone)
466 module = module_get_container(pcs, module);
467 if (module && module->module.SymType > SymNone)
468 ModuleInfo->SymType = module->module.SymType;
471 return TRUE;
474 /***********************************************************************
475 * SymGetModuleBase (IMAGEHLP.@)
477 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
479 struct process* pcs = process_find_by_handle(hProcess);
480 struct module* module;
482 if (!pcs) return 0;
483 module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
484 if (!module) return 0;
485 return module->module.BaseOfImage;
488 /******************************************************************
489 * module_reset_debug_info
490 * Removes any debug information linked to a given module.
492 void module_reset_debug_info(struct module* module)
494 module->sortlist_valid = TRUE;
495 module->addr_sorttab = NULL;
496 hash_table_destroy(&module->ht_symbols);
497 module->ht_symbols.num_buckets = 0;
498 module->ht_symbols.buckets = NULL;
499 hash_table_destroy(&module->ht_types);
500 module->ht_types.num_buckets = 0;
501 module->ht_types.buckets = NULL;
502 hash_table_destroy(&module->ht_symbols);
503 module->sources_used = module->sources_alloc = 0;
504 module->sources = NULL;