Logo AND Algorithmique Numérique Distribuée

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