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);
90 if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
91 MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
98 free_memory_map(maps);
101 void MC_take_snapshot_liveness(mc_snapshot_t snapshot)
106 memory_map_t maps = get_memory_map();
110 /* Save the std heap and the writable mapped pages of libsimgrid */
111 while (i < maps->mapsize && nb_reg < 3) {
112 reg = maps->regions[i];
113 if ((reg.prot & PROT_WRITE)){
114 if (maps->regions[i].pathname == NULL){
115 if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
116 MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
117 heap = snapshot->regions[nb_reg]->data;
121 if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
122 MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
124 start_data_libsimgrid = reg.start_addr;
126 if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
127 MC_snapshot_add_region(snapshot, 2, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
132 }else if ((reg.prot & PROT_READ)){
133 if (maps->regions[i].pathname != NULL){
134 if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
135 start_text_libsimgrid = reg.start_addr;
136 libsimgrid_path = strdup(maps->regions[i].pathname);
138 if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
139 start_text_binary = reg.start_addr;
147 snapshot->stacks = take_snapshot_stacks(heap);
149 free_memory_map(maps);
153 void MC_restore_snapshot(mc_snapshot_t snapshot)
156 for(i=0; i < snapshot->num_reg; i++){
157 MC_region_restore(snapshot->regions[i]);
162 void MC_free_snapshot(mc_snapshot_t snapshot)
165 for(i=0; i < snapshot->num_reg; i++)
166 MC_region_destroy(snapshot->regions[i]);
172 void get_libsimgrid_plt_section(){
175 char *line = NULL; /* Temporal storage for each line that is readed */
176 ssize_t read; /* Number of bytes readed */
177 size_t n = 0; /* Amount of bytes to read by getline */
180 int i, plt_not_found = 1;
181 unsigned long int size, offset;
183 char *command = bprintf( "objdump --section-headers %s", libsimgrid_path);
185 fp = popen(command, "r");
188 perror("popen failed");
190 while ((read = getline(&line, &n, fp)) != -1 && plt_not_found == 1) {
195 /* Wipeout the new line character */
196 line[read - 1] = '\0';
198 lfields[0] = strtok(line, " ");
200 if(lfields[0] == NULL)
203 if(strcmp(lfields[0], "Sections:") == 0 || strcmp(lfields[0], "Idx") == 0 || strcmp(lfields[0], "libsimgrid.so:") == 0)
206 for (i = 1; i < 7 && lfields[i - 1] != NULL; i++) {
207 lfields[i] = strtok(NULL, " ");
211 if(strcmp(lfields[1], ".plt") == 0){
212 size = strtoul(lfields[2], NULL, 16);
213 offset = strtoul(lfields[5], NULL, 16);
214 start_plt_libsimgrid = (char *)start_text_libsimgrid + offset;
215 end_plt_libsimgrid = (char *)start_plt_libsimgrid + size;
229 void get_binary_plt_section(){
232 char *line = NULL; /* Temporal storage for each line that is readed */
233 ssize_t read; /* Number of bytes readed */
234 size_t n = 0; /* Amount of bytes to read by getline */
237 int i, plt_not_found = 1;
238 unsigned long int size, offset;
240 char *command = bprintf( "objdump --section-headers %s", xbt_binary_name);
242 fp = popen(command, "r");
245 perror("popen failed");
247 while ((read = getline(&line, &n, fp)) != -1 && plt_not_found == 1) {
252 /* Wipeout the new line character */
253 line[read - 1] = '\0';
255 lfields[0] = strtok(line, " ");
257 if(lfields[0] == NULL)
260 if(strcmp(lfields[0], "Sections:") == 0 || strcmp(lfields[0], "Idx") == 0 || strcmp(lfields[0], basename(xbt_binary_name)) == 0)
263 for (i = 1; i < 7 && lfields[i - 1] != NULL; i++) {
264 lfields[i] = strtok(NULL, " ");
268 if(strcmp(lfields[1], ".plt") == 0){
269 size = strtoul(lfields[2], NULL, 16);
270 offset = strtoul(lfields[5], NULL, 16);
271 start_plt_binary = (char *)start_text_binary + offset;
272 end_plt_binary = (char *)start_plt_binary + size;
286 static void add_value(xbt_dynar_t *list, const char *type, unsigned long int val){
287 variable_value_t value = xbt_new0(s_variable_value_t, 1);
288 value->type = strdup(type);
289 if(strcmp(type, "value") == 0){
290 value->value.res = val;
292 value->value.address = (void *)val;
294 xbt_dynar_push(*list, &value);
297 static xbt_dynar_t take_snapshot_stacks(void *heap){
299 xbt_dynar_t res = xbt_dynar_new(sizeof(s_mc_snapshot_stack_t), NULL);
301 unsigned int cursor1 = 0;
302 stack_region_t current_stack;
304 xbt_dynar_foreach(stacks_areas, cursor1, current_stack){
305 mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
306 st->local_variables = get_local_variables_values(current_stack->context, heap);
307 st->stack_pointer = get_stack_pointer(current_stack, heap);
308 xbt_dynar_push(res, &st);
315 static void *get_stack_pointer(void *stack_context, void *heap){
321 ret = unw_init_local(&c, (unw_context_t *)stack_context);
323 XBT_INFO("unw_init_local failed");
327 unw_get_reg(&c, UNW_REG_SP, &sp);
329 return ((char *)heap + (size_t)(((char *)((long)sp) - (char*)std_heap)));
333 static xbt_strbuff_t get_local_variables_values(void *stack_context, void *heap){
339 char buf[512], frame_name[256];
341 ret = unw_init_local(&c, (unw_context_t *)stack_context);
343 XBT_INFO("unw_init_local failed");
347 //stack_name = strdup(((smx_process_t)((smx_ctx_sysv_t)(stack->address))->super.data)->name);
349 unw_word_t ip, sp, off;
352 xbt_dynar_t compose = xbt_dynar_new(sizeof(variable_value_t), NULL);
354 xbt_strbuff_t variables = xbt_strbuff_new();
355 xbt_dict_cursor_t dict_cursor;
357 dw_local_variable_t current_variable;
358 unsigned int cursor = 0, cursor2 = 0;
359 dw_location_entry_t entry = NULL;
360 dw_location_t location_entry = NULL;
363 void *frame_pointer_address = NULL;
368 unw_get_reg(&c, UNW_REG_IP, &ip);
369 unw_get_reg(&c, UNW_REG_SP, &sp);
372 if (unw_get_proc_name (&c, frame_name, sizeof (frame_name), &off) == 0){
374 snprintf (buf, sizeof (buf), "<%s+0x%lx>", frame_name, (long) off);
376 snprintf (buf, sizeof (buf), "<%s>", frame_name);
380 xbt_strbuff_append(variables, bprintf("ip=%-32s\n", buf));
382 frame = xbt_dict_get_or_null(mc_local_variables, frame_name);
389 true_ip = (long)frame->low_pc + (long)off;
391 /* Get frame pointer */
392 switch(frame->frame_base->type){
394 while((cursor < xbt_dynar_length(frame->frame_base->location.loclist)) && frame_found == 0){
395 entry = xbt_dynar_get_as(frame->frame_base->location.loclist, cursor, dw_location_entry_t);
396 if((true_ip >= entry->lowpc) && (true_ip < entry->highpc)){
398 switch(entry->location->type){
400 xbt_dynar_reset(compose);
402 while(cursor2 < xbt_dynar_length(entry->location->location.compose)){
403 location_entry = xbt_dynar_get_as(entry->location->location.compose, cursor2, dw_location_t);
404 switch(location_entry->type){
406 unw_get_reg(&c, location_entry->location.reg, &res);
407 add_value(&compose, "address", (long)res);
409 case e_dw_bregister_op:
410 unw_get_reg(&c, location_entry->location.breg_op.reg, &res);
411 add_value(&compose, "address", (long)res + location_entry->location.breg_op.offset);
414 xbt_dynar_reset(compose);
420 if(xbt_dynar_length(compose) > 0){
421 frame_pointer_address = xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.address ;
425 frame_pointer_address = NULL;
433 frame_pointer_address = NULL;
440 //XBT_INFO("Frame %s", frame->name);
442 xbt_dict_foreach(frame->variables, dict_cursor, variable_name, current_variable){
443 if(current_variable->location != NULL){
444 switch(current_variable->location->type){
446 xbt_dynar_reset(compose);
448 while(cursor < xbt_dynar_length(current_variable->location->location.compose)){
449 location_entry = xbt_dynar_get_as(current_variable->location->location.compose, cursor, dw_location_t);
450 switch(location_entry->type){
452 unw_get_reg(&c, location_entry->location.reg, &res);
453 add_value(&compose, "value", (long)res);
455 case e_dw_bregister_op:
456 unw_get_reg(&c, location_entry->location.breg_op.reg, &res);
457 add_value(&compose, "address", (long)res + location_entry->location.breg_op.offset);
459 case e_dw_fbregister_op:
460 if(frame_pointer_address != NULL)
461 add_value(&compose, "address", (long)((char *)frame_pointer_address + location_entry->location.fbreg_op));
464 xbt_dynar_reset(compose);
470 if(xbt_dynar_length(compose) > 0){
471 if(strcmp(xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->type, "value") == 0){
472 //XBT_INFO("Variable : %s - value : %lx", current_variable->name, xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.res);
473 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));
475 if(*((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1,variable_value_t)->value.address) == NULL){
476 //XBT_INFO("Variable : %s - address : NULL", current_variable->name);
477 xbt_strbuff_append(variables, bprintf("%s=NULL\n", current_variable->name));
478 }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)){
479 //XBT_INFO("Variable : %s - value : %d", current_variable->name, (int)(long long int)*((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.address));
480 xbt_strbuff_append(variables, bprintf("%s=%d\n", current_variable->name, (int)(long long int)*((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.address)));
482 //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));
483 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)));
487 //XBT_INFO("Variable %s undefined", current_variable->name);
488 xbt_strbuff_append(variables, bprintf("%s=undefined\n", current_variable->name));
495 //XBT_INFO("Variable : %s, no location", current_variable->name);
496 xbt_strbuff_append(variables, bprintf("%s=undefined\n", current_variable->name));
512 static void print_local_variables_values(xbt_dynar_t all_variables){
515 mc_snapshot_stack_t stack;
517 xbt_dynar_foreach(all_variables, cursor, stack){
518 XBT_INFO("%s", stack->local_variables->data);