Logo AND Algorithmique Numérique Distribuée

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