svn path=/trunk/mono/; revision=139311
[mono.git] / mono / mini / mini-exceptions.c
index 3bbac8d2a41c9cefe1a422e0fb002891c258e693..f7b56a8511916c3eb7bd236ecbb137bdb5291d7a 100644 (file)
@@ -3,8 +3,10 @@
  *
  * Authors:
  *   Dietmar Maurer (dietmar@ximian.com)
+ *   Mono Team (mono-list@lists.ximian.com)
  *
- * (C) 2001 Ximian, Inc.
+ * Copyright 2001-2003 Ximian, Inc.
+ * Copyright 2003-2008 Ximian, Inc.
  */
 
 #include <config.h>
 #include <execinfo.h>
 #endif
 
-#ifndef PLATFORM_WIN32
+#ifdef HAVE_SYS_TYPES_H
 #include <sys/types.h>
+#endif
+
+#ifdef HAVE_SYS_WAIT_H
 #include <sys/wait.h>
-#include <unistd.h>
 #endif
 
 #ifdef HAVE_UNISTD_H
@@ -42,6 +46,7 @@
 #include <mono/utils/mono-mmap.h>
 
 #include "mini.h"
+#include "debug-mini.h"
 #include "trace.h"
 
 #ifndef MONO_ARCH_CONTEXT_DEF
@@ -52,6 +57,12 @@ static gpointer restore_context_func, call_filter_func;
 static gpointer throw_exception_func, rethrow_exception_func;
 static gpointer throw_exception_by_name_func, throw_corlib_exception_func;
 
+static gpointer try_more_restore_tramp = NULL;
+static gpointer restore_stack_protection_tramp = NULL;
+
+static void try_more_restore (void);
+static void restore_stack_protection (void);
+
 void
 mono_exceptions_init (void)
 {
@@ -71,13 +82,19 @@ mono_exceptions_init (void)
                rethrow_exception_func = mono_arch_get_rethrow_exception_full (&code_size, &ji, FALSE);
        }
 #else
-#ifndef CUSTOM_EXCEPTION_HANDLING
        restore_context_func = mono_arch_get_restore_context ();
        call_filter_func = mono_arch_get_call_filter ();
-#endif
        throw_exception_func = mono_arch_get_throw_exception ();
        rethrow_exception_func = mono_arch_get_rethrow_exception ();
 #endif
+#ifdef MONO_ARCH_HAVE_RESTORE_STACK_SUPPORT
+       try_more_restore_tramp = mono_create_specific_trampoline (try_more_restore, MONO_TRAMPOLINE_RESTORE_STACK_PROT, mono_domain_get (), NULL);
+       restore_stack_protection_tramp = mono_create_specific_trampoline (restore_stack_protection, MONO_TRAMPOLINE_RESTORE_STACK_PROT, mono_domain_get (), NULL);
+#endif
+
+#ifdef MONO_ARCH_HAVE_EXCEPTIONS_INIT
+       mono_arch_exceptions_init ();
+#endif
 }
 
 gpointer
@@ -170,8 +187,6 @@ mono_get_throw_corlib_exception (void)
        return throw_corlib_exception_func;
 }
 
-#ifndef mono_find_jit_info
-
 /* mono_find_jit_info:
  *
  * This function is used to gather information from @ctx. It return the 
@@ -181,8 +196,8 @@ mono_get_throw_corlib_exception (void)
  * the @lmf if necessary. @native_offset return the IP offset from the 
  * start of the function or -1 if that info is not available.
  */
-static MonoJitInfo *
-mono_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx, 
+MonoJitInfo *
+mono_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
                    MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
                    gboolean *managed)
 {
@@ -199,7 +214,7 @@ mono_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *re
        if (managed)
                *managed = FALSE;
 
-       ji = mono_arch_find_jit_info (domain, jit_tls, res, prev_ji, ctx, new_ctx, NULL, lmf, NULL, &managed2);
+       ji = mono_arch_find_jit_info (domain, jit_tls, res, prev_ji, ctx, new_ctx, lmf, &managed2);
 
        if (ji == (gpointer)-1)
                return ji;
@@ -240,7 +255,96 @@ mono_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *re
        return ji;
 }
 
