Logo AND Algorithmique Numérique Distribuée

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