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 dw_variable_free(dw_variable_t v);
22 static void dw_variable_free_voidp(void *t);
24 static void MC_dwarf_register_global_variable(mc_object_info_t info, dw_variable_t variable);
25 static void MC_register_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
26 static void MC_dwarf_register_non_global_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
27 static void MC_dwarf_register_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
29 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing");
31 /** \brief The default DW_TAG_lower_bound for a given DW_AT_language.
33 * The default for a given language is defined in the DWARF spec.
35 * \param language consant as defined by the DWARf spec
37 static uint64_t MC_dwarf_default_lower_bound(int lang);
39 /** \brief Computes the the element_count of a DW_TAG_enumeration_type DIE
41 * This is the number of elements in a given array dimension.
43 * A reference of the compilation unit (DW_TAG_compile_unit) is
44 * needed because the default lower bound (when there is no DW_AT_lower_bound)
45 * depends of the language of the compilation unit (DW_AT_language).
47 * \param die DIE for the DW_TAG_enumeration_type or DW_TAG_subrange_type
48 * \param unit DIE of the DW_TAG_compile_unit
50 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
53 /** \brief Computes the number of elements of a given DW_TAG_array_type.
55 * \param die DIE for the DW_TAG_array_type
57 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit);
59 /** \brief Process a DIE
61 * \param info the resulting object fot the library/binary file (output)
62 * \param die the current DIE
63 * \param unit the DIE of the compile unit of the current DIE
64 * \param frame containg frame if any
66 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
67 Dwarf_Die * unit, dw_frame_t frame,
70 /** \brief Process a type DIE
72 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
73 Dwarf_Die * unit, dw_frame_t frame,
76 /** \brief Calls MC_dwarf_handle_die on all childrend of the given die
78 * \param info the resulting object fot the library/binary file (output)
79 * \param die the current DIE
80 * \param unit the DIE of the compile unit of the current DIE
81 * \param frame containg frame if any
83 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
84 Dwarf_Die * unit, dw_frame_t frame,
87 /** \brief Handle a variable (DW_TAG_variable or other)
89 * \param info the resulting object fot the library/binary file (output)
90 * \param die the current DIE
91 * \param unit the DIE of the compile unit of the current DIE
92 * \param frame containg frame if any
94 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
95 Dwarf_Die * unit, dw_frame_t frame,
98 /** \brief Get the DW_TAG_type of the DIE
101 * \return DW_TAG_type attribute as a new string (NULL if none)
103 static char *MC_dwarf_at_type(Dwarf_Die * die);
105 /** \brief A class of DWARF tags (DW_TAG_*)
107 typedef enum mc_tag_class {
116 static mc_tag_class MC_dwarf_tag_classify(int tag)
120 case DW_TAG_array_type:
121 case DW_TAG_class_type:
122 case DW_TAG_enumeration_type:
124 case DW_TAG_pointer_type:
125 case DW_TAG_reference_type:
126 case DW_TAG_rvalue_reference_type:
127 case DW_TAG_string_type:
128 case DW_TAG_structure_type:
129 case DW_TAG_subroutine_type:
130 case DW_TAG_union_type:
131 case DW_TAG_ptr_to_member_type:
132 case DW_TAG_set_type:
133 case DW_TAG_subrange_type:
134 case DW_TAG_base_type:
135 case DW_TAG_const_type:
136 case DW_TAG_file_type:
137 case DW_TAG_packed_type:
138 case DW_TAG_volatile_type:
139 case DW_TAG_restrict_type:
140 case DW_TAG_interface_type:
141 case DW_TAG_unspecified_type:
142 case DW_TAG_shared_type:
145 case DW_TAG_subprogram:
146 return mc_tag_subprogram;
148 case DW_TAG_variable:
149 case DW_TAG_formal_parameter:
150 return mc_tag_variable;
152 case DW_TAG_lexical_block:
153 case DW_TAG_try_block:
154 case DW_TAG_catch_block:
155 case DW_TAG_inlined_subroutine:
156 case DW_TAG_with_stmt:
159 case DW_TAG_namespace:
160 return mc_tag_namespace;
163 return mc_tag_unknown;
168 #define MC_DW_CLASS_UNKNOWN 0
169 #define MC_DW_CLASS_ADDRESS 1 // Location in the address space of the program
170 #define MC_DW_CLASS_BLOCK 2 // Arbitrary block of bytes
171 #define MC_DW_CLASS_CONSTANT 3
172 #define MC_DW_CLASS_STRING 3 // String
173 #define MC_DW_CLASS_FLAG 4 // Boolean
174 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
175 #define MC_DW_CLASS_EXPRLOC 6 // DWARF expression/location description
176 #define MC_DW_CLASS_LINEPTR 7
177 #define MC_DW_CLASS_LOCLISTPTR 8
178 #define MC_DW_CLASS_MACPTR 9
179 #define MC_DW_CLASS_RANGELISTPTR 10
181 /** \brief Find the DWARF data class for a given DWARF data form
183 * This mapping is defined in the DWARF spec.
185 * \param form The form (values taken from the DWARF spec)
186 * \return An internal representation for the corresponding class
188 static int MC_dwarf_form_get_class(int form)
192 return MC_DW_CLASS_ADDRESS;
197 return MC_DW_CLASS_BLOCK;
204 return MC_DW_CLASS_CONSTANT;
207 return MC_DW_CLASS_STRING;
208 case DW_FORM_ref_addr:
213 case DW_FORM_ref_udata:
214 return MC_DW_CLASS_REFERENCE;
216 case DW_FORM_flag_present:
217 return MC_DW_CLASS_FLAG;
218 case DW_FORM_exprloc:
219 return MC_DW_CLASS_EXPRLOC;
223 return MC_DW_CLASS_UNKNOWN;
227 /** \brief Get the name of the tag of a given DIE
230 * \return name of the tag of this DIE
232 static inline const char *MC_dwarf_die_tagname(Dwarf_Die * die)
234 return MC_dwarf_tagname(dwarf_tag(die));
239 /** \brief Get an attribute of a given DIE as a string
242 * \param attribute attribute
243 * \return value of the given attribute of the given DIE
245 static const char *MC_dwarf_attr_integrate_string(Dwarf_Die * die,
248 Dwarf_Attribute attr;
249 if (!dwarf_attr_integrate(die, attribute, &attr)) {
252 return dwarf_formstring(&attr);
256 /** \brief Get the linkage name of a DIE.
258 * Use either DW_AT_linkage_name or DW_AT_MIPS_linkage_name.
259 * DW_AT_linkage_name is standardized since DWARF 4.
260 * Before this version of DWARF, the MIPS extensions
261 * DW_AT_MIPS_linkage_name is used (at least by GCC).
264 * \return linkage name of the given DIE (or NULL)
266 static const char *MC_dwarf_at_linkage_name(Dwarf_Die * die)
268 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_linkage_name);
270 name = MC_dwarf_attr_integrate_string(die, DW_AT_MIPS_linkage_name);
274 static Dwarf_Off MC_dwarf_attr_dieoffset(Dwarf_Die * die, int attribute)
276 Dwarf_Attribute attr;
277 if (dwarf_hasattr_integrate(die, attribute)) {
278 dwarf_attr_integrate(die, attribute, &attr);
279 Dwarf_Die subtype_die;
280 if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
281 xbt_die("Could not find DIE");
283 return dwarf_dieoffset(&subtype_die);
288 static Dwarf_Off MC_dwarf_attr_integrate_dieoffset(Dwarf_Die * die,
291 Dwarf_Attribute attr;
292 if (dwarf_hasattr_integrate(die, attribute)) {
293 dwarf_attr_integrate(die, DW_AT_type, &attr);
294 Dwarf_Die subtype_die;
295 if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
296 xbt_die("Could not find DIE");
298 return dwarf_dieoffset(&subtype_die);
303 /** \brief Find the type/subtype (DW_AT_type) for a DIE
306 * \return DW_AT_type reference as a global offset in hexadecimal (or NULL)
308 static char *MC_dwarf_at_type(Dwarf_Die * die)
310 Dwarf_Off offset = MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
311 return offset == 0 ? NULL : bprintf("%" PRIx64, offset);
314 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die * die, int attribute)
316 Dwarf_Attribute attr;
317 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
320 if (dwarf_formaddr(&attr, &value) == 0)
321 return (uint64_t) value;
326 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die * die, int attribute,
327 uint64_t default_value)
329 Dwarf_Attribute attr;
330 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
331 return default_value;
333 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr),
334 &value) == 0 ? (uint64_t) value : default_value;
337 static bool MC_dwarf_attr_flag(Dwarf_Die * die, int attribute, bool integrate)
339 Dwarf_Attribute attr;
340 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
341 : dwarf_attr(die, attribute, &attr)) == 0)
345 if (dwarf_formflag(&attr, &result))
346 xbt_die("Unexpected form for attribute %s", MC_dwarf_attrname(attribute));
350 /** \brief Find the default lower bound for a given language
352 * The default lower bound of an array (when DW_TAG_lower_bound
353 * is missing) depends on the language of the compilation unit.
355 * \param lang Language of the compilation unit (values defined in the DWARF spec)
356 * \return Default lower bound of an array in this compilation unit
358 static uint64_t MC_dwarf_default_lower_bound(int lang)
364 case DW_LANG_C_plus_plus:
368 case DW_LANG_ObjC_plus_plus:
374 case DW_LANG_Fortran77:
375 case DW_LANG_Fortran90:
376 case DW_LANG_Fortran95:
377 case DW_LANG_Modula2:
378 case DW_LANG_Pascal83:
380 case DW_LANG_Cobol74:
381 case DW_LANG_Cobol85:
384 xbt_die("No default DW_TAG_lower_bound for language %i and none given",
390 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
393 * \param unit DIE of the compilation unit
394 * \return number of elements in the range
396 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
399 xbt_assert(dwarf_tag(die) == DW_TAG_enumeration_type
400 || dwarf_tag(die) == DW_TAG_subrange_type,
401 "MC_dwarf_subrange_element_count called with DIE of type %s",
402 MC_dwarf_die_tagname(die));
404 // Use DW_TAG_count if present:
405 if (dwarf_hasattr_integrate(die, DW_AT_count)) {
406 return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
408 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
410 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
411 // This is not really 0, but the code expects this (we do not know):
414 uint64_t upper_bound =
415 MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
417 uint64_t lower_bound = 0;
418 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
419 lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
421 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
423 return upper_bound - lower_bound + 1;
426 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
428 * The compilation unit might be needed because the default lower
429 * bound depends on the language of the compilation unit.
431 * \param die the DIE of the DW_TAG_array_type
432 * \param unit the DIE of the compilation unit
433 * \return number of elements in this array type
435 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit)
437 xbt_assert(dwarf_tag(die) == DW_TAG_array_type,
438 "MC_dwarf_array_element_count called with DIE of type %s",
439 MC_dwarf_die_tagname(die));
444 for (res = dwarf_child(die, &child); res == 0;
445 res = dwarf_siblingof(&child, &child)) {
446 int child_tag = dwarf_tag(&child);
447 if (child_tag == DW_TAG_subrange_type
448 || child_tag == DW_TAG_enumeration_type) {
449 result *= MC_dwarf_subrange_element_count(&child, unit);
457 /** \brief Initialize the location of a member of a type
458 * (DW_AT_data_member_location of a DW_TAG_member).
460 * \param type a type (struct, class)
461 * \param member the member of the type
462 * \param child DIE of the member (DW_TAG_member)
464 static void MC_dwarf_fill_member_location(mc_type_t type, mc_type_t member,
467 if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
468 xbt_die("Can't groke DW_AT_data_bit_offset.");
471 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
472 if (type->type != DW_TAG_union_type) {
474 ("Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%"
475 PRIx64 ">%s", member->name.c_str(),
476 (uint64_t) type->id, type->name.c_str());
482 Dwarf_Attribute attr;
483 dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
484 int form = dwarf_whatform(&attr);
485 int klass = MC_dwarf_form_get_class(form);
487 case MC_DW_CLASS_EXPRLOC:
488 case MC_DW_CLASS_BLOCK:
489 // Location expression:
493 if (dwarf_getlocation(&attr, &expr, &len)) {
495 ("Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%"
496 PRIx64 ">%s", MC_dwarf_attr_integrate_string(child, DW_AT_name),
497 (uint64_t) type->id, type->name.c_str());
499 simgrid::mc::DwarfExpression(expr, expr+len);
502 case MC_DW_CLASS_CONSTANT:
503 // Offset from the base address of the object:
506 if (!dwarf_formudata(&attr, &offset))
507 member->offset(offset);
509 xbt_die("Cannot get %s location <%" PRIx64 ">%s",
510 MC_dwarf_attr_integrate_string(child, DW_AT_name),
511 (uint64_t) type->id, type->name.c_str());
514 case MC_DW_CLASS_LOCLISTPTR:
515 // Reference to a location list:
517 case MC_DW_CLASS_REFERENCE:
518 // It's supposed to be possible in DWARF2 but I couldn't find its semantic
521 xbt_die("Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
527 static void dw_type_free_voidp(void *t)
529 delete *(mc_type_t*)t;
532 /** \brief Populate the list of members of a type
534 * \param info ELF object containing the type DIE
535 * \param die DIE of the type
536 * \param unit DIE of the compilation unit containing the type DIE
537 * \param type the type
539 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die * die,
540 Dwarf_Die * unit, mc_type_t type)
544 xbt_assert(!type->members);
546 xbt_dynar_new(sizeof(mc_type_t), (void (*)(void *)) dw_type_free_voidp);
547 for (res = dwarf_child(die, &child); res == 0;
548 res = dwarf_siblingof(&child, &child)) {
549 int tag = dwarf_tag(&child);
550 if (tag == DW_TAG_member || tag == DW_TAG_inheritance) {
552 // Skip declarations:
553 if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
556 // Skip compile time constants:
557 if (dwarf_hasattr(&child, DW_AT_const_value))
560 // TODO, we should use another type (because is is not a type but a member)
561 mc_type_t member = new simgrid::mc::Type();
565 member->id = dwarf_dieoffset(&child);
567 const char *name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
571 MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
572 member->element_count = -1;
574 char* type_id = MC_dwarf_at_type(&child);
576 member->dw_type_id = type_id;
580 if (dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
581 xbt_die("Can't groke DW_AT_data_bit_offset.");
584 MC_dwarf_fill_member_location(type, member, &child);
586 if (member->dw_type_id.empty()) {
587 xbt_die("Missing type for member %s of <%" PRIx64 ">%s",
588 member->name.c_str(),
589 (uint64_t) type->id, type->name.c_str());
592 xbt_dynar_push(type->members, &member);
597 /** \brief Create a MC type object from a DIE
599 * \param info current object info object
600 * \param DIE (for a given type);
601 * \param unit compilation unit of the current DIE
602 * \return MC representation of the type
604 static mc_type_t MC_dwarf_die_to_type(mc_object_info_t info, Dwarf_Die * die,
605 Dwarf_Die * unit, dw_frame_t frame,
609 mc_type_t type = new simgrid::mc::Type();
611 type->name = std::string();
612 type->element_count = -1;
614 type->type = dwarf_tag(die);
617 type->id = dwarf_dieoffset(die);
619 const char *prefix = "";
620 switch (type->type) {
621 case DW_TAG_structure_type:
624 case DW_TAG_union_type:
627 case DW_TAG_class_type:
634 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
636 char* full_name = ns ? bprintf("%s%s::%s", prefix, ns, name) :
637 bprintf("%s%s", prefix, name);
638 type->name = std::string(full_name);
642 char* type_id = MC_dwarf_at_type(die);
644 type->dw_type_id = type_id;
648 // Some compilers do not emit DW_AT_byte_size for pointer_type,
649 // so we fill this. We currently assume that the model-checked process is in
650 // the same architecture..
651 if (type->type == DW_TAG_pointer_type)
652 type->byte_size = sizeof(void*);
654 // Computation of the byte_size;
655 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
656 type->byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
657 else if (type->type == DW_TAG_array_type
658 || type->type == DW_TAG_structure_type
659 || type->type == DW_TAG_class_type) {
661 if (dwarf_aggregate_size(die, &size) == 0) {
662 type->byte_size = size;
666 switch (type->type) {
667 case DW_TAG_array_type:
668 type->element_count = MC_dwarf_array_element_count(die, unit);
669 // TODO, handle DW_byte_stride and (not) DW_bit_stride
672 case DW_TAG_pointer_type:
673 case DW_TAG_reference_type:
674 case DW_TAG_rvalue_reference_type:
675 type->is_pointer_type = 1;
678 case DW_TAG_structure_type:
679 case DW_TAG_union_type:
680 case DW_TAG_class_type:
681 MC_dwarf_add_members(info, die, unit, type);
682 char *new_ns = ns == NULL ? xbt_strdup(type->name.c_str())
683 : bprintf("%s::%s", ns, name);
684 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
692 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
693 Dwarf_Die * unit, dw_frame_t frame,
696 mc_type_t type = MC_dwarf_die_to_type(info, die, unit, frame, ns);
698 char *key = bprintf("%" PRIx64, (uint64_t) type->id);
699 xbt_dict_set(info->types, key, type, NULL);
702 if (!type->name.empty() && type->byte_size != 0) {
703 xbt_dict_set(info->full_types_by_name, type->name.c_str(), type, NULL);
707 static int mc_anonymous_variable_index = 0;
709 static dw_variable_t MC_die_to_variable(mc_object_info_t info, Dwarf_Die * die,
710 Dwarf_Die * unit, dw_frame_t frame,
713 // Skip declarations:
714 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
717 // Skip compile time constants:
718 if (dwarf_hasattr(die, DW_AT_const_value))
721 Dwarf_Attribute attr_location;
722 if (dwarf_attr(die, DW_AT_location, &attr_location) == NULL) {
723 // No location: do not add it ?
727 dw_variable_t variable = xbt_new0(s_dw_variable_t, 1);
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);
733 variable->name = xbt_strdup(name);
735 variable->type_origin = 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 of variable <%"
751 PRIx64 ">%s", (uint64_t) variable->dwarf_offset, variable->name);
754 if (len == 1 && expr[0].atom == DW_OP_addr) {
755 variable->global = 1;
756 uintptr_t offset = (uintptr_t) expr[0].number;
757 uintptr_t base = (uintptr_t) info->base_address();
758 variable->address = (void *) (base + offset);
760 mc_dwarf_location_list_init_from_expression(&variable->locations, len,
766 case MC_DW_CLASS_LOCLISTPTR:
767 case MC_DW_CLASS_CONSTANT:
768 // Reference to location list:
769 mc_dwarf_location_list_init(&variable->locations, info, die,
773 xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location in <%"
774 PRIx64 ">%s", form, form, klass, klass,
775 (uint64_t) variable->dwarf_offset, variable->name);
778 // Handle start_scope:
779 if (dwarf_hasattr(die, DW_AT_start_scope)) {
780 Dwarf_Attribute attr;
781 dwarf_attr(die, DW_AT_start_scope, &attr);
782 int form = dwarf_whatform(&attr);
783 int klass = MC_dwarf_form_get_class(form);
785 case MC_DW_CLASS_CONSTANT:
788 variable->start_scope =
789 dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
792 case MC_DW_CLASS_RANGELISTPTR: // TODO
795 ("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
796 form, klass, name == NULL ? "?" : name);
800 if (ns && variable->global) {
801 char *old_name = variable->name;
802 variable->name = bprintf("%s::%s", ns, old_name);
805 // The current code needs a variable name,
806 // generate a fake one:
807 if (!variable->name) {
808 variable->name = bprintf("@anonymous#%i", mc_anonymous_variable_index++);
814 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
815 Dwarf_Die * unit, dw_frame_t frame,
818 dw_variable_t variable =
819 MC_die_to_variable(info, die, unit, frame, ns);
820 if (variable == NULL)
822 MC_dwarf_register_variable(info, frame, variable);
825 static void mc_frame_free_voipd(dw_frame_t * p)
831 static void MC_dwarf_handle_scope_die(mc_object_info_t info, Dwarf_Die * die,
832 Dwarf_Die * unit, dw_frame_t parent_frame,
835 // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
836 int tag = dwarf_tag(die);
837 mc_tag_class klass = MC_dwarf_tag_classify(tag);
839 // (Template) Subprogram declaration:
840 if (klass == mc_tag_subprogram
841 && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
844 if (klass == mc_tag_scope)
845 xbt_assert(parent_frame, "No parent scope for this scope");
847 dw_frame_t frame = xbt_new0(s_dw_frame_t, 1);
850 frame->id = dwarf_dieoffset(die);
851 frame->object_info = info;
853 if (klass == mc_tag_subprogram) {
854 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
856 ns ? bprintf("%s::%s", ns, name) : xbt_strdup(name);
859 frame->abstract_origin_id =
860 MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
862 // This is the base address for DWARF addresses.
863 // Relocated addresses are offset from this base address.
864 // See DWARF4 spec 7.5
865 void *base = info->base_address();
867 // Variables are filled in the (recursive) call of MC_dwarf_handle_children:
869 xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
871 // TODO, support DW_AT_ranges
872 uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
873 frame->low_pc = low_pc ? ((char *) base) + low_pc : 0;
876 Dwarf_Attribute attr;
877 if (!dwarf_attr_integrate(die, DW_AT_high_pc, &attr)) {
878 xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
884 switch (MC_dwarf_form_get_class(dwarf_whatform(&attr))) {
886 // DW_AT_high_pc if an offset from the low_pc:
887 case MC_DW_CLASS_CONSTANT:
889 if (dwarf_formsdata(&attr, &offset) != 0)
890 xbt_die("Could not read constant");
891 frame->high_pc = (void *) ((char *) frame->low_pc + offset);
894 // DW_AT_high_pc is a relocatable address:
895 case MC_DW_CLASS_ADDRESS:
896 if (dwarf_formaddr(&attr, &high_pc) != 0)
897 xbt_die("Could not read address");
898 frame->high_pc = ((char *) base) + high_pc;
902 xbt_die("Unexpected class for DW_AT_high_pc");
907 if (klass == mc_tag_subprogram) {
908 Dwarf_Attribute attr_frame_base;
909 if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
910 mc_dwarf_location_list_init(&frame->frame_base, info, die,
915 xbt_dynar_new(sizeof(dw_frame_t), (void_f_pvoid_t) mc_frame_free_voipd);
918 if (klass == mc_tag_subprogram) {
919 char *key = bprintf("%" PRIx64, (uint64_t) frame->id);
920 xbt_dict_set(info->subprograms, key, frame, NULL);
922 } else if (klass == mc_tag_scope) {
923 xbt_dynar_push(parent_frame->scopes, &frame);
926 MC_dwarf_handle_children(info, die, unit, frame, ns);
929 static void mc_dwarf_handle_namespace_die(mc_object_info_t info,
930 Dwarf_Die * die, Dwarf_Die * unit,
934 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
936 xbt_die("Unexpected namespace in a subprogram");
937 char *new_ns = ns == NULL ? xbt_strdup(name)
938 : bprintf("%s::%s", ns, name);
939 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
943 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
944 Dwarf_Die * unit, dw_frame_t frame,
947 // For each child DIE:
950 for (res = dwarf_child(die, &child); res == 0;
951 res = dwarf_siblingof(&child, &child)) {
952 MC_dwarf_handle_die(info, &child, unit, frame, ns);
956 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
957 Dwarf_Die * unit, dw_frame_t frame,
960 int tag = dwarf_tag(die);
961 mc_tag_class klass = MC_dwarf_tag_classify(tag);
966 MC_dwarf_handle_type_die(info, die, unit, frame, ns);
969 // Subprogram or scope:
970 case mc_tag_subprogram:
972 MC_dwarf_handle_scope_die(info, die, unit, frame, ns);
976 case mc_tag_variable:
977 MC_dwarf_handle_variable_die(info, die, unit, frame, ns);
980 case mc_tag_namespace:
981 mc_dwarf_handle_namespace_die(info, die, unit, frame, ns);
990 /** \brief Populate the debugging informations of the given ELF object
992 * Read the DWARf information of the EFFL object and populate the
993 * lists of types, variables, functions.
995 void MC_dwarf_get_variables(mc_object_info_t info)
997 int fd = open(info->file_name, O_RDONLY);
999 xbt_die("Could not open file %s", info->file_name);
1001 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
1002 if (dwarf == NULL) {
1003 xbt_die("Your program must be compiled with -g (%s)", info->file_name);
1005 // For each compilation unit:
1006 Dwarf_Off offset = 0;
1007 Dwarf_Off next_offset = 0;
1009 while (dwarf_nextcu(dwarf, offset, &next_offset, &length, NULL, NULL, NULL) ==
1012 if (dwarf_offdie(dwarf, offset + length, &unit_die) != NULL) {
1014 // For each child DIE:
1017 for (res = dwarf_child(&unit_die, &child); res == 0;
1018 res = dwarf_siblingof(&child, &child)) {
1019 MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
1023 offset = next_offset;
1030 /************************** Free functions *************************/
1032 void mc_frame_free(dw_frame_t frame)
1034 xbt_free(frame->name);
1035 mc_dwarf_location_list_clear(&(frame->frame_base));
1036 xbt_dynar_free(&(frame->variables));
1037 xbt_dynar_free(&(frame->scopes));
1041 static void dw_type_free(mc_type_t t)
1046 void dw_variable_free(dw_variable_t v)
1050 xbt_free(v->type_origin);
1052 if (v->locations.locations)
1053 mc_dwarf_location_list_clear(&v->locations);
1058 void dw_variable_free_voidp(void *t)
1060 dw_variable_free((dw_variable_t) * (void **) t);
1063 // ***** object_info
1068 ObjectInformation::ObjectInformation()
1071 this->file_name = nullptr;
1072 this->start = nullptr;
1073 this->end = nullptr;
1074 this->start_exec = nullptr;
1075 this->end_exec = nullptr;
1076 this->start_rw = nullptr;
1077 this->end_rw = nullptr;
1078 this->start_ro = nullptr;
1079 this->end_ro = nullptr;
1080 this->subprograms = xbt_dict_new_homogeneous((void (*)(void *)) mc_frame_free);
1081 this->global_variables =
1082 xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
1083 this->types = xbt_dict_new_homogeneous((void (*)(void *)) dw_type_free);
1084 this->full_types_by_name = xbt_dict_new_homogeneous(NULL);
1085 this->functions_index = nullptr;
1088 ObjectInformation::~ObjectInformation()
1090 xbt_free(this->file_name);
1091 xbt_dict_free(&this->subprograms);
1092 xbt_dynar_free(&this->global_variables);
1093 xbt_dict_free(&this->types);
1094 xbt_dict_free(&this->full_types_by_name);
1095 xbt_dynar_free(&this->functions_index);
1098 /** Find the DWARF offset for this ELF object
1100 * An offset is applied to address found in DWARF:
1103 * <li>for an executable obejct, addresses are virtual address
1104 * (there is no offset) i.e. \f$\text{virtual address} = \{dwarf address}\f$;</li>
1105 * <li>for a shared object, the addreses are offset from the begining
1106 * of the shared object (the base address of the mapped shared
1107 * object must be used as offset
1108 * i.e. \f$\text{virtual address} = \text{shared object base address}
1109 * + \text{dwarf address}\f$.</li>
1112 void *ObjectInformation::base_address() const
1114 if (this->executable())
1117 void *result = this->start_exec;
1118 if (this->start_rw != NULL && result > (void *) this->start_rw)
1119 result = this->start_rw;
1120 if (this->start_ro != NULL && result > (void *) this->start_ro)
1121 result = this->start_ro;
1130 // ***** Functions index
1132 static int MC_compare_frame_index_items(mc_function_index_item_t a,
1133 mc_function_index_item_t b)
1135 if (a->low_pc < b->low_pc)
1137 else if (a->low_pc == b->low_pc)
1143 static void MC_make_functions_index(mc_object_info_t info)
1145 xbt_dynar_t index = xbt_dynar_new(sizeof(s_mc_function_index_item_t), NULL);
1147 // Populate the array:
1148 dw_frame_t frame = NULL;
1149 xbt_dict_cursor_t cursor;
1151 xbt_dict_foreach(info->subprograms, cursor, key, frame) {
1152 if (frame->low_pc == NULL)
1154 s_mc_function_index_item_t entry;
1155 entry.low_pc = frame->low_pc;
1156 entry.high_pc = frame->high_pc;
1157 entry.function = frame;
1158 xbt_dynar_push(index, &entry);
1161 mc_function_index_item_t base =
1162 (mc_function_index_item_t) xbt_dynar_get_ptr(index, 0);
1164 // Sort the array by low_pc:
1166 xbt_dynar_length(index),
1167 sizeof(s_mc_function_index_item_t),
1168 (int (*)(const void *, const void *)) MC_compare_frame_index_items);
1170 info->functions_index = index;
1173 static void MC_post_process_variables(mc_object_info_t info)
1175 unsigned cursor = 0;
1176 dw_variable_t variable = NULL;
1177 xbt_dynar_foreach(info->global_variables, cursor, variable) {
1178 if (variable->type_origin) {
1179 variable->type = (mc_type_t) xbt_dict_get_or_null(info->types, variable->type_origin);
1184 static void mc_post_process_scope(mc_object_info_t info, dw_frame_t scope)
1187 if (scope->tag == DW_TAG_inlined_subroutine) {
1189 // Attach correct namespaced name in inlined subroutine:
1190 char *key = bprintf("%" PRIx64, (uint64_t) scope->abstract_origin_id);
1191 dw_frame_t abstract_origin = (dw_frame_t) xbt_dict_get_or_null(info->subprograms, key);
1192 xbt_assert(abstract_origin, "Could not lookup abstract origin %s", key);
1194 scope->name = xbt_strdup(abstract_origin->name);
1198 unsigned cursor = 0;
1199 dw_variable_t variable = NULL;
1200 xbt_dynar_foreach(scope->variables, cursor, variable) {
1201 if (variable->type_origin) {
1202 variable->type = (mc_type_t) xbt_dict_get_or_null(info->types, variable->type_origin);
1206 // Recursive post-processing of nested-scopes:
1207 dw_frame_t nested_scope = NULL;
1208 xbt_dynar_foreach(scope->scopes, cursor, nested_scope)
1209 mc_post_process_scope(info, nested_scope);
1213 static void MC_post_process_functions(mc_object_info_t info)
1215 xbt_dict_cursor_t cursor;
1217 dw_frame_t subprogram = NULL;
1218 xbt_dict_foreach(info->subprograms, cursor, key, subprogram) {
1219 mc_post_process_scope(info, subprogram);
1224 /** \brief Fill/lookup the "subtype" field.
1226 static void MC_resolve_subtype(mc_object_info_t info, mc_type_t type)
1229 if (type->dw_type_id.empty())
1231 type->subtype = (mc_type_t) xbt_dict_get_or_null(
1232 info->types, type->dw_type_id.c_str());
1233 if (type->subtype == NULL)
1235 if (type->subtype->byte_size != 0)
1237 if (type->subtype->name.empty())
1239 // Try to find a more complete description of the type:
1240 // We need to fix in order to support C++.
1243 (mc_type_t) xbt_dict_get_or_null(
1244 info->full_types_by_name, type->subtype->name.c_str());
1245 if (subtype != NULL) {
1246 type->subtype = subtype;
1251 static void MC_post_process_types(mc_object_info_t info)
1253 xbt_dict_cursor_t cursor = NULL;
1257 // Lookup "subtype" field:
1258 xbt_dict_foreach(info->types, cursor, origin, type) {
1259 MC_resolve_subtype(info, type);
1263 if (type->members != NULL)
1264 xbt_dynar_foreach(type->members, i, member) {
1265 MC_resolve_subtype(info, member);
1270 /** \brief Finds informations about a given shared object/executable */
1271 std::shared_ptr<s_mc_object_info_t> MC_find_object_info(
1272 std::vector<simgrid::mc::VmMap> const& maps, const char *name, int executable)
1274 std::shared_ptr<s_mc_object_info_t> result =
1275 std::make_shared<s_mc_object_info_t>();
1277 result->flags |= MC_OBJECT_INFO_EXECUTABLE;
1278 result->file_name = xbt_strdup(name);
1279 MC_find_object_address(maps, result.get());
1280 MC_dwarf_get_variables(result.get());
1281 MC_post_process_types(result.get());
1282 MC_post_process_variables(result.get());
1283 MC_post_process_functions(result.get());
1284 MC_make_functions_index(result.get());
1285 return std::move(result);
1288 /*************************************************************************/
1290 static int MC_dwarf_get_variable_index(xbt_dynar_t variables, char *var,
1294 if (xbt_dynar_is_empty(variables))
1297 unsigned int cursor = 0;
1299 int end = xbt_dynar_length(variables) - 1;
1300 dw_variable_t var_test = NULL;
1302 while (start <= end) {
1303 cursor = (start + end) / 2;
1305 (dw_variable_t) xbt_dynar_get_as(variables, cursor, dw_variable_t);
1306 if (strcmp(var_test->name, var) < 0) {
1308 } else if (strcmp(var_test->name, var) > 0) {
1311 if (address) { /* global variable */
1312 if (var_test->address == address)
1314 if (var_test->address > address)
1318 } else { /* local variable */
1324 if (strcmp(var_test->name, var) == 0) {
1325 if (address && var_test->address < address)
1329 } else if (strcmp(var_test->name, var) < 0)
1336 void MC_dwarf_register_global_variable(mc_object_info_t info,
1337 dw_variable_t variable)
1340 MC_dwarf_get_variable_index(info->global_variables, variable->name,
1343 xbt_dynar_insert_at(info->global_variables, index, &variable);
1347 void MC_dwarf_register_non_global_variable(mc_object_info_t info,
1349 dw_variable_t variable)
1351 xbt_assert(frame, "Frame is NULL");
1353 MC_dwarf_get_variable_index(frame->variables, variable->name, NULL);
1355 xbt_dynar_insert_at(frame->variables, index, &variable);
1359 void MC_dwarf_register_variable(mc_object_info_t info, dw_frame_t frame,
1360 dw_variable_t variable)
1362 if (variable->global)
1363 MC_dwarf_register_global_variable(info, variable);
1364 else if (frame == NULL)
1365 xbt_die("No frame for this local variable");
1367 MC_dwarf_register_non_global_variable(info, frame, variable);
1370 void MC_post_process_object_info(mc_process_t process, mc_object_info_t info)
1372 xbt_dict_cursor_t cursor = NULL;
1374 mc_type_t type = NULL;
1375 xbt_dict_foreach(info->types, cursor, key, type) {
1377 mc_type_t subtype = type;
1378 while (subtype->type == DW_TAG_typedef || subtype->type == DW_TAG_volatile_type
1379 || subtype->type == DW_TAG_const_type) {
1380 if (subtype->subtype)
1381 subtype = subtype->subtype;
1386 // Resolve full_type:
1387 if (!subtype->name.empty() && subtype->byte_size == 0) {
1388 for (auto const& object_info : process->object_infos) {
1389 mc_type_t same_type = (mc_type_t)
1390 xbt_dict_get_or_null(object_info->full_types_by_name,
1391 subtype->name.c_str());
1392 if (same_type && !same_type->name.empty() && same_type->byte_size) {
1393 type->full_type = same_type;
1397 } else type->full_type = subtype;