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;
20 void *start_data_libsimgrid;
22 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size);
23 static void MC_region_restore(mc_mem_region_t reg);
24 static void MC_region_destroy(mc_mem_region_t reg);
26 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size);
28 static void add_value(xbt_dynar_t *list, const char *type, unsigned long int val);
29 static xbt_dynar_t take_snapshot_stacks(void *heap);
30 static void get_local_variables_values(xbt_dynar_t *all_variables, stack_region_t stack, void *heap);
31 static void print_local_variables_values(xbt_dynar_t all_variables);
33 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
35 mc_mem_region_t new_reg = xbt_new0(s_mc_mem_region_t, 1);
37 new_reg->start_addr = start_addr;
39 new_reg->data = xbt_malloc0(size);
40 memcpy(new_reg->data, start_addr, size);
42 XBT_DEBUG("New region : type : %d, data : %p, size : %zu", type, new_reg->data, size);
47 static void MC_region_restore(mc_mem_region_t reg)
49 /*FIXME: check if start_addr is still mapped, if it is not, then map it
50 before copying the data */
52 memcpy(reg->start_addr, reg->data, reg->size);
57 static void MC_region_destroy(mc_mem_region_t reg)
63 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size)
65 mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
66 snapshot->regions = xbt_realloc(snapshot->regions, (snapshot->num_reg + 1) * sizeof(mc_mem_region_t));
67 snapshot->regions[snapshot->num_reg] = new_reg;
72 void MC_take_snapshot(mc_snapshot_t snapshot)
76 memory_map_t maps = get_memory_map();
78 /* Save the std heap and the writable mapped pages of libsimgrid */
79 while (i < maps->mapsize) {
80 reg = maps->regions[i];
81 if ((reg.prot & PROT_WRITE)){
82 if (maps->regions[i].pathname == NULL){
83 if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
84 MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
87 if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
88 MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
95 free_memory_map(maps);
98 void MC_take_snapshot_liveness(mc_snapshot_t snapshot)
102 memory_map_t maps = get_memory_map();
106 /* Save the std heap and the writable mapped pages of libsimgrid */
107 while (i < maps->mapsize && nb_reg < 3) {
108 reg = maps->regions[i];
109 if ((reg.prot & PROT_WRITE)){
110 if (maps->regions[i].pathname == NULL){
111 if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
112 MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
113 heap = snapshot->regions[nb_reg]->data;
117 if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
118 MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
120 start_data_libsimgrid = reg.start_addr;
122 if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
123 MC_snapshot_add_region(snapshot, 2, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
128 }else if ((reg.prot & PROT_READ)){
129 if (maps->regions[i].pathname != NULL){
130 if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
131 start_text_libsimgrid = reg.start_addr;
132 libsimgrid_path = strdup(maps->regions[i].pathname);
139 snapshot->stacks = take_snapshot_stacks(heap);
141 free_memory_map(maps);
145 void MC_restore_snapshot(mc_snapshot_t snapshot)
148 for(i=0; i < snapshot->num_reg; i++){
149 MC_region_restore(snapshot->regions[i]);
154 void MC_free_snapshot(mc_snapshot_t snapshot)
157 for(i=0; i < snapshot->num_reg; i++)
158 MC_region_destroy(snapshot->regions[i]);
164 void get_plt_section(){
167 char *line = NULL; /* Temporal storage for each line that is readed */
168 ssize_t read; /* Number of bytes readed */
169 size_t n = 0; /* Amount of bytes to read by getline */
172 int i, plt_not_found = 1;
173 unsigned long int size, offset;
175 char *command = bprintf( "objdump --section-headers %s", libsimgrid_path);
177 fp = popen(command, "r");
180 perror("popen failed");
182 while ((read = getline(&line, &n, fp)) != -1 && plt_not_found == 1) {
187 /* Wipeout the new line character */
188 line[read - 1] = '\0';
190 lfields[0] = strtok(line, " ");
192 if(lfields[0] == NULL)
195 if(strcmp(lfields[0], "Sections:") == 0 || strcmp(lfields[0], "Idx") == 0 || strcmp(lfields[0], "libsimgrid.so:") == 0)
198 for (i = 1; i < 7 && lfields[i - 1] != NULL; i++) {
199 lfields[i] = strtok(NULL, " ");
203 if(strcmp(lfields[1], ".plt") == 0){
204 size = strtoul(lfields[2], NULL, 16);
205 offset = strtoul(lfields[4], NULL, 16);
206 start_plt = (char *)start_text_libsimgrid + offset;
207 end_plt = (char *)start_plt + size;