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