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));
64 static int MC_dwarf_tag_type(int tag) {
66 case DW_TAG_array_type:
67 case DW_TAG_class_type:
68 case DW_TAG_enumeration_type:
70 case DW_TAG_pointer_type:
71 case DW_TAG_string_type:
72 case DW_TAG_structure_type:
73 case DW_TAG_subroutine_type:
74 case DW_TAG_union_type:
75 case DW_TAG_ptr_to_member_type:
77 case DW_TAG_subrange_type:
78 case DW_TAG_base_type:
79 case DW_TAG_const_type:
80 case DW_TAG_file_type:
81 case DW_TAG_packed_type:
82 case DW_TAG_volatile_type:
83 case DW_TAG_restrict_type:
84 case DW_TAG_interface_type:
85 case DW_TAG_unspecified_type:
86 case DW_TAG_mutable_type:
87 case DW_TAG_shared_type:
88 case DW_TAG_rvalue_reference_type:
95 static const char* MC_dwarf_attr_string(Dwarf_Die* die, int attribute) {
97 if (!dwarf_attr_integrate(die, attribute, &attr)) {
100 return dwarf_formstring(&attr);
104 // Return a new string for the type (NULL if none)
105 static char* MC_dwarf_at_type(Dwarf_Die* die) {
106 Dwarf_Attribute attr;
107 if (dwarf_hasattr_integrate(die, DW_AT_type)) {
108 dwarf_attr_integrate(die, DW_AT_type, &attr);
109 Dwarf_Die subtype_die;
110 if (dwarf_formref_die(&attr, &subtype_die)==NULL) {
111 xbt_die("Could not find DIE for type");
113 Dwarf_Off subtype_global_offset = dwarf_dieoffset(&subtype_die);
114 return bprintf("%" PRIx64 , subtype_global_offset);
119 static uint64_t MC_dwarf_attr_uint(Dwarf_Die* die, int attribute, uint64_t default_value) {
120 Dwarf_Attribute attr;
122 return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr), &value) == 0 ? (uint64_t) value : default_value;
125 static uint64_t MC_dwarf_default_lower_bound(int lang) {
130 case DW_LANG_C_plus_plus:
134 case DW_LANG_ObjC_plus_plus:
140 case DW_LANG_Fortran77:
141 case DW_LANG_Fortran90:
142 case DW_LANG_Fortran95:
143 case DW_LANG_Modula2:
144 case DW_LANG_Pascal83:
146 case DW_LANG_Cobol74:
147 case DW_LANG_Cobol85:
150 xbt_die("No default MT_TAG_lower_bound for language %i and none given", lang);
155 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
156 // Use DW_TAG_count if present:
157 if (dwarf_hasattr_integrate(die, DW_AT_count)) {
158 return MC_dwarf_attr_uint(die, DW_AT_count, 0);
161 // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound:
163 if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
164 // This is not really 0, but the code expects this (we do not know):
167 uint64_t upper_bound = MC_dwarf_attr_uint(die, DW_AT_upper_bound, -1);
169 uint64_t lower_bound = 0;
170 if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
171 lower_bound = MC_dwarf_attr_uint(die, DW_AT_lower_bound, -1);
173 lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
175 return upper_bound - lower_bound;
178 static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
179 xbt_assert(dwarf_tag(die)==DW_TAG_array_type,
180 "MC_dwarf_array_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
185 for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
186 int child_tag = dwarf_tag(&child);
187 if (child_tag==DW_TAG_subrange_type ||child_tag==DW_TAG_enumeration_type) {
188 result *= MC_dwarf_subrange_element_count(&child, unit);
194 static void MC_dwarf_fill_member_location(dw_type_t type, dw_type_t member, Dwarf_Die* child) {
195 if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
196 xbt_die("Can't groke DW_AT_data_bit_offset.");
199 if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
200 if (type->type != DW_TAG_union_type) {
202 "Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%p>%s",
203 member->name, type->id, type->name);
209 Dwarf_Attribute attr;
210 dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
211 switch (dwarf_whatform(&attr)) {
213 case DW_FORM_exprloc:
215 //TODO, location can be an integer as well
218 if (dwarf_getlocation(&attr, &expr, &len)) {
220 "Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%p>%s",
221 MC_dwarf_attr_string(child, DW_AT_name),
222 type->id, type->name);
224 if (len==1 && expr[0].atom == DW_OP_plus_uconst) {
225 member->offset = expr[0].number;
227 xbt_die("Can't groke this location expression yet. %i %i",
228 len==1 , expr[0].atom == DW_OP_plus_uconst);
240 if (!dwarf_formudata(&attr, &offset))
241 member->offset = offset;
243 xbt_die("Cannot get DW_AT_data_member_%s location <%p>%s",
244 MC_dwarf_attr_string(child, DW_AT_name),
245 type->id, type->name);
251 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_type_t type) {
254 xbt_assert(!type->members);
255 type->members = xbt_dynar_new(sizeof(dw_type_t), (void(*)(void*))dw_type_free);
256 for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
257 if (dwarf_tag(&child)==DW_TAG_member) {
258 // TODO, we should use another type (because is is not a type but a member)
259 dw_type_t member = xbt_new0(s_dw_type_t, 1);
263 const char* name = MC_dwarf_attr_string(&child, DW_AT_name);
265 member->name = xbt_strdup(name);
269 member->byte_size = MC_dwarf_attr_uint(&child, DW_AT_byte_size, 0);
270 member->element_count = -1;
271 member->dw_type_id = MC_dwarf_at_type(&child);
272 member->members = NULL;
273 member->is_pointer_type = 0;
276 if(dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
277 xbt_die("Can't groke DW_AT_data_bit_offset.");
280 MC_dwarf_fill_member_location(type, member, &child);
282 xbt_dynar_push(type->members, &member);
287 static dw_type_t MC_dwarf_die_to_type(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit) {
289 dw_type_t type = xbt_new0(s_dw_type_t, 1);
294 type->element_count = -1;
295 type->dw_type_id = NULL;
296 type->members = NULL;
297 type->is_pointer_type = 0;
300 type->type = dwarf_tag(die);
303 type->id = (void *) dwarf_dieoffset(die);
305 const char* name = MC_dwarf_attr_string(die, DW_AT_name);
307 type->name = xbt_strdup(name);
310 XBT_DEBUG("Processing type <%p>%s", type->id, type->name);
312 type->dw_type_id = MC_dwarf_at_type(die);
314 // Computation of the byte_size;
315 if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
316 type->byte_size = MC_dwarf_attr_uint(die, DW_AT_byte_size, 0);
317 else if (type->type == DW_TAG_array_type || type->type==DW_TAG_structure_type || type->type==DW_TAG_class_type) {
319 if (dwarf_aggregate_size(die, &size)==0) {
320 type->byte_size = size;
324 switch (type->type) {
325 case DW_TAG_array_type:
326 type->element_count = MC_dwarf_array_element_count(die, unit);
327 // TODO, handle DW_byte_stride and (not) DW_bit_stride
330 case DW_TAG_pointer_type:
331 case DW_TAG_reference_type:
332 case DW_TAG_rvalue_reference_type:
333 type->is_pointer_type = 1;
336 case DW_TAG_structure_type:
337 case DW_TAG_union_type:
338 case DW_TAG_class_type:
339 MC_dwarf_add_members(info, die, unit, type);
345 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit) {
346 dw_type_t type = MC_dwarf_die_to_type(info, die, unit);
348 char* key = bprintf("%" PRIx64, (uint64_t) type->id);
349 xbt_dict_set(info->types, key, type, NULL);
353 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit) {
356 for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
357 MC_dwarf_handle_die(info, &child, unit);
361 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit) {
362 int tag = dwarf_tag(die);
365 if (MC_dwarf_tag_type(tag))
366 MC_dwarf_handle_type_die(info, die, unit);
368 // Other tag-specific handling:
373 // Recursive processing od children DIE:
374 MC_dwarf_handle_children(info, die, unit);
377 void MC_dwarf_get_variables_libdw(mc_object_info_t info) {
378 int fd = open(info->file_name, O_RDONLY);
380 xbt_die("Could not open file %s", info->file_name);
382 Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
384 xbt_die("Your program must be compiled with -g");
387 Dwarf_Off offset = 0;
388 Dwarf_Off next_offset = 0;
390 while (dwarf_nextcu (dwarf, offset, &next_offset, &length, NULL, NULL, NULL) == 0) {
392 if(dwarf_offdie(dwarf, offset+length, &die)!=NULL) {
393 MC_dwarf_handle_die(info, &die, &die);
395 offset = next_offset;