Logo AND Algorithmique Numérique Distribuée

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