Logo AND Algorithmique Numérique Distribuée

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