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);
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);
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);
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);
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 #define MC_DW_CLASS_UNKNOWN 0
121 #define MC_DW_CLASS_ADDRESS 1 // Location in the address space of the program
122 #define MC_DW_CLASS_BLOCK 2 // Arbitrary block of bytes
123 #define MC_DW_CLASS_CONSTANT 3
124 #define MC_DW_CLASS_STRING 3 // String
125 #define MC_DW_CLASS_FLAG 4 // Boolean
126 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
127 #define MC_DW_CLASS_EXPRLOC 6 // DWARF expression/location description
128 #define MC_DW_CLASS_LINEPTR 7
129 #define MC_DW_CLASS_LOCLISTPTR 8
130 #define MC_DW_CLASS_MACPTR 9
131 #define MC_DW_CLASS_RANGELISTPTR 10
133 static int MC_dwarf_form_get_class(int form) {
136 return MC_DW_CLASS_ADDRESS;
141 return MC_DW_CLASS_BLOCK;
148 return MC_DW_CLASS_CONSTANT;
151 return MC_DW_CLASS_STRING;
152 case DW_FORM_ref_addr:
157 case DW_FORM_ref_udata:
158 return MC_DW_CLASS_REFERENCE;
160 case DW_FORM_flag_present:
161 return MC_DW_CLASS_FLAG;
162 case DW_FORM_exprloc:
163 return MC_DW_CLASS_EXPRLOC;
167 return MC_DW_CLASS_UNKNOWN;
171 /** \brief Get the name of the tag of a given DIE
174 * \return name of the tag of this DIE
176 static inline const char* MC_dwarf_die_tagname(Dwarf_Die* die) {
177 return MC_dwarf_tagname(dwarf_tag(die));
182 /** \brief Get an attribute of a given DIE as a string
185 * \param attribute attribute
186 * \return value of the given attribute of the given DIE
188 static const char* MC_dwarf_attr_string(Dwarf_Die* die, int attribute) {
189 Dwarf_Attribute attr;
190 if (!dwarf_attr_integrate(die, attribute, &attr)) {
193 return dwarf_formstring(&attr);
197 /** \brief Get the linkage name of a DIE.
199 * Use either DW_AT_linkage_name or DW_AR_MIPS_linkage_name.
202 * \return linkage name of the given DIE (or NULL)
204 static const char* MC_dwarf_at_linkage_name(Dwarf_Die* die) {
205 const char* name = MC_dwarf_attr_string(die, DW_AT_linkage_name);
207 name = MC_dwarf_attr_string(die, DW_AT_MIPS_linkage_name);
211 /** \brief Create a location list from a given attribute
214 * \param attr the attribute
215 * \return MC specific representation of the location list represented by the given attribute
218 static dw_location_t MC_dwarf_get_location_list(mc_object_info_t info, Dwarf_Die* die, Dwarf_Attribute* attr) {
220 dw_location_t location = xbt_new0(s_dw_location_t, 1);
221 location->type = e_dw_loclist;
222 xbt_dynar_t loclist = xbt_dynar_new(sizeof(dw_location_entry_t), NULL);
223 location->location.loclist = loclist;
225 ptrdiff_t offset = 0;
226 Dwarf_Addr base, start, end;
232 offset = dwarf_getlocations(attr, offset, &base, &start, &end, &expr, &len);
236 xbt_die("Error while loading location list");
238 dw_location_entry_t new_entry = xbt_new0(s_dw_location_entry_t, 1);
240 void* base = info->flags & MC_OBJECT_INFO_EXECUTABLE ? 0 : MC_object_base_address(info);
242 new_entry->lowpc = (char*) base + start;
243 new_entry->highpc = (char*) base + end;
244 new_entry->location = MC_dwarf_get_expression(expr, len);
246 xbt_dynar_push(loclist, &new_entry);
251 /** \brief Find the frame base of a given frame
253 * \param ip Instruction pointer
257 void* mc_find_frame_base(void* ip, dw_frame_t frame, unw_cursor_t* unw_cursor) {
258 switch(frame->frame_base->type) {
262 for(loclist_cursor=0; loclist_cursor < xbt_dynar_length(frame->frame_base->location.loclist); loclist_cursor++){
263 dw_location_entry_t entry = xbt_dynar_get_as(frame->frame_base->location.loclist, loclist_cursor, dw_location_entry_t);
264 if((ip >= entry->lowpc) && (ip < entry->highpc)){
265 return (void*) MC_dwarf_resolve_location(unw_cursor, entry->location, NULL);
276 /** \brief Get the location expression or location list from an attribute
278 * Processes direct expressions as well as location lists.
281 * \param attr the attribute
282 * \return MC specific representation of the location represented by the given attribute
285 static dw_location_t MC_dwarf_get_location(mc_object_info_t info, Dwarf_Die* die, Dwarf_Attribute* attr) {
286 int form = dwarf_whatform(attr);
289 // The attribute is an DWARF location expression:
290 case DW_FORM_exprloc:
291 case DW_FORM_block1: // not in the spec
298 if (dwarf_getlocation(attr, &expr, &len))
299 xbt_die("Could not read location expression");
300 return MC_dwarf_get_expression(expr, len);
303 // The attribute is a reference to a location list entry:
304 case DW_FORM_sec_offset:
310 return MC_dwarf_get_location_list(info, die, attr);
315 xbt_die("Unexpected form %i list for location in attribute %s of <%p>%s",
317 MC_dwarf_attrname(attr->code),
318 (void*) dwarf_dieoffset(die),
319 MC_dwarf_attr_string(die, DW_AT_name));
324 /** \brief Get the location expression or location list from an attribute
326 * Processes direct expressions as well as location lists.
329 * \param attribute the attribute code
330 * \return MC specific representation of the location represented by the given attribute
333 static dw_location_t MC_dwarf_at_location(mc_object_info_t info, Dwarf_Die* die, int attribute) {
334 if(!dwarf_hasattr_integrate(die, attribute))
335 return xbt_new0(s_dw_location_t, 1);
337 Dwarf_Attribute attr;
338 dwarf_attr_integrate(die, attribute, &attr);
339 return MC_dwarf_get_location(info, die, &attr);
342 static char* MC_dwarf_at_type(Dwarf_Die* die) {
343 Dwarf_Attribute attr;
344 if (dwarf_hasattr_integrate(die, DW_AT_type)) {
345 dwarf_attr_integrate(die, DW_AT_type, &attr);
346 Dwarf_Die subtype_die;
347 if (dwarf_formref_die(&attr, &subtype_die)==NULL) {
348 xbt_die("Could not find DIE for type");
350 Dwarf_Off subtype_global_offset = dwarf_dieoffset(&subtype_die);
351 return bprintf("%" PRIx64 , subtype_global_offset);
356 static uint64_t MC_dwarf_attr_addr(Dwarf_Die* die, int attribute) {
357 Dwarf_Attribute attr;
358 if(dwarf_attr_integrate(die, attribute, &attr)==NULL)
361 if (dwarf_formaddr(&attr, &value) == 0)
362 return (uint64_t) value;
367 static uint64_t MC_dwarf_attr_uint(Dwarf_Die* die, int attribute, uint64_t default_value) {
368 Dwarf_Attribute attr;
369 if (dwarf_attr_integrate(die, attribute, &attr)==NULL)
370 return default_value;
372 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr), &value) == 0 ? (uint64_t) value : default_value;
375 static bool MC_dwarf_attr_flag(Dwarf_Die* die, int attribute, int integrate) {
376 Dwarf_Attribute attr;
377 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
378 : dwarf_attr(die, attribute, &attr))==0)
382 if (dwarf_formflag(&attr, &result))
383 xbt_die("Unexpected form for attribute %s",
384 MC_dwarf_attrname(attribute));
388 static uint64_t MC_dwarf_default_lower_bound(int lang) {
393 case DW_LANG_C_plus_plus:
397 case DW_LANG_ObjC_plus_plus:
403 case DW_LANG_Fortran77:
404 case DW_LANG_Fortran90:
405 case DW_LANG_Fortran95:
406 case DW_LANG_Modula2:
407 case DW_LANG_Pascal83:
409 case DW_LANG_Cobol74:
410 case DW_LANG_Cobol85:
413 xbt_die("No default MT_TAG_lower_bound for language %i and none given", lang);
418 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
419 xbt_assert(dwarf_tag(die)==DW_TAG_enumeration_type ||dwarf_tag(die)==DW_TAG_subrange_type,
420 "MC_dwarf_subrange_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
422 // Use DW_TAG_count if present:
423 if (dwarf_hasattr_integrate(die, DW_AT_count)) {
424 return MC_dwarf_attr_uint(die, DW_AT_count, 0);
427 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
429 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
430 // This is not really 0, but the code expects this (we do not know):
433 uint64_t upper_bound = MC_dwarf_attr_uint(die, DW_AT_upper_bound, -1);
435 uint64_t lower_bound = 0;
436 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
437 lower_bound = MC_dwarf_attr_uint(die, DW_AT_lower_bound, -1);
439 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
441 return upper_bound - lower_bound + 1;
444 static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
445 xbt_assert(dwarf_tag(die)==DW_TAG_array_type,
446 "MC_dwarf_array_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
451 for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
452 int child_tag = dwarf_tag(&child);
453 if (child_tag==DW_TAG_subrange_type ||child_tag==DW_TAG_enumeration_type) {
454 result *= MC_dwarf_subrange_element_count(&child, unit);
462 Dwarf_Off MC_dwarf_resolve_location(unw_cursor_t* c, dw_location_t location, void* frame_pointer_address) {
464 switch (location->type){
466 if (xbt_dynar_length(location->location.compose) > 1){
467 return 0; /* TODO : location list with optimizations enabled */
469 dw_location_t location_entry = xbt_dynar_get_as(location->location.compose, 0, dw_location_t);
470 switch (location_entry->type){
471 case e_dw_bregister_op:
472 unw_get_reg(c, location_entry->location.breg_op.reg, &res);
473 return (Dwarf_Off) ((long)res + location_entry->location.breg_op.offset);
475 case e_dw_fbregister_op:
476 if (frame_pointer_address != NULL)
477 return (Dwarf_Off)((char *)frame_pointer_address + location_entry->location.fbreg_op);
481 return 0; /* FIXME : implement other cases (with optimizations enabled) */
491 static void MC_dwarf_fill_member_location(dw_type_t type, dw_type_t member, Dwarf_Die* child) {
492 if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
493 xbt_die("Can't groke DW_AT_data_bit_offset.");
496 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
497 if (type->type != DW_TAG_union_type) {
499 "Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%p>%s",
500 member->name, type->id, type->name);
506 Dwarf_Attribute attr;
507 dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
508 int form = dwarf_whatform(&attr);
509 int klass = MC_dwarf_form_get_class(form);
511 case MC_DW_CLASS_EXPRLOC:
512 case MC_DW_CLASS_BLOCK:
513 // Location expression:
517 if (dwarf_getlocation(&attr, &expr, &len)) {
519 "Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%p>%s",
520 MC_dwarf_attr_string(child, DW_AT_name),
521 type->id, type->name);
523 if (len==1 && expr[0].atom == DW_OP_plus_uconst) {
524 member->offset = expr[0].number;
526 xbt_die("Can't groke this location expression yet.");
530 case MC_DW_CLASS_CONSTANT:
531 // Offset from the base address of the object:
534 if (!dwarf_formudata(&attr, &offset))
535 member->offset = offset;
537 xbt_die("Cannot get %s location <%p>%s",
538 MC_dwarf_attr_string(child, DW_AT_name),
539 type->id, type->name);
542 case MC_DW_CLASS_LOCLISTPTR:
543 // Reference to a location list:
545 case MC_DW_CLASS_REFERENCE:
546 // It's supposed to be possible in DWARF2 but I couldn't find its semantic
550 "Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
556 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_type_t type) {
559 xbt_assert(!type->members);
560 type->members = xbt_dynar_new(sizeof(dw_type_t), (void(*)(void*))dw_type_free);
561 for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
562 if (dwarf_tag(&child)==DW_TAG_member) {
563 // TODO, we should use another type (because is is not a type but a member)
564 dw_type_t member = xbt_new0(s_dw_type_t, 1);
568 const char* name = MC_dwarf_attr_string(&child, DW_AT_name);
570 member->name = xbt_strdup(name);
574 member->byte_size = MC_dwarf_attr_uint(&child, DW_AT_byte_size, 0);
575 member->element_count = -1;
576 member->dw_type_id = MC_dwarf_at_type(&child);
577 member->members = NULL;
578 member->is_pointer_type = 0;
581 if(dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
582 xbt_die("Can't groke DW_AT_data_bit_offset.");
585 MC_dwarf_fill_member_location(type, member, &child);
587 if (!member->dw_type_id) {
588 xbt_die("Missing type for member %s of <%p>%s", member->name, type->id, type->name);
591 xbt_dynar_push(type->members, &member);
596 /** \brief Create a MC type object from a DIE
598 * \param info current object info object
599 * \param DIE (for a given type);
600 * \param unit compilation unit of the current DIE
601 * \return MC representation of the type
603 static dw_type_t MC_dwarf_die_to_type(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit) {
605 dw_type_t type = xbt_new0(s_dw_type_t, 1);
610 type->element_count = -1;
611 type->dw_type_id = NULL;
612 type->members = NULL;
613 type->is_pointer_type = 0;
616 type->type = dwarf_tag(die);
619 type->id = (void *) dwarf_dieoffset(die);
621 const char* name = MC_dwarf_attr_string(die, DW_AT_name);
623 type->name = xbt_strdup(name);
626 XBT_DEBUG("Processing type <%p>%s", type->id, type->name);
628 type->dw_type_id = MC_dwarf_at_type(die);
630 // Computation of the byte_size;
631 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
632 type->byte_size = MC_dwarf_attr_uint(die, DW_AT_byte_size, 0);
633 else if (type->type == DW_TAG_array_type || type->type==DW_TAG_structure_type || type->type==DW_TAG_class_type) {
635 if (dwarf_aggregate_size(die, &size)==0) {
636 type->byte_size = size;
640 switch (type->type) {
641 case DW_TAG_array_type:
642 type->element_count = MC_dwarf_array_element_count(die, unit);
643 // TODO, handle DW_byte_stride and (not) DW_bit_stride
646 case DW_TAG_pointer_type:
647 case DW_TAG_reference_type:
648 case DW_TAG_rvalue_reference_type:
649 type->is_pointer_type = 1;
652 case DW_TAG_structure_type:
653 case DW_TAG_union_type:
654 case DW_TAG_class_type:
655 MC_dwarf_add_members(info, die, unit, type);
661 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit) {
662 dw_type_t type = MC_dwarf_die_to_type(info, die, unit);
664 char* key = bprintf("%" PRIx64, (uint64_t) type->id);
665 xbt_dict_set(info->types, key, type, NULL);
667 if(type->name && type->byte_size!=0) {
668 xbt_dict_set(info->types_by_name, type->name, type, NULL);
672 /** \brief Convert libdw location expresion elment into native one (or NULL in some cases) */
673 static dw_location_t MC_dwarf_get_expression_element(Dwarf_Op* op) {
674 dw_location_t element = xbt_new0(s_dw_location_t, 1);
675 uint8_t atom = op->atom;
676 if (atom >= DW_OP_reg0 && atom<= DW_OP_reg31) {
677 element->type = e_dw_register;
678 element->location.reg = atom - DW_OP_reg0;
680 else if (atom >= DW_OP_breg0 && atom<= DW_OP_breg31) {
681 element->type = e_dw_bregister_op;
682 element->location.reg = atom - DW_OP_breg0;
683 element->location.breg_op.offset = op->number;
685 else if (atom >= DW_OP_lit0 && atom<= DW_OP_lit31) {
686 element->type = e_dw_lit;
687 element->location.reg = atom - DW_OP_lit0;
691 element->type = e_dw_fbregister_op;
692 element->location.fbreg_op = op->number;
695 element->type = e_dw_piece;
696 element->location.piece = op->number;
698 case DW_OP_plus_uconst:
699 element->type = e_dw_plus_uconst;
700 element->location.plus_uconst = op->number;
703 element->type = e_dw_arithmetic;
704 element->location.arithmetic = xbt_strdup("abs");
707 element->type = e_dw_arithmetic;
708 element->location.arithmetic = xbt_strdup("and");
711 element->type = e_dw_arithmetic;
712 element->location.arithmetic = xbt_strdup("div");
715 element->type = e_dw_arithmetic;
716 element->location.arithmetic = xbt_strdup("minus");
719 element->type = e_dw_arithmetic;
720 element->location.arithmetic = xbt_strdup("mod");
723 element->type = e_dw_arithmetic;
724 element->location.arithmetic = xbt_strdup("mul");
727 element->type = e_dw_arithmetic;
728 element->location.arithmetic = xbt_strdup("neg");
731 element->type = e_dw_arithmetic;
732 element->location.arithmetic = xbt_strdup("not");
735 element->type = e_dw_arithmetic;
736 element->location.arithmetic = xbt_strdup("or");
739 element->type = e_dw_arithmetic;
740 element->location.arithmetic = xbt_strdup("plus");
743 case DW_OP_stack_value:
748 case DW_OP_deref_size:
749 element->type = e_dw_deref;
750 element->location.deref_size = (unsigned int short) op->number;
753 element->type = e_dw_deref;
754 element->location.deref_size = sizeof(void *);
757 element->type = e_dw_uconstant;
758 element->location.uconstant.bytes = 1;
759 element->location.uconstant.value = (unsigned long int) op->number;
762 element->type = e_dw_sconstant;
763 element->location.uconstant.bytes = 1;
764 element->location.uconstant.value = (unsigned long int) op->number;
768 element->type = e_dw_uconstant;
769 element->location.uconstant.bytes = 1;
770 element->location.uconstant.value = (unsigned long int) op->number;
773 element->type = e_dw_uconstant;
774 element->location.uconstant.bytes = 2;
775 element->location.uconstant.value = (unsigned long int) op->number;
778 element->type = e_dw_uconstant;
779 element->location.uconstant.bytes = 4;
780 element->location.uconstant.value = (unsigned long int) op->number;
783 element->type = e_dw_uconstant;
784 element->location.uconstant.bytes = 8;
785 element->location.uconstant.value = (unsigned long int) op->number;
789 element->type = e_dw_sconstant;
790 element->location.uconstant.bytes = 1;
791 element->location.uconstant.value = (unsigned long int) op->number;
794 element->type = e_dw_sconstant;
795 element->location.uconstant.bytes = 2;
796 element->location.uconstant.value = (unsigned long int) op->number;
799 element->type = e_dw_sconstant;
800 element->location.uconstant.bytes = 4;
801 element->location.uconstant.value = (unsigned long int) op->number;
804 element->type = e_dw_sconstant;
805 element->location.uconstant.bytes = 8;
806 element->location.uconstant.value = (unsigned long int) op->number;
809 element->type = e_dw_unsupported;
815 /** \brief Convert libdw location expresion into native one */
816 static dw_location_t MC_dwarf_get_expression(Dwarf_Op* expr, size_t len) {
817 dw_location_t loc = xbt_new0(s_dw_location_t, 1);
818 loc->type = e_dw_compose;
819 loc->location.compose = xbt_dynar_new(sizeof(dw_location_t), NULL);
822 for (i=0; i!=len; ++i) {
823 dw_location_t element = MC_dwarf_get_expression_element(expr+i);
825 xbt_dynar_push(loc->location.compose, &element);
831 static int mc_anonymous_variable_index = 0;
833 static dw_variable_t MC_die_to_variable(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame) {
835 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
838 Dwarf_Attribute attr_location;
839 if (dwarf_attr(die, DW_AT_location, &attr_location)==NULL) {
840 // No location: do not add it ?
844 dw_variable_t variable = xbt_new0(s_dw_variable_t, 1);
845 variable->dwarf_offset = dwarf_dieoffset(die);
846 variable->global = frame == NULL; // Can be override base on DW_AT_location
847 variable->name = xbt_strdup(MC_dwarf_attr_string(die, DW_AT_name));
848 variable->type_origin = MC_dwarf_at_type(die);
850 int form = dwarf_whatform(&attr_location);
851 int klass = form == DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
853 case MC_DW_CLASS_EXPRLOC:
854 case MC_DW_CLASS_BLOCK:
855 // Location expression:
859 if (dwarf_getlocation(&attr_location, &expr, &len)) {
861 "Could not read location expression in DW_AT_location of variable <%p>%s",
862 (void*) variable->dwarf_offset, variable->name);
865 if (len==1 && expr[0].atom == DW_OP_addr) {
866 variable->global = 1;
867 Dwarf_Off offset = expr[0].number;
868 // TODO, Why is this different base on the object?
869 Dwarf_Off base = strcmp(info->file_name, xbt_binary_name) !=0 ? (Dwarf_Off) info->start_exec : 0;
870 variable->address = (void*) (base + offset);
872 variable->location = MC_dwarf_get_expression(expr, len);
877 case MC_DW_CLASS_LOCLISTPTR:
878 case MC_DW_CLASS_CONSTANT:
879 // Reference to location list:
880 variable->location = MC_dwarf_get_location_list(info, die, &attr_location);
883 xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location in <%p>%s",
884 form, form, klass, klass, (void*) variable->dwarf_offset, variable->name);
887 // The current code needs a variable name,
888 // generate a fake one:
889 if(!variable->name) {
890 variable->name = bprintf("@anonymous#%i", mc_anonymous_variable_index++);
896 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame) {
897 dw_variable_t variable = MC_die_to_variable(info, die, unit, frame);
900 MC_dwarf_register_variable(info, frame, variable);
903 static void MC_dwarf_handle_subprogram_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t parent_frame) {
904 dw_frame_t frame = xbt_new0(s_dw_frame_t, 1);
906 frame->start = dwarf_dieoffset(die);
908 const char* name = MC_dwarf_at_linkage_name(die);
910 name = MC_dwarf_attr_string(die, DW_AT_name);
911 frame->name = xbt_strdup(name);
913 // This is the base address for DWARF addresses.
914 // Relocated addresses are offset from this base address.
915 // See DWARF4 spec 7.5
916 void* base = info->flags & MC_OBJECT_INFO_EXECUTABLE ? 0 : MC_object_base_address(info);
918 // Variables are filled in the (recursive) call of MC_dwarf_handle_children:
919 frame->variables = xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
920 frame->high_pc = ((char*) base) + MC_dwarf_attr_addr(die, DW_AT_high_pc);
921 frame->low_pc = ((char*) base) + MC_dwarf_attr_addr(die, DW_AT_low_pc);
922 frame->frame_base = MC_dwarf_at_location(info, die, DW_AT_frame_base);
923 frame->end = -1; // This one is now useless:
926 MC_dwarf_handle_children(info, die, unit, frame);
929 xbt_dict_set(info->local_variables, frame->name, frame, NULL);
932 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame) {
935 for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
936 MC_dwarf_handle_die(info, &child, unit, frame);
940 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame) {
941 int tag = dwarf_tag(die);
943 case DW_TAG_array_type:
944 case DW_TAG_class_type:
945 case DW_TAG_enumeration_type:
947 case DW_TAG_pointer_type:
948 case DW_TAG_string_type:
949 case DW_TAG_structure_type:
950 case DW_TAG_subroutine_type:
951 case DW_TAG_union_type:
952 case DW_TAG_ptr_to_member_type:
953 case DW_TAG_set_type:
954 case DW_TAG_subrange_type:
955 case DW_TAG_base_type:
956 case DW_TAG_const_type:
957 case DW_TAG_file_type:
958 case DW_TAG_packed_type:
959 case DW_TAG_volatile_type:
960 case DW_TAG_restrict_type:
961 case DW_TAG_interface_type:
962 case DW_TAG_unspecified_type:
963 case DW_TAG_mutable_type:
964 case DW_TAG_shared_type:
965 MC_dwarf_handle_type_die(info, die, unit);
967 case DW_TAG_subprogram:
968 MC_dwarf_handle_subprogram_die(info, die, unit, frame);
970 // case DW_TAG_formal_parameter:
971 case DW_TAG_variable:
972 case DW_TAG_formal_parameter:
973 MC_dwarf_handle_variable_die(info, die, unit, frame);
977 // Recursive processing of children DIE:
978 MC_dwarf_handle_children(info, die, unit, frame);
981 void MC_dwarf_get_variables(mc_object_info_t info) {
982 int fd = open(info->file_name, O_RDONLY);
984 xbt_die("Could not open file %s", info->file_name);
986 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
988 xbt_die("Your program must be compiled with -g");
991 Dwarf_Off offset = 0;
992 Dwarf_Off next_offset = 0;
994 while (dwarf_nextcu (dwarf, offset, &next_offset, &length, NULL, NULL, NULL) == 0) {
997 if(dwarf_offdie(dwarf, offset+length, &die)!=NULL) {
998 MC_dwarf_handle_die(info, &die, &die, NULL);
1000 offset = next_offset;