summaryrefslogblamecommitdiffstats
path: root/private/sdktools/dumpexam/da_x86.c
blob: 957cf221b587c5db941f5c184d87a22cb2d1b239 (plain) (tree)
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











































































































































































































































































































































































































































































































































































                                                                                                    
#define TARGET_i386
#include <platform.h>
#include <imagehlp.h>
#include <crash.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "dumpexam.h"


#define GetContext(p,c)    GetContextX86(p,c)
#define MAX_STACK_FRAMES   100
#define SAVE_EBP(f)        f.Reserved[0]
#define TRAP_TSS(f)        f.Reserved[1]
#define TRAP_EDITED(f)     f.Reserved[1]
#define SAVE_TRAP(f)       f.Reserved[2]


extern FILE *FileOut;

VOID
PrintRegisters(
    ULONG               Processor,
    PCONTEXT            Context,
    PKSPECIAL_REGISTERS SpecialRegContext
    );



static
DWORD
GetStackTrace(
    PDUMP_HEADER    DmpHeader,
    ULONG           Processor,
    LPSTACKFRAME    Frames,
    ULONG           MaxFrames,
    ULONG           ProgramCounter,
    ULONG           FramePointer,
    ULONG           StackPointer
    )
{
    BOOL            rVal;
    STACKFRAME      StackFrame;
    DWORD           FrameCnt;


    ZeroMemory( &StackFrame, sizeof(STACKFRAME) );

    //
    // setup the program counter
    //
    StackFrame.AddrPC.Offset       = ProgramCounter;
    StackFrame.AddrPC.Mode         = AddrModeFlat;

    //
    // setup the frame pointer
    //
    StackFrame.AddrFrame.Offset    = FramePointer;
    StackFrame.AddrFrame.Mode      = AddrModeFlat;

    //
    // setup the stack pointer
    //
    StackFrame.AddrStack.Offset    = StackPointer;
    StackFrame.AddrStack.Mode      = AddrModeFlat;

    FrameCnt = 0;
    do {
        rVal = StackWalk(
            IMAGE_FILE_MACHINE_I386,
            (HANDLE)DmpHeader,
            (HANDLE)Processor,
            &StackFrame,
            NULL,
            SwReadMemory,
            SwFunctionTableAccess,
            SwGetModuleBase,
            NULL
            );
        if (rVal) {
            CopyMemory(
                &Frames[FrameCnt],
                &StackFrame,
                sizeof(STACKFRAME)
                );
            FrameCnt += 1;
        }
    } while( rVal && FrameCnt < MaxFrames );

    return FrameCnt;
}

static
VOID
PrintStackTrace(
    PDUMP_HEADER    DmpHeader,
    ULONG           Processor,
    LPSTACKFRAME    StackFrames,
    ULONG           FrameCnt
    )
{
    PFPO_DATA           pFpoData;
    PIMAGEHLP_SYMBOL    Symbol;
    ULONG               i;
    ULONG               Displacement;
    CHAR                SymBuf[512];


    PrintHeading( "Stack Trace" );
    fprintf( FileOut, "ChildEBP RetAddr  Args to Child\n" );

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

        if (SymGetSymFromAddr( DmpHeader, StackFrames[i].AddrPC.Offset, &Displacement, sym )) {
            strcpy( SymBuf, sym->Name );
        } else {
            sprintf( SymBuf, "0x%08x", StackFrames[i].AddrPC.Offset );
        }

        fprintf(
            FileOut,
            "%08x %08x %08x %08x %08x %s",
            StackFrames[i].AddrFrame.Offset,
            StackFrames[i].AddrReturn.Offset,
            StackFrames[i].Params[0],
            StackFrames[i].Params[1],
            StackFrames[i].Params[2],
            SymBuf
            );

        if (Displacement) {
            fprintf(
                FileOut,
                "+0x%x",
                Displacement
                );
        }

        if (StackFrames[i].FuncTableEntry) {
            pFpoData = (PFPO_DATA)StackFrames[i].FuncTableEntry;
            switch (pFpoData->cbFrame) {
                case FRAME_FPO:
                    if (pFpoData->fHasSEH) {
                        fprintf( FileOut, "(FPO: [SEH])" );
                    } else {
                        fprintf( FileOut, " (FPO:" );
                        if (pFpoData->fUseBP) {
                            fprintf(
                                FileOut,
                                " [EBP 0x%08x]",
                                SAVE_EBP(StackFrames[i])
                                );
                        }
                        fprintf(
                            FileOut,
                            " [%d,%d,%d])",
                            pFpoData->cdwParams,
                            pFpoData->cdwLocals,
                            pFpoData->cbRegs
                            );
                    }
                    break;

                case FRAME_TRAP:
                    fprintf(
                        FileOut,
                        " (FPO: [%d,%d] TrapFrame%s @ %08lx)",
                        pFpoData->cdwParams,
                        pFpoData->cdwLocals,
                        TRAP_EDITED(StackFrames[i]) ? "" : "-EDITED",
                        SAVE_TRAP(StackFrames[i])
                        );
                    break;

                case FRAME_TSS:
                    fprintf( FileOut, " (FPO: TaskGate %lx:0)", TRAP_TSS(StackFrames[i]) );
                    break;

                default:
                    fprintf( FileOut, "(UKNOWN FPO TYPE)" );
                    break;
            }
        }
        fprintf( FileOut, "\n" );
    }

    fprintf( FileOut, "\n" );

}

