Add cyrillic glyphs to Wine System.
[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     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
52     {
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"))
58         {
59             if (len + 3 < size) strcpy(&out[len - 3], "<elf>");
60             else WARN("Buffer too short: %s\n", out);
61         }
62     }
63     while ((*out = tolower(*out))) out++;
64 }
65
66 /***********************************************************************
67  * Creates and links a new module to a process 
68  */
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) 
73 {
74     struct module*      module;
75
76     if (!(module = HeapAlloc(GetProcessHeap(), 0, sizeof(*module))))
77         return NULL;
78
79     memset(module, 0, sizeof(*module));
80
81     module->next = pcs->lmodules;
82     pcs->lmodules = module;
83
84     TRACE("=> %s %08lx-%08lx %s\n", 
85           type == DMT_ELF ? "ELF" : (type == DMT_PE ? "PE" : "---"),
86           mod_addr, mod_addr + size, name);
87
88     pool_init(&module->pool, 65536);
89     
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;
102
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
108      */
109     hash_table_init(&module->pool, &module->ht_symbols, 4096);
110     hash_table_init(&module->pool, &module->ht_types,   4096);
111
112     module->sources_used      = 0;
113     module->sources_alloc     = 0;
114     module->sources           = 0;
115
116     return module;
117 }
118
119 /***********************************************************************
120  *      module_find_by_name
121  *
122  */
123 struct module* module_find_by_name(const struct process* pcs, 
124                                    const char* name, enum module_type type)
125 {
126     struct module*      module;
127
128     if (type == DMT_UNKNOWN)
129     {
130         if ((module = module_find_by_name(pcs, name, DMT_PE)) ||
131             (module = module_find_by_name(pcs, name, DMT_ELF)))
132             return module;
133     }
134     else
135     {
136         for (module = pcs->lmodules; module; module = module->next)
137         {
138             if (type == module->type && !strcasecmp(name, module->module.LoadedImageName)) 
139                 return module;
140         }
141         for (module = pcs->lmodules; module; module = module->next)
142         {
143             if (type == module->type && !strcasecmp(name, module->module.ModuleName)) 
144                 return module;
145         }
146     }
147     SetLastError(ERROR_INVALID_NAME);
148     return NULL;
149 }
150
151 /***********************************************************************
152  *           module_get_container
153  *
154  */
155 struct module* module_get_container(const struct process* pcs, 
156                                     const struct module* inner)
157 {
158     struct module*      module;
159      
160     for (module = pcs->lmodules; module; module = module->next)
161     {
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;
167     }
168     return NULL;
169 }
170
171 /***********************************************************************
172  *           module_get_containee
173  *
174  */
175 struct module* module_get_containee(const struct process* pcs, 
176                                     const struct module* outter)
177 {
178     struct module*      module;
179      
180     for (module = pcs->lmodules; module; module = module->next)
181     {
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;
187     }
188     return NULL;
189 }
190
191 /******************************************************************
192  *              module_get_debug
193  *
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
200  */
201 struct module* module_get_debug(const struct process* pcs, struct module* module)
202 {
203     BOOL        ret;
204
205     if (!module) return NULL;
206     switch (module->module.SymType)
207     {
208     case SymNone:
209         module = module_get_container(pcs, module);
210         if (!module || module->module.SymType != SymDeferred) break;
211         /* fall through */
212     case SymDeferred:
213         switch (module->type)
214         {
215         case DMT_ELF: ret = elf_load_debug_info(module);     break;
216         case DMT_PE:  ret = pe_load_debug_info(pcs, module); break;
217         default:      ret = FALSE;                           break;
218         }
219         if (!ret) module->module.SymType = SymNone;
220         break;
221     default: break;
222     }
223     return (module && module->module.SymType != SymNone) ? module : NULL;
224 }
225
226 /***********************************************************************
227  *      module_find_by_addr
228  *
229  * either the addr where module is loaded, or any address inside the 
230  * module
231  */
232 struct module* module_find_by_addr(const struct process* pcs, unsigned long addr, 
233                                    enum module_type type)
234 {
235     struct module*      module;
236     
237     if (type == DMT_UNKNOWN)
238     {
239         if ((module = module_find_by_addr(pcs, addr, DMT_PE)) ||
240             (module = module_find_by_addr(pcs, addr, DMT_ELF)))
241             return module;
242     }
243     else
244     {
245         for (module = pcs->lmodules; module; module = module->next)
246         {
247             if (type == module->type && addr >= module->module.BaseOfImage &&
248                 addr < module->module.BaseOfImage + module->module.ImageSize) 
249                 return module;
250         }
251     }
252     SetLastError(ERROR_INVALID_ADDRESS);
253     return module;
254 }
255
256 static BOOL module_is_elf_container_loaded(struct process* pcs, const char* ImageName, 
257                                            const char* ModuleName)
258 {
259     char                buffer[MAX_PATH];
260     size_t              len;
261     struct module*      module;
262
263     if (!ModuleName)
264     {
265         module_fill_module(ImageName, buffer, sizeof(buffer));
266         ModuleName = buffer;
267     }
268     len = strlen(ModuleName);
269     for (module = pcs->lmodules; module; module = module->next)
270     {
271         if (!strncasecmp(module->module.ModuleName, ModuleName, len) &&
272             module->type == DMT_ELF &&
273             !strcmp(module->module.ModuleName + len, "<elf>"))
274             return TRUE;
275     }
276     return FALSE;
277 }
278
279 /***********************************************************************
280  *                      SymLoadModule (DBGHELP.@)
281  */
282 DWORD WINAPI SymLoadModule(HANDLE hProcess, HANDLE hFile, char* ImageName,
283                            char* ModuleName, DWORD BaseOfDll, DWORD SizeOfDll)
284 {
285     struct process*     pcs;
286     struct module*      module = NULL;
287
288     TRACE("(%p %p %s %s %08lx %08lx)\n", 
289           hProcess, hFile, debugstr_a(ImageName), debugstr_a(ModuleName), 
290           BaseOfDll, SizeOfDll);
291
292     pcs = process_find_by_handle(hProcess);
293     if (!pcs) return FALSE;
294
295     /* force transparent ELF loading / unloading */
296     elf_synchronize_module_list(pcs);
297
298     /* this is a Wine extension to the API just to redo the synchronisation */
299     if (!ImageName && !hFile) return 0;
300
301     if (module_is_elf_container_loaded(pcs, ImageName, ModuleName))
302     {
303         /* force the loading of DLL as builtin */
304         if ((module = pe_load_module_from_pcs(pcs, ImageName, ModuleName, BaseOfDll, SizeOfDll)))
305             goto done;
306         WARN("Couldn't locate %s\n", ImageName);
307         return 0;
308     }
309     TRACE("Assuming %s as native DLL\n", ImageName);
310     if (!(module = pe_load_module(pcs, ImageName, hFile, BaseOfDll, SizeOfDll)))
311     {
312         unsigned        len = strlen(ImageName);
313
314         if (!strcmp(ImageName + len - 3, ".so") &&
315             (module = elf_load_module(pcs, ImageName))) goto done;
316         FIXME("should no longer happen\n");
317         if ((module = pe_load_module_from_pcs(pcs, ImageName, ModuleName, BaseOfDll, SizeOfDll)))
318             goto done;
319         WARN("Couldn't locate %s\n", ImageName);
320         return 0;
321     }
322
323 done:
324     /* by default pe_load_module fills module.ModuleName from a derivation 
325      * of ImageName. Overwrite it, if we have better information
326      */
327     if (ModuleName)
328     {
329         strncpy(module->module.ModuleName, ModuleName, 
330                 sizeof(module->module.ModuleName));
331         module->module.ModuleName[sizeof(module->module.ModuleName) - 1] = '\0';
332     }
333     strncpy(module->module.ImageName, ImageName, sizeof(module->module.ImageName));
334     module->module.ImageName[sizeof(module->module.ImageName) - 1] = '\0';
335
336     return module->module.BaseOfImage;
337 }
338
339 /******************************************************************
340  *              module_remove
341  *
342  */
343 BOOL module_remove(struct process* pcs, struct module* module)
344 {
345     struct module**     p;
346
347     TRACE("%s (%p)\n", module->module.ModuleName, module);
348     hash_table_destroy(&module->ht_symbols);
349     hash_table_destroy(&module->ht_types);
350     HeapFree(GetProcessHeap(), 0, (char*)module->sources);
351     HeapFree(GetProcessHeap(), 0, module->addr_sorttab);
352     pool_destroy(&module->pool);
353
354     for (p = &pcs->lmodules; *p; p = &(*p)->next)
355     {
356         if (*p == module)
357         {
358             *p = module->next;
359             HeapFree(GetProcessHeap(), 0, module);
360             return TRUE;
361         }
362     }
363     FIXME("This shouldn't happen\n");
364     return FALSE;
365 }
366
367 /******************************************************************
368  *              SymUnloadModule (DBGHELP.@)
369  *
370  */
371 BOOL WINAPI SymUnloadModule(HANDLE hProcess, DWORD BaseOfDll)
372 {
373     struct process*     pcs;
374     struct module*      module;
375
376     pcs = process_find_by_handle(hProcess);
377     if (!pcs) return FALSE;
378     module = module_find_by_addr(pcs, BaseOfDll, DMT_UNKNOWN);
379     if (!module) return FALSE;
380     return module_remove(pcs, module);
381 }
382
383 /******************************************************************
384  *              SymEnumerateModules (DBGHELP.@)
385  *
386  */
387 BOOL  WINAPI SymEnumerateModules(HANDLE hProcess,
388                                  PSYM_ENUMMODULES_CALLBACK EnumModulesCallback,  
389                                  PVOID UserContext)
390 {
391     struct process*     pcs = process_find_by_handle(hProcess);
392     struct module*      module;
393
394     if (!pcs) return FALSE;
395     
396     for (module = pcs->lmodules; module; module = module->next)
397     {
398         if (!(dbghelp_options & SYMOPT_WINE_WITH_ELF_MODULES) && module->type != DMT_PE)
399             continue;
400         if (!EnumModulesCallback(module->module.ModuleName, 
401                                  module->module.BaseOfImage, UserContext))
402             break;
403     }
404     return TRUE;
405 }
406
407 /******************************************************************
408  *              EnumerateLoadedModules (DBGHELP.@)
409  *
410  */
411 BOOL  WINAPI EnumerateLoadedModules(HANDLE hProcess,
412                                     PENUMLOADED_MODULES_CALLBACK EnumLoadedModulesCallback,
413                                     PVOID UserContext)
414 {
415     HMODULE*    hMods;
416     char        base[256], mod[256];
417     DWORD       i, sz;
418     MODULEINFO  mi;
419
420     hMods = HeapAlloc(GetProcessHeap(), 0, sz);
421     if (!hMods) return FALSE;
422
423     if (!EnumProcessModules(hProcess, hMods, 256 * sizeof(hMods[0]), &sz))
424     {
425         /* hProcess should also be a valid process handle !! */
426         FIXME("If this happens, bump the number in mod\n");
427         HeapFree(GetProcessHeap(), 0, hMods);
428         return FALSE;
429     }
430     sz /= sizeof(HMODULE);
431     for (i = 0; i < sz; i++)
432     {
433         if (!GetModuleInformation(hProcess, hMods[i], &mi, sizeof(mi)) ||
434             !GetModuleBaseNameA(hProcess, hMods[i], base, sizeof(base)))
435             continue;
436         module_fill_module(base, mod, sizeof(mod));
437
438         EnumLoadedModulesCallback(mod, (DWORD)mi.lpBaseOfDll, mi.SizeOfImage, 
439                                   UserContext);
440     }
441     HeapFree(GetProcessHeap(), 0, hMods);
442
443     return sz != 0 && i == sz;
444 }
445
446 /******************************************************************
447  *              SymGetModuleInfo (DBGHELP.@)
448  *
449  */
450 BOOL  WINAPI SymGetModuleInfo(HANDLE hProcess, DWORD dwAddr, 
451                               PIMAGEHLP_MODULE ModuleInfo)
452 {
453     struct process*     pcs = process_find_by_handle(hProcess);
454     struct module*      module;
455
456     if (!pcs) return FALSE;
457     if (ModuleInfo->SizeOfStruct < sizeof(*ModuleInfo)) return FALSE;
458     module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
459     if (!module) return FALSE;
460
461     *ModuleInfo = module->module;
462     if (module->module.SymType == SymNone)
463     {
464         module = module_get_container(pcs, module);
465         if (module && module->module.SymType != SymNone)
466             ModuleInfo->SymType = module->module.SymType;
467     }
468
469     return TRUE;
470 }
471
472 /***********************************************************************
473  *              SymGetModuleBase (IMAGEHLP.@)
474  */
475 DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
476 {
477     struct process*     pcs = process_find_by_handle(hProcess);
478     struct module*      module;
479
480     if (!pcs) return 0;
481     module = module_find_by_addr(pcs, dwAddr, DMT_UNKNOWN);
482     if (!module) return 0;
483     return module->module.BaseOfImage;
484 }
485
486 /******************************************************************
487  *              module_reset_debug_info
488  * Removes any debug information linked to a given module.
489  */
490 void module_reset_debug_info(struct module* module)
491 {
492     module->sortlist_valid = TRUE;
493     module->addr_sorttab = NULL;
494     hash_table_destroy(&module->ht_symbols);
495     module->ht_symbols.num_buckets = 0;
496     module->ht_symbols.buckets = NULL;
497     hash_table_destroy(&module->ht_types);
498     module->ht_types.num_buckets = 0;
499     module->ht_types.buckets = NULL;
500     hash_table_destroy(&module->ht_symbols);
501     module->sources_used = module->sources_alloc = 0;
502     module->sources = NULL;
503 }