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 *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 = 4;
110 gint32 _mono_debug_using_mono_debugger = 0;
112 static gboolean mono_debug_initialized = FALSE;
113 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_free ((gpointer)header->wrapper_data->cil_code);
163 g_slist_free (header->address_list);
164 g_free (header->wrapper_data);
169 free_data_table (MonoDebugDataTable *table)
171 MonoDebugDataChunk *chunk, *next_chunk;
173 g_hash_table_foreach (table->method_hash, free_header_data, NULL);
174 g_hash_table_destroy (table->method_hash);
175 g_hash_table_destroy (table->method_address_hash);
177 table->method_hash = NULL;
178 table->method_address_hash = NULL;
180 chunk = table->first_chunk;
182 next_chunk = chunk->next;
187 table->first_chunk = table->current_chunk = NULL;
188 mono_debug_list_remove (&mono_symbol_table->data_tables, table);
192 static MonoDebugDataTable *
193 lookup_data_table (MonoDomain *domain)
195 MonoDebugDataTable *table;
197 table = g_hash_table_lookup (data_table_hash, domain);
203 free_debug_handle (MonoDebugHandle *handle)
206 mono_debug_close_mono_symbol_file (handle->symfile);
207 /* decrease the refcount added with mono_image_addref () */
208 free_data_table (handle->type_table);
209 mono_image_close (handle->image);
210 g_free (handle->image_file);
215 * Initialize debugging support.
217 * This method must be called after loading corlib,
218 * but before opening the application's main assembly because we need to set some
222 mono_debug_init (MonoDebugFormat format)
224 g_assert (!mono_debug_initialized);
226 mono_debug_initialized = TRUE;
227 mono_debug_format = format;
229 mono_debugger_initialize (_mono_debug_using_mono_debugger);
231 mono_debugger_lock ();
233 mono_symbol_table = g_new0 (MonoSymbolTable, 1);
234 mono_symbol_table->magic = MONO_DEBUGGER_MAGIC;
235 mono_symbol_table->version = MONO_DEBUGGER_MAJOR_VERSION;
236 mono_symbol_table->total_size = sizeof (MonoSymbolTable);
238 mono_debug_handles = g_hash_table_new_full
239 (NULL, NULL, NULL, (GDestroyNotify) free_debug_handle);
241 data_table_hash = g_hash_table_new_full (
242 NULL, NULL, NULL, (GDestroyNotify) free_data_table);
244 mono_debugger_class_init_func = mono_debug_add_type;
245 mono_debugger_class_loaded_methods_func = mono_debugger_class_initialized;
246 mono_install_assembly_load_hook (mono_debug_add_assembly, NULL);
248 mono_symbol_table->global_data_table = create_data_table (NULL);
250 mono_debugger_unlock ();
254 * INTERNAL USE ONLY !
257 _mono_debug_init_corlib (MonoDomain *domain)
259 if (!mono_debug_initialized)
262 mono_symbol_table->corlib = mono_debug_open_image (mono_defaults.corlib, NULL, 0);
263 mono_debugger_event (MONO_DEBUGGER_EVENT_INITIALIZE_CORLIB,
264 (guint64) (gsize) mono_symbol_table->corlib, 0);
268 mono_debug_open_image_from_memory (MonoImage *image, const guint8 *raw_contents, int size)
270 mono_debug_open_image (image, raw_contents, size);
275 mono_debug_using_mono_debugger (void)
277 return _mono_debug_using_mono_debugger;
281 mono_debug_cleanup (void)
283 if (mono_debug_handles)
284 g_hash_table_destroy (mono_debug_handles);
285 mono_debug_handles = NULL;
287 if (data_table_hash) {
288 g_hash_table_destroy (data_table_hash);
289 data_table_hash = NULL;
292 g_free (mono_symbol_table);
293 mono_symbol_table = NULL;
297 mono_debug_domain_create (MonoDomain *domain)
299 MonoDebugDataTable *table;
301 if (!mono_debug_initialized)
304 mono_debugger_lock ();
306 table = create_data_table (domain);
308 mono_debugger_event (MONO_DEBUGGER_EVENT_DOMAIN_CREATE, (guint64) (gsize) table,
309 mono_domain_get_id (domain));
311 mono_debugger_unlock ();
315 mono_debug_domain_unload (MonoDomain *domain)
317 MonoDebugDataTable *table;
319 if (!mono_debug_initialized)
322 mono_debugger_lock ();
324 table = g_hash_table_lookup (data_table_hash, domain);
326 g_warning (G_STRLOC ": unloading unknown domain %p / %d",
327 domain, mono_domain_get_id (domain));
328 mono_debugger_unlock ();
332 mono_debugger_event (MONO_DEBUGGER_EVENT_DOMAIN_UNLOAD, (guint64) (gsize) table,
333 mono_domain_get_id (domain));
335 g_hash_table_remove (data_table_hash, domain);
337 mono_debugger_unlock ();
340 static MonoDebugHandle *
341 _mono_debug_get_image (MonoImage *image)
343 return g_hash_table_lookup (mono_debug_handles, image);
347 mono_debug_close_image (MonoImage *image)
349 MonoDebugHandle *handle;
351 if (!mono_debug_initialized)
354 handle = _mono_debug_get_image (image);
358 mono_debugger_lock ();
360 mono_debugger_event (MONO_DEBUGGER_EVENT_UNLOAD_MODULE, (guint64) (gsize) handle,
363 mono_debug_list_remove (&mono_symbol_table->symbol_files, handle);
364 g_hash_table_remove (mono_debug_handles, image);
366 mono_debugger_unlock ();
369 static MonoDebugHandle *
370 mono_debug_open_image (MonoImage *image, const guint8 *raw_contents, int size)
372 MonoDebugHandle *handle;
374 if (mono_image_is_dynamic (image))
377 handle = _mono_debug_get_image (image);
381 mono_debugger_lock ();
383 handle = g_new0 (MonoDebugHandle, 1);
384 handle->index = ++next_symbol_file_id;
386 handle->image = image;
387 mono_image_addref (image);
388 handle->image_file = g_strdup (mono_image_get_filename (image));
390 handle->type_table = create_data_table (NULL);
392 handle->symfile = mono_debug_open_mono_symbols (
393 handle, raw_contents, size, _mono_debug_using_mono_debugger);
395 mono_debug_list_add (&mono_symbol_table->symbol_files, handle);
397 g_hash_table_insert (mono_debug_handles, image, handle);
399 if (mono_symbol_table->corlib)
400 mono_debugger_event (MONO_DEBUGGER_EVENT_LOAD_MODULE,
401 (guint64) (gsize) handle, 0);
403 mono_debugger_unlock ();
409 mono_debug_add_assembly (MonoAssembly *assembly, gpointer user_data)
411 mono_debugger_lock ();
412 mono_debug_open_image (mono_assembly_get_image (assembly), NULL, 0);
413 mono_debugger_unlock ();
417 allocate_data_item (MonoDebugDataTable *table, MonoDebugDataItemType type, guint32 size)
422 size = ALIGN_TO (size, sizeof (gpointer));
424 if (size + 16 < DATA_TABLE_CHUNK_SIZE)
425 chunk_size = DATA_TABLE_CHUNK_SIZE;
427 chunk_size = size + 16;
429 g_assert (table->current_chunk->current_offset == table->current_chunk->allocated_size);
431 if (table->current_chunk->allocated_size + size + 8 >= table->current_chunk->total_size) {
432 MonoDebugDataChunk *new_chunk;
434 new_chunk = g_malloc0 (sizeof (MonoDebugDataChunk) + chunk_size);
435 new_chunk->total_size = chunk_size;
437 table->current_chunk->next = new_chunk;
438 table->current_chunk = new_chunk;
441 data = &table->current_chunk->data [table->current_chunk->allocated_size];
442 table->current_chunk->allocated_size += size + 8;
444 * ((guint32 *) data) = size;
446 * ((guint32 *) data) = type;
452 write_data_item (MonoDebugDataTable *table, const guint8 *data)
454 MonoDebugDataChunk *current_chunk = table->current_chunk;
455 guint32 size = * ((guint32 *) (data - 8));
457 g_assert (current_chunk->current_offset + size + 8 == current_chunk->allocated_size);
458 current_chunk->current_offset = current_chunk->allocated_size;
461 struct LookupMethodData
463 MonoDebugMethodInfo *minfo;
468 lookup_method_func (gpointer key, gpointer value, gpointer user_data)
470 MonoDebugHandle *handle = (MonoDebugHandle *) value;
471 struct LookupMethodData *data = (struct LookupMethodData *) user_data;
477 data->minfo = mono_debug_symfile_lookup_method (handle, data->method);
480 static MonoDebugMethodInfo *
481 _mono_debug_lookup_method (MonoMethod *method)
483 struct LookupMethodData data;
486 data.method = method;
488 if (!mono_debug_handles)
491 g_hash_table_foreach (mono_debug_handles, lookup_method_func, &data);
496 * mono_debug_lookup_method:
498 * Lookup symbol file information for the method @method. The returned
499 * `MonoDebugMethodInfo' is a private structure, but it can be passed to
500 * mono_debug_symfile_lookup_location().
502 MonoDebugMethodInfo *
503 mono_debug_lookup_method (MonoMethod *method)
505 MonoDebugMethodInfo *minfo;
507 mono_debugger_lock ();
508 minfo = _mono_debug_lookup_method (method);
509 mono_debugger_unlock ();
514 write_leb128 (guint32 value, guint8 *ptr, guint8 **rptr)
517 guint8 byte = value & 0x7f;
528 write_sleb128 (gint32 value, guint8 *ptr, guint8 **rptr)
533 guint8 byte = value & 0x7f;
536 if (((value == 0) && ((byte & 0x40) == 0)) || ((value == -1) && (byte & 0x40)))
547 write_variable (MonoDebugVarInfo *var, guint8 *ptr, guint8 **rptr)
549 write_leb128 (var->index, ptr, &ptr);
550 write_sleb128 (var->offset, ptr, &ptr);
551 write_leb128 (var->size, ptr, &ptr);
552 write_leb128 (var->begin_scope, ptr, &ptr);
553 write_leb128 (var->end_scope, ptr, &ptr);
554 WRITE_UNALIGNED (gpointer, ptr, var->type);
555 ptr += sizeof (gpointer);
559 MonoDebugMethodAddress *
560 mono_debug_add_method (MonoMethod *method, MonoDebugMethodJitInfo *jit, MonoDomain *domain)
562 MonoMethod *declaring;
563 MonoDebugDataTable *table;
564 MonoDebugMethodHeader *header;
565 MonoDebugMethodAddress *address;
566 MonoDebugMethodInfo *minfo;
567 MonoDebugHandle *handle;
568 guint8 buffer [BUFSIZ];
569 guint8 *ptr, *oldptr;
570 guint32 i, size, total_size, max_size;
571 gboolean is_wrapper = FALSE;
573 mono_debugger_lock ();
575 table = lookup_data_table (domain);
577 handle = _mono_debug_get_image (method->klass->image);
578 minfo = _mono_debug_lookup_method (method);
580 if (!minfo || (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
581 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
582 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
583 (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
584 (method->wrapper_type != MONO_WRAPPER_NONE)) {
588 max_size = 24 + 8 * jit->num_line_numbers +
589 (20 + sizeof (gpointer)) * (1 + jit->num_params + jit->num_locals);
591 if (max_size > BUFSIZ)
592 ptr = oldptr = g_malloc (max_size);
594 ptr = oldptr = buffer;
596 write_leb128 (jit->prologue_end, ptr, &ptr);
597 write_leb128 (jit->epilogue_begin, ptr, &ptr);
599 write_leb128 (jit->num_line_numbers, ptr, &ptr);
600 for (i = 0; i < jit->num_line_numbers; i++) {
601 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
603 write_sleb128 (lne->il_offset, ptr, &ptr);
604 write_sleb128 (lne->native_offset, ptr, &ptr);
607 *ptr++ = jit->this_var ? 1 : 0;
609 write_variable (jit->this_var, ptr, &ptr);
611 write_leb128 (jit->num_params, ptr, &ptr);
612 for (i = 0; i < jit->num_params; i++)
613 write_variable (&jit->params [i], ptr, &ptr);
615 write_leb128 (jit->num_locals, ptr, &ptr);
616 for (i = 0; i < jit->num_locals; i++)
617 write_variable (&jit->locals [i], ptr, &ptr);
620 g_assert (size < max_size);
621 total_size = size + sizeof (MonoDebugMethodAddress);
623 address = (MonoDebugMethodAddress *) allocate_data_item (
624 table, MONO_DEBUG_DATA_ITEM_METHOD, total_size);
626 address->header.size = total_size;
627 address->header.symfile_id = handle ? handle->index : 0;
628 address->header.domain_id = mono_domain_get_id (domain);
629 address->header.method_id = is_wrapper ? 0 : minfo->index;
630 address->header.method = method;
632 address->code_start = jit->code_start;
633 address->code_size = jit->code_size;
635 memcpy (&address->data, oldptr, size);
636 if (max_size > BUFSIZ)
639 declaring = method->is_inflated ? ((MonoMethodInflated *) method)->declaring : method;
640 header = g_hash_table_lookup (table->method_hash, declaring);
643 header = &address->header;
644 g_hash_table_insert (table->method_hash, declaring, header);
647 const unsigned char* il_code;
648 MonoMethodHeader *mheader;
649 MonoDebugWrapperData *wrapper;
652 mheader = mono_method_get_header (declaring);
653 il_code = mono_method_header_get_code (mheader, &il_codesize, NULL);
655 header->wrapper_data = wrapper = g_new0 (MonoDebugWrapperData, 1);
657 wrapper->wrapper_type = method->wrapper_type;
658 wrapper->method_name = mono_method_full_name (declaring, TRUE);
659 wrapper->cil_code = mono_disasm_code (
660 NULL, declaring, il_code, il_code + il_codesize);
663 address->header.wrapper_data = header->wrapper_data;
664 header->address_list = g_slist_prepend (header->address_list, address);
667 g_hash_table_insert (table->method_address_hash, method, address);
669 write_data_item (table, (guint8 *) address);
671 mono_debugger_unlock ();
676 mono_debug_add_delegate_trampoline (gpointer code, int size)
678 MonoDebugDelegateTrampolineEntry *entry;
680 if (!mono_debug_initialized)
683 mono_debugger_lock ();
685 entry = (MonoDebugDelegateTrampolineEntry *) allocate_data_item (
686 mono_symbol_table->global_data_table, MONO_DEBUG_DATA_ITEM_DELEGATE_TRAMPOLINE,
687 sizeof (MonoDebugDelegateTrampolineEntry));
691 write_data_item (mono_symbol_table->global_data_table, (guint8 *) entry);
693 mono_debugger_unlock ();
696 static inline guint32
697 read_leb128 (guint8 *ptr, guint8 **rptr)
699 guint32 result = 0, shift = 0;
702 guint8 byte = *ptr++;
704 result |= (byte & 0x7f) << shift;
705 if ((byte & 0x80) == 0)
715 read_sleb128 (guint8 *ptr, guint8 **rptr)
721 guint8 byte = *ptr++;
723 result |= (byte & 0x7f) << shift;
729 if ((shift < 32) && (byte & 0x40))
730 result |= - (1 << shift);
739 read_variable (MonoDebugVarInfo *var, guint8 *ptr, guint8 **rptr)
741 var->index = read_leb128 (ptr, &ptr);
742 var->offset = read_sleb128 (ptr, &ptr);
743 var->size = read_leb128 (ptr, &ptr);
744 var->begin_scope = read_leb128 (ptr, &ptr);
745 var->end_scope = read_leb128 (ptr, &ptr);
746 READ_UNALIGNED (gpointer, ptr, var->type);
747 ptr += sizeof (gpointer);
752 mono_debug_free_method_jit_info (MonoDebugMethodJitInfo *jit)
756 g_free (jit->line_numbers);
757 g_free (jit->this_var);
758 g_free (jit->params);
759 g_free (jit->locals);
763 static MonoDebugMethodJitInfo *
764 mono_debug_read_method (MonoDebugMethodAddress *address)
766 MonoDebugMethodJitInfo *jit;
770 jit = g_new0 (MonoDebugMethodJitInfo, 1);
771 jit->code_start = address->code_start;
772 jit->code_size = address->code_size;
773 jit->wrapper_addr = address->wrapper_addr;
775 ptr = (guint8 *) &address->data;
777 jit->prologue_end = read_leb128 (ptr, &ptr);
778 jit->epilogue_begin = read_leb128 (ptr, &ptr);
780 jit->num_line_numbers = read_leb128 (ptr, &ptr);
781 jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
782 for (i = 0; i < jit->num_line_numbers; i++) {
783 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
785 lne->il_offset = read_sleb128 (ptr, &ptr);
786 lne->native_offset = read_sleb128 (ptr, &ptr);
790 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
791 read_variable (jit->this_var, ptr, &ptr);
794 jit->num_params = read_leb128 (ptr, &ptr);
795 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
796 for (i = 0; i < jit->num_params; i++)
797 read_variable (&jit->params [i], ptr, &ptr);
799 jit->num_locals = read_leb128 (ptr, &ptr);
800 jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
801 for (i = 0; i < jit->num_locals; i++)
802 read_variable (&jit->locals [i], ptr, &ptr);
808 mono_debug_add_type (MonoClass *klass)
810 MonoDebugHandle *handle;
811 MonoDebugClassEntry *entry;
812 guint8 buffer [BUFSIZ];
813 guint8 *ptr, *oldptr;
814 guint32 size, total_size, max_size;
817 handle = _mono_debug_get_image (klass->image);
821 if (klass->generic_class || klass->rank ||
822 (klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR))
825 mono_debugger_lock ();
827 max_size = 12 + sizeof (gpointer);
828 if (max_size > BUFSIZ)
829 ptr = oldptr = g_malloc (max_size);
831 ptr = oldptr = buffer;
833 if (klass->valuetype)
834 base_offset = - (int)(sizeof (MonoObject));
836 write_leb128 (klass->type_token, ptr, &ptr);
837 write_leb128 (klass->instance_size + base_offset, ptr, &ptr);
838 WRITE_UNALIGNED (gpointer, ptr, klass);
839 ptr += sizeof (gpointer);
842 g_assert (size < max_size);
843 total_size = size + sizeof (MonoDebugClassEntry);
845 g_assert (total_size + 9 < DATA_TABLE_CHUNK_SIZE);
847 entry = (MonoDebugClassEntry *) allocate_data_item (
848 handle->type_table, MONO_DEBUG_DATA_ITEM_CLASS, total_size);
850 entry->size = total_size;
852 memcpy (&entry->data, oldptr, size);
854 write_data_item (handle->type_table, (guint8 *) entry);
856 if (max_size > BUFSIZ)
859 mono_debugger_unlock ();
862 static MonoDebugMethodJitInfo *
863 find_method (MonoMethod *method, MonoDomain *domain)
865 MonoDebugDataTable *table;
866 MonoDebugMethodAddress *address;
868 table = lookup_data_table (domain);
869 address = g_hash_table_lookup (table->method_address_hash, method);
874 return mono_debug_read_method (address);
877 MonoDebugMethodJitInfo *
878 mono_debug_find_method (MonoMethod *method, MonoDomain *domain)
880 MonoDebugMethodJitInfo *res;
882 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
885 mono_debugger_lock ();
886 res = find_method (method, domain);
887 mono_debugger_unlock ();
891 struct LookupMethodAddressData
894 MonoDebugMethodHeader *result;
898 lookup_method_address_func (gpointer key, gpointer value, gpointer user_data)
900 MonoDebugDataTable *table = (MonoDebugDataTable *) value;
901 struct LookupMethodAddressData *data = (struct LookupMethodAddressData *) user_data;
902 MonoDebugMethodHeader *header;
904 header = g_hash_table_lookup (table->method_hash, data->method);
906 data->result = header;
909 MonoDebugMethodAddressList *
910 mono_debug_lookup_method_addresses (MonoMethod *method)
912 MonoDebugMethodAddressList *info;
913 MonoDebugMethodHeader *header = NULL;
914 struct LookupMethodAddressData data;
915 MonoMethod *declaring;
920 g_assert (mono_debug_debugger_version == 4);
922 mono_debugger_lock ();
924 declaring = method->is_inflated ? ((MonoMethodInflated *) method)->declaring : method;
926 data.method = declaring;
929 g_hash_table_foreach (data_table_hash, lookup_method_address_func, &data);
930 header = data.result;
933 mono_debugger_unlock ();
937 count = g_slist_length (header->address_list) + 1;
938 size = sizeof (MonoDebugMethodAddressList) + count * sizeof (gpointer);
940 info = g_malloc0 (size);
946 WRITE_UNALIGNED (gpointer, ptr, header);
947 ptr += sizeof (gpointer);
949 for (list = header->address_list; list; list = list->next) {
950 WRITE_UNALIGNED (gpointer, ptr, list->data);
951 ptr += sizeof (gpointer);
954 mono_debugger_unlock ();
959 il_offset_from_address (MonoMethod *method, MonoDomain *domain, guint32 native_offset)
961 MonoDebugMethodJitInfo *jit;
964 jit = find_method (method, domain);
965 if (!jit || !jit->line_numbers)
966 goto cleanup_and_fail;
968 for (i = jit->num_line_numbers - 1; i >= 0; i--) {
969 MonoDebugLineNumberEntry lne = jit->line_numbers [i];
971 if (lne.native_offset <= native_offset) {
972 mono_debug_free_method_jit_info (jit);
973 return lne.il_offset;
978 mono_debug_free_method_jit_info (jit);
983 * mono_debug_lookup_source_location:
984 * @address: Native offset within the @method's machine code.
986 * Lookup the source code corresponding to the machine instruction located at
987 * native offset @address within @method.
989 * The returned `MonoDebugSourceLocation' contains both file / line number
990 * information and the corresponding IL offset. It must be freed by
991 * mono_debug_free_source_location().
993 MonoDebugSourceLocation *
994 mono_debug_lookup_source_location (MonoMethod *method, guint32 address, MonoDomain *domain)
996 MonoDebugMethodInfo *minfo;
997 MonoDebugSourceLocation *location;
1000 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
1003 mono_debugger_lock ();
1004 minfo = _mono_debug_lookup_method (method);
1005 if (!minfo || !minfo->handle || !minfo->handle->symfile || !minfo->handle->symfile->offset_table) {
1006 mono_debugger_unlock ();
1010 offset = il_offset_from_address (method, domain, address);
1012 mono_debugger_unlock ();
1016 location = mono_debug_symfile_lookup_location (minfo, offset);
1017 mono_debugger_unlock ();
1022 * mono_debug_lookup_locals:
1024 * Return information about the local variables of MINFO.
1025 * NAMES and INDEXES are set to g_malloc-ed arrays containing the local names and
1027 * Returns: the number of elements placed into the arrays, or -1 if there is no
1028 * local variable info.
1031 mono_debug_lookup_locals (MonoMethod *method, char ***names, int **indexes)
1033 MonoDebugMethodInfo *minfo;
1039 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
1042 mono_debugger_lock ();
1043 minfo = _mono_debug_lookup_method (method);
1044 if (!minfo || !minfo->handle || !minfo->handle->symfile || !minfo->handle->symfile->offset_table) {
1045 mono_debugger_unlock ();
1049 res = mono_debug_symfile_lookup_locals (minfo, names, indexes);
1050 mono_debugger_unlock ();
1056 * mono_debug_free_source_location:
1057 * @location: A `MonoDebugSourceLocation'.
1059 * Frees the @location.
1062 mono_debug_free_source_location (MonoDebugSourceLocation *location)
1065 g_free (location->source_file);
1071 * mono_debug_print_stack_frame:
1072 * @native_offset: Native offset within the @method's machine code.
1074 * Conventient wrapper around mono_debug_lookup_source_location() which can be
1075 * used if you only want to use the location to print a stack frame.
1078 mono_debug_print_stack_frame (MonoMethod *method, guint32 native_offset, MonoDomain *domain)
1080 MonoDebugSourceLocation *location;
1081 gchar *fname, *ptr, *res;
1084 fname = mono_method_full_name (method, TRUE);
1085 for (ptr = fname; *ptr; ptr++) {
1086 if (*ptr == ':') *ptr = '.';
1089 location = mono_debug_lookup_source_location (method, native_offset, domain);
1092 if (mono_debug_initialized) {
1093 mono_debugger_lock ();
1094 offset = il_offset_from_address (method, domain, native_offset);
1095 mono_debugger_unlock ();
1101 res = g_strdup_printf ("at %s <0x%05x>", fname, native_offset);
1103 res = g_strdup_printf ("at %s <IL 0x%05x, 0x%05x>", fname, offset, native_offset);
1108 res = g_strdup_printf ("at %s [0x%05x] in %s:%d", fname, location->il_offset,
1109 location->source_file, location->row);
1112 mono_debug_free_source_location (location);
1117 mono_debug_list_add (MonoDebugList **list, gconstpointer data)
1119 MonoDebugList *element, **ptr;
1121 element = g_new0 (MonoDebugList, 1);
1122 element->data = data;
1124 for (ptr = list; *ptr; ptr = &(*ptr)->next)
1131 mono_debug_list_remove (MonoDebugList **list, gconstpointer data)
1133 MonoDebugList **ptr;
1134 MonoDebugList *next;
1136 for (ptr = list; *ptr; ptr = &(*ptr)->next) {
1137 if ((*ptr)->data != data)
1140 next = (*ptr)->next;