-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
dacimpl.h
3898 lines (3185 loc) · 139 KB
/
dacimpl.h
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: dacimpl.h
//
//
// Central header file for external data access implementation.
//
//*****************************************************************************
#ifndef __DACIMPL_H__
#define __DACIMPL_H__
#include "gcinterface.dac.h"
//---------------------------------------------------------------------------------------
// Setting DAC_HASHTABLE tells the DAC to use the hand rolled hashtable for
// storing code:DAC_INSTANCE . Otherwise, the DAC uses the STL unordered_map to.
#define DAC_HASHTABLE
#ifndef DAC_HASHTABLE
#pragma push_macro("return")
#undef return
#include <unordered_map>
#pragma pop_macro("return")
#endif //DAC_HASHTABLE
extern CRITICAL_SECTION g_dacCritSec;
// Convert between CLRDATA_ADDRESS and TADDR.
// Note that CLRDATA_ADDRESS is sign-extended (for compat with Windbg and OS conventions). Converting
// from pointer-size values to CLRDATA_ADDRESS should ALWAYS use this TO_CDADDR macro to avoid bugs when
// dealing with 3/4GB 32-bit address spaces. You must not rely on the compiler's implicit conversion
// from ULONG32 to ULONG64 - it is incorrect. Ideally we'd use some compiler tricks or static analysis
// to help detect such errors (they are nefarious since 3/4GB addresses aren't well tested) .
//
// Note: We're in the process of switching the implementation over to CORDB_ADDRESS instead, which is also
// 64 bits, but 0-extended. This means that conversions between TADDR and CORDB_ADDRESS are simple and natural,
// but as long as we have some legacy code, conversions involving CLRDATA_ADDRESS are a pain. Eventually we
// should eliminate CLRDATA_ADDRESS entirely from the implementation, but that will require moving SOS off of
// the old DAC stuff etc.
//
// Here are the possible conversions:
// TADDR -> CLRDATA_ADDRESS: TO_CDADDR
// CORDB_ADDRESS -> CLRDATA_ADDRESS: TO_CDADDR
// CLRDATA_ADDRESS -> TADDR: CLRDATA_ADDRESS_TO_TADDR
// CORDB_ADDRESS -> TADDR: CORDB_ADDRESS_TO_TADDR
// TADDR -> CORDB_ADDRESS: implicit
// CLRDATA_ADDRESS -> CORDB_ADDRESS: CLRDATA_ADDRESS_TO_TADDR
//
#define TO_CDADDR(taddr) ((CLRDATA_ADDRESS)(LONG_PTR)(taddr))
// Convert a CLRDATA_ADDRESS (64-bit unsigned sign-extended target address) to a TADDR
inline TADDR CLRDATA_ADDRESS_TO_TADDR(CLRDATA_ADDRESS cdAddr)
{
SUPPORTS_DAC;
#ifndef HOST_64BIT
static_assert_no_msg(sizeof(TADDR)==sizeof(UINT));
INT64 iSignedAddr = (INT64)cdAddr;
if (iSignedAddr > INT_MAX || iSignedAddr < INT_MIN)
{
_ASSERTE_MSG(false, "CLRDATA_ADDRESS out of range for this platform");
DacError(E_INVALIDARG);
}
#endif
return (TADDR)cdAddr;
}
// No throw, Convert a CLRDATA_ADDRESS (64-bit unsigned sign-extended target address) to a TADDR
// Use this in places where we know windbg may pass in bad addresses
inline HRESULT TRY_CLRDATA_ADDRESS_TO_TADDR(CLRDATA_ADDRESS cdAddr, TADDR* pOutTaddr)
{
SUPPORTS_DAC;
#ifndef HOST_64BIT
static_assert_no_msg(sizeof(TADDR)==sizeof(UINT));
INT64 iSignedAddr = (INT64)cdAddr;
if (iSignedAddr > INT_MAX || iSignedAddr < INT_MIN)
{
*pOutTaddr = 0;
return E_INVALIDARG;
}
#endif
*pOutTaddr = (TADDR)cdAddr;
return S_OK;
}
// Convert a CORDB_ADDRESS (64-bit unsigned 0-extended target address) to a TADDR
inline TADDR CORDB_ADDRESS_TO_TADDR(CORDB_ADDRESS cdbAddr)
{
SUPPORTS_DAC;
#ifndef HOST_64BIT
static_assert_no_msg(sizeof(TADDR)==sizeof(UINT));
if (cdbAddr > UINT_MAX)
{
_ASSERTE_MSG(false, "CORDB_ADDRESS out of range for this platform");
DacError(E_INVALIDARG);
}
#endif
return (TADDR)cdbAddr;
}
// TO_TADDR is the old way of converting CLRDATA_ADDRESSes to TADDRs. Unfortunately,
// this has been used in many places to also cast pointers (void* etc.) to TADDR, and
// so we can't actually require the argument to be a valid CLRDATA_ADDRESS. New code
// should use CLRDATA_ADDRESS_TO_TADDR instead.
#define TO_TADDR(cdaddr) ((TADDR)(cdaddr))
#define TO_CDENUM(ptr) ((CLRDATA_ENUM)(ULONG_PTR)(ptr))
#define FROM_CDENUM(type, cdenum) ((type*)(ULONG_PTR)(cdenum))
#define SIMPFRAME_ALL \
(CLRDATA_SIMPFRAME_UNRECOGNIZED | \
CLRDATA_SIMPFRAME_MANAGED_METHOD | \
CLRDATA_SIMPFRAME_RUNTIME_MANAGED_CODE | \
CLRDATA_SIMPFRAME_RUNTIME_UNMANAGED_CODE)
enum DAC_USAGE_TYPE
{
DAC_DPTR,
DAC_VPTR,
DAC_STRA,
DAC_STRW,
DAC_PAL,
};
class ReflectionModule;
struct DAC_MD_IMPORT
{
DAC_MD_IMPORT* next; // list link field
TADDR peFile; // a TADDR for a PEAssembly* or a ReflectionModule*
IMDInternalImport* impl; // Associated metadata interface
bool isAlternate; // for NGEN images set to true if the metadata corresponds to the IL image
DAC_MD_IMPORT(TADDR peFile_,
IMDInternalImport* impl_,
bool isAlt_ = false,
DAC_MD_IMPORT* next_ = NULL)
: next(next_)
, peFile(peFile_)
, impl(impl_)
, isAlternate(isAlt_)
{
SUPPORTS_DAC_HOST_ONLY;
}
};
// This class maintains a cache of IMDInternalImport* and their corresponding
// source (a PEAssembly* or a ReflectionModule*), as a singly-linked list of
// DAC_MD_IMPORT nodes. The cache is flushed whenever the process state changes
// by calling its Flush() member function.
class MDImportsCache
{
public:
MDImportsCache()
: m_head(NULL)
{}
~MDImportsCache()
{
Flush();
}
FORCEINLINE
IMDInternalImport* Get(TADDR key) const
{
SUPPORTS_DAC;
for (DAC_MD_IMPORT* importList = m_head; importList; importList = importList->next)
{
if (importList->peFile == key)
{
return importList->impl;
}
}
return NULL;
}
FORCEINLINE
DAC_MD_IMPORT* Add(TADDR peFile, IMDInternalImport* impl, bool isAlt)
{
SUPPORTS_DAC;
DAC_MD_IMPORT* importList = new (nothrow) DAC_MD_IMPORT(peFile, impl, isAlt, m_head);
if (!importList)
{
return NULL;
}
m_head = importList;
return importList;
}
void Flush()
{
DAC_MD_IMPORT* importList;
while (m_head)
{
importList = m_head;
m_head = importList->next;
importList->impl->Release();
delete importList;
}
}
private:
DAC_MD_IMPORT* m_head; // the beginning of the list of cached MD imports
};
struct METH_EXTENTS
{
ULONG32 numExtents;
ULONG32 curExtent;
// Currently only one is needed.
CLRDATA_ADDRESS_RANGE extents[1];
};
HRESULT ConvertUtf8(_In_ LPCUTF8 utf8,
ULONG32 bufLen,
ULONG32* nameLen,
_Out_writes_to_opt_(bufLen, *nameLen) PWSTR buffer);
HRESULT AllocUtf8(_In_opt_ LPCWSTR wstr,
ULONG32 srcChars,
_Outptr_ LPUTF8* utf8);
HRESULT GetFullClassNameFromMetadata(IMDInternalImport* mdImport,
mdTypeDef classToken,
ULONG32 bufferChars,
_Inout_updates_(bufferChars) LPUTF8 buffer);
HRESULT GetFullMethodNameFromMetadata(IMDInternalImport* mdImport,
mdMethodDef methodToken,
ULONG32 bufferChars,
_Inout_updates_(bufferChars) LPUTF8 buffer);
enum SplitSyntax
{
SPLIT_METHOD,
SPLIT_TYPE,
SPLIT_FIELD,
SPLIT_NO_NAME,
};
HRESULT SplitFullName(_In_z_ PCWSTR fullName,
SplitSyntax syntax,
ULONG32 memberDots,
_Outptr_opt_ LPUTF8* namespaceName,
_Outptr_opt_ LPUTF8* typeName,
_Outptr_opt_ LPUTF8* memberName,
_Outptr_opt_ LPUTF8* params);
int CompareUtf8(_In_ LPCUTF8 str1, _In_ LPCUTF8 str2, _In_ ULONG32 nameFlags);
#define INH_STATIC \
(CLRDATA_VALUE_ALL_KINDS | \
CLRDATA_VALUE_IS_INHERITED | CLRDATA_VALUE_FROM_STATIC)
HRESULT InitFieldIter(DeepFieldDescIterator* fieldIter,
TypeHandle typeHandle,
bool canHaveFields,
ULONG32 flags,
IXCLRDataTypeInstance* fromType);
ULONG32 GetTypeFieldValueFlags(TypeHandle typeHandle,
FieldDesc* fieldDesc,
ULONG32 otherFlags,
bool isDeref);
//----------------------------------------------------------------------------
//
// MetaEnum.
//
//----------------------------------------------------------------------------
class MetaEnum
{
public:
MetaEnum(void)
{
Clear();
m_appDomain = NULL;
}
~MetaEnum(void)
{
End();
}
void Clear(void)
{
m_mdImport = NULL;
m_kind = 0;
m_lastToken = mdTokenNil;
}
HRESULT Start(IMDInternalImport* mdImport, ULONG32 kind,
mdToken container);
void End(void);
HRESULT NextToken(mdToken* token,
_Outptr_opt_result_maybenull_ LPCUTF8* namespaceName,
_Outptr_opt_result_maybenull_ LPCUTF8* name);
HRESULT NextDomainToken(AppDomain** appDomain,
mdToken* token);
HRESULT NextTokenByName(_In_opt_ LPCUTF8 namespaceName,
_In_opt_ LPCUTF8 name,
ULONG32 nameFlags,
mdToken* token);
HRESULT NextDomainTokenByName(_In_opt_ LPCUTF8 namespaceName,
_In_opt_ LPCUTF8 name,
ULONG32 nameFlags,
AppDomain** appDomain, mdToken* token);
static HRESULT CdNextToken(CLRDATA_ENUM* handle,
mdToken* token)
{
MetaEnum* iter = FROM_CDENUM(MetaEnum, *handle);
if (!iter)
{
return S_FALSE;
}
return iter->NextToken(token, NULL, NULL);
}
static HRESULT CdNextDomainToken(CLRDATA_ENUM* handle,
AppDomain** appDomain,
mdToken* token)
{
MetaEnum* iter = FROM_CDENUM(MetaEnum, *handle);
if (!iter)
{
return S_FALSE;
}
return iter->NextDomainToken(appDomain, token);
}
static HRESULT CdEnd(CLRDATA_ENUM handle)
{
MetaEnum* iter = FROM_CDENUM(MetaEnum, handle);
if (iter)
{
delete iter;
return S_OK;
}
else
{
return E_INVALIDARG;
}
}
IMDInternalImport* m_mdImport;
ULONG32 m_kind;
HENUMInternal m_enum;
AppDomain* m_appDomain;
mdToken m_lastToken;
static HRESULT New(Module* mod,
ULONG32 kind,
mdToken container,
IXCLRDataAppDomain* pubAppDomain,
MetaEnum** metaEnum,
CLRDATA_ENUM* handle);
};
//----------------------------------------------------------------------------
//
// SplitName.
//
//----------------------------------------------------------------------------
class SplitName
{
public:
// Type of name and splitting being done in this instance.
SplitSyntax m_syntax;
ULONG32 m_nameFlags;
ULONG32 m_memberDots;
// Split fields.
LPUTF8 m_namespaceName;
LPUTF8 m_typeName;
mdTypeDef m_typeToken;
LPUTF8 m_memberName;
mdMethodDef m_memberToken;
LPUTF8 m_params;
// XXX Microsoft - Translated signature.
// Arbitrary extra data.
Thread* m_tlsThread;
Module* m_module;
MetaEnum m_metaEnum;
DeepFieldDescIterator m_fieldEnum;
ULONG64 m_objBase;
FieldDesc* m_lastField;
SplitName(SplitSyntax syntax, ULONG32 nameFlags,
ULONG32 memberDots);
~SplitName(void)
{
Delete();
}
void Delete(void);
void Clear(void);
HRESULT SplitString(_In_opt_ PCWSTR fullName);
bool FindType(IMDInternalImport* mdInternal);
bool FindMethod(IMDInternalImport* mdInternal);
bool FindField(IMDInternalImport* mdInternal);
int Compare(LPCUTF8 str1, LPCUTF8 str2)
{
return CompareUtf8(str1, str2, m_nameFlags);
}
static HRESULT AllocAndSplitString(_In_opt_ PCWSTR fullName,
SplitSyntax syntax,
ULONG32 nameFlags,
ULONG32 memberDots,
SplitName** split);
static HRESULT CdStartMethod(_In_opt_ PCWSTR fullName,
ULONG32 nameFlags,
Module* mod,
mdTypeDef typeToken,
AppDomain* appDomain,
IXCLRDataAppDomain* pubAppDomain,
SplitName** split,
CLRDATA_ENUM* handle);
static HRESULT CdNextMethod(CLRDATA_ENUM* handle,
mdMethodDef* token);
static HRESULT CdNextDomainMethod(CLRDATA_ENUM* handle,
AppDomain** appDomain,
mdMethodDef* token);
static HRESULT CdStartField(_In_opt_ PCWSTR fullName,
ULONG32 nameFlags,
ULONG32 fieldFlags,
IXCLRDataTypeInstance* fromTypeInst,
TypeHandle typeHandle,
Module* mod,
mdTypeDef typeToken,
ULONG64 objBase,
Thread* tlsThread,
IXCLRDataTask* pubTlsThread,
AppDomain* appDomain,
IXCLRDataAppDomain* pubAppDomain,
SplitName** split,
CLRDATA_ENUM* handle);
static HRESULT CdNextField(ClrDataAccess* dac,
CLRDATA_ENUM* handle,
IXCLRDataTypeDefinition** fieldType,
ULONG32* fieldFlags,
IXCLRDataValue** value,
ULONG32 nameBufRetLen,
ULONG32* nameLenRet,
_Out_writes_to_opt_(nameBufRetLen, *nameLenRet) WCHAR nameBufRet[ ],
IXCLRDataModule** tokenScopeRet,
mdFieldDef* tokenRet);
static HRESULT CdNextDomainField(ClrDataAccess* dac,
CLRDATA_ENUM* handle,
IXCLRDataValue** value);
static HRESULT CdStartType(_In_opt_ PCWSTR fullName,
ULONG32 nameFlags,
Module* mod,
AppDomain* appDomain,
IXCLRDataAppDomain* pubAppDomain,
SplitName** split,
CLRDATA_ENUM* handle);
static HRESULT CdNextType(CLRDATA_ENUM* handle,
mdTypeDef* token);
static HRESULT CdNextDomainType(CLRDATA_ENUM* handle,
AppDomain** appDomain,
mdTypeDef* token);
static HRESULT CdEnd(CLRDATA_ENUM handle)
{
SplitName* split = FROM_CDENUM(SplitName, handle);
if (split)
{
delete split;
return S_OK;
}
else
{
return E_INVALIDARG;
}
}
};
//----------------------------------------------------------------------------
//
// ProcessModIter.
//
//----------------------------------------------------------------------------
struct ProcessModIter
{
bool m_nextDomain;
AppDomain::AssemblyIterator m_assemIter;
Assembly* m_curAssem;
ProcessModIter(void)
{
SUPPORTS_DAC;
m_nextDomain = true;
m_curAssem = NULL;
}
Assembly * NextAssem()
{
SUPPORTS_DAC;
if (m_nextDomain)
{
m_nextDomain = false;
if (AppDomain::GetCurrentDomain() == nullptr)
{
return NULL;
}
m_assemIter = AppDomain::GetCurrentDomain()->IterateAssembliesEx((AssemblyIterationFlags)(
kIncludeLoaded | kIncludeExecution));
}
CollectibleAssemblyHolder<DomainAssembly *> pDomainAssembly;
if (!m_assemIter.Next(pDomainAssembly.This()))
{
return NULL;
}
// Note: DAC doesn't need to keep the assembly alive - see code:CollectibleAssemblyHolder#CAH_DAC
CollectibleAssemblyHolder<Assembly *> pAssembly = pDomainAssembly->GetAssembly();
return pAssembly;
}
Module* NextModule(void)
{
SUPPORTS_DAC;
m_curAssem = NextAssem();
if (!m_curAssem)
{
return NULL;
}
return m_curAssem->GetModule();
}
};
//----------------------------------------------------------------------------
//
// DacInstanceManager.
//
//----------------------------------------------------------------------------
// The data for an access may have special alignment needs and
// the cache must provide similar semantics.
#define DAC_INSTANCE_ALIGN 16
#define DAC_INSTANCE_SIG 0xdac1
// The instance manager allocates large blocks and then
// suballocates those for particular instances.
struct DAC_INSTANCE_BLOCK
{
DAC_INSTANCE_BLOCK* next;
ULONG32 bytesUsed;
ULONG32 bytesFree;
};
#define DAC_INSTANCE_BLOCK_ALLOCATION 0x40000
// Sufficient memory is allocated to guarantee storage of the
// instance header plus room for alignment padding.
// Once the aligned pointer is found, this structure is prepended to
// the aligned pointer and therefore doesn't affect the alignment
// of the actual instance data.
struct DAC_INSTANCE
{
DAC_INSTANCE* next;
TADDR addr;
ULONG32 size;
// Identifying marker to give a simple
// check for host->taddr validity.
ULONG32 sig:16;
// DPTR or VPTR. See code:DAC_USAGE_TYPE
ULONG32 usage:2;
// Marker that can be used to prevent reporting this memory to the callback
// object (via ICLRDataEnumMemoryRegionsCallback:EnumMemoryRegion)
// more than once. This bit is checked only by the DacEnumHost?PtrMem
// macros, so consistent use of those macros ensures that the memory is
// reported at most once
ULONG32 enumMem:1;
// Marker to prevent metadata gets reported to mini-dump
ULONG32 noReport:1;
// Marker to determine if EnumMemoryRegions has been called on
// a method descriptor
ULONG32 MDEnumed:1;
#ifdef HOST_64BIT
// Keep DAC_INSTANCE a multiple of DAC_INSTANCE_ALIGN
// bytes in size.
ULONG32 pad[2];
#endif
};
struct DAC_INSTANCE_PUSH
{
DAC_INSTANCE_PUSH* next;
DAC_INSTANCE_BLOCK* blocks;
ULONG64 blockMemUsage;
ULONG32 numInst;
ULONG64 instMemUsage;
};
// The runtime will want the best access locality possible,
// so it's likely that many instances will be clustered.
// The hash function needs to spread near addresses across
// hash entries, so hash on the low bits of the target address.
// Not all the way down to the LSB, though, as there generally
// won't be individual accesses at the byte level. Assume that
// most accesses will be natural-word aligned.
#define DAC_INSTANCE_HASH_BITS 10
#define DAC_INSTANCE_HASH_SHIFT 2
#define DAC_INSTANCE_HASH(addr) \
(((ULONG32)(ULONG_PTR)(addr) >> DAC_INSTANCE_HASH_SHIFT) & \
((1 << DAC_INSTANCE_HASH_BITS) - 1))
#define DAC_INSTANCE_HASH_SIZE (1 << DAC_INSTANCE_HASH_BITS)
struct DumpMemoryReportStatics
{
TSIZE_T m_cbStack; // number of bytes that we report directly for stack walk
TSIZE_T m_cbNgen; // number of bytes that we report directly for ngen images
TSIZE_T m_cbModuleList; // number of bytes that we report for module list directly
TSIZE_T m_cbClrStatics; // number of bytes that we report for CLR statics
TSIZE_T m_cbClrHeapStatics; // number of bytes that we report for CLR heap statics
TSIZE_T m_cbImplicitly; // number of bytes that we report implicitly
};
class DacInstanceManager
{
public:
DacInstanceManager(void);
~DacInstanceManager(void);
DAC_INSTANCE* Add(DAC_INSTANCE* inst);
DAC_INSTANCE* Alloc(TADDR addr, ULONG32 size, DAC_USAGE_TYPE usage);
void ReturnAlloc(DAC_INSTANCE* inst);
DAC_INSTANCE* Find(TADDR addr);
HRESULT Write(DAC_INSTANCE* inst, bool throwEx);
void Supersede(DAC_INSTANCE* inst);
void Flush(void);
void Flush(bool fSaveBlock);
void ClearEnumMemMarker(void);
void AddSuperseded(DAC_INSTANCE* inst)
{
SUPPORTS_DAC;
inst->next = m_superseded;
m_superseded = inst;
}
UINT DumpAllInstances(ICLRDataEnumMemoryRegionsCallback *pCallBack);
private:
DAC_INSTANCE_BLOCK* FindInstanceBlock(DAC_INSTANCE* inst);
void FreeAllBlocks(bool fSaveBlock);
void InitEmpty(void)
{
m_blocks = NULL;
// m_unusedBlock is not NULLed here; it can contain one block we will use after
// a flush is complete.
m_blockMemUsage = 0;
m_numInst = 0;
m_instMemUsage = 0;
#ifdef DAC_HASHTABLE
ZeroMemory(m_hash, sizeof(m_hash));
#endif
m_superseded = NULL;
m_instPushed = NULL;
}
#if defined(DAC_HASHTABLE)
typedef struct _HashInstanceKey {
TADDR addr;
DAC_INSTANCE* instance;
} HashInstanceKey;
typedef struct _HashInstanceKeyBlock {
// Blocks are chained in reverse order of allocation so that the most recently allocated
// block is searched first.
_HashInstanceKeyBlock* next;
// Entries to a block are added from the max index on down so that recently added
// entries are at the start of the block.
DWORD firstElement;
HashInstanceKey instanceKeys[] ;
} HashInstanceKeyBlock;
// The hashing function does a good job of distributing the entries across buckets. To handle a
// SO on x86, we have under 250 entries in a bucket. A 4K block size allows 511 entries on x86 and
// about half that on x64. On x64, the number of entries added to the hash table is significantly
// smaller than on x86 (and the max recursion depth for default stack sizes is also far less), so
// 4K is generally adequate.
#define HASH_INSTANCE_BLOCK_ALLOC_SIZE (4 * 1024)
#define HASH_INSTANCE_BLOCK_NUM_ELEMENTS ((HASH_INSTANCE_BLOCK_ALLOC_SIZE - offsetof(_HashInstanceKeyBlock, instanceKeys))/sizeof(HashInstanceKey))
#endif // #if defined(DAC_HASHTABLE)
DAC_INSTANCE_BLOCK* m_blocks;
DAC_INSTANCE_BLOCK* m_unusedBlock;
ULONG64 m_blockMemUsage;
ULONG32 m_numInst;
ULONG64 m_instMemUsage;
#if defined(DAC_HASHTABLE)
HashInstanceKeyBlock* m_hash[DAC_INSTANCE_HASH_SIZE];
#else //DAC_HASHTABLE
// We're going to use the STL unordered_map for our instance hash.
// This has the benefit of scaling to different workloads appropriately (as opposed to having a
// fixed number of buckets).
class DacHashCompare : public std::hash_compare<TADDR>
{
public:
// Custom hash function
// The default hash function uses a pseudo-randomizing function to get a random
// distribution. In our case, we'd actually like a more even distribution to get
// better access locality (see comments for DAC_INSTANCE_HASH_BITS).
//
// Also, when enumerating the hash during dump generation, clustering nearby addresses
// together can have a significant positive impact on the performance of the minidump
// library (at least the un-optimized version 6.5 linked into our dw20.exe - on Vista+
// we use the OS's version 6.7+ with radically improved perf characteristics). Having
// a random distribution is actually the worst-case because it means most blocks won't
// be merged until near the end, and a large number of intermediate blocks will have to
// be searched with each call.
//
// The default pseudo-randomizing function also requires a call to ldiv which shows up as
// a 3%-5% perf hit in most perf-sensitive scenarios, so this should also always be
// faster.
inline size_t operator()(const TADDR& keyval) const
{
return (unsigned)(keyval >>DAC_INSTANCE_HASH_SHIFT);
}
// Explicitly bring in the two-argument comparison function from the base class (just less-than)
// This is necessary because once we override one form of operator() above, we don't automatically
// get the others by C++ inheritance rules.
using std::hash_compare<TADDR>::operator();
#ifdef NIDUMP_CUSTOMIZED_DAC_HASH // not set
//this particular number is supposed to be roughly the same amount of
//memory as the old code (buckets * number of entries in the old
//blocks.)
//disabled for now. May tweak implementation later. It turns out that
//having a large number of initial buckets is excellent for nidump, but it
// is terrible for most other scenarios due to the cost of clearing them at
// every Flush. Once there is a better perf suite, we can tweak these values more.
static const size_t min_buckets = DAC_INSTANCE_HASH_SIZE * 256;
#endif
};
typedef std::unordered_map<TADDR, DAC_INSTANCE*, DacHashCompare > DacInstanceHash;
typedef DacInstanceHash::value_type DacInstanceHashValue;
typedef DacInstanceHash::iterator DacInstanceHashIterator;
DacInstanceHash m_hash;
#endif //DAC_HASHTABLE
DAC_INSTANCE* m_superseded;
DAC_INSTANCE_PUSH* m_instPushed;
};
#ifdef FEATURE_MINIMETADATA_IN_TRIAGEDUMPS
class DacStreamManager;
#endif // FEATURE_MINIMETADATA_IN_TRIAGEDUMPS
//----------------------------------------------------------------------------
//
// ClrDataAccess.
//
//----------------------------------------------------------------------------
class ClrDataAccess
: public IXCLRDataProcess2,
public ICLRDataEnumMemoryRegions,
public ISOSDacInterface,
public ISOSDacInterface2,
public ISOSDacInterface3,
public ISOSDacInterface4,
public ISOSDacInterface5,
public ISOSDacInterface6,
public ISOSDacInterface7,
public ISOSDacInterface8,
public ISOSDacInterface9,
public ISOSDacInterface10,
public ISOSDacInterface11,
public ISOSDacInterface12,
public ISOSDacInterface13
{
public:
ClrDataAccess(ICorDebugDataTarget * pTarget, ICLRDataTarget * pLegacyTarget=0);
virtual ~ClrDataAccess(void);
// IUnknown.
STDMETHOD(QueryInterface)(THIS_
IN REFIID interfaceId,
OUT PVOID* iface);
STDMETHOD_(ULONG, AddRef)(THIS);
STDMETHOD_(ULONG, Release)(THIS);
//
// IXCLRDataProcess.
//
virtual HRESULT STDMETHODCALLTYPE Flush( void);
virtual HRESULT STDMETHODCALLTYPE StartEnumTasks(
/* [out] */ CLRDATA_ENUM *handle);
virtual HRESULT STDMETHODCALLTYPE EnumTask(
/* [in, out] */ CLRDATA_ENUM* handle,
/* [out] */ IXCLRDataTask **task);
virtual HRESULT STDMETHODCALLTYPE EndEnumTasks(
/* [in] */ CLRDATA_ENUM handle);
virtual HRESULT STDMETHODCALLTYPE GetTaskByOSThreadID(
/* [in] */ ULONG32 OSThreadID,
/* [out] */ IXCLRDataTask **task);
virtual HRESULT STDMETHODCALLTYPE GetTaskByUniqueID(
/* [in] */ ULONG64 uniqueID,
/* [out] */ IXCLRDataTask **task);
virtual HRESULT STDMETHODCALLTYPE GetFlags(
/* [out] */ ULONG32 *flags);
virtual HRESULT STDMETHODCALLTYPE IsSameObject(
/* [in] */ IXCLRDataProcess *process);
virtual HRESULT STDMETHODCALLTYPE GetManagedObject(
/* [out] */ IXCLRDataValue **value);
virtual HRESULT STDMETHODCALLTYPE GetDesiredExecutionState(
/* [out] */ ULONG32 *state);
virtual HRESULT STDMETHODCALLTYPE SetDesiredExecutionState(
/* [in] */ ULONG32 state);
virtual HRESULT STDMETHODCALLTYPE GetAddressType(
/* [in] */ CLRDATA_ADDRESS address,
/* [out] */ CLRDataAddressType* type);
virtual HRESULT STDMETHODCALLTYPE GetRuntimeNameByAddress(
/* [in] */ CLRDATA_ADDRESS address,
/* [in] */ ULONG32 flags,
/* [in] */ ULONG32 bufLen,
/* [out] */ ULONG32 *nameLen,
/* [size_is][out] */ _Out_writes_bytes_opt_(bufLen) WCHAR nameBuf[ ],
/* [out] */ CLRDATA_ADDRESS* displacement);
virtual HRESULT STDMETHODCALLTYPE StartEnumAppDomains(
/* [out] */ CLRDATA_ENUM *handle);
virtual HRESULT STDMETHODCALLTYPE EnumAppDomain(
/* [in, out] */ CLRDATA_ENUM* handle,
/* [out] */ IXCLRDataAppDomain **appDomain);
virtual HRESULT STDMETHODCALLTYPE EndEnumAppDomains(
/* [in] */ CLRDATA_ENUM handle);
virtual HRESULT STDMETHODCALLTYPE GetAppDomainByUniqueID(
/* [in] */ ULONG64 uniqueID,
/* [out] */ IXCLRDataAppDomain **appDomain);
virtual HRESULT STDMETHODCALLTYPE StartEnumAssemblies(
/* [out] */ CLRDATA_ENUM *handle);
virtual HRESULT STDMETHODCALLTYPE EnumAssembly(
/* [in, out] */ CLRDATA_ENUM* handle,
/* [out] */ IXCLRDataAssembly **assembly);
virtual HRESULT STDMETHODCALLTYPE EndEnumAssemblies(
/* [in] */ CLRDATA_ENUM handle);
virtual HRESULT STDMETHODCALLTYPE StartEnumModules(
/* [out] */ CLRDATA_ENUM *handle);
virtual HRESULT STDMETHODCALLTYPE EnumModule(
/* [in, out] */ CLRDATA_ENUM* handle,
/* [out] */ IXCLRDataModule **mod);
virtual HRESULT STDMETHODCALLTYPE EndEnumModules(
/* [in] */ CLRDATA_ENUM handle);
virtual HRESULT STDMETHODCALLTYPE GetModuleByAddress(
/* [in] */ CLRDATA_ADDRESS address,
/* [out] */ IXCLRDataModule** mod);
virtual HRESULT STDMETHODCALLTYPE StartEnumMethodDefinitionsByAddress(
/* [in] */ CLRDATA_ADDRESS address,
/* [out] */ CLRDATA_ENUM *handle);
virtual HRESULT STDMETHODCALLTYPE EnumMethodDefinitionByAddress(
/* [in] */ CLRDATA_ENUM* handle,
/* [out] */ IXCLRDataMethodDefinition **method);
virtual HRESULT STDMETHODCALLTYPE EndEnumMethodDefinitionsByAddress(
/* [in] */ CLRDATA_ENUM handle);
virtual HRESULT STDMETHODCALLTYPE StartEnumMethodInstancesByAddress(
/* [in] */ CLRDATA_ADDRESS address,
/* [in] */ IXCLRDataAppDomain* appDomain,
/* [out] */ CLRDATA_ENUM *handle);
virtual HRESULT STDMETHODCALLTYPE EnumMethodInstanceByAddress(
/* [in] */ CLRDATA_ENUM* handle,
/* [out] */ IXCLRDataMethodInstance **method);
virtual HRESULT STDMETHODCALLTYPE EndEnumMethodInstancesByAddress(
/* [in] */ CLRDATA_ENUM handle);
virtual HRESULT STDMETHODCALLTYPE GetDataByAddress(
/* [in] */ CLRDATA_ADDRESS address,
/* [in] */ ULONG32 flags,
/* [in] */ IXCLRDataAppDomain* appDomain,
/* [in] */ IXCLRDataTask* tlsTask,
/* [in] */ ULONG32 bufLen,
/* [out] */ ULONG32 *nameLen,
/* [size_is][out] */ _Out_writes_to_opt_(bufLen, *nameLen) WCHAR nameBuf[ ],
/* [out] */ IXCLRDataValue **value,
/* [out] */ CLRDATA_ADDRESS *displacement);
virtual HRESULT STDMETHODCALLTYPE GetExceptionStateByExceptionRecord(
/* [in] */ EXCEPTION_RECORD64 *record,
/* [out] */ IXCLRDataExceptionState **exception);
virtual HRESULT STDMETHODCALLTYPE TranslateExceptionRecordToNotification(
/* [in] */ EXCEPTION_RECORD64 *record,
/* [in] */ IXCLRDataExceptionNotification *notify);
virtual HRESULT STDMETHODCALLTYPE CreateMemoryValue(
/* [in] */ IXCLRDataAppDomain* appDomain,
/* [in] */ IXCLRDataTask* tlsTask,
/* [in] */ IXCLRDataTypeInstance* type,
/* [in] */ CLRDATA_ADDRESS addr,
/* [out] */ IXCLRDataValue** value);
virtual HRESULT STDMETHODCALLTYPE SetAllTypeNotifications(
/* [in] */ IXCLRDataModule* mod,
/* [in] */ ULONG32 flags);
virtual HRESULT STDMETHODCALLTYPE SetAllCodeNotifications(
/* [in] */ IXCLRDataModule* mod,
/* [in] */ ULONG32 flags);
virtual HRESULT STDMETHODCALLTYPE GetTypeNotifications(
/* [in] */ ULONG32 numTokens,
/* [in, size_is(numTokens)] */ IXCLRDataModule* mods[],
/* [in] */ IXCLRDataModule* singleMod,
/* [in, size_is(numTokens)] */ mdTypeDef tokens[],
/* [out, size_is(numTokens)] */ ULONG32 flags[]);
virtual HRESULT STDMETHODCALLTYPE SetTypeNotifications(
/* [in] */ ULONG32 numTokens,
/* [in, size_is(numTokens)] */ IXCLRDataModule* mods[],
/* [in] */ IXCLRDataModule* singleMod,
/* [in, size_is(numTokens)] */ mdTypeDef tokens[],
/* [in, size_is(numTokens)] */ ULONG32 flags[],
/* [in] */ ULONG32 singleFlags);
virtual HRESULT STDMETHODCALLTYPE GetCodeNotifications(
/* [in] */ ULONG32 numTokens,
/* [in, size_is(numTokens)] */ IXCLRDataModule* mods[],
/* [in] */ IXCLRDataModule* singleMod,
/* [in, size_is(numTokens)] */ mdMethodDef tokens[],
/* [out, size_is(numTokens)] */ ULONG32 flags[]);
virtual HRESULT STDMETHODCALLTYPE SetCodeNotifications(