forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenummem.cpp
2126 lines (1791 loc) · 82.9 KB
/
enummem.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.
//*****************************************************************************
// File: enummem.cpp
//
//
// ICLRDataEnumMemoryRegions implementation.
//
//*****************************************************************************
#include "stdafx.h"
#include <eeconfig.h>
#include <ecall.h>
#include <gcinfodecoder.h>
#include "typestring.h"
#include "daccess.h"
#include "binder.h"
#include "runtimeinfo.h"
#ifdef FEATURE_COMWRAPPERS
#include <interoplibinterface.h>
#include <interoplibabi.h>
#endif // FEATURE_COMWRAPPERS
extern HRESULT GetDacTableAddress(ICorDebugDataTarget* dataTarget, ULONG64 baseAddress, PULONG64 dacTableAddress);
#if defined(DAC_MEASURE_PERF)
uint64_t g_nTotalTime;
uint64_t g_nStackTotalTime;
uint64_t g_nReadVirtualTotalTime;
uint64_t g_nFindTotalTime;
uint64_t g_nFindHashTotalTime;
uint64_t g_nFindHits;
uint64_t g_nFindCalls;
uint64_t g_nFindFails;
uint64_t g_nStackWalk;
uint64_t g_nFindStackTotalTime;
#endif // #if defined(DAC_MEASURE_PERF)
//
// EnumMemCollectImages - collect all images of interest for heap dumps
//
// This is used primarily to save ngen images.
// This is necessary so that heap dumps contain the full native code for the
// process. Normally mini/heap dump debugging requires that the images be
// available at debug-time, (in fact, watson explicitly does not want to
// be downloading 3rd party images). Not including images is the main size
// advantage of heap dumps over full dumps. However, since ngen images are
// produced on the client, we can't always ensure that the debugger will
// have access to the exact ngen image used in the dump. Therefore, managed
// heap dumps also include full copies of all NGen images in the process.
//
// We also currently include in-memory modules (provided by a host, or loaded
// from a Byte[]).
//
HRESULT ClrDataAccess::EnumMemCollectImages()
{
SUPPORTS_DAC;
ProcessModIter modIter;
Module* modDef = NULL;
HRESULT status = S_OK;
PEAssembly *assembly;
TADDR pStartAddr = 0;
ULONG32 ulSize = 0;
ULONG32 ulSizeBlock;
TSIZE_T cbMemoryReported = m_cbMemoryReported;
//
// Collect the ngen images - Iterating through module list
//
EX_TRY
{
while ((modDef = modIter.NextModule()))
{
EX_TRY
{
ulSize = 0;
assembly = modDef->GetPEAssembly();
// We want to save any in-memory images. These show up like mapped files
// and so would not be in a heap dump by default. Technically it's not clear we
// should include them in the dump - you can often have the files available
// after-the-fact. But in-memory modules may be harder to track down at debug time
// and people may have come to rely on this - so we'll include them for now.
if (
assembly->GetPath().IsEmpty() && // is in-memory
assembly->HasLoadedPEImage()) // skip files not yet loaded or Dynamic
{
pStartAddr = PTR_TO_TADDR(assembly->GetLoadedLayout()->GetBase());
ulSize = assembly->GetLoadedLayout()->GetSize();
}
// memory are mapped in GetOsPageSize() size.
// Some memory are mapped in but some are not. You cannot
// write all in one block. So iterating through page size
//
while (ulSize > 0)
{
//
// Note that we have talked about not writing IL and Metadata to save size.
// It turns out IL was rarely mapped in.
// Metadata is needed. The RVA field is needed for it is pointed to a
// MethodHeader MethodDesc::GetILHeader. Without this RVA,
// all locals are broken. In case, you are asked about this question again.
//
ulSizeBlock = ulSize > GetOsPageSize() ? GetOsPageSize() : ulSize;
ReportMem(pStartAddr, ulSizeBlock, false);
pStartAddr += ulSizeBlock;
ulSize -= ulSizeBlock;
}
}
EX_CATCH_RETHROW_ONLY_COR_E_OPERATIONCANCELLED
}
}
EX_CATCH_RETHROW_ONLY_COR_E_OPERATIONCANCELLED
m_dumpStats.m_cbNgen = m_cbMemoryReported - cbMemoryReported;
return status;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// collecting memory for mscorwks's heap dump critical statics
// This include the stress log, config structure, and IPC block
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
HRESULT ClrDataAccess::EnumMemCLRHeapCrticalStatic(IN CLRDataEnumMemoryFlags flags)
{
SUPPORTS_DAC;
TSIZE_T cbMemoryReported = m_cbMemoryReported;
// Write out the stress log structure itself
DacEnumHostDPtrMem(g_pStressLog);
// This is pointing to a static buffer
DacEnumHostDPtrMem(g_pConfig);
// dump GC heap structures. Note that the managed heap is not dumped out.
// We are just dump the GC heap structures.
//
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( EnumWksGlobalMemoryRegions(flags); );
#ifdef FEATURE_SVR_GC
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( EnumSvrGlobalMemoryRegions(flags); );
#endif
m_dumpStats.m_cbClrHeapStatics = m_cbMemoryReported - cbMemoryReported;
return S_OK;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// collecting memory for mscorwks's statics. This is the minimal
// set of global and statics that we need to have !threads, !pe, !ClrStack
// to work.
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
HRESULT ClrDataAccess::EnumMemCLRStatic(IN CLRDataEnumMemoryFlags flags)
{
SUPPORTS_DAC;
TSIZE_T cbMemoryReported = m_cbMemoryReported;
//
// write out the static and global content that we care.
//
// The followig macro will report memory all of the dac related mscorwks static and
// global variables. But it won't report the structures that are pointed by
// global pointers.
//
#define DEFINE_DACVAR(size_type, id, var) \
ReportMem(m_dacGlobals.id, sizeof(size_type));
ULONG64 dacTableAddress;
HRESULT hr = GetDacTableAddress(m_pTarget, m_globalBase, &dacTableAddress);
if (FAILED(hr)) {
return hr;
}
// Add the dac table memory in coreclr
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED ( ReportMem((TADDR)dacTableAddress, sizeof(m_dacGlobals)); )
// Cannot use CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED
// around conditional preprocessor directives in a sane fashion.
EX_TRY
{
#include "dacvars.h"
}
EX_CATCH
{
// Catch the exception and keep going unless COR_E_OPERATIONCANCELED
// was thrown. Used generating dumps, where rethrow will cancel dump.
}
EX_END_CATCH(RethrowCancelExceptions)
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED ( ReportMem(m_dacGlobals.dac__g_pStressLog, sizeof(StressLog *)); )
EX_TRY
{
// These two static pointers are pointed to static data of byte[]
// then run constructor in place
//
ReportMem(m_dacGlobals.SystemDomain__m_pSystemDomain, sizeof(SystemDomain));
// We need IGCHeap pointer to make EEVersion work
ReportMem(m_dacGlobals.dac__g_pGCHeap, sizeof(IGCHeap *));
// see synblk.cpp, the pointer is pointed to a static byte[]
SyncBlockCache::s_pSyncBlockCache.EnumMem();
ReportMem(m_dacGlobals.dac__g_FCDynamicallyAssignedImplementations,
sizeof(TADDR)*ECall::NUM_DYNAMICALLY_ASSIGNED_FCALL_IMPLEMENTATIONS);
ReportMem(g_gcDacGlobals.GetAddr(), sizeof(GcDacVars));
PTR_WSTR entryAssemblyPath = (PTR_WSTR)g_EntryAssemblyPath;
entryAssemblyPath.EnumMem();
// Triage dumps must not include full paths (PII data). Replace entry assembly path with file name only.
if (flags == CLRDATA_ENUM_MEM_TRIAGE)
{
WCHAR* path = entryAssemblyPath;
if (path != NULL)
{
size_t pathLen = u16_strlen(path) + 1;
// Get the file name based on the last directory separator
const WCHAR* name = u16_strrchr(path, DIRECTORY_SEPARATOR_CHAR_W);
if (name != NULL)
{
name += 1;
size_t len = u16_strlen(name) + 1;
wcscpy_s(path, len, name);
// Null out the rest of the buffer
for (size_t i = len; i < pathLen; ++i)
path[i] = W('\0');
DacUpdateMemoryRegion(entryAssemblyPath.GetAddr(), pathLen, (BYTE*)path);
}
}
}
// We need all of the dac variables referenced by the GC DAC global struct.
// This struct contains pointers to pointers, so we first dereference the pointers
// to obtain the location of the variable that's reported.
#define GC_DAC_VAR(type, name) ReportMem(g_gcDacGlobals->name.GetAddr(), sizeof(type));
#include "../../gc/gcinterface.dacvars.def"
}
EX_CATCH_RETHROW_ONLY_COR_E_OPERATIONCANCELLED
#ifndef TARGET_UNIX
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_runtimeLoadedBaseAddress.EnumMem(); )
#endif // !TARGET_UNIX
// These are the structures that are pointed by global pointers and we care.
// Some may reside in heap and some may reside as a static byte array in mscorwks.dll
// That is ok. We will report them explicitly.
//
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_pConfig.EnumMem(); )
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_pPredefinedArrayTypes.EnumMem(); )
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_pObjectClass.EnumMem(); )
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_pStringClass.EnumMem(); )
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_pArrayClass.EnumMem(); )
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_pExceptionClass.EnumMem(); )
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_pThreadAbortExceptionClass.EnumMem(); )
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_pOutOfMemoryExceptionClass.EnumMem(); )
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_pStackOverflowExceptionClass.EnumMem(); )
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_pExecutionEngineExceptionClass.EnumMem(); )
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_pDelegateClass.EnumMem(); )
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_pMulticastDelegateClass.EnumMem(); )
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_pValueTypeClass.EnumMem(); )
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_pEnumClass.EnumMem(); )
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_pThreadClass.EnumMem(); )
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_pFreeObjectMethodTable.EnumMem(); )
// These two static pointers are pointed to static data of byte[]
// then run constructor in place
//
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( SystemDomain::m_pSystemDomain.EnumMem(); )
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_pDebugger.EnumMem(); )
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_pEEInterface.EnumMem(); )
if (g_pDebugInterface != nullptr)
{
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED(g_pDebugInterface.EnumMem(); )
}
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_pEEDbgInterfaceImpl.EnumMem(); )
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_CORDebuggerControlFlags.EnumMem(); )
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_CoreLib.EnumMem(); )
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_pPredefinedArrayTypes[ELEMENT_TYPE_OBJECT].EnumMemoryRegions(flags); )
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( StubManager::EnumMemoryRegions(flags); )
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_pFinalizerThread.EnumMem(); )
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_pSuspensionThread.EnumMem(); )
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_heap_type.EnumMem(); )
m_dumpStats.m_cbClrStatics = m_cbMemoryReported - cbMemoryReported;
return S_OK;
}
HRESULT ClrDataAccess::EnumMemDumpJitManagerInfo(IN CLRDataEnumMemoryFlags flags)
{
SUPPORTS_DAC;
HRESULT status = S_OK;
if (flags == CLRDATA_ENUM_MEM_HEAP2)
{
EEJitManager* managerPtr = ExecutionManager::GetEEJitManager();
managerPtr->EnumMemoryRegions(flags);
}
return status;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// This function reports memory that a heap dump need to debug CLR
// and managed code efficiently.
//
// We will write out -
// 1. mscorwks.dll's image read/write pages
// 2. IPC blocks - shared memory (needed for debugging service and perf counter)
// 3. ngen images excluding Metadata and IL for size perf
// 4. We may want to touch the code pages on the stack - to be safe....
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
HRESULT ClrDataAccess::EnumMemoryRegionsWorkerHeap(IN CLRDataEnumMemoryFlags flags)
{
SUPPORTS_DAC;
HRESULT status = S_OK;
// clear all of the previous cached memory
Flush();
// collect ngen image
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( status = EnumMemCollectImages(); );
// collect CLR static
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( status = EnumMemCLRStatic(flags); );
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( status = EnumMemCLRHeapCrticalStatic(flags); );
// Note that we do not need to flush out all of the dac instance manager's instance.
// This is because it is a heap dump here. Assembly and AppDomain objects will be reported
// by the default heap collection mechanism by dbghelp.lib
//
// Microsoft: I suspect if we have all private read-write pages the preceding statement
// would be true, but I don't think we have that guarantee here.
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( status = EnumMemDumpModuleList(flags); );
// Iterating to all threads' stacks, as we have to collect data stored inside (core)clr.dll
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( status = EnumMemDumpAllThreadsStack(flags); )
// Dump AppDomain-specific info
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( status = EnumMemDumpAppDomainInfo(flags); )
// Dump jit manager info
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( status = EnumMemDumpJitManagerInfo(flags); )
// Dump the Debugger object data needed
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED( g_pDebugger->EnumMemoryRegions(flags); )
// now dump the memory get dragged in by using DAC API implicitly.
m_dumpStats.m_cbImplicitly = m_instances.DumpAllInstances(m_enumMemCb);
// Do not let any remaining implicitly enumerated memory leak out.
Flush();
return S_OK;
} // EnumMemoryRegionsWorkerHeap
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// Helper function for skinny mini-dump
// Pass in an managed object, this function will dump the EEClass hierarchy
// and field desc of object so SOS's !DumpObj will work
//
//
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
HRESULT ClrDataAccess::DumpManagedObject(CLRDataEnumMemoryFlags flags, OBJECTREF objRef)
{
SUPPORTS_DAC;
HRESULT status = S_OK;
if (objRef == NULL)
{
return status;
}
if (*g_gcDacGlobals->gc_structures_invalid_cnt != 0)
{
// GC is in progress, don't dump this object
return S_OK;
}
EX_TRY
{
// write out the current EE class and the direct/indirect inherited EE Classes
MethodTable *pMethodTable = objRef->GetGCSafeMethodTable();
while (pMethodTable)
{
EX_TRY
{
pMethodTable->EnumMemoryRegions(flags);
StackSString s;
// This might look odd. We are not using variable s after forming it.
// That is because our DAC inspecting API is using TypeString to form
// full type name. Form the full name here is a implicit reference to needed
// memory.
//
TypeString::AppendType(s, TypeHandle(pMethodTable), TypeString::FormatNamespace|TypeString::FormatFullInst);
}
EX_CATCH_RETHROW_ONLY_COR_E_OPERATIONCANCELLED
// Walk up to parent MethodTable
pMethodTable = pMethodTable->GetParentMethodTable();
}
// now dump the content for the managed object
objRef->EnumMemoryRegions();
}
EX_CATCH_RETHROW_ONLY_COR_E_OPERATIONCANCELLED
return status;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// Helper function for skinny mini-dump
// Pass in an managed excption object, this function will dump
// the managed exception object and some of its fields, such as message, stack trace string,
// inner exception.
//
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
HRESULT ClrDataAccess::DumpManagedExcepObject(CLRDataEnumMemoryFlags flags, OBJECTREF objRef)
{
SUPPORTS_DAC;
if (objRef == NULL)
{
return S_OK;
}
if (*g_gcDacGlobals->gc_structures_invalid_cnt != 0)
{
// GC is in progress, don't dump this object
return S_OK;
}
// write out the managed object for exception. This will only write out the
// direct field value. After this, we need to visit some selected fields, such as
// exception message and stack trace field, and dump out the object referenced via
// the fields.
//
DumpManagedObject(flags, objRef);
// If this is not a pre-allocated exception type, then we'll need to dump out enough memory to ensure
// that the lookup codepath from the Module to information for the type of this Exception will
// be present. Simply dumping the managed object itself isn't enough. Sos doesn't need this.
EX_TRY
{
MethodTable * pMethodTable = objRef->GetGCSafeMethodTable();
PTR_Module pModule = pMethodTable->GetModule();
mdTypeDef exceptionTypeDef = pMethodTable->GetCl();
if (TypeFromToken(exceptionTypeDef) != mdtTypeDef)
{
_ASSERTE(!"Module should have contained a TypeDef, dump will likely be missing exception type lookup!");
}
// The lookup from the Module that contains this TypeDef:
pModule->LookupTypeDef(RidFromToken(exceptionTypeDef));
// If it's a generic class, we need to implicitly enumerate the memory needed to look it up
// and enable the calls that ICD will want to make against the TypeHandle when retrieving the
// Exception info.
TypeHandle th;
th = ClassLoader::LookupTypeDefOrRefInModule(pModule, exceptionTypeDef);
th.EnumMemoryRegions(flags);
}
EX_CATCH_RETHROW_ONLY_COR_E_OPERATIONCANCELLED
#ifdef FEATURE_MINIMETADATA_IN_TRIAGEDUMPS
// store the exception type name
EX_TRY
{
MethodTable * pMethodTable = objRef->GetGCSafeMethodTable();
StackSString s;
TypeString::AppendType(s, TypeHandle(pMethodTable), TypeString::FormatNamespace|TypeString::FormatFullInst);
DacMdCacheAddEEName(dac_cast<TADDR>(pMethodTable), s);
}
EX_CATCH_RETHROW_ONLY_COR_E_OPERATIONCANCELLED
#endif // FEATURE_MINIMETADATA_IN_TRIAGEDUMPS
EXCEPTIONREF exceptRef = (EXCEPTIONREF)objRef;
if (flags != CLRDATA_ENUM_MEM_TRIAGE)
{
// dump the exception message field
DumpManagedObject(flags, (OBJECTREF)exceptRef->GetMessage());
}
// dump the exception's stack trace field
DumpManagedStackTraceStringObject(flags, exceptRef->GetStackTraceString());
// dump the exception's remote stack trace field only if we are not generating a triage dump, or the exception type does not override
// the StackTrace getter (see Exception.InternalPreserveStackTrace to understand why)
if (flags != CLRDATA_ENUM_MEM_TRIAGE ||
!ExceptionTypeOverridesStackTraceGetter(exceptRef->GetGCSafeMethodTable()))
{
DumpManagedStackTraceStringObject(flags, exceptRef->GetRemoteStackTraceString());
}
// Dump inner exception
DumpManagedExcepObject(flags, exceptRef->GetInnerException());
// Dump the stack trace array object and its underlying type
OBJECTREF stackTraceArrayObj = exceptRef->GetStackTraceArrayObject();
// There are cases where a managed exception does not have a stack trace.
// These cases are:
// * exception was thrown by VM and no managed frames are on the thread.
// * exception thrown is a preallocated exception.
if (stackTraceArrayObj != NULL)
{
// first dump the array's element type
TypeHandle arrayTypeHandle = stackTraceArrayObj->GetTypeHandle();
TypeHandle elementTypeHandle = arrayTypeHandle.GetArrayElementTypeHandle();
elementTypeHandle.AsMethodTable()->EnumMemoryRegions(flags);
elementTypeHandle.AsMethodTable()->GetClass()->EnumMemoryRegions(flags, elementTypeHandle.AsMethodTable());
// now dump the actual stack trace array object
DumpManagedObject(flags, (OBJECTREF)stackTraceArrayObj);
}
// Dump the stack trace native structure. Unfortunately, we need to write out the
// native structure and also dump the MethodDesc that we care about!
// We need to ensure the entire _stackTrace from the Exception is enumerated and
// included in the dump. When we touch the header and each element looking for the
// MD this happens.
StackTraceArray stackTrace;
exceptRef->GetStackTrace(stackTrace);
// The stackTraceArrayObj can be either a byte[] with the actual stack trace array or an object[] where the first element is the actual stack trace array.
// In case it was the latter, we need to dump the actual stack trace array object here too.
OBJECTREF actualStackTraceArrayObj = (OBJECTREF)stackTrace.Get();
if (actualStackTraceArrayObj != stackTraceArrayObj)
{
// first dump the array's element type
TypeHandle arrayTypeHandle = actualStackTraceArrayObj->GetTypeHandle();
TypeHandle elementTypeHandle = arrayTypeHandle.GetArrayElementTypeHandle();
elementTypeHandle.AsMethodTable()->EnumMemoryRegions(flags);
elementTypeHandle.AsMethodTable()->GetClass()->EnumMemoryRegions(flags, elementTypeHandle.AsMethodTable());
// now dump the actual stack trace array object
DumpManagedObject(flags, actualStackTraceArrayObj);
}
for(size_t i = 0; i < stackTrace.Size(); i++)
{
MethodDesc* pMD = stackTrace[i].pFunc;
if (!DacHasMethodDescBeenEnumerated(pMD) && DacValidateMD(pMD))
{
pMD->EnumMemoryRegions(flags);
// The following calls are to ensure that mscordacwks!DacDbiInterfaceImpl::GetNativeCodeInfo
// will succeed for all dumps.
// Pulls in data to translate from token to MethodDesc
FindLoadedMethodRefOrDef(pMD->GetMethodTable()->GetModule(), pMD->GetMemberDef());
// Pulls in sequence points.
DebugInfoManager::EnumMemoryRegionsForMethodDebugInfo(flags, pMD);
PCODE addr = pMD->GetNativeCode();
if (addr != (PCODE)NULL)
{
EECodeInfo codeInfo(addr);
if (codeInfo.IsValid())
{
IJitManager::MethodRegionInfo methodRegionInfo = { (TADDR)NULL, 0, (TADDR)NULL, 0 };
codeInfo.GetMethodRegionInfo(&methodRegionInfo);
}
}
}
// Enumerate the code around call site to help SOS resolve the source lines
TADDR callEnd = PCODEToPINSTR(stackTrace[i].ip);
DacEnumCodeForStackwalk(callEnd);
}
return S_OK;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// Helper function for skinny mini-dump
// Pass in a string object representing a managed stack trace, this function will
// dump it and "poison" the contents with a PII-free version of the stack trace.
//
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
HRESULT ClrDataAccess::DumpManagedStackTraceStringObject(CLRDataEnumMemoryFlags flags, STRINGREF orefStackTrace)
{
SUPPORTS_DAC;
if (orefStackTrace == NULL)
{
return S_OK;
}
// dump the stack trace string object
DumpManagedObject(flags, (OBJECTREF)orefStackTrace);
if (flags == CLRDATA_ENUM_MEM_TRIAGE)
{
// StringObject::GetSString does not support DAC, use GetBuffer/GetStringLength
SString stackTrace(dac_cast<PTR_WSTR>((TADDR)orefStackTrace->GetBuffer()), orefStackTrace->GetStringLength());
StripFileInfoFromStackTrace(stackTrace);
COUNT_T traceCharCount = stackTrace.GetCount();
_ASSERTE(traceCharCount <= orefStackTrace->GetStringLength());
// fill the rest of the string with \0
WCHAR *buffer = stackTrace.OpenUnicodeBuffer(orefStackTrace->GetStringLength());
memset(buffer + traceCharCount, 0, sizeof(WCHAR) * (orefStackTrace->GetStringLength() - traceCharCount));
// replace the string
DacUpdateMemoryRegion(dac_cast<TADDR>(orefStackTrace) + StringObject::GetBufferOffset(), sizeof(WCHAR) * orefStackTrace->GetStringLength(), (BYTE *)buffer);
}
return S_OK;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// Iterating through module list and report the memory.
// Remember to call
// m_instances.DumpAllInstances(m_enumMemCb);
// when all memory enumeration are done if you call this function!
// This is because using ProcessModIter will drag in some memory implicitly.
//
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
HRESULT ClrDataAccess::EnumMemDumpModuleList(CLRDataEnumMemoryFlags flags)
{
SUPPORTS_DAC;
ProcessModIter modIter;
Module* modDef;
TADDR base;
ULONG32 length;
PEAssembly* assembly;
TSIZE_T cbMemoryReported = m_cbMemoryReported;
//
// Iterating through module list
//
// Cannot use CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED around
// conditional pre-processor directives in a sane fashion
EX_TRY
{
while ((modDef = modIter.NextModule()))
{
// We also want to dump the link from the Module back to the AppDomain,
// since the stackwalker uses it to find the AD.
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED
(
// Pass false to ensure we force enumeration of this module's references.
modDef->EnumMemoryRegions(flags, false);
);
EX_TRY
{
// To enable a debugger to check on whether a module is an NI or IL image, they need
// the DOS header, PE headers, and IMAGE_COR20_HEADER for the Flags member.
// We expose no API today to find this out.
PTR_PEAssembly pPEAssembly = modDef->GetPEAssembly();
PEImage * pILImage = pPEAssembly->GetPEImage();
// Implicitly gets the COR header.
if ((pILImage) && (pILImage->HasLoadedLayout()))
{
pILImage->GetCorHeaderFlags();
}
}
EX_CATCH_RETHROW_ONLY_COR_E_OPERATIONCANCELLED
EX_TRY
{
assembly = modDef->GetPEAssembly();
base = PTR_TO_TADDR(assembly->GetLoadedImageContents(&length));
assembly->EnumMemoryRegions(flags);
}
EX_CATCH
{
// Catch the exception and keep going unless COR_E_OPERATIONCANCELED
// was thrown. Used generating dumps, where rethrow will cancel dump.
}
EX_END_CATCH(RethrowCancelExceptions)
}
}
EX_CATCH
{
// Catch the exception and keep going unless COR_E_OPERATIONCANCELED
// was thrown. Used generating dumps, where rethrow will cancel dump.
}
EX_END_CATCH(RethrowCancelExceptions)
m_dumpStats.m_cbModuleList = m_cbMemoryReported - cbMemoryReported;
return S_OK;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// Iterate through AppDomains and report specific memory needed
// for all dumps, such as the Module lookup path.
// This is intended for MiniDumpNormal and should be kept small.
//
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
HRESULT ClrDataAccess::EnumMemDumpAppDomainInfo(CLRDataEnumMemoryFlags flags)
{
SUPPORTS_DAC;
if (flags == CLRDATA_ENUM_MEM_HEAP2)
{
SystemDomain::System()->GetLoaderAllocator()->EnumMemoryRegions(flags);
}
AppDomain* appDomain = AppDomain::GetCurrentDomain();
if (appDomain == NULL)
return S_OK;
EX_TRY
{
CATCH_ALL_EXCEPT_RETHROW_COR_E_OPERATIONCANCELLED
(
// Note that the flags being CLRDATA_ENUM_MEM_MINI prevents
// you from pulling entire files loaded into memory into the dump.
appDomain->EnumMemoryRegions(flags, true);
);
}
EX_CATCH_RETHROW_ONLY_COR_E_OPERATIONCANCELLED
return S_OK;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// Iterating through each frame to make sure
// we dump out MethodDesc, DJI etc related info
// This is a generic helper for walking stack. However, if you call
// this function, make sure to flush instance in the DAC Instance manager.
//
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
HRESULT ClrDataAccess::EnumMemWalkStackHelper(CLRDataEnumMemoryFlags flags,
IXCLRDataStackWalk *pStackWalk,
Thread * pThread)
{
SUPPORTS_DAC;
#if defined(DAC_MEASURE_PERF)
g_nStackWalk = 1;
uint64_t nStart= GetCycleCount();
#endif
HRESULT status = S_OK;
ReleaseHolder<IXCLRDataFrame> pFrame(NULL);
ReleaseHolder<IXCLRDataMethodInstance> pMethod(NULL);
ReleaseHolder<IXCLRDataMethodDefinition> pMethodDefinition(NULL);
ReleaseHolder<IXCLRDataTypeInstance> pTypeInstance(NULL);
MethodDesc * pMethodDesc = NULL;
EX_TRY
{
TADDR previousSP = 0; //start at zero; this allows first check to always succeed.
TADDR currentSP;
currentSP = dac_cast<TADDR>(pThread->GetCachedStackLimit()) + sizeof(TADDR);
// exhaust the frames using DAC api
for (; status == S_OK; )
{
bool frameHadContext = false;
status = pStackWalk->GetFrame(&pFrame);
PCODE addr = (PCODE)NULL;
if (status == S_OK && pFrame != NULL)
{
// write out the code that ip pointed to
T_CONTEXT context;
REGDISPLAY regDisp;
if ((status=pFrame->GetContext(CONTEXT_ALL, sizeof(T_CONTEXT),
NULL, (BYTE *)&context))==S_OK)
{
// Enumerate the code around the call site to help debugger stack walking heuristics
::FillRegDisplay(®Disp, &context);
addr = GetControlPC(®Disp);
TADDR callEnd = PCODEToPINSTR(addr);
DacEnumCodeForStackwalk(callEnd);
frameHadContext = true;
}
//
// There are identical stack pointer checking semantics in code:Thread::EnumMemoryRegionsWorker
// See that code for comments.
// You ***MUST*** maintain identical semantics for both checks!
//
CLRDataSimpleFrameType simpleFrameType;
CLRDataDetailedFrameType detailedFrameType;
if (SUCCEEDED(pFrame->GetFrameType(&simpleFrameType, &detailedFrameType)))
{
if (!frameHadContext)
{
_ASSERTE(!"Stack frame should always have an associated context!");
break;
}
// This is StackFrameIterator::SFITER_FRAMELESS_METHOD, initialized by Code:ClrDataStackWalk::GetFrame
// from code:ClrDataStackWalk::RawGetFrameType
if (simpleFrameType == CLRDATA_SIMPFRAME_MANAGED_METHOD)
{
currentSP = (TADDR)GetRegdisplaySP(®Disp);
if (currentSP <= previousSP)
{
_ASSERTE(!"Target stack has been corrupted, SP for current frame must be larger than previous frame.");
break;
}
if (currentSP % sizeof(TADDR) != 0)
{
_ASSERTE(!"Target stack has been corrupted, SP must be aligned.");
break;
}
if (!pThread->IsAddressInStack(currentSP))
{
_ASSERTE(!"Target stack has been corrupted, SP must in the stack range.");
break;
}
}
}
else
{
_ASSERTE(!"The stack frame should always know what type it is!");
break;
}
status = pFrame->GetMethodInstance(&pMethod);
if (status == S_OK && pMethod != NULL)
{
// managed frame
if (SUCCEEDED(pMethod->GetTypeInstance(&pTypeInstance)) &&
(pTypeInstance != NULL))
{
pTypeInstance.Clear();
}
if(SUCCEEDED(pMethod->GetDefinition(&pMethodDefinition)) &&
(pMethodDefinition != NULL))
{
pMethodDesc = ((ClrDataMethodDefinition *)pMethodDefinition.GetValue())->GetMethodDesc();
if (pMethodDesc)
{
// If this is a generic, we'll need to pull in enough extra info that
// we get decent results later when stackwalking. Note that we do not guarantee
// we'll always get an exact type for any reference type; most of the time the
// stack walk will just show System.__Canon, which is the level of support we
// guarantee for minidumps without full memory.
EX_TRY
{
if ((pMethodDesc->AcquiresInstMethodTableFromThis()) ||
(pMethodDesc->RequiresInstMethodTableArg()))
{
// MethodTable
ReleaseHolder<IXCLRDataValue> pDV(NULL);
ReleaseHolder<IXCLRDataValue> pAssociatedValue(NULL);
CLRDATA_ADDRESS address = 0;
PTR_Object pObjThis = NULL;
if (SUCCEEDED(pFrame->GetArgumentByIndex(0, &pDV, 0, NULL, NULL)) &&
SUCCEEDED(pDV->GetAssociatedValue(&pAssociatedValue)) &&
SUCCEEDED(pAssociatedValue->GetAddress(&address)))
{
// Implicitly enumerate the object itself.
TADDR addrObjThis = CLRDATA_ADDRESS_TO_TADDR(address);
pObjThis = dac_cast<PTR_Object>(addrObjThis);
}
// And now get the extra info we need for the AcquiresInstMethodTableFromThis case.
if (pMethodDesc->AcquiresInstMethodTableFromThis())
{
// When working with the 'this' case, we need to pick up the MethodTable from
// object lookup.
PTR_MethodTable pMT = NULL;
if (pObjThis != NULL)
{
pMT = pObjThis->GetMethodTable();
}
TypeHandle th;
if (pMT != NULL)
{
th = TypeHandle(pMT);
}
Instantiation classInst = pMethodDesc->GetExactClassInstantiation(th);
Instantiation methodInst = pMethodDesc->GetMethodInstantiation();
}
}
else if (pMethodDesc->RequiresInstMethodDescArg())
{
// This method has a generic type token which is required to figure out the exact instantiation
// of the method.
// We need to use the variable index of the generic type token in order to do the look up.
CLRDATA_ADDRESS address = (CLRDATA_ADDRESS)NULL;
DWORD dwExactGenericArgsTokenIndex = 0;
ReleaseHolder<IXCLRDataValue> pDV(NULL);
ReleaseHolder<IXCLRDataValue> pAssociatedValue(NULL);
ReleaseHolder<IXCLRDataFrame2> pFrame2(NULL);
if (SUCCEEDED(pFrame->QueryInterface(__uuidof(IXCLRDataFrame2), (void**)&pFrame2)) &&
SUCCEEDED(pFrame2->GetExactGenericArgsToken(&pDV)) &&
SUCCEEDED(pDV->GetAssociatedValue(&pAssociatedValue)) &&
SUCCEEDED(pAssociatedValue->GetAddress(&address)))
{
TADDR addrMD = CLRDATA_ADDRESS_TO_TADDR(address);
PTR_MethodDesc pMD = dac_cast<PTR_MethodDesc>(addrMD);
pMD->EnumMemoryRegions(flags);
}
pMethodDesc->EnumMemoryRegions(flags);
MethodTable * pCanonicalMT = pMethodDesc->GetCanonicalMethodTable();
MethodTable * pNormalMT = pMethodDesc->GetMethodTable();
pCanonicalMT->EnumMemoryRegions(flags);
pNormalMT->EnumMemoryRegions(flags);
}
}
EX_CATCH_RETHROW_ONLY_COR_E_OPERATIONCANCELLED
pMethodDesc->EnumMemoryRegions(flags);
// The following calls are to ensure that mscordacwks!DacDbiInterfaceImpl::GetNativeCodeSequencePointsAndVarInfo
// will succeed for all dumps. Local variable info usefulness is somewhat questionable
// since most dumps will be for optimized targets. However, being able to map
// back to source lines for functions on stacks is very useful and we don't
// want to allow the function to fail for all targets.
// Pulls in sequence points and local variable info
DebugInfoManager::EnumMemoryRegionsForMethodDebugInfo(flags, pMethodDesc);
#if defined(FEATURE_EH_FUNCLETS) && defined(USE_GC_INFO_DECODER)
if (addr != (PCODE)NULL)
{
EECodeInfo codeInfo(addr);
if (codeInfo.IsValid())
{
// We want IsFilterFunclet to work for anything on the stack
codeInfo.GetJitManager()->IsFilterFunclet(&codeInfo);
// The stackwalker needs GC info to find the parent 'stack pointer' or PSP
GCInfoToken gcInfoToken = codeInfo.GetGCInfoToken();
PTR_BYTE pGCInfo = dac_cast<PTR_BYTE>(gcInfoToken.Info);
if (pGCInfo != NULL)
{
GcInfoDecoder gcDecoder(gcInfoToken, DECODE_PSP_SYM, 0);
DacEnumMemoryRegion(dac_cast<TADDR>(pGCInfo), gcDecoder.GetNumBytesRead(), true);
}
}
}
#endif // FEATURE_EH_FUNCLETS && USE_GC_INFO_DECODER
}
pMethodDefinition.Clear();
}
pMethod.Clear();
}
pFrame.Clear();
}