- Fix warnings
[reactos.git] / reactos / lib / dbghelp / module.c
1 /*
2 * File module.c - module handling for the wine debugger
3 *
4 * Copyright (C) 1993, Eric Youngdale.
5 * 2000-2004, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <ctype.h>
26 #include <string.h>
27 #include <assert.h>
28
29 #include "dbghelp_private.h"
30 #include "psapi.h"
31 #include "winreg.h"
32 #include "winternl.h"
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
36
37 static void module_fill_module(const char* in, char* out, unsigned size)
38 {
39 const char* ptr;
40 unsigned len;
41
42 for (ptr = in + strlen(in) - 1;
43 *ptr != '/' && *ptr != '\\' && ptr >= in;
44 ptr--);
45 if (ptr < in || *ptr == '/' || *ptr == '\\') ptr++;
46 strncpy(out, ptr, size);
47 out[size - 1] = '\0';
48 len = strlen(out);
49 if (len > 4 &&
50 (!strcasecmp(&out[len - 4], ".dll") || !strcasecmp(&out[len - 4], ".exe")))
51 out[len - 4] = '\0';
52 else
53 {
54 if (len > 7 &&
55 (!strcasecmp(&out[len - 7], ".dll.so") || !strcasecmp(&out[len - 7], ".exe.so")))
56 strcpy(&out[len - 7], "<elf>");
57 else if (len > 7 &&
58 out[len - 7] == '.' && !strcasecmp(&out[len - 3], ".so"))
59 {
60 if (len + 3 < size) strcpy(&out[len - 3], "<elf>");
61 else WARN("Buffer too short: %s\n", out);
62 }
63 }
64 while ((*out = tolower(*out))) out++;
65 }
66
67 /***********************************************************************
68 * Creates and links a new module to a process
69 */
70 struct module* module_new(struct process* pcs, const char* name,
71 enum module_type type,
72 unsigned long mod_addr, unsigned long size,
73 unsigned long stamp, unsigned long checksum)
74 {
75 struct module* module;
76
77 if (!(module = HeapAlloc(GetProcessHeap(), 0, sizeof(*module))))
78 return NULL;
79
80 memset(module, 0, sizeof(*module));
81
82 module->next = pcs->lmodules;
83 pcs->lmodules = module;
84
85 TRACE("=> %s %08lx-%08lx %s\n",
86 type == DMT_ELF ? "ELF" : (type == DMT_PE ? "PE" : "---"),
87 mod_addr, mod_addr + size, name);
88
89 pool_init(&module->pool, 65536);
90
91 module->module.SizeOfStruct = sizeof(module->module);
92 module->module.BaseOfImage = mod_addr;
93 module->module.ImageSize = size;
94 module_fill_module(name, module->module.ModuleName, sizeof(module->module.ModuleName));
95 module->module.ImageName[0] = '\0';
96 strncpy(module->module.LoadedImageName, name,
97 sizeof(module->module.LoadedImageName));
98 module->module.LoadedImageName[sizeof(module->module.LoadedImageName) - 1] = '\0';
99 module->module.SymType = SymNone;
100 module->module.NumSyms = 0;
101 module->module.TimeDateStamp = stamp;
102 module->module.CheckSum = checksum;
103
104 module->type = type;
105 module->sortlist_valid = FALSE;
106 module->addr_sorttab = NULL;
107 /* FIXME: this seems a bit too high (on a per module basis)
108 * need some statistics about this
109 */
110 hash_table_init(&module->pool, &module->ht_symbols, 4096);
111 hash_table_init(&module->pool, &module->ht_types, 4096);
112 vector_init(&module->vtypes, sizeof(struct symt*), 32);
113
114 module->sources_used = 0;
115 module->sources_alloc = 0;
116 module->sources = 0;
117
118 return module;
119 }
120
121 /***********************************************************************
122 * module_find_by_name
123 *
124 */
125 struct module* module_find_by_name(const struct process* pcs,
126 const char* name, enum module_type type)
127 {
128 struct module* module;
129
130 if (type == DMT_UNKNOWN)
131 {
132 if ((module = module_find_by_name(pcs, name, DMT_PE)) ||
133 (module = module_find_by_name(pcs, name, DMT_ELF)))
134 return module;
135 }
136 else
137 {
138 for (module = pcs->lmodules; module; module = module->next)
139 {
140 if (type == module->type && !strcasecmp(name, module->module.LoadedImageName))
141 return module;
142 }
143 for (module = pcs->lmodules; module; module = module->next)
144 {
145 if (type == module->type && !strcasecmp(name, module->module.ModuleName))
146 return module;
147 }
148 }
149 SetLastError(ERROR_INVALID_NAME);
150 return NULL;
151 }
152
153 /***********************************************************************
154 * module_get_container
155 *
156 */
157 struct module* module_get_container(const struct process* pcs,
158 const struct module* inner)
159 {
160 struct module* module;
161
162 for (module = pcs->lmodules; module; module = module->next)
163 {
164 if (module != inner &&
165 module->module.BaseOfImage <= inner->module.BaseOfImage &&
166 module->module.BaseOfImage + module->module.ImageSize >=
167 inner->module.BaseOfImage + inner->module.ImageSize)
168 return module;
169 }
170 return NULL;
171 }
172
173 /***********************************************************************
174 * module_get_containee
175 *
176 */
177 struct module* module_get_containee(const struct process* pcs,
178 const struct module* outter)
179 {
180 struct module* module;
181
182 for (module = pcs->lmodules; module; module = module->next)
183 {
184 if (module != outter &&
185 outter->module.BaseOfImage <= module->module.BaseOfImage &&
186 outter->module.BaseOfImage + outter->module.ImageSize >=
187 module->module.BaseOfImage + module->module.ImageSize)
188 return module;
189 }
190 return NULL;
191 }
192
193 /******************************************************************
194 * module_get_debug
195 *
196 * get the debug information from a module:
197 * - if the module's type is deferred, then force loading of debug info (and return
198 * the module itself)
199 * - if the module has no debug info and has an ELF container, then return the ELF
200 * container (and also force the ELF container's debug info loading if deferred)
201 * - otherwise return the module itself if it has some debug info
202 */
203 struct module* module_get_debug(const struct process* pcs, struct module* module)
204 {
205 struct module* parent;
206
207 if (!module) return NULL;
208 /* for a PE builtin, always get info from parent */
209 if ((parent = module_get_container(pcs, module)))
210 module = parent;
211 /* if deferred, force loading */
212 if (module->module.SymType == SymDeferred)
213 {
214 BOOL ret;
215
216 switch (module->type)
217 {
218 case DMT_ELF: ret = elf_load_debug_info(module); break;
219 case DMT_PE: ret = pe_load_debug_info(pcs, module); break;
220 default: ret = FALSE; break;
221 }
222 if (!ret) module->module.SymType = SymNone;
223 assert(module->module.SymType != SymDeferred);
224 }
225 return (module && module->module.SymType != SymNone) ? module : NULL;
226 }
227
228 /***********************************************************************
229 * module_find_by_addr
230 *
231 * either the addr where module is loaded, or any address inside the
232 * module
233 */
234 struct module* module_find_by_addr(const struct process* pcs, unsigned long addr,
235 enum module_type type)
236 {
237 struct module* module;
238
239 if (type == DMT_UNKNOWN)
240 {
241 if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
242 (module = module_find_by_addr(pcs, addr, DMT_ELF)))
243 return module;
244 }
245 else
246 {
247 for (module = pcs->lmodules; module; module = module->next)
248 {
249 if (type == module->type && addr >= module->module.BaseOfImage &&
250 addr < module->module.BaseOfImage + module->module.ImageSize)
251 return module;
252 }
253 }
254 SetLastError(ERROR_INVALID_ADDRESS);
255 return module;
256 }
257
258 static BOOL module_is_elf_container_loaded(struct process* pcs, const char* ImageName,
259 const char* ModuleName)
260 {
261 char buffer[MAX_PATH];
262 size_t len;
263 struct module* module;
264
265 if (!ModuleName)
266 {
267 module_fill_module(ImageName, buffer, sizeof(buffer));
268 ModuleName = buffer;
269 }
270 len = strlen(ModuleName);
271 for (module = pcs->lmodules; module; module = module->next)
272 {
273 if (!strncasecmp(module->module.ModuleName, ModuleName, len) &&
274 module->type == DMT_ELF &&
275 !strcmp(module->module.ModuleName + len, "<elf>"))
276 return TRUE;
277 }
278 return FALSE;
279 }
280
281 /***********************************************************************
282 * SymLoadModule (DBGHELP.@)
283 */
284 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, char* ImageName,
285 char* ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
286 {
287 struct process* pcs;
288 struct module* module = NULL;
289
290 TRACE("(%p %p %s %s %08lx %08lx)\n",
291 hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName),
292 BaseOfDll, SizeOfDll);
293
294 pcs = process_find_by_handle(hProcess);
295 if (!pcs) return FALSE;
296
297 /* force transparent ELF loading / unloading */
298 elf_synchronize_module_list(pcs);
299
300 /* this is a Wine extension to the API just to redo the synchronisation */
301 if (!ImageName && !hFile) return 0;
302
303 if (module_is_elf_container_loaded(pcs, ImageName, ModuleName))
304 {
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;
310 }
311 TRACE("Assuming %s as native DLL\n", ImageName);
312 if (!(module = pe_load_module(pcs, ImageName, hFile, BaseOfDll, SizeOfDll)))
313 {
314 unsigned len = strlen(ImageName);
315
316 if (!strcmp(ImageName + len - 3, ".so") &&
317 (module = elf_load_module(pcs, ImageName))) goto done;
318 FIXME("should have successfully loaded some debug information for image %s\n", ImageName);
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;
323 }
324
325 done:
326 /* by default pe_load_module fills module.ModuleName from a derivation
327 * of ImageName. Overwrite it, if we have better information
328 */
329 if (ModuleName)
330 {
331 strncpy(module->module.ModuleName, ModuleName,
332 sizeof(module->module.ModuleName));
333 module->module.ModuleName[sizeof(module->module.ModuleName) - 1] = '\0';
334 }
335 strncpy(module->module.ImageName, ImageName, sizeof(module->module.ImageName));
336 module->module.ImageName[sizeof(module->module.ImageName) - 1] = '\0';
337
338 return module->module.BaseOfImage;
339 }
340
341 /******************************************************************
342 * module_remove
343 *
344 */
345 BOOL module_remove(struct process* pcs, struct module* module)
346 {
347 struct module** p;
348
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);
355
356 for (p = &pcs->lmodules; *p; p = &(*p)->next)
357 {
358 if (*p == module)
359 {
360 *p = module->next;
361 HeapFree(GetProcessHeap(), 0, module);
362 return TRUE;
363 }
364 }
365 FIXME("This shouldn't happen\n");
366 return FALSE;
367 }
368
369 /******************************************************************
370 * SymUnloadModule (DBGHELP.@)
371 *
372 */
373 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
374 {
375 struct process* pcs;
376 struct module* module;
377
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);
383 }
384
385 /******************************************************************
386 * SymEnumerateModules (DBGHELP.@)
387 *
388 */
389 BOOL WINAPI SymEnumerateModules(HANDLE hProcess,
390 PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,
391 PVOID UserContext)
392 {
393 struct process* pcs = process_find_by_handle(hProcess);
394 struct module* module;
395
396 if (!pcs) return FALSE;
397
398 for (module = pcs->lmodules; module; module = module->next)
399 {
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;
405 }
406 return TRUE;
407 }
408
409 /******************************************************************
410 * EnumerateLoadedModules (DBGHELP.@)
411 *
412 */
413 BOOL WINAPI EnumerateLoadedModules(HANDLE hProcess,
414 PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
415 PVOID UserContext)
416 {
417 HMODULE* hMods;
418 char base[256], mod[256];
419 DWORD i, sz;
420 MODULEINFO mi;
421
422 hMods = HeapAlloc(GetProcessHeap(), 0, sz);
423 if (!hMods) return FALSE;
424
425 if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
426 {
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;
431 }
432 sz /= sizeof(HMODULE);
433 for (i = 0; i < sz; i++)
434 {
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));
439
440 EnumLoadedModulesCallback(mod, (DWORD)mi.lpBaseOfDll, mi.SizeOfImage,
441 UserContext);
442 }
443 HeapFree(GetProcessHeap(), 0, hMods);
444
445 return sz != 0 && i == sz;
446 }
447
448 /******************************************************************
449 * SymGetModuleInfo (DBGHELP.@)
450 *
451 */
452 BOOL WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr,
453 PIMAGEHLP_MODULE ModuleInfo)
454 {
455 struct process* pcs = process_find_by_handle(hProcess);
456 struct module* module;
457
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;
462
463 *ModuleInfo = module->module;
464 if (module->module.SymType == SymNone)
465 {
466 module = module_get_container(pcs, module);
467 if (module && module->module.SymType != SymNone)
468 ModuleInfo->SymType = module->module.SymType;
469 }
470
471 return TRUE;
472 }
473
474 /***********************************************************************
475 * SymGetModuleBase (IMAGEHLP.@)
476 */
477 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
478 {
479 struct process* pcs = process_find_by_handle(hProcess);
480 struct module* module;
481
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;
486 }
487
488 /******************************************************************
489 * module_reset_debug_info
490 * Removes any debug information linked to a given module.
491 */
492 void module_reset_debug_info(struct module* module)
493 {
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 module->vtypes.num_elts = 0;
503 hash_table_destroy(&module->ht_symbols);
504 module->sources_used = module->sources_alloc = 0;
505 module->sources = NULL;
506 }