Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Make type_is an integer instead of a string and use std:: containers
[simgrid.git] / src / mc / mc_dwarf.cpp
1 /* Copyright (c) 2008-2014. 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 <cinttypes>
8
9 #include <memory>
10
11 #include <stdlib.h>
12 #define DW_LANG_Objc DW_LANG_ObjC       /* fix spelling error in older dwarf.h */
13 #include <dwarf.h>
14 #include <elfutils/libdw.h>
15
16 #include <simgrid_config.h>
17 #include <xbt/log.h>
18 #include <xbt/sysdep.h>
19
20 #include "mc_object_info.h"
21 #include "mc_private.h"
22
23 static void MC_dwarf_register_global_variable(
24   mc_object_info_t info, std::unique_ptr<simgrid::mc::Variable> variable);
25 static void MC_register_variable(
26   mc_object_info_t info, mc_frame_t frame, std::unique_ptr<simgrid::mc::Variable> variable);
27 static void MC_dwarf_register_non_global_variable(mc_object_info_t info, mc_frame_t frame, mc_variable_t variable);
28 static void MC_dwarf_register_variable(
29   mc_object_info_t info, mc_frame_t frame,
30   std::unique_ptr<simgrid::mc::Variable> variable);
31
32 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing");
33
34 /** \brief The default DW_TAG_lower_bound for a given DW_AT_language.
35  *
36  *  The default for a given language is defined in the DWARF spec.
37  *
38  *  \param language consant as defined by the DWARf spec
39  */
40 static uint64_t MC_dwarf_default_lower_bound(int lang);
41
42 /** \brief Computes the the element_count of a DW_TAG_enumeration_type DIE
43  *
44  * This is the number of elements in a given array dimension.
45  *
46  * A reference of the compilation unit (DW_TAG_compile_unit) is
47  * needed because the default lower bound (when there is no DW_AT_lower_bound)
48  * depends of the language of the compilation unit (DW_AT_language).
49  *
50  * \param die  DIE for the DW_TAG_enumeration_type or DW_TAG_subrange_type
51  * \param unit DIE of the DW_TAG_compile_unit
52  */
53 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
54                                                 Dwarf_Die * unit);
55
56 /** \brief Computes the number of elements of a given DW_TAG_array_type.
57  *
58  * \param die DIE for the DW_TAG_array_type
59  */
60 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit);
61
62 /** \brief Process a DIE
63  *
64  *  \param info the resulting object fot the library/binary file (output)
65  *  \param die  the current DIE
66  *  \param unit the DIE of the compile unit of the current DIE
67  *  \param frame containg frame if any
68  */
69 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
70                                 Dwarf_Die * unit, mc_frame_t frame,
71                                 const char *ns);
72
73 /** \brief Process a type DIE
74  */
75 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
76                                      Dwarf_Die * unit, mc_frame_t frame,
77                                      const char *ns);
78
79 /** \brief Calls MC_dwarf_handle_die on all childrend of the given die
80  *
81  *  \param info the resulting object fot the library/binary file (output)
82  *  \param die  the current DIE
83  *  \param unit the DIE of the compile unit of the current DIE
84  *  \param frame containg frame if any
85  */
86 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
87                                      Dwarf_Die * unit, mc_frame_t frame,
88                                      const char *ns);
89
90 /** \brief Handle a variable (DW_TAG_variable or other)
91  *
92  *  \param info the resulting object fot the library/binary file (output)
93  *  \param die  the current DIE
94  *  \param unit the DIE of the compile unit of the current DIE
95  *  \param frame containg frame if any
96  */
97 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
98                                          Dwarf_Die * unit, mc_frame_t frame,
99                                          const char *ns);
100
101 /** \brief Get the DW_TAG_type of the DIE
102  *
103  *  \param die DIE
104  *  \return DW_TAG_type attribute as a new string (NULL if none)
105  */
106 static std::uint64_t MC_dwarf_at_type(Dwarf_Die * die);
107
108 /** \brief A class of DWARF tags (DW_TAG_*)
109  */
110 typedef enum mc_tag_class {
111   mc_tag_unknown,
112   mc_tag_type,
113   mc_tag_subprogram,
114   mc_tag_variable,
115   mc_tag_scope,
116   mc_tag_namespace
117 } mc_tag_class;
118
119 static mc_tag_class MC_dwarf_tag_classify(int tag)
120 {
121   switch (tag) {
122
123   case DW_TAG_array_type:
124   case DW_TAG_class_type:
125   case DW_TAG_enumeration_type:
126   case DW_TAG_typedef:
127   case DW_TAG_pointer_type:
128   case DW_TAG_reference_type:
129   case DW_TAG_rvalue_reference_type:
130   case DW_TAG_string_type:
131   case DW_TAG_structure_type:
132   case DW_TAG_subroutine_type:
133   case DW_TAG_union_type:
134   case DW_TAG_ptr_to_member_type:
135   case DW_TAG_set_type:
136   case DW_TAG_subrange_type:
137   case DW_TAG_base_type:
138   case DW_TAG_const_type:
139   case DW_TAG_file_type:
140   case DW_TAG_packed_type:
141   case DW_TAG_volatile_type:
142   case DW_TAG_restrict_type:
143   case DW_TAG_interface_type:
144   case DW_TAG_unspecified_type:
145   case DW_TAG_shared_type:
146     return mc_tag_type;
147
148   case DW_TAG_subprogram:
149     return mc_tag_subprogram;
150
151   case DW_TAG_variable:
152   case DW_TAG_formal_parameter:
153     return mc_tag_variable;
154
155   case DW_TAG_lexical_block:
156   case DW_TAG_try_block:
157   case DW_TAG_catch_block:
158   case DW_TAG_inlined_subroutine:
159   case DW_TAG_with_stmt:
160     return mc_tag_scope;
161
162   case DW_TAG_namespace:
163     return mc_tag_namespace;
164
165   default:
166     return mc_tag_unknown;
167
168   }
169 }
170
171 #define MC_DW_CLASS_UNKNOWN 0
172 #define MC_DW_CLASS_ADDRESS 1   // Location in the address space of the program
173 #define MC_DW_CLASS_BLOCK 2     // Arbitrary block of bytes
174 #define MC_DW_CLASS_CONSTANT 3
175 #define MC_DW_CLASS_STRING 3    // String
176 #define MC_DW_CLASS_FLAG 4      // Boolean
177 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
178 #define MC_DW_CLASS_EXPRLOC 6   // DWARF expression/location description
179 #define MC_DW_CLASS_LINEPTR 7
180 #define MC_DW_CLASS_LOCLISTPTR 8
181 #define MC_DW_CLASS_MACPTR 9
182 #define MC_DW_CLASS_RANGELISTPTR 10
183
184 /** \brief Find the DWARF data class for a given DWARF data form
185  *
186  *  This mapping is defined in the DWARF spec.
187  *
188  *  \param form The form (values taken from the DWARF spec)
189  *  \return An internal representation for the corresponding class
190  * */
191 static int MC_dwarf_form_get_class(int form)
192 {
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 {
237   return MC_dwarf_tagname(dwarf_tag(die));
238 }
239
240 // ***** Attributes
241
242 /** \brief Get an attribute of a given DIE as a string
243  *
244  *  \param die       the DIE
245  *  \param attribute attribute
246  *  \return value of the given attribute of the given DIE
247  */
248 static const char *MC_dwarf_attr_integrate_string(Dwarf_Die * die,
249                                                   int attribute)
250 {
251   Dwarf_Attribute attr;
252   if (!dwarf_attr_integrate(die, attribute, &attr)) {
253     return NULL;
254   } else {
255     return dwarf_formstring(&attr);
256   }
257 }
258
259 /** \brief Get the linkage name of a DIE.
260  *
261  *  Use either DW_AT_linkage_name or DW_AT_MIPS_linkage_name.
262  *  DW_AT_linkage_name is standardized since DWARF 4.
263  *  Before this version of DWARF, the MIPS extensions
264  *  DW_AT_MIPS_linkage_name is used (at least by GCC).
265  *
266  *  \param  the DIE
267  *  \return linkage name of the given DIE (or NULL)
268  * */
269 static const char *MC_dwarf_at_linkage_name(Dwarf_Die * die)
270 {
271   const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_linkage_name);
272   if (!name)
273     name = MC_dwarf_attr_integrate_string(die, DW_AT_MIPS_linkage_name);
274   return name;
275 }
276
277 static Dwarf_Off MC_dwarf_attr_dieoffset(Dwarf_Die * die, int attribute)
278 {
279   Dwarf_Attribute attr;
280   if (dwarf_hasattr_integrate(die, attribute)) {
281     dwarf_attr_integrate(die, attribute, &attr);
282     Dwarf_Die subtype_die;
283     if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
284       xbt_die("Could not find DIE");
285     }
286     return dwarf_dieoffset(&subtype_die);
287   } else
288     return 0;
289 }
290
291 static Dwarf_Off MC_dwarf_attr_integrate_dieoffset(Dwarf_Die * die,
292                                                    int attribute)
293 {
294   Dwarf_Attribute attr;
295   if (dwarf_hasattr_integrate(die, attribute)) {
296     dwarf_attr_integrate(die, DW_AT_type, &attr);
297     Dwarf_Die subtype_die;
298     if (dwarf_formref_die(&attr, &subtype_die) == NULL) {
299       xbt_die("Could not find DIE");
300     }
301     return dwarf_dieoffset(&subtype_die);
302   } else
303     return 0;
304 }
305
306 /** \brief Find the type/subtype (DW_AT_type) for a DIE
307  *
308  *  \param dit the DIE
309  *  \return DW_AT_type reference as a global offset in hexadecimal (or NULL)
310  */
311 static
312 std::uint64_t MC_dwarf_at_type(Dwarf_Die * die)
313 {
314   return MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
315 }
316
317 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die * die, int attribute)
318 {
319   Dwarf_Attribute attr;
320   if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
321     return 0;
322   Dwarf_Addr value;
323   if (dwarf_formaddr(&attr, &value) == 0)
324     return (uint64_t) value;
325   else
326     return 0;
327 }
328
329 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die * die, int attribute,
330                                              uint64_t default_value)
331 {
332   Dwarf_Attribute attr;
333   if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
334     return default_value;
335   Dwarf_Word value;
336   return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr),
337                          &value) == 0 ? (uint64_t) value : default_value;
338 }
339
340 static bool MC_dwarf_attr_flag(Dwarf_Die * die, int attribute, bool integrate)
341 {
342   Dwarf_Attribute attr;
343   if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
344        : dwarf_attr(die, attribute, &attr)) == 0)
345     return false;
346
347   bool result;
348   if (dwarf_formflag(&attr, &result))
349     xbt_die("Unexpected form for attribute %s", MC_dwarf_attrname(attribute));
350   return result;
351 }
352
353 /** \brief Find the default lower bound for a given language
354  *
355  *  The default lower bound of an array (when DW_TAG_lower_bound
356  *  is missing) depends on the language of the compilation unit.
357  *
358  *  \param lang Language of the compilation unit (values defined in the DWARF spec)
359  *  \return     Default lower bound of an array in this compilation unit
360  * */
361 static uint64_t MC_dwarf_default_lower_bound(int lang)
362 {
363   switch (lang) {
364   case DW_LANG_C:
365   case DW_LANG_C89:
366   case DW_LANG_C99:
367   case DW_LANG_C_plus_plus:
368   case DW_LANG_D:
369   case DW_LANG_Java:
370   case DW_LANG_ObjC:
371   case DW_LANG_ObjC_plus_plus:
372   case DW_LANG_Python:
373   case DW_LANG_UPC:
374     return 0;
375   case DW_LANG_Ada83:
376   case DW_LANG_Ada95:
377   case DW_LANG_Fortran77:
378   case DW_LANG_Fortran90:
379   case DW_LANG_Fortran95:
380   case DW_LANG_Modula2:
381   case DW_LANG_Pascal83:
382   case DW_LANG_PL1:
383   case DW_LANG_Cobol74:
384   case DW_LANG_Cobol85:
385     return 1;
386   default:
387     xbt_die("No default DW_TAG_lower_bound for language %i and none given",
388             lang);
389     return 0;
390   }
391 }
392
393 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
394  *
395  *  \param die  the DIE
396  *  \param unit DIE of the compilation unit
397  *  \return     number of elements in the range
398  * */
399 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
400                                                 Dwarf_Die * unit)
401 {
402   xbt_assert(dwarf_tag(die) == DW_TAG_enumeration_type
403              || dwarf_tag(die) == DW_TAG_subrange_type,
404              "MC_dwarf_subrange_element_count called with DIE of type %s",
405              MC_dwarf_die_tagname(die));
406
407   // Use DW_TAG_count if present:
408   if (dwarf_hasattr_integrate(die, DW_AT_count)) {
409     return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
410   }
411   // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
412
413   if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
414     // This is not really 0, but the code expects this (we do not know):
415     return 0;
416   }
417   uint64_t upper_bound =
418       MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
419
420   uint64_t lower_bound = 0;
421   if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
422     lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
423   } else {
424     lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
425   }
426   return upper_bound - lower_bound + 1;
427 }
428
429 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
430  *
431  *  The compilation unit might be needed because the default lower
432  *  bound depends on the language of the compilation unit.
433  *
434  *  \param die the DIE of the DW_TAG_array_type
435  *  \param unit the DIE of the compilation unit
436  *  \return number of elements in this array type
437  * */
438 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit)
439 {
440   xbt_assert(dwarf_tag(die) == DW_TAG_array_type,
441              "MC_dwarf_array_element_count called with DIE of type %s",
442              MC_dwarf_die_tagname(die));
443
444   int result = 1;
445   Dwarf_Die child;
446   int res;
447   for (res = dwarf_child(die, &child); res == 0;
448        res = dwarf_siblingof(&child, &child)) {
449     int child_tag = dwarf_tag(&child);
450     if (child_tag == DW_TAG_subrange_type
451         || child_tag == DW_TAG_enumeration_type) {
452       result *= MC_dwarf_subrange_element_count(&child, unit);
453     }
454   }
455   return result;
456 }
457
458 // ***** mc_type_t
459
460 /** \brief Initialize the location of a member of a type
461  * (DW_AT_data_member_location of a DW_TAG_member).
462  *
463  *  \param  type   a type (struct, class)
464  *  \param  member the member of the type
465  *  \param  child  DIE of the member (DW_TAG_member)
466  */
467 static void MC_dwarf_fill_member_location(mc_type_t type, mc_type_t member,
468                                           Dwarf_Die * child)
469 {
470   if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
471     xbt_die("Can't groke DW_AT_data_bit_offset.");
472   }
473
474   if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
475     if (type->type != DW_TAG_union_type) {
476       xbt_die
477           ("Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%"
478            PRIx64 ">%s", member->name.c_str(),
479            (uint64_t) type->id, type->name.c_str());
480     } else {
481       return;
482     }
483   }
484
485   Dwarf_Attribute attr;
486   dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
487   int form = dwarf_whatform(&attr);
488   int klass = MC_dwarf_form_get_class(form);
489   switch (klass) {
490   case MC_DW_CLASS_EXPRLOC:
491   case MC_DW_CLASS_BLOCK:
492     // Location expression:
493     {
494       Dwarf_Op *expr;
495       size_t len;
496       if (dwarf_getlocation(&attr, &expr, &len)) {
497         xbt_die
498             ("Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%"
499              PRIx64 ">%s", MC_dwarf_attr_integrate_string(child, DW_AT_name),
500              (uint64_t) type->id, type->name.c_str());
501       }
502       simgrid::mc::DwarfExpression(expr, expr+len);
503       break;
504     }
505   case MC_DW_CLASS_CONSTANT:
506     // Offset from the base address of the object:
507     {
508       Dwarf_Word offset;
509       if (!dwarf_formudata(&attr, &offset))
510         member->offset(offset);
511       else
512         xbt_die("Cannot get %s location <%" PRIx64 ">%s",
513                 MC_dwarf_attr_integrate_string(child, DW_AT_name),
514                 (uint64_t) type->id, type->name.c_str());
515       break;
516     }
517   case MC_DW_CLASS_LOCLISTPTR:
518     // Reference to a location list:
519     // TODO
520   case MC_DW_CLASS_REFERENCE:
521     // It's supposed to be possible in DWARF2 but I couldn't find its semantic
522     // in the spec.
523   default:
524     xbt_die("Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
525             klass, form);
526   }
527
528 }
529
530 static void dw_type_free_voidp(void *t)
531 {
532   delete *(mc_type_t*)t;
533 }
534
535 /** \brief Populate the list of members of a type
536  *
537  *  \param info ELF object containing the type DIE
538  *  \param die  DIE of the type
539  *  \param unit DIE of the compilation unit containing the type DIE
540  *  \param type the type
541  */
542 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die * die,
543                                  Dwarf_Die * unit, mc_type_t type)
544 {
545   int res;
546   Dwarf_Die child;
547   xbt_assert(type->members.empty());
548   for (res = dwarf_child(die, &child); res == 0;
549        res = dwarf_siblingof(&child, &child)) {
550     int tag = dwarf_tag(&child);
551     if (tag == DW_TAG_member || tag == DW_TAG_inheritance) {
552
553       // Skip declarations:
554       if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
555         continue;
556
557       // Skip compile time constants:
558       if (dwarf_hasattr(&child, DW_AT_const_value))
559         continue;
560
561       // TODO, we should use another type (because is is not a type but a member)
562       simgrid::mc::Type member;
563       member.type = tag;
564
565       // Global Offset:
566       member.id = dwarf_dieoffset(&child);
567
568       const char *name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
569       if (name)
570         member.name = name;
571       member.byte_size =
572           MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
573       member.element_count = -1;
574       member.type_id = MC_dwarf_at_type(&child);
575
576       if (dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
577         xbt_die("Can't groke DW_AT_data_bit_offset.");
578       }
579
580       MC_dwarf_fill_member_location(type, &member, &child);
581
582       if (!member.type_id) {
583         xbt_die("Missing type for member %s of <%" PRIx64 ">%s",
584                 member.name.c_str(),
585                 (uint64_t) type->id, type->name.c_str());
586       }
587
588       type->members.push_back(std::move(member));
589     }
590   }
591 }
592
593 /** \brief Create a MC type object from a DIE
594  *
595  *  \param info current object info object
596  *  \param DIE (for a given type);
597  *  \param unit compilation unit of the current DIE
598  *  \return MC representation of the type
599  */
600 static simgrid::mc::Type MC_dwarf_die_to_type(
601   mc_object_info_t info, Dwarf_Die * die,
602   Dwarf_Die * unit, mc_frame_t frame,
603   const char *ns)
604 {
605
606   simgrid::mc::Type type;
607   type.type = -1;
608   type.name = std::string();
609   type.element_count = -1;
610
611   type.type = dwarf_tag(die);
612
613   // Global Offset
614   type.id = dwarf_dieoffset(die);
615
616   const char *prefix = "";
617   switch (type.type) {
618   case DW_TAG_structure_type:
619     prefix = "struct ";
620     break;
621   case DW_TAG_union_type:
622     prefix = "union ";
623     break;
624   case DW_TAG_class_type:
625     prefix = "class ";
626     break;
627   default:
628     prefix = "";
629   }
630
631   const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
632   if (name != NULL) {
633     char* full_name = ns ? bprintf("%s%s::%s", prefix, ns, name) :
634       bprintf("%s%s", prefix, name);
635     type.name = std::string(full_name);
636     free(full_name);
637   }
638
639   type.type_id = MC_dwarf_at_type(die);
640
641   // Some compilers do not emit DW_AT_byte_size for pointer_type,
642   // so we fill this. We currently assume that the model-checked process is in
643   // the same architecture..
644   if (type.type == DW_TAG_pointer_type)
645     type.byte_size = sizeof(void*);
646
647   // Computation of the byte_size;
648   if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
649     type.byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
650   else if (type.type == DW_TAG_array_type
651            || type.type == DW_TAG_structure_type
652            || type.type == DW_TAG_class_type) {
653     Dwarf_Word size;
654     if (dwarf_aggregate_size(die, &size) == 0) {
655       type.byte_size = size;
656     }
657   }
658
659   switch (type.type) {
660   case DW_TAG_array_type:
661     type.element_count = MC_dwarf_array_element_count(die, unit);
662     // TODO, handle DW_byte_stride and (not) DW_bit_stride
663     break;
664
665   case DW_TAG_pointer_type:
666   case DW_TAG_reference_type:
667   case DW_TAG_rvalue_reference_type:
668     type.is_pointer_type = 1;
669     break;
670
671   case DW_TAG_structure_type:
672   case DW_TAG_union_type:
673   case DW_TAG_class_type:
674     MC_dwarf_add_members(info, die, unit, &type);
675     char *new_ns = ns == NULL ? xbt_strdup(type.name.c_str())
676         : bprintf("%s::%s", ns, name);
677     MC_dwarf_handle_children(info, die, unit, frame, new_ns);
678     free(new_ns);
679     break;
680   }
681
682   return std::move(type);
683 }
684
685 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
686                                      Dwarf_Die * unit, mc_frame_t frame,
687                                      const char *ns)
688 {
689   simgrid::mc::Type type = MC_dwarf_die_to_type(info, die, unit, frame, ns);
690   auto& t = (info->types[type.id] = std::move(type));
691   if (!t.name.empty() && type.byte_size != 0)
692     info->full_types_by_name[t.name] = &t;
693 }
694
695 static int mc_anonymous_variable_index = 0;
696
697 static std::unique_ptr<simgrid::mc::Variable> MC_die_to_variable(
698   mc_object_info_t info, Dwarf_Die * die,
699   Dwarf_Die * unit, mc_frame_t frame,
700   const char *ns)
701 {
702   // Skip declarations:
703   if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
704     return nullptr;
705
706   // Skip compile time constants:
707   if (dwarf_hasattr(die, DW_AT_const_value))
708     return nullptr;
709
710   Dwarf_Attribute attr_location;
711   if (dwarf_attr(die, DW_AT_location, &attr_location) == NULL) {
712     // No location: do not add it ?
713     return nullptr;
714   }
715
716   std::unique_ptr<simgrid::mc::Variable> variable =
717     std::unique_ptr<simgrid::mc::Variable>(new simgrid::mc::Variable());
718   variable->dwarf_offset = dwarf_dieoffset(die);
719   variable->global = frame == NULL;     // Can be override base on DW_AT_location
720   variable->object_info = info;
721
722   const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
723   if (name)
724     variable->name = name;
725   variable->type_id = MC_dwarf_at_type(die);
726
727   int form = dwarf_whatform(&attr_location);
728   int klass =
729       form ==
730       DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
731   switch (klass) {
732   case MC_DW_CLASS_EXPRLOC:
733   case MC_DW_CLASS_BLOCK:
734     // Location expression:
735     {
736       Dwarf_Op *expr;
737       size_t len;
738       if (dwarf_getlocation(&attr_location, &expr, &len)) {
739         xbt_die(
740           "Could not read location expression in DW_AT_location "
741           "of variable <%" PRIx64 ">%s",
742           (uint64_t) variable->dwarf_offset,
743           variable->name.c_str());
744       }
745
746       if (len == 1 && expr[0].atom == DW_OP_addr) {
747         variable->global = 1;
748         uintptr_t offset = (uintptr_t) expr[0].number;
749         uintptr_t base = (uintptr_t) info->base_address();
750         variable->address = (void *) (base + offset);
751       } else {
752         simgrid::mc::LocationListEntry entry;
753         entry.expression = {expr, expr + len};
754         variable->location_list = { std::move(entry) };
755       }
756
757       break;
758     }
759   case MC_DW_CLASS_LOCLISTPTR:
760   case MC_DW_CLASS_CONSTANT:
761     // Reference to location list:
762     mc_dwarf_location_list_init(
763       &variable->location_list, info, die,
764       &attr_location);
765     break;
766   default:
767     xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location "
768             "in <%" PRIx64 ">%s",
769             form, form, klass, klass,
770             (uint64_t) variable->dwarf_offset,
771             variable->name.c_str());
772   }
773
774   // Handle start_scope:
775   if (dwarf_hasattr(die, DW_AT_start_scope)) {
776     Dwarf_Attribute attr;
777     dwarf_attr(die, DW_AT_start_scope, &attr);
778     int form = dwarf_whatform(&attr);
779     int klass = MC_dwarf_form_get_class(form);
780     switch (klass) {
781     case MC_DW_CLASS_CONSTANT:
782       {
783         Dwarf_Word value;
784         variable->start_scope =
785             dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
786         break;
787       }
788     case MC_DW_CLASS_RANGELISTPTR:     // TODO
789     default:
790       xbt_die
791           ("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
792            form, klass, name == NULL ? "?" : name);
793     }
794   }
795
796   if (ns && variable->global)
797     variable->name =
798       std::string(ns) + "::" + variable->name;
799
800   // The current code needs a variable name,
801   // generate a fake one:
802   if (variable->name.empty())
803     variable->name =
804       "@anonymous#" + std::to_string(mc_anonymous_variable_index++);
805
806   return std::move(variable);
807 }
808
809 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
810                                          Dwarf_Die * unit, mc_frame_t frame,
811                                          const char *ns)
812 {
813   MC_dwarf_register_variable(info, frame,
814     MC_die_to_variable(info, die, unit, frame, ns));
815 }
816
817 static void MC_dwarf_handle_scope_die(mc_object_info_t info, Dwarf_Die * die,
818                                       Dwarf_Die * unit, mc_frame_t parent_frame,
819                                       const char *ns)
820 {
821   // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
822   int tag = dwarf_tag(die);
823   mc_tag_class klass = MC_dwarf_tag_classify(tag);
824
825   // (Template) Subprogram declaration:
826   if (klass == mc_tag_subprogram
827       && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
828     return;
829
830   if (klass == mc_tag_scope)
831     xbt_assert(parent_frame, "No parent scope for this scope");
832
833   simgrid::mc::Frame frame;
834
835   frame.tag = tag;
836   frame.id = dwarf_dieoffset(die);
837   frame.object_info = info;
838
839   if (klass == mc_tag_subprogram) {
840     const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
841     if(ns)
842       frame.name  = std::string(ns) + "::" + name;
843     else
844       frame.name = name;
845   }
846
847   frame.abstract_origin_id =
848     MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
849
850   // This is the base address for DWARF addresses.
851   // Relocated addresses are offset from this base address.
852   // See DWARF4 spec 7.5
853   void *base = info->base_address();
854
855   // TODO, support DW_AT_ranges
856   uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
857   frame.low_pc = low_pc ? ((char *) base) + low_pc : 0;
858   if (low_pc) {
859     // DW_AT_high_pc:
860     Dwarf_Attribute attr;
861     if (!dwarf_attr_integrate(die, DW_AT_high_pc, &attr)) {
862       xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
863     }
864
865     Dwarf_Sword offset;
866     Dwarf_Addr high_pc;
867
868     switch (MC_dwarf_form_get_class(dwarf_whatform(&attr))) {
869
870       // DW_AT_high_pc if an offset from the low_pc:
871     case MC_DW_CLASS_CONSTANT:
872
873       if (dwarf_formsdata(&attr, &offset) != 0)
874         xbt_die("Could not read constant");
875       frame.high_pc = (void *) ((char *) frame.low_pc + offset);
876       break;
877
878       // DW_AT_high_pc is a relocatable address:
879     case MC_DW_CLASS_ADDRESS:
880       if (dwarf_formaddr(&attr, &high_pc) != 0)
881         xbt_die("Could not read address");
882       frame.high_pc = ((char *) base) + high_pc;
883       break;
884
885     default:
886       xbt_die("Unexpected class for DW_AT_high_pc");
887
888     }
889   }
890
891   if (klass == mc_tag_subprogram) {
892     Dwarf_Attribute attr_frame_base;
893     if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
894       mc_dwarf_location_list_init(&frame.frame_base, info, die,
895                                   &attr_frame_base);
896   }
897
898   // Handle children:
899   MC_dwarf_handle_children(info, die, unit, &frame, ns);
900
901   // Register it:
902   if (klass == mc_tag_subprogram) {
903     char *key = bprintf("%" PRIx64, (uint64_t) frame.id);
904
905     xbt_dict_set(info->subprograms, key,
906       new simgrid::mc::Frame(std::move(frame)), NULL);
907     xbt_free(key);
908   } else if (klass == mc_tag_scope)
909     parent_frame->scopes.push_back(std::move(frame));
910 }
911
912 static void mc_dwarf_handle_namespace_die(mc_object_info_t info,
913                                           Dwarf_Die * die, Dwarf_Die * unit,
914                                           mc_frame_t frame,
915                                           const char *ns)
916 {
917   const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
918   if (frame)
919     xbt_die("Unexpected namespace in a subprogram");
920   char *new_ns = ns == NULL ? xbt_strdup(name)
921       : bprintf("%s::%s", ns, name);
922   MC_dwarf_handle_children(info, die, unit, frame, new_ns);
923   xbt_free(new_ns);
924 }
925
926 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
927                                      Dwarf_Die * unit, mc_frame_t frame,
928                                      const char *ns)
929 {
930   // For each child DIE:
931   Dwarf_Die child;
932   int res;
933   for (res = dwarf_child(die, &child); res == 0;
934        res = dwarf_siblingof(&child, &child)) {
935     MC_dwarf_handle_die(info, &child, unit, frame, ns);
936   }
937 }
938
939 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
940                                 Dwarf_Die * unit, mc_frame_t frame,
941                                 const char *ns)
942 {
943   int tag = dwarf_tag(die);
944   mc_tag_class klass = MC_dwarf_tag_classify(tag);
945   switch (klass) {
946
947     // Type:
948   case mc_tag_type:
949     MC_dwarf_handle_type_die(info, die, unit, frame, ns);
950     break;
951
952     // Subprogram or scope:
953   case mc_tag_subprogram:
954   case mc_tag_scope:
955     MC_dwarf_handle_scope_die(info, die, unit, frame, ns);
956     return;
957
958     // Variable:
959   case mc_tag_variable:
960     MC_dwarf_handle_variable_die(info, die, unit, frame, ns);
961     break;
962
963   case mc_tag_namespace:
964     mc_dwarf_handle_namespace_die(info, die, unit, frame, ns);
965     break;
966
967   default:
968     break;
969
970   }
971 }
972
973 /** \brief Populate the debugging informations of the given ELF object
974  *
975  *  Read the DWARf information of the EFFL object and populate the
976  *  lists of types, variables, functions.
977  */
978 void MC_dwarf_get_variables(mc_object_info_t info)
979 {
980   int fd = open(info->file_name, O_RDONLY);
981   if (fd < 0) {
982     xbt_die("Could not open file %s", info->file_name);
983   }
984   Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
985   if (dwarf == NULL) {
986     xbt_die("Your program must be compiled with -g (%s)", info->file_name);
987   }
988   // For each compilation unit:
989   Dwarf_Off offset = 0;
990   Dwarf_Off next_offset = 0;
991   size_t length;
992   while (dwarf_nextcu(dwarf, offset, &next_offset, &length, NULL, NULL, NULL) ==
993          0) {
994     Dwarf_Die unit_die;
995     if (dwarf_offdie(dwarf, offset + length, &unit_die) != NULL) {
996
997       // For each child DIE:
998       Dwarf_Die child;
999       int res;
1000       for (res = dwarf_child(&unit_die, &child); res == 0;
1001            res = dwarf_siblingof(&child, &child)) {
1002         MC_dwarf_handle_die(info, &child, &unit_die, NULL, NULL);
1003       }
1004
1005     }
1006     offset = next_offset;
1007   }
1008
1009   dwarf_end(dwarf);
1010   close(fd);
1011 }
1012
1013 // ***** Functions index
1014
1015 static int MC_compare_frame_index_items(mc_function_index_item_t a,
1016                                         mc_function_index_item_t b)
1017 {
1018   if (a->low_pc < b->low_pc)
1019     return -1;
1020   else if (a->low_pc == b->low_pc)
1021     return 0;
1022   else
1023     return 1;
1024 }
1025
1026 static void MC_make_functions_index(mc_object_info_t info)
1027 {
1028   xbt_dynar_t index = xbt_dynar_new(sizeof(s_mc_function_index_item_t), NULL);
1029
1030   // Populate the array:
1031   mc_frame_t frame = NULL;
1032   xbt_dict_cursor_t cursor;
1033   char *key;
1034   xbt_dict_foreach(info->subprograms, cursor, key, frame) {
1035     if (frame->low_pc == NULL)
1036       continue;
1037     s_mc_function_index_item_t entry;
1038     entry.low_pc = frame->low_pc;
1039     entry.high_pc = frame->high_pc;
1040     entry.function = frame;
1041     xbt_dynar_push(index, &entry);
1042   }
1043
1044   mc_function_index_item_t base =
1045       (mc_function_index_item_t) xbt_dynar_get_ptr(index, 0);
1046
1047   // Sort the array by low_pc:
1048   qsort(base,
1049         xbt_dynar_length(index),
1050         sizeof(s_mc_function_index_item_t),
1051         (int (*)(const void *, const void *)) MC_compare_frame_index_items);
1052
1053   info->functions_index = index;
1054 }
1055
1056 static void MC_post_process_variables(mc_object_info_t info)
1057 {
1058   for(simgrid::mc::Variable& variable : info->global_variables)
1059     if (variable.type_id) {
1060       auto i = info->types.find(variable.type_id);
1061       if (i != info->types.end())
1062         variable.type = &(i->second);
1063       else
1064         variable.type = nullptr;
1065     }
1066 }
1067
1068 static void mc_post_process_scope(mc_object_info_t info, mc_frame_t scope)
1069 {
1070
1071   if (scope->tag == DW_TAG_inlined_subroutine) {
1072
1073     // Attach correct namespaced name in inlined subroutine:
1074     char *key = bprintf("%" PRIx64, (uint64_t) scope->abstract_origin_id);
1075     mc_frame_t abstract_origin = (mc_frame_t) xbt_dict_get_or_null(info->subprograms, key);
1076     xbt_assert(abstract_origin, "Could not lookup abstract origin %s", key);
1077     xbt_free(key);
1078     scope->name = abstract_origin->name;
1079   }
1080
1081   // Direct:
1082   for (simgrid::mc::Variable& variable : scope->variables)
1083     if (variable.type_id) {
1084       auto i = info->types.find(variable.type_id);
1085       if (i != info->types.end())
1086         variable.type = &(i->second);
1087       else
1088         variable.type = nullptr;
1089     }
1090
1091   // Recursive post-processing of nested-scopes:
1092   for (simgrid::mc::Frame& nested_scope : scope->scopes)
1093       mc_post_process_scope(info, &nested_scope);
1094
1095 }
1096
1097 static void MC_post_process_functions(mc_object_info_t info)
1098 {
1099   xbt_dict_cursor_t cursor;
1100   char *key;
1101   mc_frame_t subprogram = NULL;
1102   xbt_dict_foreach(info->subprograms, cursor, key, subprogram) {
1103     mc_post_process_scope(info, subprogram);
1104   }
1105 }
1106
1107
1108 /** \brief Fill/lookup the "subtype" field.
1109  */
1110 static void MC_resolve_subtype(mc_object_info_t info, mc_type_t type)
1111 {
1112   if (!type->type_id)
1113     return;
1114   auto i = info->types.find(type->type_id);
1115   if (i != info->types.end())
1116     type->subtype = &(i->second);
1117   else {
1118     type->subtype = nullptr;
1119     return;
1120   }
1121   if (type->subtype->byte_size != 0)
1122     return;
1123   if (type->subtype->name.empty())
1124     return;
1125   // Try to find a more complete description of the type:
1126   // We need to fix in order to support C++.
1127
1128   auto j = info->full_types_by_name.find(type->subtype->name);
1129   if (j != info->full_types_by_name.end())
1130     type->subtype = j->second;
1131 }
1132
1133 static void MC_post_process_types(mc_object_info_t info)
1134 {
1135   // Lookup "subtype" field:
1136   for(auto& i : info->types) {
1137     MC_resolve_subtype(info, &(i.second));
1138     for (simgrid::mc::Type& member : i.second.members)
1139       MC_resolve_subtype(info, &member);
1140   }
1141 }
1142
1143 /** \brief Finds informations about a given shared object/executable */
1144 std::shared_ptr<s_mc_object_info_t> MC_find_object_info(
1145   std::vector<simgrid::mc::VmMap> const& maps, const char *name, int executable)
1146 {
1147   std::shared_ptr<s_mc_object_info_t> result =
1148     std::make_shared<s_mc_object_info_t>();
1149   if (executable)
1150     result->flags |= MC_OBJECT_INFO_EXECUTABLE;
1151   result->file_name = xbt_strdup(name);
1152   MC_find_object_address(maps, result.get());
1153   MC_dwarf_get_variables(result.get());
1154   MC_post_process_types(result.get());
1155   MC_post_process_variables(result.get());
1156   MC_post_process_functions(result.get());
1157   MC_make_functions_index(result.get());
1158   return std::move(result);
1159 }
1160
1161 /*************************************************************************/
1162
1163 static int MC_dwarf_get_variable_index(
1164   std::vector<simgrid::mc::Variable> variables, const char *var, void *address)
1165 {
1166
1167   if (variables.empty())
1168     return 0;
1169
1170   unsigned int cursor = 0;
1171   int start = 0;
1172   int end = variables.size() - 1;
1173   mc_variable_t var_test = nullptr;
1174
1175   while (start <= end) {
1176     cursor = (start + end) / 2;
1177     var_test = &variables[cursor];
1178     if (strcmp(var_test->name.c_str(), var) < 0) {
1179       start = cursor + 1;
1180     } else if (strcmp(var_test->name.c_str(), var) > 0) {
1181       end = cursor - 1;
1182     } else {
1183       if (address) {            /* global variable */
1184         if (var_test->address == address)
1185           return -1;
1186         if (var_test->address > address)
1187           end = cursor - 1;
1188         else
1189           start = cursor + 1;
1190       } else {                  /* local variable */
1191         return -1;
1192       }
1193     }
1194   }
1195
1196   if (strcmp(var_test->name.c_str(), var) == 0) {
1197     if (address && var_test->address < address)
1198       return cursor + 1;
1199     else
1200       return cursor;
1201   } else if (strcmp(var_test->name.c_str(), var) < 0)
1202     return cursor + 1;
1203   else
1204     return cursor;
1205
1206 }
1207
1208 void MC_dwarf_register_global_variable(
1209   mc_object_info_t info,
1210   std::unique_ptr<simgrid::mc::Variable> variable)
1211 {
1212   int index =
1213       MC_dwarf_get_variable_index(info->global_variables,
1214         variable->name.c_str(),
1215         variable->address);
1216   if (index != -1)
1217     info->global_variables.insert(
1218       info->global_variables.begin() + index, std::move(*variable));
1219   // TODO, else ?
1220 }
1221
1222 void MC_dwarf_register_non_global_variable(
1223   mc_object_info_t info,
1224   mc_frame_t frame,
1225   std::unique_ptr<simgrid::mc::Variable> variable)
1226 {
1227   xbt_assert(frame, "Frame is NULL");
1228   int index =
1229       MC_dwarf_get_variable_index(
1230         frame->variables, variable->name.c_str(), NULL);
1231   if (index != -1)
1232     frame->variables.insert(
1233       frame->variables.begin() + index, std::move(*variable));
1234   // TODO, else ?
1235 }
1236
1237 void MC_dwarf_register_variable(
1238   mc_object_info_t info, mc_frame_t frame,
1239   std::unique_ptr<simgrid::mc::Variable> variable)
1240 {
1241   if (!variable)
1242     return;
1243   if (variable->global)
1244     MC_dwarf_register_global_variable(info, std::move(variable));
1245   else if (frame != nullptr)
1246     MC_dwarf_register_non_global_variable(info, frame, std::move(variable));
1247   else
1248     xbt_die("No frame for this local variable");
1249 }
1250
1251 void MC_post_process_object_info(mc_process_t process, mc_object_info_t info)
1252 {
1253   for (auto& i : info->types) {
1254
1255     mc_type_t type = &(i.second);
1256     mc_type_t subtype = type;
1257     while (subtype->type == DW_TAG_typedef || subtype->type == DW_TAG_volatile_type
1258       || subtype->type == DW_TAG_const_type) {
1259       if (subtype->subtype)
1260         subtype = subtype->subtype;
1261       else
1262         break;
1263     }
1264
1265     // Resolve full_type:
1266     if (!subtype->name.empty() && subtype->byte_size == 0) {
1267       for (auto const& object_info : process->object_infos) {
1268         auto i = object_info->full_types_by_name.find(subtype->name);
1269         if (i != object_info->full_types_by_name.end()
1270             && !i->second->name.empty() && i->second->byte_size) {
1271           type->full_type = i->second;
1272           break;
1273         }
1274       }
1275     } else type->full_type = subtype;
1276
1277   }
1278 }