procps/pmap.c

373 lines
10 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-30 03:58:13 +05:30
#include <sys/ipc.h>
#include <sys/shm.h>
2004-01-27 01:31:56 +05:30
#include "proc/readproc.h"
#include "proc/version.h"
2004-01-30 03:58:13 +05:30
#include "proc/escape.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,
"Usage: pmap [-x | -d] [-q] [-A low,high] 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"
"-A limit results to the given range\n"
2002-02-02 04:17:29 +05:30
);
exit(1);
}
2002-10-27 16:05:13 +05:30
static unsigned KLONG range_low;
static unsigned KLONG range_high = ~0ull;
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
2004-01-30 03:58:13 +05:30
static unsigned shm_minor = ~0u;
static void discover_shm_minor(void){
void *addr;
int shmid;
char mapbuf[256];
if(!freopen("/proc/self/maps", "r", stdin)) return;
// create
shmid = shmget(IPC_PRIVATE, 42, IPC_CREAT | 0666);
if(shmid==-1) return; // failed; oh well
// attach
addr = shmat(shmid, NULL, SHM_RDONLY);
if(addr==(void*)-1) goto out_destroy;
2002-10-27 16:05:13 +05:30
2004-01-30 03:58:13 +05:30
while(fgets(mapbuf, sizeof mapbuf, stdin)){
char flags[32];
char *tmp; // to clean up unprintables
unsigned KLONG start, end;
unsigned long long file_offset, inode;
unsigned dev_major, dev_minor;
sscanf(mapbuf,"%"KLF"x-%"KLF"x %31s %Lx %x:%x %Lu", &start, &end, flags, &file_offset, &dev_major, &dev_minor, &inode);
tmp = strchr(mapbuf,'\n');
if(tmp) *tmp='\0';
tmp = mapbuf;
while(*tmp){
if(!isprint(*tmp)) *tmp='?';
tmp++;
}
if(start > (unsigned long)addr) continue;
if(dev_major) continue;
if(flags[3] != 's') continue;
if(strstr(mapbuf,"/SYSV")){
shm_minor = dev_minor;
break;
}
}
if(shmdt(addr)) perror("shmdt");
out_destroy:
if(shmctl(shmid, IPC_RMID, NULL)) perror("IPC_RMID");
return;
2002-10-27 16:05:13 +05:30
}
2004-01-30 03:58:13 +05:30
static const char *mapping_name(proc_t *p, unsigned KLONG addr, unsigned KLONG len, const char *mapbuf, unsigned showpath, unsigned dev_major, unsigned dev_minor, unsigned long long inode){
2004-01-30 03:58:13 +05:30
const char *cp;
if(!dev_major && dev_minor==shm_minor && strstr(mapbuf,"/SYSV")){
static char shmbuf[64];
snprintf(shmbuf, sizeof shmbuf, " [ shmid=0x%Lx ]", inode);
return shmbuf;
}
cp = strrchr(mapbuf,'/');
if(cp){
if(showpath) return strchr(mapbuf,'/');
return cp[1] ? cp+1 : cp;
2004-01-27 01:31:56 +05:30
}
2004-01-30 03:58:13 +05:30
2005-01-11 07:32:37 +05:30
cp = strchr(mapbuf,'/');
if(cp){
if(showpath) return cp;
return strrchr(cp,'/') + 1; // it WILL succeed
}
2004-01-30 03:58:13 +05:30
cp = " [ anon ]";
if( (p->start_stack >= addr) && (p->start_stack <= addr+len) ) cp = " [ stack ]";
2004-01-27 01:31:56 +05:30
return cp;
}
2002-10-27 16:05:13 +05:30
2004-01-30 03:58:13 +05:30
static int one_proc(proc_t *p){
2002-10-27 16:05:13 +05:30
char buf[32];
char mapbuf[9600];
2004-01-30 03:58:13 +05:30
char cmdbuf[512];
2002-10-27 16:05:13 +05:30
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
2004-11-05 02:20:59 +05:30
// Overkill, but who knows what is proper? The "w" prog
// uses the tty width to determine this.
int maxcmd = 0xfffff;
2004-01-30 03:58:13 +05:30
sprintf(buf,"/proc/%u/maps",p->tgid);
2002-10-27 16:05:13 +05:30
if(!freopen(buf, "r", stdin)) return 1;
2004-01-29 10:35:37 +05:30
2004-11-05 02:20:59 +05:30
escape_command(cmdbuf, p, sizeof cmdbuf, &maxcmd, ESC_ARGS|ESC_BRACKETS);
2004-01-30 03:58:13 +05:30
printf("%u: %s\n", p->tgid, cmdbuf);
if(!q_option && (x_option|d_option)){
if(x_option){
if(sizeof(KLONG)==4) printf("Address Kbytes RSS Anon Locked Mode Mapping\n");
else printf("Address Kbytes RSS Anon Locked Mode Mapping\n");
}
if(d_option){
if(sizeof(KLONG)==4) printf("Address Kbytes Mode Offset Device Mapping\n");
else printf("Address Kbytes Mode Offset Device Mapping\n");
}
}
2004-01-29 10:35:37 +05:30
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-30 03:58:13 +05:30
unsigned long long file_offset, inode;
2004-01-29 10:35:37 +05:30
unsigned dev_major, dev_minor;
2004-01-30 03:58:13 +05:30
sscanf(mapbuf,"%"KLF"x-%"KLF"x %31s %Lx %x:%x %Lu", &start, &end, flags, &file_offset, &dev_major, &dev_minor, &inode);
if(start > range_high)
break;
if(end < range_low)
continue;
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'){
2004-01-30 03:58:13 +05:30
flags[3] = '-';
2004-01-29 10:35:37 +05:30
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+
2004-01-30 03:58:13 +05:30
// an 'R' if swap not reserved (MAP_NORESERVE, SysV ISM shared mem, etc.)
flags[4] = '-';
flags[5] = '\0';
2002-10-27 16:05:13 +05:30
if(x_option){
const char *cp = mapping_name(p, start, diff, mapbuf, 0, dev_major, dev_minor, inode);
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 = mapping_name(p, start, diff, mapbuf, 0, dev_major, dev_minor, inode);
2004-01-29 10:35:37 +05:30
printf(
(sizeof(KLONG)==8)
2004-01-30 03:58:13 +05:30
? "%016"KLF"x %7lu %s %016Lx %03x:%05x %s\n"
: "%08lx %7lu %s %016Lx %03x:%05x %s\n",
2004-01-29 10:35:37 +05:30
start,
(unsigned long)(diff>>10),
flags,
file_offset,
dev_major, dev_minor,
cp
);
}
if(!x_option && !d_option){
const char *cp = mapping_name(p, start, diff, mapbuf, 1, dev_major, dev_minor, inode);
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
2004-01-30 03:58:13 +05:30
if(!q_option){
if(x_option){
if(sizeof(KLONG)==8){
printf("---------------- ------ ------ ------ ------\n");
printf(
"total kB %15ld - - -\n",
(total_shared + total_private_writeable + total_private_readonly) >> 10
);
}else{
printf("-------- ------- ------- ------- -------\n");
printf(
"total kB %7ld - - -\n",
(total_shared + total_private_writeable + total_private_readonly) >> 10
);
}
}
if(d_option){
printf(
2004-01-30 10:17:14 +05:30
"mapped: %ldK writeable/private: %ldK shared: %ldK\n",
2004-01-30 03:58:13 +05:30
(total_shared + total_private_writeable + total_private_readonly) >> 10,
total_private_writeable >> 10,
total_shared >> 10
);
}
if(!x_option && !d_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
}
2004-01-29 10:35:37 +05:30
}
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;
2004-01-30 03:58:13 +05:30
PROCTAB* PT;
proc_t p;
2002-10-27 16:05:13 +05:30
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;
case 'A':{
char *arg1;
if(walk[1]){
arg1 = walk+1;
walk += strlen(walk)-1;
}else{
arg1 = *++argv;
if(!arg1)
usage();
}
char *arg2 = strchr(arg1,',');
if(arg2)
*arg2 = '\0';
arg2 = arg2 ? arg2++ : arg1;
if(*arg1)
range_low = STRTOUKL(arg1,&arg1,16);
if(*arg2)
range_high = STRTOUKL(arg2,&arg2,16);
if(*arg1 || *arg2)
usage();
}
break;
case 'a': // Sun prints anon/swap reservations
case 'F': // Sun forces hostile ptrace-like grab
case 'l': // Sun shows unresolved dynamic names
case 'L': // Sun shows lgroup info
case 's': // Sun shows page sizes
case 'S': // Sun shows swap reservations
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
2004-01-30 03:58:13 +05:30
discover_shm_minor();
memset(&p, '\0', sizeof(p));
2004-01-30 03:58:13 +05:30
pidlist[count] = 0; // old libproc interface is zero-terminated
PT = openproc(PROC_FILLSTAT|PROC_FILLARG|PROC_PID, pidlist);
while(readproc(PT, &p)){
ret |= one_proc(&p);
count--;
}
closeproc(PT);
2002-10-27 16:05:13 +05:30
2004-01-30 03:58:13 +05:30
if(count) ret |= 42; // didn't find all processes asked for
2002-10-27 16:05:13 +05:30
return ret;
2002-02-02 04:17:29 +05:30
}