[SRE] Remove more unused icall declarations.
[mono.git] / mono / metadata / exception.c
index 76f70a3dcfc7e0a0a0c9994186aad2998b18a35e..02b738556c597c72fbc7ed9fe76a5acead5d97eb 100644 (file)
@@ -14,6 +14,7 @@
 
 #include <glib.h>
 #include <config.h>
+#include <mono/metadata/environment.h>
 #include <mono/metadata/exception.h>
 #include <mono/metadata/exception-internals.h>
 
 #include <mono/metadata/appdomain.h>
 #include <mono/metadata/mono-debug.h>
 #include <mono/utils/mono-error-internals.h>
+#include <mono/utils/mono-logger-internals.h>
 #include <string.h>
 
 #ifdef HAVE_EXECINFO_H
 #include <execinfo.h>
 #endif
 
+static MonoUnhandledExceptionFunc unhandled_exception_hook = NULL;
+static gpointer unhandled_exception_hook_data = NULL;
+
 /**
  * mono_exception_from_name:
  * @image: the Mono image where to look for the class
@@ -245,16 +250,31 @@ mono_exception_from_token_two_strings (MonoImage *image, guint32 token,
                                                                           MonoString *a1, MonoString *a2)
 {
        MonoError error;
-       MonoClass *klass;
        MonoException *ret;
+       ret = mono_exception_from_token_two_strings_checked (image, token, a1, a2, &error);
+       mono_error_cleanup (&error);
+       return ret;
+}
 
-       klass = mono_class_get_checked (image, token, &error);
-       mono_error_assert_ok (&error); /* FIXME handle the error. */
+/**
+ * mono_exception_from_token_two_strings_checked:
+ *
+ *   Same as mono_exception_from_name_two_strings, but lookup the exception class using
+ * IMAGE and TOKEN.
+ */
+MonoException *
+mono_exception_from_token_two_strings_checked (MonoImage *image, guint32 token,
+                                              MonoString *a1, MonoString *a2,
+                                              MonoError *error)
+{
+       MonoClass *klass;
+
+       mono_error_init (error);
 
-       ret = create_exception_two_strings (klass, a1, a2, &error);
-       mono_error_raise_exception (&error); /* FIXME don't raise here */
+       klass = mono_class_get_checked (image, token, error);
+       mono_error_assert_ok (error); /* FIXME handle the error. */
 
-       return ret;
+       return create_exception_two_strings (klass, a1, a2, error);
 }
 
 /**
@@ -971,7 +991,7 @@ mono_exception_get_native_backtrace (MonoException *exc)
                }
        }
 
-       free (messages);
+       g_free (messages);
        return g_string_free (text, FALSE);
 #else
        return g_strdup ("");
@@ -1036,3 +1056,50 @@ mono_error_set_pending_exception (MonoError *error)
        }
 }
 
+void
+mono_install_unhandled_exception_hook (MonoUnhandledExceptionFunc func, gpointer user_data)
+{
+       unhandled_exception_hook = func;
+       unhandled_exception_hook_data = user_data;
+}
+
+void
+mono_invoke_unhandled_exception_hook (MonoObject *exc)
+{
+       if (unhandled_exception_hook) {
+               unhandled_exception_hook (exc, unhandled_exception_hook_data);
+       } else {
+               MonoError inner_error;
+               MonoObject *other = NULL;
+               MonoString *str = mono_object_try_to_string (exc, &other, &inner_error);
+               char *msg = NULL;
+               
+               if (str && is_ok (&inner_error)) {
+                       msg = mono_string_to_utf8_checked (str, &inner_error);
+                       if (!is_ok (&inner_error)) {
+                               msg = g_strdup_printf ("Nested exception while formatting original exception");
+                               mono_error_cleanup (&inner_error);
+                       }
+               } else if (other) {
+                       char *original_backtrace = mono_exception_get_managed_backtrace ((MonoException*)exc);
+                       char *nested_backtrace = mono_exception_get_managed_backtrace ((MonoException*)other);
+
+                       msg = g_strdup_printf ("Nested exception detected.\nOriginal Exception: %s\nNested exception:%s\n",
+                               original_backtrace, nested_backtrace);
+
+                       g_free (original_backtrace);
+                       g_free (nested_backtrace);
+               } else {
+                       msg = g_strdup ("Nested exception trying to figure out what went wrong");
+               }
+               mono_runtime_printf_err ("[ERROR] FATAL UNHANDLED EXCEPTION: %s", msg);
+               g_free (msg);
+#if defined(HOST_IOS)
+               g_assertion_message ("Terminating runtime due to unhandled exception");
+#else
+               exit (mono_environment_exitcode_get ());
+#endif
+       }
+
+       g_assert_not_reached ();
+}