Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
StackTrace.cs
Go to the documentation of this file.
7using System.Text;
8
9namespace System.Diagnostics;
10
11public class StackTrace
12{
13 internal enum TraceFormat
14 {
15 Normal,
17 }
18
19 public const int METHODS_TO_SKIP = 0;
20
21 private int _numOfFrames;
22
23 private int _methodsToSkip;
24
26
27 public virtual int FrameCount => _numOfFrames;
28
29 [MethodImpl(MethodImplOptions.InternalCall)]
30 internal static extern void GetStackFramesInternal(StackFrameHelper sfh, int iSkip, bool fNeedFileInfo, Exception e);
31
33 {
34 int num = 0;
35 for (int i = 0; i < iNumFrames; i++)
36 {
37 MethodBase methodBase = StackF.GetMethodBase(i);
38 if (methodBase != null)
39 {
40 Type declaringType = methodBase.DeclaringType;
41 if (declaringType == null)
42 {
43 break;
44 }
45 string @namespace = declaringType.Namespace;
46 if (@namespace == null || !string.Equals(@namespace, "System.Diagnostics", StringComparison.Ordinal))
47 {
48 break;
49 }
50 }
51 num++;
52 }
53 return num;
54 }
55
60
65
67 {
70 stackFrameHelper.InitializeSourceInfo(0, fNeedFileInfo, e);
71 _numOfFrames = stackFrameHelper.GetNumberOfFrames();
73 {
75 }
76 if (_numOfFrames != 0)
77 {
79 for (int i = 0; i < _numOfFrames; i++)
80 {
82 }
83 if (e == null)
84 {
86 }
88 if (_numOfFrames < 0)
89 {
90 _numOfFrames = 0;
91 }
92 }
93 }
94
95 [MethodImpl(MethodImplOptions.NoInlining)]
96 public StackTrace()
97 {
99 }
100
101 [MethodImpl(MethodImplOptions.NoInlining)]
106
107 [MethodImpl(MethodImplOptions.NoInlining)]
109 {
110 if (skipFrames < 0)
111 {
113 }
115 }
116
117 [MethodImpl(MethodImplOptions.NoInlining)]
126
128 {
129 if (e == null)
130 {
131 throw new ArgumentNullException("e");
132 }
134 }
135
137 {
138 if (e == null)
139 {
140 throw new ArgumentNullException("e");
141 }
143 }
144
146 {
147 if (e == null)
148 {
149 throw new ArgumentNullException("e");
150 }
151 if (skipFrames < 0)
152 {
154 }
156 }
157
159 {
160 if (e == null)
161 {
162 throw new ArgumentNullException("e");
163 }
164 if (skipFrames < 0)
165 {
167 }
169 }
170
171 public StackTrace(StackFrame frame)
172 {
173 _stackFrames = new StackFrame[1] { frame };
174 _numOfFrames = 1;
175 }
176
177 public virtual StackFrame? GetFrame(int index)
178 {
180 {
182 }
183 return null;
184 }
185
186 public virtual StackFrame[] GetFrames()
187 {
188 if (_stackFrames == null || _numOfFrames <= 0)
189 {
190 return Array.Empty<StackFrame>();
191 }
194 return array;
195 }
196
197 public override string ToString()
198 {
199 return ToString(TraceFormat.TrailingNewLine);
200 }
201
203 {
206 return stringBuilder.ToString();
207 }
208
210 {
211 string resourceString = SR.GetResourceString("Word_At", "at");
212 string resourceString2 = SR.GetResourceString("StackTrace_InFileLineNumber", "in {0}:line {1}");
213 string resourceString3 = SR.GetResourceString("StackTrace_InFileILOffset", "in {0}:token 0x{1:x}+0x{2:x}");
214 bool flag = true;
215 for (int i = 0; i < _numOfFrames; i++)
216 {
217 StackFrame frame = GetFrame(i);
218 MethodBase method = frame?.GetMethod();
219 if (!(method != null) || (!ShowInStackTrace(method) && i != _numOfFrames - 1))
220 {
221 continue;
222 }
223 if (flag)
224 {
225 flag = false;
226 }
227 else
228 {
229 sb.AppendLine();
230 }
231 sb.Append(" ").Append(resourceString).Append(' ');
232 bool flag2 = false;
233 Type declaringType = method.DeclaringType;
234 string name = method.Name;
235 bool flag3 = false;
236 if (declaringType != null && declaringType.IsDefined(typeof(CompilerGeneratedAttribute), inherit: false))
237 {
238 flag2 = declaringType.IsAssignableTo(typeof(IAsyncStateMachine));
239 if (flag2 || declaringType.IsAssignableTo(typeof(IEnumerator)))
240 {
242 }
243 }
244 if (declaringType != null)
245 {
246 string fullName = declaringType.FullName;
247 foreach (char c in fullName)
248 {
249 sb.Append((c == '+') ? '.' : c);
250 }
251 sb.Append('.');
252 }
253 sb.Append(method.Name);
254 if (method is MethodInfo { IsGenericMethod: not false } methodInfo)
255 {
256 Type[] genericArguments = methodInfo.GetGenericArguments();
257 sb.Append('[');
258 int k = 0;
259 bool flag4 = true;
260 for (; k < genericArguments.Length; k++)
261 {
262 if (!flag4)
263 {
264 sb.Append(',');
265 }
266 else
267 {
268 flag4 = false;
269 }
271 }
272 sb.Append(']');
273 }
274 ParameterInfo[] array = null;
275 try
276 {
277 array = method.GetParameters();
278 }
279 catch
280 {
281 }
282 if (array != null)
283 {
284 sb.Append('(');
285 bool flag5 = true;
286 for (int l = 0; l < array.Length; l++)
287 {
288 if (!flag5)
289 {
290 sb.Append(", ");
291 }
292 else
293 {
294 flag5 = false;
295 }
296 string value = "<UnknownType>";
297 if (array[l].ParameterType != null)
298 {
299 value = array[l].ParameterType.Name;
300 }
301 sb.Append(value);
302 sb.Append(' ');
303 sb.Append(array[l].Name);
304 }
305 sb.Append(')');
306 }
307 if (flag3)
308 {
309 sb.Append('+');
310 sb.Append(name);
311 sb.Append('(').Append(')');
312 }
313 if (frame.GetILOffset() != -1)
314 {
315 string fileName = frame.GetFileName();
316 if (fileName != null)
317 {
318 sb.Append(' ');
320 }
321 else if (LocalAppContextSwitches.ShowILOffsets && method.ReflectedType != null)
322 {
323 string scopeName = method.ReflectedType.Module.ScopeName;
324 try
325 {
326 int metadataToken = method.MetadataToken;
327 sb.Append(' ');
329 }
331 {
332 }
333 }
334 }
336 {
337 sb.AppendLine();
338 sb.Append(SR.GetResourceString("Exception_EndStackTraceFromPreviousThrow", "--- End of stack trace from previous location ---"));
339 }
340 }
341 if (traceFormat == TraceFormat.TrailingNewLine)
342 {
343 sb.AppendLine();
344 }
345 }
346
347 private static bool ShowInStackTrace(MethodBase mb)
348 {
349 if ((mb.MethodImplementationFlags & MethodImplAttributes.AggressiveInlining) != 0)
350 {
351 return false;
352 }
353 try
354 {
355 if (mb.IsDefined(typeof(StackTraceHiddenAttribute), inherit: false))
356 {
357 return false;
358 }
359 Type declaringType = mb.DeclaringType;
360 if (declaringType != null && declaringType.IsDefined(typeof(StackTraceHiddenAttribute), inherit: false))
361 {
362 return false;
363 }
364 }
365 catch
366 {
367 }
368 return true;
369 }
370
372 {
373 declaringType = method.DeclaringType;
374 Type declaringType2 = declaringType.DeclaringType;
375 if (declaringType2 == null)
376 {
377 return false;
378 }
379 MethodInfo[] array = GetDeclaredMethods(declaringType2);
380 if (array == null)
381 {
382 return false;
383 }
385 foreach (MethodInfo methodInfo in array2)
386 {
388 if (customAttributes == null)
389 {
390 continue;
391 }
392 bool flag = false;
393 bool flag2 = false;
395 {
396 if (item.StateMachineType == declaringType)
397 {
398 flag = true;
400 }
401 }
402 if (flag)
403 {
405 declaringType = methodInfo.DeclaringType;
406 return flag2;
407 }
408 }
409 return false;
410 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern", Justification = "Using Reflection to find the state machine's corresponding method is safe because the corresponding method is the only caller of the state machine. If the state machine is present, the corresponding method will be, too.")]
411 static MethodInfo[] GetDeclaredMethods(Type type)
412 {
413 return type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
414 }
415 }
416}
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
virtual ? string GetFileName()
virtual ? MethodBase GetMethod()
static void GetStackFramesInternal(StackFrameHelper sfh, int iSkip, bool fNeedFileInfo, Exception e)
void InitializeForCurrentThread(int skipFrames, bool fNeedFileInfo)
Definition StackTrace.cs:61
StackTrace(Exception e, int skipFrames, bool fNeedFileInfo)
void CaptureStackTrace(int skipFrames, bool fNeedFileInfo, Exception e)
Definition StackTrace.cs:66
override string ToString()
static bool TryResolveStateMachineMethod(ref MethodBase method, out Type declaringType)
virtual StackFrame[] GetFrames()
StackTrace(Exception e, int skipFrames)
StackTrace(int skipFrames, bool fNeedFileInfo)
StackTrace(StackFrame frame)
static int CalculateFramesToSkip(StackFrameHelper StackF, int iNumFrames)
Definition StackTrace.cs:32
static bool ShowInStackTrace(MethodBase mb)
virtual ? StackFrame GetFrame(int index)
StackTrace(bool fNeedFileInfo)
void InitializeForException(Exception exception, int skipFrames, bool fNeedFileInfo)
Definition StackTrace.cs:56
string ToString(TraceFormat traceFormat)
void ToString(TraceFormat traceFormat, StringBuilder sb)
StackTrace(Exception e, bool fNeedFileInfo)
static CultureInfo InvariantCulture
static string GetResourceString(string resourceKey)
Definition SR.cs:101
static string ArgumentOutOfRange_NeedNonNegNum
Definition SR.cs:32
Definition SR.cs:7
StringBuilder Append(char value, int repeatCount)
StringBuilder AppendFormat(string format, object? arg0)