Logo AND Algorithmique Numérique Distribuée

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