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. */
12 #define DW_LANG_Objc DW_LANG_ObjC /* fix spelling error in older dwarf.h */
14 #include <elfutils/libdw.h>
16 #include <simgrid_config.h>
18 #include <xbt/sysdep.h>
20 #include "mc_object_info.h"
21 #include "mc_private.h"
23 static void MC_dwarf_register_global_variable(
24 mc_object_info_t info, std::unique_ptr<simgrid::mc::Variable> variable);
25 static void MC_register_variable(
26 mc_object_info_t info, mc_frame_t frame, std::unique_ptr<simgrid::mc::Variable> variable);
27 static void MC_dwarf_register_non_global_variable(mc_object_info_t info, mc_frame_t frame, mc_variable_t variable);
28 static void MC_dwarf_register_variable(
29 mc_object_info_t info, mc_frame_t frame,
30 std::unique_ptr<simgrid::mc::Variable> variable);
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(mc_object_info_t info, Dwarf_Die * die,
70 Dwarf_Die * unit, mc_frame_t frame,
73 /** \brief Process a type DIE
75 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
76 Dwarf_Die * unit, mc_frame_t 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(mc_object_info_t info, Dwarf_Die * die,
87 Dwarf_Die * unit, mc_frame_t 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(mc_object_info_t info, Dwarf_Die * die,
98 Dwarf_Die * unit, mc_frame_t 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)) {
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");
286 return dwarf_dieoffset(&subtype_die);
291 static Dwarf_Off MC_dwarf_attr_integrate_dieoffset(Dwarf_Die * die,
294 Dwarf_Attribute attr;
295 if (dwarf_hasattr_integrate(die, attribute)) {
296 dwarf_attr_integrate(die, DW_AT_type, &attr);
297 Dwarf_Die subtype_die;
298 if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
299 xbt_die("Could not find DIE");
301 return dwarf_dieoffset(&subtype_die);
306 /** \brief Find the type/subtype (DW_AT_type) for a DIE
309 * \return DW_AT_type reference as a global offset in hexadecimal (or NULL)
312 std::uint64_t MC_dwarf_at_type(Dwarf_Die * die)
314 return MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
317 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die * die, int attribute)
319 Dwarf_Attribute attr;
320 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
323 if (dwarf_formaddr(&attr, &value) == 0)
324 return (uint64_t) value;
329 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die * die, int attribute,
330 uint64_t default_value)
332 Dwarf_Attribute attr;
333 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
334 return default_value;
336 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr),
337 &value) == 0 ? (uint64_t) value : default_value;
340 static bool MC_dwarf_attr_flag(Dwarf_Die * die, int attribute, bool integrate)
342 Dwarf_Attribute attr;
343 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
344 : dwarf_attr(die, attribute, &attr)) == 0)
348 if (dwarf_formflag(&attr, &result))
349 xbt_die("Unexpected form for attribute %s", MC_dwarf_attrname(attribute));
353 /** \brief Find the default lower bound for a given language
355 * The default lower bound of an array (when DW_TAG_lower_bound
356 * is missing) depends on the language of the compilation unit.
358 * \param lang Language of the compilation unit (values defined in the DWARF spec)
359 * \return Default lower bound of an array in this compilation unit
361 static uint64_t MC_dwarf_default_lower_bound(int lang)
367 case DW_LANG_C_plus_plus:
371 case DW_LANG_ObjC_plus_plus:
377 case DW_LANG_Fortran77:
378 case DW_LANG_Fortran90:
379 case DW_LANG_Fortran95:
380 case DW_LANG_Modula2:
381 case DW_LANG_Pascal83:
383 case DW_LANG_Cobol74:
384 case DW_LANG_Cobol85:
387 xbt_die("No default DW_TAG_lower_bound for language %i and none given",
393 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
396 * \param unit DIE of the compilation unit
397 * \return number of elements in the range
399 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
402 xbt_assert(dwarf_tag(die) == DW_TAG_enumeration_type
403 || dwarf_tag(die) == DW_TAG_subrange_type,
404 "MC_dwarf_subrange_element_count called with DIE of type %s",
405 MC_dwarf_die_tagname(die));
407 // Use DW_TAG_count if present:
408 if (dwarf_hasattr_integrate(die, DW_AT_count)) {
409 return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
411 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
413 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
414 // This is not really 0, but the code expects this (we do not know):
417 uint64_t upper_bound =
418 MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
420 uint64_t lower_bound = 0;
421 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
422 lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
424 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
426 return upper_bound - lower_bound + 1;
429 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
431 * The compilation unit might be needed because the default lower
432 * bound depends on the language of the compilation unit.
434 * \param die the DIE of the DW_TAG_array_type
435 * \param unit the DIE of the compilation unit
436 * \return number of elements in this array type
438 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit)
440 xbt_assert(dwarf_tag(die) == DW_TAG_array_type,
441 "MC_dwarf_array_element_count called with DIE of type %s",
442 MC_dwarf_die_tagname(die));
447 for (res = dwarf_child(die, &child); res == 0;
448 res = dwarf_siblingof(&child, &child)) {
449 int child_tag = dwarf_tag(&child);
450 if (child_tag == DW_TAG_subrange_type
451 || child_tag == DW_TAG_enumeration_type) {
452 result *= MC_dwarf_subrange_element_count(&child, unit);
460 /** \brief Initialize the location of a member of a type
461 * (DW_AT_data_member_location of a DW_TAG_member).
463 * \param type a type (struct, class)
464 * \param member the member of the type
465 * \param child DIE of the member (DW_TAG_member)
467 static void MC_dwarf_fill_member_location(mc_type_t type, mc_type_t member,
470 if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
471 xbt_die("Can't groke DW_AT_data_bit_offset.");
474 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
475 if (type->type != DW_TAG_union_type) {
477 ("Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%"
478 PRIx64 ">%s", member->name.c_str(),
479 (uint64_t) type->id, type->name.c_str());
485 Dwarf_Attribute attr;
486 dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
487 int form = dwarf_whatform(&attr);
488 int klass = MC_dwarf_form_get_class(form);
490 case MC_DW_CLASS_EXPRLOC:
491 case MC_DW_CLASS_BLOCK:
492 // Location expression:
496 if (dwarf_getlocation(&attr, &expr, &len)) {
498 ("Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%"
499 PRIx64 ">%s", MC_dwarf_attr_integrate_string(child, DW_AT_name),
500 (uint64_t) type->id, type->name.c_str());
502 simgrid::mc::DwarfExpression(expr, expr+len);
505 case MC_DW_CLASS_CONSTANT:
506 // Offset from the base address of the object:
509 if (!dwarf_formudata(&attr, &offset))
510 member->offset(offset);
512 xbt_die("Cannot get %s location <%" PRIx64 ">%s",
513 MC_dwarf_attr_integrate_string(child, DW_AT_name),
514 (uint64_t) type->id, type->name.c_str());
517 case MC_DW_CLASS_LOCLISTPTR:
518 // Reference to a location list:
520 case MC_DW_CLASS_REFERENCE:
521 // It's supposed to be possible in DWARF2 but I couldn't find its semantic
524 xbt_die("Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
530 static void dw_type_free_voidp(void *t)
532 delete *(mc_type_t*)t;
535 /** \brief Populate the list of members of a type
537 * \param info ELF object containing the type DIE
538 * \param die DIE of the type
539 * \param unit DIE of the compilation unit containing the type DIE
540 * \param type the type
542 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die * die,
543 Dwarf_Die * unit, mc_type_t type)
547 xbt_assert(type->members.empty());
548 for (res = dwarf_child(die, &child); res == 0;
549 res = dwarf_siblingof(&child, &child)) {
550 int tag = dwarf_tag(&child);
551 if (tag == DW_TAG_member || tag == DW_TAG_inheritance) {
553 // Skip declarations:
554 if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
557 // Skip compile time constants:
558 if (dwarf_hasattr(&child, DW_AT_const_value))
561 // TODO, we should use another type (because is is not a type but a member)
562 simgrid::mc::Type member;
566 member.id = dwarf_dieoffset(&child);
568 const char *name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
572 MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
573 member.element_count = -1;
574 member.type_id = MC_dwarf_at_type(&child);
576 if (dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
577 xbt_die("Can't groke DW_AT_data_bit_offset.");
580 MC_dwarf_fill_member_location(type, &member, &child);
582 if (!member.type_id) {
583 xbt_die("Missing type for member %s of <%" PRIx64 ">%s",
585 (uint64_t) type->id, type->name.c_str());
588 type->members.push_back(std::move(member));
593 /** \brief Create a MC type object from a DIE
595 * \param info current object info object
596 * \param DIE (for a given type);
597 * \param unit compilation unit of the current DIE
598 * \return MC representation of the type
600 static simgrid::mc::Type MC_dwarf_die_to_type(
601 mc_object_info_t info, Dwarf_Die * die,
602 Dwarf_Die * unit, mc_frame_t frame,
606 simgrid::mc::Type type;
608 type.name = std::string();
609 type.element_count = -1;
611 type.type = dwarf_tag(die);
614 type.id = dwarf_dieoffset(die);
616 const char *prefix = "";
618 case DW_TAG_structure_type:
621 case DW_TAG_union_type:
624 case DW_TAG_class_type:
631 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
633 char* full_name = ns ? bprintf("%s%s::%s", prefix, ns, name) :
634 bprintf("%s%s", prefix, name);
635 type.name = std::string(full_name);
639 type.type_id = MC_dwarf_at_type(die);
641 // Some compilers do not emit DW_AT_byte_size for pointer_type,
642 // so we fill this. We currently assume that the model-checked process is in
643 // the same architecture..
644 if (type.type == DW_TAG_pointer_type)
645 type.byte_size = sizeof(void*);
647 // Computation of the byte_size;
648 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
649 type.byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
650 else if (type.type == DW_TAG_array_type
651 || type.type == DW_TAG_structure_type
652 || type.type == DW_TAG_class_type) {
654 if (dwarf_aggregate_size(die, &size) == 0) {
655 type.byte_size = size;
660 case DW_TAG_array_type:
661 type.element_count = MC_dwarf_array_element_count(die, unit);
662 // TODO, handle DW_byte_stride and (not) DW_bit_stride
665 case DW_TAG_pointer_type:
666 case DW_TAG_reference_type:
667 case DW_TAG_rvalue_reference_type:
668 type.is_pointer_type = 1;
671 case DW_TAG_structure_type:
672 case DW_TAG_union_type:
673 case DW_TAG_class_type:
674 MC_dwarf_add_members(info, die, unit, &type);
675 char *new_ns = ns == NULL ? xbt_strdup(type.name.c_str())
676 : bprintf("%s::%s", ns, name);
677 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
682 return std::move(type);
685 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
686 Dwarf_Die * unit, mc_frame_t frame,
689 simgrid::mc::Type type = MC_dwarf_die_to_type(info, die, unit, frame, ns);
690 auto& t = (info->types[type.id] = std::move(type));
691 if (!t.name.empty() && type.byte_size != 0)
692 info->full_types_by_name[t.name] = &t;
695 static int mc_anonymous_variable_index = 0;
697 static std::unique_ptr<simgrid::mc::Variable> MC_die_to_variable(
698 mc_object_info_t info, Dwarf_Die * die,
699 Dwarf_Die * unit, mc_frame_t frame,
702 // Skip declarations:
703 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
706 // Skip compile time constants:
707 if (dwarf_hasattr(die, DW_AT_const_value))
710 Dwarf_Attribute attr_location;
711 if (dwarf_attr(die, DW_AT_location, &attr_location) == NULL) {
712 // No location: do not add it ?
716 std::unique_ptr<simgrid::mc::Variable> variable =
717 std::unique_ptr<simgrid::mc::Variable>(new simgrid::mc::Variable());
718 variable->dwarf_offset = dwarf_dieoffset(die);
719 variable->global = frame == NULL; // Can be override base on DW_AT_location
720 variable->object_info = info;
722 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
724 variable->name = name;
725 variable->type_id = MC_dwarf_at_type(die);
727 int form = dwarf_whatform(&attr_location);
730 DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
732 case MC_DW_CLASS_EXPRLOC:
733 case MC_DW_CLASS_BLOCK:
734 // Location expression:
738 if (dwarf_getlocation(&attr_location, &expr, &len)) {
740 "Could not read location expression in DW_AT_location "
741 "of variable <%" PRIx64 ">%s",
742 (uint64_t) variable->dwarf_offset,
743 variable->name.c_str());
746 if (len == 1 && expr[0].atom == DW_OP_addr) {
747 variable->global = 1;
748 uintptr_t offset = (uintptr_t) expr[0].number;
749 uintptr_t base = (uintptr_t) info->base_address();
750 variable->address = (void *) (base + offset);
752 simgrid::mc::LocationListEntry entry;
753 entry.expression = {expr, expr + len};
754 variable->location_list = { std::move(entry) };
759 case MC_DW_CLASS_LOCLISTPTR:
760 case MC_DW_CLASS_CONSTANT:
761 // Reference to location list:
762 mc_dwarf_location_list_init(
763 &variable->location_list, info, die,
767 xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location "
768 "in <%" PRIx64 ">%s",
769 form, form, klass, klass,
770 (uint64_t) variable->dwarf_offset,
771 variable->name.c_str());
774 // Handle start_scope:
775 if (dwarf_hasattr(die, DW_AT_start_scope)) {
776 Dwarf_Attribute attr;
777 dwarf_attr(die, DW_AT_start_scope, &attr);
778 int form = dwarf_whatform(&attr);
779 int klass = MC_dwarf_form_get_class(form);
781 case MC_DW_CLASS_CONSTANT:
784 variable->start_scope =
785 dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
788 case MC_DW_CLASS_RANGELISTPTR: // TODO
791 ("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
792 form, klass, name == NULL ? "?" : name);
796 if (ns && variable->global)
798 std::string(ns) + "::" + variable->name;
800 // The current code needs a variable name,
801 // generate a fake one:
802 if (variable->name.empty())
804 "@anonymous#" + std::to_string(mc_anonymous_variable_index++);
806 return std::move(variable);
809 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
810 Dwarf_Die * unit, mc_frame_t frame,
813 MC_dwarf_register_variable(info, frame,
814 MC_die_to_variable(info, die, unit, frame, ns));
817 static void MC_dwarf_handle_scope_die(mc_object_info_t info, Dwarf_Die * die,
818 Dwarf_Die * unit, mc_frame_t 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;
847 frame.abstract_origin_id =
848 MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
850 // This is the base address for DWARF addresses.
851 // Relocated addresses are offset from this base address.
852 // See DWARF4 spec 7.5
853 void *base = info->base_address();
855 // TODO, support DW_AT_ranges
856 uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
857 frame.low_pc = low_pc ? ((char *) base) + low_pc : 0;
860 Dwarf_Attribute attr;
861 if (!dwarf_attr_integrate(die, DW_AT_high_pc, &attr)) {
862 xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
868 switch (MC_dwarf_form_get_class(dwarf_whatform(&attr))) {
870 // DW_AT_high_pc if an offset from the low_pc:
871 case MC_DW_CLASS_CONSTANT:
873 if (dwarf_formsdata(&attr, &offset) != 0)
874 xbt_die("Could not read constant");
875 frame.high_pc = (void *) ((char *) frame.low_pc + offset);
878 // DW_AT_high_pc is a relocatable address:
879 case MC_DW_CLASS_ADDRESS:
880 if (dwarf_formaddr(&attr, &high_pc) != 0)
881 xbt_die("Could not read address");
882 frame.high_pc = ((char *) base) + high_pc;
886 xbt_die("Unexpected class for DW_AT_high_pc");
891 if (klass == mc_tag_subprogram) {
892 Dwarf_Attribute attr_frame_base;
893 if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
894 mc_dwarf_location_list_init(&frame.frame_base, info, die,
899 MC_dwarf_handle_children(info, die, unit, &frame, ns);
902 if (klass == mc_tag_subprogram) {
903 char *key = bprintf("%" PRIx64, (uint64_t) frame.id);
905 xbt_dict_set(info->subprograms, key,
906 new simgrid::mc::Frame(std::move(frame)), NULL);
908 } else if (klass == mc_tag_scope)
909 parent_frame->scopes.push_back(std::move(frame));
912 static void mc_dwarf_handle_namespace_die(mc_object_info_t info,
913 Dwarf_Die * die, Dwarf_Die * unit,
917 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
919 xbt_die("Unexpected namespace in a subprogram");
920 char *new_ns = ns == NULL ? xbt_strdup(name)
921 : bprintf("%s::%s", ns, name);
922 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
926 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
927 Dwarf_Die * unit, mc_frame_t frame,
930 // For each child DIE:
933 for (res = dwarf_child(die, &child); res == 0;
934 res = dwarf_siblingof(&child, &child)) {
935 MC_dwarf_handle_die(info, &child, unit, frame, ns);
939 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
940 Dwarf_Die * unit, mc_frame_t frame,
943 int tag = dwarf_tag(die);
944 mc_tag_class klass = MC_dwarf_tag_classify(tag);
949 MC_dwarf_handle_type_die(info, die, unit, frame, ns);
952 // Subprogram or scope:
953 case mc_tag_subprogram:
955 MC_dwarf_handle_scope_die(info, die, unit, frame, ns);
959 case mc_tag_variable:
960 MC_dwarf_handle_variable_die(info, die, unit, frame, ns);
963 case mc_tag_namespace:
964 mc_dwarf_handle_namespace_die(info, die, unit, frame, ns);
973 /** \brief Populate the debugging informations of the given ELF object
975 * Read the DWARf information of the EFFL object and populate the
976 * lists of types, variables, functions.
978 void MC_dwarf_get_variables(mc_object_info_t info)
980 int fd = open(info->file_name, O_RDONLY);
982 xbt_die("Could not open file %s", info->file_name);
984 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
986 xbt_die("Your program must be compiled with -g (%s)", info->file_name);
988 // For each compilation unit:
989 Dwarf_Off offset = 0;
990 Dwarf_Off next_offset = 0;
992 while (dwarf_nextcu(dwarf, offset, &next_offset, &length, NULL, NULL, NULL) ==
995 if (dwarf_offdie(dwarf, offset + length, &unit_die) != NULL) {
997 // For each child DIE:
1000 for (res = dwarf_child(&unit_die, &child); res == 0;
1001 res = dwarf_siblingof(&child, &child)) {
1002 MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
1006 offset = next_offset;
1013 // ***** Functions index
1015 static int MC_compare_frame_index_items(mc_function_index_item_t a,
1016 mc_function_index_item_t b)
1018 if (a->low_pc < b->low_pc)
1020 else if (a->low_pc == b->low_pc)
1026 static void MC_make_functions_index(mc_object_info_t info)
1028 xbt_dynar_t index = xbt_dynar_new(sizeof(s_mc_function_index_item_t), NULL);
1030 // Populate the array:
1031 mc_frame_t frame = NULL;
1032 xbt_dict_cursor_t cursor;
1034 xbt_dict_foreach(info->subprograms, cursor, key, frame) {
1035 if (frame->low_pc == NULL)
1037 s_mc_function_index_item_t entry;
1038 entry.low_pc = frame->low_pc;
1039 entry.high_pc = frame->high_pc;
1040 entry.function = frame;
1041 xbt_dynar_push(index, &entry);
1044 mc_function_index_item_t base =
1045 (mc_function_index_item_t) xbt_dynar_get_ptr(index, 0);
1047 // Sort the array by low_pc:
1049 xbt_dynar_length(index),
1050 sizeof(s_mc_function_index_item_t),
1051 (int (*)(const void *, const void *)) MC_compare_frame_index_items);
1053 info->functions_index = index;
1056 static void MC_post_process_variables(mc_object_info_t info)
1058 for(simgrid::mc::Variable& variable : info->global_variables)
1059 if (variable.type_id) {
1060 auto i = info->types.find(variable.type_id);
1061 if (i != info->types.end())
1062 variable.type = &(i->second);
1064 variable.type = nullptr;
1068 static void mc_post_process_scope(mc_object_info_t info, mc_frame_t scope)
1071 if (scope->tag == DW_TAG_inlined_subroutine) {
1073 // Attach correct namespaced name in inlined subroutine:
1074 char *key = bprintf("%" PRIx64, (uint64_t) scope->abstract_origin_id);
1075 mc_frame_t abstract_origin = (mc_frame_t) xbt_dict_get_or_null(info->subprograms, key);
1076 xbt_assert(abstract_origin, "Could not lookup abstract origin %s", key);
1078 scope->name = abstract_origin->name;
1082 for (simgrid::mc::Variable& variable : scope->variables)
1083 if (variable.type_id) {
1084 auto i = info->types.find(variable.type_id);
1085 if (i != info->types.end())
1086 variable.type = &(i->second);
1088 variable.type = nullptr;
1091 // Recursive post-processing of nested-scopes:
1092 for (simgrid::mc::Frame& nested_scope : scope->scopes)
1093 mc_post_process_scope(info, &nested_scope);
1097 static void MC_post_process_functions(mc_object_info_t info)
1099 xbt_dict_cursor_t cursor;
1101 mc_frame_t subprogram = NULL;
1102 xbt_dict_foreach(info->subprograms, cursor, key, subprogram) {
1103 mc_post_process_scope(info, subprogram);
1108 /** \brief Fill/lookup the "subtype" field.
1110 static void MC_resolve_subtype(mc_object_info_t info, mc_type_t type)
1114 auto i = info->types.find(type->type_id);
1115 if (i != info->types.end())
1116 type->subtype = &(i->second);
1118 type->subtype = nullptr;
1121 if (type->subtype->byte_size != 0)
1123 if (type->subtype->name.empty())
1125 // Try to find a more complete description of the type:
1126 // We need to fix in order to support C++.
1128 auto j = info->full_types_by_name.find(type->subtype->name);
1129 if (j != info->full_types_by_name.end())
1130 type->subtype = j->second;
1133 static void MC_post_process_types(mc_object_info_t info)
1135 // Lookup "subtype" field:
1136 for(auto& i : info->types) {
1137 MC_resolve_subtype(info, &(i.second));
1138 for (simgrid::mc::Type& member : i.second.members)
1139 MC_resolve_subtype(info, &member);
1143 /** \brief Finds informations about a given shared object/executable */
1144 std::shared_ptr<s_mc_object_info_t> MC_find_object_info(
1145 std::vector<simgrid::mc::VmMap> const& maps, const char *name, int executable)
1147 std::shared_ptr<s_mc_object_info_t> result =
1148 std::make_shared<s_mc_object_info_t>();
1150 result->flags |= MC_OBJECT_INFO_EXECUTABLE;
1151 result->file_name = xbt_strdup(name);
1152 MC_find_object_address(maps, result.get());
1153 MC_dwarf_get_variables(result.get());
1154 MC_post_process_types(result.get());
1155 MC_post_process_variables(result.get());
1156 MC_post_process_functions(result.get());
1157 MC_make_functions_index(result.get());
1158 return std::move(result);
1161 /*************************************************************************/
1163 static int MC_dwarf_get_variable_index(
1164 std::vector<simgrid::mc::Variable> variables, const char *var, void *address)
1167 if (variables.empty())
1170 unsigned int cursor = 0;
1172 int end = variables.size() - 1;
1173 mc_variable_t var_test = nullptr;
1175 while (start <= end) {
1176 cursor = (start + end) / 2;
1177 var_test = &variables[cursor];
1178 if (strcmp(var_test->name.c_str(), var) < 0) {
1180 } else if (strcmp(var_test->name.c_str(), var) > 0) {
1183 if (address) { /* global variable */
1184 if (var_test->address == address)
1186 if (var_test->address > address)
1190 } else { /* local variable */
1196 if (strcmp(var_test->name.c_str(), var) == 0) {
1197 if (address && var_test->address < address)
1201 } else if (strcmp(var_test->name.c_str(), var) < 0)
1208 void MC_dwarf_register_global_variable(
1209 mc_object_info_t info,
1210 std::unique_ptr<simgrid::mc::Variable> variable)
1213 MC_dwarf_get_variable_index(info->global_variables,
1214 variable->name.c_str(),
1217 info->global_variables.insert(
1218 info->global_variables.begin() + index, std::move(*variable));
1222 void MC_dwarf_register_non_global_variable(
1223 mc_object_info_t info,
1225 std::unique_ptr<simgrid::mc::Variable> variable)
1227 xbt_assert(frame, "Frame is NULL");
1229 MC_dwarf_get_variable_index(
1230 frame->variables, variable->name.c_str(), NULL);
1232 frame->variables.insert(
1233 frame->variables.begin() + index, std::move(*variable));
1237 void MC_dwarf_register_variable(
1238 mc_object_info_t info, mc_frame_t frame,
1239 std::unique_ptr<simgrid::mc::Variable> variable)
1243 if (variable->global)
1244 MC_dwarf_register_global_variable(info, std::move(variable));
1245 else if (frame != nullptr)
1246 MC_dwarf_register_non_global_variable(info, frame, std::move(variable));
1248 xbt_die("No frame for this local variable");
1251 void MC_post_process_object_info(mc_process_t process, mc_object_info_t info)
1253 for (auto& i : info->types) {
1255 mc_type_t type = &(i.second);
1256 mc_type_t subtype = type;
1257 while (subtype->type == DW_TAG_typedef || subtype->type == DW_TAG_volatile_type
1258 || subtype->type == DW_TAG_const_type) {
1259 if (subtype->subtype)
1260 subtype = subtype->subtype;
1265 // Resolve full_type:
1266 if (!subtype->name.empty() && subtype->byte_size == 0) {
1267 for (auto const& object_info : process->object_infos) {
1268 auto i = object_info->full_types_by_name.find(subtype->name);
1269 if (i != object_info->full_types_by_name.end()
1270 && !i->second->name.empty() && i->second->byte_size) {
1271 type->full_type = i->second;
1275 } else type->full_type = subtype;