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. */
8 #define DW_LANG_Objc DW_LANG_ObjC /* fix spelling error in older dwarf.h */
10 #include <elfutils/libdw.h>
13 #include <simgrid_config.h>
15 #include <xbt/sysdep.h>
17 #include "mc_private.h"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing");
21 /** \brief The default DW_TAG_lower_bound for a given DW_AT_language.
23 * The default for a given language is defined in the DWARF spec.
25 * \param language consant as defined by the DWARf spec
27 static uint64_t MC_dwarf_default_lower_bound(int lang);
29 /** \brief Computes the the element_count of a DW_TAG_enumeration_type DIE
31 * This is the number of elements in a given array dimension.
33 * A reference of the compilation unit (DW_TAG_compile_unit) is
34 * needed because the default lower bound (when there is no DW_AT_lower_bound)
35 * depends of the language of the compilation unit (DW_AT_language).
37 * \param die DIE for the DW_TAG_enumeration_type or DW_TAG_subrange_type
38 * \param unit DIE of the DW_TAG_compile_unit
40 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
43 /** \brief Computes the number of elements of a given DW_TAG_array_type.
45 * \param die DIE for the DW_TAG_array_type
47 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit);
49 /** \brief Process a DIE
51 * \param info the resulting object fot the library/binary file (output)
52 * \param die the current DIE
53 * \param unit the DIE of the compile unit of the current DIE
54 * \param frame containg frame if any
56 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
57 Dwarf_Die * unit, dw_frame_t frame,
58 const char *namespace);
60 /** \brief Process a type DIE
62 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
63 Dwarf_Die * unit, dw_frame_t frame,
64 const char *namespace);
66 /** \brief Calls MC_dwarf_handle_die on all childrend of the given 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_children(mc_object_info_t info, Dwarf_Die * die,
74 Dwarf_Die * unit, dw_frame_t frame,
75 const char *namespace);
77 /** \brief Handle a variable (DW_TAG_variable or other)
79 * \param info the resulting object fot the library/binary file (output)
80 * \param die the current DIE
81 * \param unit the DIE of the compile unit of the current DIE
82 * \param frame containg frame if any
84 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
85 Dwarf_Die * unit, dw_frame_t frame,
86 const char *namespace);
88 /** \brief Get the DW_TAG_type of the DIE
91 * \return DW_TAG_type attribute as a new string (NULL if none)
93 static char *MC_dwarf_at_type(Dwarf_Die * die);
95 /** \brief Get the name of an attribute (DW_AT_*) from its code
97 * \param attr attribute code (see the DWARF specification)
98 * \return name of the attribute
100 const char *MC_dwarf_attrname(int attr)
103 #include "mc_dwarf_attrnames.h"
105 return "DW_AT_unknown";
109 /** \brief Get the name of a dwarf tag (DW_TAG_*) from its code
111 * \param tag tag code (see the DWARF specification)
112 * \return name of the tag
114 const char *MC_dwarf_tagname(int tag)
117 #include "mc_dwarf_tagnames.h"
119 return "DW_TAG_invalid";
121 return "DW_TAG_unknown";
125 /** \brief A class of DWARF tags (DW_TAG_*)
127 typedef enum mc_tag_class {
136 static mc_tag_class MC_dwarf_tag_classify(int tag)
140 case DW_TAG_array_type:
141 case DW_TAG_class_type:
142 case DW_TAG_enumeration_type:
144 case DW_TAG_pointer_type:
145 case DW_TAG_reference_type:
146 case DW_TAG_rvalue_reference_type:
147 case DW_TAG_string_type:
148 case DW_TAG_structure_type:
149 case DW_TAG_subroutine_type:
150 case DW_TAG_union_type:
151 case DW_TAG_ptr_to_member_type:
152 case DW_TAG_set_type:
153 case DW_TAG_subrange_type:
154 case DW_TAG_base_type:
155 case DW_TAG_const_type:
156 case DW_TAG_file_type:
157 case DW_TAG_packed_type:
158 case DW_TAG_volatile_type:
159 case DW_TAG_restrict_type:
160 case DW_TAG_interface_type:
161 case DW_TAG_unspecified_type:
162 case DW_TAG_shared_type:
165 case DW_TAG_subprogram:
166 return mc_tag_subprogram;
168 case DW_TAG_variable:
169 case DW_TAG_formal_parameter:
170 return mc_tag_variable;
172 case DW_TAG_lexical_block:
173 case DW_TAG_try_block:
174 case DW_TAG_catch_block:
175 case DW_TAG_inlined_subroutine:
176 case DW_TAG_with_stmt:
179 case DW_TAG_namespace:
180 return mc_tag_namespace;
183 return mc_tag_unknown;
188 #define MC_DW_CLASS_UNKNOWN 0
189 #define MC_DW_CLASS_ADDRESS 1 // Location in the address space of the program
190 #define MC_DW_CLASS_BLOCK 2 // Arbitrary block of bytes
191 #define MC_DW_CLASS_CONSTANT 3
192 #define MC_DW_CLASS_STRING 3 // String
193 #define MC_DW_CLASS_FLAG 4 // Boolean
194 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
195 #define MC_DW_CLASS_EXPRLOC 6 // DWARF expression/location description
196 #define MC_DW_CLASS_LINEPTR 7
197 #define MC_DW_CLASS_LOCLISTPTR 8
198 #define MC_DW_CLASS_MACPTR 9
199 #define MC_DW_CLASS_RANGELISTPTR 10
201 /** \brief Find the DWARF data class for a given DWARF data form
203 * This mapping is defined in the DWARF spec.
205 * \param form The form (values taken from the DWARF spec)
206 * \return An internal representation for the corresponding class
208 static int MC_dwarf_form_get_class(int form)
212 return MC_DW_CLASS_ADDRESS;
217 return MC_DW_CLASS_BLOCK;
224 return MC_DW_CLASS_CONSTANT;
227 return MC_DW_CLASS_STRING;
228 case DW_FORM_ref_addr:
233 case DW_FORM_ref_udata:
234 return MC_DW_CLASS_REFERENCE;
236 case DW_FORM_flag_present:
237 return MC_DW_CLASS_FLAG;
238 case DW_FORM_exprloc:
239 return MC_DW_CLASS_EXPRLOC;
243 return MC_DW_CLASS_UNKNOWN;
247 /** \brief Get the name of the tag of a given DIE
250 * \return name of the tag of this DIE
252 static inline const char *MC_dwarf_die_tagname(Dwarf_Die * die)
254 return MC_dwarf_tagname(dwarf_tag(die));
259 /** \brief Get an attribute of a given DIE as a string
262 * \param attribute attribute
263 * \return value of the given attribute of the given DIE
265 static const char *MC_dwarf_attr_integrate_string(Dwarf_Die * die,
268 Dwarf_Attribute attr;
269 if (!dwarf_attr_integrate(die, attribute, &attr)) {
272 return dwarf_formstring(&attr);
276 /** \brief Get the linkage name of a DIE.
278 * Use either DW_AT_linkage_name or DW_AT_MIPS_linkage_name.
279 * DW_AT_linkage_name is standardized since DWARF 4.
280 * Before this version of DWARF, the MIPS extensions
281 * DW_AT_MIPS_linkage_name is used (at least by GCC).
284 * \return linkage name of the given DIE (or NULL)
286 static const char *MC_dwarf_at_linkage_name(Dwarf_Die * die)
288 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_linkage_name);
290 name = MC_dwarf_attr_integrate_string(die, DW_AT_MIPS_linkage_name);
294 static Dwarf_Off MC_dwarf_attr_dieoffset(Dwarf_Die * die, int attribute)
296 Dwarf_Attribute attr;
297 if (dwarf_hasattr_integrate(die, attribute)) {
298 dwarf_attr_integrate(die, attribute, &attr);
299 Dwarf_Die subtype_die;
300 if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
301 xbt_die("Could not find DIE");
303 return dwarf_dieoffset(&subtype_die);
308 static Dwarf_Off MC_dwarf_attr_integrate_dieoffset(Dwarf_Die * die,
311 Dwarf_Attribute attr;
312 if (dwarf_hasattr_integrate(die, attribute)) {
313 dwarf_attr_integrate(die, DW_AT_type, &attr);
314 Dwarf_Die subtype_die;
315 if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
316 xbt_die("Could not find DIE");
318 return dwarf_dieoffset(&subtype_die);
323 /** \brief Find the type/subtype (DW_AT_type) for a DIE
326 * \return DW_AT_type reference as a global offset in hexadecimal (or NULL)
328 static char *MC_dwarf_at_type(Dwarf_Die * die)
330 Dwarf_Off offset = MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
331 return offset == 0 ? NULL : bprintf("%" PRIx64, offset);
334 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die * die, int attribute)
336 Dwarf_Attribute attr;
337 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
340 if (dwarf_formaddr(&attr, &value) == 0)
341 return (uint64_t) value;
346 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die * die, int attribute,
347 uint64_t default_value)
349 Dwarf_Attribute attr;
350 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
351 return default_value;
353 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr),
354 &value) == 0 ? (uint64_t) value : default_value;
357 static bool MC_dwarf_attr_flag(Dwarf_Die * die, int attribute, bool integrate)
359 Dwarf_Attribute attr;
360 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
361 : dwarf_attr(die, attribute, &attr)) == 0)
365 if (dwarf_formflag(&attr, &result))
366 xbt_die("Unexpected form for attribute %s", MC_dwarf_attrname(attribute));
370 /** \brief Find the default lower bound for a given language
372 * The default lower bound of an array (when DW_TAG_lower_bound
373 * is missing) depends on the language of the compilation unit.
375 * \param lang Language of the compilation unit (values defined in the DWARF spec)
376 * \return Default lower bound of an array in this compilation unit
378 static uint64_t MC_dwarf_default_lower_bound(int lang)
384 case DW_LANG_C_plus_plus:
388 case DW_LANG_ObjC_plus_plus:
394 case DW_LANG_Fortran77:
395 case DW_LANG_Fortran90:
396 case DW_LANG_Fortran95:
397 case DW_LANG_Modula2:
398 case DW_LANG_Pascal83:
400 case DW_LANG_Cobol74:
401 case DW_LANG_Cobol85:
404 xbt_die("No default DW_TAG_lower_bound for language %i and none given",
410 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
413 * \param unit DIE of the compilation unit
414 * \return number of elements in the range
416 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
419 xbt_assert(dwarf_tag(die) == DW_TAG_enumeration_type
420 || dwarf_tag(die) == DW_TAG_subrange_type,
421 "MC_dwarf_subrange_element_count called with DIE of type %s",
422 MC_dwarf_die_tagname(die));
424 // Use DW_TAG_count if present:
425 if (dwarf_hasattr_integrate(die, DW_AT_count)) {
426 return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
428 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
430 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
431 // This is not really 0, but the code expects this (we do not know):
434 uint64_t upper_bound =
435 MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
437 uint64_t lower_bound = 0;
438 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
439 lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
441 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
443 return upper_bound - lower_bound + 1;
446 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
448 * The compilation unit might be needed because the default lower
449 * bound depends on the language of the compilation unit.
451 * \param die the DIE of the DW_TAG_array_type
452 * \param unit the DIE of the compilation unit
453 * \return number of elements in this array type
455 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit)
457 xbt_assert(dwarf_tag(die) == DW_TAG_array_type,
458 "MC_dwarf_array_element_count called with DIE of type %s",
459 MC_dwarf_die_tagname(die));
464 for (res = dwarf_child(die, &child); res == 0;
465 res = dwarf_siblingof(&child, &child)) {
466 int child_tag = dwarf_tag(&child);
467 if (child_tag == DW_TAG_subrange_type
468 || child_tag == DW_TAG_enumeration_type) {
469 result *= MC_dwarf_subrange_element_count(&child, unit);
477 /** \brief Initialize the location of a member of a type
478 * (DW_AT_data_member_location of a DW_TAG_member).
480 * \param type a type (struct, class)
481 * \param member the member of the type
482 * \param child DIE of the member (DW_TAG_member)
484 static void MC_dwarf_fill_member_location(dw_type_t type, dw_type_t member,
487 if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
488 xbt_die("Can't groke DW_AT_data_bit_offset.");
491 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
492 if (type->type != DW_TAG_union_type) {
494 ("Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%"
495 PRIx64 ">%s", member->name, (uint64_t) type->id, type->name);
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);
518 if (len == 1 && expr[0].atom == DW_OP_plus_uconst) {
519 member->offset = expr[0].number;
521 mc_dwarf_expression_init(&member->location, len, expr);
525 case MC_DW_CLASS_CONSTANT:
526 // Offset from the base address of the object:
529 if (!dwarf_formudata(&attr, &offset))
530 member->offset = offset;
532 xbt_die("Cannot get %s location <%" PRIx64 ">%s",
533 MC_dwarf_attr_integrate_string(child, DW_AT_name),
534 (uint64_t) type->id, type->name);
537 case MC_DW_CLASS_LOCLISTPTR:
538 // Reference to a location list:
540 case MC_DW_CLASS_REFERENCE:
541 // It's supposed to be possible in DWARF2 but I couldn't find its semantic
544 xbt_die("Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
550 static void dw_type_free_voidp(void *t)
552 dw_type_free((dw_type_t) * (void **) t);
555 /** \brief Populate the list of members of a type
557 * \param info ELF object containing the type DIE
558 * \param die DIE of the type
559 * \param unit DIE of the compilation unit containing the type DIE
560 * \param type the type
562 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die * die,
563 Dwarf_Die * unit, dw_type_t type)
567 xbt_assert(!type->members);
569 xbt_dynar_new(sizeof(dw_type_t), (void (*)(void *)) dw_type_free_voidp);
570 for (res = dwarf_child(die, &child); res == 0;
571 res = dwarf_siblingof(&child, &child)) {
572 int tag = dwarf_tag(&child);
573 if (tag == DW_TAG_member || tag == DW_TAG_inheritance) {
575 // Skip declarations:
576 if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
579 // Skip compile time constants:
580 if (dwarf_hasattr(&child, DW_AT_const_value))
583 // TODO, we should use another type (because is is not a type but a member)
584 dw_type_t member = xbt_new0(s_dw_type_t, 1);
588 member->id = dwarf_dieoffset(&child);
590 const char *name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
592 member->name = xbt_strdup(name);
597 MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
598 member->element_count = -1;
599 member->dw_type_id = MC_dwarf_at_type(&child);
600 member->members = NULL;
601 member->is_pointer_type = 0;
604 if (dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
605 xbt_die("Can't groke DW_AT_data_bit_offset.");
608 MC_dwarf_fill_member_location(type, member, &child);
610 if (!member->dw_type_id) {
611 xbt_die("Missing type for member %s of <%" PRIx64 ">%s", member->name,
612 (uint64_t) type->id, type->name);
615 xbt_dynar_push(type->members, &member);
620 /** \brief Create a MC type object from a DIE
622 * \param info current object info object
623 * \param DIE (for a given type);
624 * \param unit compilation unit of the current DIE
625 * \return MC representation of the type
627 static dw_type_t MC_dwarf_die_to_type(mc_object_info_t info, Dwarf_Die * die,
628 Dwarf_Die * unit, dw_frame_t frame,
629 const char *namespace)
632 dw_type_t type = xbt_new0(s_dw_type_t, 1);
637 type->element_count = -1;
638 type->dw_type_id = NULL;
639 type->members = NULL;
640 type->is_pointer_type = 0;
643 type->type = dwarf_tag(die);
646 type->id = dwarf_dieoffset(die);
648 const char *prefix = "";
649 switch (type->type) {
650 case DW_TAG_structure_type:
653 case DW_TAG_union_type:
656 case DW_TAG_class_type:
663 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
666 namespace ? bprintf("%s%s::%s", prefix, namespace,
667 name) : bprintf("%s%s", prefix, name);
670 type->dw_type_id = MC_dwarf_at_type(die);
672 // Computation of the byte_size;
673 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
674 type->byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
675 else if (type->type == DW_TAG_array_type
676 || type->type == DW_TAG_structure_type
677 || type->type == DW_TAG_class_type) {
679 if (dwarf_aggregate_size(die, &size) == 0) {
680 type->byte_size = size;
684 switch (type->type) {
685 case DW_TAG_array_type:
686 type->element_count = MC_dwarf_array_element_count(die, unit);
687 // TODO, handle DW_byte_stride and (not) DW_bit_stride
690 case DW_TAG_pointer_type:
691 case DW_TAG_reference_type:
692 case DW_TAG_rvalue_reference_type:
693 type->is_pointer_type = 1;
696 case DW_TAG_structure_type:
697 case DW_TAG_union_type:
698 case DW_TAG_class_type:
699 MC_dwarf_add_members(info, die, unit, type);
700 char *new_namespace = namespace == NULL ? xbt_strdup(type->name)
701 : bprintf("%s::%s", namespace, name);
702 MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
710 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
711 Dwarf_Die * unit, dw_frame_t frame,
712 const char *namespace)
714 dw_type_t type = MC_dwarf_die_to_type(info, die, unit, frame, namespace);
716 char *key = bprintf("%" PRIx64, (uint64_t) type->id);
717 xbt_dict_set(info->types, key, type, NULL);
720 if (type->name && type->byte_size != 0) {
721 xbt_dict_set(info->full_types_by_name, type->name, type, NULL);
725 static int mc_anonymous_variable_index = 0;
727 static dw_variable_t MC_die_to_variable(mc_object_info_t info, Dwarf_Die * die,
728 Dwarf_Die * unit, dw_frame_t frame,
729 const char *namespace)
731 // Skip declarations:
732 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
735 // Skip compile time constants:
736 if (dwarf_hasattr(die, DW_AT_const_value))
739 Dwarf_Attribute attr_location;
740 if (dwarf_attr(die, DW_AT_location, &attr_location) == NULL) {
741 // No location: do not add it ?
745 dw_variable_t variable = xbt_new0(s_dw_variable_t, 1);
746 variable->dwarf_offset = dwarf_dieoffset(die);
747 variable->global = frame == NULL; // Can be override base on DW_AT_location
748 variable->object_info = info;
750 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
751 variable->name = xbt_strdup(name);
753 variable->type_origin = MC_dwarf_at_type(die);
755 int form = dwarf_whatform(&attr_location);
758 DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
760 case MC_DW_CLASS_EXPRLOC:
761 case MC_DW_CLASS_BLOCK:
762 // Location expression:
766 if (dwarf_getlocation(&attr_location, &expr, &len)) {
768 ("Could not read location expression in DW_AT_location of variable <%"
769 PRIx64 ">%s", (uint64_t) variable->dwarf_offset, variable->name);
772 if (len == 1 && expr[0].atom == DW_OP_addr) {
773 variable->global = 1;
774 uintptr_t offset = (uintptr_t) expr[0].number;
775 uintptr_t base = (uintptr_t) MC_object_base_address(info);
776 variable->address = (void *) (base + offset);
778 mc_dwarf_location_list_init_from_expression(&variable->locations, len,
784 case MC_DW_CLASS_LOCLISTPTR:
785 case MC_DW_CLASS_CONSTANT:
786 // Reference to location list:
787 mc_dwarf_location_list_init(&variable->locations, info, die,
791 xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location in <%"
792 PRIx64 ">%s", form, form, klass, klass,
793 (uint64_t) variable->dwarf_offset, variable->name);
796 // Handle start_scope:
797 if (dwarf_hasattr(die, DW_AT_start_scope)) {
798 Dwarf_Attribute attr;
799 dwarf_attr(die, DW_AT_start_scope, &attr);
800 int form = dwarf_whatform(&attr);
801 int klass = MC_dwarf_form_get_class(form);
803 case MC_DW_CLASS_CONSTANT:
806 variable->start_scope =
807 dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
810 case MC_DW_CLASS_RANGELISTPTR: // TODO
813 ("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
814 form, klass, name == NULL ? "?" : name);
818 if (namespace && variable->global) {
819 char *old_name = variable->name;
820 variable->name = bprintf("%s::%s", namespace, old_name);
823 // The current code needs a variable name,
824 // generate a fake one:
825 if (!variable->name) {
826 variable->name = bprintf("@anonymous#%i", mc_anonymous_variable_index++);
832 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
833 Dwarf_Die * unit, dw_frame_t frame,
834 const char *namespace)
836 dw_variable_t variable =
837 MC_die_to_variable(info, die, unit, frame, namespace);
838 if (variable == NULL)
840 MC_dwarf_register_variable(info, frame, variable);
843 static void mc_frame_free_voipd(dw_frame_t * p)
849 static void MC_dwarf_handle_scope_die(mc_object_info_t info, Dwarf_Die * die,
850 Dwarf_Die * unit, dw_frame_t parent_frame,
851 const char *namespace)
853 // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
854 int tag = dwarf_tag(die);
855 mc_tag_class klass = MC_dwarf_tag_classify(tag);
857 // (Template) Subprogram declaration:
858 if (klass == mc_tag_subprogram
859 && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
862 if (klass == mc_tag_scope)
863 xbt_assert(parent_frame, "No parent scope for this scope");
865 dw_frame_t frame = xbt_new0(s_dw_frame_t, 1);
868 frame->id = dwarf_dieoffset(die);
869 frame->object_info = info;
871 if (klass == mc_tag_subprogram) {
872 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
874 namespace ? bprintf("%s::%s", namespace, name) : xbt_strdup(name);
877 frame->abstract_origin_id =
878 MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
880 // This is the base address for DWARF addresses.
881 // Relocated addresses are offset from this base address.
882 // See DWARF4 spec 7.5
883 void *base = MC_object_base_address(info);
885 // Variables are filled in the (recursive) call of MC_dwarf_handle_children:
887 xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
889 // TODO, support DW_AT_ranges
890 uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
891 frame->low_pc = low_pc ? ((char *) base) + low_pc : 0;
894 Dwarf_Attribute attr;
895 if (!dwarf_attr_integrate(die, DW_AT_high_pc, &attr)) {
896 xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
902 switch (MC_dwarf_form_get_class(dwarf_whatform(&attr))) {
904 // DW_AT_high_pc if an offset from the low_pc:
905 case MC_DW_CLASS_CONSTANT:
907 if (dwarf_formsdata(&attr, &offset) != 0)
908 xbt_die("Could not read constant");
909 frame->high_pc = (void *) ((char *) frame->low_pc + offset);
912 // DW_AT_high_pc is a relocatable address:
913 case MC_DW_CLASS_ADDRESS:
914 if (dwarf_formaddr(&attr, &high_pc) != 0)
915 xbt_die("Could not read address");
916 frame->high_pc = ((char *) base) + high_pc;
920 xbt_die("Unexpected class for DW_AT_high_pc");
925 if (klass == mc_tag_subprogram) {
926 Dwarf_Attribute attr_frame_base;
927 if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
928 mc_dwarf_location_list_init(&frame->frame_base, info, die,
933 xbt_dynar_new(sizeof(dw_frame_t), (void_f_pvoid_t) mc_frame_free_voipd);
936 if (klass == mc_tag_subprogram) {
937 char *key = bprintf("%" PRIx64, (uint64_t) frame->id);
938 xbt_dict_set(info->subprograms, key, frame, NULL);
940 } else if (klass == mc_tag_scope) {
941 xbt_dynar_push(parent_frame->scopes, &frame);
944 MC_dwarf_handle_children(info, die, unit, frame, namespace);
947 static void mc_dwarf_handle_namespace_die(mc_object_info_t info,
948 Dwarf_Die * die, Dwarf_Die * unit,
950 const char *namespace)
952 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
954 xbt_die("Unexpected namespace in a subprogram");
955 char *new_namespace = namespace == NULL ? xbt_strdup(name)
956 : bprintf("%s::%s", namespace, name);
957 MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
958 xbt_free(new_namespace);
961 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
962 Dwarf_Die * unit, dw_frame_t frame,
963 const char *namespace)
965 // For each child DIE:
968 for (res = dwarf_child(die, &child); res == 0;
969 res = dwarf_siblingof(&child, &child)) {
970 MC_dwarf_handle_die(info, &child, unit, frame, namespace);
974 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
975 Dwarf_Die * unit, dw_frame_t frame,
976 const char *namespace)
978 int tag = dwarf_tag(die);
979 mc_tag_class klass = MC_dwarf_tag_classify(tag);
984 MC_dwarf_handle_type_die(info, die, unit, frame, namespace);
987 // Subprogram or scope:
988 case mc_tag_subprogram:
990 MC_dwarf_handle_scope_die(info, die, unit, frame, namespace);
994 case mc_tag_variable:
995 MC_dwarf_handle_variable_die(info, die, unit, frame, namespace);
998 case mc_tag_namespace:
999 mc_dwarf_handle_namespace_die(info, die, unit, frame, namespace);
1008 /** \brief Populate the debugging informations of the given ELF object
1010 * Read the DWARf information of the EFFL object and populate the
1011 * lists of types, variables, functions.
1013 void MC_dwarf_get_variables(mc_object_info_t info)
1015 int fd = open(info->file_name, O_RDONLY);
1017 xbt_die("Could not open file %s", info->file_name);
1019 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
1020 if (dwarf == NULL) {
1021 xbt_die("Your program must be compiled with -g");
1023 // For each compilation unit:
1024 Dwarf_Off offset = 0;
1025 Dwarf_Off next_offset = 0;
1027 while (dwarf_nextcu(dwarf, offset, &next_offset, &length, NULL, NULL, NULL) ==
1030 if (dwarf_offdie(dwarf, offset + length, &unit_die) != NULL) {
1032 // For each child DIE:
1035 for (res = dwarf_child(&unit_die, &child); res == 0;
1036 res = dwarf_siblingof(&child, &child)) {
1037 MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
1041 offset = next_offset;
1048 /************************** Free functions *************************/
1050 void mc_frame_free(dw_frame_t frame)
1052 xbt_free(frame->name);
1053 mc_dwarf_location_list_clear(&(frame->frame_base));
1054 xbt_dynar_free(&(frame->variables));
1055 xbt_dynar_free(&(frame->scopes));
1059 void dw_type_free(dw_type_t t)
1062 xbt_free(t->dw_type_id);
1063 xbt_dynar_free(&(t->members));
1064 mc_dwarf_expression_clear(&t->location);
1068 void dw_variable_free(dw_variable_t v)
1072 xbt_free(v->type_origin);
1074 if (v->locations.locations)
1075 mc_dwarf_location_list_clear(&v->locations);
1080 void dw_variable_free_voidp(void *t)
1082 dw_variable_free((dw_variable_t) * (void **) t);
1085 // ***** object_info
1089 mc_object_info_t MC_new_object_info(void)
1091 mc_object_info_t res = xbt_new0(s_mc_object_info_t, 1);
1092 res->subprograms = xbt_dict_new_homogeneous((void (*)(void *)) mc_frame_free);
1093 res->global_variables =
1094 xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
1095 res->types = xbt_dict_new_homogeneous((void (*)(void *)) dw_type_free);
1096 res->full_types_by_name = xbt_dict_new_homogeneous(NULL);
1100 void MC_free_object_info(mc_object_info_t * info)
1102 xbt_free(&(*info)->file_name);
1103 xbt_dict_free(&(*info)->subprograms);
1104 xbt_dynar_free(&(*info)->global_variables);
1105 xbt_dict_free(&(*info)->types);
1106 xbt_dict_free(&(*info)->full_types_by_name);
1108 xbt_dynar_free(&(*info)->functions_index);
1114 void *MC_object_base_address(mc_object_info_t info)
1116 if (info->flags & MC_OBJECT_INFO_EXECUTABLE)
1118 void *result = info->start_exec;
1119 if (info->start_rw != NULL && result > (void *) info->start_rw)
1120 result = info->start_rw;
1121 if (info->start_ro != NULL && result > (void *) info->start_ro)
1122 result = info->start_ro;
1126 // ***** Functions index
1128 static int MC_compare_frame_index_items(mc_function_index_item_t a,
1129 mc_function_index_item_t b)
1131 if (a->low_pc < b->low_pc)
1133 else if (a->low_pc == b->low_pc)
1139 static void MC_make_functions_index(mc_object_info_t info)
1141 xbt_dynar_t index = xbt_dynar_new(sizeof(s_mc_function_index_item_t), NULL);
1143 // Populate the array:
1144 dw_frame_t frame = NULL;
1145 xbt_dict_cursor_t cursor;
1147 xbt_dict_foreach(info->subprograms, cursor, key, frame) {
1148 if (frame->low_pc == NULL)
1150 s_mc_function_index_item_t entry;
1151 entry.low_pc = frame->low_pc;
1152 entry.high_pc = frame->high_pc;
1153 entry.function = frame;
1154 xbt_dynar_push(index, &entry);
1157 mc_function_index_item_t base =
1158 (mc_function_index_item_t) xbt_dynar_get_ptr(index, 0);
1160 // Sort the array by low_pc:
1162 xbt_dynar_length(index),
1163 sizeof(s_mc_function_index_item_t),
1164 (int (*)(const void *, const void *)) MC_compare_frame_index_items);
1166 info->functions_index = index;
1169 mc_object_info_t MC_ip_find_object_info(void *ip)
1172 for (i = 0; i != mc_object_infos_size; ++i) {
1173 if (ip >= (void *) mc_object_infos[i]->start_exec
1174 && ip <= (void *) mc_object_infos[i]->end_exec) {
1175 return mc_object_infos[i];
1181 static dw_frame_t MC_find_function_by_ip_and_object(void *ip,
1182 mc_object_info_t info)
1184 xbt_dynar_t dynar = info->functions_index;
1185 mc_function_index_item_t base =
1186 (mc_function_index_item_t) xbt_dynar_get_ptr(dynar, 0);
1188 int j = xbt_dynar_length(dynar) - 1;
1190 int k = i + ((j - i) / 2);
1191 if (ip < base[k].low_pc) {
1193 } else if (ip >= base[k].high_pc) {
1196 return base[k].function;
1202 dw_frame_t MC_find_function_by_ip(void *ip)
1204 mc_object_info_t info = MC_ip_find_object_info(ip);
1208 return MC_find_function_by_ip_and_object(ip, info);
1211 static void MC_post_process_variables(mc_object_info_t info)
1213 unsigned cursor = 0;
1214 dw_variable_t variable = NULL;
1215 xbt_dynar_foreach(info->global_variables, cursor, variable) {
1216 if (variable->type_origin) {
1217 variable->type = xbt_dict_get_or_null(info->types, variable->type_origin);
1222 static void mc_post_process_scope(mc_object_info_t info, dw_frame_t scope)
1225 if (scope->tag == DW_TAG_inlined_subroutine) {
1227 // Attach correct namespaced name in inlined subroutine:
1228 char *key = bprintf("%" PRIx64, (uint64_t) scope->abstract_origin_id);
1229 dw_frame_t abstract_origin = xbt_dict_get_or_null(info->subprograms, key);
1230 xbt_assert(abstract_origin, "Could not lookup abstract origin %s", key);
1232 scope->name = xbt_strdup(abstract_origin->name);
1236 unsigned cursor = 0;
1237 dw_variable_t variable = NULL;
1238 xbt_dynar_foreach(scope->variables, cursor, variable) {
1239 if (variable->type_origin) {
1240 variable->type = xbt_dict_get_or_null(info->types, variable->type_origin);
1244 // Recursive post-processing of nested-scopes:
1245 dw_frame_t nested_scope = NULL;
1246 xbt_dynar_foreach(scope->scopes, cursor, nested_scope)
1247 mc_post_process_scope(info, nested_scope);
1251 static void MC_post_process_functions(mc_object_info_t info)
1253 xbt_dict_cursor_t cursor;
1255 dw_frame_t subprogram = NULL;
1256 xbt_dict_foreach(info->subprograms, cursor, key, subprogram) {
1257 mc_post_process_scope(info, subprogram);
1262 /** \brief Fill/lookup the "subtype" field.
1264 static void MC_resolve_subtype(mc_object_info_t info, dw_type_t type)
1267 if (type->dw_type_id == NULL)
1269 type->subtype = xbt_dict_get_or_null(info->types, type->dw_type_id);
1270 if (type->subtype == NULL)
1272 if (type->subtype->byte_size != 0)
1274 if (type->subtype->name == NULL)
1276 // Try to find a more complete description of the type:
1277 // We need to fix in order to support C++.
1280 xbt_dict_get_or_null(info->full_types_by_name, type->subtype->name);
1281 if (subtype != NULL) {
1282 type->subtype = subtype;
1287 static void MC_post_process_types(mc_object_info_t info)
1289 xbt_dict_cursor_t cursor = NULL;
1293 // Lookup "subtype" field:
1294 xbt_dict_foreach(info->types, cursor, origin, type) {
1295 MC_resolve_subtype(info, type);
1299 if (type->members != NULL)
1300 xbt_dynar_foreach(type->members, i, member) {
1301 MC_resolve_subtype(info, member);
1306 /** \brief Finds informations about a given shared object/executable */
1307 mc_object_info_t MC_find_object_info(memory_map_t maps, char *name,
1310 mc_object_info_t result = MC_new_object_info();
1312 result->flags |= MC_OBJECT_INFO_EXECUTABLE;
1313 result->file_name = xbt_strdup(name);
1314 MC_find_object_address(maps, result);
1315 MC_dwarf_get_variables(result);
1316 MC_post_process_types(result);
1317 MC_post_process_variables(result);
1318 MC_post_process_functions(result);
1319 MC_make_functions_index(result);
1323 /*************************************************************************/
1325 static int MC_dwarf_get_variable_index(xbt_dynar_t variables, char *var,
1329 if (xbt_dynar_is_empty(variables))
1332 unsigned int cursor = 0;
1334 int end = xbt_dynar_length(variables) - 1;
1335 dw_variable_t var_test = NULL;
1337 while (start <= end) {
1338 cursor = (start + end) / 2;
1340 (dw_variable_t) xbt_dynar_get_as(variables, cursor, dw_variable_t);
1341 if (strcmp(var_test->name, var) < 0) {
1343 } else if (strcmp(var_test->name, var) > 0) {
1346 if (address) { /* global variable */
1347 if (var_test->address == address)
1349 if (var_test->address > address)
1353 } else { /* local variable */
1359 if (strcmp(var_test->name, var) == 0) {
1360 if (address && var_test->address < address)
1364 } else if (strcmp(var_test->name, var) < 0)
1371 void MC_dwarf_register_global_variable(mc_object_info_t info,
1372 dw_variable_t variable)
1375 MC_dwarf_get_variable_index(info->global_variables, variable->name,
1378 xbt_dynar_insert_at(info->global_variables, index, &variable);
1382 void MC_dwarf_register_non_global_variable(mc_object_info_t info,
1384 dw_variable_t variable)
1386 xbt_assert(frame, "Frame is NULL");
1388 MC_dwarf_get_variable_index(frame->variables, variable->name, NULL);
1390 xbt_dynar_insert_at(frame->variables, index, &variable);
1394 void MC_dwarf_register_variable(mc_object_info_t info, dw_frame_t frame,
1395 dw_variable_t variable)
1397 if (variable->global)
1398 MC_dwarf_register_global_variable(info, variable);
1399 else if (frame == NULL)
1400 xbt_die("No frame for this local variable");
1402 MC_dwarf_register_non_global_variable(info, frame, variable);
1405 void MC_post_process_object_info(mc_object_info_t info)
1407 xbt_dict_cursor_t cursor = NULL;
1409 dw_type_t type = NULL;
1410 xbt_dict_foreach(info->types, cursor, key, type) {
1412 // Resolve full_type:
1413 if (type->name && type->byte_size == 0) {
1414 for (size_t i = 0; i != mc_object_infos_size; ++i) {
1415 dw_type_t same_type =
1416 xbt_dict_get_or_null(mc_object_infos[i]->full_types_by_name,
1418 if (same_type && same_type->name && same_type->byte_size) {
1419 type->full_type = same_type;