Logo AND Algorithmique Numérique Distribuée

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