Logo AND Algorithmique Numérique Distribuée

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