Changes needed to run on OS X... Example runs on my laptop now. No need to push...
[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 void SnapshotGlobalSegments(){
25         int pid = getpid();
26         char buf[9000], execname[100];
27         FILE *map;
28
29         sprintf(execname, "vmmap -interleaved %d", pid);
30         map=popen(execname, "r");
31
32         if (!map) {
33                 perror("popen");
34                 exit(EXIT_FAILURE);
35         }
36
37         /* Wait for correct part */
38         while (fgets(buf, sizeof(buf), map)) {
39                 if (strstr(buf, "==== regions for process"))
40                         break;
41         }
42
43         while (fgets(buf, sizeof(buf), map)) {
44                 char regionname[200] = "";
45                 char type[23];
46                 char smstr[23];
47                 char r, w, x;
48                 char mr, mw, mx;
49                 int size;
50                 void *begin, *end;
51
52                 //Skip out at the end of the section
53                 if (buf[0]=='\n')
54                         break;
55                 
56                 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);
57
58                 if (w == 'w' && (strstr(regionname, MYBINARYNAME) || strstr(regionname, MYLIBRARYNAME))) {
59                         size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
60                         if (len != 0)
61                                 addMemoryRegionToSnapShot(begin, len);
62                         DEBUG("%s\n", buf);
63                         DEBUG("%45s: %18p - %18p\t%c%c%c%c\n", regionname, begin, end, r, w, x, p);
64                 }
65         }
66         pclose(map);
67 }
68 #else
69 void SnapshotGlobalSegments(){
70         int pid = getpid();
71         char buf[9000], filename[100];
72         FILE *map;
73
74         sprintf(filename, MAPFILE_FORMAT, pid);
75         map = fopen(filename, "r");
76         if (!map) {
77                 perror("fopen");
78                 exit(EXIT_FAILURE);
79         }
80         while (fgets(buf, sizeof(buf), map)) {
81                 char regionname[200] = "";
82                 char r, w, x, p;
83                 void *begin, *end;
84
85                 sscanf(buf, "%p-%p %c%c%c%c %*x %*x:%*x %*u %200s\n", &begin, &end, &r, &w, &x, &p, regionname);
86                 if (w == 'w' && (strstr(regionname, MYBINARYNAME) || strstr(regionname, MYLIBRARYNAME))) {
87                         size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
88                         if (len != 0)
89                                 addMemoryRegionToSnapShot(begin, len);
90                         DEBUG("%45s: %18p - %18p\t%c%c%c%c\n", regionname, begin, end, r, w, x, p);
91                 }
92         }
93         fclose(map);
94 }
95 #endif
96
97 //class definition of SnapshotStack.....
98 //declaration of constructor....
99 SnapshotStack::SnapshotStack(){
100         SnapshotGlobalSegments();
101         stack=NULL;
102 }
103
104 SnapshotStack::~SnapshotStack(){
105 }
106
107 int SnapshotStack::backTrackBeforeStep(int seqindex) {
108         while(true) {
109                 if (stack->index<=seqindex) {
110                         //have right entry
111                         rollBack(stack->snapshotid);
112                         return stack->index;
113                 }
114                 struct stackEntry *tmp=stack;
115                 MYFREE(tmp);
116                 stack=stack->next;
117         }
118 }
119
120 void SnapshotStack::snapshotStep(int seqindex) {
121         struct stackEntry *tmp=(struct stackEntry *)MYMALLOC(sizeof(struct stackEntry));
122         tmp->next=stack;
123         tmp->index=seqindex;
124         tmp->snapshotid=takeSnapshot();
125         stack=tmp;
126 }