Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Use libdw for global variables
[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 static uint64_t MC_dwarf_default_lower_bound(int lang);
25
26 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit);
27
28 /** \brief Computes the number of elements of a given DW_TAG_array_type
29  *
30  */
31 static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit);
32
33 /** \brief Checks if a given tag is a (known) type tag.
34  */
35 static int MC_dwarf_tag_type(int tag);
36 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit);
37 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit);
38 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit);
39 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit);
40 static Dwarf_Die* MC_dwarf_resolve_die(Dwarf_Die* die, int attribute);
41
42 const char* MC_dwarf_attrname(int attr) {
43   switch (attr) {
44 #include "mc_dwarf_attrnames.h"
45   default:
46     return "DW_AT_unkown";
47   }
48 }
49
50 const char* MC_dwarf_tagname(int tag) {
51   switch (tag) {
52 #include "mc_dwarf_tagnames.h"
53   case DW_TAG_invalid:
54     return "DW_TAG_invalid";
55   default:
56     return "DW_TAG_unkown";
57   }
58 }
59
60 static inline const char* MC_dwarf_die_tagname(Dwarf_Die* die) {
61   return MC_dwarf_tagname(dwarf_tag(die));
62 }
63
64 // ***** Attributes
65
66 static const char* MC_dwarf_attr_string(Dwarf_Die* die, int attribute) {
67   Dwarf_Attribute attr;
68   if (!dwarf_attr_integrate(die, attribute, &attr)) {
69         return NULL;
70   } else {
71         return dwarf_formstring(&attr);
72   }
73 }
74
75 // Return a new string for the type (NULL if none)
76 static char* MC_dwarf_at_type(Dwarf_Die* die) {
77   Dwarf_Attribute attr;
78   if (dwarf_hasattr_integrate(die, DW_AT_type)) {
79         dwarf_attr_integrate(die, DW_AT_type, &attr);
80         Dwarf_Die subtype_die;
81         if (dwarf_formref_die(&attr, &subtype_die)==NULL) {
82           xbt_die("Could not find DIE for type");
83         }
84         Dwarf_Off subtype_global_offset = dwarf_dieoffset(&subtype_die);
85     return bprintf("%" PRIx64 , subtype_global_offset);
86   }
87   else return NULL;
88 }
89
90 static uint64_t MC_dwarf_attr_uint(Dwarf_Die* die, int attribute, uint64_t default_value) {
91   Dwarf_Attribute attr;
92   Dwarf_Word value;
93   return dwarf_formudata(dwarf_attr_integrate(die, attribute, &attr), &value) == 0 ? (uint64_t) value : default_value;
94 }
95
96 static bool MC_dwarf_attr_flag(Dwarf_Die* die, int attribute, int integrate) {
97   Dwarf_Attribute attr;
98   if ((integrate ? dwarf_attr_integrate(die, attribute, &attr)
99                     : dwarf_attr(die, attribute, &attr))==0)
100     return false;
101
102   bool result;
103   if (dwarf_formflag(&attr, &result))
104     xbt_die("Unexpected form for attribute %s",
105       MC_dwarf_attrname(attribute));
106   return result;
107 }
108
109 static uint64_t MC_dwarf_default_lower_bound(int lang) {
110   switch(lang) {
111   case DW_LANG_C:
112   case DW_LANG_C89:
113   case DW_LANG_C99:
114   case DW_LANG_C_plus_plus:
115   case DW_LANG_D:
116   case DW_LANG_Java:
117   case DW_LANG_ObjC:
118   case DW_LANG_ObjC_plus_plus:
119   case DW_LANG_Python:
120   case DW_LANG_UPC:
121     return 0;
122   case DW_LANG_Ada83:
123   case DW_LANG_Ada95:
124   case DW_LANG_Fortran77:
125   case DW_LANG_Fortran90:
126   case DW_LANG_Fortran95:
127   case DW_LANG_Modula2:
128   case DW_LANG_Pascal83:
129   case DW_LANG_PL1:
130   case DW_LANG_Cobol74:
131   case DW_LANG_Cobol85:
132     return 1;
133   default:
134     xbt_die("No default MT_TAG_lower_bound for language %i and none given", lang);
135     return 0;
136   }
137 }
138
139 static uint64_t MC_dwarf_subrange_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
140   // Use DW_TAG_count if present:
141   if (dwarf_hasattr_integrate(die, DW_AT_count)) {
142     return MC_dwarf_attr_uint(die, DW_AT_count, 0);
143   }
144
145   // Otherwise compute DW_TAG_upper_bound-DW_TAG_lower_bound:
146
147   if (!dwarf_hasattr_integrate(die, DW_AT_upper_bound)) {
148         // This is not really 0, but the code expects this (we do not know):
149     return 0;
150   }
151   uint64_t upper_bound = MC_dwarf_attr_uint(die, DW_AT_upper_bound, -1);
152
153   uint64_t lower_bound = 0;
154   if (dwarf_hasattr_integrate(die, DW_AT_lower_bound)) {
155     lower_bound = MC_dwarf_attr_uint(die, DW_AT_lower_bound, -1);
156   } else {
157         lower_bound = MC_dwarf_default_lower_bound(dwarf_srclang(unit));
158   }
159   return upper_bound - lower_bound;
160 }
161
162 static uint64_t MC_dwarf_array_element_count(Dwarf_Die* die, Dwarf_Die* unit) {
163   xbt_assert(dwarf_tag(die)==DW_TAG_array_type,
164     "MC_dwarf_array_element_count called with DIE of type %s", MC_dwarf_die_tagname(die));
165
166   int result = 1;
167   Dwarf_Die child;
168   int res;
169   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
170         int child_tag = dwarf_tag(&child);
171     if (child_tag==DW_TAG_subrange_type ||child_tag==DW_TAG_enumeration_type) {
172       result *= MC_dwarf_subrange_element_count(&child, unit);
173     }
174   }
175   return result;
176 }
177
178 // ***** Location
179
180 Dwarf_Off MC_dwarf_resolve_location(unw_cursor_t* c, dw_location_t location, void* frame_pointer_address) {
181   unw_word_t res;
182   switch (location->type){
183   case e_dw_compose:
184     if (xbt_dynar_length(location->location.compose) > 1){
185       return 0; /* TODO : location list with optimizations enabled */
186     }
187     dw_location_t location_entry = xbt_dynar_get_as(location->location.compose, 0, dw_location_t);
188     switch (location_entry->type){
189     case e_dw_register:
190       unw_get_reg(c, location_entry->location.reg, &res);
191       return res;
192     case e_dw_bregister_op:
193       unw_get_reg(c, location_entry->location.breg_op.reg, &res);
194       return (Dwarf_Off) ((long)res + location_entry->location.breg_op.offset);
195       break;
196     case e_dw_fbregister_op:
197       if (frame_pointer_address != NULL)
198         return (Dwarf_Off)((char *)frame_pointer_address + location_entry->location.fbreg_op);
199       else
200         return 0;
201     default:
202       return 0; /* FIXME : implement other cases (with optimizations enabled) */
203     }
204     break;
205     default:
206       return 0;
207   }
208 }
209
210 // ***** dw_type_t
211
212 static void MC_dwarf_fill_member_location(dw_type_t type, dw_type_t member, Dwarf_Die* child) {
213   if (dwarf_hasattr(child, DW_AT_data_bit_offset)) {
214     xbt_die("Can't groke DW_AT_data_bit_offset.");
215   }
216
217   if (!dwarf_hasattr_integrate(child, DW_AT_data_member_location)) {
218     if (type->type != DW_TAG_union_type) {
219         xbt_die(
220           "Missing DW_AT_data_member_location field in DW_TAG_member %s of type <%p>%s",
221           member->name, type->id, type->name);
222     } else {
223       return;
224     }
225   }
226
227   Dwarf_Attribute attr;
228   dwarf_attr_integrate(child, DW_AT_data_member_location, &attr);
229   switch (dwarf_whatform(&attr)) {
230
231   case DW_FORM_exprloc:
232     {
233       Dwarf_Op* expr;
234       size_t len;
235       if (dwarf_getlocation(&attr, &expr, &len)) {
236         xbt_die(
237           "Could not read location expression DW_AT_data_member_location in DW_TAG_member %s of type <%p>%s",
238           MC_dwarf_attr_string(child, DW_AT_name),
239           type->id, type->name);
240       }
241       if (len==1 && expr[0].atom == DW_OP_plus_uconst) {
242         member->offset =  expr[0].number;
243       } else {
244         xbt_die("Can't groke this location expression yet. %i %i",
245           len==1 , expr[0].atom == DW_OP_plus_uconst);
246       }
247       break;
248     }
249   case DW_FORM_data1:
250   case DW_FORM_data2:
251   case DW_FORM_data4:
252   case DW_FORM_data8:
253   case DW_FORM_sdata:
254   case DW_FORM_udata:
255     {
256       Dwarf_Word offset;
257       if (!dwarf_formudata(&attr, &offset))
258         member->offset = offset;
259       else
260         xbt_die("Cannot get DW_AT_data_member_%s location <%p>%s",
261           MC_dwarf_attr_string(child, DW_AT_name),
262           type->id, type->name);
263       break;
264     }
265   }
266 }
267
268 static void MC_dwarf_add_members(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_type_t type) {
269   int res;
270   Dwarf_Die child;
271   xbt_assert(!type->members);
272   type->members = xbt_dynar_new(sizeof(dw_type_t), (void(*)(void*))dw_type_free);
273   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
274     if (dwarf_tag(&child)==DW_TAG_member) {
275       // TODO, we should use another type (because is is not a type but a member)
276       dw_type_t member = xbt_new0(s_dw_type_t, 1);
277       member->type = -1;
278       member->id = NULL;
279
280       const char* name = MC_dwarf_attr_string(&child, DW_AT_name);
281       if(name)
282         member->name = xbt_strdup(name);
283       else
284         member->name = NULL;
285
286       member->byte_size = MC_dwarf_attr_uint(&child, DW_AT_byte_size, 0);
287       member->element_count = -1;
288       member->dw_type_id = MC_dwarf_at_type(&child);
289       member->members = NULL;
290       member->is_pointer_type = 0;
291       member->offset = 0;
292
293       if(dwarf_hasattr(&child, DW_AT_data_bit_offset)) {
294         xbt_die("Can't groke DW_AT_data_bit_offset.");
295       }
296
297       MC_dwarf_fill_member_location(type, member, &child);
298
299       xbt_dynar_push(type->members, &member);
300     }
301   }
302 }
303
304 static dw_type_t MC_dwarf_die_to_type(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit) {
305
306   dw_type_t type = xbt_new0(s_dw_type_t, 1);
307   type->type = -1;
308   type->id = NULL;
309   type->name = NULL;
310   type->byte_size = 0;
311   type->element_count = -1;
312   type->dw_type_id = NULL;
313   type->members = NULL;
314   type->is_pointer_type = 0;
315   type->offset = 0;
316
317   type->type = dwarf_tag(die);
318
319   // Global Offset
320   type->id = (void *) dwarf_dieoffset(die);
321
322   const char* name = MC_dwarf_attr_string(die, DW_AT_name);
323   if (name!=NULL) {
324         type->name = xbt_strdup(name);
325   }
326
327   XBT_DEBUG("Processing type <%p>%s", type->id, type->name);
328
329   type->dw_type_id = MC_dwarf_at_type(die);
330
331   // Computation of the byte_size;
332   if (dwarf_hasattr_integrate(die, DW_AT_byte_size))
333     type->byte_size = MC_dwarf_attr_uint(die, DW_AT_byte_size, 0);
334   else if (type->type == DW_TAG_array_type || type->type==DW_TAG_structure_type || type->type==DW_TAG_class_type) {
335     Dwarf_Word size;
336     if (dwarf_aggregate_size(die, &size)==0) {
337       type->byte_size = size;
338     }
339   }
340
341   switch (type->type) {
342   case DW_TAG_array_type:
343         type->element_count = MC_dwarf_array_element_count(die, unit);
344         // TODO, handle DW_byte_stride and (not) DW_bit_stride
345         break;
346
347   case DW_TAG_pointer_type:
348   case DW_TAG_reference_type:
349   case DW_TAG_rvalue_reference_type:
350     type->is_pointer_type = 1;
351     break;
352
353   case DW_TAG_structure_type:
354   case DW_TAG_union_type:
355   case DW_TAG_class_type:
356           MC_dwarf_add_members(info, die, unit, type);
357   }
358
359   return type;
360 }
361
362 static void MC_dwarf_handle_type_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit) {
363   dw_type_t type = MC_dwarf_die_to_type(info, die, unit);
364
365   char* key = bprintf("%" PRIx64, (uint64_t) type->id);
366   xbt_dict_set(info->types, key, type, NULL);
367 }
368
369 /** \brief Convert libdw location expresion elment into native one (or NULL in some cases) */
370 static dw_location_t MC_dwarf_get_expression_element(Dwarf_Op* op) {
371   dw_location_t element = xbt_new0(s_dw_location_t, 1);
372   uint8_t atom = op->atom;
373   if (atom >= DW_OP_reg0 && atom<= DW_OP_reg31) {
374     element->type = e_dw_register;
375     element->location.reg = atom - DW_OP_reg0;
376   }
377   else if (atom >= DW_OP_breg0 && atom<= DW_OP_breg31) {
378     element->type = e_dw_bregister_op;
379     element->location.reg = atom - DW_OP_breg0;
380     element->location.breg_op.offset = op->number;
381   }
382   else if (atom >= DW_OP_lit0 && atom<= DW_OP_lit31) {
383     element->type = e_dw_lit;
384     element->location.reg = atom - DW_OP_lit0;
385   }
386   else switch (atom) {
387   case DW_OP_fbreg:
388     element->type = e_dw_fbregister_op;
389     element->location.fbreg_op = op->number;
390     break;
391   case DW_OP_piece:
392     element->type = e_dw_piece;
393     element->location.piece = op->number;
394     break;
395   case DW_OP_plus_uconst:
396     element->type = e_dw_plus_uconst;
397     element->location.plus_uconst = op->number;
398     break;
399   case DW_OP_abs:
400     element->type = e_dw_arithmetic;
401     element->location.arithmetic = xbt_strdup("abs");
402     break;
403   case DW_OP_and:
404     element->type = e_dw_arithmetic;
405     element->location.arithmetic = xbt_strdup("and");
406     break;
407   case DW_OP_div:
408     element->type = e_dw_arithmetic;
409     element->location.arithmetic = xbt_strdup("div");
410     break;
411   case DW_OP_minus:
412     element->type = e_dw_arithmetic;
413     element->location.arithmetic = xbt_strdup("minus");
414     break;
415   case DW_OP_mod:
416     element->type = e_dw_arithmetic;
417     element->location.arithmetic = xbt_strdup("mod");
418     break;
419   case DW_OP_mul:
420     element->type = e_dw_arithmetic;
421     element->location.arithmetic = xbt_strdup("mul");
422     break;
423   case DW_OP_neg:
424     element->type = e_dw_arithmetic;
425     element->location.arithmetic = xbt_strdup("neg");
426     break;
427   case DW_OP_not:
428     element->type = e_dw_arithmetic;
429     element->location.arithmetic = xbt_strdup("not");
430     break;
431   case DW_OP_or:
432     element->type = e_dw_arithmetic;
433     element->location.arithmetic = xbt_strdup("or");
434     break;
435   case DW_OP_plus:
436     element->type = e_dw_arithmetic;
437     element->location.arithmetic = xbt_strdup("plus");
438     break;
439
440   case DW_OP_stack_value:
441     // Why nothing here?
442     xbt_free(element);
443     return NULL;
444
445   case DW_OP_deref_size:
446     element->type = e_dw_deref;
447     element->location.deref_size =  (unsigned int short) op->number;
448     break;
449   case DW_OP_deref:
450     element->type = e_dw_deref;
451     element->location.deref_size = sizeof(void *);
452     break;
453   case DW_OP_constu:
454     element->type = e_dw_uconstant;
455     element->location.uconstant.bytes = 1;
456     element->location.uconstant.value = (unsigned long int) op->number;
457     break;
458   case DW_OP_consts:
459     element->type = e_dw_sconstant;
460     element->location.uconstant.bytes = 1;
461     element->location.uconstant.value = (unsigned long int) op->number;
462     break;
463
464   case DW_OP_const1u:
465     element->type = e_dw_uconstant;
466     element->location.uconstant.bytes = 1;
467     element->location.uconstant.value = (unsigned long int) op->number;
468     break;
469   case DW_OP_const2u:
470     element->type = e_dw_uconstant;
471     element->location.uconstant.bytes = 2;
472     element->location.uconstant.value = (unsigned long int) op->number;
473     break;
474   case DW_OP_const4u:
475     element->type = e_dw_uconstant;
476     element->location.uconstant.bytes = 4;
477     element->location.uconstant.value = (unsigned long int) op->number;
478     break;
479   case DW_OP_const8u:
480     element->type = e_dw_uconstant;
481     element->location.uconstant.bytes = 8;
482     element->location.uconstant.value = (unsigned long int) op->number;
483     break;
484
485   case DW_OP_const1s:
486     element->type = e_dw_sconstant;
487     element->location.uconstant.bytes = 1;
488     element->location.uconstant.value = (unsigned long int) op->number;
489     break;
490   case DW_OP_const2s:
491     element->type = e_dw_sconstant;
492     element->location.uconstant.bytes = 2;
493     element->location.uconstant.value = (unsigned long int) op->number;
494     break;
495   case DW_OP_const4s:
496     element->type = e_dw_sconstant;
497     element->location.uconstant.bytes = 4;
498     element->location.uconstant.value = (unsigned long int) op->number;
499     break;
500   case DW_OP_const8s:
501     element->type = e_dw_sconstant;
502     element->location.uconstant.bytes = 8;
503     element->location.uconstant.value = (unsigned long int) op->number;
504     break;
505   default:
506     element->type = e_dw_unsupported;
507     break;
508   }
509   return element;
510 }
511
512 /** \brief Convert libdw location expresion into native one */
513 static dw_location_t MC_dwarf_get_expression(Dwarf_Op* expr,  size_t len) {
514   dw_location_t loc = xbt_new0(s_dw_location_t, 1);
515   loc->type = e_dw_compose;
516   loc->location.compose = xbt_dynar_new(sizeof(dw_location_t), NULL);
517
518   int i;
519   for (i=0; i!=len; ++i) {
520     dw_location_t element =  MC_dwarf_get_expression_element(expr+i);
521     if (element)
522       xbt_dynar_push(loc->location.compose, &element);
523   }
524
525   return loc;
526 }
527
528 static dw_variable_t MC_die_to_variable(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit, dw_frame_t frame) {
529   // Drop declaration:
530   if (MC_dwarf_attr_flag(die, DW_AT_declaration, false))
531     return NULL;
532
533   Dwarf_Attribute attr_location;
534   if (dwarf_attr(die, DW_AT_location, &attr_location)==NULL) {
535     // No location: do not add it ?
536     return NULL;
537   }
538
539   dw_variable_t variable = xbt_new0(s_dw_variable_t, 1);
540   variable->dwarf_offset = dwarf_dieoffset(die);
541   variable->global = frame == NULL; // Can be override base on DW_AT_location
542   variable->name = xbt_strdup(MC_dwarf_attr_string(die, DW_AT_name));
543   variable->type_origin = MC_dwarf_at_type(die);
544   variable->address.address = NULL;
545
546   int form;
547   switch (form = dwarf_whatform(&attr_location)) {
548   case DW_FORM_exprloc:
549   case DW_FORM_block1: // Not in the spec but found in the wild.
550     {
551       Dwarf_Op* expr;
552       size_t len;
553       if (dwarf_getlocation(&attr_location, &expr, &len)) {
554         xbt_die(
555           "Could not read location expression in DW_AT_location of variable <%p>%s",
556           (void*) variable->dwarf_offset, variable->name);
557       }
558
559       if (len==1 && expr[0].atom == DW_OP_addr) {
560         variable->global = 1;
561         Dwarf_Off offset = expr[0].number;
562         // TODO, Why is this different base on the object?
563         Dwarf_Off base = strcmp(info->file_name, xbt_binary_name) !=0 ? (Dwarf_Off) info->start_text : 0;
564         variable->address.address = (void*) (base + offset);
565       } else {
566         variable->address.location = MC_dwarf_get_expression(expr, len);
567       }
568
569       break;
570     }
571   case DW_FORM_sec_offset: // type loclistptr
572     xbt_die("Do not handle loclist locations yet");
573     break;
574   default:
575     xbt_die("Unexpected form %i list for location in <%p>%s",
576       form, (void*) variable->dwarf_offset, variable->name);
577   }
578
579   return variable;
580 }
581
582 static void MC_dwarf_handle_variable_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit) {
583   dw_variable_t variable = MC_die_to_variable(info, die, unit, NULL);
584   if(variable==NULL)
585       return;
586   if(variable->global)
587     MC_dwarf_register_global_variable(info, variable);
588   else
589     xbt_die("Unexpected non global variable <%p>%s",
590       (void*) dwarf_dieoffset(die),
591       MC_dwarf_attr_string(die, DW_AT_name)
592       );
593 }
594
595
596 static void MC_dwarf_handle_children(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit) {
597   Dwarf_Die child;
598   int res;
599   for (res=dwarf_child(die, &child); res==0; res=dwarf_siblingof(&child,&child)) {
600     MC_dwarf_handle_die(info, &child, unit);
601   }
602 }
603
604 static void MC_dwarf_handle_die(mc_object_info_t info, Dwarf_Die* die, Dwarf_Die* unit) {
605   int tag = dwarf_tag(die);
606
607   switch (tag) {
608     case DW_TAG_array_type:
609     case DW_TAG_class_type:
610     case DW_TAG_enumeration_type:
611     case DW_TAG_typedef:
612     case DW_TAG_pointer_type:
613     case DW_TAG_string_type:
614     case DW_TAG_structure_type:
615     case DW_TAG_subroutine_type:
616     case DW_TAG_union_type:
617     case DW_TAG_ptr_to_member_type:
618     case DW_TAG_set_type:
619     case DW_TAG_subrange_type:
620     case DW_TAG_base_type:
621     case DW_TAG_const_type:
622     case DW_TAG_file_type:
623     case DW_TAG_packed_type:
624     case DW_TAG_volatile_type:
625     case DW_TAG_restrict_type:
626     case DW_TAG_interface_type:
627     case DW_TAG_unspecified_type:
628     case DW_TAG_mutable_type:
629     case DW_TAG_shared_type:
630       MC_dwarf_handle_type_die(info, die, unit);
631       break;
632     case DW_TAG_inlined_subroutine:
633     case DW_TAG_subprogram:
634       // Skip recursive processing
635       return;
636     case DW_TAG_variable:
637       if (MC_USE_LIBDW_NON_FUNCTION_VARIABLES)
638         MC_dwarf_handle_variable_die(info, die, unit);
639       break;
640   }
641
642   // Recursive processing of children DIE:
643   MC_dwarf_handle_children(info, die, unit);
644 }
645
646 void MC_dwarf_get_variables_libdw(mc_object_info_t info) {
647   int fd = open(info->file_name, O_RDONLY);
648   if (fd<0) {
649     xbt_die("Could not open file %s", info->file_name);
650   }
651   Dwarf *dwarf = dwarf_begin(fd, DWARF_C_READ);
652   if (dwarf==NULL) {
653     xbt_die("Your program must be compiled with -g");
654   }
655
656   Dwarf_Off offset = 0;
657   Dwarf_Off next_offset = 0;
658   size_t length;
659   while (dwarf_nextcu (dwarf, offset, &next_offset, &length, NULL, NULL, NULL) == 0) {
660     Dwarf_Die die;
661     if(dwarf_offdie(dwarf, offset+length, &die)!=NULL) {
662       MC_dwarf_handle_die(info, &die, &die);
663     }
664     offset = next_offset;
665   }
666
667   dwarf_end(dwarf);
668   close(fd);
669 }