Skip to content

Commit fa561c3

Browse files
committed
MOAR LOGGING
Context: good! https://devdiv.visualstudio.com/DevDiv/_build/results?buildId=3201349&view=logs&j=8556562a-ae5f-5bd1-7c4d-bf1af4b6f1e1&t=a4d74fbf-7b8e-51d6-b189-8c80ca04a74a Context: Bad! https://devdiv.visualstudio.com/DevDiv/_build/results?buildId=3201349&view=logs&j=8556562a-ae5f-5bd1-7c4d-bf1af4b6f1e1&t=1ce93f75-3079-5716-18de-288761bcb886 LOGGING FOR THE LOGGING GODS! The story so far: now we have GREF logs (yay), and can thus see their creation & destruction. Conspicuously MISSING, however, is the handle in the exception message! System.NotSupportedException: Unable to activate instance of type Xamarin.Forms.Performance.Integration.Droid.MainActivity from native handle 0xff88ad3c (key_handle 0x2629e85). The GREF logs we have DO NOT MENTION 0xff88ad3c nor 0x2629e85. Compare to the working (JIT-only) iteration, which has a `handle` statement: handle 0x2786; key_handle 0x2629e85: Java Type: `xamarin/forms/performance/integration/MainActivity`; MCW type: `Xamarin.Forms.Performance.Integration.Droid.MainActivity` This leaves one current scary hypothesis (good for Halloween!): what if the instance isn't being *activated*? When a Java Callable Wrapper constructor is executed, the Java side invokes `mono.android.TypeManager.Activate()`, which is `Java.Interop.TypeManager.n_Activate()`, which will NOT activate an instance for a variety of reasons. Is `TypeManager.n_Activate()` being executed? If it *is* executed, does it *do* anything? LOG THE HELL OUT OF IT. Ditto `TypeManager.CreateInstance()`. Additionally, update `OSBridge::_monodroid_gref_log()` so that the message is written to *both* `grefs.txt` AND `adb logcat`, to make it easier to reason about things when only one or the other is around. (This should allow the `handle` statement to appear in `adb logcat`.)
1 parent 6ab20ac commit fa561c3

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/Mono.Android/Java.Interop/TypeManager.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,17 @@ static Type[] GetParameterTypes (string signature)
133133

134134
static void n_Activate (IntPtr jnienv, IntPtr jclass, IntPtr typename_ptr, IntPtr signature_ptr, IntPtr jobject, IntPtr parameters_ptr)
135135
{
136+
JNIEnv._monodroid_gref_log (
137+
string.Format ("# jonp: n_Activate: Activating handle 0x{0} (key_handle 0x{1}).",
138+
jobject.ToString ("x"), JNIEnv.IdentityHash (jobject).ToString ("x")));
136139
var o = Java.Lang.Object.PeekObject (jobject);
137140
var ex = o as IJavaObjectEx;
138141
if (ex != null) {
139142
if (!ex.NeedsActivation && !ex.IsProxy)
140143
return;
141144
}
142145
if (!ActivationEnabled) {
146+
JNIEnv._monodroid_gref_log ($"# jonp: n_Activate: Activation not enabled. Bailing!");
143147
if (Logger.LogGlobalRef) {
144148
Logger.Log (LogLevel.Info, "monodroid-gref",
145149
string.Format ("warning: Skipping managed constructor invocation for handle 0x{0} (key_handle 0x{1}). " +
@@ -151,22 +155,26 @@ static void n_Activate (IntPtr jnienv, IntPtr jclass, IntPtr typename_ptr, IntPt
151155
}
152156

153157
Type type = Type.GetType (JNIEnv.GetString (typename_ptr, JniHandleOwnership.DoNotTransfer), throwOnError:true);
158+
JNIEnv._monodroid_gref_log ($"# jonp: n_Activate: type={type}");
154159
if (type.IsGenericTypeDefinition) {
155160
throw new NotSupportedException (
156161
"Constructing instances of generic types from Java is not supported, as the type parameters cannot be determined.",
157162
CreateJavaLocationException ());
158163
}
159164
Type[] ptypes = GetParameterTypes (JNIEnv.GetString (signature_ptr, JniHandleOwnership.DoNotTransfer));
165+
JNIEnv._monodroid_gref_log ($"# jonp: n_Activate: constructor parameter types: {string.Join (", ", (object[]) ptypes)}");
160166
object[] parms = JNIEnv.GetObjectArray (parameters_ptr, ptypes);
161167
ConstructorInfo cinfo = type.GetConstructor (ptypes);
162168
if (cinfo == null) {
163169
throw CreateMissingConstructorException (type, ptypes);
164170
}
165171
if (o != null) {
172+
JNIEnv._monodroid_gref_log ($"# jonp: n_Activate: invoking ctor on already created instance");
166173
cinfo.Invoke (o, parms);
167174
return;
168175
}
169176
try {
177+
JNIEnv._monodroid_gref_log ($"# jonp: n_Activate: creating new instance");
170178
var activator = ConstructorBuilder.CreateDelegate (type, cinfo, ptypes);
171179
activator (jobject, parms);
172180
} catch (Exception e) {
@@ -243,6 +251,7 @@ internal static IJavaObject CreateInstance (IntPtr handle, JniHandleOwnership tr
243251

244252
internal static IJavaObject CreateInstance (IntPtr handle, JniHandleOwnership transfer, Type targetType)
245253
{
254+
JNIEnv._monodroid_gref_log ($"# jonp: CreateInstance: handle=0x{handle.ToString ("x2")}, key_handle=0x{JNIEnv.IdentityHash (handle)}");
246255
Type type = null;
247256
IntPtr class_ptr = JNIEnv.GetObjectClass (handle);
248257
string class_name = GetClassName (class_ptr);
@@ -283,6 +292,8 @@ internal static IJavaObject CreateInstance (IntPtr handle, JniHandleOwnership tr
283292
type = invokerType;
284293
}
285294

295+
JNIEnv._monodroid_gref_log ($"# jonp: CreateInstance: type={type}");
296+
286297

287298
IJavaObject result = null;
288299

src/monodroid/jni/osbridge.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ OSBridge::_write_stack_trace (FILE *to, const char *from)
222222
void
223223
OSBridge::_monodroid_gref_log (const char *message)
224224
{
225+
log_info (LOG_GREF, "%s", message);
225226
if (!gref_log)
226227
return;
227228
fprintf (gref_log, "%s", message);

0 commit comments

Comments
 (0)