Logo AND Algorithmique Numérique Distribuée

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