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);
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);
39 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit);
40 static Dwarf_Die* MC_dwarf_resolve_die(Dwarf_Die* die, int attribute);
42 const char* MC_dwarf_attrname(int attr) {
44 #include "mc_dwarf_attrnames.h"
46 return "DW_AT_unkown";
50 const char* MC_dwarf_tagname(int tag) {
52 #include "mc_dwarf_tagnames.h"
54 return "DW_TAG_invalid";
56 return "DW_TAG_unkown";
60 static inline const char* MC_dwarf_die_tagname(Dwarf_Die* die) {
61 return MC_dwarf_tagname(dwarf_tag(die));
66 static const char* MC_dwarf_attr_string(Dwarf_Die* die, int attribute) {
68 if (!dwarf_attr_integrate(die, attribute, &attr)) {
71 return dwarf_formstring(&attr);
75 // Return a new string for the type (NULL if none)
76 static char* MC_dwarf_at_type(Dwarf_Die* die) {
78 if (dwarf_hasattr_integrate(die, DW_AT_type)) {
79 dwarf_attr_integrate(die, DW_AT_type, &attr);
80 Dwarf_Die subtype_die;
81 if (dwarf_formref_die(&attr, &subtype_die)==NULL) {
82 xbt_die("Could not find DIE for type");
84 Dwarf_Off subtype_global_offset = dwarf_dieoffset(&subtype_die);
85 return bprintf("%" PRIx64 , subtype_global_offset);
90 static uint64_t MC_dwarf_attr_uint(Dwarf_Die* die, int attribute, uint64_t default_value) {
93 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr), &value) == 0 ? (uint64_t) value : default_value;
96 static bool MC_dwarf_attr_flag(Dwarf_Die* die, int attribute, int integrate) {
98 if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
99 : dwarf_attr(die, attribute, &attr))==0)
103 if (dwarf_formflag(&attr, &result))
104 xbt_die("Unexpected form for attribute %s",
105 MC_dwarf_attrname(attribute));
109 static uint64_t MC_dwarf_default_lower_bound(int lang) {
114 case DW_LANG_C_plus_plus:
118 case DW_LANG_ObjC_plus_plus:
124 case DW_LANG_Fortran77:
125 case DW_LANG_Fortran90:
126 case DW_LANG_Fortran95:
127 case DW_LANG_Modula2:
128 case DW_LANG_Pascal83:
130 case DW_LANG_Cobol74:
131 case DW_LANG_Cobol85:
134 xbt_die("No default MT_TAG_lower_bound for language %i and none given", lang);
139 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
140 // Use DW_TAG_count if present:
141 if (dwarf_hasattr_integrate(die, DW_AT_count)) {
142 return MC_dwarf_attr_uint(die, DW_AT_count, 0);
145 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound:
147 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
148 // This is not really 0, but the code expects this (we do not know):
151 uint64_t upper_bound = MC_dwarf_attr_uint(die, DW_AT_upper_bound, -1);
153 uint64_t lower_bound = 0;
154 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
155 lower_bound = MC_dwarf_attr_uint(die, DW_AT_lower_bound, -1);
157 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
159 return upper_bound - lower_bound;
162 static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
163 xbt_assert(dwarf_tag(die)==DW_TAG_array_type,
164 "MC_dwarf_array_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
169 for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
170 int child_tag = dwarf_tag(&child);
171 if (child_tag==DW_TAG_subrange_type ||child_tag==DW_TAG_enumeration_type) {
172 result *= MC_dwarf_subrange_element_count(&child, unit);
180 Dwarf_Off MC_dwarf_resolve_location(unw_cursor_t* c, dw_location_t location, void* frame_pointer_address) {
182 switch (location->type){
184 if (xbt_dynar_length(location->location.compose) > 1){
185 return 0; /* TODO : location list with optimizations enabled */
187 dw_location_t location_entry = xbt_dynar_get_as(location->location.compose, 0, dw_location_t);
188 switch (location_entry->type){
190 unw_get_reg(c, location_entry->location.reg, &res);
192 case e_dw_bregister_op:
193 unw_get_reg(c, location_entry->location.breg_op.reg, &res);
194 return (Dwarf_Off) ((long)res + location_entry->location.breg_op.offset);
196 case e_dw_fbregister_op:
197 if (frame_pointer_address != NULL)
198 return (Dwarf_Off)((char *)frame_pointer_address + location_entry->location.fbreg_op);
202 return 0; /* FIXME : implement other cases (with optimizations enabled) */
212 static void MC_dwarf_fill_member_location(dw_type_t type, dw_type_t member, Dwarf_Die* child) {
213 if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
214 xbt_die("Can't groke DW_AT_data_bit_offset.");
217 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
218 if (type->type != DW_TAG_union_type) {
220 "Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%p>%s",
221 member->name, type->id, type->name);
227 Dwarf_Attribute attr;
228 dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
229 switch (dwarf_whatform(&attr)) {
231 case DW_FORM_exprloc:
235 if (dwarf_getlocation(&attr, &expr, &len)) {
237 "Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%p>%s",
238 MC_dwarf_attr_string(child, DW_AT_name),
239 type->id, type->name);
241 if (len==1 && expr[0].atom == DW_OP_plus_uconst) {
242 member->offset = expr[0].number;
244 xbt_die("Can't groke this location expression yet. %i %i",
245 len==1 , expr[0].atom == DW_OP_plus_uconst);
257 if (!dwarf_formudata(&attr, &offset))
258 member->offset = offset;
260 xbt_die("Cannot get DW_AT_data_member_%s location <%p>%s",
261 MC_dwarf_attr_string(child, DW_AT_name),
262 type->id, type->name);
268 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_type_t type) {
271 xbt_assert(!type->members);
272 type->members = xbt_dynar_new(sizeof(dw_type_t), (void(*)(void*))dw_type_free);
273 for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
274 if (dwarf_tag(&child)==DW_TAG_member) {
275 // TODO, we should use another type (because is is not a type but a member)
276 dw_type_t member = xbt_new0(s_dw_type_t, 1);
280 const char* name = MC_dwarf_attr_string(&child, DW_AT_name);
282 member->name = xbt_strdup(name);
286 member->byte_size = MC_dwarf_attr_uint(&child, DW_AT_byte_size, 0);
287 member->element_count = -1;
288 member->dw_type_id = MC_dwarf_at_type(&child);
289 member->members = NULL;
290 member->is_pointer_type = 0;
293 if(dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
294 xbt_die("Can't groke DW_AT_data_bit_offset.");
297 MC_dwarf_fill_member_location(type, member, &child);
299 xbt_dynar_push(type->members, &member);
304 static dw_type_t MC_dwarf_die_to_type(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit) {
306 dw_type_t type = xbt_new0(s_dw_type_t, 1);
311 type->element_count = -1;
312 type->dw_type_id = NULL;
313 type->members = NULL;
314 type->is_pointer_type = 0;
317 type->type = dwarf_tag(die);
320 type->id = (void *) dwarf_dieoffset(die);
322 const char* name = MC_dwarf_attr_string(die, DW_AT_name);
324 type->name = xbt_strdup(name);
327 XBT_DEBUG("Processing type <%p>%s", type->id, type->name);
329 type->dw_type_id = MC_dwarf_at_type(die);
331 // Computation of the byte_size;
332 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
333 type->byte_size = MC_dwarf_attr_uint(die, DW_AT_byte_size, 0);
334 else if (type->type == DW_TAG_array_type || type->type==DW_TAG_structure_type || type->type==DW_TAG_class_type) {
336 if (dwarf_aggregate_size(die, &size)==0) {
337 type->byte_size = size;
341 switch (type->type) {
342 case DW_TAG_array_type:
343 type->element_count = MC_dwarf_array_element_count(die, unit);
344 // TODO, handle DW_byte_stride and (not) DW_bit_stride
347 case DW_TAG_pointer_type:
348 case DW_TAG_reference_type:
349 case DW_TAG_rvalue_reference_type:
350 type->is_pointer_type = 1;
353 case DW_TAG_structure_type:
354 case DW_TAG_union_type:
355 case DW_TAG_class_type:
356 MC_dwarf_add_members(info, die, unit, type);
362 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit) {
363 dw_type_t type = MC_dwarf_die_to_type(info, die, unit);
365 char* key = bprintf("%" PRIx64, (uint64_t) type->id);
366 xbt_dict_set(info->types, key, type, NULL);
369 /** \brief Convert libdw location expresion elment into native one (or NULL in some cases) */
370 static dw_location_t MC_dwarf_get_expression_element(Dwarf_Op* op) {
371 dw_location_t element = xbt_new0(s_dw_location_t, 1);
372 uint8_t atom = op->atom;
373 if (atom >= DW_OP_reg0 && atom<= DW_OP_reg31) {
374 element->type = e_dw_register;
375 element->location.reg = atom - DW_OP_reg0;
377 else if (atom >= DW_OP_breg0 && atom<= DW_OP_breg31) {
378 element->type = e_dw_bregister_op;
379 element->location.reg = atom - DW_OP_breg0;
380 element->location.breg_op.offset = op->number;
382 else if (atom >= DW_OP_lit0 && atom<= DW_OP_lit31) {
383 element->type = e_dw_lit;
384 element->location.reg = atom - DW_OP_lit0;
388 element->type = e_dw_fbregister_op;
389 element->location.fbreg_op = op->number;
392 element->type = e_dw_piece;
393 element->location.piece = op->number;
395 case DW_OP_plus_uconst:
396 element->type = e_dw_plus_uconst;
397 element->location.plus_uconst = op->number;
400 element->type = e_dw_arithmetic;
401 element->location.arithmetic = xbt_strdup("abs");
404 element->type = e_dw_arithmetic;
405 element->location.arithmetic = xbt_strdup("and");
408 element->type = e_dw_arithmetic;
409 element->location.arithmetic = xbt_strdup("div");
412 element->type = e_dw_arithmetic;
413 element->location.arithmetic = xbt_strdup("minus");
416 element->type = e_dw_arithmetic;
417 element->location.arithmetic = xbt_strdup("mod");
420 element->type = e_dw_arithmetic;
421 element->location.arithmetic = xbt_strdup("mul");
424 element->type = e_dw_arithmetic;
425 element->location.arithmetic = xbt_strdup("neg");
428 element->type = e_dw_arithmetic;
429 element->location.arithmetic = xbt_strdup("not");
432 element->type = e_dw_arithmetic;
433 element->location.arithmetic = xbt_strdup("or");
436 element->type = e_dw_arithmetic;
437 element->location.arithmetic = xbt_strdup("plus");
440 case DW_OP_stack_value:
445 case DW_OP_deref_size:
446 element->type = e_dw_deref;
447 element->location.deref_size = (unsigned int short) op->number;
450 element->type = e_dw_deref;
451 element->location.deref_size = sizeof(void *);
454 element->type = e_dw_uconstant;
455 element->location.uconstant.bytes = 1;
456 element->location.uconstant.value = (unsigned long int) op->number;
459 element->type = e_dw_sconstant;
460 element->location.uconstant.bytes = 1;
461 element->location.uconstant.value = (unsigned long int) op->number;
465 element->type = e_dw_uconstant;
466 element->location.uconstant.bytes = 1;
467 element->location.uconstant.value = (unsigned long int) op->number;
470 element->type = e_dw_uconstant;
471 element->location.uconstant.bytes = 2;
472 element->location.uconstant.value = (unsigned long int) op->number;
475 element->type = e_dw_uconstant;
476 element->location.uconstant.bytes = 4;
477 element->location.uconstant.value = (unsigned long int) op->number;
480 element->type = e_dw_uconstant;
481 element->location.uconstant.bytes = 8;
482 element->location.uconstant.value = (unsigned long int) op->number;
486 element->type = e_dw_sconstant;
487 element->location.uconstant.bytes = 1;
488 element->location.uconstant.value = (unsigned long int) op->number;
491 element->type = e_dw_sconstant;
492 element->location.uconstant.bytes = 2;
493 element->location.uconstant.value = (unsigned long int) op->number;
496 element->type = e_dw_sconstant;
497 element->location.uconstant.bytes = 4;
498 element->location.uconstant.value = (unsigned long int) op->number;
501 element->type = e_dw_sconstant;
502 element->location.uconstant.bytes = 8;
503 element->location.uconstant.value = (unsigned long int) op->number;
506 element->type = e_dw_unsupported;
512 /** \brief Convert libdw location expresion into native one */
513 static dw_location_t MC_dwarf_get_expression(Dwarf_Op* expr, size_t len) {
514 dw_location_t loc = xbt_new0(s_dw_location_t, 1);
515 loc->type = e_dw_compose;
516 loc->location.compose = xbt_dynar_new(sizeof(dw_location_t), NULL);
519 for (i=0; i!=len; ++i) {
520 dw_location_t element = MC_dwarf_get_expression_element(expr+i);
522 xbt_dynar_push(loc->location.compose, &element);
528 static dw_variable_t MC_die_to_variable(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame) {
530 if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
533 Dwarf_Attribute attr_location;
534 if (dwarf_attr(die, DW_AT_location, &attr_location)==NULL) {
535 // No location: do not add it ?
539 dw_variable_t variable = xbt_new0(s_dw_variable_t, 1);
540 variable->dwarf_offset = dwarf_dieoffset(die);
541 variable->global = frame == NULL; // Can be override base on DW_AT_location
542 variable->name = xbt_strdup(MC_dwarf_attr_string(die, DW_AT_name));
543 variable->type_origin = MC_dwarf_at_type(die);
544 variable->address.address = NULL;
547 switch (form = dwarf_whatform(&attr_location)) {
548 case DW_FORM_exprloc:
549 case DW_FORM_block1: // Not in the spec but found in the wild.
553 if (dwarf_getlocation(&attr_location, &expr, &len)) {
555 "Could not read location expression in DW_AT_location of variable <%p>%s",
556 (void*) variable->dwarf_offset, variable->name);
559 if (len==1 && expr[0].atom == DW_OP_addr) {
560 variable->global = 1;
561 Dwarf_Off offset = expr[0].number;
562 // TODO, Why is this different base on the object?
563 Dwarf_Off base = strcmp(info->file_name, xbt_binary_name) !=0 ? (Dwarf_Off) info->start_text : 0;
564 variable->address.address = (void*) (base + offset);
566 variable->address.location = MC_dwarf_get_expression(expr, len);
571 case DW_FORM_sec_offset: // type loclistptr
572 xbt_die("Do not handle loclist locations yet");
575 xbt_die("Unexpected form %i list for location in <%p>%s",
576 form, (void*) variable->dwarf_offset, variable->name);
582 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit) {
583 dw_variable_t variable = MC_die_to_variable(info, die, unit, NULL);
587 MC_dwarf_register_global_variable(info, variable);
589 xbt_die("Unexpected non global variable <%p>%s",
590 (void*) dwarf_dieoffset(die),
591 MC_dwarf_attr_string(die, DW_AT_name)
596 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit) {
599 for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
600 MC_dwarf_handle_die(info, &child, unit);
604 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit) {
605 int tag = dwarf_tag(die);
608 case DW_TAG_array_type:
609 case DW_TAG_class_type:
610 case DW_TAG_enumeration_type:
612 case DW_TAG_pointer_type:
613 case DW_TAG_string_type:
614 case DW_TAG_structure_type:
615 case DW_TAG_subroutine_type:
616 case DW_TAG_union_type:
617 case DW_TAG_ptr_to_member_type:
618 case DW_TAG_set_type:
619 case DW_TAG_subrange_type:
620 case DW_TAG_base_type:
621 case DW_TAG_const_type:
622 case DW_TAG_file_type:
623 case DW_TAG_packed_type:
624 case DW_TAG_volatile_type:
625 case DW_TAG_restrict_type:
626 case DW_TAG_interface_type:
627 case DW_TAG_unspecified_type:
628 case DW_TAG_mutable_type:
629 case DW_TAG_shared_type:
630 MC_dwarf_handle_type_die(info, die, unit);
632 case DW_TAG_inlined_subroutine:
633 case DW_TAG_subprogram:
634 // Skip recursive processing
636 case DW_TAG_variable:
637 if (MC_USE_LIBDW_NON_FUNCTION_VARIABLES)
638 MC_dwarf_handle_variable_die(info, die, unit);
642 // Recursive processing of children DIE:
643 MC_dwarf_handle_children(info, die, unit);
646 void MC_dwarf_get_variables_libdw(mc_object_info_t info) {
647 int fd = open(info->file_name, O_RDONLY);
649 xbt_die("Could not open file %s", info->file_name);
651 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
653 xbt_die("Your program must be compiled with -g");
656 Dwarf_Off offset = 0;
657 Dwarf_Off next_offset = 0;
659 while (dwarf_nextcu (dwarf, offset, &next_offset, &length, NULL, NULL, NULL) == 0) {
661 if(dwarf_offdie(dwarf, offset+length, &die)!=NULL) {
662 MC_dwarf_handle_die(info, &die, &die);
664 offset = next_offset;