more documentation
[model-checker.git] / snapshot-interface.cc
1 #include "snapshot-interface.h"
2 #include "snapshot.h"
3 #include <iostream>
4 #include <fstream>
5 #include <unistd.h>
6 #include <sys/types.h>
7 #include <sstream>
8 #include <cstring>
9 #include <string>
10 #include <cassert>
11 #include <vector>
12 #include <utility>
13 #include <inttypes.h>
14 #include "common.h"
15
16
17 #define MYBINARYNAME "model"
18 #define MYLIBRARYNAME "libmodel.so"
19 #define MAPFILE_FORMAT "/proc/%d/maps"
20
21 SnapshotStack * snapshotObject;
22
23 #ifdef MAC
24 /** The SnapshotGlobalSegments function computes the memory regions
25  *      that may contain globals and then configures the snapshotting
26  *      library to snapshot them.
27  */
28
29 void SnapshotGlobalSegments(){
30         int pid = getpid();
31         char buf[9000], execname[100];
32         FILE *map;
33
34         sprintf(execname, "vmmap -interleaved %d", pid);
35         map=popen(execname, "r");
36
37         if (!map) {
38                 perror("popen");
39                 exit(EXIT_FAILURE);
40         }
41
42         /* Wait for correct part */
43         while (fgets(buf, sizeof(buf), map)) {
44                 if (strstr(buf, "==== regions for process"))
45                         break;
46         }
47
48         while (fgets(buf, sizeof(buf), map)) {
49                 char regionname[200] = "";
50                 char type[23];
51                 char smstr[23];
52                 char r, w, x;
53                 char mr, mw, mx;
54                 int size;
55                 void *begin, *end;
56
57                 //Skip out at the end of the section
58                 if (buf[0]=='\n')
59                         break;
60                 
61                 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
63                 if (w == 'w' && (strstr(regionname, MYBINARYNAME) || strstr(regionname, MYLIBRARYNAME))) {
64                         size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
65                         if (len != 0)
66                                 addMemoryRegionToSnapShot(begin, len);
67                         DEBUG("%s\n", buf);
68                         DEBUG("%45s: %18p - %18p\t%c%c%c%c\n", regionname, begin, end, r, w, x, p);
69                 }
70         }
71         pclose(map);
72 }
73 #else
74 /** The SnapshotGlobalSegments function computes the memory regions
75  *      that may contain globals and then configures the snapshotting
76  *      library to snapshot them.
77  */
78 void SnapshotGlobalSegments(){
79         int pid = getpid();
80         char buf[9000], filename[100];
81         FILE *map;
82
83         sprintf(filename, MAPFILE_FORMAT, pid);
84         map = fopen(filename, "r");
85         if (!map) {
86                 perror("fopen");
87                 exit(EXIT_FAILURE);
88         }
89         while (fgets(buf, sizeof(buf), map)) {
90                 char regionname[200] = "";
91                 char r, w, x, p;
92                 void *begin, *end;
93
94                 sscanf(buf, "%p-%p %c%c%c%c %*x %*x:%*x %*u %200s\n", &begin, &end, &r, &w, &x, &p, regionname);
95                 if (w == 'w' && (strstr(regionname, MYBINARYNAME) || strstr(regionname, MYLIBRARYNAME))) {
96                         size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
97                         if (len != 0)
98                                 addMemoryRegionToSnapShot(begin, len);
99                         DEBUG("%45s: %18p - %18p\t%c%c%c%c\n", regionname, begin, end, r, w, x, p);
100                 }
101         }
102         fclose(map);
103 }
104 #endif
105
106 SnapshotStack::SnapshotStack(){
107         SnapshotGlobalSegments();
108         stack=NULL;
109 }
110
111 SnapshotStack::~SnapshotStack(){
112 }
113
114
115 /** This method returns to the last snapshot before the inputted
116  * sequence number.  This function must be called from the model
117  * checking thread and not from a snapshotted stack.  
118  * @param seqindex is the sequence number to rollback before.  
119  * @return is the sequence number we actually rolled back to.
120  */
121                 
122 int SnapshotStack::backTrackBeforeStep(int seqindex) {
123         while(true) {
124                 if (stack->index<=seqindex) {
125                         //have right entry
126                         rollBack(stack->snapshotid);
127                         return stack->index;
128                 }
129                 struct stackEntry *tmp=stack;
130                 MYFREE(tmp);
131                 stack=stack->next;
132         }
133 }
134
135 /** This method takes a snapshot at the given sequence number.
136  */
137
138 void SnapshotStack::snapshotStep(int seqindex) {
139         struct stackEntry *tmp=(struct stackEntry *)MYMALLOC(sizeof(struct stackEntry));
140         tmp->next=stack;
141         tmp->index=seqindex;
142         tmp->snapshotid=takeSnapshot();
143         stack=tmp;
144 }