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