Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : save and restore file descriptors
[simgrid.git] / src / mc / mc_checkpoint.c
1 /* Copyright (c) 2008-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #define _GNU_SOURCE
8 #define UNW_LOCAL_ONLY
9
10
11 #include <string.h>
12 #include <link.h>
13 #include <dirent.h>
14
15 #include "internal_config.h"
16 #include "mc_private.h"
17 #include "xbt/module.h"
18 #include <xbt/mmalloc.h>
19 #include "../smpi/private.h"
20 #include <alloca.h>
21
22 #include "xbt/mmalloc/mmprivate.h"
23
24 #include "../simix/smx_private.h"
25
26 #include <libunwind.h>
27 #include <libelf.h>
28
29 #include "mc_private.h"
30 #include <mc/mc.h>
31
32 #include "mc_mmu.h"
33
34 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
35                                 "Logging specific to mc_checkpoint");
36
37 char *libsimgrid_path;
38
39 /************************************  Free functions **************************************/
40 /*****************************************************************************************/
41
42 static void MC_snapshot_stack_free(mc_snapshot_stack_t s)
43 {
44   if (s) {
45     xbt_dynar_free(&(s->local_variables));
46     xbt_dynar_free(&(s->stack_frames));
47     xbt_free(s);
48   }
49 }
50
51 static void MC_snapshot_stack_free_voidp(void *s)
52 {
53   MC_snapshot_stack_free((mc_snapshot_stack_t) * (void **) s);
54 }
55
56 static void local_variable_free(local_variable_t v)
57 {
58   xbt_free(v->name);
59   xbt_free(v);
60 }
61
62 static void local_variable_free_voidp(void *v)
63 {
64   local_variable_free((local_variable_t) * (void **) v);
65 }
66
67 void MC_region_destroy(mc_mem_region_t reg)
68 {
69   if (!reg)
70     return;
71
72   //munmap(reg->data, reg->size);
73   xbt_free(reg->data);
74   if (reg->page_numbers) {
75     mc_free_page_snapshot_region(reg->page_numbers, mc_page_count(reg->size));
76   }
77   xbt_free(reg);
78 }
79
80 void MC_free_snapshot(mc_snapshot_t snapshot)
81 {
82   unsigned int i;
83   for (i = 0; i < NB_REGIONS; i++) {
84     MC_region_destroy(snapshot->regions[i]);
85   }
86
87   xbt_free(snapshot->stack_sizes);
88   xbt_dynar_free(&(snapshot->stacks));
89   xbt_dynar_free(&(snapshot->to_ignore));
90   xbt_dynar_free(&snapshot->ignored_data);
91
92   if (snapshot->privatization_regions) {
93     size_t n = xbt_dynar_length(snapshot->enabled_processes);
94     for (i = 0; i != n; ++i) {
95       MC_region_destroy(snapshot->privatization_regions[i]);
96     }
97     xbt_free(snapshot->privatization_regions);
98   }
99
100   xbt_free(snapshot);
101 }
102
103 /*******************************  Snapshot regions ********************************/
104 /*********************************************************************************/
105
106 static mc_mem_region_t mc_region_new_dense(int type, void *start_addr, void* permanent_addr, size_t size, mc_mem_region_t ref_reg)
107 {
108   mc_mem_region_t new_reg = xbt_new(s_mc_mem_region_t, 1);
109   new_reg->start_addr = start_addr;
110   new_reg->permanent_addr = permanent_addr;
111   new_reg->data = NULL;
112   new_reg->size = size;
113   new_reg->page_numbers = NULL;
114   new_reg->data = xbt_malloc(size);
115   memcpy(new_reg->data, permanent_addr, size);
116   XBT_DEBUG("New region : type : %d, data : %p (real addr %p), size : %zu",
117             type, new_reg->data, permanent_addr, size);
118   return new_reg;
119
120 }
121
122 /** @brief Take a snapshot of a given region
123  *
124  * @param type
125  * @param start_addr   Address of the region in the simulated process
126  * @param permanent_addr Permanent address of this data (for privatized variables, this is the virtual address of the privatized mapping)
127  * @param size         Size of the data*
128  * @param ref_reg      Reference corresponding region
129  */
130 static mc_mem_region_t MC_region_new(int type, void *start_addr, void* permanent_addr, size_t size, mc_mem_region_t ref_reg)
131 {
132   if (_sg_mc_sparse_checkpoint) {
133     return mc_region_new_sparse(type, start_addr, permanent_addr, size, ref_reg);
134   } else  {
135     return mc_region_new_dense(type, start_addr, permanent_addr, size, ref_reg);
136   }
137 }
138
139 /** @brief Restore a region from a snapshot
140  *
141  *  If we are using per page snapshots, it is possible to use the reference
142  *  region in order to do an incremental restoration of the region: the
143  *  softclean pages which are shared between the two snapshots do not need
144  *  to be restored.
145  *
146  *  @param reg     Target region
147  *  @param reg_reg Current region (if not NULL), used for lazy per page restoration
148  */
149 static void MC_region_restore(mc_mem_region_t reg, mc_mem_region_t ref_reg)
150 {
151   /*FIXME: check if start_addr is still mapped, if it is not, then map it
152     before copying the data */
153   if (!reg->page_numbers) {
154     memcpy(reg->permanent_addr, reg->data, reg->size);
155   } else {
156     mc_region_restore_sparse(reg, ref_reg);
157   }
158   return;
159 }
160
161 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type,
162                                    void *start_addr, void* permanent_addr, size_t size)
163
164 {
165   mc_mem_region_t ref_reg =
166     mc_model_checker->parent_snapshot ? mc_model_checker->parent_snapshot->regions[type] : NULL;
167   mc_mem_region_t new_reg = MC_region_new(type, start_addr, permanent_addr, size, ref_reg);
168   snapshot->regions[type] = new_reg;
169   return;
170 }
171
172 static void MC_get_memory_regions(mc_snapshot_t snapshot)
173 {
174
175   void *start_heap = std_heap->base;
176   void *end_heap = std_heap->breakval;
177   MC_snapshot_add_region(snapshot, 0, start_heap, start_heap,
178                          (char *) end_heap - (char *) start_heap);
179   snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
180   snapshot->privatization_regions = NULL;
181
182   MC_snapshot_add_region(snapshot, 1,
183       mc_libsimgrid_info->start_rw, mc_libsimgrid_info->start_rw,
184       mc_libsimgrid_info->end_rw - mc_libsimgrid_info->start_rw);
185
186 #ifdef HAVE_SMPI
187   size_t i;
188   if (smpi_privatize_global_variables && smpi_process_count()) {
189     // Snapshot the global variable of the application separately for each
190     // simulated process:
191     snapshot->privatization_regions =
192       xbt_new(mc_mem_region_t, smpi_process_count());
193     for (i = 0; i < smpi_process_count(); i++) {
194       mc_mem_region_t ref_reg =
195         mc_model_checker->parent_snapshot ? mc_model_checker->parent_snapshot->privatization_regions[i] : NULL;
196       snapshot->privatization_regions[i] =
197         MC_region_new(-1, mc_binary_info->start_rw, smpi_privatisation_regions[i].address, size_data_exe, ref_reg);
198     }
199     snapshot->privatization_index = smpi_loaded_page;
200     snapshot->regions[2] = NULL;
201   } else
202 #endif
203   {
204     MC_snapshot_add_region(snapshot, 2,
205                            mc_binary_info->start_rw, mc_binary_info->start_rw,
206                            mc_binary_info->end_rw - mc_binary_info->start_rw);
207     snapshot->privatization_regions = NULL;
208     snapshot->privatization_index = -1;
209   }
210 }
211
212 /** @brief Finds the range of the different memory segments and binary paths */
213 void MC_init_memory_map_info()
214 {
215
216   unsigned int i = 0;
217   s_map_region_t reg;
218   memory_map_t maps = MC_get_memory_map();
219
220   maestro_stack_start = NULL;
221   maestro_stack_end = NULL;
222   libsimgrid_path = NULL;
223
224   while (i < maps->mapsize) {
225     reg = maps->regions[i];
226     if (maps->regions[i].pathname == NULL) {
227       // Nothing to do
228     } else if ((reg.prot & PROT_WRITE)
229                && !memcmp(maps->regions[i].pathname, "[stack]", 7)) {
230       maestro_stack_start = reg.start_addr;
231       maestro_stack_end = reg.end_addr;
232     } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)
233                && !memcmp(basename(maps->regions[i].pathname), "libsimgrid",
234                           10)) {
235       if (libsimgrid_path == NULL)
236         libsimgrid_path = strdup(maps->regions[i].pathname);
237     }
238     i++;
239   }
240
241   xbt_assert(maestro_stack_start, "maestro_stack_start");
242   xbt_assert(maestro_stack_end, "maestro_stack_end");
243   xbt_assert(libsimgrid_path, "libsimgrid_path&");
244
245   MC_free_memory_map(maps);
246
247 }
248
249 /** \brief Fills the position of the segments (executable, read-only, read/write).
250  *
251  * TODO, use dl_iterate_phdr to be more robust
252  * */
253 void MC_find_object_address(memory_map_t maps, mc_object_info_t result)
254 {
255
256   unsigned int i = 0;
257   s_map_region_t reg;
258   const char *name = basename(result->file_name);
259   while (i < maps->mapsize) {
260     reg = maps->regions[i];
261     if (maps->regions[i].pathname == NULL
262         || strcmp(basename(maps->regions[i].pathname), name)) {
263       // Nothing to do
264     } else if ((reg.prot & PROT_WRITE)) {
265       xbt_assert(!result->start_rw,
266                  "Multiple read-write segments for %s, not supported",
267                  maps->regions[i].pathname);
268       result->start_rw = reg.start_addr;
269       result->end_rw = reg.end_addr;
270       // .bss is usually after the .data:
271       s_map_region_t *next = &(maps->regions[i + 1]);
272       if (next->pathname == NULL && (next->prot & PROT_WRITE)
273           && next->start_addr == reg.end_addr) {
274         result->end_rw = maps->regions[i + 1].end_addr;
275       }
276     } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)) {
277       xbt_assert(!result->start_exec,
278                  "Multiple executable segments for %s, not supported",
279                  maps->regions[i].pathname);
280       result->start_exec = reg.start_addr;
281       result->end_exec = reg.end_addr;
282     } else if ((reg.prot & PROT_READ) && !(reg.prot & PROT_EXEC)) {
283       xbt_assert(!result->start_ro,
284                  "Multiple read only segments for %s, not supported",
285                  maps->regions[i].pathname);
286       result->start_ro = reg.start_addr;
287       result->end_ro = reg.end_addr;
288     }
289     i++;
290   }
291
292   xbt_assert(result->file_name);
293   xbt_assert(result->start_rw);
294   xbt_assert(result->start_exec);
295 }
296
297 /************************************* Take Snapshot ************************************/
298 /****************************************************************************************/
299
300 /** \brief Checks whether the variable is in scope for a given IP.
301  *
302  *  A variable may be defined only from a given value of IP.
303  *
304  *  \param var   Variable description
305  *  \param frame Scope description
306  *  \param ip    Instruction pointer
307  *  \return      true if the variable is valid
308  * */
309 static bool mc_valid_variable(dw_variable_t var, dw_frame_t scope,
310                               const void *ip)
311 {
312   // The variable is not yet valid:
313   if ((const void *) ((const char *) scope->low_pc + var->start_scope) > ip)
314     return false;
315   else
316     return true;
317 }
318
319 static void mc_fill_local_variables_values(mc_stack_frame_t stack_frame,
320                                            dw_frame_t scope, int process_index, xbt_dynar_t result)
321 {
322   void *ip = (void *) stack_frame->ip;
323   if (ip < scope->low_pc || ip >= scope->high_pc)
324     return;
325
326   unsigned cursor = 0;
327   dw_variable_t current_variable;
328   xbt_dynar_foreach(scope->variables, cursor, current_variable) {
329
330     if (!mc_valid_variable(current_variable, scope, (void *) stack_frame->ip))
331       continue;
332
333     int region_type;
334     if ((long) stack_frame->ip > (long) mc_libsimgrid_info->start_exec)
335       region_type = 1;
336     else
337       region_type = 2;
338
339     local_variable_t new_var = xbt_new0(s_local_variable_t, 1);
340     new_var->subprogram = stack_frame->frame;
341     new_var->ip = stack_frame->ip;
342     new_var->name = xbt_strdup(current_variable->name);
343     new_var->type = current_variable->type;
344     new_var->region = region_type;
345
346     if (current_variable->address != NULL) {
347       new_var->address = current_variable->address;
348     } else if (current_variable->locations.size != 0) {
349       s_mc_location_t location;
350       mc_dwarf_resolve_locations(&location, &current_variable->locations,
351                                               current_variable->object_info,
352                                               &(stack_frame->unw_cursor),
353                                               (void *) stack_frame->frame_base,
354                                               NULL, process_index);
355
356       switch(mc_get_location_type(&location)) {
357       case MC_LOCATION_TYPE_ADDRESS:
358         new_var->address = location.memory_location;
359         break;
360       case MC_LOCATION_TYPE_REGISTER:
361       default:
362         xbt_die("Cannot handle non-address variable");
363       }
364
365     } else {
366       xbt_die("No address");
367     }
368
369     xbt_dynar_push(result, &new_var);
370   }
371
372   // Recursive processing of nested scopes:
373   dw_frame_t nested_scope = NULL;
374   xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
375     mc_fill_local_variables_values(stack_frame, nested_scope, process_index, result);
376   }
377 }
378
379 static xbt_dynar_t MC_get_local_variables_values(xbt_dynar_t stack_frames, int process_index)
380 {
381
382   unsigned cursor1 = 0;
383   mc_stack_frame_t stack_frame;
384   xbt_dynar_t variables =
385       xbt_dynar_new(sizeof(local_variable_t), local_variable_free_voidp);
386
387   xbt_dynar_foreach(stack_frames, cursor1, stack_frame) {
388     mc_fill_local_variables_values(stack_frame, stack_frame->frame, process_index, variables);
389   }
390
391   return variables;
392 }
393
394 static void MC_stack_frame_free_voipd(void *s)
395 {
396   mc_stack_frame_t stack_frame = *(mc_stack_frame_t *) s;
397   if (stack_frame) {
398     xbt_free(stack_frame->frame_name);
399     xbt_free(stack_frame);
400   }
401 }
402
403 static xbt_dynar_t MC_unwind_stack_frames(void *stack_context)
404 {
405   xbt_dynar_t result =
406       xbt_dynar_new(sizeof(mc_stack_frame_t), MC_stack_frame_free_voipd);
407
408   unw_cursor_t c;
409
410   // TODO, check condition check (unw_init_local==0 means end of frame)
411   if (unw_init_local(&c, (unw_context_t *) stack_context) != 0) {
412
413     xbt_die("Could not initialize stack unwinding");
414
415   } else
416     while (1) {
417
418       mc_stack_frame_t stack_frame = xbt_new(s_mc_stack_frame_t, 1);
419       xbt_dynar_push(result, &stack_frame);
420
421       stack_frame->unw_cursor = c;
422
423       unw_word_t ip, sp;
424
425       unw_get_reg(&c, UNW_REG_IP, &ip);
426       unw_get_reg(&c, UNW_REG_SP, &sp);
427
428       stack_frame->ip = ip;
429       stack_frame->sp = sp;
430
431       // TODO, use real addresses in frame_t instead of fixing it here
432
433       dw_frame_t frame = MC_find_function_by_ip((void *) ip);
434       stack_frame->frame = frame;
435
436       if (frame) {
437         stack_frame->frame_name = xbt_strdup(frame->name);
438         stack_frame->frame_base =
439             (unw_word_t) mc_find_frame_base(frame, frame->object_info, &c);
440       } else {
441         stack_frame->frame_base = 0;
442         stack_frame->frame_name = NULL;
443       }
444
445       /* Stop before context switch with maestro */
446       if (frame != NULL && frame->name != NULL
447           && !strcmp(frame->name, "smx_ctx_sysv_wrapper"))
448         break;
449
450       int ret = ret = unw_step(&c);
451       if (ret == 0) {
452         xbt_die("Unexpected end of stack.");
453       } else if (ret < 0) {
454         xbt_die("Error while unwinding stack.");
455       }
456     }
457
458   if (xbt_dynar_length(result) == 0) {
459     XBT_INFO("unw_init_local failed");
460     xbt_abort();
461   }
462
463   return result;
464 };
465
466 static xbt_dynar_t MC_take_snapshot_stacks(mc_snapshot_t * snapshot)
467 {
468
469   xbt_dynar_t res =
470       xbt_dynar_new(sizeof(s_mc_snapshot_stack_t),
471                     MC_snapshot_stack_free_voidp);
472
473   unsigned int cursor = 0;
474   stack_region_t current_stack;
475
476   xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
477     mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
478     st->stack_frames = MC_unwind_stack_frames(current_stack->context);
479     st->local_variables = MC_get_local_variables_values(st->stack_frames, current_stack->process_index);
480     st->process_index = current_stack->process_index;
481
482     unw_word_t sp = xbt_dynar_get_as(st->stack_frames, 0, mc_stack_frame_t)->sp;
483
484     xbt_dynar_push(res, &st);
485     (*snapshot)->stack_sizes =
486         xbt_realloc((*snapshot)->stack_sizes, (cursor + 1) * sizeof(size_t));
487     (*snapshot)->stack_sizes[cursor] =
488       (char*) current_stack->address + current_stack->size - (char*) sp;
489   }
490
491   return res;
492
493 }
494
495 static xbt_dynar_t MC_take_snapshot_ignore()
496 {
497
498   if (mc_heap_comparison_ignore == NULL)
499     return NULL;
500
501   xbt_dynar_t cpy =
502       xbt_dynar_new(sizeof(mc_heap_ignore_region_t),
503                     heap_ignore_region_free_voidp);
504
505   unsigned int cursor = 0;
506   mc_heap_ignore_region_t current_region;
507
508   xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, current_region) {
509     mc_heap_ignore_region_t new_region = NULL;
510     new_region = xbt_new0(s_mc_heap_ignore_region_t, 1);
511     new_region->address = current_region->address;
512     new_region->size = current_region->size;
513     new_region->block = current_region->block;
514     new_region->fragment = current_region->fragment;
515     xbt_dynar_push(cpy, &new_region);
516   }
517
518   return cpy;
519
520 }
521
522 static void mc_free_snapshot_ignored_data_pvoid(void* data) {
523   mc_snapshot_ignored_data_t ignored_data = (mc_snapshot_ignored_data_t) data;
524   free(ignored_data->data);
525 }
526
527 static void MC_snapshot_handle_ignore(mc_snapshot_t snapshot)
528 {
529   snapshot->ignored_data = xbt_dynar_new(sizeof(s_mc_snapshot_ignored_data_t), mc_free_snapshot_ignored_data_pvoid);
530
531   // Copy the memory:
532   unsigned int cursor = 0;
533   mc_checkpoint_ignore_region_t region;
534   xbt_dynar_foreach (mc_checkpoint_ignore, cursor, region) {
535     s_mc_snapshot_ignored_data_t ignored_data;
536     ignored_data.start = region->addr;
537     ignored_data.size = region->size;
538     ignored_data.data = malloc(region->size);
539     memcpy(ignored_data.data, region->addr, region->size);
540     xbt_dynar_push(snapshot->ignored_data, &ignored_data);
541   }
542
543   // Zero the memory:
544   xbt_dynar_foreach (mc_checkpoint_ignore, cursor, region) {
545     memset(region->addr, 0, region->size);
546   }
547
548 }
549
550 static void MC_snapshot_ignore_restore(mc_snapshot_t snapshot)
551 {
552   unsigned int cursor = 0;
553   s_mc_snapshot_ignored_data_t ignored_data;
554   xbt_dynar_foreach (snapshot->ignored_data, cursor, ignored_data) {
555     memcpy(ignored_data.start, ignored_data.data, ignored_data.size);
556   }
557 }
558
559 /** @brief Can we remove this snapshot?
560  *
561  * Some snapshots cannot be removed (yet) because we need them
562  * at this point.
563  *
564  * @param snapshot
565  */
566 int mc_important_snapshot(mc_snapshot_t snapshot)
567 {
568   // We need this snapshot in order to know which
569   // pages needs to be stored in the next snapshot.
570   // This field is only non-NULL when using soft-dirty
571   // page tracking.
572   if (snapshot == mc_model_checker->parent_snapshot)
573     return true;
574
575   return false;
576 }
577
578 static void MC_get_current_fd(mc_snapshot_t snapshot){
579   
580  struct dirent *fd_number;
581  DIR *fd_dir;
582  char *command;
583  size_t n;
584  char *line = NULL;
585  FILE *fp;
586  int fd_value;
587
588  snapshot->total_fd = 0;
589
590  fd_dir = opendir ("/proc/self/fd");
591  if (fd_dir == NULL) {
592    printf ("Cannot open directory '/proc/self/fd'\n");
593    return;
594  }
595
596  while ((fd_number = readdir(fd_dir)) != NULL) {
597    
598    fd_value = atoi(fd_number->d_name);
599
600    if(fd_value < 3)
601      continue;
602   
603    command = bprintf("readlink /proc/self/fd/%s", fd_number->d_name);
604    fp = popen(command, "r");
605    if(fp == NULL){
606      perror("popen failed");
607      xbt_abort();
608    }
609    xbt_getline(&line, &n, fp); 
610    if(line && strncmp(line, "pipe:", 5) != 0 && strncmp(line, "socket:", 7) != 0){
611      fd_infos_t fd = xbt_new0(s_fd_infos_t, 1);
612      fd->filename = strdup(line);
613      fd->filename[strlen(line)-1] = '\0';
614      fd->number = fd_value;
615      fd->flags = fcntl(fd_value, F_GETFD);
616      fd->current_position = lseek(fd_value, 0, SEEK_CUR);
617      snapshot->current_fd = xbt_realloc(snapshot->current_fd, (snapshot->total_fd + 1) * sizeof(fd_infos_t));
618      snapshot->current_fd[snapshot->total_fd] = fd;
619      snapshot->total_fd++;
620    }
621    
622    xbt_free(command);
623    pclose(fp);
624  }
625
626  xbt_free(line);
627  closedir (fd_dir);
628
629 }
630
631 mc_snapshot_t MC_take_snapshot(int num_state)
632 {
633
634   mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
635   snapshot->enabled_processes = xbt_dynar_new(sizeof(int), NULL);
636   smx_process_t process;
637   xbt_swag_foreach(process, simix_global->process_list) {
638     xbt_dynar_push_as(snapshot->enabled_processes, int, (int)process->pid);
639   }
640
641   MC_snapshot_handle_ignore(snapshot);
642
643   MC_get_current_fd(snapshot);
644
645   /* Save the std heap and the writable mapped pages of libsimgrid and binary */
646   MC_get_memory_regions(snapshot);
647   if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
648     mc_softdirty_reset();
649   }
650
651   snapshot->to_ignore = MC_take_snapshot_ignore();
652
653   if (_sg_mc_visited > 0 || strcmp(_sg_mc_property_file, "")) {
654     snapshot->stacks =
655         MC_take_snapshot_stacks(&snapshot);
656     if (_sg_mc_hash && snapshot->stacks != NULL) {
657       snapshot->hash = mc_hash_processes_state(num_state, snapshot->stacks);
658     } else {
659       snapshot->hash = 0;
660     }
661   } else {
662     snapshot->hash = 0;
663   }
664
665   MC_snapshot_ignore_restore(snapshot);
666   if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
667     mc_model_checker->parent_snapshot = snapshot;
668   }
669   return snapshot;
670 }
671
672 void MC_restore_snapshot(mc_snapshot_t snapshot)
673 {
674   mc_snapshot_t parent_snapshot = mc_model_checker->parent_snapshot;
675
676   int new_fd;
677   unsigned int i;
678   for (i = 0; i < NB_REGIONS; i++) {
679     // For privatized, variables we decided it was not necessary to take the snapshot:
680     if (snapshot->regions[i])
681       MC_region_restore(snapshot->regions[i],
682         parent_snapshot ? parent_snapshot->regions[i] : NULL);
683   }
684
685 #ifdef HAVE_SMPI
686   if (snapshot->privatization_regions) {
687     // Restore the global variables of the application separately for each
688     // simulated process:
689     for (i = 0; i < smpi_process_count(); i++) {
690       if (snapshot->privatization_regions[i]) {
691         MC_region_restore(snapshot->privatization_regions[i],
692           parent_snapshot ? parent_snapshot->privatization_regions[i] : NULL);
693       }
694     }
695   }
696   if(snapshot->privatization_index >= 0) {
697     // We just rewrote the global variables.
698     // The privatisation segment SMPI thinks
699     // is mapped might be inconsistent with the segment which
700     // is really mapped in memory (kernel state).
701     // We ask politely SMPI to map the segment anyway,
702     // even if it thinks it is the current one:
703     smpi_really_switch_data_segment(snapshot->privatization_index);
704   }
705 #endif
706
707   for(i=0; i < snapshot->total_fd; i++){
708     
709     new_fd = open(snapshot->current_fd[i]->filename, snapshot->current_fd[i]->flags);
710     if(new_fd != -1 && new_fd != snapshot->current_fd[i]->number){
711       dup2(new_fd, snapshot->current_fd[i]->number);
712       fprintf(stderr, "%p\n", fdopen(snapshot->current_fd[i]->number, "rw"));
713       lseek(snapshot->current_fd[i]->number, snapshot->current_fd[i]->current_position, SEEK_SET);
714     };
715   }
716
717   if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
718     mc_softdirty_reset();
719   }
720
721   MC_snapshot_ignore_restore(snapshot);
722   if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
723     mc_model_checker->parent_snapshot = snapshot;
724   }
725
726 }
727
728 mc_snapshot_t simcall_HANDLER_mc_snapshot(smx_simcall_t simcall)
729 {
730   return MC_take_snapshot(1);
731 }
732
733 void *MC_snapshot(void)
734 {
735   return simcall_mc_snapshot();
736 }