File: module.c

package info (click to toggle)
wine 0.0.20020411-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 43,012 kB
  • ctags: 104,265
  • sloc: ansic: 550,196; perl: 21,747; yacc: 3,990; sh: 3,904; makefile: 3,297; tcl: 2,616; lex: 2,443
file content (644 lines) | stat: -rw-r--r-- 20,675 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/* -*- tab-width: 8; c-basic-offset: 4 -*- */
/*
 * File module.c - module handling for the wine debugger
 *
 * Copyright (C) 1993, Eric Youngdale.
 * 		 2000, Eric Pouech
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "debugger.h"
#include "wingdi.h"
#include "winuser.h"

/***********************************************************************
 * Creates and links a new module to the current process 
 *
 */
DBG_MODULE*	DEBUG_AddModule(const char* name, enum DbgModuleType type, 
				void* mod_addr, u_long size, HMODULE hmodule)
{
    DBG_MODULE*	wmod;

    if (!(wmod = (DBG_MODULE*)DBG_alloc(sizeof(*wmod))))
	return NULL;

    memset(wmod, 0, sizeof(*wmod));

    wmod->dil = DIL_DEFERRED;
    wmod->main = (DEBUG_CurrProcess->num_modules == 0);
    wmod->type = type;
    wmod->load_addr = mod_addr;
    wmod->size = size;
    wmod->handle = hmodule;
    wmod->dbg_index = DEBUG_CurrProcess->next_index;
    wmod->module_name = DBG_strdup(name);

    DEBUG_CurrProcess->modules = DBG_realloc(DEBUG_CurrProcess->modules,
					     ++DEBUG_CurrProcess->num_modules * sizeof(DBG_MODULE*));
    DEBUG_CurrProcess->modules[DEBUG_CurrProcess->num_modules - 1] = wmod;

    return wmod;
}

/***********************************************************************
 *	DEBUG_FindModuleByName
 *
 */
DBG_MODULE*	DEBUG_FindModuleByName(const char* name, enum DbgModuleType type)
{
     int		i;
     DBG_MODULE**	amod = DEBUG_CurrProcess->modules;
     
     for (i = 0; i < DEBUG_CurrProcess->num_modules; i++) {
	 if ((type == DMT_UNKNOWN || type == amod[i]->type) &&
	     !strcasecmp(name, amod[i]->module_name)) 
	     return amod[i];
     }
     return NULL;
}

/***********************************************************************
 *	DEBUG_FindModuleByAddr
 *
 * either the addr where module is loaded, or any address inside the 
 * module
 */
DBG_MODULE*	DEBUG_FindModuleByAddr(void* addr, enum DbgModuleType type)
{
     int		i;
     DBG_MODULE**	amod = DEBUG_CurrProcess->modules;
     DBG_MODULE*	res = NULL;
    
     for (i = 0; i < DEBUG_CurrProcess->num_modules; i++) {
	 if ((type == DMT_UNKNOWN || type == amod[i]->type) &&
	     (u_long)addr >= (u_long)amod[i]->load_addr &&
	     (u_long)addr < (u_long)amod[i]->load_addr + (u_long)amod[i]->size) {
	     /* amod[i] contains it... check against res now */
	     if (!res || res->load_addr < amod[i]->load_addr)
		 res = amod[i];
	 }
     }
     return res;
}

/***********************************************************************
 *		DEBUG_FindModuleByHandle
 */
DBG_MODULE*	DEBUG_FindModuleByHandle(HANDLE handle, enum DbgModuleType type)
{
     int		i;
     DBG_MODULE**	amod = DEBUG_CurrProcess->modules;
     
     for (i = 0; i < DEBUG_CurrProcess->num_modules; i++) {
	 if ((type == DMT_UNKNOWN || type == amod[i]->type) && 
	     handle == amod[i]->handle) 
	     return amod[i];
     }
     return NULL;
}

/***********************************************************************
 *		DEBUG_GetProcessMainModule
 */
DBG_MODULE*	DEBUG_GetProcessMainModule(DBG_PROCESS* process)
{
    if (!process || !process->num_modules)	return NULL;
 
    /* main module is the first to be loaded on a given process, so it's the first
     * in the array */
    assert(process->modules[0]->main);
    return process->modules[0];
}

/***********************************************************************
 *			DEBUG_RegisterELFModule
 *
 * ELF modules are also entered into the list - this is so that we
 * can make 'info shared' types of displays possible.
 */
