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