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