1 /* Copyright (c) 2008-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
13 #define DW_LANG_Objc DW_LANG_ObjC /* fix spelling error in older dwarf.h */
15 #include <elfutils/libdw.h>
17 #include <simgrid_config.h>
18 #include <simgrid/util.hpp>
20 #include <xbt/sysdep.h>
22 #include <simgrid/util.hpp>
24 #include "mc_object_info.h"
25 #include "mc_private.h"
26 #include "mc_process.h"
28 #include "mc/ObjectInformation.hpp"
29 #include "mc/Variable.hpp"
31 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing");
33 /** \brief The default DW_TAG_lower_bound for a given DW_AT_language.
35 * The default for a given language is defined in the DWARF spec.
37 * \param language consant as defined by the DWARf spec
39 static uint64_t MC_dwarf_default_lower_bound(int lang);
41 /** \brief Computes the the element_count of a DW_TAG_enumeration_type DIE
43 * This is the number of elements in a given array dimension.
45 * A reference of the compilation unit (DW_TAG_compile_unit) is
46 * needed because the default lower bound (when there is no DW_AT_lower_bound)
47 * depends of the language of the compilation unit (DW_AT_language).
49 * \param die DIE for the DW_TAG_enumeration_type or DW_TAG_subrange_type
50 * \param unit DIE of the DW_TAG_compile_unit
52 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
55 /** \brief Computes the number of elements of a given DW_TAG_array_type.
57 * \param die DIE for the DW_TAG_array_type
59 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit);
61 /** \brief Process a DIE
63 * \param info the resulting object fot the library/binary file (output)
64 * \param die the current DIE
65 * \param unit the DIE of the compile unit of the current DIE
66 * \param frame containg frame if any
68 static void MC_dwarf_handle_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
69 Dwarf_Die * unit, simgrid::mc::Frame* frame,
72 /** \brief Process a type DIE
74 static void MC_dwarf_handle_type_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
75 Dwarf_Die * unit, simgrid::mc::Frame* frame,
78 /** \brief Calls MC_dwarf_handle_die on all childrend of the given die
80 * \param info the resulting object fot the library/binary file (output)
81 * \param die the current DIE
82 * \param unit the DIE of the compile unit of the current DIE
83 * \param frame containg frame if any
85 static void MC_dwarf_handle_children(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
86 Dwarf_Die * unit, simgrid::mc::Frame* frame,
89 /** \brief Handle a variable (DW_TAG_variable or other)
91 * \param info the resulting object fot the library/binary file (output)
92 * \param die the current DIE
93 * \param unit the DIE of the compile unit of the current DIE
94 * \param frame containg frame if any
96 static void MC_dwarf_handle_variable_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
97 Dwarf_Die * unit, simgrid::mc::Frame* frame,
100 /** \brief Get the DW_TAG_type of the DIE
103 * \return DW_TAG_type attribute as a new string (NULL if none)
105 static std::uint64_t MC_dwarf_at_type(Dwarf_Die * die);
107 /** \brief A class of DWARF tags (DW_TAG_*)
109 typedef enum mc_tag_class {
118 static mc_tag_class MC_dwarf_tag_classify(int tag)
122 case DW_TAG_array_type:
123 case DW_TAG_class_type:
124 case DW_TAG_enumeration_type:
126 case DW_TAG_pointer_type:
127 case DW_TAG_reference_type:
128 case DW_TAG_rvalue_reference_type:
129 case DW_TAG_string_type:
130 case DW_TAG_structure_type:
131 case DW_TAG_subroutine_type:
132 case DW_TAG_union_type:
133 case DW_TAG_ptr_to_member_type:
134 case DW_TAG_set_type:
135 case DW_TAG_subrange_type:
136 case DW_TAG_base_type:
137 case DW_TAG_const_type:
138 case DW_TAG_file_type:
139 case DW_TAG_packed_type:
140 case DW_TAG_volatile_type:
141 case DW_TAG_restrict_type:
142 case DW_TAG_interface_type:
143 case DW_TAG_unspecified_type:
144 case DW_TAG_shared_type:
147 case DW_TAG_subprogram:
148 return mc_tag_subprogram;
150 case DW_TAG_variable:
151 case DW_TAG_formal_parameter:
152 return mc_tag_variable;
154 case DW_TAG_lexical_block:
155 case DW_TAG_try_block:
156 case DW_TAG_catch_block:
157 case DW_TAG_inlined_subroutine:
158 case DW_TAG_with_stmt:
161 case DW_TAG_namespace:
162 return mc_tag_namespace;
165 return mc_tag_unknown;
170 #define MC_DW_CLASS_UNKNOWN 0
171 #define MC_DW_CLASS_ADDRESS 1 // Location in the address space of the program
172 #define MC_DW_CLASS_BLOCK 2 // Arbitrary block of bytes
173 #define MC_DW_CLASS_CONSTANT 3
174 #define MC_DW_CLASS_STRING 3 // String
175 #define MC_DW_CLASS_FLAG 4 // Boolean
176 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
177 #define MC_DW_CLASS_EXPRLOC 6 // DWARF expression/location description
178 #define MC_DW_CLASS_LINEPTR 7
179 #define MC_DW_CLASS_LOCLISTPTR 8
180 #define MC_DW_CLASS_MACPTR 9
181 #define MC_DW_CLASS_RANGELISTPTR 10
183 /** \brief Find the DWARF data class for a given DWARF data form
185 * This mapping is defined in the DWARF spec.
187 * \param form The form (values taken from the DWARF spec)
188 * \return An internal representation for the corresponding class
190 static int MC_dwarf_form_get_class(int form)
194 return MC_DW_CLASS_ADDRESS;
199 return MC_DW_CLASS_BLOCK;
206 return MC_DW_CLASS_CONSTANT;
209 return MC_DW_CLASS_STRING;
210 case DW_FORM_ref_addr:
215 case DW_FORM_ref_udata:
216 return MC_DW_CLASS_REFERENCE;
218 case DW_FORM_flag_present:
219 return MC_DW_CLASS_FLAG;
220 case DW_FORM_exprloc:
221 return MC_DW_CLASS_EXPRLOC;
225 return MC_DW_CLASS_UNKNOWN;
229 /** \brief Get the name of the tag of a given DIE
232 * \return name of the tag of this DIE
234 static inline const char *MC_dwarf_die_tagname(Dwarf_Die * die)
236 return MC_dwarf_tagname(dwarf_tag(die));
241 /** \brief Get an attribute of a given DIE as a string
244 * \param attribute attribute
245 * \return value of the given attribute of the given DIE
247 static const char *MC_dwarf_attr_integrate_string(Dwarf_Die * die,
250 Dwarf_Attribute attr;
251 if (!dwarf_attr_integrate(die, attribute, &attr)) {
254 return dwarf_formstring(&attr);
258 /** \brief Get the linkage name of a DIE.
260 * Use either DW_AT_linkage_name or DW_AT_MIPS_linkage_name.
261 * DW_AT_linkage_name is standardized since DWARF 4.
262 * Before this version of DWARF, the MIPS extensions
263 * DW_AT_MIPS_linkage_name is used (at least by GCC).
266 * \return linkage name of the given DIE (or NULL)
268 static const char *MC_dwarf_at_linkage_name(Dwarf_Die * die)
270 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_linkage_name);
272 name = MC_dwarf_attr_integrate_string(die, DW_AT_MIPS_linkage_name);
276 static Dwarf_Off MC_dwarf_attr_dieoffset(Dwarf_Die * die, int attribute)
278 Dwarf_Attribute attr;
279 if (dwarf_hasattr_integrate(die, attribute) == 0)
281 dwarf_attr_integrate(die, attribute, &attr);
282 Dwarf_Die subtype_die;
283 if (dwarf_formref_die(&attr, &subtype_die) == NULL)
284 xbt_die("Could not find DIE");
285 return dwarf_dieoffset(&subtype_die);
288 static Dwarf_Off MC_dwarf_attr_integrate_dieoffset(Dwarf_Die * die,
291 Dwarf_Attribute attr;
292 if (dwarf_hasattr_integrate(die, attribute) == 0)
294 dwarf_attr_integrate(die, DW_AT_type, &attr);
295 Dwarf_Die subtype_die;
296 if (dwarf_formref_die(&attr, &subtype_die) == NULL)
297 xbt_die("Could not find DIE");
298 return dwarf_dieoffset(&subtype_die);
301 /** \brief Find the type/subtype (DW_AT_type) for a DIE
304 * \return DW_AT_type reference as a global offset in hexadecimal (or NULL)
307 std::uint64_t MC_dwarf_at_type(Dwarf_Die * die)
309 return MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
312 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die * die, int attribute)
314 Dwarf_Attribute attr;
315 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
318 if (dwarf_formaddr(&attr, &value) == 0)
319 return (uint64_t) value;
324 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die * die, int attribute,
325 uint64_t default_value)
327 Dwarf_Attribute attr;
328 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
329 return default_value;
331 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr),
332 &value) == 0 ? (uint64_t) value : default_value;
335 static bool MC_dwarf_attr_flag(Dwarf_Die * die, int attribute, bool integrate)
337 Dwarf_Attribute attr;
338 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
339 : dwarf_attr(die, attribute, &attr)) == 0)
343 if (dwarf_formflag(&attr, &result))
344 xbt_die("Unexpected form for attribute %s", MC_dwarf_attrname(attribute));
348 /** \brief Find the default lower bound for a given language
350 * The default lower bound of an array (when DW_TAG_lower_bound
351 * is missing) depends on the language of the compilation unit.
353 * \param lang Language of the compilation unit (values defined in the DWARF spec)
354 * \return Default lower bound of an array in this compilation unit
356 static uint64_t MC_dwarf_default_lower_bound(int lang)
362 case DW_LANG_C_plus_plus:
366 case DW_LANG_ObjC_plus_plus:
372 case DW_LANG_Fortran77:
373 case DW_LANG_Fortran90:
374 case DW_LANG_Fortran95:
375 case DW_LANG_Modula2:
376 case DW_LANG_Pascal83:
378 case DW_LANG_Cobol74:
379 case DW_LANG_Cobol85:
382 xbt_die("No default DW_TAG_lower_bound for language %i and none given",
388 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
391 * \param unit DIE of the compilation unit
392 * \return number of elements in the range
394 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
397 xbt_assert(dwarf_tag(die) == DW_TAG_enumeration_type
398 || dwarf_tag(die) == DW_TAG_subrange_type,
399 "MC_dwarf_subrange_element_count called with DIE of type %s",
400 MC_dwarf_die_tagname(die));
402 // Use DW_TAG_count if present:
403 if (dwarf_hasattr_integrate(die, DW_AT_count))
404 return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
405 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
407 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound))
408 // This is not really 0, but the code expects this (we do not know):
411 uint64_t upper_bound =
412 MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
414 uint64_t lower_bound = 0;
415 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound))
416 lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
418 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
419 return upper_bound - lower_bound + 1;
422 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
424 * The compilation unit might be needed because the default lower
425 * bound depends on the language of the compilation unit.
427 * \param die the DIE of the DW_TAG_array_type
428 * \param unit the DIE of the compilation unit
429 * \return number of elements in this array type
431 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit)
433 xbt_assert(dwarf_tag(die) == DW_TAG_array_type,
434 "MC_dwarf_array_element_count called with DIE of type %s",
435 MC_dwarf_die_tagname(die));
440 for (res = dwarf_child(die, &child); res == 0;
441 res = dwarf_siblingof(&child, &child)) {
442 int child_tag = dwarf_tag(&child);
443 if (child_tag == DW_TAG_subrange_type
444 || child_tag == DW_TAG_enumeration_type)
445 result *= MC_dwarf_subrange_element_count(&child, unit);
452 /** Sort the variable by name and address.
454 * We could use boost::container::flat_set instead.
456 static bool MC_compare_variable(
457 simgrid::mc::Variable const& a, simgrid::mc::Variable const& b)
459 int cmp = strcmp(a.name.c_str(), b.name.c_str());
465 return a.address < b.address;
468 // ***** simgrid::mc::Type*
470 /** \brief Initialize the location of a member of a type
471 * (DW_AT_data_member_location of a DW_TAG_member).
473 * \param type a type (struct, class)
474 * \param member the member of the type
475 * \param child DIE of the member (DW_TAG_member)
477 static void MC_dwarf_fill_member_location(simgrid::mc::Type* type, simgrid::mc::Type* member,
480 if (dwarf_hasattr(child, DW_AT_data_bit_offset))
481 xbt_die("Can't groke DW_AT_data_bit_offset.");
483 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
484 if (type->type == DW_TAG_union_type)
487 ("Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%"
488 PRIx64 ">%s", member->name.c_str(),
489 (uint64_t) type->id, type->name.c_str());
492 Dwarf_Attribute attr;
493 dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
494 int form = dwarf_whatform(&attr);
495 int klass = MC_dwarf_form_get_class(form);
497 case MC_DW_CLASS_EXPRLOC:
498 case MC_DW_CLASS_BLOCK:
499 // Location expression:
503 if (dwarf_getlocation(&attr, &expr, &len))
505 ("Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%"
506 PRIx64 ">%s", MC_dwarf_attr_integrate_string(child, DW_AT_name),
507 (uint64_t) type->id, type->name.c_str());
508 simgrid::mc::DwarfExpression(expr, expr+len);
511 case MC_DW_CLASS_CONSTANT:
512 // Offset from the base address of the object:
515 if (!dwarf_formudata(&attr, &offset))
516 member->offset(offset);
518 xbt_die("Cannot get %s location <%" PRIx64 ">%s",
519 MC_dwarf_attr_integrate_string(child, DW_AT_name),
520 (uint64_t) type->id, type->name.c_str());
523 case MC_DW_CLASS_LOCLISTPTR:
524 // Reference to a location list:
526 case MC_DW_CLASS_REFERENCE:
527 // It's supposed to be possible in DWARF2 but I couldn't find its semantic
530 xbt_die("Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
536 /** \brief Populate the list of members of a type
538 * \param info ELF object containing the type DIE
539 * \param die DIE of the type
540 * \param unit DIE of the compilation unit containing the type DIE
541 * \param type the type
543 static void MC_dwarf_add_members(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
544 Dwarf_Die * unit, simgrid::mc::Type* type)
548 xbt_assert(type->members.empty());
549 for (res = dwarf_child(die, &child); res == 0;
550 res = dwarf_siblingof(&child, &child)) {
551 int tag = dwarf_tag(&child);
552 if (tag == DW_TAG_member || tag == DW_TAG_inheritance) {
554 // Skip declarations:
555 if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
558 // Skip compile time constants:
559 if (dwarf_hasattr(&child, DW_AT_const_value))
562 // TODO, we should use another type (because is is not a type but a member)
563 simgrid::mc::Type member;
567 member.id = dwarf_dieoffset(&child);
569 const char *name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
573 MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
574 member.element_count = -1;
575 member.type_id = MC_dwarf_at_type(&child);
577 if (dwarf_hasattr(&child, DW_AT_data_bit_offset))
578 xbt_die("Can't groke DW_AT_data_bit_offset.");
580 MC_dwarf_fill_member_location(type, &member, &child);
583 xbt_die("Missing type for member %s of <%" PRIx64 ">%s",
585 (uint64_t) type->id, type->name.c_str());
587 type->members.push_back(std::move(member));
592 /** \brief Create a MC type object from a DIE
594 * \param info current object info object
595 * \param DIE (for a given type);
596 * \param unit compilation unit of the current DIE
597 * \return MC representation of the type
599 static simgrid::mc::Type MC_dwarf_die_to_type(
600 simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
601 Dwarf_Die * unit, simgrid::mc::Frame* frame,
604 simgrid::mc::Type type;
605 type.type = dwarf_tag(die);
606 type.name = std::string();
607 type.element_count = -1;
610 type.id = dwarf_dieoffset(die);
612 const char *prefix = "";
614 case DW_TAG_structure_type:
617 case DW_TAG_union_type:
620 case DW_TAG_class_type:
627 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
629 char* full_name = ns ? bprintf("%s%s::%s", prefix, ns, name) :
630 bprintf("%s%s", prefix, name);
631 type.name = std::string(full_name);
635 type.type_id = MC_dwarf_at_type(die);
637 // Some compilers do not emit DW_AT_byte_size for pointer_type,
638 // so we fill this. We currently assume that the model-checked process is in
639 // the same architecture..
640 if (type.type == DW_TAG_pointer_type)
641 type.byte_size = sizeof(void*);
643 // Computation of the byte_size;
644 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
645 type.byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
646 else if (type.type == DW_TAG_array_type
647 || type.type == DW_TAG_structure_type
648 || type.type == DW_TAG_class_type) {
650 if (dwarf_aggregate_size(die, &size) == 0)
651 type.byte_size = size;
655 case DW_TAG_array_type:
656 type.element_count = MC_dwarf_array_element_count(die, unit);
657 // TODO, handle DW_byte_stride and (not) DW_bit_stride
660 case DW_TAG_pointer_type:
661 case DW_TAG_reference_type:
662 case DW_TAG_rvalue_reference_type:
663 type.is_pointer_type = 1;
666 case DW_TAG_structure_type:
667 case DW_TAG_union_type:
668 case DW_TAG_class_type:
669 MC_dwarf_add_members(info, die, unit, &type);
670 char *new_ns = ns == NULL ? xbt_strdup(type.name.c_str())
671 : bprintf("%s::%s", ns, name);
672 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
677 return std::move(type);
680 static void MC_dwarf_handle_type_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
681 Dwarf_Die * unit, simgrid::mc::Frame* frame,
684 simgrid::mc::Type type = MC_dwarf_die_to_type(info, die, unit, frame, ns);
685 auto& t = (info->types[type.id] = std::move(type));
686 if (!t.name.empty() && type.byte_size != 0)
687 info->full_types_by_name[t.name] = &t;
690 static int mc_anonymous_variable_index = 0;
692 static std::unique_ptr<simgrid::mc::Variable> MC_die_to_variable(
693 simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
694 Dwarf_Die * unit, simgrid::mc::Frame* frame,
697 // Skip declarations:
698 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
701 // Skip compile time constants:
702 if (dwarf_hasattr(die, DW_AT_const_value))
705 Dwarf_Attribute attr_location;
706 if (dwarf_attr(die, DW_AT_location, &attr_location) == NULL)
707 // No location: do not add it ?
710 std::unique_ptr<simgrid::mc::Variable> variable =
711 std::unique_ptr<simgrid::mc::Variable>(new simgrid::mc::Variable());
712 variable->dwarf_offset = dwarf_dieoffset(die);
713 variable->global = frame == NULL; // Can be override base on DW_AT_location
714 variable->object_info = info;
716 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
718 variable->name = name;
719 variable->type_id = MC_dwarf_at_type(die);
721 int form = dwarf_whatform(&attr_location);
724 DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
726 case MC_DW_CLASS_EXPRLOC:
727 case MC_DW_CLASS_BLOCK:
728 // Location expression:
732 if (dwarf_getlocation(&attr_location, &expr, &len)) {
734 "Could not read location expression in DW_AT_location "
735 "of variable <%" PRIx64 ">%s",
736 (uint64_t) variable->dwarf_offset,
737 variable->name.c_str());
740 if (len == 1 && expr[0].atom == DW_OP_addr) {
741 variable->global = 1;
742 uintptr_t offset = (uintptr_t) expr[0].number;
743 uintptr_t base = (uintptr_t) info->base_address();
744 variable->address = (void *) (base + offset);
746 simgrid::mc::LocationListEntry entry;
747 entry.expression = {expr, expr + len};
748 variable->location_list = { std::move(entry) };
753 case MC_DW_CLASS_LOCLISTPTR:
754 case MC_DW_CLASS_CONSTANT:
755 // Reference to location list:
756 mc_dwarf_location_list_init(
757 &variable->location_list, info, die,
761 xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location "
762 "in <%" PRIx64 ">%s",
763 form, form, klass, klass,
764 (uint64_t) variable->dwarf_offset,
765 variable->name.c_str());
768 // Handle start_scope:
769 if (dwarf_hasattr(die, DW_AT_start_scope)) {
770 Dwarf_Attribute attr;
771 dwarf_attr(die, DW_AT_start_scope, &attr);
772 int form = dwarf_whatform(&attr);
773 int klass = MC_dwarf_form_get_class(form);
775 case MC_DW_CLASS_CONSTANT:
778 variable->start_scope =
779 dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
782 case MC_DW_CLASS_RANGELISTPTR: // TODO
785 ("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
786 form, klass, name == NULL ? "?" : name);
790 if (ns && variable->global)
792 std::string(ns) + "::" + variable->name;
794 // The current code needs a variable name,
795 // generate a fake one:
796 if (variable->name.empty())
798 "@anonymous#" + std::to_string(mc_anonymous_variable_index++);
800 return std::move(variable);
803 static void MC_dwarf_handle_variable_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
804 Dwarf_Die * unit, simgrid::mc::Frame* frame,
807 std::unique_ptr<simgrid::mc::Variable> variable =
808 MC_die_to_variable(info, die, unit, frame, ns);
811 // Those arrays are sorted later:
812 else if (variable->global)
813 info->global_variables.push_back(std::move(*variable));
814 else if (frame != nullptr)
815 frame->variables.push_back(std::move(*variable));
817 xbt_die("No frame for this local variable");
820 static void MC_dwarf_handle_scope_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
821 Dwarf_Die * unit, simgrid::mc::Frame* parent_frame,
824 // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
825 int tag = dwarf_tag(die);
826 mc_tag_class klass = MC_dwarf_tag_classify(tag);
828 // (Template) Subprogram declaration:
829 if (klass == mc_tag_subprogram
830 && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
833 if (klass == mc_tag_scope)
834 xbt_assert(parent_frame, "No parent scope for this scope");
836 simgrid::mc::Frame frame;
839 frame.id = dwarf_dieoffset(die);
840 frame.object_info = info;
842 if (klass == mc_tag_subprogram) {
843 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
845 frame.name = std::string(ns) + "::" + name;
852 frame.abstract_origin_id =
853 MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
855 // This is the base address for DWARF addresses.
856 // Relocated addresses are offset from this base address.
857 // See DWARF4 spec 7.5
858 void *base = info->base_address();
860 // TODO, support DW_AT_ranges
861 uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
862 frame.low_pc = low_pc ? ((char *) base) + low_pc : 0;
865 Dwarf_Attribute attr;
866 if (!dwarf_attr_integrate(die, DW_AT_high_pc, &attr)) {
867 xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
873 switch (MC_dwarf_form_get_class(dwarf_whatform(&attr))) {
875 // DW_AT_high_pc if an offset from the low_pc:
876 case MC_DW_CLASS_CONSTANT:
878 if (dwarf_formsdata(&attr, &offset) != 0)
879 xbt_die("Could not read constant");
880 frame.high_pc = (void *) ((char *) frame.low_pc + offset);
883 // DW_AT_high_pc is a relocatable address:
884 case MC_DW_CLASS_ADDRESS:
885 if (dwarf_formaddr(&attr, &high_pc) != 0)
886 xbt_die("Could not read address");
887 frame.high_pc = ((char *) base) + high_pc;
891 xbt_die("Unexpected class for DW_AT_high_pc");
896 if (klass == mc_tag_subprogram) {
897 Dwarf_Attribute attr_frame_base;
898 if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
899 mc_dwarf_location_list_init(&frame.frame_base, info, die,
904 MC_dwarf_handle_children(info, die, unit, &frame, ns);
906 // Someone needs this to be sorted but who?
907 std::sort(frame.variables.begin(), frame.variables.end(),
908 MC_compare_variable);
911 if (klass == mc_tag_subprogram)
912 info->subprograms[frame.id] = frame;
913 else if (klass == mc_tag_scope)
914 parent_frame->scopes.push_back(std::move(frame));
917 static void mc_dwarf_handle_namespace_die(simgrid::mc::ObjectInformation* info,
918 Dwarf_Die * die, Dwarf_Die * unit,
919 simgrid::mc::Frame* frame,
922 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
924 xbt_die("Unexpected namespace in a subprogram");
925 char *new_ns = ns == NULL ? xbt_strdup(name)
926 : bprintf("%s::%s", ns, name);
927 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
931 static void MC_dwarf_handle_children(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
932 Dwarf_Die * unit, simgrid::mc::Frame* frame,
935 // For each child DIE:
938 for (res = dwarf_child(die, &child); res == 0;
939 res = dwarf_siblingof(&child, &child)) {
940 MC_dwarf_handle_die(info, &child, unit, frame, ns);
944 static void MC_dwarf_handle_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
945 Dwarf_Die * unit, simgrid::mc::Frame* frame,
948 int tag = dwarf_tag(die);
949 mc_tag_class klass = MC_dwarf_tag_classify(tag);
954 MC_dwarf_handle_type_die(info, die, unit, frame, ns);
957 // Subprogram or scope:
958 case mc_tag_subprogram:
960 MC_dwarf_handle_scope_die(info, die, unit, frame, ns);
964 case mc_tag_variable:
965 MC_dwarf_handle_variable_die(info, die, unit, frame, ns);
968 case mc_tag_namespace:
969 mc_dwarf_handle_namespace_die(info, die, unit, frame, ns);
978 /** \brief Populate the debugging informations of the given ELF object
980 * Read the DWARf information of the EFFL object and populate the
981 * lists of types, variables, functions.
983 void MC_dwarf_get_variables(simgrid::mc::ObjectInformation* info)
985 int fd = open(info->file_name.c_str(), O_RDONLY);
987 xbt_die("Could not open file %s", info->file_name.c_str());
988 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
990 xbt_die("Missing debugging information in %s\n"
991 "Your program and its dependencies must have debugging information.\n"
992 "You might want to recompile with -g or install the suitable debugging package.\n",
993 info->file_name.c_str());
994 // For each compilation unit:
995 Dwarf_Off offset = 0;
996 Dwarf_Off next_offset = 0;
999 while (dwarf_nextcu(dwarf, offset, &next_offset, &length, NULL, NULL, NULL) ==
1002 if (dwarf_offdie(dwarf, offset + length, &unit_die) != NULL)
1003 MC_dwarf_handle_children(info, &unit_die, &unit_die, NULL, NULL);
1004 offset = next_offset;
1011 // ***** Functions index
1013 static int MC_compare_frame_index_items(simgrid::mc::FunctionIndexEntry* a,
1014 simgrid::mc::FunctionIndexEntry* b)
1016 if (a->low_pc < b->low_pc)
1018 else if (a->low_pc == b->low_pc)
1024 static void MC_make_functions_index(simgrid::mc::ObjectInformation* info)
1026 info->functions_index.clear();
1028 for (auto& e : info->subprograms) {
1029 if (e.second.low_pc == nullptr)
1031 simgrid::mc::FunctionIndexEntry entry;
1032 entry.low_pc = e.second.low_pc;
1033 entry.function = &e.second;
1034 info->functions_index.push_back(entry);
1037 info->functions_index.shrink_to_fit();
1039 // Sort the array by low_pc:
1040 std::sort(info->functions_index.begin(), info->functions_index.end(),
1041 [](simgrid::mc::FunctionIndexEntry const& a,
1042 simgrid::mc::FunctionIndexEntry const& b)
1044 return a.low_pc < b.low_pc;
1048 static void MC_post_process_variables(simgrid::mc::ObjectInformation* info)
1050 // Someone needs this to be sorted but who?
1051 std::sort(info->global_variables.begin(), info->global_variables.end(),
1052 MC_compare_variable);
1054 for(simgrid::mc::Variable& variable : info->global_variables)
1055 if (variable.type_id)
1056 variable.type = simgrid::util::find_map_ptr(
1057 info->types, variable.type_id);
1060 static void mc_post_process_scope(simgrid::mc::ObjectInformation* info, simgrid::mc::Frame* scope)
1063 if (scope->tag == DW_TAG_inlined_subroutine) {
1064 // Attach correct namespaced name in inlined subroutine:
1065 auto i = info->subprograms.find(scope->abstract_origin_id);
1066 xbt_assert(i != info->subprograms.end(),
1067 "Could not lookup abstract origin %" PRIx64,
1068 (uint64_t) scope->abstract_origin_id);
1069 scope->name = i->second.name;
1073 for (simgrid::mc::Variable& variable : scope->variables)
1074 if (variable.type_id)
1075 variable.type = simgrid::util::find_map_ptr(
1076 info->types, variable.type_id);
1078 // Recursive post-processing of nested-scopes:
1079 for (simgrid::mc::Frame& nested_scope : scope->scopes)
1080 mc_post_process_scope(info, &nested_scope);
1084 /** \brief Fill/lookup the "subtype" field.
1086 static void MC_resolve_subtype(simgrid::mc::ObjectInformation* info, simgrid::mc::Type* type)
1090 type->subtype = simgrid::util::find_map_ptr(info->types, type->type_id);
1091 if (type->subtype == nullptr)
1093 if (type->subtype->byte_size != 0)
1095 if (type->subtype->name.empty())
1097 // Try to find a more complete description of the type:
1098 // We need to fix in order to support C++.
1099 simgrid::mc::Type** subtype = simgrid::util::find_map_ptr(
1100 info->full_types_by_name, type->subtype->name);
1102 type->subtype = *subtype;
1105 static void MC_post_process_types(simgrid::mc::ObjectInformation* info)
1107 // Lookup "subtype" field:
1108 for(auto& i : info->types) {
1109 MC_resolve_subtype(info, &(i.second));
1110 for (simgrid::mc::Type& member : i.second.members)
1111 MC_resolve_subtype(info, &member);
1115 /** \brief Finds informations about a given shared object/executable */
1116 std::shared_ptr<simgrid::mc::ObjectInformation> MC_find_object_info(
1117 std::vector<simgrid::mc::VmMap> const& maps, const char *name, int executable)
1119 std::shared_ptr<simgrid::mc::ObjectInformation> result =
1120 std::make_shared<simgrid::mc::ObjectInformation>();
1122 result->flags |= simgrid::mc::ObjectInformation::Executable;
1123 result->file_name = name;
1124 MC_find_object_address(maps, result.get());
1125 MC_dwarf_get_variables(result.get());
1126 MC_post_process_variables(result.get());
1127 MC_post_process_types(result.get());
1128 for (auto& entry : result.get()->subprograms)
1129 mc_post_process_scope(result.get(), &entry.second);
1130 MC_make_functions_index(result.get());
1131 return std::move(result);
1134 /*************************************************************************/
1136 void MC_post_process_object_info(simgrid::mc::Process* process, simgrid::mc::ObjectInformation* info)
1138 for (auto& i : info->types) {
1140 simgrid::mc::Type* type = &(i.second);
1141 simgrid::mc::Type* subtype = type;
1142 while (subtype->type == DW_TAG_typedef
1143 || subtype->type == DW_TAG_volatile_type
1144 || subtype->type == DW_TAG_const_type)
1145 if (subtype->subtype)
1146 subtype = subtype->subtype;
1150 // Resolve full_type:
1151 if (!subtype->name.empty() && subtype->byte_size == 0) {
1152 for (auto const& object_info : process->object_infos) {
1153 auto i = object_info->full_types_by_name.find(subtype->name);
1154 if (i != object_info->full_types_by_name.end()
1155 && !i->second->name.empty() && i->second->byte_size) {
1156 type->full_type = i->second;
1160 } else type->full_type = subtype;