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"
13 #include "../simix/smx_private.h"
15 #include <libunwind.h>
18 #include "mc_private.h"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
21 "Logging specific to mc_checkpoint");
23 char *libsimgrid_path;
25 static void MC_find_object_address(memory_map_t maps, mc_object_info_t result);
27 /************************************ Free functions **************************************/
28 /*****************************************************************************************/
30 static void MC_snapshot_stack_free(mc_snapshot_stack_t s){
32 xbt_dynar_free(&(s->local_variables));
37 static void MC_snapshot_stack_free_voidp(void *s){
38 MC_snapshot_stack_free((mc_snapshot_stack_t) * (void **) s);
41 static void local_variable_free(local_variable_t v){
48 static void local_variable_free_voidp(void *v){
49 local_variable_free((local_variable_t) * (void **) v);
52 static void MC_region_destroy(mc_mem_region_t reg)
58 void MC_free_snapshot(mc_snapshot_t snapshot){
60 for(i=0; i < NB_REGIONS; i++)
61 MC_region_destroy(snapshot->regions[i]);
63 xbt_free(snapshot->stack_sizes);
64 xbt_dynar_free(&(snapshot->stacks));
65 xbt_dynar_free(&(snapshot->to_ignore));
70 /******************************* Snapshot regions ********************************/
71 /*********************************************************************************/
73 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
75 mc_mem_region_t new_reg = xbt_new0(s_mc_mem_region_t, 1);
76 new_reg->start_addr = start_addr;
78 new_reg->data = xbt_malloc0(size);
79 memcpy(new_reg->data, start_addr, size);
81 XBT_DEBUG("New region : type : %d, data : %p (real addr %p), size : %zu", type, new_reg->data, start_addr, size);
86 static void MC_region_restore(mc_mem_region_t reg)
88 /*FIXME: check if start_addr is still mapped, if it is not, then map it
89 before copying the data */
91 memcpy(reg->start_addr, reg->data, reg->size);
95 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size)
97 mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
98 snapshot->regions[type] = new_reg;
102 static void MC_get_memory_regions(mc_snapshot_t snapshot){
109 char *lfields[6] = {0}, *tok;
110 void *start_addr, *start_addr1, *end_addr;
114 fp = fopen("/proc/self/maps", "r");
117 "Cannot open /proc/self/maps to investigate the memory map of the process. Please report this bug.");
121 while((read = xbt_getline(&line, &n, fp)) != -1){
123 /* Wipeout the new line character */
124 line[read - 1] = '\0';
126 /* Tokenize the line using spaces as delimiters and store each token */
127 lfields[0] = strtok(line, " ");
129 for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
130 lfields[i] = strtok(NULL, " ");
133 /* First get the permissions flags, need write permission */
134 if(lfields[1][1] == 'w'){
136 /* Get the start address of the map */
137 tok = strtok(lfields[0], "-");
138 start_addr = (void *)strtoul(tok, NULL, 16);
140 if(start_addr == std_heap){ /* Std_heap ? */
141 tok = strtok(NULL, "-");
142 end_addr = (void *)strtoul(tok, NULL, 16);
143 MC_snapshot_add_region(snapshot, 0, start_addr, (char*)end_addr - (char*)start_addr);
144 snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
145 }else{ /* map name == libsimgrid || binary_name ? */
146 if(lfields[5] != NULL){
147 if(!memcmp(basename(lfields[5]), "libsimgrid", 10)){
148 tok = strtok(NULL, "-");
149 end_addr = (void *)strtoul(tok, NULL, 16);
150 size = (char*)end_addr - (char*)start_addr;
151 /* BSS and data segments may be separated according to the OS */
152 if((read = xbt_getline(&line, &n, fp)) != -1){
153 line[read - 1] = '\0';
154 lfields[0] = strtok(line, " ");
155 for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
156 lfields[i] = strtok(NULL, " ");
158 if(lfields[1][1] == 'w' && lfields[5] == NULL){
159 tok = strtok(lfields[0], "-");
160 start_addr1 = (void *)strtoul(tok, NULL, 16);
161 tok = strtok(NULL, "-");
162 size += (char *)(void *)strtoul(tok, NULL, 16) - (char*)start_addr1;
165 MC_snapshot_add_region(snapshot, 1, start_addr, size);
166 }else if(!memcmp(basename(lfields[5]), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
167 tok = strtok(NULL, "-");
168 end_addr = (void *)strtoul(tok, NULL, 16);
169 size = (char*)end_addr - (char*)start_addr;
170 /* BSS and data segments may be separated according to the OS */
171 if((read = xbt_getline(&line, &n, fp)) != -1){
172 line[read - 1] = '\0';
173 lfields[0] = strtok(line, " ");
174 for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
175 lfields[i] = strtok(NULL, " ");
177 tok = strtok(lfields[0], "-");
178 start_addr1 = (void *)strtoul(tok, NULL, 16);
179 if(lfields[1][1] == 'w'){
180 if(start_addr1 == std_heap){ /* Std_heap ? */
181 tok = strtok(NULL, "-");
182 end_addr = (void *)strtoul(tok, NULL, 16);
183 MC_snapshot_add_region(snapshot, 0, start_addr1, (char*)end_addr - (char*)start_addr1);
184 snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
185 }else if(start_addr1 != raw_heap){
186 tok = strtok(NULL, "-");
187 size += (char *)(void *)strtoul(tok, NULL, 16) - (char *)start_addr1;
191 MC_snapshot_add_region(snapshot, 2, start_addr, size);
192 }else if (!memcmp(lfields[5], "[stack]", 7)){
193 maestro_stack_start = start_addr;
194 tok = strtok(NULL, "-");
195 maestro_stack_end = (void *)strtoul(tok, NULL, 16);
208 /** @brief Finds the range of the different memory segments and binary paths */
209 void MC_init_memory_map_info(){
213 memory_map_t maps = MC_get_memory_map();
215 maestro_stack_start = NULL;
216 maestro_stack_end = NULL;
217 libsimgrid_path = NULL;
219 while (i < maps->mapsize) {
220 reg = maps->regions[i];
221 if (maps->regions[i].pathname == NULL) {
224 else if ((reg.prot & PROT_WRITE) && !memcmp(maps->regions[i].pathname, "[stack]", 7)){
225 maestro_stack_start = reg.start_addr;
226 maestro_stack_end = reg.end_addr;
227 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC) && !memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
228 if(libsimgrid_path == NULL)
229 libsimgrid_path = strdup(maps->regions[i].pathname);
234 xbt_assert(maestro_stack_start, "maestro_stack_start");
235 xbt_assert(maestro_stack_end, "maestro_stack_end");
236 xbt_assert(libsimgrid_path, "libsimgrid_path&");
238 MC_free_memory_map(maps);
242 /** \brief Fill/llokup the "subtype" field.
244 static void MC_resolve_subtype(mc_object_info_t info, dw_type_t type) {
246 if(type->dw_type_id==NULL)
248 type->subtype = xbt_dict_get_or_null(info->types, type->dw_type_id);
249 if(type->subtype==NULL)
251 if(type->subtype->byte_size != 0)
253 if(type->subtype->name==NULL)
255 // Try to find a more complete description of the type:
256 // We need to fix in order to support C++.
258 dw_type_t subtype = xbt_dict_get_or_null(info->types_by_name, type->subtype->name);
260 type->subtype = subtype;
263 // TODO, support "switch type" (looking up the type in another lib) when possible
266 static void MC_post_process_types(mc_object_info_t info) {
267 xbt_dict_cursor_t cursor = NULL;
271 // Lookup "subtype" field:
272 xbt_dict_foreach(info->types, cursor, origin, type){
273 MC_resolve_subtype(info, type);
277 if(type->members!=NULL) xbt_dynar_foreach(type->members, i, member) {
278 MC_resolve_subtype(info, member);
283 /** \brief Finds informations about a given shared object/executable */
284 mc_object_info_t MC_find_object_info(memory_map_t maps, char* name) {
285 mc_object_info_t result = MC_new_object_info();
286 result->file_name = xbt_strdup(name);
287 MC_find_object_address(maps, result);
288 MC_dwarf_get_variables(result);
289 MC_post_process_types(result);
293 /** \brief Fills the position of the .bss and .data sections. */
294 static void MC_find_object_address(memory_map_t maps, mc_object_info_t result) {
298 const char* name = basename(result->file_name);
299 while (i < maps->mapsize) {
300 reg = maps->regions[i];
301 if (maps->regions[i].pathname == NULL || strcmp(basename(maps->regions[i].pathname), name)) {
304 else if ((reg.prot & PROT_WRITE)){
305 xbt_assert(!result->start_rw,
306 "Multiple read-write segments for %s, not supported",
307 maps->regions[i].pathname);
308 result->start_rw = reg.start_addr;
309 result->end_rw = reg.end_addr;
310 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)){
311 xbt_assert(!result->start_exec,
312 "Multiple executable segments for %s, not supported",
313 maps->regions[i].pathname);
314 result->start_exec = reg.start_addr;
315 result->end_exec = reg.end_addr;
317 else if((reg.prot & PROT_READ) && !(reg.prot & PROT_EXEC)) {
318 xbt_assert(!result->start_ro,
319 "Multiple read only segments for %s, not supported",
320 maps->regions[i].pathname);
321 result->start_ro = reg.start_addr;
322 result->end_ro = reg.end_addr;
327 xbt_assert(result->file_name);
328 xbt_assert(result->start_rw);
329 xbt_assert(result->start_exec);
332 /************************************* Take Snapshot ************************************/
333 /****************************************************************************************/
335 static void MC_get_hash_global(char *snapshot_hash, void *data1, void *data2){
337 /* unsigned int cursor = 0; */
339 /* global_variable_t current_var; */
340 /* void *addr_pointed = NULL; */
341 /* void *res = NULL; */
343 /* xbt_strbuff_t clear = xbt_strbuff_new(); */
345 /* xbt_dynar_foreach(mc_global_variables, cursor, current_var){ */
346 /* if(current_var->address < start_data_libsimgrid){ /\* binary *\/ */
347 /* offset = (char *)current_var->address - (char *)start_data_binary; */
348 /* addr_pointed = *((void **)((char *)data2 + offset)); */
349 /* if(((addr_pointed >= start_plt_binary && addr_pointed <= end_plt_binary)) || ((addr_pointed >= std_heap && (char *)addr_pointed <= (char *)std_heap + STD_HEAP_SIZE ))) */
351 /* res = xbt_malloc0(current_var->size + 1); */
352 /* memset(res, 0, current_var->size + 1); */
353 /* memcpy(res, (char*)data2 + offset, current_var->size); */
354 /* }else{ /\* libsimgrid *\/ */
355 /* offset = (char *)current_var->address - (char *)start_data_libsimgrid; */
356 /* addr_pointed = *((void **)((char *)data1 + offset)); */
357 /* if((addr_pointed >= start_plt_libsimgrid && addr_pointed <= end_plt_libsimgrid) || (addr_pointed >= std_heap && (char *)addr_pointed <= (char *)std_heap + STD_HEAP_SIZE )) */
359 /* res = xbt_malloc0(current_var->size + 1); */
360 /* memset(res, 0, current_var->size + 1); */
361 /* memcpy(res, (char*)data1 + offset, current_var->size); */
363 /* if(res != NULL){ */
364 /* xbt_strbuff_append(clear, (const char*)res); */
370 /* xbt_sha(clear->data, snapshot_hash); */
372 /* xbt_strbuff_free(clear); */
376 static void MC_get_hash_local(char *snapshot_hash, xbt_dynar_t stacks){
378 /* xbt_dynar_t tokens = NULL, s_tokens = NULL; */
379 /* unsigned int cursor1 = 0, cursor2 = 0; */
380 /* mc_snapshot_stack_t current_stack; */
381 /* char *frame_name = NULL; */
384 /* xbt_strbuff_t clear = xbt_strbuff_new(); */
386 /* while(cursor1 < xbt_dynar_length(stacks)){ */
387 /* current_stack = xbt_dynar_get_as(stacks, cursor1, mc_snapshot_stack_t); */
388 /* tokens = xbt_str_split(current_stack->local_variables->data, NULL); */
390 /* while(cursor2 < xbt_dynar_length(tokens)){ */
391 /* s_tokens = xbt_str_split(xbt_dynar_get_as(tokens, cursor2, char *), "="); */
392 /* if(xbt_dynar_length(s_tokens) > 1){ */
393 /* if(strcmp(xbt_dynar_get_as(s_tokens, 0, char *), "frame_name") == 0){ */
394 /* xbt_free(frame_name); */
395 /* frame_name = xbt_strdup(xbt_dynar_get_as(s_tokens, 1, char *)); */
396 /* xbt_strbuff_append(clear, (const char*)xbt_dynar_get_as(tokens, cursor2, char *)); */
398 /* xbt_dynar_free(&s_tokens); */
401 /* addr = (void *) strtoul(xbt_dynar_get_as(s_tokens, 1, char *), NULL, 16); */
402 /* if(addr > std_heap && (char *)addr <= (char *)std_heap + STD_HEAP_SIZE){ */
404 /* xbt_dynar_free(&s_tokens); */
407 /* if(is_stack_ignore_variable(frame_name, xbt_dynar_get_as(s_tokens, 0, char *))){ */
409 /* xbt_dynar_free(&s_tokens); */
412 /* xbt_strbuff_append(clear, (const char *)xbt_dynar_get_as(tokens, cursor2, char *)); */
414 /* xbt_dynar_free(&s_tokens); */
417 /* xbt_dynar_free(&tokens); */
421 /* xbt_free(frame_name); */
423 /* xbt_sha(clear->data, snapshot_hash); */
425 /* xbt_strbuff_free(clear); */
431 static xbt_dynar_t MC_get_local_variables_values(void *stack_context){
436 char frame_name[256];
438 ret = unw_init_local(&c, (unw_context_t *)stack_context);
440 XBT_INFO("unw_init_local failed");
444 unw_word_t ip, sp, off;
447 unsigned int cursor = 0;
448 dw_variable_t current_variable;
450 void *frame_pointer_address = NULL;
454 xbt_dynar_t variables = xbt_dynar_new(sizeof(local_variable_t), local_variable_free_voidp);
456 while(ret >= 0 && !stop){
458 unw_get_reg(&c, UNW_REG_IP, &ip);
459 unw_get_reg(&c, UNW_REG_SP, &sp);
461 unw_get_proc_name(&c, frame_name, sizeof (frame_name), &off);
463 if(!strcmp(frame_name, "smx_ctx_sysv_wrapper")) /* Stop before context switch with maestro */
466 if((long)ip > (long) mc_libsimgrid_info->start_exec)
467 frame = xbt_dict_get_or_null(mc_libsimgrid_info->local_variables, frame_name);
469 frame = xbt_dict_get_or_null(mc_binary_info->local_variables, frame_name);
476 true_ip = (long)frame->low_pc + (long)off;
477 frame_pointer_address = mc_find_frame_base(true_ip, frame, &c);
481 xbt_dynar_foreach(frame->variables, cursor, current_variable){
483 if((long)ip > (long)mc_libsimgrid_info->start_exec)
488 local_variable_t new_var = xbt_new0(s_local_variable_t, 1);
489 new_var->frame = xbt_strdup(frame_name);
490 new_var->ip = (unsigned long)ip;
491 new_var->name = xbt_strdup(current_variable->name);
492 new_var->type = strdup(current_variable->type_origin);
493 new_var->region= region_type;
495 if(current_variable->address.location != NULL){
496 new_var->address = (void*) MC_dwarf_resolve_location(&c, current_variable->address.location, frame_pointer_address);
499 xbt_dynar_push(variables, &new_var);
512 static void *MC_get_stack_pointer(void *stack_context, void *heap){
518 ret = unw_init_local(&c, (unw_context_t *)stack_context);
520 XBT_INFO("unw_init_local failed");
524 unw_get_reg(&c, UNW_REG_SP, &sp);
526 return ((char *)heap + (size_t)(((char *)((long)sp) - (char*)std_heap)));
530 static xbt_dynar_t MC_take_snapshot_stacks(mc_snapshot_t *snapshot, void *heap){
532 xbt_dynar_t res = xbt_dynar_new(sizeof(s_mc_snapshot_stack_t), MC_snapshot_stack_free_voidp);
534 unsigned int cursor = 0;
535 stack_region_t current_stack;
537 xbt_dynar_foreach(stacks_areas, cursor, current_stack){
538 mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
539 st->local_variables = MC_get_local_variables_values(current_stack->context);
540 st->stack_pointer = MC_get_stack_pointer(current_stack->context, heap);
541 st->real_address = current_stack->address;
542 xbt_dynar_push(res, &st);
543 (*snapshot)->stack_sizes = xbt_realloc((*snapshot)->stack_sizes, (cursor + 1) * sizeof(size_t));
544 (*snapshot)->stack_sizes[cursor] = current_stack->size - ((char *)st->stack_pointer - (char *)((char *)heap + ((char *)current_stack->address - (char *)std_heap)));
551 static xbt_dynar_t MC_take_snapshot_ignore(){
553 if(mc_heap_comparison_ignore == NULL)
556 xbt_dynar_t cpy = xbt_dynar_new(sizeof(mc_heap_ignore_region_t), heap_ignore_region_free_voidp);
558 unsigned int cursor = 0;
559 mc_heap_ignore_region_t current_region;
561 xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, current_region){
562 mc_heap_ignore_region_t new_region = NULL;
563 new_region = xbt_new0(s_mc_heap_ignore_region_t, 1);
564 new_region->address = current_region->address;
565 new_region->size = current_region->size;
566 new_region->block = current_region->block;
567 new_region->fragment = current_region->fragment;
568 xbt_dynar_push(cpy, &new_region);
575 static void MC_dump_checkpoint_ignore(mc_snapshot_t snapshot){
577 unsigned int cursor = 0;
578 mc_checkpoint_ignore_region_t region;
581 xbt_dynar_foreach(mc_checkpoint_ignore, cursor, region){
582 if(region->addr > snapshot->regions[0]->start_addr && (char *)(region->addr) < (char *)snapshot->regions[0]->start_addr + STD_HEAP_SIZE){
583 offset = (char *)region->addr - (char *)snapshot->regions[0]->start_addr;
584 memset((char *)snapshot->regions[0]->data + offset, 0, region->size);
585 }else if(region->addr > snapshot->regions[2]->start_addr && (char *)(region->addr) < (char*)snapshot->regions[2]->start_addr + snapshot->regions[2]->size){
586 offset = (char *)region->addr - (char *)snapshot->regions[2]->start_addr;
587 memset((char *)snapshot->regions[2]->data + offset, 0, region->size);
588 }else if(region->addr > snapshot->regions[1]->start_addr && (char *)(region->addr) < (char*)snapshot->regions[1]->start_addr + snapshot->regions[1]->size){
589 offset = (char *)region->addr - (char *)snapshot->regions[1]->start_addr;
590 memset((char *)snapshot->regions[1]->data + offset, 0, region->size);
597 mc_snapshot_t MC_take_snapshot(int num_state){
599 mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
600 snapshot->nb_processes = xbt_swag_size(simix_global->process_list);
601 if(MC_USE_SNAPSHOT_HASH) {
602 snapshot->hash = mc_hash_processes_state(num_state);
607 /* Save the std heap and the writable mapped pages of libsimgrid and binary */
608 MC_get_memory_regions(snapshot);
610 snapshot->to_ignore = MC_take_snapshot_ignore();
612 if(_sg_mc_visited > 0 || strcmp(_sg_mc_property_file,"")){
613 snapshot->stacks = MC_take_snapshot_stacks(&snapshot, snapshot->regions[0]->data);
614 //MC_get_hash_global(snapshot->hash_global, snapshot->regions[1]->data, snapshot->regions[2]->data);
615 //MC_get_hash_local(snapshot->hash_local, snapshot->stacks);
619 MC_dump_checkpoint_ignore(snapshot);
625 void MC_restore_snapshot(mc_snapshot_t snapshot){
627 for(i=0; i < NB_REGIONS; i++){
628 MC_region_restore(snapshot->regions[i]);
633 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall){
634 return MC_take_snapshot(1);
637 void *MC_snapshot(void){
638 return simcall_mc_snapshot();