1 /* Copyright (c) 2008-2012 Da SimGrid Team. All rights reserved. */
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. */
7 #include "mc_private.h"
8 #include "xbt/module.h"
10 #include "../simix/smx_private.h"
12 #include <libunwind.h>
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
15 "Logging specific to mc_checkpoint");
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;
25 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size);
26 static void MC_region_restore(mc_mem_region_t reg);
27 static void MC_region_destroy(mc_mem_region_t reg);
29 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size);
31 static void add_value(xbt_dynar_t *list, const char *type, unsigned long int val);
32 static xbt_dynar_t take_snapshot_stacks(void *heap);
33 static xbt_strbuff_t get_local_variables_values(void *stack_context, void *heap);
34 static void print_local_variables_values(xbt_dynar_t all_variables);
35 static void *get_stack_pointer(void *stack_context, void *heap);
37 static void snapshot_stack_free(mc_snapshot_stack_t s);
39 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
41 mc_mem_region_t new_reg = xbt_new0(s_mc_mem_region_t, 1);
43 new_reg->start_addr = start_addr;
45 new_reg->data = xbt_malloc0(size);
46 memcpy(new_reg->data, start_addr, size);
48 XBT_DEBUG("New region : type : %d, data : %p, size : %zu", type, new_reg->data, size);
53 static void MC_region_restore(mc_mem_region_t reg)
55 /*FIXME: check if start_addr is still mapped, if it is not, then map it
56 before copying the data */
58 memcpy(reg->start_addr, reg->data, reg->size);
63 static void MC_region_destroy(mc_mem_region_t reg)
69 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size)
71 mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
72 snapshot->regions = xbt_realloc(snapshot->regions, (snapshot->num_reg + 1) * sizeof(mc_mem_region_t));
73 snapshot->regions[snapshot->num_reg] = new_reg;
78 void MC_take_snapshot(mc_snapshot_t snapshot)
82 memory_map_t maps = get_memory_map();
84 /* Save the std heap and the writable mapped pages of libsimgrid */
85 while (i < maps->mapsize) {
86 reg = maps->regions[i];
87 if ((reg.prot & PROT_WRITE)){
88 if (maps->regions[i].pathname == NULL){
89 if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
90 MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
94 if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
95 MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
97 reg = maps->regions[i];
98 while(reg.pathname == NULL && (reg.prot & PROT_WRITE) && i < maps->mapsize){
99 MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
101 reg = maps->regions[i];
112 free_memory_map(maps);
115 void MC_take_snapshot_liveness(mc_snapshot_t snapshot)
120 memory_map_t maps = get_memory_map();
124 /* Save the std heap and the writable mapped pages of libsimgrid */
125 while (i < maps->mapsize) {
126 reg = maps->regions[i];
127 if ((reg.prot & PROT_WRITE)){
128 if (maps->regions[i].pathname == NULL){
129 if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
130 MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
131 heap = snapshot->regions[nb_reg]->data;
133 }else if(reg.start_addr == raw_heap){
134 end_raw_heap = reg.end_addr;
138 if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
139 MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
140 start_data_libsimgrid = reg.start_addr;
143 reg = maps->regions[i];
144 while(reg.pathname == NULL && (reg.prot & PROT_WRITE) && i < maps->mapsize){
145 MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
147 reg = maps->regions[i];
151 if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
152 MC_snapshot_add_region(snapshot, 2, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
158 }else if ((reg.prot & PROT_READ)){
159 if (maps->regions[i].pathname != NULL){
160 if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
161 start_text_libsimgrid = reg.start_addr;
162 libsimgrid_path = strdup(maps->regions[i].pathname);
164 if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
165 start_text_binary = reg.start_addr;
175 snapshot->stacks = take_snapshot_stacks(heap);
177 free_memory_map(maps);
181 void MC_restore_snapshot(mc_snapshot_t snapshot)
184 for(i=0; i < snapshot->num_reg; i++){
185 MC_region_restore(snapshot->regions[i]);
190 void MC_free_snapshot(mc_snapshot_t snapshot)
193 for(i=0; i < snapshot->num_reg; i++)
194 MC_region_destroy(snapshot->regions[i]);
196 xbt_dynar_free(&(snapshot->stacks));
201 void get_libsimgrid_plt_section(){
204 char *line = NULL; /* Temporal storage for each line that is readed */
205 ssize_t read; /* Number of bytes readed */
206 size_t n = 0; /* Amount of bytes to read by getline */
209 int i, plt_not_found = 1;
210 unsigned long int size, offset;
212 char *command = bprintf( "objdump --section-headers %s", libsimgrid_path);
214 fp = popen(command, "r");
217 perror("popen failed");
219 while ((read = getline(&line, &n, fp)) != -1 && plt_not_found == 1) {
224 /* Wipeout the new line character */
225 line[read - 1] = '\0';
227 lfields[0] = strtok(line, " ");
229 if(lfields[0] == NULL)
232 if(strcmp(lfields[0], "Sections:") == 0 || strcmp(lfields[0], "Idx") == 0 || strncmp(lfields[0], libsimgrid_path, strlen(libsimgrid_path)) == 0)
235 for (i = 1; i < 7 && lfields[i - 1] != NULL; i++) {
236 lfields[i] = strtok(NULL, " ");
240 if(strcmp(lfields[1], ".plt") == 0){
241 size = strtoul(lfields[2], NULL, 16);
242 offset = strtoul(lfields[5], NULL, 16);
243 start_plt_libsimgrid = (char *)start_text_libsimgrid + offset;
244 end_plt_libsimgrid = (char *)start_plt_libsimgrid + size;
258 void get_binary_plt_section(){
261 char *line = NULL; /* Temporal storage for each line that is readed */
262 ssize_t read; /* Number of bytes readed */
263 size_t n = 0; /* Amount of bytes to read by getline */
266 int i, plt_not_found = 1;
267 unsigned long int size, offset;
269 char *command = bprintf( "objdump --section-headers %s", xbt_binary_name);
271 fp = popen(command, "r");
274 perror("popen failed");
276 while ((read = getline(&line, &n, fp)) != -1 && plt_not_found == 1) {
281 /* Wipeout the new line character */
282 line[read - 1] = '\0';
284 lfields[0] = strtok(line, " ");
286 if(lfields[0] == NULL)
289 if(strcmp(lfields[0], "Sections:") == 0 || strcmp(lfields[0], "Idx") == 0 || strncmp(lfields[0], basename(xbt_binary_name), strlen(xbt_binary_name)) == 0)
292 for (i = 1; i < 7 && lfields[i - 1] != NULL; i++) {
293 lfields[i] = strtok(NULL, " ");
297 if(strcmp(lfields[1], ".plt") == 0){
298 size = strtoul(lfields[2], NULL, 16);
299 offset = strtoul(lfields[5], NULL, 16);
300 start_plt_binary = (char *)start_text_binary + offset;
301 end_plt_binary = (char *)start_plt_binary + size;
315 static void add_value(xbt_dynar_t *list, const char *type, unsigned long int val){
316 variable_value_t value = xbt_new0(s_variable_value_t, 1);
317 value->type = strdup(type);
318 if(strcmp(type, "value") == 0){
319 value->value.res = val;
321 value->value.address = (void *)val;
323 xbt_dynar_push(*list, &value);
326 static xbt_dynar_t take_snapshot_stacks(void *heap){
328 xbt_dynar_t res = xbt_dynar_new(sizeof(s_mc_snapshot_stack_t), NULL);
330 unsigned int cursor1 = 0;
331 stack_region_t current_stack;
333 xbt_dynar_foreach(stacks_areas, cursor1, current_stack){
334 mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
335 st->local_variables = get_local_variables_values(current_stack->context, heap);
336 st->stack_pointer = get_stack_pointer(current_stack->context, heap);
337 xbt_dynar_push(res, &st);
344 static void *get_stack_pointer(void *stack_context, void *heap){
350 ret = unw_init_local(&c, (unw_context_t *)stack_context);
352 XBT_INFO("unw_init_local failed");
356 unw_get_reg(&c, UNW_REG_SP, &sp);
358 return ((char *)heap + (size_t)(((char *)((long)sp) - (char*)std_heap)));
362 static xbt_strbuff_t get_local_variables_values(void *stack_context, void *heap){
368 char buf[512], frame_name[256];
370 ret = unw_init_local(&c, (unw_context_t *)stack_context);
372 XBT_INFO("unw_init_local failed");
376 //stack_name = strdup(((smx_process_t)((smx_ctx_sysv_t)(stack->address))->super.data)->name);
378 unw_word_t ip, sp, off;
381 xbt_dynar_t compose = xbt_dynar_new(sizeof(variable_value_t), NULL);
383 xbt_strbuff_t variables = xbt_strbuff_new();
384 xbt_dict_cursor_t dict_cursor;
386 dw_local_variable_t current_variable;
387 unsigned int cursor = 0, cursor2 = 0;
388 dw_location_entry_t entry = NULL;
389 dw_location_t location_entry = NULL;
392 void *frame_pointer_address = NULL;
397 unw_get_reg(&c, UNW_REG_IP, &ip);
398 unw_get_reg(&c, UNW_REG_SP, &sp);
401 if (unw_get_proc_name (&c, frame_name, sizeof (frame_name), &off) == 0){
403 snprintf (buf, sizeof (buf), "<%s+0x%lx>", frame_name, (long) off);
405 snprintf (buf, sizeof (buf), "<%s>", frame_name);
409 xbt_strbuff_append(variables, bprintf("ip=%-32s\n", buf));
411 frame = xbt_dict_get_or_null(mc_local_variables, frame_name);
418 true_ip = (long)frame->low_pc + (long)off;
420 /* Get frame pointer */
421 switch(frame->frame_base->type){
423 while((cursor < xbt_dynar_length(frame->frame_base->location.loclist)) && frame_found == 0){
424 entry = xbt_dynar_get_as(frame->frame_base->location.loclist, cursor, dw_location_entry_t);
425 if((true_ip >= entry->lowpc) && (true_ip < entry->highpc)){
427 switch(entry->location->type){
429 xbt_dynar_reset(compose);
431 while(cursor2 < xbt_dynar_length(entry->location->location.compose)){
432 location_entry = xbt_dynar_get_as(entry->location->location.compose, cursor2, dw_location_t);
433 switch(location_entry->type){
435 unw_get_reg(&c, location_entry->location.reg, &res);
436 add_value(&compose, "address", (long)res);
438 case e_dw_bregister_op:
439 unw_get_reg(&c, location_entry->location.breg_op.reg, &res);
440 add_value(&compose, "address", (long)res + location_entry->location.breg_op.offset);
443 xbt_dynar_reset(compose);
449 if(xbt_dynar_length(compose) > 0){
450 frame_pointer_address = xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.address ;
454 frame_pointer_address = NULL;
462 frame_pointer_address = NULL;
469 //XBT_INFO("Frame %s", frame->name);
471 xbt_dict_foreach(frame->variables, dict_cursor, variable_name, current_variable){
472 if(current_variable->location != NULL){
473 switch(current_variable->location->type){
475 xbt_dynar_reset(compose);
477 while(cursor < xbt_dynar_length(current_variable->location->location.compose)){
478 location_entry = xbt_dynar_get_as(current_variable->location->location.compose, cursor, dw_location_t);
479 switch(location_entry->type){
481 unw_get_reg(&c, location_entry->location.reg, &res);
482 add_value(&compose, "value", (long)res);
484 case e_dw_bregister_op:
485 unw_get_reg(&c, location_entry->location.breg_op.reg, &res);
486 add_value(&compose, "address", (long)res + location_entry->location.breg_op.offset);
488 case e_dw_fbregister_op:
489 if(frame_pointer_address != NULL)
490 add_value(&compose, "address", (long)((char *)frame_pointer_address + location_entry->location.fbreg_op));
493 xbt_dynar_reset(compose);
499 if(xbt_dynar_length(compose) > 0){
500 if(strcmp(xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->type, "value") == 0){
501 //XBT_INFO("Variable : %s - value : %lx", current_variable->name, xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.res);
502 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));
504 if(*((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1,variable_value_t)->value.address) == NULL){
505 //XBT_INFO("Variable : %s - address : NULL", current_variable->name);
506 xbt_strbuff_append(variables, bprintf("%s=NULL\n", current_variable->name));
507 }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)){
508 //XBT_INFO("Variable : %s - value : %zd", current_variable->name, (size_t)*((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.address));
509 xbt_strbuff_append(variables, bprintf("%s=%zd\n", current_variable->name, (size_t)*((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.address)));
511 //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));
512 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)));
516 //XBT_INFO("Variable %s undefined", current_variable->name);
517 xbt_strbuff_append(variables, bprintf("%s=undefined\n", current_variable->name));
524 //XBT_INFO("Variable : %s, no location", current_variable->name);
525 xbt_strbuff_append(variables, bprintf("%s=undefined\n", current_variable->name));
541 static void print_local_variables_values(xbt_dynar_t all_variables){
544 mc_snapshot_stack_t stack;
546 xbt_dynar_foreach(all_variables, cursor, stack){
547 XBT_INFO("%s", stack->local_variables->data);
552 static void snapshot_stack_free(mc_snapshot_stack_t s){
554 xbt_free(s->local_variables->data);
555 xbt_free(s->local_variables);
560 void snapshot_stack_free_voidp(void *s){
561 snapshot_stack_free((mc_snapshot_stack_t) * (void **) s);