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/object-internals.h>
56 #include <mono/metadata/reflection-internals.h>
57 #include <mono/metadata/gc-internals.h>
58 #include <mono/metadata/mono-debug.h>
59 #include <mono/metadata/profiler.h>
60 #include <mono/metadata/mono-endian.h>
61 #include <mono/metadata/environment.h>
62 #include <mono/metadata/mono-mlist.h>
63 #include <mono/utils/mono-mmap.h>
64 #include <mono/utils/mono-logger-internals.h>
65 #include <mono/utils/mono-error.h>
66 #include <mono/utils/mono-error-internals.h>
70 #include "debugger-agent.h"
71 #include "seq-points.h"
72 #include "llvm-runtime.h"
73 #include "mini-llvm.h"
76 #include "mini-llvm-cpp.h"
79 #ifdef ENABLE_EXTENSION_MODULE
80 #include "../../../mono-extensions/mono/mini/mini-exceptions.c"
83 #ifndef MONO_ARCH_CONTEXT_DEF
84 #define MONO_ARCH_CONTEXT_DEF
87 static gpointer restore_context_func, call_filter_func;
88 static gpointer throw_exception_func, rethrow_exception_func;
89 static gpointer throw_corlib_exception_func;
91 static gpointer try_more_restore_tramp = NULL;
92 static gpointer restore_stack_protection_tramp = NULL;
94 static MonoUnhandledExceptionFunc unhandled_exception_hook = NULL;
95 static gpointer unhandled_exception_hook_data = NULL;
97 static void try_more_restore (void);
98 static void restore_stack_protection (void);
99 static void mono_walk_stack_full (MonoJitStackWalk func, MonoContext *start_ctx, MonoDomain *domain, MonoJitTlsData *jit_tls, MonoLMF *lmf, MonoUnwindOptions unwind_options, gpointer user_data);
100 static void mono_raise_exception_with_ctx (MonoException *exc, MonoContext *ctx);
101 static void mono_runtime_walk_stack_with_ctx (MonoJitStackWalk func, MonoContext *start_ctx, MonoUnwindOptions unwind_options, void *user_data);
104 mono_exceptions_init (void)
106 MonoRuntimeExceptionHandlingCallbacks cbs;
108 restore_context_func = mono_aot_get_trampoline ("restore_context");
109 call_filter_func = mono_aot_get_trampoline ("call_filter");
110 throw_exception_func = mono_aot_get_trampoline ("throw_exception");
111 rethrow_exception_func = mono_aot_get_trampoline ("rethrow_exception");
115 restore_context_func = mono_arch_get_restore_context (&info, FALSE);
116 mono_tramp_info_register (info, NULL);
117 call_filter_func = mono_arch_get_call_filter (&info, FALSE);
118 mono_tramp_info_register (info, NULL);
119 throw_exception_func = mono_arch_get_throw_exception (&info, FALSE);
120 mono_tramp_info_register (info, NULL);
121 rethrow_exception_func = mono_arch_get_rethrow_exception (&info, FALSE);
122 mono_tramp_info_register (info, NULL);
124 #ifdef MONO_ARCH_HAVE_RESTORE_STACK_SUPPORT
125 if (!mono_llvm_only) {
126 try_more_restore_tramp = mono_create_specific_trampoline (try_more_restore, MONO_TRAMPOLINE_RESTORE_STACK_PROT, mono_domain_get (), NULL);
127 restore_stack_protection_tramp = mono_create_specific_trampoline (restore_stack_protection, MONO_TRAMPOLINE_RESTORE_STACK_PROT, mono_domain_get (), NULL);
131 #ifdef MONO_ARCH_HAVE_EXCEPTIONS_INIT
132 mono_arch_exceptions_init ();
134 cbs.mono_walk_stack_with_ctx = mono_runtime_walk_stack_with_ctx;
135 cbs.mono_walk_stack_with_state = mono_walk_stack_with_state;
138 cbs.mono_raise_exception = mono_llvm_raise_exception;
140 cbs.mono_raise_exception = (void (*)(MonoException *))mono_get_throw_exception ();
141 cbs.mono_raise_exception_with_ctx = mono_raise_exception_with_ctx;
142 cbs.mono_exception_walk_trace = mono_exception_walk_trace;
143 cbs.mono_install_handler_block_guard = mono_install_handler_block_guard;
144 mono_install_eh_callbacks (&cbs);
148 mono_get_throw_exception (void)
150 g_assert (throw_exception_func);
151 return throw_exception_func;
155 mono_get_rethrow_exception (void)
157 g_assert (rethrow_exception_func);
158 return rethrow_exception_func;
162 mono_get_call_filter (void)
164 g_assert (call_filter_func);
165 return call_filter_func;
169 mono_get_restore_context (void)
171 g_assert (restore_context_func);
172 return restore_context_func;
176 mono_get_throw_corlib_exception (void)
178 gpointer code = NULL;
181 /* This depends on corlib classes so cannot be inited in mono_exceptions_init () */
182 if (throw_corlib_exception_func)
183 return throw_corlib_exception_func;
186 code = mono_aot_get_trampoline ("throw_corlib_exception");
188 code = mono_arch_get_throw_corlib_exception (&info, FALSE);
189 mono_tramp_info_register (info, NULL);
192 mono_memory_barrier ();
194 throw_corlib_exception_func = code;
196 return throw_corlib_exception_func;
200 * mono_get_throw_exception_addr:
202 * Return an address which stores the result of
203 * mono_get_throw_exception.
206 mono_get_throw_exception_addr (void)
208 return &throw_exception_func;
212 is_address_protected (MonoJitInfo *ji, MonoJitExceptionInfo *ei, gpointer ip)
214 MonoTryBlockHoleTableJitInfo *table;
219 if (ei->try_start > ip || ip >= ei->try_end)
222 if (!ji->has_try_block_holes)
225 table = mono_jit_info_get_try_block_hole_table_info (ji);
226 offset = (guint32)((char*)ip - (char*)ji->code_start);
227 clause = (guint16)(ei - ji->clauses);
228 g_assert (clause < ji->num_clauses);
230 for (i = 0; i < table->num_holes; ++i) {
231 MonoTryBlockHoleJitInfo *hole = &table->holes [i];
232 if (hole->clause == clause && hole->offset <= offset && hole->offset + hole->length > offset)
238 #ifdef MONO_ARCH_HAVE_UNWIND_BACKTRACE
241 static gboolean show_native_addresses = TRUE;
243 static gboolean show_native_addresses = FALSE;
246 static _Unwind_Reason_Code
247 build_stack_trace (struct _Unwind_Context *frame_ctx, void *state)
249 MonoDomain *domain = mono_domain_get ();
250 uintptr_t ip = _Unwind_GetIP (frame_ctx);
252 if (show_native_addresses || mono_jit_info_table_find (domain, (char*)ip)) {
253 GList **trace_ips = (GList **)state;
254 *trace_ips = g_list_prepend (*trace_ips, (gpointer)ip);
257 return _URC_NO_REASON;
261 get_unwind_backtrace (void)
265 _Unwind_Backtrace (build_stack_trace, &ips);
267 return g_slist_reverse (ips);
273 get_unwind_backtrace (void)
283 * Translate between the mono_arch_unwind_frame function and the old API.
286 find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
287 MonoContext *new_ctx, MonoLMF **lmf, gboolean *managed)
289 StackFrameInfo frame;
292 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
294 /* Avoid costly table lookup during stack overflow */
295 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
298 ji = mini_jit_info_table_find (domain, (char *)ip, NULL);
303 err = mono_arch_unwind_frame (domain, jit_tls, ji, ctx, new_ctx, lmf, NULL, &frame);
305 return (MonoJitInfo *)-1;
307 if (*lmf && ((*lmf) != jit_tls->first_lmf) && ((gpointer)MONO_CONTEXT_GET_SP (new_ctx) >= (gpointer)(*lmf))) {
309 * Remove any unused lmf.
310 * Mask out the lower bits which might be used to hold additional information.
312 *lmf = (MonoLMF *)(((gsize)(*lmf)->previous_lmf) & ~(SIZEOF_VOID_P -1));
315 /* Convert between the new and the old APIs */
316 switch (frame.type) {
317 case FRAME_TYPE_MANAGED:
321 case FRAME_TYPE_TRAMPOLINE:
323 case FRAME_TYPE_MANAGED_TO_NATIVE:
327 memset (res, 0, sizeof (MonoJitInfo));
328 res->d.method = frame.method;
331 case FRAME_TYPE_DEBUGGER_INVOKE: {
335 * The normal exception handling code can't handle this frame, so just
338 ji = find_jit_info (domain, jit_tls, res, NULL, new_ctx, &tmp_ctx, lmf, managed);
339 memcpy (new_ctx, &tmp_ctx, sizeof (MonoContext));
343 g_assert_not_reached ();
348 /* mono_find_jit_info:
350 * This function is used to gather information from @ctx. It return the
351 * MonoJitInfo of the corresponding function, unwinds one stack frame and
352 * stores the resulting context into @new_ctx. It also stores a string
353 * describing the stack location into @trace (if not NULL), and modifies
354 * the @lmf if necessary. @native_offset return the IP offset from the
355 * start of the function or -1 if that info is not available.
358 mono_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
359 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
363 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
365 MonoMethod *method = NULL;
376 ji = find_jit_info (domain, jit_tls, res, prev_ji, ctx, new_ctx, lmf, &managed2);
378 if (ji == (gpointer)-1)
381 if (ji && !ji->is_trampoline)
382 method = jinfo_get_method (ji);
384 if (managed2 || (method && method->wrapper_type)) {
385 const char *real_ip, *start;
388 start = (const char *)ji->code_start;
390 /* ctx->ip points into native code */
391 real_ip = (const char*)MONO_CONTEXT_GET_IP (new_ctx);
393 real_ip = (const char*)ip;
395 if ((real_ip >= start) && (real_ip <= start + ji->code_size))
396 offset = real_ip - start;
401 *native_offset = offset;
404 if (!method->wrapper_type || method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
408 *trace = mono_debug_print_stack_frame (method, offset, domain);
411 char *fname = mono_method_full_name (jinfo_get_method (res), TRUE);
412 *trace = g_strdup_printf ("in (unmanaged) %s", fname);
421 * mono_find_jit_info_ext:
423 * A version of mono_find_jit_info which returns all data in the StackFrameInfo
425 * A note about frames of type FRAME_TYPE_MANAGED_TO_NATIVE:
426 * - These frames are used to mark managed-to-native transitions, so CTX will refer to native
427 * code, and new_ctx will refer to the last managed frame. The caller should unwind once more
428 * to obtain the last managed frame.
429 * If SAVE_LOCATIONS is not NULL, it should point to an array of size MONO_MAX_IREGS.
430 * On return, it will be filled with the locations where callee saved registers are saved
431 * by the current frame. This is returned outside of StackFrameInfo because it can be
432 * quite large on some platforms.
433 * If ASYNC true, this function will be async safe, but some fields of frame and frame->ji will
437 mono_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
438 MonoJitInfo *prev_ji, MonoContext *ctx,
439 MonoContext *new_ctx, char **trace, MonoLMF **lmf,
440 mgreg_t **save_locations,
441 StackFrameInfo *frame)
444 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
446 MonoDomain *target_domain = domain;
447 MonoMethod *method = NULL;
448 gboolean async = mono_thread_info_is_async_context ();
453 /* Avoid costly table lookup during stack overflow */
454 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
457 ji = mini_jit_info_table_find (domain, (char *)ip, &target_domain);
460 target_domain = domain;
463 memset (save_locations, 0, MONO_MAX_IREGS * sizeof (mgreg_t*));
465 err = mono_arch_unwind_frame (target_domain, jit_tls, ji, ctx, new_ctx, lmf, save_locations, frame);
469 if (*lmf && ((*lmf) != jit_tls->first_lmf) && ((gpointer)MONO_CONTEXT_GET_SP (new_ctx) >= (gpointer)(*lmf))) {
471 * Remove any unused lmf.
472 * Mask out the lower bits which might be used to hold additional information.
474 *lmf = (MonoLMF *)(((gsize)(*lmf)->previous_lmf) & ~(SIZEOF_VOID_P -1));
477 if (frame->ji && !frame->ji->is_trampoline && !frame->ji->async)
478 method = jinfo_get_method (frame->ji);
480 if (frame->type == FRAME_TYPE_MANAGED && method) {
481 if (!method->wrapper_type || method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
482 frame->managed = TRUE;
485 if (frame->type == FRAME_TYPE_MANAGED_TO_NATIVE) {
487 * This type of frame is just a marker, the caller should unwind once more to get the
488 * last managed frame.
491 frame->method = NULL;
494 frame->native_offset = -1;
495 frame->domain = target_domain;
496 frame->async_context = async;
500 if (frame->type == FRAME_TYPE_MANAGED)
501 frame->method = method;
503 if (ji && (frame->managed || (method && method->wrapper_type))) {
504 const char *real_ip, *start;
506 start = (const char *)ji->code_start;
507 if (frame->type == FRAME_TYPE_MANAGED)
508 real_ip = (const char*)ip;
510 /* ctx->ip points into native code */
511 real_ip = (const char*)MONO_CONTEXT_GET_IP (new_ctx);
513 if ((real_ip >= start) && (real_ip <= start + ji->code_size))
514 frame->native_offset = real_ip - start;
516 frame->native_offset = -1;
520 *trace = mono_debug_print_stack_frame (method, frame->native_offset, domain);
522 if (trace && frame->method) {
523 char *fname = mono_method_full_name (frame->method, TRUE);
524 *trace = g_strdup_printf ("in (unmanaged) %s", fname);
533 * This function is async-safe.
536 get_generic_info_from_stack_frame (MonoJitInfo *ji, MonoContext *ctx)
538 MonoGenericJitInfo *gi;
542 if (!ji->has_generic_jit_info)
544 gi = mono_jit_info_get_generic_jit_info (ji);
550 * Search location list if available, it contains the precise location of the
551 * argument for every pc offset, even if the method was interrupted while it was in
555 int offset = (mgreg_t)MONO_CONTEXT_GET_IP (ctx) - (mgreg_t)ji->code_start;
558 for (i = 0; i < gi->nlocs; ++i) {
559 MonoDwarfLocListEntry *entry = &gi->locations [i];
561 if (offset >= entry->from && (offset < entry->to || entry->to == 0)) {
563 info = (gpointer)mono_arch_context_get_int_reg (ctx, entry->reg);
565 info = *(gpointer*)(gpointer)((char*)mono_arch_context_get_int_reg (ctx, entry->reg) + entry->offset);
569 g_assert (i < gi->nlocs);
572 info = (gpointer)mono_arch_context_get_int_reg (ctx, gi->this_reg);
574 info = *(gpointer*)(gpointer)((char*)mono_arch_context_get_int_reg (ctx, gi->this_reg) +
578 method = jinfo_get_method (ji);
579 if (mono_method_get_context (method)->method_inst) {
580 /* A MonoMethodRuntimeGenericContext* */
582 } else if ((method->flags & METHOD_ATTRIBUTE_STATIC) || method->klass->valuetype) {
586 /* Avoid returning a managed object */
587 MonoObject *this_obj = (MonoObject *)info;
589 return this_obj->vtable;
594 * generic_info is either a MonoMethodRuntimeGenericContext or a MonoVTable.
596 static MonoGenericContext
597 get_generic_context_from_stack_frame (MonoJitInfo *ji, gpointer generic_info)
599 MonoGenericContext context = { NULL, NULL };
600 MonoClass *klass, *method_container_class;
603 g_assert (generic_info);
605 method = jinfo_get_method (ji);
606 g_assert (method->is_inflated);
607 if (mono_method_get_context (method)->method_inst) {
608 MonoMethodRuntimeGenericContext *mrgctx = (MonoMethodRuntimeGenericContext *)generic_info;
610 klass = mrgctx->class_vtable->klass;
611 context.method_inst = mrgctx->method_inst;
612 g_assert (context.method_inst);
614 MonoVTable *vtable = (MonoVTable *)generic_info;
616 klass = vtable->klass;
619 //g_assert (!method->klass->generic_container);
620 if (method->klass->generic_class)
621 method_container_class = method->klass->generic_class->container_class;
623 method_container_class = method->klass;
625 /* class might refer to a subclass of method's class */
626 while (!(klass == method->klass || (klass->generic_class && klass->generic_class->container_class == method_container_class))) {
627 klass = klass->parent;
631 if (klass->generic_class || klass->generic_container)
632 context.class_inst = mini_class_get_context (klass)->class_inst;
634 if (klass->generic_class)
635 g_assert (mono_class_has_parent_and_ignore_generics (klass->generic_class->container_class, method_container_class));
637 g_assert (mono_class_has_parent_and_ignore_generics (klass, method_container_class));
643 get_method_from_stack_frame (MonoJitInfo *ji, gpointer generic_info)
646 MonoGenericContext context;
649 if (!ji->has_generic_jit_info || !mono_jit_info_get_generic_jit_info (ji)->has_this)
650 return jinfo_get_method (ji);
651 context = get_generic_context_from_stack_frame (ji, generic_info);
653 method = jinfo_get_method (ji);
654 method = mono_method_get_declaring_generic_method (method);
655 method = mono_class_inflate_generic_method_checked (method, &context, &error);
656 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
662 * mono_exception_walk_native_trace:
663 * @ex: The exception object whose frames should be walked
664 * @func: callback to call for each stack frame
665 * @user_data: data passed to the callback
667 * This function walks the stacktrace of an exception. For
668 * each frame the callback function is called with the relevant info.
669 * The walk ends when no more stack frames are found or when the callback
670 * returns a TRUE value.
674 mono_exception_walk_trace (MonoException *ex, MonoExceptionFrameWalk func, gpointer user_data)
676 MonoDomain *domain = mono_domain_get ();
677 MonoArray *ta = ex->trace_ips;
683 len = mono_array_length (ta) >> 1;
684 for (i = 0; i < len; i++) {
685 gpointer ip = mono_array_get (ta, gpointer, i * 2 + 0);
686 gpointer generic_info = mono_array_get (ta, gpointer, i * 2 + 1);
687 MonoJitInfo *ji = mono_jit_info_table_find (domain, (char *)ip);
690 if (func (NULL, ip, 0, FALSE, user_data))
693 MonoMethod *method = get_method_from_stack_frame (ji, generic_info);
694 if (func (method, ji->code_start, (char *) ip - (char *) ji->code_start, TRUE, user_data))
703 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
706 MonoDomain *domain = mono_domain_get ();
708 MonoArray *ta = exc->trace_ips;
709 MonoDebugSourceLocation *location;
713 /* Exception is not thrown yet */
714 return mono_array_new (domain, mono_defaults.stack_frame_class, 0);
717 len = mono_array_length (ta) >> 1;
719 res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
721 for (i = skip; i < len; i++) {
723 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new_checked (domain, mono_defaults.stack_frame_class, &error);
724 if (!mono_error_ok (&error)) {
725 mono_error_set_pending_exception (&error);
728 gpointer ip = mono_array_get (ta, gpointer, i * 2 + 0);
729 gpointer generic_info = mono_array_get (ta, gpointer, i * 2 + 1);
732 ji = mono_jit_info_table_find (domain, (char *)ip);
734 /* Unmanaged frame */
735 mono_array_setref (res, i, sf);
739 g_assert (ji != NULL);
742 /* Can't resolve actual method */
743 method = jinfo_get_method (ji);
745 method = get_method_from_stack_frame (ji, generic_info);
746 if (jinfo_get_method (ji)->wrapper_type) {
750 s = mono_method_get_name_full (method, TRUE, FALSE, MONO_TYPE_NAME_FORMAT_REFLECTION);
751 MONO_OBJECT_SETREF (sf, internal_method_name, mono_string_new (domain, s));
755 MonoReflectionMethod *rm = mono_method_get_object_checked (domain, method, NULL, &error);
756 if (!mono_error_ok (&error)) {
757 mono_error_set_pending_exception (&error);
760 MONO_OBJECT_SETREF (sf, method, rm);
763 sf->method_index = ji->from_aot ? mono_aot_find_method_index (method) : 0xffffff;
764 sf->method_address = (gsize) ji->code_start;
765 sf->native_offset = (char *)ip - (char *)ji->code_start;
768 * mono_debug_lookup_source_location() returns both the file / line number information
769 * and the IL offset. Note that computing the IL offset is already an expensive
770 * operation, so we shouldn't call this method twice.
772 location = mono_debug_lookup_source_location (jinfo_get_method (ji), sf->native_offset, domain);
774 sf->il_offset = location->il_offset;
777 if (mono_find_prev_seq_point_for_native_offset (domain, jinfo_get_method (ji), sf->native_offset, NULL, &sp))
778 sf->il_offset = sp.il_offset;
783 if (need_file_info) {
784 if (location && location->source_file) {
785 MONO_OBJECT_SETREF (sf, filename, mono_string_new (domain, location->source_file));
786 sf->line = location->row;
787 sf->column = location->column;
789 sf->line = sf->column = 0;
794 mono_debug_free_source_location (location);
795 mono_array_setref (res, i, sf);
802 mono_runtime_walk_stack_with_ctx (MonoJitStackWalk func, MonoContext *start_ctx, MonoUnwindOptions unwind_options, void *user_data)
805 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
806 if (jit_tls && jit_tls->orig_ex_ctx_set)
807 start_ctx = &jit_tls->orig_ex_ctx;
809 mono_walk_stack_with_ctx (func, start_ctx, unwind_options, user_data);
812 * mono_walk_stack_with_ctx:
814 * Unwind the current thread starting at @start_ctx.
816 * If @start_ctx is null, we capture the current context.
819 mono_walk_stack_with_ctx (MonoJitStackWalk func, MonoContext *start_ctx, MonoUnwindOptions unwind_options, void *user_data)
821 MonoContext extra_ctx;
822 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
823 MONO_ARCH_CONTEXT_DEF
825 if (!thread || !thread->jit_data)
829 mono_arch_flush_register_windows ();
831 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
832 MONO_INIT_CONTEXT_FROM_CURRENT (&extra_ctx);
834 MONO_INIT_CONTEXT_FROM_FUNC (&extra_ctx, mono_walk_stack_with_ctx);
836 start_ctx = &extra_ctx;
839 mono_walk_stack_full (func, start_ctx, mono_domain_get (), (MonoJitTlsData *)thread->jit_data, mono_get_lmf (), unwind_options, user_data);
843 * mono_walk_stack_with_state:
845 * Unwind a thread described by @state.
847 * State must be valid (state->valid == TRUE).
849 * If you are using this function to unwind another thread, make sure it is suspended.
851 * If @state is null, we capture the current context.
854 mono_walk_stack_with_state (MonoJitStackWalk func, MonoThreadUnwindState *state, MonoUnwindOptions unwind_options, void *user_data)
856 MonoThreadUnwindState extra_state;
858 g_assert (!mono_thread_info_is_async_context ());
859 if (!mono_thread_state_init_from_current (&extra_state))
861 state = &extra_state;
864 g_assert (state->valid);
866 if (!state->unwind_data [MONO_UNWIND_DATA_DOMAIN])
870 mono_walk_stack_full (func,
872 (MonoDomain *)state->unwind_data [MONO_UNWIND_DATA_DOMAIN],
873 (MonoJitTlsData *)state->unwind_data [MONO_UNWIND_DATA_JIT_TLS],
874 (MonoLMF *)state->unwind_data [MONO_UNWIND_DATA_LMF],
875 unwind_options, user_data);
879 mono_walk_stack (MonoJitStackWalk func, MonoUnwindOptions options, void *user_data)
881 MonoThreadUnwindState state;
882 if (!mono_thread_state_init_from_current (&state))
884 mono_walk_stack_with_state (func, &state, options, user_data);
888 * mono_walk_stack_full:
889 * @func: callback to call for each stack frame
890 * @domain: starting appdomain, can be NULL to use the current domain
891 * @unwind_options: what extra information the unwinder should gather
892 * @start_ctx: starting state of the stack walk, can be NULL.
893 * @thread: the thread whose stack to walk, can be NULL to use the current thread
894 * @lmf: the LMF of @thread, can be NULL to use the LMF of the current thread
895 * @user_data: data passed to the callback
897 * This function walks the stack of a thread, starting from the state
898 * represented by start_ctx. For each frame the callback
899 * function is called with the relevant info. The walk ends when no more
900 * managed stack frames are found or when the callback returns a TRUE value.
903 mono_walk_stack_full (MonoJitStackWalk func, MonoContext *start_ctx, MonoDomain *domain, MonoJitTlsData *jit_tls, MonoLMF *lmf, MonoUnwindOptions unwind_options, gpointer user_data)
906 MonoContext ctx, new_ctx;
907 StackFrameInfo frame;
909 mgreg_t *reg_locations [MONO_MAX_IREGS];
910 mgreg_t *new_reg_locations [MONO_MAX_IREGS];
911 gboolean get_reg_locations = unwind_options & MONO_UNWIND_REG_LOCATIONS;
912 gboolean async = mono_thread_info_is_async_context ();
914 if (mono_llvm_only) {
920 ips = get_unwind_backtrace ();
921 for (l = ips; l; l = l->next) {
922 guint8 *ip = (guint8*)l->data;
923 memset (&frame, 0, sizeof (StackFrameInfo));
924 frame.ji = mini_jit_info_table_find (domain, (char*)ip, &frame.domain);
925 if (!frame.ji || frame.ji->is_trampoline)
927 frame.type = FRAME_TYPE_MANAGED;
928 frame.method = jinfo_get_method (frame.ji);
929 // FIXME: Cannot lookup the actual method
930 frame.actual_method = frame.method;
931 if (frame.type == FRAME_TYPE_MANAGED) {
932 if (!frame.method->wrapper_type || frame.method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
933 frame.managed = TRUE;
935 frame.native_offset = ip - (guint8*)frame.ji->code_start;
936 frame.il_offset = -1;
938 if (func (&frame, NULL, user_data))
945 g_assert (start_ctx);
948 /*The LMF will be null if the target have no managed frames.*/
949 /* g_assert (lmf); */
952 g_assert (unwind_options == MONO_UNWIND_NONE);
954 memcpy (&ctx, start_ctx, sizeof (MonoContext));
955 memset (reg_locations, 0, sizeof (reg_locations));
957 while (MONO_CONTEXT_GET_SP (&ctx) < jit_tls->end_of_stack) {
959 res = mono_find_jit_info_ext (domain, jit_tls, NULL, &ctx, &new_ctx, NULL, &lmf, get_reg_locations ? new_reg_locations : NULL, &frame);
963 if ((unwind_options & MONO_UNWIND_LOOKUP_IL_OFFSET) && frame.ji && !frame.ji->is_trampoline) {
964 MonoDebugSourceLocation *source;
966 source = mono_debug_lookup_source_location (jinfo_get_method (frame.ji), frame.native_offset, domain);
968 il_offset = source->il_offset;
971 if (mono_find_prev_seq_point_for_native_offset (domain, jinfo_get_method (frame.ji), frame.native_offset, NULL, &sp))
972 il_offset = sp.il_offset;
976 mono_debug_free_source_location (source);
980 frame.il_offset = il_offset;
982 if ((unwind_options & MONO_UNWIND_LOOKUP_ACTUAL_METHOD) && frame.ji && !frame.ji->is_trampoline) {
983 frame.actual_method = get_method_from_stack_frame (frame.ji, get_generic_info_from_stack_frame (frame.ji, &ctx));
985 frame.actual_method = frame.method;
988 if (get_reg_locations)
989 frame.reg_locations = reg_locations;
991 if (func (&frame, &ctx, user_data))
994 if (get_reg_locations) {
995 for (i = 0; i < MONO_MAX_IREGS; ++i)
996 if (new_reg_locations [i])
997 reg_locations [i] = new_reg_locations [i];
1005 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
1006 MonoReflectionMethod **method,
1007 gint32 *iloffset, gint32 *native_offset,
1008 MonoString **file, gint32 *line, gint32 *column)
1011 MonoDomain *domain = mono_domain_get ();
1012 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
1013 MonoLMF *lmf = mono_get_lmf ();
1014 MonoJitInfo *ji = NULL;
1015 MonoContext ctx, new_ctx;
1016 MonoDebugSourceLocation *location;
1017 MonoMethod *jmethod = NULL, *actual_method;
1018 StackFrameInfo frame;
1021 MONO_ARCH_CONTEXT_DEF;
1023 if (mono_llvm_only) {
1025 MonoDomain *frame_domain;
1026 guint8 *frame_ip = NULL;
1028 /* FIXME: Generalize this code with an interface which returns an array of StackFrame structures */
1030 ips = get_unwind_backtrace ();
1031 for (l = ips; l && skip >= 0; l = l->next) {
1032 guint8 *ip = (guint8*)l->data;
1036 ji = mini_jit_info_table_find (mono_domain_get (), (char*)ip, &frame_domain);
1037 if (!ji || ji->is_trampoline)
1040 /* The skip count passed by the caller depends on us not filtering out MANAGED_TO_NATIVE */
1041 jmethod = jinfo_get_method (ji);
1042 if (jmethod->wrapper_type != MONO_WRAPPER_NONE && jmethod->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD && jmethod->wrapper_type != MONO_WRAPPER_MANAGED_TO_NATIVE)
1049 /* No way to resolve generic instances */
1050 actual_method = jmethod;
1051 *native_offset = frame_ip - (guint8*)ji->code_start;
1053 mono_arch_flush_register_windows ();
1055 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
1056 MONO_INIT_CONTEXT_FROM_CURRENT (&ctx);
1058 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_get_frame_info);
1064 res = mono_find_jit_info_ext (domain, jit_tls, NULL, &ctx, &new_ctx, NULL, &lmf, NULL, &frame);
1068 if (frame.type == FRAME_TYPE_MANAGED_TO_NATIVE ||
1069 frame.type == FRAME_TYPE_DEBUGGER_INVOKE ||
1070 frame.type == FRAME_TYPE_TRAMPOLINE)
1074 *native_offset = frame.native_offset;
1076 /* The skip count passed by the caller depends on us not filtering out MANAGED_TO_NATIVE */
1077 jmethod = jinfo_get_method (ji);
1078 if (jmethod->wrapper_type != MONO_WRAPPER_NONE && jmethod->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD && jmethod->wrapper_type != MONO_WRAPPER_MANAGED_TO_NATIVE)
1081 } while (skip >= 0);
1083 actual_method = get_method_from_stack_frame (ji, get_generic_info_from_stack_frame (ji, &ctx));
1086 MonoReflectionMethod *rm = mono_method_get_object_checked (domain, actual_method, NULL, &error);
1087 if (!mono_error_ok (&error)) {
1088 mono_error_set_pending_exception (&error);
1091 mono_gc_wbarrier_generic_store (method, (MonoObject*) rm);
1093 location = mono_debug_lookup_source_location (jmethod, *native_offset, domain);
1095 *iloffset = location->il_offset;
1099 if (need_file_info) {
1101 mono_gc_wbarrier_generic_store (file, (MonoObject*) mono_string_new (domain, location->source_file));
1102 *line = location->row;
1103 *column = location->column;
1106 *line = *column = 0;
1110 mono_debug_free_source_location (location);
1116 get_exception_catch_class (MonoJitExceptionInfo *ei, MonoJitInfo *ji, MonoContext *ctx)
1118 MonoClass *catch_class = ei->data.catch_class;
1119 MonoType *inflated_type;
1120 MonoGenericContext context;
1122 /*MonoJitExceptionInfo::data is an union used by filter and finally clauses too.*/
1123 if (!catch_class || ei->flags != MONO_EXCEPTION_CLAUSE_NONE)
1126 if (!ji->has_generic_jit_info || !mono_jit_info_get_generic_jit_info (ji)->has_this)
1128 context = get_generic_context_from_stack_frame (ji, get_generic_info_from_stack_frame (ji, ctx));
1130 /* FIXME: we shouldn't inflate but instead put the
1131 type in the rgctx and fetch it from there. It
1132 might be a good idea to do this lazily, i.e. only
1133 when the exception is actually thrown, so as not to
1134 waste space for exception clauses which might never
1136 inflated_type = mono_class_inflate_generic_type (&catch_class->byval_arg, &context);
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)
1213 MonoAssembly *ass = m->klass->image->assembly;
1214 MonoCustomAttrInfo* attrs;
1217 gboolean val = FALSE;
1220 if (ass->wrap_non_exception_throws_inited)
1221 return ass->wrap_non_exception_throws;
1223 klass = mono_class_get_runtime_compat_attr_class ();
1225 attrs = mono_custom_attrs_from_assembly (ass);
1227 for (i = 0; i < attrs->num_attrs; ++i) {
1228 MonoCustomAttrEntry *attr = &attrs->attrs [i];
1230 int num_named, named_type, name_len;
1233 if (!attr->ctor || attr->ctor->klass != klass)
1235 /* Decode the RuntimeCompatibilityAttribute. See reflection.c */
1236 p = (const char*)attr->data;
1237 g_assert (read16 (p) == 0x0001);
1239 num_named = read16 (p);
1245 /* data_type = *p; */
1248 if (named_type != 0x54)
1250 name_len = mono_metadata_decode_blob_size (p, &p);
1251 name = (char *)g_malloc (name_len + 1);
1252 memcpy (name, p, name_len);
1253 name [name_len] = 0;
1255 g_assert (!strcmp (name, "WrapNonExceptionThrows"));
1257 /* The value is a BOOLEAN */
1260 mono_custom_attrs_free (attrs);
1263 ass->wrap_non_exception_throws = val;
1264 mono_memory_barrier ();
1265 ass->wrap_non_exception_throws_inited = TRUE;
1270 #ifndef MONO_ARCH_STACK_GROWS_UP
1271 #define DOES_STACK_GROWS_UP 1
1273 #define DOES_STACK_GROWS_UP 0
1276 #define MAX_UNMANAGED_BACKTRACE 128
1278 build_native_trace (void)
1280 /* This puppy only makes sense on mobile, IOW, ARM. */
1281 #if defined (HAVE_BACKTRACE_SYMBOLS) && defined (TARGET_ARM)
1283 void *native_trace [MAX_UNMANAGED_BACKTRACE];
1284 int size = backtrace (native_trace, MAX_UNMANAGED_BACKTRACE);
1289 res = mono_array_new (mono_domain_get (), mono_defaults.int_class, size);
1291 for (i = 0; i < size; i++)
1292 mono_array_set (res, gpointer, i, native_trace [i]);
1300 setup_stack_trace (MonoException *mono_ex, GSList *dynamic_methods, MonoArray *initial_trace_ips, GList **trace_ips)
1302 if (mono_ex && !initial_trace_ips) {
1303 *trace_ips = g_list_reverse (*trace_ips);
1304 MONO_OBJECT_SETREF (mono_ex, trace_ips, mono_glist_to_array (*trace_ips, mono_defaults.int_class));
1305 MONO_OBJECT_SETREF (mono_ex, native_trace_ips, build_native_trace ());
1306 if (dynamic_methods) {
1307 /* These methods could go away anytime, so save a reference to them in the exception object */
1309 MonoMList *list = NULL;
1311 for (l = dynamic_methods; l; l = l->next) {
1313 MonoDomain *domain = mono_domain_get ();
1315 if (domain->method_to_dyn_method) {
1316 mono_domain_lock (domain);
1317 dis_link = (guint32)(size_t)g_hash_table_lookup (domain->method_to_dyn_method, l->data);
1318 mono_domain_unlock (domain);
1320 MonoObject *o = mono_gchandle_get_target (dis_link);
1322 list = mono_mlist_prepend (list, o);
1328 MONO_OBJECT_SETREF (mono_ex, dynamic_methods, list);
1331 g_list_free (*trace_ips);
1336 * mono_handle_exception_internal_first_pass:
1338 * The first pass of exception handling. Unwind the stack until a catch clause which can catch
1339 * OBJ is found. Run the index of the filter clause which caught the exception into
1340 * OUT_FILTER_IDX. Return TRUE if the exception is caught, FALSE otherwise.
1343 mono_handle_exception_internal_first_pass (MonoContext *ctx, MonoObject *obj, gint32 *out_filter_idx, MonoJitInfo **out_ji, MonoJitInfo **out_prev_ji, MonoObject *non_exception)
1345 MonoDomain *domain = mono_domain_get ();
1346 MonoJitInfo *ji = NULL;
1347 static int (*call_filter) (MonoContext *, gpointer) = NULL;
1348 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
1349 MonoLMF *lmf = mono_get_lmf ();
1350 MonoArray *initial_trace_ips = NULL;
1351 GList *trace_ips = NULL;
1352 GSList *dynamic_methods = NULL;
1353 MonoException *mono_ex;
1354 gboolean stack_overflow = FALSE;
1355 MonoContext initial_ctx;
1357 int frame_count = 0;
1362 g_assert (ctx != NULL);
1364 if (obj == (MonoObject *)domain->stack_overflow_ex)
1365 stack_overflow = TRUE;
1367 mono_ex = (MonoException*)obj;
1368 initial_trace_ips = mono_ex->trace_ips;
1370 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
1371 mono_ex = (MonoException*)obj;
1372 initial_trace_ips = mono_ex->trace_ips;
1378 call_filter = (int (*) (MonoContext *, void *))mono_get_call_filter ();
1380 g_assert (jit_tls->end_of_stack);
1381 g_assert (jit_tls->abort_func);
1384 *out_filter_idx = -1;
1388 *out_prev_ji = NULL;
1393 MonoContext new_ctx;
1395 int clause_index_start = 0;
1396 gboolean unwind_res = TRUE;
1398 StackFrameInfo frame;
1403 unwind_res = mono_find_jit_info_ext (domain, jit_tls, NULL, ctx, &new_ctx, NULL, &lmf, NULL, &frame);
1405 if (frame.type == FRAME_TYPE_DEBUGGER_INVOKE ||
1406 frame.type == FRAME_TYPE_MANAGED_TO_NATIVE ||
1407 frame.type == FRAME_TYPE_TRAMPOLINE) {
1411 g_assert (frame.type == FRAME_TYPE_MANAGED);
1416 setup_stack_trace (mono_ex, dynamic_methods, initial_trace_ips, &trace_ips);
1417 g_slist_free (dynamic_methods);
1422 method = jinfo_get_method (ji);
1423 //printf ("M: %s %d.\n", mono_method_full_name (method, TRUE), frame_count);
1425 if (mini_get_debug_options ()->reverse_pinvoke_exceptions && method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
1426 g_error ("A native frame was found while unwinding the stack after an exception.\n"
1427 "The native frame called the managed method:\n%s\n",
1428 mono_method_full_name (method, TRUE));
1431 if (method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
1433 * Avoid overwriting the stack trace if the exception is
1434 * rethrown. Also avoid giant stack traces during a stack
1437 if (!initial_trace_ips && (frame_count < 1000)) {
1438 trace_ips = g_list_prepend (trace_ips, MONO_CONTEXT_GET_IP (ctx));
1439 trace_ips = g_list_prepend (trace_ips,
1440 get_generic_info_from_stack_frame (ji, ctx));
1444 if (method->dynamic)
1445 dynamic_methods = g_slist_prepend (dynamic_methods, method);
1447 if (stack_overflow) {
1448 if (DOES_STACK_GROWS_UP)
1449 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx));
1451 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (ctx));
1453 free_stack = 0xffffff;
1456 for (i = clause_index_start; i < ji->num_clauses; i++) {
1457 MonoJitExceptionInfo *ei = &ji->clauses [i];
1458 gboolean filtered = FALSE;
1461 * During stack overflow, wait till the unwinding frees some stack
1462 * space before running handlers/finalizers.
1464 if (free_stack <= (64 * 1024))
1467 if (is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (ctx))) {
1469 MonoClass *catch_class = get_exception_catch_class (ei, ji, ctx);
1472 * Have to unwrap RuntimeWrappedExceptions if the
1473 * method's assembly doesn't have a RuntimeCompatibilityAttribute.
1475 if (non_exception && !wrap_non_exception_throws (method))
1476 ex_obj = non_exception;
1480 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
1481 gboolean is_user_frame = method->wrapper_type == MONO_WRAPPER_NONE || method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD;
1482 #ifndef DISABLE_PERFCOUNTERS
1483 mono_perfcounters->exceptions_filters++;
1486 Here's the thing, if this is a filter clause done by a wrapper like runtime invoke, we don't want to
1487 trim the stackframe since if it returns FALSE we lose information.
1489 FIXME Not 100% sure if it's a good idea even with user clauses.
1492 setup_stack_trace (mono_ex, dynamic_methods, initial_trace_ips, &trace_ips);
1494 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
1496 MONO_CONTEXT_SET_LLVM_EXC_REG (ctx, ex_obj);
1498 /* Can't pass the ex object in a register yet to filter clauses, because call_filter () might not support it */
1499 *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = ex_obj;
1501 g_assert (!ji->from_llvm);
1502 /* store the exception object in bp + ei->exvar_offset */
1503 *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = ex_obj;
1506 #ifdef MONO_CONTEXT_SET_LLVM_EH_SELECTOR_REG
1508 * Pass the original il clause index to the landing pad so it can
1509 * branch to the landing pad associated with the il clause.
1510 * This is needed because llvm compiled code assumes that the EH
1511 * code always branches to the innermost landing pad.
1514 MONO_CONTEXT_SET_LLVM_EH_SELECTOR_REG (ctx, ei->clause_index);
1517 mono_debugger_agent_begin_exception_filter (mono_ex, ctx, &initial_ctx);
1518 filtered = call_filter (ctx, ei->data.filter);
1519 mono_debugger_agent_end_exception_filter (mono_ex, ctx, &initial_ctx);
1520 if (filtered && out_filter_idx)
1521 *out_filter_idx = filter_idx;
1528 setup_stack_trace (mono_ex, dynamic_methods, initial_trace_ips, &trace_ips);
1529 g_slist_free (dynamic_methods);
1530 /* mono_debugger_agent_handle_exception () needs this */
1531 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1536 if (ei->flags == MONO_EXCEPTION_CLAUSE_NONE && mono_object_isinst (ex_obj, catch_class)) {
1537 setup_stack_trace (mono_ex, dynamic_methods, initial_trace_ips, &trace_ips);
1538 g_slist_free (dynamic_methods);
1543 /* mono_debugger_agent_handle_exception () needs this */
1544 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1553 g_assert_not_reached ();
1557 * mono_handle_exception_internal:
1558 * @ctx: saved processor state
1559 * @obj: the exception object
1560 * @resume: whenever to resume unwinding based on the state in MonoJitTlsData.
1563 mono_handle_exception_internal (MonoContext *ctx, MonoObject *obj, gboolean resume, MonoJitInfo **out_ji)
1566 MonoDomain *domain = mono_domain_get ();
1567 MonoJitInfo *ji, *prev_ji;
1568 static int (*call_filter) (MonoContext *, gpointer) = NULL;
1569 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
1570 MonoLMF *lmf = mono_get_lmf ();
1571 MonoException *mono_ex;
1572 gboolean stack_overflow = FALSE;
1573 MonoContext initial_ctx;
1575 int frame_count = 0;
1576 gint32 filter_idx, first_filter_idx = 0;
1579 MonoObject *non_exception = NULL;
1581 g_assert (ctx != NULL);
1583 MonoException *ex = mono_get_exception_null_reference ();
1584 MONO_OBJECT_SETREF (ex, message, mono_string_new (domain, "Object reference not set to an instance of an object"));
1585 obj = (MonoObject *)ex;
1589 * Allocate a new exception object instead of the preconstructed ones.
1591 if (obj == (MonoObject *)domain->stack_overflow_ex) {
1593 * It is not a good idea to try and put even more pressure on the little stack available.
1594 * obj = mono_get_exception_stack_overflow ();
1596 stack_overflow = TRUE;
1598 else if (obj == (MonoObject *)domain->null_reference_ex) {
1599 obj = (MonoObject *)mono_get_exception_null_reference ();
1602 if (!mono_object_isinst (obj, mono_defaults.exception_class)) {
1603 non_exception = obj;
1604 obj = (MonoObject *)mono_get_exception_runtime_wrapped (obj);
1607 mono_ex = (MonoException*)obj;
1609 if (mini_get_debug_options ()->suspend_on_exception) {
1610 mono_runtime_printf_err ("Exception thrown, suspending...");
1615 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
1616 mono_ex = (MonoException*)obj;
1621 if (mono_ex && jit_tls->class_cast_from) {
1622 if (!strcmp (mono_ex->object.vtable->klass->name, "InvalidCastException")) {
1623 char *from_name = mono_type_get_full_name (jit_tls->class_cast_from);
1624 char *to_name = mono_type_get_full_name (jit_tls->class_cast_to);
1625 char *msg = g_strdup_printf ("Unable to cast object of type '%s' to type '%s'.", from_name, to_name);
1626 mono_ex->message = mono_string_new (domain, msg);
1631 if (!strcmp (mono_ex->object.vtable->klass->name, "ArrayTypeMismatchException")) {
1632 char *from_name = mono_type_get_full_name (jit_tls->class_cast_from);
1633 char *to_name = mono_type_get_full_name (jit_tls->class_cast_to);
1634 char *msg = g_strdup_printf ("Source array of type '%s' cannot be cast to destination array type '%s'.", from_name, to_name);
1635 mono_ex->message = mono_string_new (domain, msg);
1643 call_filter = (int (*)(MonoContext *, void*))mono_get_call_filter ();
1645 g_assert (jit_tls->end_of_stack);
1646 g_assert (jit_tls->abort_func);
1649 * We set orig_ex_ctx_set to TRUE/FALSE around profiler calls to make sure it doesn't
1650 * end up being TRUE on any code path.
1652 memcpy (&jit_tls->orig_ex_ctx, ctx, sizeof (MonoContext));
1657 MonoContext ctx_cp = *ctx;
1658 if (mono_trace_is_enabled ()) {
1659 MonoMethod *system_exception_get_message = mono_class_get_method_from_name (mono_defaults.exception_class, "get_Message", 0);
1660 MonoMethod *get_message = system_exception_get_message == NULL ? NULL : mono_object_get_virtual_method (obj, system_exception_get_message);
1661 MonoObject *message;
1662 const char *type_name = mono_class_get_name (mono_object_class (mono_ex));
1664 if (get_message == NULL) {
1666 } else if (!strcmp (type_name, "OutOfMemoryException") || !strcmp (type_name, "StackOverflowException")) {
1668 msg = g_strdup_printf ("(No exception message for: %s)\n", type_name);
1670 MonoObject *exc = NULL;
1671 message = mono_runtime_try_invoke (get_message, obj, NULL, &exc, &error);
1672 g_assert (exc == NULL);
1673 mono_error_assert_ok (&error);
1676 msg = message ? mono_string_to_utf8 ((MonoString *) message) : g_strdup ("(System.Exception.Message property not available)");
1678 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);
1680 if (mono_ex && mono_trace_eval_exception (mono_object_class (mono_ex)))
1681 mono_print_thread_dump_from_ctx (ctx);
1683 jit_tls->orig_ex_ctx_set = TRUE;
1684 mono_profiler_exception_thrown (obj);
1685 jit_tls->orig_ex_ctx_set = FALSE;
1687 res = mono_handle_exception_internal_first_pass (&ctx_cp, obj, &first_filter_idx, &ji, &prev_ji, non_exception);
1690 if (mini_get_debug_options ()->break_on_exc)
1692 mono_debugger_agent_handle_exception ((MonoException *)obj, ctx, NULL);
1694 if (mini_get_debug_options ()->suspend_on_unhandled) {
1695 mono_runtime_printf_err ("Unhandled exception, suspending...");
1700 // FIXME: This runs managed code so it might cause another stack overflow when
1701 // we are handling a stack overflow
1702 mono_unhandled_exception (obj);
1704 gboolean unhandled = FALSE;
1707 * The exceptions caught by the mono_runtime_invoke_checked () calls
1708 * in the threadpool needs to be treated as unhandled (#669836).
1710 * FIXME: The check below is hackish, but its hard to distinguish
1711 * these runtime invoke calls from others in the runtime.
1713 if (ji && jinfo_get_method (ji)->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
1714 if (prev_ji && jinfo_get_method (prev_ji) == mono_defaults.threadpool_perform_wait_callback_method)
1719 mono_debugger_agent_handle_exception ((MonoException *)obj, ctx, NULL);
1721 mono_debugger_agent_handle_exception ((MonoException *)obj, ctx, &ctx_cp);
1731 MonoContext new_ctx;
1733 int clause_index_start = 0;
1734 gboolean unwind_res = TRUE;
1738 ji = jit_tls->resume_state.ji;
1739 new_ctx = jit_tls->resume_state.new_ctx;
1740 clause_index_start = jit_tls->resume_state.clause_index;
1741 lmf = jit_tls->resume_state.lmf;
1742 first_filter_idx = jit_tls->resume_state.first_filter_idx;
1743 filter_idx = jit_tls->resume_state.filter_idx;
1745 StackFrameInfo frame;
1747 unwind_res = mono_find_jit_info_ext (domain, jit_tls, NULL, ctx, &new_ctx, NULL, &lmf, NULL, &frame);
1749 if (frame.type == FRAME_TYPE_DEBUGGER_INVOKE ||
1750 frame.type == FRAME_TYPE_MANAGED_TO_NATIVE ||
1751 frame.type == FRAME_TYPE_TRAMPOLINE) {
1755 g_assert (frame.type == FRAME_TYPE_MANAGED);
1761 *(mono_get_lmf_addr ()) = lmf;
1763 jit_tls->abort_func (obj);
1764 g_assert_not_reached ();
1767 method = jinfo_get_method (ji);
1769 //printf ("M: %s %d.\n", mono_method_full_name (method, TRUE), frame_count);
1771 if (stack_overflow) {
1772 if (DOES_STACK_GROWS_UP)
1773 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx));
1775 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (ctx));
1777 free_stack = 0xffffff;
1780 for (i = clause_index_start; i < ji->num_clauses; i++) {
1781 MonoJitExceptionInfo *ei = &ji->clauses [i];
1782 gboolean filtered = FALSE;
1785 * During stack overflow, wait till the unwinding frees some stack
1786 * space before running handlers/finalizers.
1788 if (free_stack <= (64 * 1024))
1791 if (is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (ctx))) {
1793 MonoClass *catch_class = get_exception_catch_class (ei, ji, ctx);
1796 * Have to unwrap RuntimeWrappedExceptions if the
1797 * method's assembly doesn't have a RuntimeCompatibilityAttribute.
1799 if (non_exception && !wrap_non_exception_throws (method))
1800 ex_obj = non_exception;
1804 if (((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER))) {
1805 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
1806 MONO_CONTEXT_SET_LLVM_EXC_REG (ctx, ex_obj);
1808 g_assert (!ji->from_llvm);
1809 /* store the exception object in bp + ei->exvar_offset */
1810 *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = ex_obj;
1814 #ifdef MONO_CONTEXT_SET_LLVM_EH_SELECTOR_REG
1816 MONO_CONTEXT_SET_LLVM_EH_SELECTOR_REG (ctx, ei->clause_index);
1819 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
1821 * Filter clauses should only be run in the
1822 * first pass of exception handling.
1824 filtered = (filter_idx == first_filter_idx);
1828 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
1829 mono_object_isinst (ex_obj, catch_class)) || filtered) {
1831 * This guards against the situation that we abort a thread that is executing a finally clause
1832 * that was called by the EH machinery. It won't have a guard trampoline installed, so we must
1833 * check for this situation here and resume interruption if we are below the guarded block.
1835 if (G_UNLIKELY (jit_tls->handler_block_return_address)) {
1836 gboolean is_outside = FALSE;
1837 gpointer prot_bp = MONO_CONTEXT_GET_BP (&jit_tls->handler_block_context);
1838 gpointer catch_bp = MONO_CONTEXT_GET_BP (ctx);
1839 //FIXME make this stack direction aware
1840 if (catch_bp > prot_bp) {
1842 } else if (catch_bp == prot_bp) {
1843 /* Can be either try { try { } catch {} } finally {} or try { try { } finally {} } catch {}
1844 * So we check if the catch handler_start is protected by the guarded handler protected region
1847 * If there is an outstanding guarded_block return address, it means the current thread must be aborted.
1848 * This is the only way to reach out the guarded block as other cases are handled by the trampoline.
1849 * There aren't any further finally/fault handler blocks down the stack over this exception.
1850 * This must be ensured by the code that installs the guard trampoline.
1852 g_assert (ji == mini_jit_info_table_find (domain, (char *)MONO_CONTEXT_GET_IP (&jit_tls->handler_block_context), NULL));
1854 if (!is_address_protected (ji, jit_tls->handler_block, ei->handler_start)) {
1859 jit_tls->handler_block_return_address = NULL;
1860 jit_tls->handler_block = NULL;
1861 mono_thread_resume_interruption (); /*We ignore the exception here, it will be raised later*/
1865 if (mono_trace_is_enabled () && mono_trace_eval (method))
1866 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (method, TRUE));
1867 jit_tls->orig_ex_ctx_set = TRUE;
1868 mono_profiler_exception_clause_handler (method, ei->flags, i);
1869 jit_tls->orig_ex_ctx_set = FALSE;
1870 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1872 #ifndef DISABLE_PERFCOUNTERS
1873 mono_perfcounters->exceptions_depth += frame_count;
1875 if (obj == (MonoObject *)domain->stack_overflow_ex)
1876 jit_tls->handling_stack_ovf = FALSE;
1880 if (ei->flags == MONO_EXCEPTION_CLAUSE_FAULT) {
1881 if (mono_trace_is_enabled () && mono_trace_eval (method))
1882 g_print ("EXCEPTION: fault clause %d of %s\n", i, mono_method_full_name (method, TRUE));
1883 jit_tls->orig_ex_ctx_set = TRUE;
1884 mono_profiler_exception_clause_handler (method, ei->flags, i);
1885 jit_tls->orig_ex_ctx_set = FALSE;
1886 call_filter (ctx, ei->handler_start);
1888 if (ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
1889 if (mono_trace_is_enabled () && mono_trace_eval (method))
1890 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (method, TRUE));
1891 jit_tls->orig_ex_ctx_set = TRUE;
1892 mono_profiler_exception_clause_handler (method, ei->flags, i);
1893 jit_tls->orig_ex_ctx_set = FALSE;
1894 #ifndef DISABLE_PERFCOUNTERS
1895 mono_perfcounters->exceptions_finallys++;
1898 if (ji->from_llvm) {
1900 * LLVM compiled finally handlers follow the design
1901 * of the c++ ehabi, i.e. they call a resume function
1902 * at the end instead of returning to the caller.
1903 * So save the exception handling state,
1904 * mono_resume_unwind () will call us again to continue
1907 jit_tls->resume_state.ex_obj = obj;
1908 jit_tls->resume_state.ji = ji;
1909 jit_tls->resume_state.clause_index = i + 1;
1910 jit_tls->resume_state.ctx = *ctx;
1911 jit_tls->resume_state.new_ctx = new_ctx;
1912 jit_tls->resume_state.lmf = lmf;
1913 jit_tls->resume_state.first_filter_idx = first_filter_idx;
1914 jit_tls->resume_state.filter_idx = filter_idx;
1915 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1918 call_filter (ctx, ei->handler_start);
1924 jit_tls->orig_ex_ctx_set = TRUE;
1925 mono_profiler_exception_method_leave (method);
1926 jit_tls->orig_ex_ctx_set = FALSE;
1931 g_assert_not_reached ();
1935 * mono_debugger_run_finally:
1936 * @start_ctx: saved processor state
1938 * This method is called by the Mono Debugger to call all `finally' clauses of the
1939 * current stack frame. It's used when the user issues a `return' command to make
1940 * the current stack frame return. After returning from this method, the debugger
1941 * unwinds the stack one frame and gives control back to the user.
1943 * NOTE: This method is only used when running inside the Mono Debugger.
1946 mono_debugger_run_finally (MonoContext *start_ctx)
1948 static int (*call_filter) (MonoContext *, gpointer) = NULL;
1949 MonoDomain *domain = mono_domain_get ();
1950 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
1951 MonoLMF *lmf = mono_get_lmf ();
1952 MonoContext ctx, new_ctx;
1953 MonoJitInfo *ji, rji;
1958 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, NULL, NULL);
1959 if (!ji || ji == (gpointer)-1)
1963 call_filter = (int (*)(MonoContext *, void *))mono_get_call_filter ();
1965 for (i = 0; i < ji->num_clauses; i++) {
1966 MonoJitExceptionInfo *ei = &ji->clauses [i];
1968 if (is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (&ctx)) &&
1969 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1970 call_filter (&ctx, ei->handler_start);
1976 * mono_handle_exception:
1977 * @ctx: saved processor state
1978 * @obj: the exception object
1981 mono_handle_exception (MonoContext *ctx, MonoObject *obj)
1983 #ifndef DISABLE_PERFCOUNTERS
1984 mono_perfcounters->exceptions_thrown++;
1987 return mono_handle_exception_internal (ctx, obj, FALSE, NULL);
1990 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
1992 #ifndef MONO_ARCH_USE_SIGACTION
1993 #error "Can't use sigaltstack without sigaction"
1996 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
1999 mono_setup_altstack (MonoJitTlsData *tls)
2003 guint8 *staddr = NULL;
2005 if (mono_running_on_valgrind ())
2008 mono_thread_info_get_stack_bounds (&staddr, &stsize);
2012 tls->end_of_stack = staddr + stsize;
2013 tls->stack_size = stsize;
2015 /*g_print ("thread %p, stack_base: %p, stack_size: %d\n", (gpointer)pthread_self (), staddr, stsize);*/
2017 tls->stack_ovf_guard_base = staddr + mono_pagesize ();
2018 tls->stack_ovf_guard_size = ALIGN_TO (8 * 4096, mono_pagesize ());
2020 g_assert ((guint8*)&sa >= (guint8*)tls->stack_ovf_guard_base + tls->stack_ovf_guard_size);
2022 if (mono_mprotect (tls->stack_ovf_guard_base, tls->stack_ovf_guard_size, MONO_MMAP_NONE)) {
2023 /* mprotect can fail for the main thread stack */
2024 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);
2025 g_assert (gaddr == tls->stack_ovf_guard_base);
2026 tls->stack_ovf_valloced = TRUE;
2029 /* Setup an alternate signal stack */
2030 tls->signal_stack = mono_valloc (0, MONO_ARCH_SIGNAL_STACK_SIZE, MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_PRIVATE|MONO_MMAP_ANON);
2031 tls->signal_stack_size = MONO_ARCH_SIGNAL_STACK_SIZE;
2033 g_assert (tls->signal_stack);
2035 sa.ss_sp = tls->signal_stack;
2036 sa.ss_size = MONO_ARCH_SIGNAL_STACK_SIZE;
2038 g_assert (sigaltstack (&sa, NULL) == 0);
2040 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);
2044 mono_free_altstack (MonoJitTlsData *tls)
2049 sa.ss_sp = tls->signal_stack;
2050 sa.ss_size = MONO_ARCH_SIGNAL_STACK_SIZE;
2051 sa.ss_flags = SS_DISABLE;
2052 err = sigaltstack (&sa, NULL);
2053 g_assert (err == 0);
2055 if (tls->signal_stack)
2056 mono_vfree (tls->signal_stack, MONO_ARCH_SIGNAL_STACK_SIZE);
2057 if (tls->stack_ovf_valloced)
2058 mono_vfree (tls->stack_ovf_guard_base, tls->stack_ovf_guard_size);
2060 mono_mprotect (tls->stack_ovf_guard_base, tls->stack_ovf_guard_size, MONO_MMAP_READ|MONO_MMAP_WRITE);
2063 #else /* !MONO_ARCH_SIGSEGV_ON_ALTSTACK */
2066 mono_setup_altstack (MonoJitTlsData *tls)
2071 mono_free_altstack (MonoJitTlsData *tls)
2075 #endif /* MONO_ARCH_SIGSEGV_ON_ALTSTACK */
2078 try_restore_stack_protection (MonoJitTlsData *jit_tls, int extra_bytes)
2080 gint32 unprotect_size = jit_tls->stack_ovf_guard_size;
2081 /* we need to leave some room for throwing the exception */
2082 while (unprotect_size >= 0 && (char*)jit_tls->stack_ovf_guard_base + unprotect_size > ((char*)&unprotect_size - extra_bytes))
2083 unprotect_size -= mono_pagesize ();
2084 /* at this point we could try and build a new domain->stack_overflow_ex, but only if there
2085 * is sufficient stack
2087 //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);
2089 mono_mprotect (jit_tls->stack_ovf_guard_base, unprotect_size, MONO_MMAP_NONE);
2090 return unprotect_size == jit_tls->stack_ovf_guard_size;
2093 static G_GNUC_UNUSED void
2094 try_more_restore (void)
2096 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
2097 if (try_restore_stack_protection (jit_tls, 500))
2098 jit_tls->restore_stack_prot = NULL;
2101 static G_GNUC_UNUSED void
2102 restore_stack_protection (void)
2104 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
2105 MonoException *ex = mono_domain_get ()->stack_overflow_ex;
2106 /* if we can't restore the stack protection, keep a callback installed so
2107 * we'll try to restore as much stack as we can at each return from unmanaged
2110 if (try_restore_stack_protection (jit_tls, 4096))
2111 jit_tls->restore_stack_prot = NULL;
2113 jit_tls->restore_stack_prot = try_more_restore_tramp;
2114 /* here we also throw a stack overflow exception */
2115 ex->trace_ips = NULL;
2116 ex->stack_trace = NULL;
2117 mono_raise_exception (ex);
2121 mono_altstack_restore_prot (mgreg_t *regs, guint8 *code, gpointer *tramp_data, guint8* tramp)
2123 void (*func)(void) = (void (*)(void))tramp_data;
2129 mono_handle_soft_stack_ovf (MonoJitTlsData *jit_tls, MonoJitInfo *ji, void *ctx, MONO_SIG_HANDLER_INFO_TYPE *siginfo, guint8* fault_addr)
2134 /* we got a stack overflow in the soft-guard pages
2135 * There are two cases:
2136 * 1) managed code caused the overflow: we unprotect the soft-guard page
2137 * and let the arch-specific code trigger the exception handling mechanism
2138 * in the thread stack. The soft-guard pages will be protected again as the stack is unwound.
2139 * 2) unmanaged code caused the overflow: we unprotect the soft-guard page
2140 * and hope we can continue with those enabled, at least until the hard-guard page
2141 * is hit. The alternative to continuing here is to just print a message and abort.
2142 * We may add in the future the code to protect the pages again in the codepath
2143 * when we return from unmanaged to managed code.
2145 if (jit_tls->stack_ovf_guard_size && fault_addr >= (guint8*)jit_tls->stack_ovf_guard_base &&
2146 fault_addr < (guint8*)jit_tls->stack_ovf_guard_base + jit_tls->stack_ovf_guard_size) {
2147 /* we unprotect the minimum amount we can */
2149 gboolean handled = FALSE;
2151 guard_size = jit_tls->stack_ovf_guard_size - (mono_pagesize () * SIZEOF_VOID_P / 4);
2152 while (guard_size && fault_addr < (guint8*)jit_tls->stack_ovf_guard_base + guard_size) {
2153 guard_size -= mono_pagesize ();
2155 guard_size = jit_tls->stack_ovf_guard_size - guard_size;
2156 /*fprintf (stderr, "unprotecting: %d\n", guard_size);*/
2157 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);
2158 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
2160 mono_arch_handle_altstack_exception (ctx, siginfo, fault_addr, TRUE);
2165 /* We print a message: after this even managed stack overflows
2166 * may crash the runtime
2168 mono_runtime_printf_err ("Stack overflow in unmanaged: IP: %p, fault addr: %p", mono_arch_ip_from_context (ctx), fault_addr);
2169 if (!jit_tls->handling_stack_ovf) {
2170 jit_tls->restore_stack_prot = restore_stack_protection_tramp;
2171 jit_tls->handling_stack_ovf = 1;
2173 /*fprintf (stderr, "Already handling stack overflow\n");*/
2182 MonoMethod *omethod;
2184 } PrintOverflowUserData;
2186 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2188 print_overflow_stack_frame (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
2190 MonoMethod *method = NULL;
2191 PrintOverflowUserData *user_data = (PrintOverflowUserData *)data;
2194 if (frame->ji && frame->type != FRAME_TYPE_TRAMPOLINE)
2195 method = jinfo_get_method (frame->ji);
2198 if (user_data->count == 0) {
2199 /* The first frame is in its prolog, so a line number cannot be computed */
2200 user_data->count ++;
2204 /* If this is a one method overflow, skip the other instances */
2205 if (method == user_data->omethod)
2208 location = mono_debug_print_stack_frame (method, frame->native_offset, mono_domain_get ());
2209 mono_runtime_printf_err (" %s", location);
2212 if (user_data->count == 1) {
2213 mono_runtime_printf_err (" <...>");
2214 user_data->omethod = method;
2216 user_data->omethod = NULL;
2219 user_data->count ++;
2221 mono_runtime_printf_err (" at <unknown> <0x%05x>", frame->native_offset);
2228 mono_handle_hard_stack_ovf (MonoJitTlsData *jit_tls, MonoJitInfo *ji, void *ctx, guint8* fault_addr)
2230 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2231 PrintOverflowUserData ud;
2235 /* we don't do much now, but we can warn the user with a useful message */
2236 mono_runtime_printf_err ("Stack overflow: IP: %p, fault addr: %p", mono_arch_ip_from_context (ctx), fault_addr);
2238 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2239 mono_sigctx_to_monoctx (ctx, &mctx);
2241 mono_runtime_printf_err ("Stacktrace:");
2243 memset (&ud, 0, sizeof (ud));
2245 mono_walk_stack_with_ctx (print_overflow_stack_frame, &mctx, MONO_UNWIND_LOOKUP_ACTUAL_METHOD, &ud);
2247 if (ji && !ji->is_trampoline && jinfo_get_method (ji))
2248 mono_runtime_printf_err ("At %s", mono_method_full_name (jinfo_get_method (ji), TRUE));
2250 mono_runtime_printf_err ("At <unmanaged>.");
2257 print_stack_frame_to_stderr (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
2259 MonoMethod *method = NULL;
2261 if (frame->ji && frame->type != FRAME_TYPE_TRAMPOLINE)
2262 method = jinfo_get_method (frame->ji);
2265 gchar *location = mono_debug_print_stack_frame (method, frame->native_offset, mono_domain_get ());
2266 mono_runtime_printf_err (" %s", location);
2269 mono_runtime_printf_err (" at <unknown> <0x%05x>", frame->native_offset);
2274 static G_GNUC_UNUSED gboolean
2275 print_stack_frame_to_string (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
2277 GString *p = (GString*)data;
2278 MonoMethod *method = NULL;
2280 if (frame->ji && frame->type != FRAME_TYPE_TRAMPOLINE)
2281 method = jinfo_get_method (frame->ji);
2283 if (method && frame->domain) {
2284 gchar *location = mono_debug_print_stack_frame (method, frame->native_offset, frame->domain);
2285 g_string_append_printf (p, " %s\n", location);
2288 g_string_append_printf (p, " at <unknown> <0x%05x>\n", frame->native_offset);
2293 #ifndef MONO_CROSS_COMPILE
2295 static gboolean handling_sigsegv = FALSE;
2298 * mono_handle_native_sigsegv:
2300 * Handle a SIGSEGV received while in native code by printing diagnostic
2301 * information and aborting.
2304 mono_handle_native_sigsegv (int signal, void *ctx, MONO_SIG_HANDLER_INFO_TYPE *info)
2306 #ifdef MONO_ARCH_USE_SIGACTION
2307 struct sigaction sa;
2309 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
2310 const char *signal_str = (signal == SIGSEGV) ? "SIGSEGV" : "SIGABRT";
2312 if (handling_sigsegv)
2315 if (mini_get_debug_options ()->suspend_on_sigsegv) {
2316 mono_runtime_printf_err ("Received SIGSEGV, suspending...");
2327 /* To prevent infinite loops when the stack walk causes a crash */
2328 handling_sigsegv = TRUE;
2330 /* !jit_tls means the thread was not registered with the runtime */
2331 if (jit_tls && mono_thread_internal_current ()) {
2332 mono_runtime_printf_err ("Stacktrace:\n");
2334 /* FIXME: Is MONO_UNWIND_LOOKUP_IL_OFFSET correct here? */
2335 mono_walk_stack (print_stack_frame_to_stderr, MONO_UNWIND_LOOKUP_IL_OFFSET, 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) {
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 ();
2368 #if defined (HAVE_PRCTL) && defined(PR_SET_PTRACER)
2370 // Allow gdb to attach to the process even if ptrace_scope sysctl variable is set to
2371 // a value other than 0 (the most permissive ptrace scope). Most modern Linux
2372 // distributions set the scope to 1 which allows attaching only to direct children of
2373 // the current process
2374 prctl (PR_SET_PTRACER, pid, 0, 0, 0);
2378 dup2 (STDERR_FILENO, STDOUT_FILENO);
2380 mono_gdb_render_native_backtraces (crashed_pid);
2384 mono_runtime_printf_err ("\nDebug info from gdb:\n");
2385 waitpid (pid, &status, 0);
2389 #elif defined (ENABLE_EXTENSION_MODULE)
2390 mono_extension_handle_native_sigsegv (ctx, info);
2394 * A SIGSEGV indicates something went very wrong so we can no longer depend
2395 * on anything working. So try to print out lots of diagnostics, starting
2396 * with ones which have a greater chance of working.
2398 mono_runtime_printf_err (
2400 "=================================================================\n"
2401 "Got a %s while executing native code. This usually indicates\n"
2402 "a fatal error in the mono runtime or one of the native libraries \n"
2403 "used by your application.\n"
2404 "=================================================================\n",
2408 #ifdef MONO_ARCH_USE_SIGACTION
2410 /* Remove our SIGABRT handler */
2411 sa.sa_handler = SIG_DFL;
2412 sigemptyset (&sa.sa_mask);
2415 g_assert (sigaction (SIGABRT, &sa, NULL) != -1);
2419 if (!mono_do_crash_chaining) {
2420 /*Android abort is a fluke, it doesn't abort, it triggers another segv. */
2421 #if defined (PLATFORM_ANDROID)
2432 mono_handle_native_sigsegv (int signal, void *ctx, MONO_SIG_HANDLER_INFO_TYPE *info)
2434 g_assert_not_reached ();
2437 #endif /* !MONO_CROSS_COMPILE */
2440 mono_print_thread_dump_internal (void *sigctx, MonoContext *start_ctx)
2442 MonoInternalThread *thread = mono_thread_internal_current ();
2443 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2451 GError *error = NULL;
2456 text = g_string_new (0);
2458 name = g_utf16_to_utf8 (thread->name, thread->name_len, NULL, NULL, &error);
2460 g_string_append_printf (text, "\n\"%s\"", name);
2463 else if (thread->threadpool_thread)
2464 g_string_append (text, "\n\"<threadpool thread>\"");
2466 g_string_append (text, "\n\"<unnamed thread>\"");
2469 wapi_desc = wapi_current_thread_desc ();
2470 g_string_append_printf (text, " tid=0x%p this=0x%p %s\n", (gpointer)(gsize)thread->tid, thread, wapi_desc);
2474 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2476 memcpy (&ctx, start_ctx, sizeof (MonoContext));
2478 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, mono_print_thread_dump);
2480 mono_sigctx_to_monoctx (sigctx, &ctx);
2482 mono_walk_stack_with_ctx (print_stack_frame_to_string, &ctx, MONO_UNWIND_LOOKUP_ALL, text);
2484 mono_runtime_printf ("\t<Stack traces in thread dumps not supported on this platform>");
2487 mono_runtime_printf ("%s", text->str);
2489 #if PLATFORM_WIN32 && TARGET_WIN32 && _DEBUG
2490 OutputDebugStringA(text->str);
2493 g_string_free (text, TRUE);
2494 mono_runtime_stdout_fflush ();
2498 * mono_print_thread_dump:
2500 * Print information about the current thread to stdout.
2501 * SIGCTX can be NULL, allowing this to be called from gdb.
2504 mono_print_thread_dump (void *sigctx)
2506 mono_print_thread_dump_internal (sigctx, NULL);
2510 mono_print_thread_dump_from_ctx (MonoContext *ctx)
2512 mono_print_thread_dump_internal (NULL, ctx);
2516 * mono_resume_unwind:
2518 * This is called by a trampoline from LLVM compiled finally clauses to continue
2522 mono_resume_unwind (MonoContext *ctx)
2524 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
2525 MonoContext new_ctx;
2527 MONO_CONTEXT_SET_IP (ctx, MONO_CONTEXT_GET_IP (&jit_tls->resume_state.ctx));
2528 MONO_CONTEXT_SET_SP (ctx, MONO_CONTEXT_GET_SP (&jit_tls->resume_state.ctx));
2531 mono_handle_exception_internal (&new_ctx, (MonoObject *)jit_tls->resume_state.ex_obj, TRUE, NULL);
2533 mono_restore_context (&new_ctx);
2536 #ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
2541 MonoJitExceptionInfo *ei;
2542 } FindHandlerBlockData;
2545 find_last_handler_block (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
2549 FindHandlerBlockData *pdata = (FindHandlerBlockData *)data;
2550 MonoJitInfo *ji = frame->ji;
2555 ip = MONO_CONTEXT_GET_IP (ctx);
2557 for (i = 0; i < ji->num_clauses; ++i) {
2558 MonoJitExceptionInfo *ei = ji->clauses + i;
2559 if (ei->flags != MONO_EXCEPTION_CLAUSE_FINALLY)
2561 /*If ip points to the first instruction it means the handler block didn't start
2562 so we can leave its execution to the EH machinery*/
2563 if (ei->handler_start < ip && ip < ei->data.handler_end) {
2575 install_handler_block_guard (MonoJitInfo *ji, MonoContext *ctx)
2578 MonoJitExceptionInfo *clause = NULL;
2581 ip = MONO_CONTEXT_GET_IP (ctx);
2583 for (i = 0; i < ji->num_clauses; ++i) {
2584 clause = &ji->clauses [i];
2585 if (clause->flags != MONO_EXCEPTION_CLAUSE_FINALLY)
2587 if (clause->handler_start < ip && clause->data.handler_end > ip)
2591 /*no matching finally */
2592 if (i == ji->num_clauses)
2595 /*If we stopped on the instruction right before the try, we haven't actually started executing it*/
2596 if (ip == clause->handler_start)
2599 return mono_arch_install_handler_block_guard (ji, clause, ctx, mono_create_handler_block_trampoline ());
2603 * Finds the bottom handler block running and install a block guard if needed.
2604 * FIXME add full-aot support.
2607 mono_install_handler_block_guard (MonoThreadUnwindState *ctx)
2609 FindHandlerBlockData data = { 0 };
2610 MonoJitTlsData *jit_tls = (MonoJitTlsData *)ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS];
2617 /* Guard against a null MonoJitTlsData. This can happens if the thread receives the
2618 * interrupt signal before the JIT has time to initialize its TLS data for the given thread.
2620 if (!jit_tls || jit_tls->handler_block_return_address)
2623 /* Do an async safe stack walk */
2624 mono_thread_info_set_is_async_context (TRUE);
2625 mono_walk_stack_with_state (find_last_handler_block, ctx, MONO_UNWIND_NONE, &data);
2626 mono_thread_info_set_is_async_context (FALSE);
2631 memcpy (&jit_tls->handler_block_context, &data.ctx, sizeof (MonoContext));
2633 resume_ip = install_handler_block_guard (data.ji, &data.ctx);
2634 if (resume_ip == NULL)
2637 jit_tls->handler_block_return_address = resume_ip;
2638 jit_tls->handler_block = data.ei;
2645 mono_install_handler_block_guard (MonoThreadUnwindState *ctx)
2653 mono_set_cast_details (MonoClass *from, MonoClass *to)
2655 MonoJitTlsData *jit_tls = NULL;
2657 if (mini_get_debug_options ()->better_cast_details) {
2658 jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
2659 jit_tls->class_cast_from = from;
2660 jit_tls->class_cast_to = to;
2665 /*returns false if the thread is not attached*/
2667 mono_thread_state_init_from_sigctx (MonoThreadUnwindState *ctx, void *sigctx)
2669 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
2670 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
2678 mono_sigctx_to_monoctx (sigctx, &ctx->ctx);
2680 ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
2681 ctx->unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
2682 ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = thread->jit_data;
2685 mono_thread_state_init (ctx);
2688 if (!ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] || !ctx->unwind_data [MONO_UNWIND_DATA_LMF])
2694 g_error ("Implement mono_arch_sigctx_to_monoctx for the current target");
2700 mono_thread_state_init (MonoThreadUnwindState *ctx)
2702 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
2704 #if defined(MONO_CROSS_COMPILE)
2705 ctx->valid = FALSE; //A cross compiler doesn't need to suspend.
2706 #elif MONO_ARCH_HAS_MONO_CONTEXT
2707 MONO_CONTEXT_GET_CURRENT (ctx->ctx);
2709 g_error ("Use a null sigctx requires a working mono-context");
2712 ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
2713 ctx->unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
2714 ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = thread ? thread->jit_data : NULL;
2720 mono_thread_state_init_from_monoctx (MonoThreadUnwindState *ctx, MonoContext *mctx)
2722 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
2729 ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
2730 ctx->unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
2731 ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = thread->jit_data;
2736 /*returns false if the thread is not attached*/
2738 mono_thread_state_init_from_current (MonoThreadUnwindState *ctx)
2740 MonoThreadInfo *thread = mono_thread_info_current_unchecked ();
2741 MONO_ARCH_CONTEXT_DEF
2743 mono_arch_flush_register_windows ();
2745 if (!thread || !thread->jit_data) {
2749 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
2750 MONO_INIT_CONTEXT_FROM_CURRENT (&ctx->ctx);
2752 MONO_INIT_CONTEXT_FROM_FUNC (&ctx->ctx, mono_thread_state_init_from_current);
2755 ctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = mono_domain_get ();
2756 ctx->unwind_data [MONO_UNWIND_DATA_LMF] = mono_get_lmf ();
2757 ctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = thread->jit_data;
2763 mono_raise_exception_with_ctx (MonoException *exc, MonoContext *ctx)
2765 mono_handle_exception (ctx, (MonoObject *)exc);
2766 mono_restore_context (ctx);
2769 /*FIXME Move all monoctx -> sigctx conversion to signal handlers once all archs support utils/mono-context */
2771 mono_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
2773 #ifdef MONO_ARCH_HAVE_SETUP_ASYNC_CALLBACK
2774 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
2775 jit_tls->ex_ctx = *ctx;
2777 mono_arch_setup_async_callback (ctx, async_cb, user_data);
2779 g_error ("This target doesn't support mono_arch_setup_async_callback");
2784 mono_install_unhandled_exception_hook (MonoUnhandledExceptionFunc func, gpointer user_data)
2786 unhandled_exception_hook = func;
2787 unhandled_exception_hook_data = user_data;
2791 mono_invoke_unhandled_exception_hook (MonoObject *exc)
2793 if (unhandled_exception_hook) {
2794 unhandled_exception_hook (exc, unhandled_exception_hook_data);
2796 MonoObject *other = NULL;
2797 MonoString *str = mono_object_to_string (exc, &other);
2801 msg = mono_string_to_utf8 (str);
2803 char *original_backtrace = mono_exception_get_managed_backtrace ((MonoException*)exc);
2804 char *nested_backtrace = mono_exception_get_managed_backtrace ((MonoException*)other);
2806 msg = g_strdup_printf ("Nested exception detected.\nOriginal Exception: %s\nNested exception:%s\n",
2807 original_backtrace, nested_backtrace);
2809 g_free (original_backtrace);
2810 g_free (nested_backtrace);
2812 msg = g_strdup ("Nested exception trying to figure out what went wrong");
2814 mono_runtime_printf_err ("[ERROR] FATAL UNHANDLED EXCEPTION: %s", msg);
2816 #if defined(HOST_IOS)
2817 g_assertion_message ("Terminating runtime due to unhandled exception");
2819 exit (mono_environment_exitcode_get ());
2823 g_assert_not_reached ();
2827 * mono_restore_context:
2829 * Call the architecture specific restore context function.
2832 mono_restore_context (MonoContext *ctx)
2834 static void (*restore_context) (MonoContext *);
2836 if (!restore_context)
2837 restore_context = (void (*)(MonoContext *))mono_get_restore_context ();
2838 restore_context (ctx);
2839 g_assert_not_reached ();
2843 * mono_jinfo_get_unwind_info:
2845 * Return the unwind info for JI.
2848 mono_jinfo_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
2851 return mono_aot_get_unwind_info (ji, unwind_info_len);
2853 return mono_get_cached_unwind_info (ji->unwind_info, unwind_info_len);
2857 mono_jinfo_get_epilog_size (MonoJitInfo *ji)
2859 MonoArchEHJitInfo *info;
2861 info = mono_jit_info_get_arch_eh_info (ji);
2864 return info->epilog_size;
2868 * LLVM/Bitcode exception handling.
2872 throw_exception (MonoObject *ex, gboolean rethrow)
2874 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
2875 MonoException *mono_ex;
2877 if (!mono_object_isinst (ex, mono_defaults.exception_class))
2878 mono_ex = mono_get_exception_runtime_wrapped (ex);
2880 mono_ex = (MonoException*)ex;
2883 jit_tls->thrown_exc = mono_gchandle_new ((MonoObject*)mono_ex, FALSE);
2886 #ifdef MONO_ARCH_HAVE_UNWIND_BACKTRACE
2887 GList *l, *ips = NULL;
2890 _Unwind_Backtrace (build_stack_trace, &ips);
2891 /* The list contains ip-gshared info pairs */
2893 ips = g_list_reverse (ips);
2894 for (l = ips; l; l = l->next) {
2895 trace = g_list_append (trace, l->data);
2896 trace = g_list_append (trace, NULL);
2898 MONO_OBJECT_SETREF (mono_ex, trace_ips, mono_glist_to_array (trace, mono_defaults.int_class));
2900 g_list_free (trace);
2904 mono_llvm_cpp_throw_exception ();
2908 mono_llvm_throw_exception (MonoObject *ex)
2910 throw_exception (ex, FALSE);
2914 mono_llvm_rethrow_exception (MonoObject *ex)
2916 throw_exception (ex, TRUE);
2920 mono_llvm_raise_exception (MonoException *e)
2922 mono_llvm_throw_exception ((MonoObject*)e);
2926 mono_llvm_throw_corlib_exception (guint32 ex_token_index)
2928 guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
2931 ex = mono_exception_from_token (mono_defaults.exception_class->image, ex_token);
2933 mono_llvm_throw_exception ((MonoObject*)ex);
2937 * mono_llvm_resume_exception:
2939 * Resume exception propagation.
2942 mono_llvm_resume_exception (void)
2944 mono_llvm_cpp_throw_exception ();
2948 * mono_llvm_load_exception:
2950 * Return the currently thrown exception.
2953 mono_llvm_load_exception (void)
2955 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
2957 MonoException *mono_ex = (MonoException*)mono_gchandle_get_target (jit_tls->thrown_exc);
2959 if (mono_ex->trace_ips) {
2960 GList *trace_ips = NULL;
2961 gpointer ip = __builtin_return_address (0);
2963 size_t upper = mono_array_length (mono_ex->trace_ips);
2965 for (int i = 0; i < upper; i+= 2) {
2966 gpointer curr_ip = mono_array_get (mono_ex->trace_ips, gpointer, i);
2967 gpointer curr_info = mono_array_get (mono_ex->trace_ips, gpointer, i + 1);
2968 trace_ips = g_list_append (trace_ips, curr_ip);
2969 trace_ips = g_list_append (trace_ips, curr_info);
2975 // FIXME: Does this work correctly for rethrows?
2976 // We may be discarding useful information
2977 // when this gets GC'ed
2978 MONO_OBJECT_SETREF (mono_ex, trace_ips, mono_glist_to_array (trace_ips, mono_defaults.int_class));
2979 g_list_free (trace_ips);
2982 //MONO_OBJECT_SETREF (mono_ex, stack_trace, ves_icall_System_Exception_get_trace (mono_ex));
2984 MONO_OBJECT_SETREF (mono_ex, trace_ips, mono_array_new (mono_domain_get (), mono_defaults.int_class, 0));
2985 MONO_OBJECT_SETREF (mono_ex, stack_trace, mono_array_new (mono_domain_get (), mono_defaults.stack_frame_class, 0));
2988 return &mono_ex->object;
2992 * mono_llvm_clear_exception:
2994 * Mark the currently thrown exception as handled.
2997 mono_llvm_clear_exception (void)
2999 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
3000 mono_gchandle_free (jit_tls->thrown_exc);
3001 jit_tls->thrown_exc = 0;
3003 mono_memory_barrier ();
3007 * mono_llvm_match_exception:
3009 * Return the innermost clause containing REGION_START-REGION_END which can handle
3010 * the current exception.
3013 mono_llvm_match_exception (MonoJitInfo *jinfo, guint32 region_start, guint32 region_end, gpointer rgctx, MonoObject *this_obj)
3015 MonoJitTlsData *jit_tls = mono_get_jit_tls ();
3019 g_assert (jit_tls->thrown_exc);
3020 exc = mono_gchandle_get_target (jit_tls->thrown_exc);
3021 for (int i = 0; i < jinfo->num_clauses; i++) {
3022 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
3023 MonoClass *catch_class;
3025 if (! (ei->try_offset == region_start && ei->try_offset + ei->try_len == region_end) )
3028 catch_class = ei->data.catch_class;
3029 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) {
3030 MonoGenericContext context;
3031 MonoType *inflated_type;
3033 g_assert (rgctx || this_obj);
3034 context = get_generic_context_from_stack_frame (jinfo, rgctx ? rgctx : this_obj->vtable);
3035 inflated_type = mono_class_inflate_generic_type (&catch_class->byval_arg, &context);
3036 catch_class = mono_class_from_mono_type (inflated_type);
3037 mono_metadata_free_type (inflated_type);
3040 // FIXME: Handle edge cases handled in get_exception_catch_class
3041 if (ei->flags == MONO_EXCEPTION_CLAUSE_NONE && mono_object_isinst (exc, catch_class)) {
3042 index = ei->clause_index;
3044 } else if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3045 g_assert_not_reached ();
3054 mono_debug_personality (int a, _Unwind_Action b,
3055 uint64_t c, struct _Unwind_Exception *d, struct _Unwind_Context *e)
3057 g_assert_not_reached ();
3061 mono_debug_personality (void);
3064 mono_debug_personality (void)
3066 g_assert_not_reached ();