snapshot-interface: replace Subramanian's /proc/*/maps checking
[cdsspec-compiler.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
15 #include "common.h"
16
17 #define MYBINARYNAME "model"
18 #define MYLIBRARYNAME "libmodel.so"
19 #define MAPFILE_FORMAT "/proc/%d/maps"
20
21 SnapshotStack * snapshotObject;
22
23 void SnapshotGlobalSegments(){
24         int pid = getpid();
25         char buf[9000], filename[100];
26         FILE *map;
27
28         sprintf(filename, MAPFILE_FORMAT, pid);
29         map = fopen(filename, "r");
30         if (!map) {
31                 perror("fopen");
32                 exit(EXIT_FAILURE);
33         }
34         while (fgets(buf, sizeof(buf), map)) {
35                 char regionname[200] = "";
36                 char r, w, x, p;
37                 void *begin, *end;
38
39                 sscanf(buf, "%p-%p %c%c%c%c %*x %*x:%*x %*u %200s\n", &begin, &end, &r, &w, &x, &p, regionname);
40                 if (w == 'w' && (strstr(regionname, MYBINARYNAME) || strstr(regionname, MYLIBRARYNAME))) {
41                         size_t len = ((uintptr_t)end - (uintptr_t)begin) / PAGESIZE;
42                         if (len != 0)
43                                 addMemoryRegionToSnapShot(begin, len);
44                         DEBUG("%45s: %18p - %18p\t%c%c%c%c\n", regionname, begin, end, r, w, x, p);
45                 }
46         }
47         fclose(map);
48 }
49
50 //class definition of SnapshotStack.....
51 //declaration of constructor....
52 SnapshotStack::SnapshotStack(){
53         SnapshotGlobalSegments();
54         stack=NULL;
55 }
56
57 SnapshotStack::~SnapshotStack(){
58 }
59
60 int SnapshotStack::backTrackBeforeStep(int seqindex) {
61         while(true) {
62                 if (stack->index<=seqindex) {
63                         //have right entry
64                         rollBack(stack->snapshotid);
65                         return stack->index;
66                 }
67                 struct stackEntry *tmp=stack;
68                 MYFREE(tmp);
69                 stack=stack->next;
70         }
71 }
72
73 void SnapshotStack::snapshotStep(int seqindex) {
74         struct stackEntry *tmp=(struct stackEntry *)MYMALLOC(sizeof(struct stackEntry));
75         tmp->next=stack;
76         tmp->index=seqindex;
77         tmp->snapshotid=takeSnapshot();
78         stack=tmp;
79 }