Logo AND Algorithmique Numérique Distribuée

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