extra file committed accidentally
[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                 }
67         }
68         pclose(map);
69 }
70 #else
71 /** The SnapshotGlobalSegments function computes the memory regions
72  *      that may contain globals and then configures the snapshotting
73  *      library to snapshot them.
74  */
75 static void SnapshotGlobalSegments(){
76         int pid = getpid();
77         char buf[9000], filename[100];
78         FILE *map;
79
80         sprintf(filename, MAPFILE_FORMAT, pid);
81         map = fopen(filename, "r");
82         if (!map) {
83                 perror("fopen");
84                 exit(EXIT_FAILURE);
85         }
86         while (fgets(buf, sizeof(buf), map)) {
87                 char regionname[200] = "";
88                 char r, w, x, p;
89                 void *begin, *end;
90
91                 sscanf(buf, "%p-%p %c%c%c%c %*x %*x:%*x %*u %200s\n", &begin, &end, &r, &w, &x, &p, regionname);
92                 if (w == 'w' && (strstr(regionname, MYBINARYNAME) || strstr(regionname, MYLIBRARYNAME))) {
93                         size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
94                         if (len != 0)
95                                 addMemoryRegionToSnapShot(begin, len);
96                         DEBUG("%55s: %18p - %18p\t%c%c%c%c\n", regionname, begin, end, r, w, x, p);
97                 }
98         }
99         fclose(map);
100 }
101 #endif
102
103 SnapshotStack::SnapshotStack(){
104         SnapshotGlobalSegments();
105         stack=NULL;
106 }
107
108 SnapshotStack::~SnapshotStack(){
109 }
110
111
112 /** This method returns to the last snapshot before the inputted
113  * sequence number.  This function must be called from the model
114  * checking thread and not from a snapshotted stack.
115  * @param seqindex is the sequence number to rollback before.
116  * @return is the sequence number we actually rolled back to.
117  */
118 int SnapshotStack::backTrackBeforeStep(int seqindex) {
119         while(true) {
120                 if (stack->index<=seqindex) {
121                         //have right entry
122                         rollBack(stack->snapshotid);
123                         return stack->index;
124                 }
125                 struct stackEntry *tmp=stack;
126                 stack=stack->next;
127                 model_free(tmp);
128         }
129 }
130
131 /** This method takes a snapshot at the given sequence number. */
132 void SnapshotStack::snapshotStep(int seqindex) {
133         struct stackEntry *tmp=(struct stackEntry *)model_malloc(sizeof(struct stackEntry));
134         tmp->next=stack;
135         tmp->index=seqindex;
136         tmp->snapshotid=takeSnapshot();
137         stack=tmp;
138 }