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