Logo AND Algorithmique Numérique Distribuée

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