Fixed some quirks (range is 32 bit, page up/dn mismatch on HZ trackbars).
[wine] / debugger / module.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /*
3  * File module.c - module handling for the wine debugger
4  *
5  * Copyright (C) 1993, Eric Youngdale.
6  *               2000, Eric Pouech
7  */
8 #include "config.h"
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include "neexe.h"
13 #include "module.h"
14 #include "file.h"
15 #include "debugger.h"
16 #include "toolhelp.h"
17 #include "wingdi.h"
18 #include "winuser.h"
19
20 /***********************************************************************
21  * Creates and links a new module to the current process 
22  *
23  */
24 DBG_MODULE*     DEBUG_AddModule(const char* name, int type, 
25                                 void* mod_addr, HMODULE hmodule)
26 {
27     DBG_MODULE* wmod;
28
29     if (!(wmod = (DBG_MODULE*)DBG_alloc(sizeof(*wmod))))
30         return NULL;
31
32     memset(wmod, 0, sizeof(*wmod));
33
34     wmod->next = DEBUG_CurrProcess->modules;
35     wmod->status = DM_STATUS_NEW;
36     wmod->type = type;
37     wmod->load_addr = mod_addr;
38     wmod->handle = hmodule;
39     wmod->dbg_index = DEBUG_CurrProcess->next_index;
40     wmod->module_name = DBG_strdup(name);
41     DEBUG_CurrProcess->modules = wmod;
42
43     return wmod;
44 }
45
46 /***********************************************************************
47  *      DEBUG_FindModuleByName
48  *
49  */
50 DBG_MODULE*     DEBUG_FindModuleByName(const char* name, int type)
51 {
52     DBG_MODULE* wmod;
53     
54     for (wmod = DEBUG_CurrProcess->modules; wmod; wmod = wmod->next) {
55         if ((type == DM_TYPE_UNKNOWN || type == wmod->type) &&
56             !strcasecmp(name, wmod->module_name)) break;
57     }
58     return wmod;
59 }
60
61 /***********************************************************************
62  *      DEBUG_FindModuleByAddr
63  *
64  * either the addr where module is loaded, or any address inside the 
65  * module
66  */
67 DBG_MODULE*     DEBUG_FindModuleByAddr(void* addr, int type)
68 {
69     DBG_MODULE* wmod;
70     DBG_MODULE* res = NULL;
71     
72     for (wmod = DEBUG_CurrProcess->modules; wmod; wmod = wmod->next) {
73         if ((type == DM_TYPE_UNKNOWN || type == wmod->type) &&
74             (u_long)addr >= (u_long)wmod->load_addr &&
75             (!res || res->load_addr < wmod->load_addr))
76             res = wmod;
77     }
78     return res;
79 }
80
81 /***********************************************************************
82  *              DEBUG_FindModuleByHandle
83  */
84 DBG_MODULE*     DEBUG_FindModuleByHandle(HANDLE handle, int type)
85 {
86     DBG_MODULE* wmod;
87     
88     for (wmod = DEBUG_CurrProcess->modules; wmod; wmod = wmod->next) {
89         if ((type == DM_TYPE_UNKNOWN || type == wmod->type) && handle == wmod->handle) break;
90     }
91     return wmod;
92 }
93
94 /***********************************************************************
95  *                      DEBUG_RegisterELFModule
96  *
97  * ELF modules are also entered into the list - this is so that we
98  * can make 'info shared' types of displays possible.
99  */
100 DBG_MODULE* DEBUG_RegisterELFModule(u_long load_addr, const char* name)
101 {
102     DBG_MODULE* wmod = DEBUG_AddModule(name, DM_TYPE_ELF, (void*)load_addr, 0);
103
104     if (!wmod) return NULL;
105
106     wmod->status = DM_STATUS_LOADED;
107     DEBUG_CurrProcess->next_index++;
108
109     return wmod;
110 }
111
112 /***********************************************************************
113  *                      DEBUG_RegisterPEModule
114  *
115  */
116 DBG_MODULE* DEBUG_RegisterPEModule(HMODULE hModule, u_long load_addr, const char *module_name)
117 {
118     DBG_MODULE* wmod = DEBUG_AddModule(module_name, DM_TYPE_PE, (void*)load_addr, hModule);
119
120     if (!wmod) return NULL;
121
122     DEBUG_CurrProcess->next_index++;
123
124     return wmod;
125 }
126
127 /***********************************************************************
128  *                      DEBUG_RegisterNEModule
129  *
130  */
131 DBG_MODULE* DEBUG_RegisterNEModule(HMODULE hModule, void* load_addr, const char *module_name)
132 {
133     DBG_MODULE* wmod = DEBUG_AddModule(module_name, DM_TYPE_NE, load_addr, hModule);
134
135     if (!wmod) return NULL;
136
137     wmod->status = DM_STATUS_LOADED;
138     DEBUG_CurrProcess->next_index++;
139     return wmod;
140 }
141
142 /***********************************************************************
143  *           DEBUG_GetEP16
144  *
145  * Helper function fo DEBUG_LoadModuleEPs16:
146  *      finds the address of a given entry point from a given module
147  */
148 static BOOL DEBUG_GetEP16(char* moduleAddr, const NE_MODULE* module, 
149                           WORD ordinal, DBG_ADDR* addr)
150 {
151     void*               idx;
152     ET_ENTRY            entry;
153     ET_BUNDLE           bundle;
154     SEGTABLEENTRY       ste;
155
156     bundle.next = module->entry_table;
157     do {
158         if (!bundle.next)
159             return FALSE;
160         idx = moduleAddr + bundle.next;
161         if (!DEBUG_READ_MEM_VERBOSE(idx, &bundle, sizeof(bundle)))
162             return FALSE;
163     } while ((ordinal < bundle.first + 1) || (ordinal > bundle.last));
164     
165     if (!DEBUG_READ_MEM_VERBOSE((char*)idx + sizeof(ET_BUNDLE) + 
166                                 (ordinal - bundle.first - 1) * sizeof(ET_ENTRY), 
167                                 &entry, sizeof(ET_ENTRY)))
168         return FALSE;
169     
170     addr->seg = entry.segnum;
171     addr->off = entry.offs;
172     
173     if (addr->seg == 0xfe) addr->seg = 0xffff;  /* constant entry */
174     else {
175         if (!DEBUG_READ_MEM_VERBOSE(moduleAddr + module->seg_table + 
176                                     sizeof(ste) * (addr->seg - 1),
177                                     &ste, sizeof(ste)))
178             return FALSE;
179         addr->seg = GlobalHandleToSel16(ste.hSeg);
180     }
181     return TRUE;
182 }
183
184 /***********************************************************************
185  *           DEBUG_LoadModule16
186  *
187  * Load the entry points of a Win16 module into the hash table.
188  */
189 static void DEBUG_LoadModule16(HMODULE hModule, NE_MODULE* module, char* moduleAddr, const char* name)
190 {
191     DBG_VALUE   value;
192     BYTE        buf[1 + 256 + 2];
193     char        epname[512];
194     char*       cpnt;
195     DBG_MODULE* wmod;
196
197     wmod = DEBUG_RegisterNEModule(hModule, moduleAddr, name);
198
199     value.type = NULL;
200     value.cookie = DV_TARGET;
201     value.addr.seg = 0;
202     value.addr.off = 0;
203     
204     cpnt = moduleAddr + module->name_table;
205     
206     /* First search the resident names */
207     
208     /* skip module name */
209     if (!DEBUG_READ_MEM_VERBOSE(cpnt, buf, sizeof(buf)) || !buf[0])
210         return;
211     cpnt += 1 + buf[0] + sizeof(WORD);
212     
213     while (DEBUG_READ_MEM_VERBOSE(cpnt, buf, sizeof(buf)) && buf[0]) {
214         sprintf(epname, "%s.%.*s", name, buf[0], &buf[1]);
215         if (DEBUG_GetEP16(moduleAddr, module, *(WORD*)&buf[1 + buf[0]], &value.addr)) {
216             DEBUG_AddSymbol(epname, &value, NULL, SYM_WIN32 | SYM_FUNC);
217         }
218         cpnt += buf[0] + 1 + sizeof(WORD);
219     }
220     
221     /* Now search the non-resident names table */
222     if (!module->nrname_handle) return;  /* No non-resident table */
223     cpnt = (char *)GlobalLock16(module->nrname_handle);
224     while (DEBUG_READ_MEM_VERBOSE(cpnt, buf, sizeof(buf)) && buf[0]) {
225         sprintf(epname, "%s.%.*s", name, buf[0], &buf[1]);
226         if (DEBUG_GetEP16(moduleAddr, module, *(WORD*)&buf[1 + buf[0]], &value.addr)) {
227             DEBUG_AddSymbol(epname, &value, NULL, SYM_WIN32 | SYM_FUNC);
228         }
229         cpnt += buf[0] + 1 + sizeof(WORD);
230     }
231     GlobalUnlock16(module->nrname_handle);
232 }
233
234 /***********************************************************************
235  *                      DEBUG_LoadModule32
236  */
237 void    DEBUG_LoadModule32(const char* name, HANDLE hFile, DWORD base)
238 {
239     DBG_VALUE                   value;
240     char                        buffer[512];
241     char                        bufstr[256];
242     int                         i;
243     IMAGE_NT_HEADERS            pe_header;
244     DWORD                       pe_header_ofs;
245     IMAGE_SECTION_HEADER        pe_seg;
246     DWORD                       pe_seg_ofs;
247     IMAGE_DATA_DIRECTORY        dir;
248     DWORD                       dir_ofs;
249     DBG_MODULE*                 wmod;
250
251     /* FIXME: we make the assumption that hModule == base */
252     wmod = DEBUG_RegisterPEModule((HMODULE)base, base, name);
253
254     DEBUG_Printf(DBG_CHN_TRACE, "Registring 32bit DLL '%s' at %08lx\n", name, base);
255     
256     value.type = NULL;
257     value.cookie = DV_TARGET;
258     value.addr.seg = 0;
259     value.addr.off = 0;
260     
261     /* grab PE Header */
262     if (!DEBUG_READ_MEM_VERBOSE((void*)(base + OFFSET_OF(IMAGE_DOS_HEADER, e_lfanew)),
263                                 &pe_header_ofs, sizeof(pe_header_ofs)) ||
264         !DEBUG_READ_MEM_VERBOSE((void*)(base + pe_header_ofs), 
265                                 &pe_header, sizeof(pe_header)))
266         return;
267     
268     if (wmod) {
269         DEBUG_RegisterStabsDebugInfo(wmod, hFile, &pe_header, pe_header_ofs);
270         DEBUG_RegisterMSCDebugInfo(wmod, hFile, &pe_header, pe_header_ofs);     
271     }
272
273     /* Add start of DLL */
274     value.addr.off = base;
275     DEBUG_AddSymbol(name, &value, NULL, SYM_WIN32 | SYM_FUNC);
276     
277     /* Add entry point */
278     wsnprintf(buffer, sizeof(buffer), "%s.EntryPoint", name);
279     value.addr.off = base + pe_header.OptionalHeader.AddressOfEntryPoint;
280     DEBUG_AddSymbol(buffer, &value, NULL, SYM_WIN32 | SYM_FUNC);
281
282     /* Add start of sections */
283     pe_seg_ofs = pe_header_ofs + OFFSET_OF(IMAGE_NT_HEADERS, OptionalHeader) +
284         pe_header.FileHeader.SizeOfOptionalHeader;
285     
286     for (i = 0; i < pe_header.FileHeader.NumberOfSections; i++, pe_seg_ofs += sizeof(pe_seg)) {
287         if (!DEBUG_READ_MEM_VERBOSE((void*)(base + pe_seg_ofs), &pe_seg, sizeof(pe_seg)))
288             continue;
289         wsnprintf(buffer, sizeof(buffer), "%s.%s", name, pe_seg.Name);
290         value.addr.off = base + pe_seg.VirtualAddress;
291         DEBUG_AddSymbol(buffer, &value, NULL, SYM_WIN32 | SYM_FUNC);
292     }
293     
294     /* Add exported functions */
295     dir_ofs = pe_header_ofs + 
296         OFFSET_OF(IMAGE_NT_HEADERS, 
297                   OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]);
298     if (DEBUG_READ_MEM_VERBOSE((void*)(base + dir_ofs), &dir, sizeof(dir)) && dir.Size) {
299         IMAGE_EXPORT_DIRECTORY  exports;
300         WORD*                   ordinals = NULL;
301         void**                  functions = NULL;
302         DWORD*                  names = NULL;
303         int                     j;
304         
305         if (DEBUG_READ_MEM_VERBOSE((void*)(base + dir.VirtualAddress), 
306                                    &exports, sizeof(exports)) &&
307             
308             ((functions = DBG_alloc(sizeof(functions[0]) * exports.NumberOfFunctions))) &&
309             DEBUG_READ_MEM_VERBOSE((void*)(base + (DWORD)exports.AddressOfFunctions),
310                                    functions, sizeof(functions[0]) * exports.NumberOfFunctions) &&
311             
312             ((ordinals = DBG_alloc(sizeof(ordinals[0]) * exports.NumberOfNames))) &&
313             DEBUG_READ_MEM_VERBOSE((void*)(base + (DWORD)exports.AddressOfNameOrdinals),
314                                    ordinals, sizeof(ordinals[0]) * exports.NumberOfNames) &&
315             
316             ((names = DBG_alloc(sizeof(names[0]) * exports.NumberOfNames))) &&
317             DEBUG_READ_MEM_VERBOSE((void*)(base + (DWORD)exports.AddressOfNames),
318                                    names, sizeof(names[0]) * exports.NumberOfNames)) {
319             
320             for (i = 0; i < exports.NumberOfNames; i++) {
321                 if (!names[i] ||
322                     !DEBUG_READ_MEM_VERBOSE((void*)(base + names[i]), bufstr, sizeof(bufstr)))
323                     continue;
324                 bufstr[sizeof(bufstr) - 1] = 0;
325                 wsnprintf(buffer, sizeof(buffer), "%s.%s", name, bufstr);
326                 value.addr.off = base + (DWORD)functions[ordinals[i]];
327                 DEBUG_AddSymbol(buffer, &value, NULL, SYM_WIN32 | SYM_FUNC);
328             }
329             
330             for (i = 0; i < exports.NumberOfFunctions; i++) {
331                 if (!functions[i]) continue;
332                 /* Check if we already added it with a name */
333                 for (j = 0; j < exports.NumberOfNames; j++)
334                     if ((ordinals[j] == i) && names[j]) break;
335                 if (j < exports.NumberOfNames) continue;
336                 wsnprintf(buffer, sizeof(buffer), "%s.%ld", name, i + exports.Base);
337                 value.addr.off = base + (DWORD)functions[i];
338                 DEBUG_AddSymbol(buffer, &value, NULL, SYM_WIN32 | SYM_FUNC);
339             }
340         }
341         DBG_free(functions);
342         DBG_free(ordinals);
343         DBG_free(names);
344     }
345 }
346
347 /***********************************************************************
348  *              DEBUG_LoadEntryPoints
349  *
350  * Load the entry points of all the modules into the hash table.
351  */
352 int DEBUG_LoadEntryPoints(const char* pfx)
353 {
354     MODULEENTRY entry;
355     NE_MODULE   module;
356     void*       moduleAddr;
357     int         first = 0;
358     int         rowcount = 0;
359     int         len;
360
361     /* FIXME: we assume that a module is never removed from memory */
362     /* FIXME: this is (currently plain wrong when debugger is started by
363      *        attaching to an existing program => the 16 bit modules will
364      *        not be shared... not much to do on debugger side... sigh
365      */
366     if (ModuleFirst16(&entry)) do {
367         if (DEBUG_FindModuleByName(entry.szModule, DM_TYPE_UNKNOWN) ||
368             !(moduleAddr = NE_GetPtr(entry.hModule)) ||
369             !DEBUG_READ_MEM_VERBOSE(moduleAddr, &module, sizeof(module)) ||
370             (module.flags & NE_FFLAGS_WIN32) /* NE module */)
371             continue;
372         if (!first) {
373             if (pfx) DEBUG_Printf(DBG_CHN_MESG, pfx);
374             DEBUG_Printf(DBG_CHN_MESG, "   ");
375             rowcount = 3 + (pfx ? strlen(pfx) : 0);
376             first = 1;
377         }
378         
379         len = strlen(entry.szModule);
380         if ((rowcount + len) > 76) {
381             DEBUG_Printf(DBG_CHN_MESG, "\n   ");
382             rowcount = 3;
383         }
384         DEBUG_Printf(DBG_CHN_MESG, " %s", entry.szModule);
385         rowcount += len + 1;
386         
387         DEBUG_LoadModule16(entry.hModule, &module, moduleAddr, entry.szModule);
388     } while (ModuleNext16(&entry));
389     
390     if (first) DEBUG_Printf(DBG_CHN_MESG, "\n"); 
391     return first;
392 }
393
394 /***********************************************************************
395  *           DEBUG_InfoShare
396  *
397  * Display shared libarary information.
398  */
399 void DEBUG_InfoShare(void)
400 {
401     DBG_MODULE* wmod;
402     const char* xtype;
403
404     DEBUG_Printf(DBG_CHN_MESG, "Address\t\tModule\tName\n");
405
406     for (wmod = DEBUG_CurrProcess->modules; wmod; wmod = wmod->next) {
407         switch (wmod->type) {
408         case DM_TYPE_NE:        xtype = "NE"; break;
409         case DM_TYPE_PE:        xtype = "PE"; break;
410         case DM_TYPE_ELF:       xtype = "ELF"; break;
411         default:                xtype = "???"; break;
412         }
413         DEBUG_Printf(DBG_CHN_MESG, "0x%8.8x\t(%s)\t%s\n", (unsigned int)wmod->load_addr,
414                      xtype, wmod->module_name);
415     }
416 }
417
418 static const char*      DEBUG_GetModuleType(int type)
419 {
420     switch (type) {
421     case DM_TYPE_NE:    return "NE";
422     case DM_TYPE_PE:    return "PE";
423     case DM_TYPE_ELF:   return "ELF";
424     default:            return "???";;
425     }
426 }
427
428 static const char*      DEBUG_GetModuleStatus(int status)
429 {
430     switch (status) {
431     case DM_STATUS_NEW:         return "deferred"; 
432     case DM_STATUS_LOADED:      return "ok"; 
433     case DM_STATUS_ERROR:       return "error"; 
434     default:                    return "???"; 
435     }
436 }
437
438 /***********************************************************************
439  *           DEBUG_
440  * Display information about a given module (DLL or EXE)
441  */
442 void DEBUG_DumpModule(DWORD mod)
443 {
444     DBG_MODULE* wmod;
445
446     if (!(wmod = DEBUG_FindModuleByHandle(mod, DM_TYPE_UNKNOWN)) &&
447         !(wmod = DEBUG_FindModuleByAddr((void*)mod, DM_TYPE_UNKNOWN))) {
448         DEBUG_Printf(DBG_CHN_MESG, "'0x%08lx' is not a valid module handle or address\n", mod);
449         return;
450     }
451
452     DEBUG_Printf(DBG_CHN_MESG, "Module '%s' (handle=0x%08x) at 0x%8.8x (%s/%s)\n",
453                  wmod->module_name, wmod->handle, (unsigned int)wmod->load_addr,
454                  DEBUG_GetModuleType(wmod->type), DEBUG_GetModuleStatus(wmod->status));
455 }
456
457 /***********************************************************************
458  *           DEBUG_WalkModules
459  *
460  * Display information about all modules (DLLs and EXEs)
461  */
462 void DEBUG_WalkModules(void)
463 {
464     DBG_MODULE* wmod;
465     const char* xtype;
466
467     DEBUG_Printf(DBG_CHN_MESG, "Address\t\tModule\tName\n");
468
469     for (wmod = DEBUG_CurrProcess->modules; wmod; wmod = wmod->next) {
470         switch (wmod->type) {
471         case DM_TYPE_NE:        xtype = "NE"; break;
472         case DM_TYPE_PE:        xtype = "PE"; break;
473         case DM_TYPE_ELF:       continue;
474         default:                xtype = "???"; break;
475         }
476         
477         DEBUG_Printf(DBG_CHN_MESG, "0x%8.8x\t(%s)\t%s\n", 
478                      (unsigned int)wmod->load_addr, DEBUG_GetModuleType(wmod->type), 
479                      wmod->module_name);
480     }
481 }
482