Logo AND Algorithmique Numérique Distribuée

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