Merge pull request #3100 from lambdageek/dev/monoerror-exns
[mono-project.git] / mono / metadata / exception.c
blob2aff26dfd53788fe5080ef51e97cbaf0b9975045
1 /*
2 * exception.c: Exception handling
4 * Authors:
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
7 * Dick Porter (dick@ximian.com)
8 * Miguel de Icaza (miguel@ximian.com)
10 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
11 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
12 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
15 #include <glib.h>
16 #include <config.h>
17 #include <mono/metadata/exception.h>
18 #include <mono/metadata/exception-internals.h>
20 #include <mono/metadata/object-internals.h>
21 #include <mono/metadata/metadata-internals.h>
22 #include <mono/metadata/appdomain.h>
23 #include <mono/metadata/mono-debug.h>
24 #include <mono/utils/mono-error-internals.h>
25 #include <string.h>
27 #ifdef HAVE_EXECINFO_H
28 #include <execinfo.h>
29 #endif
31 /**
32 * mono_exception_from_name:
33 * @image: the Mono image where to look for the class
34 * @name_space: the namespace for the class
35 * @name: class name
37 * Creates an exception of the given namespace/name class in the
38 * current domain.
40 * Returns: the initialized exception instance.
42 MonoException *
43 mono_exception_from_name (MonoImage *image, const char *name_space,
44 const char *name)
46 return mono_exception_from_name_domain (mono_domain_get (), image, name_space, name);
49 /**
50 * mono_exception_from_name_domain:
51 * @domain: Domain where the return object will be created.
52 * @image: the Mono image where to look for the class
53 * @name_space: the namespace for the class
54 * @name: class name
56 * Creates an exception object of the given namespace/name class on
57 * the given domain.
59 * Returns: the initialized exception instance.
61 MonoException *
62 mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image,
63 const char* name_space, const char *name)
65 MonoError error;
66 MonoClass *klass;
67 MonoObject *o;
68 MonoDomain *caller_domain = mono_domain_get ();
70 klass = mono_class_load_from_name (image, name_space, name);
72 o = mono_object_new_checked (domain, klass, &error);
73 mono_error_assert_ok (&error);
75 if (domain != caller_domain)
76 mono_domain_set_internal (domain);
77 mono_runtime_object_init_checked (o, &error);
78 mono_error_assert_ok (&error);
80 if (domain != caller_domain)
81 mono_domain_set_internal (caller_domain);
83 return (MonoException *)o;
87 /**
88 * mono_exception_from_token:
89 * @image: the Mono image where to look for the class
90 * @token: The type token of the class
92 * Creates an exception of the type given by @token.
94 * Returns: the initialized exception instance.
96 MonoException *
97 mono_exception_from_token (MonoImage *image, guint32 token)
99 MonoError error;
100 MonoClass *klass;
101 MonoObject *o;
103 klass = mono_class_get_checked (image, token, &error);
104 mono_error_assert_ok (&error);
106 o = mono_object_new_checked (mono_domain_get (), klass, &error);
107 mono_error_assert_ok (&error);
109 mono_runtime_object_init_checked (o, &error);
110 mono_error_assert_ok (&error);
112 return (MonoException *)o;
115 static MonoException *
116 create_exception_two_strings (MonoClass *klass, MonoString *a1, MonoString *a2, MonoError *error)
118 MonoDomain *domain = mono_domain_get ();
119 MonoMethod *method = NULL;
120 MonoObject *o;
121 int count = 1;
122 gpointer args [2];
123 gpointer iter;
124 MonoMethod *m;
126 if (a2 != NULL)
127 count++;
129 o = mono_object_new_checked (domain, klass, error);
130 mono_error_assert_ok (error);
132 iter = NULL;
133 while ((m = mono_class_get_methods (klass, &iter))) {
134 MonoMethodSignature *sig;
136 if (strcmp (".ctor", mono_method_get_name (m)))
137 continue;
138 sig = mono_method_signature (m);
139 if (sig->param_count != count)
140 continue;
142 if (sig->params [0]->type != MONO_TYPE_STRING)
143 continue;
144 if (count == 2 && sig->params [1]->type != MONO_TYPE_STRING)
145 continue;
146 method = m;
147 break;
150 args [0] = a1;
151 args [1] = a2;
153 mono_runtime_invoke_checked (method, o, args, error);
154 return_val_if_nok (error, NULL);
156 return (MonoException *) o;
160 * mono_exception_from_name_two_strings:
161 * @image: the Mono image where to look for the class
162 * @name_space: the namespace for the class
163 * @name: class name
164 * @a1: first string argument to pass
165 * @a2: second string argument to pass
167 * Creates an exception from a constructor that takes two string
168 * arguments.
170 * Returns: the initialized exception instance.
172 MonoException *
173 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
174 const char *name, MonoString *a1, MonoString *a2)
176 MonoError error;
177 MonoException *ret;
179 ret = mono_exception_from_name_two_strings_checked (image, name_space, name, a1, a2, &error);
180 mono_error_cleanup (&error);
181 return ret;
185 * mono_exception_from_name_two_strings_checked:
186 * @image: the Mono image where to look for the class
187 * @name_space: the namespace for the class
188 * @name: class name
189 * @a1: first string argument to pass
190 * @a2: second string argument to pass
191 * @error: set on error
193 * Creates an exception from a constructor that takes two string
194 * arguments.
196 * Returns: the initialized exception instance. On failure returns
197 * NULL and sets @error.
199 MonoException *
200 mono_exception_from_name_two_strings_checked (MonoImage *image, const char *name_space,
201 const char *name, MonoString *a1, MonoString *a2,
202 MonoError *error)
204 MonoClass *klass;
206 mono_error_init (error);
207 klass = mono_class_load_from_name (image, name_space, name);
209 return create_exception_two_strings (klass, a1, a2, error);
213 * mono_exception_from_name_msg:
214 * @image: the Mono image where to look for the class
215 * @name_space: the namespace for the class
216 * @name: class name
217 * @msg: the message to embed inside the exception
219 * Creates an exception and initializes its message field.
221 * Returns: the initialized exception instance.
223 MonoException *
224 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
225 const char *name, const char *msg)
227 MonoException *ex;
229 ex = mono_exception_from_name (image, name_space, name);
231 if (msg)
232 MONO_OBJECT_SETREF (ex, message, mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg));
234 return ex;
238 * mono_exception_from_token_two_strings:
240 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
241 * IMAGE and TOKEN.
243 MonoException *
244 mono_exception_from_token_two_strings (MonoImage *image, guint32 token,
245 MonoString *a1, MonoString *a2)
247 MonoError error;
248 MonoException *ret;
249 ret = mono_exception_from_token_two_strings_checked (image, token, a1, a2, &error);
250 mono_error_cleanup (&error);
251 return ret;
255 * mono_exception_from_token_two_strings_checked:
257 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
258 * IMAGE and TOKEN.
260 MonoException *
261 mono_exception_from_token_two_strings_checked (MonoImage *image, guint32 token,
262 MonoString *a1, MonoString *a2,
263 MonoError *error)
265 MonoClass *klass;
267 mono_error_init (error);
269 klass = mono_class_get_checked (image, token, error);
270 mono_error_assert_ok (error); /* FIXME handle the error. */
272 return create_exception_two_strings (klass, a1, a2, error);
276 * mono_get_exception_divide_by_zero:
278 * Returns: a new instance of the `System.DivideByZeroException`
280 MonoException *
281 mono_get_exception_divide_by_zero ()
283 return mono_exception_from_name (mono_get_corlib (), "System",
284 "DivideByZeroException");
288 * mono_get_exception_security:
290 * Returns: a new instance of the `System.Security.SecurityException`
292 MonoException *
293 mono_get_exception_security ()
295 return mono_exception_from_name (mono_get_corlib (), "System.Security",
296 "SecurityException");
300 * mono_get_exception_thread_abort:
302 * Returns: a new instance of the `System.Threading.ThreadAbortException`
304 MonoException *
305 mono_get_exception_thread_abort ()
307 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
308 "ThreadAbortException");
312 * mono_get_exception_thread_interrupted:
314 * Returns: a new instance of the `System.Threading.ThreadInterruptedException`
316 MonoException *
317 mono_get_exception_thread_interrupted ()
319 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
320 "ThreadInterruptedException");
324 * mono_get_exception_arithmetic:
326 * Returns: a new instance of the `System.ArithmeticException`
328 MonoException *
329 mono_get_exception_arithmetic ()
331 return mono_exception_from_name (mono_get_corlib (), "System",
332 "ArithmeticException");
336 * mono_get_exception_overflow:
338 * Returns: a new instance of the `System.OverflowException`
340 MonoException *
341 mono_get_exception_overflow ()
343 return mono_exception_from_name (mono_get_corlib (), "System",
344 "OverflowException");
348 * mono_get_exception_null_reference:
350 * Returns: a new instance of the `System.NullReferenceException`
352 MonoException *
353 mono_get_exception_null_reference ()
355 return mono_exception_from_name (mono_get_corlib (), "System",
356 "NullReferenceException");
360 * mono_get_exception_execution_engine:
361 * @msg: the message to pass to the user
363 * Returns: a new instance of the `System.ExecutionEngineException`
365 MonoException *
366 mono_get_exception_execution_engine (const char *msg)
368 return mono_exception_from_name_msg (mono_get_corlib (), "System", "ExecutionEngineException", msg);
372 * mono_get_exception_serialization:
373 * @msg: the message to pass to the user
375 * Returns: a new instance of the `System.Runtime.Serialization.SerializationException`
377 MonoException *
378 mono_get_exception_serialization (const char *msg)
380 return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization", "SerializationException", msg);
384 * mono_get_exception_invalid_cast:
386 * Returns: a new instance of the `System.InvalidCastException`
388 MonoException *
389 mono_get_exception_invalid_cast ()
391 return mono_exception_from_name (mono_get_corlib (), "System", "InvalidCastException");
395 * mono_get_exception_invalid_operation:
396 * @msg: the message to pass to the user
398 * Returns: a new instance of the `System.InvalidOperationException`
400 MonoException *
401 mono_get_exception_invalid_operation (const char *msg)
403 return mono_exception_from_name_msg (mono_get_corlib (), "System",
404 "InvalidOperationException", msg);
408 * mono_get_exception_index_out_of_range:
410 * Returns: a new instance of the `System.IndexOutOfRangeException`
412 MonoException *
413 mono_get_exception_index_out_of_range ()
415 return mono_exception_from_name (mono_get_corlib (), "System",
416 "IndexOutOfRangeException");
420 * mono_get_exception_array_type_mismatch:
422 * Returns: a new instance of the `System.ArrayTypeMismatchException`
424 MonoException *
425 mono_get_exception_array_type_mismatch ()
427 return mono_exception_from_name (mono_get_corlib (), "System",
428 "ArrayTypeMismatchException");
432 * mono_get_exception_type_load:
433 * @class_name: the name of the class that could not be loaded
434 * @assembly_name: the assembly where the class was looked up.
436 * Returns: a new instance of the `System.TypeLoadException`
438 MonoException *
439 mono_get_exception_type_load (MonoString *class_name, char *assembly_name)
441 MonoString *s = assembly_name ? mono_string_new (mono_domain_get (), assembly_name) : mono_string_new (mono_domain_get (), "");
443 MonoError error;
444 MonoException *ret = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System",
445 "TypeLoadException", class_name, s, &error);
446 mono_error_assert_ok (&error);
447 return ret;
451 * mono_get_exception_not_implemented:
452 * @msg: the message to pass to the user
454 * Returns: a new instance of the `System.NotImplementedException`
456 MonoException *
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 * @msg: the message to pass to the user
466 * Returns: a new instance of the `System.NotSupportedException`
468 MonoException *
469 mono_get_exception_not_supported (const char *msg)
471 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotSupportedException", msg);
475 * mono_get_exception_missing_method:
476 * @class_name: the class where the lookup was performed.
477 * @member_name: the name of the missing method.
479 * Returns: a new instance of the `System.MissingMethodException`
481 MonoException *
482 mono_get_exception_missing_method (const char *class_name, const char *member_name)
484 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
485 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
487 MonoError 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);
491 return ret;
495 * mono_get_exception_missing_field:
496 * @class_name: the class where the lookup was performed
497 * @member_name: the name of the missing method.
499 * Returns: a new instance of the `System.MissingFieldException`
501 MonoException *
502 mono_get_exception_missing_field (const char *class_name, const char *member_name)
504 MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
505 MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
507 MonoError error;
508 MonoException *ret = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System",
509 "MissingFieldException", s1, s2, &error);
510 mono_error_assert_ok (&error);
511 return ret;
515 * mono_get_exception_argument_null:
516 * @arg: the name of the argument that is null
518 * Returns: a new instance of the `System.ArgumentNullException`
520 MonoException*
521 mono_get_exception_argument_null (const char *arg)
523 MonoException *ex;
525 ex = mono_exception_from_name (
526 mono_get_corlib (), "System", "ArgumentNullException");
528 if (arg) {
529 MonoArgumentException *argex = (MonoArgumentException *)ex;
530 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
533 return ex;
537 * mono_get_exception_argument:
538 * @arg: the name of the invalid argument.
540 * Returns: a new instance of the `System.ArgumentException`
542 MonoException *
543 mono_get_exception_argument (const char *arg, const char *msg)
545 MonoException *ex;
547 ex = mono_exception_from_name_msg (
548 mono_get_corlib (), "System", "ArgumentException", msg);
550 if (arg) {
551 MonoArgumentException *argex = (MonoArgumentException *)ex;
552 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
555 return ex;
559 * mono_get_exception_argument_out_of_range:
560 * @arg: the name of the out of range argument.
562 * Returns: a new instance of the `System.ArgumentOutOfRangeException`
564 MonoException *
565 mono_get_exception_argument_out_of_range (const char *arg)
567 MonoException *ex;
569 ex = mono_exception_from_name (
570 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
572 if (arg) {
573 MonoArgumentException *argex = (MonoArgumentException *)ex;
574 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
577 return ex;
581 * mono_get_exception_thread_state:
582 * @msg: the message to present to the user
584 * Returns: a new instance of the `System.Threading.ThreadStateException`
586 MonoException *
587 mono_get_exception_thread_state (const char *msg)
589 return mono_exception_from_name_msg (
590 mono_get_corlib (), "System.Threading", "ThreadStateException", msg);
594 * mono_get_exception_io:
595 * @msg: the message to present to the user
597 * Returns: a new instance of the `System.IO.IOException`
599 MonoException *
600 mono_get_exception_io (const char *msg)
602 return mono_exception_from_name_msg (
603 mono_get_corlib (), "System.IO", "IOException", msg);
607 * mono_get_exception_file_not_found:
608 * @fname: the name of the file not found.
610 * Returns: a new instance of the `System.IO.FileNotFoundException`
612 MonoException *
613 mono_get_exception_file_not_found (MonoString *fname)
615 MonoError error;
616 MonoException *ret = mono_exception_from_name_two_strings_checked (
617 mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname, &error);
618 mono_error_assert_ok (&error);
619 return ret;
623 * mono_get_exception_file_not_found2:
624 * @msg: an informative message for the user.
625 * @fname: the name of the file not found.
627 * Returns: a new instance of the `System.IO.FileNotFoundException`
629 MonoException *
630 mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
632 MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
634 MonoError error;
635 MonoException *ret = mono_exception_from_name_two_strings_checked (
636 mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname, &error);
637 mono_error_assert_ok (&error);
638 return ret;
642 * mono_get_exception_type_initialization:
643 * @type_name: the name of the type that failed to initialize.
644 * @inner: the inner exception.
646 * Returns: a new instance of the `System.TypeInitializationException`
648 MonoException *
649 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
651 MonoError error;
652 MonoException *ret = mono_get_exception_type_initialization_checked (type_name, inner, &error);
653 if (!is_ok (&error)) {
654 mono_error_cleanup (&error);
655 return NULL;
658 return ret;
661 MonoException *
662 mono_get_exception_type_initialization_checked (const gchar *type_name, MonoException *inner, MonoError *error)
664 MonoClass *klass;
665 gpointer args [2];
666 MonoObject *exc;
667 MonoMethod *method;
668 gpointer iter;
670 klass = mono_class_load_from_name (mono_get_corlib (), "System", "TypeInitializationException");
672 mono_class_init (klass);
674 iter = NULL;
675 while ((method = mono_class_get_methods (klass, &iter))) {
676 if (!strcmp (".ctor", mono_method_get_name (method))) {
677 MonoMethodSignature *sig = mono_method_signature (method);
679 if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_STRING && mono_class_from_mono_type (sig->params [1]) == mono_defaults.exception_class)
680 break;
682 method = NULL;
684 g_assert (method);
686 args [0] = mono_string_new (mono_domain_get (), type_name);
687 args [1] = inner;
689 exc = mono_object_new_checked (mono_domain_get (), klass, error);
690 mono_error_assert_ok (error);
692 mono_runtime_invoke_checked (method, exc, args, error);
693 return_val_if_nok (error, NULL);
695 return (MonoException *) exc;
699 * mono_get_exception_synchronization_lock:
700 * @inner: the inner exception.
702 * Returns: a new instance of the `System.SynchronizationLockException`
704 MonoException *
705 mono_get_exception_synchronization_lock (const char *msg)
707 return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg);
711 * mono_get_exception_cannot_unload_appdomain:
712 * @inner: the inner exception.
714 * Returns: a new instance of the `System.CannotUnloadAppDomainException`
716 MonoException *
717 mono_get_exception_cannot_unload_appdomain (const char *msg)
719 return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg);
723 * mono_get_exception_appdomain_unloaded
725 * Returns: a new instance of the `System.AppDomainUnloadedException`
727 MonoException *
728 mono_get_exception_appdomain_unloaded (void)
730 return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
734 * mono_get_exception_bad_image_format:
735 * @msg: an informative message for the user.
737 * Returns: a new instance of the `System.BadImageFormatException`
739 MonoException *
740 mono_get_exception_bad_image_format (const char *msg)
742 return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg);
746 * mono_get_exception_bad_image_format2:
747 * @msg: an informative message for the user.
748 * @fname: The full name of the file with the invalid image.
750 * Returns: a new instance of the `System.BadImageFormatException`
752 MonoException *
753 mono_get_exception_bad_image_format2 (const char *msg, MonoString *fname)
755 MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
757 MonoError error;
758 MonoException *ret = mono_exception_from_name_two_strings_checked (
759 mono_get_corlib (), "System", "BadImageFormatException", s, fname, &error);
760 mono_error_assert_ok (&error);
761 return ret;
765 * mono_get_exception_stack_overflow:
767 * Returns: a new instance of the `System.StackOverflowException`
769 MonoException *
770 mono_get_exception_stack_overflow (void)
772 return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");
776 * mono_get_exception_out_of_memory:
778 * Returns: a new instance of the `System.OutOfMemoryException`
780 MonoException *
781 mono_get_exception_out_of_memory (void)
783 return mono_exception_from_name (mono_get_corlib (), "System", "OutOfMemoryException");
787 * mono_get_exception_field_access:
789 * Returns: a new instance of the `System.FieldAccessException`
791 MonoException *
792 mono_get_exception_field_access (void)
794 return mono_exception_from_name (mono_get_corlib (), "System", "FieldAccessException");
798 * mono_get_exception_field_access2:
799 * @msg: an informative message for the user.
801 * Returns: a new instance of the `System.FieldAccessException`
803 MonoException *
804 mono_get_exception_field_access_msg (const char *msg)
806 return mono_exception_from_name_msg (mono_get_corlib (), "System", "FieldAccessException", msg);
810 * mono_get_exception_method_access:
812 * Returns: a new instance of the `System.MethodAccessException`
814 MonoException *
815 mono_get_exception_method_access (void)
817 return mono_exception_from_name (mono_get_corlib (), "System", "MethodAccessException");
821 * mono_get_exception_method_access2:
822 * @msg: an informative message for the user.
824 * Returns: a new instance of the `System.MethodAccessException`
826 MonoException *
827 mono_get_exception_method_access_msg (const char *msg)
829 return mono_exception_from_name_msg (mono_get_corlib (), "System", "MethodAccessException", msg);
833 * mono_get_exception_reflection_type_load:
834 * @types: an array of types that were defined in the moduled loaded.
835 * @exceptions: an array of exceptions that were thrown during the type loading.
837 * Returns: a new instance of the `System.Reflection.ReflectionTypeLoadException`
839 MonoException *
840 mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions)
842 MonoError error;
843 MonoException *ret = mono_get_exception_reflection_type_load_checked (types, exceptions, &error);
844 if (is_ok (&error)) {
845 mono_error_cleanup (&error);
846 return NULL;
849 return ret;
852 MonoException *
853 mono_get_exception_reflection_type_load_checked (MonoArray *types, MonoArray *exceptions, MonoError *error)
855 MonoClass *klass;
856 gpointer args [2];
857 MonoObject *exc;
858 MonoMethod *method;
859 gpointer iter;
861 klass = mono_class_load_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
863 mono_class_init (klass);
865 /* Find the Type[], Exception[] ctor */
866 iter = NULL;
867 while ((method = mono_class_get_methods (klass, &iter))) {
868 if (!strcmp (".ctor", mono_method_get_name (method))) {
869 MonoMethodSignature *sig = mono_method_signature (method);
871 if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_SZARRAY && sig->params [1]->type == MONO_TYPE_SZARRAY)
872 break;
874 method = NULL;
876 g_assert (method);
878 args [0] = types;
879 args [1] = exceptions;
881 exc = mono_object_new_checked (mono_domain_get (), klass, error);
882 mono_error_assert_ok (error);
884 mono_runtime_invoke_checked (method, exc, args, error);
885 return_val_if_nok (error, NULL);
887 return (MonoException *) exc;
890 MonoException *
891 mono_get_exception_runtime_wrapped (MonoObject *wrapped_exception)
893 MonoError error;
894 MonoException *ret = mono_get_exception_runtime_wrapped_checked (wrapped_exception, &error);
895 if (!is_ok (&error)) {
896 mono_error_cleanup (&error);
897 return NULL;
900 return ret;
903 MonoException *
904 mono_get_exception_runtime_wrapped_checked (MonoObject *wrapped_exception, MonoError *error)
906 MonoClass *klass;
907 MonoObject *o;
908 MonoMethod *method;
909 MonoDomain *domain = mono_domain_get ();
910 gpointer params [16];
912 klass = mono_class_load_from_name (mono_get_corlib (), "System.Runtime.CompilerServices", "RuntimeWrappedException");
914 o = mono_object_new_checked (domain, klass, error);
915 mono_error_assert_ok (error);
916 g_assert (o != NULL);
918 method = mono_class_get_method_from_name (klass, ".ctor", 1);
919 g_assert (method);
921 params [0] = wrapped_exception;
923 mono_runtime_invoke_checked (method, o, params, error);
924 return_val_if_nok (error, NULL);
926 return (MonoException *)o;
929 static gboolean
930 append_frame_and_continue (MonoMethod *method, gpointer ip, size_t native_offset, gboolean managed, gpointer user_data)
932 MonoDomain *domain = mono_domain_get ();
933 GString *text = (GString*)user_data;
935 if (method) {
936 char *msg = mono_debug_print_stack_frame (method, native_offset, domain);
937 g_string_append_printf (text, "%s\n", msg);
938 g_free (msg);
939 } else {
940 g_string_append_printf (text, "<unknown native frame 0x%x>\n", ip);
943 return FALSE;
946 char *
947 mono_exception_get_managed_backtrace (MonoException *exc)
949 GString *text;
951 text = g_string_new_len (NULL, 20);
953 if (!mono_get_eh_callbacks ()->mono_exception_walk_trace (exc, append_frame_and_continue, text))
954 g_string_append (text, "managed backtrace not available\n");
956 return g_string_free (text, FALSE);
959 char *
960 mono_exception_get_native_backtrace (MonoException *exc)
962 #ifdef HAVE_BACKTRACE_SYMBOLS
963 MonoDomain *domain;
964 MonoArray *arr = exc->native_trace_ips;
965 int i, len;
966 GString *text;
967 char **messages;
969 if (!arr)
970 return g_strdup ("");
971 domain = mono_domain_get ();
972 len = mono_array_length (arr);
973 text = g_string_new_len (NULL, len * 20);
974 messages = backtrace_symbols (mono_array_addr (arr, gpointer, 0), len);
977 for (i = 0; i < len; ++i) {
978 gpointer ip = mono_array_get (arr, gpointer, i);
979 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), (char *)ip);
980 if (ji) {
981 char *msg = mono_debug_print_stack_frame (mono_jit_info_get_method (ji), (char*)ip - (char*)ji->code_start, domain);
982 g_string_append_printf (text, "%s\n", msg);
983 g_free (msg);
984 } else {
985 g_string_append_printf (text, "%s\n", messages [i]);
989 free (messages);
990 return g_string_free (text, FALSE);
991 #else
992 return g_strdup ("");
993 #endif
996 MonoString *
997 ves_icall_Mono_Runtime_GetNativeStackTrace (MonoException *exc)
999 char *trace;
1000 MonoString *res;
1001 if (!exc) {
1002 mono_set_pending_exception (mono_get_exception_argument_null ("exception"));
1003 return NULL;
1006 trace = mono_exception_get_native_backtrace (exc);
1007 res = mono_string_new (mono_domain_get (), trace);
1008 g_free (trace);
1009 return res;
1013 * mono_error_raise_exception:
1014 * @target_error: the exception to raise
1016 * Raises the exception of @target_error.
1017 * Does nothing if @target_error has a success error code.
1018 * Aborts in case of a double fault. This happens when it can't recover from an error caused by trying
1019 * to construct the first exception object.
1020 * The error object @target_error is cleaned up.
1022 void
1023 mono_error_raise_exception (MonoError *target_error)
1025 MonoException *ex = mono_error_convert_to_exception (target_error);
1026 if (ex)
1027 mono_raise_exception (ex);
1031 * mono_error_set_pending_exception:
1032 * @error: The error
1035 * If @error is set, convert it to an exception and set the pending exception for the current icall.
1036 * Returns TRUE if @error was set, or FALSE otherwise, so that you can write:
1037 * if (mono_error_set_pending_exception (error)) {
1038 * { ... cleanup code ... }
1039 * return;
1042 gboolean
1043 mono_error_set_pending_exception (MonoError *error)
1045 MonoException *ex = mono_error_convert_to_exception (error);
1046 if (ex) {
1047 mono_set_pending_exception (ex);
1048 return TRUE;
1049 } else {
1050 return FALSE;