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