Logo AND Algorithmique Numérique Distribuée

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