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 #define SYMFILE_TABLE_CHUNK_SIZE 16
19 #define RANGE_TABLE_CHUNK_SIZE 256
20 #define CLASS_TABLE_CHUNK_SIZE 256
21 #define TYPE_TABLE_PTR_CHUNK_SIZE 256
22 #define TYPE_TABLE_CHUNK_SIZE 65536
23 #define MISC_TABLE_PTR_CHUNK_SIZE 256
24 #define MISC_TABLE_CHUNK_SIZE 65536
26 static guint32 debugger_lock_level = 0;
27 static CRITICAL_SECTION debugger_lock_mutex;
28 static gboolean mono_debugger_initialized = FALSE;
29 static MonoObject *last_exception = NULL;
31 static gboolean must_reload_symtabs = FALSE;
32 static gboolean builtin_types_initialized = FALSE;
34 static GHashTable *images = NULL;
35 static GHashTable *type_table = NULL;
36 static GHashTable *misc_table = NULL;
37 static GHashTable *class_table = NULL;
38 static GHashTable *class_info_table = NULL;
40 static MonoDebuggerRangeInfo *allocate_range_entry (MonoDebuggerSymbolFile *symfile);
41 static MonoDebuggerClassInfo *allocate_class_entry (MonoDebuggerSymbolFile *symfile);
42 static guint32 allocate_type_entry (MonoDebuggerSymbolTable *table, guint32 size, guint8 **ptr);
43 static guint32 write_type (MonoDebuggerSymbolTable *table, MonoType *type);
44 static guint32 do_write_class (MonoDebuggerSymbolTable *table, MonoClass *klass,
45 MonoDebuggerClassInfo *cinfo);
46 static guint32 write_class (MonoDebuggerSymbolTable *table, MonoClass *klass);
48 MonoDebuggerSymbolTable *mono_debugger_symbol_table = NULL;
49 void (*mono_debugger_event_handler) (MonoDebuggerEvent event, gpointer data, guint32 arg) = NULL;
51 #define WRITE_UINT32(ptr,value) G_STMT_START { \
52 * ((guint32 *) ptr) = value; \
56 #define WRITE_POINTER(ptr,value) G_STMT_START { \
57 * ((gpointer *) ptr) = value; \
58 ptr += sizeof (gpointer); \
61 #define WRITE_STRING(ptr,value) G_STMT_START { \
62 memcpy (ptr, value, strlen (value)+1); \
63 ptr += strlen (value)+1; \
67 gpointer stack_pointer;
68 MonoObject *exception_obj;
70 } MonoDebuggerExceptionInfo;
72 #ifndef PLATFORM_WIN32
74 MonoDebuggerIOLayer mono_debugger_io_layer = {
75 InitializeCriticalSection, DeleteCriticalSection, TryEnterCriticalSection,
76 EnterCriticalSection, LeaveCriticalSection, WaitForSingleObjectEx, SignalObjectAndWait,
77 WaitForMultipleObjectsEx, CreateSemaphore, ReleaseSemaphore, CreateThread,
84 mono_debugger_lock (void)
86 if (!mono_debugger_initialized) {
87 debugger_lock_level++;
91 EnterCriticalSection (&debugger_lock_mutex);
92 debugger_lock_level++;
96 mono_debugger_unlock (void)
98 g_assert (debugger_lock_level > 0);
100 if (!mono_debugger_initialized) {
101 debugger_lock_level--;
105 if (debugger_lock_level == 1) {
106 if (must_reload_symtabs) {
107 mono_debugger_event (MONO_DEBUGGER_EVENT_RELOAD_SYMTABS, NULL, 0);
108 must_reload_symtabs = FALSE;
112 debugger_lock_level--;
113 LeaveCriticalSection (&debugger_lock_mutex);
116 static MonoDebuggerSymbolFile *
117 allocate_symbol_file_entry (MonoDebuggerSymbolTable *table)
119 MonoDebuggerSymbolFile *symfile;
121 if (!table->symbol_files)
122 table->symbol_files = g_new0 (MonoDebuggerSymbolFile *, SYMFILE_TABLE_CHUNK_SIZE);
123 else if (!((table->num_symbol_files + 1) % SYMFILE_TABLE_CHUNK_SIZE)) {
124 guint32 chunks = (table->num_symbol_files + 1) / SYMFILE_TABLE_CHUNK_SIZE;
125 guint32 size = sizeof (MonoDebuggerSymbolFile *) * SYMFILE_TABLE_CHUNK_SIZE * (chunks + 1);
127 table->symbol_files = g_realloc (table->symbol_files, size);
130 symfile = g_new0 (MonoDebuggerSymbolFile, 1);
131 symfile->index = table->num_symbol_files;
132 symfile->range_entry_size = sizeof (MonoDebuggerRangeInfo);
133 symfile->class_entry_size = sizeof (MonoDebuggerClassInfo);
134 symfile->class_table_size = sizeof (MonoDebuggerClassTable);
135 table->symbol_files [table->num_symbol_files++] = symfile;
140 mono_debugger_initialize (void)
142 MonoDebuggerSymbolTable *symbol_table;
144 MONO_GC_REGISTER_ROOT (last_exception);
146 g_assert (!mono_debugger_initialized);
148 InitializeCriticalSection (&debugger_lock_mutex);
149 mono_debugger_initialized = TRUE;
151 mono_debugger_lock ();
153 symbol_table = g_new0 (MonoDebuggerSymbolTable, 1);
154 symbol_table->magic = MONO_DEBUGGER_MAGIC;
155 symbol_table->version = MONO_DEBUGGER_VERSION;
156 symbol_table->total_size = sizeof (MonoDebuggerSymbolTable);
158 images = g_hash_table_new (g_direct_hash, g_direct_equal);
159 type_table = g_hash_table_new (g_direct_hash, g_direct_equal);
160 misc_table = g_hash_table_new (g_direct_hash, g_direct_equal);
161 class_table = g_hash_table_new (g_direct_hash, g_direct_equal);
162 class_info_table = g_hash_table_new (g_direct_hash, g_direct_equal);
164 mono_debugger_symbol_table = symbol_table;
166 mono_debugger_unlock ();
169 MonoDebuggerSymbolFile *
170 mono_debugger_add_symbol_file (MonoDebugHandle *handle)
172 MonoDebuggerSymbolFile *info;
174 g_assert (mono_debugger_initialized);
175 mono_debugger_lock ();
177 info = g_hash_table_lookup (images, handle->image);
179 mono_debugger_unlock ();
183 info = allocate_symbol_file_entry (mono_debugger_symbol_table);
184 info->symfile = handle->symfile;
185 info->image = handle->image;
186 info->image_file = handle->image_file;
188 g_hash_table_insert (images, handle->image, info);
189 mono_debugger_unlock ();
195 write_builtin_type (MonoDebuggerSymbolTable *table, MonoDebuggerSymbolFile *symfile,
196 MonoClass *klass, MonoDebuggerBuiltinTypeInfo *info)
198 guint8 buffer [BUFSIZ], *ptr = buffer;
201 g_assert (!klass->init_pending);
202 mono_class_init (klass);
204 switch (klass->byval_arg.type) {
206 *ptr++ = MONO_DEBUGGER_TYPE_KIND_UNKNOWN;
207 WRITE_UINT32 (ptr, 0);
211 case MONO_TYPE_BOOLEAN:
223 *ptr++ = MONO_DEBUGGER_TYPE_KIND_FUNDAMENTAL;
224 WRITE_UINT32 (ptr, klass->instance_size - sizeof (MonoObject));
228 case MONO_TYPE_STRING: {
231 *ptr++ = MONO_DEBUGGER_TYPE_KIND_STRING;
232 WRITE_UINT32 (ptr, klass->instance_size);
234 *ptr++ = (guint8*)&string.length - (guint8*)&string;
235 *ptr++ = sizeof (string.length);
236 *ptr++ = (guint8*)&string.chars - (guint8*)&string;
243 *ptr++ = MONO_DEBUGGER_TYPE_KIND_FUNDAMENTAL;
244 WRITE_UINT32 (ptr, sizeof (void *));
248 case MONO_TYPE_VALUETYPE:
249 *ptr++ = MONO_DEBUGGER_TYPE_KIND_STRUCT;
250 WRITE_UINT32 (ptr, klass->instance_size);
254 case MONO_TYPE_CLASS:
255 *ptr++ = MONO_DEBUGGER_TYPE_KIND_CLASS;
256 WRITE_UINT32 (ptr, klass->instance_size);
260 case MONO_TYPE_OBJECT:
261 g_assert (klass == mono_defaults.object_class);
262 *ptr++ = MONO_DEBUGGER_TYPE_KIND_OBJECT;
263 WRITE_UINT32 (ptr, klass->instance_size);
268 g_error (G_STRLOC ": Unknown builtin type %s.%s - %d", klass->name_space, klass->name, klass->byval_arg.type);
272 info->type_info = allocate_type_entry (table, size, &info->type_data);
273 memcpy (info->type_data, buffer, size);
275 info->centry->info = g_new0 (MonoDebuggerClassInfo, 1); //allocate_class_entry (symfile);
276 info->centry->info->klass = klass;
278 info->centry->info->token = klass->element_class->type_token;
279 info->centry->info->rank = klass->rank;
281 info->centry->info->token = klass->type_token;
282 info->centry->info->type_info = info->type_info;
285 static MonoDebuggerBuiltinTypeInfo *
286 add_builtin_type (MonoDebuggerSymbolFile *symfile, MonoClass *klass)
288 MonoDebuggerClassEntry *centry;
289 MonoDebuggerBuiltinTypeInfo *info;
291 centry = g_new0 (MonoDebuggerClassEntry, 1);
293 info = g_new0 (MonoDebuggerBuiltinTypeInfo, 1);
295 info->centry = centry;
297 g_hash_table_insert (class_info_table, klass, centry);
299 write_builtin_type (mono_debugger_symbol_table, symfile, klass, info);
304 add_builtin_type_2 (MonoDebuggerBuiltinTypeInfo *info)
306 info->class_info = do_write_class (mono_debugger_symbol_table, info->klass, NULL);
307 * (guint32 *) (info->type_data + 5) = info->class_info;
311 add_exception_class (MonoDebuggerSymbolFile *symfile, MonoException *exc)
313 mono_debugger_start_add_type (symfile, ((MonoObject *) exc)->vtable->klass);
314 mono_debugger_add_type (symfile, ((MonoObject *) exc)->vtable->klass);
317 MonoDebuggerBuiltinTypes *
318 mono_debugger_add_builtin_types (MonoDebuggerSymbolFile *symfile)
320 MonoDebuggerBuiltinTypes *types = g_new0 (MonoDebuggerBuiltinTypes, 1);
323 mono_debugger_symbol_table->corlib = symfile;
324 mono_debugger_symbol_table->builtin_types = types;
326 types->total_size = sizeof (MonoDebuggerBuiltinTypes);
327 types->type_info_size = sizeof (MonoDebuggerBuiltinTypeInfo);
329 types->object_type = add_builtin_type (symfile, mono_defaults.object_class);
330 klass = mono_class_from_name (mono_defaults.corlib, "System", "ValueType");
332 types->valuetype_type = add_builtin_type (symfile, klass);
334 types->byte_type = add_builtin_type (symfile, mono_defaults.byte_class);
335 types->void_type = add_builtin_type (symfile, mono_defaults.void_class);
336 types->boolean_type = add_builtin_type (symfile, mono_defaults.boolean_class);
337 types->sbyte_type = add_builtin_type (symfile, mono_defaults.sbyte_class);
338 types->int16_type = add_builtin_type (symfile, mono_defaults.int16_class);
339 types->uint16_type = add_builtin_type (symfile, mono_defaults.uint16_class);
340 types->int32_type = add_builtin_type (symfile, mono_defaults.int32_class);
341 types->uint32_type = add_builtin_type (symfile, mono_defaults.uint32_class);
342 types->int_type = add_builtin_type (symfile, mono_defaults.int_class);
343 types->uint_type = add_builtin_type (symfile, mono_defaults.uint_class);
344 types->int64_type = add_builtin_type (symfile, mono_defaults.int64_class);
345 types->uint64_type = add_builtin_type (symfile, mono_defaults.uint64_class);
346 types->single_type = add_builtin_type (symfile, mono_defaults.single_class);
347 types->double_type = add_builtin_type (symfile, mono_defaults.double_class);
348 types->char_type = add_builtin_type (symfile, mono_defaults.char_class);
349 types->string_type = add_builtin_type (symfile, mono_defaults.string_class);
351 klass = mono_class_from_name (mono_defaults.corlib, "System", "Type");
353 types->type_type = add_builtin_type (symfile, klass);
355 types->enum_type = add_builtin_type (symfile, mono_defaults.enum_class);
356 types->array_type = add_builtin_type (symfile, mono_defaults.array_class);
357 types->exception_type = add_builtin_type (symfile, mono_defaults.exception_class);
359 add_builtin_type_2 (types->object_type);
360 add_builtin_type_2 (types->valuetype_type);
362 add_builtin_type_2 (types->byte_type);
363 add_builtin_type_2 (types->void_type);
364 add_builtin_type_2 (types->boolean_type);
365 add_builtin_type_2 (types->sbyte_type);
366 add_builtin_type_2 (types->int16_type);
367 add_builtin_type_2 (types->uint16_type);
368 add_builtin_type_2 (types->int32_type);
369 add_builtin_type_2 (types->uint32_type);
370 add_builtin_type_2 (types->int_type);
371 add_builtin_type_2 (types->uint_type);
372 add_builtin_type_2 (types->int64_type);
373 add_builtin_type_2 (types->uint64_type);
374 add_builtin_type_2 (types->single_type);
375 add_builtin_type_2 (types->double_type);
376 add_builtin_type_2 (types->char_type);
377 add_builtin_type_2 (types->string_type);
378 add_builtin_type_2 (types->type_type);
380 add_builtin_type_2 (types->enum_type);
381 add_builtin_type_2 (types->array_type);
382 add_builtin_type_2 (types->exception_type);
384 add_exception_class (symfile, mono_get_exception_divide_by_zero ());
385 add_exception_class (symfile, mono_get_exception_security ());
386 add_exception_class (symfile, mono_get_exception_arithmetic ());
387 add_exception_class (symfile, mono_get_exception_overflow ());
388 add_exception_class (symfile, mono_get_exception_null_reference ());
389 add_exception_class (symfile, mono_get_exception_thread_abort ());
390 add_exception_class (symfile, mono_get_exception_invalid_cast ());
391 add_exception_class (symfile, mono_get_exception_index_out_of_range ());
392 add_exception_class (symfile, mono_get_exception_thread_abort ());
393 add_exception_class (symfile, mono_get_exception_index_out_of_range ());
394 add_exception_class (symfile, mono_get_exception_array_type_mismatch ());
395 add_exception_class (symfile, mono_get_exception_missing_method ());
396 add_exception_class (symfile, mono_get_exception_appdomain_unloaded ());
397 add_exception_class (symfile, mono_get_exception_stack_overflow ());
399 builtin_types_initialized = TRUE;
405 write_class (MonoDebuggerSymbolTable *table, MonoClass *klass)
407 MonoDebuggerClassEntry *centry;
409 if (builtin_types_initialized && !klass->init_pending)
410 mono_class_init (klass);
412 centry = g_hash_table_lookup (class_info_table, klass);
414 MonoDebuggerSymbolFile *symfile = _mono_debugger_get_symfile (klass->image);
417 mono_debugger_start_add_type (symfile, klass);
418 centry = g_hash_table_lookup (class_info_table, klass);
423 g_assert (centry->info->type_info);
424 return centry->info->type_info;
427 if (!centry->type_reference) {
430 centry->type_reference = allocate_type_entry (table, 5, &ptr);
432 *ptr++ = MONO_DEBUGGER_TYPE_KIND_REFERENCE;
433 WRITE_POINTER (ptr, klass);
436 return centry->type_reference;
440 mono_debugger_start_add_type (MonoDebuggerSymbolFile *symfile, MonoClass *klass)
442 MonoDebuggerClassEntry *centry;
444 mono_debugger_lock ();
445 centry = g_hash_table_lookup (class_info_table, klass);
447 mono_debugger_unlock ();
451 centry = g_new0 (MonoDebuggerClassEntry, 1);
452 g_hash_table_insert (class_info_table, klass, centry);
453 mono_debugger_unlock ();
457 mono_debugger_add_type (MonoDebuggerSymbolFile *symfile, MonoClass *klass)
459 MonoDebuggerClassEntry *centry;
461 mono_debugger_lock ();
462 centry = g_hash_table_lookup (class_info_table, klass);
466 mono_debugger_unlock ();
470 centry->info = allocate_class_entry (symfile);
471 centry->info->klass = klass;
473 centry->info->token = klass->element_class->type_token;
474 centry->info->rank = klass->rank;
476 centry->info->token = klass->type_token;
478 do_write_class (mono_debugger_symbol_table, klass, centry->info);
480 g_assert (centry->info && centry->info->type_info);
482 symfile->generation++;
483 must_reload_symtabs = TRUE;
485 mono_debugger_unlock ();
489 mono_debugger_add_method (MonoDebuggerSymbolFile *symfile, MonoDebugMethodInfo *minfo,
490 MonoDebugMethodJitInfo *jit)
492 MonoSymbolFileMethodAddress *address;
493 MonoSymbolFileLexicalBlockEntry *block;
494 MonoDebugVarInfo *var_table;
495 MonoDebuggerRangeInfo *range;
496 MonoMethodHeader *header;
497 guint32 size, num_variables, variable_size, variable_offset;
498 guint32 type_size, type_offset, *type_index_table, has_this;
499 guint32 line_size, line_offset, block_offset, block_size;
500 MonoDebugLexicalBlockEntry *block_table;
501 MonoDebugLineNumberEntry *line_table;
506 if (!symfile->symfile->method_hash)
509 header = ((MonoMethodNormal *) minfo->method)->header;
511 symfile->generation++;
513 size = sizeof (MonoSymbolFileMethodAddress);
515 num_variables = jit->num_params + read32(&(minfo->entry->_num_locals));
516 has_this = jit->this_var != NULL;
518 variable_size = (num_variables + has_this) * sizeof (MonoDebugVarInfo);
519 variable_offset = size;
520 size += variable_size;
522 type_size = (num_variables + 1) * sizeof (gpointer);
526 if (jit->line_numbers) {
528 line_size = jit->line_numbers->len * sizeof (MonoDebugLineNumberEntry);
532 block_size = read32(&(minfo->entry->_num_lexical_blocks)) * sizeof (MonoSymbolFileLexicalBlockEntry);
536 address = g_malloc0 (size);
537 ptr = (guint8 *) address;
539 block = (MonoSymbolFileLexicalBlockEntry *)
540 (symfile->symfile->raw_contents + read32(&(minfo->entry->_lexical_block_table_offset)));
541 block_table = (MonoDebugLexicalBlockEntry *) (ptr + block_offset);
543 for (i = 0; i < read32(&(minfo->entry->_num_lexical_blocks)); i++, block++) {
544 block_table [i].start_address = _mono_debug_address_from_il_offset (jit, read32(&(block->_start_offset)));
545 block_table [i].end_address = _mono_debug_address_from_il_offset (jit, read32(&(block->_end_offset)));
548 address->size = size;
549 address->has_this = has_this;
550 address->num_params = jit->num_params;
551 address->start_address = jit->code_start;
552 address->end_address = jit->code_start + jit->code_size;
553 address->method_start_address = address->start_address + jit->prologue_end;
554 address->method_end_address = address->start_address + jit->epilogue_begin;
555 address->wrapper_address = jit->wrapper_addr;
556 address->variable_table_offset = variable_offset;
557 address->type_table_offset = type_offset;
558 address->lexical_block_table_offset = block_offset;
560 if (jit->line_numbers) {
561 address->num_line_numbers = jit->line_numbers->len;
562 address->line_number_offset = line_offset;
564 line_table = (MonoDebugLineNumberEntry *) (ptr + line_offset);
565 memcpy (line_table, jit->line_numbers->data, line_size);
568 range = allocate_range_entry (symfile);
569 range->index = minfo->index;
570 range->start_address = address->start_address;
571 range->end_address = address->end_address;
572 range->dynamic_data = address;
573 range->dynamic_size = size;
575 if ((minfo->method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
576 (minfo->method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
577 (minfo->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
580 var_table = (MonoDebugVarInfo *) (ptr + variable_offset);
581 type_table = (guint32 *) (ptr + type_offset);
583 type_index_table = (guint32 *)
584 (symfile->symfile->raw_contents + read32(&(minfo->entry->_type_index_table_offset)));
587 *var_table++ = *jit->this_var;
588 *type_table++ = write_type (mono_debugger_symbol_table, &minfo->method->klass->this_arg);
590 for (i = 0; i < jit->num_params; i++) {
591 *var_table++ = jit->params [i];
592 *type_table++ = write_type (mono_debugger_symbol_table, minfo->method->signature->params [i]);
595 if (jit->num_locals < read32(&(minfo->entry->_num_locals))) {
596 g_warning (G_STRLOC ": Method %s.%s has %d locals, but symbol file claims it has %d.",
597 minfo->method->klass->name, minfo->method->name, jit->num_locals,
598 read32(&(minfo->entry->_num_locals)));
599 var_table += read32(&(minfo->entry->_num_locals));
601 g_assert ((header != NULL) || (minfo->entry->_num_locals == 0));
602 for (i = 0; i < read32(&(minfo->entry->_num_locals)); i++) {
603 *var_table++ = jit->locals [i];
604 *type_table++ = write_type (mono_debugger_symbol_table, header->locals [i]);
608 must_reload_symtabs = TRUE;
611 static MonoDebuggerRangeInfo *
612 allocate_range_entry (MonoDebuggerSymbolFile *symfile)
614 MonoDebuggerRangeInfo *retval;
615 guint32 size, chunks;
617 if (!symfile->range_table) {
618 size = sizeof (MonoDebuggerRangeInfo) * RANGE_TABLE_CHUNK_SIZE;
619 symfile->range_table = g_malloc0 (size);
620 symfile->num_range_entries = 1;
621 return symfile->range_table;
624 if (!((symfile->num_range_entries + 1) % RANGE_TABLE_CHUNK_SIZE)) {
625 chunks = (symfile->num_range_entries + 1) / RANGE_TABLE_CHUNK_SIZE;
626 size = sizeof (MonoDebuggerRangeInfo) * RANGE_TABLE_CHUNK_SIZE * (chunks + 1);
628 symfile->range_table = g_realloc (symfile->range_table, size);
631 retval = symfile->range_table + symfile->num_range_entries;
632 symfile->num_range_entries++;
636 static MonoDebuggerClassInfo *
637 allocate_class_entry (MonoDebuggerSymbolFile *symfile)
639 MonoDebuggerClassInfo *retval;
640 MonoDebuggerClassTable *table;
643 if (!symfile->class_table_start) {
644 table = g_new0 (MonoDebuggerClassTable, 1);
645 symfile->class_table_start = symfile->current_class_table = table;
647 size = sizeof (MonoDebuggerClassInfo) * CLASS_TABLE_CHUNK_SIZE;
648 table->data = g_malloc0 (size);
649 table->size = CLASS_TABLE_CHUNK_SIZE;
655 table = symfile->current_class_table;
656 if (table->index >= table->size) {
657 table = g_new0 (MonoDebuggerClassTable, 1);
659 symfile->current_class_table->next = table;
660 symfile->current_class_table = table;
662 size = sizeof (MonoDebuggerClassInfo) * CLASS_TABLE_CHUNK_SIZE;
663 table->data = g_malloc0 (size);
664 table->size = CLASS_TABLE_CHUNK_SIZE;
670 retval = table->data + table->index;
676 * Allocate a new entry of size `size' in the type table.
677 * Returns the global offset which is to be used to reference this type and
678 * a pointer (in the `ptr' argument) which is to be used to write the type.
681 allocate_type_entry (MonoDebuggerSymbolTable *table, guint32 size, guint8 **ptr)
686 g_assert (size + 4 < TYPE_TABLE_CHUNK_SIZE);
687 g_assert (ptr != NULL);
689 /* Initialize things if necessary. */
690 if (!table->current_type_table) {
691 table->current_type_table = g_malloc0 (TYPE_TABLE_CHUNK_SIZE);
692 table->type_table_size = TYPE_TABLE_CHUNK_SIZE;
693 table->type_table_chunk_size = TYPE_TABLE_CHUNK_SIZE;
694 table->type_table_offset = MONO_DEBUGGER_TYPE_MAX + 1;
698 /* First let's check whether there's still enough room in the current_type_table. */
699 if (table->type_table_offset + size + 4 < table->type_table_size) {
700 retval = table->type_table_offset;
701 table->type_table_offset += size + 4;
702 data = ((guint8 *) table->current_type_table) + retval - table->type_table_start;
703 *(gint32 *) data = size;
704 data += sizeof(gint32);
709 /* Add the current_type_table to the type_tables vector and ... */
710 if (!table->type_tables) {
711 guint32 tsize = sizeof (gpointer) * TYPE_TABLE_PTR_CHUNK_SIZE;
712 table->type_tables = g_malloc0 (tsize);
715 if (!((table->num_type_tables + 1) % TYPE_TABLE_PTR_CHUNK_SIZE)) {
716 guint32 chunks = (table->num_type_tables + 1) / TYPE_TABLE_PTR_CHUNK_SIZE;
717 guint32 tsize = sizeof (gpointer) * TYPE_TABLE_PTR_CHUNK_SIZE * (chunks + 1);
719 table->type_tables = g_realloc (table->type_tables, tsize);
722 table->type_tables [table->num_type_tables++] = table->current_type_table;
724 /* .... allocate a new current_type_table. */
725 table->current_type_table = g_malloc0 (TYPE_TABLE_CHUNK_SIZE);
726 table->type_table_start = table->type_table_offset = table->type_table_size;
727 table->type_table_size += TYPE_TABLE_CHUNK_SIZE;
733 * Allocate a new entry of size `size' in the misc table.
734 * Returns the global offset which is to be used to reference this entry and
735 * a pointer (in the `ptr' argument) which is to be used to write the entry.
738 allocate_misc_entry (MonoDebuggerSymbolTable *table, guint32 size, guint8 **ptr)
743 g_assert (size + 4 < MISC_TABLE_CHUNK_SIZE);
744 g_assert (ptr != NULL);
746 /* Initialize things if necessary. */
747 if (!table->current_misc_table) {
748 table->current_misc_table = g_malloc0 (MISC_TABLE_CHUNK_SIZE);
749 table->misc_table_size = MISC_TABLE_CHUNK_SIZE;
750 table->misc_table_chunk_size = MISC_TABLE_CHUNK_SIZE;
751 table->misc_table_offset = 1;
755 /* First let's check whether there's still enough room in the current_misc_table. */
756 if (table->misc_table_offset + size + 4 < table->misc_table_size) {
757 retval = table->misc_table_offset;
758 table->misc_table_offset += size + 4;
759 data = ((guint8 *) table->current_misc_table) + retval - table->misc_table_start;
760 *(gint32 *) data = size;
761 data += sizeof(gint32);
766 /* Add the current_misc_table to the misc_tables vector and ... */
767 if (!table->misc_tables) {
768 guint32 tsize = sizeof (gpointer) * MISC_TABLE_PTR_CHUNK_SIZE;
769 table->misc_tables = g_malloc0 (tsize);
772 if (!((table->num_misc_tables + 1) % MISC_TABLE_PTR_CHUNK_SIZE)) {
773 guint32 chunks = (table->num_misc_tables + 1) / MISC_TABLE_PTR_CHUNK_SIZE;
774 guint32 tsize = sizeof (gpointer) * MISC_TABLE_PTR_CHUNK_SIZE * (chunks + 1);
776 table->misc_tables = g_realloc (table->misc_tables, tsize);
779 table->misc_tables [table->num_misc_tables++] = table->current_misc_table;
781 /* .... allocate a new current_misc_table. */
782 table->current_misc_table = g_malloc0 (MISC_TABLE_CHUNK_SIZE);
783 table->misc_table_start = table->misc_table_offset = table->misc_table_size;
784 table->misc_table_size += MISC_TABLE_CHUNK_SIZE;
790 property_is_static (MonoProperty *prop)
798 return method->flags & METHOD_ATTRIBUTE_STATIC;
802 event_is_static (MonoEvent *ev)
808 return method->flags & METHOD_ATTRIBUTE_STATIC;
812 do_write_class (MonoDebuggerSymbolTable *table, MonoClass *klass, MonoDebuggerClassInfo *cinfo)
814 guint8 buffer [BUFSIZ], *ptr = buffer, *old_ptr;
815 GPtrArray *methods = NULL, *static_methods = NULL, *ctors = NULL, *cctors = NULL;
816 int num_fields = 0, num_static_fields = 0, num_properties = 0, num_static_properties = 0;
817 int num_events = 0, num_static_events = 0;
818 int num_methods = 0, num_static_methods = 0, num_params = 0, num_static_params = 0, base_offset = 0;
819 int num_ctors = 0, num_ctor_params = 0;
821 int field_info_size = 0, static_field_info_size = 0, property_info_size = 0, event_info_size = 0, static_event_info_size = 0;
822 int static_property_info_size = 0, method_info_size = 0, static_method_info_size = 0;
823 int ctor_info_size = 0, cctor_info_size = 0, iface_info_size = 0;
824 guint32 size, data_size, offset, data_offset;
825 GHashTable *method_slots = NULL;
828 if (klass->init_pending)
829 g_warning (G_STRLOC ": %p - %s.%s", klass, klass->name_space, klass->name);
830 g_assert (!klass->init_pending);
831 mono_class_init (klass);
833 offset = GPOINTER_TO_UINT (g_hash_table_lookup (class_table, klass));
837 if (klass->enumtype) {
838 offset = allocate_type_entry (table, 13, &ptr);
840 cinfo->type_info = offset;
841 g_hash_table_insert (class_table, klass, GUINT_TO_POINTER (offset));
843 *ptr++ = MONO_DEBUGGER_TYPE_KIND_ENUM;
844 WRITE_UINT32 (ptr, klass->instance_size);
845 WRITE_UINT32 (ptr, MONO_DEBUGGER_TYPE_ENUM);
846 WRITE_UINT32 (ptr, write_type (table, klass->enum_basetype));
850 for (i = 0; i < klass->field.count; i++)
851 if (!(klass->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
856 for (i = 0; i < klass->property.count; i++)
857 if (!property_is_static (&klass->properties [i]))
860 ++num_static_properties;
862 for (i = 0; i < klass->event.count; i++)
863 if (!event_is_static (&klass->events [i]))
868 method_slots = g_hash_table_new (NULL, NULL);
869 methods = g_ptr_array_new ();
870 static_methods = g_ptr_array_new ();
871 ctors = g_ptr_array_new ();
872 cctors = g_ptr_array_new ();
874 for (i = 0; i < klass->method.count; i++) {
875 MonoMethod *method = klass->methods [i];
877 if (!strcmp (method->name, ".cctor")) {
879 g_ptr_array_add (cctors, method);
883 if (!strcmp (method->name, ".ctor")) {
885 num_ctor_params += method->signature->param_count;
886 g_ptr_array_add (ctors, method);
890 if (method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME)
893 if (method->slot != -1) {
894 if (g_hash_table_lookup (method_slots, GUINT_TO_POINTER (method->slot)))
896 g_hash_table_insert (method_slots, GUINT_TO_POINTER (method->slot), method);
899 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
900 ++num_static_methods;
901 num_static_params += method->signature->param_count;
902 g_ptr_array_add (static_methods, method);
905 num_params += method->signature->param_count;
906 g_ptr_array_add (methods, method);
910 g_hash_table_destroy (method_slots);
912 field_info_size = num_fields * 8;
913 static_field_info_size = num_static_fields * 8;
914 property_info_size = num_properties * (4 + 2 * sizeof (gpointer));
915 static_property_info_size = num_static_properties * (4 + 2 * sizeof (gpointer));
916 event_info_size = num_events * (4 + 2 * sizeof (gpointer));
917 static_event_info_size = num_static_events * (4 + 2 * sizeof (gpointer));
918 method_info_size = num_methods * (2 * 4 + sizeof (gpointer)) + num_params * 4;
919 static_method_info_size = num_static_methods * (2 * 4 + sizeof (gpointer)) +
920 num_static_params * 4;
921 ctor_info_size = num_ctors * (2 * 4 + sizeof (gpointer)) + num_ctor_params * 4;
922 cctor_info_size = num_cctors * (2 * 4 + sizeof (gpointer));
923 iface_info_size = klass->interface_count * 4;
925 size = 98 + sizeof (gpointer) + field_info_size + static_field_info_size +
926 property_info_size + static_property_info_size + event_info_size +
927 static_event_info_size + method_info_size + static_method_info_size +
928 ctor_info_size + cctor_info_size + iface_info_size;
932 offset = allocate_type_entry (table, data_size, &ptr);
936 cinfo->type_info = offset;
938 g_hash_table_insert (class_table, klass, GUINT_TO_POINTER (offset));
940 *ptr++ = MONO_DEBUGGER_TYPE_KIND_CLASS_INFO;
942 if (klass->valuetype)
943 base_offset = - sizeof (MonoObject);
945 WRITE_UINT32 (ptr, klass->instance_size + base_offset);
946 *ptr++ = klass->valuetype;
947 WRITE_POINTER (ptr, klass);
949 WRITE_UINT32 (ptr, num_fields);
950 WRITE_UINT32 (ptr, data_offset);
951 data_offset += field_info_size;
952 WRITE_UINT32 (ptr, num_properties);
953 WRITE_UINT32 (ptr, data_offset);
954 data_offset += property_info_size;
955 WRITE_UINT32 (ptr, num_events);
956 WRITE_UINT32 (ptr, data_offset);
957 data_offset += event_info_size;
958 WRITE_UINT32 (ptr, num_methods);
959 WRITE_UINT32 (ptr, data_offset);
960 data_offset += method_info_size;
961 WRITE_UINT32 (ptr, num_static_fields);
962 WRITE_UINT32 (ptr, data_offset);
963 data_offset += static_field_info_size;
964 WRITE_UINT32 (ptr, num_static_properties);
965 WRITE_UINT32 (ptr, data_offset);
966 data_offset += static_property_info_size;
967 WRITE_UINT32 (ptr, num_static_events);
968 WRITE_UINT32 (ptr, data_offset);
969 data_offset += static_event_info_size;
970 WRITE_UINT32 (ptr, num_static_methods);
971 WRITE_UINT32 (ptr, data_offset);
972 data_offset += static_method_info_size;
973 WRITE_UINT32 (ptr, num_ctors);
974 WRITE_UINT32 (ptr, data_offset);
975 data_offset += ctor_info_size;
976 WRITE_UINT32 (ptr, num_cctors);
977 WRITE_UINT32 (ptr, data_offset);
978 data_offset += cctor_info_size;
979 WRITE_UINT32 (ptr, klass->interface_count);
980 WRITE_UINT32 (ptr, data_offset);
981 data_offset += iface_info_size;
983 if (klass->parent && (klass->parent != mono_defaults.object_class))
984 WRITE_UINT32 (ptr, write_class (table, klass->parent));
986 WRITE_UINT32 (ptr, 0);
988 for (i = 0; i < klass->field.count; i++) {
989 if (klass->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
992 WRITE_UINT32 (ptr, klass->fields [i].offset + base_offset);
993 WRITE_UINT32 (ptr, write_type (table, klass->fields [i].type));
996 for (i = 0; i < klass->property.count; i++) {
997 if (property_is_static (&klass->properties [i]))
1000 if (klass->properties [i].get)
1001 WRITE_UINT32 (ptr, write_type (table, klass->properties [i].get->signature->ret));
1003 WRITE_UINT32 (ptr, 0);
1004 WRITE_POINTER (ptr, klass->properties [i].get);
1005 WRITE_POINTER (ptr, klass->properties [i].set);
1008 for (i = 0; i < klass->event.count; i++) {
1009 if (event_is_static (&klass->events[i]))
1012 if (klass->events [i].add) {
1013 WRITE_UINT32 (ptr, write_type (table, klass->events [i].add->signature->params[0]));
1016 g_warning ("event add method not defined");
1017 WRITE_UINT32 (ptr, 0);
1019 WRITE_POINTER (ptr, klass->events [i].add);
1020 WRITE_POINTER (ptr, klass->events [i].remove);
1024 for (i = 0; i < methods->len; i++) {
1025 MonoMethod *method = g_ptr_array_index (methods, i);
1028 WRITE_POINTER (ptr, method);
1029 if ((method->signature->ret) && (method->signature->ret->type != MONO_TYPE_VOID))
1030 WRITE_UINT32 (ptr, write_type (table, method->signature->ret));
1032 WRITE_UINT32 (ptr, 0);
1033 WRITE_UINT32 (ptr, method->signature->param_count);
1034 for (j = 0; j < method->signature->param_count; j++)
1035 WRITE_UINT32 (ptr, write_type (table, method->signature->params [j]));
1038 g_ptr_array_free (methods, FALSE);
1040 for (i = 0; i < klass->field.count; i++) {
1041 if (!(klass->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
1044 WRITE_UINT32 (ptr, klass->fields [i].offset);
1045 WRITE_UINT32 (ptr, write_type (table, klass->fields [i].type));
1048 for (i = 0; i < klass->property.count; i++) {
1049 if (!property_is_static (&klass->properties [i]))
1052 if (klass->properties [i].get)
1053 WRITE_UINT32 (ptr, write_type (table, klass->properties [i].get->signature->ret));
1055 WRITE_UINT32 (ptr, 0);
1056 WRITE_POINTER (ptr, klass->properties [i].get);
1057 WRITE_POINTER (ptr, klass->properties [i].set);
1060 for (i = 0; i < klass->event.count; i++) {
1061 if (!event_is_static (&klass->events[i]))
1064 if (klass->events [i].add) {
1065 WRITE_UINT32 (ptr, write_type (table, klass->events [i].add->signature->params[0]));
1068 g_warning ("event add method not defined");
1069 WRITE_UINT32 (ptr, 0);
1071 WRITE_POINTER (ptr, klass->events [i].add);
1072 WRITE_POINTER (ptr, klass->events [i].remove);
1076 for (i = 0; i < static_methods->len; i++) {
1077 MonoMethod *method = g_ptr_array_index (static_methods, i);
1080 WRITE_POINTER (ptr, method);
1081 if ((method->signature->ret) && (method->signature->ret->type != MONO_TYPE_VOID))
1082 WRITE_UINT32 (ptr, write_type (table, method->signature->ret));
1084 WRITE_UINT32 (ptr, 0);
1085 WRITE_UINT32 (ptr, method->signature->param_count);
1086 for (j = 0; j < method->signature->param_count; j++)
1087 WRITE_UINT32 (ptr, write_type (table, method->signature->params [j]));
1090 g_ptr_array_free (static_methods, FALSE);
1092 for (i = 0; i < ctors->len; i++) {
1093 MonoMethod *ctor = g_ptr_array_index (ctors, i);
1096 WRITE_POINTER (ptr, ctor);
1097 WRITE_UINT32 (ptr, 0);
1098 WRITE_UINT32 (ptr, ctor->signature->param_count);
1099 for (j = 0; j < ctor->signature->param_count; j++)
1100 WRITE_UINT32 (ptr, write_type (table, ctor->signature->params [j]));
1103 g_ptr_array_free (ctors, FALSE);
1105 for (i = 0; i < cctors->len; i++) {
1106 MonoMethod *cctor = g_ptr_array_index (cctors, i);
1108 WRITE_POINTER (ptr, cctor);
1109 WRITE_UINT32 (ptr, 0);
1110 WRITE_UINT32 (ptr, 0);
1113 g_ptr_array_free (cctors, FALSE);
1115 for (i = 0; i < klass->interface_count; i++)
1116 WRITE_UINT32 (ptr, write_class (table, klass->interfaces [i]));
1118 if (ptr - old_ptr != data_size) {
1119 g_warning (G_STRLOC ": %d,%d,%d", ptr - old_ptr, data_size, sizeof (gpointer));
1121 g_warning (G_STRLOC ": %s.%s", klass->name_space, klass->name);
1122 g_assert_not_reached ();
1129 * Adds type `type' to the type table and returns its offset.
1132 write_type (MonoDebuggerSymbolTable *table, MonoType *type)
1134 guint8 buffer [BUFSIZ], *ptr = buffer;
1135 guint32 size, offset;
1138 offset = GPOINTER_TO_UINT (g_hash_table_lookup (type_table, type));
1142 klass = mono_class_from_mono_type (type);
1143 if (type->type == MONO_TYPE_CLASS)
1144 return write_class (table, klass);
1146 // mono_class_init (klass);
1148 switch (type->type) {
1149 case MONO_TYPE_VOID:
1150 return MONO_DEBUGGER_TYPE_VOID;
1152 case MONO_TYPE_BOOLEAN:
1153 return MONO_DEBUGGER_TYPE_BOOLEAN;
1156 return MONO_DEBUGGER_TYPE_I1;
1159 return MONO_DEBUGGER_TYPE_U1;
1161 case MONO_TYPE_CHAR:
1162 return MONO_DEBUGGER_TYPE_CHAR;
1165 return MONO_DEBUGGER_TYPE_I2;
1168 return MONO_DEBUGGER_TYPE_U2;
1171 return MONO_DEBUGGER_TYPE_I4;
1174 return MONO_DEBUGGER_TYPE_U4;
1177 return MONO_DEBUGGER_TYPE_I8;
1180 return MONO_DEBUGGER_TYPE_U8;
1183 return MONO_DEBUGGER_TYPE_R4;
1186 return MONO_DEBUGGER_TYPE_R8;
1188 case MONO_TYPE_STRING:
1189 return MONO_DEBUGGER_TYPE_STRING;
1192 return MONO_DEBUGGER_TYPE_I;
1195 return MONO_DEBUGGER_TYPE_U;
1197 case MONO_TYPE_SZARRAY: {
1200 *ptr++ = MONO_DEBUGGER_TYPE_KIND_SZARRAY;
1201 WRITE_UINT32 (ptr, sizeof (MonoArray));
1202 WRITE_UINT32 (ptr, MONO_DEBUGGER_TYPE_ARRAY);
1203 *ptr++ = (guint8*)&array.max_length - (guint8*)&array;
1204 *ptr++ = sizeof (array.max_length);
1205 *ptr++ = (guint8*)&array.vector - (guint8*)&array;
1206 WRITE_UINT32 (ptr, write_type (table, &type->data.klass->byval_arg));
1210 case MONO_TYPE_ARRAY: {
1212 MonoArrayBounds bounds;
1214 *ptr++ = MONO_DEBUGGER_TYPE_KIND_ARRAY;
1215 WRITE_UINT32 (ptr, sizeof (MonoArray));
1216 WRITE_UINT32 (ptr, MONO_DEBUGGER_TYPE_ARRAY);
1217 *ptr++ = (guint8*)&array.max_length - (guint8*)&array;
1218 *ptr++ = sizeof (array.max_length);
1219 *ptr++ = (guint8*)&array.vector - (guint8*)&array;
1220 *ptr++ = klass->rank;
1221 *ptr++ = (guint8*)&array.bounds - (guint8*)&array;
1222 *ptr++ = sizeof (MonoArrayBounds);
1223 *ptr++ = (guint8*)&bounds.lower_bound - (guint8*)&bounds;
1224 *ptr++ = sizeof (bounds.lower_bound);
1225 *ptr++ = (guint8*)&bounds.length - (guint8*)&bounds;
1226 *ptr++ = sizeof (bounds.length);
1227 WRITE_UINT32 (ptr, write_type (table, &type->data.array->eklass->byval_arg));
1231 case MONO_TYPE_VALUETYPE:
1232 case MONO_TYPE_CLASS:
1233 case MONO_TYPE_GENERICINST:
1234 case MONO_TYPE_OBJECT:
1235 return write_class (table, klass);
1238 *ptr++ = MONO_DEBUGGER_TYPE_KIND_POINTER;
1239 WRITE_UINT32 (ptr, sizeof (gpointer));
1240 WRITE_UINT32 (ptr, write_type (table, type->data.type));
1244 /* g_message (G_STRLOC ": %s.%s - %p - %d", klass->name_space, klass->name, klass, type->type); */
1245 *ptr++ = MONO_DEBUGGER_TYPE_KIND_UNKNOWN;
1246 WRITE_UINT32 (ptr, klass->instance_size);
1247 WRITE_UINT32 (ptr, write_class (table, klass));
1251 size = ptr - buffer;
1252 offset = allocate_type_entry (mono_debugger_symbol_table, size, &ptr);
1253 memcpy (ptr, buffer, size);
1258 MonoReflectionMethod *
1259 ves_icall_MonoDebugger_GetMethod (MonoReflectionAssembly *assembly, guint32 token)
1263 method = mono_get_method (mono_assembly_get_image (assembly->assembly), token, NULL);
1265 return mono_method_get_object (mono_domain_get (), method, NULL);
1269 ves_icall_MonoDebugger_GetMethodToken (MonoReflectionAssembly *assembly, MonoReflectionMethod *method)
1271 return method->method->token;
1274 MonoReflectionType *
1275 ves_icall_MonoDebugger_GetType (MonoReflectionAssembly *assembly, guint32 token)
1279 klass = mono_class_get (mono_assembly_get_image (assembly->assembly), token);
1281 g_warning (G_STRLOC ": %x", token);
1285 return mono_type_get_object (mono_domain_get (), &klass->byval_arg);
1288 MonoReflectionType *
1289 ves_icall_MonoDebugger_GetLocalTypeFromSignature (MonoReflectionAssembly *assembly, MonoArray *signature)
1297 MONO_CHECK_ARG_NULL (assembly);
1298 MONO_CHECK_ARG_NULL (signature);
1300 domain = mono_domain_get();
1301 image = mono_assembly_get_image (assembly->assembly);
1303 ptr = mono_array_addr (signature, char, 0);
1304 g_assert (*ptr++ == 0x07);
1305 len = mono_metadata_decode_value (ptr, &ptr);
1306 g_assert (len == 1);
1308 type = mono_metadata_parse_type (image, MONO_PARSE_LOCAL, 0, ptr, &ptr);
1310 return mono_type_get_object (domain, type);
1314 mono_debugger_event (MonoDebuggerEvent event, gpointer data, guint32 arg)
1316 if (mono_debugger_event_handler)
1317 (* mono_debugger_event_handler) (event, data, arg);
1321 mono_debugger_cleanup (void)
1323 /* Do nothing yet. */
1327 * Debugger breakpoint interface.
1329 * This interface is used to insert breakpoints on methods which are not yet JITed.
1330 * The debugging code keeps a list of all such breakpoints and automatically inserts the
1331 * breakpoint when the method is JITed.
1334 static GPtrArray *breakpoints = NULL;
1337 mono_debugger_insert_breakpoint_full (MonoMethodDesc *desc)
1339 static int last_breakpoint_id = 0;
1340 MonoDebuggerBreakpointInfo *info;
1342 info = g_new0 (MonoDebuggerBreakpointInfo, 1);
1344 info->index = ++last_breakpoint_id;
1347 breakpoints = g_ptr_array_new ();
1349 g_ptr_array_add (breakpoints, info);
1355 mono_debugger_remove_breakpoint (int breakpoint_id)
1362 for (i = 0; i < breakpoints->len; i++) {
1363 MonoDebuggerBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
1365 if (info->index != breakpoint_id)
1368 mono_method_desc_free (info->desc);
1369 g_ptr_array_remove (breakpoints, info);
1378 mono_debugger_insert_breakpoint (const gchar *method_name, gboolean include_namespace)
1380 MonoMethodDesc *desc;
1382 desc = mono_method_desc_new (method_name, include_namespace);
1386 return mono_debugger_insert_breakpoint_full (desc);
1390 mono_debugger_method_has_breakpoint (MonoMethod *method)
1394 if (!breakpoints || (method->wrapper_type != MONO_WRAPPER_NONE))
1397 for (i = 0; i < breakpoints->len; i++) {
1398 MonoDebuggerBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
1400 if (!mono_method_desc_full_match (info->desc, method))
1410 mono_debugger_breakpoint_callback (MonoMethod *method, guint32 index)
1412 mono_debugger_event (MONO_DEBUGGER_EVENT_BREAKPOINT, method, index);
1416 mono_debugger_unhandled_exception (gpointer addr, gpointer stack, MonoObject *exc)
1418 if (!mono_debugger_initialized)
1421 // Prevent the object from being finalized.
1422 last_exception = exc;
1423 mono_debugger_event (MONO_DEBUGGER_EVENT_UNHANDLED_EXCEPTION, exc, addr);
1428 mono_debugger_handle_exception (gpointer addr, gpointer stack, MonoObject *exc)
1430 MonoDebuggerExceptionInfo info;
1432 if (!mono_debugger_initialized)
1435 // Prevent the object from being finalized.
1436 last_exception = exc;
1438 info.stack_pointer = stack;
1439 info.exception_obj = exc;
1442 mono_debugger_event (MONO_DEBUGGER_EVENT_EXCEPTION, &info, addr);
1446 mono_debugger_throw_exception (gpointer addr, gpointer stack, MonoObject *exc)
1448 MonoDebuggerExceptionInfo info;
1450 if (!mono_debugger_initialized)
1453 // Prevent the object from being finalized.
1454 last_exception = exc;
1456 info.stack_pointer = stack;
1457 info.exception_obj = exc;
1460 mono_debugger_event (MONO_DEBUGGER_EVENT_THROW_EXCEPTION, &info, addr);
1461 return info.stop != 0;
1465 get_exception_message (MonoObject *exc)
1467 char *message = NULL;
1473 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
1474 klass = exc->vtable->klass;
1476 while (klass && method == NULL) {
1477 for (i = 0; i < klass->method.count; ++i) {
1478 method = klass->methods [i];
1479 if (!strcmp ("ToString", method->name) &&
1480 method->signature->param_count == 0 &&
1481 method->flags & METHOD_ATTRIBUTE_VIRTUAL &&
1482 method->flags & METHOD_ATTRIBUTE_PUBLIC) {
1489 klass = klass->parent;
1494 str = (MonoString *) mono_runtime_invoke (method, exc, NULL, NULL);
1496 message = mono_string_to_utf8 (str);
1503 mono_debugger_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
1508 if (!strcmp (method->name, ".ctor")) {
1509 retval = obj = mono_object_new (mono_domain_get (), method->klass);
1511 mono_runtime_invoke (method, obj, params, exc);
1513 retval = mono_runtime_invoke (method, obj, params, exc);
1515 if (!exc || (*exc == NULL))
1518 message = get_exception_message (*exc);
1520 *exc = (MonoObject *) mono_string_new_wrapper (message);
1528 mono_debugger_lookup_type (const gchar *type_name)
1532 mono_debugger_lock ();
1534 for (i = 0; i < mono_debugger_symbol_table->num_symbol_files; i++) {
1535 MonoDebuggerSymbolFile *symfile = mono_debugger_symbol_table->symbol_files [i];
1540 name = g_strdup (type_name);
1541 type = mono_reflection_type_from_name (name, symfile->image);
1546 offset = write_type (mono_debugger_symbol_table, type);
1548 mono_debugger_unlock ();
1552 mono_debugger_unlock ();
1557 mono_debugger_lookup_assembly (const gchar *name)
1559 MonoAssembly *assembly;
1560 MonoImageOpenStatus status;
1563 mono_debugger_lock ();
1566 for (i = 0; i < mono_debugger_symbol_table->num_symbol_files; i++) {
1567 MonoDebuggerSymbolFile *symfile = mono_debugger_symbol_table->symbol_files [i];
1569 if (!strcmp (symfile->image_file, name)) {
1570 mono_debugger_unlock ();
1575 assembly = mono_assembly_open (name, &status);
1577 if (status != MONO_IMAGE_OK) {
1578 g_warning (G_STRLOC ": Cannot open image `%s'", name);
1579 mono_debugger_unlock ();
1583 must_reload_symtabs = TRUE;
1588 mono_debugger_add_wrapper (MonoMethod *wrapper, MonoDebugMethodJitInfo *jit, gpointer addr)
1590 guint32 size, offset;
1593 if (!mono_debugger_symbol_table)
1596 size = strlen (wrapper->name) + 5 + 5 * sizeof (gpointer);
1598 offset = allocate_misc_entry (mono_debugger_symbol_table, size, &ptr);
1600 WRITE_UINT32 (ptr, MONO_DEBUGGER_MISC_ENTRY_TYPE_WRAPPER);
1601 WRITE_STRING (ptr, wrapper->name);
1602 WRITE_POINTER (ptr, jit->code_start);
1603 WRITE_POINTER (ptr, jit->code_start + jit->code_size);
1604 WRITE_POINTER (ptr, addr);
1605 WRITE_POINTER (ptr, jit->code_start + jit->prologue_end);
1606 WRITE_POINTER (ptr, jit->code_start + jit->epilogue_begin);