-#endif /* mono_find_jit_info */
+static gpointer
+get_generic_info_from_stack_frame (MonoJitInfo *ji, MonoContext *ctx)
+{
+       MonoGenericJitInfo *gi;
+       gpointer info;
+
+       if (!ji->has_generic_jit_info)
+               return NULL;
+       gi = mono_jit_info_get_generic_jit_info (ji);
+       if (!gi->has_this)
+               return NULL;
+
+       if (gi->this_in_reg)
+               info = mono_arch_context_get_int_reg (ctx, gi->this_reg);
+       else
+               info = *(gpointer*)(gpointer)((char*)mono_arch_context_get_int_reg (ctx, gi->this_reg) +
+                                                                         gi->this_offset);
+       if (mono_method_get_context (ji->method)->method_inst) {
+               return info;
+       } else if ((ji->method->flags & METHOD_ATTRIBUTE_STATIC) || ji->method->klass->valuetype) {
+               return info;
+       } else {
+               /* Avoid returning a managed object */
+               MonoObject *this_obj = info;
+
+               return this_obj->vtable->klass;
+       }
+}
+
+static MonoGenericContext
+get_generic_context_from_stack_frame (MonoJitInfo *ji, gpointer generic_info)
+{
+       MonoGenericContext context = { NULL, NULL };
+       MonoClass *class, *method_container_class;
+
+       g_assert (generic_info);
+
+       g_assert (ji->method->is_inflated);
+       if (mono_method_get_context (ji->method)->method_inst) {
+               MonoMethodRuntimeGenericContext *mrgctx = generic_info;
+
+               class = mrgctx->class_vtable->klass;
+               context.method_inst = mrgctx->method_inst;
+               g_assert (context.method_inst);
+       } else if ((ji->method->flags & METHOD_ATTRIBUTE_STATIC) || ji->method->klass->valuetype) {
+               MonoVTable *vtable = generic_info;
+
+               class = vtable->klass;
+       } else {
+               class = generic_info;
+       }
+
+       g_assert (!ji->method->klass->generic_container);
+       if (ji->method->klass->generic_class)
+               method_container_class = ji->method->klass->generic_class->container_class;
+       else
+               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) {
+               class = class->parent;
+               g_assert (class);
+       }
+
+       if (class->generic_class || class->generic_container)
+               context.class_inst = mini_class_get_context (class)->class_inst;
+
+       if (class->generic_class)
+               g_assert (mono_class_has_parent_and_ignore_generics (class->generic_class->container_class, method_container_class));
+       else
+               g_assert (mono_class_has_parent_and_ignore_generics (class, method_container_class));
+
+       return context;
+}
+
+static MonoMethod*
+get_method_from_stack_frame (MonoJitInfo *ji, gpointer generic_info)
+{
+       MonoGenericContext context;
+       MonoMethod *method;
+       
+       if (!ji->has_generic_jit_info || !mono_jit_info_get_generic_jit_info (ji)->has_this)
+               return ji->method;
+       context = get_generic_context_from_stack_frame (ji, generic_info);
+
+       method = mono_method_get_declaring_generic_method (ji->method);
+       method = mono_class_inflate_generic_method (method, &context);
+
+       return method;
+}
 
 MonoString *
 ves_icall_System_Exception_get_trace (MonoException *ex)
@@ -255,11 +359,12 @@ ves_icall_System_Exception_get_trace (MonoException *ex)
                /* Exception is not thrown yet */
                return NULL;
 
-       len = mono_array_length (ta);
+       len = mono_array_length (ta) >> 1;
        trace_str = g_string_new ("");
        for (i = 0; i < len; i++) {
                MonoJitInfo *ji;
-               gpointer ip = mono_array_get (ta, gpointer, i);
+               gpointer ip = mono_array_get (ta, gpointer, i * 2 + 0);
+               gpointer generic_info = mono_array_get (ta, gpointer, i * 2 + 1);
 
                ji = mono_jit_info_table_find (domain, ip);
                if (ji == NULL) {
@@ -268,10 +373,11 @@ ves_icall_System_Exception_get_trace (MonoException *ex)
                } else {
                        gchar *location;
                        gint32 address;
+                       MonoMethod *method = get_method_from_stack_frame (ji, generic_info);
 
                        address = (char *)ip - (char *)ji->code_start;
                        location = mono_debug_print_stack_frame (
-                               ji->method, address, ex->object.vtable->domain);
+                               method, address, ex->object.vtable->domain);
 
                        g_string_append_printf (trace_str, "%s\n", location);
                        g_free (location);
@@ -297,15 +403,17 @@ ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info
                /* Exception is not thrown yet */
                return mono_array_new (domain, mono_defaults.stack_frame_class, 0);
        }
