2006-10-10 Miguel de Icaza <miguel@novell.com>
[mono.git] / mono / metadata / mono-debug-debugger.c
index 0189c706e8420c0c72cf967c1cd2aa022be9e5a9..981072583fd341d0601cadea1bf54d6edf05d81a 100644 (file)
@@ -7,6 +7,7 @@
 #include <mono/metadata/tokentype.h>
 #include <mono/metadata/appdomain.h>
 #include <mono/metadata/gc-internal.h>
+#include <mono/metadata/threads.h>
 #include <mono/os/gc_wrapper.h>
 #include <mono/metadata/object-internals.h>
 #include <mono/metadata/class-internals.h>
 static guint32 debugger_lock_level = 0;
 static CRITICAL_SECTION debugger_lock_mutex;
 static gboolean must_reload_symtabs = FALSE;
-static gboolean mono_debugger_initialized = FALSE;
+static gboolean mono_debugger_use_debugger = FALSE;
 static MonoObject *last_exception = NULL;
 
-struct _MonoDebuggerMetadataInfo {
-       int size;
-       int mono_defaults_size;
-       MonoDefaults *mono_defaults;
-       int klass_field_offset;
-       int klass_methods_offset;
-       int klass_method_count_offset;
-       int field_info_size;
-};
-
 void (*mono_debugger_event_handler) (MonoDebuggerEvent event, guint64 data, guint64 arg) = NULL;
 
 #define WRITE_UINT32(ptr,value) G_STMT_START { \
@@ -54,25 +45,12 @@ typedef struct {
        guint32 stop;
 } MonoDebuggerExceptionInfo;
 
-#ifndef PLATFORM_WIN32
-
-MonoDebuggerIOLayer mono_debugger_io_layer = {
-       InitializeCriticalSection, DeleteCriticalSection, TryEnterCriticalSection,
-       EnterCriticalSection, LeaveCriticalSection, WaitForSingleObjectEx, SignalObjectAndWait,
-       WaitForMultipleObjectsEx, CreateSemaphore, ReleaseSemaphore, CreateThread,
-       GetCurrentThreadId
-};
-
-#endif
+static int initialized = 0;
 
 void
 mono_debugger_lock (void)
 {
-       if (!mono_debugger_initialized) {
-               debugger_lock_level++;
-               return;
-       }
-
+       g_assert (initialized);
        EnterCriticalSection (&debugger_lock_mutex);
        debugger_lock_level++;
 }
