1 /* Copyright (c) 2008-2014. 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>
19 #include <xbt/sysdep.h>
21 #include "mc_object_info.h"
22 #include "mc_private.h"
24 static void MC_register_variable(
25 mc_object_info_t info, mc_frame_t frame, std::unique_ptr<simgrid::mc::Variable> variable);
26 static void MC_dwarf_register_variable(
27 mc_object_info_t info, mc_frame_t frame,
28 std::unique_ptr<simgrid::mc::Variable> variable);
30 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing");
32 /** \brief The default DW_TAG_lower_bound for a given DW_AT_language.
34 * The default for a given language is defined in the DWARF spec.
36 * \param language consant as defined by the DWARf spec
38 static uint64_t MC_dwarf_default_lower_bound(int lang);
40 /** \brief Computes the the element_count of a DW_TAG_enumeration_type DIE
42 * This is the number of elements in a given array dimension.
44 * A reference of the compilation unit (DW_TAG_compile_unit) is
45 * needed because the default lower bound (when there is no DW_AT_lower_bound)
46 * depends of the language of the compilation unit (DW_AT_language).
48 * \param die DIE for the DW_TAG_enumeration_type or DW_TAG_subrange_type
49 * \param unit DIE of the DW_TAG_compile_unit
51 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
54 /** \brief Computes the number of elements of a given DW_TAG_array_type.
56 * \param die DIE for the DW_TAG_array_type
58 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit);
60 /** \brief Process a DIE
62 * \param info the resulting object fot the library/binary file (output)
63 * \param die the current DIE
64 * \param unit the DIE of the compile unit of the current DIE
65 * \param frame containg frame if any
67 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
68 Dwarf_Die * unit, mc_frame_t frame,
71 /** \brief Process a type DIE
73 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
74 Dwarf_Die * unit, mc_frame_t frame,
77 /** \brief Calls MC_dwarf_handle_die on all childrend of the given die
79 * \param info the resulting object fot the library/binary file (output)
80 * \param die the current DIE
81 * \param unit the DIE of the compile unit of the current DIE
82 * \param frame containg frame if any
84 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
85 Dwarf_Die * unit, mc_frame_t frame,
88 /** \brief Handle a variable (DW_TAG_variable or other)
90 * \param info the resulting object fot the library/binary file (output)
91 * \param die the current DIE
92 * \param unit the DIE of the compile unit of the current DIE
93 * \param frame containg frame if any
95 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
96 Dwarf_Die * unit, mc_frame_t frame,
99 /** \brief Get the DW_TAG_type of the DIE
102 * \return DW_TAG_type attribute as a new string (NULL if none)
104 static std::uint64_t MC_dwarf_at_type(Dwarf_Die * die);
106 /** \brief A class of DWARF tags (DW_TAG_*)
108 typedef enum mc_tag_class {
117 static mc_tag_class MC_dwarf_tag_classify(int tag)
121 case DW_TAG_array_type:
122 case DW_TAG_class_type:
123 case DW_TAG_enumeration_type:
125 case DW_TAG_pointer_type:
126 case DW_TAG_reference_type:
127 case DW_TAG_rvalue_reference_type:
128 case DW_TAG_string_type:
129 case DW_TAG_structure_type:
130 case DW_TAG_subroutine_type:
131 case DW_TAG_union_type:
132 case DW_TAG_ptr_to_member_type:
133 case DW_TAG_set_type:
134 case DW_TAG_subrange_type:
135 case DW_TAG_base_type:
136 case DW_TAG_const_type:
137 case DW_TAG_file_type:
138 case DW_TAG_packed_type:
139 case DW_TAG_volatile_type:
140 case DW_TAG_restrict_type:
141 case DW_TAG_interface_type:
142 case DW_TAG_unspecified_type:
143 case DW_TAG_shared_type:
146 case DW_TAG_subprogram:
147 return mc_tag_subprogram;
149 case DW_TAG_variable:
150 case DW_TAG_formal_parameter:
151 return mc_tag_variable;
153 case DW_TAG_lexical_block:
154 case DW_TAG_try_block:
155 case DW_TAG_catch_block:
156 case DW_TAG_inlined_subroutine:
157 case DW_TAG_with_stmt:
160 case DW_TAG_namespace:
161 return mc_tag_namespace;
164 return mc_tag_unknown;
169 #define MC_DW_CLASS_UNKNOWN 0
170 #define MC_DW_CLASS_ADDRESS 1 // Location in the address space of the program
171 #define MC_DW_CLASS_BLOCK 2 // Arbitrary block of bytes
172 #define MC_DW_CLASS_CONSTANT 3
173 #define MC_DW_CLASS_STRING 3 // String
174 #define MC_DW_CLASS_FLAG 4 // Boolean
175 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
176 #define MC_DW_CLASS_EXPRLOC 6 // DWARF expression/location description
177 #define MC_DW_CLASS_LINEPTR 7
178 #define MC_DW_CLASS_LOCLISTPTR 8
179 #define MC_DW_CLASS_MACPTR 9
180 #define MC_DW_CLASS_RANGELISTPTR 10
182 /** \brief Find the DWARF data class for a given DWARF data form
184 * This mapping is defined in the DWARF spec.
186 * \param form The form (values taken from the DWARF spec)
187 * \return An internal representation for the corresponding class
189 static int MC_dwarf_form_get_class(int form)
193 return MC_DW_CLASS_ADDRESS;
198 return MC_DW_CLASS_BLOCK;
205 return MC_DW_CLASS_CONSTANT;
208 return MC_DW_CLASS_STRING;
209 case DW_FORM_ref_addr:
214 case DW_FORM_ref_udata:
215 return MC_DW_CLASS_REFERENCE;
217 case DW_FORM_flag_present:
218 return MC_DW_CLASS_FLAG;
219 case DW_FORM_exprloc:
220 return MC_DW_CLASS_EXPRLOC;
224 return MC_DW_CLASS_UNKNOWN;
228 /** \brief Get the name of the tag of a given DIE
231 * \return name of the tag of this DIE
233 static inline const char *MC_dwarf_die_tagname(Dwarf_Die * die)
235 return MC_dwarf_tagname(dwarf_tag(die));
240 /** \brief Get an attribute of a given DIE as a string
243 * \param attribute attribute
244 * \return value of the given attribute of the given DIE
246 static const char *MC_dwarf_attr_integrate_string(Dwarf_Die * die,
249 Dwarf_Attribute attr;
250 if (!dwarf_attr_integrate(die, attribute, &attr)) {
253 return dwarf_formstring(&attr);
257 /** \brief Get the linkage name of a DIE.
259 * Use either DW_AT_linkage_name or DW_AT_MIPS_linkage_name.
260 * DW_AT_linkage_name is standardized since DWARF 4.
261 * Before this version of DWARF, the MIPS extensions
262 * DW_AT_MIPS_linkage_name is used (at least by GCC).
265 * \return linkage name of the given DIE (or NULL)
267 static const char *MC_dwarf_at_linkage_name(Dwarf_Die * die)
269 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_linkage_name);
271 name = MC_dwarf_attr_integrate_string(die, DW_AT_MIPS_linkage_name);
275 static Dwarf_Off MC_dwarf_attr_dieoffset(Dwarf_Die * die, int attribute)
277 Dwarf_Attribute attr;
278 if (dwarf_hasattr_integrate(die, attribute)) {
279 dwarf_attr_integrate(die, attribute, &attr);
280 Dwarf_Die subtype_die;
281 if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
282 xbt_die("Could not find DIE");
284 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)) {
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");
299 return dwarf_dieoffset(&subtype_die);
304 /** \brief Find the type/subtype (DW_AT_type) for a DIE
307 * \return DW_AT_type reference as a global offset in hexadecimal (or NULL)
310 std::uint64_t MC_dwarf_at_type(Dwarf_Die * die)
312 return MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
315 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die * die, int attribute)
317 Dwarf_Attribute attr;
318 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
321 if (dwarf_formaddr(&attr, &value) == 0)
322 return (uint64_t) value;
327 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die * die, int attribute,
328 uint64_t default_value)
330 Dwarf_Attribute attr;
331 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
332 return default_value;
334 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr),
335 &value) == 0 ? (uint64_t) value : default_value;
338 static bool MC_dwarf_attr_flag(Dwarf_Die * die, int attribute, bool integrate)
340 Dwarf_Attribute attr;
341 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
342 : dwarf_attr(die, attribute, &attr)) == 0)
346 if (dwarf_formflag(&attr, &result))
347 xbt_die("Unexpected form for attribute %s", MC_dwarf_attrname(attribute));
351 /** \brief Find the default lower bound for a given language
353 * The default lower bound of an array (when DW_TAG_lower_bound
354 * is missing) depends on the language of the compilation unit.
356 * \param lang Language of the compilation unit (values defined in the DWARF spec)
357 * \return Default lower bound of an array in this compilation unit
359 static uint64_t MC_dwarf_default_lower_bound(int lang)
365 case DW_LANG_C_plus_plus:
369 case DW_LANG_ObjC_plus_plus:
375 case DW_LANG_Fortran77:
376 case DW_LANG_Fortran90:
377 case DW_LANG_Fortran95:
378 case DW_LANG_Modula2:
379 case DW_LANG_Pascal83:
381 case DW_LANG_Cobol74:
382 case DW_LANG_Cobol85:
385 xbt_die("No default DW_TAG_lower_bound for language %i and none given",
391 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
394 * \param unit DIE of the compilation unit
395 * \return number of elements in the range
397 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
400 xbt_assert(dwarf_tag(die) == DW_TAG_enumeration_type
401 || dwarf_tag(die) == DW_TAG_subrange_type,
402 "MC_dwarf_subrange_element_count called with DIE of type %s",
403 MC_dwarf_die_tagname(die));
405 // Use DW_TAG_count if present:
406 if (dwarf_hasattr_integrate(die, DW_AT_count)) {
407 return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
409 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
411 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
412 // This is not really 0, but the code expects this (we do not know):
415 uint64_t upper_bound =
416 MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
418 uint64_t lower_bound = 0;
419 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
420 lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
422 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
424 return upper_bound - lower_bound + 1;
427 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
429 * The compilation unit might be needed because the default lower
430 * bound depends on the language of the compilation unit.
432 * \param die the DIE of the DW_TAG_array_type
433 * \param unit the DIE of the compilation unit
434 * \return number of elements in this array type
436 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit)
438 xbt_assert(dwarf_tag(die) == DW_TAG_array_type,
439 "MC_dwarf_array_element_count called with DIE of type %s",
440 MC_dwarf_die_tagname(die));
445 for (res = dwarf_child(die, &child); res == 0;
446 res = dwarf_siblingof(&child, &child)) {
447 int child_tag = dwarf_tag(&child);
448 if (child_tag == DW_TAG_subrange_type
449 || child_tag == DW_TAG_enumeration_type) {
450 result *= MC_dwarf_subrange_element_count(&child, unit);
458 static bool MC_compare_variable(
459 simgrid::mc::Variable const& a, simgrid::mc::Variable const& b)
461 int cmp = strcmp(a.name.c_str(), b.name.c_str());
467 return a.address < b.address;
472 /** \brief Initialize the location of a member of a type
473 * (DW_AT_data_member_location of a DW_TAG_member).
475 * \param type a type (struct, class)
476 * \param member the member of the type
477 * \param child DIE of the member (DW_TAG_member)
479 static void MC_dwarf_fill_member_location(mc_type_t type, mc_type_t member,
482 if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
483 xbt_die("Can't groke DW_AT_data_bit_offset.");
486 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
487 if (type->type != DW_TAG_union_type) {
489 ("Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%"
490 PRIx64 ">%s", member->name.c_str(),
491 (uint64_t) type->id, type->name.c_str());
497 Dwarf_Attribute attr;
498 dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
499 int form = dwarf_whatform(&attr);
500 int klass = MC_dwarf_form_get_class(form);
502 case MC_DW_CLASS_EXPRLOC:
503 case MC_DW_CLASS_BLOCK:
504 // Location expression:
508 if (dwarf_getlocation(&attr, &expr, &len)) {
510 ("Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%"
511 PRIx64 ">%s", MC_dwarf_attr_integrate_string(child, DW_AT_name),
512 (uint64_t) type->id, type->name.c_str());
514 simgrid::mc::DwarfExpression(expr, expr+len);
517 case MC_DW_CLASS_CONSTANT:
518 // Offset from the base address of the object:
521 if (!dwarf_formudata(&attr, &offset))
522 member->offset(offset);
524 xbt_die("Cannot get %s location <%" PRIx64 ">%s",
525 MC_dwarf_attr_integrate_string(child, DW_AT_name),
526 (uint64_t) type->id, type->name.c_str());
529 case MC_DW_CLASS_LOCLISTPTR:
530 // Reference to a location list:
532 case MC_DW_CLASS_REFERENCE:
533 // It's supposed to be possible in DWARF2 but I couldn't find its semantic
536 xbt_die("Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
542 static void dw_type_free_voidp(void *t)
544 delete *(mc_type_t*)t;
547 /** \brief Populate the list of members of a type
549 * \param info ELF object containing the type DIE
550 * \param die DIE of the type
551 * \param unit DIE of the compilation unit containing the type DIE
552 * \param type the type
554 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die * die,
555 Dwarf_Die * unit, mc_type_t type)
559 xbt_assert(type->members.empty());
560 for (res = dwarf_child(die, &child); res == 0;
561 res = dwarf_siblingof(&child, &child)) {
562 int tag = dwarf_tag(&child);
563 if (tag == DW_TAG_member || tag == DW_TAG_inheritance) {
565 // Skip declarations:
566 if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
569 // Skip compile time constants:
570 if (dwarf_hasattr(&child, DW_AT_const_value))
573 // TODO, we should use another type (because is is not a type but a member)
574 simgrid::mc::Type member;
578 member.id = dwarf_dieoffset(&child);
580 const char *name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
584 MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
585 member.element_count = -1;
586 member.type_id = MC_dwarf_at_type(&child);
588 if (dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
589 xbt_die("Can't groke DW_AT_data_bit_offset.");
592 MC_dwarf_fill_member_location(type, &member, &child);
594 if (!member.type_id) {
595 xbt_die("Missing type for member %s of <%" PRIx64 ">%s",
597 (uint64_t) type->id, type->name.c_str());
600 type->members.push_back(std::move(member));
605 /** \brief Create a MC type object from a DIE
607 * \param info current object info object
608 * \param DIE (for a given type);
609 * \param unit compilation unit of the current DIE
610 * \return MC representation of the type
612 static simgrid::mc::Type MC_dwarf_die_to_type(
613 mc_object_info_t info, Dwarf_Die * die,
614 Dwarf_Die * unit, mc_frame_t frame,
618 simgrid::mc::Type type;
620 type.name = std::string();
621 type.element_count = -1;
623 type.type = dwarf_tag(die);
626 type.id = dwarf_dieoffset(die);
628 const char *prefix = "";
630 case DW_TAG_structure_type:
633 case DW_TAG_union_type:
636 case DW_TAG_class_type:
643 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
645 char* full_name = ns ? bprintf("%s%s::%s", prefix, ns, name) :
646 bprintf("%s%s", prefix, name);
647 type.name = std::string(full_name);
651 type.type_id = MC_dwarf_at_type(die);
653 // Some compilers do not emit DW_AT_byte_size for pointer_type,
654 // so we fill this. We currently assume that the model-checked process is in
655 // the same architecture..
656 if (type.type == DW_TAG_pointer_type)
657 type.byte_size = sizeof(void*);
659 // Computation of the byte_size;
660 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
661 type.byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
662 else if (type.type == DW_TAG_array_type
663 || type.type == DW_TAG_structure_type
664 || type.type == DW_TAG_class_type) {
666 if (dwarf_aggregate_size(die, &size) == 0) {
667 type.byte_size = size;
672 case DW_TAG_array_type:
673 type.element_count = MC_dwarf_array_element_count(die, unit);
674 // TODO, handle DW_byte_stride and (not) DW_bit_stride
677 case DW_TAG_pointer_type:
678 case DW_TAG_reference_type:
679 case DW_TAG_rvalue_reference_type:
680 type.is_pointer_type = 1;
683 case DW_TAG_structure_type:
684 case DW_TAG_union_type:
685 case DW_TAG_class_type:
686 MC_dwarf_add_members(info, die, unit, &type);
687 char *new_ns = ns == NULL ? xbt_strdup(type.name.c_str())
688 : bprintf("%s::%s", ns, name);
689 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
694 return std::move(type);
697 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
698 Dwarf_Die * unit, mc_frame_t frame,
701 simgrid::mc::Type type = MC_dwarf_die_to_type(info, die, unit, frame, ns);
702 auto& t = (info->types[type.id] = std::move(type));
703 if (!t.name.empty() && type.byte_size != 0)
704 info->full_types_by_name[t.name] = &t;
707 static int mc_anonymous_variable_index = 0;
709 static std::unique_ptr<simgrid::mc::Variable> MC_die_to_variable(
710 mc_object_info_t info, Dwarf_Die * die,
711 Dwarf_Die * unit, mc_frame_t frame,
714 // Skip declarations:
715 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
718 // Skip compile time constants:
719 if (dwarf_hasattr(die, DW_AT_const_value))
722 Dwarf_Attribute attr_location;
723 if (dwarf_attr(die, DW_AT_location, &attr_location) == NULL) {
724 // No location: do not add it ?
728 std::unique_ptr<simgrid::mc::Variable> variable =
729 std::unique_ptr<simgrid::mc::Variable>(new simgrid::mc::Variable());
730 variable->dwarf_offset = dwarf_dieoffset(die);
731 variable->global = frame == NULL; // Can be override base on DW_AT_location
732 variable->object_info = info;
734 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
736 variable->name = name;
737 variable->type_id = MC_dwarf_at_type(die);
739 int form = dwarf_whatform(&attr_location);
742 DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
744 case MC_DW_CLASS_EXPRLOC:
745 case MC_DW_CLASS_BLOCK:
746 // Location expression:
750 if (dwarf_getlocation(&attr_location, &expr, &len)) {
752 "Could not read location expression in DW_AT_location "
753 "of variable <%" PRIx64 ">%s",
754 (uint64_t) variable->dwarf_offset,
755 variable->name.c_str());
758 if (len == 1 && expr[0].atom == DW_OP_addr) {
759 variable->global = 1;
760 uintptr_t offset = (uintptr_t) expr[0].number;
761 uintptr_t base = (uintptr_t) info->base_address();
762 variable->address = (void *) (base + offset);
764 simgrid::mc::LocationListEntry entry;
765 entry.expression = {expr, expr + len};
766 variable->location_list = { std::move(entry) };
771 case MC_DW_CLASS_LOCLISTPTR:
772 case MC_DW_CLASS_CONSTANT:
773 // Reference to location list:
774 mc_dwarf_location_list_init(
775 &variable->location_list, info, die,
779 xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location "
780 "in <%" PRIx64 ">%s",
781 form, form, klass, klass,
782 (uint64_t) variable->dwarf_offset,
783 variable->name.c_str());
786 // Handle start_scope:
787 if (dwarf_hasattr(die, DW_AT_start_scope)) {
788 Dwarf_Attribute attr;
789 dwarf_attr(die, DW_AT_start_scope, &attr);
790 int form = dwarf_whatform(&attr);
791 int klass = MC_dwarf_form_get_class(form);
793 case MC_DW_CLASS_CONSTANT:
796 variable->start_scope =
797 dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
800 case MC_DW_CLASS_RANGELISTPTR: // TODO
803 ("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
804 form, klass, name == NULL ? "?" : name);
808 if (ns && variable->global)
810 std::string(ns) + "::" + variable->name;
812 // The current code needs a variable name,
813 // generate a fake one:
814 if (variable->name.empty())
816 "@anonymous#" + std::to_string(mc_anonymous_variable_index++);
818 return std::move(variable);
821 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
822 Dwarf_Die * unit, mc_frame_t frame,
825 MC_dwarf_register_variable(info, frame,
826 MC_die_to_variable(info, die, unit, frame, ns));
829 static void MC_dwarf_handle_scope_die(mc_object_info_t info, Dwarf_Die * die,
830 Dwarf_Die * unit, mc_frame_t parent_frame,
833 // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
834 int tag = dwarf_tag(die);
835 mc_tag_class klass = MC_dwarf_tag_classify(tag);
837 // (Template) Subprogram declaration:
838 if (klass == mc_tag_subprogram
839 && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
842 if (klass == mc_tag_scope)
843 xbt_assert(parent_frame, "No parent scope for this scope");
845 simgrid::mc::Frame frame;
848 frame.id = dwarf_dieoffset(die);
849 frame.object_info = info;
851 if (klass == mc_tag_subprogram) {
852 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
854 frame.name = std::string(ns) + "::" + name;
859 frame.abstract_origin_id =
860 MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
862 // This is the base address for DWARF addresses.
863 // Relocated addresses are offset from this base address.
864 // See DWARF4 spec 7.5
865 void *base = info->base_address();
867 // TODO, support DW_AT_ranges
868 uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
869 frame.low_pc = low_pc ? ((char *) base) + low_pc : 0;
872 Dwarf_Attribute attr;
873 if (!dwarf_attr_integrate(die, DW_AT_high_pc, &attr)) {
874 xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
880 switch (MC_dwarf_form_get_class(dwarf_whatform(&attr))) {
882 // DW_AT_high_pc if an offset from the low_pc:
883 case MC_DW_CLASS_CONSTANT:
885 if (dwarf_formsdata(&attr, &offset) != 0)
886 xbt_die("Could not read constant");
887 frame.high_pc = (void *) ((char *) frame.low_pc + offset);
890 // DW_AT_high_pc is a relocatable address:
891 case MC_DW_CLASS_ADDRESS:
892 if (dwarf_formaddr(&attr, &high_pc) != 0)
893 xbt_die("Could not read address");
894 frame.high_pc = ((char *) base) + high_pc;
898 xbt_die("Unexpected class for DW_AT_high_pc");
903 if (klass == mc_tag_subprogram) {
904 Dwarf_Attribute attr_frame_base;
905 if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
906 mc_dwarf_location_list_init(&frame.frame_base, info, die,
911 MC_dwarf_handle_children(info, die, unit, &frame, ns);
913 // Someone needs this to be sorted but who?
914 std::sort(frame.variables.begin(), frame.variables.end(),
915 MC_compare_variable);
918 if (klass == mc_tag_subprogram)
919 info->subprograms[frame.id] = frame;
920 else if (klass == mc_tag_scope)
921 parent_frame->scopes.push_back(std::move(frame));
924 static void mc_dwarf_handle_namespace_die(mc_object_info_t info,
925 Dwarf_Die * die, Dwarf_Die * unit,
929 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
931 xbt_die("Unexpected namespace in a subprogram");
932 char *new_ns = ns == NULL ? xbt_strdup(name)
933 : bprintf("%s::%s", ns, name);
934 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
938 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
939 Dwarf_Die * unit, mc_frame_t frame,
942 // For each child DIE:
945 for (res = dwarf_child(die, &child); res == 0;
946 res = dwarf_siblingof(&child, &child)) {
947 MC_dwarf_handle_die(info, &child, unit, frame, ns);
951 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
952 Dwarf_Die * unit, mc_frame_t frame,
955 int tag = dwarf_tag(die);
956 mc_tag_class klass = MC_dwarf_tag_classify(tag);
961 MC_dwarf_handle_type_die(info, die, unit, frame, ns);
964 // Subprogram or scope:
965 case mc_tag_subprogram:
967 MC_dwarf_handle_scope_die(info, die, unit, frame, ns);
971 case mc_tag_variable:
972 MC_dwarf_handle_variable_die(info, die, unit, frame, ns);
975 case mc_tag_namespace:
976 mc_dwarf_handle_namespace_die(info, die, unit, frame, ns);
985 /** \brief Populate the debugging informations of the given ELF object
987 * Read the DWARf information of the EFFL object and populate the
988 * lists of types, variables, functions.
990 void MC_dwarf_get_variables(mc_object_info_t info)
992 int fd = open(info->file_name.c_str(), O_RDONLY);
994 xbt_die("Could not open file %s", info->file_name.c_str());
995 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
997 xbt_die("Your program must be compiled with -g (%s)",
998 info->file_name.c_str());
999 // For each compilation unit:
1000 Dwarf_Off offset = 0;
1001 Dwarf_Off next_offset = 0;
1003 while (dwarf_nextcu(dwarf, offset, &next_offset, &length, NULL, NULL, NULL) ==
1006 if (dwarf_offdie(dwarf, offset + length, &unit_die) != NULL) {
1008 // For each child DIE:
1011 for (res = dwarf_child(&unit_die, &child); res == 0;
1012 res = dwarf_siblingof(&child, &child)) {
1013 MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
1017 offset = next_offset;
1024 // ***** Functions index
1026 static int MC_compare_frame_index_items(simgrid::mc::FunctionIndexEntry* a,
1027 simgrid::mc::FunctionIndexEntry* b)
1029 if (a->low_pc < b->low_pc)
1031 else if (a->low_pc == b->low_pc)
1037 static void MC_make_functions_index(mc_object_info_t info)
1039 info->functions_index.clear();
1041 for (auto& e : info->subprograms) {
1042 if (e.second.low_pc == nullptr)
1044 simgrid::mc::FunctionIndexEntry entry;
1045 entry.low_pc = e.second.low_pc;
1046 entry.function = &e.second;
1047 info->functions_index.push_back(entry);
1050 info->functions_index.shrink_to_fit();
1052 // Sort the array by low_pc:
1053 std::sort(info->functions_index.begin(), info->functions_index.end(),
1054 [](simgrid::mc::FunctionIndexEntry& a,
1055 simgrid::mc::FunctionIndexEntry& b)
1057 return a.low_pc < b.low_pc;
1061 static void MC_post_process_variables(mc_object_info_t info)
1063 // Someone needs this to be sorted but who?
1064 std::sort(info->global_variables.begin(), info->global_variables.end(),
1065 MC_compare_variable);
1067 for(simgrid::mc::Variable& variable : info->global_variables)
1068 if (variable.type_id) {
1069 auto i = info->types.find(variable.type_id);
1070 if (i != info->types.end())
1071 variable.type = &(i->second);
1073 variable.type = nullptr;
1077 static void mc_post_process_scope(mc_object_info_t info, mc_frame_t scope)
1080 if (scope->tag == DW_TAG_inlined_subroutine) {
1081 // Attach correct namespaced name in inlined subroutine:
1082 auto i = info->subprograms.find(scope->abstract_origin_id);
1083 xbt_assert(i != info->subprograms.end(),
1084 "Could not lookup abstract origin %" PRIx64,
1085 (uint64_t) scope->abstract_origin_id);
1086 scope->name = i->second.name;
1090 for (simgrid::mc::Variable& variable : scope->variables)
1091 if (variable.type_id) {
1092 auto i = info->types.find(variable.type_id);
1093 if (i != info->types.end())
1094 variable.type = &(i->second);
1096 variable.type = nullptr;
1099 // Recursive post-processing of nested-scopes:
1100 for (simgrid::mc::Frame& nested_scope : scope->scopes)
1101 mc_post_process_scope(info, &nested_scope);
1105 static void MC_post_process_functions(mc_object_info_t info)
1107 for (auto& entry : info->subprograms)
1108 mc_post_process_scope(info, &entry.second);
1112 /** \brief Fill/lookup the "subtype" field.
1114 static void MC_resolve_subtype(mc_object_info_t info, mc_type_t type)
1118 auto i = info->types.find(type->type_id);
1119 if (i != info->types.end())
1120 type->subtype = &(i->second);
1122 type->subtype = nullptr;
1125 if (type->subtype->byte_size != 0)
1127 if (type->subtype->name.empty())
1129 // Try to find a more complete description of the type:
1130 // We need to fix in order to support C++.
1132 auto j = info->full_types_by_name.find(type->subtype->name);
1133 if (j != info->full_types_by_name.end())
1134 type->subtype = j->second;
1137 static void MC_post_process_types(mc_object_info_t info)
1139 // Lookup "subtype" field:
1140 for(auto& i : info->types) {
1141 MC_resolve_subtype(info, &(i.second));
1142 for (simgrid::mc::Type& member : i.second.members)
1143 MC_resolve_subtype(info, &member);
1147 /** \brief Finds informations about a given shared object/executable */
1148 std::shared_ptr<s_mc_object_info_t> MC_find_object_info(
1149 std::vector<simgrid::mc::VmMap> const& maps, const char *name, int executable)
1151 std::shared_ptr<s_mc_object_info_t> result =
1152 std::make_shared<s_mc_object_info_t>();
1154 result->flags |= MC_OBJECT_INFO_EXECUTABLE;
1155 result->file_name = name;
1156 MC_find_object_address(maps, result.get());
1157 MC_dwarf_get_variables(result.get());
1158 MC_post_process_variables(result.get());
1159 MC_post_process_types(result.get());
1160 MC_post_process_functions(result.get());
1161 MC_make_functions_index(result.get());
1162 return std::move(result);
1165 /*************************************************************************/
1167 void MC_dwarf_register_variable(
1168 mc_object_info_t info, mc_frame_t frame,
1169 std::unique_ptr<simgrid::mc::Variable> variable)
1173 // Those arrays are sorted later:
1174 else if (variable->global)
1175 info->global_variables.push_back(std::move(*variable));
1176 else if (frame != nullptr)
1177 frame->variables.push_back(std::move(*variable));
1179 xbt_die("No frame for this local variable");
1182 void MC_post_process_object_info(mc_process_t process, mc_object_info_t info)
1184 for (auto& i : info->types) {
1186 mc_type_t type = &(i.second);
1187 mc_type_t subtype = type;
1188 while (subtype->type == DW_TAG_typedef || subtype->type == DW_TAG_volatile_type
1189 || subtype->type == DW_TAG_const_type) {
1190 if (subtype->subtype)
1191 subtype = subtype->subtype;
1196 // Resolve full_type:
1197 if (!subtype->name.empty() && subtype->byte_size == 0) {
1198 for (auto const& object_info : process->object_infos) {
1199 auto i = object_info->full_types_by_name.find(subtype->name);
1200 if (i != object_info->full_types_by_name.end()
1201 && !i->second->name.empty() && i->second->byte_size) {
1202 type->full_type = i->second;
1206 } else type->full_type = subtype;