6 #include "snapshot-interface.h"
10 /* MYBINARYNAME only works because our pathname usually includes 'model' (e.g.,
11 * /.../model-checker/test/userprog.o) */
12 #define MYBINARYNAME "model"
13 #define MYLIBRARYNAME "libmodel.so"
14 #define MAPFILE "/proc/self/maps"
16 SnapshotStack * snapshotObject;
19 /** The SnapshotGlobalSegments function computes the memory regions
20 * that may contain globals and then configures the snapshotting
21 * library to snapshot them.
23 static void SnapshotGlobalSegments()
26 char buf[9000], execname[100];
29 sprintf(execname, "vmmap -interleaved %d", pid);
30 map = popen(execname, "r");
37 /* Wait for correct part */
38 while (fgets(buf, sizeof(buf), map)) {
39 if (strstr(buf, "==== regions for process"))
43 while (fgets(buf, sizeof(buf), map)) {
44 char regionname[200] = "";
52 //Skip out at the end of the section
56 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);
58 if (w == 'w' && (strstr(regionname, MYBINARYNAME) || strstr(regionname, MYLIBRARYNAME))) {
59 size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
61 snapshot_add_memory_region(begin, len);
68 static void get_binary_name(char *buf, size_t len)
70 if (readlink("/proc/self/exe", buf, len) == -1) {
76 /** The SnapshotGlobalSegments function computes the memory regions
77 * that may contain globals and then configures the snapshotting
78 * library to snapshot them.
80 static void SnapshotGlobalSegments()
83 char binary_name[800];
86 map = fopen(MAPFILE, "r");
91 get_binary_name(binary_name, sizeof(binary_name));
92 while (fgets(buf, sizeof(buf), map)) {
93 char regionname[200] = "";
97 sscanf(buf, "%p-%p %c%c%c%c %*x %*x:%*x %*u %200s\n", &begin, &end, &r, &w, &x, &p, regionname);
98 if (w == 'w' && (strstr(regionname, binary_name) || strstr(regionname, MYLIBRARYNAME))) {
99 size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
101 snapshot_add_memory_region(begin, len);
102 DEBUG("%55s: %18p - %18p\t%c%c%c%c\n", regionname, begin, end, r, w, x, p);
109 SnapshotStack::SnapshotStack()
111 SnapshotGlobalSegments();
115 SnapshotStack::~SnapshotStack()
120 /** This method returns to the last snapshot before the inputted
121 * sequence number. This function must be called from the model
122 * checking thread and not from a snapshotted stack.
123 * @param seqindex is the sequence number to rollback before.
124 * @return is the sequence number we actually rolled back to.
126 int SnapshotStack::backTrackBeforeStep(int seqindex)
129 if (stack->index <= seqindex) {
131 snapshot_roll_back(stack->snapshotid);
134 struct stackEntry *tmp = stack;
140 /** This method takes a snapshot at the given sequence number. */
141 void SnapshotStack::snapshotStep(int seqindex)
143 struct stackEntry *tmp = (struct stackEntry *)model_malloc(sizeof(struct stackEntry));
145 tmp->index = seqindex;
146 tmp->snapshotid = take_snapshot();