Merge pull request #3025 from kumpera/mono_raise_exception_in_threads
[mono.git] / mono / mini / debugger-agent.c
index b2712b486eb1dcea61a4dcc220c2550953f907ca..b24853b0586836fc34b151e91751737d3fa3c283 100644 (file)
@@ -1120,10 +1120,10 @@ socket_transport_recv (void *buf, int len)
        int total = 0;
        int fd = conn_fd;
        int flags = 0;
-       static gint32 last_keepalive;
-       gint32 msecs;
+       static gint64 last_keepalive;
+       gint64 msecs;
 
-       MONO_PREPARE_BLOCKING;
+       MONO_ENTER_GC_SAFE;
 
        do {
        again:
@@ -1149,7 +1149,7 @@ socket_transport_recv (void *buf, int len)
                }
        } while ((res > 0 && total < len) || (res == -1 && get_last_sock_error () == MONO_EINTR));
 
-       MONO_FINISH_BLOCKING;
+       MONO_EXIT_GC_SAFE;
 
        return total;
 }
@@ -1173,9 +1173,9 @@ set_keepalive (void)
 static int
 socket_transport_accept (int socket_fd)
 {
-       MONO_PREPARE_BLOCKING;
+       MONO_ENTER_GC_SAFE;
        conn_fd = accept (socket_fd, NULL, NULL);
-       MONO_FINISH_BLOCKING;
+       MONO_EXIT_GC_SAFE;
 
        if (conn_fd == -1) {
                fprintf (stderr, "debugger-agent: Unable to listen on %d\n", socket_fd);
@@ -1191,13 +1191,13 @@ socket_transport_send (void *data, int len)
 {
        int res;
 
-       MONO_PREPARE_BLOCKING;
+       MONO_ENTER_GC_SAFE;
 
        do {
                res = send (conn_fd, data, len, 0);
        } while (res == -1 && get_last_sock_error () == MONO_EINTR);
 
-       MONO_FINISH_BLOCKING;
+       MONO_EXIT_GC_SAFE;
 
        if (res != len)
                return FALSE;
@@ -1316,9 +1316,9 @@ socket_transport_connect (const char *address)
                        FD_ZERO (&readfds);
                        FD_SET (sfd, &readfds);
 
-                       MONO_PREPARE_BLOCKING;
+                       MONO_ENTER_GC_SAFE;
                        res = select (sfd + 1, &readfds, NULL, NULL, &tv);
-                       MONO_FINISH_BLOCKING;
+                       MONO_EXIT_GC_SAFE;
 
                        if (res == 0) {
                                fprintf (stderr, "debugger-agent: Timed out waiting to connect.\n");
@@ -1345,16 +1345,16 @@ socket_transport_connect (const char *address)
                        if (sfd == -1)
                                continue;
 
-                       MONO_PREPARE_BLOCKING;
+                       MONO_ENTER_GC_SAFE;
                        res = connect (sfd, &sockaddr.addr, sock_len);
-                       MONO_FINISH_BLOCKING;
+                       MONO_EXIT_GC_SAFE;
 
                        if (res != -1)
                                break;       /* Success */
                        
-                       MONO_PREPARE_BLOCKING;
+                       MONO_ENTER_GC_SAFE;
                        close (sfd);
-                       MONO_FINISH_BLOCKING;
+                       MONO_EXIT_GC_SAFE;
                }
 
                if (rp == 0) {
@@ -1385,9 +1385,9 @@ socket_transport_close1 (void)
 #else
        shutdown (conn_fd, SHUT_RD);
        shutdown (listen_fd, SHUT_RDWR);
-       MONO_PREPARE_BLOCKING;
+       MONO_ENTER_GC_SAFE;
        close (listen_fd);
-       MONO_FINISH_BLOCKING;
+       MONO_EXIT_GC_SAFE;
 #endif
 }
 
@@ -2678,7 +2678,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;
 
@@ -6802,7 +6802,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.
         */
@@ -7114,7 +7114,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;
        }
@@ -7258,7 +7258,7 @@ vm_commands (int command, int id, guint8 *p, guint8 *end, Buffer *buf)
                                        MonoError error;
                                        type_resolve = TRUE;
                                        /* FIXME really okay to call while holding locks? */
-                                       t = mono_reflection_get_type_checked (ass->image, &info, ignore_case, &type_resolve, &error);
+                                       t = mono_reflection_get_type_checked (ass->image, ass->image, &info, ignore_case, &type_resolve, &error);
                                        mono_error_cleanup (&error); 
                                        if (t) {
                                                g_ptr_array_add (res_classes, mono_type_get_class (t));
@@ -7692,7 +7692,7 @@ assembly_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
                } else {
                        if (info.assembly.name)
                                NOT_IMPLEMENTED;
-                       t = mono_reflection_get_type_checked (ass->image, &info, ignorecase, &type_resolve, &error);
+                       t = mono_reflection_get_type_checked (ass->image, ass->image, &info, ignorecase, &type_resolve, &error);
                        if (!is_ok (&error)) {
                                mono_error_cleanup (&error); /* FIXME don't swallow the error */
                                mono_reflection_free_type_info (&info);
@@ -8159,7 +8159,9 @@ type_commands_internal (int command, MonoClass *klass, MonoDomain *domain, guint
 
                        vtable = mono_class_vtable (domain, f->parent);
                        val = (guint8 *)g_malloc (mono_class_instance_size (mono_class_from_mono_type (f->type)));
-                       mono_field_static_get_value_for_thread (thread ? thread : mono_thread_internal_current (), vtable, f, val);
+                       mono_field_static_get_value_for_thread (thread ? thread : mono_thread_internal_current (), vtable, f, val, &error);
+                       if (!is_ok (&error))
+                               return ERR_INVALID_FIELDID;
                        buffer_add_value (buf, f->type, val, domain);
                        g_free (val);
                }
@@ -8664,11 +8666,12 @@ 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);
 
@@ -9255,6 +9258,7 @@ string_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
 static ErrorCode
 object_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
 {
+       MonoError error;
        int objid;
        ErrorCode err;
        MonoObject *obj;
@@ -9323,7 +9327,11 @@ object_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
                                g_assert (f->type->attrs & FIELD_ATTRIBUTE_STATIC);
                                vtable = mono_class_vtable (obj->vtable->domain, f->parent);
                                val = (guint8 *)g_malloc (mono_class_instance_size (mono_class_from_mono_type (f->type)));
-                               mono_field_static_get_value (vtable, f, val);
+                               mono_field_static_get_value_checked (vtable, f, val, &error);
+                               if (!is_ok (&error)) {
+                                       mono_error_cleanup (&error); /* FIXME report the error */
+                                       return ERR_INVALID_OBJECT;
+                               }
                                buffer_add_value (buf, f->type, val, obj->vtable->domain);
                                g_free (val);
                        } else {
@@ -9332,7 +9340,11 @@ object_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
 
                                if (remote_obj) {
 #ifndef DISABLE_REMOTING
-                                       field_value = mono_load_remote_field(obj, obj_type, f, &field_storage);
+                                       field_value = mono_load_remote_field_checked(obj, obj_type, f, &field_storage, &error);
+                                       if (!is_ok (&error)) {
+                                               mono_error_cleanup (&error); /* FIXME report the error */
+                                               return ERR_INVALID_OBJECT;
+                                       }
 #else
                                        g_assert_not_reached ();
 #endif
@@ -9675,6 +9687,7 @@ wait_for_attach (void)
 static guint32 WINAPI
 debugger_thread (void *arg)
 {
+       MonoError error;
        int res, len, id, flags, command = 0;
        CommandSet command_set = (CommandSet)0;
        guint8 header [HEADER_LENGTH];
@@ -9690,8 +9703,11 @@ debugger_thread (void *arg)
        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);
+       mono_error_assert_ok (&error);
 
-       mono_thread_internal_current ()->flags |= MONO_THREAD_FLAG_DONT_MANAGE;
+       thread->flags |= MONO_THREAD_FLAG_DONT_MANAGE;
 
        mono_set_is_debugger_attached (TRUE);