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(){
25 char buf[9000], execname[100];
28 sprintf(execname, "vmmap -interleaved %d", pid);
29 map=popen(execname, "r");
36 /* Wait for correct part */
37 while (fgets(buf, sizeof(buf), map)) {
38 if (strstr(buf, "==== regions for process"))
42 while (fgets(buf, sizeof(buf), map)) {
43 char regionname[200] = "";
51 //Skip out at the end of the section
55 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);
57 if (w == 'w' && (strstr(regionname, MYBINARYNAME) || strstr(regionname, MYLIBRARYNAME))) {
58 size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
60 addMemoryRegionToSnapShot(begin, len);
67 static void get_binary_name(char *buf, size_t len)
69 if (readlink("/proc/self/exe", buf, len) == -1) {
75 /** The SnapshotGlobalSegments function computes the memory regions
76 * that may contain globals and then configures the snapshotting
77 * library to snapshot them.
79 static void SnapshotGlobalSegments(){
81 char binary_name[800];
84 map = fopen(MAPFILE, "r");
89 get_binary_name(binary_name, sizeof(binary_name));
90 while (fgets(buf, sizeof(buf), map)) {
91 char regionname[200] = "";
95 sscanf(buf, "%p-%p %c%c%c%c %*x %*x:%*x %*u %200s\n", &begin, &end, &r, &w, &x, &p, regionname);
96 if (w == 'w' && (strstr(regionname, binary_name) || strstr(regionname, MYLIBRARYNAME))) {
97 size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
99 addMemoryRegionToSnapShot(begin, len);
100 DEBUG("%55s: %18p - %18p\t%c%c%c%c\n", regionname, begin, end, r, w, x, p);
107 SnapshotStack::SnapshotStack(){
108 SnapshotGlobalSegments();
112 SnapshotStack::~SnapshotStack(){
116 /** This method returns to the last snapshot before the inputted
117 * sequence number. This function must be called from the model
118 * checking thread and not from a snapshotted stack.
119 * @param seqindex is the sequence number to rollback before.
120 * @return is the sequence number we actually rolled back to.
122 int SnapshotStack::backTrackBeforeStep(int seqindex) {
124 if (stack->index<=seqindex) {
126 rollBack(stack->snapshotid);
129 struct stackEntry *tmp=stack;
135 /** This method takes a snapshot at the given sequence number. */
136 void SnapshotStack::snapshotStep(int seqindex) {
137 struct stackEntry *tmp=(struct stackEntry *)model_malloc(sizeof(struct stackEntry));
140 tmp->snapshotid=takeSnapshot();