/
mono-debug-debugger.c
388 lines (306 loc) · 8.56 KB
/
mono-debug-debugger.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <config.h>
#include <stdlib.h>
#include <string.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/metadata.h>
#include <mono/metadata/tabledefs.h>
#include <mono/metadata/tokentype.h>
#include <mono/metadata/appdomain.h>
#include <mono/metadata/gc-internal.h>
#include <mono/metadata/threads.h>
#include <mono/os/gc_wrapper.h>
#include <mono/metadata/object-internals.h>
#include <mono/metadata/class-internals.h>
#include <mono/metadata/exception.h>
#include <mono/metadata/mono-debug.h>
#include <mono/metadata/mono-debug-debugger.h>
#include <mono/metadata/mono-endian.h>
static guint32 debugger_lock_level = 0;
static CRITICAL_SECTION debugger_lock_mutex;
static gboolean must_reload_symtabs = FALSE;
static gboolean mono_debugger_use_debugger = FALSE;
static MonoObject *last_exception = NULL;
void (*mono_debugger_event_handler) (MonoDebuggerEvent event, guint64 data, guint64 arg) = NULL;
#define WRITE_UINT32(ptr,value) G_STMT_START { \
* ((guint32 *) ptr) = value; \
ptr += 4; \
} G_STMT_END
#define WRITE_POINTER(ptr,value) G_STMT_START { \
* ((gpointer *) ptr) = (gpointer) (value); \
ptr += sizeof (gpointer); \
} G_STMT_END
#define WRITE_STRING(ptr,value) G_STMT_START { \
memcpy (ptr, value, strlen (value)+1); \
ptr += strlen (value)+1; \
} G_STMT_END
typedef struct {
gpointer stack_pointer;
MonoObject *exception_obj;
guint32 stop;
} MonoDebuggerExceptionInfo;
static int initialized = 0;
void
mono_debugger_lock (void)
{
g_assert (initialized);
EnterCriticalSection (&debugger_lock_mutex);
debugger_lock_level++;
}
void
mono_debugger_unlock (void)
{
g_assert (initialized);
if (debugger_lock_level == 1) {
if (must_reload_symtabs && mono_debugger_use_debugger) {
mono_debugger_event (MONO_DEBUGGER_EVENT_RELOAD_SYMTABS, 0, 0);
must_reload_symtabs = FALSE;
}
}
debugger_lock_level--;
LeaveCriticalSection (&debugger_lock_mutex);
}
void
mono_debugger_initialize (gboolean use_debugger)
{
MONO_GC_REGISTER_ROOT (last_exception);
g_assert (!mono_debugger_use_debugger);
InitializeCriticalSection (&debugger_lock_mutex);
mono_debugger_use_debugger = use_debugger;
initialized = 1;
}
void
mono_debugger_add_symbol_file (MonoDebugHandle *handle)
{
g_assert (mono_debugger_use_debugger);
mono_debugger_lock ();
mono_debugger_event (MONO_DEBUGGER_EVENT_ADD_MODULE, (guint64) (gsize) handle, 0);
mono_debugger_unlock ();
}
void
mono_debugger_start_add_type (MonoDebugHandle *symfile, MonoClass *klass)
{
must_reload_symtabs = TRUE;
}
void
mono_debugger_event (MonoDebuggerEvent event, guint64 data, guint64 arg)
{
if (mono_debugger_event_handler)
(* mono_debugger_event_handler) (event, data, arg);
}
void
mono_debugger_cleanup (void)
{
/* Do nothing yet. */
}
/*
* 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 *breakpoints = NULL;
int
mono_debugger_insert_breakpoint_full (MonoMethodDesc *desc)
{
static int last_breakpoint_id = 0;
MonoDebuggerBreakpointInfo *info;
info = g_new0 (MonoDebuggerBreakpointInfo, 1);
info->desc = desc;
info->index = ++last_breakpoint_id;
if (!breakpoints)
breakpoints = g_ptr_array_new ();
g_ptr_array_add (breakpoints, info);
return info->index;
}
int
mono_debugger_remove_breakpoint (int breakpoint_id)
{
int i;
if (!breakpoints)
return 0;
for (i = 0; i < breakpoints->len; i++) {
MonoDebuggerBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
if (info->index != breakpoint_id)
continue;
mono_method_desc_free (info->desc);
g_ptr_array_remove (breakpoints, info);
g_free (info);
return 1;
}
return 0;
}
int
mono_debugger_insert_breakpoint (const gchar *method_name, gboolean include_namespace)
{
MonoMethodDesc *desc;
desc = mono_method_desc_new (method_name, include_namespace);
if (!desc)
return 0;
return mono_debugger_insert_breakpoint_full (desc);
}
int
mono_debugger_method_has_breakpoint (MonoMethod *method)
{
int i;
if (!breakpoints || (method->wrapper_type != MONO_WRAPPER_NONE))
return 0;
for (i = 0; i < breakpoints->len; i++) {
MonoDebuggerBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
if (!mono_method_desc_full_match (info->desc, method))
continue;
return info->index;
}
return 0;
}
void
mono_debugger_breakpoint_callback (MonoMethod *method, guint32 index)
{
mono_debugger_event (MONO_DEBUGGER_EVENT_JIT_BREAKPOINT, (guint64) (gsize) method, index);
}
gboolean
mono_debugger_unhandled_exception (gpointer addr, gpointer stack, MonoObject *exc)
{
const gchar *name;
if (!mono_debugger_use_debugger)
return FALSE;
// Prevent the object from being finalized.
last_exception = exc;
name = mono_class_get_name (mono_object_get_class (exc));
if (!strcmp (name, "ThreadAbortException")) {
MonoThread *thread = mono_thread_current ();
mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_ABORT, 0, thread->tid);
mono_thread_exit ();
}
mono_debugger_event (MONO_DEBUGGER_EVENT_UNHANDLED_EXCEPTION,
(guint64) (gsize) exc, (guint64) (gsize) addr);
return TRUE;
}
void
mono_debugger_handle_exception (gpointer addr, gpointer stack, MonoObject *exc)
{
MonoDebuggerExceptionInfo info;
if (!mono_debugger_use_debugger)
return;
// Prevent the object from being finalized.
last_exception = exc;
info.stack_pointer = stack;
info.exception_obj = exc;
info.stop = 0;
mono_debugger_event (MONO_DEBUGGER_EVENT_HANDLE_EXCEPTION, (guint64) (gsize) &info,
(guint64) (gsize) addr);
}
gboolean
mono_debugger_throw_exception (gpointer addr, gpointer stack, MonoObject *exc)
{
MonoDebuggerExceptionInfo info;
if (!mono_debugger_use_debugger)
return FALSE;
// Prevent the object from being finalized.
last_exception = exc;
info.stack_pointer = stack;
info.exception_obj = exc;
info.stop = 0;
mono_debugger_event (MONO_DEBUGGER_EVENT_THROW_EXCEPTION, (guint64) (gsize) &info,
(guint64) (gsize) addr);
return info.stop != 0;
}
static gchar *
get_exception_message (MonoObject *exc)
{
char *message = NULL;
MonoString *str;
MonoMethod *method;
MonoClass *klass;
gint i;
if (mono_object_isinst (exc, mono_defaults.exception_class)) {
klass = exc->vtable->klass;
method = NULL;
while (klass && method == NULL) {
for (i = 0; i < klass->method.count; ++i) {
method = klass->methods [i];
if (!strcmp ("ToString", method->name) &&
mono_method_signature (method)->param_count == 0 &&
method->flags & METHOD_ATTRIBUTE_VIRTUAL &&
method->flags & METHOD_ATTRIBUTE_PUBLIC) {
break;
}
method = NULL;
}
if (method == NULL)
klass = klass->parent;
}
g_assert (method);
str = (MonoString *) mono_runtime_invoke (method, exc, NULL, NULL);
if (str)
message = mono_string_to_utf8 (str);
}
return message;
}
MonoObject *
mono_debugger_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
{
MonoObject *retval;
gchar *message;
if (!strcmp (method->name, ".ctor")) {
retval = obj = mono_object_new (mono_domain_get (), method->klass);
mono_runtime_invoke (method, obj, params, exc);
} else
retval = mono_runtime_invoke (method, obj, params, exc);
if (!exc || (*exc == NULL))
return retval;
message = get_exception_message (*exc);
if (message) {
*exc = (MonoObject *) mono_string_new_wrapper (message);
g_free (message);
}
return retval;
}
gboolean
mono_debugger_lookup_type (const gchar *type_name)
{
int i;
mono_debugger_lock ();
for (i = 0; i < mono_symbol_table->num_symbol_files; i++) {
MonoDebugHandle *symfile = mono_symbol_table->symbol_files [i];
MonoType *type;
MonoClass* klass;
gchar *name;
name = g_strdup (type_name);
type = mono_reflection_type_from_name (name, symfile->image);
g_free (name);
if (!type)
continue;
klass = mono_class_from_mono_type (type);
if (klass)
mono_class_init (klass);
mono_debugger_unlock ();
return TRUE;
}
mono_debugger_unlock ();
return FALSE;
}
gint32
mono_debugger_lookup_assembly (const gchar *name)
{
MonoAssembly *assembly;
MonoImageOpenStatus status;
int i;
mono_debugger_lock ();
again:
for (i = 0; i < mono_symbol_table->num_symbol_files; i++) {
MonoDebugHandle *symfile = mono_symbol_table->symbol_files [i];
if (!strcmp (symfile->image_file, name)) {
mono_debugger_unlock ();
return i;
}
}
assembly = mono_assembly_open (name, &status);
if (status != MONO_IMAGE_OK) {
g_warning (G_STRLOC ": Cannot open image `%s'", name);
mono_debugger_unlock ();
return -1;
}
must_reload_symtabs = TRUE;
goto again;
}