Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
aa5f9f0f4e59153c4c561b4023a393e0f3d770cd
[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_bregister_op:
533       unw_get_reg(c, location_entry->location.breg_op.reg, &res);
534       return (Dwarf_Off) ((long)res + location_entry->location.breg_op.offset);
535       break;
536     case e_dw_fbregister_op:
537       if (frame_pointer_address != NULL)
538         return (Dwarf_Off)((char *)frame_pointer_address + location_entry->location.fbreg_op);
539       else
540         return 0;
541     default:
542       return 0; /* FIXME : implement other cases (with optimizations enabled) */
543     }
544     break;
545     default:
546       return 0;
547   }
548 }
549
550 // ***** dw_type_t
551
552 static void MC_dwarf_fill_member_location(dw_type_t type, dw_type_t member, Dwarf_Die* child) {
553   if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
554     xbt_die("Can't groke DW_AT_data_bit_offset.");
555   }
556
557   if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
558     if (type->type != DW_TAG_union_type) {
559         xbt_die(
560           "Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%p>%s",
561           member->name, type->id, type->name);
562     } else {
563       return;
564     }
565   }
566
567   Dwarf_Attribute attr;
568   dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
569   int form = dwarf_whatform(&attr);
570   int klass = MC_dwarf_form_get_class(form);
571   switch (klass) {
572   case MC_DW_CLASS_EXPRLOC:
573   case MC_DW_CLASS_BLOCK:
574     // Location expression:
575     {
576       Dwarf_Op* expr;
577       size_t len;
578       if (dwarf_getlocation(&attr, &expr, &len)) {
579         xbt_die(
580           "Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%p>%s",
581           MC_dwarf_attr_string(child, DW_AT_name),
582           type->id, type->name);
583       }
584       if (len==1 && expr[0].atom == DW_OP_plus_uconst) {
585         member->offset =  expr[0].number;
586       } else {
587         xbt_die("Can't groke this location expression yet.");
588       }
589       break;
590     }
591   case MC_DW_CLASS_CONSTANT:
592     // Offset from the base address of the object:
593     {
594       Dwarf_Word offset;
595       if (!dwarf_formudata(&attr, &offset))
596         member->offset = offset;
597       else
598         xbt_die("Cannot get %s location <%p>%s",
599           MC_dwarf_attr_string(child, DW_AT_name),
600           type->id, type->name);
601       break;
602     }
603   case MC_DW_CLASS_LOCLISTPTR:
604     // Reference to a location list:
605     // TODO
606   case MC_DW_CLASS_REFERENCE:
607     // It's supposed to be possible in DWARF2 but I couldn't find its semantic
608     // in the spec.
609   default:
610     xbt_die(
611       "Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
612       klass, form);
613   }
614
615 }
616
617 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_type_t type) {
618   int res;
619   Dwarf_Die child;
620   xbt_assert(!type->members);
621   type->members = xbt_dynar_new(sizeof(dw_type_t), (void(*)(void*))dw_type_free);
622   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
623     if (dwarf_tag(&child)==DW_TAG_member) {
624
625       // Skip declarations:
626       if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
627         continue;
628
629       // Skip compile time constants:
630       if(dwarf_hasattr(&child, DW_AT_const_value))
631         continue;
632
633       // TODO, we should use another type (because is is not a type but a member)
634       dw_type_t member = xbt_new0(s_dw_type_t, 1);
635       member->type = -1;
636       member->id = NULL;
637
638       const char* name = MC_dwarf_attr_string(&child, DW_AT_name);
639       if(name)
640         member->name = xbt_strdup(name);
641       else
642         member->name = NULL;
643
644       member->byte_size = MC_dwarf_attr_uint(&child, DW_AT_byte_size, 0);
645       member->element_count = -1;
646       member->dw_type_id = MC_dwarf_at_type(&child);
647       member->members = NULL;
648       member->is_pointer_type = 0;
649       member->offset = 0;
650
651       if(dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
652         xbt_die("Can't groke DW_AT_data_bit_offset.");
653       }
654
655       MC_dwarf_fill_member_location(type, member, &child);
656
657       if (!member->dw_type_id) {
658         xbt_die("Missing type for member %s of <%p>%s", member->name, type->id, type->name);
659       }
660
661       xbt_dynar_push(type->members, &member);
662     }
663   }
664 }
665
666 /** \brief Create a MC type object from a DIE
667  *
668  *  \param info current object info object
669  *  \param DIE (for a given type);
670  *  \param unit compilation unit of the current DIE
671  *  \return MC representation of the type
672  */
673 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) {
674
675   dw_type_t type = xbt_new0(s_dw_type_t, 1);
676   type->type = -1;
677   type->id = NULL;
678   type->name = NULL;
679   type->byte_size = 0;
680   type->element_count = -1;
681   type->dw_type_id = NULL;
682   type->members = NULL;
683   type->is_pointer_type = 0;
684   type->offset = 0;
685
686   type->type = dwarf_tag(die);
687
688   // Global Offset
689   type->id = (void *) dwarf_dieoffset(die);
690
691   const char* name = MC_dwarf_attr_string(die, DW_AT_name);
692   if (name!=NULL) {
693         type->name = xbt_strdup(name);
694   }
695
696   XBT_DEBUG("Processing type <%p>%s", type->id, type->name);
697
698   type->dw_type_id = MC_dwarf_at_type(die);
699
700   // Computation of the byte_size;
701   if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
702     type->byte_size = MC_dwarf_attr_uint(die, DW_AT_byte_size, 0);
703   else if (type->type == DW_TAG_array_type || type->type==DW_TAG_structure_type || type->type==DW_TAG_class_type) {
704     Dwarf_Word size;
705     if (dwarf_aggregate_size(die, &size)==0) {
706       type->byte_size = size;
707     }
708   }
709
710   switch (type->type) {
711   case DW_TAG_array_type:
712         type->element_count = MC_dwarf_array_element_count(die, unit);
713         // TODO, handle DW_byte_stride and (not) DW_bit_stride
714         break;
715
716   case DW_TAG_pointer_type:
717   case DW_TAG_reference_type:
718   case DW_TAG_rvalue_reference_type:
719     type->is_pointer_type = 1;
720     break;
721
722   case DW_TAG_structure_type:
723   case DW_TAG_union_type:
724   case DW_TAG_class_type:
725           MC_dwarf_add_members(info, die, unit, type);
726           char* new_namespace = namespace == NULL ? xbt_strdup(type->name)
727             : bprintf("%s::%s", namespace, type->name);
728           MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
729           free(new_namespace);
730           break;
731   }
732
733   return type;
734 }
735
736 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) {
737   dw_type_t type = MC_dwarf_die_to_type(info, die, unit, frame, namespace);
738
739   char* key = bprintf("%" PRIx64, (uint64_t) type->id);
740   xbt_dict_set(info->types, key, type, NULL);
741
742   if(type->name && type->byte_size!=0) {
743     xbt_dict_set(info->types_by_name, type->name, type, NULL);
744   }
745 }
746
747 /** \brief Convert libdw location expresion elment into native one (or NULL in some cases) */
748 static dw_location_t MC_dwarf_get_expression_element(Dwarf_Op* op) {
749   dw_location_t element = xbt_new0(s_dw_location_t, 1);
750   uint8_t atom = op->atom;
751   if (atom >= DW_OP_reg0 && atom<= DW_OP_reg31) {
752     element->type = e_dw_register;
753     element->location.reg = atom - DW_OP_reg0;
754   }
755   else if (atom >= DW_OP_breg0 && atom<= DW_OP_breg31) {
756     element->type = e_dw_bregister_op;
757     element->location.reg = atom - DW_OP_breg0;
758     element->location.breg_op.offset = op->number;
759   }
760   else if (atom >= DW_OP_lit0 && atom<= DW_OP_lit31) {
761     element->type = e_dw_lit;
762     element->location.reg = atom - DW_OP_lit0;
763   }
764   else switch (atom) {
765   case DW_OP_fbreg:
766     element->type = e_dw_fbregister_op;
767     element->location.fbreg_op = op->number;
768     break;
769   case DW_OP_piece:
770     element->type = e_dw_piece;
771     element->location.piece = op->number;
772     break;
773   case DW_OP_plus_uconst:
774     element->type = e_dw_plus_uconst;
775     element->location.plus_uconst = op->number;
776     break;
777   case DW_OP_abs:
778     element->type = e_dw_arithmetic;
779     element->location.arithmetic = xbt_strdup("abs");
780     break;
781   case DW_OP_and:
782     element->type = e_dw_arithmetic;
783     element->location.arithmetic = xbt_strdup("and");
784     break;
785   case DW_OP_div:
786     element->type = e_dw_arithmetic;
787     element->location.arithmetic = xbt_strdup("div");
788     break;
789   case DW_OP_minus:
790     element->type = e_dw_arithmetic;
791     element->location.arithmetic = xbt_strdup("minus");
792     break;
793   case DW_OP_mod:
794     element->type = e_dw_arithmetic;
795     element->location.arithmetic = xbt_strdup("mod");
796     break;
797   case DW_OP_mul:
798     element->type = e_dw_arithmetic;
799     element->location.arithmetic = xbt_strdup("mul");
800     break;
801   case DW_OP_neg:
802     element->type = e_dw_arithmetic;
803     element->location.arithmetic = xbt_strdup("neg");
804     break;
805   case DW_OP_not:
806     element->type = e_dw_arithmetic;
807     element->location.arithmetic = xbt_strdup("not");
808     break;
809   case DW_OP_or:
810     element->type = e_dw_arithmetic;
811     element->location.arithmetic = xbt_strdup("or");
812     break;
813   case DW_OP_plus:
814     element->type = e_dw_arithmetic;
815     element->location.arithmetic = xbt_strdup("plus");
816     break;
817
818   case DW_OP_stack_value:
819     // Why nothing here?
820     xbt_free(element);
821     return NULL;
822
823   case DW_OP_deref_size:
824     element->type = e_dw_deref;
825     element->location.deref_size =  (unsigned int short) op->number;
826     break;
827   case DW_OP_deref:
828     element->type = e_dw_deref;
829     element->location.deref_size = sizeof(void *);
830     break;
831   case DW_OP_constu:
832     element->type = e_dw_uconstant;
833     element->location.uconstant.bytes = 1;
834     element->location.uconstant.value = (unsigned long int) op->number;
835     break;
836   case DW_OP_consts:
837     element->type = e_dw_sconstant;
838     element->location.uconstant.bytes = 1;
839     element->location.uconstant.value = (unsigned long int) op->number;
840     break;
841
842   case DW_OP_const1u:
843     element->type = e_dw_uconstant;
844     element->location.uconstant.bytes = 1;
845     element->location.uconstant.value = (unsigned long int) op->number;
846     break;
847   case DW_OP_const2u:
848     element->type = e_dw_uconstant;
849     element->location.uconstant.bytes = 2;
850     element->location.uconstant.value = (unsigned long int) op->number;
851     break;
852   case DW_OP_const4u:
853     element->type = e_dw_uconstant;
854     element->location.uconstant.bytes = 4;
855     element->location.uconstant.value = (unsigned long int) op->number;
856     break;
857   case DW_OP_const8u:
858     element->type = e_dw_uconstant;
859     element->location.uconstant.bytes = 8;
860     element->location.uconstant.value = (unsigned long int) op->number;
861     break;
862
863   case DW_OP_const1s:
864     element->type = e_dw_sconstant;
865     element->location.uconstant.bytes = 1;
866     element->location.uconstant.value = (unsigned long int) op->number;
867     break;
868   case DW_OP_const2s:
869     element->type = e_dw_sconstant;
870     element->location.uconstant.bytes = 2;
871     element->location.uconstant.value = (unsigned long int) op->number;
872     break;
873   case DW_OP_const4s:
874     element->type = e_dw_sconstant;
875     element->location.uconstant.bytes = 4;
876     element->location.uconstant.value = (unsigned long int) op->number;
877     break;
878   case DW_OP_const8s:
879     element->type = e_dw_sconstant;
880     element->location.uconstant.bytes = 8;
881     element->location.uconstant.value = (unsigned long int) op->number;
882     break;
883   default:
884     element->type = e_dw_unsupported;
885     break;
886   }
887   return element;
888 }
889
890 /** \brief Convert libdw location expresion into native one */
891 static dw_location_t MC_dwarf_get_expression(Dwarf_Op* expr,  size_t len) {
892   dw_location_t loc = xbt_new0(s_dw_location_t, 1);
893   loc->type = e_dw_compose;
894   loc->location.compose = xbt_dynar_new(sizeof(dw_location_t), NULL);
895
896   int i;
897   for (i=0; i!=len; ++i) {
898     dw_location_t element =  MC_dwarf_get_expression_element(expr+i);
899     if (element)
900       xbt_dynar_push(loc->location.compose, &element);
901   }
902
903   return loc;
904 }
905
906 static int mc_anonymous_variable_index = 0;
907
908 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) {
909   // Skip declarations:
910   if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
911     return NULL;
912
913   // Skip compile time constants:
914   if(dwarf_hasattr(die, DW_AT_const_value))
915     return NULL;
916
917   Dwarf_Attribute attr_location;
918   if (dwarf_attr(die, DW_AT_location, &attr_location)==NULL) {
919     // No location: do not add it ?
920     return NULL;
921   }
922
923   dw_variable_t variable = xbt_new0(s_dw_variable_t, 1);
924   variable->dwarf_offset = dwarf_dieoffset(die);
925   variable->global = frame == NULL; // Can be override base on DW_AT_location
926
927   const char* name = MC_dwarf_attr_string(die, DW_AT_name);
928   variable->name = xbt_strdup(name);
929
930   variable->type_origin = MC_dwarf_at_type(die);
931
932   int form = dwarf_whatform(&attr_location);
933   int klass = form == DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
934   switch (klass) {
935   case MC_DW_CLASS_EXPRLOC:
936   case MC_DW_CLASS_BLOCK:
937     // Location expression:
938     {
939       Dwarf_Op* expr;
940       size_t len;
941       if (dwarf_getlocation(&attr_location, &expr, &len)) {
942         xbt_die(
943           "Could not read location expression in DW_AT_location of variable <%p>%s",
944           (void*) variable->dwarf_offset, variable->name);
945       }
946
947       if (len==1 && expr[0].atom == DW_OP_addr) {
948         variable->global = 1;
949         Dwarf_Off offset = expr[0].number;
950         // TODO, Why is this different base on the object?
951         Dwarf_Off base = strcmp(info->file_name, xbt_binary_name) !=0 ? (Dwarf_Off) info->start_exec : 0;
952         variable->address = (void*) (base + offset);
953       } else {
954         variable->location = MC_dwarf_get_expression(expr, len);
955       }
956
957       break;
958     }
959   case MC_DW_CLASS_LOCLISTPTR:
960   case MC_DW_CLASS_CONSTANT:
961     // Reference to location list:
962     variable->location = MC_dwarf_get_location_list(info, die, &attr_location);
963     break;
964   default:
965     xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location in <%p>%s",
966       form, form, klass, klass, (void*) variable->dwarf_offset, variable->name);
967   }
968
969   // Handle start_scope:
970   if (dwarf_hasattr(die, DW_AT_start_scope)) {
971     Dwarf_Attribute attr;
972     dwarf_attr(die, DW_AT_start_scope, &attr);
973     int form  = dwarf_whatform(&attr);
974     int klass = MC_dwarf_form_get_class(form);
975     switch(klass) {
976     case MC_DW_CLASS_CONSTANT:
977     {
978       Dwarf_Word value;
979       variable->start_scope = dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
980       break;
981     }
982     default:
983       xbt_die("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
984         form, klass, name==NULL ? "?" : name);
985     }
986   }
987
988   if(namespace && variable->global) {
989     char* old_name = variable->name;
990     variable->name = bprintf("%s::%s", namespace, old_name);
991     free(old_name);
992   }
993
994   // The current code needs a variable name,
995   // generate a fake one:
996   if(!variable->name) {
997     variable->name = bprintf("@anonymous#%i", mc_anonymous_variable_index++);
998   }
999
1000   return variable;
1001 }
1002
1003 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) {
1004   dw_variable_t variable = MC_die_to_variable(info, die, unit, frame, namespace);
1005   if(variable==NULL)
1006       return;
1007   MC_dwarf_register_variable(info, frame, variable);
1008 }
1009
1010 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) {
1011   dw_frame_t frame = xbt_new0(s_dw_frame_t, 1);
1012
1013   frame->start = dwarf_dieoffset(die);
1014
1015   const char* name = MC_dwarf_attr_string(die, DW_AT_name);
1016   frame->name = namespace ? bprintf("%s::%s", namespace, name) : xbt_strdup(name);
1017
1018   // This is the base address for DWARF addresses.
1019   // Relocated addresses are offset from this base address.
1020   // See DWARF4 spec 7.5
1021   void* base = info->flags & MC_OBJECT_INFO_EXECUTABLE ? 0 : MC_object_base_address(info);
1022
1023   // Variables are filled in the (recursive) call of MC_dwarf_handle_children:
1024   frame->variables = xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
1025   frame->high_pc = ((char*) base) + MC_dwarf_attr_addr(die, DW_AT_high_pc);
1026   frame->low_pc = ((char*) base) + MC_dwarf_attr_addr(die, DW_AT_low_pc);
1027   frame->frame_base = MC_dwarf_at_location(info, die, DW_AT_frame_base);
1028   frame->end = -1; // This one is now useless:
1029
1030   // Register it:
1031   xbt_dynar_push(info->subprograms, &frame);
1032
1033   // Handle children:
1034   MC_dwarf_handle_children(info, die, unit, frame, namespace);
1035 }
1036
1037 static void mc_dwarf_handle_namespace_die(
1038     mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
1039   const char* name = MC_dwarf_attr_string(die, DW_AT_name);
1040   if(frame)
1041     xbt_die("Unexpected namespace in a subprogram");
1042   char* new_namespace = namespace == NULL ? xbt_strdup(name)
1043     : bprintf("%s::%s", namespace, name);
1044   MC_dwarf_handle_children(info, die, unit, frame, new_namespace);
1045 }
1046
1047 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
1048   Dwarf_Die child;
1049   int res;
1050   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
1051     MC_dwarf_handle_die(info, &child, unit, frame, namespace);
1052   }
1053 }
1054
1055 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame, const char* namespace) {
1056   int tag = dwarf_tag(die);
1057   mc_tag_class klass = MC_dwarf_tag_classify(tag);
1058   switch (klass) {
1059
1060     // Type:
1061     case mc_tag_type:
1062       MC_dwarf_handle_type_die(info, die, unit, frame, namespace);
1063       break;
1064
1065     // Program:
1066     case mc_tag_subprogram:
1067       MC_dwarf_handle_subprogram_die(info, die, unit, frame, namespace);
1068       return;
1069
1070     // Variable:
1071     case mc_tag_variable:
1072       MC_dwarf_handle_variable_die(info, die, unit, frame, namespace);
1073       break;
1074
1075     // Scope:
1076     case mc_tag_scope:
1077       // TODO
1078       break;
1079
1080     case mc_tag_namespace:
1081       mc_dwarf_handle_namespace_die(info, die, unit, frame, namespace);
1082       break;
1083
1084     default:
1085       break;
1086
1087   }
1088 }
1089
1090 void MC_dwarf_get_variables(mc_object_info_t info) {
1091   int fd = open(info->file_name, O_RDONLY);
1092   if (fd<0) {
1093     xbt_die("Could not open file %s", info->file_name);
1094   }
1095   Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
1096   if (dwarf==NULL) {
1097     xbt_die("Your program must be compiled with -g");
1098   }
1099
1100   Dwarf_Off offset = 0;
1101   Dwarf_Off next_offset = 0;
1102   size_t length;
1103   while (dwarf_nextcu (dwarf, offset, &next_offset, &length, NULL, NULL, NULL) == 0) {
1104     Dwarf_Die unit_die;
1105
1106     if(dwarf_offdie(dwarf, offset+length, &unit_die)!=NULL) {
1107       Dwarf_Die child;
1108       int res;
1109       for (res=dwarf_child(&unit_die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
1110         MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
1111       }
1112     }
1113     offset = next_offset;
1114   }
1115
1116   dwarf_end(dwarf);
1117   close(fd);
1118 }