Skip to content

Commit e4495a6

Browse files
TautvydasZilysJosh Peterson
authored andcommitted
Expose an icall to report unhandled exceptions in mscorlib.
1 parent 02c0f70 commit e4495a6

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

mcs/class/referencesource/mscorlib/system/exception.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,9 @@ internal Exception FixRemotingException ()
11381138

11391139
return this;
11401140
}
1141+
1142+
[MethodImplAttribute(MethodImplOptions.InternalCall)]
1143+
internal static extern void ReportUnhandledException(Exception exception);
11411144
#endif
11421145
}
11431146

mcs/class/referencesource/mscorlib/system/threading/synchronizationcontext.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,17 @@ public override void Post(SendOrPostCallback d, object state)
424424
[MonoPInvokeCallback(typeof(InvocationEntryDelegate))]
425425
private static void InvocationEntry(IntPtr arg)
426426
{
427-
var invocationContextHandle = GCHandle.FromIntPtr(arg);
428-
var invocationContext = (InvocationContext)invocationContextHandle.Target;
429-
invocationContextHandle.Free();
430-
invocationContext.Invoke();
427+
try
428+
{
429+
var invocationContextHandle = GCHandle.FromIntPtr(arg);
430+
var invocationContext = (InvocationContext)invocationContextHandle.Target;
431+
invocationContextHandle.Free();
432+
invocationContext.Invoke();
433+
}
434+
catch (Exception e)
435+
{
436+
Exception.ReportUnhandledException(e);
437+
}
431438
}
432439

433440
[AttributeUsage (AttributeTargets.Method)]

mono/metadata/exception.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,3 +1533,10 @@ mono_error_convert_to_exception_handle (MonoError *error)
15331533
return is_ok (error) ? MONO_HANDLE_CAST (MonoException, NULL_HANDLE)
15341534
: MONO_HANDLE_NEW (MonoException, mono_error_convert_to_exception (error));
15351535
}
1536+
1537+
void
1538+
ves_icall_System_Exception_ReportUnhandledException(MonoObject *exc)
1539+
{
1540+
mono_unhandled_exception (exc);
1541+
mono_invoke_unhandled_exception_hook (exc);
1542+
}

mono/metadata/exception.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ typedef void (*MonoUnhandledExceptionFunc) (MonoObject *exc, void *user
172172
MONO_API void mono_install_unhandled_exception_hook (MonoUnhandledExceptionFunc func, void *user_data);
173173
void mono_invoke_unhandled_exception_hook (MonoObject *exc);
174174

175+
void
176+
ves_icall_System_Exception_ReportUnhandledException (MonoObject *exc);
177+
175178
MONO_END_DECLS
176179

177180
#endif /* _MONO_METADATA_EXCEPTION_H_ */

mono/metadata/icall-def.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,9 @@ HANDLES(GC_7, "_SuppressFinalize", ves_icall_System_GC_SuppressFinalize, void, 1
357357
HANDLES(GC_9, "get_ephemeron_tombstone", ves_icall_System_GC_get_ephemeron_tombstone, MonoObject, 0, ())
358358
HANDLES(GC_8, "register_ephemeron_array", ves_icall_System_GC_register_ephemeron_array, void, 1, (MonoObject))
359359

360+
ICALL_TYPE(EXCEPTION, "System.Exception", EXCEPTION_1)
361+
HANDLES(ICALL(EXCEPTION_1, "ReportUnhandledException", ves_icall_System_Exception_ReportUnhandledException))
362+
360363
ICALL_TYPE(CALDATA, "System.Globalization.CalendarData", CALDATA_1)
361364
HANDLES(CALDATA_1, "fill_calendar_data", ves_icall_System_Globalization_CalendarData_fill_calendar_data, MonoBoolean, 3, (MonoCalendarData, MonoString, gint32))
362365

0 commit comments

Comments
 (0)