Logo AND Algorithmique Numérique Distribuée

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