2008-07-12 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / debug-mini.c
index 38c8d485c48aa6dbc37a1d08d25eb6035538b304..1a7dfebba81c43ce74078e13fbbdfaeeb30af7a6 100644 (file)
@@ -14,6 +14,9 @@
 #include <mono/metadata/mono-config.h>
 #include <mono/metadata/mono-debug.h>
 #include <mono/metadata/appdomain.h>
+#include <mono/metadata/threads-types.h>
+
+#define _IN_THE_MONO_DEBUGGER
 #include <mono/metadata/mono-debug-debugger.h>
 #include "debug-mini.h"
 
 #include <valgrind/valgrind.h>
 #endif
 
+#ifdef MONO_DEBUGGER_SUPPORTED
+#include <libgc/include/libgc-mono-debugger.h>
+#endif
+
 typedef struct {
        guint32 index;
        MonoMethodDesc *desc;
 } MiniDebugBreakpointInfo;
 
-typedef struct
-{
-       guint64 index;
-       MonoMethod *method;
-       MonoDebugMethodAddressList *address_list;
-} MiniDebugMethodBreakpointInfo;
-
 typedef struct
 {
        MonoDebugMethodJitInfo *jit;
@@ -41,8 +41,32 @@ typedef struct
        guint32 breakpoint_id;
 } MiniDebugMethodInfo;
 
-static void
-mono_debugger_check_breakpoints (MonoMethod *method, MonoDebugMethodAddress *debug_info);
+struct _MonoDebuggerThreadInfo {
+       guint64 tid;
+       guint64 lmf_addr;
+       guint64 end_stack;
+
+       guint64 extended_notifications;
+
+       /* Next pointer. */
+       MonoDebuggerThreadInfo *next;
+
+       /*
+        * The stack bounds are only used when reading a core file.
+        */
+       guint64 stack_start;
+       guint64 signal_stack_start;
+       guint32 stack_size;
+       guint32 signal_stack_size;
+
+       /*
+        * The debugger doesn't access anything beyond this point.
+        */
+       MonoJitTlsData *jit_tls;
+       MonoThread *thread;
+};
+
+MonoDebuggerThreadInfo *mono_debugger_thread_table = NULL;
 
 static inline void
 record_line_number (MiniDebugMethodInfo *info, guint32 address, guint32 offset)
@@ -55,15 +79,6 @@ record_line_number (MiniDebugMethodInfo *info, guint32 address, guint32 offset)
        g_array_append_val (info->line_numbers, lne);
 }
 
-static void
-mono_debug_free_method_jit_info (MonoDebugMethodJitInfo *jit)
-{
-       g_free (jit->line_numbers);
-       g_free (jit->this_var);
-       g_free (jit->params);
-       g_free (jit->locals);
-       g_free (jit);
-}
 
 void
 mono_debug_init_method (MonoCompile *cfg, MonoBasicBlock *start_block, guint32 breakpoint_id)
