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