1 /* Copyright (c) 2008-2015. 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>
18 #include <simgrid/util.hpp>
20 #include <xbt/sysdep.h>
22 #include <simgrid/util.hpp>
24 #include "mc_object_info.h"
25 #include "mc_private.h"
26 #include "mc_process.h"
28 #include "mc/ObjectInformation.hpp"
29 #include "mc/Variable.hpp"
31 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing");
33 /** \brief The default DW_TAG_lower_bound for a given DW_AT_language.
35 * The default for a given language is defined in the DWARF spec.
37 * \param language consant as defined by the DWARf spec
39 static uint64_t MC_dwarf_default_lower_bound(int lang);
41 /** \brief Computes the the element_count of a DW_TAG_enumeration_type DIE
43 * This is the number of elements in a given array dimension.
45 * A reference of the compilation unit (DW_TAG_compile_unit) is
46 * needed because the default lower bound (when there is no DW_AT_lower_bound)
47 * depends of the language of the compilation unit (DW_AT_language).
49 * \param die DIE for the DW_TAG_enumeration_type or DW_TAG_subrange_type
50 * \param unit DIE of the DW_TAG_compile_unit
52 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
55 /** \brief Computes the number of elements of a given DW_TAG_array_type.
57 * \param die DIE for the DW_TAG_array_type
59 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit);
61 /** \brief Process a DIE
63 * \param info the resulting object fot the library/binary file (output)
64 * \param die the current DIE
65 * \param unit the DIE of the compile unit of the current DIE
66 * \param frame containg frame if any
68 static void MC_dwarf_handle_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
69 Dwarf_Die * unit, simgrid::mc::Frame* frame,
72 /** \brief Process a type DIE
74 static void MC_dwarf_handle_type_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
75 Dwarf_Die * unit, simgrid::mc::Frame* frame,
78 /** \brief Calls MC_dwarf_handle_die on all childrend of the given die
80 * \param info the resulting object fot the library/binary file (output)
81 * \param die the current DIE
82 * \param unit the DIE of the compile unit of the current DIE
83 * \param frame containg frame if any
85 static void MC_dwarf_handle_children(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
86 Dwarf_Die * unit, simgrid::mc::Frame* frame,
89 /** \brief Handle a variable (DW_TAG_variable or other)
91 * \param info the resulting object fot the library/binary file (output)
92 * \param die the current DIE
93 * \param unit the DIE of the compile unit of the current DIE
94 * \param frame containg frame if any
96 static void MC_dwarf_handle_variable_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
97 Dwarf_Die * unit, simgrid::mc::Frame* frame,
100 /** \brief Get the DW_TAG_type of the DIE
103 * \return DW_TAG_type attribute as a new string (NULL if none)
105 static std::uint64_t MC_dwarf_at_type(Dwarf_Die * die);
107 /** \brief A class of DWARF tags (DW_TAG_*)
109 typedef enum mc_tag_class {
118 static mc_tag_class MC_dwarf_tag_classify(int tag)
122 case DW_TAG_array_type:
123 case DW_TAG_class_type:
124 case DW_TAG_enumeration_type:
126 case DW_TAG_pointer_type:
127 case DW_TAG_reference_type:
128 case DW_TAG_rvalue_reference_type:
129 case DW_TAG_string_type:
130 case DW_TAG_structure_type:
131 case DW_TAG_subroutine_type:
132 case DW_TAG_union_type:
133 case DW_TAG_ptr_to_member_type:
134 case DW_TAG_set_type:
135 case DW_TAG_subrange_type:
136 case DW_TAG_base_type:
137 case DW_TAG_const_type:
138 case DW_TAG_file_type:
139 case DW_TAG_packed_type:
140 case DW_TAG_volatile_type:
141 case DW_TAG_restrict_type:
142 case DW_TAG_interface_type:
143 case DW_TAG_unspecified_type:
144 case DW_TAG_shared_type:
147 case DW_TAG_subprogram:
148 return mc_tag_subprogram;
150 case DW_TAG_variable:
151 case DW_TAG_formal_parameter:
152 return mc_tag_variable;
154 case DW_TAG_lexical_block:
155 case DW_TAG_try_block:
156 case DW_TAG_catch_block:
157 case DW_TAG_inlined_subroutine:
158 case DW_TAG_with_stmt:
161 case DW_TAG_namespace:
162 return mc_tag_namespace;
165 return mc_tag_unknown;
170 #define MC_DW_CLASS_UNKNOWN 0
171 #define MC_DW_CLASS_ADDRESS 1 // Location in the address space of the program
172 #define MC_DW_CLASS_BLOCK 2 // Arbitrary block of bytes
173 #define MC_DW_CLASS_CONSTANT 3
174 #define MC_DW_CLASS_STRING 3 // String
175 #define MC_DW_CLASS_FLAG 4 // Boolean
176 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
177 #define MC_DW_CLASS_EXPRLOC 6 // DWARF expression/location description
178 #define MC_DW_CLASS_LINEPTR 7
179 #define MC_DW_CLASS_LOCLISTPTR 8
180 #define MC_DW_CLASS_MACPTR 9
181 #define MC_DW_CLASS_RANGELISTPTR 10
183 /** \brief Find the DWARF data class for a given DWARF data form
185 * This mapping is defined in the DWARF spec.
187 * \param form The form (values taken from the DWARF spec)
188 * \return An internal representation for the corresponding class
190 static int MC_dwarf_form_get_class(int form)
194 return MC_DW_CLASS_ADDRESS;
199 return MC_DW_CLASS_BLOCK;
206 return MC_DW_CLASS_CONSTANT;
209 return MC_DW_CLASS_STRING;
210 case DW_FORM_ref_addr:
215 case DW_FORM_ref_udata:
216 return MC_DW_CLASS_REFERENCE;
218 case DW_FORM_flag_present:
219 return MC_DW_CLASS_FLAG;
220 case DW_FORM_exprloc:
221 return MC_DW_CLASS_EXPRLOC;
225 return MC_DW_CLASS_UNKNOWN;
229 /** \brief Get the name of the tag of a given DIE
232 * \return name of the tag of this DIE
234 static inline const char *MC_dwarf_die_tagname(Dwarf_Die * die)
236 return MC_dwarf_tagname(dwarf_tag(die));
241 /** \brief Get an attribute of a given DIE as a string
244 * \param attribute attribute
245 * \return value of the given attribute of the given DIE
247 static const char *MC_dwarf_attr_integrate_string(Dwarf_Die * die,
250 Dwarf_Attribute attr;
251 if (!dwarf_attr_integrate(die, attribute, &attr)) {
254 return dwarf_formstring(&attr);
258 /** \brief Get the linkage name of a DIE.
260 * Use either DW_AT_linkage_name or DW_AT_MIPS_linkage_name.
261 * DW_AT_linkage_name is standardized since DWARF 4.
262 * Before this version of DWARF, the MIPS extensions
263 * DW_AT_MIPS_linkage_name is used (at least by GCC).
266 * \return linkage name of the given DIE (or NULL)
268 static const char *MC_dwarf_at_linkage_name(Dwarf_Die * die)
270 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_linkage_name);
272 name = MC_dwarf_attr_integrate_string(die, DW_AT_MIPS_linkage_name);
276 static Dwarf_Off MC_dwarf_attr_dieoffset(Dwarf_Die * die, int attribute)
278 Dwarf_Attribute attr;
279 if (dwarf_hasattr_integrate(die, attribute)) {
280 dwarf_attr_integrate(die, attribute, &attr);
281 Dwarf_Die subtype_die;
282 if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
283 xbt_die("Could not find DIE");
285 return dwarf_dieoffset(&subtype_die);
290 static Dwarf_Off MC_dwarf_attr_integrate_dieoffset(Dwarf_Die * die,
293 Dwarf_Attribute attr;
294 if (dwarf_hasattr_integrate(die, attribute)) {
295 dwarf_attr_integrate(die, DW_AT_type, &attr);
296 Dwarf_Die subtype_die;
297 if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
298 xbt_die("Could not find DIE");
300 return dwarf_dieoffset(&subtype_die);
305 /** \brief Find the type/subtype (DW_AT_type) for a DIE
308 * \return DW_AT_type reference as a global offset in hexadecimal (or NULL)
311 std::uint64_t MC_dwarf_at_type(Dwarf_Die * die)
313 return MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
316 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die * die, int attribute)
318 Dwarf_Attribute attr;
319 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
322 if (dwarf_formaddr(&attr, &value) == 0)
323 return (uint64_t) value;
328 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die * die, int attribute,
329 uint64_t default_value)
331 Dwarf_Attribute attr;
332 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
333 return default_value;
335 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr),
336 &value) == 0 ? (uint64_t) value : default_value;
339 static bool MC_dwarf_attr_flag(Dwarf_Die * die, int attribute, bool integrate)
341 Dwarf_Attribute attr;
342 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
343 : dwarf_attr(die, attribute, &attr)) == 0)
347 if (dwarf_formflag(&attr, &result))
348 xbt_die("Unexpected form for attribute %s", MC_dwarf_attrname(attribute));
352 /** \brief Find the default lower bound for a given language
354 * The default lower bound of an array (when DW_TAG_lower_bound
355 * is missing) depends on the language of the compilation unit.
357 * \param lang Language of the compilation unit (values defined in the DWARF spec)
358 * \return Default lower bound of an array in this compilation unit
360 static uint64_t MC_dwarf_default_lower_bound(int lang)
366 case DW_LANG_C_plus_plus:
370 case DW_LANG_ObjC_plus_plus:
376 case DW_LANG_Fortran77:
377 case DW_LANG_Fortran90:
378 case DW_LANG_Fortran95:
379 case DW_LANG_Modula2:
380 case DW_LANG_Pascal83:
382 case DW_LANG_Cobol74:
383 case DW_LANG_Cobol85:
386 xbt_die("No default DW_TAG_lower_bound for language %i and none given",
392 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
395 * \param unit DIE of the compilation unit
396 * \return number of elements in the range
398 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
401 xbt_assert(dwarf_tag(die) == DW_TAG_enumeration_type
402 || dwarf_tag(die) == DW_TAG_subrange_type,
403 "MC_dwarf_subrange_element_count called with DIE of type %s",
404 MC_dwarf_die_tagname(die));
406 // Use DW_TAG_count if present:
407 if (dwarf_hasattr_integrate(die, DW_AT_count)) {
408 return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
410 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
412 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
413 // This is not really 0, but the code expects this (we do not know):
416 uint64_t upper_bound =
417 MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
419 uint64_t lower_bound = 0;
420 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
421 lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
423 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
425 return upper_bound - lower_bound + 1;
428 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
430 * The compilation unit might be needed because the default lower
431 * bound depends on the language of the compilation unit.
433 * \param die the DIE of the DW_TAG_array_type
434 * \param unit the DIE of the compilation unit
435 * \return number of elements in this array type
437 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit)
439 xbt_assert(dwarf_tag(die) == DW_TAG_array_type,
440 "MC_dwarf_array_element_count called with DIE of type %s",
441 MC_dwarf_die_tagname(die));
446 for (res = dwarf_child(die, &child); res == 0;
447 res = dwarf_siblingof(&child, &child)) {
448 int child_tag = dwarf_tag(&child);
449 if (child_tag == DW_TAG_subrange_type
450 || child_tag == DW_TAG_enumeration_type) {
451 result *= MC_dwarf_subrange_element_count(&child, unit);
459 /** Sort the variable by name and address.
461 * We could use boost::container::flat_set instead.
463 static bool MC_compare_variable(
464 simgrid::mc::Variable const& a, simgrid::mc::Variable const& b)
466 int cmp = strcmp(a.name.c_str(), b.name.c_str());
472 return a.address < b.address;
475 // ***** simgrid::mc::Type*
477 /** \brief Initialize the location of a member of a type
478 * (DW_AT_data_member_location of a DW_TAG_member).
480 * \param type a type (struct, class)
481 * \param member the member of the type
482 * \param child DIE of the member (DW_TAG_member)
484 static void MC_dwarf_fill_member_location(simgrid::mc::Type* type, simgrid::mc::Type* member,
487 if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
488 xbt_die("Can't groke DW_AT_data_bit_offset.");
491 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
492 if (type->type != DW_TAG_union_type) {
494 ("Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%"
495 PRIx64 ">%s", member->name.c_str(),
496 (uint64_t) type->id, type->name.c_str());
502 Dwarf_Attribute attr;
503 dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
504 int form = dwarf_whatform(&attr);
505 int klass = MC_dwarf_form_get_class(form);
507 case MC_DW_CLASS_EXPRLOC:
508 case MC_DW_CLASS_BLOCK:
509 // Location expression:
513 if (dwarf_getlocation(&attr, &expr, &len)) {
515 ("Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%"
516 PRIx64 ">%s", MC_dwarf_attr_integrate_string(child, DW_AT_name),
517 (uint64_t) type->id, type->name.c_str());
519 simgrid::mc::DwarfExpression(expr, expr+len);
522 case MC_DW_CLASS_CONSTANT:
523 // Offset from the base address of the object:
526 if (!dwarf_formudata(&attr, &offset))
527 member->offset(offset);
529 xbt_die("Cannot get %s location <%" PRIx64 ">%s",
530 MC_dwarf_attr_integrate_string(child, DW_AT_name),
531 (uint64_t) type->id, type->name.c_str());
534 case MC_DW_CLASS_LOCLISTPTR:
535 // Reference to a location list:
537 case MC_DW_CLASS_REFERENCE:
538 // It's supposed to be possible in DWARF2 but I couldn't find its semantic
541 xbt_die("Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
547 /** \brief Populate the list of members of a type
549 * \param info ELF object containing the type DIE
550 * \param die DIE of the type
551 * \param unit DIE of the compilation unit containing the type DIE
552 * \param type the type
554 static void MC_dwarf_add_members(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
555 Dwarf_Die * unit, simgrid::mc::Type* type)
559 xbt_assert(type->members.empty());
560 for (res = dwarf_child(die, &child); res == 0;
561 res = dwarf_siblingof(&child, &child)) {
562 int tag = dwarf_tag(&child);
563 if (tag == DW_TAG_member || tag == DW_TAG_inheritance) {
565 // Skip declarations:
566 if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
569 // Skip compile time constants:
570 if (dwarf_hasattr(&child, DW_AT_const_value))
573 // TODO, we should use another type (because is is not a type but a member)
574 simgrid::mc::Type member;
578 member.id = dwarf_dieoffset(&child);
580 const char *name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
584 MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
585 member.element_count = -1;
586 member.type_id = MC_dwarf_at_type(&child);
588 if (dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
589 xbt_die("Can't groke DW_AT_data_bit_offset.");
592 MC_dwarf_fill_member_location(type, &member, &child);
594 if (!member.type_id) {
595 xbt_die("Missing type for member %s of <%" PRIx64 ">%s",
597 (uint64_t) type->id, type->name.c_str());
600 type->members.push_back(std::move(member));
605 /** \brief Create a MC type object from a DIE
607 * \param info current object info object
608 * \param DIE (for a given type);
609 * \param unit compilation unit of the current DIE
610 * \return MC representation of the type
612 static simgrid::mc::Type MC_dwarf_die_to_type(
613 simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
614 Dwarf_Die * unit, simgrid::mc::Frame* frame,
617 simgrid::mc::Type type;
618 type.type = dwarf_tag(die);
619 type.name = std::string();
620 type.element_count = -1;
623 type.id = dwarf_dieoffset(die);
625 const char *prefix = "";
627 case DW_TAG_structure_type:
630 case DW_TAG_union_type:
633 case DW_TAG_class_type:
640 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
642 char* full_name = ns ? bprintf("%s%s::%s", prefix, ns, name) :
643 bprintf("%s%s", prefix, name);
644 type.name = std::string(full_name);
648 type.type_id = MC_dwarf_at_type(die);
650 // Some compilers do not emit DW_AT_byte_size for pointer_type,
651 // so we fill this. We currently assume that the model-checked process is in
652 // the same architecture..
653 if (type.type == DW_TAG_pointer_type)
654 type.byte_size = sizeof(void*);
656 // Computation of the byte_size;
657 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
658 type.byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
659 else if (type.type == DW_TAG_array_type
660 || type.type == DW_TAG_structure_type
661 || type.type == DW_TAG_class_type) {
663 if (dwarf_aggregate_size(die, &size) == 0) {
664 type.byte_size = size;
669 case DW_TAG_array_type:
670 type.element_count = MC_dwarf_array_element_count(die, unit);
671 // TODO, handle DW_byte_stride and (not) DW_bit_stride
674 case DW_TAG_pointer_type:
675 case DW_TAG_reference_type:
676 case DW_TAG_rvalue_reference_type:
677 type.is_pointer_type = 1;
680 case DW_TAG_structure_type:
681 case DW_TAG_union_type:
682 case DW_TAG_class_type:
683 MC_dwarf_add_members(info, die, unit, &type);
684 char *new_ns = ns == NULL ? xbt_strdup(type.name.c_str())
685 : bprintf("%s::%s", ns, name);
686 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
691 return std::move(type);
694 static void MC_dwarf_handle_type_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
695 Dwarf_Die * unit, simgrid::mc::Frame* frame,
698 simgrid::mc::Type type = MC_dwarf_die_to_type(info, die, unit, frame, ns);
699 auto& t = (info->types[type.id] = std::move(type));
700 if (!t.name.empty() && type.byte_size != 0)
701 info->full_types_by_name[t.name] = &t;
704 static int mc_anonymous_variable_index = 0;
706 static std::unique_ptr<simgrid::mc::Variable> MC_die_to_variable(
707 simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
708 Dwarf_Die * unit, simgrid::mc::Frame* frame,
711 // Skip declarations:
712 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
715 // Skip compile time constants:
716 if (dwarf_hasattr(die, DW_AT_const_value))
719 Dwarf_Attribute attr_location;
720 if (dwarf_attr(die, DW_AT_location, &attr_location) == NULL) {
721 // No location: do not add it ?
725 std::unique_ptr<simgrid::mc::Variable> variable =
726 std::unique_ptr<simgrid::mc::Variable>(new simgrid::mc::Variable());
727 variable->dwarf_offset = dwarf_dieoffset(die);
728 variable->global = frame == NULL; // Can be override base on DW_AT_location
729 variable->object_info = info;
731 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
733 variable->name = name;
734 variable->type_id = MC_dwarf_at_type(die);
736 int form = dwarf_whatform(&attr_location);
739 DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
741 case MC_DW_CLASS_EXPRLOC:
742 case MC_DW_CLASS_BLOCK:
743 // Location expression:
747 if (dwarf_getlocation(&attr_location, &expr, &len)) {
749 "Could not read location expression in DW_AT_location "
750 "of variable <%" PRIx64 ">%s",
751 (uint64_t) variable->dwarf_offset,
752 variable->name.c_str());
755 if (len == 1 && expr[0].atom == DW_OP_addr) {
756 variable->global = 1;
757 uintptr_t offset = (uintptr_t) expr[0].number;
758 uintptr_t base = (uintptr_t) info->base_address();
759 variable->address = (void *) (base + offset);
761 simgrid::mc::LocationListEntry entry;
762 entry.expression = {expr, expr + len};
763 variable->location_list = { std::move(entry) };
768 case MC_DW_CLASS_LOCLISTPTR:
769 case MC_DW_CLASS_CONSTANT:
770 // Reference to location list:
771 mc_dwarf_location_list_init(
772 &variable->location_list, info, die,
776 xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location "
777 "in <%" PRIx64 ">%s",
778 form, form, klass, klass,
779 (uint64_t) variable->dwarf_offset,
780 variable->name.c_str());
783 // Handle start_scope:
784 if (dwarf_hasattr(die, DW_AT_start_scope)) {
785 Dwarf_Attribute attr;
786 dwarf_attr(die, DW_AT_start_scope, &attr);
787 int form = dwarf_whatform(&attr);
788 int klass = MC_dwarf_form_get_class(form);
790 case MC_DW_CLASS_CONSTANT:
793 variable->start_scope =
794 dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
797 case MC_DW_CLASS_RANGELISTPTR: // TODO
800 ("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
801 form, klass, name == NULL ? "?" : name);
805 if (ns && variable->global)
807 std::string(ns) + "::" + variable->name;
809 // The current code needs a variable name,
810 // generate a fake one:
811 if (variable->name.empty())
813 "@anonymous#" + std::to_string(mc_anonymous_variable_index++);
815 return std::move(variable);
818 static void MC_dwarf_handle_variable_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
819 Dwarf_Die * unit, simgrid::mc::Frame* frame,
822 std::unique_ptr<simgrid::mc::Variable> variable =
823 MC_die_to_variable(info, die, unit, frame, ns);
826 // Those arrays are sorted later:
827 else if (variable->global)
828 info->global_variables.push_back(std::move(*variable));
829 else if (frame != nullptr)
830 frame->variables.push_back(std::move(*variable));
832 xbt_die("No frame for this local variable");
835 static void MC_dwarf_handle_scope_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
836 Dwarf_Die * unit, simgrid::mc::Frame* parent_frame,
839 // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
840 int tag = dwarf_tag(die);
841 mc_tag_class klass = MC_dwarf_tag_classify(tag);
843 // (Template) Subprogram declaration:
844 if (klass == mc_tag_subprogram
845 && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
848 if (klass == mc_tag_scope)
849 xbt_assert(parent_frame, "No parent scope for this scope");
851 simgrid::mc::Frame frame;
854 frame.id = dwarf_dieoffset(die);
855 frame.object_info = info;
857 if (klass == mc_tag_subprogram) {
858 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
860 frame.name = std::string(ns) + "::" + name;
867 frame.abstract_origin_id =
868 MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
870 // This is the base address for DWARF addresses.
871 // Relocated addresses are offset from this base address.
872 // See DWARF4 spec 7.5
873 void *base = info->base_address();
875 // TODO, support DW_AT_ranges
876 uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
877 frame.low_pc = low_pc ? ((char *) base) + low_pc : 0;
880 Dwarf_Attribute attr;
881 if (!dwarf_attr_integrate(die, DW_AT_high_pc, &attr)) {
882 xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
888 switch (MC_dwarf_form_get_class(dwarf_whatform(&attr))) {
890 // DW_AT_high_pc if an offset from the low_pc:
891 case MC_DW_CLASS_CONSTANT:
893 if (dwarf_formsdata(&attr, &offset) != 0)
894 xbt_die("Could not read constant");
895 frame.high_pc = (void *) ((char *) frame.low_pc + offset);
898 // DW_AT_high_pc is a relocatable address:
899 case MC_DW_CLASS_ADDRESS:
900 if (dwarf_formaddr(&attr, &high_pc) != 0)
901 xbt_die("Could not read address");
902 frame.high_pc = ((char *) base) + high_pc;
906 xbt_die("Unexpected class for DW_AT_high_pc");
911 if (klass == mc_tag_subprogram) {
912 Dwarf_Attribute attr_frame_base;
913 if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
914 mc_dwarf_location_list_init(&frame.frame_base, info, die,
919 MC_dwarf_handle_children(info, die, unit, &frame, ns);
921 // Someone needs this to be sorted but who?
922 std::sort(frame.variables.begin(), frame.variables.end(),
923 MC_compare_variable);
926 if (klass == mc_tag_subprogram)
927 info->subprograms[frame.id] = frame;
928 else if (klass == mc_tag_scope)
929 parent_frame->scopes.push_back(std::move(frame));
932 static void mc_dwarf_handle_namespace_die(simgrid::mc::ObjectInformation* info,
933 Dwarf_Die * die, Dwarf_Die * unit,
934 simgrid::mc::Frame* frame,
937 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
939 xbt_die("Unexpected namespace in a subprogram");
940 char *new_ns = ns == NULL ? xbt_strdup(name)
941 : bprintf("%s::%s", ns, name);
942 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
946 static void MC_dwarf_handle_children(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
947 Dwarf_Die * unit, simgrid::mc::Frame* frame,
950 // For each child DIE:
953 for (res = dwarf_child(die, &child); res == 0;
954 res = dwarf_siblingof(&child, &child)) {
955 MC_dwarf_handle_die(info, &child, unit, frame, ns);
959 static void MC_dwarf_handle_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
960 Dwarf_Die * unit, simgrid::mc::Frame* frame,
963 int tag = dwarf_tag(die);
964 mc_tag_class klass = MC_dwarf_tag_classify(tag);
969 MC_dwarf_handle_type_die(info, die, unit, frame, ns);
972 // Subprogram or scope:
973 case mc_tag_subprogram:
975 MC_dwarf_handle_scope_die(info, die, unit, frame, ns);
979 case mc_tag_variable:
980 MC_dwarf_handle_variable_die(info, die, unit, frame, ns);
983 case mc_tag_namespace:
984 mc_dwarf_handle_namespace_die(info, die, unit, frame, ns);
993 /** \brief Populate the debugging informations of the given ELF object
995 * Read the DWARf information of the EFFL object and populate the
996 * lists of types, variables, functions.
998 void MC_dwarf_get_variables(simgrid::mc::ObjectInformation* info)
1000 int fd = open(info->file_name.c_str(), O_RDONLY);
1002 xbt_die("Could not open file %s", info->file_name.c_str());
1003 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
1005 xbt_die("Your program must be compiled with -g (%s)",
1006 info->file_name.c_str());
1007 // For each compilation unit:
1008 Dwarf_Off offset = 0;
1009 Dwarf_Off next_offset = 0;
1011 while (dwarf_nextcu(dwarf, offset, &next_offset, &length, NULL, NULL, NULL) ==
1014 if (dwarf_offdie(dwarf, offset + length, &unit_die) != NULL) {
1016 // For each child DIE:
1019 for (res = dwarf_child(&unit_die, &child); res == 0;
1020 res = dwarf_siblingof(&child, &child)) {
1021 MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
1025 offset = next_offset;
1032 // ***** Functions index
1034 static int MC_compare_frame_index_items(simgrid::mc::FunctionIndexEntry* a,
1035 simgrid::mc::FunctionIndexEntry* b)
1037 if (a->low_pc < b->low_pc)
1039 else if (a->low_pc == b->low_pc)
1045 static void MC_make_functions_index(simgrid::mc::ObjectInformation* info)
1047 info->functions_index.clear();
1049 for (auto& e : info->subprograms) {
1050 if (e.second.low_pc == nullptr)
1052 simgrid::mc::FunctionIndexEntry entry;
1053 entry.low_pc = e.second.low_pc;
1054 entry.function = &e.second;
1055 info->functions_index.push_back(entry);
1058 info->functions_index.shrink_to_fit();
1060 // Sort the array by low_pc:
1061 std::sort(info->functions_index.begin(), info->functions_index.end(),
1062 [](simgrid::mc::FunctionIndexEntry const& a,
1063 simgrid::mc::FunctionIndexEntry const& b)
1065 return a.low_pc < b.low_pc;
1069 static void MC_post_process_variables(simgrid::mc::ObjectInformation* info)
1071 // Someone needs this to be sorted but who?
1072 std::sort(info->global_variables.begin(), info->global_variables.end(),
1073 MC_compare_variable);
1075 for(simgrid::mc::Variable& variable : info->global_variables)
1076 if (variable.type_id) {
1077 variable.type = simgrid::util::find_map_ptr(
1078 info->types, variable.type_id);
1082 static void mc_post_process_scope(simgrid::mc::ObjectInformation* info, simgrid::mc::Frame* scope)
1085 if (scope->tag == DW_TAG_inlined_subroutine) {
1086 // Attach correct namespaced name in inlined subroutine:
1087 auto i = info->subprograms.find(scope->abstract_origin_id);
1088 xbt_assert(i != info->subprograms.end(),
1089 "Could not lookup abstract origin %" PRIx64,
1090 (uint64_t) scope->abstract_origin_id);
1091 scope->name = i->second.name;
1095 for (simgrid::mc::Variable& variable : scope->variables)
1096 if (variable.type_id) {
1097 variable.type = simgrid::util::find_map_ptr(
1098 info->types, variable.type_id);
1101 // Recursive post-processing of nested-scopes:
1102 for (simgrid::mc::Frame& nested_scope : scope->scopes)
1103 mc_post_process_scope(info, &nested_scope);
1107 /** \brief Fill/lookup the "subtype" field.
1109 static void MC_resolve_subtype(simgrid::mc::ObjectInformation* info, simgrid::mc::Type* type)
1113 type->subtype = simgrid::util::find_map_ptr(info->types, type->type_id);
1114 if (type->subtype == nullptr)
1116 if (type->subtype->byte_size != 0)
1118 if (type->subtype->name.empty())
1120 // Try to find a more complete description of the type:
1121 // We need to fix in order to support C++.
1122 simgrid::mc::Type** subtype = simgrid::util::find_map_ptr(
1123 info->full_types_by_name, type->subtype->name);
1125 type->subtype = *subtype;
1128 static void MC_post_process_types(simgrid::mc::ObjectInformation* info)
1130 // Lookup "subtype" field:
1131 for(auto& i : info->types) {
1132 MC_resolve_subtype(info, &(i.second));
1133 for (simgrid::mc::Type& member : i.second.members)
1134 MC_resolve_subtype(info, &member);
1138 /** \brief Finds informations about a given shared object/executable */
1139 std::shared_ptr<simgrid::mc::ObjectInformation> MC_find_object_info(
1140 std::vector<simgrid::mc::VmMap> const& maps, const char *name, int executable)
1142 std::shared_ptr<simgrid::mc::ObjectInformation> result =
1143 std::make_shared<simgrid::mc::ObjectInformation>();
1145 result->flags |= simgrid::mc::ObjectInformation::Executable;
1146 result->file_name = name;
1147 MC_find_object_address(maps, result.get());
1148 MC_dwarf_get_variables(result.get());
1149 MC_post_process_variables(result.get());
1150 MC_post_process_types(result.get());
1151 for (auto& entry : result.get()->subprograms)
1152 mc_post_process_scope(result.get(), &entry.second);
1153 MC_make_functions_index(result.get());
1154 return std::move(result);
1157 /*************************************************************************/
1159 void MC_post_process_object_info(simgrid::mc::Process* process, simgrid::mc::ObjectInformation* info)
1161 for (auto& i : info->types) {
1163 simgrid::mc::Type* type = &(i.second);
1164 simgrid::mc::Type* subtype = type;
1165 while (subtype->type == DW_TAG_typedef || subtype->type == DW_TAG_volatile_type
1166 || subtype->type == DW_TAG_const_type) {
1167 if (subtype->subtype)
1168 subtype = subtype->subtype;
1173 // Resolve full_type:
1174 if (!subtype->name.empty() && subtype->byte_size == 0) {
1175 for (auto const& object_info : process->object_infos) {
1176 auto i = object_info->full_types_by_name.find(subtype->name);
1177 if (i != object_info->full_types_by_name.end()
1178 && !i->second->name.empty() && i->second->byte_size) {
1179 type->full_type = i->second;
1183 } else type->full_type = subtype;