Logo AND Algorithmique Numérique Distribuée

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