Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / mini / debugger-agent.c
index b887931ac0e37c068ecbe2fd58899c7e60c2dd04..29f94ba1e06e0b4870e9d4bda438e4ab39a9718d 100644 (file)
@@ -47,7 +47,7 @@
 #include <ws2tcpip.h>
 #endif
 
-#ifdef PLATFORM_ANDROID
+#ifdef HOST_ANDROID
 #include <linux/in.h>
 #include <linux/tcp.h>
 #include <sys/endian.h>
@@ -595,11 +595,6 @@ typedef struct {
        MonoClass *klass;
 } EventInfo;
 
-/* Dummy structure used for the profiler callbacks */
-typedef struct {
-       void* dummy;
-} DebuggerProfiler;
-
 typedef struct {
        guint8 *buf, *p, *end;
 } Buffer;
@@ -612,7 +607,7 @@ typedef struct ReplyPacket {
 
 #define DEBUG(level,s) do { if (G_UNLIKELY ((level) <= log_level)) { s; fflush (log_file); } } while (0)
 
-#ifdef PLATFORM_ANDROID
+#ifdef HOST_ANDROID
 #define DEBUG_PRINTF(level, ...) do { if (G_UNLIKELY ((level) <= log_level)) { g_print (__VA_ARGS__); } } while (0)
 #else
 #define DEBUG_PRINTF(level, ...) do { if (G_UNLIKELY ((level) <= log_level)) { fprintf (log_file, __VA_ARGS__); fflush (log_file); } } while (0)
@@ -700,8 +695,6 @@ static MonoCoopCond debugger_thread_exited_cond;
 /* Mutex for the cond var above */
 static MonoCoopMutex debugger_thread_exited_mutex;
 
-static DebuggerProfiler debugger_profiler;
-
 /* The single step request instance */
 static SingleStepReq *ss_req;
 
@@ -749,7 +742,7 @@ static void thread_startup (MonoProfiler *prof, uintptr_t tid);
 
 static void thread_end (MonoProfiler *prof, uintptr_t tid);
 
-static void appdomain_load (MonoProfiler *prof, MonoDomain *domain, int result);
+static void appdomain_load (MonoProfiler *prof, MonoDomain *domain);
 
 static void appdomain_start_unload (MonoProfiler *prof, MonoDomain *domain);
 
@@ -761,7 +754,7 @@ static void emit_thread_start (gpointer key, gpointer value, gpointer user_data)
 
 static void invalidate_each_thread (gpointer key, gpointer value, gpointer user_data);
 
-static void assembly_load (MonoProfiler *prof, MonoAssembly *assembly, int result);
+static void assembly_load (MonoProfiler *prof, MonoAssembly *assembly);
 
 static void assembly_unload (MonoProfiler *prof, MonoAssembly *assembly);
 
@@ -769,7 +762,11 @@ static void emit_assembly_load (gpointer assembly, gpointer user_data);
 
 static void emit_type_load (gpointer key, gpointer type, gpointer user_data);
 
-static void jit_end (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *jinfo, int result);
+static void jit_done (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *jinfo);
+
+static void jit_failed (MonoProfiler *prof, MonoMethod *method);
+
+static void jit_end (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *jinfo);
 
 static void add_pending_breakpoints (MonoMethod *method, MonoJitInfo *jinfo);
 
@@ -995,13 +992,18 @@ mono_debugger_agent_init (void)
        mono_coop_mutex_init (&debugger_thread_exited_mutex);
        mono_coop_cond_init (&debugger_thread_exited_cond);
 
-       mono_profiler_install ((MonoProfiler*)&debugger_profiler, runtime_shutdown);
-       mono_profiler_set_events ((MonoProfileFlags)(MONO_PROFILE_APPDOMAIN_EVENTS | MONO_PROFILE_THREADS | MONO_PROFILE_ASSEMBLY_EVENTS | MONO_PROFILE_JIT_COMPILATION | MONO_PROFILE_METHOD_EVENTS));
-       mono_profiler_install_runtime_initialized (runtime_initialized);
-       mono_profiler_install_appdomain (NULL, appdomain_load, appdomain_start_unload, appdomain_unload);
-       mono_profiler_install_thread (thread_startup, thread_end);
-       mono_profiler_install_assembly (NULL, assembly_load, assembly_unload, NULL);
-       mono_profiler_install_jit_end (jit_end);
+       MonoProfilerHandle prof = mono_profiler_create (NULL);
+       mono_profiler_set_runtime_shutdown_end_callback (prof, runtime_shutdown);
+       mono_profiler_set_runtime_initialized_callback (prof, runtime_initialized);
+       mono_profiler_set_domain_loaded_callback (prof, appdomain_load);
+       mono_profiler_set_domain_unloading_callback (prof, appdomain_start_unload);
+       mono_profiler_set_domain_unloaded_callback (prof, appdomain_unload);
+       mono_profiler_set_thread_started_callback (prof, thread_startup);
+       mono_profiler_set_thread_stopped_callback (prof, thread_end);
+       mono_profiler_set_assembly_loaded_callback (prof, assembly_load);
+       mono_profiler_set_assembly_unloading_callback (prof, assembly_unload);
+       mono_profiler_set_jit_done_callback (prof, jit_done);
+       mono_profiler_set_jit_failed_callback (prof, jit_failed);
 
        mono_native_tls_alloc (&debugger_tls_id, NULL);
 
@@ -2668,7 +2670,7 @@ thread_interrupt (DebuggerTlsData *tls, MonoThreadInfo *info, MonoJitInfo *ji)
                                }
 
                                copy_unwind_state_from_frame_data (&tls->async_state, &data, jit_tls);
