Logo AND Algorithmique Numérique Distribuée

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