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_mutable_type:
163 case DW_TAG_shared_type:
166 case DW_TAG_subprogram:
167 return mc_tag_subprogram;
169 case DW_TAG_variable:
170 case DW_TAG_formal_parameter:
171 return mc_tag_variable;
173 case DW_TAG_lexical_block:
174 case DW_TAG_try_block:
175 case DW_TAG_catch_block:
176 case DW_TAG_inlined_subroutine:
177 case DW_TAG_with_stmt:
180 case DW_TAG_namespace:
181 return mc_tag_namespace;
184 return mc_tag_unknown;
189 #define MC_DW_CLASS_UNKNOWN 0
190 #define MC_DW_CLASS_ADDRESS 1 // Location in the address space of the program
191 #define MC_DW_CLASS_BLOCK 2 // Arbitrary block of bytes
192 #define MC_DW_CLASS_CONSTANT 3
193 #define MC_DW_CLASS_STRING 3 // String
194 #define MC_DW_CLASS_FLAG 4 // Boolean
195 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
196 #define MC_DW_CLASS_EXPRLOC 6 // DWARF expression/location description
197 #define MC_DW_CLASS_LINEPTR 7
198 #define MC_DW_CLASS_LOCLISTPTR 8
199 #define MC_DW_CLASS_MACPTR 9
200 #define MC_DW_CLASS_RANGELISTPTR 10
202 /** \brief Find the DWARF data class for a given DWARF data form
204 * This mapping is defined in the DWARF spec.
206 * \param form The form (values taken from the DWARF spec)
207 * \return An internal representation for the corresponding class
209 static int MC_dwarf_form_get_class(int form)
213 return MC_DW_CLASS_ADDRESS;
218 return MC_DW_CLASS_BLOCK;
225 return MC_DW_CLASS_CONSTANT;
228 return MC_DW_CLASS_STRING;
229 case DW_FORM_ref_addr:
234 case DW_FORM_ref_udata:
235 return MC_DW_CLASS_REFERENCE;
237 case DW_FORM_flag_present:
238 return MC_DW_CLASS_FLAG;
239 case DW_FORM_exprloc:
240 return MC_DW_CLASS_EXPRLOC;
244 return MC_DW_CLASS_UNKNOWN;
248 /** \brief Get the name of the tag of a given DIE
251 * \return name of the tag of this DIE
253 static inline const char *MC_dwarf_die_tagname(Dwarf_Die * die)
255 return MC_dwarf_tagname(dwarf_tag(die));
260 /** \brief Get an attribute of a given DIE as a string
263 * \param attribute attribute
264 * \return value of the given attribute of the given DIE
266 static const char *MC_dwarf_attr_integrate_string(Dwarf_Die * die,
269 Dwarf_Attribute attr;
270 if (!dwarf_attr_integrate(die, attribute, &attr)) {
273 return dwarf_formstring(&attr);
277 /** \brief Get the linkage name of a DIE.
279 * Use either DW_AT_linkage_name or DW_AT_MIPS_linkage_name.
280 * DW_AT_linkage_name is standardized since DWARF 4.
281 * Before this version of DWARF, the MIPS extensions
282 * DW_AT_MIPS_linkage_name is used (at least by GCC).
285 * \return linkage name of the given DIE (or NULL)
287 static const char *MC_dwarf_at_linkage_name(Dwarf_Die * die)
289 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_linkage_name);
291 name = MC_dwarf_attr_integrate_string(die, DW_AT_MIPS_linkage_name);
295 static Dwarf_Off MC_dwarf_attr_dieoffset(Dwarf_Die * die, int attribute)
297 Dwarf_Attribute attr;
298 if (dwarf_hasattr_integrate(die, attribute)) {
299 dwarf_attr_integrate(die, attribute, &attr);
300 Dwarf_Die subtype_die;
301 if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
302 xbt_die("Could not find DIE");
304 return dwarf_dieoffset(&subtype_die);
309 static Dwarf_Off MC_dwarf_attr_integrate_dieoffset(Dwarf_Die * die,
312 Dwarf_Attribute attr;
313 if (dwarf_hasattr_integrate(die, attribute)) {
314 dwarf_attr_integrate(die, DW_AT_type, &attr);
315 Dwarf_Die subtype_die;
316 if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
317 xbt_die("Could not find DIE");
319 return dwarf_dieoffset(&subtype_die);
324 /** \brief Find the type/subtype (DW_AT_type) for a DIE
327 * \return DW_AT_type reference as a global offset in hexadecimal (or NULL)
329 static char *MC_dwarf_at_type(Dwarf_Die * die)
331 Dwarf_Off offset = MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
332 return offset == 0 ? NULL : bprintf("%" PRIx64, offset);
335 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die * die, int attribute)
337 Dwarf_Attribute attr;
338 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
341 if (dwarf_formaddr(&attr, &value) == 0)
342 return (uint64_t) value;
347 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die * die, int attribute,
348 uint64_t default_value)
350 Dwarf_Attribute attr;
351 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
352 return default_value;
354 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr),
355 &value) == 0 ? (uint64_t) value : default_value;
358 static bool MC_dwarf_attr_flag(Dwarf_Die * die, int attribute, bool integrate)
360 Dwarf_Attribute attr;
361 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
362 : dwarf_attr(die, attribute, &attr)) == 0)
366 if (dwarf_formflag(&attr, &result))
367 xbt_die("Unexpected form for attribute %s", MC_dwarf_attrname(attribute));
371 /** \brief Find the default lower bound for a given language
373 * The default lower bound of an array (when DW_TAG_lower_bound
374 * is missing) depends on the language of the compilation unit.
376 * \param lang Language of the compilation unit (values defined in the DWARF spec)
377 * \return Default lower bound of an array in this compilation unit
379 static uint64_t MC_dwarf_default_lower_bound(int lang)
385 case DW_LANG_C_plus_plus:
389 case DW_LANG_ObjC_plus_plus:
395 case DW_LANG_Fortran77:
396 case DW_LANG_Fortran90:
397 case DW_LANG_Fortran95:
398 case DW_LANG_Modula2:
399 case DW_LANG_Pascal83:
401 case DW_LANG_Cobol74:
402 case DW_LANG_Cobol85:
405 xbt_die("No default DW_TAG_lower_bound for language %i and none given",
411 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
414 * \param unit DIE of the compilation unit
415 * \return number of elements in the range
417 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
420 xbt_assert(dwarf_tag(die) == DW_TAG_enumeration_type
421 || dwarf_tag(die) == DW_TAG_subrange_type,
422 "MC_dwarf_subrange_element_count called with DIE of type %s",
423 MC_dwarf_die_tagname(die));
425 // Use DW_TAG_count if present:
426 if (dwarf_hasattr_integrate(die, DW_AT_count)) {
427 return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
429 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
431 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
432 // This is not really 0, but the code expects this (we do not know):
435 uint64_t upper_bound =
436 MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
438 uint64_t lower_bound = 0;
439 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
440 lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
442 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
444 return upper_bound - lower_bound + 1;
447 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
449 * The compilation unit might be needed because the default lower
450 * bound depends on the language of the compilation unit.
452 * \param die the DIE of the DW_TAG_array_type
453 * \param unit the DIE of the compilation unit
454 * \return number of elements in this array type
456 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit)
458 xbt_assert(dwarf_tag(die) == DW_TAG_array_type,
459 "MC_dwarf_array_element_count called with DIE of type %s",
460 MC_dwarf_die_tagname(die));
465 for (res = dwarf_child(die, &child); res == 0;
466 res = dwarf_siblingof(&child, &child)) {
467 int child_tag = dwarf_tag(&child);
468 if (child_tag == DW_TAG_subrange_type
469 || child_tag == DW_TAG_enumeration_type) {
470 result *= MC_dwarf_subrange_element_count(&child, unit);
478 /** \brief Initialize the location of a member of a type
479 * (DW_AT_data_member_location of a DW_TAG_member).
481 * \param type a type (struct, class)
482 * \param member the member of the type
483 * \param child DIE of the member (DW_TAG_member)
485 static void MC_dwarf_fill_member_location(dw_type_t type, dw_type_t member,
488 if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
489 xbt_die("Can't groke DW_AT_data_bit_offset.");
492 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
493 if (type->type != DW_TAG_union_type) {
495 ("Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%"
496 PRIx64 ">%s", member->name, (uint64_t) type->id, type->name);
502 Dwarf_Attribute attr;
503 dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
504 int form = dwarf_whatform(&attr);
505 int klass = MC_dwarf_form_get_class(form);
507 case MC_DW_CLASS_EXPRLOC:
508 case MC_DW_CLASS_BLOCK:
509 // Location expression:
513 if (dwarf_getlocation(&attr, &expr, &len)) {
515 ("Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%"
516 PRIx64 ">%s", MC_dwarf_attr_integrate_string(child, DW_AT_name),
517 (uint64_t) type->id, type->name);
519 if (len == 1 && expr[0].atom == DW_OP_plus_uconst) {
520 member->offset = expr[0].number;
522 mc_dwarf_expression_init(&member->location, len, expr);
526 case MC_DW_CLASS_CONSTANT:
527 // Offset from the base address of the object:
530 if (!dwarf_formudata(&attr, &offset))
531 member->offset = offset;
533 xbt_die("Cannot get %s location <%" PRIx64 ">%s",
534 MC_dwarf_attr_integrate_string(child, DW_AT_name),
535 (uint64_t) type->id, type->name);
538 case MC_DW_CLASS_LOCLISTPTR:
539 // Reference to a location list:
541 case MC_DW_CLASS_REFERENCE:
542 // It's supposed to be possible in DWARF2 but I couldn't find its semantic
545 xbt_die("Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
551 static void dw_type_free_voidp(void *t)
553 dw_type_free((dw_type_t) * (void **) t);
556 /** \brief Populate the list of members of a type
558 * \param info ELF object containing the type DIE
559 * \param die DIE of the type
560 * \param unit DIE of the compilation unit containing the type DIE
561 * \param type the type
563 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die * die,
564 Dwarf_Die * unit, dw_type_t type)
568 xbt_assert(!type->members);
570 xbt_dynar_new(sizeof(dw_type_t), (void (*)(void *)) dw_type_free_voidp);
571 for (res = dwarf_child(die, &child); res == 0;
572 res = dwarf_siblingof(&child, &child)) {
573 int tag = dwarf_tag(&child);
574 if (tag == DW_TAG_member || tag == DW_TAG_inheritance) {
576 // Skip declarations:
577 if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
580 // Skip compile time constants:
581 if (dwarf_hasattr(&child, DW_AT_const_value))
584 // TODO, we should use another type (because is is not a type but a member)
585 dw_type_t member = xbt_new0(s_dw_type_t, 1);
589 member->id = dwarf_dieoffset(&child);
591 const char *name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
593 member->name = xbt_strdup(name);
598 MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
599 member->element_count = -1;
600 member->dw_type_id = MC_dwarf_at_type(&child);
601 member->members = NULL;
602 member->is_pointer_type = 0;
605 if (dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
606 xbt_die("Can't groke DW_AT_data_bit_offset.");
609 MC_dwarf_fill_member_location(type, member, &child);
611 if (!member->dw_type_id) {
612 xbt_die("Missing type for member %s of <%" PRIx64 ">%s", member->name,
613 (uint64_t) type->id, type->name);
616 xbt_dynar_push(type->members, &member);
621 /** \brief Create a MC type object from a DIE
623 * \param info current object info object
624 * \param DIE (for a given type);
625 * \param unit compilation unit of the current DIE
626 * \return MC representation of the type
628 static dw_type_t MC_dwarf_die_to_type(mc_object_info_t info, Dwarf_Die * die,
629 Dwarf_Die * unit, dw_frame_t frame,
630 const char *namespace)
633 dw_type_t type = xbt_new0(s_dw_type_t, 1);
638 type->element_count = -1;
639 type->dw_type_id = NULL;
640 type->members = NULL;
641 type->is_pointer_type = 0;
644 type->type = dwarf_tag(die);
647 type->id = dwarf_dieoffset(die);
649 const char *prefix = "";
650 switch (type->type) {
651 case DW_TAG_structure_type:
654 case DW_TAG_union_type:
657 case DW_TAG_class_type:
664 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
667 namespace ? bprintf("%s%s::%s", prefix, namespace,
668 name) : bprintf("%s%s", prefix, name);
671 type->dw_type_id = MC_dwarf_at_type(die);
673 // Computation of the byte_size;
674 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
675 type->byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
676 else if (type->type == DW_TAG_array_type
677 || type->type == DW_TAG_structure_type
678 || type->type == DW_TAG_class_type) {
680 if (dwarf_aggregate_size(die, &size) == 0) {
681 type->byte_size = size;
685 switch (type->type) {
686 case DW_TAG_array_type:
687 type->element_count = MC_dwarf_array_element_count(die, unit);
688 // TODO, handle DW_byte_stride and (not) DW_bit_stride
691 case DW_TAG_pointer_type:
692 case DW_TAG_reference_type:
693 case DW_TAG_rvalue_reference_type:
694 type->is_pointer_type = 1;
697 case DW_TAG_structure_type:
698 case DW_TAG_union_type:
699 case DW_TAG_class_type:
700 MC_dwarf_add_members(info, die, unit, type);
701 char *new_namespace = namespace == NULL ? xbt_strdup(type->name)
702 : bprintf("%s::%s", namespace, name);
703 MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
711 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
712 Dwarf_Die * unit, dw_frame_t frame,
713 const char *namespace)
715 dw_type_t type = MC_dwarf_die_to_type(info, die, unit, frame, namespace);
717 char *key = bprintf("%" PRIx64, (uint64_t) type->id);
718 xbt_dict_set(info->types, key, type, NULL);
721 if (type->name && type->byte_size != 0) {
722 xbt_dict_set(info->full_types_by_name, type->name, type, NULL);
726 static int mc_anonymous_variable_index = 0;
728 static dw_variable_t MC_die_to_variable(mc_object_info_t info, Dwarf_Die * die,
729 Dwarf_Die * unit, dw_frame_t frame,
730 const char *namespace)
732 // Skip declarations:
733 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
736 // Skip compile time constants:
737 if (dwarf_hasattr(die, DW_AT_const_value))
740 Dwarf_Attribute attr_location;
741 if (dwarf_attr(die, DW_AT_location, &attr_location) == NULL) {
742 // No location: do not add it ?
746 dw_variable_t variable = xbt_new0(s_dw_variable_t, 1);
747 variable->dwarf_offset = dwarf_dieoffset(die);
748 variable->global = frame == NULL; // Can be override base on DW_AT_location
749 variable->object_info = info;
751 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
752 variable->name = xbt_strdup(name);
754 variable->type_origin = MC_dwarf_at_type(die);
756 int form = dwarf_whatform(&attr_location);
759 DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
761 case MC_DW_CLASS_EXPRLOC:
762 case MC_DW_CLASS_BLOCK:
763 // Location expression:
767 if (dwarf_getlocation(&attr_location, &expr, &len)) {
769 ("Could not read location expression in DW_AT_location of variable <%"
770 PRIx64 ">%s", (uint64_t) variable->dwarf_offset, variable->name);
773 if (len == 1 && expr[0].atom == DW_OP_addr) {
774 variable->global = 1;
775 uintptr_t offset = (uintptr_t) expr[0].number;
776 uintptr_t base = (uintptr_t) MC_object_base_address(info);
777 variable->address = (void *) (base + offset);
779 mc_dwarf_location_list_init_from_expression(&variable->locations, len,
785 case MC_DW_CLASS_LOCLISTPTR:
786 case MC_DW_CLASS_CONSTANT:
787 // Reference to location list:
788 mc_dwarf_location_list_init(&variable->locations, info, die,
792 xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location in <%"
793 PRIx64 ">%s", form, form, klass, klass,
794 (uint64_t) variable->dwarf_offset, variable->name);
797 // Handle start_scope:
798 if (dwarf_hasattr(die, DW_AT_start_scope)) {
799 Dwarf_Attribute attr;
800 dwarf_attr(die, DW_AT_start_scope, &attr);
801 int form = dwarf_whatform(&attr);
802 int klass = MC_dwarf_form_get_class(form);
804 case MC_DW_CLASS_CONSTANT:
807 variable->start_scope =
808 dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
811 case MC_DW_CLASS_RANGELISTPTR: // TODO
814 ("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
815 form, klass, name == NULL ? "?" : name);
819 if (namespace && variable->global) {
820 char *old_name = variable->name;
821 variable->name = bprintf("%s::%s", namespace, old_name);
824 // The current code needs a variable name,
825 // generate a fake one:
826 if (!variable->name) {
827 variable->name = bprintf("@anonymous#%i", mc_anonymous_variable_index++);
833 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
834 Dwarf_Die * unit, dw_frame_t frame,
835 const char *namespace)
837 dw_variable_t variable =
838 MC_die_to_variable(info, die, unit, frame, namespace);
839 if (variable == NULL)
841 MC_dwarf_register_variable(info, frame, variable);
844 static void mc_frame_free_voipd(dw_frame_t * p)
850 static void MC_dwarf_handle_scope_die(mc_object_info_t info, Dwarf_Die * die,
851 Dwarf_Die * unit, dw_frame_t parent_frame,
852 const char *namespace)
854 // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
855 int tag = dwarf_tag(die);
856 mc_tag_class klass = MC_dwarf_tag_classify(tag);
858 // (Template) Subprogram declaration:
859 if (klass == mc_tag_subprogram
860 && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
863 if (klass == mc_tag_scope)
864 xbt_assert(parent_frame, "No parent scope for this scope");
866 dw_frame_t frame = xbt_new0(s_dw_frame_t, 1);
869 frame->id = dwarf_dieoffset(die);
870 frame->object_info = info;
872 if (klass == mc_tag_subprogram) {
873 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
875 namespace ? bprintf("%s::%s", namespace, name) : xbt_strdup(name);
878 frame->abstract_origin_id =
879 MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
881 // This is the base address for DWARF addresses.
882 // Relocated addresses are offset from this base address.
883 // See DWARF4 spec 7.5
884 void *base = MC_object_base_address(info);
886 // Variables are filled in the (recursive) call of MC_dwarf_handle_children:
888 xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
890 // TODO, support DW_AT_ranges
891 uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
892 frame->low_pc = low_pc ? ((char *) base) + low_pc : 0;
895 Dwarf_Attribute attr;
896 if (!dwarf_attr_integrate(die, DW_AT_high_pc, &attr)) {
897 xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
903 switch (MC_dwarf_form_get_class(dwarf_whatform(&attr))) {
905 // DW_AT_high_pc if an offset from the low_pc:
906 case MC_DW_CLASS_CONSTANT:
908 if (dwarf_formsdata(&attr, &offset) != 0)
909 xbt_die("Could not read constant");
910 frame->high_pc = (void *) ((char *) frame->low_pc + offset);
913 // DW_AT_high_pc is a relocatable address:
914 case MC_DW_CLASS_ADDRESS:
915 if (dwarf_formaddr(&attr, &high_pc) != 0)
916 xbt_die("Could not read address");
917 frame->high_pc = ((char *) base) + high_pc;
921 xbt_die("Unexpected class for DW_AT_high_pc");
926 if (klass == mc_tag_subprogram) {
927 Dwarf_Attribute attr_frame_base;
928 if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
929 mc_dwarf_location_list_init(&frame->frame_base, info, die,
934 xbt_dynar_new(sizeof(dw_frame_t), (void_f_pvoid_t) mc_frame_free_voipd);
937 if (klass == mc_tag_subprogram) {
938 char *key = bprintf("%" PRIx64, (uint64_t) frame->id);
939 xbt_dict_set(info->subprograms, key, frame, NULL);
941 } else if (klass == mc_tag_scope) {
942 xbt_dynar_push(parent_frame->scopes, &frame);
945 MC_dwarf_handle_children(info, die, unit, frame, namespace);
948 static void mc_dwarf_handle_namespace_die(mc_object_info_t info,
949 Dwarf_Die * die, Dwarf_Die * unit,
951 const char *namespace)
953 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
955 xbt_die("Unexpected namespace in a subprogram");
956 char *new_namespace = namespace == NULL ? xbt_strdup(name)
957 : bprintf("%s::%s", namespace, name);
958 MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
959 xbt_free(new_namespace);
962 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
963 Dwarf_Die * unit, dw_frame_t frame,
964 const char *namespace)
966 // For each child DIE:
969 for (res = dwarf_child(die, &child); res == 0;
970 res = dwarf_siblingof(&child, &child)) {
971 MC_dwarf_handle_die(info, &child, unit, frame, namespace);
975 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
976 Dwarf_Die * unit, dw_frame_t frame,
977 const char *namespace)
979 int tag = dwarf_tag(die);
980 mc_tag_class klass = MC_dwarf_tag_classify(tag);
985 MC_dwarf_handle_type_die(info, die, unit, frame, namespace);
988 // Subprogram or scope:
989 case mc_tag_subprogram:
991 MC_dwarf_handle_scope_die(info, die, unit, frame, namespace);
995 case mc_tag_variable:
996 MC_dwarf_handle_variable_die(info, die, unit, frame, namespace);
999 case mc_tag_namespace:
1000 mc_dwarf_handle_namespace_die(info, die, unit, frame, namespace);
1009 /** \brief Populate the debugging informations of the given ELF object
1011 * Read the DWARf information of the EFFL object and populate the
1012 * lists of types, variables, functions.
1014 void MC_dwarf_get_variables(mc_object_info_t info)
1016 int fd = open(info->file_name, O_RDONLY);
1018 xbt_die("Could not open file %s", info->file_name);
1020 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
1021 if (dwarf == NULL) {
1022 xbt_die("Your program must be compiled with -g");
1024 // For each compilation unit:
1025 Dwarf_Off offset = 0;
1026 Dwarf_Off next_offset = 0;
1028 while (dwarf_nextcu(dwarf, offset, &next_offset, &length, NULL, NULL, NULL) ==
1031 if (dwarf_offdie(dwarf, offset + length, &unit_die) != NULL) {
1033 // For each child DIE:
1036 for (res = dwarf_child(&unit_die, &child); res == 0;
1037 res = dwarf_siblingof(&child, &child)) {
1038 MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
1042 offset = next_offset;
1049 /************************** Free functions *************************/
1051 void mc_frame_free(dw_frame_t frame)
1053 xbt_free(frame->name);
1054 mc_dwarf_location_list_clear(&(frame->frame_base));
1055 xbt_dynar_free(&(frame->variables));
1056 xbt_dynar_free(&(frame->scopes));
1060 void dw_type_free(dw_type_t t)
1063 xbt_free(t->dw_type_id);
1064 xbt_dynar_free(&(t->members));
1065 mc_dwarf_expression_clear(&t->location);
1069 void dw_variable_free(dw_variable_t v)
1073 xbt_free(v->type_origin);
1075 if (v->locations.locations)
1076 mc_dwarf_location_list_clear(&v->locations);
1081 void dw_variable_free_voidp(void *t)
1083 dw_variable_free((dw_variable_t) * (void **) t);
1086 // ***** object_info
1090 mc_object_info_t MC_new_object_info(void)
1092 mc_object_info_t res = xbt_new0(s_mc_object_info_t, 1);
1093 res->subprograms = xbt_dict_new_homogeneous((void (*)(void *)) mc_frame_free);
1094 res->global_variables =
1095 xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
1096 res->types = xbt_dict_new_homogeneous((void (*)(void *)) dw_type_free);
1097 res->full_types_by_name = xbt_dict_new_homogeneous(NULL);
1101 void MC_free_object_info(mc_object_info_t * info)
1103 xbt_free(&(*info)->file_name);
1104 xbt_dict_free(&(*info)->subprograms);
1105 xbt_dynar_free(&(*info)->global_variables);
1106 xbt_dict_free(&(*info)->types);
1107 xbt_dict_free(&(*info)->full_types_by_name);
1109 xbt_dynar_free(&(*info)->functions_index);
1115 void *MC_object_base_address(mc_object_info_t info)
1117 if (info->flags & MC_OBJECT_INFO_EXECUTABLE)
1119 void *result = info->start_exec;
1120 if (info->start_rw != NULL && result > (void *) info->start_rw)
1121 result = info->start_rw;
1122 if (info->start_ro != NULL && result > (void *) info->start_ro)
1123 result = info->start_ro;
1127 // ***** Functions index
1129 static int MC_compare_frame_index_items(mc_function_index_item_t a,
1130 mc_function_index_item_t b)
1132 if (a->low_pc < b->low_pc)
1134 else if (a->low_pc == b->low_pc)
1140 static void MC_make_functions_index(mc_object_info_t info)
1142 xbt_dynar_t index = xbt_dynar_new(sizeof(s_mc_function_index_item_t), NULL);
1144 // Populate the array:
1145 dw_frame_t frame = NULL;
1146 xbt_dict_cursor_t cursor;
1148 xbt_dict_foreach(info->subprograms, cursor, key, frame) {
1149 if (frame->low_pc == NULL)
1151 s_mc_function_index_item_t entry;
1152 entry.low_pc = frame->low_pc;
1153 entry.high_pc = frame->high_pc;
1154 entry.function = frame;
1155 xbt_dynar_push(index, &entry);
1158 mc_function_index_item_t base =
1159 (mc_function_index_item_t) xbt_dynar_get_ptr(index, 0);
1161 // Sort the array by low_pc:
1163 xbt_dynar_length(index),
1164 sizeof(s_mc_function_index_item_t),
1165 (int (*)(const void *, const void *)) MC_compare_frame_index_items);
1167 info->functions_index = index;
1170 mc_object_info_t MC_ip_find_object_info(void *ip)
1173 for (i = 0; i != mc_object_infos_size; ++i) {
1174 if (ip >= (void *) mc_object_infos[i]->start_exec
1175 && ip <= (void *) mc_object_infos[i]->end_exec) {
1176 return mc_object_infos[i];
1182 static dw_frame_t MC_find_function_by_ip_and_object(void *ip,
1183 mc_object_info_t info)
1185 xbt_dynar_t dynar = info->functions_index;
1186 mc_function_index_item_t base =
1187 (mc_function_index_item_t) xbt_dynar_get_ptr(dynar, 0);
1189 int j = xbt_dynar_length(dynar) - 1;
1191 int k = i + ((j - i) / 2);
1192 if (ip < base[k].low_pc) {
1194 } else if (ip >= base[k].high_pc) {
1197 return base[k].function;
1203 dw_frame_t MC_find_function_by_ip(void *ip)
1205 mc_object_info_t info = MC_ip_find_object_info(ip);
1209 return MC_find_function_by_ip_and_object(ip, info);
1212 static void MC_post_process_variables(mc_object_info_t info)
1214 unsigned cursor = 0;
1215 dw_variable_t variable = NULL;
1216 xbt_dynar_foreach(info->global_variables, cursor, variable) {
1217 if (variable->type_origin) {
1218 variable->type = xbt_dict_get_or_null(info->types, variable->type_origin);
1223 static void mc_post_process_scope(mc_object_info_t info, dw_frame_t scope)
1226 if (scope->tag == DW_TAG_inlined_subroutine) {
1228 // Attach correct namespaced name in inlined subroutine:
1229 char *key = bprintf("%" PRIx64, (uint64_t) scope->abstract_origin_id);
1230 dw_frame_t abstract_origin = xbt_dict_get_or_null(info->subprograms, key);
1231 xbt_assert(abstract_origin, "Could not lookup abstract origin %s", key);
1233 scope->name = xbt_strdup(abstract_origin->name);
1237 unsigned cursor = 0;
1238 dw_variable_t variable = NULL;
1239 xbt_dynar_foreach(scope->variables, cursor, variable) {
1240 if (variable->type_origin) {
1241 variable->type = xbt_dict_get_or_null(info->types, variable->type_origin);
1245 // Recursive post-processing of nested-scopes:
1246 dw_frame_t nested_scope = NULL;
1247 xbt_dynar_foreach(scope->scopes, cursor, nested_scope)
1248 mc_post_process_scope(info, nested_scope);
1252 static void MC_post_process_functions(mc_object_info_t info)
1254 xbt_dict_cursor_t cursor;
1256 dw_frame_t subprogram = NULL;
1257 xbt_dict_foreach(info->subprograms, cursor, key, subprogram) {
1258 mc_post_process_scope(info, subprogram);
1262 /** \brief Finds informations about a given shared object/executable */
1263 mc_object_info_t MC_find_object_info(memory_map_t maps, char *name,
1266 mc_object_info_t result = MC_new_object_info();
1268 result->flags |= MC_OBJECT_INFO_EXECUTABLE;
1269 result->file_name = xbt_strdup(name);
1270 MC_find_object_address(maps, result);
1271 MC_dwarf_get_variables(result);
1272 MC_post_process_types(result);
1273 MC_post_process_variables(result);
1274 MC_post_process_functions(result);
1275 MC_make_functions_index(result);
1279 /*************************************************************************/
1281 static int MC_dwarf_get_variable_index(xbt_dynar_t variables, char *var,
1285 if (xbt_dynar_is_empty(variables))
1288 unsigned int cursor = 0;
1290 int end = xbt_dynar_length(variables) - 1;
1291 dw_variable_t var_test = NULL;
1293 while (start <= end) {
1294 cursor = (start + end) / 2;
1296 (dw_variable_t) xbt_dynar_get_as(variables, cursor, dw_variable_t);
1297 if (strcmp(var_test->name, var) < 0) {
1299 } else if (strcmp(var_test->name, var) > 0) {
1302 if (address) { /* global variable */
1303 if (var_test->address == address)
1305 if (var_test->address > address)
1309 } else { /* local variable */
1315 if (strcmp(var_test->name, var) == 0) {
1316 if (address && var_test->address < address)
1320 } else if (strcmp(var_test->name, var) < 0)
1327 void MC_dwarf_register_global_variable(mc_object_info_t info,
1328 dw_variable_t variable)
1331 MC_dwarf_get_variable_index(info->global_variables, variable->name,
1334 xbt_dynar_insert_at(info->global_variables, index, &variable);
1338 void MC_dwarf_register_non_global_variable(mc_object_info_t info,
1340 dw_variable_t variable)
1342 xbt_assert(frame, "Frame is NULL");
1344 MC_dwarf_get_variable_index(frame->variables, variable->name, NULL);
1346 xbt_dynar_insert_at(frame->variables, index, &variable);
1350 void MC_dwarf_register_variable(mc_object_info_t info, dw_frame_t frame,
1351 dw_variable_t variable)
1353 if (variable->global)
1354 MC_dwarf_register_global_variable(info, variable);
1355 else if (frame == NULL)
1356 xbt_die("No frame for this local variable");
1358 MC_dwarf_register_non_global_variable(info, frame, variable);
1361 void MC_post_process_object_info(mc_object_info_t info)
1363 xbt_dict_cursor_t cursor = NULL;
1365 dw_type_t type = NULL;
1366 xbt_dict_foreach(info->types, cursor, key, type) {
1368 // Resolve full_type:
1369 if (type->name && type->byte_size == 0) {
1370 for (size_t i = 0; i != mc_object_infos_size; ++i) {
1371 dw_type_t same_type =
1372 xbt_dict_get_or_null(mc_object_infos[i]->full_types_by_name,
1374 if (same_type && same_type->name && same_type->byte_size) {
1375 type->full_type = same_type;