This repository was archived by the owner on Jun 5, 2024. It is now read-only.
forked from benaadams/Ben.Demystifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnhancedStackTrace.Frames.cs
895 lines (744 loc) · 32.8 KB
/
EnhancedStackTrace.Frames.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
// Copyright (c) Ben A Adams. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
using static System.Reflection.BindingFlags;
namespace Apkd.Internal
{
sealed partial class EnhancedStackTrace
{
static readonly Type StackTraceHiddenAttibuteType = Type.GetType("System.Diagnostics.StackTraceHiddenAttribute", false);
static readonly MethodInfo UnityEditorInspectorWindowOnGuiMethod = typeof(UnityEditor.Editor).Assembly.GetType("UnityEditor.InspectorWindow", false)?.GetMethod("OnGUI", NonPublic | Instance);
static readonly ThreadLocal<StringBuilder> TempStringBuilder = new ThreadLocal<StringBuilder>(() => new StringBuilder(capacity: 256));
static readonly CacheDictionary<ICustomAttributeProvider, object[]> customAttributeCache
= new CacheDictionary<ICustomAttributeProvider, object[]>(cacheSize: 256);
static readonly CacheDictionary<MethodBase, ResolvedMethod> resolvedMethodCache
= new CacheDictionary<MethodBase, ResolvedMethod>(cacheSize: 256);
static List<EnhancedStackFrame> GetFrames(Exception exception)
{
if (exception == null)
return new List<EnhancedStackFrame>();
var needFileInfo = true;
var stackTrace = new StackTrace(exception, needFileInfo);
return GetFrames(stackTrace);
}
static readonly FieldInfo capturedTracesFieldInfo = typeof(StackTrace).GetField("captured_traces", NonPublic | Instance);
static StackTrace[] GetInnerStackTraces(StackTrace st)
=> capturedTracesFieldInfo?.GetValue(st) as StackTrace[] ?? Array.Empty<StackTrace>();
static List<EnhancedStackFrame> GetFrames(StackTrace stackTrace)
{
IEnumerable<EnhancedStackFrame> EnumerateEnhancedFrames(StackTrace st)
{
IEnumerable<StackFrame> EnumerateFrames(StackTrace st2)
{
foreach (var inner in GetInnerStackTraces(st2))
foreach (var frame in EnumerateFrames(inner))
yield return frame;
for (var i = 0; i < st2.FrameCount; i++)
yield return st2.GetFrame(i);
}
EnhancedStackFrame MakeEnhancedFrame(StackFrame frame, MethodBase method)
=> new EnhancedStackFrame(
frame,
methodInfo: GetResolvedMethod(method),
fileName: frame.GetFileName(),
lineNumber: frame.GetFileLineNumber(),
colNumber: frame.GetFileColumnNumber()
);
bool collapseNext = false;
StackFrame current = null;
foreach (var next in EnumerateFrames(stackTrace))
{
try
{
if (current == null)
continue;
var method = current.GetMethod();
if (method == null) // TODO: remove this
continue;
bool shouldExcludeFromCollapse = ShouldExcludeFromCollapse(method);
if (shouldExcludeFromCollapse)
collapseNext = false;
if ((collapseNext || ShouldCollapseStackFrames(method)) && !shouldExcludeFromCollapse)
{
if (ShouldCollapseStackFrames(next.GetMethod()))
{
collapseNext = true;
continue;
}
else
{
collapseNext = false;
}
}
if (!ShouldShowInStackTrace(method))
continue;
yield return MakeEnhancedFrame(current, method);
}
finally
{
current = next;
}
}
if (current != null)
yield return MakeEnhancedFrame(current, current.GetMethod());
}
var resultList = new List<EnhancedStackFrame>(capacity: stackTrace.FrameCount);
foreach (var item in EnumerateEnhancedFrames(stackTrace))
resultList.Add(item);
return resultList;
}
static bool IsDefined<T>(MemberInfo member)
{
foreach (var attr in GetCustomAttributes(member))
if (attr is T)
return true;
return false;
}
static object[] GetCustomAttributes(ICustomAttributeProvider obj)
=> customAttributeCache.GetOrInitializeValue(obj, x => x.GetCustomAttributes(inherit: false));
internal static ResolvedMethod GetResolvedMethod(MethodBase methodBase)
=> resolvedMethodCache.GetOrInitializeValue(methodBase, x => GetResolvedMethodInternal(methodBase));
internal static ResolvedMethod GetResolvedMethodInternal(MethodBase methodBase)
{
// Special case: no method available
if (methodBase == null)
return null;
var method = methodBase;
var methodDisplayInfo = new ResolvedMethod { SubMethodBase = method };
// Type name
var type = method.DeclaringType;
var subMethodName = method.Name;
var methodName = method.Name;
if (type != null && IsDefined<CompilerGeneratedAttribute>(type) && (typeof(IAsyncStateMachine).IsAssignableFrom(type) || typeof(IEnumerator).IsAssignableFrom(type)))
{
methodDisplayInfo.IsAsync = typeof(IAsyncStateMachine).IsAssignableFrom(type);
// Convert StateMachine methods to correct overload +MoveNext()
if (!TryResolveStateMachineMethod(ref method, out type))
{
methodDisplayInfo.SubMethodBase = null;
subMethodName = null;
}
methodName = method.Name;
}
// Method name
methodDisplayInfo.MethodBase = method;
methodDisplayInfo.Name = methodName;
if (method.Name.IndexOf('<') >= 0)
{
if (TryResolveGeneratedName(ref method, out type, out methodName, out subMethodName, out var kind, out var ordinal))
{
methodName = method.Name;
methodDisplayInfo.MethodBase = method;
methodDisplayInfo.Name = methodName;
methodDisplayInfo.Ordinal = ordinal;
}
else
{
methodDisplayInfo.MethodBase = null;
}
methodDisplayInfo.IsLambda = (kind == GeneratedNameKind.LambdaMethod);
if (methodDisplayInfo.IsLambda && type != null)
{
if (methodName == ".cctor")
{
if (type.IsGenericTypeDefinition && !type.IsConstructedGenericType)
{
// TODO: diagnose type's generic type arguments from frame's "this" or something
}
else
{
var fields = type.GetFields(Static | Public | NonPublic);
foreach (var field in fields)
{
var value = field.GetValue(field);
if (value is Delegate d)
{
if (ReferenceEquals(d.Method, methodBase) &&
d.Target.ToString() == methodBase.DeclaringType.ToString())
{
methodDisplayInfo.Name = field.Name;
methodDisplayInfo.IsLambda = false;
method = methodBase;
break;
}
}
}
}
}
}
}
if (subMethodName != methodName)
methodDisplayInfo.SubMethod = subMethodName;
// ResolveStateMachineMethod may have set declaringType to null
if (type != null)
methodDisplayInfo.DeclaringType = type;
if (method is MethodInfo mi)
{
var returnParameter = mi.ReturnParameter;
if (returnParameter != null)
{
methodDisplayInfo.ReturnParameter = GetParameter(mi.ReturnParameter);
}
else if (mi.ReturnType != null)
{
methodDisplayInfo.ReturnParameter = new ResolvedParameter
{
Prefix = "",
Name = "",
ResolvedType = mi.ReturnType,
};
}
}
if (method.IsGenericMethod)
{
var genericArguments = method.GetGenericArguments();
var builder = TempStringBuilder.Value.Clear();
builder.Append('<');
for (int i = 0; i < genericArguments.Length; ++i)
{
if (i > 0)
builder.Append(',').Append(' ');
builder.AppendTypeDisplayName(genericArguments[i], fullName: false, includeGenericParameterNames: true);
}
builder.Append('>');
methodDisplayInfo.GenericArguments = builder.ToString();
methodDisplayInfo.ResolvedGenericArguments = genericArguments;
}
// Method parameters
var parameters = method.GetParameters();
if (parameters.Length > 0)
{
var parameterList = new List<ResolvedParameter>(parameters.Length);
foreach (var parameter in parameters)
parameterList.Add(GetParameter(parameter));
methodDisplayInfo.Parameters = parameterList;
}
if (methodDisplayInfo.SubMethodBase == methodDisplayInfo.MethodBase)
{
methodDisplayInfo.SubMethodBase = null;
}
else if (methodDisplayInfo.SubMethodBase != null)
{
parameters = methodDisplayInfo.SubMethodBase.GetParameters();
if (parameters.Length > 0)
{
var parameterList = new List<ResolvedParameter>(parameters.Length);
foreach (var parameter in parameters)
{
var param = GetParameter(parameter);
if (param.Name?.StartsWith("<") ?? true) continue;
parameterList.Add(param);
}
methodDisplayInfo.SubMethodParameters = parameterList;
}
}
return methodDisplayInfo;
}
static bool TryResolveGeneratedName(ref MethodBase method, out Type type, out string methodName, out string subMethodName, out GeneratedNameKind kind, out int? ordinal)
{
kind = GeneratedNameKind.None;
type = method.DeclaringType;
subMethodName = null;
ordinal = null;
methodName = method.Name;
var generatedName = methodName;
if (!TryParseGeneratedName(generatedName, out kind, out var openBracketOffset, out var closeBracketOffset))
return false;
methodName = generatedName.Substring(openBracketOffset + 1, closeBracketOffset - openBracketOffset - 1);
switch (kind)
{
case GeneratedNameKind.LocalFunction:
var localNameStart = generatedName.IndexOf((char)kind, closeBracketOffset + 1);
if (localNameStart < 0)
break;
localNameStart += 3;
if (localNameStart < generatedName.Length)
{
var localNameEnd = generatedName.IndexOf("|", localNameStart, StringComparison.Ordinal);
if (localNameEnd > 0)
subMethodName = generatedName.Substring(localNameStart, localNameEnd - localNameStart);
}
break;
case GeneratedNameKind.LambdaMethod:
subMethodName = "";
break;
}
var dt = method.DeclaringType;
if (dt == null)
return false;
var matchHint = GetMatchHint(kind, method);
var matchName = methodName;
var candidateMethods = dt.GetMethods(Public | NonPublic | Static | Instance | DeclaredOnly).Where(m => m.Name == matchName);
if (TryResolveSourceMethod(candidateMethods, kind, matchHint, ref method, ref type, out ordinal))
return true;
var candidateConstructors = dt.GetConstructors(Public | NonPublic | Static | Instance | DeclaredOnly).Where(m => m.Name == matchName);
if (TryResolveSourceMethod(candidateConstructors, kind, matchHint, ref method, ref type, out ordinal))
return true;
const int MaxResolveDepth = 10;
for (var i = 0; i < MaxResolveDepth; i++)
{
dt = dt.DeclaringType;
if (dt == null)
return false;
candidateMethods = dt.GetMethods(Public | NonPublic | Static | Instance | DeclaredOnly).Where(m => m.Name == matchName);
if (TryResolveSourceMethod(candidateMethods, kind, matchHint, ref method, ref type, out ordinal))
return true;
candidateConstructors = dt.GetConstructors(Public | NonPublic | Static | Instance | DeclaredOnly).Where(m => m.Name == matchName);
if (TryResolveSourceMethod(candidateConstructors, kind, matchHint, ref method, ref type, out ordinal))
return true;
if (methodName == ".cctor")
{
candidateConstructors = dt.GetConstructors(Public | NonPublic | Static | DeclaredOnly).Where(m => m.Name == matchName);
foreach (var cctor in candidateConstructors)
{
method = cctor;
type = dt;
return true;
}
}
}
return false;
}
static bool TryResolveSourceMethod(IEnumerable<MethodBase> candidateMethods, GeneratedNameKind kind, string matchHint, ref MethodBase method, ref Type type, out int? ordinal)
{
ordinal = null;
foreach (var candidateMethod in candidateMethods)
{
var methodBody = candidateMethod.GetMethodBody();
if (kind == GeneratedNameKind.LambdaMethod)
{
foreach (var v in EnumerableIList.Create(methodBody?.LocalVariables))
{
if (v.LocalType == type)
GetOrdinal(method, ref ordinal);
method = candidateMethod;
type = method.DeclaringType;
return true;
}
}
try
{
var rawIL = methodBody?.GetILAsByteArray();
if (rawIL == null)
continue;
var reader = new ILReader(rawIL);
while (reader.Read(candidateMethod))
{
if (reader.Operand is MethodBase mb)
{
if (method == mb || (matchHint != null && method.Name.Contains(matchHint)))
{
if (kind == GeneratedNameKind.LambdaMethod)
GetOrdinal(method, ref ordinal);
method = candidateMethod;
type = method.DeclaringType;
return true;
}
}
}
}
catch
{
// https://github.com/benaadams/Ben.Demystifier/issues/32
// Skip methods where il can't be interpreted
}
}
return false;
}
static void GetOrdinal(MethodBase method, ref int? ordinal)
{
#if APKD_STACKTRACE_LAMBDAORDINALS
var lamdaStart = method.Name.IndexOf((char)GeneratedNameKind.LambdaMethod + "__") + 3;
if (lamdaStart > 3)
{
var secondStart = method.Name.IndexOf('_', lamdaStart) + 1;
if (secondStart > 0)
{
lamdaStart = secondStart;
}
if (!int.TryParse(method.Name.Substring(lamdaStart), out var foundOrdinal))
{
ordinal = null;
return;
}
ordinal = foundOrdinal;
var methods = method.DeclaringType.GetMethods(Public | NonPublic | Static | Instance | DeclaredOnly);
var startName = method.Name.Substring(0, lamdaStart);
var count = 0;
foreach (var m in methods)
{
if (m.Name.Length > lamdaStart && m.Name.StartsWith(startName))
{
count++;
if (count > 1)
break;
}
}
if (count <= 1)
ordinal = null;
}
#endif
}
static string GetMatchHint(GeneratedNameKind kind, MethodBase method)
{
var methodName = method.Name;
switch (kind)
{
case GeneratedNameKind.LocalFunction:
var start = methodName.IndexOf("|", StringComparison.Ordinal);
if (start < 1)
return null;
var end = methodName.IndexOf("_", start, StringComparison.Ordinal) + 1;
if (end <= start)
return null;
return methodName.Substring(start, end - start);
}
return null;
}
// Parse the generated name. Returns true for names of the form
// [CS$]<[middle]>c[__[suffix]] where [CS$] is included for certain
// generated names, where [middle] and [__[suffix]] are optional,
// and where c is a single character in [1-9a-z]
// (csharp\LanguageAnalysis\LIB\SpecialName.cpp).
internal static bool TryParseGeneratedName(
string name,
out GeneratedNameKind kind,
out int openBracketOffset,
out int closeBracketOffset)
{
openBracketOffset = -1;
if (name.StartsWith("CS$<", StringComparison.Ordinal))
openBracketOffset = 3;
else if (name.StartsWith("<", StringComparison.Ordinal))
openBracketOffset = 0;
if (openBracketOffset >= 0)
{
closeBracketOffset = IndexOfBalancedParenthesis(name, openBracketOffset, '>');
if (closeBracketOffset >= 0 && closeBracketOffset + 1 < name.Length)
{
int c = name[closeBracketOffset + 1];
if ((c >= '1' && c <= '9') || (c >= 'a' && c <= 'z')) // Note '0' is not special.
{
kind = (GeneratedNameKind)c;
return true;
}
}
}
kind = GeneratedNameKind.None;
openBracketOffset = -1;
closeBracketOffset = -1;
return false;
}
static int IndexOfBalancedParenthesis(string str, int openingOffset, char closing)
{
var opening = str[openingOffset];
var depth = 1;
for (var i = openingOffset + 1; i < str.Length; i++)
{
var c = str[i];
if (c == opening)
{
depth++;
}
else if (c == closing)
{
depth--;
if (depth == 0)
return i;
}
}
return -1;
}
static string GetPrefix(ParameterInfo parameter, Type parameterType)
{
if (parameter.IsOut)
return "out";
if (parameterType != null && parameterType.IsByRef)
{
var attribs = GetCustomAttributes(parameter);
if (attribs?.Length > 0)
foreach (var attrib in attribs)
if (attrib is Attribute att && att.GetType().IsIsReadOnlyAttribute())
return "in";
return "ref";
}
return string.Empty;
}
static ResolvedParameter GetParameter(ParameterInfo parameter)
{
var parameterType = parameter.ParameterType;
var prefix = GetPrefix(parameter, parameterType);
if (parameterType == null)
{
return new ResolvedParameter
{
Prefix = prefix,
Name = parameter.Name,
ResolvedType = parameterType,
};
}
if (parameterType.IsGenericType)
{
var customAttribs = GetCustomAttributes(parameter);
Attribute tupleNameAttribute = null;
foreach (var attr in customAttribs)
if (attr is Attribute tena && tena.IsTupleElementNameAttribute())
tupleNameAttribute = tena;
// ReSharper disable ExpressionIsAlwaysNull
#if APKD_STACKTRACE_FULLPARAMS
var tupleNames = tupleNameAttribute?.GetTransformNames();
#else
var tupleNames = null as IList<string>;
#endif
if (tupleNameAttribute != null)
return GetValueTupleParameter(tupleNames, prefix, parameter.Name, parameterType);
// ReSharper restore ExpressionIsAlwaysNull
}
if (parameterType.IsByRef)
parameterType = parameterType.GetElementType();
return new ResolvedParameter
{
Prefix = prefix,
Name = parameter.Name,
ResolvedType = parameterType,
};
}
static ResolvedParameter GetValueTupleParameter(IList<string> tupleNames, string prefix, string name, Type parameterType)
{
return new ValueTupleResolvedParameter
{
TupleNames = tupleNames,
Prefix = prefix,
Name = name,
ResolvedType = parameterType
};
}
static string GetValueTupleParameterName(IList<string> tupleNames, Type parameterType)
{
var sb = new StringBuilder();
sb.Append('(');
var args = parameterType.GetGenericArguments();
for (var i = 0; i < args.Length; i++)
{
if (i > 0)
sb.Append(',').Append(' ');
sb.AppendTypeDisplayName(args[i], fullName: false, includeGenericParameterNames: true);
if (i >= tupleNames.Count)
continue;
var argName = tupleNames[i];
if (argName == null)
continue;
sb.Append(' ');
sb.Append(argName);
}
sb.Append(')');
return sb.ToString();
}
static bool ShouldCollapseStackFrames(MethodBase method)
{
var comparison = StringComparison.Ordinal;
string typeName = method?.DeclaringType?.FullName;
if (string.IsNullOrWhiteSpace(typeName))
return false;
return typeName.StartsWith("UnityEditor.", comparison) ||
typeName.StartsWith("UnityEngine.", comparison) ||
typeName.StartsWith("System.", comparison) ||
typeName.StartsWith("UnityScript.Lang.", comparison) ||
typeName.StartsWith("Odin.Editor.", comparison) ||
typeName.StartsWith("Boo.Lang.", comparison);
}
static bool ShouldExcludeFromCollapse(MethodBase method)
=> method == UnityEditorInspectorWindowOnGuiMethod;
static bool ShouldShowInStackTrace(MethodBase method)
{
Debug.Assert(method != null);
var type = method.DeclaringType;
if (type == typeof(Task<>) && method.Name == "InnerInvoke")
return false;
if (type == typeof(Task))
{
switch (method.Name)
{
case "ExecuteWithThreadLocal":
case "Execute":
case "ExecutionContextCallback":
case "ExecuteEntry":
case "InnerInvoke":
return false;
}
}
if (type == typeof(ExecutionContext))
{
switch (method.Name)
{
case "RunInternal":
case "Run":
return false;
}
}
if (StackTraceHiddenAttibuteType != null)
{
// Don't show any methods marked with the StackTraceHiddenAttribute
// https://github.com/dotnet/coreclr/pull/14652
if (IsStackTraceHidden(method))
return false;
}
if (type == null)
return true;
string typeFullName = type.FullName;
if (StackTraceHiddenAttibuteType != null)
{
// Don't show any types marked with the StackTraceHiddenAttribute
// https://github.com/dotnet/coreclr/pull/14652
if (IsStackTraceHidden(type))
return false;
}
else
{
// Fallbacks for runtime pre-StackTraceHiddenAttribute
if (type == typeof(ExceptionDispatchInfo) && method.Name == "Throw")
{
return false;
}
else if (type == typeof(TaskAwaiter) ||
type == typeof(TaskAwaiter<>) ||
type == typeof(ConfiguredTaskAwaitable.ConfiguredTaskAwaiter) ||
type == typeof(ConfiguredTaskAwaitable<>.ConfiguredTaskAwaiter))
{
switch (method.Name)
{
case "HandleNonSuccessAndDebuggerNotification":
case "ThrowForNonSuccess":
case "ValidateEnd":
case "GetResult":
return false;
}
}
else if (typeFullName == "System.ThrowHelper")
{
return false;
}
}
// collapse internal async frames
if (typeFullName.StartsWith("System.Runtime.CompilerServices.Async", StringComparison.Ordinal))
return false;
#if ODIN_INSPECTOR
// support for the Sirenix.OdinInspector package
if (typeFullName.StartsWith("Sirenix.OdinInspector", StringComparison.Ordinal))
return false;
#endif
// support for the Apkd.AsyncManager package
if (typeFullName.StartsWith("Apkd.Internal.AsyncManager", StringComparison.Ordinal))
return false;
if (typeFullName.StartsWith("Apkd.Internal.Continuation`1", StringComparison.Ordinal))
return false;
// collapse internal unity logging methods
if (typeFullName == "UnityEngine.DebugLogHandler")
return false;
if (typeFullName == "UnityEngine.Logger")
return false;
if (typeFullName == "UnityEngine.Debug")
return false;
return true;
}
static bool IsStackTraceHidden(MemberInfo memberInfo)
{
if (!memberInfo.Module.Assembly.ReflectionOnly)
{
foreach (var attr in GetCustomAttributes(memberInfo))
{
if (attr.GetType() == StackTraceHiddenAttibuteType)
return true;
return false;
}
}
EnumerableIList<CustomAttributeData> attributes;
try
{
attributes = EnumerableIList.Create(memberInfo.GetCustomAttributesData());
}
catch (NotImplementedException)
{
return false;
}
// reflection-only attribute, match on name
foreach (var attribute in attributes)
if (attribute.AttributeType.FullName == StackTraceHiddenAttibuteType.FullName)
return true;
return false;
}
static bool TryResolveStateMachineMethod(ref MethodBase method, out Type declaringType)
{
Debug.Assert(method != null);
Debug.Assert(method.DeclaringType != null);
declaringType = method.DeclaringType;
var parentType = declaringType.DeclaringType;
if (parentType == null)
return false;
var methods = parentType.GetMethods(Public | NonPublic | Static | Instance | DeclaredOnly);
if (methods == null)
return false;
foreach (var candidateMethod in methods)
{
var attributes = GetCustomAttributes(candidateMethod);
if (attributes == null)
continue;
foreach (var attr in attributes)
{
if (attr is StateMachineAttribute sma && sma.StateMachineType == declaringType)
{
method = candidateMethod;
declaringType = candidateMethod.DeclaringType;
// Mark the iterator as changed; so it gets the + annotation of the original method
// async statemachines resolve directly to their builder methods so aren't marked as changed
return sma is IteratorStateMachineAttribute;
}
}
}
return false;
}
internal enum GeneratedNameKind
{
None = 0,
// Used by EE:
ThisProxyField = '4',
HoistedLocalField = '5',
DisplayClassLocalOrField = '8',
LambdaMethod = 'b',
LambdaDisplayClass = 'c',
StateMachineType = 'd',
LocalFunction = 'g', // note collision with Deprecated_InitializerLocal, however this one is only used for method names
// Used by EnC:
AwaiterField = 'u',
HoistedSynthesizedLocalField = 's',
// Currently not parsed:
StateMachineStateField = '1',
IteratorCurrentBackingField = '2',
StateMachineParameterProxyField = '3',
ReusableHoistedLocalField = '7',
LambdaCacheField = '9',
FixedBufferField = 'e',
AnonymousType = 'f',
TransparentIdentifier = 'h',
AnonymousTypeField = 'i',
AutoPropertyBackingField = 'k',
IteratorCurrentThreadIdField = 'l',
IteratorFinallyMethod = 'm',
BaseMethodWrapper = 'n',
AsyncBuilderField = 't',
DynamicCallSiteContainerType = 'o',
DynamicCallSiteField = 'p'
}
}
}