From ff9223b13e9f215599e4480d2e7b22559167bd96 Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Wed, 15 Jul 2020 16:01:10 -0300 Subject: [PATCH 1/3] Trying to remove the usage of mono_unhandled_exception. --- src/Mono.Android/Android.Runtime/JNIEnv.cs | 12 ------------ .../Android.Runtime/JNINativeWrapper.cs | 18 ++---------------- .../UncaughtExceptionHandler.cs | 8 -------- 3 files changed, 2 insertions(+), 36 deletions(-) diff --git a/src/Mono.Android/Android.Runtime/JNIEnv.cs b/src/Mono.Android/Android.Runtime/JNIEnv.cs index 660d6bb3686..4cb6c9fb0dc 100644 --- a/src/Mono.Android/Android.Runtime/JNIEnv.cs +++ b/src/Mono.Android/Android.Runtime/JNIEnv.cs @@ -237,18 +237,10 @@ static void ManualJavaObjectDispose (Java.Lang.Object obj) GC.SuppressFinalize (obj); } - static Action mono_unhandled_exception = null!; static Action AppDomain_DoUnhandledException = null!; static void Initialize () { - if (mono_unhandled_exception == null) { - var mono_UnhandledException = typeof (System.Diagnostics.Debugger) - .GetMethod ("Mono_UnhandledException", BindingFlags.NonPublic | BindingFlags.Static); - if (mono_UnhandledException != null) - mono_unhandled_exception = (Action) Delegate.CreateDelegate (typeof(Action), mono_UnhandledException); - } - if (AppDomain_DoUnhandledException == null) { var ad_due = typeof (AppDomain) .GetMethod ("DoUnhandledException", @@ -276,10 +268,6 @@ internal static void PropagateUncaughtException (IntPtr env, IntPtr javaThreadPt var javaException = JavaObject.GetObject (env, javaExceptionPtr, JniHandleOwnership.DoNotTransfer)!; - // Disabled until Linker error surfaced in https://github.com/xamarin/xamarin-android/pull/4302#issuecomment-596400025 is resolved - //System.Diagnostics.Debugger.Mono_UnhandledException (javaException); - mono_unhandled_exception?.Invoke (javaException); - try { var jltp = javaException as JavaProxyThrowable; Exception? innerException = jltp?.InnerException; diff --git a/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs b/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs index eab1f812555..55c1c8a4313 100644 --- a/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs +++ b/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs @@ -7,7 +7,6 @@ namespace Android.Runtime { public static class JNINativeWrapper { - static MethodInfo? mono_unhandled_exception_method; static MethodInfo? exception_handler_method; static MethodInfo? wait_for_bridge_processing_method; @@ -15,12 +14,6 @@ static void get_runtime_types () { if (exception_handler_method != null) return; -#if MONOANDROID1_0 - mono_unhandled_exception_method = typeof (System.Diagnostics.Debugger).GetMethod ( - "Mono_UnhandledException", BindingFlags.NonPublic | BindingFlags.Static); - if (mono_unhandled_exception_method == null) - AndroidEnvironment.FailFast ("Cannot find System.Diagnostics.Debugger.Mono_UnhandledException"); -#endif exception_handler_method = typeof (AndroidEnvironment).GetMethod ( "UnhandledException", BindingFlags.NonPublic | BindingFlags.Static); if (exception_handler_method == null) @@ -70,15 +63,8 @@ public static Delegate CreateDelegate (Delegate dlg) ig.Emit (OpCodes.Leave, label); bool filter = Debugger.IsAttached || !JNIEnv.PropagateExceptions; - if (filter && mono_unhandled_exception_method != null) { - ig.BeginExceptFilterBlock (); - - ig.Emit (OpCodes.Call, mono_unhandled_exception_method); - ig.Emit (OpCodes.Ldc_I4_1); - ig.BeginCatchBlock (null!); - } else { - ig.BeginCatchBlock (typeof (Exception)); - } + + ig.BeginCatchBlock (typeof (Exception)); ig.Emit (OpCodes.Dup); ig.Emit (OpCodes.Call, exception_handler_method!); diff --git a/src/Mono.Android/Android.Runtime/UncaughtExceptionHandler.cs b/src/Mono.Android/Android.Runtime/UncaughtExceptionHandler.cs index a2fbb29a64e..c602650ef47 100644 --- a/src/Mono.Android/Android.Runtime/UncaughtExceptionHandler.cs +++ b/src/Mono.Android/Android.Runtime/UncaughtExceptionHandler.cs @@ -7,8 +7,6 @@ namespace Android.Runtime { sealed class UncaughtExceptionHandler : Java.Lang.Object, Java.Lang.Thread.IUncaughtExceptionHandler { - Action? mono_unhandled_exception; - Action? AppDomain_DoUnhandledException; Java.Lang.Thread.IUncaughtExceptionHandler defaultHandler; @@ -30,7 +28,6 @@ public void UncaughtException (Java.Lang.Thread thread, Java.Lang.Throwable ex) Android.Runtime.AndroidEnvironment.FailFast ($"Unable to initialize UncaughtExceptionHandler. Nested exception caught: {e}"); } - mono_unhandled_exception! (ex); if (AppDomain_DoUnhandledException != null) { try { var jltp = ex as JavaProxyThrowable; @@ -48,11 +45,6 @@ public void UncaughtException (Java.Lang.Thread thread, Java.Lang.Throwable ex) void Initialize () { - if (mono_unhandled_exception == null) { - var mono_UnhandledException = typeof (System.Diagnostics.Debugger) - .GetMethod ("Mono_UnhandledException", BindingFlags.NonPublic | BindingFlags.Static); - mono_unhandled_exception = (Action) Delegate.CreateDelegate (typeof(Action), mono_UnhandledException); - } if (AppDomain_DoUnhandledException == null) { var ad_due = typeof (AppDomain) From 649f8cf20063d06a3b26303bc58deb3fc02eb7df Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Thu, 16 Jul 2020 12:51:45 -0300 Subject: [PATCH 2/3] Removing unused variable. --- src/Mono.Android/Android.Runtime/JNINativeWrapper.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs b/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs index 55c1c8a4313..9d92d76d5b7 100644 --- a/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs +++ b/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs @@ -62,8 +62,6 @@ public static Delegate CreateDelegate (Delegate dlg) ig.Emit (OpCodes.Leave, label); - bool filter = Debugger.IsAttached || !JNIEnv.PropagateExceptions; - ig.BeginCatchBlock (typeof (Exception)); ig.Emit (OpCodes.Dup); From 1f510349cbeae62d58fb13827aeac6e1c4f03254 Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Thu, 27 Aug 2020 16:03:30 -0300 Subject: [PATCH 3/3] Totally remove this try catch generated when the debugger is attached. --- .../Android.Runtime/JNINativeWrapper.cs | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs b/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs index 9d92d76d5b7..f8356023221 100644 --- a/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs +++ b/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs @@ -51,26 +51,34 @@ public static Delegate CreateDelegate (Delegate dlg) ig.Emit (OpCodes.Call, wait_for_bridge_processing_method!); - var label = ig.BeginExceptionBlock (); + bool filter = Debugger.IsAttached || !JNIEnv.PropagateExceptions; + if (!filter) { + var label = ig.BeginExceptionBlock (); - for (int i = 0; i < param_types.Length; i++) - ig.Emit (OpCodes.Ldarg, i); - ig.Emit (OpCodes.Call, dlg.Method); + for (int i = 0; i < param_types.Length; i++) + ig.Emit (OpCodes.Ldarg, i); + ig.Emit (OpCodes.Call, dlg.Method); - if (retval != null) - ig.Emit (OpCodes.Stloc, retval); + if (retval != null) + ig.Emit (OpCodes.Stloc, retval); - ig.Emit (OpCodes.Leave, label); + ig.Emit (OpCodes.Leave, label); - ig.BeginCatchBlock (typeof (Exception)); + ig.BeginCatchBlock (typeof (Exception)); - ig.Emit (OpCodes.Dup); - ig.Emit (OpCodes.Call, exception_handler_method!); + ig.Emit (OpCodes.Dup); + ig.Emit (OpCodes.Call, exception_handler_method!); - if (filter) - ig.Emit (OpCodes.Throw); + ig.EndExceptionBlock (); + } + else { //let the debugger handle the exception + for (int i = 0; i < param_types.Length; i++) + ig.Emit (OpCodes.Ldarg, i); + ig.Emit (OpCodes.Call, dlg.Method); - ig.EndExceptionBlock (); + if (retval != null) + ig.Emit (OpCodes.Stloc, retval); + } if (retval != null) ig.Emit (OpCodes.Ldloc, retval);