VOID
PrintStackTraceX86(
    PDUMP_HEADER    DmpHeader,
    ULONG           Processor
    )
{
    PFPO_DATA           pFpoData;
    CONTEXT             Context;
    STACKFRAME          StackFrames[MAX_STACK_FRAMES];
    ULONG               FrameCnt;
    ULONG               i;
    CHAR                buf[32];


    GetContext( Processor, &Context );

    FrameCnt = GetStackTrace(
        DmpHeader,
        Processor,
        StackFrames,
        MAX_STACK_FRAMES,
        Context.Eip,
        Context.Ebp,
        Context.Esp
        );

    PrintStackTrace(
        DmpHeader,
        Processor,
        StackFrames,
        FrameCnt
        );

    for (i=0; i<FrameCnt; i++) {
        if (StackFrames[i].FuncTableEntry) {
            pFpoData = (PFPO_DATA)StackFrames[i].FuncTableEntry;
            if (pFpoData->cbFrame == FRAME_TRAP) {
                sprintf( buf, "%08x", SAVE_TRAP(StackFrames[i]) );
                DoExtension( "trap",  buf, Processor, (DWORD)GetRegisterValue( &Context, REG_IP ) );
                fprintf( FileOut, "\n" );
            }
        }
    }
}

VOID
BugCheckHeuristicsX86(
    PDUMP_HEADER    DmpHeader,
    ULONG           Processor
    )
{
    STACKFRAME          StackFrames[MAX_STACK_FRAMES];
    ULONG               FrameCnt;
    PIMAGEHLP_SYMBOL    Symbol;
    ULONG               i;
    ULONG               cb;
    ULONG               Ptrs[2];
    CHAR                buf[32];
    CONTEXT             Context;


    if (DmpHeader->BugCheckCode == KMODE_EXCEPTION_NOT_HANDLED) {
        PrintHeading(
            "Dump Analysis Heuristics for Bugcode %s",
            GetBugText(DmpHeader->BugCheckCode)
            );
        fprintf(
            FileOut,
            "Exception Code:              0x%08x\n",
            DmpHeader->BugCheckParameter1
            );
        fprintf(
            FileOut,
            "Address of Exception:        0x%08x\n",
            DmpHeader->BugCheckParameter2
            );
        fprintf(
            FileOut,
            "Parameter #0:                0x%08x\n",
            DmpHeader->BugCheckParameter3
            );
        fprintf(
            FileOut,
            "Parameter #1:                0x%08x\n\n",
            DmpHeader->BugCheckParameter4
            );
        if (!SymGetSymFromName( DmpHeader, "PspUnhandledExceptionInSystemThread", sym )) {
            return;
        }
        GetContext( Processor, &Context );
        FrameCnt = GetStackTrace(
            DmpHeader,
            Processor,
            StackFrames,
            MAX_STACK_FRAMES,
            Context.Eip,
            Context.Ebp,
            Context.Esp
            );
        for (i=0; i<FrameCnt; i++) {
            if (StackFrames[i].AddrPC.Offset >= sym->Address &&
                    StackFrames[i].AddrPC.Offset < sym->Address + sym->Size) {
                break;
            }
        }
        if (i == FrameCnt) {
            return;
        }
        cb = DmpReadMemory( (PVOID)StackFrames[i].Params[0], Ptrs, sizeof(Ptrs) );
        if (cb != sizeof(Ptrs)) {
            return;
        }
        GetContext( Processor, &Context );
        sprintf( buf, "%08x", Ptrs[0] );
        DoExtension( "exr", buf, Processor, (DWORD)GetRegisterValue( &Context, REG_IP ) );
        cb = DmpReadMemory( (PVOID)Ptrs[1], &Context, sizeof(Context) );
        if (cb != sizeof(Context)) {
            return;
        }
        PrintRegisters( Processor, &Context, NULL );
        FrameCnt = GetStackTrace(
            DmpHeader,
            Processor,
            StackFrames,
            MAX_STACK_FRAMES,
            Context.Eip,
            Context.Ebp,
            Context.Esp
            );
        PrintStackTrace(
            DmpHeader,
            Processor,
            StackFrames,
            FrameCnt
            );
        DoDisassemble( Context.Eip );
    }

    if (DmpHeader->BugCheckCode == IRQL_NOT_LESS_OR_EQUAL) {
        PrintHeading(
            "Dump Analysis Heuristics for Bugcode %s",
            GetBugText(DmpHeader->BugCheckCode)
            );
        fprintf(
            FileOut,
            "Invalid Address Referenced:  0x%08x\n",
            DmpHeader->BugCheckParameter1
            );
        fprintf(
            FileOut,
            "IRQL:                        %d\n",
            DmpHeader->BugCheckParameter2
            );
        fprintf(
            FileOut,
            "Access Type:                 %s\n",
            DmpHeader->BugCheckParameter3 ? "Read" : "Write"
            );
        fprintf(
            FileOut,
            "Code Address:                0x%08x\n\n",
            DmpHeader->BugCheckParameter4
            );
        sprintf( buf, "%08x", DmpHeader->BugCheckParameter1 );
        GetContext( Processor, &Context );
        DoExtension( "pool", buf, Processor, (DWORD)GetRegisterValue( &Context, REG_IP ) );
    }
}

