Reading proc/(id)/maps

1,030 views
Skip to first unread message

Paul Lamb

unread,
Dec 12, 2010, 2:39:46 PM12/12/10
to android-ndk
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:

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:

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?

Chris Stratton

unread,
Dec 13, 2010, 12:42:04 AM12/13/10
to android-ndk
/proc/id/maps works fine. But why are you launching cat to do your
file access? You are writing a C program, just fopen() the thing and
have at it.

Start up the adb shell or a terminal program and it's shell if you
want to see what the file looks like - it's pretty standard linux
though, since it's a dynamically created by kernel code on access, and
I don't believe there are any android changes, or at least not any
drastic ones, to that part of the kernel.

For that matter, you should be able to test this code on a linux
desktop with next to no changes.

Paul Lamb

unread,
Dec 13, 2010, 9:16:40 PM12/13/10
to android-ndk
I ended up using /proc/self/maps to get what I was looking for rather
than the process id (I still don't know why that was empty). Anyway
you are right, my method was a bit overkill - it was just something I
threw together quickly, and since I already had the methods for
executing a command-line, "cat file > sdcard/file" was the quickest
hack to get there.

Chris Stratton

unread,
Dec 13, 2010, 11:40:54 PM12/13/10
to android-ndk
I would think cat /proc/self/maps would get the maps for cat, which is
presumably an invocation of toolbox, rather than your application's
maps.

Paul Lamb

unread,
Dec 14, 2010, 7:20:32 AM12/14/10
to android-ndk
No, I went with fopen() in that case ;)
Reply all
Reply to author
Forward
0 new messages