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_unkown";
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_unkown";
120 /** \brief A class of DWARF tags (DW_TAG_*)
122 typedef enum mc_tag_class {
130 static mc_tag_class MC_dwarg_tag_classify(int tag) {
133 case DW_TAG_array_type:
134 case DW_TAG_class_type:
135 case DW_TAG_enumeration_type:
137 case DW_TAG_pointer_type:
138 case DW_TAG_string_type:
139 case DW_TAG_structure_type:
140 case DW_TAG_subroutine_type:
141 case DW_TAG_union_type:
142 case DW_TAG_ptr_to_member_type:
143 case DW_TAG_set_type:
144 case DW_TAG_subrange_type:
145 case DW_TAG_base_type:
146 case DW_TAG_const_type:
147 case DW_TAG_file_type:
148 case DW_TAG_packed_type:
149 case DW_TAG_volatile_type:
150 case DW_TAG_restrict_type:
151 case DW_TAG_interface_type:
152 case DW_TAG_unspecified_type:
153 case DW_TAG_mutable_type:
154 case DW_TAG_shared_type:
157 case DW_TAG_subprogram:
158 return mc_tag_subprogram;
160 case DW_TAG_variable:
161 case DW_TAG_formal_parameter:
162 return mc_tag_variable;
164 case DW_TAG_lexical_block:
165 case DW_TAG_try_block:
166 case DW_TAG_inlined_subroutine:
170 return mc_tag_unkonwn;
175 #define MC_DW_CLASS_UNKNOWN 0
176 #define MC_DW_CLASS_ADDRESS 1 // Location in the address space of the program
177 #define MC_DW_CLASS_BLOCK 2 // Arbitrary block of bytes
178 #define MC_DW_CLASS_CONSTANT 3
179 #define MC_DW_CLASS_STRING 3 // String
180 #define MC_DW_CLASS_FLAG 4 // Boolean
181 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
182 #define MC_DW_CLASS_EXPRLOC 6 // DWARF expression/location description
183 #define MC_DW_CLASS_LINEPTR 7
184 #define MC_DW_CLASS_LOCLISTPTR 8
185 #define MC_DW_CLASS_MACPTR 9
186 #define MC_DW_CLASS_RANGELISTPTR 10
188 static int MC_dwarf_form_get_class(int form) {
191 return MC_DW_CLASS_ADDRESS;
196 return MC_DW_CLASS_BLOCK;
203 return MC_DW_CLASS_CONSTANT;
206 return MC_DW_CLASS_STRING;
207 case DW_FORM_ref_addr:
212 case DW_FORM_ref_udata:
213 return MC_DW_CLASS_REFERENCE;
215 case DW_FORM_flag_present:
216 return MC_DW_CLASS_FLAG;
217 case DW_FORM_exprloc:
218 return MC_DW_CLASS_EXPRLOC;
222 return MC_DW_CLASS_UNKNOWN;
226 /** \brief Get the name of the tag of a given DIE
229 * \return name of the tag of this DIE
231 static inline const char* MC_dwarf_die_tagname(Dwarf_Die* die) {
232 return MC_dwarf_tagname(dwarf_tag(die));
237 /** \brief Get an attribute of a given DIE as a string
240 * \param attribute attribute
241 * \return value of the given attribute of the given DIE
243 static const char* MC_dwarf_attr_string(Dwarf_Die* die, int attribute) {
244 Dwarf_Attribute attr;
245 if (!dwarf_attr_integrate(die, attribute, &attr)) {
248 return dwarf_formstring(&attr);
252 /** \brief Get the linkage name of a DIE.
254 * Use either DW_AT_linkage_name or DW_AR_MIPS_linkage_name.
257 * \return linkage name of the given DIE (or NULL)
259 static const char* MC_dwarf_at_linkage_name(Dwarf_Die* die) {
260 const char* name = MC_dwarf_attr_string(die, DW_AT_linkage_name);
262 name = MC_dwarf_attr_string(die, DW_AT_MIPS_linkage_name);
266 /** \brief Create a location list from a given attribute
269 * \param attr the attribute
270 * \return MC specific representation of the location list represented by the given attribute
273 static dw_location_t MC_dwarf_get_location_list(mc_object_info_t info, Dwarf_Die* die, Dwarf_Attribute* attr) {
275 dw_location_t location = xbt_new0(s_dw_location_t, 1);
276 location->type = e_dw_loclist;
277 xbt_dynar_t loclist = xbt_dynar_new(sizeof(dw_location_entry_t), NULL);
278 location->location.loclist = loclist;
280 ptrdiff_t offset = 0;
281 Dwarf_Addr base, start, end;
287 offset = dwarf_getlocations(attr, offset, &base, &start, &end, &expr, &len);
291 xbt_die("Error while loading location list");
293 dw_location_entry_t new_entry = xbt_new0(s_dw_location_entry_t, 1);
295 void* base = info->flags & MC_OBJECT_INFO_EXECUTABLE ? 0 : MC_object_base_address(info);
297 new_entry->lowpc = (char*) base + start;
298 new_entry->highpc = (char*) base + end;
299 new_entry->location = MC_dwarf_get_expression(expr, len);
301 xbt_dynar_push(loclist, &new_entry);
306 /** \brief Find the frame base of a given frame
308 * \param ip Instruction pointer
312 void* mc_find_frame_base(void* ip, dw_frame_t frame, unw_cursor_t* unw_cursor) {
313 switch(frame->frame_base->type) {
317 for(loclist_cursor=0; loclist_cursor < xbt_dynar_length(frame->frame_base->location.loclist); loclist_cursor++){
318 dw_location_entry_t entry = xbt_dynar_get_as(frame->frame_base->location.loclist, loclist_cursor, dw_location_entry_t);
319 if((ip >= entry->lowpc) && (ip < entry->highpc)){
320 return (void*) MC_dwarf_resolve_location(unw_cursor, entry->location, NULL);
331 /** \brief Get the location expression or location list from an attribute
333 * Processes direct expressions as well as location lists.
336 * \param attr the attribute
337 * \return MC specific representation of the location represented by the given attribute
340 static dw_location_t MC_dwarf_get_location(mc_object_info_t info, Dwarf_Die* die, Dwarf_Attribute* attr) {
341 int form = dwarf_whatform(attr);
344 // The attribute is an DWARF location expression:
345 case DW_FORM_exprloc:
346 case DW_FORM_block1: // not in the spec
353 if (dwarf_getlocation(attr, &expr, &len))
354 xbt_die("Could not read location expression");
355 return MC_dwarf_get_expression(expr, len);
358 // The attribute is a reference to a location list entry:
359 case DW_FORM_sec_offset:
365 return MC_dwarf_get_location_list(info, die, attr);
370 xbt_die("Unexpected form %i list for location in attribute %s of <%p>%s",
372 MC_dwarf_attrname(attr->code),
373 (void*) dwarf_dieoffset(die),
374 MC_dwarf_attr_string(die, DW_AT_name));
379 /** \brief Get the location expression or location list from an attribute
381 * Processes direct expressions as well as location lists.
384 * \param attribute the attribute code
385 * \return MC specific representation of the location represented by the given attribute
388 static dw_location_t MC_dwarf_at_location(mc_object_info_t info, Dwarf_Die* die, int attribute) {
389 if(!dwarf_hasattr_integrate(die, attribute))
390 return xbt_new0(s_dw_location_t, 1);
392 Dwarf_Attribute attr;
393 dwarf_attr_integrate(die, attribute, &attr);
394 return MC_dwarf_get_location(info, die, &attr);
397 static char* MC_dwarf_at_type(Dwarf_Die* die) {
398 Dwarf_Attribute attr;
399 if (dwarf_hasattr_integrate(die, DW_AT_type)) {
400 dwarf_attr_integrate(die, DW_AT_type, &attr);
401 Dwarf_Die subtype_die;
402 if (dwarf_formref_die(&attr, &subtype_die)==NULL) {
403 xbt_die("Could not find DIE for type");
405 Dwarf_Off subtype_global_offset = dwarf_dieoffset(&subtype_die);
406 return bprintf("%" PRIx64 , subtype_global_offset);
411 static uint64_t MC_dwarf_attr_addr(Dwarf_Die* die, int attribute) {
412 Dwarf_Attribute attr;
413 if(dwarf_attr_integrate(die, attribute, &attr)==NULL)
416 if (dwarf_formaddr(&attr, &value) == 0)
417 return (uint64_t) value;
422 static uint64_t MC_dwarf_attr_uint(Dwarf_Die* die, int attribute, uint64_t default_value) {
423 Dwarf_Attribute attr;
424 if (dwarf_attr_integrate(die, attribute, &attr)==NULL)
425 return default_value;
427 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr), &value) == 0 ? (uint64_t) value : default_value;
430 static bool MC_dwarf_attr_flag(Dwarf_Die* die, int attribute, int integrate) {
431 Dwarf_Attribute attr;
432 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
433 : dwarf_attr(die, attribute, &attr))==0)
437 if (dwarf_formflag(&attr, &result))
438 xbt_die("Unexpected form for attribute %s",
439 MC_dwarf_attrname(attribute));
443 static uint64_t MC_dwarf_default_lower_bound(int lang) {
448 case DW_LANG_C_plus_plus:
452 case DW_LANG_ObjC_plus_plus:
458 case DW_LANG_Fortran77:
459 case DW_LANG_Fortran90:
460 case DW_LANG_Fortran95:
461 case DW_LANG_Modula2:
462 case DW_LANG_Pascal83:
464 case DW_LANG_Cobol74:
465 case DW_LANG_Cobol85:
468 xbt_die("No default MT_TAG_lower_bound for language %i and none given", lang);
473 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
474 xbt_assert(dwarf_tag(die)==DW_TAG_enumeration_type ||dwarf_tag(die)==DW_TAG_subrange_type,
475 "MC_dwarf_subrange_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
477 // Use DW_TAG_count if present:
478 if (dwarf_hasattr_integrate(die, DW_AT_count)) {
479 return MC_dwarf_attr_uint(die, DW_AT_count, 0);
482 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
484 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
485 // This is not really 0, but the code expects this (we do not know):
488 uint64_t upper_bound = MC_dwarf_attr_uint(die, DW_AT_upper_bound, -1);
490 uint64_t lower_bound = 0;
491 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
492 lower_bound = MC_dwarf_attr_uint(die, DW_AT_lower_bound, -1);
494 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
496 return upper_bound - lower_bound + 1;
499 static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
500 xbt_assert(dwarf_tag(die)==DW_TAG_array_type,
501 "MC_dwarf_array_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
506 for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
507 int child_tag = dwarf_tag(&child);
508 if (child_tag==DW_TAG_subrange_type ||child_tag==DW_TAG_enumeration_type) {
509 result *= MC_dwarf_subrange_element_count(&child, unit);
517 Dwarf_Off MC_dwarf_resolve_location(unw_cursor_t* c, dw_location_t location, void* frame_pointer_address) {
519 switch (location->type){
521 if (xbt_dynar_length(location->location.compose) > 1){
522 return 0; /* TODO : location list with optimizations enabled */
524 dw_location_t location_entry = xbt_dynar_get_as(location->location.compose, 0, dw_location_t);
525 switch (location_entry->type){
527 unw_get_reg(c, location_entry->location.reg, &res);
529 case e_dw_bregister_op:
530 unw_get_reg(c, location_entry->location.breg_op.reg, &res);
531 return (Dwarf_Off) ((long)res + location_entry->location.breg_op.offset);
533 case e_dw_fbregister_op:
534 if (frame_pointer_address != NULL)
535 return (Dwarf_Off)((char *)frame_pointer_address + location_entry->location.fbreg_op);
539 return 0; /* FIXME : implement other cases (with optimizations enabled) */
549 static void MC_dwarf_fill_member_location(dw_type_t type, dw_type_t member, Dwarf_Die* child) {
550 if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
551 xbt_die("Can't groke DW_AT_data_bit_offset.");
554 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
555 if (type->type != DW_TAG_union_type) {
557 "Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%p>%s",
558 member->name, type->id, type->name);
564 Dwarf_Attribute attr;
565 dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
566 int form = dwarf_whatform(&attr);
567 int klass = MC_dwarf_form_get_class(form);
569 case MC_DW_CLASS_EXPRLOC:
570 case MC_DW_CLASS_BLOCK:
571 // Location expression:
575 if (dwarf_getlocation(&attr, &expr, &len)) {
577 "Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%p>%s",
578 MC_dwarf_attr_string(child, DW_AT_name),
579 type->id, type->name);
581 if (len==1 && expr[0].atom == DW_OP_plus_uconst) {
582 member->offset = expr[0].number;
584 xbt_die("Can't groke this location expression yet.");
588 case MC_DW_CLASS_CONSTANT:
589 // Offset from the base address of the object:
592 if (!dwarf_formudata(&attr, &offset))
593 member->offset = offset;
595 xbt_die("Cannot get %s location <%p>%s",
596 MC_dwarf_attr_string(child, DW_AT_name),
597 type->id, type->name);
600 case MC_DW_CLASS_LOCLISTPTR:
601 // Reference to a location list:
603 case MC_DW_CLASS_REFERENCE:
604 // It's supposed to be possible in DWARF2 but I couldn't find its semantic
608 "Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
614 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_type_t type) {
617 xbt_assert(!type->members);
618 type->members = xbt_dynar_new(sizeof(dw_type_t), (void(*)(void*))dw_type_free);
619 for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
620 if (dwarf_tag(&child)==DW_TAG_member) {
621 // TODO, we should use another type (because is is not a type but a member)
622 dw_type_t member = xbt_new0(s_dw_type_t, 1);
626 const char* name = MC_dwarf_attr_string(&child, DW_AT_name);
628 member->name = xbt_strdup(name);
632 member->byte_size = MC_dwarf_attr_uint(&child, DW_AT_byte_size, 0);
633 member->element_count = -1;
634 member->dw_type_id = MC_dwarf_at_type(&child);
635 member->members = NULL;
636 member->is_pointer_type = 0;
639 if(dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
640 xbt_die("Can't groke DW_AT_data_bit_offset.");
643 MC_dwarf_fill_member_location(type, member, &child);
645 if (!member->dw_type_id) {
646 xbt_die("Missing type for member %s of <%p>%s", member->name, type->id, type->name);
649 xbt_dynar_push(type->members, &member);
654 /** \brief Create a MC type object from a DIE
656 * \param info current object info object
657 * \param DIE (for a given type);
658 * \param unit compilation unit of the current DIE
659 * \return MC representation of the type
661 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) {
663 dw_type_t type = xbt_new0(s_dw_type_t, 1);
668 type->element_count = -1;
669 type->dw_type_id = NULL;
670 type->members = NULL;
671 type->is_pointer_type = 0;
674 type->type = dwarf_tag(die);
677 type->id = (void *) dwarf_dieoffset(die);
679 const char* name = MC_dwarf_attr_string(die, DW_AT_name);
681 type->name = xbt_strdup(name);
684 XBT_DEBUG("Processing type <%p>%s", type->id, type->name);
686 type->dw_type_id = MC_dwarf_at_type(die);
688 // Computation of the byte_size;
689 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
690 type->byte_size = MC_dwarf_attr_uint(die, DW_AT_byte_size, 0);
691 else if (type->type == DW_TAG_array_type || type->type==DW_TAG_structure_type || type->type==DW_TAG_class_type) {
693 if (dwarf_aggregate_size(die, &size)==0) {
694 type->byte_size = size;
698 switch (type->type) {
699 case DW_TAG_array_type:
700 type->element_count = MC_dwarf_array_element_count(die, unit);
701 // TODO, handle DW_byte_stride and (not) DW_bit_stride
704 case DW_TAG_pointer_type:
705 case DW_TAG_reference_type:
706 case DW_TAG_rvalue_reference_type:
707 type->is_pointer_type = 1;
710 case DW_TAG_structure_type:
711 case DW_TAG_union_type:
712 case DW_TAG_class_type:
713 MC_dwarf_add_members(info, die, unit, type);
714 char* new_namespace = namespace == NULL ? xbt_strdup(type->name)
715 : bprintf("%s::%s", namespace, type->name);
716 MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
724 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) {
725 dw_type_t type = MC_dwarf_die_to_type(info, die, unit, frame, namespace);
727 char* key = bprintf("%" PRIx64, (uint64_t) type->id);
728 xbt_dict_set(info->types, key, type, NULL);
730 if(type->name && type->byte_size!=0) {
731 xbt_dict_set(info->types_by_name, type->name, type, NULL);
735 /** \brief Convert libdw location expresion elment into native one (or NULL in some cases) */
736 static dw_location_t MC_dwarf_get_expression_element(Dwarf_Op* op) {
737 dw_location_t element = xbt_new0(s_dw_location_t, 1);
738 uint8_t atom = op->atom;
739 if (atom >= DW_OP_reg0 && atom<= DW_OP_reg31) {
740 element->type = e_dw_register;
741 element->location.reg = atom - DW_OP_reg0;
743 else if (atom >= DW_OP_breg0 && atom<= DW_OP_breg31) {
744 element->type = e_dw_bregister_op;
745 element->location.reg = atom - DW_OP_breg0;
746 element->location.breg_op.offset = op->number;
748 else if (atom >= DW_OP_lit0 && atom<= DW_OP_lit31) {
749 element->type = e_dw_lit;
750 element->location.reg = atom - DW_OP_lit0;
754 element->type = e_dw_fbregister_op;
755 element->location.fbreg_op = op->number;
758 element->type = e_dw_piece;
759 element->location.piece = op->number;
761 case DW_OP_plus_uconst:
762 element->type = e_dw_plus_uconst;
763 element->location.plus_uconst = op->number;
766 element->type = e_dw_arithmetic;
767 element->location.arithmetic = xbt_strdup("abs");
770 element->type = e_dw_arithmetic;
771 element->location.arithmetic = xbt_strdup("and");
774 element->type = e_dw_arithmetic;
775 element->location.arithmetic = xbt_strdup("div");
778 element->type = e_dw_arithmetic;
779 element->location.arithmetic = xbt_strdup("minus");
782 element->type = e_dw_arithmetic;
783 element->location.arithmetic = xbt_strdup("mod");
786 element->type = e_dw_arithmetic;
787 element->location.arithmetic = xbt_strdup("mul");
790 element->type = e_dw_arithmetic;
791 element->location.arithmetic = xbt_strdup("neg");
794 element->type = e_dw_arithmetic;
795 element->location.arithmetic = xbt_strdup("not");
798 element->type = e_dw_arithmetic;
799 element->location.arithmetic = xbt_strdup("or");
802 element->type = e_dw_arithmetic;
803 element->location.arithmetic = xbt_strdup("plus");
806 case DW_OP_stack_value:
811 case DW_OP_deref_size:
812 element->type = e_dw_deref;
813 element->location.deref_size = (unsigned int short) op->number;
816 element->type = e_dw_deref;
817 element->location.deref_size = sizeof(void *);
820 element->type = e_dw_uconstant;
821 element->location.uconstant.bytes = 1;
822 element->location.uconstant.value = (unsigned long int) op->number;
825 element->type = e_dw_sconstant;
826 element->location.uconstant.bytes = 1;
827 element->location.uconstant.value = (unsigned long int) op->number;
831 element->type = e_dw_uconstant;
832 element->location.uconstant.bytes = 1;
833 element->location.uconstant.value = (unsigned long int) op->number;
836 element->type = e_dw_uconstant;
837 element->location.uconstant.bytes = 2;
838 element->location.uconstant.value = (unsigned long int) op->number;
841 element->type = e_dw_uconstant;
842 element->location.uconstant.bytes = 4;
843 element->location.uconstant.value = (unsigned long int) op->number;
846 element->type = e_dw_uconstant;
847 element->location.uconstant.bytes = 8;
848 element->location.uconstant.value = (unsigned long int) op->number;
852 element->type = e_dw_sconstant;
853 element->location.uconstant.bytes = 1;
854 element->location.uconstant.value = (unsigned long int) op->number;
857 element->type = e_dw_sconstant;
858 element->location.uconstant.bytes = 2;
859 element->location.uconstant.value = (unsigned long int) op->number;
862 element->type = e_dw_sconstant;
863 element->location.uconstant.bytes = 4;
864 element->location.uconstant.value = (unsigned long int) op->number;
867 element->type = e_dw_sconstant;
868 element->location.uconstant.bytes = 8;
869 element->location.uconstant.value = (unsigned long int) op->number;
872 element->type = e_dw_unsupported;
878 /** \brief Convert libdw location expresion into native one */
879 static dw_location_t MC_dwarf_get_expression(Dwarf_Op* expr, size_t len) {
880 dw_location_t loc = xbt_new0(s_dw_location_t, 1);
881 loc->type = e_dw_compose;
882 loc->location.compose = xbt_dynar_new(sizeof(dw_location_t), NULL);
885 for (i=0; i!=len; ++i) {
886 dw_location_t element = MC_dwarf_get_expression_element(expr+i);
888 xbt_dynar_push(loc->location.compose, &element);
894 static int mc_anonymous_variable_index = 0;
896 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) {
898 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
901 Dwarf_Attribute attr_location;
902 if (dwarf_attr(die, DW_AT_location, &attr_location)==NULL) {
903 // No location: do not add it ?
907 dw_variable_t variable = xbt_new0(s_dw_variable_t, 1);
908 variable->dwarf_offset = dwarf_dieoffset(die);
909 variable->global = frame == NULL; // Can be override base on DW_AT_location
911 const char* name = MC_dwarf_attr_string(die, DW_AT_name);
912 variable->name = xbt_strdup(name);
914 variable->type_origin = MC_dwarf_at_type(die);
916 int klass = MC_dwarf_form_get_class(dwarf_whatform(&attr_location));
918 case MC_DW_CLASS_EXPRLOC:
919 case MC_DW_CLASS_BLOCK:
920 // Location expression:
924 if (dwarf_getlocation(&attr_location, &expr, &len)) {
926 "Could not read location expression in DW_AT_location of variable <%p>%s",
927 (void*) variable->dwarf_offset, variable->name);
930 if (len==1 && expr[0].atom == DW_OP_addr) {
931 variable->global = 1;
932 Dwarf_Off offset = expr[0].number;
933 // TODO, Why is this different base on the object?
934 Dwarf_Off base = strcmp(info->file_name, xbt_binary_name) !=0 ? (Dwarf_Off) info->start_exec : 0;
935 variable->address = (void*) (base + offset);
937 variable->location = MC_dwarf_get_expression(expr, len);
942 case MC_DW_CLASS_LOCLISTPTR:
943 case MC_DW_CLASS_CONSTANT:
944 // Reference to location list:
945 variable->location = MC_dwarf_get_location_list(info, die, &attr_location);
948 xbt_die("Unexpected calss 0x%x (%i) list for location in <%p>%s",
949 klass, klass, (void*) variable->dwarf_offset, variable->name);
952 if(namespace && variable->global) {
953 char* old_name = variable->name;
954 variable->name = bprintf("%s::%s", namespace, old_name);
958 // The current code needs a variable name,
959 // generate a fake one:
960 if(!variable->name) {
961 variable->name = bprintf("@anonymous#%i", mc_anonymous_variable_index++);
967 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) {
968 dw_variable_t variable = MC_die_to_variable(info, die, unit, frame, namespace);
971 MC_dwarf_register_variable(info, frame, variable);
974 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) {
975 dw_frame_t frame = xbt_new0(s_dw_frame_t, 1);
977 frame->start = dwarf_dieoffset(die);
979 const char* name = MC_dwarf_attr_string(die, DW_AT_name);
980 frame->name = namespace ? bprintf("%s::%s", namespace, name) : xbt_strdup(name);
982 // This is the base address for DWARF addresses.
983 // Relocated addresses are offset from this base address.
984 // See DWARF4 spec 7.5
985 void* base = info->flags & MC_OBJECT_INFO_EXECUTABLE ? 0 : MC_object_base_address(info);
987 // Variables are filled in the (recursive) call of MC_dwarf_handle_children:
988 frame->variables = xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
989 frame->high_pc = ((char*) base) + MC_dwarf_attr_addr(die, DW_AT_high_pc);
990 frame->low_pc = ((char*) base) + MC_dwarf_attr_addr(die, DW_AT_low_pc);
991 frame->frame_base = MC_dwarf_at_location(info, die, DW_AT_frame_base);
992 frame->end = -1; // This one is now useless:
995 xbt_dynar_push(info->subprograms, &frame);
998 MC_dwarf_handle_children(info, die, unit, frame, namespace);
1001 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
1004 for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
1005 MC_dwarf_handle_die(info, &child, unit, frame, namespace);
1009 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
1010 int tag = dwarf_tag(die);
1011 mc_tag_class klass = MC_dwarg_tag_classify(tag);
1016 MC_dwarf_handle_type_die(info, die, unit, frame, namespace);
1020 case mc_tag_subprogram:
1021 MC_dwarf_handle_subprogram_die(info, die, unit, frame, namespace);
1025 case mc_tag_variable:
1026 MC_dwarf_handle_variable_die(info, die, unit, frame, namespace);
1040 void MC_dwarf_get_variables(mc_object_info_t info) {
1041 int fd = open(info->file_name, O_RDONLY);
1043 xbt_die("Could not open file %s", info->file_name);
1045 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
1047 xbt_die("Your program must be compiled with -g");
1050 Dwarf_Off offset = 0;
1051 Dwarf_Off next_offset = 0;
1053 while (dwarf_nextcu (dwarf, offset, &next_offset, &length, NULL, NULL, NULL) == 0) {
1056 if(dwarf_offdie(dwarf, offset+length, &unit_die)!=NULL) {
1059 for (res=dwarf_child(&unit_die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
1060 MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
1063 offset = next_offset;