Logo AND Algorithmique Numérique Distribuée

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