Logo AND Algorithmique Numérique Distribuée

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