* Mono Team (mono-list@lists.ximian.com)
*
* Copyright 2001-2003 Ximian, Inc.
- * Copyright 2003-2008 Ximian, Inc.
+ * Copyright 2003-2008 Novell, Inc.
+ * Copyright 2011 Xamarin Inc (http://www.xamarin.com).
*/
#include <config.h>
if (!gi->has_this)
return NULL;
- if (gi->this_in_reg)
- info = (gpointer)mono_arch_context_get_int_reg (ctx, gi->this_reg);
- else
- info = *(gpointer*)(gpointer)((char*)mono_arch_context_get_int_reg (ctx, gi->this_reg) +
- gi->this_offset);
+ info = NULL;
+ /*
+ * Search location list if available, it contains the precise location of the
+ * argument for every pc offset, even if the method was interrupted while it was in
+ * its prolog.
+ */
+ if (gi->nlocs) {
+ int offset = (mgreg_t)MONO_CONTEXT_GET_IP (ctx) - (mgreg_t)ji->code_start;
+ int i;
+
+ for (i = 0; i < gi->nlocs; ++i) {
+ MonoDwarfLocListEntry *entry = &gi->locations [i];
+
+ if (offset >= entry->from && (offset < entry->to || entry->to == 0)) {
+ if (entry->is_reg)
+ info = (gpointer)mono_arch_context_get_int_reg (ctx, entry->reg);
+ else
+ info = *(gpointer*)(gpointer)((char*)mono_arch_context_get_int_reg (ctx, entry->reg) + entry->offset);
+ break;
+ }
+ }
+ g_assert (i < gi->nlocs);
+ } else {
+ if (gi->this_in_reg)
+ info = (gpointer)mono_arch_context_get_int_reg (ctx, gi->this_reg);
+ else
+ info = *(gpointer*)(gpointer)((char*)mono_arch_context_get_int_reg (ctx, gi->this_reg) +
+ gi->this_offset);
+ }
+
if (mono_method_get_context (ji->method)->method_inst) {
return info;
} else if ((ji->method->flags & METHOD_ATTRIBUTE_STATIC) || ji->method->klass->valuetype) {
mono_runtime_walk_stack_with_ctx (MonoJitStackWalk func, MonoContext *start_ctx, MonoUnwindOptions unwind_options, void *user_data)
{
if (!start_ctx) {
- MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+ MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
if (jit_tls && jit_tls->orig_ex_ctx_set)
start_ctx = &jit_tls->orig_ex_ctx;
}
MonoString **file, gint32 *line, gint32 *column)
{
MonoDomain *domain = mono_domain_get ();
- MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+ MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
MonoLMF *lmf = mono_get_lmf ();
MonoJitInfo *ji = NULL;
MonoContext ctx, new_ctx;
#define DOES_STACK_GROWS_UP 0
#endif
+
+#define setup_managed_stacktrace_information() do { \
+ if (mono_ex && !initial_trace_ips) { \
+ trace_ips = g_list_reverse (trace_ips); \
+ 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_OBJECT_SETREF (mono_ex, stack_trace, ves_icall_System_Exception_get_trace (mono_ex)); \
+ } \
+ g_list_free (trace_ips); \
+ trace_ips = NULL; \
+} while (0)
/*
* mono_handle_exception_internal_first_pass:
*
* OUT_FILTER_IDX. Return TRUE if the exception is caught, FALSE otherwise.
*/
static gboolean
-mono_handle_exception_internal_first_pass (MonoContext *ctx, gpointer obj, gpointer original_ip, gint32 *out_filter_idx, MonoJitInfo **out_ji, MonoObject *non_exception)
+mono_handle_exception_internal_first_pass (MonoContext *ctx, gpointer obj, gint32 *out_filter_idx, MonoJitInfo **out_ji, MonoObject *non_exception)
{
MonoDomain *domain = mono_domain_get ();
MonoJitInfo *ji;
static int (*call_filter) (MonoContext *, gpointer) = NULL;
- MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+ MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
MonoLMF *lmf = mono_get_lmf ();
MonoArray *initial_trace_ips = NULL;
GList *trace_ips = NULL;
}
if (!unwind_res) {
- if (mono_ex && !initial_trace_ips) {
- trace_ips = g_list_reverse (trace_ips);
- 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_OBJECT_SETREF (mono_ex, stack_trace, ves_icall_System_Exception_get_trace (mono_ex));
- }
- g_list_free (trace_ips);
+ setup_managed_stacktrace_information ();
return FALSE;
}
ex_obj = obj;
if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
+ gboolean is_user_frame = ji->method->wrapper_type == MONO_WRAPPER_NONE || ji->method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD;
mono_perfcounters->exceptions_filters++;
mono_debugger_call_exception_handler (ei->data.filter, MONO_CONTEXT_GET_SP (ctx), ex_obj);
- if (mono_ex && !initial_trace_ips) {
- trace_ips = g_list_reverse (trace_ips);
- 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_OBJECT_SETREF (mono_ex, stack_trace, ves_icall_System_Exception_get_trace (mono_ex));
- }
- g_list_free (trace_ips);
- trace_ips = NULL;
+ /*
+ Here's the thing, if this is a filter clause done by a wrapper like runtime invoke, we don't want to
+ trim the stackframe since if it returns FALSE we lose information.
+
+ FIXME Not 100% sure if it's a good idea even with user clauses.
+ */
+ if (is_user_frame)
+ setup_managed_stacktrace_information ();
if (ji->from_llvm) {
#ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
filter_idx ++;
if (filtered) {
+ if (!is_user_frame)
+ setup_managed_stacktrace_information ();
/* mono_debugger_agent_handle_exception () needs this */
MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
return TRUE;
}
if (ei->flags == MONO_EXCEPTION_CLAUSE_NONE && mono_object_isinst (ex_obj, catch_class)) {
- if (mono_ex && !initial_trace_ips) {
- trace_ips = g_list_reverse (trace_ips);
- 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_OBJECT_SETREF (mono_ex, stack_trace, ves_icall_System_Exception_get_trace (mono_ex));
- }
- g_list_free (trace_ips);
+ setup_managed_stacktrace_information ();
if (out_ji)
*out_ji = ji;
* @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 resume, MonoJitInfo **out_ji)
+mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gboolean resume, MonoJitInfo **out_ji)
{
MonoDomain *domain = mono_domain_get ();
MonoJitInfo *ji;
static int (*call_filter) (MonoContext *, gpointer) = NULL;
static void (*restore_context) (void *);
- MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+ MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
MonoLMF *lmf = mono_get_lmf ();
MonoException *mono_ex;
gboolean stack_overflow = FALSE;
mono_profiler_exception_thrown (obj);
jit_tls->orig_ex_ctx_set = FALSE;
- res = mono_handle_exception_internal_first_pass (&ctx_cp, obj, original_ip, &first_filter_idx, &ji, non_exception);
+ res = mono_handle_exception_internal_first_pass (&ctx_cp, obj, &first_filter_idx, &ji, non_exception);
if (!res) {
- if (mono_break_on_exc)
+ if (mini_get_debug_options ()->break_on_exc)
G_BREAKPOINT ();
mono_debugger_agent_handle_exception (obj, ctx, NULL);
* The debugger wants us to stop only if this exception is user-unhandled.
*/
- ret = mono_handle_exception_internal_first_pass (&ctx_cp, obj, MONO_CONTEXT_GET_IP (ctx), NULL, &ji, NULL);
+ ret = mono_handle_exception_internal_first_pass (&ctx_cp, obj, NULL, &ji, NULL);
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
{
static int (*call_filter) (MonoContext *, gpointer) = NULL;
MonoDomain *domain = mono_domain_get ();
- MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+ MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
MonoLMF *lmf = mono_get_lmf ();
MonoContext ctx, new_ctx;
MonoJitInfo *ji, rji;
* 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)
+mono_handle_exception (MonoContext *ctx, gpointer obj)
{
- if (!test_only)
- mono_perfcounters->exceptions_thrown++;
+ mono_perfcounters->exceptions_thrown++;
- g_assert (!test_only);
- return mono_handle_exception_internal (ctx, obj, original_ip, FALSE, NULL);
+ return mono_handle_exception_internal (ctx, obj, FALSE, NULL);
}
#ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
mono_setup_altstack (MonoJitTlsData *tls)
{
size_t stsize = 0;
- struct sigaltstack sa;
+ stack_t sa;
guint8 *staddr = NULL;
if (mono_running_on_valgrind ())
sa.ss_sp = tls->signal_stack;
sa.ss_size = MONO_ARCH_SIGNAL_STACK_SIZE;
+#if __APPLE__
+ sa.ss_flags = 0;
+#else
sa.ss_flags = SS_ONSTACK;
- sigaltstack (&sa, NULL);
+#endif
+ g_assert (sigaltstack (&sa, NULL) == 0);
+
+ mono_gc_register_altstack ((char*)tls->stack_ovf_guard_base + tls->stack_ovf_guard_size, (char*)staddr + stsize - ((char*)tls->stack_ovf_guard_base + tls->stack_ovf_guard_size), tls->signal_stack, tls->signal_stack_size);
}
void
mono_free_altstack (MonoJitTlsData *tls)
{
- struct sigaltstack sa;
+ stack_t sa;
int err;
sa.ss_sp = tls->signal_stack;
return unprotect_size == jit_tls->stack_ovf_guard_size;
}
-static void
+static G_GNUC_UNUSED void
try_more_restore (void)
{
- MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+ MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
if (try_restore_stack_protection (jit_tls, 500))
jit_tls->restore_stack_prot = NULL;
}
-static void
+static G_GNUC_UNUSED void
restore_stack_protection (void)
{
- MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+ MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
MonoException *ex = mono_domain_get ()->stack_overflow_ex;
/* if we can't restore the stack protection, keep a callback installed so
* we'll try to restore as much stack as we can at each return from unmanaged
#ifdef MONO_ARCH_USE_SIGACTION
struct sigaction sa;
#endif
- MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+ MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
if (handling_sigsegv)
return;
#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;
- int stdout_pipe [2] = { -1, -1 };
pid_t pid;
int status;
- char buffer [1024];
+ pid_t crashed_pid = getpid ();
- res = pipe (stdout_pipe);
- g_assert (res != -1);
-
//pid = fork ();
/*
* glibc fork acquires some locks, so if the crash happened inside malloc/free,
pid = mono_runtime_syscall_fork ();
if (pid == 0) {
- close (stdout_pipe [0]);
- dup2 (stdout_pipe [1], STDOUT_FILENO);
-
- for (i = getdtablesize () - 1; i >= 3; i--)
- close (i);
-
- if (!mono_gdb_render_native_backtraces ())
- close (STDOUT_FILENO);
+ dup2 (STDERR_FILENO, STDOUT_FILENO);
+ mono_gdb_render_native_backtraces (crashed_pid);
exit (1);
}
- close (stdout_pipe [1]);
-
fprintf (stderr, "\nDebug info from gdb:\n\n");
-
- while (1) {
- int nread = read (stdout_pipe [0], buffer, 1024);
-
- if (nread <= 0)
- break;
- write (STDERR_FILENO, buffer, nread);
- }
-
- waitpid (pid, &status, WNOHANG);
+ waitpid (pid, &status, 0);
}
#endif
/*
void
mono_resume_unwind (MonoContext *ctx)
{
- MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+ MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
static void (*restore_context) (MonoContext *);
MonoContext new_ctx;
MONO_CONTEXT_SET_SP (ctx, MONO_CONTEXT_GET_SP (&jit_tls->resume_state.ctx));
new_ctx = *ctx;
- mono_handle_exception_internal (&new_ctx, jit_tls->resume_state.ex_obj, NULL, TRUE, NULL);
+ mono_handle_exception_internal (&new_ctx, jit_tls->resume_state.ex_obj, TRUE, NULL);
if (!restore_context)
restore_context = mono_get_restore_context ();
MonoJitTlsData *jit_tls = NULL;
if (mini_get_debug_options ()->better_cast_details) {
- jit_tls = TlsGetValue (mono_jit_tls_id);
+ jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
jit_tls->class_cast_from = from;
jit_tls->class_cast_to = to;
}
if (sigctx)
mono_arch_sigctx_to_monoctx (sigctx, &ctx->ctx);
else
-#if MONO_ARCH_HAS_MONO_CONTEXT
+#if MONO_ARCH_HAS_MONO_CONTEXT && !defined(MONO_CROSS_COMPILE)
MONO_CONTEXT_GET_CURRENT (ctx->ctx);
#else
g_error ("Use a null sigctx requires a working mono-context");
void (*restore_context) (MonoContext *);
restore_context = mono_get_restore_context ();
- mono_handle_exception (ctx, exc, NULL, FALSE);
+ mono_handle_exception (ctx, exc);
restore_context (ctx);
}
mono_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
{
#ifdef MONO_ARCH_HAVE_SETUP_ASYNC_CALLBACK
- MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+ MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
jit_tls->ex_ctx = *ctx;
mono_arch_setup_async_callback (ctx, async_cb, user_data);