-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathstrike.cpp
13746 lines (11955 loc) · 436 KB
/
strike.cpp
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// ==++==
//
//
// ==--==
// ===========================================================================
// STRIKE.CPP
// ===========================================================================
//
// History:
// 09/07/99 Microsoft Created
//
//************************************************************************************************
// SOS is the native debugging extension designed to support investigations into CLR (mis-)
// behavior by both users of the runtime as well as the code owners. It allows inspection of
// internal structures, of user visible entities, as well as execution control.
//
// This is the main SOS file hosting the implementation of all the exposed commands. A good
// starting point for understanding the semantics of these commands is the sosdocs.txt file.
//
// #CrossPlatformSOS
// SOS currently supports cross platform debugging from x86 to ARM. It takes a different approach
// from the DAC: whereas for the DAC we produce one binary for each supported host-target
// architecture pair, for SOS we produce only one binary for each host architecture; this one
// binary contains code for all supported target architectures. In doing this SOS depends on two
// assumptions:
// . that the debugger will load the appropriate DAC, and
// . that the host and target word size is identical.
// The second assumption is identical to the DAC assumption, and there will be considerable effort
// required (in the EE, the DAC, and SOS) if we ever need to remove it.
//
// In an ideal world SOS would be able to retrieve all platform specific information it needs
// either from the debugger or from DAC. However, SOS has taken some subtle and not so subtle
// dependencies on the CLR and the target platform.
// To resolve this problem, SOS now abstracts the target behind the IMachine interface, and uses
// calls on IMachine to take target-specific actions. It implements X86Machine, ARMMachine, and
// AMD64Machine. An instance of these exists in each appropriate host (e.g. the X86 version of SOS
// contains instances of X86Machine and ARMMachine, the ARM version contains an instance of
// ARMMachine, and the AMD64 version contains an instance of AMD64Machine). The code included in
// each version if determined by the SosTarget*** MSBuild symbols, and SOS_TARGET_*** conditional
// compilation symbols (as specified in sos.targets).
//
// Most of the target specific code is hosted in disasm.h/.cpp, and disasmX86.cpp, disasmARM.cpp.
// Some code currently under _TARGET_*** ifdefs may need to be reviewed/revisited.
//
// Issues:
// The one-binary-per-host decision does have some drawbacks:
// . Currently including system headers or even CLR headers will only account for the host
// target, IOW, when building the X86 version of SOS, CONTEXT will refer to the X86 CONTEXT
// structure, so we need to be careful when debugging ARM targets. The CONTEXT issue is
// partially resolved by CROSS_PLATFORM_CONTEXT (there is still a need to be very careful
// when handling arrays of CONTEXTs - see _EFN_StackTrace for details on this).
// . For larger includes (e.g. GC info), we will need to include files in specific namespaces,
// with specific _TARGET_*** macros defined in order to avoid name clashes and ensure correct
// system types are used.
// -----------------------------------------------------------------------------------------------
#define DO_NOT_DISABLE_RAND //this is a standalone tool, and can use rand()
#include <windows.h>
#include <winver.h>
#include <winternl.h>
#include <psapi.h>
#include <inttypes.h>
#ifndef FEATURE_PAL
#include <list>
#endif // !FEATURE_PAL
#include <wchar.h>
#include "platformspecific.h"
#define NOEXTAPI
#define KDEXT_64BIT
#include <wdbgexts.h>
#undef DECLARE_API
#undef StackTrace
#include <dbghelp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdexcept>
#include <deque>
#include <iostream>
#include <sstream>
#include "strike.h"
#include "sos.h"
#ifndef STRESS_LOG
#define STRESS_LOG
#endif // STRESS_LOG
#define STRESS_LOG_READONLY
#include "stresslog.h"
#include "util.h"
#include "corhdr.h"
#include "cor.h"
#include "cordebug.h"
#include "dacprivate.h"
#include "corexcep.h"
#include <dumpcommon.h>
#define CORHANDLE_MASK 0x1
#define SWITCHED_OUT_FIBER_OSID 0xbaadf00d;
#define DEFINE_EXT_GLOBALS
#include "data.h"
#include "disasm.h"
#include "predeftlsslot.h"
#include "hillclimbing.h"
#include "sos_md.h"
#ifndef FEATURE_PAL
#include "ExpressionNode.h"
#include "WatchCmd.h"
#include "tls.h"
typedef struct _VM_COUNTERS {
SIZE_T PeakVirtualSize;
SIZE_T VirtualSize;
ULONG PageFaultCount;
SIZE_T PeakWorkingSetSize;
SIZE_T WorkingSetSize;
SIZE_T QuotaPeakPagedPoolUsage;
SIZE_T QuotaPagedPoolUsage;
SIZE_T QuotaPeakNonPagedPoolUsage;
SIZE_T QuotaNonPagedPoolUsage;
SIZE_T PagefileUsage;
SIZE_T PeakPagefileUsage;
} VM_COUNTERS;
typedef VM_COUNTERS *PVM_COUNTERS;
const PROCESSINFOCLASS ProcessVmCounters = static_cast<PROCESSINFOCLASS>(3);
#endif // !FEATURE_PAL
// Max number of methods that !dumpmodule -prof will print
const UINT kcMaxMethodDescsForProfiler = 100;
#include <set>
#include <vector>
#include <map>
#include <tuple>
#include <memory>
#include <functional>
#include <algorithm>
BOOL ControlC = FALSE;
WCHAR g_mdName[mdNameLen];
#ifndef FEATURE_PAL
HMODULE g_hInstance = NULL;
#endif // !FEATURE_PAL
#ifdef _MSC_VER
#pragma warning(disable:4244) // conversion from 'unsigned int' to 'unsigned short', possible loss of data
#pragma warning(disable:4189) // local variable is initialized but not referenced
#endif
#ifdef FEATURE_PAL
#define SOSPrefix ""
#else
extern const char* g_sosPrefix;
#define SOSPrefix g_sosPrefix
#endif
#if defined _X86_ && !defined FEATURE_PAL
// disable FPO for X86 builds
#pragma optimize("y", off)
#endif
#undef assert
#ifdef _MSC_VER
#pragma warning(default:4244)
#pragma warning(default:4189)
#endif
#ifndef FEATURE_PAL
#include "ntinfo.h"
#endif // FEATURE_PAL
#ifndef IfFailRet
#define IfFailRet(EXPR) do { Status = (EXPR); if(FAILED(Status)) { return (Status); } } while (0)
#endif
#ifdef FEATURE_PAL
#define MINIDUMP_NOT_SUPPORTED()
#define ONLY_SUPPORTED_ON_WINDOWS_TARGET()
#else // !FEATURE_PAL
#define MINIDUMP_NOT_SUPPORTED() \
if (IsMiniDumpFile()) \
{ \
ExtOut("This command is not supported in a minidump without full memory\n"); \
ExtOut("To try the command anyway, run !MinidumpMode 0\n"); \
return Status; \
}
#define ONLY_SUPPORTED_ON_WINDOWS_TARGET() \
if (!IsWindowsTarget()) \
{ \
ExtOut("This command is only supported for Windows targets\n"); \
return Status; \
}
#include "safemath.h"
DECLARE_API (MinidumpMode)
{
INIT_API();
ONLY_SUPPORTED_ON_WINDOWS_TARGET();
DWORD_PTR Value=0;
CMDValue arg[] =
{ // vptr, type;
{&Value, COHEX}
};
size_t nArg;
if (!GetCMDOption(args, NULL, 0, arg, ARRAY_SIZE(arg), &nArg))
{
return E_INVALIDARG;
}
if (nArg == 0)
{
// Print status of current mode
ExtOut("Current mode: %s - unsafe minidump commands are %s.\n",
g_InMinidumpSafeMode ? "1" : "0",
g_InMinidumpSafeMode ? "disabled" : "enabled");
}
else
{
if (Value != 0 && Value != 1)
{
ExtOut("Mode must be 0 or 1\n");
return Status;
}
g_InMinidumpSafeMode = (BOOL) Value;
ExtOut("Unsafe minidump commands are %s.\n",
g_InMinidumpSafeMode ? "disabled" : "enabled");
}
return Status;
}
#endif // FEATURE_PAL
/**********************************************************************\
* Routine Description: *
* *
* This function is called to get the MethodDesc for a given eip *
* *
\**********************************************************************/
DECLARE_API(IP2MD)
{
INIT_API_PROBE_MANAGED("ip2md");
MINIDUMP_NOT_SUPPORTED();
BOOL dml = FALSE;
TADDR IP = 0;
CMDOption option[] =
{ // name, vptr, type, hasValue
{"/d", &dml, COBOOL, FALSE},
};
CMDValue arg[] =
{ // vptr, type
{&IP, COHEX},
};
size_t nArg;
if (!GetCMDOption(args, option, ARRAY_SIZE(option), arg, ARRAY_SIZE(arg), &nArg))
{
return E_INVALIDARG;
}
EnableDMLHolder dmlHolder(dml);
if (IP == 0)
{
ExtOut("%s is not IP\n", args);
return E_INVALIDARG;
}
CLRDATA_ADDRESS cdaStart = TO_CDADDR(IP);
CLRDATA_ADDRESS pMD;
if ((Status = g_sos->GetMethodDescPtrFromIP(cdaStart, &pMD)) != S_OK)
{
ExtOut("Failed to request MethodData, not in JIT code range\n");
return Status;
}
DMLOut("MethodDesc: %s\n", DMLMethodDesc(pMD));
DumpMDInfo(TO_TADDR(pMD), cdaStart, FALSE /* fStackTraceFormat */);
WCHAR filename[MAX_LONGPATH];
ULONG linenum;
// symlines will be non-zero only if SYMOPT_LOAD_LINES was set in the symbol options
ULONG symlines = 0;
if (SUCCEEDED(g_ExtSymbols->GetSymbolOptions(&symlines)))
{
symlines &= SYMOPT_LOAD_LINES;
}
if (symlines != 0 &&
SUCCEEDED(GetLineByOffset(TO_CDADDR(IP), &linenum, filename, ARRAY_SIZE(filename))))
{
ExtOut("Source file: %S @ %d\n", filename, linenum);
}
return Status;
}
// (MAX_STACK_FRAMES is also used by x86 to prevent infinite loops in _EFN_StackTrace)
#define MAX_STACK_FRAMES 1000
// I use a global set of frames for stack walking on win64 because the debugger's
// GetStackTrace function doesn't provide a way to find out the total size of a stackwalk,
// and I'd like to have a reasonably big maximum without overflowing the stack by declaring
// the buffer locally and I also want to get a managed trace in a low memory environment
// (so no dynamic allocation if possible).
DEBUG_STACK_FRAME g_Frames[MAX_STACK_FRAMES];
CROSS_PLATFORM_CONTEXT g_FrameContexts[MAX_STACK_FRAMES];
static HRESULT
GetContextStackTrace(ULONG osThreadId, PULONG pnumFrames)
{
PDEBUG_CONTROL4 debugControl4;
HRESULT hr = S_OK;
*pnumFrames = 0;
// Do we have advanced capability?
if (g_ExtControl->QueryInterface(__uuidof(IDebugControl4), (void **)&debugControl4) == S_OK)
{
ULONG oldId, id;
g_ExtSystem->GetCurrentThreadId(&oldId);
if ((hr = g_ExtSystem->GetThreadIdBySystemId(osThreadId, &id)) != S_OK) {
return hr;
}
g_ExtSystem->SetCurrentThreadId(id);
// GetContextStackTrace fills g_FrameContexts as an array of
// contexts packed as target architecture contexts. We cannot
// safely cast this as an array of CROSS_PLATFORM_CONTEXT, since
// sizeof(CROSS_PLATFORM_CONTEXT) != sizeof(TGT_CONTEXT)
hr = debugControl4->GetContextStackTrace(
NULL,
0,
g_Frames,
MAX_STACK_FRAMES,
g_FrameContexts,
MAX_STACK_FRAMES*g_targetMachine->GetContextSize(),
g_targetMachine->GetContextSize(),
pnumFrames);
g_ExtSystem->SetCurrentThreadId(oldId);
debugControl4->Release();
}
return hr;
}
/**********************************************************************\
* Routine Description: *
* *
* This function displays the stack trace. It looks at each DWORD *
* on stack. If the DWORD is a return address, the symbol name or
* managed function name is displayed. *
* *
\**********************************************************************/
void DumpStackInternal(DumpStackFlag *pDSFlag)
{
ReloadSymbolWithLineInfo();
ULONG64 StackOffset;
g_ExtRegisters->GetStackOffset (&StackOffset);
if (pDSFlag->top == 0) {
pDSFlag->top = TO_TADDR(StackOffset);
}
size_t value;
while (g_ExtData->ReadVirtual(TO_CDADDR(pDSFlag->top), &value, sizeof(size_t), NULL) != S_OK) {
if (IsInterrupt())
return;
pDSFlag->top = NextOSPageAddress(pDSFlag->top);
}
#ifndef FEATURE_PAL
if (IsWindowsTarget() && (pDSFlag->end == 0)) {
// Find the current stack range
NT_TIB teb;
ULONG64 dwTebAddr = 0;
if (SUCCEEDED(g_ExtSystem->GetCurrentThreadTeb(&dwTebAddr)))
{
if (SafeReadMemory(TO_TADDR(dwTebAddr), &teb, sizeof(NT_TIB), NULL))
{
if (pDSFlag->top > TO_TADDR(teb.StackLimit)
&& pDSFlag->top <= TO_TADDR(teb.StackBase))
{
if (pDSFlag->end == 0 || pDSFlag->end > TO_TADDR(teb.StackBase))
pDSFlag->end = TO_TADDR(teb.StackBase);
}
}
}
}
#endif // FEATURE_PAL
if (pDSFlag->end == 0)
{
ExtOut("TEB information is not available so a stack size of 0xFFFF is assumed\n");
pDSFlag->end = pDSFlag->top + 0xFFFF;
}
if (pDSFlag->end < pDSFlag->top)
{
ExtOut("Wrong option: stack selection wrong\n");
return;
}
DumpStackWorker(*pDSFlag);
}
DECLARE_API(DumpStack)
{
INIT_API_NO_RET_ON_FAILURE("dumpstack");
MINIDUMP_NOT_SUPPORTED();
DumpStackFlag DSFlag;
DSFlag.fEEonly = FALSE;
DSFlag.fSuppressSrcInfo = FALSE;
DSFlag.top = 0;
DSFlag.end = 0;
BOOL unwind = FALSE;
BOOL dml = FALSE;
CMDOption option[] = {
// name, vptr, type, hasValue
{"-EE", &DSFlag.fEEonly, COBOOL, FALSE},
{"-n", &DSFlag.fSuppressSrcInfo, COBOOL, FALSE},
{"-unwind", &unwind, COBOOL, FALSE},
{"/d", &dml, COBOOL, FALSE}
};
CMDValue arg[] = {
// vptr, type
{&DSFlag.top, COHEX},
{&DSFlag.end, COHEX}
};
size_t nArg;
if (!GetCMDOption(args, option, ARRAY_SIZE(option), arg, ARRAY_SIZE(arg), &nArg))
{
return E_INVALIDARG;
}
// symlines will be non-zero only if SYMOPT_LOAD_LINES was set in the symbol options
ULONG symlines = 0;
if (!DSFlag.fSuppressSrcInfo && SUCCEEDED(g_ExtSymbols->GetSymbolOptions(&symlines)))
{
symlines &= SYMOPT_LOAD_LINES;
}
DSFlag.fSuppressSrcInfo = DSFlag.fSuppressSrcInfo || (symlines == 0);
EnableDMLHolder enabledml(dml);
ULONG sysId = 0, id = 0;
g_ExtSystem->GetCurrentThreadSystemId(&sysId);
ExtOut("OS Thread Id: 0x%x ", sysId);
g_ExtSystem->GetCurrentThreadId(&id);
ExtOut("(%d)\n", id);
DumpStackInternal(&DSFlag);
return Status;
}
/**********************************************************************\
* Routine Description: *
* *
* This function displays the stack trace for threads that EE knows *
* from ThreadStore. *
* *
\**********************************************************************/
DECLARE_API (EEStack)
{
INIT_API();
MINIDUMP_NOT_SUPPORTED();
DumpStackFlag DSFlag;
DSFlag.fEEonly = FALSE;
DSFlag.fSuppressSrcInfo = FALSE;
DSFlag.top = 0;
DSFlag.end = 0;
BOOL bShortList = FALSE;
BOOL dml = FALSE;
CMDOption option[] =
{ // name, vptr, type, hasValue
{"-EE", &DSFlag.fEEonly, COBOOL, FALSE},
{"-short", &bShortList, COBOOL, FALSE},
{"/d", &dml, COBOOL, FALSE}
};
if (!GetCMDOption(args, option, ARRAY_SIZE(option), NULL, 0, NULL))
{
return E_INVALIDARG;
}
EnableDMLHolder enableDML(dml);
ULONG Tid;
g_ExtSystem->GetCurrentThreadId(&Tid);
DacpThreadStoreData ThreadStore;
if ((Status = ThreadStore.Request(g_sos)) != S_OK)
{
ExtOut("Failed to request ThreadStore\n");
return Status;
}
CLRDATA_ADDRESS CurThread = ThreadStore.firstThread;
while (CurThread)
{
if (IsInterrupt())
break;
DacpThreadData Thread;
if ((Status = Thread.Request(g_sos, CurThread)) != S_OK)
{
ExtOut("Failed to request Thread at %p\n", SOS_PTR(CurThread));
return Status;
}
ULONG id=0;
if (g_ExtSystem->GetThreadIdBySystemId (Thread.osThreadId, &id) != S_OK)
{
CurThread = Thread.nextThread;
continue;
}
ExtOut("---------------------------------------------\n");
ExtOut("Thread %3d\n", id);
BOOL doIt = FALSE;
#define TS_Hijacked 0x00000080
if (!bShortList)
{
doIt = TRUE;
}
else if ((Thread.lockCount > 0) || (Thread.state & TS_Hijacked))
{
// TODO: bring back || (int)vThread.m_pFrame != -1 {
doIt = TRUE;
}
else
{
ULONG64 IP;
g_ExtRegisters->GetInstructionOffset (&IP);
JITTypes jitType;
TADDR methodDesc;
TADDR gcinfoAddr;
IP2MethodDesc (TO_TADDR(IP), methodDesc, jitType, gcinfoAddr);
if (methodDesc)
{
doIt = TRUE;
}
}
if (doIt)
{
g_ExtSystem->SetCurrentThreadId(id);
DSFlag.top = 0;
DSFlag.end = 0;
DumpStackInternal(&DSFlag);
}
CurThread = Thread.nextThread;
}
g_ExtSystem->SetCurrentThreadId(Tid);
return Status;
}
/**********************************************************************\
* Routine Description: *
* *
* This function is called to dump the contents of a MethodDesc *
* for a given address *
* *
\**********************************************************************/
DECLARE_API(DumpMD)
{
INIT_API_PROBE_MANAGED("dumpmd");
MINIDUMP_NOT_SUPPORTED();
DWORD_PTR dwStartAddr = (TADDR)0;
BOOL dml = FALSE;
CMDOption option[] =
{ // name, vptr, type, hasValue
{"/d", &dml, COBOOL, FALSE},
};
CMDValue arg[] =
{ // vptr, type
{&dwStartAddr, COHEX},
};
size_t nArg;
if (!GetCMDOption(args, option, ARRAY_SIZE(option), arg, ARRAY_SIZE(arg), &nArg))
{
return E_INVALIDARG;
}
EnableDMLHolder dmlHolder(dml);
DumpMDInfo(dwStartAddr);
return Status;
}
BOOL GatherDynamicInfo(TADDR DynamicMethodObj, DacpObjectData *codeArray,
DacpObjectData *tokenArray, TADDR *ptokenArrayAddr)
{
BOOL bRet = FALSE;
int iOffset;
DacpObjectData objData; // temp object
if (codeArray == NULL || tokenArray == NULL)
return bRet;
if (objData.Request(g_sos, TO_CDADDR(DynamicMethodObj)) != S_OK)
return bRet;
iOffset = GetObjFieldOffset(TO_CDADDR(DynamicMethodObj), objData.MethodTable, W("m_resolver"));
if (iOffset <= 0)
return bRet;
TADDR resolverPtr;
if (FAILED(MOVE(resolverPtr, DynamicMethodObj + iOffset)))
return bRet;
if (objData.Request(g_sos, TO_CDADDR(resolverPtr)) != S_OK)
return bRet;
iOffset = GetObjFieldOffset(TO_CDADDR(resolverPtr), objData.MethodTable, W("m_code"));
if (iOffset <= 0)
return bRet;
TADDR codePtr;
if (FAILED(MOVE(codePtr, resolverPtr + iOffset)))
return bRet;
if (codeArray->Request(g_sos, TO_CDADDR(codePtr)) != S_OK)
return bRet;
if (codeArray->dwComponentSize != 1)
return bRet;
// We also need the resolution table
iOffset = GetObjFieldOffset (TO_CDADDR(resolverPtr), objData.MethodTable, W("m_scope"));
if (iOffset <= 0)
return bRet;
TADDR scopePtr;
if (FAILED(MOVE(scopePtr, resolverPtr + iOffset)))
return bRet;
if (objData.Request(g_sos, TO_CDADDR(scopePtr)) != S_OK)
return bRet;
iOffset = GetObjFieldOffset (TO_CDADDR(scopePtr), objData.MethodTable, W("m_tokens"));
if (iOffset <= 0)
return bRet;
TADDR tokensPtr;
if (FAILED(MOVE(tokensPtr, scopePtr + iOffset)))
return bRet;
if (objData.Request(g_sos, TO_CDADDR(tokensPtr)) != S_OK)
return bRet;
iOffset = GetObjFieldOffset(TO_CDADDR(tokensPtr), objData.MethodTable, W("_items"));
if (iOffset <= 0)
return bRet;
TADDR itemsPtr;
MOVE (itemsPtr, tokensPtr + iOffset);
*ptokenArrayAddr = itemsPtr;
if (tokenArray->Request(g_sos, TO_CDADDR(itemsPtr)) != S_OK)
return bRet;
bRet = TRUE; // whew.
return bRet;
}
typedef std::tuple<TADDR, IMetaDataImport* > GetILAddressResult;
GetILAddressResult GetILAddress(const DacpMethodDescData& MethodDescData);
/**********************************************************************\
* Routine Description: *
* *
* Displays the Microsoft intermediate language (MSIL) that is *
* associated with a managed method. *
\**********************************************************************/
DECLARE_API(DumpIL)
{
INIT_API_PROBE_MANAGED("dumpil");
MINIDUMP_NOT_SUPPORTED();
DWORD_PTR dwStartAddr = (TADDR)0;
DWORD_PTR dwDynamicMethodObj = (TADDR)0;
BOOL dml = FALSE;
BOOL fILPointerDirectlySpecified = FALSE;
CMDOption option[] =
{ // name, vptr, type, hasValue
{"-i", &fILPointerDirectlySpecified, COBOOL, FALSE},
{"/i", &fILPointerDirectlySpecified, COBOOL, FALSE},
{"/d", &dml, COBOOL, FALSE},
};
CMDValue arg[] =
{ // vptr, type
{&dwStartAddr, COHEX},
};
size_t nArg;
if (!GetCMDOption(args, option, ARRAY_SIZE(option), arg, ARRAY_SIZE(arg), &nArg))
{
return E_INVALIDARG;
}
EnableDMLHolder dmlHolder(dml);
if (dwStartAddr == (TADDR)0)
{
ExtOut("Must pass a valid expression\n");
return Status;
}
if (fILPointerDirectlySpecified)
{
return DecodeILFromAddress(NULL, dwStartAddr);
}
if (sos::IsObject(dwStartAddr))
{
dwDynamicMethodObj = dwStartAddr;
}
if (dwDynamicMethodObj == (TADDR)0)
{
// We have been given a MethodDesc
DacpMethodDescData MethodDescData;
if (MethodDescData.Request(g_sos, TO_CDADDR(dwStartAddr)) != S_OK)
{
ExtOut("%p is not a MethodDesc\n", SOS_PTR(dwStartAddr));
return Status;
}
if (MethodDescData.bIsDynamic && MethodDescData.managedDynamicMethodObject)
{
dwDynamicMethodObj = TO_TADDR(MethodDescData.managedDynamicMethodObject);
if (dwDynamicMethodObj == (TADDR)0)
{
ExtOut("Unable to print IL for DynamicMethodDesc %p\n", SOS_PTR(dwDynamicMethodObj));
return Status;
}
}
else
{
GetILAddressResult result = GetILAddress(MethodDescData);
if (std::get<0>(result) == (TADDR)0)
{
ExtOut("ilAddr is %p\n", SOS_PTR(std::get<0>(result)));
return E_FAIL;
}
ExtOut("ilAddr is %p pImport is %p\n", SOS_PTR(std::get<0>(result)), SOS_PTR(std::get<1>(result)));
TADDR ilAddr = std::get<0>(result);
ToRelease<IMetaDataImport> pImport(std::get<1>(result));
IfFailRet(DecodeILFromAddress(pImport, ilAddr));
}
}
if (dwDynamicMethodObj != (TADDR)0)
{
// We have a DynamicMethod managed object, let us visit the town and paint.
DacpObjectData codeArray;
DacpObjectData tokenArray;
DWORD_PTR tokenArrayAddr;
if (!GatherDynamicInfo (dwDynamicMethodObj, &codeArray, &tokenArray, &tokenArrayAddr))
{
DMLOut("Error gathering dynamic info from object at %s.\n", DMLObject(dwDynamicMethodObj));
return Status;
}
// Read the memory into a local buffer
BYTE *pArray = new NOTHROW BYTE[(SIZE_T)codeArray.dwNumComponents];
if (pArray == NULL)
{
ExtOut("Not enough memory to read IL\n");
return Status;
}
Status = g_ExtData->ReadVirtual(UL64_TO_CDA(codeArray.ArrayDataPtr), pArray, (ULONG)codeArray.dwNumComponents, NULL);
if (Status != S_OK)
{
ExtOut("Failed to read memory\n");
delete [] pArray;
return Status;
}
// Now we have a local copy of the IL, and a managed array for token resolution.
// Visit our IL parser with this info.
ExtOut("This is dynamic IL. Exception info is not reported at this time.\n");
ExtOut("If a token is unresolved, run \"%sdumpobj <addr>\" on the addr given\n", SOSPrefix);
ExtOut("in parenthesis. You can also look at the token table yourself, by\n");
ExtOut("running \"%sdumparray %p\".\n\n", SOSPrefix, SOS_PTR(tokenArrayAddr));
DecodeDynamicIL(pArray, (ULONG)codeArray.dwNumComponents, tokenArray);
delete [] pArray;
}
return Status;
}
void DumpSigWorker (
DWORD_PTR dwSigAddr,
DWORD_PTR dwModuleAddr,
BOOL fMethod)
{
//
// Find the length of the signature and copy it into the debugger process.
//
ULONG cbSig = 0;
const ULONG cbSigInc = 256;
ArrayHolder<COR_SIGNATURE> pSig = new NOTHROW COR_SIGNATURE[cbSigInc];
if (pSig == NULL)
{
ReportOOM();
return;
}
CQuickBytes sigString;
for (;;)
{
if (IsInterrupt())
return;
ULONG cbCopied;
if (!SafeReadMemory(TO_TADDR(dwSigAddr + cbSig), pSig + cbSig, cbSigInc, &cbCopied))
return;
cbSig += cbCopied;
sigString.ReSize(0);
GetSignatureStringResults result;
if (fMethod)
result = GetMethodSignatureString(pSig, cbSig, dwModuleAddr, &sigString);
else
result = GetSignatureString(pSig, cbSig, dwModuleAddr, &sigString);
if (GSS_ERROR == result)
return;
if (GSS_SUCCESS == result)
break;
// If we didn't get the full amount back, and we failed to parse the
// signature, it's not valid because of insufficient data
if (cbCopied < 256)
{
ExtOut("Invalid signature\n");
return;
}
#ifdef _PREFAST_
#pragma warning(push)
#pragma warning(disable:6280) // "Suppress PREFast warning about mismatch alloc/free"
#endif
PCOR_SIGNATURE pSigNew = (PCOR_SIGNATURE)realloc(pSig, cbSig+cbSigInc);
#ifdef _PREFAST_
#pragma warning(pop)
#endif
if (pSigNew == NULL)
{
ExtOut("Out of memory\n");
return;
}
pSig = pSigNew;
}
ExtOut("%S\n", (PCWSTR)sigString.Ptr());
}
/**********************************************************************\
* Routine Description: *
* *
* This function is called to dump a signature object. *
* *
\**********************************************************************/
DECLARE_API(DumpSig)
{
INIT_API();
MINIDUMP_NOT_SUPPORTED();
//
// Fetch arguments
//
StringHolder sigExpr;
StringHolder moduleExpr;
CMDValue arg[] =
{
{&sigExpr.data, COSTRING},
{&moduleExpr.data, COSTRING}
};
size_t nArg;
if (!GetCMDOption(args, NULL, 0, arg, ARRAY_SIZE(arg), &nArg))
{
return E_INVALIDARG;
}
if (nArg != 2)
{
ExtOut("%sdumpsig <sigaddr> <moduleaddr>\n", SOSPrefix);
return E_INVALIDARG;
}
DWORD_PTR dwSigAddr = GetExpression(sigExpr.data);
DWORD_PTR dwModuleAddr = GetExpression(moduleExpr.data);
if (dwSigAddr == 0 || dwModuleAddr == 0)
{
ExtOut("Invalid parameters %s %s\n", sigExpr.data, moduleExpr.data);
return Status;
}
DumpSigWorker(dwSigAddr, dwModuleAddr, TRUE);
return Status;
}
/**********************************************************************\
* Routine Description: *
* *
* This function is called to dump a portion of a signature object. *
* *
\**********************************************************************/
DECLARE_API(DumpSigElem)
{
INIT_API();
MINIDUMP_NOT_SUPPORTED();
//
// Fetch arguments
//
StringHolder sigExpr;
StringHolder moduleExpr;
CMDValue arg[] =
{
{&sigExpr.data, COSTRING},
{&moduleExpr.data, COSTRING}
};
size_t nArg;
if (!GetCMDOption(args, NULL, 0, arg, ARRAY_SIZE(arg), &nArg))
{
return E_INVALIDARG;
}
if (nArg != 2)
{
ExtOut("%sdumpsigelem <sigaddr> <moduleaddr>\n", SOSPrefix);
return E_INVALIDARG;
}
DWORD_PTR dwSigAddr = GetExpression(sigExpr.data);
DWORD_PTR dwModuleAddr = GetExpression(moduleExpr.data);