Fix the DISABLE_JIT build.
[mono.git] / mono / mini / mini-exceptions.c
index 6e4711a0737d17bdaf04f6ff6e5fb501718de6ef..6cb5c2ac5178ae5556b13f60059378dc419661a3 100644 (file)
@@ -6,7 +6,8 @@
  *   Mono Team (mono-list@lists.ximian.com)
  *
  * Copyright 2001-2003 Ximian, Inc.
- * Copyright 2003-2008 Ximian, Inc.
+ * Copyright 2003-2008 Novell, Inc.
+ * Copyright 2011 Xamarin Inc (http://www.xamarin.com).
  */
 
 #include <config.h>
@@ -44,7 +45,9 @@
 #include <mono/metadata/mono-debug.h>
 #include <mono/metadata/profiler.h>
 #include <mono/metadata/mono-endian.h>
+#include <mono/metadata/environment.h>
 #include <mono/utils/mono-mmap.h>
+#include <mono/utils/mono-logger-internal.h>
 
 #include "mini.h"
 #include "debug-mini.h"
@@ -62,6 +65,9 @@ static gpointer throw_corlib_exception_func;
 static gpointer try_more_restore_tramp = NULL;
 static gpointer restore_stack_protection_tramp = NULL;
 
+static MonoUnhandledExceptionFunc unhandled_exception_hook = NULL;
+static gpointer unhandled_exception_hook_data = NULL;
+
 static void try_more_restore (void);
 static void restore_stack_protection (void);
 static void mono_walk_stack_full (MonoJitStackWalk func, MonoContext *start_ctx, MonoDomain *domain, MonoJitTlsData *jit_tls, MonoLMF *lmf, MonoUnwindOptions unwind_options, gpointer user_data);
@@ -432,11 +438,36 @@ get_generic_info_from_stack_frame (MonoJitInfo *ji, MonoContext *ctx)
        if (!gi->has_this)
                return NULL;
 
-       if (gi->this_in_reg)
-               info = (gpointer)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);
+       info = NULL;
+       /*
+        * Search location list if available, it contains the precise location of the
+        * argument for every pc offset, even if the method was interrupted while it was in
+        * its prolog.
+        */
+       if (gi->nlocs) {
+               int offset = (mgreg_t)MONO_CONTEXT_GET_IP (ctx) - (mgreg_t)ji->code_start;
+               int i;
+
+               for (i = 0; i < gi->nlocs; ++i) {
+                       MonoDwarfLocListEntry *entry = &gi->locations [i];
+
+                       if (offset >= entry->from && (offset < entry->to || entry->to == 0)) {
+                               if (entry->is_reg)
+                                       info = (gpointer)mono_arch_context_get_int_reg (ctx, entry->reg);
+                               else
+                                       info = *(gpointer*)(gpointer)((char*)mono_arch_context_get_int_reg (ctx, entry->reg) + entry->offset);
+                               break;
+                       }
+               }
+               g_assert (i < gi->nlocs);
+       } else {
+               if (gi->this_in_reg)
+                       info = (gpointer)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) {
@@ -511,6 +542,47 @@ get_method_from_stack_frame (MonoJitInfo *ji, gpointer generic_info)
        return method;
 }
 