ULONGLONG
GetRegisterValueX86(
    PCONTEXT        Context,
    ULONG           Register
    )
{
    ULONG   Value = 0;

    switch( Register ) {
        case REG_IP:
            Value = Context->Eip;
            break;

        case REG_FP:
            Value = Context->Ebp;
            break;

        case REG_SP:
            Value = Context->Esp;
            break;
    }

    return (LONG)Value;
}


VOID
GetContextX86(
    ULONG   Processor,
    PVOID   Context
    )
{
    DmpGetContext( Processor, Context );
}



#define FLAGIOPL    1
#define FLAGVIP     2
#define FLAGVIF     3
#define FLAGOF      4
#define FLAGDF      5
#define FLAGIF      6
#define FLAGSF      7
#define FLAGZF      8
#define FLAGAF      9
#define FLAGPF      10
#define FLAGCF      11


static
ULONG
GetFlag(
    ULONG FlagsReg,
    ULONG Flag
    )
{
    switch( Flag ) {
        case FLAGIOPL:    return ((FlagsReg >> 12) & 3);
        case FLAGVIP:     return ((FlagsReg >> 20) & 1);
        case FLAGVIF:     return ((FlagsReg >> 19) & 1);
        case FLAGOF:      return ((FlagsReg >> 11) & 1);
        case FLAGDF:      return ((FlagsReg >> 10) & 1);
        case FLAGIF:      return ((FlagsReg >>  9) & 1);
        case FLAGSF:      return ((FlagsReg >>  7) & 1);
        case FLAGZF:      return ((FlagsReg >>  6) & 1);
        case FLAGAF:      return ((FlagsReg >>  4) & 1);
        case FLAGPF:      return ((FlagsReg >>  2) & 1);
        case FLAGCF:      return ((FlagsReg >>  0) & 1);
    }
    return 0;
}

