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);
266 /** \brief Get the linkage name of a DIE.
268 * Use either DW_AT_linkage_name or DW_AT_MIPS_linkage_name.
269 * DW_AT_linkage_name is standardized since DWARF 4.
270 * Before this version of DWARF, the MIPS extensions
271 * DW_AT_MIPS_linkage_name is used (at least by GCC).
274 * \return linkage name of the given DIE (or nullptr)
276 static const char *MC_dwarf_at_linkage_name(Dwarf_Die * die)
278 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_linkage_name);
280 name = MC_dwarf_attr_integrate_string(die, DW_AT_MIPS_linkage_name);
284 static Dwarf_Off MC_dwarf_attr_dieoffset(Dwarf_Die * die, int attribute)
286 Dwarf_Attribute attr;
287 if (dwarf_hasattr_integrate(die, attribute) == 0)
289 dwarf_attr_integrate(die, attribute, &attr);
290 Dwarf_Die subtype_die;
291 if (dwarf_formref_die(&attr, &subtype_die) == nullptr)
292 xbt_die("Could not find DIE");
293 return dwarf_dieoffset(&subtype_die);
296 static Dwarf_Off MC_dwarf_attr_integrate_dieoffset(Dwarf_Die * die,
299 Dwarf_Attribute attr;
300 if (dwarf_hasattr_integrate(die, attribute) == 0)
302 dwarf_attr_integrate(die, DW_AT_type, &attr);
303 Dwarf_Die subtype_die;
304 if (dwarf_formref_die(&attr, &subtype_die) == nullptr)
305 xbt_die("Could not find DIE");
306 return dwarf_dieoffset(&subtype_die);
309 /** \brief Find the type/subtype (DW_AT_type) for a DIE
312 * \return DW_AT_type reference as a global offset in hexadecimal (or nullptr)
315 std::uint64_t MC_dwarf_at_type(Dwarf_Die * die)
317 return MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
320 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die * die, int attribute)
322 Dwarf_Attribute attr;
323 if (dwarf_attr_integrate(die, attribute, &attr) == nullptr)
326 if (dwarf_formaddr(&attr, &value) == 0)
327 return (uint64_t) value;
332 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die * die, int attribute,
333 uint64_t default_value)
335 Dwarf_Attribute attr;
336 if (dwarf_attr_integrate(die, attribute, &attr) == nullptr)
337 return default_value;
339 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr),
340 &value) == 0 ? (uint64_t) value : default_value;
343 static bool MC_dwarf_attr_flag(Dwarf_Die * die, int attribute, bool integrate)
345 Dwarf_Attribute attr;
346 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
347 : dwarf_attr(die, attribute, &attr)) == 0)
351 if (dwarf_formflag(&attr, &result))
352 xbt_die("Unexpected form for attribute %s",
353 simgrid::dwarf::attrname(attribute));
357 /** \brief Find the default lower bound for a given language
359 * The default lower bound of an array (when DW_TAG_lower_bound
360 * is missing) depends on the language of the compilation unit.
362 * \param lang Language of the compilation unit (values defined in the DWARF spec)
363 * \return Default lower bound of an array in this compilation unit
365 static uint64_t MC_dwarf_default_lower_bound(int lang)
371 case DW_LANG_C_plus_plus:
375 case DW_LANG_ObjC_plus_plus:
381 case DW_LANG_Fortran77:
382 case DW_LANG_Fortran90:
383 case DW_LANG_Fortran95:
384 case DW_LANG_Modula2:
385 case DW_LANG_Pascal83:
387 case DW_LANG_Cobol74:
388 case DW_LANG_Cobol85:
391 xbt_die("No default DW_TAG_lower_bound for language %i and none given",
397 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
400 * \param unit DIE of the compilation unit
401 * \return number of elements in the range
403 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
406 xbt_assert(dwarf_tag(die) == DW_TAG_enumeration_type
407 || dwarf_tag(die) == DW_TAG_subrange_type,
408 "MC_dwarf_subrange_element_count called with DIE of type %s",
409 simgrid::dwarf::tagname(die));
411 // Use DW_TAG_count if present:
412 if (dwarf_hasattr_integrate(die, DW_AT_count))
413 return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
414 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
416 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound))
417 // This is not really 0, but the code expects this (we do not know):
420 uint64_t upper_bound =
421 MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
423 uint64_t lower_bound = 0;
424 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound))
425 lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
427 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
428 return upper_bound - lower_bound + 1;
431 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
433 * The compilation unit might be needed because the default lower
434 * bound depends on the language of the compilation unit.
436 * \param die the DIE of the DW_TAG_array_type
437 * \param unit the DIE of the compilation unit
438 * \return number of elements in this array type
440 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit)
442 xbt_assert(dwarf_tag(die) == DW_TAG_array_type,
443 "MC_dwarf_array_element_count called with DIE of type %s",
444 simgrid::dwarf::tagname(die));
449 for (res = dwarf_child(die, &child); res == 0;
450 res = dwarf_siblingof(&child, &child)) {
451 int child_tag = dwarf_tag(&child);
452 if (child_tag == DW_TAG_subrange_type
453 || child_tag == DW_TAG_enumeration_type)
454 result *= MC_dwarf_subrange_element_count(&child, unit);
461 /** Sort the variable by name and address.
463 * We could use boost::container::flat_set instead.
465 static bool MC_compare_variable(
466 simgrid::mc::Variable const& a, simgrid::mc::Variable const& b)
468 int cmp = strcmp(a.name.c_str(), b.name.c_str());
474 return a.address < b.address;
477 // ***** simgrid::mc::Type*
479 /** \brief Initialize the location of a member of a type
480 * (DW_AT_data_member_location of a DW_TAG_member).
482 * \param type a type (struct, class)
483 * \param member the member of the type
484 * \param child DIE of the member (DW_TAG_member)
486 static void MC_dwarf_fill_member_location(
487 simgrid::mc::Type* type, simgrid::mc::Member* member, Dwarf_Die * child)
489 if (dwarf_hasattr(child, DW_AT_data_bit_offset))
490 xbt_die("Can't groke DW_AT_data_bit_offset.");
492 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
493 if (type->type == DW_TAG_union_type)
496 ("Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%"
497 PRIx64 ">%s", member->name.c_str(),
498 (uint64_t) type->id, type->name.c_str());
501 Dwarf_Attribute attr;
502 dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
503 int form = dwarf_whatform(&attr);
504 simgrid::dwarf::FormClass form_class = simgrid::dwarf::classify_form(form);
505 switch (form_class) {
506 case simgrid::dwarf::FormClass::ExprLoc:
507 case simgrid::dwarf::FormClass::Block:
508 // Location expression:
512 if (dwarf_getlocation(&attr, &expr, &len))
514 ("Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%"
515 PRIx64 ">%s", MC_dwarf_attr_integrate_string(child, DW_AT_name),
516 (uint64_t) type->id, type->name.c_str());
517 member->location_expression = simgrid::dwarf::DwarfExpression(expr, expr+len);
520 case simgrid::dwarf::FormClass::Constant:
521 // Offset from the base address of the object:
524 if (!dwarf_formudata(&attr, &offset))
525 member->offset(offset);
527 xbt_die("Cannot get %s location <%" PRIx64 ">%s",
528 MC_dwarf_attr_integrate_string(child, DW_AT_name),
529 (uint64_t) type->id, type->name.c_str());
532 case simgrid::dwarf::FormClass::LocListPtr:
533 // Reference to a location list:
535 case simgrid::dwarf::FormClass::Reference:
536 // It's supposed to be possible in DWARF2 but I couldn't find its semantic
539 xbt_die("Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
540 (int) form_class, form);
545 /** \brief Populate the list of members of a type
547 * \param info ELF object containing the type DIE
548 * \param die DIE of the type
549 * \param unit DIE of the compilation unit containing the type DIE
550 * \param type the type
552 static void MC_dwarf_add_members(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
553 Dwarf_Die * unit, simgrid::mc::Type* type)
557 xbt_assert(type->members.empty());
558 for (res = dwarf_child(die, &child); res == 0;
559 res = dwarf_siblingof(&child, &child)) {
560 int tag = dwarf_tag(&child);
561 if (tag == DW_TAG_member || tag == DW_TAG_inheritance) {
563 // Skip declarations:
564 if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
567 // Skip compile time constants:
568 if (dwarf_hasattr(&child, DW_AT_const_value))
571 // TODO, we should use another type (because is is not a type but a member)
572 simgrid::mc::Member member;
573 member.inheritance = tag == DW_TAG_inheritance;
575 const char *name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
579 MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
580 member.type_id = MC_dwarf_at_type(&child);
582 if (dwarf_hasattr(&child, DW_AT_data_bit_offset))
583 xbt_die("Can't groke DW_AT_data_bit_offset.");
585 MC_dwarf_fill_member_location(type, &member, &child);
588 xbt_die("Missing type for member %s of <%" PRIx64 ">%s",
590 (uint64_t) type->id, type->name.c_str());
592 type->members.push_back(std::move(member));
597 /** \brief Create a MC type object from a DIE
599 * \param info current object info object
600 * \param DIE (for a given type);
601 * \param unit compilation unit of the current DIE
602 * \return MC representation of the type
604 static simgrid::mc::Type MC_dwarf_die_to_type(
605 simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
606 Dwarf_Die * unit, simgrid::mc::Frame* frame,
609 simgrid::mc::Type type;
610 type.type = dwarf_tag(die);
611 type.name = std::string();
612 type.element_count = -1;
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);
633 if (name != nullptr) {
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;
660 case DW_TAG_array_type:
661 type.element_count = MC_dwarf_array_element_count(die, unit);
662 // TODO, handle DW_byte_stride and (not) DW_bit_stride
665 case DW_TAG_pointer_type:
666 case DW_TAG_reference_type:
667 case DW_TAG_rvalue_reference_type:
670 case DW_TAG_structure_type:
671 case DW_TAG_union_type:
672 case DW_TAG_class_type:
673 MC_dwarf_add_members(info, die, unit, &type);
674 char *new_ns = ns == nullptr ? xbt_strdup(type.name.c_str())
675 : bprintf("%s::%s", ns, name);
676 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
681 return std::move(type);
684 static void MC_dwarf_handle_type_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
685 Dwarf_Die * unit, simgrid::mc::Frame* frame,
688 simgrid::mc::Type type = MC_dwarf_die_to_type(info, die, unit, frame, ns);
689 auto& t = (info->types[type.id] = std::move(type));
690 if (!t.name.empty() && type.byte_size != 0)
691 info->full_types_by_name[t.name] = &t;
694 static int mc_anonymous_variable_index = 0;
696 static std::unique_ptr<simgrid::mc::Variable> MC_die_to_variable(
697 simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
698 Dwarf_Die * unit, simgrid::mc::Frame* frame,
701 // Skip declarations:
702 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
705 // Skip compile time constants:
706 if (dwarf_hasattr(die, DW_AT_const_value))
709 Dwarf_Attribute attr_location;
710 if (dwarf_attr(die, DW_AT_location, &attr_location) == nullptr)
711 // No location: do not add it ?
714 std::unique_ptr<simgrid::mc::Variable> variable =
715 std::unique_ptr<simgrid::mc::Variable>(new simgrid::mc::Variable());
716 variable->dwarf_offset = dwarf_dieoffset(die);
717 variable->global = frame == nullptr; // Can be override base on DW_AT_location
718 variable->object_info = info;
720 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
722 variable->name = name;
723 variable->type_id = MC_dwarf_at_type(die);
725 int form = dwarf_whatform(&attr_location);
726 simgrid::dwarf::FormClass form_class;
727 if (form == DW_FORM_sec_offset)
728 form_class = simgrid::dwarf::FormClass::Constant;
730 form_class = simgrid::dwarf::classify_form(form);
731 switch (form_class) {
732 case simgrid::dwarf::FormClass::ExprLoc:
733 case simgrid::dwarf::FormClass::Block:
734 // Location expression:
738 if (dwarf_getlocation(&attr_location, &expr, &len)) {
740 "Could not read location expression in DW_AT_location "
741 "of variable <%" PRIx64 ">%s",
742 (uint64_t) variable->dwarf_offset,
743 variable->name.c_str());
746 if (len == 1 && expr[0].atom == DW_OP_addr) {
747 variable->global = 1;
748 uintptr_t offset = (uintptr_t) expr[0].number;
749 uintptr_t base = (uintptr_t) info->base_address();
750 variable->address = (void *) (base + offset);
752 variable->location_list = {
753 simgrid::dwarf::DwarfExpression(expr, expr + len) };
758 case simgrid::dwarf::FormClass::LocListPtr:
759 case simgrid::dwarf::FormClass::Constant:
760 // Reference to location list:
761 variable->location_list = simgrid::dwarf::location_list(
762 *info, attr_location);
766 xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location "
767 "in <%" PRIx64 ">%s",
768 form, form, (int) form_class, (int) form_class,
769 (uint64_t) variable->dwarf_offset,
770 variable->name.c_str());
773 // Handle start_scope:
774 if (dwarf_hasattr(die, DW_AT_start_scope)) {
775 Dwarf_Attribute attr;
776 dwarf_attr(die, DW_AT_start_scope, &attr);
777 int form = dwarf_whatform(&attr);
778 simgrid::dwarf::FormClass form_class = simgrid::dwarf::classify_form(form);
779 switch (form_class) {
780 case simgrid::dwarf::FormClass::Constant:
783 variable->start_scope =
784 dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
788 case simgrid::dwarf::FormClass::RangeListPtr: // TODO
791 ("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
792 form, (int) form_class, name == nullptr ? "?" : name);
796 if (ns && variable->global)
798 std::string(ns) + "::" + variable->name;
800 // The current code needs a variable name,
801 // generate a fake one:
802 if (variable->name.empty())
804 "@anonymous#" + std::to_string(mc_anonymous_variable_index++);
806 return std::move(variable);
809 static void MC_dwarf_handle_variable_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
810 Dwarf_Die * unit, simgrid::mc::Frame* frame,
813 std::unique_ptr<simgrid::mc::Variable> variable =
814 MC_die_to_variable(info, die, unit, frame, ns);
817 // Those arrays are sorted later:
818 else if (variable->global)
819 info->global_variables.push_back(std::move(*variable));
820 else if (frame != nullptr)
821 frame->variables.push_back(std::move(*variable));
823 xbt_die("No frame for this local variable");
826 static void MC_dwarf_handle_scope_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
827 Dwarf_Die * unit, simgrid::mc::Frame* parent_frame,
830 // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
831 int tag = dwarf_tag(die);
832 simgrid::dwarf::TagClass klass = simgrid::dwarf::classify_tag(tag);
834 // (Template) Subprogram declaration:
835 if (klass == simgrid::dwarf::TagClass::Subprogram
836 && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
839 if (klass == simgrid::dwarf::TagClass::Scope)
840 xbt_assert(parent_frame, "No parent scope for this scope");
842 simgrid::mc::Frame frame;
844 frame.id = dwarf_dieoffset(die);
845 frame.object_info = info;
847 if (klass == simgrid::dwarf::TagClass::Subprogram) {
848 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
850 frame.name = std::string(ns) + "::" + name;
855 frame.abstract_origin_id =
856 MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
858 // This is the base address for DWARF addresses.
859 // Relocated addresses are offset from this base address.
860 // See DWARF4 spec 7.5
861 std::uint64_t base = (std::uint64_t) info->base_address();
863 // TODO, support DW_AT_ranges
864 uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
865 frame.range.begin() = low_pc ? (std::uint64_t) base + low_pc : 0;
868 Dwarf_Attribute attr;
869 if (!dwarf_attr_integrate(die, DW_AT_high_pc, &attr))
870 xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
875 switch (simgrid::dwarf::classify_form(dwarf_whatform(&attr))) {
877 // DW_AT_high_pc if an offset from the low_pc:
878 case simgrid::dwarf::FormClass::Constant:
880 if (dwarf_formsdata(&attr, &offset) != 0)
881 xbt_die("Could not read constant");
882 frame.range.end() = frame.range.begin() + offset;
885 // DW_AT_high_pc is a relocatable address:
886 case simgrid::dwarf::FormClass::Address:
887 if (dwarf_formaddr(&attr, &high_pc) != 0)
888 xbt_die("Could not read address");
889 frame.range.begin() = base + high_pc;
893 xbt_die("Unexpected class for DW_AT_high_pc");
898 if (klass == simgrid::dwarf::TagClass::Subprogram) {
899 Dwarf_Attribute attr_frame_base;
900 if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
901 frame.frame_base_location = simgrid::dwarf::location_list(*info,
906 MC_dwarf_handle_children(info, die, unit, &frame, ns);
908 // We sort them in order to have an (somewhat) efficient by name
910 std::sort(frame.variables.begin(), frame.variables.end(),
911 MC_compare_variable);
914 if (klass == simgrid::dwarf::TagClass::Subprogram)
915 info->subprograms[frame.id] = std::move(frame);
916 else if (klass == simgrid::dwarf::TagClass::Scope)
917 parent_frame->scopes.push_back(std::move(frame));
920 static void mc_dwarf_handle_namespace_die(simgrid::mc::ObjectInformation* info,
921 Dwarf_Die * die, Dwarf_Die * unit,
922 simgrid::mc::Frame* frame,
925 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
927 xbt_die("Unexpected namespace in a subprogram");
928 char *new_ns = ns == nullptr ? xbt_strdup(name)
929 : bprintf("%s::%s", ns, name);
930 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
934 static void MC_dwarf_handle_children(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
935 Dwarf_Die * unit, simgrid::mc::Frame* frame,
938 // For each child DIE:
941 for (res = dwarf_child(die, &child); res == 0;
942 res = dwarf_siblingof(&child, &child))
943 MC_dwarf_handle_die(info, &child, unit, frame, ns);
946 static void MC_dwarf_handle_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
947 Dwarf_Die * unit, simgrid::mc::Frame* frame,
950 int tag = dwarf_tag(die);
951 simgrid::dwarf::TagClass klass = simgrid::dwarf::classify_tag(tag);
955 case simgrid::dwarf::TagClass::Type:
956 MC_dwarf_handle_type_die(info, die, unit, frame, ns);
959 // Subprogram or scope:
960 case simgrid::dwarf::TagClass::Subprogram:
961 case simgrid::dwarf::TagClass::Scope:
962 MC_dwarf_handle_scope_die(info, die, unit, frame, ns);
966 case simgrid::dwarf::TagClass::Variable:
967 MC_dwarf_handle_variable_die(info, die, unit, frame, ns);
970 case simgrid::dwarf::TagClass::Namespace:
971 mc_dwarf_handle_namespace_die(info, die, unit, frame, ns);
981 Elf64_Half get_type(Elf* elf)
983 Elf64_Ehdr* ehdr64 = elf64_getehdr(elf);
985 return ehdr64->e_type;
986 Elf32_Ehdr* ehdr32 = elf32_getehdr(elf);
988 return ehdr32->e_type;
989 xbt_die("Could not get ELF heeader");
993 void read_dwarf_info(simgrid::mc::ObjectInformation* info, Dwarf* dwarf)
995 // For each compilation unit:
996 Dwarf_Off offset = 0;
997 Dwarf_Off next_offset = 0;
1000 while (dwarf_nextcu(dwarf, offset, &next_offset, &length, nullptr, NULL, NULL) ==
1003 if (dwarf_offdie(dwarf, offset + length, &unit_die) != nullptr)
1004 MC_dwarf_handle_children(info, &unit_die, &unit_die, nullptr, NULL);
1005 offset = next_offset;
1009 /** \brief Populate the debugging informations of the given ELF object
1011 * Read the DWARf information of the EFFL object and populate the
1012 * lists of types, variables, functions.
1015 void MC_dwarf_get_variables(simgrid::mc::ObjectInformation* info)
1017 if (elf_version(EV_CURRENT) == EV_NONE)
1018 xbt_die("libelf initialization error");
1020 int fd = open(info->file_name.c_str(), O_RDONLY);
1022 xbt_die("Could not open file %s", info->file_name.c_str());
1023 Elf* elf = elf_begin(fd, ELF_C_READ, nullptr);
1025 xbt_die("Not an ELF file 1");
1026 Elf_Kind kind = elf_kind(elf);
1027 if (kind != ELF_K_ELF)
1028 xbt_die("Not an ELF file 2");
1030 Elf64_Half type = get_type(elf);
1031 if (type == ET_EXEC)
1032 info->flags |= simgrid::mc::ObjectInformation::Executable;
1034 Dwarf* dwarf = dwarf_begin_elf (elf, DWARF_C_READ, nullptr);
1035 // Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
1036 if (dwarf != nullptr) {
1037 read_dwarf_info(info, dwarf);
1042 xbt_die("Missing debugging information in %s\n"
1043 "Your program and its dependencies must have debugging information.\n"
1044 "You might want to recompile with -g or install the suitable debugging package.\n",
1045 info->file_name.c_str());
1051 // ***** Functions index
1053 static int MC_compare_frame_index_items(simgrid::mc::FunctionIndexEntry* a,
1054 simgrid::mc::FunctionIndexEntry* b)
1056 if (a->low_pc < b->low_pc)
1058 else if (a->low_pc == b->low_pc)
1064 static void MC_make_functions_index(simgrid::mc::ObjectInformation* info)
1066 info->functions_index.clear();
1068 for (auto& e : info->subprograms) {
1069 if (e.second.range.begin() == 0)
1071 simgrid::mc::FunctionIndexEntry entry;
1072 entry.low_pc = (void*) e.second.range.begin();
1073 entry.function = &e.second;
1074 info->functions_index.push_back(entry);
1077 info->functions_index.shrink_to_fit();
1079 // Sort the array by low_pc:
1080 std::sort(info->functions_index.begin(), info->functions_index.end(),
1081 [](simgrid::mc::FunctionIndexEntry const& a,
1082 simgrid::mc::FunctionIndexEntry const& b)
1084 return a.low_pc < b.low_pc;
1088 static void MC_post_process_variables(simgrid::mc::ObjectInformation* info)
1090 // Someone needs this to be sorted but who?
1091 std::sort(info->global_variables.begin(), info->global_variables.end(),
1092 MC_compare_variable);
1094 for(simgrid::mc::Variable& variable : info->global_variables)
1095 if (variable.type_id)
1096 variable.type = simgrid::util::find_map_ptr(
1097 info->types, variable.type_id);
1100 static void mc_post_process_scope(simgrid::mc::ObjectInformation* info, simgrid::mc::Frame* scope)
1103 if (scope->tag == DW_TAG_inlined_subroutine) {
1104 // Attach correct namespaced name in inlined subroutine:
1105 auto i = info->subprograms.find(scope->abstract_origin_id);
1106 xbt_assert(i != info->subprograms.end(),
1107 "Could not lookup abstract origin %" PRIx64,
1108 (std::uint64_t) scope->abstract_origin_id);
1109 scope->name = i->second.name;
1113 for (simgrid::mc::Variable& variable : scope->variables)
1114 if (variable.type_id)
1115 variable.type = simgrid::util::find_map_ptr(
1116 info->types, variable.type_id);
1118 // Recursive post-processing of nested-scopes:
1119 for (simgrid::mc::Frame& nested_scope : scope->scopes)
1120 mc_post_process_scope(info, &nested_scope);
1125 simgrid::mc::Type* MC_resolve_type(
1126 simgrid::mc::ObjectInformation* info, unsigned type_id)
1130 simgrid::mc::Type* type = simgrid::util::find_map_ptr(info->types, type_id);
1131 if (type == nullptr)
1134 // We already have the information on the type:
1135 if (type->byte_size != 0)
1138 // Don't have a name, we can't find a more complete version:
1139 if (type->name.empty())
1142 // Try to find a more complete description of the type:
1143 // We need to fix in order to support C++.
1144 simgrid::mc::Type** subtype = simgrid::util::find_map_ptr(
1145 info->full_types_by_name, type->name);
1151 static void MC_post_process_types(simgrid::mc::ObjectInformation* info)
1153 // Lookup "subtype" field:
1154 for(auto& i : info->types) {
1155 i.second.subtype = MC_resolve_type(info, i.second.type_id);
1156 for (simgrid::mc::Member& member : i.second.members)
1157 member.type = MC_resolve_type(info, member.type_id);
1161 /** \brief Finds informations about a given shared object/executable */
1162 std::shared_ptr<simgrid::mc::ObjectInformation> MC_find_object_info(
1163 std::vector<simgrid::xbt::VmMap> const& maps, const char *name)
1165 std::shared_ptr<simgrid::mc::ObjectInformation> result =
1166 std::make_shared<simgrid::mc::ObjectInformation>();
1167 result->file_name = name;
1168 simgrid::mc::find_object_address(maps, result.get());
1169 MC_dwarf_get_variables(result.get());
1170 MC_post_process_variables(result.get());
1171 MC_post_process_types(result.get());
1172 for (auto& entry : result.get()->subprograms)
1173 mc_post_process_scope(result.get(), &entry.second);
1174 MC_make_functions_index(result.get());
1175 return std::move(result);
1178 /*************************************************************************/
1180 void MC_post_process_object_info(simgrid::mc::Process* process, simgrid::mc::ObjectInformation* info)
1182 for (auto& i : info->types) {
1184 simgrid::mc::Type* type = &(i.second);
1185 simgrid::mc::Type* subtype = type;
1186 while (subtype->type == DW_TAG_typedef
1187 || subtype->type == DW_TAG_volatile_type
1188 || subtype->type == DW_TAG_const_type)
1189 if (subtype->subtype)
1190 subtype = subtype->subtype;
1194 // Resolve full_type:
1195 if (!subtype->name.empty() && subtype->byte_size == 0)
1196 for (auto const& object_info : process->object_infos) {
1197 auto i = object_info->full_types_by_name.find(subtype->name);
1198 if (i != object_info->full_types_by_name.end()
1199 && !i->second->name.empty() && i->second->byte_size) {
1200 type->full_type = i->second;
1204 else type->full_type = subtype;
1212 /** Convert a DWARF register into a libunwind register
1214 * DWARF and libunwind does not use the same convention for numbering the
1215 * registers on some architectures. The function makes the necessary
1218 int dwarf_register_to_libunwind(int dwarf_register)
1220 #if defined(__x86_64__)
1221 // It seems for this arch, DWARF and libunwind agree in the numbering:
1222 return dwarf_register;
1223 #elif defined(__i386__)
1224 // Could't find the authoritative source of information for this.
1225 // This is inspired from http://source.winehq.org/source/dlls/dbghelp/cpu_i386.c#L517.
1226 switch (dwarf_register) {
1246 return UNW_X86_EFLAGS;
1276 xbt_die("Bad/unknown register number.");
1279 #error This architecture is not supported yet for DWARF expression evaluation.