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_event (MonoDebuggerEvent event, guint64 data, guint64 arg)
98 if (mono_debugger_event_handler)
99 (* mono_debugger_event_handler) (event, data, arg);
103 mono_debugger_cleanup (void)
105 mono_debugger_event (MONO_DEBUGGER_EVENT_FINALIZE_MANAGED_CODE, 0, 0);
106 mono_debugger_event_handler = NULL;
110 mono_debugger_unhandled_exception (gpointer addr, gpointer stack, MonoObject *exc)
114 if (!mono_debugger_use_debugger)
117 // Prevent the object from being finalized.
118 last_exception = exc;
120 name = mono_class_get_name (mono_object_get_class (exc));
121 if (!strcmp (name, "ThreadAbortException")) {
122 MonoThread *thread = mono_thread_current ();
123 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_ABORT, 0, thread->tid);
127 mono_debugger_event (MONO_DEBUGGER_EVENT_UNHANDLED_EXCEPTION,
128 (guint64) (gsize) exc, (guint64) (gsize) addr);
133 mono_debugger_handle_exception (gpointer addr, gpointer stack, MonoObject *exc)
135 MonoDebuggerExceptionInfo info;
137 if (!mono_debugger_use_debugger)
140 // Prevent the object from being finalized.
141 last_exception = exc;
143 info.stack_pointer = stack;
144 info.exception_obj = exc;
147 mono_debugger_event (MONO_DEBUGGER_EVENT_HANDLE_EXCEPTION, (guint64) (gsize) &info,
148 (guint64) (gsize) addr);
152 mono_debugger_throw_exception (gpointer addr, gpointer stack, MonoObject *exc)
154 MonoDebuggerExceptionInfo info;
156 if (!mono_debugger_use_debugger)
159 // Prevent the object from being finalized.
160 last_exception = exc;
162 info.stack_pointer = stack;
163 info.exception_obj = exc;
166 mono_debugger_event (MONO_DEBUGGER_EVENT_THROW_EXCEPTION, (guint64) (gsize) &info,
167 (guint64) (gsize) addr);
168 return info.stop != 0;
172 get_exception_message (MonoObject *exc)
174 char *message = NULL;
180 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
181 klass = exc->vtable->klass;
183 while (klass && method == NULL) {
184 for (i = 0; i < klass->method.count; ++i) {
185 method = klass->methods [i];
186 if (!strcmp ("ToString", method->name) &&
187 mono_method_signature (method)->param_count == 0 &&
188 method->flags & METHOD_ATTRIBUTE_VIRTUAL &&
189 method->flags & METHOD_ATTRIBUTE_PUBLIC) {
196 klass = klass->parent;
201 str = (MonoString *) mono_runtime_invoke (method, exc, NULL, NULL);
203 message = mono_string_to_utf8 (str);
210 mono_debugger_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
215 if (!strcmp (method->name, ".ctor")) {
216 retval = obj = mono_object_new (mono_domain_get (), method->klass);
218 mono_runtime_invoke (method, obj, params, exc);
220 retval = mono_runtime_invoke (method, obj, params, exc);
222 if (!exc || (*exc == NULL))
225 message = get_exception_message (*exc);
227 *exc = (MonoObject *) mono_string_new_wrapper (message);
235 mono_debugger_lookup_type (const gchar *type_name)
238 mono_debugger_lock ();
240 for (i = 0; i < mono_symbol_table->num_symbol_files; i++) {
241 MonoDebugHandle *symfile = mono_symbol_table->symbol_files [i];
246 name = g_strdup (type_name);
247 type = mono_reflection_type_from_name (name, symfile->image);
252 klass = mono_class_from_mono_type (type);
254 mono_class_init (klass);
256 mono_debugger_unlock ();
260 mono_debugger_unlock ();
265 mono_debugger_lookup_assembly (const gchar *name)
267 MonoAssembly *assembly;
268 MonoImageOpenStatus status;
271 mono_debugger_lock ();
274 for (i = 0; i < mono_symbol_table->num_symbol_files; i++) {
275 MonoDebugHandle *symfile = mono_symbol_table->symbol_files [i];
277 if (!strcmp (symfile->image_file, name)) {
278 mono_debugger_unlock ();
283 assembly = mono_assembly_open (name, &status);
285 if (status != MONO_IMAGE_OK) {
286 g_warning (G_STRLOC ": Cannot open image `%s'", name);
287 mono_debugger_unlock ();
291 must_reload_symtabs = TRUE;
296 mono_debugger_add_type (MonoDebugHandle *symfile, MonoClass *klass)
298 must_reload_symtabs = TRUE;