forked from jbevain/PmipMyCallStack
-
Notifications
You must be signed in to change notification settings - Fork 10
/
UnityMixedCallstackFilter.cs
181 lines (157 loc) · 6.73 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
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;
using UnityMixedCallStack;
namespace UnityMixedCallstack
{
public class UnityMixedCallstackFilter : IDkmCallStackFilter, IDkmLoadCompleteNotification, IDkmModuleInstanceLoadNotification
{
private static bool _enabled;
public static IVsOutputWindowPane _debugPane;
private static Dictionary<int, PmipFile> _currentFiles = new Dictionary<int, PmipFile>();
struct PmipFile
{
public int count;
public int pid;
public string path;
}
public void OnLoadComplete(DkmProcess process, DkmWorkList workList, DkmEventDescriptor eventDescriptor)
{
PmipReader.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 };
try
{
DkmStackWalkFrame[] retVal = new[] { UnityMixedStackFrame(stackContext, input) };
return retVal;
} catch (Exception ex)
{
_debugPane?.OutputString("UNITYMIXEDCALLSTACK :: ip : " + input.Process.LivePart.Id + " threw exception: " + ex.Message + "\n" + ex.StackTrace);
}
return new[] { input };
}
private static DkmStackWalkFrame UnityMixedStackFrame(DkmStackContext stackContext, DkmStackWalkFrame frame)
{
RefreshStackData(frame.Process.LivePart.Id);
#if DEBUG
_debugPane?.OutputString($"UNITYMIXEDCALLSTACK :: done refreshing data for pid({frame.Process.LivePart.Id}) :: looking for address: {frame.InstructionAddress.CPUInstructionPart.InstructionPointer:X16}\n");
#endif
string name = null;
if (PmipReader.TryGetDescriptionForIp(frame.InstructionAddress.CPUInstructionPart.InstructionPointer, out name))
{
#if DEBUG
_debugPane?.OutputString($"ip: {frame.InstructionAddress.CPUInstructionPart.InstructionPointer:X16} ### {name}\n");
#endif
return DkmStackWalkFrame.Create(
stackContext.Thread,
frame.InstructionAddress,
frame.FrameBase,
frame.FrameSize,
frame.Flags,
name,
frame.Registers,
frame.Annotations);
}
#if DEBUG
else
_debugPane?.OutputString($"IP not found: {frame.InstructionAddress.CPUInstructionPart.InstructionPointer:X16}\n");
#endif
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 bool CheckForUpdatedFiles(FileInfo[] taskFiles)
{
bool retVal = false;
try
{
foreach (FileInfo taskFile in taskFiles)
{
string fName = Path.GetFileNameWithoutExtension(taskFile.Name);
string[] tokens = fName.Split('_');
PmipFile pmipFile = new PmipFile()
{
count = int.Parse(tokens[2]),
pid = int.Parse(tokens[1]),
path = taskFile.FullName
};
// 3 is legacy and treat everything as root domain
if (tokens.Length == 3 &&
(!_currentFiles.TryGetValue(0, out PmipFile curFile) ||
curFile.count < pmipFile.count || curFile.pid != pmipFile.pid))
{
_currentFiles[0] = pmipFile;
retVal = true;
}
else if (tokens.Length == 4)
{
int domainID = int.Parse(tokens[3]);
if (!_currentFiles.TryGetValue(domainID, out PmipFile cFile) || cFile.count < pmipFile.count ||
cFile.pid != pmipFile.pid)
{
_currentFiles[domainID] = pmipFile;
retVal = true;
}
}
}
} catch (Exception e)
{
_debugPane?.OutputString("MIXEDCALLSTACK :: Exception thrown during CheckForUpdatedFiles: " + e.Message + "\n");
_enabled = false;
}
return retVal;
}
private static void RefreshStackData(int pid)
{
DirectoryInfo taskDirectory = new DirectoryInfo(Path.GetTempPath());
FileInfo[] taskFiles = taskDirectory.GetFiles("pmip_" + pid + "_*.txt");
if (taskFiles.Length < 1)
return;
if (CheckForUpdatedFiles(taskFiles))
PmipReader.DisposeStreams();
foreach (PmipFile pmipFile in _currentFiles.Values)
{
_enabled = PmipReader.ReadPmipFile(pmipFile.path);
if (!_enabled)
{
_debugPane?.OutputString($"Unable to read file: {pmipFile.path}\n");
}
}
PmipReader.Sort();
}
public void OnModuleInstanceLoad(DkmModuleInstance moduleInstance, DkmWorkList workList, DkmEventDescriptorS eventDescriptor)
{
if (moduleInstance.Name.Contains("mono-2.0") && moduleInstance.MinidumpInfoPart == null)
_enabled = true;
}
}
}