DBG_MODULE* DEBUG_RegisterELFModule(u_long load_addr, u_long size, const char* name)
{
    DBG_MODULE*	wmod = DEBUG_AddModule(name, DMT_ELF, (void*)load_addr, size, 0);

    if (!wmod) return NULL;

    DEBUG_CurrProcess->next_index++;

    return wmod;
}

/***********************************************************************
 *			DEBUG_RegisterPEModule
 *
 */
DBG_MODULE* DEBUG_RegisterPEModule(HMODULE hModule, u_long load_addr, u_long size, const char *module_name)
{
    DBG_MODULE*	wmod = DEBUG_AddModule(module_name, DMT_PE, (void*)load_addr, size, hModule);

    if (!wmod) return NULL;

    DEBUG_CurrProcess->next_index++;

    return wmod;
}

/***********************************************************************
 *			DEBUG_RegisterNEModule
 *
 */
DBG_MODULE* DEBUG_RegisterNEModule(HMODULE hModule, void* load_addr, u_long size, const char *module_name)
{
    DBG_MODULE*	wmod = DEBUG_AddModule(module_name, DMT_NE, load_addr, size, hModule);

    if (!wmod) return NULL;

    DEBUG_CurrProcess->next_index++;
    return wmod;
}

#if 0
/***********************************************************************
 *           DEBUG_GetEP16
 *
 * Helper function fo DEBUG_LoadModuleEPs16:
 *	finds the address of a given entry point from a given module
 */
static BOOL DEBUG_GetEP16(char* moduleAddr, const NE_MODULE* module, 
			  WORD ordinal, DBG_ADDR* addr)
{
    void*		idx;
    ET_ENTRY		entry;
    ET_BUNDLE		bundle;
    SEGTABLEENTRY	ste;

    bundle.next = module->entry_table;
    do {
	if (!bundle.next)
	    return FALSE;
	idx = moduleAddr + bundle.next;
	if (!DEBUG_READ_MEM_VERBOSE(idx, &bundle, sizeof(bundle)))
	    return FALSE;
    } while ((ordinal < bundle.first + 1) || (ordinal > bundle.last));
    
    if (!DEBUG_READ_MEM_VERBOSE((char*)idx + sizeof(ET_BUNDLE) + 
				(ordinal - bundle.first - 1) * sizeof(ET_ENTRY), 
				&entry, sizeof(ET_ENTRY)))
	return FALSE;
    
    addr->seg = entry.segnum;
    addr->off = entry.offs;
    
    if (addr->seg == 0xfe) addr->seg = 0xffff;  /* constant entry */
    else {
	if (!DEBUG_READ_MEM_VERBOSE(moduleAddr + module->seg_table + 
				    sizeof(ste) * (addr->seg - 1),
				    &ste, sizeof(ste)))
	    return FALSE;
	addr->seg = GlobalHandleToSel16(ste.hSeg);
    }
    return TRUE;
}

/***********************************************************************
 *           DEBUG_LoadModule16
 *
 * Load the entry points of a Win16 module into the hash table.
 */
static void DEBUG_LoadModule16(HMODULE hModule, NE_MODULE* module, char* moduleAddr, const char* name)
{
    DBG_VALUE	value;
    BYTE	buf[1 + 256 + 2];
    char 	epname[512];
    char*	cpnt;
    DBG_MODULE*	wmod;

    wmod = DEBUG_RegisterNEModule(hModule, moduleAddr, name);

    value.type = NULL;
    value.cookie = DV_TARGET;
    value.addr.seg = 0;
    value.addr.off = 0;
    
    cpnt = moduleAddr + module->name_table;
    
    /* First search the resident names */
    
    /* skip module name */
    if (!DEBUG_READ_MEM_VERBOSE(cpnt, buf, sizeof(buf)) || !buf[0])
	return;
    cpnt += 1 + buf[0] + sizeof(WORD);
    
    while (DEBUG_READ_MEM_VERBOSE(cpnt, buf, sizeof(buf)) && buf[0]) {
	sprintf(epname, "%s.%.*s", name, buf[0], &buf[1]);
	if (DEBUG_GetEP16(moduleAddr, module, *(WORD*)&buf[1 + buf[0]], &value.addr)) {
	    DEBUG_AddSymbol(epname, &value, NULL, SYM_WIN32 | SYM_FUNC);
	}
	cpnt += buf[0] + 1 + sizeof(WORD);
    }
    
    /* Now search the non-resident names table */
    if (!module->nrname_handle) return;  /* No non-resident table */
    cpnt = (char *)GlobalLock16(module->nrname_handle);
    while (DEBUG_READ_MEM_VERBOSE(cpnt, buf, sizeof(buf)) && buf[0]) {
	sprintf(epname, "%s.%.*s", name, buf[0], &buf[1]);
	if (DEBUG_GetEP16(moduleAddr, module, *(WORD*)&buf[1 + buf[0]], &value.addr)) {
	    DEBUG_AddSymbol(epname, &value, NULL, SYM_WIN32 | SYM_FUNC);
	}
	cpnt += buf[0] + 1 + sizeof(WORD);
    }
    GlobalUnlock16(module->nrname_handle);
}
#endif

