Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://github.com/Onesphore/simgrid into Onesphore-master
[simgrid.git] / src / mc / mc_dwarf.cpp
1 /* Copyright (c) 2008-2018. 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 #include <cstdint>
9
10 #include <memory>
11 #include <utility>
12
13 #include <boost/range/algorithm.hpp>
14
15 #include <fcntl.h>
16 #include <cstdlib>
17 #include <elfutils/libdw.h>
18
19 #include <boost/algorithm/string/predicate.hpp>
20
21 #include "src/simgrid/util.hpp"
22 #include "xbt/log.h"
23 #include "xbt/string.hpp"
24 #include "xbt/sysdep.h"
25 #include <simgrid/config.h>
26
27 #include "src/mc/mc_dwarf.hpp"
28 #include "src/mc/mc_private.hpp"
29
30 #include "src/mc/ObjectInformation.hpp"
31 #include "src/mc/Variable.hpp"
32 #include "src/mc/remote/RemoteClient.hpp"
33
34 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing");
35
36 /** \brief The default DW_TAG_lower_bound for a given DW_AT_language.
37  *
38  *  The default for a given language is defined in the DWARF spec.
39  *
40  *  \param language constant as defined by the DWARf spec
41  */
42 static uint64_t MC_dwarf_default_lower_bound(int lang);
43
44 /** \brief Computes the the element_count of a DW_TAG_enumeration_type DIE
45  *
46  * This is the number of elements in a given array dimension.
47  *
48  * A reference of the compilation unit (DW_TAG_compile_unit) is
49  * needed because the default lower bound (when there is no DW_AT_lower_bound)
50  * depends of the language of the compilation unit (DW_AT_language).
51  *
52  * \param die  DIE for the DW_TAG_enumeration_type or DW_TAG_subrange_type
53  * \param unit DIE of the DW_TAG_compile_unit
54  */
55 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit);
56
57 /** \brief Computes the number of elements of a given DW_TAG_array_type.
58  *
59  * \param die DIE for the DW_TAG_array_type
60  */
61 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit);
62
63 /** \brief Process a DIE
64  *
65  *  \param info the resulting object fot the library/binary file (output)
66  *  \param die  the current DIE
67  *  \param unit the DIE of the compile unit of the current DIE
68  *  \param frame containing frame if any
69  */
70 static void MC_dwarf_handle_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
71                                 Dwarf_Die * unit, simgrid::mc::Frame* frame,
72                                 const char *ns);
73
74 /** \brief Process a type DIE
75  */
76 static void MC_dwarf_handle_type_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
77                                      Dwarf_Die * unit, simgrid::mc::Frame* frame,
78                                      const char *ns);
79
80 /** \brief Calls MC_dwarf_handle_die on all children of the given die
81  *
82  *  \param info the resulting object fot the library/binary file (output)
83  *  \param die  the current DIE
84  *  \param unit the DIE of the compile unit of the current DIE
85  *  \param frame containing frame if any
86  */
87 static void MC_dwarf_handle_children(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
88                                      Dwarf_Die * unit, simgrid::mc::Frame* frame,
89                                      const char *ns);
90
91 /** \brief Handle a variable (DW_TAG_variable or other)
92  *
93  *  \param info the resulting object fot the library/binary file (output)
94  *  \param die  the current DIE
95  *  \param unit the DIE of the compile unit of the current DIE
96  *  \param frame containing frame if any
97  */
98 static void MC_dwarf_handle_variable_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
99                                          Dwarf_Die * unit, simgrid::mc::Frame* frame,
100                                          const char *ns);
101
102 /** \brief Get the DW_TAG_type of the DIE
103  *
104  *  \param die DIE
105  *  \return DW_TAG_type attribute as a new string (nullptr if none)
106  */
107 static std::uint64_t MC_dwarf_at_type(Dwarf_Die * die);
108
109 namespace simgrid {
110 namespace dwarf {
111
112 enum class TagClass {
113   Unknown,
114   Type,
115   Subprogram,
116   Variable,
117   Scope,
118   Namespace
119 };
120
121 /*** Class of forms defined in the DWARF standard */
122 enum class FormClass {
123   Unknown,
124   Address,   // Location in the program's address space
125   Block,     // Arbitrary block of bytes
126   Constant,
127   String,
128   Flag,      // Boolean value
129   Reference, // Reference to another DIE
130   ExprLoc,   // DWARF expression/location description
131   LinePtr,
132   LocListPtr,
133   MacPtr,
134   RangeListPtr
135 };
136
137 static
138 TagClass classify_tag(int tag)
139 {
140   switch (tag) {
141
142   case DW_TAG_array_type:
143   case DW_TAG_class_type:
144   case DW_TAG_enumeration_type:
145   case DW_TAG_typedef:
146   case DW_TAG_pointer_type:
147   case DW_TAG_reference_type:
148   case DW_TAG_rvalue_reference_type:
149   case DW_TAG_string_type:
150   case DW_TAG_structure_type:
151   case DW_TAG_subroutine_type:
152   case DW_TAG_union_type:
153   case DW_TAG_ptr_to_member_type:
154   case DW_TAG_set_type:
155   case DW_TAG_subrange_type:
156   case DW_TAG_base_type:
157   case DW_TAG_const_type:
158   case DW_TAG_file_type:
159   case DW_TAG_packed_type:
160   case DW_TAG_volatile_type:
161   case DW_TAG_restrict_type:
162   case DW_TAG_interface_type:
163   case DW_TAG_unspecified_type:
164   case DW_TAG_shared_type:
165     return TagClass::Type;
166
167   case DW_TAG_subprogram:
168     return TagClass::Subprogram;
169
170   case DW_TAG_variable:
171   case DW_TAG_formal_parameter:
172     return TagClass::Variable;
173
174   case DW_TAG_lexical_block:
175   case DW_TAG_try_block:
176   case DW_TAG_catch_block:
177   case DW_TAG_inlined_subroutine:
178   case DW_TAG_with_stmt:
179     return TagClass::Scope;
180
181   case DW_TAG_namespace:
182     return TagClass::Namespace;
183
184   default:
185     return TagClass::Unknown;
186   }
187 }
188
189 /** \brief Find the DWARF data class for a given DWARF data form
190  *
191  *  This mapping is defined in the DWARF spec.
192  *
193  *  \param form The form (values taken from the DWARF spec)
194  *  \return An internal representation for the corresponding class
195  * */
196 static
197 FormClass classify_form(int form)
198 {
199   switch (form) {
200   case DW_FORM_addr:
201     return FormClass::Address;
202   case DW_FORM_block2:
203   case DW_FORM_block4:
204   case DW_FORM_block:
205   case DW_FORM_block1:
206     return FormClass::Block;
207   case DW_FORM_data1:
208   case DW_FORM_data2:
209   case DW_FORM_data4:
210   case DW_FORM_data8:
211   case DW_FORM_udata:
212   case DW_FORM_sdata:
213     return FormClass::Constant;
214   case DW_FORM_string:
215   case DW_FORM_strp:
216     return FormClass::String;
217   case DW_FORM_ref_addr:
218   case DW_FORM_ref1:
219   case DW_FORM_ref2:
220   case DW_FORM_ref4:
221   case DW_FORM_ref8:
222   case DW_FORM_ref_udata:
223     return FormClass::Reference;
224   case DW_FORM_flag:
225   case DW_FORM_flag_present:
226     return FormClass::Flag;
227   case DW_FORM_exprloc:
228     return FormClass::ExprLoc;
229     // TODO sec offset
230     // TODO indirect
231   default:
232     return FormClass::Unknown;
233   }
234 }
235
236 /** \brief Get the name of the tag of a given DIE
237  *
238  *  \param die DIE
239  *  \return name of the tag of this DIE
240  */
241 inline XBT_PRIVATE
242 const char *tagname(Dwarf_Die * die)
243 {
244   return simgrid::dwarf::tagname(dwarf_tag(die));
245 }
246
247 }
248 }
249
250 // ***** Attributes
251
252 /** \brief Get an attribute of a given DIE as a string
253  *
254  *  \param die       the DIE
255  *  \param attribute attribute
256  *  \return value of the given attribute of the given DIE
257  */
258 static const char *MC_dwarf_attr_integrate_string(Dwarf_Die * die,
259                                                   int attribute)
260 {
261   Dwarf_Attribute attr;
262   if (not dwarf_attr_integrate(die, attribute, &attr))
263     return nullptr;
264   else
265     return dwarf_formstring(&attr);
266 }
267
268 static Dwarf_Off MC_dwarf_attr_dieoffset(Dwarf_Die * die, int attribute)
269 {
270   Dwarf_Attribute attr;
271   if (dwarf_hasattr_integrate(die, attribute) == 0)
272     return 0;
273   dwarf_attr_integrate(die, attribute, &attr);
274   Dwarf_Die subtype_die;
275   if (dwarf_formref_die(&attr, &subtype_die) == nullptr)
276     xbt_die("Could not find DIE");
277   return dwarf_dieoffset(&subtype_die);
278 }
279
280 static Dwarf_Off MC_dwarf_attr_integrate_dieoffset(Dwarf_Die * die,
281                                                    int attribute)
282 {
283   Dwarf_Attribute attr;
284   if (dwarf_hasattr_integrate(die, attribute) == 0)
285     return 0;
286   dwarf_attr_integrate(die, DW_AT_type, &attr);
287   Dwarf_Die subtype_die;
288   if (dwarf_formref_die(&attr, &subtype_die) == nullptr)
289     xbt_die("Could not find DIE");
290   return dwarf_dieoffset(&subtype_die);
291 }
292
293 /** \brief Find the type/subtype (DW_AT_type) for a DIE
294  *
295  *  \param die the DIE
296  *  \return DW_AT_type reference as a global offset in hexadecimal (or nullptr)
297  */
298 static
299 std::uint64_t MC_dwarf_at_type(Dwarf_Die * die)
300 {
301   return MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
302 }
303
304 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die * die, int attribute)
305 {
306   Dwarf_Attribute attr;
307   if (dwarf_attr_integrate(die, attribute, &attr) == nullptr)
308     return 0;
309   Dwarf_Addr value;
310   if (dwarf_formaddr(&attr, &value) == 0)
311     return (uint64_t) value;
312   else
313     return 0;
314 }
315
316 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die * die, int attribute,
317                                              uint64_t default_value)
318 {
319   Dwarf_Attribute attr;
320   if (dwarf_attr_integrate(die, attribute, &attr) == nullptr)
321     return default_value;
322   Dwarf_Word value;
323   return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr),
324                          &value) == 0 ? (uint64_t) value : default_value;
325 }
326
327 static bool MC_dwarf_attr_flag(Dwarf_Die * die, int attribute, bool integrate)
328 {
329   Dwarf_Attribute attr;
330   if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
331        : dwarf_attr(die, attribute, &attr)) == 0)
332     return false;
333
334   bool result;
335   if (dwarf_formflag(&attr, &result))
336     xbt_die("Unexpected form for attribute %s",
337       simgrid::dwarf::attrname(attribute));
338   return result;
339 }
340
341 /** @brief Find the default lower bound for a given language
342  *
343  *  The default lower bound of an array (when DW_TAG_lower_bound
344  *  is missing) depends on the language of the compilation unit.
345  *
346  *  @param lang Language of the compilation unit (values defined in the DWARF spec)
347  *  @return     Default lower bound of an array in this compilation unit
348  * */
349 static uint64_t MC_dwarf_default_lower_bound(int lang)
350 {
351   switch (lang) {
352   case DW_LANG_C:
353   case DW_LANG_C89:
354   case DW_LANG_C99:
355   case DW_LANG_C_plus_plus:
356   case DW_LANG_D:
357   case DW_LANG_Java:
358   case DW_LANG_ObjC:
359   case DW_LANG_ObjC_plus_plus:
360   case DW_LANG_Python:
361   case DW_LANG_UPC:
362     return 0;
363   case DW_LANG_Ada83:
364   case DW_LANG_Ada95:
365   case DW_LANG_Fortran77:
366   case DW_LANG_Fortran90:
367   case DW_LANG_Fortran95:
368   case DW_LANG_Modula2:
369   case DW_LANG_Pascal83:
370   case DW_LANG_PL1:
371   case DW_LANG_Cobol74:
372   case DW_LANG_Cobol85:
373     return 1;
374   default:
375     xbt_die("No default DW_TAG_lower_bound for language %i and none given",
376             lang);
377     return 0;
378   }
379 }
380
381 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
382  *
383  *  \param die  the DIE
384  *  \param unit DIE of the compilation unit
385  *  \return     number of elements in the range
386  * */
387 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
388                                                 Dwarf_Die * unit)
389 {
390   xbt_assert(dwarf_tag(die) == DW_TAG_enumeration_type
391              || dwarf_tag(die) == DW_TAG_subrange_type,
392              "MC_dwarf_subrange_element_count called with DIE of type %s",
393              simgrid::dwarf::tagname(die));
394
395   // Use DW_TAG_count if present:
396   if (dwarf_hasattr_integrate(die, DW_AT_count))
397     return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
398   // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
399
400   if (not dwarf_hasattr_integrate(die, DW_AT_upper_bound))
401     // This is not really 0, but the code expects this (we do not know):
402     return 0;
403
404   uint64_t upper_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, static_cast<uint64_t>(-1));
405
406   uint64_t lower_bound = 0;
407   if (dwarf_hasattr_integrate(die, DW_AT_lower_bound))
408     lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, static_cast<uint64_t>(-1));
409   else
410     lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
411   return upper_bound - lower_bound + 1;
412 }
413
414 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
415  *
416  *  The compilation unit might be needed because the default lower
417  *  bound depends on the language of the compilation unit.
418  *
419  *  \param die the DIE of the DW_TAG_array_type
420  *  \param unit the DIE of the compilation unit
421  *  \return number of elements in this array type
422  * */
423 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit)
424 {
425   xbt_assert(dwarf_tag(die) == DW_TAG_array_type,
426              "MC_dwarf_array_element_count called with DIE of type %s",
427              simgrid::dwarf::tagname(die));
428
429   int result = 1;
430   Dwarf_Die child;
431   int res;
432   for (res = dwarf_child(die, &child); res == 0;
433        res = dwarf_siblingof(&child, &child)) {
434     int child_tag = dwarf_tag(&child);
435     if (child_tag == DW_TAG_subrange_type
436         || child_tag == DW_TAG_enumeration_type)
437       result *= MC_dwarf_subrange_element_count(&child, unit);
438   }
439   return result;
440 }
441
442 // ***** Variable
443
444 /** Sort the variable by name and address.
445  *
446  *  We could use boost::container::flat_set instead.
447  */
448 static bool MC_compare_variable(
449   simgrid::mc::Variable const& a, simgrid::mc::Variable const& b)
450 {
451   int cmp = strcmp(a.name.c_str(), b.name.c_str());
452   if (cmp < 0)
453     return true;
454   else if (cmp > 0)
455     return false;
456   else
457     return a.address < b.address;
458 }
459
460 // ***** simgrid::mc::Type*
461
462 /** \brief Initialize the location of a member of a type
463  * (DW_AT_data_member_location of a DW_TAG_member).
464  *
465  *  \param  type   a type (struct, class)
466  *  \param  member the member of the type
467  *  \param  child  DIE of the member (DW_TAG_member)
468  */
469 static void MC_dwarf_fill_member_location(
470   simgrid::mc::Type* type, simgrid::mc::Member* member, Dwarf_Die * child)
471 {
472   if (dwarf_hasattr(child, DW_AT_data_bit_offset))
473     xbt_die("Can't groke DW_AT_data_bit_offset.");
474
475   if (not dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
476     if (type->type == DW_TAG_union_type)
477       return;
478     xbt_die
479         ("Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%"
480          PRIx64 ">%s", member->name.c_str(),
481          (uint64_t) type->id, type->name.c_str());
482   }
483
484   Dwarf_Attribute attr;
485   dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
486   int form = dwarf_whatform(&attr);
487   simgrid::dwarf::FormClass form_class = simgrid::dwarf::classify_form(form);
488   switch (form_class) {
489   case simgrid::dwarf::FormClass::ExprLoc:
490   case simgrid::dwarf::FormClass::Block:
491     // Location expression:
492     {
493       Dwarf_Op *expr;
494       size_t len;
495       if (dwarf_getlocation(&attr, &expr, &len))
496         xbt_die
497             ("Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%"
498              PRIx64 ">%s", MC_dwarf_attr_integrate_string(child, DW_AT_name),
499              (uint64_t) type->id, type->name.c_str());
500       member->location_expression = simgrid::dwarf::DwarfExpression(expr, expr+len);
501       break;
502     }
503   case simgrid::dwarf::FormClass::Constant:
504     // Offset from the base address of the object:
505     {
506       Dwarf_Word offset;
507       if (not dwarf_formudata(&attr, &offset))
508         member->offset(offset);
509       else
510         xbt_die("Cannot get %s location <%" PRIx64 ">%s",
511                 MC_dwarf_attr_integrate_string(child, DW_AT_name),
512                 (uint64_t) type->id, type->name.c_str());
513       break;
514     }
515
516   default:
517     // includes FormClass::LocListPtr (reference to a location list: TODO) and FormClass::Reference (it's supposed to be
518     // possible in DWARF2 but I couldn't find its semantic in the spec)
519     xbt_die("Can't handle form class (%d) / form 0x%x as DW_AT_member_location", (int)form_class, (unsigned)form);
520   }
521
522 }
523
524 /** \brief Populate the list of members of a type
525  *
526  *  \param info ELF object containing the type DIE
527  *  \param die  DIE of the type
528  *  \param unit DIE of the compilation unit containing the type DIE
529  *  \param type the type
530  */
531 static void MC_dwarf_add_members(simgrid::mc::ObjectInformation* /*info*/, Dwarf_Die* die, Dwarf_Die* /*unit*/,
532                                  simgrid::mc::Type* type)
533 {
534   int res;
535   Dwarf_Die child;
536   xbt_assert(type->members.empty());
537   for (res = dwarf_child(die, &child); res == 0;
538        res = dwarf_siblingof(&child, &child)) {
539     int tag = dwarf_tag(&child);
540     if (tag == DW_TAG_member || tag == DW_TAG_inheritance) {
541
542       // Skip declarations:
543       if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
544         continue;
545
546       // Skip compile time constants:
547       if (dwarf_hasattr(&child, DW_AT_const_value))
548         continue;
549
550       // TODO, we should use another type (because is is not a type but a member)
551       simgrid::mc::Member member;
552       if (tag == DW_TAG_inheritance)
553         member.flags |= simgrid::mc::Member::INHERITANCE_FLAG;
554
555       const char *name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
556       if (name)
557         member.name = name;
558       // Those base names are used by GCC and clang for virtual table pointers
559       // respectively ("__vptr$ClassName", "__vptr.ClassName"):
560       if (boost::algorithm::starts_with(member.name, "__vptr$") ||
561         boost::algorithm::starts_with(member.name, "__vptr."))
562         member.flags |= simgrid::mc::Member::VIRTUAL_POINTER_FLAG;
563       // A cleaner solution would be to check against the type:
564       // ---
565       // tag: DW_TAG_member
566       // name: "_vptr$Foo"
567       // type:
568       //   # Type for a pointer to a vtable
569       //   tag: DW_TAG_pointer_type
570       //   type:
571       //     # Type for a vtable:
572       //     tag: DW_TAG_pointer_type
573       //     name: "__vtbl_ptr_type"
574       //     type:
575       //       tag: DW_TAG_subroutine_type
576       //       type:
577       //         tag: DW_TAG_base_type
578       //         name: "int"
579       // ---
580
581       member.byte_size =
582           MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
583       member.type_id = MC_dwarf_at_type(&child);
584
585       if (dwarf_hasattr(&child, DW_AT_data_bit_offset))
586         xbt_die("Can't groke DW_AT_data_bit_offset.");
587
588       MC_dwarf_fill_member_location(type, &member, &child);
589
590       if (not member.type_id)
591         xbt_die("Missing type for member %s of <%" PRIx64 ">%s",
592                 member.name.c_str(),
593                 (uint64_t) type->id, type->name.c_str());
594
595       type->members.push_back(std::move(member));
596     }
597   }
598 }
599
600 /** \brief Create a MC type object from a DIE
601  *
602  *  \param info current object info object
603  *  \param die DIE (for a given type)
604  *  \param unit compilation unit of the current DIE
605  *  \return MC representation of the type
606  */
607 static simgrid::mc::Type MC_dwarf_die_to_type(
608   simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
609   Dwarf_Die * unit, simgrid::mc::Frame* frame,
610   const char *ns)
611 {
612   simgrid::mc::Type type;
613   type.type = dwarf_tag(die);
614   type.name = std::string();
615   type.element_count = -1;
616
617   // Global Offset
618   type.id = dwarf_dieoffset(die);
619
620   const char *prefix = "";
621   switch (type.type) {
622   case DW_TAG_structure_type:
623     prefix = "struct ";
624     break;
625   case DW_TAG_union_type:
626     prefix = "union ";
627     break;
628   case DW_TAG_class_type:
629     prefix = "class ";
630     break;
631   default:
632     prefix = "";
633   }
634
635   const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
636   if (name != nullptr) {
637     if (ns)
638       type.name = simgrid::xbt::string_printf("%s%s::%s", prefix, ns, name);
639     else
640       type.name = simgrid::xbt::string_printf("%s%s", prefix, name);
641   }
642
643   type.type_id = MC_dwarf_at_type(die);
644
645   // Some compilers do not emit DW_AT_byte_size for pointer_type,
646   // so we fill this. We currently assume that the model-checked process is in
647   // the same architecture..
648   if (type.type == DW_TAG_pointer_type)
649     type.byte_size = sizeof(void*);
650
651   // Computation of the byte_size
652   if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
653     type.byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
654   else if (type.type == DW_TAG_array_type
655            || type.type == DW_TAG_structure_type
656            || type.type == DW_TAG_class_type) {
657     Dwarf_Word size;
658     if (dwarf_aggregate_size(die, &size) == 0)
659       type.byte_size = size;
660   }
661
662   switch (type.type) {
663   case DW_TAG_array_type:
664     type.element_count = MC_dwarf_array_element_count(die, unit);
665     // TODO, handle DW_byte_stride and (not) DW_bit_stride
666     break;
667
668   case DW_TAG_pointer_type:
669   case DW_TAG_reference_type:
670   case DW_TAG_rvalue_reference_type:
671     break;
672
673   case DW_TAG_structure_type:
674   case DW_TAG_union_type:
675   case DW_TAG_class_type:
676     MC_dwarf_add_members(info, die, unit, &type);
677     MC_dwarf_handle_children(info, die, unit, frame,
678                              ns ? simgrid::xbt::string_printf("%s::%s", ns, name).c_str() : type.name.c_str());
679     break;
680
681   default:
682     XBT_DEBUG("Unhandled type: %d (%s)", type.type, simgrid::dwarf::tagname(type.type));
683     break;
684   }
685
686   return type;
687 }
688
689 static void MC_dwarf_handle_type_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
690                                      Dwarf_Die * unit, simgrid::mc::Frame* frame,
691                                      const char *ns)
692 {
693   simgrid::mc::Type type = MC_dwarf_die_to_type(info, die, unit, frame, ns);
694   auto& t = (info->types[type.id] = std::move(type));
695   if (not t.name.empty() && type.byte_size != 0)
696     info->full_types_by_name[t.name] = &t;
697 }
698
699 static int mc_anonymous_variable_index = 0;
700
701 static std::unique_ptr<simgrid::mc::Variable> MC_die_to_variable(simgrid::mc::ObjectInformation* info, Dwarf_Die* die,
702                                                                  Dwarf_Die* /*unit*/, simgrid::mc::Frame* frame,
703                                                                  const char* ns)
704 {
705   // Skip declarations:
706   if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
707     return nullptr;
708
709   // Skip compile time constants:
710   if (dwarf_hasattr(die, DW_AT_const_value))
711     return nullptr;
712
713   Dwarf_Attribute attr_location;
714   if (dwarf_attr(die, DW_AT_location, &attr_location) == nullptr)
715     // No location: do not add it ?
716     return nullptr;
717
718   std::unique_ptr<simgrid::mc::Variable> variable =
719     std::unique_ptr<simgrid::mc::Variable>(new simgrid::mc::Variable());
720   variable->id = dwarf_dieoffset(die);
721   variable->global = frame == nullptr;     // Can be override base on DW_AT_location
722   variable->object_info = info;
723
724   const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
725   if (name)
726     variable->name = name;
727   variable->type_id = MC_dwarf_at_type(die);
728
729   int form = dwarf_whatform(&attr_location);
730   simgrid::dwarf::FormClass form_class;
731   if (form == DW_FORM_sec_offset)
732     form_class = simgrid::dwarf::FormClass::Constant;
733   else
734     form_class = simgrid::dwarf::classify_form(form);
735   switch (form_class) {
736   case simgrid::dwarf::FormClass::ExprLoc:
737   case simgrid::dwarf::FormClass::Block:
738     // Location expression:
739     {
740       Dwarf_Op *expr;
741       size_t len;
742       if (dwarf_getlocation(&attr_location, &expr, &len)) {
743         xbt_die(
744           "Could not read location expression in DW_AT_location "
745           "of variable <%" PRIx64 ">%s",
746           (uint64_t) variable->id,
747           variable->name.c_str());
748       }
749
750       if (len == 1 && expr[0].atom == DW_OP_addr) {
751         variable->global = true;
752         uintptr_t offset = (uintptr_t) expr[0].number;
753         uintptr_t base = (uintptr_t) info->base_address();
754         variable->address = (void *) (base + offset);
755       } else
756         variable->location_list = {
757             simgrid::dwarf::LocationListEntry(simgrid::dwarf::DwarfExpression(expr, expr + len))};
758
759       break;
760     }
761
762   case simgrid::dwarf::FormClass::LocListPtr:
763   case simgrid::dwarf::FormClass::Constant:
764     // Reference to location list:
765     variable->location_list = simgrid::dwarf::location_list(
766       *info, attr_location);
767     break;
768
769   default:
770     xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location in <%" PRIx64 ">%s", (unsigned)form, form,
771             (unsigned)form_class, (int)form_class, (uint64_t)variable->id, 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     simgrid::dwarf::FormClass form_class = simgrid::dwarf::classify_form(form);
780     if (form_class == simgrid::dwarf::FormClass::Constant) {
781       Dwarf_Word value;
782       variable->start_scope = dwarf_formudata(&attr, &value) == 0 ? (size_t)value : 0;
783     } else {
784       // TODO: FormClass::RangeListPtr
785       xbt_die("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s", (unsigned)form,
786               (unsigned)form_class, name == nullptr ? "?" : name);
787     }
788   }
789
790   if (ns && variable->global)
791     variable->name =
792       std::string(ns) + "::" + variable->name;
793
794   // The current code needs a variable name,
795   // generate a fake one:
796   if (variable->name.empty()) {
797     variable->name = "@anonymous#" + std::to_string(mc_anonymous_variable_index);
798     mc_anonymous_variable_index++;
799   }
800   return variable;
801 }
802
803 static void MC_dwarf_handle_variable_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
804                                          Dwarf_Die * unit, simgrid::mc::Frame* frame,
805                                          const char *ns)
806 {
807   std::unique_ptr<simgrid::mc::Variable> variable =
808     MC_die_to_variable(info, die, unit, frame, ns);
809   if (not variable)
810     return;
811   // Those arrays are sorted later:
812   if (variable->global)
813     info->global_variables.push_back(std::move(*variable));
814   else if (frame != nullptr)
815     frame->variables.push_back(std::move(*variable));
816   else
817     xbt_die("No frame for this local variable");
818 }
819
820 static void MC_dwarf_handle_scope_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
821                                       Dwarf_Die * unit, simgrid::mc::Frame* parent_frame,
822                                       const char *ns)
823 {
824   // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
825   int tag = dwarf_tag(die);
826   simgrid::dwarf::TagClass klass = simgrid::dwarf::classify_tag(tag);
827
828   // (Template) Subprogram declaration:
829   if (klass == simgrid::dwarf::TagClass::Subprogram
830       && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
831     return;
832
833   if (klass == simgrid::dwarf::TagClass::Scope)
834     xbt_assert(parent_frame, "No parent scope for this scope");
835
836   simgrid::mc::Frame frame;
837   frame.tag = tag;
838   frame.id = dwarf_dieoffset(die);
839   frame.object_info = info;
840
841   if (klass == simgrid::dwarf::TagClass::Subprogram) {
842     const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
843     if (name && ns)
844       frame.name  = std::string(ns) + "::" + name;
845     else if (name)
846       frame.name = name;
847   }
848
849   frame.abstract_origin_id =
850     MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
851
852   // This is the base address for DWARF addresses.
853   // Relocated addresses are offset from this base address.
854   // See DWARF4 spec 7.5
855   std::uint64_t base = (std::uint64_t) info->base_address();
856
857   // TODO, support DW_AT_ranges
858   uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
859   frame.range.begin() = low_pc ? (std::uint64_t) base + low_pc : 0;
860   if (low_pc) {
861     // DW_AT_high_pc:
862     Dwarf_Attribute attr;
863     if (not dwarf_attr_integrate(die, DW_AT_high_pc, &attr))
864       xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
865
866     Dwarf_Sword offset;
867     Dwarf_Addr high_pc;
868
869     switch (simgrid::dwarf::classify_form(dwarf_whatform(&attr))) {
870
871       // DW_AT_high_pc if an offset from the low_pc:
872     case simgrid::dwarf::FormClass::Constant:
873
874       if (dwarf_formsdata(&attr, &offset) != 0)
875         xbt_die("Could not read constant");
876       frame.range.end() = frame.range.begin() + offset;
877       break;
878
879       // DW_AT_high_pc is a relocatable address:
880     case simgrid::dwarf::FormClass::Address:
881       if (dwarf_formaddr(&attr, &high_pc) != 0)
882         xbt_die("Could not read address");
883       frame.range.end() = base + high_pc;
884       break;
885
886     default:
887       xbt_die("Unexpected class for DW_AT_high_pc");
888
889     }
890   }
891
892   if (klass == simgrid::dwarf::TagClass::Subprogram) {
893     Dwarf_Attribute attr_frame_base;
894     if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
895       frame.frame_base_location = simgrid::dwarf::location_list(*info,
896                                   attr_frame_base);
897   }
898
899   // Handle children:
900   MC_dwarf_handle_children(info, die, unit, &frame, ns);
901
902   // We sort them in order to have an (somewhat) efficient by name
903   // lookup:
904   boost::range::sort(frame.variables, MC_compare_variable);
905
906   // Register it:
907   if (klass == simgrid::dwarf::TagClass::Subprogram)
908     info->subprograms[frame.id] = std::move(frame);
909   else if (klass == simgrid::dwarf::TagClass::Scope)
910     parent_frame->scopes.push_back(std::move(frame));
911 }
912
913 static void mc_dwarf_handle_namespace_die(simgrid::mc::ObjectInformation* info,
914                                           Dwarf_Die * die, Dwarf_Die * unit,
915                                           simgrid::mc::Frame* frame,
916                                           const char *ns)
917 {
918   const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
919   if (frame)
920     xbt_die("Unexpected namespace in a subprogram");
921   char *new_ns = ns == nullptr ? xbt_strdup(name)
922       : bprintf("%s::%s", ns, name);
923   MC_dwarf_handle_children(info, die, unit, frame, new_ns);
924   xbt_free(new_ns);
925 }
926
927 static void MC_dwarf_handle_children(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
928                                      Dwarf_Die * unit, simgrid::mc::Frame* frame,
929                                      const char *ns)
930 {
931   // For each child DIE:
932   Dwarf_Die child;
933   int res;
934   for (res = dwarf_child(die, &child); res == 0;
935        res = dwarf_siblingof(&child, &child))
936     MC_dwarf_handle_die(info, &child, unit, frame, ns);
937 }
938
939 static void MC_dwarf_handle_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
940                                 Dwarf_Die * unit, simgrid::mc::Frame* frame,
941                                 const char *ns)
942 {
943   int tag = dwarf_tag(die);
944   simgrid::dwarf::TagClass klass = simgrid::dwarf::classify_tag(tag);
945   switch (klass) {
946
947     // Type:
948   case simgrid::dwarf::TagClass::Type:
949     MC_dwarf_handle_type_die(info, die, unit, frame, ns);
950     break;
951
952     // Subprogram or scope:
953   case simgrid::dwarf::TagClass::Subprogram:
954   case simgrid::dwarf::TagClass::Scope:
955     MC_dwarf_handle_scope_die(info, die, unit, frame, ns);
956     return;
957
958     // Variable:
959   case simgrid::dwarf::TagClass::Variable:
960     MC_dwarf_handle_variable_die(info, die, unit, frame, ns);
961     break;
962
963   case simgrid::dwarf::TagClass::Namespace:
964     mc_dwarf_handle_namespace_die(info, die, unit, frame, ns);
965     break;
966
967   default:
968     break;
969
970   }
971 }
972
973 static
974 Elf64_Half get_type(Elf* elf)
975 {
976   Elf64_Ehdr* ehdr64 = elf64_getehdr(elf);
977   if (ehdr64)
978     return ehdr64->e_type;
979   Elf32_Ehdr* ehdr32 = elf32_getehdr(elf);
980   if (ehdr32)
981     return ehdr32->e_type;
982   xbt_die("Could not get ELF heeader");
983 }
984
985 static
986 void read_dwarf_info(simgrid::mc::ObjectInformation* info, Dwarf* dwarf)
987 {
988   // For each compilation unit:
989   Dwarf_Off offset = 0;
990   Dwarf_Off next_offset = 0;
991   size_t length;
992
993   while (dwarf_nextcu(dwarf, offset, &next_offset, &length, nullptr, nullptr, nullptr) ==
994          0) {
995     Dwarf_Die unit_die;
996     if (dwarf_offdie(dwarf, offset + length, &unit_die) != nullptr)
997       MC_dwarf_handle_children(info, &unit_die, &unit_die, nullptr, nullptr);
998     offset = next_offset;
999   }
1000 }
1001
1002 /** Get the build-id (NT_GNU_BUILD_ID) from the ELF file
1003  *
1004  *  This build-id may is used to locate an external debug (DWARF) file
1005  *  for this ELF file.
1006  *
1007  *  @param  elf libelf handle for an ELF file
1008  *  @return build-id for this ELF file (or an empty vector if none is found)
1009  */
1010 static
1011 std::vector<char> get_build_id(Elf* elf)
1012 {
1013 #ifdef __linux
1014   // Summary: the GNU build ID is stored in a ("GNU, NT_GNU_BUILD_ID) note
1015   // found in a PT_NOTE entry in the program header table.
1016
1017   size_t phnum;
1018   if (elf_getphdrnum (elf, &phnum) != 0)
1019     xbt_die("Could not read program headers");
1020
1021   // Iterate over the program headers and find the PT_NOTE ones:
1022   for (size_t i = 0; i < phnum; ++i) {
1023     GElf_Phdr phdr_temp;
1024     GElf_Phdr *phdr = gelf_getphdr(elf, i, &phdr_temp);
1025     if (phdr->p_type != PT_NOTE)
1026       continue;
1027
1028     Elf_Data* data = elf_getdata_rawchunk(elf, phdr->p_offset, phdr->p_filesz, ELF_T_NHDR);
1029
1030     // Iterate over the notes and find the NT_GNU_BUILD_ID one:
1031     size_t pos = 0;
1032     while (pos < data->d_size) {
1033       GElf_Nhdr nhdr;
1034       // Location of the name within Elf_Data:
1035       size_t name_pos;
1036       size_t desc_pos;
1037       pos = gelf_getnote(data, pos, &nhdr, &name_pos, &desc_pos);
1038       // A build ID note is identified by the pair ("GNU", NT_GNU_BUILD_ID)
1039       // (a namespace and a type within this namespace):
1040       if (nhdr.n_type == NT_GNU_BUILD_ID
1041           && nhdr.n_namesz == sizeof("GNU")
1042           && memcmp((char*) data->d_buf + name_pos, "GNU", sizeof("GNU")) == 0) {
1043         XBT_DEBUG("Found GNU/NT_GNU_BUILD_ID note");
1044         char* start = (char*) data->d_buf + desc_pos;
1045         char* end = (char*) start + nhdr.n_descsz;
1046         return std::vector<char>(start, end);
1047       }
1048     }
1049
1050   }
1051 #endif
1052   return std::vector<char>();
1053 }
1054
1055 static char hexdigits[16] = {
1056   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
1057   'a', 'b', 'c', 'd', 'e', 'f'
1058 };
1059
1060 /** Binary data to hexadecimal */
1061 static inline
1062 std::array<char, 2> to_hex(std::uint8_t byte)
1063 {
1064   // Horrid double braces!
1065   // Apparently, this is needed in C++11 (not in C++14).
1066   return { { hexdigits[byte >> 4], hexdigits[byte & 0xF] } };
1067 }
1068
1069 /** Binary data to hexadecimal */
1070 static
1071 std::string to_hex(const char* data, std::size_t count)
1072 {
1073   std::string res;
1074   res.resize(2*count);
1075   for (std::size_t i = 0; i < count; i++) {
1076     std::array<char, 2> hex_byte = to_hex(data[i]);
1077     for (int j = 0; j < 2; ++j)
1078       res[2 * i + j] = hex_byte[j];
1079   }
1080   return res;
1081 }
1082
1083 /** Binary data to hexadecimal */
1084 static
1085 std::string to_hex(std::vector<char> const& data)
1086 {
1087   return to_hex(data.data(), data.size());
1088 }
1089
1090 /** Base directories for external debug files */
1091 static
1092 const char* debug_paths[] = {
1093   "/usr/lib/debug/",
1094   "/usr/local/lib/debug/",
1095 };
1096
1097 /** Locate an external debug file from the NT_GNU_BUILD_ID
1098  *
1099  *  This is one of the mechanisms used for
1100  *  [separate debug files](https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html).
1101  */
1102 // Example:
1103 // /usr/lib/debug/.build-id/0b/dc77f1c29aea2b14ff5acd9a19ab3175ffdeae.debug
1104 static
1105 std::string find_by_build_id(std::vector<char> id)
1106 {
1107   std::string filename;
1108   std::string hex = to_hex(id);
1109   for (const char* const& debug_path : debug_paths) {
1110     // Example:
1111     filename = std::string(debug_path) + ".build-id/"
1112       + to_hex(id.data(), 1) + '/'
1113       + to_hex(id.data() + 1, id.size() - 1) + ".debug";
1114     XBT_DEBUG("Checking debug file: %s", filename.c_str());
1115     if (access(filename.c_str(), F_OK) == 0) {
1116       XBT_DEBUG("Found debug file: %s\n", hex.c_str());
1117       return filename;
1118     }
1119   }
1120   XBT_DEBUG("Not debuf info found for build ID %s\n", hex.data());
1121   return std::string();
1122 }
1123
1124 /** \brief Populate the debugging informations of the given ELF object
1125  *
1126  *  Read the DWARf information of the EFFL object and populate the
1127  *  lists of types, variables, functions.
1128  */
1129 static
1130 void MC_load_dwarf(simgrid::mc::ObjectInformation* info)
1131 {
1132   if (elf_version(EV_CURRENT) == EV_NONE)
1133     xbt_die("libelf initialization error");
1134
1135   // Open the ELF file:
1136   int fd = open(info->file_name.c_str(), O_RDONLY);
1137   if (fd < 0)
1138     xbt_die("Could not open file %s", info->file_name.c_str());
1139   Elf* elf = elf_begin(fd, ELF_C_READ, nullptr);
1140   if (elf == nullptr)
1141     xbt_die("Not an ELF file");
1142   Elf_Kind kind = elf_kind(elf);
1143   if (kind != ELF_K_ELF)
1144     xbt_die("Not an ELF file");
1145
1146   // Remember if this is a `ET_EXEC` (fixed location) or `ET_DYN`:
1147   Elf64_Half type = get_type(elf);
1148   if (type == ET_EXEC)
1149     info->flags |= simgrid::mc::ObjectInformation::Executable;
1150
1151   // Read DWARF debug information in the file:
1152   Dwarf* dwarf = dwarf_begin_elf (elf, DWARF_C_READ, nullptr);
1153   if (dwarf != nullptr) {
1154     read_dwarf_info(info, dwarf);
1155     dwarf_end(dwarf);
1156     elf_end(elf);
1157     close(fd);
1158     return;
1159   }
1160   dwarf_end(dwarf);
1161
1162   // If there was no DWARF in the file, try to find it in a separate file.
1163   // Different methods might be used to store the DWARF informations:
1164   //  * GNU NT_GNU_BUILD_ID
1165   //  * .gnu_debuglink
1166   // See https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html
1167   // for reference of what we are doing.
1168
1169   // Try with NT_GNU_BUILD_ID: we find the build ID in the ELF file and then
1170   // use this ID to find the file in some known locations in the filesystem.
1171   std::vector<char> build_id = get_build_id(elf);
1172   if (not build_id.empty()) {
1173     elf_end(elf);
1174     close(fd);
1175
1176     // Find the debug file using the build id:
1177     std::string debug_file = find_by_build_id(build_id);
1178     if (debug_file.empty()) {
1179       std::string hex = to_hex(build_id);
1180       xbt_die("Missing debug info for %s with build-id %s\n"
1181         "You might want to install the suitable debugging package.\n",
1182         info->file_name.c_str(), hex.c_str());
1183     }
1184
1185     // Load the DWARF info from this file:
1186     XBT_DEBUG("Load DWARF for %s from %s",
1187       info->file_name.c_str(), debug_file.c_str());
1188     fd = open(debug_file.c_str(), O_RDONLY);
1189     if (fd < 0)
1190       xbt_die("Could not open file %s", debug_file.c_str());
1191     Dwarf* dwarf = dwarf_begin(fd, DWARF_C_READ);
1192     if (dwarf == nullptr)
1193       xbt_die("No DWARF info in %s for %s",
1194         debug_file.c_str(), info->file_name.c_str());
1195     read_dwarf_info(info, dwarf);
1196     dwarf_end(dwarf);
1197     close(fd);
1198     return;
1199   }
1200
1201   // TODO, try to find DWARF info using .gnu_debuglink.
1202
1203   elf_end(elf);
1204   close(fd);
1205   xbt_die("Debugging information not found for %s\n"
1206     "Try recompiling with -g\n",
1207     info->file_name.c_str());
1208 }
1209
1210 // ***** Functions index
1211
1212 static void MC_make_functions_index(simgrid::mc::ObjectInformation* info)
1213 {
1214   info->functions_index.clear();
1215
1216   for (auto& e : info->subprograms) {
1217     if (e.second.range.begin() == 0)
1218       continue;
1219     simgrid::mc::FunctionIndexEntry entry;
1220     entry.low_pc = (void*) e.second.range.begin();
1221     entry.function = &e.second;
1222     info->functions_index.push_back(entry);
1223   }
1224
1225   info->functions_index.shrink_to_fit();
1226
1227   // Sort the array by low_pc:
1228   boost::range::sort(info->functions_index,
1229         [](simgrid::mc::FunctionIndexEntry const& a,
1230           simgrid::mc::FunctionIndexEntry const& b)
1231         {
1232           return a.low_pc < b.low_pc;
1233         });
1234 }
1235
1236 static void MC_post_process_variables(simgrid::mc::ObjectInformation* info)
1237 {
1238   // Someone needs this to be sorted but who?
1239   boost::range::sort(info->global_variables, MC_compare_variable);
1240
1241   for (simgrid::mc::Variable& variable : info->global_variables)
1242     if (variable.type_id)
1243       variable.type = simgrid::util::find_map_ptr(
1244         info->types, variable.type_id);
1245 }
1246
1247 static void mc_post_process_scope(simgrid::mc::ObjectInformation* info, simgrid::mc::Frame* scope)
1248 {
1249
1250   if (scope->tag == DW_TAG_inlined_subroutine) {
1251     // Attach correct namespaced name in inlined subroutine:
1252     auto i = info->subprograms.find(scope->abstract_origin_id);
1253     xbt_assert(i != info->subprograms.end(),
1254       "Could not lookup abstract origin %" PRIx64,
1255       (std::uint64_t) scope->abstract_origin_id);
1256     scope->name = i->second.name;
1257   }
1258
1259   // Direct:
1260   for (simgrid::mc::Variable& variable : scope->variables)
1261     if (variable.type_id)
1262       variable.type = simgrid::util::find_map_ptr(
1263         info->types, variable.type_id);
1264
1265   // Recursive post-processing of nested-scopes:
1266   for (simgrid::mc::Frame& nested_scope : scope->scopes)
1267     mc_post_process_scope(info, &nested_scope);
1268 }
1269
1270 static
1271 simgrid::mc::Type* MC_resolve_type(
1272   simgrid::mc::ObjectInformation* info, unsigned type_id)
1273 {
1274   if (not type_id)
1275     return nullptr;
1276   simgrid::mc::Type* type = simgrid::util::find_map_ptr(info->types, type_id);
1277   if (type == nullptr)
1278     return nullptr;
1279
1280   // We already have the information on the type:
1281   if (type->byte_size != 0)
1282     return type;
1283
1284   // Don't have a name, we can't find a more complete version:
1285   if (type->name.empty())
1286     return type;
1287
1288   // Try to find a more complete description of the type:
1289   // We need to fix in order to support C++.
1290   simgrid::mc::Type** subtype = simgrid::util::find_map_ptr(
1291     info->full_types_by_name, type->name);
1292   if (subtype)
1293     type = *subtype;
1294   return type;
1295 }
1296
1297 static void MC_post_process_types(simgrid::mc::ObjectInformation* info)
1298 {
1299   // Lookup "subtype" field:
1300   for (auto& i : info->types) {
1301     i.second.subtype = MC_resolve_type(info, i.second.type_id);
1302     for (simgrid::mc::Member& member : i.second.members)
1303       member.type = MC_resolve_type(info, member.type_id);
1304   }
1305 }
1306
1307 namespace simgrid {
1308 namespace mc {
1309
1310 /** \brief Finds informations about a given shared object/executable */
1311 std::shared_ptr<simgrid::mc::ObjectInformation> createObjectInformation(
1312   std::vector<simgrid::xbt::VmMap> const& maps, const char *name)
1313 {
1314   std::shared_ptr<simgrid::mc::ObjectInformation> result =
1315     std::make_shared<simgrid::mc::ObjectInformation>();
1316   result->file_name = name;
1317   simgrid::mc::find_object_address(maps, result.get());
1318   MC_load_dwarf(result.get());
1319   MC_post_process_variables(result.get());
1320   MC_post_process_types(result.get());
1321   for (auto& entry : result.get()->subprograms)
1322     mc_post_process_scope(result.get(), &entry.second);
1323   MC_make_functions_index(result.get());
1324   return result;
1325 }
1326
1327 /*************************************************************************/
1328
1329 void postProcessObjectInformation(simgrid::mc::RemoteClient* process, simgrid::mc::ObjectInformation* info)
1330 {
1331   for (auto& i : info->types) {
1332
1333     simgrid::mc::Type* type = &(i.second);
1334     simgrid::mc::Type* subtype = type;
1335     while (subtype->type == DW_TAG_typedef
1336         || subtype->type == DW_TAG_volatile_type
1337         || subtype->type == DW_TAG_const_type)
1338       if (subtype->subtype)
1339         subtype = subtype->subtype;
1340       else
1341         break;
1342
1343     // Resolve full_type:
1344     if (not subtype->name.empty() && subtype->byte_size == 0)
1345       for (auto const& object_info : process->object_infos) {
1346         auto i = object_info->full_types_by_name.find(subtype->name);
1347         if (i != object_info->full_types_by_name.end() && not i->second->name.empty() && i->second->byte_size) {
1348           type->full_type = i->second;
1349           break;
1350         }
1351       }
1352     else type->full_type = subtype;
1353
1354   }
1355 }
1356
1357 }
1358 }
1359
1360 namespace simgrid {
1361 namespace dwarf {
1362
1363 /** Convert a DWARF register into a libunwind register
1364  *
1365  *  DWARF and libunwind does not use the same convention for numbering the
1366  *  registers on some architectures. The function makes the necessary
1367  *  conversion.
1368  */
1369 int dwarf_register_to_libunwind(int dwarf_register)
1370 {
1371 #if defined(__x86_64__)
1372   // It seems for this arch, DWARF and libunwind agree in the numbering:
1373   return dwarf_register;
1374 #elif defined(__i386__)
1375   // Couldn't find the authoritative source of information for this.
1376   // This is inspired from http://source.winehq.org/source/dlls/dbghelp/cpu_i386.c#L517.
1377   switch (dwarf_register) {
1378   case 0:
1379     return UNW_X86_EAX;
1380   case 1:
1381     return UNW_X86_ECX;
1382   case 2:
1383     return UNW_X86_EDX;
1384   case 3:
1385     return UNW_X86_EBX;
1386   case 4:
1387     return UNW_X86_ESP;
1388   case 5:
1389     return UNW_X86_EBP;
1390   case 6:
1391     return UNW_X86_ESI;
1392   case 7:
1393     return UNW_X86_EDI;
1394   case 8:
1395     return UNW_X86_EIP;
1396   case 9:
1397     return UNW_X86_EFLAGS;
1398   case 10:
1399     return UNW_X86_CS;
1400   case 11:
1401     return UNW_X86_SS;
1402   case 12:
1403     return UNW_X86_DS;
1404   case 13:
1405     return UNW_X86_ES;
1406   case 14:
1407     return UNW_X86_FS;
1408   case 15:
1409     return UNW_X86_GS;
1410   case 16:
1411     return UNW_X86_ST0;
1412   case 17:
1413     return UNW_X86_ST1;
1414   case 18:
1415     return UNW_X86_ST2;
1416   case 19:
1417     return UNW_X86_ST3;
1418   case 20:
1419     return UNW_X86_ST4;
1420   case 21:
1421     return UNW_X86_ST5;
1422   case 22:
1423     return UNW_X86_ST6;
1424   case 23:
1425     return UNW_X86_ST7;
1426   default:
1427     xbt_die("Bad/unknown register number.");
1428   }
1429 #else
1430 #error This architecture is not supported yet for DWARF expression evaluation.
1431 #endif
1432 }
1433
1434 }
1435 }