Logo AND Algorithmique Numérique Distribuée

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