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)
9 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
13 #include <mono/metadata/assembly.h>
14 #include <mono/metadata/tabledefs.h>
15 #include <mono/metadata/tokentype.h>
16 #include <mono/metadata/appdomain.h>
17 #include <mono/metadata/class-internals.h>
18 #include <mono/metadata/mono-debug.h>
19 #include <mono/metadata/mono-debug-debugger.h>
20 #include <mono/metadata/mono-endian.h>
21 #include <mono/metadata/gc-internal.h>
22 #include <mono/metadata/mempool.h>
25 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
27 #if NO_UNALIGNED_ACCESS
28 #define WRITE_UNALIGNED(type, addr, val) \
29 memcpy(addr, &val, sizeof(type))
30 #define READ_UNALIGNED(type, addr, val) \
31 memcpy(&val, addr, sizeof(type))
33 #define WRITE_UNALIGNED(type, addr, val) \
34 (*(type *)(addr) = (val))
35 #define READ_UNALIGNED(type, addr, val) \
36 val = (*(type *)(addr))
39 /* This contains per-domain info */
40 struct _MonoDebugDataTable {
42 GHashTable *method_address_hash;
45 /* This contains JIT debugging information about a method in serialized format */
46 struct _MonoDebugMethodAddress {
47 const guint8 *code_start;
49 guint8 data [MONO_ZERO_LEN_ARRAY];
52 static MonoDebugFormat mono_debug_format = MONO_DEBUG_FORMAT_NONE;
54 static gboolean mono_debug_initialized = FALSE;
55 /* Maps MonoImage -> MonoMonoDebugHandle */
56 static GHashTable *mono_debug_handles;
57 /* Maps MonoDomain -> MonoDataTable */
58 static GHashTable *data_table_hash;
60 static mono_mutex_t debugger_lock_mutex;
62 static int initialized = 0;
63 static gboolean is_attached = FALSE;
65 static MonoDebugHandle *mono_debug_open_image (MonoImage *image, const guint8 *raw_contents, int size);
67 static MonoDebugHandle *mono_debug_get_image (MonoImage *image);
68 static void mono_debug_add_assembly (MonoAssembly *assembly,
71 static MonoDebugHandle *open_symfile_from_bundle (MonoImage *image);
73 static MonoDebugDataTable *
74 create_data_table (MonoDomain *domain)
76 MonoDebugDataTable *table;
78 table = g_new0 (MonoDebugDataTable, 1);
80 table->mp = mono_mempool_new ();
81 table->method_address_hash = g_hash_table_new (NULL, NULL);
84 g_hash_table_insert (data_table_hash, domain, table);
90 free_data_table (MonoDebugDataTable *table)
92 mono_mempool_destroy (table->mp);
93 g_hash_table_destroy (table->method_address_hash);
98 static MonoDebugDataTable *
99 lookup_data_table (MonoDomain *domain)
101 MonoDebugDataTable *table;
103 table = g_hash_table_lookup (data_table_hash, domain);
105 g_error ("lookup_data_table () failed for %p\n", domain);
112 free_debug_handle (MonoDebugHandle *handle)
115 mono_debug_close_mono_symbol_file (handle->symfile);
116 /* decrease the refcount added with mono_image_addref () */
117 mono_image_close (handle->image);
122 * Initialize debugging support.
124 * This method must be called after loading corlib,
125 * but before opening the application's main assembly because we need to set some
129 mono_debug_init (MonoDebugFormat format)
131 g_assert (!mono_debug_initialized);
132 if (format == MONO_DEBUG_FORMAT_DEBUGGER)
133 g_error ("The mdb debugger is no longer supported.");
135 mono_debug_initialized = TRUE;
136 mono_debug_format = format;
138 mono_debugger_initialize ();
140 mono_debugger_lock ();
142 mono_debug_handles = g_hash_table_new_full
143 (NULL, NULL, NULL, (GDestroyNotify) free_debug_handle);
145 data_table_hash = g_hash_table_new_full (
146 NULL, NULL, NULL, (GDestroyNotify) free_data_table);
148 mono_install_assembly_load_hook (mono_debug_add_assembly, NULL);
150 mono_debugger_unlock ();
154 mono_debug_open_image_from_memory (MonoImage *image, const guint8 *raw_contents, int size)
156 if (!mono_debug_initialized)
159 mono_debug_open_image (image, raw_contents, size);
163 mono_debug_cleanup (void)
165 if (mono_debug_handles)
166 g_hash_table_destroy (mono_debug_handles);
167 mono_debug_handles = NULL;
169 if (data_table_hash) {
170 g_hash_table_destroy (data_table_hash);
171 data_table_hash = NULL;
176 mono_debug_domain_create (MonoDomain *domain)
178 MonoDebugDataTable *table;
180 if (!mono_debug_initialized)
183 mono_debugger_lock ();
185 table = create_data_table (domain);
187 mono_debugger_unlock ();
191 mono_debug_domain_unload (MonoDomain *domain)
193 MonoDebugDataTable *table;
195 if (!mono_debug_initialized)
198 mono_debugger_lock ();
200 table = g_hash_table_lookup (data_table_hash, domain);
202 g_warning (G_STRLOC ": unloading unknown domain %p / %d",
203 domain, mono_domain_get_id (domain));
204 mono_debugger_unlock ();
208 g_hash_table_remove (data_table_hash, domain);
210 mono_debugger_unlock ();
214 * LOCKING: Assumes the debug lock is held.
216 static MonoDebugHandle *
217 mono_debug_get_image (MonoImage *image)
219 return g_hash_table_lookup (mono_debug_handles, image);
223 mono_debug_close_image (MonoImage *image)
225 MonoDebugHandle *handle;
227 if (!mono_debug_initialized)
230 mono_debugger_lock ();
232 handle = mono_debug_get_image (image);
234 mono_debugger_unlock ();
238 g_hash_table_remove (mono_debug_handles, image);
240 mono_debugger_unlock ();
243 static MonoDebugHandle *
244 mono_debug_open_image (MonoImage *image, const guint8 *raw_contents, int size)
246 MonoDebugHandle *handle;
248 if (mono_image_is_dynamic (image))
251 mono_debugger_lock ();
253 handle = mono_debug_get_image (image);
254 if (handle != NULL) {
255 mono_debugger_unlock ();
259 handle = g_new0 (MonoDebugHandle, 1);
261 handle->image = image;
262 mono_image_addref (image);
264 handle->symfile = mono_debug_open_mono_symbols (
265 handle, raw_contents, size, FALSE);
267 g_hash_table_insert (mono_debug_handles, image, handle);
269 mono_debugger_unlock ();
275 mono_debug_add_assembly (MonoAssembly *assembly, gpointer user_data)
277 MonoDebugHandle *handle;
280 mono_debugger_lock ();
281 image = mono_assembly_get_image (assembly);
282 handle = open_symfile_from_bundle (image);
284 mono_debug_open_image (image, NULL, 0);
285 mono_debugger_unlock ();
288 struct LookupMethodData
290 MonoDebugMethodInfo *minfo;
295 lookup_method_func (gpointer key, gpointer value, gpointer user_data)
297 MonoDebugHandle *handle = (MonoDebugHandle *) value;
298 struct LookupMethodData *data = (struct LookupMethodData *) user_data;
304 data->minfo = mono_debug_symfile_lookup_method (handle, data->method);
307 static MonoDebugMethodInfo *
308 mono_debug_lookup_method_internal (MonoMethod *method)
310 struct LookupMethodData data;
313 data.method = method;
315 if (!mono_debug_handles)
318 g_hash_table_foreach (mono_debug_handles, lookup_method_func, &data);
323 * mono_debug_lookup_method:
325 * Lookup symbol file information for the method @method. The returned
326 * `MonoDebugMethodInfo' is a private structure, but it can be passed to
327 * mono_debug_symfile_lookup_location().
329 MonoDebugMethodInfo *
330 mono_debug_lookup_method (MonoMethod *method)
332 MonoDebugMethodInfo *minfo;
334 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
337 mono_debugger_lock ();
338 minfo = mono_debug_lookup_method_internal (method);
339 mono_debugger_unlock ();
350 lookup_image_func (gpointer key, gpointer value, gpointer user_data)
352 MonoDebugHandle *handle = (MonoDebugHandle *) value;
353 LookupImageData *data = (LookupImageData *) user_data;
358 if (handle->image == data->image && handle->symfile)
363 mono_debug_image_has_debug_info (MonoImage *image)
365 LookupImageData data;
367 if (!mono_debug_handles)
370 memset (&data, 0, sizeof (data));
373 mono_debugger_lock ();
374 g_hash_table_foreach (mono_debug_handles, lookup_image_func, &data);
375 mono_debugger_unlock ();
380 write_leb128 (guint32 value, guint8 *ptr, guint8 **rptr)
383 guint8 byte = value & 0x7f;
394 write_sleb128 (gint32 value, guint8 *ptr, guint8 **rptr)
399 guint8 byte = value & 0x7f;
402 if (((value == 0) && ((byte & 0x40) == 0)) || ((value == -1) && (byte & 0x40)))
413 write_variable (MonoDebugVarInfo *var, guint8 *ptr, guint8 **rptr)
415 write_leb128 (var->index, ptr, &ptr);
416 write_sleb128 (var->offset, ptr, &ptr);
417 write_leb128 (var->size, ptr, &ptr);
418 write_leb128 (var->begin_scope, ptr, &ptr);
419 write_leb128 (var->end_scope, ptr, &ptr);
420 WRITE_UNALIGNED (gpointer, ptr, var->type);
421 ptr += sizeof (gpointer);
425 MonoDebugMethodAddress *
426 mono_debug_add_method (MonoMethod *method, MonoDebugMethodJitInfo *jit, MonoDomain *domain)
428 MonoDebugDataTable *table;
429 MonoDebugMethodAddress *address;
430 MonoDebugMethodInfo *minfo;
431 MonoDebugHandle *handle;
432 guint8 buffer [BUFSIZ];
433 guint8 *ptr, *oldptr;
434 guint32 i, size, total_size, max_size;
436 mono_debugger_lock ();
438 table = lookup_data_table (domain);
440 handle = mono_debug_get_image (method->klass->image);
441 minfo = mono_debug_lookup_method_internal (method);
443 max_size = (5 * 5) + 1 + (10 * jit->num_line_numbers) +
444 (25 + sizeof (gpointer)) * (1 + jit->num_params + jit->num_locals);
446 if (max_size > BUFSIZ)
447 ptr = oldptr = g_malloc (max_size);
449 ptr = oldptr = buffer;
451 write_leb128 (jit->prologue_end, ptr, &ptr);
452 write_leb128 (jit->epilogue_begin, ptr, &ptr);
454 write_leb128 (jit->num_line_numbers, ptr, &ptr);
455 for (i = 0; i < jit->num_line_numbers; i++) {
456 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
458 write_sleb128 (lne->il_offset, ptr, &ptr);
459 write_sleb128 (lne->native_offset, ptr, &ptr);
462 *ptr++ = jit->this_var ? 1 : 0;
464 write_variable (jit->this_var, ptr, &ptr);
466 write_leb128 (jit->num_params, ptr, &ptr);
467 for (i = 0; i < jit->num_params; i++)
468 write_variable (&jit->params [i], ptr, &ptr);
470 write_leb128 (jit->num_locals, ptr, &ptr);
471 for (i = 0; i < jit->num_locals; i++)
472 write_variable (&jit->locals [i], ptr, &ptr);
474 *ptr++ = jit->gsharedvt_info_var ? 1 : 0;
475 if (jit->gsharedvt_info_var) {
476 write_variable (jit->gsharedvt_info_var, ptr, &ptr);
477 write_variable (jit->gsharedvt_locals_var, ptr, &ptr);
481 g_assert (size < max_size);
482 total_size = size + sizeof (MonoDebugMethodAddress);
484 if (method_is_dynamic (method)) {
485 address = g_malloc0 (total_size);
487 address = mono_mempool_alloc (table->mp, total_size);
490 address->code_start = jit->code_start;
491 address->code_size = jit->code_size;
493 memcpy (&address->data, oldptr, size);
494 if (max_size > BUFSIZ)
497 g_hash_table_insert (table->method_address_hash, method, address);
499 mono_debugger_unlock ();
504 mono_debug_remove_method (MonoMethod *method, MonoDomain *domain)
506 MonoDebugDataTable *table;
507 MonoDebugMethodAddress *address;
509 if (!mono_debug_initialized)
512 g_assert (method_is_dynamic (method));
514 mono_debugger_lock ();
516 table = lookup_data_table (domain);
518 address = g_hash_table_lookup (table->method_address_hash, method);
522 g_hash_table_remove (table->method_address_hash, method);
524 mono_debugger_unlock ();
528 mono_debug_add_delegate_trampoline (gpointer code, int size)
532 static inline guint32
533 read_leb128 (guint8 *ptr, guint8 **rptr)
535 guint32 result = 0, shift = 0;
538 guint8 byte = *ptr++;
540 result |= (byte & 0x7f) << shift;
541 if ((byte & 0x80) == 0)
551 read_sleb128 (guint8 *ptr, guint8 **rptr)
557 guint8 byte = *ptr++;
559 result |= (byte & 0x7f) << shift;
565 if ((shift < 32) && (byte & 0x40))
566 result |= - (1 << shift);
575 read_variable (MonoDebugVarInfo *var, guint8 *ptr, guint8 **rptr)
577 var->index = read_leb128 (ptr, &ptr);
578 var->offset = read_sleb128 (ptr, &ptr);
579 var->size = read_leb128 (ptr, &ptr);
580 var->begin_scope = read_leb128 (ptr, &ptr);
581 var->end_scope = read_leb128 (ptr, &ptr);
582 READ_UNALIGNED (gpointer, ptr, var->type);
583 ptr += sizeof (gpointer);
588 mono_debug_free_method_jit_info (MonoDebugMethodJitInfo *jit)
592 g_free (jit->line_numbers);
593 g_free (jit->this_var);
594 g_free (jit->params);
595 g_free (jit->locals);
596 g_free (jit->gsharedvt_info_var);
597 g_free (jit->gsharedvt_locals_var);
601 static MonoDebugMethodJitInfo *
602 mono_debug_read_method (MonoDebugMethodAddress *address)
604 MonoDebugMethodJitInfo *jit;
608 jit = g_new0 (MonoDebugMethodJitInfo, 1);
609 jit->code_start = address->code_start;
610 jit->code_size = address->code_size;
612 ptr = (guint8 *) &address->data;
614 jit->prologue_end = read_leb128 (ptr, &ptr);
615 jit->epilogue_begin = read_leb128 (ptr, &ptr);
617 jit->num_line_numbers = read_leb128 (ptr, &ptr);
618 jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
619 for (i = 0; i < jit->num_line_numbers; i++) {
620 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
622 lne->il_offset = read_sleb128 (ptr, &ptr);
623 lne->native_offset = read_sleb128 (ptr, &ptr);
627 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
628 read_variable (jit->this_var, ptr, &ptr);
631 jit->num_params = read_leb128 (ptr, &ptr);
632 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
633 for (i = 0; i < jit->num_params; i++)
634 read_variable (&jit->params [i], ptr, &ptr);
636 jit->num_locals = read_leb128 (ptr, &ptr);
637 jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
638 for (i = 0; i < jit->num_locals; i++)
639 read_variable (&jit->locals [i], ptr, &ptr);
642 jit->gsharedvt_info_var = g_new0 (MonoDebugVarInfo, 1);
643 jit->gsharedvt_locals_var = g_new0 (MonoDebugVarInfo, 1);
644 read_variable (jit->gsharedvt_info_var, ptr, &ptr);
645 read_variable (jit->gsharedvt_locals_var, ptr, &ptr);
651 static MonoDebugMethodJitInfo *
652 find_method (MonoMethod *method, MonoDomain *domain)
654 MonoDebugDataTable *table;
655 MonoDebugMethodAddress *address;
657 table = lookup_data_table (domain);
658 address = g_hash_table_lookup (table->method_address_hash, method);
663 return mono_debug_read_method (address);
666 MonoDebugMethodJitInfo *
667 mono_debug_find_method (MonoMethod *method, MonoDomain *domain)
669 MonoDebugMethodJitInfo *res;
671 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
674 mono_debugger_lock ();
675 res = find_method (method, domain);
676 mono_debugger_unlock ();
680 MonoDebugMethodAddressList *
681 mono_debug_lookup_method_addresses (MonoMethod *method)
683 g_assert_not_reached ();
688 il_offset_from_address (MonoMethod *method, MonoDomain *domain, guint32 native_offset)
690 MonoDebugMethodJitInfo *jit;
693 jit = find_method (method, domain);
694 if (!jit || !jit->line_numbers)
695 goto cleanup_and_fail;
697 for (i = jit->num_line_numbers - 1; i >= 0; i--) {
698 MonoDebugLineNumberEntry lne = jit->line_numbers [i];
700 if (lne.native_offset <= native_offset) {
701 mono_debug_free_method_jit_info (jit);
702 return lne.il_offset;
707 mono_debug_free_method_jit_info (jit);
712 * mono_debug_il_offset_from_address:
714 * Compute the IL offset corresponding to NATIVE_OFFSET inside the native
715 * code of METHOD in DOMAIN.
718 mono_debug_il_offset_from_address (MonoMethod *method, MonoDomain *domain, guint32 native_offset)
722 mono_debugger_lock ();
724 res = il_offset_from_address (method, domain, native_offset);
726 mono_debugger_unlock ();
732 * mono_debug_lookup_source_location:
733 * @address: Native offset within the @method's machine code.
735 * Lookup the source code corresponding to the machine instruction located at
736 * native offset @address within @method.
738 * The returned `MonoDebugSourceLocation' contains both file / line number
739 * information and the corresponding IL offset. It must be freed by
740 * mono_debug_free_source_location().
742 MonoDebugSourceLocation *
743 mono_debug_lookup_source_location (MonoMethod *method, guint32 address, MonoDomain *domain)
745 MonoDebugMethodInfo *minfo;
746 MonoDebugSourceLocation *location;
749 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
752 mono_debugger_lock ();
753 minfo = mono_debug_lookup_method_internal (method);
754 if (!minfo || !minfo->handle || !minfo->handle->symfile || !mono_debug_symfile_is_loaded (minfo->handle->symfile)) {
755 mono_debugger_unlock ();
759 offset = il_offset_from_address (method, domain, address);
761 mono_debugger_unlock ();
765 location = mono_debug_symfile_lookup_location (minfo, offset);
766 mono_debugger_unlock ();
771 * mono_debug_lookup_locals:
773 * Return information about the local variables of MINFO.
774 * The result should be freed using mono_debug_symfile_free_locals ().
777 mono_debug_lookup_locals (MonoMethod *method)
779 MonoDebugMethodInfo *minfo;
780 MonoDebugLocalsInfo *res;
782 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
785 mono_debugger_lock ();
786 minfo = mono_debug_lookup_method_internal (method);
787 if (!minfo || !minfo->handle || !minfo->handle->symfile || !mono_debug_symfile_is_loaded (minfo->handle->symfile)) {
788 mono_debugger_unlock ();
792 res = mono_debug_symfile_lookup_locals (minfo);
793 mono_debugger_unlock ();
799 * mono_debug_free_source_location:
800 * @location: A `MonoDebugSourceLocation'.
802 * Frees the @location.
805 mono_debug_free_source_location (MonoDebugSourceLocation *location)
808 g_free (location->source_file);
814 * mono_debug_print_stack_frame:
815 * @native_offset: Native offset within the @method's machine code.
817 * Conventient wrapper around mono_debug_lookup_source_location() which can be
818 * used if you only want to use the location to print a stack frame.
821 mono_debug_print_stack_frame (MonoMethod *method, guint32 native_offset, MonoDomain *domain)
823 MonoDebugSourceLocation *location;
824 gchar *fname, *ptr, *res;
827 fname = mono_method_full_name (method, TRUE);
828 for (ptr = fname; *ptr; ptr++) {
829 if (*ptr == ':') *ptr = '.';
832 location = mono_debug_lookup_source_location (method, native_offset, domain);
835 if (mono_debug_initialized) {
836 mono_debugger_lock ();
837 offset = il_offset_from_address (method, domain, native_offset);
838 mono_debugger_unlock ();
844 res = g_strdup_printf ("at %s <0x%05x>", fname, native_offset);
846 res = g_strdup_printf ("at %s <IL 0x%05x, 0x%05x>", fname, offset, native_offset);
851 res = g_strdup_printf ("at %s [0x%05x] in %s:%d", fname, location->il_offset,
852 location->source_file, location->row);
855 mono_debug_free_source_location (location);
860 mono_set_is_debugger_attached (gboolean attached)
862 is_attached = attached;
866 mono_is_debugger_attached (void)
875 typedef struct _BundledSymfile BundledSymfile;
877 struct _BundledSymfile {
878 BundledSymfile *next;
880 const mono_byte *raw_contents;
884 static BundledSymfile *bundled_symfiles = NULL;
887 mono_register_symfile_for_assembly (const char *assembly_name, const mono_byte *raw_contents, int size)
889 BundledSymfile *bsymfile;
891 bsymfile = g_new0 (BundledSymfile, 1);
892 bsymfile->aname = assembly_name;
893 bsymfile->raw_contents = raw_contents;
894 bsymfile->size = size;
895 bsymfile->next = bundled_symfiles;
896 bundled_symfiles = bsymfile;
899 static MonoDebugHandle *
900 open_symfile_from_bundle (MonoImage *image)
902 BundledSymfile *bsymfile;
904 for (bsymfile = bundled_symfiles; bsymfile; bsymfile = bsymfile->next) {
905 if (strcmp (bsymfile->aname, image->module_name))
908 return mono_debug_open_image (image, bsymfile->raw_contents, bsymfile->size);
915 mono_debugger_lock (void)
917 g_assert (initialized);
918 mono_mutex_lock (&debugger_lock_mutex);
922 mono_debugger_unlock (void)
924 g_assert (initialized);
925 mono_mutex_unlock (&debugger_lock_mutex);
929 mono_debugger_initialize ()
931 mono_mutex_init_recursive (&debugger_lock_mutex);
936 * mono_debug_enabled:
938 * Returns true is debug information is enabled. This doesn't relate if a debugger is present or not.
941 mono_debug_enabled (void)
943 return mono_debug_format != MONO_DEBUG_FORMAT_NONE;