Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Do not waste time calling libunwind get_proc_name in the hot spots
[simgrid.git] / src / mc / mc_checkpoint.c
1 /* Copyright (c) 2008-2013. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #define _GNU_SOURCE
8 #define UNW_LOCAL_ONLY
9
10 #include <string.h>
11 #include <link.h>
12 #include "mc_private.h"
13 #include "xbt/module.h"
14 #include <xbt/mmalloc.h>
15
16 #include "xbt/mmalloc/mmprivate.h"
17
18 #include "../simix/smx_private.h"
19
20 #include <libunwind.h>
21 #include <libelf.h>
22
23 #include "mc_private.h"
24
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
26                                 "Logging specific to mc_checkpoint");
27
28 char *libsimgrid_path;
29
30 /************************************  Free functions **************************************/
31 /*****************************************************************************************/
32
33 static void MC_snapshot_stack_free(mc_snapshot_stack_t s){
34   if(s){
35     xbt_dynar_free(&(s->local_variables));
36     xbt_dynar_free(&(s->stack_frames));
37     xbt_free(s);
38   }
39 }
40
41 static void MC_snapshot_stack_free_voidp(void *s){
42   MC_snapshot_stack_free((mc_snapshot_stack_t) * (void **) s);
43 }
44
45 static void local_variable_free(local_variable_t v){
46   xbt_free(v->frame);
47   xbt_free(v->name);
48   xbt_free(v->type);
49   xbt_free(v);
50 }
51
52 static void local_variable_free_voidp(void *v){
53   local_variable_free((local_variable_t) * (void **) v);
54 }
55
56 static void MC_region_destroy(mc_mem_region_t reg)
57 {
58   xbt_free(reg->data);
59   xbt_free(reg);
60 }
61
62 void MC_free_snapshot(mc_snapshot_t snapshot){
63   unsigned int i;
64   for(i=0; i < NB_REGIONS; i++)
65     MC_region_destroy(snapshot->regions[i]);
66
67   xbt_free(snapshot->stack_sizes);
68   xbt_dynar_free(&(snapshot->stacks));
69   xbt_dynar_free(&(snapshot->to_ignore));
70   xbt_free(snapshot);
71 }
72
73
74 /*******************************  Snapshot regions ********************************/
75 /*********************************************************************************/
76
77 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
78 {
79   mc_mem_region_t new_reg = xbt_new(s_mc_mem_region_t, 1);
80   new_reg->start_addr = start_addr;
81   new_reg->size = size;
82   new_reg->data = xbt_malloc(size);
83   memcpy(new_reg->data, start_addr, size);
84
85   XBT_DEBUG("New region : type : %d, data : %p (real addr %p), size : %zu", type, new_reg->data, start_addr, size);
86   
87   return new_reg;
88 }
89
90 static void MC_region_restore(mc_mem_region_t reg)
91 {
92   /*FIXME: check if start_addr is still mapped, if it is not, then map it
93     before copying the data */
94  
95   memcpy(reg->start_addr, reg->data, reg->size);
96   return;
97 }
98
99 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size)
100 {
101   mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
102   snapshot->regions[type] = new_reg;
103   return;
104
105
106 static void MC_get_memory_regions(mc_snapshot_t snapshot){
107
108   void* start_heap = ((xbt_mheap_t)std_heap)->base;
109   void* end_heap   = ((xbt_mheap_t)std_heap)->breakval;
110   MC_snapshot_add_region(snapshot, 0, start_heap, (char*) end_heap - (char*) start_heap);
111   snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
112
113   MC_snapshot_add_region(snapshot, 1,  mc_libsimgrid_info->start_rw, mc_libsimgrid_info->end_rw - mc_libsimgrid_info->start_rw);
114   MC_snapshot_add_region(snapshot, 2,  mc_binary_info->start_rw, mc_binary_info->end_rw - mc_binary_info->start_rw);
115 }
116
117 /** @brief Finds the range of the different memory segments and binary paths */
118 void MC_init_memory_map_info(){
119  
120   unsigned int i = 0;
121   s_map_region_t reg;
122   memory_map_t maps = MC_get_memory_map();
123
124   maestro_stack_start = NULL;
125   maestro_stack_end = NULL;
126   libsimgrid_path = NULL;
127
128   while (i < maps->mapsize) {
129     reg = maps->regions[i];
130     if (maps->regions[i].pathname == NULL) {
131       // Nothing to do
132     }
133     else if ((reg.prot & PROT_WRITE) && !memcmp(maps->regions[i].pathname, "[stack]", 7)){
134           maestro_stack_start = reg.start_addr;
135           maestro_stack_end = reg.end_addr;
136     } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC) && !memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
137       if(libsimgrid_path == NULL)
138           libsimgrid_path = strdup(maps->regions[i].pathname);
139     }
140     i++;
141   }
142
143   xbt_assert(maestro_stack_start, "maestro_stack_start");
144   xbt_assert(maestro_stack_end, "maestro_stack_end");
145   xbt_assert(libsimgrid_path, "libsimgrid_path&");
146
147   MC_free_memory_map(maps);
148
149 }
150
151 /** \brief Fill/llokup the "subtype" field.
152  */
153 static void MC_resolve_subtype(mc_object_info_t info, dw_type_t type) {
154
155   if(type->dw_type_id==NULL)
156     return;
157   type->subtype = xbt_dict_get_or_null(info->types, type->dw_type_id);
158   if(type->subtype==NULL)
159     return;
160   if(type->subtype->byte_size != 0)
161     return;
162   if(type->subtype->name==NULL)
163     return;
164   // Try to find a more complete description of the type:
165   // We need to fix in order to support C++.
166
167   dw_type_t subtype = xbt_dict_get_or_null(info->types_by_name, type->subtype->name);
168   if(subtype!=NULL) {
169     type->subtype = subtype;
170   }
171
172   // TODO, support "switch type" (looking up the type in another lib) when possible
173 }
174
175 void MC_post_process_types(mc_object_info_t info) {
176   xbt_dict_cursor_t cursor = NULL;
177   char *origin;
178   dw_type_t type;
179
180   // Lookup "subtype" field:
181   xbt_dict_foreach(info->types, cursor, origin, type){
182     MC_resolve_subtype(info, type);
183
184     dw_type_t member;
185     unsigned int i = 0;
186     if(type->members!=NULL) xbt_dynar_foreach(type->members, i, member) {
187       MC_resolve_subtype(info, member);
188     }
189   }
190 }
191
192 /** \brief Fills the position of the .bss and .data sections. */
193 void MC_find_object_address(memory_map_t maps, mc_object_info_t result) {
194
195   unsigned int i = 0;
196   s_map_region_t reg;
197   const char* name = basename(result->file_name);
198   while (i < maps->mapsize) {
199     reg = maps->regions[i];
200     if (maps->regions[i].pathname == NULL || strcmp(basename(maps->regions[i].pathname),  name)) {
201       // Nothing to do
202     }
203     else if ((reg.prot & PROT_WRITE)){
204           xbt_assert(!result->start_rw,
205             "Multiple read-write segments for %s, not supported",
206             maps->regions[i].pathname);
207           result->start_rw = reg.start_addr;
208           result->end_rw   = reg.end_addr;
209           // .bss is usually after the .data:
210           // TODO, use dl_iterate_phdr to be more robust
211           s_map_region_t* next = &(maps->regions[i+1]);
212           if(next->pathname == NULL && (next->prot & PROT_WRITE) && next->start_addr == reg.end_addr) {
213             result->end_rw = maps->regions[i+1].end_addr;
214           }
215     } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)){
216           xbt_assert(!result->start_exec,
217             "Multiple executable segments for %s, not supported",
218             maps->regions[i].pathname);
219           result->start_exec = reg.start_addr;
220           result->end_exec   = reg.end_addr;
221     }
222     else if((reg.prot & PROT_READ) && !(reg.prot & PROT_EXEC)) {
223         xbt_assert(!result->start_ro,
224           "Multiple read only segments for %s, not supported",
225           maps->regions[i].pathname);
226         result->start_ro = reg.start_addr;
227         result->end_ro   = reg.end_addr;
228     }
229     i++;
230   }
231
232   xbt_assert(result->file_name);
233   xbt_assert(result->start_rw);
234   xbt_assert(result->start_exec);
235 }
236
237 /************************************* Take Snapshot ************************************/
238 /****************************************************************************************/
239
240 static xbt_dynar_t MC_get_local_variables_values(xbt_dynar_t stack_frames){
241
242   unsigned cursor1 = 0;
243   mc_stack_frame_t stack_frame;
244   xbt_dynar_t variables = xbt_dynar_new(sizeof(local_variable_t), local_variable_free_voidp);
245
246   xbt_dynar_foreach(stack_frames,cursor1,stack_frame) {
247
248     unsigned cursor2 = 0;
249     dw_variable_t current_variable;
250     xbt_dynar_foreach(stack_frame->frame->variables, cursor2, current_variable){
251       
252       int region_type;
253       if((long)stack_frame->ip > (long)mc_libsimgrid_info->start_exec)
254         region_type = 1;
255       else
256         region_type = 2;
257
258       local_variable_t new_var = xbt_new0(s_local_variable_t, 1);
259       new_var->frame = xbt_strdup(stack_frame->frame_name);
260       new_var->ip = stack_frame->ip;
261       new_var->name = xbt_strdup(current_variable->name);
262       new_var->type = strdup(current_variable->type_origin);
263       new_var->region= region_type;
264       
265       /* if(current_variable->address!=NULL) {
266         new_var->address = current_variable->address;
267       } else */
268       if(current_variable->location != NULL){
269         new_var->address = (void*) MC_dwarf_resolve_location(
270           &(stack_frame->unw_cursor), current_variable->location, (void*)stack_frame->frame_base);
271       }
272
273       xbt_dynar_push(variables, &new_var);
274
275     }
276   }
277
278   return variables;
279
280 }
281
282 static void MC_stack_frame_free_voipd(void *s){
283   mc_stack_frame_t stack_frame = *(mc_stack_frame_t*)s;
284   if(stack_frame) {
285     xbt_free(stack_frame->frame_name);
286     xbt_free(stack_frame);
287   }
288 }
289
290 static xbt_dynar_t MC_unwind_stack_frames(void *stack_context) {
291   xbt_dynar_t result = xbt_dynar_new(sizeof(mc_stack_frame_t), MC_stack_frame_free_voipd);
292
293   unw_cursor_t c;
294
295   dw_frame_t test = MC_find_function_by_ip(&MC_unwind_stack_frames);
296   xbt_assert(test);
297
298   int ret;
299   for(ret = unw_init_local(&c, (unw_context_t *)stack_context); ret >= 0; ret = unw_step(&c)){
300     mc_stack_frame_t stack_frame = xbt_new(s_mc_stack_frame_t, 1);
301     xbt_dynar_push(result, &stack_frame);
302
303     stack_frame->unw_cursor = c;
304
305     unw_word_t ip, sp;
306
307     unw_get_reg(&c, UNW_REG_IP, &ip);
308     unw_get_reg(&c, UNW_REG_SP, &sp);
309
310     stack_frame->ip = ip;
311     stack_frame->sp = sp;
312
313     // TODO, use real addresses in frame_t instead of fixing it here
314
315     dw_frame_t frame = MC_find_function_by_ip((void*) ip);
316     stack_frame->frame = frame;
317
318     if(frame) {
319       stack_frame->frame_name = xbt_strdup(frame->name);
320
321       mc_object_info_t info = MC_ip_find_object_info((void*)ip);
322
323       // This is the instruction pointer as present in the DWARF of the object:
324       // Relocated addresses are offset for shared objets and constant for executables objects.
325       // See DWARF4 spec 7.5
326       unw_word_t normalized_ip;
327       if(info->flags & MC_OBJECT_INFO_EXECUTABLE) {
328         normalized_ip = ip;
329       } else {
330         void* base = MC_object_base_address(info);
331         normalized_ip = (unw_word_t) ip - (unw_word_t) base;
332       }
333
334       stack_frame->frame_base = (unw_word_t)mc_find_frame_base(normalized_ip, frame, &c);
335     } else {
336       stack_frame->frame_base = 0;
337     }
338
339     /* Stop before context switch with maestro */
340     if(frame!=NULL && frame->name!=NULL && !strcmp(frame->name, "smx_ctx_sysv_wrapper"))
341       break;
342   }
343
344   if(xbt_dynar_length(result) == 0){
345     XBT_INFO("unw_init_local failed");
346     xbt_abort();
347   }
348
349   return result;
350 };
351
352 static xbt_dynar_t MC_take_snapshot_stacks(mc_snapshot_t *snapshot, void *heap){
353
354   xbt_dynar_t res = xbt_dynar_new(sizeof(s_mc_snapshot_stack_t), MC_snapshot_stack_free_voidp);
355
356   unsigned int cursor = 0;
357   stack_region_t current_stack;
358   
359   xbt_dynar_foreach(stacks_areas, cursor, current_stack){
360     mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
361     st->stack_frames = MC_unwind_stack_frames(current_stack->context);
362     st->local_variables = MC_get_local_variables_values(st->stack_frames);
363
364     unw_word_t sp = xbt_dynar_get_as(st->stack_frames, 0, mc_stack_frame_t)->sp;
365     st->stack_pointer = ((char *)heap + (size_t)(((char *)((long)sp) - (char*)std_heap)));
366
367     st->real_address = current_stack->address;
368     xbt_dynar_push(res, &st);
369     (*snapshot)->stack_sizes = xbt_realloc((*snapshot)->stack_sizes, (cursor + 1) * sizeof(size_t));
370     (*snapshot)->stack_sizes[cursor] = current_stack->size - ((char *)st->stack_pointer - (char *)((char *)heap + ((char *)current_stack->address - (char *)std_heap)));
371   }
372
373   return res;
374
375 }
376
377 static xbt_dynar_t MC_take_snapshot_ignore(){
378   
379   if(mc_heap_comparison_ignore == NULL)
380     return NULL;
381
382   xbt_dynar_t cpy = xbt_dynar_new(sizeof(mc_heap_ignore_region_t), heap_ignore_region_free_voidp);
383
384   unsigned int cursor = 0;
385   mc_heap_ignore_region_t current_region;
386
387   xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, current_region){
388     mc_heap_ignore_region_t new_region = NULL;
389     new_region = xbt_new0(s_mc_heap_ignore_region_t, 1);
390     new_region->address = current_region->address;
391     new_region->size = current_region->size;
392     new_region->block = current_region->block;
393     new_region->fragment = current_region->fragment;
394     xbt_dynar_push(cpy, &new_region);
395   }
396
397   return cpy;
398
399 }
400
401 static void MC_dump_checkpoint_ignore(mc_snapshot_t snapshot){
402   
403   unsigned int cursor = 0;
404   mc_checkpoint_ignore_region_t region;
405   size_t offset;
406   
407   xbt_dynar_foreach(mc_checkpoint_ignore, cursor, region){
408     if(region->addr > snapshot->regions[0]->start_addr && (char *)(region->addr) < (char *)snapshot->regions[0]->start_addr + STD_HEAP_SIZE){
409       offset = (char *)region->addr - (char *)snapshot->regions[0]->start_addr;
410       memset((char *)snapshot->regions[0]->data + offset, 0, region->size);
411     }else if(region->addr > snapshot->regions[2]->start_addr && (char *)(region->addr) < (char*)snapshot->regions[2]->start_addr + snapshot->regions[2]->size){
412       offset = (char *)region->addr - (char *)snapshot->regions[2]->start_addr;
413       memset((char *)snapshot->regions[2]->data + offset, 0, region->size);
414     }else if(region->addr > snapshot->regions[1]->start_addr && (char *)(region->addr) < (char*)snapshot->regions[1]->start_addr + snapshot->regions[1]->size){
415       offset = (char *)region->addr - (char *)snapshot->regions[1]->start_addr;
416       memset((char *)snapshot->regions[1]->data + offset, 0, region->size);
417     }
418   }
419
420 }
421
422
423 mc_snapshot_t MC_take_snapshot(int num_state){
424
425   mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
426   snapshot->nb_processes = xbt_swag_size(simix_global->process_list);
427
428   /* Save the std heap and the writable mapped pages of libsimgrid and binary */
429   MC_get_memory_regions(snapshot);
430
431   snapshot->to_ignore = MC_take_snapshot_ignore();
432
433   if(_sg_mc_visited > 0 || strcmp(_sg_mc_property_file,"")){
434     snapshot->stacks = MC_take_snapshot_stacks(&snapshot, snapshot->regions[0]->data);
435     if(_sg_mc_hash && snapshot->stacks!=NULL) {
436       snapshot->hash = mc_hash_processes_state(num_state, snapshot->stacks);
437     } else {
438       snapshot->hash = 0;
439     }
440   }
441   else {
442     snapshot->hash = 0;
443   }
444
445   if(num_state > 0)
446     MC_dump_checkpoint_ignore(snapshot);
447
448   return snapshot;
449
450 }
451
452 void MC_restore_snapshot(mc_snapshot_t snapshot){
453   unsigned int i;
454   for(i=0; i < NB_REGIONS; i++){
455     MC_region_restore(snapshot->regions[i]);
456   }
457
458 }
459
460 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall){
461   return MC_take_snapshot(1);
462 }
463
464 void *MC_snapshot(void){
465   return simcall_mc_snapshot();
466 }