Print a normal stack trace on a hard stack overflow.
[mono.git] / mono / mini / mini-exceptions.c
index 934f9ab53836f5b5971265bef9ce0f32fce0aa68..2f4bce7a5ccd2cc4bcde819b6b677ed1aab1f2dc 100644 (file)
@@ -64,7 +64,6 @@ static gpointer restore_stack_protection_tramp = NULL;
 
 static void try_more_restore (void);
 static void restore_stack_protection (void);
-static void mono_walk_stack_full (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoContext *start_ctx, MonoStackFrameWalk func, gboolean use_new_ctx, gpointer user_data);
 
 void
 mono_exceptions_init (void)
@@ -171,7 +170,6 @@ is_address_protected (MonoJitInfo *ji, MonoJitExceptionInfo *ei, gpointer ip)
        guint32 offset;
        guint16 clause;
 
-       /*FIXME check if under s390 it should be ei->try_start >= ip*/
        if (ei->try_start > ip || ip >= ei->try_end)
                return FALSE;
 
@@ -191,16 +189,13 @@ is_address_protected (MonoJitInfo *ji, MonoJitExceptionInfo *ei, gpointer ip)
        return TRUE;
 }
 
-#ifdef MONO_ARCH_HAVE_FIND_JIT_INFO_EXT
-
 /*
- * find_jit_info_no_ext:
+ * find_jit_info:
  *
- * If the target has the find_jit_info_ext version of this function, define the old
- * version here which translates between the old and new APIs.
+ * Translate between the mono_arch_find_jit_info function and the old API.
  */
 static MonoJitInfo *
