Logo AND Algorithmique Numérique Distribuée

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