Logo AND Algorithmique Numérique Distribuée

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