snapshot-interface: don't redefine PAGESIZE
[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
14 #define MYBINARYNAME "model"
15 #define MYLIBRARYNAME "libmodel.so"
16 #define PROCNAME      "/proc/*/maps"
17 #define REPLACEPOS              6
18
19 typedef std::basic_string<char, std::char_traits<char>, MyAlloc<char> > MyString;
20
21 SnapshotStack * snapshotObject;
22
23 /*This looks like it might leak memory...  Subramanian should fix this. */
24
25 typedef std::basic_stringstream< char, std::char_traits< char >, MyAlloc< char > > MyStringStream;
26 std::vector< MyString, MyAlloc< MyString> > splitString( MyString input, char delim ){
27         std::vector< MyString, MyAlloc< MyString > > splits;
28         MyStringStream ss( input );     
29         MyString item;
30         while( std::getline( ss, item, delim ) ){
31                 splits.push_back( item );       
32         }
33         return splits;
34 }
35
36 bool checkPermissions( MyString permStr ){
37         return permStr.find("w") != MyString::npos;
38 }
39 static void takeSegmentSnapshot( const MyString & lineText ){
40         std::vector< MyString, MyAlloc< MyString > > firstSplit = splitString( lineText, ' ' );
41         if( checkPermissions( firstSplit[ 1 ] ) ){
42                 std::vector< MyString, MyAlloc< MyString > > secondSplit = splitString( firstSplit[ 0 ], '-' );    
43                 size_t val1 = 0, val2 = 0;
44                 sscanf( secondSplit[ 0 ].c_str(), "%zx", &val1 );
45                 sscanf( secondSplit[ 1 ].c_str(), "%zx", &val2 );
46                 size_t len = ( val2 - val1 ) / PAGESIZE;    
47                 if( 0 != len ){
48                         addMemoryRegionToSnapShot( ( void * )val1, len );        
49                 }
50         }
51 }
52 void SnapshotGlobalSegments(){
53         MyString fn = PROCNAME;
54         static char sProcessSize[ 12 ] = { 0 };
55         std::pair< const char *, bool > dataSect[ 2 ];
56         dataSect[ 0 ] = std::make_pair( MYBINARYNAME, false );
57         dataSect[ 1 ] = std::make_pair( MYLIBRARYNAME, false );
58         static pid_t sProcID = 0;
59         if( 0 == sProcID ) {
60                 sProcID = getpid();     
61                 sprintf( sProcessSize, "%d", sProcID );
62         }
63         fn.replace( REPLACEPOS, 1, sProcessSize );
64         std::ifstream procName( fn.c_str() );
65         if( procName.is_open() ){
66                 MyString line;
67                 while( procName.good() ){
68                         getline( procName, line );
69                         int i;
70                         for( i = 0; i < 2; ++i ){
71                                 if( MyString::npos != line.find( dataSect[ i ].first ) ) break;                 
72                         }
73                         if( i >= 2 || dataSect[ i ].second == true ) continue;
74                         dataSect[ i ].second = true;
75                         if( !procName.good() )return;
76                         getline( procName, line );
77                         takeSegmentSnapshot( line );    
78                 }       
79         }
80 }
81
82 //class definition of SnapshotStack.....
83 //declaration of constructor....
84 SnapshotStack::SnapshotStack(){
85         SnapshotGlobalSegments();
86         stack=NULL;
87 }
88         
89 SnapshotStack::~SnapshotStack(){
90 }
91         
92 int SnapshotStack::backTrackBeforeStep(int seqindex) {
93         while(true) {
94                 if (stack->index<=seqindex) {
95                         //have right entry
96                         rollBack(stack->snapshotid);
97                         return stack->index;
98                 }
99                 struct stackEntry *tmp=stack;
100                 MYFREE(tmp);
101                 stack=stack->next;
102         }
103 }
104
105 void SnapshotStack::snapshotStep(int seqindex) {
106         struct stackEntry *tmp=(struct stackEntry *)MYMALLOC(sizeof(struct stackEntry));
107         tmp->next=stack;
108         tmp->index=seqindex;
109         tmp->snapshotid=takeSnapshot();
110         stack=tmp;
111 }