Logo AND Algorithmique Numérique Distribuée

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