-                               copy_unwind_state_from_frame_data (&tls->context, &data, jit_tls);
+                               /* Don't set tls->context, it could race with the thread processing a breakpoint etc. */
                        } else {
                                tls->async_state.valid = FALSE;
                        }
@@ -3660,13 +3662,10 @@ process_event (EventKind event, gpointer arg, gint32 il_offset, MonoContext *ctx
                        return;
 
                if (agent_config.defer) {
-                       /* Make sure the thread id is always set when doing deferred debugging */
                        if (is_debugger_thread ()) {
                                /* Don't suspend on events from the debugger thread */
                                suspend_policy = SUSPEND_POLICY_NONE;
-                               thread = mono_thread_get_main ();
                        }
-                       else thread = mono_thread_current ();
                } else {
                        if (is_debugger_thread () && event != EVENT_KIND_VM_DEATH)
                                // FIXME: Send these with a NULL thread, don't suspend the current thread
@@ -3689,7 +3688,7 @@ process_event (EventKind event, gpointer arg, gint32 il_offset, MonoContext *ctx
                        thread = NULL;
                } else {
                        if (!thread)
-                               thread = mono_thread_current ();
+                               thread = is_debugger_thread () ? mono_thread_get_main () : mono_thread_current ();
 
                        if (event == EVENT_KIND_VM_START && arg != NULL)
                                thread = (MonoThread *)arg;
@@ -3960,7 +3959,7 @@ thread_end (MonoProfiler *prof, uintptr_t tid)
 }
 
 static void
-appdomain_load (MonoProfiler *prof, MonoDomain *domain, int result)
+appdomain_load (MonoProfiler *prof, MonoDomain *domain)
 {
        mono_loader_lock ();
        g_hash_table_insert (domains, domain, domain);
@@ -4022,7 +4021,7 @@ invalidate_each_thread (gpointer key, gpointer value, gpointer user_data)
 }
 
 static void
-assembly_load (MonoProfiler *prof, MonoAssembly *assembly, int result)
+assembly_load (MonoProfiler *prof, MonoAssembly *assembly)
 {
        /* Sent later in jit_end () */
        dbg_lock ();
@@ -4110,7 +4109,19 @@ send_assemblies_for_domain (MonoDomain *domain, void *user_data)
 }
 
 static void
-jit_end (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *jinfo, int result)
+jit_done (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *jinfo)
+{
+       jit_end (prof, method, jinfo);
+}
+
+static void
+jit_failed (MonoProfiler *prof, MonoMethod *method)
+{
+       jit_end (prof, method, NULL);
+}
+
+static void
+jit_end (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *jinfo)
 {
        /*
         * We emit type load events when the first method of the type is JITted,
@@ -4138,7 +4149,7 @@ jit_end (MonoProfiler *prof, MonoMethod *method, MonoJitInfo *jinfo, int result)
 
        send_type_load (method->klass);
 
-       if (!result && jinfo)
+       if (jinfo)
                add_pending_breakpoints (method, jinfo);
 }
 
@@ -4735,25 +4746,17 @@ breakpoint_matches_assembly (MonoBreakpoint *bp, MonoAssembly *assembly)
        return bp->method && bp->method->klass->image->assembly == assembly;
 }
 
-static MonoObject*
-get_this (StackFrame *frame)
+static gpointer
+get_this_addr (StackFrame *frame)
 {
        //Logic inspiered by "add_var" method and took out path that happens in async method for getting this
        MonoDebugVarInfo *var = frame->jit->this_var;
        if ((var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS) != MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET)
                return NULL;
 
-       guint8 * addr = (guint8 *)mono_arch_context_get_int_reg (&frame->ctx, var->index & ~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS);
+       guint8 *addr = (guint8 *)mono_arch_context_get_int_reg (&frame->ctx, var->index & ~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS);
        addr += (gint32)var->offset;
-       return *(MonoObject**)addr;
-}
-
-//This ID is used to figure out if breakpoint hit on resumeOffset belongs to us or not
-//since thread probably changed...
-static int
-get_this_async_id (StackFrame *frame)
-{
-       return get_objid (get_this (frame));
+       return addr;
 }
 
 static MonoMethod*
@@ -4768,31 +4771,106 @@ get_set_notification_method (MonoClass* async_builder_class)
        return set_notification_method;
 }
 
-static void
-set_set_notification_for_wait_completion_flag (StackFrame *frame)
+static MonoMethod*
+get_object_id_for_debugger_method (MonoClass* async_builder_class)
 {
-       MonoObject* obj = get_this (frame);
-       g_assert (obj);
-       MonoClassField *builder_field = mono_class_get_field_from_name (obj->vtable->klass, "<>t__builder");
+       MonoError error;
+       GPtrArray *array = mono_class_get_methods_by_name (async_builder_class, "get_ObjectIdForDebugger", 0x24, FALSE, FALSE, &error);
+       mono_error_assert_ok (&error);
+       g_assert (array->len == 1);
+       MonoMethod *method = (MonoMethod *)g_ptr_array_index (array, 0);
+       g_ptr_array_free (array, TRUE);
+       return method;
+}
+
+/* Return the address of the AsyncMethodBuilder struct belonging to the state machine method pointed to by FRAME */
+static gpointer
+get_async_method_builder (StackFrame *frame)
+{
+       MonoObject *this_obj;
+       MonoClassField *builder_field;
+       gpointer builder;
+       guint8 *this_addr;
+
+       builder_field = mono_class_get_field_from_name (frame->method->klass, "<>t__builder");
        g_assert (builder_field);
-       MonoObject* builder;
+
+       this_addr = get_this_addr (frame);
+       if (!this_addr)
+               return NULL;
+
+       if (frame->method->klass->valuetype) {
+               guint8 *vtaddr = *(guint8**)this_addr;
+               builder = (char*)vtaddr + builder_field->offset - sizeof (MonoObject);
+       } else {
+               this_obj = *(MonoObject**)this_addr;
+               builder = (char*)this_obj + builder_field->offset;
+       }
+
+       return builder;
+}
+
+//This ID is used to figure out if breakpoint hit on resumeOffset belongs to us or not
+//since thread probably changed...
+static int
+get_this_async_id (StackFrame *frame)
+{
+       MonoClassField *builder_field;
+       gpointer builder;
+       MonoMethod *method;
+       MonoObject *ex;
        MonoError error;
-       builder = mono_field_get_value_object_checked (frame->domain, builder_field, obj, &error);
+       MonoObject *obj;
+       gboolean old_disable_breakpoints = FALSE;
+       DebuggerTlsData *tls;
+
+       /*
+        * FRAME points to a method in a state machine class/struct.
+        * Call the ObjectIdForDebugger method of the associated method builder type.
+        */
+       builder = get_async_method_builder (frame);
+       if (!builder)
+               return 0;
+
+       builder_field = mono_class_get_field_from_name (frame->method->klass, "<>t__builder");
+       g_assert (builder_field);
+
+       tls = (DebuggerTlsData *)mono_native_tls_get_value (debugger_tls_id);
+       if (tls) {
+               old_disable_breakpoints = tls->disable_breakpoints;
+               tls->disable_breakpoints = TRUE;
+       }
+
+       method = get_object_id_for_debugger_method (mono_class_from_mono_type (builder_field->type));
+       obj = mono_runtime_try_invoke (method, builder, NULL, &ex, &error);
        mono_error_assert_ok (&error);
+
+       if (tls)
+               tls->disable_breakpoints = old_disable_breakpoints;
+
+       return get_objid (obj);
+}
+
+static void
+set_set_notification_for_wait_completion_flag (StackFrame *frame)
+{
+       MonoClassField *builder_field = mono_class_get_field_from_name (frame->method->klass, "<>t__builder");
+       g_assert (builder_field);
+       gpointer builder = get_async_method_builder (frame);
        g_assert (builder);
 
        void* args [1];
        gboolean arg = TRUE;
+       MonoError error;
        args [0] = &arg;
-       mono_runtime_invoke_checked (get_set_notification_method (builder->vtable->klass), mono_object_unbox (builder), args, &error);
+       mono_runtime_invoke_checked (get_set_notification_method (mono_class_from_mono_type (builder_field->type)), builder, args, &error);
        mono_error_assert_ok (&error);
-       mono_field_set_value (obj, builder_field, mono_object_unbox (builder));
 }
 
 static MonoMethod* notify_debugger_of_wait_completion_method_cache = NULL;
 
 static MonoMethod*
-get_notify_debugger_of_wait_completion_method ()
+get_notify_debugger_of_wait_completion_method (void)
 {
        if (notify_debugger_of_wait_completion_method_cache != NULL)
                return notify_debugger_of_wait_completion_method_cache;
@@ -5927,7 +6005,7 @@ mono_debugger_agent_debug_log_is_enabled (void)
        return agent_config.enabled;
 }
 
-#if defined(PLATFORM_ANDROID) || defined(TARGET_ANDROID)
+#if defined(HOST_ANDROID) || defined(TARGET_ANDROID)
 void
 mono_debugger_agent_unhandled_exception (MonoException *exc)
 {
@@ -6968,7 +7046,7 @@ do_invoke_method (DebuggerTlsData *tls, Buffer *buf, InvokeData *invoke, guint8
        MonoMethodSignature *sig;
        guint8 **arg_buf;
        void **args;
-       MonoObject *this_arg, *res, *exc;
+       MonoObject *this_arg, *res, *exc = NULL;
        MonoDomain *domain;
        guint8 *this_buf;
 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
@@ -7140,7 +7218,7 @@ do_invoke_method (DebuggerTlsData *tls, Buffer *buf, InvokeData *invoke, guint8
 
        mono_stopwatch_start (&watch);
        res = mono_runtime_try_invoke (m, m->klass->valuetype ? (gpointer) this_buf : (gpointer) this_arg, args, &exc, &error);
-       if (exc == NULL && !mono_error_ok (&error)) {
+       if (!mono_error_ok (&error) && exc == NULL) {
                exc = (MonoObject*) mono_error_convert_to_exception (&error);
        } else {
                mono_error_cleanup (&error); /* FIXME report error */
@@ -7613,7 +7691,7 @@ vm_commands (int command, int id, guint8 *p, guint8 *end, Buffer *buf)
 
                tls->abort_requested = TRUE;
 
-               mono_thread_internal_abort (THREAD_TO_INTERNAL (thread));
+               mono_thread_internal_abort (THREAD_TO_INTERNAL (thread), FALSE);
                mono_loader_unlock ();
                break;
        }