Merge pull request #806 from AxlPr/master
[mono.git] / mono / metadata / mono-debug-debugger.c
1 /*
2  * mono-debug-debugger.c: 
3  *
4  * Author:
5  *      Mono Project (http://www.mono-project.com)
6  *
7  * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
8  * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
9  */
10
11 #include <config.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <mono/metadata/assembly.h>
15 #include <mono/metadata/metadata.h>
16 #include <mono/metadata/tabledefs.h>
17 #include <mono/metadata/tokentype.h>
18 #include <mono/metadata/appdomain.h>
19 #include <mono/metadata/gc-internal.h>
20 #include <mono/metadata/threads.h>
21 #include <mono/metadata/gc-internal.h>
22 #include <mono/metadata/object-internals.h>
23 #include <mono/metadata/class-internals.h>
24 #include <mono/metadata/domain-internals.h>
25 #include <mono/metadata/exception.h>
26 #include <mono/metadata/mono-debug.h>
27 #include <mono/metadata/mono-debug-debugger.h>
28 #include <mono/metadata/mono-endian.h>
29
30 static guint32 debugger_lock_level = 0;
31 static CRITICAL_SECTION debugger_lock_mutex;
32
33 typedef struct
34 {
35         guint32 index;
36         MonoMethod *method;
37         MonoDebugMethodAddressList *address_list;
38 } MethodBreakpointInfo;
39
40 static int initialized = 0;
41
42 void
43 mono_debugger_lock (void)
44 {
45         g_assert (initialized);
46         EnterCriticalSection (&debugger_lock_mutex);
47         debugger_lock_level++;
48 }
49
50 void
51 mono_debugger_unlock (void)
52 {
53         g_assert (initialized);
54         debugger_lock_level--;
55         LeaveCriticalSection (&debugger_lock_mutex);
56 }
57
58 void
59 mono_debugger_initialize ()
60 {
61         InitializeCriticalSection (&debugger_lock_mutex);
62         initialized = 1;
63 }
64
65 /*
66  * Debugger breakpoint interface.
67  *
68  * This interface is used to insert breakpoints on methods which are not yet JITed.
69  * The debugging code keeps a list of all such breakpoints and automatically inserts the
70  * breakpoint when the method is JITed.
71  */
72
73 static GPtrArray *method_breakpoints = NULL;
74
75 MonoDebugMethodAddressList *
76 mono_debugger_insert_method_breakpoint (MonoMethod *method, guint64 index)
77 {
78         MethodBreakpointInfo *info;
79
80         info = g_new0 (MethodBreakpointInfo, 1);
81         info->method = method;
82         info->index = index;
83
84         info->address_list = mono_debug_lookup_method_addresses (method);
85
86         if (!method_breakpoints)
87                 method_breakpoints = g_ptr_array_new ();
88
89         g_ptr_array_add (method_breakpoints, info);
90
91         return info->address_list;
92 }
93
94 int
95 mono_debugger_remove_method_breakpoint (guint64 index)
96 {
97         int i;
98
99         if (!method_breakpoints)
100                 return 0;
101
102         for (i = 0; i < method_breakpoints->len; i++) {
103                 MethodBreakpointInfo *info = g_ptr_array_index (method_breakpoints, i);
104
105                 if (info->index != index)
106                         continue;
107
108                 g_ptr_array_remove (method_breakpoints, info);
109                 g_free (info->address_list);
110                 g_free (info);
111                 return 1;
112         }
113
114         return 0;
115 }