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, *end_plt;
19 char *libsimgrid_path;
21 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size);
22 static void MC_region_restore(mc_mem_region_t reg);
23 static void MC_region_destroy(mc_mem_region_t reg);
25 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size);
27 static void add_value(xbt_dynar_t *list, const char *type, unsigned long int val);
28 static xbt_dynar_t take_snapshot_stacks(void *heap);
29 static void get_local_variables_values(xbt_dynar_t *all_variables, stack_region_t stack, void *heap);
30 static void print_local_variables_values(xbt_dynar_t all_variables);
32 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
34 mc_mem_region_t new_reg = xbt_new0(s_mc_mem_region_t, 1);
36 new_reg->start_addr = start_addr;
38 new_reg->data = xbt_malloc0(size);
39 memcpy(new_reg->data, start_addr, size);
41 XBT_DEBUG("New region : type : %d, data : %p, size : %zu", type, new_reg->data, size);
46 static void MC_region_restore(mc_mem_region_t reg)
48 /*FIXME: check if start_addr is still mapped, if it is not, then map it
49 before copying the data */
51 memcpy(reg->start_addr, reg->data, reg->size);
56 static void MC_region_destroy(mc_mem_region_t reg)
62 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size)
64 mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
65 snapshot->regions = xbt_realloc(snapshot->regions, (snapshot->num_reg + 1) * sizeof(mc_mem_region_t));
66 snapshot->regions[snapshot->num_reg] = new_reg;
71 void MC_take_snapshot(mc_snapshot_t snapshot)
75 memory_map_t maps = get_memory_map();
77 /* Save the std heap and the writable mapped pages of libsimgrid */
78 while (i < maps->mapsize) {
79 reg = maps->regions[i];
80 if ((reg.prot & PROT_WRITE)){
81 if (maps->regions[i].pathname == NULL){
82 if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
83 MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
86 if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
87 MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
94 free_memory_map(maps);
97 void MC_take_snapshot_liveness(mc_snapshot_t snapshot)
101 memory_map_t maps = get_memory_map();
105 /* Save the std heap and the writable mapped pages of libsimgrid */
106 while (i < maps->mapsize && nb_reg < 3) {
107 reg = maps->regions[i];
108 if ((reg.prot & PROT_WRITE)){
109 if (maps->regions[i].pathname == NULL){
110 if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
111 MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
112 heap = snapshot->regions[nb_reg]->data;
116 if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
117 MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
120 if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
121 MC_snapshot_add_region(snapshot, 2, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
126 }else if ((reg.prot & PROT_READ)){
127 if (maps->regions[i].pathname != NULL){
128 if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
129 start_text_libsimgrid = reg.start_addr;
130 libsimgrid_path = strdup(maps->regions[i].pathname);
137 snapshot->stacks = take_snapshot_stacks(heap);
139 free_memory_map(maps);
143 void MC_restore_snapshot(mc_snapshot_t snapshot)
146 for(i=0; i < snapshot->num_reg; i++){
147 MC_region_restore(snapshot->regions[i]);
152 void MC_free_snapshot(mc_snapshot_t snapshot)
155 for(i=0; i < snapshot->num_reg; i++)
156 MC_region_destroy(snapshot->regions[i]);
162 void get_plt_section(){
165 char *line = NULL; /* Temporal storage for each line that is readed */
166 ssize_t read; /* Number of bytes readed */
167 size_t n = 0; /* Amount of bytes to read by getline */
170 int i, plt_not_found = 1;
171 unsigned long int size, offset;
173 char *command = bprintf( "objdump --section-headers %s", libsimgrid_path);
175 fp = popen(command, "r");
178 perror("popen failed");
180 while ((read = getline(&line, &n, fp)) != -1 && plt_not_found == 1) {
185 /* Wipeout the new line character */
186 line[read - 1] = '\0';
188 lfields[0] = strtok(line, " ");
190 if(lfields[0] == NULL)
193 if(strcmp(lfields[0], "Sections:") == 0 || strcmp(lfields[0], "Idx") == 0 || strcmp(lfields[0], "libsimgrid.so:") == 0)
196 for (i = 1; i < 7 && lfields[i - 1] != NULL; i++) {
197 lfields[i] = strtok(NULL, " ");
201 if(strcmp(lfields[1], ".plt") == 0){
202 size = strtoul(lfields[2], NULL, 16);
203 offset = strtoul(lfields[4], NULL, 16);
204 start_plt = (char *)start_text_libsimgrid + offset;
205 end_plt = (char *)start_plt + size;