Logo AND Algorithmique Numérique Distribuée

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