Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Have a more complete/accurate view of an given object mapping in memory
[simgrid.git] / src / mc / mc_dwarf.c
1 /* Copyright (c) 2008-2013. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdlib.h>
8 #include <dwarf.h>
9 #include <elfutils/libdw.h>
10 #include <inttypes.h>
11
12 #include <simgrid_config.h>
13 #include <xbt/log.h>
14 #include <xbt/sysdep.h>
15
16 #include "mc_private.h"
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_dwarf, mc, "DWARF processing");
19
20 /** \brief The default DW_TAG_lower_bound for a given DW_AT_language.
21  *
22  *  The default for a given language is defined in the DWARF spec.
23  *
24  *  \param language consant as defined by the DWARf spec
25  */
26 static uint64_t MC_dwarf_default_lower_bound(int lang);
27
28 /** \brief Computes the the element_count of a DW_TAG_enumeration_type DIE
29  *
30  * This is the number of elements in a given array dimension.
31  *
32  * A reference of the compilation unit (DW_TAG_compile_unit) is
33  * needed because the default lower bound (when there is no DW_AT_lower_bound)
34  * depends of the language of the compilation unit (DW_AT_language).
35  *
36  * \param die  DIE for the DW_TAG_enumeration_type or DW_TAG_subrange_type
37  * \param unit DIE of the DW_TAG_compile_unit
38  */
39 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit);
40
41 /** \brief Computes the number of elements of a given DW_TAG_array_type.
42  *
43  * \param die DIE for the DW_TAG_array_type
44  */
45 static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit);
46
47 /** \brief Process a DIE
48  *
49  *  \param info the resulting object fot the library/binary file (output)
50  *  \param die  the current DIE
51  *  \param unit the DIE of the compile unit of the current DIE
52  *  \param frame containg frame if any
53  */
54 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame);
55
56 /** \brief Process a type DIE
57  */
58 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit);
59
60 /** \brief Calls MC_dwarf_handle_die on all childrend of the given die
61  *
62  *  \param info the resulting object fot the library/binary file (output)
63  *  \param die  the current DIE
64  *  \param unit the DIE of the compile unit of the current DIE
65  *  \param frame containg frame if any
66  */
67 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame);
68
69 /** \brief Handle a variable (DW_TAG_variable or other)
70  *
71  *  \param info the resulting object fot the library/binary file (output)
72  *  \param die  the current DIE
73  *  \param unit the DIE of the compile unit of the current DIE
74  *  \param frame containg frame if any
75  */
76 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame);
77
78 /** \brief Convert a libdw DWARF expression into a MC representation of the location
79  *
80  *  \param expr array of DWARf operations
81  *  \param len  number of elements
82  *  \return a new MC expression
83  */
84 static dw_location_t MC_dwarf_get_expression(Dwarf_Op* expr,  size_t len);
85
86 /** \brief Get the DW_TAG_type of the DIE
87  *
88  *  \param die DIE
89  *  \return DW_TAG_type attribute as a new string (NULL if none)
90  */
91 static char* MC_dwarf_at_type(Dwarf_Die* die);
92
93 /** \brief Get the name of an attribute (DW_AT_*) from its code
94  *
95  *  \param attr attribute code (see the DWARF specification)
96  *  \return name of the attribute
97  */
98 const char* MC_dwarf_attrname(int attr) {
99   switch (attr) {
100 #include "mc_dwarf_attrnames.h"
101   default:
102     return "DW_AT_unkown";
103   }
104 }
105
106 /** \brief Get the name of a dwarf tag (DW_TAG_*) from its code
107  *
108  *  \param tag tag code (see the DWARF specification)
109  *  \return name of the tag
110  */
111 const char* MC_dwarf_tagname(int tag) {
112   switch (tag) {
113 #include "mc_dwarf_tagnames.h"
114   case DW_TAG_invalid:
115     return "DW_TAG_invalid";
116   default:
117     return "DW_TAG_unkown";
118   }
119 }
120
121 #define MC_DW_CLASS_UNKNOWN 0
122 #define MC_DW_CLASS_ADDRESS 1   // Location in the address space of the program
123 #define MC_DW_CLASS_BLOCK 2     // Arbitrary block of bytes
124 #define MC_DW_CLASS_CONSTANT 3
125 #define MC_DW_CLASS_STRING 3    // String
126 #define MC_DW_CLASS_FLAG 4      // Boolean
127 #define MC_DW_CLASS_REFERENCE 5 // Reference to another DIE
128 #define MC_DW_CLASS_EXPRLOC 6   // DWARF expression/location description
129 #define MC_DW_CLASS_LINEPTR 7
130 #define MC_DW_CLASS_LOCLISTPTR 8
131 #define MC_DW_CLASS_MACPTR 9
132 #define MC_DW_CLASS_RANGELISTPTR 10
133
134 static int MC_dwarf_form_get_class(int form) {
135   switch(form) {
136   case DW_FORM_addr:
137     return MC_DW_CLASS_ADDRESS;
138   case DW_FORM_block2:
139   case DW_FORM_block4:
140   case DW_FORM_block:
141   case DW_FORM_block1:
142     return MC_DW_CLASS_BLOCK;
143   case DW_FORM_data2:
144   case DW_FORM_data4:
145   case DW_FORM_data8:
146   case DW_FORM_udata:
147   case DW_FORM_sdata:
148     return MC_DW_CLASS_CONSTANT;
149   case DW_FORM_string:
150   case DW_FORM_strp:
151     return MC_DW_CLASS_STRING;
152   case DW_FORM_ref_addr:
153   case DW_FORM_ref1:
154   case DW_FORM_ref2:
155   case DW_FORM_ref4:
156   case DW_FORM_ref8:
157   case DW_FORM_ref_udata:
158     return MC_DW_CLASS_REFERENCE;
159   case DW_FORM_flag:
160   case DW_FORM_flag_present:
161     return MC_DW_CLASS_FLAG;
162   case DW_FORM_exprloc:
163     return MC_DW_CLASS_EXPRLOC;
164   // TODO sec offset
165   // TODO indirect
166   default:
167     return MC_DW_CLASS_UNKNOWN;
168   }
169 }
170
171 /** \brief Get the name of the tag of a given DIE
172  *
173  *  \param die DIE
174  *  \return name of the tag of this DIE
175  */
176 static inline const char* MC_dwarf_die_tagname(Dwarf_Die* die) {
177   return MC_dwarf_tagname(dwarf_tag(die));
178 }
179
180 // ***** Attributes
181
182 /** \brief Get an attribute of a given DIE as a string
183  *
184  *  \param the DIE
185  *  \param attribute attribute
186  *  \return value of the given attribute of the given DIE
187  */
188 static const char* MC_dwarf_attr_string(Dwarf_Die* die, int attribute) {
189   Dwarf_Attribute attr;
190   if (!dwarf_attr_integrate(die, attribute, &attr)) {
191         return NULL;
192   } else {
193         return dwarf_formstring(&attr);
194   }
195 }
196
197 /** \brief Get the linkage name of a DIE.
198  *
199  *  Use either DW_AT_linkage_name or DW_AR_MIPS_linkage_name.
200  *
201  *  \param DIE
202  *  \return linkage name of the given DIE (or NULL)
203  * */
204 static const char* MC_dwarf_at_linkage_name(Dwarf_Die* die) {
205   const char* name = MC_dwarf_attr_string(die, DW_AT_linkage_name);
206   if (!name)
207     name = MC_dwarf_attr_string(die, DW_AT_MIPS_linkage_name);
208   return name;
209 }
210
211 /** \brief Create a location list from a given attribute
212  *
213  *  \param die the DIE
214  *  \param attr the attribute
215  *  \return MC specific representation of the location list represented by the given attribute
216  *  of the given die
217  */
218 static dw_location_t MC_dwarf_get_location_list(Dwarf_Die* die, Dwarf_Attribute* attr) {
219
220   dw_location_t location = xbt_new0(s_dw_location_t, 1);
221   location->type = e_dw_loclist;
222   xbt_dynar_t loclist = xbt_dynar_new(sizeof(dw_location_entry_t), NULL);
223   location->location.loclist = loclist;
224
225   ptrdiff_t offset = 0;
226   Dwarf_Addr base, start, end;
227   Dwarf_Op *expr;
228   size_t len;
229
230   while (1) {
231
232     offset = dwarf_getlocations(attr, offset, &base, &start, &end, &expr, &len);
233     if (offset==0)
234       return location;
235     else if (offset==-1)
236       xbt_die("Error while loading location list");
237
238     dw_location_entry_t new_entry = xbt_new0(s_dw_location_entry_t, 1);
239     new_entry->lowpc = start;
240     new_entry->highpc = end;
241     new_entry->location = MC_dwarf_get_expression(expr, len);
242
243     xbt_dynar_push(loclist, &new_entry);
244
245   }
246 }
247
248 /** \brief Get the location expression or location list from an attribute
249  *
250  *  Processes direct expressions as well as location lists.
251  *
252  *  \param die the DIE
253  *  \param attr the attribute
254  *  \return MC specific representation of the location represented by the given attribute
255  *  of the given die
256  */
257 static dw_location_t MC_dwarf_get_location(Dwarf_Die* die, Dwarf_Attribute* attr) {
258   int form = dwarf_whatform(attr);
259   switch (form) {
260
261   // The attribute is an DWARF location expression:
262   case DW_FORM_exprloc:
263   case DW_FORM_block1: // not in the spec
264   case DW_FORM_block2:
265   case DW_FORM_block4:
266   case DW_FORM_block:
267     {
268       Dwarf_Op* expr;
269       size_t len;
270       if (dwarf_getlocation(attr, &expr, &len))
271         xbt_die("Could not read location expression");
272       return MC_dwarf_get_expression(expr, len);
273     }
274
275   // The attribute is a reference to a location list entry:
276   case DW_FORM_sec_offset:
277   case DW_FORM_data2:
278   case DW_FORM_data4:
279   case DW_FORM_data8:
280     {
281       return MC_dwarf_get_location_list(die, attr);
282     }
283     break;
284
285   default:
286     xbt_die("Unexpected form %i list for location in attribute %s of <%p>%s",
287       form,
288       MC_dwarf_attrname(attr->code),
289       (void*) dwarf_dieoffset(die),
290       MC_dwarf_attr_string(die, DW_AT_name));
291     return NULL;
292   }
293 }
294
295 /** \brief Get the location expression or location list from an attribute
296  *
297  *  Processes direct expressions as well as location lists.
298  *
299  *  \param die the DIE
300  *  \param attribute the attribute code
301  *  \return MC specific representation of the location represented by the given attribute
302  *  of the given die
303  */
304 static dw_location_t MC_dwarf_at_location(Dwarf_Die* die, int attribute) {
305   if(!dwarf_hasattr_integrate(die, attribute))
306     return xbt_new0(s_dw_location_t, 1);
307
308   Dwarf_Attribute attr;
309   dwarf_attr_integrate(die, attribute, &attr);
310   return MC_dwarf_get_location(die, &attr);
311 }
312
313 static char* MC_dwarf_at_type(Dwarf_Die* die) {
314   Dwarf_Attribute attr;
315   if (dwarf_hasattr_integrate(die, DW_AT_type)) {
316         dwarf_attr_integrate(die, DW_AT_type, &attr);
317         Dwarf_Die subtype_die;
318         if (dwarf_formref_die(&attr, &subtype_die)==NULL) {
319           xbt_die("Could not find DIE for type");
320         }
321         Dwarf_Off subtype_global_offset = dwarf_dieoffset(&subtype_die);
322     return bprintf("%" PRIx64 , subtype_global_offset);
323   }
324   else return NULL;
325 }
326
327 static uint64_t MC_dwarf_attr_addr(Dwarf_Die* die, int attribute) {
328   Dwarf_Attribute attr;
329   if(dwarf_attr_integrate(die, attribute, &attr)==NULL)
330     return 0;
331   Dwarf_Addr value;
332   if (dwarf_formaddr(&attr, &value) == 0)
333     return (uint64_t) value;
334   else
335     return 0;
336 }
337
338 static uint64_t MC_dwarf_attr_uint(Dwarf_Die* die, int attribute, uint64_t default_value) {
339   Dwarf_Attribute attr;
340   if (dwarf_attr_integrate(die, attribute, &attr)==NULL)
341     return default_value;
342   Dwarf_Word value;
343   return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr), &value) == 0 ? (uint64_t) value : default_value;
344 }
345
346 static bool MC_dwarf_attr_flag(Dwarf_Die* die, int attribute, int integrate) {
347   Dwarf_Attribute attr;
348   if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
349                     : dwarf_attr(die, attribute, &attr))==0)
350     return false;
351
352   bool result;
353   if (dwarf_formflag(&attr, &result))
354     xbt_die("Unexpected form for attribute %s",
355       MC_dwarf_attrname(attribute));
356   return result;
357 }
358
359 static uint64_t MC_dwarf_default_lower_bound(int lang) {
360   switch(lang) {
361   case DW_LANG_C:
362   case DW_LANG_C89:
363   case DW_LANG_C99:
364   case DW_LANG_C_plus_plus:
365   case DW_LANG_D:
366   case DW_LANG_Java:
367   case DW_LANG_ObjC:
368   case DW_LANG_ObjC_plus_plus:
369   case DW_LANG_Python:
370   case DW_LANG_UPC:
371     return 0;
372   case DW_LANG_Ada83:
373   case DW_LANG_Ada95:
374   case DW_LANG_Fortran77:
375   case DW_LANG_Fortran90:
376   case DW_LANG_Fortran95:
377   case DW_LANG_Modula2:
378   case DW_LANG_Pascal83:
379   case DW_LANG_PL1:
380   case DW_LANG_Cobol74:
381   case DW_LANG_Cobol85:
382     return 1;
383   default:
384     xbt_die("No default MT_TAG_lower_bound for language %i and none given", lang);
385     return 0;
386   }
387 }
388
389 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
390   xbt_assert(dwarf_tag(die)==DW_TAG_enumeration_type ||dwarf_tag(die)==DW_TAG_subrange_type,
391       "MC_dwarf_subrange_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
392
393   // Use DW_TAG_count if present:
394   if (dwarf_hasattr_integrate(die, DW_AT_count)) {
395     return MC_dwarf_attr_uint(die, DW_AT_count, 0);
396   }
397
398   // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound + 1:
399
400   if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
401         // This is not really 0, but the code expects this (we do not know):
402     return 0;
403   }
404   uint64_t upper_bound = MC_dwarf_attr_uint(die, DW_AT_upper_bound, -1);
405
406   uint64_t lower_bound = 0;
407   if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
408     lower_bound = MC_dwarf_attr_uint(die, DW_AT_lower_bound, -1);
409   } else {
410         lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
411   }
412   return upper_bound - lower_bound + 1;
413 }
414
415 static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
416   xbt_assert(dwarf_tag(die)==DW_TAG_array_type,
417     "MC_dwarf_array_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
418
419   int result = 1;
420   Dwarf_Die child;
421   int res;
422   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
423         int child_tag = dwarf_tag(&child);
424     if (child_tag==DW_TAG_subrange_type ||child_tag==DW_TAG_enumeration_type) {
425       result *= MC_dwarf_subrange_element_count(&child, unit);
426     }
427   }
428   return result;
429 }
430
431 // ***** Location
432
433 Dwarf_Off MC_dwarf_resolve_location(unw_cursor_t* c, dw_location_t location, void* frame_pointer_address) {
434   unw_word_t res;
435   switch (location->type){
436   case e_dw_compose:
437     if (xbt_dynar_length(location->location.compose) > 1){
438       return 0; /* TODO : location list with optimizations enabled */
439     }
440     dw_location_t location_entry = xbt_dynar_get_as(location->location.compose, 0, dw_location_t);
441     switch (location_entry->type){
442     case e_dw_register:
443       unw_get_reg(c, location_entry->location.reg, &res);
444       return res;
445     case e_dw_bregister_op:
446       unw_get_reg(c, location_entry->location.breg_op.reg, &res);
447       return (Dwarf_Off) ((long)res + location_entry->location.breg_op.offset);
448       break;
449     case e_dw_fbregister_op:
450       if (frame_pointer_address != NULL)
451         return (Dwarf_Off)((char *)frame_pointer_address + location_entry->location.fbreg_op);
452       else
453         return 0;
454     default:
455       return 0; /* FIXME : implement other cases (with optimizations enabled) */
456     }
457     break;
458     default:
459       return 0;
460   }
461 }
462
463 // ***** dw_type_t
464
465 static void MC_dwarf_fill_member_location(dw_type_t type, dw_type_t member, Dwarf_Die* child) {
466   if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
467     xbt_die("Can't groke DW_AT_data_bit_offset.");
468   }
469
470   if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
471     if (type->type != DW_TAG_union_type) {
472         xbt_die(
473           "Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%p>%s",
474           member->name, type->id, type->name);
475     } else {
476       return;
477     }
478   }
479
480   Dwarf_Attribute attr;
481   dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
482   int klass = MC_dwarf_form_get_class(dwarf_whatform(&attr));
483   switch (klass) {
484   case MC_DW_CLASS_EXPRLOC:
485   case MC_DW_CLASS_BLOCK:
486     // Location expression:
487     {
488       Dwarf_Op* expr;
489       size_t len;
490       if (dwarf_getlocation(&attr, &expr, &len)) {
491         xbt_die(
492           "Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%p>%s",
493           MC_dwarf_attr_string(child, DW_AT_name),
494           type->id, type->name);
495       }
496       if (len==1 && expr[0].atom == DW_OP_plus_uconst) {
497         member->offset =  expr[0].number;
498       } else {
499         xbt_die("Can't groke this location expression yet.");
500       }
501       break;
502     }
503   case MC_DW_CLASS_CONSTANT:
504     // Offset from the base address of the object:
505     {
506       Dwarf_Word offset;
507       if (!dwarf_formudata(&attr, &offset))
508         member->offset = offset;
509       else
510         xbt_die("Cannot get %s location <%p>%s",
511           MC_dwarf_attr_string(child, DW_AT_name),
512           type->id, type->name);
513       break;
514     }
515   case MC_DW_CLASS_LOCLISTPTR:
516     // Reference to a location list:
517     // TODO
518   case MC_DW_CLASS_REFERENCE:
519     // It's supposed to be possible in DWARF2 but I couldn't find its semantic
520     // in the spec.
521   default:
522     xbt_die("Can't handle form class 0x%x (%i) as DW_AT_member_location", klass, klass);
523   }
524
525 }
526
527 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_type_t type) {
528   int res;
529   Dwarf_Die child;
530   xbt_assert(!type->members);
531   type->members = xbt_dynar_new(sizeof(dw_type_t), (void(*)(void*))dw_type_free);
532   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
533     if (dwarf_tag(&child)==DW_TAG_member) {
534       // TODO, we should use another type (because is is not a type but a member)
535       dw_type_t member = xbt_new0(s_dw_type_t, 1);
536       member->type = -1;
537       member->id = NULL;
538
539       const char* name = MC_dwarf_attr_string(&child, DW_AT_name);
540       if(name)
541         member->name = xbt_strdup(name);
542       else
543         member->name = NULL;
544
545       member->byte_size = MC_dwarf_attr_uint(&child, DW_AT_byte_size, 0);
546       member->element_count = -1;
547       member->dw_type_id = MC_dwarf_at_type(&child);
548       member->members = NULL;
549       member->is_pointer_type = 0;
550       member->offset = 0;
551
552       if(dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
553         xbt_die("Can't groke DW_AT_data_bit_offset.");
554       }
555
556       MC_dwarf_fill_member_location(type, member, &child);
557
558       if (!member->dw_type_id) {
559         xbt_die("Missing type for member %s of <%p>%s", member->name, type->id, type->name);
560       }
561
562       xbt_dynar_push(type->members, &member);
563     }
564   }
565 }
566
567 /** \brief Create a MC type object from a DIE
568  *
569  *  \param info current object info object
570  *  \param DIE (for a given type);
571  *  \param unit compilation unit of the current DIE
572  *  \return MC representation of the type
573  */
574 static dw_type_t MC_dwarf_die_to_type(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit) {
575
576   dw_type_t type = xbt_new0(s_dw_type_t, 1);
577   type->type = -1;
578   type->id = NULL;
579   type->name = NULL;
580   type->byte_size = 0;
581   type->element_count = -1;
582   type->dw_type_id = NULL;
583   type->members = NULL;
584   type->is_pointer_type = 0;
585   type->offset = 0;
586
587   type->type = dwarf_tag(die);
588
589   // Global Offset
590   type->id = (void *) dwarf_dieoffset(die);
591
592   const char* name = MC_dwarf_attr_string(die, DW_AT_name);
593   if (name!=NULL) {
594         type->name = xbt_strdup(name);
595   }
596
597   XBT_DEBUG("Processing type <%p>%s", type->id, type->name);
598
599   type->dw_type_id = MC_dwarf_at_type(die);
600
601   // Computation of the byte_size;
602   if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
603     type->byte_size = MC_dwarf_attr_uint(die, DW_AT_byte_size, 0);
604   else if (type->type == DW_TAG_array_type || type->type==DW_TAG_structure_type || type->type==DW_TAG_class_type) {
605     Dwarf_Word size;
606     if (dwarf_aggregate_size(die, &size)==0) {
607       type->byte_size = size;
608     }
609   }
610
611   switch (type->type) {
612   case DW_TAG_array_type:
613         type->element_count = MC_dwarf_array_element_count(die, unit);
614         // TODO, handle DW_byte_stride and (not) DW_bit_stride
615         break;
616
617   case DW_TAG_pointer_type:
618   case DW_TAG_reference_type:
619   case DW_TAG_rvalue_reference_type:
620     type->is_pointer_type = 1;
621     break;
622
623   case DW_TAG_structure_type:
624   case DW_TAG_union_type:
625   case DW_TAG_class_type:
626           MC_dwarf_add_members(info, die, unit, type);
627   }
628
629   return type;
630 }
631
632 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit) {
633   dw_type_t type = MC_dwarf_die_to_type(info, die, unit);
634
635   char* key = bprintf("%" PRIx64, (uint64_t) type->id);
636   xbt_dict_set(info->types, key, type, NULL);
637 }
638
639 /** \brief Convert libdw location expresion elment into native one (or NULL in some cases) */
640 static dw_location_t MC_dwarf_get_expression_element(Dwarf_Op* op) {
641   dw_location_t element = xbt_new0(s_dw_location_t, 1);
642   uint8_t atom = op->atom;
643   if (atom >= DW_OP_reg0 && atom<= DW_OP_reg31) {
644     element->type = e_dw_register;
645     element->location.reg = atom - DW_OP_reg0;
646   }
647   else if (atom >= DW_OP_breg0 && atom<= DW_OP_breg31) {
648     element->type = e_dw_bregister_op;
649     element->location.reg = atom - DW_OP_breg0;
650     element->location.breg_op.offset = op->number;
651   }
652   else if (atom >= DW_OP_lit0 && atom<= DW_OP_lit31) {
653     element->type = e_dw_lit;
654     element->location.reg = atom - DW_OP_lit0;
655   }
656   else switch (atom) {
657   case DW_OP_fbreg:
658     element->type = e_dw_fbregister_op;
659     element->location.fbreg_op = op->number;
660     break;
661   case DW_OP_piece:
662     element->type = e_dw_piece;
663     element->location.piece = op->number;
664     break;
665   case DW_OP_plus_uconst:
666     element->type = e_dw_plus_uconst;
667     element->location.plus_uconst = op->number;
668     break;
669   case DW_OP_abs:
670     element->type = e_dw_arithmetic;
671     element->location.arithmetic = xbt_strdup("abs");
672     break;
673   case DW_OP_and:
674     element->type = e_dw_arithmetic;
675     element->location.arithmetic = xbt_strdup("and");
676     break;
677   case DW_OP_div:
678     element->type = e_dw_arithmetic;
679     element->location.arithmetic = xbt_strdup("div");
680     break;
681   case DW_OP_minus:
682     element->type = e_dw_arithmetic;
683     element->location.arithmetic = xbt_strdup("minus");
684     break;
685   case DW_OP_mod:
686     element->type = e_dw_arithmetic;
687     element->location.arithmetic = xbt_strdup("mod");
688     break;
689   case DW_OP_mul:
690     element->type = e_dw_arithmetic;
691     element->location.arithmetic = xbt_strdup("mul");
692     break;
693   case DW_OP_neg:
694     element->type = e_dw_arithmetic;
695     element->location.arithmetic = xbt_strdup("neg");
696     break;
697   case DW_OP_not:
698     element->type = e_dw_arithmetic;
699     element->location.arithmetic = xbt_strdup("not");
700     break;
701   case DW_OP_or:
702     element->type = e_dw_arithmetic;
703     element->location.arithmetic = xbt_strdup("or");
704     break;
705   case DW_OP_plus:
706     element->type = e_dw_arithmetic;
707     element->location.arithmetic = xbt_strdup("plus");
708     break;
709
710   case DW_OP_stack_value:
711     // Why nothing here?
712     xbt_free(element);
713     return NULL;
714
715   case DW_OP_deref_size:
716     element->type = e_dw_deref;
717     element->location.deref_size =  (unsigned int short) op->number;
718     break;
719   case DW_OP_deref:
720     element->type = e_dw_deref;
721     element->location.deref_size = sizeof(void *);
722     break;
723   case DW_OP_constu:
724     element->type = e_dw_uconstant;
725     element->location.uconstant.bytes = 1;
726     element->location.uconstant.value = (unsigned long int) op->number;
727     break;
728   case DW_OP_consts:
729     element->type = e_dw_sconstant;
730     element->location.uconstant.bytes = 1;
731     element->location.uconstant.value = (unsigned long int) op->number;
732     break;
733
734   case DW_OP_const1u:
735     element->type = e_dw_uconstant;
736     element->location.uconstant.bytes = 1;
737     element->location.uconstant.value = (unsigned long int) op->number;
738     break;
739   case DW_OP_const2u:
740     element->type = e_dw_uconstant;
741     element->location.uconstant.bytes = 2;
742     element->location.uconstant.value = (unsigned long int) op->number;
743     break;
744   case DW_OP_const4u:
745     element->type = e_dw_uconstant;
746     element->location.uconstant.bytes = 4;
747     element->location.uconstant.value = (unsigned long int) op->number;
748     break;
749   case DW_OP_const8u:
750     element->type = e_dw_uconstant;
751     element->location.uconstant.bytes = 8;
752     element->location.uconstant.value = (unsigned long int) op->number;
753     break;
754
755   case DW_OP_const1s:
756     element->type = e_dw_sconstant;
757     element->location.uconstant.bytes = 1;
758     element->location.uconstant.value = (unsigned long int) op->number;
759     break;
760   case DW_OP_const2s:
761     element->type = e_dw_sconstant;
762     element->location.uconstant.bytes = 2;
763     element->location.uconstant.value = (unsigned long int) op->number;
764     break;
765   case DW_OP_const4s:
766     element->type = e_dw_sconstant;
767     element->location.uconstant.bytes = 4;
768     element->location.uconstant.value = (unsigned long int) op->number;
769     break;
770   case DW_OP_const8s:
771     element->type = e_dw_sconstant;
772     element->location.uconstant.bytes = 8;
773     element->location.uconstant.value = (unsigned long int) op->number;
774     break;
775   default:
776     element->type = e_dw_unsupported;
777     break;
778   }
779   return element;
780 }
781
782 /** \brief Convert libdw location expresion into native one */
783 static dw_location_t MC_dwarf_get_expression(Dwarf_Op* expr,  size_t len) {
784   dw_location_t loc = xbt_new0(s_dw_location_t, 1);
785   loc->type = e_dw_compose;
786   loc->location.compose = xbt_dynar_new(sizeof(dw_location_t), NULL);
787
788   int i;
789   for (i=0; i!=len; ++i) {
790     dw_location_t element =  MC_dwarf_get_expression_element(expr+i);
791     if (element)
792       xbt_dynar_push(loc->location.compose, &element);
793   }
794
795   return loc;
796 }
797
798 static dw_variable_t MC_die_to_variable(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame) {
799   // Drop declaration:
800   if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
801     return NULL;
802
803   Dwarf_Attribute attr_location;
804   if (dwarf_attr(die, DW_AT_location, &attr_location)==NULL) {
805     // No location: do not add it ?
806     return NULL;
807   }
808
809   dw_variable_t variable = xbt_new0(s_dw_variable_t, 1);
810   variable->dwarf_offset = dwarf_dieoffset(die);
811   variable->global = frame == NULL; // Can be override base on DW_AT_location
812   variable->name = xbt_strdup(MC_dwarf_attr_string(die, DW_AT_name));
813   variable->type_origin = MC_dwarf_at_type(die);
814   variable->address.address = NULL;
815
816   int klass = MC_dwarf_form_get_class(dwarf_whatform(&attr_location));
817   switch (klass) {
818   case MC_DW_CLASS_EXPRLOC:
819   case MC_DW_CLASS_BLOCK:
820     // Location expression:
821     {
822       Dwarf_Op* expr;
823       size_t len;
824       if (dwarf_getlocation(&attr_location, &expr, &len)) {
825         xbt_die(
826           "Could not read location expression in DW_AT_location of variable <%p>%s",
827           (void*) variable->dwarf_offset, variable->name);
828       }
829
830       if (len==1 && expr[0].atom == DW_OP_addr) {
831         variable->global = 1;
832         Dwarf_Off offset = expr[0].number;
833         // TODO, Why is this different base on the object?
834         Dwarf_Off base = strcmp(info->file_name, xbt_binary_name) !=0 ? (Dwarf_Off) info->start_exec : 0;
835         variable->address.address = (void*) (base + offset);
836       } else {
837         variable->address.location = MC_dwarf_get_expression(expr, len);
838       }
839
840       break;
841     }
842   case MC_DW_CLASS_LOCLISTPTR:
843   case MC_DW_CLASS_CONSTANT:
844     // Reference to location list:
845     variable->address.location = MC_dwarf_get_location_list(die, &attr_location);
846     break;
847   default:
848     xbt_die("Unexpected calss 0x%x (%i) list for location in <%p>%s",
849       klass, klass, (void*) variable->dwarf_offset, variable->name);
850   }
851
852   return variable;
853 }
854
855 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame) {
856   dw_variable_t variable = MC_die_to_variable(info, die, unit, frame);
857   if(variable==NULL)
858       return;
859   MC_dwarf_register_variable(info, frame, variable);
860 }
861
862 static void MC_dwarf_handle_subprogram_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t parent_frame) {
863   dw_frame_t frame = xbt_new0(s_dw_frame_t, 1);
864
865   frame->start = dwarf_dieoffset(die);
866
867   const char* name = MC_dwarf_at_linkage_name(die);
868   if (name==NULL)
869     name = MC_dwarf_attr_string(die, DW_AT_name);
870   frame->name = xbt_strdup(name);
871
872   // Variables are filled in the (recursive) call of MC_dwarf_handle_children:
873   frame->variables = xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
874   frame->high_pc = (void*) MC_dwarf_attr_addr(die, DW_AT_high_pc);
875   frame->low_pc = (void*) MC_dwarf_attr_addr(die, DW_AT_low_pc);
876   frame->frame_base = MC_dwarf_at_location(die, DW_AT_frame_base);
877   frame->end = -1; // This one is now useless:
878
879   // Handle children:
880   MC_dwarf_handle_children(info, die, unit, frame);
881
882   // Register it:
883   xbt_dict_set(info->local_variables, frame->name, frame, NULL);
884 }
885
886 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame) {
887   Dwarf_Die child;
888   int res;
889   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
890     MC_dwarf_handle_die(info, &child, unit, frame);
891   }
892 }
893
894 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame) {
895   int tag = dwarf_tag(die);
896   switch (tag) {
897     case DW_TAG_array_type:
898     case DW_TAG_class_type:
899     case DW_TAG_enumeration_type:
900     case DW_TAG_typedef:
901     case DW_TAG_pointer_type:
902     case DW_TAG_string_type:
903     case DW_TAG_structure_type:
904     case DW_TAG_subroutine_type:
905     case DW_TAG_union_type:
906     case DW_TAG_ptr_to_member_type:
907     case DW_TAG_set_type:
908     case DW_TAG_subrange_type:
909     case DW_TAG_base_type:
910     case DW_TAG_const_type:
911     case DW_TAG_file_type:
912     case DW_TAG_packed_type:
913     case DW_TAG_volatile_type:
914     case DW_TAG_restrict_type:
915     case DW_TAG_interface_type:
916     case DW_TAG_unspecified_type:
917     case DW_TAG_mutable_type:
918     case DW_TAG_shared_type:
919       MC_dwarf_handle_type_die(info, die, unit);
920       break;
921     case DW_TAG_inlined_subroutine:
922     case DW_TAG_subprogram:
923       MC_dwarf_handle_subprogram_die(info, die, unit, frame);
924       return;
925     // case DW_TAG_formal_parameter:
926     case DW_TAG_variable:
927     case DW_TAG_formal_parameter:
928       MC_dwarf_handle_variable_die(info, die, unit, frame);
929       break;
930   }
931
932   // Recursive processing of children DIE:
933   MC_dwarf_handle_children(info, die, unit, frame);
934 }
935
936 void MC_dwarf_get_variables(mc_object_info_t info) {
937   int fd = open(info->file_name, O_RDONLY);
938   if (fd<0) {
939     xbt_die("Could not open file %s", info->file_name);
940   }
941   Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
942   if (dwarf==NULL) {
943     xbt_die("Your program must be compiled with -g");
944   }
945
946   Dwarf_Off offset = 0;
947   Dwarf_Off next_offset = 0;
948   size_t length;
949   while (dwarf_nextcu (dwarf, offset, &next_offset, &length, NULL, NULL, NULL) == 0) {
950     Dwarf_Die die;
951     if(dwarf_offdie(dwarf, offset+length, &die)!=NULL) {
952       MC_dwarf_handle_die(info, &die, &die, NULL);
953     }
954     offset = next_offset;
955   }
956
957   dwarf_end(dwarf);
958   close(fd);
959 }