/***********************************************************************
 *			DEBUG_LoadModule32
 */
void	DEBUG_LoadModule32(const char* name, HANDLE hFile, DWORD base)
{
     IMAGE_NT_HEADERS		pe_header;
     DWORD			nth_ofs;
     DBG_MODULE*		wmod = NULL;
     int 			i;
     IMAGE_SECTION_HEADER 	pe_seg;
     DWORD			pe_seg_ofs;
     DWORD			size = 0;
     enum DbgInfoLoad		dil = DIL_ERROR;

     /* grab PE Header */
     if (!DEBUG_READ_MEM_VERBOSE((void*)(base + OFFSET_OF(IMAGE_DOS_HEADER, e_lfanew)),
				 &nth_ofs, sizeof(nth_ofs)) ||
	 !DEBUG_READ_MEM_VERBOSE((void*)(base + nth_ofs), &pe_header, sizeof(pe_header)))
	 return;
     
     pe_seg_ofs = nth_ofs + OFFSET_OF(IMAGE_NT_HEADERS, OptionalHeader) +
	 pe_header.FileHeader.SizeOfOptionalHeader;
     
     for (i = 0; i < pe_header.FileHeader.NumberOfSections; i++, pe_seg_ofs += sizeof(pe_seg)) {
	 if (!DEBUG_READ_MEM_VERBOSE((void*)(base + pe_seg_ofs), &pe_seg, sizeof(pe_seg)))
	     continue;
	 if (size < pe_seg.VirtualAddress + pe_seg.SizeOfRawData)
	     size = pe_seg.VirtualAddress + pe_seg.SizeOfRawData;
     }

     /* FIXME: we make the assumption that hModule == base */
     wmod = DEBUG_RegisterPEModule((HMODULE)base, base, size, name);
     if (wmod) {
	 dil = DEBUG_RegisterStabsDebugInfo(wmod, hFile, &pe_header, nth_ofs);
	 if (dil != DIL_LOADED)
	     dil = DEBUG_RegisterMSCDebugInfo(wmod, hFile, &pe_header, nth_ofs);
	 if (dil != DIL_LOADED)
	     dil = DEBUG_RegisterPEDebugInfo(wmod, hFile, &pe_header, nth_ofs); 
	 wmod->dil = dil;
     }

     DEBUG_ReportDIL(dil, "32bit DLL", name, base);
}

/***********************************************************************
 *			DEBUG_RegisterPEDebugInfo
 */
