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)
16 #ifdef HAVE_SYS_PARAM_H
17 #include <sys/param.h>
20 #include <mono/metadata/metadata.h>
21 #include <mono/metadata/tabledefs.h>
22 #include <mono/metadata/tokentype.h>
23 #include <mono/metadata/appdomain.h>
24 #include <mono/metadata/exception.h>
25 #include <mono/metadata/debug-helpers.h>
26 #include <mono/metadata/mono-debug.h>
27 #include <mono/metadata/debug-mono-symfile.h>
28 #include <mono/metadata/mono-debug-debugger.h>
29 #include <mono/metadata/mono-endian.h>
30 #include <mono/metadata/metadata-internals.h>
31 #include <mono/metadata/class-internals.h>
32 #include <mono/utils/mono-mmap.h>
39 #define RANGE_TABLE_CHUNK_SIZE 256
40 #define CLASS_TABLE_CHUNK_SIZE 256
41 #define TYPE_TABLE_PTR_CHUNK_SIZE 256
42 #define TYPE_TABLE_CHUNK_SIZE 65536
44 struct _MonoSymbolFile {
45 const uint8_t *raw_contents;
46 int raw_contents_size;
47 void *raw_contents_handle;
51 GHashTable *method_hash;
52 GHashTable *source_hash;
53 MonoSymbolFileOffsetTable *offset_table;
54 gboolean was_loaded_from_memory;
58 free_method_info (MonoDebugMethodInfo *minfo)
64 free_source_info (MonoDebugSourceInfo *sinfo)
66 g_free (sinfo->source_file);
73 load_symfile (MonoDebugHandle *handle, MonoSymbolFile *symfile, mono_bool in_the_debugger)
75 const char *ptr, *start;
80 ptr = start = (const char*)symfile->raw_contents;
85 ptr += sizeof(uint64_t);
86 if (magic != MONO_SYMBOL_FILE_MAGIC) {
88 g_warning ("Symbol file %s is not a mono symbol file", symfile->filename);
93 ptr += sizeof(uint32_t);
95 ptr += sizeof(uint32_t);
98 * 50.0 is the frozen version for Mono 2.0.
100 * Nobody except me (Martin) is allowed to check the minor version.
102 if (major != MONO_SYMBOL_FILE_MAJOR_VERSION) {
103 if (!in_the_debugger)
104 g_warning ("Symbol file %s has incorrect version (expected %d.%d, got %d)",
105 symfile->filename, MONO_SYMBOL_FILE_MAJOR_VERSION,
106 MONO_SYMBOL_FILE_MINOR_VERSION, major);
110 guid = mono_guid_to_string ((const uint8_t *) ptr);
113 if (strcmp (handle->image->guid, guid)) {
114 if (!in_the_debugger)
115 g_warning ("Symbol file %s doesn't match image %s", symfile->filename,
122 symfile->major_version = major;
123 symfile->minor_version = minor;
125 symfile->offset_table = (MonoSymbolFileOffsetTable *) ptr;
127 symfile->method_hash = g_hash_table_new_full (
128 NULL, NULL, NULL, (GDestroyNotify) free_method_info);
130 symfile->source_hash = g_hash_table_new_full (
131 NULL, NULL, NULL, (GDestroyNotify) free_source_info);
138 mono_debug_open_mono_symbols (MonoDebugHandle *handle, const uint8_t *raw_contents,
139 int size, gboolean in_the_debugger)
141 MonoSymbolFile *symfile;
143 mono_debugger_lock ();
144 symfile = g_new0 (MonoSymbolFile, 1);
146 if (raw_contents != NULL) {
148 symfile->raw_contents_size = size;
149 symfile->raw_contents = p = g_malloc (size);
150 memcpy (p, raw_contents, size);
151 symfile->filename = g_strdup_printf ("LoadedFromMemory");
152 symfile->was_loaded_from_memory = TRUE;
155 symfile->filename = g_strdup_printf ("%s.mdb", mono_image_get_filename (handle->image));
156 symfile->was_loaded_from_memory = FALSE;
157 if ((f = mono_file_map_open (symfile->filename))) {
158 symfile->raw_contents_size = mono_file_map_size (f);
159 if (symfile->raw_contents_size == 0) {
160 if (!in_the_debugger)
161 g_warning ("stat of %s failed: %s",
162 symfile->filename, g_strerror (errno));
164 symfile->raw_contents = mono_file_map (symfile->raw_contents_size, MONO_MMAP_READ|MONO_MMAP_PRIVATE, mono_file_map_fd (f), 0, &symfile->raw_contents_handle);
167 mono_file_map_close (f);
171 if (load_symfile (handle, symfile, in_the_debugger)) {
172 mono_debugger_unlock ();
174 } else if (!in_the_debugger) {
175 mono_debug_close_mono_symbol_file (symfile);
176 mono_debugger_unlock ();
180 mono_debugger_unlock ();
185 mono_debug_close_mono_symbol_file (MonoSymbolFile *symfile)
190 mono_debugger_lock ();
191 if (symfile->method_hash)
192 g_hash_table_destroy (symfile->method_hash);
194 if (symfile->raw_contents) {
195 if (symfile->was_loaded_from_memory)
196 g_free ((gpointer)symfile->raw_contents);
198 mono_file_unmap ((gpointer) symfile->raw_contents, symfile->raw_contents_handle);
201 if (symfile->filename)
202 g_free (symfile->filename);
204 mono_debugger_unlock ();
208 mono_debug_symfile_is_loaded (MonoSymbolFile *symfile)
210 return symfile && symfile->offset_table;
215 read_leb128 (const uint8_t *ptr, const uint8_t **rptr)
224 ret = ret | ((b & 0x7f) << shift);
226 } while ((b & 0x80) == 0x80);
235 read_string (const uint8_t *ptr, const uint8_t **endp)
238 int len = read_leb128 (ptr, &ptr);
240 s = g_filename_from_utf8 ((const char *) ptr, len, NULL, NULL, NULL);
248 MonoSymbolFile *symfile;
249 int line_base, line_range, max_address_incr;
251 uint32_t last_line, last_file, last_offset;
253 int line, file, offset;
258 check_line (StatementMachine *stm, int offset, MonoDebugSourceLocation **location)
260 gchar *source_file = NULL;
262 if (stm->offset <= offset) {
263 stm->last_offset = stm->offset;
264 stm->last_file = stm->file;
265 if (stm->line != 0xfeefee)
266 stm->last_line = stm->line;
270 if (stm->last_file) {
271 int offset = read32(&(stm->symfile->offset_table->_source_table_offset)) +
272 (stm->last_file - 1) * sizeof (MonoSymbolFileSourceEntry);
273 MonoSymbolFileSourceEntry *se = (MonoSymbolFileSourceEntry *)
274 (stm->symfile->raw_contents + offset);
276 source_file = read_string (stm->symfile->raw_contents + read32(&(se->_data_offset)), NULL);
279 if (stm->last_line == 0) {
281 * The IL offset is less than the first IL offset which has a corresponding
288 *location = g_new0 (MonoDebugSourceLocation, 1);
289 (*location)->source_file = source_file;
290 (*location)->row = stm->last_line;
291 (*location)->il_offset = stm->last_offset;
296 * mono_debug_symfile_lookup_location:
297 * @minfo: A `MonoDebugMethodInfo' which can be retrieved by
298 * mono_debug_lookup_method().
299 * @offset: IL offset within the corresponding method's CIL code.
301 * This function is similar to mono_debug_lookup_location(), but we
302 * already looked up the method and also already did the
303 * `native address -> IL offset' mapping.
305 MonoDebugSourceLocation *
306 mono_debug_symfile_lookup_location (MonoDebugMethodInfo *minfo, uint32_t offset)
308 MonoDebugSourceLocation *location = NULL;
309 MonoSymbolFile *symfile;
310 const unsigned char *ptr;
311 StatementMachine stm;
313 #define DW_LNS_copy 1
314 #define DW_LNS_advance_pc 2
315 #define DW_LNS_advance_line 3
316 #define DW_LNS_set_file 4
317 #define DW_LNS_const_add_pc 8
319 #define DW_LNE_end_sequence 1
320 #define DW_LNE_MONO_negate_is_hidden 0x40
322 #define DW_LNE_MONO__extensions_start 0x40
323 #define DW_LNE_MONO__extensions_end 0x7f
325 if ((symfile = minfo->handle->symfile) == NULL)
328 stm.line_base = read32 (&symfile->offset_table->_line_number_table_line_base);
329 stm.line_range = read32 (&symfile->offset_table->_line_number_table_line_range);
330 stm.opcode_base = (uint8_t) read32 (&symfile->offset_table->_line_number_table_opcode_base);
331 stm.max_address_incr = (255 - stm.opcode_base) / stm.line_range;
333 mono_debugger_lock ();
335 ptr = symfile->raw_contents + minfo->lnt_offset;
337 stm.symfile = symfile;
338 stm.offset = stm.last_offset = 0;
344 stm.is_hidden = FALSE;
347 uint8_t opcode = *ptr++;
350 uint8_t size = *ptr++;
351 const unsigned char *end_ptr = ptr + size;
355 if (opcode == DW_LNE_end_sequence) {
356 if (check_line (&stm, -1, &location))
359 } else if (opcode == DW_LNE_MONO_negate_is_hidden) {
360 stm.is_hidden = !stm.is_hidden;
361 } else if ((opcode >= DW_LNE_MONO__extensions_start) &&
362 (opcode <= DW_LNE_MONO__extensions_end)) {
363 ; // reserved for future extensions
365 g_warning ("Unknown extended opcode %x in LNT", opcode);
370 } else if (opcode < stm.opcode_base) {
373 if (check_line (&stm, offset, &location))
376 case DW_LNS_advance_pc:
377 stm.offset += read_leb128 (ptr, &ptr);
379 case DW_LNS_advance_line:
380 stm.line += read_leb128 (ptr, &ptr);
382 case DW_LNS_set_file:
383 stm.file = read_leb128 (ptr, &ptr);
385 case DW_LNS_const_add_pc:
386 stm.offset += stm.max_address_incr;
389 g_warning ("Unknown standard opcode %x in LNT", opcode);
393 opcode -= stm.opcode_base;
395 stm.offset += opcode / stm.line_range;
396 stm.line += stm.line_base + (opcode % stm.line_range);
398 if (check_line (&stm, offset, &location))
404 mono_debugger_unlock ();
408 mono_debugger_unlock ();
413 add_line (StatementMachine *stm, GPtrArray *il_offset_array, GPtrArray *line_number_array, GPtrArray *source_file_array)
416 g_ptr_array_add (il_offset_array, GUINT_TO_POINTER (stm->offset));
417 g_ptr_array_add (line_number_array, GUINT_TO_POINTER (stm->line));
418 g_ptr_array_add (source_file_array, GUINT_TO_POINTER (stm->file));
421 if (!stm->is_hidden && !stm->first_file)
422 stm->first_file = stm->file;
426 * mono_debug_symfile_free_location:
428 * Free a MonoDebugSourceLocation returned by
429 * mono_debug_symfile_lookup_location
432 mono_debug_symfile_free_location (MonoDebugSourceLocation *location)
434 g_free (location->source_file);
439 * LOCKING: Assumes the debugger lock is held.
441 static MonoDebugSourceInfo*
442 get_source_info (MonoSymbolFile *symfile, int index)
444 MonoDebugSourceInfo *info;
446 info = g_hash_table_lookup (symfile->source_hash, GUINT_TO_POINTER (index));
448 int offset = read32(&(symfile->offset_table->_source_table_offset)) +
449 (index - 1) * sizeof (MonoSymbolFileSourceEntry);
450 MonoSymbolFileSourceEntry *se = (MonoSymbolFileSourceEntry *)
451 (symfile->raw_contents + offset);
452 const uint8_t *ptr = symfile->raw_contents + read32(&(se->_data_offset));
454 info = g_new0 (MonoDebugSourceInfo, 1);
455 info->source_file = read_string (ptr, &ptr);
456 info->guid = g_malloc0 (16);
457 memcpy (info->guid, ptr, 16);
459 info->hash = g_malloc0 (16);
460 memcpy (info->hash, ptr, 16);
462 g_hash_table_insert (symfile->source_hash, GUINT_TO_POINTER (index), info);
468 * mono_debug_symfile_get_line_numbers_full:
470 * On return, SOURCE_FILE_LIST will point to a GPtrArray of MonoDebugSourceFile
471 * structures, and SOURCE_FILES will contain indexes into this array.
472 * The MonoDebugSourceFile structures are owned by this module.
475 mono_debug_symfile_get_line_numbers_full (MonoDebugMethodInfo *minfo, char **source_file, GPtrArray **source_file_list, int *n_il_offsets, int **il_offsets, int **line_numbers, int **source_files)
477 // FIXME: Unify this with mono_debug_symfile_lookup_location
478 MonoSymbolFile *symfile;
479 const unsigned char *ptr;
480 StatementMachine stm;
482 GPtrArray *il_offset_array, *line_number_array, *source_file_array;
484 if (source_file_list)
485 *source_file_list = NULL;
489 *source_files = NULL;
493 if ((symfile = minfo->handle->symfile) == NULL)
496 il_offset_array = g_ptr_array_new ();
497 line_number_array = g_ptr_array_new ();
498 source_file_array = g_ptr_array_new ();
500 stm.line_base = read32 (&symfile->offset_table->_line_number_table_line_base);
501 stm.line_range = read32 (&symfile->offset_table->_line_number_table_line_range);
502 stm.opcode_base = (uint8_t) read32 (&symfile->offset_table->_line_number_table_opcode_base);
503 stm.max_address_incr = (255 - stm.opcode_base) / stm.line_range;
505 mono_debugger_lock ();
507 ptr = symfile->raw_contents + minfo->lnt_offset;
509 stm.symfile = symfile;
510 stm.offset = stm.last_offset = 0;
516 stm.is_hidden = FALSE;
519 uint8_t opcode = *ptr++;
522 uint8_t size = *ptr++;
523 const unsigned char *end_ptr = ptr + size;
527 if (opcode == DW_LNE_end_sequence) {
528 if (il_offset_array->len == 0)
531 add_line (&stm, il_offset_array, line_number_array, source_file_array);
533 } else if (opcode == DW_LNE_MONO_negate_is_hidden) {
534 stm.is_hidden = !stm.is_hidden;
535 } else if ((opcode >= DW_LNE_MONO__extensions_start) &&
536 (opcode <= DW_LNE_MONO__extensions_end)) {
537 ; // reserved for future extensions
539 g_warning ("Unknown extended opcode %x in LNT", opcode);
544 } else if (opcode < stm.opcode_base) {
547 add_line (&stm, il_offset_array, line_number_array, source_file_array);
549 case DW_LNS_advance_pc:
550 stm.offset += read_leb128 (ptr, &ptr);
552 case DW_LNS_advance_line:
553 stm.line += read_leb128 (ptr, &ptr);
555 case DW_LNS_set_file:
556 stm.file = read_leb128 (ptr, &ptr);
558 case DW_LNS_const_add_pc:
559 stm.offset += stm.max_address_incr;
562 g_warning ("Unknown standard opcode %x in LNT", opcode);
563 g_assert_not_reached ();
566 opcode -= stm.opcode_base;
568 stm.offset += opcode / stm.line_range;
569 stm.line += stm.line_base + (opcode % stm.line_range);
571 add_line (&stm, il_offset_array, line_number_array, source_file_array);
575 if (!stm.file && stm.first_file)
576 stm.file = stm.first_file;
578 if (stm.file && source_file) {
579 int offset = read32(&(stm.symfile->offset_table->_source_table_offset)) +
580 (stm.file - 1) * sizeof (MonoSymbolFileSourceEntry);
581 MonoSymbolFileSourceEntry *se = (MonoSymbolFileSourceEntry *)
582 (stm.symfile->raw_contents + offset);
585 *source_file = read_string (stm.symfile->raw_contents + read32(&(se->_data_offset)), NULL);
588 if (source_file_list) {
589 int file, last_file = 0;
591 *source_file_list = g_ptr_array_new ();
593 *source_files = g_malloc (il_offset_array->len * sizeof (int));
595 for (i = 0; i < il_offset_array->len; ++i) {
596 file = GPOINTER_TO_UINT (g_ptr_array_index (source_file_array, i));
597 if (file && file != last_file) {
598 MonoDebugSourceInfo *info = get_source_info (symfile, file);
600 g_ptr_array_add (*source_file_list, info);
604 (*source_files) [i] = (*source_file_list)->len - 1;
606 if ((*source_file_list)->len == 0 && stm.file) {
607 MonoDebugSourceInfo *info = get_source_info (symfile, stm.file);
609 g_ptr_array_add (*source_file_list, info);
614 *n_il_offsets = il_offset_array->len;
615 if (il_offsets && line_numbers) {
616 *il_offsets = g_malloc (il_offset_array->len * sizeof (int));
617 *line_numbers = g_malloc (il_offset_array->len * sizeof (int));
618 for (i = 0; i < il_offset_array->len; ++i) {
619 (*il_offsets) [i] = GPOINTER_TO_UINT (g_ptr_array_index (il_offset_array, i));
620 (*line_numbers) [i] = GPOINTER_TO_UINT (g_ptr_array_index (line_number_array, i));
623 g_ptr_array_free (il_offset_array, TRUE);
624 g_ptr_array_free (line_number_array, TRUE);
626 mono_debugger_unlock ();
631 * mono_debug_symfile_get_line_numbers:
633 * All the output parameters can be NULL.
636 mono_debug_symfile_get_line_numbers (MonoDebugMethodInfo *minfo, char **source_file, int *n_il_offsets, int **il_offsets, int **line_numbers)
638 mono_debug_symfile_get_line_numbers_full (minfo, source_file, NULL, n_il_offsets, il_offsets, line_numbers, NULL);
642 _mono_debug_address_from_il_offset (MonoDebugMethodJitInfo *jit, uint32_t il_offset)
646 if (!jit || !jit->line_numbers)
649 for (i = jit->num_line_numbers - 1; i >= 0; i--) {
650 MonoDebugLineNumberEntry lne = jit->line_numbers [i];
652 if (lne.il_offset <= il_offset)
653 return lne.native_offset;
660 compare_method (const void *key, const void *object)
662 uint32_t token = GPOINTER_TO_UINT (key);
663 MonoSymbolFileMethodEntry *me = (MonoSymbolFileMethodEntry*)object;
665 return token - read32(&(me->_token));
668 MonoDebugMethodInfo *
669 mono_debug_symfile_lookup_method (MonoDebugHandle *handle, MonoMethod *method)
671 MonoSymbolFileMethodEntry *first_ie, *ie;
672 MonoDebugMethodInfo *minfo;
673 MonoSymbolFile *symfile = handle->symfile;
675 if (!symfile->method_hash)
678 if (handle->image != mono_class_get_image (mono_method_get_class (method)))
681 mono_debugger_lock ();
683 minfo = g_hash_table_lookup (symfile->method_hash, method);
685 mono_debugger_unlock ();
689 first_ie = (MonoSymbolFileMethodEntry *)
690 (symfile->raw_contents + read32(&(symfile->offset_table->_method_table_offset)));
692 ie = bsearch (GUINT_TO_POINTER (mono_method_get_token (method)), first_ie,
693 read32(&(symfile->offset_table->_method_count)),
694 sizeof (MonoSymbolFileMethodEntry), compare_method);
697 mono_debugger_unlock ();
701 minfo = g_new0 (MonoDebugMethodInfo, 1);
702 minfo->index = (ie - first_ie) + 1;
703 minfo->method = method;
704 minfo->handle = handle;
706 minfo->data_offset = read32 (&(ie->_data_offset));
707 minfo->lnt_offset = read32 (&(ie->_line_number_table));
709 g_hash_table_insert (symfile->method_hash, method, minfo);
711 mono_debugger_unlock ();
716 * mono_debug_symfile_lookup_locals:
718 * Return information about the local variables of MINFO from the symbol file.
719 * Return NULL if no information can be found.
720 * The result should be freed using mono_debug_symfile_free_locals ().
723 mono_debug_symfile_lookup_locals (MonoDebugMethodInfo *minfo)
725 MonoSymbolFile *symfile = minfo->handle->symfile;
727 int i, len, compile_unit_index, locals_offset, num_locals, block_index;
728 int namespace_id, code_block_table_offset;
729 MonoDebugLocalsInfo *res;
734 p = symfile->raw_contents + minfo->data_offset;
736 compile_unit_index = read_leb128 (p, &p);
737 locals_offset = read_leb128 (p, &p);
738 namespace_id = read_leb128 (p, &p);
739 code_block_table_offset = read_leb128 (p, &p);
741 res = g_new0 (MonoDebugLocalsInfo, 1);
743 p = symfile->raw_contents + code_block_table_offset;
744 res->num_blocks = read_leb128 (p, &p);
745 res->code_blocks = g_new0 (MonoDebugCodeBlock, res->num_blocks);
746 for (i = 0; i < res->num_blocks; ++i) {
747 res->code_blocks [i].type = read_leb128 (p, &p);
748 res->code_blocks [i].parent = read_leb128 (p, &p);
749 res->code_blocks [i].start_offset = read_leb128 (p, &p);
750 res->code_blocks [i].end_offset = read_leb128 (p, &p);
753 p = symfile->raw_contents + locals_offset;
754 num_locals = read_leb128 (p, &p);
756 res->num_locals = num_locals;
757 res->locals = g_new0 (MonoDebugLocalVar, num_locals);
759 for (i = 0; i < num_locals; ++i) {
760 res->locals [i].index = read_leb128 (p, &p);
761 len = read_leb128 (p, &p);
762 res->locals [i].name = g_malloc (len + 1);
763 memcpy (res->locals [i].name, p, len);
764 res->locals [i].name [len] = '\0';
766 block_index = read_leb128 (p, &p);
767 if (block_index >= 1 && block_index <= res->num_blocks)
768 res->locals [i].block = &res->code_blocks [block_index - 1];
775 * mono_debug_symfile_free_locals:
777 * Free all the data allocated by mono_debug_symfile_lookup_locals ().
780 mono_debug_symfile_free_locals (MonoDebugLocalsInfo *info)
784 for (i = 0; i < info->num_locals; ++i)
785 g_free (info->locals [i].name);
786 g_free (info->locals);
787 g_free (info->code_blocks);