2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief DWARF debugging info support
23 * @author Matthias Braun
31 #include "bedwarf_t.h"
45 #include "lc_opts_enum.h"
48 #include "beemitter.h"
58 static int debug_level = LEVEL_NONE;
61 * Usually we simply use the DW_TAG_xxx numbers for our abbrev IDs, but for
62 * the cases where we need multiple ids with the same DW_TAG we define new IDs
65 typedef enum custom_abbrevs {
66 abbrev_void_pointer_type = 100,
67 abbrev_unnamed_formal_parameter,
68 abbrev_void_subroutine_type,
69 abbrev_bitfield_member,
75 typedef struct dwarf_t {
76 const ir_entity *cur_ent; /**< current method entity */
77 const be_stack_layout_t *layout; /**< current stack layout */
78 unsigned next_type_nr; /**< next type number */
79 pmap *file_map; /**< a map from file names to number in file list */
80 const char **file_list;
81 const ir_entity **pubnames_list;
82 pset_new_t emitted_types;
83 const char *main_file; /**< name of the main source file */
84 const char *curr_file; /**< name of the current source file */
91 static dwarf_source_language language;
92 static const char *comp_dir;
94 static unsigned insert_file(const char *filename)
97 void *entry = pmap_get(env.file_map, filename);
99 return PTR_TO_INT(entry);
101 ARR_APP1(const char*, env.file_list, filename);
102 num = (unsigned)ARR_LEN(env.file_list);
103 pmap_insert(env.file_map, filename, INT_TO_PTR(num));
104 /* TODO: quote chars in string */
105 be_emit_irprintf("\t.file %u \"%s\"\n", num, filename);
109 static void emit_int32(uint32_t value)
111 be_emit_irprintf("\t.long %u\n", value);
112 be_emit_write_line();
115 static void emit_int16(uint16_t value)
117 be_emit_irprintf("\t.short %u\n", value);
118 be_emit_write_line();
121 static void emit_int8(uint8_t value)
123 be_emit_irprintf("\t.byte %u\n", value);
124 be_emit_write_line();
127 static void emit_uleb128(unsigned value)
129 be_emit_irprintf("\t.uleb128 0x%x\n", value);
130 be_emit_write_line();
133 static unsigned get_uleb128_size(unsigned value)
139 } while (value != 0);
143 static void emit_string(const char *string)
145 be_emit_irprintf("\t.asciz \"%s\"\n", string);
146 be_emit_write_line();
149 static void emit_ref(const ir_entity *entity)
151 be_emit_cstring("\t.long ");
152 be_gas_emit_entity(entity);
154 be_emit_write_line();
157 static void emit_string_printf(const char *fmt, ...)
161 be_emit_cstring("\t.asciz \"");
162 be_emit_irvprintf(fmt, ap);
163 be_emit_cstring("\"\n");
166 be_emit_write_line();
169 static void emit_address(const char *name)
171 be_emit_cstring("\t.long ");
172 be_emit_string(be_gas_get_private_prefix());
173 be_emit_string(name);
175 be_emit_write_line();
178 static void emit_size(const char *from_label, const char *to_label)
180 be_emit_cstring("\t.long ");
181 be_emit_string(be_gas_get_private_prefix());
182 be_emit_string(to_label);
183 be_emit_cstring(" - ");
184 be_emit_string(be_gas_get_private_prefix());
185 be_emit_string(from_label);
187 be_emit_write_line();
190 static void emit_label(const char *name)
192 be_emit_string(be_gas_get_private_prefix());
193 be_emit_string(name);
194 be_emit_cstring(":\n");
195 be_emit_write_line();
198 static void register_attribute(dwarf_attribute attribute, dwarf_form form)
200 emit_uleb128(attribute);
204 static void begin_abbrev(unsigned code, dwarf_tag tag, dw_children children)
211 static void end_abbrev(void)
217 static void emit_line_info(void)
219 be_gas_emit_switch_section(GAS_SECTION_DEBUG_LINE);
221 emit_label("line_section_begin");
222 /* on elf systems gas handles producing the line info for us, and we
223 * don't have to do anything */
224 if (be_gas_object_file_format != OBJECT_FILE_FORMAT_ELF) {
226 emit_size("line_info_begin", "line_info_end");
228 emit_label("line_info_begin");
229 emit_int16(2); /* version */
230 emit_size("line_info_prolog_begin", "line_info_prolog_end");
231 emit_label("line_info_prolog_begin");
232 emit_int8(1); /* len of smallest instruction TODO: query from backend */
233 emit_int8(1); /* default is statement */
234 emit_int8(246); /* line base */
235 emit_int8(245); /* line range */
236 emit_int8(10); /* opcode base */
248 /* include directory list */
249 emit_string("/foo/bar");
253 for (i = 0; i < ARR_LEN(env.file_list); ++i) {
254 emit_string(env.file_list[i]);
255 emit_uleb128(1); /* directory */
256 emit_uleb128(0); /* modification time */
257 emit_uleb128(0); /* file length */
261 emit_label("line_info_prolog_end");
263 /* TODO: put the line_info program here */
265 emit_label("line_info_end");
269 static void emit_pubnames(void)
273 be_gas_emit_switch_section(GAS_SECTION_DEBUG_PUBNAMES);
275 emit_size("pubnames_begin", "pubnames_end");
276 emit_label("pubnames_begin");
278 emit_int16(2); /* version */
279 emit_size("info_section_begin", "info_begin");
280 emit_size("compile_unit_begin", "compile_unit_end");
282 for (i = 0; i < ARR_LEN(env.pubnames_list); ++i) {
283 const ir_entity *entity = env.pubnames_list[i];
284 be_emit_irprintf("\t.long %sE%ld - %sinfo_begin\n",
285 be_gas_get_private_prefix(),
286 get_entity_nr(entity), be_gas_get_private_prefix());
287 emit_string(get_entity_name(entity));
291 emit_label("pubnames_end");
294 void be_dwarf_location(dbg_info *dbgi)
299 if (debug_level < LEVEL_LOCATIONS)
301 loc = ir_retrieve_dbg_info(dbgi);
305 filenum = insert_file(loc.file);
306 be_emit_irprintf("\t.loc %u %u %u\n", filenum, loc.line, loc.column);
307 be_emit_write_line();
310 static bool is_extern_entity(const ir_entity *entity)
312 ir_visited_t visibility = get_entity_visibility(entity);
313 return visibility == ir_visibility_default
314 || visibility == ir_visibility_external;
317 static void emit_entity_label(const ir_entity *entity)
319 be_emit_irprintf("%sE%ld:\n", be_gas_get_private_prefix(),
320 get_entity_nr(entity));
321 be_emit_write_line();
324 static void register_dbginfo_attributes(void)
326 register_attribute(DW_AT_decl_file, DW_FORM_udata);
327 register_attribute(DW_AT_decl_line, DW_FORM_udata);
328 register_attribute(DW_AT_decl_column, DW_FORM_udata);
331 static void emit_dbginfo(const dbg_info *dbgi)
333 src_loc_t const loc = ir_retrieve_dbg_info(dbgi);
334 unsigned const file = loc.file ? insert_file(loc.file) : 0;
336 emit_uleb128(loc.line);
337 emit_uleb128(loc.column);
340 static void emit_subprogram_abbrev(void)
342 begin_abbrev(DW_TAG_subprogram, DW_TAG_subprogram, DW_CHILDREN_no);
343 register_attribute(DW_AT_name, DW_FORM_string);
344 register_dbginfo_attributes();
345 //register_attribute(DW_AT_prototyped, DW_FORM_flag);
346 //register_attribute(DW_AT_type, DW_FORM_ref4);
347 register_attribute(DW_AT_external, DW_FORM_flag);
348 register_attribute(DW_AT_low_pc, DW_FORM_addr);
349 register_attribute(DW_AT_high_pc, DW_FORM_addr);
350 //register_attribute(DW_AT_frame_base, DW_FORM_block1);
354 void be_dwarf_method_begin(const ir_entity *entity)
356 if (debug_level < LEVEL_BASIC)
358 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
360 emit_entity_label(entity);
361 emit_uleb128(DW_TAG_subprogram);
362 emit_string(get_entity_ld_name(entity));
363 emit_dbginfo(get_entity_dbg_info(entity));
364 emit_int8(is_extern_entity(entity));
366 be_emit_irprintf("\t.long %smethod_end_%s\n", be_gas_get_private_prefix(),
367 get_entity_ld_name(entity));
369 ARR_APP1(const ir_entity*, env.pubnames_list, entity);
371 env.cur_ent = entity;
374 void be_dwarf_method_end(void)
376 if (debug_level < LEVEL_BASIC)
378 const ir_entity *entity = env.cur_ent;
379 be_emit_irprintf("%smethod_end_%s:\n", be_gas_get_private_prefix(),
380 get_entity_ld_name(entity));
383 static void emit_type(ir_type *type);
385 static void emit_base_type_abbrev(void)
387 begin_abbrev(DW_TAG_base_type, DW_TAG_base_type, DW_CHILDREN_no);
388 register_attribute(DW_AT_encoding, DW_FORM_data1);
389 register_attribute(DW_AT_byte_size, DW_FORM_data1);
390 register_attribute(DW_AT_name, DW_FORM_string);
394 static void emit_type_label(const ir_type *type)
396 be_emit_irprintf("%sT%ld:\n", be_gas_get_private_prefix(), get_type_nr(type));
397 be_emit_write_line();
400 static void emit_type_address(const ir_type *type)
402 be_emit_irprintf("\t.long %sT%ld - %sinfo_begin\n",
403 be_gas_get_private_prefix(),
404 get_type_nr(type), be_gas_get_private_prefix());
405 be_emit_write_line();
408 static void emit_base_type(const ir_type *type)
411 ir_mode *mode = get_type_mode(type);
412 ir_print_type(buf, sizeof(buf), type);
414 emit_type_label(type);
415 emit_uleb128(DW_TAG_base_type);
416 if (mode_is_int(mode)) {
418 if (strcmp(buf, "_Bool")==0 || strcmp(buf, "bool")==0) {
419 emit_int8(DW_ATE_boolean);
421 emit_int8(mode_is_signed(mode) ? DW_ATE_signed : DW_ATE_unsigned);
423 } else if (mode_is_reference(mode)) {
424 emit_int8(DW_ATE_address);
425 } else if (mode_is_float(mode)) {
426 emit_int8(DW_ATE_float);
428 panic("mode not implemented yet");
430 emit_int8(get_mode_size_bytes(mode));
434 static void emit_pointer_type_abbrev(void)
436 begin_abbrev(DW_TAG_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
437 register_attribute(DW_AT_type, DW_FORM_ref4);
438 register_attribute(DW_AT_byte_size, DW_FORM_data1);
441 /* for void* pointer s*/
442 begin_abbrev(abbrev_void_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
443 register_attribute(DW_AT_byte_size, DW_FORM_data1);
447 static void emit_pointer_type(const ir_type *type)
449 ir_type *points_to = get_pointer_points_to_type(type);
450 unsigned size = get_type_size_bytes(type);
453 if (!is_Primitive_type(points_to) || get_type_mode(points_to) != mode_ANY) {
454 emit_type(points_to);
456 emit_type_label(type);
457 emit_uleb128(DW_TAG_pointer_type);
458 emit_type_address(points_to);
460 emit_type_label(type);
461 emit_uleb128(abbrev_void_pointer_type);
466 static void emit_array_type_abbrev(void)
468 begin_abbrev(DW_TAG_array_type, DW_TAG_array_type, DW_CHILDREN_yes);
469 register_attribute(DW_AT_type, DW_FORM_ref4);
472 begin_abbrev(DW_TAG_subrange_type, DW_TAG_subrange_type, DW_CHILDREN_no);
473 register_attribute(DW_AT_upper_bound, DW_FORM_udata);
477 static void emit_array_type(const ir_type *type)
479 ir_type *element_type = get_array_element_type(type);
481 if (get_array_n_dimensions(type) != 1)
482 panic("dwarf: multidimensional arrays no supported yet");
484 emit_type(element_type);
486 emit_type_label(type);
487 emit_uleb128(DW_TAG_array_type);
488 emit_type_address(element_type);
490 if (has_array_upper_bound(type, 0)) {
491 int bound = get_array_upper_bound_int(type, 0);
492 emit_uleb128(DW_TAG_subrange_type);
499 static void emit_compound_type_abbrev(void)
501 begin_abbrev(DW_TAG_structure_type, DW_TAG_structure_type, DW_CHILDREN_yes);
502 register_attribute(DW_AT_byte_size, DW_FORM_udata);
503 // TODO register_dbginfo_attributes();
506 begin_abbrev(DW_TAG_union_type, DW_TAG_union_type, DW_CHILDREN_yes);
507 register_attribute(DW_AT_byte_size, DW_FORM_udata);
508 // TODO register_dbginfo_attributes();
511 begin_abbrev(DW_TAG_class_type, DW_TAG_class_type, DW_CHILDREN_yes);
512 register_attribute(DW_AT_byte_size, DW_FORM_udata);
513 // TODO register_dbginfo_attributes();
516 begin_abbrev(DW_TAG_member, DW_TAG_member, DW_CHILDREN_no);
517 register_attribute(DW_AT_type, DW_FORM_ref4);
518 register_attribute(DW_AT_name, DW_FORM_string);
519 register_dbginfo_attributes();
520 register_attribute(DW_AT_data_member_location, DW_FORM_block1);
523 begin_abbrev(abbrev_bitfield_member, DW_TAG_member, DW_CHILDREN_no);
524 register_attribute(DW_AT_byte_size, DW_FORM_udata);
525 register_attribute(DW_AT_bit_size, DW_FORM_udata);
526 register_attribute(DW_AT_bit_offset, DW_FORM_udata);
527 register_attribute(DW_AT_type, DW_FORM_ref4);
528 register_attribute(DW_AT_name, DW_FORM_string);
529 register_dbginfo_attributes();
530 register_attribute(DW_AT_data_member_location, DW_FORM_block1);
534 static void emit_op_plus_uconst(unsigned value)
536 emit_int8(DW_OP_plus_uconst);
540 static void emit_compound_type(const ir_type *type)
543 size_t n_members = get_compound_n_members(type);
545 for (i = 0; i < n_members; ++i) {
546 ir_entity *member = get_compound_member(type, i);
547 ir_type *member_type = get_entity_type(member);
548 if (is_Primitive_type(member_type)) {
549 ir_type *base = get_primitive_base_type(member_type);
553 emit_type(member_type);
556 emit_type_label(type);
557 if (is_Struct_type(type)) {
558 emit_uleb128(DW_TAG_structure_type);
559 } else if (is_Union_type(type)) {
560 emit_uleb128(DW_TAG_union_type);
562 assert(is_Class_type(type));
563 emit_uleb128(DW_TAG_class_type);
565 emit_uleb128(get_type_size_bytes(type));
566 for (i = 0; i < n_members; ++i) {
567 ir_entity *member = get_compound_member(type, i);
568 ir_type *member_type = get_entity_type(member);
569 int offset = get_entity_offset(member);
572 if (is_Primitive_type(member_type) &&
573 (base = get_primitive_base_type(member_type))) {
574 unsigned bit_offset = get_entity_offset_bits_remainder(member);
575 unsigned base_size = get_type_size_bytes(base);
576 ir_mode *mode = get_type_mode(member_type);
577 unsigned bit_size = get_mode_size_bits(mode);
579 bit_offset = base_size*8 - bit_offset - bit_size;
581 emit_uleb128(abbrev_bitfield_member);
582 emit_uleb128(base_size);
583 emit_uleb128(bit_size);
584 emit_uleb128(bit_offset);
587 emit_uleb128(DW_TAG_member);
590 emit_type_address(member_type);
591 emit_string(get_entity_name(member));
592 emit_dbginfo(get_entity_dbg_info(member));
594 emit_int8(1 + get_uleb128_size(offset));
595 emit_op_plus_uconst(offset);
601 static void emit_subroutine_type_abbrev(void)
603 begin_abbrev(DW_TAG_subroutine_type,
604 DW_TAG_subroutine_type, DW_CHILDREN_yes);
605 register_attribute(DW_AT_prototyped, DW_FORM_flag);
606 register_attribute(DW_AT_type, DW_FORM_ref4);
609 begin_abbrev(abbrev_void_subroutine_type,
610 DW_TAG_subroutine_type, DW_CHILDREN_yes);
611 register_attribute(DW_AT_prototyped, DW_FORM_flag);
614 begin_abbrev(abbrev_unnamed_formal_parameter,
615 DW_TAG_formal_parameter, DW_CHILDREN_no);
616 register_attribute(DW_AT_type, DW_FORM_ref4);
620 static void emit_subroutine_type(const ir_type *type)
622 size_t n_params = get_method_n_params(type);
623 size_t n_ress = get_method_n_ress(type);
625 for (i = 0; i < n_params; ++i) {
626 ir_type *param_type = get_method_param_type(type, i);
627 emit_type(param_type);
629 for (i = 0; i < n_ress; ++i) {
630 ir_type *res_type = get_method_res_type(type, i);
634 emit_type_label(type);
635 emit_uleb128(n_ress == 0 ? abbrev_void_subroutine_type : DW_TAG_subroutine_type);
636 emit_int8(1); /* prototyped */
638 /* dwarf only supports 1 return type */
639 ir_type *res_type = get_method_res_type(type, 0);
640 emit_type_address(res_type);
643 for (i = 0; i < n_params; ++i) {
644 ir_type *param_type = get_method_param_type(type, i);
645 emit_uleb128(abbrev_unnamed_formal_parameter);
646 emit_type_address(param_type);
651 static void emit_type(ir_type *type)
653 if (pset_new_insert(&env.emitted_types, type))
656 switch (get_type_tpop_code(type)) {
657 case tpo_primitive: emit_base_type(type); break;
658 case tpo_pointer: emit_pointer_type(type); break;
659 case tpo_array: emit_array_type(type); break;
662 case tpo_union: emit_compound_type(type); break;
663 case tpo_method: emit_subroutine_type(type); break;
665 panic("bedwarf: type %+F not implemented yet", type);
669 static void emit_op_addr(const ir_entity *entity)
671 emit_int8(DW_OP_addr);
672 be_emit_cstring("\t.long ");
673 be_gas_emit_entity(entity);
675 be_emit_write_line();
678 static void emit_variable_abbrev(void)
680 begin_abbrev(DW_TAG_variable, DW_TAG_variable, DW_CHILDREN_no);
681 register_attribute(DW_AT_name, DW_FORM_string);
682 register_attribute(DW_AT_type, DW_FORM_ref4);
683 register_attribute(DW_AT_external, DW_FORM_flag);
684 register_dbginfo_attributes();
685 register_attribute(DW_AT_location, DW_FORM_block1);
689 void be_dwarf_variable(const ir_entity *entity)
691 ir_type *type = get_entity_type(entity);
693 if (debug_level < LEVEL_BASIC)
695 if (get_entity_ld_name(entity)[0] == '\0')
698 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
702 emit_entity_label(entity);
703 emit_uleb128(DW_TAG_variable);
704 emit_string(get_entity_ld_name(entity));
705 emit_type_address(type);
706 emit_int8(is_extern_entity(entity));
707 emit_dbginfo(get_entity_dbg_info(entity));
709 emit_int8(5); /* block length */
710 emit_op_addr(entity);
712 ARR_APP1(const ir_entity*, env.pubnames_list, entity);
715 static void emit_compile_unit_abbrev(void)
717 begin_abbrev(DW_TAG_compile_unit, DW_TAG_compile_unit, DW_CHILDREN_yes);
718 register_attribute(DW_AT_stmt_list, DW_FORM_data4);
719 register_attribute(DW_AT_producer, DW_FORM_string);
720 register_attribute(DW_AT_name, DW_FORM_string);
722 register_attribute(DW_AT_language, DW_FORM_data2);
723 if (comp_dir != NULL)
724 register_attribute(DW_AT_comp_dir, DW_FORM_string);
728 static void emit_abbrev(void)
730 /* create abbreviation for compile_unit */
731 be_gas_emit_switch_section(GAS_SECTION_DEBUG_ABBREV);
733 emit_label("abbrev_begin");
735 emit_compile_unit_abbrev();
736 emit_variable_abbrev();
737 emit_subprogram_abbrev();
738 emit_base_type_abbrev();
739 emit_pointer_type_abbrev();
740 emit_array_type_abbrev();
741 emit_compound_type_abbrev();
742 emit_subroutine_type_abbrev();
746 void be_dwarf_unit_begin(const char *filename)
748 if (debug_level < LEVEL_BASIC)
752 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
753 emit_label("info_section_begin");
754 emit_label("info_begin");
756 /* length of compilation unit info */
757 emit_size("compile_unit_begin", "compile_unit_end");
758 emit_label("compile_unit_begin");
759 emit_int16(2); /* dwarf version */
760 emit_address("abbrev_begin");
761 emit_int8(4); /* pointer size */
763 /* compile_unit die */
764 emit_uleb128(DW_TAG_compile_unit);
765 emit_address("line_section_begin");
766 emit_string_printf("libFirm (%u.%u %s)", ir_get_version_major(),
767 ir_get_version_minor(),
768 ir_get_version_revision());
769 emit_string(filename);
771 emit_int16(language);
772 if (comp_dir != NULL)
773 emit_string(comp_dir);
776 void be_dwarf_unit_end(void)
778 if (debug_level < LEVEL_BASIC)
780 be_gas_emit_switch_section(GAS_SECTION_TEXT);
781 emit_label("section_end");
783 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
784 emit_uleb128(0); /* end of compile_unit DIE */
786 emit_label("compile_unit_end");
792 void be_dwarf_close(void)
794 if (debug_level < LEVEL_BASIC)
796 pmap_destroy(env.file_map);
797 DEL_ARR_F(env.file_list);
798 DEL_ARR_F(env.pubnames_list);
799 pset_new_destroy(&env.emitted_types);
802 /* Opens a dwarf handler */
803 void be_dwarf_open(void)
805 if (debug_level < LEVEL_BASIC)
807 env.file_map = pmap_create();
808 env.file_list = NEW_ARR_F(const char*, 0);
809 env.pubnames_list = NEW_ARR_F(const ir_entity*, 0);
810 pset_new_init(&env.emitted_types);
813 void be_dwarf_set_source_language(dwarf_source_language new_language)
815 language = new_language;
818 void be_dwarf_set_compilation_directory(const char *new_comp_dir)
820 comp_dir = new_comp_dir;
823 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_dwarf)
824 void be_init_dwarf(void)
826 static const lc_opt_enum_int_items_t level_items[] = {
827 { "none", LEVEL_NONE },
828 { "basic", LEVEL_BASIC },
829 { "locations", LEVEL_LOCATIONS },
830 { "frameinfo", LEVEL_FRAMEINFO },
833 static lc_opt_enum_int_var_t debug_level_opt = {
834 &debug_level, level_items
836 static lc_opt_table_entry_t be_main_options[] = {
837 LC_OPT_ENT_ENUM_INT("debug", "debug output (dwarf) level",
841 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
842 lc_opt_add_table(be_grp, be_main_options);