enum DbgInfoLoad	DEBUG_RegisterPEDebugInfo(DBG_MODULE* wmod, HANDLE hFile,
						  void* _nth, unsigned long nth_ofs)
{
    DBG_VALUE			value;
    char			buffer[512];
    char			bufstr[256];
    unsigned int 		i;
    IMAGE_SECTION_HEADER 	pe_seg;
    DWORD			pe_seg_ofs;
    IMAGE_DATA_DIRECTORY 	dir;
    DWORD			dir_ofs;
    const char*			prefix;
    IMAGE_NT_HEADERS*		nth = (PIMAGE_NT_HEADERS)_nth;
    DWORD			base = (u_long)wmod->load_addr;

    value.type = NULL;
    value.cookie = DV_TARGET;
    value.addr.seg = 0;
    value.addr.off = 0;
    
    /* Add start of DLL */
    value.addr.off = base;
    if ((prefix = strrchr(wmod->module_name, '\\' ))) prefix++;
    else prefix = wmod->module_name;

    DEBUG_AddSymbol(prefix, &value, NULL, SYM_WIN32 | SYM_FUNC);
    
    /* Add entry point */
    snprintf(buffer, sizeof(buffer), "%s.EntryPoint", prefix);
    value.addr.off = base + nth->OptionalHeader.AddressOfEntryPoint;
    DEBUG_AddSymbol(buffer, &value, NULL, SYM_WIN32 | SYM_FUNC);

    /* Add start of sections */
    pe_seg_ofs = nth_ofs + OFFSET_OF(IMAGE_NT_HEADERS, OptionalHeader) +
	nth->FileHeader.SizeOfOptionalHeader;
    
    for (i = 0; i < nth->FileHeader.NumberOfSections; i++, pe_seg_ofs += sizeof(pe_seg)) {
	if (!DEBUG_READ_MEM_VERBOSE((void*)(base + pe_seg_ofs), &pe_seg, sizeof(pe_seg)))
	    continue;
	snprintf(buffer, sizeof(buffer), "%s.%s", prefix, pe_seg.Name);
	value.addr.off = base + pe_seg.VirtualAddress;
	DEBUG_AddSymbol(buffer, &value, NULL, SYM_WIN32 | SYM_FUNC);
    }
    
    /* Add exported functions */
    dir_ofs = nth_ofs + 
	OFFSET_OF(IMAGE_NT_HEADERS, 
		  OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]);
    if (DEBUG_READ_MEM_VERBOSE((void*)(base + dir_ofs), &dir, sizeof(dir)) && dir.Size) {
	IMAGE_EXPORT_DIRECTORY 	exports;
	WORD*			ordinals = NULL;
	void**			functions = NULL;
	DWORD*			names = NULL;
	unsigned int		j;
	
	if (DEBUG_READ_MEM_VERBOSE((void*)(base + dir.VirtualAddress), 
				   &exports, sizeof(exports)) &&
	    
	    ((functions = DBG_alloc(sizeof(functions[0]) * exports.NumberOfFunctions))) &&
	    DEBUG_READ_MEM_VERBOSE((void*)(base + (DWORD)exports.AddressOfFunctions),
				   functions, sizeof(functions[0]) * exports.NumberOfFunctions) &&
	    
	    ((ordinals = DBG_alloc(sizeof(ordinals[0]) * exports.NumberOfNames))) &&
	    DEBUG_READ_MEM_VERBOSE((void*)(base + (DWORD)exports.AddressOfNameOrdinals),
				   ordinals, sizeof(ordinals[0]) * exports.NumberOfNames) &&
	    
	    ((names = DBG_alloc(sizeof(names[0]) * exports.NumberOfNames))) &&
	    DEBUG_READ_MEM_VERBOSE((void*)(base + (DWORD)exports.AddressOfNames),
				   names, sizeof(names[0]) * exports.NumberOfNames)) {

	    for (i = 0; i < exports.NumberOfNames; i++) {
		if (!names[i] ||
		    !DEBUG_READ_MEM_VERBOSE((void*)(base + names[i]), bufstr, sizeof(bufstr)))
		    continue;
		bufstr[sizeof(bufstr) - 1] = 0;
		snprintf(buffer, sizeof(buffer), "%s.%s", prefix, bufstr);
		value.addr.off = base + (DWORD)functions[ordinals[i]];
		DEBUG_AddSymbol(buffer, &value, NULL, SYM_WIN32 | SYM_FUNC);
	    }
	    
	    for (i = 0; i < exports.NumberOfFunctions; i++) {
		if (!functions[i]) continue;
		/* Check if we already added it with a name */
		for (j = 0; j < exports.NumberOfNames; j++)
		    if ((ordinals[j] == i) && names[j]) break;
		if (j < exports.NumberOfNames) continue;
		snprintf(buffer, sizeof(buffer), "%s.%ld", prefix, i + exports.Base);
		value.addr.off = base + (DWORD)functions[i];
		DEBUG_AddSymbol(buffer, &value, NULL, SYM_WIN32 | SYM_FUNC);
	    }
	}
	DBG_free(functions);
	DBG_free(ordinals);
	DBG_free(names);
    }
    /* no real debug info, only entry points */
    return DIL_NOINFO;
}

/***********************************************************************
 *           	DEBUG_LoadEntryPoints
 *
 * Load the entry points of all the modules into the hash table.
 */