-find_jit_info_no_ext (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx, 
+find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx, 
                           MonoContext *new_ctx, MonoLMF **lmf, gboolean *managed)
 {
        StackFrameInfo frame;
@@ -217,7 +212,7 @@ find_jit_info_no_ext (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *
        if (managed)
                *managed = FALSE;
 
-       err = mono_arch_find_jit_info_ext (domain, jit_tls, ji, ctx, new_ctx, lmf, &frame);
+       err = mono_arch_find_jit_info (domain, jit_tls, ji, ctx, new_ctx, lmf, NULL, &frame);
        if (!err)
                return (gpointer)-1;
 
@@ -226,7 +221,7 @@ find_jit_info_no_ext (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *
        case FRAME_TYPE_MANAGED:
                if (managed)
                        *managed = TRUE;
-               return ji;
+               return frame.ji;
        case FRAME_TYPE_MANAGED_TO_NATIVE:
                if (frame.ji)
                        return frame.ji;
@@ -242,7 +237,7 @@ find_jit_info_no_ext (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *
                 * The normal exception handling code can't handle this frame, so just
                 * skip it.
                 */
-               ji = find_jit_info_no_ext (domain, jit_tls, res, NULL, new_ctx, &tmp_ctx, lmf, managed);
+               ji = find_jit_info (domain, jit_tls, res, NULL, new_ctx, &tmp_ctx, lmf, managed);
                memcpy (new_ctx, &tmp_ctx, sizeof (MonoContext));
                return ji;
        }
@@ -252,8 +247,6 @@ find_jit_info_no_ext (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *
        }
 }
 
-#endif
-
 /* mono_find_jit_info:
  *
  * This function is used to gather information from @ctx. It return the 
@@ -281,11 +274,7 @@ mono_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *re
        if (managed)
                *managed = FALSE;
 
-#ifdef MONO_ARCH_HAVE_FIND_JIT_INFO_EXT
-       ji = find_jit_info_no_ext (domain, jit_tls, res, prev_ji, ctx, new_ctx, lmf, &managed2);
-#else
-       ji = mono_arch_find_jit_info (domain, jit_tls, res, prev_ji, ctx, new_ctx, lmf, &managed2);
-#endif
+       ji = find_jit_info (domain, jit_tls, res, prev_ji, ctx, new_ctx, lmf, &managed2);
 
        if (ji == (gpointer)-1)
                return ji;
@@ -310,7 +299,7 @@ mono_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *re
                        *native_offset = offset;
 
                if (managed)
-                       if (!ji->method->wrapper_type)
+                       if (!ji->method->wrapper_type || ji->method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
                                *managed = TRUE;
 
                if (trace)
@@ -326,18 +315,25 @@ mono_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *re
        return ji;
 }
 
-#ifdef MONO_ARCH_HAVE_FIND_JIT_INFO_EXT
-
 /*
  * mono_find_jit_info_ext:
  *
  *   A version of mono_find_jit_info which returns all data in the StackFrameInfo
  * structure.
+ * A note about frames of type FRAME_TYPE_MANAGED_TO_NATIVE:
+ * - These frames are used to mark managed-to-native transitions, so CTX will refer to native
+ * code, and new_ctx will refer to the last managed frame. The caller should unwind once more
+ * to obtain the last managed frame.
+ * If SAVE_LOCATIONS is not NULL, it should point to an array of size MONO_MAX_IREGS.
+ * On return, it will be filled with the locations where callee saved registers are saved
+ * by the current frame. This is returned outside of StackFrameInfo because it can be
+ * quite large on some platforms.
  */
 gboolean
 mono_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls, 
                                                MonoJitInfo *prev_ji, MonoContext *ctx,
                                                MonoContext *new_ctx, char **trace, MonoLMF **lmf,
+                                               mgreg_t **save_locations,
                                                StackFrameInfo *frame)
 {
        gboolean err;
@@ -357,15 +353,35 @@ mono_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
        if (!target_domain)
                target_domain = domain;
 
-       err = mono_arch_find_jit_info_ext (target_domain, jit_tls, ji, ctx, new_ctx, lmf, frame);
+       if (save_locations)
+               memset (save_locations, 0, MONO_MAX_IREGS * sizeof (mgreg_t*));
+
+       err = mono_arch_find_jit_info (target_domain, jit_tls, ji, ctx, new_ctx, lmf, save_locations, frame);
        if (!err)
                return FALSE;
 
+       if (frame->type == FRAME_TYPE_MANAGED) {
+               if (!ji->method->wrapper_type || ji->method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
+                       frame->managed = TRUE;
+       }
+
+       if (frame->type == FRAME_TYPE_MANAGED_TO_NATIVE) {
+               /*
+                * This type of frame is just a marker, the caller should unwind once more to get the
+                * last managed frame.
+                */
+               frame->ji = NULL;
+               frame->method = NULL;
+       }
+
        frame->native_offset = -1;
        frame->domain = target_domain;
 
        ji = frame->ji;
 
+       if (frame->type == FRAME_TYPE_MANAGED)
+               frame->method = ji->method;
+
        if (ji && (frame->managed || ji->method->wrapper_type)) {
                const char *real_ip, *start;
 
@@ -394,8 +410,6 @@ mono_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls,
        return TRUE;
 }
 
-#endif /* MONO_ARCH_HAVE_FIND_JIT_INFO_EXT */
-
 static gpointer
 get_generic_info_from_stack_frame (MonoJitInfo *ji, MonoContext *ctx)
 {
@@ -455,7 +469,7 @@ get_generic_context_from_stack_frame (MonoJitInfo *ji, gpointer generic_info)
                method_container_class = ji->method->klass;
 
        /* class might refer to a subclass of ji->method's class */
-       while (class->generic_class && class->generic_class->container_class != method_container_class) {
+       while (!(class == ji->method->klass || (class->generic_class && class->generic_class->container_class == method_container_class))) {
                class = class->parent;
                g_assert (class);
        }
@@ -607,127 +621,95 @@ ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info
        return res;
 }
 
-/**
- * mono_walk_stack:
- * @domain: starting appdomain
- * @jit_tls: JIT data for the thread
- * @start_ctx: starting state of the stack frame
- * @func: callback to call for each stack frame
- * @user_data: data passed to the callback
- *
- * This function walks the stack of a thread, starting from the state
- * represented by jit_tls and start_ctx. For each frame the callback
- * function is called with the relevant info. The walk ends when no more
- * managed stack frames are found or when the callback returns a TRUE value.
- * Note that the function can be used to walk the stack of a thread 
- * different from the current.
- */
-void
-mono_walk_stack (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoContext *start_ctx, MonoStackFrameWalk func, gpointer user_data)
-{
-       mono_walk_stack_full (domain, jit_tls, start_ctx, func, TRUE, user_data);
-}
+typedef struct {
+       MonoStackWalk func;
+       gpointer user_data;
+} StackWalkUserData;
 
-static void
-mono_walk_stack_full (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoContext *start_ctx, MonoStackFrameWalk func, gboolean use_new_ctx, gpointer user_data)
+static gboolean
+stack_walk_adapter (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
 {
-       MonoLMF *lmf = mono_get_lmf ();
-       MonoJitInfo *ji, rji;
-       gint native_offset;
-       gboolean managed;
-       MonoContext ctx, new_ctx;
-
-       ctx = *start_ctx;
-
-       while (MONO_CONTEXT_GET_SP (&ctx) < jit_tls->end_of_stack) {
-               /* 
-                * FIXME: mono_find_jit_info () will need to be able to return a different
-                * MonoDomain when apddomain transitions are found on the stack.
-                */
-               ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
-               if (!ji || ji == (gpointer)-1)
-                       return;
+       StackWalkUserData *d = data;
 
-               if (func (domain, use_new_ctx ? &new_ctx : &ctx, ji, user_data))
-                       return;
-
-               ctx = new_ctx;
+       switch (frame->type) {
+       case FRAME_TYPE_DEBUGGER_INVOKE:
+       case FRAME_TYPE_MANAGED_TO_NATIVE:
+               return FALSE;
+       case FRAME_TYPE_MANAGED:
+               g_assert (frame->ji);
+               return d->func (frame->ji->method, frame->native_offset, frame->il_offset, frame->managed, d->user_data);
+               break;
+       default:
+               g_assert_not_reached ();
+               return FALSE;
        }
 }
 
 void
-mono_jit_walk_stack_from_ctx (MonoStackWalk func, MonoContext *start_ctx, gboolean do_il_offset, gpointer user_data)
+mono_jit_walk_stack_from_ctx (MonoStackWalk func, MonoContext *start_ctx, MonoUnwindOptions unwind_options, gpointer user_data)
 {
-       MonoDomain *domain = mono_domain_get ();
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
-       MonoLMF *lmf = mono_get_lmf ();
-       MonoJitInfo *ji, rji;
-       gint native_offset, il_offset;
-       gboolean managed;
-       MonoContext ctx, new_ctx;
+       StackWalkUserData d;
 
-       MONO_ARCH_CONTEXT_DEF
+       d.func = func;
+       d.user_data = user_data;
 
-       mono_arch_flush_register_windows ();
-
-       if (start_ctx) {
-               memcpy (&ctx, start_ctx, sizeof (MonoContext));
-       } else {
-#ifdef MONO_INIT_CONTEXT_FROM_CURRENT
-       MONO_INIT_CONTEXT_FROM_CURRENT (&ctx);
-#else
-    MONO_INIT_CONTEXT_FROM_FUNC (&ctx, mono_jit_walk_stack_from_ctx);
-#endif
-       }
-
-       while (MONO_CONTEXT_GET_SP (&ctx) < jit_tls->end_of_stack) {
-               ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
-               g_assert (ji);
-
-               if (ji == (gpointer)-1)
-                       return;
-
-               if (do_il_offset) {
-                       MonoDebugSourceLocation *source;
-
-                       source = mono_debug_lookup_source_location (ji->method, native_offset, domain);
-                       il_offset = source ? source->il_offset : -1;
-                       mono_debug_free_source_location (source);
-               } else
-                       il_offset = -1;
-
-               if (func (ji->method, native_offset, il_offset, managed, user_data))
-                       return;
-               
-               ctx = new_ctx;
-       }
+       mono_walk_stack (stack_walk_adapter, mono_domain_get (), start_ctx, unwind_options, mono_thread_internal_current (), mono_get_lmf (), &d);
 }
 
 void
 mono_jit_walk_stack (MonoStackWalk func, gboolean do_il_offset, gpointer user_data)
 {
-       mono_jit_walk_stack_from_ctx (func, NULL, do_il_offset, user_data);
+       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoUnwindOptions unwind_options = do_il_offset ? MONO_UNWIND_LOOKUP_ALL : MONO_UNWIND_DEFAULT;
+       if (jit_tls && jit_tls->orig_ex_ctx_set)
+               mono_jit_walk_stack_from_ctx (func, &jit_tls->orig_ex_ctx, unwind_options, user_data);
+       else
+               mono_jit_walk_stack_from_ctx (func, NULL, unwind_options, user_data);
 }
 
+/**
+ * mono_walk_stack:
+ * @func: callback to call for each stack frame
+ * @domain: starting appdomain, can be NULL to use the current domain
+ * @unwind_options: what extra information the unwinder should gather
+ * @start_ctx: starting state of the stack walk, can be NULL.
+ * @thread: the thread whose stack to walk, can be NULL to use the current thread
+ * @lmf: the LMF of @thread, can be NULL to use the LMF of the current thread
+ * @user_data: data passed to the callback
+ *
+ * This function walks the stack of a thread, starting from the state
+ * represented by start_ctx. For each frame the callback
+ * function is called with the relevant info. The walk ends when no more
+ * managed stack frames are found or when the callback returns a TRUE value.
+ */
 void
-mono_jit_walk_stack_from_ctx_in_thread (MonoJitStackWalk func, MonoDomain *domain, MonoContext *start_ctx, gboolean do_il_offset, MonoInternalThread *thread, MonoLMF *lmf, gpointer user_data)
+mono_walk_stack (MonoJitStackWalk func, MonoDomain *domain, MonoContext *start_ctx, MonoUnwindOptions unwind_options, MonoInternalThread *thread, MonoLMF *lmf, gpointer user_data)
 {
-       MonoJitTlsData *jit_tls = thread->jit_data;
+       MonoJitTlsData *jit_tls;
        gint il_offset;
        MonoContext ctx, new_ctx;
        StackFrameInfo frame;
-#ifndef MONO_ARCH_HAVE_FIND_JIT_INFO_EXT
-       gint native_offset;
-       gboolean managed;
-       MonoJitInfo *ji, rji;
-#else
        gboolean res;
-#endif
        
        MONO_ARCH_CONTEXT_DEF
 
        mono_arch_flush_register_windows ();
 
+       if (!thread) {
+               thread = mono_thread_internal_current ();
+               lmf = mono_get_lmf ();
+       }
+
+       /* A NULL thread->jit_data can happen in a small window during thread startup: the thread
+        * allocation happens, we do a stack walk (for example with
+        * --profile=log:nocalls and xsp) but the jit is not fully setup for the thread
+        *  yet. Of course there are no stack frames, so just returning is ok.
+        *  A NULL thread can happen during domain unload with the same test.
+        */
+       if (!thread || !thread->jit_data)
+               return;
+       jit_tls = thread->jit_data;
+
        if (start_ctx) {
                memcpy (&ctx, start_ctx, sizeof (MonoContext));
        } else {
@@ -741,23 +723,11 @@ mono_jit_walk_stack_from_ctx_in_thread (MonoJitStackWalk func, MonoDomain *domai
 
        while (MONO_CONTEXT_GET_SP (&ctx) < jit_tls->end_of_stack) {
                frame.lmf = lmf;
-#ifdef MONO_ARCH_HAVE_FIND_JIT_INFO_EXT
-               res = mono_find_jit_info_ext (domain, jit_tls, NULL, &ctx, &new_ctx, NULL, &lmf, &frame);
+               res = mono_find_jit_info_ext (domain, jit_tls, NULL, &ctx, &new_ctx, NULL, &lmf, NULL, &frame);
                if (!res)
                        return;
-#else
-               ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
-               g_assert (ji);
-               frame.type = FRAME_TYPE_MANAGED;
-               frame.ji = ji;
-               frame.managed = managed;
-               frame.native_offset = native_offset;
-
-               if (ji == (gpointer)-1)
-                       return;
-#endif
 
-               if (do_il_offset && frame.ji) {
+               if ((unwind_options & MONO_UNWIND_LOOKUP_IL_OFFSET) && frame.ji) {
                        MonoDebugSourceLocation *source;
 
                        source = mono_debug_lookup_source_location (frame.ji->method, frame.native_offset, domain);
@@ -768,6 +738,12 @@ mono_jit_walk_stack_from_ctx_in_thread (MonoJitStackWalk func, MonoDomain *domai
 
                frame.il_offset = il_offset;
 
+               if ((unwind_options & MONO_UNWIND_LOOKUP_ACTUAL_METHOD) && frame.ji) {
+                       frame.actual_method = get_method_from_stack_frame (frame.ji, get_generic_info_from_stack_frame (frame.ji, &ctx));
+               } else {
+                       frame.actual_method = frame.method;
+               }
+
                if (func (&frame, &ctx, user_data))
                        return;
                
@@ -775,7 +751,6 @@ mono_jit_walk_stack_from_ctx_in_thread (MonoJitStackWalk func, MonoDomain *domai
        }
 }
 
-
 MonoBoolean
 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info, 
                          MonoReflectionMethod **method, 
@@ -785,10 +760,12 @@ ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
        MonoDomain *domain = mono_domain_get ();
        MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
        MonoLMF *lmf = mono_get_lmf ();
-       MonoJitInfo *ji, rji;
-       MonoContext ctx, new_ctx, ji_ctx;
+       MonoJitInfo *ji = NULL;
+       MonoContext ctx, new_ctx;
        MonoDebugSourceLocation *location;
-       MonoMethod *last_method = NULL, *actual_method;
+       MonoMethod *actual_method;
+       StackFrameInfo frame;
+       gboolean res;
 
        MONO_ARCH_CONTEXT_DEF;
 
@@ -800,19 +777,18 @@ ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
        MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_get_frame_info);
 #endif
 
+       new_ctx = ctx;
        do {
-               ji_ctx = ctx;
-               ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, (int*) native_offset, NULL);
                ctx = new_ctx;
+               res = mono_find_jit_info_ext (domain, jit_tls, NULL, &ctx, &new_ctx, NULL, &lmf, NULL, &frame);
+               if (!res)
+                       return FALSE;
 
-               if (ji && ji != (gpointer)-1 &&
-                               MONO_CONTEXT_GET_IP (&ctx) >= ji->code_start &&
-                               (guint8*)MONO_CONTEXT_GET_IP (&ctx) < (guint8*)ji->code_start + ji->code_size) {
-                       ji_ctx = ctx;
-               }
+               if (frame.type == FRAME_TYPE_MANAGED_TO_NATIVE || frame.type == FRAME_TYPE_DEBUGGER_INVOKE)
+                       continue;
 
-               if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_SP (&ctx) >= jit_tls->end_of_stack)
-                       return FALSE;
+               ji = frame.ji;
+               *native_offset = frame.native_offset;
 
                /* skip all wrappers ??*/
                if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
@@ -823,22 +799,10 @@ ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
                        ji->method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED)
                        continue;
 
-               if (ji->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE && ji->method == last_method) {
-                       /*
-                        * FIXME: Native-to-managed wrappers sometimes show up twice.
-                        * Probably the whole mono_find_jit_info () stuff needs to be fixed so this 
-                        * isn't needed.
-                        */
-                       continue;
-               }
-
-               last_method = ji->method;
-
                skip--;
-
        } while (skip >= 0);
 
-       actual_method = get_method_from_stack_frame (ji, get_generic_info_from_stack_frame (ji, &ji_ctx));
+       actual_method = get_method_from_stack_frame (ji, get_generic_info_from_stack_frame (ji, &ctx));
 
        mono_gc_wbarrier_generic_store (method, (MonoObject*) mono_method_get_object (domain, actual_method, NULL));
 
@@ -870,9 +834,13 @@ typedef struct {
 } MonoFrameSecurityInfo;
 
 static gboolean
-callback_get_first_frame_security_info (MonoDomain *domain, MonoContext *ctx, MonoJitInfo *ji, gpointer data)
+callback_get_first_frame_security_info (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
 {
        MonoFrameSecurityInfo *si = (MonoFrameSecurityInfo*) data;
+       MonoJitInfo *ji = frame->ji;
+
+       if (!ji)
+               return FALSE;
 
        /* FIXME: skip all wrappers ?? probably not - case by case testing is required */
        if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
@@ -888,7 +856,7 @@ callback_get_first_frame_security_info (MonoDomain *domain, MonoContext *ctx, Mo
                return FALSE;
        }
 
-       si->frame = mono_declsec_create_frame (domain, ji);
+       si->frame = mono_declsec_create_frame (frame->domain, ji);
 
        /* Stop - we only want the first frame (e.g. LinkDemand and InheritanceDemand) */
        return TRUE;
@@ -907,25 +875,12 @@ MonoSecurityFrame*
 ves_icall_System_Security_SecurityFrame_GetSecurityFrame (gint32 skip)
 {
        MonoDomain *domain = mono_domain_get ();
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
        MonoFrameSecurityInfo si;
-       MonoContext ctx;
-
-       MONO_ARCH_CONTEXT_DEF
-
-#ifdef MONO_INIT_CONTEXT_FROM_CURRENT
-       MONO_INIT_CONTEXT_FROM_CURRENT (&ctx);
-#else
-       MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_System_Security_SecurityFrame_GetSecurityFrame);
-#endif
-
-#if    defined(__ia64__) || defined(__s390__) || defined(__s390x__)
-       skip--;
-#endif
 
        si.skips = skip;
        si.frame = NULL;
-       mono_walk_stack (domain, jit_tls, &ctx, callback_get_first_frame_security_info, (gpointer)&si);
+
+       mono_walk_stack (callback_get_first_frame_security_info, domain, NULL, MONO_UNWIND_DEFAULT, NULL, NULL, &si);
 
        return (si.skips == 0) ? si.frame : NULL;
 }
@@ -954,9 +909,13 @@ grow_array (MonoSecurityStack *stack)
 }
 
 static gboolean
-callback_get_stack_frames_security_info (MonoDomain *domain, MonoContext *ctx, MonoJitInfo *ji, gpointer data)
+callback_get_stack_frames_security_info (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
 {
        MonoSecurityStack *ss = (MonoSecurityStack*) data;
+       MonoJitInfo *ji = frame->ji;
+
+       if (!ji)
+               return FALSE;
 
        /* FIXME: skip all wrappers ?? probably not - case by case testing is required */
        if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
@@ -975,7 +934,7 @@ callback_get_stack_frames_security_info (MonoDomain *domain, MonoContext *ctx, M
        if (ss->count == ss->maximum)
                grow_array (ss);
 
-       mono_array_setref (ss->stack, ss->count++, mono_declsec_create_frame (domain, ji));
+       mono_array_setref (ss->stack, ss->count++, mono_declsec_create_frame (frame->domain, ji));
 
        /* continue down the stack */
        return FALSE;
@@ -1013,17 +972,7 @@ MonoArray*
 ves_icall_System_Security_SecurityFrame_GetSecurityStack (gint32 skip)
 {
        MonoDomain *domain = mono_domain_get ();
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
        MonoSecurityStack ss;
-       MonoContext ctx;
-
-       MONO_ARCH_CONTEXT_DEF
-
-#ifdef MONO_INIT_CONTEXT_FROM_CURRENT
-       MONO_INIT_CONTEXT_FROM_CURRENT (&ctx);
-#else
-       MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_System_Security_SecurityFrame_GetSecurityStack);
-#endif
 
 #if    defined(__ia64__) || defined(__s390__) || defined(__s390x__)
        skip--;
@@ -1033,7 +982,7 @@ ves_icall_System_Security_SecurityFrame_GetSecurityStack (gint32 skip)
        ss.count = 0;
        ss.maximum = MONO_CAS_INITIAL_STACK_SIZE;
        ss.stack = mono_array_new (domain, mono_defaults.runtimesecurityframe_class, ss.maximum);
-       mono_walk_stack (domain, jit_tls, &ctx, callback_get_stack_frames_security_info, (gpointer)&ss);
+       mono_walk_stack (callback_get_stack_frames_security_info, domain, NULL, MONO_UNWIND_DEFAULT, NULL, NULL, &ss);
        /* g_warning ("STACK RESULT: %d out of %d", ss.count, ss.maximum); */
        return ss.stack;
 }
@@ -1171,6 +1120,7 @@ wrap_non_exception_throws (MonoMethod *m)
                        /* The value is a BOOLEAN */
                        val = *p;
                }
+               mono_custom_attrs_free (attrs);
        }
 
        ass->wrap_non_exception_throws = val;
@@ -1180,32 +1130,241 @@ wrap_non_exception_throws (MonoMethod *m)
        return val;
 }
 
+#ifndef MONO_ARCH_STACK_GROWS_UP
+#define DOES_STACK_GROWS_UP 1
+#else
+#define DOES_STACK_GROWS_UP 0
+#endif
+
+/*
+ * mono_handle_exception_internal_first_pass:
+ *
+ *   The first pass of exception handling. Unwind the stack until a catch clause which can catch
+ * OBJ is found. Run the index of the filter clause which caught the exception into
+ * OUT_FILTER_IDX. Return TRUE if the exception is caught, FALSE otherwise.
+ */
+static gboolean
+mono_handle_exception_internal_first_pass (MonoContext *ctx, gpointer obj, gpointer original_ip, gint32 *out_filter_idx, MonoJitInfo **out_ji, MonoObject *non_exception)
+{
+       MonoDomain *domain = mono_domain_get ();
+       MonoJitInfo *ji;
+       static int (*call_filter) (MonoContext *, gpointer) = NULL;
+       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoLMF *lmf = mono_get_lmf ();
+       MonoArray *initial_trace_ips = NULL;
+       GList *trace_ips = NULL;
+       MonoException *mono_ex;
+       gboolean stack_overflow = FALSE;
+       MonoContext initial_ctx;
+       int frame_count = 0;
+       gboolean has_dynamic_methods = FALSE;
+       gint32 filter_idx;
+       int i;
+       MonoObject *ex_obj;
+
+       g_assert (ctx != NULL);
+
+       if (obj == domain->stack_overflow_ex)
+               stack_overflow = TRUE;
+
+       mono_ex = (MonoException*)obj;
+       initial_trace_ips = mono_ex->trace_ips;
+
+       if (mono_object_isinst (obj, mono_defaults.exception_class)) {
+               mono_ex = (MonoException*)obj;
+               initial_trace_ips = mono_ex->trace_ips;
+       } else {
+               mono_ex = NULL;
+       }
+
+       if (!call_filter)
+               call_filter = mono_get_call_filter ();
+
+       g_assert (jit_tls->end_of_stack);
+       g_assert (jit_tls->abort_func);
+
+       if (out_filter_idx)
+               *out_filter_idx = -1;
+       if (out_ji)
+               *out_ji = NULL;
+       filter_idx = 0;
+       initial_ctx = *ctx;
+
+       while (1) {
+               MonoContext new_ctx;
+               guint32 free_stack;
+               int clause_index_start = 0;
+               gboolean unwind_res = TRUE;
+               
+               StackFrameInfo frame;
+
+               unwind_res = mono_find_jit_info_ext (domain, jit_tls, NULL, ctx, &new_ctx, NULL, &lmf, NULL, &frame);
+               if (unwind_res) {
+                       if (frame.type == FRAME_TYPE_DEBUGGER_INVOKE || frame.type == FRAME_TYPE_MANAGED_TO_NATIVE) {
+                               *ctx = new_ctx;
+                               continue;
+                       }
+                       g_assert (frame.type == FRAME_TYPE_MANAGED);
+                       ji = frame.ji;
+               }
+
+               if (!unwind_res) {
+                       if (mono_ex && !initial_trace_ips) {
+                               trace_ips = g_list_reverse (trace_ips);
+                               MONO_OBJECT_SETREF (mono_ex, trace_ips, glist_to_array (trace_ips, mono_defaults.int_class));
+                               if (has_dynamic_methods)
+                                       /* These methods could go away anytime, so compute the stack trace now */
+                                       MONO_OBJECT_SETREF (mono_ex, stack_trace, ves_icall_System_Exception_get_trace (mono_ex));
+                       }
+                       g_list_free (trace_ips);
+                       return FALSE;
+               }
+
+               frame_count ++;
+               //printf ("M: %s %d.\n", mono_method_full_name (ji->method, TRUE), frame_count);
+
+               if (mini_get_debug_options ()->reverse_pinvoke_exceptions && ji->method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
+                       g_error ("A native frame was found while unwinding the stack after an exception.\n"
+                                        "The native frame called the managed method:\n%s\n",
+                                        mono_method_full_name (ji->method, TRUE));
+               }
+
+               if (ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
+                       /* 
+                        * Avoid overwriting the stack trace if the exception is
+                        * rethrown. Also avoid giant stack traces during a stack
+                        * overflow.
+                        */
+                       if (!initial_trace_ips && (frame_count < 1000)) {
+                               trace_ips = g_list_prepend (trace_ips, MONO_CONTEXT_GET_IP (ctx));
+                               trace_ips = g_list_prepend (trace_ips,
+                                                                                       get_generic_info_from_stack_frame (ji, ctx));
+                       }
+               }
+
+               if (ji->method->dynamic)
+                       has_dynamic_methods = TRUE;
+
+               if (stack_overflow) {
+                       if (DOES_STACK_GROWS_UP)
+                               free_stack = (guint8*)(MONO_CONTEXT_GET_SP (ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx));
+                       else
+                               free_stack = (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (ctx));
+               } else {
+                       free_stack = 0xffffff;
+               }
+                               
+               for (i = clause_index_start; i < ji->num_clauses; i++) {
+                       MonoJitExceptionInfo *ei = &ji->clauses [i];
+                       gboolean filtered = FALSE;
+
+                       /* 
+                        * During stack overflow, wait till the unwinding frees some stack
+                        * space before running handlers/finalizers.
+                        */
+                       if (free_stack <= (64 * 1024))
+                               continue;
+
+                       if (is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (ctx))) {
+                               /* catch block */
+                               MonoClass *catch_class = get_exception_catch_class (ei, ji, ctx);
+
+                               /*
+                                * Have to unwrap RuntimeWrappedExceptions if the
+                                * method's assembly doesn't have a RuntimeCompatibilityAttribute.
+                                */
+                               if (non_exception && !wrap_non_exception_throws (ji->method))
+                                       ex_obj = non_exception;
+                               else
+                                       ex_obj = obj;
+
+                               if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
+                                       mono_perfcounters->exceptions_filters++;
+                                       mono_debugger_call_exception_handler (ei->data.filter, MONO_CONTEXT_GET_SP (ctx), ex_obj);
+                                       if (mono_ex && !initial_trace_ips) {
+                                               trace_ips = g_list_reverse (trace_ips);
+                                               MONO_OBJECT_SETREF (mono_ex, trace_ips, glist_to_array (trace_ips, mono_defaults.int_class));
+
+                                               if (has_dynamic_methods)
+                                                       /* These methods could go away anytime, so compute the stack trace now */
+                                                       MONO_OBJECT_SETREF (mono_ex, stack_trace, ves_icall_System_Exception_get_trace (mono_ex));
+                                       }
+                                       g_list_free (trace_ips);
+                                       trace_ips = NULL;
+
+                                       if (ji->from_llvm) {
+#ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
+                                               MONO_CONTEXT_SET_LLVM_EXC_REG (ctx, ex_obj);
+#else
+                                               g_assert_not_reached ();
+#endif
+                                       } else {
+                                               /* store the exception object in bp + ei->exvar_offset */
+                                               *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = ex_obj;
+                                       }
+
+                                       mono_debugger_agent_begin_exception_filter (mono_ex, ctx, &initial_ctx);
+                                       filtered = call_filter (ctx, ei->data.filter);
+                                       mono_debugger_agent_end_exception_filter (mono_ex, ctx, &initial_ctx);
+                                       if (filtered && out_filter_idx)
+                                               *out_filter_idx = filter_idx;
+                                       if (out_ji)
+                                               *out_ji = ji;
+                                       filter_idx ++;
+
+                                       if (filtered) {
+                                               /* mono_debugger_agent_handle_exception () needs this */
+                                               MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
+                                               return TRUE;
+                                       }
+                               }
+
+                               if (ei->flags == MONO_EXCEPTION_CLAUSE_NONE && mono_object_isinst (ex_obj, catch_class)) {
+                                       if (mono_ex && !initial_trace_ips) {
+                                               trace_ips = g_list_reverse (trace_ips);
+                                               MONO_OBJECT_SETREF (mono_ex, trace_ips, glist_to_array (trace_ips, mono_defaults.int_class));
+                                               if (has_dynamic_methods)
+                                                       /* These methods could go away anytime, so compute the stack trace now */
+                                                       MONO_OBJECT_SETREF (mono_ex, stack_trace, ves_icall_System_Exception_get_trace (mono_ex));
+                                       }
+                                       g_list_free (trace_ips);
+
+                                       /* mono_debugger_agent_handle_exception () needs this */
+                                       MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
+                                       return TRUE;
+                               }
+                       }
+               }
+
+               *ctx = new_ctx;
+       }
+
+       g_assert_not_reached ();
+}
+
 /**
  * mono_handle_exception_internal:
  * @ctx: saved processor state
  * @obj: the exception object
- * @test_only: only test if the exception is caught, but dont call handlers
- * @out_filter_idx: out parameter. if test_only is true, set to the index of 
- * the first filter clause which caught the exception.
  * @resume: whenever to resume unwinding based on the state in MonoJitTlsData.
  */
 static gboolean
-mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer original_ip, gboolean test_only, gboolean resume, gint32 *out_filter_idx, MonoJitInfo **out_ji, MonoObject *non_exception)
+mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer original_ip, gboolean resume, MonoJitInfo **out_ji)
 {
        MonoDomain *domain = mono_domain_get ();
-       MonoJitInfo *ji, rji;
+       MonoJitInfo *ji;
        static int (*call_filter) (MonoContext *, gpointer) = NULL;
        static void (*restore_context) (void *);
        MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
        MonoLMF *lmf = mono_get_lmf ();
-       MonoArray *initial_trace_ips = NULL;
-       GList *trace_ips = NULL;
        MonoException *mono_ex;
        gboolean stack_overflow = FALSE;
        MonoContext initial_ctx;
        int frame_count = 0;
-       gboolean has_dynamic_methods = FALSE;
        gint32 filter_idx, first_filter_idx;
+       int i;
+       MonoObject *ex_obj;
+       MonoObject *non_exception = NULL;
 
        g_assert (ctx != NULL);
        if (!obj) {
@@ -1228,29 +1387,38 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
                obj = mono_get_exception_null_reference ();
        }
 
-       if (!test_only && !mono_object_isinst (obj, mono_defaults.exception_class)) {
+       if (!mono_object_isinst (obj, mono_defaults.exception_class)) {
                non_exception = obj;
                obj = mono_get_exception_runtime_wrapped (obj);
        }
 
        mono_ex = (MonoException*)obj;
-       initial_trace_ips = mono_ex->trace_ips;
 
        if (mono_object_isinst (obj, mono_defaults.exception_class)) {
                mono_ex = (MonoException*)obj;
-               initial_trace_ips = mono_ex->trace_ips;
        } else {
                mono_ex = NULL;
        }
 
-       if (mono_ex && jit_tls->class_cast_from && !strcmp (mono_ex->object.vtable->klass->name, "InvalidCastException")) {
-               char *from_name = mono_type_get_full_name (jit_tls->class_cast_from);
-               char *to_name = mono_type_get_full_name (jit_tls->class_cast_to);
-               char *msg = g_strdup_printf ("Unable to cast object of type '%s' to type '%s'.", from_name, to_name);
-               mono_ex->message = mono_string_new (domain, msg);
-               g_free (from_name);
-               g_free (to_name);
-               g_free (msg);
+       if (mono_ex && jit_tls->class_cast_from) {
+               if (!strcmp (mono_ex->object.vtable->klass->name, "InvalidCastException")) {
+                       char *from_name = mono_type_get_full_name (jit_tls->class_cast_from);
+                       char *to_name = mono_type_get_full_name (jit_tls->class_cast_to);
+                       char *msg = g_strdup_printf ("Unable to cast object of type '%s' to type '%s'.", from_name, to_name);
+                       mono_ex->message = mono_string_new (domain, msg);
+                       g_free (from_name);
+                       g_free (to_name);
+                       g_free (msg);
+               }
+               if (!strcmp (mono_ex->object.vtable->klass->name, "ArrayTypeMismatchException")) {
+                       char *from_name = mono_type_get_full_name (jit_tls->class_cast_from);
+                       char *to_name = mono_type_get_full_name (jit_tls->class_cast_to);
+                       char *msg = g_strdup_printf ("Source array of type '%s' cannot be cast to destination array type '%s'.", from_name, to_name);
+                       mono_ex->message = mono_string_new (domain, msg);
+                       g_free (from_name);
+                       g_free (to_name);
+                       g_free (msg);
+               }
        }
 
        if (!call_filter)
@@ -1262,7 +1430,15 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
        g_assert (jit_tls->end_of_stack);
        g_assert (jit_tls->abort_func);
 
-       if (!test_only && !resume) {
+       /*
+        * We set orig_ex_ctx_set to TRUE/FALSE around profiler calls to make sure it doesn't
+        * end up being TRUE on any code path.
+        */
+       memcpy (&jit_tls->orig_ex_ctx, ctx, sizeof (MonoContext));
+
+       if (!resume) {
+               gboolean res;
+
                MonoContext ctx_cp = *ctx;
                if (mono_trace_is_enabled ()) {
                        MonoMethod *system_exception_get_message = mono_class_get_method_from_name (mono_defaults.exception_class, "get_Message", 0);
@@ -1288,11 +1464,23 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
                        if (mono_ex && mono_trace_eval_exception (mono_object_class (mono_ex)))
                                mono_print_thread_dump_from_ctx (ctx);
                }
+               jit_tls->orig_ex_ctx_set = TRUE;
                mono_profiler_exception_thrown (obj);
-               if (!mono_handle_exception_internal (&ctx_cp, obj, original_ip, TRUE, FALSE, &first_filter_idx, out_ji, non_exception)) {
+               jit_tls->orig_ex_ctx_set = FALSE;
+
+               res = mono_handle_exception_internal_first_pass (&ctx_cp, obj, original_ip, &first_filter_idx, out_ji, non_exception);
+
+               if (!res) {
                        if (mono_break_on_exc)
                                G_BREAKPOINT ();
                        mono_debugger_agent_handle_exception (obj, ctx, NULL);
+
+                       if (mini_get_debug_options ()->suspend_on_unhandled) {
+                               fprintf (stderr, "Unhandled exception, suspending...");
+                               while (1)
+                                       ;
+                       }
+
                        // FIXME: This runs managed code so it might cause another stack overflow when
                        // we are handling a stack overflow
                        mono_unhandled_exception (obj);
@@ -1301,19 +1489,17 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
                }
        }
 
-       if (out_filter_idx)
-               *out_filter_idx = -1;
        if (out_ji)
                *out_ji = NULL;
        filter_idx = 0;
        initial_ctx = *ctx;
-       memset (&rji, 0, sizeof (rji));
 
        while (1) {
                MonoContext new_ctx;
                guint32 free_stack;
                int clause_index_start = 0;
-
+               gboolean unwind_res = TRUE;
+               
                if (resume) {
                        resume = FALSE;
                        ji = jit_tls->resume_state.ji;
@@ -1323,254 +1509,186 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
                        first_filter_idx = jit_tls->resume_state.first_filter_idx;
                        filter_idx = jit_tls->resume_state.filter_idx;
                } else {
-                       ji = mono_find_jit_info (domain, jit_tls, &rji, &rji, ctx, &new_ctx, 
-                                                                        NULL, &lmf, NULL, NULL);
-                       if (!ji) {
-                               g_warning ("Exception inside function without unwind info");
-                               g_assert_not_reached ();
-                       }
-               }
+                       StackFrameInfo frame;
 
-               if (ji != (gpointer)-1 && !(ji->code_start <= MONO_CONTEXT_GET_IP (ctx) && (((guint8*)ji->code_start + ji->code_size >= (guint8*)MONO_CONTEXT_GET_IP (ctx))))) {
-                       /*
-                        * The exception was raised in native code and we got back to managed code 
-                        * using the LMF.
-                        */
-                       *ctx = new_ctx;
-                       continue;
+                       unwind_res = mono_find_jit_info_ext (domain, jit_tls, NULL, ctx, &new_ctx, NULL, &lmf, NULL, &frame);
+                       if (unwind_res) {
+                               if (frame.type == FRAME_TYPE_DEBUGGER_INVOKE || frame.type == FRAME_TYPE_MANAGED_TO_NATIVE) {
+                                       *ctx = new_ctx;
+                                       continue;
+                               }
+                               g_assert (frame.type == FRAME_TYPE_MANAGED);
+                               ji = frame.ji;
+                       }
                }
 
-               if (ji != (gpointer)-1) {
-                       frame_count ++;
-                       //printf ("M: %s %d %d.\n", mono_method_full_name (ji->method, TRUE), frame_count, test_only);
+               if (!unwind_res) {
+                       *(mono_get_lmf_addr ()) = lmf;
 
-                       if (mini_get_debug_options ()->reverse_pinvoke_exceptions && ji->method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
-                               g_error ("A native frame was found while unwinding the stack after an exception.\n"
-                               "The native frame called the managed method:\n%s\n",
-                               mono_method_full_name (ji->method, TRUE));
-                       }
-
-                       if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
-                               /* 
-                                * Avoid overwriting the stack trace if the exception is
-                                * rethrown. Also avoid giant stack traces during a stack
-                                * overflow.
-                                */
-                               if (!initial_trace_ips && (frame_count < 1000)) {
-                                       trace_ips = g_list_prepend (trace_ips, MONO_CONTEXT_GET_IP (ctx));
-                                       trace_ips = g_list_prepend (trace_ips,
-                                               get_generic_info_from_stack_frame (ji, ctx));
-                               }
-                       }
+                       jit_tls->abort_func (obj);
+                       g_assert_not_reached ();
+               }
 
-                       if (ji->method->dynamic)
-                               has_dynamic_methods = TRUE;
+               frame_count ++;
+               //printf ("M: %s %d.\n", mono_method_full_name (ji->method, TRUE), frame_count);
 
-                       if (stack_overflow)
-#ifndef MONO_ARCH_STACK_GROWS_UP
+               if (stack_overflow) {
+                       if (DOES_STACK_GROWS_UP)
                                free_stack = (guint8*)(MONO_CONTEXT_GET_SP (ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx));
-#else
-                               free_stack = (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (ctx));
-#endif
                        else
-                               free_stack = 0xffffff;
+                               free_stack = (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (ctx));
+               } else {
+                       free_stack = 0xffffff;
+               }
+                               
+               for (i = clause_index_start; i < ji->num_clauses; i++) {
+                       MonoJitExceptionInfo *ei = &ji->clauses [i];
+                       gboolean filtered = FALSE;
 
                        /* 
                         * During stack overflow, wait till the unwinding frees some stack
                         * space before running handlers/finalizers.
                         */
-                       if ((free_stack > (64 * 1024)) && ji->num_clauses) {
-                               int i;
-                               MonoObject *ex_obj;
-                               
-                               for (i = clause_index_start; i < ji->num_clauses; i++) {
-                                       MonoJitExceptionInfo *ei = &ji->clauses [i];
-                                       gboolean filtered = FALSE;
+                       if (free_stack <= (64 * 1024))
+                               continue;
 
-#if defined(__s390__)
-                                       /* 
-                                        * This is required in cases where a try block starts immediately after
-                                        * a call which causes an exception. Testcase: tests/exception8.cs.
-                                        * FIXME: Clean this up.
-                                        */
-                                       if (ei->try_start < MONO_CONTEXT_GET_IP (ctx) && MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) {
-#else
-                                       if (is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (ctx))) {
-#endif
-                                               /* catch block */
-                                               MonoClass *catch_class = get_exception_catch_class (ei, ji, ctx);
+                       if (is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (ctx))) {
+                               /* catch block */
+                               MonoClass *catch_class = get_exception_catch_class (ei, ji, ctx);
 
-                                               /*
-                                                * Have to unwrap RuntimeWrappedExceptions if the
-                                                * method's assembly doesn't have a RuntimeCompatibilityAttribute.
-                                                */
-                                               if (non_exception && !wrap_non_exception_throws (ji->method))
-                                                       ex_obj = non_exception;
-                                               else
-                                                       ex_obj = obj;
+                               /*
+                                * Have to unwrap RuntimeWrappedExceptions if the
+                                * method's assembly doesn't have a RuntimeCompatibilityAttribute.
+                                */
+                               if (non_exception && !wrap_non_exception_throws (ji->method))
+                                       ex_obj = non_exception;
+                               else
+                                       ex_obj = obj;
 
-                                               if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)) {
-                                                       if (ji->from_llvm) {
+                               if (((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER))) {
+                                       if (ji->from_llvm) {
 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
-                                                               MONO_CONTEXT_SET_LLVM_EXC_REG (ctx, ex_obj);
+                                               MONO_CONTEXT_SET_LLVM_EXC_REG (ctx, ex_obj);
 #else
-                                                               g_assert_not_reached ();
+                                               g_assert_not_reached ();
 #endif
-                                                       } else {
-                                                               /* store the exception object in bp + ei->exvar_offset */
-                                                               *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = ex_obj;
-                                                       }
-                                               }
+                                       } else {
+                                               /* store the exception object in bp + ei->exvar_offset */
+                                               *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = ex_obj;
+                                       }
+                               }
 
-                                               if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
-                                                       if (test_only) {
-                                                               mono_perfcounters->exceptions_filters++;
-                                                               mono_debugger_call_exception_handler (ei->data.filter, MONO_CONTEXT_GET_SP (ctx), ex_obj);
-                                                               filtered = call_filter (ctx, ei->data.filter);
-                                                               if (filtered && out_filter_idx)
-                                                                       *out_filter_idx = filter_idx;
-                                                               if (out_ji)
-                                                                       *out_ji = ji;
-                                                       }
-                                                       else {
-                                                               /* 
-                                                                * Filter clauses should only be run in the 
-                                                                * first pass of exception handling.
-                                                                */
-                                                               filtered = (filter_idx == first_filter_idx);
-                                                       }
-                                                       filter_idx ++;
-                                               }
+                               if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
+                                       /* 
+                                        * Filter clauses should only be run in the 
+                                        * first pass of exception handling.
+                                        */
+                                       filtered = (filter_idx == first_filter_idx);
+                                       filter_idx ++;
+                               }
 
-                                               if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE && 
-                                                    mono_object_isinst (ex_obj, catch_class)) || filtered) {
-                                                       if (test_only) {
-                                                               if (mono_ex && !initial_trace_ips) {
-                                                                       trace_ips = g_list_reverse (trace_ips);
-                                                                       MONO_OBJECT_SETREF (mono_ex, trace_ips, glist_to_array (trace_ips, mono_defaults.int_class));
-                                                                       if (has_dynamic_methods)
-                                                                               /* These methods could go away anytime, so compute the stack trace now */
-                                                                               MONO_OBJECT_SETREF (mono_ex, stack_trace, ves_icall_System_Exception_get_trace (mono_ex));
-                                                               }
-                                                               g_list_free (trace_ips);
-
-                                                               return TRUE;
-                                                       }
-                                                       /*
-                                                        * This guards against the situation that we abort a thread that is executing a finally clause
-                                                        * that was called by the EH machinery. It won't have a guard trampoline installed, so we must
-                                                        * check for this situation here and resume interruption if we are below the guarded block.
+                               if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE && 
+                                        mono_object_isinst (ex_obj, catch_class)) || filtered) {
+                                       /*
+                                        * This guards against the situation that we abort a thread that is executing a finally clause
+                                        * that was called by the EH machinery. It won't have a guard trampoline installed, so we must
+                                        * check for this situation here and resume interruption if we are below the guarded block.
+                                        */
+                                       if (G_UNLIKELY (jit_tls->handler_block_return_address)) {
+                                               gboolean is_outside = FALSE;
+                                               gpointer prot_bp = MONO_CONTEXT_GET_BP (&jit_tls->handler_block_context);
+                                               gpointer catch_bp = MONO_CONTEXT_GET_BP (ctx);
+                                               //FIXME make this stack direction aware
+                                               if (catch_bp > prot_bp) {
+                                                       is_outside = TRUE;
+                                               } else if (catch_bp == prot_bp) {
+                                                       /* Can be either try { try { } catch {} } finally {} or try { try { } finally {} } catch {}
+                                                        * So we check if the catch handler_start is protected by the guarded handler protected region
+                                                        *
+                                                        * Assumptions:
+                                                        *      If there is an outstanding guarded_block return address, it means the current thread must be aborted.
+                                                        *      This is the only way to reach out the guarded block as other cases are handled by the trampoline.
+                                                        *      There aren't any further finally/fault handler blocks down the stack over this exception.
+                                                        *   This must be ensured by the code that installs the guard trampoline.
                                                         */
-                                                       if (G_UNLIKELY (jit_tls->handler_block_return_address)) {
-                                                               gboolean is_outside = FALSE;
-                                                               gpointer prot_bp = MONO_CONTEXT_GET_BP (&jit_tls->ex_ctx);
-                                                               gpointer catch_bp = MONO_CONTEXT_GET_BP (ctx);
-                                                               //FIXME make this stack direction aware
-                                                               if (catch_bp > prot_bp) {
-                                                                       is_outside = TRUE;
-                                                               } else if (catch_bp == prot_bp) {
-                                                                       /* Can be either try { try { } catch {} } finally {} or try { try { } finally {} } catch {}
-                                                                        * So we check if the catch handler_start is protected by the guarded handler protected region
-                                                                        *
-                                                                        * Assumptions:
-                                                                        *      If there is an outstanding guarded_block return address, it means the current thread must be aborted.
-                                                                        *      This is the only way to reach out the guarded block as other cases are handled by the trampoline.
-                                                                        *      There aren't any further finally/fault handler blocks down the stack over this exception.
-                                                                        *   This must be ensured by the code that installs the guard trampoline.
-                                                                        */
-                                                                       g_assert (ji == mini_jit_info_table_find (domain, MONO_CONTEXT_GET_IP (&jit_tls->ex_ctx), NULL));
-
-                                                                       if (!is_address_protected (ji, jit_tls->handler_block, ei->handler_start)) {
-                                                                               is_outside = TRUE;
-                                                                       }
-                                                               }
-                                                               if (is_outside) {
-                                                                       jit_tls->handler_block_return_address = NULL;
-                                                                       jit_tls->handler_block = NULL;
-                                                                       mono_thread_resume_interruption (); /*We ignore the exception here, it will be raised later*/
-                                                               }
-                                                       }
+                                                       g_assert (ji == mini_jit_info_table_find (domain, MONO_CONTEXT_GET_IP (&jit_tls->handler_block_context), NULL));
 
-                                                       if (mono_trace_is_enabled () && mono_trace_eval (ji->method))
-                                                               g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
-                                                       mono_profiler_exception_clause_handler (ji->method, ei->flags, i);
-                                                       mono_debugger_call_exception_handler (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), ex_obj);
-                                                       MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
-                                                       *(mono_get_lmf_addr ()) = lmf;
-                                                       mono_perfcounters->exceptions_depth += frame_count;
-                                                       if (obj == domain->stack_overflow_ex)
-                                                               jit_tls->handling_stack_ovf = FALSE;
-
-                                                       return 0;
-                                               }
-                                               if (!test_only && is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (ctx)) &&
-                                                   (ei->flags == MONO_EXCEPTION_CLAUSE_FAULT)) {
-                                                       if (mono_trace_is_enabled () && mono_trace_eval (ji->method))
-                                                               g_print ("EXCEPTION: fault clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
-                                                       mono_profiler_exception_clause_handler (ji->method, ei->flags, i);
-                                                       mono_debugger_call_exception_handler (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), ex_obj);
-                                                       call_filter (ctx, ei->handler_start);
-                                               }
-                                               if (!test_only && is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (ctx)) &&
-                                                   (ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY)) {
-                                                       if (mono_trace_is_enabled () && mono_trace_eval (ji->method))
-                                                               g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
-                                                       mono_profiler_exception_clause_handler (ji->method, ei->flags, i);
-                                                       mono_debugger_call_exception_handler (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), ex_obj);
-                                                       mono_perfcounters->exceptions_finallys++;
-                                                       *(mono_get_lmf_addr ()) = lmf;
-                                                       if (ji->from_llvm) {
-                                                               /* 
-                                                                * LLVM compiled finally handlers follow the design
-                                                                * of the c++ ehabi, i.e. they call a resume function
-                                                                * at the end instead of returning to the caller.
-                                                                * So save the exception handling state,
-                                                                * mono_resume_unwind () will call us again to continue
-                                                                * the unwinding.
-                                                                */
-                                                               jit_tls->resume_state.ex_obj = obj;
-                                                               jit_tls->resume_state.ji = ji;
-                                                               jit_tls->resume_state.clause_index = i + 1;
-                                                               jit_tls->resume_state.ctx = *ctx;
-                                                               jit_tls->resume_state.new_ctx = new_ctx;
-                                                               jit_tls->resume_state.lmf = lmf;
-                                                               jit_tls->resume_state.first_filter_idx = first_filter_idx;
-                                                               jit_tls->resume_state.filter_idx = filter_idx;
-                                                               MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
-                                                               return 0;
-                                                       } else {
-                                                               call_filter (ctx, ei->handler_start);
+                                                       if (!is_address_protected (ji, jit_tls->handler_block, ei->handler_start)) {
+                                                               is_outside = TRUE;
                                                        }
                                                }
-                                               
+                                               if (is_outside) {
+                                                       jit_tls->handler_block_return_address = NULL;
+                                                       jit_tls->handler_block = NULL;
+                                                       mono_thread_resume_interruption (); /*We ignore the exception here, it will be raised later*/
+                                               }
+                                       }
+
+                                       if (mono_trace_is_enabled () && mono_trace_eval (ji->method))
+                                               g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
+                                       jit_tls->orig_ex_ctx_set = TRUE;
+                                       mono_profiler_exception_clause_handler (ji->method, ei->flags, i);
+                                       jit_tls->orig_ex_ctx_set = FALSE;
+                                       mono_debugger_call_exception_handler (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), ex_obj);
+                                       MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
+                                       *(mono_get_lmf_addr ()) = lmf;
+                                       mono_perfcounters->exceptions_depth += frame_count;
+                                       if (obj == domain->stack_overflow_ex)
+                                               jit_tls->handling_stack_ovf = FALSE;
+
+                                       return 0;
+                               }
+                               if (is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (ctx)) &&
+                                       (ei->flags == MONO_EXCEPTION_CLAUSE_FAULT)) {
+                                       if (mono_trace_is_enabled () && mono_trace_eval (ji->method))
+                                               g_print ("EXCEPTION: fault clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
+                                       jit_tls->orig_ex_ctx_set = TRUE;
+                                       mono_profiler_exception_clause_handler (ji->method, ei->flags, i);
+                                       jit_tls->orig_ex_ctx_set = FALSE;
+                                       mono_debugger_call_exception_handler (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), ex_obj);
+                                       call_filter (ctx, ei->handler_start);
+                               }
+                               if (is_address_protected (ji, ei, MONO_CONTEXT_GET_IP (ctx)) &&
+                                       (ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY)) {
+                                       if (mono_trace_is_enabled () && mono_trace_eval (ji->method))
+                                               g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
+                                       jit_tls->orig_ex_ctx_set = TRUE;
+                                       mono_profiler_exception_clause_handler (ji->method, ei->flags, i);
+                                       jit_tls->orig_ex_ctx_set = FALSE;
+                                       mono_debugger_call_exception_handler (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), ex_obj);
+                                       mono_perfcounters->exceptions_finallys++;
+                                       *(mono_get_lmf_addr ()) = lmf;
+                                       if (ji->from_llvm) {
+                                               /* 
+                                                * LLVM compiled finally handlers follow the design
+                                                * of the c++ ehabi, i.e. they call a resume function
+                                                * at the end instead of returning to the caller.
+                                                * So save the exception handling state,
+                                                * mono_resume_unwind () will call us again to continue
+                                                * the unwinding.
+                                                */
+                                               jit_tls->resume_state.ex_obj = obj;
+                                               jit_tls->resume_state.ji = ji;
+                                               jit_tls->resume_state.clause_index = i + 1;
+                                               jit_tls->resume_state.ctx = *ctx;
+                                               jit_tls->resume_state.new_ctx = new_ctx;
+                                               jit_tls->resume_state.lmf = lmf;
+                                               jit_tls->resume_state.first_filter_idx = first_filter_idx;
+                                               jit_tls->resume_state.filter_idx = filter_idx;
+                                               MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
+                                               return 0;
+                                       } else {
+                                               call_filter (ctx, ei->handler_start);
                                        }
                                }
                        }
