|
| 1 | +using MemoScope.Core; |
| 2 | +using MemoScope.Core.Data; |
| 3 | +using Microsoft.Diagnostics.Runtime; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Threading; |
| 7 | +using System.Windows.Forms; |
| 8 | +using WinFwk.UIMessages; |
| 9 | +using WinFwk.UIModules; |
| 10 | + |
| 11 | +namespace MemoScope.Modules.Referers |
| 12 | +{ |
| 13 | + public static class ReferersAnalysis |
| 14 | + { |
| 15 | + public static bool HasReferers(MessageBus msgBus, ClrDump clrDump, IEnumerable<ulong> addresses) |
| 16 | + { |
| 17 | + foreach(ulong address in addresses) { |
| 18 | + if (clrDump.HasReferers(address)) |
| 19 | + { |
| 20 | + return true; |
| 21 | + } |
| 22 | + } |
| 23 | + return false; |
| 24 | + } |
| 25 | + |
| 26 | + public static List<ReferersInformation> AnalyzeReferers(MessageBus msgBus, ClrDump clrDump, HashSet<ulong> addresses) |
| 27 | + { |
| 28 | + var referers = new List<ReferersInformation>(); |
| 29 | + var dico = new Dictionary<ClrType, Dictionary<string, ReferersInformation>>(); |
| 30 | + CancellationTokenSource token = new CancellationTokenSource(); |
| 31 | + msgBus.BeginTask("Analyzing referers...", token); |
| 32 | + Application.DoEvents(); // todo: avoid this call to Application.DoEvents() |
| 33 | + int count = addresses.Count; |
| 34 | + int i = 0; |
| 35 | + foreach(var address in addresses) |
| 36 | + { |
| 37 | + i++; |
| 38 | + if( token.IsCancellationRequested) |
| 39 | + { |
| 40 | + msgBus.EndTask("Referers analyze: cancelled."); |
| 41 | + return referers; |
| 42 | + } |
| 43 | + if ( i % 1024 == 0) |
| 44 | + { |
| 45 | + msgBus.Status($"Analyzing referers: {(double)i/count:p2}, {i:###,###,###,##0} / {count:###,###,###,##0}..."); |
| 46 | + Application.DoEvents();// todo: avoid this call to Application.DoEvents() |
| 47 | + } |
| 48 | + foreach( var refererAddress in clrDump.EnumerateReferers(address)) |
| 49 | + { |
| 50 | + var type = clrDump.GetObjectType(refererAddress); |
| 51 | + string field; |
| 52 | + if (type.IsArray) |
| 53 | + { |
| 54 | + field = "[ * ]"; |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + field = clrDump.GetFieldNameReference(address, refererAddress); |
| 59 | + } |
| 60 | + Dictionary<string, ReferersInformation> toto; |
| 61 | + if( ! dico.TryGetValue(type, out toto)) |
| 62 | + { |
| 63 | + toto = new Dictionary<string, ReferersInformation>(); |
| 64 | + dico[type] = toto; |
| 65 | + } |
| 66 | + |
| 67 | + ReferersInformation referersInformation; |
| 68 | + if ( ! toto.TryGetValue(field, out referersInformation)) |
| 69 | + { |
| 70 | + referersInformation = new ReferersInformation(clrDump, type, field, msgBus); |
| 71 | + toto[field] = referersInformation; |
| 72 | + } |
| 73 | + |
| 74 | + referersInformation.References.Add(address); |
| 75 | + referersInformation.Instances.Add(refererAddress); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + foreach(var kvpType in dico) |
| 80 | + { |
| 81 | + var type = kvpType.Key; |
| 82 | + foreach(var kvpField in kvpType.Value) |
| 83 | + { |
| 84 | + var refInfo = kvpField.Value; |
| 85 | + referers.Add(refInfo); |
| 86 | + refInfo.Init(); |
| 87 | + } |
| 88 | + } |
| 89 | + msgBus.EndTask("Referers analyzed."); |
| 90 | + Application.DoEvents();// todo: avoid this call to Application.DoEvents() |
| 91 | + return referers; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments