Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f1e1c922b9abb1e6bd2150e5aa8ad80e168bc8e5
[simgrid.git] / src / mc / mc_global.c
1 /* Copyright (c) 2008-2013. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <unistd.h>
8 #include <sys/types.h>
9 #include <sys/wait.h>
10 #include <sys/time.h>
11
12 #include "simgrid/sg_config.h"
13 #include "../surf/surf_private.h"
14 #include "../simix/smx_private.h"
15 #include "../xbt/mmalloc/mmprivate.h"
16 #include "xbt/fifo.h"
17 #include "mc_private.h"
18 #include "xbt/automaton.h"
19 #include "xbt/dict.h"
20
21 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
23                                 "Logging specific to MC (global)");
24
25 /* Configuration support */
26 e_mc_reduce_t mc_reduce_kind=e_mc_reduce_unset;
27
28 int _sg_do_model_check = 0;
29 int _sg_mc_checkpoint=0;
30 char* _sg_mc_property_file=NULL;
31 int _sg_mc_timeout=0;
32 int _sg_mc_max_depth=1000;
33 int _sg_mc_visited=0;
34 char *_sg_mc_dot_output_file = NULL;
35
36 int user_max_depth_reached = 0;
37
38 void _mc_cfg_cb_reduce(const char *name, int pos) {
39   if (_sg_cfg_init_status && !_sg_do_model_check) {
40     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.");
41   }
42   char *val= xbt_cfg_get_string(_sg_cfg_set, name);
43   if (!strcasecmp(val,"none")) {
44     mc_reduce_kind = e_mc_reduce_none;
45   } else if (!strcasecmp(val,"dpor")) {
46     mc_reduce_kind = e_mc_reduce_dpor;
47   } else {
48     xbt_die("configuration option %s can only take 'none' or 'dpor' as a value",name);
49   }
50 }
51
52 void _mc_cfg_cb_checkpoint(const char *name, int pos) {
53   if (_sg_cfg_init_status && !_sg_do_model_check) {
54     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.");
55   }
56   _sg_mc_checkpoint = xbt_cfg_get_int(_sg_cfg_set, name);
57 }
58 void _mc_cfg_cb_property(const char *name, int pos) {
59   if (_sg_cfg_init_status && !_sg_do_model_check) {
60     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.");
61   }
62   _sg_mc_property_file= xbt_cfg_get_string(_sg_cfg_set, name);
63 }
64
65 void _mc_cfg_cb_timeout(const char *name, int pos) {
66   if (_sg_cfg_init_status && !_sg_do_model_check) {
67     xbt_die("You are specifying a value to enable/disable timeout for wait requests after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
68   }
69   _sg_mc_timeout= xbt_cfg_get_boolean(_sg_cfg_set, name);
70 }
71
72 void _mc_cfg_cb_max_depth(const char *name, int pos) {
73   if (_sg_cfg_init_status && !_sg_do_model_check) {
74     xbt_die("You are specifying a max depth value after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
75   }
76   _sg_mc_max_depth= xbt_cfg_get_int(_sg_cfg_set, name);
77 }
78
79 void _mc_cfg_cb_visited(const char *name, int pos) {
80   if (_sg_cfg_init_status && !_sg_do_model_check) {
81     xbt_die("You are specifying a number of stored visited states after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
82   }
83   _sg_mc_visited= xbt_cfg_get_int(_sg_cfg_set, name);
84 }
85
86 void _mc_cfg_cb_dot_output(const char *name, int pos) {
87   if (_sg_cfg_init_status && !_sg_do_model_check) {
88     xbt_die("You are specifying a file name for a dot output of graph state 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.");
89   }
90   _sg_mc_dot_output_file= xbt_cfg_get_string(_sg_cfg_set, name);
91 }
92
93 /* MC global data structures */
94 mc_state_t mc_current_state = NULL;
95 char mc_replay_mode = FALSE;
96 double *mc_time = NULL;
97 __thread mc_comparison_times_t mc_comp_times = NULL;
98 __thread double mc_snapshot_comparison_time;
99 mc_stats_t mc_stats = NULL;
100
101 /* Safety */
102 xbt_fifo_t mc_stack_safety = NULL;
103 mc_global_t initial_state_safety = NULL;
104
105 /* Liveness */
106 xbt_fifo_t mc_stack_liveness = NULL;
107 mc_global_t initial_state_liveness = NULL;
108 int compare;
109
110 xbt_automaton_t _mc_property_automaton = NULL;
111
112 /* Variables */
113 xbt_dict_t mc_local_variables_libsimgrid = NULL;
114 xbt_dict_t mc_local_variables_binary = NULL;
115 xbt_dynar_t mc_global_variables_libsimgrid = NULL;
116 xbt_dynar_t mc_global_variables_binary = NULL;
117 xbt_dict_t mc_variables_type_libsimgrid = NULL;
118 xbt_dict_t mc_variables_type_binary = NULL;
119
120 /* Ignore mechanism */
121 xbt_dynar_t mc_stack_comparison_ignore;
122 xbt_dynar_t mc_data_bss_comparison_ignore;
123 extern xbt_dynar_t mc_heap_comparison_ignore;
124 extern xbt_dynar_t stacks_areas;
125
126 /* Dot output */
127 FILE *dot_output = NULL;
128 const char* colors[13];
129
130
131 /*******************************  DWARF Information *******************************/
132 /**********************************************************************************/
133
134 /************************** Free functions *************************/
135
136 static void dw_location_free(dw_location_t l){
137   if(l){
138     if(l->type == e_dw_loclist)
139       xbt_dynar_free(&(l->location.loclist));
140     else if(l->type == e_dw_compose)
141       xbt_dynar_free(&(l->location.compose));
142     else if(l->type == e_dw_arithmetic)
143       xbt_free(l->location.arithmetic);
144   
145     xbt_free(l);
146   }
147 }
148
149 static void dw_location_entry_free(dw_location_entry_t e){
150   dw_location_free(e->location);
151   xbt_free(e);
152 }
153
154 static void dw_type_free(dw_type_t t){
155   xbt_free(t->name);
156   xbt_free(t->dw_type_id);
157   xbt_dynar_free(&(t->members));
158   xbt_free(t);
159 }
160
161 static void dw_type_free_voidp(void *t){
162   dw_type_free((dw_type_t) * (void **) t);
163 }
164
165 static void dw_variable_free(dw_variable_t v){
166   if(v){
167     xbt_free(v->name);
168     xbt_free(v->type_origin);
169     if(!v->global)
170       dw_location_free(v->address.location);
171     xbt_free(v);
172   }
173 }
174
175 static void dw_variable_free_voidp(void *t){
176   dw_variable_free((dw_variable_t) * (void **) t);
177 }
178
179 /*************************************************************************/
180
181 static dw_location_t MC_dwarf_get_location(xbt_dict_t location_list, char *expr){
182
183   dw_location_t loc = xbt_new0(s_dw_location_t, 1);
184
185   if(location_list != NULL){
186     
187     char *key = bprintf("%d", (int)strtoul(expr, NULL, 16));
188     loc->type = e_dw_loclist;
189     loc->location.loclist =  (xbt_dynar_t)xbt_dict_get_or_null(location_list, key);
190     if(loc->location.loclist == NULL)
191       XBT_INFO("Key not found in loclist");
192     xbt_free(key);
193     return loc;
194
195   }else{
196
197     int cursor = 0;
198     char *tok = NULL, *tok2 = NULL; 
199     
200     xbt_dynar_t tokens1 = xbt_str_split(expr, ";");
201     xbt_dynar_t tokens2;
202
203     loc->type = e_dw_compose;
204     loc->location.compose = xbt_dynar_new(sizeof(dw_location_t), NULL);
205
206     while(cursor < xbt_dynar_length(tokens1)){
207
208       tok = xbt_dynar_get_as(tokens1, cursor, char*);
209       tokens2 = xbt_str_split(tok, " ");
210       tok2 = xbt_dynar_get_as(tokens2, 0, char*);
211       
212       if(strncmp(tok2, "DW_OP_reg", 9) == 0){
213         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
214         new_element->type = e_dw_register;
215         new_element->location.reg = atoi(strtok(tok2, "DW_OP_reg"));
216         xbt_dynar_push(loc->location.compose, &new_element);     
217       }else if(strcmp(tok2, "DW_OP_fbreg:") == 0){
218         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
219         new_element->type = e_dw_fbregister_op;
220         new_element->location.fbreg_op = atoi(xbt_dynar_get_as(tokens2, xbt_dynar_length(tokens2) - 1, char*));
221         xbt_dynar_push(loc->location.compose, &new_element);
222       }else if(strncmp(tok2, "DW_OP_breg", 10) == 0){
223         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
224         new_element->type = e_dw_bregister_op;
225         new_element->location.breg_op.reg = atoi(strtok(tok2, "DW_OP_breg"));
226         new_element->location.breg_op.offset = atoi(xbt_dynar_get_as(tokens2, xbt_dynar_length(tokens2) - 1, char*));
227         xbt_dynar_push(loc->location.compose, &new_element);
228       }else if(strncmp(tok2, "DW_OP_lit", 9) == 0){
229         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
230         new_element->type = e_dw_lit;
231         new_element->location.lit = atoi(strtok(tok2, "DW_OP_lit"));
232         xbt_dynar_push(loc->location.compose, &new_element);
233       }else if(strcmp(tok2, "DW_OP_piece:") == 0){
234         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
235         new_element->type = e_dw_piece;
236         new_element->location.piece = atoi(xbt_dynar_get_as(tokens2, xbt_dynar_length(tokens2) - 1, char*));
237         xbt_dynar_push(loc->location.compose, &new_element);
238       }else if(strcmp(tok2, "DW_OP_plus_uconst:") == 0){
239         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
240         new_element->type = e_dw_plus_uconst;
241         new_element->location.plus_uconst = atoi(xbt_dynar_get_as(tokens2, xbt_dynar_length(tokens2) - 1, char *));
242         xbt_dynar_push(loc->location.compose, &new_element);
243       }else if(strcmp(tok, "DW_OP_abs") == 0 || 
244                strcmp(tok, "DW_OP_and") == 0 ||
245                strcmp(tok, "DW_OP_div") == 0 ||
246                strcmp(tok, "DW_OP_minus") == 0 ||
247                strcmp(tok, "DW_OP_mod") == 0 ||
248                strcmp(tok, "DW_OP_mul") == 0 ||
249                strcmp(tok, "DW_OP_neg") == 0 ||
250                strcmp(tok, "DW_OP_not") == 0 ||
251                strcmp(tok, "DW_OP_or") == 0 ||
252                strcmp(tok, "DW_OP_plus") == 0){               
253         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
254         new_element->type = e_dw_arithmetic;
255         new_element->location.arithmetic = strdup(strtok(tok2, "DW_OP_"));
256         xbt_dynar_push(loc->location.compose, &new_element);
257       }else if(strcmp(tok, "DW_OP_stack_value") == 0){
258       }else if(strcmp(tok2, "DW_OP_deref_size:") == 0){
259         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
260         new_element->type = e_dw_deref;
261         new_element->location.deref_size = (unsigned int short) atoi(xbt_dynar_get_as(tokens2, xbt_dynar_length(tokens2) - 1, char*));
262         xbt_dynar_push(loc->location.compose, &new_element);
263       }else if(strcmp(tok, "DW_OP_deref") == 0){
264         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
265         new_element->type = e_dw_deref;
266         new_element->location.deref_size = sizeof(void *);
267         xbt_dynar_push(loc->location.compose, &new_element);
268       }else if(strcmp(tok2, "DW_OP_constu:") == 0){
269         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
270         new_element->type = e_dw_uconstant;
271         new_element->location.uconstant.bytes = 1;
272         new_element->location.uconstant.value = (unsigned long int)(atoi(xbt_dynar_get_as(tokens2, xbt_dynar_length(tokens2) - 1, char*)));
273         xbt_dynar_push(loc->location.compose, &new_element);
274       }else if(strcmp(tok2, "DW_OP_consts:") == 0){
275         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
276         new_element->type = e_dw_sconstant;
277         new_element->location.sconstant.bytes = 1;
278         new_element->location.sconstant.value = (long int)(atoi(xbt_dynar_get_as(tokens2, xbt_dynar_length(tokens2) - 1, char*)));
279         xbt_dynar_push(loc->location.compose, &new_element);
280       }else if(strcmp(tok2, "DW_OP_const1u:") == 0 ||
281                strcmp(tok2, "DW_OP_const2u:") == 0 ||
282                strcmp(tok2, "DW_OP_const4u:") == 0 ||
283                strcmp(tok2, "DW_OP_const8u:") == 0){
284         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
285         new_element->type = e_dw_uconstant;
286         new_element->location.uconstant.bytes = tok2[11] - '0';
287         new_element->location.uconstant.value = (unsigned long int)(atoi(xbt_dynar_get_as(tokens2, xbt_dynar_length(tokens2) - 1, char*)));
288         xbt_dynar_push(loc->location.compose, &new_element);
289       }else if(strcmp(tok, "DW_OP_const1s") == 0 ||
290                strcmp(tok, "DW_OP_const2s") == 0 ||
291                strcmp(tok, "DW_OP_const4s") == 0 ||
292                strcmp(tok, "DW_OP_const8s") == 0){
293         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
294         new_element->type = e_dw_sconstant;
295         new_element->location.sconstant.bytes = tok2[11] - '0';
296         new_element->location.sconstant.value = (long int)(atoi(xbt_dynar_get_as(tokens2, xbt_dynar_length(tokens2) - 1, char*)));
297         xbt_dynar_push(loc->location.compose, &new_element);
298       }else{
299         dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
300         new_element->type = e_dw_unsupported;
301         xbt_dynar_push(loc->location.compose, &new_element);
302       }
303
304       cursor++;
305       xbt_dynar_free(&tokens2);
306
307     }
308     
309     xbt_dynar_free(&tokens1);
310
311     return loc;
312     
313   }
314
315 }
316
317 /** @brief Extract the location lists from an ELF file (.debug_loc)
318  *
319  *  @return A map from the offset in the list (in hexadecimal string)
320  *          into a location list (dynar of dw_location_entry_t).
321  */
322 static xbt_dict_t MC_dwarf_get_location_list(const char *elf_file){
323
324   char *command = bprintf("LANG=C objdump -Wo %s", elf_file);
325
326   FILE *fp = popen(command, "r");
327
328   if(fp == NULL){
329     perror("popen for objdump failed");
330     xbt_abort();
331   }
332
333   int debug = 0; /*Detect if the program has been compiled with -g */
334
335   xbt_dict_t location_list = xbt_dict_new_homogeneous(NULL);
336   char *line = NULL, *loc_expr = NULL;
337   ssize_t read;
338   size_t n = 0;
339   int cursor_remove;
340   xbt_dynar_t split = NULL;
341
342   while ((read = xbt_getline(&line, &n, fp)) != -1) {
343
344     /* Wipeout the new line character */
345     line[read - 1] = '\0';
346
347     xbt_str_trim(line, NULL);
348     
349     if(n == 0)
350       continue;
351
352     if(strlen(line) == 0)
353       continue;
354
355     if(debug == 0){
356
357       if(strncmp(line, elf_file, strlen(elf_file)) == 0)
358         continue;
359       
360       if(strncmp(line, "Contents", 8) == 0)
361         continue;
362
363       if(strncmp(line, "Offset", 6) == 0){
364         debug = 1;
365         continue;
366       }
367     }
368
369     if(debug == 0){
370       XBT_INFO("Your program must be compiled with -g");
371       xbt_abort();
372     }
373
374     xbt_dynar_t loclist = xbt_dynar_new(sizeof(dw_location_entry_t), NULL);
375
376     xbt_str_strip_spaces(line);
377     split = xbt_str_split(line, " ");
378
379     char *key = NULL;
380     while(read != -1 && strcmp("<End", (char *)xbt_dynar_get_as(split, 1, char *)) != 0){
381       
382       // Take the key from the first line of the list:
383       if(key==NULL){
384         key = bprintf("%d", (int)strtoul((char *)xbt_dynar_get_as(split, 0, char *), NULL, 16));
385       }
386
387       dw_location_entry_t new_entry = xbt_new0(s_dw_location_entry_t, 1);
388       new_entry->lowpc = strtoul((char *)xbt_dynar_get_as(split, 1, char *), NULL, 16);
389       new_entry->highpc = strtoul((char *)xbt_dynar_get_as(split, 2, char *), NULL, 16);
390       
391       cursor_remove =0;
392       while(cursor_remove < 3){
393         xbt_dynar_remove_at(split, 0, NULL);
394         cursor_remove++;
395       }
396
397       loc_expr = xbt_str_join(split, " ");
398       xbt_str_ltrim(loc_expr, "(");
399       xbt_str_rtrim(loc_expr, ")");
400       new_entry->location = MC_dwarf_get_location(NULL, loc_expr);
401
402       xbt_dynar_push(loclist, &new_entry);
403
404       xbt_dynar_free(&split);
405       free(loc_expr);
406
407       read = xbt_getline(&line, &n, fp);
408       if(read != -1){
409         line[read - 1] = '\0';
410         xbt_str_strip_spaces(line);
411         split = xbt_str_split(line, " ");
412       }
413
414     }
415
416
417     xbt_dict_set(location_list, key, loclist, NULL);
418     xbt_free(key);
419     
420     xbt_dynar_free(&split);
421
422   }
423
424   xbt_free(line);
425   xbt_free(command);
426   pclose(fp);
427
428   return location_list;
429 }
430
431 static dw_frame_t MC_dwarf_get_frame_by_offset(xbt_dict_t all_variables, unsigned long int offset){
432
433   xbt_dict_cursor_t cursor = NULL;
434   char *name;
435   dw_frame_t res;
436
437   xbt_dict_foreach(all_variables, cursor, name, res) {
438     if(offset >= res->start && offset < res->end){
439       xbt_dict_cursor_free(&cursor);
440       return res;
441     }
442   }
443
444   xbt_dict_cursor_free(&cursor);
445   return NULL;
446   
447 }
448
449 static dw_variable_t MC_dwarf_get_variable_by_name(dw_frame_t frame, char *var){
450
451   unsigned int cursor = 0;
452   dw_variable_t current_var;
453
454   xbt_dynar_foreach(frame->variables, cursor, current_var){
455     if(strcmp(var, current_var->name) == 0)
456       return current_var;
457   }
458
459   return NULL;
460 }
461
462 static int MC_dwarf_get_variable_index(xbt_dynar_t variables, char* var, void *address){
463
464   if(xbt_dynar_is_empty(variables))
465     return 0;
466
467   unsigned int cursor = 0;
468   int start = 0;
469   int end = xbt_dynar_length(variables) - 1;
470   dw_variable_t var_test = NULL;
471
472   while(start <= end){
473     cursor = (start + end) / 2;
474     var_test = (dw_variable_t)xbt_dynar_get_as(variables, cursor, dw_variable_t);
475     if(strcmp(var_test->name, var) < 0){
476       start = cursor + 1;
477     }else if(strcmp(var_test->name, var) > 0){
478       end = cursor - 1;
479     }else{
480       if(address){ /* global variable */
481         if(var_test->address.address == address)
482           return -1;
483         if(var_test->address.address > address)
484           end = cursor - 1;
485         else
486           start = cursor + 1;
487       }else{ /* local variable */
488         return -1;
489       }
490     }
491   }
492
493   if(strcmp(var_test->name, var) == 0){
494     if(address && var_test->address.address < address)
495       return cursor+1;
496     else
497       return cursor;
498   }else if(strcmp(var_test->name, var) < 0)
499     return cursor+1;
500   else
501     return cursor;
502
503 }
504
505
506 static void MC_dwarf_get_variables(const char *elf_file, xbt_dict_t location_list, xbt_dict_t *local_variables, xbt_dynar_t *global_variables, xbt_dict_t *types){
507
508   char *command = bprintf("LANG=C objdump -Wi %s", elf_file);
509   
510   FILE *fp = popen(command, "r");
511
512   if(fp == NULL)
513     perror("popen for objdump failed");
514
515   char *line = NULL, *origin, *abstract_origin, *current_frame = NULL, 
516     *subprogram_name = NULL, *subprogram_start = NULL, *subprogram_end = NULL,
517     *node_type = NULL, *location_type = NULL, *variable_name = NULL, 
518     *loc_expr = NULL, *name = NULL, *end =NULL, *type_origin = NULL, *global_address = NULL, 
519     *parent_value = NULL;
520
521   ssize_t read =0;
522   size_t n = 0;
523   int global_variable = 0, parent = 0, new_frame = 0, new_variable = 1, size = 0, 
524     is_pointer = 0, struct_decl = 0, member_end = 0,
525     enumeration_size = 0, subrange = 0, union_decl = 0, offset = 0, index = 0;
526   
527   xbt_dynar_t split = NULL, split2 = NULL;
528
529   xbt_dict_t variables_origin = xbt_dict_new_homogeneous(xbt_free);
530   xbt_dict_t subprograms_origin = xbt_dict_new_homogeneous(xbt_free);
531
532   dw_frame_t variable_frame, subroutine_frame = NULL;
533
534   e_dw_type_type type_type = -1;
535
536   read = xbt_getline(&line, &n, fp);
537
538   while (read != -1) {
539
540     /* Wipeout the new line character */
541     line[read - 1] = '\0';
542   
543     if(n == 0 || strlen(line) == 0){
544       read = xbt_getline(&line, &n, fp);
545       continue;
546     }
547
548     xbt_str_ltrim(line, NULL);
549     xbt_str_strip_spaces(line);
550     
551     if(line[0] != '<'){
552       read = xbt_getline(&line, &n, fp);
553       continue;
554     }
555     
556     xbt_dynar_free(&split);
557     split = xbt_str_split(line, " ");
558
559     /* Get node type */
560     node_type = xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *);
561
562     if(strcmp(node_type, "(DW_TAG_subprogram)") == 0){ /* New frame */
563       /* We build/complete a dw_frame_t object
564        * and append it if necessary to the local_variables dictionnary */
565
566       dw_frame_t frame = NULL;
567
568       strtok(xbt_dynar_get_as(split, 0, char *), "<");
569       subprogram_start = xbt_strdup(strtok(NULL, "<"));
570       xbt_str_rtrim(subprogram_start, ">:");
571
572       read = xbt_getline(&line, &n, fp);
573    
574       while(read != -1){
575
576         /* Wipeout the new line character */
577         line[read - 1] = '\0';
578
579         if(n == 0 || strlen(line) == 0){
580           read = xbt_getline(&line, &n, fp);
581           continue;
582         }
583         
584         xbt_dynar_free(&split);
585         xbt_str_rtrim(line, NULL);
586         xbt_str_strip_spaces(line);
587         split = xbt_str_split(line, " ");
588           
589         node_type = xbt_dynar_get_as(split, 1, char *);
590
591         if(strncmp(node_type, "DW_AT_", 6) != 0)
592           break;
593
594         if(strcmp(node_type, "DW_AT_sibling") == 0){
595
596           subprogram_end = xbt_strdup(xbt_dynar_get_as(split, 3, char*));
597           xbt_str_ltrim(subprogram_end, "<0x");
598           xbt_str_rtrim(subprogram_end, ">");
599           
600         }else if(strcmp(node_type, "DW_AT_abstract_origin:") == 0){ /* Frame already in dict */
601           
602           new_frame = 0;
603           abstract_origin = xbt_strdup(xbt_dynar_get_as(split, 2, char*));
604           xbt_str_ltrim(abstract_origin, "<0x");
605           xbt_str_rtrim(abstract_origin, ">");
606           subprogram_name = (char *)xbt_dict_get_or_null(subprograms_origin, abstract_origin);
607           frame = xbt_dict_get_or_null(*local_variables, subprogram_name); 
608           xbt_free(abstract_origin);
609
610         }else if(strcmp(node_type, "DW_AT_name") == 0){
611
612           new_frame = 1;
613           xbt_free(current_frame);
614           frame = xbt_new0(s_dw_frame_t, 1);
615           frame->name = strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *)); 
616           frame->variables = xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
617           frame->frame_base = xbt_new0(s_dw_location_t, 1); 
618           current_frame = strdup(frame->name);
619
620           xbt_dict_set(subprograms_origin, subprogram_start, xbt_strdup(frame->name), NULL);
621         
622         }else if(strcmp(node_type, "DW_AT_frame_base") == 0){
623
624           location_type = xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *);
625
626           if(strcmp(location_type, "list)") == 0){ /* Search location in location list */
627
628             frame->frame_base = MC_dwarf_get_location(location_list, xbt_dynar_get_as(split, 3, char *));
629              
630           }else{
631                 
632             xbt_str_strip_spaces(line);
633             split2 = xbt_str_split(line, "(");
634             xbt_dynar_remove_at(split2, 0, NULL);
635             loc_expr = xbt_str_join(split2, " ");
636             xbt_str_rtrim(loc_expr, ")");
637             frame->frame_base = MC_dwarf_get_location(NULL, loc_expr);
638             xbt_dynar_free(&split2);
639             xbt_free(loc_expr);
640
641           }
642  
643         }else if(strcmp(node_type, "DW_AT_low_pc") == 0){
644           
645           if(frame != NULL)
646             frame->low_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
647
648         }else if(strcmp(node_type, "DW_AT_high_pc") == 0){
649
650           if(frame != NULL)
651             frame->high_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
652
653         }else if(strcmp(node_type, "DW_AT_MIPS_linkage_name:") == 0){
654
655           xbt_free(frame->name);
656           xbt_free(current_frame);
657           frame->name = strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *));   
658           current_frame = strdup(frame->name);
659           xbt_dict_set(subprograms_origin, subprogram_start, xbt_strdup(frame->name), NULL);
660
661         }
662
663         read = xbt_getline(&line, &n, fp);
664
665       }
666  
667       if(new_frame == 1){
668         frame->start = strtoul(subprogram_start, NULL, 16);
669         if(subprogram_end != NULL)
670           frame->end = strtoul(subprogram_end, NULL, 16);
671         xbt_dict_set(*local_variables, frame->name, frame, NULL);
672       }
673
674       xbt_free(subprogram_start);
675       xbt_free(subprogram_end);
676       subprogram_end = NULL;
677         
678
679     }else if(strcmp(node_type, "(DW_TAG_variable)") == 0){ /* New variable */
680       /* We build a dw_variable_t object and append it either to
681          the list of variables of the frame (local variable)
682          or to the list of global variables (global variables). */
683
684       dw_variable_t var = NULL;
685       
686       parent_value = strdup(xbt_dynar_get_as(split, 0, char *));
687       parent_value = strtok(parent_value,"<");
688       xbt_str_rtrim(parent_value, ">");
689       parent = atoi(parent_value);
690       xbt_free(parent_value);
691
692       if(parent == 1)
693         global_variable = 1;
694     
695       strtok(xbt_dynar_get_as(split, 0, char *), "<");
696       origin = xbt_strdup(strtok(NULL, "<"));
697       xbt_str_rtrim(origin, ">:");
698       
699       read = xbt_getline(&line, &n, fp);
700       
701       while(read != -1){
702
703         /* Wipeout the new line character */
704         line[read - 1] = '\0'; 
705
706         if(n == 0 || strlen(line) == 0){
707           read = xbt_getline(&line, &n, fp);
708           continue;
709         }
710     
711         xbt_dynar_free(&split);
712         xbt_str_rtrim(line, NULL);
713         xbt_str_strip_spaces(line);
714         split = xbt_str_split(line, " ");
715   
716         node_type = xbt_dynar_get_as(split, 1, char *);
717
718         if(strncmp(node_type, "DW_AT_", 6) != 0)
719           break;
720
721         if(strcmp(node_type, "DW_AT_name") == 0){
722
723           var = xbt_new0(s_dw_variable_t, 1);
724           var->name = xbt_strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *));
725           xbt_dict_set(variables_origin, origin, xbt_strdup(var->name), NULL);
726          
727         }else if(strcmp(node_type, "DW_AT_abstract_origin:") == 0){
728
729           new_variable = 0;
730
731           abstract_origin = xbt_dynar_get_as(split, 2, char *);
732           xbt_str_ltrim(abstract_origin, "<0x");
733           xbt_str_rtrim(abstract_origin, ">");
734           
735           variable_name = (char *)xbt_dict_get_or_null(variables_origin, abstract_origin);
736           variable_frame = MC_dwarf_get_frame_by_offset(*local_variables, strtoul(abstract_origin, NULL, 16));
737           var = MC_dwarf_get_variable_by_name(variable_frame, variable_name); 
738
739         }else if(strcmp(node_type, "DW_AT_location") == 0){
740
741           if(var != NULL){
742
743             if(!global_variable){
744
745               location_type = xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *);
746
747               if(strcmp(location_type, "list)") == 0){ /* Search location in location list */
748                 var->address.location = MC_dwarf_get_location(location_list, xbt_dynar_get_as(split, 3, char *));
749               }else{
750                 xbt_str_strip_spaces(line);
751                 split2 = xbt_str_split(line, "(");
752                 xbt_dynar_remove_at(split2, 0, NULL);
753                 loc_expr = xbt_str_join(split2, " ");
754                 xbt_str_rtrim(loc_expr, ")");
755                 if(strncmp("DW_OP_addr", loc_expr, 10) == 0){
756                   global_variable = 1;
757                   xbt_dynar_free(&split2);
758                   split2 = xbt_str_split(loc_expr, " ");
759                   if(strcmp(elf_file, xbt_binary_name) != 0)
760                     var->address.address = (char *)start_text_libsimgrid + (long)((void *)strtoul(xbt_dynar_get_as(split2, xbt_dynar_length(split2) - 1, char*), NULL, 16));
761                   else
762                     var->address.address = (void *)strtoul(xbt_dynar_get_as(split2, xbt_dynar_length(split2) - 1, char*), NULL, 16);
763                 }else{
764                   var->address.location = MC_dwarf_get_location(NULL, loc_expr);
765                 }
766                 xbt_dynar_free(&split2);
767                 xbt_free(loc_expr);
768               }
769             }else{
770               global_address = xbt_strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *));
771               xbt_str_rtrim(global_address, ")");
772               if(strcmp(elf_file, xbt_binary_name) != 0)
773                 var->address.address = (char *)start_text_libsimgrid + (long)((void *)strtoul(global_address, NULL, 16));
774               else
775                 var->address.address = (void *)strtoul(global_address, NULL, 16);
776               xbt_free(global_address);
777               global_address = NULL;
778             }
779
780           }
781                    
782         }else if(strcmp(node_type, "DW_AT_type") == 0){
783           
784           type_origin = xbt_strdup(xbt_dynar_get_as(split, 3, char *));
785           xbt_str_ltrim(type_origin, "<0x");
786           xbt_str_rtrim(type_origin, ">");
787         
788         }else if(strcmp(node_type, "DW_AT_declaration") == 0){
789
790           new_variable = 0;
791           if(new_variable){
792             dw_variable_free(var);
793             var = NULL;
794           }
795         
796         }else if(strcmp(node_type, "DW_AT_artificial") == 0){
797           
798           new_variable = 0;
799           if(new_variable){
800             dw_variable_free(var);
801             var = NULL;
802           }
803         
804         }
805
806         read = xbt_getline(&line, &n, fp);
807  
808       }
809
810       if(new_variable == 1){
811         
812         if(!global_variable){
813           variable_frame = xbt_dict_get_or_null(*local_variables, current_frame);
814           var->type_origin = strdup(type_origin);
815           var->global = 0;
816           index = MC_dwarf_get_variable_index(variable_frame->variables, var->name, NULL);
817           if(index != -1)
818             xbt_dynar_insert_at(variable_frame->variables, index, &var);
819         }else{
820           var->type_origin = strdup(type_origin);
821           var->global = 1;
822           index = MC_dwarf_get_variable_index(*global_variables, var->name, var->address.address);
823           if(index != -1)
824             xbt_dynar_insert_at(*global_variables, index, &var); 
825         }
826
827          xbt_free(type_origin);
828          type_origin = NULL;
829       }
830
831       global_variable = 0;
832       new_variable = 1;
833
834     }else if(strcmp(node_type, "(DW_TAG_inlined_subroutine)") == 0){
835       /* Update the information on the frame (we should duplicate it instead). */
836
837       read = xbt_getline(&line, &n, fp);
838
839       while(read != -1){
840
841         /* Wipeout the new line character */
842         line[read - 1] = '\0'; 
843
844         if(n == 0 || strlen(line) == 0){
845           read = xbt_getline(&line, &n, fp);
846           continue;
847         }
848
849         xbt_dynar_free(&split);
850         xbt_str_rtrim(line, NULL);
851         xbt_str_strip_spaces(line);
852         split = xbt_str_split(line, " ");
853         
854         if(strncmp(xbt_dynar_get_as(split, 1, char *), "DW_AT_", 6) != 0)
855           break;
856           
857         node_type = xbt_dynar_get_as(split, 1, char *);
858
859         if(strcmp(node_type, "DW_AT_abstract_origin:") == 0){
860
861           origin = xbt_dynar_get_as(split, 2, char *);
862           xbt_str_ltrim(origin, "<0x");
863           xbt_str_rtrim(origin, ">");
864           
865           subprogram_name = (char *)xbt_dict_get_or_null(subprograms_origin, origin);
866           subroutine_frame = xbt_dict_get_or_null(*local_variables, subprogram_name);
867         
868         }else if(strcmp(node_type, "DW_AT_low_pc") == 0){
869
870           subroutine_frame->low_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
871
872         }else if(strcmp(node_type, "DW_AT_high_pc") == 0){
873
874           subroutine_frame->high_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
875         }
876
877         read = xbt_getline(&line, &n, fp);
878       
879       }
880
881     }else if(strcmp(node_type, "(DW_TAG_base_type)") == 0 
882              || strcmp(node_type, "(DW_TAG_enumeration_type)") == 0
883              || strcmp(node_type, "(DW_TAG_enumerator)") == 0
884              || strcmp(node_type, "(DW_TAG_typedef)") == 0
885              || strcmp(node_type, "(DW_TAG_const_type)") == 0
886              || strcmp(node_type, "(DW_TAG_subroutine_type)") == 0
887              || strcmp(node_type, "(DW_TAG_volatile_type)") == 0
888              || (is_pointer = !strcmp(node_type, "(DW_TAG_pointer_type)"))){
889
890       /* Create the and add it to the types dictionnary */
891
892       if(strcmp(node_type, "(DW_TAG_base_type)") == 0)
893         type_type = e_dw_base_type;
894       else if(strcmp(node_type, "(DW_TAG_enumeration_type)") == 0)
895         type_type = e_dw_enumeration_type;
896       else if(strcmp(node_type, "(DW_TAG_enumerator)") == 0)
897         type_type = e_dw_enumerator;
898       else if(strcmp(node_type, "(DW_TAG_typedef)") == 0)
899         type_type = e_dw_typedef;
900       else if(strcmp(node_type, "(DW_TAG_const_type)") == 0)
901         type_type = e_dw_const_type;
902       else if(strcmp(node_type, "(DW_TAG_pointer_type)") == 0)
903         type_type = e_dw_pointer_type;
904       else if(strcmp(node_type, "(DW_TAG_subroutine_type)") == 0)
905         type_type = e_dw_subroutine_type;
906       else if(strcmp(node_type, "(DW_TAG_volatile_type)") == 0)
907         type_type = e_dw_volatile_type;
908
909       strtok(xbt_dynar_get_as(split, 0, char *), "<");
910       origin = strdup(strtok(NULL, "<"));
911       xbt_str_rtrim(origin, ">:");
912       
913       read = xbt_getline(&line, &n, fp);
914       
915       while(read != -1){
916         
917          /* Wipeout the new line character */
918         line[read - 1] = '\0'; 
919
920         if(n == 0 || strlen(line) == 0){
921           read = xbt_getline(&line, &n, fp);
922           continue;
923         }
924
925         xbt_dynar_free(&split);
926         xbt_str_rtrim(line, NULL);
927         xbt_str_strip_spaces(line);
928         split = xbt_str_split(line, " ");
929         
930         if(strncmp(xbt_dynar_get_as(split, 1, char *), "DW_AT_", 6) != 0)
931           break;
932
933         node_type = xbt_dynar_get_as(split, 1, char *);
934
935         if(strcmp(node_type, "DW_AT_byte_size") == 0){
936           size = strtol(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char*), NULL, 10);
937           if(type_type == e_dw_enumeration_type)
938             enumeration_size = size;
939         }else if(strcmp(node_type, "DW_AT_name") == 0){
940           end = xbt_str_join(split, " ");
941           xbt_dynar_free(&split);
942           split = xbt_str_split(end, "):");
943           xbt_str_ltrim(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char*), NULL);
944           name = xbt_strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char*));
945         }else if(strcmp(node_type, "DW_AT_type") == 0){
946           type_origin = xbt_strdup(xbt_dynar_get_as(split, 3, char *));
947           xbt_str_ltrim(type_origin, "<0x");
948           xbt_str_rtrim(type_origin, ">");
949         }
950         
951         read = xbt_getline(&line, &n, fp);
952       }
953
954       dw_type_t type = xbt_new0(s_dw_type_t, 1);
955       type->type = type_type;
956       if(name)
957         type->name = xbt_strdup(name);
958       else
959         type->name = xbt_strdup("undefined");
960       type->is_pointer_type = is_pointer;
961       type->id = (void *)strtoul(origin, NULL, 16);
962       if(type_origin)
963         type->dw_type_id = xbt_strdup(type_origin);
964       if(type_type == e_dw_enumerator)
965         type->size = enumeration_size;
966       else
967         type->size = size;
968       type->members = NULL;
969
970       xbt_dict_set(*types, origin, type, NULL); 
971
972       xbt_free(name);
973       name = NULL;
974       xbt_free(type_origin);
975       type_origin = NULL;
976       xbt_free(end);
977       end = NULL;
978
979       is_pointer = 0;
980       size = 0;
981       xbt_free(origin);
982
983     }else if(strcmp(node_type, "(DW_TAG_structure_type)") == 0 || strcmp(node_type, "(DW_TAG_union_type)") == 0){
984       
985       if(strcmp(node_type, "(DW_TAG_structure_type)") == 0)
986         struct_decl = 1;
987       else
988         union_decl = 1;
989
990       strtok(xbt_dynar_get_as(split, 0, char *), "<");
991       origin = strdup(strtok(NULL, "<"));
992       xbt_str_rtrim(origin, ">:");
993       
994       read = xbt_getline(&line, &n, fp);
995
996       dw_type_t type = NULL;
997
998       while(read != -1){
999       
1000         while(read != -1){
1001         
1002           /* Wipeout the new line character */
1003           line[read - 1] = '\0'; 
1004
1005           if(n == 0 || strlen(line) == 0){
1006             read = xbt_getline(&line, &n, fp);
1007             continue;
1008           }
1009
1010           xbt_dynar_free(&split);
1011           xbt_str_rtrim(line, NULL);
1012           xbt_str_strip_spaces(line);
1013           split = xbt_str_split(line, " ");
1014         
1015           node_type = xbt_dynar_get_as(split, 1, char *);
1016
1017           if(strncmp(node_type, "DW_AT_", 6) != 0){
1018             member_end = 1;
1019             break;
1020           }
1021
1022           if(strcmp(node_type, "DW_AT_byte_size") == 0){
1023             size = strtol(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char*), NULL, 10);
1024           }else if(strcmp(node_type, "DW_AT_name") == 0){
1025             xbt_free(end);
1026             end = xbt_str_join(split, " ");
1027             xbt_dynar_free(&split);
1028             split = xbt_str_split(end, "):");
1029             xbt_str_ltrim(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char*), NULL);
1030             name = xbt_strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char*));
1031           }else if(strcmp(node_type, "DW_AT_type") == 0){
1032             type_origin = xbt_strdup(xbt_dynar_get_as(split, 3, char *));
1033             xbt_str_ltrim(type_origin, "<0x");
1034             xbt_str_rtrim(type_origin, ">");
1035           }else if(strcmp(node_type, "DW_AT_data_member_location:") == 0){
1036             xbt_free(end);
1037             end = xbt_str_join(split, " ");
1038             xbt_dynar_free(&split);
1039             split = xbt_str_split(end, "DW_OP_plus_uconst:");
1040             xbt_str_ltrim(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *), NULL);
1041             xbt_str_rtrim(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *), ")");
1042             offset = strtol(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char*), NULL, 10);
1043           }
1044
1045           read = xbt_getline(&line, &n, fp);
1046           
1047         }
1048
1049         if(member_end && type){         
1050           member_end = 0;
1051           
1052           dw_type_t member_type = xbt_new0(s_dw_type_t, 1);
1053           member_type->name = xbt_strdup(name);
1054           member_type->size = size;
1055           member_type->is_pointer_type = is_pointer;
1056           member_type->id = (void *)strtoul(origin, NULL, 16);
1057           member_type->offset = offset;
1058           if(type_origin)
1059             member_type->dw_type_id = xbt_strdup(type_origin);
1060
1061           xbt_dynar_push(type->members, &member_type);
1062
1063           xbt_free(name);
1064           name = NULL;
1065           xbt_free(end);
1066           end = NULL;
1067           xbt_free(type_origin);
1068           type_origin = NULL;
1069           size = 0;
1070           offset = 0;
1071
1072           xbt_free(origin);
1073           origin = NULL;
1074           strtok(xbt_dynar_get_as(split, 0, char *), "<");
1075           origin = strdup(strtok(NULL, "<"));
1076           xbt_str_rtrim(origin, ">:");
1077
1078         }
1079
1080         if(struct_decl || union_decl){
1081           type = xbt_new0(s_dw_type_t, 1);
1082           if(struct_decl)
1083             type->type = e_dw_structure_type;
1084           else
1085             type->type = e_dw_union_type;
1086           type->name = xbt_strdup(name);
1087           type->size = size;
1088           type->is_pointer_type = is_pointer;
1089           type->id = (void *)strtoul(origin, NULL, 16);
1090           if(type_origin)
1091             type->dw_type_id = xbt_strdup(type_origin);
1092           type->members = xbt_dynar_new(sizeof(dw_type_t), dw_type_free_voidp);
1093           
1094           xbt_dict_set(*types, origin, type, NULL); 
1095           
1096           xbt_free(name);
1097           name = NULL;
1098           xbt_free(end);
1099           end = NULL;
1100           xbt_free(type_origin);
1101           type_origin = NULL;
1102           size = 0;
1103           struct_decl = 0;
1104           union_decl = 0;
1105
1106         }
1107
1108         if(strcmp(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *), "(DW_TAG_member)") != 0)
1109           break;  
1110
1111         read = xbt_getline(&line, &n, fp);
1112     
1113       }
1114
1115       xbt_free(origin);
1116       origin = NULL;
1117
1118     }else if(strcmp(node_type, "(DW_TAG_array_type)") == 0){
1119       
1120       strtok(xbt_dynar_get_as(split, 0, char *), "<");
1121       origin = strdup(strtok(NULL, "<"));
1122       xbt_str_rtrim(origin, ">:");
1123       
1124       read = xbt_getline(&line, &n, fp);
1125
1126       dw_type_t type = NULL;
1127
1128       while(read != -1){
1129       
1130         while(read != -1){
1131         
1132           /* Wipeout the new line character */
1133           line[read - 1] = '\0'; 
1134
1135           if(n == 0 || strlen(line) == 0){
1136             read = xbt_getline(&line, &n, fp);
1137             continue;
1138           }
1139
1140           xbt_dynar_free(&split);
1141           xbt_str_rtrim(line, NULL);
1142           xbt_str_strip_spaces(line);
1143           split = xbt_str_split(line, " ");
1144         
1145           node_type = xbt_dynar_get_as(split, 1, char *);
1146
1147           if(strncmp(node_type, "DW_AT_", 6) != 0)
1148             break;
1149
1150           if(strcmp(node_type, "DW_AT_upper_bound") == 0){
1151             size = strtol(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char*), NULL, 10);
1152           }else if(strcmp(node_type, "DW_AT_name") == 0){
1153             end = xbt_str_join(split, " ");
1154             xbt_dynar_free(&split);
1155             split = xbt_str_split(end, "):");
1156             xbt_str_ltrim(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char*), NULL);
1157             name = xbt_strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char*));
1158           }else if(strcmp(node_type, "DW_AT_type") == 0){
1159             type_origin = xbt_strdup(xbt_dynar_get_as(split, 3, char *));
1160             xbt_str_ltrim(type_origin, "<0x");
1161             xbt_str_rtrim(type_origin, ">");
1162           }
1163
1164           read = xbt_getline(&line, &n, fp);
1165           
1166         }
1167
1168         if(strcmp(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *), "(DW_TAG_subrange_type)") == 0){
1169           subrange = 1;         
1170         }
1171
1172         if(subrange && type){         
1173           type->size = size;
1174       
1175           xbt_free(name);
1176           name = NULL;
1177           xbt_free(end);
1178           end = NULL;
1179           xbt_free(type_origin);
1180           type_origin = NULL;
1181           size = 0;
1182
1183           xbt_free(origin);
1184           origin = NULL;
1185           strtok(xbt_dynar_get_as(split, 0, char *), "<");
1186           origin = strdup(strtok(NULL, "<"));
1187           xbt_str_rtrim(origin, ">:");
1188
1189         }else {
1190           
1191           type = xbt_new0(s_dw_type_t, 1);
1192           type->type = e_dw_array_type;
1193           type->name = xbt_strdup(name);
1194           type->is_pointer_type = is_pointer;
1195           type->id = (void *)strtoul(origin, NULL, 16);
1196           if(type_origin)
1197             type->dw_type_id = xbt_strdup(type_origin);
1198           type->members = NULL;
1199           
1200           xbt_dict_set(*types, origin, type, NULL); 
1201           
1202           xbt_free(name);
1203           name = NULL;
1204           xbt_free(end);
1205           end = NULL;
1206           xbt_free(type_origin);
1207           type_origin = NULL;
1208           size = 0;
1209         }
1210
1211         if(strcmp(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *), "(DW_TAG_subrange_type)") != 0)
1212           break;  
1213
1214         read = xbt_getline(&line, &n, fp);
1215     
1216       }
1217
1218       xbt_free(origin);
1219       origin = NULL;
1220
1221     }else{
1222
1223       read = xbt_getline(&line, &n, fp);
1224
1225     }
1226
1227   }
1228   
1229   xbt_dynar_free(&split);
1230   xbt_dict_free(&variables_origin);
1231   xbt_dict_free(&subprograms_origin);
1232   xbt_free(line);
1233   xbt_free(command);
1234   pclose(fp);
1235   
1236 }
1237
1238
1239 /*******************************  Ignore mechanism *******************************/
1240 /*********************************************************************************/
1241
1242 xbt_dynar_t mc_checkpoint_ignore;
1243
1244 typedef struct s_mc_stack_ignore_variable{
1245   char *var_name;
1246   char *frame;
1247 }s_mc_stack_ignore_variable_t, *mc_stack_ignore_variable_t;
1248
1249 typedef struct s_mc_data_bss_ignore_variable{
1250   char *name;
1251 }s_mc_data_bss_ignore_variable_t, *mc_data_bss_ignore_variable_t;
1252
1253 /**************************** Free functions ******************************/
1254
1255 static void stack_ignore_variable_free(mc_stack_ignore_variable_t v){
1256   xbt_free(v->var_name);
1257   xbt_free(v->frame);
1258   xbt_free(v);
1259 }
1260
1261 static void stack_ignore_variable_free_voidp(void *v){
1262   stack_ignore_variable_free((mc_stack_ignore_variable_t) * (void **) v);
1263 }
1264
1265 void heap_ignore_region_free(mc_heap_ignore_region_t r){
1266   xbt_free(r);
1267 }
1268
1269 void heap_ignore_region_free_voidp(void *r){
1270   heap_ignore_region_free((mc_heap_ignore_region_t) * (void **) r);
1271 }
1272
1273 static void data_bss_ignore_variable_free(mc_data_bss_ignore_variable_t v){
1274   xbt_free(v->name);
1275   xbt_free(v);
1276 }
1277
1278 static void data_bss_ignore_variable_free_voidp(void *v){
1279   data_bss_ignore_variable_free((mc_data_bss_ignore_variable_t) * (void **) v);
1280 }
1281
1282 static void checkpoint_ignore_region_free(mc_checkpoint_ignore_region_t r){
1283   xbt_free(r);
1284 }
1285
1286 static void checkpoint_ignore_region_free_voidp(void *r){
1287   checkpoint_ignore_region_free((mc_checkpoint_ignore_region_t) * (void **) r);
1288 }
1289
1290 /***********************************************************************/
1291
1292 void MC_ignore_heap(void *address, size_t size){
1293
1294   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
1295
1296   MC_SET_RAW_MEM;
1297
1298   mc_heap_ignore_region_t region = NULL;
1299   region = xbt_new0(s_mc_heap_ignore_region_t, 1);
1300   region->address = address;
1301   region->size = size;
1302   
1303   region->block = ((char*)address - (char*)((xbt_mheap_t)std_heap)->heapbase) / BLOCKSIZE + 1;
1304   
1305   if(((xbt_mheap_t)std_heap)->heapinfo[region->block].type == 0){
1306     region->fragment = -1;
1307     ((xbt_mheap_t)std_heap)->heapinfo[region->block].busy_block.ignore++;
1308   }else{
1309     region->fragment = ((uintptr_t) (ADDR2UINT (address) % (BLOCKSIZE))) >> ((xbt_mheap_t)std_heap)->heapinfo[region->block].type;
1310     ((xbt_mheap_t)std_heap)->heapinfo[region->block].busy_frag.ignore[region->fragment]++;
1311   }
1312   
1313   if(mc_heap_comparison_ignore == NULL){
1314     mc_heap_comparison_ignore = xbt_dynar_new(sizeof(mc_heap_ignore_region_t), heap_ignore_region_free_voidp);
1315     xbt_dynar_push(mc_heap_comparison_ignore, &region);
1316     if(!raw_mem_set)
1317       MC_UNSET_RAW_MEM;
1318     return;
1319   }
1320
1321   unsigned int cursor = 0;
1322   mc_heap_ignore_region_t current_region = NULL;
1323   int start = 0;
1324   int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
1325   
1326   while(start <= end){
1327     cursor = (start + end) / 2;
1328     current_region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
1329     if(current_region->address == address){
1330       heap_ignore_region_free(region);
1331       if(!raw_mem_set)
1332         MC_UNSET_RAW_MEM;
1333       return;
1334     }else if(current_region->address < address){
1335       start = cursor + 1;
1336     }else{
1337       end = cursor - 1;
1338     }   
1339   }
1340
1341   if(current_region->address < address)
1342     xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor + 1, &region);
1343   else
1344     xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor, &region);
1345
1346   if(!raw_mem_set)
1347     MC_UNSET_RAW_MEM;
1348 }
1349
1350 void MC_remove_ignore_heap(void *address, size_t size){
1351
1352   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
1353
1354   MC_SET_RAW_MEM;
1355
1356   unsigned int cursor = 0;
1357   int start = 0;
1358   int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
1359   mc_heap_ignore_region_t region;
1360   int ignore_found = 0;
1361
1362   while(start <= end){
1363     cursor = (start + end) / 2;
1364     region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
1365     if(region->address == address){
1366       ignore_found = 1;
1367       break;
1368     }else if(region->address < address){
1369       start = cursor + 1;
1370     }else{
1371       if((char * )region->address <= ((char *)address + size)){
1372         ignore_found = 1;
1373         break;
1374       }else{
1375         end = cursor - 1;   
1376       }
1377     }
1378   }
1379   
1380   if(ignore_found == 1){
1381     xbt_dynar_remove_at(mc_heap_comparison_ignore, cursor, NULL);
1382     MC_remove_ignore_heap(address, size);
1383   }
1384
1385   if(!raw_mem_set)
1386     MC_UNSET_RAW_MEM;
1387
1388 }
1389
1390 void MC_ignore_global_variable(const char *name){
1391
1392   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
1393
1394   MC_SET_RAW_MEM;
1395
1396   if(mc_global_variables_libsimgrid){
1397
1398     unsigned int cursor = 0;
1399     dw_variable_t current_var;
1400     int start = 0;
1401     int end = xbt_dynar_length(mc_global_variables_libsimgrid) - 1;
1402
1403     while(start <= end){
1404       cursor = (start + end) /2;
1405       current_var = (dw_variable_t)xbt_dynar_get_as(mc_global_variables_libsimgrid, cursor, dw_variable_t);
1406       if(strcmp(current_var->name, name) == 0){
1407         xbt_dynar_remove_at(mc_global_variables_libsimgrid, cursor, NULL);
1408         start = 0;
1409         end = xbt_dynar_length(mc_global_variables_libsimgrid) - 1;
1410       }else if(strcmp(current_var->name, name) < 0){
1411         start = cursor + 1;
1412       }else{
1413         end = cursor - 1;
1414       } 
1415     }
1416    
1417   }else{
1418
1419     if(mc_data_bss_comparison_ignore == NULL)
1420       mc_data_bss_comparison_ignore = xbt_dynar_new(sizeof(mc_data_bss_ignore_variable_t), data_bss_ignore_variable_free_voidp);
1421
1422     mc_data_bss_ignore_variable_t var = NULL;
1423     var = xbt_new0(s_mc_data_bss_ignore_variable_t, 1);
1424     var->name = strdup(name);
1425
1426     if(xbt_dynar_is_empty(mc_data_bss_comparison_ignore)){
1427
1428       xbt_dynar_insert_at(mc_data_bss_comparison_ignore, 0, &var);
1429
1430     }else{
1431     
1432       unsigned int cursor = 0;
1433       int start = 0;
1434       int end = xbt_dynar_length(mc_data_bss_comparison_ignore) - 1;
1435       mc_data_bss_ignore_variable_t current_var = NULL;
1436
1437       while(start <= end){
1438         cursor = (start + end) / 2;
1439         current_var = (mc_data_bss_ignore_variable_t)xbt_dynar_get_as(mc_data_bss_comparison_ignore, cursor, mc_data_bss_ignore_variable_t);
1440         if(strcmp(current_var->name, name) == 0){
1441           data_bss_ignore_variable_free(var);
1442           if(!raw_mem_set)
1443             MC_UNSET_RAW_MEM;
1444           return;
1445         }else if(strcmp(current_var->name, name) < 0){
1446           start = cursor + 1;
1447         }else{
1448           end = cursor - 1;
1449         }
1450       }
1451
1452       if(strcmp(current_var->name, name) < 0)
1453         xbt_dynar_insert_at(mc_data_bss_comparison_ignore, cursor + 1, &var);
1454       else
1455         xbt_dynar_insert_at(mc_data_bss_comparison_ignore, cursor, &var);
1456
1457     }
1458   }
1459
1460   if(!raw_mem_set)
1461     MC_UNSET_RAW_MEM;
1462 }
1463
1464 void MC_ignore_local_variable(const char *var_name, const char *frame_name){
1465   
1466   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
1467
1468   MC_SET_RAW_MEM;
1469
1470   if(mc_local_variables_libsimgrid){
1471     unsigned int cursor = 0;
1472     dw_variable_t current_var;
1473     int start, end;
1474     if(strcmp(frame_name, "*") == 0){ /* Remove variable in all frames */
1475       xbt_dict_cursor_t dict_cursor;
1476       char *current_frame_name;
1477       dw_frame_t frame;
1478       xbt_dict_foreach(mc_local_variables_libsimgrid, dict_cursor, current_frame_name, frame){
1479         start = 0;
1480         end = xbt_dynar_length(frame->variables) - 1;
1481         while(start <= end){
1482           cursor = (start + end) / 2;
1483           current_var = (dw_variable_t)xbt_dynar_get_as(frame->variables, cursor, dw_variable_t); 
1484           if(strcmp(current_var->name, var_name) == 0){
1485             xbt_dynar_remove_at(frame->variables, cursor, NULL);
1486             start = 0;
1487             end = xbt_dynar_length(frame->variables) - 1;
1488           }else if(strcmp(current_var->name, var_name) < 0){
1489             start = cursor + 1;
1490           }else{
1491             end = cursor - 1;
1492           } 
1493         }
1494       }
1495        xbt_dict_foreach(mc_local_variables_binary, dict_cursor, current_frame_name, frame){
1496         start = 0;
1497         end = xbt_dynar_length(frame->variables) - 1;
1498         while(start <= end){
1499           cursor = (start + end) / 2;
1500           current_var = (dw_variable_t)xbt_dynar_get_as(frame->variables, cursor, dw_variable_t); 
1501           if(strcmp(current_var->name, var_name) == 0){
1502             xbt_dynar_remove_at(frame->variables, cursor, NULL);
1503             start = 0;
1504             end = xbt_dynar_length(frame->variables) - 1;
1505           }else if(strcmp(current_var->name, var_name) < 0){
1506             start = cursor + 1;
1507           }else{
1508             end = cursor - 1;
1509           } 
1510         }
1511       }
1512     }else{
1513       xbt_dynar_t variables_list = ((dw_frame_t)xbt_dict_get_or_null(mc_local_variables_libsimgrid, frame_name))->variables;
1514       start = 0;
1515       end = xbt_dynar_length(variables_list) - 1;
1516       while(start <= end){
1517         cursor = (start + end) / 2;
1518         current_var = (dw_variable_t)xbt_dynar_get_as(variables_list, cursor, dw_variable_t);
1519         if(strcmp(current_var->name, var_name) == 0){
1520           xbt_dynar_remove_at(variables_list, cursor, NULL);
1521           start = 0;
1522           end = xbt_dynar_length(variables_list) - 1;
1523         }else if(strcmp(current_var->name, var_name) < 0){
1524           start = cursor + 1;
1525         }else{
1526           end = cursor - 1;
1527         } 
1528       }
1529     } 
1530   }else{
1531
1532     if(mc_stack_comparison_ignore == NULL)
1533       mc_stack_comparison_ignore = xbt_dynar_new(sizeof(mc_stack_ignore_variable_t), stack_ignore_variable_free_voidp);
1534   
1535     mc_stack_ignore_variable_t var = NULL;
1536     var = xbt_new0(s_mc_stack_ignore_variable_t, 1);
1537     var->var_name = strdup(var_name);
1538     var->frame = strdup(frame_name);
1539   
1540     if(xbt_dynar_is_empty(mc_stack_comparison_ignore)){
1541
1542       xbt_dynar_insert_at(mc_stack_comparison_ignore, 0, &var);
1543
1544     }else{
1545     
1546       unsigned int cursor = 0;
1547       int start = 0;
1548       int end = xbt_dynar_length(mc_stack_comparison_ignore) - 1;
1549       mc_stack_ignore_variable_t current_var = NULL;
1550
1551       while(start <= end){
1552         cursor = (start + end) / 2;
1553         current_var = (mc_stack_ignore_variable_t)xbt_dynar_get_as(mc_stack_comparison_ignore, cursor, mc_stack_ignore_variable_t);
1554         if(strcmp(current_var->frame, frame_name) == 0){
1555           if(strcmp(current_var->var_name, var_name) == 0){
1556             stack_ignore_variable_free(var);
1557             if(!raw_mem_set)
1558               MC_UNSET_RAW_MEM;
1559             return;
1560           }else if(strcmp(current_var->var_name, var_name) < 0){
1561             start = cursor + 1;
1562           }else{
1563             end = cursor - 1;
1564           }
1565         }else if(strcmp(current_var->frame, frame_name) < 0){
1566           start = cursor + 1;
1567         }else{
1568           end = cursor - 1;
1569         }
1570       }
1571
1572       if(strcmp(current_var->frame, frame_name) == 0){
1573         if(strcmp(current_var->var_name, var_name) < 0){
1574           xbt_dynar_insert_at(mc_stack_comparison_ignore, cursor + 1, &var);
1575         }else{
1576           xbt_dynar_insert_at(mc_stack_comparison_ignore, cursor, &var);
1577         }
1578       }else if(strcmp(current_var->frame, frame_name) < 0){
1579         xbt_dynar_insert_at(mc_stack_comparison_ignore, cursor + 1, &var);
1580       }else{
1581         xbt_dynar_insert_at(mc_stack_comparison_ignore, cursor, &var);
1582       }
1583     }
1584   }
1585
1586   if(!raw_mem_set)
1587     MC_UNSET_RAW_MEM;
1588
1589 }
1590
1591 void MC_new_stack_area(void *stack, char *name, void* context, size_t size){
1592
1593   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
1594
1595   MC_SET_RAW_MEM;
1596
1597   if(stacks_areas == NULL)
1598     stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
1599   
1600   stack_region_t region = NULL;
1601   region = xbt_new0(s_stack_region_t, 1);
1602   region->address = stack;
1603   region->process_name = strdup(name);
1604   region->context = context;
1605   region->size = size;
1606   region->block = ((char*)stack - (char*)((xbt_mheap_t)std_heap)->heapbase) / BLOCKSIZE + 1;
1607   xbt_dynar_push(stacks_areas, &region);
1608
1609   if(!raw_mem_set)
1610     MC_UNSET_RAW_MEM;
1611 }
1612
1613 void MC_ignore(void *addr, size_t size){
1614
1615   int raw_mem_set= (mmalloc_get_current_heap() == raw_heap);
1616
1617   MC_SET_RAW_MEM;
1618
1619   if(mc_checkpoint_ignore == NULL)
1620     mc_checkpoint_ignore = xbt_dynar_new(sizeof(mc_checkpoint_ignore_region_t), checkpoint_ignore_region_free_voidp);
1621
1622   mc_checkpoint_ignore_region_t region = xbt_new0(s_mc_checkpoint_ignore_region_t, 1);
1623   region->addr = addr;
1624   region->size = size;
1625
1626   if(xbt_dynar_is_empty(mc_checkpoint_ignore)){
1627     xbt_dynar_push(mc_checkpoint_ignore, &region);
1628   }else{
1629      
1630     unsigned int cursor = 0;
1631     int start = 0;
1632     int end = xbt_dynar_length(mc_checkpoint_ignore) -1;
1633     mc_checkpoint_ignore_region_t current_region = NULL;
1634
1635     while(start <= end){
1636       cursor = (start + end) / 2;
1637       current_region = (mc_checkpoint_ignore_region_t)xbt_dynar_get_as(mc_checkpoint_ignore, cursor, mc_checkpoint_ignore_region_t);
1638       if(current_region->addr == addr){
1639         if(current_region->size == size){
1640           checkpoint_ignore_region_free(region);
1641           if(!raw_mem_set)
1642             MC_UNSET_RAW_MEM;
1643           return;
1644         }else if(current_region->size < size){
1645           start = cursor + 1;
1646         }else{
1647           end = cursor - 1;
1648         }
1649       }else if(current_region->addr < addr){
1650           start = cursor + 1;
1651       }else{
1652         end = cursor - 1;
1653       }
1654     }
1655
1656      if(current_region->addr == addr){
1657        if(current_region->size < size){
1658         xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, &region);
1659       }else{
1660         xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, &region);
1661       }
1662     }else if(current_region->addr < addr){
1663        xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, &region);
1664     }else{
1665        xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, &region);
1666     }
1667   }
1668
1669   if(!raw_mem_set)
1670     MC_UNSET_RAW_MEM;
1671 }
1672
1673 /*******************************  Initialisation of MC *******************************/
1674 /*********************************************************************************/
1675
1676 static void MC_dump_ignored_local_variables(void){
1677
1678   if(mc_stack_comparison_ignore == NULL || xbt_dynar_is_empty(mc_stack_comparison_ignore))
1679     return;
1680
1681   unsigned int cursor = 0;
1682   mc_stack_ignore_variable_t current_var;
1683
1684   xbt_dynar_foreach(mc_stack_comparison_ignore, cursor, current_var){
1685     MC_ignore_local_variable(current_var->var_name, current_var->frame);
1686   }
1687
1688   xbt_dynar_free(&mc_stack_comparison_ignore);
1689   mc_stack_comparison_ignore = NULL;
1690  
1691 }
1692
1693 static void MC_dump_ignored_global_variables(void){
1694
1695   if(mc_data_bss_comparison_ignore == NULL || xbt_dynar_is_empty(mc_data_bss_comparison_ignore))
1696     return;
1697
1698   unsigned int cursor = 0;
1699   mc_data_bss_ignore_variable_t current_var;
1700
1701   xbt_dynar_foreach(mc_data_bss_comparison_ignore, cursor, current_var){
1702     MC_ignore_global_variable(current_var->name);
1703   } 
1704
1705   xbt_dynar_free(&mc_data_bss_comparison_ignore);
1706   mc_data_bss_comparison_ignore = NULL;
1707
1708 }
1709
1710 void MC_init(){
1711
1712   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
1713   
1714   compare = 0;
1715
1716   /* Initialize the data structures that must be persistent across every
1717      iteration of the model-checker (in RAW memory) */
1718
1719   MC_SET_RAW_MEM;
1720
1721   MC_init_memory_map_info();
1722   
1723   mc_local_variables_libsimgrid = xbt_dict_new_homogeneous(NULL);
1724   mc_local_variables_binary = xbt_dict_new_homogeneous(NULL);
1725   mc_global_variables_libsimgrid = xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
1726   mc_global_variables_binary = xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
1727   mc_variables_type_libsimgrid = xbt_dict_new_homogeneous(NULL);
1728   mc_variables_type_binary = xbt_dict_new_homogeneous(NULL);
1729
1730   XBT_INFO("Get debug information ...");
1731
1732   /* Get local variables in binary for state equality detection */
1733   xbt_dict_t binary_location_list = MC_dwarf_get_location_list(xbt_binary_name);
1734   MC_dwarf_get_variables(xbt_binary_name, binary_location_list, &mc_local_variables_binary, &mc_global_variables_binary, &mc_variables_type_binary);
1735
1736   /* Get local variables in libsimgrid for state equality detection */
1737   xbt_dict_t libsimgrid_location_list = MC_dwarf_get_location_list(libsimgrid_path);
1738   MC_dwarf_get_variables(libsimgrid_path, libsimgrid_location_list, &mc_local_variables_libsimgrid, &mc_global_variables_libsimgrid, &mc_variables_type_libsimgrid);
1739
1740   xbt_dict_free(&libsimgrid_location_list);
1741   xbt_dict_free(&binary_location_list);
1742
1743   XBT_INFO("Get debug information done !");
1744
1745   /* Remove variables ignored before getting list of variables */
1746   MC_dump_ignored_local_variables();
1747   MC_dump_ignored_global_variables();
1748   
1749   /* Get .plt section (start and end addresses) for data libsimgrid and data program comparison */
1750   MC_get_libsimgrid_plt_section();
1751   MC_get_binary_plt_section();
1752
1753    /* Init parmap */
1754   parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
1755
1756   MC_UNSET_RAW_MEM;
1757
1758    /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
1759   MC_ignore_local_variable("e", "*");
1760   MC_ignore_local_variable("__ex_cleanup", "*");
1761   MC_ignore_local_variable("__ex_mctx_en", "*");
1762   MC_ignore_local_variable("__ex_mctx_me", "*");
1763   MC_ignore_local_variable("__xbt_ex_ctx_ptr", "*");
1764   MC_ignore_local_variable("_log_ev", "*");
1765   MC_ignore_local_variable("_throw_ctx", "*");
1766   MC_ignore_local_variable("ctx", "*");
1767
1768   MC_ignore_local_variable("next_context", "smx_ctx_sysv_suspend_serial");
1769   MC_ignore_local_variable("i", "smx_ctx_sysv_suspend_serial");
1770
1771   /* Ignore local variable about time used for tracing */
1772   MC_ignore_local_variable("start_time", "*"); 
1773
1774   MC_ignore_global_variable("mc_comp_times");
1775   MC_ignore_global_variable("mc_snapshot_comparison_time"); 
1776   MC_ignore_global_variable("mc_time");
1777   MC_ignore_global_variable("smpi_current_rank");
1778   MC_ignore_global_variable("counter"); /* Static variable used for tracing */
1779   MC_ignore_global_variable("maestro_stack_start");
1780   MC_ignore_global_variable("maestro_stack_end");
1781
1782   MC_ignore_heap(&(simix_global->process_to_run), sizeof(simix_global->process_to_run));
1783   MC_ignore_heap(&(simix_global->process_that_ran), sizeof(simix_global->process_that_ran));
1784   MC_ignore_heap(simix_global->process_to_run, sizeof(*(simix_global->process_to_run)));
1785   MC_ignore_heap(simix_global->process_that_ran, sizeof(*(simix_global->process_that_ran)));
1786   
1787   smx_process_t process;
1788   xbt_swag_foreach(process, simix_global->process_list){
1789     MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
1790   }
1791
1792   if(raw_mem_set)
1793     MC_SET_RAW_MEM;
1794
1795 }
1796
1797 static void MC_init_dot_output(){ /* FIXME : more colors */
1798
1799   colors[0] = "blue";
1800   colors[1] = "red";
1801   colors[2] = "green3";
1802   colors[3] = "goldenrod";
1803   colors[4] = "brown";
1804   colors[5] = "purple";
1805   colors[6] = "magenta";
1806   colors[7] = "turquoise4";
1807   colors[8] = "gray25";
1808   colors[9] = "forestgreen";
1809   colors[10] = "hotpink";
1810   colors[11] = "lightblue";
1811   colors[12] = "tan";
1812
1813   dot_output = fopen(_sg_mc_dot_output_file, "w");
1814   
1815   if(dot_output == NULL){
1816     perror("Error open dot output file");
1817     xbt_abort();
1818   }
1819
1820   fprintf(dot_output, "digraph graphname{\n fixedsize=true; rankdir=TB; ranksep=.25; edge [fontsize=12]; node [fontsize=10, shape=circle,width=.5 ]; graph [resolution=20, fontsize=10];\n");
1821
1822 }
1823
1824 /*******************************  Core of MC *******************************/
1825 /**************************************************************************/
1826
1827 void MC_do_the_modelcheck_for_real() {
1828
1829   MC_SET_RAW_MEM;
1830   mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
1831   MC_UNSET_RAW_MEM;
1832   
1833   if (!_sg_mc_property_file || _sg_mc_property_file[0]=='\0') {
1834     if (mc_reduce_kind==e_mc_reduce_unset)
1835       mc_reduce_kind=e_mc_reduce_dpor;
1836
1837     XBT_INFO("Check a safety property");
1838     MC_modelcheck_safety();
1839
1840   } else  {
1841
1842     if (mc_reduce_kind==e_mc_reduce_unset)
1843       mc_reduce_kind=e_mc_reduce_none;
1844
1845     XBT_INFO("Check the liveness property %s",_sg_mc_property_file);
1846     MC_automaton_load(_sg_mc_property_file);
1847     MC_modelcheck_liveness();
1848   }
1849 }
1850
1851 void MC_modelcheck_safety(void)
1852 {
1853   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
1854
1855   /* Check if MC is already initialized */
1856   if (initial_state_safety)
1857     return;
1858
1859   mc_time = xbt_new0(double, simix_process_maxpid);
1860
1861   /* mc_time refers to clock for each process -> ignore it for heap comparison */  
1862   MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
1863
1864   /* Initialize the data structures that must be persistent across every
1865      iteration of the model-checker (in RAW memory) */
1866   
1867   MC_SET_RAW_MEM;
1868
1869   /* Initialize statistics */
1870   mc_stats = xbt_new0(s_mc_stats_t, 1);
1871   mc_stats->state_size = 1;
1872
1873   /* Create exploration stack */
1874   mc_stack_safety = xbt_fifo_new();
1875
1876   if((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0]!='\0'))
1877     MC_init_dot_output();
1878
1879   MC_UNSET_RAW_MEM;
1880
1881   if(_sg_mc_visited > 0){
1882     MC_init();
1883   }else{
1884     MC_SET_RAW_MEM;
1885     MC_init_memory_map_info();
1886     MC_get_libsimgrid_plt_section();
1887     MC_get_binary_plt_section();
1888     MC_UNSET_RAW_MEM;
1889   }
1890
1891   MC_dpor_init();
1892
1893   MC_SET_RAW_MEM;
1894   /* Save the initial state */
1895   initial_state_safety = xbt_new0(s_mc_global_t, 1);
1896   initial_state_safety->snapshot = MC_take_snapshot(0);
1897   MC_UNSET_RAW_MEM;
1898
1899   MC_dpor();
1900
1901   if(raw_mem_set)
1902     MC_SET_RAW_MEM;
1903
1904   xbt_abort();
1905   //MC_exit();
1906 }
1907
1908 void MC_modelcheck_liveness(){
1909
1910   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
1911
1912   MC_init();
1913
1914   mc_time = xbt_new0(double, simix_process_maxpid);
1915
1916   /* mc_time refers to clock for each process -> ignore it for heap comparison */  
1917   MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
1918  
1919   MC_SET_RAW_MEM;
1920  
1921   /* Initialize statistics */
1922   mc_stats = xbt_new0(s_mc_stats_t, 1);
1923   mc_stats->state_size = 1;
1924
1925   /* Create exploration stack */
1926   mc_stack_liveness = xbt_fifo_new();
1927
1928   /* Create the initial state */
1929   initial_state_liveness = xbt_new0(s_mc_global_t, 1);
1930
1931   if((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0]!='\0'))
1932     MC_init_dot_output();
1933   
1934   MC_UNSET_RAW_MEM;
1935
1936   MC_ddfs_init();
1937
1938   /* We're done */
1939   MC_print_statistics(mc_stats);
1940   xbt_free(mc_time);
1941
1942   if(raw_mem_set)
1943     MC_SET_RAW_MEM;
1944
1945 }
1946
1947
1948 void MC_exit(void)
1949 {
1950   xbt_free(mc_time);
1951   MC_memory_exit();
1952   //xbt_abort();
1953 }
1954
1955 int SIMIX_pre_mc_random(smx_simcall_t simcall, int min, int max){
1956
1957   return simcall->mc_value;
1958 }
1959
1960
1961 int MC_random(int min, int max)
1962 {
1963   /*FIXME: return mc_current_state->executed_transition->random.value;*/
1964   return simcall_mc_random(min, max);
1965 }
1966
1967 /**
1968  * \brief Schedules all the process that are ready to run
1969  */
1970 void MC_wait_for_requests(void)
1971 {
1972   smx_process_t process;
1973   smx_simcall_t req;
1974   unsigned int iter;
1975
1976   while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
1977     SIMIX_process_runall();
1978     xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
1979       req = &process->simcall;
1980       if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
1981         SIMIX_simcall_pre(req, 0);
1982     }
1983   }
1984 }
1985
1986 int MC_deadlock_check()
1987 {
1988   int deadlock = FALSE;
1989   smx_process_t process;
1990   if(xbt_swag_size(simix_global->process_list)){
1991     deadlock = TRUE;
1992     xbt_swag_foreach(process, simix_global->process_list){
1993       if(process->simcall.call != SIMCALL_NONE
1994          && MC_request_is_enabled(&process->simcall)){
1995         deadlock = FALSE;
1996         break;
1997       }
1998     }
1999   }
2000   return deadlock;
2001 }
2002
2003 /**
2004  * \brief Re-executes from the state at position start all the transitions indicated by
2005  *        a given model-checker stack.
2006  * \param stack The stack with the transitions to execute.
2007  * \param start Start index to begin the re-execution.
2008  */
2009 void MC_replay(xbt_fifo_t stack, int start)
2010 {
2011   int raw_mem = (mmalloc_get_current_heap() == raw_heap);
2012
2013   int value, i = 1, count = 1;
2014   char *req_str;
2015   smx_simcall_t req = NULL, saved_req = NULL;
2016   xbt_fifo_item_t item, start_item;
2017   mc_state_t state;
2018   smx_process_t process = NULL;
2019
2020   XBT_DEBUG("**** Begin Replay ****");
2021
2022   if(start == -1){
2023     /* Restore the initial state */
2024     MC_restore_snapshot(initial_state_safety->snapshot);
2025     /* At the moment of taking the snapshot the raw heap was set, so restoring
2026      * it will set it back again, we have to unset it to continue  */
2027     MC_UNSET_RAW_MEM;
2028   }
2029
2030   start_item = xbt_fifo_get_last_item(stack);
2031   if(start != -1){
2032     while (i != start){
2033       start_item = xbt_fifo_get_prev_item(start_item);
2034       i++;
2035     }
2036   }
2037
2038   MC_SET_RAW_MEM;
2039   xbt_dict_reset(first_enabled_state);
2040   xbt_swag_foreach(process, simix_global->process_list){
2041     if(MC_process_is_enabled(process)){
2042       char *key = bprintf("%lu", process->pid);
2043       char *data = bprintf("%d", count);
2044       xbt_dict_set(first_enabled_state, key, data, NULL);
2045       xbt_free(key);
2046     }
2047   }
2048   MC_UNSET_RAW_MEM;
2049   
2050
2051   /* Traverse the stack from the state at position start and re-execute the transitions */
2052   for (item = start_item;
2053        item != xbt_fifo_get_first_item(stack);
2054        item = xbt_fifo_get_prev_item(item)) {
2055
2056     state = (mc_state_t) xbt_fifo_get_item_content(item);
2057     saved_req = MC_state_get_executed_request(state, &value);
2058    
2059     MC_SET_RAW_MEM;
2060     char *key = bprintf("%lu", saved_req->issuer->pid);
2061     xbt_dict_remove(first_enabled_state, key); 
2062     xbt_free(key);
2063     MC_UNSET_RAW_MEM;
2064    
2065     if(saved_req){
2066       /* because we got a copy of the executed request, we have to fetch the  
2067          real one, pointed by the request field of the issuer process */
2068       req = &saved_req->issuer->simcall;
2069
2070       /* Debug information */
2071       if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
2072         req_str = MC_request_to_string(req, value);
2073         XBT_DEBUG("Replay: %s (%p)", req_str, state);
2074         xbt_free(req_str);
2075       }
2076     }
2077     
2078     SIMIX_simcall_pre(req, value);
2079     MC_wait_for_requests();
2080
2081     count++;
2082
2083     MC_SET_RAW_MEM;
2084     /* Insert in dict all enabled processes */
2085     xbt_swag_foreach(process, simix_global->process_list){
2086       if(MC_process_is_enabled(process) /*&& !MC_state_process_is_done(state, process)*/){
2087         char *key = bprintf("%lu", process->pid);
2088         if(xbt_dict_get_or_null(first_enabled_state, key) == NULL){
2089           char *data = bprintf("%d", count);
2090           xbt_dict_set(first_enabled_state, key, data, NULL);
2091         }
2092         xbt_free(key);
2093       }
2094     }
2095     MC_UNSET_RAW_MEM;
2096          
2097     /* Update statistics */
2098     mc_stats->visited_states++;
2099     mc_stats->executed_transitions++;
2100
2101   }
2102
2103   XBT_DEBUG("**** End Replay ****");
2104
2105   if(raw_mem)
2106     MC_SET_RAW_MEM;
2107   else
2108     MC_UNSET_RAW_MEM;
2109   
2110
2111 }
2112
2113 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
2114 {
2115
2116   initial_state_liveness->raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
2117
2118   int value;
2119   char *req_str;
2120   smx_simcall_t req = NULL, saved_req = NULL;
2121   xbt_fifo_item_t item;
2122   mc_state_t state;
2123   mc_pair_t pair;
2124   int depth = 1;
2125
2126   XBT_DEBUG("**** Begin Replay ****");
2127
2128   /* Restore the initial state */
2129   MC_restore_snapshot(initial_state_liveness->snapshot);
2130
2131   /* At the moment of taking the snapshot the raw heap was set, so restoring
2132    * it will set it back again, we have to unset it to continue  */
2133   if(!initial_state_liveness->raw_mem_set)
2134     MC_UNSET_RAW_MEM;
2135
2136   if(all_stack){
2137
2138     item = xbt_fifo_get_last_item(stack);
2139
2140     while(depth <= xbt_fifo_size(stack)){
2141
2142       pair = (mc_pair_t) xbt_fifo_get_item_content(item);
2143       state = (mc_state_t) pair->graph_state;
2144
2145       if(pair->requests > 0){
2146    
2147         saved_req = MC_state_get_executed_request(state, &value);
2148         //XBT_DEBUG("SavedReq->call %u", saved_req->call);
2149       
2150         if(saved_req != NULL){
2151           /* because we got a copy of the executed request, we have to fetch the  
2152              real one, pointed by the request field of the issuer process */
2153           req = &saved_req->issuer->simcall;
2154           //XBT_DEBUG("Req->call %u", req->call);
2155   
2156           /* Debug information */
2157           if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
2158             req_str = MC_request_to_string(req, value);
2159             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
2160             xbt_free(req_str);
2161           }
2162   
2163         }
2164  
2165         SIMIX_simcall_pre(req, value);
2166         MC_wait_for_requests();
2167       }
2168
2169       depth++;
2170     
2171       /* Update statistics */
2172       mc_stats->visited_pairs++;
2173       mc_stats->executed_transitions++;
2174
2175       item = xbt_fifo_get_prev_item(item);
2176     }
2177
2178   }else{
2179
2180     /* Traverse the stack from the initial state and re-execute the transitions */
2181     for (item = xbt_fifo_get_last_item(stack);
2182          item != xbt_fifo_get_first_item(stack);
2183          item = xbt_fifo_get_prev_item(item)) {
2184
2185       pair = (mc_pair_t) xbt_fifo_get_item_content(item);
2186       state = (mc_state_t) pair->graph_state;
2187
2188       if(pair->requests > 0){
2189    
2190         saved_req = MC_state_get_executed_request(state, &value);
2191         //XBT_DEBUG("SavedReq->call %u", saved_req->call);
2192       
2193         if(saved_req != NULL){
2194           /* because we got a copy of the executed request, we have to fetch the  
2195              real one, pointed by the request field of the issuer process */
2196           req = &saved_req->issuer->simcall;
2197           //XBT_DEBUG("Req->call %u", req->call);
2198   
2199           /* Debug information */
2200           if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
2201             req_str = MC_request_to_string(req, value);
2202             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
2203             xbt_free(req_str);
2204           }
2205   
2206         }
2207  
2208         SIMIX_simcall_pre(req, value);
2209         MC_wait_for_requests();
2210       }
2211
2212       depth++;
2213     
2214       /* Update statistics */
2215       mc_stats->visited_pairs++;
2216       mc_stats->executed_transitions++;
2217     }
2218   }  
2219
2220   XBT_DEBUG("**** End Replay ****");
2221
2222   if(initial_state_liveness->raw_mem_set)
2223     MC_SET_RAW_MEM;
2224   else
2225     MC_UNSET_RAW_MEM;
2226   
2227 }
2228
2229 /**
2230  * \brief Dumps the contents of a model-checker's stack and shows the actual
2231  *        execution trace
2232  * \param stack The stack to dump
2233  */
2234 void MC_dump_stack_safety(xbt_fifo_t stack)
2235 {
2236   
2237   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
2238
2239   MC_show_stack_safety(stack);
2240
2241   if(!_sg_mc_checkpoint){
2242
2243     mc_state_t state;
2244
2245     MC_SET_RAW_MEM;
2246     while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
2247       MC_state_delete(state);
2248     MC_UNSET_RAW_MEM;
2249
2250   }
2251
2252   if(raw_mem_set)
2253     MC_SET_RAW_MEM;
2254   else
2255     MC_UNSET_RAW_MEM;
2256   
2257 }
2258
2259
2260 void MC_show_stack_safety(xbt_fifo_t stack)
2261 {
2262
2263   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
2264
2265   MC_SET_RAW_MEM;
2266
2267   int value;
2268   mc_state_t state;
2269   xbt_fifo_item_t item;
2270   smx_simcall_t req;
2271   char *req_str = NULL;
2272   
2273   for (item = xbt_fifo_get_last_item(stack);
2274        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
2275         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
2276     req = MC_state_get_executed_request(state, &value);
2277     if(req){
2278       req_str = MC_request_to_string(req, value);
2279       XBT_INFO("%s", req_str);
2280       xbt_free(req_str);
2281     }
2282   }
2283
2284   if(!raw_mem_set)
2285     MC_UNSET_RAW_MEM;
2286 }
2287
2288 void MC_show_deadlock(smx_simcall_t req)
2289 {
2290   /*char *req_str = NULL;*/
2291   XBT_INFO("**************************");
2292   XBT_INFO("*** DEAD-LOCK DETECTED ***");
2293   XBT_INFO("**************************");
2294   XBT_INFO("Locked request:");
2295   /*req_str = MC_request_to_string(req);
2296     XBT_INFO("%s", req_str);
2297     xbt_free(req_str);*/
2298   XBT_INFO("Counter-example execution trace:");
2299   MC_dump_stack_safety(mc_stack_safety);
2300   MC_print_statistics(mc_stats);
2301 }
2302
2303
2304 void MC_show_stack_liveness(xbt_fifo_t stack){
2305   int value;
2306   mc_pair_t pair;
2307   xbt_fifo_item_t item;
2308   smx_simcall_t req;
2309   char *req_str = NULL;
2310   
2311   for (item = xbt_fifo_get_last_item(stack);
2312        (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
2313         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
2314     req = MC_state_get_executed_request(pair->graph_state, &value);
2315     if(req){
2316       if(pair->requests>0){
2317         req_str = MC_request_to_string(req, value);
2318         XBT_INFO("%s", req_str);
2319         xbt_free(req_str);
2320       }else{
2321         XBT_INFO("End of system requests but evolution in Büchi automaton");
2322       }
2323     }
2324   }
2325 }
2326
2327 void MC_dump_stack_liveness(xbt_fifo_t stack){
2328
2329   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
2330
2331   mc_pair_t pair;
2332
2333   MC_SET_RAW_MEM;
2334   while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
2335     MC_pair_delete(pair);
2336   MC_UNSET_RAW_MEM;
2337
2338   if(raw_mem_set)
2339     MC_SET_RAW_MEM;
2340
2341 }
2342
2343
2344 void MC_print_statistics(mc_stats_t stats)
2345 {
2346   if(stats->expanded_pairs == 0){
2347     XBT_INFO("Expanded states = %lu", stats->expanded_states);
2348     XBT_INFO("Visited states = %lu", stats->visited_states);
2349   }else{
2350     XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
2351     XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
2352   }
2353   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
2354   MC_SET_RAW_MEM;
2355   if((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0]!='\0')){
2356     fprintf(dot_output, "}\n");
2357     fclose(dot_output);
2358   }
2359   MC_UNSET_RAW_MEM;
2360 }
2361
2362 void MC_assert(int prop)
2363 {
2364   if (MC_is_active() && !prop){
2365     XBT_INFO("**************************");
2366     XBT_INFO("*** PROPERTY NOT VALID ***");
2367     XBT_INFO("**************************");
2368     XBT_INFO("Counter-example execution trace:");
2369     MC_dump_stack_safety(mc_stack_safety);
2370     MC_print_statistics(mc_stats);
2371     xbt_abort();
2372   }
2373 }
2374
2375 void MC_cut(void){
2376   user_max_depth_reached = 1;
2377 }
2378
2379 void MC_process_clock_add(smx_process_t process, double amount)
2380 {
2381   mc_time[process->pid] += amount;
2382 }
2383
2384 double MC_process_clock_get(smx_process_t process)
2385 {
2386   if(mc_time){
2387     if(process != NULL)
2388       return mc_time[process->pid];
2389     else 
2390       return -1;
2391   }else{
2392     return 0;
2393   }
2394 }
2395
2396 void MC_automaton_load(const char *file){
2397
2398   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
2399
2400   MC_SET_RAW_MEM;
2401
2402   if (_mc_property_automaton == NULL)
2403     _mc_property_automaton = xbt_automaton_new();
2404   
2405   xbt_automaton_load(_mc_property_automaton,file);
2406
2407   MC_UNSET_RAW_MEM;
2408
2409   if(raw_mem_set)
2410     MC_SET_RAW_MEM;
2411
2412 }
2413
2414 void MC_automaton_new_propositional_symbol(const char* id, void* fct) {
2415
2416   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
2417
2418   MC_SET_RAW_MEM;
2419
2420   if (_mc_property_automaton == NULL)
2421     _mc_property_automaton = xbt_automaton_new();
2422
2423   xbt_automaton_propositional_symbol_new(_mc_property_automaton,id,fct);
2424
2425   MC_UNSET_RAW_MEM;
2426
2427   if(raw_mem_set)
2428     MC_SET_RAW_MEM;
2429   
2430 }
2431
2432
2433