4 #include <mono/metadata/metadata.h>
5 #include <mono/metadata/rawbuffer.h>
6 #include <mono/metadata/appdomain.h>
7 #include <mono/metadata/exception.h>
8 #include <mono/metadata/debug-symfile.h>
17 /* Keep in sync with Mono.CSharp.Debugger.MonoDwarfFileWriter */
19 #define MRT_target_address_size 0x01
20 #define MRT_il_offset 0x02
21 #define MRT_method_start_address 0x03
22 #define MRT_method_end_address 0x04
23 #define MRT_local_variable 0x05
24 #define MRT_method_parameter 0x06
25 #define MRT_type_sizeof 0x07
26 #define MRT_type_field_offset 0x08
27 #define MRT_mono_string_sizeof 0x09
28 #define MRT_mono_string_offset 0x0a
29 #define MRT_mono_array_sizeof 0x0b
30 #define MRT_mono_array_offset 0x0c
31 #define MRT_mono_array_bounds_sizeof 0x0d
32 #define MRT_mono_array_bounds_offset 0x0e
33 #define MRT_variable_start_scope 0x0f
34 #define MRT_variable_end_scope 0x10
35 #define MRT_mono_string_fieldsize 0x11
36 #define MRT_mono_array_fieldsize 0x12
37 #define MRT_type_field_fieldsize 0x13
39 #define MRI_string_offset_length 0x00
40 #define MRI_string_offset_vector 0x01
42 #define MRI_array_offset_bounds 0x00
43 #define MRI_array_offset_max_length 0x01
44 #define MRI_array_offset_vector 0x02
46 #define MRI_array_bounds_offset_lower 0x00
47 #define MRI_array_bounds_offset_length 0x01
49 #define MRS_debug_info 0x01
50 #define MRS_debug_abbrev 0x02
51 #define MRS_debug_line 0x03
52 #define MRS_mono_reloc_table 0x04
54 #define DW_OP_const4s 0x0d
55 #define DW_OP_plus 0x22
56 #define DW_OP_reg0 0x50
57 #define DW_OP_breg0 0x70
58 #define DW_OP_fbreg 0x91
59 #define DW_OP_piece 0x93
60 #define DW_OP_nop 0x96
65 get_sections_elf32 (MonoDebugSymbolFile *symfile, gboolean emit_warnings)
68 Elf32_Shdr *section, *strtab_section;
72 header = (Elf32_Ehdr *)symfile->raw_contents;
73 if (header->e_version != EV_CURRENT) {
75 g_warning ("Symbol file %s has unknown ELF version %d",
76 symfile->file_name, header->e_version);
80 if (header->e_machine != EM_386) {
82 g_warning ("ELF file %s is for unknown architecture %d",
83 symfile->file_name, header->e_machine);
87 if (header->e_shentsize != sizeof (*section)) {
89 g_warning ("ELF file %s has unknown section header size "
90 "(expected %d, got %d)", symfile->file_name,
91 sizeof (*section), header->e_shentsize);
95 symfile->section_offsets = g_new0 (MonoDebugSymbolFileSection, MONO_DEBUG_SYMBOL_SECTION_MAX);
97 section = (Elf32_Shdr *)(symfile->raw_contents + header->e_shoff);
98 strtab_section = section + header->e_shstrndx;
99 strtab = symfile->raw_contents + strtab_section->sh_offset;
101 for (i = 0; i < header->e_shnum; i++, section++) {
102 const gchar *name = strtab + section->sh_name;
104 if (!strcmp (name, ".debug_info")) {
105 MonoDebugSymbolFileSection *sfs;
107 sfs = &symfile->section_offsets [MONO_DEBUG_SYMBOL_SECTION_DEBUG_INFO];
108 sfs->type = MONO_DEBUG_SYMBOL_SECTION_DEBUG_INFO;
109 sfs->file_offset = section->sh_offset;
110 sfs->size = section->sh_size;
111 } else if (!strcmp (name, ".debug_line")) {
112 MonoDebugSymbolFileSection *sfs;
114 sfs = &symfile->section_offsets [MONO_DEBUG_SYMBOL_SECTION_DEBUG_LINE];
115 sfs->type = MONO_DEBUG_SYMBOL_SECTION_DEBUG_LINE;
116 sfs->file_offset = section->sh_offset;
117 sfs->size = section->sh_size;
118 } else if (!strcmp (name, ".debug_abbrev")) {
119 MonoDebugSymbolFileSection *sfs;
121 sfs = &symfile->section_offsets [MONO_DEBUG_SYMBOL_SECTION_DEBUG_ABBREV];
122 sfs->type = MONO_DEBUG_SYMBOL_SECTION_DEBUG_ABBREV;
123 sfs->file_offset = section->sh_offset;
124 sfs->size = section->sh_size;
125 } else if (!strcmp (name, ".mono_reloc_table")) {
126 MonoDebugSymbolFileSection *sfs;
128 sfs = &symfile->section_offsets [MONO_DEBUG_SYMBOL_SECTION_MONO_RELOC_TABLE];
129 sfs->type = MONO_DEBUG_SYMBOL_SECTION_MONO_RELOC_TABLE;
130 sfs->file_offset = section->sh_offset;
131 sfs->size = section->sh_size;
138 #endif /* HAVE_ELF_H */
141 get_sections (MonoDebugSymbolFile *symfile, gboolean emit_warnings)
144 if (!strncmp (symfile->raw_contents, ELFMAG, strlen (ELFMAG)))
145 return get_sections_elf32 (symfile, emit_warnings);
149 g_warning ("Symbol file %s has unknown file format", symfile->file_name);
155 mono_debug_class_get (MonoDebugSymbolFile *symfile, guint32 type_token)
159 if ((klass = g_hash_table_lookup (symfile->image->class_cache, GUINT_TO_POINTER (type_token))))
165 MonoDebugSymbolFile *
166 mono_debug_open_symbol_file (MonoImage *image, const char *filename, gboolean emit_warnings)
168 MonoDebugSymbolFile *symfile;
173 fd = open (filename, O_RDWR);
176 g_warning ("Can't open symbol file: %s", filename);
180 file_size = lseek (fd, 0, SEEK_END);
181 lseek (fd, 0, SEEK_SET);
183 if (file_size == (off_t) -1) {
185 g_warning ("Can't get size of symbol file: %s", filename);
189 ptr = mono_raw_buffer_load (fd, 1, 0, file_size);
192 g_warning ("Can't read symbol file: %s", filename);
196 symfile = g_new0 (MonoDebugSymbolFile, 1);
198 symfile->file_name = g_strdup (filename);
199 symfile->image = image;
200 symfile->raw_contents = ptr;
201 symfile->raw_contents_size = file_size;
203 if (!get_sections (symfile, emit_warnings)) {
204 mono_debug_close_symbol_file (symfile);
212 mono_debug_close_symbol_file (MonoDebugSymbolFile *symfile)
217 if (symfile->raw_contents)
218 mono_raw_buffer_free (symfile->raw_contents);
222 g_free (symfile->file_name);
223 g_free (symfile->section_offsets);
228 relocate_variable (MonoDebugVarInfo *var, void *base_ptr)
231 * ((guint8 *) base_ptr)++ = DW_OP_nop;
232 * ((guint8 *) base_ptr)++ = DW_OP_nop;
233 * ((guint8 *) base_ptr)++ = DW_OP_nop;
234 * ((guint8 *) base_ptr)++ = DW_OP_nop;
235 * ((guint8 *) base_ptr)++ = DW_OP_nop;
236 * ((guint8 *) base_ptr)++ = DW_OP_nop;
237 * ((guint8 *) base_ptr)++ = DW_OP_nop;
238 * ((guint8 *) base_ptr)++ = DW_OP_nop;
243 * Update the location description for a local variable or method parameter.
244 * MCS always reserves 8 bytes for us to do this, if we don't need them all
245 * we just fill up the rest with DW_OP_nop's.
248 switch (var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS) {
249 case MONO_DEBUG_VAR_ADDRESS_MODE_STACK:
251 * Variable is on the stack.
253 * If `index' is zero, use the normal frame register. Otherwise, bits
254 * 0..4 of `index' contain the frame register.
256 * Both DW_OP_fbreg and DW_OP_breg0 ... DW_OP_breg31 take an ULeb128
257 * argument - since this has an variable size, we set it to zero and
258 * manually add a 4 byte constant using DW_OP_plus.
261 /* Use the normal frame register (%ebp on the i386). */
262 * ((guint8 *) base_ptr)++ = DW_OP_fbreg;
264 /* Use a custom frame register. */
265 * ((guint8 *) base_ptr)++ = DW_OP_breg0 + (var->index & 0x001f);
266 * ((guint8 *) base_ptr)++ = 0;
267 * ((guint8 *) base_ptr)++ = DW_OP_const4s;
268 * ((gint32 *) base_ptr)++ = var->offset;
269 * ((guint8 *) base_ptr)++ = DW_OP_plus;
272 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
274 * Variable is in the register whose number is contained in bits 0..4
277 * We need to write exactly 8 bytes in this location description, so instead
278 * of filling up the rest with DW_OP_nop's just add the `offset' even if
281 * ((guint8 *) base_ptr)++ = DW_OP_reg0 + (var->index & 0x001f);
282 * ((guint8 *) base_ptr)++ = DW_OP_nop;
283 * ((guint8 *) base_ptr)++ = DW_OP_const4s;
284 * ((gint32 *) base_ptr)++ = var->offset;
285 * ((guint8 *) base_ptr)++ = DW_OP_plus;
288 case MONO_DEBUG_VAR_ADDRESS_MODE_TWO_REGISTERS:
290 * Variable is in two registers whose numbers are in bits 0..4 and 5..9 of
291 * the `index' field. Don't add `offset' since we have only two bytes left,
292 * fill them up with DW_OP_nop's.
294 * ((guint8 *) base_ptr)++ = DW_OP_reg0 + (var->index & 0x001f);
295 * ((guint8 *) base_ptr)++ = DW_OP_piece;
296 * ((guint8 *) base_ptr)++ = sizeof (int);
297 * ((guint8 *) base_ptr)++ = DW_OP_reg0 + ((var->index & 0x1f0) >> 5);
298 * ((guint8 *) base_ptr)++ = DW_OP_piece;
299 * ((guint8 *) base_ptr)++ = sizeof (int);
300 * ((guint8 *) base_ptr)++ = DW_OP_nop;
301 * ((guint8 *) base_ptr)++ = DW_OP_nop;
305 g_assert_not_reached ();
310 mono_debug_update_symbol_file (MonoDebugSymbolFile *symfile,
311 MonoDebugMethodInfoFunc method_info_func,
314 const char *reloc_ptr, *reloc_start, *reloc_end;
315 int version, already_relocated = 0;
318 if (!symfile->section_offsets [MONO_DEBUG_SYMBOL_SECTION_MONO_RELOC_TABLE].file_offset)
321 reloc_ptr = reloc_start = symfile->raw_contents +
322 symfile->section_offsets [MONO_DEBUG_SYMBOL_SECTION_MONO_RELOC_TABLE].file_offset;
324 version = *((guint16 *) reloc_ptr)++;
325 if (version != MONO_DEBUG_SYMBOL_FILE_VERSION) {
326 g_warning ("Symbol file %s has incorrect relocation table version "
327 "(expected %d, got %d)", symfile->file_name,
328 MONO_DEBUG_SYMBOL_FILE_VERSION, version);
332 already_relocated = *reloc_ptr;
333 *((char *) reloc_ptr)++ = 1;
335 reloc_size = *((guint32 *) reloc_ptr)++;
336 reloc_end = reloc_ptr + reloc_size;
338 while (reloc_ptr < reloc_end) {
339 int type, size, section, offset;
344 size = * ((guint32 *) reloc_ptr)++;
349 section = *tmp_ptr++;
350 offset = *((guint32 *) tmp_ptr)++;
352 if (section >= MONO_DEBUG_SYMBOL_SECTION_MAX) {
353 g_warning ("Symbol file %s contains a relocation entry for unknown section %d",
354 symfile->file_name, section);
358 if (!symfile->section_offsets [section].file_offset) {
359 g_warning ("Symbol file %s contains a relocation entry for non-existing "
360 "section %d", symfile->file_name, section);
364 base_ptr = symfile->raw_contents + symfile->section_offsets [section].file_offset;
365 base_ptr = base_ptr + offset;
368 case MRT_target_address_size:
369 * (guint8 *) base_ptr = sizeof (void *);
371 case MRT_method_start_address: {
372 int token = *((guint32 *) tmp_ptr)++;
373 MonoDebugMethodInfo *minfo;
375 minfo = method_info_func (symfile, token, user_data);
378 * (void **) base_ptr = 0;
383 g_message ("Start of `%s' relocated to %p", minfo->method->name, minfo->code_start);
386 * (void **) base_ptr = minfo->code_start;
390 case MRT_method_end_address: {
391 int token = *((guint32 *) tmp_ptr)++;
392 MonoDebugMethodInfo *minfo;
394 minfo = method_info_func (symfile, token, user_data);
397 * (void **) base_ptr = 0;
401 * (void **) base_ptr = (char *)minfo->code_start + minfo->code_size;
405 case MRT_il_offset: {
406 guint32 token = *((guint32 *) tmp_ptr)++;
407 guint32 original = *((guint32 *) tmp_ptr)++;
408 MonoDebugMethodInfo *minfo;
412 minfo = method_info_func (symfile, token, user_data);
415 * (void **) base_ptr = 0;
419 address = minfo->code_size;
421 for (i = 0; i < minfo->num_il_offsets; i++) {
422 MonoDebugILOffsetInfo *il = &minfo->il_offsets [i];
424 if (il->offset >= original) {
425 address = il->address;
431 g_message ("Relocating IL offset %04x in `%s' to %d (%p)",
432 original, minfo->method->name, address,
433 minfo->code_start + address);
436 * (void **) base_ptr = minfo->code_start + address;
440 case MRT_local_variable: {
441 guint32 token = *((guint32 *) tmp_ptr)++;
442 guint32 original = *((guint32 *) tmp_ptr)++;
443 MonoDebugMethodInfo *minfo;
445 minfo = method_info_func (symfile, token, user_data);
448 relocate_variable (NULL, base_ptr);
452 if (original > minfo->num_locals) {
453 g_warning ("Symbol file %s contains relocation entry for non-existing "
454 "local variable %d, but method %s only has %d local variables.",
455 symfile->file_name, original, minfo->method->name,
460 relocate_variable (&minfo->locals [original], base_ptr);
464 case MRT_method_parameter: {
465 guint32 token = *((guint32 *) tmp_ptr)++;
466 guint32 original = *((guint32 *) tmp_ptr)++;
467 MonoDebugMethodInfo *minfo;
469 minfo = method_info_func (symfile, token, user_data);
472 relocate_variable (NULL, base_ptr);
476 if (minfo->method->signature->hasthis) {
478 relocate_variable (minfo->this_var, base_ptr);
485 if (original > minfo->num_params) {
486 g_warning ("Symbol file %s contains relocation entry for non-existing "
487 "parameter %d, but method %s only has %d parameters.",
488 symfile->file_name, original, minfo->method->name,
493 relocate_variable (&minfo->params [original], base_ptr);
497 case MRT_type_sizeof: {
498 guint32 token = *((guint32 *) tmp_ptr)++;
499 MonoClass *klass = mono_debug_class_get (symfile, token);
504 mono_class_init (klass);
506 if (klass->enumtype || klass->valuetype)
507 * (gint8 *) base_ptr = klass->instance_size - sizeof (MonoObject);
509 * (gint8 *) base_ptr = klass->instance_size;
513 case MRT_type_field_offset: {
514 guint32 token = *((guint32 *) tmp_ptr)++;
515 guint32 original = *((guint32 *) tmp_ptr)++;
516 MonoClass *klass = mono_debug_class_get (symfile, token);
522 mono_class_init (klass);
524 if (original > klass->field.count) {
525 g_warning ("Symbol file %s contains invalid field offset entry.",
533 off = klass->fields [original].offset;
534 if (klass->byval_arg.type == MONO_TYPE_VALUETYPE)
535 off -= sizeof (MonoObject);
538 g_message ("Setting field %d of type %u to offset %d", original,
542 * (guint32 *) base_ptr = off;
546 case MRT_mono_string_sizeof:
547 * (gint8 *) base_ptr = sizeof (MonoString);
550 case MRT_mono_string_offset: {
551 guint32 idx = *((guint32 *) tmp_ptr)++;
556 case MRI_string_offset_length:
557 off = (guchar *) &string.length - (guchar *) &string;
560 case MRI_string_offset_vector:
561 // fixme: don know how to fix this
562 g_assert_not_reached ();
563 //off = (guchar *) &string.c_str - (guchar *) &string;
567 g_warning ("Symbol file %s contains invalid string offset entry",
572 * (guint32 *) base_ptr = off;
576 case MRT_mono_array_sizeof:
577 * (gint8 *) base_ptr = sizeof (MonoArray);
580 case MRT_mono_array_offset: {
581 guint32 idx = *((guint32 *) tmp_ptr)++;
586 case MRI_array_offset_bounds:
587 off = (guchar *) &array.bounds - (guchar *) &array;
590 case MRI_array_offset_max_length:
591 off = (guchar *) &array.max_length - (guchar *) &array;
594 case MRI_array_offset_vector:
595 off = (guchar *) &array.vector - (guchar *) &array;
599 g_warning ("Symbol file %s contains invalid array offset entry",
604 * (guint32 *) base_ptr = off;
609 case MRT_mono_array_bounds_sizeof:
610 * (gint8 *) base_ptr = sizeof (MonoArrayBounds);
613 case MRT_mono_array_bounds_offset: {
614 guint32 idx = *((guint32 *) tmp_ptr)++;
615 MonoArrayBounds bounds;
619 case MRI_array_bounds_offset_lower:
620 off = (guchar *) &bounds.lower_bound - (guchar *) &bounds;
623 case MRI_array_bounds_offset_length:
624 off = (guchar *) &bounds.length - (guchar *) &bounds;
628 g_warning ("Symbol file %s contains invalid array bounds offset entry",
633 * (guint32 *) base_ptr = off;
638 case MRT_variable_start_scope: {
639 guint32 token = *((guint32 *) tmp_ptr)++;
640 guint32 original = *((guint32 *) tmp_ptr)++;
641 MonoDebugMethodInfo *minfo;
644 minfo = method_info_func (symfile, token, user_data);
647 * (void **) base_ptr = 0;
651 if (original > minfo->num_locals) {
652 g_warning ("Symbol file %s contains relocation entry for non-existing "
653 "local variable %d, but method %s only has %d local variables.",
654 symfile->file_name, original, minfo->method->name,
659 address = minfo->locals [original].begin_scope;
661 * (void **) base_ptr = minfo->code_start + address;
666 case MRT_variable_end_scope: {
667 guint32 token = *((guint32 *) tmp_ptr)++;
668 guint32 original = *((guint32 *) tmp_ptr)++;
669 MonoDebugMethodInfo *minfo;
672 minfo = method_info_func (symfile, token, user_data);
675 * (void **) base_ptr = 0;
679 if (original > minfo->num_locals) {
680 g_warning ("Symbol file %s contains relocation entry for non-existing "
681 "local variable %d, but method %s only has %d local variables.",
682 symfile->file_name, original, minfo->method->name,
687 address = minfo->locals [original].end_scope;
689 * (void **) base_ptr = minfo->code_start + address;
694 case MRT_mono_string_fieldsize: {
695 guint32 idx = *((guint32 *) tmp_ptr)++;
700 case MRI_string_offset_length:
701 fieldsize = sizeof (string.length);
704 case MRI_string_offset_vector:
706 //fieldsize = sizeof (string.c_str);
707 g_assert_not_reached ();
711 g_warning ("Symbol file %s contains invalid string fieldsize entry",
716 * (guint32 *) base_ptr = fieldsize;
721 case MRT_mono_array_fieldsize: {
722 guint32 idx = *((guint32 *) tmp_ptr)++;
727 case MRI_array_offset_bounds:
728 fieldsize = sizeof (array.bounds);
731 case MRI_array_offset_max_length:
732 fieldsize = sizeof (array.max_length);
735 case MRI_array_offset_vector:
736 fieldsize = sizeof (array.vector);
740 g_warning ("Symbol file %s contains invalid array fieldsize entry",
745 * (guint32 *) base_ptr = fieldsize;
752 g_warning ("Symbol file %s contains unknown relocation entry %d",
753 symfile->file_name, type);
758 mono_raw_buffer_update (symfile->raw_contents, symfile->raw_contents_size);
762 ves_icall_Debugger_MonoSymbolWriter_get_local_type_from_sig (MonoReflectionAssembly *assembly,
763 MonoArray *signature)
771 MONO_CHECK_ARG_NULL (assembly);
772 MONO_CHECK_ARG_NULL (signature);
774 domain = mono_domain_get();
775 image = assembly->assembly->image;
777 ptr = mono_array_addr (signature, char, 0);
778 g_assert (*ptr++ == 0x07);
779 len = mono_metadata_decode_value (ptr, &ptr);
782 type = mono_metadata_parse_type (image, MONO_PARSE_LOCAL, 0, ptr, &ptr);
784 return mono_type_get_object (domain, type);
787 MonoReflectionMethod *
788 ves_icall_Debugger_MonoSymbolWriter_method_from_token (MonoReflectionAssembly *assembly, guint32 token)
794 MONO_CHECK_ARG_NULL (assembly);
796 domain = mono_domain_get();
797 image = assembly->assembly->image;
799 method = mono_get_method (image, token, NULL);
801 return mono_method_get_object (domain, method);
805 ves_icall_Debugger_DwarfFileWriter_get_type_token (MonoReflectionType *type)
807 MonoClass *klass = mono_class_from_mono_type (type->type);
809 mono_class_init (klass);
811 return klass->type_token;