Logo AND Algorithmique Numérique Distribuée

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