[threads] Remove mono_threads_create_thread (#4411)
[mono.git] / mono / mini / debugger-agent.c
index f64481d661fc599ae756a683e236674d61ba2d7d..e18cb98d643bd015f474be8bec56aa36e03706b6 100644 (file)
@@ -53,8 +53,7 @@
 #endif
 
 #include <mono/metadata/mono-debug.h>
-#include <mono/metadata/mono-debug-debugger.h>
-#include <mono/metadata/debug-mono-symfile.h>
+#include <mono/metadata/debug-internals.h>
 #include <mono/metadata/gc-internals.h>
 #include <mono/metadata/environment.h>
 #include <mono/metadata/threads-types.h>
@@ -819,7 +818,13 @@ register_socket_transport (void);
 static inline gboolean
 is_debugger_thread (void)
 {
-       return mono_native_thread_id_equals (mono_native_thread_id_get (), debugger_thread_id);
+       MonoInternalThread *internal;
+
+       internal = mono_thread_internal_current ();
+       if (!internal)
+               return FALSE;
+
+       return internal->debugger_thread;
 }
 
 static int
@@ -830,9 +835,10 @@ parse_address (char *address, char **host, int *port)
        if (pos == NULL || pos == address)
                return 1;
 
-       *host = (char *)g_malloc (pos - address + 1);
-       strncpy (*host, address, pos - address);
-       (*host) [pos - address] = '\0';
+       size_t len = pos - address;
+       *host = (char *)g_malloc (len + 1);
+       memcpy (*host, address, len);
+       (*host) [len] = '\0';
 
        *port = atoi (pos + 1);
 
@@ -997,14 +1003,11 @@ mono_debugger_agent_init (void)
        /* Needed by the hash_table_new_type () call below */
        mono_gc_base_init ();
 
-       thread_to_tls = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_KEY_GC, MONO_ROOT_SOURCE_DEBUGGER, "thread-to-tls table");
-       MONO_GC_REGISTER_ROOT_FIXED (thread_to_tls, MONO_ROOT_SOURCE_DEBUGGER, "thread-to-tls table");
+       thread_to_tls = mono_g_hash_table_new_type ((GHashFunc)mono_object_hash, NULL, MONO_HASH_KEY_GC, MONO_ROOT_SOURCE_DEBUGGER, "thread-to-tls table");
 
        tid_to_thread = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_VALUE_GC, MONO_ROOT_SOURCE_DEBUGGER, "tid-to-thread table");
-       MONO_GC_REGISTER_ROOT_FIXED (tid_to_thread, MONO_ROOT_SOURCE_DEBUGGER, "tid-to-thread table");
 
        tid_to_thread_obj = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_VALUE_GC, MONO_ROOT_SOURCE_DEBUGGER, "tid-to-thread object table");
-       MONO_GC_REGISTER_ROOT_FIXED (tid_to_thread_obj, MONO_ROOT_SOURCE_DEBUGGER, "tid-to-thread object table");
 
        pending_assembly_loads = g_ptr_array_new ();
        domains = g_hash_table_new (mono_aligned_addr_hash, NULL);
@@ -1637,7 +1640,13 @@ stop_debugger_thread (void)
 static void
 start_debugger_thread (void)
 {
-       debugger_thread_handle = mono_threads_create_thread (debugger_thread, NULL, NULL, NULL);
+       MonoError error;
+       MonoInternalThread *thread;
+
+       thread = mono_thread_create_internal (mono_get_root_domain (), debugger_thread, NULL, MONO_THREAD_CREATE_FLAGS_DEBUGGER, &error);
+       mono_error_assert_ok (&error);
+
+       debugger_thread_handle = mono_threads_open_thread_handle (thread->handle);
        g_assert (debugger_thread_handle);
 }
 
@@ -1941,8 +1950,7 @@ objrefs_init (void)
 {
        objrefs = g_hash_table_new_full (NULL, NULL, NULL, free_objref);
        obj_to_objref = g_hash_table_new (NULL, NULL);
-       suspended_objs = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_KEY_GC, MONO_ROOT_SOURCE_DEBUGGER, "suspended objects table");
-       MONO_GC_REGISTER_ROOT_FIXED (suspended_objs, MONO_ROOT_SOURCE_DEBUGGER, "suspended objects table");
+       suspended_objs = mono_g_hash_table_new_type ((GHashFunc)mono_object_hash, NULL, MONO_HASH_KEY_GC, MONO_ROOT_SOURCE_DEBUGGER, "suspended objects table");
 }
 
 static void
@@ -2686,7 +2694,7 @@ notify_thread (gpointer key, gpointer value, gpointer user_data)
        DebuggerTlsData *tls = (DebuggerTlsData *)value;
        MonoNativeThreadId tid = MONO_UINT_TO_NATIVE_THREAD_ID (thread->tid);
 
-       if (mono_native_thread_id_equals (mono_native_thread_id_get (), tid) || tls->terminated)
+       if (mono_thread_internal_is_current (thread) || tls->terminated)
                return;
 
        DEBUG_PRINTF (1, "[%p] Interrupting %p...\n", (gpointer) (gsize) mono_native_thread_id_get (), (gpointer)tid);
