|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Linq; |
| 7 | +using Microsoft.Diagnostics.DebugServices; |
| 8 | +using Microsoft.Diagnostics.Runtime; |
| 9 | +using static Microsoft.Diagnostics.ExtensionCommands.TableOutput; |
| 10 | + |
| 11 | +namespace Microsoft.Diagnostics.ExtensionCommands |
| 12 | +{ |
| 13 | + [Command(Name = "ephtoloh", Help = "Finds ephemeral objects which reference the large object heap.")] |
| 14 | + public class FindEphemeralReferencesToLOHCommand : CommandBase |
| 15 | + { |
| 16 | + // IComparer for binary search |
| 17 | + private readonly IComparer<(ClrObject, ClrObject)> _firstObjectComparer = Comparer<(ClrObject, ClrObject)>.Create((x, y) => x.Item1.Address.CompareTo(y.Item1.Address)); |
| 18 | + |
| 19 | + [ServiceImport] |
| 20 | + public ClrRuntime Runtime { get; set; } |
| 21 | + |
| 22 | + public override void Invoke() |
| 23 | + { |
| 24 | + int segments = Runtime.Heap.Segments.Count(seg => seg.Kind is not GCSegmentKind.Frozen or GCSegmentKind.Pinned); |
| 25 | + if (segments > 50) |
| 26 | + { |
| 27 | + string gcSegKind = Runtime.Heap.SubHeaps[0].HasRegions ? "regions" : "segments"; |
| 28 | + Console.WriteLineWarning($"Walking {segments:n0} {gcSegKind}, this may take a moment..."); |
| 29 | + } |
| 30 | + |
| 31 | + TableOutput output = new(Console, (16, "x12"), (64, ""), (16, "x12")); |
| 32 | + |
| 33 | + // Ephemeral -> Large |
| 34 | + List<(ClrObject From, ClrObject To)> ephToLoh = FindEphemeralToLOH().OrderBy(i => i.From.Address).ThenBy(i => i.To.Address).ToList(); |
| 35 | + if (ephToLoh.Count == 0) |
| 36 | + { |
| 37 | + Console.WriteLine("No Ephemeral objects pointing to the Large objects."); |
| 38 | + } |
| 39 | + else |
| 40 | + { |
| 41 | + Console.WriteLine("Ephemeral objects pointing to the Large objects:"); |
| 42 | + Console.WriteLine(); |
| 43 | + output.WriteRow("Ephemeral", "Ephemeral Type", "Large Object", "Large Object Type"); |
| 44 | + |
| 45 | + foreach ((ClrObject from, ClrObject to) in ephToLoh) |
| 46 | + { |
| 47 | + output.WriteRow(new DmlDumpObj(from), from.Type?.Name, new DmlDumpObj(to), to.Type?.Name); |
| 48 | + } |
| 49 | + |
| 50 | + Console.WriteLine(); |
| 51 | + } |
| 52 | + |
| 53 | + // Large -> Ephemeral |
| 54 | + List<(ClrObject From, ClrObject To)> lohToEph = FindLOHToEphemeral().OrderBy(i => i.From.Address).ThenBy(i => i.To.Address).ToList(); |
| 55 | + if (lohToEph.Count == 0) |
| 56 | + { |
| 57 | + Console.WriteLine("No Large objects pointing to Ephemeral objects."); |
| 58 | + } |
| 59 | + else |
| 60 | + { |
| 61 | + Console.WriteLine("Large objects pointing to Ephemeral objects:"); |
| 62 | + Console.WriteLine(); |
| 63 | + output.WriteRow("Ephemeral", "Ephemeral Type", "Large Object", "Large Object Type"); |
| 64 | + |
| 65 | + foreach ((ClrObject from, ClrObject to) in lohToEph) |
| 66 | + { |
| 67 | + output.WriteRow(new DmlDumpObj(from), from.Type?.Name, new DmlDumpObj(to), to.Type?.Name); |
| 68 | + } |
| 69 | + |
| 70 | + Console.WriteLine(); |
| 71 | + } |
| 72 | + |
| 73 | + // Ephemeral -> Large -> Ephemeral |
| 74 | + if (ephToLoh.Count != 0 && lohToEph.Count != 0) |
| 75 | + { |
| 76 | + // We'll use output to signify if we need to print a header or not. |
| 77 | + output = null; |
| 78 | + foreach ((ClrObject from, ClrObject to) in ephToLoh) |
| 79 | + { |
| 80 | + int index = lohToEph.BinarySearch((to, to), _firstObjectComparer); |
| 81 | + if (index < 0) |
| 82 | + { |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + Debug.Assert(to == lohToEph[index].From); |
| 87 | + ClrObject ephEnd = lohToEph[index].To; |
| 88 | + |
| 89 | + if (output is null) |
| 90 | + { |
| 91 | + Console.WriteLine($"Ephemeral objects which point to Large objects which point to Ephemeral objects:"); |
| 92 | + Console.WriteLine(); |
| 93 | + output = new(Console, (16, "x12"), (64, ""), (16, "x12"), (64, ""), (16, "x12")); |
| 94 | + output.WriteRow(new DmlDumpObj(from), from.Type?.Name, new DmlDumpObj(to), to.Type?.Name, new DmlDumpObj(ephEnd), ephEnd.Type?.Name); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (output is null) |
| 99 | + { |
| 100 | + Console.WriteLine("No Ephemeral objects which point to Large objects which point to Ephemeral objects."); |
| 101 | + } |
| 102 | + else |
| 103 | + { |
| 104 | + Console.WriteLine(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + foreach ((ClrObject From, ClrObject To) item in ephToLoh) |
| 109 | + { |
| 110 | + if (lohToEph.Any(r => item.To.Address == r.From.Address)) |
| 111 | + { |
| 112 | + Console.WriteLine("error!"); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private IEnumerable<(ClrObject From, ClrObject To)> FindEphemeralToLOH() |
| 118 | + { |
| 119 | + foreach (ClrSegment seg in Runtime.Heap.Segments) |
| 120 | + { |
| 121 | + Console.CancellationToken.ThrowIfCancellationRequested(); |
| 122 | + |
| 123 | + if (seg.Kind is GCSegmentKind.Frozen or GCSegmentKind.Large or GCSegmentKind.Generation2 or GCSegmentKind.Pinned) |
| 124 | + { |
| 125 | + continue; |
| 126 | + } |
| 127 | + |
| 128 | + foreach (ClrObject obj in seg.EnumerateObjects().Where(obj => obj.IsValid && obj.ContainsPointers)) |
| 129 | + { |
| 130 | + Console.CancellationToken.ThrowIfCancellationRequested(); |
| 131 | + |
| 132 | + // This handles both regions and segments |
| 133 | + Generation gen = obj.GetGeneration(seg); |
| 134 | + if (gen is not Generation.Gen0 or Generation.Gen1) |
| 135 | + { |
| 136 | + continue; |
| 137 | + } |
| 138 | + |
| 139 | + foreach (ClrObject objRef in obj.EnumerateReferences(carefully: true, considerDependantHandles: false)) |
| 140 | + { |
| 141 | + Console.CancellationToken.ThrowIfCancellationRequested(); |
| 142 | + |
| 143 | + if (!objRef.IsValid || objRef.IsFree) // heap corruption |
| 144 | + { |
| 145 | + continue; |
| 146 | + } |
| 147 | + |
| 148 | + Generation refGen = objRef.GetGeneration(null); |
| 149 | + if (refGen == Generation.Large) |
| 150 | + { |
| 151 | + yield return (obj, objRef); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private IEnumerable<(ClrObject From, ClrObject To)> FindLOHToEphemeral() |
| 159 | + { |
| 160 | + foreach (ClrSegment seg in Runtime.Heap.Segments.Where(seg => seg.Kind == GCSegmentKind.Large)) |
| 161 | + { |
| 162 | + Console.CancellationToken.ThrowIfCancellationRequested(); |
| 163 | + |
| 164 | + foreach (ClrObject obj in seg.EnumerateObjects().Where(obj => obj.IsValid && obj.ContainsPointers)) |
| 165 | + { |
| 166 | + Console.CancellationToken.ThrowIfCancellationRequested(); |
| 167 | + |
| 168 | + foreach (ClrObject objRef in obj.EnumerateReferences(carefully: true, considerDependantHandles: false)) |
| 169 | + { |
| 170 | + Console.CancellationToken.ThrowIfCancellationRequested(); |
| 171 | + |
| 172 | + if (!objRef.IsValid || objRef.IsFree) // heap corruption |
| 173 | + { |
| 174 | + continue; |
| 175 | + } |
| 176 | + |
| 177 | + Generation refGen = objRef.GetGeneration(null); |
| 178 | + if (refGen is Generation.Gen0 or Generation.Gen1) |
| 179 | + { |
| 180 | + yield return (obj, objRef); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments