From 3428bb02a955844c0e1e80cfbf0c3c40fe535903 Mon Sep 17 00:00:00 2001 From: Thays Date: Tue, 20 Jul 2021 14:18:07 -0300 Subject: [PATCH 1/5] Trying to use embedded api and avoid changing System.Diagnostics.Debugger class. --- src/Mono.Android/Android.Runtime/JNIEnv.cs | 7 +++++-- src/Mono.Android/Android.Runtime/JNINativeWrapper.cs | 8 +++----- src/monodroid/jni/monodroid-glue.cc | 7 +++++++ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Mono.Android/Android.Runtime/JNIEnv.cs b/src/Mono.Android/Android.Runtime/JNIEnv.cs index bbb192696be..aa235ab05fd 100644 --- a/src/Mono.Android/Android.Runtime/JNIEnv.cs +++ b/src/Mono.Android/Android.Runtime/JNIEnv.cs @@ -247,8 +247,8 @@ static void ManualJavaObjectDispose (Java.Lang.Object obj) static void Initialize () { if (mono_unhandled_exception == null) { - var mono_UnhandledException = typeof (System.Diagnostics.Debugger) - .GetMethod ("Mono_UnhandledException", BindingFlags.NonPublic | BindingFlags.Static); + var mono_UnhandledException = typeof (Android.Runtime.JNIEnv) + .GetMethod ("monodroid_debugger_unhandled_exception", BindingFlags.NonPublic | BindingFlags.Static); if (mono_UnhandledException != null) mono_unhandled_exception = (Action) Delegate.CreateDelegate (typeof(Action), mono_UnhandledException); } @@ -682,6 +682,9 @@ public static string GetClassNameFromInstance (IntPtr jobject) [MethodImplAttribute(MethodImplOptions.InternalCall)] static extern unsafe IntPtr monodroid_typemap_managed_to_java (Type type, byte* mvid); + [MethodImplAttribute(MethodImplOptions.InternalCall)] + static extern unsafe void monodroid_debugger_unhandled_exception (Exception e); + internal static void LogTypemapTrace (StackTrace st) { string? trace = st.ToString ()?.Trim (); diff --git a/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs b/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs index eab1f812555..7495e3d4255 100644 --- a/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs +++ b/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs @@ -15,12 +15,10 @@ 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); + mono_unhandled_exception_method = typeof (Android.Runtime.JNIEnv).GetMethod ( + "monodroid_debugger_unhandled_exception", BindingFlags.NonPublic | BindingFlags.Static); if (mono_unhandled_exception_method == null) - AndroidEnvironment.FailFast ("Cannot find System.Diagnostics.Debugger.Mono_UnhandledException"); -#endif + AndroidEnvironment.FailFast ("Cannot find Android.Runtime.JNIEnv.monodroid_debugger_unhandled_exception"); exception_handler_method = typeof (AndroidEnvironment).GetMethod ( "UnhandledException", BindingFlags.NonPublic | BindingFlags.Static); if (exception_handler_method == null) diff --git a/src/monodroid/jni/monodroid-glue.cc b/src/monodroid/jni/monodroid-glue.cc index e7eade011c4..04017128397 100644 --- a/src/monodroid/jni/monodroid-glue.cc +++ b/src/monodroid/jni/monodroid-glue.cc @@ -1009,6 +1009,7 @@ MonodroidRuntime::init_android_runtime ( { mono_add_internal_call ("Java.Interop.TypeManager::monodroid_typemap_java_to_managed", reinterpret_cast(typemap_java_to_managed)); mono_add_internal_call ("Android.Runtime.JNIEnv::monodroid_typemap_managed_to_java", reinterpret_cast(typemap_managed_to_java)); + mono_add_internal_call ("System.Diagnostics.Debugger::monodroid_debugger_unhandled_exception", reinterpret_cast (monodroid_debugger_unhandled_exception)); struct JnienvInitializeArgs init = {}; init.javaVm = osBridge.get_jvm (); @@ -1785,6 +1786,12 @@ monodroid_Mono_UnhandledException_internal ([[maybe_unused]] MonoException *ex) // Do nothing with it here, we let the exception naturally propagate on the managed side } +static void +monodroid_debugger_unhandled_exception (MonoException *ex) +{ + mono_debugger_agent_unhandled_exception (ex); +} + MonoDomain* MonodroidRuntime::create_and_initialize_domain (JNIEnv* env, jclass runtimeClass, jstring_array_wrapper &runtimeApks, jstring_array_wrapper &assemblies, [[maybe_unused]] jobjectArray assembliesBytes, From c102e3fd467b264616b3cf3be6e4dab55735c4c4 Mon Sep 17 00:00:00 2001 From: Thays Date: Tue, 20 Jul 2021 18:18:29 -0300 Subject: [PATCH 2/5] Fixing implementation. --- src/Mono.Android/Android.Runtime/JNIEnv.cs | 7 +++++++ .../Android.Runtime/JNINativeWrapper.cs | 8 ++++++++ src/monodroid/jni/monodroid-glue-internal.hh | 3 +++ src/monodroid/jni/monodroid-glue.cc | 19 ++++++++++++------- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/Mono.Android/Android.Runtime/JNIEnv.cs b/src/Mono.Android/Android.Runtime/JNIEnv.cs index aa235ab05fd..6c89a631923 100644 --- a/src/Mono.Android/Android.Runtime/JNIEnv.cs +++ b/src/Mono.Android/Android.Runtime/JNIEnv.cs @@ -247,8 +247,13 @@ static void ManualJavaObjectDispose (Java.Lang.Object obj) static void Initialize () { if (mono_unhandled_exception == null) { +#if NETCOREAPP var mono_UnhandledException = typeof (Android.Runtime.JNIEnv) .GetMethod ("monodroid_debugger_unhandled_exception", BindingFlags.NonPublic | BindingFlags.Static); +#else + var mono_UnhandledException = typeof (System.Diagnostics.Debugger) + .GetMethod ("Mono_UnhandledException", BindingFlags.NonPublic | BindingFlags.Static); +#endif if (mono_UnhandledException != null) mono_unhandled_exception = (Action) Delegate.CreateDelegate (typeof(Action), mono_UnhandledException); } @@ -682,8 +687,10 @@ public static string GetClassNameFromInstance (IntPtr jobject) [MethodImplAttribute(MethodImplOptions.InternalCall)] static extern unsafe IntPtr monodroid_typemap_managed_to_java (Type type, byte* mvid); +#if NETCOREAPP [MethodImplAttribute(MethodImplOptions.InternalCall)] static extern unsafe void monodroid_debugger_unhandled_exception (Exception e); +#endif internal static void LogTypemapTrace (StackTrace st) { diff --git a/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs b/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs index 7495e3d4255..7de58ad6141 100644 --- a/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs +++ b/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs @@ -15,10 +15,18 @@ 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 +#if NETCOREAPP mono_unhandled_exception_method = typeof (Android.Runtime.JNIEnv).GetMethod ( "monodroid_debugger_unhandled_exception", BindingFlags.NonPublic | BindingFlags.Static); if (mono_unhandled_exception_method == null) AndroidEnvironment.FailFast ("Cannot find Android.Runtime.JNIEnv.monodroid_debugger_unhandled_exception"); +#endif exception_handler_method = typeof (AndroidEnvironment).GetMethod ( "UnhandledException", BindingFlags.NonPublic | BindingFlags.Static); if (exception_handler_method == null) diff --git a/src/monodroid/jni/monodroid-glue-internal.hh b/src/monodroid/jni/monodroid-glue-internal.hh index 4e5ea9e74c5..b82c8e722f4 100644 --- a/src/monodroid/jni/monodroid-glue-internal.hh +++ b/src/monodroid/jni/monodroid-glue-internal.hh @@ -284,6 +284,9 @@ namespace xamarin::android::internal static MonoReflectionType* typemap_java_to_managed (MonoString *java_type_name); static const char* typemap_managed_to_java (MonoReflectionType *type, const uint8_t *mvid); +#if defined (NET6) + static void monodroid_debugger_unhandled_exception (MonoException *ex); +#endif #if defined (DEBUG) void set_debug_env_vars (void); diff --git a/src/monodroid/jni/monodroid-glue.cc b/src/monodroid/jni/monodroid-glue.cc index 04017128397..8e0fcee3a7b 100644 --- a/src/monodroid/jni/monodroid-glue.cc +++ b/src/monodroid/jni/monodroid-glue.cc @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -1000,6 +1001,14 @@ MonodroidRuntime::lookup_bridge_info (MonoDomain *domain, MonoImage *image, cons } #endif // ndef NET6 +#if defined (NET6) +void +MonodroidRuntime::monodroid_debugger_unhandled_exception (MonoException *ex) +{ + mono_debugger_agent_unhandled_exception (ex); +} +#endif + void MonodroidRuntime::init_android_runtime ( #if !defined (NET6) @@ -1009,7 +1018,9 @@ MonodroidRuntime::init_android_runtime ( { mono_add_internal_call ("Java.Interop.TypeManager::monodroid_typemap_java_to_managed", reinterpret_cast(typemap_java_to_managed)); mono_add_internal_call ("Android.Runtime.JNIEnv::monodroid_typemap_managed_to_java", reinterpret_cast(typemap_managed_to_java)); - mono_add_internal_call ("System.Diagnostics.Debugger::monodroid_debugger_unhandled_exception", reinterpret_cast (monodroid_debugger_unhandled_exception)); +#if NET6 + mono_add_internal_call ("Android.Runtime.JNIEnv::monodroid_debugger_unhandled_exception", reinterpret_cast (monodroid_debugger_unhandled_exception)); +#endif struct JnienvInitializeArgs init = {}; init.javaVm = osBridge.get_jvm (); @@ -1786,12 +1797,6 @@ monodroid_Mono_UnhandledException_internal ([[maybe_unused]] MonoException *ex) // Do nothing with it here, we let the exception naturally propagate on the managed side } -static void -monodroid_debugger_unhandled_exception (MonoException *ex) -{ - mono_debugger_agent_unhandled_exception (ex); -} - MonoDomain* MonodroidRuntime::create_and_initialize_domain (JNIEnv* env, jclass runtimeClass, jstring_array_wrapper &runtimeApks, jstring_array_wrapper &assemblies, [[maybe_unused]] jobjectArray assembliesBytes, From 77444c3d91821ec2565158be59e681d992ac1f42 Mon Sep 17 00:00:00 2001 From: Thays Date: Tue, 20 Jul 2021 18:20:12 -0300 Subject: [PATCH 3/5] Remove include. --- src/monodroid/jni/monodroid-glue.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/src/monodroid/jni/monodroid-glue.cc b/src/monodroid/jni/monodroid-glue.cc index 8e0fcee3a7b..91cc5820c3c 100644 --- a/src/monodroid/jni/monodroid-glue.cc +++ b/src/monodroid/jni/monodroid-glue.cc @@ -22,7 +22,6 @@ #include #include -#include #include #include #include From d45e9220ebc1ba8e937ec1e1c8c382a7651de115 Mon Sep 17 00:00:00 2001 From: Thays Date: Tue, 20 Jul 2021 23:33:15 -0300 Subject: [PATCH 4/5] Changing what @jonpryor suggested. --- src/Mono.Android/Android.Runtime/JNIEnv.cs | 12 ++++++------ src/Mono.Android/Android.Runtime/JNINativeWrapper.cs | 7 ++----- src/monodroid/jni/monodroid-glue.cc | 2 +- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/Mono.Android/Android.Runtime/JNIEnv.cs b/src/Mono.Android/Android.Runtime/JNIEnv.cs index 6c89a631923..e1fc5908e9d 100644 --- a/src/Mono.Android/Android.Runtime/JNIEnv.cs +++ b/src/Mono.Android/Android.Runtime/JNIEnv.cs @@ -241,19 +241,19 @@ static void ManualJavaObjectDispose (Java.Lang.Object obj) GC.SuppressFinalize (obj); } - static Action mono_unhandled_exception = null!; +#if NETCOREAPP + internal static Action mono_unhandled_exception = monodroid_debugger_unhandled_exception; +#else // NETCOREAPP + internal static Action mono_unhandled_exception = null!; +#endif // NETCOREAPP + static Action AppDomain_DoUnhandledException = null!; static void Initialize () { if (mono_unhandled_exception == null) { -#if NETCOREAPP - var mono_UnhandledException = typeof (Android.Runtime.JNIEnv) - .GetMethod ("monodroid_debugger_unhandled_exception", BindingFlags.NonPublic | BindingFlags.Static); -#else var mono_UnhandledException = typeof (System.Diagnostics.Debugger) .GetMethod ("Mono_UnhandledException", BindingFlags.NonPublic | BindingFlags.Static); -#endif if (mono_UnhandledException != null) mono_unhandled_exception = (Action) Delegate.CreateDelegate (typeof(Action), mono_UnhandledException); } diff --git a/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs b/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs index 7de58ad6141..88b1e3b110b 100644 --- a/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs +++ b/src/Mono.Android/Android.Runtime/JNINativeWrapper.cs @@ -22,11 +22,8 @@ static void get_runtime_types () AndroidEnvironment.FailFast ("Cannot find System.Diagnostics.Debugger.Mono_UnhandledException"); #endif #if NETCOREAPP - mono_unhandled_exception_method = typeof (Android.Runtime.JNIEnv).GetMethod ( - "monodroid_debugger_unhandled_exception", BindingFlags.NonPublic | BindingFlags.Static); - if (mono_unhandled_exception_method == null) - AndroidEnvironment.FailFast ("Cannot find Android.Runtime.JNIEnv.monodroid_debugger_unhandled_exception"); -#endif + mono_unhandled_exception_method = JNIEnv.mono_unhandled_exception.Method; +#endif // NETCOREAPP exception_handler_method = typeof (AndroidEnvironment).GetMethod ( "UnhandledException", BindingFlags.NonPublic | BindingFlags.Static); if (exception_handler_method == null) diff --git a/src/monodroid/jni/monodroid-glue.cc b/src/monodroid/jni/monodroid-glue.cc index 91cc5820c3c..0484e598cb5 100644 --- a/src/monodroid/jni/monodroid-glue.cc +++ b/src/monodroid/jni/monodroid-glue.cc @@ -1017,7 +1017,7 @@ MonodroidRuntime::init_android_runtime ( { mono_add_internal_call ("Java.Interop.TypeManager::monodroid_typemap_java_to_managed", reinterpret_cast(typemap_java_to_managed)); mono_add_internal_call ("Android.Runtime.JNIEnv::monodroid_typemap_managed_to_java", reinterpret_cast(typemap_managed_to_java)); -#if NET6 +#if defined (NET6) mono_add_internal_call ("Android.Runtime.JNIEnv::monodroid_debugger_unhandled_exception", reinterpret_cast (monodroid_debugger_unhandled_exception)); #endif From f16dfc563766d4791c1c37b586d096ce43a05839 Mon Sep 17 00:00:00 2001 From: Thays Date: Tue, 20 Jul 2021 23:34:32 -0300 Subject: [PATCH 5/5] Adding comment. --- src/Mono.Android/Android.Runtime/JNIEnv.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mono.Android/Android.Runtime/JNIEnv.cs b/src/Mono.Android/Android.Runtime/JNIEnv.cs index e1fc5908e9d..5dc17a24f49 100644 --- a/src/Mono.Android/Android.Runtime/JNIEnv.cs +++ b/src/Mono.Android/Android.Runtime/JNIEnv.cs @@ -690,7 +690,7 @@ public static string GetClassNameFromInstance (IntPtr jobject) #if NETCOREAPP [MethodImplAttribute(MethodImplOptions.InternalCall)] static extern unsafe void monodroid_debugger_unhandled_exception (Exception e); -#endif +#endif // NETCOREAPP internal static void LogTypemapTrace (StackTrace st) {