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 * \param image the Mono image where to look for the class
40 * \param name_space the namespace for the class
41 * \param name class name
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 * \param domain Domain where the return object will be created.
58 * \param image the Mono image where to look for the class
59 * \param name_space the namespace for the class
60 * \param name class name
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 * \param image the Mono image where to look for the class
96 * \param token The type token of the class
98 * Creates an exception of the type given by \p 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 * \param image the Mono image where to look for the class
168 * \param name_space the namespace for the class
169 * \param name class name
170 * \param a1 first string argument to pass
171 * \param 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 * \param image the Mono image where to look for the class
193 * \param name_space the namespace for the class
194 * \param name class name
195 * \param a1 first string argument to pass
196 * \param a2 second string argument to pass
197 * \param 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 \p 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 * \param image the Mono image where to look for the class
221 * \param name_space the namespace for the class
222 * \param name class name
223 * \param 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)
236 ex = mono_exception_from_name (image, name_space, name);
239 MonoString *msg_str = mono_string_new_checked (mono_object_get_domain ((MonoObject*)ex), msg, &error);
240 mono_error_assert_ok (&error);
241 MONO_OBJECT_SETREF (ex, message, msg_str);
248 * mono_exception_from_token_two_strings:
250 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
254 mono_exception_from_token_two_strings (MonoImage *image, guint32 token,
255 MonoString *a1, MonoString *a2)
259 ret = mono_exception_from_token_two_strings_checked (image, token, a1, a2, &error);
260 mono_error_cleanup (&error);
265 * mono_exception_from_token_two_strings_checked:
267 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
271 mono_exception_from_token_two_strings_checked (MonoImage *image, guint32 token,
272 MonoString *a1, MonoString *a2,
279 klass = mono_class_get_checked (image, token, error);
280 mono_error_assert_ok (error); /* FIXME handle the error. */
282 return create_exception_two_strings (klass, a1, a2, error);
286 * mono_get_exception_divide_by_zero:
287 * \returns a new instance of the \c System.DivideByZeroException
290 mono_get_exception_divide_by_zero ()
292 return mono_exception_from_name (mono_get_corlib (), "System",
293 "DivideByZeroException");
297 * mono_get_exception_security:
298 * \returns a new instance of the \c System.Security.SecurityException
301 mono_get_exception_security ()
303 return mono_exception_from_name (mono_get_corlib (), "System.Security",
304 "SecurityException");
308 * mono_get_exception_thread_abort:
309 * \returns a new instance of the \c System.Threading.ThreadAbortException
312 mono_get_exception_thread_abort ()
314 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
315 "ThreadAbortException");
319 * mono_get_exception_thread_interrupted:
320 * \returns a new instance of the \c 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:
331 * \returns a new instance of the \c System.ArithmeticException
334 mono_get_exception_arithmetic ()
336 return mono_exception_from_name (mono_get_corlib (), "System",
337 "ArithmeticException");
341 * mono_get_exception_overflow:
342 * \returns a new instance of the \c System.OverflowException
345 mono_get_exception_overflow ()
347 return mono_exception_from_name (mono_get_corlib (), "System",
348 "OverflowException");
352 * mono_get_exception_null_reference:
353 * \returns a new instance of the \c System.NullReferenceException
356 mono_get_exception_null_reference ()
358 return mono_exception_from_name (mono_get_corlib (), "System",
359 "NullReferenceException");
363 * mono_get_exception_execution_engine:
364 * \param msg the message to pass to the user
365 * \returns a new instance of the \c System.ExecutionEngineException
368 mono_get_exception_execution_engine (const char *msg)
370 return mono_exception_from_name_msg (mono_get_corlib (), "System", "ExecutionEngineException", msg);
374 * mono_get_exception_serialization:
375 * \param msg the message to pass to the user
376 * \returns a new instance of the \c System.Runtime.Serialization.SerializationException
379 mono_get_exception_serialization (const char *msg)
381 return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization", "SerializationException", msg);
385 * mono_get_exception_invalid_cast:
386 * \returns a new instance of the \c System.InvalidCastException
389 mono_get_exception_invalid_cast ()
391 return mono_exception_from_name (mono_get_corlib (), "System", "InvalidCastException");
395 * mono_get_exception_invalid_operation:
396 * \param msg the message to pass to the user
397 * \returns a new instance of the \c System.InvalidOperationException
400 mono_get_exception_invalid_operation (const char *msg)
402 return mono_exception_from_name_msg (mono_get_corlib (), "System",
403 "InvalidOperationException", msg);
407 * mono_get_exception_index_out_of_range:
408 * \returns a new instance of the \c System.IndexOutOfRangeException
411 mono_get_exception_index_out_of_range ()
413 return mono_exception_from_name (mono_get_corlib (), "System",
414 "IndexOutOfRangeException");
418 * mono_get_exception_array_type_mismatch:
419 * \returns a new instance of the \c System.ArrayTypeMismatchException
422 mono_get_exception_array_type_mismatch ()
424 return mono_exception_from_name (mono_get_corlib (), "System",
425 "ArrayTypeMismatchException");
429 * mono_get_exception_type_load:
430 * \param class_name the name of the class that could not be loaded
431 * \param assembly_name the assembly where the class was looked up.
432 * \returns a new instance of the \c System.TypeLoadException
435 mono_get_exception_type_load (MonoString *class_name, char *assembly_name)
438 MonoString *s = NULL;
440 s = mono_string_new_checked (mono_domain_get (), assembly_name, &error);
441 mono_error_assert_ok (&error);
443 s = mono_string_empty (mono_domain_get ());
445 MonoException *ret = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System",
446 "TypeLoadException", class_name, s, &error);
447 mono_error_assert_ok (&error);
452 * mono_get_exception_not_implemented:
453 * \param msg the message to pass to the user
454 * \returns a new instance of the \c System.NotImplementedException
457 mono_get_exception_not_implemented (const char *msg)
459 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotImplementedException", msg);
463 * mono_get_exception_not_supported:
464 * \param msg the message to pass to the user
465 * \returns a new instance of the \c System.NotSupportedException
468 mono_get_exception_not_supported (const char *msg)
470 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotSupportedException", msg);
474 * mono_get_exception_missing_method:
475 * \param class_name the class where the lookup was performed.
476 * \param member_name the name of the missing method.
477 * \returns a new instance of the \c System.MissingMethodException
480 mono_get_exception_missing_method (const char *class_name, const char *member_name)
483 MonoString *s1 = mono_string_new_checked (mono_domain_get (), class_name, &error);
484 mono_error_assert_ok (&error);
485 MonoString *s2 = mono_string_new_checked (mono_domain_get (), member_name, &error);
486 mono_error_assert_ok (&error);
488 MonoException *ret = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System",
489 "MissingMethodException", s1, s2, &error);
490 mono_error_assert_ok (&error);
495 * mono_get_exception_missing_field:
496 * \param class_name the class where the lookup was performed
497 * \param member_name the name of the missing method.
498 * \returns a new instance of the \c System.MissingFieldException
501 mono_get_exception_missing_field (const char *class_name, const char *member_name)
504 MonoString *s1 = mono_string_new_checked (mono_domain_get (), class_name, &error);
505 mono_error_assert_ok (&error);
506 MonoString *s2 = mono_string_new_checked (mono_domain_get (), member_name, &error);
507 mono_error_assert_ok (&error);
509 MonoException *ret = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System",
510 "MissingFieldException", s1, s2, &error);
511 mono_error_assert_ok (&error);
516 * mono_get_exception_argument_null:
517 * \param arg the name of the argument that is null
518 * \returns a new instance of the \c System.ArgumentNullException
521 mono_get_exception_argument_null (const char *arg)
525 ex = mono_exception_from_name (
526 mono_get_corlib (), "System", "ArgumentNullException");
530 MonoArgumentException *argex = (MonoArgumentException *)ex;
531 MonoString *arg_str = mono_string_new_checked (mono_object_get_domain ((MonoObject*)ex), arg, &error);
532 mono_error_assert_ok (&error);
533 MONO_OBJECT_SETREF (argex, param_name, arg_str);
540 * mono_get_exception_argument:
541 * \param arg the name of the invalid argument.
542 * \returns a new instance of the \c System.ArgumentException
545 mono_get_exception_argument (const char *arg, const char *msg)
549 ex = mono_exception_from_name_msg (
550 mono_get_corlib (), "System", "ArgumentException", msg);
554 MonoArgumentException *argex = (MonoArgumentException *)ex;
555 MonoString *arg_str = mono_string_new_checked (mono_object_get_domain ((MonoObject*)ex), arg, &error);
556 mono_error_assert_ok (&error);
557 MONO_OBJECT_SETREF (argex, param_name, arg_str);
564 * mono_get_exception_argument_out_of_range:
565 * \param arg the name of the out of range argument.
566 * \returns a new instance of the \c System.ArgumentOutOfRangeException
569 mono_get_exception_argument_out_of_range (const char *arg)
573 ex = mono_exception_from_name (
574 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
578 MonoArgumentException *argex = (MonoArgumentException *)ex;
579 MonoString *arg_str = mono_string_new_checked (mono_object_get_domain ((MonoObject*)ex), arg, &error);
580 mono_error_assert_ok (&error);
581 MONO_OBJECT_SETREF (argex, param_name, arg_str);
588 * mono_get_exception_thread_state:
589 * \param msg the message to present to the user
590 * \returns a new instance of the \c 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 * \param msg the message to present to the user
602 * \returns a new instance of the \c System.IO.IOException
605 mono_get_exception_io (const char *msg)
607 return mono_exception_from_name_msg (
608 mono_get_corlib (), "System.IO", "IOException", msg);
612 * mono_get_exception_file_not_found:
613 * \param fname the name of the file not found.
614 * \returns a new instance of the \c System.IO.FileNotFoundException
617 mono_get_exception_file_not_found (MonoString *fname)
620 MonoException *ret = mono_exception_from_name_two_strings_checked (
621 mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname, &error);
622 mono_error_assert_ok (&error);
627 * mono_get_exception_file_not_found2:
628 * \param msg an informative message for the user.
629 * \param fname the name of the file not found.
630 * \returns a new instance of the \c System.IO.FileNotFoundException
633 mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
636 MonoString *s = NULL;
638 s = mono_string_new_checked (mono_domain_get (), msg, &error);
639 mono_error_assert_ok (&error);
642 MonoException *ret = mono_exception_from_name_two_strings_checked (
643 mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname, &error);
644 mono_error_assert_ok (&error);
649 * mono_get_exception_type_initialization:
650 * \param type_name the name of the type that failed to initialize.
651 * \param inner the inner exception.
652 * \returns a new instance of the \c 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)
678 klass = mono_class_load_from_name (mono_get_corlib (), "System", "TypeInitializationException");
680 mono_class_init (klass);
683 while ((method = mono_class_get_methods (klass, &iter))) {
684 if (!strcmp (".ctor", mono_method_get_name (method))) {
685 MonoMethodSignature *sig = mono_method_signature (method);
687 if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_STRING && mono_class_from_mono_type (sig->params [1]) == mono_defaults.exception_class)
694 MonoString *type_name_str = mono_string_new_checked (mono_domain_get (), type_name, error);
695 mono_error_assert_ok (error);
696 args [0] = type_name_str;
699 exc = mono_object_new_checked (mono_domain_get (), klass, error);
700 mono_error_assert_ok (error);
702 mono_runtime_invoke_checked (method, exc, args, error);
703 return_val_if_nok (error, NULL);
705 return (MonoException *) exc;
709 * mono_get_exception_synchronization_lock:
710 * \param inner the inner exception.
711 * \returns a new instance of the \c System.SynchronizationLockException
714 mono_get_exception_synchronization_lock (const char *msg)
716 return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg);
720 * mono_get_exception_cannot_unload_appdomain:
721 * \param inner the inner exception.
722 * \returns a new instance of the \c System.CannotUnloadAppDomainException
725 mono_get_exception_cannot_unload_appdomain (const char *msg)
727 return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg);
731 * mono_get_exception_appdomain_unloaded
732 * \returns a new instance of the \c System.AppDomainUnloadedException
735 mono_get_exception_appdomain_unloaded (void)
737 return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
741 * mono_get_exception_bad_image_format:
742 * \param msg an informative message for the user.
743 * \returns a new instance of the \c 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 * \param msg an informative message for the user.
754 * \param fname The full name of the file with the invalid image.
755 * \returns a new instance of the \c System.BadImageFormatException
758 mono_get_exception_bad_image_format2 (const char *msg, MonoString *fname)
761 MonoString *s = NULL;
764 s = mono_string_new_checked (mono_domain_get (), msg, &error);
765 mono_error_assert_ok (&error);
768 MonoException *ret = mono_exception_from_name_two_strings_checked (
769 mono_get_corlib (), "System", "BadImageFormatException", s, fname, &error);
770 mono_error_assert_ok (&error);
775 * mono_get_exception_stack_overflow:
776 * \returns a new instance of the \c System.StackOverflowException
779 mono_get_exception_stack_overflow (void)
781 return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");
785 * mono_get_exception_out_of_memory:
786 * \returns a new instance of the \c System.OutOfMemoryException
789 mono_get_exception_out_of_memory (void)
791 return mono_exception_from_name (mono_get_corlib (), "System", "OutOfMemoryException");
795 * mono_get_exception_field_access:
796 * \returns a new instance of the \c System.FieldAccessException
799 mono_get_exception_field_access (void)
801 return mono_exception_from_name (mono_get_corlib (), "System", "FieldAccessException");
805 * mono_get_exception_field_access2:
806 * \param msg an informative message for the user.
807 * \returns a new instance of the \c 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:
817 * \returns a new instance of the \c System.MethodAccessException
820 mono_get_exception_method_access (void)
822 return mono_exception_from_name (mono_get_corlib (), "System", "MethodAccessException");
826 * mono_get_exception_method_access2:
827 * \param msg an informative message for the user.
828 * \returns a new instance of the \c System.MethodAccessException
831 mono_get_exception_method_access_msg (const char *msg)
833 return mono_exception_from_name_msg (mono_get_corlib (), "System", "MethodAccessException", msg);
837 * mono_get_exception_reflection_type_load:
838 * \param types an array of types that were defined in the moduled loaded.
839 * \param exceptions an array of exceptions that were thrown during the type loading.
840 * \returns a new instance of the \c System.Reflection.ReflectionTypeLoadException
843 mono_get_exception_reflection_type_load (MonoArray *types_raw, MonoArray *exceptions_raw)
845 HANDLE_FUNCTION_ENTER ();
847 MONO_HANDLE_DCL (MonoArray, types);
848 MONO_HANDLE_DCL (MonoArray, exceptions);
849 MonoExceptionHandle ret = mono_get_exception_reflection_type_load_checked (types, exceptions, &error);
850 if (is_ok (&error)) {
851 mono_error_cleanup (&error);
852 ret = MONO_HANDLE_CAST (MonoException, NULL_HANDLE);
857 HANDLE_FUNCTION_RETURN_OBJ (ret);
862 mono_get_exception_reflection_type_load_checked (MonoArrayHandle types, MonoArrayHandle exceptions, MonoError *error)
870 klass = mono_class_load_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
872 mono_class_init (klass);
874 /* Find the Type[], Exception[] ctor */
876 while ((method = mono_class_get_methods (klass, &iter))) {
877 if (!strcmp (".ctor", mono_method_get_name (method))) {
878 MonoMethodSignature *sig = mono_method_signature (method);
880 if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_SZARRAY && sig->params [1]->type == MONO_TYPE_SZARRAY)
887 MonoExceptionHandle exc = MONO_HANDLE_NEW (MonoException, mono_object_new_checked (mono_domain_get (), klass, error));
888 mono_error_assert_ok (error);
891 args [0] = MONO_HANDLE_RAW (types);
892 args [1] = MONO_HANDLE_RAW (exceptions);
894 mono_runtime_invoke_checked (method, MONO_HANDLE_RAW (exc), args, error);
895 return_val_if_nok (error, MONO_HANDLE_CAST (MonoException, NULL_HANDLE));
901 * mono_get_exception_runtime_wrapped:
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 * \param target_error the exception to raise
1038 * Raises the exception of \p target_error.
1039 * Does nothing if \p 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 \p 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:
1054 * \param error The error
1055 * If \p error is set, convert it to an exception and set the pending exception for the current icall.
1056 * \returns TRUE if \p error was set, or FALSE otherwise, so that you can write:
1057 * if (mono_error_set_pending_exception (error)) {
1058 * { ... cleanup code ... }
1063 mono_error_set_pending_exception (MonoError *error)
1065 MonoException *ex = mono_error_convert_to_exception (error);
1067 mono_set_pending_exception (ex);
1075 mono_install_unhandled_exception_hook (MonoUnhandledExceptionFunc func, void *user_data)
1077 unhandled_exception_hook = func;
1078 unhandled_exception_hook_data = user_data;
1082 mono_invoke_unhandled_exception_hook (MonoObject *exc)
1084 if (unhandled_exception_hook) {
1085 unhandled_exception_hook (exc, unhandled_exception_hook_data);
1087 MonoError inner_error;
1088 MonoObject *other = NULL;
1089 MonoString *str = mono_object_try_to_string (exc, &other, &inner_error);
1092 if (str && is_ok (&inner_error)) {
1093 msg = mono_string_to_utf8_checked (str, &inner_error);
1094 if (!is_ok (&inner_error)) {
1095 msg = g_strdup_printf ("Nested exception while formatting original exception");
1096 mono_error_cleanup (&inner_error);
1099 char *original_backtrace = mono_exception_get_managed_backtrace ((MonoException*)exc);
1100 char *nested_backtrace = mono_exception_get_managed_backtrace ((MonoException*)other);
1102 msg = g_strdup_printf ("Nested exception detected.\nOriginal Exception: %s\nNested exception:%s\n",
1103 original_backtrace, nested_backtrace);
1105 g_free (original_backtrace);
1106 g_free (nested_backtrace);
1108 msg = g_strdup ("Nested exception trying to figure out what went wrong");
1110 mono_runtime_printf_err ("[ERROR] FATAL UNHANDLED EXCEPTION: %s", msg);
1112 #if defined(HOST_IOS)
1113 g_assertion_message ("Terminating runtime due to unhandled exception");
1115 exit (mono_environment_exitcode_get ());
1119 g_assert_not_reached ();