1 /* Copyright (c) 2008-2013. The SimGrid Team.
2 * All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include <elfutils/libdw.h>
11 #include <simgrid_config.h>
13 #include <xbt/sysdep.h>
15 #include "mc_private.h"
17 // Define symbols not present in old version of dwarf.h.
18 // Old version of dwarf.h, use DW_LANG_Objc instead of this:
19 #define DW_LANG_ObjC 0x0010
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing");
23 /** \brief The default DW_TAG_lower_bound for a given DW_AT_language.
25 * The default for a given language is defined in the DWARF spec.
27 * \param language consant as defined by the DWARf spec
29 static uint64_t MC_dwarf_default_lower_bound(int lang);
31 /** \brief Computes the the element_count of a DW_TAG_enumeration_type DIE
33 * This is the number of elements in a given array dimension.
35 * A reference of the compilation unit (DW_TAG_compile_unit) is
36 * needed because the default lower bound (when there is no DW_AT_lower_bound)
37 * depends of the language of the compilation unit (DW_AT_language).
39 * \param die DIE for the DW_TAG_enumeration_type or DW_TAG_subrange_type
40 * \param unit DIE of the DW_TAG_compile_unit
42 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit);
44 /** \brief Computes the number of elements of a given DW_TAG_array_type.
46 * \param die DIE for the DW_TAG_array_type
48 static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit);
50 /** \brief Process a DIE
52 * \param info the resulting object fot the library/binary file (output)
53 * \param die the current DIE
54 * \param unit the DIE of the compile unit of the current DIE
55 * \param frame containg frame if any
57 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace);
59 /** \brief Process a type DIE
61 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace);
63 /** \brief Calls MC_dwarf_handle_die on all childrend of the given die
65 * \param info the resulting object fot the library/binary file (output)
66 * \param die the current DIE
67 * \param unit the DIE of the compile unit of the current DIE
68 * \param frame containg frame if any
70 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace);
72 /** \brief Handle a variable (DW_TAG_variable or other)
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_variable_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace);
81 /** \brief Get the DW_TAG_type of the DIE
84 * \return DW_TAG_type attribute as a new string (NULL if none)
86 static char* MC_dwarf_at_type(Dwarf_Die* die);
88 /** \brief Get the name of an attribute (DW_AT_*) from its code
90 * \param attr attribute code (see the DWARF specification)
91 * \return name of the attribute
93 const char* MC_dwarf_attrname(int attr) {
95 #include "mc_dwarf_attrnames.h"
97 return "DW_AT_unknown";
101 /** \brief Get the name of a dwarf tag (DW_TAG_*) from its code
103 * \param tag tag code (see the DWARF specification)
104 * \return name of the tag
106 const char* MC_dwarf_tagname(int tag) {
108 #include "mc_dwarf_tagnames.h"
110 return "DW_TAG_invalid";
112 return "DW_TAG_unknown";
116 /** \brief A class of DWARF tags (DW_TAG_*)
118 typedef enum mc_tag_class {
127 static mc_tag_class MC_dwarf_tag_classify(int tag) {
130 case DW_TAG_array_type:
131 case DW_TAG_class_type:
132 case DW_TAG_enumeration_type:
134 case DW_TAG_pointer_type:
135 case DW_TAG_reference_type:
136 case DW_TAG_rvalue_reference_type:
137 case DW_TAG_string_type:
138 case DW_TAG_structure_type:
139 case DW_TAG_subroutine_type:
140 case DW_TAG_union_type:
141 case DW_TAG_ptr_to_member_type:
142 case DW_TAG_set_type:
143 case DW_TAG_subrange_type:
144 case DW_TAG_base_type:
145 case DW_TAG_const_type:
146 case DW_TAG_file_type:
147 case DW_TAG_packed_type:
148 case DW_TAG_volatile_type:
149 case DW_TAG_restrict_type:
150 case DW_TAG_interface_type:
151 case DW_TAG_unspecified_type:
152 case DW_TAG_mutable_type:
153 case DW_TAG_shared_type:
156 case DW_TAG_subprogram:
157 return mc_tag_subprogram;
159 case DW_TAG_variable:
160 case DW_TAG_formal_parameter:
161 return mc_tag_variable;
163 case DW_TAG_lexical_block:
164 case DW_TAG_try_block:
165 case DW_TAG_catch_block:
166 case DW_TAG_inlined_subroutine:
167 case DW_TAG_with_stmt:
170 case DW_TAG_namespace:
171 return mc_tag_namespace;
174 return mc_tag_unknown;
179 #define MC_DW_CLASS_UNKNOWN 0
180 #define MC_DW_CLASS_ADDRESS 1 // Location in the address space of the program
181 #define MC_DW_CLASS_BLOCK 2 // Arbitrary block of bytes
182 #define MC_DW_CLASS_CONSTANT 3
183 #define MC_DW_CLASS_STRING 3 // String
184 #define MC_DW_CLASS_FLAG 4 // Boolean
185 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
186 #define MC_DW_CLASS_EXPRLOC 6 // DWARF expression/location description
187 #define MC_DW_CLASS_LINEPTR 7
188 #define MC_DW_CLASS_LOCLISTPTR 8
189 #define MC_DW_CLASS_MACPTR 9
190 #define MC_DW_CLASS_RANGELISTPTR 10
192 /** \brief Find the DWARF data class for a given DWARF data form
194 * This mapping is defined in the DWARF spec.
196 * \param form The form (values taken from the DWARF spec)
197 * \return An internal representation for the corresponding class
199 static int MC_dwarf_form_get_class(int form) {
202 return MC_DW_CLASS_ADDRESS;
207 return MC_DW_CLASS_BLOCK;
214 return MC_DW_CLASS_CONSTANT;
217 return MC_DW_CLASS_STRING;
218 case DW_FORM_ref_addr:
223 case DW_FORM_ref_udata:
224 return MC_DW_CLASS_REFERENCE;
226 case DW_FORM_flag_present:
227 return MC_DW_CLASS_FLAG;
228 case DW_FORM_exprloc:
229 return MC_DW_CLASS_EXPRLOC;
233 return MC_DW_CLASS_UNKNOWN;
237 /** \brief Get the name of the tag of a given DIE
240 * \return name of the tag of this DIE
242 static inline const char* MC_dwarf_die_tagname(Dwarf_Die* die) {
243 return MC_dwarf_tagname(dwarf_tag(die));
248 /** \brief Get an attribute of a given DIE as a string
251 * \param attribute attribute
252 * \return value of the given attribute of the given DIE
254 static const char* MC_dwarf_attr_integrate_string(Dwarf_Die* die, int attribute) {
255 Dwarf_Attribute attr;
256 if (!dwarf_attr_integrate(die, attribute, &attr)) {
259 return dwarf_formstring(&attr);
263 /** \brief Get the linkage name of a DIE.
265 * Use either DW_AT_linkage_name or DW_AT_MIPS_linkage_name.
266 * DW_AT_linkage_name is standardized since DWARF 4.
267 * Before this version of DWARF, the MIPS extensions
268 * DW_AT_MIPS_linkage_name is used (at least by GCC).
271 * \return linkage name of the given DIE (or NULL)
273 static const char* MC_dwarf_at_linkage_name(Dwarf_Die* die) {
274 const char* name = MC_dwarf_attr_integrate_string(die, DW_AT_linkage_name);
276 name = MC_dwarf_attr_integrate_string(die, DW_AT_MIPS_linkage_name);
280 static Dwarf_Off MC_dwarf_attr_dieoffset(Dwarf_Die* die, int attribute) {
281 Dwarf_Attribute attr;
282 if (dwarf_hasattr_integrate(die, attribute)) {
283 dwarf_attr_integrate(die, attribute, &attr);
284 Dwarf_Die subtype_die;
285 if (dwarf_formref_die(&attr, &subtype_die)==NULL) {
286 xbt_die("Could not find DIE");
288 return dwarf_dieoffset(&subtype_die);
293 static Dwarf_Off MC_dwarf_attr_integrate_dieoffset(Dwarf_Die* die, int attribute) {
294 Dwarf_Attribute attr;
295 if (dwarf_hasattr_integrate(die, attribute)) {
296 dwarf_attr_integrate(die, DW_AT_type, &attr);
297 Dwarf_Die subtype_die;
298 if (dwarf_formref_die(&attr, &subtype_die)==NULL) {
299 xbt_die("Could not find DIE");
301 return dwarf_dieoffset(&subtype_die);
306 /** \brief Find the type/subtype (DW_AT_type) for a DIE
309 * \return DW_AT_type reference as a global offset in hexadecimal (or NULL)
311 static char* MC_dwarf_at_type(Dwarf_Die* die) {
312 Dwarf_Off offset = MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
313 return offset == 0 ? NULL : bprintf("%" PRIx64 , offset);
316 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die* die, int attribute) {
317 Dwarf_Attribute attr;
318 if(dwarf_attr_integrate(die, attribute, &attr)==NULL)
321 if (dwarf_formaddr(&attr, &value) == 0)
322 return (uint64_t) value;
327 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die* die, int attribute, uint64_t default_value) {
328 Dwarf_Attribute attr;
329 if (dwarf_attr_integrate(die, attribute, &attr)==NULL)
330 return default_value;
332 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr), &value) == 0 ? (uint64_t) value : default_value;
335 static bool MC_dwarf_attr_flag(Dwarf_Die* die, int attribute, bool integrate) {
336 Dwarf_Attribute attr;
337 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
338 : dwarf_attr(die, attribute, &attr))==0)
342 if (dwarf_formflag(&attr, &result))
343 xbt_die("Unexpected form for attribute %s",
344 MC_dwarf_attrname(attribute));
348 /** \brief Find the default lower bound for a given language
350 * The default lower bound of an array (when DW_TAG_lower_bound
351 * is missing) depends on the language of the compilation unit.
353 * \param lang Language of the compilation unit (values defined in the DWARF spec)
354 * \return Default lower bound of an array in this compilation unit
356 static uint64_t MC_dwarf_default_lower_bound(int lang) {
361 case DW_LANG_C_plus_plus:
365 case DW_LANG_ObjC_plus_plus:
371 case DW_LANG_Fortran77:
372 case DW_LANG_Fortran90:
373 case DW_LANG_Fortran95:
374 case DW_LANG_Modula2:
375 case DW_LANG_Pascal83:
377 case DW_LANG_Cobol74:
378 case DW_LANG_Cobol85:
381 xbt_die("No default DW_TAG_lower_bound for language %i and none given", lang);
386 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
389 * \param unit DIE of the compilation unit
390 * \return number of elements in the range
392 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
393 xbt_assert(dwarf_tag(die)==DW_TAG_enumeration_type ||dwarf_tag(die)==DW_TAG_subrange_type,
394 "MC_dwarf_subrange_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
396 // Use DW_TAG_count if present:
397 if (dwarf_hasattr_integrate(die, DW_AT_count)) {
398 return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
401 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
403 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
404 // This is not really 0, but the code expects this (we do not know):
407 uint64_t upper_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
409 uint64_t lower_bound = 0;
410 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
411 lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
413 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
415 return upper_bound - lower_bound + 1;
418 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
420 * The compilation unit might be needed because the default lower
421 * bound depends on the language of the compilation unit.
423 * \param die the DIE of the DW_TAG_array_type
424 * \param unit the DIE of the compilation unit
425 * \return number of elements in this array type
427 static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
428 xbt_assert(dwarf_tag(die)==DW_TAG_array_type,
429 "MC_dwarf_array_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
434 for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
435 int child_tag = dwarf_tag(&child);
436 if (child_tag==DW_TAG_subrange_type ||child_tag==DW_TAG_enumeration_type) {
437 result *= MC_dwarf_subrange_element_count(&child, unit);
445 /** \brief Initialize the location of a member of a type
446 * (DW_AT_data_member_location of a DW_TAG_member).
448 * \param type a type (struct, class)
449 * \param member the member of the type
450 * \param child DIE of the member (DW_TAG_member)
452 static void MC_dwarf_fill_member_location(dw_type_t type, dw_type_t member, Dwarf_Die* child) {
453 if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
454 xbt_die("Can't groke DW_AT_data_bit_offset.");
457 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
458 if (type->type != DW_TAG_union_type) {
460 "Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%p>%s",
461 member->name, type->id, type->name);
467 Dwarf_Attribute attr;
468 dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
469 int form = dwarf_whatform(&attr);
470 int klass = MC_dwarf_form_get_class(form);
472 case MC_DW_CLASS_EXPRLOC:
473 case MC_DW_CLASS_BLOCK:
474 // Location expression:
478 if (dwarf_getlocation(&attr, &expr, &len)) {
480 "Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%p>%s",
481 MC_dwarf_attr_integrate_string(child, DW_AT_name),
482 type->id, type->name);
484 if (len==1 && expr[0].atom == DW_OP_plus_uconst) {
485 member->offset = expr[0].number;
487 mc_dwarf_expression_init(&member->location, len, expr);
491 case MC_DW_CLASS_CONSTANT:
492 // Offset from the base address of the object:
495 if (!dwarf_formudata(&attr, &offset))
496 member->offset = offset;
498 xbt_die("Cannot get %s location <%p>%s",
499 MC_dwarf_attr_integrate_string(child, DW_AT_name),
500 type->id, type->name);
503 case MC_DW_CLASS_LOCLISTPTR:
504 // Reference to a location list:
506 case MC_DW_CLASS_REFERENCE:
507 // It's supposed to be possible in DWARF2 but I couldn't find its semantic
511 "Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
517 static void dw_type_free_voidp(void *t){
518 dw_type_free((dw_type_t) * (void **) t);
521 /** \brief Populate the list of members of a type
523 * \param info ELF object containing the type DIE
524 * \param die DIE of the type
525 * \param unit DIE of the compilation unit containing the type DIE
526 * \param type the type
528 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_type_t type) {
531 xbt_assert(!type->members);
532 type->members = xbt_dynar_new(sizeof(dw_type_t), (void(*)(void*))dw_type_free_voidp);
533 for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
534 int tag = dwarf_tag(&child);
535 if (tag==DW_TAG_member || tag==DW_TAG_inheritance) {
537 // Skip declarations:
538 if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
541 // Skip compile time constants:
542 if(dwarf_hasattr(&child, DW_AT_const_value))
545 // TODO, we should use another type (because is is not a type but a member)
546 dw_type_t member = xbt_new0(s_dw_type_t, 1);
550 member->id = (void *) dwarf_dieoffset(&child);
552 const char* name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
554 member->name = xbt_strdup(name);
558 member->byte_size = MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
559 member->element_count = -1;
560 member->dw_type_id = MC_dwarf_at_type(&child);
561 member->members = NULL;
562 member->is_pointer_type = 0;
565 if(dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
566 xbt_die("Can't groke DW_AT_data_bit_offset.");
569 MC_dwarf_fill_member_location(type, member, &child);
571 if (!member->dw_type_id) {
572 xbt_die("Missing type for member %s of <%p>%s", member->name, type->id, type->name);
575 xbt_dynar_push(type->members, &member);
580 /** \brief Create a MC type object from a DIE
582 * \param info current object info object
583 * \param DIE (for a given type);
584 * \param unit compilation unit of the current DIE
585 * \return MC representation of the type
587 static dw_type_t MC_dwarf_die_to_type(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
589 dw_type_t type = xbt_new0(s_dw_type_t, 1);
594 type->element_count = -1;
595 type->dw_type_id = NULL;
596 type->members = NULL;
597 type->is_pointer_type = 0;
600 type->type = dwarf_tag(die);
603 type->id = (void *) dwarf_dieoffset(die);
605 const char* prefix = "";
606 switch (type->type) {
607 case DW_TAG_structure_type:
610 case DW_TAG_union_type:
613 case DW_TAG_class_type:
620 const char* name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
622 type->name = namespace ? bprintf("%s%s::%s", prefix, namespace, name) : bprintf("%s%s", prefix, name);
625 XBT_DEBUG("Processing type <%p>%s", type->id, type->name);
627 type->dw_type_id = MC_dwarf_at_type(die);
629 // Computation of the byte_size;
630 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
631 type->byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
632 else if (type->type == DW_TAG_array_type || type->type==DW_TAG_structure_type || type->type==DW_TAG_class_type) {
634 if (dwarf_aggregate_size(die, &size)==0) {
635 type->byte_size = size;
639 switch (type->type) {
640 case DW_TAG_array_type:
641 type->element_count = MC_dwarf_array_element_count(die, unit);
642 // TODO, handle DW_byte_stride and (not) DW_bit_stride
645 case DW_TAG_pointer_type:
646 case DW_TAG_reference_type:
647 case DW_TAG_rvalue_reference_type:
648 type->is_pointer_type = 1;
651 case DW_TAG_structure_type:
652 case DW_TAG_union_type:
653 case DW_TAG_class_type:
654 MC_dwarf_add_members(info, die, unit, type);
655 char* new_namespace = namespace == NULL ? xbt_strdup(type->name)
656 : bprintf("%s::%s", namespace, name);
657 MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
665 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
666 dw_type_t type = MC_dwarf_die_to_type(info, die, unit, frame, namespace);
668 char* key = bprintf("%" PRIx64, (uint64_t) type->id);
669 xbt_dict_set(info->types, key, type, NULL);
672 if(type->name && type->byte_size!=0) {
673 xbt_dict_set(info->full_types_by_name, type->name, type, NULL);
677 static int mc_anonymous_variable_index = 0;
679 static dw_variable_t MC_die_to_variable(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
680 // Skip declarations:
681 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
684 // Skip compile time constants:
685 if(dwarf_hasattr(die, DW_AT_const_value))
688 Dwarf_Attribute attr_location;
689 if (dwarf_attr(die, DW_AT_location, &attr_location)==NULL) {
690 // No location: do not add it ?
694 dw_variable_t variable = xbt_new0(s_dw_variable_t, 1);
695 variable->dwarf_offset = dwarf_dieoffset(die);
696 variable->global = frame == NULL; // Can be override base on DW_AT_location
697 variable->object_info = info;
699 const char* name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
700 variable->name = xbt_strdup(name);
702 variable->type_origin = MC_dwarf_at_type(die);
704 int form = dwarf_whatform(&attr_location);
705 int klass = form == DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
707 case MC_DW_CLASS_EXPRLOC:
708 case MC_DW_CLASS_BLOCK:
709 // Location expression:
713 if (dwarf_getlocation(&attr_location, &expr, &len)) {
715 "Could not read location expression in DW_AT_location of variable <%p>%s",
716 (void*) variable->dwarf_offset, variable->name);
719 if (len==1 && expr[0].atom == DW_OP_addr) {
720 variable->global = 1;
721 Dwarf_Off offset = expr[0].number;
722 Dwarf_Off base = (Dwarf_Off) MC_object_base_address(info);
723 variable->address = (void*) (base + offset);
725 mc_dwarf_location_list_init_from_expression(&variable->locations, len, expr);
730 case MC_DW_CLASS_LOCLISTPTR:
731 case MC_DW_CLASS_CONSTANT:
732 // Reference to location list:
733 mc_dwarf_location_list_init(&variable->locations, info, die, &attr_location);
736 xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location in <%p>%s",
737 form, form, klass, klass, (void*) variable->dwarf_offset, variable->name);
740 // Handle start_scope:
741 if (dwarf_hasattr(die, DW_AT_start_scope)) {
742 Dwarf_Attribute attr;
743 dwarf_attr(die, DW_AT_start_scope, &attr);
744 int form = dwarf_whatform(&attr);
745 int klass = MC_dwarf_form_get_class(form);
747 case MC_DW_CLASS_CONSTANT:
750 variable->start_scope = dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
753 case MC_DW_CLASS_RANGELISTPTR: // TODO
755 xbt_die("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
756 form, klass, name==NULL ? "?" : name);
760 if(namespace && variable->global) {
761 char* old_name = variable->name;
762 variable->name = bprintf("%s::%s", namespace, old_name);
766 // The current code needs a variable name,
767 // generate a fake one:
768 if(!variable->name) {
769 variable->name = bprintf("@anonymous#%i", mc_anonymous_variable_index++);
775 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
776 dw_variable_t variable = MC_die_to_variable(info, die, unit, frame, namespace);
779 MC_dwarf_register_variable(info, frame, variable);
782 static void mc_frame_free_voipd(dw_frame_t* p) {
787 static void MC_dwarf_handle_scope_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t parent_frame, const char* namespace) {
788 // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
789 int tag = dwarf_tag(die);
790 mc_tag_class klass = MC_dwarf_tag_classify(tag);
792 // (Template) Subprogram declaration:
793 if(klass==mc_tag_subprogram && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
796 if(klass==mc_tag_scope)
797 xbt_assert(parent_frame, "No parent scope for this scope");
799 dw_frame_t frame = xbt_new0(s_dw_frame_t, 1);
802 frame->id = dwarf_dieoffset(die);
803 frame->object_info = info;
805 if(klass==mc_tag_subprogram) {
806 const char* name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
807 frame->name = namespace ? bprintf("%s::%s", namespace, name) : xbt_strdup(name);
810 frame->abstract_origin_id = MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
812 // This is the base address for DWARF addresses.
813 // Relocated addresses are offset from this base address.
814 // See DWARF4 spec 7.5
815 void* base = MC_object_base_address(info);
817 // Variables are filled in the (recursive) call of MC_dwarf_handle_children:
818 frame->variables = xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
820 // TODO, support DW_AT_ranges
821 uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
822 frame->low_pc = low_pc ? ((char*) base) + low_pc : 0;
825 Dwarf_Attribute attr;
826 if(!dwarf_attr_integrate(die, DW_AT_high_pc, &attr)) {
827 xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
833 switch(MC_dwarf_form_get_class(dwarf_whatform(&attr))) {
835 // DW_AT_high_pc if an offset from the low_pc:
836 case MC_DW_CLASS_CONSTANT:
838 if (dwarf_formsdata(&attr, &offset) !=0)
839 xbt_die("Could not read constant");
840 frame->high_pc = (void*) ((Dwarf_Off)frame->low_pc + offset);
843 // DW_AT_high_pc is a relocatable address:
844 case MC_DW_CLASS_ADDRESS:
845 if (dwarf_formaddr(&attr, &high_pc) != 0)
846 xbt_die("Could not read address");
847 frame->high_pc = ((char*) base) + high_pc;
851 xbt_die("Unexpected class for DW_AT_high_pc");
856 if(klass==mc_tag_subprogram) {
857 Dwarf_Attribute attr_frame_base;
858 if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
859 mc_dwarf_location_list_init(&frame->frame_base, info, die, &attr_frame_base);
862 frame->scopes = xbt_dynar_new(sizeof(dw_frame_t), (void_f_pvoid_t) mc_frame_free_voipd);
865 if(klass==mc_tag_subprogram) {
866 char* key = bprintf("%" PRIx64, (uint64_t) frame->id);
867 xbt_dict_set(info->subprograms, key, frame, NULL);
869 } else if(klass==mc_tag_scope) {
870 xbt_dynar_push(parent_frame->scopes, &frame);
874 MC_dwarf_handle_children(info, die, unit, frame, namespace);
877 static void mc_dwarf_handle_namespace_die(
878 mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
879 const char* name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
881 xbt_die("Unexpected namespace in a subprogram");
882 char* new_namespace = namespace == NULL ? xbt_strdup(name)
883 : bprintf("%s::%s", namespace, name);
884 MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
885 xbt_free(new_namespace);
888 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
889 // For each child DIE:
892 for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
893 MC_dwarf_handle_die(info, &child, unit, frame, namespace);
897 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
898 int tag = dwarf_tag(die);
899 mc_tag_class klass = MC_dwarf_tag_classify(tag);
904 MC_dwarf_handle_type_die(info, die, unit, frame, namespace);
907 // Subprogram or scope:
908 case mc_tag_subprogram:
910 MC_dwarf_handle_scope_die(info, die, unit, frame, namespace);
914 case mc_tag_variable:
915 MC_dwarf_handle_variable_die(info, die, unit, frame, namespace);
918 case mc_tag_namespace:
919 mc_dwarf_handle_namespace_die(info, die, unit, frame, namespace);
928 /** \brief Populate the debugging informations of the given ELF object
930 * Read the DWARf information of the EFFL object and populate the
931 * lists of types, variables, functions.
933 void MC_dwarf_get_variables(mc_object_info_t info) {
934 int fd = open(info->file_name, O_RDONLY);
936 xbt_die("Could not open file %s", info->file_name);
938 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
940 xbt_die("Your program must be compiled with -g");
943 // For each compilation unit:
944 Dwarf_Off offset = 0;
945 Dwarf_Off next_offset = 0;
947 while (dwarf_nextcu (dwarf, offset, &next_offset, &length, NULL, NULL, NULL) == 0) {
949 if(dwarf_offdie(dwarf, offset+length, &unit_die)!=NULL) {
951 // For each child DIE:
954 for (res=dwarf_child(&unit_die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
955 MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
959 offset = next_offset;