Logo AND Algorithmique Numérique Distribuée

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