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/os/gc_wrapper.h>
11 #include <mono/metadata/object-internals.h>
12 #include <mono/metadata/class-internals.h>
13 #include <mono/metadata/exception.h>
14 #include <mono/metadata/mono-debug.h>
15 #include <mono/metadata/mono-debug-debugger.h>
16 #include <mono/metadata/mono-endian.h>
18 static guint32 debugger_lock_level = 0;
19 static CRITICAL_SECTION debugger_lock_mutex;
20 static gboolean must_reload_symtabs = FALSE;
21 static gboolean mono_debugger_use_debugger = FALSE;
22 static MonoObject *last_exception = NULL;
24 struct _MonoDebuggerMetadataInfo {
26 int mono_defaults_size;
27 MonoDefaults *mono_defaults;
28 int klass_field_offset;
29 int klass_methods_offset;
30 int klass_method_count_offset;
34 void (*mono_debugger_event_handler) (MonoDebuggerEvent event, guint64 data, guint64 arg) = NULL;
36 #define WRITE_UINT32(ptr,value) G_STMT_START { \
37 * ((guint32 *) ptr) = value; \
41 #define WRITE_POINTER(ptr,value) G_STMT_START { \
42 * ((gpointer *) ptr) = (gpointer) (value); \
43 ptr += sizeof (gpointer); \
46 #define WRITE_STRING(ptr,value) G_STMT_START { \
47 memcpy (ptr, value, strlen (value)+1); \
48 ptr += strlen (value)+1; \
52 gpointer stack_pointer;
53 MonoObject *exception_obj;
55 } MonoDebuggerExceptionInfo;
57 #ifndef PLATFORM_WIN32
59 MonoDebuggerIOLayer mono_debugger_io_layer = {
60 InitializeCriticalSection, DeleteCriticalSection, TryEnterCriticalSection,
61 EnterCriticalSection, LeaveCriticalSection, WaitForSingleObjectEx, SignalObjectAndWait,
62 WaitForMultipleObjectsEx, CreateSemaphore, ReleaseSemaphore, CreateThread,
68 static int initialized = 0;
71 mono_debugger_lock (void)
73 g_assert (initialized);
74 EnterCriticalSection (&debugger_lock_mutex);
75 debugger_lock_level++;
79 mono_debugger_unlock (void)
81 g_assert (initialized);
82 if (debugger_lock_level == 1) {
83 if (must_reload_symtabs && mono_debugger_use_debugger) {
84 mono_debugger_event (MONO_DEBUGGER_EVENT_RELOAD_SYMTABS, 0, 0);
85 must_reload_symtabs = FALSE;
89 debugger_lock_level--;
90 LeaveCriticalSection (&debugger_lock_mutex);
94 mono_debugger_initialize (gboolean use_debugger)
96 MONO_GC_REGISTER_ROOT (last_exception);
98 g_assert (!mono_debugger_use_debugger);
100 InitializeCriticalSection (&debugger_lock_mutex);
101 mono_debugger_use_debugger = use_debugger;
106 mono_debugger_add_symbol_file (MonoDebugHandle *handle)
108 g_assert (mono_debugger_use_debugger);
110 mono_debugger_lock ();
111 mono_debugger_event (MONO_DEBUGGER_EVENT_ADD_MODULE, GPOINTER_TO_UINT (handle), 0);
112 mono_debugger_unlock ();
116 mono_debugger_add_builtin_types (MonoDebugHandle *symfile)
118 MonoDebuggerMetadataInfo *info;
121 mono_symbol_table->corlib = symfile;
122 mono_symbol_table->metadata_info = info = g_new0 (MonoDebuggerMetadataInfo, 1);
124 info->size = sizeof (MonoDebuggerMetadataInfo);
125 info->mono_defaults = &mono_defaults;
126 info->mono_defaults_size = sizeof (MonoDefaults);
127 info->klass_field_offset = (guint8*)&klass.fields - (guint8*)&klass;
128 info->klass_methods_offset = (guint8*)&klass.methods - (guint8*)&klass;
129 info->klass_method_count_offset = (guint8*)&klass.method.count - (guint8*)&klass;
130 info->field_info_size = sizeof (MonoClassField);
134 mono_debugger_start_add_type (MonoDebugHandle *symfile, MonoClass *klass)
136 must_reload_symtabs = TRUE;
140 mono_debugger_event (MonoDebuggerEvent event, guint64 data, guint64 arg)
142 if (mono_debugger_event_handler)
143 (* mono_debugger_event_handler) (event, data, arg);
147 mono_debugger_cleanup (void)
149 /* Do nothing yet. */
153 * Debugger breakpoint interface.
155 * This interface is used to insert breakpoints on methods which are not yet JITed.
156 * The debugging code keeps a list of all such breakpoints and automatically inserts the
157 * breakpoint when the method is JITed.
160 static GPtrArray *breakpoints = NULL;
163 mono_debugger_insert_breakpoint_full (MonoMethodDesc *desc)
165 static int last_breakpoint_id = 0;
166 MonoDebuggerBreakpointInfo *info;
168 info = g_new0 (MonoDebuggerBreakpointInfo, 1);
170 info->index = ++last_breakpoint_id;
173 breakpoints = g_ptr_array_new ();
175 g_ptr_array_add (breakpoints, info);
181 mono_debugger_remove_breakpoint (int breakpoint_id)
188 for (i = 0; i < breakpoints->len; i++) {
189 MonoDebuggerBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
191 if (info->index != breakpoint_id)
194 mono_method_desc_free (info->desc);
195 g_ptr_array_remove (breakpoints, info);
204 mono_debugger_insert_breakpoint (const gchar *method_name, gboolean include_namespace)
206 MonoMethodDesc *desc;
208 desc = mono_method_desc_new (method_name, include_namespace);
212 return mono_debugger_insert_breakpoint_full (desc);
216 mono_debugger_method_has_breakpoint (MonoMethod *method)
220 if (!breakpoints || (method->wrapper_type != MONO_WRAPPER_NONE))
223 for (i = 0; i < breakpoints->len; i++) {
224 MonoDebuggerBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
226 if (!mono_method_desc_full_match (info->desc, method))
236 mono_debugger_breakpoint_callback (MonoMethod *method, guint32 index)
238 mono_debugger_event (MONO_DEBUGGER_EVENT_BREAKPOINT, GPOINTER_TO_UINT (method), index);
242 mono_debugger_unhandled_exception (gpointer addr, gpointer stack, MonoObject *exc)
244 if (!mono_debugger_use_debugger)
247 // Prevent the object from being finalized.
248 last_exception = exc;
249 mono_debugger_event (MONO_DEBUGGER_EVENT_UNHANDLED_EXCEPTION,
250 GPOINTER_TO_UINT (exc), GPOINTER_TO_UINT (addr));
255 mono_debugger_handle_exception (gpointer addr, gpointer stack, MonoObject *exc)
257 MonoDebuggerExceptionInfo info;
259 if (!mono_debugger_use_debugger)
262 // Prevent the object from being finalized.
263 last_exception = exc;
265 info.stack_pointer = stack;
266 info.exception_obj = exc;
269 mono_debugger_event (MONO_DEBUGGER_EVENT_EXCEPTION, GPOINTER_TO_UINT (&info),
270 GPOINTER_TO_UINT (addr));
274 mono_debugger_throw_exception (gpointer addr, gpointer stack, MonoObject *exc)
276 MonoDebuggerExceptionInfo info;
278 if (!mono_debugger_use_debugger)
281 // Prevent the object from being finalized.
282 last_exception = exc;
284 info.stack_pointer = stack;
285 info.exception_obj = exc;
288 mono_debugger_event (MONO_DEBUGGER_EVENT_THROW_EXCEPTION, GPOINTER_TO_UINT (&info),
289 GPOINTER_TO_UINT (addr));
290 return info.stop != 0;
294 get_exception_message (MonoObject *exc)
296 char *message = NULL;
302 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
303 klass = exc->vtable->klass;
305 while (klass && method == NULL) {
306 for (i = 0; i < klass->method.count; ++i) {
307 method = klass->methods [i];
308 if (!strcmp ("ToString", method->name) &&
309 mono_method_signature (method)->param_count == 0 &&
310 method->flags & METHOD_ATTRIBUTE_VIRTUAL &&
311 method->flags & METHOD_ATTRIBUTE_PUBLIC) {
318 klass = klass->parent;
323 str = (MonoString *) mono_runtime_invoke (method, exc, NULL, NULL);
325 message = mono_string_to_utf8 (str);
332 mono_debugger_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
337 if (!strcmp (method->name, ".ctor")) {
338 retval = obj = mono_object_new (mono_domain_get (), method->klass);
340 mono_runtime_invoke (method, obj, params, exc);
342 retval = mono_runtime_invoke (method, obj, params, exc);
344 if (!exc || (*exc == NULL))
347 message = get_exception_message (*exc);
349 *exc = (MonoObject *) mono_string_new_wrapper (message);
357 mono_debugger_lookup_type (const gchar *type_name)
360 mono_debugger_lock ();
362 for (i = 0; i < mono_symbol_table->num_symbol_files; i++) {
363 MonoDebugHandle *symfile = mono_symbol_table->symbol_files [i];
368 name = g_strdup (type_name);
369 type = mono_reflection_type_from_name (name, symfile->image);
374 klass = mono_class_from_mono_type (type);
376 mono_class_init (klass);
378 mono_debugger_unlock ();
382 mono_debugger_unlock ();
387 mono_debugger_lookup_assembly (const gchar *name)
389 MonoAssembly *assembly;
390 MonoImageOpenStatus status;
393 mono_debugger_lock ();
396 for (i = 0; i < mono_symbol_table->num_symbol_files; i++) {
397 MonoDebugHandle *symfile = mono_symbol_table->symbol_files [i];
399 if (!strcmp (symfile->image_file, name)) {
400 mono_debugger_unlock ();
405 assembly = mono_assembly_open (name, &status);
407 if (status != MONO_IMAGE_OK) {
408 g_warning (G_STRLOC ": Cannot open image `%s'", name);
409 mono_debugger_unlock ();
413 must_reload_symtabs = TRUE;