-
Notifications
You must be signed in to change notification settings - Fork 350
/
util.cpp
5867 lines (5113 loc) · 171 KB
/
util.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.
// ==++==
//
//
// ==--==
#include "sos.h"
#include "disasm.h"
#include <dbghelp.h>
#include "corhdr.h"
#include "cor.h"
#include "dacprivate.h"
#include "sospriv.h"
#include "corerror.h"
#include "safemath.h"
#include <psapi.h>
#include <cordebug.h>
#include <xcordebug.h>
#include <mscoree.h>
#include <tchar.h>
#include "gcinfo.h"
#ifndef STRESS_LOG
#define STRESS_LOG
#endif // STRESS_LOG
#define STRESS_LOG_READONLY
#include "stresslog.h"
#ifdef FEATURE_PAL
#include <sys/stat.h>
#include <dlfcn.h>
#endif // !FEATURE_PAL
#include "coreclrhost.h"
#include <set>
#include <string>
#if defined(__APPLE__)
#include <mach-o/dyld.h>
#endif
const char * const CorElementTypeName[ELEMENT_TYPE_MAX]=
{
#define TYPEINFO(e,ns,c,s,g,ia,ip,if,im,gv) c,
#include "cortypeinfo.h"
#undef TYPEINFO
};
const char * const CorElementTypeNamespace[ELEMENT_TYPE_MAX]=
{
#define TYPEINFO(e,ns,c,s,g,ia,ip,if,im,gv) ns,
#include "cortypeinfo.h"
#undef TYPEINFO
};
IXCLRDataProcess *g_clrData = NULL;
ISOSDacInterface *g_sos = NULL;
ISOSDacInterface15 *g_sos15 = NULL;
#ifndef IfFailRet
#define IfFailRet(EXPR) do { Status = (EXPR); if(FAILED(Status)) { return (Status); } } while (0)
#endif
// Max number of reverted rejit versions that !dumpmd and !ip2md will print
const UINT kcMaxRevertedRejitData = 10;
const UINT kcMaxTieredVersions = 10;
#ifndef FEATURE_PAL
// ensure we always allocate on the process heap
void* __cdecl operator new(size_t size) throw()
{ return HeapAlloc(GetProcessHeap(), 0, size); }
void __cdecl operator delete(void* pObj) throw()
{ HeapFree(GetProcessHeap(), 0, pObj); }
void* __cdecl operator new[](size_t size) throw()
{ return HeapAlloc(GetProcessHeap(), 0, size); }
void __cdecl operator delete[](void* pObj) throw()
{ HeapFree(GetProcessHeap(), 0, pObj); }
/**********************************************************************\
* Routine Description: *
* *
* This function is called to get the memory address given a symbol *
* name. It handles difference in symbol name between ntsd and *
* windbg. *
* *
\**********************************************************************/
DWORD_PTR GetValueFromExpression(___in __in_z const char *const instr)
{
_ASSERTE(g_pRuntime != nullptr);
LoadRuntimeSymbols();
std::string symbol;
symbol.append(GetRuntimeModuleName());
symbol.append("!");
symbol.append(instr);
ULONG64 dwAddr;
const char* str = symbol.c_str();
char name[256];
dwAddr = 0;
HRESULT hr = g_ExtSymbols->GetOffsetByName (str, &dwAddr);
if (SUCCEEDED(hr))
return (DWORD_PTR)dwAddr;
else if (hr == S_FALSE && dwAddr)
return (DWORD_PTR)dwAddr;
strcpy_s (name, ARRAY_SIZE(name), str);
char *ptr;
if ((ptr = strstr (name, "__")) != NULL)
{
ptr[0] = ':';
ptr[1] = ':';
ptr += 2;
while ((ptr = strstr(ptr, "__")) != NULL)
{
ptr[0] = ':';
ptr[1] = ':';
ptr += 2;
}
dwAddr = 0;
hr = g_ExtSymbols->GetOffsetByName (name, &dwAddr);
if (SUCCEEDED(hr))
return (DWORD_PTR)dwAddr;
else if (hr == S_FALSE && dwAddr)
return (DWORD_PTR)dwAddr;
}
else if ((ptr = strstr (name, "::")) != NULL)
{
ptr[0] = '_';
ptr[1] = '_';
ptr += 2;
while ((ptr = strstr(ptr, "::")) != NULL)
{
ptr[0] = '_';
ptr[1] = '_';
ptr += 2;
}
dwAddr = 0;
hr = g_ExtSymbols->GetOffsetByName (name, &dwAddr);
if (SUCCEEDED(hr))
return (DWORD_PTR)dwAddr;
else if (hr == S_FALSE && dwAddr)
return (DWORD_PTR)dwAddr;
}
return 0;
}
#endif // FEATURE_PAL
void ReportOOM()
{
ExtOut("SOS Error: Out of memory\n");
}
BOOL IsDumpFile()
{
static int g_fDumpFile = -1;
if (g_fDumpFile == -1) {
ULONG Class;
ULONG Qualifier;
g_ExtControl->GetDebuggeeType(&Class,&Qualifier);
if (Qualifier >= DEBUG_DUMP_SMALL)
g_fDumpFile = 1;
else
g_fDumpFile = 0;
}
return g_fDumpFile != 0;
}
BOOL g_InMinidumpSafeMode = FALSE;
BOOL IsMiniDumpFileNODAC ()
{
#ifndef FEATURE_PAL
ULONG Class;
ULONG Qualifier;
g_ExtControl->GetDebuggeeType(&Class,&Qualifier);
if (Qualifier == DEBUG_DUMP_SMALL)
{
g_ExtControl->GetDumpFormatFlags(&Qualifier);
if ((Qualifier & DEBUG_FORMAT_USER_SMALL_FULL_MEMORY) == 0)
{
return TRUE;
}
}
#endif // FEATURE_PAL
return FALSE;
}
// We use this predicate to mean the smallest, most restrictive kind of
// minidump file. There is no heap dump, only that set of information
// gathered to make !clrstack, !threads, !help, !eeversion and !pe work.
BOOL IsMiniDumpFile ()
{
#ifndef FEATURE_PAL
// It is okay for this to be static, because although the debugger may debug multiple
// managed processes at once, I don't believe multiple dumpfiles of different
// types is a scenario to worry about.
if (IsMiniDumpFileNODAC())
{
// Beyond recognizing the dump type above, all we can rely on for this
// is a flag set by the user indicating they want a safe mode minidump
// experience. This is primarily for testing.
return g_InMinidumpSafeMode;
}
#endif // FEATURE_PAL
return FALSE;
}
ULONG DebuggeeType()
{
static ULONG Class = DEBUG_CLASS_UNINITIALIZED;
if (Class == DEBUG_CLASS_UNINITIALIZED) {
ULONG Qualifier;
g_ExtControl->GetDebuggeeType(&Class,&Qualifier);
}
return Class;
}
const WCHAR GetTargetDirectorySeparatorW()
{
if (IsWindowsTarget()) {
return W('\\');
}
else {
return W('/');
}
}
#ifndef FEATURE_PAL
// Check if a file exist
BOOL FileExist (const char *filename)
{
WIN32_FIND_DATAA FindFileData;
HANDLE handle = FindFirstFileA(filename, &FindFileData);
if (handle != INVALID_HANDLE_VALUE) {
FindClose (handle);
return TRUE;
}
else
return FALSE;
}
BOOL FileExist (const WCHAR *filename)
{
WIN32_FIND_DATAW FindFileData;
HANDLE handle = FindFirstFileW (filename, &FindFileData);
if (handle != INVALID_HANDLE_VALUE) {
FindClose (handle);
return TRUE;
}
else
return FALSE;
}
/**********************************************************************\
* Routine Description: *
* *
* This function is called to find out if a dll is bbt-ized *
* *
\**********************************************************************/
BOOL IsRetailBuild (size_t base)
{
IMAGE_DOS_HEADER DosHeader;
if (g_ExtData->ReadVirtual(TO_CDADDR(base), &DosHeader, sizeof(DosHeader), NULL) != S_OK)
return FALSE;
IMAGE_NT_HEADERS32 Header32;
if (g_ExtData->ReadVirtual(TO_CDADDR(base + DosHeader.e_lfanew), &Header32, sizeof(Header32), NULL) != S_OK)
return FALSE;
// If there is no COMHeader, this can not be managed code.
if (Header32.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress == 0)
return FALSE;
size_t debugDirAddr = base + Header32.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress;
size_t nSize = Header32.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].Size;
IMAGE_DEBUG_DIRECTORY debugDir;
size_t nbytes = 0;
while (nbytes < nSize) {
if (g_ExtData->ReadVirtual(TO_CDADDR(debugDirAddr+nbytes), &debugDir, sizeof(debugDir), NULL) != S_OK)
return FALSE;
if (debugDir.Type == 0xA) {
return TRUE;
}
nbytes += sizeof(debugDir);
}
return FALSE;
}
#endif // !FEATURE_PAL
/**********************************************************************\
* Routine Description: *
* *
* This function is called to read memory from the debugee's *
* address space. If the initial read fails, it attempts to read *
* only up to the edge of the page containing "offset". *
* *
\**********************************************************************/
BOOL SafeReadMemory (TADDR offset, PVOID lpBuffer, ULONG cb, PULONG lpcbBytesRead)
{
BOOL bRet = SUCCEEDED(g_ExtData->ReadVirtual(TO_CDADDR(offset), lpBuffer, cb, lpcbBytesRead));
if (!bRet)
{
cb = _min(cb, (ULONG)(NextOSPageAddress(offset) - offset));
bRet = SUCCEEDED(g_ExtData->ReadVirtual(TO_CDADDR(offset), lpBuffer, cb, lpcbBytesRead));
}
return bRet;
}
ULONG OSPageSize ()
{
static ULONG pageSize = 0;
if (pageSize == 0)
g_ExtControl->GetPageSize(&pageSize);
return pageSize;
}
size_t NextOSPageAddress (size_t addr)
{
size_t pageSize = OSPageSize();
return (addr+pageSize)&(~(pageSize-1));
}
/**********************************************************************\
* Routine Description: *
* *
* This function is called to get the address of MethodDesc *
* given an ip address *
* *
\**********************************************************************/
void IP2MethodDesc (DWORD_PTR IP, DWORD_PTR &methodDesc, JITTypes &jitType,
DWORD_PTR &gcinfoAddr)
{
CLRDATA_ADDRESS EIP = TO_CDADDR(IP);
DacpCodeHeaderData codeHeaderData;
methodDesc = (TADDR)0;
gcinfoAddr = (TADDR)0;
if (codeHeaderData.Request(g_sos, EIP) != S_OK)
{
return;
}
methodDesc = (DWORD_PTR) codeHeaderData.MethodDescPtr;
jitType = (JITTypes) codeHeaderData.JITType;
gcinfoAddr = (DWORD_PTR) codeHeaderData.GCInfo;
}
BOOL IsValueField (DacpFieldDescData *pFD)
{
return (pFD->Type == ELEMENT_TYPE_VALUETYPE);
}
static DWORD_PTR ResolveByRefField(DacpFieldDescData* pFD, DWORD_PTR dwAddr, CLRDATA_ADDRESS* methodTable)
{
if (dwAddr == 0)
return 0;
ToRelease<IMetaDataImport> pImport = MDImportForModule(TO_TADDR(pFD->ModuleOfType));
PCCOR_SIGNATURE pSignatureBlob = NULL;
ULONG sigBlobLength = 0;
if(FAILED(pImport->GetFieldProps(pFD->mb, NULL, NULL, 0, NULL, NULL, &pSignatureBlob, &sigBlobLength, NULL, NULL, NULL)))
return 0;
SigParser sigParser(pSignatureBlob, sigBlobLength);
sigParser.SkipExactlyOne();
// Move past and assert the ByRef
CorElementType etype;
if (FAILED(sigParser.GetElemType(&etype)))
return 0;
_ASSERTE(etype == ELEMENT_TYPE_BYREF);
// Get the byref's type. If this is also a byref we give up.
if (FAILED(sigParser.GetElemType(&etype)) || etype == ELEMENT_TYPE_BYREF)
return 0;
// If the type was determined to be a valuetype, we need the methodtable
// to be able to properly display it.
if (etype == ELEMENT_TYPE_VALUETYPE)
{
mdToken token = mdTokenNil;
if (FAILED(sigParser.GetToken(&token)))
return 0;
CLRDATA_ADDRESS methodTableMaybe = 0;
if (FAILED(g_sos->GetMethodDescFromToken(pFD->ModuleOfType, token, &methodTableMaybe)))
return 0;
*methodTable = methodTableMaybe;
}
// The byref has been confirmed and we now have a concrete type to read.
// Now get the target of the byref.
DWORD_PTR tgt;
CLRDATA_ADDRESS address = TO_CDADDR(dwAddr);
if (FAILED(g_ExtData->ReadVirtual(dwAddr, &tgt, sizeof(DWORD_PTR), NULL)))
return 0;
return tgt;
}
void DisplayDataMember (DacpFieldDescData* pFD, DWORD_PTR dwAddr, BOOL fAlign=TRUE)
{
if (dwAddr > 0)
{
// we must have called this function for a "real" (non-zero size) data type
PREFIX_ASSUME(gElementTypeInfo[pFD->Type] != 0);
DWORD_PTR dwTmp = dwAddr;
bool bVTStatic = (pFD->bIsStatic && pFD->Type == ELEMENT_TYPE_VALUETYPE);
if (gElementTypeInfo[pFD->Type] != NO_SIZE || bVTStatic)
{
union Value
{
char ch;
short Short;
DWORD_PTR ptr;
int Int;
unsigned int UInt;
__int64 Int64;
unsigned __int64 UInt64;
float Float;
double Double;
} value;
ZeroMemory(&value, sizeof(value));
if (bVTStatic)
{
// static VTypes are boxed
moveBlock (value, dwTmp, gElementTypeInfo[ELEMENT_TYPE_CLASS]);
}
else
{
moveBlock (value, dwTmp, gElementTypeInfo[pFD->Type]);
}
switch (pFD->Type)
{
case ELEMENT_TYPE_I1:
// there's no ANSI conformant type specifier for
// signed char, so use the next best thing,
// signed short (sign extending)
if (fAlign)
ExtOut("%" POINTERSIZE "hd", (short)value.ch);
else
ExtOut("%d", value.ch);
break;
case ELEMENT_TYPE_I2:
if (fAlign)
ExtOut("%" POINTERSIZE "hd", value.Short);
else
ExtOut("%d", value.Short);
break;
case ELEMENT_TYPE_I4:
if (fAlign)
ExtOut("%" POINTERSIZE "d", value.Int);
else
ExtOut("%d", value.Int);
break;
case ELEMENT_TYPE_I8:
if (fAlign)
ExtOut("%" POINTERSIZE "I64d", value.Int64);
else
ExtOut("%I64d", value.Int64);
break;
case ELEMENT_TYPE_U1:
case ELEMENT_TYPE_BOOLEAN:
if (fAlign)
// there's no ANSI conformant type specifier for
// unsigned char, so use the next best thing,
// unsigned short, not extending the sign
ExtOut("%" POINTERSIZE "hu", (USHORT)value.Short);
else
ExtOut("%u", value.ch);
break;
case ELEMENT_TYPE_U2:
if (fAlign)
ExtOut("%" POINTERSIZE "hu", value.Short);
else
ExtOut("%u", value.Short);
break;
case ELEMENT_TYPE_U4:
if (fAlign)
ExtOut("%" POINTERSIZE "u", value.UInt);
else
ExtOut("%u", value.UInt);
break;
case ELEMENT_TYPE_U8:
if (fAlign)
ExtOut("%" POINTERSIZE "I64u", value.UInt64);
else
ExtOut("%I64u", value.UInt64);
break;
case ELEMENT_TYPE_I:
case ELEMENT_TYPE_U:
if (fAlign)
ExtOut("%" POINTERSIZE "p", SOS_PTR(value.ptr));
else
ExtOut("%p", SOS_PTR(value.ptr));
break;
case ELEMENT_TYPE_R4:
ExtOut("%f", value.Float);
break;
case ELEMENT_TYPE_R8:
ExtOut("%f", value.Double);
break;
case ELEMENT_TYPE_CHAR:
if (fAlign)
ExtOut("%" POINTERSIZE "hx", value.Short);
else
ExtOut("%x", value.Short);
break;
case ELEMENT_TYPE_VALUETYPE:
if (value.ptr)
DMLOut(DMLValueClass(pFD->MTOfType, dwTmp));
else
ExtOut("%p", SOS_PTR(0));
break;
case ELEMENT_TYPE_BYREF:
{
CLRDATA_ADDRESS methodTable = 0;
DWORD_PTR tgt = ResolveByRefField(pFD, value.ptr, &methodTable);
if (tgt)
{
if (methodTable)
DMLOut(DMLByRefValueClass(value.ptr, methodTable, tgt));
else
DMLOut(DMLByRefObject(value.ptr, tgt));
}
else
{
// This will display zero if that is what the value was or the
// supplied value if ResolveByRefField() failed.
ExtOut("%p", SOS_PTR(value.ptr));
}
}
break;
default:
if (value.ptr)
DMLOut(DMLObject(value.ptr));
else
ExtOut("%p", SOS_PTR(0));
break;
}
}
else
{
if (pFD->Type == ELEMENT_TYPE_VALUETYPE)
DMLOut(DMLValueClass(pFD->MTOfType, dwTmp));
else
ExtOut("%p", SOS_PTR(0));
}
}
else
{
ExtOut("%" POINTERSIZE "s", " ");
}
}
HRESULT GetStaticFieldPTR(DWORD_PTR* pOutPtr, DacpDomainLocalModuleData* pDLMD, ISOSDacInterface14* pSOS14, CLRDATA_ADDRESS cdaMT, DacpMethodTableData* pMTD, DacpFieldDescData* pFDD, BYTE* pFlags = 0)
{
DWORD_PTR dwTmp;
CLRDATA_ADDRESS pBaseAddress = 0;
BOOL isGCStatic = pFDD->Type == ELEMENT_TYPE_VALUETYPE || pFDD->Type == ELEMENT_TYPE_CLASS;
if (pSOS14)
{
HRESULT hr = pSOS14->GetStaticBaseAddress(cdaMT, !isGCStatic ? &pBaseAddress : NULL, isGCStatic ? &pBaseAddress : NULL);
if (FAILED(hr))
{
ExtOut("GetStaticBaseAddress failed");
return hr;
}
}
else
{
if (isGCStatic)
{
pBaseAddress = pDLMD->pGCStaticDataStart;
}
else
{
pBaseAddress = pDLMD->pNonGCStaticDataStart;
}
}
dwTmp = (DWORD_PTR)pBaseAddress + pFDD->dwOffset;
*pOutPtr = 0;
if (pSOS14)
{
MethodTableInitializationFlags initFlags;
if (pFlags)
*pFlags = 0;
HRESULT hr = pSOS14->GetMethodTableInitializationFlags(cdaMT, &initFlags);
if (FAILED(hr))
{
ExtOut("GetMethodTableInitializationFlags failed");
}
else
{
if (pFlags)
*pFlags = (BYTE)initFlags; // It so happens that these two types of flags match up
}
*pOutPtr = dwTmp;
}
else
{
if (pMTD->bIsDynamic)
{
ExtOut("dynamic statics NYI");
return E_FAIL;
}
else
{
if (pFlags && pMTD->bIsShared)
{
BYTE flags;
DWORD_PTR pTargetFlags = (DWORD_PTR) pDLMD->pClassData + RidFromToken(pMTD->cl) - 1;
move_xp_retHRESULT (flags, pTargetFlags);
*pFlags = flags;
}
*pOutPtr = dwTmp;
}
}
return S_OK;
}
void GetDLMFlags(DacpDomainLocalModuleData* pDLMD, DacpMethodTableData* pMTD, BYTE* pFlags)
{
if (pMTD->bIsDynamic)
{
ExtOut("dynamic statics NYI");
return;
}
else
{
if (pFlags)
{
BYTE flags;
DWORD_PTR pTargetFlags = (DWORD_PTR) pDLMD->pClassData + RidFromToken(pMTD->cl) - 1;
move_xp (flags, pTargetFlags);
*pFlags = flags;
}
}
return;
}
HRESULT GetThreadStaticFieldPTR(DWORD_PTR* pOutPtr, CLRDATA_ADDRESS cdaThread, DacpThreadLocalModuleData* pTLMD, ISOSDacInterface14* pSOS14, CLRDATA_ADDRESS cdaMT, DacpMethodTableData* pMTD, DacpFieldDescData* pFDD, BYTE* pFlags = 0)
{
DWORD_PTR dwTmp;
CLRDATA_ADDRESS pBase = 0;
BOOL isGCStatic = pFDD->Type == ELEMENT_TYPE_VALUETYPE || pFDD->Type == ELEMENT_TYPE_CLASS;
if (pFlags)
*pFlags = 0;
if (pSOS14)
{
HRESULT hr = pSOS14->GetThreadStaticBaseAddress(cdaMT, cdaThread, !isGCStatic ? &pBase : NULL, isGCStatic ? &pBase : NULL);
if (FAILED(hr))
{
ExtOut("GetThreadStaticBaseAddress failed");
return hr;
}
if (pBase != 0)
{
if (pFlags)
*pFlags = 4; // Flag 4 indicates that the thread static data is allocated
}
}
else
{
if (isGCStatic)
{
pBase = pTLMD->pGCStaticDataStart;
}
else
{
pBase = pTLMD->pNonGCStaticDataStart;
}
}
dwTmp = (DWORD_PTR)pBase + pFDD->dwOffset;
*pOutPtr = 0;
if (pSOS14)
{
MethodTableInitializationFlags initFlags;
HRESULT hr = pSOS14->GetMethodTableInitializationFlags(cdaMT, &initFlags);
if (FAILED(hr))
{
ExtOut("GetMethodTableInitializationFlags failed");
}
else
{
*pFlags |= (BYTE)initFlags; // It so happens that these two types of flags match up
}
*pOutPtr = dwTmp;
}
else
{
if (pMTD->bIsDynamic)
{
ExtOut("dynamic thread statics NYI");
return E_FAIL;
}
else
{
if (pFlags)
{
BYTE flags;
DWORD_PTR pTargetFlags = (DWORD_PTR) pTLMD->pClassData + RidFromToken(pMTD->cl) - 1;
move_xp_retHRESULT (flags, pTargetFlags);
*pFlags = flags;
}
*pOutPtr = dwTmp;
}
}
return S_OK;
}
void DisplaySharedStatic(ULONG64 dwModuleDomainID, CLRDATA_ADDRESS cdaMT, DacpMethodTableData* pMT, DacpFieldDescData *pFD)
{
DacpAppDomainStoreData adsData;
if (adsData.Request(g_sos)!=S_OK)
{
ExtOut("Unable to get AppDomain information\n");
}
ArrayHolder<CLRDATA_ADDRESS> pArray = new CLRDATA_ADDRESS[adsData.DomainCount];
if (pArray==NULL)
{
ReportOOM();
return;
}
if (g_sos->GetAppDomainList(adsData.DomainCount,pArray, NULL)!=S_OK)
{
ExtOut("Unable to get array of AppDomains\n");
return;
}
#if defined(_TARGET_WIN64_)
ExtOut(" >> Domain:Value ");
#else
ExtOut(" >> Domain:Value ");
#endif
// Skip the SystemDomain and SharedDomain
for (int i = 0; i < adsData.DomainCount ; i ++)
{
DacpAppDomainData appdomainData;
if (appdomainData.Request(g_sos,pArray[i])!=S_OK)
{
ExtOut("Unable to get AppDomain %lx\n",pArray[i]);
return;
}
DacpDomainLocalModuleData vDomainLocalModule;
if (g_sos->GetDomainLocalModuleDataFromAppDomain(appdomainData.AppDomainPtr, (int)dwModuleDomainID, &vDomainLocalModule) != S_OK)
{
// On .NET Core, dwModuleDomainID is the address of the DomainLocalModule.
if (vDomainLocalModule.Request(g_sos, dwModuleDomainID) != S_OK)
{
DMLOut(" %s:NotInit ", DMLDomain(pArray[i]));
continue;
}
}
DWORD_PTR dwTmp;
BYTE Flags = 0;
GetStaticFieldPTR(&dwTmp, &vDomainLocalModule, NULL, cdaMT, pMT, pFD, &Flags);
if ((Flags&1) == 0) {
// We have not initialized this yet.
DMLOut(" %s:NotInit ", DMLDomain(pArray[i]));
continue;
}
else if (Flags & 2) {
// We have not initialized this yet.
DMLOut(" %s:FailInit", DMLDomain(pArray[i]));
continue;
}
DMLOut(" %s:", DMLDomain(appdomainData.AppDomainPtr));
DisplayDataMember(pFD, dwTmp, FALSE);
}
ExtOut(" <<\n");
}
void DisplayThreadStatic (DacpModuleData* pModule, CLRDATA_ADDRESS cdaMT, DacpMethodTableData* pMT, DacpFieldDescData *pFD, BOOL fIsShared)
{
SIZE_T dwModuleIndex = (SIZE_T)pModule->dwModuleIndex;
SIZE_T dwModuleDomainID = (SIZE_T)pModule->dwModuleID;
DacpThreadStoreData ThreadStore;
ThreadStore.Request(g_sos);
ExtOut(" >> Thread:Value");
CLRDATA_ADDRESS CurThread = ThreadStore.firstThread;
while (CurThread)
{
DacpThreadData vThread;
if (vThread.Request(g_sos, CurThread) != S_OK)
{
ExtOut(" error getting thread %p, aborting this field\n", SOS_PTR(CurThread));
return;
}
if (vThread.osThreadId != 0)
{
CLRDATA_ADDRESS appDomainAddr = vThread.domain;
ISOSDacInterface14 *pSOS14 = nullptr;
HRESULT hr = g_sos->QueryInterface(__uuidof(ISOSDacInterface14), reinterpret_cast<LPVOID*>(&pSOS14));
if (SUCCEEDED(hr))
{
DWORD_PTR dwTmp;
BYTE Flags = 0;
HRESULT hr = GetThreadStaticFieldPTR(&dwTmp, CurThread, NULL, pSOS14, cdaMT, pMT, pFD, &Flags);
pSOS14->Release();
if (SUCCEEDED(hr) && (Flags&4))
{
ExtOut(" %x:", vThread.osThreadId);
DisplayDataMember(pFD, dwTmp, FALSE);
}
}
else
{
// Get the DLM (we need this to check the ClassInit flags).
// It's annoying that we have to issue one request for
// domain-neutral modules and domain-specific modules.
DacpDomainLocalModuleData vDomainLocalModule;
if (fIsShared)
{
if (g_sos->GetDomainLocalModuleDataFromAppDomain(appDomainAddr, (int)dwModuleDomainID, &vDomainLocalModule) != S_OK)
{
// On .NET Core, dwModuleDomainID is the address of the DomainLocalModule.
if (vDomainLocalModule.Request(g_sos, dwModuleDomainID) != S_OK)
{
// Not initialized, go to next thread and continue looping
CurThread = vThread.nextThread;
continue;
}
}
}
else
{
if (g_sos->GetDomainLocalModuleDataFromModule(pMT->Module, &vDomainLocalModule) != S_OK)
{
// Not initialized, go to next thread
// and continue looping
CurThread = vThread.nextThread;
continue;
}
}
// Get the TLM
DacpThreadLocalModuleData vThreadLocalModule;
if (g_sos->GetThreadLocalModuleData(CurThread, (int)dwModuleIndex, &vThreadLocalModule) != S_OK)
{
// Not initialized, go to next thread
// and continue looping
CurThread = vThread.nextThread;
continue;
}
DWORD_PTR dwTmp;
BYTE Flags = 0;
GetThreadStaticFieldPTR(&dwTmp, CurThread, &vThreadLocalModule, NULL, cdaMT, pMT, pFD, &Flags);
if ((Flags&4) == 0)
{
// Not allocated, go to next thread
// and continue looping
CurThread = vThread.nextThread;
continue;
}
Flags = 0;
GetDLMFlags(&vDomainLocalModule, pMT, &Flags);
if ((Flags&1) == 0)
{
// Not initialized, go to next thread
// and continue looping
CurThread = vThread.nextThread;
continue;
}
ExtOut(" %x:", vThread.osThreadId);
DisplayDataMember(pFD, dwTmp, FALSE);
}
}
// Go to next thread
CurThread = vThread.nextThread;
}
ExtOut(" <<\n");
}
const char * ElementTypeName(unsigned type)
{
switch (type) {
case ELEMENT_TYPE_PTR:
return "PTR";
break;
case ELEMENT_TYPE_BYREF:
return "BYREF";
break;
case ELEMENT_TYPE_VALUETYPE:
return "VALUETYPE";
break;
case ELEMENT_TYPE_CLASS:
return "CLASS";
break;
case ELEMENT_TYPE_VAR:
return "VAR";
break;
case ELEMENT_TYPE_ARRAY:
return "ARRAY";
break;
case ELEMENT_TYPE_FNPTR:
return "FNPTR";
break;
case ELEMENT_TYPE_SZARRAY:
return "SZARRAY";
break;
case ELEMENT_TYPE_MVAR:
return "MVAR";
break;
default:
if ((type >= ARRAY_SIZE(CorElementTypeName)) || (CorElementTypeName[type] == NULL))
{
return "";
}
return CorElementTypeName[type];
break;
}
} // ElementTypeName
const char * ElementTypeNamespace(unsigned type)
{
if ((type >= ARRAY_SIZE(CorElementTypeName)) || (CorElementTypeNamespace[type] == NULL))
{
return "";
}
return CorElementTypeNamespace[type];
}
void ComposeName_s(CorElementType Type, __out_ecount(capacity_buffer) LPSTR buffer, size_t capacity_buffer)
{
const char *p = ElementTypeNamespace(Type);
if ((p) && (*p != '\0'))
{
strcpy_s(buffer,capacity_buffer,p);
strcat_s(buffer,capacity_buffer,".");
strcat_s(buffer,capacity_buffer,ElementTypeName(Type));
}
else
{
strcpy_s(buffer,capacity_buffer,ElementTypeName(Type));
}
}
// NOTE: pszName is changed
// INPUT MAXCHARS RETURN
// HelloThere 5 ...re
// HelloThere 8 ...There
LPWSTR FormatTypeName (__out_ecount (maxChars) LPWSTR pszName, UINT maxChars)
{
UINT iStart = 0;