Logo AND Algorithmique Numérique Distribuée

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