Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Get local variables in binary and libsimgrid.so and their values for each system...
[simgrid.git] / src / mc / mc_global.c
1 /* Copyright (c) 2008-2012 Da SimGrid Team. All rights reserved.            */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <unistd.h>
7 #include <sys/types.h>
8 #include <sys/wait.h>
9 #include <sys/time.h>
10
11 #include "../surf/surf_private.h"
12 #include "../simix/smx_private.h"
13 #include "../xbt/mmalloc/mmprivate.h"
14 #include "xbt/fifo.h"
15 #include "mc_private.h"
16 #include "xbt/automaton.h"
17 #include "xbt/dict.h"
18
19 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
21                                 "Logging specific to MC (global)");
22
23 /* Configuration support */
24 e_mc_reduce_t mc_reduce_kind=e_mc_reduce_unset;
25
26 extern int _surf_init_status;
27 void _mc_cfg_cb_reduce(const char *name, int pos) {
28   if (_surf_init_status && !_surf_do_model_check) {
29     xbt_die("You are specifying a reduction strategy after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
30   }
31   char *val= xbt_cfg_get_string(_surf_cfg_set, name);
32   if (!strcasecmp(val,"none")) {
33     mc_reduce_kind = e_mc_reduce_none;
34   } else if (!strcasecmp(val,"dpor")) {
35     mc_reduce_kind = e_mc_reduce_dpor;
36   } else {
37     xbt_die("configuration option %s can only take 'none' or 'dpor' as a value",name);
38   }
39   xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
40 }
41
42 void _mc_cfg_cb_checkpoint(const char *name, int pos) {
43   if (_surf_init_status && !_surf_do_model_check) {
44     xbt_die("You are specifying a checkpointing value after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
45   }
46   _surf_mc_checkpoint = xbt_cfg_get_int(_surf_cfg_set, name);
47   xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
48 }
49 void _mc_cfg_cb_property(const char *name, int pos) {
50   if (_surf_init_status && !_surf_do_model_check) {
51     xbt_die("You are specifying a property after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
52   }
53   _surf_mc_property_file= xbt_cfg_get_string(_surf_cfg_set, name);
54   xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
55 }
56
57
58 /* MC global data structures */
59
60 mc_state_t mc_current_state = NULL;
61 char mc_replay_mode = FALSE;
62 double *mc_time = NULL;
63 mc_snapshot_t initial_snapshot = NULL;
64 int raw_mem_set;
65
66 /* Safety */
67
68 xbt_fifo_t mc_stack_safety = NULL;
69 mc_stats_t mc_stats = NULL;
70
71 /* Liveness */
72
73 mc_stats_pair_t mc_stats_pair = NULL;
74 xbt_fifo_t mc_stack_liveness = NULL;
75 mc_snapshot_t initial_snapshot_liveness = NULL;
76 int compare;
77
78 /* Local */
79 xbt_dict_t mc_local_variables = NULL;
80
81 /* Ignore mechanism */
82 extern xbt_dynar_t mc_comparison_ignore;
83 extern xbt_dynar_t stacks_areas;
84
85 xbt_automaton_t _mc_property_automaton = NULL;
86
87 /* Static functions */
88
89 static void MC_assert_pair(int prop);
90 static dw_location_t get_location(xbt_dict_t location_list, char *expr);
91 static dw_frame_t get_frame_by_offset(xbt_dict_t all_variables, unsigned long int offset);
92
93 void MC_do_the_modelcheck_for_real() {
94   if (!_surf_mc_property_file || _surf_mc_property_file[0]=='\0') {
95     if (mc_reduce_kind==e_mc_reduce_unset)
96       mc_reduce_kind=e_mc_reduce_dpor;
97
98     XBT_INFO("Check a safety property");
99     MC_modelcheck();
100
101   } else  {
102
103     if (mc_reduce_kind==e_mc_reduce_unset)
104       mc_reduce_kind=e_mc_reduce_none;
105
106     XBT_INFO("Check the liveness property %s",_surf_mc_property_file);
107     MC_automaton_load(_surf_mc_property_file);
108     MC_modelcheck_liveness();
109   }
110 }
111
112 /**
113  *  \brief Initialize the model-checker data structures
114  */
115 void MC_init_safety(void)
116 {
117
118   raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
119
120   /* Check if MC is already initialized */
121   if (initial_snapshot)
122     return;
123
124   mc_time = xbt_new0(double, simix_process_maxpid);
125
126   /* Initialize the data structures that must be persistent across every
127      iteration of the model-checker (in RAW memory) */
128   
129   MC_SET_RAW_MEM;
130
131   /* Initialize statistics */
132   mc_stats = xbt_new0(s_mc_stats_t, 1);
133   mc_stats->state_size = 1;
134
135   /* Create exploration stack */
136   mc_stack_safety = xbt_fifo_new();
137
138   MC_UNSET_RAW_MEM;
139
140   MC_dpor_init();
141
142   MC_SET_RAW_MEM;
143   /* Save the initial state */
144   initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
145   MC_take_snapshot(initial_snapshot);
146   MC_UNSET_RAW_MEM;
147
148
149   if(raw_mem_set)
150     MC_SET_RAW_MEM;
151   else
152     MC_UNSET_RAW_MEM;
153   
154 }
155
156 void MC_compare(void){
157   compare = 1;
158 }
159
160
161 void MC_modelcheck(void)
162 {
163   MC_init_safety();
164   MC_dpor();
165   MC_exit();
166 }
167
168 void MC_modelcheck_liveness(){
169
170   raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
171
172   /* init stuff */
173   XBT_INFO("Start init mc");
174   
175   mc_time = xbt_new0(double, simix_process_maxpid);
176
177   /* mc_time refers to clock for each process -> ignore it for heap comparison */
178   int i;
179   for(i = 0; i<simix_process_maxpid; i++)
180     MC_ignore(&(mc_time[i]), sizeof(double));
181   
182   compare = 0;
183
184   /* Initialize the data structures that must be persistent across every
185      iteration of the model-checker (in RAW memory) */
186
187   MC_SET_RAW_MEM;
188
189   char *ls_path = get_libsimgrid_path(); 
190   
191   mc_local_variables = xbt_dict_new_homogeneous(NULL);
192
193   /* Get local variables in binary for state equality detection */
194   xbt_dict_t binary_location_list = MC_get_location_list(xbt_binary_name);
195   MC_get_local_variables(xbt_binary_name, binary_location_list, &mc_local_variables);
196
197   /* Get local variables in libsimgrid for state equality detection */
198   xbt_dict_t libsimgrid_location_list = MC_get_location_list(ls_path);
199   MC_get_local_variables(ls_path, libsimgrid_location_list, &mc_local_variables);
200   
201   /* Initialize statistics */
202   mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
203
204   XBT_DEBUG("Creating stack");
205
206   /* Create exploration stack */
207   mc_stack_liveness = xbt_fifo_new();
208
209   MC_UNSET_RAW_MEM;
210
211   MC_ddfs_init();
212
213   /* We're done */
214   MC_print_statistics_pairs(mc_stats_pair);
215   xbt_free(mc_time);
216   MC_memory_exit();
217   exit(0);
218 }
219
220
221 void MC_exit(void)
222 {
223   MC_print_statistics(mc_stats);
224   xbt_free(mc_time);
225   MC_memory_exit();
226 }
227
228
229 int MC_random(int min, int max)
230 {
231   /*FIXME: return mc_current_state->executed_transition->random.value;*/
232   return 0;
233 }
234
235 /**
236  * \brief Schedules all the process that are ready to run
237  */
238 void MC_wait_for_requests(void)
239 {
240   smx_process_t process;
241   smx_simcall_t req;
242   unsigned int iter;
243
244   while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
245     SIMIX_process_runall();
246     xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
247       req = &process->simcall;
248       if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
249         SIMIX_simcall_pre(req, 0);
250     }
251   }
252 }
253
254 int MC_deadlock_check()
255 {
256   int deadlock = FALSE;
257   smx_process_t process;
258   if(xbt_swag_size(simix_global->process_list)){
259     deadlock = TRUE;
260     xbt_swag_foreach(process, simix_global->process_list){
261       if(process->simcall.call != SIMCALL_NONE
262          && MC_request_is_enabled(&process->simcall)){
263         deadlock = FALSE;
264         break;
265       }
266     }
267   }
268   return deadlock;
269 }
270
271 /**
272  * \brief Re-executes from the state at position start all the transitions indicated by
273  *        a given model-checker stack.
274  * \param stack The stack with the transitions to execute.
275  * \param start Start index to begin the re-execution.
276  */
277 void MC_replay(xbt_fifo_t stack, int start)
278 {
279   raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
280
281   int value, i = 1;
282   char *req_str;
283   smx_simcall_t req = NULL, saved_req = NULL;
284   xbt_fifo_item_t item, start_item;
285   mc_state_t state;
286
287   XBT_DEBUG("**** Begin Replay ****");
288
289   if(start == -1){
290     /* Restore the initial state */
291     MC_restore_snapshot(initial_snapshot);
292     /* At the moment of taking the snapshot the raw heap was set, so restoring
293      * it will set it back again, we have to unset it to continue  */
294     MC_UNSET_RAW_MEM;
295   }
296
297   start_item = xbt_fifo_get_last_item(stack);
298   if(start != -1){
299     while (i != start){
300       start_item = xbt_fifo_get_prev_item(start_item);
301       i++;
302     }
303   }
304
305   /* Traverse the stack from the state at position start and re-execute the transitions */
306   for (item = start_item;
307        item != xbt_fifo_get_first_item(stack);
308        item = xbt_fifo_get_prev_item(item)) {
309
310     state = (mc_state_t) xbt_fifo_get_item_content(item);
311     saved_req = MC_state_get_executed_request(state, &value);
312    
313     if(saved_req){
314       /* because we got a copy of the executed request, we have to fetch the  
315          real one, pointed by the request field of the issuer process */
316       req = &saved_req->issuer->simcall;
317
318       /* Debug information */
319       if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
320         req_str = MC_request_to_string(req, value);
321         XBT_DEBUG("Replay: %s (%p)", req_str, state);
322         xbt_free(req_str);
323       }
324     }
325          
326     SIMIX_simcall_pre(req, value);
327     MC_wait_for_requests();
328          
329     /* Update statistics */
330     mc_stats->visited_states++;
331     mc_stats->executed_transitions++;
332   }
333   XBT_DEBUG("**** End Replay ****");
334
335   if(raw_mem_set)
336     MC_SET_RAW_MEM;
337   else
338     MC_UNSET_RAW_MEM;
339   
340
341 }
342
343 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
344 {
345
346   raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
347
348   int value;
349   char *req_str;
350   smx_simcall_t req = NULL, saved_req = NULL;
351   xbt_fifo_item_t item;
352   mc_state_t state;
353   mc_pair_stateless_t pair;
354   int depth = 1;
355
356   XBT_DEBUG("**** Begin Replay ****");
357
358   /* Restore the initial state */
359   MC_restore_snapshot(initial_snapshot_liveness);
360   /* At the moment of taking the snapshot the raw heap was set, so restoring
361    * it will set it back again, we have to unset it to continue  */
362   MC_UNSET_RAW_MEM;
363
364   if(all_stack){
365
366     item = xbt_fifo_get_last_item(stack);
367
368     while(depth <= xbt_fifo_size(stack)){
369
370       pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
371       state = (mc_state_t) pair->graph_state;
372
373       if(pair->requests > 0){
374    
375         saved_req = MC_state_get_executed_request(state, &value);
376         //XBT_DEBUG("SavedReq->call %u", saved_req->call);
377       
378         if(saved_req != NULL){
379           /* because we got a copy of the executed request, we have to fetch the  
380              real one, pointed by the request field of the issuer process */
381           req = &saved_req->issuer->simcall;
382           //XBT_DEBUG("Req->call %u", req->call);
383   
384           /* Debug information */
385           if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
386             req_str = MC_request_to_string(req, value);
387             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
388             xbt_free(req_str);
389           }
390   
391         }
392  
393         SIMIX_simcall_pre(req, value);
394         MC_wait_for_requests();
395       }
396
397       depth++;
398     
399       /* Update statistics */
400       mc_stats_pair->visited_pairs++;
401
402       item = xbt_fifo_get_prev_item(item);
403     }
404
405   }else{
406
407     /* Traverse the stack from the initial state and re-execute the transitions */
408     for (item = xbt_fifo_get_last_item(stack);
409          item != xbt_fifo_get_first_item(stack);
410          item = xbt_fifo_get_prev_item(item)) {
411
412       pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
413       state = (mc_state_t) pair->graph_state;
414
415       if(pair->requests > 0){
416    
417         saved_req = MC_state_get_executed_request(state, &value);
418         //XBT_DEBUG("SavedReq->call %u", saved_req->call);
419       
420         if(saved_req != NULL){
421           /* because we got a copy of the executed request, we have to fetch the  
422              real one, pointed by the request field of the issuer process */
423           req = &saved_req->issuer->simcall;
424           //XBT_DEBUG("Req->call %u", req->call);
425   
426           /* Debug information */
427           if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
428             req_str = MC_request_to_string(req, value);
429             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
430             xbt_free(req_str);
431           }
432   
433         }
434  
435         SIMIX_simcall_pre(req, value);
436         MC_wait_for_requests();
437       }
438
439       depth++;
440     
441       /* Update statistics */
442       mc_stats_pair->visited_pairs++;
443     }
444   }  
445
446   XBT_DEBUG("**** End Replay ****");
447
448   if(raw_mem_set)
449     MC_SET_RAW_MEM;
450   else
451     MC_UNSET_RAW_MEM;
452   
453 }
454
455 /**
456  * \brief Dumps the contents of a model-checker's stack and shows the actual
457  *        execution trace
458  * \param stack The stack to dump
459  */
460 void MC_dump_stack_safety(xbt_fifo_t stack)
461 {
462   
463   raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
464
465   MC_show_stack_safety(stack);
466
467   if(!_surf_mc_checkpoint){
468
469     mc_state_t state;
470
471     MC_SET_RAW_MEM;
472     while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
473       MC_state_delete(state);
474     MC_UNSET_RAW_MEM;
475
476   }
477
478   if(raw_mem_set)
479     MC_SET_RAW_MEM;
480   else
481     MC_UNSET_RAW_MEM;
482   
483 }
484
485
486 void MC_show_stack_safety(xbt_fifo_t stack)
487 {
488   int value;
489   mc_state_t state;
490   xbt_fifo_item_t item;
491   smx_simcall_t req;
492   char *req_str = NULL;
493   
494   for (item = xbt_fifo_get_last_item(stack);
495        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
496         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
497     req = MC_state_get_executed_request(state, &value);
498     if(req){
499       req_str = MC_request_to_string(req, value);
500       XBT_INFO("%s", req_str);
501       xbt_free(req_str);
502     }
503   }
504 }
505
506 void MC_show_deadlock(smx_simcall_t req)
507 {
508   /*char *req_str = NULL;*/
509   XBT_INFO("**************************");
510   XBT_INFO("*** DEAD-LOCK DETECTED ***");
511   XBT_INFO("**************************");
512   XBT_INFO("Locked request:");
513   /*req_str = MC_request_to_string(req);
514     XBT_INFO("%s", req_str);
515     xbt_free(req_str);*/
516   XBT_INFO("Counter-example execution trace:");
517   MC_dump_stack_safety(mc_stack_safety);
518 }
519
520
521 void MC_show_stack_liveness(xbt_fifo_t stack){
522   int value;
523   mc_pair_stateless_t pair;
524   xbt_fifo_item_t item;
525   smx_simcall_t req;
526   char *req_str = NULL;
527   
528   for (item = xbt_fifo_get_last_item(stack);
529        (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
530         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
531     req = MC_state_get_executed_request(pair->graph_state, &value);
532     if(req){
533       if(pair->requests>0){
534         req_str = MC_request_to_string(req, value);
535         XBT_INFO("%s", req_str);
536         xbt_free(req_str);
537       }else{
538         XBT_INFO("End of system requests but evolution in Büchi automaton");
539       }
540     }
541   }
542 }
543
544 void MC_dump_stack_liveness(xbt_fifo_t stack){
545
546   raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
547
548   mc_pair_stateless_t pair;
549
550   MC_SET_RAW_MEM;
551   while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
552     MC_pair_stateless_delete(pair);
553   MC_UNSET_RAW_MEM;
554
555   if(raw_mem_set)
556     MC_SET_RAW_MEM;
557   else
558     MC_UNSET_RAW_MEM;
559
560 }
561
562
563 void MC_print_statistics(mc_stats_t stats)
564 {
565   //XBT_INFO("State space size ~= %lu", stats->state_size);
566   XBT_INFO("Expanded states = %lu", stats->expanded_states);
567   XBT_INFO("Visited states = %lu", stats->visited_states);
568   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
569   XBT_INFO("Expanded / Visited = %lf",
570            (double) stats->visited_states / stats->expanded_states);
571   /*XBT_INFO("Exploration coverage = %lf",
572     (double)stats->expanded_states / stats->state_size); */
573 }
574
575 void MC_print_statistics_pairs(mc_stats_pair_t stats)
576 {
577   XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
578   XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
579   //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
580   XBT_INFO("Expanded / Visited = %lf",
581            (double) stats->visited_pairs / stats->expanded_pairs);
582   /*XBT_INFO("Exploration coverage = %lf",
583     (double)stats->expanded_states / stats->state_size); */
584 }
585
586 void MC_assert(int prop)
587 {
588   if (MC_IS_ENABLED && !prop){
589     XBT_INFO("**************************");
590     XBT_INFO("*** PROPERTY NOT VALID ***");
591     XBT_INFO("**************************");
592     XBT_INFO("Counter-example execution trace:");
593     MC_dump_stack_safety(mc_stack_safety);
594     MC_print_statistics(mc_stats);
595     xbt_abort();
596   }
597 }
598
599 static void MC_assert_pair(int prop){
600   if (MC_IS_ENABLED && !prop) {
601     XBT_INFO("**************************");
602     XBT_INFO("*** PROPERTY NOT VALID ***");
603     XBT_INFO("**************************");
604     //XBT_INFO("Counter-example execution trace:");
605     MC_show_stack_liveness(mc_stack_liveness);
606     //MC_dump_snapshot_stack(mc_snapshot_stack);
607     MC_print_statistics_pairs(mc_stats_pair);
608     xbt_abort();
609   }
610 }
611
612 void MC_process_clock_add(smx_process_t process, double amount)
613 {
614   mc_time[process->pid] += amount;
615 }
616
617 double MC_process_clock_get(smx_process_t process)
618 {
619   if(mc_time)
620     return mc_time[process->pid];
621   else
622     return 0;
623 }
624
625 void MC_automaton_load(const char *file){
626
627   raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
628
629   MC_SET_RAW_MEM;
630
631   if (_mc_property_automaton == NULL)
632     _mc_property_automaton = xbt_automaton_new();
633   
634   xbt_automaton_load(_mc_property_automaton,file);
635
636   MC_UNSET_RAW_MEM;
637
638   if(raw_mem_set)
639     MC_SET_RAW_MEM;
640   else
641     MC_UNSET_RAW_MEM;
642
643 }
644
645 void MC_automaton_new_propositional_symbol(const char* id, void* fct) {
646
647   raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
648
649   MC_SET_RAW_MEM;
650
651   if (_mc_property_automaton == NULL)
652     _mc_property_automaton = xbt_automaton_new();
653
654   xbt_new_propositional_symbol(_mc_property_automaton,id,fct);
655
656   MC_UNSET_RAW_MEM;
657
658   if(raw_mem_set)
659     MC_SET_RAW_MEM;
660   else
661     MC_UNSET_RAW_MEM;
662   
663 }
664
665 /************ MC_ignore ***********/ 
666
667 void MC_ignore(void *address, size_t size){
668
669   MC_SET_RAW_MEM;
670   
671   if(mc_comparison_ignore == NULL)
672     mc_comparison_ignore = xbt_dynar_new(sizeof(mc_ignore_region_t), NULL);
673
674   mc_ignore_region_t region = NULL;
675   region = xbt_new0(s_mc_ignore_region_t, 1);
676   region->address = address;
677   region->size = size;
678
679   if((address >= std_heap) && (address <= (void*)((char *)std_heap + STD_HEAP_SIZE))){
680
681     region->block = ((char*)address - (char*)((xbt_mheap_t)std_heap)->heapbase) / BLOCKSIZE + 1;
682     
683     if(((xbt_mheap_t)std_heap)->heapinfo[region->block].type == 0){
684       region->fragment = -1;
685     }else{
686       region->fragment = ((uintptr_t) (ADDR2UINT (address) % (BLOCKSIZE))) >> ((xbt_mheap_t)std_heap)->heapinfo[region->block].type;
687     }
688     
689   }
690
691   unsigned int cursor = 0;
692   mc_ignore_region_t current_region;
693   xbt_dynar_foreach(mc_comparison_ignore, cursor, current_region){
694     if(current_region->address > address)
695       break;
696   }
697
698   xbt_dynar_insert_at(mc_comparison_ignore, cursor, &region);
699
700   MC_UNSET_RAW_MEM;
701 }
702
703 void MC_new_stack_area(void *stack, char *name){
704
705   if(stacks_areas == NULL)
706     stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
707
708   MC_SET_RAW_MEM;
709   stack_region_t region = NULL;
710   region = xbt_new0(s_stack_region_t, 1);
711   region->address = stack;
712   region->process_name = strdup(name);
713   xbt_dynar_push(stacks_areas, &region);
714   MC_UNSET_RAW_MEM;
715 }
716
717 /************ DWARF ***********/
718
719 xbt_dict_t MC_get_location_list(const char *elf_file){
720
721   char *command = bprintf("objdump -Wo %s", elf_file);
722
723   FILE *fp = popen(command, "r");
724
725   if(fp == NULL)
726     perror("popen for objdump failed");
727
728   int debug = 0; /*Detect if the program has been compiled with -g */
729
730   xbt_dict_t location_list = xbt_dict_new_homogeneous(NULL);
731   char *line = NULL, *loc_expr = NULL;
732   ssize_t read;
733   size_t n = 0;
734   int cursor_remove;
735   xbt_dynar_t split = NULL;
736
737   while ((read = getline(&line, &n, fp)) != -1) {
738
739     /* Wipeout the new line character */
740     line[read - 1] = '\0';
741
742     xbt_str_trim(line, NULL);
743     
744     if(n == 0)
745       continue;
746
747     if(strlen(line) == 0)
748       continue;
749
750     if(debug == 0){
751
752       if(strncmp(line, elf_file, strlen(elf_file)) == 0)
753         continue;
754       
755       if(strncmp(line, "Contents", 8) == 0)
756         continue;
757
758       if(strncmp(line, "Offset", 6) == 0){
759         debug = 1;
760         continue;
761       }
762     }
763
764     if(debug == 0){
765       XBT_INFO("Your program must be compiled with -g");
766       xbt_abort();
767     }
768
769     xbt_dynar_t loclist = xbt_dynar_new(sizeof(dw_location_entry_t), NULL);
770
771     xbt_str_strip_spaces(line);
772     split = xbt_str_split(line, " ");
773
774     while(read != -1 && strcmp("<End", (char *)xbt_dynar_get_as(split, 1, char *)) != 0){
775       
776       dw_location_entry_t new_entry = xbt_new0(s_dw_location_entry_t, 1);
777       new_entry->lowpc = strtoul((char *)xbt_dynar_get_as(split, 1, char *), NULL, 16);
778       new_entry->highpc = strtoul((char *)xbt_dynar_get_as(split, 2, char *), NULL, 16);
779       
780       cursor_remove =0;
781       while(cursor_remove < 3){
782         xbt_dynar_remove_at(split, 0, NULL);
783         cursor_remove++;
784       }
785
786       loc_expr = xbt_str_join(split, " ");
787       xbt_str_ltrim(loc_expr, "(");
788       xbt_str_rtrim(loc_expr, ")");
789       new_entry->location = get_location(NULL, loc_expr);
790
791       xbt_dynar_push(loclist, &new_entry);
792
793       xbt_dynar_free(&split);
794       free(loc_expr);
795
796       read = getline(&line, &n, fp);
797       if(read != -1){
798         line[read - 1] = '\0';
799         xbt_str_strip_spaces(line);
800         split = xbt_str_split(line, " ");
801       }
802
803     }
804
805
806     char *key = bprintf("%d", (int)strtoul((char *)xbt_dynar_get_as(split, 0, char *), NULL, 16));
807     xbt_dict_set(location_list, key, loclist, NULL);
808     
809     xbt_dynar_free(&split);
810
811   }
812
813   free(line);
814   free(command);
815   pclose(fp);
816
817   return location_list;
818 }
819
820 char *get_libsimgrid_path(){
821
822   char *command = bprintf("ldd %s", xbt_binary_name);
823   
824   FILE *fp = popen(command, "r");
825   if(fp == NULL)
826     perror("popen for ldd failed");
827
828   char *line;
829   ssize_t read;
830   size_t n = 0;
831   xbt_dynar_t split;
832   
833   while((read = getline(&line, &n, fp)) != -1){
834   
835     if(n == 0)
836       continue;
837
838     /* Wipeout the new line character */
839     line[read - 1] = '\0';
840
841     xbt_str_strip_spaces(line);
842     xbt_str_ltrim(line, NULL);
843     split = xbt_str_split(line, " ");
844
845     if(strncmp((char *)xbt_dynar_get_as(split, 0, char *), "libsimgrid.so", 13) == 0){
846       free(line);
847       free(command);
848       pclose(fp);
849       return ((char *)xbt_dynar_get_as(split, 2, char *));
850     }
851
852     xbt_dynar_free(&split);
853     
854   }
855
856   free(line);
857   free(command);
858   pclose(fp);
859
860   return NULL;
861   
862 }
863
864 static dw_frame_t get_frame_by_offset(xbt_dict_t all_variables, unsigned long int offset){
865
866   xbt_dict_cursor_t cursor = NULL;
867   char *name;
868   dw_frame_t res;
869
870   xbt_dict_foreach(all_variables, cursor, name, res) {
871     if(offset >= res->start && offset < res->end)
872       return res;
873   }
874
875   return NULL;
876   
877 }
878
879 void MC_get_local_variables(const char *elf_file, xbt_dict_t location_list, xbt_dict_t *all_variables){
880
881   char *command = bprintf("objdump -Wi %s", elf_file);
882   
883   FILE *fp = popen(command, "r");
884
885   if(fp == NULL)
886     perror("popen for objdump failed");
887
888   char *line = NULL, *origin, *abstract_origin, *current_frame = NULL;
889   ssize_t read =0;
890   size_t n = 0;
891   int valid_variable = 1;
892   char *node_type = NULL, *location_type = NULL, *variable_name = NULL, *loc_expr = NULL;
893   xbt_dynar_t split = NULL, split2 = NULL;
894
895   xbt_dict_t variables_origin = xbt_dict_new_homogeneous(NULL);
896   xbt_dict_t subprograms_origin = xbt_dict_new_homogeneous(NULL);
897   char *subprogram_name = NULL, *subprogram_start = NULL, *subprogram_end = NULL;
898   int new_frame = 0, new_variable = 0;
899   dw_frame_t variable_frame, subroutine_frame = NULL;
900
901   read = getline(&line, &n, fp);
902
903   while (read != -1) {
904
905     if(n == 0){
906       read = getline(&line, &n, fp);
907       continue;
908     }
909  
910     /* Wipeout the new line character */
911     line[read - 1] = '\0';
912    
913     if(strlen(line) == 0){
914       read = getline(&line, &n, fp);
915       continue;
916     }
917
918     xbt_str_ltrim(line, NULL);
919     xbt_str_strip_spaces(line);
920     
921     if(line[0] != '<'){
922       read = getline(&line, &n, fp);
923       continue;
924     }
925     
926     xbt_dynar_free(&split);
927     split = xbt_str_split(line, " ");
928
929     /* Get node type */
930     node_type = xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *);
931
932     if(strcmp(node_type, "(DW_TAG_subprogram)") == 0){ /* New frame */
933
934       dw_frame_t frame = NULL;
935
936       strtok(xbt_dynar_get_as(split, 0, char *), "<");
937       subprogram_start = strdup(strtok(NULL, "<"));
938       xbt_str_rtrim(subprogram_start, ">:");
939
940       read = getline(&line, &n, fp);
941    
942       while(read != -1){
943
944         if(n == 0){
945           read = getline(&line, &n, fp);
946           continue;
947         }
948
949         /* Wipeout the new line character */
950         line[read - 1] = '\0';
951         
952         if(strlen(line) == 0){
953           read = getline(&line, &n, fp);
954           continue;
955         }
956       
957         xbt_dynar_free(&split);
958         xbt_str_rtrim(line, NULL);
959         xbt_str_strip_spaces(line);
960         split = xbt_str_split(line, " ");
961           
962         node_type = xbt_dynar_get_as(split, 1, char *);
963
964         if(strncmp(node_type, "DW_AT_", 6) != 0)
965           break;
966
967         if(strcmp(node_type, "DW_AT_sibling") == 0){
968
969           subprogram_end = strdup(xbt_dynar_get_as(split, 3, char*));
970           xbt_str_ltrim(subprogram_end, "<0x");
971           xbt_str_rtrim(subprogram_end, ">");
972           
973         }else if(strcmp(node_type, "DW_AT_abstract_origin:") == 0){ /* Frame already in dict */
974           
975           new_frame = 0;
976           abstract_origin = strdup(xbt_dynar_get_as(split, 2, char*));
977           xbt_str_ltrim(abstract_origin, "<0x");
978           xbt_str_rtrim(abstract_origin, ">");
979           subprogram_name = (char *)xbt_dict_get_or_null(subprograms_origin, abstract_origin);
980           frame = xbt_dict_get_or_null(*all_variables, subprogram_name); 
981
982         }else if(strcmp(node_type, "DW_AT_name") == 0){
983
984           new_frame = 1;
985           free(current_frame);
986           frame = xbt_new0(s_dw_frame_t, 1);
987           frame->name = strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *)); 
988           frame->variables = xbt_dict_new_homogeneous(NULL);
989           frame->frame_base = xbt_new0(s_dw_location_t, 1); 
990           current_frame = strdup(frame->name);
991
992           xbt_dict_set(subprograms_origin, subprogram_start, frame->name, NULL);
993         
994         }else if(strcmp(node_type, "DW_AT_frame_base") == 0){
995
996           location_type = xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *);
997
998           if(strcmp(location_type, "list)") == 0){ /* Search location in location list */
999
1000             frame->frame_base = get_location(location_list, xbt_dynar_get_as(split, 3, char *));
1001              
1002           }else{
1003                 
1004             xbt_str_strip_spaces(line);
1005             split2 = xbt_str_split(line, "(");
1006             xbt_dynar_remove_at(split2, 0, NULL);
1007             loc_expr = xbt_str_join(split2, " ");
1008             xbt_str_rtrim(loc_expr, ")");
1009             frame->frame_base = get_location(NULL, loc_expr);
1010             xbt_dynar_free(&split2);
1011
1012           }
1013  
1014         }else if(strcmp(node_type, "DW_AT_low_pc") == 0){
1015           
1016           if(frame != NULL)
1017             frame->low_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1018
1019         }else if(strcmp(node_type, "DW_AT_high_pc") == 0){
1020
1021           if(frame != NULL)
1022             frame->high_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1023
1024         }else if(strcmp(node_type, "DW_AT_MIPS_linkage_name:") == 0){
1025
1026           free(frame->name);
1027           free(current_frame);
1028           frame->name = strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *));   
1029           current_frame = strdup(frame->name);
1030           xbt_dict_set(subprograms_origin, subprogram_start, frame->name, NULL);
1031
1032         }
1033
1034         read = getline(&line, &n, fp);
1035
1036       }
1037  
1038       if(new_frame == 1){
1039         frame->start = strtoul(subprogram_start, NULL, 16);
1040         if(subprogram_end != NULL)
1041           frame->end = strtoul(subprogram_end, NULL, 16);
1042         xbt_dict_set(*all_variables, frame->name, frame, NULL);
1043       }
1044
1045       free(subprogram_start);
1046       if(subprogram_end != NULL){
1047         free(subprogram_end);
1048         subprogram_end = NULL;
1049       }
1050         
1051
1052     }else if(strcmp(node_type, "(DW_TAG_variable)") == 0){ /* New variable */
1053
1054       dw_local_variable_t var = NULL;
1055       
1056       strtok(xbt_dynar_get_as(split, 0, char *), "<");
1057       origin = strdup(strtok(NULL, "<"));
1058       xbt_str_rtrim(origin, ">:");
1059       
1060       read = getline(&line, &n, fp);
1061       
1062       while(read != -1){
1063
1064         if(n == 0){
1065           read = getline(&line, &n, fp);
1066           continue;
1067         }
1068
1069         /* Wipeout the new line character */
1070         line[read - 1] = '\0'; 
1071
1072         if(strlen(line) == 0){
1073           read = getline(&line, &n, fp);
1074           continue;
1075         }
1076        
1077         xbt_dynar_free(&split);
1078         xbt_str_rtrim(line, NULL);
1079         xbt_str_strip_spaces(line);
1080         split = xbt_str_split(line, " ");
1081   
1082         node_type = xbt_dynar_get_as(split, 1, char *);
1083
1084         if(strncmp(node_type, "DW_AT_", 6) != 0)
1085           break;
1086
1087         if(strcmp(node_type, "DW_AT_name") == 0){
1088
1089           new_variable = 1;
1090           var = xbt_new0(s_dw_local_variable_t, 1);
1091           var->name = strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *));
1092
1093           xbt_dict_set(variables_origin, origin, var->name, NULL);
1094          
1095         }else if(strcmp(node_type, "DW_AT_abstract_origin:") == 0){
1096
1097           new_variable = 0;
1098           abstract_origin = xbt_dynar_get_as(split, 2, char *);
1099           xbt_str_ltrim(abstract_origin, "<0x");
1100           xbt_str_rtrim(abstract_origin, ">");
1101           
1102           variable_name = (char *)xbt_dict_get_or_null(variables_origin, abstract_origin);
1103           variable_frame = get_frame_by_offset(*all_variables, strtoul(abstract_origin, NULL, 16));
1104           var = xbt_dict_get_or_null(variable_frame->variables, variable_name);   
1105
1106         }else if(strcmp(node_type, "DW_AT_location") == 0){
1107
1108           if(valid_variable == 1 && var != NULL){
1109
1110             var->location = xbt_new0(s_dw_location_t, 1);
1111
1112             location_type = xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *);
1113
1114             if(strcmp(location_type, "list)") == 0){ /* Search location in location list */
1115
1116               var->location = get_location(location_list, xbt_dynar_get_as(split, 3, char *));
1117              
1118             }else{
1119                 
1120               xbt_str_strip_spaces(line);
1121               split2 = xbt_str_split(line, "(");
1122               xbt_dynar_remove_at(split2, 0, NULL);
1123               loc_expr = xbt_str_join(split2, " ");
1124               xbt_str_rtrim(loc_expr, ")");
1125               var->location = get_location(NULL, loc_expr);
1126               xbt_dynar_free(&split2);
1127
1128             }
1129
1130           }
1131            
1132         }else if(strcmp(node_type, "DW_AT_external") == 0){
1133
1134           valid_variable = 0;
1135         
1136         }
1137
1138         read = getline(&line, &n, fp);
1139  
1140       }
1141
1142       if(new_variable == 1 && valid_variable == 1){
1143         
1144         variable_frame = xbt_dict_get_or_null(*all_variables, current_frame);
1145         xbt_dict_set(variable_frame->variables, var->name, var, NULL);
1146       }
1147
1148       valid_variable = 1;
1149       new_variable = 0;
1150
1151     }else if(strcmp(node_type, "(DW_TAG_inlined_subroutine)") == 0){
1152
1153       strtok(xbt_dynar_get_as(split, 0, char *), "<");
1154       origin = strdup(strtok(NULL, "<"));
1155       xbt_str_rtrim(origin, ">:");
1156
1157       read = getline(&line, &n, fp);
1158
1159       while(read != -1){
1160
1161         /* Wipeout the new line character */
1162         line[read - 1] = '\0'; 
1163
1164         if(n == 0){
1165           read = getline(&line, &n, fp);
1166           continue;
1167         }
1168
1169         if(strlen(line) == 0){
1170           read = getline(&line, &n, fp);
1171           continue;
1172         }
1173
1174         xbt_dynar_free(&split);
1175         xbt_str_rtrim(line, NULL);
1176         xbt_str_strip_spaces(line);
1177         split = xbt_str_split(line, " ");
1178         
1179         if(strncmp(xbt_dynar_get_as(split, 1, char *), "DW_AT_", 6) != 0)
1180           break;
1181           
1182         node_type = xbt_dynar_get_as(split, 1, char *);
1183
1184         if(strcmp(node_type, "DW_AT_abstract_origin:") == 0){
1185
1186           origin = xbt_dynar_get_as(split, 2, char *);
1187           xbt_str_ltrim(origin, "<0x");
1188           xbt_str_rtrim(origin, ">");
1189           
1190           subprogram_name = (char *)xbt_dict_get_or_null(subprograms_origin, origin);
1191           subroutine_frame = xbt_dict_get_or_null(*all_variables, subprogram_name);
1192         
1193         }else if(strcmp(node_type, "DW_AT_low_pc") == 0){
1194
1195           subroutine_frame->low_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1196
1197         }else if(strcmp(node_type, "DW_AT_high_pc") == 0){
1198
1199           subroutine_frame->high_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1200         }
1201
1202         read = getline(&line, &n, fp);
1203       
1204       }
1205
1206     }else{
1207
1208       read = getline(&line, &n, fp);
1209
1210     }
1211
1212   }
1213   
1214   xbt_dynar_free(&split);
1215   free(line);
1216   free(command);
1217   pclose(fp);
1218   
1219 }
1220
1221 static dw_location_t get_location(xbt_dict_t location_list, char *expr){
1222
1223   dw_location_t loc = xbt_new0(s_dw_location_t, 1);
1224
1225   if(location_list != NULL){
1226     
1227     char *key = bprintf("%d", (int)strtoul(expr, NULL, 16));
1228     loc->type = e_dw_loclist;
1229     loc->location.loclist =  (xbt_dynar_t)xbt_dict_get_or_null(location_list, key);
1230     if(loc == NULL)
1231       XBT_INFO("Key not found in loclist");
1232     return loc;
1233
1234   }else{
1235
1236     int cursor = 0;
1237     char *tok = NULL, *tok2 = NULL; 
1238     
1239     xbt_dynar_t tokens1 = xbt_str_split(expr, ";");
1240     xbt_dynar_t tokens2;
1241
1242     loc->type = e_dw_compose;
1243     loc->location.compose = xbt_dynar_new(sizeof(dw_location_t), NULL);
1244
1245     while(cursor < xbt_dynar_length(tokens1)){
1246
1247       tok = xbt_dynar_get_as(tokens1, cursor, char*);
1248       tokens2 = xbt_str_split(tok, " ");
1249       tok2 = xbt_dynar_get_as(tokens2, 0, char*);
1250       
1251       if(strncmp(tok2, "DW_OP_reg", 9) == 0){
1252         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1253         new_element->type = e_dw_register;
1254         new_element->location.reg = atoi(strtok(tok2, "DW_OP_reg"));
1255         xbt_dynar_push(loc->location.compose, &new_element);     
1256       }else if(strcmp(tok2, "DW_OP_fbreg:") == 0){
1257         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1258         new_element->type = e_dw_fbregister_op;
1259         new_element->location.fbreg_op = atoi(xbt_dynar_get_as(tokens2, 1, char*));
1260         xbt_dynar_push(loc->location.compose, &new_element);
1261       }else if(strncmp(tok2, "DW_OP_breg", 10) == 0){
1262         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1263         new_element->type = e_dw_bregister_op;
1264         new_element->location.breg_op.reg = atoi(strtok(tok2, "DW_OP_breg"));
1265         new_element->location.breg_op.offset = atoi(xbt_dynar_get_as(tokens2, 2, char*));
1266         xbt_dynar_push(loc->location.compose, &new_element);
1267       }else if(strncmp(tok2, "DW_OP_lit", 9) == 0){
1268         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1269         new_element->type = e_dw_lit;
1270         new_element->location.lit = atoi(strtok(tok2, "DW_OP_lit"));
1271         xbt_dynar_push(loc->location.compose, &new_element);
1272       }else if(strcmp(tok2, "DW_OP_piece:") == 0){
1273         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1274         new_element->type = e_dw_piece;
1275         new_element->location.piece = atoi(xbt_dynar_get_as(tokens2, 1, char*));
1276         /*if(strlen(xbt_dynar_get_as(tokens2, 1, char*)) > 1)
1277           new_element->location.piece = atoi(xbt_dynar_get_as(tokens2, 1, char*));
1278         else
1279         new_element->location.piece = xbt_dynar_get_as(tokens2, 1, char*)[0] - '0';*/
1280         xbt_dynar_push(loc->location.compose, &new_element);
1281       }else if(strcmp(tok2, "DW_OP_plus_uconst:") == 0){
1282         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1283         new_element->type = e_dw_plus_uconst;
1284         new_element->location.plus_uconst = atoi(xbt_dynar_get_as(tokens2, 1, char *));
1285         xbt_dynar_push(loc->location.compose, &new_element);
1286       }else if(strcmp(tok, "DW_OP_abs") == 0 || 
1287                strcmp(tok, "DW_OP_and") == 0 ||
1288                strcmp(tok, "DW_OP_div") == 0 ||
1289                strcmp(tok, "DW_OP_minus") == 0 ||
1290                strcmp(tok, "DW_OP_mod") == 0 ||
1291                strcmp(tok, "DW_OP_mul") == 0 ||
1292                strcmp(tok, "DW_OP_neg") == 0 ||
1293                strcmp(tok, "DW_OP_not") == 0 ||
1294                strcmp(tok, "DW_OP_or") == 0 ||
1295                strcmp(tok, "DW_OP_plus") == 0){               
1296         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1297         new_element->type = e_dw_arithmetic;
1298         new_element->location.arithmetic = strdup(strtok(tok2, "DW_OP_"));
1299         xbt_dynar_push(loc->location.compose, &new_element);
1300       }else if(strcmp(tok, "DW_OP_stack_value") == 0){
1301       }else if(strcmp(tok2, "DW_OP_deref_size:") == 0){
1302         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1303         new_element->type = e_dw_deref;
1304         new_element->location.deref_size = (unsigned int short) atoi(xbt_dynar_get_as(tokens2, 1, char*));
1305         /*if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1306           new_element->location.deref_size = atoi(xbt_dynar_get_as(tokens, cursor, char*));
1307         else
1308         new_element->location.deref_size = xbt_dynar_get_as(tokens, cursor, char*)[0] - '0';*/
1309         xbt_dynar_push(loc->location.compose, &new_element);
1310       }else if(strcmp(tok, "DW_OP_deref") == 0){
1311         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1312         new_element->type = e_dw_deref;
1313         new_element->location.deref_size = sizeof(void *);
1314         xbt_dynar_push(loc->location.compose, &new_element);
1315       }else if(strcmp(tok2, "DW_OP_constu:") == 0){
1316         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1317         new_element->type = e_dw_uconstant;
1318         new_element->location.uconstant.bytes = 1;
1319         new_element->location.uconstant.value = (unsigned long int)(atoi(xbt_dynar_get_as(tokens2, 1, char*)));
1320         /*if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1321           new_element->location.uconstant.value = (unsigned long int)(atoi(xbt_dynar_get_as(tokens, cursor, char*)));
1322         else
1323         new_element->location.uconstant.value = (unsigned long int)(xbt_dynar_get_as(tokens, cursor, char*)[0] - '0');*/
1324         xbt_dynar_push(loc->location.compose, &new_element);
1325       }else if(strcmp(tok2, "DW_OP_consts:") == 0){
1326         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1327         new_element->type = e_dw_sconstant;
1328         new_element->location.sconstant.bytes = 1;
1329         new_element->location.sconstant.value = (long int)(atoi(xbt_dynar_get_as(tokens2, 1, char*)));
1330         xbt_dynar_push(loc->location.compose, &new_element);
1331       }else if(strcmp(tok2, "DW_OP_const1u:") == 0 ||
1332                strcmp(tok2, "DW_OP_const2u:") == 0 ||
1333                strcmp(tok2, "DW_OP_const4u:") == 0 ||
1334                strcmp(tok2, "DW_OP_const8u:") == 0){
1335         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1336         new_element->type = e_dw_uconstant;
1337         new_element->location.uconstant.bytes = tok2[11] - '0';
1338         new_element->location.uconstant.value = (unsigned long int)(atoi(xbt_dynar_get_as(tokens2, 1, char*)));
1339         /*if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1340           new_element->location.constant.value = atoi(xbt_dynar_get_as(tokens, cursor, char*));
1341         else
1342         new_element->location.constant.value = xbt_dynar_get_as(tokens, cursor, char*)[0] - '0';*/
1343         xbt_dynar_push(loc->location.compose, &new_element);
1344       }else if(strcmp(tok, "DW_OP_const1s") == 0 ||
1345                strcmp(tok, "DW_OP_const2s") == 0 ||
1346                strcmp(tok, "DW_OP_const4s") == 0 ||
1347                strcmp(tok, "DW_OP_const8s") == 0){
1348         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1349         new_element->type = e_dw_sconstant;
1350         new_element->location.sconstant.bytes = tok2[11] - '0';
1351         new_element->location.sconstant.value = (long int)(atoi(xbt_dynar_get_as(tokens2, 1, char*)));
1352         xbt_dynar_push(loc->location.compose, &new_element);
1353       }else{
1354         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1355         new_element->type = e_dw_unsupported;
1356         xbt_dynar_push(loc->location.compose, &new_element);
1357       }
1358
1359       cursor++;
1360       xbt_dynar_free(&tokens2);
1361
1362     }
1363     
1364     xbt_dynar_free(&tokens1);
1365
1366     return loc;
1367     
1368   }
1369
1370 }
1371
1372
1373 void print_local_variables(xbt_dict_t list){
1374   
1375   dw_location_entry_t entry;
1376   dw_location_t location_entry;
1377   unsigned int cursor3 = 0, cursor4 = 0;
1378   xbt_dict_cursor_t cursor = 0, cursor2 = 0;
1379
1380   char *frame_name, *variable_name;
1381   dw_frame_t current_frame;
1382   dw_local_variable_t current_variable;
1383
1384   xbt_dict_foreach(list, cursor, frame_name, current_frame){ 
1385     fprintf(stderr, "Frame name : %s\n", current_frame->name);
1386     fprintf(stderr, "Location type : %d\n", current_frame->frame_base->type);
1387     xbt_dict_foreach((xbt_dict_t)current_frame->variables, cursor2, variable_name, current_variable){
1388       fprintf(stderr, "Name : %s\n", current_variable->name);
1389       if(current_variable->location == NULL)
1390         continue;
1391       fprintf(stderr, "Location type : %d\n", current_variable->location->type);
1392       switch(current_variable->location->type){
1393       case e_dw_loclist :
1394         xbt_dynar_foreach(current_variable->location->location.loclist, cursor3, entry){
1395           fprintf(stderr, "Lowpc : %lx, Highpc : %lx,", entry->lowpc, entry->highpc);
1396           switch(entry->location->type){
1397           case e_dw_register :
1398             fprintf(stderr, " Location : in register %d\n", entry->location->location.reg);
1399             break;
1400           case e_dw_bregister_op:
1401             fprintf(stderr, " Location : Add %d to the value in register %d\n", entry->location->location.breg_op.offset, entry->location->location.breg_op.reg);
1402             break;
1403           case e_dw_lit:
1404             fprintf(stderr, "Value already kwnown : %d\n", entry->location->location.lit);
1405             break;
1406           case e_dw_fbregister_op:
1407             fprintf(stderr, " Location : %d bytes from logical frame pointer\n", entry->location->location.fbreg_op);
1408             break;
1409           case e_dw_compose:
1410             fprintf(stderr, " Location :\n");
1411             xbt_dynar_foreach(entry->location->location.compose, cursor4, location_entry){
1412               switch(location_entry->type){
1413               case e_dw_register :
1414                 fprintf(stderr, " %d) in register %d\n", cursor4 + 1, location_entry->location.reg);
1415                 break;
1416               case e_dw_bregister_op:
1417                 fprintf(stderr, " %d) add %d to the value in register %d\n", cursor4 + 1, location_entry->location.breg_op.offset, location_entry->location.breg_op.reg);
1418                 break;
1419               case e_dw_lit:
1420                 fprintf(stderr, "%d) Value already kwnown : %d\n", cursor4 + 1, location_entry->location.lit);
1421                 break;
1422               case e_dw_fbregister_op:
1423                 fprintf(stderr, " %d) %d bytes from logical frame pointer\n", cursor4 + 1, location_entry->location.fbreg_op);
1424                 break;
1425               case e_dw_deref:
1426                 fprintf(stderr, " %d) Pop the stack entry and treats it as an address (size of data %d)\n", cursor4 + 1, location_entry->location.deref_size);
1427                 break;
1428               case e_dw_arithmetic :
1429                 fprintf(stderr, "%d) arithmetic operation : %s\n", cursor4 + 1, location_entry->location.arithmetic);
1430                 break;
1431               case e_dw_piece:
1432                 fprintf(stderr, "%d) The %d byte(s) previous value\n", cursor4 + 1, location_entry->location.piece);
1433                 break;
1434               case e_dw_uconstant :
1435                 fprintf(stderr, "%d) Unsigned constant %lu\n", cursor4 + 1, location_entry->location.uconstant.value);
1436                 break;
1437               case e_dw_sconstant :
1438                 fprintf(stderr, "%d) Signed constant %lu\n", cursor4 + 1, location_entry->location.sconstant.value);
1439                 break;
1440               default :
1441                 fprintf(stderr, "%d) Location type not supported\n", cursor4 + 1);
1442                 break;
1443               }
1444             }
1445             break;
1446           default:
1447             fprintf(stderr, "Location type not supported\n");
1448             break;
1449           }
1450         }
1451         break;
1452       case e_dw_compose:
1453         cursor4 = 0;
1454         fprintf(stderr, "Location :\n");
1455         xbt_dynar_foreach(current_variable->location->location.compose, cursor4, location_entry){
1456           switch(location_entry->type){
1457           case e_dw_register :
1458             fprintf(stderr, " %d) in register %d\n", cursor4 + 1, location_entry->location.reg);
1459             break;
1460           case e_dw_bregister_op:
1461             fprintf(stderr, " %d) add %d to the value in register %d\n", cursor4 + 1, location_entry->location.breg_op.offset, location_entry->location.breg_op.reg);
1462             break;
1463           case e_dw_lit:
1464             fprintf(stderr, "%d) Value already kwnown : %d\n", cursor4 + 1, location_entry->location.lit);
1465             break;
1466           case e_dw_fbregister_op:
1467             fprintf(stderr, " %d) %d bytes from logical frame pointer\n", cursor4 + 1, location_entry->location.fbreg_op);
1468             break;
1469           case e_dw_deref:
1470             fprintf(stderr, " %d) Pop the stack entry and treats it as an address (size of data %d)\n", cursor4 + 1, location_entry->location.deref_size);
1471             break;
1472           case e_dw_arithmetic :
1473             fprintf(stderr, "%d) arithmetic operation : %s\n", cursor4 + 1, location_entry->location.arithmetic);
1474             break;
1475           case e_dw_piece:
1476             fprintf(stderr, "%d) The %d byte(s) previous value\n", cursor4 + 1, location_entry->location.piece);
1477             break;
1478           case e_dw_uconstant :
1479             fprintf(stderr, "%d) Unsigned constant %lu\n", cursor4 + 1, location_entry->location.uconstant.value);
1480             break;
1481           case e_dw_sconstant :
1482             fprintf(stderr, "%d) Signed constant %lu\n", cursor4 + 1, location_entry->location.sconstant.value);
1483             break;
1484           default :
1485             fprintf(stderr, "%d) Location type not supported\n", cursor4 + 1);
1486             break;
1487           }
1488         }
1489         break;
1490       default :
1491         fprintf(stderr, "Location type not supported\n");
1492         break;
1493       }
1494     }
1495   }
1496
1497 }