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