Logo AND Algorithmique Numérique Distribuée

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