-                       if (!test_only)
-                               mono_profiler_exception_method_leave (ji->method);
                }
 
-               *ctx = new_ctx;
-
-               if (ji == (gpointer)-1) {
-
-                       if (!test_only) {
-                               *(mono_get_lmf_addr ()) = lmf;
+               jit_tls->orig_ex_ctx_set = TRUE;
+               mono_profiler_exception_method_leave (ji->method);
+               jit_tls->orig_ex_ctx_set = FALSE;
 
-                               jit_tls->abort_func (obj);
-                               g_assert_not_reached ();
-                       } else {
-                               if (mono_ex && !initial_trace_ips) {
-                                       trace_ips = g_list_reverse (trace_ips);
-                                       MONO_OBJECT_SETREF (mono_ex, trace_ips, glist_to_array (trace_ips, mono_defaults.int_class));
-                                       if (has_dynamic_methods)
-                                               /* These methods could go away anytime, so compute the stack trace now */
-                                               MONO_OBJECT_SETREF (mono_ex, stack_trace, ves_icall_System_Exception_get_trace (mono_ex));
-                               }
-                               g_list_free (trace_ips);
-                               return FALSE;
-                       }
-               }
+               *ctx = new_ctx;
        }
 
        g_assert_not_reached ();
@@ -1617,7 +1735,7 @@ mono_debugger_handle_exception (MonoContext *ctx, MonoObject *obj)
                 * The debugger wants us to stop only if this exception is user-unhandled.
                 */
 
