Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : change MC_take_snapshot_liveness declaration (snapshot returned inste...
[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;
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 mc_snapshot_t MC_take_snapshot_liveness()
116 {
117
118   raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
119
120   MC_SET_RAW_MEM;
121
122   mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
123
124   unsigned int i = 0;
125   s_map_region_t reg;
126   memory_map_t maps = get_memory_map();
127   int nb_reg = 0;
128   void *heap = NULL;
129
130   /* Save the std heap and the writable mapped pages of libsimgrid */
131   while (i < maps->mapsize) {
132     reg = maps->regions[i];
133     if ((reg.prot & PROT_WRITE)){
134       if (maps->regions[i].pathname == NULL){
135         if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
136           MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
137           heap = snapshot->regions[nb_reg]->data;
138           nb_reg++;
139         }else if(reg.start_addr == raw_heap){
140           end_raw_heap = reg.end_addr;
141         }
142         i++;
143       } else {
144         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
145           MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
146           start_data_libsimgrid = reg.start_addr;
147           nb_reg++;
148           i++;
149           reg = maps->regions[i];
150           while(reg.pathname == NULL && (reg.prot & PROT_WRITE) && i < maps->mapsize){
151             MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
152             i++;
153             reg = maps->regions[i];
154             nb_reg++;
155           }
156         } else {
157           if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
158             MC_snapshot_add_region(snapshot, 2, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
159             nb_reg++;
160           }
161           i++;
162         }
163       }
164     }else if ((reg.prot & PROT_READ)){
165       if (maps->regions[i].pathname != NULL){
166         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
167           start_text_libsimgrid = reg.start_addr;
168           libsimgrid_path = strdup(maps->regions[i].pathname);
169         }else{
170           if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
171             start_text_binary = reg.start_addr;
172           }
173         }
174       }
175       i++;
176     }else{
177       i++;
178     }
179   }
180
181   snapshot->stacks = take_snapshot_stacks(heap);
182   
183   free_memory_map(maps);
184
185   MC_UNSET_RAW_MEM;
186
187   if(raw_mem_set)
188     MC_SET_RAW_MEM;
189
190   return snapshot;
191
192 }
193
194 void MC_restore_snapshot(mc_snapshot_t snapshot)
195 {
196   unsigned int i;
197   for(i=0; i < snapshot->num_reg; i++){
198     MC_region_restore(snapshot->regions[i]);
199   }
200
201 }
202
203 void MC_free_snapshot(mc_snapshot_t snapshot)
204 {
205   unsigned int i;
206   for(i=0; i < snapshot->num_reg; i++)
207     MC_region_destroy(snapshot->regions[i]);
208
209   xbt_dynar_free(&(snapshot->stacks));
210   xbt_free(snapshot);
211 }
212
213
214 void get_libsimgrid_plt_section(){
215
216   FILE *fp;
217   char *line = NULL;            /* Temporal storage for each line that is readed */
218   ssize_t read;                 /* Number of bytes readed */
219   size_t n = 0;                 /* Amount of bytes to read by getline */
220
221   char *lfields[7];
222   int i, plt_not_found = 1;
223   unsigned long int size, offset;
224
225   char *command = bprintf( "objdump --section-headers %s", libsimgrid_path);
226
227   fp = popen(command, "r");
228
229   if(fp == NULL)
230     perror("popen failed");
231
232   while ((read = getline(&line, &n, fp)) != -1 && plt_not_found == 1) {
233
234     if(n == 0)
235       continue;
236
237     /* Wipeout the new line character */
238     line[read - 1] = '\0';
239
240     lfields[0] = strtok(line, " ");
241
242     if(lfields[0] == NULL)
243       continue;
244
245     if(strcmp(lfields[0], "Sections:") == 0 || strcmp(lfields[0], "Idx") == 0 || strncmp(lfields[0], libsimgrid_path, strlen(libsimgrid_path)) == 0)
246       continue;
247
248     for (i = 1; i < 7 && lfields[i - 1] != NULL; i++) {
249       lfields[i] = strtok(NULL, " ");
250     }
251
252     if(i>=6){
253       if(strcmp(lfields[1], ".plt") == 0){
254         size = strtoul(lfields[2], NULL, 16);
255         offset = strtoul(lfields[5], NULL, 16);
256         start_plt_libsimgrid = (char *)start_text_libsimgrid + offset;
257         end_plt_libsimgrid = (char *)start_plt_libsimgrid + size;
258         plt_not_found = 0;
259       }
260     }
261     
262     
263   }
264
265   free(command);
266   free(line);
267   pclose(fp);
268
269 }
270
271 void get_binary_plt_section(){
272
273   FILE *fp;
274   char *line = NULL;            /* Temporal storage for each line that is readed */
275   ssize_t read;                 /* Number of bytes readed */
276   size_t n = 0;                 /* Amount of bytes to read by getline */
277
278   char *lfields[7];
279   int i, plt_not_found = 1;
280   unsigned long int size, offset;
281
282   char *command = bprintf( "objdump --section-headers %s", xbt_binary_name);
283
284   fp = popen(command, "r");
285
286   if(fp == NULL)
287     perror("popen failed");
288
289   while ((read = getline(&line, &n, fp)) != -1 && plt_not_found == 1) {
290
291     if(n == 0)
292       continue;
293
294     /* Wipeout the new line character */
295     line[read - 1] = '\0';
296
297     lfields[0] = strtok(line, " ");
298
299     if(lfields[0] == NULL)
300       continue;
301
302     if(strcmp(lfields[0], "Sections:") == 0 || strcmp(lfields[0], "Idx") == 0 || strncmp(lfields[0], basename(xbt_binary_name), strlen(xbt_binary_name)) == 0)
303       continue;
304
305     for (i = 1; i < 7 && lfields[i - 1] != NULL; i++) {
306       lfields[i] = strtok(NULL, " ");
307     }
308
309     if(i>=6){
310       if(strcmp(lfields[1], ".plt") == 0){
311         size = strtoul(lfields[2], NULL, 16);
312         offset = strtoul(lfields[5], NULL, 16);
313         start_plt_binary = (char *)start_text_binary + offset;
314         end_plt_binary = (char *)start_plt_binary + size;
315         plt_not_found = 0;
316       }
317     }
318     
319     
320   }
321
322   free(command);
323   free(line);
324   pclose(fp);
325
326 }
327
328 static void add_value(xbt_dynar_t *list, const char *type, unsigned long int val){
329   variable_value_t value = xbt_new0(s_variable_value_t, 1);
330   value->type = strdup(type);
331   if(strcmp(type, "value") == 0){
332     value->value.res = val;
333   }else{
334     value->value.address = (void *)val;
335   }
336   xbt_dynar_push(*list, &value);
337 }
338
339 static xbt_dynar_t take_snapshot_stacks(void *heap){
340
341   xbt_dynar_t res = xbt_dynar_new(sizeof(s_mc_snapshot_stack_t), NULL);
342
343   unsigned int cursor1 = 0;
344   stack_region_t current_stack;
345   
346   xbt_dynar_foreach(stacks_areas, cursor1, current_stack){
347     mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
348     st->local_variables = get_local_variables_values(current_stack->context, heap);
349     st->stack_pointer = get_stack_pointer(current_stack->context, heap);
350     xbt_dynar_push(res, &st);
351   }
352
353   return res;
354
355 }
356
357 static void *get_stack_pointer(void *stack_context, void *heap){
358
359   unw_cursor_t c;
360   int ret;
361   unw_word_t sp;
362
363   ret = unw_init_local(&c, (unw_context_t *)stack_context);
364   if(ret < 0){
365     XBT_INFO("unw_init_local failed");
366     xbt_abort();
367   }
368
369   unw_get_reg(&c, UNW_REG_SP, &sp);
370
371   return ((char *)heap + (size_t)(((char *)((long)sp) - (char*)std_heap)));
372
373 }
374
375 static xbt_strbuff_t get_local_variables_values(void *stack_context, void *heap){
376   
377   unw_cursor_t c;
378   int ret;
379   //char *stack_name;
380
381   char frame_name[256];
382   
383   ret = unw_init_local(&c, (unw_context_t *)stack_context);
384   if(ret < 0){
385     XBT_INFO("unw_init_local failed");
386     xbt_abort();
387   }
388
389   //stack_name = strdup(((smx_process_t)((smx_ctx_sysv_t)(stack->address))->super.data)->name);
390
391   unw_word_t ip, sp, off;
392   dw_frame_t frame;
393  
394   xbt_dynar_t compose = xbt_dynar_new(sizeof(variable_value_t), NULL);
395
396   xbt_strbuff_t variables = xbt_strbuff_new();
397   xbt_dict_cursor_t dict_cursor;
398   char *variable_name;
399   dw_local_variable_t current_variable;
400   unsigned int cursor = 0, cursor2 = 0;
401   dw_location_entry_t entry = NULL;
402   dw_location_t location_entry = NULL;
403   unw_word_t res;
404   int frame_found = 0;
405   void *frame_pointer_address = NULL;
406   long true_ip;
407
408   while(ret >= 0){
409
410     unw_get_reg(&c, UNW_REG_IP, &ip);
411     unw_get_reg(&c, UNW_REG_SP, &sp);
412
413     unw_get_proc_name (&c, frame_name, sizeof (frame_name), &off);
414
415     xbt_strbuff_append(variables, bprintf("ip=%s\n", frame_name));
416
417     frame = xbt_dict_get_or_null(mc_local_variables, frame_name);
418
419     if(frame == NULL){
420       ret = unw_step(&c);
421       continue;
422     }
423
424     true_ip = (long)frame->low_pc + (long)off;
425
426     /* Get frame pointer */
427     switch(frame->frame_base->type){
428     case e_dw_loclist:
429       while((cursor < xbt_dynar_length(frame->frame_base->location.loclist)) && frame_found == 0){
430         entry = xbt_dynar_get_as(frame->frame_base->location.loclist, cursor, dw_location_entry_t);
431         if((true_ip >= entry->lowpc) && (true_ip < entry->highpc)){
432           frame_found = 1;
433           switch(entry->location->type){
434           case e_dw_compose:
435             xbt_dynar_reset(compose);
436             cursor2 = 0;
437             while(cursor2 < xbt_dynar_length(entry->location->location.compose)){
438               location_entry = xbt_dynar_get_as(entry->location->location.compose, cursor2, dw_location_t);
439               switch(location_entry->type){
440               case e_dw_register:
441                 unw_get_reg(&c, location_entry->location.reg, &res);
442                 add_value(&compose, "address", (long)res);
443                 break;
444               case e_dw_bregister_op:
445                 unw_get_reg(&c, location_entry->location.breg_op.reg, &res);
446                 add_value(&compose, "address", (long)res + location_entry->location.breg_op.offset);
447                 break;
448               default:
449                 xbt_dynar_reset(compose);
450                 break;
451               }
452               cursor2++;
453             }
454
455             if(xbt_dynar_length(compose) > 0){
456               frame_pointer_address = xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.address ; 
457             }
458             break;
459           default :
460             frame_pointer_address = NULL;
461             break;
462           }
463         }
464         cursor++;
465       }
466       break;
467     default :
468       frame_pointer_address = NULL;
469       break;
470     }
471
472     frame_found = 0;
473     cursor = 0;
474
475     //XBT_INFO("Frame %s", frame->name);
476
477     xbt_dict_foreach(frame->variables, dict_cursor, variable_name, current_variable){
478       if(current_variable->location != NULL){
479         switch(current_variable->location->type){
480         case e_dw_compose:
481           xbt_dynar_reset(compose);
482           cursor = 0;
483           while(cursor < xbt_dynar_length(current_variable->location->location.compose)){
484             location_entry = xbt_dynar_get_as(current_variable->location->location.compose, cursor, dw_location_t);
485             switch(location_entry->type){
486             case e_dw_register:
487               unw_get_reg(&c, location_entry->location.reg, &res);
488               add_value(&compose, "value", (long)res);
489               break;
490             case e_dw_bregister_op:
491               unw_get_reg(&c, location_entry->location.breg_op.reg, &res);
492               add_value(&compose, "address", (long)res + location_entry->location.breg_op.offset);
493               break;
494             case e_dw_fbregister_op:
495               if(frame_pointer_address != NULL)
496                 add_value(&compose, "address", (long)((char *)frame_pointer_address + location_entry->location.fbreg_op));
497               break;
498             default:
499               xbt_dynar_reset(compose);
500               break;
501             }
502             cursor++;
503           }
504           
505           if(xbt_dynar_length(compose) > 0){
506             if(strcmp(xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->type, "value") == 0){
507               //XBT_INFO("Variable : %s - value : %lx", current_variable->name, xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.res);
508               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));
509             }else{
510               if(*((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1,variable_value_t)->value.address) == NULL){
511                 //XBT_INFO("Variable : %s - address : NULL", current_variable->name);
512                 xbt_strbuff_append(variables, bprintf("%s=NULL\n", current_variable->name));
513               }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)){
514                 //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));
515                 xbt_strbuff_append(variables, bprintf("%s=%zd\n", current_variable->name, (size_t)*((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.address)));
516               }else{
517                 //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));  
518                 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)));
519               }
520             }
521           }else{
522             //XBT_INFO("Variable %s undefined", current_variable->name);
523             xbt_strbuff_append(variables, bprintf("%s=undefined\n", current_variable->name));
524           }
525           break;
526         default :
527           break;
528         }
529       }else{
530         //XBT_INFO("Variable : %s, no location", current_variable->name);
531         xbt_strbuff_append(variables, bprintf("%s=undefined\n", current_variable->name));
532       }
533     }    
534  
535     ret = unw_step(&c);
536
537     //XBT_INFO(" ");
538      
539   }
540
541   //free(stack_name);
542
543   return variables;
544
545 }
546
547 static void print_local_variables_values(xbt_dynar_t all_variables){
548
549   unsigned cursor = 0;
550   mc_snapshot_stack_t stack;
551
552   xbt_dynar_foreach(all_variables, cursor, stack){
553     XBT_INFO("%s", stack->local_variables->data);
554   }
555 }
556
557
558 static void snapshot_stack_free(mc_snapshot_stack_t s){
559   if(s){
560     xbt_free(s->local_variables->data);
561     xbt_free(s->local_variables);
562     xbt_free(s);
563   }
564 }
565
566 void snapshot_stack_free_voidp(void *s){
567   snapshot_stack_free((mc_snapshot_stack_t) * (void **) s);
568 }