Skip to content

Commit 4b9c1a2

Browse files
Expose an icall to report unhandled exceptions in mscorlib.
1 parent b022c9b commit 4b9c1a2

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
@@ -1114,6 +1114,9 @@ internal Exception FixRemotingException ()
11141114

11151115
return this;
11161116
}
1117+
1118+
[MethodImplAttribute(MethodImplOptions.InternalCall)]
1119+
internal static extern void ReportUnhandledException(Exception exception);
11171120
#endif
11181121
}
11191122

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,10 +420,17 @@ public override void Post(SendOrPostCallback d, object state)
420420
[MonoPInvokeCallback(typeof(InvocationEntryDelegate))]
421421
private static void InvocationEntry(IntPtr arg)
422422
{
423-
var invocationContextHandle = GCHandle.FromIntPtr(arg);
424-
var invocationContext = (InvocationContext)invocationContextHandle.Target;
425-
invocationContextHandle.Free();
426-
invocationContext.Invoke();
423+
try
424+
{
425+
var invocationContextHandle = GCHandle.FromIntPtr(arg);
426+
var invocationContext = (InvocationContext)invocationContextHandle.Target;
427+
invocationContextHandle.Free();
428+
invocationContext.Invoke();
429+
}
430+
catch (Exception e)
431+
{
432+
Exception.ReportUnhandledException(e);
433+
}
427434
}
428435

429436
[AttributeUsage (AttributeTargets.Method)]

mono/metadata/exception.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,3 +1183,10 @@ mono_exception_from_name_four_strings_checked (MonoImage *image, const char *nam
11831183

11841184
return create_exception_four_strings (klass, a1, a2, a3, a4, error);
11851185
}
1186+
1187+
void
1188+
ves_icall_System_Exception_ReportUnhandledException(MonoObject *exc)
1189+
{
1190+
mono_unhandled_exception (exc);
1191+
mono_invoke_unhandled_exception_hook (exc);
1192+
}

mono/metadata/exception.h

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

164+
void
165+
ves_icall_System_Exception_ReportUnhandledException (MonoObject *exc);
166+
164167
MONO_END_DECLS
165168

166169
#endif /* _MONO_METADATA_EXCEPTION_H_ */

mono/metadata/icall-def.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,9 @@ HANDLES(ICALL(ENV_18, "internalGetGacPath", ves_icall_System_Environment_GetGacP
293293
HANDLES(ICALL(ENV_19, "internalGetHome", ves_icall_System_Environment_InternalGetHome))
294294
ICALL(ENV_20, "set_ExitCode", mono_environment_exitcode_set)
295295

296+
ICALL_TYPE(EXCEPTION, "System.Exception", EXCEPTION_1)
297+
HANDLES(ICALL(EXCEPTION_1, "ReportUnhandledException", ves_icall_System_Exception_ReportUnhandledException))
298+
296299
ICALL_TYPE(GC, "System.GC", GC_0)
297300
ICALL(GC_0, "GetCollectionCount", mono_gc_collection_count)
298301
ICALL(GC_0a, "GetGeneration", mono_gc_get_generation)

0 commit comments

Comments
 (0)