5 * Mono Project (http://www.mono-project.com)
7 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
8 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
12 #include <mono/metadata/assembly.h>
13 #include <mono/metadata/tabledefs.h>
14 #include <mono/metadata/tokentype.h>
15 #include <mono/metadata/appdomain.h>
16 #include <mono/metadata/class-internals.h>
17 #include <mono/metadata/mono-debug.h>
18 #include <mono/metadata/mono-debug-debugger.h>
19 #include <mono/metadata/mono-endian.h>
22 #define DATA_TABLE_CHUNK_SIZE 16384
24 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
26 #if NO_UNALIGNED_ACCESS
27 #define RETURN_UNALIGNED(type, addr) \
30 memcpy(&val, p + offset, sizeof(val)); \
33 #define WRITE_UNALIGNED(type, addr, val) \
34 memcpy(addr, &val, sizeof(type))
35 #define READ_UNALIGNED(type, addr, val) \
36 memcpy(&val, addr, sizeof(type))
38 #define RETURN_UNALIGNED(type, addr) \
39 return *(type*)(p + offset);
40 #define WRITE_UNALIGNED(type, addr, val) \
41 (*(type *)(addr) = (val))
42 #define READ_UNALIGNED(type, addr, val) \
43 val = (*(type *)(addr))
47 MONO_DEBUG_DATA_ITEM_UNKNOWN = 0,
48 MONO_DEBUG_DATA_ITEM_CLASS,
49 MONO_DEBUG_DATA_ITEM_METHOD,
50 MONO_DEBUG_DATA_ITEM_DELEGATE_TRAMPOLINE
51 } MonoDebugDataItemType;
53 typedef struct _MonoDebugDataChunk MonoDebugDataChunk;
55 struct _MonoDebugDataChunk {
57 guint32 allocated_size;
58 guint32 current_offset;
60 MonoDebugDataChunk *next;
61 guint8 data [MONO_ZERO_LEN_ARRAY];
64 struct _MonoDebugDataTable {
66 gint32 _dummy; /* alignment for next field. */
67 MonoDebugDataChunk *first_chunk;
68 MonoDebugDataChunk *current_chunk;
69 GHashTable *method_hash;
70 GHashTable *method_address_hash;
74 const gchar *method_name;
75 const gchar *obsolete_cil_code;
77 } MonoDebugWrapperData;
84 MonoDebugWrapperData *wrapper_data;
87 } MonoDebugMethodHeader;
89 struct _MonoDebugMethodAddress {
90 MonoDebugMethodHeader header;
91 const guint8 *code_start;
92 const guint8 *wrapper_addr;
94 guint8 data [MONO_ZERO_LEN_ARRAY];
97 struct _MonoDebugClassEntry {
99 guint8 data [MONO_ZERO_LEN_ARRAY];
105 } MonoDebugDelegateTrampolineEntry;
107 MonoSymbolTable *mono_symbol_table = NULL;
108 MonoDebugFormat mono_debug_format = MONO_DEBUG_FORMAT_NONE;
109 gint32 mono_debug_debugger_version = 5;
110 gint32 _mono_debug_using_mono_debugger = 0;
112 static gboolean mono_debug_initialized = FALSE;
113 static GHashTable *mono_debug_handles = NULL;
115 static GHashTable *data_table_hash = NULL;
116 static int next_symbol_file_id = 0;
118 static MonoDebugHandle *mono_debug_open_image (MonoImage *image, const guint8 *raw_contents, int size);
120 static MonoDebugHandle *_mono_debug_get_image (MonoImage *image);
121 static void mono_debug_add_assembly (MonoAssembly *assembly,
123 static void mono_debug_add_type (MonoClass *klass);
125 void _mono_debug_init_corlib (MonoDomain *domain);
127 extern void (*mono_debugger_class_init_func) (MonoClass *klass);
128 extern void (*mono_debugger_class_loaded_methods_func) (MonoClass *klass);
130 static MonoDebugDataTable *
131 create_data_table (MonoDomain *domain)
133 MonoDebugDataTable *table;
134 MonoDebugDataChunk *chunk;
136 table = g_new0 (MonoDebugDataTable, 1);
137 table->domain = domain ? mono_domain_get_id (domain) : -1;
139 table->method_address_hash = g_hash_table_new (NULL, NULL);
140 table->method_hash = g_hash_table_new (NULL, NULL);
142 chunk = g_malloc0 (sizeof (MonoDebugDataChunk) + DATA_TABLE_CHUNK_SIZE);
143 chunk->total_size = DATA_TABLE_CHUNK_SIZE;
145 table->first_chunk = table->current_chunk = chunk;
148 mono_debug_list_add (&mono_symbol_table->data_tables, table);
149 g_hash_table_insert (data_table_hash, domain, table);
156 free_header_data (gpointer key, gpointer value, gpointer user_data)
158 MonoDebugMethodHeader *header = (MonoDebugMethodHeader*)value;
160 if (header->wrapper_data) {
161 g_free ((gpointer)header->wrapper_data->method_name);
162 g_slist_free (header->address_list);
163 g_free (header->wrapper_data);
168 free_data_table (MonoDebugDataTable *table)
170 MonoDebugDataChunk *chunk, *next_chunk;
172 g_hash_table_foreach (table->method_hash, free_header_data, NULL);
173 g_hash_table_destroy (table->method_hash);
174 g_hash_table_destroy (table->method_address_hash);
176 table->method_hash = NULL;
177 table->method_address_hash = NULL;
179 chunk = table->first_chunk;
181 next_chunk = chunk->next;
186 table->first_chunk = table->current_chunk = NULL;
187 mono_debug_list_remove (&mono_symbol_table->data_tables, table);
191 static MonoDebugDataTable *
192 lookup_data_table (MonoDomain *domain)
194 MonoDebugDataTable *table;
196 table = g_hash_table_lookup (data_table_hash, domain);
202 free_debug_handle (MonoDebugHandle *handle)
205 mono_debug_close_mono_symbol_file (handle->symfile);
206 /* decrease the refcount added with mono_image_addref () */
207 free_data_table (handle->type_table);
208 mono_image_close (handle->image);
209 g_free (handle->image_file);
214 * Initialize debugging support.
216 * This method must be called after loading corlib,
217 * but before opening the application's main assembly because we need to set some
221 mono_debug_init (MonoDebugFormat format)
223 g_assert (!mono_debug_initialized);
225 if (_mono_debug_using_mono_debugger)
226 format = MONO_DEBUG_FORMAT_DEBUGGER;
228 mono_debug_initialized = TRUE;
229 mono_debug_format = format;
232 * This must be called before mono_debugger_initialize(), because the
233 * latter registers GC roots.
235 mono_gc_base_init ();
237 mono_debugger_initialize (_mono_debug_using_mono_debugger);
239 mono_debugger_lock ();
241 mono_symbol_table = g_new0 (MonoSymbolTable, 1);
242 mono_symbol_table->magic = MONO_DEBUGGER_MAGIC;
243 mono_symbol_table->version = MONO_DEBUGGER_MAJOR_VERSION;
244 mono_symbol_table->total_size = sizeof (MonoSymbolTable);
246 mono_debug_handles = g_hash_table_new_full
247 (NULL, NULL, NULL, (GDestroyNotify) free_debug_handle);
249 data_table_hash = g_hash_table_new_full (
250 NULL, NULL, NULL, (GDestroyNotify) free_data_table);
252 mono_debugger_class_init_func = mono_debug_add_type;
253 mono_debugger_class_loaded_methods_func = mono_debugger_class_initialized;
254 mono_install_assembly_load_hook (mono_debug_add_assembly, NULL);
256 mono_symbol_table->global_data_table = create_data_table (NULL);
258 mono_debugger_unlock ();
262 * INTERNAL USE ONLY !
265 _mono_debug_init_corlib (MonoDomain *domain)
267 if (!mono_debug_initialized)
270 mono_symbol_table->corlib = mono_debug_open_image (mono_defaults.corlib, NULL, 0);
271 mono_debugger_event (MONO_DEBUGGER_EVENT_INITIALIZE_CORLIB,
272 (guint64) (gsize) mono_symbol_table->corlib, 0);
276 mono_debug_open_image_from_memory (MonoImage *image, const guint8 *raw_contents, int size)
278 mono_debug_open_image (image, raw_contents, size);
283 mono_debug_using_mono_debugger (void)
285 return _mono_debug_using_mono_debugger;
289 mono_debug_cleanup (void)
291 if (mono_debug_handles)
292 g_hash_table_destroy (mono_debug_handles);
293 mono_debug_handles = NULL;
295 if (data_table_hash) {
296 g_hash_table_destroy (data_table_hash);
297 data_table_hash = NULL;
300 if (mono_symbol_table) {
301 if (mono_symbol_table->global_data_table)
302 free_data_table (mono_symbol_table->global_data_table);
304 g_free (mono_symbol_table);
305 mono_symbol_table = NULL;
310 mono_debug_domain_create (MonoDomain *domain)
312 MonoDebugDataTable *table;
314 if (!mono_debug_initialized)
317 mono_debugger_lock ();
319 table = create_data_table (domain);
321 mono_debugger_event (MONO_DEBUGGER_EVENT_DOMAIN_CREATE, (guint64) (gsize) table,
322 mono_domain_get_id (domain));
324 mono_debugger_unlock ();
328 mono_debug_domain_unload (MonoDomain *domain)
330 MonoDebugDataTable *table;
332 if (!mono_debug_initialized)
335 mono_debugger_lock ();
337 table = g_hash_table_lookup (data_table_hash, domain);
339 g_warning (G_STRLOC ": unloading unknown domain %p / %d",
340 domain, mono_domain_get_id (domain));
341 mono_debugger_unlock ();
345 mono_debugger_event (MONO_DEBUGGER_EVENT_DOMAIN_UNLOAD, (guint64) (gsize) table,
346 mono_domain_get_id (domain));
348 g_hash_table_remove (data_table_hash, domain);
350 mono_debugger_unlock ();
354 * LOCKING: Assumes the debug lock is held.
356 static MonoDebugHandle *
357 _mono_debug_get_image (MonoImage *image)
359 return g_hash_table_lookup (mono_debug_handles, image);
363 mono_debug_close_image (MonoImage *image)
365 MonoDebugHandle *handle;
367 if (!mono_debug_initialized)
370 mono_debugger_lock ();
372 handle = _mono_debug_get_image (image);
374 mono_debugger_unlock ();
378 mono_debugger_event (MONO_DEBUGGER_EVENT_UNLOAD_MODULE, (guint64) (gsize) handle,
381 mono_debug_list_remove (&mono_symbol_table->symbol_files, handle);
382 g_hash_table_remove (mono_debug_handles, image);
384 mono_debugger_unlock ();
387 static MonoDebugHandle *
388 mono_debug_open_image (MonoImage *image, const guint8 *raw_contents, int size)
390 MonoDebugHandle *handle;
392 if (mono_image_is_dynamic (image))
395 mono_debugger_lock ();
397 handle = _mono_debug_get_image (image);
398 if (handle != NULL) {
399 mono_debugger_unlock ();
403 handle = g_new0 (MonoDebugHandle, 1);
404 handle->index = ++next_symbol_file_id;
406 handle->image = image;
407 mono_image_addref (image);
408 handle->image_file = g_strdup (mono_image_get_filename (image));
410 handle->type_table = create_data_table (NULL);
412 handle->symfile = mono_debug_open_mono_symbols (
413 handle, raw_contents, size, _mono_debug_using_mono_debugger);
415 mono_debug_list_add (&mono_symbol_table->symbol_files, handle);
417 g_hash_table_insert (mono_debug_handles, image, handle);
419 if (mono_symbol_table->corlib)
420 mono_debugger_event (MONO_DEBUGGER_EVENT_LOAD_MODULE,
421 (guint64) (gsize) handle, 0);
423 mono_debugger_unlock ();
429 mono_debug_add_assembly (MonoAssembly *assembly, gpointer user_data)
431 mono_debugger_lock ();
432 mono_debug_open_image (mono_assembly_get_image (assembly), NULL, 0);
433 mono_debugger_unlock ();
437 allocate_data_item (MonoDebugDataTable *table, MonoDebugDataItemType type, guint32 size)
442 size = ALIGN_TO (size, sizeof (gpointer));
444 if (size + 16 < DATA_TABLE_CHUNK_SIZE)
445 chunk_size = DATA_TABLE_CHUNK_SIZE;
447 chunk_size = size + 16;
449 g_assert (table->current_chunk->current_offset == table->current_chunk->allocated_size);
451 if (table->current_chunk->allocated_size + size + 8 >= table->current_chunk->total_size) {
452 MonoDebugDataChunk *new_chunk;
454 new_chunk = g_malloc0 (sizeof (MonoDebugDataChunk) + chunk_size);
455 new_chunk->total_size = chunk_size;
457 table->current_chunk->next = new_chunk;
458 table->current_chunk = new_chunk;
461 data = &table->current_chunk->data [table->current_chunk->allocated_size];
462 table->current_chunk->allocated_size += size + 8;
464 * ((guint32 *) data) = size;
466 * ((guint32 *) data) = type;
472 write_data_item (MonoDebugDataTable *table, const guint8 *data)
474 MonoDebugDataChunk *current_chunk = table->current_chunk;
475 guint32 size = * ((guint32 *) (data - 8));
477 g_assert (current_chunk->current_offset + size + 8 == current_chunk->allocated_size);
478 current_chunk->current_offset = current_chunk->allocated_size;
481 struct LookupMethodData
483 MonoDebugMethodInfo *minfo;
488 lookup_method_func (gpointer key, gpointer value, gpointer user_data)
490 MonoDebugHandle *handle = (MonoDebugHandle *) value;
491 struct LookupMethodData *data = (struct LookupMethodData *) user_data;
497 data->minfo = mono_debug_symfile_lookup_method (handle, data->method);
500 static MonoDebugMethodInfo *
501 _mono_debug_lookup_method (MonoMethod *method)
503 struct LookupMethodData data;
506 data.method = method;
508 if (!mono_debug_handles)
511 g_hash_table_foreach (mono_debug_handles, lookup_method_func, &data);
516 * mono_debug_lookup_method:
518 * Lookup symbol file information for the method @method. The returned
519 * `MonoDebugMethodInfo' is a private structure, but it can be passed to
520 * mono_debug_symfile_lookup_location().
522 MonoDebugMethodInfo *
523 mono_debug_lookup_method (MonoMethod *method)
525 MonoDebugMethodInfo *minfo;
527 mono_debugger_lock ();
528 minfo = _mono_debug_lookup_method (method);
529 mono_debugger_unlock ();
534 write_leb128 (guint32 value, guint8 *ptr, guint8 **rptr)
537 guint8 byte = value & 0x7f;
548 write_sleb128 (gint32 value, guint8 *ptr, guint8 **rptr)
553 guint8 byte = value & 0x7f;
556 if (((value == 0) && ((byte & 0x40) == 0)) || ((value == -1) && (byte & 0x40)))
567 write_variable (MonoDebugVarInfo *var, guint8 *ptr, guint8 **rptr)
569 write_leb128 (var->index, ptr, &ptr);
570 write_sleb128 (var->offset, ptr, &ptr);
571 write_leb128 (var->size, ptr, &ptr);
572 write_leb128 (var->begin_scope, ptr, &ptr);
573 write_leb128 (var->end_scope, ptr, &ptr);
574 WRITE_UNALIGNED (gpointer, ptr, var->type);
575 ptr += sizeof (gpointer);
579 MonoDebugMethodAddress *
580 mono_debug_add_method (MonoMethod *method, MonoDebugMethodJitInfo *jit, MonoDomain *domain)
582 MonoMethod *declaring;
583 MonoDebugDataTable *table;
584 MonoDebugMethodHeader *header;
585 MonoDebugMethodAddress *address;
586 MonoDebugMethodInfo *minfo;
587 MonoDebugHandle *handle;
588 guint8 buffer [BUFSIZ];
589 guint8 *ptr, *oldptr;
590 guint32 i, size, total_size, max_size;
591 gboolean is_wrapper = FALSE;
593 mono_debugger_lock ();
595 table = lookup_data_table (domain);
597 handle = _mono_debug_get_image (method->klass->image);
598 minfo = _mono_debug_lookup_method (method);
600 if (!minfo || (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
601 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
602 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
603 (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
604 (method->wrapper_type != MONO_WRAPPER_NONE)) {
608 max_size = (5 * 5) + 1 + (10 * jit->num_line_numbers) +
609 (25 + sizeof (gpointer)) * (1 + jit->num_params + jit->num_locals);
611 if (max_size > BUFSIZ)
612 ptr = oldptr = g_malloc (max_size);
614 ptr = oldptr = buffer;
616 write_leb128 (jit->prologue_end, ptr, &ptr);
617 write_leb128 (jit->epilogue_begin, ptr, &ptr);
619 write_leb128 (jit->num_line_numbers, ptr, &ptr);
620 for (i = 0; i < jit->num_line_numbers; i++) {
621 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
623 write_sleb128 (lne->il_offset, ptr, &ptr);
624 write_sleb128 (lne->native_offset, ptr, &ptr);
627 *ptr++ = jit->this_var ? 1 : 0;
629 write_variable (jit->this_var, ptr, &ptr);
631 write_leb128 (jit->num_params, ptr, &ptr);
632 for (i = 0; i < jit->num_params; i++)
633 write_variable (&jit->params [i], ptr, &ptr);
635 write_leb128 (jit->num_locals, ptr, &ptr);
636 for (i = 0; i < jit->num_locals; i++)
637 write_variable (&jit->locals [i], ptr, &ptr);
640 g_assert (size < max_size);
641 total_size = size + sizeof (MonoDebugMethodAddress);
643 address = (MonoDebugMethodAddress *) allocate_data_item (
644 table, MONO_DEBUG_DATA_ITEM_METHOD, total_size);
646 address->header.size = total_size;
647 address->header.symfile_id = handle ? handle->index : 0;
648 address->header.domain_id = mono_domain_get_id (domain);
649 address->header.method_id = is_wrapper ? 0 : minfo->index;
650 address->header.method = method;
652 address->code_start = jit->code_start;
653 address->code_size = jit->code_size;
655 memcpy (&address->data, oldptr, size);
656 if (max_size > BUFSIZ)
659 declaring = method->is_inflated ? ((MonoMethodInflated *) method)->declaring : method;
660 header = g_hash_table_lookup (table->method_hash, declaring);
663 header = &address->header;
664 g_hash_table_insert (table->method_hash, declaring, header);
667 MonoDebugWrapperData *wrapper;
669 header->wrapper_data = wrapper = g_new0 (MonoDebugWrapperData, 1);
671 wrapper->wrapper_type = method->wrapper_type;
672 wrapper->method_name = mono_method_full_name (declaring, TRUE);
673 wrapper->obsolete_cil_code = "";
676 address->header.wrapper_data = header->wrapper_data;
677 header->address_list = g_slist_prepend (header->address_list, address);
680 g_hash_table_insert (table->method_address_hash, method, address);
682 write_data_item (table, (guint8 *) address);
684 mono_debugger_unlock ();
689 mono_debug_add_delegate_trampoline (gpointer code, int size)
691 MonoDebugDelegateTrampolineEntry *entry;
693 if (!mono_debug_initialized)
696 mono_debugger_lock ();
698 entry = (MonoDebugDelegateTrampolineEntry *) allocate_data_item (
699 mono_symbol_table->global_data_table, MONO_DEBUG_DATA_ITEM_DELEGATE_TRAMPOLINE,
700 sizeof (MonoDebugDelegateTrampolineEntry));
704 write_data_item (mono_symbol_table->global_data_table, (guint8 *) entry);
706 mono_debugger_unlock ();
709 static inline guint32
710 read_leb128 (guint8 *ptr, guint8 **rptr)
712 guint32 result = 0, shift = 0;
715 guint8 byte = *ptr++;
717 result |= (byte & 0x7f) << shift;
718 if ((byte & 0x80) == 0)
728 read_sleb128 (guint8 *ptr, guint8 **rptr)
734 guint8 byte = *ptr++;
736 result |= (byte & 0x7f) << shift;
742 if ((shift < 32) && (byte & 0x40))
743 result |= - (1 << shift);
752 read_variable (MonoDebugVarInfo *var, guint8 *ptr, guint8 **rptr)
754 var->index = read_leb128 (ptr, &ptr);
755 var->offset = read_sleb128 (ptr, &ptr);
756 var->size = read_leb128 (ptr, &ptr);
757 var->begin_scope = read_leb128 (ptr, &ptr);
758 var->end_scope = read_leb128 (ptr, &ptr);
759 READ_UNALIGNED (gpointer, ptr, var->type);
760 ptr += sizeof (gpointer);
765 mono_debug_free_method_jit_info (MonoDebugMethodJitInfo *jit)
769 g_free (jit->line_numbers);
770 g_free (jit->this_var);
771 g_free (jit->params);
772 g_free (jit->locals);
776 static MonoDebugMethodJitInfo *
777 mono_debug_read_method (MonoDebugMethodAddress *address)
779 MonoDebugMethodJitInfo *jit;
783 jit = g_new0 (MonoDebugMethodJitInfo, 1);
784 jit->code_start = address->code_start;
785 jit->code_size = address->code_size;
786 jit->wrapper_addr = address->wrapper_addr;
788 ptr = (guint8 *) &address->data;
790 jit->prologue_end = read_leb128 (ptr, &ptr);
791 jit->epilogue_begin = read_leb128 (ptr, &ptr);
793 jit->num_line_numbers = read_leb128 (ptr, &ptr);
794 jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
795 for (i = 0; i < jit->num_line_numbers; i++) {
796 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
798 lne->il_offset = read_sleb128 (ptr, &ptr);
799 lne->native_offset = read_sleb128 (ptr, &ptr);
803 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
804 read_variable (jit->this_var, ptr, &ptr);
807 jit->num_params = read_leb128 (ptr, &ptr);
808 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
809 for (i = 0; i < jit->num_params; i++)
810 read_variable (&jit->params [i], ptr, &ptr);
812 jit->num_locals = read_leb128 (ptr, &ptr);
813 jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
814 for (i = 0; i < jit->num_locals; i++)
815 read_variable (&jit->locals [i], ptr, &ptr);
821 mono_debug_add_type (MonoClass *klass)
823 MonoDebugHandle *handle;
824 MonoDebugClassEntry *entry;
825 guint8 buffer [BUFSIZ];
826 guint8 *ptr, *oldptr;
827 guint32 size, total_size, max_size;
830 if (klass->generic_class || klass->rank ||
831 (klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR))
834 mono_debugger_lock ();
836 handle = _mono_debug_get_image (klass->image);
838 mono_debugger_unlock ();
842 max_size = 12 + sizeof (gpointer);
843 if (max_size > BUFSIZ)
844 ptr = oldptr = g_malloc (max_size);
846 ptr = oldptr = buffer;
848 if (klass->valuetype)
849 base_offset = - (int)(sizeof (MonoObject));
851 write_leb128 (klass->type_token, ptr, &ptr);
852 write_leb128 (klass->instance_size + base_offset, ptr, &ptr);
853 WRITE_UNALIGNED (gpointer, ptr, klass);
854 ptr += sizeof (gpointer);
857 g_assert (size < max_size);
858 total_size = size + sizeof (MonoDebugClassEntry);
860 g_assert (total_size + 9 < DATA_TABLE_CHUNK_SIZE);
862 entry = (MonoDebugClassEntry *) allocate_data_item (
863 handle->type_table, MONO_DEBUG_DATA_ITEM_CLASS, total_size);
865 entry->size = total_size;
867 memcpy (&entry->data, oldptr, size);
869 write_data_item (handle->type_table, (guint8 *) entry);
871 if (max_size > BUFSIZ)
874 mono_debugger_unlock ();
877 static MonoDebugMethodJitInfo *
878 find_method (MonoMethod *method, MonoDomain *domain)
880 MonoDebugDataTable *table;
881 MonoDebugMethodAddress *address;
883 table = lookup_data_table (domain);
884 address = g_hash_table_lookup (table->method_address_hash, method);
889 return mono_debug_read_method (address);
892 MonoDebugMethodJitInfo *
893 mono_debug_find_method (MonoMethod *method, MonoDomain *domain)
895 MonoDebugMethodJitInfo *res;
897 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
900 mono_debugger_lock ();
901 res = find_method (method, domain);
902 mono_debugger_unlock ();
906 struct LookupMethodAddressData
909 MonoDebugMethodHeader *result;
913 lookup_method_address_func (gpointer key, gpointer value, gpointer user_data)
915 MonoDebugDataTable *table = (MonoDebugDataTable *) value;
916 struct LookupMethodAddressData *data = (struct LookupMethodAddressData *) user_data;
917 MonoDebugMethodHeader *header;
919 header = g_hash_table_lookup (table->method_hash, data->method);
921 data->result = header;
924 MonoDebugMethodAddressList *
925 mono_debug_lookup_method_addresses (MonoMethod *method)
927 MonoDebugMethodAddressList *info;
928 MonoDebugMethodHeader *header = NULL;
929 struct LookupMethodAddressData data;
930 MonoMethod *declaring;
935 g_assert ((mono_debug_debugger_version == 4) || (mono_debug_debugger_version == 5));
937 mono_debugger_lock ();
939 declaring = method->is_inflated ? ((MonoMethodInflated *) method)->declaring : method;
941 data.method = declaring;
944 g_hash_table_foreach (data_table_hash, lookup_method_address_func, &data);
945 header = data.result;
948 mono_debugger_unlock ();
952 count = g_slist_length (header->address_list) + 1;
953 size = sizeof (MonoDebugMethodAddressList) + count * sizeof (gpointer);
955 info = g_malloc0 (size);
961 WRITE_UNALIGNED (gpointer, ptr, header);
962 ptr += sizeof (gpointer);
964 for (list = header->address_list; list; list = list->next) {
965 WRITE_UNALIGNED (gpointer, ptr, list->data);
966 ptr += sizeof (gpointer);
969 mono_debugger_unlock ();
974 il_offset_from_address (MonoMethod *method, MonoDomain *domain, guint32 native_offset)
976 MonoDebugMethodJitInfo *jit;
979 jit = find_method (method, domain);
980 if (!jit || !jit->line_numbers)
981 goto cleanup_and_fail;
983 for (i = jit->num_line_numbers - 1; i >= 0; i--) {
984 MonoDebugLineNumberEntry lne = jit->line_numbers [i];
986 if (lne.native_offset <= native_offset) {
987 mono_debug_free_method_jit_info (jit);
988 return lne.il_offset;
993 mono_debug_free_method_jit_info (jit);
998 * mono_debug_il_offset_from_address:
1000 * Compute the IL offset corresponding to NATIVE_OFFSET inside the native
1001 * code of METHOD in DOMAIN.
1004 mono_debug_il_offset_from_address (MonoMethod *method, MonoDomain *domain, guint32 native_offset)
1008 mono_debugger_lock ();
1010 res = il_offset_from_address (method, domain, native_offset);
1012 mono_debugger_unlock ();
1018 * mono_debug_lookup_source_location:
1019 * @address: Native offset within the @method's machine code.
1021 * Lookup the source code corresponding to the machine instruction located at
1022 * native offset @address within @method.
1024 * The returned `MonoDebugSourceLocation' contains both file / line number
1025 * information and the corresponding IL offset. It must be freed by
1026 * mono_debug_free_source_location().
1028 MonoDebugSourceLocation *
1029 mono_debug_lookup_source_location (MonoMethod *method, guint32 address, MonoDomain *domain)
1031 MonoDebugMethodInfo *minfo;
1032 MonoDebugSourceLocation *location;
1035 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
1038 mono_debugger_lock ();
1039 minfo = _mono_debug_lookup_method (method);
1040 if (!minfo || !minfo->handle || !minfo->handle->symfile || !mono_debug_symfile_is_loaded (minfo->handle->symfile)) {
1041 mono_debugger_unlock ();
1045 offset = il_offset_from_address (method, domain, address);
1047 mono_debugger_unlock ();
1051 location = mono_debug_symfile_lookup_location (minfo, offset);
1052 mono_debugger_unlock ();
1057 * mono_debug_lookup_locals:
1059 * Return information about the local variables of MINFO.
1060 * The result should be freed using mono_debug_symfile_free_locals ().
1062 MonoDebugLocalsInfo*
1063 mono_debug_lookup_locals (MonoMethod *method)
1065 MonoDebugMethodInfo *minfo;
1066 MonoDebugLocalsInfo *res;
1068 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
1071 mono_debugger_lock ();
1072 minfo = _mono_debug_lookup_method (method);
1073 if (!minfo || !minfo->handle || !minfo->handle->symfile || !mono_debug_symfile_is_loaded (minfo->handle->symfile)) {
1074 mono_debugger_unlock ();
1078 res = mono_debug_symfile_lookup_locals (minfo);
1079 mono_debugger_unlock ();
1085 * mono_debug_free_source_location:
1086 * @location: A `MonoDebugSourceLocation'.
1088 * Frees the @location.
1091 mono_debug_free_source_location (MonoDebugSourceLocation *location)
1094 g_free (location->source_file);
1100 * mono_debug_print_stack_frame:
1101 * @native_offset: Native offset within the @method's machine code.
1103 * Conventient wrapper around mono_debug_lookup_source_location() which can be
1104 * used if you only want to use the location to print a stack frame.
1107 mono_debug_print_stack_frame (MonoMethod *method, guint32 native_offset, MonoDomain *domain)
1109 MonoDebugSourceLocation *location;
1110 gchar *fname, *ptr, *res;
1113 fname = mono_method_full_name (method, TRUE);
1114 for (ptr = fname; *ptr; ptr++) {
1115 if (*ptr == ':') *ptr = '.';
1118 location = mono_debug_lookup_source_location (method, native_offset, domain);
1121 if (mono_debug_initialized) {
1122 mono_debugger_lock ();
1123 offset = il_offset_from_address (method, domain, native_offset);
1124 mono_debugger_unlock ();
1130 res = g_strdup_printf ("at %s <0x%05x>", fname, native_offset);
1132 res = g_strdup_printf ("at %s <IL 0x%05x, 0x%05x>", fname, offset, native_offset);
1137 res = g_strdup_printf ("at %s [0x%05x] in %s:%d", fname, location->il_offset,
1138 location->source_file, location->row);
1141 mono_debug_free_source_location (location);
1146 mono_debug_list_add (MonoDebugList **list, gconstpointer data)
1148 MonoDebugList *element, **ptr;
1150 element = g_new0 (MonoDebugList, 1);
1151 element->data = data;
1153 for (ptr = list; *ptr; ptr = &(*ptr)->next)
1160 mono_debug_list_remove (MonoDebugList **list, gconstpointer data)
1162 MonoDebugList **ptr;
1163 MonoDebugList *next;
1165 for (ptr = list; *ptr; ptr = &(*ptr)->next) {
1166 if ((*ptr)->data != data)
1169 next = (*ptr)->next;
1176 static gboolean is_attached = FALSE;
1179 mono_set_is_debugger_attached (gboolean attached)
1181 is_attached = attached;
1185 mono_is_debugger_attached (void)