Logo AND Algorithmique Numérique Distribuée

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