procps/pmap.c

284 lines
7.6 KiB
C
Raw Normal View History

2002-10-27 16:14:05 +05:30
/*
* Copyright 2002 by Albert Cahalan; all rights reserved.
2002-10-27 16:14:05 +05:30
* This file may be used subject to the terms and conditions of the
* GNU Library General Public License Version 2, or any later version
* at your option, as published by the Free Software Foundation.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*/
2002-02-02 04:17:29 +05:30
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
2002-10-27 16:05:13 +05:30
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
2004-01-27 01:31:56 +05:30
#include "proc/readproc.h"
#include "proc/version.h"
2002-02-02 04:17:29 +05:30
2002-12-09 13:23:09 +05:30
static void usage(void) NORETURN;
2002-10-27 16:05:13 +05:30
static void usage(void){
2002-02-02 04:17:29 +05:30
fprintf(stderr,
2004-01-29 10:35:37 +05:30
"Usage: pmap [-x | -d] [-q] pid...\n"
2002-02-02 04:17:29 +05:30
"-x show details\n"
2004-01-29 10:35:37 +05:30
"-d show offset and device number\n"
"-q quiet; less header/footer info\n"
"-V show the version number\n"
2002-02-02 04:17:29 +05:30
);
exit(1);
}
2002-10-27 16:05:13 +05:30
static int V_option;
static int r_option; // ignored -- for SunOS compatibility
static int x_option;
2004-01-29 09:03:44 +05:30
static int d_option;
static int q_option;
2002-10-27 16:05:13 +05:30
static const char *get_args(unsigned pid){
static char cmdbuf[64];
2002-02-02 04:17:29 +05:30
char buf[32];
int fd;
2002-10-27 16:05:13 +05:30
ssize_t count;
do{
sprintf(buf,"/proc/%u/cmdline",pid);
if( (( fd=open(buf,O_RDONLY) )) == -1) break;
count = read(fd, cmdbuf, sizeof(cmdbuf)-1);
close(fd);
if(count<1) break;
cmdbuf[count] = '\0';
if(!isprint(cmdbuf[0])) break;
while(count--) if(!isprint(cmdbuf[count])) cmdbuf[count]=' ';
return cmdbuf;
}while(0);
do{
char *cp;
sprintf(buf,"/proc/%u/stat",pid);
if( (( fd=open(buf,O_RDONLY) )) == -1) break;
count = read(fd, cmdbuf, sizeof(cmdbuf)-1);
close(fd);
if(count<1) break;
cmdbuf[count] = '\0';
while(count--) if(!isprint(cmdbuf[count])) cmdbuf[count]=' ';
cp = strrchr(cmdbuf,')');
if(!cp) break;
cp[0] = ']';
cp[1] = '\0';
cp = strchr(cmdbuf,'(');
if(!cp) break;
if(!isprint(cp[1])) break;
cp[0] = '[';
return cp;
}while(0);
return "[]"; // as good as anything
}
2004-01-27 01:31:56 +05:30
static const char *anon_name(int pid, unsigned KLONG addr, unsigned KLONG len){
const char *cp = " [ anon ]";
proc_t proc;
static int oldpid = -1;
if (pid==oldpid || get_proc_stats(pid, &proc)){
oldpid = pid;
2004-01-27 01:31:56 +05:30
if( (proc.start_stack >= addr) && (proc.start_stack <= addr+len) ) cp = " [ stack ]";
}
return cp;
}
2002-10-27 16:05:13 +05:30
static int one_proc(unsigned pid){
char buf[32];
char mapbuf[9600];
unsigned long total_shared = 0ul;
2004-01-29 10:35:37 +05:30
unsigned long total_private_readonly = 0ul;
unsigned long total_private_writeable = 0ul;
2002-10-27 16:05:13 +05:30
2002-02-02 04:17:29 +05:30
sprintf(buf,"/proc/%u/maps",pid);
2002-10-27 16:05:13 +05:30
if(!freopen(buf, "r", stdin)) return 1;
printf("%u: %s\n", pid, get_args(pid));
2004-01-29 10:35:37 +05:30
if(x_option && !q_option)
printf("Address Kbytes RSS Anon Locked Mode Mapping\n");
if(d_option && !q_option)
printf("Address Kbytes Mode Offset Device Mapping\n");
2002-10-27 16:05:13 +05:30
while(fgets(mapbuf,sizeof mapbuf,stdin)){
char flags[32];
char *tmp; // to clean up unprintables
2004-01-27 01:31:56 +05:30
unsigned KLONG start, end, diff;
2004-01-29 10:35:37 +05:30
unsigned long long file_offset;
unsigned dev_major, dev_minor;
sscanf(mapbuf,"%"KLF"x-%"KLF"x %31s %Lx %x:%x", &start, &end, flags, &file_offset, &dev_major, &dev_minor);
2002-10-27 16:05:13 +05:30
tmp = strchr(mapbuf,'\n');
if(tmp) *tmp='\0';
tmp = mapbuf;
while(*tmp){
if(!isprint(*tmp)) *tmp='?';
tmp++;
}
diff = end-start;
if(flags[3]=='s') total_shared += diff;
2004-01-29 10:35:37 +05:30
if(flags[3]=='p'){
if(flags[1]=='w') total_private_writeable += diff;
else total_private_readonly += diff;
}
2004-01-29 09:03:44 +05:30
// format used by Solaris 9 and procps-3.2.0+
if(flags[3] == 'p') flags[3] = '-';
flags[4] = '-'; // an 'R' if swap not reserved (MAP_NORESERVE, SysV ISM shared mem, etc.)
flags[5] = '\0';
2002-10-27 16:05:13 +05:30
if(x_option){
const char *cp = strrchr(mapbuf,'/');
if(cp && cp[1]) cp++;
2004-01-27 01:31:56 +05:30
if(!cp) cp = anon_name(pid, start, diff);
2002-10-27 16:05:13 +05:30
printf(
2004-01-27 01:31:56 +05:30
(sizeof(KLONG)==8)
2004-01-29 09:03:44 +05:30
? "%016"KLF"x %7lu - - - %s %s\n"
: "%08lx %7lu - - - %s %s\n",
2002-10-27 16:05:13 +05:30
start,
2004-01-27 01:31:56 +05:30
(unsigned long)(diff>>10),
2004-01-29 09:03:44 +05:30
flags,
2002-10-27 16:05:13 +05:30
cp
);
2004-01-29 10:35:37 +05:30
}
if(d_option){
const char *cp = strrchr(mapbuf,'/');
if(cp && cp[1]) cp++;
if(!cp) cp = anon_name(pid, start, diff);
printf(
(sizeof(KLONG)==8)
? "%016"KLF"x %7lu %s %016Lx %03x:%05x %s\n"
: "%08lx %7lu %s %016Lx %03x:%05x %s\n",
start,
(unsigned long)(diff>>10),
flags,
file_offset,
dev_major, dev_minor,
cp
);
}
if(!x_option && !d_option){
2002-10-27 16:05:13 +05:30
const char *cp = strchr(mapbuf,'/');
2004-01-27 01:31:56 +05:30
if(!cp) cp = anon_name(pid, start, diff);
2002-10-27 16:05:13 +05:30
printf(
2004-01-27 01:31:56 +05:30
(sizeof(KLONG)==8)
2004-01-29 09:03:44 +05:30
? "%016"KLF"x %6luK %s %s\n"
: "%08lx %6luK %s %s\n",
2002-10-27 16:05:13 +05:30
start,
2004-01-27 01:31:56 +05:30
(unsigned long)(diff>>10),
2004-01-29 09:03:44 +05:30
flags,
2002-10-27 16:05:13 +05:30
cp
);
}
}
2004-01-29 10:35:37 +05:30
if(x_option && !q_option){
2004-01-27 01:31:56 +05:30
if(sizeof(KLONG)==8){
2002-10-27 16:05:13 +05:30
printf("---------------- ------ ------ ------ ------\n");
printf(
2004-01-29 10:35:37 +05:30
"total kB %15ld - - -\n",
(total_shared + total_private_writeable + total_private_readonly) >> 10
2002-10-27 16:05:13 +05:30
);
}else{
2004-01-29 09:03:44 +05:30
printf("-------- ------- ------- ------- -------\n");
2002-10-27 16:05:13 +05:30
printf(
2004-01-29 09:03:44 +05:30
"total kB %7ld - - -\n",
2004-01-29 10:35:37 +05:30
(total_shared + total_private_writeable + total_private_readonly) >> 10
2002-10-27 16:05:13 +05:30
);
}
}
2004-01-29 10:35:37 +05:30
if(d_option && !q_option){
printf(
"mapped %ldK writeable/private: %ldK shared: %ldK\n",
(total_shared + total_private_writeable + total_private_readonly) >> 10,
total_private_writeable >> 10,
total_shared >> 10
);
}
if(!x_option && !d_option && !q_option){
if(sizeof(KLONG)==8) printf(" total %16ldK\n", (total_shared + total_private_writeable + total_private_readonly) >> 10);
else printf(" total %8ldK\n", (total_shared + total_private_writeable + total_private_readonly) >> 10);
}
2002-10-27 16:05:13 +05:30
return 0;
2002-02-02 04:17:29 +05:30
}
2002-10-27 16:05:13 +05:30
2002-02-02 04:17:29 +05:30
int main(int argc, char *argv[]){
2002-10-27 16:05:13 +05:30
unsigned *pidlist;
unsigned count = 0;
unsigned u;
int ret = 0;
if(argc<2) usage();
pidlist = malloc(sizeof(unsigned)*argc); // a bit more than needed perhaps
while(*++argv){
if(!strcmp("--version",*argv)){
V_option++;
continue;
}
if(**argv=='-'){
char *walk = *argv;
if(!walk[1]) usage();
while(*++walk){
switch(*walk){
case 'V':
V_option++;
break;
case 'x':
x_option++;
break;
case 'r':
r_option++;
break;
2004-01-29 09:03:44 +05:30
case 'd':
d_option++;
break;
case 'q':
q_option++;
break;
2002-10-27 16:05:13 +05:30
default:
usage();
}
}
}else{
char *walk = *argv;
char *endp;
unsigned long pid;
2004-01-27 01:31:56 +05:30
if(!strncmp("/proc/",walk,6)){
walk += 6;
// user allowed to do: pmap /proc/*
if(*walk<'0' || *walk>'9') continue;
}
2002-10-27 16:05:13 +05:30
if(*walk<'0' || *walk>'9') usage();
pid = strtoul(walk, &endp, 0);
if(pid<1ul || pid>0x7ffffffful || *endp) usage();
pidlist[count++] = pid;
}
}
2004-01-29 09:03:44 +05:30
if( (x_option|V_option|r_option|d_option|q_option) >> 1 ) usage(); // dupes
2002-10-27 16:05:13 +05:30
if(V_option){
2004-01-29 09:03:44 +05:30
if(count|x_option|r_option|d_option|q_option) usage();
2002-10-27 16:05:13 +05:30
fprintf(stdout, "pmap (%s)\n", procps_version);
return 0;
}
if(count<1) usage(); // no processes
2004-01-29 10:35:37 +05:30
if(d_option && x_option) usage();
2002-10-27 16:05:13 +05:30
u=0;
while(u<count) ret |= one_proc(pidlist[u++]);
return ret;
2002-02-02 04:17:29 +05:30
}