Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Restructure config headers
[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   size_t size = 0;
158   void *start = NULL;
159
160   /* Save the std heap and the writable mapped pages of libsimgrid */
161   while (i < maps->mapsize) {
162     reg = maps->regions[i];
163     if ((reg.prot & PROT_WRITE)){
164       if (maps->regions[i].pathname == NULL){
165         if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
166           MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
167           heap = snapshot->regions[nb_reg]->data;
168           nb_reg++;
169         }else if(reg.start_addr == raw_heap){
170           end_raw_heap = reg.end_addr;
171         }
172         i++;
173       } else {
174         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
175           size = (char*)reg.end_addr - (char*)reg.start_addr;
176           start = reg.start_addr;
177           nb_reg++;
178           i++;
179           reg = maps->regions[i];
180           if(reg.pathname == NULL && (reg.prot & PROT_WRITE) && i < maps->mapsize){
181             size += (char*)reg.end_addr - (char*)reg.start_addr;
182             reg = maps->regions[i];
183             i++;
184             nb_reg++;
185           }
186           MC_snapshot_add_region(snapshot, 1, start, size);
187         } else {
188           if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
189             MC_snapshot_add_region(snapshot, 2, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
190             nb_reg++;
191             i++;
192             reg = maps->regions[i];
193             if(reg.pathname == NULL && (reg.prot & PROT_WRITE) && reg.start_addr != std_heap && reg.start_addr != raw_heap && i < maps->mapsize){
194               MC_snapshot_add_region(snapshot, 2, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
195               reg = maps->regions[i];
196               nb_reg++;
197             }
198           }else{
199             i++;
200           }
201         }
202       }
203     }else{
204       i++;
205     }
206   }
207
208   if(_sg_mc_visited > 0 || strcmp(_sg_mc_property_file,""))
209     snapshot->stacks = take_snapshot_stacks(heap);
210   
211   free_memory_map(maps);
212
213   MC_UNSET_RAW_MEM;
214
215   if(raw_mem)
216     MC_SET_RAW_MEM;
217
218   return snapshot;
219
220 }
221
222 void MC_restore_snapshot(mc_snapshot_t snapshot)
223 {
224   unsigned int i;
225   for(i=0; i < snapshot->num_reg; i++){
226     MC_region_restore(snapshot->regions[i]);
227   }
228
229 }
230
231 void MC_free_snapshot(mc_snapshot_t snapshot)
232 {
233   unsigned int i;
234   for(i=0; i < snapshot->num_reg; i++)
235     MC_region_destroy(snapshot->regions[i]);
236
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   free(command);
302   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   free(command);
365   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(void *heap){
382
383   xbt_dynar_t res = xbt_dynar_new(sizeof(s_mc_snapshot_stack_t), NULL);
384
385   unsigned int cursor1 = 0;
386   stack_region_t current_stack;
387   
388   xbt_dynar_foreach(stacks_areas, cursor1, 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   }
394
395   return res;
396
397 }
398
399 static void *get_stack_pointer(void *stack_context, void *heap){
400
401   unw_cursor_t c;
402   int ret;
403   unw_word_t sp;
404
405   ret = unw_init_local(&c, (unw_context_t *)stack_context);
406   if(ret < 0){
407     XBT_INFO("unw_init_local failed");
408     xbt_abort();
409   }
410
411   unw_get_reg(&c, UNW_REG_SP, &sp);
412
413   return ((char *)heap + (size_t)(((char *)((long)sp) - (char*)std_heap)));
414
415 }
416
417 static xbt_strbuff_t get_local_variables_values(void *stack_context, void *heap){
418   
419   unw_cursor_t c;
420   int ret;
421
422   char frame_name[256];
423   
424   ret = unw_init_local(&c, (unw_context_t *)stack_context);
425   if(ret < 0){
426     XBT_INFO("unw_init_local failed");
427     xbt_abort();
428   }
429
430   unw_word_t ip, sp, off;
431   dw_frame_t frame;
432  
433   xbt_dynar_t compose = xbt_dynar_new(sizeof(variable_value_t), NULL);
434
435   xbt_strbuff_t variables = xbt_strbuff_new();
436   xbt_dict_cursor_t dict_cursor;
437   char *variable_name;
438   dw_local_variable_t current_variable;
439   unsigned int cursor = 0, cursor2 = 0;
440   dw_location_entry_t entry = NULL;
441   dw_location_t location_entry = NULL;
442   unw_word_t res;
443   int frame_found = 0;
444   void *frame_pointer_address = NULL;
445   long true_ip;
446
447   while(ret >= 0){
448
449     unw_get_reg(&c, UNW_REG_IP, &ip);
450     unw_get_reg(&c, UNW_REG_SP, &sp);
451
452     unw_get_proc_name (&c, frame_name, sizeof (frame_name), &off);
453
454     xbt_strbuff_append(variables, bprintf("ip=%s\n", frame_name));
455
456     frame = xbt_dict_get_or_null(mc_local_variables, frame_name);
457
458     if(frame == NULL){
459       ret = unw_step(&c);
460       continue;
461     }
462
463     true_ip = (long)frame->low_pc + (long)off;
464
465     /* Get frame pointer */
466     switch(frame->frame_base->type){
467     case e_dw_loclist:
468       while((cursor < xbt_dynar_length(frame->frame_base->location.loclist)) && frame_found == 0){
469         entry = xbt_dynar_get_as(frame->frame_base->location.loclist, cursor, dw_location_entry_t);
470         if((true_ip >= entry->lowpc) && (true_ip < entry->highpc)){
471           frame_found = 1;
472           switch(entry->location->type){
473           case e_dw_compose:
474             xbt_dynar_reset(compose);
475             cursor2 = 0;
476             while(cursor2 < xbt_dynar_length(entry->location->location.compose)){
477               location_entry = xbt_dynar_get_as(entry->location->location.compose, cursor2, dw_location_t);
478               switch(location_entry->type){
479               case e_dw_register:
480                 unw_get_reg(&c, location_entry->location.reg, &res);
481                 add_value(&compose, "address", (long)res);
482                 break;
483               case e_dw_bregister_op:
484                 unw_get_reg(&c, location_entry->location.breg_op.reg, &res);
485                 add_value(&compose, "address", (long)res + location_entry->location.breg_op.offset);
486                 break;
487               default:
488                 xbt_dynar_reset(compose);
489                 break;
490               }
491               cursor2++;
492             }
493
494             if(xbt_dynar_length(compose) > 0){
495               frame_pointer_address = xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.address ; 
496             }
497             break;
498           default :
499             frame_pointer_address = NULL;
500             break;
501           }
502         }
503         cursor++;
504       }
505       break;
506     default :
507       frame_pointer_address = NULL;
508       break;
509     }
510
511     frame_found = 0;
512     cursor = 0;
513
514     xbt_dict_foreach(frame->variables, dict_cursor, variable_name, current_variable){
515       if(current_variable->location != NULL){
516         switch(current_variable->location->type){
517         case e_dw_compose:
518           xbt_dynar_reset(compose);
519           cursor = 0;
520           while(cursor < xbt_dynar_length(current_variable->location->location.compose)){
521             location_entry = xbt_dynar_get_as(current_variable->location->location.compose, cursor, dw_location_t);
522             switch(location_entry->type){
523             case e_dw_register:
524               unw_get_reg(&c, location_entry->location.reg, &res);
525               add_value(&compose, "value", (long)res);
526               break;
527             case e_dw_bregister_op:
528               unw_get_reg(&c, location_entry->location.breg_op.reg, &res);
529               add_value(&compose, "address", (long)res + location_entry->location.breg_op.offset);
530               break;
531             case e_dw_fbregister_op:
532               if(frame_pointer_address != NULL)
533                 add_value(&compose, "address", (long)((char *)frame_pointer_address + location_entry->location.fbreg_op));
534               break;
535             default:
536               xbt_dynar_reset(compose);
537               break;
538             }
539             cursor++;
540           }
541           
542           if(xbt_dynar_length(compose) > 0){
543             if(strcmp(xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->type, "value") == 0){
544               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));
545             }else{
546               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){
547                 xbt_strbuff_append(variables, bprintf("%s=NULL\n", current_variable->name));
548               }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)){
549                 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)));
550               }else{ 
551                 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)));
552               }
553             }
554           }else{
555             xbt_strbuff_append(variables, bprintf("%s=undefined\n", current_variable->name));
556           }
557           break;
558         default :
559           break;
560         }
561       }else{
562         xbt_strbuff_append(variables, bprintf("%s=undefined\n", current_variable->name));
563       }
564     }    
565  
566     ret = unw_step(&c);
567      
568   }
569
570   return variables;
571
572 }
573
574 static void print_local_variables_values(xbt_dynar_t all_variables){
575
576   unsigned cursor = 0;
577   mc_snapshot_stack_t stack;
578
579   xbt_dynar_foreach(all_variables, cursor, stack){
580     XBT_INFO("%s", stack->local_variables->data);
581   }
582 }
583
584
585 static void snapshot_stack_free(mc_snapshot_stack_t s){
586   if(s){
587     xbt_free(s->local_variables->data);
588     xbt_free(s->local_variables);
589     xbt_free(s);
590   }
591 }
592
593 void snapshot_stack_free_voidp(void *s){
594   snapshot_stack_free((mc_snapshot_stack_t) * (void **) s);
595 }
596
597 void *MC_snapshot(void){
598
599   return simcall_mc_snapshot();
600   
601 }