Skip to content

Commit e4debf7

Browse files
authored
[One .NET] Use new API for exception debugger notification (#6106)
[One .NET] Use Mono embedding API for exception debugger notification (#6106) Context: dotnet/runtime#56071 Context: #4877 Context: #4927 (comment) Context: #4927 (comment) Context: xamarin/monodroid@3e9de5a Context: xamarin/monodroid@b0f8597 Context: xamarin/monodroid@12a012e What should happen when an exception is thrown and a debugger is attached? This is in fact a loaded question: there's what Xamarin.Android (Legacy) *has* done, vs. what .NET 6 Android for .NET does, vs. what "should" happen. What "should" happen is easiest: 1. We should behave like a "normal" Desktop .NET app when a debugger is attached, AND 2. We shouldn't corrupt JVM state. Unfortunately, (1)+(2) is currently not possible, in part because Java doesn't have an equivalent to Windows' [two attempt][0] debugger notification infrastructure. See #4877 for details. What Legacy Xamarin.Android does is also detailed in #4877, and relies on the `Debugger.Mono_UnhandledException()` method in order to alert an attached debugger that there is an exception to show to the user. However, `Debugger.Mono_UnhandledException()` never made it to the `dotnet/runtime` repo. It never existed there. Thus, what .NET 6 Android for .NET *currently* does is…*nothing*. If an exception is thrown and a debugger is attached, the debugger is *not* notified. Eventually you'll get an unhandled exception, long after it was originally thrown; see commit c1a2ee7. PR dotnet/runtime#56071 added a new `mono_debugger_agent_unhandled_exception()` Mono embedding API which is equivalent to `Debugger.Mono_UnhandledException()` for use with .NET 6 + MonoVM. Update `src/Mono.Android` and `src/monodroid` so that `mono_debugger_agent_unhandled_exception()` is used to alert the debugger that an exception has been thrown at a JNI boundary. This should allow .NET 6 + Android to have equivalent exception handling semantics as legacy Xamarin.Android. [0]: https://docs.microsoft.com/en-us/windows/win32/debug/debugger-exception-handling
1 parent 97e543a commit e4debf7

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

src/Mono.Android/Android.Runtime/JNIEnv.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,12 @@ static void ManualJavaObjectDispose (Java.Lang.Object obj)
241241
GC.SuppressFinalize (obj);
242242
}
243243

244-
static Action<Exception> mono_unhandled_exception = null!;
244+
#if NETCOREAPP
245+
internal static Action<Exception> mono_unhandled_exception = monodroid_debugger_unhandled_exception;
246+
#else // NETCOREAPP
247+
internal static Action<Exception> mono_unhandled_exception = null!;
248+
#endif // NETCOREAPP
249+
245250
#if !NETCOREAPP
246251
static Action<AppDomain, UnhandledExceptionEventArgs> AppDomain_DoUnhandledException = null!;
247252
#endif // ndef NETCOREAPP
@@ -695,6 +700,11 @@ public static string GetClassNameFromInstance (IntPtr jobject)
695700
[MethodImplAttribute(MethodImplOptions.InternalCall)]
696701
static extern unsafe IntPtr monodroid_typemap_managed_to_java (Type type, byte* mvid);
697702

703+
#if NETCOREAPP
704+
[MethodImplAttribute(MethodImplOptions.InternalCall)]
705+
static extern unsafe void monodroid_debugger_unhandled_exception (Exception e);
706+
#endif // NETCOREAPP
707+
698708
internal static void LogTypemapTrace (StackTrace st)
699709
{
700710
string? trace = st.ToString ()?.Trim ();

src/Mono.Android/Android.Runtime/JNINativeWrapper.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ static void get_runtime_types ()
2121
if (mono_unhandled_exception_method == null)
2222
AndroidEnvironment.FailFast ("Cannot find System.Diagnostics.Debugger.Mono_UnhandledException");
2323
#endif
24+
#if NETCOREAPP
25+
mono_unhandled_exception_method = JNIEnv.mono_unhandled_exception.Method;
26+
#endif // NETCOREAPP
2427
exception_handler_method = typeof (AndroidEnvironment).GetMethod (
2528
"UnhandledException", BindingFlags.NonPublic | BindingFlags.Static);
2629
if (exception_handler_method == null)

src/monodroid/jni/monodroid-glue-internal.hh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ namespace xamarin::android::internal
286286
static MonoReflectionType* typemap_java_to_managed (MonoString *java_type_name);
287287

288288
static const char* typemap_managed_to_java (MonoReflectionType *type, const uint8_t *mvid);
289+
#if defined (NET6)
290+
static void monodroid_debugger_unhandled_exception (MonoException *ex);
291+
#endif
289292

290293
#if defined (DEBUG)
291294
void set_debug_env_vars (void);

src/monodroid/jni/monodroid-glue.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,14 @@ MonodroidRuntime::lookup_bridge_info (MonoDomain *domain, MonoImage *image, cons
10011001
}
10021002
#endif // ndef NET6
10031003

1004+
#if defined (NET6)
1005+
void
1006+
MonodroidRuntime::monodroid_debugger_unhandled_exception (MonoException *ex)
1007+
{
1008+
mono_debugger_agent_unhandled_exception (ex);
1009+
}
1010+
#endif
1011+
10041012
void
10051013
MonodroidRuntime::init_android_runtime (
10061014
#if !defined (NET6)
@@ -1011,6 +1019,7 @@ MonodroidRuntime::init_android_runtime (
10111019
mono_add_internal_call ("Java.Interop.TypeManager::monodroid_typemap_java_to_managed", reinterpret_cast<const void*>(typemap_java_to_managed));
10121020
mono_add_internal_call ("Android.Runtime.JNIEnv::monodroid_typemap_managed_to_java", reinterpret_cast<const void*>(typemap_managed_to_java));
10131021
#if defined (NET6)
1022+
mono_add_internal_call ("Android.Runtime.JNIEnv::monodroid_debugger_unhandled_exception", reinterpret_cast<const void*> (monodroid_debugger_unhandled_exception));
10141023
mono_add_internal_call ("Android.Runtime.JNIEnv::monodroid_unhandled_exception", reinterpret_cast<const void*>(monodroid_unhandled_exception));
10151024
#endif // def NET6
10161025

0 commit comments

Comments
 (0)