@@ -23,6 +23,7 @@ internal class Instrumenter
2323 private readonly string [ ] _includeFilters ;
2424 private readonly string [ ] _excludedFiles ;
2525 private readonly string [ ] _excludedAttributes ;
26+ private readonly bool _isCoreLibrary ;
2627 private InstrumenterResult _result ;
2728 private FieldDefinition _customTrackerHitsFilePath ;
2829 private FieldDefinition _customTrackerHitsArray ;
@@ -41,6 +42,8 @@ public Instrumenter(string module, string identifier, string[] excludeFilters, s
4142 _includeFilters = includeFilters ;
4243 _excludedFiles = excludedFiles ?? Array . Empty < string > ( ) ;
4344 _excludedAttributes = excludedAttributes ;
45+
46+ _isCoreLibrary = Path . GetFileNameWithoutExtension ( _module ) == "System.Private.CoreLib" ;
4447 }
4548
4649 public bool CanInstrument ( ) => InstrumentationHelper . HasPdb ( _module ) ;
@@ -74,8 +77,7 @@ private void InstrumentModule()
7477 {
7578 resolver . AddSearchDirectory ( Path . GetDirectoryName ( _module ) ) ;
7679 var parameters = new ReaderParameters { ReadSymbols = true , AssemblyResolver = resolver } ;
77- bool isCoreLib = Path . GetFileNameWithoutExtension ( _module ) == "System.Private.CoreLib" ;
78- if ( isCoreLib )
80+ if ( _isCoreLibrary )
7981 {
8082 parameters . MetadataImporterProvider = new CoreLibMetadataImporterProvider ( ) ;
8183 }
@@ -97,7 +99,7 @@ private void InstrumentModule()
9799 var actualType = type . DeclaringType ?? type ;
98100 if ( ! actualType . CustomAttributes . Any ( IsExcludeAttribute )
99101 // Instrumenting Interlocked which is used for recording hits would cause an infinite loop.
100- && ( ! isCoreLib || actualType . FullName != "System.Threading.Interlocked" )
102+ && ( ! _isCoreLibrary || actualType . FullName != "System.Threading.Interlocked" )
101103 && ! InstrumentationHelper . IsTypeExcluded ( _module , actualType . FullName , _excludeFilters )
102104 && InstrumentationHelper . IsTypeIncluded ( _module , actualType . FullName , _includeFilters ) )
103105 InstrumentType ( type ) ;
@@ -430,9 +432,12 @@ private Instruction AddInstrumentationInstructions(MethodDefinition method, ILPr
430432 {
431433 if ( _customTrackerRecordHitMethod == null )
432434 {
435+ var recordHitMethodName = _isCoreLibrary
436+ ? nameof ( ModuleTrackerTemplate . RecordHitInCoreLibrary )
437+ : nameof ( ModuleTrackerTemplate . RecordHit ) ;
433438 _customTrackerRecordHitMethod = new MethodReference (
434- "RecordHit" , method . Module . TypeSystem . Void , _customTrackerTypeDef ) ;
435- _customTrackerRecordHitMethod . Parameters . Add ( new ParameterDefinition ( method . Module . TypeSystem . Int32 ) ) ;
439+ recordHitMethodName , method . Module . TypeSystem . Void , _customTrackerTypeDef ) ;
440+ _customTrackerRecordHitMethod . Parameters . Add ( new ParameterDefinition ( "hitLocationIndex" , ParameterAttributes . None , method . Module . TypeSystem . Int32 ) ) ;
436441 }
437442
438443 var indxInstr = Instruction . Create ( OpCodes . Ldc_I4 , hitEntryIndex ) ;
0 commit comments