@@ -31,6 +31,7 @@ internal class Instrumenter
3131 private TypeDefinition _customTrackerTypeDef ;
3232 private MethodReference _customTrackerRegisterUnloadEventsMethod ;
3333 private MethodReference _customTrackerRecordHitMethod ;
34+ private List < string > _asyncMachineStateMethod ;
3435
3536 public Instrumenter ( string module , string identifier , string [ ] excludeFilters , string [ ] includeFilters , string [ ] excludedFiles , string [ ] excludedAttributes )
3637 {
@@ -61,6 +62,8 @@ public InstrumenterResult Instrument()
6162
6263 InstrumentModule ( ) ;
6364
65+ _result . AsyncMachineStateMethod = _asyncMachineStateMethod == null ? Array . Empty < string > ( ) : _asyncMachineStateMethod . ToArray ( ) ;
66+
6467 return _result ;
6568 }
6669
@@ -372,6 +375,7 @@ private Instruction AddInstrumentationCode(MethodDefinition method, ILProcessor
372375
373376 var key = ( branchPoint . StartLine , ( int ) branchPoint . Ordinal ) ;
374377 if ( ! document . Branches . ContainsKey ( key ) )
378+ {
375379 document . Branches . Add ( key ,
376380 new Branch
377381 {
@@ -385,12 +389,43 @@ private Instruction AddInstrumentationCode(MethodDefinition method, ILProcessor
385389 }
386390 ) ;
387391
392+ if ( IsAsyncStateMachineBranch ( method . DeclaringType , method ) )
393+ {
394+ if ( _asyncMachineStateMethod == null )
395+ {
396+ _asyncMachineStateMethod = new List < string > ( ) ;
397+ }
398+
399+ if ( ! _asyncMachineStateMethod . Contains ( method . FullName ) )
400+ {
401+ _asyncMachineStateMethod . Add ( method . FullName ) ;
402+ }
403+ }
404+ }
405+
388406 var entry = ( true , document . Index , branchPoint . StartLine , ( int ) branchPoint . Ordinal ) ;
389407 _result . HitCandidates . Add ( entry ) ;
390408
391409 return AddInstrumentationInstructions ( method , processor , instruction , _result . HitCandidates . Count - 1 ) ;
392410 }
393411
412+ private bool IsAsyncStateMachineBranch ( TypeDefinition typeDef , MethodDefinition method )
413+ {
414+ if ( ! method . FullName . EndsWith ( "::MoveNext()" ) )
415+ {
416+ return false ;
417+ }
418+
419+ foreach ( InterfaceImplementation implementedInterface in typeDef . Interfaces )
420+ {
421+ if ( implementedInterface . InterfaceType . FullName == "System.Runtime.CompilerServices.IAsyncStateMachine" )
422+ {
423+ return true ;
424+ }
425+ }
426+ return false ;
427+ }
428+
394429 private Instruction AddInstrumentationInstructions ( MethodDefinition method , ILProcessor processor , Instruction instruction , int hitEntryIndex )
395430 {
396431 if ( _customTrackerRecordHitMethod == null )
0 commit comments