fix /proc/#/maps parse faliures, formatting cleanup

This commit is contained in:
Glenn Maynard
2004-01-02 04:13:43 +00:00
parent a9985795a4
commit eb63dc754f
+29 -32
View File
@@ -180,27 +180,27 @@ static int xtoi( const char *hex )
static int get_readable_ranges( const void **starts, const void **ends, int size ) static int get_readable_ranges( const void **starts, const void **ends, int size )
{ {
char path[PATH_MAX] = "/proc/"; char path[PATH_MAX] = "/proc/";
strcat( path, itoa(getpid()) ); strcat( path, itoa(getpid()) );
strcat( path, "/maps" ); strcat( path, "/maps" );
int fd = open(path, O_RDONLY); int fd = open(path, O_RDONLY);
if( fd == -1 ) if( fd == -1 )
return false; return false;
/* Format: /* Format:
* *
* 402dd000-402de000 rw-p 00010000 03:03 16815669 /lib/libnsl-2.3.1.so * 402dd000-402de000 rw-p 00010000 03:03 16815669 /lib/libnsl-2.3.1.so
* or * or
* bfffb000-c0000000 rwxp ffffc000 00:00 0 * bfffb000-c0000000 rwxp ffffc000 00:00 0
* *
* Look for the range that includes the stack pointer. */ * Look for the range that includes the stack pointer. */
char file[1024]; char file[1024];
int file_used = 0; int file_used = 0;
bool eof = false; bool eof = false;
int got = 0; int got = 0;
while(!eof && got < size-1 ) while( !eof && got < size-1 )
{ {
int ret = read( fd, file+file_used, sizeof(file) - file_used); int ret = read( fd, file+file_used, sizeof(file) - file_used);
if( ret < int(sizeof(file)) - file_used) if( ret < int(sizeof(file)) - file_used)
eof = true; eof = true;
@@ -215,42 +215,39 @@ static int get_readable_ranges( const void **starts, const void **ends, int size
break; break;
*p++ = 0; *p++ = 0;
char line[1024];
strcpy( line, file );
memmove(file, p, file_used);
file_used -= p-file;
/* Search for the hyphen. */ /* Search for the hyphen. */
char *hyphen = strchr( file, '-' ); char *hyphen = strchr( line, '-' );
if( hyphen == NULL ) if( hyphen == NULL )
{ continue; /* Parse error. */
/* Parse error. */
continue;
}
/* Search for the space. */ /* Search for the space. */
char *space = strchr( hyphen, ' ' ); char *space = strchr( hyphen, ' ' );
if( space == NULL ) if( space == NULL )
{ continue; /* Parse error. */
/* Parse error. */
continue;
}
/* " rwxp". If space[1] isn't 'r', then the block isn't readable. */ /* " rwxp". If space[1] isn't 'r', then the block isn't readable. */
if( strlen(space) < 2 || space[1] != 'r' ) if( strlen(space) < 2 || space[1] != 'r' )
continue; continue;
*starts++ = (const void *) xtoi( file ); *starts++ = (const void *) xtoi( line );
*ends++ = (const void *) xtoi( hyphen+1 ); *ends++ = (const void *) xtoi( hyphen+1 );
file_used -= p-file;
memmove(file, p, file_used);
} }
if( file_used == sizeof(file) ) if( file_used == sizeof(file) )
{ {
/* Line longer than the buffer. Weird; bail. */ /* Line longer than the buffer. Weird; bail. */
close(fd); break;
return false;
} }
} }
close(fd);
*starts++ = NULL; *starts++ = NULL;
*ends++ = NULL; *ends++ = NULL;
@@ -285,7 +282,7 @@ static void initialize_do_backtrace()
static void do_backtrace( void **buf, size_t size, bool ignore_before_sig = true ) static void do_backtrace( void **buf, size_t size, bool ignore_before_sig = true )
{ {
/* Read /proc/pid/maps to find the address range of the stack. */ /* Read /proc/pid/maps to find the address range of the stack. */
const void *readable_begin[1024], *readable_end[1024]; const void *readable_begin[1024], *readable_end[1024];
get_readable_ranges( readable_begin, readable_end, 1024 ); get_readable_ranges( readable_begin, readable_end, 1024 );
/* Find the stack memory blocks. */ /* Find the stack memory blocks. */