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).
21 #ifdef HAVE_EXECINFO_H
25 #ifdef HAVE_SYS_TYPES_H
26 #include <sys/types.h>
29 #ifdef HAVE_SYS_WAIT_H
37 #ifdef HAVE_SYS_SYSCALL_H
38 #include <sys/syscall.h>
41 #ifdef HAVE_SYS_PRCTL_H
42 #include <sys/prctl.h>
49 #include <mono/metadata/appdomain.h>
50 #include <mono/metadata/tabledefs.h>
51 #include <mono/metadata/threads.h>
52 #include <mono/metadata/threads-types.h>
53 #include <mono/metadata/debug-helpers.h>
54 #include <mono/metadata/exception.h>
55 #include <mono/metadata/exception-internals.h>
56 #include <mono/metadata/object-internals.h>
57 #include <mono/metadata/reflection-internals.h>
58 #include <mono/metadata/gc-internals.h>
59 #include <mono/metadata/mono-debug.h>
60 #include <mono/metadata/profiler.h>
61 #include <mono/metadata/mono-endian.h>
62 #include <mono/metadata/environment.h>
63 #include <mono/metadata/mono-mlist.h>
64 #include <mono/utils/mono-mmap.h>
65 #include <mono/utils/mono-logger-internals.h>
66 #include <mono/utils/mono-error.h>
67 #include <mono/utils/mono-error-internals.h>
71 #include "debugger-agent.h"
72 #include "seq-points.h"
73 #include "llvm-runtime.h"
74 #include "mini-llvm.h"
77 #include "mini-llvm-cpp.h"
80 #ifndef MONO_ARCH_CONTEXT_DEF
81 #define MONO_ARCH_CONTEXT_DEF
84 static gpointer restore_context_func, call_filter_func;
85 static gpointer throw_exception_func, rethrow_exception_func;
86 static gpointer throw_corlib_exception_func;
88 static gpointer try_more_restore_tramp = NULL;
89 static gpointer restore_stack_protection_tramp = NULL;
91 static MonoUnhandledExceptionFunc unhandled_exception_hook = NULL;
92 static gpointer unhandled_exception_hook_data = NULL;
94 static void try_more_restore (void);
95 static void restore_stack_protection (void);
96 static void mono_walk_stack_full (MonoJitStackWalk func, MonoContext *start_ctx, MonoDomain *domain, MonoJitTlsData *jit_tls, MonoLMF *lmf, MonoUnwindOptions unwind_options, gpointer user_data);
97 static void mono_raise_exception_with_ctx (MonoException *exc, MonoContext *ctx);
98 static void mono_runtime_walk_stack_with_ctx (MonoJitStackWalk func, MonoContext *start_ctx, MonoUnwindOptions unwind_options, void *user_data);
101 mono_exceptions_init (void)
103 MonoRuntimeExceptionHandlingCallbacks cbs;
105 restore_context_func = mono_aot_get_trampoline ("restore_context");
106 call_filter_func = mono_aot_get_trampoline ("call_filter");
107 throw_exception_func = mono_aot_get_trampoline ("throw_exception");
108 rethrow_exception_func = mono_aot_get_trampoline ("rethrow_exception");
112 restore_context_func = mono_arch_get_restore_context (&info, FALSE);
113 mono_tramp_info_register (info, NULL);
114 call_filter_func = mono_arch_get_call_filter (&info, FALSE);
115 mono_tramp_info_register (info, NULL);
116 throw_exception_func = mono_arch_get_throw_exception (&info, FALSE);
117 mono_tramp_info_register (info, NULL);
118 rethrow_exception_func = mono_arch_get_rethrow_exception (&info, FALSE);
119 mono_tramp_info_register (info, NULL);
121 #ifdef MONO_ARCH_HAVE_RESTORE_STACK_SUPPORT
122 if (!mono_llvm_only) {
123 try_more_restore_tramp = mono_create_specific_trampoline (try_more_restore, MONO_TRAMPOLINE_RESTORE_STACK_PROT, mono_domain_get (), NULL);
124 restore_stack_protection_tramp = mono_create_specific_trampoline (restore_stack_protection, MONO_TRAMPOLINE_RESTORE_STACK_PROT, mono_domain_get (), NULL);
128 #ifdef MONO_ARCH_HAVE_EXCEPTIONS_INIT
129 mono_arch_exceptions_init ();
131 cbs.mono_walk_stack_with_ctx = mono_runtime_walk_stack_with_ctx;
132 cbs.mono_walk_stack_with_state = mono_walk_stack_with_state;
135 cbs.mono_raise_exception = mono_llvm_raise_exception;
137 cbs.mono_raise_exception = (void (*)(MonoException *))mono_get_throw_exception ();
138 cbs.mono_raise_exception_with_ctx = mono_raise_exception_with_ctx;
139 cbs.mono_exception_walk_trace = mono_exception_walk_trace;
140 cbs.mono_install_handler_block_guard = mono_install_handler_block_guard;
141 mono_install_eh_callbacks (&cbs);
145 mono_get_throw_exception (void)
147 g_assert (throw_exception_func);
148 return throw_exception_func;
152 mono_get_rethrow_exception (void)
154 g_assert (rethrow_exception_func);
155 return rethrow_exception_func;
159 mono_get_call_filter (void)
161 g_assert (call_filter_func);
162 return call_filter_func;
166 mono_get_restore_context (void)
168 g_assert (restore_context_func);
169 return restore_context_func;
173 mono_get_throw_corlib_exception (void)
175 gpointer code = NULL;
178 /* This depends on corlib classes so cannot be inited in mono_exceptions_init () */
179 if (throw_corlib_exception_func)
180 return throw_corlib_exception_func;
183 code = mono_aot_get_trampoline ("throw_corlib_exception");
185 code = mono_arch_get_throw_corlib_exception (&info, FALSE);
186 mono_tramp_info_register (info, NULL);
189 mono_memory_barrier ();
191 throw_corlib_exception_func = code;
193 return throw_corlib_exception_func;
197 * mono_get_throw_exception_addr:
199 * Return an address which stores the result of
200 * mono_get_throw_exception.
203 mono_get_throw_exception_addr (void)
205 return &throw_exception_func;
209 is_address_protected (MonoJitInfo *ji, MonoJitExceptionInfo *ei, gpointer ip)
211 MonoTryBlockHoleTableJitInfo *table;
216 if (ei->try_start > ip || ip >= ei->try_end)
219 if (!ji->has_try_block_holes)
222 table = mono_jit_info_get_try_block_hole_table_info (ji);
223 offset = (guint32)((char*)ip - (char*)ji->code_start);
224 clause = (guint16)(ei - ji->clauses);
225 g_assert (clause < ji->num_clauses);
227 for (i = 0; i < table->num_holes; ++i) {
228 MonoTryBlockHoleJitInfo *hole = &table->holes [i];
229 if (hole->clause == clause && hole->offset <= offset && hole->offset + hole->length > offset)
235 #ifdef MONO_ARCH_HAVE_UNWIND_BACKTRACE
238 static gboolean show_native_addresses = TRUE;
240 static gboolean show_native_addresses = FALSE;
243 static _Unwind_Reason_Code
244 build_stack_trace (struct _Unwind_Context *frame_ctx, void *state)
246 MonoDomain *domain = mono_domain_get ();
247 uintptr_t ip = _Unwind_GetIP (frame_ctx);
249 if (show_native_addresses || mono_jit_info_table_find (domain, (char*)ip)) {
250 GList **trace_ips = (GList **)state;
251 *trace_ips = g_list_prepend (*trace_ips, (gpointer)ip);
254 return _URC_NO_REASON;
258 get_unwind_backtrace (void)
262 _Unwind_Backtrace (build_stack_trace, &ips);
264 return g_slist_reverse (ips);
270 get_unwind_backtrace (void)
280 * Translate between the mono_arch_unwind_frame function and the old API.
283 find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
284 MonoContext *new_ctx, MonoLMF **lmf, gboolean *managed)
286 StackFrameInfo frame;
289 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
291 /* Avoid costly table lookup during stack overflow */
292 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
295 ji = mini_jit_info_table_find (domain, (char *)ip, NULL);
300 err = mono_arch_unwind_frame (domain, jit_tls, ji, ctx, new_ctx, lmf, NULL, &frame);
302 return (MonoJitInfo *)-1;
304 if (*lmf && ((*lmf) != jit_tls->first_lmf) && ((gpointer)MONO_CONTEXT_GET_SP (new_ctx) >= (gpointer)(*lmf))) {
306 * Remove any unused lmf.
307 * Mask out the lower bits which might be used to hold additional information.
309 *lmf = (MonoLMF *)(((gsize)(*lmf)->previous_lmf) & ~(SIZEOF_VOID_P -1));
312 /* Convert between the new and the old APIs */
313 switch (frame.type) {
314 case FRAME_TYPE_MANAGED:
318 case FRAME_TYPE_TRAMPOLINE:
320 case FRAME_TYPE_MANAGED_TO_NATIVE:
324 memset (res, 0, sizeof (MonoJitInfo));
325 res->d.method = frame.method;
328 case FRAME_TYPE_DEBUGGER_INVOKE: {
332 * The normal exception handling code can't handle this frame, so just
335 ji = find_jit_info (domain, jit_tls, res, NULL, new_ctx, &tmp_ctx, lmf, managed);
336 memcpy (new_ctx, &tmp_ctx, sizeof (MonoContext));
340 g_assert_not_reached ();
345 /* mono_find_jit_info:
347 * This function is used to gather information from @ctx. It return the
348 * MonoJitInfo of the corresponding function, unwinds one stack frame and
349 * stores the resulting context into @new_ctx. It also stores a string
350 * describing the stack location into @trace (if not NULL), and modifies
351 * the @lmf if necessary. @native_offset return the IP offset from the
352 * start of the function or -1 if that info is not available.
355 mono_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
356 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
360 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
362 MonoMethod *method = NULL;
373 ji = find_jit_info (domain, jit_tls, res, prev_ji, ctx, new_ctx, lmf, &managed2);
375 if (ji == (gpointer)-1)
378 if (ji && !ji->is_trampoline)
379 method = jinfo_get_method (ji);
381 if (managed2 || (method && method->wrapper_type)) {
382 const char *real_ip, *start;
385 start = (const char *)ji->code_start;
387 /* ctx->ip points into native code */
388 real_ip = (const char*)MONO_CONTEXT_GET_IP (new_ctx);
390 real_ip = (const char*)ip;
392 if ((real_ip >= start) && (real_ip <= start + ji->code_size))
393 offset = real_ip - start;
398 *native_offset = offset;
401 if (!method->wrapper_type || method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
405 *trace = mono_debug_print_stack_frame (method, offset, domain);
408 char *fname = mono_method_full_name (jinfo_get_method (res), TRUE);
409 *trace = g_strdup_printf ("in (unmanaged) %s", fname);
418 * mono_find_jit_info_ext:
420 * A version of mono_find_jit_info which returns all data in the StackFrameInfo
422 * A note about frames of type FRAME_TYPE_MANAGED_TO_NATIVE:
423 * - These frames are used to mark managed-to-native transitions, so CTX will refer to native
424 * code, and new_ctx will refer to the last managed frame. The caller should unwind once more
425 * to obtain the last managed frame.
426 * If SAVE_LOCATIONS is not NULL, it should point to an array of size MONO_MAX_IREGS.
427 * On return, it will be filled with the locations where callee saved registers are saved
428 * by the current frame. This is returned outside of StackFrameInfo because it can be
429 * quite large on some platforms.
430 * If ASYNC true, this function will be async safe, but some fields of frame and frame->ji will
434 mono_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
435 MonoJitInfo *prev_ji, MonoContext *ctx,
436 MonoContext *new_ctx, char **trace, MonoLMF **lmf,
437 mgreg_t **save_locations,
438 StackFrameInfo *frame)
441 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
443 MonoDomain *target_domain = domain;
444 MonoMethod *method = NULL;
445 gboolean async = mono_thread_info_is_async_context ();
450 /* Avoid costly table lookup during stack overflow */
451 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
454 ji = mini_jit_info_table_find (domain, (char *)ip, &target_domain);
457 target_domain = domain;
460 memset (save_locations, 0, MONO_MAX_IREGS * sizeof (mgreg_t*));
462 err = mono_arch_unwind_frame (target_domain, jit_tls, ji, ctx, new_ctx, lmf, save_locations, frame);
466 if (*lmf && ((*lmf) != jit_tls->first_lmf) && ((gpointer)MONO_CONTEXT_GET_SP (new_ctx) >= (gpointer)(*lmf))) {
468 * Remove any unused lmf.
469 * Mask out the lower bits which might be used to hold additional information.
471 *lmf = (MonoLMF *)(((gsize)(*lmf)->previous_lmf) & ~(SIZEOF_VOID_P -1));
474 if (frame->ji && !frame->ji->is_trampoline && !frame->ji->async)
475 method = jinfo_get_method (frame->ji);
477 if (frame->type == FRAME_TYPE_MANAGED && method) {
478 if (!method->wrapper_type || method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
479 frame->managed = TRUE;
482 if (frame->type == FRAME_TYPE_MANAGED_TO_NATIVE) {
484 * This type of frame is just a marker, the caller should unwind once more to get the
485 * last managed frame.
488 frame->method = NULL;
491 frame->native_offset = -1;
492 frame->domain = target_domain;
493 frame->async_context = async;
497 if (frame->type == FRAME_TYPE_MANAGED)
498 frame->method = method;
500 if (ji && (frame->managed || (method && method->wrapper_type))) {
501 const char *real_ip, *start;
503 start = (const char *)ji->code_start;
504 if (frame->type == FRAME_TYPE_MANAGED)
505 real_ip = (const char*)ip;
507 /* ctx->ip points into native code */
508 real_ip = (const char*)MONO_CONTEXT_GET_IP (new_ctx);
510 if ((real_ip >= start) && (real_ip <= start + ji->code_size))
511 frame->native_offset = real_ip - start;
513 frame->native_offset = -1;
517 *trace = mono_debug_print_stack_frame (method, frame->native_offset, domain);
519 if (trace && frame->method) {
520 char *fname = mono_method_full_name (frame->method, TRUE);
521 *trace = g_strdup_printf ("in (unmanaged) %s", fname);
530 * This function is async-safe.
533 get_generic_info_from_stack_frame (MonoJitInfo *ji, MonoContext *ctx)
535 MonoGenericJitInfo *gi;
539 if (!ji->has_generic_jit_info)
541 gi = mono_jit_info_get_generic_jit_info (ji);
547 * Search location list if available, it contains the precise location of the
548 * argument for every pc offset, even if the method was interrupted while it was in
552 int offset = (mgreg_t)MONO_CONTEXT_GET_IP (ctx) - (mgreg_t)ji->code_start;
555 for (i = 0; i < gi->nlocs; ++i) {
556 MonoDwarfLocListEntry *entry = &gi->locations [i];
558 if (offset >= entry->from && (offset < entry->to || entry->to == 0)) {
560 info = (gpointer)mono_arch_context_get_int_reg (ctx, entry->reg);
562 info = *(gpointer*)(gpointer)((char*)mono_arch_context_get_int_reg (ctx, entry->reg) + entry->offset);
566 g_assert (i < gi->nlocs);
569 info = (gpointer)mono_arch_context_get_int_reg (ctx, gi->this_reg);
571 info = *(gpointer*)(gpointer)((char*)mono_arch_context_get_int_reg (ctx, gi->this_reg) +
575 method = jinfo_get_method (ji);
576 if (mono_method_get_context (method)->method_inst) {
577 /* A MonoMethodRuntimeGenericContext* */
579 } else if ((method->flags & METHOD_ATTRIBUTE_STATIC) || method->klass->valuetype) {
583 /* Avoid returning a managed object */
584 MonoObject *this_obj = (MonoObject *)info;
586 return this_obj->vtable;
591 * generic_info is either a MonoMethodRuntimeGenericContext or a MonoVTable.
593 static MonoGenericContext
594 get_generic_context_from_stack_frame (MonoJitInfo *ji, gpointer generic_info)
596 MonoGenericContext context = { NULL, NULL };
597 MonoClass *klass, *method_container_class;
600 g_assert (generic_info);
602 method = jinfo_get_method (ji);
603 g_assert (method->is_inflated);
604 if (mono_method_get_context (method)->method_inst) {
605 MonoMethodRuntimeGenericContext *mrgctx = (MonoMethodRuntimeGenericContext *)generic_info;
607 klass = mrgctx->class_vtable->klass;
608 context.method_inst = mrgctx->method_inst;
609 g_assert (context.method_inst);
611 MonoVTable *vtable = (MonoVTable *)generic_info;
613 klass = vtable->klass;
616 //g_assert (!method->klass->generic_container);
617 if (method->klass->generic_class)
618 method_container_class = method->klass->generic_class->container_class;
620 method_container_class = method->klass;
622 /* class might refer to a subclass of method's class */
623 while (!(klass == method->klass || (klass->generic_class && klass->generic_class->container_class == method_container_class))) {
624 klass = klass->parent;
628 if (klass->generic_class || klass->generic_container)
629 context.class_inst = mini_class_get_context (klass)->class_inst;
631 if (klass->generic_class)
632 g_assert (mono_class_has_parent_and_ignore_generics (klass->generic_class->container_class, method_container_class));
634 g_assert (mono_class_has_parent_and_ignore_generics (klass, method_container_class));
640 get_method_from_stack_frame (MonoJitInfo *ji, gpointer generic_info)
643 MonoGenericContext context;
646 if (!ji->has_generic_jit_info || !mono_jit_info_get_generic_jit_info (ji)->has_this)
647 return jinfo_get_method (ji);
648 context = get_generic_context_from_stack_frame (ji, generic_info);
650 method = jinfo_get_method (ji);
651 method = mono_method_get_declaring_generic_method (method);
652 method = mono_class_inflate_generic_method_checked (method, &context, &error);
653 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
659 * mono_exception_walk_native_trace:
660 * @ex: The exception object whose frames should be walked
661 * @func: callback to call for each stack frame
662 * @user_data: data passed to the callback
664 * This function walks the stacktrace of an exception. For
665 * each frame the callback function is called with the relevant info.
666 * The walk ends when no more stack frames are found or when the callback
667 * returns a TRUE value.
671 mono_exception_walk_trace (MonoException *ex, MonoExceptionFrameWalk func, gpointer user_data)
673 MonoDomain *domain = mono_domain_get ();
674 MonoArray *ta = ex->trace_ips;
680 len = mono_array_length (ta) >> 1;
681 for (i = 0; i < len; i++) {
682 gpointer ip = mono_array_get (ta, gpointer, i * 2 + 0);
683 gpointer generic_info = mono_array_get (ta, gpointer, i * 2 + 1);
684 MonoJitInfo *ji = mono_jit_info_table_find (domain, (char *)ip);
687 if (func (NULL, ip, 0, FALSE, user_data))
690 MonoMethod *method = get_method_from_stack_frame (ji, generic_info);
691 if (func (method, ji->code_start, (char *) ip - (char *) ji->code_start, TRUE, user_data))
700 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
703 MonoDomain *domain = mono_domain_get ();
705 MonoArray *ta = exc->trace_ips;
706 MonoDebugSourceLocation *location;
710 /* Exception is not thrown yet */
711 return mono_array_new (domain, mono_defaults.stack_frame_class, 0);
714 len = mono_array_length (ta) >> 1;
716 res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
718 for (i = skip; i < len; i++) {
720 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new_checked (domain, mono_defaults.stack_frame_class, &error);
721 if (!mono_error_ok (&error)) {
722 mono_error_set_pending_exception (&error);
725 gpointer ip = mono_array_get (ta, gpointer, i * 2 + 0);
726 gpointer generic_info = mono_array_get (ta, gpointer, i * 2 + 1);
729 ji = mono_jit_info_table_find (domain, (char *)ip);
731 /* Unmanaged frame */
732 mono_array_setref (res, i, sf);
736 g_assert (ji != NULL);
739 /* Can't resolve actual method */
740 method = jinfo_get_method (ji);
742 method = get_method_from_stack_frame (ji, generic_info);
743 if (jinfo_get_method (ji)->wrapper_type) {
747 s = mono_method_get_name_full (method, TRUE, FALSE, MONO_TYPE_NAME_FORMAT_REFLECTION);
748 MONO_OBJECT_SETREF (sf, internal_method_name, mono_string_new (domain, s));
752 MonoReflectionMethod *rm = mono_method_get_object_checked (domain, method, NULL, &error);
753 if (!mono_error_ok (&error)) {
754 mono_error_set_pending_exception (&error);
757 MONO_OBJECT_SETREF (sf, method, rm);
760 sf->method_index = ji->from_aot ? mono_aot_find_method_index (method) : 0xffffff;
761 sf->method_address = (gsize) ji->code_start;
762 sf->native_offset = (char *)ip - (char *)ji->code_start;
765 * mono_debug_lookup_source_location() returns both the file / line number information
766 * and the IL offset. Note that computing the IL offset is already an expensive
767 * operation, so we shouldn't call this method twice.
769 location = mono_debug_lookup_source_location (jinfo_get_method (ji), sf->native_offset, domain);
771 sf->il_offset = location->il_offset;
774 if (mono_find_prev_seq_point_for_native_offset (domain, jinfo_get_method (ji), sf->native_offset, NULL, &sp))
775 sf->il_offset = sp.il_offset;
780 if (need_file_info) {
781 if (location && location->source_file) {
782 MONO_OBJECT_SETREF (sf, filename, mono_string_new (domain, location->source_file));
783 sf->line = location->row;
784 sf->column = location->column;
786 sf->line = sf->column = 0;
791 mono_debug_free_source_location (location);
792 mono_array_setref (res, i, sf);
799 mono_runtime_walk_stack_with_ctx (MonoJitStackWalk func, MonoContext *start_ctx, MonoUnwindOptions unwind_options, void *user_data)
802 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
803 if (jit_tls && jit_tls->orig_ex_ctx_set)
804 start_ctx = &jit_tls->orig_ex_ctx;
806 mono_walk_stack_with_ctx (func, start_ctx, unwind_options, user_data);
809 * mono_walk_stack_with_ctx:
811 * Unwind the current thread starting at @start_ctx.
813 * If @start_ctx is null, we capture the current context.
816 mono_walk_stack_with_ctx (MonoJitStackWalk func, MonoContext *start_ctx, MonoUnwindOptions unwind_options, void *user_data)
818 MonoContext extra_ctx;
819 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
820 MONO_ARCH_CONTEXT_DEF
822 if (!thread || !thread->jit_data)
826 mono_arch_flush_register_windows ();
828 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
829 MONO_INIT_CONTEXT_FROM_CURRENT (&extra_ctx);
831 MONO_INIT_CONTEXT_FROM_FUNC (&extra_ctx, mono_walk_stack_with_ctx);
833 start_ctx = &extra_ctx;
836 mono_walk_stack_full (func, start_ctx, mono_domain_get (), (MonoJitTlsData *)thread->jit_data, mono_get_lmf (), unwind_options, user_data);
840 * mono_walk_stack_with_state:
842 * Unwind a thread described by @state.
844 * State must be valid (state->valid == TRUE).
846 * If you are using this function to unwind another thread, make sure it is suspended.
848 * If @state is null, we capture the current context.
851 mono_walk_stack_with_state (MonoJitStackWalk func, MonoThreadUnwindState *state, MonoUnwindOptions unwind_options, void *user_data)
853 MonoThreadUnwindState extra_state;
855 g_assert (!mono_thread_info_is_async_context ());
856 if (!mono_thread_state_init_from_current (&extra_state))
858 state = &extra_state;
861 g_assert (state->valid);
863 if (!state->unwind_data [MONO_UNWIND_DATA_DOMAIN])
867 mono_walk_stack_full (func,
869 (MonoDomain *)state->unwind_data [MONO_UNWIND_DATA_DOMAIN],
870 (MonoJitTlsData *)state->unwind_data [MONO_UNWIND_DATA_JIT_TLS],
871 (MonoLMF *)state->unwind_data [MONO_UNWIND_DATA_LMF],
872 unwind_options, user_data);
876 mono_walk_stack (MonoJitStackWalk func, MonoUnwindOptions options, void *user_data)
878 MonoThreadUnwindState state;
879 if (!mono_thread_state_init_from_current (&state))
881 mono_walk_stack_with_state (func, &state, options, user_data);
885 * mono_walk_stack_full:
886 * @func: callback to call for each stack frame
887 * @domain: starting appdomain, can be NULL to use the current domain
888 * @unwind_options: what extra information the unwinder should gather
889 * @start_ctx: starting state of the stack walk, can be NULL.
890 * @thread: the thread whose stack to walk, can be NULL to use the current thread
891 * @lmf: the LMF of @thread, can be NULL to use the LMF of the current thread
892 * @user_data: data passed to the callback
894 * This function walks the stack of a thread, starting from the state
895 * represented by start_ctx. For each frame the callback
896 * function is called with the relevant info. The walk ends when no more
897 * managed stack frames are found or when the callback returns a TRUE value.
900 mono_walk_stack_full (MonoJitStackWalk func, MonoContext *start_ctx, MonoDomain *domain, MonoJitTlsData *jit_tls, MonoLMF *lmf, MonoUnwindOptions unwind_options, gpointer user_data)
903 MonoContext ctx, new_ctx;
904 StackFrameInfo frame;
906 mgreg_t *reg_locations [MONO_MAX_IREGS];
907 mgreg_t *new_reg_locations [MONO_MAX_IREGS];
908 gboolean get_reg_locations = unwind_options & MONO_UNWIND_REG_LOCATIONS;
909 gboolean async = mono_thread_info_is_async_context ();
911 if (mono_llvm_only) {
917 ips = get_unwind_backtrace ();
918 for (l = ips; l; l = l->next) {
919 guint8 *ip = (guint8*)l->data;
920 memset (&frame, 0, sizeof (StackFrameInfo));
921 frame.ji = mini_jit_info_table_find (domain, (char*)ip, &frame.domain);
922 if (!frame.ji || frame.ji->is_trampoline)
924 frame.type = FRAME_TYPE_MANAGED;
925 frame.method = jinfo_get_method (frame.ji);
926 // FIXME: Cannot lookup the actual method
927 frame.actual_method = frame.method;
928 if (frame.type == FRAME_TYPE_MANAGED) {
929 if (!frame.method->wrapper_type || frame.method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
930 frame.managed = TRUE;
932 frame.native_offset = ip - (guint8*)frame.ji->code_start;
933 frame.il_offset = -1;
935 if (func (&frame, NULL, user_data))
942 g_assert (start_ctx);
945 /*The LMF will be null if the target have no managed frames.*/
946 /* g_assert (lmf); */
949 g_assert (unwind_options == MONO_UNWIND_NONE);
951 memcpy (&ctx, start_ctx, sizeof (MonoContext));
952 memset (reg_locations, 0, sizeof (reg_locations));
954 while (MONO_CONTEXT_GET_SP (&ctx) < jit_tls->end_of_stack) {
956 res = mono_find_jit_info_ext (domain, jit_tls, NULL, &ctx, &new_ctx, NULL, &lmf, get_reg_locations ? new_reg_locations : NULL, &frame);
960 if ((unwind_options & MONO_UNWIND_LOOKUP_IL_OFFSET) && frame.ji && !frame.ji->is_trampoline) {
961 MonoDebugSourceLocation *source;
963 source = mono_debug_lookup_source_location (jinfo_get_method (frame.ji), frame.native_offset, domain);
965 il_offset = source->il_offset;
968 if (mono_find_prev_seq_point_for_native_offset (domain, jinfo_get_method (frame.ji), frame.native_offset, NULL, &sp))
969 il_offset = sp.il_offset;
973 mono_debug_free_source_location (source);
977 frame.il_offset = il_offset;
979 if ((unwind_options & MONO_UNWIND_LOOKUP_ACTUAL_METHOD) && frame.ji && !frame.ji->is_trampoline) {
980 frame.actual_method = get_method_from_stack_frame (frame.ji, get_generic_info_from_stack_frame (frame.ji, &ctx));
982 frame.actual_method = frame.method;
985 if (get_reg_locations)
986 frame.reg_locations = reg_locations;
988 if (func (&frame, &ctx, user_data))
991 if (get_reg_locations) {
992 for (i = 0; i < MONO_MAX_IREGS; ++i)
993 if (new_reg_locations [i])
994 reg_locations [i] = new_reg_locations [i];
1002 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
1003 MonoReflectionMethod **method,
1004 gint32 *iloffset, gint32 *native_offset,
1005 MonoString **file, gint32 *line, gint32 *column)
1008 MonoDomain *domain = mono_domain_get ();
1009 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
1010 MonoLMF *lmf = mono_get_lmf ();
1011 MonoJitInfo *ji = NULL;
1012 MonoContext ctx, new_ctx;
1013 MonoDebugSourceLocation *location;
1014 MonoMethod *jmethod = NULL, *actual_method;
1015 StackFrameInfo frame;
1018 MONO_ARCH_CONTEXT_DEF;
1020 if (mono_llvm_only) {
1022 MonoDomain *frame_domain;
1023 guint8 *frame_ip = NULL;
1025 /* FIXME: Generalize this code with an interface which returns an array of StackFrame structures */
1027 ips = get_unwind_backtrace ();
1028 for (l = ips; l && skip >= 0; l = l->next) {
1029 guint8 *ip = (guint8*)l->data;
1033 ji = mini_jit_info_table_find (mono_domain_get (), (char*)ip, &frame_domain);
1034 if (!ji || ji->is_trampoline)
1037 /* The skip count passed by the caller depends on us not filtering out MANAGED_TO_NATIVE */
1038 jmethod = jinfo_get_method (ji);
1039 if (jmethod->wrapper_type != MONO_WRAPPER_NONE && jmethod->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD && jmethod->wrapper_type != MONO_WRAPPER_MANAGED_TO_NATIVE)
1046 /* No way to resolve generic instances */
1047 actual_method = jmethod;
1048 *native_offset = frame_ip - (guint8*)ji->code_start;
1050 mono_arch_flush_register_windows ();
1052 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
1053 MONO_INIT_CONTEXT_FROM_CURRENT (&ctx);
1055 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_get_frame_info);
1061 res = mono_find_jit_info_ext (domain, jit_tls, NULL, &ctx, &new_ctx, NULL, &lmf, NULL, &frame);
1065 if (frame.type == FRAME_TYPE_MANAGED_TO_NATIVE ||
1066 frame.type == FRAME_TYPE_DEBUGGER_INVOKE ||
1067 frame.type == FRAME_TYPE_TRAMPOLINE)
1071 *native_offset = frame.native_offset;
1073 /* The skip count passed by the caller depends on us not filtering out MANAGED_TO_NATIVE */
1074 jmethod = jinfo_get_method (ji);
1075 if (jmethod->wrapper_type != MONO_WRAPPER_NONE && jmethod->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD && jmethod->wrapper_type != MONO_WRAPPER_MANAGED_TO_NATIVE)
1078 } while (skip >= 0);
1080 actual_method = get_method_from_stack_frame (ji, get_generic_info_from_stack_frame (ji, &ctx));
1083 MonoReflectionMethod *rm = mono_method_get_object_checked (domain, actual_method, NULL, &error);
1084 if (!mono_error_ok (&error)) {
1085 mono_error_set_pending_exception (&error);
1088 mono_gc_wbarrier_generic_store (method, (MonoObject*) rm);
1090 location = mono_debug_lookup_source_location (jmethod, *native_offset, domain);
1092 *iloffset = location->il_offset;
1096 if (need_file_info) {
1098 mono_gc_wbarrier_generic_store (file, (MonoObject*) mono_string_new (domain, location->source_file));
1099 *line = location->row;
1100 *column = location->column;
1103 *line = *column = 0;
1107 mono_debug_free_source_location (location);
1113 get_exception_catch_class (MonoJitExceptionInfo *ei, MonoJitInfo *ji, MonoContext *ctx)
1116 MonoClass *catch_class = ei->data.catch_class;
1117 MonoType *inflated_type;
1118 MonoGenericContext context;
1120 /*MonoJitExceptionInfo::data is an union used by filter and finally clauses too.*/
1121 if (!catch_class || ei->flags != MONO_EXCEPTION_CLAUSE_NONE)
1124 if (!ji->has_generic_jit_info || !mono_jit_info_get_generic_jit_info (ji)->has_this)
1126 context = get_generic_context_from_stack_frame (ji, get_generic_info_from_stack_frame (ji, ctx));
1128 /* FIXME: we shouldn't inflate but instead put the
1129 type in the rgctx and fetch it from there. It
1130 might be a good idea to do this lazily, i.e. only
1131 when the exception is actually thrown, so as not to
1132 waste space for exception clauses which might never
1134 inflated_type = mono_class_inflate_generic_type_checked (&catch_class->byval_arg, &context, &error);
1135 mono_error_assert_ok (&error); /* FIXME don't swallow the error */
1137 catch_class = mono_class_from_mono_type (inflated_type);
1138 mono_metadata_free_type (inflated_type);
1144 * mini_jit_info_table_find_ext:
1146 * Same as mono_jit_info_table_find, but search all the domains of the current thread
1147 * if ADDR is not found in DOMAIN. The domain where the method was found is stored into
1148 * OUT_DOMAIN if it is not NULL.
1151 mini_jit_info_table_find_ext (MonoDomain *domain, char *addr, gboolean allow_trampolines, MonoDomain **out_domain)
1154 MonoInternalThread *t = mono_thread_internal_current ();
1160 ji = mono_jit_info_table_find_internal (domain, addr, TRUE, allow_trampolines);
1163 *out_domain = domain;
1167 /* maybe it is shared code, so we also search in the root domain */
1168 if (domain != mono_get_root_domain ()) {
1169 ji = mono_jit_info_table_find_internal (mono_get_root_domain (), addr, TRUE, allow_trampolines);
1172 *out_domain = mono_get_root_domain ();
1180 refs = (gpointer *)((t->appdomain_refs) ? *(gpointer *) t->appdomain_refs : NULL);
1181 for (; refs && *refs; refs++) {
1182 if (*refs != domain && *refs != mono_get_root_domain ()) {
1183 ji = mono_jit_info_table_find_internal ((MonoDomain*) *refs, addr, TRUE, allow_trampolines);
1186 *out_domain = (MonoDomain*) *refs;
1196 mini_jit_info_table_find (MonoDomain *domain, char *addr, MonoDomain **out_domain)
1198 return mini_jit_info_table_find_ext (domain, addr, FALSE, out_domain);
1201 /* Class lazy loading functions */
1202 static GENERATE_GET_CLASS_WITH_CACHE (runtime_compat_attr, System.Runtime.CompilerServices, RuntimeCompatibilityAttribute)
1205 * wrap_non_exception_throws:
1207 * Determine whenever M's assembly has a RuntimeCompatibilityAttribute with the
1208 * WrapNonExceptionThrows flag set.
1211 wrap_non_exception_throws (MonoMethod *m)
1214 MonoAssembly *ass = m->klass->image->assembly;
1215 MonoCustomAttrInfo* attrs;
1218 gboolean val = FALSE;
1221 if (ass->wrap_non_exception_throws_inited)
1222 return ass->wrap_non_exception_throws;
1224 klass = mono_class_get_runtime_compat_attr_class ();
1226 attrs = mono_custom_attrs_from_assembly_checked (ass, &error);
1227 mono_error_cleanup (&error); /* FIXME don't swallow the error */
1229 for (i = 0; i < attrs->num_attrs; ++i) {
1230 MonoCustomAttrEntry *attr = &attrs->attrs [i];
1232 int num_named, named_type, name_len;
1235 if (!attr->ctor || attr->ctor->klass != klass)
1237 /* Decode the RuntimeCompatibilityAttribute. See reflection.c */
1238 p = (const char*)attr->data;
1239 g_assert (read16 (p) == 0x0001);
1241 num_named = read16 (p);
1247 /* data_type = *p; */
1250 if (named_type != 0x54)
1252 name_len = mono_metadata_decode_blob_size (p, &p);
1253 name = (char *)g_malloc (name_len + 1);
1254 memcpy (name, p, name_len);
1255 name [name_len] = 0;
1257 g_assert (!strcmp (name, "WrapNonExceptionThrows"));
1259 /* The value is a BOOLEAN */
1262 mono_custom_attrs_free (attrs);
1265 ass->wrap_non_exception_throws = val;
1266 mono_memory_barrier ();
1267 ass->wrap_non_exception_throws_inited = TRUE;
1272 #ifndef MONO_ARCH_STACK_GROWS_UP
1273 #define DOES_STACK_GROWS_UP 1
1275 #define DOES_STACK_GROWS_UP 0
1278 #define MAX_UNMANAGED_BACKTRACE 128
1280 build_native_trace (void)
1282 /* This puppy only makes sense on mobile, IOW, ARM. */
1283 #if defined (HAVE_BACKTRACE_SYMBOLS) && defined (TARGET_ARM)
1285 void *native_trace [MAX_UNMANAGED_BACKTRACE];
1286 int size = backtrace (native_trace, MAX_UNMANAGED_BACKTRACE);
1291 res = mono_array_new (mono_domain_get (), mono_defaults.int_class, size);
1293 for (i = 0; i < size; i++)
1294 mono_array_set (res, gpointer, i, native_trace [i]);
1302 setup_stack_trace (MonoException *mono_ex, GSList *dynamic_methods, MonoArray *initial_trace_ips, GList **trace_ips)
1304 if (mono_ex && !initial_trace_ips) {
1305 *trace_ips = g_list_reverse (*trace_ips);
1306 MONO_OBJECT_SETREF (mono_ex, trace_ips, mono_glist_to_array (*trace_ips, mono_defaults.int_class));
1307 MONO_OBJECT_SETREF (mono_ex, native_trace_ips, build_native_trace ());
1308 if (dynamic_methods) {
1309 /* These methods could go away anytime, so save a reference to them in the exception object */
1311 MonoMList *list = NULL;
1313 for (l = dynamic_methods; l; l = l->next) {
1315 MonoDomain *domain = mono_domain_get ();
1317 if (domain->method_to_dyn_method) {
1318 mono_domain_lock (domain);
1319 dis_link = (guint32)(size_t)g_hash_table_lookup (domain->method_to_dyn_method, l->data);
1320 mono_domain_unlock (domain);
1322 MonoObject *o = mono_gchandle_get_target (dis_link);
1324 list = mono_mlist_prepend (list, o);
1330 MONO_OBJECT_SETREF (mono_ex, dynamic_methods, list);
1333 g_list_free (*trace_ips);
1338 * mono_handle_exception_internal_first_pass:
1340 * The first pass of exception handling. Unwind the stack until a catch clause which can catch
1341 * OBJ is found. Run the index of the filter clause which caught the exception into
1342 * OUT_FILTER_IDX. Return TRUE if the exception is caught, FALSE otherwise.
1345 mono_handle_exception_internal_first_pass (MonoContext *ctx, MonoObject *obj, gint32 *out_filter_idx, MonoJitInfo **out_ji, MonoJitInfo **out_prev_ji, MonoObject *non_exception)
1348 MonoDomain *domain = mono_domain_get ();
1349 MonoJitInfo *ji = NULL;
1350 static int (*call_filter) (MonoContext *, gpointer) = NULL;
1351 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
1352 MonoLMF *lmf = mono_get_lmf ();
1353 MonoArray *initial_trace_ips = NULL;
1354 GList *trace_ips = NULL;
1355 GSList *dynamic_methods = NULL;
1356 MonoException *mono_ex;
1357 gboolean stack_overflow = FALSE;
1358 MonoContext initial_ctx;
1360 int frame_count = 0;
1365 g_assert (ctx != NULL);
1367 if (obj == (MonoObject *)domain->stack_overflow_ex)
1368 stack_overflow = TRUE;
1370 mono_ex = (MonoException*)obj;
1371 initial_trace_ips = mono_ex->trace_ips;
1373 if (mono_object_isinst_checked (obj, mono_defaults.exception_class, &error)) {
1374 mono_ex = (MonoException*)obj;
1375 initial_trace_ips = mono_ex->trace_ips;
1377 mono_error_assert_ok (&error);
1382 call_filter = (int (*) (MonoContext *, void *))mono_get_call_filter ();
1384 g_assert (jit_tls->end_of_stack);
1385 g_assert (jit_tls->abort_func);
1388 *out_filter_idx = -1;
1392 *out_prev_ji = NULL;
1397 MonoContext new_ctx;
1399 int clause_index_start = 0;
1400 gboolean unwind_res = TRUE;
1402 StackFrameInfo frame;
1407 unwind_res = mono_find_jit_info_ext (domain, jit_tls, NULL, ctx, &new_ctx, NULL, &lmf, NULL, &frame);
1409 if (frame.type == FRAME_TYPE_DEBUGGER_INVOKE ||
1410 frame.type == FRAME_TYPE_MANAGED_TO_NATIVE ||
1411 frame.type == FRAME_TYPE_TRAMPOLINE) {
1415 g_assert (frame.type == FRAME_TYPE_MANAGED);
1420 setup_stack_trace (mono_ex, dynamic_methods, initial_trace_ips, &trace_ips);
1421 g_slist_free (dynamic_methods);
1426 method = jinfo_get_method (ji);
1427 //printf ("M: %s %d.\n", mono_method_full_name (method, TRUE), frame_count);
1429 if (mini_get_debug_options ()->reverse_pinvoke_exceptions && method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
1430 g_error ("A native frame was found while unwinding the stack after an exception.\n"
1431 "The native frame called the managed method:\n%s\n",
1432 mono_method_full_name (method, TRUE));
1435 if (method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
1437 * Avoid overwriting the stack trace if the exception is
1438 * rethrown. Also avoid giant stack traces during a stack
1441 if (!initial_trace_ips && (frame_count < 1000)) {
1442 trace_ips = g_list_prepend (trace_ips, MONO_CONTEXT_GET_IP (ctx));
1443 trace_ips = g_list_prepend (trace_ips,
1444 get_generic_info_from_stack_frame (ji, ctx));
1448 if (method->dynamic)
1449 dynamic_methods = g_slist_prepend (dynamic_methods, method);
1451 if (stack_overflow) {
1452 if (DOES_STACK_GROWS_UP)
1453 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx));
1455 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (ctx));
1457 free_stack = 0xffffff;
1460 for (i = clause_index_start; i < ji->num_clauses; i++) {
1461 MonoJitExceptionInfo *ei = &ji->clauses [i];
1462 gboolean filtered = FALSE;
1465 * During stack overflow, wait till the unwinding frees some stack
1466 * space before running handlers/finalizers.
1468 if (free_stack <= (64 * 1024))
1471 if (is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (ctx))) {
1473 MonoClass *catch_class = get_exception_catch_class (ei, ji, ctx);
1476 * Have to unwrap RuntimeWrappedExceptions if the
1477 * method's assembly doesn't have a RuntimeCompatibilityAttribute.
1479 if (non_exception && !wrap_non_exception_throws (method))
1480 ex_obj = non_exception;
1484 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
1485 gboolean is_user_frame = method->wrapper_type == MONO_WRAPPER_NONE || method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD;
1486 #ifndef DISABLE_PERFCOUNTERS
1487 mono_perfcounters->exceptions_filters++;
1490 Here's the thing, if this is a filter clause done by a wrapper like runtime invoke, we don't want to
1491 trim the stackframe since if it returns FALSE we lose information.
1493 FIXME Not 100% sure if it's a good idea even with user clauses.
1496 setup_stack_trace (mono_ex, dynamic_methods, initial_trace_ips, &trace_ips);
1498 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
1500 MONO_CONTEXT_SET_LLVM_EXC_REG (ctx, ex_obj);
1502 /* Can't pass the ex object in a register yet to filter clauses, because call_filter () might not support it */
1503 *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = ex_obj;
1505 g_assert (!ji->from_llvm);
1506 /* store the exception object in bp + ei->exvar_offset */
1507 *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = ex_obj;
1510 #ifdef MONO_CONTEXT_SET_LLVM_EH_SELECTOR_REG
1512 * Pass the original il clause index to the landing pad so it can
1513 * branch to the landing pad associated with the il clause.
1514 * This is needed because llvm compiled code assumes that the EH
1515 * code always branches to the innermost landing pad.
1518 MONO_CONTEXT_SET_LLVM_EH_SELECTOR_REG (ctx, ei->clause_index);
1521 mono_debugger_agent_begin_exception_filter (mono_ex, ctx, &initial_ctx);
1522 filtered = call_filter (ctx, ei->data.filter);
1523 mono_debugger_agent_end_exception_filter (mono_ex, ctx, &initial_ctx);
1524 if (filtered && out_filter_idx)
1525 *out_filter_idx = filter_idx;
1532 setup_stack_trace (mono_ex, dynamic_methods, initial_trace_ips, &trace_ips);
1533 g_slist_free (dynamic_methods);
1534 /* mono_debugger_agent_handle_exception () needs this */
1535 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1540 if (ei->flags == MONO_EXCEPTION_CLAUSE_NONE && mono_object_isinst_checked (ex_obj, catch_class, &error)) {
1541 setup_stack_trace (mono_ex, dynamic_methods, initial_trace_ips, &trace_ips);
1542 g_slist_free (dynamic_methods);
1547 /* mono_debugger_agent_handle_exception () needs this */
1548 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1551 mono_error_cleanup (&error);
1558 g_assert_not_reached ();
1562 * mono_handle_exception_internal:
1563 * @ctx: saved processor state
1564 * @obj: the exception object
1565 * @resume: whenever to resume unwinding based on the state in MonoJitTlsData.
1568 mono_handle_exception_internal (MonoContext *ctx, MonoObject *obj, gboolean resume, MonoJitInfo **out_ji)
1571 MonoDomain *domain = mono_domain_get ();
1572 MonoJitInfo *ji, *prev_ji;
1573 static int (*call_filter) (MonoContext *, gpointer) = NULL;
1574 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
1575 MonoLMF *lmf = mono_get_lmf ();
1576 MonoException *mono_ex;
1577 gboolean stack_overflow = FALSE;
1578 MonoContext initial_ctx;
1580 int frame_count = 0;
1581 gint32 filter_idx, first_filter_idx = 0;
1584 MonoObject *non_exception = NULL;
1586 g_assert (ctx != NULL);
1588 MonoException *ex = mono_get_exception_null_reference ();
1589 MONO_OBJECT_SETREF (ex, message, mono_string_new (domain, "Object reference not set to an instance of an object"));
1590 obj = (MonoObject *)ex;
1594 * Allocate a new exception object instead of the preconstructed ones.
1596 if (obj == (MonoObject *)domain->stack_overflow_ex) {
1598 * It is not a good idea to try and put even more pressure on the little stack available.
1599 * obj = mono_get_exception_stack_overflow ();
1601 stack_overflow = TRUE;
1603 else if (obj == (MonoObject *)domain->null_reference_ex) {
1604 obj = (MonoObject *)mono_get_exception_null_reference ();
1607 if (!mono_object_isinst_checked (obj, mono_defaults.exception_class, &error)) {
1608 mono_error_assert_ok (&error);
1609 non_exception = obj;
1610 obj = (MonoObject *)mono_get_exception_runtime_wrapped_checked (obj, &error);
1611 mono_error_assert_ok (&error);
1614 mono_ex = (MonoException*)obj;
1616 if (mini_get_debug_options ()->suspend_on_exception) {
1617 mono_runtime_printf_err ("Exception thrown, suspending...");
1622 if (mono_object_isinst_checked (obj, mono_defaults.exception_class, &error)) {
1623 mono_ex = (MonoException*)obj;
1625 mono_error_assert_ok (&error);
1629 if (mono_ex && jit_tls->class_cast_from) {
1630 if (!strcmp (mono_ex->object.vtable->klass->name, "InvalidCastException")) {
1631 char *from_name = mono_type_get_full_name (jit_tls->class_cast_from);
1632 char *to_name = mono_type_get_full_name (jit_tls->class_cast_to);
1633 char *msg = g_strdup_printf ("Unable to cast object of type '%s' to type '%s'.", from_name, to_name);
1634 mono_ex->message = mono_string_new (domain, msg);
1639 if (!strcmp (mono_ex->object.vtable->klass->name, "ArrayTypeMismatchException")) {
1640 char *from_name = mono_type_get_full_name (jit_tls->class_cast_from);
1641 char *to_name = mono_type_get_full_name (jit_tls->class_cast_to);
1642 char *msg = g_strdup_printf ("Source array of type '%s' cannot be cast to destination array type '%s'.", from_name, to_name);
1643 mono_ex->message = mono_string_new (domain, msg);
1651 call_filter = (int (*)(MonoContext *, void*))mono_get_call_filter ();
1653 g_assert (jit_tls->end_of_stack);
1654 g_assert (jit_tls->abort_func);
1657 * We set orig_ex_ctx_set to TRUE/FALSE around profiler calls to make sure it doesn't
1658 * end up being TRUE on any code path.
1660 memcpy (&jit_tls->orig_ex_ctx, ctx, sizeof (MonoContext));
1665 MonoContext ctx_cp = *ctx;
1666 if (mono_trace_is_enabled ()) {
1667 MonoMethod *system_exception_get_message = mono_class_get_method_from_name (mono_defaults.exception_class, "get_Message", 0);
1668 MonoMethod *get_message = system_exception_get_message == NULL ? NULL : mono_object_get_virtual_method (obj, system_exception_get_message);
1669 MonoObject *message;
1670 const char *type_name = mono_class_get_name (mono_object_class (mono_ex));
1672 if (get_message == NULL) {
1674 } else if (!strcmp (type_name, "OutOfMemoryException") || !strcmp (type_name, "StackOverflowException")) {
1676 msg = g_strdup_printf ("(No exception message for: %s)\n", type_name);
1678 MonoObject *exc = NULL;
1679 message = mono_runtime_try_invoke (get_message, obj, NULL, &exc, &error);
1680 g_assert (exc == NULL);
1681 mono_error_assert_ok (&error);
1684 msg = message ? mono_string_to_utf8 ((MonoString *) message) : g_strdup ("(System.Exception.Message property not available)");
1686 g_print ("[%p:] EXCEPTION handling: %s.%s: %s\n", (void*)mono_native_thread_id_get (), mono_object_class (obj)->name_space, mono_object_class (obj)->name, msg);
1688 if (mono_ex && mono_trace_eval_exception (mono_object_class (mono_ex)))
1689 mono_print_thread_dump_from_ctx (ctx);
1691 jit_tls->orig_ex_ctx_set = TRUE;
1692 mono_profiler_exception_thrown (obj);
1693 jit_tls->orig_ex_ctx_set = FALSE;
1695 res = mono_handle_exception_internal_first_pass (&ctx_cp, obj, &first_filter_idx, &ji, &prev_ji, non_exception);
1698 if (mini_get_debug_options ()->break_on_exc)
1700 mono_debugger_agent_handle_exception ((MonoException *)obj, ctx, NULL);
1702 if (mini_get_debug_options ()->suspend_on_unhandled) {
1703 mono_runtime_printf_err ("Unhandled exception, suspending...");
1708 // FIXME: This runs managed code so it might cause another stack overflow when
1709 // we are handling a stack overflow
1710 mono_unhandled_exception (obj);
1712 gboolean unhandled = FALSE;
1715 * The exceptions caught by the mono_runtime_invoke_checked () calls
1716 * in the threadpool needs to be treated as unhandled (#669836).
1718 * FIXME: The check below is hackish, but its hard to distinguish
1719 * these runtime invoke calls from others in the runtime.
1721 if (ji && jinfo_get_method (ji)->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
1722 if (prev_ji && jinfo_get_method (prev_ji) == mono_defaults.threadpool_perform_wait_callback_method)
1727 mono_debugger_agent_handle_exception ((MonoException *)obj, ctx, NULL);
1729 mono_debugger_agent_handle_exception ((MonoException *)obj, ctx, &ctx_cp);
1739 MonoContext new_ctx;
1741 int clause_index_start = 0;
1742 gboolean unwind_res = TRUE;
1746 ji = jit_tls->resume_state.ji;
1747 new_ctx = jit_tls->resume_state.new_ctx;
1748 clause_index_start = jit_tls->resume_state.clause_index;
1749 lmf = jit_tls->resume_state.lmf;
1750 first_filter_idx = jit_tls->resume_state.first_filter_idx;
1751 filter_idx = jit_tls->resume_state.filter_idx;
1753 StackFrameInfo frame;
1755 unwind_res = mono_find_jit_info_ext (domain, jit_tls, NULL, ctx, &new_ctx, NULL, &lmf, NULL, &frame);
1757 if (frame.type == FRAME_TYPE_DEBUGGER_INVOKE ||
1758 frame.type == FRAME_TYPE_MANAGED_TO_NATIVE ||
1759 frame.type == FRAME_TYPE_TRAMPOLINE) {
1763 g_assert (frame.type == FRAME_TYPE_MANAGED);
1769 *(mono_get_lmf_addr ()) = lmf;
1771 jit_tls->abort_func (obj);
1772 g_assert_not_reached ();
1775 method = jinfo_get_method (ji);
1777 //printf ("M: %s %d.\n", mono_method_full_name (method, TRUE), frame_count);
1779 if (stack_overflow) {
1780 if (DOES_STACK_GROWS_UP)
1781 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx));
1783 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (ctx));
1785 free_stack = 0xffffff;
1788 for (i = clause_index_start; i < ji->num_clauses; i++) {
1789 MonoJitExceptionInfo *ei = &ji->clauses [i];
1790 gboolean filtered = FALSE;
1793 * During stack overflow, wait till the unwinding frees some stack
1794 * space before running handlers/finalizers.
1796 if (free_stack <= (64 * 1024))
1799 if (is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (ctx))) {
1801 MonoClass *catch_class = get_exception_catch_class (ei, ji, ctx);
1804 * Have to unwrap RuntimeWrappedExceptions if the
1805 * method's assembly doesn't have a RuntimeCompatibilityAttribute.
1807 if (non_exception && !wrap_non_exception_throws (method))
1808 ex_obj = non_exception;
1812 if (((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER))) {
1813 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
1814 MONO_CONTEXT_SET_LLVM_EXC_REG (ctx, ex_obj);
1816 g_assert (!ji->from_llvm);
1817 /* store the exception object in bp + ei->exvar_offset */
1818 *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = ex_obj;
1822 #ifdef MONO_CONTEXT_SET_LLVM_EH_SELECTOR_REG
1824 MONO_CONTEXT_SET_LLVM_EH_SELECTOR_REG (ctx, ei->clause_index);
1827 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
1829 * Filter clauses should only be run in the
1830 * first pass of exception handling.
1832 filtered = (filter_idx == first_filter_idx);
1836 mono_error_init (&error);
1837 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
1838 mono_object_isinst_checked (ex_obj, catch_class, &error)) || filtered) {
1840 * This guards against the situation that we abort a thread that is executing a finally clause
1841 * that was called by the EH machinery. It won't have a guard trampoline installed, so we must
1842 * check for this situation here and resume interruption if we are below the guarded block.
1844 if (G_UNLIKELY (jit_tls->handler_block_return_address)) {
1845 gboolean is_outside = FALSE;
1846 gpointer prot_bp = MONO_CONTEXT_GET_BP (&jit_tls->handler_block_context);
1847 gpointer catch_bp = MONO_CONTEXT_GET_BP (ctx);
1848 //FIXME make this stack direction aware
1849 if (catch_bp > prot_bp) {
1851 } else if (catch_bp == prot_bp) {
1852 /* Can be either try { try { } catch {} } finally {} or try { try { } finally {} } catch {}
1853 * So we check if the catch handler_start is protected by the guarded handler protected region
1856 * If there is an outstanding guarded_block return address, it means the current thread must be aborted.
1857 * This is the only way to reach out the guarded block as other cases are handled by the trampoline.
1858 * There aren't any further finally/fault handler blocks down the stack over this exception.
1859 * This must be ensured by the code that installs the guard trampoline.
1861 g_assert (ji == mini_jit_info_table_find (domain, (char *)MONO_CONTEXT_GET_IP (&jit_tls->handler_block_context), NULL));
1863 if (!is_address_protected (ji, jit_tls->handler_block, ei->handler_start)) {
1868 jit_tls->handler_block_return_address = NULL;
1869 jit_tls->handler_block = NULL;
1870 mono_thread_resume_interruption (); /*We ignore the exception here, it will be raised later*/
1874 if (mono_trace_is_enabled () && mono_trace_eval (method))
1875 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (method, TRUE));
1876 jit_tls->orig_ex_ctx_set = TRUE;
1877 mono_profiler_exception_clause_handler (method, ei->flags, i);
1878 jit_tls->orig_ex_ctx_set = FALSE;
1879 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1881 #ifndef DISABLE_PERFCOUNTERS
1882 mono_perfcounters->exceptions_depth += frame_count;
1884 if (obj == (MonoObject *)domain->stack_overflow_ex)
1885 jit_tls->handling_stack_ovf = FALSE;
1889 mono_error_cleanup (&error);
1890 if (ei->flags == MONO_EXCEPTION_CLAUSE_FAULT) {
1891 if (mono_trace_is_enabled () && mono_trace_eval (method))
1892 g_print ("EXCEPTION: fault clause %d of %s\n", i, mono_method_full_name (method, TRUE));
1893 jit_tls->orig_ex_ctx_set = TRUE;
1894 mono_profiler_exception_clause_handler (method, ei->flags, i);
1895 jit_tls->orig_ex_ctx_set = FALSE;
1896 call_filter (ctx, ei->handler_start);
1898 if (ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
1899 if (mono_trace_is_enabled () && mono_trace_eval (method))
1900 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (method, TRUE));
1901 jit_tls->orig_ex_ctx_set = TRUE;
1902 mono_profiler_exception_clause_handler (method, ei->flags, i);
1903 jit_tls->orig_ex_ctx_set = FALSE;
1904 #ifndef DISABLE_PERFCOUNTERS
1905 mono_perfcounters->exceptions_finallys++;
1908 if (ji->from_llvm) {
1910 * LLVM compiled finally handlers follow the design
1911 * of the c++ ehabi, i.e. they call a resume function
1912 * at the end instead of returning to the caller.
1913 * So save the exception handling state,
1914 * mono_resume_unwind () will call us again to continue
1917 jit_tls->resume_state.ex_obj = obj;
1918 jit_tls->resume_state.ji = ji;
1919 jit_tls->resume_state.clause_index = i + 1;
1920 jit_tls->resume_state.ctx = *ctx;
1921 jit_tls->resume_state.new_ctx = new_ctx;
1922 jit_tls->resume_state.lmf = lmf;
1923 jit_tls->resume_state.first_filter_idx = first_filter_idx;
1924 jit_tls->resume_state.filter_idx = filter_idx;
1925 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1928 call_filter (ctx, ei->handler_start);
1934 jit_tls->orig_ex_ctx_set = TRUE;
1935 mono_profiler_exception_method_leave (method);
1936 jit_tls->orig_ex_ctx_set = FALSE;
1941 g_assert_not_reached ();
1945 * mono_debugger_run_finally:
1946 * @start_ctx: saved processor state
1948 * This method is called by the Mono Debugger to call all `finally' clauses of the
1949 * current stack frame. It's used when the user issues a `return' command to make
1950 * the current stack frame return. After returning from this method, the debugger
1951 * unwinds the stack one frame and gives control back to the user.
1953 * NOTE: This method is only used when running inside the Mono Debugger.
1956 mono_debugger_run_finally (MonoContext *start_ctx)
1958 static int (*call_filter) (MonoContext *, gpointer) = NULL;
1959 MonoDomain *domain = mono_domain_get ();
1960 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
1961 MonoLMF *lmf = mono_get_lmf ();
1962 MonoContext ctx, new_ctx;
1963 MonoJitInfo *ji, rji;
1968 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, NULL, NULL);
1969 if (!ji || ji == (gpointer)-1)
1973 call_filter = (int (*)(MonoContext *, void *))mono_get_call_filter ();
1975 for (i = 0; i < ji->num_clauses; i++) {
1976 MonoJitExceptionInfo *ei = &ji->clauses [i];
1978 if (is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (&ctx)) &&
1979 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1980 call_filter (&ctx, ei->handler_start);
1986 * mono_handle_exception:
1987 * @ctx: saved processor state
1988 * @obj: the exception object
1991 mono_handle_exception (MonoContext *ctx, MonoObject *obj)
1993 #ifndef DISABLE_PERFCOUNTERS
1994 mono_perfcounters->exceptions_thrown++;
1997 return mono_handle_exception_internal (ctx, obj, FALSE, NULL);
2000 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
2002 #ifndef MONO_ARCH_USE_SIGACTION
2003 #error "Can't use sigaltstack without sigaction"
2006 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
2009 mono_setup_altstack (MonoJitTlsData *tls)
2013 guint8 *staddr = NULL;
2015 if (mono_running_on_valgrind ())
2018 mono_thread_info_get_stack_bounds (&staddr, &stsize);
2022 tls->end_of_stack = staddr + stsize;
2023 tls->stack_size = stsize;
2025 /*g_print ("thread %p, stack_base: %p, stack_size: %d\n", (gpointer)pthread_self (), staddr, stsize);*/
2027 tls->stack_ovf_guard_base = staddr + mono_pagesize ();
2028 tls->stack_ovf_guard_size = ALIGN_TO (8 * 4096, mono_pagesize ());
2030 g_assert ((guint8*)&sa >= (guint8*)tls->stack_ovf_guard_base + tls->stack_ovf_guard_size);
2032 if (mono_mprotect (tls->stack_ovf_guard_base, tls->stack_ovf_guard_size, MONO_MMAP_NONE)) {
2033 /* mprotect can fail for the main thread stack */
2034 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);
2035 g_assert (gaddr == tls->stack_ovf_guard_base);
2036 tls->stack_ovf_valloced = TRUE;
2039 /* Setup an alternate signal stack */
2040 tls->signal_stack = mono_valloc (0, MONO_ARCH_SIGNAL_STACK_SIZE, MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_PRIVATE|MONO_MMAP_ANON);
2041 tls->signal_stack_size = MONO_ARCH_SIGNAL_STACK_SIZE;
2043 g_assert (tls->signal_stack);
2045 sa.ss_sp = tls->signal_stack;
2046 sa.ss_size = MONO_ARCH_SIGNAL_STACK_SIZE;
2048 g_assert (sigaltstack (&sa, NULL) == 0);
2050 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);
2054 mono_free_altstack (MonoJitTlsData *tls)
2059 sa.ss_sp = tls->signal_stack;
2060 sa.ss_size = MONO_ARCH_SIGNAL_STACK_SIZE;
2061 sa.ss_flags = SS_DISABLE;
2062 err = sigaltstack (&sa, NULL);
2063 g_assert (err == 0);
2065 if (tls->signal_stack)
2066 mono_vfree (tls->signal_stack, MONO_ARCH_SIGNAL_STACK_SIZE);
2067 if (tls->stack_ovf_valloced)
2068 mono_vfree (tls->stack_ovf_guard_base, tls->stack_ovf_guard_size);
2070 mono_mprotect (tls->stack_ovf_guard_base, tls->stack_ovf_guard_size, MONO_MMAP_READ|MONO_MMAP_WRITE);
2073 #else /* !MONO_ARCH_SIGSEGV_ON_ALTSTACK */
2076 mono_setup_altstack (MonoJitTlsData *tls)
2081 mono_free_altstack (MonoJitTlsData *tls)
2085 #endif /* MONO_ARCH_SIGSEGV_ON_ALTSTACK */
2088 try_restore_stack_protection (MonoJitTlsData *jit_tls, int extra_bytes)
2090 gint32 unprotect_size = jit_tls->stack_ovf_guard_size;
2091 /* we need to leave some room for throwing the exception */
2092 while (unprotect_size >= 0 && (char*)jit_tls->stack_ovf_guard_base + unprotect_size > ((char*)&unprotect_size - extra_bytes))
2093 unprotect_size -= mono_pagesize ();
2094 /* at this point we could try and build a new domain->stack_overflow_ex, but only if there
2095 * is sufficient stack
2097 //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);
2099 mono_mprotect (jit_tls->stack_ovf_guard_base, unprotect_size, MONO_MMAP_NONE);
2100 return unprotect_size == jit_tls->stack_ovf_guard_size;
2103 static G_GNUC_UNUSED void
2104 try_more_restore (void)
2106 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
2107 if (try_restore_stack_protection (jit_tls, 500))
2108 jit_tls->restore_stack_prot = NULL;
2111 static G_GNUC_UNUSED void
2112 restore_stack_protection (void)
2114 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
2115 MonoException *ex = mono_domain_get ()->stack_overflow_ex;
2116 /* if we can't restore the stack protection, keep a callback installed so
2117 * we'll try to restore as much stack as we can at each return from unmanaged
2120 if (try_restore_stack_protection (jit_tls, 4096))
2121 jit_tls->restore_stack_prot = NULL;
2123 jit_tls->restore_stack_prot = try_more_restore_tramp;
2124 /* here we also throw a stack overflow exception */
2125 ex->trace_ips = NULL;
2126 ex->stack_trace = NULL;
2127 mono_raise_exception (ex);
2131 mono_altstack_restore_prot (mgreg_t *regs, guint8 *code, gpointer *tramp_data, guint8* tramp)
2133 void (*func)(void) = (void (*)(void))tramp_data;
2139 mono_handle_soft_stack_ovf (MonoJitTlsData *jit_tls, MonoJitInfo *ji, void *ctx, MONO_SIG_HANDLER_INFO_TYPE *siginfo, guint8* fault_addr)
2144 /* we got a stack overflow in the soft-guard pages
2145 * There are two cases:
2146 * 1) managed code caused the overflow: we unprotect the soft-guard page
2147 * and let the arch-specific code trigger the exception handling mechanism
2148 * in the thread stack. The soft-guard pages will be protected again as the stack is unwound.
2149 * 2) unmanaged code caused the overflow: we unprotect the soft-guard page
2150 * and hope we can continue with those enabled, at least until the hard-guard page
2151 * is hit. The alternative to continuing here is to just print a message and abort.
2152 * We may add in the future the code to protect the pages again in the codepath
2153 * when we return from unmanaged to managed code.
2155 if (jit_tls->stack_ovf_guard_size && fault_addr >= (guint8*)jit_tls->stack_ovf_guard_base &&
2156 fault_addr < (guint8*)jit_tls->stack_ovf_guard_base + jit_tls->stack_ovf_guard_size) {
2157 /* we unprotect the minimum amount we can */
2159 gboolean handled = FALSE;
2161 guard_size = jit_tls->stack_ovf_guard_size - (mono_pagesize () * SIZEOF_VOID_P / 4);
2162 while (guard_size && fault_addr < (guint8*)jit_tls->stack_ovf_guard_base + guard_size) {
2163 guard_size -= mono_pagesize ();
2165 guard_size = jit_tls->stack_ovf_guard_size - guard_size;
2166 /*fprintf (stderr, "unprotecting: %d\n", guard_size);*/
2167 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);
2168 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
2170 mono_arch_handle_altstack_exception (ctx, siginfo, fault_addr, TRUE);
2175 /* We print a message: after this even managed stack overflows
2176 * may crash the runtime
2178 mono_runtime_printf_err ("Stack overflow in unmanaged: IP: %p, fault addr: %p", mono_arch_ip_from_context (ctx), fault_addr);
2179 if (!jit_tls->handling_stack_ovf) {
2180 jit_tls->restore_stack_prot = restore_stack_protection_tramp;
2181 jit_tls->handling_stack_ovf = 1;
2183 /*fprintf (stderr, "Already handling stack overflow\n");*/
2192 MonoMethod *omethod;
2194 } PrintOverflowUserData;
2196 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2198 print_overflow_stack_frame (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
2200 MonoMethod *method = NULL;
2201 PrintOverflowUserData *user_data = (PrintOverflowUserData *)data;
2204 if (frame->ji && frame->type != FRAME_TYPE_TRAMPOLINE)
2205 method = jinfo_get_method (frame->ji);
2208 if (user_data->count == 0) {
2209 /* The first frame is in its prolog, so a line number cannot be computed */
2210 user_data->count ++;
2214 /* If this is a one method overflow, skip the other instances */
2215 if (method == user_data->omethod)
2218 location = mono_debug_print_stack_frame (method, frame->native_offset, mono_domain_get ());
2219 mono_runtime_printf_err (" %s", location);
2222 if (user_data->count == 1) {
2223 mono_runtime_printf_err (" <...>");
2224 user_data->omethod = method;
2226 user_data->omethod = NULL;
2229 user_data->count ++;
2231 mono_runtime_printf_err (" at <unknown> <0x%05x>", frame->native_offset);
2238 mono_handle_hard_stack_ovf (MonoJitTlsData *jit_tls, MonoJitInfo *ji, void *ctx, guint8* fault_addr)
2240 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2241 PrintOverflowUserData ud;
2245 /* we don't do much now, but we can warn the user with a useful message */
2246 mono_runtime_printf_err ("Stack overflow: IP: %p, fault addr: %p", mono_arch_ip_from_context (ctx), fault_addr);
2248 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2249 mono_sigctx_to_monoctx (ctx, &mctx);
2251 mono_runtime_printf_err ("Stacktrace:");
2253 memset (&ud, 0, sizeof (ud));
2255 mono_walk_stack_with_ctx (print_overflow_stack_frame, &mctx, MONO_UNWIND_LOOKUP_ACTUAL_METHOD, &ud);
2257 if (ji && !ji->is_trampoline && jinfo_get_method (ji))
2258 mono_runtime_printf_err ("At %s", mono_method_full_name (jinfo_get_method (ji), TRUE));
2260 mono_runtime_printf_err ("At <unmanaged>.");
2267 print_stack_frame_to_stderr (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
2269 MonoMethod *method = NULL;
2271 if (frame->ji && frame->type != FRAME_TYPE_TRAMPOLINE)
2272 method = jinfo_get_method (frame->ji);
2275 gchar *location = mono_debug_print_stack_frame (method, frame->native_offset, mono_domain_get ());
2276 mono_runtime_printf_err (" %s", location);
2279 mono_runtime_printf_err (" at <unknown> <0x%05x>", frame->native_offset);
2284 static G_GNUC_UNUSED gboolean
2285 print_stack_frame_to_string (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
2287 GString *p = (GString*)data;
2288 MonoMethod *method = NULL;
2290 if (frame->ji && frame->type != FRAME_TYPE_TRAMPOLINE)
2291 method = jinfo_get_method (frame->ji);
2293 if (method && frame->domain) {
2294 gchar *location = mono_debug_print_stack_frame (method, frame->native_offset, frame->domain);
2295 g_string_append_printf (p, " %s\n", location);
2298 g_string_append_printf (p, " at <unknown> <0x%05x>\n", frame->native_offset);
2303 #ifndef MONO_CROSS_COMPILE
2305 static gboolean handling_sigsegv = FALSE;
2308 * mono_handle_native_sigsegv:
2310 * Handle a SIGSEGV received while in native code by printing diagnostic
2311 * information and aborting.
2314 mono_handle_native_sigsegv (int signal, void *ctx, MONO_SIG_HANDLER_INFO_TYPE *info)
2316 #ifdef MONO_ARCH_USE_SIGACTION
2317 struct sigaction sa;
2319 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
2320 const char *signal_str = (signal == SIGSEGV) ? "SIGSEGV" : "SIGABRT";
2322 if (handling_sigsegv)
2325 if (mini_get_debug_options ()->suspend_on_sigsegv) {
2326 mono_runtime_printf_err ("Received SIGSEGV, suspending...");
2337 /* To prevent infinite loops when the stack walk causes a crash */
2338 handling_sigsegv = TRUE;
2340 /* !jit_tls means the thread was not registered with the runtime */
2341 if (jit_tls && mono_thread_internal_current ()) {
2342 mono_runtime_printf_err ("Stacktrace:\n");
2344 /* FIXME: Is MONO_UNWIND_LOOKUP_IL_OFFSET correct here? */
2345 mono_walk_stack (print_stack_frame_to_stderr, MONO_UNWIND_LOOKUP_IL_OFFSET, NULL);
2348 #ifdef HAVE_BACKTRACE_SYMBOLS
2354 mono_runtime_printf_err ("\nNative stacktrace:\n");
2356 size = backtrace (array, 256);
2357 names = backtrace_symbols (array, size);
2358 for (i =0; i < size; ++i) {
2359 mono_runtime_printf_err ("\t%s", names [i]);
2363 /* Try to get more meaningful information using gdb */
2365 #if !defined(HOST_WIN32) && defined(HAVE_SYS_SYSCALL_H) && defined(SYS_fork)
2366 if (!mini_get_debug_options ()->no_gdb_backtrace) {
2367 /* From g_spawn_command_line_sync () in eglib */
2370 pid_t crashed_pid = getpid ();
2374 * glibc fork acquires some locks, so if the crash happened inside malloc/free,
2375 * it will deadlock. Call the syscall directly instead.
2377 pid = mono_runtime_syscall_fork ();
2378 #if defined (HAVE_PRCTL) && defined(PR_SET_PTRACER)
2380 // Allow gdb to attach to the process even if ptrace_scope sysctl variable is set to
2381 // a value other than 0 (the most permissive ptrace scope). Most modern Linux
2382 // distributions set the scope to 1 which allows attaching only to direct children of
2383 // the current process
2384 prctl (PR_SET_PTRACER, pid, 0, 0, 0);
2388 dup2 (STDERR_FILENO, STDOUT_FILENO);
2390 mono_gdb_render_native_backtraces (crashed_pid);
2394 mono_runtime_printf_err ("\nDebug info from gdb:\n");
2395 waitpid (pid, &status, 0);
2400 mono_exception_native_unwind (ctx, info);
2404 * A SIGSEGV indicates something went very wrong so we can no longer depend
2405 * on anything working. So try to print out lots of diagnostics, starting
2406 * with ones which have a greater chance of working.
2408 mono_runtime_printf_err (
2410 "=================================================================\n"
2411 "Got a %s while executing native code. This usually indicates\n"
2412 "a fatal error in the mono runtime or one of the native libraries \n"
2413 "used by your application.\n"
2414 "=================================================================\n",
2418 #ifdef MONO_ARCH_USE_SIGACTION
2420 /* Remove our SIGABRT handler */
2421 sa.sa_handler = SIG_DFL;
2422 sigemptyset (&sa.sa_mask);
2425 g_assert (sigaction (SIGABRT, &sa, NULL) != -1);
2429 if (!mono_do_crash_chaining) {
2430 /*Android abort is a fluke, it doesn't abort, it triggers another segv. */
2431 #if defined (PLATFORM_ANDROID)
2442 mono_handle_native_sigsegv (int signal, void *ctx, MONO_SIG_HANDLER_INFO_TYPE *info)
2444 g_assert_not_reached ();
2447 #endif /* !MONO_CROSS_COMPILE */
2450 mono_print_thread_dump_internal (void *sigctx, MonoContext *start_ctx)
2452 MonoInternalThread *thread = mono_thread_internal_current ();
2453 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2461 GError *error = NULL;
2466 text = g_string_new (0);
2468 name = g_utf16_to_utf8 (thread->name, thread->name_len, NULL, NULL, &error);
2470 g_string_append_printf (text, "\n\"%s\"", name);
2473 else if (thread->threadpool_thread)
2474 g_string_append (text, "\n\"<threadpool thread>\"");
2476 g_string_append (text, "\n\"<unnamed thread>\"");
2479 wapi_desc = wapi_current_thread_desc ();
2480 g_string_append_printf (text, " tid=0x%p this=0x%p %s\n", (gpointer)(gsize)thread->tid, thread, wapi_desc);
2484 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2486 memcpy (&ctx, start_ctx, sizeof (MonoContext));
2488 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, mono_print_thread_dump);
2490 mono_sigctx_to_monoctx (sigctx, &ctx);
2492 mono_walk_stack_with_ctx (print_stack_frame_to_string, &ctx, MONO_UNWIND_LOOKUP_ALL, text);
2494 mono_runtime_printf ("\t<Stack traces in thread dumps not supported on this platform>");
2497 mono_runtime_printf ("%s", text->str);
2499 #if PLATFORM_WIN32 && TARGET_WIN32 && _DEBUG
2500 OutputDebugStringA(text->str);
2503 g_string_free (text, TRUE);
2504 mono_runtime_stdout_fflush ();
2508 * mono_print_thread_dump:
2510 * Print information about the current thread to stdout.
2511 * SIGCTX can be NULL, allowing this to be called from gdb.
2514 mono_print_thread_dump (void *sigctx)
2516 mono_print_thread_dump_internal (sigctx, NULL);
2520 mono_print_thread_dump_from_ctx (MonoContext *ctx)
2522 mono_print_thread_dump_internal (NULL, ctx);
2526 * mono_resume_unwind:
2528 * This is called by a trampoline from LLVM compiled finally clauses to continue
2532 mono_resume_unwind (MonoContext *ctx)
2534 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
2535 MonoContext new_ctx;
2537 MONO_CONTEXT_SET_IP (ctx, MONO_CONTEXT_GET_IP (&jit_tls->resume_state.ctx));
2538 MONO_CONTEXT_SET_SP (ctx, MONO_CONTEXT_GET_SP (&jit_tls->resume_state.ctx));
2541 mono_handle_exception_internal (&new_ctx, (MonoObject *)jit_tls->resume_state.ex_obj, TRUE, NULL);
2543 mono_restore_context (&new_ctx);
2546 #ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
2551 MonoJitExceptionInfo *ei;
2552 } FindHandlerBlockData;
2555 find_last_handler_block (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
2559 FindHandlerBlockData *pdata = (FindHandlerBlockData *)data;
2560 MonoJitInfo *ji = frame->ji;
2565 ip = MONO_CONTEXT_GET_IP (ctx);
2567 for (i = 0; i < ji->num_clauses; ++i) {
2568 MonoJitExceptionInfo *ei = ji->clauses + i;
2569 if (ei->flags != MONO_EXCEPTION_CLAUSE_FINALLY)
2571 /*If ip points to the first instruction it means the handler block didn't start
2572 so we can leave its execution to the EH machinery*/
2573 if (ei->handler_start < ip && ip < ei->data.handler_end) {
2585 install_handler_block_guard (MonoJitInfo *ji, MonoContext *ctx)
2588 MonoJitExceptionInfo *clause = NULL;
2591 ip = MONO_CONTEXT_GET_IP (ctx);
2593 for (i = 0; i < ji->num_clauses; ++i) {
2594 clause = &ji->clauses [i];
2595 if (clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY)
2597 if (clause->handler_start < ip && clause->data.handler_end > ip)
2601 /*no matching finally */
2602 if (i == ji->num_clauses)
2605 /*If we stopped on the instruction right before the try, we haven't actually started executing it*/
2606 if (ip == clause->handler_start)
2609 return mono_arch_install_handler_block_guard (ji, clause, ctx, mono_create_handler_block_trampoline ());
2613 * Finds the bottom handler block running and install a block guard if needed.
2614 * FIXME add full-aot support.
2617 mono_install_handler_block_guard (MonoThreadUnwindState *ctx)
2619 FindHandlerBlockData data = { 0 };
2620 MonoJitTlsData *jit_tls = (MonoJitTlsData *)ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS];
2627 /* Guard against a null MonoJitTlsData. This can happens if the thread receives the
2628 * interrupt signal before the JIT has time to initialize its TLS data for the given thread.
2630 if (!jit_tls || jit_tls->handler_block_return_address)
2633 /* Do an async safe stack walk */
2634 mono_thread_info_set_is_async_context (TRUE);
2635 mono_walk_stack_with_state (find_last_handler_block, ctx, MONO_UNWIND_NONE, &data);
2636 mono_thread_info_set_is_async_context (FALSE);
2641 memcpy (&jit_tls->handler_block_context, &data.ctx, sizeof (MonoContext));
2643 resume_ip = install_handler_block_guard (data.ji, &data.ctx);
2644 if (resume_ip == NULL)
2647 jit_tls->handler_block_return_address = resume_ip;
2648 jit_tls->handler_block = data.ei;
2655 mono_install_handler_block_guard (MonoThreadUnwindState *ctx)
2663 mono_set_cast_details (MonoClass *from, MonoClass *to)
2665 MonoJitTlsData *jit_tls = NULL;
2667 if (mini_get_debug_options ()->better_cast_details) {
2668 jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
2669 jit_tls->class_cast_from = from;
2670 jit_tls->class_cast_to = to;
2675 /*returns false if the thread is not attached*/
2677 mono_thread_state_init_from_sigctx (MonoThreadUnwindState *ctx, void *sigctx)
2679 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2680 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
2688 mono_sigctx_to_monoctx (sigctx, &ctx->ctx);
2690 ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
2691 ctx->unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
2692 ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = thread->jit_data;
2695 mono_thread_state_init (ctx);
2698 if (!ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] || !ctx->unwind_data [MONO_UNWIND_DATA_LMF])
2704 g_error ("Implement mono_arch_sigctx_to_monoctx for the current target");
2710 mono_thread_state_init (MonoThreadUnwindState *ctx)
2712 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
2714 #if defined(MONO_CROSS_COMPILE)
2715 ctx->valid = FALSE; //A cross compiler doesn't need to suspend.
2716 #elif MONO_ARCH_HAS_MONO_CONTEXT
2717 MONO_CONTEXT_GET_CURRENT (ctx->ctx);
2719 g_error ("Use a null sigctx requires a working mono-context");
2722 ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
2723 ctx->unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
2724 ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = thread ? thread->jit_data : NULL;
2730 mono_thread_state_init_from_monoctx (MonoThreadUnwindState *ctx, MonoContext *mctx)
2732 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
2739 ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
2740 ctx->unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
2741 ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = thread->jit_data;
2746 /*returns false if the thread is not attached*/
2748 mono_thread_state_init_from_current (MonoThreadUnwindState *ctx)
2750 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
2751 MONO_ARCH_CONTEXT_DEF
2753 mono_arch_flush_register_windows ();
2755 if (!thread || !thread->jit_data) {
2759 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
2760 MONO_INIT_CONTEXT_FROM_CURRENT (&ctx->ctx);
2762 MONO_INIT_CONTEXT_FROM_FUNC (&ctx->ctx, mono_thread_state_init_from_current);
2765 ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
2766 ctx->unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
2767 ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = thread->jit_data;
2773 mono_raise_exception_with_ctx (MonoException *exc, MonoContext *ctx)
2775 mono_handle_exception (ctx, (MonoObject *)exc);
2776 mono_restore_context (ctx);
2779 /*FIXME Move all monoctx -> sigctx conversion to signal handlers once all archs support utils/mono-context */
2781 mono_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
2783 #ifdef MONO_ARCH_HAVE_SETUP_ASYNC_CALLBACK
2784 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
2785 jit_tls->ex_ctx = *ctx;
2787 mono_arch_setup_async_callback (ctx, async_cb, user_data);
2789 g_error ("This target doesn't support mono_arch_setup_async_callback");
2794 mono_install_unhandled_exception_hook (MonoUnhandledExceptionFunc func, gpointer user_data)
2796 unhandled_exception_hook = func;
2797 unhandled_exception_hook_data = user_data;
2801 mono_invoke_unhandled_exception_hook (MonoObject *exc)
2803 if (unhandled_exception_hook) {
2804 unhandled_exception_hook (exc, unhandled_exception_hook_data);
2806 MonoObject *other = NULL;
2807 MonoString *str = mono_object_to_string (exc, &other);
2811 msg = mono_string_to_utf8 (str);
2813 char *original_backtrace = mono_exception_get_managed_backtrace ((MonoException*)exc);
2814 char *nested_backtrace = mono_exception_get_managed_backtrace ((MonoException*)other);
2816 msg = g_strdup_printf ("Nested exception detected.\nOriginal Exception: %s\nNested exception:%s\n",
2817 original_backtrace, nested_backtrace);
2819 g_free (original_backtrace);
2820 g_free (nested_backtrace);
2822 msg = g_strdup ("Nested exception trying to figure out what went wrong");
2824 mono_runtime_printf_err ("[ERROR] FATAL UNHANDLED EXCEPTION: %s", msg);
2826 #if defined(HOST_IOS)
2827 g_assertion_message ("Terminating runtime due to unhandled exception");
2829 exit (mono_environment_exitcode_get ());
2833 g_assert_not_reached ();
2837 * mono_restore_context:
2839 * Call the architecture specific restore context function.
2842 mono_restore_context (MonoContext *ctx)
2844 static void (*restore_context) (MonoContext *);
2846 if (!restore_context)
2847 restore_context = (void (*)(MonoContext *))mono_get_restore_context ();
2848 restore_context (ctx);
2849 g_assert_not_reached ();
2853 * mono_jinfo_get_unwind_info:
2855 * Return the unwind info for JI.
2858 mono_jinfo_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
2861 return mono_aot_get_unwind_info (ji, unwind_info_len);
2863 return mono_get_cached_unwind_info (ji->unwind_info, unwind_info_len);
2867 mono_jinfo_get_epilog_size (MonoJitInfo *ji)
2869 MonoArchEHJitInfo *info;
2871 info = mono_jit_info_get_arch_eh_info (ji);
2874 return info->epilog_size;
2878 * LLVM/Bitcode exception handling.
2882 throw_exception (MonoObject *ex, gboolean rethrow)
2885 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
2886 MonoException *mono_ex;
2888 if (!mono_object_isinst_checked (ex, mono_defaults.exception_class, &error)) {
2889 mono_error_assert_ok (&error);
2890 mono_ex = mono_get_exception_runtime_wrapped_checked (ex, &error);
2891 mono_error_assert_ok (&error);
2894 mono_ex = (MonoException*)ex;
2897 jit_tls->thrown_exc = mono_gchandle_new ((MonoObject*)mono_ex, FALSE);
2900 #ifdef MONO_ARCH_HAVE_UNWIND_BACKTRACE
2901 GList *l, *ips = NULL;
2904 _Unwind_Backtrace (build_stack_trace, &ips);
2905 /* The list contains ip-gshared info pairs */
2907 ips = g_list_reverse (ips);
2908 for (l = ips; l; l = l->next) {
2909 trace = g_list_append (trace, l->data);
2910 trace = g_list_append (trace, NULL);
2912 MONO_OBJECT_SETREF (mono_ex, trace_ips, mono_glist_to_array (trace, mono_defaults.int_class));
2914 g_list_free (trace);
2918 mono_llvm_cpp_throw_exception ();
2922 mono_llvm_throw_exception (MonoObject *ex)
2924 throw_exception (ex, FALSE);
2928 mono_llvm_rethrow_exception (MonoObject *ex)
2930 throw_exception (ex, TRUE);
2934 mono_llvm_raise_exception (MonoException *e)
2936 mono_llvm_throw_exception ((MonoObject*)e);
2940 mono_llvm_throw_corlib_exception (guint32 ex_token_index)
2942 guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
2945 ex = mono_exception_from_token (mono_defaults.exception_class->image, ex_token);
2947 mono_llvm_throw_exception ((MonoObject*)ex);
2951 * mono_llvm_resume_exception:
2953 * Resume exception propagation.
2956 mono_llvm_resume_exception (void)
2958 mono_llvm_cpp_throw_exception ();
2962 * mono_llvm_load_exception:
2964 * Return the currently thrown exception.
2967 mono_llvm_load_exception (void)
2969 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
2971 MonoException *mono_ex = (MonoException*)mono_gchandle_get_target (jit_tls->thrown_exc);
2973 if (mono_ex->trace_ips) {
2974 GList *trace_ips = NULL;
2975 gpointer ip = __builtin_return_address (0);
2977 size_t upper = mono_array_length (mono_ex->trace_ips);
2979 for (int i = 0; i < upper; i+= 2) {
2980 gpointer curr_ip = mono_array_get (mono_ex->trace_ips, gpointer, i);
2981 gpointer curr_info = mono_array_get (mono_ex->trace_ips, gpointer, i + 1);
2982 trace_ips = g_list_append (trace_ips, curr_ip);
2983 trace_ips = g_list_append (trace_ips, curr_info);
2989 // FIXME: Does this work correctly for rethrows?
2990 // We may be discarding useful information
2991 // when this gets GC'ed
2992 MONO_OBJECT_SETREF (mono_ex, trace_ips, mono_glist_to_array (trace_ips, mono_defaults.int_class));
2993 g_list_free (trace_ips);
2996 //MONO_OBJECT_SETREF (mono_ex, stack_trace, ves_icall_System_Exception_get_trace (mono_ex));
2998 MONO_OBJECT_SETREF (mono_ex, trace_ips, mono_array_new (mono_domain_get (), mono_defaults.int_class, 0));
2999 MONO_OBJECT_SETREF (mono_ex, stack_trace, mono_array_new (mono_domain_get (), mono_defaults.stack_frame_class, 0));
3002 return &mono_ex->object;
3006 * mono_llvm_clear_exception:
3008 * Mark the currently thrown exception as handled.
3011 mono_llvm_clear_exception (void)
3013 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
3014 mono_gchandle_free (jit_tls->thrown_exc);
3015 jit_tls->thrown_exc = 0;
3017 mono_memory_barrier ();
3021 * mono_llvm_match_exception:
3023 * Return the innermost clause containing REGION_START-REGION_END which can handle
3024 * the current exception.
3027 mono_llvm_match_exception (MonoJitInfo *jinfo, guint32 region_start, guint32 region_end, gpointer rgctx, MonoObject *this_obj)
3030 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
3034 g_assert (jit_tls->thrown_exc);
3035 exc = mono_gchandle_get_target (jit_tls->thrown_exc);
3036 for (int i = 0; i < jinfo->num_clauses; i++) {
3037 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
3038 MonoClass *catch_class;
3040 if (! (ei->try_offset == region_start && ei->try_offset + ei->try_len == region_end) )
3043 catch_class = ei->data.catch_class;
3044 if (catch_class->byval_arg.type == MONO_TYPE_VAR || catch_class->byval_arg.type == MONO_TYPE_MVAR || catch_class->byval_arg.type == MONO_TYPE_GENERICINST) {
3045 MonoGenericContext context;
3046 MonoType *inflated_type;
3048 g_assert (rgctx || this_obj);
3049 context = get_generic_context_from_stack_frame (jinfo, rgctx ? rgctx : this_obj->vtable);
3050 inflated_type = mono_class_inflate_generic_type_checked (&catch_class->byval_arg, &context, &error);
3051 mono_error_assert_ok (&error); /* FIXME don't swallow the error */
3053 catch_class = mono_class_from_mono_type (inflated_type);
3054 mono_metadata_free_type (inflated_type);
3057 // FIXME: Handle edge cases handled in get_exception_catch_class
3058 if (ei->flags == MONO_EXCEPTION_CLAUSE_NONE && mono_object_isinst_checked (exc, catch_class, &error)) {
3059 index = ei->clause_index;
3062 mono_error_assert_ok (&error);
3064 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3065 g_assert_not_reached ();
3074 mono_debug_personality (int a, _Unwind_Action b,
3075 uint64_t c, struct _Unwind_Exception *d, struct _Unwind_Context *e)
3077 g_assert_not_reached ();
3081 mono_debug_personality (void);
3084 mono_debug_personality (void)
3086 g_assert_not_reached ();