-
Notifications
You must be signed in to change notification settings - Fork 544
/
Copy pathJavaProxyThrowable.cs
171 lines (140 loc) · 5.71 KB
/
JavaProxyThrowable.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
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
using System.Text;
using StackTraceElement = Java.Lang.StackTraceElement;
namespace Android.Runtime {
sealed class JavaProxyThrowable : Java.Lang.Error {
public readonly Exception InnerException;
JavaProxyThrowable (string message, Exception innerException)
: base (message)
{
InnerException = innerException;
}
public static JavaProxyThrowable Create (Exception innerException)
{
if (innerException == null) {
throw new ArgumentNullException (nameof (innerException));
}
// We prepend managed exception type to message since Java will see `JavaProxyThrowable` instead.
var proxy = new JavaProxyThrowable ($"[{innerException.GetType ()}]: {innerException.Message}", innerException);
try {
proxy.TranslateStackTrace ();
} catch (Exception ex) {
// We shouldn't throw here, just try to do the best we can do
Console.WriteLine ($"JavaProxyThrowable: translation threw an exception: {ex}");
proxy = new JavaProxyThrowable (innerException.ToString (), innerException);
}
return proxy;
}
(int lineNumber, string? methodName, string? className) GetFrameInfo (StackFrame? managedFrame, MethodBase? managedMethod)
{
string? methodName = null;
string? className = null;
if (managedFrame == null) {
if (managedMethod != null) {
methodName = managedMethod.Name;
className = managedMethod.DeclaringType?.FullName;
}
return (-1, methodName, className);
}
int lineNumber = -1;
lineNumber = managedFrame.GetFileLineNumber ();
if (lineNumber == 0) {
// -2 means it's a native frame
lineNumber = managedFrame.HasNativeImage () ? -2 : -1;
}
if (managedMethod != null) {
// If we have no line number information and if it's a managed frame, add the
// IL offset.
if (lineNumber == -1 && managedFrame.HasILOffset ()) {
methodName = $"{managedMethod.Name} + 0x{managedFrame.GetILOffset():x}";
} else {
methodName = managedMethod.Name;
}
return (lineNumber, methodName, managedMethod.DeclaringType?.FullName);
}
string frameString = managedFrame.ToString ();
var sb = new StringBuilder ();
// We take the part of the returned string that stretches from the beginning to the first space character
// and treat it as the method name.
// https://github.com/dotnet/runtime/blob/18c3ad05c3fc127c3b7f37c49bc350bf7f8264a0/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/DeveloperExperience/DeveloperExperience.cs#L15-L55
int pos = frameString.IndexOf (' ');
string? fullName = null;
if (pos > 1) {
fullName = frameString.Substring (0, pos);
}
if (!String.IsNullOrEmpty (fullName) && (pos = fullName.LastIndexOf ('.')) >= 1) {
className = pos + 1 < fullName.Length ? fullName.Substring (pos + 1) : null;
fullName = fullName.Substring (0, pos);
}
if (!String.IsNullOrEmpty (fullName)) {
sb.Append (fullName);
} else if (managedFrame.HasNativeImage ()) {
// We have no name, so we'll put the native IP
nint nativeIP = managedFrame.GetNativeIP ();
sb.Append (CultureInfo.InvariantCulture, $"Native 0x{nativeIP:x}");
}
if (sb.Length > 0) {
// We will also append information native offset information, if available and only if we
// have recorded any previous information, since the offset without context is useless.
int nativeOffset = managedFrame.GetNativeOffset ();
if (nativeOffset != StackFrame.OFFSET_UNKNOWN) {
sb.Append (" + ");
sb.Append (CultureInfo.InvariantCulture, $"0x{nativeOffset:x}");
}
}
if (sb.Length > 0) {
methodName = sb.ToString ();
}
return (lineNumber, methodName, className);
}
void TranslateStackTrace ()
{
// FIXME: https://github.com/xamarin/xamarin-android/issues/8724
// StackFrame.GetMethod() will return null under NativeAOT;
// However, you can still get useful information from StackFrame.ToString():
// MainActivity.OnCreate() + 0x37 at offset 55 in file:line:column <filename unknown>:0:0
[UnconditionalSuppressMessage ("Trimming", "IL2026", Justification = "StackFrame.GetMethod() is \"best attempt\", we handle null & exceptions")]
static MethodBase? StackFrameGetMethod (StackFrame frame) =>
frame.GetMethod ();
var trace = new StackTrace (InnerException, fNeedFileInfo: true);
if (trace.FrameCount <= 0) {
return;
}
StackTraceElement[]? javaTrace = null;
try {
javaTrace = GetStackTrace ();
} catch (Exception ex) {
// Report...
Console.WriteLine ($"JavaProxyThrowable: obtaining Java stack trace threw an exception: {ex}");
// ..but ignore
}
StackFrame[] frames = trace.GetFrames ();
int nElements = frames.Length + (javaTrace?.Length ?? 0);
StackTraceElement[] elements = new StackTraceElement[nElements];
const string Unknown = "Unknown";
for (int i = 0; i < frames.Length; i++) {
StackFrame managedFrame = frames[i];
MethodBase? managedMethod = StackFrameGetMethod (managedFrame);
// https://developer.android.com/reference/java/lang/StackTraceElement?hl=en#StackTraceElement(java.lang.String,%20java.lang.String,%20java.lang.String,%20int)
(int lineNumber, string? methodName, string? declaringClass) = GetFrameInfo (managedFrame, managedMethod);
var throwableFrame = new StackTraceElement (
declaringClass: declaringClass ?? Unknown,
methodName: methodName ?? Unknown,
fileName: managedFrame?.GetFileName (),
lineNumber: lineNumber
);
elements[i] = throwableFrame;
}
if (javaTrace != null) {
for (int i = frames.Length; i < nElements; i++) {
elements[i] = javaTrace[i - frames.Length];
}
}
SetStackTrace (elements);
}
}
}