6 #include "snapshot-interface.h"
10 #include "stl-model.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 MAPFILE "/proc/self/maps"
17 struct snapshot_entry {
18 snapshot_entry(snapshot_id id, int idx) : snapshotid(id), index(idx) { }
19 snapshot_id snapshotid;
26 int backTrackBeforeStep(int seq_index);
27 void snapshotStep(int seq_index);
31 ModelVector<struct snapshot_entry> stack;
34 static SnapshotStack *snap_stack;
37 /** The SnapshotGlobalSegments function computes the memory regions
38 * that may contain globals and then configures the snapshotting
39 * library to snapshot them.
41 static void SnapshotGlobalSegments()
44 char buf[9000], execname[100];
47 sprintf(execname, "vmmap -interleaved %d", pid);
48 map = popen(execname, "r");
55 /* Wait for correct part */
56 while (fgets(buf, sizeof(buf), map)) {
57 if (strstr(buf, "==== regions for process"))
61 while (fgets(buf, sizeof(buf), map)) {
62 char regionname[200] = "";
69 //Skip out at the end of the section
73 sscanf(buf, "%22s %p-%p", type, &begin, &end);
75 char * secondpart = strstr(buf, "]");
77 sscanf(&secondpart[2], "%c%c%c/%c%c%c SM=%3s %200s\n", &r, &w, &x, &mr, &mw, &mx, smstr, regionname);
78 if (w == 'w' && strstr(regionname, MYBINARYNAME)) {
79 size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
81 snapshot_add_memory_region(begin, len);
88 static void get_binary_name(char *buf, size_t len)
90 ssize_t size = readlink("/proc/self/exe", buf, len);
96 /* Terminate string */
97 if ((size_t)size > len)
102 /** The SnapshotGlobalSegments function computes the memory regions
103 * that may contain globals and then configures the snapshotting
104 * library to snapshot them.
106 static void SnapshotGlobalSegments()
109 char binary_name[800];
112 map = fopen(MAPFILE, "r");
117 get_binary_name(binary_name, sizeof(binary_name));
118 while (fgets(buf, sizeof(buf), map)) {
119 char regionname[200] = "";
123 sscanf(buf, "%p-%p %c%c%c%c %*x %*x:%*x %*u %200s\n", &begin, &end, &r, &w, &x, &p, regionname);
124 if (w == 'w' && strstr(regionname, binary_name)) {
125 size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
127 snapshot_add_memory_region(begin, len);
128 DEBUG("%55s: %18p - %18p\t%c%c%c%c\n", regionname, begin, end, r, w, x, p);
135 /** This method returns to the last snapshot before the inputted
136 * sequence number. This function must be called from the model
137 * checking thread and not from a snapshotted stack.
138 * @param seqindex is the sequence number to rollback before.
139 * @return is the sequence number we actually rolled back to.
141 int SnapshotStack::backTrackBeforeStep(int seqindex)
144 for (i = (int)stack.size() - 1; i >= 0; i++)
145 if (stack[i].index <= seqindex)
151 snapshot_roll_back(stack[i].snapshotid);
152 return stack[i].index;
155 /** This method takes a snapshot at the given sequence number. */
156 void SnapshotStack::snapshotStep(int seqindex)
158 stack.push_back(snapshot_entry(take_snapshot(), seqindex));
161 void snapshot_stack_init()
163 snap_stack = new SnapshotStack();
164 SnapshotGlobalSegments();
167 void snapshot_record(int seq_index)
169 snap_stack->snapshotStep(seq_index);
172 int snapshot_backtrack_before(int seq_index)
174 return snap_stack->backTrackBeforeStep(seq_index);