Logo AND Algorithmique Numérique Distribuée

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