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 <simgrid/util.hpp>
21 #include <xbt/sysdep.h>
23 #include <simgrid/util.hpp>
25 #include "mc_object_info.h"
26 #include "mc_private.h"
27 #include "mc_dwarf.hpp"
29 #include "mc/Process.hpp"
30 #include "mc/ObjectInformation.hpp"
31 #include "mc/Variable.hpp"
33 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing");
35 /** \brief The default DW_TAG_lower_bound for a given DW_AT_language.
37 * The default for a given language is defined in the DWARF spec.
39 * \param language consant as defined by the DWARf spec
41 static uint64_t MC_dwarf_default_lower_bound(int lang);
43 /** \brief Computes the the element_count of a DW_TAG_enumeration_type DIE
45 * This is the number of elements in a given array dimension.
47 * A reference of the compilation unit (DW_TAG_compile_unit) is
48 * needed because the default lower bound (when there is no DW_AT_lower_bound)
49 * depends of the language of the compilation unit (DW_AT_language).
51 * \param die DIE for the DW_TAG_enumeration_type or DW_TAG_subrange_type
52 * \param unit DIE of the DW_TAG_compile_unit
54 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
57 /** \brief Computes the number of elements of a given DW_TAG_array_type.
59 * \param die DIE for the DW_TAG_array_type
61 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit);
63 /** \brief Process a DIE
65 * \param info the resulting object fot the library/binary file (output)
66 * \param die the current DIE
67 * \param unit the DIE of the compile unit of the current DIE
68 * \param frame containg frame if any
70 static void MC_dwarf_handle_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
71 Dwarf_Die * unit, simgrid::mc::Frame* frame,
74 /** \brief Process a type DIE
76 static void MC_dwarf_handle_type_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
77 Dwarf_Die * unit, simgrid::mc::Frame* frame,
80 /** \brief Calls MC_dwarf_handle_die on all childrend of the given die
82 * \param info the resulting object fot the library/binary file (output)
83 * \param die the current DIE
84 * \param unit the DIE of the compile unit of the current DIE
85 * \param frame containg frame if any
87 static void MC_dwarf_handle_children(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
88 Dwarf_Die * unit, simgrid::mc::Frame* frame,
91 /** \brief Handle a variable (DW_TAG_variable or other)
93 * \param info the resulting object fot the library/binary file (output)
94 * \param die the current DIE
95 * \param unit the DIE of the compile unit of the current DIE
96 * \param frame containg frame if any
98 static void MC_dwarf_handle_variable_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
99 Dwarf_Die * unit, simgrid::mc::Frame* frame,
102 /** \brief Get the DW_TAG_type of the DIE
105 * \return DW_TAG_type attribute as a new string (NULL if none)
107 static std::uint64_t MC_dwarf_at_type(Dwarf_Die * die);
112 enum class TagClass {
121 /*** Class of forms defined in the DWARF standard */
122 enum class FormClass {
124 Address, // Location in the program's address space
125 Block, // Arbitrary block of bytes
128 Flag, // Boolean value
129 Reference, // Reference to another DIE
130 ExprLoc, // DWARF expression/location description
138 TagClass classify_tag(int tag)
142 case DW_TAG_array_type:
143 case DW_TAG_class_type:
144 case DW_TAG_enumeration_type:
146 case DW_TAG_pointer_type:
147 case DW_TAG_reference_type:
148 case DW_TAG_rvalue_reference_type:
149 case DW_TAG_string_type:
150 case DW_TAG_structure_type:
151 case DW_TAG_subroutine_type:
152 case DW_TAG_union_type:
153 case DW_TAG_ptr_to_member_type:
154 case DW_TAG_set_type:
155 case DW_TAG_subrange_type:
156 case DW_TAG_base_type:
157 case DW_TAG_const_type:
158 case DW_TAG_file_type:
159 case DW_TAG_packed_type:
160 case DW_TAG_volatile_type:
161 case DW_TAG_restrict_type:
162 case DW_TAG_interface_type:
163 case DW_TAG_unspecified_type:
164 case DW_TAG_shared_type:
165 return TagClass::Type;
167 case DW_TAG_subprogram:
168 return TagClass::Subprogram;
170 case DW_TAG_variable:
171 case DW_TAG_formal_parameter:
172 return TagClass::Variable;
174 case DW_TAG_lexical_block:
175 case DW_TAG_try_block:
176 case DW_TAG_catch_block:
177 case DW_TAG_inlined_subroutine:
178 case DW_TAG_with_stmt:
179 return TagClass::Scope;
181 case DW_TAG_namespace:
182 return TagClass::Namespace;
185 return TagClass::Unknown;
189 /** \brief Find the DWARF data class for a given DWARF data form
191 * This mapping is defined in the DWARF spec.
193 * \param form The form (values taken from the DWARF spec)
194 * \return An internal representation for the corresponding class
197 FormClass classify_form(int form)
201 return FormClass::Address;
206 return FormClass::Block;
213 return FormClass::Constant;
216 return FormClass::String;
217 case DW_FORM_ref_addr:
222 case DW_FORM_ref_udata:
223 return FormClass::Reference;
225 case DW_FORM_flag_present:
226 return FormClass::Flag;
227 case DW_FORM_exprloc:
228 return FormClass::ExprLoc;
232 return FormClass::Unknown;
236 /** \brief Get the name of the tag of a given DIE
239 * \return name of the tag of this DIE
242 const char *tagname(Dwarf_Die * die)
244 return simgrid::dwarf::tagname(dwarf_tag(die));
252 /** \brief Get an attribute of a given DIE as a string
255 * \param attribute attribute
256 * \return value of the given attribute of the given DIE
258 static const char *MC_dwarf_attr_integrate_string(Dwarf_Die * die,
261 Dwarf_Attribute attr;
262 if (!dwarf_attr_integrate(die, attribute, &attr)) {
265 return dwarf_formstring(&attr);
269 /** \brief Get the linkage name of a DIE.
271 * Use either DW_AT_linkage_name or DW_AT_MIPS_linkage_name.
272 * DW_AT_linkage_name is standardized since DWARF 4.
273 * Before this version of DWARF, the MIPS extensions
274 * DW_AT_MIPS_linkage_name is used (at least by GCC).
277 * \return linkage name of the given DIE (or NULL)
279 static const char *MC_dwarf_at_linkage_name(Dwarf_Die * die)
281 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_linkage_name);
283 name = MC_dwarf_attr_integrate_string(die, DW_AT_MIPS_linkage_name);
287 static Dwarf_Off MC_dwarf_attr_dieoffset(Dwarf_Die * die, int attribute)
289 Dwarf_Attribute attr;
290 if (dwarf_hasattr_integrate(die, attribute) == 0)
292 dwarf_attr_integrate(die, attribute, &attr);
293 Dwarf_Die subtype_die;
294 if (dwarf_formref_die(&attr, &subtype_die) == NULL)
295 xbt_die("Could not find DIE");
296 return dwarf_dieoffset(&subtype_die);
299 static Dwarf_Off MC_dwarf_attr_integrate_dieoffset(Dwarf_Die * die,
302 Dwarf_Attribute attr;
303 if (dwarf_hasattr_integrate(die, attribute) == 0)
305 dwarf_attr_integrate(die, DW_AT_type, &attr);
306 Dwarf_Die subtype_die;
307 if (dwarf_formref_die(&attr, &subtype_die) == NULL)
308 xbt_die("Could not find DIE");
309 return dwarf_dieoffset(&subtype_die);
312 /** \brief Find the type/subtype (DW_AT_type) for a DIE
315 * \return DW_AT_type reference as a global offset in hexadecimal (or NULL)
318 std::uint64_t MC_dwarf_at_type(Dwarf_Die * die)
320 return MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
323 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die * die, int attribute)
325 Dwarf_Attribute attr;
326 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
329 if (dwarf_formaddr(&attr, &value) == 0)
330 return (uint64_t) value;
335 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die * die, int attribute,
336 uint64_t default_value)
338 Dwarf_Attribute attr;
339 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
340 return default_value;
342 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr),
343 &value) == 0 ? (uint64_t) value : default_value;
346 static bool MC_dwarf_attr_flag(Dwarf_Die * die, int attribute, bool integrate)
348 Dwarf_Attribute attr;
349 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
350 : dwarf_attr(die, attribute, &attr)) == 0)
354 if (dwarf_formflag(&attr, &result))
355 xbt_die("Unexpected form for attribute %s",
356 simgrid::dwarf::attrname(attribute));
360 /** \brief Find the default lower bound for a given language
362 * The default lower bound of an array (when DW_TAG_lower_bound
363 * is missing) depends on the language of the compilation unit.
365 * \param lang Language of the compilation unit (values defined in the DWARF spec)
366 * \return Default lower bound of an array in this compilation unit
368 static uint64_t MC_dwarf_default_lower_bound(int lang)
374 case DW_LANG_C_plus_plus:
378 case DW_LANG_ObjC_plus_plus:
384 case DW_LANG_Fortran77:
385 case DW_LANG_Fortran90:
386 case DW_LANG_Fortran95:
387 case DW_LANG_Modula2:
388 case DW_LANG_Pascal83:
390 case DW_LANG_Cobol74:
391 case DW_LANG_Cobol85:
394 xbt_die("No default DW_TAG_lower_bound for language %i and none given",
400 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
403 * \param unit DIE of the compilation unit
404 * \return number of elements in the range
406 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
409 xbt_assert(dwarf_tag(die) == DW_TAG_enumeration_type
410 || dwarf_tag(die) == DW_TAG_subrange_type,
411 "MC_dwarf_subrange_element_count called with DIE of type %s",
412 simgrid::dwarf::tagname(die));
414 // Use DW_TAG_count if present:
415 if (dwarf_hasattr_integrate(die, DW_AT_count))
416 return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
417 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
419 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound))
420 // This is not really 0, but the code expects this (we do not know):
423 uint64_t upper_bound =
424 MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
426 uint64_t lower_bound = 0;
427 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound))
428 lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
430 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
431 return upper_bound - lower_bound + 1;
434 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
436 * The compilation unit might be needed because the default lower
437 * bound depends on the language of the compilation unit.
439 * \param die the DIE of the DW_TAG_array_type
440 * \param unit the DIE of the compilation unit
441 * \return number of elements in this array type
443 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit)
445 xbt_assert(dwarf_tag(die) == DW_TAG_array_type,
446 "MC_dwarf_array_element_count called with DIE of type %s",
447 simgrid::dwarf::tagname(die));
452 for (res = dwarf_child(die, &child); res == 0;
453 res = dwarf_siblingof(&child, &child)) {
454 int child_tag = dwarf_tag(&child);
455 if (child_tag == DW_TAG_subrange_type
456 || child_tag == DW_TAG_enumeration_type)
457 result *= MC_dwarf_subrange_element_count(&child, unit);
464 /** Sort the variable by name and address.
466 * We could use boost::container::flat_set instead.
468 static bool MC_compare_variable(
469 simgrid::mc::Variable const& a, simgrid::mc::Variable const& b)
471 int cmp = strcmp(a.name.c_str(), b.name.c_str());
477 return a.address < b.address;
480 // ***** simgrid::mc::Type*
482 /** \brief Initialize the location of a member of a type
483 * (DW_AT_data_member_location of a DW_TAG_member).
485 * \param type a type (struct, class)
486 * \param member the member of the type
487 * \param child DIE of the member (DW_TAG_member)
489 static void MC_dwarf_fill_member_location(
490 simgrid::mc::Type* type, simgrid::mc::Member* member, Dwarf_Die * child)
492 if (dwarf_hasattr(child, DW_AT_data_bit_offset))
493 xbt_die("Can't groke DW_AT_data_bit_offset.");
495 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
496 if (type->type == DW_TAG_union_type)
499 ("Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%"
500 PRIx64 ">%s", member->name.c_str(),
501 (uint64_t) type->id, type->name.c_str());
504 Dwarf_Attribute attr;
505 dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
506 int form = dwarf_whatform(&attr);
507 simgrid::dwarf::FormClass form_class = simgrid::dwarf::classify_form(form);
508 switch (form_class) {
509 case simgrid::dwarf::FormClass::ExprLoc:
510 case simgrid::dwarf::FormClass::Block:
511 // Location expression:
515 if (dwarf_getlocation(&attr, &expr, &len))
517 ("Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%"
518 PRIx64 ">%s", MC_dwarf_attr_integrate_string(child, DW_AT_name),
519 (uint64_t) type->id, type->name.c_str());
520 member->location_expression = simgrid::mc::DwarfExpression(expr, expr+len);
523 case simgrid::dwarf::FormClass::Constant:
524 // Offset from the base address of the object:
527 if (!dwarf_formudata(&attr, &offset))
528 member->offset(offset);
530 xbt_die("Cannot get %s location <%" PRIx64 ">%s",
531 MC_dwarf_attr_integrate_string(child, DW_AT_name),
532 (uint64_t) type->id, type->name.c_str());
535 case simgrid::dwarf::FormClass::LocListPtr:
536 // Reference to a location list:
538 case simgrid::dwarf::FormClass::Reference:
539 // It's supposed to be possible in DWARF2 but I couldn't find its semantic
542 xbt_die("Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
543 (int) form_class, form);
548 /** \brief Populate the list of members of a type
550 * \param info ELF object containing the type DIE
551 * \param die DIE of the type
552 * \param unit DIE of the compilation unit containing the type DIE
553 * \param type the type
555 static void MC_dwarf_add_members(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
556 Dwarf_Die * unit, simgrid::mc::Type* type)
560 xbt_assert(type->members.empty());
561 for (res = dwarf_child(die, &child); res == 0;
562 res = dwarf_siblingof(&child, &child)) {
563 int tag = dwarf_tag(&child);
564 if (tag == DW_TAG_member || tag == DW_TAG_inheritance) {
566 // Skip declarations:
567 if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
570 // Skip compile time constants:
571 if (dwarf_hasattr(&child, DW_AT_const_value))
574 // TODO, we should use another type (because is is not a type but a member)
575 simgrid::mc::Member member;
576 member.inheritance = tag == DW_TAG_inheritance;
578 const char *name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
582 MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
583 member.type_id = MC_dwarf_at_type(&child);
585 if (dwarf_hasattr(&child, DW_AT_data_bit_offset))
586 xbt_die("Can't groke DW_AT_data_bit_offset.");
588 MC_dwarf_fill_member_location(type, &member, &child);
591 xbt_die("Missing type for member %s of <%" PRIx64 ">%s",
593 (uint64_t) type->id, type->name.c_str());
595 type->members.push_back(std::move(member));
600 /** \brief Create a MC type object from a DIE
602 * \param info current object info object
603 * \param DIE (for a given type);
604 * \param unit compilation unit of the current DIE
605 * \return MC representation of the type
607 static simgrid::mc::Type MC_dwarf_die_to_type(
608 simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
609 Dwarf_Die * unit, simgrid::mc::Frame* frame,
612 simgrid::mc::Type type;
613 type.type = dwarf_tag(die);
614 type.name = std::string();
615 type.element_count = -1;
618 type.id = dwarf_dieoffset(die);
620 const char *prefix = "";
622 case DW_TAG_structure_type:
625 case DW_TAG_union_type:
628 case DW_TAG_class_type:
635 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
637 char* full_name = ns ? bprintf("%s%s::%s", prefix, ns, name) :
638 bprintf("%s%s", prefix, name);
639 type.name = std::string(full_name);
643 type.type_id = MC_dwarf_at_type(die);
645 // Some compilers do not emit DW_AT_byte_size for pointer_type,
646 // so we fill this. We currently assume that the model-checked process is in
647 // the same architecture..
648 if (type.type == DW_TAG_pointer_type)
649 type.byte_size = sizeof(void*);
651 // Computation of the byte_size;
652 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
653 type.byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
654 else if (type.type == DW_TAG_array_type
655 || type.type == DW_TAG_structure_type
656 || type.type == DW_TAG_class_type) {
658 if (dwarf_aggregate_size(die, &size) == 0)
659 type.byte_size = size;
663 case DW_TAG_array_type:
664 type.element_count = MC_dwarf_array_element_count(die, unit);
665 // TODO, handle DW_byte_stride and (not) DW_bit_stride
668 case DW_TAG_pointer_type:
669 case DW_TAG_reference_type:
670 case DW_TAG_rvalue_reference_type:
671 type.is_pointer_type = 1;
674 case DW_TAG_structure_type:
675 case DW_TAG_union_type:
676 case DW_TAG_class_type:
677 MC_dwarf_add_members(info, die, unit, &type);
678 char *new_ns = ns == NULL ? xbt_strdup(type.name.c_str())
679 : bprintf("%s::%s", ns, name);
680 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
685 return std::move(type);
688 static void MC_dwarf_handle_type_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
689 Dwarf_Die * unit, simgrid::mc::Frame* frame,
692 simgrid::mc::Type type = MC_dwarf_die_to_type(info, die, unit, frame, ns);
693 auto& t = (info->types[type.id] = std::move(type));
694 if (!t.name.empty() && type.byte_size != 0)
695 info->full_types_by_name[t.name] = &t;
698 static int mc_anonymous_variable_index = 0;
700 static std::unique_ptr<simgrid::mc::Variable> MC_die_to_variable(
701 simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
702 Dwarf_Die * unit, simgrid::mc::Frame* frame,
705 // Skip declarations:
706 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
709 // Skip compile time constants:
710 if (dwarf_hasattr(die, DW_AT_const_value))
713 Dwarf_Attribute attr_location;
714 if (dwarf_attr(die, DW_AT_location, &attr_location) == NULL)
715 // No location: do not add it ?
718 std::unique_ptr<simgrid::mc::Variable> variable =
719 std::unique_ptr<simgrid::mc::Variable>(new simgrid::mc::Variable());
720 variable->dwarf_offset = dwarf_dieoffset(die);
721 variable->global = frame == NULL; // Can be override base on DW_AT_location
722 variable->object_info = info;
724 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
726 variable->name = name;
727 variable->type_id = MC_dwarf_at_type(die);
729 int form = dwarf_whatform(&attr_location);
730 simgrid::dwarf::FormClass form_class;
731 if (form == DW_FORM_sec_offset)
732 form_class = simgrid::dwarf::FormClass::Constant;
734 form_class = simgrid::dwarf::classify_form(form);
735 switch (form_class) {
736 case simgrid::dwarf::FormClass::ExprLoc:
737 case simgrid::dwarf::FormClass::Block:
738 // Location expression:
742 if (dwarf_getlocation(&attr_location, &expr, &len)) {
744 "Could not read location expression in DW_AT_location "
745 "of variable <%" PRIx64 ">%s",
746 (uint64_t) variable->dwarf_offset,
747 variable->name.c_str());
750 if (len == 1 && expr[0].atom == DW_OP_addr) {
751 variable->global = 1;
752 uintptr_t offset = (uintptr_t) expr[0].number;
753 uintptr_t base = (uintptr_t) info->base_address();
754 variable->address = (void *) (base + offset);
756 simgrid::mc::LocationListEntry entry;
757 entry.expression = {expr, expr + len};
758 variable->location_list = { std::move(entry) };
764 case simgrid::dwarf::FormClass::LocListPtr:
765 case simgrid::dwarf::FormClass::Constant:
766 // Reference to location list:
767 mc_dwarf_location_list_init(
768 &variable->location_list, info, die,
773 xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location "
774 "in <%" PRIx64 ">%s",
775 form, form, (int) form_class, (int) form_class,
776 (uint64_t) variable->dwarf_offset,
777 variable->name.c_str());
780 // Handle start_scope:
781 if (dwarf_hasattr(die, DW_AT_start_scope)) {
782 Dwarf_Attribute attr;
783 dwarf_attr(die, DW_AT_start_scope, &attr);
784 int form = dwarf_whatform(&attr);
785 simgrid::dwarf::FormClass form_class = simgrid::dwarf::classify_form(form);
786 switch (form_class) {
787 case simgrid::dwarf::FormClass::Constant:
790 variable->start_scope =
791 dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
795 case simgrid::dwarf::FormClass::RangeListPtr: // TODO
798 ("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
799 form, (int) form_class, name == NULL ? "?" : name);
803 if (ns && variable->global)
805 std::string(ns) + "::" + variable->name;
807 // The current code needs a variable name,
808 // generate a fake one:
809 if (variable->name.empty())
811 "@anonymous#" + std::to_string(mc_anonymous_variable_index++);
813 return std::move(variable);
816 static void MC_dwarf_handle_variable_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
817 Dwarf_Die * unit, simgrid::mc::Frame* frame,
820 std::unique_ptr<simgrid::mc::Variable> variable =
821 MC_die_to_variable(info, die, unit, frame, ns);
824 // Those arrays are sorted later:
825 else if (variable->global)
826 info->global_variables.push_back(std::move(*variable));
827 else if (frame != nullptr)
828 frame->variables.push_back(std::move(*variable));
830 xbt_die("No frame for this local variable");
833 static void MC_dwarf_handle_scope_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
834 Dwarf_Die * unit, simgrid::mc::Frame* parent_frame,
837 // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
838 int tag = dwarf_tag(die);
839 simgrid::dwarf::TagClass klass = simgrid::dwarf::classify_tag(tag);
841 // (Template) Subprogram declaration:
842 if (klass == simgrid::dwarf::TagClass::Subprogram
843 && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
846 if (klass == simgrid::dwarf::TagClass::Scope)
847 xbt_assert(parent_frame, "No parent scope for this scope");
849 simgrid::mc::Frame frame;
852 frame.id = dwarf_dieoffset(die);
853 frame.object_info = info;
855 if (klass == simgrid::dwarf::TagClass::Subprogram) {
856 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
858 frame.name = std::string(ns) + "::" + name;
865 frame.abstract_origin_id =
866 MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
868 // This is the base address for DWARF addresses.
869 // Relocated addresses are offset from this base address.
870 // See DWARF4 spec 7.5
871 void *base = info->base_address();
873 // TODO, support DW_AT_ranges
874 uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
875 frame.low_pc = low_pc ? ((char *) base) + low_pc : 0;
878 Dwarf_Attribute attr;
879 if (!dwarf_attr_integrate(die, DW_AT_high_pc, &attr)) {
880 xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
886 switch (simgrid::dwarf::classify_form(dwarf_whatform(&attr))) {
888 // DW_AT_high_pc if an offset from the low_pc:
889 case simgrid::dwarf::FormClass::Constant:
891 if (dwarf_formsdata(&attr, &offset) != 0)
892 xbt_die("Could not read constant");
893 frame.high_pc = (void *) ((char *) frame.low_pc + offset);
896 // DW_AT_high_pc is a relocatable address:
897 case simgrid::dwarf::FormClass::Address:
898 if (dwarf_formaddr(&attr, &high_pc) != 0)
899 xbt_die("Could not read address");
900 frame.high_pc = ((char *) base) + high_pc;
904 xbt_die("Unexpected class for DW_AT_high_pc");
909 if (klass == simgrid::dwarf::TagClass::Subprogram) {
910 Dwarf_Attribute attr_frame_base;
911 if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
912 mc_dwarf_location_list_init(&frame.frame_base, info, die,
917 MC_dwarf_handle_children(info, die, unit, &frame, ns);
919 // Someone needs this to be sorted but who?
920 std::sort(frame.variables.begin(), frame.variables.end(),
921 MC_compare_variable);
924 if (klass == simgrid::dwarf::TagClass::Subprogram)
925 info->subprograms[frame.id] = frame;
926 else if (klass == simgrid::dwarf::TagClass::Scope)
927 parent_frame->scopes.push_back(std::move(frame));
930 static void mc_dwarf_handle_namespace_die(simgrid::mc::ObjectInformation* info,
931 Dwarf_Die * die, Dwarf_Die * unit,
932 simgrid::mc::Frame* frame,
935 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
937 xbt_die("Unexpected namespace in a subprogram");
938 char *new_ns = ns == NULL ? xbt_strdup(name)
939 : bprintf("%s::%s", ns, name);
940 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
944 static void MC_dwarf_handle_children(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
945 Dwarf_Die * unit, simgrid::mc::Frame* frame,
948 // For each child DIE:
951 for (res = dwarf_child(die, &child); res == 0;
952 res = dwarf_siblingof(&child, &child)) {
953 MC_dwarf_handle_die(info, &child, unit, frame, ns);
957 static void MC_dwarf_handle_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
958 Dwarf_Die * unit, simgrid::mc::Frame* frame,
961 int tag = dwarf_tag(die);
962 simgrid::dwarf::TagClass klass = simgrid::dwarf::classify_tag(tag);
966 case simgrid::dwarf::TagClass::Type:
967 MC_dwarf_handle_type_die(info, die, unit, frame, ns);
970 // Subprogram or scope:
971 case simgrid::dwarf::TagClass::Subprogram:
972 case simgrid::dwarf::TagClass::Scope:
973 MC_dwarf_handle_scope_die(info, die, unit, frame, ns);
977 case simgrid::dwarf::TagClass::Variable:
978 MC_dwarf_handle_variable_die(info, die, unit, frame, ns);
981 case simgrid::dwarf::TagClass::Namespace:
982 mc_dwarf_handle_namespace_die(info, die, unit, frame, ns);
991 /** \brief Populate the debugging informations of the given ELF object
993 * Read the DWARf information of the EFFL object and populate the
994 * lists of types, variables, functions.
997 void MC_dwarf_get_variables(simgrid::mc::ObjectInformation* info)
999 int fd = open(info->file_name.c_str(), O_RDONLY);
1001 xbt_die("Could not open file %s", info->file_name.c_str());
1002 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
1004 xbt_die("Missing debugging information in %s\n"
1005 "Your program and its dependencies must have debugging information.\n"
1006 "You might want to recompile with -g or install the suitable debugging package.\n",
1007 info->file_name.c_str());
1008 // For each compilation unit:
1009 Dwarf_Off offset = 0;
1010 Dwarf_Off next_offset = 0;
1013 while (dwarf_nextcu(dwarf, offset, &next_offset, &length, NULL, NULL, NULL) ==
1016 if (dwarf_offdie(dwarf, offset + length, &unit_die) != NULL)
1017 MC_dwarf_handle_children(info, &unit_die, &unit_die, NULL, NULL);
1018 offset = next_offset;
1025 // ***** Functions index
1027 static int MC_compare_frame_index_items(simgrid::mc::FunctionIndexEntry* a,
1028 simgrid::mc::FunctionIndexEntry* b)
1030 if (a->low_pc < b->low_pc)
1032 else if (a->low_pc == b->low_pc)
1038 static void MC_make_functions_index(simgrid::mc::ObjectInformation* info)
1040 info->functions_index.clear();
1042 for (auto& e : info->subprograms) {
1043 if (e.second.low_pc == nullptr)
1045 simgrid::mc::FunctionIndexEntry entry;
1046 entry.low_pc = e.second.low_pc;
1047 entry.function = &e.second;
1048 info->functions_index.push_back(entry);
1051 info->functions_index.shrink_to_fit();
1053 // Sort the array by low_pc:
1054 std::sort(info->functions_index.begin(), info->functions_index.end(),
1055 [](simgrid::mc::FunctionIndexEntry const& a,
1056 simgrid::mc::FunctionIndexEntry const& b)
1058 return a.low_pc < b.low_pc;
1062 static void MC_post_process_variables(simgrid::mc::ObjectInformation* info)
1064 // Someone needs this to be sorted but who?
1065 std::sort(info->global_variables.begin(), info->global_variables.end(),
1066 MC_compare_variable);
1068 for(simgrid::mc::Variable& variable : info->global_variables)
1069 if (variable.type_id)
1070 variable.type = simgrid::util::find_map_ptr(
1071 info->types, variable.type_id);
1074 static void mc_post_process_scope(simgrid::mc::ObjectInformation* info, simgrid::mc::Frame* scope)
1077 if (scope->tag == DW_TAG_inlined_subroutine) {
1078 // Attach correct namespaced name in inlined subroutine:
1079 auto i = info->subprograms.find(scope->abstract_origin_id);
1080 xbt_assert(i != info->subprograms.end(),
1081 "Could not lookup abstract origin %" PRIx64,
1082 (std::uint64_t) scope->abstract_origin_id);
1083 scope->name = i->second.name;
1087 for (simgrid::mc::Variable& variable : scope->variables)
1088 if (variable.type_id)
1089 variable.type = simgrid::util::find_map_ptr(
1090 info->types, variable.type_id);
1092 // Recursive post-processing of nested-scopes:
1093 for (simgrid::mc::Frame& nested_scope : scope->scopes)
1094 mc_post_process_scope(info, &nested_scope);
1099 simgrid::mc::Type* MC_resolve_type(
1100 simgrid::mc::ObjectInformation* info, unsigned type_id)
1104 simgrid::mc::Type* type = simgrid::util::find_map_ptr(info->types, type_id);
1105 if (type == nullptr)
1108 // We already have the information on the type:
1109 if (type->byte_size != 0)
1112 // Don't have a name, we can't find a more complete version:
1113 if (type->name.empty())
1116 // Try to find a more complete description of the type:
1117 // We need to fix in order to support C++.
1118 simgrid::mc::Type** subtype = simgrid::util::find_map_ptr(
1119 info->full_types_by_name, type->name);
1125 static void MC_post_process_types(simgrid::mc::ObjectInformation* info)
1127 // Lookup "subtype" field:
1128 for(auto& i : info->types) {
1129 i.second.subtype = MC_resolve_type(info, i.second.type_id);
1130 for (simgrid::mc::Member& member : i.second.members)
1131 member.type = MC_resolve_type(info, member.type_id);
1135 /** \brief Finds informations about a given shared object/executable */
1136 std::shared_ptr<simgrid::mc::ObjectInformation> MC_find_object_info(
1137 std::vector<simgrid::mc::VmMap> const& maps, const char *name, int executable)
1139 std::shared_ptr<simgrid::mc::ObjectInformation> result =
1140 std::make_shared<simgrid::mc::ObjectInformation>();
1142 result->flags |= simgrid::mc::ObjectInformation::Executable;
1143 result->file_name = name;
1144 MC_find_object_address(maps, result.get());
1145 MC_dwarf_get_variables(result.get());
1146 MC_post_process_variables(result.get());
1147 MC_post_process_types(result.get());
1148 for (auto& entry : result.get()->subprograms)
1149 mc_post_process_scope(result.get(), &entry.second);
1150 MC_make_functions_index(result.get());
1151 return std::move(result);
1154 /*************************************************************************/
1156 void MC_post_process_object_info(simgrid::mc::Process* process, simgrid::mc::ObjectInformation* info)
1158 for (auto& i : info->types) {
1160 simgrid::mc::Type* type = &(i.second);
1161 simgrid::mc::Type* subtype = type;
1162 while (subtype->type == DW_TAG_typedef
1163 || subtype->type == DW_TAG_volatile_type
1164 || subtype->type == DW_TAG_const_type)
1165 if (subtype->subtype)
1166 subtype = subtype->subtype;
1170 // Resolve full_type:
1171 if (!subtype->name.empty() && subtype->byte_size == 0) {
1172 for (auto const& object_info : process->object_infos) {
1173 auto i = object_info->full_types_by_name.find(subtype->name);
1174 if (i != object_info->full_types_by_name.end()
1175 && !i->second->name.empty() && i->second->byte_size) {
1176 type->full_type = i->second;
1180 } else type->full_type = subtype;