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"
53 typedef struct dwarf_t {
54 dbg_handle base; /**< the base class */
55 const ir_entity *cur_ent; /**< current method entity */
56 const be_stack_layout_t *layout; /**< current stack layout */
57 unsigned next_type_nr; /**< next type number */
58 pmap *file_map; /**< a map from file names to number in file list */
59 const char **file_list;
60 const ir_entity **pubnames_list;
61 pset_new_t emitted_types;
62 const char *main_file; /**< name of the main source file */
63 const char *curr_file; /**< name of the current source file */
68 static dwarf_source_language language;
69 static const char *comp_dir;
71 static unsigned insert_file(dwarf_t *env, const char *filename)
74 void *entry = pmap_get(env->file_map, filename);
76 return PTR_TO_INT(entry);
78 ARR_APP1(const char*, env->file_list, filename);
79 num = (unsigned)ARR_LEN(env->file_list);
80 pmap_insert(env->file_map, filename, INT_TO_PTR(num));
81 /* TODO: quote chars in string */
82 be_emit_irprintf("\t.file %u \"%s\"\n", num, filename);
86 static void emit_int32(uint32_t value)
88 be_emit_irprintf("\t.long %u\n", value);
92 static void emit_int16(uint16_t value)
94 be_emit_irprintf("\t.short %u\n", value);
98 static void emit_int8(uint8_t value)
100 be_emit_irprintf("\t.byte %u\n", value);
101 be_emit_write_line();
104 static void emit_uleb128(unsigned value)
106 be_emit_irprintf("\t.uleb128 0x%x\n", value);
107 be_emit_write_line();
110 static unsigned get_uleb128_size(unsigned value)
116 } while (value != 0);
120 static void emit_string(const char *string)
122 be_emit_irprintf("\t.asciz \"%s\"\n", string);
123 be_emit_write_line();
126 static void emit_ref(const ir_entity *entity)
128 be_emit_cstring("\t.long ");
129 be_gas_emit_entity(entity);
131 be_emit_write_line();
134 static void emit_string_printf(const char *fmt, ...)
138 be_emit_cstring("\t.asciz \"");
139 be_emit_irvprintf(fmt, ap);
140 be_emit_cstring("\"\n");
143 be_emit_write_line();
146 static void emit_address(const char *name)
148 be_emit_cstring("\t.long ");
149 be_emit_string(be_gas_get_private_prefix());
150 be_emit_string(name);
152 be_emit_write_line();
155 static void emit_size(const char *from_label, const char *to_label)
157 be_emit_cstring("\t.long ");
158 be_emit_string(be_gas_get_private_prefix());
159 be_emit_string(to_label);
160 be_emit_cstring(" - ");
161 be_emit_string(be_gas_get_private_prefix());
162 be_emit_string(from_label);
164 be_emit_write_line();
167 static void emit_label(const char *name)
169 be_emit_string(be_gas_get_private_prefix());
170 be_emit_string(name);
171 be_emit_cstring(":\n");
172 be_emit_write_line();
175 static void register_attribute(dwarf_attribute attribute, dwarf_form form)
177 emit_uleb128(attribute);
181 static void begin_abbrev(unsigned code, dwarf_tag tag, dw_children children)
188 static void end_abbrev(void)
194 static void emit_line_info(dwarf_t *env)
196 be_gas_emit_switch_section(GAS_SECTION_DEBUG_LINE);
198 emit_label("line_section_begin");
199 /* on elf systems gas handles producing the line info for us, and we
200 * don't have to do anything */
201 if (be_gas_object_file_format != OBJECT_FILE_FORMAT_ELF) {
203 emit_size("line_info_begin", "line_info_end");
205 emit_label("line_info_begin");
206 emit_int16(2); /* version */
207 emit_size("line_info_prolog_begin", "line_info_prolog_end");
208 emit_label("line_info_prolog_begin");
209 emit_int8(1); /* len of smallest instruction TODO: query from backend */
210 emit_int8(1); /* default is statement */
211 emit_int8(246); /* line base */
212 emit_int8(245); /* line range */
213 emit_int8(10); /* opcode base */
225 /* include directory list */
226 emit_string("/foo/bar");
230 for (i = 0; i < ARR_LEN(env->file_list); ++i) {
231 emit_string(env->file_list[0]);
232 emit_uleb128(1); /* directory */
233 emit_uleb128(0); /* modification time */
234 emit_uleb128(0); /* file length */
238 emit_label("line_info_prolog_end");
240 /* TODO: put the line_info program here */
242 emit_label("line_info_end");
246 static void emit_pubnames(dwarf_t *env)
250 be_gas_emit_switch_section(GAS_SECTION_DEBUG_PUBNAMES);
252 emit_size("pubnames_begin", "pubnames_end");
253 emit_label("pubnames_begin");
255 emit_int16(2); /* version */
256 emit_size("info_section_begin", "info_begin");
257 emit_size("compile_unit_begin", "compile_unit_end");
259 for (i = 0; i < ARR_LEN(env->pubnames_list); ++i) {
260 const ir_entity *entity = env->pubnames_list[i];
261 be_emit_irprintf("\t.long %sE%ld - %sinfo_begin\n",
262 be_gas_get_private_prefix(),
263 get_entity_nr(entity), be_gas_get_private_prefix());
264 emit_string(get_entity_name(entity));
268 emit_label("pubnames_end");
271 static void dwarf_set_dbg_info(dbg_handle *h, dbg_info *dbgi)
273 dwarf_t *const env = (dwarf_t*)h;
274 src_loc_t const loc = ir_retrieve_dbg_info(dbgi);
280 filenum = insert_file(env, loc.file);
281 be_emit_irprintf("\t.loc %u %u %u\n", filenum, loc.line, loc.column);
282 be_emit_write_line();
285 static bool is_extern_entity(const ir_entity *entity)
287 ir_visited_t visibility = get_entity_visibility(entity);
288 return visibility == ir_visibility_default
289 || visibility == ir_visibility_external;
292 static void emit_entity_label(const ir_entity *entity)
294 be_emit_irprintf("%sE%ld:\n", be_gas_get_private_prefix(),
295 get_entity_nr(entity));
296 be_emit_write_line();
299 static void register_dbginfo_attributes(void)
301 register_attribute(DW_AT_decl_file, DW_FORM_udata);
302 register_attribute(DW_AT_decl_line, DW_FORM_udata);
303 register_attribute(DW_AT_decl_column, DW_FORM_udata);
307 * Emit values for DW_AT_decl_file, DW_AT_decl_line and DW_AT_decl_column.
309 static void emit_dbginfo(dwarf_t *env, const dbg_info *dbgi)
311 src_loc_t const loc = ir_retrieve_dbg_info(dbgi);
312 unsigned const file = loc.file ? insert_file(env, loc.file) : 0;
314 emit_uleb128(loc.line);
315 emit_uleb128(loc.column);
318 static void emit_subprogram_abbrev(void)
320 begin_abbrev(DW_TAG_subprogram, DW_TAG_subprogram, DW_CHILDREN_no);
321 register_attribute(DW_AT_name, DW_FORM_string);
322 register_dbginfo_attributes();
323 //register_attribute(DW_AT_prototyped, DW_FORM_flag);
324 //register_attribute(DW_AT_type, DW_FORM_ref4);
325 register_attribute(DW_AT_external, DW_FORM_flag);
326 register_attribute(DW_AT_low_pc, DW_FORM_addr);
327 register_attribute(DW_AT_high_pc, DW_FORM_addr);
328 //register_attribute(DW_AT_frame_base, DW_FORM_block1);
333 * dump the drwarf for a method begin
335 static void dwarf_method_begin(dbg_handle *handle, const ir_entity *entity)
337 dwarf_t *env = (dwarf_t*)handle;
339 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
341 emit_entity_label(entity);
342 emit_uleb128(DW_TAG_subprogram);
343 emit_string(get_entity_ld_name(entity));
344 emit_dbginfo(env, get_entity_dbg_info(entity));
345 emit_int8(is_extern_entity(entity));
347 be_emit_irprintf("\t.long %smethod_end_%s\n", be_gas_get_private_prefix(),
348 get_entity_ld_name(entity));
350 ARR_APP1(const ir_entity*, env->pubnames_list, entity);
352 env->cur_ent = entity;
356 * dump the drwarf for a method end
358 static void dwarf_method_end(dbg_handle *handle)
360 dwarf_t *env = (dwarf_t*)handle;
361 const ir_entity *entity = env->cur_ent;
363 be_emit_irprintf("%smethod_end_%s:\n", be_gas_get_private_prefix(),
364 get_entity_ld_name(entity));
367 static void dwarf_types(dbg_handle *handle)
372 static void emit_type(dwarf_t *env, ir_type *type);
374 static void emit_base_type_abbrev(void)
376 begin_abbrev(DW_TAG_base_type, DW_TAG_base_type, DW_CHILDREN_no);
377 register_attribute(DW_AT_encoding, DW_FORM_data1);
378 register_attribute(DW_AT_byte_size, DW_FORM_data1);
379 register_attribute(DW_AT_name, DW_FORM_string);
383 static void emit_type_label(const ir_type *type)
385 be_emit_irprintf("%sT%ld:\n", be_gas_get_private_prefix(), get_type_nr(type));
386 be_emit_write_line();
389 static void emit_type_address(const ir_type *type)
391 be_emit_irprintf("\t.long %sT%ld - %sinfo_begin\n",
392 be_gas_get_private_prefix(),
393 get_type_nr(type), be_gas_get_private_prefix());
394 be_emit_write_line();
397 static void emit_base_type(const ir_type *type)
400 ir_mode *mode = get_type_mode(type);
401 ir_print_type(buf, sizeof(buf), type);
403 emit_type_label(type);
404 emit_uleb128(DW_TAG_base_type);
405 if (mode_is_int(mode)) {
407 if (strcmp(buf, "_Bool")==0 || strcmp(buf, "bool")==0) {
408 emit_int8(DW_ATE_boolean);
410 emit_int8(mode_is_signed(mode) ? DW_ATE_signed : DW_ATE_unsigned);
412 } else if (mode_is_reference(mode)) {
413 emit_int8(DW_ATE_address);
414 } else if (mode_is_float(mode)) {
415 emit_int8(DW_ATE_float);
417 panic("mode not implemented yet");
419 emit_int8(get_mode_size_bytes(mode));
423 static void emit_pointer_type_abbrev(void)
425 begin_abbrev(DW_TAG_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
426 register_attribute(DW_AT_type, DW_FORM_ref4);
427 register_attribute(DW_AT_byte_size, DW_FORM_data1);
430 /* for void* pointer s*/
431 begin_abbrev(abbrev_void_pointer_type, DW_TAG_pointer_type, DW_CHILDREN_no);
432 register_attribute(DW_AT_byte_size, DW_FORM_data1);
436 static void emit_pointer_type(dwarf_t *env, const ir_type *type)
438 ir_type *points_to = get_pointer_points_to_type(type);
439 unsigned size = get_type_size_bytes(type);
442 if (!is_Primitive_type(points_to) || get_type_mode(points_to) != mode_ANY) {
443 emit_type(env, points_to);
445 emit_type_label(type);
446 emit_uleb128(DW_TAG_pointer_type);
447 emit_type_address(points_to);
449 emit_type_label(type);
450 emit_uleb128(abbrev_void_pointer_type);
455 static void emit_array_type_abbrev(void)
457 begin_abbrev(DW_TAG_array_type, DW_TAG_array_type, DW_CHILDREN_yes);
458 register_attribute(DW_AT_type, DW_FORM_ref4);
461 begin_abbrev(DW_TAG_subrange_type, DW_TAG_subrange_type, DW_CHILDREN_no);
462 register_attribute(DW_AT_upper_bound, DW_FORM_udata);
466 static void emit_array_type(dwarf_t *env, const ir_type *type)
468 ir_type *element_type = get_array_element_type(type);
470 if (get_array_n_dimensions(type) != 1)
471 panic("dwarf: multidimensional arrays no supported yet");
473 emit_type(env, element_type);
475 emit_type_label(type);
476 emit_uleb128(DW_TAG_array_type);
477 emit_type_address(element_type);
479 if (has_array_upper_bound(type, 0)) {
480 int bound = get_array_upper_bound_int(type, 0);
481 emit_uleb128(DW_TAG_subrange_type);
488 static void emit_compound_type_abbrev(void)
490 begin_abbrev(DW_TAG_structure_type, DW_TAG_structure_type, DW_CHILDREN_yes);
491 register_attribute(DW_AT_byte_size, DW_FORM_udata);
492 // TODO register_dbginfo_attributes();
495 begin_abbrev(DW_TAG_union_type, DW_TAG_union_type, DW_CHILDREN_yes);
496 register_attribute(DW_AT_byte_size, DW_FORM_udata);
497 // TODO register_dbginfo_attributes();
500 begin_abbrev(DW_TAG_class_type, DW_TAG_class_type, DW_CHILDREN_yes);
501 register_attribute(DW_AT_byte_size, DW_FORM_udata);
502 // TODO register_dbginfo_attributes();
505 begin_abbrev(DW_TAG_member, DW_TAG_member, DW_CHILDREN_no);
506 register_attribute(DW_AT_type, DW_FORM_ref4);
507 register_attribute(DW_AT_name, DW_FORM_string);
508 register_dbginfo_attributes();
509 register_attribute(DW_AT_data_member_location, DW_FORM_block1);
512 begin_abbrev(abbrev_bitfield_member, DW_TAG_member, DW_CHILDREN_no);
513 register_attribute(DW_AT_byte_size, DW_FORM_udata);
514 register_attribute(DW_AT_bit_size, DW_FORM_udata);
515 register_attribute(DW_AT_bit_offset, DW_FORM_udata);
516 register_attribute(DW_AT_type, DW_FORM_ref4);
517 register_attribute(DW_AT_name, DW_FORM_string);
518 register_dbginfo_attributes();
519 register_attribute(DW_AT_data_member_location, DW_FORM_block1);
523 static void emit_op_plus_uconst(unsigned value)
525 emit_int8(DW_OP_plus_uconst);
529 static void emit_compound_type(dwarf_t *env, const ir_type *type)
532 size_t n_members = get_compound_n_members(type);
534 for (i = 0; i < n_members; ++i) {
535 ir_entity *member = get_compound_member(type, i);
536 ir_type *member_type = get_entity_type(member);
537 if (is_Primitive_type(member_type)) {
538 ir_type *base = get_primitive_base_type(member_type);
542 emit_type(env, member_type);
545 emit_type_label(type);
546 if (is_Struct_type(type)) {
547 emit_uleb128(DW_TAG_structure_type);
548 } else if (is_Union_type(type)) {
549 emit_uleb128(DW_TAG_union_type);
551 assert(is_Class_type(type));
552 emit_uleb128(DW_TAG_class_type);
554 emit_uleb128(get_type_size_bytes(type));
555 for (i = 0; i < n_members; ++i) {
556 ir_entity *member = get_compound_member(type, i);
557 ir_type *member_type = get_entity_type(member);
558 int offset = get_entity_offset(member);
561 if (is_Primitive_type(member_type) &&
562 (base = get_primitive_base_type(member_type))) {
563 unsigned bit_offset = get_entity_offset_bits_remainder(member);
564 unsigned base_size = get_type_size_bytes(base);
565 ir_mode *mode = get_type_mode(member_type);
566 unsigned bit_size = get_mode_size_bits(mode);
568 bit_offset = base_size*8 - bit_offset - bit_size;
570 emit_uleb128(abbrev_bitfield_member);
571 emit_uleb128(base_size);
572 emit_uleb128(bit_size);
573 emit_uleb128(bit_offset);
576 emit_uleb128(DW_TAG_member);
579 emit_type_address(member_type);
580 emit_string(get_entity_name(member));
581 emit_dbginfo(env, get_entity_dbg_info(member));
583 emit_int8(1 + get_uleb128_size(offset));
584 emit_op_plus_uconst(offset);
590 static void emit_subroutine_type_abbrev(void)
592 begin_abbrev(DW_TAG_subroutine_type,
593 DW_TAG_subroutine_type, DW_CHILDREN_yes);
594 register_attribute(DW_AT_prototyped, DW_FORM_flag);
595 register_attribute(DW_AT_type, DW_FORM_ref4);
598 begin_abbrev(abbrev_void_subroutine_type,
599 DW_TAG_subroutine_type, DW_CHILDREN_yes);
600 register_attribute(DW_AT_prototyped, DW_FORM_flag);
603 begin_abbrev(abbrev_unnamed_formal_parameter,
604 DW_TAG_formal_parameter, DW_CHILDREN_no);
605 register_attribute(DW_AT_type, DW_FORM_ref4);
609 static void emit_subroutine_type(dwarf_t *env, const ir_type *type)
611 size_t n_params = get_method_n_params(type);
612 size_t n_ress = get_method_n_ress(type);
614 for (i = 0; i < n_params; ++i) {
615 ir_type *param_type = get_method_param_type(type, i);
616 emit_type(env, param_type);
618 for (i = 0; i < n_ress; ++i) {
619 ir_type *res_type = get_method_res_type(type, i);
620 emit_type(env, res_type);
623 emit_type_label(type);
624 emit_uleb128(n_ress == 0 ? abbrev_void_subroutine_type : DW_TAG_subroutine_type);
625 emit_int8(1); /* prototyped */
627 /* dwarf only supports 1 return type */
628 ir_type *res_type = get_method_res_type(type, 0);
629 emit_type_address(res_type);
632 for (i = 0; i < n_params; ++i) {
633 ir_type *param_type = get_method_param_type(type, i);
634 emit_uleb128(abbrev_unnamed_formal_parameter);
635 emit_type_address(param_type);
640 static void emit_type(dwarf_t *env, ir_type *type)
642 if (pset_new_insert(&env->emitted_types, type))
645 switch (get_type_tpop_code(type)) {
646 case tpo_primitive: emit_base_type(type); break;
647 case tpo_pointer: emit_pointer_type(env, type); break;
648 case tpo_array: emit_array_type(env, type); break;
651 case tpo_union: emit_compound_type(env, type); break;
652 case tpo_method: emit_subroutine_type(env, type); break;
654 panic("bedwarf: type %+F not implemented yet", type);
658 static void emit_op_addr(const ir_entity *entity)
660 emit_int8(DW_OP_addr);
661 be_emit_cstring("\t.long ");
662 be_gas_emit_entity(entity);
664 be_emit_write_line();
667 static void emit_variable_abbrev(void)
669 begin_abbrev(DW_TAG_variable, DW_TAG_variable, DW_CHILDREN_no);
670 register_attribute(DW_AT_name, DW_FORM_string);
671 register_attribute(DW_AT_type, DW_FORM_ref4);
672 register_attribute(DW_AT_external, DW_FORM_flag);
673 register_dbginfo_attributes();
674 register_attribute(DW_AT_location, DW_FORM_block1);
678 static void dwarf_variable(dbg_handle *handle, const ir_entity *entity)
680 dwarf_t *env = (dwarf_t*) handle;
681 ir_type *type = get_entity_type(entity);
683 if (get_entity_ld_name(entity)[0] == '\0')
686 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
688 emit_type(env, type);
690 emit_entity_label(entity);
691 emit_uleb128(DW_TAG_variable);
692 emit_string(get_entity_ld_name(entity));
693 emit_type_address(type);
694 emit_int8(is_extern_entity(entity));
695 emit_dbginfo(env, get_entity_dbg_info(entity));
697 emit_int8(5); /* block length */
698 emit_op_addr(entity);
700 ARR_APP1(const ir_entity*, env->pubnames_list, entity);
703 static void emit_compile_unit_abbrev(void)
705 begin_abbrev(DW_TAG_compile_unit, DW_TAG_compile_unit, DW_CHILDREN_yes);
706 register_attribute(DW_AT_stmt_list, DW_FORM_data4);
707 register_attribute(DW_AT_producer, DW_FORM_string);
708 register_attribute(DW_AT_name, DW_FORM_string);
710 register_attribute(DW_AT_language, DW_FORM_data2);
711 if (comp_dir != NULL)
712 register_attribute(DW_AT_comp_dir, DW_FORM_string);
716 static void emit_abbrev(void)
718 /* create abbreviation for compile_unit */
719 be_gas_emit_switch_section(GAS_SECTION_DEBUG_ABBREV);
721 emit_label("abbrev_begin");
723 emit_compile_unit_abbrev();
724 emit_variable_abbrev();
725 emit_subprogram_abbrev();
726 emit_base_type_abbrev();
727 emit_pointer_type_abbrev();
728 emit_array_type_abbrev();
729 emit_compound_type_abbrev();
730 emit_subroutine_type_abbrev();
735 * start a new source object (compilation unit)
737 static void dwarf_unit_begin(dbg_handle *handle, const char *filename)
743 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
744 emit_label("info_section_begin");
745 emit_label("info_begin");
747 /* length of compilation unit info */
748 emit_size("compile_unit_begin", "compile_unit_end");
749 emit_label("compile_unit_begin");
750 emit_int16(2); /* dwarf version */
751 emit_address("abbrev_begin");
752 emit_int8(4); /* pointer size */
754 /* compile_unit die */
755 emit_uleb128(DW_TAG_compile_unit);
756 emit_address("line_section_begin");
757 emit_string_printf("libFirm (%u.%u %s)", ir_get_version_major(),
758 ir_get_version_minor(),
759 ir_get_version_revision());
760 emit_string(filename);
762 emit_int16(DW_LANG_C_plus_plus);
763 if (comp_dir != NULL)
764 emit_string(comp_dir);
767 static void dwarf_unit_end(dbg_handle *handle)
769 dwarf_t *env = (dwarf_t*)handle;
771 be_gas_emit_switch_section(GAS_SECTION_TEXT);
772 emit_label("section_end");
774 be_gas_emit_switch_section(GAS_SECTION_DEBUG_INFO);
775 emit_uleb128(0); /* end of compile_unit DIE */
777 emit_label("compile_unit_end");
784 * Close the drwarf handler.
786 static void dwarf_close(dbg_handle *handle)
788 dwarf_t *h = (dwarf_t *)handle;
789 pmap_destroy(h->file_map);
790 DEL_ARR_F(h->file_list);
791 DEL_ARR_F(h->pubnames_list);
792 pset_new_destroy(&h->emitted_types);
796 /** The drwarf operations. */
797 static const debug_ops dwarf_ops = {
808 /* Opens a drwarf handler */
809 static dbg_handle *be_dwarf_open(void)
811 dwarf_t *h = XMALLOCZ(dwarf_t);
813 h->base.ops = &dwarf_ops;
814 h->file_map = pmap_create();
815 h->file_list = NEW_ARR_F(const char*, 0);
816 h->pubnames_list = NEW_ARR_F(const ir_entity*, 0);
817 pset_new_init(&h->emitted_types);
822 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_dwarf)
823 void be_init_dwarf(void)
825 be_register_dbgout_module("dwarf", be_dwarf_open);
828 void be_dwarf_set_source_language(dwarf_source_language new_language)
830 language = new_language;
833 void be_dwarf_set_compilation_directory(const char *new_comp_dir)
835 comp_dir = new_comp_dir;