int DEBUG_LoadEntryPoints(const char* pfx)
{
    int		first = 0;
    /* FIXME: with address space separation in space, this is plain wrong
     *	      it requires the 16 bit WOW debugging interface...
     */
#if 0
    MODULEENTRY	entry;
    NE_MODULE	module;
    void*	moduleAddr;
    int		rowcount = 0;
    int		len;

    /* FIXME: we assume that a module is never removed from memory */
    /* FIXME: this is (currently plain wrong when debugger is started by
     *	      attaching to an existing program => the 16 bit modules will
     *        not be shared... not much to do on debugger side... sigh
     */
    if (ModuleFirst16(&entry)) do {
	if (DEBUG_FindModuleByName(entry.szModule, DM_TYPE_UNKNOWN) ||
	    !(moduleAddr = NE_GetPtr(entry.hModule)) ||
	    !DEBUG_READ_MEM_VERBOSE(moduleAddr, &module, sizeof(module)) ||
	    (module.flags & NE_FFLAGS_WIN32) /* NE module */)
	    continue;
	if (!first) {
	    if (pfx) DEBUG_Printf(DBG_CHN_MESG, pfx);
	    DEBUG_Printf(DBG_CHN_MESG, "   ");
	    rowcount = 3 + (pfx ? strlen(pfx) : 0);
	    first = 1;
	}
	
	len = strlen(entry.szModule);
	if ((rowcount + len) > 76) {
	    DEBUG_Printf(DBG_CHN_MESG, "\n   ");
	    rowcount = 3;
	}
	DEBUG_Printf(DBG_CHN_MESG, " %s", entry.szModule);
	rowcount += len + 1;
	
	DEBUG_LoadModule16(entry.hModule, &module, moduleAddr, entry.szModule);
    } while (ModuleNext16(&entry));
#endif
    
    if (first) DEBUG_Printf(DBG_CHN_MESG, "\n"); 
    return first;
}

void	DEBUG_ReportDIL(enum DbgInfoLoad dil, const char* pfx, const char* filename, DWORD load_addr)
{
    const char*	fmt;

    switch (dil) {
    case DIL_DEFERRED:	
	fmt = "Deferring debug information loading for %s '%s' (0x%08x)\n";
	break;
    case DIL_LOADED:	
	fmt = "Loaded debug information from %s '%s' (0x%08x)\n"; 	
	break;
    case DIL_NOINFO:	
	fmt = "No debug information in %s '%s' (0x%08x)\n";		
	break;
    case DIL_ERROR:	
	fmt = "Can't find file for %s '%s' (0x%08x)\n";			
	break;
    default: 
	DEBUG_Printf(DBG_CHN_ERR, "Oooocch (%d)\n", dil); 
	return;
    }
   
    DEBUG_Printf(DBG_CHN_MESG, fmt, pfx, filename, load_addr);
}

static const char*      DEBUG_GetModuleType(enum DbgModuleType type)
{
    switch (type) {
    case DMT_NE:    return "NE";
    case DMT_PE:    return "PE";
    case DMT_ELF:   return "ELF";
    default:        return "???";;
    }
}

static const char*      DEBUG_GetDbgInfo(enum DbgInfoLoad dil)
{
    switch (dil) {
    case DIL_LOADED:	return "loaded";
    case DIL_DEFERRED: 	return "deferred";
    case DIL_NOINFO:	return "none";
    case DIL_ERROR:	return "error";
    default:            return "?";
    }
}

/***********************************************************************
 *           DEBUG_ModuleCompare
 *
 * returns -1 is p1 < p2, 0 is p1 == p2, +1 if p1 > p2
 * order used is order on load_addr of a module
 */
static int	DEBUG_ModuleCompare(const void* p1, const void* p2)
{
    return (*((const DBG_MODULE**)p1))->load_addr - 
	   (*((const DBG_MODULE**)p2))->load_addr;
}

/***********************************************************************
 *           DEBUG_IsContainer
 *
 * returns TRUE is wmod_child is contained (inside bounds) of wmod_cntnr
 */
static inline BOOL DEBUG_IsContainer(const DBG_MODULE* wmod_cntnr, 
				     const DBG_MODULE* wmod_child)
{
    return wmod_cntnr->load_addr < wmod_child->load_addr &&
	(DWORD)wmod_cntnr->load_addr + wmod_cntnr->size > 
	(DWORD)wmod_child->load_addr + wmod_child->size;
}

static void	DEBUG_InfoShareModule(const DBG_MODULE* module, int ident)
{
    if (ident) DEBUG_Printf(DBG_CHN_MESG, "  \\-");
    DEBUG_Printf(DBG_CHN_MESG, "%s\t0x%08lx-%08lx\t%s\n", 
		 DEBUG_GetModuleType(module->type),
		 (DWORD)module->load_addr, (DWORD)module->load_addr + module->size,
		 module->module_name);
}	

