1 /* Copyright (c) 2008-2013. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include <elfutils/libdw.h>
12 #include <simgrid_config.h>
14 #include <xbt/sysdep.h>
16 #include "mc_private.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing");
20 /** \brief The default DW_TAG_lower_bound for a given DW_AT_language.
22 * The default for a given language is defined in the DWARF spec.
24 static uint64_t MC_dwarf_default_lower_bound(int lang);
26 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit);
28 /** \brief Computes the number of elements of a given DW_TAG_array_type
31 static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit);
33 /** \brief Checks if a given tag is a (known) type tag.
35 static int MC_dwarf_tag_type(int tag);
36 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame);
37 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit);
38 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame);
39 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame);
40 static dw_location_t MC_dwarf_get_expression(Dwarf_Op* expr, size_t len);
41 static Dwarf_Die* MC_dwarf_resolve_die(Dwarf_Die* die, int attribute);
42 static char* MC_dwarf_at_type(Dwarf_Die* die);
44 const char* MC_dwarf_attrname(int attr) {
46 #include "mc_dwarf_attrnames.h"
48 return "DW_AT_unkown";
52 const char* MC_dwarf_tagname(int tag) {
54 #include "mc_dwarf_tagnames.h"
56 return "DW_TAG_invalid";
58 return "DW_TAG_unkown";
62 static inline const char* MC_dwarf_die_tagname(Dwarf_Die* die) {
63 return MC_dwarf_tagname(dwarf_tag(die));
68 static const char* MC_dwarf_attr_string(Dwarf_Die* die, int attribute) {
70 if (!dwarf_attr_integrate(die, attribute, &attr)) {
73 return dwarf_formstring(&attr);
77 /** \brief Get the linkage name (DW_AT_linkage_name or DW_AR_MIPS_linkage_name)
79 static const char* MC_dwarf_at_linkage_name(Dwarf_Die* die) {
80 const char* name = MC_dwarf_attr_string(die, DW_AT_linkage_name);
82 name = MC_dwarf_attr_string(die, DW_AT_MIPS_linkage_name);
86 static dw_location_t MC_dwarf_resolve_location_list(mc_object_info_t info, Dwarf_Word offset) {
87 char *key = bprintf("%ld", (long) offset);
88 dw_location_t loc = xbt_new0(s_dw_location_t, 1);
89 loc->type = e_dw_loclist;
90 loc->location.loclist = (xbt_dynar_t)xbt_dict_get_or_null(info->location_list, key);
91 if (!loc->location.loclist)
92 XBT_INFO("Key not found in loclist");
97 static dw_location_t MC_dwarf_get_location(Dwarf_Die* die, Dwarf_Attribute* attr, mc_object_info_t info) {
98 int form = dwarf_whatform(attr);
100 case DW_FORM_exprloc:
101 case DW_FORM_block1: // not in the spec
108 if (dwarf_getlocation(attr, &expr, &len))
109 xbt_die("Could not read location expression");
110 return MC_dwarf_get_expression(expr, len);
112 case DW_FORM_sec_offset:
118 if (!dwarf_formudata(attr, &offset))
119 return MC_dwarf_resolve_location_list(info, offset);
121 xbt_die("Location list not found");
125 xbt_die("Unexpected form %i list for location in attribute %s of <%p>%s",
127 MC_dwarf_attrname(attr->code),
128 (void*) dwarf_dieoffset(die),
129 MC_dwarf_attr_string(die, DW_AT_name));
134 static dw_location_t MC_dwarf_at_location(Dwarf_Die* die, int attribute, mc_object_info_t info) {
135 if(!dwarf_hasattr_integrate(die, attribute))
136 return xbt_new0(s_dw_location_t, 1);
138 Dwarf_Attribute attr;
139 dwarf_attr_integrate(die, attribute, &attr);
140 return MC_dwarf_get_location(die, &attr, info);
143 // Return a new string for the type (NULL if none)
144 static char* MC_dwarf_at_type(Dwarf_Die* die) {
145 Dwarf_Attribute attr;
146 if (dwarf_hasattr_integrate(die, DW_AT_type)) {
147 dwarf_attr_integrate(die, DW_AT_type, &attr);
148 Dwarf_Die subtype_die;
149 if (dwarf_formref_die(&attr, &subtype_die)==NULL) {
150 xbt_die("Could not find DIE for type");
152 Dwarf_Off subtype_global_offset = dwarf_dieoffset(&subtype_die);
153 return bprintf("%" PRIx64 , subtype_global_offset);
158 static uint64_t MC_dwarf_attr_addr(Dwarf_Die* die, int attribute) {
159 Dwarf_Attribute attr;
160 if(dwarf_attr_integrate(die, attribute, &attr)==NULL)
163 if (dwarf_formaddr(&attr, &value) == 0)
164 return (uint64_t) value;
169 static uint64_t MC_dwarf_attr_uint(Dwarf_Die* die, int attribute, uint64_t default_value) {
170 Dwarf_Attribute attr;
171 if (dwarf_attr_integrate(die, attribute, &attr)==NULL)
172 return default_value;
174 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr), &value) == 0 ? (uint64_t) value : default_value;
177 static bool MC_dwarf_attr_flag(Dwarf_Die* die, int attribute, int integrate) {
178 Dwarf_Attribute attr;
179 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
180 : dwarf_attr(die, attribute, &attr))==0)
184 if (dwarf_formflag(&attr, &result))
185 xbt_die("Unexpected form for attribute %s",
186 MC_dwarf_attrname(attribute));
190 static uint64_t MC_dwarf_default_lower_bound(int lang) {
195 case DW_LANG_C_plus_plus:
199 case DW_LANG_ObjC_plus_plus:
205 case DW_LANG_Fortran77:
206 case DW_LANG_Fortran90:
207 case DW_LANG_Fortran95:
208 case DW_LANG_Modula2:
209 case DW_LANG_Pascal83:
211 case DW_LANG_Cobol74:
212 case DW_LANG_Cobol85:
215 xbt_die("No default MT_TAG_lower_bound for language %i and none given", lang);
220 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
221 // Use DW_TAG_count if present:
222 if (dwarf_hasattr_integrate(die, DW_AT_count)) {
223 return MC_dwarf_attr_uint(die, DW_AT_count, 0);
226 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound:
228 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
229 // This is not really 0, but the code expects this (we do not know):
232 uint64_t upper_bound = MC_dwarf_attr_uint(die, DW_AT_upper_bound, -1);
234 uint64_t lower_bound = 0;
235 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
236 lower_bound = MC_dwarf_attr_uint(die, DW_AT_lower_bound, -1);
238 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
240 return upper_bound - lower_bound;
243 static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
244 xbt_assert(dwarf_tag(die)==DW_TAG_array_type,
245 "MC_dwarf_array_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
250 for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
251 int child_tag = dwarf_tag(&child);
252 if (child_tag==DW_TAG_subrange_type ||child_tag==DW_TAG_enumeration_type) {
253 result *= MC_dwarf_subrange_element_count(&child, unit);
261 Dwarf_Off MC_dwarf_resolve_location(unw_cursor_t* c, dw_location_t location, void* frame_pointer_address) {
263 switch (location->type){
265 if (xbt_dynar_length(location->location.compose) > 1){
266 return 0; /* TODO : location list with optimizations enabled */
268 dw_location_t location_entry = xbt_dynar_get_as(location->location.compose, 0, dw_location_t);
269 switch (location_entry->type){
271 unw_get_reg(c, location_entry->location.reg, &res);
273 case e_dw_bregister_op:
274 unw_get_reg(c, location_entry->location.breg_op.reg, &res);
275 return (Dwarf_Off) ((long)res + location_entry->location.breg_op.offset);
277 case e_dw_fbregister_op:
278 if (frame_pointer_address != NULL)
279 return (Dwarf_Off)((char *)frame_pointer_address + location_entry->location.fbreg_op);
283 return 0; /* FIXME : implement other cases (with optimizations enabled) */
293 static void MC_dwarf_fill_member_location(dw_type_t type, dw_type_t member, Dwarf_Die* child) {
294 if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
295 xbt_die("Can't groke DW_AT_data_bit_offset.");
298 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
299 if (type->type != DW_TAG_union_type) {
301 "Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%p>%s",
302 member->name, type->id, type->name);
308 Dwarf_Attribute attr;
309 dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
310 switch (dwarf_whatform(&attr)) {
312 case DW_FORM_exprloc:
316 if (dwarf_getlocation(&attr, &expr, &len)) {
318 "Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%p>%s",
319 MC_dwarf_attr_string(child, DW_AT_name),
320 type->id, type->name);
322 if (len==1 && expr[0].atom == DW_OP_plus_uconst) {
323 member->offset = expr[0].number;
325 xbt_die("Can't groke this location expression yet. %i %i",
326 len==1 , expr[0].atom == DW_OP_plus_uconst);
338 if (!dwarf_formudata(&attr, &offset))
339 member->offset = offset;
341 xbt_die("Cannot get DW_AT_data_member_%s location <%p>%s",
342 MC_dwarf_attr_string(child, DW_AT_name),
343 type->id, type->name);
349 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_type_t type) {
352 xbt_assert(!type->members);
353 type->members = xbt_dynar_new(sizeof(dw_type_t), (void(*)(void*))dw_type_free);
354 for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
355 if (dwarf_tag(&child)==DW_TAG_member) {
356 // TODO, we should use another type (because is is not a type but a member)
357 dw_type_t member = xbt_new0(s_dw_type_t, 1);
361 const char* name = MC_dwarf_attr_string(&child, DW_AT_name);
363 member->name = xbt_strdup(name);
367 member->byte_size = MC_dwarf_attr_uint(&child, DW_AT_byte_size, 0);
368 member->element_count = -1;
369 member->dw_type_id = MC_dwarf_at_type(&child);
370 member->members = NULL;
371 member->is_pointer_type = 0;
374 if(dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
375 xbt_die("Can't groke DW_AT_data_bit_offset.");
378 MC_dwarf_fill_member_location(type, member, &child);
380 if (!member->dw_type_id) {
381 xbt_die("Missing type for member %s of <%p>%s", member->name, type->id, type->name);
384 xbt_dynar_push(type->members, &member);
389 static dw_type_t MC_dwarf_die_to_type(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit) {
391 dw_type_t type = xbt_new0(s_dw_type_t, 1);
396 type->element_count = -1;
397 type->dw_type_id = NULL;
398 type->members = NULL;
399 type->is_pointer_type = 0;
402 type->type = dwarf_tag(die);
405 type->id = (void *) dwarf_dieoffset(die);
407 const char* name = MC_dwarf_attr_string(die, DW_AT_name);
409 type->name = xbt_strdup(name);
412 XBT_DEBUG("Processing type <%p>%s", type->id, type->name);
414 type->dw_type_id = MC_dwarf_at_type(die);
416 // Computation of the byte_size;
417 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
418 type->byte_size = MC_dwarf_attr_uint(die, DW_AT_byte_size, 0);
419 else if (type->type == DW_TAG_array_type || type->type==DW_TAG_structure_type || type->type==DW_TAG_class_type) {
421 if (dwarf_aggregate_size(die, &size)==0) {
422 type->byte_size = size;
426 switch (type->type) {
427 case DW_TAG_array_type:
428 type->element_count = MC_dwarf_array_element_count(die, unit);
429 // TODO, handle DW_byte_stride and (not) DW_bit_stride
432 case DW_TAG_pointer_type:
433 case DW_TAG_reference_type:
434 case DW_TAG_rvalue_reference_type:
435 type->is_pointer_type = 1;
438 case DW_TAG_structure_type:
439 case DW_TAG_union_type:
440 case DW_TAG_class_type:
441 MC_dwarf_add_members(info, die, unit, type);
447 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit) {
448 dw_type_t type = MC_dwarf_die_to_type(info, die, unit);
450 char* key = bprintf("%" PRIx64, (uint64_t) type->id);
451 xbt_dict_set(info->types, key, type, NULL);
454 /** \brief Convert libdw location expresion elment into native one (or NULL in some cases) */
455 static dw_location_t MC_dwarf_get_expression_element(Dwarf_Op* op) {
456 dw_location_t element = xbt_new0(s_dw_location_t, 1);
457 uint8_t atom = op->atom;
458 if (atom >= DW_OP_reg0 && atom<= DW_OP_reg31) {
459 element->type = e_dw_register;
460 element->location.reg = atom - DW_OP_reg0;
462 else if (atom >= DW_OP_breg0 && atom<= DW_OP_breg31) {
463 element->type = e_dw_bregister_op;
464 element->location.reg = atom - DW_OP_breg0;
465 element->location.breg_op.offset = op->number;
467 else if (atom >= DW_OP_lit0 && atom<= DW_OP_lit31) {
468 element->type = e_dw_lit;
469 element->location.reg = atom - DW_OP_lit0;
473 element->type = e_dw_fbregister_op;
474 element->location.fbreg_op = op->number;
477 element->type = e_dw_piece;
478 element->location.piece = op->number;
480 case DW_OP_plus_uconst:
481 element->type = e_dw_plus_uconst;
482 element->location.plus_uconst = op->number;
485 element->type = e_dw_arithmetic;
486 element->location.arithmetic = xbt_strdup("abs");
489 element->type = e_dw_arithmetic;
490 element->location.arithmetic = xbt_strdup("and");
493 element->type = e_dw_arithmetic;
494 element->location.arithmetic = xbt_strdup("div");
497 element->type = e_dw_arithmetic;
498 element->location.arithmetic = xbt_strdup("minus");
501 element->type = e_dw_arithmetic;
502 element->location.arithmetic = xbt_strdup("mod");
505 element->type = e_dw_arithmetic;
506 element->location.arithmetic = xbt_strdup("mul");
509 element->type = e_dw_arithmetic;
510 element->location.arithmetic = xbt_strdup("neg");
513 element->type = e_dw_arithmetic;
514 element->location.arithmetic = xbt_strdup("not");
517 element->type = e_dw_arithmetic;
518 element->location.arithmetic = xbt_strdup("or");
521 element->type = e_dw_arithmetic;
522 element->location.arithmetic = xbt_strdup("plus");
525 case DW_OP_stack_value:
530 case DW_OP_deref_size:
531 element->type = e_dw_deref;
532 element->location.deref_size = (unsigned int short) op->number;
535 element->type = e_dw_deref;
536 element->location.deref_size = sizeof(void *);
539 element->type = e_dw_uconstant;
540 element->location.uconstant.bytes = 1;
541 element->location.uconstant.value = (unsigned long int) op->number;
544 element->type = e_dw_sconstant;
545 element->location.uconstant.bytes = 1;
546 element->location.uconstant.value = (unsigned long int) op->number;
550 element->type = e_dw_uconstant;
551 element->location.uconstant.bytes = 1;
552 element->location.uconstant.value = (unsigned long int) op->number;
555 element->type = e_dw_uconstant;
556 element->location.uconstant.bytes = 2;
557 element->location.uconstant.value = (unsigned long int) op->number;
560 element->type = e_dw_uconstant;
561 element->location.uconstant.bytes = 4;
562 element->location.uconstant.value = (unsigned long int) op->number;
565 element->type = e_dw_uconstant;
566 element->location.uconstant.bytes = 8;
567 element->location.uconstant.value = (unsigned long int) op->number;
571 element->type = e_dw_sconstant;
572 element->location.uconstant.bytes = 1;
573 element->location.uconstant.value = (unsigned long int) op->number;
576 element->type = e_dw_sconstant;
577 element->location.uconstant.bytes = 2;
578 element->location.uconstant.value = (unsigned long int) op->number;
581 element->type = e_dw_sconstant;
582 element->location.uconstant.bytes = 4;
583 element->location.uconstant.value = (unsigned long int) op->number;
586 element->type = e_dw_sconstant;
587 element->location.uconstant.bytes = 8;
588 element->location.uconstant.value = (unsigned long int) op->number;
591 element->type = e_dw_unsupported;
597 /** \brief Convert libdw location expresion into native one */
598 static dw_location_t MC_dwarf_get_expression(Dwarf_Op* expr, size_t len) {
599 dw_location_t loc = xbt_new0(s_dw_location_t, 1);
600 loc->type = e_dw_compose;
601 loc->location.compose = xbt_dynar_new(sizeof(dw_location_t), NULL);
604 for (i=0; i!=len; ++i) {
605 dw_location_t element = MC_dwarf_get_expression_element(expr+i);
607 xbt_dynar_push(loc->location.compose, &element);
613 static dw_variable_t MC_die_to_variable(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame) {
615 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
618 Dwarf_Attribute attr_location;
619 if (dwarf_attr(die, DW_AT_location, &attr_location)==NULL) {
620 // No location: do not add it ?
624 dw_variable_t variable = xbt_new0(s_dw_variable_t, 1);
625 variable->dwarf_offset = dwarf_dieoffset(die);
626 variable->global = frame == NULL; // Can be override base on DW_AT_location
627 variable->name = xbt_strdup(MC_dwarf_attr_string(die, DW_AT_name));
628 variable->type_origin = MC_dwarf_at_type(die);
629 variable->address.address = NULL;
632 switch (form = dwarf_whatform(&attr_location)) {
633 case DW_FORM_exprloc:
634 case DW_FORM_block1: // Not in the spec but found in the wild.
638 if (dwarf_getlocation(&attr_location, &expr, &len)) {
640 "Could not read location expression in DW_AT_location of variable <%p>%s",
641 (void*) variable->dwarf_offset, variable->name);
644 if (len==1 && expr[0].atom == DW_OP_addr) {
645 variable->global = 1;
646 Dwarf_Off offset = expr[0].number;
647 // TODO, Why is this different base on the object?
648 Dwarf_Off base = strcmp(info->file_name, xbt_binary_name) !=0 ? (Dwarf_Off) info->start_text : 0;
649 variable->address.address = (void*) (base + offset);
651 variable->address.location = MC_dwarf_get_expression(expr, len);
656 case DW_FORM_sec_offset: // type loclistptr
658 xbt_die("Do not handle loclist locations yet");
661 xbt_die("Unexpected form %i list for location in <%p>%s",
662 form, (void*) variable->dwarf_offset, variable->name);
668 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame) {
669 dw_variable_t variable = MC_die_to_variable(info, die, unit, frame);
672 MC_dwarf_register_variable(info, frame, variable);
675 static void MC_dwarf_handle_subprogram_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t parent_frame) {
676 dw_frame_t frame = xbt_new0(s_dw_frame_t, 1);
678 frame->start = dwarf_dieoffset(die);
680 const char* name = MC_dwarf_at_linkage_name(die);
682 name = MC_dwarf_attr_string(die, DW_AT_name);
683 frame->name = xbt_strdup(name);
685 // Variables are filled in the (recursive) call of MC_dwarf_handle_children:
686 frame->variables = xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
687 frame->high_pc = (void*) MC_dwarf_attr_addr(die, DW_AT_high_pc);
688 frame->low_pc = (void*) MC_dwarf_attr_addr(die, DW_AT_low_pc);
689 frame->frame_base = MC_dwarf_at_location(die, DW_AT_frame_base, info);
690 frame->end = -1; // This one is now useless:
693 MC_dwarf_handle_children(info, die, unit, frame);
696 xbt_dict_set(info->local_variables, frame->name, frame, NULL);
699 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame) {
702 for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
703 MC_dwarf_handle_die(info, &child, unit, frame);
707 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame) {
708 int tag = dwarf_tag(die);
710 case DW_TAG_array_type:
711 case DW_TAG_class_type:
712 case DW_TAG_enumeration_type:
714 case DW_TAG_pointer_type:
715 case DW_TAG_string_type:
716 case DW_TAG_structure_type:
717 case DW_TAG_subroutine_type:
718 case DW_TAG_union_type:
719 case DW_TAG_ptr_to_member_type:
720 case DW_TAG_set_type:
721 case DW_TAG_subrange_type:
722 case DW_TAG_base_type:
723 case DW_TAG_const_type:
724 case DW_TAG_file_type:
725 case DW_TAG_packed_type:
726 case DW_TAG_volatile_type:
727 case DW_TAG_restrict_type:
728 case DW_TAG_interface_type:
729 case DW_TAG_unspecified_type:
730 case DW_TAG_mutable_type:
731 case DW_TAG_shared_type:
732 MC_dwarf_handle_type_die(info, die, unit);
734 case DW_TAG_inlined_subroutine:
735 case DW_TAG_subprogram:
736 MC_dwarf_handle_subprogram_die(info, die, unit, frame);
738 // case DW_TAG_formal_parameter:
739 case DW_TAG_variable:
740 MC_dwarf_handle_variable_die(info, die, unit, frame);
744 // Recursive processing of children DIE:
745 MC_dwarf_handle_children(info, die, unit, frame);
748 void MC_dwarf_get_variables_libdw(mc_object_info_t info) {
749 int fd = open(info->file_name, O_RDONLY);
751 xbt_die("Could not open file %s", info->file_name);
753 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
755 xbt_die("Your program must be compiled with -g");
758 Dwarf_Off offset = 0;
759 Dwarf_Off next_offset = 0;
761 while (dwarf_nextcu (dwarf, offset, &next_offset, &length, NULL, NULL, NULL) == 0) {
763 if(dwarf_offdie(dwarf, offset+length, &die)!=NULL) {
764 MC_dwarf_handle_die(info, &die, &die, NULL);
766 offset = next_offset;