Logo AND Algorithmique Numérique Distribuée

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