@@ -26,6 +26,7 @@ internal class Instrumenter
26
26
private FieldDefinition _customModuleTrackerHitsArray ;
27
27
private FieldDefinition _customModuleTrackerHitsFilePath ;
28
28
private ILProcessor _customModuleTrackerClassConstructorIl ;
29
+ private TypeDefinition _customTrackerTypeDef ;
29
30
private MethodReference _cachedInterlockedIncMethod ;
30
31
31
32
public Instrumenter ( string module , string identifier , string [ ] excludeFilters , string [ ] includeFilters , string [ ] excludedFiles )
@@ -101,14 +102,16 @@ private void AddCustomModuleTrackerToModule(ModuleDefinition module)
101
102
TypeDefinition moduleTrackerTemplate = xad . MainModule . GetType (
102
103
"Coverlet.Core.Instrumentation" , nameof ( ModuleTrackerTemplate ) ) ;
103
104
104
- TypeDefinition customTrackerTypeDef = new TypeDefinition (
105
+ _customTrackerTypeDef = new TypeDefinition (
105
106
"Coverlet.Core.Instrumentation.Tracker" , Path . GetFileNameWithoutExtension ( module . Name ) + "_" + _identifier , moduleTrackerTemplate . Attributes ) ;
106
107
107
- customTrackerTypeDef . BaseType = module . TypeSystem . Object ;
108
+ _customTrackerTypeDef . BaseType = module . TypeSystem . Object ;
108
109
foreach ( FieldDefinition fieldDef in moduleTrackerTemplate . Fields )
109
110
{
110
111
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 ) ;
112
115
113
116
if ( fieldClone . Name == "HitsArray" )
114
117
_customModuleTrackerHitsArray = fieldClone ;
@@ -120,6 +123,14 @@ private void AddCustomModuleTrackerToModule(ModuleDefinition module)
120
123
{
121
124
MethodDefinition methodOnCustomType = new MethodDefinition ( methodDef . Name , methodDef . Attributes , methodDef . ReturnType ) ;
122
125
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
+
123
134
foreach ( var variable in methodDef . Body . Variables )
124
135
{
125
136
methodOnCustomType . Body . Variables . Add ( new VariableDefinition ( module . ImportReference ( variable . VariableType ) ) ) ;
@@ -144,12 +155,12 @@ private void AddCustomModuleTrackerToModule(ModuleDefinition module)
144
155
{
145
156
// Move to the custom type
146
157
instr . Operand = new MethodReference (
147
- methodReference . Name , methodReference . ReturnType , customTrackerTypeDef ) ;
158
+ methodReference . Name , methodReference . ReturnType , _customTrackerTypeDef ) ;
148
159
}
149
160
}
150
161
else if ( instr . Operand is FieldReference fieldReference )
151
162
{
152
- instr . Operand = customTrackerTypeDef . Fields . Single ( fd => fd . Name == fieldReference . Name ) ;
163
+ instr . Operand = _customTrackerTypeDef . Fields . Single ( fd => fd . Name == fieldReference . Name ) ;
153
164
}
154
165
else if ( instr . Operand is TypeReference typeReference )
155
166
{
@@ -162,10 +173,10 @@ private void AddCustomModuleTrackerToModule(ModuleDefinition module)
162
173
foreach ( var handler in methodDef . Body . ExceptionHandlers )
163
174
methodOnCustomType . Body . ExceptionHandlers . Add ( handler ) ;
164
175
165
- customTrackerTypeDef . Methods . Add ( methodOnCustomType ) ;
176
+ _customTrackerTypeDef . Methods . Add ( methodOnCustomType ) ;
166
177
}
167
178
168
- module . Types . Add ( customTrackerTypeDef ) ;
179
+ module . Types . Add ( _customTrackerTypeDef ) ;
169
180
}
170
181
171
182
Debug . Assert ( _customModuleTrackerHitsArray != null ) ;
@@ -234,7 +245,7 @@ private void InstrumentIL(MethodDefinition method)
234
245
foreach ( ExceptionHandler handler in processor . Body . ExceptionHandlers )
235
246
ReplaceExceptionHandlerBoundary ( handler , instruction , target ) ;
236
247
237
- index += 5 ;
248
+ index += 2 ;
238
249
}
239
250
240
251
foreach ( var _branchTarget in targetedBranchPoints )
@@ -255,7 +266,7 @@ private void InstrumentIL(MethodDefinition method)
255
266
foreach ( ExceptionHandler handler in processor . Body . ExceptionHandlers )
256
267
ReplaceExceptionHandlerBoundary ( handler , instruction , target ) ;
257
268
258
- index += 5 ;
269
+ index += 2 ;
259
270
}
260
271
261
272
index ++ ;
@@ -282,7 +293,7 @@ private Instruction AddInstrumentationCode(MethodDefinition method, ILProcessor
282
293
var entry = ( false , document . Index , sequencePoint . StartLine , sequencePoint . EndLine ) ;
283
294
_result . HitCandidates . Add ( entry ) ;
284
295
285
- return AddInstrumentationInstructions ( method , processor , instruction , _result . HitCandidates . Count ) ;
296
+ return AddInstrumentationInstructions ( method , processor , instruction , _result . HitCandidates . Count - 1 ) ;
286
297
}
287
298
288
299
private Instruction AddInstrumentationCode ( MethodDefinition method , ILProcessor processor , Instruction instruction , BranchPoint branchPoint )
@@ -312,31 +323,45 @@ private Instruction AddInstrumentationCode(MethodDefinition method, ILProcessor
312
323
var entry = ( true , document . Index , branchPoint . StartLine , ( int ) branchPoint . Ordinal ) ;
313
324
_result . HitCandidates . Add ( entry ) ;
314
325
315
- return AddInstrumentationInstructions ( method , processor , instruction , _result . HitCandidates . Count ) ;
326
+ return AddInstrumentationInstructions ( method , processor , instruction , _result . HitCandidates . Count - 1 ) ;
316
327
}
317
328
318
329
private Instruction AddInstrumentationInstructions ( MethodDefinition method , ILProcessor processor , Instruction instruction , int hitEntryIndex )
319
330
{
320
331
if ( _cachedInterlockedIncMethod == null )
321
332
{
322
333
_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 ) ) ;
325
336
}
326
337
327
- var sfldInstr = Instruction . Create ( OpCodes . Ldsfld , _customModuleTrackerHitsArray ) ;
328
338
var indxInstr = Instruction . Create ( OpCodes . Ldc_I4 , hitEntryIndex ) ;
329
- var arefInstr = Instruction . Create ( OpCodes . Ldelema , method . Module . TypeSystem . Int32 ) ;
330
339
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 ) ;
338
340
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;
340
365
}
341
366
342
367
private static void ReplaceInstructionTarget ( Instruction instruction , Instruction oldTarget , Instruction newTarget )
0 commit comments