Logo AND Algorithmique Numérique Distribuée

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