Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ccf89dea23da09395beef72585c344fd1ac49ca8
[simgrid.git] / src / mc / mc_dwarf.c
1 /* Copyright (c) 2008-2013. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdlib.h>
8 #include <dwarf.h>
9 #include <elfutils/libdw.h>
10 #include <inttypes.h>
11
12 #include <simgrid_config.h>
13 #include <xbt/log.h>
14 #include <xbt/sysdep.h>
15
16 #include "mc_private.h"
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing");
19
20 /** \brief The default DW_TAG_lower_bound for a given DW_AT_language.
21  *
22  *  The default for a given language is defined in the DWARF spec.
23  *
24  *  \param language consant as defined by the DWARf spec
25  */
26 static uint64_t MC_dwarf_default_lower_bound(int lang);
27
28 /** \brief Computes the the element_count of a DW_TAG_enumeration_type DIE
29  *
30  * This is the number of elements in a given array dimension.
31  *
32  * A reference of the compilation unit (DW_TAG_compile_unit) is
33  * needed because the default lower bound (when there is no DW_AT_lower_bound)
34  * depends of the language of the compilation unit (DW_AT_language).
35  *
36  * \param die  DIE for the DW_TAG_enumeration_type or DW_TAG_subrange_type
37  * \param unit DIE of the DW_TAG_compile_unit
38  */
39 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit);
40
41 /** \brief Computes the number of elements of a given DW_TAG_array_type.
42  *
43  * \param die DIE for the DW_TAG_array_type
44  */
45 static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit);
46
47 /** \brief Process a DIE
48  *
49  *  \param info the resulting object fot the library/binary file (output)
50  *  \param die  the current DIE
51  *  \param unit the DIE of the compile unit of the current DIE
52  *  \param frame containg frame if any
53  */
54 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame);
55
56 /** \brief Process a type DIE
57  */
58 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit);
59
60 /** \brief Calls MC_dwarf_handle_die on all childrend of the given die
61  *
62  *  \param info the resulting object fot the library/binary file (output)
63  *  \param die  the current DIE
64  *  \param unit the DIE of the compile unit of the current DIE
65  *  \param frame containg frame if any
66  */
67 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame);
68
69 /** \brief Handle a variable (DW_TAG_variable or other)
70  *
71  *  \param info the resulting object fot the library/binary file (output)
72  *  \param die  the current DIE
73  *  \param unit the DIE of the compile unit of the current DIE
74  *  \param frame containg frame if any
75  */
76 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame);
77
78 /** \brief Convert a libdw DWARF expression into a MC representation of the location
79  *
80  *  \param expr array of DWARf operations
81  *  \param len  number of elements
82  *  \return a new MC expression
83  */
84 static dw_location_t MC_dwarf_get_expression(Dwarf_Op* expr,  size_t len);
85
86 /** \brief Get the DW_TAG_type of the DIE
87  *
88  *  \param die DIE
89  *  \return DW_TAG_type attribute as a new string (NULL if none)
90  */
91 static char* MC_dwarf_at_type(Dwarf_Die* die);
92
93 /** \brief Get the name of an attribute (DW_AT_*) from its code
94  *
95  *  \param attr attribute code (see the DWARF specification)
96  *  \return name of the attribute
97  */
98 const char* MC_dwarf_attrname(int attr) {
99   switch (attr) {
100 #include "mc_dwarf_attrnames.h"
101   default:
102     return "DW_AT_unkown";
103   }
104 }
105
106 /** \brief Get the name of a dwarf tag (DW_TAG_*) from its code
107  *
108  *  \param tag tag code (see the DWARF specification)
109  *  \return name of the tag
110  */
111 const char* MC_dwarf_tagname(int tag) {
112   switch (tag) {
113 #include "mc_dwarf_tagnames.h"
114   case DW_TAG_invalid:
115     return "DW_TAG_invalid";
116   default:
117     return "DW_TAG_unkown";
118   }
119 }
120
121 /** \brief Get the name of the tag of a given DIE
122  *
123  *  \param die DIE
124  *  \return name of the tag of this DIE
125  */
126 static inline const char* MC_dwarf_die_tagname(Dwarf_Die* die) {
127   return MC_dwarf_tagname(dwarf_tag(die));
128 }
129
130 // ***** Attributes
131
132 /** \brief Get an attribute of a given DIE as a string
133  *
134  *  \param the DIE
135  *  \param attribute attribute
136  *  \return value of the given attribute of the given DIE
137  */
138 static const char* MC_dwarf_attr_string(Dwarf_Die* die, int attribute) {
139   Dwarf_Attribute attr;
140   if (!dwarf_attr_integrate(die, attribute, &attr)) {
141         return NULL;
142   } else {
143         return dwarf_formstring(&attr);
144   }
145 }
146
147 /** \brief Get the linkage name of a DIE.
148  *
149  *  Use either DW_AT_linkage_name or DW_AR_MIPS_linkage_name.
150  *
151  *  \param DIE
152  *  \return linkage name of the given DIE (or NULL)
153  * */
154 static const char* MC_dwarf_at_linkage_name(Dwarf_Die* die) {
155   const char* name = MC_dwarf_attr_string(die, DW_AT_linkage_name);
156   if (!name)
157     name = MC_dwarf_attr_string(die, DW_AT_MIPS_linkage_name);
158   return name;
159 }
160
161 /** \brief Create a location list from a given attribute
162  *
163  *  \param die the DIE
164  *  \param attr the attribute
165  *  \return MC specific representation of the location list represented by the given attribute
166  *  of the given die
167  */
168 static dw_location_t MC_dwarf_get_location_list_libdw(Dwarf_Die* die, Dwarf_Attribute* attr) {
169
170
171   dw_location_t location = xbt_new0(s_dw_location_t, 1);
172   location->type = e_dw_loclist;
173   xbt_dynar_t loclist = xbt_dynar_new(sizeof(dw_location_entry_t), NULL);
174   location->location.loclist = loclist;
175
176   ptrdiff_t offset = 0;
177   Dwarf_Addr base, start, end;
178   Dwarf_Op *expr;
179   size_t len;
180
181   while (1) {
182
183     offset = dwarf_getlocations(attr, offset, &base, &start, &end, &expr, &len);
184     if (offset==0)
185       return location;
186     else if (offset==-1)
187       xbt_die("Error while loading location list");
188
189     dw_location_entry_t new_entry = xbt_new0(s_dw_location_entry_t, 1);
190     new_entry->lowpc = start;
191     new_entry->highpc = end;
192     new_entry->location = MC_dwarf_get_expression(expr, len);
193
194     xbt_dynar_push(loclist, &new_entry);
195
196   }
197 }
198
199 /** \brief Get the location expression or location list from an attribute
200  *
201  *  Processes direct expressions as well as location lists.
202  *
203  *  \param die the DIE
204  *  \param attr the attribute
205  *  \return MC specific representation of the location represented by the given attribute
206  *  of the given die
207  */
208 static dw_location_t MC_dwarf_get_location(Dwarf_Die* die, Dwarf_Attribute* attr) {
209   int form = dwarf_whatform(attr);
210   switch (form) {
211
212   // The attribute is an DWARF location expression:
213   case DW_FORM_exprloc:
214   case DW_FORM_block1: // not in the spec
215   case DW_FORM_block2:
216   case DW_FORM_block4:
217   case DW_FORM_block:
218     {
219       Dwarf_Op* expr;
220       size_t len;
221       if (dwarf_getlocation(attr, &expr, &len))
222         xbt_die("Could not read location expression");
223       return MC_dwarf_get_expression(expr, len);
224     }
225
226   // The attribute is a reference to a location list entry:
227   case DW_FORM_sec_offset:
228   case DW_FORM_data2:
229   case DW_FORM_data4:
230   case DW_FORM_data8:
231     {
232       return MC_dwarf_get_location_list_libdw(die, attr);
233     }
234     break;
235
236   default:
237     xbt_die("Unexpected form %i list for location in attribute %s of <%p>%s",
238       form,
239       MC_dwarf_attrname(attr->code),
240       (void*) dwarf_dieoffset(die),
241       MC_dwarf_attr_string(die, DW_AT_name));
242     return NULL;
243   }
244 }
245
246 /** \brief Get the location expression or location list from an attribute
247  *
248  *  Processes direct expressions as well as location lists.
249  *
250  *  \param die the DIE
251  *  \param attribute the attribute code
252  *  \return MC specific representation of the location represented by the given attribute
253  *  of the given die
254  */
255 static dw_location_t MC_dwarf_at_location(Dwarf_Die* die, int attribute, mc_object_info_t info) {
256   if(!dwarf_hasattr_integrate(die, attribute))
257     return xbt_new0(s_dw_location_t, 1);
258
259   Dwarf_Attribute attr;
260   dwarf_attr_integrate(die, attribute, &attr);
261   return MC_dwarf_get_location(die, &attr);
262 }
263
264 static char* MC_dwarf_at_type(Dwarf_Die* die) {
265   Dwarf_Attribute attr;
266   if (dwarf_hasattr_integrate(die, DW_AT_type)) {
267         dwarf_attr_integrate(die, DW_AT_type, &attr);
268         Dwarf_Die subtype_die;
269         if (dwarf_formref_die(&attr, &subtype_die)==NULL) {
270           xbt_die("Could not find DIE for type");
271         }
272         Dwarf_Off subtype_global_offset = dwarf_dieoffset(&subtype_die);
273     return bprintf("%" PRIx64 , subtype_global_offset);
274   }
275   else return NULL;
276 }
277
278 static uint64_t MC_dwarf_attr_addr(Dwarf_Die* die, int attribute) {
279   Dwarf_Attribute attr;
280   if(dwarf_attr_integrate(die, attribute, &attr)==NULL)
281     return 0;
282   Dwarf_Addr value;
283   if (dwarf_formaddr(&attr, &value) == 0)
284     return (uint64_t) value;
285   else
286     return 0;
287 }
288
289 static uint64_t MC_dwarf_attr_uint(Dwarf_Die* die, int attribute, uint64_t default_value) {
290   Dwarf_Attribute attr;
291   if (dwarf_attr_integrate(die, attribute, &attr)==NULL)
292     return default_value;
293   Dwarf_Word value;
294   return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr), &value) == 0 ? (uint64_t) value : default_value;
295 }
296
297 static bool MC_dwarf_attr_flag(Dwarf_Die* die, int attribute, int integrate) {
298   Dwarf_Attribute attr;
299   if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
300                     : dwarf_attr(die, attribute, &attr))==0)
301     return false;
302
303   bool result;
304   if (dwarf_formflag(&attr, &result))
305     xbt_die("Unexpected form for attribute %s",
306       MC_dwarf_attrname(attribute));
307   return result;
308 }
309
310 static uint64_t MC_dwarf_default_lower_bound(int lang) {
311   switch(lang) {
312   case DW_LANG_C:
313   case DW_LANG_C89:
314   case DW_LANG_C99:
315   case DW_LANG_C_plus_plus:
316   case DW_LANG_D:
317   case DW_LANG_Java:
318   case DW_LANG_ObjC:
319   case DW_LANG_ObjC_plus_plus:
320   case DW_LANG_Python:
321   case DW_LANG_UPC:
322     return 0;
323   case DW_LANG_Ada83:
324   case DW_LANG_Ada95:
325   case DW_LANG_Fortran77:
326   case DW_LANG_Fortran90:
327   case DW_LANG_Fortran95:
328   case DW_LANG_Modula2:
329   case DW_LANG_Pascal83:
330   case DW_LANG_PL1:
331   case DW_LANG_Cobol74:
332   case DW_LANG_Cobol85:
333     return 1;
334   default:
335     xbt_die("No default MT_TAG_lower_bound for language %i and none given", lang);
336     return 0;
337   }
338 }
339
340 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
341   xbt_assert(dwarf_tag(die)==DW_TAG_enumeration_type ||dwarf_tag(die)==DW_TAG_subrange_type,
342       "MC_dwarf_subrange_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
343
344   // Use DW_TAG_count if present:
345   if (dwarf_hasattr_integrate(die, DW_AT_count)) {
346     return MC_dwarf_attr_uint(die, DW_AT_count, 0);
347   }
348
349   // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound:
350
351   if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
352         // This is not really 0, but the code expects this (we do not know):
353     return 0;
354   }
355   uint64_t upper_bound = MC_dwarf_attr_uint(die, DW_AT_upper_bound, -1);
356
357   uint64_t lower_bound = 0;
358   if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
359     lower_bound = MC_dwarf_attr_uint(die, DW_AT_lower_bound, -1);
360   } else {
361         lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
362   }
363   return upper_bound - lower_bound;
364 }
365
366 static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
367   xbt_assert(dwarf_tag(die)==DW_TAG_array_type,
368     "MC_dwarf_array_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
369
370   int result = 1;
371   Dwarf_Die child;
372   int res;
373   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
374         int child_tag = dwarf_tag(&child);
375     if (child_tag==DW_TAG_subrange_type ||child_tag==DW_TAG_enumeration_type) {
376       result *= MC_dwarf_subrange_element_count(&child, unit);
377     }
378   }
379   return result;
380 }
381
382 // ***** Location
383
384 Dwarf_Off MC_dwarf_resolve_location(unw_cursor_t* c, dw_location_t location, void* frame_pointer_address) {
385   unw_word_t res;
386   switch (location->type){
387   case e_dw_compose:
388     if (xbt_dynar_length(location->location.compose) > 1){
389       return 0; /* TODO : location list with optimizations enabled */
390     }
391     dw_location_t location_entry = xbt_dynar_get_as(location->location.compose, 0, dw_location_t);
392     switch (location_entry->type){
393     case e_dw_register:
394       unw_get_reg(c, location_entry->location.reg, &res);
395       return res;
396     case e_dw_bregister_op:
397       unw_get_reg(c, location_entry->location.breg_op.reg, &res);
398       return (Dwarf_Off) ((long)res + location_entry->location.breg_op.offset);
399       break;
400     case e_dw_fbregister_op:
401       if (frame_pointer_address != NULL)
402         return (Dwarf_Off)((char *)frame_pointer_address + location_entry->location.fbreg_op);
403       else
404         return 0;
405     default:
406       return 0; /* FIXME : implement other cases (with optimizations enabled) */
407     }
408     break;
409     default:
410       return 0;
411   }
412 }
413
414 // ***** dw_type_t
415
416 static void MC_dwarf_fill_member_location(dw_type_t type, dw_type_t member, Dwarf_Die* child) {
417   if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
418     xbt_die("Can't groke DW_AT_data_bit_offset.");
419   }
420
421   if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
422     if (type->type != DW_TAG_union_type) {
423         xbt_die(
424           "Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%p>%s",
425           member->name, type->id, type->name);
426     } else {
427       return;
428     }
429   }
430
431   Dwarf_Attribute attr;
432   dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
433   switch (dwarf_whatform(&attr)) {
434
435   case DW_FORM_exprloc:
436     {
437       Dwarf_Op* expr;
438       size_t len;
439       if (dwarf_getlocation(&attr, &expr, &len)) {
440         xbt_die(
441           "Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%p>%s",
442           MC_dwarf_attr_string(child, DW_AT_name),
443           type->id, type->name);
444       }
445       if (len==1 && expr[0].atom == DW_OP_plus_uconst) {
446         member->offset =  expr[0].number;
447       } else {
448         xbt_die("Can't groke this location expression yet. %i %i",
449           len==1 , expr[0].atom == DW_OP_plus_uconst);
450       }
451       break;
452     }
453   case DW_FORM_data1:
454   case DW_FORM_data2:
455   case DW_FORM_data4:
456   case DW_FORM_data8:
457   case DW_FORM_sdata:
458   case DW_FORM_udata:
459     {
460       Dwarf_Word offset;
461       if (!dwarf_formudata(&attr, &offset))
462         member->offset = offset;
463       else
464         xbt_die("Cannot get DW_AT_data_member_%s location <%p>%s",
465           MC_dwarf_attr_string(child, DW_AT_name),
466           type->id, type->name);
467       break;
468     }
469   }
470 }
471
472 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_type_t type) {
473   int res;
474   Dwarf_Die child;
475   xbt_assert(!type->members);
476   type->members = xbt_dynar_new(sizeof(dw_type_t), (void(*)(void*))dw_type_free);
477   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
478     if (dwarf_tag(&child)==DW_TAG_member) {
479       // TODO, we should use another type (because is is not a type but a member)
480       dw_type_t member = xbt_new0(s_dw_type_t, 1);
481       member->type = -1;
482       member->id = NULL;
483
484       const char* name = MC_dwarf_attr_string(&child, DW_AT_name);
485       if(name)
486         member->name = xbt_strdup(name);
487       else
488         member->name = NULL;
489
490       member->byte_size = MC_dwarf_attr_uint(&child, DW_AT_byte_size, 0);
491       member->element_count = -1;
492       member->dw_type_id = MC_dwarf_at_type(&child);
493       member->members = NULL;
494       member->is_pointer_type = 0;
495       member->offset = 0;
496
497       if(dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
498         xbt_die("Can't groke DW_AT_data_bit_offset.");
499       }
500
501       MC_dwarf_fill_member_location(type, member, &child);
502
503       if (!member->dw_type_id) {
504         xbt_die("Missing type for member %s of <%p>%s", member->name, type->id, type->name);
505       }
506
507       xbt_dynar_push(type->members, &member);
508     }
509   }
510 }
511
512 /** \brief Create a MC type object from a DIE
513  *
514  *  \param info current object info object
515  *  \param DIE (for a given type);
516  *  \param unit compilation unit of the current DIE
517  *  \return MC representation of the type
518  */
519 static dw_type_t MC_dwarf_die_to_type(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit) {
520
521   dw_type_t type = xbt_new0(s_dw_type_t, 1);
522   type->type = -1;
523   type->id = NULL;
524   type->name = NULL;
525   type->byte_size = 0;
526   type->element_count = -1;
527   type->dw_type_id = NULL;
528   type->members = NULL;
529   type->is_pointer_type = 0;
530   type->offset = 0;
531
532   type->type = dwarf_tag(die);
533
534   // Global Offset
535   type->id = (void *) dwarf_dieoffset(die);
536
537   const char* name = MC_dwarf_attr_string(die, DW_AT_name);
538   if (name!=NULL) {
539         type->name = xbt_strdup(name);
540   }
541
542   XBT_DEBUG("Processing type <%p>%s", type->id, type->name);
543
544   type->dw_type_id = MC_dwarf_at_type(die);
545
546   // Computation of the byte_size;
547   if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
548     type->byte_size = MC_dwarf_attr_uint(die, DW_AT_byte_size, 0);
549   else if (type->type == DW_TAG_array_type || type->type==DW_TAG_structure_type || type->type==DW_TAG_class_type) {
550     Dwarf_Word size;
551     if (dwarf_aggregate_size(die, &size)==0) {
552       type->byte_size = size;
553     }
554   }
555
556   switch (type->type) {
557   case DW_TAG_array_type:
558         type->element_count = MC_dwarf_array_element_count(die, unit);
559         // TODO, handle DW_byte_stride and (not) DW_bit_stride
560         break;
561
562   case DW_TAG_pointer_type:
563   case DW_TAG_reference_type:
564   case DW_TAG_rvalue_reference_type:
565     type->is_pointer_type = 1;
566     break;
567
568   case DW_TAG_structure_type:
569   case DW_TAG_union_type:
570   case DW_TAG_class_type:
571           MC_dwarf_add_members(info, die, unit, type);
572   }
573
574   return type;
575 }
576
577 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit) {
578   dw_type_t type = MC_dwarf_die_to_type(info, die, unit);
579
580   char* key = bprintf("%" PRIx64, (uint64_t) type->id);
581   xbt_dict_set(info->types, key, type, NULL);
582 }
583
584 /** \brief Convert libdw location expresion elment into native one (or NULL in some cases) */
585 static dw_location_t MC_dwarf_get_expression_element(Dwarf_Op* op) {
586   dw_location_t element = xbt_new0(s_dw_location_t, 1);
587   uint8_t atom = op->atom;
588   if (atom >= DW_OP_reg0 && atom<= DW_OP_reg31) {
589     element->type = e_dw_register;
590     element->location.reg = atom - DW_OP_reg0;
591   }
592   else if (atom >= DW_OP_breg0 && atom<= DW_OP_breg31) {
593     element->type = e_dw_bregister_op;
594     element->location.reg = atom - DW_OP_breg0;
595     element->location.breg_op.offset = op->number;
596   }
597   else if (atom >= DW_OP_lit0 && atom<= DW_OP_lit31) {
598     element->type = e_dw_lit;
599     element->location.reg = atom - DW_OP_lit0;
600   }
601   else switch (atom) {
602   case DW_OP_fbreg:
603     element->type = e_dw_fbregister_op;
604     element->location.fbreg_op = op->number;
605     break;
606   case DW_OP_piece:
607     element->type = e_dw_piece;
608     element->location.piece = op->number;
609     break;
610   case DW_OP_plus_uconst:
611     element->type = e_dw_plus_uconst;
612     element->location.plus_uconst = op->number;
613     break;
614   case DW_OP_abs:
615     element->type = e_dw_arithmetic;
616     element->location.arithmetic = xbt_strdup("abs");
617     break;
618   case DW_OP_and:
619     element->type = e_dw_arithmetic;
620     element->location.arithmetic = xbt_strdup("and");
621     break;
622   case DW_OP_div:
623     element->type = e_dw_arithmetic;
624     element->location.arithmetic = xbt_strdup("div");
625     break;
626   case DW_OP_minus:
627     element->type = e_dw_arithmetic;
628     element->location.arithmetic = xbt_strdup("minus");
629     break;
630   case DW_OP_mod:
631     element->type = e_dw_arithmetic;
632     element->location.arithmetic = xbt_strdup("mod");
633     break;
634   case DW_OP_mul:
635     element->type = e_dw_arithmetic;
636     element->location.arithmetic = xbt_strdup("mul");
637     break;
638   case DW_OP_neg:
639     element->type = e_dw_arithmetic;
640     element->location.arithmetic = xbt_strdup("neg");
641     break;
642   case DW_OP_not:
643     element->type = e_dw_arithmetic;
644     element->location.arithmetic = xbt_strdup("not");
645     break;
646   case DW_OP_or:
647     element->type = e_dw_arithmetic;
648     element->location.arithmetic = xbt_strdup("or");
649     break;
650   case DW_OP_plus:
651     element->type = e_dw_arithmetic;
652     element->location.arithmetic = xbt_strdup("plus");
653     break;
654
655   case DW_OP_stack_value:
656     // Why nothing here?
657     xbt_free(element);
658     return NULL;
659
660   case DW_OP_deref_size:
661     element->type = e_dw_deref;
662     element->location.deref_size =  (unsigned int short) op->number;
663     break;
664   case DW_OP_deref:
665     element->type = e_dw_deref;
666     element->location.deref_size = sizeof(void *);
667     break;
668   case DW_OP_constu:
669     element->type = e_dw_uconstant;
670     element->location.uconstant.bytes = 1;
671     element->location.uconstant.value = (unsigned long int) op->number;
672     break;
673   case DW_OP_consts:
674     element->type = e_dw_sconstant;
675     element->location.uconstant.bytes = 1;
676     element->location.uconstant.value = (unsigned long int) op->number;
677     break;
678
679   case DW_OP_const1u:
680     element->type = e_dw_uconstant;
681     element->location.uconstant.bytes = 1;
682     element->location.uconstant.value = (unsigned long int) op->number;
683     break;
684   case DW_OP_const2u:
685     element->type = e_dw_uconstant;
686     element->location.uconstant.bytes = 2;
687     element->location.uconstant.value = (unsigned long int) op->number;
688     break;
689   case DW_OP_const4u:
690     element->type = e_dw_uconstant;
691     element->location.uconstant.bytes = 4;
692     element->location.uconstant.value = (unsigned long int) op->number;
693     break;
694   case DW_OP_const8u:
695     element->type = e_dw_uconstant;
696     element->location.uconstant.bytes = 8;
697     element->location.uconstant.value = (unsigned long int) op->number;
698     break;
699
700   case DW_OP_const1s:
701     element->type = e_dw_sconstant;
702     element->location.uconstant.bytes = 1;
703     element->location.uconstant.value = (unsigned long int) op->number;
704     break;
705   case DW_OP_const2s:
706     element->type = e_dw_sconstant;
707     element->location.uconstant.bytes = 2;
708     element->location.uconstant.value = (unsigned long int) op->number;
709     break;
710   case DW_OP_const4s:
711     element->type = e_dw_sconstant;
712     element->location.uconstant.bytes = 4;
713     element->location.uconstant.value = (unsigned long int) op->number;
714     break;
715   case DW_OP_const8s:
716     element->type = e_dw_sconstant;
717     element->location.uconstant.bytes = 8;
718     element->location.uconstant.value = (unsigned long int) op->number;
719     break;
720   default:
721     element->type = e_dw_unsupported;
722     break;
723   }
724   return element;
725 }
726
727 /** \brief Convert libdw location expresion into native one */
728 static dw_location_t MC_dwarf_get_expression(Dwarf_Op* expr,  size_t len) {
729   dw_location_t loc = xbt_new0(s_dw_location_t, 1);
730   loc->type = e_dw_compose;
731   loc->location.compose = xbt_dynar_new(sizeof(dw_location_t), NULL);
732
733   int i;
734   for (i=0; i!=len; ++i) {
735     dw_location_t element =  MC_dwarf_get_expression_element(expr+i);
736     if (element)
737       xbt_dynar_push(loc->location.compose, &element);
738   }
739
740   return loc;
741 }
742
743 static dw_variable_t MC_die_to_variable(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame) {
744   // Drop declaration:
745   if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
746     return NULL;
747
748   Dwarf_Attribute attr_location;
749   if (dwarf_attr(die, DW_AT_location, &attr_location)==NULL) {
750     // No location: do not add it ?
751     return NULL;
752   }
753
754   dw_variable_t variable = xbt_new0(s_dw_variable_t, 1);
755   variable->dwarf_offset = dwarf_dieoffset(die);
756   variable->global = frame == NULL; // Can be override base on DW_AT_location
757   variable->name = xbt_strdup(MC_dwarf_attr_string(die, DW_AT_name));
758   variable->type_origin = MC_dwarf_at_type(die);
759   variable->address.address = NULL;
760
761   int form;
762   switch (form = dwarf_whatform(&attr_location)) {
763   case DW_FORM_exprloc:
764   case DW_FORM_block1: // Not in the spec but found in the wild.
765     {
766       Dwarf_Op* expr;
767       size_t len;
768       if (dwarf_getlocation(&attr_location, &expr, &len)) {
769         xbt_die(
770           "Could not read location expression in DW_AT_location of variable <%p>%s",
771           (void*) variable->dwarf_offset, variable->name);
772       }
773
774       if (len==1 && expr[0].atom == DW_OP_addr) {
775         variable->global = 1;
776         Dwarf_Off offset = expr[0].number;
777         // TODO, Why is this different base on the object?
778         Dwarf_Off base = strcmp(info->file_name, xbt_binary_name) !=0 ? (Dwarf_Off) info->start_text : 0;
779         variable->address.address = (void*) (base + offset);
780       } else {
781         variable->address.location = MC_dwarf_get_expression(expr, len);
782       }
783
784       break;
785     }
786   case DW_FORM_sec_offset: // type loclistptr
787   case DW_FORM_data4:
788     xbt_die("Do not handle loclist locations yet");
789     break;
790   default:
791     xbt_die("Unexpected form %i list for location in <%p>%s",
792       form, (void*) variable->dwarf_offset, variable->name);
793   }
794
795   return variable;
796 }
797
798 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame) {
799   dw_variable_t variable = MC_die_to_variable(info, die, unit, frame);
800   if(variable==NULL)
801       return;
802   MC_dwarf_register_variable(info, frame, variable);
803 }
804
805 static void MC_dwarf_handle_subprogram_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t parent_frame) {
806   dw_frame_t frame = xbt_new0(s_dw_frame_t, 1);
807
808   frame->start = dwarf_dieoffset(die);
809
810   const char* name = MC_dwarf_at_linkage_name(die);
811   if (name==NULL)
812     name = MC_dwarf_attr_string(die, DW_AT_name);
813   frame->name = xbt_strdup(name);
814
815   // Variables are filled in the (recursive) call of MC_dwarf_handle_children:
816   frame->variables = xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
817   frame->high_pc = (void*) MC_dwarf_attr_addr(die, DW_AT_high_pc);
818   frame->low_pc = (void*) MC_dwarf_attr_addr(die, DW_AT_low_pc);
819   frame->frame_base = MC_dwarf_at_location(die, DW_AT_frame_base, info);
820   frame->end = -1; // This one is now useless:
821
822   // Handle children:
823   MC_dwarf_handle_children(info, die, unit, frame);
824
825   // Register it:
826   xbt_dict_set(info->local_variables, frame->name, frame, NULL);
827 }
828
829 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame) {
830   Dwarf_Die child;
831   int res;
832   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
833     MC_dwarf_handle_die(info, &child, unit, frame);
834   }
835 }
836
837 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame) {
838   int tag = dwarf_tag(die);
839   switch (tag) {
840     case DW_TAG_array_type:
841     case DW_TAG_class_type:
842     case DW_TAG_enumeration_type:
843     case DW_TAG_typedef:
844     case DW_TAG_pointer_type:
845     case DW_TAG_string_type:
846     case DW_TAG_structure_type:
847     case DW_TAG_subroutine_type:
848     case DW_TAG_union_type:
849     case DW_TAG_ptr_to_member_type:
850     case DW_TAG_set_type:
851     case DW_TAG_subrange_type:
852     case DW_TAG_base_type:
853     case DW_TAG_const_type:
854     case DW_TAG_file_type:
855     case DW_TAG_packed_type:
856     case DW_TAG_volatile_type:
857     case DW_TAG_restrict_type:
858     case DW_TAG_interface_type:
859     case DW_TAG_unspecified_type:
860     case DW_TAG_mutable_type:
861     case DW_TAG_shared_type:
862       MC_dwarf_handle_type_die(info, die, unit);
863       break;
864     case DW_TAG_inlined_subroutine:
865     case DW_TAG_subprogram:
866       MC_dwarf_handle_subprogram_die(info, die, unit, frame);
867       return;
868     // case DW_TAG_formal_parameter:
869     case DW_TAG_variable:
870       MC_dwarf_handle_variable_die(info, die, unit, frame);
871       break;
872   }
873
874   // Recursive processing of children DIE:
875   MC_dwarf_handle_children(info, die, unit, frame);
876 }
877
878 void MC_dwarf_get_variables(mc_object_info_t info) {
879   int fd = open(info->file_name, O_RDONLY);
880   if (fd<0) {
881     xbt_die("Could not open file %s", info->file_name);
882   }
883   Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
884   if (dwarf==NULL) {
885     xbt_die("Your program must be compiled with -g");
886   }
887
888   Dwarf_Off offset = 0;
889   Dwarf_Off next_offset = 0;
890   size_t length;
891   while (dwarf_nextcu (dwarf, offset, &next_offset, &length, NULL, NULL, NULL) == 0) {
892     Dwarf_Die die;
893     if(dwarf_offdie(dwarf, offset+length, &die)!=NULL) {
894       MC_dwarf_handle_die(info, &die, &die, NULL);
895     }
896     offset = next_offset;
897   }
898
899   dwarf_end(dwarf);
900   close(fd);
901 }