Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
abd70f6fe80853a3e11670fad3ed9269836c58c5
[simgrid.git] / src / mc / mc_checkpoint.c
1 /* Copyright (c) 2008-2013. 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 "../simix/smx_private.h"
17
18 #include <libunwind.h>
19 #include <libelf.h>
20
21 #include "mc_private.h"
22
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
24                                 "Logging specific to mc_checkpoint");
25
26 char *libsimgrid_path;
27
28 static void MC_find_object_address(memory_map_t maps, mc_object_info_t result);
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->type);
49   xbt_free(v);
50 }
51
52 static void local_variable_free_voidp(void *v){
53   local_variable_free((local_variable_t) * (void **) v);
54 }
55
56 static void MC_region_destroy(mc_mem_region_t reg)
57 {
58   xbt_free(reg->data);
59   xbt_free(reg);
60 }
61
62 void MC_free_snapshot(mc_snapshot_t snapshot){
63   unsigned int i;
64   for(i=0; i < NB_REGIONS; i++)
65     MC_region_destroy(snapshot->regions[i]);
66
67   xbt_free(snapshot->stack_sizes);
68   xbt_dynar_free(&(snapshot->stacks));
69   xbt_dynar_free(&(snapshot->to_ignore));
70   xbt_free(snapshot);
71 }
72
73
74 /*******************************  Snapshot regions ********************************/
75 /*********************************************************************************/
76
77 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
78 {
79   mc_mem_region_t new_reg = xbt_new(s_mc_mem_region_t, 1);
80   new_reg->start_addr = start_addr;
81   new_reg->size = size;
82   new_reg->data = xbt_malloc(size);
83   memcpy(new_reg->data, start_addr, size);
84
85   XBT_DEBUG("New region : type : %d, data : %p (real addr %p), size : %zu", type, new_reg->data, start_addr, size);
86   
87   return new_reg;
88 }
89
90 static void MC_region_restore(mc_mem_region_t reg)
91 {
92   /*FIXME: check if start_addr is still mapped, if it is not, then map it
93     before copying the data */
94  
95   memcpy(reg->start_addr, reg->data, reg->size);
96   return;
97 }
98
99 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size)
100 {
101   mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
102   snapshot->regions[type] = new_reg;
103   return;
104
105
106 static void MC_get_memory_regions(mc_snapshot_t snapshot){
107
108   FILE *fp;
109   char *line = NULL;
110   ssize_t read;
111   size_t n = 0;
112   
113   char *lfields[6] = {0}, *tok;
114   void *start_addr, *start_addr1, *end_addr;
115   size_t size;
116   int i;
117
118   fp = fopen("/proc/self/maps", "r");
119   
120   xbt_assert(fp, 
121              "Cannot open /proc/self/maps to investigate the memory map of the process. Please report this bug.");
122
123   setbuf(fp, NULL);
124
125   while((read = xbt_getline(&line, &n, fp)) != -1){
126
127     /* Wipeout the new line character */
128     line[read - 1] = '\0';
129
130     /* Tokenize the line using spaces as delimiters and store each token */
131     lfields[0] = strtok(line, " ");
132
133     for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
134       lfields[i] = strtok(NULL, " ");
135     }
136
137     /* First get the permissions flags, need write permission */
138     if(lfields[1][1] == 'w'){
139
140       /* Get the start address of the map */
141       tok = strtok(lfields[0], "-");
142       start_addr = (void *)strtoul(tok, NULL, 16);
143     
144       if(start_addr == std_heap){     /* Std_heap ? */
145         tok = strtok(NULL, "-");
146         end_addr = (void *)strtoul(tok, NULL, 16);
147         MC_snapshot_add_region(snapshot, 0, start_addr, (char*)end_addr - (char*)start_addr);
148         snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
149       }else{ /* map name == libsimgrid || binary_name ? */
150         if(lfields[5] != NULL){
151           if(!memcmp(basename(lfields[5]), "libsimgrid", 10)){
152             tok = strtok(NULL, "-");
153             end_addr = (void *)strtoul(tok, NULL, 16);
154             size = (char*)end_addr - (char*)start_addr;
155             /* BSS and data segments may be separated according to the OS */
156             if((read = xbt_getline(&line, &n, fp)) != -1){
157               line[read - 1] = '\0';
158               lfields[0] = strtok(line, " ");
159               for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
160                 lfields[i] = strtok(NULL, " ");
161               }
162               if(lfields[1][1] == 'w' && lfields[5] == NULL){
163                 tok = strtok(lfields[0], "-");
164                 start_addr1 = (void *)strtoul(tok, NULL, 16);
165                 tok = strtok(NULL, "-");
166                 size += (char *)(void *)strtoul(tok, NULL, 16) - (char*)start_addr1;
167               }
168             }
169             MC_snapshot_add_region(snapshot, 1, start_addr, size);
170           }else if(!memcmp(basename(lfields[5]), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
171             tok = strtok(NULL, "-");
172             end_addr = (void *)strtoul(tok, NULL, 16);
173             size = (char*)end_addr - (char*)start_addr;
174              /* BSS and data segments may be separated according to the OS */
175             if((read = xbt_getline(&line, &n, fp)) != -1){
176               line[read - 1] = '\0';
177               lfields[0] = strtok(line, " ");
178               for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
179                 lfields[i] = strtok(NULL, " ");
180               }
181               tok = strtok(lfields[0], "-");
182               start_addr1 = (void *)strtoul(tok, NULL, 16);
183               if(lfields[1][1] == 'w'){
184                 if(start_addr1 == std_heap){     /* Std_heap ? */
185                   tok = strtok(NULL, "-");
186                   end_addr = (void *)strtoul(tok, NULL, 16);
187                   MC_snapshot_add_region(snapshot, 0, start_addr1, (char*)end_addr - (char*)start_addr1);
188                   snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
189                 }else if(start_addr1 != raw_heap){
190                   tok = strtok(NULL, "-");
191                   size += (char *)(void *)strtoul(tok, NULL, 16) - (char *)start_addr1;
192                 }
193               }
194             }
195             MC_snapshot_add_region(snapshot, 2, start_addr, size);
196           }else if (!memcmp(lfields[5], "[stack]", 7)){
197             maestro_stack_start = start_addr;
198             tok = strtok(NULL, "-");
199             maestro_stack_end = (void *)strtoul(tok, NULL, 16);
200           }
201         }
202       }
203     }
204     
205   }
206
207   free(line);
208   fclose(fp);
209
210 }
211
212 /** @brief Finds the range of the different memory segments and binary paths */
213 void MC_init_memory_map_info(){
214  
215   unsigned int i = 0;
216   s_map_region_t reg;
217   memory_map_t maps = MC_get_memory_map();
218
219   maestro_stack_start = NULL;
220   maestro_stack_end = NULL;
221   libsimgrid_path = NULL;
222
223   while (i < maps->mapsize) {
224     reg = maps->regions[i];
225     if (maps->regions[i].pathname == NULL) {
226       // Nothing to do
227     }
228     else if ((reg.prot & PROT_WRITE) && !memcmp(maps->regions[i].pathname, "[stack]", 7)){
229           maestro_stack_start = reg.start_addr;
230           maestro_stack_end = reg.end_addr;
231     } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC) && !memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
232       if(libsimgrid_path == NULL)
233           libsimgrid_path = strdup(maps->regions[i].pathname);
234     }
235     i++;
236   }
237
238   xbt_assert(maestro_stack_start, "maestro_stack_start");
239   xbt_assert(maestro_stack_end, "maestro_stack_end");
240   xbt_assert(libsimgrid_path, "libsimgrid_path&");
241
242   MC_free_memory_map(maps);
243
244 }
245
246 /** \brief Fill/llokup the "subtype" field.
247  */
248 static void MC_resolve_subtype(mc_object_info_t info, dw_type_t type) {
249
250   if(type->dw_type_id==NULL)
251     return;
252   type->subtype = xbt_dict_get_or_null(info->types, type->dw_type_id);
253   if(type->subtype==NULL)
254     return;
255   if(type->subtype->byte_size != 0)
256     return;
257   if(type->subtype->name==NULL)
258     return;
259   // Try to find a more complete description of the type:
260   // We need to fix in order to support C++.
261
262   dw_type_t subtype = xbt_dict_get_or_null(info->types_by_name, type->subtype->name);
263   if(subtype!=NULL) {
264     type->subtype = subtype;
265   }
266
267   // TODO, support "switch type" (looking up the type in another lib) when possible
268 }
269
270 static void MC_post_process_types(mc_object_info_t info) {
271   xbt_dict_cursor_t cursor = NULL;
272   char *origin;
273   dw_type_t type;
274
275   // Lookup "subtype" field:
276   xbt_dict_foreach(info->types, cursor, origin, type){
277     MC_resolve_subtype(info, type);
278
279     dw_type_t member;
280     unsigned int i = 0;
281     if(type->members!=NULL) xbt_dynar_foreach(type->members, i, member) {
282       MC_resolve_subtype(info, member);
283     }
284   }
285 }
286
287 /** \brief Finds informations about a given shared object/executable */
288 mc_object_info_t MC_find_object_info(memory_map_t maps, char* name) {
289   mc_object_info_t result = MC_new_object_info();
290   result->file_name = xbt_strdup(name);
291   MC_find_object_address(maps, result);
292   MC_dwarf_get_variables(result);
293   MC_post_process_types(result);
294   return result;
295 }
296
297 /** \brief Fills the position of the .bss and .data sections. */
298 static void MC_find_object_address(memory_map_t maps, mc_object_info_t result) {
299
300   unsigned int i = 0;
301   s_map_region_t reg;
302   const char* name = basename(result->file_name);
303   while (i < maps->mapsize) {
304     reg = maps->regions[i];
305     if (maps->regions[i].pathname == NULL || strcmp(basename(maps->regions[i].pathname),  name)) {
306       // Nothing to do
307     }
308     else if ((reg.prot & PROT_WRITE)){
309           xbt_assert(!result->start_rw,
310             "Multiple read-write segments for %s, not supported",
311             maps->regions[i].pathname);
312           result->start_rw = reg.start_addr;
313           result->end_rw   = reg.end_addr;
314     } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)){
315           xbt_assert(!result->start_exec,
316             "Multiple executable segments for %s, not supported",
317             maps->regions[i].pathname);
318           result->start_exec = reg.start_addr;
319           result->end_exec   = reg.end_addr;
320     }
321     else if((reg.prot & PROT_READ) && !(reg.prot & PROT_EXEC)) {
322         xbt_assert(!result->start_ro,
323           "Multiple read only segments for %s, not supported",
324           maps->regions[i].pathname);
325         result->start_ro = reg.start_addr;
326         result->end_ro   = reg.end_addr;
327     }
328     i++;
329   }
330
331   xbt_assert(result->file_name);
332   xbt_assert(result->start_rw);
333   xbt_assert(result->start_exec);
334 }
335
336 /************************************* Take Snapshot ************************************/
337 /****************************************************************************************/
338
339 static xbt_dynar_t MC_get_local_variables_values(xbt_dynar_t stack_frames){
340
341   unsigned cursor1 = 0;
342   mc_stack_frame_t stack_frame;
343   xbt_dynar_t variables = xbt_dynar_new(sizeof(local_variable_t), local_variable_free_voidp);
344
345   xbt_dynar_foreach(stack_frames,cursor1,stack_frame) {
346
347     unsigned cursor2 = 0;
348     dw_variable_t current_variable;
349     xbt_dynar_foreach(stack_frame->frame->variables, cursor2, current_variable){
350       
351       int region_type;
352       if((long)stack_frame->ip > (long)mc_libsimgrid_info->start_exec)
353         region_type = 1;
354       else
355         region_type = 2;
356
357       local_variable_t new_var = xbt_new0(s_local_variable_t, 1);
358       new_var->frame = xbt_strdup(stack_frame->frame_name);
359       new_var->ip = stack_frame->ip;
360       new_var->name = xbt_strdup(current_variable->name);
361       new_var->type = strdup(current_variable->type_origin);
362       new_var->region= region_type;
363       
364       /* if(current_variable->address!=NULL) {
365         new_var->address = current_variable->address;
366       } else */
367       if(current_variable->location != NULL){
368         new_var->address = (void*) MC_dwarf_resolve_location(
369           &(stack_frame->unw_cursor), current_variable->location, (void*)stack_frame->frame_base);
370       }
371
372       xbt_dynar_push(variables, &new_var);
373
374     }
375   }
376
377   return variables;
378
379 }
380
381 static void MC_stack_frame_free_voipd(void *s){
382   mc_stack_frame_t stack_frame = *(mc_stack_frame_t*)s;
383   if(stack_frame) {
384     xbt_free(stack_frame->frame_name);
385     xbt_free(stack_frame);
386   }
387 }
388
389 static xbt_dynar_t MC_unwind_stack_frames(void *stack_context) {
390   xbt_dynar_t result = xbt_dynar_new(sizeof(mc_stack_frame_t), MC_stack_frame_free_voipd);
391
392   unw_cursor_t c;
393
394   char frame_name[256];
395
396   int ret;
397   for(ret = unw_init_local(&c, (unw_context_t *)stack_context); ret >= 0; ret = unw_step(&c)){
398     mc_stack_frame_t stack_frame = xbt_new(s_mc_stack_frame_t, 1);
399     xbt_dynar_push(result, &stack_frame);
400
401     stack_frame->unw_cursor = c;
402
403     unw_get_reg(&c, UNW_REG_IP, &stack_frame->ip);
404     unw_get_reg(&c, UNW_REG_SP, &stack_frame->sp);
405
406     unw_word_t off;
407     unw_get_proc_name(&c, frame_name, sizeof (frame_name), &off);
408     stack_frame->frame_name = xbt_strdup(frame_name);
409
410     dw_frame_t frame;
411     if((long)stack_frame->ip > (long) mc_libsimgrid_info->start_exec)
412       frame = xbt_dict_get_or_null(mc_libsimgrid_info->local_variables, frame_name);
413     else
414       frame = xbt_dict_get_or_null(mc_binary_info->local_variables, frame_name);
415     stack_frame->frame = frame;
416
417     if(frame != NULL){
418       unw_word_t normalized_ip = (unw_word_t)frame->low_pc + (unw_word_t)off;
419       stack_frame->frame_base = (unw_word_t)mc_find_frame_base(normalized_ip, frame, &c);
420     } else {
421       stack_frame->frame_base = 0;
422     }
423
424     /* Stop before context switch with maestro */
425     if(!strcmp(frame_name, "smx_ctx_sysv_wrapper"))
426       break;
427   }
428
429   if(xbt_dynar_length(result) == 0){
430     XBT_INFO("unw_init_local failed");
431     xbt_abort();
432   }
433
434   return result;
435 };
436
437 static xbt_dynar_t MC_take_snapshot_stacks(mc_snapshot_t *snapshot, void *heap){
438
439   xbt_dynar_t res = xbt_dynar_new(sizeof(s_mc_snapshot_stack_t), MC_snapshot_stack_free_voidp);
440
441   unsigned int cursor = 0;
442   stack_region_t current_stack;
443   
444   xbt_dynar_foreach(stacks_areas, cursor, current_stack){
445     mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
446     st->stack_frames = MC_unwind_stack_frames(current_stack->context);
447     st->local_variables = MC_get_local_variables_values(st->stack_frames);
448
449     unw_word_t sp = xbt_dynar_get_as(st->stack_frames, 0, mc_stack_frame_t)->sp;
450     st->stack_pointer = ((char *)heap + (size_t)(((char *)((long)sp) - (char*)std_heap)));
451
452     st->real_address = current_stack->address;
453     xbt_dynar_push(res, &st);
454     (*snapshot)->stack_sizes = xbt_realloc((*snapshot)->stack_sizes, (cursor + 1) * sizeof(size_t));
455     (*snapshot)->stack_sizes[cursor] = current_stack->size - ((char *)st->stack_pointer - (char *)((char *)heap + ((char *)current_stack->address - (char *)std_heap)));
456   }
457
458   return res;
459
460 }
461
462 static xbt_dynar_t MC_take_snapshot_ignore(){
463   
464   if(mc_heap_comparison_ignore == NULL)
465     return NULL;
466
467   xbt_dynar_t cpy = xbt_dynar_new(sizeof(mc_heap_ignore_region_t), heap_ignore_region_free_voidp);
468
469   unsigned int cursor = 0;
470   mc_heap_ignore_region_t current_region;
471
472   xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, current_region){
473     mc_heap_ignore_region_t new_region = NULL;
474     new_region = xbt_new0(s_mc_heap_ignore_region_t, 1);
475     new_region->address = current_region->address;
476     new_region->size = current_region->size;
477     new_region->block = current_region->block;
478     new_region->fragment = current_region->fragment;
479     xbt_dynar_push(cpy, &new_region);
480   }
481
482   return cpy;
483
484 }
485
486 static void MC_dump_checkpoint_ignore(mc_snapshot_t snapshot){
487   
488   unsigned int cursor = 0;
489   mc_checkpoint_ignore_region_t region;
490   size_t offset;
491   
492   xbt_dynar_foreach(mc_checkpoint_ignore, cursor, region){
493     if(region->addr > snapshot->regions[0]->start_addr && (char *)(region->addr) < (char *)snapshot->regions[0]->start_addr + STD_HEAP_SIZE){
494       offset = (char *)region->addr - (char *)snapshot->regions[0]->start_addr;
495       memset((char *)snapshot->regions[0]->data + offset, 0, region->size);
496     }else if(region->addr > snapshot->regions[2]->start_addr && (char *)(region->addr) < (char*)snapshot->regions[2]->start_addr + snapshot->regions[2]->size){
497       offset = (char *)region->addr - (char *)snapshot->regions[2]->start_addr;
498       memset((char *)snapshot->regions[2]->data + offset, 0, region->size);
499     }else if(region->addr > snapshot->regions[1]->start_addr && (char *)(region->addr) < (char*)snapshot->regions[1]->start_addr + snapshot->regions[1]->size){
500       offset = (char *)region->addr - (char *)snapshot->regions[1]->start_addr;
501       memset((char *)snapshot->regions[1]->data + offset, 0, region->size);
502     }
503   }
504
505 }
506
507
508 mc_snapshot_t MC_take_snapshot(int num_state){
509
510   mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
511   snapshot->nb_processes = xbt_swag_size(simix_global->process_list);
512
513   /* Save the std heap and the writable mapped pages of libsimgrid and binary */
514   MC_get_memory_regions(snapshot);
515
516   snapshot->to_ignore = MC_take_snapshot_ignore();
517
518   if(_sg_mc_visited > 0 || strcmp(_sg_mc_property_file,"")){
519     snapshot->stacks = MC_take_snapshot_stacks(&snapshot, snapshot->regions[0]->data);
520     if(_sg_mc_hash && snapshot->stacks!=NULL) {
521       snapshot->hash = mc_hash_processes_state(num_state, snapshot->stacks);
522     } else {
523       snapshot->hash = 0;
524     }
525   }
526   else {
527     snapshot->hash = 0;
528   }
529
530   if(num_state > 0)
531     MC_dump_checkpoint_ignore(snapshot);
532
533   return snapshot;
534
535 }
536
537 void MC_restore_snapshot(mc_snapshot_t snapshot){
538   unsigned int i;
539   for(i=0; i < NB_REGIONS; i++){
540     MC_region_restore(snapshot->regions[i]);
541   }
542
543 }
544
545 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall){
546   return MC_take_snapshot(1);
547 }
548
549 void *MC_snapshot(void){
550   return simcall_mc_snapshot();
551 }