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