Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Use string::compare instead of strcmp
[simgrid.git] / src / mc / mc_libdw.cpp
1 /* Copyright (c) 2008-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <cstdint>
8
9 #include <algorithm>
10 #include <memory>
11 #include <string>
12 #include <utility>
13
14 #include <dwarf.h>
15
16 #include <xbt/log.h>
17
18 #include "mc/Frame.hpp"
19 #include "mc/ObjectInformation.hpp"
20 #include "mc/Variable.hpp"
21
22 #include "mc/mc_dwarf.hpp"
23 #include "mc/mc_libdw.hpp"
24
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_libdw, mc, "DWARF processing");
26
27 /** \brief Get the name of the tag of a given DIE
28  *
29  *  \param die DIE
30  *  \return name of the tag of this DIE
31  */
32 static inline const char *MC_dwarf_die_tagname(Dwarf_Die * die)
33 {
34   return MC_dwarf_tagname(dwarf_tag(die));
35 }
36
37 static
38 simgrid::mc::DwarfExpression MC_dwarf_expression(Dwarf_Op* ops, std::size_t len)
39 {
40   simgrid::mc::DwarfExpression expression(len);
41   for (std::size_t i = 0; i != len; ++i) {
42     expression[i].atom = ops[i].atom;
43     expression[i].number = ops[i].number;
44     expression[i].number2 = ops[i].number2;
45     expression[i].offset = ops[i].offset;
46   }
47   return std::move(expression);
48 }
49
50 static void mc_dwarf_location_list_init_libdw(
51   simgrid::mc::LocationList* list, simgrid::mc::ObjectInformation* info,
52   Dwarf_Die * die, Dwarf_Attribute * attr)
53 {
54   list->clear();
55
56   ptrdiff_t offset = 0;
57   Dwarf_Addr base, start, end;
58   Dwarf_Op *ops;
59   size_t len;
60
61   while (1) {
62
63     offset = dwarf_getlocations(attr, offset, &base, &start, &end, &ops, &len);
64     if (offset == 0)
65       return;
66     else if (offset == -1)
67       xbt_die("Error while loading location list");
68
69     simgrid::mc::LocationListEntry entry;
70     entry.expression = MC_dwarf_expression(ops, len);
71
72     void *base = info->base_address();
73     // If start == 0, this is not a location list:
74     entry.lowpc = start == 0 ? NULL : (char *) base + start;
75     entry.highpc = start == 0 ? NULL : (char *) base + end;
76
77     list->push_back(std::move(entry));
78   }
79
80 }
81
82 // ***** Attributes
83
84 /** \brief Get an attribute of a given DIE as a string
85  *
86  *  \param die       the DIE
87  *  \param attribute attribute
88  *  \return value of the given attribute of the given DIE
89  */
90 static const char *MC_dwarf_attr_integrate_string(Dwarf_Die * die,
91                                                   int attribute)
92 {
93   Dwarf_Attribute attr;
94   if (!dwarf_attr_integrate(die, attribute, &attr)) {
95     return NULL;
96   } else {
97     return dwarf_formstring(&attr);
98   }
99 }
100
101 /** \brief Get the linkage name of a DIE.
102  *
103  *  Use either DW_AT_linkage_name or DW_AT_MIPS_linkage_name.
104  *  DW_AT_linkage_name is standardized since DWARF 4.
105  *  Before this version of DWARF, the MIPS extensions
106  *  DW_AT_MIPS_linkage_name is used (at least by GCC).
107  *
108  *  \param  the DIE
109  *  \return linkage name of the given DIE (or NULL)
110  * */
111 static const char *MC_dwarf_at_linkage_name(Dwarf_Die * die)
112 {
113   const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_linkage_name);
114   if (!name)
115     name = MC_dwarf_attr_integrate_string(die, DW_AT_MIPS_linkage_name);
116   return name;
117 }
118
119 static Dwarf_Off MC_dwarf_attr_dieoffset(Dwarf_Die * die, int attribute)
120 {
121   Dwarf_Attribute attr;
122   if (dwarf_hasattr_integrate(die, attribute) == 0)
123     return 0;
124   dwarf_attr_integrate(die, attribute, &attr);
125   Dwarf_Die subtype_die;
126   if (dwarf_formref_die(&attr, &subtype_die) == NULL)
127     xbt_die("Could not find DIE");
128   return dwarf_dieoffset(&subtype_die);
129 }
130
131 static Dwarf_Off MC_dwarf_attr_integrate_dieoffset(Dwarf_Die * die,
132                                                    int attribute)
133 {
134   Dwarf_Attribute attr;
135   if (dwarf_hasattr_integrate(die, attribute) == 0)
136     return 0;
137   dwarf_attr_integrate(die, DW_AT_type, &attr);
138   Dwarf_Die subtype_die;
139   if (dwarf_formref_die(&attr, &subtype_die) == NULL)
140     xbt_die("Could not find DIE");
141   return dwarf_dieoffset(&subtype_die);
142 }
143
144 /** \brief Find the type/subtype (DW_AT_type) for a DIE
145  *
146  *  \param dit the DIE
147  *  \return DW_AT_type reference as a global offset in hexadecimal (or NULL)
148  */
149 static
150 std::uint64_t MC_dwarf_at_type(Dwarf_Die * die)
151 {
152   return MC_dwarf_attr_integrate_dieoffset(die, DW_AT_type);
153 }
154
155 static std::uint64_t MC_dwarf_attr_integrate_addr(Dwarf_Die * die, int attribute)
156 {
157   Dwarf_Attribute attr;
158   if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
159     return 0;
160   Dwarf_Addr value;
161   if (dwarf_formaddr(&attr, &value) == 0)
162     return (std::uint64_t) value;
163   else
164     return 0;
165 }
166
167 static std::uint64_t MC_dwarf_attr_integrate_uint(Dwarf_Die * die, int attribute,
168                                                   std::uint64_t default_value)
169 {
170   Dwarf_Attribute attr;
171   if (dwarf_attr_integrate(die, attribute, &attr) == NULL)
172     return default_value;
173   Dwarf_Word value;
174   return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr),
175                          &value) == 0 ? (std::uint64_t) value : default_value;
176 }
177
178 static bool MC_dwarf_attr_flag(Dwarf_Die * die, int attribute, bool integrate)
179 {
180   Dwarf_Attribute attr;
181   if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
182        : dwarf_attr(die, attribute, &attr)) == 0)
183     return false;
184
185   bool result;
186   if (dwarf_formflag(&attr, &result))
187     xbt_die("Unexpected form for attribute %s", MC_dwarf_attrname(attribute));
188   return result;
189 }
190
191 /** \brief Finds the number of elements in a DW_TAG_subrange_type or DW_TAG_enumeration_type DIE
192  *
193  *  \param die  the DIE
194  *  \param unit DIE of the compilation unit
195  *  \return     number of elements in the range
196  * */
197 static std::uint64_t MC_dwarf_subrange_element_count(Dwarf_Die * die,
198                                                      Dwarf_Die * unit)
199 {
200   xbt_assert(dwarf_tag(die) == DW_TAG_enumeration_type
201              || dwarf_tag(die) == DW_TAG_subrange_type,
202              "MC_dwarf_subrange_element_count called with DIE of type %s",
203              MC_dwarf_die_tagname(die));
204
205   // Use DW_TAG_count if present:
206   if (dwarf_hasattr_integrate(die, DW_AT_count))
207     return MC_dwarf_attr_integrate_uint(die, DW_AT_count, 0);
208   // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
209
210   if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound))
211     // This is not really 0, but the code expects this (we do not know):
212     return 0;
213
214   std::uint64_t upper_bound =
215       MC_dwarf_attr_integrate_uint(die, DW_AT_upper_bound, -1);
216
217   std::uint64_t lower_bound = 0;
218   if (dwarf_hasattr_integrate(die, DW_AT_lower_bound))
219     lower_bound = MC_dwarf_attr_integrate_uint(die, DW_AT_lower_bound, -1);
220   else
221     lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
222   return upper_bound - lower_bound + 1;
223 }
224
225 /** \brief Finds the number of elements in a array type (DW_TAG_array_type)
226  *
227  *  The compilation unit might be needed because the default lower
228  *  bound depends on the language of the compilation unit.
229  *
230  *  \param die the DIE of the DW_TAG_array_type
231  *  \param unit the DIE of the compilation unit
232  *  \return number of elements in this array type
233  * */
234 static
235 std::uint64_t MC_dwarf_array_element_count(Dwarf_Die * die, Dwarf_Die * unit)
236 {
237   xbt_assert(dwarf_tag(die) == DW_TAG_array_type,
238              "MC_dwarf_array_element_count called with DIE of type %s",
239              MC_dwarf_die_tagname(die));
240
241   int result = 1;
242   Dwarf_Die child;
243   int res;
244   for (res = dwarf_child(die, &child); res == 0;
245        res = dwarf_siblingof(&child, &child)) {
246     int child_tag = dwarf_tag(&child);
247     if (child_tag == DW_TAG_subrange_type
248         || child_tag == DW_TAG_enumeration_type)
249       result *= MC_dwarf_subrange_element_count(&child, unit);
250   }
251   return result;
252 }
253
254 // ***** simgrid::mc::Type*
255
256 /** \brief Initialize the location of a member of a type
257  * (DW_AT_data_member_location of a DW_TAG_member).
258  *
259  *  \param  type   a type (struct, class)
260  *  \param  member the member of the type
261  *  \param  child  DIE of the member (DW_TAG_member)
262  */
263 static void MC_dwarf_fill_member_location(simgrid::mc::Type* type, simgrid::mc::Type* member,
264                                           Dwarf_Die * child)
265 {
266   if (dwarf_hasattr(child, DW_AT_data_bit_offset))
267     xbt_die("Can't groke DW_AT_data_bit_offset.");
268
269   if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
270     if (type->type == DW_TAG_union_type)
271       return;
272     xbt_die
273         ("Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%"
274          PRIx64 ">%s", member->name.c_str(),
275          (std::uint64_t) type->id, type->name.c_str());
276   }
277
278   Dwarf_Attribute attr;
279   dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
280   int form = dwarf_whatform(&attr);
281   int klass = MC_dwarf_form_get_class(form);
282   switch (klass) {
283   case MC_DW_CLASS_EXPRLOC:
284   case MC_DW_CLASS_BLOCK:
285     // Location expression:
286     {
287       Dwarf_Op *expr;
288       size_t len;
289       if (dwarf_getlocation(&attr, &expr, &len))
290         xbt_die
291             ("Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%"
292              PRIx64 ">%s", MC_dwarf_attr_integrate_string(child, DW_AT_name),
293              (std::uint64_t) type->id, type->name.c_str());
294       member->location_expression = MC_dwarf_expression(expr, len);
295       break;
296     }
297   case MC_DW_CLASS_CONSTANT:
298     // Offset from the base address of the object:
299     {
300       Dwarf_Word offset;
301       if (!dwarf_formudata(&attr, &offset))
302         member->offset(offset);
303       else
304         xbt_die("Cannot get %s location <%" PRIx64 ">%s",
305                 MC_dwarf_attr_integrate_string(child, DW_AT_name),
306                 (std::uint64_t) type->id, type->name.c_str());
307       break;
308     }
309   case MC_DW_CLASS_LOCLISTPTR:
310     // Reference to a location list:
311     // TODO
312   case MC_DW_CLASS_REFERENCE:
313     // It's supposed to be possible in DWARF2 but I couldn't find its semantic
314     // in the spec.
315   default:
316     xbt_die("Can't handle form class (%i) / form 0x%x as DW_AT_member_location",
317             klass, form);
318   }
319
320 }
321
322 /** \brief Populate the list of members of a type
323  *
324  *  \param info ELF object containing the type DIE
325  *  \param die  DIE of the type
326  *  \param unit DIE of the compilation unit containing the type DIE
327  *  \param type the type
328  */
329 static void MC_dwarf_add_members(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
330                                  Dwarf_Die * unit, simgrid::mc::Type* type)
331 {
332   int res;
333   Dwarf_Die child;
334   xbt_assert(type->members.empty());
335   for (res = dwarf_child(die, &child); res == 0;
336        res = dwarf_siblingof(&child, &child)) {
337     int tag = dwarf_tag(&child);
338     if (tag == DW_TAG_member || tag == DW_TAG_inheritance) {
339
340       // Skip declarations:
341       if (MC_dwarf_attr_flag(&child, DW_AT_declaration, false))
342         continue;
343
344       // Skip compile time constants:
345       if (dwarf_hasattr(&child, DW_AT_const_value))
346         continue;
347
348       // TODO, we should use another type (because is is not a type but a member)
349       simgrid::mc::Type member;
350       member.type = tag;
351
352       // Global Offset:
353       member.id = dwarf_dieoffset(&child);
354
355       const char *name = MC_dwarf_attr_integrate_string(&child, DW_AT_name);
356       if (name)
357         member.name = name;
358       member.byte_size =
359           MC_dwarf_attr_integrate_uint(&child, DW_AT_byte_size, 0);
360       member.element_count = -1;
361       member.type_id = MC_dwarf_at_type(&child);
362
363       if (dwarf_hasattr(&child, DW_AT_data_bit_offset))
364         xbt_die("Can't groke DW_AT_data_bit_offset.");
365
366       MC_dwarf_fill_member_location(type, &member, &child);
367
368       if (!member.type_id)
369         xbt_die("Missing type for member %s of <%" PRIx64 ">%s",
370                 member.name.c_str(),
371                 (std::uint64_t) type->id, type->name.c_str());
372
373       type->members.push_back(std::move(member));
374     }
375   }
376 }
377
378 /** \brief Create a MC type object from a DIE
379  *
380  *  \param info current object info object
381  *  \param DIE (for a given type);
382  *  \param unit compilation unit of the current DIE
383  *  \return MC representation of the type
384  */
385 static simgrid::mc::Type MC_dwarf_die_to_type(
386   simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
387   Dwarf_Die * unit, simgrid::mc::Frame* frame,
388   const char *ns)
389 {
390   simgrid::mc::Type type;
391   type.type = dwarf_tag(die);
392   type.name = std::string();
393   type.element_count = -1;
394
395   // Global Offset
396   type.id = dwarf_dieoffset(die);
397
398   const char *prefix = "";
399   switch (type.type) {
400   case DW_TAG_structure_type:
401     prefix = "struct ";
402     break;
403   case DW_TAG_union_type:
404     prefix = "union ";
405     break;
406   case DW_TAG_class_type:
407     prefix = "class ";
408     break;
409   default:
410     prefix = "";
411   }
412
413   const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
414   if (name != NULL) {
415     char* full_name = ns ? bprintf("%s%s::%s", prefix, ns, name) :
416       bprintf("%s%s", prefix, name);
417     type.name = std::string(full_name);
418     free(full_name);
419   }
420
421   type.type_id = MC_dwarf_at_type(die);
422
423   // Some compilers do not emit DW_AT_byte_size for pointer_type,
424   // so we fill this. We currently assume that the model-checked process is in
425   // the same architecture..
426   if (type.type == DW_TAG_pointer_type)
427     type.byte_size = sizeof(void*);
428
429   // Computation of the byte_size;
430   if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
431     type.byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0);
432   else if (type.type == DW_TAG_array_type
433            || type.type == DW_TAG_structure_type
434            || type.type == DW_TAG_class_type) {
435     Dwarf_Word size;
436     if (dwarf_aggregate_size(die, &size) == 0)
437       type.byte_size = size;
438   }
439
440   switch (type.type) {
441   case DW_TAG_array_type:
442     type.element_count = MC_dwarf_array_element_count(die, unit);
443     // TODO, handle DW_byte_stride and (not) DW_bit_stride
444     break;
445
446   case DW_TAG_pointer_type:
447   case DW_TAG_reference_type:
448   case DW_TAG_rvalue_reference_type:
449     type.is_pointer_type = 1;
450     break;
451
452   case DW_TAG_structure_type:
453   case DW_TAG_union_type:
454   case DW_TAG_class_type:
455     MC_dwarf_add_members(info, die, unit, &type);
456     char *new_ns = ns == NULL ? xbt_strdup(type.name.c_str())
457         : bprintf("%s::%s", ns, name);
458     MC_dwarf_handle_children(info, die, unit, frame, new_ns);
459     free(new_ns);
460     break;
461   }
462
463   return std::move(type);
464 }
465
466 static void MC_dwarf_handle_type_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
467                                      Dwarf_Die * unit, simgrid::mc::Frame* frame,
468                                      const char *ns)
469 {
470   simgrid::mc::Type type = MC_dwarf_die_to_type(info, die, unit, frame, ns);
471   auto& t = (info->types[type.id] = std::move(type));
472   if (!t.name.empty() && type.byte_size != 0)
473     info->full_types_by_name[t.name] = &t;
474 }
475
476 static int mc_anonymous_variable_index = 0;
477
478 static std::unique_ptr<simgrid::mc::Variable> MC_die_to_variable(
479   simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
480   Dwarf_Die * unit, simgrid::mc::Frame* frame,
481   const char *ns)
482 {
483   // Skip declarations:
484   if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
485     return nullptr;
486
487   // Skip compile time constants:
488   if (dwarf_hasattr(die, DW_AT_const_value))
489     return nullptr;
490
491   Dwarf_Attribute attr_location;
492   if (dwarf_attr(die, DW_AT_location, &attr_location) == NULL)
493     // No location: do not add it ?
494     return nullptr;
495
496   std::unique_ptr<simgrid::mc::Variable> variable =
497     std::unique_ptr<simgrid::mc::Variable>(new simgrid::mc::Variable());
498   variable->dwarf_offset = dwarf_dieoffset(die);
499   variable->global = frame == NULL;     // Can be override base on DW_AT_location
500   variable->object_info = info;
501
502   const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
503   if (name)
504     variable->name = name;
505   variable->type_id = MC_dwarf_at_type(die);
506
507   int form = dwarf_whatform(&attr_location);
508   int klass =
509       form ==
510       DW_FORM_sec_offset ? MC_DW_CLASS_CONSTANT : MC_dwarf_form_get_class(form);
511   switch (klass) {
512   case MC_DW_CLASS_EXPRLOC:
513   case MC_DW_CLASS_BLOCK:
514     // Location expression:
515     {
516       Dwarf_Op *expr;
517       size_t len;
518       if (dwarf_getlocation(&attr_location, &expr, &len)) {
519         xbt_die(
520           "Could not read location expression in DW_AT_location "
521           "of variable <%" PRIx64 ">%s",
522           (std::uint64_t) variable->dwarf_offset,
523           variable->name.c_str());
524       }
525
526       if (len == 1 && expr[0].atom == DW_OP_addr) {
527         variable->global = 1;
528         uintptr_t offset = (uintptr_t) expr[0].number;
529         uintptr_t base = (uintptr_t) info->base_address();
530         variable->address = (void *) (base + offset);
531       } else {
532         simgrid::mc::LocationListEntry entry;
533         entry.expression = MC_dwarf_expression(expr, len);
534         variable->location_list = { std::move(entry) };
535       }
536
537       break;
538     }
539   case MC_DW_CLASS_LOCLISTPTR:
540   case MC_DW_CLASS_CONSTANT:
541     // Reference to location list:
542     mc_dwarf_location_list_init_libdw(
543       &variable->location_list, info, die,
544       &attr_location);
545     break;
546   default:
547     xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location "
548             "in <%" PRIx64 ">%s",
549             form, form, klass, klass,
550             (std::uint64_t) variable->dwarf_offset,
551             variable->name.c_str());
552   }
553
554   // Handle start_scope:
555   if (dwarf_hasattr(die, DW_AT_start_scope)) {
556     Dwarf_Attribute attr;
557     dwarf_attr(die, DW_AT_start_scope, &attr);
558     int form = dwarf_whatform(&attr);
559     int klass = MC_dwarf_form_get_class(form);
560     switch (klass) {
561     case MC_DW_CLASS_CONSTANT:
562       {
563         Dwarf_Word value;
564         variable->start_scope =
565             dwarf_formudata(&attr, &value) == 0 ? (size_t) value : 0;
566         break;
567       }
568     case MC_DW_CLASS_RANGELISTPTR:     // TODO
569     default:
570       xbt_die
571           ("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s",
572            form, klass, name == NULL ? "?" : name);
573     }
574   }
575
576   if (ns && variable->global)
577     variable->name =
578       std::string(ns) + "::" + variable->name;
579
580   // The current code needs a variable name,
581   // generate a fake one:
582   if (variable->name.empty())
583     variable->name =
584       "@anonymous#" + std::to_string(mc_anonymous_variable_index++);
585
586   return std::move(variable);
587 }
588
589 static void MC_dwarf_handle_variable_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
590                                          Dwarf_Die * unit, simgrid::mc::Frame* frame,
591                                          const char *ns)
592 {
593   std::unique_ptr<simgrid::mc::Variable> variable =
594     MC_die_to_variable(info, die, unit, frame, ns);
595   if (!variable)
596     return;
597   // Those arrays are sorted later:
598   else if (variable->global)
599     info->global_variables.push_back(std::move(*variable));
600   else if (frame != nullptr)
601     frame->variables.push_back(std::move(*variable));
602   else
603     xbt_die("No frame for this local variable");
604 }
605
606 static void MC_dwarf_handle_scope_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
607                                       Dwarf_Die * unit, simgrid::mc::Frame* parent_frame,
608                                       const char *ns)
609 {
610   // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt
611   int tag = dwarf_tag(die);
612   mc_tag_class klass = MC_dwarf_tag_classify(tag);
613
614   // (Template) Subprogram declaration:
615   if (klass == mc_tag_subprogram
616       && MC_dwarf_attr_flag(die, DW_AT_declaration, false))
617     return;
618
619   if (klass == mc_tag_scope)
620     xbt_assert(parent_frame, "No parent scope for this scope");
621
622   simgrid::mc::Frame frame;
623
624   frame.tag = tag;
625   frame.id = dwarf_dieoffset(die);
626   frame.object_info = info;
627
628   if (klass == mc_tag_subprogram) {
629     const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
630     if(ns)
631       frame.name  = std::string(ns) + "::" + name;
632     else if (name)
633       frame.name = name;
634     else
635       frame.name.clear();
636   }
637
638   frame.abstract_origin_id =
639     MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin);
640
641   // This is the base address for DWARF addresses.
642   // Relocated addresses are offset from this base address.
643   // See DWARF4 spec 7.5
644   void *base = info->base_address();
645
646   // TODO, support DW_AT_ranges
647   std::uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc);
648   frame.low_pc = low_pc ? ((char *) base) + low_pc : 0;
649   if (low_pc) {
650     // DW_AT_high_pc:
651     Dwarf_Attribute attr;
652     if (!dwarf_attr_integrate(die, DW_AT_high_pc, &attr)) {
653       xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc");
654     }
655
656     Dwarf_Sword offset;
657     Dwarf_Addr high_pc;
658
659     switch (MC_dwarf_form_get_class(dwarf_whatform(&attr))) {
660
661       // DW_AT_high_pc if an offset from the low_pc:
662     case MC_DW_CLASS_CONSTANT:
663
664       if (dwarf_formsdata(&attr, &offset) != 0)
665         xbt_die("Could not read constant");
666       frame.high_pc = (void *) ((char *) frame.low_pc + offset);
667       break;
668
669       // DW_AT_high_pc is a relocatable address:
670     case MC_DW_CLASS_ADDRESS:
671       if (dwarf_formaddr(&attr, &high_pc) != 0)
672         xbt_die("Could not read address");
673       frame.high_pc = ((char *) base) + high_pc;
674       break;
675
676     default:
677       xbt_die("Unexpected class for DW_AT_high_pc");
678
679     }
680   }
681
682   if (klass == mc_tag_subprogram) {
683     Dwarf_Attribute attr_frame_base;
684     if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base))
685       mc_dwarf_location_list_init_libdw(&frame.frame_base, info, die,
686                                   &attr_frame_base);
687   }
688
689   // Handle children:
690   MC_dwarf_handle_children(info, die, unit, &frame, ns);
691
692   // Someone needs this to be sorted but who?
693   std::sort(frame.variables.begin(), frame.variables.end(),
694     MC_compare_variable);
695
696   // Register it:
697   if (klass == mc_tag_subprogram)
698     info->subprograms[frame.id] = frame;
699   else if (klass == mc_tag_scope)
700     parent_frame->scopes.push_back(std::move(frame));
701 }
702
703 static void mc_dwarf_handle_namespace_die(simgrid::mc::ObjectInformation* info,
704                                           Dwarf_Die * die, Dwarf_Die * unit,
705                                           simgrid::mc::Frame* frame,
706                                           const char *ns)
707 {
708   const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name);
709   if (frame)
710     xbt_die("Unexpected namespace in a subprogram");
711   char *new_ns = ns == NULL ? xbt_strdup(name)
712       : bprintf("%s::%s", ns, name);
713   MC_dwarf_handle_children(info, die, unit, frame, new_ns);
714   xbt_free(new_ns);
715 }
716
717 static void MC_dwarf_handle_children(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
718                                      Dwarf_Die * unit, simgrid::mc::Frame* frame,
719                                      const char *ns)
720 {
721   // For each child DIE:
722   Dwarf_Die child;
723   int res;
724   for (res = dwarf_child(die, &child); res == 0;
725        res = dwarf_siblingof(&child, &child)) {
726     MC_dwarf_handle_die(info, &child, unit, frame, ns);
727   }
728 }
729
730 static void MC_dwarf_handle_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die,
731                                 Dwarf_Die * unit, simgrid::mc::Frame* frame,
732                                 const char *ns)
733 {
734   int tag = dwarf_tag(die);
735   mc_tag_class klass = MC_dwarf_tag_classify(tag);
736   switch (klass) {
737
738     // Type:
739   case mc_tag_type:
740     MC_dwarf_handle_type_die(info, die, unit, frame, ns);
741     break;
742
743     // Subprogram or scope:
744   case mc_tag_subprogram:
745   case mc_tag_scope:
746     MC_dwarf_handle_scope_die(info, die, unit, frame, ns);
747     return;
748
749     // Variable:
750   case mc_tag_variable:
751     MC_dwarf_handle_variable_die(info, die, unit, frame, ns);
752     break;
753
754   case mc_tag_namespace:
755     mc_dwarf_handle_namespace_die(info, die, unit, frame, ns);
756     break;
757
758   default:
759     break;
760
761   }
762 }
763
764 /** \brief Populate the debugging informations of the given ELF object
765  *
766  *  Read the DWARf information of the EFFL object and populate the
767  *  lists of types, variables, functions.
768  */
769 void MC_dwarf_get_variables(simgrid::mc::ObjectInformation* info)
770 {
771   int fd = open(info->file_name.c_str(), O_RDONLY);
772   if (fd < 0)
773     xbt_die("Could not open file %s", info->file_name.c_str());
774   Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
775   if (dwarf == NULL)
776     xbt_die("Missing debugging information in %s\n"
777       "Your program and its dependencies must have debugging information.\n"
778       "You might want to recompile with -g or install the suitable debugging package.\n",
779       info->file_name.c_str());
780   // For each compilation unit:
781   Dwarf_Off offset = 0;
782   Dwarf_Off next_offset = 0;
783   size_t length;
784
785   while (dwarf_nextcu(dwarf, offset, &next_offset, &length, NULL, NULL, NULL) ==
786          0) {
787     Dwarf_Die unit_die;
788     if (dwarf_offdie(dwarf, offset + length, &unit_die) != NULL)
789       MC_dwarf_handle_children(info, &unit_die, &unit_die, NULL, NULL);
790     offset = next_offset;
791   }
792
793   dwarf_end(dwarf);
794   close(fd);
795 }