Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'mc++' into mc-merge
[simgrid.git] / src / mc / mc_dwarf.c
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. */
5
6 #include <stdlib.h>
7 #include <dwarf.h>
8 #include <elfutils/libdw.h>
9 #include <inttypes.h>
10
11 #include <simgrid_config.h>
12 #include <xbt/log.h>
13 #include <xbt/sysdep.h>
14
15 #include "mc_private.h"
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing");
18
19 /** \brief The default DW_TAG_lower_bound for a given DW_AT_language.
20  *
21  *  The default for a given language is defined in the DWARF spec.
22  *
23  *  \param language consant as defined by the DWARf spec
24  */
25 static uint64_t MC_dwarf_default_lower_bound(int lang);
26
27 /** \brief Computes the the element_count of a DW_TAG_enumeration_type DIE
28  *
29  * This is the number of elements in a given array dimension.
30  *
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).
34  *
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
37  */
38 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit);
39
40 /** \brief Computes the number of elements of a given DW_TAG_array_type.
41  *
42  * \param die DIE for the DW_TAG_array_type
43  */
44 static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit);
45
46 /** \brief Process a DIE
47  *
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
52  */
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);
54
55 /** \brief Process a type DIE
56  */
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);
58
59 /** \brief Calls MC_dwarf_handle_die on all childrend of the given die
60  *
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
65  */
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);
67
68 /** \brief Handle a variable (DW_TAG_variable or other)
69  *
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
74  */
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);
76
77 /** \brief Convert a libdw DWARF expression into a MC representation of the location
78  *
79  *  \param expr array of DWARf operations
80  *  \param len  number of elements
81  *  \return a new MC expression
82  */
83 static dw_location_t MC_dwarf_get_expression(Dwarf_Op* expr,  size_t len);
84
85 /** \brief Get the DW_TAG_type of the DIE
86  *
87  *  \param die DIE
88  *  \return DW_TAG_type attribute as a new string (NULL if none)
89  */
90 static char* MC_dwarf_at_type(Dwarf_Die* die);
91
92 /** \brief Get the name of an attribute (DW_AT_*) from its code
93  *
94  *  \param attr attribute code (see the DWARF specification)
95  *  \return name of the attribute
96  */
97 const char* MC_dwarf_attrname(int attr) {
98   switch (attr) {
99 #include "mc_dwarf_attrnames.h"
100   default:
101     return "DW_AT_unknown";
102   }
103 }
104
105 /** \brief Get the name of a dwarf tag (DW_TAG_*) from its code
106  *
107  *  \param tag tag code (see the DWARF specification)
108  *  \return name of the tag
109  */
110 const char* MC_dwarf_tagname(int tag) {
111   switch (tag) {
112 #include "mc_dwarf_tagnames.h"
113   case DW_TAG_invalid:
114     return "DW_TAG_invalid";
115   default:
116     return "DW_TAG_unknown";
117   }
118 }
119
120 /** \brief A class of DWARF tags (DW_TAG_*)
121  */
122 typedef enum mc_tag_class {
123   mc_tag_unknown,
124   mc_tag_type,
125   mc_tag_subprogram,
126   mc_tag_variable,
127   mc_tag_scope,
128   mc_tag_namespace
129 } mc_tag_class;
130
131 static mc_tag_class MC_dwarf_tag_classify(int tag) {
132   switch (tag) {
133
134     case DW_TAG_array_type:
135     case DW_TAG_class_type:
136     case DW_TAG_enumeration_type:
137     case DW_TAG_typedef:
138     case DW_TAG_pointer_type:
139     case DW_TAG_reference_type:
140     case DW_TAG_rvalue_reference_type:
141     case DW_TAG_string_type:
142     case DW_TAG_structure_type:
143     case DW_TAG_subroutine_type:
144     case DW_TAG_union_type:
145     case DW_TAG_ptr_to_member_type:
146     case DW_TAG_set_type:
147     case DW_TAG_subrange_type:
148     case DW_TAG_base_type:
149     case DW_TAG_const_type:
150     case DW_TAG_file_type:
151     case DW_TAG_packed_type:
152     case DW_TAG_volatile_type:
153     case DW_TAG_restrict_type:
154     case DW_TAG_interface_type:
155     case DW_TAG_unspecified_type:
156     case DW_TAG_mutable_type:
157     case DW_TAG_shared_type:
158       return mc_tag_type;
159
160     case DW_TAG_subprogram:
161       return mc_tag_subprogram;
162
163     case DW_TAG_variable:
164     case DW_TAG_formal_parameter:
165       return mc_tag_variable;
166
167     case DW_TAG_lexical_block:
168     case DW_TAG_try_block:
169     case DW_TAG_inlined_subroutine:
170       return mc_tag_scope;
171
172     case DW_TAG_namespace:
173       return mc_tag_namespace;
174
175     default:
176       return mc_tag_unknown;
177
178   }
179 }
180
181 #define MC_DW_CLASS_UNKNOWN 0
182 #define MC_DW_CLASS_ADDRESS 1   // Location in the address space of the program
183 #define MC_DW_CLASS_BLOCK 2     // Arbitrary block of bytes
184 #define MC_DW_CLASS_CONSTANT 3
185 #define MC_DW_CLASS_STRING 3    // String
186 #define MC_DW_CLASS_FLAG 4      // Boolean
187 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
188 #define MC_DW_CLASS_EXPRLOC 6   // DWARF expression/location description
189 #define MC_DW_CLASS_LINEPTR 7
190 #define MC_DW_CLASS_LOCLISTPTR 8
191 #define MC_DW_CLASS_MACPTR 9
192 #define MC_DW_CLASS_RANGELISTPTR 10
193
194 static int MC_dwarf_form_get_class(int form) {
195   switch(form) {
196   case DW_FORM_addr:
197     return MC_DW_CLASS_ADDRESS;
198   case DW_FORM_block2:
199   case DW_FORM_block4:
200   case DW_FORM_block:
201   case DW_FORM_block1:
202     return MC_DW_CLASS_BLOCK;
203   case DW_FORM_data1:
204   case DW_FORM_data2:
205   case DW_FORM_data4:
206   case DW_FORM_data8:
207   case DW_FORM_udata:
208   case DW_FORM_sdata:
209     return MC_DW_CLASS_CONSTANT;
210   case DW_FORM_string:
211   case DW_FORM_strp:
212     return MC_DW_CLASS_STRING;
213   case DW_FORM_ref_addr:
214   case DW_FORM_ref1:
215   case DW_FORM_ref2:
216   case DW_FORM_ref4:
217   case DW_FORM_ref8:
218   case DW_FORM_ref_udata:
219     return MC_DW_CLASS_REFERENCE;
220   case DW_FORM_flag:
221   case DW_FORM_flag_present:
222     return MC_DW_CLASS_FLAG;
223   case DW_FORM_exprloc:
224     return MC_DW_CLASS_EXPRLOC;
225   // TODO sec offset
226   // TODO indirect
227   default:
228     return MC_DW_CLASS_UNKNOWN;
229   }
230 }
231
232 /** \brief Get the name of the tag of a given DIE
233  *
234  *  \param die DIE
235  *  \return name of the tag of this DIE
236  */
237 static inline const char* MC_dwarf_die_tagname(Dwarf_Die* die) {
238   return MC_dwarf_tagname(dwarf_tag(die));
239 }
240
241 // ***** Attributes
242
243 /** \brief Get an attribute of a given DIE as a string
244  *
245  *  \param the DIE
246  *  \param attribute attribute
247  *  \return value of the given attribute of the given DIE
248  */
249 static const char* MC_dwarf_attr_string(Dwarf_Die* die, int attribute) {
250   Dwarf_Attribute attr;
251   if (!dwarf_attr_integrate(die, attribute, &attr)) {
252         return NULL;
253   } else {
254         return dwarf_formstring(&attr);
255   }
256 }
257
258 /** \brief Get the linkage name of a DIE.
259  *
260  *  Use either DW_AT_linkage_name or DW_AR_MIPS_linkage_name.
261  *
262  *  \param DIE
263  *  \return linkage name of the given DIE (or NULL)
264  * */
265 static const char* MC_dwarf_at_linkage_name(Dwarf_Die* die) {
266   const char* name = MC_dwarf_attr_string(die, DW_AT_linkage_name);
267   if (!name)
268     name = MC_dwarf_attr_string(die, DW_AT_MIPS_linkage_name);
269   return name;
270 }
271
272 /** \brief Create a location list from a given attribute
273  *
274  *  \param die the DIE
275  *  \param attr the attribute
276  *  \return MC specific representation of the location list represented by the given attribute
277  *  of the given die
278  */
279 static dw_location_t MC_dwarf_get_location_list(mc_object_info_t info, Dwarf_Die* die, Dwarf_Attribute* attr) {
280
281   dw_location_t location = xbt_new0(s_dw_location_t, 1);
282   location->type = e_dw_loclist;
283   xbt_dynar_t loclist = xbt_dynar_new(sizeof(dw_location_entry_t), NULL);
284   location->location.loclist = loclist;
285
286   ptrdiff_t offset = 0;
287   Dwarf_Addr base, start, end;
288   Dwarf_Op *expr;
289   size_t len;
290
291   while (1) {
292
293     offset = dwarf_getlocations(attr, offset, &base, &start, &end, &expr, &len);
294     if (offset==0)
295       return location;
296     else if (offset==-1)
297       xbt_die("Error while loading location list");
298
299     dw_location_entry_t new_entry = xbt_new0(s_dw_location_entry_t, 1);
300
301     void* base = info->flags & MC_OBJECT_INFO_EXECUTABLE ? 0 : MC_object_base_address(info);
302
303     new_entry->lowpc = (char*) base + start;
304     new_entry->highpc = (char*) base + end;
305     new_entry->location = MC_dwarf_get_expression(expr, len);
306
307     xbt_dynar_push(loclist, &new_entry);
308
309   }
310 }
311
312 /** \brief Find the frame base of a given frame
313  *
314  *  \param ip         Instruction pointer
315  *  \param frame
316  *  \param unw_cursor
317  */
318 void* mc_find_frame_base(void* ip, dw_frame_t frame, unw_cursor_t* unw_cursor) {
319   switch(frame->frame_base->type) {
320   case e_dw_loclist:
321   {
322     int loclist_cursor;
323     for(loclist_cursor=0; loclist_cursor < xbt_dynar_length(frame->frame_base->location.loclist); loclist_cursor++){
324       dw_location_entry_t entry = xbt_dynar_get_as(frame->frame_base->location.loclist, loclist_cursor, dw_location_entry_t);
325       if((ip >= entry->lowpc) && (ip < entry->highpc)){
326         return (void*) MC_dwarf_resolve_location(unw_cursor, entry->location, NULL);
327       }
328     }
329     return NULL;
330   }
331   // Not handled:
332   default:
333     return NULL;
334   }
335 }
336
337 /** \brief Get the location expression or location list from an attribute
338  *
339  *  Processes direct expressions as well as location lists.
340  *
341  *  \param die the DIE
342  *  \param attr the attribute
343  *  \return MC specific representation of the location represented by the given attribute
344  *  of the given die
345  */
346 static dw_location_t MC_dwarf_get_location(mc_object_info_t info, Dwarf_Die* die, Dwarf_Attribute* attr) {
347   int form = dwarf_whatform(attr);
348   switch (form) {
349
350   // The attribute is an DWARF location expression:
351   case DW_FORM_exprloc:
352   case DW_FORM_block1: // not in the spec
353   case DW_FORM_block2:
354   case DW_FORM_block4:
355   case DW_FORM_block:
356     {
357       Dwarf_Op* expr;
358       size_t len;
359       if (dwarf_getlocation(attr, &expr, &len))
360         xbt_die("Could not read location expression");
361       return MC_dwarf_get_expression(expr, len);
362     }
363
364   // The attribute is a reference to a location list entry:
365   case DW_FORM_sec_offset:
366   case DW_FORM_data1:
367   case DW_FORM_data2:
368   case DW_FORM_data4:
369   case DW_FORM_data8:
370     {
371       return MC_dwarf_get_location_list(info, die, attr);
372     }
373     break;
374
375   default:
376     xbt_die("Unexpected form %i list for location in attribute %s of <%p>%s",
377       form,
378       MC_dwarf_attrname(attr->code),
379       (void*) dwarf_dieoffset(die),
380       MC_dwarf_attr_string(die, DW_AT_name));
381     return NULL;
382   }
383 }
384
385 /** \brief Get the location expression or location list from an attribute
386  *
387  *  Processes direct expressions as well as location lists.
388  *
389  *  \param die the DIE
390  *  \param attribute the attribute code
391  *  \return MC specific representation of the location represented by the given attribute
392  *  of the given die
393  */
394 static dw_location_t MC_dwarf_at_location(mc_object_info_t info, Dwarf_Die* die, int attribute) {
395   if(!dwarf_hasattr_integrate(die, attribute))
396     return xbt_new0(s_dw_location_t, 1);
397
398   Dwarf_Attribute attr;
399   dwarf_attr_integrate(die, attribute, &attr);
400   return MC_dwarf_get_location(info, die, &attr);
401 }
402
403 static char* MC_dwarf_at_type(Dwarf_Die* die) {
404   Dwarf_Attribute attr;
405   if (dwarf_hasattr_integrate(die, DW_AT_type)) {
406         dwarf_attr_integrate(die, DW_AT_type, &attr);
407         Dwarf_Die subtype_die;
408         if (dwarf_formref_die(&attr, &subtype_die)==NULL) {
409           xbt_die("Could not find DIE for type");
410         }
411         Dwarf_Off subtype_global_offset = dwarf_dieoffset(&subtype_die);
412     return bprintf("%" PRIx64 , subtype_global_offset);
413   }
414   else return NULL;
415 }
416
417 static uint64_t MC_dwarf_attr_addr(Dwarf_Die* die, int attribute) {
418   Dwarf_Attribute attr;
419   if(dwarf_attr_integrate(die, attribute, &attr)==NULL)
420     return 0;
421   Dwarf_Addr value;
422   if (dwarf_formaddr(&attr, &value) == 0)
423     return (uint64_t) value;
424   else
425     return 0;
426 }
427
428 static uint64_t MC_dwarf_attr_uint(Dwarf_Die* die, int attribute, uint64_t default_value) {
429   Dwarf_Attribute attr;
430   if (dwarf_attr_integrate(die, attribute, &attr)==NULL)
431     return default_value;
432   Dwarf_Word value;
433   return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr), &value) == 0 ? (uint64_t) value : default_value;
434 }
435
436 static bool MC_dwarf_attr_flag(Dwarf_Die* die, int attribute, int integrate) {
437   Dwarf_Attribute attr;
438   if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
439                     : dwarf_attr(die, attribute, &attr))==0)
440     return false;
441
442   bool result;
443   if (dwarf_formflag(&attr, &result))
444     xbt_die("Unexpected form for attribute %s",
445       MC_dwarf_attrname(attribute));
446   return result;
447 }
448
449 static uint64_t MC_dwarf_default_lower_bound(int lang) {
450   switch(lang) {
451   case DW_LANG_C:
452   case DW_LANG_C89:
453   case DW_LANG_C99:
454   case DW_LANG_C_plus_plus:
455   case DW_LANG_D:
456   case DW_LANG_Java:
457   case DW_LANG_ObjC:
458   case DW_LANG_ObjC_plus_plus:
459   case DW_LANG_Python:
460   case DW_LANG_UPC:
461     return 0;
462   case DW_LANG_Ada83:
463   case DW_LANG_Ada95:
464   case DW_LANG_Fortran77:
465   case DW_LANG_Fortran90:
466   case DW_LANG_Fortran95:
467   case DW_LANG_Modula2:
468   case DW_LANG_Pascal83:
469   case DW_LANG_PL1:
470   case DW_LANG_Cobol74:
471   case DW_LANG_Cobol85:
472     return 1;
473   default:
474     xbt_die("No default MT_TAG_lower_bound for language %i and none given", lang);
475     return 0;
476   }
477 }
478
479 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
480   xbt_assert(dwarf_tag(die)==DW_TAG_enumeration_type ||dwarf_tag(die)==DW_TAG_subrange_type,
481       "MC_dwarf_subrange_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
482
483   // Use DW_TAG_count if present:
484   if (dwarf_hasattr_integrate(die, DW_AT_count)) {
485     return MC_dwarf_attr_uint(die, DW_AT_count, 0);
486   }
487
488   // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
489
490   if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
491         // This is not really 0, but the code expects this (we do not know):
492     return 0;
493   }
494   uint64_t upper_bound = MC_dwarf_attr_uint(die, DW_AT_upper_bound, -1);
495
496   uint64_t lower_bound = 0;
497   if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
498     lower_bound = MC_dwarf_attr_uint(die, DW_AT_lower_bound, -1);
499   } else {
500         lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
501   }
502   return upper_bound - lower_bound + 1;
503 }
504
505 static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
506   xbt_assert(dwarf_tag(die)==DW_TAG_array_type,
507     "MC_dwarf_array_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
508
509   int result = 1;
510   Dwarf_Die child;
511   int res;
512   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
513         int child_tag = dwarf_tag(&child);
514     if (child_tag==DW_TAG_subrange_type ||child_tag==DW_TAG_enumeration_type) {
515       result *= MC_dwarf_subrange_element_count(&child, unit);
516     }
517   }
518   return result;
519 }
520
521 // ***** Location
522
523 Dwarf_Off MC_dwarf_resolve_location(unw_cursor_t* c, dw_location_t location, void* frame_pointer_address) {
524   unw_word_t res;
525   switch (location->type){
526   case e_dw_compose:
527     if (xbt_dynar_length(location->location.compose) > 1){
528       return 0; /* TODO : location list with optimizations enabled */
529     }
530     dw_location_t location_entry = xbt_dynar_get_as(location->location.compose, 0, dw_location_t);
531     switch (location_entry->type){
532     case e_dw_register:
533       unw_get_reg(c, location_entry->location.reg, &res);
534       return res;
535     case e_dw_bregister_op:
536       unw_get_reg(c, location_entry->location.breg_op.reg, &res);
537       return (Dwarf_Off) ((long)res + location_entry->location.breg_op.offset);
538       break;
539     case e_dw_fbregister_op:
540       if (frame_pointer_address != NULL)
541         return (Dwarf_Off)((char *)frame_pointer_address + location_entry->location.fbreg_op);
542       else
543         return 0;
544     default:
545       return 0; /* FIXME : implement other cases (with optimizations enabled) */
546     }
547     break;
548     default:
549       return 0;
550   }
551 }
552
553 // ***** dw_type_t
554
555 static void MC_dwarf_fill_member_location(dw_type_t type, dw_type_t member, Dwarf_Die* child) {
556   if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
557     xbt_die("Can't groke DW_AT_data_bit_offset.");
558   }
559
560   if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
561     if (type->type != DW_TAG_union_type) {
562         xbt_die(
563           "Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%p>%s",
564           member->name, type->id, type->name);
565     } else {
566       return;
567     }
568   }
569
570   Dwarf_Attribute attr;
571   dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
572   int form  = dwarf_whatform(&attr);
573   int klass = MC_dwarf_form_get_class(form);
574   switch (klass) {
575   case MC_DW_CLASS_EXPRLOC:
576   case MC_DW_CLASS_BLOCK:
577     // Location expression:
578     {
579       Dwarf_Op* expr;
580       size_t len;
581       if (dwarf_getlocation(&attr, &expr, &len)) {
582         xbt_die(
583           "Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%p>%s",
584           MC_dwarf_attr_string(child, DW_AT_name),
585           type->id, type->name);
586       }
587       if (len==1 && expr[0].atom == DW_OP_plus_uconst) {
588         member->offset =  expr[0].number;
589       } else {
590         xbt_die("Can't groke this location expression yet.");
591       }
592       break;
593     }
594   case MC_DW_CLASS_CONSTANT:
595     // Offset from the base address of the object:
596     {
597       Dwarf_Word offset;
598       if (!dwarf_formudata(&attr, &offset))
599         member->offset = offset;
600       else
601         xbt_die("Cannot get %s location <%p>%s",
602           MC_dwarf_attr_string(child, DW_AT_name),
603           type->id, type->name);
604       break;
605     }
606   case MC_DW_CLASS_LOCLISTPTR:
607     // Reference to a location list:
608     // TODO
609   case MC_DW_CLASS_REFERENCE:
610     // It's supposed to be possible in DWARF2 but I couldn't find its semantic
611     // in the spec.
612   default:
613     xbt_die(
614       "Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
615       klass, form);
616   }
617
618 }
619
620 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_type_t type) {
621   int res;
622   Dwarf_Die child;
623   xbt_assert(!type->members);
624   type->members = xbt_dynar_new(sizeof(dw_type_t), (void(*)(void*))dw_type_free);
625   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
626     if (dwarf_tag(&child)==DW_TAG_member) {
627
628       // Skip declarations:
629       if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
630         continue;
631
632       // Skip compile time constants:
633       if(dwarf_hasattr(&child, DW_AT_const_value))
634         continue;
635
636       // TODO, we should use another type (because is is not a type but a member)
637       dw_type_t member = xbt_new0(s_dw_type_t, 1);
638       member->type = -1;
639       member->id = NULL;
640
641       const char* name = MC_dwarf_attr_string(&child, DW_AT_name);
642       if(name)
643         member->name = xbt_strdup(name);
644       else
645         member->name = NULL;
646
647       member->byte_size = MC_dwarf_attr_uint(&child, DW_AT_byte_size, 0);
648       member->element_count = -1;
649       member->dw_type_id = MC_dwarf_at_type(&child);
650       member->members = NULL;
651       member->is_pointer_type = 0;
652       member->offset = 0;
653
654       if(dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
655         xbt_die("Can't groke DW_AT_data_bit_offset.");
656       }
657
658       MC_dwarf_fill_member_location(type, member, &child);
659
660       if (!member->dw_type_id) {
661         xbt_die("Missing type for member %s of <%p>%s", member->name, type->id, type->name);
662       }
663
664       xbt_dynar_push(type->members, &member);
665     }
666   }
667 }
668
669 /** \brief Create a MC type object from a DIE
670  *
671  *  \param info current object info object
672  *  \param DIE (for a given type);
673  *  \param unit compilation unit of the current DIE
674  *  \return MC representation of the type
675  */
676 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) {
677
678   dw_type_t type = xbt_new0(s_dw_type_t, 1);
679   type->type = -1;
680   type->id = NULL;
681   type->name = NULL;
682   type->byte_size = 0;
683   type->element_count = -1;
684   type->dw_type_id = NULL;
685   type->members = NULL;
686   type->is_pointer_type = 0;
687   type->offset = 0;
688
689   type->type = dwarf_tag(die);
690
691   // Global Offset
692   type->id = (void *) dwarf_dieoffset(die);
693
694   const char* name = MC_dwarf_attr_string(die, DW_AT_name);
695   if (name!=NULL) {
696         type->name = xbt_strdup(name);
697   }
698
699   XBT_DEBUG("Processing type <%p>%s", type->id, type->name);
700
701   type->dw_type_id = MC_dwarf_at_type(die);
702
703   // Computation of the byte_size;
704   if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
705     type->byte_size = MC_dwarf_attr_uint(die, DW_AT_byte_size, 0);
706   else if (type->type == DW_TAG_array_type || type->type==DW_TAG_structure_type || type->type==DW_TAG_class_type) {
707     Dwarf_Word size;
708     if (dwarf_aggregate_size(die, &size)==0) {
709       type->byte_size = size;
710     }
711   }
712
713   switch (type->type) {
714   case DW_TAG_array_type:
715         type->element_count = MC_dwarf_array_element_count(die, unit);
716         // TODO, handle DW_byte_stride and (not) DW_bit_stride
717         break;
718
719   case DW_TAG_pointer_type:
720   case DW_TAG_reference_type:
721   case DW_TAG_rvalue_reference_type:
722     type->is_pointer_type = 1;
723     break;
724
725   case DW_TAG_structure_type:
726   case DW_TAG_union_type:
727   case DW_TAG_class_type:
728           MC_dwarf_add_members(info, die, unit, type);
729           char* new_namespace = namespace == NULL ? xbt_strdup(type->name)
730             : bprintf("%s::%s", namespace, type->name);
731           MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
732           free(new_namespace);
733           break;
734   }
735
736   return type;
737 }
738
739 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) {
740   dw_type_t type = MC_dwarf_die_to_type(info, die, unit, frame, namespace);
741
742   char* key = bprintf("%" PRIx64, (uint64_t) type->id);
743   xbt_dict_set(info->types, key, type, NULL);
744
745   if(type->name && type->byte_size!=0) {
746     xbt_dict_set(info->types_by_name, type->name, type, NULL);
747   }
748 }
749
750 /** \brief Convert libdw location expresion elment into native one (or NULL in some cases) */
751 static dw_location_t MC_dwarf_get_expression_element(Dwarf_Op* op) {
752   dw_location_t element = xbt_new0(s_dw_location_t, 1);
753   uint8_t atom = op->atom;
754   if (atom >= DW_OP_reg0 && atom<= DW_OP_reg31) {
755     element->type = e_dw_register;
756     element->location.reg = atom - DW_OP_reg0;
757   }
758   else if (atom >= DW_OP_breg0 && atom<= DW_OP_breg31) {
759     element->type = e_dw_bregister_op;
760     element->location.reg = atom - DW_OP_breg0;
761     element->location.breg_op.offset = op->number;
762   }
763   else if (atom >= DW_OP_lit0 && atom<= DW_OP_lit31) {
764     element->type = e_dw_lit;
765     element->location.reg = atom - DW_OP_lit0;
766   }
767   else switch (atom) {
768   case DW_OP_fbreg:
769     element->type = e_dw_fbregister_op;
770     element->location.fbreg_op = op->number;
771     break;
772   case DW_OP_piece:
773     element->type = e_dw_piece;
774     element->location.piece = op->number;
775     break;
776   case DW_OP_plus_uconst:
777     element->type = e_dw_plus_uconst;
778     element->location.plus_uconst = op->number;
779     break;
780   case DW_OP_abs:
781     element->type = e_dw_arithmetic;
782     element->location.arithmetic = xbt_strdup("abs");
783     break;
784   case DW_OP_and:
785     element->type = e_dw_arithmetic;
786     element->location.arithmetic = xbt_strdup("and");
787     break;
788   case DW_OP_div:
789     element->type = e_dw_arithmetic;
790     element->location.arithmetic = xbt_strdup("div");
791     break;
792   case DW_OP_minus:
793     element->type = e_dw_arithmetic;
794     element->location.arithmetic = xbt_strdup("minus");
795     break;
796   case DW_OP_mod:
797     element->type = e_dw_arithmetic;
798     element->location.arithmetic = xbt_strdup("mod");
799     break;
800   case DW_OP_mul:
801     element->type = e_dw_arithmetic;
802     element->location.arithmetic = xbt_strdup("mul");
803     break;
804   case DW_OP_neg:
805     element->type = e_dw_arithmetic;
806     element->location.arithmetic = xbt_strdup("neg");
807     break;
808   case DW_OP_not:
809     element->type = e_dw_arithmetic;
810     element->location.arithmetic = xbt_strdup("not");
811     break;
812   case DW_OP_or:
813     element->type = e_dw_arithmetic;
814     element->location.arithmetic = xbt_strdup("or");
815     break;
816   case DW_OP_plus:
817     element->type = e_dw_arithmetic;
818     element->location.arithmetic = xbt_strdup("plus");
819     break;
820
821   case DW_OP_stack_value:
822     // Why nothing here?
823     xbt_free(element);
824     return NULL;
825
826   case DW_OP_deref_size:
827     element->type = e_dw_deref;
828     element->location.deref_size =  (unsigned int short) op->number;
829     break;
830   case DW_OP_deref:
831     element->type = e_dw_deref;
832     element->location.deref_size = sizeof(void *);
833     break;
834   case DW_OP_constu:
835     element->type = e_dw_uconstant;
836     element->location.uconstant.bytes = 1;
837     element->location.uconstant.value = (unsigned long int) op->number;
838     break;
839   case DW_OP_consts:
840     element->type = e_dw_sconstant;
841     element->location.uconstant.bytes = 1;
842     element->location.uconstant.value = (unsigned long int) op->number;
843     break;
844
845   case DW_OP_const1u:
846     element->type = e_dw_uconstant;
847     element->location.uconstant.bytes = 1;
848     element->location.uconstant.value = (unsigned long int) op->number;
849     break;
850   case DW_OP_const2u:
851     element->type = e_dw_uconstant;
852     element->location.uconstant.bytes = 2;
853     element->location.uconstant.value = (unsigned long int) op->number;
854     break;
855   case DW_OP_const4u:
856     element->type = e_dw_uconstant;
857     element->location.uconstant.bytes = 4;
858     element->location.uconstant.value = (unsigned long int) op->number;
859     break;
860   case DW_OP_const8u:
861     element->type = e_dw_uconstant;
862     element->location.uconstant.bytes = 8;
863     element->location.uconstant.value = (unsigned long int) op->number;
864     break;
865
866   case DW_OP_const1s:
867     element->type = e_dw_sconstant;
868     element->location.uconstant.bytes = 1;
869     element->location.uconstant.value = (unsigned long int) op->number;
870     break;
871   case DW_OP_const2s:
872     element->type = e_dw_sconstant;
873     element->location.uconstant.bytes = 2;
874     element->location.uconstant.value = (unsigned long int) op->number;
875     break;
876   case DW_OP_const4s:
877     element->type = e_dw_sconstant;
878     element->location.uconstant.bytes = 4;
879     element->location.uconstant.value = (unsigned long int) op->number;
880     break;
881   case DW_OP_const8s:
882     element->type = e_dw_sconstant;
883     element->location.uconstant.bytes = 8;
884     element->location.uconstant.value = (unsigned long int) op->number;
885     break;
886   default:
887     element->type = e_dw_unsupported;
888     break;
889   }
890   return element;
891 }
892
893 /** \brief Convert libdw location expresion into native one */
894 static dw_location_t MC_dwarf_get_expression(Dwarf_Op* expr,  size_t len) {
895   dw_location_t loc = xbt_new0(s_dw_location_t, 1);
896   loc->type = e_dw_compose;
897   loc->location.compose = xbt_dynar_new(sizeof(dw_location_t), NULL);
898
899   int i;
900   for (i=0; i!=len; ++i) {
901     dw_location_t element =  MC_dwarf_get_expression_element(expr+i);
902     if (element)
903       xbt_dynar_push(loc->location.compose, &element);
904   }
905
906   return loc;
907 }
908
909 static int mc_anonymous_variable_index = 0;
910
911 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) {
912   // Skip declarations:
913   if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
914     return NULL;
915
916   // Skip compile time constants:
917   if(dwarf_hasattr(die, DW_AT_const_value))
918     return NULL;
919
920   Dwarf_Attribute attr_location;
921   if (dwarf_attr(die, DW_AT_location, &attr_location)==NULL) {
922     // No location: do not add it ?
923     return NULL;
924   }
925
926   dw_variable_t variable = xbt_new0(s_dw_variable_t, 1);
927   variable->dwarf_offset = dwarf_dieoffset(die);
928   variable->global = frame == NULL; // Can be override base on DW_AT_location
929
930   const char* name = MC_dwarf_attr_string(die, DW_AT_name);
931   variable->name = xbt_strdup(name);
932
933   variable->type_origin = MC_dwarf_at_type(die);
934
935   int klass = MC_dwarf_form_get_class(dwarf_whatform(&attr_location));
936   switch (klass) {
937   case MC_DW_CLASS_EXPRLOC:
938   case MC_DW_CLASS_BLOCK:
939     // Location expression:
940     {
941       Dwarf_Op* expr;
942       size_t len;
943       if (dwarf_getlocation(&attr_location, &expr, &len)) {
944         xbt_die(
945           "Could not read location expression in DW_AT_location of variable <%p>%s",
946           (void*) variable->dwarf_offset, variable->name);
947       }
948
949       if (len==1 && expr[0].atom == DW_OP_addr) {
950         variable->global = 1;
951         Dwarf_Off offset = expr[0].number;
952         // TODO, Why is this different base on the object?
953         Dwarf_Off base = strcmp(info->file_name, xbt_binary_name) !=0 ? (Dwarf_Off) info->start_exec : 0;
954         variable->address = (void*) (base + offset);
955       } else {
956         variable->location = MC_dwarf_get_expression(expr, len);
957       }
958
959       break;
960     }
961   case MC_DW_CLASS_LOCLISTPTR:
962   case MC_DW_CLASS_CONSTANT:
963     // Reference to location list:
964     variable->location = MC_dwarf_get_location_list(info, die, &attr_location);
965     break;
966   default:
967     xbt_die("Unexpected calss 0x%x (%i) list for location in <%p>%s",
968       klass, klass, (void*) variable->dwarf_offset, variable->name);
969   }
970
971   // Handle start_scope:
972   if (dwarf_hasattr(die, DW_AT_start_scope)) {
973     Dwarf_Attribute attr;
974     dwarf_attr(die, DW_AT_start_scope, &attr);
975     int form  = dwarf_whatform(&attr);
976     int klass = MC_dwarf_form_get_class(form);
977     switch(klass) {
978     case MC_DW_CLASS_CONSTANT:
979     {
980       Dwarf_Word value;
981       variable->start_scope = dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
982       break;
983     }
984     default:
985       xbt_die("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
986         form, klass, name==NULL ? "?" : name);
987     }
988   }
989
990   if(namespace && variable->global) {
991     char* old_name = variable->name;
992     variable->name = bprintf("%s::%s", namespace, old_name);
993     free(old_name);
994   }
995
996   // The current code needs a variable name,
997   // generate a fake one:
998   if(!variable->name) {
999     variable->name = bprintf("@anonymous#%i", mc_anonymous_variable_index++);
1000   }
1001
1002   return variable;
1003 }
1004
1005 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) {
1006   dw_variable_t variable = MC_die_to_variable(info, die, unit, frame, namespace);
1007   if(variable==NULL)
1008       return;
1009   MC_dwarf_register_variable(info, frame, variable);
1010 }
1011
1012 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) {
1013   dw_frame_t frame = xbt_new0(s_dw_frame_t, 1);
1014
1015   frame->start = dwarf_dieoffset(die);
1016
1017   const char* name = MC_dwarf_attr_string(die, DW_AT_name);
1018   frame->name = namespace ? bprintf("%s::%s", namespace, name) : xbt_strdup(name);
1019
1020   // This is the base address for DWARF addresses.
1021   // Relocated addresses are offset from this base address.
1022   // See DWARF4 spec 7.5
1023   void* base = info->flags & MC_OBJECT_INFO_EXECUTABLE ? 0 : MC_object_base_address(info);
1024
1025   // Variables are filled in the (recursive) call of MC_dwarf_handle_children:
1026   frame->variables = xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
1027   frame->high_pc = ((char*) base) + MC_dwarf_attr_addr(die, DW_AT_high_pc);
1028   frame->low_pc = ((char*) base) + MC_dwarf_attr_addr(die, DW_AT_low_pc);
1029   frame->frame_base = MC_dwarf_at_location(info, die, DW_AT_frame_base);
1030   frame->end = -1; // This one is now useless:
1031
1032   // Register it:
1033   xbt_dynar_push(info->subprograms, &frame);
1034
1035   // Handle children:
1036   MC_dwarf_handle_children(info, die, unit, frame, namespace);
1037 }
1038
1039 static void mc_dwarf_handle_namespace_die(
1040     mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
1041   const char* name = MC_dwarf_attr_string(die, DW_AT_name);
1042   if(frame)
1043     xbt_die("Unexpected namespace in a subprogram");
1044   char* new_namespace = namespace == NULL ? xbt_strdup(name)
1045     : bprintf("%s::%s", namespace, name);
1046   MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
1047 }
1048
1049 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
1050   Dwarf_Die child;
1051   int res;
1052   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
1053     MC_dwarf_handle_die(info, &child, unit, frame, namespace);
1054   }
1055 }
1056
1057 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
1058   int tag = dwarf_tag(die);
1059   mc_tag_class klass = MC_dwarf_tag_classify(tag);
1060   switch (klass) {
1061
1062     // Type:
1063     case mc_tag_type:
1064       MC_dwarf_handle_type_die(info, die, unit, frame, namespace);
1065       break;
1066
1067     // Program:
1068     case mc_tag_subprogram:
1069       MC_dwarf_handle_subprogram_die(info, die, unit, frame, namespace);
1070       return;
1071
1072     // Variable:
1073     case mc_tag_variable:
1074       MC_dwarf_handle_variable_die(info, die, unit, frame, namespace);
1075       break;
1076
1077     // Scope:
1078     case mc_tag_scope:
1079       // TODO
1080       break;
1081
1082     case mc_tag_namespace:
1083       mc_dwarf_handle_namespace_die(info, die, unit, frame, namespace);
1084       break;
1085
1086     default:
1087       break;
1088
1089   }
1090 }
1091
1092 void MC_dwarf_get_variables(mc_object_info_t info) {
1093   int fd = open(info->file_name, O_RDONLY);
1094   if (fd<0) {
1095     xbt_die("Could not open file %s", info->file_name);
1096   }
1097   Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
1098   if (dwarf==NULL) {
1099     xbt_die("Your program must be compiled with -g");
1100   }
1101
1102   Dwarf_Off offset = 0;
1103   Dwarf_Off next_offset = 0;
1104   size_t length;
1105   while (dwarf_nextcu (dwarf, offset, &next_offset, &length, NULL, NULL, NULL) == 0) {
1106     Dwarf_Die unit_die;
1107
1108     if(dwarf_offdie(dwarf, offset+length, &unit_die)!=NULL) {
1109       Dwarf_Die child;
1110       int res;
1111       for (res=dwarf_child(&unit_die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
1112         MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
1113       }
1114     }
1115     offset = next_offset;
1116   }
1117
1118   dwarf_end(dwarf);
1119   close(fd);
1120 }