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;
24 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size);
25 static void MC_region_restore(mc_mem_region_t reg);
26 static void MC_region_destroy(mc_mem_region_t reg);
28 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size);
30 static void add_value(xbt_dynar_t *list, const char *type, unsigned long int val);
31 static xbt_dynar_t take_snapshot_stacks(void *heap);
32 static xbt_strbuff_t get_local_variables_values(void *stack_context, void *heap);
33 static void print_local_variables_values(xbt_dynar_t all_variables);
34 static void *get_stack_pointer(void *stack_context, void *heap);
36 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
38 mc_mem_region_t new_reg = xbt_new0(s_mc_mem_region_t, 1);
40 new_reg->start_addr = start_addr;
42 new_reg->data = xbt_malloc0(size);
43 memcpy(new_reg->data, start_addr, size);
45 XBT_DEBUG("New region : type : %d, data : %p, size : %zu", type, new_reg->data, size);
50 static void MC_region_restore(mc_mem_region_t reg)
52 /*FIXME: check if start_addr is still mapped, if it is not, then map it
53 before copying the data */
55 memcpy(reg->start_addr, reg->data, reg->size);
60 static void MC_region_destroy(mc_mem_region_t reg)
66 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size)
68 mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
69 snapshot->regions = xbt_realloc(snapshot->regions, (snapshot->num_reg + 1) * sizeof(mc_mem_region_t));
70 snapshot->regions[snapshot->num_reg] = new_reg;
75 void MC_take_snapshot(mc_snapshot_t snapshot)
79 memory_map_t maps = get_memory_map();
81 /* Save the std heap and the writable mapped pages of libsimgrid */
82 while (i < maps->mapsize) {
83 reg = maps->regions[i];
84 if ((reg.prot & PROT_WRITE)){
85 if (maps->regions[i].pathname == NULL){
86 if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
87 MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
91 if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
92 MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
94 reg = maps->regions[i];
95 while(reg.pathname == NULL && (reg.prot & PROT_WRITE) && i < maps->mapsize){
96 MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
98 reg = maps->regions[i];
109 free_memory_map(maps);
112 void MC_take_snapshot_liveness(mc_snapshot_t snapshot)
117 memory_map_t maps = get_memory_map();
121 /* Save the std heap and the writable mapped pages of libsimgrid */
122 while (i < maps->mapsize) {
123 reg = maps->regions[i];
124 if ((reg.prot & PROT_WRITE)){
125 if (maps->regions[i].pathname == NULL){
126 if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
127 MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
128 heap = snapshot->regions[nb_reg]->data;
133 if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
134 MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
135 start_data_libsimgrid = reg.start_addr;
138 reg = maps->regions[i];
139 while(reg.pathname == NULL && (reg.prot & PROT_WRITE) && i < maps->mapsize){
140 MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
142 reg = maps->regions[i];
146 if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
147 MC_snapshot_add_region(snapshot, 2, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
153 }else if ((reg.prot & PROT_READ)){
154 if (maps->regions[i].pathname != NULL){
155 if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
156 start_text_libsimgrid = reg.start_addr;
157 libsimgrid_path = strdup(maps->regions[i].pathname);
159 if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
160 start_text_binary = reg.start_addr;
170 snapshot->stacks = take_snapshot_stacks(heap);
172 free_memory_map(maps);
176 void MC_restore_snapshot(mc_snapshot_t snapshot)
179 for(i=0; i < snapshot->num_reg; i++){
180 MC_region_restore(snapshot->regions[i]);
185 void MC_free_snapshot(mc_snapshot_t snapshot)
188 for(i=0; i < snapshot->num_reg; i++)
189 MC_region_destroy(snapshot->regions[i]);
195 void get_libsimgrid_plt_section(){
198 char *line = NULL; /* Temporal storage for each line that is readed */
199 ssize_t read; /* Number of bytes readed */
200 size_t n = 0; /* Amount of bytes to read by getline */
203 int i, plt_not_found = 1;
204 unsigned long int size, offset;
206 char *command = bprintf( "objdump --section-headers %s", libsimgrid_path);
208 fp = popen(command, "r");
211 perror("popen failed");
213 while ((read = getline(&line, &n, fp)) != -1 && plt_not_found == 1) {
218 /* Wipeout the new line character */
219 line[read - 1] = '\0';
221 lfields[0] = strtok(line, " ");
223 if(lfields[0] == NULL)
226 if(strcmp(lfields[0], "Sections:") == 0 || strcmp(lfields[0], "Idx") == 0 || strcmp(lfields[0], "libsimgrid.so:") == 0)
229 for (i = 1; i < 7 && lfields[i - 1] != NULL; i++) {
230 lfields[i] = strtok(NULL, " ");
234 if(strcmp(lfields[1], ".plt") == 0){
235 size = strtoul(lfields[2], NULL, 16);
236 offset = strtoul(lfields[5], NULL, 16);
237 start_plt_libsimgrid = (char *)start_text_libsimgrid + offset;
238 end_plt_libsimgrid = (char *)start_plt_libsimgrid + size;
252 void get_binary_plt_section(){
255 char *line = NULL; /* Temporal storage for each line that is readed */
256 ssize_t read; /* Number of bytes readed */
257 size_t n = 0; /* Amount of bytes to read by getline */
260 int i, plt_not_found = 1;
261 unsigned long int size, offset;
263 char *command = bprintf( "objdump --section-headers %s", xbt_binary_name);
265 fp = popen(command, "r");
268 perror("popen failed");
270 while ((read = getline(&line, &n, fp)) != -1 && plt_not_found == 1) {
275 /* Wipeout the new line character */
276 line[read - 1] = '\0';
278 lfields[0] = strtok(line, " ");
280 if(lfields[0] == NULL)
283 if(strcmp(lfields[0], "Sections:") == 0 || strcmp(lfields[0], "Idx") == 0 || strcmp(lfields[0], basename(xbt_binary_name)) == 0)
286 for (i = 1; i < 7 && lfields[i - 1] != NULL; i++) {
287 lfields[i] = strtok(NULL, " ");
291 if(strcmp(lfields[1], ".plt") == 0){
292 size = strtoul(lfields[2], NULL, 16);
293 offset = strtoul(lfields[5], NULL, 16);
294 start_plt_binary = (char *)start_text_binary + offset;
295 end_plt_binary = (char *)start_plt_binary + size;
309 static void add_value(xbt_dynar_t *list, const char *type, unsigned long int val){
310 variable_value_t value = xbt_new0(s_variable_value_t, 1);
311 value->type = strdup(type);
312 if(strcmp(type, "value") == 0){
313 value->value.res = val;
315 value->value.address = (void *)val;
317 xbt_dynar_push(*list, &value);
320 static xbt_dynar_t take_snapshot_stacks(void *heap){
322 xbt_dynar_t res = xbt_dynar_new(sizeof(s_mc_snapshot_stack_t), NULL);
324 unsigned int cursor1 = 0;
325 stack_region_t current_stack;
327 xbt_dynar_foreach(stacks_areas, cursor1, current_stack){
328 mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
329 st->local_variables = get_local_variables_values(current_stack->context, heap);
330 st->stack_pointer = get_stack_pointer(current_stack, heap);
331 xbt_dynar_push(res, &st);
338 static void *get_stack_pointer(void *stack_context, void *heap){
344 ret = unw_init_local(&c, (unw_context_t *)stack_context);
346 XBT_INFO("unw_init_local failed");
350 unw_get_reg(&c, UNW_REG_SP, &sp);
352 return ((char *)heap + (size_t)(((char *)((long)sp) - (char*)std_heap)));
356 static xbt_strbuff_t get_local_variables_values(void *stack_context, void *heap){
362 char buf[512], frame_name[256];
364 ret = unw_init_local(&c, (unw_context_t *)stack_context);
366 XBT_INFO("unw_init_local failed");
370 //stack_name = strdup(((smx_process_t)((smx_ctx_sysv_t)(stack->address))->super.data)->name);
372 unw_word_t ip, sp, off;
375 xbt_dynar_t compose = xbt_dynar_new(sizeof(variable_value_t), NULL);
377 xbt_strbuff_t variables = xbt_strbuff_new();
378 xbt_dict_cursor_t dict_cursor;
380 dw_local_variable_t current_variable;
381 unsigned int cursor = 0, cursor2 = 0;
382 dw_location_entry_t entry = NULL;
383 dw_location_t location_entry = NULL;
386 void *frame_pointer_address = NULL;
391 unw_get_reg(&c, UNW_REG_IP, &ip);
392 unw_get_reg(&c, UNW_REG_SP, &sp);
395 if (unw_get_proc_name (&c, frame_name, sizeof (frame_name), &off) == 0){
397 snprintf (buf, sizeof (buf), "<%s+0x%lx>", frame_name, (long) off);
399 snprintf (buf, sizeof (buf), "<%s>", frame_name);
403 xbt_strbuff_append(variables, bprintf("ip=%-32s\n", buf));
405 frame = xbt_dict_get_or_null(mc_local_variables, frame_name);
412 true_ip = (long)frame->low_pc + (long)off;
414 /* Get frame pointer */
415 switch(frame->frame_base->type){
417 while((cursor < xbt_dynar_length(frame->frame_base->location.loclist)) && frame_found == 0){
418 entry = xbt_dynar_get_as(frame->frame_base->location.loclist, cursor, dw_location_entry_t);
419 if((true_ip >= entry->lowpc) && (true_ip < entry->highpc)){
421 switch(entry->location->type){
423 xbt_dynar_reset(compose);
425 while(cursor2 < xbt_dynar_length(entry->location->location.compose)){
426 location_entry = xbt_dynar_get_as(entry->location->location.compose, cursor2, dw_location_t);
427 switch(location_entry->type){
429 unw_get_reg(&c, location_entry->location.reg, &res);
430 add_value(&compose, "address", (long)res);
432 case e_dw_bregister_op:
433 unw_get_reg(&c, location_entry->location.breg_op.reg, &res);
434 add_value(&compose, "address", (long)res + location_entry->location.breg_op.offset);
437 xbt_dynar_reset(compose);
443 if(xbt_dynar_length(compose) > 0){
444 frame_pointer_address = xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.address ;
448 frame_pointer_address = NULL;
456 frame_pointer_address = NULL;
463 //XBT_INFO("Frame %s", frame->name);
465 xbt_dict_foreach(frame->variables, dict_cursor, variable_name, current_variable){
466 if(current_variable->location != NULL){
467 switch(current_variable->location->type){
469 xbt_dynar_reset(compose);
471 while(cursor < xbt_dynar_length(current_variable->location->location.compose)){
472 location_entry = xbt_dynar_get_as(current_variable->location->location.compose, cursor, dw_location_t);
473 switch(location_entry->type){
475 unw_get_reg(&c, location_entry->location.reg, &res);
476 add_value(&compose, "value", (long)res);
478 case e_dw_bregister_op:
479 unw_get_reg(&c, location_entry->location.breg_op.reg, &res);
480 add_value(&compose, "address", (long)res + location_entry->location.breg_op.offset);
482 case e_dw_fbregister_op:
483 if(frame_pointer_address != NULL)
484 add_value(&compose, "address", (long)((char *)frame_pointer_address + location_entry->location.fbreg_op));
487 xbt_dynar_reset(compose);
493 if(xbt_dynar_length(compose) > 0){
494 if(strcmp(xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->type, "value") == 0){
495 //XBT_INFO("Variable : %s - value : %lx", current_variable->name, xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.res);
496 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));
498 if(*((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1,variable_value_t)->value.address) == NULL){
499 //XBT_INFO("Variable : %s - address : NULL", current_variable->name);
500 xbt_strbuff_append(variables, bprintf("%s=NULL\n", current_variable->name));
501 }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)){
502 //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));
503 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)));
505 //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));
506 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)));
510 //XBT_INFO("Variable %s undefined", current_variable->name);
511 xbt_strbuff_append(variables, bprintf("%s=undefined\n", current_variable->name));
518 //XBT_INFO("Variable : %s, no location", current_variable->name);
519 xbt_strbuff_append(variables, bprintf("%s=undefined\n", current_variable->name));
535 static void print_local_variables_values(xbt_dynar_t all_variables){
538 mc_snapshot_stack_t stack;
540 xbt_dynar_foreach(all_variables, cursor, stack){
541 XBT_INFO("%s", stack->local_variables->data);