+/**
+ * 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
+ * @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)
+{
+ return mono_handle_exception_internal (ctx, obj, original_ip, test_only, NULL);
+}
+
+#endif /* CUSTOM_EXCEPTION_HANDLING */
+
+#ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
+
+void
+mono_setup_altstack (MonoJitTlsData *tls)
+{
+ pthread_t self = pthread_self();
+ pthread_attr_t attr;
+ size_t stsize = 0;
+ struct sigaltstack sa;
+ guint8 *staddr = NULL;
+ guint8 *current = (guint8*)&staddr;
+
+ if (mono_running_on_valgrind ())
+ return;
+
+ /* Determine stack boundaries */
+ pthread_attr_init( &attr );
+#ifdef HAVE_PTHREAD_GETATTR_NP
+ pthread_getattr_np( self, &attr );
+#else
+#ifdef HAVE_PTHREAD_ATTR_GET_NP
+ pthread_attr_get_np( self, &attr );
+#elif defined(sun)
+ pthread_attr_getstacksize( &attr, &stsize );
+#else
+#error "Not implemented"
+#endif
+#endif
+#ifndef sun
+ pthread_attr_getstack( &attr, (void**)&staddr, &stsize );
+#endif
+
+ g_assert (staddr);
+
+ g_assert ((current > staddr) && (current < staddr + stsize));
+
+ tls->end_of_stack = staddr + stsize;
+
+ /*
+ * threads created by nptl does not seem to have a guard page, and
+ * since the main thread is not created by us, we can't even set one.
+ * Increasing stsize fools the SIGSEGV signal handler into thinking this
+ * is a stack overflow exception.
+ */
+ tls->stack_size = stsize + getpagesize ();
+
+ /* Setup an alternate signal stack */
+ tls->signal_stack = mmap (0, MONO_ARCH_SIGNAL_STACK_SIZE, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+ tls->signal_stack_size = MONO_ARCH_SIGNAL_STACK_SIZE;
+
+ g_assert (tls->signal_stack);
+
+ sa.ss_sp = tls->signal_stack;
+ sa.ss_size = MONO_ARCH_SIGNAL_STACK_SIZE;
+ sa.ss_flags = SS_ONSTACK;
+ sigaltstack (&sa, NULL);
+}
+
+void
+mono_free_altstack (MonoJitTlsData *tls)
+{
+ struct sigaltstack sa;
+ int err;
+
+ sa.ss_sp = tls->signal_stack;
+ sa.ss_size = MONO_ARCH_SIGNAL_STACK_SIZE;
+ sa.ss_flags = SS_DISABLE;
+ err = sigaltstack (&sa, NULL);
+ g_assert (err == 0);
+
+ if (tls->signal_stack)
+ munmap (tls->signal_stack, MONO_ARCH_SIGNAL_STACK_SIZE);
+}
+
+#endif /* MONO_ARCH_SIGSEGV_ON_ALTSTACK */
+
+static gboolean
+print_stack_frame (MonoMethod *method, gint32 native_offset, gint32 il_offset, gboolean managed, gpointer data)
+{
+ FILE *stream = (FILE*)data;
+
+ if (method) {
+ gchar *location = mono_debug_print_stack_frame (method, native_offset, mono_domain_get ());
+ fprintf (stream, " %s\n", location);
+ g_free (location);
+ } else
+ fprintf (stream, " at <unknown> <0x%05x>\n", 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 (int signal, void *ctx)
+{
+#ifndef PLATFORM_WIN32
+ struct sigaction sa;
+#endif
+ const char *signal_str = (signal == SIGSEGV) ? "SIGSEGV" : "SIGABRT";
+
+
+ /*
+ * 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 chance of working.
+ */
+ fprintf (stderr,
+ "\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);
+
+ fprintf (stderr, "Stacktrace:\n\n");
+
+ mono_jit_walk_stack (print_stack_frame, TRUE, stderr);
+
+ 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
+
+#ifndef PLATFORM_WIN32
+
+ /* Remove our SIGABRT handler */
+ sa.sa_handler = SIG_DFL;
+ sigemptyset (&sa.sa_mask);
+ sa.sa_flags = 0;
+
+ g_assert (sigaction (SIGABRT, &sa, NULL) != -1);
+
+#endif
+
+ abort ();
+}
+
+/*
+ * mono_print_thread_dump:
+ *
+ * Print information about the current thread to stdout.
+ */
+void
+mono_print_thread_dump (void *sigctx)
+{
+ MonoThread *thread = mono_thread_current ();
+#ifdef __i386__
+ MonoContext ctx;
+#endif
+ char *name;
+ GError *error = NULL;
+
+ if (thread->name) {
+ name = g_utf16_to_utf8 (thread->name, thread->name_len, NULL, NULL, &error);
+ g_assert (!error);
+ fprintf (stdout, "\n\"%s\"", name);
+ g_free (name);
+ }
+ else
+ fprintf (stdout, "\n\"\"");
+
+ fprintf (stdout, " tid=0x%p this=0x%p:\n", (gpointer)(gsize)thread->tid, thread);
+
+ /* FIXME: */
+#ifdef __i386__
+ mono_arch_sigctx_to_monoctx (sigctx, &ctx);
+
+ mono_jit_walk_stack_from_ctx (print_stack_frame, &ctx, TRUE, stdout);
+#else
+ printf ("\t<Stack traces in thread dumps not supported on this platform>\n");
+#endif
+
+ fflush (stdout);
+}