Merge pull request #3110 from BrzVlad/fix-complex-conc-scan
[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 <config.h>
17 #include <mono/metadata/exception.h>
18 #include <mono/metadata/exception-internals.h>
19
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>
26
27 #ifdef HAVE_EXECINFO_H
28 #include <execinfo.h>
29 #endif
30
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
36  *
37  * Creates an exception of the given namespace/name class in the
38  * current domain.
39  *
40  * Returns: the initialized exception instance.
41  */
42 MonoException *
43 mono_exception_from_name (MonoImage *image, const char *name_space,
44                           const char *name)
45 {
46         return mono_exception_from_name_domain (mono_domain_get (), image, name_space, name);
47 }
48
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
55  *
56  * Creates an exception object of the given namespace/name class on
57  * the given domain.
58  *
59  * Returns: the initialized exception instance.
60  */
61 MonoException *
62 mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image, 
63                                  const char* name_space, const char *name)
64 {
65         MonoError error;
66         MonoClass *klass;
67         MonoObject *o;
68         MonoDomain *caller_domain = mono_domain_get ();
69
70         klass = mono_class_load_from_name (image, name_space, name);
71
72         o = mono_object_new_checked (domain, klass, &error);
73         mono_error_assert_ok (&error);
74
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);
79
80         if (domain != caller_domain)
81                 mono_domain_set_internal (caller_domain);
82
83         return (MonoException *)o;
84 }
85
86
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
91  *
92  * Creates an exception of the type given by @token.
93  *
94  * Returns: the initialized exception instance.
95  */
96 MonoException *
97 mono_exception_from_token (MonoImage *image, guint32 token)
98 {
99         MonoError error;
100         MonoClass *klass;
101         MonoObject *o;
102
103         klass = mono_class_get_checked (image, token, &error);
104         mono_error_assert_ok (&error);
105
106         o = mono_object_new_checked (mono_domain_get (), klass, &error);
107         mono_error_assert_ok (&error);
108         
109         mono_runtime_object_init_checked (o, &error);
110         mono_error_assert_ok (&error);
111
112         return (MonoException *)o;
113 }
114
115 static MonoException *
116 create_exception_two_strings (MonoClass *klass, MonoString *a1, MonoString *a2, MonoError *error)
117 {
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;
125
126         if (a2 != NULL)
127                 count++;
128         
129         o = mono_object_new_checked (domain, klass, error);
130         mono_error_assert_ok (error);
131
132         iter = NULL;
133         while ((m = mono_class_get_methods (klass, &iter))) {
134                 MonoMethodSignature *sig;
135                 
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;
141
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;
148         }
149
150         args [0] = a1;
151         args [1] = a2;
152
153         mono_runtime_invoke_checked (method, o, args, error);
154         return_val_if_nok (error, NULL);
155
156         return (MonoException *) o;
157 }
158
159 /**
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
166  *
167  * Creates an exception from a constructor that takes two string
168  * arguments.
169  *
170  * Returns: the initialized exception instance.
171  */
172 MonoException *
173 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
174                                       const char *name, MonoString *a1, MonoString *a2)
175 {
176         MonoError error;
177         MonoException *ret;
178
179         ret = mono_exception_from_name_two_strings_checked (image, name_space, name, a1, a2, &error);
180         mono_error_cleanup (&error);
181         return ret;
182 }
183
184 /**
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
192  *
193  * Creates an exception from a constructor that takes two string
194  * arguments.
195  *
196  * Returns: the initialized exception instance. On failure returns
197  * NULL and sets @error.
198  */
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)
203 {
204         MonoClass *klass;
205
206         mono_error_init (error);
207         klass = mono_class_load_from_name (image, name_space, name);
208
209         return create_exception_two_strings (klass, a1, a2, error);
210 }
211
212 /**
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
218  *
219  * Creates an exception and initializes its message field.
220  *
221  * Returns: the initialized exception instance.
222  */
223 MonoException *
224 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
225                               const char *name, const char *msg)
226 {
227         MonoException *ex;
228
229         ex = mono_exception_from_name (image, name_space, name);
230
231         if (msg)
232                 MONO_OBJECT_SETREF (ex, message, mono_string_new (mono_object_get_domain ((MonoObject*)ex), msg));
233
234         return ex;
235 }
236
237 /**
238  * mono_exception_from_token_two_strings:
239  *
240  *   Same as mono_exception_from_name_two_strings, but lookup the exception class using
241  * IMAGE and TOKEN.
242  */
243 MonoException *
244 mono_exception_from_token_two_strings (MonoImage *image, guint32 token,
245                                                                            MonoString *a1, MonoString *a2)
246 {
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;
252 }
253
254 /**
255  * mono_exception_from_token_two_strings_checked:
256  *
257  *   Same as mono_exception_from_name_two_strings, but lookup the exception class using
258  * IMAGE and TOKEN.
259  */
260 MonoException *
261 mono_exception_from_token_two_strings_checked (MonoImage *image, guint32 token,
262                                                MonoString *a1, MonoString *a2,
263                                                MonoError *error)
264 {
265         MonoClass *klass;
266
267         mono_error_init (error);
268
269         klass = mono_class_get_checked (image, token, error);
270         mono_error_assert_ok (error); /* FIXME handle the error. */
271
272         return create_exception_two_strings (klass, a1, a2, error);
273 }
274
275 /**
276  * mono_get_exception_divide_by_zero:
277  *
278  * Returns: a new instance of the `System.DivideByZeroException`
279  */
280 MonoException *
281 mono_get_exception_divide_by_zero ()
282 {
283         return mono_exception_from_name (mono_get_corlib (), "System",
284                                          "DivideByZeroException");
285 }
286
287 /**
288  * mono_get_exception_security:
289  *
290  * Returns: a new instance of the `System.Security.SecurityException`
291  */
292 MonoException *
293 mono_get_exception_security ()
294 {
295         return mono_exception_from_name (mono_get_corlib (), "System.Security",
296                                          "SecurityException");
297 }
298
299 /**
300  * mono_get_exception_thread_abort:
301  *
302  * Returns: a new instance of the `System.Threading.ThreadAbortException`
303  */
304 MonoException *
305 mono_get_exception_thread_abort ()
306 {
307         return mono_exception_from_name (mono_get_corlib (), "System.Threading",
308                                          "ThreadAbortException");
309 }
310
311 /**
312  * mono_get_exception_thread_interrupted:
313  *
314  * Returns: a new instance of the `System.Threading.ThreadInterruptedException`
315  */
316 MonoException *
317 mono_get_exception_thread_interrupted ()
318 {
319         return mono_exception_from_name (mono_get_corlib (), "System.Threading",
320                                          "ThreadInterruptedException");
321 }
322
323 /**
324  * mono_get_exception_arithmetic:
325  *
326  * Returns: a new instance of the `System.ArithmeticException`
327  */
328 MonoException *
329 mono_get_exception_arithmetic ()
330 {
331         return mono_exception_from_name (mono_get_corlib (), "System",
332                                          "ArithmeticException");
333 }
334
335 /**
336  * mono_get_exception_overflow:
337  *
338  * Returns: a new instance of the `System.OverflowException`
339  */
340 MonoException *
341 mono_get_exception_overflow ()
342 {
343         return mono_exception_from_name (mono_get_corlib (), "System",
344                                          "OverflowException");
345 }
346
347 /**
348  * mono_get_exception_null_reference:
349  *
350  * Returns: a new instance of the `System.NullReferenceException`
351  */
352 MonoException *
353 mono_get_exception_null_reference ()
354 {
355         return mono_exception_from_name (mono_get_corlib (), "System",
356                                          "NullReferenceException");
357 }
358
359 /**
360  * mono_get_exception_execution_engine:
361  * @msg: the message to pass to the user
362  *
363  * Returns: a new instance of the `System.ExecutionEngineException`
364  */
365 MonoException *
366 mono_get_exception_execution_engine (const char *msg)
367 {
368         return mono_exception_from_name_msg (mono_get_corlib (), "System", "ExecutionEngineException", msg);
369 }
370
371 /**
372  * mono_get_exception_serialization:
373  * @msg: the message to pass to the user
374  *
375  * Returns: a new instance of the `System.Runtime.Serialization.SerializationException`
376  */
377 MonoException *
378 mono_get_exception_serialization (const char *msg)
379 {
380         return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization", "SerializationException", msg);
381 }
382
383 /**
384  * mono_get_exception_invalid_cast:
385  *
386  * Returns: a new instance of the `System.InvalidCastException`
387  */
388 MonoException *
389 mono_get_exception_invalid_cast ()
390 {
391         return mono_exception_from_name (mono_get_corlib (), "System", "InvalidCastException");
392 }
393
394 /**
395  * mono_get_exception_invalid_operation:
396  * @msg: the message to pass to the user
397  *
398  * Returns: a new instance of the `System.InvalidOperationException`
399  */
400 MonoException *
401 mono_get_exception_invalid_operation (const char *msg)
402 {
403         return mono_exception_from_name_msg (mono_get_corlib (), "System",
404                                         "InvalidOperationException", msg);
405 }
406
407 /**
408  * mono_get_exception_index_out_of_range:
409  *
410  * Returns: a new instance of the `System.IndexOutOfRangeException`
411  */
412 MonoException *
413 mono_get_exception_index_out_of_range ()
414 {
415         return mono_exception_from_name (mono_get_corlib (), "System",
416                                          "IndexOutOfRangeException");
417 }
418
419 /**
420  * mono_get_exception_array_type_mismatch:
421  *
422  * Returns: a new instance of the `System.ArrayTypeMismatchException`
423  */
424 MonoException *
425 mono_get_exception_array_type_mismatch ()
426 {
427         return mono_exception_from_name (mono_get_corlib (), "System",
428                                          "ArrayTypeMismatchException");
429 }
430
431 /**
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.
435  *
436  * Returns: a new instance of the `System.TypeLoadException`
437  */
438 MonoException *
439 mono_get_exception_type_load (MonoString *class_name, char *assembly_name)
440 {
441         MonoString *s = assembly_name ? mono_string_new (mono_domain_get (), assembly_name) : mono_string_new (mono_domain_get (), "");
442
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;
448 }
449
450 /**
451  * mono_get_exception_not_implemented:
452  * @msg: the message to pass to the user
453  *
454  * Returns: a new instance of the `System.NotImplementedException`
455  */
456 MonoException *
457 mono_get_exception_not_implemented (const char *msg)
458 {
459         return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotImplementedException", msg);
460 }
461
462 /**
463  * mono_get_exception_not_supported:
464  * @msg: the message to pass to the user
465  *
466  * Returns: a new instance of the `System.NotSupportedException`
467  */
468 MonoException *
469 mono_get_exception_not_supported (const char *msg)
470 {
471         return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotSupportedException", msg);
472 }
473
474 /**
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.
478  *
479  * Returns: a new instance of the `System.MissingMethodException`
480  */
481 MonoException *
482 mono_get_exception_missing_method (const char *class_name, const char *member_name)
483 {
484         MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
485         MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
486
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;
492 }
493
494 /**
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.
498  *
499  * Returns: a new instance of the `System.MissingFieldException`
500  */
501 MonoException *
502 mono_get_exception_missing_field (const char *class_name, const char *member_name)
503 {
504         MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
505         MonoString *s2 = mono_string_new (mono_domain_get (), member_name);
506
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;
512 }
513
514 /**
515  * mono_get_exception_argument_null:
516  * @arg: the name of the argument that is null
517  *
518  * Returns: a new instance of the `System.ArgumentNullException`
519  */
520 MonoException*
521 mono_get_exception_argument_null (const char *arg)
522 {
523         MonoException *ex;
524
525         ex = mono_exception_from_name ( 
526                 mono_get_corlib (), "System", "ArgumentNullException");
527
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));
531         }
532         
533         return ex;
534 }
535
536 /**
537  * mono_get_exception_argument:
538  * @arg: the name of the invalid argument.
539  *
540  * Returns: a new instance of the `System.ArgumentException`
541  */
542 MonoException *
543 mono_get_exception_argument (const char *arg, const char *msg)
544 {
545         MonoException *ex;
546
547         ex = mono_exception_from_name_msg (
548                 mono_get_corlib (), "System", "ArgumentException", msg);
549
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));
553         }
554         
555         return ex;
556 }
557
558 /**
559  * mono_get_exception_argument_out_of_range:
560  * @arg: the name of the out of range argument.
561  *
562  * Returns: a new instance of the `System.ArgumentOutOfRangeException`
563  */
564 MonoException *
565 mono_get_exception_argument_out_of_range (const char *arg)
566 {
567         MonoException *ex;
568
569         ex = mono_exception_from_name (
570                 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
571
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));
575         }
576         
577         return ex;
578 }
579
580 /**
581  * mono_get_exception_thread_state:
582  * @msg: the message to present to the user
583  *
584  * Returns: a new instance of the `System.Threading.ThreadStateException`
585  */
586 MonoException *
587 mono_get_exception_thread_state (const char *msg)
588 {
589         return mono_exception_from_name_msg (
590                 mono_get_corlib (), "System.Threading", "ThreadStateException", msg);
591 }
592
593 /**
594  * mono_get_exception_io:
595  * @msg: the message to present to the user
596  *
597  * Returns: a new instance of the `System.IO.IOException`
598  */
599 MonoException *
600 mono_get_exception_io (const char *msg)
601 {
602         return mono_exception_from_name_msg ( 
603                 mono_get_corlib (), "System.IO", "IOException", msg);
604 }
605
606 /**
607  * mono_get_exception_file_not_found:
608  * @fname: the name of the file not found.
609  *
610  * Returns: a new instance of the `System.IO.FileNotFoundException`
611  */
612 MonoException *
613 mono_get_exception_file_not_found (MonoString *fname)
614 {
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;
620 }
621
622 /**
623  * mono_get_exception_file_not_found2:
624  * @msg: an informative message for the user.
625  * @fname: the name of the file not found.
626  *
627  * Returns: a new instance of the `System.IO.FileNotFoundException`
628  */
629 MonoException *
630 mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
631 {
632         MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
633
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;
639 }
640
641 /**
642  * mono_get_exception_type_initialization:
643  * @type_name: the name of the type that failed to initialize.
644  * @inner: the inner exception.
645  *
646  * Returns: a new instance of the `System.TypeInitializationException`
647  */
648 MonoException *
649 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
650 {
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;
656         }
657
658         return ret;
659 }
660
661 MonoException *
662 mono_get_exception_type_initialization_checked (const gchar *type_name, MonoException *inner, MonoError *error)
663 {
664         MonoClass *klass;
665         gpointer args [2];
666         MonoObject *exc;
667         MonoMethod *method;
668         gpointer iter;
669
670         klass = mono_class_load_from_name (mono_get_corlib (), "System", "TypeInitializationException");
671
672         mono_class_init (klass);
673
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);
678
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;
681                 }
682                 method = NULL;
683         }
684         g_assert (method);
685
686         args [0] = mono_string_new (mono_domain_get (), type_name);
687         args [1] = inner;
688
689         exc = mono_object_new_checked (mono_domain_get (), klass, error);
690         mono_error_assert_ok (error);
691
692         mono_runtime_invoke_checked (method, exc, args, error);
693         return_val_if_nok (error, NULL);
694
695         return (MonoException *) exc;
696 }
697
698 /**
699  * mono_get_exception_synchronization_lock:
700  * @inner: the inner exception.
701  *
702  * Returns: a new instance of the `System.SynchronizationLockException`
703  */
704 MonoException *
705 mono_get_exception_synchronization_lock (const char *msg)
706 {
707         return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg);
708 }
709
710 /**
711  * mono_get_exception_cannot_unload_appdomain:
712  * @inner: the inner exception.
713  *
714  * Returns: a new instance of the `System.CannotUnloadAppDomainException`
715  */
716 MonoException *
717 mono_get_exception_cannot_unload_appdomain (const char *msg)
718 {
719         return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg);
720 }
721
722 /**
723  * mono_get_exception_appdomain_unloaded
724  *
725  * Returns: a new instance of the `System.AppDomainUnloadedException`
726  */
727 MonoException *
728 mono_get_exception_appdomain_unloaded (void)
729 {
730         return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
731 }
732
733 /**
734  * mono_get_exception_bad_image_format:
735  * @msg: an informative message for the user.
736  *
737  * Returns: a new instance of the `System.BadImageFormatException`
738  */
739 MonoException *
740 mono_get_exception_bad_image_format (const char *msg)
741 {
742         return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg);
743 }       
744
745 /**
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.
749  *
750  * Returns: a new instance of the `System.BadImageFormatException`
751  */
752 MonoException *
753 mono_get_exception_bad_image_format2 (const char *msg, MonoString *fname)
754 {
755         MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
756
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;
762 }
763
764 /**
765  * mono_get_exception_stack_overflow:
766  *
767  * Returns: a new instance of the `System.StackOverflowException`
768  */
769 MonoException *
770 mono_get_exception_stack_overflow (void)
771 {
772         return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");       
773 }
774
775 /**
776  * mono_get_exception_out_of_memory:
777  *
778  * Returns: a new instance of the `System.OutOfMemoryException`
779  */
780 MonoException *
781 mono_get_exception_out_of_memory (void)
782 {
783         return mono_exception_from_name (mono_get_corlib (), "System", "OutOfMemoryException");
784 }
785
786 /**
787  * mono_get_exception_field_access:
788  *
789  * Returns: a new instance of the `System.FieldAccessException`
790  */
791 MonoException *
792 mono_get_exception_field_access (void)
793 {
794         return mono_exception_from_name (mono_get_corlib (), "System", "FieldAccessException");
795 }
796
797 /**
798  * mono_get_exception_field_access2:
799  * @msg: an informative message for the user.
800  *
801  * Returns: a new instance of the `System.FieldAccessException`
802  */
803 MonoException *
804 mono_get_exception_field_access_msg (const char *msg)
805 {
806         return mono_exception_from_name_msg (mono_get_corlib (), "System", "FieldAccessException", msg);
807 }
808
809 /**
810  * mono_get_exception_method_access:
811  *
812  * Returns: a new instance of the `System.MethodAccessException`
813  */
814 MonoException *
815 mono_get_exception_method_access (void)
816 {
817         return mono_exception_from_name (mono_get_corlib (), "System", "MethodAccessException");
818 }
819
820 /**
821  * mono_get_exception_method_access2:
822  * @msg: an informative message for the user.
823  *
824  * Returns: a new instance of the `System.MethodAccessException`
825  */
826 MonoException *
827 mono_get_exception_method_access_msg (const char *msg)
828 {
829         return mono_exception_from_name_msg (mono_get_corlib (), "System", "MethodAccessException", msg);
830 }
831
832 /**
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.
836  *
837  * Returns: a new instance of the `System.Reflection.ReflectionTypeLoadException`
838  */
839 MonoException *
840 mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions)
841 {
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;
847         }
848
849         return ret;
850 }
851
852 MonoException *
853 mono_get_exception_reflection_type_load_checked (MonoArray *types, MonoArray *exceptions, MonoError *error)
854 {
855         MonoClass *klass;
856         gpointer args [2];
857         MonoObject *exc;
858         MonoMethod *method;
859         gpointer iter;
860
861         klass = mono_class_load_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
862
863         mono_class_init (klass);
864
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);
870
871                         if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_SZARRAY && sig->params [1]->type == MONO_TYPE_SZARRAY)
872                                 break;
873                 }
874                 method = NULL;
875         }
876         g_assert (method);
877
878         args [0] = types;
879         args [1] = exceptions;
880
881         exc = mono_object_new_checked (mono_domain_get (), klass, error);
882         mono_error_assert_ok (error);
883
884         mono_runtime_invoke_checked (method, exc, args, error);
885         return_val_if_nok (error, NULL);
886
887         return (MonoException *) exc;
888 }
889
890 MonoException *
891 mono_get_exception_runtime_wrapped (MonoObject *wrapped_exception)
892 {
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;
898         }
899
900         return ret;
901 }
902
903 MonoException *
904 mono_get_exception_runtime_wrapped_checked (MonoObject *wrapped_exception, MonoError *error)
905 {
906         MonoClass *klass;
907         MonoObject *o;
908         MonoMethod *method;
909         MonoDomain *domain = mono_domain_get ();
910         gpointer params [16];
911
912         klass = mono_class_load_from_name (mono_get_corlib (), "System.Runtime.CompilerServices", "RuntimeWrappedException");
913
914         o = mono_object_new_checked (domain, klass, error);
915         mono_error_assert_ok (error);
916         g_assert (o != NULL);
917
918         method = mono_class_get_method_from_name (klass, ".ctor", 1);
919         g_assert (method);
920
921         params [0] = wrapped_exception;
922
923         mono_runtime_invoke_checked (method, o, params, error);
924         return_val_if_nok (error, NULL);
925
926         return (MonoException *)o;
927 }       
928
929 static gboolean
930 append_frame_and_continue (MonoMethod *method, gpointer ip, size_t native_offset, gboolean managed, gpointer user_data)
931 {
932         MonoDomain *domain = mono_domain_get ();
933         GString *text = (GString*)user_data;
934
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);
941         }
942
943         return FALSE;
944 }
945
946 char *
947 mono_exception_get_managed_backtrace (MonoException *exc)
948 {
949         GString *text;
950
951         text = g_string_new_len (NULL, 20);
952
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");
955
956         return g_string_free (text, FALSE);
957 }
958
959 char *
960 mono_exception_get_native_backtrace (MonoException *exc)
961 {
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;
968
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);
975
976
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]);
986                 }
987         }
988
989         free (messages);
990         return g_string_free (text, FALSE);
991 #else
992         return g_strdup ("");
993 #endif
994 }
995
996 MonoString *
997 ves_icall_Mono_Runtime_GetNativeStackTrace (MonoException *exc)
998 {
999         char *trace;
1000         MonoString *res;
1001         if (!exc) {
1002                 mono_set_pending_exception (mono_get_exception_argument_null ("exception"));
1003                 return NULL;
1004         }
1005
1006         trace = mono_exception_get_native_backtrace (exc);
1007         res = mono_string_new (mono_domain_get (), trace);
1008         g_free (trace);
1009         return res;
1010 }
1011
1012 /**
1013  * mono_error_raise_exception:
1014  * @target_error: the exception to raise
1015  *
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.
1021 */
1022 void
1023 mono_error_raise_exception (MonoError *target_error)
1024 {
1025         MonoException *ex = mono_error_convert_to_exception (target_error);
1026         if (ex)
1027                 mono_raise_exception (ex);
1028 }
1029
1030 /**
1031  * mono_error_set_pending_exception:
1032  * @error: The error
1033  *
1034  *
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;
1040  *    }
1041  */
1042 gboolean
1043 mono_error_set_pending_exception (MonoError *error)
1044 {
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;
1051         }
1052 }
1053