Logo AND Algorithmique Numérique Distribuée

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