#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>
#include <mono/metadata/exception.h>
#include <mono/metadata/gc-internal.h>
#include <mono/metadata/mono-debug.h>
-#include <mono/metadata/mono-debug-debugger.h>
#include "mini.h"
+#include "trace.h"
+
+#ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
+#include <unistd.h>
+#include <sys/mman.h>
+#endif
#define IS_ON_SIGALTSTACK(jit_tls) ((jit_tls) && ((guint8*)&(jit_tls) > (guint8*)(jit_tls)->signal_stack) && ((guint8*)&(jit_tls) < ((guint8*)(jit_tls)->signal_stack + (jit_tls)->signal_stack_size)))
+#ifndef MONO_ARCH_CONTEXT_DEF
+#define MONO_ARCH_CONTEXT_DEF
+#endif
+
#ifndef mono_find_jit_info
/* mono_find_jit_info:
*/
static MonoJitInfo *
mono_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
- MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
- gboolean *managed)
+ MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
+ gboolean *managed)
{
gboolean managed2;
gpointer ip = MONO_CONTEXT_GET_IP (ctx);
return ji;
if (managed2 || ji->method->wrapper_type) {
- char *source_location, *tmpaddr, *fname;
- gint32 offset, iloffset;
+ const char *real_ip, *start;
+ gint32 offset;
+ start = (const char *)ji->code_start;
if (!managed2)
/* ctx->ip points into native code */
- offset = (char*)MONO_CONTEXT_GET_IP (new_ctx) - (char*)ji->code_start;
+ real_ip = (const char*)MONO_CONTEXT_GET_IP (new_ctx);
else
- offset = (char *)ip - (char *)ji->code_start;
+ real_ip = (const char*)ip;
+
+ if ((real_ip >= start) && (real_ip <= start + ji->code_size))
+ offset = real_ip - start;
+ else
+ offset = -1;
if (native_offset)
*native_offset = offset;
if (!ji->method->wrapper_type)
*managed = TRUE;
- if (trace) {
- source_location = mono_debug_source_location_from_address (ji->method, offset, NULL, domain);
- iloffset = mono_debug_il_offset_from_address (ji->method, offset, domain);
-
- if (iloffset < 0)
- tmpaddr = g_strdup_printf ("<0x%05x>", offset);
- else
- tmpaddr = g_strdup_printf ("[0x%05x]", iloffset);
-
- fname = mono_method_full_name (ji->method, TRUE);
-
- if (source_location)
- *trace = g_strdup_printf ("in %s (at %s) %s", tmpaddr, source_location, fname);
- else
- *trace = g_strdup_printf ("in %s %s", tmpaddr, fname);
-
- g_free (fname);
- g_free (source_location);
- g_free (tmpaddr);
- }
- }
- else {
+ if (trace)
+ *trace = mono_debug_print_stack_frame (ji->method, offset, domain);
+ } else {
if (trace) {
char *fname = mono_method_full_name (res->method, TRUE);
*trace = g_strdup_printf ("in (unmanaged) %s", fname);
MonoArray *ta = ex->trace_ips;
int i, len;
GString *trace_str;
- char tmpaddr [256];
if (ta == NULL)
/* Exception is not thrown yet */
/* Unmanaged frame */
g_string_append_printf (trace_str, "in (unmanaged) %p\n", ip);
} else {
- char *source_location, *fname;
- gint32 address, iloffset;
+ gchar *location;
+ gint32 address;
address = (char *)ip - (char *)ji->code_start;
+ location = mono_debug_print_stack_frame (
+ ji->method, address, ex->object.vtable->domain);
- source_location = mono_debug_source_location_from_address (ji->method, address, NULL, ex->object.vtable->domain);
- iloffset = mono_debug_il_offset_from_address (ji->method, address, ex->object.vtable->domain);
-
- if (iloffset < 0)
- sprintf (tmpaddr, "<0x%05x>", address);
- else
- sprintf (tmpaddr, "[0x%05x]", iloffset);
-
- fname = mono_method_full_name (ji->method, TRUE);
-
- if (source_location)
- g_string_append_printf (trace_str, "in %s (at %s) %s\n", tmpaddr, source_location, fname);
- else
- g_string_append_printf (trace_str, "in %s %s\n", tmpaddr, fname);
-
- g_free (fname);
- g_free (source_location);
+ g_string_append_printf (trace_str, "%s\n", location);
+ g_free (location);
}
}
MonoDomain *domain = mono_domain_get ();
MonoArray *res;
MonoArray *ta = exc->trace_ips;
+ MonoDebugSourceLocation *location;
int i, len;
if (ta == NULL) {
ji = mono_jit_info_table_find (domain, ip);
if (ji == NULL) {
/* Unmanaged frame */
- mono_array_set (res, gpointer, i, sf);
+ mono_array_setref (res, i, sf);
continue;
}
sf->method = NULL;
s = mono_method_full_name (ji->method, TRUE);
- sf->internal_method_name = mono_string_new (domain, s);
+ MONO_OBJECT_SETREF (sf, internal_method_name, mono_string_new (domain, s));
g_free (s);
}
else
- sf->method = mono_method_get_object (domain, ji->method, NULL);
+ MONO_OBJECT_SETREF (sf, method, mono_method_get_object (domain, ji->method, NULL));
sf->native_offset = (char *)ip - (char *)ji->code_start;
- sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
+ /*
+ * mono_debug_lookup_source_location() returns both the file / line number information
+ * and the IL offset. Note that computing the IL offset is already an expensive
+ * operation, so we shouldn't call this method twice.
+ */
+ location = mono_debug_lookup_source_location (ji->method, sf->native_offset, domain);
+ if (location)
+ sf->il_offset = location->il_offset;
+ else
+ sf->il_offset = 0;
if (need_file_info) {
- gchar *filename;
-
- filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
-
- sf->filename = filename? mono_string_new (domain, filename): NULL;
- sf->column = 0;
-
- g_free (filename);
+ if (location) {
+ MONO_OBJECT_SETREF (sf, filename, mono_string_new (domain, location->source_file));
+ sf->line = location->row;
+ sf->column = location->column;
+ } else {
+ sf->line = sf->column = 0;
+ sf->filename = NULL;
+ }
}
- mono_array_set (res, gpointer, i, sf);
+ mono_debug_free_source_location (location);
+ mono_array_setref (res, i, sf);
}
return res;
#ifndef CUSTOM_STACK_WALK
void
-mono_jit_walk_stack (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
+mono_jit_walk_stack_from_ctx (MonoStackWalk func, MonoContext *start_ctx, gboolean do_il_offset, gpointer user_data)
+{
MonoDomain *domain = mono_domain_get ();
MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
MonoLMF *lmf = jit_tls->lmf;
MonoJitInfo *ji, rji;
gint native_offset, il_offset;
gboolean managed;
-
MonoContext ctx, new_ctx;
-#ifdef _MSC_VER
- unsigned int stackptr, retaddr;
- __asm mov stackptr, ebp;
- __asm mov eax, DWORD PTR [ebp + 4];
- __asm mov retaddr, eax;
- MONO_CONTEXT_SET_IP (&ctx, retaddr);
- /* FIXME: NOT WORKING -- THIS IS __builtin_frame_address (0) NOT (1) */
- MONO_CONTEXT_SET_BP (&ctx, stackptr);
-#else
+ MONO_ARCH_CONTEXT_DEF
+
mono_arch_flush_register_windows ();
- MONO_CONTEXT_SET_IP (&ctx, __builtin_return_address (0));
- MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (1));
+ 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
+ }
+
while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
-
ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
g_assert (ji);
if (ji == (gpointer)-1)
return;
- il_offset = do_il_offset ? mono_debug_il_offset_from_address (ji->method, native_offset, domain): -1;
+ if (do_il_offset) {
+ MonoDebugSourceLocation *source;
+
+ source = mono_debug_lookup_source_location (ji->method, native_offset, domain);
+ il_offset = source ? source->il_offset : -1;
+ mono_debug_free_source_location (source);
+ } else
+ il_offset = -1;
if (func (ji->method, native_offset, il_offset, managed, user_data))
return;
}
}
+void
+mono_jit_walk_stack (MonoStackWalk func, gboolean do_il_offset, gpointer user_data)
+{
+ mono_jit_walk_stack_from_ctx (func, NULL, do_il_offset, user_data);
+}
+
MonoBoolean
ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
MonoReflectionMethod **method,
MonoLMF *lmf = jit_tls->lmf;
MonoJitInfo *ji, rji;
MonoContext ctx, new_ctx;
- gssize stackptr;
+ MonoDebugSourceLocation *location;
+
+ MONO_ARCH_CONTEXT_DEF;
mono_arch_flush_register_windows ();
-#ifdef _MSC_VER
- __asm mov stackptr, ebp;
+#ifdef MONO_INIT_CONTEXT_FROM_CURRENT
+ MONO_INIT_CONTEXT_FROM_CURRENT (&ctx);
#else
- stackptr = (gssize)__builtin_frame_address (0);
+ MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_get_frame_info);
#endif
- MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
- MONO_CONTEXT_SET_BP (&ctx, stackptr);
-
+ /*
+ * FIXME: This is needed because of the LMF stuff which doesn't exist on ia64.
+ * Probably the whole mono_find_jit_info () stuff needs to be fixed so this isn't
+ * needed even on other platforms. This is also true for s390/s390x.
+ */
+#if !defined(__ia64__) && !defined(__s390__) && !defined(__s390x__)
skip++;
+#endif
do {
ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, native_offset, NULL);
} while (skip >= 0);
*method = mono_method_get_object (domain, ji->method, NULL);
- *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset, domain);
-
- if (need_file_info) {
- gchar *filename;
-
- filename = mono_debug_source_location_from_address (ji->method, *native_offset, line, domain);
- *file = filename? mono_string_new (domain, filename): NULL;
- *column = 0;
+ location = mono_debug_lookup_source_location (ji->method, *native_offset, domain);
+ if (location)
+ *iloffset = location->il_offset;
+ else
+ *iloffset = 0;
- g_free (filename);
+ if (need_file_info) {
+ if (location) {
+ *file = mono_string_new (domain, location->source_file);
+ *line = location->row;
+ *column = location->column;
+ } else {
+ *file = NULL;
+ *line = *column = 0;
+ }
}
+ mono_debug_free_source_location (location);
+
return TRUE;
}
MonoFrameSecurityInfo si;
MonoContext ctx;
-#ifdef _MSC_VER
- /* seems that MSC doesn't like having __asm in macros */
- unsigned int stackptr;
- mono_arch_flush_register_windows ();
- __asm mov stackptr, ebp;
- MONO_CONTEXT_SET_IP (&ctx, ves_icall_System_Security_SecurityFrame_GetSecurityFrame);
- MONO_CONTEXT_SET_BP (&ctx, stackptr);
-#else
+ MONO_ARCH_CONTEXT_DEF
+
MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_System_Security_SecurityFrame_GetSecurityFrame);
-#endif
si.skips = skip;
si.frame = NULL;
int i;
for (i=0; i < stack->maximum; i++) {
gpointer frame = mono_array_get (stack->stack, gpointer, i);
- mono_array_set (newstack, gpointer, i, frame);
+ mono_array_setref (newstack, i, frame);
}
stack->maximum = newsize;
stack->stack = newstack;
if (ss->count == ss->maximum)
grow_array (ss);
- mono_array_set (ss->stack, gpointer, ss->count++, mono_declsec_create_frame (domain, ji));
+ mono_array_setref (ss->stack, ss->count++, mono_declsec_create_frame (domain, ji));
/* continue down the stack */
return FALSE;
MonoSecurityStack ss;
MonoContext ctx;
-#ifdef _MSC_VER
- /* seems that MSC doesn't like having __asm in macros */
- unsigned int stackptr;
- mono_arch_flush_register_windows ();
- __asm mov stackptr, ebp;
- MONO_CONTEXT_SET_IP (&ctx, ves_icall_System_Security_SecurityFrame_GetSecurityStack);
- MONO_CONTEXT_SET_BP (&ctx, stackptr);
-#else
+ MONO_ARCH_CONTEXT_DEF
+
MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_System_Security_SecurityFrame_GetSecurityStack);
-#endif
ss.skips = skip;
ss.count = 0;
#ifndef CUSTOM_EXCEPTION_HANDLING
/**
- * mono_handle_exception:
+ * mono_handle_exception_internal:
* @ctx: saved processor state
* @obj: the exception object
* @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.
*/
-gboolean
-mono_handle_exception (MonoContext *ctx, gpointer obj, gpointer original_ip, gboolean test_only)
+static gboolean
+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;
int frame_count = 0;
gboolean gc_disabled = FALSE;
gboolean has_dynamic_methods = FALSE;
+ gint32 filter_idx, first_filter_idx;
/*
* This function might execute on an alternate signal stack, and Boehm GC
g_assert (ctx != NULL);
if (!obj) {
MonoException *ex = mono_get_exception_null_reference ();
- ex->message = mono_string_new (domain, "Object reference not set to an instance of an object");
+ MONO_OBJECT_SETREF (ex, message, mono_string_new (domain, "Object reference not set to an instance of an object"));
obj = (MonoObject *)ex;
}
if (!test_only) {
MonoContext ctx_cp = *ctx;
- if (mono_jit_trace_calls != NULL)
+ if (mono_trace_is_enabled ())
g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
- if (!mono_handle_exception (&ctx_cp, obj, original_ip, TRUE)) {
+ if (!mono_handle_exception_internal (&ctx_cp, obj, original_ip, TRUE, &first_filter_idx)) {
if (mono_break_on_exc)
G_BREAKPOINT ();
mono_unhandled_exception (obj);
}
}
+ if (out_filter_idx)
+ *out_filter_idx = -1;
+ filter_idx = 0;
initial_ctx = *ctx;
memset (&rji, 0, sizeof (rji));
if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)) {
/* store the exception object in cfg->excvar */
g_assert (ei->exvar_offset);
- *((gpointer *)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = obj;
+ *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = obj;
}
if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
- mono_debugger_handle_exception (ei->data.filter, MONO_CONTEXT_GET_SP (ctx), obj);
- filtered = call_filter (ctx, ei->data.filter);
+ // mono_debugger_handle_exception (ei->data.filter, MONO_CONTEXT_GET_SP (ctx), obj);
+ if (test_only) {
+ filtered = call_filter (ctx, ei->data.filter);
+ if (filtered && out_filter_idx)
+ *out_filter_idx = filter_idx;
+ }
+ else {
+ /*
+ * Filter clauses should only be run in the
+ * first pass of exception handling.
+ */
+ filtered = (filter_idx == first_filter_idx);
+ }
+ filter_idx ++;
}
if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
if (test_only) {
if (mono_ex && !initial_trace_ips) {
trace_ips = g_list_reverse (trace_ips);
- mono_ex->trace_ips = glist_to_array (trace_ips, mono_defaults.int_class);
+ 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_ex->stack_trace = ves_icall_System_Exception_get_trace (mono_ex);
+ MONO_OBJECT_SETREF (mono_ex, stack_trace, ves_icall_System_Exception_get_trace (mono_ex));
}
g_list_free (trace_ips);
mono_gc_enable ();
return TRUE;
}
- if (mono_jit_trace_calls != NULL && mono_trace_eval (ji->method))
+ if (mono_trace_is_enabled () && mono_trace_eval (ji->method))
g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
mono_debugger_handle_exception (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), obj);
MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
(ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
- if (mono_jit_trace_calls != NULL && mono_trace_eval (ji->method))
+ if (mono_trace_is_enabled () && mono_trace_eval (ji->method))
g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
mono_debugger_handle_exception (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), obj);
call_filter (ctx, ei->handler_start);
*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 ();
if (IS_ON_SIGALTSTACK (jit_tls)) {
/* Switch back to normal stack */
- if (stack_overflow)
+ if (stack_overflow) {
/* Free up some stack space */
MONO_CONTEXT_SET_SP (&initial_ctx, (gssize)(MONO_CONTEXT_GET_SP (&initial_ctx)) + (64 * 1024));
+ g_assert ((gssize)MONO_CONTEXT_GET_SP (&initial_ctx) < (gssize)jit_tls->end_of_stack);
+ }
MONO_CONTEXT_SET_IP (&initial_ctx, (gssize)jit_tls->abort_func);
restore_context (&initial_ctx);
}
} else {
if (mono_ex && !initial_trace_ips) {
trace_ips = g_list_reverse (trace_ips);
- mono_ex->trace_ips = glist_to_array (trace_ips, mono_defaults.int_class);
+ 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_ex->stack_trace = ves_icall_System_Exception_get_trace (mono_ex);
+ MONO_OBJECT_SETREF (mono_ex, stack_trace, ves_icall_System_Exception_get_trace (mono_ex));
}
g_list_free (trace_ips);
return FALSE;
g_assert_not_reached ();
}
-#endif /* CUSTOM_EXECPTION_HANDLING */
+/**
+ * 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);
+}