2 * mini-exceptions.c: generic exception support
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
15 #ifdef HAVE_EXECINFO_H
19 #include <mono/metadata/appdomain.h>
20 #include <mono/metadata/tabledefs.h>
21 #include <mono/metadata/threads.h>
22 #include <mono/metadata/debug-helpers.h>
23 #include <mono/metadata/exception.h>
24 #include <mono/metadata/gc-internal.h>
25 #include <mono/metadata/mono-debug.h>
30 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
35 #define IS_ON_SIGALTSTACK(jit_tls) ((jit_tls) && ((guint8*)&(jit_tls) > (guint8*)(jit_tls)->signal_stack) && ((guint8*)&(jit_tls) < ((guint8*)(jit_tls)->signal_stack + (jit_tls)->signal_stack_size)))
37 #ifndef MONO_ARCH_CONTEXT_DEF
38 #define MONO_ARCH_CONTEXT_DEF
41 #ifndef mono_find_jit_info
43 /* mono_find_jit_info:
45 * This function is used to gather information from @ctx. It return the
46 * MonoJitInfo of the corresponding function, unwinds one stack frame and
47 * stores the resulting context into @new_ctx. It also stores a string
48 * describing the stack location into @trace (if not NULL), and modifies
49 * the @lmf if necessary. @native_offset return the IP offset from the
50 * start of the function or -1 if that info is not available.
53 mono_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
54 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
58 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
70 ji = mono_arch_find_jit_info (domain, jit_tls, res, prev_ji, ctx, new_ctx, NULL, lmf, NULL, &managed2);
72 if (ji == (gpointer)-1)
75 if (managed2 || ji->method->wrapper_type) {
76 char *source_location, *tmpaddr, *fname;
77 gint32 offset, iloffset;
80 /* ctx->ip points into native code */
81 offset = (char*)MONO_CONTEXT_GET_IP (new_ctx) - (char*)ji->code_start;
83 offset = (char *)ip - (char *)ji->code_start;
86 *native_offset = offset;
89 if (!ji->method->wrapper_type)
93 source_location = mono_debug_source_location_from_address (ji->method, offset, NULL, domain);
94 iloffset = mono_debug_il_offset_from_address (ji->method, offset, domain);
97 tmpaddr = g_strdup_printf ("<0x%05x>", offset);
99 tmpaddr = g_strdup_printf ("[0x%05x]", iloffset);
101 fname = mono_method_full_name (ji->method, TRUE);
104 *trace = g_strdup_printf ("in %s (at %s) %s", tmpaddr, source_location, fname);
106 *trace = g_strdup_printf ("in %s %s", tmpaddr, fname);
109 g_free (source_location);
115 char *fname = mono_method_full_name (res->method, TRUE);
116 *trace = g_strdup_printf ("in (unmanaged) %s", fname);
124 #endif /* mono_find_jit_info */
127 ves_icall_System_Exception_get_trace (MonoException *ex)
129 MonoDomain *domain = mono_domain_get ();
131 MonoArray *ta = ex->trace_ips;
137 /* Exception is not thrown yet */
140 len = mono_array_length (ta);
141 trace_str = g_string_new ("");
142 for (i = 0; i < len; i++) {
144 gpointer ip = mono_array_get (ta, gpointer, i);
146 ji = mono_jit_info_table_find (domain, ip);
148 /* Unmanaged frame */
149 g_string_append_printf (trace_str, "in (unmanaged) %p\n", ip);
151 char *source_location, *fname;
152 gint32 address, iloffset;
154 address = (char *)ip - (char *)ji->code_start;
156 source_location = mono_debug_source_location_from_address (ji->method, address, NULL, ex->object.vtable->domain);
157 iloffset = mono_debug_il_offset_from_address (ji->method, address, ex->object.vtable->domain);
160 sprintf (tmpaddr, "<0x%05x>", address);
162 sprintf (tmpaddr, "[0x%05x]", iloffset);
164 fname = mono_method_full_name (ji->method, TRUE);
167 g_string_append_printf (trace_str, "in %s (at %s) %s\n", tmpaddr, source_location, fname);
169 g_string_append_printf (trace_str, "in %s %s\n", tmpaddr, fname);
172 g_free (source_location);
176 res = mono_string_new (ex->object.vtable->domain, trace_str->str);
177 g_string_free (trace_str, TRUE);
183 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
185 MonoDomain *domain = mono_domain_get ();
187 MonoArray *ta = exc->trace_ips;
191 /* Exception is not thrown yet */
192 return mono_array_new (domain, mono_defaults.stack_frame_class, 0);
195 len = mono_array_length (ta);
197 res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
199 for (i = skip; i < len; i++) {
201 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
202 gpointer ip = mono_array_get (ta, gpointer, i);
204 ji = mono_jit_info_table_find (domain, ip);
206 /* Unmanaged frame */
207 mono_array_setref (res, i, sf);
211 g_assert (ji != NULL);
213 if (ji->method->wrapper_type) {
217 s = mono_method_full_name (ji->method, TRUE);
218 MONO_OBJECT_SETREF (sf, internal_method_name, mono_string_new (domain, s));
222 MONO_OBJECT_SETREF (sf, method, mono_method_get_object (domain, ji->method, NULL));
223 sf->native_offset = (char *)ip - (char *)ji->code_start;
225 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
227 if (need_file_info) {
230 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
233 MONO_OBJECT_SETREF (sf, filename, mono_string_new (domain, filename));
241 mono_array_setref (res, i, sf);
249 * @domain: starting appdomain
250 * @jit_tls: JIT data for the thread
251 * @start_ctx: starting state of the stack frame
252 * @func: callback to call for each stack frame
253 * @user_data: data passed to the callback
255 * This function walks the stack of a thread, starting from the state
256 * represented by jit_tls and start_ctx. For each frame the callback
257 * function is called with the relevant info. The walk ends when no more
258 * managed stack frames are found or when the callback returns a TRUE value.
259 * Note that the function can be used to walk the stack of a thread
260 * different from the current.
263 mono_walk_stack (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoContext *start_ctx, MonoStackFrameWalk func, gpointer user_data)
265 MonoLMF *lmf = jit_tls->lmf;
266 MonoJitInfo *ji, rji;
269 MonoContext ctx, new_ctx;
273 while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
275 * FIXME: mono_find_jit_info () will need to be able to return a different
276 * MonoDomain when apddomain transitions are found on the stack.
278 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
279 if (!ji || ji == (gpointer)-1)
282 if (func (domain, &new_ctx, ji, user_data))
289 #ifndef CUSTOM_STACK_WALK
292 mono_jit_walk_stack_from_ctx (MonoStackWalk func, MonoContext *start_ctx, gboolean do_il_offset, gpointer user_data)
294 MonoDomain *domain = mono_domain_get ();
295 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
296 MonoLMF *lmf = jit_tls->lmf;
297 MonoJitInfo *ji, rji;
298 gint native_offset, il_offset;
300 MonoContext ctx, new_ctx;
302 MONO_ARCH_CONTEXT_DEF
304 mono_arch_flush_register_windows ();
307 memcpy (&ctx, start_ctx, sizeof (MonoContext));
309 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
310 MONO_INIT_CONTEXT_FROM_CURRENT (&ctx);
312 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, mono_jit_walk_stack_from_ctx);
316 while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
318 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
321 if (ji == (gpointer)-1)
324 il_offset = do_il_offset ? mono_debug_il_offset_from_address (ji->method, native_offset, domain): -1;
326 if (func (ji->method, native_offset, il_offset, managed, user_data))
334 mono_jit_walk_stack (MonoStackWalk func, gboolean do_il_offset, gpointer user_data)
336 mono_jit_walk_stack_from_ctx (func, NULL, do_il_offset, user_data);
340 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
341 MonoReflectionMethod **method,
342 gint32 *iloffset, gint32 *native_offset,
343 MonoString **file, gint32 *line, gint32 *column)
345 MonoDomain *domain = mono_domain_get ();
346 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
347 MonoLMF *lmf = jit_tls->lmf;
348 MonoJitInfo *ji, rji;
349 MonoContext ctx, new_ctx;
351 MONO_ARCH_CONTEXT_DEF;
353 mono_arch_flush_register_windows ();
355 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_get_frame_info);
360 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, native_offset, NULL);
364 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
367 /* skip all wrappers ??*/
368 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
369 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE ||
370 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH ||
371 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
372 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
379 *method = mono_method_get_object (domain, ji->method, NULL);
380 *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset, domain);
382 if (need_file_info) {
385 filename = mono_debug_source_location_from_address (ji->method, *native_offset, line, domain);
387 *file = filename? mono_string_new (domain, filename): NULL;
396 #endif /* CUSTOM_STACK_WALK */
400 MonoSecurityFrame *frame;
401 } MonoFrameSecurityInfo;
404 callback_get_first_frame_security_info (MonoDomain *domain, MonoContext *ctx, MonoJitInfo *ji, gpointer data)
406 MonoFrameSecurityInfo *si = (MonoFrameSecurityInfo*) data;
408 /* FIXME: skip all wrappers ?? probably not - case by case testing is required */
409 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
410 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE ||
411 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH ||
412 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
413 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE) {
422 si->frame = mono_declsec_create_frame (domain, ji);
424 /* Stop - we only want the first frame (e.g. LinkDemand and InheritanceDemand) */
429 * ves_icall_System_Security_SecurityFrame_GetSecurityFrame:
430 * @skip: the number of stack frames to skip
432 * This function returns a the security informations of a single stack frame
433 * (after the skipped ones). This is required for [NonCas]LinkDemand[Choice]
434 * and [NonCas]InheritanceDemand[Choice] as only the caller security is
438 ves_icall_System_Security_SecurityFrame_GetSecurityFrame (gint32 skip)
440 MonoDomain *domain = mono_domain_get ();
441 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
442 MonoFrameSecurityInfo si;
445 MONO_ARCH_CONTEXT_DEF
447 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_System_Security_SecurityFrame_GetSecurityFrame);
451 mono_walk_stack (domain, jit_tls, &ctx, callback_get_first_frame_security_info, (gpointer)&si);
453 return (si.skips == 0) ? si.frame : NULL;
465 grow_array (MonoSecurityStack *stack)
467 MonoDomain *domain = mono_domain_get ();
468 guint32 newsize = (stack->maximum << 1);
469 MonoArray *newstack = mono_array_new (domain, mono_defaults.runtimesecurityframe_class, newsize);
471 for (i=0; i < stack->maximum; i++) {
472 gpointer frame = mono_array_get (stack->stack, gpointer, i);
473 mono_array_setref (newstack, i, frame);
475 stack->maximum = newsize;
476 stack->stack = newstack;
480 callback_get_stack_frames_security_info (MonoDomain *domain, MonoContext *ctx, MonoJitInfo *ji, gpointer data)
482 MonoSecurityStack *ss = (MonoSecurityStack*) data;
484 /* FIXME: skip all wrappers ?? probably not - case by case testing is required */
485 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
486 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE ||
487 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH ||
488 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
489 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE) {
498 if (ss->count == ss->maximum)
501 mono_array_setref (ss->stack, ss->count++, mono_declsec_create_frame (domain, ji));
503 /* continue down the stack */
508 glist_to_array (GList *list, MonoClass *eclass)
510 MonoDomain *domain = mono_domain_get ();
517 len = g_list_length (list);
518 res = mono_array_new (domain, eclass, len);
520 for (i = 0; list; list = list->next, i++)
521 mono_array_set (res, gpointer, i, list->data);
527 * ves_icall_System_Security_SecurityFrame_GetSecurityStack:
528 * @skip: the number of stack frames to skip
530 * This function returns an managed array of containing the security
531 * informations for each frame (after the skipped ones). This is used for
532 * [NonCas]Demand[Choice] where the complete evaluation of the stack is
536 ves_icall_System_Security_SecurityFrame_GetSecurityStack (gint32 skip)
538 MonoDomain *domain = mono_domain_get ();
539 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
540 MonoSecurityStack ss;
543 MONO_ARCH_CONTEXT_DEF
545 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_System_Security_SecurityFrame_GetSecurityStack);
549 ss.maximum = MONO_CAS_INITIAL_STACK_SIZE;
550 ss.stack = mono_array_new (domain, mono_defaults.runtimesecurityframe_class, ss.maximum);
551 mono_walk_stack (domain, jit_tls, &ctx, callback_get_stack_frames_security_info, (gpointer)&ss);
552 /* g_warning ("STACK RESULT: %d out of %d", ss.count, ss.maximum); */
556 #ifndef CUSTOM_EXCEPTION_HANDLING
559 * mono_handle_exception_internal:
560 * @ctx: saved processor state
561 * @obj: the exception object
562 * @test_only: only test if the exception is caught, but dont call handlers
563 * @out_filter_idx: out parameter. if test_only is true, set to the index of
564 * the first filter clause which caught the exception.
567 mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer original_ip, gboolean test_only, gint32 *out_filter_idx)
569 MonoDomain *domain = mono_domain_get ();
570 MonoJitInfo *ji, rji;
571 static int (*call_filter) (MonoContext *, gpointer) = NULL;
572 static void (*restore_context) (void *);
573 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
574 MonoLMF *lmf = jit_tls->lmf;
575 MonoArray *initial_trace_ips = NULL;
576 GList *trace_ips = NULL;
577 MonoException *mono_ex;
578 gboolean stack_overflow = FALSE;
579 MonoContext initial_ctx;
581 gboolean gc_disabled = FALSE;
582 gboolean has_dynamic_methods = FALSE;
583 gint32 filter_idx, first_filter_idx;
586 * This function might execute on an alternate signal stack, and Boehm GC
588 * Also, since the altstack is small, stack space intensive operations like
589 * JIT compilation should be avoided.
591 if (IS_ON_SIGALTSTACK (jit_tls)) {
593 * FIXME: disabling/enabling GC while already on a signal stack might
594 * not be safe either.
596 /* Have to reenable it later */
601 g_assert (ctx != NULL);
603 MonoException *ex = mono_get_exception_null_reference ();
604 MONO_OBJECT_SETREF (ex, message, mono_string_new (domain, "Object reference not set to an instance of an object"));
605 obj = (MonoObject *)ex;
609 * Allocate a new exception object instead of the preconstructed ones.
610 * We can't do this in sigsegv_signal_handler, since GC is not yet
613 if (obj == domain->stack_overflow_ex) {
614 obj = mono_get_exception_stack_overflow ();
615 stack_overflow = TRUE;
617 else if (obj == domain->null_reference_ex) {
618 obj = mono_get_exception_null_reference ();
621 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
622 mono_ex = (MonoException*)obj;
623 initial_trace_ips = mono_ex->trace_ips;
629 call_filter = mono_arch_get_call_filter ();
631 if (!restore_context)
632 restore_context = mono_arch_get_restore_context ();
634 g_assert (jit_tls->end_of_stack);
635 g_assert (jit_tls->abort_func);
638 MonoContext ctx_cp = *ctx;
639 if (mono_trace_is_enabled ())
640 g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
641 if (!mono_handle_exception_internal (&ctx_cp, obj, original_ip, TRUE, &first_filter_idx)) {
642 if (mono_break_on_exc)
644 mono_unhandled_exception (obj);
646 if (mono_debugger_unhandled_exception (original_ip, MONO_CONTEXT_GET_SP (ctx), obj)) {
648 * If this returns true, then we're running inside the
649 * Mono Debugger and the debugger wants us to restore the
650 * context and continue (normally, the debugger inserts
651 * a breakpoint on the `original_ip', so it regains control
652 * immediately after restoring the context).
654 MONO_CONTEXT_SET_IP (ctx, original_ip);
655 restore_context (ctx);
656 g_assert_not_reached ();
662 *out_filter_idx = -1;
665 memset (&rji, 0, sizeof (rji));
671 ji = mono_find_jit_info (domain, jit_tls, &rji, &rji, ctx, &new_ctx,
672 NULL, &lmf, NULL, NULL);
674 g_warning ("Exception inside function without unwind info");
675 g_assert_not_reached ();
678 if (ji != (gpointer)-1) {
680 //printf ("M: %s %d %d.\n", mono_method_full_name (ji->method, TRUE), frame_count, test_only);
682 if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
684 * Avoid overwriting the stack trace if the exception is
685 * rethrown. Also avoid giant stack traces during a stack
688 if (!initial_trace_ips && (frame_count < 1000)) {
689 trace_ips = g_list_prepend (trace_ips, MONO_CONTEXT_GET_IP (ctx));
693 if (ji->method->dynamic)
694 has_dynamic_methods = TRUE;
697 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
699 free_stack = 0xffffff;
702 * During stack overflow, wait till the unwinding frees some stack
703 * space before running handlers/finalizers.
705 if ((free_stack > (64 * 1024)) && ji->num_clauses) {
708 for (i = 0; i < ji->num_clauses; i++) {
709 MonoJitExceptionInfo *ei = &ji->clauses [i];
710 gboolean filtered = FALSE;
713 if (ei->try_start < MONO_CONTEXT_GET_IP (ctx) &&
715 if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
717 MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) {
720 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)) {
721 /* store the exception object in cfg->excvar */
722 g_assert (ei->exvar_offset);
723 *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = obj;
726 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
727 // mono_debugger_handle_exception (ei->data.filter, MONO_CONTEXT_GET_SP (ctx), obj);
729 filtered = call_filter (ctx, ei->data.filter);
730 if (filtered && out_filter_idx)
731 *out_filter_idx = filter_idx;
735 * Filter clauses should only be run in the
736 * first pass of exception handling.
738 filtered = (filter_idx == first_filter_idx);
743 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
744 mono_object_isinst (obj, ei->data.catch_class)) || filtered) {
746 if (mono_ex && !initial_trace_ips) {
747 trace_ips = g_list_reverse (trace_ips);
748 MONO_OBJECT_SETREF (mono_ex, trace_ips, glist_to_array (trace_ips, mono_defaults.int_class));
749 if (has_dynamic_methods)
750 /* These methods could go away anytime, so compute the stack trace now */
751 MONO_OBJECT_SETREF (mono_ex, stack_trace, ves_icall_System_Exception_get_trace (mono_ex));
753 g_list_free (trace_ips);
759 if (mono_trace_is_enabled () && mono_trace_eval (ji->method))
760 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
761 mono_debugger_handle_exception (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), obj);
762 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
769 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
770 MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
771 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
772 if (mono_trace_is_enabled () && mono_trace_eval (ji->method))
773 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
774 mono_debugger_handle_exception (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), obj);
775 call_filter (ctx, ei->handler_start);
785 if (ji == (gpointer)-1) {
792 if (IS_ON_SIGALTSTACK (jit_tls)) {
793 /* Switch back to normal stack */
794 if (stack_overflow) {
795 /* Free up some stack space */
796 MONO_CONTEXT_SET_SP (&initial_ctx, (gssize)(MONO_CONTEXT_GET_SP (&initial_ctx)) + (64 * 1024));
797 g_assert ((gssize)MONO_CONTEXT_GET_SP (&initial_ctx) < (gssize)jit_tls->end_of_stack);
799 MONO_CONTEXT_SET_IP (&initial_ctx, (gssize)jit_tls->abort_func);
800 restore_context (&initial_ctx);
803 jit_tls->abort_func (obj);
804 g_assert_not_reached ();
806 if (mono_ex && !initial_trace_ips) {
807 trace_ips = g_list_reverse (trace_ips);
808 MONO_OBJECT_SETREF (mono_ex, trace_ips, glist_to_array (trace_ips, mono_defaults.int_class));
809 if (has_dynamic_methods)
810 /* These methods could go away anytime, so compute the stack trace now */
811 MONO_OBJECT_SETREF (mono_ex, stack_trace, ves_icall_System_Exception_get_trace (mono_ex));
813 g_list_free (trace_ips);
819 g_assert_not_reached ();
823 * mono_debugger_run_finally:
824 * @start_ctx: saved processor state
826 * This method is called by the Mono Debugger to call all `finally' clauses of the
827 * current stack frame. It's used when the user issues a `return' command to make
828 * the current stack frame return. After returning from this method, the debugger
829 * unwinds the stack one frame and gives control back to the user.
831 * NOTE: This method is only used when running inside the Mono Debugger.
834 mono_debugger_run_finally (MonoContext *start_ctx)
836 static int (*call_filter) (MonoContext *, gpointer) = NULL;
837 MonoDomain *domain = mono_domain_get ();
838 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
839 MonoLMF *lmf = jit_tls->lmf;
840 MonoContext ctx, new_ctx;
841 MonoJitInfo *ji, rji;
846 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, NULL, NULL);
847 if (!ji || ji == (gpointer)-1)
851 call_filter = mono_arch_get_call_filter ();
853 for (i = 0; i < ji->num_clauses; i++) {
854 MonoJitExceptionInfo *ei = &ji->clauses [i];
856 if ((ei->try_start <= MONO_CONTEXT_GET_IP (&ctx)) &&
857 (MONO_CONTEXT_GET_IP (&ctx) < ei->try_end) &&
858 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
859 call_filter (&ctx, ei->handler_start);
865 * mono_handle_exception:
866 * @ctx: saved processor state
867 * @obj: the exception object
868 * @test_only: only test if the exception is caught, but dont call handlers
871 mono_handle_exception (MonoContext *ctx, gpointer obj, gpointer original_ip, gboolean test_only)
873 return mono_handle_exception_internal (ctx, obj, original_ip, test_only, NULL);
876 #endif /* CUSTOM_EXCEPTION_HANDLING */
878 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
881 mono_setup_altstack (MonoJitTlsData *tls)
883 pthread_t self = pthread_self();
886 struct sigaltstack sa;
887 guint8 *staddr = NULL;
888 guint8 *current = (guint8*)&staddr;
890 if (mono_running_on_valgrind ())
893 /* Determine stack boundaries */
894 pthread_attr_init( &attr );
895 #ifdef HAVE_PTHREAD_GETATTR_NP
896 pthread_getattr_np( self, &attr );
898 #ifdef HAVE_PTHREAD_ATTR_GET_NP
899 pthread_attr_get_np( self, &attr );
901 pthread_attr_getstacksize( &attr, &stsize );
903 #error "Not implemented"
907 pthread_attr_getstack( &attr, (void**)&staddr, &stsize );
912 g_assert ((current > staddr) && (current < staddr + stsize));
914 tls->end_of_stack = staddr + stsize;
917 * threads created by nptl does not seem to have a guard page, and
918 * since the main thread is not created by us, we can't even set one.
919 * Increasing stsize fools the SIGSEGV signal handler into thinking this
920 * is a stack overflow exception.
922 tls->stack_size = stsize + getpagesize ();
924 /* Setup an alternate signal stack */
925 tls->signal_stack = mmap (0, MONO_ARCH_SIGNAL_STACK_SIZE, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
926 tls->signal_stack_size = MONO_ARCH_SIGNAL_STACK_SIZE;
928 g_assert (tls->signal_stack);
930 sa.ss_sp = tls->signal_stack;
931 sa.ss_size = MONO_ARCH_SIGNAL_STACK_SIZE;
932 sa.ss_flags = SS_ONSTACK;
933 sigaltstack (&sa, NULL);
937 mono_free_altstack (MonoJitTlsData *tls)
939 struct sigaltstack sa;
942 sa.ss_sp = tls->signal_stack;
943 sa.ss_size = MONO_ARCH_SIGNAL_STACK_SIZE;
944 sa.ss_flags = SS_DISABLE;
945 err = sigaltstack (&sa, NULL);
948 if (tls->signal_stack)
949 munmap (tls->signal_stack, MONO_ARCH_SIGNAL_STACK_SIZE);
952 #endif /* MONO_ARCH_SIGSEGV_ON_ALTSTACK */
955 print_stack_frame (MonoMethod *method, gint32 native_offset, gint32 il_offset, gboolean managed, gpointer data)
957 FILE *stream = (FILE*)data;
960 char *fname = mono_method_full_name (method, TRUE);
962 if (il_offset != -1) {
963 gchar *source_location = mono_debug_source_location_from_address (method, native_offset, NULL, mono_domain_get ());
966 fprintf (stream, "in %s (at %s)\n", fname, source_location);
968 fprintf (stream, "in %s [IL 0x%lx]\n", fname, (long)il_offset);
970 fprintf (stream, "in %s <0x%lx>\n", fname, (long)native_offset);
973 fprintf (stream, "in <unknown> <%lx>\n", (long)native_offset);
979 * mono_handle_native_sigsegv:
981 * Handle a SIGSEGV received while in native code by printing diagnostic
982 * information and aborting.
985 mono_handle_native_sigsegv (void *ctx)
988 * A SIGSEGV indicates something went very wrong so we can no longer depend
989 * on anything working. So try to print out lots of diagnostics, starting
990 * with ones which have a greater change of working.
994 "=================================================================\n"
995 "Got a SIGSEGV while executing native code. This usually indicates\n"
996 "a fatal error in the mono runtime or one of the native libraries \n"
997 "used by your application.\n"
998 "=================================================================\n"
1001 fprintf (stderr, "Stacktrace:\n\n");
1003 mono_jit_walk_stack (print_stack_frame, TRUE, stderr);
1007 #ifdef HAVE_BACKTRACE_SYMBOLS
1013 fprintf (stderr, "\nNative stacktrace:\n\n");
1015 size = backtrace (array, 256);
1016 names = backtrace_symbols (array, size);
1017 for (i =0; i < size; ++i) {
1018 fprintf (stderr, "\t%s\n", names [i]);
1030 * mono_print_thread_dump:
1032 * Print information about the current thread to stdout.
1035 mono_print_thread_dump (void *sigctx)
1037 MonoThread *thread = mono_thread_current ();
1042 GError *error = NULL;
1045 name = g_utf16_to_utf8 (thread->name, thread->name_len, NULL, NULL, &error);
1047 fprintf (stdout, "\n\"%s\"", name);
1051 fprintf (stdout, "\n\"\"");
1053 fprintf (stdout, " tid=0x%p this=0x%p:\n", (gpointer)(gsize)thread->tid, thread);
1057 mono_arch_sigctx_to_monoctx (sigctx, &ctx);
1059 mono_jit_walk_stack_from_ctx (print_stack_frame, &ctx, TRUE, stdout);
1061 printf ("\t<Stack traces in thread dumps not supported on this platform>\n");