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"
28 #include "mc/Process.hpp"
29 #include "mc/ObjectInformation.hpp"
30 #include "mc/Variable.hpp"
32 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing");
34 /** \brief The default DW_TAG_lower_bound for a given DW_AT_language.
36 * The default for a given language is defined in the DWARF spec.
38 * \param language consant as defined by the DWARf spec
40 static uint64_t MC_dwarf_default_lower_bound(int lang);
42 /** \brief Computes the the element_count of a DW_TAG_enumeration_type DIE
44 * This is the number of elements in a given array dimension.
46 * A reference of the compilation unit (DW_TAG_compile_unit) is
47 * needed because the default lower bound (when there is no DW_AT_lower_bound)
48 * depends of the language of the compilation unit (DW_AT_language).
50 * \param die DIE for the DW_TAG_enumeration_type or DW_TAG_subrange_type
51 * \param unit DIE of the DW_TAG_compile_unit
53 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
56 /** \brief Computes the number of elements of a given DW_TAG_array_type.
58 * \param die DIE for the DW_TAG_array_type
60 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit);
62 /** \brief Process a DIE
64 * \param info the resulting object fot the library/binary file (output)
65 * \param die the current DIE
66 * \param unit the DIE of the compile unit of the current DIE
67 * \param frame containg frame if any
69 static void MC_dwarf_handle_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
70 Dwarf_Die * unit, simgrid::mc::Frame* frame,
73 /** \brief Process a type DIE
75 static void MC_dwarf_handle_type_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
76 Dwarf_Die * unit, simgrid::mc::Frame* frame,
79 /** \brief Calls MC_dwarf_handle_die on all childrend of the given die
81 * \param info the resulting object fot the library/binary file (output)
82 * \param die the current DIE
83 * \param unit the DIE of the compile unit of the current DIE
84 * \param frame containg frame if any
86 static void MC_dwarf_handle_children(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
87 Dwarf_Die * unit, simgrid::mc::Frame* frame,
90 /** \brief Handle a variable (DW_TAG_variable or other)
92 * \param info the resulting object fot the library/binary file (output)
93 * \param die the current DIE
94 * \param unit the DIE of the compile unit of the current DIE
95 * \param frame containg frame if any
97 static void MC_dwarf_handle_variable_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
98 Dwarf_Die * unit, simgrid::mc::Frame* frame,
101 /** \brief Get the DW_TAG_type of the DIE
104 * \return DW_TAG_type attribute as a new string (NULL if none)
106 static std::uint64_t MC_dwarf_at_type(Dwarf_Die * die);
108 /** \brief A class of DWARF tags (DW_TAG_*)
110 typedef enum mc_tag_class {
119 static mc_tag_class MC_dwarf_tag_classify(int tag)
123 case DW_TAG_array_type:
124 case DW_TAG_class_type:
125 case DW_TAG_enumeration_type:
127 case DW_TAG_pointer_type:
128 case DW_TAG_reference_type:
129 case DW_TAG_rvalue_reference_type:
130 case DW_TAG_string_type:
131 case DW_TAG_structure_type:
132 case DW_TAG_subroutine_type:
133 case DW_TAG_union_type:
134 case DW_TAG_ptr_to_member_type:
135 case DW_TAG_set_type:
136 case DW_TAG_subrange_type:
137 case DW_TAG_base_type:
138 case DW_TAG_const_type:
139 case DW_TAG_file_type:
140 case DW_TAG_packed_type:
141 case DW_TAG_volatile_type:
142 case DW_TAG_restrict_type:
143 case DW_TAG_interface_type:
144 case DW_TAG_unspecified_type:
145 case DW_TAG_shared_type:
148 case DW_TAG_subprogram:
149 return mc_tag_subprogram;
151 case DW_TAG_variable:
152 case DW_TAG_formal_parameter:
153 return mc_tag_variable;
155 case DW_TAG_lexical_block:
156 case DW_TAG_try_block:
157 case DW_TAG_catch_block:
158 case DW_TAG_inlined_subroutine:
159 case DW_TAG_with_stmt:
162 case DW_TAG_namespace:
163 return mc_tag_namespace;
166 return mc_tag_unknown;
171 #define MC_DW_CLASS_UNKNOWN 0
172 #define MC_DW_CLASS_ADDRESS 1 // Location in the address space of the program
173 #define MC_DW_CLASS_BLOCK 2 // Arbitrary block of bytes
174 #define MC_DW_CLASS_CONSTANT 3
175 #define MC_DW_CLASS_STRING 3 // String
176 #define MC_DW_CLASS_FLAG 4 // Boolean
177 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
178 #define MC_DW_CLASS_EXPRLOC 6 // DWARF expression/location description
179 #define MC_DW_CLASS_LINEPTR 7
180 #define MC_DW_CLASS_LOCLISTPTR 8
181 #define MC_DW_CLASS_MACPTR 9
182 #define MC_DW_CLASS_RANGELISTPTR 10
184 /** \brief Find the DWARF data class for a given DWARF data form
186 * This mapping is defined in the DWARF spec.
188 * \param form The form (values taken from the DWARF spec)
189 * \return An internal representation for the corresponding class
191 static int MC_dwarf_form_get_class(int form)
195 return MC_DW_CLASS_ADDRESS;
200 return MC_DW_CLASS_BLOCK;
207 return MC_DW_CLASS_CONSTANT;
210 return MC_DW_CLASS_STRING;
211 case DW_FORM_ref_addr:
216 case DW_FORM_ref_udata:
217 return MC_DW_CLASS_REFERENCE;
219 case DW_FORM_flag_present:
220 return MC_DW_CLASS_FLAG;
221 case DW_FORM_exprloc:
222 return MC_DW_CLASS_EXPRLOC;
226 return MC_DW_CLASS_UNKNOWN;
230 /** \brief Get the name of the tag of a given DIE
233 * \return name of the tag of this DIE
235 static inline const char *MC_dwarf_die_tagname(Dwarf_Die * die)
237 return MC_dwarf_tagname(dwarf_tag(die));
242 /** \brief Get an attribute of a given DIE as a string
245 * \param attribute attribute
246 * \return value of the given attribute of the given DIE
248 static const char *MC_dwarf_attr_integrate_string(Dwarf_Die * die,
251 Dwarf_Attribute attr;
252 if (!dwarf_attr_integrate(die, attribute, &attr)) {
255 return dwarf_formstring(&attr);
259 /** \brief Get the linkage name of a DIE.
261 * Use either DW_AT_linkage_name or DW_AT_MIPS_linkage_name.
262 * DW_AT_linkage_name is standardized since DWARF 4.
263 * Before this version of DWARF, the MIPS extensions
264 * DW_AT_MIPS_linkage_name is used (at least by GCC).
267 * \return linkage name of the given DIE (or NULL)
269 static const char *MC_dwarf_at_linkage_name(Dwarf_Die * die)
271 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_linkage_name);
273 name = MC_dwarf_attr_integrate_string(die, DW_AT_MIPS_linkage_name);
277 static Dwarf_Off MC_dwarf_attr_dieoffset(Dwarf_Die * die, int attribute)
279 Dwarf_Attribute attr;
280 if (dwarf_hasattr_integrate(die, attribute) == 0)
282 dwarf_attr_integrate(die, attribute, &attr);
283 Dwarf_Die subtype_die;
284 if (dwarf_formref_die(&attr, &subtype_die) == NULL)
285 xbt_die("Could not find DIE");
286 return dwarf_dieoffset(&subtype_die);
289 static Dwarf_Off MC_dwarf_attr_integrate_dieoffset(Dwarf_Die * die,
292 Dwarf_Attribute attr;
293 if (dwarf_hasattr_integrate(die, attribute) == 0)
295 dwarf_attr_integrate(die, DW_AT_type, &attr);
296 Dwarf_Die subtype_die;
297 if (dwarf_formref_die(&attr, &subtype_die) == NULL)
298 xbt_die("Could not find DIE");
299 return dwarf_dieoffset(&subtype_die);
302 /** \brief Find the type/subtype (DW_AT_type) for a DIE
305 * \return DW_AT_type reference as a global offset in hexadecimal (or NULL)
308 std::uint64_t MC_dwarf_at_type(Dwarf_Die * die)
310 return MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
313 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die * die, int attribute)
315 Dwarf_Attribute attr;
316 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
319 if (dwarf_formaddr(&attr, &value) == 0)
320 return (uint64_t) value;
325 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die * die, int attribute,
326 uint64_t default_value)
328 Dwarf_Attribute attr;
329 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
330 return default_value;
332 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr),
333 &value) == 0 ? (uint64_t) value : default_value;
336 static bool MC_dwarf_attr_flag(Dwarf_Die * die, int attribute, bool integrate)
338 Dwarf_Attribute attr;
339 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
340 : dwarf_attr(die, attribute, &attr)) == 0)
344 if (dwarf_formflag(&attr, &result))
345 xbt_die("Unexpected form for attribute %s", MC_dwarf_attrname(attribute));
349 /** \brief Find the default lower bound for a given language
351 * The default lower bound of an array (when DW_TAG_lower_bound
352 * is missing) depends on the language of the compilation unit.
354 * \param lang Language of the compilation unit (values defined in the DWARF spec)
355 * \return Default lower bound of an array in this compilation unit
357 static uint64_t MC_dwarf_default_lower_bound(int lang)
363 case DW_LANG_C_plus_plus:
367 case DW_LANG_ObjC_plus_plus:
373 case DW_LANG_Fortran77:
374 case DW_LANG_Fortran90:
375 case DW_LANG_Fortran95:
376 case DW_LANG_Modula2:
377 case DW_LANG_Pascal83:
379 case DW_LANG_Cobol74:
380 case DW_LANG_Cobol85:
383 xbt_die("No default DW_TAG_lower_bound for language %i and none given",
389 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
392 * \param unit DIE of the compilation unit
393 * \return number of elements in the range
395 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
398 xbt_assert(dwarf_tag(die) == DW_TAG_enumeration_type
399 || dwarf_tag(die) == DW_TAG_subrange_type,
400 "MC_dwarf_subrange_element_count called with DIE of type %s",
401 MC_dwarf_die_tagname(die));
403 // Use DW_TAG_count if present:
404 if (dwarf_hasattr_integrate(die, DW_AT_count))
405 return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
406 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
408 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound))
409 // This is not really 0, but the code expects this (we do not know):
412 uint64_t upper_bound =
413 MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
415 uint64_t lower_bound = 0;
416 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound))
417 lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
419 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
420 return upper_bound - lower_bound + 1;
423 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
425 * The compilation unit might be needed because the default lower
426 * bound depends on the language of the compilation unit.
428 * \param die the DIE of the DW_TAG_array_type
429 * \param unit the DIE of the compilation unit
430 * \return number of elements in this array type
432 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit)
434 xbt_assert(dwarf_tag(die) == DW_TAG_array_type,
435 "MC_dwarf_array_element_count called with DIE of type %s",
436 MC_dwarf_die_tagname(die));
441 for (res = dwarf_child(die, &child); res == 0;
442 res = dwarf_siblingof(&child, &child)) {
443 int child_tag = dwarf_tag(&child);
444 if (child_tag == DW_TAG_subrange_type
445 || child_tag == DW_TAG_enumeration_type)
446 result *= MC_dwarf_subrange_element_count(&child, unit);
453 /** Sort the variable by name and address.
455 * We could use boost::container::flat_set instead.
457 static bool MC_compare_variable(
458 simgrid::mc::Variable const& a, simgrid::mc::Variable const& b)
460 int cmp = strcmp(a.name.c_str(), b.name.c_str());
466 return a.address < b.address;
469 // ***** simgrid::mc::Type*
471 /** \brief Initialize the location of a member of a type
472 * (DW_AT_data_member_location of a DW_TAG_member).
474 * \param type a type (struct, class)
475 * \param member the member of the type
476 * \param child DIE of the member (DW_TAG_member)
478 static void MC_dwarf_fill_member_location(
479 simgrid::mc::Type* type, simgrid::mc::Member* member, Dwarf_Die * child)
481 if (dwarf_hasattr(child, DW_AT_data_bit_offset))
482 xbt_die("Can't groke DW_AT_data_bit_offset.");
484 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
485 if (type->type == DW_TAG_union_type)
488 ("Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%"
489 PRIx64 ">%s", member->name.c_str(),
490 (uint64_t) type->id, type->name.c_str());
493 Dwarf_Attribute attr;
494 dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
495 int form = dwarf_whatform(&attr);
496 int klass = MC_dwarf_form_get_class(form);
498 case MC_DW_CLASS_EXPRLOC:
499 case MC_DW_CLASS_BLOCK:
500 // Location expression:
504 if (dwarf_getlocation(&attr, &expr, &len))
506 ("Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%"
507 PRIx64 ">%s", MC_dwarf_attr_integrate_string(child, DW_AT_name),
508 (uint64_t) type->id, type->name.c_str());
509 member->location_expression = simgrid::mc::DwarfExpression(expr, expr+len);
512 case MC_DW_CLASS_CONSTANT:
513 // Offset from the base address of the object:
516 if (!dwarf_formudata(&attr, &offset))
517 member->offset(offset);
519 xbt_die("Cannot get %s location <%" PRIx64 ">%s",
520 MC_dwarf_attr_integrate_string(child, DW_AT_name),
521 (uint64_t) type->id, type->name.c_str());
524 case MC_DW_CLASS_LOCLISTPTR:
525 // Reference to a location list:
527 case MC_DW_CLASS_REFERENCE:
528 // It's supposed to be possible in DWARF2 but I couldn't find its semantic
531 xbt_die("Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
537 /** \brief Populate the list of members of a type
539 * \param info ELF object containing the type DIE
540 * \param die DIE of the type
541 * \param unit DIE of the compilation unit containing the type DIE
542 * \param type the type
544 static void MC_dwarf_add_members(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
545 Dwarf_Die * unit, simgrid::mc::Type* type)
549 xbt_assert(type->members.empty());
550 for (res = dwarf_child(die, &child); res == 0;
551 res = dwarf_siblingof(&child, &child)) {
552 int tag = dwarf_tag(&child);
553 if (tag == DW_TAG_member || tag == DW_TAG_inheritance) {
555 // Skip declarations:
556 if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
559 // Skip compile time constants:
560 if (dwarf_hasattr(&child, DW_AT_const_value))
563 // TODO, we should use another type (because is is not a type but a member)
564 simgrid::mc::Member member;
565 member.inheritance = tag == DW_TAG_inheritance;
567 const char *name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
571 MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
572 member.type_id = MC_dwarf_at_type(&child);
574 if (dwarf_hasattr(&child, DW_AT_data_bit_offset))
575 xbt_die("Can't groke DW_AT_data_bit_offset.");
577 MC_dwarf_fill_member_location(type, &member, &child);
580 xbt_die("Missing type for member %s of <%" PRIx64 ">%s",
582 (uint64_t) type->id, type->name.c_str());
584 type->members.push_back(std::move(member));
589 /** \brief Create a MC type object from a DIE
591 * \param info current object info object
592 * \param DIE (for a given type);
593 * \param unit compilation unit of the current DIE
594 * \return MC representation of the type
596 static simgrid::mc::Type MC_dwarf_die_to_type(
597 simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
598 Dwarf_Die * unit, simgrid::mc::Frame* frame,
601 simgrid::mc::Type type;
602 type.type = dwarf_tag(die);
603 type.name = std::string();
604 type.element_count = -1;
607 type.id = dwarf_dieoffset(die);
609 const char *prefix = "";
611 case DW_TAG_structure_type:
614 case DW_TAG_union_type:
617 case DW_TAG_class_type:
624 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
626 char* full_name = ns ? bprintf("%s%s::%s", prefix, ns, name) :
627 bprintf("%s%s", prefix, name);
628 type.name = std::string(full_name);
632 type.type_id = MC_dwarf_at_type(die);
634 // Some compilers do not emit DW_AT_byte_size for pointer_type,
635 // so we fill this. We currently assume that the model-checked process is in
636 // the same architecture..
637 if (type.type == DW_TAG_pointer_type)
638 type.byte_size = sizeof(void*);
640 // Computation of the byte_size;
641 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
642 type.byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
643 else if (type.type == DW_TAG_array_type
644 || type.type == DW_TAG_structure_type
645 || type.type == DW_TAG_class_type) {
647 if (dwarf_aggregate_size(die, &size) == 0)
648 type.byte_size = size;
652 case DW_TAG_array_type:
653 type.element_count = MC_dwarf_array_element_count(die, unit);
654 // TODO, handle DW_byte_stride and (not) DW_bit_stride
657 case DW_TAG_pointer_type:
658 case DW_TAG_reference_type:
659 case DW_TAG_rvalue_reference_type:
660 type.is_pointer_type = 1;
663 case DW_TAG_structure_type:
664 case DW_TAG_union_type:
665 case DW_TAG_class_type:
666 MC_dwarf_add_members(info, die, unit, &type);
667 char *new_ns = ns == NULL ? xbt_strdup(type.name.c_str())
668 : bprintf("%s::%s", ns, name);
669 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
674 return std::move(type);
677 static void MC_dwarf_handle_type_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
678 Dwarf_Die * unit, simgrid::mc::Frame* frame,
681 simgrid::mc::Type type = MC_dwarf_die_to_type(info, die, unit, frame, ns);
682 auto& t = (info->types[type.id] = std::move(type));
683 if (!t.name.empty() && type.byte_size != 0)
684 info->full_types_by_name[t.name] = &t;
687 static int mc_anonymous_variable_index = 0;
689 static std::unique_ptr<simgrid::mc::Variable> MC_die_to_variable(
690 simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
691 Dwarf_Die * unit, simgrid::mc::Frame* frame,
694 // Skip declarations:
695 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
698 // Skip compile time constants:
699 if (dwarf_hasattr(die, DW_AT_const_value))
702 Dwarf_Attribute attr_location;
703 if (dwarf_attr(die, DW_AT_location, &attr_location) == NULL)
704 // No location: do not add it ?
707 std::unique_ptr<simgrid::mc::Variable> variable =
708 std::unique_ptr<simgrid::mc::Variable>(new simgrid::mc::Variable());
709 variable->dwarf_offset = dwarf_dieoffset(die);
710 variable->global = frame == NULL; // Can be override base on DW_AT_location
711 variable->object_info = info;
713 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
715 variable->name = name;
716 variable->type_id = MC_dwarf_at_type(die);
718 int form = dwarf_whatform(&attr_location);
721 DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
723 case MC_DW_CLASS_EXPRLOC:
724 case MC_DW_CLASS_BLOCK:
725 // Location expression:
729 if (dwarf_getlocation(&attr_location, &expr, &len)) {
731 "Could not read location expression in DW_AT_location "
732 "of variable <%" PRIx64 ">%s",
733 (uint64_t) variable->dwarf_offset,
734 variable->name.c_str());
737 if (len == 1 && expr[0].atom == DW_OP_addr) {
738 variable->global = 1;
739 uintptr_t offset = (uintptr_t) expr[0].number;
740 uintptr_t base = (uintptr_t) info->base_address();
741 variable->address = (void *) (base + offset);
743 simgrid::mc::LocationListEntry entry;
744 entry.expression = {expr, expr + len};
745 variable->location_list = { std::move(entry) };
750 case MC_DW_CLASS_LOCLISTPTR:
751 case MC_DW_CLASS_CONSTANT:
752 // Reference to location list:
753 mc_dwarf_location_list_init(
754 &variable->location_list, info, die,
758 xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location "
759 "in <%" PRIx64 ">%s",
760 form, form, klass, klass,
761 (uint64_t) variable->dwarf_offset,
762 variable->name.c_str());
765 // Handle start_scope:
766 if (dwarf_hasattr(die, DW_AT_start_scope)) {
767 Dwarf_Attribute attr;
768 dwarf_attr(die, DW_AT_start_scope, &attr);
769 int form = dwarf_whatform(&attr);
770 int klass = MC_dwarf_form_get_class(form);
772 case MC_DW_CLASS_CONSTANT:
775 variable->start_scope =
776 dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
779 case MC_DW_CLASS_RANGELISTPTR: // TODO
782 ("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
783 form, klass, name == NULL ? "?" : name);
787 if (ns && variable->global)
789 std::string(ns) + "::" + variable->name;
791 // The current code needs a variable name,
792 // generate a fake one:
793 if (variable->name.empty())
795 "@anonymous#" + std::to_string(mc_anonymous_variable_index++);
797 return std::move(variable);
800 static void MC_dwarf_handle_variable_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
801 Dwarf_Die * unit, simgrid::mc::Frame* frame,
804 std::unique_ptr<simgrid::mc::Variable> variable =
805 MC_die_to_variable(info, die, unit, frame, ns);
808 // Those arrays are sorted later:
809 else if (variable->global)
810 info->global_variables.push_back(std::move(*variable));
811 else if (frame != nullptr)
812 frame->variables.push_back(std::move(*variable));
814 xbt_die("No frame for this local variable");
817 static void MC_dwarf_handle_scope_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
818 Dwarf_Die * unit, simgrid::mc::Frame* parent_frame,
821 // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
822 int tag = dwarf_tag(die);
823 mc_tag_class klass = MC_dwarf_tag_classify(tag);
825 // (Template) Subprogram declaration:
826 if (klass == mc_tag_subprogram
827 && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
830 if (klass == mc_tag_scope)
831 xbt_assert(parent_frame, "No parent scope for this scope");
833 simgrid::mc::Frame frame;
836 frame.id = dwarf_dieoffset(die);
837 frame.object_info = info;
839 if (klass == mc_tag_subprogram) {
840 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
842 frame.name = std::string(ns) + "::" + name;
849 frame.abstract_origin_id =
850 MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
852 // This is the base address for DWARF addresses.
853 // Relocated addresses are offset from this base address.
854 // See DWARF4 spec 7.5
855 void *base = info->base_address();
857 // TODO, support DW_AT_ranges
858 uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
859 frame.low_pc = low_pc ? ((char *) base) + low_pc : 0;
862 Dwarf_Attribute attr;
863 if (!dwarf_attr_integrate(die, DW_AT_high_pc, &attr)) {
864 xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
870 switch (MC_dwarf_form_get_class(dwarf_whatform(&attr))) {
872 // DW_AT_high_pc if an offset from the low_pc:
873 case MC_DW_CLASS_CONSTANT:
875 if (dwarf_formsdata(&attr, &offset) != 0)
876 xbt_die("Could not read constant");
877 frame.high_pc = (void *) ((char *) frame.low_pc + offset);
880 // DW_AT_high_pc is a relocatable address:
881 case MC_DW_CLASS_ADDRESS:
882 if (dwarf_formaddr(&attr, &high_pc) != 0)
883 xbt_die("Could not read address");
884 frame.high_pc = ((char *) base) + high_pc;
888 xbt_die("Unexpected class for DW_AT_high_pc");
893 if (klass == mc_tag_subprogram) {
894 Dwarf_Attribute attr_frame_base;
895 if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
896 mc_dwarf_location_list_init(&frame.frame_base, info, die,
901 MC_dwarf_handle_children(info, die, unit, &frame, ns);
903 // Someone needs this to be sorted but who?
904 std::sort(frame.variables.begin(), frame.variables.end(),
905 MC_compare_variable);
908 if (klass == mc_tag_subprogram)
909 info->subprograms[frame.id] = frame;
910 else if (klass == mc_tag_scope)
911 parent_frame->scopes.push_back(std::move(frame));
914 static void mc_dwarf_handle_namespace_die(simgrid::mc::ObjectInformation* info,
915 Dwarf_Die * die, Dwarf_Die * unit,
916 simgrid::mc::Frame* frame,
919 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
921 xbt_die("Unexpected namespace in a subprogram");
922 char *new_ns = ns == NULL ? xbt_strdup(name)
923 : bprintf("%s::%s", ns, name);
924 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
928 static void MC_dwarf_handle_children(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
929 Dwarf_Die * unit, simgrid::mc::Frame* frame,
932 // For each child DIE:
935 for (res = dwarf_child(die, &child); res == 0;
936 res = dwarf_siblingof(&child, &child)) {
937 MC_dwarf_handle_die(info, &child, unit, frame, ns);
941 static void MC_dwarf_handle_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
942 Dwarf_Die * unit, simgrid::mc::Frame* frame,
945 int tag = dwarf_tag(die);
946 mc_tag_class klass = MC_dwarf_tag_classify(tag);
951 MC_dwarf_handle_type_die(info, die, unit, frame, ns);
954 // Subprogram or scope:
955 case mc_tag_subprogram:
957 MC_dwarf_handle_scope_die(info, die, unit, frame, ns);
961 case mc_tag_variable:
962 MC_dwarf_handle_variable_die(info, die, unit, frame, ns);
965 case mc_tag_namespace:
966 mc_dwarf_handle_namespace_die(info, die, unit, frame, ns);
975 /** \brief Populate the debugging informations of the given ELF object
977 * Read the DWARf information of the EFFL object and populate the
978 * lists of types, variables, functions.
980 void MC_dwarf_get_variables(simgrid::mc::ObjectInformation* info)
982 int fd = open(info->file_name.c_str(), O_RDONLY);
984 xbt_die("Could not open file %s", info->file_name.c_str());
985 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
987 xbt_die("Missing debugging information in %s\n"
988 "Your program and its dependencies must have debugging information.\n"
989 "You might want to recompile with -g or install the suitable debugging package.\n",
990 info->file_name.c_str());
991 // For each compilation unit:
992 Dwarf_Off offset = 0;
993 Dwarf_Off next_offset = 0;
996 while (dwarf_nextcu(dwarf, offset, &next_offset, &length, NULL, NULL, NULL) ==
999 if (dwarf_offdie(dwarf, offset + length, &unit_die) != NULL)
1000 MC_dwarf_handle_children(info, &unit_die, &unit_die, NULL, NULL);
1001 offset = next_offset;
1008 // ***** Functions index
1010 static int MC_compare_frame_index_items(simgrid::mc::FunctionIndexEntry* a,
1011 simgrid::mc::FunctionIndexEntry* b)
1013 if (a->low_pc < b->low_pc)
1015 else if (a->low_pc == b->low_pc)
1021 static void MC_make_functions_index(simgrid::mc::ObjectInformation* info)
1023 info->functions_index.clear();
1025 for (auto& e : info->subprograms) {
1026 if (e.second.low_pc == nullptr)
1028 simgrid::mc::FunctionIndexEntry entry;
1029 entry.low_pc = e.second.low_pc;
1030 entry.function = &e.second;
1031 info->functions_index.push_back(entry);
1034 info->functions_index.shrink_to_fit();
1036 // Sort the array by low_pc:
1037 std::sort(info->functions_index.begin(), info->functions_index.end(),
1038 [](simgrid::mc::FunctionIndexEntry const& a,
1039 simgrid::mc::FunctionIndexEntry const& b)
1041 return a.low_pc < b.low_pc;
1045 static void MC_post_process_variables(simgrid::mc::ObjectInformation* info)
1047 // Someone needs this to be sorted but who?
1048 std::sort(info->global_variables.begin(), info->global_variables.end(),
1049 MC_compare_variable);
1051 for(simgrid::mc::Variable& variable : info->global_variables)
1052 if (variable.type_id)
1053 variable.type = simgrid::util::find_map_ptr(
1054 info->types, variable.type_id);
1057 static void mc_post_process_scope(simgrid::mc::ObjectInformation* info, simgrid::mc::Frame* scope)
1060 if (scope->tag == DW_TAG_inlined_subroutine) {
1061 // Attach correct namespaced name in inlined subroutine:
1062 auto i = info->subprograms.find(scope->abstract_origin_id);
1063 xbt_assert(i != info->subprograms.end(),
1064 "Could not lookup abstract origin %" PRIx64,
1065 (std::uint64_t) scope->abstract_origin_id);
1066 scope->name = i->second.name;
1070 for (simgrid::mc::Variable& variable : scope->variables)
1071 if (variable.type_id)
1072 variable.type = simgrid::util::find_map_ptr(
1073 info->types, variable.type_id);
1075 // Recursive post-processing of nested-scopes:
1076 for (simgrid::mc::Frame& nested_scope : scope->scopes)
1077 mc_post_process_scope(info, &nested_scope);
1082 simgrid::mc::Type* MC_resolve_type(
1083 simgrid::mc::ObjectInformation* info, unsigned type_id)
1087 simgrid::mc::Type* type = simgrid::util::find_map_ptr(info->types, type_id);
1088 if (type == nullptr)
1091 // We already have the information on the type:
1092 if (type->byte_size != 0)
1095 // Don't have a name, we can't find a more complete version:
1096 if (type->name.empty())
1099 // Try to find a more complete description of the type:
1100 // We need to fix in order to support C++.
1101 simgrid::mc::Type** subtype = simgrid::util::find_map_ptr(
1102 info->full_types_by_name, type->name);
1108 static void MC_post_process_types(simgrid::mc::ObjectInformation* info)
1110 // Lookup "subtype" field:
1111 for(auto& i : info->types) {
1112 i.second.subtype = MC_resolve_type(info, i.second.type_id);
1113 for (simgrid::mc::Member& member : i.second.members)
1114 member.type = MC_resolve_type(info, member.type_id);
1118 /** \brief Finds informations about a given shared object/executable */
1119 std::shared_ptr<simgrid::mc::ObjectInformation> MC_find_object_info(
1120 std::vector<simgrid::mc::VmMap> const& maps, const char *name, int executable)
1122 std::shared_ptr<simgrid::mc::ObjectInformation> result =
1123 std::make_shared<simgrid::mc::ObjectInformation>();
1125 result->flags |= simgrid::mc::ObjectInformation::Executable;
1126 result->file_name = name;
1127 MC_find_object_address(maps, result.get());
1128 MC_dwarf_get_variables(result.get());
1129 MC_post_process_variables(result.get());
1130 MC_post_process_types(result.get());
1131 for (auto& entry : result.get()->subprograms)
1132 mc_post_process_scope(result.get(), &entry.second);
1133 MC_make_functions_index(result.get());
1134 return std::move(result);
1137 /*************************************************************************/
1139 void MC_post_process_object_info(simgrid::mc::Process* process, simgrid::mc::ObjectInformation* info)
1141 for (auto& i : info->types) {
1143 simgrid::mc::Type* type = &(i.second);
1144 simgrid::mc::Type* subtype = type;
1145 while (subtype->type == DW_TAG_typedef
1146 || subtype->type == DW_TAG_volatile_type
1147 || subtype->type == DW_TAG_const_type)
1148 if (subtype->subtype)
1149 subtype = subtype->subtype;
1153 // Resolve full_type:
1154 if (!subtype->name.empty() && subtype->byte_size == 0) {
1155 for (auto const& object_info : process->object_infos) {
1156 auto i = object_info->full_types_by_name.find(subtype->name);
1157 if (i != object_info->full_types_by_name.end()
1158 && !i->second->name.empty() && i->second->byte_size) {
1159 type->full_type = i->second;
1163 } else type->full_type = subtype;