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_object_info.h"
18 #include "mc_private.h"
20 static void MC_dwarf_register_global_variable(mc_object_info_t info, dw_variable_t variable);
21 static void MC_register_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
22 static void MC_dwarf_register_non_global_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
23 static void MC_dwarf_register_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing");
27 /** \brief The default DW_TAG_lower_bound for a given DW_AT_language.
29 * The default for a given language is defined in the DWARF spec.
31 * \param language consant as defined by the DWARf spec
33 static uint64_t MC_dwarf_default_lower_bound(int lang);
35 /** \brief Computes the the element_count of a DW_TAG_enumeration_type DIE
37 * This is the number of elements in a given array dimension.
39 * A reference of the compilation unit (DW_TAG_compile_unit) is
40 * needed because the default lower bound (when there is no DW_AT_lower_bound)
41 * depends of the language of the compilation unit (DW_AT_language).
43 * \param die DIE for the DW_TAG_enumeration_type or DW_TAG_subrange_type
44 * \param unit DIE of the DW_TAG_compile_unit
46 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
49 /** \brief Computes the number of elements of a given DW_TAG_array_type.
51 * \param die DIE for the DW_TAG_array_type
53 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit);
55 /** \brief Process a DIE
57 * \param info the resulting object fot the library/binary file (output)
58 * \param die the current DIE
59 * \param unit the DIE of the compile unit of the current DIE
60 * \param frame containg frame if any
62 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
63 Dwarf_Die * unit, dw_frame_t frame,
64 const char *namespace);
66 /** \brief Process a type DIE
68 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
69 Dwarf_Die * unit, dw_frame_t frame,
70 const char *namespace);
72 /** \brief Calls MC_dwarf_handle_die on all childrend of the given die
74 * \param info the resulting object fot the library/binary file (output)
75 * \param die the current DIE
76 * \param unit the DIE of the compile unit of the current DIE
77 * \param frame containg frame if any
79 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
80 Dwarf_Die * unit, dw_frame_t frame,
81 const char *namespace);
83 /** \brief Handle a variable (DW_TAG_variable or other)
85 * \param info the resulting object fot the library/binary file (output)
86 * \param die the current DIE
87 * \param unit the DIE of the compile unit of the current DIE
88 * \param frame containg frame if any
90 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
91 Dwarf_Die * unit, dw_frame_t frame,
92 const char *namespace);
94 /** \brief Get the DW_TAG_type of the DIE
97 * \return DW_TAG_type attribute as a new string (NULL if none)
99 static char *MC_dwarf_at_type(Dwarf_Die * die);
101 /** \brief Get the name of an attribute (DW_AT_*) from its code
103 * \param attr attribute code (see the DWARF specification)
104 * \return name of the attribute
106 const char *MC_dwarf_attrname(int attr)
109 #include "mc_dwarf_attrnames.h"
111 return "DW_AT_unknown";
115 /** \brief Get the name of a dwarf tag (DW_TAG_*) from its code
117 * \param tag tag code (see the DWARF specification)
118 * \return name of the tag
120 const char *MC_dwarf_tagname(int tag)
123 #include "mc_dwarf_tagnames.h"
125 return "DW_TAG_invalid";
127 return "DW_TAG_unknown";
131 /** \brief A class of DWARF tags (DW_TAG_*)
133 typedef enum mc_tag_class {
142 static mc_tag_class MC_dwarf_tag_classify(int tag)
146 case DW_TAG_array_type:
147 case DW_TAG_class_type:
148 case DW_TAG_enumeration_type:
150 case DW_TAG_pointer_type:
151 case DW_TAG_reference_type:
152 case DW_TAG_rvalue_reference_type:
153 case DW_TAG_string_type:
154 case DW_TAG_structure_type:
155 case DW_TAG_subroutine_type:
156 case DW_TAG_union_type:
157 case DW_TAG_ptr_to_member_type:
158 case DW_TAG_set_type:
159 case DW_TAG_subrange_type:
160 case DW_TAG_base_type:
161 case DW_TAG_const_type:
162 case DW_TAG_file_type:
163 case DW_TAG_packed_type:
164 case DW_TAG_volatile_type:
165 case DW_TAG_restrict_type:
166 case DW_TAG_interface_type:
167 case DW_TAG_unspecified_type:
168 case DW_TAG_shared_type:
171 case DW_TAG_subprogram:
172 return mc_tag_subprogram;
174 case DW_TAG_variable:
175 case DW_TAG_formal_parameter:
176 return mc_tag_variable;
178 case DW_TAG_lexical_block:
179 case DW_TAG_try_block:
180 case DW_TAG_catch_block:
181 case DW_TAG_inlined_subroutine:
182 case DW_TAG_with_stmt:
185 case DW_TAG_namespace:
186 return mc_tag_namespace;
189 return mc_tag_unknown;
194 #define MC_DW_CLASS_UNKNOWN 0
195 #define MC_DW_CLASS_ADDRESS 1 // Location in the address space of the program
196 #define MC_DW_CLASS_BLOCK 2 // Arbitrary block of bytes
197 #define MC_DW_CLASS_CONSTANT 3
198 #define MC_DW_CLASS_STRING 3 // String
199 #define MC_DW_CLASS_FLAG 4 // Boolean
200 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
201 #define MC_DW_CLASS_EXPRLOC 6 // DWARF expression/location description
202 #define MC_DW_CLASS_LINEPTR 7
203 #define MC_DW_CLASS_LOCLISTPTR 8
204 #define MC_DW_CLASS_MACPTR 9
205 #define MC_DW_CLASS_RANGELISTPTR 10
207 /** \brief Find the DWARF data class for a given DWARF data form
209 * This mapping is defined in the DWARF spec.
211 * \param form The form (values taken from the DWARF spec)
212 * \return An internal representation for the corresponding class
214 static int MC_dwarf_form_get_class(int form)
218 return MC_DW_CLASS_ADDRESS;
223 return MC_DW_CLASS_BLOCK;
230 return MC_DW_CLASS_CONSTANT;
233 return MC_DW_CLASS_STRING;
234 case DW_FORM_ref_addr:
239 case DW_FORM_ref_udata:
240 return MC_DW_CLASS_REFERENCE;
242 case DW_FORM_flag_present:
243 return MC_DW_CLASS_FLAG;
244 case DW_FORM_exprloc:
245 return MC_DW_CLASS_EXPRLOC;
249 return MC_DW_CLASS_UNKNOWN;
253 /** \brief Get the name of the tag of a given DIE
256 * \return name of the tag of this DIE
258 static inline const char *MC_dwarf_die_tagname(Dwarf_Die * die)
260 return MC_dwarf_tagname(dwarf_tag(die));
265 /** \brief Get an attribute of a given DIE as a string
268 * \param attribute attribute
269 * \return value of the given attribute of the given DIE
271 static const char *MC_dwarf_attr_integrate_string(Dwarf_Die * die,
274 Dwarf_Attribute attr;
275 if (!dwarf_attr_integrate(die, attribute, &attr)) {
278 return dwarf_formstring(&attr);
282 /** \brief Get the linkage name of a DIE.
284 * Use either DW_AT_linkage_name or DW_AT_MIPS_linkage_name.
285 * DW_AT_linkage_name is standardized since DWARF 4.
286 * Before this version of DWARF, the MIPS extensions
287 * DW_AT_MIPS_linkage_name is used (at least by GCC).
290 * \return linkage name of the given DIE (or NULL)
292 static const char *MC_dwarf_at_linkage_name(Dwarf_Die * die)
294 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_linkage_name);
296 name = MC_dwarf_attr_integrate_string(die, DW_AT_MIPS_linkage_name);
300 static Dwarf_Off MC_dwarf_attr_dieoffset(Dwarf_Die * die, int attribute)
302 Dwarf_Attribute attr;
303 if (dwarf_hasattr_integrate(die, attribute)) {
304 dwarf_attr_integrate(die, attribute, &attr);
305 Dwarf_Die subtype_die;
306 if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
307 xbt_die("Could not find DIE");
309 return dwarf_dieoffset(&subtype_die);
314 static Dwarf_Off MC_dwarf_attr_integrate_dieoffset(Dwarf_Die * die,
317 Dwarf_Attribute attr;
318 if (dwarf_hasattr_integrate(die, attribute)) {
319 dwarf_attr_integrate(die, DW_AT_type, &attr);
320 Dwarf_Die subtype_die;
321 if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
322 xbt_die("Could not find DIE");
324 return dwarf_dieoffset(&subtype_die);
329 /** \brief Find the type/subtype (DW_AT_type) for a DIE
332 * \return DW_AT_type reference as a global offset in hexadecimal (or NULL)
334 static char *MC_dwarf_at_type(Dwarf_Die * die)
336 Dwarf_Off offset = MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
337 return offset == 0 ? NULL : bprintf("%" PRIx64, offset);
340 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die * die, int attribute)
342 Dwarf_Attribute attr;
343 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
346 if (dwarf_formaddr(&attr, &value) == 0)
347 return (uint64_t) value;
352 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die * die, int attribute,
353 uint64_t default_value)
355 Dwarf_Attribute attr;
356 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
357 return default_value;
359 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr),
360 &value) == 0 ? (uint64_t) value : default_value;
363 static bool MC_dwarf_attr_flag(Dwarf_Die * die, int attribute, bool integrate)
365 Dwarf_Attribute attr;
366 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
367 : dwarf_attr(die, attribute, &attr)) == 0)
371 if (dwarf_formflag(&attr, &result))
372 xbt_die("Unexpected form for attribute %s", MC_dwarf_attrname(attribute));
376 /** \brief Find the default lower bound for a given language
378 * The default lower bound of an array (when DW_TAG_lower_bound
379 * is missing) depends on the language of the compilation unit.
381 * \param lang Language of the compilation unit (values defined in the DWARF spec)
382 * \return Default lower bound of an array in this compilation unit
384 static uint64_t MC_dwarf_default_lower_bound(int lang)
390 case DW_LANG_C_plus_plus:
394 case DW_LANG_ObjC_plus_plus:
400 case DW_LANG_Fortran77:
401 case DW_LANG_Fortran90:
402 case DW_LANG_Fortran95:
403 case DW_LANG_Modula2:
404 case DW_LANG_Pascal83:
406 case DW_LANG_Cobol74:
407 case DW_LANG_Cobol85:
410 xbt_die("No default DW_TAG_lower_bound for language %i and none given",
416 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
419 * \param unit DIE of the compilation unit
420 * \return number of elements in the range
422 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
425 xbt_assert(dwarf_tag(die) == DW_TAG_enumeration_type
426 || dwarf_tag(die) == DW_TAG_subrange_type,
427 "MC_dwarf_subrange_element_count called with DIE of type %s",
428 MC_dwarf_die_tagname(die));
430 // Use DW_TAG_count if present:
431 if (dwarf_hasattr_integrate(die, DW_AT_count)) {
432 return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
434 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
436 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
437 // This is not really 0, but the code expects this (we do not know):
440 uint64_t upper_bound =
441 MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
443 uint64_t lower_bound = 0;
444 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
445 lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
447 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
449 return upper_bound - lower_bound + 1;
452 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
454 * The compilation unit might be needed because the default lower
455 * bound depends on the language of the compilation unit.
457 * \param die the DIE of the DW_TAG_array_type
458 * \param unit the DIE of the compilation unit
459 * \return number of elements in this array type
461 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit)
463 xbt_assert(dwarf_tag(die) == DW_TAG_array_type,
464 "MC_dwarf_array_element_count called with DIE of type %s",
465 MC_dwarf_die_tagname(die));
470 for (res = dwarf_child(die, &child); res == 0;
471 res = dwarf_siblingof(&child, &child)) {
472 int child_tag = dwarf_tag(&child);
473 if (child_tag == DW_TAG_subrange_type
474 || child_tag == DW_TAG_enumeration_type) {
475 result *= MC_dwarf_subrange_element_count(&child, unit);
483 /** \brief Initialize the location of a member of a type
484 * (DW_AT_data_member_location of a DW_TAG_member).
486 * \param type a type (struct, class)
487 * \param member the member of the type
488 * \param child DIE of the member (DW_TAG_member)
490 static void MC_dwarf_fill_member_location(dw_type_t type, dw_type_t member,
493 if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
494 xbt_die("Can't groke DW_AT_data_bit_offset.");
497 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
498 if (type->type != DW_TAG_union_type) {
500 ("Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%"
501 PRIx64 ">%s", member->name, (uint64_t) type->id, type->name);
507 Dwarf_Attribute attr;
508 dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
509 int form = dwarf_whatform(&attr);
510 int klass = MC_dwarf_form_get_class(form);
512 case MC_DW_CLASS_EXPRLOC:
513 case MC_DW_CLASS_BLOCK:
514 // Location expression:
518 if (dwarf_getlocation(&attr, &expr, &len)) {
520 ("Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%"
521 PRIx64 ">%s", MC_dwarf_attr_integrate_string(child, DW_AT_name),
522 (uint64_t) type->id, type->name);
524 if (len == 1 && expr[0].atom == DW_OP_plus_uconst) {
525 member->offset = expr[0].number;
527 mc_dwarf_expression_init(&member->location, len, expr);
531 case MC_DW_CLASS_CONSTANT:
532 // Offset from the base address of the object:
535 if (!dwarf_formudata(&attr, &offset))
536 member->offset = offset;
538 xbt_die("Cannot get %s location <%" PRIx64 ">%s",
539 MC_dwarf_attr_integrate_string(child, DW_AT_name),
540 (uint64_t) type->id, type->name);
543 case MC_DW_CLASS_LOCLISTPTR:
544 // Reference to a location list:
546 case MC_DW_CLASS_REFERENCE:
547 // It's supposed to be possible in DWARF2 but I couldn't find its semantic
550 xbt_die("Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
556 static void dw_type_free_voidp(void *t)
558 dw_type_free((dw_type_t) * (void **) t);
561 /** \brief Populate the list of members of a type
563 * \param info ELF object containing the type DIE
564 * \param die DIE of the type
565 * \param unit DIE of the compilation unit containing the type DIE
566 * \param type the type
568 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die * die,
569 Dwarf_Die * unit, dw_type_t type)
573 xbt_assert(!type->members);
575 xbt_dynar_new(sizeof(dw_type_t), (void (*)(void *)) dw_type_free_voidp);
576 for (res = dwarf_child(die, &child); res == 0;
577 res = dwarf_siblingof(&child, &child)) {
578 int tag = dwarf_tag(&child);
579 if (tag == DW_TAG_member || tag == DW_TAG_inheritance) {
581 // Skip declarations:
582 if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
585 // Skip compile time constants:
586 if (dwarf_hasattr(&child, DW_AT_const_value))
589 // TODO, we should use another type (because is is not a type but a member)
590 dw_type_t member = xbt_new0(s_dw_type_t, 1);
594 member->id = dwarf_dieoffset(&child);
596 const char *name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
598 member->name = xbt_strdup(name);
603 MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
604 member->element_count = -1;
605 member->dw_type_id = MC_dwarf_at_type(&child);
606 member->members = NULL;
607 member->is_pointer_type = 0;
610 if (dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
611 xbt_die("Can't groke DW_AT_data_bit_offset.");
614 MC_dwarf_fill_member_location(type, member, &child);
616 if (!member->dw_type_id) {
617 xbt_die("Missing type for member %s of <%" PRIx64 ">%s", member->name,
618 (uint64_t) type->id, type->name);
621 xbt_dynar_push(type->members, &member);
626 /** \brief Create a MC type object from a DIE
628 * \param info current object info object
629 * \param DIE (for a given type);
630 * \param unit compilation unit of the current DIE
631 * \return MC representation of the type
633 static dw_type_t MC_dwarf_die_to_type(mc_object_info_t info, Dwarf_Die * die,
634 Dwarf_Die * unit, dw_frame_t frame,
635 const char *namespace)
638 dw_type_t type = xbt_new0(s_dw_type_t, 1);
643 type->element_count = -1;
644 type->dw_type_id = NULL;
645 type->members = NULL;
646 type->is_pointer_type = 0;
649 type->type = dwarf_tag(die);
652 type->id = dwarf_dieoffset(die);
654 const char *prefix = "";
655 switch (type->type) {
656 case DW_TAG_structure_type:
659 case DW_TAG_union_type:
662 case DW_TAG_class_type:
669 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
672 namespace ? bprintf("%s%s::%s", prefix, namespace,
673 name) : bprintf("%s%s", prefix, name);
676 type->dw_type_id = MC_dwarf_at_type(die);
678 // Computation of the byte_size;
679 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
680 type->byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
681 else if (type->type == DW_TAG_array_type
682 || type->type == DW_TAG_structure_type
683 || type->type == DW_TAG_class_type) {
685 if (dwarf_aggregate_size(die, &size) == 0) {
686 type->byte_size = size;
690 switch (type->type) {
691 case DW_TAG_array_type:
692 type->element_count = MC_dwarf_array_element_count(die, unit);
693 // TODO, handle DW_byte_stride and (not) DW_bit_stride
696 case DW_TAG_pointer_type:
697 case DW_TAG_reference_type:
698 case DW_TAG_rvalue_reference_type:
699 type->is_pointer_type = 1;
702 case DW_TAG_structure_type:
703 case DW_TAG_union_type:
704 case DW_TAG_class_type:
705 MC_dwarf_add_members(info, die, unit, type);
706 char *new_namespace = namespace == NULL ? xbt_strdup(type->name)
707 : bprintf("%s::%s", namespace, name);
708 MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
716 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
717 Dwarf_Die * unit, dw_frame_t frame,
718 const char *namespace)
720 dw_type_t type = MC_dwarf_die_to_type(info, die, unit, frame, namespace);
722 char *key = bprintf("%" PRIx64, (uint64_t) type->id);
723 xbt_dict_set(info->types, key, type, NULL);
726 if (type->name && type->byte_size != 0) {
727 xbt_dict_set(info->full_types_by_name, type->name, type, NULL);
731 static int mc_anonymous_variable_index = 0;
733 static dw_variable_t MC_die_to_variable(mc_object_info_t info, Dwarf_Die * die,
734 Dwarf_Die * unit, dw_frame_t frame,
735 const char *namespace)
737 // Skip declarations:
738 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
741 // Skip compile time constants:
742 if (dwarf_hasattr(die, DW_AT_const_value))
745 Dwarf_Attribute attr_location;
746 if (dwarf_attr(die, DW_AT_location, &attr_location) == NULL) {
747 // No location: do not add it ?
751 dw_variable_t variable = xbt_new0(s_dw_variable_t, 1);
752 variable->dwarf_offset = dwarf_dieoffset(die);
753 variable->global = frame == NULL; // Can be override base on DW_AT_location
754 variable->object_info = info;
756 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
757 variable->name = xbt_strdup(name);
759 variable->type_origin = MC_dwarf_at_type(die);
761 int form = dwarf_whatform(&attr_location);
764 DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
766 case MC_DW_CLASS_EXPRLOC:
767 case MC_DW_CLASS_BLOCK:
768 // Location expression:
772 if (dwarf_getlocation(&attr_location, &expr, &len)) {
774 ("Could not read location expression in DW_AT_location of variable <%"
775 PRIx64 ">%s", (uint64_t) variable->dwarf_offset, variable->name);
778 if (len == 1 && expr[0].atom == DW_OP_addr) {
779 variable->global = 1;
780 uintptr_t offset = (uintptr_t) expr[0].number;
781 uintptr_t base = (uintptr_t) MC_object_base_address(info);
782 variable->address = (void *) (base + offset);
784 mc_dwarf_location_list_init_from_expression(&variable->locations, len,
790 case MC_DW_CLASS_LOCLISTPTR:
791 case MC_DW_CLASS_CONSTANT:
792 // Reference to location list:
793 mc_dwarf_location_list_init(&variable->locations, info, die,
797 xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location in <%"
798 PRIx64 ">%s", form, form, klass, klass,
799 (uint64_t) variable->dwarf_offset, variable->name);
802 // Handle start_scope:
803 if (dwarf_hasattr(die, DW_AT_start_scope)) {
804 Dwarf_Attribute attr;
805 dwarf_attr(die, DW_AT_start_scope, &attr);
806 int form = dwarf_whatform(&attr);
807 int klass = MC_dwarf_form_get_class(form);
809 case MC_DW_CLASS_CONSTANT:
812 variable->start_scope =
813 dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
816 case MC_DW_CLASS_RANGELISTPTR: // TODO
819 ("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
820 form, klass, name == NULL ? "?" : name);
824 if (namespace && variable->global) {
825 char *old_name = variable->name;
826 variable->name = bprintf("%s::%s", namespace, old_name);
829 // The current code needs a variable name,
830 // generate a fake one:
831 if (!variable->name) {
832 variable->name = bprintf("@anonymous#%i", mc_anonymous_variable_index++);
838 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
839 Dwarf_Die * unit, dw_frame_t frame,
840 const char *namespace)
842 dw_variable_t variable =
843 MC_die_to_variable(info, die, unit, frame, namespace);
844 if (variable == NULL)
846 MC_dwarf_register_variable(info, frame, variable);
849 static void mc_frame_free_voipd(dw_frame_t * p)
855 static void MC_dwarf_handle_scope_die(mc_object_info_t info, Dwarf_Die * die,
856 Dwarf_Die * unit, dw_frame_t parent_frame,
857 const char *namespace)
859 // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
860 int tag = dwarf_tag(die);
861 mc_tag_class klass = MC_dwarf_tag_classify(tag);
863 // (Template) Subprogram declaration:
864 if (klass == mc_tag_subprogram
865 && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
868 if (klass == mc_tag_scope)
869 xbt_assert(parent_frame, "No parent scope for this scope");
871 dw_frame_t frame = xbt_new0(s_dw_frame_t, 1);
874 frame->id = dwarf_dieoffset(die);
875 frame->object_info = info;
877 if (klass == mc_tag_subprogram) {
878 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
880 namespace ? bprintf("%s::%s", namespace, name) : xbt_strdup(name);
883 frame->abstract_origin_id =
884 MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
886 // This is the base address for DWARF addresses.
887 // Relocated addresses are offset from this base address.
888 // See DWARF4 spec 7.5
889 void *base = MC_object_base_address(info);
891 // Variables are filled in the (recursive) call of MC_dwarf_handle_children:
893 xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
895 // TODO, support DW_AT_ranges
896 uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
897 frame->low_pc = low_pc ? ((char *) base) + low_pc : 0;
900 Dwarf_Attribute attr;
901 if (!dwarf_attr_integrate(die, DW_AT_high_pc, &attr)) {
902 xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
908 switch (MC_dwarf_form_get_class(dwarf_whatform(&attr))) {
910 // DW_AT_high_pc if an offset from the low_pc:
911 case MC_DW_CLASS_CONSTANT:
913 if (dwarf_formsdata(&attr, &offset) != 0)
914 xbt_die("Could not read constant");
915 frame->high_pc = (void *) ((char *) frame->low_pc + offset);
918 // DW_AT_high_pc is a relocatable address:
919 case MC_DW_CLASS_ADDRESS:
920 if (dwarf_formaddr(&attr, &high_pc) != 0)
921 xbt_die("Could not read address");
922 frame->high_pc = ((char *) base) + high_pc;
926 xbt_die("Unexpected class for DW_AT_high_pc");
931 if (klass == mc_tag_subprogram) {
932 Dwarf_Attribute attr_frame_base;
933 if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
934 mc_dwarf_location_list_init(&frame->frame_base, info, die,
939 xbt_dynar_new(sizeof(dw_frame_t), (void_f_pvoid_t) mc_frame_free_voipd);
942 if (klass == mc_tag_subprogram) {
943 char *key = bprintf("%" PRIx64, (uint64_t) frame->id);
944 xbt_dict_set(info->subprograms, key, frame, NULL);
946 } else if (klass == mc_tag_scope) {
947 xbt_dynar_push(parent_frame->scopes, &frame);
950 MC_dwarf_handle_children(info, die, unit, frame, namespace);
953 static void mc_dwarf_handle_namespace_die(mc_object_info_t info,
954 Dwarf_Die * die, Dwarf_Die * unit,
956 const char *namespace)
958 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
960 xbt_die("Unexpected namespace in a subprogram");
961 char *new_namespace = namespace == NULL ? xbt_strdup(name)
962 : bprintf("%s::%s", namespace, name);
963 MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
964 xbt_free(new_namespace);
967 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
968 Dwarf_Die * unit, dw_frame_t frame,
969 const char *namespace)
971 // For each child DIE:
974 for (res = dwarf_child(die, &child); res == 0;
975 res = dwarf_siblingof(&child, &child)) {
976 MC_dwarf_handle_die(info, &child, unit, frame, namespace);
980 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
981 Dwarf_Die * unit, dw_frame_t frame,
982 const char *namespace)
984 int tag = dwarf_tag(die);
985 mc_tag_class klass = MC_dwarf_tag_classify(tag);
990 MC_dwarf_handle_type_die(info, die, unit, frame, namespace);
993 // Subprogram or scope:
994 case mc_tag_subprogram:
996 MC_dwarf_handle_scope_die(info, die, unit, frame, namespace);
1000 case mc_tag_variable:
1001 MC_dwarf_handle_variable_die(info, die, unit, frame, namespace);
1004 case mc_tag_namespace:
1005 mc_dwarf_handle_namespace_die(info, die, unit, frame, namespace);
1014 /** \brief Populate the debugging informations of the given ELF object
1016 * Read the DWARf information of the EFFL object and populate the
1017 * lists of types, variables, functions.
1019 void MC_dwarf_get_variables(mc_object_info_t info)
1021 int fd = open(info->file_name, O_RDONLY);
1023 xbt_die("Could not open file %s", info->file_name);
1025 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
1026 if (dwarf == NULL) {
1027 xbt_die("Your program must be compiled with -g (%s)", info->file_name);
1029 // For each compilation unit:
1030 Dwarf_Off offset = 0;
1031 Dwarf_Off next_offset = 0;
1033 while (dwarf_nextcu(dwarf, offset, &next_offset, &length, NULL, NULL, NULL) ==
1036 if (dwarf_offdie(dwarf, offset + length, &unit_die) != NULL) {
1038 // For each child DIE:
1041 for (res = dwarf_child(&unit_die, &child); res == 0;
1042 res = dwarf_siblingof(&child, &child)) {
1043 MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
1047 offset = next_offset;
1054 /************************** Free functions *************************/
1056 void mc_frame_free(dw_frame_t frame)
1058 xbt_free(frame->name);
1059 mc_dwarf_location_list_clear(&(frame->frame_base));
1060 xbt_dynar_free(&(frame->variables));
1061 xbt_dynar_free(&(frame->scopes));
1065 void dw_type_free(dw_type_t t)
1068 xbt_free(t->dw_type_id);
1069 xbt_dynar_free(&(t->members));
1070 mc_dwarf_expression_clear(&t->location);
1074 void dw_variable_free(dw_variable_t v)
1078 xbt_free(v->type_origin);
1080 if (v->locations.locations)
1081 mc_dwarf_location_list_clear(&v->locations);
1086 void dw_variable_free_voidp(void *t)
1088 dw_variable_free((dw_variable_t) * (void **) t);
1091 // ***** object_info
1095 mc_object_info_t MC_new_object_info(void)
1097 mc_object_info_t res = xbt_new0(s_mc_object_info_t, 1);
1098 res->subprograms = xbt_dict_new_homogeneous((void (*)(void *)) mc_frame_free);
1099 res->global_variables =
1100 xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
1101 res->types = xbt_dict_new_homogeneous((void (*)(void *)) dw_type_free);
1102 res->full_types_by_name = xbt_dict_new_homogeneous(NULL);
1106 void MC_free_object_info(mc_object_info_t * info)
1108 xbt_free(&(*info)->file_name);
1109 xbt_dict_free(&(*info)->subprograms);
1110 xbt_dynar_free(&(*info)->global_variables);
1111 xbt_dict_free(&(*info)->types);
1112 xbt_dict_free(&(*info)->full_types_by_name);
1114 xbt_dynar_free(&(*info)->functions_index);
1120 void *MC_object_base_address(mc_object_info_t info)
1122 if (info->flags & MC_OBJECT_INFO_EXECUTABLE)
1124 void *result = info->start_exec;
1125 if (info->start_rw != NULL && result > (void *) info->start_rw)
1126 result = info->start_rw;
1127 if (info->start_ro != NULL && result > (void *) info->start_ro)
1128 result = info->start_ro;
1132 // ***** Functions index
1134 static int MC_compare_frame_index_items(mc_function_index_item_t a,
1135 mc_function_index_item_t b)
1137 if (a->low_pc < b->low_pc)
1139 else if (a->low_pc == b->low_pc)
1145 static void MC_make_functions_index(mc_object_info_t info)
1147 xbt_dynar_t index = xbt_dynar_new(sizeof(s_mc_function_index_item_t), NULL);
1149 // Populate the array:
1150 dw_frame_t frame = NULL;
1151 xbt_dict_cursor_t cursor;
1153 xbt_dict_foreach(info->subprograms, cursor, key, frame) {
1154 if (frame->low_pc == NULL)
1156 s_mc_function_index_item_t entry;
1157 entry.low_pc = frame->low_pc;
1158 entry.high_pc = frame->high_pc;
1159 entry.function = frame;
1160 xbt_dynar_push(index, &entry);
1163 mc_function_index_item_t base =
1164 (mc_function_index_item_t) xbt_dynar_get_ptr(index, 0);
1166 // Sort the array by low_pc:
1168 xbt_dynar_length(index),
1169 sizeof(s_mc_function_index_item_t),
1170 (int (*)(const void *, const void *)) MC_compare_frame_index_items);
1172 info->functions_index = index;
1175 static void MC_post_process_variables(mc_object_info_t info)
1177 unsigned cursor = 0;
1178 dw_variable_t variable = NULL;
1179 xbt_dynar_foreach(info->global_variables, cursor, variable) {
1180 if (variable->type_origin) {
1181 variable->type = xbt_dict_get_or_null(info->types, variable->type_origin);
1186 static void mc_post_process_scope(mc_object_info_t info, dw_frame_t scope)
1189 if (scope->tag == DW_TAG_inlined_subroutine) {
1191 // Attach correct namespaced name in inlined subroutine:
1192 char *key = bprintf("%" PRIx64, (uint64_t) scope->abstract_origin_id);
1193 dw_frame_t abstract_origin = xbt_dict_get_or_null(info->subprograms, key);
1194 xbt_assert(abstract_origin, "Could not lookup abstract origin %s", key);
1196 scope->name = xbt_strdup(abstract_origin->name);
1200 unsigned cursor = 0;
1201 dw_variable_t variable = NULL;
1202 xbt_dynar_foreach(scope->variables, cursor, variable) {
1203 if (variable->type_origin) {
1204 variable->type = xbt_dict_get_or_null(info->types, variable->type_origin);
1208 // Recursive post-processing of nested-scopes:
1209 dw_frame_t nested_scope = NULL;
1210 xbt_dynar_foreach(scope->scopes, cursor, nested_scope)
1211 mc_post_process_scope(info, nested_scope);
1215 static void MC_post_process_functions(mc_object_info_t info)
1217 xbt_dict_cursor_t cursor;
1219 dw_frame_t subprogram = NULL;
1220 xbt_dict_foreach(info->subprograms, cursor, key, subprogram) {
1221 mc_post_process_scope(info, subprogram);
1226 /** \brief Fill/lookup the "subtype" field.
1228 static void MC_resolve_subtype(mc_object_info_t info, dw_type_t type)
1231 if (type->dw_type_id == NULL)
1233 type->subtype = xbt_dict_get_or_null(info->types, type->dw_type_id);
1234 if (type->subtype == NULL)
1236 if (type->subtype->byte_size != 0)
1238 if (type->subtype->name == NULL)
1240 // Try to find a more complete description of the type:
1241 // We need to fix in order to support C++.
1244 xbt_dict_get_or_null(info->full_types_by_name, type->subtype->name);
1245 if (subtype != NULL) {
1246 type->subtype = subtype;
1251 static void MC_post_process_types(mc_object_info_t info)
1253 xbt_dict_cursor_t cursor = NULL;
1257 // Lookup "subtype" field:
1258 xbt_dict_foreach(info->types, cursor, origin, type) {
1259 MC_resolve_subtype(info, type);
1263 if (type->members != NULL)
1264 xbt_dynar_foreach(type->members, i, member) {
1265 MC_resolve_subtype(info, member);
1270 /** \brief Finds informations about a given shared object/executable */
1271 mc_object_info_t MC_find_object_info(memory_map_t maps, const char *name,
1274 mc_object_info_t result = MC_new_object_info();
1276 result->flags |= MC_OBJECT_INFO_EXECUTABLE;
1277 result->file_name = xbt_strdup(name);
1278 MC_find_object_address(maps, result);
1279 MC_dwarf_get_variables(result);
1280 MC_post_process_types(result);
1281 MC_post_process_variables(result);
1282 MC_post_process_functions(result);
1283 MC_make_functions_index(result);
1287 /*************************************************************************/
1289 static int MC_dwarf_get_variable_index(xbt_dynar_t variables, char *var,
1293 if (xbt_dynar_is_empty(variables))
1296 unsigned int cursor = 0;
1298 int end = xbt_dynar_length(variables) - 1;
1299 dw_variable_t var_test = NULL;
1301 while (start <= end) {
1302 cursor = (start + end) / 2;
1304 (dw_variable_t) xbt_dynar_get_as(variables, cursor, dw_variable_t);
1305 if (strcmp(var_test->name, var) < 0) {
1307 } else if (strcmp(var_test->name, var) > 0) {
1310 if (address) { /* global variable */
1311 if (var_test->address == address)
1313 if (var_test->address > address)
1317 } else { /* local variable */
1323 if (strcmp(var_test->name, var) == 0) {
1324 if (address && var_test->address < address)
1328 } else if (strcmp(var_test->name, var) < 0)
1335 void MC_dwarf_register_global_variable(mc_object_info_t info,
1336 dw_variable_t variable)
1339 MC_dwarf_get_variable_index(info->global_variables, variable->name,
1342 xbt_dynar_insert_at(info->global_variables, index, &variable);
1346 void MC_dwarf_register_non_global_variable(mc_object_info_t info,
1348 dw_variable_t variable)
1350 xbt_assert(frame, "Frame is NULL");
1352 MC_dwarf_get_variable_index(frame->variables, variable->name, NULL);
1354 xbt_dynar_insert_at(frame->variables, index, &variable);
1358 void MC_dwarf_register_variable(mc_object_info_t info, dw_frame_t frame,
1359 dw_variable_t variable)
1361 if (variable->global)
1362 MC_dwarf_register_global_variable(info, variable);
1363 else if (frame == NULL)
1364 xbt_die("No frame for this local variable");
1366 MC_dwarf_register_non_global_variable(info, frame, variable);
1369 void MC_post_process_object_info(mc_process_t process, mc_object_info_t info)
1371 xbt_dict_cursor_t cursor = NULL;
1373 dw_type_t type = NULL;
1374 xbt_dict_foreach(info->types, cursor, key, type) {
1376 // Resolve full_type:
1377 if (type->name && type->byte_size == 0) {
1378 for (size_t i = 0; i != process->object_infos_size; ++i) {
1379 dw_type_t same_type =
1380 xbt_dict_get_or_null(process->object_infos[i]->full_types_by_name,
1382 if (same_type && same_type->name && same_type->byte_size) {
1383 type->full_type = same_type;