Reading proc/(id)/maps

Put problems with the Android NDK in this forum.

Reading proc/(id)/maps

Postby paulscode » Sun Dec 12, 2010 8:37 pm

For some reason, it seems that the proc/(id)/maps is always empty, even if I access it from the program itself using the following code:
Code: Select all
char* execCmd( char* cmdline )
{
   __android_log_print( ANDROID_LOG_VERBOSE, "execCmd()", "Executing command '%s'", cmdline );
   strcpy( outBuffer, "" );
    FILE* pipe = popen( cmdline, "r");
    if( !pipe )
    {
        __android_log_print( ANDROID_LOG_VERBOSE, "execCmd()", "Failed to open a pipe" );
       return NULL;
    }
    if( !feof( pipe ) )
    {
        if( fgets( outBuffer, MAX_COMMAND_OUTPUT_LENGTH, pipe ) != NULL )
        {
           pclose( pipe );
            return (char*) &outBuffer;
        }
    }
    pclose(pipe);
    return NULL;
}

char* readProcname( char* pidStr )
{
   strcpy( cmdBuffer, "cat /proc/" );
   strcat( cmdBuffer, pidStr );
   strcat( cmdBuffer, "/cmdline" );
   return execCmd( (char*) &cmdBuffer );
}

int readMemoryMaps()
{
   char* procname = "paulscode.android.mupen64plus";
   char* procdir = "/proc";
   DIR* dip;
   struct dirent* dit;

   char* c;

   if( ( dip = opendir( procdir ) ) == NULL )
   {
       __android_log_print( ANDROID_LOG_ERROR, "readMemoryMaps()", "Unable to open directory stream for '%s'", procdir );
       return -1;
   }

   while( ( dit = readdir( dip ) ) != NULL )
   {
      c = readProcname( dit->d_name );
      if( c != NULL && strcmp( c, procname ) == 0 )
      {
         __android_log_print( ANDROID_LOG_VERBOSE, "readMemoryMaps()", "Found process '%s', pid='%s'", procname, dit->d_name );

         strcpy( cmdBuffer, "cat /proc/" );
         strcat( cmdBuffer, dit->d_name );
         strcat( cmdBuffer, "/maps > /sdcard/data/mp64pmapcontents.txt" );
         execCmd( cmdBuffer );

         return atoi( dit->d_name );
      }
   }
   return -1;
}

Output shows it finds the correct process id and map, and "cats" it to a file:
Code: Select all
12-12 14:11:42.123: VERBOSE/execCmd()(7372): Executing command 'cat /proc/7372/cmdline'
12-12 14:11:42.123: VERBOSE/readMemoryMaps()(7372): Found process 'paulscode.android.mupen64plus', pid='7372'
12-12 14:11:42.123: VERBOSE/execCmd()(7372): Executing command 'cat /proc/7372/maps > /sdcard/data/mp64pmapcontents.txt'


It creates the output file fine, but when I open it, it's empty. Another method I used (more hackish, but it verifies that "maps" is empty) was to stick an infinite loop in my program just before the line where it crashes, then pull up the terminal emulator on the phone and cat /proc/(id)/maps. This also results in no output.

There are a couple of things I can think of that might be causing this. Perhaps someone here might know:
1) proc/(id)/maps is only populated for statically-linked programs
2) It takes a long time for this file to be populated
3) The Android OS does not utilize this feature of Linux
4) I do not have the proper permissions to read the contents of this file
5) User error // probably the most likely reason

Are there other ways to determine where components of a shared library are loaded in memory in relation to each other?
paulscode
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Thu Nov 11, 2010 3:57 pm

Top

Return to NDK Problems

Who is online

Users browsing this forum: No registered users and 2 guests