Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : remove unused variable with debug mode disabled
[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, *end_plt;
19 char *libsimgrid_path;
20 void *start_data_libsimgrid;
21
22 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size);
23 static void MC_region_restore(mc_mem_region_t reg);
24 static void MC_region_destroy(mc_mem_region_t reg);
25
26 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size);
27
28 static void add_value(xbt_dynar_t *list, const char *type, unsigned long int val);
29 static xbt_dynar_t take_snapshot_stacks(void *heap);
30 static void get_local_variables_values(xbt_dynar_t *all_variables, stack_region_t stack, void *heap);
31 static void print_local_variables_values(xbt_dynar_t all_variables);
32
33 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
34 {
35   mc_mem_region_t new_reg = xbt_new0(s_mc_mem_region_t, 1);
36   new_reg->type = type;
37   new_reg->start_addr = start_addr;
38   new_reg->size = size;
39   new_reg->data = xbt_malloc0(size);
40   memcpy(new_reg->data, start_addr, size);
41
42   XBT_DEBUG("New region : type : %d, data : %p, size : %zu", type, new_reg->data, size);
43   
44   return new_reg;
45 }
46
47 static void MC_region_restore(mc_mem_region_t reg)
48 {
49   /*FIXME: check if start_addr is still mapped, if it is not, then map it
50     before copying the data */
51  
52   memcpy(reg->start_addr, reg->data, reg->size);
53  
54   return;
55 }
56
57 static void MC_region_destroy(mc_mem_region_t reg)
58 {
59   xbt_free(reg->data);
60   xbt_free(reg);
61 }
62
63 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size)
64 {
65   mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
66   snapshot->regions = xbt_realloc(snapshot->regions, (snapshot->num_reg + 1) * sizeof(mc_mem_region_t));
67   snapshot->regions[snapshot->num_reg] = new_reg;
68   snapshot->num_reg++;
69   return;
70
71
72 void MC_take_snapshot(mc_snapshot_t snapshot)
73 {
74   unsigned int i = 0;
75   s_map_region_t reg;
76   memory_map_t maps = get_memory_map();
77
78   /* Save the std heap and the writable mapped pages of libsimgrid */
79   while (i < maps->mapsize) {
80     reg = maps->regions[i];
81     if ((reg.prot & PROT_WRITE)){
82       if (maps->regions[i].pathname == NULL){
83         if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
84           MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
85         }
86       } else {
87         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
88           MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
89         } 
90       }
91     }
92     i++;
93   }
94
95   free_memory_map(maps);
96 }
97
98 void MC_take_snapshot_liveness(mc_snapshot_t snapshot)
99 {
100   unsigned int i = 0;
101   s_map_region_t reg;
102   memory_map_t maps = get_memory_map();
103   int nb_reg = 0;
104   void *heap = NULL;
105
106   /* Save the std heap and the writable mapped pages of libsimgrid */
107   while (i < maps->mapsize && nb_reg < 3) {
108     reg = maps->regions[i];
109     if ((reg.prot & PROT_WRITE)){
110       if (maps->regions[i].pathname == NULL){
111         if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
112           MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
113           heap = snapshot->regions[nb_reg]->data;
114           nb_reg++;
115         }
116       } else {
117         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
118           MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
119           nb_reg++;
120           start_data_libsimgrid = reg.start_addr;
121         } else {
122           if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
123             MC_snapshot_add_region(snapshot, 2, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
124             nb_reg++;
125           }
126         }
127       }
128     }else if ((reg.prot & PROT_READ)){
129       if (maps->regions[i].pathname != NULL){
130         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
131           start_text_libsimgrid = reg.start_addr;
132           libsimgrid_path = strdup(maps->regions[i].pathname);
133         }
134       }
135     }
136     i++;
137   }
138
139   snapshot->stacks = take_snapshot_stacks(heap);
140   
141   free_memory_map(maps);
142
143 }
144
145 void MC_restore_snapshot(mc_snapshot_t snapshot)
146 {
147   unsigned int i;
148   for(i=0; i < snapshot->num_reg; i++){
149     MC_region_restore(snapshot->regions[i]);
150   }
151
152 }
153
154 void MC_free_snapshot(mc_snapshot_t snapshot)
155 {
156   unsigned int i;
157   for(i=0; i < snapshot->num_reg; i++)
158     MC_region_destroy(snapshot->regions[i]);
159
160   xbt_free(snapshot);
161 }
162
163
164 void get_plt_section(){
165
166   FILE *fp;
167   char *line = NULL;            /* Temporal storage for each line that is readed */
168   ssize_t read;                 /* Number of bytes readed */
169   size_t n = 0;                 /* Amount of bytes to read by getline */
170
171   char *lfields[7];
172   int i, plt_not_found = 1;
173   unsigned long int size, offset;
174
175   char *command = bprintf( "objdump --section-headers %s", libsimgrid_path);
176
177   fp = popen(command, "r");
178
179   if(fp == NULL)
180     perror("popen failed");
181
182   while ((read = getline(&line, &n, fp)) != -1 && plt_not_found == 1) {
183
184     if(n == 0)
185       continue;
186
187     /* Wipeout the new line character */
188     line[read - 1] = '\0';
189
190     lfields[0] = strtok(line, " ");
191
192     if(lfields[0] == NULL)
193       continue;
194
195     if(strcmp(lfields[0], "Sections:") == 0 || strcmp(lfields[0], "Idx") == 0 || strcmp(lfields[0], "libsimgrid.so:") == 0)
196       continue;
197
198     for (i = 1; i < 7 && lfields[i - 1] != NULL; i++) {
199       lfields[i] = strtok(NULL, " ");
200     }
201
202     if(i>=5){
203       if(strcmp(lfields[1], ".plt") == 0){
204         size = strtoul(lfields[2], NULL, 16);
205         offset = strtoul(lfields[4], NULL, 16);
206         start_plt = (char *)start_text_libsimgrid + offset;
207         end_plt = (char *)start_plt + size;
208         plt_not_found = 0;
209       }
210     }
211     
212     
213   }
214
215   free(command);
216   free(line);
217   pclose(fp);
218
219 }
220