Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : add region (data of program) in snapshot
[simgrid.git] / src / mc / mc_checkpoint.c
1 #include <libgen.h>
2 #include "private.h"
3
4
5 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
6                                 "Logging specific to mc_checkpoint");
7
8 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size);
9 static void MC_region_restore(mc_mem_region_t reg);
10 static void MC_region_destroy(mc_mem_region_t reg);
11 static void MC_snapshot_destroy(mc_snapshot_t s);
12
13 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size);
14
15 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
16 {
17   mc_mem_region_t new_reg = xbt_new0(s_mc_mem_region_t, 1);
18   new_reg->type = type;
19   new_reg->start_addr = start_addr;
20   new_reg->size = size;
21   new_reg->data = xbt_malloc0(size);
22   memcpy(new_reg->data, start_addr, size);
23   return new_reg;
24 }
25
26 static void MC_region_restore(mc_mem_region_t reg)
27 {
28   /*FIXME: check if start_addr is still mapped, if it is not, then map it
29     before copying the data */
30   memcpy(reg->start_addr, reg->data, reg->size);
31 }
32
33 static void MC_region_destroy(mc_mem_region_t reg)
34 {
35   xbt_free(reg->data);
36   xbt_free(reg);
37 }
38
39 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size)
40 {
41   mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
42   snapshot->regions = xbt_realloc(snapshot->regions, (snapshot->num_reg + 1) * sizeof(mc_mem_region_t));
43   snapshot->regions[snapshot->num_reg] = new_reg;
44   snapshot->num_reg++;
45   return;
46
47
48 void MC_take_snapshot(mc_snapshot_t snapshot)
49 {
50   unsigned int i = 0;
51   s_map_region reg;
52   memory_map_t maps = get_memory_map();
53
54   XBT_DEBUG("Take snapshot");
55
56   /* Save the std heap and the writable mapped pages of libsimgrid */
57   while (i < maps->mapsize) {
58     reg = maps->regions[i];
59     if ((reg.prot & PROT_WRITE)){
60       if (maps->regions[i].pathname == NULL){
61         if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
62           MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
63         }
64       } else {
65         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
66           MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
67         } 
68       }
69     }
70     i++;
71   }
72
73
74   /* FIXME: free the memory map */
75 }
76
77 static void MC_snapshot_destroy(mc_snapshot_t s){
78
79   int i;
80
81   for(i=0; i< s->num_reg; i++){
82     MC_region_destroy(s->regions[i]);
83   }
84
85   s->num_reg=0;
86
87 }
88
89 void MC_take_snapshot_liveness(mc_snapshot_t snapshot, char *prgm)
90 {
91   unsigned int i = 0;
92   s_map_region reg;
93   memory_map_t maps = get_memory_map();
94
95   MC_snapshot_destroy(snapshot);
96
97   XBT_DEBUG("Take snapshot");
98
99   /* Save the std heap and the writable mapped pages of libsimgrid */
100   while (i < maps->mapsize) {
101     reg = maps->regions[i];
102     if ((reg.prot & PROT_WRITE)){
103       if (maps->regions[i].pathname == NULL){
104         if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
105           MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
106           //XBT_DEBUG("Region heap");
107         }
108       } else {
109         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
110           MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
111           //XBT_DEBUG("Region simgrid");
112         } else {
113           if (!memcmp(basename(maps->regions[i].pathname), basename(prgm), strlen(basename(prgm)))){
114             MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
115             //XBT_DEBUG("Region prog");
116           } 
117         }
118       }
119     }
120     i++;
121   }
122
123
124   /* FIXME: free the memory map */
125 }
126
127 void MC_restore_snapshot(mc_snapshot_t snapshot)
128 {
129   unsigned int i;
130   for(i=0; i < snapshot->num_reg; i++)
131     MC_region_restore(snapshot->regions[i]);
132 }
133
134 void MC_free_snapshot(mc_snapshot_t snapshot)
135 {
136   unsigned int i;
137   for(i=0; i < snapshot->num_reg; i++)
138     MC_region_destroy(snapshot->regions[i]);
139
140   xbt_free(snapshot);
141 }