2010-01-04 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / mini / debug-debugger.c
index b76d4d52a2ca5b57ca2dc56ed692967c7cb07a12..8e03e8ecf44586fef4a0f5470bae51fae818ed71 100644 (file)
@@ -59,13 +59,13 @@ static MonoDebuggerMetadataInfo debugger_metadata_info = {
        sizeof (MonoDebuggerMetadataInfo),
        sizeof (MonoDefaults),
        &mono_defaults,
-       sizeof (MonoType),
+       MONO_SIZEOF_TYPE,
        sizeof (MonoArrayType),
        sizeof (MonoClass),
-       sizeof (MonoThread),
-       G_STRUCT_OFFSET (MonoThread, tid),
-       G_STRUCT_OFFSET (MonoThread, stack_ptr),
-       G_STRUCT_OFFSET (MonoThread, end_stack),
+       sizeof (MonoInternalThread),
+       G_STRUCT_OFFSET (MonoInternalThread, tid),
+       G_STRUCT_OFFSET (MonoInternalThread, stack_ptr),
+       G_STRUCT_OFFSET (MonoInternalThread, end_stack),
        G_STRUCT_OFFSET (MonoClass, image),
        G_STRUCT_OFFSET (MonoClass, instance_size),
        G_STRUCT_OFFSET (MonoClass, parent),
@@ -120,7 +120,7 @@ MonoDebuggerInfo MONO_DEBUGGER__debugger_info = {
        MONO_DEBUGGER_MAGIC,
        MONO_DEBUGGER_MAJOR_VERSION,
        MONO_DEBUGGER_MINOR_VERSION,
-       0, /* dummy */
+       0, /* runtime_flags */
        sizeof (MonoDebuggerInfo),
        sizeof (MonoSymbolTable),
        MONO_TRAMPOLINE_NUM,
@@ -456,15 +456,44 @@ debugger_init_code_buffer (void)
 }
 
 extern MonoDebuggerInfo *MONO_DEBUGGER__debugger_info_ptr;
+extern long MONO_DEBUGGER__using_debugger;
 
 static void
 debugger_initialize (void)
 {
 }
 
+/**
+ * Check whether we're running inside the debugger.
+ *
+ * There seems to be a bug in some versions of glibc which causes _dl_debug_state() being called with
+ * RT_CONSISTENT before relocations are done.
+ *
+ * If that happens, the debugger cannot read the `MONO_DEBUGGER__debugger_info' structure at the time
+ * the `libmono.so' library is loaded.
+ *
+ * As a workaround, the `mdb_debug_info' now also contains a global variable called
+ * `MONO_DEBUGGER__using_debugger' which may we set to 1 by the debugger to tell us that we're running
+ * inside the debugger.
+ *
+ * mini_init() checks this and calls mini_debugger_init() if necessary.
+ *
+ */
+
+gboolean
+mini_debug_running_inside_mdb (void)
+{
+       return MONO_DEBUGGER__using_debugger || mono_debug_using_mono_debugger ();
+}
+
 void
-mono_debugger_init (void)
+mini_debugger_init (void)
 {
+       if (mono_debugger_event_handler) {
+               g_warning (G_STRLOC ": duplicate call to mono_debugger_init()!");
+               return;
+       }
+
        debugger_executable_code_buffer = mono_global_codeman_reserve (EXECUTABLE_CODE_BUFFER_SIZE);
        mono_debugger_event_handler = debugger_event_handler;
 
@@ -486,6 +515,12 @@ mono_debugger_init (void)
                             (guint64) (gssize) MONO_DEBUGGER__debugger_info_ptr, 0);
 }
 
+void
+mini_debugger_set_attach_ok (void)
+{
+       MONO_DEBUGGER__debugger_info.runtime_flags |= DEBUGGER_RUNTIME_FLAGS_ATTACH_OK;
+}
+
 typedef struct 
 {
        MonoDomain *domain;
@@ -509,7 +544,7 @@ main_thread_handler (gpointer user_data)
 }
 
 int
-mono_debugger_main (MonoDomain *domain, MonoAssembly *assembly, int argc, char **argv)
+mini_debugger_main (MonoDomain *domain, MonoAssembly *assembly, int argc, char **argv)
 {
        MainThreadArgs main_args;
        MonoImage *image;