@@ -2958,7 +2966,7 @@ count_thread (gpointer key, gpointer value, gpointer user_data)
 {
        DebuggerTlsData *tls = (DebuggerTlsData *)value;
 
-       if (!tls->suspended && !tls->terminated)
+       if (!tls->suspended && !tls->terminated && !mono_thread_internal_is_current (tls->thread))
                *(int*)user_data = *(int*)user_data + 1;
 }
 
@@ -3252,8 +3260,8 @@ emit_appdomain_load (gpointer key, gpointer value, gpointer user_data)
 static void
 emit_thread_start (gpointer key, gpointer value, gpointer user_data)
 {
-       if (!mono_native_thread_id_equals (MONO_UINT_TO_NATIVE_THREAD_ID (GPOINTER_TO_UINT (key)), debugger_thread_id))
-               process_profiler_event (EVENT_KIND_THREAD_START, value);
+       g_assert (!mono_native_thread_id_equals (MONO_UINT_TO_NATIVE_THREAD_ID (GPOINTER_TO_UINT (key)), debugger_thread_id));
+       process_profiler_event (EVENT_KIND_THREAD_START, value);
 }
 
 /*
@@ -3814,7 +3822,7 @@ thread_startup (MonoProfiler *prof, uintptr_t tid)
        MonoInternalThread *old_thread;
        DebuggerTlsData *tls;
 
-       if (mono_native_thread_id_equals (MONO_UINT_TO_NATIVE_THREAD_ID (tid), debugger_thread_id))
+       if (is_debugger_thread ())
                return;
 
        g_assert (mono_native_thread_id_equals (MONO_UINT_TO_NATIVE_THREAD_ID (tid), MONO_UINT_TO_NATIVE_THREAD_ID (thread->tid)));
@@ -3893,8 +3901,7 @@ thread_end (MonoProfiler *prof, uintptr_t tid)
        if (thread) {
                DEBUG_PRINTF (1, "[%p] Thread terminated, obj=%p, tls=%p.\n", (gpointer)tid, thread, tls);
 
-               if (mono_native_thread_id_equals (mono_native_thread_id_get (), MONO_UINT_TO_NATIVE_THREAD_ID (tid))
-                    && !mono_native_tls_get_value (debugger_tls_id)
+               if (mono_thread_internal_is_current (thread) && !mono_native_tls_get_value (debugger_tls_id)
                ) {
                        /*
                         * This can happen on darwin since we deregister threads using pthread dtors.
@@ -4153,7 +4160,7 @@ insert_breakpoint (MonoSeqPointInfo *seq_points, MonoDomain *domain, MonoJitInfo
        gboolean it_has_sp = FALSE;
 
        if (error)
-               mono_error_init (error);
+               error_init (error);
 
        mono_seq_point_iterator_init (&it, seq_points);
        while (mono_seq_point_iterator_next (&it)) {
@@ -4353,7 +4360,7 @@ set_bp_in_method (MonoDomain *domain, MonoMethod *method, MonoSeqPointInfo *seq_
        MonoJitInfo *ji;
 
        if (error)
-               mono_error_init (error);
+               error_init (error);
 
        code = mono_jit_find_compiled_method_with_jit_info (domain, method, &ji);
        if (!code) {
@@ -4399,7 +4406,7 @@ set_breakpoint (MonoMethod *method, long il_offset, EventRequest *req, MonoError
        int i;
 
        if (error)
-               mono_error_init (error);
+               error_init (error);
 
        // FIXME:
        // - suspend/resume the vm to prevent code patching problems
@@ -8011,7 +8018,7 @@ get_assembly_object_command (MonoDomain *domain, MonoAssembly *ass, Buffer *buf,
 {
        HANDLE_FUNCTION_ENTER();
        ErrorCode err = ERR_NONE;
-       mono_error_init (error);
+       error_init (error);
        MonoReflectionAssemblyHandle o = mono_assembly_get_object_handle (domain, ass, error);
        if (MONO_HANDLE_IS_NULL (o)) {
                err = ERR_INVALID_OBJECT;
@@ -8662,7 +8669,7 @@ type_commands_internal (int command, MonoClass *klass, MonoDomain *domain, guint
                MonoError error;
                GPtrArray *array;
 
-               mono_error_init (&error);
+               error_init (&error);
                array = mono_class_get_methods_by_name (klass, name, flags & ~BINDING_FLAGS_IGNORE_CASE, (flags & BINDING_FLAGS_IGNORE_CASE) != 0, TRUE, &error);
                if (!is_ok (&error)) {
                        mono_error_cleanup (&error);
@@ -10135,12 +10142,12 @@ debugger_thread (void *arg)
 
        debugger_thread_id = mono_native_thread_id_get ();
 
-       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);
+       MonoInternalThread *internal = mono_thread_internal_current ();
+       mono_thread_set_name_internal (internal, mono_string_new (mono_domain_get (), "Debugger agent"), TRUE, FALSE, &error);
        mono_error_assert_ok (&error);
 
-       thread->internal_thread->state |= ThreadState_Background;
-       thread->internal_thread->flags |= MONO_THREAD_FLAG_DONT_MANAGE;
+       internal->state |= ThreadState_Background;
+       internal->flags |= MONO_THREAD_FLAG_DONT_MANAGE;
 
        if (agent_config.defer) {
                if (!wait_for_attach ()) {