summaryrefslogtreecommitdiffstats
path: root/private/mvdm/bde/bdemisc.c
blob: 65f646e635b9af098cb28cc9da4e5f84d55aa393 (plain) (blame)
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
/*++

Copyright (c) 1990  Microsoft Corporation

Module Name:

    ntsdexts.c

Abstract:

    This function contains the default ntsd debugger extensions

Author:

    Bob Day      (bobday) 29-Feb-1992 Grabbed standard header

Revision History:

--*/

#include <precomp.h>
#pragma hdrstop
#include <ctype.h>

VOID
DumpMemory(
    UINT DumpType
    )
{
    VDMCONTEXT ThreadContext;
    int mode;
    int i, j, lines = 8;
    WORD selector;
    ULONG offset;
    ULONG base;
    char ch;

    if (!DumpType) {
        return;
    }

    mode = GetContext( &ThreadContext );

    if (!GetNextToken()) {
        PRINTF("Please specify an address\n");
        return;
    }

    if (!ParseIntelAddress(&mode, &selector, &offset)) {
        return;
    }

    if (GetNextToken()) {
        if ((*lpArgumentString == 'l') || (*lpArgumentString == 'L')) {
            lpArgumentString++;
        }
        lines = (EXPRESSION(lpArgumentString)*DumpType+15)/16;
    }

    base = GetInfoFromSelector(selector, mode, NULL) + GetIntelBase();

    for (i=0; i<lines; i++) {
        if (offset & 0xFFFF0000) {
            PRINTF("%04x:%08lx ", selector, offset);
        } else {
            PRINTF("%04x:%04x ", selector, LOWORD(offset));
        }

        switch(DumpType) {

        case 1:
            for (j=0; j<16; j++) {
                if (j==8) {
                    PRINTF("-");
                } else {
                    PRINTF(" ");
                }
                PRINTF("%02x", ReadByteSafe(base+offset+j));
            }
            PRINTF("  ");

            for (j=0; j<16; j++) {
                ch = ReadByteSafe(base+offset+j);
                if (isprint(ch)) {
                    PRINTF("%c", ch);
                } else {
                    PRINTF(".");
                }
            }

            break;
        case 2:
            for (j=0; j<16; j+=2) {
                PRINTF(" %04x", ReadWordSafe(base+offset+j));
            }
            break;
        case 4:
            for (j=0; j<16; j+=4) {
                PRINTF(" %08lx", ReadDwordSafe(base+offset+j));
            }
            break;
        }
        PRINTF("\n");
        offset += 16;

    }

}



VOID
DumpRegs(
) {
    VDMCONTEXT              ThreadContext;
    int                     mode;

    mode = GetContext( &ThreadContext );

    PRINTF("eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
            ThreadContext.Eax,
            ThreadContext.Ebx,
            ThreadContext.Ecx,
            ThreadContext.Edx,
            ThreadContext.Esi,
            ThreadContext.Edi );
    PRINTF("eip=%08lx esp=%08lx ebp=%08lx                ",
            ThreadContext.Eip,
            ThreadContext.Esp,
            ThreadContext.Ebp );
    if ( ThreadContext.EFlags & FLAG_OVERFLOW ) {
        PRINTF("ov ");
    } else {
        PRINTF("nv ");
    }
    if ( ThreadContext.EFlags & FLAG_DIRECTION ) {
        PRINTF("dn ");
    } else {
        PRINTF("up ");
    }
    if ( ThreadContext.EFlags & FLAG_INTERRUPT ) {
        PRINTF("ei ");
    } else {
        PRINTF("di ");
    }
    if ( ThreadContext.EFlags & FLAG_SIGN ) {
        PRINTF("ng ");
    } else {
        PRINTF("pl ");
    }
    if ( ThreadContext.EFlags & FLAG_ZERO ) {
        PRINTF("zr ");
    } else {
        PRINTF("nz ");
    }
    if ( ThreadContext.EFlags & FLAG_AUXILLIARY ) {
        PRINTF("ac ");
    } else {
        PRINTF("na ");
    }
    if ( ThreadContext.EFlags & FLAG_PARITY ) {
        PRINTF("po ");
    } else {
        PRINTF("pe ");
    }
    if ( ThreadContext.EFlags & FLAG_CARRY ) {
        PRINTF("cy ");
    } else {
        PRINTF("nc ");
    }
    PRINTF("\n");
    PRINTF("cs=%04x  ss=%04x  ds=%04x  es=%04x  fs=%04x  gs=%04x             efl=%08lx\n",
            ThreadContext.SegCs,
            ThreadContext.SegSs,
            ThreadContext.SegDs,
            ThreadContext.SegEs,
            ThreadContext.SegFs,
            ThreadContext.SegGs,
            ThreadContext.EFlags );
}


VOID
ListModules(
) {
    VDMCONTEXT              ThreadContext;
    int                     mode;
    HEAPENTRY               he = {0};
    SELECTORINFO si;

    mode = GetContext( &ThreadContext );

    if (GetNextToken()) {
        he.Selector = (WORD) EXPRESSION( lpArgumentString );
    }

    PRINTF("Sel    Base     Limit  Type Seg   Module\n");
    PRINTF("==== ======== ======== ==== ==== ========\n");


    while (FindHeapEntry(&he, FALSE)) {
        if (he.SegmentNumber != -1) {
            GetInfoFromSelector((WORD)(he.gnode.pga_handle | 1), PROT_MODE, &si);
            PRINTF("%04X %08lX %08lX",
                he.gnode.pga_handle | 1,
                he.gnode.pga_address,
                he.gnode.pga_size - 1);

            PRINTF(" %s", si.bCode ? "Code" : "Data");

            PRINTF(" %04X %s\n",
                he.SegmentNumber, 
                he.OwnerName);
        }

    }

}

