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