1 /* Copyright (c) 2008-2013. The SimGrid Team.
2 * All rights reserved. */
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. */
10 #include "mc_private.h"
11 #include "xbt/module.h"
12 #include <xbt/mmalloc.h>
14 #include "../simix/smx_private.h"
16 #include <libunwind.h>
19 #include "mc_private.h"
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
22 "Logging specific to mc_checkpoint");
24 char *libsimgrid_path;
26 static void MC_find_object_address(memory_map_t maps, mc_object_info_t result);
28 /************************************ Free functions **************************************/
29 /*****************************************************************************************/
31 static void MC_snapshot_stack_free(mc_snapshot_stack_t s){
33 xbt_dynar_free(&(s->local_variables));
38 static void MC_snapshot_stack_free_voidp(void *s){
39 MC_snapshot_stack_free((mc_snapshot_stack_t) * (void **) s);
42 static void local_variable_free(local_variable_t v){
49 static void local_variable_free_voidp(void *v){
50 local_variable_free((local_variable_t) * (void **) v);
53 static void MC_region_destroy(mc_mem_region_t reg)
59 void MC_free_snapshot(mc_snapshot_t snapshot){
61 for(i=0; i < NB_REGIONS; i++)
62 MC_region_destroy(snapshot->regions[i]);
64 xbt_free(snapshot->stack_sizes);
65 xbt_dynar_free(&(snapshot->stacks));
66 xbt_dynar_free(&(snapshot->to_ignore));
71 /******************************* Snapshot regions ********************************/
72 /*********************************************************************************/
74 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
76 mc_mem_region_t new_reg = xbt_new(s_mc_mem_region_t, 1);
77 new_reg->start_addr = start_addr;
79 new_reg->data = xbt_malloc(size);
80 memcpy(new_reg->data, start_addr, size);
82 XBT_DEBUG("New region : type : %d, data : %p (real addr %p), size : %zu", type, new_reg->data, start_addr, size);
87 static void MC_region_restore(mc_mem_region_t reg)
89 /*FIXME: check if start_addr is still mapped, if it is not, then map it
90 before copying the data */
92 memcpy(reg->start_addr, reg->data, reg->size);
96 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size)
98 mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
99 snapshot->regions[type] = new_reg;
103 static void MC_get_memory_regions(mc_snapshot_t snapshot){
110 char *lfields[6] = {0}, *tok;
111 void *start_addr, *start_addr1, *end_addr;
115 fp = fopen("/proc/self/maps", "r");
118 "Cannot open /proc/self/maps to investigate the memory map of the process. Please report this bug.");
122 while((read = xbt_getline(&line, &n, fp)) != -1){
124 /* Wipeout the new line character */
125 line[read - 1] = '\0';
127 /* Tokenize the line using spaces as delimiters and store each token */
128 lfields[0] = strtok(line, " ");
130 for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
131 lfields[i] = strtok(NULL, " ");
134 /* First get the permissions flags, need write permission */
135 if(lfields[1][1] == 'w'){
137 /* Get the start address of the map */
138 tok = strtok(lfields[0], "-");
139 start_addr = (void *)strtoul(tok, NULL, 16);
141 if(start_addr == std_heap){ /* Std_heap ? */
142 tok = strtok(NULL, "-");
143 end_addr = (void *)strtoul(tok, NULL, 16);
144 MC_snapshot_add_region(snapshot, 0, start_addr, (char*)end_addr - (char*)start_addr);
145 snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
146 }else{ /* map name == libsimgrid || binary_name ? */
147 if(lfields[5] != NULL){
148 if(!memcmp(basename(lfields[5]), "libsimgrid", 10)){
149 tok = strtok(NULL, "-");
150 end_addr = (void *)strtoul(tok, NULL, 16);
151 size = (char*)end_addr - (char*)start_addr;
152 /* BSS and data segments may be separated according to the OS */
153 if((read = xbt_getline(&line, &n, fp)) != -1){
154 line[read - 1] = '\0';
155 lfields[0] = strtok(line, " ");
156 for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
157 lfields[i] = strtok(NULL, " ");
159 if(lfields[1][1] == 'w' && lfields[5] == NULL){
160 tok = strtok(lfields[0], "-");
161 start_addr1 = (void *)strtoul(tok, NULL, 16);
162 tok = strtok(NULL, "-");
163 size += (char *)(void *)strtoul(tok, NULL, 16) - (char*)start_addr1;
166 MC_snapshot_add_region(snapshot, 1, start_addr, size);
167 }else if(!memcmp(basename(lfields[5]), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
168 tok = strtok(NULL, "-");
169 end_addr = (void *)strtoul(tok, NULL, 16);
170 size = (char*)end_addr - (char*)start_addr;
171 /* BSS and data segments may be separated according to the OS */
172 if((read = xbt_getline(&line, &n, fp)) != -1){
173 line[read - 1] = '\0';
174 lfields[0] = strtok(line, " ");
175 for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
176 lfields[i] = strtok(NULL, " ");
178 tok = strtok(lfields[0], "-");
179 start_addr1 = (void *)strtoul(tok, NULL, 16);
180 if(lfields[1][1] == 'w'){
181 if(start_addr1 == std_heap){ /* Std_heap ? */
182 tok = strtok(NULL, "-");
183 end_addr = (void *)strtoul(tok, NULL, 16);
184 MC_snapshot_add_region(snapshot, 0, start_addr1, (char*)end_addr - (char*)start_addr1);
185 snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
186 }else if(start_addr1 != raw_heap){
187 tok = strtok(NULL, "-");
188 size += (char *)(void *)strtoul(tok, NULL, 16) - (char *)start_addr1;
192 MC_snapshot_add_region(snapshot, 2, start_addr, size);
193 }else if (!memcmp(lfields[5], "[stack]", 7)){
194 maestro_stack_start = start_addr;
195 tok = strtok(NULL, "-");
196 maestro_stack_end = (void *)strtoul(tok, NULL, 16);
209 /** @brief Finds the range of the different memory segments and binary paths */
210 void MC_init_memory_map_info(){
214 memory_map_t maps = MC_get_memory_map();
216 maestro_stack_start = NULL;
217 maestro_stack_end = NULL;
218 libsimgrid_path = NULL;
220 while (i < maps->mapsize) {
221 reg = maps->regions[i];
222 if (maps->regions[i].pathname == NULL) {
225 else if ((reg.prot & PROT_WRITE) && !memcmp(maps->regions[i].pathname, "[stack]", 7)){
226 maestro_stack_start = reg.start_addr;
227 maestro_stack_end = reg.end_addr;
228 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC) && !memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
229 if(libsimgrid_path == NULL)
230 libsimgrid_path = strdup(maps->regions[i].pathname);
235 xbt_assert(maestro_stack_start, "maestro_stack_start");
236 xbt_assert(maestro_stack_end, "maestro_stack_end");
237 xbt_assert(libsimgrid_path, "libsimgrid_path&");
239 MC_free_memory_map(maps);
243 /** \brief Fill/llokup the "subtype" field.
245 static void MC_resolve_subtype(mc_object_info_t info, dw_type_t type) {
247 if(type->dw_type_id==NULL)
249 type->subtype = xbt_dict_get_or_null(info->types, type->dw_type_id);
250 if(type->subtype==NULL)
252 if(type->subtype->byte_size != 0)
254 if(type->subtype->name==NULL)
256 // Try to find a more complete description of the type:
257 // We need to fix in order to support C++.
259 dw_type_t subtype = xbt_dict_get_or_null(info->types_by_name, type->subtype->name);
261 type->subtype = subtype;
264 // TODO, support "switch type" (looking up the type in another lib) when possible
267 static void MC_post_process_types(mc_object_info_t info) {
268 xbt_dict_cursor_t cursor = NULL;
272 // Lookup "subtype" field:
273 xbt_dict_foreach(info->types, cursor, origin, type){
274 MC_resolve_subtype(info, type);
278 if(type->members!=NULL) xbt_dynar_foreach(type->members, i, member) {
279 MC_resolve_subtype(info, member);
284 /** \brief Finds informations about a given shared object/executable */
285 mc_object_info_t MC_find_object_info(memory_map_t maps, char* name) {
286 mc_object_info_t result = MC_new_object_info();
287 result->file_name = xbt_strdup(name);
288 MC_find_object_address(maps, result);
289 MC_dwarf_get_variables(result);
290 MC_post_process_types(result);
294 /** \brief Fills the position of the .bss and .data sections. */
295 static void MC_find_object_address(memory_map_t maps, mc_object_info_t result) {
299 const char* name = basename(result->file_name);
300 while (i < maps->mapsize) {
301 reg = maps->regions[i];
302 if (maps->regions[i].pathname == NULL || strcmp(basename(maps->regions[i].pathname), name)) {
305 else if ((reg.prot & PROT_WRITE)){
306 xbt_assert(!result->start_rw,
307 "Multiple read-write segments for %s, not supported",
308 maps->regions[i].pathname);
309 result->start_rw = reg.start_addr;
310 result->end_rw = reg.end_addr;
311 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)){
312 xbt_assert(!result->start_exec,
313 "Multiple executable segments for %s, not supported",
314 maps->regions[i].pathname);
315 result->start_exec = reg.start_addr;
316 result->end_exec = reg.end_addr;
318 else if((reg.prot & PROT_READ) && !(reg.prot & PROT_EXEC)) {
319 xbt_assert(!result->start_ro,
320 "Multiple read only segments for %s, not supported",
321 maps->regions[i].pathname);
322 result->start_ro = reg.start_addr;
323 result->end_ro = reg.end_addr;
328 xbt_assert(result->file_name);
329 xbt_assert(result->start_rw);
330 xbt_assert(result->start_exec);
333 /************************************* Take Snapshot ************************************/
334 /****************************************************************************************/
336 static void MC_get_hash_global(char *snapshot_hash, void *data1, void *data2){
338 /* unsigned int cursor = 0; */
340 /* global_variable_t current_var; */
341 /* void *addr_pointed = NULL; */
342 /* void *res = NULL; */
344 /* xbt_strbuff_t clear = xbt_strbuff_new(); */
346 /* xbt_dynar_foreach(mc_global_variables, cursor, current_var){ */
347 /* if(current_var->address < start_data_libsimgrid){ /\* binary *\/ */
348 /* offset = (char *)current_var->address - (char *)start_data_binary; */
349 /* addr_pointed = *((void **)((char *)data2 + offset)); */
350 /* if(((addr_pointed >= start_plt_binary && addr_pointed <= end_plt_binary)) || ((addr_pointed >= std_heap && (char *)addr_pointed <= (char *)std_heap + STD_HEAP_SIZE ))) */
352 /* res = xbt_malloc0(current_var->size + 1); */
353 /* memset(res, 0, current_var->size + 1); */
354 /* memcpy(res, (char*)data2 + offset, current_var->size); */
355 /* }else{ /\* libsimgrid *\/ */
356 /* offset = (char *)current_var->address - (char *)start_data_libsimgrid; */
357 /* addr_pointed = *((void **)((char *)data1 + offset)); */
358 /* if((addr_pointed >= start_plt_libsimgrid && addr_pointed <= end_plt_libsimgrid) || (addr_pointed >= std_heap && (char *)addr_pointed <= (char *)std_heap + STD_HEAP_SIZE )) */
360 /* res = xbt_malloc0(current_var->size + 1); */
361 /* memset(res, 0, current_var->size + 1); */
362 /* memcpy(res, (char*)data1 + offset, current_var->size); */
364 /* if(res != NULL){ */
365 /* xbt_strbuff_append(clear, (const char*)res); */
371 /* xbt_sha(clear->data, snapshot_hash); */
373 /* xbt_strbuff_free(clear); */
377 static void MC_get_hash_local(char *snapshot_hash, xbt_dynar_t stacks){
379 /* xbt_dynar_t tokens = NULL, s_tokens = NULL; */
380 /* unsigned int cursor1 = 0, cursor2 = 0; */
381 /* mc_snapshot_stack_t current_stack; */
382 /* char *frame_name = NULL; */
385 /* xbt_strbuff_t clear = xbt_strbuff_new(); */
387 /* while(cursor1 < xbt_dynar_length(stacks)){ */
388 /* current_stack = xbt_dynar_get_as(stacks, cursor1, mc_snapshot_stack_t); */
389 /* tokens = xbt_str_split(current_stack->local_variables->data, NULL); */
391 /* while(cursor2 < xbt_dynar_length(tokens)){ */
392 /* s_tokens = xbt_str_split(xbt_dynar_get_as(tokens, cursor2, char *), "="); */
393 /* if(xbt_dynar_length(s_tokens) > 1){ */
394 /* if(strcmp(xbt_dynar_get_as(s_tokens, 0, char *), "frame_name") == 0){ */
395 /* xbt_free(frame_name); */
396 /* frame_name = xbt_strdup(xbt_dynar_get_as(s_tokens, 1, char *)); */
397 /* xbt_strbuff_append(clear, (const char*)xbt_dynar_get_as(tokens, cursor2, char *)); */
399 /* xbt_dynar_free(&s_tokens); */
402 /* addr = (void *) strtoul(xbt_dynar_get_as(s_tokens, 1, char *), NULL, 16); */
403 /* if(addr > std_heap && (char *)addr <= (char *)std_heap + STD_HEAP_SIZE){ */
405 /* xbt_dynar_free(&s_tokens); */
408 /* if(is_stack_ignore_variable(frame_name, xbt_dynar_get_as(s_tokens, 0, char *))){ */
410 /* xbt_dynar_free(&s_tokens); */
413 /* xbt_strbuff_append(clear, (const char *)xbt_dynar_get_as(tokens, cursor2, char *)); */
415 /* xbt_dynar_free(&s_tokens); */
418 /* xbt_dynar_free(&tokens); */
422 /* xbt_free(frame_name); */
424 /* xbt_sha(clear->data, snapshot_hash); */
426 /* xbt_strbuff_free(clear); */
432 static xbt_dynar_t MC_get_local_variables_values(void *stack_context){
437 char frame_name[256];
439 ret = unw_init_local(&c, (unw_context_t *)stack_context);
441 XBT_INFO("unw_init_local failed");
445 unw_word_t ip, sp, off;
448 unsigned int cursor = 0;
449 dw_variable_t current_variable;
451 void *frame_pointer_address = NULL;
455 xbt_dynar_t variables = xbt_dynar_new(sizeof(local_variable_t), local_variable_free_voidp);
457 while(ret >= 0 && !stop){
459 unw_get_reg(&c, UNW_REG_IP, &ip);
460 unw_get_reg(&c, UNW_REG_SP, &sp);
462 unw_get_proc_name(&c, frame_name, sizeof (frame_name), &off);
464 if(!strcmp(frame_name, "smx_ctx_sysv_wrapper")) /* Stop before context switch with maestro */
467 if((long)ip > (long) mc_libsimgrid_info->start_exec)
468 frame = xbt_dict_get_or_null(mc_libsimgrid_info->local_variables, frame_name);
470 frame = xbt_dict_get_or_null(mc_binary_info->local_variables, frame_name);
477 true_ip = (long)frame->low_pc + (long)off;
478 frame_pointer_address = mc_find_frame_base(true_ip, frame, &c);
482 xbt_dynar_foreach(frame->variables, cursor, current_variable){
484 if((long)ip > (long)mc_libsimgrid_info->start_exec)
489 local_variable_t new_var = xbt_new0(s_local_variable_t, 1);
490 new_var->frame = xbt_strdup(frame_name);
491 new_var->ip = (unsigned long)ip;
492 new_var->name = xbt_strdup(current_variable->name);
493 new_var->type = strdup(current_variable->type_origin);
494 new_var->region= region_type;
496 if(current_variable->address.location != NULL){
497 new_var->address = (void*) MC_dwarf_resolve_location(&c, current_variable->address.location, frame_pointer_address);
500 xbt_dynar_push(variables, &new_var);
513 static void *MC_get_stack_pointer(void *stack_context, void *heap){
519 ret = unw_init_local(&c, (unw_context_t *)stack_context);
521 XBT_INFO("unw_init_local failed");
525 unw_get_reg(&c, UNW_REG_SP, &sp);
527 return ((char *)heap + (size_t)(((char *)((long)sp) - (char*)std_heap)));
531 static xbt_dynar_t MC_take_snapshot_stacks(mc_snapshot_t *snapshot, void *heap){
533 xbt_dynar_t res = xbt_dynar_new(sizeof(s_mc_snapshot_stack_t), MC_snapshot_stack_free_voidp);
535 unsigned int cursor = 0;
536 stack_region_t current_stack;
538 xbt_dynar_foreach(stacks_areas, cursor, current_stack){
539 mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
540 st->local_variables = MC_get_local_variables_values(current_stack->context);
541 st->stack_pointer = MC_get_stack_pointer(current_stack->context, heap);
542 st->real_address = current_stack->address;
543 xbt_dynar_push(res, &st);
544 (*snapshot)->stack_sizes = xbt_realloc((*snapshot)->stack_sizes, (cursor + 1) * sizeof(size_t));
545 (*snapshot)->stack_sizes[cursor] = current_stack->size - ((char *)st->stack_pointer - (char *)((char *)heap + ((char *)current_stack->address - (char *)std_heap)));
552 static xbt_dynar_t MC_take_snapshot_ignore(){
554 if(mc_heap_comparison_ignore == NULL)
557 xbt_dynar_t cpy = xbt_dynar_new(sizeof(mc_heap_ignore_region_t), heap_ignore_region_free_voidp);
559 unsigned int cursor = 0;
560 mc_heap_ignore_region_t current_region;
562 xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, current_region){
563 mc_heap_ignore_region_t new_region = NULL;
564 new_region = xbt_new0(s_mc_heap_ignore_region_t, 1);
565 new_region->address = current_region->address;
566 new_region->size = current_region->size;
567 new_region->block = current_region->block;
568 new_region->fragment = current_region->fragment;
569 xbt_dynar_push(cpy, &new_region);
576 static void MC_dump_checkpoint_ignore(mc_snapshot_t snapshot){
578 unsigned int cursor = 0;
579 mc_checkpoint_ignore_region_t region;
582 xbt_dynar_foreach(mc_checkpoint_ignore, cursor, region){
583 if(region->addr > snapshot->regions[0]->start_addr && (char *)(region->addr) < (char *)snapshot->regions[0]->start_addr + STD_HEAP_SIZE){
584 offset = (char *)region->addr - (char *)snapshot->regions[0]->start_addr;
585 memset((char *)snapshot->regions[0]->data + offset, 0, region->size);
586 }else if(region->addr > snapshot->regions[2]->start_addr && (char *)(region->addr) < (char*)snapshot->regions[2]->start_addr + snapshot->regions[2]->size){
587 offset = (char *)region->addr - (char *)snapshot->regions[2]->start_addr;
588 memset((char *)snapshot->regions[2]->data + offset, 0, region->size);
589 }else if(region->addr > snapshot->regions[1]->start_addr && (char *)(region->addr) < (char*)snapshot->regions[1]->start_addr + snapshot->regions[1]->size){
590 offset = (char *)region->addr - (char *)snapshot->regions[1]->start_addr;
591 memset((char *)snapshot->regions[1]->data + offset, 0, region->size);
598 mc_snapshot_t MC_take_snapshot(int num_state){
600 mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
601 snapshot->nb_processes = xbt_swag_size(simix_global->process_list);
602 if(MC_USE_SNAPSHOT_HASH) {
603 snapshot->hash = mc_hash_processes_state(num_state);
608 /* Save the std heap and the writable mapped pages of libsimgrid and binary */
609 MC_get_memory_regions(snapshot);
611 snapshot->to_ignore = MC_take_snapshot_ignore();
613 if(_sg_mc_visited > 0 || strcmp(_sg_mc_property_file,"")){
614 snapshot->stacks = MC_take_snapshot_stacks(&snapshot, snapshot->regions[0]->data);
615 //MC_get_hash_global(snapshot->hash_global, snapshot->regions[1]->data, snapshot->regions[2]->data);
616 //MC_get_hash_local(snapshot->hash_local, snapshot->stacks);
620 MC_dump_checkpoint_ignore(snapshot);
626 void MC_restore_snapshot(mc_snapshot_t snapshot){
628 for(i=0; i < NB_REGIONS; i++){
629 MC_region_restore(snapshot->regions[i]);
634 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall){
635 return MC_take_snapshot(1);
638 void *MC_snapshot(void){
639 return simcall_mc_snapshot();