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"
43 #include "be_dbgout_t.h"
46 #include "beemitter.h"
51 * Usually we simply use the DW_TAG_xxx numbers for our abbrev IDs, but for
52 * the cases where we need multiple ids with the same DW_TAG we define new IDs
55 typedef enum custom_abbrevs {
56 abbrev_void_pointer_type = 100,
57 abbrev_unnamed_formal_parameter,
58 abbrev_void_subroutine_type,
59 abbrev_bitfield_member,
65 typedef struct dwarf_t {
66 dbg_handle base; /**< the base class */
67 const ir_entity *cur_ent; /**< current method entity */
68 const be_stack_layout_t *layout; /**< current stack layout */
69 unsigned next_type_nr; /**< next type number */
70 pmap *file_map; /**< a map from file names to number in file list */
71 const char **file_list;
72 const ir_entity **pubnames_list;
73 pset_new_t emitted_types;
74 const char *main_file; /**< name of the main source file */
75 const char *curr_file; /**< name of the current source file */
80 static dwarf_source_language language;
81 static const char *comp_dir;
83 static unsigned insert_file(dwarf_t *env, const char *filename)
86 void *entry = pmap_get(env->file_map, filename);
88 return PTR_TO_INT(entry);
90 ARR_APP1(const char*, env->file_list, filename);
91 num = (unsigned)ARR_LEN(env->file_list);
92 pmap_insert(env->file_map, filename, INT_TO_PTR(num));
93 /* TODO: quote chars in string */
94 be_emit_irprintf("\t.file %u \"%s\"\n", num, filename);
98 static void emit_int32(uint32_t value)
100 be_emit_irprintf("\t.long %u\n", value);
101 be_emit_write_line();
104 static void emit_int16(uint16_t value)
106 be_emit_irprintf("\t.short %u\n", value);
107 be_emit_write_line();
110 static void emit_int8(uint8_t value)
112 be_emit_irprintf("\t.byte %u\n", value);
113 be_emit_write_line();
116 static void emit_uleb128(unsigned value)
118 be_emit_irprintf("\t.uleb128 0x%x\n", value);
119 be_emit_write_line();
122 static unsigned get_uleb128_size(unsigned value)
128 } while (value != 0);
132 static void emit_string(const char *string)
134 be_emit_irprintf("\t.asciz \"%s\"\n", string);
135 be_emit_write_line();
138 static void emit_ref(const ir_entity *entity)
140 be_emit_cstring("\t.long ");
141 be_gas_emit_entity(entity);
143 be_emit_write_line();
146 static void emit_string_printf(const char *fmt, ...)
150 be_emit_cstring("\t.asciz \"");
151 be_emit_irvprintf(fmt, ap);
152 be_emit_cstring("\"\n");
155 be_emit_write_line();
158 static void emit_address(const char *name)
160 be_emit_cstring("\t.long ");
161 be_emit_string(be_gas_get_private_prefix());
162 be_emit_string(name);
164 be_emit_write_line();
167 static void emit_size(const char *from_label, const char *to_label)
169 be_emit_cstring("\t.long ");
170 be_emit_string(be_gas_get_private_prefix());
171 be_emit_string(to_label);
172 be_emit_cstring(" - ");
173 be_emit_string(be_gas_get_private_prefix());
174 be_emit_string(from_label);
176 be_emit_write_line();
179 static void emit_label(const char *name)
181 be_emit_string(be_gas_get_private_prefix());
182 be_emit_string(name);
183 be_emit_cstring(":\n");
184 be_emit_write_line();
187 static void register_attribute(dwarf_attribute attribute, dwarf_form form)
189 emit_uleb128(attribute);
193 static void begin_abbrev(unsigned code, dwarf_tag tag, dw_children children)
200 static void end_abbrev(void)
206 static void emit_line_info(dwarf_t *env)
208 be_gas_emit_switch_section(GAS_SECTION_DEBUG_LINE);
210 emit_label("line_section_begin");
211 /* on elf systems gas handles producing the line info for us, and we
212 * don't have to do anything */
213 if (be_gas_object_file_format != OBJECT_FILE_FORMAT_ELF) {
215 emit_size("line_info_begin", "line_info_end");
217 emit_label("line_info_begin");
218 emit_int16(2); /* version */
219 emit_size("line_info_prolog_begin", "line_info_prolog_end");
220 emit_label("line_info_prolog_begin");
221 emit_int8(1); /* len of smallest instruction TODO: query from backend */
222 emit_int8(1); /* default is statement */
223 emit_int8(246); /* line base */
224 emit_int8(245); /* line range */
225 emit_int8(10); /* opcode base */
237 /* include directory list */
238 emit_string("/foo/bar");
242 for (i = 0; i < ARR_LEN(env->file_list); ++i) {
243 emit_string(env->file_list[i]);
244 emit_uleb128(1); /* directory */
245 emit_uleb128(0); /* modification time */
246 emit_uleb128(0); /* file length */
250 emit_label("line_info_prolog_end");
252 /* TODO: put the line_info program here */
254 emit_label("line_info_end");
258 static void emit_pubnames(dwarf_t *env)
262 be_gas_emit_switch_section(GAS_SECTION_DEBUG_PUBNAMES);
264 emit_size("pubnames_begin", "pubnames_end");
265 emit_label("pubnames_begin");
267 emit_int16(2); /* version */
268 emit_size("info_section_begin", "info_begin");
269 emit_size("compile_unit_begin", "compile_unit_end");
271 for (i = 0; i < ARR_LEN(env->pubnames_list); ++i) {
272 const ir_entity *entity = env->pubnames_list[i];
273 be_emit_irprintf("\t.long %sE%ld - %sinfo_begin\n",
274 be_gas_get_private_prefix(),
275 get_entity_nr(entity), be_gas_get_private_prefix());
276 emit_string(get_entity_name(entity));
280 emit_label("pubnames_end");
283 static void dwarf_set_dbg_info(dbg_handle *h, dbg_info *dbgi)
285 dwarf_t *const env = (dwarf_t*)h;
286 src_loc_t const loc = ir_retrieve_dbg_info(dbgi);
292 filenum = insert_file(env, loc.file);
293 be_emit_irprintf("\t.loc %u %u %u\n", filenum, loc.line, loc.column);
294 be_emit_write_line();
297 static bool is_extern_entity(const ir_entity *entity)
299 ir_visited_t visibility = get_entity_visibility(entity);
300 return visibility == ir_visibility_default
301 || visibility == ir_visibility_external;
304 static void emit_entity_label(const ir_entity *entity)
306 be_emit_irprintf("%sE%ld:\n", be_gas_get_private_prefix(),
307 get_entity_nr(entity));
308 be_emit_write_line();
311 static void register_dbginfo_attributes(void)
313 register_attribute(DW_AT_decl_file, DW_FORM_udata);
314 register_attribute(DW_AT_decl_line, DW_FORM_udata);
315 register_attribute(DW_AT_decl_column, DW_FORM_udata);
319 * Emit values for DW_AT_decl_file, DW_AT_decl_line and DW_AT_decl_column.
321 static void emit_dbginfo(dwarf_t *env, const dbg_info *dbgi)
323 src_loc_t const loc = ir_retrieve_dbg_info(dbgi);
324 unsigned const file = loc.file ? insert_file(env, loc.file) : 0;
326 emit_uleb128(loc.line);
327 emit_uleb128(loc.column);
330 static void emit_subprogram_abbrev(void)
332 begin_abbrev(DW_TAG_subprogram, DW_TAG_subprogram, DW_CHILDREN_no);
333 register_attribute(DW_AT_name, DW_FORM_string);
334 register_dbginfo_attributes();
335 //register_attribute(DW_AT_prototyped, DW_FORM_flag);
336 //register_attribute(DW_AT_type, DW_FORM_ref4);
337 register_attribute(DW_AT_external, DW_FORM_flag);
338 register_attribute(DW_AT_low_pc, DW_FORM_addr);
339 register_attribute(DW_AT_high_pc, DW_FORM_addr);
340 //register_attribute(DW_AT_frame_base, DW_FORM_block1);
345 * dump the dwarf for a method begin
347 static void dwarf_method_begin(dbg_handle *handle, const ir_entity *entity)
349 dwarf_t *env = (dwarf_t*)handle;
351 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
353 emit_entity_label(entity);
354 emit_uleb128(DW_TAG_subprogram);
355 emit_string(get_entity_ld_name(entity));
356 emit_dbginfo(env, get_entity_dbg_info(entity));
357 emit_int8(is_extern_entity(entity));
359 be_emit_irprintf("\t.long %smethod_end_%s\n", be_gas_get_private_prefix(),
360 get_entity_ld_name(entity));
362 ARR_APP1(const ir_entity*, env->pubnames_list, entity);
364 env->cur_ent = entity;
368 * dump the dwarf for a method end
370 static void dwarf_method_end(dbg_handle *handle)
372 dwarf_t *env = (dwarf_t*)handle;
373 const ir_entity *entity = env->cur_ent;
375 be_emit_irprintf("%smethod_end_%s:\n", be_gas_get_private_prefix(),
376 get_entity_ld_name(entity));
379 static void dwarf_types(dbg_handle *handle)
384 static void emit_type(dwarf_t *env, ir_type *type);
386 static void emit_base_type_abbrev(void)
388 begin_abbrev(DW_TAG_base_type, DW_TAG_base_type, DW_CHILDREN_no);
389 register_attribute(DW_AT_encoding, DW_FORM_data1);
390 register_attribute(DW_AT_byte_size, DW_FORM_data1);
391 register_attribute(DW_AT_name, DW_FORM_string);
395 static void emit_type_label(const ir_type *type)
397 be_emit_irprintf("%sT%ld:\n", be_gas_get_private_prefix(), get_type_nr(type));
398 be_emit_write_line();
401 static void emit_type_address(const ir_type *type)
403 be_emit_irprintf("\t.long %sT%ld - %sinfo_begin\n",
404 be_gas_get_private_prefix(),
405 get_type_nr(type), be_gas_get_private_prefix());
406 be_emit_write_line();
409 static void emit_base_type(const ir_type *type)
412 ir_mode *mode = get_type_mode(type);
413 ir_print_type(buf, sizeof(buf), type);
415 emit_type_label(type);
416 emit_uleb128(DW_TAG_base_type);
417 if (mode_is_int(mode)) {
419 if (strcmp(buf, "_Bool")==0 || strcmp(buf, "bool")==0) {
420 emit_int8(DW_ATE_boolean);
422 emit_int8(mode_is_signed(mode) ? DW_ATE_signed : DW_ATE_unsigned);
424 } else if (mode_is_reference(mode)) {
425 emit_int8(DW_ATE_address);
426 } else if (mode_is_float(mode)) {
427 emit_int8(DW_ATE_float);
429 panic("mode not implemented yet");
431 emit_int8(get_mode_size_bytes(mode));
435 static void emit_pointer_type_abbrev(void)
437 begin_abbrev(DW_TAG_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
438 register_attribute(DW_AT_type, DW_FORM_ref4);
439 register_attribute(DW_AT_byte_size, DW_FORM_data1);
442 /* for void* pointer s*/
443 begin_abbrev(abbrev_void_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
444 register_attribute(DW_AT_byte_size, DW_FORM_data1);
448 static void emit_pointer_type(dwarf_t *env, const ir_type *type)
450 ir_type *points_to = get_pointer_points_to_type(type);
451 unsigned size = get_type_size_bytes(type);
454 if (!is_Primitive_type(points_to) || get_type_mode(points_to) != mode_ANY) {
455 emit_type(env, points_to);
457 emit_type_label(type);
458 emit_uleb128(DW_TAG_pointer_type);
459 emit_type_address(points_to);
461 emit_type_label(type);
462 emit_uleb128(abbrev_void_pointer_type);
467 static void emit_array_type_abbrev(void)
469 begin_abbrev(DW_TAG_array_type, DW_TAG_array_type, DW_CHILDREN_yes);
470 register_attribute(DW_AT_type, DW_FORM_ref4);
473 begin_abbrev(DW_TAG_subrange_type, DW_TAG_subrange_type, DW_CHILDREN_no);
474 register_attribute(DW_AT_upper_bound, DW_FORM_udata);
478 static void emit_array_type(dwarf_t *env, const ir_type *type)
480 ir_type *element_type = get_array_element_type(type);
482 if (get_array_n_dimensions(type) != 1)
483 panic("dwarf: multidimensional arrays no supported yet");
485 emit_type(env, element_type);
487 emit_type_label(type);
488 emit_uleb128(DW_TAG_array_type);
489 emit_type_address(element_type);
491 if (has_array_upper_bound(type, 0)) {
492 int bound = get_array_upper_bound_int(type, 0);
493 emit_uleb128(DW_TAG_subrange_type);
500 static void emit_compound_type_abbrev(void)
502 begin_abbrev(DW_TAG_structure_type, DW_TAG_structure_type, DW_CHILDREN_yes);
503 register_attribute(DW_AT_byte_size, DW_FORM_udata);
504 // TODO register_dbginfo_attributes();
507 begin_abbrev(DW_TAG_union_type, DW_TAG_union_type, DW_CHILDREN_yes);
508 register_attribute(DW_AT_byte_size, DW_FORM_udata);
509 // TODO register_dbginfo_attributes();
512 begin_abbrev(DW_TAG_class_type, DW_TAG_class_type, DW_CHILDREN_yes);
513 register_attribute(DW_AT_byte_size, DW_FORM_udata);
514 // TODO register_dbginfo_attributes();
517 begin_abbrev(DW_TAG_member, DW_TAG_member, DW_CHILDREN_no);
518 register_attribute(DW_AT_type, DW_FORM_ref4);
519 register_attribute(DW_AT_name, DW_FORM_string);
520 register_dbginfo_attributes();
521 register_attribute(DW_AT_data_member_location, DW_FORM_block1);
524 begin_abbrev(abbrev_bitfield_member, DW_TAG_member, DW_CHILDREN_no);
525 register_attribute(DW_AT_byte_size, DW_FORM_udata);
526 register_attribute(DW_AT_bit_size, DW_FORM_udata);
527 register_attribute(DW_AT_bit_offset, DW_FORM_udata);
528 register_attribute(DW_AT_type, DW_FORM_ref4);
529 register_attribute(DW_AT_name, DW_FORM_string);
530 register_dbginfo_attributes();
531 register_attribute(DW_AT_data_member_location, DW_FORM_block1);
535 static void emit_op_plus_uconst(unsigned value)
537 emit_int8(DW_OP_plus_uconst);
541 static void emit_compound_type(dwarf_t *env, const ir_type *type)
544 size_t n_members = get_compound_n_members(type);
546 for (i = 0; i < n_members; ++i) {
547 ir_entity *member = get_compound_member(type, i);
548 ir_type *member_type = get_entity_type(member);
549 if (is_Primitive_type(member_type)) {
550 ir_type *base = get_primitive_base_type(member_type);
554 emit_type(env, member_type);
557 emit_type_label(type);
558 if (is_Struct_type(type)) {
559 emit_uleb128(DW_TAG_structure_type);
560 } else if (is_Union_type(type)) {
561 emit_uleb128(DW_TAG_union_type);
563 assert(is_Class_type(type));
564 emit_uleb128(DW_TAG_class_type);
566 emit_uleb128(get_type_size_bytes(type));
567 for (i = 0; i < n_members; ++i) {
568 ir_entity *member = get_compound_member(type, i);
569 ir_type *member_type = get_entity_type(member);
570 int offset = get_entity_offset(member);
573 if (is_Primitive_type(member_type) &&
574 (base = get_primitive_base_type(member_type))) {
575 unsigned bit_offset = get_entity_offset_bits_remainder(member);
576 unsigned base_size = get_type_size_bytes(base);
577 ir_mode *mode = get_type_mode(member_type);
578 unsigned bit_size = get_mode_size_bits(mode);
580 bit_offset = base_size*8 - bit_offset - bit_size;
582 emit_uleb128(abbrev_bitfield_member);
583 emit_uleb128(base_size);
584 emit_uleb128(bit_size);
585 emit_uleb128(bit_offset);
588 emit_uleb128(DW_TAG_member);
591 emit_type_address(member_type);
592 emit_string(get_entity_name(member));
593 emit_dbginfo(env, get_entity_dbg_info(member));
595 emit_int8(1 + get_uleb128_size(offset));
596 emit_op_plus_uconst(offset);
602 static void emit_subroutine_type_abbrev(void)
604 begin_abbrev(DW_TAG_subroutine_type,
605 DW_TAG_subroutine_type, DW_CHILDREN_yes);
606 register_attribute(DW_AT_prototyped, DW_FORM_flag);
607 register_attribute(DW_AT_type, DW_FORM_ref4);
610 begin_abbrev(abbrev_void_subroutine_type,
611 DW_TAG_subroutine_type, DW_CHILDREN_yes);
612 register_attribute(DW_AT_prototyped, DW_FORM_flag);
615 begin_abbrev(abbrev_unnamed_formal_parameter,
616 DW_TAG_formal_parameter, DW_CHILDREN_no);
617 register_attribute(DW_AT_type, DW_FORM_ref4);
621 static void emit_subroutine_type(dwarf_t *env, const ir_type *type)
623 size_t n_params = get_method_n_params(type);
624 size_t n_ress = get_method_n_ress(type);
626 for (i = 0; i < n_params; ++i) {
627 ir_type *param_type = get_method_param_type(type, i);
628 emit_type(env, param_type);
630 for (i = 0; i < n_ress; ++i) {
631 ir_type *res_type = get_method_res_type(type, i);
632 emit_type(env, res_type);
635 emit_type_label(type);
636 emit_uleb128(n_ress == 0 ? abbrev_void_subroutine_type : DW_TAG_subroutine_type);
637 emit_int8(1); /* prototyped */
639 /* dwarf only supports 1 return type */
640 ir_type *res_type = get_method_res_type(type, 0);
641 emit_type_address(res_type);
644 for (i = 0; i < n_params; ++i) {
645 ir_type *param_type = get_method_param_type(type, i);
646 emit_uleb128(abbrev_unnamed_formal_parameter);
647 emit_type_address(param_type);
652 static void emit_type(dwarf_t *env, ir_type *type)
654 if (pset_new_insert(&env->emitted_types, type))
657 switch (get_type_tpop_code(type)) {
658 case tpo_primitive: emit_base_type(type); break;
659 case tpo_pointer: emit_pointer_type(env, type); break;
660 case tpo_array: emit_array_type(env, type); break;
663 case tpo_union: emit_compound_type(env, type); break;
664 case tpo_method: emit_subroutine_type(env, type); break;
666 panic("bedwarf: type %+F not implemented yet", type);
670 static void emit_op_addr(const ir_entity *entity)
672 emit_int8(DW_OP_addr);
673 be_emit_cstring("\t.long ");
674 be_gas_emit_entity(entity);
676 be_emit_write_line();
679 static void emit_variable_abbrev(void)
681 begin_abbrev(DW_TAG_variable, DW_TAG_variable, DW_CHILDREN_no);
682 register_attribute(DW_AT_name, DW_FORM_string);
683 register_attribute(DW_AT_type, DW_FORM_ref4);
684 register_attribute(DW_AT_external, DW_FORM_flag);
685 register_dbginfo_attributes();
686 register_attribute(DW_AT_location, DW_FORM_block1);
690 static void dwarf_variable(dbg_handle *handle, const ir_entity *entity)
692 dwarf_t *env = (dwarf_t*) handle;
693 ir_type *type = get_entity_type(entity);
695 if (get_entity_ld_name(entity)[0] == '\0')
698 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
700 emit_type(env, type);
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(env, 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();
747 * start a new source object (compilation unit)
749 static void dwarf_unit_begin(dbg_handle *handle, const char *filename)
755 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
756 emit_label("info_section_begin");
757 emit_label("info_begin");
759 /* length of compilation unit info */
760 emit_size("compile_unit_begin", "compile_unit_end");
761 emit_label("compile_unit_begin");
762 emit_int16(2); /* dwarf version */
763 emit_address("abbrev_begin");
764 emit_int8(4); /* pointer size */
766 /* compile_unit die */
767 emit_uleb128(DW_TAG_compile_unit);
768 emit_address("line_section_begin");
769 emit_string_printf("libFirm (%u.%u %s)", ir_get_version_major(),
770 ir_get_version_minor(),
771 ir_get_version_revision());
772 emit_string(filename);
774 emit_int16(language);
775 if (comp_dir != NULL)
776 emit_string(comp_dir);
779 static void dwarf_unit_end(dbg_handle *handle)
781 dwarf_t *env = (dwarf_t*)handle;
783 be_gas_emit_switch_section(GAS_SECTION_TEXT);
784 emit_label("section_end");
786 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
787 emit_uleb128(0); /* end of compile_unit DIE */
789 emit_label("compile_unit_end");
796 * Close the dwarf handler.
798 static void dwarf_close(dbg_handle *handle)
800 dwarf_t *h = (dwarf_t *)handle;
801 pmap_destroy(h->file_map);
802 DEL_ARR_F(h->file_list);
803 DEL_ARR_F(h->pubnames_list);
804 pset_new_destroy(&h->emitted_types);
808 /** The dwarf operations. */
809 static const debug_ops dwarf_ops = {
820 /* Opens a dwarf handler */
821 static dbg_handle *be_dwarf_open(void)
823 dwarf_t *h = XMALLOCZ(dwarf_t);
825 h->base.ops = &dwarf_ops;
826 h->file_map = pmap_create();
827 h->file_list = NEW_ARR_F(const char*, 0);
828 h->pubnames_list = NEW_ARR_F(const ir_entity*, 0);
829 pset_new_init(&h->emitted_types);
834 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_dwarf)
835 void be_init_dwarf(void)
837 be_register_dbgout_module("dwarf", be_dwarf_open);
840 void be_dwarf_set_source_language(dwarf_source_language new_language)
842 language = new_language;
845 void be_dwarf_set_compilation_directory(const char *new_comp_dir)
847 comp_dir = new_comp_dir;