execution: refactor common CV propagation into its own function
[cdsspec-compiler.git] / snapshot-interface.cc
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <cstring>
4 #include <inttypes.h>
5
6 #include "snapshot-interface.h"
7 #include "snapshot.h"
8 #include "common.h"
9 #include "mymemory.h"
10 #include "stl-model.h"
11
12 /* MYBINARYNAME only works because our pathname usually includes 'model' (e.g.,
13  * /.../model-checker/test/userprog.o) */
14 #define MYBINARYNAME "model"
15 #define MAPFILE "/proc/self/maps"
16
17 struct snapshot_entry {
18         snapshot_entry(snapshot_id id, int idx) : snapshotid(id), index(idx) { }
19         snapshot_id snapshotid;
20         int index;
21         MEMALLOC
22 };
23
24 class SnapshotStack {
25  public:
26         int backTrackBeforeStep(int seq_index);
27         void snapshotStep(int seq_index);
28
29         MEMALLOC
30  private:
31         ModelVector<struct snapshot_entry> stack;
32 };
33
34 static SnapshotStack *snap_stack;
35
36 #ifdef MAC
37 /** The SnapshotGlobalSegments function computes the memory regions
38  *      that may contain globals and then configures the snapshotting
39  *      library to snapshot them.
40  */
41 static void SnapshotGlobalSegments()
42 {
43         int pid = getpid();
44         char buf[9000], execname[100];
45         FILE *map;
46
47         sprintf(execname, "vmmap -interleaved %d", pid);
48         map = popen(execname, "r");
49
50         if (!map) {
51                 perror("popen");
52                 exit(EXIT_FAILURE);
53         }
54
55         /* Wait for correct part */
56         while (fgets(buf, sizeof(buf), map)) {
57                 if (strstr(buf, "==== regions for process"))
58                         break;
59         }
60
61         while (fgets(buf, sizeof(buf), map)) {
62                 char regionname[200] = "";
63                 char type[23];
64                 char smstr[23];
65                 char r, w, x;
66                 char mr, mw, mx;
67                 int size;
68                 void *begin, *end;
69
70                 //Skip out at the end of the section
71                 if (buf[0] == '\n')
72                         break;
73
74                 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);
75
76                 if (w == 'w' && strstr(regionname, MYBINARYNAME)) {
77                         size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
78                         if (len != 0)
79                                 snapshot_add_memory_region(begin, len);
80                 }
81         }
82         pclose(map);
83 }
84 #else
85
86 static void get_binary_name(char *buf, size_t len)
87 {
88         ssize_t size = readlink("/proc/self/exe", buf, len);
89         if (size < 0) {
90                 perror("readlink");
91                 exit(EXIT_FAILURE);
92         }
93
94         /* Terminate string */
95         if ((size_t)size > len)
96                 size = len;
97         buf[size] = '\0';
98 }
99
100 /** The SnapshotGlobalSegments function computes the memory regions
101  *      that may contain globals and then configures the snapshotting
102  *      library to snapshot them.
103  */
104 static void SnapshotGlobalSegments()
105 {
106         char buf[9000];
107         char binary_name[800];
108         FILE *map;
109
110         map = fopen(MAPFILE, "r");
111         if (!map) {
112                 perror("fopen");
113                 exit(EXIT_FAILURE);
114         }
115         get_binary_name(binary_name, sizeof(binary_name));
116         while (fgets(buf, sizeof(buf), map)) {
117                 char regionname[200] = "";
118                 char r, w, x, p;
119                 void *begin, *end;
120
121                 sscanf(buf, "%p-%p %c%c%c%c %*x %*x:%*x %*u %200s\n", &begin, &end, &r, &w, &x, &p, regionname);
122                 if (w == 'w' && strstr(regionname, binary_name)) {
123                         size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
124                         if (len != 0)
125                                 snapshot_add_memory_region(begin, len);
126                         DEBUG("%55s: %18p - %18p\t%c%c%c%c\n", regionname, begin, end, r, w, x, p);
127                 }
128         }
129         fclose(map);
130 }
131 #endif
132
133 /** This method returns to the last snapshot before the inputted
134  * sequence number.  This function must be called from the model
135  * checking thread and not from a snapshotted stack.
136  * @param seqindex is the sequence number to rollback before.
137  * @return is the sequence number we actually rolled back to.
138  */
139 int SnapshotStack::backTrackBeforeStep(int seqindex)
140 {
141         int i;
142         for (i = (int)stack.size() - 1; i >= 0; i++)
143                 if (stack[i].index <= seqindex)
144                         break;
145                 else
146                         stack.pop_back();
147
148         ASSERT(i >= 0);
149         snapshot_roll_back(stack[i].snapshotid);
150         return stack[i].index;
151 }
152
153 /** This method takes a snapshot at the given sequence number. */
154 void SnapshotStack::snapshotStep(int seqindex)
155 {
156         stack.push_back(snapshot_entry(take_snapshot(), seqindex));
157 }
158
159 void snapshot_stack_init()
160 {
161         snap_stack = new SnapshotStack();
162         SnapshotGlobalSegments();
163 }
164
165 void snapshot_record(int seq_index)
166 {
167         snap_stack->snapshotStep(seq_index);
168 }
169
170 int snapshot_backtrack_before(int seq_index)
171 {
172         return snap_stack->backTrackBeforeStep(seq_index);
173 }