Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Make Process::subprograms a std::unordered_map
[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::uint64_t 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::uint64_t MC_dwarf_at_type(Dwarf_Die * die)
313 {
314   return MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
315 }
316
317 static uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die * die, int attribute)
318 {
319   Dwarf_Attribute attr;
320   if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
321     return 0;
322   Dwarf_Addr value;
323   if (dwarf_formaddr(&attr, &value) == 0)
324     return (uint64_t) value;
325   else
326     return 0;
327 }
328
329 static uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die * die, int attribute,
330                                              uint64_t default_value)
331 {
332   Dwarf_Attribute attr;
333   if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
334     return default_value;
335   Dwarf_Word value;
336   return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr),
337                          &value) == 0 ? (uint64_t) value : default_value;
338 }
339
340 static bool MC_dwarf_attr_flag(Dwarf_Die * die, int attribute, bool integrate)
341 {
342   Dwarf_Attribute attr;
343   if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
344        : dwarf_attr(die, attribute, &attr)) == 0)
345     return false;
346
347   bool result;
348   if (dwarf_formflag(&attr, &result))
349     xbt_die("Unexpected form for attribute %s", MC_dwarf_attrname(attribute));
350   return result;
351 }
352
353 /** \brief Find the default lower bound for a given language
354  *
355  *  The default lower bound of an array (when DW_TAG_lower_bound
356  *  is missing) depends on the language of the compilation unit.
357  *
358  *  \param lang Language of the compilation unit (values defined in the DWARF spec)
359  *  \return     Default lower bound of an array in this compilation unit
360  * */
361 static uint64_t MC_dwarf_default_lower_bound(int lang)
362 {
363   switch (lang) {
364   case DW_LANG_C:
365   case DW_LANG_C89:
366   case DW_LANG_C99:
367   case DW_LANG_C_plus_plus:
368   case DW_LANG_D:
369   case DW_LANG_Java:
370   case DW_LANG_ObjC:
371   case DW_LANG_ObjC_plus_plus:
372   case DW_LANG_Python:
373   case DW_LANG_UPC:
374     return 0;
375   case DW_LANG_Ada83:
376   case DW_LANG_Ada95:
377   case DW_LANG_Fortran77:
378   case DW_LANG_Fortran90:
379   case DW_LANG_Fortran95:
380   case DW_LANG_Modula2:
381   case DW_LANG_Pascal83:
382   case DW_LANG_PL1:
383   case DW_LANG_Cobol74:
384   case DW_LANG_Cobol85:
385     return 1;
386   default:
387     xbt_die("No default DW_TAG_lower_bound for language %i and none given",
388             lang);
389     return 0;
390   }
391 }
392
393 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
394  *
395  *  \param die  the DIE
396  *  \param unit DIE of the compilation unit
397  *  \return     number of elements in the range
398  * */
399 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
400                                                 Dwarf_Die * unit)
401 {
402   xbt_assert(dwarf_tag(die) == DW_TAG_enumeration_type
403              || dwarf_tag(die) == DW_TAG_subrange_type,
404              "MC_dwarf_subrange_element_count called with DIE of type %s",
405              MC_dwarf_die_tagname(die));
406
407   // Use DW_TAG_count if present:
408   if (dwarf_hasattr_integrate(die, DW_AT_count)) {
409     return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
410   }
411   // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
412
413   if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
414     // This is not really 0, but the code expects this (we do not know):
415     return 0;
416   }
417   uint64_t upper_bound =
418       MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
419
420   uint64_t lower_bound = 0;
421   if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
422     lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
423   } else {
424     lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
425   }
426   return upper_bound - lower_bound + 1;
427 }
428
429 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
430  *
431  *  The compilation unit might be needed because the default lower
432  *  bound depends on the language of the compilation unit.
433  *
434  *  \param die the DIE of the DW_TAG_array_type
435  *  \param unit the DIE of the compilation unit
436  *  \return number of elements in this array type
437  * */
438 static uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit)
439 {
440   xbt_assert(dwarf_tag(die) == DW_TAG_array_type,
441              "MC_dwarf_array_element_count called with DIE of type %s",
442              MC_dwarf_die_tagname(die));
443
444   int result = 1;
445   Dwarf_Die child;
446   int res;
447   for (res = dwarf_child(die, &child); res == 0;
448        res = dwarf_siblingof(&child, &child)) {
449     int child_tag = dwarf_tag(&child);
450     if (child_tag == DW_TAG_subrange_type
451         || child_tag == DW_TAG_enumeration_type) {
452       result *= MC_dwarf_subrange_element_count(&child, unit);
453     }
454   }
455   return result;
456 }
457
458 // ***** mc_type_t
459
460 /** \brief Initialize the location of a member of a type
461  * (DW_AT_data_member_location of a DW_TAG_member).
462  *
463  *  \param  type   a type (struct, class)
464  *  \param  member the member of the type
465  *  \param  child  DIE of the member (DW_TAG_member)
466  */
467 static void MC_dwarf_fill_member_location(mc_type_t type, mc_type_t member,
468                                           Dwarf_Die * child)
469 {
470   if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
471     xbt_die("Can't groke DW_AT_data_bit_offset.");
472   }
473
474   if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
475     if (type->type != DW_TAG_union_type) {
476       xbt_die
477           ("Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%"
478            PRIx64 ">%s", member->name.c_str(),
479            (uint64_t) type->id, type->name.c_str());
480     } else {
481       return;
482     }
483   }
484
485   Dwarf_Attribute attr;
486   dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
487   int form = dwarf_whatform(&attr);
488   int klass = MC_dwarf_form_get_class(form);
489   switch (klass) {
490   case MC_DW_CLASS_EXPRLOC:
491   case MC_DW_CLASS_BLOCK:
492     // Location expression:
493     {
494       Dwarf_Op *expr;
495       size_t len;
496       if (dwarf_getlocation(&attr, &expr, &len)) {
497         xbt_die
498             ("Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%"
499              PRIx64 ">%s", MC_dwarf_attr_integrate_string(child, DW_AT_name),
500              (uint64_t) type->id, type->name.c_str());
501       }
502       simgrid::mc::DwarfExpression(expr, expr+len);
503       break;
504     }
505   case MC_DW_CLASS_CONSTANT:
506     // Offset from the base address of the object:
507     {
508       Dwarf_Word offset;
509       if (!dwarf_formudata(&attr, &offset))
510         member->offset(offset);
511       else
512         xbt_die("Cannot get %s location <%" PRIx64 ">%s",
513                 MC_dwarf_attr_integrate_string(child, DW_AT_name),
514                 (uint64_t) type->id, type->name.c_str());
515       break;
516     }
517   case MC_DW_CLASS_LOCLISTPTR:
518     // Reference to a location list:
519     // TODO
520   case MC_DW_CLASS_REFERENCE:
521     // It's supposed to be possible in DWARF2 but I couldn't find its semantic
522     // in the spec.
523   default:
524     xbt_die("Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
525             klass, form);
526   }
527
528 }
529
530 static void dw_type_free_voidp(void *t)
531 {
532   delete *(mc_type_t*)t;
533 }
534
535 /** \brief Populate the list of members of a type
536  *
537  *  \param info ELF object containing the type DIE
538  *  \param die  DIE of the type
539  *  \param unit DIE of the compilation unit containing the type DIE
540  *  \param type the type
541  */
542 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die * die,
543                                  Dwarf_Die * unit, mc_type_t type)
544 {
545   int res;
546   Dwarf_Die child;
547   xbt_assert(type->members.empty());
548   for (res = dwarf_child(die, &child); res == 0;
549        res = dwarf_siblingof(&child, &child)) {
550     int tag = dwarf_tag(&child);
551     if (tag == DW_TAG_member || tag == DW_TAG_inheritance) {
552
553       // Skip declarations:
554       if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
555         continue;
556
557       // Skip compile time constants:
558       if (dwarf_hasattr(&child, DW_AT_const_value))
559         continue;
560
561       // TODO, we should use another type (because is is not a type but a member)
562       simgrid::mc::Type member;
563       member.type = tag;
564
565       // Global Offset:
566       member.id = dwarf_dieoffset(&child);
567
568       const char *name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
569       if (name)
570         member.name = name;
571       member.byte_size =
572           MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
573       member.element_count = -1;
574       member.type_id = MC_dwarf_at_type(&child);
575
576       if (dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
577         xbt_die("Can't groke DW_AT_data_bit_offset.");
578       }
579
580       MC_dwarf_fill_member_location(type, &member, &child);
581
582       if (!member.type_id) {
583         xbt_die("Missing type for member %s of <%" PRIx64 ">%s",
584                 member.name.c_str(),
585                 (uint64_t) type->id, type->name.c_str());
586       }
587
588       type->members.push_back(std::move(member));
589     }
590   }
591 }
592
593 /** \brief Create a MC type object from a DIE
594  *
595  *  \param info current object info object
596  *  \param DIE (for a given type);
597  *  \param unit compilation unit of the current DIE
598  *  \return MC representation of the type
599  */
600 static simgrid::mc::Type MC_dwarf_die_to_type(
601   mc_object_info_t info, Dwarf_Die * die,
602   Dwarf_Die * unit, mc_frame_t frame,
603   const char *ns)
604 {
605
606   simgrid::mc::Type type;
607   type.type = -1;
608   type.name = std::string();
609   type.element_count = -1;
610
611   type.type = dwarf_tag(die);
612
613   // Global Offset
614   type.id = dwarf_dieoffset(die);
615
616   const char *prefix = "";
617   switch (type.type) {
618   case DW_TAG_structure_type:
619     prefix = "struct ";
620     break;
621   case DW_TAG_union_type:
622     prefix = "union ";
623     break;
624   case DW_TAG_class_type:
625     prefix = "class ";
626     break;
627   default:
628     prefix = "";
629   }
630
631   const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
632   if (name != NULL) {
633     char* full_name = ns ? bprintf("%s%s::%s", prefix, ns, name) :
634       bprintf("%s%s", prefix, name);
635     type.name = std::string(full_name);
636     free(full_name);
637   }
638
639   type.type_id = MC_dwarf_at_type(die);
640
641   // Some compilers do not emit DW_AT_byte_size for pointer_type,
642   // so we fill this. We currently assume that the model-checked process is in
643   // the same architecture..
644   if (type.type == DW_TAG_pointer_type)
645     type.byte_size = sizeof(void*);
646
647   // Computation of the byte_size;
648   if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
649     type.byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
650   else if (type.type == DW_TAG_array_type
651            || type.type == DW_TAG_structure_type
652            || type.type == DW_TAG_class_type) {
653     Dwarf_Word size;
654     if (dwarf_aggregate_size(die, &size) == 0) {
655       type.byte_size = size;
656     }
657   }
658
659   switch (type.type) {
660   case DW_TAG_array_type:
661     type.element_count = MC_dwarf_array_element_count(die, unit);
662     // TODO, handle DW_byte_stride and (not) DW_bit_stride
663     break;
664
665   case DW_TAG_pointer_type:
666   case DW_TAG_reference_type:
667   case DW_TAG_rvalue_reference_type:
668     type.is_pointer_type = 1;
669     break;
670
671   case DW_TAG_structure_type:
672   case DW_TAG_union_type:
673   case DW_TAG_class_type:
674     MC_dwarf_add_members(info, die, unit, &type);
675     char *new_ns = ns == NULL ? xbt_strdup(type.name.c_str())
676         : bprintf("%s::%s", ns, name);
677     MC_dwarf_handle_children(info, die, unit, frame, new_ns);
678     free(new_ns);
679     break;
680   }
681
682   return std::move(type);
683 }
684
685 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die * die,
686                                      Dwarf_Die * unit, mc_frame_t frame,
687                                      const char *ns)
688 {
689   simgrid::mc::Type type = MC_dwarf_die_to_type(info, die, unit, frame, ns);
690   auto& t = (info->types[type.id] = std::move(type));
691   if (!t.name.empty() && type.byte_size != 0)
692     info->full_types_by_name[t.name] = &t;
693 }
694
695 static int mc_anonymous_variable_index = 0;
696
697 static std::unique_ptr<simgrid::mc::Variable> MC_die_to_variable(
698   mc_object_info_t info, Dwarf_Die * die,
699   Dwarf_Die * unit, mc_frame_t frame,
700   const char *ns)
701 {
702   // Skip declarations:
703   if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
704     return nullptr;
705
706   // Skip compile time constants:
707   if (dwarf_hasattr(die, DW_AT_const_value))
708     return nullptr;
709
710   Dwarf_Attribute attr_location;
711   if (dwarf_attr(die, DW_AT_location, &attr_location) == NULL) {
712     // No location: do not add it ?
713     return nullptr;
714   }
715
716   std::unique_ptr<simgrid::mc::Variable> variable =
717     std::unique_ptr<simgrid::mc::Variable>(new simgrid::mc::Variable());
718   variable->dwarf_offset = dwarf_dieoffset(die);
719   variable->global = frame == NULL;     // Can be override base on DW_AT_location
720   variable->object_info = info;
721
722   const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
723   if (name)
724     variable->name = name;
725   variable->type_id = MC_dwarf_at_type(die);
726
727   int form = dwarf_whatform(&attr_location);
728   int klass =
729       form ==
730       DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
731   switch (klass) {
732   case MC_DW_CLASS_EXPRLOC:
733   case MC_DW_CLASS_BLOCK:
734     // Location expression:
735     {
736       Dwarf_Op *expr;
737       size_t len;
738       if (dwarf_getlocation(&attr_location, &expr, &len)) {
739         xbt_die(
740           "Could not read location expression in DW_AT_location "
741           "of variable <%" PRIx64 ">%s",
742           (uint64_t) variable->dwarf_offset,
743           variable->name.c_str());
744       }
745
746       if (len == 1 && expr[0].atom == DW_OP_addr) {
747         variable->global = 1;
748         uintptr_t offset = (uintptr_t) expr[0].number;
749         uintptr_t base = (uintptr_t) info->base_address();
750         variable->address = (void *) (base + offset);
751       } else {
752         simgrid::mc::LocationListEntry entry;
753         entry.expression = {expr, expr + len};
754         variable->location_list = { std::move(entry) };
755       }
756
757       break;
758     }
759   case MC_DW_CLASS_LOCLISTPTR:
760   case MC_DW_CLASS_CONSTANT:
761     // Reference to location list:
762     mc_dwarf_location_list_init(
763       &variable->location_list, info, die,
764       &attr_location);
765     break;
766   default:
767     xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location "
768             "in <%" PRIx64 ">%s",
769             form, form, klass, klass,
770             (uint64_t) variable->dwarf_offset,
771             variable->name.c_str());
772   }
773
774   // Handle start_scope:
775   if (dwarf_hasattr(die, DW_AT_start_scope)) {
776     Dwarf_Attribute attr;
777     dwarf_attr(die, DW_AT_start_scope, &attr);
778     int form = dwarf_whatform(&attr);
779     int klass = MC_dwarf_form_get_class(form);
780     switch (klass) {
781     case MC_DW_CLASS_CONSTANT:
782       {
783         Dwarf_Word value;
784         variable->start_scope =
785             dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
786         break;
787       }
788     case MC_DW_CLASS_RANGELISTPTR:     // TODO
789     default:
790       xbt_die
791           ("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
792            form, klass, name == NULL ? "?" : name);
793     }
794   }
795
796   if (ns && variable->global)
797     variable->name =
798       std::string(ns) + "::" + variable->name;
799
800   // The current code needs a variable name,
801   // generate a fake one:
802   if (variable->name.empty())
803     variable->name =
804       "@anonymous#" + std::to_string(mc_anonymous_variable_index++);
805
806   return std::move(variable);
807 }
808
809 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die * die,
810                                          Dwarf_Die * unit, mc_frame_t frame,
811                                          const char *ns)
812 {
813   MC_dwarf_register_variable(info, frame,
814     MC_die_to_variable(info, die, unit, frame, ns));
815 }
816
817 static void MC_dwarf_handle_scope_die(mc_object_info_t info, Dwarf_Die * die,
818                                       Dwarf_Die * unit, mc_frame_t parent_frame,
819                                       const char *ns)
820 {
821   // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
822   int tag = dwarf_tag(die);
823   mc_tag_class klass = MC_dwarf_tag_classify(tag);
824
825   // (Template) Subprogram declaration:
826   if (klass == mc_tag_subprogram
827       && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
828     return;
829
830   if (klass == mc_tag_scope)
831     xbt_assert(parent_frame, "No parent scope for this scope");
832
833   simgrid::mc::Frame frame;
834
835   frame.tag = tag;
836   frame.id = dwarf_dieoffset(die);
837   frame.object_info = info;
838
839   if (klass == mc_tag_subprogram) {
840     const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
841     if(ns)
842       frame.name  = std::string(ns) + "::" + name;
843     else
844       frame.name = name;
845   }
846
847   frame.abstract_origin_id =
848     MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
849
850   // This is the base address for DWARF addresses.
851   // Relocated addresses are offset from this base address.
852   // See DWARF4 spec 7.5
853   void *base = info->base_address();
854
855   // TODO, support DW_AT_ranges
856   uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
857   frame.low_pc = low_pc ? ((char *) base) + low_pc : 0;
858   if (low_pc) {
859     // DW_AT_high_pc:
860     Dwarf_Attribute attr;
861     if (!dwarf_attr_integrate(die, DW_AT_high_pc, &attr)) {
862       xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
863     }
864
865     Dwarf_Sword offset;
866     Dwarf_Addr high_pc;
867
868     switch (MC_dwarf_form_get_class(dwarf_whatform(&attr))) {
869
870       // DW_AT_high_pc if an offset from the low_pc:
871     case MC_DW_CLASS_CONSTANT:
872
873       if (dwarf_formsdata(&attr, &offset) != 0)
874         xbt_die("Could not read constant");
875       frame.high_pc = (void *) ((char *) frame.low_pc + offset);
876       break;
877
878       // DW_AT_high_pc is a relocatable address:
879     case MC_DW_CLASS_ADDRESS:
880       if (dwarf_formaddr(&attr, &high_pc) != 0)
881         xbt_die("Could not read address");
882       frame.high_pc = ((char *) base) + high_pc;
883       break;
884
885     default:
886       xbt_die("Unexpected class for DW_AT_high_pc");
887
888     }
889   }
890
891   if (klass == mc_tag_subprogram) {
892     Dwarf_Attribute attr_frame_base;
893     if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
894       mc_dwarf_location_list_init(&frame.frame_base, info, die,
895                                   &attr_frame_base);
896   }
897
898   // Handle children:
899   MC_dwarf_handle_children(info, die, unit, &frame, ns);
900
901   // Register it:
902   if (klass == mc_tag_subprogram)
903     info->subprograms[frame.id] = frame;
904   else if (klass == mc_tag_scope)
905     parent_frame->scopes.push_back(std::move(frame));
906 }
907
908 static void mc_dwarf_handle_namespace_die(mc_object_info_t info,
909                                           Dwarf_Die * die, Dwarf_Die * unit,
910                                           mc_frame_t frame,
911                                           const char *ns)
912 {
913   const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
914   if (frame)
915     xbt_die("Unexpected namespace in a subprogram");
916   char *new_ns = ns == NULL ? xbt_strdup(name)
917       : bprintf("%s::%s", ns, name);
918   MC_dwarf_handle_children(info, die, unit, frame, new_ns);
919   xbt_free(new_ns);
920 }
921
922 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die * die,
923                                      Dwarf_Die * unit, mc_frame_t frame,
924                                      const char *ns)
925 {
926   // For each child DIE:
927   Dwarf_Die child;
928   int res;
929   for (res = dwarf_child(die, &child); res == 0;
930        res = dwarf_siblingof(&child, &child)) {
931     MC_dwarf_handle_die(info, &child, unit, frame, ns);
932   }
933 }
934
935 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die * die,
936                                 Dwarf_Die * unit, mc_frame_t frame,
937                                 const char *ns)
938 {
939   int tag = dwarf_tag(die);
940   mc_tag_class klass = MC_dwarf_tag_classify(tag);
941   switch (klass) {
942
943     // Type:
944   case mc_tag_type:
945     MC_dwarf_handle_type_die(info, die, unit, frame, ns);
946     break;
947
948     // Subprogram or scope:
949   case mc_tag_subprogram:
950   case mc_tag_scope:
951     MC_dwarf_handle_scope_die(info, die, unit, frame, ns);
952     return;
953
954     // Variable:
955   case mc_tag_variable:
956     MC_dwarf_handle_variable_die(info, die, unit, frame, ns);
957     break;
958
959   case mc_tag_namespace:
960     mc_dwarf_handle_namespace_die(info, die, unit, frame, ns);
961     break;
962
963   default:
964     break;
965
966   }
967 }
968
969 /** \brief Populate the debugging informations of the given ELF object
970  *
971  *  Read the DWARf information of the EFFL object and populate the
972  *  lists of types, variables, functions.
973  */
974 void MC_dwarf_get_variables(mc_object_info_t info)
975 {
976   int fd = open(info->file_name, O_RDONLY);
977   if (fd < 0) {
978     xbt_die("Could not open file %s", info->file_name);
979   }
980   Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
981   if (dwarf == NULL) {
982     xbt_die("Your program must be compiled with -g (%s)", info->file_name);
983   }
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(mc_function_index_item_t a,
1012                                         mc_function_index_item_t 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   xbt_dynar_t index = xbt_dynar_new(sizeof(s_mc_function_index_item_t), NULL);
1025
1026   for (auto& e : info->subprograms) {
1027     if (e.second.low_pc == nullptr)
1028       continue;
1029     s_mc_function_index_item_t entry;
1030     entry.low_pc = e.second.low_pc;
1031     entry.high_pc = e.second.high_pc;
1032     entry.function = &e.second;
1033     xbt_dynar_push(index, &entry);
1034   }
1035
1036   mc_function_index_item_t base =
1037       (mc_function_index_item_t) xbt_dynar_get_ptr(index, 0);
1038
1039   // Sort the array by low_pc:
1040   qsort(base,
1041         xbt_dynar_length(index),
1042         sizeof(s_mc_function_index_item_t),
1043         (int (*)(const void *, const void *)) MC_compare_frame_index_items);
1044
1045   info->functions_index = index;
1046 }
1047
1048 static void MC_post_process_variables(mc_object_info_t info)
1049 {
1050   for(simgrid::mc::Variable& variable : info->global_variables)
1051     if (variable.type_id) {
1052       auto i = info->types.find(variable.type_id);
1053       if (i != info->types.end())
1054         variable.type = &(i->second);
1055       else
1056         variable.type = nullptr;
1057     }
1058 }
1059
1060 static void mc_post_process_scope(mc_object_info_t info, mc_frame_t scope)
1061 {
1062
1063   if (scope->tag == DW_TAG_inlined_subroutine) {
1064     // Attach correct namespaced name in inlined subroutine:
1065     auto i = info->subprograms.find(scope->abstract_origin_id);
1066     xbt_assert(i != info->subprograms.end(),
1067       "Could not lookup abstract origin %" PRIx64,
1068       (uint64_t) scope->abstract_origin_id);
1069     scope->name = i->second.name;
1070   }
1071
1072   // Direct:
1073   for (simgrid::mc::Variable& variable : scope->variables)
1074     if (variable.type_id) {
1075       auto i = info->types.find(variable.type_id);
1076       if (i != info->types.end())
1077         variable.type = &(i->second);
1078       else
1079         variable.type = nullptr;
1080     }
1081
1082   // Recursive post-processing of nested-scopes:
1083   for (simgrid::mc::Frame& nested_scope : scope->scopes)
1084       mc_post_process_scope(info, &nested_scope);
1085
1086 }
1087
1088 static void MC_post_process_functions(mc_object_info_t info)
1089 {
1090   for (auto& entry : info->subprograms)
1091     mc_post_process_scope(info, &entry.second);
1092 }
1093
1094
1095 /** \brief Fill/lookup the "subtype" field.
1096  */
1097 static void MC_resolve_subtype(mc_object_info_t info, mc_type_t type)
1098 {
1099   if (!type->type_id)
1100     return;
1101   auto i = info->types.find(type->type_id);
1102   if (i != info->types.end())
1103     type->subtype = &(i->second);
1104   else {
1105     type->subtype = nullptr;
1106     return;
1107   }
1108   if (type->subtype->byte_size != 0)
1109     return;
1110   if (type->subtype->name.empty())
1111     return;
1112   // Try to find a more complete description of the type:
1113   // We need to fix in order to support C++.
1114
1115   auto j = info->full_types_by_name.find(type->subtype->name);
1116   if (j != info->full_types_by_name.end())
1117     type->subtype = j->second;
1118 }
1119
1120 static void MC_post_process_types(mc_object_info_t info)
1121 {
1122   // Lookup "subtype" field:
1123   for(auto& i : info->types) {
1124     MC_resolve_subtype(info, &(i.second));
1125     for (simgrid::mc::Type& member : i.second.members)
1126       MC_resolve_subtype(info, &member);
1127   }
1128 }
1129
1130 /** \brief Finds informations about a given shared object/executable */
1131 std::shared_ptr<s_mc_object_info_t> MC_find_object_info(
1132   std::vector<simgrid::mc::VmMap> const& maps, const char *name, int executable)
1133 {
1134   std::shared_ptr<s_mc_object_info_t> result =
1135     std::make_shared<s_mc_object_info_t>();
1136   if (executable)
1137     result->flags |= MC_OBJECT_INFO_EXECUTABLE;
1138   result->file_name = xbt_strdup(name);
1139   MC_find_object_address(maps, result.get());
1140   MC_dwarf_get_variables(result.get());
1141   MC_post_process_types(result.get());
1142   MC_post_process_variables(result.get());
1143   MC_post_process_functions(result.get());
1144   MC_make_functions_index(result.get());
1145   return std::move(result);
1146 }
1147
1148 /*************************************************************************/
1149
1150 static int MC_dwarf_get_variable_index(
1151   std::vector<simgrid::mc::Variable> variables, const char *var, void *address)
1152 {
1153
1154   if (variables.empty())
1155     return 0;
1156
1157   unsigned int cursor = 0;
1158   int start = 0;
1159   int end = variables.size() - 1;
1160   mc_variable_t var_test = nullptr;
1161
1162   while (start <= end) {
1163     cursor = (start + end) / 2;
1164     var_test = &variables[cursor];
1165     if (strcmp(var_test->name.c_str(), var) < 0) {
1166       start = cursor + 1;
1167     } else if (strcmp(var_test->name.c_str(), var) > 0) {
1168       end = cursor - 1;
1169     } else {
1170       if (address) {            /* global variable */
1171         if (var_test->address == address)
1172           return -1;
1173         if (var_test->address > address)
1174           end = cursor - 1;
1175         else
1176           start = cursor + 1;
1177       } else {                  /* local variable */
1178         return -1;
1179       }
1180     }
1181   }
1182
1183   if (strcmp(var_test->name.c_str(), var) == 0) {
1184     if (address && var_test->address < address)
1185       return cursor + 1;
1186     else
1187       return cursor;
1188   } else if (strcmp(var_test->name.c_str(), var) < 0)
1189     return cursor + 1;
1190   else
1191     return cursor;
1192
1193 }
1194
1195 void MC_dwarf_register_global_variable(
1196   mc_object_info_t info,
1197   std::unique_ptr<simgrid::mc::Variable> variable)
1198 {
1199   int index =
1200       MC_dwarf_get_variable_index(info->global_variables,
1201         variable->name.c_str(),
1202         variable->address);
1203   if (index != -1)
1204     info->global_variables.insert(
1205       info->global_variables.begin() + index, std::move(*variable));
1206   // TODO, else ?
1207 }
1208
1209 void MC_dwarf_register_non_global_variable(
1210   mc_object_info_t info,
1211   mc_frame_t frame,
1212   std::unique_ptr<simgrid::mc::Variable> variable)
1213 {
1214   xbt_assert(frame, "Frame is NULL");
1215   int index =
1216       MC_dwarf_get_variable_index(
1217         frame->variables, variable->name.c_str(), NULL);
1218   if (index != -1)
1219     frame->variables.insert(
1220       frame->variables.begin() + index, std::move(*variable));
1221   // TODO, else ?
1222 }
1223
1224 void MC_dwarf_register_variable(
1225   mc_object_info_t info, mc_frame_t frame,
1226   std::unique_ptr<simgrid::mc::Variable> variable)
1227 {
1228   if (!variable)
1229     return;
1230   if (variable->global)
1231     MC_dwarf_register_global_variable(info, std::move(variable));
1232   else if (frame != nullptr)
1233     MC_dwarf_register_non_global_variable(info, frame, std::move(variable));
1234   else
1235     xbt_die("No frame for this local variable");
1236 }
1237
1238 void MC_post_process_object_info(mc_process_t process, mc_object_info_t info)
1239 {
1240   for (auto& i : info->types) {
1241
1242     mc_type_t type = &(i.second);
1243     mc_type_t subtype = type;
1244     while (subtype->type == DW_TAG_typedef || subtype->type == DW_TAG_volatile_type
1245       || subtype->type == DW_TAG_const_type) {
1246       if (subtype->subtype)
1247         subtype = subtype->subtype;
1248       else
1249         break;
1250     }
1251
1252     // Resolve full_type:
1253     if (!subtype->name.empty() && subtype->byte_size == 0) {
1254       for (auto const& object_info : process->object_infos) {
1255         auto i = object_info->full_types_by_name.find(subtype->name);
1256         if (i != object_info->full_types_by_name.end()
1257             && !i->second->name.empty() && i->second->byte_size) {
1258           type->full_type = i->second;
1259           break;
1260         }
1261       }
1262     } else type->full_type = subtype;
1263
1264   }
1265 }