Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : init MC for liveness in a separate function
[simgrid.git] / src / mc / mc_checkpoint.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 <libgen.h>
7 #include "mc_private.h"
8 #include "xbt/module.h"
9
10 #include "../simix/smx_private.h"
11
12 #include <libunwind.h>
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
15                                 "Logging specific to mc_checkpoint");
16
17 void *start_text_libsimgrid;
18 void *start_plt_libsimgrid, *end_plt_libsimgrid;
19 void *start_plt_binary, *end_plt_binary;
20 char *libsimgrid_path;
21 void *start_data_libsimgrid;
22 void *start_text_binary;
23 void *end_raw_heap;
24
25 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size);
26 static void MC_region_restore(mc_mem_region_t reg);
27 static void MC_region_destroy(mc_mem_region_t reg);
28
29 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size);
30
31 static void add_value(xbt_dynar_t *list, const char *type, unsigned long int val);
32 static xbt_dynar_t take_snapshot_stacks(void *heap);
33 static xbt_strbuff_t get_local_variables_values(void *stack_context, void *heap);
34 static void print_local_variables_values(xbt_dynar_t all_variables);
35 static void *get_stack_pointer(void *stack_context, void *heap);
36
37 static void snapshot_stack_free(mc_snapshot_stack_t s);
38
39 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
40 {
41   mc_mem_region_t new_reg = xbt_new0(s_mc_mem_region_t, 1);
42   new_reg->type = type;
43   new_reg->start_addr = start_addr;
44   new_reg->size = size;
45   new_reg->data = xbt_malloc0(size);
46   memcpy(new_reg->data, start_addr, size);
47
48   XBT_DEBUG("New region : type : %d, data : %p, size : %zu", type, new_reg->data, size);
49   
50   return new_reg;
51 }
52
53 static void MC_region_restore(mc_mem_region_t reg)
54 {
55   /*FIXME: check if start_addr is still mapped, if it is not, then map it
56     before copying the data */
57  
58   memcpy(reg->start_addr, reg->data, reg->size);
59  
60   return;
61 }
62
63 static void MC_region_destroy(mc_mem_region_t reg)
64 {
65   xbt_free(reg->data);
66   xbt_free(reg);
67 }
68
69 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size)
70 {
71   mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
72   snapshot->regions = xbt_realloc(snapshot->regions, (snapshot->num_reg + 1) * sizeof(mc_mem_region_t));
73   snapshot->regions[snapshot->num_reg] = new_reg;
74   snapshot->num_reg++;
75   return;
76
77
78 void MC_take_snapshot(mc_snapshot_t snapshot)
79 {
80   unsigned int i = 0;
81   s_map_region_t reg;
82   memory_map_t maps = get_memory_map();
83
84   /* Save the std heap and the writable mapped pages of libsimgrid */
85   while (i < maps->mapsize) {
86     reg = maps->regions[i];
87     if ((reg.prot & PROT_WRITE)){
88       if (maps->regions[i].pathname == NULL){
89         if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
90           MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
91         }
92         i++;
93       } else {
94         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
95           MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
96           i++;
97           reg = maps->regions[i];
98           while(reg.pathname == NULL && (reg.prot & PROT_WRITE) && i < maps->mapsize){
99             MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
100             i++;
101             reg = maps->regions[i];
102           }
103         }else{
104           i++;
105         } 
106       }
107     }else{
108       i++;
109     }
110   }
111
112   free_memory_map(maps);
113 }
114
115 void MC_init_memory_map_info(){
116   
117   unsigned int i = 0;
118   s_map_region_t reg;
119   memory_map_t maps = get_memory_map();
120
121    while (i < maps->mapsize) {
122     reg = maps->regions[i];
123     if ((reg.prot & PROT_WRITE)){
124       if (maps->regions[i].pathname == NULL){
125         if(reg.start_addr == raw_heap){
126           end_raw_heap = reg.end_addr;
127         }
128       } else {
129         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
130           start_data_libsimgrid = reg.start_addr;
131         }
132       }
133     }else if ((reg.prot & PROT_READ)){
134       if (maps->regions[i].pathname != NULL){
135         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
136           start_text_libsimgrid = reg.start_addr;
137           libsimgrid_path = strdup(maps->regions[i].pathname);
138         }else{
139           if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
140             start_text_binary = reg.start_addr;
141           }
142         }
143       }
144     }
145     i++;
146   }
147
148 }
149
150 mc_snapshot_t MC_take_snapshot_liveness()
151 {
152
153   raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
154
155   MC_SET_RAW_MEM;
156
157   mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
158
159   unsigned int i = 0;
160   s_map_region_t reg;
161   memory_map_t maps = get_memory_map();
162   int nb_reg = 0;
163   void *heap = NULL;
164
165   /* Save the std heap and the writable mapped pages of libsimgrid */
166   while (i < maps->mapsize) {
167     reg = maps->regions[i];
168     if ((reg.prot & PROT_WRITE)){
169       if (maps->regions[i].pathname == NULL){
170         if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
171           MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
172           heap = snapshot->regions[nb_reg]->data;
173           nb_reg++;
174         }else if(reg.start_addr == raw_heap){
175           end_raw_heap = reg.end_addr;
176         }
177         i++;
178       } else {
179         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
180           MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
181           start_data_libsimgrid = reg.start_addr;
182           nb_reg++;
183           i++;
184           reg = maps->regions[i];
185           while(reg.pathname == NULL && (reg.prot & PROT_WRITE) && i < maps->mapsize){
186             MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
187             i++;
188             reg = maps->regions[i];
189             nb_reg++;
190           }
191         } else {
192           if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
193             MC_snapshot_add_region(snapshot, 2, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
194             nb_reg++;
195           }
196           i++;
197         }
198       }
199     }else if ((reg.prot & PROT_READ)){
200       if (maps->regions[i].pathname != NULL){
201         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
202           start_text_libsimgrid = reg.start_addr;
203           libsimgrid_path = strdup(maps->regions[i].pathname);
204         }else{
205           if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
206             start_text_binary = reg.start_addr;
207           }
208         }
209       }
210       i++;
211     }else{
212       i++;
213     }
214   }
215
216   snapshot->stacks = take_snapshot_stacks(heap);
217   
218   free_memory_map(maps);
219
220   MC_UNSET_RAW_MEM;
221
222   if(raw_mem_set)
223     MC_SET_RAW_MEM;
224
225   return snapshot;
226
227 }
228
229 void MC_restore_snapshot(mc_snapshot_t snapshot)
230 {
231   unsigned int i;
232   for(i=0; i < snapshot->num_reg; i++){
233     MC_region_restore(snapshot->regions[i]);
234   }
235
236 }
237
238 void MC_free_snapshot(mc_snapshot_t snapshot)
239 {
240   unsigned int i;
241   for(i=0; i < snapshot->num_reg; i++)
242     MC_region_destroy(snapshot->regions[i]);
243
244   xbt_dynar_free(&(snapshot->stacks));
245   xbt_free(snapshot);
246 }
247
248
249 void get_libsimgrid_plt_section(){
250
251   FILE *fp;
252   char *line = NULL;            /* Temporal storage for each line that is readed */
253   ssize_t read;                 /* Number of bytes readed */
254   size_t n = 0;                 /* Amount of bytes to read by getline */
255
256   char *lfields[7];
257   int i, plt_not_found = 1;
258   unsigned long int size, offset;
259
260   if(libsimgrid_path == NULL)
261     libsimgrid_path = get_libsimgrid_path();
262
263   char *command = bprintf("objdump --section-headers %s", libsimgrid_path);
264
265   fp = popen(command, "r");
266
267   if(fp == NULL)
268     perror("popen failed");
269
270   while ((read = getline(&line, &n, fp)) != -1 && plt_not_found == 1) {
271
272     if(n == 0)
273       continue;
274
275     /* Wipeout the new line character */
276     line[read - 1] = '\0';
277
278     lfields[0] = strtok(line, " ");
279
280     if(lfields[0] == NULL)
281       continue;
282
283     if(strcmp(lfields[0], "Sections:") == 0 || strcmp(lfields[0], "Idx") == 0 || strncmp(lfields[0], libsimgrid_path, strlen(libsimgrid_path)) == 0)
284       continue;
285
286     for (i = 1; i < 7 && lfields[i - 1] != NULL; i++) {
287       lfields[i] = strtok(NULL, " ");
288     }
289
290     if(i>=6){
291       if(strcmp(lfields[1], ".plt") == 0){
292         size = strtoul(lfields[2], NULL, 16);
293         offset = strtoul(lfields[5], NULL, 16);
294         start_plt_libsimgrid = (char *)start_text_libsimgrid + offset;
295         end_plt_libsimgrid = (char *)start_plt_libsimgrid + size;
296         plt_not_found = 0;
297       }
298     }
299     
300   }
301
302   free(command);
303   free(line);
304   pclose(fp);
305
306 }
307
308 void get_binary_plt_section(){
309
310   FILE *fp;
311   char *line = NULL;            /* Temporal storage for each line that is readed */
312   ssize_t read;                 /* Number of bytes readed */
313   size_t n = 0;                 /* Amount of bytes to read by getline */
314
315   char *lfields[7];
316   int i, plt_not_found = 1;
317   unsigned long int size, offset;
318
319   char *command = bprintf( "objdump --section-headers %s", xbt_binary_name);
320
321   fp = popen(command, "r");
322
323   if(fp == NULL)
324     perror("popen failed");
325
326   while ((read = getline(&line, &n, fp)) != -1 && plt_not_found == 1) {
327
328     if(n == 0)
329       continue;
330
331     /* Wipeout the new line character */
332     line[read - 1] = '\0';
333
334     lfields[0] = strtok(line, " ");
335
336     if(lfields[0] == NULL)
337       continue;
338
339     if(strcmp(lfields[0], "Sections:") == 0 || strcmp(lfields[0], "Idx") == 0 || strncmp(lfields[0], basename(xbt_binary_name), strlen(xbt_binary_name)) == 0)
340       continue;
341
342     for (i = 1; i < 7 && lfields[i - 1] != NULL; i++) {
343       lfields[i] = strtok(NULL, " ");
344     }
345
346     if(i>=6){
347       if(strcmp(lfields[1], ".plt") == 0){
348         size = strtoul(lfields[2], NULL, 16);
349         offset = strtoul(lfields[5], NULL, 16);
350         start_plt_binary = (char *)start_text_binary + offset;
351         end_plt_binary = (char *)start_plt_binary + size;
352         plt_not_found = 0;
353       }
354     }
355     
356     
357   }
358
359   free(command);
360   free(line);
361   pclose(fp);
362
363 }
364
365 static void add_value(xbt_dynar_t *list, const char *type, unsigned long int val){
366   variable_value_t value = xbt_new0(s_variable_value_t, 1);
367   value->type = strdup(type);
368   if(strcmp(type, "value") == 0){
369     value->value.res = val;
370   }else{
371     value->value.address = (void *)val;
372   }
373   xbt_dynar_push(*list, &value);
374 }
375
376 static xbt_dynar_t take_snapshot_stacks(void *heap){
377
378   xbt_dynar_t res = xbt_dynar_new(sizeof(s_mc_snapshot_stack_t), NULL);
379
380   unsigned int cursor1 = 0;
381   stack_region_t current_stack;
382   
383   xbt_dynar_foreach(stacks_areas, cursor1, current_stack){
384     mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
385     st->local_variables = get_local_variables_values(current_stack->context, heap);
386     st->stack_pointer = get_stack_pointer(current_stack->context, heap);
387     xbt_dynar_push(res, &st);
388   }
389
390   return res;
391
392 }
393
394 static void *get_stack_pointer(void *stack_context, void *heap){
395
396   unw_cursor_t c;
397   int ret;
398   unw_word_t sp;
399
400   ret = unw_init_local(&c, (unw_context_t *)stack_context);
401   if(ret < 0){
402     XBT_INFO("unw_init_local failed");
403     xbt_abort();
404   }
405
406   unw_get_reg(&c, UNW_REG_SP, &sp);
407
408   return ((char *)heap + (size_t)(((char *)((long)sp) - (char*)std_heap)));
409
410 }
411
412 static xbt_strbuff_t get_local_variables_values(void *stack_context, void *heap){
413   
414   unw_cursor_t c;
415   int ret;
416   //char *stack_name;
417
418   char frame_name[256];
419   
420   ret = unw_init_local(&c, (unw_context_t *)stack_context);
421   if(ret < 0){
422     XBT_INFO("unw_init_local failed");
423     xbt_abort();
424   }
425
426   //stack_name = strdup(((smx_process_t)((smx_ctx_sysv_t)(stack->address))->super.data)->name);
427
428   unw_word_t ip, sp, off;
429   dw_frame_t frame;
430  
431   xbt_dynar_t compose = xbt_dynar_new(sizeof(variable_value_t), NULL);
432
433   xbt_strbuff_t variables = xbt_strbuff_new();
434   xbt_dict_cursor_t dict_cursor;
435   char *variable_name;
436   dw_local_variable_t current_variable;
437   unsigned int cursor = 0, cursor2 = 0;
438   dw_location_entry_t entry = NULL;
439   dw_location_t location_entry = NULL;
440   unw_word_t res;
441   int frame_found = 0;
442   void *frame_pointer_address = NULL;
443   long true_ip;
444
445   while(ret >= 0){
446
447     unw_get_reg(&c, UNW_REG_IP, &ip);
448     unw_get_reg(&c, UNW_REG_SP, &sp);
449
450     unw_get_proc_name (&c, frame_name, sizeof (frame_name), &off);
451
452     xbt_strbuff_append(variables, bprintf("ip=%s\n", frame_name));
453
454     frame = xbt_dict_get_or_null(mc_local_variables, frame_name);
455
456     if(frame == NULL){
457       ret = unw_step(&c);
458       continue;
459     }
460
461     true_ip = (long)frame->low_pc + (long)off;
462
463     /* Get frame pointer */
464     switch(frame->frame_base->type){
465     case e_dw_loclist:
466       while((cursor < xbt_dynar_length(frame->frame_base->location.loclist)) && frame_found == 0){
467         entry = xbt_dynar_get_as(frame->frame_base->location.loclist, cursor, dw_location_entry_t);
468         if((true_ip >= entry->lowpc) && (true_ip < entry->highpc)){
469           frame_found = 1;
470           switch(entry->location->type){
471           case e_dw_compose:
472             xbt_dynar_reset(compose);
473             cursor2 = 0;
474             while(cursor2 < xbt_dynar_length(entry->location->location.compose)){
475               location_entry = xbt_dynar_get_as(entry->location->location.compose, cursor2, dw_location_t);
476               switch(location_entry->type){
477               case e_dw_register:
478                 unw_get_reg(&c, location_entry->location.reg, &res);
479                 add_value(&compose, "address", (long)res);
480                 break;
481               case e_dw_bregister_op:
482                 unw_get_reg(&c, location_entry->location.breg_op.reg, &res);
483                 add_value(&compose, "address", (long)res + location_entry->location.breg_op.offset);
484                 break;
485               default:
486                 xbt_dynar_reset(compose);
487                 break;
488               }
489               cursor2++;
490             }
491
492             if(xbt_dynar_length(compose) > 0){
493               frame_pointer_address = xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.address ; 
494             }
495             break;
496           default :
497             frame_pointer_address = NULL;
498             break;
499           }
500         }
501         cursor++;
502       }
503       break;
504     default :
505       frame_pointer_address = NULL;
506       break;
507     }
508
509     frame_found = 0;
510     cursor = 0;
511
512     //XBT_INFO("Frame %s", frame->name);
513
514     xbt_dict_foreach(frame->variables, dict_cursor, variable_name, current_variable){
515       if(current_variable->location != NULL){
516         switch(current_variable->location->type){
517         case e_dw_compose:
518           xbt_dynar_reset(compose);
519           cursor = 0;
520           while(cursor < xbt_dynar_length(current_variable->location->location.compose)){
521             location_entry = xbt_dynar_get_as(current_variable->location->location.compose, cursor, dw_location_t);
522             switch(location_entry->type){
523             case e_dw_register:
524               unw_get_reg(&c, location_entry->location.reg, &res);
525               add_value(&compose, "value", (long)res);
526               break;
527             case e_dw_bregister_op:
528               unw_get_reg(&c, location_entry->location.breg_op.reg, &res);
529               add_value(&compose, "address", (long)res + location_entry->location.breg_op.offset);
530               break;
531             case e_dw_fbregister_op:
532               if(frame_pointer_address != NULL)
533                 add_value(&compose, "address", (long)((char *)frame_pointer_address + location_entry->location.fbreg_op));
534               break;
535             default:
536               xbt_dynar_reset(compose);
537               break;
538             }
539             cursor++;
540           }
541           
542           if(xbt_dynar_length(compose) > 0){
543             if(strcmp(xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->type, "value") == 0){
544               //XBT_INFO("Variable : %s - value : %lx", current_variable->name, xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.res);
545               xbt_strbuff_append(variables, bprintf("%s=%lx\n", current_variable->name, xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.res));
546             }else{
547               if(*((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1,variable_value_t)->value.address) == NULL){
548                 //XBT_INFO("Variable : %s - address : NULL", current_variable->name);
549                 xbt_strbuff_append(variables, bprintf("%s=NULL\n", current_variable->name));
550               }else if(((long)*((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1,variable_value_t)->value.address) > 0xffffffff) || ((long)*((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1,variable_value_t)->value.address) < (long)start_text_binary)){
551                 //XBT_INFO("Variable : %s - value : %zd", current_variable->name, (size_t)*((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.address));
552                 xbt_strbuff_append(variables, bprintf("%s=%zd\n", current_variable->name, (size_t)*((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.address)));
553               }else{
554                 //XBT_INFO("Variable : %s - address : %p", current_variable->name, *((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.address));  
555                 xbt_strbuff_append(variables, bprintf("%s=%p\n", current_variable->name, *((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.address)));
556               }
557             }
558           }else{
559             //XBT_INFO("Variable %s undefined", current_variable->name);
560             xbt_strbuff_append(variables, bprintf("%s=undefined\n", current_variable->name));
561           }
562           break;
563         default :
564           break;
565         }
566       }else{
567         //XBT_INFO("Variable : %s, no location", current_variable->name);
568         xbt_strbuff_append(variables, bprintf("%s=undefined\n", current_variable->name));
569       }
570     }    
571  
572     ret = unw_step(&c);
573
574     //XBT_INFO(" ");
575      
576   }
577
578   //free(stack_name);
579
580   return variables;
581
582 }
583
584 static void print_local_variables_values(xbt_dynar_t all_variables){
585
586   unsigned cursor = 0;
587   mc_snapshot_stack_t stack;
588
589   xbt_dynar_foreach(all_variables, cursor, stack){
590     XBT_INFO("%s", stack->local_variables->data);
591   }
592 }
593
594
595 static void snapshot_stack_free(mc_snapshot_stack_t s){
596   if(s){
597     xbt_free(s->local_variables->data);
598     xbt_free(s->local_variables);
599     xbt_free(s);
600   }
601 }
602
603 void snapshot_stack_free_voidp(void *s){
604   snapshot_stack_free((mc_snapshot_stack_t) * (void **) s);
605 }