2010-02-10 Geoff Norton <gnorton@novell.com>
[mono.git] / mono / mini / mini-exceptions.c
index 3f6f884fbabb9c2f77a0bdee800a78be5d0032ef..c0fc622058afb5c708057c3e5787ebf0d4544e91 100644 (file)
@@ -48,6 +48,7 @@
 #include "mini.h"
 #include "debug-mini.h"
 #include "trace.h"
+#include "debugger-agent.h"
 
 #ifndef MONO_ARCH_CONTEXT_DEF
 #define MONO_ARCH_CONTEXT_DEF
@@ -128,6 +129,8 @@ mono_get_restore_context (void)
 gpointer
 mono_get_throw_exception_by_name (void)
 {
+#ifdef MONO_ARCH_HAVE_THROW_EXCEPTION_BY_NAME
+
        gpointer code = NULL;
 #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
        guint32 code_size;
@@ -151,6 +154,13 @@ mono_get_throw_exception_by_name (void)
 
        throw_exception_by_name_func = code;
 
+#else
+
+       throw_exception_by_name_func = NULL;
+
+       g_assert_not_reached ();
+#endif
+
        return throw_exception_by_name_func;
 }
 
@@ -187,6 +197,69 @@ mono_get_throw_corlib_exception (void)
        return throw_corlib_exception_func;
 }
 
+#ifdef MONO_ARCH_HAVE_FIND_JIT_INFO_EXT
+
+/*
+ * find_jit_info_no_ext:
+ *
+ * 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.
+ */
+static MonoJitInfo *
+find_jit_info_no_ext (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx, 
+                          MonoContext *new_ctx, MonoLMF **lmf, gboolean *managed)
+{
+       StackFrameInfo frame;
+       MonoJitInfo *ji;
+       gboolean err;
+       gpointer ip = MONO_CONTEXT_GET_IP (ctx);
+
+       /* Avoid costly table lookup during stack overflow */
+       if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
+               ji = prev_ji;
+       else
+               ji = mini_jit_info_table_find (domain, ip, NULL);
+
+       if (managed)
+               *managed = FALSE;
+
+       err = mono_arch_find_jit_info_ext (domain, jit_tls, ji, ctx, new_ctx, lmf, &frame);
+       if (!err)
+               return (gpointer)-1;
+
+       /* Convert between the new and the old APIs */
+       switch (frame.type) {
+       case FRAME_TYPE_MANAGED:
+               if (managed)
+                       *managed = TRUE;
+               return ji;
+       case FRAME_TYPE_MANAGED_TO_NATIVE:
+               if (frame.ji)
+                       return frame.ji;
+               else {
+                       memset (res, 0, sizeof (MonoJitInfo));
+                       res->method = frame.method;
+                       return res;
+               }
+       case FRAME_TYPE_DEBUGGER_INVOKE: {
+               MonoContext tmp_ctx;
+
+               /*
+                * 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);
+               memcpy (new_ctx, &tmp_ctx, sizeof (MonoContext));
+               return ji;
+       }
+       default:
+               g_assert_not_reached ();
+               return NULL;
+       }
+}
+
+#endif
+
 /* mono_find_jit_info:
  *
  * This function is used to gather information from @ctx. It return the 
@@ -214,7 +287,11 @@ 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
 
        if (ji == (gpointer)-1)
                return ji;
@@ -255,6 +332,76 @@ 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.
+ */
+gboolean
+mono_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls, 
+                                               MonoJitInfo *prev_ji, MonoContext *ctx,
+                                               MonoContext *new_ctx, char **trace, MonoLMF **lmf,
+                                               StackFrameInfo *frame)
+{
+       gboolean err;
+       gpointer ip = MONO_CONTEXT_GET_IP (ctx);
+       MonoJitInfo *ji;
+       MonoDomain *target_domain;
+
+       if (trace)
+               *trace = NULL;
+
+       /* Avoid costly table lookup during stack overflow */
+       if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
+               ji = prev_ji;
+       else
+               ji = mini_jit_info_table_find (domain, ip, &target_domain);
+
+       if (!target_domain)
+               target_domain = domain;
+
+       err = mono_arch_find_jit_info_ext (target_domain, jit_tls, ji, ctx, new_ctx, lmf, frame);
+       if (!err)
+               return FALSE;
+
+       frame->native_offset = -1;
+       frame->domain = target_domain;
+
+       ji = frame->ji;
+
+       if (ji && (frame->managed || ji->method->wrapper_type)) {
+               const char *real_ip, *start;
+
+               start = (const char *)ji->code_start;
+               if (!frame->managed)
+                       /* ctx->ip points into native code */
+                       real_ip = (const char*)MONO_CONTEXT_GET_IP (new_ctx);
+               else
+                       real_ip = (const char*)ip;
+
+               if ((real_ip >= start) && (real_ip <= start + ji->code_size))
+                       frame->native_offset = real_ip - start;
+               else
+                       frame->native_offset = -1;
+
+               if (trace)
+                       *trace = mono_debug_print_stack_frame (ji->method, frame->native_offset, domain);
+       } else {
+               if (trace && frame->method) {
+                       char *fname = mono_method_full_name (frame->method, TRUE);
+                       *trace = g_strdup_printf ("in (unmanaged) %s", fname);
+                       g_free (fname);
+               }
+       }
+
+       return TRUE;
+}
+
+#endif /* MONO_ARCH_HAVE_FIND_JIT_INFO_EXT */
+
 static gpointer
 get_generic_info_from_stack_frame (MonoJitInfo *ji, MonoContext *ctx)
 {
@@ -562,6 +709,73 @@ mono_jit_walk_stack (MonoStackWalk func, gboolean do_il_offset, gpointer user_da
        mono_jit_walk_stack_from_ctx (func, NULL, do_il_offset, user_data);
 }
 
+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)
+{
+       MonoJitTlsData *jit_tls = thread->jit_data;
+       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 (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
+               g_assert (thread == mono_thread_internal_current ());
+       }
+
+       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);
+               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) {
+                       MonoDebugSourceLocation *source;
+
+                       source = mono_debug_lookup_source_location (frame.ji->method, frame.native_offset, domain);
+                       il_offset = source ? source->il_offset : -1;
+                       mono_debug_free_source_location (source);
+               } else
+                       il_offset = -1;
+
+               frame.il_offset = il_offset;
+
+               if (func (&frame, &ctx, user_data))
+                       return;
+               
+               ctx = new_ctx;
+       }
+}
+
+
 MonoBoolean
 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info, 
                          MonoReflectionMethod **method, 