+/**
+ * mono_exception_walk_native_trace:
+ * @ex: The exception object whose frames should be walked
+ * @func: callback to call for each stack frame
+ * @user_data: data passed to the callback
+ *
+ * This function walks the stacktrace of an exception. For
+ * each frame the callback function is called with the relevant info.
+ * The walk ends when no more stack frames are found or when the callback
+ * returns a TRUE value.
+ */
+gboolean
+mono_exception_walk_trace (MonoException *ex, MonoExceptionFrameWalk func, gpointer user_data)
+{
+       MonoDomain *domain = mono_domain_get ();
+       MonoArray *ta = ex->trace_ips;
+       int len, i;
+
+       if (ta == NULL)
+               return FALSE;
+
+       len = mono_array_length (ta) >> 1;
+       for (i = 0; i < len; i++) {
+               gpointer ip = mono_array_get (ta, gpointer, i * 2 + 0);
+               gpointer generic_info = mono_array_get (ta, gpointer, i * 2 + 1);
+               MonoJitInfo *ji = mono_jit_info_table_find (domain, ip);
+
+               if (ji == NULL) {
+                       if (func (NULL, ip, 0, FALSE, user_data))
+                               return TRUE;
+               } else {
+                       MonoMethod *method = get_method_from_stack_frame (ji, generic_info);
+                       if (func (method, ji->code_start, (char *) ip - (char *) ji->code_start, TRUE, user_data))
+                               return TRUE;
+               }
+       }
+       
+       return len > 0;
+}
+
 MonoString *
 ves_icall_System_Exception_get_trace (MonoException *ex)
 {
@@ -635,7 +707,7 @@ static void
 mono_runtime_walk_stack_with_ctx (MonoJitStackWalk func, MonoContext *start_ctx, MonoUnwindOptions unwind_options, void *user_data)
 {
        if (!start_ctx) {
-               MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+               MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
                if (jit_tls && jit_tls->orig_ex_ctx_set)
                        start_ctx = &jit_tls->orig_ex_ctx;
        }
@@ -793,7 +865,7 @@ ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
                          MonoString **file, gint32 *line, gint32 *column)
 {
        MonoDomain *domain = mono_domain_get ();
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        MonoLMF *lmf = mono_get_lmf ();
        MonoJitInfo *ji = NULL;
        MonoContext ctx, new_ctx;
@@ -825,15 +897,9 @@ ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
                ji = frame.ji;
                *native_offset = frame.native_offset;
 
-               /* skip all wrappers ??*/
-               if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
-                   ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE ||
-                   ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH ||
-                   ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
-                   ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE ||
-                       ji->method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED)
+               /* The skip count passed by the caller depends on us not filtering out MANAGED_TO_NATIVE */
+               if (ji->method->wrapper_type != MONO_WRAPPER_NONE && ji->method->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD && ji->method->wrapper_type != MONO_WRAPPER_MANAGED_TO_NATIVE)
                        continue;
-
                skip--;
        } while (skip >= 0);
 
@@ -1170,6 +1236,41 @@ wrap_non_exception_throws (MonoMethod *m)
 #define DOES_STACK_GROWS_UP 0
 #endif
 
