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. */
14 #define DW_LANG_Objc DW_LANG_ObjC /* fix spelling error in older dwarf.h */
16 #include <elfutils/libdw.h>
18 #include <simgrid_config.h>
19 #include "src/simgrid/util.hpp"
21 #include <xbt/sysdep.h>
23 #include "src/mc/mc_private.h"
24 #include "src/mc/mc_dwarf.hpp"
26 #include "src/mc/mc_object_info.h"
27 #include "src/mc/Process.hpp"
28 #include "src/mc/ObjectInformation.hpp"
29 #include "src/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 (nullptr if none)
105 static std::uint64_t MC_dwarf_at_type(Dwarf_Die * die);
110 enum class TagClass {
119 /*** Class of forms defined in the DWARF standard */
120 enum class FormClass {
122 Address, // Location in the program's address space
123 Block, // Arbitrary block of bytes
126 Flag, // Boolean value
127 Reference, // Reference to another DIE
128 ExprLoc, // DWARF expression/location description
136 TagClass classify_tag(int tag)
140 case DW_TAG_array_type:
141 case DW_TAG_class_type:
142 case DW_TAG_enumeration_type:
144 case DW_TAG_pointer_type:
145 case DW_TAG_reference_type:
146 case DW_TAG_rvalue_reference_type:
147 case DW_TAG_string_type:
148 case DW_TAG_structure_type:
149 case DW_TAG_subroutine_type:
150 case DW_TAG_union_type:
151 case DW_TAG_ptr_to_member_type:
152 case DW_TAG_set_type:
153 case DW_TAG_subrange_type:
154 case DW_TAG_base_type:
155 case DW_TAG_const_type:
156 case DW_TAG_file_type:
157 case DW_TAG_packed_type:
158 case DW_TAG_volatile_type:
159 case DW_TAG_restrict_type:
160 case DW_TAG_interface_type:
161 case DW_TAG_unspecified_type:
162 case DW_TAG_shared_type:
163 return TagClass::Type;
165 case DW_TAG_subprogram:
166 return TagClass::Subprogram;
168 case DW_TAG_variable:
169 case DW_TAG_formal_parameter:
170 return TagClass::Variable;
172 case DW_TAG_lexical_block:
173 case DW_TAG_try_block:
174 case DW_TAG_catch_block:
175 case DW_TAG_inlined_subroutine:
176 case DW_TAG_with_stmt:
177 return TagClass::Scope;
179 case DW_TAG_namespace:
180 return TagClass::Namespace;
183 return TagClass::Unknown;
187 /** \brief Find the DWARF data class for a given DWARF data form
189 * This mapping is defined in the DWARF spec.
191 * \param form The form (values taken from the DWARF spec)
192 * \return An internal representation for the corresponding class
195 FormClass classify_form(int form)
199 return FormClass::Address;
204 return FormClass::Block;
211 return FormClass::Constant;
214 return FormClass::String;
215 case DW_FORM_ref_addr:
220 case DW_FORM_ref_udata:
221 return FormClass::Reference;
223 case DW_FORM_flag_present:
224 return FormClass::Flag;
225 case DW_FORM_exprloc:
226 return FormClass::ExprLoc;
230 return FormClass::Unknown;
234 /** \brief Get the name of the tag of a given DIE
237 * \return name of the tag of this DIE
240 const char *tagname(Dwarf_Die * die)
242 return simgrid::dwarf::tagname(dwarf_tag(die));
250 /** \brief Get an attribute of a given DIE as a string
253 * \param attribute attribute
254 * \return value of the given attribute of the given DIE
256 static const char *MC_dwarf_attr_integrate_string(Dwarf_Die * die,
259 Dwarf_Attribute attr;
260 if (!dwarf_attr_integrate(die, attribute, &attr)) {
263 return dwarf_formstring(&attr);
267 /** \brief Get the linkage name of a DIE.
269 * Use either DW_AT_linkage_name or DW_AT_MIPS_linkage_name.
270 * DW_AT_linkage_name is standardized since DWARF 4.
271 * Before this version of DWARF, the MIPS extensions
272 * DW_AT_MIPS_linkage_name is used (at least by GCC).
275 * \return linkage name of the given DIE (or nullptr)
277 static const char *MC_dwarf_at_linkage_name(Dwarf_Die * die)
279 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_linkage_name);
281 name = MC_dwarf_attr_integrate_string(die, DW_AT_MIPS_linkage_name);
285 static Dwarf_Off MC_dwarf_attr_dieoffset(Dwarf_Die * die, int attribute)
287 Dwarf_Attribute attr;
288 if (dwarf_hasattr_integrate(die, attribute) == 0)
290 dwarf_attr_integrate(die, attribute, &attr);
291 Dwarf_Die subtype_die;
292 if (dwarf_formref_die(&attr, &subtype_die) == nullptr)
293 xbt_die("Could not find DIE");
294 return dwarf_dieoffset(&subtype_die);
297 static Dwarf_Off MC_dwarf_attr_integrate_dieoffset(Dwarf_Die * die,
300 Dwarf_Attribute attr;
301 if (dwarf_hasattr_integrate(die, attribute) == 0)
303 dwarf_attr_integrate(die, DW_AT_type, &attr);
304 Dwarf_Die subtype_die;
305 if (dwarf_formref_die(&attr, &subtype_die) == nullptr)
306 xbt_die("Could not find DIE");
307 return dwarf_dieoffset(&subtype_die);
310 /** \brief Find the type/subtype (DW_AT_type) for a DIE
313 * \return DW_AT_type reference as a global offset in hexadecimal (or nullptr)
316 std::uint64_t MC_dwarf_at_type(Dwarf_Die * die)
318 return MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
321 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die * die, int attribute)
323 Dwarf_Attribute attr;
324 if (dwarf_attr_integrate(die, attribute, &attr) == nullptr)
327 if (dwarf_formaddr(&attr, &value) == 0)
328 return (uint64_t) value;
333 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die * die, int attribute,
334 uint64_t default_value)
336 Dwarf_Attribute attr;
337 if (dwarf_attr_integrate(die, attribute, &attr) == nullptr)
338 return default_value;
340 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr),
341 &value) == 0 ? (uint64_t) value : default_value;
344 static bool MC_dwarf_attr_flag(Dwarf_Die * die, int attribute, bool integrate)
346 Dwarf_Attribute attr;
347 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
348 : dwarf_attr(die, attribute, &attr)) == 0)
352 if (dwarf_formflag(&attr, &result))
353 xbt_die("Unexpected form for attribute %s",
354 simgrid::dwarf::attrname(attribute));
358 /** \brief Find the default lower bound for a given language
360 * The default lower bound of an array (when DW_TAG_lower_bound
361 * is missing) depends on the language of the compilation unit.
363 * \param lang Language of the compilation unit (values defined in the DWARF spec)
364 * \return Default lower bound of an array in this compilation unit
366 static uint64_t MC_dwarf_default_lower_bound(int lang)
372 case DW_LANG_C_plus_plus:
376 case DW_LANG_ObjC_plus_plus:
382 case DW_LANG_Fortran77:
383 case DW_LANG_Fortran90:
384 case DW_LANG_Fortran95:
385 case DW_LANG_Modula2:
386 case DW_LANG_Pascal83:
388 case DW_LANG_Cobol74:
389 case DW_LANG_Cobol85:
392 xbt_die("No default DW_TAG_lower_bound for language %i and none given",
398 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
401 * \param unit DIE of the compilation unit
402 * \return number of elements in the range
404 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
407 xbt_assert(dwarf_tag(die) == DW_TAG_enumeration_type
408 || dwarf_tag(die) == DW_TAG_subrange_type,
409 "MC_dwarf_subrange_element_count called with DIE of type %s",
410 simgrid::dwarf::tagname(die));
412 // Use DW_TAG_count if present:
413 if (dwarf_hasattr_integrate(die, DW_AT_count))
414 return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
415 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
417 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound))
418 // This is not really 0, but the code expects this (we do not know):
421 uint64_t upper_bound =
422 MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
424 uint64_t lower_bound = 0;
425 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound))
426 lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
428 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
429 return upper_bound - lower_bound + 1;
432 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
434 * The compilation unit might be needed because the default lower
435 * bound depends on the language of the compilation unit.
437 * \param die the DIE of the DW_TAG_array_type
438 * \param unit the DIE of the compilation unit
439 * \return number of elements in this array type
441 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit)
443 xbt_assert(dwarf_tag(die) == DW_TAG_array_type,
444 "MC_dwarf_array_element_count called with DIE of type %s",
445 simgrid::dwarf::tagname(die));
450 for (res = dwarf_child(die, &child); res == 0;
451 res = dwarf_siblingof(&child, &child)) {
452 int child_tag = dwarf_tag(&child);
453 if (child_tag == DW_TAG_subrange_type
454 || child_tag == DW_TAG_enumeration_type)
455 result *= MC_dwarf_subrange_element_count(&child, unit);
462 /** Sort the variable by name and address.
464 * We could use boost::container::flat_set instead.
466 static bool MC_compare_variable(
467 simgrid::mc::Variable const& a, simgrid::mc::Variable const& b)
469 int cmp = strcmp(a.name.c_str(), b.name.c_str());
475 return a.address < b.address;
478 // ***** simgrid::mc::Type*
480 /** \brief Initialize the location of a member of a type
481 * (DW_AT_data_member_location of a DW_TAG_member).
483 * \param type a type (struct, class)
484 * \param member the member of the type
485 * \param child DIE of the member (DW_TAG_member)
487 static void MC_dwarf_fill_member_location(
488 simgrid::mc::Type* type, simgrid::mc::Member* member, Dwarf_Die * child)
490 if (dwarf_hasattr(child, DW_AT_data_bit_offset))
491 xbt_die("Can't groke DW_AT_data_bit_offset.");
493 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
494 if (type->type == DW_TAG_union_type)
497 ("Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%"
498 PRIx64 ">%s", member->name.c_str(),
499 (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 simgrid::dwarf::FormClass form_class = simgrid::dwarf::classify_form(form);
506 switch (form_class) {
507 case simgrid::dwarf::FormClass::ExprLoc:
508 case simgrid::dwarf::FormClass::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());
518 member->location_expression = simgrid::dwarf::DwarfExpression(expr, expr+len);
521 case simgrid::dwarf::FormClass::Constant:
522 // Offset from the base address of the object:
525 if (!dwarf_formudata(&attr, &offset))
526 member->offset(offset);
528 xbt_die("Cannot get %s location <%" PRIx64 ">%s",
529 MC_dwarf_attr_integrate_string(child, DW_AT_name),
530 (uint64_t) type->id, type->name.c_str());
533 case simgrid::dwarf::FormClass::LocListPtr:
534 // Reference to a location list:
536 case simgrid::dwarf::FormClass::Reference:
537 // It's supposed to be possible in DWARF2 but I couldn't find its semantic
540 xbt_die("Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
541 (int) form_class, form);
546 /** \brief Populate the list of members of a type
548 * \param info ELF object containing the type DIE
549 * \param die DIE of the type
550 * \param unit DIE of the compilation unit containing the type DIE
551 * \param type the type
553 static void MC_dwarf_add_members(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
554 Dwarf_Die * unit, simgrid::mc::Type* type)
558 xbt_assert(type->members.empty());
559 for (res = dwarf_child(die, &child); res == 0;
560 res = dwarf_siblingof(&child, &child)) {
561 int tag = dwarf_tag(&child);
562 if (tag == DW_TAG_member || tag == DW_TAG_inheritance) {
564 // Skip declarations:
565 if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
568 // Skip compile time constants:
569 if (dwarf_hasattr(&child, DW_AT_const_value))
572 // TODO, we should use another type (because is is not a type but a member)
573 simgrid::mc::Member member;
574 member.inheritance = tag == DW_TAG_inheritance;
576 const char *name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
580 MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
581 member.type_id = MC_dwarf_at_type(&child);
583 if (dwarf_hasattr(&child, DW_AT_data_bit_offset))
584 xbt_die("Can't groke DW_AT_data_bit_offset.");
586 MC_dwarf_fill_member_location(type, &member, &child);
589 xbt_die("Missing type for member %s of <%" PRIx64 ">%s",
591 (uint64_t) type->id, type->name.c_str());
593 type->members.push_back(std::move(member));
598 /** \brief Create a MC type object from a DIE
600 * \param info current object info object
601 * \param DIE (for a given type);
602 * \param unit compilation unit of the current DIE
603 * \return MC representation of the type
605 static simgrid::mc::Type MC_dwarf_die_to_type(
606 simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
607 Dwarf_Die * unit, simgrid::mc::Frame* frame,
610 simgrid::mc::Type type;
611 type.type = dwarf_tag(die);
612 type.name = std::string();
613 type.element_count = -1;
616 type.id = dwarf_dieoffset(die);
618 const char *prefix = "";
620 case DW_TAG_structure_type:
623 case DW_TAG_union_type:
626 case DW_TAG_class_type:
633 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
634 if (name != nullptr) {
635 char* full_name = ns ? bprintf("%s%s::%s", prefix, ns, name) :
636 bprintf("%s%s", prefix, name);
637 type.name = std::string(full_name);
641 type.type_id = MC_dwarf_at_type(die);
643 // Some compilers do not emit DW_AT_byte_size for pointer_type,
644 // so we fill this. We currently assume that the model-checked process is in
645 // the same architecture..
646 if (type.type == DW_TAG_pointer_type)
647 type.byte_size = sizeof(void*);
649 // Computation of the byte_size;
650 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
651 type.byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
652 else if (type.type == DW_TAG_array_type
653 || type.type == DW_TAG_structure_type
654 || type.type == DW_TAG_class_type) {
656 if (dwarf_aggregate_size(die, &size) == 0)
657 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:
671 case DW_TAG_structure_type:
672 case DW_TAG_union_type:
673 case DW_TAG_class_type:
674 MC_dwarf_add_members(info, die, unit, &type);
675 char *new_ns = ns == nullptr ? xbt_strdup(type.name.c_str())
676 : bprintf("%s::%s", ns, name);
677 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
682 return std::move(type);
685 static void MC_dwarf_handle_type_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
686 Dwarf_Die * unit, simgrid::mc::Frame* frame,
689 simgrid::mc::Type type = MC_dwarf_die_to_type(info, die, unit, frame, ns);
690 auto& t = (info->types[type.id] = std::move(type));
691 if (!t.name.empty() && type.byte_size != 0)
692 info->full_types_by_name[t.name] = &t;
695 static int mc_anonymous_variable_index = 0;
697 static std::unique_ptr<simgrid::mc::Variable> MC_die_to_variable(
698 simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
699 Dwarf_Die * unit, simgrid::mc::Frame* frame,
702 // Skip declarations:
703 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
706 // Skip compile time constants:
707 if (dwarf_hasattr(die, DW_AT_const_value))
710 Dwarf_Attribute attr_location;
711 if (dwarf_attr(die, DW_AT_location, &attr_location) == nullptr)
712 // No location: do not add it ?
715 std::unique_ptr<simgrid::mc::Variable> variable =
716 std::unique_ptr<simgrid::mc::Variable>(new simgrid::mc::Variable());
717 variable->dwarf_offset = dwarf_dieoffset(die);
718 variable->global = frame == nullptr; // Can be override base on DW_AT_location
719 variable->object_info = info;
721 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
723 variable->name = name;
724 variable->type_id = MC_dwarf_at_type(die);
726 int form = dwarf_whatform(&attr_location);
727 simgrid::dwarf::FormClass form_class;
728 if (form == DW_FORM_sec_offset)
729 form_class = simgrid::dwarf::FormClass::Constant;
731 form_class = simgrid::dwarf::classify_form(form);
732 switch (form_class) {
733 case simgrid::dwarf::FormClass::ExprLoc:
734 case simgrid::dwarf::FormClass::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 variable->location_list = {
754 simgrid::dwarf::DwarfExpression(expr, expr + len) };
759 case simgrid::dwarf::FormClass::LocListPtr:
760 case simgrid::dwarf::FormClass::Constant:
761 // Reference to location list:
762 variable->location_list = simgrid::dwarf::location_list(
763 *info, attr_location);
767 xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location "
768 "in <%" PRIx64 ">%s",
769 form, form, (int) form_class, (int) form_class,
770 (uint64_t) variable->dwarf_offset,
771 variable->name.c_str());
774 // Handle start_scope:
775 if (dwarf_hasattr(die, DW_AT_start_scope)) {
776 Dwarf_Attribute attr;
777 dwarf_attr(die, DW_AT_start_scope, &attr);
778 int form = dwarf_whatform(&attr);
779 simgrid::dwarf::FormClass form_class = simgrid::dwarf::classify_form(form);
780 switch (form_class) {
781 case simgrid::dwarf::FormClass::Constant:
784 variable->start_scope =
785 dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
789 case simgrid::dwarf::FormClass::RangeListPtr: // TODO
792 ("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
793 form, (int) form_class, name == nullptr ? "?" : 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(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
811 Dwarf_Die * unit, simgrid::mc::Frame* frame,
814 std::unique_ptr<simgrid::mc::Variable> variable =
815 MC_die_to_variable(info, die, unit, frame, ns);
818 // Those arrays are sorted later:
819 else if (variable->global)
820 info->global_variables.push_back(std::move(*variable));
821 else if (frame != nullptr)
822 frame->variables.push_back(std::move(*variable));
824 xbt_die("No frame for this local variable");
827 static void MC_dwarf_handle_scope_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
828 Dwarf_Die * unit, simgrid::mc::Frame* parent_frame,
831 // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
832 int tag = dwarf_tag(die);
833 simgrid::dwarf::TagClass klass = simgrid::dwarf::classify_tag(tag);
835 // (Template) Subprogram declaration:
836 if (klass == simgrid::dwarf::TagClass::Subprogram
837 && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
840 if (klass == simgrid::dwarf::TagClass::Scope)
841 xbt_assert(parent_frame, "No parent scope for this scope");
843 simgrid::mc::Frame frame;
845 frame.id = dwarf_dieoffset(die);
846 frame.object_info = info;
848 if (klass == simgrid::dwarf::TagClass::Subprogram) {
849 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
851 frame.name = std::string(ns) + "::" + name;
856 frame.abstract_origin_id =
857 MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
859 // This is the base address for DWARF addresses.
860 // Relocated addresses are offset from this base address.
861 // See DWARF4 spec 7.5
862 std::uint64_t base = (std::uint64_t) info->base_address();
864 // TODO, support DW_AT_ranges
865 uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
866 frame.range.begin() = low_pc ? (std::uint64_t) base + low_pc : 0;
869 Dwarf_Attribute attr;
870 if (!dwarf_attr_integrate(die, DW_AT_high_pc, &attr)) {
871 xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
877 switch (simgrid::dwarf::classify_form(dwarf_whatform(&attr))) {
879 // DW_AT_high_pc if an offset from the low_pc:
880 case simgrid::dwarf::FormClass::Constant:
882 if (dwarf_formsdata(&attr, &offset) != 0)
883 xbt_die("Could not read constant");
884 frame.range.end() = frame.range.begin() + offset;
887 // DW_AT_high_pc is a relocatable address:
888 case simgrid::dwarf::FormClass::Address:
889 if (dwarf_formaddr(&attr, &high_pc) != 0)
890 xbt_die("Could not read address");
891 frame.range.begin() = base + high_pc;
895 xbt_die("Unexpected class for DW_AT_high_pc");
900 if (klass == simgrid::dwarf::TagClass::Subprogram) {
901 Dwarf_Attribute attr_frame_base;
902 if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
903 frame.frame_base_location = simgrid::dwarf::location_list(*info,
908 MC_dwarf_handle_children(info, die, unit, &frame, ns);
910 // We sort them in order to have an (somewhat) efficient by name
912 std::sort(frame.variables.begin(), frame.variables.end(),
913 MC_compare_variable);
916 if (klass == simgrid::dwarf::TagClass::Subprogram)
917 info->subprograms[frame.id] = std::move(frame);
918 else if (klass == simgrid::dwarf::TagClass::Scope)
919 parent_frame->scopes.push_back(std::move(frame));
922 static void mc_dwarf_handle_namespace_die(simgrid::mc::ObjectInformation* info,
923 Dwarf_Die * die, Dwarf_Die * unit,
924 simgrid::mc::Frame* frame,
927 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
929 xbt_die("Unexpected namespace in a subprogram");
930 char *new_ns = ns == nullptr ? xbt_strdup(name)
931 : bprintf("%s::%s", ns, name);
932 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
936 static void MC_dwarf_handle_children(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
937 Dwarf_Die * unit, simgrid::mc::Frame* frame,
940 // For each child DIE:
943 for (res = dwarf_child(die, &child); res == 0;
944 res = dwarf_siblingof(&child, &child)) {
945 MC_dwarf_handle_die(info, &child, unit, frame, ns);
949 static void MC_dwarf_handle_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
950 Dwarf_Die * unit, simgrid::mc::Frame* frame,
953 int tag = dwarf_tag(die);
954 simgrid::dwarf::TagClass klass = simgrid::dwarf::classify_tag(tag);
958 case simgrid::dwarf::TagClass::Type:
959 MC_dwarf_handle_type_die(info, die, unit, frame, ns);
962 // Subprogram or scope:
963 case simgrid::dwarf::TagClass::Subprogram:
964 case simgrid::dwarf::TagClass::Scope:
965 MC_dwarf_handle_scope_die(info, die, unit, frame, ns);
969 case simgrid::dwarf::TagClass::Variable:
970 MC_dwarf_handle_variable_die(info, die, unit, frame, ns);
973 case simgrid::dwarf::TagClass::Namespace:
974 mc_dwarf_handle_namespace_die(info, die, unit, frame, ns);
984 Elf64_Half MC_dwarf_elf_type(Dwarf* dwarf)
986 Elf* elf = dwarf_getelf(dwarf);
987 Elf64_Ehdr* ehdr64 = elf64_getehdr(elf);
989 return ehdr64->e_type;
990 Elf32_Ehdr* ehdr32 = elf32_getehdr(elf);
992 return ehdr32->e_type;
993 xbt_die("Could not get ELF heeader");
996 /** \brief Populate the debugging informations of the given ELF object
998 * Read the DWARf information of the EFFL object and populate the
999 * lists of types, variables, functions.
1002 void MC_dwarf_get_variables(simgrid::mc::ObjectInformation* info)
1004 int fd = open(info->file_name.c_str(), O_RDONLY);
1006 xbt_die("Could not open file %s", info->file_name.c_str());
1007 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
1008 if (dwarf == nullptr)
1009 xbt_die("Missing debugging information in %s\n"
1010 "Your program and its dependencies must have debugging information.\n"
1011 "You might want to recompile with -g or install the suitable debugging package.\n",
1012 info->file_name.c_str());
1014 Elf64_Half elf_type = MC_dwarf_elf_type(dwarf);
1015 if (elf_type == ET_EXEC)
1016 info->flags |= simgrid::mc::ObjectInformation::Executable;
1018 // For each compilation unit:
1019 Dwarf_Off offset = 0;
1020 Dwarf_Off next_offset = 0;
1023 while (dwarf_nextcu(dwarf, offset, &next_offset, &length, nullptr, NULL, NULL) ==
1026 if (dwarf_offdie(dwarf, offset + length, &unit_die) != nullptr)
1027 MC_dwarf_handle_children(info, &unit_die, &unit_die, nullptr, NULL);
1028 offset = next_offset;
1035 // ***** Functions index
1037 static int MC_compare_frame_index_items(simgrid::mc::FunctionIndexEntry* a,
1038 simgrid::mc::FunctionIndexEntry* b)
1040 if (a->low_pc < b->low_pc)
1042 else if (a->low_pc == b->low_pc)
1048 static void MC_make_functions_index(simgrid::mc::ObjectInformation* info)
1050 info->functions_index.clear();
1052 for (auto& e : info->subprograms) {
1053 if (e.second.range.begin() == 0)
1055 simgrid::mc::FunctionIndexEntry entry;
1056 entry.low_pc = (void*) e.second.range.begin();
1057 entry.function = &e.second;
1058 info->functions_index.push_back(entry);
1061 info->functions_index.shrink_to_fit();
1063 // Sort the array by low_pc:
1064 std::sort(info->functions_index.begin(), info->functions_index.end(),
1065 [](simgrid::mc::FunctionIndexEntry const& a,
1066 simgrid::mc::FunctionIndexEntry const& b)
1068 return a.low_pc < b.low_pc;
1072 static void MC_post_process_variables(simgrid::mc::ObjectInformation* info)
1074 // Someone needs this to be sorted but who?
1075 std::sort(info->global_variables.begin(), info->global_variables.end(),
1076 MC_compare_variable);
1078 for(simgrid::mc::Variable& variable : info->global_variables)
1079 if (variable.type_id)
1080 variable.type = simgrid::util::find_map_ptr(
1081 info->types, variable.type_id);
1084 static void mc_post_process_scope(simgrid::mc::ObjectInformation* info, simgrid::mc::Frame* 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 (std::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 variable.type = simgrid::util::find_map_ptr(
1100 info->types, variable.type_id);
1102 // Recursive post-processing of nested-scopes:
1103 for (simgrid::mc::Frame& nested_scope : scope->scopes)
1104 mc_post_process_scope(info, &nested_scope);
1109 simgrid::mc::Type* MC_resolve_type(
1110 simgrid::mc::ObjectInformation* info, unsigned type_id)
1114 simgrid::mc::Type* type = simgrid::util::find_map_ptr(info->types, type_id);
1115 if (type == nullptr)
1118 // We already have the information on the type:
1119 if (type->byte_size != 0)
1122 // Don't have a name, we can't find a more complete version:
1123 if (type->name.empty())
1126 // Try to find a more complete description of the type:
1127 // We need to fix in order to support C++.
1128 simgrid::mc::Type** subtype = simgrid::util::find_map_ptr(
1129 info->full_types_by_name, type->name);
1135 static void MC_post_process_types(simgrid::mc::ObjectInformation* info)
1137 // Lookup "subtype" field:
1138 for(auto& i : info->types) {
1139 i.second.subtype = MC_resolve_type(info, i.second.type_id);
1140 for (simgrid::mc::Member& member : i.second.members)
1141 member.type = MC_resolve_type(info, member.type_id);
1145 /** \brief Finds informations about a given shared object/executable */
1146 std::shared_ptr<simgrid::mc::ObjectInformation> MC_find_object_info(
1147 std::vector<simgrid::xbt::VmMap> const& maps, const char *name)
1149 std::shared_ptr<simgrid::mc::ObjectInformation> result =
1150 std::make_shared<simgrid::mc::ObjectInformation>();
1151 result->file_name = name;
1152 simgrid::mc::find_object_address(maps, result.get());
1153 MC_dwarf_get_variables(result.get());
1154 MC_post_process_variables(result.get());
1155 MC_post_process_types(result.get());
1156 for (auto& entry : result.get()->subprograms)
1157 mc_post_process_scope(result.get(), &entry.second);
1158 MC_make_functions_index(result.get());
1159 return std::move(result);
1162 /*************************************************************************/
1164 void MC_post_process_object_info(simgrid::mc::Process* process, simgrid::mc::ObjectInformation* info)
1166 for (auto& i : info->types) {
1168 simgrid::mc::Type* type = &(i.second);
1169 simgrid::mc::Type* subtype = type;
1170 while (subtype->type == DW_TAG_typedef
1171 || subtype->type == DW_TAG_volatile_type
1172 || subtype->type == DW_TAG_const_type)
1173 if (subtype->subtype)
1174 subtype = subtype->subtype;
1178 // Resolve full_type:
1179 if (!subtype->name.empty() && subtype->byte_size == 0) {
1180 for (auto const& object_info : process->object_infos) {
1181 auto i = object_info->full_types_by_name.find(subtype->name);
1182 if (i != object_info->full_types_by_name.end()
1183 && !i->second->name.empty() && i->second->byte_size) {
1184 type->full_type = i->second;
1188 } else type->full_type = subtype;
1196 /** Convert a DWARF register into a libunwind register
1198 * DWARF and libunwind does not use the same convention for numbering the
1199 * registers on some architectures. The function makes the necessary
1202 int dwarf_register_to_libunwind(int dwarf_register)
1204 #if defined(__x86_64__)
1205 // It seems for this arch, DWARF and libunwind agree in the numbering:
1206 return dwarf_register;
1207 #elif defined(__i386__)
1208 // Could't find the authoritative source of information for this.
1209 // This is inspired from http://source.winehq.org/source/dlls/dbghelp/cpu_i386.c#L517.
1210 switch (dwarf_register) {
1230 return UNW_X86_EFLAGS;
1260 xbt_die("Bad/unknown register number.");
1263 #error This architecture is not supported yet for DWARF expression evaluation.