2006-01-03 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / mini-exceptions.c
index 3d8917e7612873959ebdca74bb943c89a7cc77cc..b7fcf5208f746f7e87f9ce7946abcf2c1de0e2e6 100644 (file)
 #include <signal.h>
 #include <string.h>
 
+#ifdef HAVE_EXECINFO_H
+#include <execinfo.h>
+#endif
+
 #include <mono/metadata/appdomain.h>
 #include <mono/metadata/tabledefs.h>
 #include <mono/metadata/threads.h>
@@ -553,7 +557,7 @@ ves_icall_System_Security_SecurityFrame_GetSecurityStack (gint32 skip)
  * the first filter clause which caught the exception.
  */
 static gboolean
-mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer original_ip, gboolean test_only, guint32 *out_filter_idx)
+mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer original_ip, gboolean test_only, gint32 *out_filter_idx)
 {
        MonoDomain *domain = mono_domain_get ();
        MonoJitInfo *ji, rji;
@@ -569,7 +573,7 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
        int frame_count = 0;
        gboolean gc_disabled = FALSE;
        gboolean has_dynamic_methods = FALSE;
-       guint32 filter_idx, first_filter_idx;
+       gint32 filter_idx, first_filter_idx;
        
        /*
         * This function might execute on an alternate signal stack, and Boehm GC
@@ -647,6 +651,8 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
                }
        }
 
+       if (out_filter_idx)
+               *out_filter_idx = -1;
        filter_idx = 0;
        initial_ctx = *ctx;
        memset (&rji, 0, sizeof (rji));
@@ -769,7 +775,7 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
 
                *ctx = new_ctx;
 
-               if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
+               if (ji == (gpointer)-1) {
                        if (gc_disabled)
                                mono_gc_enable ();
 
@@ -806,6 +812,48 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer origina
        g_assert_not_reached ();
 }
 
+/**
+ * mono_debugger_run_finally:
+ * @start_ctx: saved processor state
+ *
+ * This method is called by the Mono Debugger to call all `finally' clauses of the
+ * current stack frame.  It's used when the user issues a `return' command to make
+ * the current stack frame return.  After returning from this method, the debugger
+ * unwinds the stack one frame and gives control back to the user.
+ *
+ * NOTE: This method is only used when running inside the Mono Debugger.
+ */
+void
+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);
+       MonoLMF *lmf = jit_tls->lmf;
+       MonoContext ctx, new_ctx;
+       MonoJitInfo *ji, rji;
+       int i;
+
+       ctx = *start_ctx;
+
+       ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, NULL, NULL);
+       if (!ji || ji == (gpointer)-1)
+               return;
+
+       if (!call_filter)
+               call_filter = mono_arch_get_call_filter ();
+
+       for (i = 0; i < ji->num_clauses; i++) {
+               MonoJitExceptionInfo *ei = &ji->clauses [i];
+
+               if ((ei->try_start <= MONO_CONTEXT_GET_IP (&ctx)) && 
+                   (MONO_CONTEXT_GET_IP (&ctx) < ei->try_end) &&
+                   (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
+                       call_filter (&ctx, ei->handler_start);
+               }
+       }
+}
+
 /**
  * mono_handle_exception:
  * @ctx: saved processor state
@@ -895,3 +943,68 @@ mono_free_altstack (MonoJitTlsData *tls)
 }
 
 #endif /* MONO_ARCH_SIGSEGV_ON_ALTSTACK */
+
+static gboolean
+print_stack_frame (MonoMethod *method, gint32 native_offset, gint32 il_offset, gboolean managed, gpointer data)
+{
+       if (method) {
+               if (il_offset != -1)
+                       fprintf (stderr, "in [0x%lx] %s\n", (long)il_offset, mono_method_full_name (method, TRUE));
+               else
+                       fprintf (stderr, "in <0x%lx> %s\n", (long)native_offset, mono_method_full_name (method, TRUE));
+       } else
+               fprintf (stderr, "in <%lx> <unknown>\n", (long)native_offset);
+
+       return FALSE;
+}
+
+/*
+ * mono_handle_native_sigsegv:
+ *
+ *   Handle a SIGSEGV received while in native code by printing diagnostic 
+ * information and aborting.
+ */
+void
+mono_handle_native_sigsegv (void *ctx)
+{
+       /*
+        * A SIGSEGV indicates something went very wrong so we can no longer depend
+        * on anything working. So try to print out lots of diagnostics, starting 
+        * with ones which have a greater change of working.
+        */
+       fprintf (stderr,
+                        "\n"
+                        "=================================================================\n"
+                        "Got a SIGSEGV 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");
+
+       fprintf (stderr, "Stacktrace:\n\n");
+
+       mono_jit_walk_stack (print_stack_frame, TRUE, NULL);
+
+       fflush (stderr);
+
+#ifdef HAVE_BACKTRACE_SYMBOLS
+ {
+       void *array [256];
+       char **names;
+       int i, size;
+
+       fprintf (stderr, "\nNative stacktrace:\n\n");
+
+       size = backtrace (array, 256);
+       names = backtrace_symbols (array, size);
+       for (i =0; i < size; ++i) {
+               fprintf (stderr, "\t%s\n", names [i]);
+       }
+       free (names);
+ }
+
+       fflush (stderr);
+#endif
+
+       abort ();
+}