6 * Paolo Molaro (lupus@ximian.com)
7 * Dietmar Maurer (dietmar@ximian.com)
8 * Dick Porter (dick@ximian.com)
9 * Miguel de Icaza (miguel@ximian.com)
11 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
12 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
13 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
18 #include <mono/metadata/environment.h>
19 #include <mono/metadata/exception.h>
20 #include <mono/metadata/exception-internals.h>
22 #include <mono/metadata/object-internals.h>
23 #include <mono/metadata/metadata-internals.h>
24 #include <mono/metadata/appdomain.h>
25 #include <mono/metadata/mono-debug.h>
26 #include <mono/utils/mono-error-internals.h>
27 #include <mono/utils/mono-logger-internals.h>
30 #ifdef HAVE_EXECINFO_H
34 static MonoUnhandledExceptionFunc unhandled_exception_hook = NULL;
35 static gpointer unhandled_exception_hook_data = NULL;
38 * mono_exception_from_name:
39 * @image: the Mono image where to look for the class
40 * @name_space: the namespace for the class
43 * Creates an exception of the given namespace/name class in the
46 * Returns: the initialized exception instance.
49 mono_exception_from_name (MonoImage *image, const char *name_space,
52 return mono_exception_from_name_domain (mono_domain_get (), image, name_space, name);
56 * mono_exception_from_name_domain:
57 * @domain: Domain where the return object will be created.
58 * @image: the Mono image where to look for the class
59 * @name_space: the namespace for the class
62 * Creates an exception object of the given namespace/name class on
65 * Returns: the initialized exception instance.
68 mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image,
69 const char* name_space, const char *name)
74 MonoDomain *caller_domain = mono_domain_get ();
76 klass = mono_class_load_from_name (image, name_space, name);
78 o = mono_object_new_checked (domain, klass, &error);
79 mono_error_assert_ok (&error);
81 if (domain != caller_domain)
82 mono_domain_set_internal (domain);
83 mono_runtime_object_init_checked (o, &error);
84 mono_error_assert_ok (&error);
86 if (domain != caller_domain)
87 mono_domain_set_internal (caller_domain);
89 return (MonoException *)o;
94 * mono_exception_from_token:
95 * @image: the Mono image where to look for the class
96 * @token: The type token of the class
98 * Creates an exception of the type given by @token.
100 * Returns: the initialized exception instance.
103 mono_exception_from_token (MonoImage *image, guint32 token)
109 klass = mono_class_get_checked (image, token, &error);
110 mono_error_assert_ok (&error);
112 o = mono_object_new_checked (mono_domain_get (), klass, &error);
113 mono_error_assert_ok (&error);
115 mono_runtime_object_init_checked (o, &error);
116 mono_error_assert_ok (&error);
118 return (MonoException *)o;
121 static MonoException *
122 create_exception_two_strings (MonoClass *klass, MonoString *a1, MonoString *a2, MonoError *error)
124 MonoDomain *domain = mono_domain_get ();
125 MonoMethod *method = NULL;
135 o = mono_object_new_checked (domain, klass, error);
136 mono_error_assert_ok (error);
139 while ((m = mono_class_get_methods (klass, &iter))) {
140 MonoMethodSignature *sig;
142 if (strcmp (".ctor", mono_method_get_name (m)))
144 sig = mono_method_signature (m);
145 if (sig->param_count != count)
148 if (sig->params [0]->type != MONO_TYPE_STRING)
150 if (count == 2 && sig->params [1]->type != MONO_TYPE_STRING)
159 mono_runtime_invoke_checked (method, o, args, error);
160 return_val_if_nok (error, NULL);
162 return (MonoException *) o;
166 * mono_exception_from_name_two_strings:
167 * @image: the Mono image where to look for the class
168 * @name_space: the namespace for the class
170 * @a1: first string argument to pass
171 * @a2: second string argument to pass
173 * Creates an exception from a constructor that takes two string
176 * Returns: the initialized exception instance.
179 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
180 const char *name, MonoString *a1, MonoString *a2)
185 ret = mono_exception_from_name_two_strings_checked (image, name_space, name, a1, a2, &error);
186 mono_error_cleanup (&error);
191 * mono_exception_from_name_two_strings_checked:
192 * @image: the Mono image where to look for the class
193 * @name_space: the namespace for the class
195 * @a1: first string argument to pass
196 * @a2: second string argument to pass
197 * @error: set on error
199 * Creates an exception from a constructor that takes two string
202 * Returns: the initialized exception instance. On failure returns
203 * NULL and sets @error.
206 mono_exception_from_name_two_strings_checked (MonoImage *image, const char *name_space,
207 const char *name, MonoString *a1, MonoString *a2,
213 klass = mono_class_load_from_name (image, name_space, name);
215 return create_exception_two_strings (klass, a1, a2, error);
219 * mono_exception_from_name_msg:
220 * @image: the Mono image where to look for the class
221 * @name_space: the namespace for the class
223 * @msg: the message to embed inside the exception
225 * Creates an exception and initializes its message field.
227 * Returns: the initialized exception instance.
230 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
231 const char *name, const char *msg)
235 ex = mono_exception_from_name (image, name_space, name);
238 MONO_OBJECT_SETREF (ex, message, mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg));
244 * mono_exception_from_token_two_strings:
246 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
250 mono_exception_from_token_two_strings (MonoImage *image, guint32 token,
251 MonoString *a1, MonoString *a2)
255 ret = mono_exception_from_token_two_strings_checked (image, token, a1, a2, &error);
256 mono_error_cleanup (&error);
261 * mono_exception_from_token_two_strings_checked:
263 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
267 mono_exception_from_token_two_strings_checked (MonoImage *image, guint32 token,
268 MonoString *a1, MonoString *a2,
275 klass = mono_class_get_checked (image, token, error);
276 mono_error_assert_ok (error); /* FIXME handle the error. */
278 return create_exception_two_strings (klass, a1, a2, error);
282 * mono_get_exception_divide_by_zero:
284 * Returns: a new instance of the `System.DivideByZeroException`
287 mono_get_exception_divide_by_zero ()
289 return mono_exception_from_name (mono_get_corlib (), "System",
290 "DivideByZeroException");
294 * mono_get_exception_security:
296 * Returns: a new instance of the `System.Security.SecurityException`
299 mono_get_exception_security ()
301 return mono_exception_from_name (mono_get_corlib (), "System.Security",
302 "SecurityException");
306 * mono_get_exception_thread_abort:
308 * Returns: a new instance of the `System.Threading.ThreadAbortException`
311 mono_get_exception_thread_abort ()
313 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
314 "ThreadAbortException");
318 * mono_get_exception_thread_interrupted:
320 * Returns: a new instance of the `System.Threading.ThreadInterruptedException`
323 mono_get_exception_thread_interrupted ()
325 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
326 "ThreadInterruptedException");
330 * mono_get_exception_arithmetic:
332 * Returns: a new instance of the `System.ArithmeticException`
335 mono_get_exception_arithmetic ()
337 return mono_exception_from_name (mono_get_corlib (), "System",
338 "ArithmeticException");
342 * mono_get_exception_overflow:
344 * Returns: a new instance of the `System.OverflowException`
347 mono_get_exception_overflow ()
349 return mono_exception_from_name (mono_get_corlib (), "System",
350 "OverflowException");
354 * mono_get_exception_null_reference:
356 * Returns: a new instance of the `System.NullReferenceException`
359 mono_get_exception_null_reference ()
361 return mono_exception_from_name (mono_get_corlib (), "System",
362 "NullReferenceException");
366 * mono_get_exception_execution_engine:
367 * @msg: the message to pass to the user
369 * Returns: a new instance of the `System.ExecutionEngineException`
372 mono_get_exception_execution_engine (const char *msg)
374 return mono_exception_from_name_msg (mono_get_corlib (), "System", "ExecutionEngineException", msg);
378 * mono_get_exception_serialization:
379 * @msg: the message to pass to the user
381 * Returns: a new instance of the `System.Runtime.Serialization.SerializationException`
384 mono_get_exception_serialization (const char *msg)
386 return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization", "SerializationException", msg);
390 * mono_get_exception_invalid_cast:
392 * Returns: a new instance of the `System.InvalidCastException`
395 mono_get_exception_invalid_cast ()
397 return mono_exception_from_name (mono_get_corlib (), "System", "InvalidCastException");
401 * mono_get_exception_invalid_operation:
402 * @msg: the message to pass to the user
404 * Returns: a new instance of the `System.InvalidOperationException`
407 mono_get_exception_invalid_operation (const char *msg)
409 return mono_exception_from_name_msg (mono_get_corlib (), "System",
410 "InvalidOperationException", msg);
414 * mono_get_exception_index_out_of_range:
416 * Returns: a new instance of the `System.IndexOutOfRangeException`
419 mono_get_exception_index_out_of_range ()
421 return mono_exception_from_name (mono_get_corlib (), "System",
422 "IndexOutOfRangeException");
426 * mono_get_exception_array_type_mismatch:
428 * Returns: a new instance of the `System.ArrayTypeMismatchException`
431 mono_get_exception_array_type_mismatch ()
433 return mono_exception_from_name (mono_get_corlib (), "System",
434 "ArrayTypeMismatchException");
438 * mono_get_exception_type_load:
439 * @class_name: the name of the class that could not be loaded
440 * @assembly_name: the assembly where the class was looked up.
442 * Returns: a new instance of the `System.TypeLoadException`
445 mono_get_exception_type_load (MonoString *class_name, char *assembly_name)
447 MonoString *s = assembly_name ? mono_string_new (mono_domain_get (), assembly_name) : mono_string_new (mono_domain_get (), "");
450 MonoException *ret = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System",
451 "TypeLoadException", class_name, s, &error);
452 mono_error_assert_ok (&error);
457 * mono_get_exception_not_implemented:
458 * @msg: the message to pass to the user
460 * Returns: a new instance of the `System.NotImplementedException`
463 mono_get_exception_not_implemented (const char *msg)
465 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotImplementedException", msg);
469 * mono_get_exception_not_supported:
470 * @msg: the message to pass to the user
472 * Returns: a new instance of the `System.NotSupportedException`
475 mono_get_exception_not_supported (const char *msg)
477 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotSupportedException", msg);
481 * mono_get_exception_missing_method:
482 * @class_name: the class where the lookup was performed.
483 * @member_name: the name of the missing method.
485 * Returns: a new instance of the `System.MissingMethodException`
488 mono_get_exception_missing_method (const char *class_name, const char *member_name)
490 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
491 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
494 MonoException *ret = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System",
495 "MissingMethodException", s1, s2, &error);
496 mono_error_assert_ok (&error);
501 * mono_get_exception_missing_field:
502 * @class_name: the class where the lookup was performed
503 * @member_name: the name of the missing method.
505 * Returns: a new instance of the `System.MissingFieldException`
508 mono_get_exception_missing_field (const char *class_name, const char *member_name)
510 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
511 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
514 MonoException *ret = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System",
515 "MissingFieldException", s1, s2, &error);
516 mono_error_assert_ok (&error);
521 * mono_get_exception_argument_null:
522 * @arg: the name of the argument that is null
524 * Returns: a new instance of the `System.ArgumentNullException`
527 mono_get_exception_argument_null (const char *arg)
531 ex = mono_exception_from_name (
532 mono_get_corlib (), "System", "ArgumentNullException");
535 MonoArgumentException *argex = (MonoArgumentException *)ex;
536 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
543 * mono_get_exception_argument:
544 * @arg: the name of the invalid argument.
546 * Returns: a new instance of the `System.ArgumentException`
549 mono_get_exception_argument (const char *arg, const char *msg)
553 ex = mono_exception_from_name_msg (
554 mono_get_corlib (), "System", "ArgumentException", msg);
557 MonoArgumentException *argex = (MonoArgumentException *)ex;
558 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
565 * mono_get_exception_argument_out_of_range:
566 * @arg: the name of the out of range argument.
568 * Returns: a new instance of the `System.ArgumentOutOfRangeException`
571 mono_get_exception_argument_out_of_range (const char *arg)
575 ex = mono_exception_from_name (
576 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
579 MonoArgumentException *argex = (MonoArgumentException *)ex;
580 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
587 * mono_get_exception_thread_state:
588 * @msg: the message to present to the user
590 * Returns: a new instance of the `System.Threading.ThreadStateException`
593 mono_get_exception_thread_state (const char *msg)
595 return mono_exception_from_name_msg (
596 mono_get_corlib (), "System.Threading", "ThreadStateException", msg);
600 * mono_get_exception_io:
601 * @msg: the message to present to the user
603 * Returns: a new instance of the `System.IO.IOException`
606 mono_get_exception_io (const char *msg)
608 return mono_exception_from_name_msg (
609 mono_get_corlib (), "System.IO", "IOException", msg);
613 * mono_get_exception_file_not_found:
614 * @fname: the name of the file not found.
616 * Returns: a new instance of the `System.IO.FileNotFoundException`
619 mono_get_exception_file_not_found (MonoString *fname)
622 MonoException *ret = mono_exception_from_name_two_strings_checked (
623 mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname, &error);
624 mono_error_assert_ok (&error);
629 * mono_get_exception_file_not_found2:
630 * @msg: an informative message for the user.
631 * @fname: the name of the file not found.
633 * Returns: a new instance of the `System.IO.FileNotFoundException`
636 mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
638 MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
641 MonoException *ret = mono_exception_from_name_two_strings_checked (
642 mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname, &error);
643 mono_error_assert_ok (&error);
648 * mono_get_exception_type_initialization:
649 * @type_name: the name of the type that failed to initialize.
650 * @inner: the inner exception.
652 * Returns: a new instance of the `System.TypeInitializationException`
655 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
658 MonoException *ret = mono_get_exception_type_initialization_checked (type_name, inner, &error);
659 if (!is_ok (&error)) {
660 mono_error_cleanup (&error);
668 mono_get_exception_type_initialization_checked (const gchar *type_name, MonoException *inner, MonoError *error)
676 klass = mono_class_load_from_name (mono_get_corlib (), "System", "TypeInitializationException");
678 mono_class_init (klass);
681 while ((method = mono_class_get_methods (klass, &iter))) {
682 if (!strcmp (".ctor", mono_method_get_name (method))) {
683 MonoMethodSignature *sig = mono_method_signature (method);
685 if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_STRING && mono_class_from_mono_type (sig->params [1]) == mono_defaults.exception_class)
692 args [0] = mono_string_new (mono_domain_get (), type_name);
695 exc = mono_object_new_checked (mono_domain_get (), klass, error);
696 mono_error_assert_ok (error);
698 mono_runtime_invoke_checked (method, exc, args, error);
699 return_val_if_nok (error, NULL);
701 return (MonoException *) exc;
705 * mono_get_exception_synchronization_lock:
706 * @inner: the inner exception.
708 * Returns: a new instance of the `System.SynchronizationLockException`
711 mono_get_exception_synchronization_lock (const char *msg)
713 return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg);
717 * mono_get_exception_cannot_unload_appdomain:
718 * @inner: the inner exception.
720 * Returns: a new instance of the `System.CannotUnloadAppDomainException`
723 mono_get_exception_cannot_unload_appdomain (const char *msg)
725 return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg);
729 * mono_get_exception_appdomain_unloaded
731 * Returns: a new instance of the `System.AppDomainUnloadedException`
734 mono_get_exception_appdomain_unloaded (void)
736 return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
740 * mono_get_exception_bad_image_format:
741 * @msg: an informative message for the user.
743 * Returns: a new instance of the `System.BadImageFormatException`
746 mono_get_exception_bad_image_format (const char *msg)
748 return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg);
752 * mono_get_exception_bad_image_format2:
753 * @msg: an informative message for the user.
754 * @fname: The full name of the file with the invalid image.
756 * Returns: a new instance of the `System.BadImageFormatException`
759 mono_get_exception_bad_image_format2 (const char *msg, MonoString *fname)
761 MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
764 MonoException *ret = mono_exception_from_name_two_strings_checked (
765 mono_get_corlib (), "System", "BadImageFormatException", s, fname, &error);
766 mono_error_assert_ok (&error);
771 * mono_get_exception_stack_overflow:
773 * Returns: a new instance of the `System.StackOverflowException`
776 mono_get_exception_stack_overflow (void)
778 return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");
782 * mono_get_exception_out_of_memory:
784 * Returns: a new instance of the `System.OutOfMemoryException`
787 mono_get_exception_out_of_memory (void)
789 return mono_exception_from_name (mono_get_corlib (), "System", "OutOfMemoryException");
793 * mono_get_exception_field_access:
795 * Returns: a new instance of the `System.FieldAccessException`
798 mono_get_exception_field_access (void)
800 return mono_exception_from_name (mono_get_corlib (), "System", "FieldAccessException");
804 * mono_get_exception_field_access2:
805 * @msg: an informative message for the user.
807 * Returns: a new instance of the `System.FieldAccessException`
810 mono_get_exception_field_access_msg (const char *msg)
812 return mono_exception_from_name_msg (mono_get_corlib (), "System", "FieldAccessException", msg);
816 * mono_get_exception_method_access:
818 * Returns: a new instance of the `System.MethodAccessException`
821 mono_get_exception_method_access (void)
823 return mono_exception_from_name (mono_get_corlib (), "System", "MethodAccessException");
827 * mono_get_exception_method_access2:
828 * @msg: an informative message for the user.
830 * Returns: a new instance of the `System.MethodAccessException`
833 mono_get_exception_method_access_msg (const char *msg)
835 return mono_exception_from_name_msg (mono_get_corlib (), "System", "MethodAccessException", msg);
839 * mono_get_exception_reflection_type_load:
840 * @types: an array of types that were defined in the moduled loaded.
841 * @exceptions: an array of exceptions that were thrown during the type loading.
843 * Returns: a new instance of the `System.Reflection.ReflectionTypeLoadException`
846 mono_get_exception_reflection_type_load (MonoArray *types_raw, MonoArray *exceptions_raw)
848 HANDLE_FUNCTION_ENTER ();
850 MONO_HANDLE_DCL (MonoArray, types);
851 MONO_HANDLE_DCL (MonoArray, exceptions);
852 MonoExceptionHandle ret = mono_get_exception_reflection_type_load_checked (types, exceptions, &error);
853 if (is_ok (&error)) {
854 mono_error_cleanup (&error);
855 ret = MONO_HANDLE_CAST (MonoException, NULL_HANDLE);
860 HANDLE_FUNCTION_RETURN_OBJ (ret);
865 mono_get_exception_reflection_type_load_checked (MonoArrayHandle types, MonoArrayHandle exceptions, MonoError *error)
873 klass = mono_class_load_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
875 mono_class_init (klass);
877 /* Find the Type[], Exception[] ctor */
879 while ((method = mono_class_get_methods (klass, &iter))) {
880 if (!strcmp (".ctor", mono_method_get_name (method))) {
881 MonoMethodSignature *sig = mono_method_signature (method);
883 if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_SZARRAY && sig->params [1]->type == MONO_TYPE_SZARRAY)
890 MonoExceptionHandle exc = MONO_HANDLE_NEW (MonoException, mono_object_new_checked (mono_domain_get (), klass, error));
891 mono_error_assert_ok (error);
894 args [0] = MONO_HANDLE_RAW (types);
895 args [1] = MONO_HANDLE_RAW (exceptions);
897 mono_runtime_invoke_checked (method, MONO_HANDLE_RAW (exc), args, error);
898 return_val_if_nok (error, MONO_HANDLE_CAST (MonoException, NULL_HANDLE));
904 mono_get_exception_runtime_wrapped (MonoObject *wrapped_exception)
907 MonoException *ret = mono_get_exception_runtime_wrapped_checked (wrapped_exception, &error);
908 if (!is_ok (&error)) {
909 mono_error_cleanup (&error);
917 mono_get_exception_runtime_wrapped_checked (MonoObject *wrapped_exception, MonoError *error)
922 MonoDomain *domain = mono_domain_get ();
923 gpointer params [16];
925 klass = mono_class_load_from_name (mono_get_corlib (), "System.Runtime.CompilerServices", "RuntimeWrappedException");
927 o = mono_object_new_checked (domain, klass, error);
928 mono_error_assert_ok (error);
929 g_assert (o != NULL);
931 method = mono_class_get_method_from_name (klass, ".ctor", 1);
934 params [0] = wrapped_exception;
936 mono_runtime_invoke_checked (method, o, params, error);
937 return_val_if_nok (error, NULL);
939 return (MonoException *)o;
943 append_frame_and_continue (MonoMethod *method, gpointer ip, size_t native_offset, gboolean managed, gpointer user_data)
945 MonoDomain *domain = mono_domain_get ();
946 GString *text = (GString*)user_data;
949 char *msg = mono_debug_print_stack_frame (method, native_offset, domain);
950 g_string_append_printf (text, "%s\n", msg);
953 g_string_append_printf (text, "<unknown native frame 0x%x>\n", ip);
960 mono_exception_get_managed_backtrace (MonoException *exc)
964 text = g_string_new_len (NULL, 20);
966 if (!mono_get_eh_callbacks ()->mono_exception_walk_trace (exc, append_frame_and_continue, text))
967 g_string_append (text, "managed backtrace not available\n");
969 return g_string_free (text, FALSE);
973 mono_exception_handle_get_native_backtrace (MonoExceptionHandle exc)
975 #ifdef HAVE_BACKTRACE_SYMBOLS
977 MonoArrayHandle arr = MONO_HANDLE_NEW(MonoArray, NULL);
982 MONO_HANDLE_GET (arr, exc, native_trace_ips);
984 if (MONO_HANDLE_IS_NULL(arr))
985 return g_strdup ("");
986 domain = mono_domain_get ();
987 len = mono_array_handle_length (arr);
988 text = g_string_new_len (NULL, len * 20);
990 void *addr = MONO_ARRAY_HANDLE_PIN (arr, gpointer, 0, &gchandle);
992 messages = backtrace_symbols (addr, len);
994 mono_gchandle_free (gchandle);
996 for (i = 0; i < len; ++i) {
998 MONO_HANDLE_ARRAY_GETVAL (ip, arr, gpointer, i);
999 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), (char *)ip);
1001 char *msg = mono_debug_print_stack_frame (mono_jit_info_get_method (ji), (char*)ip - (char*)ji->code_start, domain);
1002 g_string_append_printf (text, "%s\n", msg);
1005 g_string_append_printf (text, "%s\n", messages [i]);
1010 return g_string_free (text, FALSE);
1012 return g_strdup ("");
1017 ves_icall_Mono_Runtime_GetNativeStackTrace (MonoExceptionHandle exc, MonoError *error)
1020 MonoStringHandle res;
1024 mono_error_set_argument_null (error, "exception", "");
1025 return NULL_HANDLE_STRING;
1028 trace = mono_exception_handle_get_native_backtrace (exc);
1029 res = mono_string_new_handle (mono_domain_get (), trace, error);
1035 * mono_error_raise_exception:
1036 * @target_error: the exception to raise
1038 * Raises the exception of @target_error.
1039 * Does nothing if @target_error has a success error code.
1040 * Aborts in case of a double fault. This happens when it can't recover from an error caused by trying
1041 * to construct the first exception object.
1042 * The error object @target_error is cleaned up.
1045 mono_error_raise_exception (MonoError *target_error)
1047 MonoException *ex = mono_error_convert_to_exception (target_error);
1049 mono_raise_exception (ex);
1053 * mono_error_set_pending_exception:
1057 * If @error is set, convert it to an exception and set the pending exception for the current icall.
1058 * Returns TRUE if @error was set, or FALSE otherwise, so that you can write:
1059 * if (mono_error_set_pending_exception (error)) {
1060 * { ... cleanup code ... }
1065 mono_error_set_pending_exception (MonoError *error)
1067 MonoException *ex = mono_error_convert_to_exception (error);
1069 mono_set_pending_exception (ex);
1077 mono_install_unhandled_exception_hook (MonoUnhandledExceptionFunc func, void *user_data)
1079 unhandled_exception_hook = func;
1080 unhandled_exception_hook_data = user_data;
1084 mono_invoke_unhandled_exception_hook (MonoObject *exc)
1086 if (unhandled_exception_hook) {
1087 unhandled_exception_hook (exc, unhandled_exception_hook_data);
1089 MonoError inner_error;
1090 MonoObject *other = NULL;
1091 MonoString *str = mono_object_try_to_string (exc, &other, &inner_error);
1094 if (str && is_ok (&inner_error)) {
1095 msg = mono_string_to_utf8_checked (str, &inner_error);
1096 if (!is_ok (&inner_error)) {
1097 msg = g_strdup_printf ("Nested exception while formatting original exception");
1098 mono_error_cleanup (&inner_error);
1101 char *original_backtrace = mono_exception_get_managed_backtrace ((MonoException*)exc);
1102 char *nested_backtrace = mono_exception_get_managed_backtrace ((MonoException*)other);
1104 msg = g_strdup_printf ("Nested exception detected.\nOriginal Exception: %s\nNested exception:%s\n",
1105 original_backtrace, nested_backtrace);
1107 g_free (original_backtrace);
1108 g_free (nested_backtrace);
1110 msg = g_strdup ("Nested exception trying to figure out what went wrong");
1112 mono_runtime_printf_err ("[ERROR] FATAL UNHANDLED EXCEPTION: %s", msg);
1114 #if defined(HOST_IOS)
1115 g_assertion_message ("Terminating runtime due to unhandled exception");
1117 exit (mono_environment_exitcode_get ());
1121 g_assert_not_reached ();