-       
-       len = mono_array_length (ta);
+
+       len = mono_array_length (ta) >> 1;
 
        res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
 
        for (i = skip; i < len; i++) {
                MonoJitInfo *ji;
                MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
-               gpointer ip = mono_array_get (ta, gpointer, i);
+               gpointer ip = mono_array_get (ta, gpointer, i * 2 + 0);
+               gpointer generic_info = mono_array_get (ta, gpointer, i * 2 + 1);
+               MonoMethod *method;
 
                ji = mono_jit_info_table_find (domain, ip);
                if (ji == NULL) {
@@ -316,16 +424,17 @@ ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info
 
                g_assert (ji != NULL);
 
+               method = get_method_from_stack_frame (ji, generic_info);
                if (ji->method->wrapper_type) {
                        char *s;
 
                        sf->method = NULL;
-                       s = mono_method_full_name (ji->method, TRUE);
+                       s = mono_method_full_name (method, TRUE);
                        MONO_OBJECT_SETREF (sf, internal_method_name, mono_string_new (domain, s));
                        g_free (s);
                }
                else
-                       MONO_OBJECT_SETREF (sf, method, mono_method_get_object (domain, ji->method, NULL));
+                       MONO_OBJECT_SETREF (sf, method, mono_method_get_object (domain, method, NULL));
                sf->native_offset = (char *)ip - (char *)ji->code_start;
 
                /*
@@ -399,8 +508,6 @@ mono_walk_stack (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoContext *start
        }
 }
 
-#ifndef CUSTOM_STACK_WALK
-
 void
 mono_jit_walk_stack_from_ctx (MonoStackWalk func, MonoContext *start_ctx, gboolean do_il_offset, gpointer user_data)
 {
@@ -465,9 +572,9 @@ ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
        MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
        MonoLMF *lmf = mono_get_lmf ();
        MonoJitInfo *ji, rji;
-       MonoContext ctx, new_ctx;
+       MonoContext ctx, new_ctx, ji_ctx;
        MonoDebugSourceLocation *location;
-       MonoMethod *last_method = NULL;
+       MonoMethod *last_method = NULL, *actual_method;
 
        MONO_ARCH_CONTEXT_DEF;
 
@@ -480,10 +587,16 @@ ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
 #endif
 
        do {
-               ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, native_offset, NULL);
-               
+               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;
-               
+
+               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 (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_SP (&ctx) >= jit_tls->end_of_stack)
                        return FALSE;
 
@@ -511,7 +624,9 @@ ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
 
        } while (skip >= 0);
 
-       *method = mono_method_get_object (domain, ji->method, NULL);
+       actual_method = get_method_from_stack_frame (ji, get_generic_info_from_stack_frame (ji, &ji_ctx));
+
+       mono_gc_wbarrier_generic_store (method, (MonoObject*) mono_method_get_object (domain, actual_method, NULL));
 
        location = mono_debug_lookup_source_location (ji->method, *native_offset, domain);
        if (location)
@@ -521,7 +636,7 @@ ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
 
        if (need_file_info) {
                if (location) {
-                       *file = mono_string_new (domain, location->source_file);
+                       mono_gc_wbarrier_generic_store (file, (MonoObject*) mono_string_new (domain, location->source_file));
                        *line = location->row;
                        *column = location->column;
                } else {
@@ -535,8 +650,6 @@ ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
        return TRUE;
 }
 
-#endif /* CUSTOM_STACK_WALK */
-
 typedef struct {
        guint32 skips;
        MonoSecurityFrame *frame;
@@ -711,65 +824,19 @@ ves_icall_System_Security_SecurityFrame_GetSecurityStack (gint32 skip)
        return ss.stack;
 }
 
-#ifndef CUSTOM_EXCEPTION_HANDLING
-
 static MonoClass*
 get_exception_catch_class (MonoJitExceptionInfo *ei, MonoJitInfo *ji, MonoContext *ctx)
 {
        MonoClass *catch_class = ei->data.catch_class;
-       MonoGenericJitInfo *gi;
-       gpointer info;
-       MonoClass *class, *method_container_class;
        MonoType *inflated_type;
-       MonoGenericContext context = { NULL, NULL };
+       MonoGenericContext context;
 
        if (!catch_class)
                return NULL;
 
-       if (!ji->has_generic_jit_info)
+       if (!ji->has_generic_jit_info || !mono_jit_info_get_generic_jit_info (ji)->has_this)
                return catch_class;
-
-       gi = mono_jit_info_get_generic_jit_info (ji);
-       if (!gi->has_this)
-               return catch_class;
-
-       if (gi->this_in_reg)
-               info = mono_arch_context_get_int_reg (ctx, gi->this_reg);
-       else
-               info = *(gpointer*)(gpointer)((char*)mono_arch_context_get_int_reg (ctx, gi->this_reg) +
-                               gi->this_offset);
-
-       g_assert (ji->method->is_inflated);
-
-       if (mono_method_get_context (ji->method)->method_inst) {
-               MonoMethodRuntimeGenericContext *mrgctx = info;
-
-               class = mrgctx->class_vtable->klass;
-               context.method_inst = mrgctx->method_inst;
-               g_assert (context.method_inst);
-       } else if (ji->method->flags & METHOD_ATTRIBUTE_STATIC) {
-               MonoVTable *vtable = info;
-
-               class = vtable->klass;
-       } else {
-               MonoObject *this = info;
-
-               class = this->vtable->klass;
-       }
-
-       if (class->generic_class || class->generic_container)
-               context.class_inst = mini_class_get_context (class)->class_inst;
-
-       g_assert (!ji->method->klass->generic_container);
-       if (ji->method->klass->generic_class)
-               method_container_class = ji->method->klass->generic_class->container_class;
-       else
-               method_container_class = ji->method->klass;
-
-       if (class->generic_class)
-               g_assert (class->generic_class->container_class == method_container_class);
-       else
-               g_assert (!class->generic_container && class == method_container_class);
+       context = get_generic_context_from_stack_frame (ji, get_generic_info_from_stack_frame (ji, ctx));
 
        /* FIXME: we shouldn't inflate but instead put the
           type in the rgctx and fetch it from there.  It
@@ -793,7 +860,7 @@ get_exception_catch_class (MonoJitExceptionInfo *ei, MonoJitInfo *ji, MonoContex
  * the first filter clause which caught the exception.
  */
 static gboolean
-mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer original_ip, gboolean test_only, gint32 *out_filter_idx)
+mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer original_ip, gboolean test_only, gint32 *out_filter_idx, MonoJitInfo **out_ji)
 {
        MonoDomain *domain = mono_domain_get ();
        MonoJitInfo *ji, rji;
@@ -821,7 +888,10 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
         * Allocate a new exception object instead of the preconstructed ones.
         */
        if (obj == domain->stack_overflow_ex) {
-               obj = mono_get_exception_stack_overflow ();
+               /*
+                * It is not a good idea to try and put even more pressure on the little stack available.
+                * obj = mono_get_exception_stack_overflow ();
+                */
                stack_overflow = TRUE;
        }
        else if (obj == domain->null_reference_ex) {
@@ -835,7 +905,7 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
                mono_ex = NULL;
        }
 
-       if (mono_ex && jit_tls->class_cast_to && !strcmp (mono_ex->object.vtable->klass->name, "InvalidCastException")) {
+       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);
@@ -857,32 +927,21 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
        if (!test_only) {
                MonoContext ctx_cp = *ctx;
                if (mono_trace_is_enabled ())
-                       g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
+                       g_print ("[%p:] EXCEPTION handling: %s\n", (void*)GetCurrentThreadId (), mono_object_class (obj)->name);
                mono_profiler_exception_thrown (obj);
-               if (!mono_handle_exception_internal (&ctx_cp, obj, original_ip, TRUE, &first_filter_idx)) {
+               if (!mono_handle_exception_internal (&ctx_cp, obj, original_ip, TRUE, &first_filter_idx, out_ji)) {
                        if (mono_break_on_exc)
                                G_BREAKPOINT ();
                        // FIXME: This runs managed code so it might cause another stack overflow when
                        // we are handling a stack overflow
                        mono_unhandled_exception (obj);
-
-                       if (mono_debugger_unhandled_exception (original_ip, MONO_CONTEXT_GET_SP (ctx), obj)) {
-                               /*
-                                * If this returns true, then we're running inside the
-                                * Mono Debugger and the debugger wants us to restore the
-                                * context and continue (normally, the debugger inserts
-                                * a breakpoint on the `original_ip', so it regains control
-                                * immediately after restoring the context).
-                                */
-                               MONO_CONTEXT_SET_IP (ctx, original_ip);
-                               restore_context (ctx);
-                               g_assert_not_reached ();
-                       }
                }
        }
 
        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));
@@ -919,6 +978,8 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
                                 */
                                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));
                                }
                        }
 
