Logo AND Algorithmique Numérique Distribuée

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