/***********************************************************************
 *           DEBUG_InfoShare
 *
 * Display shared libarary information.
 */
void DEBUG_InfoShare(void)
{
    DBG_MODULE**	ref;
    int			i, j;
    
    ref = DBG_alloc(sizeof(DBG_MODULE*) * DEBUG_CurrProcess->num_modules);
    if (!ref) return;
    
    DEBUG_Printf(DBG_CHN_MESG, "Module\tAddress\t\t\tName\t%d modules\n", 
		 DEBUG_CurrProcess->num_modules);
    
    memcpy(ref, DEBUG_CurrProcess->modules, 
	   sizeof(DBG_MODULE*) * DEBUG_CurrProcess->num_modules);
    qsort(ref, DEBUG_CurrProcess->num_modules, sizeof(DBG_MODULE*), 
	  DEBUG_ModuleCompare);
    for (i = 0; i < DEBUG_CurrProcess->num_modules; i++) {
	switch (ref[i]->type) {
	case DMT_ELF:
	    DEBUG_InfoShareModule(ref[i], 0);
	    for (j = 0; j < DEBUG_CurrProcess->num_modules; j++) {
		if (ref[j]->type != DMT_ELF && DEBUG_IsContainer(ref[i], ref[j]))
		    DEBUG_InfoShareModule(ref[j], 1);
	    }
	    break;
	case DMT_NE:
	case DMT_PE:
	    /* check module is not in ELF */
	    for (j = 0; j < DEBUG_CurrProcess->num_modules; j++) {
		if (ref[j]->type == DMT_ELF &&
		    DEBUG_IsContainer(ref[j], ref[i]))
		    break;
	    }
	    if (j >= DEBUG_CurrProcess->num_modules) 
		DEBUG_InfoShareModule(ref[i], 0);
	    break;
	default:
	    DEBUG_Printf(DBG_CHN_ERR, "Unknown type (%d)\n", ref[i]->type);
	}
    }
    DBG_free(ref);
}

/***********************************************************************
 *           DEBUG_DumpModule
 * Display information about a given module (DLL or EXE)
 */
void DEBUG_DumpModule(DWORD mod)
{
    DBG_MODULE*	wmod;

    if (!(wmod = DEBUG_FindModuleByHandle((HANDLE)mod, DMT_UNKNOWN)) &&
	!(wmod = DEBUG_FindModuleByAddr((void*)mod, DMT_UNKNOWN))) {
	DEBUG_Printf(DBG_CHN_MESG, "'0x%08lx' is not a valid module handle or address\n", mod);
	return;
    }

    DEBUG_Printf(DBG_CHN_MESG, "Module '%s' (handle=%p) 0x%08lx-0x%08lx (%s, debug info %s)\n",
		 wmod->module_name, wmod->handle, (DWORD)wmod->load_addr,
		 (DWORD)wmod->load_addr + wmod->size,
		 DEBUG_GetModuleType(wmod->type), DEBUG_GetDbgInfo(wmod->dil));
}

/***********************************************************************
 *           DEBUG_WalkModules
 *
 * Display information about all modules (DLLs and EXEs)
 */
void DEBUG_WalkModules(void)
{
    DBG_MODULE**	amod;
    int			i;
    
    DEBUG_Printf(DBG_CHN_MESG, "Address\t\t\tModule\tName\n");
    
    amod = DBG_alloc(sizeof(DBG_MODULE*) * DEBUG_CurrProcess->num_modules);
    if (!amod) return;
    
    memcpy(amod, DEBUG_CurrProcess->modules, 
	   sizeof(DBG_MODULE*) * DEBUG_CurrProcess->num_modules);
    qsort(amod, DEBUG_CurrProcess->num_modules, sizeof(DBG_MODULE*), 
	  DEBUG_ModuleCompare);
    for (i = 0; i < DEBUG_CurrProcess->num_modules; i++) {
	if (amod[i]->type == DMT_ELF)	continue;
 	
	DEBUG_Printf(DBG_CHN_MESG, "0x%08lx-%08lx\t(%s)\t%s\n", 
		     (DWORD)amod[i]->load_addr, 
		     (DWORD)amod[i]->load_addr + amod[i]->size,
		     DEBUG_GetModuleType(amod[i]->type), amod[i]->module_name);
    }
    DBG_free(amod);
}