-
Notifications
You must be signed in to change notification settings - Fork 3
/
StackTraceHelper.cs
205 lines (178 loc) · 6 KB
/
StackTraceHelper.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using HarmonyLib;
using ImperialPlugins.UnturnedProfiler.Commands;
using ImperialPlugins.UnturnedProfiler.Extensions;
using UnityEngine;
using Logger = Rocket.Core.Logging.Logger;
namespace ImperialPlugins.UnturnedProfiler
{
public static class StackTraceHelper
{
private static readonly Dictionary<int, List<MethodBase>> s_Stack = new Dictionary<int, List<MethodBase>>();
private static BindingFlags m_BindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
private static readonly HarmonyMethod s_PrefixMethod;
private static readonly HarmonyMethod s_PostfixMethod;
private static readonly ThreadLocal<int> s_NoLogAreas = new ThreadLocal<int>(() => 0);
static StackTraceHelper()
{
var preExecutionMethod = typeof(StackTraceHelper).GetMethod(nameof(OnPreExecution), BindingFlags.NonPublic | BindingFlags.Static);
s_PrefixMethod = new HarmonyMethod(preExecutionMethod);
var postExecutionMethod = typeof(StackTraceHelper).GetMethod(nameof(OnPostExecution), BindingFlags.NonPublic | BindingFlags.Static);
s_PostfixMethod = new HarmonyMethod(postExecutionMethod);
}
public static IEnumerable<MethodBase> GetStackTrace()
{
return GetStackTrace(Thread.CurrentThread);
}
public static IEnumerable<MethodBase> GetStackTrace(Thread thread)
{
if (!s_Stack.ContainsKey(thread.ManagedThreadId))
{
return Enumerable.Empty<MethodBase>();
}
return s_Stack[thread.ManagedThreadId];
}
public static void EnterNoLogArea()
{
s_NoLogAreas.Value++;
}
public static void LeaveNoLogArea()
{
s_NoLogAreas.Value--;
}
public static void RegisterAssemblyForStacktracePatch(Assembly assembly)
{
if (ExcludedAssemblies
.Concat(new[]{
"UnityEngine",
"Assembly-CSharp"
})
.Any(c => assembly.FullName.Contains(c)))
{
return;
}
ICollection<Type> types;
try
{
types = assembly.GetAllTypes();
}
catch (ReflectionTypeLoadException ex)
{
types = ex.Types;
}
var selfAssembly = typeof(ProfilerPlugin).Assembly;
foreach (var type in types)
{
// whitelist /freezeserver
if (assembly == selfAssembly && type != typeof(CommandFreezeServer))
{
continue;
}
foreach (var method in type.GetMethods(m_BindingFlags))
{
RegisterMethod(assembly, method);
}
foreach (var constructor in type.GetConstructors(m_BindingFlags))
{
RegisterMethod(assembly, constructor);
}
foreach (var property in type.GetProperties(m_BindingFlags))
{
var getter = property.GetGetMethod(true);
if (getter != null)
{
RegisterMethod(assembly, getter);
}
var setter = property.GetGetMethod(true);
if (setter != null)
{
RegisterMethod(assembly, setter);
}
}
}
}
private static void RegisterMethod(Assembly asm, MethodBase method)
{
var pluginInstance = ProfilerPlugin.Instance;
try
{
pluginInstance.Harmony.Patch(method, s_PrefixMethod, s_PostfixMethod);
}
catch
{
// ignored
}
}
private static void OnPreExecution(MethodBase __originalMethod)
{
if (s_NoLogAreas.Value > 0)
{
return;
}
EnterNoLogArea();
try
{
var threadId = Thread.CurrentThread.ManagedThreadId;
if (!s_Stack.ContainsKey(threadId))
{
s_Stack.Add(threadId, new List<MethodBase>());
}
var stack = s_Stack[threadId];
if (stack.Count >= 10)
{
stack.RemoveAt(0);
}
stack.Add(__originalMethod);
}
finally
{
LeaveNoLogArea();
}
}
private static void OnPostExecution(MethodBase __originalMethod)
{
if (s_NoLogAreas.Value > 0)
{
return;
}
EnterNoLogArea();
try
{
if (!s_Stack.ContainsKey(Thread.CurrentThread.ManagedThreadId))
{
return;
}
var stack = s_Stack[Thread.CurrentThread.ManagedThreadId];
var idx = stack.Count - 1;
if (stack.Count == 0 || stack[idx] != __originalMethod)
{
return;
}
stack.RemoveAt(idx);
}
finally
{
LeaveNoLogArea();
}
}
public static string[] ExcludedAssemblies = {
"Harmony",
"System",
"Mono",
"Microsoft",
"Newtonsoft",
"Pathfinding",
"UnityEngine.UI",
"UnityEngine.Video",
"UnityEngine.Networking",
"UnityEngine.Timeline",
"UnityEngine.PostProcessing",
"AstarPath",
"mscorlib"
};
}
}