Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : fix MC_state_set_executed_request
[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, size : %zu", type, new_reg->data, 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(reg.start_addr == raw_heap){
96           end_raw_heap = reg.end_addr;
97         }
98       } else {
99         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
100           start_data_libsimgrid = reg.start_addr;
101           i++;
102           reg = maps->regions[i];
103           if(reg.pathname == NULL && (reg.prot & PROT_WRITE) && i < maps->mapsize)
104             start_bss_libsimgrid = reg.start_addr;
105         }else if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
106           start_data_binary = reg.start_addr;
107           i++;
108           reg = maps->regions[i];
109           if(reg.pathname == NULL && (reg.prot & PROT_WRITE) && reg.start_addr != std_heap && reg.start_addr != raw_heap && i < maps->mapsize){
110             start_bss_binary = reg.start_addr;
111             i++;
112           }
113         }
114       }
115     }else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)){
116       if (maps->regions[i].pathname != NULL){
117         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
118           start_text_libsimgrid = reg.start_addr;
119           libsimgrid_path = strdup(maps->regions[i].pathname);
120         }else{
121           if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
122             start_text_binary = reg.start_addr;
123           }
124         }
125       }
126     }
127     i++;
128   }
129   
130   free_memory_map(maps);
131
132   MC_UNSET_RAW_MEM;
133
134   if(raw_mem_set)
135     MC_SET_RAW_MEM;
136
137 }
138
139 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall){
140   return MC_take_snapshot();
141 }
142
143 mc_snapshot_t MC_take_snapshot()
144 {
145
146   int raw_mem = (mmalloc_get_current_heap() == raw_heap);
147   
148   MC_SET_RAW_MEM;
149
150   mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
151
152   unsigned int i = 0;
153   s_map_region_t reg;
154   memory_map_t maps = get_memory_map();
155   int nb_reg = 0;
156   void *heap = NULL;
157
158   /* Save the std heap and the writable mapped pages of libsimgrid */
159   while (i < maps->mapsize) {
160     reg = maps->regions[i];
161     if ((reg.prot & PROT_WRITE)){
162       if (maps->regions[i].pathname == NULL){
163         if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
164           MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
165           heap = snapshot->regions[nb_reg]->data;
166           nb_reg++;
167         }else if(reg.start_addr == raw_heap){
168           end_raw_heap = reg.end_addr;
169         }
170         i++;
171       } else {
172         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
173           MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
174           nb_reg++;
175           i++;
176           reg = maps->regions[i];
177           if(reg.pathname == NULL && (reg.prot & PROT_WRITE) && i < maps->mapsize){
178             MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
179             reg = maps->regions[i];
180             i++;
181             nb_reg++;
182           }
183         } else {
184           if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
185             MC_snapshot_add_region(snapshot, 2, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
186             nb_reg++;
187             i++;
188             reg = maps->regions[i];
189             if(reg.pathname == NULL && (reg.prot & PROT_WRITE) && reg.start_addr != std_heap && reg.start_addr != raw_heap && i < maps->mapsize){
190               MC_snapshot_add_region(snapshot, 2, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
191               reg = maps->regions[i];
192               nb_reg++;
193             }
194           }else{
195             i++;
196           }
197         }
198       }
199     }else{
200       i++;
201     }
202   }
203
204   if(_surf_mc_visited > 0 || strcmp(_surf_mc_property_file,""))
205     snapshot->stacks = take_snapshot_stacks(heap);
206   
207   free_memory_map(maps);
208
209   MC_UNSET_RAW_MEM;
210
211   if(raw_mem)
212     MC_SET_RAW_MEM;
213
214   return snapshot;
215
216 }
217
218 void MC_restore_snapshot(mc_snapshot_t snapshot)
219 {
220   unsigned int i;
221   for(i=0; i < snapshot->num_reg; i++){
222     MC_region_restore(snapshot->regions[i]);
223   }
224
225 }
226
227 void MC_free_snapshot(mc_snapshot_t snapshot)
228 {
229   unsigned int i;
230   for(i=0; i < snapshot->num_reg; i++)
231     MC_region_destroy(snapshot->regions[i]);
232
233   xbt_dynar_free(&(snapshot->stacks));
234   xbt_free(snapshot);
235 }
236
237
238 void get_libsimgrid_plt_section(){
239
240   FILE *fp;
241   char *line = NULL;            /* Temporal storage for each line that is readed */
242   ssize_t read;                 /* Number of bytes readed */
243   size_t n = 0;                 /* Amount of bytes to read by getline */
244
245   char *lfields[7];
246   int i, plt_found = 0;
247   unsigned long int size, offset;
248
249   if(libsimgrid_path == NULL)
250     libsimgrid_path = get_libsimgrid_path();
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   free(command);
301   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   free(command);
364   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), NULL);
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     xbt_dynar_push(res, &st);
392   }
393
394   return res;
395
396 }
397
398 static void *get_stack_pointer(void *stack_context, void *heap){
399
400   unw_cursor_t c;
401   int ret;
402   unw_word_t sp;
403
404   ret = unw_init_local(&c, (unw_context_t *)stack_context);
405   if(ret < 0){
406     XBT_INFO("unw_init_local failed");
407     xbt_abort();
408   }
409
410   unw_get_reg(&c, UNW_REG_SP, &sp);
411
412   return ((char *)heap + (size_t)(((char *)((long)sp) - (char*)std_heap)));
413
414 }
415
416 static xbt_strbuff_t get_local_variables_values(void *stack_context, void *heap){
417   
418   unw_cursor_t c;
419   int ret;
420
421   char frame_name[256];
422   
423   ret = unw_init_local(&c, (unw_context_t *)stack_context);
424   if(ret < 0){
425     XBT_INFO("unw_init_local failed");
426     xbt_abort();
427   }
428
429   unw_word_t ip, sp, off;
430   dw_frame_t frame;
431  
432   xbt_dynar_t compose = xbt_dynar_new(sizeof(variable_value_t), NULL);
433
434   xbt_strbuff_t variables = xbt_strbuff_new();
435   xbt_dict_cursor_t dict_cursor;
436   char *variable_name;
437   dw_local_variable_t current_variable;
438   unsigned int cursor = 0, cursor2 = 0;
439   dw_location_entry_t entry = NULL;
440   dw_location_t location_entry = NULL;
441   unw_word_t res;
442   int frame_found = 0;
443   void *frame_pointer_address = NULL;
444   long true_ip;
445
446   while(ret >= 0){
447
448     unw_get_reg(&c, UNW_REG_IP, &ip);
449     unw_get_reg(&c, UNW_REG_SP, &sp);
450
451     unw_get_proc_name (&c, frame_name, sizeof (frame_name), &off);
452
453     xbt_strbuff_append(variables, bprintf("ip=%s\n", frame_name));
454
455     frame = xbt_dict_get_or_null(mc_local_variables, frame_name);
456
457     if(frame == NULL){
458       ret = unw_step(&c);
459       continue;
460     }
461
462     true_ip = (long)frame->low_pc + (long)off;
463
464     /* Get frame pointer */
465     switch(frame->frame_base->type){
466     case e_dw_loclist:
467       while((cursor < xbt_dynar_length(frame->frame_base->location.loclist)) && frame_found == 0){
468         entry = xbt_dynar_get_as(frame->frame_base->location.loclist, cursor, dw_location_entry_t);
469         if((true_ip >= entry->lowpc) && (true_ip < entry->highpc)){
470           frame_found = 1;
471           switch(entry->location->type){
472           case e_dw_compose:
473             xbt_dynar_reset(compose);
474             cursor2 = 0;
475             while(cursor2 < xbt_dynar_length(entry->location->location.compose)){
476               location_entry = xbt_dynar_get_as(entry->location->location.compose, cursor2, dw_location_t);
477               switch(location_entry->type){
478               case e_dw_register:
479                 unw_get_reg(&c, location_entry->location.reg, &res);
480                 add_value(&compose, "address", (long)res);
481                 break;
482               case e_dw_bregister_op:
483                 unw_get_reg(&c, location_entry->location.breg_op.reg, &res);
484                 add_value(&compose, "address", (long)res + location_entry->location.breg_op.offset);
485                 break;
486               default:
487                 xbt_dynar_reset(compose);
488                 break;
489               }
490               cursor2++;
491             }
492
493             if(xbt_dynar_length(compose) > 0){
494               frame_pointer_address = xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.address ; 
495             }
496             break;
497           default :
498             frame_pointer_address = NULL;
499             break;
500           }
501         }
502         cursor++;
503       }
504       break;
505     default :
506       frame_pointer_address = NULL;
507       break;
508     }
509
510     frame_found = 0;
511     cursor = 0;
512
513     xbt_dict_foreach(frame->variables, dict_cursor, variable_name, current_variable){
514       if(current_variable->location != NULL){
515         switch(current_variable->location->type){
516         case e_dw_compose:
517           xbt_dynar_reset(compose);
518           cursor = 0;
519           while(cursor < xbt_dynar_length(current_variable->location->location.compose)){
520             location_entry = xbt_dynar_get_as(current_variable->location->location.compose, cursor, dw_location_t);
521             switch(location_entry->type){
522             case e_dw_register:
523               unw_get_reg(&c, location_entry->location.reg, &res);
524               add_value(&compose, "value", (long)res);
525               break;
526             case e_dw_bregister_op:
527               unw_get_reg(&c, location_entry->location.breg_op.reg, &res);
528               add_value(&compose, "address", (long)res + location_entry->location.breg_op.offset);
529               break;
530             case e_dw_fbregister_op:
531               if(frame_pointer_address != NULL)
532                 add_value(&compose, "address", (long)((char *)frame_pointer_address + location_entry->location.fbreg_op));
533               break;
534             default:
535               xbt_dynar_reset(compose);
536               break;
537             }
538             cursor++;
539           }
540           
541           if(xbt_dynar_length(compose) > 0){
542             if(strcmp(xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->type, "value") == 0){
543               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));
544             }else{
545               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){
546                 xbt_strbuff_append(variables, bprintf("%s=NULL\n", current_variable->name));
547               }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)){
548                 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)));
549               }else{ 
550                 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)));
551               }
552             }
553           }else{
554             xbt_strbuff_append(variables, bprintf("%s=undefined\n", current_variable->name));
555           }
556           break;
557         default :
558           break;
559         }
560       }else{
561         xbt_strbuff_append(variables, bprintf("%s=undefined\n", current_variable->name));
562       }
563     }    
564  
565     ret = unw_step(&c);
566      
567   }
568
569   return variables;
570
571 }
572
573 static void print_local_variables_values(xbt_dynar_t all_variables){
574
575   unsigned cursor = 0;
576   mc_snapshot_stack_t stack;
577
578   xbt_dynar_foreach(all_variables, cursor, stack){
579     XBT_INFO("%s", stack->local_variables->data);
580   }
581 }
582
583
584 static void snapshot_stack_free(mc_snapshot_stack_t s){
585   if(s){
586     xbt_free(s->local_variables->data);
587     xbt_free(s->local_variables);
588     xbt_free(s);
589   }
590 }
591
592 void snapshot_stack_free_voidp(void *s){
593   snapshot_stack_free((mc_snapshot_stack_t) * (void **) s);
594 }
595
596 void *MC_snapshot(void){
597
598   return simcall_mc_snapshot();
599   
600 }