VOID
PrintRegistersX86(
    ULONG   Processor
    )
{
    CONTEXT             Context;
    KSPECIAL_REGISTERS  SpecialRegContext;
    ULONG               cb;


    GetContext(
        Processor,
        &Context
        );

    DmpReadControlSpace(
        (USHORT)Processor,
        (PVOID)sizeof(CONTEXT),
        (PVOID)&SpecialRegContext,
        sizeof(KSPECIAL_REGISTERS),
        &cb
        );

    PrintRegisters( Processor, &Context, &SpecialRegContext );
}


static
VOID
PrintRegisters(
    ULONG               Processor,
    PCONTEXT            Context,
    PKSPECIAL_REGISTERS SpecialRegContext
    )
{
    PrintHeading( "Register Dump For Processor #%d", Processor );

    fprintf(
        FileOut,
        "eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n",
        Context->Eax,
        Context->Ebx,
        Context->Ecx,
        Context->Edx,
        Context->Esi,
        Context->Edi
        );

    fprintf(
        FileOut,
        "eip=%08x esp=%08x ebp=%08x ",
        Context->Eip,
        Context->Esp,
        Context->Ebp
        );

    fprintf(
        FileOut,
        "iopl=%1x %s %s %s %s %s %s %s %s %s %s\n",
        GetFlag(FLAGIOPL,Context->EFlags),
        GetFlag(FLAGVIP,Context->EFlags)   ? "vip" : "   ",
        GetFlag(FLAGVIF,Context->EFlags)   ? "vif" : "   ",
        GetFlag(FLAGOF,Context->EFlags)    ? "ov" : "nv",
        GetFlag(FLAGDF,Context->EFlags)    ? "dn" : "up",
        GetFlag(FLAGIF,Context->EFlags)    ? "ei" : "di",
        GetFlag(FLAGSF,Context->EFlags)    ? "ng" : "pl",
        GetFlag(FLAGZF,Context->EFlags)    ? "zr" : "nz",
        GetFlag(FLAGAF,Context->EFlags)    ? "ac" : "na",
        GetFlag(FLAGPF,Context->EFlags)    ? "po" : "pe",
        GetFlag(FLAGCF,Context->EFlags)    ? "cy" : "nc"
        );

    fprintf(
        FileOut,
        "cs=%04x  ss=%04x  ds=%04x  es=%04x  fs=%04x  gs=%04x             efl=%08x\n",
        Context->SegCs,
        Context->SegSs,
        Context->SegDs,
        Context->SegEs,
        Context->SegFs,
        Context->SegGs,
        Context->EFlags
        );

    if (SpecialRegContext) {
        fprintf(
            FileOut,
            "cr0=%08x cr2=%08x cr3=%08x ",
            SpecialRegContext->Cr0,
            SpecialRegContext->Cr2,
            SpecialRegContext->Cr3
            );

        fprintf(
            FileOut,
            "dr0=%08x dr1=%08x dr2=%08x\n",
            SpecialRegContext->KernelDr0,
            SpecialRegContext->KernelDr1,
            SpecialRegContext->KernelDr2
            );

        fprintf(
            FileOut,
            "dr3=%08x dr6=%08x dr7=%08x cr4=%08x\n",
            SpecialRegContext->KernelDr3,
            SpecialRegContext->KernelDr6,
            SpecialRegContext->KernelDr7,
            SpecialRegContext->Cr4
            );

        fprintf(
            FileOut,
            "gdtr=%08lx   gdtl=%04lx idtr=%08lx   idtl=%04lx tr=%04lx  ldtr=%04x\n",
            SpecialRegContext->Gdtr.Base,
            SpecialRegContext->Gdtr.Limit,
            SpecialRegContext->Idtr.Base,
            SpecialRegContext->Idtr.Limit,
            SpecialRegContext->Tr,
            SpecialRegContext->Ldtr
            );
    }

    fprintf( FileOut, "\n" );
}