forked from dotnet/diagnostics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDumpAsyncCommand.cs
More file actions
1198 lines (1039 loc) · 55.5 KB
/
Copy pathDumpAsyncCommand.cs
File metadata and controls
1198 lines (1039 loc) · 55.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using Microsoft.Diagnostics.DebugServices;
using Microsoft.Diagnostics.Runtime;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace Microsoft.Diagnostics.ExtensionCommands
{
[Command(Name = CommandName, Aliases = new string[] { "DumpAsync" }, Help = "Displays information about async \"stacks\" on the garbage-collected heap.")]
public sealed class DumpAsyncCommand : ExtensionCommandBase
{
/// <summary>The name of the command.</summary>
private const string CommandName = "dumpasync";
/// <summary>Indent width.</summary>
private const int TabWidth = 2;
/// <summary>The command invocation syntax when used in Debugger Markup Language (DML) commands.</summary>
private const string DmlCommandInvoke = $"!{CommandName}";
/// <summary>The help text to render when asked for help.</summary>
private static readonly string s_detailedHelpText =
$"Usage: {CommandName} [--stats] [--coalesce] [--address <object address>] [--methodtable <mt address>] [--type <partial type name>] [--tasks] [--completed] [--fields]" + Environment.NewLine +
Environment.NewLine +
"Displays information about async \"stacks\" on the garbage-collected heap. Stacks" + Environment.NewLine +
"are synthesized by finding all task objects (including async state machine box" + Environment.NewLine +
"objects) on the GC heap and chaining them together based on continuations." + Environment.NewLine +
Environment.NewLine +
"Options:" + Environment.NewLine +
" --stats Summarize all async frames found rather than showing detailed stacks." + Environment.NewLine +
" --coalesce Coalesce stacks and portions of stacks that are the same." + Environment.NewLine +
" --address Only show stacks that include the object with the specified address." + Environment.NewLine +
" --methodtable Only show stacks that include objects with the specified method table." + Environment.NewLine +
" --type Only show stacks that include objects whose type includes the specified name in its name." + Environment.NewLine +
" --tasks Include stacks that contain only non-state machine task objects." + Environment.NewLine +
" --completed Include completed tasks in stacks." + Environment.NewLine +
" --fields Show fields for each async stack frame." + Environment.NewLine +
Environment.NewLine +
"Examples:" + Environment.NewLine +
$"Summarize all async frames associated with a specific method table address: !{CommandName} --stats --methodtable 0x00007ffbcfbe0970" + Environment.NewLine +
$"Show all stacks coalesced by common frames: !{CommandName} --coalesce" + Environment.NewLine +
$"Show each stack that includes \"ReadAsync\": !{CommandName} --type ReadAsync" + Environment.NewLine +
$"Show each stack that includes an object at a specific address, and include fields: !{CommandName} --address 0x000001264adce778 --fields";
/// <summary>Gets the runtime for the process. Set by the command framework.</summary>
public ClrRuntime? Runtime { get; set; }
/// <summary>Gets whether to only show stacks that include the object with the specified address.</summary>
[Option(Name = "--address", Aliases = new string[] { "-addr" }, Help = "Only show stacks that include the object with the specified address.")]
public ulong? ObjectAddress { get; set; }
/// <summary>Gets whether to only show stacks that include objects with the specified method table.</summary>
[Option(Name = "--methodtable", Aliases = new string[] { "-mt" }, Help = "Only show stacks that include objects with the specified method table.")]
public ulong? MethodTableAddress { get; set; }
/// <summary>Gets whether to only show stacks that include objects whose type includes the specified name in its name.</summary>
[Option(Name = "--type", Help = "Only show stacks that include objects whose type includes the specified name in its name.")]
public string? NameSubstring { get; set; }
/// <summary>Gets whether to include stacks that contain only non-state machine task objects.</summary>
[Option(Name = "--tasks", Aliases = new string[] { "-t" }, Help = "Include stacks that contain only non-state machine task objects.")]
public bool IncludeTasks { get; set; }
/// <summary>Gets whether to include completed tasks in stacks.</summary>
[Option(Name = "--completed", Aliases = new string[] { "-c" }, Help = "Include completed tasks in stacks.")]
public bool IncludeCompleted { get; set; }
/// <summary>Gets whether to show state machine fields for every async stack frame that has them.</summary>
[Option(Name = "--fields", Aliases = new string[] { "-f" }, Help = "Show state machine fields for every async stack frame that has them.")]
public bool DisplayFields { get; set; }
/// <summary>Gets whether to summarize all async frames found rather than showing detailed stacks.</summary>
[Option(Name = "--stats", Help = "Summarize all async frames found rather than showing detailed stacks.")]
public bool Summarize { get; set; }
/// <summary>Gets whether to coalesce stacks and portions of stacks that are the same.</summary>
[Option(Name = "--coalesce", Help = "Coalesce stacks and portions of stacks that are the same.")]
public bool CoalesceStacks { get; set; }
/// <summary>Invokes the command.</summary>
public override void ExtensionInvoke()
{
ClrRuntime? runtime = Runtime;
if (runtime is null)
{
WriteLineError("Unable to access runtime.");
return;
}
ClrHeap heap = runtime.Heap;
if (!heap.CanWalkHeap)
{
WriteLineError("Unable to examine the heap.");
return;
}
ClrType? taskType = runtime.BaseClassLibrary.GetTypeByName("System.Threading.Tasks.Task");
if (taskType is null)
{
WriteLineError("Unable to find required type.");
return;
}
ClrStaticField? taskCompletionSentinelType = taskType.GetStaticFieldByName("s_taskCompletionSentinel");
ClrObject taskCompletionSentinel = default;
if (taskCompletionSentinelType is not null)
{
Debug.Assert(taskCompletionSentinelType.IsObjectReference);
taskCompletionSentinel = taskCompletionSentinelType.ReadObject(runtime.BaseClassLibrary.AppDomain);
}
// Enumerate the heap, gathering up all relevant async-related objects.
Dictionary<ClrObject, AsyncObject> objects = CollectObjects();
// Render the data according to the options specified.
if (Summarize)
{
RenderStats();
}
else if (CoalesceStacks)
{
RenderCoalescedStacks();
}
else
{
RenderStacks();
}
return;
// <summary>Group frames and summarize how many of each occurred.</summary>
void RenderStats()
{
// Enumerate all of the "frames", and create a mapping from a rendering of that
// frame to its associated type and how many times that frame occurs.
var typeCounts = new Dictionary<string, (ClrType Type, int Count)>();
foreach (KeyValuePair<ClrObject, AsyncObject> pair in objects)
{
ClrObject obj = pair.Key;
if (obj.Type is null)
{
continue;
}
string description = Describe(obj);
if (!typeCounts.TryGetValue(description, out (ClrType Type, int Count) value))
{
value = (obj.Type, 0);
}
value.Count++;
typeCounts[description] = value;
}
// Render one line per frame.
WriteHeaderLine($"{"MT",-16} {"Count",-8} Type");
foreach (KeyValuePair<string, (ClrType Type, int Count)> entry in typeCounts.OrderByDescending(e => e.Value.Count))
{
WriteMethodTable(entry.Value.Type.MethodTable, asyncObject: true);
WriteLine($" {entry.Value.Count,-8:N0} {entry.Key}");
}
}
// <summary>Group stacks at each frame in order to render a tree of coalesced stacks.</summary>
void RenderCoalescedStacks()
{
// Find all stacks to include.
var startingList = new List<ClrObject>();
foreach (KeyValuePair<ClrObject, AsyncObject> entry in objects)
{
Console.CancellationToken.ThrowIfCancellationRequested();
AsyncObject obj = entry.Value;
if (obj.TopLevel && ShouldIncludeStack(obj))
{
startingList.Add(entry.Key);
}
}
// If we found any, render them.
if (startingList.Count > 0)
{
RenderLevel(startingList, 0);
}
// <summary>Renders the next level of frames for coalesced stacks.</summary>
void RenderLevel(List<ClrObject> frames, int depth)
{
Console.CancellationToken.ThrowIfCancellationRequested();
List<ClrObject> nextLevel = new List<ClrObject>();
// Grouping function. We want to treat all objects that render the same as the same entity.
// For async state machines, we include the await state, both because we want it to render
// and because we want to see state machines at different positions as part of different groups.
Func<ClrObject, string> groupBy = o =>
{
string description = Describe(o);
if (objects.TryGetValue(o, out AsyncObject asyncObject) && asyncObject.IsStateMachine)
{
description = $"({asyncObject.AwaitState}) {description}";
}
return description;
};
// Group all of the frames, rendering each group as a single line with a count.
// Then recur for each.
int stackId = 1;
foreach (IGrouping<string, ClrObject> group in frames.GroupBy(groupBy).OrderByDescending(g => g.Count()))
{
int count = group.Count();
Debug.Assert(count > 0);
// For top-level frames, write out a header.
if (depth == 0)
{
WriteHeaderLine($"STACKS {stackId++}");
}
// Write out the count and frame.
Write($"{Tabs(depth)}[{count}] ");
WriteMethodTable(group.First().Type?.MethodTable ?? 0, asyncObject: true);
WriteLine($" {group.Key}");
// Gather up all of the next level of frames.
nextLevel.Clear();
foreach (ClrObject next in group)
{
if (objects.TryGetValue(next, out AsyncObject asyncObject))
{
// Note that the merging of multiple continuations can lead to numbers increasing at a particular
// level of the coalesced stacks. It's not clear there's a better answer.
nextLevel.AddRange(asyncObject.Continuations);
}
}
// If we found any, recur.
if (nextLevel.Count != 0)
{
RenderLevel(nextLevel, depth + 1);
}
if (depth == 0)
{
WriteLine("");
}
}
}
}
// <summary>Render each stack of frames.</summary>
void RenderStacks()
{
var stack = new Stack<(AsyncObject AsyncObject, int Depth)>();
// Find every top-level object (ones that nothing else has as a continuation) and output
// a stack starting from each.
int stackId = 1;
foreach (KeyValuePair<ClrObject, AsyncObject> entry in objects)
{
Console.CancellationToken.ThrowIfCancellationRequested();
AsyncObject top = entry.Value;
if (!top.TopLevel || !ShouldIncludeStack(top))
{
continue;
}
int depth = 0;
WriteHeaderLine($"STACK {stackId++}");
// If the top-level frame is an async method that's paused at an await, it must be waiting on
// something. Try to synthesize a frame to represent that thing, just to provide a little more information.
if (top.IsStateMachine && top.AwaitState >= 0 && !IsCompleted(top.TaskStateFlags) &&
top.StateMachine is IAddressableTypedEntity stateMachine &&
stateMachine.Type is not null)
{
// Short of parsing the method's IL, we don't have a perfect way to know which awaiter field
// corresponds to the current await state, as awaiter fields are shared across all awaits that
// use the same awaiter type. We instead employ a heuristic. If the await state is 0, the
// associated field will be the first one (<>u__1); even if other awaits share it, it's fine
// to use. Similarly, if there's only one awaiter field, we know that must be the one being
// used. In all other situations, we can't know which of the multiple awaiter fields maps
// to the await state, so we instead employ a heuristic of looking for one that's non-zero.
// The C# compiler zero's out awaiter fields when it's done with them, so if we find an awaiter
// field with any non-zero bytes, it must be the one in use. This can have false negatives,
// as it's perfectly valid for an awaiter to be all zero bytes, but it's better than nothing.
if ((top.AwaitState == 0) ||
stateMachine.Type.Fields.Count(f => f.Name is null || f.Name.StartsWith("<>u__", StringComparison.Ordinal) == true) == 1) // if the name is null, we have to assume it's an awaiter
{
if (stateMachine.Type.GetFieldByName("<>u__1") is ClrInstanceField field &&
TrySynthesizeAwaiterFrame(field))
{
depth++;
}
}
else
{
foreach (ClrInstanceField field in stateMachine.Type.Fields)
{
// Look for awaiter fields. This is the naming convention employed by the C# compiler.
if (field.Name?.StartsWith("<>u__") == true)
{
if (field.IsObjectReference)
{
if (stateMachine.ReadObjectField(field.Name) is ClrObject { IsNull: false } awaiter)
{
if (TrySynthesizeAwaiterFrame(field))
{
depth++;
}
break;
}
}
else if (field.IsValueType &&
stateMachine.ReadValueTypeField(field.Name) is ClrValueType { IsValid: true } awaiter &&
awaiter.Type is not null)
{
byte[] awaiterBytes = new byte[awaiter.Type.StaticSize - (runtime.DataTarget!.DataReader.PointerSize * 2)];
if (runtime.DataTarget!.DataReader.Read(awaiter.Address, awaiterBytes) == awaiterBytes.Length && !AllZero(awaiterBytes))
{
if (TrySynthesizeAwaiterFrame(field))
{
depth++;
}
break;
}
}
}
}
}
// <summary>Writes out a frame for the specified awaiter field, if possible.</summary>
bool TrySynthesizeAwaiterFrame(ClrInstanceField field)
{
if (field?.Name is string name)
{
if (field.IsObjectReference)
{
ClrObject awaiter = stateMachine.ReadObjectField(name);
if (awaiter.Type is not null)
{
Write("<< Awaiting: ");
WriteAddress(awaiter.Address, asyncObject: false);
Write(" ");
WriteMethodTable(awaiter.Type.MethodTable, asyncObject: false);
Write(awaiter.Type.Name);
WriteLine(" >>");
return true;
}
}
else if (field.IsValueType)
{
ClrValueType awaiter = stateMachine.ReadValueTypeField(name);
if (awaiter.Type is not null)
{
Write("<< Awaiting: ");
WriteValueTypeAddress(awaiter.Address, awaiter.Type.MethodTable);
Write(" ");
WriteMethodTable(awaiter.Type.MethodTable, asyncObject: false);
Write($" {awaiter.Type.Name}");
WriteLine(" >>");
return true;
}
}
}
return false;
}
}
// Push the root node onto the stack to start the iteration. Then as long as there are nodes left
// on the stack, pop the next, render it, and push any continuations it may have back onto the stack.
Debug.Assert(stack.Count == 0);
stack.Push((top, depth));
while (stack.Count > 0)
{
(AsyncObject frame, depth) = stack.Pop();
Write($"{Tabs(depth)}");
WriteAddress(frame.Object.Address, asyncObject: true);
Write(" ");
WriteMethodTable(frame.Object.Type?.MethodTable ?? 0, asyncObject: true);
Write($" {(frame.IsStateMachine ? $"({frame.AwaitState})" : $"({DescribeTaskFlags(frame.TaskStateFlags)})")} {Describe(frame.Object)}");
WriteCodeLink(frame.NativeCode);
WriteLine("");
if (DisplayFields)
{
RenderFields(frame.StateMachine ?? frame.Object, depth + 4); // +4 for extra indent for fields
}
foreach (ClrObject continuation in frame.Continuations)
{
if (objects.TryGetValue(continuation, out AsyncObject asyncContinuation))
{
stack.Push((asyncContinuation, depth + 1));
}
else
{
string state = TryGetTaskStateFlags(continuation, out int flags) ? DescribeTaskFlags(flags) : "";
Write($"{Tabs(depth + 1)}");
WriteAddress(continuation.Address, asyncObject: true);
Write(" ");
WriteMethodTable(continuation.Type?.MethodTable ?? 0, asyncObject: true);
WriteLine($" ({state}) {Describe(continuation)}");
}
}
}
WriteLine("");
}
}
// <summary>Determine whether the stack rooted in this object should be rendered.</summary>
bool ShouldIncludeStack(AsyncObject obj)
{
// We want to render the stack for this object once we find any node that should be
// included based on the criteria specified as arguments _and_ if the include tasks
// options wasn't specified, once we find any node that's an async state machine.
// That way, we scope the output down to just stacks that contain something the
// user is interested in seeing.
bool sawShouldInclude = false;
bool sawStateMachine = IncludeTasks;
var stack = new Stack<AsyncObject>();
stack.Push(obj);
while (stack.Count > 0)
{
obj = stack.Pop();
sawShouldInclude |= obj.IncludeInOutput;
sawStateMachine |= obj.IsStateMachine;
if (sawShouldInclude && sawStateMachine)
{
return true;
}
foreach (ClrObject continuation in obj.Continuations)
{
if (objects.TryGetValue(continuation, out AsyncObject asyncContinuation))
{
stack.Push(asyncContinuation);
}
}
}
return false;
}
// <summary>Outputs a line of information for each instance field on the object.</summary>
void RenderFields(IAddressableTypedEntity? obj, int depth)
{
if (obj?.Type is not null)
{
string depthTab = new string(' ', depth * TabWidth);
WriteHeaderLine($"{depthTab}{"Address",16} {"MT",16} {"Type",-32} {"Value",16} Name");
foreach (ClrInstanceField field in obj.Type.Fields)
{
if (field.Type is not null)
{
Write($"{depthTab}");
if (field.IsObjectReference)
{
ClrObject objRef = field.ReadObject(obj.Address, obj.Type.IsValueType);
WriteAddress(objRef.Address, asyncObject: false);
}
else
{
WriteValueTypeAddress(field.GetAddress(obj.Address, obj.Type.IsValueType), field.Type.MethodTable);
}
Write(" ");
WriteMethodTable(field.Type.MethodTable, asyncObject: false);
WriteLine($" {Truncate(field.Type.Name, 32),-32} {Truncate(GetDisplay(obj, field).ToString(), 16),16} {field.Name}");
}
}
}
}
// <summary>Gets a printable description for the specified object.</summary>
string Describe(ClrObject obj)
{
string description = string.Empty;
if (obj.Type?.Name is not null)
{
// Default the description to the type name.
description = obj.Type.Name;
if (IsStateMachineBox(obj.Type))
{
// Remove the boilerplate box type from the name.
int pos = description.IndexOf("StateMachineBox<", StringComparison.Ordinal);
if (pos >= 0)
{
ReadOnlySpan<char> slice = description.AsSpan(pos + "StateMachineBox<".Length);
slice = slice.Slice(0, slice.Length - 1); // remove trailing >
description = slice.ToString();
}
}
else if (TryGetValidObjectField(obj, "m_action", out ClrObject taskDelegate))
{
// If we can figure out what the task's delegate points to, append the method signature.
if (TryGetMethodFromDelegate(runtime, taskDelegate, out ClrMethod? method))
{
description = $"{description} {{{method!.Signature}}}";
}
}
else if (obj.Address != 0 && taskCompletionSentinel.Address == obj.Address)
{
description = "TaskCompletionSentinel";
}
}
return description;
}
// <summary>Determines whether the specified object is of interest to the user based on their criteria provided as command arguments.</summary>
bool IncludeInOutput(ClrObject obj)
{
if (ObjectAddress is ulong addr && obj.Address != addr)
{
return false;
}
if (obj.Type is not null)
{
if (MethodTableAddress is ulong mt && obj.Type.MethodTable != mt)
{
return false;
}
if (NameSubstring is not null && obj.Type.Name is not null && !obj.Type.Name.Contains(NameSubstring))
{
return false;
}
}
return true;
}
// <summary>Finds all of the relevant async-related objects on the heap.</summary>
Dictionary<ClrObject, AsyncObject> CollectObjects()
{
var found = new Dictionary<ClrObject, AsyncObject>();
// Enumerate the heap, looking for all relevant objects.
foreach (ClrObject obj in heap.EnumerateObjects())
{
Console.CancellationToken.ThrowIfCancellationRequested();
if (!obj.IsValid || obj.Type is null)
{
Trace.TraceError($"(Skipping invalid object {obj})");
continue;
}
// Skip objects too small to be state machines or tasks, simply to help with performance.
if (obj.Size <= 24)
{
continue;
}
// We only care about task-related objects (all boxes are tasks).
if (!IsTask(obj.Type))
{
continue;
}
// This is currently working around an issue that result in enumerating segments multiple times in 6.0 runtimes
// up to 6.0.5. The PR that fixes it is https://github.com/dotnet/runtime/pull/67995, but we have this here for back compat.
if (found.ContainsKey(obj))
{
continue;
}
// If we're only going to render a summary (which only considers objects individually and not
// as part of chains) and if this object shouldn't be included, we don't need to do anything more.
if (Summarize &&
(!IncludeInOutput(obj) || (!IncludeTasks && !IsStateMachineBox(obj.Type))))
{
continue;
}
// If we couldn't get state flags for the task, something's wrong; skip it.
if (!TryGetTaskStateFlags(obj, out int taskStateFlags))
{
continue;
}
// If we're supposed to ignore already completed tasks and this one is completed, skip it.
if (!IncludeCompleted && IsCompleted(taskStateFlags))
{
continue;
}
// Gather up the necessary data for the object and store it.
AsyncObject result = new()
{
Object = obj,
IsStateMachine = IsStateMachineBox(obj.Type),
IncludeInOutput = IncludeInOutput(obj),
TaskStateFlags = taskStateFlags,
};
if (result.IsStateMachine && TryGetStateMachine(obj, out result.StateMachine))
{
bool gotState = TryRead(result.StateMachine!, "<>1__state", out result.AwaitState);
Debug.Assert(gotState);
if (result.StateMachine?.Type is ClrType stateMachineType)
{
foreach (ClrMethod method in stateMachineType.Methods)
{
if (method.NativeCode != ulong.MaxValue)
{
result.NativeCode = method.NativeCode;
if (method.Name == "MoveNext")
{
break;
}
}
}
}
}
if (TryGetContinuation(obj, out ClrObject continuation))
{
AddContinuation(continuation, result.Continuations);
}
found.Add(obj, result);
}
// Mark off objects that are referenced by others and thus aren't top level
foreach (KeyValuePair<ClrObject, AsyncObject> entry in found)
{
foreach (ClrObject continuation in entry.Value.Continuations)
{
if (found.TryGetValue(continuation, out AsyncObject asyncContinuation))
{
asyncContinuation.TopLevel = false;
}
}
}
return found;
}
// <summary>Adds the continuation into the list of continuations.</summary>
// <remarks>
// If the continuation is actually a List{object}, enumerate the list to add
// each of the individual continuations to the continuations list.
// </remarks>
void AddContinuation(ClrObject continuation, List<ClrObject> continuations)
{
if (continuation.Type is not null)
{
if (continuation.Type.Name is not null &&
continuation.Type.Name.StartsWith("System.Collections.Generic.List<", StringComparison.Ordinal))
{
if (continuation.Type.GetFieldByName("_items") is ClrInstanceField itemsField)
{
ClrObject itemsObj = itemsField.ReadObject(continuation.Address, interior: false);
if (!itemsObj.IsNull)
{
ClrArray items = itemsObj.AsArray();
if (items.Rank == 1)
{
for (int i = 0; i < items.Length; i++)
{
if (items.GetObjectValue(i) is ClrObject { IsValid: true } c)
{
continuations.Add(ResolveContinuation(c));
}
}
}
}
}
}
else
{
continuations.Add(continuation);
}
}
}
// <summary>Tries to get the object contents of a Task's continuations field</summary>
bool TryGetContinuation(ClrObject obj, out ClrObject continuation)
{
if (obj.Type is not null &&
obj.Type.GetFieldByName("m_continuationObject") is ClrInstanceField continuationObjectField &&
continuationObjectField.ReadObject(obj.Address, interior: false) is ClrObject { IsValid: true } continuationObject)
{
continuation = ResolveContinuation(continuationObject);
return true;
}
continuation = default;
return false;
}
// <summary>Analyzes a continuation object to try to follow to the actual continuation target.</summary>
ClrObject ResolveContinuation(ClrObject continuation)
{
ClrObject tmp;
// If the continuation is an async method box, there's nothing more to resolve.
if (IsTask(continuation.Type) && IsStateMachineBox(continuation.Type))
{
return continuation;
}
// If it's a standard task continuation, get its task field.
if (TryGetValidObjectField(continuation, "m_task", out tmp))
{
return tmp;
}
// If it's storing an action wrapper, try to follow to that action's target.
if (TryGetValidObjectField(continuation, "m_action", out tmp))
{
continuation = tmp;
}
// If we now have an Action, try to follow through to the delegate's target.
if (TryGetValidObjectField(continuation, "_target", out tmp))
{
continuation = tmp;
// In some cases, the delegate's target might be a ContinuationWrapper, in which case we want to unwrap that as well.
if (continuation.Type?.Name == "System.Runtime.CompilerServices.AsyncMethodBuilderCore+ContinuationWrapper" &&
TryGetValidObjectField(continuation, "_continuation", out tmp))
{
continuation = tmp;
if (TryGetValidObjectField(continuation, "_target", out tmp))
{
continuation = tmp;
}
}
}
// Use whatever we ended with.
return continuation;
}
// <summary>Determines if a type is or is derived from Task.</summary>
bool IsTask(ClrType? type)
{
while (type is not null)
{
if (type.MetadataToken == taskType.MetadataToken &&
type.Module == taskType.Module)
{
return true;
}
type = type.BaseType;
}
return false;
}
}
/// <summary>Writes out a header line. If DML is supported, this will be bolded.</summary>
private void WriteHeaderLine(string text)
{
if (Console.SupportsDml)
{
Console.WriteDml($"<b>{text}</b>{Environment.NewLine}");
}
else
{
WriteLine(text);
}
}
/// <summary>Writes out a method table address. If DML is supported, this will be linked.</summary>
/// <param name="mt">The method table address.</param>
/// <param name="asyncObject">
/// true if this is an async-related object; otherwise, false. If true and if DML is supported,
/// a link to dumpasync will be generated. If false and if DML is supported, a link to dumpmt
/// will be generated.
/// </param>
private void WriteMethodTable(ulong mt, bool asyncObject)
{
string completed = IncludeCompleted ? "--completed" : "";
string tasks = IncludeTasks ? "--tasks" : "";
switch ((Console.SupportsDml, asyncObject, IntPtr.Size))
{
case (false, _, 4):
Console.Write($"{mt,16:x8}");
break;
case (false, _, 8):
Console.Write($"{mt:x16}");
break;
case (true, true, 4):
Console.WriteDml($"<exec cmd=\"{DmlCommandInvoke} --methodtable 0x{mt:x8} {tasks} {completed}\">{mt,16:x8}</exec>");
break;
case (true, true, 8):
Console.WriteDml($"<exec cmd=\"{DmlCommandInvoke} --methodtable 0x{mt:x16} {tasks} {completed}\">{mt:x16}</exec>");
break;
case (true, false, 4):
Console.WriteDml($"<exec cmd=\"!DumpMT /d 0x{mt:x8}\">{mt,16:x8}</exec>");
break;
case (true, false, 8):
Console.WriteDml($"<exec cmd=\"!DumpMT /d 0x{mt:x16}\">{mt:x16}</exec>");
break;
}
}
/// <summary>Writes out an object address. If DML is supported, this will be linked.</summary>
/// <param name="addr">The object address.</param>
/// <param name="asyncObject">
/// true if this is an async-related object; otherwise, false. If true and if DML is supported,
/// a link to dumpasync will be generated. If false and if DML is supported, a link to dumpobj
/// will be generated.
/// </param>
private void WriteAddress(ulong addr, bool asyncObject)
{
string completed = IncludeCompleted ? "--completed" : "";
string tasks = IncludeTasks ? "--tasks" : "";
switch ((Console.SupportsDml, asyncObject, IntPtr.Size))
{
case (false, _, 4):
Console.Write($"{addr,16:x8}");
break;
case (false, _, 8):
Console.Write($"{addr:x16}");
break;
case (true, true, 4):
Console.WriteDml($"<exec cmd=\"{DmlCommandInvoke} --address 0x{addr:x8} {tasks} {completed} --fields\">{addr,16:x8}</exec>");
break;
case (true, true, 8):
Console.WriteDml($"<exec cmd=\"{DmlCommandInvoke} --address 0x{addr:x16} {tasks} {completed} --fields\">{addr:x16}</exec>");
break;
case (true, false, 4):
Console.WriteDml($"<exec cmd=\"!DumpObj /d 0x{addr:x8}\">{addr,16:x8}</exec>");
break;
case (true, false, 8):
Console.WriteDml($"<exec cmd=\"!DumpObj /d 0x{addr:x16}\">{addr:x16}</exec>");
break;
}
}
/// <summary>Writes out a value type address. If DML is supported, this will be linked.</summary>
/// <param name="addr">The value type's address.</param>
/// <param name="mt">The value type's method table address.</param>
private void WriteValueTypeAddress(ulong addr, ulong mt)
{
switch ((Console.SupportsDml, IntPtr.Size))
{
case (false, 4):
Console.Write($"{addr,16:x8}");
break;
case (false, 8):
Console.Write($"{addr:x16}");
break;
case (true, 4):
Console.WriteDml($"<exec cmd=\"!DumpVC /d 0x{mt:x8} 0x{addr:x8}\">{addr,16:x8}</exec>");
break;
case (true, 8):
Console.WriteDml($"<exec cmd=\"!DumpVC /d 0x{mt:x16} 0x{addr:x16}\">{addr:x16}</exec>");
break;
}
}
/// <summary>Writes out a link that should open the source code for an address, if available.</summary>
/// <remarks>If DML is not supported, this is a nop.</remarks>
private void WriteCodeLink(ulong address)
{
if (address != 0 && address != ulong.MaxValue &&
Console.SupportsDml)
{
Console.WriteDml($" <link cmd=\".open -a 0x{address:x}\" alt=\"Source link\">@ {address:x}</link>");
}
}
/// <summary>Gets whether the specified type is an AsyncStateMachineBox{T}.</summary>
private static bool IsStateMachineBox(ClrType? type)
{
// Ideally we would compare the metadata token and module for the generic template for the type,
// but that information isn't fully available via ClrMd, nor can it currently find DebugFinalizableAsyncStateMachineBox
// due to various limitations. So we're left with string comparison.
const string Prefix = "System.Runtime.CompilerServices.AsyncTaskMethodBuilder<";
return
type?.Name is string name &&
name.StartsWith(Prefix, StringComparison.Ordinal) &&
name.IndexOf("AsyncStateMachineBox", Prefix.Length, StringComparison.Ordinal) >= 0;
}
/// <summary>Tries to get the compiler-generated state machine instance from a state machine box.</summary>
private static bool TryGetStateMachine(ClrObject obj, out IAddressableTypedEntity? stateMachine)
{
// AsyncStateMachineBox<T> has a StateMachine field storing the compiler-generated instance.
if (obj.Type?.GetFieldByName("StateMachine") is ClrInstanceField field)
{
if (field.IsValueType)
{
if (obj.ReadValueTypeField("StateMachine") is ClrValueType { IsValid: true } t)
{
stateMachine = t;
return true;
}
}
else if (field.ReadObject(obj.Address, interior: false) is ClrObject { IsValid: true } t)
{
stateMachine = t;
return true;
}
}
stateMachine = null;
return false;
}
/// <summary>Extract from the specified field of the specified object something that can be ToString'd.</summary>
private static object GetDisplay(IAddressableTypedEntity obj, ClrInstanceField field)
{
if (field.Name is string fieldName)
{
switch (field.ElementType)
{
case ClrElementType.Boolean:
return obj.ReadField<bool>(fieldName) ? "true" : "false";
case ClrElementType.Char:
char c = obj.ReadField<char>(fieldName);
return c >= 32 && c < 127 ? $"'{c}'" : $"'\\u{(int)c:X4}'";
case ClrElementType.Int8:
return obj.ReadField<sbyte>(fieldName);
case ClrElementType.UInt8:
return obj.ReadField<byte>(fieldName);
case ClrElementType.Int16:
return obj.ReadField<short>(fieldName);
case ClrElementType.UInt16:
return obj.ReadField<ushort>(fieldName);
case ClrElementType.Int32:
return obj.ReadField<int>(fieldName);
case ClrElementType.UInt32:
return obj.ReadField<uint>(fieldName);
case ClrElementType.Int64:
return obj.ReadField<long>(fieldName);
case ClrElementType.UInt64:
return obj.ReadField<ulong>(fieldName);
case ClrElementType.Float:
return obj.ReadField<float>(fieldName);
case ClrElementType.Double:
return obj.ReadField<double>(fieldName);
case ClrElementType.String:
return $"\"{obj.ReadStringField(fieldName)}\"";
case ClrElementType.Pointer:
case ClrElementType.NativeInt:
case ClrElementType.NativeUInt:
case ClrElementType.FunctionPointer:
return obj.ReadField<ulong>(fieldName).ToString(IntPtr.Size == 8 ? "x16" : "x8");
case ClrElementType.SZArray:
ClrObject arrayObj = obj.ReadObjectField(fieldName);
if (!arrayObj.IsNull)
{
ClrArray arrayObjAsArray = arrayObj.AsArray();
return $"{arrayObj.Type?.ComponentType?.ToString() ?? "unknown"}[{arrayObjAsArray.Length}]";
}
return "null";