Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : add nb_processes in snapshot
[simgrid.git] / src / mc / mc_checkpoint.c
1 /* Copyright (c) 2008-2012 Da SimGrid Team. All rights reserved.            */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <libgen.h>
7 #include "mc_private.h"
8 #include "xbt/module.h"
9
10 #include "../simix/smx_private.h"
11
12 #include <libunwind.h>
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
15                                 "Logging specific to mc_checkpoint");
16
17 void *start_text_libsimgrid;
18 void *start_plt_libsimgrid, *end_plt_libsimgrid;
19 void *start_got_plt_libsimgrid, *end_got_plt_libsimgrid;
20 void *start_plt_binary, *end_plt_binary;
21 void *start_got_plt_binary, *end_got_plt_binary;
22 char *libsimgrid_path;
23 void *start_data_libsimgrid, *start_bss_libsimgrid;
24 void *start_data_binary, *start_bss_binary;
25 void *start_text_binary;
26 void *end_raw_heap;
27
28 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size);
29 static void MC_region_restore(mc_mem_region_t reg);
30 static void MC_region_destroy(mc_mem_region_t reg);
31
32 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size);
33
34 static void add_value(xbt_dynar_t *list, const char *type, unsigned long int val);
35 static xbt_dynar_t take_snapshot_stacks(void *heap);
36 static xbt_strbuff_t get_local_variables_values(void *stack_context, void *heap);
37 static void print_local_variables_values(xbt_dynar_t all_variables);
38 static void *get_stack_pointer(void *stack_context, void *heap);
39
40 static void snapshot_stack_free(mc_snapshot_stack_t s);
41
42 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
43 {
44   mc_mem_region_t new_reg = xbt_new0(s_mc_mem_region_t, 1);
45   new_reg->type = type;
46   new_reg->start_addr = start_addr;
47   new_reg->size = size;
48   new_reg->data = xbt_malloc0(size);
49   memcpy(new_reg->data, start_addr, size);
50
51   XBT_DEBUG("New region : type : %d, data : %p (real addr %p), size : %zu", type, new_reg->data, start_addr, size);
52   
53   return new_reg;
54 }
55
56 static void MC_region_restore(mc_mem_region_t reg)
57 {
58   /*FIXME: check if start_addr is still mapped, if it is not, then map it
59     before copying the data */
60  
61   memcpy(reg->start_addr, reg->data, reg->size);
62  
63   return;
64 }
65
66 static void MC_region_destroy(mc_mem_region_t reg)
67 {
68   xbt_free(reg->data);
69   xbt_free(reg);
70 }
71
72 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size)
73 {
74   mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
75   snapshot->regions = xbt_realloc(snapshot->regions, (snapshot->num_reg + 1) * sizeof(mc_mem_region_t));
76   snapshot->regions[snapshot->num_reg] = new_reg;
77   snapshot->num_reg++;
78   return;
79
80
81 void MC_init_memory_map_info(){
82
83   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
84
85   MC_SET_RAW_MEM;
86   
87   unsigned int i = 0;
88   s_map_region_t reg;
89   memory_map_t maps = get_memory_map();
90
91   while (i < maps->mapsize) {
92     reg = maps->regions[i];
93     if ((reg.prot & PROT_WRITE)){
94       if (maps->regions[i].pathname != NULL){
95         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
96           start_data_libsimgrid = reg.start_addr;
97           i++;
98           reg = maps->regions[i];
99           if(reg.pathname == NULL && (reg.prot & PROT_WRITE) && i < maps->mapsize)
100             start_bss_libsimgrid = reg.start_addr;
101         }else if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
102           start_data_binary = reg.start_addr;
103           i++;
104           reg = maps->regions[i];
105           if(reg.pathname == NULL && (reg.prot & PROT_WRITE) && reg.start_addr != std_heap && reg.start_addr != raw_heap && i < maps->mapsize){
106             start_bss_binary = reg.start_addr;
107             i++;
108           }
109         }else if(!memcmp(maps->regions[i].pathname, "[heap]", 6)){
110           end_raw_heap = reg.end_addr;
111           i++;
112         }
113       }
114     }else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)){
115       if (maps->regions[i].pathname != NULL){
116         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
117           start_text_libsimgrid = reg.start_addr;
118           libsimgrid_path = strdup(maps->regions[i].pathname);
119         }else if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
120           start_text_binary = reg.start_addr;
121         }
122       }
123     }
124     i++;
125   }
126   
127   free_memory_map(maps);
128
129   MC_UNSET_RAW_MEM;
130
131   if(raw_mem_set)
132     MC_SET_RAW_MEM;
133
134 }
135
136 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall){
137   return MC_take_snapshot();
138 }
139
140 mc_snapshot_t MC_take_snapshot()
141 {
142   int raw_mem = (mmalloc_get_current_heap() == raw_heap);
143   
144   MC_SET_RAW_MEM;
145
146   mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
147   snapshot->nb_processes = xbt_swag_size(simix_global->process_list);
148
149   unsigned int i = 0;
150   s_map_region_t reg;
151   memory_map_t maps = get_memory_map();
152   int nb_reg = 0;
153   void *heap = NULL;
154   size_t size = 0;
155   void *start = NULL;
156
157   /* Save the std heap and the writable mapped pages of libsimgrid */
158   while (i < maps->mapsize) {
159     reg = maps->regions[i];
160     if ((reg.prot & PROT_WRITE)){
161       if (maps->regions[i].pathname == NULL){
162         if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
163           MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
164           snapshot->heap_chunks_used = mmalloc_get_chunks_used(std_heap);
165           heap = snapshot->regions[nb_reg]->data;
166           nb_reg++;
167         }
168         i++;
169       } else{ 
170         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
171           size = (char*)reg.end_addr - (char*)reg.start_addr;
172           start = reg.start_addr;
173           nb_reg++;
174           i++;
175           reg = maps->regions[i];
176           if(reg.pathname == NULL && (reg.prot & PROT_WRITE) && i < maps->mapsize){
177             size += (char*)reg.end_addr - (char*)reg.start_addr;
178             reg = maps->regions[i];
179             i++;
180           }
181           MC_snapshot_add_region(snapshot, 1, start, size);
182         }else if(!memcmp(maps->regions[i].pathname, "[heap]", 6)){
183           end_raw_heap = reg.end_addr;
184           i++;
185         } else if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
186           size = (char*)reg.end_addr - (char*)reg.start_addr;
187           start = reg.start_addr;
188           nb_reg++;
189           i++;
190           reg = maps->regions[i];
191           if(reg.pathname == NULL && (reg.prot & PROT_WRITE) && reg.start_addr != std_heap && reg.start_addr != raw_heap && i < maps->mapsize){
192             size += (char*)reg.end_addr - (char*)reg.start_addr;
193             reg = maps->regions[i];
194             i++;
195           }
196           MC_snapshot_add_region(snapshot, 2, start, size);
197         }else{
198           i++;
199         }
200       }
201     }else{
202       i++;
203     }
204   }
205
206   if(_sg_mc_visited > 0 || strcmp(_sg_mc_property_file,""))
207     snapshot->stacks = take_snapshot_stacks(heap);
208   
209   free_memory_map(maps);
210
211   MC_UNSET_RAW_MEM;
212
213   if(raw_mem)
214     MC_SET_RAW_MEM;
215
216   return snapshot;
217
218 }
219
220 void MC_restore_snapshot(mc_snapshot_t snapshot)
221 {
222   unsigned int i;
223   for(i=0; i < snapshot->num_reg; i++){
224     MC_region_restore(snapshot->regions[i]);
225   }
226
227 }
228
229 void MC_free_snapshot(mc_snapshot_t snapshot)
230 {
231   unsigned int i;
232   for(i=0; i < snapshot->num_reg; i++)
233     MC_region_destroy(snapshot->regions[i]);
234
235   xbt_free(snapshot->regions);
236   xbt_dynar_free(&(snapshot->stacks));
237   xbt_free(snapshot);
238 }
239
240
241 void get_libsimgrid_plt_section(){
242
243   FILE *fp;
244   char *line = NULL;            /* Temporal storage for each line that is readed */
245   ssize_t read;                 /* Number of bytes readed */
246   size_t n = 0;                 /* Amount of bytes to read by getline */
247
248   char *lfields[7];
249   int i, plt_found = 0;
250   unsigned long int size, offset;
251
252   char *command = bprintf("objdump --section-headers %s", libsimgrid_path);
253
254   fp = popen(command, "r");
255
256   if(fp == NULL){
257     perror("popen failed");
258     xbt_abort();
259   }
260
261   while ((read = getline(&line, &n, fp)) != -1 && plt_found != 2) {
262
263     if(n == 0)
264       continue;
265
266     /* Wipeout the new line character */
267     line[read - 1] = '\0';
268
269     lfields[0] = strtok(line, " ");
270
271     if(lfields[0] == NULL)
272       continue;
273
274     if(strcmp(lfields[0], "Sections:") == 0 || strcmp(lfields[0], "Idx") == 0 || strncmp(lfields[0], libsimgrid_path, strlen(libsimgrid_path)) == 0)
275       continue;
276
277     for (i = 1; i < 7 && lfields[i - 1] != NULL; i++) {
278       lfields[i] = strtok(NULL, " ");
279     }
280
281     if(i>=6){
282       if(strcmp(lfields[1], ".plt") == 0){
283         size = strtoul(lfields[2], NULL, 16);
284         offset = strtoul(lfields[5], NULL, 16);
285         start_plt_libsimgrid = (char *)start_text_libsimgrid + offset;
286         end_plt_libsimgrid = (char *)start_plt_libsimgrid + size;
287         plt_found++;
288       }else if(strcmp(lfields[1], ".got.plt") == 0){
289         size = strtoul(lfields[2], NULL, 16);
290         offset = strtoul(lfields[5], NULL, 16);
291         start_got_plt_libsimgrid = (char *)start_text_libsimgrid + offset;
292         end_got_plt_libsimgrid = (char *)start_got_plt_libsimgrid + size;
293         plt_found++;
294        }
295
296     }
297     
298   }
299
300   xbt_free(command);
301   xbt_free(line);
302   pclose(fp);
303
304 }
305
306 void get_binary_plt_section(){
307
308   FILE *fp;
309   char *line = NULL;            /* Temporal storage for each line that is readed */
310   ssize_t read;                 /* Number of bytes readed */
311   size_t n = 0;                 /* Amount of bytes to read by getline */
312
313   char *lfields[7];
314   int i, plt_found = 0;
315   unsigned long int size;
316
317   char *command = bprintf( "objdump --section-headers %s", xbt_binary_name);
318
319   fp = popen(command, "r");
320
321   if(fp == NULL){
322     perror("popen failed");
323     xbt_abort();
324   }
325
326   while ((read = getline(&line, &n, fp)) != -1 && plt_found != 2) {
327
328     if(n == 0)
329       continue;
330
331     /* Wipeout the new line character */
332     line[read - 1] = '\0';
333
334     lfields[0] = strtok(line, " ");
335
336     if(lfields[0] == NULL)
337       continue;
338
339     if(strcmp(lfields[0], "Sections:") == 0 || strcmp(lfields[0], "Idx") == 0 || strncmp(lfields[0], basename(xbt_binary_name), strlen(xbt_binary_name)) == 0)
340       continue;
341
342     for (i = 1; i < 7 && lfields[i - 1] != NULL; i++) {
343       lfields[i] = strtok(NULL, " ");
344     }
345
346     if(i>=6){
347       if(strcmp(lfields[1], ".plt") == 0){
348         size = strtoul(lfields[2], NULL, 16);
349         start_plt_binary = (void *)strtoul(lfields[3], NULL, 16);
350         end_plt_binary = (char *)start_plt_binary + size;
351         plt_found++;
352       }else if(strcmp(lfields[1], ".got.plt") == 0){
353         size = strtoul(lfields[2], NULL, 16);
354         start_got_plt_binary = (char *)strtoul(lfields[3], NULL, 16);
355         end_got_plt_binary = (char *)start_got_plt_binary + size;
356         plt_found++;
357        }
358     }
359     
360     
361   }
362
363   xbt_free(command);
364   xbt_free(line);
365   pclose(fp);
366
367 }
368
369 static void add_value(xbt_dynar_t *list, const char *type, unsigned long int val){
370   variable_value_t value = xbt_new0(s_variable_value_t, 1);
371   value->type = strdup(type);
372   if(strcmp(type, "value") == 0){
373     value->value.res = val;
374   }else{
375     value->value.address = (void *)val;
376   }
377   xbt_dynar_push(*list, &value);
378 }
379
380 static xbt_dynar_t take_snapshot_stacks(void *heap){
381
382   xbt_dynar_t res = xbt_dynar_new(sizeof(s_mc_snapshot_stack_t), snapshot_stack_free_voidp);
383
384   unsigned int cursor1 = 0;
385   stack_region_t current_stack;
386   
387   xbt_dynar_foreach(stacks_areas, cursor1, current_stack){
388     mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
389     st->local_variables = get_local_variables_values(current_stack->context, heap);
390     st->stack_pointer = get_stack_pointer(current_stack->context, heap);
391     st->size_used = current_stack->size - ((char *)st->stack_pointer - (char *)((char *)heap + ((char *)current_stack->address - (char *)std_heap)));
392     xbt_dynar_push(res, &st);
393   }
394
395   return res;
396
397 }
398
399 static void *get_stack_pointer(void *stack_context, void *heap){
400
401   unw_cursor_t c;
402   int ret;
403   unw_word_t sp;
404
405   ret = unw_init_local(&c, (unw_context_t *)stack_context);
406   if(ret < 0){
407     XBT_INFO("unw_init_local failed");
408     xbt_abort();
409   }
410
411   unw_get_reg(&c, UNW_REG_SP, &sp);
412
413   return ((char *)heap + (size_t)(((char *)((long)sp) - (char*)std_heap)));
414
415 }
416
417 static xbt_strbuff_t get_local_variables_values(void *stack_context, void *heap){
418   
419   unw_cursor_t c;
420   int ret;
421
422   char frame_name[256];
423   
424   ret = unw_init_local(&c, (unw_context_t *)stack_context);
425   if(ret < 0){
426     XBT_INFO("unw_init_local failed");
427     xbt_abort();
428   }
429
430   unw_word_t ip, sp, off;
431   dw_frame_t frame;
432  
433   xbt_dynar_t compose = xbt_dynar_new(sizeof(variable_value_t), variable_value_free_voidp);
434
435   xbt_strbuff_t variables = xbt_strbuff_new();
436   xbt_dict_cursor_t dict_cursor;
437   char *variable_name;
438   dw_local_variable_t current_variable;
439   unsigned int cursor = 0, cursor2 = 0;
440   dw_location_entry_t entry = NULL;
441   dw_location_t location_entry = NULL;
442   unw_word_t res;
443   int frame_found = 0;
444   void *frame_pointer_address = NULL;
445   long true_ip;
446   char *to_append;
447
448   while(ret >= 0){
449
450     unw_get_reg(&c, UNW_REG_IP, &ip);
451     unw_get_reg(&c, UNW_REG_SP, &sp);
452
453     unw_get_proc_name(&c, frame_name, sizeof (frame_name), &off);
454
455     frame = xbt_dict_get_or_null(mc_local_variables, frame_name);
456
457     if(frame == NULL){
458       xbt_dynar_free(&compose);
459       xbt_dict_cursor_free(&dict_cursor);
460       return variables;
461     }
462
463     to_append = bprintf("ip=%s\n", frame_name);
464     xbt_strbuff_append(variables, to_append);
465     xbt_free(to_append);
466
467     true_ip = (long)frame->low_pc + (long)off;
468
469     /* Get frame pointer */
470     switch(frame->frame_base->type){
471     case e_dw_loclist:
472       while((cursor < xbt_dynar_length(frame->frame_base->location.loclist)) && frame_found == 0){
473         entry = xbt_dynar_get_as(frame->frame_base->location.loclist, cursor, dw_location_entry_t);
474         if((true_ip >= entry->lowpc) && (true_ip < entry->highpc)){
475           frame_found = 1;
476           switch(entry->location->type){
477           case e_dw_compose:
478             xbt_dynar_reset(compose);
479             cursor2 = 0;
480             while(cursor2 < xbt_dynar_length(entry->location->location.compose)){
481               location_entry = xbt_dynar_get_as(entry->location->location.compose, cursor2, dw_location_t);
482               switch(location_entry->type){
483               case e_dw_register:
484                 unw_get_reg(&c, location_entry->location.reg, &res);
485                 add_value(&compose, "address", (long)res);
486                 break;
487               case e_dw_bregister_op:
488                 unw_get_reg(&c, location_entry->location.breg_op.reg, &res);
489                 add_value(&compose, "address", (long)res + location_entry->location.breg_op.offset);
490                 break;
491               default:
492                 xbt_dynar_reset(compose);
493                 break;
494               }
495               cursor2++;
496             }
497
498             if(xbt_dynar_length(compose) > 0){
499               frame_pointer_address = xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.address ; 
500             }
501             break;
502           default :
503             frame_pointer_address = NULL;
504             break;
505           }
506         }
507         cursor++;
508       }
509       break;
510     default :
511       frame_pointer_address = NULL;
512       break;
513     }
514
515     frame_found = 0;
516     cursor = 0;
517
518     xbt_dict_foreach(frame->variables, dict_cursor, variable_name, current_variable){
519       if(current_variable->location != NULL){
520         switch(current_variable->location->type){
521         case e_dw_compose:
522           xbt_dynar_reset(compose);
523           cursor = 0;
524           while(cursor < xbt_dynar_length(current_variable->location->location.compose)){
525             location_entry = xbt_dynar_get_as(current_variable->location->location.compose, cursor, dw_location_t);
526             switch(location_entry->type){
527             case e_dw_register:
528               unw_get_reg(&c, location_entry->location.reg, &res);
529               add_value(&compose, "value", (long)res);
530               break;
531             case e_dw_bregister_op:
532               unw_get_reg(&c, location_entry->location.breg_op.reg, &res);
533               add_value(&compose, "address", (long)res + location_entry->location.breg_op.offset);
534               break;
535             case e_dw_fbregister_op:
536               if(frame_pointer_address != NULL)
537                 add_value(&compose, "address", (long)((char *)frame_pointer_address + location_entry->location.fbreg_op));
538               break;
539             default:
540               xbt_dynar_reset(compose);
541               break;
542             }
543             cursor++;
544           }
545           
546           if(xbt_dynar_length(compose) > 0){
547             if(strcmp(xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->type, "value") == 0){
548               to_append = bprintf("%s=%lx\n", current_variable->name, xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.res);
549               xbt_strbuff_append(variables, to_append);
550               xbt_free(to_append);
551             }else{
552               if((long)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1,variable_value_t)->value.address < 0 || *((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1,variable_value_t)->value.address) == NULL){
553                 to_append = bprintf("%s=NULL\n", current_variable->name);
554                 xbt_strbuff_append(variables, to_append);
555                 xbt_free(to_append);
556               }else if(((long)*((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1,variable_value_t)->value.address) > 0xffffffff) || ((long)*((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1,variable_value_t)->value.address) < (long)start_text_binary)){
557                 to_append = bprintf("%s=%d\n", current_variable->name, (int)(long)*((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.address));
558                 xbt_strbuff_append(variables, to_append);
559                 xbt_free(to_append);
560               }else{ 
561                 to_append = bprintf("%s=%p\n", current_variable->name, *((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.address));
562                 xbt_strbuff_append(variables, to_append);
563                 xbt_free(to_append);
564               }
565             }
566           }else{
567             to_append = bprintf("%s=undefined\n", current_variable->name);
568             xbt_strbuff_append(variables, to_append);
569             xbt_free(to_append);
570           }
571           break;
572         default :
573           break;
574         }
575       }else{
576         to_append = bprintf("%s=undefined\n", current_variable->name);
577         xbt_strbuff_append(variables, to_append);
578         xbt_free(to_append);
579       }
580     }    
581  
582     ret = unw_step(&c);
583      
584   }
585
586   xbt_dynar_free(&compose);
587   xbt_dict_cursor_free(&dict_cursor);
588
589   return variables;
590
591 }
592
593 static void print_local_variables_values(xbt_dynar_t all_variables){
594
595   unsigned cursor = 0;
596   mc_snapshot_stack_t stack;
597
598   xbt_dynar_foreach(all_variables, cursor, stack){
599     XBT_INFO("%s", stack->local_variables->data);
600   }
601 }
602
603
604 static void snapshot_stack_free(mc_snapshot_stack_t s){
605   if(s){
606     xbt_free(s->local_variables->data);
607     xbt_free(s->local_variables);
608     xbt_free(s);
609   }
610 }
611
612 void snapshot_stack_free_voidp(void *s){
613   snapshot_stack_free((mc_snapshot_stack_t) * (void **) s);
614 }
615
616 void *MC_snapshot(void){
617
618   return simcall_mc_snapshot();
619   
620 }
621
622 void variable_value_free(variable_value_t v){
623   if(v){
624     xbt_free(v->type);
625     xbt_free(v);
626   }
627 }
628
629 void variable_value_free_voidp(void* v){
630   variable_value_free((variable_value_t) * (void **)v);
631 }
632