cece58d0d10240c813b1be515ddc1e33825a7a47
[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 <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                         printf("%s\n", buf);
60                 
61                         size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
62                         if (len != 0)
63                                 addMemoryRegionToSnapShot(begin, len);
64                         DEBUG("%45s: %18p - %18p\t%c%c%c%c\n", regionname, begin, end, r, w, x, p);
65                 }
66         }
67         pclose(map);
68 }
69 #else
70 void SnapshotGlobalSegments(){
71         int pid = getpid();
72         char buf[9000], filename[100];
73         FILE *map;
74
75         sprintf(filename, MAPFILE_FORMAT, pid);
76         map = fopen(filename, "r");
77         if (!map) {
78                 perror("fopen");
79                 exit(EXIT_FAILURE);
80         }
81         while (fgets(buf, sizeof(buf), map)) {
82                 char regionname[200] = "";
83                 char r, w, x, p;
84                 void *begin, *end;
85
86                 sscanf(buf, "%p-%p %c%c%c%c %*x %*x:%*x %*u %200s\n", &begin, &end, &r, &w, &x, &p, regionname);
87                 if (w == 'w' && (strstr(regionname, MYBINARYNAME) || strstr(regionname, MYLIBRARYNAME))) {
88                         size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
89                         if (len != 0)
90                                 addMemoryRegionToSnapShot(begin, len);
91                         DEBUG("%45s: %18p - %18p\t%c%c%c%c\n", regionname, begin, end, r, w, x, p);
92                 }
93         }
94         fclose(map);
95 }
96 #endif
97
98 //class definition of SnapshotStack.....
99 //declaration of constructor....
100 SnapshotStack::SnapshotStack(){
101         SnapshotGlobalSegments();
102         stack=NULL;
103 }
104
105 SnapshotStack::~SnapshotStack(){
106 }
107
108 int SnapshotStack::backTrackBeforeStep(int seqindex) {
109         while(true) {
110                 if (stack->index<=seqindex) {
111                         //have right entry
112                         rollBack(stack->snapshotid);
113                         return stack->index;
114                 }
115                 struct stackEntry *tmp=stack;
116                 MYFREE(tmp);
117                 stack=stack->next;
118         }
119 }
120
121 void SnapshotStack::snapshotStep(int seqindex) {
122         struct stackEntry *tmp=(struct stackEntry *)MYMALLOC(sizeof(struct stackEntry));
123         tmp->next=stack;
124         tmp->index=seqindex;
125         tmp->snapshotid=takeSnapshot();
126         stack=tmp;
127 }