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. */
10 #define DW_LANG_Objc DW_LANG_ObjC /* fix spelling error in older dwarf.h */
12 #include <elfutils/libdw.h>
14 #include <simgrid_config.h>
16 #include <xbt/sysdep.h>
18 #include "mc_object_info.h"
19 #include "mc_private.h"
21 static void mc_variable_free_voidp(void *t)
23 delete *(simgrid::mc::Variable**)t;
26 static void mc_frame_free(void* frame)
28 delete (simgrid::mc::Frame*)frame;
31 static void MC_dwarf_register_global_variable(mc_object_info_t info, mc_variable_t variable);
32 static void MC_register_variable(mc_object_info_t info, mc_frame_t frame, mc_variable_t variable);
33 static void MC_dwarf_register_non_global_variable(mc_object_info_t info, mc_frame_t frame, mc_variable_t variable);
34 static void MC_dwarf_register_variable(mc_object_info_t info, mc_frame_t frame, mc_variable_t variable);
36 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing");
38 /** \brief The default DW_TAG_lower_bound for a given DW_AT_language.
40 * The default for a given language is defined in the DWARF spec.
42 * \param language consant as defined by the DWARf spec
44 static uint64_t MC_dwarf_default_lower_bound(int lang);
46 /** \brief Computes the the element_count of a DW_TAG_enumeration_type DIE
48 * This is the number of elements in a given array dimension.
50 * A reference of the compilation unit (DW_TAG_compile_unit) is
51 * needed because the default lower bound (when there is no DW_AT_lower_bound)
52 * depends of the language of the compilation unit (DW_AT_language).
54 * \param die DIE for the DW_TAG_enumeration_type or DW_TAG_subrange_type
55 * \param unit DIE of the DW_TAG_compile_unit
57 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
60 /** \brief Computes the number of elements of a given DW_TAG_array_type.
62 * \param die DIE for the DW_TAG_array_type
64 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit);
66 /** \brief Process a DIE
68 * \param info the resulting object fot the library/binary file (output)
69 * \param die the current DIE
70 * \param unit the DIE of the compile unit of the current DIE
71 * \param frame containg frame if any
73 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
74 Dwarf_Die * unit, mc_frame_t frame,
77 /** \brief Process a type DIE
79 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
80 Dwarf_Die * unit, mc_frame_t frame,
83 /** \brief Calls MC_dwarf_handle_die on all childrend of the given die
85 * \param info the resulting object fot the library/binary file (output)
86 * \param die the current DIE
87 * \param unit the DIE of the compile unit of the current DIE
88 * \param frame containg frame if any
90 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
91 Dwarf_Die * unit, mc_frame_t frame,
94 /** \brief Handle a variable (DW_TAG_variable or other)
96 * \param info the resulting object fot the library/binary file (output)
97 * \param die the current DIE
98 * \param unit the DIE of the compile unit of the current DIE
99 * \param frame containg frame if any
101 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
102 Dwarf_Die * unit, mc_frame_t frame,
105 /** \brief Get the DW_TAG_type of the DIE
108 * \return DW_TAG_type attribute as a new string (NULL if none)
110 static std::string MC_dwarf_at_type(Dwarf_Die * die);
112 /** \brief A class of DWARF tags (DW_TAG_*)
114 typedef enum mc_tag_class {
123 static mc_tag_class MC_dwarf_tag_classify(int tag)
127 case DW_TAG_array_type:
128 case DW_TAG_class_type:
129 case DW_TAG_enumeration_type:
131 case DW_TAG_pointer_type:
132 case DW_TAG_reference_type:
133 case DW_TAG_rvalue_reference_type:
134 case DW_TAG_string_type:
135 case DW_TAG_structure_type:
136 case DW_TAG_subroutine_type:
137 case DW_TAG_union_type:
138 case DW_TAG_ptr_to_member_type:
139 case DW_TAG_set_type:
140 case DW_TAG_subrange_type:
141 case DW_TAG_base_type:
142 case DW_TAG_const_type:
143 case DW_TAG_file_type:
144 case DW_TAG_packed_type:
145 case DW_TAG_volatile_type:
146 case DW_TAG_restrict_type:
147 case DW_TAG_interface_type:
148 case DW_TAG_unspecified_type:
149 case DW_TAG_shared_type:
152 case DW_TAG_subprogram:
153 return mc_tag_subprogram;
155 case DW_TAG_variable:
156 case DW_TAG_formal_parameter:
157 return mc_tag_variable;
159 case DW_TAG_lexical_block:
160 case DW_TAG_try_block:
161 case DW_TAG_catch_block:
162 case DW_TAG_inlined_subroutine:
163 case DW_TAG_with_stmt:
166 case DW_TAG_namespace:
167 return mc_tag_namespace;
170 return mc_tag_unknown;
175 #define MC_DW_CLASS_UNKNOWN 0
176 #define MC_DW_CLASS_ADDRESS 1 // Location in the address space of the program
177 #define MC_DW_CLASS_BLOCK 2 // Arbitrary block of bytes
178 #define MC_DW_CLASS_CONSTANT 3
179 #define MC_DW_CLASS_STRING 3 // String
180 #define MC_DW_CLASS_FLAG 4 // Boolean
181 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
182 #define MC_DW_CLASS_EXPRLOC 6 // DWARF expression/location description
183 #define MC_DW_CLASS_LINEPTR 7
184 #define MC_DW_CLASS_LOCLISTPTR 8
185 #define MC_DW_CLASS_MACPTR 9
186 #define MC_DW_CLASS_RANGELISTPTR 10
188 /** \brief Find the DWARF data class for a given DWARF data form
190 * This mapping is defined in the DWARF spec.
192 * \param form The form (values taken from the DWARF spec)
193 * \return An internal representation for the corresponding class
195 static int MC_dwarf_form_get_class(int form)
199 return MC_DW_CLASS_ADDRESS;
204 return MC_DW_CLASS_BLOCK;
211 return MC_DW_CLASS_CONSTANT;
214 return MC_DW_CLASS_STRING;
215 case DW_FORM_ref_addr:
220 case DW_FORM_ref_udata:
221 return MC_DW_CLASS_REFERENCE;
223 case DW_FORM_flag_present:
224 return MC_DW_CLASS_FLAG;
225 case DW_FORM_exprloc:
226 return MC_DW_CLASS_EXPRLOC;
230 return MC_DW_CLASS_UNKNOWN;
234 /** \brief Get the name of the tag of a given DIE
237 * \return name of the tag of this DIE
239 static inline const char *MC_dwarf_die_tagname(Dwarf_Die * die)
241 return MC_dwarf_tagname(dwarf_tag(die));
246 /** \brief Get an attribute of a given DIE as a string
249 * \param attribute attribute
250 * \return value of the given attribute of the given DIE
252 static const char *MC_dwarf_attr_integrate_string(Dwarf_Die * die,
255 Dwarf_Attribute attr;
256 if (!dwarf_attr_integrate(die, attribute, &attr)) {
259 return dwarf_formstring(&attr);
263 /** \brief Get the linkage name of a DIE.
265 * Use either DW_AT_linkage_name or DW_AT_MIPS_linkage_name.
266 * DW_AT_linkage_name is standardized since DWARF 4.
267 * Before this version of DWARF, the MIPS extensions
268 * DW_AT_MIPS_linkage_name is used (at least by GCC).
271 * \return linkage name of the given DIE (or NULL)
273 static const char *MC_dwarf_at_linkage_name(Dwarf_Die * die)
275 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_linkage_name);
277 name = MC_dwarf_attr_integrate_string(die, DW_AT_MIPS_linkage_name);
281 static Dwarf_Off MC_dwarf_attr_dieoffset(Dwarf_Die * die, int attribute)
283 Dwarf_Attribute attr;
284 if (dwarf_hasattr_integrate(die, attribute)) {
285 dwarf_attr_integrate(die, attribute, &attr);
286 Dwarf_Die subtype_die;
287 if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
288 xbt_die("Could not find DIE");
290 return dwarf_dieoffset(&subtype_die);
295 static Dwarf_Off MC_dwarf_attr_integrate_dieoffset(Dwarf_Die * die,
298 Dwarf_Attribute attr;
299 if (dwarf_hasattr_integrate(die, attribute)) {
300 dwarf_attr_integrate(die, DW_AT_type, &attr);
301 Dwarf_Die subtype_die;
302 if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
303 xbt_die("Could not find DIE");
305 return dwarf_dieoffset(&subtype_die);
310 /** \brief Find the type/subtype (DW_AT_type) for a DIE
313 * \return DW_AT_type reference as a global offset in hexadecimal (or NULL)
316 std::string MC_dwarf_at_type(Dwarf_Die * die)
318 Dwarf_Off offset = MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
320 return std::string();
321 char* s = bprintf("%" PRIx64, offset);
324 return std::move(res);
327 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die * die, int attribute)
329 Dwarf_Attribute attr;
330 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
333 if (dwarf_formaddr(&attr, &value) == 0)
334 return (uint64_t) value;
339 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die * die, int attribute,
340 uint64_t default_value)
342 Dwarf_Attribute attr;
343 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
344 return default_value;
346 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr),
347 &value) == 0 ? (uint64_t) value : default_value;
350 static bool MC_dwarf_attr_flag(Dwarf_Die * die, int attribute, bool integrate)
352 Dwarf_Attribute attr;
353 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
354 : dwarf_attr(die, attribute, &attr)) == 0)
358 if (dwarf_formflag(&attr, &result))
359 xbt_die("Unexpected form for attribute %s", MC_dwarf_attrname(attribute));
363 /** \brief Find the default lower bound for a given language
365 * The default lower bound of an array (when DW_TAG_lower_bound
366 * is missing) depends on the language of the compilation unit.
368 * \param lang Language of the compilation unit (values defined in the DWARF spec)
369 * \return Default lower bound of an array in this compilation unit
371 static uint64_t MC_dwarf_default_lower_bound(int lang)
377 case DW_LANG_C_plus_plus:
381 case DW_LANG_ObjC_plus_plus:
387 case DW_LANG_Fortran77:
388 case DW_LANG_Fortran90:
389 case DW_LANG_Fortran95:
390 case DW_LANG_Modula2:
391 case DW_LANG_Pascal83:
393 case DW_LANG_Cobol74:
394 case DW_LANG_Cobol85:
397 xbt_die("No default DW_TAG_lower_bound for language %i and none given",
403 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
406 * \param unit DIE of the compilation unit
407 * \return number of elements in the range
409 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
412 xbt_assert(dwarf_tag(die) == DW_TAG_enumeration_type
413 || dwarf_tag(die) == DW_TAG_subrange_type,
414 "MC_dwarf_subrange_element_count called with DIE of type %s",
415 MC_dwarf_die_tagname(die));
417 // Use DW_TAG_count if present:
418 if (dwarf_hasattr_integrate(die, DW_AT_count)) {
419 return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
421 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
423 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
424 // This is not really 0, but the code expects this (we do not know):
427 uint64_t upper_bound =
428 MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
430 uint64_t lower_bound = 0;
431 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
432 lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
434 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
436 return upper_bound - lower_bound + 1;
439 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
441 * The compilation unit might be needed because the default lower
442 * bound depends on the language of the compilation unit.
444 * \param die the DIE of the DW_TAG_array_type
445 * \param unit the DIE of the compilation unit
446 * \return number of elements in this array type
448 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit)
450 xbt_assert(dwarf_tag(die) == DW_TAG_array_type,
451 "MC_dwarf_array_element_count called with DIE of type %s",
452 MC_dwarf_die_tagname(die));
457 for (res = dwarf_child(die, &child); res == 0;
458 res = dwarf_siblingof(&child, &child)) {
459 int child_tag = dwarf_tag(&child);
460 if (child_tag == DW_TAG_subrange_type
461 || child_tag == DW_TAG_enumeration_type) {
462 result *= MC_dwarf_subrange_element_count(&child, unit);
470 /** \brief Initialize the location of a member of a type
471 * (DW_AT_data_member_location of a DW_TAG_member).
473 * \param type a type (struct, class)
474 * \param member the member of the type
475 * \param child DIE of the member (DW_TAG_member)
477 static void MC_dwarf_fill_member_location(mc_type_t type, mc_type_t member,
480 if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
481 xbt_die("Can't groke DW_AT_data_bit_offset.");
484 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
485 if (type->type != DW_TAG_union_type) {
487 ("Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%"
488 PRIx64 ">%s", member->name.c_str(),
489 (uint64_t) type->id, type->name.c_str());
495 Dwarf_Attribute attr;
496 dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
497 int form = dwarf_whatform(&attr);
498 int klass = MC_dwarf_form_get_class(form);
500 case MC_DW_CLASS_EXPRLOC:
501 case MC_DW_CLASS_BLOCK:
502 // Location expression:
506 if (dwarf_getlocation(&attr, &expr, &len)) {
508 ("Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%"
509 PRIx64 ">%s", MC_dwarf_attr_integrate_string(child, DW_AT_name),
510 (uint64_t) type->id, type->name.c_str());
512 simgrid::mc::DwarfExpression(expr, expr+len);
515 case MC_DW_CLASS_CONSTANT:
516 // Offset from the base address of the object:
519 if (!dwarf_formudata(&attr, &offset))
520 member->offset(offset);
522 xbt_die("Cannot get %s location <%" PRIx64 ">%s",
523 MC_dwarf_attr_integrate_string(child, DW_AT_name),
524 (uint64_t) type->id, type->name.c_str());
527 case MC_DW_CLASS_LOCLISTPTR:
528 // Reference to a location list:
530 case MC_DW_CLASS_REFERENCE:
531 // It's supposed to be possible in DWARF2 but I couldn't find its semantic
534 xbt_die("Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
540 static void dw_type_free_voidp(void *t)
542 delete *(mc_type_t*)t;
545 /** \brief Populate the list of members of a type
547 * \param info ELF object containing the type DIE
548 * \param die DIE of the type
549 * \param unit DIE of the compilation unit containing the type DIE
550 * \param type the type
552 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die * die,
553 Dwarf_Die * unit, mc_type_t type)
557 xbt_assert(type->members.empty());
558 for (res = dwarf_child(die, &child); res == 0;
559 res = dwarf_siblingof(&child, &child)) {
560 int tag = dwarf_tag(&child);
561 if (tag == DW_TAG_member || tag == DW_TAG_inheritance) {
563 // Skip declarations:
564 if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
567 // Skip compile time constants:
568 if (dwarf_hasattr(&child, DW_AT_const_value))
571 // TODO, we should use another type (because is is not a type but a member)
572 simgrid::mc::Type member;
576 member.id = dwarf_dieoffset(&child);
578 const char *name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
582 MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
583 member.element_count = -1;
584 member.type_id = MC_dwarf_at_type(&child);
586 if (dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
587 xbt_die("Can't groke DW_AT_data_bit_offset.");
590 MC_dwarf_fill_member_location(type, &member, &child);
592 if (member.type_id.empty()) {
593 xbt_die("Missing type for member %s of <%" PRIx64 ">%s",
595 (uint64_t) type->id, type->name.c_str());
598 type->members.push_back(std::move(member));
603 /** \brief Create a MC type object from a DIE
605 * \param info current object info object
606 * \param DIE (for a given type);
607 * \param unit compilation unit of the current DIE
608 * \return MC representation of the type
610 static mc_type_t MC_dwarf_die_to_type(mc_object_info_t info, Dwarf_Die * die,
611 Dwarf_Die * unit, mc_frame_t frame,
615 mc_type_t type = new simgrid::mc::Type();
617 type->name = std::string();
618 type->element_count = -1;
620 type->type = dwarf_tag(die);
623 type->id = dwarf_dieoffset(die);
625 const char *prefix = "";
626 switch (type->type) {
627 case DW_TAG_structure_type:
630 case DW_TAG_union_type:
633 case DW_TAG_class_type:
640 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
642 char* full_name = ns ? bprintf("%s%s::%s", prefix, ns, name) :
643 bprintf("%s%s", prefix, name);
644 type->name = std::string(full_name);
648 type->type_id = MC_dwarf_at_type(die);
650 // Some compilers do not emit DW_AT_byte_size for pointer_type,
651 // so we fill this. We currently assume that the model-checked process is in
652 // the same architecture..
653 if (type->type == DW_TAG_pointer_type)
654 type->byte_size = sizeof(void*);
656 // Computation of the byte_size;
657 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
658 type->byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
659 else if (type->type == DW_TAG_array_type
660 || type->type == DW_TAG_structure_type
661 || type->type == DW_TAG_class_type) {
663 if (dwarf_aggregate_size(die, &size) == 0) {
664 type->byte_size = size;
668 switch (type->type) {
669 case DW_TAG_array_type:
670 type->element_count = MC_dwarf_array_element_count(die, unit);
671 // TODO, handle DW_byte_stride and (not) DW_bit_stride
674 case DW_TAG_pointer_type:
675 case DW_TAG_reference_type:
676 case DW_TAG_rvalue_reference_type:
677 type->is_pointer_type = 1;
680 case DW_TAG_structure_type:
681 case DW_TAG_union_type:
682 case DW_TAG_class_type:
683 MC_dwarf_add_members(info, die, unit, type);
684 char *new_ns = ns == NULL ? xbt_strdup(type->name.c_str())
685 : bprintf("%s::%s", ns, name);
686 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
694 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
695 Dwarf_Die * unit, mc_frame_t frame,
698 mc_type_t type = MC_dwarf_die_to_type(info, die, unit, frame, ns);
700 char *key = bprintf("%" PRIx64, (uint64_t) type->id);
701 xbt_dict_set(info->types, key, type, NULL);
704 if (!type->name.empty() && type->byte_size != 0) {
705 xbt_dict_set(info->full_types_by_name, type->name.c_str(), type, NULL);
709 static int mc_anonymous_variable_index = 0;
711 static mc_variable_t MC_die_to_variable(mc_object_info_t info, Dwarf_Die * die,
712 Dwarf_Die * unit, mc_frame_t frame,
715 // Skip declarations:
716 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
719 // Skip compile time constants:
720 if (dwarf_hasattr(die, DW_AT_const_value))
723 Dwarf_Attribute attr_location;
724 if (dwarf_attr(die, DW_AT_location, &attr_location) == NULL) {
725 // No location: do not add it ?
729 simgrid::mc::Variable* 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 mc_dwarf_location_list_init_from_expression(
765 &variable->location_list, len,
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++);
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_variable_t variable =
826 MC_die_to_variable(info, die, unit, frame, ns);
827 if (variable == NULL)
829 MC_dwarf_register_variable(info, frame, variable);
832 static void MC_dwarf_handle_scope_die(mc_object_info_t info, Dwarf_Die * die,
833 Dwarf_Die * unit, mc_frame_t parent_frame,
836 // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
837 int tag = dwarf_tag(die);
838 mc_tag_class klass = MC_dwarf_tag_classify(tag);
840 // (Template) Subprogram declaration:
841 if (klass == mc_tag_subprogram
842 && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
845 if (klass == mc_tag_scope)
846 xbt_assert(parent_frame, "No parent scope for this scope");
848 mc_frame_t frame = new simgrid::mc::Frame();
851 frame->id = dwarf_dieoffset(die);
852 frame->object_info = info;
854 if (klass == mc_tag_subprogram) {
855 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
857 frame->name = std::string(ns) + "::" + name;
862 frame->abstract_origin_id =
863 MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
865 // This is the base address for DWARF addresses.
866 // Relocated addresses are offset from this base address.
867 // See DWARF4 spec 7.5
868 void *base = info->base_address();
870 // TODO, support DW_AT_ranges
871 uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
872 frame->low_pc = low_pc ? ((char *) base) + low_pc : 0;
875 Dwarf_Attribute attr;
876 if (!dwarf_attr_integrate(die, DW_AT_high_pc, &attr)) {
877 xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
883 switch (MC_dwarf_form_get_class(dwarf_whatform(&attr))) {
885 // DW_AT_high_pc if an offset from the low_pc:
886 case MC_DW_CLASS_CONSTANT:
888 if (dwarf_formsdata(&attr, &offset) != 0)
889 xbt_die("Could not read constant");
890 frame->high_pc = (void *) ((char *) frame->low_pc + offset);
893 // DW_AT_high_pc is a relocatable address:
894 case MC_DW_CLASS_ADDRESS:
895 if (dwarf_formaddr(&attr, &high_pc) != 0)
896 xbt_die("Could not read address");
897 frame->high_pc = ((char *) base) + high_pc;
901 xbt_die("Unexpected class for DW_AT_high_pc");
906 if (klass == mc_tag_subprogram) {
907 Dwarf_Attribute attr_frame_base;
908 if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
909 mc_dwarf_location_list_init(&frame->frame_base, info, die,
914 if (klass == mc_tag_subprogram) {
915 char *key = bprintf("%" PRIx64, (uint64_t) frame->id);
916 xbt_dict_set(info->subprograms, key, frame, NULL);
918 } else if (klass == mc_tag_scope) {
919 xbt_dynar_push(parent_frame->scopes, &frame);
922 MC_dwarf_handle_children(info, die, unit, frame, ns);
925 static void mc_dwarf_handle_namespace_die(mc_object_info_t info,
926 Dwarf_Die * die, Dwarf_Die * unit,
930 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
932 xbt_die("Unexpected namespace in a subprogram");
933 char *new_ns = ns == NULL ? xbt_strdup(name)
934 : bprintf("%s::%s", ns, name);
935 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
939 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
940 Dwarf_Die * unit, mc_frame_t frame,
943 // For each child DIE:
946 for (res = dwarf_child(die, &child); res == 0;
947 res = dwarf_siblingof(&child, &child)) {
948 MC_dwarf_handle_die(info, &child, unit, frame, ns);
952 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
953 Dwarf_Die * unit, mc_frame_t frame,
956 int tag = dwarf_tag(die);
957 mc_tag_class klass = MC_dwarf_tag_classify(tag);
962 MC_dwarf_handle_type_die(info, die, unit, frame, ns);
965 // Subprogram or scope:
966 case mc_tag_subprogram:
968 MC_dwarf_handle_scope_die(info, die, unit, frame, ns);
972 case mc_tag_variable:
973 MC_dwarf_handle_variable_die(info, die, unit, frame, ns);
976 case mc_tag_namespace:
977 mc_dwarf_handle_namespace_die(info, die, unit, frame, ns);
986 /** \brief Populate the debugging informations of the given ELF object
988 * Read the DWARf information of the EFFL object and populate the
989 * lists of types, variables, functions.
991 void MC_dwarf_get_variables(mc_object_info_t info)
993 int fd = open(info->file_name, O_RDONLY);
995 xbt_die("Could not open file %s", info->file_name);
997 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
999 xbt_die("Your program must be compiled with -g (%s)", info->file_name);
1001 // For each compilation unit:
1002 Dwarf_Off offset = 0;
1003 Dwarf_Off next_offset = 0;
1005 while (dwarf_nextcu(dwarf, offset, &next_offset, &length, NULL, NULL, NULL) ==
1008 if (dwarf_offdie(dwarf, offset + length, &unit_die) != NULL) {
1010 // For each child DIE:
1013 for (res = dwarf_child(&unit_die, &child); res == 0;
1014 res = dwarf_siblingof(&child, &child)) {
1015 MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
1019 offset = next_offset;
1026 /************************** Free functions *************************/
1028 static void dw_type_free(mc_type_t t)
1033 // ***** object_info
1038 ObjectInformation::ObjectInformation()
1041 this->file_name = nullptr;
1042 this->start = nullptr;
1043 this->end = nullptr;
1044 this->start_exec = nullptr;
1045 this->end_exec = nullptr;
1046 this->start_rw = nullptr;
1047 this->end_rw = nullptr;
1048 this->start_ro = nullptr;
1049 this->end_ro = nullptr;
1050 this->subprograms = xbt_dict_new_homogeneous(mc_frame_free);
1051 this->global_variables =
1052 xbt_dynar_new(sizeof(mc_variable_t), mc_variable_free_voidp);
1053 this->types = xbt_dict_new_homogeneous((void (*)(void *)) dw_type_free);
1054 this->full_types_by_name = xbt_dict_new_homogeneous(NULL);
1055 this->functions_index = nullptr;
1058 ObjectInformation::~ObjectInformation()
1060 xbt_free(this->file_name);
1061 xbt_dict_free(&this->subprograms);
1062 xbt_dynar_free(&this->global_variables);
1063 xbt_dict_free(&this->types);
1064 xbt_dict_free(&this->full_types_by_name);
1065 xbt_dynar_free(&this->functions_index);
1068 /** Find the DWARF offset for this ELF object
1070 * An offset is applied to address found in DWARF:
1073 * <li>for an executable obejct, addresses are virtual address
1074 * (there is no offset) i.e. \f$\text{virtual address} = \{dwarf address}\f$;</li>
1075 * <li>for a shared object, the addreses are offset from the begining
1076 * of the shared object (the base address of the mapped shared
1077 * object must be used as offset
1078 * i.e. \f$\text{virtual address} = \text{shared object base address}
1079 * + \text{dwarf address}\f$.</li>
1082 void *ObjectInformation::base_address() const
1084 if (this->executable())
1087 void *result = this->start_exec;
1088 if (this->start_rw != NULL && result > (void *) this->start_rw)
1089 result = this->start_rw;
1090 if (this->start_ro != NULL && result > (void *) this->start_ro)
1091 result = this->start_ro;
1100 // ***** Functions index
1102 static int MC_compare_frame_index_items(mc_function_index_item_t a,
1103 mc_function_index_item_t b)
1105 if (a->low_pc < b->low_pc)
1107 else if (a->low_pc == b->low_pc)
1113 static void MC_make_functions_index(mc_object_info_t info)
1115 xbt_dynar_t index = xbt_dynar_new(sizeof(s_mc_function_index_item_t), NULL);
1117 // Populate the array:
1118 mc_frame_t frame = NULL;
1119 xbt_dict_cursor_t cursor;
1121 xbt_dict_foreach(info->subprograms, cursor, key, frame) {
1122 if (frame->low_pc == NULL)
1124 s_mc_function_index_item_t entry;
1125 entry.low_pc = frame->low_pc;
1126 entry.high_pc = frame->high_pc;
1127 entry.function = frame;
1128 xbt_dynar_push(index, &entry);
1131 mc_function_index_item_t base =
1132 (mc_function_index_item_t) xbt_dynar_get_ptr(index, 0);
1134 // Sort the array by low_pc:
1136 xbt_dynar_length(index),
1137 sizeof(s_mc_function_index_item_t),
1138 (int (*)(const void *, const void *)) MC_compare_frame_index_items);
1140 info->functions_index = index;
1143 static void MC_post_process_variables(mc_object_info_t info)
1145 unsigned cursor = 0;
1146 mc_variable_t variable = nullptr;
1147 xbt_dynar_foreach(info->global_variables, cursor, variable)
1148 if (!variable->type_id.empty())
1149 variable->type = (mc_type_t) xbt_dict_get_or_null(
1150 info->types, variable->type_id.c_str());
1153 static void mc_post_process_scope(mc_object_info_t info, mc_frame_t scope)
1156 if (scope->tag == DW_TAG_inlined_subroutine) {
1158 // Attach correct namespaced name in inlined subroutine:
1159 char *key = bprintf("%" PRIx64, (uint64_t) scope->abstract_origin_id);
1160 mc_frame_t abstract_origin = (mc_frame_t) xbt_dict_get_or_null(info->subprograms, key);
1161 xbt_assert(abstract_origin, "Could not lookup abstract origin %s", key);
1163 scope->name = abstract_origin->name;
1167 unsigned cursor = 0;
1168 mc_variable_t variable = nullptr;
1169 xbt_dynar_foreach(scope->variables, cursor, variable)
1170 if (!variable->type_id.empty())
1171 variable->type = (mc_type_t) xbt_dict_get_or_null(
1172 info->types, variable->type_id.c_str());
1174 // Recursive post-processing of nested-scopes:
1175 mc_frame_t nested_scope = nullptr;
1176 xbt_dynar_foreach(scope->scopes, cursor, nested_scope)
1177 mc_post_process_scope(info, nested_scope);
1181 static void MC_post_process_functions(mc_object_info_t info)
1183 xbt_dict_cursor_t cursor;
1185 mc_frame_t subprogram = NULL;
1186 xbt_dict_foreach(info->subprograms, cursor, key, subprogram) {
1187 mc_post_process_scope(info, subprogram);
1192 /** \brief Fill/lookup the "subtype" field.
1194 static void MC_resolve_subtype(mc_object_info_t info, mc_type_t type)
1197 if (type->type_id.empty())
1199 type->subtype = (mc_type_t) xbt_dict_get_or_null(
1200 info->types, type->type_id.c_str());
1201 if (type->subtype == NULL)
1203 if (type->subtype->byte_size != 0)
1205 if (type->subtype->name.empty())
1207 // Try to find a more complete description of the type:
1208 // We need to fix in order to support C++.
1211 (mc_type_t) xbt_dict_get_or_null(
1212 info->full_types_by_name, type->subtype->name.c_str());
1213 if (subtype != NULL) {
1214 type->subtype = subtype;
1219 static void MC_post_process_types(mc_object_info_t info)
1221 xbt_dict_cursor_t cursor = NULL;
1225 // Lookup "subtype" field:
1226 xbt_dict_foreach(info->types, cursor, origin, type) {
1227 MC_resolve_subtype(info, type);
1228 for (simgrid::mc::Type& member : type->members)
1229 MC_resolve_subtype(info, &member);
1233 /** \brief Finds informations about a given shared object/executable */
1234 std::shared_ptr<s_mc_object_info_t> MC_find_object_info(
1235 std::vector<simgrid::mc::VmMap> const& maps, const char *name, int executable)
1237 std::shared_ptr<s_mc_object_info_t> result =
1238 std::make_shared<s_mc_object_info_t>();
1240 result->flags |= MC_OBJECT_INFO_EXECUTABLE;
1241 result->file_name = xbt_strdup(name);
1242 MC_find_object_address(maps, result.get());
1243 MC_dwarf_get_variables(result.get());
1244 MC_post_process_types(result.get());
1245 MC_post_process_variables(result.get());
1246 MC_post_process_functions(result.get());
1247 MC_make_functions_index(result.get());
1248 return std::move(result);
1251 /*************************************************************************/
1253 static int MC_dwarf_get_variable_index(xbt_dynar_t variables, const char *var,
1257 if (xbt_dynar_is_empty(variables))
1260 unsigned int cursor = 0;
1262 int end = xbt_dynar_length(variables) - 1;
1263 mc_variable_t var_test = NULL;
1265 while (start <= end) {
1266 cursor = (start + end) / 2;
1268 (mc_variable_t) xbt_dynar_get_as(variables, cursor, mc_variable_t);
1269 if (strcmp(var_test->name.c_str(), var) < 0) {
1271 } else if (strcmp(var_test->name.c_str(), var) > 0) {
1274 if (address) { /* global variable */
1275 if (var_test->address == address)
1277 if (var_test->address > address)
1281 } else { /* local variable */
1287 if (strcmp(var_test->name.c_str(), var) == 0) {
1288 if (address && var_test->address < address)
1292 } else if (strcmp(var_test->name.c_str(), var) < 0)
1299 void MC_dwarf_register_global_variable(mc_object_info_t info,
1300 mc_variable_t variable)
1303 MC_dwarf_get_variable_index(info->global_variables,
1304 variable->name.c_str(),
1307 xbt_dynar_insert_at(info->global_variables, index, &variable);
1311 void MC_dwarf_register_non_global_variable(mc_object_info_t info,
1313 mc_variable_t variable)
1315 xbt_assert(frame, "Frame is NULL");
1317 MC_dwarf_get_variable_index(
1318 frame->variables, variable->name.c_str(), NULL);
1320 xbt_dynar_insert_at(frame->variables, index, &variable);
1324 void MC_dwarf_register_variable(mc_object_info_t info, mc_frame_t frame,
1325 mc_variable_t variable)
1327 if (variable->global)
1328 MC_dwarf_register_global_variable(info, variable);
1329 else if (frame == NULL)
1330 xbt_die("No frame for this local variable");
1332 MC_dwarf_register_non_global_variable(info, frame, variable);
1335 void MC_post_process_object_info(mc_process_t process, mc_object_info_t info)
1337 xbt_dict_cursor_t cursor = NULL;
1339 mc_type_t type = NULL;
1340 xbt_dict_foreach(info->types, cursor, key, type) {
1342 mc_type_t subtype = type;
1343 while (subtype->type == DW_TAG_typedef || subtype->type == DW_TAG_volatile_type
1344 || subtype->type == DW_TAG_const_type) {
1345 if (subtype->subtype)
1346 subtype = subtype->subtype;
1351 // Resolve full_type:
1352 if (!subtype->name.empty() && subtype->byte_size == 0) {
1353 for (auto const& object_info : process->object_infos) {
1354 mc_type_t same_type = (mc_type_t)
1355 xbt_dict_get_or_null(object_info->full_types_by_name,
1356 subtype->name.c_str());
1357 if (same_type && !same_type->name.empty() && same_type->byte_size) {
1358 type->full_type = same_type;
1362 } else type->full_type = subtype;