4 #include <mono/metadata/metadata.h>
5 #include <mono/metadata/rawbuffer.h>
6 #include <mono/metadata/tokentype.h>
7 #include <mono/metadata/appdomain.h>
8 #include <mono/metadata/exception.h>
9 #include <mono/metadata/debug-symfile.h>
18 /* Keep in sync with Mono.CSharp.Debugger.MonoDwarfFileWriter */
20 #define MRT_target_address_size 0x01
21 #define MRT_il_offset 0x02
22 #define MRT_method_start_address 0x03
23 #define MRT_method_end_address 0x04
24 #define MRT_local_variable 0x05
25 #define MRT_method_parameter 0x06
26 #define MRT_type_sizeof 0x07
27 #define MRT_type_field_offset 0x08
28 #define MRT_mono_string_sizeof 0x09
29 #define MRT_mono_string_offset 0x0a
30 #define MRT_mono_array_sizeof 0x0b
31 #define MRT_mono_array_offset 0x0c
32 #define MRT_mono_array_bounds_sizeof 0x0d
33 #define MRT_mono_array_bounds_offset 0x0e
34 #define MRT_variable_start_scope 0x0f
35 #define MRT_variable_end_scope 0x10
36 #define MRT_mono_string_fieldsize 0x11
37 #define MRT_mono_array_fieldsize 0x12
38 #define MRT_type_field_fieldsize 0x13
40 #define MRI_string_offset_length 0x00
41 #define MRI_string_offset_chars 0x01
43 #define MRI_array_offset_bounds 0x00
44 #define MRI_array_offset_max_length 0x01
45 #define MRI_array_offset_vector 0x02
47 #define MRI_array_bounds_offset_lower 0x00
48 #define MRI_array_bounds_offset_length 0x01
50 #define MRS_debug_info 0x01
51 #define MRS_debug_abbrev 0x02
52 #define MRS_debug_line 0x03
53 #define MRS_mono_reloc_table 0x04
55 #define DW_OP_const4s 0x0d
56 #define DW_OP_plus 0x22
57 #define DW_OP_reg0 0x50
58 #define DW_OP_breg0 0x70
59 #define DW_OP_fbreg 0x91
60 #define DW_OP_piece 0x93
61 #define DW_OP_nop 0x96
66 get_sections_elf32 (MonoDebugSymbolFile *symfile, gboolean emit_warnings)
69 Elf32_Shdr *section, *strtab_section;
73 header = (Elf32_Ehdr *)symfile->raw_contents;
74 if (header->e_version != EV_CURRENT) {
76 g_warning ("Symbol file %s has unknown ELF version %d",
77 symfile->file_name, header->e_version);
81 if (header->e_machine != EM_386) {
83 g_warning ("ELF file %s is for unknown architecture %d",
84 symfile->file_name, header->e_machine);
88 if (header->e_shentsize != sizeof (*section)) {
90 g_warning ("ELF file %s has unknown section header size "
91 "(expected %d, got %d)", symfile->file_name,
92 sizeof (*section), header->e_shentsize);
96 symfile->section_offsets = g_new0 (MonoDebugSymbolFileSection, MONO_DEBUG_SYMBOL_SECTION_MAX);
98 section = (Elf32_Shdr *)(symfile->raw_contents + header->e_shoff);
99 strtab_section = section + header->e_shstrndx;
100 strtab = symfile->raw_contents + strtab_section->sh_offset;
102 for (i = 0; i < header->e_shnum; i++, section++) {
103 const gchar *name = strtab + section->sh_name;
105 if (!strcmp (name, ".debug_info")) {
106 MonoDebugSymbolFileSection *sfs;
108 sfs = &symfile->section_offsets [MONO_DEBUG_SYMBOL_SECTION_DEBUG_INFO];
109 sfs->type = MONO_DEBUG_SYMBOL_SECTION_DEBUG_INFO;
110 sfs->file_offset = section->sh_offset;
111 sfs->size = section->sh_size;
112 } else if (!strcmp (name, ".debug_line")) {
113 MonoDebugSymbolFileSection *sfs;
115 sfs = &symfile->section_offsets [MONO_DEBUG_SYMBOL_SECTION_DEBUG_LINE];
116 sfs->type = MONO_DEBUG_SYMBOL_SECTION_DEBUG_LINE;
117 sfs->file_offset = section->sh_offset;
118 sfs->size = section->sh_size;
119 } else if (!strcmp (name, ".debug_abbrev")) {
120 MonoDebugSymbolFileSection *sfs;
122 sfs = &symfile->section_offsets [MONO_DEBUG_SYMBOL_SECTION_DEBUG_ABBREV];
123 sfs->type = MONO_DEBUG_SYMBOL_SECTION_DEBUG_ABBREV;
124 sfs->file_offset = section->sh_offset;
125 sfs->size = section->sh_size;
126 } else if (!strcmp (name, ".mono_reloc_table")) {
127 MonoDebugSymbolFileSection *sfs;
129 sfs = &symfile->section_offsets [MONO_DEBUG_SYMBOL_SECTION_MONO_RELOC_TABLE];
130 sfs->type = MONO_DEBUG_SYMBOL_SECTION_MONO_RELOC_TABLE;
131 sfs->file_offset = section->sh_offset;
132 sfs->size = section->sh_size;
133 } else if (!strcmp (name, ".mono_line_numbers")) {
134 MonoDebugSymbolFileSection *sfs;
136 sfs = &symfile->section_offsets [MONO_DEBUG_SYMBOL_SECTION_MONO_LINE_NUMBERS];
137 sfs->type = MONO_DEBUG_SYMBOL_SECTION_MONO_LINE_NUMBERS;
138 sfs->file_offset = section->sh_offset;
139 sfs->size = section->sh_size;
146 #endif /* HAVE_ELF_H */
149 get_sections (MonoDebugSymbolFile *symfile, gboolean emit_warnings)
153 static const char ELFMAG[] = { ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3, 0 };
155 if (!strncmp (symfile->raw_contents, ELFMAG, strlen (ELFMAG)))
156 return get_sections_elf32 (symfile, emit_warnings);
160 g_warning ("Symbol file %s has unknown file format", symfile->file_name);
166 read_line_numbers (MonoDebugSymbolFile *symfile)
168 const char *ptr, *start, *end;
172 if (!symfile->section_offsets [MONO_DEBUG_SYMBOL_SECTION_MONO_LINE_NUMBERS].file_offset)
175 ptr = start = symfile->raw_contents +
176 symfile->section_offsets [MONO_DEBUG_SYMBOL_SECTION_MONO_LINE_NUMBERS].file_offset;
178 version = *((guint16 *) ptr)++;
179 if (version != MONO_DEBUG_SYMBOL_FILE_VERSION) {
180 g_warning ("Symbol file %s has incorrect line number table version "
181 "(expected %d, got %d)", symfile->file_name,
182 MONO_DEBUG_SYMBOL_FILE_VERSION, version);
186 section_size = *((guint32 *) ptr)++;
187 end = ptr + section_size;
189 symfile->line_number_table = g_hash_table_new_full (g_direct_hash, g_direct_equal,
190 NULL, (GDestroyNotify) g_free);
193 MonoDebugLineNumberBlock *lnb;
194 guint32 token, source_offset;
197 token = * ((guint32 *) ptr)++;
198 method = mono_get_method (symfile->image, token, NULL);
202 lnb = g_new0 (MonoDebugLineNumberBlock, 1);
204 source_offset = * ((guint32 *) ptr)++;
205 lnb->source_file = (const char *) start + source_offset;
206 lnb->start_line = * ((guint32 *) ptr)++;
207 lnb->file_offset = * ((guint32 *) ptr)++;
209 g_hash_table_insert (symfile->line_number_table, method, lnb);
214 mono_debug_class_get (MonoDebugSymbolFile *symfile, guint32 type_token)
218 if ((klass = g_hash_table_lookup (symfile->image->class_cache, GUINT_TO_POINTER (type_token))))
224 MonoDebugSymbolFile *
225 mono_debug_open_symbol_file (MonoImage *image, const char *filename, gboolean emit_warnings)
227 MonoDebugSymbolFile *symfile;
232 fd = open (filename, O_RDWR);
235 g_warning ("Can't open symbol file: %s", filename);
239 file_size = lseek (fd, 0, SEEK_END);
240 lseek (fd, 0, SEEK_SET);
242 if (file_size == (off_t) -1) {
244 g_warning ("Can't get size of symbol file: %s", filename);
248 ptr = mono_raw_buffer_load (fd, 1, 0, file_size);
251 g_warning ("Can't read symbol file: %s", filename);
255 symfile = g_new0 (MonoDebugSymbolFile, 1);
257 symfile->file_name = g_strdup (filename);
258 symfile->image = image;
259 symfile->raw_contents = ptr;
260 symfile->raw_contents_size = file_size;
262 if (!get_sections (symfile, emit_warnings)) {
263 mono_debug_close_symbol_file (symfile);
267 read_line_numbers (symfile);
273 mono_debug_close_symbol_file (MonoDebugSymbolFile *symfile)
278 if (symfile->raw_contents)
279 mono_raw_buffer_free (symfile->raw_contents);
283 if (symfile->line_number_table)
284 g_hash_table_destroy (symfile->line_number_table);
285 g_free (symfile->file_name);
286 g_free (symfile->section_offsets);
291 relocate_variable (MonoDebugVarInfo *var, void *base_ptr)
294 * ((guint8 *) base_ptr)++ = DW_OP_nop;
295 * ((guint8 *) base_ptr)++ = DW_OP_nop;
296 * ((guint8 *) base_ptr)++ = DW_OP_nop;
297 * ((guint8 *) base_ptr)++ = DW_OP_nop;
298 * ((guint8 *) base_ptr)++ = DW_OP_nop;
299 * ((guint8 *) base_ptr)++ = DW_OP_nop;
300 * ((guint8 *) base_ptr)++ = DW_OP_nop;
301 * ((guint8 *) base_ptr)++ = DW_OP_nop;
306 * Update the location description for a local variable or method parameter.
307 * MCS always reserves 8 bytes for us to do this, if we don't need them all
308 * we just fill up the rest with DW_OP_nop's.
311 switch (var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS) {
312 case MONO_DEBUG_VAR_ADDRESS_MODE_STACK:
314 * Variable is on the stack.
316 * If `index' is zero, use the normal frame register. Otherwise, bits
317 * 0..4 of `index' contain the frame register.
319 * Both DW_OP_fbreg and DW_OP_breg0 ... DW_OP_breg31 take an ULeb128
320 * argument - since this has an variable size, we set it to zero and
321 * manually add a 4 byte constant using DW_OP_plus.
324 /* Use the normal frame register (%ebp on the i386). */
325 * ((guint8 *) base_ptr)++ = DW_OP_fbreg;
327 /* Use a custom frame register. */
328 * ((guint8 *) base_ptr)++ = DW_OP_breg0 + (var->index & 0x001f);
329 * ((guint8 *) base_ptr)++ = 0;
330 * ((guint8 *) base_ptr)++ = DW_OP_const4s;
331 * ((gint32 *) base_ptr)++ = var->offset;
332 * ((guint8 *) base_ptr)++ = DW_OP_plus;
335 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
337 * Variable is in the register whose number is contained in bits 0..4
340 * We need to write exactly 8 bytes in this location description, so instead
341 * of filling up the rest with DW_OP_nop's just add the `offset' even if
344 * ((guint8 *) base_ptr)++ = DW_OP_reg0 + (var->index & 0x001f);
345 * ((guint8 *) base_ptr)++ = DW_OP_nop;
346 * ((guint8 *) base_ptr)++ = DW_OP_const4s;
347 * ((gint32 *) base_ptr)++ = var->offset;
348 * ((guint8 *) base_ptr)++ = DW_OP_plus;
351 case MONO_DEBUG_VAR_ADDRESS_MODE_TWO_REGISTERS:
353 * Variable is in two registers whose numbers are in bits 0..4 and 5..9 of
354 * the `index' field. Don't add `offset' since we have only two bytes left,
355 * fill them up with DW_OP_nop's.
357 * ((guint8 *) base_ptr)++ = DW_OP_reg0 + (var->index & 0x001f);
358 * ((guint8 *) base_ptr)++ = DW_OP_piece;
359 * ((guint8 *) base_ptr)++ = sizeof (int);
360 * ((guint8 *) base_ptr)++ = DW_OP_reg0 + ((var->index & 0x1f0) >> 5);
361 * ((guint8 *) base_ptr)++ = DW_OP_piece;
362 * ((guint8 *) base_ptr)++ = sizeof (int);
363 * ((guint8 *) base_ptr)++ = DW_OP_nop;
364 * ((guint8 *) base_ptr)++ = DW_OP_nop;
368 g_assert_not_reached ();
373 mono_debug_update_symbol_file (MonoDebugSymbolFile *symfile,
374 MonoDebugMethodInfoFunc method_info_func,
377 const char *reloc_ptr, *reloc_start, *reloc_end;
378 int version, already_relocated = 0;
381 if (!symfile->section_offsets [MONO_DEBUG_SYMBOL_SECTION_MONO_RELOC_TABLE].file_offset)
384 reloc_ptr = reloc_start = symfile->raw_contents +
385 symfile->section_offsets [MONO_DEBUG_SYMBOL_SECTION_MONO_RELOC_TABLE].file_offset;
387 version = *((guint16 *) reloc_ptr)++;
388 if (version != MONO_DEBUG_SYMBOL_FILE_VERSION) {
389 g_warning ("Symbol file %s has incorrect relocation table version "
390 "(expected %d, got %d)", symfile->file_name,
391 MONO_DEBUG_SYMBOL_FILE_VERSION, version);
395 already_relocated = *reloc_ptr;
396 *((char *) reloc_ptr)++ = 1;
398 reloc_size = *((guint32 *) reloc_ptr)++;
399 reloc_end = reloc_ptr + reloc_size;
401 while (reloc_ptr < reloc_end) {
402 int type, size, section, offset;
407 size = * ((guint32 *) reloc_ptr)++;
412 section = *tmp_ptr++;
413 offset = *((guint32 *) tmp_ptr)++;
415 if (section >= MONO_DEBUG_SYMBOL_SECTION_MAX) {
416 g_warning ("Symbol file %s contains a relocation entry for unknown section %d",
417 symfile->file_name, section);
421 if (!symfile->section_offsets [section].file_offset) {
422 g_warning ("Symbol file %s contains a relocation entry for non-existing "
423 "section %d", symfile->file_name, section);
427 base_ptr = symfile->raw_contents + symfile->section_offsets [section].file_offset;
428 base_ptr = base_ptr + offset;
431 case MRT_target_address_size:
432 * (guint8 *) base_ptr = sizeof (void *);
434 case MRT_method_start_address: {
435 int token = *((guint32 *) tmp_ptr)++;
436 MonoDebugMethodInfo *minfo;
438 minfo = method_info_func (symfile, token, user_data);
441 * (void **) base_ptr = 0;
446 g_message ("Start of `%s' (%ld) relocated to %p", minfo->method->name,
447 token, minfo->code_start);
450 * (void **) base_ptr = minfo->code_start;
454 case MRT_method_end_address: {
455 int token = *((guint32 *) tmp_ptr)++;
456 MonoDebugMethodInfo *minfo;
458 minfo = method_info_func (symfile, token, user_data);
461 * (void **) base_ptr = 0;
465 * (void **) base_ptr = (char *)minfo->code_start + minfo->code_size;
469 case MRT_il_offset: {
470 guint32 token = *((guint32 *) tmp_ptr)++;
471 guint32 original = *((guint32 *) tmp_ptr)++;
472 MonoDebugMethodInfo *minfo;
476 minfo = method_info_func (symfile, token, user_data);
479 * (void **) base_ptr = 0;
483 address = minfo->code_size;
485 for (i = 0; i < minfo->num_il_offsets; i++) {
486 MonoDebugILOffsetInfo *il = &minfo->il_offsets [i];
488 if (il->offset >= original) {
489 address = il->address;
495 g_message ("Relocating IL offset %04x in `%s' to %d (%p)",
496 original, minfo->method->name, address,
497 minfo->code_start + address);
500 * (void **) base_ptr = minfo->code_start + address;
504 case MRT_local_variable: {
505 guint32 token = *((guint32 *) tmp_ptr)++;
506 guint32 original = *((guint32 *) tmp_ptr)++;
507 MonoDebugMethodInfo *minfo;
509 minfo = method_info_func (symfile, token, user_data);
512 relocate_variable (NULL, base_ptr);
516 if (original > minfo->num_locals) {
517 g_warning ("Symbol file %s contains relocation entry for non-existing "
518 "local variable %d, but method %s only has %d local variables.",
519 symfile->file_name, original, minfo->method->name,
521 g_message (G_STRLOC ": %d", token);
526 relocate_variable (&minfo->locals [original], base_ptr);
530 case MRT_method_parameter: {
531 guint32 token = *((guint32 *) tmp_ptr)++;
532 guint32 original = *((guint32 *) tmp_ptr)++;
533 MonoDebugMethodInfo *minfo;
535 minfo = method_info_func (symfile, token, user_data);
538 relocate_variable (NULL, base_ptr);
542 if (minfo->method->signature->hasthis) {
544 relocate_variable (minfo->this_var, base_ptr);
551 if (original > minfo->num_params) {
552 g_warning ("Symbol file %s contains relocation entry for non-existing "
553 "parameter %d, but method %s only has %d parameters.",
554 symfile->file_name, original, minfo->method->name,
559 relocate_variable (&minfo->params [original], base_ptr);
563 case MRT_type_sizeof: {
564 guint32 token = *((guint32 *) tmp_ptr)++;
565 MonoClass *klass = mono_debug_class_get (symfile, token);
570 mono_class_init (klass);
572 if (klass->enumtype || klass->valuetype)
573 * (gint8 *) base_ptr = klass->instance_size - sizeof (MonoObject);
575 * (gint8 *) base_ptr = klass->instance_size;
579 case MRT_type_field_offset: {
580 guint32 token = *((guint32 *) tmp_ptr)++;
581 guint32 original = *((guint32 *) tmp_ptr)++;
582 MonoClass *klass = mono_debug_class_get (symfile, token);
588 mono_class_init (klass);
590 if (original > klass->field.count) {
591 g_warning ("Symbol file %s contains invalid field offset entry.",
593 g_message (G_STRLOC ": %d", token);
594 /* G_BREAKPOINT (); */
601 off = klass->fields [original].offset;
602 if (klass->byval_arg.type == MONO_TYPE_VALUETYPE)
603 off -= sizeof (MonoObject);
606 g_message ("Setting field %d of type %u to offset %d", original,
610 * (guint32 *) base_ptr = off;
614 case MRT_mono_string_sizeof:
615 * (gint8 *) base_ptr = sizeof (MonoString);
618 case MRT_mono_string_offset: {
619 guint32 idx = *((guint32 *) tmp_ptr)++;
624 case MRI_string_offset_length:
625 off = (guchar *) &string.length - (guchar *) &string;
628 case MRI_string_offset_chars:
629 off = (guchar *) &string.chars - (guchar *) &string;
633 g_warning ("Symbol file %s contains invalid string offset entry",
638 * (guint32 *) base_ptr = off;
642 case MRT_mono_array_sizeof:
643 * (gint8 *) base_ptr = sizeof (MonoArray);
646 case MRT_mono_array_offset: {
647 guint32 idx = *((guint32 *) tmp_ptr)++;
652 case MRI_array_offset_bounds:
653 off = (guchar *) &array.bounds - (guchar *) &array;
656 case MRI_array_offset_max_length:
657 off = (guchar *) &array.max_length - (guchar *) &array;
660 case MRI_array_offset_vector:
661 off = (guchar *) &array.vector - (guchar *) &array;
665 g_warning ("Symbol file %s contains invalid array offset entry",
670 * (guint32 *) base_ptr = off;
675 case MRT_mono_array_bounds_sizeof:
676 * (gint8 *) base_ptr = sizeof (MonoArrayBounds);
679 case MRT_mono_array_bounds_offset: {
680 guint32 idx = *((guint32 *) tmp_ptr)++;
681 MonoArrayBounds bounds;
685 case MRI_array_bounds_offset_lower:
686 off = (guchar *) &bounds.lower_bound - (guchar *) &bounds;
689 case MRI_array_bounds_offset_length:
690 off = (guchar *) &bounds.length - (guchar *) &bounds;
694 g_warning ("Symbol file %s contains invalid array bounds offset entry",
699 * (guint32 *) base_ptr = off;
704 case MRT_variable_start_scope: {
705 guint32 token = *((guint32 *) tmp_ptr)++;
706 guint32 original = *((guint32 *) tmp_ptr)++;
707 MonoDebugMethodInfo *minfo;
710 minfo = method_info_func (symfile, token, user_data);
712 if (!minfo || !minfo->locals) {
713 * (void **) base_ptr = 0;
717 if (original > minfo->num_locals) {
718 g_warning ("Symbol file %s contains relocation entry for non-existing "
719 "local variable %d, but method %s only has %d local variables.",
720 symfile->file_name, original, minfo->method->name,
725 address = minfo->locals [original].begin_scope;
727 * (void **) base_ptr = minfo->code_start + address;
732 case MRT_variable_end_scope: {
733 guint32 token = *((guint32 *) tmp_ptr)++;
734 guint32 original = *((guint32 *) tmp_ptr)++;
735 MonoDebugMethodInfo *minfo;
738 minfo = method_info_func (symfile, token, user_data);
740 if (!minfo || !minfo->locals) {
741 * (void **) base_ptr = 0;
745 if (original > minfo->num_locals) {
746 g_warning ("Symbol file %s contains relocation entry for non-existing "
747 "local variable %d, but method %s only has %d local variables.",
748 symfile->file_name, original, minfo->method->name,
753 address = minfo->locals [original].end_scope;
755 * (void **) base_ptr = minfo->code_start + address;
760 case MRT_mono_string_fieldsize: {
761 guint32 idx = *((guint32 *) tmp_ptr)++;
766 case MRI_string_offset_length:
767 fieldsize = sizeof (string.length);
771 g_warning ("Symbol file %s contains invalid string fieldsize entry",
776 * (guint32 *) base_ptr = fieldsize;
781 case MRT_mono_array_fieldsize: {
782 guint32 idx = *((guint32 *) tmp_ptr)++;
787 case MRI_array_offset_bounds:
788 fieldsize = sizeof (array.bounds);
791 case MRI_array_offset_max_length:
792 fieldsize = sizeof (array.max_length);
795 case MRI_array_offset_vector:
796 fieldsize = sizeof (array.vector);
800 g_warning ("Symbol file %s contains invalid array fieldsize entry",
805 * (guint32 *) base_ptr = fieldsize;
812 g_warning ("Symbol file %s contains unknown relocation entry %d",
813 symfile->file_name, type);
818 mono_raw_buffer_update (symfile->raw_contents, symfile->raw_contents_size);
822 mono_debug_find_source_location (MonoDebugSymbolFile *symfile, MonoMethod *method, guint32 offset)
824 MonoDebugLineNumberBlock *lnb;
827 if (!symfile->line_number_table)
830 lnb = g_hash_table_lookup (symfile->line_number_table, method);
834 ptr = symfile->raw_contents +
835 symfile->section_offsets [MONO_DEBUG_SYMBOL_SECTION_MONO_LINE_NUMBERS].file_offset;
837 ptr += lnb->file_offset;
840 guint32 row, iloffset;
842 row = * ((guint32 *) ptr)++;
843 iloffset = * ((guint32 *) ptr)++;
850 if (iloffset >= offset)
851 return g_strdup_printf ("%s:%d", lnb->source_file, row);