Merge pull request #2820 from kumpera/license-change-rebased
[mono.git] / mono / mini / debugger-agent.c
index f0b4d4f0201c4985c0232ad5539d67ec0e8655d5..b2712b486eb1dcea61a4dcc220c2550953f907ca 100644 (file)
@@ -6,6 +6,7 @@
  *
  * Copyright 2009-2010 Novell, Inc.
  * Copyright 2011 Xamarin Inc.
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  */
 
 #include <config.h>
@@ -230,7 +231,7 @@ typedef struct {
        gboolean abort_requested;
 
        /*
-        * The current mono_runtime_invoke invocation.
+        * The current mono_runtime_invoke_checked invocation.
         */
        InvokeData *invoke;
 
@@ -3323,25 +3324,23 @@ static void
 init_jit_info_dbg_attrs (MonoJitInfo *ji)
 {
        static MonoClass *hidden_klass, *step_through_klass, *non_user_klass;
+       MonoError error;
        MonoCustomAttrInfo *ainfo;
 
        if (ji->dbg_attrs_inited)
                return;
 
-       if (!hidden_klass) {
-               hidden_klass = mono_class_from_name (mono_defaults.corlib, "System.Diagnostics", "DebuggerHiddenAttribute");
-               g_assert (hidden_klass);
-       }
-       if (!step_through_klass) {
-               step_through_klass = mono_class_from_name (mono_defaults.corlib, "System.Diagnostics", "DebuggerStepThroughAttribute");
-               g_assert (step_through_klass);
-       }
-       if (!non_user_klass) {
-               non_user_klass = mono_class_from_name (mono_defaults.corlib, "System.Diagnostics", "DebuggerNonUserCodeAttribute");
-               g_assert (non_user_klass);
-       }
+       if (!hidden_klass)
+               hidden_klass = mono_class_load_from_name (mono_defaults.corlib, "System.Diagnostics", "DebuggerHiddenAttribute");
 
-       ainfo = mono_custom_attrs_from_method (jinfo_get_method (ji));
+       if (!step_through_klass)
+               step_through_klass = mono_class_load_from_name (mono_defaults.corlib, "System.Diagnostics", "DebuggerStepThroughAttribute");
+
+       if (!non_user_klass)
+               non_user_klass = mono_class_load_from_name (mono_defaults.corlib, "System.Diagnostics", "DebuggerNonUserCodeAttribute");
+
+       ainfo = mono_custom_attrs_from_method_checked (jinfo_get_method (ji), &error);
+       mono_error_cleanup (&error); /* FIXME don't swallow the error? */
        if (ainfo) {
                if (mono_custom_attrs_has_attr (ainfo, hidden_klass))
                        ji->dbg_hidden = TRUE;
@@ -3352,7 +3351,8 @@ init_jit_info_dbg_attrs (MonoJitInfo *ji)
                mono_custom_attrs_free (ainfo);
        }
 
-       ainfo = mono_custom_attrs_from_class (jinfo_get_method (ji)->klass);
+       ainfo = mono_custom_attrs_from_class_checked (jinfo_get_method (ji)->klass, &error);
+       mono_error_cleanup (&error); /* FIXME don't swallow the error? */
        if (ainfo) {
                if (mono_custom_attrs_has_attr (ainfo, step_through_klass))
                        ji->dbg_step_through = TRUE;
@@ -6032,6 +6032,7 @@ decode_value_internal (MonoType *t, int type, MonoDomain *domain, guint8 *addr,
                        } else if (type == VALUE_TYPE_ID_NULL) {
                                *(MonoObject**)addr = NULL;
                        } else if (type == MONO_TYPE_VALUETYPE) {
+                               MonoError error;
                                guint8 *buf2;
                                gboolean is_enum;
                                MonoClass *klass;
@@ -6063,7 +6064,8 @@ decode_value_internal (MonoType *t, int type, MonoDomain *domain, guint8 *addr,
                                        g_free (vtype_buf);
                                        return err;
                                }
-                               *(MonoObject**)addr = mono_value_box (d, klass, vtype_buf);
+                               *(MonoObject**)addr = mono_value_box_checked (d, klass, vtype_buf, &error);
+                               mono_error_cleanup (&error);
                                g_free (vtype_buf);
                        } else {
                                char *name = mono_type_full_name (t);
@@ -6085,6 +6087,7 @@ decode_value_internal (MonoType *t, int type, MonoDomain *domain, guint8 *addr,
 static ErrorCode
 decode_value (MonoType *t, MonoDomain *domain, guint8 *addr, guint8 *buf, guint8 **endbuf, guint8 *limit)
 {
+       MonoError error;
        ErrorCode err;
        int type = decode_byte (buf, &buf, limit);
 
@@ -6109,7 +6112,12 @@ decode_value (MonoType *t, MonoDomain *domain, guint8 *addr, guint8 *buf, guint8
                                g_free (nullable_buf);
                                return err;
                        }
-                       mono_nullable_init (addr, mono_value_box (domain, mono_class_from_mono_type (targ), nullable_buf), mono_class_from_mono_type (t));
+                       MonoObject *boxed = mono_value_box_checked (domain, mono_class_from_mono_type (targ), nullable_buf, &error);
+                       if (!is_ok (&error)) {
+                               mono_error_cleanup (&error);
+                               return ERR_INVALID_OBJECT;
+                       }
+                       mono_nullable_init (addr, boxed, mono_class_from_mono_type (t));
                        g_free (nullable_buf);
                        *endbuf = buf;
                        return ERR_NONE;
@@ -6461,6 +6469,7 @@ add_thread (gpointer key, gpointer value, gpointer user_data)
 static ErrorCode
 do_invoke_method (DebuggerTlsData *tls, Buffer *buf, InvokeData *invoke, guint8 *p, guint8 **endp)
 {
+       MonoError error;
        guint8 *end = invoke->endp;
        MonoMethod *m;
        int i, nargs;
@@ -6482,7 +6491,10 @@ do_invoke_method (DebuggerTlsData *tls, Buffer *buf, InvokeData *invoke, guint8
                 */
                this_arg = NULL;
                DEBUG_PRINTF (1, "[%p] Invoking method '%s' on receiver '%s'.\n", (gpointer) (gsize) mono_native_thread_id_get (), mono_method_full_name (invoke->method, TRUE), this_arg ? this_arg->vtable->klass->name : "<null>");
-               mono_runtime_invoke (invoke->method, NULL, invoke->args, &exc);
+
+               mono_runtime_try_invoke (invoke->method, NULL, invoke->args, &exc, &error);
+               mono_error_assert_ok (&error);
+
                g_assert_not_reached ();
        }
 
@@ -6559,8 +6571,11 @@ do_invoke_method (DebuggerTlsData *tls, Buffer *buf, InvokeData *invoke, guint8
                if (!strcmp (m->name, ".ctor")) {
                        if (m->klass->flags & TYPE_ATTRIBUTE_ABSTRACT)
                                return ERR_INVALID_ARGUMENT;
-                       else
-                               this_arg = mono_object_new (domain, m->klass);
+                       else {
+                               MonoError error;
+                               this_arg = mono_object_new_checked (domain, m->klass, &error);
+                               mono_error_assert_ok (&error);
+                       }
                } else {
                        return ERR_INVALID_ARGUMENT;
                }
@@ -6627,10 +6642,12 @@ do_invoke_method (DebuggerTlsData *tls, Buffer *buf, InvokeData *invoke, guint8
 #endif
 
        mono_stopwatch_start (&watch);
-       if (m->klass->valuetype)
-               res = mono_runtime_invoke (m, this_buf, args, &exc);
-       else
-               res = mono_runtime_invoke (m, this_arg, args, &exc);
+       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)) {
+               exc = (MonoObject*) mono_error_convert_to_exception (&error);
+       } else {
+               mono_error_cleanup (&error); /* FIXME report error */
+       }
        mono_stopwatch_stop (&watch);
        DEBUG_PRINTF (1, "[%p] Invoke result: %p, exc: %s, time: %ld ms.\n", (gpointer) (gsize) mono_native_thread_id_get (), res, exc ? exc->vtable->klass->name : NULL, (long)mono_stopwatch_elapsed_ms (&watch));
        if (exc) {
@@ -6786,7 +6803,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
-        * after the mono_runtime_invoke() already returned, but it doesn't matter
+        * after the mono_runtime_invoke_checked() already returned, but it doesn't matter
         * because we reset the abort here.
         */
 
@@ -6948,7 +6965,7 @@ vm_commands (int command, int id, guint8 *p, guint8 *end, Buffer *buf)
                wait_for_suspend ();
 
 #ifdef TRY_MANAGED_SYSTEM_ENVIRONMENT_EXIT
-               env_class = mono_class_from_name (mono_defaults.corlib, "System", "Environment");
+               env_class = mono_class_try_load_from_name (mono_defaults.corlib, "System", "Environment");
                if (env_class)
                        exit_method = mono_class_get_method_from_name (env_class, "Exit", 1);
 #endif
@@ -7081,7 +7098,7 @@ vm_commands (int command, int id, guint8 *p, guint8 *end, Buffer *buf)
                }
 
                /*
-                * Check whether we're still inside the mono_runtime_invoke() and that it's
+                * Check whether we're still inside the mono_runtime_invoke_checked() and that it's
                 * actually the correct invocation.
                 *
                 * Careful, we do not stop the thread that's doing the invocation, so we can't
@@ -7238,8 +7255,11 @@ vm_commands (int command, int id, guint8 *p, guint8 *end, Buffer *buf)
                                ass = (MonoAssembly *)tmp->data;
 
                                if (ass->image) {
+                                       MonoError error;
                                        type_resolve = TRUE;
-                                       t = mono_reflection_get_type (ass->image, &info, ignore_case, &type_resolve);
+                                       /* FIXME really okay to call while holding locks? */
+                                       t = mono_reflection_get_type_checked (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));
                                                g_ptr_array_add (res_domains, domain);
@@ -7572,6 +7592,7 @@ domain_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
                break;
        }
        case CMD_APPDOMAIN_CREATE_BOXED_VALUE: {
+               MonoError error;
                MonoClass *klass;
                MonoDomain *domain2;
                MonoObject *o;
@@ -7586,7 +7607,8 @@ domain_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
                // FIXME:
                g_assert (domain == domain2);
 
-               o = mono_object_new (domain, klass);
+               o = mono_object_new_checked (domain, klass, &error);
+               mono_error_assert_ok (&error);
 
                err = decode_value (&klass->byval_arg, domain, (guint8 *)mono_object_unbox (o), p, &p, end);
                if (err != ERR_NONE)
@@ -7643,11 +7665,17 @@ assembly_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
                break;
        }
        case CMD_ASSEMBLY_GET_OBJECT: {
-               MonoObject *o = (MonoObject*)mono_assembly_get_object (domain, ass);
+               MonoError error;
+               MonoObject *o = (MonoObject*)mono_assembly_get_object_checked (domain, ass, &error);
+               if (!o) {
+                       mono_error_cleanup (&error); /* FIXME don't swallow the error */
+                       return ERR_INVALID_OBJECT;
+               }
                buffer_add_objid (buf, o);
                break;
        }
        case CMD_ASSEMBLY_GET_TYPE: {
+               MonoError error;
                char *s = decode_string (p, &p, end);
                gboolean ignorecase = decode_byte (p, &p, end);
                MonoTypeNameParse info;
@@ -7664,7 +7692,13 @@ assembly_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
                } else {
                        if (info.assembly.name)
                                NOT_IMPLEMENTED;
-                       t = mono_reflection_get_type (ass->image, &info, ignorecase, &type_resolve);
+                       t = mono_reflection_get_type_checked (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);
+                               g_free (s);
+                               return ERR_INVALID_ARGUMENT;
+                       }
                }
                buffer_add_typeid (buf, domain, t ? mono_class_from_mono_type (t) : NULL);
                mono_reflection_free_type_info (&info);
@@ -8018,7 +8052,11 @@ type_commands_internal (int command, MonoClass *klass, MonoDomain *domain, guint
                if (err != ERR_NONE)
                        return err;
 
-               cinfo = mono_custom_attrs_from_class (klass);
+               cinfo = mono_custom_attrs_from_class_checked (klass, &error);
+               if (!is_ok (&error)) {
+                       mono_error_cleanup (&error); /* FIXME don't swallow the error message */
+                       return ERR_LOADER_ERROR;
+               }
 
                err = buffer_add_cattrs (buf, domain, klass->image, attr_klass, cinfo);
                if (err != ERR_NONE)
@@ -8037,7 +8075,11 @@ type_commands_internal (int command, MonoClass *klass, MonoDomain *domain, guint
                if (err != ERR_NONE)
                        return err;
 
-               cinfo = mono_custom_attrs_from_field (klass, field);
+               cinfo = mono_custom_attrs_from_field_checked (klass, field, &error);
+               if (!is_ok (&error)) {
+                       mono_error_cleanup (&error); /* FIXME don't swallow the error message */
+                       return ERR_LOADER_ERROR;
+               }
 
                err = buffer_add_cattrs (buf, domain, klass->image, attr_klass, cinfo);
                if (err != ERR_NONE)
@@ -8056,7 +8098,11 @@ type_commands_internal (int command, MonoClass *klass, MonoDomain *domain, guint
                if (err != ERR_NONE)
                        return err;
 
-               cinfo = mono_custom_attrs_from_property (klass, prop);
+               cinfo = mono_custom_attrs_from_property_checked (klass, prop, &error);
+               if (!is_ok (&error)) {
+                       mono_error_cleanup (&error); /* FIXME don't swallow the error message */
+                       return ERR_LOADER_ERROR;
+               }
 
                err = buffer_add_cattrs (buf, domain, klass->image, attr_klass, cinfo);
                if (err != ERR_NONE)
@@ -8168,7 +8214,10 @@ type_commands_internal (int command, MonoClass *klass, MonoDomain *domain, guint
        }
        case CMD_TYPE_GET_OBJECT: {
                MonoObject *o = (MonoObject*)mono_type_get_object_checked (domain, &klass->byval_arg, &error);
-               mono_error_raise_exception (&error); /* FIXME don't raise here */
+               if (!mono_error_ok (&error)) {
+                       mono_error_cleanup (&error);
+                       return ERR_INVALID_OBJECT;
+               }
                buffer_add_objid (buf, o);
                break;
        }
@@ -8291,9 +8340,11 @@ type_commands_internal (int command, MonoClass *klass, MonoDomain *domain, guint
                break;
        }
        case CMD_TYPE_CREATE_INSTANCE: {
+               MonoError error;
                MonoObject *obj;
 
-               obj = mono_object_new (domain, klass);
+               obj = mono_object_new_checked (domain, klass, &error);
+               mono_error_assert_ok (&error);
                buffer_add_objid (buf, obj);
                break;
        }
@@ -8343,6 +8394,7 @@ method_commands_internal (int command, MonoMethod *method, MonoDomain *domain, g
                break;
        }
        case CMD_METHOD_GET_DEBUG_INFO: {
+               MonoError error;
                MonoDebugMethodInfo *minfo;
                char *source_file;
                int i, j, n_il_offsets;
@@ -8350,8 +8402,9 @@ method_commands_internal (int command, MonoMethod *method, MonoDomain *domain, g
                GPtrArray *source_file_list;
                MonoSymSeqPoint *sym_seq_points;
 
-               header = mono_method_get_header (method);
+               header = mono_method_get_header_checked (method, &error);
                if (!header) {
+                       mono_error_cleanup (&error); /* FIXME don't swallow the error */
                        buffer_add_int (buf, 0);
                        buffer_add_string (buf, "");
                        buffer_add_int (buf, 0);
@@ -8438,13 +8491,16 @@ method_commands_internal (int command, MonoMethod *method, MonoDomain *domain, g
                break;
        }
        case CMD_METHOD_GET_LOCALS_INFO: {
+               MonoError error;
                int i, num_locals;
                MonoDebugLocalsInfo *locals;
                int *locals_map = NULL;
 
-               header = mono_method_get_header (method);
-               if (!header)
+               header = mono_method_get_header_checked (method, &error);
+               if (!header) {
+                       mono_error_cleanup (&error); /* FIXME don't swallow the error */
                        return ERR_INVALID_ARGUMENT;
+               }
 
                locals = mono_debug_lookup_locals (method);
                if (!locals) {
@@ -8565,10 +8621,12 @@ method_commands_internal (int command, MonoMethod *method, MonoDomain *domain, g
                }
                break;
        case CMD_METHOD_GET_BODY: {
+               MonoError error;
                int i;
 
-               header = mono_method_get_header (method);
+               header = mono_method_get_header_checked (method, &error);
                if (!header) {
+                       mono_error_cleanup (&error); /* FIXME don't swallow the error */
                        buffer_add_int (buf, 0);
 
                        if (CHECK_PROTOCOL_VERSION (2, 18))
@@ -8667,6 +8725,7 @@ method_commands_internal (int command, MonoMethod *method, MonoDomain *domain, g
                break;
        }
        case CMD_METHOD_GET_CATTRS: {
+               MonoError error;
                MonoClass *attr_klass;
                MonoCustomAttrInfo *cinfo;
 
@@ -8675,7 +8734,11 @@ method_commands_internal (int command, MonoMethod *method, MonoDomain *domain, g
                if (err != ERR_NONE)
                        return err;
 
-               cinfo = mono_custom_attrs_from_method (method);
+               cinfo = mono_custom_attrs_from_method_checked (method, &error);
+               if (!is_ok (&error)) {
+                       mono_error_cleanup (&error); /* FIXME don't swallow the error message */
+                       return ERR_LOADER_ERROR;
+               }
 
                err = buffer_add_cattrs (buf, domain, method->klass->image, attr_klass, cinfo);
                if (err != ERR_NONE)
@@ -8962,8 +9025,10 @@ frame_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
 
        switch (command) {
        case CMD_STACK_FRAME_GET_VALUES: {
+               MonoError error;
                len = decode_int (p, &p, end);
-               header = mono_method_get_header (frame->actual_method);
+               header = mono_method_get_header_checked (frame->actual_method, &error);
+               mono_error_assert_ok (&error); /* FIXME report error */
 
                for (i = 0; i < len; ++i) {
                        pos = decode_int (p, &p, end);
@@ -9014,12 +9079,14 @@ frame_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
                break;
        }
        case CMD_STACK_FRAME_SET_VALUES: {
+               MonoError error;
                guint8 *val_buf;
                MonoType *t;
                MonoDebugVarInfo *var;
 
                len = decode_int (p, &p, end);
-               header = mono_method_get_header (frame->actual_method);
+               header = mono_method_get_header_checked (frame->actual_method, &error);
+               mono_error_assert_ok (&error); /* FIXME report error */
 
                for (i = 0; i < len; ++i) {
                        pos = decode_int (p, &p, end);
@@ -9616,12 +9683,13 @@ 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 ();
 
-       mono_jit_thread_attach (mono_get_root_domain ());
+       attach_cookie = mono_jit_thread_attach (mono_get_root_domain (), &attach_dummy);
 
        mono_thread_internal_current ()->flags |= MONO_THREAD_FLAG_DONT_MANAGE;
 
@@ -9774,7 +9842,9 @@ debugger_thread (void *arg)
                DEBUG_PRINTF (2, "[dbg] Detached - restarting clean debugger thread.\n");
                start_debugger_thread ();
        }
-       
+
+       mono_jit_thread_detach (attach_cookie, &attach_dummy);
+
        return 0;
 }