Logo AND Algorithmique Numérique Distribuée

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