Merge pull request #3040 from xmcclure/debugger-step-recursive
[mono.git] / mono / mini / debugger-agent.c
index 017315fe02f61be11d1d21b8fd66a520fdd4ebe5..e567ed30fa797879ff151a594832f666603638f2 100644 (file)
@@ -1953,7 +1953,7 @@ get_objref (MonoObject *obj)
        int hash = 0;
 
        if (obj == NULL)
-               return 0;
+               return NULL;
 
        if (suspend_count) {
                /*
@@ -2028,7 +2028,10 @@ clear_suspended_objs (void)
 static inline int
 get_objid (MonoObject *obj)
 {
-       return get_objref (obj)->id;
+       if (!obj)
+               return 0;
+       else
+               return get_objref (obj)->id;
 }
 
 /*
@@ -2678,7 +2681,7 @@ notify_thread (gpointer key, gpointer value, gpointer user_data)
 
        DEBUG_PRINTF (1, "[%p] Interrupting %p...\n", (gpointer) (gsize) mono_native_thread_id_get (), (gpointer)tid);
 
-       /* This is _not_ equivalent to ves_icall_System_Threading_Thread_Abort () */
+       /* This is _not_ equivalent to mono_thread_internal_abort () */
        InterruptData interrupt_data = { 0 };
        interrupt_data.tls = tls;
 
@@ -3626,11 +3629,15 @@ process_event (EventKind event, gpointer arg, gint32 il_offset, MonoContext *ctx
 
                ecount ++;
 
-               if (!thread)
-                       thread = mono_thread_current ();
+               if (event == EVENT_KIND_VM_DEATH) {
+                       thread = NULL;
+               } else {
+                       if (!thread)
+                               thread = mono_thread_current ();
 
-               if (event == EVENT_KIND_VM_START && arg != NULL)
-                       thread = (MonoThread *)arg;
+                       if (event == EVENT_KIND_VM_START && arg != NULL)
+                               thread = (MonoThread *)arg;
+               }
 
                buffer_add_objid (&buf, (MonoObject*)thread); // thread
 
@@ -3792,7 +3799,7 @@ runtime_initialized (MonoProfiler *prof)
 static void
 runtime_shutdown (MonoProfiler *prof)
 {
-       process_profiler_event (EVENT_KIND_VM_DEATH, mono_thread_current ());
+       process_profiler_event (EVENT_KIND_VM_DEATH, NULL);
 
        mono_debugger_agent_cleanup ();
 }
@@ -4320,9 +4327,12 @@ set_bp_in_method (MonoDomain *domain, MonoMethod *method, MonoSeqPointInfo *seq_
 
        code = mono_jit_find_compiled_method_with_jit_info (domain, method, &ji);
        if (!code) {
+               MonoError oerror;
+
                /* Might be AOTed code */
-               code = mono_aot_get_method (domain, method);
+               code = mono_aot_get_method_checked (domain, method, &oerror);
                g_assert (code);
+               mono_error_assert_ok (&oerror);
                ji = mono_jit_info_table_find (domain, (char *)code);
                g_assert (ji);
        }
@@ -5525,6 +5535,7 @@ ss_clear_for_assembly (SingleStepReq *req, MonoAssembly *assembly)
 void
 mono_debugger_agent_debug_log (int level, MonoString *category, MonoString *message)
 {
+       MonoError error;
        int suspend_policy;
        GSList *events;
        EventInfo ei;
@@ -5537,8 +5548,16 @@ mono_debugger_agent_debug_log (int level, MonoString *category, MonoString *mess
        mono_loader_unlock ();
 
        ei.level = level;
-       ei.category = category ? mono_string_to_utf8 (category) : NULL;
-       ei.message = message ? mono_string_to_utf8 (message) : NULL;
+       ei.category = NULL;
+       if (category) {
+               ei.category = mono_string_to_utf8_checked (category, &error);
+               mono_error_cleanup (&error);
+       }
+       ei.message = NULL;
+       if (message) {
+               ei.message = mono_string_to_utf8_checked (message, &error);
+               mono_error_cleanup  (&error);
+       }
 
        process_event (EVENT_KIND_USER_LOG, &ei, 0, NULL, events, suspend_policy);
 
@@ -6900,7 +6919,7 @@ invoke_method (void)
        /*
         * Take the loader lock to avoid race conditions with CMD_VM_ABORT_INVOKE:
         *
-        * It is possible that ves_icall_System_Threading_Thread_Abort () was called
+        * It is possible that mono_thread_internal_abort () was called
         * after the mono_runtime_invoke_checked() already returned, but it doesn't matter
         * because we reset the abort here.
         */
@@ -7212,7 +7231,7 @@ vm_commands (int command, int id, guint8 *p, guint8 *end, Buffer *buf)
 
                tls->abort_requested = TRUE;
 
-               ves_icall_System_Threading_Thread_Abort (THREAD_TO_INTERNAL (thread), NULL);
+               mono_thread_internal_abort (THREAD_TO_INTERNAL (thread));
                mono_loader_unlock ();
                break;
        }
@@ -7882,7 +7901,7 @@ field_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
 static void
 buffer_add_cattr_arg (Buffer *buf, MonoType *t, MonoDomain *domain, MonoObject *val)
 {
-       if (val && val->vtable->klass == mono_defaults.monotype_class) {
+       if (val && val->vtable->klass == mono_defaults.runtimetype_class) {
                /* Special case these so the client doesn't have to handle Type objects */
                
                buffer_add_byte (buf, VALUE_TYPE_ID_TYPE);
@@ -8764,13 +8783,15 @@ method_commands_internal (int command, MonoMethod *method, MonoDomain *domain, g
                // FIXME: Generics
                switch (mono_metadata_token_code (token)) {
                case MONO_TOKEN_STRING: {
+                       MonoError error;
                        MonoString *s;
                        char *s2;
 
-                       s = mono_ldstr (domain, method->klass->image, mono_metadata_token_index (token));
-                       g_assert (s);
+                       s = mono_ldstr_checked (domain, method->klass->image, mono_metadata_token_index (token), &error);
+                       mono_error_assert_ok (&error); /* FIXME don't swallow the error */
 
-                       s2 = mono_string_to_utf8 (s);
+                       s2 = mono_string_to_utf8_checked (s, &error);
+                       mono_error_assert_ok (&error);
 
                        buffer_add_byte (buf, TOKEN_TYPE_STRING);
                        buffer_add_string (buf, s2);
@@ -8778,6 +8799,7 @@ method_commands_internal (int command, MonoMethod *method, MonoDomain *domain, g
                        break;
                }
                default: {
+                       MonoError error;
                        gpointer val;
                        MonoClass *handle_class;
 
@@ -8791,7 +8813,6 @@ method_commands_internal (int command, MonoMethod *method, MonoDomain *domain, g
                                        break;
                                }
                        } else {
-                               MonoError error;
                                val = mono_ldtoken_checked (method->klass->image, token, &handle_class, NULL, &error);
                                if (!val)
                                        g_error ("Could not load token due to %s", mono_error_get_message (&error));
@@ -8812,7 +8833,8 @@ method_commands_internal (int command, MonoMethod *method, MonoDomain *domain, g
                        } else if (handle_class == mono_defaults.string_class) {
                                char *s;
 
-                               s = mono_string_to_utf8 ((MonoString *)val);
+                               s = mono_string_to_utf8_checked ((MonoString *)val, &error);
+                               mono_error_assert_ok (&error);
                                buffer_add_byte (buf, TOKEN_TYPE_STRING);
                                buffer_add_string (buf, s);
                                g_free (s);
@@ -9328,7 +9350,9 @@ string_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
                        buffer_add_int (buf, mono_string_length (str) * 2);
                        buffer_add_data (buf, (guint8*)mono_string_chars (str), mono_string_length (str) * 2);
                } else {
-                       s = mono_string_to_utf8 (str);
+                       MonoError error;
+                       s = mono_string_to_utf8_checked (str, &error);
+                       mono_error_assert_ok (&error);
                        buffer_add_string (buf, s);
                        g_free (s);
                }
@@ -9793,18 +9817,17 @@ debugger_thread (void *arg)
        ErrorCode err;
        gboolean no_reply;
        gboolean attach_failed = FALSE;
-       gpointer attach_cookie, attach_dummy;
 
        DEBUG_PRINTF (1, "[dbg] Agent thread started, pid=%p\n", (gpointer) (gsize) mono_native_thread_id_get ());
 
        debugger_thread_id = mono_native_thread_id_get ();
 
-       attach_cookie = mono_jit_thread_attach (mono_get_root_domain (), &attach_dummy);
-       MonoInternalThread *thread = mono_thread_internal_current ();
-       mono_thread_set_name_internal (thread, mono_string_new (mono_get_root_domain (), "Debugger agent"), TRUE, &error);
+       MonoThread *thread = mono_thread_attach (mono_get_root_domain ());
+       mono_thread_set_name_internal (thread->internal_thread, mono_string_new (mono_get_root_domain (), "Debugger agent"), TRUE, &error);
        mono_error_assert_ok (&error);
 
-       thread->flags |= MONO_THREAD_FLAG_DONT_MANAGE;
+       thread->internal_thread->state |= ThreadState_Background;
+       thread->internal_thread->flags |= MONO_THREAD_FLAG_DONT_MANAGE;
 
        mono_set_is_debugger_attached (TRUE);
        
@@ -9956,8 +9979,6 @@ debugger_thread (void *arg)
                start_debugger_thread ();
        }
 
-       mono_jit_thread_detach (attach_cookie, &attach_dummy);
-
        return 0;
 }