7 #include "snapshot-interface.h"
12 /* MYBINARYNAME only works because our pathname usually includes 'model' (e.g.,
13 * /.../model-checker/test/userprog.o) */
14 #define MYBINARYNAME "model"
15 #define MYLIBRARYNAME "libmodel.so"
16 #define MAPFILE "/proc/self/maps"
18 struct snapshot_entry {
19 snapshot_entry(snapshot_id id, int idx) : snapshotid(id), index(idx) { }
20 snapshot_id snapshotid;
27 int backTrackBeforeStep(int seq_index);
28 void snapshotStep(int seq_index);
32 std::vector<struct snapshot_entry, ModelAlloc<struct snapshot_entry> > stack;
35 static SnapshotStack *snap_stack;
38 /** The SnapshotGlobalSegments function computes the memory regions
39 * that may contain globals and then configures the snapshotting
40 * library to snapshot them.
42 static void SnapshotGlobalSegments()
45 char buf[9000], execname[100];
48 sprintf(execname, "vmmap -interleaved %d", pid);
49 map = popen(execname, "r");
56 /* Wait for correct part */
57 while (fgets(buf, sizeof(buf), map)) {
58 if (strstr(buf, "==== regions for process"))
62 while (fgets(buf, sizeof(buf), map)) {
63 char regionname[200] = "";
71 //Skip out at the end of the section
75 sscanf(buf, "%22s %p-%p [%5dK] %c%c%c/%c%c%c SM=%3s %200s\n", type, &begin, &end, &size, &r, &w, &x, &mr, &mw, &mx, smstr, regionname);
77 if (w == 'w' && (strstr(regionname, MYBINARYNAME) || strstr(regionname, MYLIBRARYNAME))) {
78 size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
80 snapshot_add_memory_region(begin, len);
87 static void get_binary_name(char *buf, size_t len)
89 if (readlink("/proc/self/exe", buf, len) == -1) {
95 /** The SnapshotGlobalSegments function computes the memory regions
96 * that may contain globals and then configures the snapshotting
97 * library to snapshot them.
99 static void SnapshotGlobalSegments()
102 char binary_name[800];
105 map = fopen(MAPFILE, "r");
110 get_binary_name(binary_name, sizeof(binary_name));
111 while (fgets(buf, sizeof(buf), map)) {
112 char regionname[200] = "";
116 sscanf(buf, "%p-%p %c%c%c%c %*x %*x:%*x %*u %200s\n", &begin, &end, &r, &w, &x, &p, regionname);
117 if (w == 'w' && (strstr(regionname, binary_name) || strstr(regionname, MYLIBRARYNAME))) {
118 size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
120 snapshot_add_memory_region(begin, len);
121 DEBUG("%55s: %18p - %18p\t%c%c%c%c\n", regionname, begin, end, r, w, x, p);
128 /** This method returns to the last snapshot before the inputted
129 * sequence number. This function must be called from the model
130 * checking thread and not from a snapshotted stack.
131 * @param seqindex is the sequence number to rollback before.
132 * @return is the sequence number we actually rolled back to.
134 int SnapshotStack::backTrackBeforeStep(int seqindex)
137 for (i = (int)stack.size() - 1; i >= 0; i++)
138 if (stack[i].index <= seqindex)
144 snapshot_roll_back(stack[i].snapshotid);
145 return stack[i].index;
148 /** This method takes a snapshot at the given sequence number. */
149 void SnapshotStack::snapshotStep(int seqindex)
151 stack.push_back(snapshot_entry(take_snapshot(), seqindex));
154 void snapshot_stack_init()
156 snap_stack = new SnapshotStack();
157 SnapshotGlobalSegments();
160 void snapshot_record(int seq_index)
162 snap_stack->snapshotStep(seq_index);
165 int snapshot_backtrack_before(int seq_index)
167 return snap_stack->backTrackBeforeStep(seq_index);