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::string 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::string MC_dwarf_at_type(Dwarf_Die * die)
314 Dwarf_Off offset = MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
316 return std::string();
317 char* s = bprintf("%" PRIx64, offset);
320 return std::move(res);
323 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die * die, int attribute)
325 Dwarf_Attribute attr;
326 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
329 if (dwarf_formaddr(&attr, &value) == 0)
330 return (uint64_t) value;
335 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die * die, int attribute,
336 uint64_t default_value)
338 Dwarf_Attribute attr;
339 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
340 return default_value;
342 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr),
343 &value) == 0 ? (uint64_t) value : default_value;
346 static bool MC_dwarf_attr_flag(Dwarf_Die * die, int attribute, bool integrate)
348 Dwarf_Attribute attr;
349 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
350 : dwarf_attr(die, attribute, &attr)) == 0)
354 if (dwarf_formflag(&attr, &result))
355 xbt_die("Unexpected form for attribute %s", MC_dwarf_attrname(attribute));
359 /** \brief Find the default lower bound for a given language
361 * The default lower bound of an array (when DW_TAG_lower_bound
362 * is missing) depends on the language of the compilation unit.
364 * \param lang Language of the compilation unit (values defined in the DWARF spec)
365 * \return Default lower bound of an array in this compilation unit
367 static uint64_t MC_dwarf_default_lower_bound(int lang)
373 case DW_LANG_C_plus_plus:
377 case DW_LANG_ObjC_plus_plus:
383 case DW_LANG_Fortran77:
384 case DW_LANG_Fortran90:
385 case DW_LANG_Fortran95:
386 case DW_LANG_Modula2:
387 case DW_LANG_Pascal83:
389 case DW_LANG_Cobol74:
390 case DW_LANG_Cobol85:
393 xbt_die("No default DW_TAG_lower_bound for language %i and none given",
399 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
402 * \param unit DIE of the compilation unit
403 * \return number of elements in the range
405 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
408 xbt_assert(dwarf_tag(die) == DW_TAG_enumeration_type
409 || dwarf_tag(die) == DW_TAG_subrange_type,
410 "MC_dwarf_subrange_element_count called with DIE of type %s",
411 MC_dwarf_die_tagname(die));
413 // Use DW_TAG_count if present:
414 if (dwarf_hasattr_integrate(die, DW_AT_count)) {
415 return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
417 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
419 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
420 // This is not really 0, but the code expects this (we do not know):
423 uint64_t upper_bound =
424 MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
426 uint64_t lower_bound = 0;
427 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
428 lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
430 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
432 return upper_bound - lower_bound + 1;
435 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
437 * The compilation unit might be needed because the default lower
438 * bound depends on the language of the compilation unit.
440 * \param die the DIE of the DW_TAG_array_type
441 * \param unit the DIE of the compilation unit
442 * \return number of elements in this array type
444 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit)
446 xbt_assert(dwarf_tag(die) == DW_TAG_array_type,
447 "MC_dwarf_array_element_count called with DIE of type %s",
448 MC_dwarf_die_tagname(die));
453 for (res = dwarf_child(die, &child); res == 0;
454 res = dwarf_siblingof(&child, &child)) {
455 int child_tag = dwarf_tag(&child);
456 if (child_tag == DW_TAG_subrange_type
457 || child_tag == DW_TAG_enumeration_type) {
458 result *= MC_dwarf_subrange_element_count(&child, unit);
466 /** \brief Initialize the location of a member of a type
467 * (DW_AT_data_member_location of a DW_TAG_member).
469 * \param type a type (struct, class)
470 * \param member the member of the type
471 * \param child DIE of the member (DW_TAG_member)
473 static void MC_dwarf_fill_member_location(mc_type_t type, mc_type_t member,
476 if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
477 xbt_die("Can't groke DW_AT_data_bit_offset.");
480 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
481 if (type->type != DW_TAG_union_type) {
483 ("Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%"
484 PRIx64 ">%s", member->name.c_str(),
485 (uint64_t) type->id, type->name.c_str());
491 Dwarf_Attribute attr;
492 dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
493 int form = dwarf_whatform(&attr);
494 int klass = MC_dwarf_form_get_class(form);
496 case MC_DW_CLASS_EXPRLOC:
497 case MC_DW_CLASS_BLOCK:
498 // Location expression:
502 if (dwarf_getlocation(&attr, &expr, &len)) {
504 ("Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%"
505 PRIx64 ">%s", MC_dwarf_attr_integrate_string(child, DW_AT_name),
506 (uint64_t) type->id, type->name.c_str());
508 simgrid::mc::DwarfExpression(expr, expr+len);
511 case MC_DW_CLASS_CONSTANT:
512 // Offset from the base address of the object:
515 if (!dwarf_formudata(&attr, &offset))
516 member->offset(offset);
518 xbt_die("Cannot get %s location <%" PRIx64 ">%s",
519 MC_dwarf_attr_integrate_string(child, DW_AT_name),
520 (uint64_t) type->id, type->name.c_str());
523 case MC_DW_CLASS_LOCLISTPTR:
524 // Reference to a location list:
526 case MC_DW_CLASS_REFERENCE:
527 // It's supposed to be possible in DWARF2 but I couldn't find its semantic
530 xbt_die("Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
536 static void dw_type_free_voidp(void *t)
538 delete *(mc_type_t*)t;
541 /** \brief Populate the list of members of a type
543 * \param info ELF object containing the type DIE
544 * \param die DIE of the type
545 * \param unit DIE of the compilation unit containing the type DIE
546 * \param type the type
548 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die * die,
549 Dwarf_Die * unit, mc_type_t type)
553 xbt_assert(type->members.empty());
554 for (res = dwarf_child(die, &child); res == 0;
555 res = dwarf_siblingof(&child, &child)) {
556 int tag = dwarf_tag(&child);
557 if (tag == DW_TAG_member || tag == DW_TAG_inheritance) {
559 // Skip declarations:
560 if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
563 // Skip compile time constants:
564 if (dwarf_hasattr(&child, DW_AT_const_value))
567 // TODO, we should use another type (because is is not a type but a member)
568 simgrid::mc::Type member;
572 member.id = dwarf_dieoffset(&child);
574 const char *name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
578 MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
579 member.element_count = -1;
580 member.type_id = MC_dwarf_at_type(&child);
582 if (dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
583 xbt_die("Can't groke DW_AT_data_bit_offset.");
586 MC_dwarf_fill_member_location(type, &member, &child);
588 if (member.type_id.empty()) {
589 xbt_die("Missing type for member %s of <%" PRIx64 ">%s",
591 (uint64_t) type->id, type->name.c_str());
594 type->members.push_back(std::move(member));
599 /** \brief Create a MC type object from a DIE
601 * \param info current object info object
602 * \param DIE (for a given type);
603 * \param unit compilation unit of the current DIE
604 * \return MC representation of the type
606 static mc_type_t MC_dwarf_die_to_type(mc_object_info_t info, Dwarf_Die * die,
607 Dwarf_Die * unit, mc_frame_t frame,
611 mc_type_t type = new simgrid::mc::Type();
613 type->name = std::string();
614 type->element_count = -1;
616 type->type = dwarf_tag(die);
619 type->id = dwarf_dieoffset(die);
621 const char *prefix = "";
622 switch (type->type) {
623 case DW_TAG_structure_type:
626 case DW_TAG_union_type:
629 case DW_TAG_class_type:
636 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
638 char* full_name = ns ? bprintf("%s%s::%s", prefix, ns, name) :
639 bprintf("%s%s", prefix, name);
640 type->name = std::string(full_name);
644 type->type_id = MC_dwarf_at_type(die);
646 // Some compilers do not emit DW_AT_byte_size for pointer_type,
647 // so we fill this. We currently assume that the model-checked process is in
648 // the same architecture..
649 if (type->type == DW_TAG_pointer_type)
650 type->byte_size = sizeof(void*);
652 // Computation of the byte_size;
653 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
654 type->byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
655 else if (type->type == DW_TAG_array_type
656 || type->type == DW_TAG_structure_type
657 || type->type == DW_TAG_class_type) {
659 if (dwarf_aggregate_size(die, &size) == 0) {
660 type->byte_size = size;
664 switch (type->type) {
665 case DW_TAG_array_type:
666 type->element_count = MC_dwarf_array_element_count(die, unit);
667 // TODO, handle DW_byte_stride and (not) DW_bit_stride
670 case DW_TAG_pointer_type:
671 case DW_TAG_reference_type:
672 case DW_TAG_rvalue_reference_type:
673 type->is_pointer_type = 1;
676 case DW_TAG_structure_type:
677 case DW_TAG_union_type:
678 case DW_TAG_class_type:
679 MC_dwarf_add_members(info, die, unit, type);
680 char *new_ns = ns == NULL ? xbt_strdup(type->name.c_str())
681 : bprintf("%s::%s", ns, name);
682 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
690 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
691 Dwarf_Die * unit, mc_frame_t frame,
694 mc_type_t type = MC_dwarf_die_to_type(info, die, unit, frame, ns);
696 char *key = bprintf("%" PRIx64, (uint64_t) type->id);
697 xbt_dict_set(info->types, key, type, NULL);
700 if (!type->name.empty() && type->byte_size != 0) {
701 xbt_dict_set(info->full_types_by_name, type->name.c_str(), type, NULL);
705 static int mc_anonymous_variable_index = 0;
707 static std::unique_ptr<simgrid::mc::Variable> MC_die_to_variable(
708 mc_object_info_t info, Dwarf_Die * die,
709 Dwarf_Die * unit, mc_frame_t frame,
712 // Skip declarations:
713 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
716 // Skip compile time constants:
717 if (dwarf_hasattr(die, DW_AT_const_value))
720 Dwarf_Attribute attr_location;
721 if (dwarf_attr(die, DW_AT_location, &attr_location) == NULL) {
722 // No location: do not add it ?
726 std::unique_ptr<simgrid::mc::Variable> variable =
727 std::unique_ptr<simgrid::mc::Variable>(new simgrid::mc::Variable());
728 variable->dwarf_offset = dwarf_dieoffset(die);
729 variable->global = frame == NULL; // Can be override base on DW_AT_location
730 variable->object_info = info;
732 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
734 variable->name = name;
735 variable->type_id = MC_dwarf_at_type(die);
737 int form = dwarf_whatform(&attr_location);
740 DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
742 case MC_DW_CLASS_EXPRLOC:
743 case MC_DW_CLASS_BLOCK:
744 // Location expression:
748 if (dwarf_getlocation(&attr_location, &expr, &len)) {
750 "Could not read location expression in DW_AT_location "
751 "of variable <%" PRIx64 ">%s",
752 (uint64_t) variable->dwarf_offset,
753 variable->name.c_str());
756 if (len == 1 && expr[0].atom == DW_OP_addr) {
757 variable->global = 1;
758 uintptr_t offset = (uintptr_t) expr[0].number;
759 uintptr_t base = (uintptr_t) info->base_address();
760 variable->address = (void *) (base + offset);
762 simgrid::mc::LocationListEntry entry;
763 entry.expression = {expr, expr + len};
764 variable->location_list = { std::move(entry) };
769 case MC_DW_CLASS_LOCLISTPTR:
770 case MC_DW_CLASS_CONSTANT:
771 // Reference to location list:
772 mc_dwarf_location_list_init(
773 &variable->location_list, info, die,
777 xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location "
778 "in <%" PRIx64 ">%s",
779 form, form, klass, klass,
780 (uint64_t) variable->dwarf_offset,
781 variable->name.c_str());
784 // Handle start_scope:
785 if (dwarf_hasattr(die, DW_AT_start_scope)) {
786 Dwarf_Attribute attr;
787 dwarf_attr(die, DW_AT_start_scope, &attr);
788 int form = dwarf_whatform(&attr);
789 int klass = MC_dwarf_form_get_class(form);
791 case MC_DW_CLASS_CONSTANT:
794 variable->start_scope =
795 dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
798 case MC_DW_CLASS_RANGELISTPTR: // TODO
801 ("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
802 form, klass, name == NULL ? "?" : name);
806 if (ns && variable->global)
808 std::string(ns) + "::" + variable->name;
810 // The current code needs a variable name,
811 // generate a fake one:
812 if (variable->name.empty())
814 "@anonymous#" + std::to_string(mc_anonymous_variable_index++);
816 return std::move(variable);
819 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
820 Dwarf_Die * unit, mc_frame_t frame,
823 MC_dwarf_register_variable(info, frame,
824 MC_die_to_variable(info, die, unit, frame, ns));
827 static void MC_dwarf_handle_scope_die(mc_object_info_t info, Dwarf_Die * die,
828 Dwarf_Die * unit, mc_frame_t parent_frame,
831 // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
832 int tag = dwarf_tag(die);
833 mc_tag_class klass = MC_dwarf_tag_classify(tag);
835 // (Template) Subprogram declaration:
836 if (klass == mc_tag_subprogram
837 && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
840 if (klass == mc_tag_scope)
841 xbt_assert(parent_frame, "No parent scope for this scope");
843 simgrid::mc::Frame frame;
846 frame.id = dwarf_dieoffset(die);
847 frame.object_info = info;
849 if (klass == mc_tag_subprogram) {
850 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
852 frame.name = std::string(ns) + "::" + name;
857 frame.abstract_origin_id =
858 MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
860 // This is the base address for DWARF addresses.
861 // Relocated addresses are offset from this base address.
862 // See DWARF4 spec 7.5
863 void *base = info->base_address();
865 // TODO, support DW_AT_ranges
866 uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
867 frame.low_pc = low_pc ? ((char *) base) + low_pc : 0;
870 Dwarf_Attribute attr;
871 if (!dwarf_attr_integrate(die, DW_AT_high_pc, &attr)) {
872 xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
878 switch (MC_dwarf_form_get_class(dwarf_whatform(&attr))) {
880 // DW_AT_high_pc if an offset from the low_pc:
881 case MC_DW_CLASS_CONSTANT:
883 if (dwarf_formsdata(&attr, &offset) != 0)
884 xbt_die("Could not read constant");
885 frame.high_pc = (void *) ((char *) frame.low_pc + offset);
888 // DW_AT_high_pc is a relocatable address:
889 case MC_DW_CLASS_ADDRESS:
890 if (dwarf_formaddr(&attr, &high_pc) != 0)
891 xbt_die("Could not read address");
892 frame.high_pc = ((char *) base) + high_pc;
896 xbt_die("Unexpected class for DW_AT_high_pc");
901 if (klass == mc_tag_subprogram) {
902 Dwarf_Attribute attr_frame_base;
903 if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
904 mc_dwarf_location_list_init(&frame.frame_base, info, die,
909 MC_dwarf_handle_children(info, die, unit, &frame, ns);
912 if (klass == mc_tag_subprogram) {
913 char *key = bprintf("%" PRIx64, (uint64_t) frame.id);
915 xbt_dict_set(info->subprograms, key,
916 new simgrid::mc::Frame(std::move(frame)), NULL);
918 } else if (klass == mc_tag_scope)
919 parent_frame->scopes.push_back(std::move(frame));
922 static void mc_dwarf_handle_namespace_die(mc_object_info_t info,
923 Dwarf_Die * die, Dwarf_Die * unit,
927 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
929 xbt_die("Unexpected namespace in a subprogram");
930 char *new_ns = ns == NULL ? xbt_strdup(name)
931 : bprintf("%s::%s", ns, name);
932 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
936 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
937 Dwarf_Die * unit, mc_frame_t frame,
940 // For each child DIE:
943 for (res = dwarf_child(die, &child); res == 0;
944 res = dwarf_siblingof(&child, &child)) {
945 MC_dwarf_handle_die(info, &child, unit, frame, ns);
949 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
950 Dwarf_Die * unit, mc_frame_t frame,
953 int tag = dwarf_tag(die);
954 mc_tag_class klass = MC_dwarf_tag_classify(tag);
959 MC_dwarf_handle_type_die(info, die, unit, frame, ns);
962 // Subprogram or scope:
963 case mc_tag_subprogram:
965 MC_dwarf_handle_scope_die(info, die, unit, frame, ns);
969 case mc_tag_variable:
970 MC_dwarf_handle_variable_die(info, die, unit, frame, ns);
973 case mc_tag_namespace:
974 mc_dwarf_handle_namespace_die(info, die, unit, frame, ns);
983 /** \brief Populate the debugging informations of the given ELF object
985 * Read the DWARf information of the EFFL object and populate the
986 * lists of types, variables, functions.
988 void MC_dwarf_get_variables(mc_object_info_t info)
990 int fd = open(info->file_name, O_RDONLY);
992 xbt_die("Could not open file %s", info->file_name);
994 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
996 xbt_die("Your program must be compiled with -g (%s)", info->file_name);
998 // For each compilation unit:
999 Dwarf_Off offset = 0;
1000 Dwarf_Off next_offset = 0;
1002 while (dwarf_nextcu(dwarf, offset, &next_offset, &length, NULL, NULL, NULL) ==
1005 if (dwarf_offdie(dwarf, offset + length, &unit_die) != NULL) {
1007 // For each child DIE:
1010 for (res = dwarf_child(&unit_die, &child); res == 0;
1011 res = dwarf_siblingof(&child, &child)) {
1012 MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
1016 offset = next_offset;
1023 // ***** Functions index
1025 static int MC_compare_frame_index_items(mc_function_index_item_t a,
1026 mc_function_index_item_t b)
1028 if (a->low_pc < b->low_pc)
1030 else if (a->low_pc == b->low_pc)
1036 static void MC_make_functions_index(mc_object_info_t info)
1038 xbt_dynar_t index = xbt_dynar_new(sizeof(s_mc_function_index_item_t), NULL);
1040 // Populate the array:
1041 mc_frame_t frame = NULL;
1042 xbt_dict_cursor_t cursor;
1044 xbt_dict_foreach(info->subprograms, cursor, key, frame) {
1045 if (frame->low_pc == NULL)
1047 s_mc_function_index_item_t entry;
1048 entry.low_pc = frame->low_pc;
1049 entry.high_pc = frame->high_pc;
1050 entry.function = frame;
1051 xbt_dynar_push(index, &entry);
1054 mc_function_index_item_t base =
1055 (mc_function_index_item_t) xbt_dynar_get_ptr(index, 0);
1057 // Sort the array by low_pc:
1059 xbt_dynar_length(index),
1060 sizeof(s_mc_function_index_item_t),
1061 (int (*)(const void *, const void *)) MC_compare_frame_index_items);
1063 info->functions_index = index;
1066 static void MC_post_process_variables(mc_object_info_t info)
1068 for(simgrid::mc::Variable& variable : info->global_variables)
1069 if (!variable.type_id.empty())
1070 variable.type = (mc_type_t) xbt_dict_get_or_null(
1071 info->types, variable.type_id.c_str());
1074 static void mc_post_process_scope(mc_object_info_t info, mc_frame_t scope)
1077 if (scope->tag == DW_TAG_inlined_subroutine) {
1079 // Attach correct namespaced name in inlined subroutine:
1080 char *key = bprintf("%" PRIx64, (uint64_t) scope->abstract_origin_id);
1081 mc_frame_t abstract_origin = (mc_frame_t) xbt_dict_get_or_null(info->subprograms, key);
1082 xbt_assert(abstract_origin, "Could not lookup abstract origin %s", key);
1084 scope->name = abstract_origin->name;
1088 for (simgrid::mc::Variable& variable : scope->variables)
1089 if (!variable.type_id.empty())
1090 variable.type = (mc_type_t) xbt_dict_get_or_null(
1091 info->types, variable.type_id.c_str());
1093 // Recursive post-processing of nested-scopes:
1094 for (simgrid::mc::Frame& nested_scope : scope->scopes)
1095 mc_post_process_scope(info, &nested_scope);
1099 static void MC_post_process_functions(mc_object_info_t info)
1101 xbt_dict_cursor_t cursor;
1103 mc_frame_t subprogram = NULL;
1104 xbt_dict_foreach(info->subprograms, cursor, key, subprogram) {
1105 mc_post_process_scope(info, subprogram);
1110 /** \brief Fill/lookup the "subtype" field.
1112 static void MC_resolve_subtype(mc_object_info_t info, mc_type_t type)
1115 if (type->type_id.empty())
1117 type->subtype = (mc_type_t) xbt_dict_get_or_null(
1118 info->types, type->type_id.c_str());
1119 if (type->subtype == NULL)
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++.
1129 (mc_type_t) xbt_dict_get_or_null(
1130 info->full_types_by_name, type->subtype->name.c_str());
1131 if (subtype != NULL) {
1132 type->subtype = subtype;
1137 static void MC_post_process_types(mc_object_info_t info)
1139 xbt_dict_cursor_t cursor = NULL;
1143 // Lookup "subtype" field:
1144 xbt_dict_foreach(info->types, cursor, origin, type) {
1145 MC_resolve_subtype(info, type);
1146 for (simgrid::mc::Type& member : type->members)
1147 MC_resolve_subtype(info, &member);
1151 /** \brief Finds informations about a given shared object/executable */
1152 std::shared_ptr<s_mc_object_info_t> MC_find_object_info(
1153 std::vector<simgrid::mc::VmMap> const& maps, const char *name, int executable)
1155 std::shared_ptr<s_mc_object_info_t> result =
1156 std::make_shared<s_mc_object_info_t>();
1158 result->flags |= MC_OBJECT_INFO_EXECUTABLE;
1159 result->file_name = xbt_strdup(name);
1160 MC_find_object_address(maps, result.get());
1161 MC_dwarf_get_variables(result.get());
1162 MC_post_process_types(result.get());
1163 MC_post_process_variables(result.get());
1164 MC_post_process_functions(result.get());
1165 MC_make_functions_index(result.get());
1166 return std::move(result);
1169 /*************************************************************************/
1171 static int MC_dwarf_get_variable_index(
1172 std::vector<simgrid::mc::Variable> variables, const char *var, void *address)
1175 if (variables.empty())
1178 unsigned int cursor = 0;
1180 int end = variables.size() - 1;
1181 mc_variable_t var_test = nullptr;
1183 while (start <= end) {
1184 cursor = (start + end) / 2;
1185 var_test = &variables[cursor];
1186 if (strcmp(var_test->name.c_str(), var) < 0) {
1188 } else if (strcmp(var_test->name.c_str(), var) > 0) {
1191 if (address) { /* global variable */
1192 if (var_test->address == address)
1194 if (var_test->address > address)
1198 } else { /* local variable */
1204 if (strcmp(var_test->name.c_str(), var) == 0) {
1205 if (address && var_test->address < address)
1209 } else if (strcmp(var_test->name.c_str(), var) < 0)
1216 void MC_dwarf_register_global_variable(
1217 mc_object_info_t info,
1218 std::unique_ptr<simgrid::mc::Variable> variable)
1221 MC_dwarf_get_variable_index(info->global_variables,
1222 variable->name.c_str(),
1225 info->global_variables.insert(
1226 info->global_variables.begin() + index, std::move(*variable));
1230 void MC_dwarf_register_non_global_variable(
1231 mc_object_info_t info,
1233 std::unique_ptr<simgrid::mc::Variable> variable)
1235 xbt_assert(frame, "Frame is NULL");
1237 MC_dwarf_get_variable_index(
1238 frame->variables, variable->name.c_str(), NULL);
1240 frame->variables.insert(
1241 frame->variables.begin() + index, std::move(*variable));
1245 void MC_dwarf_register_variable(
1246 mc_object_info_t info, mc_frame_t frame,
1247 std::unique_ptr<simgrid::mc::Variable> variable)
1251 if (variable->global)
1252 MC_dwarf_register_global_variable(info, std::move(variable));
1253 else if (frame != nullptr)
1254 MC_dwarf_register_non_global_variable(info, frame, std::move(variable));
1256 xbt_die("No frame for this local variable");
1259 void MC_post_process_object_info(mc_process_t process, mc_object_info_t info)
1261 xbt_dict_cursor_t cursor = NULL;
1263 mc_type_t type = NULL;
1264 xbt_dict_foreach(info->types, cursor, key, type) {
1266 mc_type_t subtype = type;
1267 while (subtype->type == DW_TAG_typedef || subtype->type == DW_TAG_volatile_type
1268 || subtype->type == DW_TAG_const_type) {
1269 if (subtype->subtype)
1270 subtype = subtype->subtype;
1275 // Resolve full_type:
1276 if (!subtype->name.empty() && subtype->byte_size == 0) {
1277 for (auto const& object_info : process->object_infos) {
1278 mc_type_t same_type = (mc_type_t)
1279 xbt_dict_get_or_null(object_info->full_types_by_name,
1280 subtype->name.c_str());
1281 if (same_type && !same_type->name.empty() && same_type->byte_size) {
1282 type->full_type = same_type;
1286 } else type->full_type = subtype;