@@ -80,15 +58,9 @@ mono_debugger_lock (void)
 void
 mono_debugger_unlock (void)
 {
-       g_assert (debugger_lock_level > 0);
-
-       if (!mono_debugger_initialized) {
-               debugger_lock_level--;
-               return;
-       }
-
+       g_assert (initialized);
        if (debugger_lock_level == 1) {
-               if (must_reload_symtabs) {
+               if (must_reload_symtabs && mono_debugger_use_debugger) {
                        mono_debugger_event (MONO_DEBUGGER_EVENT_RELOAD_SYMTABS, 0, 0);
                        must_reload_symtabs = FALSE;
                }
@@ -99,115 +71,33 @@ mono_debugger_unlock (void)
 }
 
 void
-mono_debugger_initialize (void)
+mono_debugger_initialize (gboolean use_debugger)
 {
        MONO_GC_REGISTER_ROOT (last_exception);
        
-       g_assert (!mono_debugger_initialized);
+       g_assert (!mono_debugger_use_debugger);
 
        InitializeCriticalSection (&debugger_lock_mutex);
-       mono_debugger_initialized = TRUE;
+       mono_debugger_use_debugger = use_debugger;
+       initialized = 1;
 }
 
 void
 mono_debugger_add_symbol_file (MonoDebugHandle *handle)
 {
-       g_assert (mono_debugger_initialized);
+       g_assert (mono_debugger_use_debugger);
 
        mono_debugger_lock ();
-       mono_debugger_event (MONO_DEBUGGER_EVENT_ADD_MODULE, GPOINTER_TO_UINT (handle), 0);
+       mono_debugger_event (MONO_DEBUGGER_EVENT_ADD_MODULE, (guint64) (gsize) handle, 0);
        mono_debugger_unlock ();
 }
 
-void
-mono_debugger_add_builtin_types (MonoDebugHandle *symfile)
-{
-       MonoDebuggerMetadataInfo *info;
-       MonoClass klass;
-
-       mono_symbol_table->corlib = symfile;
-       mono_symbol_table->metadata_info = info = g_new0 (MonoDebuggerMetadataInfo, 1);
-
-       info->size = sizeof (MonoDebuggerMetadataInfo);
-       info->mono_defaults = &mono_defaults;
-       info->mono_defaults_size = sizeof (MonoDefaults);
-       info->klass_field_offset = (guint8*)&klass.fields - (guint8*)&klass;
-       info->klass_methods_offset = (guint8*)&klass.methods - (guint8*)&klass;
-       info->klass_method_count_offset = (guint8*)&klass.method.count - (guint8*)&klass;
-       info->field_info_size = sizeof (MonoClassField);
-}
-
 void
 mono_debugger_start_add_type (MonoDebugHandle *symfile, MonoClass *klass)
 {
        must_reload_symtabs = TRUE;
 }
 
-void
-mono_debugger_add_type (MonoDebugHandle *symfile, MonoClass *klass)
-{
-}
-
-void
-mono_debugger_add_method (MonoDebugMethodJitInfo *jit)
-{
-}
-
-MonoReflectionMethod *
-ves_icall_MonoDebugger_GetMethod (MonoReflectionAssembly *assembly, guint32 token)
-{
-       MonoMethod *method;
-
-       method = mono_get_method (mono_assembly_get_image (assembly->assembly), token, NULL);
-
-       return mono_method_get_object (mono_domain_get (), method, NULL);
-}
-
-int
-ves_icall_MonoDebugger_GetMethodToken (MonoReflectionAssembly *assembly, MonoReflectionMethod *method)
-{
-       return method->method->token;
-}
-
-MonoReflectionType *
-ves_icall_MonoDebugger_GetType (MonoReflectionAssembly *assembly, guint32 token)
-{
-       MonoClass *klass;
-
-       klass = mono_class_get (mono_assembly_get_image (assembly->assembly), token);
-       if (!klass) {
-               g_warning (G_STRLOC ": %x", token);
-               return NULL;
-       }
-
-       return mono_type_get_object (mono_domain_get (), &klass->byval_arg);
-}
-
-MonoReflectionType *
-ves_icall_MonoDebugger_GetLocalTypeFromSignature (MonoReflectionAssembly *assembly, MonoArray *signature)
-{
-       MonoDomain *domain; 
-       MonoImage *image;
-       MonoType *type;
-       const char *ptr;
-       int len = 0;
-
-       MONO_CHECK_ARG_NULL (assembly);
-       MONO_CHECK_ARG_NULL (signature);
-
-       domain = mono_domain_get();
-       image = mono_assembly_get_image (assembly->assembly);
-
-       ptr = mono_array_addr (signature, char, 0);
-       g_assert (*ptr++ == 0x07);
-       len = mono_metadata_decode_value (ptr, &ptr);
-       g_assert (len == 1);
-
-       type = mono_metadata_parse_type (image, MONO_PARSE_LOCAL, 0, ptr, &ptr);
-
-       return mono_type_get_object (domain, type);
-}
-
 void
 mono_debugger_event (MonoDebuggerEvent event, guint64 data, guint64 arg)
 {
@@ -218,7 +108,8 @@ mono_debugger_event (MonoDebuggerEvent event, guint64 data, guint64 arg)
 void
 mono_debugger_cleanup (void)
 {
-       /* Do nothing yet. */
+       mono_debugger_event (MONO_DEBUGGER_EVENT_FINALIZE_MANAGED_CODE, 0, 0);
+       mono_debugger_event_handler = NULL;
 }
 
 /*
@@ -307,19 +198,29 @@ mono_debugger_method_has_breakpoint (MonoMethod *method)
 void
 mono_debugger_breakpoint_callback (MonoMethod *method, guint32 index)
 {
-       mono_debugger_event (MONO_DEBUGGER_EVENT_BREAKPOINT, GPOINTER_TO_UINT (method), index);
+       mono_debugger_event (MONO_DEBUGGER_EVENT_JIT_BREAKPOINT, (guint64) (gsize) method, index);
 }
 
 gboolean
 mono_debugger_unhandled_exception (gpointer addr, gpointer stack, MonoObject *exc)
 {
-       if (!mono_debugger_initialized)
+       const gchar *name;
+
+       if (!mono_debugger_use_debugger)
                return FALSE;
 
        // Prevent the object from being finalized.
        last_exception = exc;
+
+       name = mono_class_get_name (mono_object_get_class (exc));
+       if (!strcmp (name, "ThreadAbortException")) {
+               MonoThread *thread = mono_thread_current ();
+               mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_ABORT, 0, thread->tid);
+               mono_thread_exit ();
+       }
+
        mono_debugger_event (MONO_DEBUGGER_EVENT_UNHANDLED_EXCEPTION,
-                            GPOINTER_TO_UINT (exc), GPOINTER_TO_UINT (addr));
+                            (guint64) (gsize) exc, (guint64) (gsize) addr);
        return TRUE;
 }
 
@@ -328,7 +229,7 @@ mono_debugger_handle_exception (gpointer addr, gpointer stack, MonoObject *exc)
 {
        MonoDebuggerExceptionInfo info;
 
-       if (!mono_debugger_initialized)
+       if (!mono_debugger_use_debugger)
                return;
 
        // Prevent the object from being finalized.
@@ -338,8 +239,8 @@ mono_debugger_handle_exception (gpointer addr, gpointer stack, MonoObject *exc)
        info.exception_obj = exc;
        info.stop = 0;
 
-       mono_debugger_event (MONO_DEBUGGER_EVENT_EXCEPTION, GPOINTER_TO_UINT (&info),
-                            GPOINTER_TO_UINT (addr));
+       mono_debugger_event (MONO_DEBUGGER_EVENT_HANDLE_EXCEPTION, (guint64) (gsize) &info,
+                            (guint64) (gsize) addr);
 }
 
 gboolean
@@ -347,7 +248,7 @@ mono_debugger_throw_exception (gpointer addr, gpointer stack, MonoObject *exc)
 {
        MonoDebuggerExceptionInfo info;
 
-       if (!mono_debugger_initialized)
+       if (!mono_debugger_use_debugger)
                return FALSE;
 
        // Prevent the object from being finalized.
@@ -357,8 +258,8 @@ mono_debugger_throw_exception (gpointer addr, gpointer stack, MonoObject *exc)
        info.exception_obj = exc;
        info.stop = 0;
 
-       mono_debugger_event (MONO_DEBUGGER_EVENT_THROW_EXCEPTION, GPOINTER_TO_UINT (&info),
-                            GPOINTER_TO_UINT (addr));
+       mono_debugger_event (MONO_DEBUGGER_EVENT_THROW_EXCEPTION, (guint64) (gsize) &info,
+                            (guint64) (gsize) addr);
        return info.stop != 0;
 }
 
@@ -406,9 +307,6 @@ mono_debugger_runtime_invoke (MonoMethod *method, void *obj, void **params, Mono
        MonoObject *retval;
        gchar *message;
 
-       //      g_message (G_STRLOC ": %p - %p - %p - %p", method, obj, params, exc);
-       //      g_message (G_STRLOC ": %s.%s", method->klass->name, method->name);
-
        if (!strcmp (method->name, ".ctor")) {
                retval = obj = mono_object_new (mono_domain_get (), method->klass);