|
5 | 5 |
|
6 | 6 | #if DEBUG
|
7 | 7 | using System.Diagnostics;
|
| 8 | +using System.IO; |
8 | 9 | using System.Runtime.InteropServices;
|
9 | 10 | #endif
|
10 | 11 |
|
11 | 12 | namespace Rubberduck.VBEditor.ComManagement
|
12 | 13 | {
|
13 | 14 | public abstract class ComSafeBase : IComSafe
|
14 | 15 | {
|
15 |
| -#if DEBUG |
16 |
| - protected IEnumerable<string> Trace = null; |
17 |
| -#endif |
18 |
| - |
19 | 16 | public abstract void Add(ISafeComWrapper comWrapper);
|
20 | 17 |
|
| 18 | + public abstract bool TryRemove(ISafeComWrapper comWrapper); |
| 19 | + |
21 | 20 | //We do not use GetHashCode because subclasses of SafeComWrapper<T> overwrite this method
|
22 | 21 | //and we need to distinguish between individual instances.
|
23 | 22 | protected int GetComWrapperObjectHashCode(ISafeComWrapper comWrapper)
|
24 | 23 | {
|
25 | 24 | return RuntimeHelpers.GetHashCode(comWrapper);
|
26 | 25 | }
|
27 | 26 |
|
28 |
| - public abstract bool TryRemove(ISafeComWrapper comWrapper); |
29 |
| - |
| 27 | + private bool _disposed; |
30 | 28 | public void Dispose()
|
31 | 29 | {
|
32 | 30 | Dispose(true);
|
| 31 | + |
| 32 | +#if DEBUG |
| 33 | + if (_disposed) |
| 34 | + { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + _disposed = true; |
| 39 | + |
| 40 | + lock (_streamLock) |
| 41 | + { |
| 42 | + try |
| 43 | + { |
| 44 | + if (_traceStream == null) |
| 45 | + { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + _traceStream.Close(); |
| 50 | + if (string.IsNullOrWhiteSpace(_directory)) |
| 51 | + { |
| 52 | + File.Delete(_traceFilePath); |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + File.Move(_traceFilePath, |
| 57 | + Path.Combine(_directory, |
| 58 | + Path.GetFileNameWithoutExtension(_traceFilePath) + " final.csv")); |
| 59 | + } |
| 60 | + } |
| 61 | + finally |
| 62 | + { |
| 63 | + _traceStream?.Dispose(); |
| 64 | + _traceStream = null; |
| 65 | + } |
| 66 | + } |
| 67 | +#endif |
33 | 68 | }
|
34 | 69 |
|
35 | 70 | protected abstract void Dispose(bool disposing);
|
36 | 71 |
|
37 | 72 | #if DEBUG
|
| 73 | + private struct TraceData |
| 74 | + { |
| 75 | + internal int HashCode { get; set; } |
| 76 | + internal string IUnknownAddress { get; set; } |
| 77 | + internal IEnumerable<string> StackTrace { get; set; } |
| 78 | + } |
| 79 | + private StreamWriter _traceStream; |
| 80 | + private string _traceFilePath; |
| 81 | + private string _directory; |
| 82 | + private readonly object _streamLock = new object(); |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// The first few stack frames come from the ComSafe and thus are not |
| 86 | + /// particularly interesting. Typically, we want to look at the frames |
| 87 | + /// outside the ComSafe. |
| 88 | + /// </summary> |
| 89 | + private const int StackTraceNumberOfElementsToSkipOnRemoval = 6; |
| 90 | + private const int StackTrackNumberOfElementsToSkipOnAddUpdate = 8; |
| 91 | + private const int StackTraceDepth = 5; |
| 92 | + |
38 | 93 | /// <summary>
|
39 | 94 | /// Provide a serialized list of the COM Safe
|
40 | 95 | /// to make it easy to analyze what is inside
|
41 | 96 | /// the COM Safe at the different points of
|
42 | 97 | /// the session's lifetime.
|
43 | 98 | /// </summary>
|
44 |
| - public void Serialize() |
| 99 | + public void Serialize(string targetDirectory) |
| 100 | + { |
| 101 | + lock (_streamLock) |
| 102 | + { |
| 103 | + _directory = targetDirectory; |
| 104 | + var serializeTime = DateTime.UtcNow; |
| 105 | + using (var stream = File.AppendText(Path.Combine(_directory, |
| 106 | + $"COM Safe Content Snapshot {serializeTime:yyyyMMddhhmmss}.csv"))) |
| 107 | + { |
| 108 | + stream.WriteLine( |
| 109 | + $"Ordinal\tKey\tCOM Wrapper Type\tWrapping Null?\tIUnknown Pointer Address"); |
| 110 | + var i = 0; |
| 111 | + foreach (var kvp in GetWrappers()) |
| 112 | + { |
| 113 | + var line = kvp.Value != null |
| 114 | + ? $"{i++}\t{kvp.Key}\t\"{kvp.Value.GetType().FullName}\"\t\"{kvp.Value.IsWrappingNullReference}\"\t\"{(kvp.Value.IsWrappingNullReference ? "null" : GetPtrAddress(kvp.Value.Target))}\"" |
| 115 | + : $"{i++}\t{kvp.Key}\t\"null\"\t\"null\"\t\"null\""; |
| 116 | + stream.WriteLine(line); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if (_traceStream == null) |
| 121 | + { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + _traceStream.Flush(); |
| 126 | + File.Copy(_traceFilePath, Path.Combine(_directory, $"COM Safe Stack Trace {serializeTime:yyyyMMddhhmmss}.csv")); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + protected void TraceAdd(ISafeComWrapper comWrapper) |
| 131 | + { |
| 132 | + Trace("Add", comWrapper, StackTrackNumberOfElementsToSkipOnAddUpdate); |
| 133 | + } |
| 134 | + |
| 135 | + protected void TraceUpdate(ISafeComWrapper comWrapper) |
45 | 136 | {
|
46 |
| - using (var stream = System.IO.File.AppendText($"comSafeOutput {DateTime.UtcNow:yyyyMMddhhmmss}.csv")) |
| 137 | + Trace("Update", comWrapper, StackTrackNumberOfElementsToSkipOnAddUpdate); |
| 138 | + } |
| 139 | + |
| 140 | + protected void TraceRemove(ISafeComWrapper comWrapper, bool wasRemoved) |
| 141 | + { |
| 142 | + var activity = wasRemoved ? "Removed" : "Not removed"; |
| 143 | + Trace(activity, comWrapper, StackTraceNumberOfElementsToSkipOnRemoval); |
| 144 | + } |
| 145 | + |
| 146 | + private readonly object _idLock = new object(); |
| 147 | + private int _id; |
| 148 | + private void Trace(string activity, ISafeComWrapper comWrapper, int framesToSkip) |
| 149 | + { |
| 150 | + lock (_streamLock) |
47 | 151 | {
|
48 |
| - stream.WriteLine( |
49 |
| - "Ordinal\tKey\tCOM Wrapper Type\tWrapping Null?\tIUnknown Pointer Address\tLevel 1\tLevel 2\tLevel 3"); |
50 |
| - var i = 0; |
51 |
| - foreach (var kvp in GetWrappers()) |
| 152 | + if (_disposed) |
| 153 | + { |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + if (_traceStream == null) |
| 158 | + { |
| 159 | + var directory = Path.GetTempPath(); |
| 160 | + _traceFilePath = Path.Combine(directory, |
| 161 | + $"COM Safe Stack Trace {DateTime.UtcNow:yyyyMMddhhmmss}.{GetHashCode()}.csv"); |
| 162 | + _traceStream = File.AppendText(_traceFilePath); |
| 163 | + _traceStream.WriteLine( |
| 164 | + $"Ordinal\tTimestamp\tActivity\tKey\tIUnknown Pointer Address\t{FrameHeaders()}"); |
| 165 | + } |
| 166 | + |
| 167 | + int id; |
| 168 | + lock (_idLock) |
52 | 169 | {
|
53 |
| - var line = kvp.Value != null |
54 |
| - ? $"{i++}\t{kvp.Key}\t\"{kvp.Value.GetType().FullName}\"\t\"{kvp.Value.IsWrappingNullReference}\"\t\"{(kvp.Value.IsWrappingNullReference ? "null" : GetPtrAddress(kvp.Value))}\"\t\"{string.Join("\"\t\"", Trace)}\"" |
55 |
| - : $"{i++}\t{kvp.Key}\t\"null\"\t\"null\"\t\"null\"\t\"{string.Join("\"\t\"", Trace)}\""; |
56 |
| - stream.WriteLine(line); |
| 170 | + id = _id++; |
57 | 171 | }
|
| 172 | + |
| 173 | + var traceData = new TraceData |
| 174 | + { |
| 175 | + HashCode = GetComWrapperObjectHashCode(comWrapper), |
| 176 | + IUnknownAddress = comWrapper.IsWrappingNullReference ? "null" : GetPtrAddress(comWrapper.Target), |
| 177 | + StackTrace = GetStackTrace(StackTraceDepth, framesToSkip) |
| 178 | + }; |
| 179 | + |
| 180 | + var line = |
| 181 | + $"{id}\t{DateTime.UtcNow}\t\"{activity}\"\t{traceData.HashCode}\t{traceData.IUnknownAddress}\t\"{string.Join("\"\t\"", traceData.StackTrace)}\""; |
| 182 | + _traceStream.WriteLine(line); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + private static string FrameHeaders() |
| 187 | + { |
| 188 | + var headers = new System.Text.StringBuilder(); |
| 189 | + for(var i = 1; i <= StackTraceDepth; i++) |
| 190 | + { |
| 191 | + headers.Append($"Frame {i}\t"); |
58 | 192 | }
|
| 193 | + |
| 194 | + return headers.ToString(); |
59 | 195 | }
|
60 | 196 |
|
61 | 197 | protected abstract IDictionary<int, ISafeComWrapper> GetWrappers();
|
62 | 198 |
|
63 |
| - protected static IEnumerable<string> GetStackTrace(int frames, int offset) |
| 199 | + private static IEnumerable<string> GetStackTrace(int frames, int framesToSkip) |
64 | 200 | {
|
65 | 201 | var list = new List<string>();
|
66 | 202 | var trace = new StackTrace();
|
67 |
| - if ((trace.FrameCount - offset) < frames) |
| 203 | + if (trace.FrameCount < (frames + framesToSkip)) |
| 204 | + { |
| 205 | + frames = trace.FrameCount; |
| 206 | + } |
| 207 | + else |
68 | 208 | {
|
69 |
| - frames = (trace.FrameCount - offset); |
| 209 | + frames += framesToSkip; |
70 | 210 | }
|
71 | 211 |
|
72 |
| - for (var i = 1; i <= frames; i++) |
| 212 | + framesToSkip -= 1; |
| 213 | + frames -= 1; |
| 214 | + |
| 215 | + for (var i = framesToSkip; i < frames; i++) |
73 | 216 | {
|
74 |
| - var frame = trace.GetFrame(i + offset); |
75 |
| - var typeName = frame.GetMethod().DeclaringType?.FullName ?? string.Empty; |
| 217 | + var frame = trace.GetFrame(i); |
| 218 | + var type = frame.GetMethod().DeclaringType; |
| 219 | + |
| 220 | + var typeName = type?.FullName ?? string.Empty; |
76 | 221 | var methodName = frame.GetMethod().Name;
|
77 | 222 |
|
78 | 223 | var qualifiedName = $"{typeName}{(typeName.Length > 0 ? "::" : string.Empty)}{methodName}";
|
|
0 commit comments