Logo AND Algorithmique Numérique Distribuée

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