summaryrefslogtreecommitdiff
path: root/src/gcdump/gcdumpnonx86.cpp
blob: 7343ac9771413e25c69243b9d655f5ded0bf05fe (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
/*****************************************************************************
 *                               GCDumpNonX86.cpp
 */

#include "gcdump.h"

#define LIMITED_METHOD_CONTRACT ((void)0)
#define WRAPPER_NO_CONTRACT ((void)0)

#define GCINFODECODER_NO_EE
#include "gcinfodecoder.h"
#include "gcinfodumper.h"


PCSTR GetRegName (UINT32 regnum)
{
#ifdef _TARGET_AMD64_

    switch (regnum)
    {
    case 0: return "rax";
    case 1: return "rcx";
    case 2: return "rdx";
    case 3: return "rbx";
    case 4: return "rsp";
    case 5: return "rbp";
    case 6: return "rsi";
    case 7: return "rdi";
    case 8: return "r8";
    case 9: return "r9";
    case 10: return "r10";
    case 11: return "r11";
    case 12: return "r12";
    case 13: return "r13";
    case 14: return "r14";
    case 15: return "r15";
    }
    
    
    return "???";
#elif defined(_TARGET_ARM64_)

    static CHAR szRegName[16];
    if (regnum < 29)
    {
        _snprintf_s(szRegName, _countof(szRegName), sizeof(szRegName), "X%u", regnum);
        return szRegName;
    }
    else if(regnum == 29)
    {
        return "Fp";
    }
    else if(regnum == 30)
    {
        return "Lr";
    }
    else if(regnum == 31)
    {
        return "Sp";
    }
    
    return "???";
#elif defined(_TARGET_ARM_)
    if (regnum > 128)
        return "???";

    static CHAR szRegName[16];
    _snprintf_s(szRegName, _countof(szRegName), sizeof(szRegName), "r%u", regnum);
    return szRegName;

#endif
}


/*****************************************************************************/


GCDump::GCDump(UINT32 gcInfoVer, bool encBytes, unsigned maxEncBytes, bool dumpCodeOffs)
  : gcInfoVersion(gcInfoVer), 
    fDumpEncBytes   (encBytes    ),
    cMaxEncBytes    (maxEncBytes ), 
    fDumpCodeOffsets(dumpCodeOffs)
{
}


/*****************************************************************************/


struct GcInfoDumpState
{
    UINT32 LastCodeOffset;
    BOOL fAnythingPrinted;
    BOOL fSafePoint;
    UINT32 FrameRegister;
    GCDump::printfFtn pfnPrintf;
};


BOOL InterruptibleStateChangeCallback (
            UINT32 CodeOffset,
            BOOL fInterruptible,
            PVOID pvData)
{
    GcInfoDumpState *pState = (GcInfoDumpState*)pvData;

    if (pState->fAnythingPrinted)
    {
        pState->pfnPrintf("\n");
        pState->fAnythingPrinted = FALSE;
        pState->fSafePoint = FALSE;
    }
    
    pState->pfnPrintf("%08x%s interruptible\n", CodeOffset, fInterruptible ? "" : " not");

    pState->LastCodeOffset = -1;

    return FALSE;
}

BOOL SafePointCallback (
            UINT32 CodeOffset,
            PVOID pvData)
{
    GcInfoDumpState *pState = (GcInfoDumpState*)pvData;

    if (pState->fAnythingPrinted)
    {
        pState->pfnPrintf("\n");
    }
    
    pState->pfnPrintf("%08x is a safepoint: ", CodeOffset);

    pState->LastCodeOffset = CodeOffset;
    pState->fAnythingPrinted = TRUE;
    pState->fSafePoint = TRUE;

    return FALSE;
}


VOID PrintFlags (GCDump::printfFtn pfnPrintf, GcSlotFlags Flags)
{
    if (Flags & GC_SLOT_PINNED)
        pfnPrintf("(pinned)");

    if (Flags & GC_SLOT_INTERIOR)
        pfnPrintf("(interior)");

    if (Flags & GC_SLOT_UNTRACKED)
        pfnPrintf("(untracked)");
}


BOOL RegisterStateChangeCallback (
            UINT32 CodeOffset,
            UINT32 RegisterNumber,
            GcSlotFlags Flags,
            GcSlotState NewState,
            PVOID pvData)
{
    GcInfoDumpState *pState = (GcInfoDumpState*)pvData;

    if (pState->fSafePoint && (GC_SLOT_LIVE != NewState))
    {
        // Don't print deaths for safepoints
        return FALSE;
    }

    if (pState->LastCodeOffset != CodeOffset)
    {
        if (pState->fAnythingPrinted)
            pState->pfnPrintf("\n");

        pState->pfnPrintf("%08x", CodeOffset);

        pState->LastCodeOffset = CodeOffset;
    }

    char delta = ((GC_SLOT_LIVE == NewState) ? '+' : '-');

    pState->pfnPrintf(" %c%s", delta, GetRegName(RegisterNumber));

    PrintFlags(pState->pfnPrintf, Flags);

    pState->fAnythingPrinted = TRUE;

    return FALSE;
}

    
BOOL StackSlotStateChangeCallback (
            UINT32 CodeOffset,
            GcSlotFlags flags,
            GcStackSlotBase BaseRegister,
            SSIZE_T StackOffset,
            GcSlotState NewState,
            PVOID pvData)
{
    GcInfoDumpState *pState = (GcInfoDumpState*)pvData;

    if (pState->fSafePoint && (GC_SLOT_LIVE != NewState))
    {
        // Don't print deaths for safepoints
        return FALSE;
    }

    if (pState->LastCodeOffset != CodeOffset)
    {
        if (pState->fAnythingPrinted)
            pState->pfnPrintf("\n");

        if ((CodeOffset == -2) && !pState->fAnythingPrinted)
            pState->pfnPrintf("Untracked:");
        else
            pState->pfnPrintf("%08x", CodeOffset);

        pState->LastCodeOffset = CodeOffset;
    }

    char delta = ((GC_SLOT_LIVE == NewState) ? '+' : '-');

    CHAR sign = '+';

    // the dumper's call back (in GcInfoDumper.cpp) has to "guess" the base register
    // for stack slots it usually guesses it wrong ......
    // We try to filter out at least the non-sensical combinations
    //     - negative offset relative to SP
    //     - positive offset relative to CALLER_SP

    if (StackOffset < 0)
    {
        StackOffset = -StackOffset;
        sign = '-';
#ifndef GCINFODUMPER_IS_FIXED
        if (BaseRegister == GC_SP_REL)
        {                                    // negative offset to SP????
            BaseRegister = GC_CALLER_SP_REL;
        }
#endif // !GCINFODUMPER_IS_FIXED
    }
#ifndef GCINFODUMPER_IS_FIXED
    else if (BaseRegister == GC_CALLER_SP_REL)
    {                                       // positive offset to Caller_SP????
        BaseRegister = GC_SP_REL;
    }
#endif // !GCINFODUMPER_IS_FIXED

    

    PCSTR pszBaseReg;

    switch (BaseRegister)
    {
    case GC_CALLER_SP_REL: pszBaseReg = "caller.sp";                       break;
    case GC_SP_REL:        pszBaseReg = "sp";                              break;
    case GC_FRAMEREG_REL:  pszBaseReg = GetRegName(pState->FrameRegister); break;
    default:               pszBaseReg = "???";                             break;
    }
    
    pState->pfnPrintf(" %c%s%c%x", delta, pszBaseReg, sign, StackOffset);

    PrintFlags(pState->pfnPrintf, flags);

    pState->fAnythingPrinted = TRUE;

    return FALSE;
}

    
size_t      GCDump::DumpGCTable(PTR_CBYTE      gcInfoBlock,
                                unsigned       methodSize,
                                bool           verifyGCTables)
{
    GCInfoToken gcInfoToken = { dac_cast<PTR_VOID>(gcInfoBlock), gcInfoVersion };
    GcInfoDecoder hdrdecoder(gcInfoToken,
                             (GcInfoDecoderFlags)(  DECODE_SECURITY_OBJECT
                                                  | DECODE_GS_COOKIE
                                                  | DECODE_CODE_LENGTH
                                                  | DECODE_PSP_SYM
                                                  | DECODE_VARARG
                                                  | DECODE_GENERICS_INST_CONTEXT
                                                  | DECODE_GC_LIFETIMES
                                                  | DECODE_PROLOG_LENGTH
                                                  | DECODE_RETURN_KIND),
                             0);

    if (NO_SECURITY_OBJECT != hdrdecoder.GetSecurityObjectStackSlot() ||
        NO_GENERICS_INST_CONTEXT != hdrdecoder.GetGenericsInstContextStackSlot() ||
        NO_GS_COOKIE == hdrdecoder.GetGSCookieStackSlot())
    {
        gcPrintf("Prolog size: ");
        UINT32 prologSize = hdrdecoder.GetPrologSize();
        gcPrintf("%d\n", prologSize);
    }
    
    gcPrintf("Security object: ");
    if (NO_SECURITY_OBJECT == hdrdecoder.GetSecurityObjectStackSlot())
    {
        gcPrintf("<none>\n");
    }
    else
    {
        INT32 ofs = hdrdecoder.GetSecurityObjectStackSlot();
        char sign = '+';

        if (ofs < 0)
        {
            sign = '-';
            ofs = -ofs;
        }

        gcPrintf("caller.sp%c%x\n", sign, ofs);
    }

    gcPrintf("GS cookie: ");
    if (NO_GS_COOKIE == hdrdecoder.GetGSCookieStackSlot())
    {
        gcPrintf("<none>\n");
    }
    else
    {
        INT32 ofs = hdrdecoder.GetGSCookieStackSlot();
        char sign = '+';

        if (ofs < 0)
        {
            sign = '-';
            ofs = -ofs;
        }

        gcPrintf("caller.sp%c%x\n", sign, ofs);

        UINT32 validRangeStart = hdrdecoder.GetGSCookieValidRangeStart();
        UINT32 validRangeEnd = hdrdecoder.GetGSCookieValidRangeEnd();
        gcPrintf("GS cookie valid range: [%x;%x)\n", validRangeStart, validRangeEnd);
    }

    gcPrintf("PSPSym: ");
    if (NO_PSP_SYM == hdrdecoder.GetPSPSymStackSlot())
    {
        gcPrintf("<none>\n");
    }
    else
    {
        INT32 ofs = hdrdecoder.GetPSPSymStackSlot();
        char sign = '+';

        if (ofs < 0)
        {
            sign = '-';
            ofs = -ofs;
        }

#ifdef _TARGET_AMD64_
        // The PSPSym is relative to InitialSP on X64 and CallerSP on other platforms.
        gcPrintf("initial.sp%c%x\n", sign, ofs);
#else
        gcPrintf("caller.sp%c%x\n", sign, ofs);
#endif
    }

    gcPrintf("Generics inst context: ");
    if (NO_GENERICS_INST_CONTEXT == hdrdecoder.GetGenericsInstContextStackSlot())
    {
        gcPrintf("<none>\n");
    }
    else
    {
        INT32 ofs = hdrdecoder.GetGenericsInstContextStackSlot();
        char sign = '+';

        if (ofs < 0)
        {
            sign = '-';
            ofs = -ofs;
        }

        gcPrintf("caller.sp%c%x\n", sign, ofs);
    }

    gcPrintf("PSP slot: ");
    if (NO_PSP_SYM == hdrdecoder.GetPSPSymStackSlot())
    {
        gcPrintf("<none>\n");
    }
    else
    {
        INT32 ofs = hdrdecoder.GetPSPSymStackSlot();
        char sign = '+';

        if (ofs < 0)
        {
            sign = '-';
            ofs = -ofs;
        }

        gcPrintf("caller.sp%c%x\n", sign, ofs);

    }
    
    gcPrintf("GenericInst slot: ");
    if (NO_GENERICS_INST_CONTEXT == hdrdecoder.GetGenericsInstContextStackSlot())
    {
        gcPrintf("<none>\n");
    }
    else
    {
        INT32 ofs = hdrdecoder.GetGenericsInstContextStackSlot();
        char sign = '+';

        if (ofs < 0)
        {
            sign = '-';
            ofs = -ofs;
        }

        gcPrintf("caller.sp%c%x ", sign, ofs);

        if (hdrdecoder.HasMethodDescGenericsInstContext())
             gcPrintf("(GENERIC_PARAM_CONTEXT_METHODDESC)\n");
        else if (hdrdecoder.HasMethodTableGenericsInstContext())
             gcPrintf("(GENERIC_PARAM_CONTEXT_METHODHANDLE)\n");
        else
             gcPrintf("(GENERIC_PARAM_CONTEXT_THIS)\n");
    }
    
    gcPrintf("Varargs: %u\n", hdrdecoder.GetIsVarArg());
    gcPrintf("Frame pointer: %s\n", NO_STACK_BASE_REGISTER == hdrdecoder.GetStackBaseRegister()
                                    ? "<none>"
                                    : GetRegName(hdrdecoder.GetStackBaseRegister()));

    gcPrintf("Wants Report Only Leaf: %u\n", hdrdecoder.WantsReportOnlyLeaf());
    
#ifdef FIXED_STACK_PARAMETER_SCRATCH_AREA
    gcPrintf("Size of parameter area: %x\n", hdrdecoder.GetSizeOfStackParameterArea());
#endif

    ReturnKind returnKind = hdrdecoder.GetReturnKind();
    gcPrintf("Return Kind: %s\n", ReturnKindToString(returnKind));

    UINT32 cbEncodedMethodSize = hdrdecoder.GetCodeLength();
    gcPrintf("Code size: %x\n", cbEncodedMethodSize);

    GcInfoDumper dumper(gcInfoToken);

    GcInfoDumpState state;
    state.LastCodeOffset = -1;
    state.fAnythingPrinted = FALSE;
    state.fSafePoint = FALSE;
    state.FrameRegister = hdrdecoder.GetStackBaseRegister();
    state.pfnPrintf = gcPrintf;

    GcInfoDumper::EnumerateStateChangesResults result = dumper.EnumerateStateChanges(
            &InterruptibleStateChangeCallback,
            &RegisterStateChangeCallback,
            &StackSlotStateChangeCallback,
            &SafePointCallback,
            &state);

    if (state.fAnythingPrinted)
        gcPrintf("\n");

    switch (result)
    {
    case GcInfoDumper::SUCCESS:
        // do nothing
        break;

    case GcInfoDumper::OUT_OF_MEMORY:
        gcPrintf("out of memory\n");
        break;

    case GcInfoDumper::REPORTED_REGISTER_IN_CALLERS_FRAME:
        gcPrintf("reported register in caller's frame\n");
        break;

    case GcInfoDumper::REPORTED_FRAME_POINTER:
        gcPrintf("reported frame register\n");
        break;

    case GcInfoDumper::REPORTED_INVALID_BASE_REGISTER:
        gcPrintf("reported pointer relative to wrong base register\n");
        break;

    case GcInfoDumper::REPORTED_INVALID_POINTER:
        gcPrintf("reported invalid pointer\n");
        break;

    case GcInfoDumper::DECODER_FAILED:
        gcPrintf("decoder failed\n");
        break;

    default:
        gcPrintf("invalid GC info\n");
        break;
    }

    return (result == GcInfoDumper::SUCCESS) ? dumper.GetGCInfoSize() : 0;
}


/*****************************************************************************/

void    GCDump::DumpPtrsInFrame(PTR_CBYTE   infoBlock,
                                PTR_CBYTE   codeBlock,
                                unsigned    offs,
                                bool        verifyGCTables)
{
    _ASSERTE(!"NYI");
}


#define _common_h_

#ifndef LOG
#define LOG(x) ((void)0)
#endif

#define GCINFODECODER_CONTRACT(contract)
#define GET_CALLER_SP(pREGDISPLAY) ((size_t)GetSP(pREGDISPLAY->pCallerContext))
#define VALIDATE_OBJECTREF(objref, fDeep) ((void)0)
#define VALIDATE_ROOT(isInterior, hCallBack, pObjRef) ((void)0)
#include "../vm/gcinfodecoder.cpp"
#include "../gcinfo/gcinfodumper.cpp"