Logo AND Algorithmique Numérique Distribuée

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