Logo AND Algorithmique Numérique Distribuée

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