-               ret = mono_handle_exception_internal (&ctx_cp, obj, MONO_CONTEXT_GET_IP (ctx), TRUE, FALSE, NULL, &ji, NULL);
+               ret = mono_handle_exception_internal_first_pass (&ctx_cp, obj, MONO_CONTEXT_GET_IP (ctx), NULL, &ji, NULL);
                if (ret && (ji != NULL) && (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE)) {
                        /*
                         * The exception is handled in a runtime-invoke wrapper, that means that it's unhandled
@@ -1695,7 +1813,8 @@ mono_handle_exception (MonoContext *ctx, gpointer obj, gpointer original_ip, gbo
        if (!test_only)
                mono_perfcounters->exceptions_thrown++;
 
-       return mono_handle_exception_internal (ctx, obj, original_ip, test_only, FALSE, NULL, NULL, NULL);
+       g_assert (!test_only);
+       return mono_handle_exception_internal (ctx, obj, original_ip, FALSE, NULL);
 }
 
 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
@@ -1721,6 +1840,7 @@ mono_setup_altstack (MonoJitTlsData *tls)
        g_assert (staddr);
 
        tls->end_of_stack = staddr + stsize;
+       tls->stack_size = stsize;
 
        /*g_print ("thread %p, stack_base: %p, stack_size: %d\n", (gpointer)pthread_self (), staddr, stsize);*/
 
@@ -1736,14 +1856,6 @@ mono_setup_altstack (MonoJitTlsData *tls)
                tls->stack_ovf_valloced = TRUE;
        }
 