VOID
DumpDescriptor(
) {
    VDMCONTEXT              ThreadContext;
    WORD                    selector;
    ULONG                   Base;
    int                     mode;
    SELECTORINFO            si;
    int                     i, count = 16;

    mode = GetContext( &ThreadContext );

    if (GetNextToken()) {
        selector = (WORD) EXPRESSION( lpArgumentString );
    } else {
        PRINTF("Please enter a selector\n");
        return;
    }

    for (i=0; i<count; i++) {

        Base = GetInfoFromSelector( selector, mode, &si );

        PRINTF("%04X => Base: %08lX", selector, Base);

#ifndef i386
        PRINTF(" (%08X)", Base+GetIntelBase());
#endif

        PRINTF("  Limit: %08lX  %s %s %s %s\n",
                si.Limit,
                si.bPresent ? " P" : "NP",
                si.bSystem ? "System" : si.bCode     ? "Code  " : "Data  ",
                si.bSystem ? ""       : si.bWrite    ? "W" : "R",
                si.bSystem ? ""       : si.bAccessed ? "A" : ""
                );

        selector+=8;
    }

}


//
//  Dump Taskinfo;
//
//  If no argument, dump all wow tasks.
//  If 0, dump current WOW task
//  Else dump the specifies task {which is thread-id as shown by
//  ~ command under ntsd like 37.6b so thread-id is 6b)
//

void DumpTaskInfo (ptd,mode)
PTD  ptd;
int    mode;
{

    ULONG                   Base;
    TDB                     tdb;
    BOOL                    b;

    Base = GetInfoFromSelector( ptd->htask16, mode, NULL );
    b = ReadProcessMem( hCurrentProcess,
                           (LPVOID) (Base+GetIntelBase()),
                           &tdb,
                           sizeof(tdb),
                           NULL );

    if ( !b ) {
        PRINTF("Failure reading TDB at %X\n", Base );
        return;
    }

    PRINTF("\nDump for ThreadId = %x\n",ptd->dwThreadID);
    PRINTF("    Stack          =   %x:%x\n",HIWORD(ptd->vpStack),LOWORD(ptd->vpStack));
    PRINTF("    HTask (TDB)    =   %x\n", ptd->htask16);
    PRINTF("    HInst          =   %x\n", ptd->hInst16);
    PRINTF("    HMod16         =   %x\n", ptd->hMod16);
    PRINTF("    CompatFlags    =   %x\n",ptd->dwWOWCompatFlags);
    PRINTF("    HThread        =   %x\n",ptd->hThread);
    PRINTF("    TDBFlags       =   %x\n",tdb.TDB_flags);
    PRINTF("    ExpWinVer      =   %x\n",tdb.TDB_ExpWinVer);
    PRINTF("    DTA            =   %x:%x\n",HIWORD(tdb.TDB_DTA),LOWORD(tdb.TDB_DTA));
    PRINTF("    CurDir         =   %.64s\n",tdb.TDB_Directory);
    PRINTF("    ModName        =   %.8s\n",tdb.TDB_ModName);
}

void TaskInfo (
) {
    VDMCONTEXT              ThreadContext;
    DWORD                   ThreadId;
    PTD                     ptd,ptdHead;
    TD                      td;
    int                     mode;
    BOOL                    b,fFound=FALSE;


    mode = GetContext( &ThreadContext );

    ThreadId = (DWORD)-1;  // Assume Dump All Tasks
    if (GetNextToken()) {
        ThreadId = (DWORD) EXPRESSION( lpArgumentString );
    }

    ptdHead = (PTD)EXPRESSION("wow32!gptdTaskHead");

    // get the pointer to first TD
    b = ReadProcessMem( hCurrentProcess,
                           (LPVOID) (ptdHead),
                           &ptd,
                           sizeof(DWORD),
                           NULL );

    if ( !b ) {
        PRINTF("Failure reading gptdTaskHead at %08lX\n", ptdHead );
        return;
    }

    // enumerate td list to find the match(es)
    while (ptd) {
        b = ReadProcessMem( hCurrentProcess,
                           (LPVOID) (ptd),
                           &td,
                           sizeof(TD),
                           NULL );
        if ( !b ) {
            PRINTF("Failure reading TD At %08lX\n", ptd );
            return;
        }

        if (ThreadId == -1) {
            DumpTaskInfo (&td,mode);
            fFound = TRUE;
        }
        else {
            if (ThreadId == td.dwThreadID) {
                DumpTaskInfo (&td,mode);
                fFound = TRUE;
                break;
            }
        }
        ptd = td.ptdNext;
    }

    if (!fFound) {
        if (ThreadId == -1) {
            PRINTF("No WOW Task Found.\n");
        }
        else
            PRINTF("WOW Task With Thread Id = %02x Not Found.\n",ThreadId);
    }
    return;
}