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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing");
19 /** \brief The default DW_TAG_lower_bound for a given DW_AT_language.
21 * The default for a given language is defined in the DWARF spec.
23 * \param language consant as defined by the DWARf spec
25 static uint64_t MC_dwarf_default_lower_bound(int lang);
27 /** \brief Computes the the element_count of a DW_TAG_enumeration_type DIE
29 * This is the number of elements in a given array dimension.
31 * A reference of the compilation unit (DW_TAG_compile_unit) is
32 * needed because the default lower bound (when there is no DW_AT_lower_bound)
33 * depends of the language of the compilation unit (DW_AT_language).
35 * \param die DIE for the DW_TAG_enumeration_type or DW_TAG_subrange_type
36 * \param unit DIE of the DW_TAG_compile_unit
38 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit);
40 /** \brief Computes the number of elements of a given DW_TAG_array_type.
42 * \param die DIE for the DW_TAG_array_type
44 static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit);
46 /** \brief Process a DIE
48 * \param info the resulting object fot the library/binary file (output)
49 * \param die the current DIE
50 * \param unit the DIE of the compile unit of the current DIE
51 * \param frame containg frame if any
53 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace);
55 /** \brief Process a type DIE
57 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);
59 /** \brief Calls MC_dwarf_handle_die on all childrend of the given die
61 * \param info the resulting object fot the library/binary file (output)
62 * \param die the current DIE
63 * \param unit the DIE of the compile unit of the current DIE
64 * \param frame containg frame if any
66 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace);
68 /** \brief Handle a variable (DW_TAG_variable or other)
70 * \param info the resulting object fot the library/binary file (output)
71 * \param die the current DIE
72 * \param unit the DIE of the compile unit of the current DIE
73 * \param frame containg frame if any
75 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);
77 /** \brief Convert a libdw DWARF expression into a MC representation of the location
79 * \param expr array of DWARf operations
80 * \param len number of elements
81 * \return a new MC expression
83 static dw_location_t MC_dwarf_get_expression(Dwarf_Op* expr, size_t len);
85 /** \brief Get the DW_TAG_type of the DIE
88 * \return DW_TAG_type attribute as a new string (NULL if none)
90 static char* MC_dwarf_at_type(Dwarf_Die* die);
92 /** \brief Get the name of an attribute (DW_AT_*) from its code
94 * \param attr attribute code (see the DWARF specification)
95 * \return name of the attribute
97 const char* MC_dwarf_attrname(int attr) {
99 #include "mc_dwarf_attrnames.h"
101 return "DW_AT_unknown";
105 /** \brief Get the name of a dwarf tag (DW_TAG_*) from its code
107 * \param tag tag code (see the DWARF specification)
108 * \return name of the tag
110 const char* MC_dwarf_tagname(int tag) {
112 #include "mc_dwarf_tagnames.h"
114 return "DW_TAG_invalid";
116 return "DW_TAG_unknown";
120 /** \brief A class of DWARF tags (DW_TAG_*)
122 typedef enum mc_tag_class {
131 static mc_tag_class MC_dwarf_tag_classify(int tag) {
134 case DW_TAG_array_type:
135 case DW_TAG_class_type:
136 case DW_TAG_enumeration_type:
138 case DW_TAG_pointer_type:
139 case DW_TAG_reference_type:
140 case DW_TAG_rvalue_reference_type:
141 case DW_TAG_string_type:
142 case DW_TAG_structure_type:
143 case DW_TAG_subroutine_type:
144 case DW_TAG_union_type:
145 case DW_TAG_ptr_to_member_type:
146 case DW_TAG_set_type:
147 case DW_TAG_subrange_type:
148 case DW_TAG_base_type:
149 case DW_TAG_const_type:
150 case DW_TAG_file_type:
151 case DW_TAG_packed_type:
152 case DW_TAG_volatile_type:
153 case DW_TAG_restrict_type:
154 case DW_TAG_interface_type:
155 case DW_TAG_unspecified_type:
156 case DW_TAG_mutable_type:
157 case DW_TAG_shared_type:
160 case DW_TAG_subprogram:
161 return mc_tag_subprogram;
163 case DW_TAG_variable:
164 case DW_TAG_formal_parameter:
165 return mc_tag_variable;
167 case DW_TAG_lexical_block:
168 case DW_TAG_try_block:
169 case DW_TAG_inlined_subroutine:
172 case DW_TAG_namespace:
173 return mc_tag_namespace;
176 return mc_tag_unknown;
181 #define MC_DW_CLASS_UNKNOWN 0
182 #define MC_DW_CLASS_ADDRESS 1 // Location in the address space of the program
183 #define MC_DW_CLASS_BLOCK 2 // Arbitrary block of bytes
184 #define MC_DW_CLASS_CONSTANT 3
185 #define MC_DW_CLASS_STRING 3 // String
186 #define MC_DW_CLASS_FLAG 4 // Boolean
187 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
188 #define MC_DW_CLASS_EXPRLOC 6 // DWARF expression/location description
189 #define MC_DW_CLASS_LINEPTR 7
190 #define MC_DW_CLASS_LOCLISTPTR 8
191 #define MC_DW_CLASS_MACPTR 9
192 #define MC_DW_CLASS_RANGELISTPTR 10
194 static int MC_dwarf_form_get_class(int form) {
197 return MC_DW_CLASS_ADDRESS;
202 return MC_DW_CLASS_BLOCK;
209 return MC_DW_CLASS_CONSTANT;
212 return MC_DW_CLASS_STRING;
213 case DW_FORM_ref_addr:
218 case DW_FORM_ref_udata:
219 return MC_DW_CLASS_REFERENCE;
221 case DW_FORM_flag_present:
222 return MC_DW_CLASS_FLAG;
223 case DW_FORM_exprloc:
224 return MC_DW_CLASS_EXPRLOC;
228 return MC_DW_CLASS_UNKNOWN;
232 /** \brief Get the name of the tag of a given DIE
235 * \return name of the tag of this DIE
237 static inline const char* MC_dwarf_die_tagname(Dwarf_Die* die) {
238 return MC_dwarf_tagname(dwarf_tag(die));
243 /** \brief Get an attribute of a given DIE as a string
246 * \param attribute attribute
247 * \return value of the given attribute of the given DIE
249 static const char* MC_dwarf_attr_string(Dwarf_Die* die, int attribute) {
250 Dwarf_Attribute attr;
251 if (!dwarf_attr_integrate(die, attribute, &attr)) {
254 return dwarf_formstring(&attr);
258 /** \brief Get the linkage name of a DIE.
260 * Use either DW_AT_linkage_name or DW_AR_MIPS_linkage_name.
263 * \return linkage name of the given DIE (or NULL)
265 static const char* MC_dwarf_at_linkage_name(Dwarf_Die* die) {
266 const char* name = MC_dwarf_attr_string(die, DW_AT_linkage_name);
268 name = MC_dwarf_attr_string(die, DW_AT_MIPS_linkage_name);
272 /** \brief Create a location list from a given attribute
275 * \param attr the attribute
276 * \return MC specific representation of the location list represented by the given attribute
279 static dw_location_t MC_dwarf_get_location_list(mc_object_info_t info, Dwarf_Die* die, Dwarf_Attribute* attr) {
281 dw_location_t location = xbt_new0(s_dw_location_t, 1);
282 location->type = e_dw_loclist;
283 xbt_dynar_t loclist = xbt_dynar_new(sizeof(dw_location_entry_t), NULL);
284 location->location.loclist = loclist;
286 ptrdiff_t offset = 0;
287 Dwarf_Addr base, start, end;
293 offset = dwarf_getlocations(attr, offset, &base, &start, &end, &expr, &len);
297 xbt_die("Error while loading location list");
299 dw_location_entry_t new_entry = xbt_new0(s_dw_location_entry_t, 1);
301 void* base = info->flags & MC_OBJECT_INFO_EXECUTABLE ? 0 : MC_object_base_address(info);
303 new_entry->lowpc = (char*) base + start;
304 new_entry->highpc = (char*) base + end;
305 new_entry->location = MC_dwarf_get_expression(expr, len);
307 xbt_dynar_push(loclist, &new_entry);
312 /** \brief Find the frame base of a given frame
314 * \param ip Instruction pointer
318 void* mc_find_frame_base(void* ip, dw_frame_t frame, unw_cursor_t* unw_cursor) {
319 switch(frame->frame_base->type) {
323 for(loclist_cursor=0; loclist_cursor < xbt_dynar_length(frame->frame_base->location.loclist); loclist_cursor++){
324 dw_location_entry_t entry = xbt_dynar_get_as(frame->frame_base->location.loclist, loclist_cursor, dw_location_entry_t);
325 if((ip >= entry->lowpc) && (ip < entry->highpc)){
326 return (void*) MC_dwarf_resolve_location(unw_cursor, entry->location, NULL);
337 /** \brief Get the location expression or location list from an attribute
339 * Processes direct expressions as well as location lists.
342 * \param attr the attribute
343 * \return MC specific representation of the location represented by the given attribute
346 static dw_location_t MC_dwarf_get_location(mc_object_info_t info, Dwarf_Die* die, Dwarf_Attribute* attr) {
347 int form = dwarf_whatform(attr);
350 // The attribute is an DWARF location expression:
351 case DW_FORM_exprloc:
352 case DW_FORM_block1: // not in the spec
359 if (dwarf_getlocation(attr, &expr, &len))
360 xbt_die("Could not read location expression");
361 return MC_dwarf_get_expression(expr, len);
364 // The attribute is a reference to a location list entry:
365 case DW_FORM_sec_offset:
371 return MC_dwarf_get_location_list(info, die, attr);
376 xbt_die("Unexpected form %i list for location in attribute %s of <%p>%s",
378 MC_dwarf_attrname(attr->code),
379 (void*) dwarf_dieoffset(die),
380 MC_dwarf_attr_string(die, DW_AT_name));
385 /** \brief Get the location expression or location list from an attribute
387 * Processes direct expressions as well as location lists.
390 * \param attribute the attribute code
391 * \return MC specific representation of the location represented by the given attribute
394 static dw_location_t MC_dwarf_at_location(mc_object_info_t info, Dwarf_Die* die, int attribute) {
395 if(!dwarf_hasattr_integrate(die, attribute))
396 return xbt_new0(s_dw_location_t, 1);
398 Dwarf_Attribute attr;
399 dwarf_attr_integrate(die, attribute, &attr);
400 return MC_dwarf_get_location(info, die, &attr);
403 static char* MC_dwarf_at_type(Dwarf_Die* die) {
404 Dwarf_Attribute attr;
405 if (dwarf_hasattr_integrate(die, DW_AT_type)) {
406 dwarf_attr_integrate(die, DW_AT_type, &attr);
407 Dwarf_Die subtype_die;
408 if (dwarf_formref_die(&attr, &subtype_die)==NULL) {
409 xbt_die("Could not find DIE for type");
411 Dwarf_Off subtype_global_offset = dwarf_dieoffset(&subtype_die);
412 return bprintf("%" PRIx64 , subtype_global_offset);
417 static uint64_t MC_dwarf_attr_addr(Dwarf_Die* die, int attribute) {
418 Dwarf_Attribute attr;
419 if(dwarf_attr_integrate(die, attribute, &attr)==NULL)
422 if (dwarf_formaddr(&attr, &value) == 0)
423 return (uint64_t) value;
428 static uint64_t MC_dwarf_attr_uint(Dwarf_Die* die, int attribute, uint64_t default_value) {
429 Dwarf_Attribute attr;
430 if (dwarf_attr_integrate(die, attribute, &attr)==NULL)
431 return default_value;
433 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr), &value) == 0 ? (uint64_t) value : default_value;
436 static bool MC_dwarf_attr_flag(Dwarf_Die* die, int attribute, int integrate) {
437 Dwarf_Attribute attr;
438 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
439 : dwarf_attr(die, attribute, &attr))==0)
443 if (dwarf_formflag(&attr, &result))
444 xbt_die("Unexpected form for attribute %s",
445 MC_dwarf_attrname(attribute));
449 static uint64_t MC_dwarf_default_lower_bound(int lang) {
454 case DW_LANG_C_plus_plus:
458 case DW_LANG_ObjC_plus_plus:
464 case DW_LANG_Fortran77:
465 case DW_LANG_Fortran90:
466 case DW_LANG_Fortran95:
467 case DW_LANG_Modula2:
468 case DW_LANG_Pascal83:
470 case DW_LANG_Cobol74:
471 case DW_LANG_Cobol85:
474 xbt_die("No default MT_TAG_lower_bound for language %i and none given", lang);
479 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
480 xbt_assert(dwarf_tag(die)==DW_TAG_enumeration_type ||dwarf_tag(die)==DW_TAG_subrange_type,
481 "MC_dwarf_subrange_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
483 // Use DW_TAG_count if present:
484 if (dwarf_hasattr_integrate(die, DW_AT_count)) {
485 return MC_dwarf_attr_uint(die, DW_AT_count, 0);
488 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
490 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
491 // This is not really 0, but the code expects this (we do not know):
494 uint64_t upper_bound = MC_dwarf_attr_uint(die, DW_AT_upper_bound, -1);
496 uint64_t lower_bound = 0;
497 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
498 lower_bound = MC_dwarf_attr_uint(die, DW_AT_lower_bound, -1);
500 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
502 return upper_bound - lower_bound + 1;
505 static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
506 xbt_assert(dwarf_tag(die)==DW_TAG_array_type,
507 "MC_dwarf_array_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
512 for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
513 int child_tag = dwarf_tag(&child);
514 if (child_tag==DW_TAG_subrange_type ||child_tag==DW_TAG_enumeration_type) {
515 result *= MC_dwarf_subrange_element_count(&child, unit);
523 Dwarf_Off MC_dwarf_resolve_location(unw_cursor_t* c, dw_location_t location, void* frame_pointer_address) {
525 switch (location->type){
527 if (xbt_dynar_length(location->location.compose) > 1){
528 return 0; /* TODO : location list with optimizations enabled */
530 dw_location_t location_entry = xbt_dynar_get_as(location->location.compose, 0, dw_location_t);
531 switch (location_entry->type){
532 case e_dw_bregister_op:
533 unw_get_reg(c, location_entry->location.breg_op.reg, &res);
534 return (Dwarf_Off) ((long)res + location_entry->location.breg_op.offset);
536 case e_dw_fbregister_op:
537 if (frame_pointer_address != NULL)
538 return (Dwarf_Off)((char *)frame_pointer_address + location_entry->location.fbreg_op);
542 return 0; /* FIXME : implement other cases (with optimizations enabled) */
552 static void MC_dwarf_fill_member_location(dw_type_t type, dw_type_t member, Dwarf_Die* child) {
553 if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
554 xbt_die("Can't groke DW_AT_data_bit_offset.");
557 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
558 if (type->type != DW_TAG_union_type) {
560 "Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%p>%s",
561 member->name, type->id, type->name);
567 Dwarf_Attribute attr;
568 dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
569 int form = dwarf_whatform(&attr);
570 int klass = MC_dwarf_form_get_class(form);
572 case MC_DW_CLASS_EXPRLOC:
573 case MC_DW_CLASS_BLOCK:
574 // Location expression:
578 if (dwarf_getlocation(&attr, &expr, &len)) {
580 "Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%p>%s",
581 MC_dwarf_attr_string(child, DW_AT_name),
582 type->id, type->name);
584 if (len==1 && expr[0].atom == DW_OP_plus_uconst) {
585 member->offset = expr[0].number;
587 xbt_die("Can't groke this location expression yet.");
591 case MC_DW_CLASS_CONSTANT:
592 // Offset from the base address of the object:
595 if (!dwarf_formudata(&attr, &offset))
596 member->offset = offset;
598 xbt_die("Cannot get %s location <%p>%s",
599 MC_dwarf_attr_string(child, DW_AT_name),
600 type->id, type->name);
603 case MC_DW_CLASS_LOCLISTPTR:
604 // Reference to a location list:
606 case MC_DW_CLASS_REFERENCE:
607 // It's supposed to be possible in DWARF2 but I couldn't find its semantic
611 "Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
617 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_type_t type) {
620 xbt_assert(!type->members);
621 type->members = xbt_dynar_new(sizeof(dw_type_t), (void(*)(void*))dw_type_free);
622 for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
623 if (dwarf_tag(&child)==DW_TAG_member) {
625 // Skip declarations:
626 if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
629 // Skip compile time constants:
630 if(dwarf_hasattr(&child, DW_AT_const_value))
633 // TODO, we should use another type (because is is not a type but a member)
634 dw_type_t member = xbt_new0(s_dw_type_t, 1);
638 const char* name = MC_dwarf_attr_string(&child, DW_AT_name);
640 member->name = xbt_strdup(name);
644 member->byte_size = MC_dwarf_attr_uint(&child, DW_AT_byte_size, 0);
645 member->element_count = -1;
646 member->dw_type_id = MC_dwarf_at_type(&child);
647 member->members = NULL;
648 member->is_pointer_type = 0;
651 if(dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
652 xbt_die("Can't groke DW_AT_data_bit_offset.");
655 MC_dwarf_fill_member_location(type, member, &child);
657 if (!member->dw_type_id) {
658 xbt_die("Missing type for member %s of <%p>%s", member->name, type->id, type->name);
661 xbt_dynar_push(type->members, &member);
666 /** \brief Create a MC type object from a DIE
668 * \param info current object info object
669 * \param DIE (for a given type);
670 * \param unit compilation unit of the current DIE
671 * \return MC representation of the type
673 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) {
675 dw_type_t type = xbt_new0(s_dw_type_t, 1);
680 type->element_count = -1;
681 type->dw_type_id = NULL;
682 type->members = NULL;
683 type->is_pointer_type = 0;
686 type->type = dwarf_tag(die);
689 type->id = (void *) dwarf_dieoffset(die);
691 const char* name = MC_dwarf_attr_string(die, DW_AT_name);
693 type->name = xbt_strdup(name);
696 XBT_DEBUG("Processing type <%p>%s", type->id, type->name);
698 type->dw_type_id = MC_dwarf_at_type(die);
700 // Computation of the byte_size;
701 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
702 type->byte_size = MC_dwarf_attr_uint(die, DW_AT_byte_size, 0);
703 else if (type->type == DW_TAG_array_type || type->type==DW_TAG_structure_type || type->type==DW_TAG_class_type) {
705 if (dwarf_aggregate_size(die, &size)==0) {
706 type->byte_size = size;
710 switch (type->type) {
711 case DW_TAG_array_type:
712 type->element_count = MC_dwarf_array_element_count(die, unit);
713 // TODO, handle DW_byte_stride and (not) DW_bit_stride
716 case DW_TAG_pointer_type:
717 case DW_TAG_reference_type:
718 case DW_TAG_rvalue_reference_type:
719 type->is_pointer_type = 1;
722 case DW_TAG_structure_type:
723 case DW_TAG_union_type:
724 case DW_TAG_class_type:
725 MC_dwarf_add_members(info, die, unit, type);
726 char* new_namespace = namespace == NULL ? xbt_strdup(type->name)
727 : bprintf("%s::%s", namespace, type->name);
728 MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
736 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) {
737 dw_type_t type = MC_dwarf_die_to_type(info, die, unit, frame, namespace);
739 char* key = bprintf("%" PRIx64, (uint64_t) type->id);
740 xbt_dict_set(info->types, key, type, NULL);
742 if(type->name && type->byte_size!=0) {
743 xbt_dict_set(info->types_by_name, type->name, type, NULL);
747 /** \brief Convert libdw location expresion elment into native one (or NULL in some cases) */
748 static dw_location_t MC_dwarf_get_expression_element(Dwarf_Op* op) {
749 dw_location_t element = xbt_new0(s_dw_location_t, 1);
750 uint8_t atom = op->atom;
751 if (atom >= DW_OP_reg0 && atom<= DW_OP_reg31) {
752 element->type = e_dw_register;
753 element->location.reg = atom - DW_OP_reg0;
755 else if (atom >= DW_OP_breg0 && atom<= DW_OP_breg31) {
756 element->type = e_dw_bregister_op;
757 element->location.reg = atom - DW_OP_breg0;
758 element->location.breg_op.offset = op->number;
760 else if (atom >= DW_OP_lit0 && atom<= DW_OP_lit31) {
761 element->type = e_dw_lit;
762 element->location.reg = atom - DW_OP_lit0;
766 element->type = e_dw_fbregister_op;
767 element->location.fbreg_op = op->number;
770 element->type = e_dw_piece;
771 element->location.piece = op->number;
773 case DW_OP_plus_uconst:
774 element->type = e_dw_plus_uconst;
775 element->location.plus_uconst = op->number;
778 element->type = e_dw_arithmetic;
779 element->location.arithmetic = xbt_strdup("abs");
782 element->type = e_dw_arithmetic;
783 element->location.arithmetic = xbt_strdup("and");
786 element->type = e_dw_arithmetic;
787 element->location.arithmetic = xbt_strdup("div");
790 element->type = e_dw_arithmetic;
791 element->location.arithmetic = xbt_strdup("minus");
794 element->type = e_dw_arithmetic;
795 element->location.arithmetic = xbt_strdup("mod");
798 element->type = e_dw_arithmetic;
799 element->location.arithmetic = xbt_strdup("mul");
802 element->type = e_dw_arithmetic;
803 element->location.arithmetic = xbt_strdup("neg");
806 element->type = e_dw_arithmetic;
807 element->location.arithmetic = xbt_strdup("not");
810 element->type = e_dw_arithmetic;
811 element->location.arithmetic = xbt_strdup("or");
814 element->type = e_dw_arithmetic;
815 element->location.arithmetic = xbt_strdup("plus");
818 case DW_OP_stack_value:
823 case DW_OP_deref_size:
824 element->type = e_dw_deref;
825 element->location.deref_size = (unsigned int short) op->number;
828 element->type = e_dw_deref;
829 element->location.deref_size = sizeof(void *);
832 element->type = e_dw_uconstant;
833 element->location.uconstant.bytes = 1;
834 element->location.uconstant.value = (unsigned long int) op->number;
837 element->type = e_dw_sconstant;
838 element->location.uconstant.bytes = 1;
839 element->location.uconstant.value = (unsigned long int) op->number;
843 element->type = e_dw_uconstant;
844 element->location.uconstant.bytes = 1;
845 element->location.uconstant.value = (unsigned long int) op->number;
848 element->type = e_dw_uconstant;
849 element->location.uconstant.bytes = 2;
850 element->location.uconstant.value = (unsigned long int) op->number;
853 element->type = e_dw_uconstant;
854 element->location.uconstant.bytes = 4;
855 element->location.uconstant.value = (unsigned long int) op->number;
858 element->type = e_dw_uconstant;
859 element->location.uconstant.bytes = 8;
860 element->location.uconstant.value = (unsigned long int) op->number;
864 element->type = e_dw_sconstant;
865 element->location.uconstant.bytes = 1;
866 element->location.uconstant.value = (unsigned long int) op->number;
869 element->type = e_dw_sconstant;
870 element->location.uconstant.bytes = 2;
871 element->location.uconstant.value = (unsigned long int) op->number;
874 element->type = e_dw_sconstant;
875 element->location.uconstant.bytes = 4;
876 element->location.uconstant.value = (unsigned long int) op->number;
879 element->type = e_dw_sconstant;
880 element->location.uconstant.bytes = 8;
881 element->location.uconstant.value = (unsigned long int) op->number;
884 element->type = e_dw_unsupported;
890 /** \brief Convert libdw location expresion into native one */
891 static dw_location_t MC_dwarf_get_expression(Dwarf_Op* expr, size_t len) {
892 dw_location_t loc = xbt_new0(s_dw_location_t, 1);
893 loc->type = e_dw_compose;
894 loc->location.compose = xbt_dynar_new(sizeof(dw_location_t), NULL);
897 for (i=0; i!=len; ++i) {
898 dw_location_t element = MC_dwarf_get_expression_element(expr+i);
900 xbt_dynar_push(loc->location.compose, &element);
906 static int mc_anonymous_variable_index = 0;
908 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) {
909 // Skip declarations:
910 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
913 // Skip compile time constants:
914 if(dwarf_hasattr(die, DW_AT_const_value))
917 Dwarf_Attribute attr_location;
918 if (dwarf_attr(die, DW_AT_location, &attr_location)==NULL) {
919 // No location: do not add it ?
923 dw_variable_t variable = xbt_new0(s_dw_variable_t, 1);
924 variable->dwarf_offset = dwarf_dieoffset(die);
925 variable->global = frame == NULL; // Can be override base on DW_AT_location
927 const char* name = MC_dwarf_attr_string(die, DW_AT_name);
928 variable->name = xbt_strdup(name);
930 variable->type_origin = MC_dwarf_at_type(die);
932 int form = dwarf_whatform(&attr_location);
933 int klass = form == DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
935 case MC_DW_CLASS_EXPRLOC:
936 case MC_DW_CLASS_BLOCK:
937 // Location expression:
941 if (dwarf_getlocation(&attr_location, &expr, &len)) {
943 "Could not read location expression in DW_AT_location of variable <%p>%s",
944 (void*) variable->dwarf_offset, variable->name);
947 if (len==1 && expr[0].atom == DW_OP_addr) {
948 variable->global = 1;
949 Dwarf_Off offset = expr[0].number;
950 // TODO, Why is this different base on the object?
951 Dwarf_Off base = strcmp(info->file_name, xbt_binary_name) !=0 ? (Dwarf_Off) info->start_exec : 0;
952 variable->address = (void*) (base + offset);
954 variable->location = MC_dwarf_get_expression(expr, len);
959 case MC_DW_CLASS_LOCLISTPTR:
960 case MC_DW_CLASS_CONSTANT:
961 // Reference to location list:
962 variable->location = MC_dwarf_get_location_list(info, die, &attr_location);
965 xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location in <%p>%s",
966 form, form, klass, klass, (void*) variable->dwarf_offset, variable->name);
969 // Handle start_scope:
970 if (dwarf_hasattr(die, DW_AT_start_scope)) {
971 Dwarf_Attribute attr;
972 dwarf_attr(die, DW_AT_start_scope, &attr);
973 int form = dwarf_whatform(&attr);
974 int klass = MC_dwarf_form_get_class(form);
976 case MC_DW_CLASS_CONSTANT:
979 variable->start_scope = dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
983 xbt_die("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
984 form, klass, name==NULL ? "?" : name);
988 if(namespace && variable->global) {
989 char* old_name = variable->name;
990 variable->name = bprintf("%s::%s", namespace, old_name);
994 // The current code needs a variable name,
995 // generate a fake one:
996 if(!variable->name) {
997 variable->name = bprintf("@anonymous#%i", mc_anonymous_variable_index++);
1003 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) {
1004 dw_variable_t variable = MC_die_to_variable(info, die, unit, frame, namespace);
1007 MC_dwarf_register_variable(info, frame, variable);
1010 static void MC_dwarf_handle_subprogram_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t parent_frame, const char* namespace) {
1011 dw_frame_t frame = xbt_new0(s_dw_frame_t, 1);
1013 frame->start = dwarf_dieoffset(die);
1015 const char* name = MC_dwarf_attr_string(die, DW_AT_name);
1016 frame->name = namespace ? bprintf("%s::%s", namespace, name) : xbt_strdup(name);
1018 // This is the base address for DWARF addresses.
1019 // Relocated addresses are offset from this base address.
1020 // See DWARF4 spec 7.5
1021 void* base = info->flags & MC_OBJECT_INFO_EXECUTABLE ? 0 : MC_object_base_address(info);
1023 // Variables are filled in the (recursive) call of MC_dwarf_handle_children:
1024 frame->variables = xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
1025 frame->high_pc = ((char*) base) + MC_dwarf_attr_addr(die, DW_AT_high_pc);
1026 frame->low_pc = ((char*) base) + MC_dwarf_attr_addr(die, DW_AT_low_pc);
1027 frame->frame_base = MC_dwarf_at_location(info, die, DW_AT_frame_base);
1028 frame->end = -1; // This one is now useless:
1031 xbt_dynar_push(info->subprograms, &frame);
1034 MC_dwarf_handle_children(info, die, unit, frame, namespace);
1037 static void mc_dwarf_handle_namespace_die(
1038 mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
1039 const char* name = MC_dwarf_attr_string(die, DW_AT_name);
1041 xbt_die("Unexpected namespace in a subprogram");
1042 char* new_namespace = namespace == NULL ? xbt_strdup(name)
1043 : bprintf("%s::%s", namespace, name);
1044 MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
1047 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
1050 for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
1051 MC_dwarf_handle_die(info, &child, unit, frame, namespace);
1055 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
1056 int tag = dwarf_tag(die);
1057 mc_tag_class klass = MC_dwarf_tag_classify(tag);
1062 MC_dwarf_handle_type_die(info, die, unit, frame, namespace);
1066 case mc_tag_subprogram:
1067 MC_dwarf_handle_subprogram_die(info, die, unit, frame, namespace);
1071 case mc_tag_variable:
1072 MC_dwarf_handle_variable_die(info, die, unit, frame, namespace);
1080 case mc_tag_namespace:
1081 mc_dwarf_handle_namespace_die(info, die, unit, frame, namespace);
1090 void MC_dwarf_get_variables(mc_object_info_t info) {
1091 int fd = open(info->file_name, O_RDONLY);
1093 xbt_die("Could not open file %s", info->file_name);
1095 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
1097 xbt_die("Your program must be compiled with -g");
1100 Dwarf_Off offset = 0;
1101 Dwarf_Off next_offset = 0;
1103 while (dwarf_nextcu (dwarf, offset, &next_offset, &length, NULL, NULL, NULL) == 0) {
1106 if(dwarf_offdie(dwarf, offset+length, &unit_die)!=NULL) {
1109 for (res=dwarf_child(&unit_die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
1110 MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
1113 offset = next_offset;