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_variable_free_voidp(void *t)
25 delete *(simgrid::mc::Variable**)t;
28 static void mc_frame_free(void* frame)
30 delete (simgrid::mc::Frame*)frame;
33 static void MC_dwarf_register_global_variable(
34 mc_object_info_t info, std::unique_ptr<simgrid::mc::Variable> variable);
35 static void MC_register_variable(
36 mc_object_info_t info, mc_frame_t frame, std::unique_ptr<simgrid::mc::Variable> variable);
37 static void MC_dwarf_register_non_global_variable(mc_object_info_t info, mc_frame_t frame, mc_variable_t variable);
38 static void MC_dwarf_register_variable(
39 mc_object_info_t info, mc_frame_t frame,
40 std::unique_ptr<simgrid::mc::Variable> variable);
42 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing");
44 /** \brief The default DW_TAG_lower_bound for a given DW_AT_language.
46 * The default for a given language is defined in the DWARF spec.
48 * \param language consant as defined by the DWARf spec
50 static uint64_t MC_dwarf_default_lower_bound(int lang);
52 /** \brief Computes the the element_count of a DW_TAG_enumeration_type DIE
54 * This is the number of elements in a given array dimension.
56 * A reference of the compilation unit (DW_TAG_compile_unit) is
57 * needed because the default lower bound (when there is no DW_AT_lower_bound)
58 * depends of the language of the compilation unit (DW_AT_language).
60 * \param die DIE for the DW_TAG_enumeration_type or DW_TAG_subrange_type
61 * \param unit DIE of the DW_TAG_compile_unit
63 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
66 /** \brief Computes the number of elements of a given DW_TAG_array_type.
68 * \param die DIE for the DW_TAG_array_type
70 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit);
72 /** \brief Process a DIE
74 * \param info the resulting object fot the library/binary file (output)
75 * \param die the current DIE
76 * \param unit the DIE of the compile unit of the current DIE
77 * \param frame containg frame if any
79 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
80 Dwarf_Die * unit, mc_frame_t frame,
83 /** \brief Process a type DIE
85 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
86 Dwarf_Die * unit, mc_frame_t frame,
89 /** \brief Calls MC_dwarf_handle_die on all childrend of the given die
91 * \param info the resulting object fot the library/binary file (output)
92 * \param die the current DIE
93 * \param unit the DIE of the compile unit of the current DIE
94 * \param frame containg frame if any
96 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
97 Dwarf_Die * unit, mc_frame_t frame,
100 /** \brief Handle a variable (DW_TAG_variable or other)
102 * \param info the resulting object fot the library/binary file (output)
103 * \param die the current DIE
104 * \param unit the DIE of the compile unit of the current DIE
105 * \param frame containg frame if any
107 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
108 Dwarf_Die * unit, mc_frame_t frame,
111 /** \brief Get the DW_TAG_type of the DIE
114 * \return DW_TAG_type attribute as a new string (NULL if none)
116 static std::string MC_dwarf_at_type(Dwarf_Die * die);
118 /** \brief A class of DWARF tags (DW_TAG_*)
120 typedef enum mc_tag_class {
129 static mc_tag_class MC_dwarf_tag_classify(int tag)
133 case DW_TAG_array_type:
134 case DW_TAG_class_type:
135 case DW_TAG_enumeration_type:
137 case DW_TAG_pointer_type:
138 case DW_TAG_reference_type:
139 case DW_TAG_rvalue_reference_type:
140 case DW_TAG_string_type:
141 case DW_TAG_structure_type:
142 case DW_TAG_subroutine_type:
143 case DW_TAG_union_type:
144 case DW_TAG_ptr_to_member_type:
145 case DW_TAG_set_type:
146 case DW_TAG_subrange_type:
147 case DW_TAG_base_type:
148 case DW_TAG_const_type:
149 case DW_TAG_file_type:
150 case DW_TAG_packed_type:
151 case DW_TAG_volatile_type:
152 case DW_TAG_restrict_type:
153 case DW_TAG_interface_type:
154 case DW_TAG_unspecified_type:
155 case DW_TAG_shared_type:
158 case DW_TAG_subprogram:
159 return mc_tag_subprogram;
161 case DW_TAG_variable:
162 case DW_TAG_formal_parameter:
163 return mc_tag_variable;
165 case DW_TAG_lexical_block:
166 case DW_TAG_try_block:
167 case DW_TAG_catch_block:
168 case DW_TAG_inlined_subroutine:
169 case DW_TAG_with_stmt:
172 case DW_TAG_namespace:
173 return mc_tag_namespace;
176 return mc_tag_unknown;
181 #define MC_DW_CLASS_UNKNOWN 0
182 #define MC_DW_CLASS_ADDRESS 1 // Location in the address space of the program
183 #define MC_DW_CLASS_BLOCK 2 // Arbitrary block of bytes
184 #define MC_DW_CLASS_CONSTANT 3
185 #define MC_DW_CLASS_STRING 3 // String
186 #define MC_DW_CLASS_FLAG 4 // Boolean
187 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
188 #define MC_DW_CLASS_EXPRLOC 6 // DWARF expression/location description
189 #define MC_DW_CLASS_LINEPTR 7
190 #define MC_DW_CLASS_LOCLISTPTR 8
191 #define MC_DW_CLASS_MACPTR 9
192 #define MC_DW_CLASS_RANGELISTPTR 10
194 /** \brief Find the DWARF data class for a given DWARF data form
196 * This mapping is defined in the DWARF spec.
198 * \param form The form (values taken from the DWARF spec)
199 * \return An internal representation for the corresponding class
201 static int MC_dwarf_form_get_class(int form)
205 return MC_DW_CLASS_ADDRESS;
210 return MC_DW_CLASS_BLOCK;
217 return MC_DW_CLASS_CONSTANT;
220 return MC_DW_CLASS_STRING;
221 case DW_FORM_ref_addr:
226 case DW_FORM_ref_udata:
227 return MC_DW_CLASS_REFERENCE;
229 case DW_FORM_flag_present:
230 return MC_DW_CLASS_FLAG;
231 case DW_FORM_exprloc:
232 return MC_DW_CLASS_EXPRLOC;
236 return MC_DW_CLASS_UNKNOWN;
240 /** \brief Get the name of the tag of a given DIE
243 * \return name of the tag of this DIE
245 static inline const char *MC_dwarf_die_tagname(Dwarf_Die * die)
247 return MC_dwarf_tagname(dwarf_tag(die));
252 /** \brief Get an attribute of a given DIE as a string
255 * \param attribute attribute
256 * \return value of the given attribute of the given DIE
258 static const char *MC_dwarf_attr_integrate_string(Dwarf_Die * die,
261 Dwarf_Attribute attr;
262 if (!dwarf_attr_integrate(die, attribute, &attr)) {
265 return dwarf_formstring(&attr);
269 /** \brief Get the linkage name of a DIE.
271 * Use either DW_AT_linkage_name or DW_AT_MIPS_linkage_name.
272 * DW_AT_linkage_name is standardized since DWARF 4.
273 * Before this version of DWARF, the MIPS extensions
274 * DW_AT_MIPS_linkage_name is used (at least by GCC).
277 * \return linkage name of the given DIE (or NULL)
279 static const char *MC_dwarf_at_linkage_name(Dwarf_Die * die)
281 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_linkage_name);
283 name = MC_dwarf_attr_integrate_string(die, DW_AT_MIPS_linkage_name);
287 static Dwarf_Off MC_dwarf_attr_dieoffset(Dwarf_Die * die, int attribute)
289 Dwarf_Attribute attr;
290 if (dwarf_hasattr_integrate(die, attribute)) {
291 dwarf_attr_integrate(die, attribute, &attr);
292 Dwarf_Die subtype_die;
293 if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
294 xbt_die("Could not find DIE");
296 return dwarf_dieoffset(&subtype_die);
301 static Dwarf_Off MC_dwarf_attr_integrate_dieoffset(Dwarf_Die * die,
304 Dwarf_Attribute attr;
305 if (dwarf_hasattr_integrate(die, attribute)) {
306 dwarf_attr_integrate(die, DW_AT_type, &attr);
307 Dwarf_Die subtype_die;
308 if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
309 xbt_die("Could not find DIE");
311 return dwarf_dieoffset(&subtype_die);
316 /** \brief Find the type/subtype (DW_AT_type) for a DIE
319 * \return DW_AT_type reference as a global offset in hexadecimal (or NULL)
322 std::string MC_dwarf_at_type(Dwarf_Die * die)
324 Dwarf_Off offset = MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
326 return std::string();
327 char* s = bprintf("%" PRIx64, offset);
330 return std::move(res);
333 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die * die, int attribute)
335 Dwarf_Attribute attr;
336 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
339 if (dwarf_formaddr(&attr, &value) == 0)
340 return (uint64_t) value;
345 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die * die, int attribute,
346 uint64_t default_value)
348 Dwarf_Attribute attr;
349 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
350 return default_value;
352 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr),
353 &value) == 0 ? (uint64_t) value : default_value;
356 static bool MC_dwarf_attr_flag(Dwarf_Die * die, int attribute, bool integrate)
358 Dwarf_Attribute attr;
359 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
360 : dwarf_attr(die, attribute, &attr)) == 0)
364 if (dwarf_formflag(&attr, &result))
365 xbt_die("Unexpected form for attribute %s", MC_dwarf_attrname(attribute));
369 /** \brief Find the default lower bound for a given language
371 * The default lower bound of an array (when DW_TAG_lower_bound
372 * is missing) depends on the language of the compilation unit.
374 * \param lang Language of the compilation unit (values defined in the DWARF spec)
375 * \return Default lower bound of an array in this compilation unit
377 static uint64_t MC_dwarf_default_lower_bound(int lang)
383 case DW_LANG_C_plus_plus:
387 case DW_LANG_ObjC_plus_plus:
393 case DW_LANG_Fortran77:
394 case DW_LANG_Fortran90:
395 case DW_LANG_Fortran95:
396 case DW_LANG_Modula2:
397 case DW_LANG_Pascal83:
399 case DW_LANG_Cobol74:
400 case DW_LANG_Cobol85:
403 xbt_die("No default DW_TAG_lower_bound for language %i and none given",
409 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
412 * \param unit DIE of the compilation unit
413 * \return number of elements in the range
415 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
418 xbt_assert(dwarf_tag(die) == DW_TAG_enumeration_type
419 || dwarf_tag(die) == DW_TAG_subrange_type,
420 "MC_dwarf_subrange_element_count called with DIE of type %s",
421 MC_dwarf_die_tagname(die));
423 // Use DW_TAG_count if present:
424 if (dwarf_hasattr_integrate(die, DW_AT_count)) {
425 return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
427 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
429 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
430 // This is not really 0, but the code expects this (we do not know):
433 uint64_t upper_bound =
434 MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
436 uint64_t lower_bound = 0;
437 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
438 lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
440 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
442 return upper_bound - lower_bound + 1;
445 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
447 * The compilation unit might be needed because the default lower
448 * bound depends on the language of the compilation unit.
450 * \param die the DIE of the DW_TAG_array_type
451 * \param unit the DIE of the compilation unit
452 * \return number of elements in this array type
454 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit)
456 xbt_assert(dwarf_tag(die) == DW_TAG_array_type,
457 "MC_dwarf_array_element_count called with DIE of type %s",
458 MC_dwarf_die_tagname(die));
463 for (res = dwarf_child(die, &child); res == 0;
464 res = dwarf_siblingof(&child, &child)) {
465 int child_tag = dwarf_tag(&child);
466 if (child_tag == DW_TAG_subrange_type
467 || child_tag == DW_TAG_enumeration_type) {
468 result *= MC_dwarf_subrange_element_count(&child, unit);
476 /** \brief Initialize the location of a member of a type
477 * (DW_AT_data_member_location of a DW_TAG_member).
479 * \param type a type (struct, class)
480 * \param member the member of the type
481 * \param child DIE of the member (DW_TAG_member)
483 static void MC_dwarf_fill_member_location(mc_type_t type, mc_type_t member,
486 if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
487 xbt_die("Can't groke DW_AT_data_bit_offset.");
490 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
491 if (type->type != DW_TAG_union_type) {
493 ("Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%"
494 PRIx64 ">%s", member->name.c_str(),
495 (uint64_t) type->id, type->name.c_str());
501 Dwarf_Attribute attr;
502 dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
503 int form = dwarf_whatform(&attr);
504 int klass = MC_dwarf_form_get_class(form);
506 case MC_DW_CLASS_EXPRLOC:
507 case MC_DW_CLASS_BLOCK:
508 // Location expression:
512 if (dwarf_getlocation(&attr, &expr, &len)) {
514 ("Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%"
515 PRIx64 ">%s", MC_dwarf_attr_integrate_string(child, DW_AT_name),
516 (uint64_t) type->id, type->name.c_str());
518 simgrid::mc::DwarfExpression(expr, expr+len);
521 case MC_DW_CLASS_CONSTANT:
522 // Offset from the base address of the object:
525 if (!dwarf_formudata(&attr, &offset))
526 member->offset(offset);
528 xbt_die("Cannot get %s location <%" PRIx64 ">%s",
529 MC_dwarf_attr_integrate_string(child, DW_AT_name),
530 (uint64_t) type->id, type->name.c_str());
533 case MC_DW_CLASS_LOCLISTPTR:
534 // Reference to a location list:
536 case MC_DW_CLASS_REFERENCE:
537 // It's supposed to be possible in DWARF2 but I couldn't find its semantic
540 xbt_die("Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
546 static void dw_type_free_voidp(void *t)
548 delete *(mc_type_t*)t;
551 /** \brief Populate the list of members of a type
553 * \param info ELF object containing the type DIE
554 * \param die DIE of the type
555 * \param unit DIE of the compilation unit containing the type DIE
556 * \param type the type
558 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die * die,
559 Dwarf_Die * unit, mc_type_t type)
563 xbt_assert(type->members.empty());
564 for (res = dwarf_child(die, &child); res == 0;
565 res = dwarf_siblingof(&child, &child)) {
566 int tag = dwarf_tag(&child);
567 if (tag == DW_TAG_member || tag == DW_TAG_inheritance) {
569 // Skip declarations:
570 if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
573 // Skip compile time constants:
574 if (dwarf_hasattr(&child, DW_AT_const_value))
577 // TODO, we should use another type (because is is not a type but a member)
578 simgrid::mc::Type member;
582 member.id = dwarf_dieoffset(&child);
584 const char *name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
588 MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
589 member.element_count = -1;
590 member.type_id = MC_dwarf_at_type(&child);
592 if (dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
593 xbt_die("Can't groke DW_AT_data_bit_offset.");
596 MC_dwarf_fill_member_location(type, &member, &child);
598 if (member.type_id.empty()) {
599 xbt_die("Missing type for member %s of <%" PRIx64 ">%s",
601 (uint64_t) type->id, type->name.c_str());
604 type->members.push_back(std::move(member));
609 /** \brief Create a MC type object from a DIE
611 * \param info current object info object
612 * \param DIE (for a given type);
613 * \param unit compilation unit of the current DIE
614 * \return MC representation of the type
616 static mc_type_t MC_dwarf_die_to_type(mc_object_info_t info, Dwarf_Die * die,
617 Dwarf_Die * unit, mc_frame_t frame,
621 mc_type_t type = new simgrid::mc::Type();
623 type->name = std::string();
624 type->element_count = -1;
626 type->type = dwarf_tag(die);
629 type->id = dwarf_dieoffset(die);
631 const char *prefix = "";
632 switch (type->type) {
633 case DW_TAG_structure_type:
636 case DW_TAG_union_type:
639 case DW_TAG_class_type:
646 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
648 char* full_name = ns ? bprintf("%s%s::%s", prefix, ns, name) :
649 bprintf("%s%s", prefix, name);
650 type->name = std::string(full_name);
654 type->type_id = MC_dwarf_at_type(die);
656 // Some compilers do not emit DW_AT_byte_size for pointer_type,
657 // so we fill this. We currently assume that the model-checked process is in
658 // the same architecture..
659 if (type->type == DW_TAG_pointer_type)
660 type->byte_size = sizeof(void*);
662 // Computation of the byte_size;
663 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
664 type->byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
665 else if (type->type == DW_TAG_array_type
666 || type->type == DW_TAG_structure_type
667 || type->type == DW_TAG_class_type) {
669 if (dwarf_aggregate_size(die, &size) == 0) {
670 type->byte_size = size;
674 switch (type->type) {
675 case DW_TAG_array_type:
676 type->element_count = MC_dwarf_array_element_count(die, unit);
677 // TODO, handle DW_byte_stride and (not) DW_bit_stride
680 case DW_TAG_pointer_type:
681 case DW_TAG_reference_type:
682 case DW_TAG_rvalue_reference_type:
683 type->is_pointer_type = 1;
686 case DW_TAG_structure_type:
687 case DW_TAG_union_type:
688 case DW_TAG_class_type:
689 MC_dwarf_add_members(info, die, unit, type);
690 char *new_ns = ns == NULL ? xbt_strdup(type->name.c_str())
691 : bprintf("%s::%s", ns, name);
692 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
700 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
701 Dwarf_Die * unit, mc_frame_t frame,
704 mc_type_t type = MC_dwarf_die_to_type(info, die, unit, frame, ns);
706 char *key = bprintf("%" PRIx64, (uint64_t) type->id);
707 xbt_dict_set(info->types, key, type, NULL);
710 if (!type->name.empty() && type->byte_size != 0) {
711 xbt_dict_set(info->full_types_by_name, type->name.c_str(), type, NULL);
715 static int mc_anonymous_variable_index = 0;
717 static std::unique_ptr<simgrid::mc::Variable> MC_die_to_variable(
718 mc_object_info_t info, Dwarf_Die * die,
719 Dwarf_Die * unit, mc_frame_t frame,
722 // Skip declarations:
723 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
726 // Skip compile time constants:
727 if (dwarf_hasattr(die, DW_AT_const_value))
730 Dwarf_Attribute attr_location;
731 if (dwarf_attr(die, DW_AT_location, &attr_location) == NULL) {
732 // No location: do not add it ?
736 std::unique_ptr<simgrid::mc::Variable> variable =
737 std::unique_ptr<simgrid::mc::Variable>(new simgrid::mc::Variable());
738 variable->dwarf_offset = dwarf_dieoffset(die);
739 variable->global = frame == NULL; // Can be override base on DW_AT_location
740 variable->object_info = info;
742 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
744 variable->name = name;
745 variable->type_id = MC_dwarf_at_type(die);
747 int form = dwarf_whatform(&attr_location);
750 DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
752 case MC_DW_CLASS_EXPRLOC:
753 case MC_DW_CLASS_BLOCK:
754 // Location expression:
758 if (dwarf_getlocation(&attr_location, &expr, &len)) {
760 "Could not read location expression in DW_AT_location "
761 "of variable <%" PRIx64 ">%s",
762 (uint64_t) variable->dwarf_offset,
763 variable->name.c_str());
766 if (len == 1 && expr[0].atom == DW_OP_addr) {
767 variable->global = 1;
768 uintptr_t offset = (uintptr_t) expr[0].number;
769 uintptr_t base = (uintptr_t) info->base_address();
770 variable->address = (void *) (base + offset);
772 simgrid::mc::LocationListEntry entry;
773 entry.expression = {expr, expr + len};
774 variable->location_list = { std::move(entry) };
779 case MC_DW_CLASS_LOCLISTPTR:
780 case MC_DW_CLASS_CONSTANT:
781 // Reference to location list:
782 mc_dwarf_location_list_init(
783 &variable->location_list, info, die,
787 xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location "
788 "in <%" PRIx64 ">%s",
789 form, form, klass, klass,
790 (uint64_t) variable->dwarf_offset,
791 variable->name.c_str());
794 // Handle start_scope:
795 if (dwarf_hasattr(die, DW_AT_start_scope)) {
796 Dwarf_Attribute attr;
797 dwarf_attr(die, DW_AT_start_scope, &attr);
798 int form = dwarf_whatform(&attr);
799 int klass = MC_dwarf_form_get_class(form);
801 case MC_DW_CLASS_CONSTANT:
804 variable->start_scope =
805 dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
808 case MC_DW_CLASS_RANGELISTPTR: // TODO
811 ("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
812 form, klass, name == NULL ? "?" : name);
816 if (ns && variable->global)
818 std::string(ns) + "::" + variable->name;
820 // The current code needs a variable name,
821 // generate a fake one:
822 if (variable->name.empty())
824 "@anonymous#" + std::to_string(mc_anonymous_variable_index++);
826 return std::move(variable);
829 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
830 Dwarf_Die * unit, mc_frame_t frame,
833 MC_dwarf_register_variable(info, frame,
834 MC_die_to_variable(info, die, unit, frame, ns));
837 static void MC_dwarf_handle_scope_die(mc_object_info_t info, Dwarf_Die * die,
838 Dwarf_Die * unit, mc_frame_t parent_frame,
841 // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
842 int tag = dwarf_tag(die);
843 mc_tag_class klass = MC_dwarf_tag_classify(tag);
845 // (Template) Subprogram declaration:
846 if (klass == mc_tag_subprogram
847 && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
850 if (klass == mc_tag_scope)
851 xbt_assert(parent_frame, "No parent scope for this scope");
853 simgrid::mc::Frame frame;
856 frame.id = dwarf_dieoffset(die);
857 frame.object_info = info;
859 if (klass == mc_tag_subprogram) {
860 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
862 frame.name = std::string(ns) + "::" + name;
867 frame.abstract_origin_id =
868 MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
870 // This is the base address for DWARF addresses.
871 // Relocated addresses are offset from this base address.
872 // See DWARF4 spec 7.5
873 void *base = info->base_address();
875 // TODO, support DW_AT_ranges
876 uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
877 frame.low_pc = low_pc ? ((char *) base) + low_pc : 0;
880 Dwarf_Attribute attr;
881 if (!dwarf_attr_integrate(die, DW_AT_high_pc, &attr)) {
882 xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
888 switch (MC_dwarf_form_get_class(dwarf_whatform(&attr))) {
890 // DW_AT_high_pc if an offset from the low_pc:
891 case MC_DW_CLASS_CONSTANT:
893 if (dwarf_formsdata(&attr, &offset) != 0)
894 xbt_die("Could not read constant");
895 frame.high_pc = (void *) ((char *) frame.low_pc + offset);
898 // DW_AT_high_pc is a relocatable address:
899 case MC_DW_CLASS_ADDRESS:
900 if (dwarf_formaddr(&attr, &high_pc) != 0)
901 xbt_die("Could not read address");
902 frame.high_pc = ((char *) base) + high_pc;
906 xbt_die("Unexpected class for DW_AT_high_pc");
911 if (klass == mc_tag_subprogram) {
912 Dwarf_Attribute attr_frame_base;
913 if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
914 mc_dwarf_location_list_init(&frame.frame_base, info, die,
919 MC_dwarf_handle_children(info, die, unit, &frame, ns);
922 if (klass == mc_tag_subprogram) {
923 char *key = bprintf("%" PRIx64, (uint64_t) frame.id);
925 xbt_dict_set(info->subprograms, key,
926 new simgrid::mc::Frame(std::move(frame)), NULL);
928 } else if (klass == mc_tag_scope)
929 parent_frame->scopes.push_back(std::move(frame));
932 static void mc_dwarf_handle_namespace_die(mc_object_info_t info,
933 Dwarf_Die * die, Dwarf_Die * unit,
937 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
939 xbt_die("Unexpected namespace in a subprogram");
940 char *new_ns = ns == NULL ? xbt_strdup(name)
941 : bprintf("%s::%s", ns, name);
942 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
946 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
947 Dwarf_Die * unit, mc_frame_t frame,
950 // For each child DIE:
953 for (res = dwarf_child(die, &child); res == 0;
954 res = dwarf_siblingof(&child, &child)) {
955 MC_dwarf_handle_die(info, &child, unit, frame, ns);
959 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
960 Dwarf_Die * unit, mc_frame_t frame,
963 int tag = dwarf_tag(die);
964 mc_tag_class klass = MC_dwarf_tag_classify(tag);
969 MC_dwarf_handle_type_die(info, die, unit, frame, ns);
972 // Subprogram or scope:
973 case mc_tag_subprogram:
975 MC_dwarf_handle_scope_die(info, die, unit, frame, ns);
979 case mc_tag_variable:
980 MC_dwarf_handle_variable_die(info, die, unit, frame, ns);
983 case mc_tag_namespace:
984 mc_dwarf_handle_namespace_die(info, die, unit, frame, ns);
993 /** \brief Populate the debugging informations of the given ELF object
995 * Read the DWARf information of the EFFL object and populate the
996 * lists of types, variables, functions.
998 void MC_dwarf_get_variables(mc_object_info_t info)
1000 int fd = open(info->file_name, O_RDONLY);
1002 xbt_die("Could not open file %s", info->file_name);
1004 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
1005 if (dwarf == NULL) {
1006 xbt_die("Your program must be compiled with -g (%s)", info->file_name);
1008 // For each compilation unit:
1009 Dwarf_Off offset = 0;
1010 Dwarf_Off next_offset = 0;
1012 while (dwarf_nextcu(dwarf, offset, &next_offset, &length, NULL, NULL, NULL) ==
1015 if (dwarf_offdie(dwarf, offset + length, &unit_die) != NULL) {
1017 // For each child DIE:
1020 for (res = dwarf_child(&unit_die, &child); res == 0;
1021 res = dwarf_siblingof(&child, &child)) {
1022 MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
1026 offset = next_offset;
1033 /************************** Free functions *************************/
1035 static void dw_type_free(mc_type_t t)
1040 // ***** object_info
1045 ObjectInformation::ObjectInformation()
1048 this->file_name = nullptr;
1049 this->start = nullptr;
1050 this->end = nullptr;
1051 this->start_exec = nullptr;
1052 this->end_exec = nullptr;
1053 this->start_rw = nullptr;
1054 this->end_rw = nullptr;
1055 this->start_ro = nullptr;
1056 this->end_ro = nullptr;
1057 this->subprograms = xbt_dict_new_homogeneous(mc_frame_free);
1058 this->types = xbt_dict_new_homogeneous((void (*)(void *)) dw_type_free);
1059 this->full_types_by_name = xbt_dict_new_homogeneous(NULL);
1060 this->functions_index = nullptr;
1063 ObjectInformation::~ObjectInformation()
1065 xbt_free(this->file_name);
1066 xbt_dict_free(&this->subprograms);
1067 xbt_dict_free(&this->types);
1068 xbt_dict_free(&this->full_types_by_name);
1069 xbt_dynar_free(&this->functions_index);
1072 /** Find the DWARF offset for this ELF object
1074 * An offset is applied to address found in DWARF:
1077 * <li>for an executable obejct, addresses are virtual address
1078 * (there is no offset) i.e. \f$\text{virtual address} = \{dwarf address}\f$;</li>
1079 * <li>for a shared object, the addreses are offset from the begining
1080 * of the shared object (the base address of the mapped shared
1081 * object must be used as offset
1082 * i.e. \f$\text{virtual address} = \text{shared object base address}
1083 * + \text{dwarf address}\f$.</li>
1086 void *ObjectInformation::base_address() const
1088 if (this->executable())
1091 void *result = this->start_exec;
1092 if (this->start_rw != NULL && result > (void *) this->start_rw)
1093 result = this->start_rw;
1094 if (this->start_ro != NULL && result > (void *) this->start_ro)
1095 result = this->start_ro;
1104 // ***** Functions index
1106 static int MC_compare_frame_index_items(mc_function_index_item_t a,
1107 mc_function_index_item_t b)
1109 if (a->low_pc < b->low_pc)
1111 else if (a->low_pc == b->low_pc)
1117 static void MC_make_functions_index(mc_object_info_t info)
1119 xbt_dynar_t index = xbt_dynar_new(sizeof(s_mc_function_index_item_t), NULL);
1121 // Populate the array:
1122 mc_frame_t frame = NULL;
1123 xbt_dict_cursor_t cursor;
1125 xbt_dict_foreach(info->subprograms, cursor, key, frame) {
1126 if (frame->low_pc == NULL)
1128 s_mc_function_index_item_t entry;
1129 entry.low_pc = frame->low_pc;
1130 entry.high_pc = frame->high_pc;
1131 entry.function = frame;
1132 xbt_dynar_push(index, &entry);
1135 mc_function_index_item_t base =
1136 (mc_function_index_item_t) xbt_dynar_get_ptr(index, 0);
1138 // Sort the array by low_pc:
1140 xbt_dynar_length(index),
1141 sizeof(s_mc_function_index_item_t),
1142 (int (*)(const void *, const void *)) MC_compare_frame_index_items);
1144 info->functions_index = index;
1147 static void MC_post_process_variables(mc_object_info_t info)
1149 for(simgrid::mc::Variable& variable : info->global_variables)
1150 if (!variable.type_id.empty())
1151 variable.type = (mc_type_t) xbt_dict_get_or_null(
1152 info->types, variable.type_id.c_str());
1155 static void mc_post_process_scope(mc_object_info_t info, mc_frame_t scope)
1158 if (scope->tag == DW_TAG_inlined_subroutine) {
1160 // Attach correct namespaced name in inlined subroutine:
1161 char *key = bprintf("%" PRIx64, (uint64_t) scope->abstract_origin_id);
1162 mc_frame_t abstract_origin = (mc_frame_t) xbt_dict_get_or_null(info->subprograms, key);
1163 xbt_assert(abstract_origin, "Could not lookup abstract origin %s", key);
1165 scope->name = abstract_origin->name;
1169 for (simgrid::mc::Variable& variable : scope->variables)
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 for (simgrid::mc::Frame& nested_scope : scope->scopes)
1176 mc_post_process_scope(info, &nested_scope);
1180 static void MC_post_process_functions(mc_object_info_t info)
1182 xbt_dict_cursor_t cursor;
1184 mc_frame_t subprogram = NULL;
1185 xbt_dict_foreach(info->subprograms, cursor, key, subprogram) {
1186 mc_post_process_scope(info, subprogram);
1191 /** \brief Fill/lookup the "subtype" field.
1193 static void MC_resolve_subtype(mc_object_info_t info, mc_type_t type)
1196 if (type->type_id.empty())
1198 type->subtype = (mc_type_t) xbt_dict_get_or_null(
1199 info->types, type->type_id.c_str());
1200 if (type->subtype == NULL)
1202 if (type->subtype->byte_size != 0)
1204 if (type->subtype->name.empty())
1206 // Try to find a more complete description of the type:
1207 // We need to fix in order to support C++.
1210 (mc_type_t) xbt_dict_get_or_null(
1211 info->full_types_by_name, type->subtype->name.c_str());
1212 if (subtype != NULL) {
1213 type->subtype = subtype;
1218 static void MC_post_process_types(mc_object_info_t info)
1220 xbt_dict_cursor_t cursor = NULL;
1224 // Lookup "subtype" field:
1225 xbt_dict_foreach(info->types, cursor, origin, type) {
1226 MC_resolve_subtype(info, type);
1227 for (simgrid::mc::Type& member : type->members)
1228 MC_resolve_subtype(info, &member);
1232 /** \brief Finds informations about a given shared object/executable */
1233 std::shared_ptr<s_mc_object_info_t> MC_find_object_info(
1234 std::vector<simgrid::mc::VmMap> const& maps, const char *name, int executable)
1236 std::shared_ptr<s_mc_object_info_t> result =
1237 std::make_shared<s_mc_object_info_t>();
1239 result->flags |= MC_OBJECT_INFO_EXECUTABLE;
1240 result->file_name = xbt_strdup(name);
1241 MC_find_object_address(maps, result.get());
1242 MC_dwarf_get_variables(result.get());
1243 MC_post_process_types(result.get());
1244 MC_post_process_variables(result.get());
1245 MC_post_process_functions(result.get());
1246 MC_make_functions_index(result.get());
1247 return std::move(result);
1250 /*************************************************************************/
1252 static int MC_dwarf_get_variable_index(
1253 std::vector<simgrid::mc::Variable> variables, const char *var, void *address)
1256 if (variables.empty())
1259 unsigned int cursor = 0;
1261 int end = variables.size() - 1;
1262 mc_variable_t var_test = nullptr;
1264 while (start <= end) {
1265 cursor = (start + end) / 2;
1266 var_test = &variables[cursor];
1267 if (strcmp(var_test->name.c_str(), var) < 0) {
1269 } else if (strcmp(var_test->name.c_str(), var) > 0) {
1272 if (address) { /* global variable */
1273 if (var_test->address == address)
1275 if (var_test->address > address)
1279 } else { /* local variable */
1285 if (strcmp(var_test->name.c_str(), var) == 0) {
1286 if (address && var_test->address < address)
1290 } else if (strcmp(var_test->name.c_str(), var) < 0)
1297 void MC_dwarf_register_global_variable(
1298 mc_object_info_t info,
1299 std::unique_ptr<simgrid::mc::Variable> variable)
1302 MC_dwarf_get_variable_index(info->global_variables,
1303 variable->name.c_str(),
1306 info->global_variables.insert(
1307 info->global_variables.begin() + index, std::move(*variable));
1311 void MC_dwarf_register_non_global_variable(
1312 mc_object_info_t info,
1314 std::unique_ptr<simgrid::mc::Variable> variable)
1316 xbt_assert(frame, "Frame is NULL");
1318 MC_dwarf_get_variable_index(
1319 frame->variables, variable->name.c_str(), NULL);
1321 frame->variables.insert(
1322 frame->variables.begin() + index, std::move(*variable));
1326 void MC_dwarf_register_variable(
1327 mc_object_info_t info, mc_frame_t frame,
1328 std::unique_ptr<simgrid::mc::Variable> variable)
1332 if (variable->global)
1333 MC_dwarf_register_global_variable(info, std::move(variable));
1334 else if (frame != nullptr)
1335 MC_dwarf_register_non_global_variable(info, frame, std::move(variable));
1337 xbt_die("No frame for this local variable");
1340 void MC_post_process_object_info(mc_process_t process, mc_object_info_t info)
1342 xbt_dict_cursor_t cursor = NULL;
1344 mc_type_t type = NULL;
1345 xbt_dict_foreach(info->types, cursor, key, type) {
1347 mc_type_t subtype = type;
1348 while (subtype->type == DW_TAG_typedef || subtype->type == DW_TAG_volatile_type
1349 || subtype->type == DW_TAG_const_type) {
1350 if (subtype->subtype)
1351 subtype = subtype->subtype;
1356 // Resolve full_type:
1357 if (!subtype->name.empty() && subtype->byte_size == 0) {
1358 for (auto const& object_info : process->object_infos) {
1359 mc_type_t same_type = (mc_type_t)
1360 xbt_dict_get_or_null(object_info->full_types_by_name,
1361 subtype->name.c_str());
1362 if (same_type && !same_type->name.empty() && same_type->byte_size) {
1363 type->full_type = same_type;
1367 } else type->full_type = subtype;