2 #include <mono/metadata/assembly.h>
3 #include <mono/metadata/tabledefs.h>
4 #include <mono/metadata/tokentype.h>
5 #include <mono/metadata/appdomain.h>
6 #include <mono/metadata/class-internals.h>
7 #include <mono/metadata/mono-debug.h>
8 #include <mono/metadata/mono-debug-debugger.h>
9 #include <mono/metadata/mono-endian.h>
12 #define DATA_TABLE_CHUNK_SIZE 16384
14 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
16 #if NO_UNALIGNED_ACCESS
17 #define RETURN_UNALIGNED(type, addr) \
20 memcpy(&val, p + offset, sizeof(val)); \
23 #define WRITE_UNALIGNED(type, addr, val) \
24 memcpy(addr, &val, sizeof(type))
25 #define READ_UNALIGNED(type, addr, val) \
26 memcpy(&val, addr, sizeof(type))
28 #define RETURN_UNALIGNED(type, addr) \
29 return *(type*)(p + offset);
30 #define WRITE_UNALIGNED(type, addr, val) \
31 (*(type *)(addr) = (val))
32 #define READ_UNALIGNED(type, addr, val) \
33 val = (*(type *)(addr))
37 MONO_DEBUG_DATA_ITEM_UNKNOWN = 0,
38 MONO_DEBUG_DATA_ITEM_CLASS,
39 MONO_DEBUG_DATA_ITEM_METHOD,
40 MONO_DEBUG_DATA_ITEM_DELEGATE_TRAMPOLINE
41 } MonoDebugDataItemType;
43 typedef struct _MonoDebugDataChunk MonoDebugDataChunk;
45 struct _MonoDebugDataChunk {
47 guint32 allocated_size;
48 guint32 current_offset;
50 MonoDebugDataChunk *next;
51 guint8 data [MONO_ZERO_LEN_ARRAY];
54 struct _MonoDebugDataTable {
56 gint32 _dummy; /* alignment for next field. */
57 MonoDebugDataChunk *first_chunk;
58 MonoDebugDataChunk *current_chunk;
59 GHashTable *method_hash;
60 GHashTable *method_address_hash;
64 const gchar *method_name;
65 const gchar *cil_code;
67 } MonoDebugWrapperData;
74 MonoDebugWrapperData *wrapper_data;
77 } MonoDebugMethodHeader;
79 struct _MonoDebugMethodAddress {
80 MonoDebugMethodHeader header;
81 const guint8 *code_start;
82 const guint8 *wrapper_addr;
84 guint8 data [MONO_ZERO_LEN_ARRAY];
87 struct _MonoDebugClassEntry {
89 guint8 data [MONO_ZERO_LEN_ARRAY];
95 } MonoDebugDelegateTrampolineEntry;
97 MonoSymbolTable *mono_symbol_table = NULL;
98 MonoDebugFormat mono_debug_format = MONO_DEBUG_FORMAT_NONE;
99 gint32 mono_debug_debugger_version = 3;
101 static gboolean in_the_mono_debugger = FALSE;
102 static gboolean mono_debug_initialized = FALSE;
103 GHashTable *mono_debug_handles = NULL;
105 static GHashTable *data_table_hash = NULL;
106 static int next_symbol_file_id = 0;
108 static MonoDebugHandle *mono_debug_open_image (MonoImage *image, const guint8 *raw_contents, int size);
110 static MonoDebugHandle *_mono_debug_get_image (MonoImage *image);
111 static void mono_debug_add_assembly (MonoAssembly *assembly,
113 static void mono_debug_add_type (MonoClass *klass);
115 void _mono_debug_init_corlib (MonoDomain *domain);
117 extern void (*mono_debugger_class_init_func) (MonoClass *klass);
118 extern void (*mono_debugger_class_loaded_methods_func) (MonoClass *klass);
120 static MonoDebugDataTable *
121 create_data_table (MonoDomain *domain)
123 MonoDebugDataTable *table;
124 MonoDebugDataChunk *chunk;
126 table = g_new0 (MonoDebugDataTable, 1);
127 table->domain = domain ? mono_domain_get_id (domain) : -1;
129 table->method_address_hash = g_hash_table_new (NULL, NULL);
130 table->method_hash = g_hash_table_new (NULL, NULL);
132 chunk = g_malloc0 (sizeof (MonoDebugDataChunk) + DATA_TABLE_CHUNK_SIZE);
133 chunk->total_size = DATA_TABLE_CHUNK_SIZE;
135 table->first_chunk = table->current_chunk = chunk;
138 mono_debug_list_add (&mono_symbol_table->data_tables, table);
139 g_hash_table_insert (data_table_hash, domain, table);
146 free_header_data (gpointer key, gpointer value, gpointer user_data)
148 MonoDebugMethodHeader *header = (MonoDebugMethodHeader*)value;
150 if (header->wrapper_data) {
151 g_free ((gpointer)header->wrapper_data->method_name);
152 g_free ((gpointer)header->wrapper_data->cil_code);
153 g_slist_free (header->address_list);
154 g_free (header->wrapper_data);
159 free_data_table (MonoDebugDataTable *table)
161 MonoDebugDataChunk *chunk, *next_chunk;
163 g_hash_table_foreach (table->method_hash, free_header_data, NULL);
164 g_hash_table_destroy (table->method_hash);
165 g_hash_table_destroy (table->method_address_hash);
167 table->method_hash = NULL;
168 table->method_address_hash = NULL;
170 chunk = table->first_chunk;
172 next_chunk = chunk->next;
177 table->first_chunk = table->current_chunk = NULL;
178 mono_debug_list_remove (&mono_symbol_table->data_tables, table);
182 static MonoDebugDataTable *
183 lookup_data_table (MonoDomain *domain)
185 MonoDebugDataTable *table;
187 table = g_hash_table_lookup (data_table_hash, domain);
193 free_debug_handle (MonoDebugHandle *handle)
196 mono_debug_close_mono_symbol_file (handle->symfile);
197 /* decrease the refcount added with mono_image_addref () */
198 free_data_table (handle->type_table);
199 mono_image_close (handle->image);
200 g_free (handle->image_file);
205 * Initialize debugging support.
207 * This method must be called after loading corlib,
208 * but before opening the application's main assembly because we need to set some
212 mono_debug_init (MonoDebugFormat format)
214 g_assert (!mono_debug_initialized);
216 mono_debug_initialized = TRUE;
217 mono_debug_format = format;
218 in_the_mono_debugger = format == MONO_DEBUG_FORMAT_DEBUGGER;
220 mono_debugger_initialize (in_the_mono_debugger);
222 mono_debugger_lock ();
224 mono_symbol_table = g_new0 (MonoSymbolTable, 1);
225 mono_symbol_table->magic = MONO_DEBUGGER_MAGIC;
226 mono_symbol_table->version = MONO_DEBUGGER_VERSION;
227 mono_symbol_table->total_size = sizeof (MonoSymbolTable);
229 mono_debug_handles = g_hash_table_new_full
230 (NULL, NULL, NULL, (GDestroyNotify) free_debug_handle);
232 data_table_hash = g_hash_table_new_full (
233 NULL, NULL, NULL, (GDestroyNotify) free_data_table);
235 mono_debugger_class_init_func = mono_debug_add_type;
236 mono_debugger_class_loaded_methods_func = mono_debugger_class_initialized;
237 mono_install_assembly_load_hook (mono_debug_add_assembly, NULL);
239 mono_symbol_table->global_data_table = create_data_table (NULL);
241 mono_debugger_unlock ();
245 * INTERNAL USE ONLY !
248 _mono_debug_init_corlib (MonoDomain *domain)
250 if (!mono_debug_initialized)
253 mono_symbol_table->corlib = mono_debug_open_image (mono_defaults.corlib, NULL, 0);
254 mono_debugger_event (MONO_DEBUGGER_EVENT_INITIALIZE_CORLIB,
255 (guint64) (gsize) mono_symbol_table->corlib, 0);
259 mono_debug_open_image_from_memory (MonoImage *image, const guint8 *raw_contents, int size)
261 mono_debug_open_image (image, raw_contents, size);
266 mono_debug_using_mono_debugger (void)
268 return in_the_mono_debugger;
272 mono_debug_cleanup (void)
274 if (mono_debug_handles)
275 g_hash_table_destroy (mono_debug_handles);
276 mono_debug_handles = NULL;
278 if (data_table_hash) {
279 g_hash_table_destroy (data_table_hash);
280 data_table_hash = NULL;
283 g_free (mono_symbol_table);
284 mono_symbol_table = NULL;
288 mono_debug_domain_create (MonoDomain *domain)
290 MonoDebugDataTable *table;
292 if (!mono_debug_initialized)
295 mono_debugger_lock ();
297 table = create_data_table (domain);
299 mono_debugger_event (MONO_DEBUGGER_EVENT_DOMAIN_CREATE, (guint64) (gsize) table,
300 mono_domain_get_id (domain));
302 mono_debugger_unlock ();
306 mono_debug_domain_unload (MonoDomain *domain)
308 MonoDebugDataTable *table;
310 if (!mono_debug_initialized)
313 mono_debugger_lock ();
315 table = g_hash_table_lookup (data_table_hash, domain);
317 g_warning (G_STRLOC ": unloading unknown domain %p / %d",
318 domain, mono_domain_get_id (domain));
319 mono_debugger_unlock ();
323 mono_debugger_event (MONO_DEBUGGER_EVENT_DOMAIN_UNLOAD, (guint64) (gsize) table,
324 mono_domain_get_id (domain));
326 g_hash_table_remove (data_table_hash, domain);
328 mono_debugger_unlock ();
331 static MonoDebugHandle *
332 _mono_debug_get_image (MonoImage *image)
334 return g_hash_table_lookup (mono_debug_handles, image);
338 mono_debug_close_image (MonoImage *image)
340 MonoDebugHandle *handle;
342 if (!mono_debug_initialized)
345 handle = _mono_debug_get_image (image);
349 mono_debugger_lock ();
351 mono_debugger_event (MONO_DEBUGGER_EVENT_UNLOAD_MODULE, (guint64) (gsize) handle,
354 mono_debug_list_remove (&mono_symbol_table->symbol_files, handle);
355 g_hash_table_remove (mono_debug_handles, image);
357 mono_debugger_unlock ();
360 static MonoDebugHandle *
361 mono_debug_open_image (MonoImage *image, const guint8 *raw_contents, int size)
363 MonoDebugHandle *handle;
365 if (mono_image_is_dynamic (image))
368 handle = _mono_debug_get_image (image);
372 mono_debugger_lock ();
374 handle = g_new0 (MonoDebugHandle, 1);
375 handle->index = ++next_symbol_file_id;
377 handle->image = image;
378 mono_image_addref (image);
379 handle->image_file = g_strdup (mono_image_get_filename (image));
381 handle->type_table = create_data_table (NULL);
383 handle->symfile = mono_debug_open_mono_symbols (handle, raw_contents, size, in_the_mono_debugger);
385 mono_debug_list_add (&mono_symbol_table->symbol_files, handle);
387 g_hash_table_insert (mono_debug_handles, image, handle);
389 if (mono_symbol_table->corlib)
390 mono_debugger_event (MONO_DEBUGGER_EVENT_LOAD_MODULE,
391 (guint64) (gsize) handle, 0);
393 mono_debugger_unlock ();
399 mono_debug_add_assembly (MonoAssembly *assembly, gpointer user_data)
401 mono_debugger_lock ();
402 mono_debug_open_image (mono_assembly_get_image (assembly), NULL, 0);
403 mono_debugger_unlock ();
407 allocate_data_item (MonoDebugDataTable *table, MonoDebugDataItemType type, guint32 size)
412 size = ALIGN_TO (size, sizeof (gpointer));
414 if (size + 16 < DATA_TABLE_CHUNK_SIZE)
415 chunk_size = DATA_TABLE_CHUNK_SIZE;
417 chunk_size = size + 16;
419 g_assert (table->current_chunk->current_offset == table->current_chunk->allocated_size);
421 if (table->current_chunk->allocated_size + size + 8 >= table->current_chunk->total_size) {
422 MonoDebugDataChunk *new_chunk;
424 new_chunk = g_malloc0 (sizeof (MonoDebugDataChunk) + chunk_size);
425 new_chunk->total_size = chunk_size;
427 table->current_chunk->next = new_chunk;
428 table->current_chunk = new_chunk;
431 data = &table->current_chunk->data [table->current_chunk->allocated_size];
432 table->current_chunk->allocated_size += size + 8;
434 * ((guint32 *) data) = size;
436 * ((guint32 *) data) = type;
442 write_data_item (MonoDebugDataTable *table, const guint8 *data)
444 MonoDebugDataChunk *current_chunk = table->current_chunk;
445 guint32 size = * ((guint32 *) (data - 8));
447 g_assert (current_chunk->current_offset + size + 8 == current_chunk->allocated_size);
448 current_chunk->current_offset = current_chunk->allocated_size;
451 struct LookupMethodData
453 MonoDebugMethodInfo *minfo;
458 lookup_method_func (gpointer key, gpointer value, gpointer user_data)
460 MonoDebugHandle *handle = (MonoDebugHandle *) value;
461 struct LookupMethodData *data = (struct LookupMethodData *) user_data;
467 data->minfo = mono_debug_symfile_lookup_method (handle, data->method);
470 static MonoDebugMethodInfo *
471 _mono_debug_lookup_method (MonoMethod *method)
473 struct LookupMethodData data;
476 data.method = method;
478 if (!mono_debug_handles)
481 g_hash_table_foreach (mono_debug_handles, lookup_method_func, &data);
486 * mono_debug_lookup_method:
488 * Lookup symbol file information for the method @method. The returned
489 * `MonoDebugMethodInfo' is a private structure, but it can be passed to
490 * mono_debug_symfile_lookup_location().
492 MonoDebugMethodInfo *
493 mono_debug_lookup_method (MonoMethod *method)
495 MonoDebugMethodInfo *minfo;
497 mono_debugger_lock ();
498 minfo = _mono_debug_lookup_method (method);
499 mono_debugger_unlock ();
504 write_leb128 (guint32 value, guint8 *ptr, guint8 **rptr)
507 guint8 byte = value & 0x7f;
518 write_sleb128 (gint32 value, guint8 *ptr, guint8 **rptr)
523 guint8 byte = value & 0x7f;
526 if (((value == 0) && ((byte & 0x40) == 0)) || ((value == -1) && (byte & 0x40)))
537 write_variable (MonoDebugVarInfo *var, guint8 *ptr, guint8 **rptr)
539 write_leb128 (var->index, ptr, &ptr);
540 write_sleb128 (var->offset, ptr, &ptr);
541 write_leb128 (var->size, ptr, &ptr);
542 write_leb128 (var->begin_scope, ptr, &ptr);
543 write_leb128 (var->end_scope, ptr, &ptr);
544 WRITE_UNALIGNED (gpointer, ptr, var->type);
545 ptr += sizeof (gpointer);
549 MonoDebugMethodAddress *
550 mono_debug_add_method (MonoMethod *method, MonoDebugMethodJitInfo *jit, MonoDomain *domain)
552 MonoMethod *declaring;
553 MonoDebugDataTable *table;
554 MonoDebugMethodHeader *header;
555 MonoDebugMethodAddress *address;
556 MonoDebugMethodInfo *minfo;
557 MonoDebugHandle *handle;
558 guint8 buffer [BUFSIZ];
559 guint8 *ptr, *oldptr;
560 guint32 i, size, total_size, max_size;
561 gboolean is_wrapper = FALSE;
563 mono_debugger_lock ();
565 table = lookup_data_table (domain);
567 handle = _mono_debug_get_image (method->klass->image);
568 minfo = _mono_debug_lookup_method (method);
570 if (!minfo || (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
571 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
572 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
573 (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
574 (method->wrapper_type != MONO_WRAPPER_NONE)) {
578 max_size = 24 + 8 * jit->num_line_numbers +
579 (20 + sizeof (gpointer)) * (1 + jit->num_params + jit->num_locals);
581 if (max_size > BUFSIZ)
582 ptr = oldptr = g_malloc (max_size);
584 ptr = oldptr = buffer;
586 write_leb128 (jit->prologue_end, ptr, &ptr);
587 write_leb128 (jit->epilogue_begin, ptr, &ptr);
589 write_leb128 (jit->num_line_numbers, ptr, &ptr);
590 for (i = 0; i < jit->num_line_numbers; i++) {
591 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
593 write_sleb128 (lne->il_offset, ptr, &ptr);
594 write_sleb128 (lne->native_offset, ptr, &ptr);
597 *ptr++ = jit->this_var ? 1 : 0;
599 write_variable (jit->this_var, ptr, &ptr);
601 write_leb128 (jit->num_params, ptr, &ptr);
602 for (i = 0; i < jit->num_params; i++)
603 write_variable (&jit->params [i], ptr, &ptr);
605 write_leb128 (jit->num_locals, ptr, &ptr);
606 for (i = 0; i < jit->num_locals; i++)
607 write_variable (&jit->locals [i], ptr, &ptr);
610 g_assert (size < max_size);
611 total_size = size + sizeof (MonoDebugMethodAddress);
613 address = (MonoDebugMethodAddress *) allocate_data_item (
614 table, MONO_DEBUG_DATA_ITEM_METHOD, total_size);
616 address->header.size = total_size;
617 address->header.symfile_id = handle ? handle->index : 0;
618 address->header.domain_id = mono_domain_get_id (domain);
619 address->header.method_id = is_wrapper ? 0 : minfo->index;
620 address->header.method = method;
622 address->code_start = jit->code_start;
623 address->code_size = jit->code_size;
625 memcpy (&address->data, oldptr, size);
626 if (max_size > BUFSIZ)
629 declaring = method->is_inflated ? ((MonoMethodInflated *) method)->declaring : method;
630 header = g_hash_table_lookup (table->method_hash, declaring);
633 header = &address->header;
634 g_hash_table_insert (table->method_hash, declaring, header);
637 const unsigned char* il_code;
638 MonoMethodHeader *mheader;
639 MonoDebugWrapperData *wrapper;
642 mheader = mono_method_get_header (declaring);
643 il_code = mono_method_header_get_code (mheader, &il_codesize, NULL);
645 header->wrapper_data = wrapper = g_new0 (MonoDebugWrapperData, 1);
647 wrapper->wrapper_type = method->wrapper_type;
648 wrapper->method_name = mono_method_full_name (declaring, TRUE);
649 wrapper->cil_code = mono_disasm_code (
650 NULL, declaring, il_code, il_code + il_codesize);
653 address->header.wrapper_data = header->wrapper_data;
654 header->address_list = g_slist_prepend (header->address_list, address);
657 g_hash_table_insert (table->method_address_hash, method, address);
659 write_data_item (table, (guint8 *) address);
661 mono_debugger_unlock ();
666 mono_debug_add_delegate_trampoline (gpointer code, int size)
668 MonoDebugDelegateTrampolineEntry *entry;
670 if (!mono_debug_initialized)
673 mono_debugger_lock ();
675 entry = (MonoDebugDelegateTrampolineEntry *) allocate_data_item (
676 mono_symbol_table->global_data_table, MONO_DEBUG_DATA_ITEM_DELEGATE_TRAMPOLINE,
677 sizeof (MonoDebugDelegateTrampolineEntry));
681 write_data_item (mono_symbol_table->global_data_table, (guint8 *) entry);
683 mono_debugger_unlock ();
686 static inline guint32
687 read_leb128 (guint8 *ptr, guint8 **rptr)
689 guint32 result = 0, shift = 0;
692 guint8 byte = *ptr++;
694 result |= (byte & 0x7f) << shift;
695 if ((byte & 0x80) == 0)
705 read_sleb128 (guint8 *ptr, guint8 **rptr)
711 guint8 byte = *ptr++;
713 result |= (byte & 0x7f) << shift;
719 if ((shift < 32) && (byte & 0x40))
720 result |= - (1 << shift);
729 read_variable (MonoDebugVarInfo *var, guint8 *ptr, guint8 **rptr)
731 var->index = read_leb128 (ptr, &ptr);
732 var->offset = read_sleb128 (ptr, &ptr);
733 var->size = read_leb128 (ptr, &ptr);
734 var->begin_scope = read_leb128 (ptr, &ptr);
735 var->end_scope = read_leb128 (ptr, &ptr);
736 READ_UNALIGNED (gpointer, ptr, var->type);
737 ptr += sizeof (gpointer);
741 static MonoDebugMethodJitInfo *
742 mono_debug_read_method (MonoDebugMethodAddress *address)
744 MonoDebugMethodJitInfo *jit;
748 jit = g_new0 (MonoDebugMethodJitInfo, 1);
749 jit->code_start = address->code_start;
750 jit->code_size = address->code_size;
751 jit->wrapper_addr = address->wrapper_addr;
753 ptr = (guint8 *) &address->data;
755 jit->prologue_end = read_leb128 (ptr, &ptr);
756 jit->epilogue_begin = read_leb128 (ptr, &ptr);
758 jit->num_line_numbers = read_leb128 (ptr, &ptr);
759 jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
760 for (i = 0; i < jit->num_line_numbers; i++) {
761 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
763 lne->il_offset = read_sleb128 (ptr, &ptr);
764 lne->native_offset = read_sleb128 (ptr, &ptr);
768 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
769 read_variable (jit->this_var, ptr, &ptr);
772 jit->num_params = read_leb128 (ptr, &ptr);
773 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
774 for (i = 0; i < jit->num_params; i++)
775 read_variable (&jit->params [i], ptr, &ptr);
777 jit->num_locals = read_leb128 (ptr, &ptr);
778 jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
779 for (i = 0; i < jit->num_locals; i++)
780 read_variable (&jit->locals [i], ptr, &ptr);
786 mono_debug_add_type (MonoClass *klass)
788 MonoDebugHandle *handle;
789 MonoDebugClassEntry *entry;
790 guint8 buffer [BUFSIZ];
791 guint8 *ptr, *oldptr;
792 guint32 size, total_size, max_size;
795 handle = _mono_debug_get_image (klass->image);
799 if (klass->generic_class || klass->rank ||
800 (klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR))
803 mono_debugger_lock ();
805 max_size = 12 + sizeof (gpointer);
806 if (max_size > BUFSIZ)
807 ptr = oldptr = g_malloc (max_size);
809 ptr = oldptr = buffer;
811 if (klass->valuetype)
812 base_offset = - (int)(sizeof (MonoObject));
814 write_leb128 (klass->type_token, ptr, &ptr);
815 write_leb128 (klass->instance_size + base_offset, ptr, &ptr);
816 WRITE_UNALIGNED (gpointer, ptr, klass);
817 ptr += sizeof (gpointer);
820 g_assert (size < max_size);
821 total_size = size + sizeof (MonoDebugClassEntry);
823 g_assert (total_size + 9 < DATA_TABLE_CHUNK_SIZE);
825 entry = (MonoDebugClassEntry *) allocate_data_item (
826 handle->type_table, MONO_DEBUG_DATA_ITEM_CLASS, total_size);
828 entry->size = total_size;
830 memcpy (&entry->data, oldptr, size);
832 write_data_item (handle->type_table, (guint8 *) entry);
834 if (max_size > BUFSIZ)
837 mono_debugger_unlock ();
840 static MonoDebugMethodJitInfo *
841 find_method (MonoMethod *method, MonoDomain *domain)
843 MonoDebugDataTable *table;
844 MonoDebugMethodAddress *address;
846 table = lookup_data_table (domain);
847 address = g_hash_table_lookup (table->method_address_hash, method);
852 return mono_debug_read_method (address);
855 MonoDebugMethodJitInfo *
856 mono_debug_find_method (MonoMethod *method, MonoDomain *domain)
858 MonoDebugMethodJitInfo *res;
859 mono_debugger_lock ();
860 res = find_method (method, domain);
861 mono_debugger_unlock ();
865 struct LookupMethodAddressData
868 MonoDebugMethodHeader *result;
872 lookup_method_address_func (gpointer key, gpointer value, gpointer user_data)
874 MonoDebugDataTable *table = (MonoDebugDataTable *) value;
875 struct LookupMethodAddressData *data = (struct LookupMethodAddressData *) user_data;
876 MonoDebugMethodHeader *header;
878 header = g_hash_table_lookup (table->method_hash, data->method);
880 data->result = header;
883 MonoDebugMethodAddressList *
884 mono_debug_lookup_method_addresses (MonoMethod *method)
886 MonoDebugMethodAddressList *info;
887 MonoDebugMethodHeader *header = NULL;
888 struct LookupMethodAddressData data;
889 MonoMethod *declaring;
894 g_assert (mono_debug_debugger_version == 3);
896 mono_debugger_lock ();
898 declaring = method->is_inflated ? ((MonoMethodInflated *) method)->declaring : method;
900 data.method = declaring;
903 g_hash_table_foreach (data_table_hash, lookup_method_address_func, &data);
904 header = data.result;
907 mono_debugger_unlock ();
911 count = g_slist_length (header->address_list) + 1;
912 size = sizeof (MonoDebugMethodAddressList) + count * sizeof (gpointer);
914 info = g_malloc0 (size);
920 WRITE_UNALIGNED (gpointer, ptr, header);
921 ptr += sizeof (gpointer);
923 for (list = header->address_list; list; list = list->next) {
924 WRITE_UNALIGNED (gpointer, ptr, list->data);
925 ptr += sizeof (gpointer);
928 mono_debugger_unlock ();
933 il_offset_from_address (MonoMethod *method, MonoDomain *domain, guint32 native_offset)
935 MonoDebugMethodJitInfo *jit;
938 jit = find_method (method, domain);
939 if (!jit || !jit->line_numbers)
942 for (i = jit->num_line_numbers - 1; i >= 0; i--) {
943 MonoDebugLineNumberEntry lne = jit->line_numbers [i];
945 if (lne.native_offset <= native_offset)
946 return lne.il_offset;
953 * mono_debug_lookup_source_location:
954 * @address: Native offset within the @method's machine code.
956 * Lookup the source code corresponding to the machine instruction located at
957 * native offset @address within @method.
959 * The returned `MonoDebugSourceLocation' contains both file / line number
960 * information and the corresponding IL offset. It must be freed by
961 * mono_debug_free_source_location().
963 MonoDebugSourceLocation *
964 mono_debug_lookup_source_location (MonoMethod *method, guint32 address, MonoDomain *domain)
966 MonoDebugMethodInfo *minfo;
967 MonoDebugSourceLocation *location;
970 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
973 mono_debugger_lock ();
974 minfo = _mono_debug_lookup_method (method);
975 if (!minfo || !minfo->handle || !minfo->handle->symfile || !minfo->handle->symfile->offset_table) {
976 mono_debugger_unlock ();
980 offset = il_offset_from_address (method, domain, address);
982 mono_debugger_unlock ();
986 location = mono_debug_symfile_lookup_location (minfo, offset);
987 mono_debugger_unlock ();
992 * mono_debug_free_source_location:
993 * @location: A `MonoDebugSourceLocation'.
995 * Frees the @location.
998 mono_debug_free_source_location (MonoDebugSourceLocation *location)
1001 g_free (location->source_file);
1007 * mono_debug_print_stack_frame:
1008 * @native_offset: Native offset within the @method's machine code.
1010 * Conventient wrapper around mono_debug_lookup_source_location() which can be
1011 * used if you only want to use the location to print a stack frame.
1014 mono_debug_print_stack_frame (MonoMethod *method, guint32 native_offset, MonoDomain *domain)
1016 MonoDebugSourceLocation *location;
1017 gchar *fname, *ptr, *res;
1019 fname = mono_method_full_name (method, TRUE);
1020 for (ptr = fname; *ptr; ptr++) {
1021 if (*ptr == ':') *ptr = '.';
1024 location = mono_debug_lookup_source_location (method, native_offset, domain);
1027 res = g_strdup_printf ("at %s <0x%05x>", fname, native_offset);
1032 res = g_strdup_printf ("at %s [0x%05x] in %s:%d", fname, location->il_offset,
1033 location->source_file, location->row);
1036 mono_debug_free_source_location (location);
1041 mono_debug_list_add (MonoDebugList **list, gconstpointer data)
1043 MonoDebugList *element, **ptr;
1045 element = g_new0 (MonoDebugList, 1);
1046 element->data = data;
1048 for (ptr = list; *ptr; ptr = &(*ptr)->next)
1055 mono_debug_list_remove (MonoDebugList **list, gconstpointer data)
1057 MonoDebugList **ptr;
1058 MonoDebugList *next;
1060 for (ptr = list; *ptr; ptr = &(*ptr)->next) {
1061 if ((*ptr)->data != data)
1064 next = (*ptr)->next;