@@ -875,6 +1089,16 @@ mini_jit_info_table_find (MonoDomain *domain, char *addr, MonoDomain **out_domai
                return ji;
        }
 
+       /* maybe it is shared code, so we also search in the root domain */
+       if (domain != mono_get_root_domain ()) {
+               ji = mono_jit_info_table_find (mono_get_root_domain (), addr);
+               if (ji) {
+                       if (out_domain)
+                               *out_domain = mono_get_root_domain ();
+                       return ji;
+               }
+       }
+
        for (l = t->appdomain_refs; l; l = l->next) {
                if (l->data != domain) {
                        ji = mono_jit_info_table_find ((MonoDomain*)l->data, addr);
@@ -896,9 +1120,10 @@ mini_jit_info_table_find (MonoDomain *domain, char *addr, MonoDomain **out_domai
  * @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, gint32 *out_filter_idx, MonoJitInfo **out_ji)
+mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer original_ip, gboolean test_only, gboolean resume, gint32 *out_filter_idx, MonoJitInfo **out_ji)
 {
        MonoDomain *domain = mono_domain_get ();
        MonoJitInfo *ji, rji;
@@ -922,6 +1147,9 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
                obj = (MonoObject *)ex;
        } 
 
+       if (!test_only)
+               mono_debugger_agent_handle_exception (obj, ctx);
+
        /*
         * Allocate a new exception object instead of the preconstructed ones.
         */
@@ -967,11 +1195,12 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
                if (mono_trace_is_enabled ())
                        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, out_ji)) {
+               if (!mono_handle_exception_internal (&ctx_cp, obj, original_ip, TRUE, FALSE, &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_debugger_agent_handle_unhandled_exception (obj, ctx);
                        mono_unhandled_exception (obj);
                }
        }
@@ -1059,8 +1288,16 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
                                                MonoClass *catch_class = get_exception_catch_class (ei, ji, ctx);
 
                                                if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)) {
-                                                       /* store the exception object in bp + ei->exvar_offset */
-                                                       *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = obj;
+                                                       if (ji->from_llvm) {
+#ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
+                                                               MONO_CONTEXT_SET_LLVM_EXC_REG (ctx, 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)) = obj;
+                                                       }
                                                }
 
                                                if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
