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 static void MC_dwarf_register_global_variable(mc_object_info_t info, dw_variable_t variable);
20 static void MC_register_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
21 static void MC_dwarf_register_non_global_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
22 static void MC_dwarf_register_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing");
26 /** \brief The default DW_TAG_lower_bound for a given DW_AT_language.
28 * The default for a given language is defined in the DWARF spec.
30 * \param language consant as defined by the DWARf spec
32 static uint64_t MC_dwarf_default_lower_bound(int lang);
34 /** \brief Computes the the element_count of a DW_TAG_enumeration_type DIE
36 * This is the number of elements in a given array dimension.
38 * A reference of the compilation unit (DW_TAG_compile_unit) is
39 * needed because the default lower bound (when there is no DW_AT_lower_bound)
40 * depends of the language of the compilation unit (DW_AT_language).
42 * \param die DIE for the DW_TAG_enumeration_type or DW_TAG_subrange_type
43 * \param unit DIE of the DW_TAG_compile_unit
45 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
48 /** \brief Computes the number of elements of a given DW_TAG_array_type.
50 * \param die DIE for the DW_TAG_array_type
52 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit);
54 /** \brief Process a DIE
56 * \param info the resulting object fot the library/binary file (output)
57 * \param die the current DIE
58 * \param unit the DIE of the compile unit of the current DIE
59 * \param frame containg frame if any
61 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
62 Dwarf_Die * unit, dw_frame_t frame,
63 const char *namespace);
65 /** \brief Process a type DIE
67 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
68 Dwarf_Die * unit, dw_frame_t frame,
69 const char *namespace);
71 /** \brief Calls MC_dwarf_handle_die on all childrend of the given die
73 * \param info the resulting object fot the library/binary file (output)
74 * \param die the current DIE
75 * \param unit the DIE of the compile unit of the current DIE
76 * \param frame containg frame if any
78 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
79 Dwarf_Die * unit, dw_frame_t frame,
80 const char *namespace);
82 /** \brief Handle a variable (DW_TAG_variable or other)
84 * \param info the resulting object fot the library/binary file (output)
85 * \param die the current DIE
86 * \param unit the DIE of the compile unit of the current DIE
87 * \param frame containg frame if any
89 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
90 Dwarf_Die * unit, dw_frame_t frame,
91 const char *namespace);
93 /** \brief Get the DW_TAG_type of the DIE
96 * \return DW_TAG_type attribute as a new string (NULL if none)
98 static char *MC_dwarf_at_type(Dwarf_Die * die);
100 /** \brief Get the name of an attribute (DW_AT_*) from its code
102 * \param attr attribute code (see the DWARF specification)
103 * \return name of the attribute
105 const char *MC_dwarf_attrname(int attr)
108 #include "mc_dwarf_attrnames.h"
110 return "DW_AT_unknown";
114 /** \brief Get the name of a dwarf tag (DW_TAG_*) from its code
116 * \param tag tag code (see the DWARF specification)
117 * \return name of the tag
119 const char *MC_dwarf_tagname(int tag)
122 #include "mc_dwarf_tagnames.h"
124 return "DW_TAG_invalid";
126 return "DW_TAG_unknown";
130 /** \brief A class of DWARF tags (DW_TAG_*)
132 typedef enum mc_tag_class {
141 static mc_tag_class MC_dwarf_tag_classify(int tag)
145 case DW_TAG_array_type:
146 case DW_TAG_class_type:
147 case DW_TAG_enumeration_type:
149 case DW_TAG_pointer_type:
150 case DW_TAG_reference_type:
151 case DW_TAG_rvalue_reference_type:
152 case DW_TAG_string_type:
153 case DW_TAG_structure_type:
154 case DW_TAG_subroutine_type:
155 case DW_TAG_union_type:
156 case DW_TAG_ptr_to_member_type:
157 case DW_TAG_set_type:
158 case DW_TAG_subrange_type:
159 case DW_TAG_base_type:
160 case DW_TAG_const_type:
161 case DW_TAG_file_type:
162 case DW_TAG_packed_type:
163 case DW_TAG_volatile_type:
164 case DW_TAG_restrict_type:
165 case DW_TAG_interface_type:
166 case DW_TAG_unspecified_type:
167 case DW_TAG_shared_type:
170 case DW_TAG_subprogram:
171 return mc_tag_subprogram;
173 case DW_TAG_variable:
174 case DW_TAG_formal_parameter:
175 return mc_tag_variable;
177 case DW_TAG_lexical_block:
178 case DW_TAG_try_block:
179 case DW_TAG_catch_block:
180 case DW_TAG_inlined_subroutine:
181 case DW_TAG_with_stmt:
184 case DW_TAG_namespace:
185 return mc_tag_namespace;
188 return mc_tag_unknown;
193 #define MC_DW_CLASS_UNKNOWN 0
194 #define MC_DW_CLASS_ADDRESS 1 // Location in the address space of the program
195 #define MC_DW_CLASS_BLOCK 2 // Arbitrary block of bytes
196 #define MC_DW_CLASS_CONSTANT 3
197 #define MC_DW_CLASS_STRING 3 // String
198 #define MC_DW_CLASS_FLAG 4 // Boolean
199 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
200 #define MC_DW_CLASS_EXPRLOC 6 // DWARF expression/location description
201 #define MC_DW_CLASS_LINEPTR 7
202 #define MC_DW_CLASS_LOCLISTPTR 8
203 #define MC_DW_CLASS_MACPTR 9
204 #define MC_DW_CLASS_RANGELISTPTR 10
206 /** \brief Find the DWARF data class for a given DWARF data form
208 * This mapping is defined in the DWARF spec.
210 * \param form The form (values taken from the DWARF spec)
211 * \return An internal representation for the corresponding class
213 static int MC_dwarf_form_get_class(int form)
217 return MC_DW_CLASS_ADDRESS;
222 return MC_DW_CLASS_BLOCK;
229 return MC_DW_CLASS_CONSTANT;
232 return MC_DW_CLASS_STRING;
233 case DW_FORM_ref_addr:
238 case DW_FORM_ref_udata:
239 return MC_DW_CLASS_REFERENCE;
241 case DW_FORM_flag_present:
242 return MC_DW_CLASS_FLAG;
243 case DW_FORM_exprloc:
244 return MC_DW_CLASS_EXPRLOC;
248 return MC_DW_CLASS_UNKNOWN;
252 /** \brief Get the name of the tag of a given DIE
255 * \return name of the tag of this DIE
257 static inline const char *MC_dwarf_die_tagname(Dwarf_Die * die)
259 return MC_dwarf_tagname(dwarf_tag(die));
264 /** \brief Get an attribute of a given DIE as a string
267 * \param attribute attribute
268 * \return value of the given attribute of the given DIE
270 static const char *MC_dwarf_attr_integrate_string(Dwarf_Die * die,
273 Dwarf_Attribute attr;
274 if (!dwarf_attr_integrate(die, attribute, &attr)) {
277 return dwarf_formstring(&attr);
281 /** \brief Get the linkage name of a DIE.
283 * Use either DW_AT_linkage_name or DW_AT_MIPS_linkage_name.
284 * DW_AT_linkage_name is standardized since DWARF 4.
285 * Before this version of DWARF, the MIPS extensions
286 * DW_AT_MIPS_linkage_name is used (at least by GCC).
289 * \return linkage name of the given DIE (or NULL)
291 static const char *MC_dwarf_at_linkage_name(Dwarf_Die * die)
293 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_linkage_name);
295 name = MC_dwarf_attr_integrate_string(die, DW_AT_MIPS_linkage_name);
299 static Dwarf_Off MC_dwarf_attr_dieoffset(Dwarf_Die * die, int attribute)
301 Dwarf_Attribute attr;
302 if (dwarf_hasattr_integrate(die, attribute)) {
303 dwarf_attr_integrate(die, attribute, &attr);
304 Dwarf_Die subtype_die;
305 if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
306 xbt_die("Could not find DIE");
308 return dwarf_dieoffset(&subtype_die);
313 static Dwarf_Off MC_dwarf_attr_integrate_dieoffset(Dwarf_Die * die,
316 Dwarf_Attribute attr;
317 if (dwarf_hasattr_integrate(die, attribute)) {
318 dwarf_attr_integrate(die, DW_AT_type, &attr);
319 Dwarf_Die subtype_die;
320 if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
321 xbt_die("Could not find DIE");
323 return dwarf_dieoffset(&subtype_die);
328 /** \brief Find the type/subtype (DW_AT_type) for a DIE
331 * \return DW_AT_type reference as a global offset in hexadecimal (or NULL)
333 static char *MC_dwarf_at_type(Dwarf_Die * die)
335 Dwarf_Off offset = MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
336 return offset == 0 ? NULL : bprintf("%" PRIx64, offset);
339 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die * die, int attribute)
341 Dwarf_Attribute attr;
342 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
345 if (dwarf_formaddr(&attr, &value) == 0)
346 return (uint64_t) value;
351 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die * die, int attribute,
352 uint64_t default_value)
354 Dwarf_Attribute attr;
355 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
356 return default_value;
358 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr),
359 &value) == 0 ? (uint64_t) value : default_value;
362 static bool MC_dwarf_attr_flag(Dwarf_Die * die, int attribute, bool integrate)
364 Dwarf_Attribute attr;
365 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
366 : dwarf_attr(die, attribute, &attr)) == 0)
370 if (dwarf_formflag(&attr, &result))
371 xbt_die("Unexpected form for attribute %s", MC_dwarf_attrname(attribute));
375 /** \brief Find the default lower bound for a given language
377 * The default lower bound of an array (when DW_TAG_lower_bound
378 * is missing) depends on the language of the compilation unit.
380 * \param lang Language of the compilation unit (values defined in the DWARF spec)
381 * \return Default lower bound of an array in this compilation unit
383 static uint64_t MC_dwarf_default_lower_bound(int lang)
389 case DW_LANG_C_plus_plus:
393 case DW_LANG_ObjC_plus_plus:
399 case DW_LANG_Fortran77:
400 case DW_LANG_Fortran90:
401 case DW_LANG_Fortran95:
402 case DW_LANG_Modula2:
403 case DW_LANG_Pascal83:
405 case DW_LANG_Cobol74:
406 case DW_LANG_Cobol85:
409 xbt_die("No default DW_TAG_lower_bound for language %i and none given",
415 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
418 * \param unit DIE of the compilation unit
419 * \return number of elements in the range
421 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
424 xbt_assert(dwarf_tag(die) == DW_TAG_enumeration_type
425 || dwarf_tag(die) == DW_TAG_subrange_type,
426 "MC_dwarf_subrange_element_count called with DIE of type %s",
427 MC_dwarf_die_tagname(die));
429 // Use DW_TAG_count if present:
430 if (dwarf_hasattr_integrate(die, DW_AT_count)) {
431 return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
433 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
435 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
436 // This is not really 0, but the code expects this (we do not know):
439 uint64_t upper_bound =
440 MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
442 uint64_t lower_bound = 0;
443 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
444 lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
446 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
448 return upper_bound - lower_bound + 1;
451 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
453 * The compilation unit might be needed because the default lower
454 * bound depends on the language of the compilation unit.
456 * \param die the DIE of the DW_TAG_array_type
457 * \param unit the DIE of the compilation unit
458 * \return number of elements in this array type
460 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit)
462 xbt_assert(dwarf_tag(die) == DW_TAG_array_type,
463 "MC_dwarf_array_element_count called with DIE of type %s",
464 MC_dwarf_die_tagname(die));
469 for (res = dwarf_child(die, &child); res == 0;
470 res = dwarf_siblingof(&child, &child)) {
471 int child_tag = dwarf_tag(&child);
472 if (child_tag == DW_TAG_subrange_type
473 || child_tag == DW_TAG_enumeration_type) {
474 result *= MC_dwarf_subrange_element_count(&child, unit);
482 /** \brief Initialize the location of a member of a type
483 * (DW_AT_data_member_location of a DW_TAG_member).
485 * \param type a type (struct, class)
486 * \param member the member of the type
487 * \param child DIE of the member (DW_TAG_member)
489 static void MC_dwarf_fill_member_location(dw_type_t type, dw_type_t member,
492 if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
493 xbt_die("Can't groke DW_AT_data_bit_offset.");
496 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
497 if (type->type != DW_TAG_union_type) {
499 ("Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%"
500 PRIx64 ">%s", member->name, (uint64_t) type->id, type->name);
506 Dwarf_Attribute attr;
507 dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
508 int form = dwarf_whatform(&attr);
509 int klass = MC_dwarf_form_get_class(form);
511 case MC_DW_CLASS_EXPRLOC:
512 case MC_DW_CLASS_BLOCK:
513 // Location expression:
517 if (dwarf_getlocation(&attr, &expr, &len)) {
519 ("Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%"
520 PRIx64 ">%s", MC_dwarf_attr_integrate_string(child, DW_AT_name),
521 (uint64_t) type->id, type->name);
523 if (len == 1 && expr[0].atom == DW_OP_plus_uconst) {
524 member->offset = expr[0].number;
526 mc_dwarf_expression_init(&member->location, len, expr);
530 case MC_DW_CLASS_CONSTANT:
531 // Offset from the base address of the object:
534 if (!dwarf_formudata(&attr, &offset))
535 member->offset = offset;
537 xbt_die("Cannot get %s location <%" PRIx64 ">%s",
538 MC_dwarf_attr_integrate_string(child, DW_AT_name),
539 (uint64_t) type->id, type->name);
542 case MC_DW_CLASS_LOCLISTPTR:
543 // Reference to a location list:
545 case MC_DW_CLASS_REFERENCE:
546 // It's supposed to be possible in DWARF2 but I couldn't find its semantic
549 xbt_die("Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
555 static void dw_type_free_voidp(void *t)
557 dw_type_free((dw_type_t) * (void **) t);
560 /** \brief Populate the list of members of a type
562 * \param info ELF object containing the type DIE
563 * \param die DIE of the type
564 * \param unit DIE of the compilation unit containing the type DIE
565 * \param type the type
567 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die * die,
568 Dwarf_Die * unit, dw_type_t type)
572 xbt_assert(!type->members);
574 xbt_dynar_new(sizeof(dw_type_t), (void (*)(void *)) dw_type_free_voidp);
575 for (res = dwarf_child(die, &child); res == 0;
576 res = dwarf_siblingof(&child, &child)) {
577 int tag = dwarf_tag(&child);
578 if (tag == DW_TAG_member || tag == DW_TAG_inheritance) {
580 // Skip declarations:
581 if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
584 // Skip compile time constants:
585 if (dwarf_hasattr(&child, DW_AT_const_value))
588 // TODO, we should use another type (because is is not a type but a member)
589 dw_type_t member = xbt_new0(s_dw_type_t, 1);
593 member->id = dwarf_dieoffset(&child);
595 const char *name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
597 member->name = xbt_strdup(name);
602 MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
603 member->element_count = -1;
604 member->dw_type_id = MC_dwarf_at_type(&child);
605 member->members = NULL;
606 member->is_pointer_type = 0;
609 if (dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
610 xbt_die("Can't groke DW_AT_data_bit_offset.");
613 MC_dwarf_fill_member_location(type, member, &child);
615 if (!member->dw_type_id) {
616 xbt_die("Missing type for member %s of <%" PRIx64 ">%s", member->name,
617 (uint64_t) type->id, type->name);
620 xbt_dynar_push(type->members, &member);
625 /** \brief Create a MC type object from a DIE
627 * \param info current object info object
628 * \param DIE (for a given type);
629 * \param unit compilation unit of the current DIE
630 * \return MC representation of the type
632 static dw_type_t MC_dwarf_die_to_type(mc_object_info_t info, Dwarf_Die * die,
633 Dwarf_Die * unit, dw_frame_t frame,
634 const char *namespace)
637 dw_type_t type = xbt_new0(s_dw_type_t, 1);
642 type->element_count = -1;
643 type->dw_type_id = NULL;
644 type->members = NULL;
645 type->is_pointer_type = 0;
648 type->type = dwarf_tag(die);
651 type->id = dwarf_dieoffset(die);
653 const char *prefix = "";
654 switch (type->type) {
655 case DW_TAG_structure_type:
658 case DW_TAG_union_type:
661 case DW_TAG_class_type:
668 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
671 namespace ? bprintf("%s%s::%s", prefix, namespace,
672 name) : bprintf("%s%s", prefix, name);
675 type->dw_type_id = MC_dwarf_at_type(die);
677 // Computation of the byte_size;
678 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
679 type->byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
680 else if (type->type == DW_TAG_array_type
681 || type->type == DW_TAG_structure_type
682 || type->type == DW_TAG_class_type) {
684 if (dwarf_aggregate_size(die, &size) == 0) {
685 type->byte_size = size;
689 switch (type->type) {
690 case DW_TAG_array_type:
691 type->element_count = MC_dwarf_array_element_count(die, unit);
692 // TODO, handle DW_byte_stride and (not) DW_bit_stride
695 case DW_TAG_pointer_type:
696 case DW_TAG_reference_type:
697 case DW_TAG_rvalue_reference_type:
698 type->is_pointer_type = 1;
701 case DW_TAG_structure_type:
702 case DW_TAG_union_type:
703 case DW_TAG_class_type:
704 MC_dwarf_add_members(info, die, unit, type);
705 char *new_namespace = namespace == NULL ? xbt_strdup(type->name)
706 : bprintf("%s::%s", namespace, name);
707 MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
715 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
716 Dwarf_Die * unit, dw_frame_t frame,
717 const char *namespace)
719 dw_type_t type = MC_dwarf_die_to_type(info, die, unit, frame, namespace);
721 char *key = bprintf("%" PRIx64, (uint64_t) type->id);
722 xbt_dict_set(info->types, key, type, NULL);
725 if (type->name && type->byte_size != 0) {
726 xbt_dict_set(info->full_types_by_name, type->name, type, NULL);
730 static int mc_anonymous_variable_index = 0;
732 static dw_variable_t MC_die_to_variable(mc_object_info_t info, Dwarf_Die * die,
733 Dwarf_Die * unit, dw_frame_t frame,
734 const char *namespace)
736 // Skip declarations:
737 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
740 // Skip compile time constants:
741 if (dwarf_hasattr(die, DW_AT_const_value))
744 Dwarf_Attribute attr_location;
745 if (dwarf_attr(die, DW_AT_location, &attr_location) == NULL) {
746 // No location: do not add it ?
750 dw_variable_t variable = xbt_new0(s_dw_variable_t, 1);
751 variable->dwarf_offset = dwarf_dieoffset(die);
752 variable->global = frame == NULL; // Can be override base on DW_AT_location
753 variable->object_info = info;
755 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
756 variable->name = xbt_strdup(name);
758 variable->type_origin = MC_dwarf_at_type(die);
760 int form = dwarf_whatform(&attr_location);
763 DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
765 case MC_DW_CLASS_EXPRLOC:
766 case MC_DW_CLASS_BLOCK:
767 // Location expression:
771 if (dwarf_getlocation(&attr_location, &expr, &len)) {
773 ("Could not read location expression in DW_AT_location of variable <%"
774 PRIx64 ">%s", (uint64_t) variable->dwarf_offset, variable->name);
777 if (len == 1 && expr[0].atom == DW_OP_addr) {
778 variable->global = 1;
779 uintptr_t offset = (uintptr_t) expr[0].number;
780 uintptr_t base = (uintptr_t) MC_object_base_address(info);
781 variable->address = (void *) (base + offset);
783 mc_dwarf_location_list_init_from_expression(&variable->locations, len,
789 case MC_DW_CLASS_LOCLISTPTR:
790 case MC_DW_CLASS_CONSTANT:
791 // Reference to location list:
792 mc_dwarf_location_list_init(&variable->locations, info, die,
796 xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location in <%"
797 PRIx64 ">%s", form, form, klass, klass,
798 (uint64_t) variable->dwarf_offset, variable->name);
801 // Handle start_scope:
802 if (dwarf_hasattr(die, DW_AT_start_scope)) {
803 Dwarf_Attribute attr;
804 dwarf_attr(die, DW_AT_start_scope, &attr);
805 int form = dwarf_whatform(&attr);
806 int klass = MC_dwarf_form_get_class(form);
808 case MC_DW_CLASS_CONSTANT:
811 variable->start_scope =
812 dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
815 case MC_DW_CLASS_RANGELISTPTR: // TODO
818 ("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
819 form, klass, name == NULL ? "?" : name);
823 if (namespace && variable->global) {
824 char *old_name = variable->name;
825 variable->name = bprintf("%s::%s", namespace, old_name);
828 // The current code needs a variable name,
829 // generate a fake one:
830 if (!variable->name) {
831 variable->name = bprintf("@anonymous#%i", mc_anonymous_variable_index++);
837 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
838 Dwarf_Die * unit, dw_frame_t frame,
839 const char *namespace)
841 dw_variable_t variable =
842 MC_die_to_variable(info, die, unit, frame, namespace);
843 if (variable == NULL)
845 MC_dwarf_register_variable(info, frame, variable);
848 static void mc_frame_free_voipd(dw_frame_t * p)
854 static void MC_dwarf_handle_scope_die(mc_object_info_t info, Dwarf_Die * die,
855 Dwarf_Die * unit, dw_frame_t parent_frame,
856 const char *namespace)
858 // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
859 int tag = dwarf_tag(die);
860 mc_tag_class klass = MC_dwarf_tag_classify(tag);
862 // (Template) Subprogram declaration:
863 if (klass == mc_tag_subprogram
864 && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
867 if (klass == mc_tag_scope)
868 xbt_assert(parent_frame, "No parent scope for this scope");
870 dw_frame_t frame = xbt_new0(s_dw_frame_t, 1);
873 frame->id = dwarf_dieoffset(die);
874 frame->object_info = info;
876 if (klass == mc_tag_subprogram) {
877 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
879 namespace ? bprintf("%s::%s", namespace, name) : xbt_strdup(name);
882 frame->abstract_origin_id =
883 MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
885 // This is the base address for DWARF addresses.
886 // Relocated addresses are offset from this base address.
887 // See DWARF4 spec 7.5
888 void *base = MC_object_base_address(info);
890 // Variables are filled in the (recursive) call of MC_dwarf_handle_children:
892 xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
894 // TODO, support DW_AT_ranges
895 uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
896 frame->low_pc = low_pc ? ((char *) base) + low_pc : 0;
899 Dwarf_Attribute attr;
900 if (!dwarf_attr_integrate(die, DW_AT_high_pc, &attr)) {
901 xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
907 switch (MC_dwarf_form_get_class(dwarf_whatform(&attr))) {
909 // DW_AT_high_pc if an offset from the low_pc:
910 case MC_DW_CLASS_CONSTANT:
912 if (dwarf_formsdata(&attr, &offset) != 0)
913 xbt_die("Could not read constant");
914 frame->high_pc = (void *) ((char *) frame->low_pc + offset);
917 // DW_AT_high_pc is a relocatable address:
918 case MC_DW_CLASS_ADDRESS:
919 if (dwarf_formaddr(&attr, &high_pc) != 0)
920 xbt_die("Could not read address");
921 frame->high_pc = ((char *) base) + high_pc;
925 xbt_die("Unexpected class for DW_AT_high_pc");
930 if (klass == mc_tag_subprogram) {
931 Dwarf_Attribute attr_frame_base;
932 if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
933 mc_dwarf_location_list_init(&frame->frame_base, info, die,
938 xbt_dynar_new(sizeof(dw_frame_t), (void_f_pvoid_t) mc_frame_free_voipd);
941 if (klass == mc_tag_subprogram) {
942 char *key = bprintf("%" PRIx64, (uint64_t) frame->id);
943 xbt_dict_set(info->subprograms, key, frame, NULL);
945 } else if (klass == mc_tag_scope) {
946 xbt_dynar_push(parent_frame->scopes, &frame);
949 MC_dwarf_handle_children(info, die, unit, frame, namespace);
952 static void mc_dwarf_handle_namespace_die(mc_object_info_t info,
953 Dwarf_Die * die, Dwarf_Die * unit,
955 const char *namespace)
957 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
959 xbt_die("Unexpected namespace in a subprogram");
960 char *new_namespace = namespace == NULL ? xbt_strdup(name)
961 : bprintf("%s::%s", namespace, name);
962 MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
963 xbt_free(new_namespace);
966 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
967 Dwarf_Die * unit, dw_frame_t frame,
968 const char *namespace)
970 // For each child DIE:
973 for (res = dwarf_child(die, &child); res == 0;
974 res = dwarf_siblingof(&child, &child)) {
975 MC_dwarf_handle_die(info, &child, unit, frame, namespace);
979 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
980 Dwarf_Die * unit, dw_frame_t frame,
981 const char *namespace)
983 int tag = dwarf_tag(die);
984 mc_tag_class klass = MC_dwarf_tag_classify(tag);
989 MC_dwarf_handle_type_die(info, die, unit, frame, namespace);
992 // Subprogram or scope:
993 case mc_tag_subprogram:
995 MC_dwarf_handle_scope_die(info, die, unit, frame, namespace);
999 case mc_tag_variable:
1000 MC_dwarf_handle_variable_die(info, die, unit, frame, namespace);
1003 case mc_tag_namespace:
1004 mc_dwarf_handle_namespace_die(info, die, unit, frame, namespace);
1013 /** \brief Populate the debugging informations of the given ELF object
1015 * Read the DWARf information of the EFFL object and populate the
1016 * lists of types, variables, functions.
1018 void MC_dwarf_get_variables(mc_object_info_t info)
1020 int fd = open(info->file_name, O_RDONLY);
1022 xbt_die("Could not open file %s", info->file_name);
1024 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
1025 if (dwarf == NULL) {
1026 xbt_die("Your program must be compiled with -g");
1028 // For each compilation unit:
1029 Dwarf_Off offset = 0;
1030 Dwarf_Off next_offset = 0;
1032 while (dwarf_nextcu(dwarf, offset, &next_offset, &length, NULL, NULL, NULL) ==
1035 if (dwarf_offdie(dwarf, offset + length, &unit_die) != NULL) {
1037 // For each child DIE:
1040 for (res = dwarf_child(&unit_die, &child); res == 0;
1041 res = dwarf_siblingof(&child, &child)) {
1042 MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
1046 offset = next_offset;
1053 /************************** Free functions *************************/
1055 void mc_frame_free(dw_frame_t frame)
1057 xbt_free(frame->name);
1058 mc_dwarf_location_list_clear(&(frame->frame_base));
1059 xbt_dynar_free(&(frame->variables));
1060 xbt_dynar_free(&(frame->scopes));
1064 void dw_type_free(dw_type_t t)
1067 xbt_free(t->dw_type_id);
1068 xbt_dynar_free(&(t->members));
1069 mc_dwarf_expression_clear(&t->location);
1073 void dw_variable_free(dw_variable_t v)
1077 xbt_free(v->type_origin);
1079 if (v->locations.locations)
1080 mc_dwarf_location_list_clear(&v->locations);
1085 void dw_variable_free_voidp(void *t)
1087 dw_variable_free((dw_variable_t) * (void **) t);
1090 // ***** object_info
1094 mc_object_info_t MC_new_object_info(void)
1096 mc_object_info_t res = xbt_new0(s_mc_object_info_t, 1);
1097 res->subprograms = xbt_dict_new_homogeneous((void (*)(void *)) mc_frame_free);
1098 res->global_variables =
1099 xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
1100 res->types = xbt_dict_new_homogeneous((void (*)(void *)) dw_type_free);
1101 res->full_types_by_name = xbt_dict_new_homogeneous(NULL);
1105 void MC_free_object_info(mc_object_info_t * info)
1107 xbt_free(&(*info)->file_name);
1108 xbt_dict_free(&(*info)->subprograms);
1109 xbt_dynar_free(&(*info)->global_variables);
1110 xbt_dict_free(&(*info)->types);
1111 xbt_dict_free(&(*info)->full_types_by_name);
1113 xbt_dynar_free(&(*info)->functions_index);
1119 void *MC_object_base_address(mc_object_info_t info)
1121 if (info->flags & MC_OBJECT_INFO_EXECUTABLE)
1123 void *result = info->start_exec;
1124 if (info->start_rw != NULL && result > (void *) info->start_rw)
1125 result = info->start_rw;
1126 if (info->start_ro != NULL && result > (void *) info->start_ro)
1127 result = info->start_ro;
1131 // ***** Functions index
1133 static int MC_compare_frame_index_items(mc_function_index_item_t a,
1134 mc_function_index_item_t b)
1136 if (a->low_pc < b->low_pc)
1138 else if (a->low_pc == b->low_pc)
1144 static void MC_make_functions_index(mc_object_info_t info)
1146 xbt_dynar_t index = xbt_dynar_new(sizeof(s_mc_function_index_item_t), NULL);
1148 // Populate the array:
1149 dw_frame_t frame = NULL;
1150 xbt_dict_cursor_t cursor;
1152 xbt_dict_foreach(info->subprograms, cursor, key, frame) {
1153 if (frame->low_pc == NULL)
1155 s_mc_function_index_item_t entry;
1156 entry.low_pc = frame->low_pc;
1157 entry.high_pc = frame->high_pc;
1158 entry.function = frame;
1159 xbt_dynar_push(index, &entry);
1162 mc_function_index_item_t base =
1163 (mc_function_index_item_t) xbt_dynar_get_ptr(index, 0);
1165 // Sort the array by low_pc:
1167 xbt_dynar_length(index),
1168 sizeof(s_mc_function_index_item_t),
1169 (int (*)(const void *, const void *)) MC_compare_frame_index_items);
1171 info->functions_index = index;
1174 mc_object_info_t MC_ip_find_object_info(void *ip)
1177 for (i = 0; i != mc_object_infos_size; ++i) {
1178 if (ip >= (void *) mc_object_infos[i]->start_exec
1179 && ip <= (void *) mc_object_infos[i]->end_exec) {
1180 return mc_object_infos[i];
1186 static dw_frame_t MC_find_function_by_ip_and_object(void *ip,
1187 mc_object_info_t info)
1189 xbt_dynar_t dynar = info->functions_index;
1190 mc_function_index_item_t base =
1191 (mc_function_index_item_t) xbt_dynar_get_ptr(dynar, 0);
1193 int j = xbt_dynar_length(dynar) - 1;
1195 int k = i + ((j - i) / 2);
1196 if (ip < base[k].low_pc) {
1198 } else if (ip >= base[k].high_pc) {
1201 return base[k].function;
1207 dw_frame_t MC_find_function_by_ip(void *ip)
1209 mc_object_info_t info = MC_ip_find_object_info(ip);
1213 return MC_find_function_by_ip_and_object(ip, info);
1216 static void MC_post_process_variables(mc_object_info_t info)
1218 unsigned cursor = 0;
1219 dw_variable_t variable = NULL;
1220 xbt_dynar_foreach(info->global_variables, cursor, variable) {
1221 if (variable->type_origin) {
1222 variable->type = xbt_dict_get_or_null(info->types, variable->type_origin);
1227 static void mc_post_process_scope(mc_object_info_t info, dw_frame_t scope)
1230 if (scope->tag == DW_TAG_inlined_subroutine) {
1232 // Attach correct namespaced name in inlined subroutine:
1233 char *key = bprintf("%" PRIx64, (uint64_t) scope->abstract_origin_id);
1234 dw_frame_t abstract_origin = xbt_dict_get_or_null(info->subprograms, key);
1235 xbt_assert(abstract_origin, "Could not lookup abstract origin %s", key);
1237 scope->name = xbt_strdup(abstract_origin->name);
1241 unsigned cursor = 0;
1242 dw_variable_t variable = NULL;
1243 xbt_dynar_foreach(scope->variables, cursor, variable) {
1244 if (variable->type_origin) {
1245 variable->type = xbt_dict_get_or_null(info->types, variable->type_origin);
1249 // Recursive post-processing of nested-scopes:
1250 dw_frame_t nested_scope = NULL;
1251 xbt_dynar_foreach(scope->scopes, cursor, nested_scope)
1252 mc_post_process_scope(info, nested_scope);
1256 static void MC_post_process_functions(mc_object_info_t info)
1258 xbt_dict_cursor_t cursor;
1260 dw_frame_t subprogram = NULL;
1261 xbt_dict_foreach(info->subprograms, cursor, key, subprogram) {
1262 mc_post_process_scope(info, subprogram);
1267 /** \brief Fill/lookup the "subtype" field.
1269 static void MC_resolve_subtype(mc_object_info_t info, dw_type_t type)
1272 if (type->dw_type_id == NULL)
1274 type->subtype = xbt_dict_get_or_null(info->types, type->dw_type_id);
1275 if (type->subtype == NULL)
1277 if (type->subtype->byte_size != 0)
1279 if (type->subtype->name == NULL)
1281 // Try to find a more complete description of the type:
1282 // We need to fix in order to support C++.
1285 xbt_dict_get_or_null(info->full_types_by_name, type->subtype->name);
1286 if (subtype != NULL) {
1287 type->subtype = subtype;
1292 static void MC_post_process_types(mc_object_info_t info)
1294 xbt_dict_cursor_t cursor = NULL;
1298 // Lookup "subtype" field:
1299 xbt_dict_foreach(info->types, cursor, origin, type) {
1300 MC_resolve_subtype(info, type);
1304 if (type->members != NULL)
1305 xbt_dynar_foreach(type->members, i, member) {
1306 MC_resolve_subtype(info, member);
1311 /** \brief Finds informations about a given shared object/executable */
1312 mc_object_info_t MC_find_object_info(memory_map_t maps, char *name,
1315 mc_object_info_t result = MC_new_object_info();
1317 result->flags |= MC_OBJECT_INFO_EXECUTABLE;
1318 result->file_name = xbt_strdup(name);
1319 MC_find_object_address(maps, result);
1320 MC_dwarf_get_variables(result);
1321 MC_post_process_types(result);
1322 MC_post_process_variables(result);
1323 MC_post_process_functions(result);
1324 MC_make_functions_index(result);
1328 /*************************************************************************/
1330 static int MC_dwarf_get_variable_index(xbt_dynar_t variables, char *var,
1334 if (xbt_dynar_is_empty(variables))
1337 unsigned int cursor = 0;
1339 int end = xbt_dynar_length(variables) - 1;
1340 dw_variable_t var_test = NULL;
1342 while (start <= end) {
1343 cursor = (start + end) / 2;
1345 (dw_variable_t) xbt_dynar_get_as(variables, cursor, dw_variable_t);
1346 if (strcmp(var_test->name, var) < 0) {
1348 } else if (strcmp(var_test->name, var) > 0) {
1351 if (address) { /* global variable */
1352 if (var_test->address == address)
1354 if (var_test->address > address)
1358 } else { /* local variable */
1364 if (strcmp(var_test->name, var) == 0) {
1365 if (address && var_test->address < address)
1369 } else if (strcmp(var_test->name, var) < 0)
1376 void MC_dwarf_register_global_variable(mc_object_info_t info,
1377 dw_variable_t variable)
1380 MC_dwarf_get_variable_index(info->global_variables, variable->name,
1383 xbt_dynar_insert_at(info->global_variables, index, &variable);
1387 void MC_dwarf_register_non_global_variable(mc_object_info_t info,
1389 dw_variable_t variable)
1391 xbt_assert(frame, "Frame is NULL");
1393 MC_dwarf_get_variable_index(frame->variables, variable->name, NULL);
1395 xbt_dynar_insert_at(frame->variables, index, &variable);
1399 void MC_dwarf_register_variable(mc_object_info_t info, dw_frame_t frame,
1400 dw_variable_t variable)
1402 if (variable->global)
1403 MC_dwarf_register_global_variable(info, variable);
1404 else if (frame == NULL)
1405 xbt_die("No frame for this local variable");
1407 MC_dwarf_register_non_global_variable(info, frame, variable);
1410 void MC_post_process_object_info(mc_object_info_t info)
1412 xbt_dict_cursor_t cursor = NULL;
1414 dw_type_t type = NULL;
1415 xbt_dict_foreach(info->types, cursor, key, type) {
1417 // Resolve full_type:
1418 if (type->name && type->byte_size == 0) {
1419 for (size_t i = 0; i != mc_object_infos_size; ++i) {
1420 dw_type_t same_type =
1421 xbt_dict_get_or_null(mc_object_infos[i]->full_types_by_name,
1423 if (same_type && same_type->name && same_type->byte_size) {
1424 type->full_type = same_type;