Home | History | Annotate | Line # | Download | only in pstat
pstat.c revision 1.102
      1 /*	$NetBSD: pstat.c,v 1.102 2007/10/20 01:38:45 briggs Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1980, 1991, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
     35 	The Regents of the University of California.  All rights reserved.\n");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)pstat.c	8.16 (Berkeley) 5/9/95";
     41 #else
     42 __RCSID("$NetBSD: pstat.c,v 1.102 2007/10/20 01:38:45 briggs Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 #include <sys/param.h>
     47 #include <sys/time.h>
     48 #include <sys/vnode.h>
     49 #include <sys/ucred.h>
     50 #include <stdbool.h>
     51 #define _KERNEL
     52 #include <sys/file.h>
     53 #include <ufs/ufs/inode.h>
     54 #define NFS
     55 #include <sys/mount.h>
     56 #undef NFS
     57 #include <sys/uio.h>
     58 #include <sys/namei.h>
     59 #include <miscfs/genfs/layer.h>
     60 #include <miscfs/union/union.h>
     61 #undef _KERNEL
     62 #include <sys/stat.h>
     63 #include <nfs/nfsproto.h>
     64 #include <nfs/rpcv2.h>
     65 #include <nfs/nfs.h>
     66 #include <nfs/nfsnode.h>
     67 #include <sys/ioctl.h>
     68 #include <sys/tty.h>
     69 #include <sys/conf.h>
     70 #include <sys/device.h>
     71 
     72 #include <sys/sysctl.h>
     73 
     74 #include <err.h>
     75 #include <kvm.h>
     76 #include <limits.h>
     77 #include <nlist.h>
     78 #include <stdio.h>
     79 #include <stdlib.h>
     80 #include <string.h>
     81 #include <unistd.h>
     82 
     83 #include "swapctl.h"
     84 
     85 struct nlist nl[] = {
     86 #define	V_MOUNTLIST	0
     87 	{ "_mountlist" },	/* address of head of mount list. */
     88 #define	V_NUMV		1
     89 	{ "_numvnodes" },
     90 #define	FNL_NFILE	2
     91 	{ "_nfiles" },
     92 #define FNL_MAXFILE	3
     93 	{ "_maxfiles" },
     94 #define TTY_NTTY	4
     95 	{ "_tty_count" },
     96 #define TTY_TTYLIST	5
     97 	{ "_ttylist" },
     98 #define NLMANDATORY TTY_TTYLIST	/* names up to here are mandatory */
     99 	{ "" }
    100 };
    101 
    102 int	usenumflag;
    103 int	totalflag;
    104 int	kflag;
    105 int	hflag;
    106 char	*nlistf	= NULL;
    107 char	*memf	= NULL;
    108 kvm_t	*kd;
    109 
    110 static const char * const dtypes[] = { DTYPE_NAMES };
    111 
    112 
    113 static const struct {
    114 	u_int m_flag;
    115 	u_int m_visible;
    116 	const char *m_name;
    117 } mnt_flags[] = {
    118 	__MNT_FLAGS
    119 };
    120 
    121 struct flagbit_desc {
    122 	u_int fd_flags;
    123 	char fd_mark;
    124 };
    125 
    126 #define	SVAR(var) __STRING(var)	/* to force expansion */
    127 #define	KGET(idx, var)							\
    128 	KGET1(idx, &var, sizeof(var), SVAR(var))
    129 #define	KGET1(idx, p, s, msg)						\
    130 	KGET2(nl[idx].n_value, p, s, msg)
    131 #define	KGET2(addr, p, s, msg) do {					\
    132 	if (kvm_read(kd, (u_long)(addr), p, s) != s)			\
    133 		warnx("cannot read %s: %s", msg, kvm_geterr(kd));	\
    134 } while (/* CONSTCOND */0)
    135 #define	KGETRET(addr, p, s, msg) do {					\
    136 	if (kvm_read(kd, (u_long)(addr), p, s) != s) {			\
    137 		warnx("cannot read %s: %s", msg, kvm_geterr(kd));	\
    138 		return (0);						\
    139 	}								\
    140 } while (/* CONSTCOND */0)
    141 
    142 #if 1				/* This is copied from vmstat/vmstat.c */
    143 /*
    144  * Print single word.  `ovflow' is number of characters didn't fit
    145  * on the last word.  `fmt' is a format string to print this word.
    146  * It must contain asterisk for field width.  `width' is a width
    147  * occupied by this word.  `fixed' is a number of constant chars in
    148  * `fmt'.  `val' is a value to be printed using format string `fmt'.
    149  */
    150 #define	PRWORD(ovflw, fmt, width, fixed, val) do {	\
    151 	(ovflw) += printf((fmt),			\
    152 	    (width) - (fixed) - (ovflw) > 0 ?		\
    153 	    (width) - (fixed) - (ovflw) : 0,		\
    154 	    (val)) - (width);				\
    155 	if ((ovflw) < 0)				\
    156 		(ovflw) = 0;				\
    157 } while (/* CONSTCOND */0)
    158 #endif
    159 
    160 void	filemode(void);
    161 int	getfiles(char **, int *, char **);
    162 int	getflags(const struct flagbit_desc *, char *, u_int);
    163 struct mount *
    164 	getmnt(struct mount *);
    165 char *	kinfo_vnodes(int *);
    166 void	layer_header(void);
    167 int	layer_print(struct vnode *, int);
    168 char *	loadvnodes(int *);
    169 int	main(int, char **);
    170 void	mount_print(struct mount *);
    171 void	nfs_header(void);
    172 int	nfs_print(struct vnode *, int);
    173 void	ttymode(void);
    174 void	ttyprt(struct tty *);
    175 void	ufs_header(void);
    176 int	ufs_print(struct vnode *, int);
    177 int	ext2fs_print(struct vnode *, int);
    178 void	union_header(void);
    179 int	union_print(struct vnode *, int);
    180 void	usage(void);
    181 void	vnode_header(void);
    182 int	vnode_print(struct vnode *, struct vnode *);
    183 void	vnodemode(void);
    184 
    185 int
    186 main(int argc, char *argv[])
    187 {
    188 	int ch, i, quit, ret, use_sysctl;
    189 	int fileflag, swapflag, ttyflag, vnodeflag;
    190 	gid_t egid = getegid();
    191 	char buf[_POSIX2_LINE_MAX];
    192 
    193 	setegid(getgid());
    194 	fileflag = swapflag = ttyflag = vnodeflag = 0;
    195 	while ((ch = getopt(argc, argv, "TM:N:fghikmnstv")) != -1)
    196 		switch (ch) {
    197 		case 'f':
    198 			fileflag = 1;
    199 			break;
    200 		case 'M':
    201 			memf = optarg;
    202 			break;
    203 		case 'N':
    204 			nlistf = optarg;
    205 			break;
    206 		case 'n':
    207 			usenumflag = 1;
    208 			break;
    209 		case 's':
    210 			swapflag = 1;
    211 			break;
    212 		case 'T':
    213 			totalflag = 1;
    214 			break;
    215 		case 't':
    216 			ttyflag = 1;
    217 			break;
    218 		case 'k':
    219 			kflag = 1;
    220 			break;
    221 		case 'g':
    222 			kflag = 3; /* 1k ^ 3 */
    223 			break;
    224 		case 'h':
    225 			hflag = 1;
    226 			break;
    227 		case 'm':
    228 			kflag = 2; /* 1k ^ 2 */
    229 			break;
    230 		case 'v':
    231 		case 'i':		/* Backward compatibility. */
    232 			vnodeflag = 1;
    233 			break;
    234 		default:
    235 			usage();
    236 		}
    237 	argc -= optind;
    238 	argv += optind;
    239 
    240 	/*
    241 	 * Discard setgid privileges.  If not the running kernel, we toss
    242 	 * them away totally so that bad guys can't print interesting stuff
    243 	 * from kernel memory, otherwise switch back to kmem for the
    244 	 * duration of the kvm_openfiles() call.
    245 	 */
    246 	if (nlistf != NULL || memf != NULL)
    247 		(void)setgid(getgid());
    248 	else
    249 		(void)setegid(egid);
    250 
    251 	use_sysctl = (nlistf == NULL && memf == NULL);
    252 
    253 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0)
    254 		errx(1, "kvm_openfiles: %s", buf);
    255 
    256 	/* get rid of it now anyway */
    257 	if (nlistf == NULL && memf == NULL)
    258 		(void)setgid(getgid());
    259 	if ((ret = kvm_nlist(kd, nl)) != 0) {
    260 		if (ret == -1)
    261 			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
    262 		for (i = quit = 0; i <= NLMANDATORY; i++)
    263 			if (!nl[i].n_value) {
    264 				quit = 1;
    265 				warnx("undefined symbol: %s", nl[i].n_name);
    266 			}
    267 		if (quit)
    268 			exit(1);
    269 	}
    270 	if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
    271 		usage();
    272 	if (fileflag || totalflag)
    273 		filemode();
    274 	if (vnodeflag || totalflag)
    275 		vnodemode();
    276 	if (ttyflag)
    277 		ttymode();
    278 	if (swapflag || totalflag)
    279 		if (use_sysctl)
    280 			list_swap(0, kflag, 0, totalflag, 1, hflag);
    281 	exit(0);
    282 }
    283 
    284 #define	VPTRSZ  sizeof(struct vnode *)
    285 #define	VNODESZ sizeof(struct vnode)
    286 #define	PTRSTRWIDTH ((int)sizeof(void *) * 2) /* Width of resulting string
    287 						 when pointer is printed
    288 						 in hexadecimal. */
    289 
    290 void
    291 vnodemode(void)
    292 {
    293 	char *e_vnodebase, *endvnode, *evp;
    294 	struct vnode *vp;
    295 	struct mount *maddr, *mp;
    296 	int numvnodes, ovflw;
    297 	int (*vnode_fsprint) (struct vnode *, int); /* per-fs data printer */
    298 
    299 	mp = NULL;
    300 	e_vnodebase = loadvnodes(&numvnodes);
    301 	if (totalflag) {
    302 		(void)printf("%7d vnodes\n", numvnodes);
    303 		goto out;
    304 	}
    305 	endvnode = e_vnodebase + numvnodes * (VPTRSZ + VNODESZ);
    306 	(void)printf("%d active vnodes\n", numvnodes);
    307 
    308 #define	ST	mp->mnt_stat
    309 #define	FSTYPE_IS(mp, name)						\
    310 	(strncmp((mp)->mnt_stat.f_fstypename, (name), 			\
    311 	sizeof((mp)->mnt_stat.f_fstypename)) == 0)
    312 	maddr = NULL;
    313 	vnode_fsprint = NULL;
    314 	for (evp = e_vnodebase; evp < endvnode; evp += VPTRSZ + VNODESZ) {
    315 		vp = (struct vnode *)(evp + VPTRSZ);
    316 		if (vp->v_mount != maddr) {
    317 			/*
    318 			 * New filesystem
    319 			 */
    320 			if ((mp = getmnt(vp->v_mount)) == NULL)
    321 				continue;
    322 			maddr = vp->v_mount;
    323 			mount_print(mp);
    324 			vnode_header();
    325 			if (FSTYPE_IS(mp, MOUNT_FFS) ||
    326 			    FSTYPE_IS(mp, MOUNT_MFS)) {
    327 				ufs_header();
    328 				vnode_fsprint = ufs_print;
    329 			} else if (FSTYPE_IS(mp, MOUNT_NFS)) {
    330 				nfs_header();
    331 				vnode_fsprint = nfs_print;
    332 			} else if (FSTYPE_IS(mp, MOUNT_EXT2FS)) {
    333 				ufs_header();
    334 				vnode_fsprint = ext2fs_print;
    335 			} else if (FSTYPE_IS(mp, MOUNT_NULL) ||
    336 			    FSTYPE_IS(mp, MOUNT_OVERLAY) ||
    337 			    FSTYPE_IS(mp, MOUNT_UMAP)) {
    338 				layer_header();
    339 				vnode_fsprint = layer_print;
    340 			} else if (FSTYPE_IS(mp, MOUNT_UNION)) {
    341 				union_header();
    342 				vnode_fsprint = union_print;
    343 			} else
    344 				vnode_fsprint = NULL;
    345 			(void)printf("\n");
    346 		}
    347 		ovflw = vnode_print(*(struct vnode **)evp, vp);
    348 		if (VTOI(vp) != NULL && vnode_fsprint != NULL)
    349 			(*vnode_fsprint)(vp, ovflw);
    350 		(void)printf("\n");
    351 	}
    352 
    353  out:
    354 	if (e_vnodebase)
    355 		free(e_vnodebase);
    356 }
    357 
    358 int
    359 getflags(const struct flagbit_desc *fd, char *p, u_int flags)
    360 {
    361 	char *q = p;
    362 
    363 	if (flags == 0) {
    364 		*p++ = '-';
    365 		*p = '\0';
    366 		return (0);
    367 	}
    368 
    369 	for (; fd->fd_flags != 0; fd++)
    370 		if ((flags & fd->fd_flags) != 0)
    371 			*p++ = fd->fd_mark;
    372 	*p = '\0';
    373 	return (p - q);
    374 }
    375 
    376 const struct flagbit_desc vnode_flags[] = {
    377 	{ VV_ROOT,	'R' },
    378 	{ VI_TEXT,	'T' },
    379 	{ VV_SYSTEM,	'S' },
    380 	{ VV_ISTTY,	'I' },
    381 	{ VI_EXECMAP,	'E' },
    382 	{ VI_XLOCK,	'L' },
    383 	{ VI_XWANT,	'W' },
    384 	{ VI_ALIASED,	'A' },
    385 	{ VU_DIROP,	'D' },
    386 	{ VI_LAYER,	'Y' },
    387 	{ VI_ONWORKLST,	'O' },
    388 	{ 0,		'\0' },
    389 };
    390 
    391 void
    392 vnode_header(void)
    393 {
    394 
    395 	(void)printf("%-*s TYP VFLAG  USE HOLD TAG NPAGE",
    396 	    PTRSTRWIDTH, "ADDR");
    397 }
    398 
    399 int
    400 vnode_print(struct vnode *avnode, struct vnode *vp)
    401 {
    402 	char *type, flags[sizeof(vnode_flags) / sizeof(vnode_flags[0])];
    403 	int ovflw;
    404 
    405 	/*
    406 	 * set type
    407 	 */
    408 	switch (vp->v_type) {
    409 	case VNON:
    410 		type = "non"; break;
    411 	case VREG:
    412 		type = "reg"; break;
    413 	case VDIR:
    414 		type = "dir"; break;
    415 	case VBLK:
    416 		type = "blk"; break;
    417 	case VCHR:
    418 		type = "chr"; break;
    419 	case VLNK:
    420 		type = "lnk"; break;
    421 	case VSOCK:
    422 		type = "soc"; break;
    423 	case VFIFO:
    424 		type = "fif"; break;
    425 	case VBAD:
    426 		type = "bad"; break;
    427 	default:
    428 		type = "unk"; break;
    429 	}
    430 	/*
    431 	 * gather flags
    432 	 */
    433 	(void)getflags(vnode_flags, flags,
    434 	    vp->v_uflag | vp->v_iflag | vp->v_vflag);
    435 
    436 	ovflw = 0;
    437 	PRWORD(ovflw, "%*lx", PTRSTRWIDTH, 0, (long)avnode);
    438 	PRWORD(ovflw, " %*s", 4, 1, type);
    439 	PRWORD(ovflw, " %*s", 6, 1, flags);
    440 	PRWORD(ovflw, " %*ld", 5, 1, (long)vp->v_usecount);
    441 	PRWORD(ovflw, " %*ld", 5, 1, (long)vp->v_holdcnt);
    442 	PRWORD(ovflw, " %*d", 4, 1, vp->v_tag);
    443 	PRWORD(ovflw, " %*d", 6, 1, vp->v_uobj.uo_npages);
    444 	return (ovflw);
    445 }
    446 
    447 const struct flagbit_desc ufs_flags[] = {
    448 	{ IN_ACCESS,	'A' },
    449 	{ IN_CHANGE,	'C' },
    450 	{ IN_UPDATE,	'U' },
    451 	{ IN_MODIFIED,	'M' },
    452 	{ IN_ACCESSED,	'a' },
    453 	{ IN_RENAME,	'R' },
    454 	{ IN_SHLOCK,	'S' },
    455 	{ IN_EXLOCK,	'E' },
    456 	{ IN_CLEANING,	'c' },
    457 	{ IN_ADIROP,	'D' },
    458 	{ IN_SPACECOUNTED, 's' },
    459 	{ 0,		'\0' },
    460 };
    461 
    462 void
    463 ufs_header(void)
    464 {
    465 
    466 	(void)printf(" FILEID IFLAG RDEV|SZ");
    467 }
    468 
    469 int
    470 ufs_print(struct vnode *vp, int ovflw)
    471 {
    472 	struct inode inode, *ip = &inode;
    473 	union dinode {
    474 		struct ufs1_dinode dp1;
    475 		struct ufs2_dinode dp2;
    476 	} dip;
    477 	char flags[sizeof(ufs_flags) / sizeof(ufs_flags[0])];
    478 	char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */
    479 	char *name;
    480 	mode_t type;
    481 	dev_t rdev;
    482 
    483 	KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
    484 	KGETRET(ip->i_din.ffs1_din, &dip, sizeof (struct ufs1_dinode),
    485 	    "inode's dinode");
    486 
    487 	if (ip->i_size == dip.dp1.di_size)
    488 		rdev = dip.dp1.di_rdev;
    489 	else {
    490 		KGETRET(ip->i_din.ffs1_din, &dip, sizeof (struct ufs2_dinode),
    491 		    "inode's UFS2 dinode");
    492 		rdev = dip.dp2.di_rdev;
    493 	}
    494 
    495 	/*
    496 	 * XXX need to to locking state.
    497 	 */
    498 
    499 	(void)getflags(ufs_flags, flags, ip->i_flag);
    500 	PRWORD(ovflw, " %*llu", 7, 1, (unsigned long long)ip->i_number);
    501 	PRWORD(ovflw, " %*s", 6, 1, flags);
    502 	type = ip->i_mode & S_IFMT;
    503 	if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
    504 		if (usenumflag ||
    505 		    (name = devname(rdev, type)) == NULL) {
    506 			snprintf(dev, sizeof(dev), "%d,%d",
    507 			    major(rdev), minor(rdev));
    508 			name = dev;
    509 		}
    510 		PRWORD(ovflw, " %*s", 8, 1, name);
    511 	} else
    512 		PRWORD(ovflw, " %*lld", 8, 1, (long long)ip->i_size);
    513 	return 0;
    514 }
    515 
    516 int
    517 ext2fs_print(struct vnode *vp, int ovflw)
    518 {
    519 	struct inode inode, *ip = &inode;
    520 	struct ext2fs_dinode dip;
    521 	char flags[sizeof(ufs_flags) / sizeof(ufs_flags[0])];
    522 	char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */
    523 	char *name;
    524 	mode_t type;
    525 
    526 	KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
    527 	KGETRET(ip->i_din.e2fs_din, &dip, sizeof (struct ext2fs_dinode),
    528 	    "inode's dinode");
    529 
    530 	/*
    531 	 * XXX need to to locking state.
    532 	 */
    533 
    534 	(void)getflags(ufs_flags, flags, ip->i_flag);
    535 	PRWORD(ovflw, " %*llu", 7, 1, (unsigned long long)ip->i_number);
    536 	PRWORD(ovflw, " %*s", 6, 1, flags);
    537 	type = dip.e2di_mode & S_IFMT;
    538 	if (S_ISCHR(dip.e2di_mode) || S_ISBLK(dip.e2di_mode)) {
    539 		if (usenumflag ||
    540 		    (name = devname(dip.e2di_rdev, type)) == NULL) {
    541 			snprintf(dev, sizeof(dev), "%d,%d",
    542 			    major(dip.e2di_rdev), minor(dip.e2di_rdev));
    543 			name = dev;
    544 		}
    545 		PRWORD(ovflw, " %*s", 8, 1, name);
    546 	} else
    547 		PRWORD(ovflw, " %*u", 8, 1, (u_int)dip.e2di_size);
    548 	return (0);
    549 }
    550 
    551 const struct flagbit_desc nfs_flags[] = {
    552 	{ NFLUSHWANT,	'W' },
    553 	{ NFLUSHINPROG,	'P' },
    554 	{ NMODIFIED,	'M' },
    555 	{ NWRITEERR,	'E' },
    556 	{ NACC,		'A' },
    557 	{ NUPD,		'U' },
    558 	{ NCHG,		'C' },
    559 	{ 0,		'\0' },
    560 };
    561 
    562 void
    563 nfs_header(void)
    564 {
    565 
    566 	(void)printf(" FILEID NFLAG RDEV|SZ");
    567 }
    568 
    569 int
    570 nfs_print(struct vnode *vp, int ovflw)
    571 {
    572 	struct nfsnode nfsnode, *np = &nfsnode;
    573 	char flags[sizeof(nfs_flags) / sizeof(nfs_flags[0])];
    574 	char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */
    575 	struct vattr va;
    576 	char *name;
    577 	mode_t type;
    578 
    579 	KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
    580 	(void)getflags(nfs_flags, flags, np->n_flag);
    581 
    582 	KGETRET(np->n_vattr, &va, sizeof(va), "vnode attr");
    583 	PRWORD(ovflw, " %*ld", 7, 1, (long)va.va_fileid);
    584 	PRWORD(ovflw, " %*s", 6, 1, flags);
    585 	switch (va.va_type) {
    586 	case VCHR:
    587 		type = S_IFCHR;
    588 		goto device;
    589 
    590 	case VBLK:
    591 		type = S_IFBLK;
    592 	device:
    593 		if (usenumflag || (name = devname(va.va_rdev, type)) == NULL) {
    594 			(void)snprintf(dev, sizeof(dev), "%d,%d",
    595 			    major(va.va_rdev), minor(va.va_rdev));
    596 			name = dev;
    597 		}
    598 		PRWORD(ovflw, " %*s", 8, 1, name);
    599 		break;
    600 	default:
    601 		PRWORD(ovflw, " %*lld", 8, 1, (long long)np->n_size);
    602 		break;
    603 	}
    604 	return (0);
    605 }
    606 
    607 void
    608 layer_header(void)
    609 {
    610 
    611 	(void)printf(" %*s", PTRSTRWIDTH, "LOWER");
    612 }
    613 
    614 int
    615 layer_print(struct vnode *vp, int ovflw)
    616 {
    617 	struct layer_node lnode, *lp = &lnode;
    618 
    619 	KGETRET(VTOLAYER(vp), &lnode, sizeof(lnode), "layer vnode");
    620 
    621 	PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 1, (long)lp->layer_lowervp);
    622 	return (0);
    623 }
    624 
    625 void
    626 union_header(void)
    627 {
    628 
    629 	(void)printf(" %*s %*s", PTRSTRWIDTH, "UPPER", PTRSTRWIDTH, "LOWER");
    630 }
    631 
    632 int
    633 union_print(struct vnode *vp, int ovflw)
    634 {
    635 	struct union_node unode, *up = &unode;
    636 
    637 	KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode");
    638 
    639 	PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 1, (long)up->un_uppervp);
    640 	PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 1, (long)up->un_lowervp);
    641 	return (0);
    642 }
    643 
    644 /*
    645  * Given a pointer to a mount structure in kernel space,
    646  * read it in and return a usable pointer to it.
    647  */
    648 struct mount *
    649 getmnt(struct mount *maddr)
    650 {
    651 	static struct mtab {
    652 		struct mtab *next;
    653 		struct mount *maddr;
    654 		struct mount mount;
    655 	} *mhead = NULL;
    656 	struct mtab *mt;
    657 	struct mount mb;
    658 
    659 	for (mt = mhead; mt != NULL; mt = mt->next)
    660 		if (maddr == mt->maddr)
    661 			return (&mt->mount);
    662 	KGETRET(maddr, &mb, sizeof(struct mount), "mount table");
    663 	if ((mt = malloc(sizeof(struct mtab))) == NULL)
    664 		err(1, "malloc");
    665 	mt->mount = mb;
    666 	mt->maddr = maddr;
    667 	mt->next = mhead;
    668 	mhead = mt;
    669 	return (&mt->mount);
    670 }
    671 
    672 void
    673 mount_print(struct mount *mp)
    674 {
    675 	int flags;
    676 
    677 	(void)printf("*** MOUNT %s %s on %s", ST.f_fstypename,
    678 	    ST.f_mntfromname, ST.f_mntonname);
    679 	if ((flags = mp->mnt_flag) != 0) {
    680 		int i;
    681 		const char *sep = " (";
    682 
    683 		for (i = 0; i < sizeof mnt_flags / sizeof mnt_flags[0]; i++) {
    684 			if (flags & mnt_flags[i].m_flag) {
    685 				(void)printf("%s%s", sep, mnt_flags[i].m_name);
    686 				flags &= ~mnt_flags[i].m_flag;
    687 				sep = ",";
    688 			}
    689 		}
    690 		if (flags)
    691 			(void)printf("%sunknown_flags:%x", sep, flags);
    692 		(void)printf(")");
    693 	}
    694 	(void)printf("\n");
    695 }
    696 
    697 char *
    698 loadvnodes(int *avnodes)
    699 {
    700 	int mib[2];
    701 	size_t copysize;
    702 	char *vnodebase;
    703 
    704 	if (totalflag) {
    705 		KGET(V_NUMV, *avnodes);
    706 		return NULL;
    707 	}
    708 	if (memf != NULL) {
    709 		/*
    710 		 * do it by hand
    711 		 */
    712 		return (kinfo_vnodes(avnodes));
    713 	}
    714 	mib[0] = CTL_KERN;
    715 	mib[1] = KERN_VNODE;
    716 	if (sysctl(mib, 2, NULL, &copysize, NULL, 0) == -1)
    717 		err(1, "sysctl: KERN_VNODE");
    718 	if ((vnodebase = malloc(copysize)) == NULL)
    719 		err(1, "malloc");
    720 	if (sysctl(mib, 2, vnodebase, &copysize, NULL, 0) == -1)
    721 		err(1, "sysctl: KERN_VNODE");
    722 	if (copysize % (VPTRSZ + VNODESZ))
    723 		errx(1, "vnode size mismatch");
    724 	*avnodes = copysize / (VPTRSZ + VNODESZ);
    725 
    726 	return (vnodebase);
    727 }
    728 
    729 /*
    730  * simulate what a running kernel does in in kinfo_vnode
    731  */
    732 char *
    733 kinfo_vnodes(int *avnodes)
    734 {
    735 	struct mntlist mountlist;
    736 	struct mount *mp, mount;
    737 	struct vnode *vp, vnode;
    738 	char *beg, *bp, *ep;
    739 	int numvnodes;
    740 
    741 	KGET(V_NUMV, numvnodes);
    742 	if ((bp = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
    743 		err(1, "malloc");
    744 	beg = bp;
    745 	ep = bp + (numvnodes + 20) * (VPTRSZ + VNODESZ);
    746 	KGET(V_MOUNTLIST, mountlist);
    747 	for (mp = mountlist.cqh_first;;
    748 	    mp = mount.mnt_list.cqe_next) {
    749 		KGET2(mp, &mount, sizeof(mount), "mount entry");
    750 		TAILQ_FOREACH(vp, &mount.mnt_vnodelist, v_mntvnodes) {
    751 			KGET2(vp, &vnode, sizeof(vnode), "vnode");
    752 			if (bp + VPTRSZ + VNODESZ > ep)
    753 				/* XXX - should realloc */
    754 				errx(1, "no more room for vnodes");
    755 			memmove(bp, &vp, VPTRSZ);
    756 			bp += VPTRSZ;
    757 			memmove(bp, &vnode, VNODESZ);
    758 			bp += VNODESZ;
    759 		}
    760 		if (mp == mountlist.cqh_last)
    761 			break;
    762 	}
    763 	*avnodes = (bp - beg) / (VPTRSZ + VNODESZ);
    764 	return (beg);
    765 }
    766 
    767 void
    768 ttymode(void)
    769 {
    770 	int ntty;
    771 	struct ttylist_head tty_head;
    772 	struct tty *tp, tty;
    773 
    774 	KGET(TTY_NTTY, ntty);
    775 	(void)printf("%d terminal device%s\n", ntty, ntty == 1 ? "" : "s");
    776 	KGET(TTY_TTYLIST, tty_head);
    777 	(void)printf(
    778 	    "  LINE RAW CAN OUT  HWT LWT     COL STATE  %-*s  PGID DISC\n",
    779 	    PTRSTRWIDTH, "SESS");
    780 	for (tp = tty_head.tqh_first; tp; tp = tty.tty_link.tqe_next) {
    781 		KGET2(tp, &tty, sizeof tty, "tty struct");
    782 		ttyprt(&tty);
    783 	}
    784 }
    785 
    786 static const struct flagbit_desc ttystates[] = {
    787 	{ TS_ISOPEN,	'O'},
    788 	{ TS_DIALOUT,	'>'},
    789 	{ TS_CARR_ON,	'C'},
    790 	{ TS_TIMEOUT,	'T'},
    791 	{ TS_FLUSH,	'F'},
    792 	{ TS_BUSY,	'B'},
    793 	{ TS_ASLEEP,	'A'},
    794 	{ TS_XCLUDE,	'X'},
    795 	{ TS_TTSTOP,	'S'},
    796 	{ TS_TBLOCK,	'K'},
    797 	{ TS_ASYNC,	'Y'},
    798 	{ TS_BKSL,	'D'},
    799 	{ TS_ERASE,	'E'},
    800 	{ TS_LNCH,	'L'},
    801 	{ TS_TYPEN,	'P'},
    802 	{ TS_CNTTB,	'N'},
    803 	{ 0,		'\0'},
    804 };
    805 
    806 void
    807 ttyprt(struct tty *tp)
    808 {
    809 	char state[sizeof(ttystates) / sizeof(ttystates[0]) + 1];
    810 	char dev[2 + 3 + 1 + 5 + 1]; /* 12bit major + 20bit minor */
    811 	struct linesw t_linesw;
    812 	const char *name;
    813 	char buffer;
    814 	pid_t pgid;
    815 	int n, ovflw;
    816 
    817 	if (usenumflag || (name = devname(tp->t_dev, S_IFCHR)) == NULL) {
    818 		(void)snprintf(dev, sizeof(dev), "0x%3x:%x",
    819 		    major(tp->t_dev), minor(tp->t_dev));
    820 		name = dev;
    821 	}
    822 	ovflw = 0;
    823 	PRWORD(ovflw, "%-*s", 7, 0, name);
    824 	PRWORD(ovflw, " %*d", 3, 1, tp->t_rawq.c_cc);
    825 	PRWORD(ovflw, " %*d", 4, 1, tp->t_canq.c_cc);
    826 	PRWORD(ovflw, " %*d", 4, 1, tp->t_outq.c_cc);
    827 	PRWORD(ovflw, " %*d", 5, 1, tp->t_hiwat);
    828 	PRWORD(ovflw, " %*d", 4, 1, tp->t_lowat);
    829 	PRWORD(ovflw, " %*d", 8, 1, tp->t_column);
    830 	n = getflags(ttystates, state, tp->t_state);
    831 	if (tp->t_wopen) {
    832 		state[n++] = 'W';
    833 		state[n] = '\0';
    834 	}
    835 	PRWORD(ovflw, " %-*s", 7, 1, state);
    836 	PRWORD(ovflw, " %*lX", PTRSTRWIDTH + 1, 1, (u_long)tp->t_session);
    837 	pgid = 0;
    838 	if (tp->t_pgrp != NULL)
    839 		KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
    840 	PRWORD(ovflw, " %*d", 6, 1, pgid);
    841 	KGET2(tp->t_linesw, &t_linesw, sizeof(t_linesw),
    842 	    "line discipline switch table");
    843 	name = t_linesw.l_name;
    844 	(void)putchar(' ');
    845 	for (;;) {
    846 		KGET2(name, &buffer, sizeof(buffer), "line discipline name");
    847 		if (buffer == '\0')
    848 			break;
    849 		(void)putchar(buffer);
    850 		name++;
    851 	}
    852 	(void)putchar('\n');
    853 }
    854 
    855 static const struct flagbit_desc filemode_flags[] = {
    856 	{ FREAD,	'R' },
    857 	{ FWRITE,	'W' },
    858 	{ FAPPEND,	'A' },
    859 #ifdef FSHLOCK	/* currently gone */
    860 	{ FSHLOCK,	'S' },
    861 	{ FEXLOCK,	'X' },
    862 #endif
    863 	{ FASYNC,	'I' },
    864 	{ 0,		'\0' },
    865 };
    866 
    867 void
    868 filemode(void)
    869 {
    870 	struct file *fp;
    871 	struct file *addr;
    872 	char flags[sizeof(filemode_flags) / sizeof(filemode_flags[0])];
    873 	char *buf, *offset;
    874 	int len, maxfile, nfile, ovflw;
    875 
    876 	KGET(FNL_MAXFILE, maxfile);
    877 	if (totalflag) {
    878 		KGET(FNL_NFILE, nfile);
    879 		(void)printf("%3d/%3d files\n", nfile, maxfile);
    880 		return;
    881 	}
    882 	if (getfiles(&buf, &len, &offset) == -1)
    883 		return;
    884 	/*
    885 	 * Getfiles returns in malloc'd memory a pointer to the first file
    886 	 * structure, and then an array of file structs (whose addresses are
    887 	 * derivable from the previous entry).
    888 	 */
    889 	addr = ((struct filelist *)offset)->lh_first;
    890 	fp = (struct file *)(offset + sizeof(struct filelist));
    891 	nfile = (len - sizeof(struct filelist)) / sizeof(struct file);
    892 
    893 	(void)printf("%d/%d open files\n", nfile, maxfile);
    894 	(void)printf("%*s%s%*s TYPE    FLG     CNT  MSG  %*s%s%*s USE IFLG OFFSET\n",
    895 	    (PTRSTRWIDTH - 4) / 2, "", " LOC", (PTRSTRWIDTH - 4) / 2, "",
    896 	    (PTRSTRWIDTH - 4) / 2, "", "DATA", (PTRSTRWIDTH - 4) / 2, "");
    897 	for (; (char *)fp < offset + len; addr = fp->f_list.le_next, fp++) {
    898 		if ((unsigned)fp->f_type >= sizeof(dtypes) / sizeof(dtypes[0]))
    899 			continue;
    900 		ovflw = 0;
    901 		(void)getflags(filemode_flags, flags, fp->f_flag);
    902 		PRWORD(ovflw, "%*lx", PTRSTRWIDTH, 0, (long)addr);
    903 		PRWORD(ovflw, " %-*s", 9, 1, dtypes[fp->f_type]);
    904 		PRWORD(ovflw, " %*s", 6, 1, flags);
    905 		PRWORD(ovflw, " %*d", 5, 1, fp->f_count);
    906 		PRWORD(ovflw, " %*d", 5, 1, fp->f_msgcount);
    907 		PRWORD(ovflw, "  %*lx", PTRSTRWIDTH + 1, 2, (long)fp->f_data);
    908 		PRWORD(ovflw, " %*d", 5, 1, fp->f_usecount);
    909 		PRWORD(ovflw, " %*x", 5, 1, fp->f_iflags);
    910 		if (fp->f_offset < 0)
    911 			PRWORD(ovflw, "  %-*lld\n", PTRSTRWIDTH + 1, 2,
    912 			    (long long)fp->f_offset);
    913 		else
    914 			PRWORD(ovflw, "  %-*lld\n", PTRSTRWIDTH + 1, 2,
    915 			    (long long)fp->f_offset);
    916 	}
    917 	free(buf);
    918 }
    919 
    920 int
    921 getfiles(char **abuf, int *alen, char **aoffset)
    922 {
    923 	size_t len;
    924 	int mib[2];
    925 	char *buf;
    926 	size_t offset;
    927 
    928 	/*
    929 	 * XXX
    930 	 * Add emulation of KINFO_FILE here.
    931 	 */
    932 	if (memf != NULL)
    933 		errx(1, "files on dead kernel, not implemented");
    934 
    935 	mib[0] = CTL_KERN;
    936 	mib[1] = KERN_FILE;
    937 	if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) {
    938 		warn("sysctl: KERN_FILE");
    939 		return (-1);
    940 	}
    941 	/* We need to align (struct file *) in the buffer. */
    942 	offset = len % sizeof(off_t);
    943 	if ((buf = malloc(len + offset)) == NULL)
    944 		err(1, "malloc");
    945 	if (sysctl(mib, 2, buf + offset, &len, NULL, 0) == -1) {
    946 		warn("sysctl: KERN_FILE");
    947 		return (-1);
    948 	}
    949 	*abuf = buf;
    950 	*alen = len;
    951 	*aoffset = (buf + offset);
    952 	return (0);
    953 }
    954 
    955 void
    956 usage(void)
    957 {
    958 
    959 	(void)fprintf(stderr,
    960 	    "usage: %s [-T|-f|-s|-t|-v] [-ghkmn] [-M core] [-N system]\n",
    961 	    getprogname());
    962 	exit(1);
    963 }
    964