2 * mini-exceptions.c: generic exception support
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
15 #ifdef HAVE_EXECINFO_H
19 #ifndef PLATFORM_WIN32
20 #include <sys/types.h>
24 #include <mono/metadata/appdomain.h>
25 #include <mono/metadata/tabledefs.h>
26 #include <mono/metadata/threads.h>
27 #include <mono/metadata/debug-helpers.h>
28 #include <mono/metadata/exception.h>
29 #include <mono/metadata/gc-internal.h>
30 #include <mono/metadata/mono-debug.h>
35 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
40 /* FreeBSD and NetBSD need SA_STACK and MAP_ANON re-definitions */
41 # if defined(__FreeBSD__) || defined(__NetBSD__)
43 # define SA_STACK SA_ONSTACK
45 # ifndef MAP_ANONYMOUS
46 # define MAP_ANONYMOUS MAP_ANON
50 #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)))
52 #ifndef MONO_ARCH_CONTEXT_DEF
53 #define MONO_ARCH_CONTEXT_DEF
56 #ifndef mono_find_jit_info
58 /* mono_find_jit_info:
60 * This function is used to gather information from @ctx. It return the
61 * MonoJitInfo of the corresponding function, unwinds one stack frame and
62 * stores the resulting context into @new_ctx. It also stores a string
63 * describing the stack location into @trace (if not NULL), and modifies
64 * the @lmf if necessary. @native_offset return the IP offset from the
65 * start of the function or -1 if that info is not available.
68 mono_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
69 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
73 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
85 ji = mono_arch_find_jit_info (domain, jit_tls, res, prev_ji, ctx, new_ctx, NULL, lmf, NULL, &managed2);
87 if (ji == (gpointer)-1)
90 if (managed2 || ji->method->wrapper_type) {
91 const char *real_ip, *start;
94 start = (const char *)ji->code_start;
96 /* ctx->ip points into native code */
97 real_ip = (const char*)MONO_CONTEXT_GET_IP (new_ctx);
99 real_ip = (const char*)ip;
101 if ((real_ip >= start) && (real_ip <= start + ji->code_size))
102 offset = real_ip - start;
107 *native_offset = offset;
110 if (!ji->method->wrapper_type)
114 *trace = mono_debug_print_stack_frame (ji->method, offset, domain);
117 char *fname = mono_method_full_name (res->method, TRUE);
118 *trace = g_strdup_printf ("in (unmanaged) %s", fname);
126 #endif /* mono_find_jit_info */
129 ves_icall_System_Exception_get_trace (MonoException *ex)
131 MonoDomain *domain = mono_domain_get ();
133 MonoArray *ta = ex->trace_ips;
138 /* Exception is not thrown yet */
141 len = mono_array_length (ta);
142 trace_str = g_string_new ("");
143 for (i = 0; i < len; i++) {
145 gpointer ip = mono_array_get (ta, gpointer, i);
147 ji = mono_jit_info_table_find (domain, ip);
149 /* Unmanaged frame */
150 g_string_append_printf (trace_str, "in (unmanaged) %p\n", ip);
155 address = (char *)ip - (char *)ji->code_start;
156 location = mono_debug_print_stack_frame (
157 ji->method, address, ex->object.vtable->domain);
159 g_string_append_printf (trace_str, "%s\n", location);
164 res = mono_string_new (ex->object.vtable->domain, trace_str->str);
165 g_string_free (trace_str, TRUE);
171 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
173 MonoDomain *domain = mono_domain_get ();
175 MonoArray *ta = exc->trace_ips;
176 MonoDebugSourceLocation *location;
180 /* Exception is not thrown yet */
181 return mono_array_new (domain, mono_defaults.stack_frame_class, 0);
184 len = mono_array_length (ta);
186 res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
188 for (i = skip; i < len; i++) {
190 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
191 gpointer ip = mono_array_get (ta, gpointer, i);
193 ji = mono_jit_info_table_find (domain, ip);
195 /* Unmanaged frame */
196 mono_array_setref (res, i, sf);
200 g_assert (ji != NULL);
202 if (ji->method->wrapper_type) {
206 s = mono_method_full_name (ji->method, TRUE);
207 MONO_OBJECT_SETREF (sf, internal_method_name, mono_string_new (domain, s));
211 MONO_OBJECT_SETREF (sf, method, mono_method_get_object (domain, ji->method, NULL));
212 sf->native_offset = (char *)ip - (char *)ji->code_start;
215 * mono_debug_lookup_source_location() returns both the file / line number information
216 * and the IL offset. Note that computing the IL offset is already an expensive
217 * operation, so we shouldn't call this method twice.
219 location = mono_debug_lookup_source_location (ji->method, sf->native_offset, domain);
221 sf->il_offset = location->il_offset;
225 if (need_file_info) {
227 MONO_OBJECT_SETREF (sf, filename, mono_string_new (domain, location->source_file));
228 sf->line = location->row;
229 sf->column = location->column;
231 sf->line = sf->column = 0;
236 mono_debug_free_source_location (location);
237 mono_array_setref (res, i, sf);
245 * @domain: starting appdomain
246 * @jit_tls: JIT data for the thread
247 * @start_ctx: starting state of the stack frame
248 * @func: callback to call for each stack frame
249 * @user_data: data passed to the callback
251 * This function walks the stack of a thread, starting from the state
252 * represented by jit_tls and start_ctx. For each frame the callback
253 * function is called with the relevant info. The walk ends when no more
254 * managed stack frames are found or when the callback returns a TRUE value.
255 * Note that the function can be used to walk the stack of a thread
256 * different from the current.
259 mono_walk_stack (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoContext *start_ctx, MonoStackFrameWalk func, gpointer user_data)
261 MonoLMF *lmf = mono_get_lmf ();
262 MonoJitInfo *ji, rji;
265 MonoContext ctx, new_ctx;
269 while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
271 * FIXME: mono_find_jit_info () will need to be able to return a different
272 * MonoDomain when apddomain transitions are found on the stack.
274 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
275 if (!ji || ji == (gpointer)-1)
278 if (func (domain, &new_ctx, ji, user_data))
285 #ifndef CUSTOM_STACK_WALK
288 mono_jit_walk_stack_from_ctx (MonoStackWalk func, MonoContext *start_ctx, gboolean do_il_offset, gpointer user_data)
290 MonoDomain *domain = mono_domain_get ();
291 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
292 MonoLMF *lmf = mono_get_lmf ();
293 MonoJitInfo *ji, rji;
294 gint native_offset, il_offset;
296 MonoContext ctx, new_ctx;
298 MONO_ARCH_CONTEXT_DEF
300 mono_arch_flush_register_windows ();
303 memcpy (&ctx, start_ctx, sizeof (MonoContext));
305 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
306 MONO_INIT_CONTEXT_FROM_CURRENT (&ctx);
308 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, mono_jit_walk_stack_from_ctx);
312 while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
313 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
316 if (ji == (gpointer)-1)
320 MonoDebugSourceLocation *source;
322 source = mono_debug_lookup_source_location (ji->method, native_offset, domain);
323 il_offset = source ? source->il_offset : -1;
324 mono_debug_free_source_location (source);
328 if (func (ji->method, native_offset, il_offset, managed, user_data))
336 mono_jit_walk_stack (MonoStackWalk func, gboolean do_il_offset, gpointer user_data)
338 mono_jit_walk_stack_from_ctx (func, NULL, do_il_offset, user_data);
342 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
343 MonoReflectionMethod **method,
344 gint32 *iloffset, gint32 *native_offset,
345 MonoString **file, gint32 *line, gint32 *column)
347 MonoDomain *domain = mono_domain_get ();
348 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
349 MonoLMF *lmf = mono_get_lmf ();
350 MonoJitInfo *ji, rji;
351 MonoContext ctx, new_ctx;
352 MonoDebugSourceLocation *location;
354 MONO_ARCH_CONTEXT_DEF;
356 mono_arch_flush_register_windows ();
358 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
359 MONO_INIT_CONTEXT_FROM_CURRENT (&ctx);
361 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_get_frame_info);
365 * FIXME: This is needed because of the LMF stuff which doesn't exist on ia64.
366 * Probably the whole mono_find_jit_info () stuff needs to be fixed so this isn't
367 * needed even on other platforms. This is also true for s390/s390x.
369 #if !defined(__ia64__) && !defined(__s390__) && !defined(__s390x__)
374 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, native_offset, NULL);
378 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
381 /* skip all wrappers ??*/
382 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
383 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE ||
384 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH ||
385 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
386 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
393 *method = mono_method_get_object (domain, ji->method, NULL);
395 location = mono_debug_lookup_source_location (ji->method, *native_offset, domain);
397 *iloffset = location->il_offset;
401 if (need_file_info) {
403 *file = mono_string_new (domain, location->source_file);
404 *line = location->row;
405 *column = location->column;
412 mono_debug_free_source_location (location);
417 #endif /* CUSTOM_STACK_WALK */
421 MonoSecurityFrame *frame;
422 } MonoFrameSecurityInfo;
425 callback_get_first_frame_security_info (MonoDomain *domain, MonoContext *ctx, MonoJitInfo *ji, gpointer data)
427 MonoFrameSecurityInfo *si = (MonoFrameSecurityInfo*) data;
429 /* FIXME: skip all wrappers ?? probably not - case by case testing is required */
430 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
431 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE ||
432 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH ||
433 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
434 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE) {
443 si->frame = mono_declsec_create_frame (domain, ji);
445 /* Stop - we only want the first frame (e.g. LinkDemand and InheritanceDemand) */
450 * ves_icall_System_Security_SecurityFrame_GetSecurityFrame:
451 * @skip: the number of stack frames to skip
453 * This function returns a the security informations of a single stack frame
454 * (after the skipped ones). This is required for [NonCas]LinkDemand[Choice]
455 * and [NonCas]InheritanceDemand[Choice] as only the caller security is
459 ves_icall_System_Security_SecurityFrame_GetSecurityFrame (gint32 skip)
461 MonoDomain *domain = mono_domain_get ();
462 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
463 MonoFrameSecurityInfo si;
466 MONO_ARCH_CONTEXT_DEF
468 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
469 MONO_INIT_CONTEXT_FROM_CURRENT (&ctx);
471 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_System_Security_SecurityFrame_GetSecurityFrame);
476 mono_walk_stack (domain, jit_tls, &ctx, callback_get_first_frame_security_info, (gpointer)&si);
478 return (si.skips == 0) ? si.frame : NULL;
490 grow_array (MonoSecurityStack *stack)
492 MonoDomain *domain = mono_domain_get ();
493 guint32 newsize = (stack->maximum << 1);
494 MonoArray *newstack = mono_array_new (domain, mono_defaults.runtimesecurityframe_class, newsize);
496 for (i=0; i < stack->maximum; i++) {
497 gpointer frame = mono_array_get (stack->stack, gpointer, i);
498 mono_array_setref (newstack, i, frame);
500 stack->maximum = newsize;
501 stack->stack = newstack;
505 callback_get_stack_frames_security_info (MonoDomain *domain, MonoContext *ctx, MonoJitInfo *ji, gpointer data)
507 MonoSecurityStack *ss = (MonoSecurityStack*) data;
509 /* FIXME: skip all wrappers ?? probably not - case by case testing is required */
510 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
511 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE ||
512 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH ||
513 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
514 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE) {
523 if (ss->count == ss->maximum)
526 mono_array_setref (ss->stack, ss->count++, mono_declsec_create_frame (domain, ji));
528 /* continue down the stack */
533 glist_to_array (GList *list, MonoClass *eclass)
535 MonoDomain *domain = mono_domain_get ();
542 len = g_list_length (list);
543 res = mono_array_new (domain, eclass, len);
545 for (i = 0; list; list = list->next, i++)
546 mono_array_set (res, gpointer, i, list->data);
552 * ves_icall_System_Security_SecurityFrame_GetSecurityStack:
553 * @skip: the number of stack frames to skip
555 * This function returns an managed array of containing the security
556 * informations for each frame (after the skipped ones). This is used for
557 * [NonCas]Demand[Choice] where the complete evaluation of the stack is
561 ves_icall_System_Security_SecurityFrame_GetSecurityStack (gint32 skip)
563 MonoDomain *domain = mono_domain_get ();
564 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
565 MonoSecurityStack ss;
568 MONO_ARCH_CONTEXT_DEF
570 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
571 MONO_INIT_CONTEXT_FROM_CURRENT (&ctx);
573 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_System_Security_SecurityFrame_GetSecurityStack);
578 ss.maximum = MONO_CAS_INITIAL_STACK_SIZE;
579 ss.stack = mono_array_new (domain, mono_defaults.runtimesecurityframe_class, ss.maximum);
580 mono_walk_stack (domain, jit_tls, &ctx, callback_get_stack_frames_security_info, (gpointer)&ss);
581 /* g_warning ("STACK RESULT: %d out of %d", ss.count, ss.maximum); */
585 #ifndef CUSTOM_EXCEPTION_HANDLING
588 * mono_handle_exception_internal:
589 * @ctx: saved processor state
590 * @obj: the exception object
591 * @test_only: only test if the exception is caught, but dont call handlers
592 * @out_filter_idx: out parameter. if test_only is true, set to the index of
593 * the first filter clause which caught the exception.
596 mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer original_ip, gboolean test_only, gint32 *out_filter_idx)
598 MonoDomain *domain = mono_domain_get ();
599 MonoJitInfo *ji, rji;
600 static int (*call_filter) (MonoContext *, gpointer) = NULL;
601 static void (*restore_context) (void *);
602 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
603 MonoLMF *lmf = mono_get_lmf ();
604 MonoArray *initial_trace_ips = NULL;
605 GList *trace_ips = NULL;
606 MonoException *mono_ex;
607 gboolean stack_overflow = FALSE;
608 MonoContext initial_ctx;
610 gboolean gc_disabled = FALSE;
611 gboolean has_dynamic_methods = FALSE;
612 gint32 filter_idx, first_filter_idx;
615 * This function might execute on an alternate signal stack, and Boehm GC
617 * Also, since the altstack is small, stack space intensive operations like
618 * JIT compilation should be avoided.
620 if (IS_ON_SIGALTSTACK (jit_tls)) {
622 * FIXME: disabling/enabling GC while already on a signal stack might
623 * not be safe either.
625 /* Have to reenable it later */
630 g_assert (ctx != NULL);
632 MonoException *ex = mono_get_exception_null_reference ();
633 MONO_OBJECT_SETREF (ex, message, mono_string_new (domain, "Object reference not set to an instance of an object"));
634 obj = (MonoObject *)ex;
638 * Allocate a new exception object instead of the preconstructed ones.
639 * We can't do this in sigsegv_signal_handler, since GC is not yet
642 if (obj == domain->stack_overflow_ex) {
643 obj = mono_get_exception_stack_overflow ();
644 stack_overflow = TRUE;
646 else if (obj == domain->null_reference_ex) {
647 obj = mono_get_exception_null_reference ();
650 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
651 mono_ex = (MonoException*)obj;
652 initial_trace_ips = mono_ex->trace_ips;
658 call_filter = mono_arch_get_call_filter ();
660 if (!restore_context)
661 restore_context = mono_arch_get_restore_context ();
663 g_assert (jit_tls->end_of_stack);
664 g_assert (jit_tls->abort_func);
667 MonoContext ctx_cp = *ctx;
668 if (mono_trace_is_enabled ())
669 g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
670 if (!mono_handle_exception_internal (&ctx_cp, obj, original_ip, TRUE, &first_filter_idx)) {
671 if (mono_break_on_exc)
673 mono_unhandled_exception (obj);
675 if (mono_debugger_unhandled_exception (original_ip, MONO_CONTEXT_GET_SP (ctx), obj)) {
677 * If this returns true, then we're running inside the
678 * Mono Debugger and the debugger wants us to restore the
679 * context and continue (normally, the debugger inserts
680 * a breakpoint on the `original_ip', so it regains control
681 * immediately after restoring the context).
683 MONO_CONTEXT_SET_IP (ctx, original_ip);
684 restore_context (ctx);
685 g_assert_not_reached ();
691 *out_filter_idx = -1;
694 memset (&rji, 0, sizeof (rji));
700 ji = mono_find_jit_info (domain, jit_tls, &rji, &rji, ctx, &new_ctx,
701 NULL, &lmf, NULL, NULL);
703 g_warning ("Exception inside function without unwind info");
704 g_assert_not_reached ();
707 if (ji != (gpointer)-1) {
709 //printf ("M: %s %d %d.\n", mono_method_full_name (ji->method, TRUE), frame_count, test_only);
711 if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
713 * Avoid overwriting the stack trace if the exception is
714 * rethrown. Also avoid giant stack traces during a stack
717 if (!initial_trace_ips && (frame_count < 1000)) {
718 trace_ips = g_list_prepend (trace_ips, MONO_CONTEXT_GET_IP (ctx));
722 if (ji->method->dynamic)
723 has_dynamic_methods = TRUE;
726 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
728 free_stack = 0xffffff;
731 * During stack overflow, wait till the unwinding frees some stack
732 * space before running handlers/finalizers.
734 if ((free_stack > (64 * 1024)) && ji->num_clauses) {
737 for (i = 0; i < ji->num_clauses; i++) {
738 MonoJitExceptionInfo *ei = &ji->clauses [i];
739 gboolean filtered = FALSE;
742 if (ei->try_start < MONO_CONTEXT_GET_IP (ctx) &&
744 if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
746 MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) {
749 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)) {
750 /* store the exception object in bp + ei->exvar_offset */
751 *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = obj;
754 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
755 // mono_debugger_handle_exception (ei->data.filter, MONO_CONTEXT_GET_SP (ctx), obj);
757 filtered = call_filter (ctx, ei->data.filter);
758 if (filtered && out_filter_idx)
759 *out_filter_idx = filter_idx;
763 * Filter clauses should only be run in the
764 * first pass of exception handling.
766 filtered = (filter_idx == first_filter_idx);
771 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
772 mono_object_isinst (obj, ei->data.catch_class)) || filtered) {
774 if (mono_ex && !initial_trace_ips) {
775 trace_ips = g_list_reverse (trace_ips);
776 MONO_OBJECT_SETREF (mono_ex, trace_ips, glist_to_array (trace_ips, mono_defaults.int_class));
777 if (has_dynamic_methods)
778 /* These methods could go away anytime, so compute the stack trace now */
779 MONO_OBJECT_SETREF (mono_ex, stack_trace, ves_icall_System_Exception_get_trace (mono_ex));
781 g_list_free (trace_ips);
787 if (mono_trace_is_enabled () && mono_trace_eval (ji->method))
788 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
789 mono_debugger_handle_exception (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), obj);
790 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
791 *(mono_get_lmf_addr ()) = lmf;
797 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
798 MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
799 (ei->flags == MONO_EXCEPTION_CLAUSE_FAULT)) {
800 if (mono_trace_is_enabled () && mono_trace_eval (ji->method))
801 g_print ("EXCEPTION: fault clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
802 mono_debugger_handle_exception (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), obj);
803 call_filter (ctx, ei->handler_start);
805 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
806 MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
807 (ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY)) {
808 if (mono_trace_is_enabled () && mono_trace_eval (ji->method))
809 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
810 mono_debugger_handle_exception (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), obj);
811 call_filter (ctx, ei->handler_start);
821 if (ji == (gpointer)-1) {
826 *(mono_get_lmf_addr ()) = lmf;
828 if (IS_ON_SIGALTSTACK (jit_tls)) {
829 /* Switch back to normal stack */
830 if (stack_overflow) {
831 /* Free up some stack space */
832 MONO_CONTEXT_SET_SP (&initial_ctx, (gssize)(MONO_CONTEXT_GET_SP (&initial_ctx)) + (64 * 1024));
833 g_assert ((gssize)MONO_CONTEXT_GET_SP (&initial_ctx) < (gssize)jit_tls->end_of_stack);
835 #ifdef MONO_CONTEXT_SET_FUNC
836 /* jit_tls->abort_func is a function descriptor on ia64 */
837 MONO_CONTEXT_SET_FUNC (&initial_ctx, (gssize)jit_tls->abort_func);
839 MONO_CONTEXT_SET_IP (&initial_ctx, (gssize)jit_tls->abort_func);
841 restore_context (&initial_ctx);
844 jit_tls->abort_func (obj);
845 g_assert_not_reached ();
847 if (mono_ex && !initial_trace_ips) {
848 trace_ips = g_list_reverse (trace_ips);
849 MONO_OBJECT_SETREF (mono_ex, trace_ips, glist_to_array (trace_ips, mono_defaults.int_class));
850 if (has_dynamic_methods)
851 /* These methods could go away anytime, so compute the stack trace now */
852 MONO_OBJECT_SETREF (mono_ex, stack_trace, ves_icall_System_Exception_get_trace (mono_ex));
854 g_list_free (trace_ips);
860 g_assert_not_reached ();
864 * mono_debugger_run_finally:
865 * @start_ctx: saved processor state
867 * This method is called by the Mono Debugger to call all `finally' clauses of the
868 * current stack frame. It's used when the user issues a `return' command to make
869 * the current stack frame return. After returning from this method, the debugger
870 * unwinds the stack one frame and gives control back to the user.
872 * NOTE: This method is only used when running inside the Mono Debugger.
875 mono_debugger_run_finally (MonoContext *start_ctx)
877 static int (*call_filter) (MonoContext *, gpointer) = NULL;
878 MonoDomain *domain = mono_domain_get ();
879 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
880 MonoLMF *lmf = mono_get_lmf ();
881 MonoContext ctx, new_ctx;
882 MonoJitInfo *ji, rji;
887 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, NULL, NULL);
888 if (!ji || ji == (gpointer)-1)
892 call_filter = mono_arch_get_call_filter ();
894 for (i = 0; i < ji->num_clauses; i++) {
895 MonoJitExceptionInfo *ei = &ji->clauses [i];
897 if ((ei->try_start <= MONO_CONTEXT_GET_IP (&ctx)) &&
898 (MONO_CONTEXT_GET_IP (&ctx) < ei->try_end) &&
899 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
900 call_filter (&ctx, ei->handler_start);
906 * mono_handle_exception:
907 * @ctx: saved processor state
908 * @obj: the exception object
909 * @test_only: only test if the exception is caught, but dont call handlers
912 mono_handle_exception (MonoContext *ctx, gpointer obj, gpointer original_ip, gboolean test_only)
914 return mono_handle_exception_internal (ctx, obj, original_ip, test_only, NULL);
917 #endif /* CUSTOM_EXCEPTION_HANDLING */
919 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
922 mono_setup_altstack (MonoJitTlsData *tls)
924 pthread_t self = pthread_self();
927 struct sigaltstack sa;
928 guint8 *staddr = NULL;
929 guint8 *current = (guint8*)&staddr;
931 if (mono_running_on_valgrind ())
934 /* Determine stack boundaries */
935 pthread_attr_init( &attr );
936 #ifdef HAVE_PTHREAD_GETATTR_NP
937 pthread_getattr_np( self, &attr );
939 #ifdef HAVE_PTHREAD_ATTR_GET_NP
940 pthread_attr_get_np( self, &attr );
942 pthread_attr_getstacksize( &attr, &stsize );
944 #error "Not implemented"
949 pthread_attr_getstack( &attr, (void**)&staddr, &stsize );
952 pthread_attr_destroy (&attr);
956 g_assert ((current > staddr) && (current < staddr + stsize));
958 tls->end_of_stack = staddr + stsize;
961 * threads created by nptl does not seem to have a guard page, and
962 * since the main thread is not created by us, we can't even set one.
963 * Increasing stsize fools the SIGSEGV signal handler into thinking this
964 * is a stack overflow exception.
966 tls->stack_size = stsize + getpagesize ();
968 /* Setup an alternate signal stack */
969 tls->signal_stack = mmap (0, MONO_ARCH_SIGNAL_STACK_SIZE, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
970 tls->signal_stack_size = MONO_ARCH_SIGNAL_STACK_SIZE;
972 g_assert (tls->signal_stack);
974 sa.ss_sp = tls->signal_stack;
975 sa.ss_size = MONO_ARCH_SIGNAL_STACK_SIZE;
976 sa.ss_flags = SS_ONSTACK;
977 sigaltstack (&sa, NULL);
981 mono_free_altstack (MonoJitTlsData *tls)
983 struct sigaltstack sa;
986 sa.ss_sp = tls->signal_stack;
987 sa.ss_size = MONO_ARCH_SIGNAL_STACK_SIZE;
988 sa.ss_flags = SS_DISABLE;
989 err = sigaltstack (&sa, NULL);
992 if (tls->signal_stack)
993 munmap (tls->signal_stack, MONO_ARCH_SIGNAL_STACK_SIZE);
996 #endif /* MONO_ARCH_SIGSEGV_ON_ALTSTACK */
999 print_stack_frame (MonoMethod *method, gint32 native_offset, gint32 il_offset, gboolean managed, gpointer data)
1001 FILE *stream = (FILE*)data;
1004 gchar *location = mono_debug_print_stack_frame (method, native_offset, mono_domain_get ());
1005 fprintf (stream, " %s\n", location);
1008 fprintf (stream, " at <unknown> <0x%05x>\n", native_offset);
1013 static gboolean handling_sigsegv = FALSE;
1016 * mono_handle_native_sigsegv:
1018 * Handle a SIGSEGV received while in native code by printing diagnostic
1019 * information and aborting.
1022 mono_handle_native_sigsegv (int signal, void *ctx)
1024 #ifndef PLATFORM_WIN32
1025 struct sigaction sa;
1027 const char *signal_str = (signal == SIGSEGV) ? "SIGSEGV" : "SIGABRT";
1029 if (handling_sigsegv)
1032 /* To prevent infinite loops when the stack walk causes a crash */
1033 handling_sigsegv = TRUE;
1035 fprintf (stderr, "Stacktrace:\n\n");
1037 mono_jit_walk_stack (print_stack_frame, TRUE, stderr);
1041 #ifdef HAVE_BACKTRACE_SYMBOLS
1050 fprintf (stderr, "\nNative stacktrace:\n\n");
1052 size = backtrace (array, 256);
1053 names = backtrace_symbols (array, size);
1054 for (i =0; i < size; ++i) {
1055 fprintf (stderr, "\t%s\n", names [i]);
1061 /* Try to get more meaningful information using gdb */
1063 #ifndef PLATFORM_WIN32
1064 sprintf (cmd, "gdb --ex 'attach %ld' --ex 'info threads' --ex 'thread apply all bt' --batch", (long)getpid ());
1066 int res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
1069 fprintf (stderr, "\nDebug info from gdb:\n\n");
1070 fprintf (stderr, "%s\n", out);
1075 * A SIGSEGV indicates something went very wrong so we can no longer depend
1076 * on anything working. So try to print out lots of diagnostics, starting
1077 * with ones which have a greater chance of working.
1081 "=================================================================\n"
1082 "Got a %s while executing native code. This usually indicates\n"
1083 "a fatal error in the mono runtime or one of the native libraries \n"
1084 "used by your application.\n"
1085 "=================================================================\n"
1091 #ifndef PLATFORM_WIN32
1093 /* Remove our SIGABRT handler */
1094 sa.sa_handler = SIG_DFL;
1095 sigemptyset (&sa.sa_mask);
1098 g_assert (sigaction (SIGABRT, &sa, NULL) != -1);
1106 * mono_print_thread_dump:
1108 * Print information about the current thread to stdout.
1111 mono_print_thread_dump (void *sigctx)
1113 MonoThread *thread = mono_thread_current ();
1118 GError *error = NULL;
1121 name = g_utf16_to_utf8 (thread->name, thread->name_len, NULL, NULL, &error);
1123 fprintf (stdout, "\n\"%s\"", name);
1127 fprintf (stdout, "\n\"\"");
1129 fprintf (stdout, " tid=0x%p this=0x%p:\n", (gpointer)(gsize)thread->tid, thread);
1133 mono_arch_sigctx_to_monoctx (sigctx, &ctx);
1135 mono_jit_walk_stack_from_ctx (print_stack_frame, &ctx, TRUE, stdout);
1137 printf ("\t<Stack traces in thread dumps not supported on this platform>\n");