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"
22 static void MC_dwarf_register_global_variable(mc_object_info_t info, dw_variable_t variable);
23 static void MC_register_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
24 static void MC_dwarf_register_non_global_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
25 static void MC_dwarf_register_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing");
29 /** \brief The default DW_TAG_lower_bound for a given DW_AT_language.
31 * The default for a given language is defined in the DWARF spec.
33 * \param language consant as defined by the DWARf spec
35 static uint64_t MC_dwarf_default_lower_bound(int lang);
37 /** \brief Computes the the element_count of a DW_TAG_enumeration_type DIE
39 * This is the number of elements in a given array dimension.
41 * A reference of the compilation unit (DW_TAG_compile_unit) is
42 * needed because the default lower bound (when there is no DW_AT_lower_bound)
43 * depends of the language of the compilation unit (DW_AT_language).
45 * \param die DIE for the DW_TAG_enumeration_type or DW_TAG_subrange_type
46 * \param unit DIE of the DW_TAG_compile_unit
48 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
51 /** \brief Computes the number of elements of a given DW_TAG_array_type.
53 * \param die DIE for the DW_TAG_array_type
55 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit);
57 /** \brief Process a DIE
59 * \param info the resulting object fot the library/binary file (output)
60 * \param die the current DIE
61 * \param unit the DIE of the compile unit of the current DIE
62 * \param frame containg frame if any
64 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
65 Dwarf_Die * unit, dw_frame_t frame,
68 /** \brief Process a type DIE
70 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
71 Dwarf_Die * unit, dw_frame_t frame,
74 /** \brief Calls MC_dwarf_handle_die on all childrend of the given die
76 * \param info the resulting object fot the library/binary file (output)
77 * \param die the current DIE
78 * \param unit the DIE of the compile unit of the current DIE
79 * \param frame containg frame if any
81 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
82 Dwarf_Die * unit, dw_frame_t frame,
85 /** \brief Handle a variable (DW_TAG_variable or other)
87 * \param info the resulting object fot the library/binary file (output)
88 * \param die the current DIE
89 * \param unit the DIE of the compile unit of the current DIE
90 * \param frame containg frame if any
92 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
93 Dwarf_Die * unit, dw_frame_t frame,
96 /** \brief Get the DW_TAG_type of the DIE
99 * \return DW_TAG_type attribute as a new string (NULL if none)
101 static char *MC_dwarf_at_type(Dwarf_Die * die);
103 /** \brief Get the name of an attribute (DW_AT_*) from its code
105 * \param attr attribute code (see the DWARF specification)
106 * \return name of the attribute
108 const char *MC_dwarf_attrname(int attr)
111 #include "mc_dwarf_attrnames.h"
113 return "DW_AT_unknown";
117 /** \brief Get the name of a dwarf tag (DW_TAG_*) from its code
119 * \param tag tag code (see the DWARF specification)
120 * \return name of the tag
122 const char *MC_dwarf_tagname(int tag)
125 #include "mc_dwarf_tagnames.h"
127 return "DW_TAG_invalid";
129 return "DW_TAG_unknown";
133 /** \brief A class of DWARF tags (DW_TAG_*)
135 typedef enum mc_tag_class {
144 static mc_tag_class MC_dwarf_tag_classify(int tag)
148 case DW_TAG_array_type:
149 case DW_TAG_class_type:
150 case DW_TAG_enumeration_type:
152 case DW_TAG_pointer_type:
153 case DW_TAG_reference_type:
154 case DW_TAG_rvalue_reference_type:
155 case DW_TAG_string_type:
156 case DW_TAG_structure_type:
157 case DW_TAG_subroutine_type:
158 case DW_TAG_union_type:
159 case DW_TAG_ptr_to_member_type:
160 case DW_TAG_set_type:
161 case DW_TAG_subrange_type:
162 case DW_TAG_base_type:
163 case DW_TAG_const_type:
164 case DW_TAG_file_type:
165 case DW_TAG_packed_type:
166 case DW_TAG_volatile_type:
167 case DW_TAG_restrict_type:
168 case DW_TAG_interface_type:
169 case DW_TAG_unspecified_type:
170 case DW_TAG_shared_type:
173 case DW_TAG_subprogram:
174 return mc_tag_subprogram;
176 case DW_TAG_variable:
177 case DW_TAG_formal_parameter:
178 return mc_tag_variable;
180 case DW_TAG_lexical_block:
181 case DW_TAG_try_block:
182 case DW_TAG_catch_block:
183 case DW_TAG_inlined_subroutine:
184 case DW_TAG_with_stmt:
187 case DW_TAG_namespace:
188 return mc_tag_namespace;
191 return mc_tag_unknown;
196 #define MC_DW_CLASS_UNKNOWN 0
197 #define MC_DW_CLASS_ADDRESS 1 // Location in the address space of the program
198 #define MC_DW_CLASS_BLOCK 2 // Arbitrary block of bytes
199 #define MC_DW_CLASS_CONSTANT 3
200 #define MC_DW_CLASS_STRING 3 // String
201 #define MC_DW_CLASS_FLAG 4 // Boolean
202 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
203 #define MC_DW_CLASS_EXPRLOC 6 // DWARF expression/location description
204 #define MC_DW_CLASS_LINEPTR 7
205 #define MC_DW_CLASS_LOCLISTPTR 8
206 #define MC_DW_CLASS_MACPTR 9
207 #define MC_DW_CLASS_RANGELISTPTR 10
209 /** \brief Find the DWARF data class for a given DWARF data form
211 * This mapping is defined in the DWARF spec.
213 * \param form The form (values taken from the DWARF spec)
214 * \return An internal representation for the corresponding class
216 static int MC_dwarf_form_get_class(int form)
220 return MC_DW_CLASS_ADDRESS;
225 return MC_DW_CLASS_BLOCK;
232 return MC_DW_CLASS_CONSTANT;
235 return MC_DW_CLASS_STRING;
236 case DW_FORM_ref_addr:
241 case DW_FORM_ref_udata:
242 return MC_DW_CLASS_REFERENCE;
244 case DW_FORM_flag_present:
245 return MC_DW_CLASS_FLAG;
246 case DW_FORM_exprloc:
247 return MC_DW_CLASS_EXPRLOC;
251 return MC_DW_CLASS_UNKNOWN;
255 /** \brief Get the name of the tag of a given DIE
258 * \return name of the tag of this DIE
260 static inline const char *MC_dwarf_die_tagname(Dwarf_Die * die)
262 return MC_dwarf_tagname(dwarf_tag(die));
267 /** \brief Get an attribute of a given DIE as a string
270 * \param attribute attribute
271 * \return value of the given attribute of the given DIE
273 static const char *MC_dwarf_attr_integrate_string(Dwarf_Die * die,
276 Dwarf_Attribute attr;
277 if (!dwarf_attr_integrate(die, attribute, &attr)) {
280 return dwarf_formstring(&attr);
284 /** \brief Get the linkage name of a DIE.
286 * Use either DW_AT_linkage_name or DW_AT_MIPS_linkage_name.
287 * DW_AT_linkage_name is standardized since DWARF 4.
288 * Before this version of DWARF, the MIPS extensions
289 * DW_AT_MIPS_linkage_name is used (at least by GCC).
292 * \return linkage name of the given DIE (or NULL)
294 static const char *MC_dwarf_at_linkage_name(Dwarf_Die * die)
296 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_linkage_name);
298 name = MC_dwarf_attr_integrate_string(die, DW_AT_MIPS_linkage_name);
302 static Dwarf_Off MC_dwarf_attr_dieoffset(Dwarf_Die * die, int attribute)
304 Dwarf_Attribute attr;
305 if (dwarf_hasattr_integrate(die, attribute)) {
306 dwarf_attr_integrate(die, attribute, &attr);
307 Dwarf_Die subtype_die;
308 if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
309 xbt_die("Could not find DIE");
311 return dwarf_dieoffset(&subtype_die);
316 static Dwarf_Off MC_dwarf_attr_integrate_dieoffset(Dwarf_Die * die,
319 Dwarf_Attribute attr;
320 if (dwarf_hasattr_integrate(die, attribute)) {
321 dwarf_attr_integrate(die, DW_AT_type, &attr);
322 Dwarf_Die subtype_die;
323 if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
324 xbt_die("Could not find DIE");
326 return dwarf_dieoffset(&subtype_die);
331 /** \brief Find the type/subtype (DW_AT_type) for a DIE
334 * \return DW_AT_type reference as a global offset in hexadecimal (or NULL)
336 static char *MC_dwarf_at_type(Dwarf_Die * die)
338 Dwarf_Off offset = MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
339 return offset == 0 ? NULL : bprintf("%" PRIx64, offset);
342 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die * die, int attribute)
344 Dwarf_Attribute attr;
345 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
348 if (dwarf_formaddr(&attr, &value) == 0)
349 return (uint64_t) value;
354 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die * die, int attribute,
355 uint64_t default_value)
357 Dwarf_Attribute attr;
358 if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
359 return default_value;
361 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr),
362 &value) == 0 ? (uint64_t) value : default_value;
365 static bool MC_dwarf_attr_flag(Dwarf_Die * die, int attribute, bool integrate)
367 Dwarf_Attribute attr;
368 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
369 : dwarf_attr(die, attribute, &attr)) == 0)
373 if (dwarf_formflag(&attr, &result))
374 xbt_die("Unexpected form for attribute %s", MC_dwarf_attrname(attribute));
378 /** \brief Find the default lower bound for a given language
380 * The default lower bound of an array (when DW_TAG_lower_bound
381 * is missing) depends on the language of the compilation unit.
383 * \param lang Language of the compilation unit (values defined in the DWARF spec)
384 * \return Default lower bound of an array in this compilation unit
386 static uint64_t MC_dwarf_default_lower_bound(int lang)
392 case DW_LANG_C_plus_plus:
396 case DW_LANG_ObjC_plus_plus:
402 case DW_LANG_Fortran77:
403 case DW_LANG_Fortran90:
404 case DW_LANG_Fortran95:
405 case DW_LANG_Modula2:
406 case DW_LANG_Pascal83:
408 case DW_LANG_Cobol74:
409 case DW_LANG_Cobol85:
412 xbt_die("No default DW_TAG_lower_bound for language %i and none given",
418 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
421 * \param unit DIE of the compilation unit
422 * \return number of elements in the range
424 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
427 xbt_assert(dwarf_tag(die) == DW_TAG_enumeration_type
428 || dwarf_tag(die) == DW_TAG_subrange_type,
429 "MC_dwarf_subrange_element_count called with DIE of type %s",
430 MC_dwarf_die_tagname(die));
432 // Use DW_TAG_count if present:
433 if (dwarf_hasattr_integrate(die, DW_AT_count)) {
434 return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
436 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
438 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
439 // This is not really 0, but the code expects this (we do not know):
442 uint64_t upper_bound =
443 MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
445 uint64_t lower_bound = 0;
446 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
447 lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
449 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
451 return upper_bound - lower_bound + 1;
454 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
456 * The compilation unit might be needed because the default lower
457 * bound depends on the language of the compilation unit.
459 * \param die the DIE of the DW_TAG_array_type
460 * \param unit the DIE of the compilation unit
461 * \return number of elements in this array type
463 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit)
465 xbt_assert(dwarf_tag(die) == DW_TAG_array_type,
466 "MC_dwarf_array_element_count called with DIE of type %s",
467 MC_dwarf_die_tagname(die));
472 for (res = dwarf_child(die, &child); res == 0;
473 res = dwarf_siblingof(&child, &child)) {
474 int child_tag = dwarf_tag(&child);
475 if (child_tag == DW_TAG_subrange_type
476 || child_tag == DW_TAG_enumeration_type) {
477 result *= MC_dwarf_subrange_element_count(&child, unit);
485 /** \brief Initialize the location of a member of a type
486 * (DW_AT_data_member_location of a DW_TAG_member).
488 * \param type a type (struct, class)
489 * \param member the member of the type
490 * \param child DIE of the member (DW_TAG_member)
492 static void MC_dwarf_fill_member_location(dw_type_t type, dw_type_t member,
495 if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
496 xbt_die("Can't groke DW_AT_data_bit_offset.");
499 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
500 if (type->type != DW_TAG_union_type) {
502 ("Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%"
503 PRIx64 ">%s", member->name, (uint64_t) type->id, type->name);
509 Dwarf_Attribute attr;
510 dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
511 int form = dwarf_whatform(&attr);
512 int klass = MC_dwarf_form_get_class(form);
514 case MC_DW_CLASS_EXPRLOC:
515 case MC_DW_CLASS_BLOCK:
516 // Location expression:
520 if (dwarf_getlocation(&attr, &expr, &len)) {
522 ("Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%"
523 PRIx64 ">%s", MC_dwarf_attr_integrate_string(child, DW_AT_name),
524 (uint64_t) type->id, type->name);
526 if (len == 1 && expr[0].atom == DW_OP_plus_uconst) {
527 member->offset = expr[0].number;
529 mc_dwarf_expression_init(&member->location, len, expr);
533 case MC_DW_CLASS_CONSTANT:
534 // Offset from the base address of the object:
537 if (!dwarf_formudata(&attr, &offset))
538 member->offset = offset;
540 xbt_die("Cannot get %s location <%" PRIx64 ">%s",
541 MC_dwarf_attr_integrate_string(child, DW_AT_name),
542 (uint64_t) type->id, type->name);
545 case MC_DW_CLASS_LOCLISTPTR:
546 // Reference to a location list:
548 case MC_DW_CLASS_REFERENCE:
549 // It's supposed to be possible in DWARF2 but I couldn't find its semantic
552 xbt_die("Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
558 static void dw_type_free_voidp(void *t)
560 dw_type_free((dw_type_t) * (void **) t);
563 /** \brief Populate the list of members of a type
565 * \param info ELF object containing the type DIE
566 * \param die DIE of the type
567 * \param unit DIE of the compilation unit containing the type DIE
568 * \param type the type
570 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die * die,
571 Dwarf_Die * unit, dw_type_t type)
575 xbt_assert(!type->members);
577 xbt_dynar_new(sizeof(dw_type_t), (void (*)(void *)) dw_type_free_voidp);
578 for (res = dwarf_child(die, &child); res == 0;
579 res = dwarf_siblingof(&child, &child)) {
580 int tag = dwarf_tag(&child);
581 if (tag == DW_TAG_member || tag == DW_TAG_inheritance) {
583 // Skip declarations:
584 if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
587 // Skip compile time constants:
588 if (dwarf_hasattr(&child, DW_AT_const_value))
591 // TODO, we should use another type (because is is not a type but a member)
592 dw_type_t member = xbt_new0(s_dw_type_t, 1);
596 member->id = dwarf_dieoffset(&child);
598 const char *name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
600 member->name = xbt_strdup(name);
605 MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
606 member->element_count = -1;
607 member->dw_type_id = MC_dwarf_at_type(&child);
608 member->members = NULL;
609 member->is_pointer_type = 0;
612 if (dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
613 xbt_die("Can't groke DW_AT_data_bit_offset.");
616 MC_dwarf_fill_member_location(type, member, &child);
618 if (!member->dw_type_id) {
619 xbt_die("Missing type for member %s of <%" PRIx64 ">%s", member->name,
620 (uint64_t) type->id, type->name);
623 xbt_dynar_push(type->members, &member);
628 /** \brief Create a MC type object from a DIE
630 * \param info current object info object
631 * \param DIE (for a given type);
632 * \param unit compilation unit of the current DIE
633 * \return MC representation of the type
635 static dw_type_t MC_dwarf_die_to_type(mc_object_info_t info, Dwarf_Die * die,
636 Dwarf_Die * unit, dw_frame_t frame,
640 dw_type_t type = xbt_new0(s_dw_type_t, 1);
645 type->element_count = -1;
646 type->dw_type_id = NULL;
647 type->members = NULL;
648 type->is_pointer_type = 0;
651 type->type = dwarf_tag(die);
654 type->id = dwarf_dieoffset(die);
656 const char *prefix = "";
657 switch (type->type) {
658 case DW_TAG_structure_type:
661 case DW_TAG_union_type:
664 case DW_TAG_class_type:
671 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
674 ns ? bprintf("%s%s::%s", prefix, ns,
675 name) : bprintf("%s%s", prefix, name);
678 type->dw_type_id = MC_dwarf_at_type(die);
680 // Computation of the byte_size;
681 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
682 type->byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
683 else if (type->type == DW_TAG_array_type
684 || type->type == DW_TAG_structure_type
685 || type->type == DW_TAG_class_type) {
687 if (dwarf_aggregate_size(die, &size) == 0) {
688 type->byte_size = size;
692 switch (type->type) {
693 case DW_TAG_array_type:
694 type->element_count = MC_dwarf_array_element_count(die, unit);
695 // TODO, handle DW_byte_stride and (not) DW_bit_stride
698 case DW_TAG_pointer_type:
699 case DW_TAG_reference_type:
700 case DW_TAG_rvalue_reference_type:
701 type->is_pointer_type = 1;
704 case DW_TAG_structure_type:
705 case DW_TAG_union_type:
706 case DW_TAG_class_type:
707 MC_dwarf_add_members(info, die, unit, type);
708 char *new_ns = ns == NULL ? xbt_strdup(type->name)
709 : bprintf("%s::%s", ns, name);
710 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
718 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
719 Dwarf_Die * unit, dw_frame_t frame,
722 dw_type_t type = MC_dwarf_die_to_type(info, die, unit, frame, ns);
724 char *key = bprintf("%" PRIx64, (uint64_t) type->id);
725 xbt_dict_set(info->types, key, type, NULL);
728 if (type->name && type->byte_size != 0) {
729 xbt_dict_set(info->full_types_by_name, type->name, type, NULL);
733 static int mc_anonymous_variable_index = 0;
735 static dw_variable_t MC_die_to_variable(mc_object_info_t info, Dwarf_Die * die,
736 Dwarf_Die * unit, dw_frame_t frame,
739 // Skip declarations:
740 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
743 // Skip compile time constants:
744 if (dwarf_hasattr(die, DW_AT_const_value))
747 Dwarf_Attribute attr_location;
748 if (dwarf_attr(die, DW_AT_location, &attr_location) == NULL) {
749 // No location: do not add it ?
753 dw_variable_t variable = xbt_new0(s_dw_variable_t, 1);
754 variable->dwarf_offset = dwarf_dieoffset(die);
755 variable->global = frame == NULL; // Can be override base on DW_AT_location
756 variable->object_info = info;
758 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
759 variable->name = xbt_strdup(name);
761 variable->type_origin = MC_dwarf_at_type(die);
763 int form = dwarf_whatform(&attr_location);
766 DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
768 case MC_DW_CLASS_EXPRLOC:
769 case MC_DW_CLASS_BLOCK:
770 // Location expression:
774 if (dwarf_getlocation(&attr_location, &expr, &len)) {
776 ("Could not read location expression in DW_AT_location of variable <%"
777 PRIx64 ">%s", (uint64_t) variable->dwarf_offset, variable->name);
780 if (len == 1 && expr[0].atom == DW_OP_addr) {
781 variable->global = 1;
782 uintptr_t offset = (uintptr_t) expr[0].number;
783 uintptr_t base = (uintptr_t) MC_object_base_address(info);
784 variable->address = (void *) (base + offset);
786 mc_dwarf_location_list_init_from_expression(&variable->locations, len,
792 case MC_DW_CLASS_LOCLISTPTR:
793 case MC_DW_CLASS_CONSTANT:
794 // Reference to location list:
795 mc_dwarf_location_list_init(&variable->locations, info, die,
799 xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location in <%"
800 PRIx64 ">%s", form, form, klass, klass,
801 (uint64_t) variable->dwarf_offset, variable->name);
804 // Handle start_scope:
805 if (dwarf_hasattr(die, DW_AT_start_scope)) {
806 Dwarf_Attribute attr;
807 dwarf_attr(die, DW_AT_start_scope, &attr);
808 int form = dwarf_whatform(&attr);
809 int klass = MC_dwarf_form_get_class(form);
811 case MC_DW_CLASS_CONSTANT:
814 variable->start_scope =
815 dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
818 case MC_DW_CLASS_RANGELISTPTR: // TODO
821 ("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
822 form, klass, name == NULL ? "?" : name);
826 if (ns && variable->global) {
827 char *old_name = variable->name;
828 variable->name = bprintf("%s::%s", ns, old_name);
831 // The current code needs a variable name,
832 // generate a fake one:
833 if (!variable->name) {
834 variable->name = bprintf("@anonymous#%i", mc_anonymous_variable_index++);
840 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
841 Dwarf_Die * unit, dw_frame_t frame,
844 dw_variable_t variable =
845 MC_die_to_variable(info, die, unit, frame, ns);
846 if (variable == NULL)
848 MC_dwarf_register_variable(info, frame, variable);
851 static void mc_frame_free_voipd(dw_frame_t * p)
857 static void MC_dwarf_handle_scope_die(mc_object_info_t info, Dwarf_Die * die,
858 Dwarf_Die * unit, dw_frame_t parent_frame,
861 // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
862 int tag = dwarf_tag(die);
863 mc_tag_class klass = MC_dwarf_tag_classify(tag);
865 // (Template) Subprogram declaration:
866 if (klass == mc_tag_subprogram
867 && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
870 if (klass == mc_tag_scope)
871 xbt_assert(parent_frame, "No parent scope for this scope");
873 dw_frame_t frame = xbt_new0(s_dw_frame_t, 1);
876 frame->id = dwarf_dieoffset(die);
877 frame->object_info = info;
879 if (klass == mc_tag_subprogram) {
880 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
882 ns ? bprintf("%s::%s", ns, name) : xbt_strdup(name);
885 frame->abstract_origin_id =
886 MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
888 // This is the base address for DWARF addresses.
889 // Relocated addresses are offset from this base address.
890 // See DWARF4 spec 7.5
891 void *base = MC_object_base_address(info);
893 // Variables are filled in the (recursive) call of MC_dwarf_handle_children:
895 xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
897 // TODO, support DW_AT_ranges
898 uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
899 frame->low_pc = low_pc ? ((char *) base) + low_pc : 0;
902 Dwarf_Attribute attr;
903 if (!dwarf_attr_integrate(die, DW_AT_high_pc, &attr)) {
904 xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
910 switch (MC_dwarf_form_get_class(dwarf_whatform(&attr))) {
912 // DW_AT_high_pc if an offset from the low_pc:
913 case MC_DW_CLASS_CONSTANT:
915 if (dwarf_formsdata(&attr, &offset) != 0)
916 xbt_die("Could not read constant");
917 frame->high_pc = (void *) ((char *) frame->low_pc + offset);
920 // DW_AT_high_pc is a relocatable address:
921 case MC_DW_CLASS_ADDRESS:
922 if (dwarf_formaddr(&attr, &high_pc) != 0)
923 xbt_die("Could not read address");
924 frame->high_pc = ((char *) base) + high_pc;
928 xbt_die("Unexpected class for DW_AT_high_pc");
933 if (klass == mc_tag_subprogram) {
934 Dwarf_Attribute attr_frame_base;
935 if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
936 mc_dwarf_location_list_init(&frame->frame_base, info, die,
941 xbt_dynar_new(sizeof(dw_frame_t), (void_f_pvoid_t) mc_frame_free_voipd);
944 if (klass == mc_tag_subprogram) {
945 char *key = bprintf("%" PRIx64, (uint64_t) frame->id);
946 xbt_dict_set(info->subprograms, key, frame, NULL);
948 } else if (klass == mc_tag_scope) {
949 xbt_dynar_push(parent_frame->scopes, &frame);
952 MC_dwarf_handle_children(info, die, unit, frame, ns);
955 static void mc_dwarf_handle_namespace_die(mc_object_info_t info,
956 Dwarf_Die * die, Dwarf_Die * unit,
960 const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
962 xbt_die("Unexpected namespace in a subprogram");
963 char *new_ns = ns == NULL ? xbt_strdup(name)
964 : bprintf("%s::%s", ns, name);
965 MC_dwarf_handle_children(info, die, unit, frame, new_ns);
969 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
970 Dwarf_Die * unit, dw_frame_t frame,
973 // For each child DIE:
976 for (res = dwarf_child(die, &child); res == 0;
977 res = dwarf_siblingof(&child, &child)) {
978 MC_dwarf_handle_die(info, &child, unit, frame, ns);
982 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
983 Dwarf_Die * unit, dw_frame_t frame,
986 int tag = dwarf_tag(die);
987 mc_tag_class klass = MC_dwarf_tag_classify(tag);
992 MC_dwarf_handle_type_die(info, die, unit, frame, ns);
995 // Subprogram or scope:
996 case mc_tag_subprogram:
998 MC_dwarf_handle_scope_die(info, die, unit, frame, ns);
1002 case mc_tag_variable:
1003 MC_dwarf_handle_variable_die(info, die, unit, frame, ns);
1006 case mc_tag_namespace:
1007 mc_dwarf_handle_namespace_die(info, die, unit, frame, ns);
1016 /** \brief Populate the debugging informations of the given ELF object
1018 * Read the DWARf information of the EFFL object and populate the
1019 * lists of types, variables, functions.
1021 void MC_dwarf_get_variables(mc_object_info_t info)
1023 int fd = open(info->file_name, O_RDONLY);
1025 xbt_die("Could not open file %s", info->file_name);
1027 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
1028 if (dwarf == NULL) {
1029 xbt_die("Your program must be compiled with -g (%s)", info->file_name);
1031 // For each compilation unit:
1032 Dwarf_Off offset = 0;
1033 Dwarf_Off next_offset = 0;
1035 while (dwarf_nextcu(dwarf, offset, &next_offset, &length, NULL, NULL, NULL) ==
1038 if (dwarf_offdie(dwarf, offset + length, &unit_die) != NULL) {
1040 // For each child DIE:
1043 for (res = dwarf_child(&unit_die, &child); res == 0;
1044 res = dwarf_siblingof(&child, &child)) {
1045 MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
1049 offset = next_offset;
1056 /************************** Free functions *************************/
1058 void mc_frame_free(dw_frame_t frame)
1060 xbt_free(frame->name);
1061 mc_dwarf_location_list_clear(&(frame->frame_base));
1062 xbt_dynar_free(&(frame->variables));
1063 xbt_dynar_free(&(frame->scopes));
1067 void dw_type_free(dw_type_t t)
1070 xbt_free(t->dw_type_id);
1071 xbt_dynar_free(&(t->members));
1072 mc_dwarf_expression_clear(&t->location);
1076 void dw_variable_free(dw_variable_t v)
1080 xbt_free(v->type_origin);
1082 if (v->locations.locations)
1083 mc_dwarf_location_list_clear(&v->locations);
1088 void dw_variable_free_voidp(void *t)
1090 dw_variable_free((dw_variable_t) * (void **) t);
1093 // ***** object_info
1097 mc_object_info_t MC_new_object_info(void)
1099 mc_object_info_t res = xbt_new0(s_mc_object_info_t, 1);
1100 res->subprograms = xbt_dict_new_homogeneous((void (*)(void *)) mc_frame_free);
1101 res->global_variables =
1102 xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
1103 res->types = xbt_dict_new_homogeneous((void (*)(void *)) dw_type_free);
1104 res->full_types_by_name = xbt_dict_new_homogeneous(NULL);
1108 void MC_free_object_info(mc_object_info_t * info)
1110 xbt_free(&(*info)->file_name);
1111 xbt_dict_free(&(*info)->subprograms);
1112 xbt_dynar_free(&(*info)->global_variables);
1113 xbt_dict_free(&(*info)->types);
1114 xbt_dict_free(&(*info)->full_types_by_name);
1116 xbt_dynar_free(&(*info)->functions_index);
1122 void *MC_object_base_address(mc_object_info_t info)
1124 if (info->flags & MC_OBJECT_INFO_EXECUTABLE)
1126 void *result = info->start_exec;
1127 if (info->start_rw != NULL && result > (void *) info->start_rw)
1128 result = info->start_rw;
1129 if (info->start_ro != NULL && result > (void *) info->start_ro)
1130 result = info->start_ro;
1134 // ***** Functions index
1136 static int MC_compare_frame_index_items(mc_function_index_item_t a,
1137 mc_function_index_item_t b)
1139 if (a->low_pc < b->low_pc)
1141 else if (a->low_pc == b->low_pc)
1147 static void MC_make_functions_index(mc_object_info_t info)
1149 xbt_dynar_t index = xbt_dynar_new(sizeof(s_mc_function_index_item_t), NULL);
1151 // Populate the array:
1152 dw_frame_t frame = NULL;
1153 xbt_dict_cursor_t cursor;
1155 xbt_dict_foreach(info->subprograms, cursor, key, frame) {
1156 if (frame->low_pc == NULL)
1158 s_mc_function_index_item_t entry;
1159 entry.low_pc = frame->low_pc;
1160 entry.high_pc = frame->high_pc;
1161 entry.function = frame;
1162 xbt_dynar_push(index, &entry);
1165 mc_function_index_item_t base =
1166 (mc_function_index_item_t) xbt_dynar_get_ptr(index, 0);
1168 // Sort the array by low_pc:
1170 xbt_dynar_length(index),
1171 sizeof(s_mc_function_index_item_t),
1172 (int (*)(const void *, const void *)) MC_compare_frame_index_items);
1174 info->functions_index = index;
1177 static void MC_post_process_variables(mc_object_info_t info)
1179 unsigned cursor = 0;
1180 dw_variable_t variable = NULL;
1181 xbt_dynar_foreach(info->global_variables, cursor, variable) {
1182 if (variable->type_origin) {
1183 variable->type = (dw_type_t) xbt_dict_get_or_null(info->types, variable->type_origin);
1188 static void mc_post_process_scope(mc_object_info_t info, dw_frame_t scope)
1191 if (scope->tag == DW_TAG_inlined_subroutine) {
1193 // Attach correct namespaced name in inlined subroutine:
1194 char *key = bprintf("%" PRIx64, (uint64_t) scope->abstract_origin_id);
1195 dw_frame_t abstract_origin = (dw_frame_t) xbt_dict_get_or_null(info->subprograms, key);
1196 xbt_assert(abstract_origin, "Could not lookup abstract origin %s", key);
1198 scope->name = xbt_strdup(abstract_origin->name);
1202 unsigned cursor = 0;
1203 dw_variable_t variable = NULL;
1204 xbt_dynar_foreach(scope->variables, cursor, variable) {
1205 if (variable->type_origin) {
1206 variable->type = (dw_type_t) xbt_dict_get_or_null(info->types, variable->type_origin);
1210 // Recursive post-processing of nested-scopes:
1211 dw_frame_t nested_scope = NULL;
1212 xbt_dynar_foreach(scope->scopes, cursor, nested_scope)
1213 mc_post_process_scope(info, nested_scope);
1217 static void MC_post_process_functions(mc_object_info_t info)
1219 xbt_dict_cursor_t cursor;
1221 dw_frame_t subprogram = NULL;
1222 xbt_dict_foreach(info->subprograms, cursor, key, subprogram) {
1223 mc_post_process_scope(info, subprogram);
1228 /** \brief Fill/lookup the "subtype" field.
1230 static void MC_resolve_subtype(mc_object_info_t info, dw_type_t type)
1233 if (type->dw_type_id == NULL)
1235 type->subtype = (dw_type_t) xbt_dict_get_or_null(info->types, type->dw_type_id);
1236 if (type->subtype == NULL)
1238 if (type->subtype->byte_size != 0)
1240 if (type->subtype->name == NULL)
1242 // Try to find a more complete description of the type:
1243 // We need to fix in order to support C++.
1246 (dw_type_t) xbt_dict_get_or_null(info->full_types_by_name, type->subtype->name);
1247 if (subtype != NULL) {
1248 type->subtype = subtype;
1253 static void MC_post_process_types(mc_object_info_t info)
1255 xbt_dict_cursor_t cursor = NULL;
1259 // Lookup "subtype" field:
1260 xbt_dict_foreach(info->types, cursor, origin, type) {
1261 MC_resolve_subtype(info, type);
1265 if (type->members != NULL)
1266 xbt_dynar_foreach(type->members, i, member) {
1267 MC_resolve_subtype(info, member);
1272 /** \brief Finds informations about a given shared object/executable */
1273 mc_object_info_t MC_find_object_info(memory_map_t maps, const char *name,
1276 mc_object_info_t result = MC_new_object_info();
1278 result->flags |= MC_OBJECT_INFO_EXECUTABLE;
1279 result->file_name = xbt_strdup(name);
1280 MC_find_object_address(maps, result);
1281 MC_dwarf_get_variables(result);
1282 MC_post_process_types(result);
1283 MC_post_process_variables(result);
1284 MC_post_process_functions(result);
1285 MC_make_functions_index(result);
1289 /*************************************************************************/
1291 static int MC_dwarf_get_variable_index(xbt_dynar_t variables, char *var,
1295 if (xbt_dynar_is_empty(variables))
1298 unsigned int cursor = 0;
1300 int end = xbt_dynar_length(variables) - 1;
1301 dw_variable_t var_test = NULL;
1303 while (start <= end) {
1304 cursor = (start + end) / 2;
1306 (dw_variable_t) xbt_dynar_get_as(variables, cursor, dw_variable_t);
1307 if (strcmp(var_test->name, var) < 0) {
1309 } else if (strcmp(var_test->name, var) > 0) {
1312 if (address) { /* global variable */
1313 if (var_test->address == address)
1315 if (var_test->address > address)
1319 } else { /* local variable */
1325 if (strcmp(var_test->name, var) == 0) {
1326 if (address && var_test->address < address)
1330 } else if (strcmp(var_test->name, var) < 0)
1337 void MC_dwarf_register_global_variable(mc_object_info_t info,
1338 dw_variable_t variable)
1341 MC_dwarf_get_variable_index(info->global_variables, variable->name,
1344 xbt_dynar_insert_at(info->global_variables, index, &variable);
1348 void MC_dwarf_register_non_global_variable(mc_object_info_t info,
1350 dw_variable_t variable)
1352 xbt_assert(frame, "Frame is NULL");
1354 MC_dwarf_get_variable_index(frame->variables, variable->name, NULL);
1356 xbt_dynar_insert_at(frame->variables, index, &variable);
1360 void MC_dwarf_register_variable(mc_object_info_t info, dw_frame_t frame,
1361 dw_variable_t variable)
1363 if (variable->global)
1364 MC_dwarf_register_global_variable(info, variable);
1365 else if (frame == NULL)
1366 xbt_die("No frame for this local variable");
1368 MC_dwarf_register_non_global_variable(info, frame, variable);
1371 void MC_post_process_object_info(mc_process_t process, mc_object_info_t info)
1373 xbt_dict_cursor_t cursor = NULL;
1375 dw_type_t type = NULL;
1376 xbt_dict_foreach(info->types, cursor, key, type) {
1378 dw_type_t subtype = type;
1379 while (subtype->type == DW_TAG_typedef || subtype->type == DW_TAG_volatile_type
1380 || subtype->type == DW_TAG_const_type) {
1381 if (subtype->subtype)
1382 subtype = subtype->subtype;
1387 // Resolve full_type:
1388 if (subtype->name && subtype->byte_size == 0) {
1389 for (size_t i = 0; i != process->object_infos_size; ++i) {
1390 dw_type_t same_type = (dw_type_t)
1391 xbt_dict_get_or_null(process->object_infos[i]->full_types_by_name,
1393 if (same_type && same_type->name && same_type->byte_size) {
1394 type->full_type = same_type;
1398 } else type->full_type = subtype;