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 static void MC_dwarf_register_global_variable(
25 mc_object_info_t info, std::unique_ptr<simgrid::mc::Variable> variable);
26 static void MC_register_variable(
27 mc_object_info_t info, mc_frame_t frame, std::unique_ptr<simgrid::mc::Variable> variable);
28 static void MC_dwarf_register_non_global_variable(mc_object_info_t info, mc_frame_t frame, mc_variable_t variable);
29 static void MC_dwarf_register_variable(
30 mc_object_info_t info, mc_frame_t frame,
31 std::unique_ptr<simgrid::mc::Variable> variable);
33 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing");
35 /** \brief The default DW_TAG_lower_bound for a given DW_AT_language.
37 * The default for a given language is defined in the DWARF spec.
39 * \param language consant as defined by the DWARf spec
41 static uint64_t MC_dwarf_default_lower_bound(int lang);
43 /** \brief Computes the the element_count of a DW_TAG_enumeration_type DIE
45 * This is the number of elements in a given array dimension.
47 * A reference of the compilation unit (DW_TAG_compile_unit) is
48 * needed because the default lower bound (when there is no DW_AT_lower_bound)
49 * depends of the language of the compilation unit (DW_AT_language).
51 * \param die DIE for the DW_TAG_enumeration_type or DW_TAG_subrange_type
52 * \param unit DIE of the DW_TAG_compile_unit
54 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
57 /** \brief Computes the number of elements of a given DW_TAG_array_type.
59 * \param die DIE for the DW_TAG_array_type
61 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit);
63 /** \brief Process a DIE
65 * \param info the resulting object fot the library/binary file (output)
66 * \param die the current DIE
67 * \param unit the DIE of the compile unit of the current DIE
68 * \param frame containg frame if any
70 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
71 Dwarf_Die * unit, mc_frame_t frame,
74 /** \brief Process a type DIE
76 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
77 Dwarf_Die * unit, mc_frame_t frame,
80 /** \brief Calls MC_dwarf_handle_die on all childrend of the given die
82 * \param info the resulting object fot the library/binary file (output)
83 * \param die the current DIE
84 * \param unit the DIE of the compile unit of the current DIE
85 * \param frame containg frame if any
87 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
88 Dwarf_Die * unit, mc_frame_t frame,
91 /** \brief Handle a variable (DW_TAG_variable or other)
93 * \param info the resulting object fot the library/binary file (output)
94 * \param die the current DIE
95 * \param unit the DIE of the compile unit of the current DIE
96 * \param frame containg frame if any
98 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
99 Dwarf_Die * unit, mc_frame_t frame,
102 /** \brief Get the DW_TAG_type of the DIE
105 * \return DW_TAG_type attribute as a new string (NULL if none)
107 static std::uint64_t MC_dwarf_at_type(Dwarf_Die * die);
109 /** \brief A class of DWARF tags (DW_TAG_*)
111 typedef enum mc_tag_class {
120 static mc_tag_class MC_dwarf_tag_classify(int tag)
124 case DW_TAG_array_type:
125 case DW_TAG_class_type:
126 case DW_TAG_enumeration_type:
128 case DW_TAG_pointer_type:
129 case DW_TAG_reference_type:
130 case DW_TAG_rvalue_reference_type:
131 case DW_TAG_string_type:
132 case DW_TAG_structure_type:
133 case DW_TAG_subroutine_type:
134 case DW_TAG_union_type:
135 case DW_TAG_ptr_to_member_type:
136 case DW_TAG_set_type:
137 case DW_TAG_subrange_type:
138 case DW_TAG_base_type:
139 case DW_TAG_const_type:
140 case DW_TAG_file_type:
141 case DW_TAG_packed_type:
142 case DW_TAG_volatile_type:
143 case DW_TAG_restrict_type:
144 case DW_TAG_interface_type:
145 case DW_TAG_unspecified_type:
146 case DW_TAG_shared_type:
149 case DW_TAG_subprogram:
150 return mc_tag_subprogram;
152 case DW_TAG_variable:
153 case DW_TAG_formal_parameter:
154 return mc_tag_variable;
156 case DW_TAG_lexical_block:
157 case DW_TAG_try_block:
158 case DW_TAG_catch_block:
159 case DW_TAG_inlined_subroutine:
160 case DW_TAG_with_stmt:
163 case DW_TAG_namespace:
164 return mc_tag_namespace;
167 return mc_tag_unknown;
172 #define MC_DW_CLASS_UNKNOWN 0
173 #define MC_DW_CLASS_ADDRESS 1 // Location in the address space of the program
174 #define MC_DW_CLASS_BLOCK 2 // Arbitrary block of bytes
175 #define MC_DW_CLASS_CONSTANT 3
176 #define MC_DW_CLASS_STRING 3 // String
177 #define MC_DW_CLASS_FLAG 4 // Boolean
178 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
179 #define MC_DW_CLASS_EXPRLOC 6 // DWARF expression/location description
180 #define MC_DW_CLASS_LINEPTR 7
181 #define MC_DW_CLASS_LOCLISTPTR 8
182 #define MC_DW_CLASS_MACPTR 9
183 #define MC_DW_CLASS_RANGELISTPTR 10
185 /** \brief Find the DWARF data class for a given DWARF data form
187 * This mapping is defined in the DWARF spec.
189 * \param form The form (values taken from the DWARF spec)
190 * \return An internal representation for the corresponding class
192 static int MC_dwarf_form_get_class(int form)
196 return MC_DW_CLASS_ADDRESS;
201 return MC_DW_CLASS_BLOCK;
208 return MC_DW_CLASS_CONSTANT;
211 return MC_DW_CLASS_STRING;
212 case DW_FORM_ref_addr:
217 case DW_FORM_ref_udata:
218 return MC_DW_CLASS_REFERENCE;
220 case DW_FORM_flag_present:
221 return MC_DW_CLASS_FLAG;
222 case DW_FORM_exprloc:
223 return MC_DW_CLASS_EXPRLOC;
227 return MC_DW_CLASS_UNKNOWN;
231 /** \brief Get the name of the tag of a given DIE
234 * \return name of the tag of this DIE
236 static inline const char *MC_dwarf_die_tagname(Dwarf_Die * die)
238 return MC_dwarf_tagname(dwarf_tag(die));
243 /** \brief Get an attribute of a given DIE as a string
246 * \param attribute attribute
247 * \return value of the given attribute of the given DIE
249 static const char *MC_dwarf_attr_integrate_string(Dwarf_Die * die,
252 Dwarf_Attribute attr;
253 if (!dwarf_attr_integrate(die, attribute, &attr)) {
256 return dwarf_formstring(&attr);
260 /** \brief Get the linkage name of a DIE.
262 * Use either DW_AT_linkage_name or DW_AT_MIPS_linkage_name.
263 * DW_AT_linkage_name is standardized since DWARF 4.
264 * Before this version of DWARF, the MIPS extensions
265 * DW_AT_MIPS_linkage_name is used (at least by GCC).
268 * \return linkage name of the given DIE (or NULL)
270 static const char *MC_dwarf_at_linkage_name(Dwarf_Die * die)
272 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_linkage_name);
274 name = MC_dwarf_attr_integrate_string(die, DW_AT_MIPS_linkage_name);
278 static Dwarf_Off MC_dwarf_attr_dieoffset(Dwarf_Die * die, int attribute)
280 Dwarf_Attribute attr;
281 if (dwarf_hasattr_integrate(die, attribute)) {
282 dwarf_attr_integrate(die, attribute, &attr);
283 Dwarf_Die subtype_die;
284 if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
285 xbt_die("Could not find DIE");
287 return dwarf_dieoffset(&subtype_die);
292 static Dwarf_Off MC_dwarf_attr_integrate_dieoffset(Dwarf_Die * die,
295 Dwarf_Attribute attr;
296 if (dwarf_hasattr_integrate(die, attribute)) {
297 dwarf_attr_integrate(die, DW_AT_type, &attr);
298 Dwarf_Die subtype_die;
299 if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
300 xbt_die("Could not find DIE");
302 return dwarf_dieoffset(&subtype_die);
307 /** \brief Find the type/subtype (DW_AT_type) for a DIE
310 * \return DW_AT_type reference as a global offset in hexadecimal (or NULL)
313 std::uint64_t MC_dwarf_at_type(Dwarf_Die * die)
315 return MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
318 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die * die, int attribute)
320 Dwarf_Attribute attr;
321 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
324 if (dwarf_formaddr(&attr, &value) == 0)
325 return (uint64_t) value;
330 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die * die, int attribute,
331 uint64_t default_value)
333 Dwarf_Attribute attr;
334 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
335 return default_value;
337 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr),
338 &value) == 0 ? (uint64_t) value : default_value;
341 static bool MC_dwarf_attr_flag(Dwarf_Die * die, int attribute, bool integrate)
343 Dwarf_Attribute attr;
344 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
345 : dwarf_attr(die, attribute, &attr)) == 0)
349 if (dwarf_formflag(&attr, &result))
350 xbt_die("Unexpected form for attribute %s", MC_dwarf_attrname(attribute));
354 /** \brief Find the default lower bound for a given language
356 * The default lower bound of an array (when DW_TAG_lower_bound
357 * is missing) depends on the language of the compilation unit.
359 * \param lang Language of the compilation unit (values defined in the DWARF spec)
360 * \return Default lower bound of an array in this compilation unit
362 static uint64_t MC_dwarf_default_lower_bound(int lang)
368 case DW_LANG_C_plus_plus:
372 case DW_LANG_ObjC_plus_plus:
378 case DW_LANG_Fortran77:
379 case DW_LANG_Fortran90:
380 case DW_LANG_Fortran95:
381 case DW_LANG_Modula2:
382 case DW_LANG_Pascal83:
384 case DW_LANG_Cobol74:
385 case DW_LANG_Cobol85:
388 xbt_die("No default DW_TAG_lower_bound for language %i and none given",
394 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
397 * \param unit DIE of the compilation unit
398 * \return number of elements in the range
400 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
403 xbt_assert(dwarf_tag(die) == DW_TAG_enumeration_type
404 || dwarf_tag(die) == DW_TAG_subrange_type,
405 "MC_dwarf_subrange_element_count called with DIE of type %s",
406 MC_dwarf_die_tagname(die));
408 // Use DW_TAG_count if present:
409 if (dwarf_hasattr_integrate(die, DW_AT_count)) {
410 return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
412 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
414 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
415 // This is not really 0, but the code expects this (we do not know):
418 uint64_t upper_bound =
419 MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
421 uint64_t lower_bound = 0;
422 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
423 lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
425 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
427 return upper_bound - lower_bound + 1;
430 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
432 * The compilation unit might be needed because the default lower
433 * bound depends on the language of the compilation unit.
435 * \param die the DIE of the DW_TAG_array_type
436 * \param unit the DIE of the compilation unit
437 * \return number of elements in this array type
439 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit)
441 xbt_assert(dwarf_tag(die) == DW_TAG_array_type,
442 "MC_dwarf_array_element_count called with DIE of type %s",
443 MC_dwarf_die_tagname(die));
448 for (res = dwarf_child(die, &child); res == 0;
449 res = dwarf_siblingof(&child, &child)) {
450 int child_tag = dwarf_tag(&child);
451 if (child_tag == DW_TAG_subrange_type
452 || child_tag == DW_TAG_enumeration_type) {
453 result *= MC_dwarf_subrange_element_count(&child, unit);
461 /** \brief Initialize the location of a member of a type
462 * (DW_AT_data_member_location of a DW_TAG_member).
464 * \param type a type (struct, class)
465 * \param member the member of the type
466 * \param child DIE of the member (DW_TAG_member)
468 static void MC_dwarf_fill_member_location(mc_type_t type, mc_type_t member,
471 if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
472 xbt_die("Can't groke DW_AT_data_bit_offset.");
475 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
476 if (type->type != DW_TAG_union_type) {
478 ("Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%"
479 PRIx64 ">%s", member->name.c_str(),
480 (uint64_t) type->id, type->name.c_str());
486 Dwarf_Attribute attr;
487 dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
488 int form = dwarf_whatform(&attr);
489 int klass = MC_dwarf_form_get_class(form);
491 case MC_DW_CLASS_EXPRLOC:
492 case MC_DW_CLASS_BLOCK:
493 // Location expression:
497 if (dwarf_getlocation(&attr, &expr, &len)) {
499 ("Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%"
500 PRIx64 ">%s", MC_dwarf_attr_integrate_string(child, DW_AT_name),
501 (uint64_t) type->id, type->name.c_str());
503 simgrid::mc::DwarfExpression(expr, expr+len);
506 case MC_DW_CLASS_CONSTANT:
507 // Offset from the base address of the object:
510 if (!dwarf_formudata(&attr, &offset))
511 member->offset(offset);
513 xbt_die("Cannot get %s location <%" PRIx64 ">%s",
514 MC_dwarf_attr_integrate_string(child, DW_AT_name),
515 (uint64_t) type->id, type->name.c_str());
518 case MC_DW_CLASS_LOCLISTPTR:
519 // Reference to a location list:
521 case MC_DW_CLASS_REFERENCE:
522 // It's supposed to be possible in DWARF2 but I couldn't find its semantic
525 xbt_die("Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
531 static void dw_type_free_voidp(void *t)
533 delete *(mc_type_t*)t;
536 /** \brief Populate the list of members of a type
538 * \param info ELF object containing the type DIE
539 * \param die DIE of the type
540 * \param unit DIE of the compilation unit containing the type DIE
541 * \param type the type
543 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die * die,
544 Dwarf_Die * unit, mc_type_t type)
548 xbt_assert(type->members.empty());
549 for (res = dwarf_child(die, &child); res == 0;
550 res = dwarf_siblingof(&child, &child)) {
551 int tag = dwarf_tag(&child);
552 if (tag == DW_TAG_member || tag == DW_TAG_inheritance) {
554 // Skip declarations:
555 if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
558 // Skip compile time constants:
559 if (dwarf_hasattr(&child, DW_AT_const_value))
562 // TODO, we should use another type (because is is not a type but a member)
563 simgrid::mc::Type member;
567 member.id = dwarf_dieoffset(&child);
569 const char *name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
573 MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
574 member.element_count = -1;
575 member.type_id = MC_dwarf_at_type(&child);
577 if (dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
578 xbt_die("Can't groke DW_AT_data_bit_offset.");
581 MC_dwarf_fill_member_location(type, &member, &child);
583 if (!member.type_id) {
584 xbt_die("Missing type for member %s of <%" PRIx64 ">%s",
586 (uint64_t) type->id, type->name.c_str());
589 type->members.push_back(std::move(member));
594 /** \brief Create a MC type object from a DIE
596 * \param info current object info object
597 * \param DIE (for a given type);
598 * \param unit compilation unit of the current DIE
599 * \return MC representation of the type
601 static simgrid::mc::Type MC_dwarf_die_to_type(
602 mc_object_info_t info, Dwarf_Die * die,
603 Dwarf_Die * unit, mc_frame_t frame,
607 simgrid::mc::Type type;
609 type.name = std::string();
610 type.element_count = -1;
612 type.type = dwarf_tag(die);
615 type.id = dwarf_dieoffset(die);
617 const char *prefix = "";
619 case DW_TAG_structure_type:
622 case DW_TAG_union_type:
625 case DW_TAG_class_type:
632 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
634 char* full_name = ns ? bprintf("%s%s::%s", prefix, ns, name) :
635 bprintf("%s%s", prefix, name);
636 type.name = std::string(full_name);
640 type.type_id = MC_dwarf_at_type(die);
642 // Some compilers do not emit DW_AT_byte_size for pointer_type,
643 // so we fill this. We currently assume that the model-checked process is in
644 // the same architecture..
645 if (type.type == DW_TAG_pointer_type)
646 type.byte_size = sizeof(void*);
648 // Computation of the byte_size;
649 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
650 type.byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
651 else if (type.type == DW_TAG_array_type
652 || type.type == DW_TAG_structure_type
653 || type.type == DW_TAG_class_type) {
655 if (dwarf_aggregate_size(die, &size) == 0) {
656 type.byte_size = size;
661 case DW_TAG_array_type:
662 type.element_count = MC_dwarf_array_element_count(die, unit);
663 // TODO, handle DW_byte_stride and (not) DW_bit_stride
666 case DW_TAG_pointer_type:
667 case DW_TAG_reference_type:
668 case DW_TAG_rvalue_reference_type:
669 type.is_pointer_type = 1;
672 case DW_TAG_structure_type:
673 case DW_TAG_union_type:
674 case DW_TAG_class_type:
675 MC_dwarf_add_members(info, die, unit, &type);
676 char *new_ns = ns == NULL ? xbt_strdup(type.name.c_str())
677 : bprintf("%s::%s", ns, name);
678 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
683 return std::move(type);
686 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
687 Dwarf_Die * unit, mc_frame_t frame,
690 simgrid::mc::Type type = MC_dwarf_die_to_type(info, die, unit, frame, ns);
691 auto& t = (info->types[type.id] = std::move(type));
692 if (!t.name.empty() && type.byte_size != 0)
693 info->full_types_by_name[t.name] = &t;
696 static int mc_anonymous_variable_index = 0;
698 static std::unique_ptr<simgrid::mc::Variable> MC_die_to_variable(
699 mc_object_info_t info, Dwarf_Die * die,
700 Dwarf_Die * unit, mc_frame_t frame,
703 // Skip declarations:
704 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
707 // Skip compile time constants:
708 if (dwarf_hasattr(die, DW_AT_const_value))
711 Dwarf_Attribute attr_location;
712 if (dwarf_attr(die, DW_AT_location, &attr_location) == NULL) {
713 // No location: do not add it ?
717 std::unique_ptr<simgrid::mc::Variable> variable =
718 std::unique_ptr<simgrid::mc::Variable>(new simgrid::mc::Variable());
719 variable->dwarf_offset = dwarf_dieoffset(die);
720 variable->global = frame == NULL; // Can be override base on DW_AT_location
721 variable->object_info = info;
723 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
725 variable->name = name;
726 variable->type_id = MC_dwarf_at_type(die);
728 int form = dwarf_whatform(&attr_location);
731 DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
733 case MC_DW_CLASS_EXPRLOC:
734 case MC_DW_CLASS_BLOCK:
735 // Location expression:
739 if (dwarf_getlocation(&attr_location, &expr, &len)) {
741 "Could not read location expression in DW_AT_location "
742 "of variable <%" PRIx64 ">%s",
743 (uint64_t) variable->dwarf_offset,
744 variable->name.c_str());
747 if (len == 1 && expr[0].atom == DW_OP_addr) {
748 variable->global = 1;
749 uintptr_t offset = (uintptr_t) expr[0].number;
750 uintptr_t base = (uintptr_t) info->base_address();
751 variable->address = (void *) (base + offset);
753 simgrid::mc::LocationListEntry entry;
754 entry.expression = {expr, expr + len};
755 variable->location_list = { std::move(entry) };
760 case MC_DW_CLASS_LOCLISTPTR:
761 case MC_DW_CLASS_CONSTANT:
762 // Reference to location list:
763 mc_dwarf_location_list_init(
764 &variable->location_list, info, die,
768 xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location "
769 "in <%" PRIx64 ">%s",
770 form, form, klass, klass,
771 (uint64_t) variable->dwarf_offset,
772 variable->name.c_str());
775 // Handle start_scope:
776 if (dwarf_hasattr(die, DW_AT_start_scope)) {
777 Dwarf_Attribute attr;
778 dwarf_attr(die, DW_AT_start_scope, &attr);
779 int form = dwarf_whatform(&attr);
780 int klass = MC_dwarf_form_get_class(form);
782 case MC_DW_CLASS_CONSTANT:
785 variable->start_scope =
786 dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
789 case MC_DW_CLASS_RANGELISTPTR: // TODO
792 ("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
793 form, klass, name == NULL ? "?" : name);
797 if (ns && variable->global)
799 std::string(ns) + "::" + variable->name;
801 // The current code needs a variable name,
802 // generate a fake one:
803 if (variable->name.empty())
805 "@anonymous#" + std::to_string(mc_anonymous_variable_index++);
807 return std::move(variable);
810 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
811 Dwarf_Die * unit, mc_frame_t frame,
814 MC_dwarf_register_variable(info, frame,
815 MC_die_to_variable(info, die, unit, frame, ns));
818 static void MC_dwarf_handle_scope_die(mc_object_info_t info, Dwarf_Die * die,
819 Dwarf_Die * unit, mc_frame_t parent_frame,
822 // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
823 int tag = dwarf_tag(die);
824 mc_tag_class klass = MC_dwarf_tag_classify(tag);
826 // (Template) Subprogram declaration:
827 if (klass == mc_tag_subprogram
828 && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
831 if (klass == mc_tag_scope)
832 xbt_assert(parent_frame, "No parent scope for this scope");
834 simgrid::mc::Frame frame;
837 frame.id = dwarf_dieoffset(die);
838 frame.object_info = info;
840 if (klass == mc_tag_subprogram) {
841 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
843 frame.name = std::string(ns) + "::" + name;
848 frame.abstract_origin_id =
849 MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
851 // This is the base address for DWARF addresses.
852 // Relocated addresses are offset from this base address.
853 // See DWARF4 spec 7.5
854 void *base = info->base_address();
856 // TODO, support DW_AT_ranges
857 uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
858 frame.low_pc = low_pc ? ((char *) base) + low_pc : 0;
861 Dwarf_Attribute attr;
862 if (!dwarf_attr_integrate(die, DW_AT_high_pc, &attr)) {
863 xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
869 switch (MC_dwarf_form_get_class(dwarf_whatform(&attr))) {
871 // DW_AT_high_pc if an offset from the low_pc:
872 case MC_DW_CLASS_CONSTANT:
874 if (dwarf_formsdata(&attr, &offset) != 0)
875 xbt_die("Could not read constant");
876 frame.high_pc = (void *) ((char *) frame.low_pc + offset);
879 // DW_AT_high_pc is a relocatable address:
880 case MC_DW_CLASS_ADDRESS:
881 if (dwarf_formaddr(&attr, &high_pc) != 0)
882 xbt_die("Could not read address");
883 frame.high_pc = ((char *) base) + high_pc;
887 xbt_die("Unexpected class for DW_AT_high_pc");
892 if (klass == mc_tag_subprogram) {
893 Dwarf_Attribute attr_frame_base;
894 if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
895 mc_dwarf_location_list_init(&frame.frame_base, info, die,
900 MC_dwarf_handle_children(info, die, unit, &frame, ns);
903 if (klass == mc_tag_subprogram)
904 info->subprograms[frame.id] = frame;
905 else if (klass == mc_tag_scope)
906 parent_frame->scopes.push_back(std::move(frame));
909 static void mc_dwarf_handle_namespace_die(mc_object_info_t info,
910 Dwarf_Die * die, Dwarf_Die * unit,
914 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
916 xbt_die("Unexpected namespace in a subprogram");
917 char *new_ns = ns == NULL ? xbt_strdup(name)
918 : bprintf("%s::%s", ns, name);
919 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
923 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
924 Dwarf_Die * unit, mc_frame_t frame,
927 // For each child DIE:
930 for (res = dwarf_child(die, &child); res == 0;
931 res = dwarf_siblingof(&child, &child)) {
932 MC_dwarf_handle_die(info, &child, unit, frame, ns);
936 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
937 Dwarf_Die * unit, mc_frame_t frame,
940 int tag = dwarf_tag(die);
941 mc_tag_class klass = MC_dwarf_tag_classify(tag);
946 MC_dwarf_handle_type_die(info, die, unit, frame, ns);
949 // Subprogram or scope:
950 case mc_tag_subprogram:
952 MC_dwarf_handle_scope_die(info, die, unit, frame, ns);
956 case mc_tag_variable:
957 MC_dwarf_handle_variable_die(info, die, unit, frame, ns);
960 case mc_tag_namespace:
961 mc_dwarf_handle_namespace_die(info, die, unit, frame, ns);
970 /** \brief Populate the debugging informations of the given ELF object
972 * Read the DWARf information of the EFFL object and populate the
973 * lists of types, variables, functions.
975 void MC_dwarf_get_variables(mc_object_info_t info)
977 int fd = open(info->file_name.c_str(), O_RDONLY);
979 xbt_die("Could not open file %s", info->file_name.c_str());
980 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
982 xbt_die("Your program must be compiled with -g (%s)",
983 info->file_name.c_str());
984 // For each compilation unit:
985 Dwarf_Off offset = 0;
986 Dwarf_Off next_offset = 0;
988 while (dwarf_nextcu(dwarf, offset, &next_offset, &length, NULL, NULL, NULL) ==
991 if (dwarf_offdie(dwarf, offset + length, &unit_die) != NULL) {
993 // For each child DIE:
996 for (res = dwarf_child(&unit_die, &child); res == 0;
997 res = dwarf_siblingof(&child, &child)) {
998 MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
1002 offset = next_offset;
1009 // ***** Functions index
1011 static int MC_compare_frame_index_items(simgrid::mc::FunctionIndexEntry* a,
1012 simgrid::mc::FunctionIndexEntry* b)
1014 if (a->low_pc < b->low_pc)
1016 else if (a->low_pc == b->low_pc)
1022 static void MC_make_functions_index(mc_object_info_t info)
1024 info->functions_index.clear();
1026 for (auto& e : info->subprograms) {
1027 if (e.second.low_pc == nullptr)
1029 simgrid::mc::FunctionIndexEntry entry;
1030 entry.low_pc = e.second.low_pc;
1031 entry.function = &e.second;
1032 info->functions_index.push_back(entry);
1035 info->functions_index.shrink_to_fit();
1037 // Sort the array by low_pc:
1038 std::sort(info->functions_index.begin(), info->functions_index.end(),
1039 [](simgrid::mc::FunctionIndexEntry& a,
1040 simgrid::mc::FunctionIndexEntry& b)
1042 return a.low_pc < b.low_pc;
1046 static void MC_post_process_variables(mc_object_info_t info)
1048 for(simgrid::mc::Variable& variable : info->global_variables)
1049 if (variable.type_id) {
1050 auto i = info->types.find(variable.type_id);
1051 if (i != info->types.end())
1052 variable.type = &(i->second);
1054 variable.type = nullptr;
1058 static void mc_post_process_scope(mc_object_info_t info, mc_frame_t scope)
1061 if (scope->tag == DW_TAG_inlined_subroutine) {
1062 // Attach correct namespaced name in inlined subroutine:
1063 auto i = info->subprograms.find(scope->abstract_origin_id);
1064 xbt_assert(i != info->subprograms.end(),
1065 "Could not lookup abstract origin %" PRIx64,
1066 (uint64_t) scope->abstract_origin_id);
1067 scope->name = i->second.name;
1071 for (simgrid::mc::Variable& variable : scope->variables)
1072 if (variable.type_id) {
1073 auto i = info->types.find(variable.type_id);
1074 if (i != info->types.end())
1075 variable.type = &(i->second);
1077 variable.type = nullptr;
1080 // Recursive post-processing of nested-scopes:
1081 for (simgrid::mc::Frame& nested_scope : scope->scopes)
1082 mc_post_process_scope(info, &nested_scope);
1086 static void MC_post_process_functions(mc_object_info_t info)
1088 for (auto& entry : info->subprograms)
1089 mc_post_process_scope(info, &entry.second);
1093 /** \brief Fill/lookup the "subtype" field.
1095 static void MC_resolve_subtype(mc_object_info_t info, mc_type_t type)
1099 auto i = info->types.find(type->type_id);
1100 if (i != info->types.end())
1101 type->subtype = &(i->second);
1103 type->subtype = nullptr;
1106 if (type->subtype->byte_size != 0)
1108 if (type->subtype->name.empty())
1110 // Try to find a more complete description of the type:
1111 // We need to fix in order to support C++.
1113 auto j = info->full_types_by_name.find(type->subtype->name);
1114 if (j != info->full_types_by_name.end())
1115 type->subtype = j->second;
1118 static void MC_post_process_types(mc_object_info_t info)
1120 // Lookup "subtype" field:
1121 for(auto& i : info->types) {
1122 MC_resolve_subtype(info, &(i.second));
1123 for (simgrid::mc::Type& member : i.second.members)
1124 MC_resolve_subtype(info, &member);
1128 /** \brief Finds informations about a given shared object/executable */
1129 std::shared_ptr<s_mc_object_info_t> MC_find_object_info(
1130 std::vector<simgrid::mc::VmMap> const& maps, const char *name, int executable)
1132 std::shared_ptr<s_mc_object_info_t> result =
1133 std::make_shared<s_mc_object_info_t>();
1135 result->flags |= MC_OBJECT_INFO_EXECUTABLE;
1136 result->file_name = name;
1137 MC_find_object_address(maps, result.get());
1138 MC_dwarf_get_variables(result.get());
1139 MC_post_process_types(result.get());
1140 MC_post_process_variables(result.get());
1141 MC_post_process_functions(result.get());
1142 MC_make_functions_index(result.get());
1143 return std::move(result);
1146 /*************************************************************************/
1148 static int MC_dwarf_get_variable_index(
1149 std::vector<simgrid::mc::Variable> variables, const char *var, void *address)
1152 if (variables.empty())
1155 unsigned int cursor = 0;
1157 int end = variables.size() - 1;
1158 mc_variable_t var_test = nullptr;
1160 while (start <= end) {
1161 cursor = (start + end) / 2;
1162 var_test = &variables[cursor];
1163 if (strcmp(var_test->name.c_str(), var) < 0) {
1165 } else if (strcmp(var_test->name.c_str(), var) > 0) {
1168 if (address) { /* global variable */
1169 if (var_test->address == address)
1171 if (var_test->address > address)
1175 } else { /* local variable */
1181 if (strcmp(var_test->name.c_str(), var) == 0) {
1182 if (address && var_test->address < address)
1186 } else if (strcmp(var_test->name.c_str(), var) < 0)
1193 void MC_dwarf_register_global_variable(
1194 mc_object_info_t info,
1195 std::unique_ptr<simgrid::mc::Variable> variable)
1198 MC_dwarf_get_variable_index(info->global_variables,
1199 variable->name.c_str(),
1202 info->global_variables.insert(
1203 info->global_variables.begin() + index, std::move(*variable));
1207 void MC_dwarf_register_non_global_variable(
1208 mc_object_info_t info,
1210 std::unique_ptr<simgrid::mc::Variable> variable)
1212 xbt_assert(frame, "Frame is NULL");
1214 MC_dwarf_get_variable_index(
1215 frame->variables, variable->name.c_str(), NULL);
1217 frame->variables.insert(
1218 frame->variables.begin() + index, std::move(*variable));
1222 void MC_dwarf_register_variable(
1223 mc_object_info_t info, mc_frame_t frame,
1224 std::unique_ptr<simgrid::mc::Variable> variable)
1228 if (variable->global)
1229 MC_dwarf_register_global_variable(info, std::move(variable));
1230 else if (frame != nullptr)
1231 MC_dwarf_register_non_global_variable(info, frame, std::move(variable));
1233 xbt_die("No frame for this local variable");
1236 void MC_post_process_object_info(mc_process_t process, mc_object_info_t info)
1238 for (auto& i : info->types) {
1240 mc_type_t type = &(i.second);
1241 mc_type_t subtype = type;
1242 while (subtype->type == DW_TAG_typedef || subtype->type == DW_TAG_volatile_type
1243 || subtype->type == DW_TAG_const_type) {
1244 if (subtype->subtype)
1245 subtype = subtype->subtype;
1250 // Resolve full_type:
1251 if (!subtype->name.empty() && subtype->byte_size == 0) {
1252 for (auto const& object_info : process->object_infos) {
1253 auto i = object_info->full_types_by_name.find(subtype->name);
1254 if (i != object_info->full_types_by_name.end()
1255 && !i->second->name.empty() && i->second->byte_size) {
1256 type->full_type = i->second;
1260 } else type->full_type = subtype;