-       /*
-        * threads created by nptl does not seem to have a guard page, and
-        * since the main thread is not created by us, we can't even set one.
-        * Increasing stsize fools the SIGSEGV signal handler into thinking this
-        * is a stack overflow exception.
-        */
-       tls->stack_size = stsize + mono_pagesize ();
-
        /* Setup an alternate signal stack */
        tls->signal_stack = mono_valloc (0, MONO_ARCH_SIGNAL_STACK_SIZE, MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_PRIVATE|MONO_MMAP_ANON);
        tls->signal_stack_size = MONO_ARCH_SIGNAL_STACK_SIZE;
@@ -1891,6 +2003,74 @@ mono_handle_soft_stack_ovf (MonoJitTlsData *jit_tls, MonoJitInfo *ji, void *ctx,
        return FALSE;
 }
 
+typedef struct {
+       FILE *stream;
+       MonoMethod *omethod;
+       int count;
+} PrintOverflowUserData;
+
+static gboolean
+print_overflow_stack_frame (MonoMethod *method, gint32 native_offset, gint32 il_offset, gboolean managed, gpointer data)
+{
+       PrintOverflowUserData *user_data = data;
+       FILE *stream = user_data->stream;
+       gchar *location;
+
+       if (method) {
+               if (user_data->count == 0) {
+                       /* The first frame is in its prolog, so a line number cannot be computed */
+                       user_data->count ++;
+                       return FALSE;
+               }
+
+               /* If this is a one method overflow, skip the other instances */
+               if (method == user_data->omethod)
+                       return FALSE;
+
+               location = mono_debug_print_stack_frame (method, native_offset, mono_domain_get ());
+               fprintf (stream, "  %s\n", location);
+               g_free (location);
+
+               if (user_data->count == 1) {
+                       fprintf (stream, "  <...>\n");
+                       user_data->omethod = method;
+               } else {
+                       user_data->omethod = NULL;
+               }
+
+               user_data->count ++;
+       } else
+               fprintf (stream, "  at <unknown> <0x%05x>\n", native_offset);
+
+       return FALSE;
+}
+
+void
+mono_handle_hard_stack_ovf (MonoJitTlsData *jit_tls, MonoJitInfo *ji, void *ctx, guint8* fault_addr)
+{
+       const char *method;
+       /* we don't do much now, but we can warn the user with a useful message */
+       fprintf (stderr, "Stack overflow: IP: %p, fault addr: %p\n", mono_arch_ip_from_context (ctx), fault_addr);
+       if (ji && ji->method) {
+               PrintOverflowUserData ud;
+               MonoContext mctx;
+
+               mono_arch_sigctx_to_monoctx (ctx, &mctx);
+                       
+               method = mono_method_full_name (ji->method, TRUE);
+               fprintf (stderr, "Stacktrace:\n");
+
+               memset (&ud, 0, sizeof (ud));
+               ud.stream = stderr;
+
+               mono_jit_walk_stack_from_ctx (print_overflow_stack_frame, &mctx, MONO_UNWIND_LOOKUP_ACTUAL_METHOD, &ud);
+       } else {
+               method = "Unmanaged";
+               fprintf (stderr, "  at %s\n", method);
+       }
+       _exit (1);
+}
+
 static gboolean
 print_stack_frame (MonoMethod *method, gint32 native_offset, gint32 il_offset, gboolean managed, gpointer data)
 {
@@ -2093,12 +2273,17 @@ mono_print_thread_dump_internal (void *sigctx, MonoContext *start_ctx)
        else
                mono_arch_sigctx_to_monoctx (sigctx, &ctx);
 
-       mono_jit_walk_stack_from_ctx (print_stack_frame_to_string, &ctx, TRUE, text);
+       mono_jit_walk_stack_from_ctx (print_stack_frame_to_string, &ctx, MONO_UNWIND_LOOKUP_ALL, text);
 #else
        printf ("\t<Stack traces in thread dumps not supported on this platform>\n");
 #endif
 
        fprintf (stdout, "%s", text->str);
+
+#if PLATFORM_WIN32 && TARGET_WIN32 && _DEBUG
+       OutputDebugStringA(text->str);
+#endif
+
        g_string_free (text, TRUE);
        fflush (stdout);
 }