@@ -965,11 +1026,14 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
                                                }
 
                                                if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
-                                                       // mono_debugger_handle_exception (ei->data.filter, MONO_CONTEXT_GET_SP (ctx), obj);
                                                        if (test_only) {
+                                                               mono_perfcounters->exceptions_filters++;
+                                                               mono_debugger_call_exception_handler (ei->data.filter, MONO_CONTEXT_GET_SP (ctx), 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 {
                                                                /* 
@@ -998,9 +1062,12 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
                                                        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_handle_exception (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), obj);
+                                                       mono_debugger_call_exception_handler (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), 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;
                                                }
@@ -1010,7 +1077,7 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
                                                        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_handle_exception (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), obj);
+                                                       mono_debugger_call_exception_handler (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), obj);
                                                        call_filter (ctx, ei->handler_start);
                                                }
                                                if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) && 
@@ -1019,7 +1086,8 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
                                                        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_handle_exception (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), obj);
+                                                       mono_debugger_call_exception_handler (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), obj);
+                                                       mono_perfcounters->exceptions_finallys++;
                                                        call_filter (ctx, ei->handler_start);
                                                }
                                                
@@ -1056,6 +1124,72 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
        g_assert_not_reached ();
 }
 
+/*
+ * mono_debugger_handle_exception:
+ *
+ *  Notify the debugger about exceptions.  Returns TRUE if the debugger wants us to stop
+ *  at the exception and FALSE to resume with the normal exception handling.
+ *
+ *  The arch code is responsible to setup @ctx in a way that MONO_CONTEXT_GET_IP () and
+ *  MONO_CONTEXT_GET_SP () point to the throw instruction; ie. before executing the
+ *  `callq throw' instruction.
+ */
+gboolean
+mono_debugger_handle_exception (MonoContext *ctx, MonoObject *obj)
+{
+       MonoDebuggerExceptionAction action;
+
+       if (!mono_debug_using_mono_debugger ())
+               return FALSE;
+
+       if (!obj) {
+               MonoException *ex = mono_get_exception_null_reference ();
+               MONO_OBJECT_SETREF (ex, message, mono_string_new (mono_domain_get (), "Object reference not set to an instance of an object"));
+               obj = (MonoObject *)ex;
+       }
+
+       action = _mono_debugger_throw_exception (MONO_CONTEXT_GET_IP (ctx), MONO_CONTEXT_GET_SP (ctx), obj);
+
+       if (action == MONO_DEBUGGER_EXCEPTION_ACTION_STOP) {
+               /*
+                * The debugger wants us to stop on the `throw' instruction.
+                * By the time we get here, it already inserted a breakpoint there.
+                */
+               return TRUE;
+       } else if (action == MONO_DEBUGGER_EXCEPTION_ACTION_STOP_UNHANDLED) {
+               MonoContext ctx_cp = *ctx;
+               MonoJitInfo *ji = NULL;
+               gboolean ret;
+
+               /*
+                * 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, NULL, &ji);
+               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
+                        * inside the method being invoked, so we handle it like a user-unhandled exception.
+                        */
+                       ret = FALSE;
+               }
+
+               if (!ret) {
+                       /*
+                        * The exception is user-unhandled - tell the debugger to stop.
+                        */
+                       return _mono_debugger_unhandled_exception (MONO_CONTEXT_GET_IP (ctx), MONO_CONTEXT_GET_SP (ctx), obj);
+               }
+
+               /*
+                * The exception is catched somewhere - resume with the normal exception handling and don't
+                * stop in the debugger.
+                */
+       }
+
+       return FALSE;
+}
+
 /**
  * mono_debugger_run_finally:
  * @start_ctx: saved processor state
@@ -1107,17 +1241,19 @@ mono_debugger_run_finally (MonoContext *start_ctx)
 gboolean
 mono_handle_exception (MonoContext *ctx, gpointer obj, gpointer original_ip, gboolean test_only)
 {
-       return mono_handle_exception_internal (ctx, obj, original_ip, test_only, NULL);
+       if (!test_only)
+               mono_perfcounters->exceptions_thrown++;
+       return mono_handle_exception_internal (ctx, obj, original_ip, test_only, NULL, NULL);
 }
 
-#endif /* CUSTOM_EXCEPTION_HANDLING */
-
 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
 
 #ifndef MONO_ARCH_USE_SIGACTION
 #error "Can't use sigaltstack without sigaction"
 #endif
 
+#define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
+
 void
 mono_setup_altstack (MonoJitTlsData *tls)
 {
@@ -1137,7 +1273,7 @@ mono_setup_altstack (MonoJitTlsData *tls)
        /*g_print ("thread %p, stack_base: %p, stack_size: %d\n", (gpointer)pthread_self (), staddr, stsize);*/
 
        tls->stack_ovf_guard_base = staddr + mono_pagesize ();
-       tls->stack_ovf_guard_size = mono_pagesize () * 8;
+       tls->stack_ovf_guard_size = ALIGN_TO (8 * 4096, mono_pagesize ());
 
        if (mono_mprotect (tls->stack_ovf_guard_base, tls->stack_ovf_guard_size, MONO_MMAP_NONE)) {
                /* mprotect can fail for the main thread stack */
@@ -1154,7 +1290,7 @@ mono_setup_altstack (MonoJitTlsData *tls)
        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_EXEC|MONO_MMAP_PRIVATE|MONO_MMAP_ANON);
+       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;
 
        g_assert (tls->signal_stack);
@@ -1195,6 +1331,107 @@ mono_free_altstack (MonoJitTlsData *tls)
 
 #endif /* MONO_ARCH_SIGSEGV_ON_ALTSTACK */
 
+static gboolean
+try_restore_stack_protection (MonoJitTlsData *jit_tls, int extra_bytes)
+{
+       gint32 unprotect_size = jit_tls->stack_ovf_guard_size;
+       /* we need to leave some room for throwing the exception */
+       while (unprotect_size >= 0 && (char*)jit_tls->stack_ovf_guard_base + unprotect_size > ((char*)&unprotect_size - extra_bytes))
+               unprotect_size -= mono_pagesize ();
+       /* at this point we could try and build a new domain->stack_overflow_ex, but only if there
+        * is sufficient stack
+        */
+       //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);
+       if (unprotect_size)
+               mono_mprotect (jit_tls->stack_ovf_guard_base, unprotect_size, MONO_MMAP_NONE);
+       return unprotect_size == jit_tls->stack_ovf_guard_size;
+}
+
+static void
+try_more_restore (void)
+{
+       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       if (try_restore_stack_protection (jit_tls, 500))
+               jit_tls->restore_stack_prot = NULL;
+}
+
+static void
+restore_stack_protection (void)
+{
+       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoException *ex = mono_domain_get ()->stack_overflow_ex;
+       /* if we can't restore the stack protection, keep a callback installed so
+        * we'll try to restore as much stack as we can at each return from unmanaged
+        * code.
+        */
+       if (try_restore_stack_protection (jit_tls, 4096))
+               jit_tls->restore_stack_prot = NULL;
+       else
+               jit_tls->restore_stack_prot = try_more_restore_tramp;
+       /* here we also throw a stack overflow exception */
+       ex->trace_ips = NULL;
+       ex->stack_trace = NULL;
+       mono_raise_exception (ex);
+}
+
+gpointer
+mono_altstack_restore_prot (mgreg_t *regs, guint8 *code, gpointer *tramp_data, guint8* tramp)
+{
+       void (*func)(void) = (gpointer)tramp_data;
+       func ();
+       return NULL;
+}
+
+gboolean
+mono_handle_soft_stack_ovf (MonoJitTlsData *jit_tls, MonoJitInfo *ji, void *ctx, guint8* fault_addr)
+{
+       /* we got a stack overflow in the soft-guard pages
+        * There are two cases:
+        * 1) managed code caused the overflow: we unprotect the soft-guard page
+        * and let the arch-specific code trigger the exception handling mechanism
+        * in the thread stack. The soft-guard pages will be protected again as the stack is unwound.
+        * 2) unmanaged code caused the overflow: we unprotect the soft-guard page
+        * and hope we can continue with those enabled, at least until the hard-guard page
+        * is hit. The alternative to continuing here is to just print a message and abort.
+        * We may add in the future the code to protect the pages again in the codepath
+        * when we return from unmanaged to managed code.
+        */
+       if (jit_tls->stack_ovf_guard_size && fault_addr >= (guint8*)jit_tls->stack_ovf_guard_base &&
+                       fault_addr < (guint8*)jit_tls->stack_ovf_guard_base + jit_tls->stack_ovf_guard_size) {
+               /* we unprotect the minimum amount we can */
+               guint32 guard_size;
+               gboolean handled = FALSE;
+
+               guard_size = jit_tls->stack_ovf_guard_size - (mono_pagesize () * SIZEOF_VOID_P / 4);
+               while (guard_size && fault_addr < (guint8*)jit_tls->stack_ovf_guard_base + guard_size) {
+                       guard_size -= mono_pagesize ();
+               }
+               guard_size = jit_tls->stack_ovf_guard_size - guard_size;
+               /*fprintf (stderr, "unprotecting: %d\n", guard_size);*/
+               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);
+#ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
+               if (ji) {
+                       mono_arch_handle_altstack_exception (ctx, fault_addr, TRUE);
+                       handled = TRUE;
+               }
+#endif
+               if (!handled) {
+                       /* We print a message: after this even managed stack overflows
+                        * may crash the runtime
+                        */
+                       fprintf (stderr, "Stack overflow in unmanaged: IP: %p, fault addr: %p\n", mono_arch_ip_from_context (ctx), fault_addr);
+                       if (!jit_tls->handling_stack_ovf) {
+                               jit_tls->restore_stack_prot = restore_stack_protection_tramp;
+                               jit_tls->handling_stack_ovf = 1;
+                       } else {
+                               /*fprintf (stderr, "Already handling stack overflow\n");*/
+                       }
+               }
+               return TRUE;
+       }
+       return FALSE;
+}
+
 static gboolean
 print_stack_frame (MonoMethod *method, gint32 native_offset, gint32 il_offset, gboolean managed, gpointer data)
 {
@@ -1237,20 +1474,31 @@ static gboolean handling_sigsegv = FALSE;
 void
 mono_handle_native_sigsegv (int signal, void *ctx)
 {
-#ifndef PLATFORM_WIN32
+#ifdef MONO_ARCH_USE_SIGACTION
        struct sigaction sa;
 #endif
+       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+
        if (handling_sigsegv)
                return;
 
+       if (mini_get_debug_options ()->suspend_on_sigsegv) {
+               fprintf (stderr, "Received SIGSEGV, suspending...");
+               while (1)
+                       ;
+       }
+
        /* To prevent infinite loops when the stack walk causes a crash */
        handling_sigsegv = TRUE;
 
-       fprintf (stderr, "Stacktrace:\n\n");
+       /* !jit_tls means the thread was not registered with the runtime */
+       if (jit_tls) {
+               fprintf (stderr, "Stacktrace:\n\n");
 
-       mono_jit_walk_stack (print_stack_frame, TRUE, stderr);
+               mono_jit_walk_stack (print_stack_frame, TRUE, stderr);
 
-       fflush (stderr);
+               fflush (stderr);
+       }
 
 #ifdef HAVE_BACKTRACE_SYMBOLS
  {
@@ -1273,13 +1521,11 @@ mono_handle_native_sigsegv (int signal, void *ctx)
        /* Try to get more meaningful information using gdb */
 
 #if !defined(PLATFORM_WIN32) && defined(HAVE_SYS_SYSCALL_H) && defined(SYS_fork)
-       if (!mini_get_debug_options ()->no_gdb_backtrace) {
+       if (!mini_get_debug_options ()->no_gdb_backtrace && !mono_debug_using_mono_debugger ()) {
                /* From g_spawn_command_line_sync () in eglib */
                int res;
                int stdout_pipe [2] = { -1, -1 };
                pid_t pid;
-               const char *argv [16];
-               char buf1 [128];
                int status;
                char buffer [1024];
 
@@ -1291,7 +1537,8 @@ mono_handle_native_sigsegv (int signal, void *ctx)
                 * glibc fork acquires some locks, so if the crash happened inside malloc/free,
                 * it will deadlock. Call the syscall directly instead.
                 */
-               pid = syscall (SYS_fork);
+               pid = mono_runtime_syscall_fork ();
+
                if (pid == 0) {
                        close (stdout_pipe [0]);
                        dup2 (stdout_pipe [1], STDOUT_FILENO);
@@ -1299,23 +1546,9 @@ mono_handle_native_sigsegv (int signal, void *ctx)
                        for (i = getdtablesize () - 1; i >= 3; i--)
                                close (i);
 
-                       argv [0] = g_find_program_in_path ("gdb");
-                       if (argv [0] == NULL) {
+                       if (!mono_gdb_render_native_backtraces ())
                                close (STDOUT_FILENO);
-                               exit (1);
-                       }
 
-                       argv [1] = "-ex";
-                       sprintf (buf1, "attach %ld", (long)getpid ());
-                       argv [2] = buf1;
-                       argv [3] = "--ex";
-                       argv [4] = "info threads";
-                       argv [5] = "--ex";
-                       argv [6] = "thread apply all bt";
-                       argv [7] = "--batch";
-                       argv [8] = 0;
-
-                       execv (argv [0], (char**)argv);
                        exit (1);
                }
 
@@ -1351,7 +1584,7 @@ mono_handle_native_sigsegv (int signal, void *ctx)
  }
 #endif
 
-#ifndef PLATFORM_WIN32
+#ifdef MONO_ARCH_USE_SIGACTION
 
        /* Remove our SIGABRT handler */
        sa.sa_handler = SIG_DFL;
@@ -1379,7 +1612,7 @@ mono_print_thread_dump (void *sigctx)
        MonoContext ctx;
 #endif
        GString* text = g_string_new (0);
-       char *name;
+       char *name, *wapi_desc;
        GError *error = NULL;
 
        if (thread->name) {
@@ -1388,13 +1621,18 @@ mono_print_thread_dump (void *sigctx)
                g_string_append_printf (text, "\n\"%s\"", name);
                g_free (name);
        }
+       else if (thread->threadpool_thread)
+               g_string_append (text, "\n\"<threadpool thread>\"");
        else
-               g_string_append (text, "\n\"\"");
+               g_string_append (text, "\n\"<unnamed thread>\"");
 
-       g_string_append_printf (text, " tid=0x%p this=0x%p:\n", (gpointer)(gsize)thread->tid, thread);
+#ifndef PLATFORM_WIN32
+       wapi_desc = wapi_current_thread_desc ();
+       g_string_append_printf (text, " tid=0x%p this=0x%p %s\n", (gpointer)(gsize)thread->tid, thread,  wapi_desc);
+       free (wapi_desc);
+#endif
 
-       /* FIXME: */
-#if defined(__i386__) || defined(__x86_64__)
+#ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
        if (!sigctx)
                MONO_INIT_CONTEXT_FROM_FUNC (&ctx, mono_print_thread_dump);
        else