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