Logo AND Algorithmique Numérique Distribuée

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