Logo AND Algorithmique Numérique Distribuée

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