@@ -1127,7 +1364,23 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
                                                        mono_debugger_call_exception_handler (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), obj);
                                                        mono_perfcounters->exceptions_finallys++;
                                                        *(mono_get_lmf_addr ()) = lmf;
-                                                       call_filter (ctx, ei->handler_start);
+                                                       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.
+                                                                */
+                                                               MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
+                                                               *(mono_get_lmf_addr ()) = lmf;
+                                                               jit_tls->ex_ctx = new_ctx;
+                                                               jit_tls->ex_obj = obj;
+                                                               return 0;
+                                                       } else {
+                                                               call_filter (ctx, ei->handler_start);
+                                                       }
                                                }
                                                
                                        }
@@ -1204,7 +1457,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, NULL, &ji);
+               ret = mono_handle_exception_internal (&ctx_cp, obj, MONO_CONTEXT_GET_IP (ctx), TRUE, FALSE, 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
@@ -1282,7 +1535,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, NULL, NULL);
+
+       return mono_handle_exception_internal (ctx, obj, original_ip, test_only, FALSE, NULL, NULL);
 }
 
 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
@@ -1531,7 +1785,7 @@ mono_handle_native_sigsegv (int signal, void *ctx)
        handling_sigsegv = TRUE;
 
        /* !jit_tls means the thread was not registered with the runtime */
-       if (jit_tls) {
+       if (jit_tls && mono_thread_internal_current ()) {
                fprintf (stderr, "Stacktrace:\n\n");
 
                mono_jit_walk_stack (print_stack_frame, TRUE, stderr);
@@ -1559,7 +1813,7 @@ 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 !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;
@@ -1637,14 +1891,8 @@ mono_handle_native_sigsegv (int signal, void *ctx)
        abort ();
 }
 
-/*
- * mono_print_thread_dump:
- *
- *   Print information about the current thread to stdout.
- * SIGCTX can be NULL, allowing this to be called from gdb.
- */
-void
-mono_print_thread_dump (void *sigctx)
+static void
+mono_print_thread_dump_internal (void *sigctx, MonoContext *start_ctx)
 {
        MonoInternalThread *thread = mono_thread_internal_current ();
 #if defined(__i386__) || defined(__x86_64__)
@@ -1665,14 +1913,16 @@ mono_print_thread_dump (void *sigctx)
        else
                g_string_append (text, "\n\"<unnamed thread>\"");
 
-#ifndef PLATFORM_WIN32
+#ifndef HOST_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
 
 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
-       if (!sigctx)
+       if (start_ctx) {
+               memcpy (&ctx, start_ctx, sizeof (MonoContext));
+       } else if (!sigctx)
                MONO_INIT_CONTEXT_FROM_FUNC (&ctx, mono_print_thread_dump);
        else
                mono_arch_sigctx_to_monoctx (sigctx, &ctx);
@@ -1686,3 +1936,44 @@ mono_print_thread_dump (void *sigctx)
        g_string_free (text, TRUE);
        fflush (stdout);
 }
+
+/*
+ * mono_print_thread_dump:
+ *
+ *   Print information about the current thread to stdout.
+ * SIGCTX can be NULL, allowing this to be called from gdb.
+ */
+void
+mono_print_thread_dump (void *sigctx)
+{
+       mono_print_thread_dump_internal (sigctx, NULL);
+}
+
+void
+mono_print_thread_dump_from_ctx (MonoContext *ctx)
+{
+       mono_print_thread_dump_internal (NULL, ctx);
+}
+
+/*
+ * mono_resume_unwind:
+ *
+ *   This is called by code at the end of LLVM compiled finally clauses to continue
+ * unwinding.
+ */
+void
+mono_resume_unwind (void)
+{
+       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       static void (*restore_context) (MonoContext *);
+       MonoContext ctx;
+
+       memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
+
+       mono_handle_exception_internal (&ctx, jit_tls->ex_obj, NULL, FALSE, TRUE, NULL, NULL);
+
+       if (!restore_context)
+               restore_context = mono_get_restore_context ();
+
+       restore_context (&ctx);
+}