2 * mini-exceptions.c: generic exception support
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Mono Team (mono-list@lists.ximian.com)
8 * Copyright 2001-2003 Ximian, Inc.
9 * Copyright 2003-2008 Novell, Inc.
10 * Copyright 2011 Xamarin Inc (http://www.xamarin.com).
18 #ifdef HAVE_EXECINFO_H
22 #ifdef HAVE_SYS_TYPES_H
23 #include <sys/types.h>
26 #ifdef HAVE_SYS_WAIT_H
34 #ifdef HAVE_SYS_SYSCALL_H
35 #include <sys/syscall.h>
38 #include <mono/metadata/appdomain.h>
39 #include <mono/metadata/tabledefs.h>
40 #include <mono/metadata/threads.h>
41 #include <mono/metadata/threads-types.h>
42 #include <mono/metadata/debug-helpers.h>
43 #include <mono/metadata/exception.h>
44 #include <mono/metadata/gc-internal.h>
45 #include <mono/metadata/mono-debug.h>
46 #include <mono/metadata/profiler.h>
47 #include <mono/metadata/mono-endian.h>
48 #include <mono/metadata/environment.h>
49 #include <mono/utils/mono-mmap.h>
50 #include <mono/utils/mono-logger-internal.h>
53 #include "debug-mini.h"
55 #include "debugger-agent.h"
57 #ifndef MONO_ARCH_CONTEXT_DEF
58 #define MONO_ARCH_CONTEXT_DEF
61 static gpointer restore_context_func, call_filter_func;
62 static gpointer throw_exception_func, rethrow_exception_func;
63 static gpointer throw_corlib_exception_func;
65 static gpointer try_more_restore_tramp = NULL;
66 static gpointer restore_stack_protection_tramp = NULL;
68 static MonoUnhandledExceptionFunc unhandled_exception_hook = NULL;
69 static gpointer unhandled_exception_hook_data = NULL;
71 static void try_more_restore (void);
72 static void restore_stack_protection (void);
73 static void mono_walk_stack_full (MonoJitStackWalk func, MonoContext *start_ctx, MonoDomain *domain, MonoJitTlsData *jit_tls, MonoLMF *lmf, MonoUnwindOptions unwind_options, gpointer user_data);
74 static void mono_raise_exception_with_ctx (MonoException *exc, MonoContext *ctx);
75 static void mono_runtime_walk_stack_with_ctx (MonoJitStackWalk func, MonoContext *start_ctx, MonoUnwindOptions unwind_options, void *user_data);
78 mono_exceptions_init (void)
80 MonoRuntimeExceptionHandlingCallbacks cbs;
82 restore_context_func = mono_aot_get_trampoline ("restore_context");
83 call_filter_func = mono_aot_get_trampoline ("call_filter");
84 throw_exception_func = mono_aot_get_trampoline ("throw_exception");
85 rethrow_exception_func = mono_aot_get_trampoline ("rethrow_exception");
89 restore_context_func = mono_arch_get_restore_context (&info, FALSE);
91 mono_save_trampoline_xdebug_info (info);
92 mono_tramp_info_free (info);
94 call_filter_func = mono_arch_get_call_filter (&info, FALSE);
96 mono_save_trampoline_xdebug_info (info);
97 mono_tramp_info_free (info);
99 throw_exception_func = mono_arch_get_throw_exception (&info, FALSE);
101 mono_save_trampoline_xdebug_info (info);
102 mono_tramp_info_free (info);
104 rethrow_exception_func = mono_arch_get_rethrow_exception (&info, FALSE);
106 mono_save_trampoline_xdebug_info (info);
107 mono_tramp_info_free (info);
110 #ifdef MONO_ARCH_HAVE_RESTORE_STACK_SUPPORT
111 try_more_restore_tramp = mono_create_specific_trampoline (try_more_restore, MONO_TRAMPOLINE_RESTORE_STACK_PROT, mono_domain_get (), NULL);
112 restore_stack_protection_tramp = mono_create_specific_trampoline (restore_stack_protection, MONO_TRAMPOLINE_RESTORE_STACK_PROT, mono_domain_get (), NULL);
115 #ifdef MONO_ARCH_HAVE_EXCEPTIONS_INIT
116 mono_arch_exceptions_init ();
118 cbs.mono_walk_stack_with_ctx = mono_runtime_walk_stack_with_ctx;
119 cbs.mono_walk_stack_with_state = mono_walk_stack_with_state;
120 cbs.mono_raise_exception = mono_get_throw_exception ();
121 cbs.mono_raise_exception_with_ctx = mono_raise_exception_with_ctx;
122 cbs.mono_exception_walk_trace = mono_exception_walk_trace;
123 cbs.mono_install_handler_block_guard = mono_install_handler_block_guard;
124 mono_install_eh_callbacks (&cbs);
128 mono_get_throw_exception (void)
130 g_assert (throw_exception_func);
131 return throw_exception_func;
135 mono_get_rethrow_exception (void)
137 g_assert (rethrow_exception_func);
138 return rethrow_exception_func;
142 mono_get_call_filter (void)
144 g_assert (call_filter_func);
145 return call_filter_func;
149 mono_get_restore_context (void)
151 g_assert (restore_context_func);
152 return restore_context_func;
156 mono_get_throw_corlib_exception (void)
158 gpointer code = NULL;
161 /* This depends on corlib classes so cannot be inited in mono_exceptions_init () */
162 if (throw_corlib_exception_func)
163 return throw_corlib_exception_func;
166 code = mono_aot_get_trampoline ("throw_corlib_exception");
168 code = mono_arch_get_throw_corlib_exception (&info, FALSE);
170 mono_save_trampoline_xdebug_info (info);
171 mono_tramp_info_free (info);
175 mono_memory_barrier ();
177 throw_corlib_exception_func = code;
179 return throw_corlib_exception_func;
183 is_address_protected (MonoJitInfo *ji, MonoJitExceptionInfo *ei, gpointer ip)
185 MonoTryBlockHoleTableJitInfo *table;
190 if (ei->try_start > ip || ip >= ei->try_end)
193 if (!ji->has_try_block_holes)
196 table = mono_jit_info_get_try_block_hole_table_info (ji);
197 offset = (guint32)((char*)ip - (char*)ji->code_start);
198 clause = (guint16)(ei - ji->clauses);
199 g_assert (clause < ji->num_clauses);
201 for (i = 0; i < table->num_holes; ++i) {
202 MonoTryBlockHoleJitInfo *hole = &table->holes [i];
203 if (hole->clause == clause && hole->offset <= offset && hole->offset + hole->length > offset)
212 * Translate between the mono_arch_find_jit_info function and the old API.
215 find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
216 MonoContext *new_ctx, MonoLMF **lmf, gboolean *managed)
218 StackFrameInfo frame;
221 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
223 /* Avoid costly table lookup during stack overflow */
224 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
227 ji = mini_jit_info_table_find (domain, ip, NULL);
232 err = mono_arch_find_jit_info (domain, jit_tls, ji, ctx, new_ctx, lmf, NULL, &frame);
236 /* Convert between the new and the old APIs */
237 switch (frame.type) {
238 case FRAME_TYPE_MANAGED:
242 case FRAME_TYPE_MANAGED_TO_NATIVE:
246 memset (res, 0, sizeof (MonoJitInfo));
247 res->d.method = frame.method;
250 case FRAME_TYPE_DEBUGGER_INVOKE: {
254 * The normal exception handling code can't handle this frame, so just
257 ji = find_jit_info (domain, jit_tls, res, NULL, new_ctx, &tmp_ctx, lmf, managed);
258 memcpy (new_ctx, &tmp_ctx, sizeof (MonoContext));
262 g_assert_not_reached ();
267 /* mono_find_jit_info:
269 * This function is used to gather information from @ctx. It return the
270 * MonoJitInfo of the corresponding function, unwinds one stack frame and
271 * stores the resulting context into @new_ctx. It also stores a string
272 * describing the stack location into @trace (if not NULL), and modifies
273 * the @lmf if necessary. @native_offset return the IP offset from the
274 * start of the function or -1 if that info is not available.
277 mono_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
278 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
282 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
284 MonoMethod *method = NULL;
295 ji = find_jit_info (domain, jit_tls, res, prev_ji, ctx, new_ctx, lmf, &managed2);
297 if (ji == (gpointer)-1)
301 method = jinfo_get_method (ji);
303 if (managed2 || (ji && method->wrapper_type)) {
304 const char *real_ip, *start;
307 start = (const char *)ji->code_start;
309 /* ctx->ip points into native code */
310 real_ip = (const char*)MONO_CONTEXT_GET_IP (new_ctx);
312 real_ip = (const char*)ip;
314 if ((real_ip >= start) && (real_ip <= start + ji->code_size))
315 offset = real_ip - start;
320 *native_offset = offset;
323 if (!method->wrapper_type || method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
327 *trace = mono_debug_print_stack_frame (method, offset, domain);
330 char *fname = mono_method_full_name (jinfo_get_method (res), TRUE);
331 *trace = g_strdup_printf ("in (unmanaged) %s", fname);
340 * mono_find_jit_info_ext:
342 * A version of mono_find_jit_info which returns all data in the StackFrameInfo
344 * A note about frames of type FRAME_TYPE_MANAGED_TO_NATIVE:
345 * - These frames are used to mark managed-to-native transitions, so CTX will refer to native
346 * code, and new_ctx will refer to the last managed frame. The caller should unwind once more
347 * to obtain the last managed frame.
348 * If SAVE_LOCATIONS is not NULL, it should point to an array of size MONO_MAX_IREGS.
349 * On return, it will be filled with the locations where callee saved registers are saved
350 * by the current frame. This is returned outside of StackFrameInfo because it can be
351 * quite large on some platforms.
352 * If ASYNC true, this function will be async safe, but some fields of frame and frame->ji will
356 mono_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
357 MonoJitInfo *prev_ji, MonoContext *ctx,
358 MonoContext *new_ctx, char **trace, MonoLMF **lmf,
359 mgreg_t **save_locations,
360 StackFrameInfo *frame)
363 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
365 MonoDomain *target_domain = domain;
366 MonoMethod *method = NULL;
367 gboolean async = mono_thread_info_is_async_context ();
372 /* Avoid costly table lookup during stack overflow */
373 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
376 ji = mini_jit_info_table_find (domain, ip, &target_domain);
379 target_domain = domain;
382 memset (save_locations, 0, MONO_MAX_IREGS * sizeof (mgreg_t*));
384 err = mono_arch_find_jit_info (target_domain, jit_tls, ji, ctx, new_ctx, lmf, save_locations, frame);
388 if (frame->ji && !frame->ji->async)
389 method = jinfo_get_method (frame->ji);
391 if (frame->type == FRAME_TYPE_MANAGED && method) {
392 if (!method->wrapper_type || method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
393 frame->managed = TRUE;
396 if (frame->type == FRAME_TYPE_MANAGED_TO_NATIVE) {
398 * This type of frame is just a marker, the caller should unwind once more to get the
399 * last managed frame.
402 frame->method = NULL;
405 frame->native_offset = -1;
406 frame->domain = target_domain;
407 frame->async_context = async;
411 if (frame->type == FRAME_TYPE_MANAGED)
412 frame->method = method;
414 if (ji && (frame->managed || (method && method->wrapper_type))) {
415 const char *real_ip, *start;
417 start = (const char *)ji->code_start;
419 /* ctx->ip points into native code */
420 real_ip = (const char*)MONO_CONTEXT_GET_IP (new_ctx);
422 real_ip = (const char*)ip;
424 if ((real_ip >= start) && (real_ip <= start + ji->code_size))
425 frame->native_offset = real_ip - start;
427 frame->native_offset = -1;
430 *trace = mono_debug_print_stack_frame (method, frame->native_offset, domain);
432 if (trace && frame->method) {
433 char *fname = mono_method_full_name (frame->method, TRUE);
434 *trace = g_strdup_printf ("in (unmanaged) %s", fname);
443 get_generic_info_from_stack_frame (MonoJitInfo *ji, MonoContext *ctx)
445 MonoGenericJitInfo *gi;
449 if (!ji->has_generic_jit_info)
451 gi = mono_jit_info_get_generic_jit_info (ji);
457 * Search location list if available, it contains the precise location of the
458 * argument for every pc offset, even if the method was interrupted while it was in
462 int offset = (mgreg_t)MONO_CONTEXT_GET_IP (ctx) - (mgreg_t)ji->code_start;
465 for (i = 0; i < gi->nlocs; ++i) {
466 MonoDwarfLocListEntry *entry = &gi->locations [i];
468 if (offset >= entry->from && (offset < entry->to || entry->to == 0)) {
470 info = (gpointer)mono_arch_context_get_int_reg (ctx, entry->reg);
472 info = *(gpointer*)(gpointer)((char*)mono_arch_context_get_int_reg (ctx, entry->reg) + entry->offset);
476 g_assert (i < gi->nlocs);
479 info = (gpointer)mono_arch_context_get_int_reg (ctx, gi->this_reg);
481 info = *(gpointer*)(gpointer)((char*)mono_arch_context_get_int_reg (ctx, gi->this_reg) +
485 method = jinfo_get_method (ji);
486 if (mono_method_get_context (method)->method_inst) {
488 } else if ((method->flags & METHOD_ATTRIBUTE_STATIC) || method->klass->valuetype) {
491 /* Avoid returning a managed object */
492 MonoObject *this_obj = info;
494 return this_obj->vtable->klass;
498 static MonoGenericContext
499 get_generic_context_from_stack_frame (MonoJitInfo *ji, gpointer generic_info)
501 MonoGenericContext context = { NULL, NULL };
502 MonoClass *class, *method_container_class;
505 g_assert (generic_info);
507 method = jinfo_get_method (ji);
508 g_assert (method->is_inflated);
509 if (mono_method_get_context (method)->method_inst) {
510 MonoMethodRuntimeGenericContext *mrgctx = generic_info;
512 class = mrgctx->class_vtable->klass;
513 context.method_inst = mrgctx->method_inst;
514 g_assert (context.method_inst);
515 } else if ((method->flags & METHOD_ATTRIBUTE_STATIC) || method->klass->valuetype) {
516 MonoVTable *vtable = generic_info;
518 class = vtable->klass;
520 class = generic_info;
523 //g_assert (!method->klass->generic_container);
524 if (method->klass->generic_class)
525 method_container_class = method->klass->generic_class->container_class;
527 method_container_class = method->klass;
529 /* class might refer to a subclass of method's class */
530 while (!(class == method->klass || (class->generic_class && class->generic_class->container_class == method_container_class))) {
531 class = class->parent;
535 if (class->generic_class || class->generic_container)
536 context.class_inst = mini_class_get_context (class)->class_inst;
538 if (class->generic_class)
539 g_assert (mono_class_has_parent_and_ignore_generics (class->generic_class->container_class, method_container_class));
541 g_assert (mono_class_has_parent_and_ignore_generics (class, method_container_class));
547 get_method_from_stack_frame (MonoJitInfo *ji, gpointer generic_info)
549 MonoGenericContext context;
552 if (!ji->has_generic_jit_info || !mono_jit_info_get_generic_jit_info (ji)->has_this)
553 return jinfo_get_method (ji);
554 context = get_generic_context_from_stack_frame (ji, generic_info);
556 method = jinfo_get_method (ji);
557 method = mono_method_get_declaring_generic_method (method);
558 method = mono_class_inflate_generic_method (method, &context);
564 * mono_exception_walk_native_trace:
565 * @ex: The exception object whose frames should be walked
566 * @func: callback to call for each stack frame
567 * @user_data: data passed to the callback
569 * This function walks the stacktrace of an exception. For
570 * each frame the callback function is called with the relevant info.
571 * The walk ends when no more stack frames are found or when the callback
572 * returns a TRUE value.
576 mono_exception_walk_trace (MonoException *ex, MonoExceptionFrameWalk func, gpointer user_data)
578 MonoDomain *domain = mono_domain_get ();
579 MonoArray *ta = ex->trace_ips;
585 len = mono_array_length (ta) >> 1;
586 for (i = 0; i < len; i++) {
587 gpointer ip = mono_array_get (ta, gpointer, i * 2 + 0);
588 gpointer generic_info = mono_array_get (ta, gpointer, i * 2 + 1);
589 MonoJitInfo *ji = mono_jit_info_table_find (domain, ip);
592 if (func (NULL, ip, 0, FALSE, user_data))
595 MonoMethod *method = get_method_from_stack_frame (ji, generic_info);
596 if (func (method, ji->code_start, (char *) ip - (char *) ji->code_start, TRUE, user_data))
605 ves_icall_System_Exception_get_trace (MonoException *ex)
607 MonoDomain *domain = mono_domain_get ();
609 MonoArray *ta = ex->trace_ips;
614 /* Exception is not thrown yet */
617 len = mono_array_length (ta) >> 1;
618 trace_str = g_string_new ("");
619 for (i = 0; i < len; i++) {
621 gpointer ip = mono_array_get (ta, gpointer, i * 2 + 0);
622 gpointer generic_info = mono_array_get (ta, gpointer, i * 2 + 1);
624 ji = mono_jit_info_table_find (domain, ip);
626 /* Unmanaged frame */
627 g_string_append_printf (trace_str, "in (unmanaged) %p\n", ip);
631 MonoMethod *method = get_method_from_stack_frame (ji, generic_info);
633 address = (char *)ip - (char *)ji->code_start;
634 location = mono_debug_print_stack_frame (
635 method, address, ex->object.vtable->domain);
637 g_string_append_printf (trace_str, "%s\n", location);
642 res = mono_string_new (ex->object.vtable->domain, trace_str->str);
643 g_string_free (trace_str, TRUE);
649 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
651 MonoDomain *domain = mono_domain_get ();
653 MonoArray *ta = exc->trace_ips;
654 MonoDebugSourceLocation *location;
658 /* Exception is not thrown yet */
659 return mono_array_new (domain, mono_defaults.stack_frame_class, 0);
662 len = mono_array_length (ta) >> 1;
664 res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
666 for (i = skip; i < len; i++) {
668 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
669 gpointer ip = mono_array_get (ta, gpointer, i * 2 + 0);
670 gpointer generic_info = mono_array_get (ta, gpointer, i * 2 + 1);
673 ji = mono_jit_info_table_find (domain, ip);
675 /* Unmanaged frame */
676 mono_array_setref (res, i, sf);
680 g_assert (ji != NULL);
682 method = get_method_from_stack_frame (ji, generic_info);
683 if (jinfo_get_method (ji)->wrapper_type) {
687 s = mono_method_full_name (method, TRUE);
688 MONO_OBJECT_SETREF (sf, internal_method_name, mono_string_new (domain, s));
692 MONO_OBJECT_SETREF (sf, method, mono_method_get_object (domain, method, NULL));
693 sf->native_offset = (char *)ip - (char *)ji->code_start;
696 * mono_debug_lookup_source_location() returns both the file / line number information
697 * and the IL offset. Note that computing the IL offset is already an expensive
698 * operation, so we shouldn't call this method twice.
700 location = mono_debug_lookup_source_location (jinfo_get_method (ji), sf->native_offset, domain);
702 sf->il_offset = location->il_offset;
706 if (need_file_info) {
707 if (location && location->source_file) {
708 MONO_OBJECT_SETREF (sf, filename, mono_string_new (domain, location->source_file));
709 sf->line = location->row;
710 sf->column = location->column;
712 sf->line = sf->column = 0;
717 mono_debug_free_source_location (location);
718 mono_array_setref (res, i, sf);
725 mono_runtime_walk_stack_with_ctx (MonoJitStackWalk func, MonoContext *start_ctx, MonoUnwindOptions unwind_options, void *user_data)
728 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
729 if (jit_tls && jit_tls->orig_ex_ctx_set)
730 start_ctx = &jit_tls->orig_ex_ctx;
732 mono_walk_stack_with_ctx (func, start_ctx, unwind_options, user_data);
735 * mono_walk_stack_with_ctx:
737 * Unwind the current thread starting at @start_ctx.
739 * If @start_ctx is null, we capture the current context.
742 mono_walk_stack_with_ctx (MonoJitStackWalk func, MonoContext *start_ctx, MonoUnwindOptions unwind_options, void *user_data)
744 MonoContext extra_ctx;
745 MonoInternalThread *thread = mono_thread_internal_current ();
746 MONO_ARCH_CONTEXT_DEF
748 if (!thread || !thread->jit_data)
752 mono_arch_flush_register_windows ();
754 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
755 MONO_INIT_CONTEXT_FROM_CURRENT (&extra_ctx);
757 MONO_INIT_CONTEXT_FROM_FUNC (&extra_ctx, mono_walk_stack_with_ctx);
759 start_ctx = &extra_ctx;
762 mono_walk_stack_full (func, start_ctx, mono_domain_get (), thread->jit_data, mono_get_lmf (), unwind_options, user_data);
766 * mono_walk_stack_with_state:
768 * Unwind a thread described by @state.
770 * State must be valid (state->valid == TRUE).
772 * If you are using this function to unwind another thread, make sure it is suspended.
774 * If @state is null, we capture the current context.
777 mono_walk_stack_with_state (MonoJitStackWalk func, MonoThreadUnwindState *state, MonoUnwindOptions unwind_options, void *user_data)
779 MonoThreadUnwindState extra_state;
781 g_assert (!mono_thread_info_is_async_context ());
782 if (!mono_thread_state_init_from_current (&extra_state))
784 state = &extra_state;
787 g_assert (state->valid);
789 mono_walk_stack_full (func,
791 state->unwind_data [MONO_UNWIND_DATA_DOMAIN],
792 state->unwind_data [MONO_UNWIND_DATA_JIT_TLS],
793 state->unwind_data [MONO_UNWIND_DATA_LMF],
794 unwind_options, user_data);
798 mono_walk_stack (MonoJitStackWalk func, MonoUnwindOptions options, void *user_data)
800 MonoThreadUnwindState state;
801 if (!mono_thread_state_init_from_current (&state))
803 mono_walk_stack_with_state (func, &state, options, user_data);
807 * mono_walk_stack_full:
808 * @func: callback to call for each stack frame
809 * @domain: starting appdomain, can be NULL to use the current domain
810 * @unwind_options: what extra information the unwinder should gather
811 * @start_ctx: starting state of the stack walk, can be NULL.
812 * @thread: the thread whose stack to walk, can be NULL to use the current thread
813 * @lmf: the LMF of @thread, can be NULL to use the LMF of the current thread
814 * @user_data: data passed to the callback
816 * This function walks the stack of a thread, starting from the state
817 * represented by start_ctx. For each frame the callback
818 * function is called with the relevant info. The walk ends when no more
819 * managed stack frames are found or when the callback returns a TRUE value.
822 mono_walk_stack_full (MonoJitStackWalk func, MonoContext *start_ctx, MonoDomain *domain, MonoJitTlsData *jit_tls, MonoLMF *lmf, MonoUnwindOptions unwind_options, gpointer user_data)
825 MonoContext ctx, new_ctx;
826 StackFrameInfo frame;
828 mgreg_t *reg_locations [MONO_MAX_IREGS];
829 mgreg_t *new_reg_locations [MONO_MAX_IREGS];
830 gboolean get_reg_locations = unwind_options & MONO_UNWIND_REG_LOCATIONS;
831 gboolean async = mono_thread_info_is_async_context ();
833 g_assert (start_ctx);
836 /*The LMF will be null if the target have no managed frames.*/
837 /* g_assert (lmf); */
840 g_assert (unwind_options == MONO_UNWIND_NONE);
842 memcpy (&ctx, start_ctx, sizeof (MonoContext));
843 memset (reg_locations, 0, sizeof (reg_locations));
845 while (MONO_CONTEXT_GET_SP (&ctx) < jit_tls->end_of_stack) {
847 res = mono_find_jit_info_ext (domain, jit_tls, NULL, &ctx, &new_ctx, NULL, &lmf, get_reg_locations ? new_reg_locations : NULL, &frame);
851 if ((unwind_options & MONO_UNWIND_LOOKUP_IL_OFFSET) && frame.ji) {
852 MonoDebugSourceLocation *source;
854 source = mono_debug_lookup_source_location (jinfo_get_method (frame.ji), frame.native_offset, domain);
855 il_offset = source ? source->il_offset : -1;
856 mono_debug_free_source_location (source);
860 frame.il_offset = il_offset;
862 if ((unwind_options & MONO_UNWIND_LOOKUP_ACTUAL_METHOD) && frame.ji) {
863 frame.actual_method = get_method_from_stack_frame (frame.ji, get_generic_info_from_stack_frame (frame.ji, &ctx));
865 frame.actual_method = frame.method;
868 if (get_reg_locations)
869 frame.reg_locations = reg_locations;
871 if (func (&frame, &ctx, user_data))
874 if (get_reg_locations) {
875 for (i = 0; i < MONO_MAX_IREGS; ++i)
876 if (new_reg_locations [i])
877 reg_locations [i] = new_reg_locations [i];
885 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
886 MonoReflectionMethod **method,
887 gint32 *iloffset, gint32 *native_offset,
888 MonoString **file, gint32 *line, gint32 *column)
890 MonoDomain *domain = mono_domain_get ();
891 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
892 MonoLMF *lmf = mono_get_lmf ();
893 MonoJitInfo *ji = NULL;
894 MonoContext ctx, new_ctx;
895 MonoDebugSourceLocation *location;
896 MonoMethod *jmethod, *actual_method;
897 StackFrameInfo frame;
900 MONO_ARCH_CONTEXT_DEF;
902 mono_arch_flush_register_windows ();
904 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
905 MONO_INIT_CONTEXT_FROM_CURRENT (&ctx);
907 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_get_frame_info);
913 res = mono_find_jit_info_ext (domain, jit_tls, NULL, &ctx, &new_ctx, NULL, &lmf, NULL, &frame);
917 if (frame.type == FRAME_TYPE_MANAGED_TO_NATIVE || frame.type == FRAME_TYPE_DEBUGGER_INVOKE)
921 *native_offset = frame.native_offset;
923 /* The skip count passed by the caller depends on us not filtering out MANAGED_TO_NATIVE */
924 jmethod = jinfo_get_method (ji);
925 if (jmethod->wrapper_type != MONO_WRAPPER_NONE && jmethod->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD && jmethod->wrapper_type != MONO_WRAPPER_MANAGED_TO_NATIVE)
930 actual_method = get_method_from_stack_frame (ji, get_generic_info_from_stack_frame (ji, &ctx));
932 mono_gc_wbarrier_generic_store (method, (MonoObject*) mono_method_get_object (domain, actual_method, NULL));
934 location = mono_debug_lookup_source_location (jmethod, *native_offset, domain);
936 *iloffset = location->il_offset;
940 if (need_file_info) {
942 mono_gc_wbarrier_generic_store (file, (MonoObject*) mono_string_new (domain, location->source_file));
943 *line = location->row;
944 *column = location->column;
951 mono_debug_free_source_location (location);
958 MonoSecurityFrame *frame;
959 } MonoFrameSecurityInfo;
962 callback_get_first_frame_security_info (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
964 MonoFrameSecurityInfo *si = (MonoFrameSecurityInfo*) data;
965 MonoJitInfo *ji = frame->ji;
971 /* FIXME: skip all wrappers ?? probably not - case by case testing is required */
972 method = jinfo_get_method (ji);
973 if (method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
974 method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE ||
975 method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH ||
976 method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
977 method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE) {
986 si->frame = mono_declsec_create_frame (frame->domain, ji);
988 /* Stop - we only want the first frame (e.g. LinkDemand and InheritanceDemand) */
993 * ves_icall_System_Security_SecurityFrame_GetSecurityFrame:
994 * @skip: the number of stack frames to skip
996 * This function returns a the security informations of a single stack frame
997 * (after the skipped ones). This is required for [NonCas]LinkDemand[Choice]
998 * and [NonCas]InheritanceDemand[Choice] as only the caller security is
1002 ves_icall_System_Security_SecurityFrame_GetSecurityFrame (gint32 skip)
1004 MonoFrameSecurityInfo si;
1009 mono_walk_stack (callback_get_first_frame_security_info, MONO_UNWIND_DEFAULT, &si);
1011 return (si.skips == 0) ? si.frame : NULL;
1020 } MonoSecurityStack;
1023 grow_array (MonoSecurityStack *stack)
1025 MonoDomain *domain = mono_domain_get ();
1026 guint32 newsize = (stack->maximum << 1);
1027 MonoArray *newstack = mono_array_new (domain, mono_defaults.runtimesecurityframe_class, newsize);
1029 for (i=0; i < stack->maximum; i++) {
1030 gpointer frame = mono_array_get (stack->stack, gpointer, i);
1031 mono_array_setref (newstack, i, frame);
1033 stack->maximum = newsize;
1034 stack->stack = newstack;
1038 callback_get_stack_frames_security_info (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
1040 MonoSecurityStack *ss = (MonoSecurityStack*) data;
1041 MonoJitInfo *ji = frame->ji;
1047 /* FIXME: skip all wrappers ?? probably not - case by case testing is required */
1048 method = jinfo_get_method (ji);
1049 if (method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
1050 method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE ||
1051 method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH ||
1052 method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
1053 method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE) {
1057 if (ss->skips > 0) {
1062 if (ss->count == ss->maximum)
1065 mono_array_setref (ss->stack, ss->count++, mono_declsec_create_frame (frame->domain, ji));
1067 /* continue down the stack */
1072 glist_to_array (GList *list, MonoClass *eclass)
1074 MonoDomain *domain = mono_domain_get ();
1081 len = g_list_length (list);
1082 res = mono_array_new (domain, eclass, len);
1084 for (i = 0; list; list = list->next, i++)
1085 mono_array_set (res, gpointer, i, list->data);
1091 * ves_icall_System_Security_SecurityFrame_GetSecurityStack:
1092 * @skip: the number of stack frames to skip
1094 * This function returns an managed array of containing the security
1095 * informations for each frame (after the skipped ones). This is used for
1096 * [NonCas]Demand[Choice] where the complete evaluation of the stack is
1100 ves_icall_System_Security_SecurityFrame_GetSecurityStack (gint32 skip)
1102 MonoSecurityStack ss;
1104 #if defined(__ia64__) || defined(__s390__) || defined(__s390x__)
1110 ss.maximum = MONO_CAS_INITIAL_STACK_SIZE;
1111 ss.stack = mono_array_new (mono_domain_get (), mono_defaults.runtimesecurityframe_class, ss.maximum);
1112 mono_walk_stack (callback_get_stack_frames_security_info, MONO_UNWIND_DEFAULT, &ss);
1113 /* g_warning ("STACK RESULT: %d out of %d", ss.count, ss.maximum); */
1118 get_exception_catch_class (MonoJitExceptionInfo *ei, MonoJitInfo *ji, MonoContext *ctx)
1120 MonoClass *catch_class = ei->data.catch_class;
1121 MonoType *inflated_type;
1122 MonoGenericContext context;
1124 /*MonoJitExceptionInfo::data is an union used by filter and finally clauses too.*/
1125 if (!catch_class || ei->flags != MONO_EXCEPTION_CLAUSE_NONE)
1128 if (!ji->has_generic_jit_info || !mono_jit_info_get_generic_jit_info (ji)->has_this)
1130 context = get_generic_context_from_stack_frame (ji, get_generic_info_from_stack_frame (ji, ctx));
1132 /* FIXME: we shouldn't inflate but instead put the
1133 type in the rgctx and fetch it from there. It
1134 might be a good idea to do this lazily, i.e. only
1135 when the exception is actually thrown, so as not to
1136 waste space for exception clauses which might never
1138 inflated_type = mono_class_inflate_generic_type (&catch_class->byval_arg, &context);
1139 catch_class = mono_class_from_mono_type (inflated_type);
1140 mono_metadata_free_type (inflated_type);
1146 * mini_jit_info_table_find:
1148 * Same as mono_jit_info_table_find, but search all the domains of the current thread
1149 * if ADDR is not found in DOMAIN. The domain where the method was found is stored into
1150 * OUT_DOMAIN if it is not NULL.
1153 mini_jit_info_table_find (MonoDomain *domain, char *addr, MonoDomain **out_domain)
1156 MonoInternalThread *t = mono_thread_internal_current ();
1162 ji = mono_jit_info_table_find (domain, addr);
1165 *out_domain = domain;
1169 /* maybe it is shared code, so we also search in the root domain */
1170 if (domain != mono_get_root_domain ()) {
1171 ji = mono_jit_info_table_find (mono_get_root_domain (), addr);
1174 *out_domain = mono_get_root_domain ();
1179 refs = (t->appdomain_refs) ? *(gpointer *) t->appdomain_refs : NULL;
1180 for (; refs && *refs; refs++) {
1181 if (*refs != domain && *refs != mono_get_root_domain ()) {
1182 ji = mono_jit_info_table_find ((MonoDomain*) *refs, addr);
1185 *out_domain = (MonoDomain*) *refs;
1195 * wrap_non_exception_throws:
1197 * Determine whenever M's assembly has a RuntimeCompatibilityAttribute with the
1198 * WrapNonExceptionThrows flag set.
1201 wrap_non_exception_throws (MonoMethod *m)
1203 MonoAssembly *ass = m->klass->image->assembly;
1204 MonoCustomAttrInfo* attrs;
1205 static MonoClass *klass;
1207 gboolean val = FALSE;
1210 if (ass->wrap_non_exception_throws_inited)
1211 return ass->wrap_non_exception_throws;
1213 klass = mono_class_from_name_cached (mono_defaults.corlib, "System.Runtime.CompilerServices", "RuntimeCompatibilityAttribute");
1215 attrs = mono_custom_attrs_from_assembly (ass);
1217 for (i = 0; i < attrs->num_attrs; ++i) {
1218 MonoCustomAttrEntry *attr = &attrs->attrs [i];
1220 int len, num_named, named_type, data_type, name_len;
1223 if (!attr->ctor || attr->ctor->klass != klass)
1225 /* Decode the RuntimeCompatibilityAttribute. See reflection.c */
1226 len = attr->data_size;
1227 p = (const char*)attr->data;
1228 g_assert (read16 (p) == 0x0001);
1230 num_named = read16 (p);
1239 if (named_type != 0x54)
1241 name_len = mono_metadata_decode_blob_size (p, &p);
1242 name = g_malloc (name_len + 1);
1243 memcpy (name, p, name_len);
1244 name [name_len] = 0;
1246 g_assert (!strcmp (name, "WrapNonExceptionThrows"));
1248 /* The value is a BOOLEAN */
1251 mono_custom_attrs_free (attrs);
1254 ass->wrap_non_exception_throws = val;
1255 mono_memory_barrier ();
1256 ass->wrap_non_exception_throws_inited = TRUE;
1261 #ifndef MONO_ARCH_STACK_GROWS_UP
1262 #define DOES_STACK_GROWS_UP 1
1264 #define DOES_STACK_GROWS_UP 0
1267 #define MAX_UNMANAGED_BACKTRACE 128
1269 build_native_trace (void)
1271 /* This puppy only makes sense on mobile, IOW, ARM. */
1272 #if defined (HAVE_BACKTRACE_SYMBOLS) && defined (TARGET_ARM)
1274 void *native_trace [MAX_UNMANAGED_BACKTRACE];
1275 int size = backtrace (native_trace, MAX_UNMANAGED_BACKTRACE);
1280 res = mono_array_new (mono_domain_get (), mono_defaults.int_class, size);
1282 for (i = 0; i < size; i++)
1283 mono_array_set (res, gpointer, i, native_trace [i]);
1290 #define setup_managed_stacktrace_information() do { \
1291 if (mono_ex && !initial_trace_ips) { \
1292 trace_ips = g_list_reverse (trace_ips); \
1293 MONO_OBJECT_SETREF (mono_ex, trace_ips, glist_to_array (trace_ips, mono_defaults.int_class)); \
1294 MONO_OBJECT_SETREF (mono_ex, native_trace_ips, build_native_trace ()); \
1295 if (has_dynamic_methods) \
1296 /* These methods could go away anytime, so compute the stack trace now */ \
1297 MONO_OBJECT_SETREF (mono_ex, stack_trace, ves_icall_System_Exception_get_trace (mono_ex)); \
1299 g_list_free (trace_ips); \
1303 * mono_handle_exception_internal_first_pass:
1305 * The first pass of exception handling. Unwind the stack until a catch clause which can catch
1306 * OBJ is found. Run the index of the filter clause which caught the exception into
1307 * OUT_FILTER_IDX. Return TRUE if the exception is caught, FALSE otherwise.
1310 mono_handle_exception_internal_first_pass (MonoContext *ctx, gpointer obj, gint32 *out_filter_idx, MonoJitInfo **out_ji, MonoJitInfo **out_prev_ji, MonoObject *non_exception)
1312 MonoDomain *domain = mono_domain_get ();
1313 MonoJitInfo *ji = NULL;
1314 static int (*call_filter) (MonoContext *, gpointer) = NULL;
1315 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
1316 MonoLMF *lmf = mono_get_lmf ();
1317 MonoArray *initial_trace_ips = NULL;
1318 GList *trace_ips = NULL;
1319 MonoException *mono_ex;
1320 gboolean stack_overflow = FALSE;
1321 MonoContext initial_ctx;
1323 int frame_count = 0;
1324 gboolean has_dynamic_methods = FALSE;
1329 g_assert (ctx != NULL);
1331 if (obj == domain->stack_overflow_ex)
1332 stack_overflow = TRUE;
1334 mono_ex = (MonoException*)obj;
1335 initial_trace_ips = mono_ex->trace_ips;
1337 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
1338 mono_ex = (MonoException*)obj;
1339 initial_trace_ips = mono_ex->trace_ips;
1345 call_filter = mono_get_call_filter ();
1347 g_assert (jit_tls->end_of_stack);
1348 g_assert (jit_tls->abort_func);
1351 *out_filter_idx = -1;
1355 *out_prev_ji = NULL;
1360 MonoContext new_ctx;
1362 int clause_index_start = 0;
1363 gboolean unwind_res = TRUE;
1365 StackFrameInfo frame;
1370 unwind_res = mono_find_jit_info_ext (domain, jit_tls, NULL, ctx, &new_ctx, NULL, &lmf, NULL, &frame);
1372 if (frame.type == FRAME_TYPE_DEBUGGER_INVOKE || frame.type == FRAME_TYPE_MANAGED_TO_NATIVE) {
1376 g_assert (frame.type == FRAME_TYPE_MANAGED);
1381 setup_managed_stacktrace_information ();
1386 method = jinfo_get_method (ji);
1387 //printf ("M: %s %d.\n", mono_method_full_name (method, TRUE), frame_count);
1389 if (mini_get_debug_options ()->reverse_pinvoke_exceptions && method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
1390 g_error ("A native frame was found while unwinding the stack after an exception.\n"
1391 "The native frame called the managed method:\n%s\n",
1392 mono_method_full_name (method, TRUE));
1395 if (method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
1397 * Avoid overwriting the stack trace if the exception is
1398 * rethrown. Also avoid giant stack traces during a stack
1401 if (!initial_trace_ips && (frame_count < 1000)) {
1402 trace_ips = g_list_prepend (trace_ips, MONO_CONTEXT_GET_IP (ctx));
1403 trace_ips = g_list_prepend (trace_ips,
1404 get_generic_info_from_stack_frame (ji, ctx));
1408 if (method->dynamic)
1409 has_dynamic_methods = TRUE;
1411 if (stack_overflow) {
1412 if (DOES_STACK_GROWS_UP)
1413 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx));
1415 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (ctx));
1417 free_stack = 0xffffff;
1420 for (i = clause_index_start; i < ji->num_clauses; i++) {
1421 MonoJitExceptionInfo *ei = &ji->clauses [i];
1422 gboolean filtered = FALSE;
1425 * During stack overflow, wait till the unwinding frees some stack
1426 * space before running handlers/finalizers.
1428 if (free_stack <= (64 * 1024))
1431 if (is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (ctx))) {
1433 MonoClass *catch_class = get_exception_catch_class (ei, ji, ctx);
1436 * Have to unwrap RuntimeWrappedExceptions if the
1437 * method's assembly doesn't have a RuntimeCompatibilityAttribute.
1439 if (non_exception && !wrap_non_exception_throws (method))
1440 ex_obj = non_exception;
1444 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
1445 gboolean is_user_frame = method->wrapper_type == MONO_WRAPPER_NONE || method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD;
1446 #ifndef DISABLE_PERFCOUNTERS
1447 mono_perfcounters->exceptions_filters++;
1449 mono_debugger_call_exception_handler (ei->data.filter, MONO_CONTEXT_GET_SP (ctx), ex_obj);
1452 Here's the thing, if this is a filter clause done by a wrapper like runtime invoke, we don't want to
1453 trim the stackframe since if it returns FALSE we lose information.
1455 FIXME Not 100% sure if it's a good idea even with user clauses.
1458 setup_managed_stacktrace_information ();
1460 if (ji->from_llvm) {
1461 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
1462 MONO_CONTEXT_SET_LLVM_EXC_REG (ctx, ex_obj);
1464 g_assert_not_reached ();
1467 /* store the exception object in bp + ei->exvar_offset */
1468 *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = ex_obj;
1471 mono_debugger_agent_begin_exception_filter (mono_ex, ctx, &initial_ctx);
1472 filtered = call_filter (ctx, ei->data.filter);
1473 mono_debugger_agent_end_exception_filter (mono_ex, ctx, &initial_ctx);
1474 if (filtered && out_filter_idx)
1475 *out_filter_idx = filter_idx;
1482 setup_managed_stacktrace_information ();
1483 /* mono_debugger_agent_handle_exception () needs this */
1484 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1489 if (ei->flags == MONO_EXCEPTION_CLAUSE_NONE && mono_object_isinst (ex_obj, catch_class)) {
1490 setup_managed_stacktrace_information ();
1495 /* mono_debugger_agent_handle_exception () needs this */
1496 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1505 g_assert_not_reached ();
1509 * mono_handle_exception_internal:
1510 * @ctx: saved processor state
1511 * @obj: the exception object
1512 * @resume: whenever to resume unwinding based on the state in MonoJitTlsData.
1515 mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gboolean resume, MonoJitInfo **out_ji)
1517 MonoDomain *domain = mono_domain_get ();
1518 MonoJitInfo *ji, *prev_ji;
1519 static int (*call_filter) (MonoContext *, gpointer) = NULL;
1520 static void (*restore_context) (void *);
1521 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
1522 MonoLMF *lmf = mono_get_lmf ();
1523 MonoException *mono_ex;
1524 gboolean stack_overflow = FALSE;
1525 MonoContext initial_ctx;
1527 int frame_count = 0;
1528 gint32 filter_idx, first_filter_idx;
1531 MonoObject *non_exception = NULL;
1533 g_assert (ctx != NULL);
1535 MonoException *ex = mono_get_exception_null_reference ();
1536 MONO_OBJECT_SETREF (ex, message, mono_string_new (domain, "Object reference not set to an instance of an object"));
1537 obj = (MonoObject *)ex;
1541 * Allocate a new exception object instead of the preconstructed ones.
1543 if (obj == domain->stack_overflow_ex) {
1545 * It is not a good idea to try and put even more pressure on the little stack available.
1546 * obj = mono_get_exception_stack_overflow ();
1548 stack_overflow = TRUE;
1550 else if (obj == domain->null_reference_ex) {
1551 obj = mono_get_exception_null_reference ();
1554 if (!mono_object_isinst (obj, mono_defaults.exception_class)) {
1555 non_exception = obj;
1556 obj = mono_get_exception_runtime_wrapped (obj);
1559 mono_ex = (MonoException*)obj;
1561 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
1562 mono_ex = (MonoException*)obj;
1567 if (mono_ex && jit_tls->class_cast_from) {
1568 if (!strcmp (mono_ex->object.vtable->klass->name, "InvalidCastException")) {
1569 char *from_name = mono_type_get_full_name (jit_tls->class_cast_from);
1570 char *to_name = mono_type_get_full_name (jit_tls->class_cast_to);
1571 char *msg = g_strdup_printf ("Unable to cast object of type '%s' to type '%s'.", from_name, to_name);
1572 mono_ex->message = mono_string_new (domain, msg);
1577 if (!strcmp (mono_ex->object.vtable->klass->name, "ArrayTypeMismatchException")) {
1578 char *from_name = mono_type_get_full_name (jit_tls->class_cast_from);
1579 char *to_name = mono_type_get_full_name (jit_tls->class_cast_to);
1580 char *msg = g_strdup_printf ("Source array of type '%s' cannot be cast to destination array type '%s'.", from_name, to_name);
1581 mono_ex->message = mono_string_new (domain, msg);
1589 call_filter = mono_get_call_filter ();
1591 if (!restore_context)
1592 restore_context = mono_get_restore_context ();
1594 g_assert (jit_tls->end_of_stack);
1595 g_assert (jit_tls->abort_func);
1598 * We set orig_ex_ctx_set to TRUE/FALSE around profiler calls to make sure it doesn't
1599 * end up being TRUE on any code path.
1601 memcpy (&jit_tls->orig_ex_ctx, ctx, sizeof (MonoContext));
1606 MonoContext ctx_cp = *ctx;
1607 if (mono_trace_is_enabled ()) {
1608 MonoMethod *system_exception_get_message = mono_class_get_method_from_name (mono_defaults.exception_class, "get_Message", 0);
1609 MonoMethod *get_message = system_exception_get_message == NULL ? NULL : mono_object_get_virtual_method (obj, system_exception_get_message);
1610 MonoObject *message;
1611 const char *type_name = mono_class_get_name (mono_object_class (mono_ex));
1613 MonoObject *exc = NULL;
1614 if (get_message == NULL) {
1616 } else if (!strcmp (type_name, "OutOfMemoryException") || !strcmp (type_name, "StackOverflowException")) {
1618 msg = g_strdup_printf ("(No exception message for: %s)\n", type_name);
1620 message = mono_runtime_invoke (get_message, obj, NULL, &exc);
1624 msg = message ? mono_string_to_utf8 ((MonoString *) message) : g_strdup ("(System.Exception.Message property not available)");
1626 g_print ("[%p:] EXCEPTION handling: %s.%s: %s\n", (void*)GetCurrentThreadId (), mono_object_class (obj)->name_space, mono_object_class (obj)->name, msg);
1628 if (mono_ex && mono_trace_eval_exception (mono_object_class (mono_ex)))
1629 mono_print_thread_dump_from_ctx (ctx);
1631 jit_tls->orig_ex_ctx_set = TRUE;
1632 mono_profiler_exception_thrown (obj);
1633 jit_tls->orig_ex_ctx_set = FALSE;
1635 res = mono_handle_exception_internal_first_pass (&ctx_cp, obj, &first_filter_idx, &ji, &prev_ji, non_exception);
1638 if (mini_get_debug_options ()->break_on_exc)
1640 mono_debugger_agent_handle_exception (obj, ctx, NULL);
1642 if (mini_get_debug_options ()->suspend_on_unhandled) {
1643 mono_runtime_printf_err ("Unhandled exception, suspending...");
1648 // FIXME: This runs managed code so it might cause another stack overflow when
1649 // we are handling a stack overflow
1650 mono_unhandled_exception (obj);
1652 gboolean unhandled = FALSE;
1655 * The exceptions caught by the mono_runtime_invoke () calls in mono_async_invoke () needs to be treated as
1656 * unhandled (#669836).
1657 * FIXME: The check below is hackish, but its hard to distinguish these runtime invoke calls from others
1660 if (ji && jinfo_get_method (ji)->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
1662 MonoInternalThread *thread = mono_thread_internal_current ();
1663 if (jinfo_get_method (prev_ji) == thread->async_invoke_method)
1668 mono_debugger_agent_handle_exception (obj, ctx, NULL);
1670 mono_debugger_agent_handle_exception (obj, ctx, &ctx_cp);
1680 MonoContext new_ctx;
1682 int clause_index_start = 0;
1683 gboolean unwind_res = TRUE;
1687 ji = jit_tls->resume_state.ji;
1688 new_ctx = jit_tls->resume_state.new_ctx;
1689 clause_index_start = jit_tls->resume_state.clause_index;
1690 lmf = jit_tls->resume_state.lmf;
1691 first_filter_idx = jit_tls->resume_state.first_filter_idx;
1692 filter_idx = jit_tls->resume_state.filter_idx;
1694 StackFrameInfo frame;
1696 unwind_res = mono_find_jit_info_ext (domain, jit_tls, NULL, ctx, &new_ctx, NULL, &lmf, NULL, &frame);
1698 if (frame.type == FRAME_TYPE_DEBUGGER_INVOKE || frame.type == FRAME_TYPE_MANAGED_TO_NATIVE) {
1702 g_assert (frame.type == FRAME_TYPE_MANAGED);
1708 *(mono_get_lmf_addr ()) = lmf;
1710 jit_tls->abort_func (obj);
1711 g_assert_not_reached ();
1714 method = jinfo_get_method (ji);
1716 //printf ("M: %s %d.\n", mono_method_full_name (method, TRUE), frame_count);
1718 if (stack_overflow) {
1719 if (DOES_STACK_GROWS_UP)
1720 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx));
1722 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (ctx));
1724 free_stack = 0xffffff;
1727 for (i = clause_index_start; i < ji->num_clauses; i++) {
1728 MonoJitExceptionInfo *ei = &ji->clauses [i];
1729 gboolean filtered = FALSE;
1732 * During stack overflow, wait till the unwinding frees some stack
1733 * space before running handlers/finalizers.
1735 if (free_stack <= (64 * 1024))
1738 if (is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (ctx))) {
1740 MonoClass *catch_class = get_exception_catch_class (ei, ji, ctx);
1743 * Have to unwrap RuntimeWrappedExceptions if the
1744 * method's assembly doesn't have a RuntimeCompatibilityAttribute.
1746 if (non_exception && !wrap_non_exception_throws (method))
1747 ex_obj = non_exception;
1751 if (((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER))) {
1752 if (ji->from_llvm) {
1753 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
1754 MONO_CONTEXT_SET_LLVM_EXC_REG (ctx, ex_obj);
1756 g_assert_not_reached ();
1759 /* store the exception object in bp + ei->exvar_offset */
1760 *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = ex_obj;
1764 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
1766 * Filter clauses should only be run in the
1767 * first pass of exception handling.
1769 filtered = (filter_idx == first_filter_idx);
1773 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
1774 mono_object_isinst (ex_obj, catch_class)) || filtered) {
1776 * This guards against the situation that we abort a thread that is executing a finally clause
1777 * that was called by the EH machinery. It won't have a guard trampoline installed, so we must
1778 * check for this situation here and resume interruption if we are below the guarded block.
1780 if (G_UNLIKELY (jit_tls->handler_block_return_address)) {
1781 gboolean is_outside = FALSE;
1782 gpointer prot_bp = MONO_CONTEXT_GET_BP (&jit_tls->handler_block_context);
1783 gpointer catch_bp = MONO_CONTEXT_GET_BP (ctx);
1784 //FIXME make this stack direction aware
1785 if (catch_bp > prot_bp) {
1787 } else if (catch_bp == prot_bp) {
1788 /* Can be either try { try { } catch {} } finally {} or try { try { } finally {} } catch {}
1789 * So we check if the catch handler_start is protected by the guarded handler protected region
1792 * If there is an outstanding guarded_block return address, it means the current thread must be aborted.
1793 * This is the only way to reach out the guarded block as other cases are handled by the trampoline.
1794 * There aren't any further finally/fault handler blocks down the stack over this exception.
1795 * This must be ensured by the code that installs the guard trampoline.
1797 g_assert (ji == mini_jit_info_table_find (domain, MONO_CONTEXT_GET_IP (&jit_tls->handler_block_context), NULL));
1799 if (!is_address_protected (ji, jit_tls->handler_block, ei->handler_start)) {
1804 jit_tls->handler_block_return_address = NULL;
1805 jit_tls->handler_block = NULL;
1806 mono_thread_resume_interruption (); /*We ignore the exception here, it will be raised later*/
1810 if (mono_trace_is_enabled () && mono_trace_eval (method))
1811 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (method, TRUE));
1812 jit_tls->orig_ex_ctx_set = TRUE;
1813 mono_profiler_exception_clause_handler (method, ei->flags, i);
1814 jit_tls->orig_ex_ctx_set = FALSE;
1815 mono_debugger_call_exception_handler (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), ex_obj);
1816 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1817 *(mono_get_lmf_addr ()) = lmf;
1818 #ifndef DISABLE_PERFCOUNTERS
1819 mono_perfcounters->exceptions_depth += frame_count;
1821 if (obj == domain->stack_overflow_ex)
1822 jit_tls->handling_stack_ovf = FALSE;
1826 if (is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (ctx)) &&
1827 (ei->flags == MONO_EXCEPTION_CLAUSE_FAULT)) {
1828 if (mono_trace_is_enabled () && mono_trace_eval (method))
1829 g_print ("EXCEPTION: fault clause %d of %s\n", i, mono_method_full_name (method, TRUE));
1830 jit_tls->orig_ex_ctx_set = TRUE;
1831 mono_profiler_exception_clause_handler (method, ei->flags, i);
1832 jit_tls->orig_ex_ctx_set = FALSE;
1833 mono_debugger_call_exception_handler (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), ex_obj);
1834 call_filter (ctx, ei->handler_start);
1836 if (is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (ctx)) &&
1837 (ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY)) {
1838 if (mono_trace_is_enabled () && mono_trace_eval (method))
1839 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (method, TRUE));
1840 jit_tls->orig_ex_ctx_set = TRUE;
1841 mono_profiler_exception_clause_handler (method, ei->flags, i);
1842 jit_tls->orig_ex_ctx_set = FALSE;
1843 mono_debugger_call_exception_handler (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), ex_obj);
1844 #ifndef DISABLE_PERFCOUNTERS
1845 mono_perfcounters->exceptions_finallys++;
1847 *(mono_get_lmf_addr ()) = lmf;
1848 if (ji->from_llvm) {
1850 * LLVM compiled finally handlers follow the design
1851 * of the c++ ehabi, i.e. they call a resume function
1852 * at the end instead of returning to the caller.
1853 * So save the exception handling state,
1854 * mono_resume_unwind () will call us again to continue
1857 jit_tls->resume_state.ex_obj = obj;
1858 jit_tls->resume_state.ji = ji;
1859 jit_tls->resume_state.clause_index = i + 1;
1860 jit_tls->resume_state.ctx = *ctx;
1861 jit_tls->resume_state.new_ctx = new_ctx;
1862 jit_tls->resume_state.lmf = lmf;
1863 jit_tls->resume_state.first_filter_idx = first_filter_idx;
1864 jit_tls->resume_state.filter_idx = filter_idx;
1865 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1868 call_filter (ctx, ei->handler_start);
1874 jit_tls->orig_ex_ctx_set = TRUE;
1875 mono_profiler_exception_method_leave (method);
1876 jit_tls->orig_ex_ctx_set = FALSE;
1881 g_assert_not_reached ();
1885 * mono_debugger_handle_exception:
1887 * Notify the debugger about exceptions. Returns TRUE if the debugger wants us to stop
1888 * at the exception and FALSE to resume with the normal exception handling.
1890 * The arch code is responsible to setup @ctx in a way that MONO_CONTEXT_GET_IP () and
1891 * MONO_CONTEXT_GET_SP () point to the throw instruction; ie. before executing the
1892 * `callq throw' instruction.
1895 mono_debugger_handle_exception (MonoContext *ctx, MonoObject *obj)
1897 MonoDebuggerExceptionAction action;
1899 if (!mono_debug_using_mono_debugger ())
1903 MonoException *ex = mono_get_exception_null_reference ();
1904 MONO_OBJECT_SETREF (ex, message, mono_string_new (mono_domain_get (), "Object reference not set to an instance of an object"));
1905 obj = (MonoObject *)ex;
1908 action = _mono_debugger_throw_exception (MONO_CONTEXT_GET_IP (ctx), MONO_CONTEXT_GET_SP (ctx), obj);
1910 if (action == MONO_DEBUGGER_EXCEPTION_ACTION_STOP) {
1912 * The debugger wants us to stop on the `throw' instruction.
1913 * By the time we get here, it already inserted a breakpoint there.
1916 } else if (action == MONO_DEBUGGER_EXCEPTION_ACTION_STOP_UNHANDLED) {
1917 MonoContext ctx_cp = *ctx;
1918 MonoJitInfo *ji = NULL;
1922 * The debugger wants us to stop only if this exception is user-unhandled.
1925 ret = mono_handle_exception_internal_first_pass (&ctx_cp, obj, NULL, &ji, NULL, NULL);
1926 if (ret && (ji != NULL) && (jinfo_get_method (ji)->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE)) {
1928 * The exception is handled in a runtime-invoke wrapper, that means that it's unhandled
1929 * inside the method being invoked, so we handle it like a user-unhandled exception.
1936 * The exception is user-unhandled - tell the debugger to stop.
1938 return _mono_debugger_unhandled_exception (MONO_CONTEXT_GET_IP (ctx), MONO_CONTEXT_GET_SP (ctx), obj);
1942 * The exception is catched somewhere - resume with the normal exception handling and don't
1943 * stop in the debugger.
1951 * mono_debugger_run_finally:
1952 * @start_ctx: saved processor state
1954 * This method is called by the Mono Debugger to call all `finally' clauses of the
1955 * current stack frame. It's used when the user issues a `return' command to make
1956 * the current stack frame return. After returning from this method, the debugger
1957 * unwinds the stack one frame and gives control back to the user.
1959 * NOTE: This method is only used when running inside the Mono Debugger.
1962 mono_debugger_run_finally (MonoContext *start_ctx)
1964 static int (*call_filter) (MonoContext *, gpointer) = NULL;
1965 MonoDomain *domain = mono_domain_get ();
1966 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
1967 MonoLMF *lmf = mono_get_lmf ();
1968 MonoContext ctx, new_ctx;
1969 MonoJitInfo *ji, rji;
1974 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, NULL, NULL);
1975 if (!ji || ji == (gpointer)-1)
1979 call_filter = mono_get_call_filter ();
1981 for (i = 0; i < ji->num_clauses; i++) {
1982 MonoJitExceptionInfo *ei = &ji->clauses [i];
1984 if (is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (&ctx)) &&
1985 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1986 call_filter (&ctx, ei->handler_start);
1992 * mono_handle_exception:
1993 * @ctx: saved processor state
1994 * @obj: the exception object
1997 mono_handle_exception (MonoContext *ctx, gpointer obj)
1999 #ifndef DISABLE_PERFCOUNTERS
2000 mono_perfcounters->exceptions_thrown++;
2003 return mono_handle_exception_internal (ctx, obj, FALSE, NULL);
2006 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
2008 #ifndef MONO_ARCH_USE_SIGACTION
2009 #error "Can't use sigaltstack without sigaction"
2012 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
2015 mono_setup_altstack (MonoJitTlsData *tls)
2019 guint8 *staddr = NULL;
2021 if (mono_running_on_valgrind ())
2024 mono_thread_get_stack_bounds (&staddr, &stsize);
2028 tls->end_of_stack = staddr + stsize;
2029 tls->stack_size = stsize;
2031 /*g_print ("thread %p, stack_base: %p, stack_size: %d\n", (gpointer)pthread_self (), staddr, stsize);*/
2033 tls->stack_ovf_guard_base = staddr + mono_pagesize ();
2034 tls->stack_ovf_guard_size = ALIGN_TO (8 * 4096, mono_pagesize ());
2036 g_assert ((guint8*)&sa >= (guint8*)tls->stack_ovf_guard_base + tls->stack_ovf_guard_size);
2038 if (mono_mprotect (tls->stack_ovf_guard_base, tls->stack_ovf_guard_size, MONO_MMAP_NONE)) {
2039 /* mprotect can fail for the main thread stack */
2040 gpointer gaddr = mono_valloc (tls->stack_ovf_guard_base, tls->stack_ovf_guard_size, MONO_MMAP_NONE|MONO_MMAP_PRIVATE|MONO_MMAP_ANON|MONO_MMAP_FIXED);
2041 g_assert (gaddr == tls->stack_ovf_guard_base);
2042 tls->stack_ovf_valloced = TRUE;
2045 /* Setup an alternate signal stack */
2046 tls->signal_stack = mono_valloc (0, MONO_ARCH_SIGNAL_STACK_SIZE, MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_PRIVATE|MONO_MMAP_ANON);
2047 tls->signal_stack_size = MONO_ARCH_SIGNAL_STACK_SIZE;
2049 g_assert (tls->signal_stack);
2051 sa.ss_sp = tls->signal_stack;
2052 sa.ss_size = MONO_ARCH_SIGNAL_STACK_SIZE;
2054 g_assert (sigaltstack (&sa, NULL) == 0);
2056 mono_gc_register_altstack ((char*)tls->stack_ovf_guard_base + tls->stack_ovf_guard_size, (char*)staddr + stsize - ((char*)tls->stack_ovf_guard_base + tls->stack_ovf_guard_size), tls->signal_stack, tls->signal_stack_size);
2060 mono_free_altstack (MonoJitTlsData *tls)
2065 sa.ss_sp = tls->signal_stack;
2066 sa.ss_size = MONO_ARCH_SIGNAL_STACK_SIZE;
2067 sa.ss_flags = SS_DISABLE;
2068 err = sigaltstack (&sa, NULL);
2069 g_assert (err == 0);
2071 if (tls->signal_stack)
2072 mono_vfree (tls->signal_stack, MONO_ARCH_SIGNAL_STACK_SIZE);
2073 if (tls->stack_ovf_valloced)
2074 mono_vfree (tls->stack_ovf_guard_base, tls->stack_ovf_guard_size);
2076 mono_mprotect (tls->stack_ovf_guard_base, tls->stack_ovf_guard_size, MONO_MMAP_READ|MONO_MMAP_WRITE);
2079 #else /* !MONO_ARCH_SIGSEGV_ON_ALTSTACK */
2082 mono_setup_altstack (MonoJitTlsData *tls)
2087 mono_free_altstack (MonoJitTlsData *tls)
2091 #endif /* MONO_ARCH_SIGSEGV_ON_ALTSTACK */
2094 try_restore_stack_protection (MonoJitTlsData *jit_tls, int extra_bytes)
2096 gint32 unprotect_size = jit_tls->stack_ovf_guard_size;
2097 /* we need to leave some room for throwing the exception */
2098 while (unprotect_size >= 0 && (char*)jit_tls->stack_ovf_guard_base + unprotect_size > ((char*)&unprotect_size - extra_bytes))
2099 unprotect_size -= mono_pagesize ();
2100 /* at this point we could try and build a new domain->stack_overflow_ex, but only if there
2101 * is sufficient stack
2103 //fprintf (stderr, "restoring stack protection: %p-%p (%d)\n", jit_tls->stack_ovf_guard_base, (char*)jit_tls->stack_ovf_guard_base + unprotect_size, unprotect_size);
2105 mono_mprotect (jit_tls->stack_ovf_guard_base, unprotect_size, MONO_MMAP_NONE);
2106 return unprotect_size == jit_tls->stack_ovf_guard_size;
2109 static G_GNUC_UNUSED void
2110 try_more_restore (void)
2112 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
2113 if (try_restore_stack_protection (jit_tls, 500))
2114 jit_tls->restore_stack_prot = NULL;
2117 static G_GNUC_UNUSED void
2118 restore_stack_protection (void)
2120 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
2121 MonoException *ex = mono_domain_get ()->stack_overflow_ex;
2122 /* if we can't restore the stack protection, keep a callback installed so
2123 * we'll try to restore as much stack as we can at each return from unmanaged
2126 if (try_restore_stack_protection (jit_tls, 4096))
2127 jit_tls->restore_stack_prot = NULL;
2129 jit_tls->restore_stack_prot = try_more_restore_tramp;
2130 /* here we also throw a stack overflow exception */
2131 ex->trace_ips = NULL;
2132 ex->stack_trace = NULL;
2133 mono_raise_exception (ex);
2137 mono_altstack_restore_prot (mgreg_t *regs, guint8 *code, gpointer *tramp_data, guint8* tramp)
2139 void (*func)(void) = (gpointer)tramp_data;
2145 mono_handle_soft_stack_ovf (MonoJitTlsData *jit_tls, MonoJitInfo *ji, void *ctx, guint8* fault_addr)
2147 /* we got a stack overflow in the soft-guard pages
2148 * There are two cases:
2149 * 1) managed code caused the overflow: we unprotect the soft-guard page
2150 * and let the arch-specific code trigger the exception handling mechanism
2151 * in the thread stack. The soft-guard pages will be protected again as the stack is unwound.
2152 * 2) unmanaged code caused the overflow: we unprotect the soft-guard page
2153 * and hope we can continue with those enabled, at least until the hard-guard page
2154 * is hit. The alternative to continuing here is to just print a message and abort.
2155 * We may add in the future the code to protect the pages again in the codepath
2156 * when we return from unmanaged to managed code.
2158 if (jit_tls->stack_ovf_guard_size && fault_addr >= (guint8*)jit_tls->stack_ovf_guard_base &&
2159 fault_addr < (guint8*)jit_tls->stack_ovf_guard_base + jit_tls->stack_ovf_guard_size) {
2160 /* we unprotect the minimum amount we can */
2162 gboolean handled = FALSE;
2164 guard_size = jit_tls->stack_ovf_guard_size - (mono_pagesize () * SIZEOF_VOID_P / 4);
2165 while (guard_size && fault_addr < (guint8*)jit_tls->stack_ovf_guard_base + guard_size) {
2166 guard_size -= mono_pagesize ();
2168 guard_size = jit_tls->stack_ovf_guard_size - guard_size;
2169 /*fprintf (stderr, "unprotecting: %d\n", guard_size);*/
2170 mono_mprotect ((char*)jit_tls->stack_ovf_guard_base + jit_tls->stack_ovf_guard_size - guard_size, guard_size, MONO_MMAP_READ|MONO_MMAP_WRITE);
2171 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
2173 mono_arch_handle_altstack_exception (ctx, fault_addr, TRUE);
2178 /* We print a message: after this even managed stack overflows
2179 * may crash the runtime
2181 mono_runtime_printf_err ("Stack overflow in unmanaged: IP: %p, fault addr: %p", mono_arch_ip_from_context (ctx), fault_addr);
2182 if (!jit_tls->handling_stack_ovf) {
2183 jit_tls->restore_stack_prot = restore_stack_protection_tramp;
2184 jit_tls->handling_stack_ovf = 1;
2186 /*fprintf (stderr, "Already handling stack overflow\n");*/
2195 MonoMethod *omethod;
2197 } PrintOverflowUserData;
2200 print_overflow_stack_frame (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
2202 MonoMethod *method = NULL;
2203 PrintOverflowUserData *user_data = data;
2207 method = jinfo_get_method (frame->ji);
2210 if (user_data->count == 0) {
2211 /* The first frame is in its prolog, so a line number cannot be computed */
2212 user_data->count ++;
2216 /* If this is a one method overflow, skip the other instances */
2217 if (method == user_data->omethod)
2220 location = mono_debug_print_stack_frame (method, frame->native_offset, mono_domain_get ());
2221 mono_runtime_printf_err (" %s", location);
2224 if (user_data->count == 1) {
2225 mono_runtime_printf_err (" <...>");
2226 user_data->omethod = method;
2228 user_data->omethod = NULL;
2231 user_data->count ++;
2233 mono_runtime_printf_err (" at <unknown> <0x%05x>", frame->native_offset);
2239 mono_handle_hard_stack_ovf (MonoJitTlsData *jit_tls, MonoJitInfo *ji, void *ctx, guint8* fault_addr)
2241 PrintOverflowUserData ud;
2244 /* we don't do much now, but we can warn the user with a useful message */
2245 mono_runtime_printf_err ("Stack overflow: IP: %p, fault addr: %p", mono_arch_ip_from_context (ctx), fault_addr);
2247 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2248 mono_arch_sigctx_to_monoctx (ctx, &mctx);
2250 mono_runtime_printf_err ("Stacktrace:");
2252 memset (&ud, 0, sizeof (ud));
2254 mono_walk_stack_with_ctx (print_overflow_stack_frame, &mctx, MONO_UNWIND_LOOKUP_ACTUAL_METHOD, &ud);
2256 if (ji && jinfo_get_method (ji))
2257 mono_runtime_printf_err ("At %s", mono_method_full_name (jinfo_get_method (ji), TRUE));
2259 mono_runtime_printf_err ("At <unmanaged>.");
2266 print_stack_frame_to_stderr (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
2268 MonoMethod *method = NULL;
2271 method = jinfo_get_method (frame->ji);
2274 gchar *location = mono_debug_print_stack_frame (method, frame->native_offset, mono_domain_get ());
2275 mono_runtime_printf_err (" %s", location);
2278 mono_runtime_printf_err (" at <unknown> <0x%05x>", frame->native_offset);
2283 static G_GNUC_UNUSED gboolean
2284 print_stack_frame_to_string (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
2286 GString *p = (GString*)data;
2287 MonoMethod *method = NULL;
2290 method = jinfo_get_method (frame->ji);
2293 gchar *location = mono_debug_print_stack_frame (method, frame->native_offset, mono_domain_get ());
2294 g_string_append_printf (p, " %s\n", location);
2297 g_string_append_printf (p, " at <unknown> <0x%05x>\n", frame->native_offset);
2302 static gboolean handling_sigsegv = FALSE;
2305 * mono_handle_native_sigsegv:
2307 * Handle a SIGSEGV received while in native code by printing diagnostic
2308 * information and aborting.
2311 mono_handle_native_sigsegv (int signal, void *ctx)
2313 #ifdef MONO_ARCH_USE_SIGACTION
2314 struct sigaction sa;
2316 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
2317 const char *signal_str = (signal == SIGSEGV) ? "SIGSEGV" : "SIGABRT";
2319 if (handling_sigsegv)
2322 if (mini_get_debug_options ()->suspend_on_sigsegv) {
2323 mono_runtime_printf_err ("Received SIGSEGV, suspending...");
2328 /* To prevent infinite loops when the stack walk causes a crash */
2329 handling_sigsegv = TRUE;
2331 /* !jit_tls means the thread was not registered with the runtime */
2332 if (jit_tls && mono_thread_internal_current ()) {
2333 mono_runtime_printf_err ("Stacktrace:\n");
2335 mono_walk_stack (print_stack_frame_to_stderr, TRUE, NULL);
2338 #ifdef HAVE_BACKTRACE_SYMBOLS
2344 mono_runtime_printf_err ("\nNative stacktrace:\n");
2346 size = backtrace (array, 256);
2347 names = backtrace_symbols (array, size);
2348 for (i =0; i < size; ++i) {
2349 mono_runtime_printf_err ("\t%s", names [i]);
2353 /* Try to get more meaningful information using gdb */
2355 #if !defined(HOST_WIN32) && defined(HAVE_SYS_SYSCALL_H) && defined(SYS_fork)
2356 if (!mini_get_debug_options ()->no_gdb_backtrace && !mono_debug_using_mono_debugger ()) {
2357 /* From g_spawn_command_line_sync () in eglib */
2360 pid_t crashed_pid = getpid ();
2364 * glibc fork acquires some locks, so if the crash happened inside malloc/free,
2365 * it will deadlock. Call the syscall directly instead.
2367 pid = mono_runtime_syscall_fork ();
2370 dup2 (STDERR_FILENO, STDOUT_FILENO);
2372 mono_gdb_render_native_backtraces (crashed_pid);
2376 mono_runtime_printf_err ("\nDebug info from gdb:\n");
2377 waitpid (pid, &status, 0);
2384 * A SIGSEGV indicates something went very wrong so we can no longer depend
2385 * on anything working. So try to print out lots of diagnostics, starting
2386 * with ones which have a greater chance of working.
2388 mono_runtime_printf_err (
2390 "=================================================================\n"
2391 "Got a %s while executing native code. This usually indicates\n"
2392 "a fatal error in the mono runtime or one of the native libraries \n"
2393 "used by your application.\n"
2394 "=================================================================\n",
2398 #ifdef MONO_ARCH_USE_SIGACTION
2400 /* Remove our SIGABRT handler */
2401 sa.sa_handler = SIG_DFL;
2402 sigemptyset (&sa.sa_mask);
2405 g_assert (sigaction (SIGABRT, &sa, NULL) != -1);
2409 /*Android abort is a fluke, it doesn't abort, it triggers another segv. */
2410 #if defined (PLATFORM_ANDROID)
2418 mono_print_thread_dump_internal (void *sigctx, MonoContext *start_ctx)
2420 MonoInternalThread *thread = mono_thread_internal_current ();
2421 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2424 GString* text = g_string_new (0);
2425 char *name, *wapi_desc;
2426 GError *error = NULL;
2429 name = g_utf16_to_utf8 (thread->name, thread->name_len, NULL, NULL, &error);
2431 g_string_append_printf (text, "\n\"%s\"", name);
2434 else if (thread->threadpool_thread)
2435 g_string_append (text, "\n\"<threadpool thread>\"");
2437 g_string_append (text, "\n\"<unnamed thread>\"");
2440 wapi_desc = wapi_current_thread_desc ();
2441 g_string_append_printf (text, " tid=0x%p this=0x%p %s\n", (gpointer)(gsize)thread->tid, thread, wapi_desc);
2445 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2447 memcpy (&ctx, start_ctx, sizeof (MonoContext));
2449 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, mono_print_thread_dump);
2451 mono_arch_sigctx_to_monoctx (sigctx, &ctx);
2453 mono_walk_stack_with_ctx (print_stack_frame_to_string, &ctx, MONO_UNWIND_LOOKUP_ALL, text);
2455 mono_runtime_printf ("\t<Stack traces in thread dumps not supported on this platform>");
2458 mono_runtime_printf ("%s", text->str);
2460 #if PLATFORM_WIN32 && TARGET_WIN32 && _DEBUG
2461 OutputDebugStringA(text->str);
2464 g_string_free (text, TRUE);
2465 mono_runtime_stdout_fflush ();
2469 * mono_print_thread_dump:
2471 * Print information about the current thread to stdout.
2472 * SIGCTX can be NULL, allowing this to be called from gdb.
2475 mono_print_thread_dump (void *sigctx)
2477 mono_print_thread_dump_internal (sigctx, NULL);
2481 mono_print_thread_dump_from_ctx (MonoContext *ctx)
2483 mono_print_thread_dump_internal (NULL, ctx);
2487 * mono_resume_unwind:
2489 * This is called by a trampoline from LLVM compiled finally clauses to continue
2493 mono_resume_unwind (MonoContext *ctx)
2495 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
2496 static void (*restore_context) (MonoContext *);
2497 MonoContext new_ctx;
2499 MONO_CONTEXT_SET_IP (ctx, MONO_CONTEXT_GET_IP (&jit_tls->resume_state.ctx));
2500 MONO_CONTEXT_SET_SP (ctx, MONO_CONTEXT_GET_SP (&jit_tls->resume_state.ctx));
2503 mono_handle_exception_internal (&new_ctx, jit_tls->resume_state.ex_obj, TRUE, NULL);
2505 if (!restore_context)
2506 restore_context = mono_get_restore_context ();
2508 restore_context (&new_ctx);
2511 #ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
2516 MonoJitExceptionInfo *ei;
2517 } FindHandlerBlockData;
2520 find_last_handler_block (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
2524 FindHandlerBlockData *pdata = data;
2525 MonoJitInfo *ji = frame->ji;
2530 ip = MONO_CONTEXT_GET_IP (ctx);
2532 for (i = 0; i < ji->num_clauses; ++i) {
2533 MonoJitExceptionInfo *ei = ji->clauses + i;
2534 if (ei->flags != MONO_EXCEPTION_CLAUSE_FINALLY)
2536 /*If ip points to the first instruction it means the handler block didn't start
2537 so we can leave its execution to the EH machinery*/
2538 if (ei->handler_start < ip && ip < ei->data.handler_end) {
2550 install_handler_block_guard (MonoJitInfo *ji, MonoContext *ctx)
2553 MonoJitExceptionInfo *clause = NULL;
2556 ip = MONO_CONTEXT_GET_IP (ctx);
2558 for (i = 0; i < ji->num_clauses; ++i) {
2559 clause = &ji->clauses [i];
2560 if (clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY)
2562 if (clause->handler_start < ip && clause->data.handler_end > ip)
2566 /*no matching finally */
2567 if (i == ji->num_clauses)
2570 /*If we stopped on the instruction right before the try, we haven't actually started executing it*/
2571 if (ip == clause->handler_start)
2574 return mono_arch_install_handler_block_guard (ji, clause, ctx, mono_create_handler_block_trampoline ());
2578 * Finds the bottom handler block running and install a block guard if needed.
2579 * FIXME add full-aot support.
2582 mono_install_handler_block_guard (MonoThreadUnwindState *ctx)
2584 FindHandlerBlockData data = { 0 };
2585 MonoJitTlsData *jit_tls = ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS];
2592 /* Guard against a null MonoJitTlsData. This can happens if the thread receives the
2593 * interrupt signal before the JIT has time to initialize its TLS data for the given thread.
2595 if (!jit_tls || jit_tls->handler_block_return_address)
2598 /* Do an async safe stack walk */
2599 mono_thread_info_set_is_async_context (TRUE);
2600 mono_walk_stack_with_state (find_last_handler_block, ctx, MONO_UNWIND_NONE, &data);
2601 mono_thread_info_set_is_async_context (FALSE);
2606 memcpy (&jit_tls->handler_block_context, &data.ctx, sizeof (MonoContext));
2608 resume_ip = install_handler_block_guard (data.ji, &data.ctx);
2609 if (resume_ip == NULL)
2612 jit_tls->handler_block_return_address = resume_ip;
2613 jit_tls->handler_block = data.ei;
2620 mono_install_handler_block_guard (MonoThreadUnwindState *ctx)
2628 mono_set_cast_details (MonoClass *from, MonoClass *to)
2630 MonoJitTlsData *jit_tls = NULL;
2632 if (mini_get_debug_options ()->better_cast_details) {
2633 jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
2634 jit_tls->class_cast_from = from;
2635 jit_tls->class_cast_to = to;
2640 /*returns false if the thread is not attached*/
2642 mono_thread_state_init_from_sigctx (MonoThreadUnwindState *ctx, void *sigctx)
2644 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2645 MonoInternalThread *thread = mono_thread_internal_current ();
2646 if (!thread || !thread->jit_data) {
2652 mono_arch_sigctx_to_monoctx (sigctx, &ctx->ctx);
2654 #if MONO_ARCH_HAS_MONO_CONTEXT && !defined(MONO_CROSS_COMPILE)
2655 MONO_CONTEXT_GET_CURRENT (ctx->ctx);
2657 g_error ("Use a null sigctx requires a working mono-context");
2660 ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
2661 ctx->unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
2662 ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = thread->jit_data;
2666 g_error ("Implement mono_arch_sigctx_to_monoctx for the current target");
2672 mono_thread_state_init_from_monoctx (MonoThreadUnwindState *ctx, MonoContext *mctx)
2674 MonoInternalThread *thread = mono_thread_internal_current ();
2675 if (!thread || !thread->jit_data) {
2681 ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
2682 ctx->unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
2683 ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = thread->jit_data;
2688 /*returns false if the thread is not attached*/
2690 mono_thread_state_init_from_current (MonoThreadUnwindState *ctx)
2692 MonoInternalThread *thread = mono_thread_internal_current ();
2693 MONO_ARCH_CONTEXT_DEF
2695 mono_arch_flush_register_windows ();
2697 if (!thread || !thread->jit_data) {
2701 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
2702 MONO_INIT_CONTEXT_FROM_CURRENT (&ctx->ctx);
2704 MONO_INIT_CONTEXT_FROM_FUNC (&ctx->ctx, mono_thread_state_init_from_current);
2707 ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
2708 ctx->unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
2709 ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = thread->jit_data;
2715 mono_raise_exception_with_ctx (MonoException *exc, MonoContext *ctx)
2717 void (*restore_context) (MonoContext *);
2718 restore_context = mono_get_restore_context ();
2720 mono_handle_exception (ctx, exc);
2721 restore_context (ctx);
2724 /*FIXME Move all monoctx -> sigctx conversion to signal handlers once all archs support utils/mono-context */
2726 mono_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
2728 #ifdef MONO_ARCH_HAVE_SETUP_ASYNC_CALLBACK
2729 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
2730 jit_tls->ex_ctx = *ctx;
2732 mono_arch_setup_async_callback (ctx, async_cb, user_data);
2734 g_error ("This target doesn't support mono_arch_setup_async_callback");
2739 mono_install_unhandled_exception_hook (MonoUnhandledExceptionFunc func, gpointer user_data)
2741 unhandled_exception_hook = func;
2742 unhandled_exception_hook_data = user_data;
2746 mono_invoke_unhandled_exception_hook (MonoObject *exc)
2748 if (unhandled_exception_hook) {
2749 unhandled_exception_hook (exc, unhandled_exception_hook_data);
2751 MonoObject *other = NULL;
2752 MonoString *str = mono_object_to_string (exc, &other);
2756 msg = mono_string_to_utf8 (str);
2758 char *original_backtrace = mono_exception_get_managed_backtrace ((MonoException*)exc);
2759 char *nested_backtrace = mono_exception_get_managed_backtrace ((MonoException*)other);
2761 msg = g_strdup_printf ("Nested exception detected.\nOriginal Exception: %s\nNested exception:%s\n",
2762 original_backtrace, nested_backtrace);
2764 g_free (original_backtrace);
2765 g_free (nested_backtrace);
2767 msg = g_strdup ("Nested exception trying to figure out what went wrong");
2769 mono_runtime_printf_err ("[ERROR] FATAL UNHANDLED EXCEPTION: %s", msg);
2771 #if defined(__APPLE__) && defined(__arm__)
2772 g_assertion_message ("Terminating runtime due to unhandled exception");
2774 exit (mono_environment_exitcode_get ());
2778 g_assert_not_reached ();