Adding support to return global segments of the process using the /proc/maps
[cdsspec-compiler.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 typedef std::basic_stringstream< char, std::char_traits< char >, MyAlloc< char > > MyStringStream;
14 std::vector< MyString, MyAlloc< MyString> > splitString( MyString input, char delim ){
15         std::vector< MyString, MyAlloc< MyString > > splits;
16         MyStringStream ss( input );     
17         MyString item;
18         while( std::getline( ss, item, delim ) ){
19                 splits.push_back( item );       
20         }
21         return splits;
22 }
23
24 bool checkPermissions( MyString permStr ){
25         return permStr.find("w") != MyString::npos;
26 }
27 std::vector< std::pair< void *, size_t >, MyAlloc< std::pair< void *, size_t > > > snapshot_utils::ReturnGlobalSegmentsToSnapshot(){
28         std::vector< std::pair< void *, size_t >, MyAlloc< std::pair< void *, size_t > > >  theVec;
29         MyString fn = PROCNAME;
30         static char sProcessSize[ 12 ] = { 0 };
31         std::pair< const char *, bool > dataSect[ 3 ];
32         dataSect[ 0 ] = std::make_pair( MYBINARYNAME, false );
33         dataSect[ 1 ] = std::make_pair( MYLIBRARYNAME, false );
34         dataSect[ 2 ] = std::make_pair( MYALLOCNAME, false );
35         static pid_t sProcID = 0;
36         if( 0 == sProcID ) {
37                 sProcID = getpid();     
38                 sprintf( sProcessSize, "%d", sProcID );
39         }
40         fn.replace( REPLACEPOS, 1, sProcessSize );
41         std::ifstream procName( fn.c_str() );
42         if( procName.is_open() ){
43                 MyString line;
44                 while( procName.good() ){
45                         getline( procName, line );
46                         int i  = 0;
47                         for( i = 0; i < 3; ++i ){
48                                 if( MyString::npos != line.find( dataSect[ i ].first ) ) break;                 
49                         }
50                         if( i >= 3 || dataSect[ i ].second == true ) continue;
51                         dataSect[ i ].second = true;
52                         if( !procName.good() )return theVec;
53                         getline( procName, line );
54                         std::vector< MyString, MyAlloc< MyString > > firstSplit = splitString( line, ' ' );
55                         if( !checkPermissions( firstSplit[ 1 ]  ) ) continue;
56                         std::vector< MyString, MyAlloc< MyString > > secondSplit = splitString( firstSplit[ 0 ], '-' );
57                         size_t val1 = 0, val2 = 0;
58                         sscanf( secondSplit[ 0 ].c_str(), "%zx", &val1 );
59                         sscanf( secondSplit[ 1 ].c_str(), "%zx", &val2 );
60                         size_t len = ( val2 - val1 ) / PAGESIZE;
61                         theVec.push_back( std::make_pair( ( void * ) val1, len ) );
62                 }       
63         }
64         return theVec;
65 }