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