Logo AND Algorithmique Numérique Distribuée

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