pmap: new usage & fix coding style

Coding style fixed and more readable help output.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2011-06-05 18:05:26 +02:00
parent 6dd092412f
commit d50884788d

399
pmap.c
View File

@ -9,94 +9,115 @@
* GNU Library General Public License for more details. * GNU Library General Public License for more details.
*/ */
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <sys/ipc.h> #include <sys/ipc.h>
#include <sys/shm.h> #include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "proc/escape.h"
#include "proc/readproc.h" #include "proc/readproc.h"
#include "proc/version.h" #include "proc/version.h"
#include "proc/escape.h"
static void usage(void) NORETURN; static void __attribute__ ((__noreturn__))
static void usage(void){ usage(FILE * out)
fprintf(stderr, {
"Usage: pmap [-x | -d] [-q] [-A low,high] pid...\n" fprintf(out,
"-x show details\n" "\nUsage: %s [options] pid [pid ...]\n"
"-d show offset and device number\n" "\nOptions:\n", program_invocation_short_name);
"-q quiet; less header/footer info\n" fprintf(out,
"-V show the version number\n" " -x, --extended show details\n"
"-A limit results to the given range\n" " -d, --device show the device format\n"
); " -q, --quiet do not display header and footer\n"
exit(1); " -A, --range=<low>[,<high>] limit results to the given range\n"
" -h, --help display this help text\n"
" -V, --version display version information and exit\n");
fprintf(out, "\nFor more information see pmap(1).\n");
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
} }
static unsigned KLONG range_low; static unsigned KLONG range_low;
static unsigned KLONG range_high = ~0ull; static unsigned KLONG range_high = ~0ull;
static int V_option;
static int r_option; // ignored -- for SunOS compatibility
static int x_option;
static int d_option; static int d_option;
static int q_option; static int q_option;
static int x_option;
static unsigned shm_minor = ~0u; static unsigned shm_minor = ~0u;
static void discover_shm_minor(void){ static void discover_shm_minor(void)
{
void *addr; void *addr;
int shmid; int shmid;
char mapbuf[256]; char mapbuf[256];
if(!freopen("/proc/self/maps", "r", stdin)) return; if (!freopen("/proc/self/maps", "r", stdin))
return;
// create /* create */
shmid = shmget(IPC_PRIVATE, 42, IPC_CREAT | 0666); shmid = shmget(IPC_PRIVATE, 42, IPC_CREAT | 0666);
if(shmid==-1) return; // failed; oh well if (shmid == -1)
// attach /* failed; oh well */
return;
/* attach */
addr = shmat(shmid, NULL, SHM_RDONLY); addr = shmat(shmid, NULL, SHM_RDONLY);
if(addr==(void*)-1) goto out_destroy; if (addr == (void *)-1)
goto out_destroy;
while (fgets(mapbuf, sizeof mapbuf, stdin)) { while (fgets(mapbuf, sizeof mapbuf, stdin)) {
char flags[32]; char flags[32];
char *tmp; // to clean up unprintables /* to clean up unprintables */
char *tmp;
unsigned KLONG start, end; unsigned KLONG start, end;
unsigned long long file_offset, inode; unsigned long long file_offset, inode;
unsigned dev_major, dev_minor; 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); 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'); tmp = strchr(mapbuf, '\n');
if(tmp) *tmp='\0'; if (tmp)
*tmp = '\0';
tmp = mapbuf; tmp = mapbuf;
while (*tmp) { while (*tmp) {
if(!isprint(*tmp)) *tmp='?'; if (!isprint(*tmp))
*tmp = '?';
tmp++; tmp++;
} }
if(start > (unsigned long)addr) continue; if (start > (unsigned long)addr)
if(dev_major) continue; continue;
if(flags[3] != 's') continue; if (dev_major)
continue;
if (flags[3] != 's')
continue;
if (strstr(mapbuf, "/SYSV")) { if (strstr(mapbuf, "/SYSV")) {
shm_minor = dev_minor; shm_minor = dev_minor;
break; break;
} }
} }
if(shmdt(addr)) perror("shmdt"); if (shmdt(addr))
perror("shmdt");
out_destroy: out_destroy:
if(shmctl(shmid, IPC_RMID, NULL)) perror("IPC_RMID"); if (shmctl(shmid, IPC_RMID, NULL))
perror("IPC_RMID");
return; return;
} }
static const char *mapping_name(proc_t * p, unsigned KLONG addr,
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){ unsigned KLONG len, const char *mapbuf,
unsigned showpath, unsigned dev_major,
unsigned dev_minor, unsigned long long inode)
{
const char *cp; const char *cp;
if (!dev_major && dev_minor == shm_minor && strstr(mapbuf, "/SYSV")) { if (!dev_major && dev_minor == shm_minor && strstr(mapbuf, "/SYSV")) {
@ -107,22 +128,27 @@ static const char *mapping_name(proc_t *p, unsigned KLONG addr, unsigned KLONG l
cp = strrchr(mapbuf, '/'); cp = strrchr(mapbuf, '/');
if (cp) { if (cp) {
if(showpath) return strchr(mapbuf,'/'); if (showpath)
return strchr(mapbuf, '/');
return cp[1] ? cp + 1 : cp; return cp[1] ? cp + 1 : cp;
} }
cp = strchr(mapbuf, '/'); cp = strchr(mapbuf, '/');
if (cp) { if (cp) {
if(showpath) return cp; if (showpath)
return strrchr(cp,'/') + 1; // it WILL succeed return cp;
/* it WILL succeed */
return strrchr(cp, '/') + 1;
} }
cp = " [ anon ]"; cp = " [ anon ]";
if( (p->start_stack >= addr) && (p->start_stack <= addr+len) ) cp = " [ stack ]"; if ((p->start_stack >= addr) && (p->start_stack <= addr + len))
cp = " [ stack ]";
return cp; return cp;
} }
static int one_proc(proc_t *p){ static int one_proc(proc_t * p)
{
char buf[32]; char buf[32];
char mapbuf[9600]; char mapbuf[9600];
char cmdbuf[512]; char cmdbuf[512];
@ -139,34 +165,47 @@ static int one_proc(proc_t *p){
unsigned long long total_private_dirty = 0ull; unsigned long long total_private_dirty = 0ull;
unsigned long long total_shared_dirty = 0ull; unsigned long long total_shared_dirty = 0ull;
// Overkill, but who knows what is proper? The "w" prog /* Overkill, but who knows what is proper? The "w" prog uses
// uses the tty width to determine this. * the tty width to determine this.
*/
int maxcmd = 0xfffff; int maxcmd = 0xfffff;
sprintf(buf, "/proc/%u/maps", p->tgid); sprintf(buf, "/proc/%u/maps", p->tgid);
if ( (fp = fopen(buf, "r")) == NULL) return 1; if ((fp = fopen(buf, "r")) == NULL)
return 1;
if (x_option) { if (x_option) {
sprintf(buf, "/proc/%u/smaps", p->tgid); sprintf(buf, "/proc/%u/smaps", p->tgid);
if ( (fp = freopen(buf, "r", fp)) == NULL) return 1; if ((fp = freopen(buf, "r", fp)) == NULL)
return 1;
} }
escape_command(cmdbuf, p, sizeof cmdbuf, &maxcmd, ESC_ARGS|ESC_BRACKETS); escape_command(cmdbuf, p, sizeof cmdbuf, &maxcmd,
ESC_ARGS | ESC_BRACKETS);
printf("%u: %s\n", p->tgid, cmdbuf); printf("%u: %s\n", p->tgid, cmdbuf);
if (!q_option && (x_option | d_option)) { if (!q_option && (x_option | d_option)) {
if (x_option) { if (x_option) {
if(sizeof(KLONG)==4) printf("Address Kbytes RSS Dirty Mode Mapping\n"); if (sizeof(KLONG) == 4)
else printf("Address Kbytes RSS Dirty Mode Mapping\n"); printf
("Address Kbytes RSS Dirty Mode Mapping\n");
else
printf
("Address Kbytes RSS Dirty Mode Mapping\n");
} }
if (d_option) { if (d_option) {
if(sizeof(KLONG)==4) printf("Address Kbytes Mode Offset Device Mapping\n"); if (sizeof(KLONG) == 4)
else printf("Address Kbytes Mode Offset Device Mapping\n"); printf
("Address Kbytes Mode Offset Device Mapping\n");
else
printf
("Address Kbytes Mode Offset Device Mapping\n");
} }
} }
while (fgets(mapbuf, sizeof mapbuf, fp)) { while (fgets(mapbuf, sizeof mapbuf, fp)) {
char flags[32]; char flags[32];
char *tmp; // to clean up unprintables /* to clean up unprintables */
char *tmp;
unsigned KLONG start, end, diff = 0; unsigned KLONG start, end, diff = 0;
unsigned long long file_offset, inode; unsigned long long file_offset, inode;
unsigned dev_major, dev_minor; unsigned dev_major, dev_minor;
@ -176,7 +215,9 @@ static int one_proc(proc_t *p){
/* hex values are lower case or numeric, keys are upper */ /* hex values are lower case or numeric, keys are upper */
if (mapbuf[0] >= 'A' && mapbuf[0] <= 'Z') { if (mapbuf[0] >= 'A' && mapbuf[0] <= 'Z') {
/* Its a key */ /* Its a key */
if (sscanf(mapbuf,"%20[^:]: %llu", smap_key, &smap_value) == 2) { if (sscanf
(mapbuf, "%20[^:]: %llu", smap_key,
&smap_value) == 2) {
if (strncmp("Rss", smap_key, 3) == 0) { if (strncmp("Rss", smap_key, 3) == 0) {
rss = smap_value; rss = smap_value;
total_rss += smap_value; total_rss += smap_value;
@ -192,27 +233,28 @@ static int one_proc(proc_t *p){
total_private_dirty += smap_value; total_private_dirty += smap_value;
continue; continue;
} }
if (strncmp("Swap", smap_key, 4) == 0) { /*doesnt matter as long as last*/ if (strncmp("Swap", smap_key, 4) == 0) {
printf( /*doesnt matter as long as last */
(sizeof(KLONG)==8) printf((sizeof(KLONG) == 8)
? "%016"KLF"x %7lu %7llu %7llu %s %s\n" ? "%016" KLF
: "%08lx %7lu %7llu %7llu %s %s\n", "x %7lu %7llu %7llu %s %s\n" :
"%08lx %7lu %7llu %7llu %s %s\n",
start, start,
(unsigned long)(diff>>10), (unsigned long)(diff >> 10), rss,
rss,
(private_dirty + shared_dirty), (private_dirty + shared_dirty),
flags, flags, cp2);
cp2
);
/* reset some counters */ /* reset some counters */
rss = shared_dirty = private_dirty = 0ull; rss = shared_dirty = private_dirty =
0ull;
continue; continue;
} }
/* Other keys */ /* Other keys */
continue; continue;
} }
} }
sscanf(mapbuf,"%"KLF"x-%"KLF"x %31s %Lx %x:%x %Lu", &start, &end, flags, &file_offset, &dev_major, &dev_minor, &inode); 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) if (start > range_high)
break; break;
@ -220,197 +262,218 @@ static int one_proc(proc_t *p){
continue; continue;
tmp = strchr(mapbuf, '\n'); tmp = strchr(mapbuf, '\n');
if(tmp) *tmp='\0'; if (tmp)
*tmp = '\0';
tmp = mapbuf; tmp = mapbuf;
while (*tmp) { while (*tmp) {
if(!isprint(*tmp)) *tmp='?'; if (!isprint(*tmp))
*tmp = '?';
tmp++; tmp++;
} }
diff = end - start; diff = end - start;
if(flags[3]=='s') total_shared += diff; if (flags[3] == 's')
total_shared += diff;
if (flags[3] == 'p') { if (flags[3] == 'p') {
flags[3] = '-'; flags[3] = '-';
if(flags[1]=='w') total_private_writeable += diff; if (flags[1] == 'w')
else total_private_readonly += diff; total_private_writeable += diff;
else
total_private_readonly += diff;
} }
/* format used by Solaris 9 and procps-3.2.0+ an 'R'
// format used by Solaris 9 and procps-3.2.0+ * if swap not reserved (MAP_NORESERVE, SysV ISM
// an 'R' if swap not reserved (MAP_NORESERVE, SysV ISM shared mem, etc.) * shared mem, etc.)
*/
flags[4] = '-'; flags[4] = '-';
flags[5] = '\0'; flags[5] = '\0';
if (x_option) { if (x_option) {
cp2 = mapping_name(p, start, diff, mapbuf, 0, dev_major, dev_minor, inode); cp2 =
mapping_name(p, start, diff, mapbuf, 0, dev_major,
dev_minor, inode);
/* printed with the keys */ /* printed with the keys */
continue; continue;
} }
if (d_option) { if (d_option) {
const char *cp = mapping_name(p, start, diff, mapbuf, 0, dev_major, dev_minor, inode); const char *cp =
printf( mapping_name(p, start, diff, mapbuf, 0, dev_major,
(sizeof(KLONG)==8) dev_minor, inode);
printf((sizeof(KLONG) == 8)
? "%016" KLF "x %7lu %s %016Lx %03x:%05x %s\n" ? "%016" KLF "x %7lu %s %016Lx %03x:%05x %s\n"
: "%08lx %7lu %s %016Lx %03x:%05x %s\n", : "%08lx %7lu %s %016Lx %03x:%05x %s\n",
start, start,
(unsigned long)(diff >> 10), (unsigned long)(diff >> 10),
flags, flags, file_offset, dev_major, dev_minor, cp);
file_offset,
dev_major, dev_minor,
cp
);
} }
if (!x_option && !d_option) { if (!x_option && !d_option) {
const char *cp = mapping_name(p, start, diff, mapbuf, 1, dev_major, dev_minor, inode); const char *cp =
printf( mapping_name(p, start, diff, mapbuf, 1, dev_major,
(sizeof(KLONG)==8) dev_minor, inode);
printf((sizeof(KLONG) == 8)
? "%016" KLF "x %6luK %s %s\n" ? "%016" KLF "x %6luK %s %s\n"
: "%08lx %6luK %s %s\n", : "%08lx %6luK %s %s\n",
start, start, (unsigned long)(diff >> 10), flags, cp);
(unsigned long)(diff>>10),
flags,
cp
);
} }
} }
if (!q_option) { if (!q_option) {
if (x_option) { if (x_option) {
if (sizeof(KLONG) == 8) { if (sizeof(KLONG) == 8) {
printf("---------------- ------ ------ ------\n"); printf
printf( ("---------------- ------ ------ ------\n");
"total kB %15ld %7llu %7llu\n", printf("total kB %15ld %7llu %7llu\n",
(total_shared + total_private_writeable + total_private_readonly) >> 10, (total_shared + total_private_writeable +
total_rss, (total_shared_dirty+total_private_dirty) total_private_readonly) >> 10,
total_rss,
(total_shared_dirty +
total_private_dirty)
); );
} else { } else {
printf("-------- ------- ------- ------- -------\n"); printf
printf( ("-------- ------- ------- ------- -------\n");
"total kB %7ld - - -\n", printf
(total_shared + total_private_writeable + total_private_readonly) >> 10 ("total kB %7ld - - -\n",
); (total_shared + total_private_writeable +
total_private_readonly) >> 10);
} }
} }
if (d_option) { if (d_option) {
printf( printf
"mapped: %ldK writeable/private: %ldK shared: %ldK\n", ("mapped: %ldK writeable/private: %ldK shared: %ldK\n",
(total_shared + total_private_writeable + total_private_readonly) >> 10, (total_shared + total_private_writeable +
total_private_writeable >> 10, total_private_readonly) >> 10,
total_shared >> 10 total_private_writeable >> 10, total_shared >> 10);
);
} }
if (!x_option && !d_option) { if (!x_option && !d_option) {
if(sizeof(KLONG)==8) printf(" total %16ldK\n", (total_shared + total_private_writeable + total_private_readonly) >> 10); if (sizeof(KLONG) == 8)
else printf(" total %8ldK\n", (total_shared + total_private_writeable + total_private_readonly) >> 10); 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);
} }
} }
return 0; return 0;
} }
int main(int argc, char **argv)
int main(int argc, char *argv[]){ {
unsigned *pidlist; unsigned *pidlist;
unsigned count = 0; unsigned count = 0;
PROCTAB *PT; PROCTAB *PT;
proc_t p; proc_t p;
int ret = 0; int ret = 0, c;
if(argc<2) usage(); static const struct option longopts[] = {
pidlist = malloc(sizeof(unsigned)*argc); // a bit more than needed perhaps {"extended", no_argument, NULL, 'x'},
{"device", no_argument, NULL, 'd'},
{"quiet", no_argument, NULL, 'q'},
{"range", required_argument, NULL, 'A'},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'V'},
{NULL, 0, NULL, 0}
};
while(*++argv){ x_option = d_option = q_option = 0;
if(!strcmp("--version",*argv)){
V_option++; while ((c = getopt_long(argc, argv, "xrdqA:hV", longopts, NULL)) != -1)
continue; switch (c) {
}
if(**argv=='-'){
char *walk = *argv;
if(!walk[1]) usage();
while(*++walk){
switch(*walk){
case 'V':
V_option++;
break;
case 'x': case 'x':
x_option++; x_option = 1;
break; break;
case 'r': case 'r':
r_option++; warnx("option -r is ignored as SunOS compatibility");
break; break;
case 'd': case 'd':
d_option++; d_option = 1;
break; break;
case 'q': case 'q':
q_option++; q_option = 1;
break; break;
case 'A':{ case 'A':
{
/* FIXME: this should be a function. */
char *walk = optarg;
char *arg1; char *arg1;
char *arg2;
if (walk[1]) { if (walk[1]) {
arg1 = walk + 1; arg1 = walk + 1;
walk += strlen(walk) - 1; walk += strlen(walk) - 1;
} else { } else {
arg1 = *++argv; arg1 = *++argv;
if (!arg1) if (!arg1)
usage(); usage(stderr);
} }
char *arg2 = strchr(arg1,','); arg2 = strchr(arg1, ',');
if (arg2) if (arg2)
*arg2 = '\0'; *arg2 = '\0';
if(arg2) ++arg2; arg2 = *arg2 ? arg2++ : arg1;
else arg2 = arg1;
if (*arg1) if (*arg1)
range_low = STRTOUKL(arg1, &arg1, 16); range_low = STRTOUKL(arg1, &arg1, 16);
if (*arg2) if (*arg2)
range_high = STRTOUKL(arg2, &arg2, 16); range_high = STRTOUKL(arg2, &arg2, 16);
if (*arg1 || *arg2) if (*arg1 || *arg2)
usage(); usage(stderr);
} }
break; break;
case 'a': // Sun prints anon/swap reservations case 'h':
case 'F': // Sun forces hostile ptrace-like grab usage(stdout);
case 'l': // Sun shows unresolved dynamic names case 'V':
case 'L': // Sun shows lgroup info display_version();
case 's': // Sun shows page sizes return EXIT_SUCCESS;
case 'S': // Sun shows swap reservations 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 */
default: default:
usage(); usage(stderr);
} }
}
}else{ argc -= optind;
char *walk = *argv; argv += optind;
if (argc < 1)
errx(EXIT_FAILURE, "argument missing");
if (d_option && x_option)
errx(EXIT_FAILURE, "options -d and -x can not coexist");
pidlist = malloc(sizeof(unsigned) * argc);
if (pidlist == NULL)
err(EXIT_FAILURE, "cannot allocate %zu bytes",
sizeof(unsigned) * argc);
while (*argv) {
char *walk = *argv++;
char *endp; char *endp;
unsigned long pid; unsigned long pid;
if (!strncmp("/proc/", walk, 6)) { if (!strncmp("/proc/", walk, 6)) {
walk += 6; walk += 6;
// user allowed to do: pmap /proc/* /* user allowed to do: pmap /proc/PID */
if(*walk<'0' || *walk>'9') continue; if (*walk < '0' || *walk > '9')
continue;
} }
if(*walk<'0' || *walk>'9') usage(); if (*walk < '0' || *walk > '9')
usage(stderr);
pid = strtoul(walk, &endp, 0); pid = strtoul(walk, &endp, 0);
if(pid<1ul || pid>0x7ffffffful || *endp) usage(); if (pid < 1ul || pid > 0x7ffffffful || *endp)
usage(stderr);
pidlist[count++] = pid; pidlist[count++] = pid;
} }
}
if( (x_option|V_option|r_option|d_option|q_option) >> 1 ) usage(); // dupes
if(V_option){
if(count|x_option|r_option|d_option|q_option) usage();
fprintf(stdout, "pmap (%s)\n", procps_version);
return 0;
}
if(count<1) usage(); // no processes
if(d_option && x_option) usage();
discover_shm_minor(); discover_shm_minor();
memset(&p, '\0', sizeof(p)); memset(&p, '\0', sizeof(p));
pidlist[count] = 0; // old libproc interface is zero-terminated /* old libproc interface is zero-terminated */
pidlist[count] = 0;
PT = openproc(PROC_FILLSTAT | PROC_FILLARG | PROC_PID, pidlist); PT = openproc(PROC_FILLSTAT | PROC_FILLARG | PROC_PID, pidlist);
while (readproc(PT, &p)) { while (readproc(PT, &p)) {
ret |= one_proc(&p); ret |= one_proc(&p);
@ -418,6 +481,8 @@ int main(int argc, char *argv[]){
} }
closeproc(PT); closeproc(PT);
if(count) ret |= 42; // didn't find all processes asked for if (count)
/* didn't find all processes asked for */
ret |= 42;
return ret; return ret;
} }