+#define MAX_UNMANAGED_BACKTRACE 128
+static MonoArray*
+build_native_trace (void)
+{
+/* This puppy only makes sense on mobile, IOW, ARM. */
+#if defined (HAVE_BACKTRACE_SYMBOLS) && defined (TARGET_ARM)
+       MonoArray *res;
+       void *native_trace [MAX_UNMANAGED_BACKTRACE];
+       int size = backtrace (native_trace, MAX_UNMANAGED_BACKTRACE);
+       int i;
+
+       if (!size)
+               return NULL;
+       res = mono_array_new (mono_domain_get (), mono_defaults.int_class, size);
+
+       for (i = 0; i < size; i++)
+               mono_array_set (res, gpointer, i, native_trace [i]);
+       return res;
+#else
+       return NULL;
+#endif
+}
+
+#define setup_managed_stacktrace_information() do {    \
+       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));   \
+               MONO_OBJECT_SETREF (mono_ex, native_trace_ips, build_native_trace ());  \
+               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;       \
+} while (0)
 /*
  * mono_handle_exception_internal_first_pass:
  *
@@ -1178,12 +1279,12 @@ wrap_non_exception_throws (MonoMethod *m)
  * 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)
+mono_handle_exception_internal_first_pass (MonoContext *ctx, gpointer obj, 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);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        MonoLMF *lmf = mono_get_lmf ();
        MonoArray *initial_trace_ips = NULL;
        GList *trace_ips = NULL;
@@ -1243,14 +1344,7 @@ mono_handle_exception_internal_first_pass (MonoContext *ctx, gpointer obj, gpoin
                }
 
                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);
+                       setup_managed_stacktrace_information ();
                        return FALSE;
                }
 
@@ -1313,18 +1407,20 @@ mono_handle_exception_internal_first_pass (MonoContext *ctx, gpointer obj, gpoin
                                        ex_obj = obj;
 
                                if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
+                                       gboolean is_user_frame = ji->method->wrapper_type == MONO_WRAPPER_NONE || ji->method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD;
+#ifndef DISABLE_PERFCOUNTERS
                                        mono_perfcounters->exceptions_filters++;
+#endif
                                        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;
+                                       /*
+                                       Here's the thing, if this is a filter clause done by a wrapper like runtime invoke, we don't want to
+                                       trim the stackframe since if it returns FALSE we lose information.
+
+                                       FIXME Not 100% sure if it's a good idea even with user clauses.
+                                       */
+                                       if (is_user_frame)
+                                               setup_managed_stacktrace_information ();
 
                                        if (ji->from_llvm) {
 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
@@ -1347,6 +1443,8 @@ mono_handle_exception_internal_first_pass (MonoContext *ctx, gpointer obj, gpoin
                                        filter_idx ++;
 
                                        if (filtered) {
+                                               if (!is_user_frame)
+                                                       setup_managed_stacktrace_information ();
                                                /* mono_debugger_agent_handle_exception () needs this */
                                                MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
                                                return TRUE;
@@ -1354,14 +1452,7 @@ mono_handle_exception_internal_first_pass (MonoContext *ctx, gpointer obj, gpoin
                                }
 
                                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);
+                                       setup_managed_stacktrace_information ();
 
                                        if (out_ji)
                                                *out_ji = ji;
@@ -1386,13 +1477,13 @@ mono_handle_exception_internal_first_pass (MonoContext *ctx, gpointer obj, gpoin
  * @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 resume, MonoJitInfo **out_ji)
+mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gboolean resume, MonoJitInfo **out_ji)
 {
        MonoDomain *domain = mono_domain_get ();
        MonoJitInfo *ji;
        static int (*call_filter) (MonoContext *, gpointer) = NULL;
        static void (*restore_context) (void *);
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        MonoLMF *lmf = mono_get_lmf ();
        MonoException *mono_ex;
        gboolean stack_overflow = FALSE;
@@ -1505,15 +1596,15 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
                mono_profiler_exception_thrown (obj);
                jit_tls->orig_ex_ctx_set = FALSE;
 
-               res = mono_handle_exception_internal_first_pass (&ctx_cp, obj, original_ip, &first_filter_idx, &ji, non_exception);
+               res = mono_handle_exception_internal_first_pass (&ctx_cp, obj, &first_filter_idx, &ji, non_exception);
 
                if (!res) {
-                       if (mono_break_on_exc)
+                       if (mini_get_debug_options ()->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...");
+                               mono_runtime_printf_err ("Unhandled exception, suspending...");
                                while (1)
                                        ;
                        }
@@ -1676,7 +1767,9 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
                                        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;
+#ifndef DISABLE_PERFCOUNTERS
                                        mono_perfcounters->exceptions_depth += frame_count;
+#endif
                                        if (obj == domain->stack_overflow_ex)
                                                jit_tls->handling_stack_ovf = FALSE;
 
@@ -1700,7 +1793,9 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
                                        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);
+#ifndef DISABLE_PERFCOUNTERS
                                        mono_perfcounters->exceptions_finallys++;
+#endif
                                        *(mono_get_lmf_addr ()) = lmf;
                                        if (ji->from_llvm) {
                                                /* 
@@ -1779,7 +1874,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_first_pass (&ctx_cp, obj, MONO_CONTEXT_GET_IP (ctx), NULL, &ji, NULL);
+               ret = mono_handle_exception_internal_first_pass (&ctx_cp, obj, 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
@@ -1820,7 +1915,7 @@ mono_debugger_run_finally (MonoContext *start_ctx)
 {
        static int (*call_filter) (MonoContext *, gpointer) = NULL;
        MonoDomain *domain = mono_domain_get ();
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        MonoLMF *lmf = mono_get_lmf ();
        MonoContext ctx, new_ctx;
        MonoJitInfo *ji, rji;
@@ -1849,16 +1944,15 @@ mono_debugger_run_finally (MonoContext *start_ctx)
  * mono_handle_exception:
  * @ctx: saved processor state
  * @obj: the exception object
- * @test_only: only test if the exception is caught, but dont call handlers
  */
 gboolean
-mono_handle_exception (MonoContext *ctx, gpointer obj, gpointer original_ip, gboolean test_only)
+mono_handle_exception (MonoContext *ctx, gpointer obj)
 {
-       if (!test_only)
-               mono_perfcounters->exceptions_thrown++;
+#ifndef DISABLE_PERFCOUNTERS
+       mono_perfcounters->exceptions_thrown++;
+#endif
 
-       g_assert (!test_only);
-       return mono_handle_exception_internal (ctx, obj, original_ip, FALSE, NULL);
+       return mono_handle_exception_internal (ctx, obj, FALSE, NULL);
 }
 
 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
@@ -1873,7 +1967,7 @@ void
 mono_setup_altstack (MonoJitTlsData *tls)
 {
        size_t stsize = 0;
-       struct sigaltstack sa;
+       stack_t sa;
        guint8 *staddr = NULL;
 
        if (mono_running_on_valgrind ())
@@ -1908,14 +2002,20 @@ mono_setup_altstack (MonoJitTlsData *tls)
 
        sa.ss_sp = tls->signal_stack;
        sa.ss_size = MONO_ARCH_SIGNAL_STACK_SIZE;
+#if __APPLE__
+       sa.ss_flags = 0;
+#else
        sa.ss_flags = SS_ONSTACK;
-       sigaltstack (&sa, NULL);
+#endif
+       g_assert (sigaltstack (&sa, NULL) == 0);
+
+       mono_gc_register_altstack ((char*)tls->stack_ovf_guard_base + tls->stack_ovf_guard_size, (char*)staddr + stsize - ((char*)tls->stack_ovf_guard_base + tls->stack_ovf_guard_size), tls->signal_stack, tls->signal_stack_size);
 }
 
 void
 mono_free_altstack (MonoJitTlsData *tls)
 {
-       struct sigaltstack sa;
+       stack_t sa;
        int err;
 
        sa.ss_sp = tls->signal_stack;
@@ -1962,18 +2062,18 @@ try_restore_stack_protection (MonoJitTlsData *jit_tls, int extra_bytes)
        return unprotect_size == jit_tls->stack_ovf_guard_size;
 }
 
-static void
+static G_GNUC_UNUSED void
 try_more_restore (void)
 {
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        if (try_restore_stack_protection (jit_tls, 500))
                jit_tls->restore_stack_prot = NULL;
 }
 
-static void
+static G_GNUC_UNUSED void
 restore_stack_protection (void)
 {
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (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
@@ -2034,7 +2134,7 @@ mono_handle_soft_stack_ovf (MonoJitTlsData *jit_tls, MonoJitInfo *ji, void *ctx,
                        /* 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);
+                       mono_runtime_printf_err ("Stack overflow in unmanaged: IP: %p, fault addr: %p", 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;
@@ -2048,7 +2148,6 @@ mono_handle_soft_stack_ovf (MonoJitTlsData *jit_tls, MonoJitInfo *ji, void *ctx,
 }
 
 typedef struct {
-       FILE *stream;
        MonoMethod *omethod;
        int count;
 } PrintOverflowUserData;
@@ -2058,7 +2157,6 @@ print_overflow_stack_frame (StackFrameInfo *frame, MonoContext *ctx, gpointer da
 {
        MonoMethod *method = NULL;
        PrintOverflowUserData *user_data = data;
-       FILE *stream = user_data->stream;
        gchar *location;
 
        if (frame->ji)
@@ -2076,11 +2174,11 @@ print_overflow_stack_frame (StackFrameInfo *frame, MonoContext *ctx, gpointer da
                        return FALSE;
 
                location = mono_debug_print_stack_frame (method, frame->native_offset, mono_domain_get ());
-               fprintf (stream, "  %s\n", location);
+               mono_runtime_printf_err ("  %s", location);
                g_free (location);
 
                if (user_data->count == 1) {
-                       fprintf (stream, "  <...>\n");
+                       mono_runtime_printf_err ("  <...>");
                        user_data->omethod = method;
                } else {
                        user_data->omethod = NULL;
@@ -2088,7 +2186,7 @@ print_overflow_stack_frame (StackFrameInfo *frame, MonoContext *ctx, gpointer da
 
                user_data->count ++;
        } else
-               fprintf (stream, "  at <unknown> <0x%05x>\n", frame->native_offset);
+               mono_runtime_printf_err ("  at <unknown> <0x%05x>", frame->native_offset);
 
        return FALSE;
 }
@@ -2100,41 +2198,39 @@ mono_handle_hard_stack_ovf (MonoJitTlsData *jit_tls, MonoJitInfo *ji, void *ctx,
        MonoContext mctx;
 
        /* 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);
+       mono_runtime_printf_err ("Stack overflow: IP: %p, fault addr: %p", mono_arch_ip_from_context (ctx), fault_addr);
 
 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
        mono_arch_sigctx_to_monoctx (ctx, &mctx);
                        
-       fprintf (stderr, "Stacktrace:\n");
+       mono_runtime_printf_err ("Stacktrace:");
 
        memset (&ud, 0, sizeof (ud));
-       ud.stream = stderr;
 
        mono_walk_stack_with_ctx (print_overflow_stack_frame, &mctx, MONO_UNWIND_LOOKUP_ACTUAL_METHOD, &ud);
 #else
        if (ji && ji->method)
-               fprintf (stderr, "At %s\n", mono_method_full_name (ji->method, TRUE));
+               mono_runtime_printf_err ("At %s", mono_method_full_name (ji->method, TRUE));
        else
-               fprintf (stderr, "At <unmanaged>.\n");
+               mono_runtime_printf_err ("At <unmanaged>.");
 #endif
 
        _exit (1);
 }
 
 static gboolean
-print_stack_frame (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
+print_stack_frame_to_stderr (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
 {
-       FILE *stream = (FILE*)data;
        MonoMethod *method = NULL;
        if (frame->ji)
                method = frame->ji->method;
 
        if (method) {
                gchar *location = mono_debug_print_stack_frame (method, frame->native_offset, mono_domain_get ());
-               fprintf (stream, "  %s\n", location);
+               mono_runtime_printf_err ("  %s", location);
                g_free (location);
        } else
-               fprintf (stream, "  at <unknown> <0x%05x>\n", frame->native_offset);
+               mono_runtime_printf_err ("  at <unknown> <0x%05x>", frame->native_offset);
 
        return FALSE;
 }
@@ -2171,13 +2267,13 @@ mono_handle_native_sigsegv (int signal, void *ctx)
 #ifdef MONO_ARCH_USE_SIGACTION
        struct sigaction sa;
 #endif
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
 
        if (handling_sigsegv)
                return;
 
        if (mini_get_debug_options ()->suspend_on_sigsegv) {
-               fprintf (stderr, "Received SIGSEGV, suspending...");
+               mono_runtime_printf_err ("Received SIGSEGV, suspending...");
                while (1)
                        ;
        }
@@ -2187,11 +2283,9 @@ mono_handle_native_sigsegv (int signal, void *ctx)
 
        /* !jit_tls means the thread was not registered with the runtime */
        if (jit_tls && mono_thread_internal_current ()) {
-               fprintf (stderr, "Stacktrace:\n\n");
-
-               mono_walk_stack (print_stack_frame, TRUE, stderr);
+               mono_runtime_printf_err ("Stacktrace:\n");
 
-               fflush (stderr);
+               mono_walk_stack (print_stack_frame_to_stderr, TRUE, NULL);
        }
 
 #ifdef HAVE_BACKTRACE_SYMBOLS
@@ -2201,31 +2295,24 @@ mono_handle_native_sigsegv (int signal, void *ctx)
        int i, size;
        const char *signal_str = (signal == SIGSEGV) ? "SIGSEGV" : "SIGABRT";
 
-       fprintf (stderr, "\nNative stacktrace:\n\n");
+       mono_runtime_printf_err ("\nNative stacktrace:\n");
 
        size = backtrace (array, 256);
        names = backtrace_symbols (array, size);
        for (i =0; i < size; ++i) {
-               fprintf (stderr, "\t%s\n", names [i]);
+               mono_runtime_printf_err ("\t%s", names [i]);
        }
        free (names);
 
-       fflush (stderr);
-
        /* Try to get more meaningful information using gdb */
 
 #if !defined(HOST_WIN32) && defined(HAVE_SYS_SYSCALL_H) && defined(SYS_fork)
        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;
                int status;
-               char buffer [1024];
+               pid_t crashed_pid = getpid ();
 
-               res = pipe (stdout_pipe);
-               g_assert (res != -1);
-                       
                //pid = fork ();
                /*
                 * glibc fork acquires some locks, so if the crash happened inside malloc/free,
@@ -2234,31 +2321,14 @@ mono_handle_native_sigsegv (int signal, void *ctx)
                pid = mono_runtime_syscall_fork ();
 
                if (pid == 0) {
-                       close (stdout_pipe [0]);
-                       dup2 (stdout_pipe [1], STDOUT_FILENO);
-
-                       for (i = getdtablesize () - 1; i >= 3; i--)
-                               close (i);
-
-                       if (!mono_gdb_render_native_backtraces ())
-                               close (STDOUT_FILENO);
+                       dup2 (STDERR_FILENO, STDOUT_FILENO);
 
+                       mono_gdb_render_native_backtraces (crashed_pid);
                        exit (1);
                }
 
-               close (stdout_pipe [1]);
-
-               fprintf (stderr, "\nDebug info from gdb:\n\n");
-
-               while (1) {
-                       int nread = read (stdout_pipe [0], buffer, 1024);
-
-                       if (nread <= 0)
-                               break;
-                       write (STDERR_FILENO, buffer, nread);
-               }               
-
-               waitpid (pid, &status, WNOHANG);
+               mono_runtime_printf_err ("\nDebug info from gdb:\n");
+               waitpid (pid, &status, 0);
        }
 #endif
        /*
@@ -2266,14 +2336,14 @@ mono_handle_native_sigsegv (int signal, void *ctx)
         * on anything working. So try to print out lots of diagnostics, starting 
         * with ones which have a greater chance of working.
         */
-       fprintf (stderr,
+       mono_runtime_printf_err (
                         "\n"
                         "=================================================================\n"
                         "Got a %s while executing native code. This usually indicates\n"
                         "a fatal error in the mono runtime or one of the native libraries \n"
                         "used by your application.\n"
-                        "=================================================================\n"
-                        "\n", signal_str);
+                        "=================================================================\n",
+                       signal_str);
 
  }
 #endif
@@ -2330,17 +2400,17 @@ mono_print_thread_dump_internal (void *sigctx, MonoContext *start_ctx)
 
        mono_walk_stack_with_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");
+       mono_runtime_printf ("\t<Stack traces in thread dumps not supported on this platform>");
 #endif
 
-       fprintf (stdout, "%s", text->str);
+       mono_runtime_printf ("%s", text->str);
 
 #if PLATFORM_WIN32 && TARGET_WIN32 && _DEBUG
        OutputDebugStringA(text->str);
 #endif
 
        g_string_free (text, TRUE);
-       fflush (stdout);
+       mono_runtime_stdout_fflush ();
 }
 
 /*
@@ -2370,7 +2440,7 @@ mono_print_thread_dump_from_ctx (MonoContext *ctx)
 void
 mono_resume_unwind (MonoContext *ctx)
 {
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        static void (*restore_context) (MonoContext *);
        MonoContext new_ctx;
 
@@ -2378,7 +2448,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, TRUE, NULL);
+       mono_handle_exception_internal (&new_ctx, jit_tls->resume_state.ex_obj, TRUE, NULL);
 
        if (!restore_context)
                restore_context = mono_get_restore_context ();
@@ -2508,7 +2578,7 @@ 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 = mono_native_tls_get_value (mono_jit_tls_id);
                jit_tls->class_cast_from = from;
                jit_tls->class_cast_to = to;
        }
@@ -2529,7 +2599,7 @@ mono_thread_state_init_from_sigctx (MonoThreadUnwindState *ctx, void *sigctx)
        if (sigctx)
                mono_arch_sigctx_to_monoctx (sigctx, &ctx->ctx);
        else
-#if MONO_ARCH_HAS_MONO_CONTEXT
+#if MONO_ARCH_HAS_MONO_CONTEXT && !defined(MONO_CROSS_COMPILE)
                MONO_CONTEXT_GET_CURRENT (ctx->ctx);
 #else
                g_error ("Use a null sigctx requires a working mono-context");
@@ -2595,7 +2665,7 @@ mono_raise_exception_with_ctx (MonoException *exc, MonoContext *ctx)
        void (*restore_context) (MonoContext *);
        restore_context = mono_get_restore_context ();
 
-       mono_handle_exception (ctx, exc, NULL, FALSE);
+       mono_handle_exception (ctx, exc);
        restore_context (ctx);
 }
 
@@ -2604,7 +2674,7 @@ void
 mono_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
 {
 #ifdef MONO_ARCH_HAVE_SETUP_ASYNC_CALLBACK
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        jit_tls->ex_ctx = *ctx;
 
        mono_arch_setup_async_callback (ctx, async_cb, user_data);
@@ -2612,3 +2682,34 @@ mono_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpoint
        g_error ("This target doesn't support mono_arch_setup_async_callback");
 #endif
 }
+
+void
+mono_install_unhandled_exception_hook (MonoUnhandledExceptionFunc func, gpointer user_data)
+{
+       unhandled_exception_hook = func;
+       unhandled_exception_hook_data = user_data;
+}
+
+void
+mono_invoke_unhandled_exception_hook (MonoObject *exc)
+{
+       if (unhandled_exception_hook) {
+               unhandled_exception_hook (exc, unhandled_exception_hook_data);
+       } else {
+               MonoObject *other = NULL;
+               MonoString *str = mono_object_to_string (exc, &other);
+               if (str) {
+                       char *msg = mono_string_to_utf8 (str);
+                       mono_runtime_printf_err ("[ERROR] FATAL UNHANDLED EXCEPTION: %s", msg);
+                       g_free (msg);
+               }
+
+#if defined(__APPLE__) && defined(__arm__)
+               g_assertion_message ("Terminating runtime due to unhandled exception");
+#else
+               exit (mono_environment_exitcode_get ());
+#endif
+       }
+
+       g_assert_not_reached ();
+}