Logo AND Algorithmique Numérique Distribuée

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