Logo AND Algorithmique Numérique Distribuée

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