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] = "";
70 //Skip out at the end of the section
74 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);
76 if (w == 'w' && strstr(regionname, MYBINARYNAME)) {
77 size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
79 snapshot_add_memory_region(begin, len);
86 static void get_binary_name(char *buf, size_t len)
88 ssize_t size = readlink("/proc/self/exe", buf, len);
94 /* Terminate string */
95 if ((size_t)size > len)
100 /** The SnapshotGlobalSegments function computes the memory regions
101 * that may contain globals and then configures the snapshotting
102 * library to snapshot them.
104 static void SnapshotGlobalSegments()
107 char binary_name[800];
110 map = fopen(MAPFILE, "r");
115 get_binary_name(binary_name, sizeof(binary_name));
116 while (fgets(buf, sizeof(buf), map)) {
117 char regionname[200] = "";
121 sscanf(buf, "%p-%p %c%c%c%c %*x %*x:%*x %*u %200s\n", &begin, &end, &r, &w, &x, &p, regionname);
122 if (w == 'w' && strstr(regionname, binary_name)) {
123 size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
125 snapshot_add_memory_region(begin, len);
126 DEBUG("%55s: %18p - %18p\t%c%c%c%c\n", regionname, begin, end, r, w, x, p);
133 /** This method returns to the last snapshot before the inputted
134 * sequence number. This function must be called from the model
135 * checking thread and not from a snapshotted stack.
136 * @param seqindex is the sequence number to rollback before.
137 * @return is the sequence number we actually rolled back to.
139 int SnapshotStack::backTrackBeforeStep(int seqindex)
142 for (i = (int)stack.size() - 1; i >= 0; i++)
143 if (stack[i].index <= seqindex)
149 snapshot_roll_back(stack[i].snapshotid);
150 return stack[i].index;
153 /** This method takes a snapshot at the given sequence number. */
154 void SnapshotStack::snapshotStep(int seqindex)
156 stack.push_back(snapshot_entry(take_snapshot(), seqindex));
159 void snapshot_stack_init()
161 snap_stack = new SnapshotStack();
162 SnapshotGlobalSegments();
165 void snapshot_record(int seq_index)
167 snap_stack->snapshotStep(seq_index);
170 int snapshot_backtrack_before(int seq_index)
172 return snap_stack->backTrackBeforeStep(seq_index);