4 #include <mono/metadata/assembly.h>
5 #include <mono/metadata/metadata.h>
6 #include <mono/metadata/tabledefs.h>
7 #include <mono/metadata/tokentype.h>
8 #include <mono/metadata/appdomain.h>
9 #include <mono/metadata/gc-internal.h>
10 #include <mono/metadata/threads.h>
11 #include <mono/os/gc_wrapper.h>
12 #include <mono/metadata/object-internals.h>
13 #include <mono/metadata/class-internals.h>
14 #include <mono/metadata/exception.h>
15 #include <mono/metadata/mono-debug.h>
16 #include <mono/metadata/mono-debug-debugger.h>
17 #include <mono/metadata/mono-endian.h>
19 static guint32 debugger_lock_level = 0;
20 static CRITICAL_SECTION debugger_lock_mutex;
21 static gboolean must_reload_symtabs = FALSE;
22 static gboolean mono_debugger_use_debugger = FALSE;
23 static MonoObject *last_exception = NULL;
25 void (*mono_debugger_event_handler) (MonoDebuggerEvent event, guint64 data, guint64 arg) = NULL;
27 #define WRITE_UINT32(ptr,value) G_STMT_START { \
28 * ((guint32 *) ptr) = value; \
32 #define WRITE_POINTER(ptr,value) G_STMT_START { \
33 * ((gpointer *) ptr) = (gpointer) (value); \
34 ptr += sizeof (gpointer); \
37 #define WRITE_STRING(ptr,value) G_STMT_START { \
38 memcpy (ptr, value, strlen (value)+1); \
39 ptr += strlen (value)+1; \
43 gpointer stack_pointer;
44 MonoObject *exception_obj;
46 } MonoDebuggerExceptionInfo;
48 static int initialized = 0;
51 mono_debugger_lock (void)
53 g_assert (initialized);
54 EnterCriticalSection (&debugger_lock_mutex);
55 debugger_lock_level++;
59 mono_debugger_unlock (void)
61 g_assert (initialized);
62 if (debugger_lock_level == 1) {
63 if (must_reload_symtabs && mono_debugger_use_debugger) {
64 mono_debugger_event (MONO_DEBUGGER_EVENT_RELOAD_SYMTABS, 0, 0);
65 must_reload_symtabs = FALSE;
69 debugger_lock_level--;
70 LeaveCriticalSection (&debugger_lock_mutex);
74 mono_debugger_initialize (gboolean use_debugger)
76 MONO_GC_REGISTER_ROOT (last_exception);
78 g_assert (!mono_debugger_use_debugger);
80 InitializeCriticalSection (&debugger_lock_mutex);
81 mono_debugger_use_debugger = use_debugger;
86 mono_debugger_add_symbol_file (MonoDebugHandle *handle)
88 g_assert (mono_debugger_use_debugger);
90 mono_debugger_lock ();
91 mono_debugger_event (MONO_DEBUGGER_EVENT_ADD_MODULE, (guint64) (gsize) handle, 0);
92 mono_debugger_unlock ();
96 mono_debugger_start_add_type (MonoDebugHandle *symfile, MonoClass *klass)
98 must_reload_symtabs = TRUE;
102 mono_debugger_event (MonoDebuggerEvent event, guint64 data, guint64 arg)
104 if (mono_debugger_event_handler)
105 (* mono_debugger_event_handler) (event, data, arg);
109 mono_debugger_cleanup (void)
111 /* Do nothing yet. */
115 * Debugger breakpoint interface.
117 * This interface is used to insert breakpoints on methods which are not yet JITed.
118 * The debugging code keeps a list of all such breakpoints and automatically inserts the
119 * breakpoint when the method is JITed.
122 static GPtrArray *breakpoints = NULL;
125 mono_debugger_insert_breakpoint_full (MonoMethodDesc *desc)
127 static int last_breakpoint_id = 0;
128 MonoDebuggerBreakpointInfo *info;
130 info = g_new0 (MonoDebuggerBreakpointInfo, 1);
132 info->index = ++last_breakpoint_id;
135 breakpoints = g_ptr_array_new ();
137 g_ptr_array_add (breakpoints, info);
143 mono_debugger_remove_breakpoint (int breakpoint_id)
150 for (i = 0; i < breakpoints->len; i++) {
151 MonoDebuggerBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
153 if (info->index != breakpoint_id)
156 mono_method_desc_free (info->desc);
157 g_ptr_array_remove (breakpoints, info);
166 mono_debugger_insert_breakpoint (const gchar *method_name, gboolean include_namespace)
168 MonoMethodDesc *desc;
170 desc = mono_method_desc_new (method_name, include_namespace);
174 return mono_debugger_insert_breakpoint_full (desc);
178 mono_debugger_method_has_breakpoint (MonoMethod *method)
182 if (!breakpoints || (method->wrapper_type != MONO_WRAPPER_NONE))
185 for (i = 0; i < breakpoints->len; i++) {
186 MonoDebuggerBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
188 if (!mono_method_desc_full_match (info->desc, method))
198 mono_debugger_breakpoint_callback (MonoMethod *method, guint32 index)
200 mono_debugger_event (MONO_DEBUGGER_EVENT_JIT_BREAKPOINT, (guint64) (gsize) method, index);
204 mono_debugger_unhandled_exception (gpointer addr, gpointer stack, MonoObject *exc)
208 if (!mono_debugger_use_debugger)
211 // Prevent the object from being finalized.
212 last_exception = exc;
214 name = mono_class_get_name (mono_object_get_class (exc));
215 if (!strcmp (name, "ThreadAbortException")) {
216 MonoThread *thread = mono_thread_current ();
217 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_ABORT, 0, thread->tid);
221 mono_debugger_event (MONO_DEBUGGER_EVENT_UNHANDLED_EXCEPTION,
222 (guint64) (gsize) exc, (guint64) (gsize) addr);
227 mono_debugger_handle_exception (gpointer addr, gpointer stack, MonoObject *exc)
229 MonoDebuggerExceptionInfo info;
231 if (!mono_debugger_use_debugger)
234 // Prevent the object from being finalized.
235 last_exception = exc;
237 info.stack_pointer = stack;
238 info.exception_obj = exc;
241 mono_debugger_event (MONO_DEBUGGER_EVENT_HANDLE_EXCEPTION, (guint64) (gsize) &info,
242 (guint64) (gsize) addr);
246 mono_debugger_throw_exception (gpointer addr, gpointer stack, MonoObject *exc)
248 MonoDebuggerExceptionInfo info;
250 if (!mono_debugger_use_debugger)
253 // Prevent the object from being finalized.
254 last_exception = exc;
256 info.stack_pointer = stack;
257 info.exception_obj = exc;
260 mono_debugger_event (MONO_DEBUGGER_EVENT_THROW_EXCEPTION, (guint64) (gsize) &info,
261 (guint64) (gsize) addr);
262 return info.stop != 0;
266 get_exception_message (MonoObject *exc)
268 char *message = NULL;
274 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
275 klass = exc->vtable->klass;
277 while (klass && method == NULL) {
278 for (i = 0; i < klass->method.count; ++i) {
279 method = klass->methods [i];
280 if (!strcmp ("ToString", method->name) &&
281 mono_method_signature (method)->param_count == 0 &&
282 method->flags & METHOD_ATTRIBUTE_VIRTUAL &&
283 method->flags & METHOD_ATTRIBUTE_PUBLIC) {
290 klass = klass->parent;
295 str = (MonoString *) mono_runtime_invoke (method, exc, NULL, NULL);
297 message = mono_string_to_utf8 (str);
304 mono_debugger_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
309 if (!strcmp (method->name, ".ctor")) {
310 retval = obj = mono_object_new (mono_domain_get (), method->klass);
312 mono_runtime_invoke (method, obj, params, exc);
314 retval = mono_runtime_invoke (method, obj, params, exc);
316 if (!exc || (*exc == NULL))
319 message = get_exception_message (*exc);
321 *exc = (MonoObject *) mono_string_new_wrapper (message);
329 mono_debugger_lookup_type (const gchar *type_name)
332 mono_debugger_lock ();
334 for (i = 0; i < mono_symbol_table->num_symbol_files; i++) {
335 MonoDebugHandle *symfile = mono_symbol_table->symbol_files [i];
340 name = g_strdup (type_name);
341 type = mono_reflection_type_from_name (name, symfile->image);
346 klass = mono_class_from_mono_type (type);
348 mono_class_init (klass);
350 mono_debugger_unlock ();
354 mono_debugger_unlock ();
359 mono_debugger_lookup_assembly (const gchar *name)
361 MonoAssembly *assembly;
362 MonoImageOpenStatus status;
365 mono_debugger_lock ();
368 for (i = 0; i < mono_symbol_table->num_symbol_files; i++) {
369 MonoDebugHandle *symfile = mono_symbol_table->symbol_files [i];
371 if (!strcmp (symfile->image_file, name)) {
372 mono_debugger_unlock ();
377 assembly = mono_assembly_open (name, &status);
379 if (status != MONO_IMAGE_OK) {
380 g_warning (G_STRLOC ": Cannot open image `%s'", name);
381 mono_debugger_unlock ();
385 must_reload_symtabs = TRUE;