Logo AND Algorithmique Numérique Distribuée

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