Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
211d99dd3169219422588fdff29e131b904f7172
[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 <string.h>
11 #include <link.h>
12 #include "mc_private.h"
13 #include "xbt/module.h"
14 #include <xbt/mmalloc.h>
15 #include "../smpi/private.h"
16
17 #include "xbt/mmalloc/mmprivate.h"
18
19 #include "../simix/smx_private.h"
20
21 #include <libunwind.h>
22 #include <libelf.h>
23
24 #include "mc_private.h"
25
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
27                                 "Logging specific to mc_checkpoint");
28
29 char *libsimgrid_path;
30
31 /************************************  Free functions **************************************/
32 /*****************************************************************************************/
33
34 static void MC_snapshot_stack_free(mc_snapshot_stack_t s)
35 {
36   if (s) {
37     xbt_dynar_free(&(s->local_variables));
38     xbt_dynar_free(&(s->stack_frames));
39     xbt_free(s);
40   }
41 }
42
43 static void MC_snapshot_stack_free_voidp(void *s)
44 {
45   MC_snapshot_stack_free((mc_snapshot_stack_t) * (void **) s);
46 }
47
48 static void local_variable_free(local_variable_t v)
49 {
50   xbt_free(v->name);
51   xbt_free(v);
52 }
53
54 static void local_variable_free_voidp(void *v)
55 {
56   local_variable_free((local_variable_t) * (void **) v);
57 }
58
59 static void MC_region_destroy(mc_mem_region_t reg)
60 {
61   //munmap(reg->data, reg->size);
62   xbt_free(reg->data);
63   xbt_free(reg);
64 }
65
66 void MC_free_snapshot(mc_snapshot_t snapshot)
67 {
68   unsigned int i;
69   for (i = 0; i < NB_REGIONS; i++)
70     MC_region_destroy(snapshot->regions[i]);
71
72   xbt_free(snapshot->stack_sizes);
73   xbt_dynar_free(&(snapshot->stacks));
74   xbt_dynar_free(&(snapshot->to_ignore));
75
76   if (snapshot->privatization_regions) {
77     size_t n = snapshot->nb_processes;
78     for (i = 0; i != n; ++i) {
79       MC_region_destroy(snapshot->privatization_regions[i]);
80     }
81     xbt_free(snapshot->privatization_regions);
82   }
83
84   xbt_free(snapshot);
85 }
86
87
88 /*******************************  Snapshot regions ********************************/
89 /*********************************************************************************/
90
91 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
92 {
93   mc_mem_region_t new_reg = xbt_new(s_mc_mem_region_t, 1);
94   new_reg->start_addr = start_addr;
95   new_reg->size = size;
96   //new_reg->data = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
97   //if(new_reg->data==MAP_FAILED)
98   //xbt_die("Could not mmap new memory for snapshot.");
99   new_reg->data = xbt_malloc(size);
100   memcpy(new_reg->data, start_addr, size);
101   //madvise(new_reg->data, size, MADV_MERGEABLE);
102
103   XBT_DEBUG("New region : type : %d, data : %p (real addr %p), size : %zu",
104             type, new_reg->data, start_addr, size);
105
106   return new_reg;
107 }
108
109 static void MC_region_restore(mc_mem_region_t reg)
110 {
111   /*FIXME: check if start_addr is still mapped, if it is not, then map it
112      before copying the data */
113
114   memcpy(reg->start_addr, reg->data, reg->size);
115   return;
116 }
117
118 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type,
119                                    void *start_addr, size_t size)
120 {
121   mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
122   snapshot->regions[type] = new_reg;
123   return;
124 }
125
126 static void MC_get_memory_regions(mc_snapshot_t snapshot)
127 {
128   size_t i;
129
130   void *start_heap = ((xbt_mheap_t) std_heap)->base;
131   void *end_heap = ((xbt_mheap_t) std_heap)->breakval;
132   MC_snapshot_add_region(snapshot, 0, start_heap,
133                          (char *) end_heap - (char *) start_heap);
134   snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
135
136   MC_snapshot_add_region(snapshot, 1, mc_libsimgrid_info->start_rw,
137                          mc_libsimgrid_info->end_rw -
138                          mc_libsimgrid_info->start_rw);
139   if (!smpi_privatize_global_variables) {
140     MC_snapshot_add_region(snapshot, 2, mc_binary_info->start_rw,
141                            mc_binary_info->end_rw - mc_binary_info->start_rw);
142     snapshot->privatization_regions = NULL;
143     snapshot->privatization_index = -1;
144   } else {
145     snapshot->privatization_regions =
146         xbt_new(mc_mem_region_t, SIMIX_process_count());
147     for (i = 0; i < SIMIX_process_count(); i++) {
148       snapshot->privatization_regions[i] =
149           MC_region_new(-1, mappings[i], size_data_exe);
150     }
151     snapshot->privatization_index = loaded_page;
152   }
153 }
154
155 /** @brief Finds the range of the different memory segments and binary paths */
156 void MC_init_memory_map_info()
157 {
158
159   unsigned int i = 0;
160   s_map_region_t reg;
161   memory_map_t maps = MC_get_memory_map();
162
163   maestro_stack_start = NULL;
164   maestro_stack_end = NULL;
165   libsimgrid_path = NULL;
166
167   while (i < maps->mapsize) {
168     reg = maps->regions[i];
169     if (maps->regions[i].pathname == NULL) {
170       // Nothing to do
171     } else if ((reg.prot & PROT_WRITE)
172                && !memcmp(maps->regions[i].pathname, "[stack]", 7)) {
173       maestro_stack_start = reg.start_addr;
174       maestro_stack_end = reg.end_addr;
175     } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)
176                && !memcmp(basename(maps->regions[i].pathname), "libsimgrid",
177                           10)) {
178       if (libsimgrid_path == NULL)
179         libsimgrid_path = strdup(maps->regions[i].pathname);
180     }
181     i++;
182   }
183
184   xbt_assert(maestro_stack_start, "maestro_stack_start");
185   xbt_assert(maestro_stack_end, "maestro_stack_end");
186   xbt_assert(libsimgrid_path, "libsimgrid_path&");
187
188   MC_free_memory_map(maps);
189
190 }
191
192 /** \brief Fill/lookup the "subtype" field.
193  */
194 static void MC_resolve_subtype(mc_object_info_t info, dw_type_t type)
195 {
196
197   if (type->dw_type_id == NULL)
198     return;
199   type->subtype = xbt_dict_get_or_null(info->types, type->dw_type_id);
200   if (type->subtype == NULL)
201     return;
202   if (type->subtype->byte_size != 0)
203     return;
204   if (type->subtype->name == NULL)
205     return;
206   // Try to find a more complete description of the type:
207   // We need to fix in order to support C++.
208
209   dw_type_t subtype =
210       xbt_dict_get_or_null(info->full_types_by_name, type->subtype->name);
211   if (subtype != NULL) {
212     type->subtype = subtype;
213   }
214
215 }
216
217 void MC_post_process_types(mc_object_info_t info)
218 {
219   xbt_dict_cursor_t cursor = NULL;
220   char *origin;
221   dw_type_t type;
222
223   // Lookup "subtype" field:
224   xbt_dict_foreach(info->types, cursor, origin, type) {
225     MC_resolve_subtype(info, type);
226
227     dw_type_t member;
228     unsigned int i = 0;
229     if (type->members != NULL)
230       xbt_dynar_foreach(type->members, i, member) {
231       MC_resolve_subtype(info, member);
232       }
233   }
234 }
235
236 /** \brief Fills the position of the segments (executable, read-only, read/write).
237  *
238  * TODO, use dl_iterate_phdr to be more robust
239  * */
240 void MC_find_object_address(memory_map_t maps, mc_object_info_t result)
241 {
242
243   unsigned int i = 0;
244   s_map_region_t reg;
245   const char *name = basename(result->file_name);
246   while (i < maps->mapsize) {
247     reg = maps->regions[i];
248     if (maps->regions[i].pathname == NULL
249         || strcmp(basename(maps->regions[i].pathname), name)) {
250       // Nothing to do
251     } else if ((reg.prot & PROT_WRITE)) {
252       xbt_assert(!result->start_rw,
253                  "Multiple read-write segments for %s, not supported",
254                  maps->regions[i].pathname);
255       result->start_rw = reg.start_addr;
256       result->end_rw = reg.end_addr;
257       // .bss is usually after the .data:
258       s_map_region_t *next = &(maps->regions[i + 1]);
259       if (next->pathname == NULL && (next->prot & PROT_WRITE)
260           && next->start_addr == reg.end_addr) {
261         result->end_rw = maps->regions[i + 1].end_addr;
262       }
263     } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)) {
264       xbt_assert(!result->start_exec,
265                  "Multiple executable segments for %s, not supported",
266                  maps->regions[i].pathname);
267       result->start_exec = reg.start_addr;
268       result->end_exec = reg.end_addr;
269     } else if ((reg.prot & PROT_READ) && !(reg.prot & PROT_EXEC)) {
270       xbt_assert(!result->start_ro,
271                  "Multiple read only segments for %s, not supported",
272                  maps->regions[i].pathname);
273       result->start_ro = reg.start_addr;
274       result->end_ro = reg.end_addr;
275     }
276     i++;
277   }
278
279   xbt_assert(result->file_name);
280   xbt_assert(result->start_rw);
281   xbt_assert(result->start_exec);
282 }
283
284 /************************************* Take Snapshot ************************************/
285 /****************************************************************************************/
286
287 /** \brief Checks whether the variable is in scope for a given IP.
288  *
289  *  A variable may be defined only from a given value of IP.
290  *
291  *  \param var   Variable description
292  *  \param frame Scope description
293  *  \param ip    Instruction pointer
294  *  \return      true if the variable is valid
295  * */
296 static bool mc_valid_variable(dw_variable_t var, dw_frame_t scope,
297                               const void *ip)
298 {
299   // The variable is not yet valid:
300   if ((const void *) ((const char *) scope->low_pc + var->start_scope) > ip)
301     return false;
302   else
303     return true;
304 }
305
306 static void mc_fill_local_variables_values(mc_stack_frame_t stack_frame,
307                                            dw_frame_t scope, xbt_dynar_t result)
308 {
309   void *ip = (void *) stack_frame->ip;
310   if (ip < scope->low_pc || ip >= scope->high_pc)
311     return;
312
313   unsigned cursor = 0;
314   dw_variable_t current_variable;
315   xbt_dynar_foreach(scope->variables, cursor, current_variable) {
316
317     if (!mc_valid_variable(current_variable, scope, (void *) stack_frame->ip))
318       continue;
319
320     int region_type;
321     if ((long) stack_frame->ip > (long) mc_libsimgrid_info->start_exec)
322       region_type = 1;
323     else
324       region_type = 2;
325
326     local_variable_t new_var = xbt_new0(s_local_variable_t, 1);
327     new_var->subprogram = stack_frame->frame;
328     new_var->ip = stack_frame->ip;
329     new_var->name = xbt_strdup(current_variable->name);
330     new_var->type = current_variable->type;
331     new_var->region = region_type;
332
333     if (current_variable->address != NULL) {
334       new_var->address = current_variable->address;
335     } else if (current_variable->locations.size != 0) {
336       new_var->address =
337           (void *) mc_dwarf_resolve_locations(&current_variable->locations,
338                                               current_variable->object_info,
339                                               &(stack_frame->unw_cursor),
340                                               (void *) stack_frame->frame_base,
341                                               NULL);
342     } else {
343       xbt_die("No address");
344     }
345
346     xbt_dynar_push(result, &new_var);
347   }
348
349   // Recursive processing of nested scopes:
350   dw_frame_t nested_scope = NULL;
351   xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
352     mc_fill_local_variables_values(stack_frame, nested_scope, result);
353   }
354 }
355
356 static xbt_dynar_t MC_get_local_variables_values(xbt_dynar_t stack_frames)
357 {
358
359   unsigned cursor1 = 0;
360   mc_stack_frame_t stack_frame;
361   xbt_dynar_t variables =
362       xbt_dynar_new(sizeof(local_variable_t), local_variable_free_voidp);
363
364   xbt_dynar_foreach(stack_frames, cursor1, stack_frame) {
365     mc_fill_local_variables_values(stack_frame, stack_frame->frame, variables);
366   }
367
368   return variables;
369 }
370
371 static void MC_stack_frame_free_voipd(void *s)
372 {
373   mc_stack_frame_t stack_frame = *(mc_stack_frame_t *) s;
374   if (stack_frame) {
375     xbt_free(stack_frame->frame_name);
376     xbt_free(stack_frame);
377   }
378 }
379
380 static xbt_dynar_t MC_unwind_stack_frames(void *stack_context)
381 {
382   xbt_dynar_t result =
383       xbt_dynar_new(sizeof(mc_stack_frame_t), MC_stack_frame_free_voipd);
384
385   unw_cursor_t c;
386
387   // TODO, check condition check (unw_init_local==0 means end of frame)
388   if (unw_init_local(&c, (unw_context_t *) stack_context) != 0) {
389
390     xbt_die("Could not initialize stack unwinding");
391
392   } else
393     while (1) {
394
395       mc_stack_frame_t stack_frame = xbt_new(s_mc_stack_frame_t, 1);
396       xbt_dynar_push(result, &stack_frame);
397
398       stack_frame->unw_cursor = c;
399
400       unw_word_t ip, sp;
401
402       unw_get_reg(&c, UNW_REG_IP, &ip);
403       unw_get_reg(&c, UNW_REG_SP, &sp);
404
405       stack_frame->ip = ip;
406       stack_frame->sp = sp;
407
408       // TODO, use real addresses in frame_t instead of fixing it here
409
410       dw_frame_t frame = MC_find_function_by_ip((void *) ip);
411       stack_frame->frame = frame;
412
413       if (frame) {
414         stack_frame->frame_name = xbt_strdup(frame->name);
415         stack_frame->frame_base =
416             (unw_word_t) mc_find_frame_base(frame, frame->object_info, &c);
417       } else {
418         stack_frame->frame_base = 0;
419         stack_frame->frame_name = NULL;
420       }
421
422       /* Stop before context switch with maestro */
423       if (frame != NULL && frame->name != NULL
424           && !strcmp(frame->name, "smx_ctx_sysv_wrapper"))
425         break;
426
427       int ret = ret = unw_step(&c);
428       if (ret == 0) {
429         xbt_die("Unexpected end of stack.");
430       } else if (ret < 0) {
431         xbt_die("Error while unwinding stack.");
432       }
433     }
434
435   if (xbt_dynar_length(result) == 0) {
436     XBT_INFO("unw_init_local failed");
437     xbt_abort();
438   }
439
440   return result;
441 };
442
443 static xbt_dynar_t MC_take_snapshot_stacks(mc_snapshot_t * snapshot, void *heap)
444 {
445
446   xbt_dynar_t res =
447       xbt_dynar_new(sizeof(s_mc_snapshot_stack_t),
448                     MC_snapshot_stack_free_voidp);
449
450   unsigned int cursor = 0;
451   stack_region_t current_stack;
452
453   xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
454     mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
455     st->stack_frames = MC_unwind_stack_frames(current_stack->context);
456     st->local_variables = MC_get_local_variables_values(st->stack_frames);
457
458     unw_word_t sp = xbt_dynar_get_as(st->stack_frames, 0, mc_stack_frame_t)->sp;
459     st->stack_pointer =
460         ((char *) heap + (size_t) (((char *) ((long) sp) - (char *) std_heap)));
461
462     st->real_address = current_stack->address;
463     xbt_dynar_push(res, &st);
464     (*snapshot)->stack_sizes =
465         xbt_realloc((*snapshot)->stack_sizes, (cursor + 1) * sizeof(size_t));
466     (*snapshot)->stack_sizes[cursor] =
467         current_stack->size - ((char *) st->stack_pointer -
468                                (char *) ((char *) heap +
469                                          ((char *) current_stack->address -
470                                           (char *) std_heap)));
471   }
472
473   return res;
474
475 }
476
477 static xbt_dynar_t MC_take_snapshot_ignore()
478 {
479
480   if (mc_heap_comparison_ignore == NULL)
481     return NULL;
482
483   xbt_dynar_t cpy =
484       xbt_dynar_new(sizeof(mc_heap_ignore_region_t),
485                     heap_ignore_region_free_voidp);
486
487   unsigned int cursor = 0;
488   mc_heap_ignore_region_t current_region;
489
490   xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, current_region) {
491     mc_heap_ignore_region_t new_region = NULL;
492     new_region = xbt_new0(s_mc_heap_ignore_region_t, 1);
493     new_region->address = current_region->address;
494     new_region->size = current_region->size;
495     new_region->block = current_region->block;
496     new_region->fragment = current_region->fragment;
497     xbt_dynar_push(cpy, &new_region);
498   }
499
500   return cpy;
501
502 }
503
504 static void MC_dump_checkpoint_ignore(mc_snapshot_t snapshot)
505 {
506
507   unsigned int cursor = 0;
508   mc_checkpoint_ignore_region_t region;
509   size_t offset;
510
511   xbt_dynar_foreach(mc_checkpoint_ignore, cursor, region) {
512     if (region->addr > snapshot->regions[0]->start_addr
513         && (char *) (region->addr) <
514         (char *) snapshot->regions[0]->start_addr + STD_HEAP_SIZE) {
515       offset =
516           (char *) region->addr - (char *) snapshot->regions[0]->start_addr;
517       memset((char *) snapshot->regions[0]->data + offset, 0, region->size);
518     } else if (region->addr > snapshot->regions[2]->start_addr
519                && (char *) (region->addr) <
520                (char *) snapshot->regions[2]->start_addr +
521                snapshot->regions[2]->size) {
522       offset =
523           (char *) region->addr - (char *) snapshot->regions[2]->start_addr;
524       memset((char *) snapshot->regions[2]->data + offset, 0, region->size);
525     } else if (region->addr > snapshot->regions[1]->start_addr
526                && (char *) (region->addr) <
527                (char *) snapshot->regions[1]->start_addr +
528                snapshot->regions[1]->size) {
529       offset =
530           (char *) region->addr - (char *) snapshot->regions[1]->start_addr;
531       memset((char *) snapshot->regions[1]->data + offset, 0, region->size);
532     }
533   }
534
535 }
536
537 mc_snapshot_t MC_take_snapshot(int num_state)
538 {
539
540   mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
541   snapshot->nb_processes = xbt_swag_size(simix_global->process_list);
542
543   /* Save the std heap and the writable mapped pages of libsimgrid and binary */
544   MC_get_memory_regions(snapshot);
545
546   snapshot->to_ignore = MC_take_snapshot_ignore();
547
548   if (_sg_mc_visited > 0 || strcmp(_sg_mc_property_file, "")) {
549     snapshot->stacks =
550         MC_take_snapshot_stacks(&snapshot, snapshot->regions[0]->data);
551     if (_sg_mc_hash && snapshot->stacks != NULL) {
552       snapshot->hash = mc_hash_processes_state(num_state, snapshot->stacks);
553     } else {
554       snapshot->hash = 0;
555     }
556   } else {
557     snapshot->hash = 0;
558   }
559
560   if (num_state > 0)
561     MC_dump_checkpoint_ignore(snapshot);
562
563   // mprotect the region after zero-ing ignored parts:
564   /*size_t i;
565      for(i=0; i!=NB_REGIONS; ++i) {
566      mc_mem_region_t region = snapshot->regions[i];
567      mprotect(region->data, region->size, PROT_READ);
568      } */
569
570   return snapshot;
571
572 }
573
574 void MC_restore_snapshot(mc_snapshot_t snapshot)
575 {
576   unsigned int i;
577   for (i = 0; i < NB_REGIONS; i++) {
578     // For privatized, variables we decided it was not necessary to take the snapshot:
579     if (snapshot->regions[i])
580       MC_region_restore(snapshot->regions[i]);
581   }
582
583   if (snapshot->privatization_regions) {
584     for (i = 0; i < SIMIX_process_count(); i++) {
585       if (snapshot->privatization_regions[i]) {
586         MC_region_restore(snapshot->privatization_regions[i]);
587       }
588     }
589     switch_data_segment(snapshot->privatization_index);
590   }
591 }
592
593 void *mc_translate_address(uintptr_t addr, mc_snapshot_t snapshot)
594 {
595
596   // If not in a process state/clone:
597   if (!snapshot) {
598     return (uintptr_t *) addr;
599   }
600   // If it is in a snapshot:
601   for (size_t i = 0; i != NB_REGIONS; ++i) {
602     mc_mem_region_t region = snapshot->regions[i];
603     uintptr_t start = (uintptr_t) region->start_addr;
604     uintptr_t end = start + region->size;
605
606     // The address is in this region:
607     if (addr >= start && addr < end) {
608       uintptr_t offset = addr - start;
609       return (void *) ((uintptr_t) region->data + offset);
610     }
611
612   }
613
614   // It is not in a snapshot:
615   return (void *) addr;
616 }
617
618 uintptr_t mc_untranslate_address(void *addr, mc_snapshot_t snapshot)
619 {
620   if (!snapshot) {
621     return (uintptr_t) addr;
622   }
623
624   for (size_t i = 0; i != NB_REGIONS; ++i) {
625     mc_mem_region_t region = snapshot->regions[i];
626     if (addr >= region->data
627         && addr <= (void *) (((char *) region->data) + region->size)) {
628       size_t offset = (size_t) ((char *) addr - (char *) region->data);
629       return ((uintptr_t) region->start_addr) + offset;
630     }
631   }
632
633   return (uintptr_t) addr;
634 }
635
636 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall)
637 {
638   return MC_take_snapshot(1);
639 }
640
641 void *MC_snapshot(void)
642 {
643   return simcall_mc_snapshot();
644 }