@@ -2138,7 +2323,7 @@ mono_resume_unwind (MonoContext *ctx)
        MONO_CONTEXT_SET_SP (ctx, MONO_CONTEXT_GET_SP (&jit_tls->resume_state.ctx));
        new_ctx = *ctx;
 
-       mono_handle_exception_internal (&new_ctx, jit_tls->resume_state.ex_obj, NULL, FALSE, TRUE, NULL, NULL, NULL);
+       mono_handle_exception_internal (&new_ctx, jit_tls->resume_state.ex_obj, NULL, TRUE, NULL);
 
        if (!restore_context)
                restore_context = mono_get_restore_context ();
@@ -2155,11 +2340,15 @@ typedef struct {
 } FindHandlerBlockData;
 
 static gboolean
-find_last_handler_block (MonoDomain *domain, MonoContext *ctx, MonoJitInfo *ji, gpointer data)
+find_last_handler_block (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
 {
        int i;
        gpointer ip;
        FindHandlerBlockData *pdata = data;
+       MonoJitInfo *ji = frame->ji;
+
+       if (!ji)
+               return FALSE;
 
        if (ji->method->wrapper_type)
                return FALSE;
@@ -2228,12 +2417,12 @@ mono_install_handler_block_guard (MonoInternalThread *thread, MonoContext *ctx)
        if (!jit_tls || jit_tls->handler_block_return_address)
                return FALSE;
 
-       mono_walk_stack_full (domain, jit_tls, ctx, find_last_handler_block, FALSE, &data);
+       mono_walk_stack (find_last_handler_block, domain, ctx, MONO_UNWIND_SIGNAL_SAFE, NULL, NULL, &data);
 
        if (!data.ji)
                return FALSE;
 
-       memcpy (&jit_tls->ex_ctx, &data.ctx, sizeof (MonoContext));
+       memcpy (&jit_tls->handler_block_context, &data.ctx, sizeof (MonoContext));
 
        resume_ip = install_handler_block_guard (data.ji, &data.ctx);
        if (resume_ip == NULL)
@@ -2258,3 +2447,15 @@ mono_install_handler_block_guard (MonoInternalThread *thread, MonoContext *ctx)
 
 #endif
 
+void
+mono_set_cast_details (MonoClass *from, MonoClass *to)
+{
+       MonoJitTlsData *jit_tls = NULL;
+
+       if (mini_get_debug_options ()->better_cast_details) {
+               jit_tls = TlsGetValue (mono_jit_tls_id);
+               jit_tls->class_cast_from = from;
+               jit_tls->class_cast_to = to;
+       }
+}
+