@@ -26,6 +26,7 @@ internal class Instrumenter
2626 private FieldDefinition _customModuleTrackerHitsArray ;
2727 private FieldDefinition _customModuleTrackerHitsFilePath ;
2828 private ILProcessor _customModuleTrackerClassConstructorIl ;
29+ private TypeDefinition _customTrackerTypeDef ;
2930 private MethodReference _cachedInterlockedIncMethod ;
3031
3132 public Instrumenter ( string module , string identifier , string [ ] excludeFilters , string [ ] includeFilters , string [ ] excludedFiles )
@@ -101,14 +102,16 @@ private void AddCustomModuleTrackerToModule(ModuleDefinition module)
101102 TypeDefinition moduleTrackerTemplate = xad . MainModule . GetType (
102103 "Coverlet.Core.Instrumentation" , nameof ( ModuleTrackerTemplate ) ) ;
103104
104- TypeDefinition customTrackerTypeDef = new TypeDefinition (
105+ _customTrackerTypeDef = new TypeDefinition (
105106 "Coverlet.Core.Instrumentation.Tracker" , Path . GetFileNameWithoutExtension ( module . Name ) + "_" + _identifier , moduleTrackerTemplate . Attributes ) ;
106107
107- customTrackerTypeDef . BaseType = module . TypeSystem . Object ;
108+ _customTrackerTypeDef . BaseType = module . TypeSystem . Object ;
108109 foreach ( FieldDefinition fieldDef in moduleTrackerTemplate . Fields )
109110 {
110111 var fieldClone = new FieldDefinition ( fieldDef . Name , fieldDef . Attributes , fieldDef . FieldType ) ;
111- customTrackerTypeDef . Fields . Add ( fieldClone ) ;
112+ fieldClone . FieldType = module . ImportReference ( fieldClone . FieldType ) ;
113+
114+ _customTrackerTypeDef . Fields . Add ( fieldClone ) ;
112115
113116 if ( fieldClone . Name == "HitsArray" )
114117 _customModuleTrackerHitsArray = fieldClone ;
@@ -120,6 +123,14 @@ private void AddCustomModuleTrackerToModule(ModuleDefinition module)
120123 {
121124 MethodDefinition methodOnCustomType = new MethodDefinition ( methodDef . Name , methodDef . Attributes , methodDef . ReturnType ) ;
122125
126+ if ( methodDef . Name == "RecordHit" )
127+ {
128+ foreach ( var parameter in methodDef . Parameters )
129+ {
130+ methodOnCustomType . Parameters . Add ( new ParameterDefinition ( module . ImportReference ( parameter . ParameterType ) ) ) ;
131+ }
132+ }
133+
123134 foreach ( var variable in methodDef . Body . Variables )
124135 {
125136 methodOnCustomType . Body . Variables . Add ( new VariableDefinition ( module . ImportReference ( variable . VariableType ) ) ) ;
@@ -144,12 +155,12 @@ private void AddCustomModuleTrackerToModule(ModuleDefinition module)
144155 {
145156 // Move to the custom type
146157 instr . Operand = new MethodReference (
147- methodReference . Name , methodReference . ReturnType , customTrackerTypeDef ) ;
158+ methodReference . Name , methodReference . ReturnType , _customTrackerTypeDef ) ;
148159 }
149160 }
150161 else if ( instr . Operand is FieldReference fieldReference )
151162 {
152- instr . Operand = customTrackerTypeDef . Fields . Single ( fd => fd . Name == fieldReference . Name ) ;
163+ instr . Operand = _customTrackerTypeDef . Fields . Single ( fd => fd . Name == fieldReference . Name ) ;
153164 }
154165 else if ( instr . Operand is TypeReference typeReference )
155166 {
@@ -162,10 +173,10 @@ private void AddCustomModuleTrackerToModule(ModuleDefinition module)
162173 foreach ( var handler in methodDef . Body . ExceptionHandlers )
163174 methodOnCustomType . Body . ExceptionHandlers . Add ( handler ) ;
164175
165- customTrackerTypeDef . Methods . Add ( methodOnCustomType ) ;
176+ _customTrackerTypeDef . Methods . Add ( methodOnCustomType ) ;
166177 }
167178
168- module . Types . Add ( customTrackerTypeDef ) ;
179+ module . Types . Add ( _customTrackerTypeDef ) ;
169180 }
170181
171182 Debug . Assert ( _customModuleTrackerHitsArray != null ) ;
@@ -234,7 +245,7 @@ private void InstrumentIL(MethodDefinition method)
234245 foreach ( ExceptionHandler handler in processor . Body . ExceptionHandlers )
235246 ReplaceExceptionHandlerBoundary ( handler , instruction , target ) ;
236247
237- index += 5 ;
248+ index += 2 ;
238249 }
239250
240251 foreach ( var _branchTarget in targetedBranchPoints )
@@ -255,7 +266,7 @@ private void InstrumentIL(MethodDefinition method)
255266 foreach ( ExceptionHandler handler in processor . Body . ExceptionHandlers )
256267 ReplaceExceptionHandlerBoundary ( handler , instruction , target ) ;
257268
258- index += 5 ;
269+ index += 2 ;
259270 }
260271
261272 index ++ ;
@@ -282,7 +293,7 @@ private Instruction AddInstrumentationCode(MethodDefinition method, ILProcessor
282293 var entry = ( false , document . Index , sequencePoint . StartLine , sequencePoint . EndLine ) ;
283294 _result . HitCandidates . Add ( entry ) ;
284295
285- return AddInstrumentationInstructions ( method , processor , instruction , _result . HitCandidates . Count ) ;
296+ return AddInstrumentationInstructions ( method , processor , instruction , _result . HitCandidates . Count - 1 ) ;
286297 }
287298
288299 private Instruction AddInstrumentationCode ( MethodDefinition method , ILProcessor processor , Instruction instruction , BranchPoint branchPoint )
@@ -312,31 +323,45 @@ private Instruction AddInstrumentationCode(MethodDefinition method, ILProcessor
312323 var entry = ( true , document . Index , branchPoint . StartLine , ( int ) branchPoint . Ordinal ) ;
313324 _result . HitCandidates . Add ( entry ) ;
314325
315- return AddInstrumentationInstructions ( method , processor , instruction , _result . HitCandidates . Count ) ;
326+ return AddInstrumentationInstructions ( method , processor , instruction , _result . HitCandidates . Count - 1 ) ;
316327 }
317328
318329 private Instruction AddInstrumentationInstructions ( MethodDefinition method , ILProcessor processor , Instruction instruction , int hitEntryIndex )
319330 {
320331 if ( _cachedInterlockedIncMethod == null )
321332 {
322333 _cachedInterlockedIncMethod = new MethodReference (
323- "Increment " , method . Module . TypeSystem . Int32 , method . Module . ImportReference ( typeof ( System . Threading . Interlocked ) ) ) ;
324- _cachedInterlockedIncMethod . Parameters . Add ( new ParameterDefinition ( new ByReferenceType ( method . Module . TypeSystem . Int32 ) ) ) ;
334+ "RecordHit " , method . Module . TypeSystem . Void , _customTrackerTypeDef ) ;
335+ _cachedInterlockedIncMethod . Parameters . Add ( new ParameterDefinition ( method . Module . TypeSystem . Int32 ) ) ;
325336 }
326337
327- var sfldInstr = Instruction . Create ( OpCodes . Ldsfld , _customModuleTrackerHitsArray ) ;
328338 var indxInstr = Instruction . Create ( OpCodes . Ldc_I4 , hitEntryIndex ) ;
329- var arefInstr = Instruction . Create ( OpCodes . Ldelema , method . Module . TypeSystem . Int32 ) ;
330339 var callInstr = Instruction . Create ( OpCodes . Call , _cachedInterlockedIncMethod ) ;
331- var popInstr = Instruction . Create ( OpCodes . Pop ) ;
332-
333- processor . InsertBefore ( instruction , popInstr ) ;
334- processor . InsertBefore ( popInstr , callInstr ) ;
335- processor . InsertBefore ( callInstr , arefInstr ) ;
336- processor . InsertBefore ( arefInstr , indxInstr ) ;
337- processor . InsertBefore ( indxInstr , sfldInstr ) ;
338340
339- return sfldInstr ;
341+ processor . InsertBefore ( instruction , callInstr ) ;
342+ processor . InsertBefore ( callInstr , indxInstr ) ;
343+
344+ return indxInstr ;
345+ //if (_cachedInterlockedIncMethod == null)
346+ //{
347+ // _cachedInterlockedIncMethod = new MethodReference(
348+ // "Increment", method.Module.TypeSystem.Int32, method.Module.ImportReference(typeof(System.Threading.Interlocked)));
349+ // _cachedInterlockedIncMethod.Parameters.Add(new ParameterDefinition(new ByReferenceType(method.Module.TypeSystem.Int32)));
350+ //}
351+
352+ //var sfldInstr = Instruction.Create(OpCodes.Ldsfld, _customModuleTrackerHitsArray);
353+ //var indxInstr = Instruction.Create(OpCodes.Ldc_I4, hitEntryIndex);
354+ //var arefInstr = Instruction.Create(OpCodes.Ldelema, method.Module.TypeSystem.Int32);
355+ //var callInstr = Instruction.Create(OpCodes.Call, _cachedInterlockedIncMethod);
356+ //var popInstr = Instruction.Create(OpCodes.Pop);
357+
358+ //processor.InsertBefore(instruction, popInstr);
359+ //processor.InsertBefore(popInstr, callInstr);
360+ //processor.InsertBefore(callInstr, arefInstr);
361+ //processor.InsertBefore(arefInstr, indxInstr);
362+ //processor.InsertBefore(indxInstr, sfldInstr);
363+
364+ //return sfldInstr;
340365 }
341366
342367 private static void ReplaceInstructionTarget ( Instruction instruction , Instruction oldTarget , Instruction newTarget )
0 commit comments