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/exception.h>
9 #include <mono/metadata/mono-debug.h>
10 #include <mono/metadata/mono-debug-debugger.h>
12 #define SYMFILE_TABLE_CHUNK_SIZE 16
13 #define RANGE_TABLE_CHUNK_SIZE 256
14 #define CLASS_TABLE_CHUNK_SIZE 256
15 #define TYPE_TABLE_PTR_CHUNK_SIZE 256
16 #define TYPE_TABLE_CHUNK_SIZE 65536
18 static CRITICAL_SECTION debugger_lock_mutex;
19 static gboolean mono_debugger_initialized = FALSE;
21 static GHashTable *images = NULL;
22 static GHashTable *type_table = NULL;
23 static GHashTable *class_table = NULL;
24 static GHashTable *class_info_table = NULL;
26 static MonoDebuggerRangeInfo *allocate_range_entry (MonoDebuggerSymbolFile *symfile);
27 static MonoDebuggerClassInfo *allocate_class_entry (MonoDebuggerSymbolFile *symfile);
28 static guint32 allocate_type_entry (MonoDebuggerSymbolTable *table, guint32 size, guint8 **ptr);
29 static guint32 write_type (MonoDebuggerSymbolTable *table, MonoType *type);
30 static guint32 write_class (MonoDebuggerSymbolTable *table, MonoClass *klass);
32 MonoDebuggerSymbolTable *mono_debugger_symbol_table = NULL;
33 void (*mono_debugger_event_handler) (MonoDebuggerEvent event, gpointer data, guint32 arg) = NULL;
35 #define WRITE_UINT32(ptr,value) G_STMT_START { \
36 * ((guint32 *) ptr) = value; \
40 #define WRITE_POINTER(ptr,value) G_STMT_START { \
41 * ((gpointer *) ptr) = value; \
42 ptr += sizeof (gpointer); \
45 #ifndef PLATFORM_WIN32
47 MonoDebuggerIOLayer mono_debugger_io_layer = {
48 InitializeCriticalSection, DeleteCriticalSection, TryEnterCriticalSection,
49 EnterCriticalSection, LeaveCriticalSection, WaitForSingleObject, SignalObjectAndWait,
50 WaitForMultipleObjects, CreateSemaphore, ReleaseSemaphore, CreateThread
56 mono_debugger_lock (void)
58 if (mono_debugger_initialized)
59 EnterCriticalSection (&debugger_lock_mutex);
63 mono_debugger_unlock (void)
65 if (mono_debugger_initialized)
66 LeaveCriticalSection (&debugger_lock_mutex);
69 static MonoDebuggerSymbolFile *
70 allocate_symbol_file_entry (MonoDebuggerSymbolTable *table)
72 MonoDebuggerSymbolFile *symfile;
74 if (!table->symbol_files)
75 table->symbol_files = g_new0 (MonoDebuggerSymbolFile *, SYMFILE_TABLE_CHUNK_SIZE);
76 else if (!((table->num_symbol_files + 1) % SYMFILE_TABLE_CHUNK_SIZE)) {
77 guint32 chunks = (table->num_symbol_files + 1) / SYMFILE_TABLE_CHUNK_SIZE;
78 guint32 size = sizeof (MonoDebuggerSymbolFile *) * SYMFILE_TABLE_CHUNK_SIZE * (chunks + 1);
80 table->symbol_files = g_realloc (table->symbol_files, size);
83 symfile = g_new0 (MonoDebuggerSymbolFile, 1);
84 symfile->index = table->num_symbol_files;
85 table->symbol_files [table->num_symbol_files++] = symfile;
90 mono_debugger_initialize (MonoDomain *domain)
92 MonoDebuggerSymbolTable *symbol_table;
94 g_assert (!mono_debugger_initialized);
96 InitializeCriticalSection (&debugger_lock_mutex);
98 mono_debugger_lock ();
100 symbol_table = g_new0 (MonoDebuggerSymbolTable, 1);
101 symbol_table->magic = MONO_DEBUGGER_MAGIC;
102 symbol_table->version = MONO_DEBUGGER_VERSION;
103 symbol_table->total_size = sizeof (MonoDebuggerSymbolTable);
105 symbol_table->domain = domain;
107 images = g_hash_table_new (g_direct_hash, g_direct_equal);
108 type_table = g_hash_table_new (g_direct_hash, g_direct_equal);
109 class_table = g_hash_table_new (g_direct_hash, g_direct_equal);
110 class_info_table = g_hash_table_new (g_direct_hash, g_direct_equal);
112 mono_debugger_symbol_table = symbol_table;
113 mono_debugger_initialized = TRUE;
115 mono_debugger_unlock ();
118 MonoDebuggerSymbolFile *
119 mono_debugger_add_symbol_file (MonoDebugHandle *handle)
121 MonoDebuggerSymbolFile *info;
123 g_assert (mono_debugger_initialized);
124 mono_debugger_lock ();
126 info = g_hash_table_lookup (images, handle->image);
130 info = allocate_symbol_file_entry (mono_debugger_symbol_table);
131 info->symfile = handle->symfile;
132 info->image = handle->image;
133 info->image_file = handle->image_file;
135 g_hash_table_insert (images, handle->image, info);
136 mono_debugger_unlock ();
142 write_builtin_type (MonoDebuggerSymbolTable *table, MonoClass *klass, MonoDebuggerBuiltinTypeInfo *info)
144 guint8 buffer [BUFSIZ], *ptr = buffer;
147 g_assert (!klass->init_pending);
148 mono_class_init (klass);
150 switch (klass->byval_arg.type) {
152 *ptr++ = MONO_DEBUGGER_TYPE_KIND_UNKNOWN;
153 WRITE_UINT32 (ptr, 0);
157 case MONO_TYPE_BOOLEAN:
169 *ptr++ = MONO_DEBUGGER_TYPE_KIND_FUNDAMENTAL;
170 WRITE_UINT32 (ptr, klass->instance_size - sizeof (MonoObject));
174 case MONO_TYPE_STRING: {
177 *ptr++ = MONO_DEBUGGER_TYPE_KIND_STRING;
178 WRITE_UINT32 (ptr, klass->instance_size);
180 *ptr++ = (guint8*)&string.length - (guint8*)&string;
181 *ptr++ = sizeof (string.length);
182 *ptr++ = (guint8*)&string.chars - (guint8*)&string;
189 *ptr++ = MONO_DEBUGGER_TYPE_KIND_FUNDAMENTAL;
190 WRITE_UINT32 (ptr, sizeof (void *));
194 case MONO_TYPE_VALUETYPE:
195 *ptr++ = MONO_DEBUGGER_TYPE_KIND_STRUCT;
196 WRITE_UINT32 (ptr, klass->instance_size);
200 case MONO_TYPE_CLASS:
201 *ptr++ = MONO_DEBUGGER_TYPE_KIND_CLASS;
202 WRITE_UINT32 (ptr, klass->instance_size);
206 case MONO_TYPE_OBJECT:
207 g_assert (klass == mono_defaults.object_class);
208 *ptr++ = MONO_DEBUGGER_TYPE_KIND_OBJECT;
209 WRITE_UINT32 (ptr, klass->instance_size);
214 g_error (G_STRLOC ": Unknown builtin type %s.%s - %d", klass->name_space, klass->name, klass->byval_arg.type);
218 info->cinfo->type_info = info->type_info = allocate_type_entry (table, size, &info->type_data);
219 memcpy (info->type_data, buffer, size);
222 static MonoDebuggerBuiltinTypeInfo *
223 add_builtin_type (MonoDebuggerSymbolFile *symfile, MonoClass *klass)
225 MonoDebuggerClassInfo *cinfo;
226 MonoDebuggerBuiltinTypeInfo *info;
228 cinfo = g_new0 (MonoDebuggerClassInfo, 1);
229 cinfo->klass = klass;
231 cinfo->token = klass->element_class->type_token;
232 cinfo->rank = klass->rank;
234 cinfo->token = klass->type_token;
236 g_hash_table_insert (class_info_table, klass, cinfo);
238 info = g_new0 (MonoDebuggerBuiltinTypeInfo, 1);
242 write_builtin_type (mono_debugger_symbol_table, klass, info);
247 add_builtin_type_2 (MonoDebuggerBuiltinTypeInfo *info)
249 info->class_info = write_class (mono_debugger_symbol_table, info->klass);
250 * (guint32 *) (info->type_data + 5) = info->class_info;
253 MonoDebuggerBuiltinTypes *
254 mono_debugger_add_builtin_types (MonoDebuggerSymbolFile *symfile)
256 MonoDebuggerBuiltinTypes *types = g_new0 (MonoDebuggerBuiltinTypes, 1);
258 mono_debugger_symbol_table->corlib = symfile;
259 mono_debugger_symbol_table->builtin_types = types;
261 types->total_size = sizeof (MonoDebuggerBuiltinTypes);
262 types->type_info_size = sizeof (MonoDebuggerBuiltinTypeInfo);
264 types->object_type = add_builtin_type (symfile, mono_defaults.object_class);
265 types->byte_type = add_builtin_type (symfile, mono_defaults.byte_class);
266 types->void_type = add_builtin_type (symfile, mono_defaults.void_class);
267 types->boolean_type = add_builtin_type (symfile, mono_defaults.boolean_class);
268 types->sbyte_type = add_builtin_type (symfile, mono_defaults.sbyte_class);
269 types->int16_type = add_builtin_type (symfile, mono_defaults.int16_class);
270 types->uint16_type = add_builtin_type (symfile, mono_defaults.uint16_class);
271 types->int32_type = add_builtin_type (symfile, mono_defaults.int32_class);
272 types->uint32_type = add_builtin_type (symfile, mono_defaults.uint32_class);
273 types->int_type = add_builtin_type (symfile, mono_defaults.int_class);
274 types->uint_type = add_builtin_type (symfile, mono_defaults.uint_class);
275 types->int64_type = add_builtin_type (symfile, mono_defaults.int64_class);
276 types->uint64_type = add_builtin_type (symfile, mono_defaults.uint64_class);
277 types->single_type = add_builtin_type (symfile, mono_defaults.single_class);
278 types->double_type = add_builtin_type (symfile, mono_defaults.double_class);
279 types->char_type = add_builtin_type (symfile, mono_defaults.char_class);
280 types->string_type = add_builtin_type (symfile, mono_defaults.string_class);
282 types->enum_type = add_builtin_type (symfile, mono_defaults.enum_class);
283 types->array_type = add_builtin_type (symfile, mono_defaults.array_class);
284 types->exception_type = add_builtin_type (symfile, mono_defaults.exception_class);
286 add_builtin_type_2 (types->object_type);
287 add_builtin_type_2 (types->byte_type);
288 add_builtin_type_2 (types->void_type);
289 add_builtin_type_2 (types->boolean_type);
290 add_builtin_type_2 (types->sbyte_type);
291 add_builtin_type_2 (types->int16_type);
292 add_builtin_type_2 (types->uint16_type);
293 add_builtin_type_2 (types->int32_type);
294 add_builtin_type_2 (types->uint32_type);
295 add_builtin_type_2 (types->int_type);
296 add_builtin_type_2 (types->uint_type);
297 add_builtin_type_2 (types->int64_type);
298 add_builtin_type_2 (types->uint64_type);
299 add_builtin_type_2 (types->single_type);
300 add_builtin_type_2 (types->double_type);
301 add_builtin_type_2 (types->char_type);
302 add_builtin_type_2 (types->string_type);
303 add_builtin_type_2 (types->enum_type);
304 add_builtin_type_2 (types->array_type);
305 add_builtin_type_2 (types->exception_type);
311 mono_debugger_add_type (MonoDebuggerSymbolFile *symfile, MonoClass *klass)
313 MonoDebuggerClassInfo *cinfo;
315 mono_debugger_lock ();
317 cinfo = g_hash_table_lookup (class_info_table, klass);
319 mono_debugger_unlock ();
323 symfile->generation++;
325 cinfo = allocate_class_entry (symfile);
326 cinfo->klass = klass;
328 cinfo->token = klass->element_class->type_token;
329 cinfo->rank = klass->rank;
331 cinfo->token = klass->type_token;
332 g_hash_table_insert (class_info_table, klass, cinfo);
334 cinfo->type_info = write_class (mono_debugger_symbol_table, klass);
336 mono_debugger_event (MONO_DEBUGGER_EVENT_TYPE_ADDED, NULL, 0);
337 mono_debugger_unlock ();
341 mono_debugger_add_method (MonoDebuggerSymbolFile *symfile, MonoDebugMethodInfo *minfo,
342 MonoDebugMethodJitInfo *jit)
344 MonoSymbolFileMethodAddress *address;
345 MonoSymbolFileLexicalBlockEntry *block;
346 MonoDebugVarInfo *var_table;
347 MonoDebuggerRangeInfo *range;
348 MonoMethodHeader *header;
349 guint32 size, num_variables, variable_size, variable_offset;
350 guint32 type_size, type_offset, *type_index_table, has_this;
351 guint32 line_size, line_offset, block_offset, block_size;
352 MonoDebugLexicalBlockEntry *block_table;
353 MonoDebugLineNumberEntry *line_table;
358 if (!symfile->symfile->method_hash)
361 header = ((MonoMethodNormal *) minfo->method)->header;
363 symfile->generation++;
365 size = sizeof (MonoSymbolFileMethodAddress);
367 num_variables = minfo->entry->num_parameters + minfo->entry->num_locals;
368 has_this = jit->this_var != NULL;
370 variable_size = (num_variables + has_this) * sizeof (MonoDebugVarInfo);
371 variable_offset = size;
372 size += variable_size;
374 type_size = (num_variables + 1) * sizeof (gpointer);
378 if (jit->line_numbers) {
380 line_size = jit->line_numbers->len * sizeof (MonoDebugLineNumberEntry);
384 block_size = minfo->entry->num_lexical_blocks * sizeof (MonoSymbolFileLexicalBlockEntry);
388 address = g_malloc0 (size);
389 ptr = (guint8 *) address;
391 block = (MonoSymbolFileLexicalBlockEntry *)
392 (symfile->symfile->raw_contents + minfo->entry->lexical_block_table_offset);
393 block_table = (MonoDebugLexicalBlockEntry *) (ptr + block_offset);
395 for (i = 0; i < minfo->entry->num_lexical_blocks; i++, block++) {
396 block_table [i].start_address = _mono_debug_address_from_il_offset (jit, block->start_offset);
397 block_table [i].end_address = _mono_debug_address_from_il_offset (jit, block->end_offset);
400 address->size = size;
401 address->has_this = has_this;
402 address->start_address = jit->code_start;
403 address->end_address = jit->code_start + jit->code_size;
404 address->method_start_address = address->start_address + jit->prologue_end;
405 address->method_end_address = address->start_address + jit->epilogue_begin;
406 address->wrapper_address = jit->wrapper_addr;
407 address->variable_table_offset = variable_offset;
408 address->type_table_offset = type_offset;
409 address->lexical_block_table_offset = block_offset;
411 if (jit->line_numbers) {
412 address->num_line_numbers = jit->line_numbers->len;
413 address->line_number_offset = line_offset;
415 line_table = (MonoDebugLineNumberEntry *) (ptr + line_offset);
416 memcpy (line_table, jit->line_numbers->data, line_size);
419 range = allocate_range_entry (symfile);
420 range->index = minfo->index;
421 range->start_address = address->start_address;
422 range->end_address = address->end_address;
423 range->dynamic_data = address;
424 range->dynamic_size = size;
426 if ((minfo->method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
427 (minfo->method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
428 (minfo->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
431 var_table = (MonoDebugVarInfo *) (ptr + variable_offset);
432 type_table = (guint32 *) (ptr + type_offset);
434 type_index_table = (guint32 *)
435 (symfile->symfile->raw_contents + minfo->entry->type_index_table_offset);
438 *var_table++ = *jit->this_var;
439 *type_table++ = write_type (mono_debugger_symbol_table, &minfo->method->klass->this_arg);
441 if (jit->num_params != minfo->entry->num_parameters) {
442 g_warning (G_STRLOC ": Method %s.%s has %d parameters, but symbol file claims it has %d.",
443 minfo->method->klass->name, minfo->method->name, jit->num_params,
444 minfo->entry->num_parameters);
445 var_table += minfo->entry->num_parameters;
447 for (i = 0; i < jit->num_params; i++) {
448 *var_table++ = jit->params [i];
449 *type_table++ = write_type (mono_debugger_symbol_table, minfo->method->signature->params [i]);
453 if (jit->num_locals < minfo->entry->num_locals) {
454 g_warning (G_STRLOC ": Method %s.%s has %d locals, but symbol file claims it has %d.",
455 minfo->method->klass->name, minfo->method->name, jit->num_locals,
456 minfo->entry->num_locals);
457 var_table += minfo->entry->num_locals;
459 g_assert ((header != NULL) || (minfo->entry->num_locals == 0));
460 for (i = 0; i < minfo->entry->num_locals; i++) {
461 *var_table++ = jit->locals [i];
462 *type_table++ = write_type (mono_debugger_symbol_table, header->locals [i]);
466 mono_debugger_event (MONO_DEBUGGER_EVENT_METHOD_ADDED, NULL, 0);
469 static MonoDebuggerRangeInfo *
470 allocate_range_entry (MonoDebuggerSymbolFile *symfile)
472 MonoDebuggerRangeInfo *retval;
473 guint32 size, chunks;
475 symfile->range_entry_size = sizeof (MonoDebuggerRangeInfo);
477 if (!symfile->range_table) {
478 size = sizeof (MonoDebuggerRangeInfo) * RANGE_TABLE_CHUNK_SIZE;
479 symfile->range_table = g_malloc0 (size);
480 symfile->num_range_entries = 1;
481 return symfile->range_table;
484 if (!((symfile->num_range_entries + 1) % RANGE_TABLE_CHUNK_SIZE)) {
485 chunks = (symfile->num_range_entries + 1) / RANGE_TABLE_CHUNK_SIZE;
486 size = sizeof (MonoDebuggerRangeInfo) * RANGE_TABLE_CHUNK_SIZE * (chunks + 1);
488 symfile->range_table = g_realloc (symfile->range_table, size);
491 retval = symfile->range_table + symfile->num_range_entries;
492 symfile->num_range_entries++;
496 static MonoDebuggerClassInfo *
497 allocate_class_entry (MonoDebuggerSymbolFile *symfile)
499 MonoDebuggerClassInfo *retval;
500 guint32 size, chunks;
502 symfile->class_entry_size = sizeof (MonoDebuggerClassInfo);
504 if (!symfile->class_table) {
505 size = sizeof (MonoDebuggerClassInfo) * CLASS_TABLE_CHUNK_SIZE;
506 symfile->class_table = g_malloc0 (size);
507 symfile->num_class_entries = 1;
508 return symfile->class_table;
511 if (!((symfile->num_class_entries + 1) % CLASS_TABLE_CHUNK_SIZE)) {
512 chunks = (symfile->num_class_entries + 1) / CLASS_TABLE_CHUNK_SIZE;
513 size = sizeof (MonoDebuggerClassInfo) * CLASS_TABLE_CHUNK_SIZE * (chunks + 1);
515 symfile->class_table = g_realloc (symfile->class_table, size);
518 retval = symfile->class_table + symfile->num_class_entries;
519 symfile->num_class_entries++;
524 * Allocate a new entry of size `size' in the type table.
525 * Returns the global offset which is to be used to reference this type and
526 * a pointer (in the `ptr' argument) which is to be used to write the type.
529 allocate_type_entry (MonoDebuggerSymbolTable *table, guint32 size, guint8 **ptr)
534 g_assert (size + 4 < TYPE_TABLE_CHUNK_SIZE);
535 g_assert (ptr != NULL);
537 /* Initialize things if necessary. */
538 if (!table->current_type_table) {
539 table->current_type_table = g_malloc0 (TYPE_TABLE_CHUNK_SIZE);
540 table->type_table_size = TYPE_TABLE_CHUNK_SIZE;
541 table->type_table_chunk_size = TYPE_TABLE_CHUNK_SIZE;
542 table->type_table_offset = 1;
546 /* First let's check whether there's still enough room in the current_type_table. */
547 if (table->type_table_offset + size + 4 < table->type_table_size) {
548 retval = table->type_table_offset;
549 table->type_table_offset += size + 4;
550 data = ((guint8 *) table->current_type_table) + retval - table->type_table_start;
551 *(gint32 *) data = size;
552 data += sizeof(gint32);
557 /* Add the current_type_table to the type_tables vector and ... */
558 if (!table->type_tables) {
559 guint32 tsize = sizeof (gpointer) * TYPE_TABLE_PTR_CHUNK_SIZE;
560 table->type_tables = g_malloc0 (tsize);
563 if (!((table->num_type_tables + 1) % TYPE_TABLE_PTR_CHUNK_SIZE)) {
564 guint32 chunks = (table->num_type_tables + 1) / TYPE_TABLE_PTR_CHUNK_SIZE;
565 guint32 tsize = sizeof (gpointer) * TYPE_TABLE_PTR_CHUNK_SIZE * (chunks + 1);
567 table->type_tables = g_realloc (table->type_tables, tsize);
570 table->type_tables [table->num_type_tables++] = table->current_type_table;
572 /* .... allocate a new current_type_table. */
573 table->current_type_table = g_malloc0 (TYPE_TABLE_CHUNK_SIZE);
574 table->type_table_start = table->type_table_offset = table->type_table_size;
575 table->type_table_size += TYPE_TABLE_CHUNK_SIZE;
581 property_is_static (MonoProperty *prop)
589 return method->flags & METHOD_ATTRIBUTE_STATIC;
593 write_class (MonoDebuggerSymbolTable *table, MonoClass *klass)
595 guint8 buffer [BUFSIZ], *ptr = buffer, *old_ptr;
596 GPtrArray *methods = NULL, *static_methods = NULL;
597 int num_fields = 0, num_static_fields = 0, num_properties = 0, num_static_properties = 0;
598 int num_methods = 0, num_static_methods = 0, num_params = 0, num_static_params = 0, base_offset = 0;
599 guint32 size, data_size, offset;
600 GHashTable *method_slots = NULL;
603 g_assert (!klass->init_pending);
604 mono_class_init (klass);
606 offset = GPOINTER_TO_UINT (g_hash_table_lookup (class_table, klass));
610 if (klass->enumtype) {
611 offset = allocate_type_entry (table, 13, &ptr);
612 g_hash_table_insert (class_table, klass, GUINT_TO_POINTER (offset));
614 *ptr++ = MONO_DEBUGGER_TYPE_KIND_ENUM;
615 WRITE_UINT32 (ptr, klass->instance_size);
616 WRITE_UINT32 (ptr, table->builtin_types->enum_type->type_info);
617 WRITE_UINT32 (ptr, write_type (table, klass->enum_basetype));
621 for (i = 0; i < klass->field.count; i++)
622 if (!(klass->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
627 for (i = 0; i < klass->property.count; i++)
628 if (!property_is_static (&klass->properties [i]))
631 ++num_static_properties;
633 method_slots = g_hash_table_new (NULL, NULL);
634 methods = g_ptr_array_new ();
635 static_methods = g_ptr_array_new ();
637 for (i = 0; i < klass->method.count; i++) {
638 MonoMethod *method = klass->methods [i];
640 if (strcmp (method->name, ".ctor") == 0 || strcmp (method->name, ".cctor") == 0)
642 if (method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME)
644 if (!((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC))
646 if (g_hash_table_lookup (method_slots, GUINT_TO_POINTER (method->slot)))
648 g_hash_table_insert (method_slots, GUINT_TO_POINTER (method->slot), method);
650 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
651 ++num_static_methods;
652 num_static_params += method->signature->param_count;
653 g_ptr_array_add (static_methods, method);
656 num_params += method->signature->param_count;
657 g_ptr_array_add (methods, method);
661 g_hash_table_destroy (method_slots);
663 size = 58 + sizeof (gpointer) + num_fields * 8 + num_static_fields * 8 + num_properties * (4 + 2 * sizeof (gpointer)) +
664 num_static_properties * (4 + 2 * sizeof (gpointer)) + num_methods * (8 + sizeof (gpointer)) + num_params * 4 +
665 num_static_methods * (8 + sizeof (gpointer)) + num_static_params * 4;
669 offset = allocate_type_entry (table, data_size, &ptr);
672 g_hash_table_insert (class_table, klass, GUINT_TO_POINTER (offset));
674 *ptr++ = MONO_DEBUGGER_TYPE_KIND_CLASS_INFO;
676 if (klass->valuetype)
677 base_offset = - sizeof (MonoObject);
679 WRITE_UINT32 (ptr, klass->instance_size + base_offset);
680 *ptr++ = klass->valuetype;
681 WRITE_POINTER (ptr, klass);
682 WRITE_UINT32 (ptr, num_fields);
683 WRITE_UINT32 (ptr, num_fields * 8);
684 WRITE_UINT32 (ptr, num_static_fields);
685 WRITE_UINT32 (ptr, num_static_fields * 8);
686 WRITE_UINT32 (ptr, num_properties);
687 WRITE_UINT32 (ptr, num_properties * (4 + 2 * sizeof (gpointer)));
688 WRITE_UINT32 (ptr, num_static_properties);
689 WRITE_UINT32 (ptr, num_static_properties * (4 + 2 * sizeof (gpointer)));
690 WRITE_UINT32 (ptr, num_methods);
691 WRITE_UINT32 (ptr, num_methods * (4 + 2 * sizeof (gpointer)) + num_params * sizeof (gpointer));
692 WRITE_UINT32 (ptr, num_static_methods);
693 WRITE_UINT32 (ptr, num_static_methods * (4 + 2 * sizeof (gpointer)) + num_static_params * sizeof (gpointer));
694 for (i = 0; i < klass->field.count; i++) {
695 if (klass->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
698 WRITE_UINT32 (ptr, klass->fields [i].offset + base_offset);
699 WRITE_UINT32 (ptr, write_type (table, klass->fields [i].type));
702 for (i = 0; i < klass->property.count; i++) {
703 if (property_is_static (&klass->properties [i]))
706 if (klass->properties [i].get)
707 WRITE_UINT32 (ptr, write_type (table, klass->properties [i].get->signature->ret));
709 WRITE_UINT32 (ptr, 0);
710 WRITE_POINTER (ptr, klass->properties [i].get);
711 WRITE_POINTER (ptr, klass->properties [i].set);
714 for (i = 0; i < methods->len; i++) {
715 MonoMethod *method = g_ptr_array_index (methods, i);
718 WRITE_POINTER (ptr, method);
719 if ((method->signature->ret) && (method->signature->ret->type != MONO_TYPE_VOID))
720 WRITE_UINT32 (ptr, write_type (table, method->signature->ret));
722 WRITE_UINT32 (ptr, 0);
723 WRITE_UINT32 (ptr, method->signature->param_count);
724 for (j = 0; j < method->signature->param_count; j++)
725 WRITE_UINT32 (ptr, write_type (table, method->signature->params [j]));
728 g_ptr_array_free (methods, FALSE);
730 for (i = 0; i < klass->field.count; i++) {
731 if (!(klass->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
734 WRITE_UINT32 (ptr, klass->fields [i].offset);
735 WRITE_UINT32 (ptr, write_type (table, klass->fields [i].type));
738 for (i = 0; i < klass->property.count; i++) {
739 if (!property_is_static (&klass->properties [i]))
742 if (klass->properties [i].get)
743 WRITE_UINT32 (ptr, write_type (table, klass->properties [i].get->signature->ret));
745 WRITE_UINT32 (ptr, 0);
746 WRITE_POINTER (ptr, klass->properties [i].get);
747 WRITE_POINTER (ptr, klass->properties [i].set);
750 for (i = 0; i < static_methods->len; i++) {
751 MonoMethod *method = g_ptr_array_index (static_methods, i);
754 WRITE_POINTER (ptr, method);
755 if ((method->signature->ret) && (method->signature->ret->type != MONO_TYPE_VOID))
756 WRITE_UINT32 (ptr, write_type (table, method->signature->ret));
758 WRITE_UINT32 (ptr, 0);
759 WRITE_UINT32 (ptr, method->signature->param_count);
760 for (j = 0; j < method->signature->param_count; j++)
761 WRITE_UINT32 (ptr, write_type (table, method->signature->params [j]));
764 if (klass->parent && (klass->parent != mono_defaults.object_class))
765 WRITE_UINT32 (ptr, write_class (table, klass->parent));
767 WRITE_UINT32 (ptr, 0);
769 if (ptr - old_ptr != data_size) {
770 g_warning (G_STRLOC ": %d,%d,%d", ptr - old_ptr, data_size, sizeof (gpointer));
772 g_warning (G_STRLOC ": %s.%s", klass->name_space, klass->name);
773 g_assert_not_reached ();
780 * Adds type `type' to the type table and returns its offset.
783 write_type (MonoDebuggerSymbolTable *table, MonoType *type)
785 guint8 buffer [BUFSIZ], *ptr = buffer;
786 guint32 size, offset;
789 offset = GPOINTER_TO_UINT (g_hash_table_lookup (type_table, type));
793 klass = mono_class_from_mono_type (type);
794 if (klass->init_pending)
796 mono_class_init (klass);
798 switch (type->type) {
800 return table->builtin_types->void_type->type_info;
802 case MONO_TYPE_BOOLEAN:
803 return table->builtin_types->boolean_type->type_info;
806 return table->builtin_types->sbyte_type->type_info;
809 return table->builtin_types->byte_type->type_info;
812 return table->builtin_types->char_type->type_info;
815 return table->builtin_types->int16_type->type_info;
818 return table->builtin_types->uint16_type->type_info;
821 return table->builtin_types->int32_type->type_info;
824 return table->builtin_types->uint32_type->type_info;
827 return table->builtin_types->int64_type->type_info;
830 return table->builtin_types->uint64_type->type_info;
833 return table->builtin_types->single_type->type_info;
836 return table->builtin_types->double_type->type_info;
838 case MONO_TYPE_STRING:
839 return table->builtin_types->string_type->type_info;
842 return table->builtin_types->int_type->type_info;
845 return table->builtin_types->uint_type->type_info;
847 case MONO_TYPE_SZARRAY: {
850 *ptr++ = MONO_DEBUGGER_TYPE_KIND_SZARRAY;
851 WRITE_UINT32 (ptr, sizeof (MonoArray));
852 g_assert (table->builtin_types->array_type->type_info != 0);
853 WRITE_UINT32 (ptr, table->builtin_types->array_type->type_info);
854 *ptr++ = (guint8*)&array.max_length - (guint8*)&array;
855 *ptr++ = sizeof (array.max_length);
856 *ptr++ = (guint8*)&array.vector - (guint8*)&array;
857 WRITE_UINT32 (ptr, write_type (table, &type->data.klass->byval_arg));
861 case MONO_TYPE_ARRAY: {
863 MonoArrayBounds bounds;
865 *ptr++ = MONO_DEBUGGER_TYPE_KIND_ARRAY;
866 WRITE_UINT32 (ptr, sizeof (MonoArray));
867 g_assert (table->builtin_types->array_type->type_info != 0);
868 WRITE_UINT32 (ptr, table->builtin_types->array_type->type_info);
869 *ptr++ = (guint8*)&array.max_length - (guint8*)&array;
870 *ptr++ = sizeof (array.max_length);
871 *ptr++ = (guint8*)&array.vector - (guint8*)&array;
872 *ptr++ = klass->rank;
873 *ptr++ = (guint8*)&array.bounds - (guint8*)&array;
874 *ptr++ = sizeof (MonoArrayBounds);
875 *ptr++ = (guint8*)&bounds.lower_bound - (guint8*)&bounds;
876 *ptr++ = sizeof (bounds.lower_bound);
877 *ptr++ = (guint8*)&bounds.length - (guint8*)&bounds;
878 *ptr++ = sizeof (bounds.length);
879 WRITE_UINT32 (ptr, write_type (table, &type->data.array->eklass->byval_arg));
883 case MONO_TYPE_VALUETYPE:
884 case MONO_TYPE_CLASS:
885 case MONO_TYPE_OBJECT:
886 return write_class (table, klass);
889 *ptr++ = MONO_DEBUGGER_TYPE_KIND_POINTER;
890 WRITE_UINT32 (ptr, sizeof (gpointer));
891 WRITE_UINT32 (ptr, write_type (table, type->data.type));
895 g_message (G_STRLOC ": %s.%s - %p - %d", klass->name_space, klass->name, klass, type->type);
896 *ptr++ = MONO_DEBUGGER_TYPE_KIND_UNKNOWN;
897 WRITE_UINT32 (ptr, klass->instance_size);
898 WRITE_UINT32 (ptr, write_class (table, klass));
903 offset = allocate_type_entry (mono_debugger_symbol_table, size, &ptr);
904 memcpy (ptr, buffer, size);
909 MonoReflectionMethod *
910 ves_icall_MonoDebugger_GetMethod (MonoReflectionAssembly *assembly, guint32 token)
914 method = mono_get_method (assembly->assembly->image, token, NULL);
916 return mono_method_get_object (mono_domain_get (), method, NULL);
920 ves_icall_MonoDebugger_GetMethodToken (MonoReflectionAssembly *assembly, MonoReflectionMethod *method)
922 return method->method->token;
926 ves_icall_MonoDebugger_GetType (MonoReflectionAssembly *assembly, guint32 token)
930 klass = mono_class_get (assembly->assembly->image, token);
932 g_warning (G_STRLOC ": %x", token);
936 return mono_type_get_object (mono_domain_get (), &klass->byval_arg);
940 ves_icall_MonoDebugger_GetLocalTypeFromSignature (MonoReflectionAssembly *assembly, MonoArray *signature)
948 MONO_CHECK_ARG_NULL (assembly);
949 MONO_CHECK_ARG_NULL (signature);
951 domain = mono_domain_get();
952 image = assembly->assembly->image;
954 ptr = mono_array_addr (signature, char, 0);
955 g_assert (*ptr++ == 0x07);
956 len = mono_metadata_decode_value (ptr, &ptr);
959 type = mono_metadata_parse_type (image, MONO_PARSE_LOCAL, 0, ptr, &ptr);
961 return mono_type_get_object (domain, type);
965 mono_debugger_event (MonoDebuggerEvent event, gpointer data, guint32 arg)
967 if (mono_debugger_event_handler)
968 (* mono_debugger_event_handler) (event, data, arg);
972 mono_debugger_cleanup (void)
974 /* Do nothing yet. */
978 * Debugger breakpoint interface.
980 * This interface is used to insert breakpoints on methods which are not yet JITed.
981 * The debugging code keeps a list of all such breakpoints and automatically inserts the
982 * breakpoint when the method is JITed.
985 static GPtrArray *breakpoints = NULL;
988 mono_debugger_insert_breakpoint_full (MonoMethodDesc *desc)
990 static int last_breakpoint_id = 0;
991 MonoDebuggerBreakpointInfo *info;
993 info = g_new0 (MonoDebuggerBreakpointInfo, 1);
995 info->index = ++last_breakpoint_id;
998 breakpoints = g_ptr_array_new ();
1000 g_ptr_array_add (breakpoints, info);
1006 mono_debugger_remove_breakpoint (int breakpoint_id)
1013 for (i = 0; i < breakpoints->len; i++) {
1014 MonoDebuggerBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
1016 if (info->index != breakpoint_id)
1019 mono_method_desc_free (info->desc);
1020 g_ptr_array_remove (breakpoints, info);
1029 mono_debugger_insert_breakpoint (const gchar *method_name, gboolean include_namespace)
1031 MonoMethodDesc *desc;
1033 desc = mono_method_desc_new (method_name, include_namespace);
1037 return mono_debugger_insert_breakpoint_full (desc);
1041 mono_debugger_method_has_breakpoint (MonoMethod *method)
1045 if (!breakpoints || (method->wrapper_type != MONO_WRAPPER_NONE))
1048 for (i = 0; i < breakpoints->len; i++) {
1049 MonoDebuggerBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
1051 if (!mono_method_desc_full_match (info->desc, method))
1061 mono_debugger_breakpoint_callback (MonoMethod *method, guint32 index)
1063 mono_debugger_event (MONO_DEBUGGER_EVENT_BREAKPOINT, method, index);
1067 get_exception_message (MonoObject *exc)
1069 char *message = NULL;
1075 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
1076 klass = exc->vtable->klass;
1078 while (klass && method == NULL) {
1079 for (i = 0; i < klass->method.count; ++i) {
1080 method = klass->methods [i];
1081 if (!strcmp ("ToString", method->name) &&
1082 method->signature->param_count == 0 &&
1083 method->flags & METHOD_ATTRIBUTE_VIRTUAL &&
1084 method->flags & METHOD_ATTRIBUTE_PUBLIC) {
1091 klass = klass->parent;
1096 str = (MonoString *) mono_runtime_invoke (method, exc, NULL, NULL);
1098 message = mono_string_to_utf8 (str);
1105 mono_debugger_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
1110 if (method->klass->valuetype && (obj != NULL))
1111 obj = mono_value_box (mono_domain_get (), method->klass, obj);
1113 retval = mono_runtime_invoke (method, obj, params, exc);
1117 message = get_exception_message (*exc);
1119 *exc = (MonoObject *) mono_string_new_wrapper (message);
1127 mono_debugger_lookup_type (const gchar *type_name)
1131 mono_debugger_lock ();
1133 for (i = 0; i < mono_debugger_symbol_table->num_symbol_files; i++) {
1134 MonoDebuggerSymbolFile *symfile = mono_debugger_symbol_table->symbol_files [i];
1138 type = mono_reflection_type_from_name (type_name, symfile->image);
1142 offset = write_type (mono_debugger_symbol_table, type);
1144 mono_debugger_unlock ();
1148 mono_debugger_unlock ();