1 #include "snapshot-interface.h"
15 /* MYBINARYNAME only works because our pathname usually includes 'model' (e.g.,
16 * /.../model-checker/test/userprog.o) */
17 #define MYBINARYNAME "model"
18 #define MYLIBRARYNAME "libmodel.so"
19 #define MAPFILE "/proc/self/maps"
21 SnapshotStack * snapshotObject;
24 /** The SnapshotGlobalSegments function computes the memory regions
25 * that may contain globals and then configures the snapshotting
26 * library to snapshot them.
28 static void SnapshotGlobalSegments(){
30 char buf[9000], execname[100];
33 sprintf(execname, "vmmap -interleaved %d", pid);
34 map=popen(execname, "r");
41 /* Wait for correct part */
42 while (fgets(buf, sizeof(buf), map)) {
43 if (strstr(buf, "==== regions for process"))
47 while (fgets(buf, sizeof(buf), map)) {
48 char regionname[200] = "";
56 //Skip out at the end of the section
60 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);
62 if (w == 'w' && (strstr(regionname, MYBINARYNAME) || strstr(regionname, MYLIBRARYNAME))) {
63 size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
65 addMemoryRegionToSnapShot(begin, len);
72 static void get_binary_name(char *buf, size_t len)
74 if (readlink("/proc/self/exe", buf, len) == -1) {
80 /** The SnapshotGlobalSegments function computes the memory regions
81 * that may contain globals and then configures the snapshotting
82 * library to snapshot them.
84 static void SnapshotGlobalSegments(){
86 char binary_name[800];
89 map = fopen(MAPFILE, "r");
94 get_binary_name(binary_name, sizeof(binary_name));
95 while (fgets(buf, sizeof(buf), map)) {
96 char regionname[200] = "";
100 sscanf(buf, "%p-%p %c%c%c%c %*x %*x:%*x %*u %200s\n", &begin, &end, &r, &w, &x, &p, regionname);
101 if (w == 'w' && (strstr(regionname, binary_name) || strstr(regionname, MYLIBRARYNAME))) {
102 size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
104 addMemoryRegionToSnapShot(begin, len);
105 DEBUG("%55s: %18p - %18p\t%c%c%c%c\n", regionname, begin, end, r, w, x, p);
112 SnapshotStack::SnapshotStack(){
113 SnapshotGlobalSegments();
117 SnapshotStack::~SnapshotStack(){
121 /** This method returns to the last snapshot before the inputted
122 * sequence number. This function must be called from the model
123 * checking thread and not from a snapshotted stack.
124 * @param seqindex is the sequence number to rollback before.
125 * @return is the sequence number we actually rolled back to.
127 int SnapshotStack::backTrackBeforeStep(int seqindex) {
129 if (stack->index<=seqindex) {
131 rollBack(stack->snapshotid);
134 struct stackEntry *tmp=stack;
140 /** This method takes a snapshot at the given sequence number. */
141 void SnapshotStack::snapshotStep(int seqindex) {
142 struct stackEntry *tmp=(struct stackEntry *)model_malloc(sizeof(struct stackEntry));
145 tmp->snapshotid=takeSnapshot();