Logo AND Algorithmique Numérique Distribuée

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