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