https://github.com/dotnet/coreclr/pull/18160?tdsourcetag=s_pcqq_aiomsg
Debugging async methods with sos can be time consuming and relies on knowing how to use dumpheap, dumpvc, gcroot, and other commands to get the desired information, while often digging through a myriad of objects on the heap to find the desired info.
This commit adds a new DumpAsync command, which finds the async state machine objects on the GC heap and outputs relevant information about each, including the fields of its state machine, any registered continuation, and GC roots for the state machine object (as they often serve as a valid substitute for call stacks).
Example program used as a test:
using System.Threading.Tasks;class Program { static async Task Main() => await MethodA(); static async Task MethodA() => await MethodB(); static async Task MethodB() => await MethodC(); static async Task MethodC() => await MethodD(); static async Task MethodD() => await Task.Delay(int.MaxValue); }
and example command output:
0:011> !DumpAsync -type MethodD Address MT Size Name#0000001989f413de0 00007ff88c506ba8 112 System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib],[Program+d__4, test]]StateMachine: Program+ d__4 (struct) MT Field Offset Type VT Attr Value Name00007ff8d3df4b80 400000d 0 System.Int32 1 instance 0 <>1__state00007ff8d3e082c0 400000e 8 ...TaskMethodBuilder 1 instance 000001989f413e38 <>t__builder00007ff8d3dfea90 400000f 10 ...vices.TaskAwaiter 1 instance 000001989f413e40 <>u__1Continuation: 000001989f413e50 (System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib],[Program+ d__3, test]])GC roots: Thread 2936c: 000000071a37e050 00007ff8d3ac1657 System.Threading.Tasks.Task.SpinThenBlockingWait(Int32, System.Threading.CancellationToken) [d:\repos\coreclr\src\System.Private.CoreLib\src\System\Threading\Tasks\Task.cs @ 2977] rbp+10: 000000071a37e0c0 -> 000001989f413fa0 System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib],[Program+ d__0, test]] -> 000001989f413f30 System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib],[Program+ d__1, test]] -> 000001989f413ec0 System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib],[Program+ d__2, test]] -> 000001989f413e50 System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib],[Program+ d__3, test]] -> 000001989f413de0 System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib],[Program+ d__4, test]] HandleTable: 000001989d8415f8 (pinned handle) -> 00000198af3e1038 System.Object[] -> 000001989f413410 System.Threading.TimerQueue[] -> 000001989f413468 System.Threading.TimerQueue -> 000001989f413330 System.Threading.TimerQueueTimer -> 000001989f412e40 System.Threading.Tasks.Task+DelayPromise -> 000001989f413de0 System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib],[Program+ d__4, test]]Found 1 state machines.