Logo AND Algorithmique Numérique Distribuée

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