2010-02-10 Geoff Norton <gnorton@novell.com>
[mono.git] / mono / mini / mini-exceptions.c
index 8d29d6477caa3c41cbb350a308d4955074d3dfa3..c0fc622058afb5c708057c3e5787ebf0d4544e91 100644 (file)
@@ -129,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;
@@ -152,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;
 }
 
@@ -331,7 +340,7 @@ mono_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *re
  *   A version of mono_find_jit_info which returns all data in the StackFrameInfo
  * structure.
  */
-static gboolean
+gboolean
 mono_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls, 
                                                MonoJitInfo *prev_ji, MonoContext *ctx,
                                                MonoContext *new_ctx, char **trace, MonoLMF **lmf,
@@ -1080,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);
@@ -1101,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;
@@ -1175,7 +1195,7 @@ 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
@@ -1268,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) {
@@ -1336,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);
+                                                       }
                                                }
                                                
                                        }
@@ -1413,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
@@ -1492,7 +1536,7 @@ 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
@@ -1741,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);
@@ -1847,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__)
@@ -1882,7 +1920,9 @@ mono_print_thread_dump (void *sigctx)
 #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);
@@ -1896,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);
+}