First set of licensing changes
[mono.git] / mono / metadata / exception.c
1 /*
2  * exception.c: Exception handling
3  *
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)
9  *
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.
13  */
14
15 #include <glib.h>
16 #include <mono/metadata/exception.h>
17 #include <mono/metadata/exception-internals.h>
18
19 #include <mono/metadata/object-internals.h>
20 #include <mono/metadata/metadata-internals.h>
21 #include <mono/metadata/appdomain.h>
22 #include <mono/metadata/mono-debug.h>
23 #include <mono/utils/mono-error-internals.h>
24 #include <string.h>
25
26 #ifdef HAVE_EXECINFO_H
27 #include <execinfo.h>
28 #endif
29
30 /**
31  * mono_exception_from_name:
32  * @image: the Mono image where to look for the class
33  * @name_space: the namespace for the class
34  * @name: class name
35  *
36  * Creates an exception of the given namespace/name class in the
37  * current domain.
38  *
39  * Returns: the initialized exception instance.
40  */
41 MonoException *
42 mono_exception_from_name (MonoImage *image, const char *name_space,
43                           const char *name)
44 {
45         return mono_exception_from_name_domain (mono_domain_get (), image, name_space, name);
46 }
47
48 /**
49  * mono_exception_from_name_domain:
50  * @domain: Domain where the return object will be created.
51  * @image: the Mono image where to look for the class
52  * @name_space: the namespace for the class
53  * @name: class name
54  *
55  * Creates an exception object of the given namespace/name class on
56  * the given domain.
57  *
58  * Returns: the initialized exception instance.
59  */
60 MonoException *
61 mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image, 
62                                  const char* name_space, const char *name)
63 {
64         MonoError error;
65         MonoClass *klass;
66         MonoObject *o;
67         MonoDomain *caller_domain = mono_domain_get ();
68
69         klass = mono_class_load_from_name (image, name_space, name);
70
71         o = mono_object_new_checked (domain, klass, &error);
72         mono_error_assert_ok (&error);
73
74         if (domain != caller_domain)
75                 mono_domain_set_internal (domain);
76         mono_runtime_object_init_checked (o, &error);
77         mono_error_assert_ok (&error);
78
79         if (domain != caller_domain)
80                 mono_domain_set_internal (caller_domain);
81
82         return (MonoException *)o;
83 }
84
85
86 /**
87  * mono_exception_from_token:
88  * @image: the Mono image where to look for the class
89  * @token: The type token of the class
90  *
91  * Creates an exception of the type given by @token.
92  *
93  * Returns: the initialized exception instance.
94  */
95 MonoException *
96 mono_exception_from_token (MonoImage *image, guint32 token)
97 {
98         MonoError error;
99         MonoClass *klass;
100         MonoObject *o;
101
102         klass = mono_class_get_checked (image, token, &error);
103         mono_error_assert_ok (&error);
104
105         o = mono_object_new_checked (mono_domain_get (), klass, &error);
106         mono_error_assert_ok (&error);
107         
108         mono_runtime_object_init_checked (o, &error);
109         mono_error_assert_ok (&error);
110
111         return (MonoException *)o;
112 }
113
114 static MonoException *
115 create_exception_two_strings (MonoClass *klass, MonoString *a1, MonoString *a2, MonoError *error)
116 {
117         MonoDomain *domain = mono_domain_get ();
118         MonoMethod *method = NULL;
119         MonoObject *o;
120         int count = 1;
121         gpointer args [2];
122         gpointer iter;
123         MonoMethod *m;
124
125         if (a2 != NULL)
126                 count++;
127         
128         o = mono_object_new_checked (domain, klass, error);
129         mono_error_assert_ok (error);
130
131         iter = NULL;
132         while ((m = mono_class_get_methods (klass, &iter))) {
133                 MonoMethodSignature *sig;
134                 
135                 if (strcmp (".ctor", mono_method_get_name (m)))
136                         continue;
137                 sig = mono_method_signature (m);
138                 if (sig->param_count != count)
139                         continue;
140
141                 if (sig->params [0]->type != MONO_TYPE_STRING)
142                         continue;
143                 if (count == 2 && sig->params [1]->type != MONO_TYPE_STRING)
144                         continue;
145                 method = m;
146                 break;
147         }
148
149         args [0] = a1;
150         args [1] = a2;
151
152         mono_runtime_invoke_checked (method, o, args, error);
153         return_val_if_nok (error, NULL);
154
155         return (MonoException *) o;
156 }
157
158 /**
159  * mono_exception_from_name_two_strings:
160  * @image: the Mono image where to look for the class
161  * @name_space: the namespace for the class
162  * @name: class name
163  * @a1: first string argument to pass
164  * @a2: second string argument to pass
165  *
166  * Creates an exception from a constructor that takes two string
167  * arguments.
168  *
169  * Returns: the initialized exception instance.
170  */
171 MonoException *
172 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
173                                       const char *name, MonoString *a1, MonoString *a2)
174 {
175         MonoError error;
176         MonoClass *klass;
177         MonoException *ret;
178
179         klass = mono_class_load_from_name (image, name_space, name);
180
181         ret = create_exception_two_strings (klass, a1, a2, &error);
182         mono_error_raise_exception (&error); /* FIXME don't raise here */
183
184         return ret;
185 }
186
187 /**
188  * mono_exception_from_name_msg:
189  * @image: the Mono image where to look for the class
190  * @name_space: the namespace for the class
191  * @name: class name
192  * @msg: the message to embed inside the exception
193  *
194  * Creates an exception and initializes its message field.
195  *
196  * Returns: the initialized exception instance.
197  */
198 MonoException *
199 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
200                               const char *name, const char *msg)
201 {
202         MonoException *ex;
203
204         ex = mono_exception_from_name (image, name_space, name);
205
206         if (msg)
207                 MONO_OBJECT_SETREF (ex, message, mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg));
208
209         return ex;
210 }
211
212 /**
213  * mono_exception_from_token_two_strings:
214  *
215  *   Same as mono_exception_from_name_two_strings, but lookup the exception class using
216  * IMAGE and TOKEN.
217  */
218 MonoException *
219 mono_exception_from_token_two_strings (MonoImage *image, guint32 token,
220                                                                            MonoString *a1, MonoString *a2)
221 {
222         MonoError error;
223         MonoClass *klass;
224         MonoException *ret;
225
226         klass = mono_class_get_checked (image, token, &error);
227         mono_error_assert_ok (&error); /* FIXME handle the error. */
228
229         ret = create_exception_two_strings (klass, a1, a2, &error);
230         mono_error_raise_exception (&error); /* FIXME don't raise here */
231
232         return ret;
233 }
234
235 /**
236  * mono_get_exception_divide_by_zero:
237  *
238  * Returns: a new instance of the `System.DivideByZeroException`
239  */
240 MonoException *
241 mono_get_exception_divide_by_zero ()
242 {
243         return mono_exception_from_name (mono_get_corlib (), "System",
244                                          "DivideByZeroException");
245 }
246
247 /**
248  * mono_get_exception_security:
249  *
250  * Returns: a new instance of the `System.Security.SecurityException`
251  */
252 MonoException *
253 mono_get_exception_security ()
254 {
255         return mono_exception_from_name (mono_get_corlib (), "System.Security",
256                                          "SecurityException");
257 }
258
259 /**
260  * mono_get_exception_thread_abort:
261  *
262  * Returns: a new instance of the `System.Threading.ThreadAbortException`
263  */
264 MonoException *
265 mono_get_exception_thread_abort ()
266 {
267         return mono_exception_from_name (mono_get_corlib (), "System.Threading",
268                                          "ThreadAbortException");
269 }
270
271 /**
272  * mono_get_exception_thread_interrupted:
273  *
274  * Returns: a new instance of the `System.Threading.ThreadInterruptedException`
275  */
276 MonoException *
277 mono_get_exception_thread_interrupted ()
278 {
279         return mono_exception_from_name (mono_get_corlib (), "System.Threading",
280                                          "ThreadInterruptedException");
281 }
282
283 /**
284  * mono_get_exception_arithmetic:
285  *
286  * Returns: a new instance of the `System.ArithmeticException`
287  */
288 MonoException *
289 mono_get_exception_arithmetic ()
290 {
291         return mono_exception_from_name (mono_get_corlib (), "System",
292                                          "ArithmeticException");
293 }
294
295 /**
296  * mono_get_exception_overflow:
297  *
298  * Returns: a new instance of the `System.OverflowException`
299  */
300 MonoException *
301 mono_get_exception_overflow ()
302 {
303         return mono_exception_from_name (mono_get_corlib (), "System",
304                                          "OverflowException");
305 }
306
307 /**
308  * mono_get_exception_null_reference:
309  *
310  * Returns: a new instance of the `System.NullReferenceException`
311  */
312 MonoException *
313 mono_get_exception_null_reference ()
314 {
315         return mono_exception_from_name (mono_get_corlib (), "System",
316                                          "NullReferenceException");
317 }
318
319 /**
320  * mono_get_exception_execution_engine:
321  * @msg: the message to pass to the user
322  *
323  * Returns: a new instance of the `System.ExecutionEngineException`
324  */
325 MonoException *
326 mono_get_exception_execution_engine (const char *msg)
327 {
328         return mono_exception_from_name_msg (mono_get_corlib (), "System", "ExecutionEngineException", msg);
329 }
330
331 /**
332  * mono_get_exception_serialization:
333  * @msg: the message to pass to the user
334  *
335  * Returns: a new instance of the `System.Runtime.Serialization.SerializationException`
336  */
337 MonoException *
338 mono_get_exception_serialization (const char *msg)
339 {
340         return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization", "SerializationException", msg);
341 }
342
343 /**
344  * mono_get_exception_invalid_cast:
345  *
346  * Returns: a new instance of the `System.InvalidCastException`
347  */
348 MonoException *
349 mono_get_exception_invalid_cast ()
350 {
351         return mono_exception_from_name (mono_get_corlib (), "System", "InvalidCastException");
352 }
353
354 /**
355  * mono_get_exception_invalid_operation:
356  * @msg: the message to pass to the user
357  *
358  * Returns: a new instance of the `System.InvalidOperationException`
359  */
360 MonoException *
361 mono_get_exception_invalid_operation (const char *msg)
362 {
363         return mono_exception_from_name_msg (mono_get_corlib (), "System",
364                                         "InvalidOperationException", msg);
365 }
366
367 /**
368  * mono_get_exception_index_out_of_range:
369  *
370  * Returns: a new instance of the `System.IndexOutOfRangeException`
371  */
372 MonoException *
373 mono_get_exception_index_out_of_range ()
374 {
375         return mono_exception_from_name (mono_get_corlib (), "System",
376                                          "IndexOutOfRangeException");
377 }
378
379 /**
380  * mono_get_exception_array_type_mismatch:
381  *
382  * Returns: a new instance of the `System.ArrayTypeMismatchException`
383  */
384 MonoException *
385 mono_get_exception_array_type_mismatch ()
386 {
387         return mono_exception_from_name (mono_get_corlib (), "System",
388                                          "ArrayTypeMismatchException");
389 }
390
391 /**
392  * mono_get_exception_type_load:
393  * @class_name: the name of the class that could not be loaded
394  * @assembly_name: the assembly where the class was looked up.
395  *
396  * Returns: a new instance of the `System.TypeLoadException`
397  */
398 MonoException *
399 mono_get_exception_type_load (MonoString *class_name, char *assembly_name)
400 {
401         MonoString *s = assembly_name ? mono_string_new (mono_domain_get (), assembly_name) : mono_string_new (mono_domain_get (), "");
402
403         return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
404                                                      "TypeLoadException", class_name, s);
405 }
406
407 /**
408  * mono_get_exception_not_implemented:
409  * @msg: the message to pass to the user
410  *
411  * Returns: a new instance of the `System.NotImplementedException`
412  */
413 MonoException *
414 mono_get_exception_not_implemented (const char *msg)
415 {
416         return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotImplementedException", msg);
417 }
418
419 /**
420  * mono_get_exception_not_supported:
421  * @msg: the message to pass to the user
422  *
423  * Returns: a new instance of the `System.NotSupportedException`
424  */
425 MonoException *
426 mono_get_exception_not_supported (const char *msg)
427 {
428         return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotSupportedException", msg);
429 }
430
431 /**
432  * mono_get_exception_missing_method:
433  * @class_name: the class where the lookup was performed.
434  * @member_name: the name of the missing method.
435  *
436  * Returns: a new instance of the `System.MissingMethodException`
437  */
438 MonoException *
439 mono_get_exception_missing_method (const char *class_name, const char *member_name)
440 {
441         MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
442         MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
443
444         return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
445                                                      "MissingMethodException", s1, s2);
446 }
447
448 /**
449  * mono_get_exception_missing_field:
450  * @class_name: the class where the lookup was performed
451  * @member_name: the name of the missing method.
452  *
453  * Returns: a new instance of the `System.MissingFieldException`
454  */
455 MonoException *
456 mono_get_exception_missing_field (const char *class_name, const char *member_name)
457 {
458         MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
459         MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
460
461         return mono_exception_from_name_two_strings (mono_get_corlib (), "System",
462                                                      "MissingFieldException", s1, s2);
463 }
464
465 /**
466  * mono_get_exception_argument_null:
467  * @arg: the name of the argument that is null
468  *
469  * Returns: a new instance of the `System.ArgumentNullException`
470  */
471 MonoException*
472 mono_get_exception_argument_null (const char *arg)
473 {
474         MonoException *ex;
475
476         ex = mono_exception_from_name ( 
477                 mono_get_corlib (), "System", "ArgumentNullException");
478
479         if (arg) {
480                 MonoArgumentException *argex = (MonoArgumentException *)ex;
481                 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
482         }
483         
484         return ex;
485 }
486
487 /**
488  * mono_get_exception_argument:
489  * @arg: the name of the invalid argument.
490  *
491  * Returns: a new instance of the `System.ArgumentException`
492  */
493 MonoException *
494 mono_get_exception_argument (const char *arg, const char *msg)
495 {
496         MonoException *ex;
497
498         ex = mono_exception_from_name_msg (
499                 mono_get_corlib (), "System", "ArgumentException", msg);
500
501         if (arg) {
502                 MonoArgumentException *argex = (MonoArgumentException *)ex;
503                 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
504         }
505         
506         return ex;
507 }
508
509 /**
510  * mono_get_exception_argument_out_of_range:
511  * @arg: the name of the out of range argument.
512  *
513  * Returns: a new instance of the `System.ArgumentOutOfRangeException`
514  */
515 MonoException *
516 mono_get_exception_argument_out_of_range (const char *arg)
517 {
518         MonoException *ex;
519
520         ex = mono_exception_from_name (
521                 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
522
523         if (arg) {
524                 MonoArgumentException *argex = (MonoArgumentException *)ex;
525                 MONO_OBJECT_SETREF (argex, param_name, mono_string_new (mono_object_get_domain ((MonoObject*)ex), arg));
526         }
527         
528         return ex;
529 }
530
531 /**
532  * mono_get_exception_thread_state:
533  * @msg: the message to present to the user
534  *
535  * Returns: a new instance of the `System.Threading.ThreadStateException`
536  */
537 MonoException *
538 mono_get_exception_thread_state (const char *msg)
539 {
540         return mono_exception_from_name_msg (
541                 mono_get_corlib (), "System.Threading", "ThreadStateException", msg);
542 }
543
544 /**
545  * mono_get_exception_io:
546  * @msg: the message to present to the user
547  *
548  * Returns: a new instance of the `System.IO.IOException`
549  */
550 MonoException *
551 mono_get_exception_io (const char *msg)
552 {
553         return mono_exception_from_name_msg ( 
554                 mono_get_corlib (), "System.IO", "IOException", msg);
555 }
556
557 /**
558  * mono_get_exception_file_not_found:
559  * @fname: the name of the file not found.
560  *
561  * Returns: a new instance of the `System.IO.FileNotFoundException`
562  */
563 MonoException *
564 mono_get_exception_file_not_found (MonoString *fname)
565 {
566         return mono_exception_from_name_two_strings (
567                 mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname);
568 }
569
570 /**
571  * mono_get_exception_file_not_found2:
572  * @msg: an informative message for the user.
573  * @fname: the name of the file not found.
574  *
575  * Returns: a new instance of the `System.IO.FileNotFoundException`
576  */
577 MonoException *
578 mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
579 {
580         MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
581
582         return mono_exception_from_name_two_strings (
583                 mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname);
584 }
585
586 /**
587  * mono_get_exception_type_initialization:
588  * @type_name: the name of the type that failed to initialize.
589  * @inner: the inner exception.
590  *
591  * Returns: a new instance of the `System.TypeInitializationException`
592  */
593 MonoException *
594 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
595 {
596         MonoError error;
597         MonoException *ret = mono_get_exception_type_initialization_checked (type_name, inner, &error);
598         if (!is_ok (&error)) {
599                 mono_error_cleanup (&error);
600                 return NULL;
601         }
602
603         return ret;
604 }
605
606 MonoException *
607 mono_get_exception_type_initialization_checked (const gchar *type_name, MonoException *inner, MonoError *error)
608 {
609         MonoClass *klass;
610         gpointer args [2];
611         MonoObject *exc;
612         MonoMethod *method;
613         gpointer iter;
614
615         klass = mono_class_load_from_name (mono_get_corlib (), "System", "TypeInitializationException");
616
617         mono_class_init (klass);
618
619         iter = NULL;
620         while ((method = mono_class_get_methods (klass, &iter))) {
621                 if (!strcmp (".ctor", mono_method_get_name (method))) {
622                         MonoMethodSignature *sig = mono_method_signature (method);
623
624                         if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_STRING && mono_class_from_mono_type (sig->params [1]) == mono_defaults.exception_class)
625                                 break;
626                 }
627                 method = NULL;
628         }
629         g_assert (method);
630
631         args [0] = mono_string_new (mono_domain_get (), type_name);
632         args [1] = inner;
633
634         exc = mono_object_new_checked (mono_domain_get (), klass, error);
635         mono_error_assert_ok (error);
636
637         mono_runtime_invoke_checked (method, exc, args, error);
638         return_val_if_nok (error, NULL);
639
640         return (MonoException *) exc;
641 }
642
643 /**
644  * mono_get_exception_synchronization_lock:
645  * @inner: the inner exception.
646  *
647  * Returns: a new instance of the `System.SynchronizationLockException`
648  */
649 MonoException *
650 mono_get_exception_synchronization_lock (const char *msg)
651 {
652         return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg);
653 }
654
655 /**
656  * mono_get_exception_cannot_unload_appdomain:
657  * @inner: the inner exception.
658  *
659  * Returns: a new instance of the `System.CannotUnloadAppDomainException`
660  */
661 MonoException *
662 mono_get_exception_cannot_unload_appdomain (const char *msg)
663 {
664         return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg);
665 }
666
667 /**
668  * mono_get_exception_appdomain_unloaded
669  *
670  * Returns: a new instance of the `System.AppDomainUnloadedException`
671  */
672 MonoException *
673 mono_get_exception_appdomain_unloaded (void)
674 {
675         return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
676 }
677
678 /**
679  * mono_get_exception_bad_image_format:
680  * @msg: an informative message for the user.
681  *
682  * Returns: a new instance of the `System.BadImageFormatException`
683  */
684 MonoException *
685 mono_get_exception_bad_image_format (const char *msg)
686 {
687         return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg);
688 }       
689
690 /**
691  * mono_get_exception_bad_image_format2:
692  * @msg: an informative message for the user.
693  * @fname: The full name of the file with the invalid image.
694  *
695  * Returns: a new instance of the `System.BadImageFormatException`
696  */
697 MonoException *
698 mono_get_exception_bad_image_format2 (const char *msg, MonoString *fname)
699 {
700         MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
701
702         return mono_exception_from_name_two_strings (
703                 mono_get_corlib (), "System", "BadImageFormatException", s, fname);
704 }
705
706 /**
707  * mono_get_exception_stack_overflow:
708  *
709  * Returns: a new instance of the `System.StackOverflowException`
710  */
711 MonoException *
712 mono_get_exception_stack_overflow (void)
713 {
714         return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");       
715 }
716
717 /**
718  * mono_get_exception_out_of_memory:
719  *
720  * Returns: a new instance of the `System.OutOfMemoryException`
721  */
722 MonoException *
723 mono_get_exception_out_of_memory (void)
724 {
725         return mono_exception_from_name (mono_get_corlib (), "System", "OutOfMemoryException");
726 }
727
728 /**
729  * mono_get_exception_field_access:
730  *
731  * Returns: a new instance of the `System.FieldAccessException`
732  */
733 MonoException *
734 mono_get_exception_field_access (void)
735 {
736         return mono_exception_from_name (mono_get_corlib (), "System", "FieldAccessException");
737 }
738
739 /**
740  * mono_get_exception_field_access2:
741  * @msg: an informative message for the user.
742  *
743  * Returns: a new instance of the `System.FieldAccessException`
744  */
745 MonoException *
746 mono_get_exception_field_access_msg (const char *msg)
747 {
748         return mono_exception_from_name_msg (mono_get_corlib (), "System", "FieldAccessException", msg);
749 }
750
751 /**
752  * mono_get_exception_method_access:
753  *
754  * Returns: a new instance of the `System.MethodAccessException`
755  */
756 MonoException *
757 mono_get_exception_method_access (void)
758 {
759         return mono_exception_from_name (mono_get_corlib (), "System", "MethodAccessException");
760 }
761
762 /**
763  * mono_get_exception_method_access2:
764  * @msg: an informative message for the user.
765  *
766  * Returns: a new instance of the `System.MethodAccessException`
767  */
768 MonoException *
769 mono_get_exception_method_access_msg (const char *msg)
770 {
771         return mono_exception_from_name_msg (mono_get_corlib (), "System", "MethodAccessException", msg);
772 }
773
774 /**
775  * mono_get_exception_reflection_type_load:
776  * @types: an array of types that were defined in the moduled loaded.
777  * @exceptions: an array of exceptions that were thrown during the type loading.
778  *
779  * Returns: a new instance of the `System.Reflection.ReflectionTypeLoadException`
780  */
781 MonoException *
782 mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions)
783 {
784         MonoError error;
785         MonoException *ret = mono_get_exception_reflection_type_load_checked (types, exceptions, &error);
786         if (is_ok (&error)) {
787                 mono_error_cleanup (&error);
788                 return NULL;
789         }
790
791         return ret;
792 }
793
794 MonoException *
795 mono_get_exception_reflection_type_load_checked (MonoArray *types, MonoArray *exceptions, MonoError *error)
796 {
797         MonoClass *klass;
798         gpointer args [2];
799         MonoObject *exc;
800         MonoMethod *method;
801         gpointer iter;
802
803         klass = mono_class_load_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
804
805         mono_class_init (klass);
806
807         /* Find the Type[], Exception[] ctor */
808         iter = NULL;
809         while ((method = mono_class_get_methods (klass, &iter))) {
810                 if (!strcmp (".ctor", mono_method_get_name (method))) {
811                         MonoMethodSignature *sig = mono_method_signature (method);
812
813                         if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_SZARRAY && sig->params [1]->type == MONO_TYPE_SZARRAY)
814                                 break;
815                 }
816                 method = NULL;
817         }
818         g_assert (method);
819
820         args [0] = types;
821         args [1] = exceptions;
822
823         exc = mono_object_new_checked (mono_domain_get (), klass, error);
824         mono_error_assert_ok (error);
825
826         mono_runtime_invoke_checked (method, exc, args, error);
827         return_val_if_nok (error, NULL);
828
829         return (MonoException *) exc;
830 }
831
832 MonoException *
833 mono_get_exception_runtime_wrapped (MonoObject *wrapped_exception)
834 {
835         MonoError error;
836         MonoException *ret = mono_get_exception_runtime_wrapped_checked (wrapped_exception, &error);
837         if (!is_ok (&error)) {
838                 mono_error_cleanup (&error);
839                 return NULL;
840         }
841
842         return ret;
843 }
844
845 MonoException *
846 mono_get_exception_runtime_wrapped_checked (MonoObject *wrapped_exception, MonoError *error)
847 {
848         MonoClass *klass;
849         MonoObject *o;
850         MonoMethod *method;
851         MonoDomain *domain = mono_domain_get ();
852         gpointer params [16];
853
854         klass = mono_class_load_from_name (mono_get_corlib (), "System.Runtime.CompilerServices", "RuntimeWrappedException");
855
856         o = mono_object_new_checked (domain, klass, error);
857         mono_error_assert_ok (error);
858         g_assert (o != NULL);
859
860         method = mono_class_get_method_from_name (klass, ".ctor", 1);
861         g_assert (method);
862
863         params [0] = wrapped_exception;
864
865         mono_runtime_invoke_checked (method, o, params, error);
866         return_val_if_nok (error, NULL);
867
868         return (MonoException *)o;
869 }       
870
871 static gboolean
872 append_frame_and_continue (MonoMethod *method, gpointer ip, size_t native_offset, gboolean managed, gpointer user_data)
873 {
874         MonoDomain *domain = mono_domain_get ();
875         GString *text = (GString*)user_data;
876
877         if (method) {
878                 char *msg = mono_debug_print_stack_frame (method, native_offset, domain);
879                 g_string_append_printf (text, "%s\n", msg);
880                 g_free (msg);
881         } else {
882                 g_string_append_printf (text, "<unknown native frame 0x%x>\n", ip);
883         }
884
885         return FALSE;
886 }
887
888 char *
889 mono_exception_get_managed_backtrace (MonoException *exc)
890 {
891         GString *text;
892
893         text = g_string_new_len (NULL, 20);
894
895         if (!mono_get_eh_callbacks ()->mono_exception_walk_trace (exc, append_frame_and_continue, text))
896                 g_string_append (text, "managed backtrace not available\n");
897
898         return g_string_free (text, FALSE);
899 }
900
901 char *
902 mono_exception_get_native_backtrace (MonoException *exc)
903 {
904 #ifdef HAVE_BACKTRACE_SYMBOLS
905         MonoDomain *domain;
906         MonoArray *arr = exc->native_trace_ips;
907         int i, len;
908         GString *text;
909         char **messages;
910
911         if (!arr)
912                 return g_strdup ("");
913         domain = mono_domain_get ();
914         len = mono_array_length (arr);
915         text = g_string_new_len (NULL, len * 20);
916         messages = backtrace_symbols (mono_array_addr (arr, gpointer, 0), len);
917
918
919         for (i = 0; i < len; ++i) {
920                 gpointer ip = mono_array_get (arr, gpointer, i);
921                 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), (char *)ip);
922                 if (ji) {
923                         char *msg = mono_debug_print_stack_frame (mono_jit_info_get_method (ji), (char*)ip - (char*)ji->code_start, domain);
924                         g_string_append_printf (text, "%s\n", msg);
925                         g_free (msg);
926                 } else {
927                         g_string_append_printf (text, "%s\n", messages [i]);
928                 }
929         }
930
931         free (messages);
932         return g_string_free (text, FALSE);
933 #else
934         return g_strdup ("");
935 #endif
936 }
937
938 MonoString *
939 ves_icall_Mono_Runtime_GetNativeStackTrace (MonoException *exc)
940 {
941         char *trace;
942         MonoString *res;
943         if (!exc)
944                 mono_raise_exception (mono_get_exception_argument_null ("exception"));
945
946         trace = mono_exception_get_native_backtrace (exc);
947         res = mono_string_new (mono_domain_get (), trace);
948         g_free (trace);
949         return res;
950 }
951
952 /**
953  * mono_error_raise_exception:
954  * @target_error: the exception to raise
955  *
956  * Raises the exception of @target_error.
957  * Does nothing if @target_error has a success error code.
958  * Aborts in case of a double fault. This happens when it can't recover from an error caused by trying
959  * to construct the first exception object.
960  * The error object @target_error is cleaned up.
961 */
962 void
963 mono_error_raise_exception (MonoError *target_error)
964 {
965         MonoException *ex = mono_error_convert_to_exception (target_error);
966         if (ex)
967                 mono_raise_exception (ex);
968 }
969
970 /**
971  * mono_error_set_pending_exception:
972  * @error: The error
973  *
974  *
975  * If @error is set, convert it to an exception and set the pending exception for the current icall.
976  * Returns TRUE if @error was set, or FALSE otherwise, so that you can write:
977  *    if (mono_error_set_pending_exception (error)) {
978  *      { ... cleanup code ... }
979  *      return;
980  *    }
981  */
982 gboolean
983 mono_error_set_pending_exception (MonoError *error)
984 {
985         MonoException *ex = mono_error_convert_to_exception (error);
986         if (ex) {
987                 mono_set_pending_exception (ex);
988                 return TRUE;
989         } else {
990                 return FALSE;
991         }
992 }
993