forked from Unity-Technologies/UnityMixedCallstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnityMixedCallstackFilter.cs
188 lines (154 loc) · 6.96 KB
/
UnityMixedCallstackFilter.cs
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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Debugger;
using Microsoft.VisualStudio.Debugger.CallStack;
using Microsoft.VisualStudio.Debugger.ComponentInterfaces;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
namespace UnityMixedCallstack
{
public class UnityMixedCallstackFilter : IDkmCallStackFilter, IDkmLoadCompleteNotification, IDkmModuleInstanceLoadNotification
{
private static List<Range> _rangesSortedByIp = new List<Range>();
private static FuzzyRangeComparer _comparer = new FuzzyRangeComparer();
private static bool _enabled;
private static IVsOutputWindowPane _debugPane;
private static string _currentFile;
private static FileStream _fileStream;
private static StreamReader _fileStreamReader;
public void OnLoadComplete(DkmProcess process, DkmWorkList workList, DkmEventDescriptor eventDescriptor)
{
DisposeStreams();
if (_debugPane == null)
{
IVsOutputWindow outWindow = Package.GetGlobalService(typeof(SVsOutputWindow)) as IVsOutputWindow;
Guid debugPaneGuid = VSConstants.GUID_OutWindowDebugPane;
outWindow?.GetPane(ref debugPaneGuid, out _debugPane);
}
}
public DkmStackWalkFrame[] FilterNextFrame(DkmStackContext stackContext, DkmStackWalkFrame input)
{
if (input == null) // after last frame
return null;
if (input.InstructionAddress == null) // error case
return new[] { input };
if (input.InstructionAddress.ModuleInstance != null && input.InstructionAddress.ModuleInstance.Module != null) // code in existing module
return new[] { input };
if (!_enabled) // environment variable not set
return new[] { input };
return new[] { UnityMixedStackFrame(stackContext, input) };
}
private static DkmStackWalkFrame UnityMixedStackFrame(DkmStackContext stackContext, DkmStackWalkFrame frame)
{
RefreshStackData(frame.Process.LivePart.Id);
string name = null;
if (TryGetDescriptionForIp(frame.InstructionAddress.CPUInstructionPart.InstructionPointer, out name))
return DkmStackWalkFrame.Create(
stackContext.Thread,
frame.InstructionAddress,
frame.FrameBase,
frame.FrameSize,
frame.Flags,
name,
frame.Registers,
frame.Annotations);
return frame;
}
private static int GetFileNameSequenceNum(string path)
{
var name = Path.GetFileNameWithoutExtension(path);
const char delemiter = '_';
var tokens = name.Split(delemiter);
if (tokens.Length != 3)
return -1;
return int.Parse(tokens[2]);
}
private static void DisposeStreams()
{
_fileStreamReader?.Dispose();
_fileStreamReader = null;
_fileStream?.Dispose();
_fileStream = null;
_currentFile = null;
_rangesSortedByIp.Clear();
}
private static void RefreshStackData(int pid)
{
DirectoryInfo taskDirectory = new DirectoryInfo(Path.GetTempPath());
FileInfo[] taskFiles = taskDirectory.GetFiles("pmip_" + pid + "_*.txt");
if (taskFiles.Length < 1)
return;
Array.Sort(taskFiles, (a, b) => GetFileNameSequenceNum(a.Name).CompareTo(GetFileNameSequenceNum(b.Name)));
var fileName = taskFiles[taskFiles.Length - 1].FullName;
if (_currentFile != fileName)
{
DisposeStreams();
try
{
_fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
_fileStreamReader = new StreamReader(_fileStream);
_currentFile = fileName;
var versionStr = _fileStreamReader.ReadLine();
const char delimiter = ':';
var tokens = versionStr.Split(delimiter);
if (tokens.Length != 2)
throw new Exception("Failed reading input file " + fileName + ": Incorrect format");
var version = double.Parse(tokens[1]);
if(version > 1.0)
throw new Exception("Failed reading input file " + fileName + ": A newer version of UnityMixedCallstacks plugin is required to read this file");
}
catch (Exception ex)
{
_debugPane?.OutputString("Unable to read dumped pmip file: " + ex.Message + "\n");
DisposeStreams();
_enabled = false;
return;
}
}
try
{
string line;
while ((line = _fileStreamReader.ReadLine()) != null)
{
const char delemiter = ';';
var tokens = line.Split(delemiter);
//should never happen, but lets be safe and not get array out of bounds if it does
if (tokens.Length != 3)
continue;
var startip = tokens[0];
var endip = tokens[1];
var description = tokens[2];
var startiplong = ulong.Parse(startip, NumberStyles.HexNumber);
var endipint = ulong.Parse(endip, NumberStyles.HexNumber);
_rangesSortedByIp.Add(new Range() { Name = description, Start = startiplong, End = endipint });
}
}
catch (Exception ex)
{
_debugPane?.OutputString("Unable to read dumped pmip file: " + ex.Message + "\n");
DisposeStreams();
_enabled = false;
return;
}
_rangesSortedByIp.Sort((r1, r2) => r1.Start.CompareTo(r2.Start));
}
private static bool TryGetDescriptionForIp(ulong ip, out string name)
{
name = string.Empty;
var rangeToFindIp = new Range() { Start = ip };
var index = _rangesSortedByIp.BinarySearch(rangeToFindIp, _comparer);
if (index < 0)
return false;
name = _rangesSortedByIp[index].Name;
return true;
}
public void OnModuleInstanceLoad(DkmModuleInstance moduleInstance, DkmWorkList workList, DkmEventDescriptorS eventDescriptor)
{
if (moduleInstance.Name.Contains("mono-2.0") && moduleInstance.MinidumpInfoPart == null)
_enabled = true;
}
}
}