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