2 * debug-mono-symfile.c:
5 * Mono Project (http://www.mono-project.com)
7 * Copyright (C) 2005-2008 Novell, Inc. (http://www.novell.com)
8 * Copyright 2012 Xamarin Inc (http://www.xamarin.com)
9 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
17 #ifdef HAVE_SYS_PARAM_H
18 #include <sys/param.h>
21 #include <mono/metadata/metadata.h>
22 #include <mono/metadata/tabledefs.h>
23 #include <mono/metadata/tokentype.h>
24 #include <mono/metadata/appdomain.h>
25 #include <mono/metadata/exception.h>
26 #include <mono/metadata/debug-helpers.h>
27 #include <mono/metadata/mono-debug.h>
28 #include <mono/metadata/debug-mono-symfile.h>
29 #include <mono/metadata/mono-debug-debugger.h>
30 #include <mono/metadata/mono-endian.h>
31 #include <mono/metadata/metadata-internals.h>
32 #include <mono/metadata/class-internals.h>
33 #include <mono/utils/mono-mmap.h>
34 #include <mono/utils/bsearch.h>
41 #define RANGE_TABLE_CHUNK_SIZE 256
42 #define CLASS_TABLE_CHUNK_SIZE 256
43 #define TYPE_TABLE_PTR_CHUNK_SIZE 256
44 #define TYPE_TABLE_CHUNK_SIZE 65536
46 struct _MonoSymbolFile {
47 const uint8_t *raw_contents;
48 int raw_contents_size;
49 void *raw_contents_handle;
53 GHashTable *method_hash;
54 GHashTable *source_hash;
55 MonoSymbolFileOffsetTable *offset_table;
56 gboolean was_loaded_from_memory;
60 free_method_info (MonoDebugMethodInfo *minfo)
66 free_source_info (MonoDebugSourceInfo *sinfo)
68 g_free (sinfo->source_file);
75 load_symfile (MonoDebugHandle *handle, MonoSymbolFile *symfile, mono_bool in_the_debugger)
77 const char *ptr, *start;
82 ptr = start = (const char*)symfile->raw_contents;
87 ptr += sizeof(uint64_t);
88 if (magic != MONO_SYMBOL_FILE_MAGIC) {
90 g_warning ("Symbol file %s is not a mono symbol file", symfile->filename);
95 ptr += sizeof(uint32_t);
97 ptr += sizeof(uint32_t);
100 * 50.0 is the frozen version for Mono 2.0.
102 * Nobody except me (Martin) is allowed to check the minor version.
104 if (major != MONO_SYMBOL_FILE_MAJOR_VERSION) {
105 if (!in_the_debugger)
106 g_warning ("Symbol file %s has incorrect version (expected %d.%d, got %d)",
107 symfile->filename, MONO_SYMBOL_FILE_MAJOR_VERSION,
108 MONO_SYMBOL_FILE_MINOR_VERSION, major);
112 guid = mono_guid_to_string ((const uint8_t *) ptr);
115 if (strcmp (handle->image->guid, guid)) {
116 if (!in_the_debugger)
117 g_warning ("Symbol file %s doesn't match image %s", symfile->filename,
118 handle->image->name);
124 symfile->major_version = major;
125 symfile->minor_version = minor;
127 symfile->offset_table = (MonoSymbolFileOffsetTable *) ptr;
129 symfile->method_hash = g_hash_table_new_full (
130 NULL, NULL, NULL, (GDestroyNotify) free_method_info);
132 symfile->source_hash = g_hash_table_new_full (
133 NULL, NULL, NULL, (GDestroyNotify) free_source_info);
140 mono_debug_open_mono_symbols (MonoDebugHandle *handle, const uint8_t *raw_contents,
141 int size, gboolean in_the_debugger)
143 MonoSymbolFile *symfile;
145 mono_debugger_lock ();
146 symfile = g_new0 (MonoSymbolFile, 1);
148 if (raw_contents != NULL) {
150 symfile->raw_contents_size = size;
151 symfile->raw_contents = p = (unsigned char *)g_malloc (size);
152 memcpy (p, raw_contents, size);
153 symfile->filename = g_strdup_printf ("LoadedFromMemory");
154 symfile->was_loaded_from_memory = TRUE;
158 symfile->filename = g_strdup_printf ("%s.mdb", mono_image_get_filename (handle->image));
159 symfile->was_loaded_from_memory = FALSE;
160 if ((f = mono_file_map_open (symfile->filename))) {
161 symfile->raw_contents_size = mono_file_map_size (f);
162 if (symfile->raw_contents_size == 0) {
163 if (!in_the_debugger)
164 g_warning ("stat of %s failed: %s",
165 symfile->filename, g_strerror (errno));
167 symfile->raw_contents = (const unsigned char *)mono_file_map (symfile->raw_contents_size, MONO_MMAP_READ|MONO_MMAP_PRIVATE, mono_file_map_fd (f), 0, &symfile->raw_contents_handle);
170 mono_file_map_close (f);
174 if (load_symfile (handle, symfile, in_the_debugger)) {
175 mono_debugger_unlock ();
177 } else if (!in_the_debugger) {
178 mono_debug_close_mono_symbol_file (symfile);
179 mono_debugger_unlock ();
183 mono_debugger_unlock ();
188 mono_debug_close_mono_symbol_file (MonoSymbolFile *symfile)
193 mono_debugger_lock ();
194 if (symfile->method_hash)
195 g_hash_table_destroy (symfile->method_hash);
197 if (symfile->raw_contents) {
198 if (symfile->was_loaded_from_memory)
199 g_free ((gpointer)symfile->raw_contents);
201 mono_file_unmap ((gpointer) symfile->raw_contents, symfile->raw_contents_handle);
204 if (symfile->filename)
205 g_free (symfile->filename);
207 mono_debugger_unlock ();
211 mono_debug_symfile_is_loaded (MonoSymbolFile *symfile)
213 return symfile && symfile->offset_table;
218 read_leb128 (const uint8_t *ptr, const uint8_t **rptr)
227 ret = ret | ((b & 0x7f) << shift);
229 } while ((b & 0x80) == 0x80);
238 read_string (const uint8_t *ptr, const uint8_t **endp)
241 int len = read_leb128 (ptr, &ptr);
243 s = g_filename_from_utf8 ((const char *) ptr, len, NULL, NULL, NULL);
251 MonoSymbolFile *symfile;
252 int line_base, line_range, max_address_incr;
254 uint32_t last_line, last_file, last_offset;
256 int line, file, offset;
261 check_line (StatementMachine *stm, int offset, MonoDebugSourceLocation **location)
263 gchar *source_file = NULL;
265 if (stm->offset <= offset) {
266 stm->last_offset = stm->offset;
267 stm->last_file = stm->file;
268 if (stm->line != 0xfeefee)
269 stm->last_line = stm->line;
273 if (stm->last_file) {
274 int offset = read32(&(stm->symfile->offset_table->_source_table_offset)) +
275 (stm->last_file - 1) * sizeof (MonoSymbolFileSourceEntry);
276 MonoSymbolFileSourceEntry *se = (MonoSymbolFileSourceEntry *)
277 (stm->symfile->raw_contents + offset);
279 source_file = read_string (stm->symfile->raw_contents + read32(&(se->_data_offset)), NULL);
282 if (stm->last_line == 0) {
284 * The IL offset is less than the first IL offset which has a corresponding
291 *location = g_new0 (MonoDebugSourceLocation, 1);
292 (*location)->source_file = source_file;
293 (*location)->row = stm->last_line;
294 (*location)->il_offset = stm->last_offset;
299 * mono_debug_symfile_lookup_location:
300 * @minfo: A `MonoDebugMethodInfo' which can be retrieved by
301 * mono_debug_lookup_method().
302 * @offset: IL offset within the corresponding method's CIL code.
304 * This function is similar to mono_debug_lookup_location(), but we
305 * already looked up the method and also already did the
306 * `native address -> IL offset' mapping.
308 MonoDebugSourceLocation *
309 mono_debug_symfile_lookup_location (MonoDebugMethodInfo *minfo, uint32_t offset)
311 MonoDebugSourceLocation *location = NULL;
312 MonoSymbolFile *symfile;
313 const unsigned char *ptr;
314 StatementMachine stm;
316 #define DW_LNS_copy 1
317 #define DW_LNS_advance_pc 2
318 #define DW_LNS_advance_line 3
319 #define DW_LNS_set_file 4
320 #define DW_LNS_const_add_pc 8
322 #define DW_LNE_end_sequence 1
323 #define DW_LNE_MONO_negate_is_hidden 0x40
325 #define DW_LNE_MONO__extensions_start 0x40
326 #define DW_LNE_MONO__extensions_end 0x7f
328 if ((symfile = minfo->handle->symfile) == NULL)
331 stm.line_base = read32 (&symfile->offset_table->_line_number_table_line_base);
332 stm.line_range = read32 (&symfile->offset_table->_line_number_table_line_range);
333 stm.opcode_base = (uint8_t) read32 (&symfile->offset_table->_line_number_table_opcode_base);
334 stm.max_address_incr = (255 - stm.opcode_base) / stm.line_range;
336 mono_debugger_lock ();
338 ptr = symfile->raw_contents + minfo->lnt_offset;
340 stm.symfile = symfile;
341 stm.offset = stm.last_offset = 0;
347 stm.is_hidden = FALSE;
350 uint8_t opcode = *ptr++;
353 uint8_t size = *ptr++;
354 const unsigned char *end_ptr = ptr + size;
358 if (opcode == DW_LNE_end_sequence) {
359 if (check_line (&stm, -1, &location))
362 } else if (opcode == DW_LNE_MONO_negate_is_hidden) {
363 stm.is_hidden = !stm.is_hidden;
364 } else if ((opcode >= DW_LNE_MONO__extensions_start) &&
365 (opcode <= DW_LNE_MONO__extensions_end)) {
366 ; // reserved for future extensions
368 g_warning ("Unknown extended opcode %x in LNT", opcode);
373 } else if (opcode < stm.opcode_base) {
376 if (check_line (&stm, offset, &location))
379 case DW_LNS_advance_pc:
380 stm.offset += read_leb128 (ptr, &ptr);
382 case DW_LNS_advance_line:
383 stm.line += read_leb128 (ptr, &ptr);
385 case DW_LNS_set_file:
386 stm.file = read_leb128 (ptr, &ptr);
388 case DW_LNS_const_add_pc:
389 stm.offset += stm.max_address_incr;
392 g_warning ("Unknown standard opcode %x in LNT", opcode);
396 opcode -= stm.opcode_base;
398 stm.offset += opcode / stm.line_range;
399 stm.line += stm.line_base + (opcode % stm.line_range);
401 if (check_line (&stm, offset, &location))
407 mono_debugger_unlock ();
411 mono_debugger_unlock ();
416 add_line (StatementMachine *stm, GPtrArray *il_offset_array, GPtrArray *line_number_array, GPtrArray *source_file_array, GPtrArray *hidden_array)
418 g_ptr_array_add (il_offset_array, GUINT_TO_POINTER (stm->offset));
419 g_ptr_array_add (line_number_array, GUINT_TO_POINTER (stm->line));
420 g_ptr_array_add (source_file_array, GUINT_TO_POINTER (stm->file));
421 g_ptr_array_add (hidden_array, GUINT_TO_POINTER (stm->is_hidden || stm->line <= 0));
423 if (!stm->is_hidden && !stm->first_file)
424 stm->first_file = stm->file;
428 * mono_debug_symfile_free_location:
430 * Free a MonoDebugSourceLocation returned by
431 * mono_debug_symfile_lookup_location
434 mono_debug_symfile_free_location (MonoDebugSourceLocation *location)
436 g_free (location->source_file);
441 * LOCKING: Assumes the debugger lock is held.
443 static MonoDebugSourceInfo*
444 get_source_info (MonoSymbolFile *symfile, int index)
446 MonoDebugSourceInfo *info;
448 info = (MonoDebugSourceInfo *)g_hash_table_lookup (symfile->source_hash, GUINT_TO_POINTER (index));
450 int offset = read32(&(symfile->offset_table->_source_table_offset)) +
451 (index - 1) * sizeof (MonoSymbolFileSourceEntry);
452 MonoSymbolFileSourceEntry *se = (MonoSymbolFileSourceEntry *)
453 (symfile->raw_contents + offset);
454 const uint8_t *ptr = symfile->raw_contents + read32(&(se->_data_offset));
456 info = g_new0 (MonoDebugSourceInfo, 1);
457 info->source_file = read_string (ptr, &ptr);
458 info->guid = (guint8 *)g_malloc0 (16);
459 memcpy (info->guid, ptr, 16);
461 info->hash = (guint8 *)g_malloc0 (16);
462 memcpy (info->hash, ptr, 16);
464 g_hash_table_insert (symfile->source_hash, GUINT_TO_POINTER (index), info);
470 LNT_FLAG_HAS_COLUMN_INFO = 1 << 1,
471 LNT_FLAG_HAS_END_INFO = 1 << 2,
472 } LineNumberTableFlags;
474 static LineNumberTableFlags
475 method_get_lnt_flags (MonoDebugMethodInfo *minfo)
477 MonoSymbolFile *symfile;
478 const unsigned char *ptr;
481 if ((symfile = minfo->handle->symfile) == NULL)
482 return (LineNumberTableFlags)0;
484 ptr = symfile->raw_contents + minfo->data_offset;
486 /* Has to read 'flags' which is preceeded by a bunch of other data */
487 /* compile_unit_index */
488 read_leb128 (ptr, &ptr);
489 /* local variable table offset */
490 read_leb128 (ptr, &ptr);
492 read_leb128 (ptr, &ptr);
493 /* code block table offset */
494 read_leb128 (ptr, &ptr);
495 /* scope variable table offset */
496 read_leb128 (ptr, &ptr);
497 /* real name offset */
498 read_leb128 (ptr, &ptr);
500 flags = read_leb128 (ptr, &ptr);
501 return (LineNumberTableFlags)flags;
505 * mono_debug_symfile_get_seq_points:
507 * On return, SOURCE_FILE_LIST will point to a GPtrArray of MonoDebugSourceFile
508 * structures, and SOURCE_FILES will contain indexes into this array.
509 * The MonoDebugSourceFile structures are owned by this module.
512 mono_debug_symfile_get_seq_points (MonoDebugMethodInfo *minfo, char **source_file, GPtrArray **source_file_list, int **source_files, MonoSymSeqPoint **seq_points, int *n_seq_points)
514 // FIXME: Unify this with mono_debug_symfile_lookup_location
515 MonoSymbolFile *symfile;
516 const unsigned char *ptr;
517 StatementMachine stm;
519 LineNumberTableFlags flags;
520 GPtrArray *il_offset_array, *line_number_array, *source_file_array, *hidden_array;
521 gboolean has_column_info, has_end_info;
522 MonoSymSeqPoint *sps;
524 if (source_file_list)
525 *source_file_list = NULL;
531 *source_files = NULL;
535 if ((symfile = minfo->handle->symfile) == NULL)
538 flags = method_get_lnt_flags (minfo);
539 has_column_info = (flags & LNT_FLAG_HAS_COLUMN_INFO) > 0;
540 has_end_info = (flags & LNT_FLAG_HAS_END_INFO) > 0;
542 il_offset_array = g_ptr_array_new ();
543 line_number_array = g_ptr_array_new ();
544 source_file_array = g_ptr_array_new ();
545 hidden_array = g_ptr_array_new();
547 stm.line_base = read32 (&symfile->offset_table->_line_number_table_line_base);
548 stm.line_range = read32 (&symfile->offset_table->_line_number_table_line_range);
549 stm.opcode_base = (uint8_t) read32 (&symfile->offset_table->_line_number_table_opcode_base);
550 stm.max_address_incr = (255 - stm.opcode_base) / stm.line_range;
552 mono_debugger_lock ();
554 ptr = symfile->raw_contents + minfo->lnt_offset;
556 stm.symfile = symfile;
557 stm.offset = stm.last_offset = 0;
563 stm.is_hidden = FALSE;
566 uint8_t opcode = *ptr++;
569 uint8_t size = *ptr++;
570 const unsigned char *end_ptr = ptr + size;
574 if (opcode == DW_LNE_end_sequence) {
575 if (il_offset_array->len == 0)
579 } else if (opcode == DW_LNE_MONO_negate_is_hidden) {
580 stm.is_hidden = !stm.is_hidden;
581 } else if ((opcode >= DW_LNE_MONO__extensions_start) &&
582 (opcode <= DW_LNE_MONO__extensions_end)) {
583 ; // reserved for future extensions
585 g_warning ("Unknown extended opcode %x in LNT", opcode);
590 } else if (opcode < stm.opcode_base) {
593 add_line (&stm, il_offset_array, line_number_array, source_file_array, hidden_array);
595 case DW_LNS_advance_pc:
596 stm.offset += read_leb128 (ptr, &ptr);
598 case DW_LNS_advance_line:
599 stm.line += read_leb128 (ptr, &ptr);
601 case DW_LNS_set_file:
602 stm.file = read_leb128 (ptr, &ptr);
604 case DW_LNS_const_add_pc:
605 stm.offset += stm.max_address_incr;
608 g_warning ("Unknown standard opcode %x in LNT", opcode);
609 g_assert_not_reached ();
612 opcode -= stm.opcode_base;
614 stm.offset += opcode / stm.line_range;
615 stm.line += stm.line_base + (opcode % stm.line_range);
617 add_line (&stm, il_offset_array, line_number_array, source_file_array, hidden_array);
621 if (!stm.file && stm.first_file)
622 stm.file = stm.first_file;
624 if (stm.file && source_file) {
625 int offset = read32(&(stm.symfile->offset_table->_source_table_offset)) +
626 (stm.file - 1) * sizeof (MonoSymbolFileSourceEntry);
627 MonoSymbolFileSourceEntry *se = (MonoSymbolFileSourceEntry *)
628 (stm.symfile->raw_contents + offset);
631 *source_file = read_string (stm.symfile->raw_contents + read32(&(se->_data_offset)), NULL);
634 if (source_file_list) {
635 int file, last_file = 0;
637 *source_file_list = g_ptr_array_new ();
639 *source_files = (int *)g_malloc (il_offset_array->len * sizeof (int));
641 for (i = 0; i < il_offset_array->len; ++i) {
642 file = GPOINTER_TO_UINT (g_ptr_array_index (source_file_array, i));
643 if (file && file != last_file) {
644 MonoDebugSourceInfo *info = get_source_info (symfile, file);
646 g_ptr_array_add (*source_file_list, info);
650 (*source_files) [i] = (*source_file_list)->len - 1;
652 if ((*source_file_list)->len == 0 && stm.file) {
653 MonoDebugSourceInfo *info = get_source_info (symfile, stm.file);
655 g_ptr_array_add (*source_file_list, info);
660 g_assert (seq_points);
662 n = il_offset_array->len;
663 for (i = 0; i < il_offset_array->len; i++) {
664 if (GPOINTER_TO_UINT (g_ptr_array_index (hidden_array, i))) {
670 *seq_points = sps = g_new0 (MonoSymSeqPoint, n);
672 for (i = 0; i < il_offset_array->len; ++i) {
673 MonoSymSeqPoint *sp = &(sps [j]);
674 if (!GPOINTER_TO_UINT (g_ptr_array_index (hidden_array, i))) {
675 sp->il_offset = GPOINTER_TO_UINT (g_ptr_array_index (il_offset_array, i));
676 sp->line = GPOINTER_TO_UINT (g_ptr_array_index (line_number_array, i));
684 if (has_column_info) {
686 for (i = 0; i < il_offset_array->len; ++i) {
687 MonoSymSeqPoint *sp = &(sps [j]);
688 int column = read_leb128 (ptr, &ptr);
689 if (!GPOINTER_TO_UINT (g_ptr_array_index (hidden_array, i))) {
698 for (i = 0; i < il_offset_array->len; ++i) {
699 MonoSymSeqPoint *sp = &(sps [j]);
700 int end_row, end_column = -1;
702 end_row = read_leb128 (ptr, &ptr);
703 if (end_row != 0xffffff) {
704 end_row += GPOINTER_TO_UINT (g_ptr_array_index (line_number_array, i));
705 end_column = read_leb128 (ptr, &ptr);
706 if (!GPOINTER_TO_UINT (g_ptr_array_index (hidden_array, i))) {
707 sp->end_line = end_row;
708 sp->end_column = end_column;
716 g_ptr_array_free (il_offset_array, TRUE);
717 g_ptr_array_free (line_number_array, TRUE);
718 g_ptr_array_free (hidden_array, TRUE);
720 mono_debugger_unlock ();
725 compare_method (const void *key, const void *object)
727 uint32_t token = GPOINTER_TO_UINT (key);
728 MonoSymbolFileMethodEntry *me = (MonoSymbolFileMethodEntry*)object;
730 return token - read32(&(me->_token));
733 MonoDebugMethodInfo *
734 mono_debug_symfile_lookup_method (MonoDebugHandle *handle, MonoMethod *method)
736 MonoSymbolFileMethodEntry *first_ie, *ie;
737 MonoDebugMethodInfo *minfo;
738 MonoSymbolFile *symfile = handle->symfile;
740 if (!symfile->method_hash)
743 if (handle->image != mono_class_get_image (mono_method_get_class (method)))
746 mono_debugger_lock ();
748 minfo = (MonoDebugMethodInfo *)g_hash_table_lookup (symfile->method_hash, method);
750 mono_debugger_unlock ();
754 first_ie = (MonoSymbolFileMethodEntry *)
755 (symfile->raw_contents + read32(&(symfile->offset_table->_method_table_offset)));
757 ie = (MonoSymbolFileMethodEntry *)mono_binary_search (GUINT_TO_POINTER (mono_method_get_token (method)), first_ie,
758 read32(&(symfile->offset_table->_method_count)),
759 sizeof (MonoSymbolFileMethodEntry), compare_method);
762 mono_debugger_unlock ();
766 minfo = g_new0 (MonoDebugMethodInfo, 1);
767 minfo->index = (ie - first_ie) + 1;
768 minfo->method = method;
769 minfo->handle = handle;
771 minfo->data_offset = read32 (&(ie->_data_offset));
772 minfo->lnt_offset = read32 (&(ie->_line_number_table));
774 g_hash_table_insert (symfile->method_hash, method, minfo);
776 mono_debugger_unlock ();
781 * mono_debug_symfile_lookup_locals:
783 * Return information about the local variables of MINFO from the symbol file.
784 * Return NULL if no information can be found.
785 * The result should be freed using mono_debug_symfile_free_locals ().
788 mono_debug_symfile_lookup_locals (MonoDebugMethodInfo *minfo)
790 MonoSymbolFile *symfile = minfo->handle->symfile;
792 int i, len, locals_offset, num_locals, block_index;
793 int code_block_table_offset;
794 MonoDebugLocalsInfo *res;
799 p = symfile->raw_contents + minfo->data_offset;
801 /* compile_unit_index = */ read_leb128 (p, &p);
802 locals_offset = read_leb128 (p, &p);
803 /* namespace_id = */ read_leb128 (p, &p);
804 code_block_table_offset = read_leb128 (p, &p);
806 res = g_new0 (MonoDebugLocalsInfo, 1);
808 p = symfile->raw_contents + code_block_table_offset;
809 res->num_blocks = read_leb128 (p, &p);
810 res->code_blocks = g_new0 (MonoDebugCodeBlock, res->num_blocks);
811 for (i = 0; i < res->num_blocks; ++i) {
812 res->code_blocks [i].type = read_leb128 (p, &p);
813 res->code_blocks [i].parent = read_leb128 (p, &p);
814 res->code_blocks [i].start_offset = read_leb128 (p, &p);
815 res->code_blocks [i].end_offset = read_leb128 (p, &p);
818 p = symfile->raw_contents + locals_offset;
819 num_locals = read_leb128 (p, &p);
821 res->num_locals = num_locals;
822 res->locals = g_new0 (MonoDebugLocalVar, num_locals);
824 for (i = 0; i < num_locals; ++i) {
825 res->locals [i].index = read_leb128 (p, &p);
826 len = read_leb128 (p, &p);
827 res->locals [i].name = (char *)g_malloc (len + 1);
828 memcpy (res->locals [i].name, p, len);
829 res->locals [i].name [len] = '\0';
831 block_index = read_leb128 (p, &p);
832 if (block_index >= 1 && block_index <= res->num_blocks)
833 res->locals [i].block = &res->code_blocks [block_index - 1];