4 #include <mono/metadata/metadata.h>
5 #include <mono/metadata/tabledefs.h>
6 #include <mono/metadata/tokentype.h>
7 #include <mono/metadata/appdomain.h>
8 #include <mono/metadata/object-internals.h>
9 #include <mono/metadata/class-internals.h>
10 #include <mono/metadata/exception.h>
11 #include <mono/metadata/mono-debug.h>
12 #include <mono/metadata/mono-debug-debugger.h>
13 #include <mono/metadata/mono-endian.h>
15 #define SYMFILE_TABLE_CHUNK_SIZE 16
16 #define RANGE_TABLE_CHUNK_SIZE 256
17 #define CLASS_TABLE_CHUNK_SIZE 256
18 #define TYPE_TABLE_PTR_CHUNK_SIZE 256
19 #define TYPE_TABLE_CHUNK_SIZE 65536
21 static guint32 debugger_lock_level = 0;
22 static CRITICAL_SECTION debugger_lock_mutex;
23 static gboolean mono_debugger_initialized = FALSE;
25 static gboolean must_reload_symtabs = FALSE;
27 static GHashTable *images = NULL;
28 static GHashTable *type_table = NULL;
29 static GHashTable *class_table = NULL;
30 static GHashTable *class_info_table = NULL;
32 static MonoDebuggerRangeInfo *allocate_range_entry (MonoDebuggerSymbolFile *symfile);
33 static MonoDebuggerClassInfo *allocate_class_entry (MonoDebuggerSymbolFile *symfile);
34 static guint32 allocate_type_entry (MonoDebuggerSymbolTable *table, guint32 size, guint8 **ptr);
35 static guint32 write_type (MonoDebuggerSymbolTable *table, MonoType *type);
36 static guint32 write_class (MonoDebuggerSymbolTable *table, MonoClass *klass);
38 MonoDebuggerSymbolTable *mono_debugger_symbol_table = NULL;
39 void (*mono_debugger_event_handler) (MonoDebuggerEvent event, gpointer data, guint32 arg) = NULL;
41 #define WRITE_UINT32(ptr,value) G_STMT_START { \
42 * ((guint32 *) ptr) = value; \
46 #define WRITE_POINTER(ptr,value) G_STMT_START { \
47 * ((gpointer *) ptr) = value; \
48 ptr += sizeof (gpointer); \
51 #ifndef PLATFORM_WIN32
53 MonoDebuggerIOLayer mono_debugger_io_layer = {
54 InitializeCriticalSection, DeleteCriticalSection, TryEnterCriticalSection,
55 EnterCriticalSection, LeaveCriticalSection, WaitForSingleObjectEx, SignalObjectAndWait,
56 WaitForMultipleObjectsEx, CreateSemaphore, ReleaseSemaphore, CreateThread,
63 mono_debugger_lock (void)
65 if (!mono_debugger_initialized) {
66 debugger_lock_level++;
70 EnterCriticalSection (&debugger_lock_mutex);
71 debugger_lock_level++;
75 mono_debugger_unlock (void)
77 g_assert (debugger_lock_level > 0);
79 if (!mono_debugger_initialized) {
80 debugger_lock_level--;
84 if (debugger_lock_level == 1) {
85 if (must_reload_symtabs) {
86 mono_debugger_event (MONO_DEBUGGER_EVENT_RELOAD_SYMTABS, NULL, 0);
87 must_reload_symtabs = FALSE;
91 debugger_lock_level--;
92 LeaveCriticalSection (&debugger_lock_mutex);
95 static MonoDebuggerSymbolFile *
96 allocate_symbol_file_entry (MonoDebuggerSymbolTable *table)
98 MonoDebuggerSymbolFile *symfile;
100 if (!table->symbol_files)
101 table->symbol_files = g_new0 (MonoDebuggerSymbolFile *, SYMFILE_TABLE_CHUNK_SIZE);
102 else if (!((table->num_symbol_files + 1) % SYMFILE_TABLE_CHUNK_SIZE)) {
103 guint32 chunks = (table->num_symbol_files + 1) / SYMFILE_TABLE_CHUNK_SIZE;
104 guint32 size = sizeof (MonoDebuggerSymbolFile *) * SYMFILE_TABLE_CHUNK_SIZE * (chunks + 1);
106 table->symbol_files = g_realloc (table->symbol_files, size);
109 symfile = g_new0 (MonoDebuggerSymbolFile, 1);
110 symfile->index = table->num_symbol_files;
111 symfile->range_entry_size = sizeof (MonoDebuggerRangeInfo);
112 symfile->class_entry_size = sizeof (MonoDebuggerClassInfo);
113 table->symbol_files [table->num_symbol_files++] = symfile;
118 mono_debugger_initialize (MonoDomain *domain)
120 MonoDebuggerSymbolTable *symbol_table;
122 g_assert (!mono_debugger_initialized);
124 InitializeCriticalSection (&debugger_lock_mutex);
125 mono_debugger_initialized = TRUE;
127 mono_debugger_lock ();
129 symbol_table = g_new0 (MonoDebuggerSymbolTable, 1);
130 symbol_table->magic = MONO_DEBUGGER_MAGIC;
131 symbol_table->version = MONO_DEBUGGER_VERSION;
132 symbol_table->total_size = sizeof (MonoDebuggerSymbolTable);
134 symbol_table->domain = domain;
136 images = g_hash_table_new (g_direct_hash, g_direct_equal);
137 type_table = g_hash_table_new (g_direct_hash, g_direct_equal);
138 class_table = g_hash_table_new (g_direct_hash, g_direct_equal);
139 class_info_table = g_hash_table_new (g_direct_hash, g_direct_equal);
141 mono_debugger_symbol_table = symbol_table;
143 mono_debugger_unlock ();
146 MonoDebuggerSymbolFile *
147 mono_debugger_add_symbol_file (MonoDebugHandle *handle)
149 MonoDebuggerSymbolFile *info;
151 g_assert (mono_debugger_initialized);
152 mono_debugger_lock ();
154 info = g_hash_table_lookup (images, handle->image);
156 mono_debugger_unlock ();
160 info = allocate_symbol_file_entry (mono_debugger_symbol_table);
161 info->symfile = handle->symfile;
162 info->image = handle->image;
163 info->image_file = handle->image_file;
165 g_hash_table_insert (images, handle->image, info);
166 mono_debugger_unlock ();
172 write_builtin_type (MonoDebuggerSymbolTable *table, MonoClass *klass, MonoDebuggerBuiltinTypeInfo *info)
174 guint8 buffer [BUFSIZ], *ptr = buffer;
177 g_assert (!klass->init_pending);
178 mono_class_init (klass);
180 switch (klass->byval_arg.type) {
182 *ptr++ = MONO_DEBUGGER_TYPE_KIND_UNKNOWN;
183 WRITE_UINT32 (ptr, 0);
187 case MONO_TYPE_BOOLEAN:
199 *ptr++ = MONO_DEBUGGER_TYPE_KIND_FUNDAMENTAL;
200 WRITE_UINT32 (ptr, klass->instance_size - sizeof (MonoObject));
204 case MONO_TYPE_STRING: {
207 *ptr++ = MONO_DEBUGGER_TYPE_KIND_STRING;
208 WRITE_UINT32 (ptr, klass->instance_size);
210 *ptr++ = (guint8*)&string.length - (guint8*)&string;
211 *ptr++ = sizeof (string.length);
212 *ptr++ = (guint8*)&string.chars - (guint8*)&string;
219 *ptr++ = MONO_DEBUGGER_TYPE_KIND_FUNDAMENTAL;
220 WRITE_UINT32 (ptr, sizeof (void *));
224 case MONO_TYPE_VALUETYPE:
225 *ptr++ = MONO_DEBUGGER_TYPE_KIND_STRUCT;
226 WRITE_UINT32 (ptr, klass->instance_size);
230 case MONO_TYPE_CLASS:
231 *ptr++ = MONO_DEBUGGER_TYPE_KIND_CLASS;
232 WRITE_UINT32 (ptr, klass->instance_size);
236 case MONO_TYPE_OBJECT:
237 g_assert (klass == mono_defaults.object_class);
238 *ptr++ = MONO_DEBUGGER_TYPE_KIND_OBJECT;
239 WRITE_UINT32 (ptr, klass->instance_size);
244 g_error (G_STRLOC ": Unknown builtin type %s.%s - %d", klass->name_space, klass->name, klass->byval_arg.type);
248 info->cinfo->type_info = info->type_info = allocate_type_entry (table, size, &info->type_data);
249 memcpy (info->type_data, buffer, size);
252 static MonoDebuggerBuiltinTypeInfo *
253 add_builtin_type (MonoDebuggerSymbolFile *symfile, MonoClass *klass)
255 MonoDebuggerClassInfo *cinfo;
256 MonoDebuggerBuiltinTypeInfo *info;
258 cinfo = g_new0 (MonoDebuggerClassInfo, 1);
259 cinfo->klass = klass;
261 cinfo->token = klass->element_class->type_token;
262 cinfo->rank = klass->rank;
264 cinfo->token = klass->type_token;
266 g_hash_table_insert (class_info_table, klass, cinfo);
268 info = g_new0 (MonoDebuggerBuiltinTypeInfo, 1);
272 write_builtin_type (mono_debugger_symbol_table, klass, info);
277 add_builtin_type_2 (MonoDebuggerBuiltinTypeInfo *info)
279 info->class_info = write_class (mono_debugger_symbol_table, info->klass);
280 * (guint32 *) (info->type_data + 5) = info->class_info;
283 MonoDebuggerBuiltinTypes *
284 mono_debugger_add_builtin_types (MonoDebuggerSymbolFile *symfile)
286 MonoDebuggerBuiltinTypes *types = g_new0 (MonoDebuggerBuiltinTypes, 1);
288 mono_debugger_symbol_table->corlib = symfile;
289 mono_debugger_symbol_table->builtin_types = types;
291 types->total_size = sizeof (MonoDebuggerBuiltinTypes);
292 types->type_info_size = sizeof (MonoDebuggerBuiltinTypeInfo);
294 types->object_type = add_builtin_type (symfile, mono_defaults.object_class);
295 types->byte_type = add_builtin_type (symfile, mono_defaults.byte_class);
296 types->void_type = add_builtin_type (symfile, mono_defaults.void_class);
297 types->boolean_type = add_builtin_type (symfile, mono_defaults.boolean_class);
298 types->sbyte_type = add_builtin_type (symfile, mono_defaults.sbyte_class);
299 types->int16_type = add_builtin_type (symfile, mono_defaults.int16_class);
300 types->uint16_type = add_builtin_type (symfile, mono_defaults.uint16_class);
301 types->int32_type = add_builtin_type (symfile, mono_defaults.int32_class);
302 types->uint32_type = add_builtin_type (symfile, mono_defaults.uint32_class);
303 types->int_type = add_builtin_type (symfile, mono_defaults.int_class);
304 types->uint_type = add_builtin_type (symfile, mono_defaults.uint_class);
305 types->int64_type = add_builtin_type (symfile, mono_defaults.int64_class);
306 types->uint64_type = add_builtin_type (symfile, mono_defaults.uint64_class);
307 types->single_type = add_builtin_type (symfile, mono_defaults.single_class);
308 types->double_type = add_builtin_type (symfile, mono_defaults.double_class);
309 types->char_type = add_builtin_type (symfile, mono_defaults.char_class);
310 types->string_type = add_builtin_type (symfile, mono_defaults.string_class);
312 types->enum_type = add_builtin_type (symfile, mono_defaults.enum_class);
313 types->array_type = add_builtin_type (symfile, mono_defaults.array_class);
314 types->exception_type = add_builtin_type (symfile, mono_defaults.exception_class);
316 add_builtin_type_2 (types->object_type);
317 add_builtin_type_2 (types->byte_type);
318 add_builtin_type_2 (types->void_type);
319 add_builtin_type_2 (types->boolean_type);
320 add_builtin_type_2 (types->sbyte_type);
321 add_builtin_type_2 (types->int16_type);
322 add_builtin_type_2 (types->uint16_type);
323 add_builtin_type_2 (types->int32_type);
324 add_builtin_type_2 (types->uint32_type);
325 add_builtin_type_2 (types->int_type);
326 add_builtin_type_2 (types->uint_type);
327 add_builtin_type_2 (types->int64_type);
328 add_builtin_type_2 (types->uint64_type);
329 add_builtin_type_2 (types->single_type);
330 add_builtin_type_2 (types->double_type);
331 add_builtin_type_2 (types->char_type);
332 add_builtin_type_2 (types->string_type);
333 add_builtin_type_2 (types->enum_type);
334 add_builtin_type_2 (types->array_type);
335 add_builtin_type_2 (types->exception_type);
341 mono_debugger_add_type (MonoDebuggerSymbolFile *symfile, MonoClass *klass)
343 MonoDebuggerClassInfo *cinfo;
345 mono_debugger_lock ();
347 cinfo = g_hash_table_lookup (class_info_table, klass);
349 mono_debugger_unlock ();
353 symfile->generation++;
355 cinfo = allocate_class_entry (symfile);
356 cinfo->klass = klass;
358 cinfo->token = klass->element_class->type_token;
359 cinfo->rank = klass->rank;
361 cinfo->token = klass->type_token;
362 g_hash_table_insert (class_info_table, klass, cinfo);
364 cinfo->type_info = write_class (mono_debugger_symbol_table, klass);
366 must_reload_symtabs = TRUE;
367 mono_debugger_unlock ();
371 mono_debugger_add_method (MonoDebuggerSymbolFile *symfile, MonoDebugMethodInfo *minfo,
372 MonoDebugMethodJitInfo *jit)
374 MonoSymbolFileMethodAddress *address;
375 MonoSymbolFileLexicalBlockEntry *block;
376 MonoDebugVarInfo *var_table;
377 MonoDebuggerRangeInfo *range;
378 MonoMethodHeader *header;
379 guint32 size, num_variables, variable_size, variable_offset;
380 guint32 type_size, type_offset, *type_index_table, has_this;
381 guint32 line_size, line_offset, block_offset, block_size;
382 MonoDebugLexicalBlockEntry *block_table;
383 MonoDebugLineNumberEntry *line_table;
388 if (!symfile->symfile->method_hash)
391 header = ((MonoMethodNormal *) minfo->method)->header;
393 symfile->generation++;
395 size = sizeof (MonoSymbolFileMethodAddress);
397 num_variables = read32(&(minfo->entry->_num_parameters)) + read32(&(minfo->entry->_num_locals));
398 has_this = jit->this_var != NULL;
400 variable_size = (num_variables + has_this) * sizeof (MonoDebugVarInfo);
401 variable_offset = size;
402 size += variable_size;
404 type_size = (num_variables + 1) * sizeof (gpointer);
408 if (jit->line_numbers) {
410 line_size = jit->line_numbers->len * sizeof (MonoDebugLineNumberEntry);
414 block_size = read32(&(minfo->entry->_num_lexical_blocks)) * sizeof (MonoSymbolFileLexicalBlockEntry);
418 address = g_malloc0 (size);
419 ptr = (guint8 *) address;
421 block = (MonoSymbolFileLexicalBlockEntry *)
422 (symfile->symfile->raw_contents + read32(&(minfo->entry->_lexical_block_table_offset)));
423 block_table = (MonoDebugLexicalBlockEntry *) (ptr + block_offset);
425 for (i = 0; i < read32(&(minfo->entry->_num_lexical_blocks)); i++, block++) {
426 block_table [i].start_address = _mono_debug_address_from_il_offset (jit, read32(&(block->_start_offset)));
427 block_table [i].end_address = _mono_debug_address_from_il_offset (jit, read32(&(block->_end_offset)));
430 address->size = size;
431 address->has_this = has_this;
432 address->start_address = jit->code_start;
433 address->end_address = jit->code_start + jit->code_size;
434 address->method_start_address = address->start_address + jit->prologue_end;
435 address->method_end_address = address->start_address + jit->epilogue_begin;
436 address->wrapper_address = jit->wrapper_addr;
437 address->variable_table_offset = variable_offset;
438 address->type_table_offset = type_offset;
439 address->lexical_block_table_offset = block_offset;
441 if (jit->line_numbers) {
442 address->num_line_numbers = jit->line_numbers->len;
443 address->line_number_offset = line_offset;
445 line_table = (MonoDebugLineNumberEntry *) (ptr + line_offset);
446 memcpy (line_table, jit->line_numbers->data, line_size);
449 range = allocate_range_entry (symfile);
450 range->index = minfo->index;
451 range->start_address = address->start_address;
452 range->end_address = address->end_address;
453 range->dynamic_data = address;
454 range->dynamic_size = size;
456 if ((minfo->method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
457 (minfo->method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
458 (minfo->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
461 var_table = (MonoDebugVarInfo *) (ptr + variable_offset);
462 type_table = (guint32 *) (ptr + type_offset);
464 type_index_table = (guint32 *)
465 (symfile->symfile->raw_contents + read32(&(minfo->entry->_type_index_table_offset)));
468 *var_table++ = *jit->this_var;
469 *type_table++ = write_type (mono_debugger_symbol_table, &minfo->method->klass->this_arg);
471 if (jit->num_params != read32(&(minfo->entry->_num_parameters))) {
472 g_warning (G_STRLOC ": Method %s.%s has %d parameters, but symbol file claims it has %d.",
473 minfo->method->klass->name, minfo->method->name, jit->num_params,
474 read32(&(minfo->entry->_num_parameters)));
475 var_table += read32(&(minfo->entry->_num_parameters));
477 for (i = 0; i < jit->num_params; i++) {
478 *var_table++ = jit->params [i];
479 *type_table++ = write_type (mono_debugger_symbol_table, minfo->method->signature->params [i]);
483 if (jit->num_locals < read32(&(minfo->entry->_num_locals))) {
484 g_warning (G_STRLOC ": Method %s.%s has %d locals, but symbol file claims it has %d.",
485 minfo->method->klass->name, minfo->method->name, jit->num_locals,
486 read32(&(minfo->entry->_num_locals)));
487 var_table += read32(&(minfo->entry->_num_locals));
489 g_assert ((header != NULL) || (minfo->entry->_num_locals == 0));
490 for (i = 0; i < read32(&(minfo->entry->_num_locals)); i++) {
491 *var_table++ = jit->locals [i];
492 *type_table++ = write_type (mono_debugger_symbol_table, header->locals [i]);
496 must_reload_symtabs = TRUE;
499 static MonoDebuggerRangeInfo *
500 allocate_range_entry (MonoDebuggerSymbolFile *symfile)
502 MonoDebuggerRangeInfo *retval;
503 guint32 size, chunks;
505 if (!symfile->range_table) {
506 size = sizeof (MonoDebuggerRangeInfo) * RANGE_TABLE_CHUNK_SIZE;
507 symfile->range_table = g_malloc0 (size);
508 symfile->num_range_entries = 1;
509 return symfile->range_table;
512 if (!((symfile->num_range_entries + 1) % RANGE_TABLE_CHUNK_SIZE)) {
513 chunks = (symfile->num_range_entries + 1) / RANGE_TABLE_CHUNK_SIZE;
514 size = sizeof (MonoDebuggerRangeInfo) * RANGE_TABLE_CHUNK_SIZE * (chunks + 1);
516 symfile->range_table = g_realloc (symfile->range_table, size);
519 retval = symfile->range_table + symfile->num_range_entries;
520 symfile->num_range_entries++;
524 static MonoDebuggerClassInfo *
525 allocate_class_entry (MonoDebuggerSymbolFile *symfile)
527 MonoDebuggerClassInfo *retval;
528 guint32 size, chunks;
530 if (!symfile->class_table) {
531 size = sizeof (MonoDebuggerClassInfo) * CLASS_TABLE_CHUNK_SIZE;
532 symfile->class_table = g_malloc0 (size);
533 symfile->num_class_entries = 1;
534 return symfile->class_table;
537 if (!((symfile->num_class_entries + 1) % CLASS_TABLE_CHUNK_SIZE)) {
538 chunks = (symfile->num_class_entries + 1) / CLASS_TABLE_CHUNK_SIZE;
539 size = sizeof (MonoDebuggerClassInfo) * CLASS_TABLE_CHUNK_SIZE * (chunks + 1);
541 symfile->class_table = g_realloc (symfile->class_table, size);
544 retval = symfile->class_table + symfile->num_class_entries;
545 symfile->num_class_entries++;
550 * Allocate a new entry of size `size' in the type table.
551 * Returns the global offset which is to be used to reference this type and
552 * a pointer (in the `ptr' argument) which is to be used to write the type.
555 allocate_type_entry (MonoDebuggerSymbolTable *table, guint32 size, guint8 **ptr)
560 g_assert (size + 4 < TYPE_TABLE_CHUNK_SIZE);
561 g_assert (ptr != NULL);
563 /* Initialize things if necessary. */
564 if (!table->current_type_table) {
565 table->current_type_table = g_malloc0 (TYPE_TABLE_CHUNK_SIZE);
566 table->type_table_size = TYPE_TABLE_CHUNK_SIZE;
567 table->type_table_chunk_size = TYPE_TABLE_CHUNK_SIZE;
568 table->type_table_offset = 1;
572 /* First let's check whether there's still enough room in the current_type_table. */
573 if (table->type_table_offset + size + 4 < table->type_table_size) {
574 retval = table->type_table_offset;
575 table->type_table_offset += size + 4;
576 data = ((guint8 *) table->current_type_table) + retval - table->type_table_start;
577 *(gint32 *) data = size;
578 data += sizeof(gint32);
583 /* Add the current_type_table to the type_tables vector and ... */
584 if (!table->type_tables) {
585 guint32 tsize = sizeof (gpointer) * TYPE_TABLE_PTR_CHUNK_SIZE;
586 table->type_tables = g_malloc0 (tsize);
589 if (!((table->num_type_tables + 1) % TYPE_TABLE_PTR_CHUNK_SIZE)) {
590 guint32 chunks = (table->num_type_tables + 1) / TYPE_TABLE_PTR_CHUNK_SIZE;
591 guint32 tsize = sizeof (gpointer) * TYPE_TABLE_PTR_CHUNK_SIZE * (chunks + 1);
593 table->type_tables = g_realloc (table->type_tables, tsize);
596 table->type_tables [table->num_type_tables++] = table->current_type_table;
598 /* .... allocate a new current_type_table. */
599 table->current_type_table = g_malloc0 (TYPE_TABLE_CHUNK_SIZE);
600 table->type_table_start = table->type_table_offset = table->type_table_size;
601 table->type_table_size += TYPE_TABLE_CHUNK_SIZE;
607 property_is_static (MonoProperty *prop)
615 return method->flags & METHOD_ATTRIBUTE_STATIC;
619 write_class (MonoDebuggerSymbolTable *table, MonoClass *klass)
621 guint8 buffer [BUFSIZ], *ptr = buffer, *old_ptr;
622 GPtrArray *methods = NULL, *static_methods = NULL, *ctors = NULL;
623 int num_fields = 0, num_static_fields = 0, num_properties = 0, num_static_properties = 0;
624 int num_methods = 0, num_static_methods = 0, num_params = 0, num_static_params = 0, base_offset = 0;
625 int num_ctors = 0, num_ctor_params = 0;
626 guint32 size, data_size, offset;
627 GHashTable *method_slots = NULL;
630 g_assert (!klass->init_pending);
631 mono_class_init (klass);
633 offset = GPOINTER_TO_UINT (g_hash_table_lookup (class_table, klass));
637 if (klass->enumtype) {
638 offset = allocate_type_entry (table, 13, &ptr);
639 g_hash_table_insert (class_table, klass, GUINT_TO_POINTER (offset));
641 *ptr++ = MONO_DEBUGGER_TYPE_KIND_ENUM;
642 WRITE_UINT32 (ptr, klass->instance_size);
643 WRITE_UINT32 (ptr, table->builtin_types->enum_type->type_info);
644 WRITE_UINT32 (ptr, write_type (table, klass->enum_basetype));
648 for (i = 0; i < klass->field.count; i++)
649 if (!(klass->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
654 for (i = 0; i < klass->property.count; i++)
655 if (!property_is_static (&klass->properties [i]))
658 ++num_static_properties;
660 method_slots = g_hash_table_new (NULL, NULL);
661 methods = g_ptr_array_new ();
662 static_methods = g_ptr_array_new ();
663 ctors = g_ptr_array_new ();
665 for (i = 0; i < klass->method.count; i++) {
666 MonoMethod *method = klass->methods [i];
668 if (!strcmp (method->name, ".cctor"))
670 if (!((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC))
673 if (!strcmp (method->name, ".ctor")) {
675 num_ctor_params += method->signature->param_count;
676 g_ptr_array_add (ctors, method);
680 if (method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME)
683 if (method->slot != -1) {
684 if (g_hash_table_lookup (method_slots, GUINT_TO_POINTER (method->slot)))
686 g_hash_table_insert (method_slots, GUINT_TO_POINTER (method->slot), method);
689 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
690 ++num_static_methods;
691 num_static_params += method->signature->param_count;
692 g_ptr_array_add (static_methods, method);
695 num_params += method->signature->param_count;
696 g_ptr_array_add (methods, method);
700 g_hash_table_destroy (method_slots);
702 size = 66 + sizeof (gpointer) + num_fields * 8 + num_static_fields * 8 + num_properties * (4 + 2 * sizeof (gpointer)) +
703 num_static_properties * (4 + 2 * sizeof (gpointer)) + num_methods * (8 + sizeof (gpointer)) + num_params * 4 +
704 num_static_methods * (8 + sizeof (gpointer)) + num_static_params * 4 + num_ctors * (8 + sizeof (gpointer)) +
709 offset = allocate_type_entry (table, data_size, &ptr);
712 g_hash_table_insert (class_table, klass, GUINT_TO_POINTER (offset));
714 *ptr++ = MONO_DEBUGGER_TYPE_KIND_CLASS_INFO;
716 if (klass->valuetype)
717 base_offset = - sizeof (MonoObject);
719 WRITE_UINT32 (ptr, klass->instance_size + base_offset);
720 *ptr++ = klass->valuetype;
721 WRITE_POINTER (ptr, klass);
722 WRITE_UINT32 (ptr, num_fields);
723 WRITE_UINT32 (ptr, num_fields * 8);
724 WRITE_UINT32 (ptr, num_static_fields);
725 WRITE_UINT32 (ptr, num_static_fields * 8);
726 WRITE_UINT32 (ptr, num_properties);
727 WRITE_UINT32 (ptr, num_properties * (4 + 2 * sizeof (gpointer)));
728 WRITE_UINT32 (ptr, num_static_properties);
729 WRITE_UINT32 (ptr, num_static_properties * (4 + 2 * sizeof (gpointer)));
730 WRITE_UINT32 (ptr, num_methods);
731 WRITE_UINT32 (ptr, num_methods * (4 + 2 * sizeof (gpointer)) + num_params * sizeof (gpointer));
732 WRITE_UINT32 (ptr, num_static_methods);
733 WRITE_UINT32 (ptr, num_static_methods * (4 + 2 * sizeof (gpointer)) + num_static_params * sizeof (gpointer));
734 WRITE_UINT32 (ptr, num_ctors);
735 WRITE_UINT32 (ptr, num_ctors * (4 + 2 * sizeof (gpointer)) + num_ctor_params * sizeof (gpointer));
736 if (klass->parent && (klass->parent != mono_defaults.object_class))
737 WRITE_UINT32 (ptr, write_class (table, klass->parent));
739 WRITE_UINT32 (ptr, 0);
741 for (i = 0; i < klass->field.count; i++) {
742 if (klass->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
745 WRITE_UINT32 (ptr, klass->fields [i].offset + base_offset);
746 WRITE_UINT32 (ptr, write_type (table, klass->fields [i].type));
749 for (i = 0; i < klass->property.count; i++) {
750 if (property_is_static (&klass->properties [i]))
753 if (klass->properties [i].get)
754 WRITE_UINT32 (ptr, write_type (table, klass->properties [i].get->signature->ret));
756 WRITE_UINT32 (ptr, 0);
757 WRITE_POINTER (ptr, klass->properties [i].get);
758 WRITE_POINTER (ptr, klass->properties [i].set);
761 for (i = 0; i < methods->len; i++) {
762 MonoMethod *method = g_ptr_array_index (methods, i);
765 WRITE_POINTER (ptr, method);
766 if ((method->signature->ret) && (method->signature->ret->type != MONO_TYPE_VOID))
767 WRITE_UINT32 (ptr, write_type (table, method->signature->ret));
769 WRITE_UINT32 (ptr, 0);
770 WRITE_UINT32 (ptr, method->signature->param_count);
771 for (j = 0; j < method->signature->param_count; j++)
772 WRITE_UINT32 (ptr, write_type (table, method->signature->params [j]));
775 g_ptr_array_free (methods, FALSE);
777 for (i = 0; i < klass->field.count; i++) {
778 if (!(klass->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
781 WRITE_UINT32 (ptr, klass->fields [i].offset);
782 WRITE_UINT32 (ptr, write_type (table, klass->fields [i].type));
785 for (i = 0; i < klass->property.count; i++) {
786 if (!property_is_static (&klass->properties [i]))
789 if (klass->properties [i].get)
790 WRITE_UINT32 (ptr, write_type (table, klass->properties [i].get->signature->ret));
792 WRITE_UINT32 (ptr, 0);
793 WRITE_POINTER (ptr, klass->properties [i].get);
794 WRITE_POINTER (ptr, klass->properties [i].set);
797 for (i = 0; i < static_methods->len; i++) {
798 MonoMethod *method = g_ptr_array_index (static_methods, i);
801 WRITE_POINTER (ptr, method);
802 if ((method->signature->ret) && (method->signature->ret->type != MONO_TYPE_VOID))
803 WRITE_UINT32 (ptr, write_type (table, method->signature->ret));
805 WRITE_UINT32 (ptr, 0);
806 WRITE_UINT32 (ptr, method->signature->param_count);
807 for (j = 0; j < method->signature->param_count; j++)
808 WRITE_UINT32 (ptr, write_type (table, method->signature->params [j]));
811 g_ptr_array_free (static_methods, FALSE);
813 for (i = 0; i < ctors->len; i++) {
814 MonoMethod *ctor = g_ptr_array_index (ctors, i);
817 WRITE_POINTER (ptr, ctor);
818 WRITE_UINT32 (ptr, 0);
819 WRITE_UINT32 (ptr, ctor->signature->param_count);
820 for (j = 0; j < ctor->signature->param_count; j++)
821 WRITE_UINT32 (ptr, write_type (table, ctor->signature->params [j]));
824 g_ptr_array_free (ctors, FALSE);
826 if (ptr - old_ptr != data_size) {
827 g_warning (G_STRLOC ": %d,%d,%d", ptr - old_ptr, data_size, sizeof (gpointer));
829 g_warning (G_STRLOC ": %s.%s", klass->name_space, klass->name);
830 g_assert_not_reached ();
837 * Adds type `type' to the type table and returns its offset.
840 write_type (MonoDebuggerSymbolTable *table, MonoType *type)
842 guint8 buffer [BUFSIZ], *ptr = buffer;
843 guint32 size, offset;
846 offset = GPOINTER_TO_UINT (g_hash_table_lookup (type_table, type));
850 klass = mono_class_from_mono_type (type);
851 if (klass->init_pending)
853 mono_class_init (klass);
855 switch (type->type) {
857 return table->builtin_types->void_type->type_info;
859 case MONO_TYPE_BOOLEAN:
860 return table->builtin_types->boolean_type->type_info;
863 return table->builtin_types->sbyte_type->type_info;
866 return table->builtin_types->byte_type->type_info;
869 return table->builtin_types->char_type->type_info;
872 return table->builtin_types->int16_type->type_info;
875 return table->builtin_types->uint16_type->type_info;
878 return table->builtin_types->int32_type->type_info;
881 return table->builtin_types->uint32_type->type_info;
884 return table->builtin_types->int64_type->type_info;
887 return table->builtin_types->uint64_type->type_info;
890 return table->builtin_types->single_type->type_info;
893 return table->builtin_types->double_type->type_info;
895 case MONO_TYPE_STRING:
896 return table->builtin_types->string_type->type_info;
899 return table->builtin_types->int_type->type_info;
902 return table->builtin_types->uint_type->type_info;
904 case MONO_TYPE_SZARRAY: {
907 *ptr++ = MONO_DEBUGGER_TYPE_KIND_SZARRAY;
908 WRITE_UINT32 (ptr, sizeof (MonoArray));
909 g_assert (table->builtin_types->array_type->type_info != 0);
910 WRITE_UINT32 (ptr, table->builtin_types->array_type->type_info);
911 *ptr++ = (guint8*)&array.max_length - (guint8*)&array;
912 *ptr++ = sizeof (array.max_length);
913 *ptr++ = (guint8*)&array.vector - (guint8*)&array;
914 WRITE_UINT32 (ptr, write_type (table, &type->data.klass->byval_arg));
918 case MONO_TYPE_ARRAY: {
920 MonoArrayBounds bounds;
922 *ptr++ = MONO_DEBUGGER_TYPE_KIND_ARRAY;
923 WRITE_UINT32 (ptr, sizeof (MonoArray));
924 g_assert (table->builtin_types->array_type->type_info != 0);
925 WRITE_UINT32 (ptr, table->builtin_types->array_type->type_info);
926 *ptr++ = (guint8*)&array.max_length - (guint8*)&array;
927 *ptr++ = sizeof (array.max_length);
928 *ptr++ = (guint8*)&array.vector - (guint8*)&array;
929 *ptr++ = klass->rank;
930 *ptr++ = (guint8*)&array.bounds - (guint8*)&array;
931 *ptr++ = sizeof (MonoArrayBounds);
932 *ptr++ = (guint8*)&bounds.lower_bound - (guint8*)&bounds;
933 *ptr++ = sizeof (bounds.lower_bound);
934 *ptr++ = (guint8*)&bounds.length - (guint8*)&bounds;
935 *ptr++ = sizeof (bounds.length);
936 WRITE_UINT32 (ptr, write_type (table, &type->data.array->eklass->byval_arg));
940 case MONO_TYPE_VALUETYPE:
941 case MONO_TYPE_CLASS:
942 case MONO_TYPE_OBJECT:
943 return write_class (table, klass);
946 *ptr++ = MONO_DEBUGGER_TYPE_KIND_POINTER;
947 WRITE_UINT32 (ptr, sizeof (gpointer));
948 WRITE_UINT32 (ptr, write_type (table, type->data.type));
952 /* g_message (G_STRLOC ": %s.%s - %p - %d", klass->name_space, klass->name, klass, type->type); */
953 *ptr++ = MONO_DEBUGGER_TYPE_KIND_UNKNOWN;
954 WRITE_UINT32 (ptr, klass->instance_size);
955 WRITE_UINT32 (ptr, write_class (table, klass));
960 offset = allocate_type_entry (mono_debugger_symbol_table, size, &ptr);
961 memcpy (ptr, buffer, size);
966 MonoReflectionMethod *
967 ves_icall_MonoDebugger_GetMethod (MonoReflectionAssembly *assembly, guint32 token)
971 method = mono_get_method (mono_assembly_get_image (assembly->assembly), token, NULL);
973 return mono_method_get_object (mono_domain_get (), method, NULL);
977 ves_icall_MonoDebugger_GetMethodToken (MonoReflectionAssembly *assembly, MonoReflectionMethod *method)
979 return method->method->token;
983 ves_icall_MonoDebugger_GetType (MonoReflectionAssembly *assembly, guint32 token)
987 klass = mono_class_get (mono_assembly_get_image (assembly->assembly), token);
989 g_warning (G_STRLOC ": %x", token);
993 return mono_type_get_object (mono_domain_get (), &klass->byval_arg);
997 ves_icall_MonoDebugger_GetLocalTypeFromSignature (MonoReflectionAssembly *assembly, MonoArray *signature)
1005 MONO_CHECK_ARG_NULL (assembly);
1006 MONO_CHECK_ARG_NULL (signature);
1008 domain = mono_domain_get();
1009 image = mono_assembly_get_image (assembly->assembly);
1011 ptr = mono_array_addr (signature, char, 0);
1012 g_assert (*ptr++ == 0x07);
1013 len = mono_metadata_decode_value (ptr, &ptr);
1014 g_assert (len == 1);
1016 type = mono_metadata_parse_type (image, MONO_PARSE_LOCAL, 0, ptr, &ptr);
1018 return mono_type_get_object (domain, type);
1022 mono_debugger_event (MonoDebuggerEvent event, gpointer data, guint32 arg)
1024 if (mono_debugger_event_handler)
1025 (* mono_debugger_event_handler) (event, data, arg);
1029 mono_debugger_cleanup (void)
1031 /* Do nothing yet. */
1035 * Debugger breakpoint interface.
1037 * This interface is used to insert breakpoints on methods which are not yet JITed.
1038 * The debugging code keeps a list of all such breakpoints and automatically inserts the
1039 * breakpoint when the method is JITed.
1042 static GPtrArray *breakpoints = NULL;
1045 mono_debugger_insert_breakpoint_full (MonoMethodDesc *desc)
1047 static int last_breakpoint_id = 0;
1048 MonoDebuggerBreakpointInfo *info;
1050 info = g_new0 (MonoDebuggerBreakpointInfo, 1);
1052 info->index = ++last_breakpoint_id;
1055 breakpoints = g_ptr_array_new ();
1057 g_ptr_array_add (breakpoints, info);
1063 mono_debugger_remove_breakpoint (int breakpoint_id)
1070 for (i = 0; i < breakpoints->len; i++) {
1071 MonoDebuggerBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
1073 if (info->index != breakpoint_id)
1076 mono_method_desc_free (info->desc);
1077 g_ptr_array_remove (breakpoints, info);
1086 mono_debugger_insert_breakpoint (const gchar *method_name, gboolean include_namespace)
1088 MonoMethodDesc *desc;
1090 desc = mono_method_desc_new (method_name, include_namespace);
1094 return mono_debugger_insert_breakpoint_full (desc);
1098 mono_debugger_method_has_breakpoint (MonoMethod *method)
1102 if (!breakpoints || (method->wrapper_type != MONO_WRAPPER_NONE))
1105 for (i = 0; i < breakpoints->len; i++) {
1106 MonoDebuggerBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
1108 if (!mono_method_desc_full_match (info->desc, method))
1118 mono_debugger_breakpoint_callback (MonoMethod *method, guint32 index)
1120 mono_debugger_event (MONO_DEBUGGER_EVENT_BREAKPOINT, method, index);
1124 get_exception_message (MonoObject *exc)
1126 char *message = NULL;
1132 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
1133 klass = exc->vtable->klass;
1135 while (klass && method == NULL) {
1136 for (i = 0; i < klass->method.count; ++i) {
1137 method = klass->methods [i];
1138 if (!strcmp ("ToString", method->name) &&
1139 method->signature->param_count == 0 &&
1140 method->flags & METHOD_ATTRIBUTE_VIRTUAL &&
1141 method->flags & METHOD_ATTRIBUTE_PUBLIC) {
1148 klass = klass->parent;
1153 str = (MonoString *) mono_runtime_invoke (method, exc, NULL, NULL);
1155 message = mono_string_to_utf8 (str);
1162 mono_debugger_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
1167 if (method->klass->valuetype && (obj != NULL))
1168 obj = mono_value_box (mono_domain_get (), method->klass, obj);
1170 if (!strcmp (method->name, ".ctor")) {
1171 retval = obj = mono_object_new (mono_domain_get (), method->klass);
1173 mono_runtime_invoke (method, obj, params, exc);
1175 retval = mono_runtime_invoke (method, obj, params, exc);
1180 message = get_exception_message (*exc);
1182 *exc = (MonoObject *) mono_string_new_wrapper (message);
1190 mono_debugger_lookup_type (const gchar *type_name)
1194 mono_debugger_lock ();
1196 for (i = 0; i < mono_debugger_symbol_table->num_symbol_files; i++) {
1197 MonoDebuggerSymbolFile *symfile = mono_debugger_symbol_table->symbol_files [i];
1202 name = g_strdup (type_name);
1203 type = mono_reflection_type_from_name (name, symfile->image);
1208 offset = write_type (mono_debugger_symbol_table, type);
1210 mono_debugger_unlock ();
1214 mono_debugger_unlock ();
1219 mono_debugger_lookup_assembly (const gchar *name)
1221 MonoAssembly *assembly;
1222 MonoImageOpenStatus status;
1225 mono_debugger_lock ();
1228 for (i = 0; i < mono_debugger_symbol_table->num_symbol_files; i++) {
1229 MonoDebuggerSymbolFile *symfile = mono_debugger_symbol_table->symbol_files [i];
1231 if (!strcmp (symfile->image_file, name)) {
1232 mono_debugger_unlock ();
1237 assembly = mono_assembly_open (name, &status);
1239 if (status != MONO_IMAGE_OK) {
1240 g_warning (G_STRLOC ": Cannot open image `%s'", name);
1241 mono_debugger_unlock ();
1245 must_reload_symtabs = TRUE;