@@ -104,6 +119,8 @@ mono_debug_open_method (MonoCompile *cfg)
 static void
 write_variable (MonoInst *inst, MonoDebugVarInfo *var)
 {
+       var->type = inst->inst_vtype;
+
        if (inst->opcode == OP_REGVAR)
                var->index = inst->dreg | MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER;
        else {
@@ -241,7 +258,8 @@ mono_debug_close_method (MonoCompile *cfg)
        jit->epilogue_begin = cfg->epilog_begin;
        jit->code_size = cfg->code_len;
 
-       record_line_number (info, jit->epilogue_begin, header->code_size);
+       if (jit->epilogue_begin)
+                  record_line_number (info, jit->epilogue_begin, header->code_size);
 
        jit->num_params = sig->param_count;
        jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
@@ -419,18 +437,17 @@ serialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
 void
 mono_debug_serialize_debug_info (MonoCompile *cfg, guint8 **out_buf, guint32 *buf_len)
 {
-       MiniDebugMethodInfo *info;
        MonoDebugMethodJitInfo *jit;
        guint32 size, prev_offset, prev_native_offset;
        guint8 *buf, *p;
        int i;
 
-       info = (MiniDebugMethodInfo *) cfg->debug_info;
-       if (!info || !info->jit) {
+       /* Can't use cfg->debug_info as it is freed by close_method () */
+       jit = mono_debug_find_method (cfg->method, mono_domain_get ());
+       if (!jit) {
                *buf_len = 0;
                return;
        }
-       jit = info->jit;
 
        size = ((jit->num_params + jit->num_locals + 1) * 10) + (jit->num_line_numbers * 10) + 64;
        p = buf = g_malloc (size);
@@ -637,81 +654,7 @@ mono_debug_print_vars (gpointer ip, gboolean only_arguments)
                        print_var_info (&jit->locals [i], i, "", "Local");
                }
        }
-}
-
-
-/*
- * Debugger breakpoint interface.
- *
- * This interface is used to insert breakpoints on methods which are not yet JITed.
- * The debugging code keeps a list of all such breakpoints and automatically inserts the
- * breakpoint when the method is JITed.
- */
-
-static GPtrArray *method_breakpoints = NULL;
-
-MonoDebugMethodAddressList *
-mono_debugger_insert_method_breakpoint (MonoMethod *method, guint64 index)
-{
-       MiniDebugMethodBreakpointInfo *info;
-
-       info = g_new0 (MiniDebugMethodBreakpointInfo, 1);
-       info->method = method;
-       info->index = index;
-
-       info->address_list = mono_debug_lookup_method_addresses (method);
-
-       if (!method_breakpoints)
-               method_breakpoints = g_ptr_array_new ();
-
-       g_ptr_array_add (method_breakpoints, info);
-
-       return info->address_list;
-}
-
-int
-mono_debugger_remove_method_breakpoint (guint64 index)
-{
-       int i;
-
-       if (!method_breakpoints)
-               return 0;
-
-       for (i = 0; i < method_breakpoints->len; i++) {
-               MiniDebugMethodBreakpointInfo *info = g_ptr_array_index (method_breakpoints, i);
-
-               if (info->index != index)
-                       continue;
-
-               g_ptr_array_remove (method_breakpoints, info);
-               g_free (info->address_list);
-               g_free (info);
-               return 1;
-       }
-
-       return 0;
-}
-
-static void
-mono_debugger_check_breakpoints (MonoMethod *method, MonoDebugMethodAddress *debug_info)
-{
-       int i;
-
-       if (!method_breakpoints)
-               return;
-
-       if (method->is_inflated)
-               method = ((MonoMethodInflated *) method)->declaring;
-
-       for (i = 0; i < method_breakpoints->len; i++) {
-               MiniDebugMethodBreakpointInfo *info = g_ptr_array_index (method_breakpoints, i);
-
-               if (method != info->method)
-                       continue;
-
-               mono_debugger_event (MONO_DEBUGGER_EVENT_JIT_BREAKPOINT,
-                                    (guint64) (gsize) debug_info, info->index);
-       }
+       mono_debug_free_method_jit_info (jit);
 }
 
 /*
@@ -802,3 +745,103 @@ mono_debugger_breakpoint_callback (MonoMethod *method, guint32 index)
 {
        mono_debugger_event (MONO_DEBUGGER_EVENT_JIT_BREAKPOINT, (guint64) (gsize) method, index);
 }
+
+void
+mono_debugger_thread_created (gsize tid, MonoThread *thread, MonoJitTlsData *jit_tls)
+{
+#ifdef MONO_DEBUGGER_SUPPORTED
+       size_t stsize = 0;
+       guint8 *staddr = NULL;
+       MonoDebuggerThreadInfo *info;
+
+       if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
+               return;
+
+       mono_debugger_lock ();
+
+       mono_thread_get_stack_bounds (&staddr, &stsize);
+
+       info = g_new0 (MonoDebuggerThreadInfo, 1);
+       info->tid = tid;
+       info->thread = thread;
+       info->stack_start = (guint64) (gsize) staddr;
+       info->signal_stack_start = (guint64) (gsize) jit_tls->signal_stack;
+       info->stack_size = stsize;
+       info->signal_stack_size = jit_tls->signal_stack_size;
+       info->end_stack = (guint64) (gsize) GC_mono_debugger_get_stack_ptr ();
+       info->lmf_addr = (guint64) (gsize) mono_get_lmf_addr ();
+       info->jit_tls = jit_tls;
+
+       info->next = mono_debugger_thread_table;
+       mono_debugger_thread_table = info;
+
+       mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CREATED,
+                            tid, (guint64) (gsize) info);
+
+       mono_debugger_unlock ();
+#endif /* MONO_DEBUGGER_SUPPORTED */
+}
+
+void
+mono_debugger_thread_cleanup (MonoJitTlsData *jit_tls)
+{
+#ifdef MONO_DEBUGGER_SUPPORTED
+       MonoDebuggerThreadInfo **ptr;
+
+       if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
+               return;
+
+       mono_debugger_lock ();
+
+       for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
+               MonoDebuggerThreadInfo *info = *ptr;
+
+               if (info->jit_tls != jit_tls)
+                       continue;
+
+               mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CLEANUP,
+                                    info->tid, (guint64) (gsize) info);
+
+               *ptr = info->next;
+               g_free (info);
+               break;
+       }
+
+       mono_debugger_unlock ();
+#endif
+}
+
+void
+mono_debugger_extended_notification (MonoDebuggerEvent event, guint64 data, guint64 arg)
+{
+#ifdef MONO_DEBUGGER_SUPPORTED
+       MonoDebuggerThreadInfo **ptr;
+       MonoThread *thread = mono_thread_current ();
+
+       if (!mono_debug_using_mono_debugger ())
+               return;
+
+       mono_debugger_lock ();
+
+       for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
+               MonoDebuggerThreadInfo *info = *ptr;
+
+               if (info->thread != thread)
+                       continue;
+
+               if ((info->extended_notifications & (int) event) == 0)
+                       continue;
+
+               mono_debugger_event (event, data, arg);
+       }
+
+       mono_debugger_unlock ();
+#endif
+}
+
+void
+mono_debugger_trampoline_compiled (MonoMethod *method, const guint8 *code)
+{
+       mono_debugger_extended_notification (MONO_DEBUGGER_EVENT_TRAMPOLINE,
+                                            (guint64) (gsize) method, (guint64) (gsize) code);
+}