1 /* Copyright (c) 2008-2014. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
13 #define DW_LANG_Objc DW_LANG_ObjC /* fix spelling error in older dwarf.h */
15 #include <elfutils/libdw.h>
17 #include <simgrid_config.h>
19 #include <xbt/sysdep.h>
21 #include "mc_object_info.h"
22 #include "mc_private.h"
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing");
26 /** \brief The default DW_TAG_lower_bound for a given DW_AT_language.
28 * The default for a given language is defined in the DWARF spec.
30 * \param language consant as defined by the DWARf spec
32 static uint64_t MC_dwarf_default_lower_bound(int lang);
34 /** \brief Computes the the element_count of a DW_TAG_enumeration_type DIE
36 * This is the number of elements in a given array dimension.
38 * A reference of the compilation unit (DW_TAG_compile_unit) is
39 * needed because the default lower bound (when there is no DW_AT_lower_bound)
40 * depends of the language of the compilation unit (DW_AT_language).
42 * \param die DIE for the DW_TAG_enumeration_type or DW_TAG_subrange_type
43 * \param unit DIE of the DW_TAG_compile_unit
45 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
48 /** \brief Computes the number of elements of a given DW_TAG_array_type.
50 * \param die DIE for the DW_TAG_array_type
52 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit);
54 /** \brief Process a DIE
56 * \param info the resulting object fot the library/binary file (output)
57 * \param die the current DIE
58 * \param unit the DIE of the compile unit of the current DIE
59 * \param frame containg frame if any
61 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
62 Dwarf_Die * unit, mc_frame_t frame,
65 /** \brief Process a type DIE
67 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
68 Dwarf_Die * unit, mc_frame_t frame,
71 /** \brief Calls MC_dwarf_handle_die on all childrend of the given die
73 * \param info the resulting object fot the library/binary file (output)
74 * \param die the current DIE
75 * \param unit the DIE of the compile unit of the current DIE
76 * \param frame containg frame if any
78 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
79 Dwarf_Die * unit, mc_frame_t frame,
82 /** \brief Handle a variable (DW_TAG_variable or other)
84 * \param info the resulting object fot the library/binary file (output)
85 * \param die the current DIE
86 * \param unit the DIE of the compile unit of the current DIE
87 * \param frame containg frame if any
89 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
90 Dwarf_Die * unit, mc_frame_t frame,
93 /** \brief Get the DW_TAG_type of the DIE
96 * \return DW_TAG_type attribute as a new string (NULL if none)
98 static std::uint64_t MC_dwarf_at_type(Dwarf_Die * die);
100 /** \brief A class of DWARF tags (DW_TAG_*)
102 typedef enum mc_tag_class {
111 static mc_tag_class MC_dwarf_tag_classify(int tag)
115 case DW_TAG_array_type:
116 case DW_TAG_class_type:
117 case DW_TAG_enumeration_type:
119 case DW_TAG_pointer_type:
120 case DW_TAG_reference_type:
121 case DW_TAG_rvalue_reference_type:
122 case DW_TAG_string_type:
123 case DW_TAG_structure_type:
124 case DW_TAG_subroutine_type:
125 case DW_TAG_union_type:
126 case DW_TAG_ptr_to_member_type:
127 case DW_TAG_set_type:
128 case DW_TAG_subrange_type:
129 case DW_TAG_base_type:
130 case DW_TAG_const_type:
131 case DW_TAG_file_type:
132 case DW_TAG_packed_type:
133 case DW_TAG_volatile_type:
134 case DW_TAG_restrict_type:
135 case DW_TAG_interface_type:
136 case DW_TAG_unspecified_type:
137 case DW_TAG_shared_type:
140 case DW_TAG_subprogram:
141 return mc_tag_subprogram;
143 case DW_TAG_variable:
144 case DW_TAG_formal_parameter:
145 return mc_tag_variable;
147 case DW_TAG_lexical_block:
148 case DW_TAG_try_block:
149 case DW_TAG_catch_block:
150 case DW_TAG_inlined_subroutine:
151 case DW_TAG_with_stmt:
154 case DW_TAG_namespace:
155 return mc_tag_namespace;
158 return mc_tag_unknown;
163 #define MC_DW_CLASS_UNKNOWN 0
164 #define MC_DW_CLASS_ADDRESS 1 // Location in the address space of the program
165 #define MC_DW_CLASS_BLOCK 2 // Arbitrary block of bytes
166 #define MC_DW_CLASS_CONSTANT 3
167 #define MC_DW_CLASS_STRING 3 // String
168 #define MC_DW_CLASS_FLAG 4 // Boolean
169 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
170 #define MC_DW_CLASS_EXPRLOC 6 // DWARF expression/location description
171 #define MC_DW_CLASS_LINEPTR 7
172 #define MC_DW_CLASS_LOCLISTPTR 8
173 #define MC_DW_CLASS_MACPTR 9
174 #define MC_DW_CLASS_RANGELISTPTR 10
176 /** \brief Find the DWARF data class for a given DWARF data form
178 * This mapping is defined in the DWARF spec.
180 * \param form The form (values taken from the DWARF spec)
181 * \return An internal representation for the corresponding class
183 static int MC_dwarf_form_get_class(int form)
187 return MC_DW_CLASS_ADDRESS;
192 return MC_DW_CLASS_BLOCK;
199 return MC_DW_CLASS_CONSTANT;
202 return MC_DW_CLASS_STRING;
203 case DW_FORM_ref_addr:
208 case DW_FORM_ref_udata:
209 return MC_DW_CLASS_REFERENCE;
211 case DW_FORM_flag_present:
212 return MC_DW_CLASS_FLAG;
213 case DW_FORM_exprloc:
214 return MC_DW_CLASS_EXPRLOC;
218 return MC_DW_CLASS_UNKNOWN;
222 /** \brief Get the name of the tag of a given DIE
225 * \return name of the tag of this DIE
227 static inline const char *MC_dwarf_die_tagname(Dwarf_Die * die)
229 return MC_dwarf_tagname(dwarf_tag(die));
234 /** \brief Get an attribute of a given DIE as a string
237 * \param attribute attribute
238 * \return value of the given attribute of the given DIE
240 static const char *MC_dwarf_attr_integrate_string(Dwarf_Die * die,
243 Dwarf_Attribute attr;
244 if (!dwarf_attr_integrate(die, attribute, &attr)) {
247 return dwarf_formstring(&attr);
251 /** \brief Get the linkage name of a DIE.
253 * Use either DW_AT_linkage_name or DW_AT_MIPS_linkage_name.
254 * DW_AT_linkage_name is standardized since DWARF 4.
255 * Before this version of DWARF, the MIPS extensions
256 * DW_AT_MIPS_linkage_name is used (at least by GCC).
259 * \return linkage name of the given DIE (or NULL)
261 static const char *MC_dwarf_at_linkage_name(Dwarf_Die * die)
263 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_linkage_name);
265 name = MC_dwarf_attr_integrate_string(die, DW_AT_MIPS_linkage_name);
269 static Dwarf_Off MC_dwarf_attr_dieoffset(Dwarf_Die * die, int attribute)
271 Dwarf_Attribute attr;
272 if (dwarf_hasattr_integrate(die, attribute)) {
273 dwarf_attr_integrate(die, attribute, &attr);
274 Dwarf_Die subtype_die;
275 if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
276 xbt_die("Could not find DIE");
278 return dwarf_dieoffset(&subtype_die);
283 static Dwarf_Off MC_dwarf_attr_integrate_dieoffset(Dwarf_Die * die,
286 Dwarf_Attribute attr;
287 if (dwarf_hasattr_integrate(die, attribute)) {
288 dwarf_attr_integrate(die, DW_AT_type, &attr);
289 Dwarf_Die subtype_die;
290 if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
291 xbt_die("Could not find DIE");
293 return dwarf_dieoffset(&subtype_die);
298 /** \brief Find the type/subtype (DW_AT_type) for a DIE
301 * \return DW_AT_type reference as a global offset in hexadecimal (or NULL)
304 std::uint64_t MC_dwarf_at_type(Dwarf_Die * die)
306 return MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
309 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die * die, int attribute)
311 Dwarf_Attribute attr;
312 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
315 if (dwarf_formaddr(&attr, &value) == 0)
316 return (uint64_t) value;
321 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die * die, int attribute,
322 uint64_t default_value)
324 Dwarf_Attribute attr;
325 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
326 return default_value;
328 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr),
329 &value) == 0 ? (uint64_t) value : default_value;
332 static bool MC_dwarf_attr_flag(Dwarf_Die * die, int attribute, bool integrate)
334 Dwarf_Attribute attr;
335 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
336 : dwarf_attr(die, attribute, &attr)) == 0)
340 if (dwarf_formflag(&attr, &result))
341 xbt_die("Unexpected form for attribute %s", MC_dwarf_attrname(attribute));
345 /** \brief Find the default lower bound for a given language
347 * The default lower bound of an array (when DW_TAG_lower_bound
348 * is missing) depends on the language of the compilation unit.
350 * \param lang Language of the compilation unit (values defined in the DWARF spec)
351 * \return Default lower bound of an array in this compilation unit
353 static uint64_t MC_dwarf_default_lower_bound(int lang)
359 case DW_LANG_C_plus_plus:
363 case DW_LANG_ObjC_plus_plus:
369 case DW_LANG_Fortran77:
370 case DW_LANG_Fortran90:
371 case DW_LANG_Fortran95:
372 case DW_LANG_Modula2:
373 case DW_LANG_Pascal83:
375 case DW_LANG_Cobol74:
376 case DW_LANG_Cobol85:
379 xbt_die("No default DW_TAG_lower_bound for language %i and none given",
385 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
388 * \param unit DIE of the compilation unit
389 * \return number of elements in the range
391 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
394 xbt_assert(dwarf_tag(die) == DW_TAG_enumeration_type
395 || dwarf_tag(die) == DW_TAG_subrange_type,
396 "MC_dwarf_subrange_element_count called with DIE of type %s",
397 MC_dwarf_die_tagname(die));
399 // Use DW_TAG_count if present:
400 if (dwarf_hasattr_integrate(die, DW_AT_count)) {
401 return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
403 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
405 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
406 // This is not really 0, but the code expects this (we do not know):
409 uint64_t upper_bound =
410 MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
412 uint64_t lower_bound = 0;
413 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
414 lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
416 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
418 return upper_bound - lower_bound + 1;
421 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
423 * The compilation unit might be needed because the default lower
424 * bound depends on the language of the compilation unit.
426 * \param die the DIE of the DW_TAG_array_type
427 * \param unit the DIE of the compilation unit
428 * \return number of elements in this array type
430 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit)
432 xbt_assert(dwarf_tag(die) == DW_TAG_array_type,
433 "MC_dwarf_array_element_count called with DIE of type %s",
434 MC_dwarf_die_tagname(die));
439 for (res = dwarf_child(die, &child); res == 0;
440 res = dwarf_siblingof(&child, &child)) {
441 int child_tag = dwarf_tag(&child);
442 if (child_tag == DW_TAG_subrange_type
443 || child_tag == DW_TAG_enumeration_type) {
444 result *= MC_dwarf_subrange_element_count(&child, unit);
452 /** Sort the variable by name and address.
454 * We could use boost::container::flat_set instead.
456 static bool MC_compare_variable(
457 simgrid::mc::Variable const& a, simgrid::mc::Variable const& b)
459 int cmp = strcmp(a.name.c_str(), b.name.c_str());
465 return a.address < b.address;
470 /** \brief Initialize the location of a member of a type
471 * (DW_AT_data_member_location of a DW_TAG_member).
473 * \param type a type (struct, class)
474 * \param member the member of the type
475 * \param child DIE of the member (DW_TAG_member)
477 static void MC_dwarf_fill_member_location(mc_type_t type, mc_type_t member,
480 if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
481 xbt_die("Can't groke DW_AT_data_bit_offset.");
484 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
485 if (type->type != DW_TAG_union_type) {
487 ("Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%"
488 PRIx64 ">%s", member->name.c_str(),
489 (uint64_t) type->id, type->name.c_str());
495 Dwarf_Attribute attr;
496 dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
497 int form = dwarf_whatform(&attr);
498 int klass = MC_dwarf_form_get_class(form);
500 case MC_DW_CLASS_EXPRLOC:
501 case MC_DW_CLASS_BLOCK:
502 // Location expression:
506 if (dwarf_getlocation(&attr, &expr, &len)) {
508 ("Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%"
509 PRIx64 ">%s", MC_dwarf_attr_integrate_string(child, DW_AT_name),
510 (uint64_t) type->id, type->name.c_str());
512 simgrid::mc::DwarfExpression(expr, expr+len);
515 case MC_DW_CLASS_CONSTANT:
516 // Offset from the base address of the object:
519 if (!dwarf_formudata(&attr, &offset))
520 member->offset(offset);
522 xbt_die("Cannot get %s location <%" PRIx64 ">%s",
523 MC_dwarf_attr_integrate_string(child, DW_AT_name),
524 (uint64_t) type->id, type->name.c_str());
527 case MC_DW_CLASS_LOCLISTPTR:
528 // Reference to a location list:
530 case MC_DW_CLASS_REFERENCE:
531 // It's supposed to be possible in DWARF2 but I couldn't find its semantic
534 xbt_die("Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
540 static void dw_type_free_voidp(void *t)
542 delete *(mc_type_t*)t;
545 /** \brief Populate the list of members of a type
547 * \param info ELF object containing the type DIE
548 * \param die DIE of the type
549 * \param unit DIE of the compilation unit containing the type DIE
550 * \param type the type
552 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die * die,
553 Dwarf_Die * unit, mc_type_t type)
557 xbt_assert(type->members.empty());
558 for (res = dwarf_child(die, &child); res == 0;
559 res = dwarf_siblingof(&child, &child)) {
560 int tag = dwarf_tag(&child);
561 if (tag == DW_TAG_member || tag == DW_TAG_inheritance) {
563 // Skip declarations:
564 if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
567 // Skip compile time constants:
568 if (dwarf_hasattr(&child, DW_AT_const_value))
571 // TODO, we should use another type (because is is not a type but a member)
572 simgrid::mc::Type member;
576 member.id = dwarf_dieoffset(&child);
578 const char *name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
582 MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
583 member.element_count = -1;
584 member.type_id = MC_dwarf_at_type(&child);
586 if (dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
587 xbt_die("Can't groke DW_AT_data_bit_offset.");
590 MC_dwarf_fill_member_location(type, &member, &child);
592 if (!member.type_id) {
593 xbt_die("Missing type for member %s of <%" PRIx64 ">%s",
595 (uint64_t) type->id, type->name.c_str());
598 type->members.push_back(std::move(member));
603 /** \brief Create a MC type object from a DIE
605 * \param info current object info object
606 * \param DIE (for a given type);
607 * \param unit compilation unit of the current DIE
608 * \return MC representation of the type
610 static simgrid::mc::Type MC_dwarf_die_to_type(
611 mc_object_info_t info, Dwarf_Die * die,
612 Dwarf_Die * unit, mc_frame_t frame,
616 simgrid::mc::Type type;
618 type.name = std::string();
619 type.element_count = -1;
621 type.type = dwarf_tag(die);
624 type.id = dwarf_dieoffset(die);
626 const char *prefix = "";
628 case DW_TAG_structure_type:
631 case DW_TAG_union_type:
634 case DW_TAG_class_type:
641 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
643 char* full_name = ns ? bprintf("%s%s::%s", prefix, ns, name) :
644 bprintf("%s%s", prefix, name);
645 type.name = std::string(full_name);
649 type.type_id = MC_dwarf_at_type(die);
651 // Some compilers do not emit DW_AT_byte_size for pointer_type,
652 // so we fill this. We currently assume that the model-checked process is in
653 // the same architecture..
654 if (type.type == DW_TAG_pointer_type)
655 type.byte_size = sizeof(void*);
657 // Computation of the byte_size;
658 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
659 type.byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
660 else if (type.type == DW_TAG_array_type
661 || type.type == DW_TAG_structure_type
662 || type.type == DW_TAG_class_type) {
664 if (dwarf_aggregate_size(die, &size) == 0) {
665 type.byte_size = size;
670 case DW_TAG_array_type:
671 type.element_count = MC_dwarf_array_element_count(die, unit);
672 // TODO, handle DW_byte_stride and (not) DW_bit_stride
675 case DW_TAG_pointer_type:
676 case DW_TAG_reference_type:
677 case DW_TAG_rvalue_reference_type:
678 type.is_pointer_type = 1;
681 case DW_TAG_structure_type:
682 case DW_TAG_union_type:
683 case DW_TAG_class_type:
684 MC_dwarf_add_members(info, die, unit, &type);
685 char *new_ns = ns == NULL ? xbt_strdup(type.name.c_str())
686 : bprintf("%s::%s", ns, name);
687 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
692 return std::move(type);
695 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
696 Dwarf_Die * unit, mc_frame_t frame,
699 simgrid::mc::Type type = MC_dwarf_die_to_type(info, die, unit, frame, ns);
700 auto& t = (info->types[type.id] = std::move(type));
701 if (!t.name.empty() && type.byte_size != 0)
702 info->full_types_by_name[t.name] = &t;
705 static int mc_anonymous_variable_index = 0;
707 static std::unique_ptr<simgrid::mc::Variable> MC_die_to_variable(
708 mc_object_info_t info, Dwarf_Die * die,
709 Dwarf_Die * unit, mc_frame_t frame,
712 // Skip declarations:
713 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
716 // Skip compile time constants:
717 if (dwarf_hasattr(die, DW_AT_const_value))
720 Dwarf_Attribute attr_location;
721 if (dwarf_attr(die, DW_AT_location, &attr_location) == NULL) {
722 // No location: do not add it ?
726 std::unique_ptr<simgrid::mc::Variable> variable =
727 std::unique_ptr<simgrid::mc::Variable>(new simgrid::mc::Variable());
728 variable->dwarf_offset = dwarf_dieoffset(die);
729 variable->global = frame == NULL; // Can be override base on DW_AT_location
730 variable->object_info = info;
732 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
734 variable->name = name;
735 variable->type_id = MC_dwarf_at_type(die);
737 int form = dwarf_whatform(&attr_location);
740 DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
742 case MC_DW_CLASS_EXPRLOC:
743 case MC_DW_CLASS_BLOCK:
744 // Location expression:
748 if (dwarf_getlocation(&attr_location, &expr, &len)) {
750 "Could not read location expression in DW_AT_location "
751 "of variable <%" PRIx64 ">%s",
752 (uint64_t) variable->dwarf_offset,
753 variable->name.c_str());
756 if (len == 1 && expr[0].atom == DW_OP_addr) {
757 variable->global = 1;
758 uintptr_t offset = (uintptr_t) expr[0].number;
759 uintptr_t base = (uintptr_t) info->base_address();
760 variable->address = (void *) (base + offset);
762 simgrid::mc::LocationListEntry entry;
763 entry.expression = {expr, expr + len};
764 variable->location_list = { std::move(entry) };
769 case MC_DW_CLASS_LOCLISTPTR:
770 case MC_DW_CLASS_CONSTANT:
771 // Reference to location list:
772 mc_dwarf_location_list_init(
773 &variable->location_list, info, die,
777 xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location "
778 "in <%" PRIx64 ">%s",
779 form, form, klass, klass,
780 (uint64_t) variable->dwarf_offset,
781 variable->name.c_str());
784 // Handle start_scope:
785 if (dwarf_hasattr(die, DW_AT_start_scope)) {
786 Dwarf_Attribute attr;
787 dwarf_attr(die, DW_AT_start_scope, &attr);
788 int form = dwarf_whatform(&attr);
789 int klass = MC_dwarf_form_get_class(form);
791 case MC_DW_CLASS_CONSTANT:
794 variable->start_scope =
795 dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
798 case MC_DW_CLASS_RANGELISTPTR: // TODO
801 ("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
802 form, klass, name == NULL ? "?" : name);
806 if (ns && variable->global)
808 std::string(ns) + "::" + variable->name;
810 // The current code needs a variable name,
811 // generate a fake one:
812 if (variable->name.empty())
814 "@anonymous#" + std::to_string(mc_anonymous_variable_index++);
816 return std::move(variable);
819 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
820 Dwarf_Die * unit, mc_frame_t frame,
823 std::unique_ptr<simgrid::mc::Variable> variable =
824 MC_die_to_variable(info, die, unit, frame, ns);
827 // Those arrays are sorted later:
828 else if (variable->global)
829 info->global_variables.push_back(std::move(*variable));
830 else if (frame != nullptr)
831 frame->variables.push_back(std::move(*variable));
833 xbt_die("No frame for this local variable");
836 static void MC_dwarf_handle_scope_die(mc_object_info_t info, Dwarf_Die * die,
837 Dwarf_Die * unit, mc_frame_t parent_frame,
840 // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
841 int tag = dwarf_tag(die);
842 mc_tag_class klass = MC_dwarf_tag_classify(tag);
844 // (Template) Subprogram declaration:
845 if (klass == mc_tag_subprogram
846 && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
849 if (klass == mc_tag_scope)
850 xbt_assert(parent_frame, "No parent scope for this scope");
852 simgrid::mc::Frame frame;
855 frame.id = dwarf_dieoffset(die);
856 frame.object_info = info;
858 if (klass == mc_tag_subprogram) {
859 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
861 frame.name = std::string(ns) + "::" + name;
866 frame.abstract_origin_id =
867 MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
869 // This is the base address for DWARF addresses.
870 // Relocated addresses are offset from this base address.
871 // See DWARF4 spec 7.5
872 void *base = info->base_address();
874 // TODO, support DW_AT_ranges
875 uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
876 frame.low_pc = low_pc ? ((char *) base) + low_pc : 0;
879 Dwarf_Attribute attr;
880 if (!dwarf_attr_integrate(die, DW_AT_high_pc, &attr)) {
881 xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
887 switch (MC_dwarf_form_get_class(dwarf_whatform(&attr))) {
889 // DW_AT_high_pc if an offset from the low_pc:
890 case MC_DW_CLASS_CONSTANT:
892 if (dwarf_formsdata(&attr, &offset) != 0)
893 xbt_die("Could not read constant");
894 frame.high_pc = (void *) ((char *) frame.low_pc + offset);
897 // DW_AT_high_pc is a relocatable address:
898 case MC_DW_CLASS_ADDRESS:
899 if (dwarf_formaddr(&attr, &high_pc) != 0)
900 xbt_die("Could not read address");
901 frame.high_pc = ((char *) base) + high_pc;
905 xbt_die("Unexpected class for DW_AT_high_pc");
910 if (klass == mc_tag_subprogram) {
911 Dwarf_Attribute attr_frame_base;
912 if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
913 mc_dwarf_location_list_init(&frame.frame_base, info, die,
918 MC_dwarf_handle_children(info, die, unit, &frame, ns);
920 // Someone needs this to be sorted but who?
921 std::sort(frame.variables.begin(), frame.variables.end(),
922 MC_compare_variable);
925 if (klass == mc_tag_subprogram)
926 info->subprograms[frame.id] = frame;
927 else if (klass == mc_tag_scope)
928 parent_frame->scopes.push_back(std::move(frame));
931 static void mc_dwarf_handle_namespace_die(mc_object_info_t info,
932 Dwarf_Die * die, Dwarf_Die * unit,
936 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
938 xbt_die("Unexpected namespace in a subprogram");
939 char *new_ns = ns == NULL ? xbt_strdup(name)
940 : bprintf("%s::%s", ns, name);
941 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
945 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
946 Dwarf_Die * unit, mc_frame_t frame,
949 // For each child DIE:
952 for (res = dwarf_child(die, &child); res == 0;
953 res = dwarf_siblingof(&child, &child)) {
954 MC_dwarf_handle_die(info, &child, unit, frame, ns);
958 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
959 Dwarf_Die * unit, mc_frame_t frame,
962 int tag = dwarf_tag(die);
963 mc_tag_class klass = MC_dwarf_tag_classify(tag);
968 MC_dwarf_handle_type_die(info, die, unit, frame, ns);
971 // Subprogram or scope:
972 case mc_tag_subprogram:
974 MC_dwarf_handle_scope_die(info, die, unit, frame, ns);
978 case mc_tag_variable:
979 MC_dwarf_handle_variable_die(info, die, unit, frame, ns);
982 case mc_tag_namespace:
983 mc_dwarf_handle_namespace_die(info, die, unit, frame, ns);
992 /** \brief Populate the debugging informations of the given ELF object
994 * Read the DWARf information of the EFFL object and populate the
995 * lists of types, variables, functions.
997 void MC_dwarf_get_variables(mc_object_info_t info)
999 int fd = open(info->file_name.c_str(), O_RDONLY);
1001 xbt_die("Could not open file %s", info->file_name.c_str());
1002 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
1004 xbt_die("Your program must be compiled with -g (%s)",
1005 info->file_name.c_str());
1006 // For each compilation unit:
1007 Dwarf_Off offset = 0;
1008 Dwarf_Off next_offset = 0;
1010 while (dwarf_nextcu(dwarf, offset, &next_offset, &length, NULL, NULL, NULL) ==
1013 if (dwarf_offdie(dwarf, offset + length, &unit_die) != NULL) {
1015 // For each child DIE:
1018 for (res = dwarf_child(&unit_die, &child); res == 0;
1019 res = dwarf_siblingof(&child, &child)) {
1020 MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
1024 offset = next_offset;
1031 // ***** Functions index
1033 static int MC_compare_frame_index_items(simgrid::mc::FunctionIndexEntry* a,
1034 simgrid::mc::FunctionIndexEntry* b)
1036 if (a->low_pc < b->low_pc)
1038 else if (a->low_pc == b->low_pc)
1044 static void MC_make_functions_index(mc_object_info_t info)
1046 info->functions_index.clear();
1048 for (auto& e : info->subprograms) {
1049 if (e.second.low_pc == nullptr)
1051 simgrid::mc::FunctionIndexEntry entry;
1052 entry.low_pc = e.second.low_pc;
1053 entry.function = &e.second;
1054 info->functions_index.push_back(entry);
1057 info->functions_index.shrink_to_fit();
1059 // Sort the array by low_pc:
1060 std::sort(info->functions_index.begin(), info->functions_index.end(),
1061 [](simgrid::mc::FunctionIndexEntry& a,
1062 simgrid::mc::FunctionIndexEntry& b)
1064 return a.low_pc < b.low_pc;
1068 static void MC_post_process_variables(mc_object_info_t info)
1070 // Someone needs this to be sorted but who?
1071 std::sort(info->global_variables.begin(), info->global_variables.end(),
1072 MC_compare_variable);
1074 for(simgrid::mc::Variable& variable : info->global_variables)
1075 if (variable.type_id) {
1076 auto i = info->types.find(variable.type_id);
1077 if (i != info->types.end())
1078 variable.type = &(i->second);
1080 variable.type = nullptr;
1084 static void mc_post_process_scope(mc_object_info_t info, mc_frame_t scope)
1087 if (scope->tag == DW_TAG_inlined_subroutine) {
1088 // Attach correct namespaced name in inlined subroutine:
1089 auto i = info->subprograms.find(scope->abstract_origin_id);
1090 xbt_assert(i != info->subprograms.end(),
1091 "Could not lookup abstract origin %" PRIx64,
1092 (uint64_t) scope->abstract_origin_id);
1093 scope->name = i->second.name;
1097 for (simgrid::mc::Variable& variable : scope->variables)
1098 if (variable.type_id) {
1099 auto i = info->types.find(variable.type_id);
1100 if (i != info->types.end())
1101 variable.type = &(i->second);
1103 variable.type = nullptr;
1106 // Recursive post-processing of nested-scopes:
1107 for (simgrid::mc::Frame& nested_scope : scope->scopes)
1108 mc_post_process_scope(info, &nested_scope);
1112 static void MC_post_process_functions(mc_object_info_t info)
1114 for (auto& entry : info->subprograms)
1115 mc_post_process_scope(info, &entry.second);
1119 /** \brief Fill/lookup the "subtype" field.
1121 static void MC_resolve_subtype(mc_object_info_t info, mc_type_t type)
1125 auto i = info->types.find(type->type_id);
1126 if (i != info->types.end())
1127 type->subtype = &(i->second);
1129 type->subtype = nullptr;
1132 if (type->subtype->byte_size != 0)
1134 if (type->subtype->name.empty())
1136 // Try to find a more complete description of the type:
1137 // We need to fix in order to support C++.
1139 auto j = info->full_types_by_name.find(type->subtype->name);
1140 if (j != info->full_types_by_name.end())
1141 type->subtype = j->second;
1144 static void MC_post_process_types(mc_object_info_t info)
1146 // Lookup "subtype" field:
1147 for(auto& i : info->types) {
1148 MC_resolve_subtype(info, &(i.second));
1149 for (simgrid::mc::Type& member : i.second.members)
1150 MC_resolve_subtype(info, &member);
1154 /** \brief Finds informations about a given shared object/executable */
1155 std::shared_ptr<s_mc_object_info_t> MC_find_object_info(
1156 std::vector<simgrid::mc::VmMap> const& maps, const char *name, int executable)
1158 std::shared_ptr<s_mc_object_info_t> result =
1159 std::make_shared<s_mc_object_info_t>();
1161 result->flags |= MC_OBJECT_INFO_EXECUTABLE;
1162 result->file_name = name;
1163 MC_find_object_address(maps, result.get());
1164 MC_dwarf_get_variables(result.get());
1165 MC_post_process_variables(result.get());
1166 MC_post_process_types(result.get());
1167 MC_post_process_functions(result.get());
1168 MC_make_functions_index(result.get());
1169 return std::move(result);
1172 /*************************************************************************/
1174 void MC_post_process_object_info(mc_process_t process, mc_object_info_t info)
1176 for (auto& i : info->types) {
1178 mc_type_t type = &(i.second);
1179 mc_type_t subtype = type;
1180 while (subtype->type == DW_TAG_typedef || subtype->type == DW_TAG_volatile_type
1181 || subtype->type == DW_TAG_const_type) {
1182 if (subtype->subtype)
1183 subtype = subtype->subtype;
1188 // Resolve full_type:
1189 if (!subtype->name.empty() && subtype->byte_size == 0) {
1190 for (auto const& object_info : process->object_infos) {
1191 auto i = object_info->full_types_by_name.find(subtype->name);
1192 if (i != object_info->full_types_by_name.end()
1193 && !i->second->name.empty() && i->second->byte_size) {
1194 type->full_type = i->second;
1198 } else type->full_type = subtype;