2 * Copyright (c) 1980, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1980, 1991, 1993, 1994 The Regents of the University of California. All rights reserved.
30 * @(#)pstat.c 8.16 (Berkeley) 5/9/95
31 * $FreeBSD: src/usr.sbin/pstat/pstat.c,v 1.49.2.5 2002/07/12 09:12:49 des Exp $
35 #include <sys/kinfo.h>
36 #include <sys/param.h>
38 #include <sys/vnode.h>
39 #include <sys/ucred.h>
41 #include <vfs/ufs/quota.h>
42 #include <vfs/ufs/inode.h>
43 #include <sys/mount.h>
45 #include <sys/namei.h>
46 #include <vfs/union/union.h>
48 #include <nfs/rpcv2.h>
49 #include <nfs/nfsproto.h>
51 #include <nfs/nfsnode.h>
52 #include <sys/ioctl.h>
53 #include <sys/ioctl_compat.h> /* XXX NTTYDISC is too well hidden */
56 #include <sys/blist.h>
58 #include <sys/sysctl.h>
71 # define KCORE_KINFO_WRAPPER
88 const size_t NL_LAST_MANDATORY = NL_NUMVNODES;
91 { "_mountlist", 0, 0, 0, 0 }, /* address of head of mount list. */
92 { "_numvnodes", 0, 0, 0, 0 },
93 { "_rc_tty", 0, 0, 0, 0 },
94 { "_nrc_tty", 0, 0, 0, 0 },
95 { "_cy_tty", 0, 0, 0, 0 },
96 { "_ncy_tty", 0, 0, 0, 0 },
97 { "_si__tty", 0, 0, 0, 0 },
98 { "_si_Nports", 0, 0, 0, 0 },
109 const char *usagestr;
115 { MNT_RDONLY, "rdonly" },
116 { MNT_SYNCHRONOUS, "sync" },
117 { MNT_NOEXEC, "noexec" },
118 { MNT_NOSUID, "nosuid" },
119 { MNT_NODEV, "nodev" },
120 { MNT_UNION, "union" },
121 { MNT_ASYNC, "async" },
122 { MNT_SUIDDIR, "suiddir" },
123 { MNT_SOFTDEP, "softdep" },
124 { MNT_NOSYMFOLLOW, "nosymfollow" },
125 { MNT_NOATIME, "noatime" },
126 { MNT_NOCLUSTERR, "noclusterread" },
127 { MNT_NOCLUSTERW, "noclusterwrite" },
128 { MNT_EXRDONLY, "exrdonly" },
129 { MNT_EXPORTED, "exported" },
130 { MNT_DEFEXPORTED, "defexported" },
131 { MNT_EXPORTANON, "exportanon" },
132 { MNT_EXKERB, "exkerb" },
133 { MNT_EXPUBLIC, "public" },
134 { MNT_LOCAL, "local" },
135 { MNT_QUOTA, "quota" },
136 { MNT_ROOTFS, "rootfs" },
137 { MNT_USER, "user" },
138 { MNT_IGNORE, "ignore" },
139 { MNT_UPDATE, "update" },
140 { MNT_DELEXPORT, "delexport" },
141 { MNT_RELOAD, "reload" },
142 { MNT_FORCE, "force" },
147 #define SVAR(var) __STRING(var) /* to force expansion */
148 #define KGET(idx, var) \
149 KGET1(idx, &var, sizeof(var), SVAR(var))
150 #define KGET1(idx, p, s, msg) \
151 KGET2(nl[idx].n_value, p, s, msg)
152 #define KGET2(addr, p, s, msg) \
153 if (kvm_read(kd, (u_long)(addr), p, s) != s) \
154 warnx("cannot read %s: %s", msg, kvm_geterr(kd))
155 #define KGETN(idx, var) \
156 KGET1N(idx, &var, sizeof(var), SVAR(var))
157 #define KGET1N(idx, p, s, msg) \
158 KGET2N(nl[idx].n_value, p, s, msg)
159 #define KGET2N(addr, p, s, msg) \
160 ((kvm_read(kd, (u_long)(addr), p, s) == s) ? 1 : 0)
161 #define KGETRET(addr, p, s, msg) \
162 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \
163 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \
168 int getfiles(char **, int *);
170 getmnt(struct mount *);
175 void mount_print(struct mount *);
176 void nfs_header(void);
177 int nfs_print(struct vnode *);
180 void ttyprt(struct tty *, int);
181 void ttytype(struct tty *, const char *, int, int, int);
182 void ufs_header(void);
183 int ufs_print(struct vnode *);
184 void union_header(void);
185 int union_print(struct vnode *);
186 static void usage(void);
187 void vnode_header(void);
188 void vnode_print(struct vnode *, struct vnode *);
189 void vnodemode(void);
192 main(int argc, char **argv)
195 int fileflag, ttyflag, vnodeflag;
196 char buf[_POSIX2_LINE_MAX];
199 fileflag = swapflag = ttyflag = vnodeflag = 0;
201 /* We will behave like good old swapinfo if thus invoked */
202 opts = strrchr(argv[0],'/');
207 if (!strcmp(opts,"swapinfo")) {
210 usagestr = "swapinfo [-k] [-M core] [-N system]";
212 opts = "TM:N:fiknstv";
213 usagestr = "pstat [-Tfknst] [-M core] [-N system]";
216 while ((ch = getopt(argc, argv, opts)) != -1)
222 if (setenv("BLOCKSIZE", "1K", 1) == -1)
223 warn("setenv: cannot set BLOCKSIZE=1K");
244 case 'i': /* Backward compatibility. */
245 errx(1, "vnode mode not supported");
257 * Discard setgid privileges if not the running kernel so that bad
258 * guys can't print interesting stuff from kernel memory.
260 if (nlistf != NULL || memf != NULL)
263 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
264 errx(1, "kvm_openfiles: %s", buf);
266 if (kcore_wrapper_open(nlistf, memf, buf))
267 errx(1, "kcore_open: %s", buf);
269 if ((ret = kvm_nlist(kd, nl)) != 0) {
274 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
275 for (i = 0; i < NL_LAST_MANDATORY; i++) {
276 if (!nl[i].n_value) {
278 warnx("undefined symbol: %s", nl[i].n_name);
284 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
286 if (fileflag || totalflag)
292 if (swapflag || totalflag)
300 fprintf(stderr, "usage: %s\n", usagestr);
305 struct vnode *avnode;
312 struct e_vnode *e_vnodebase, *endvnode, *evp;
314 struct mount *maddr, *mp = NULL;
317 e_vnodebase = loadvnodes(&numvnodes);
319 printf("%7d vnodes\n", numvnodes);
322 endvnode = e_vnodebase + numvnodes;
323 printf("%d active vnodes\n", numvnodes);
326 #define ST mp->mnt_stat
328 for (evp = e_vnodebase; evp < endvnode; evp++) {
330 if (vp->v_mount != maddr) {
334 if ((mp = getmnt(vp->v_mount)) == NULL)
339 if (!strcmp(ST.f_fstypename, "ufs") ||
340 !strcmp(ST.f_fstypename, "mfs"))
342 else if (!strcmp(ST.f_fstypename, "nfs"))
344 else if (!strcmp(ST.f_fstypename, "union"))
348 vnode_print(evp->avnode, vp);
349 if (!strcmp(ST.f_fstypename, "ufs") ||
350 !strcmp(ST.f_fstypename, "mfs"))
352 else if (!strcmp(ST.f_fstypename, "nfs"))
354 else if (!strcmp(ST.f_fstypename, "union"))
364 printf("ADDR TYP VFLAG USE HOLD");
368 vnode_print(struct vnode *avnode, struct vnode *vp)
379 switch (vp->v_type) {
421 if (flag & (VAGE0 | VAGE1))
433 if (flag & VONWORKLST)
436 if (flag & VINACTIVE)
438 if (flag & VRECLAIMED)
447 * Convert SYSREF ref counts into something more
448 * human readable for display.
450 if ((refs = vp->v_sysref.refcnt) < 0)
451 refs = -(refs + 0x40000001);
452 printf("%8lx %s %5s %4d %4d",
453 (u_long)(void *)avnode, type, flags, refs, vp->v_auxrefs);
459 printf(" FILEID IFLAG RDEV|SZ");
463 ufs_print(struct vnode *vp)
466 struct inode inode, *ip = &inode;
467 char flagbuf[16], *flags = flagbuf;
471 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
473 if (flag & IN_ACCESS)
475 if (flag & IN_CHANGE)
477 if (flag & IN_UPDATE)
479 if (flag & IN_MODIFIED)
481 if (flag & IN_RENAME)
483 if (flag & IN_SHLOCK)
485 if (flag & IN_EXLOCK)
487 if (flag & IN_HASHED)
489 if (flag & IN_LAZYMOD)
495 printf(" %6ju %5s", (uintmax_t)ip->i_number, flagbuf);
496 type = ip->i_mode & S_IFMT;
497 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
498 if (usenumflag || ((name = devname(ip->i_rdev, type)) == NULL))
500 major(ip->i_rdev), minor(ip->i_rdev));
502 printf(" %7s", name);
504 printf(" %7qd", ip->i_size);
511 printf(" FILEID NFLAG RDEV|SZ");
515 nfs_print(struct vnode *vp)
517 struct nfsnode nfsnode, *np = &nfsnode;
518 char flagbuf[16], *flags = flagbuf;
523 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
525 if (flag & NFLUSHWANT)
527 if (flag & NFLUSHINPROG)
529 if (flag & NLMODIFIED)
531 if (flag & NRMODIFIED)
533 if (flag & NWRITEERR)
535 if (flag & NQNFSEVICTED)
551 #define VT np->n_vattr
552 printf(" %6ju %5s", (uintmax_t)VT.va_fileid, flagbuf);
553 type = VT.va_mode & S_IFMT;
554 if (S_ISCHR(VT.va_mode) || S_ISBLK(VT.va_mode))
555 if (usenumflag || ((name = devname((VT.va_rmajor << 8) | VT.va_rminor, type)) == NULL))
557 VT.va_rmajor, VT.va_rminor);
559 printf(" %7s", name);
561 printf(" %7qd", np->n_size);
568 printf(" UPPER LOWER");
572 union_print(struct vnode *vp)
574 struct union_node unode, *up = &unode;
576 KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode");
578 printf(" %8lx %8lx", (u_long)(void *)up->un_uppervp,
579 (u_long)(void *)up->un_lowervp);
584 * Given a pointer to a mount structure in kernel space,
585 * read it in and return a usable pointer to it.
588 getmnt(struct mount *maddr)
597 for (mt = mhead; mt != NULL; mt = mt->next)
598 if (maddr == mt->maddr)
600 if ((mt = malloc(sizeof(struct mtab))) == NULL)
602 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
610 mount_print(struct mount *mp)
614 #define ST mp->mnt_stat
615 printf("*** MOUNT %s %s on %s", ST.f_fstypename,
616 ST.f_mntfromname, ST.f_mntonname);
617 if ((flags = mp->mnt_flag)) {
619 const char *sep = " (";
621 for (i = 0; mnt_flags[i].m_flag; i++) {
622 if (flags & mnt_flags[i].m_flag) {
623 printf("%s%s", sep, mnt_flags[i].m_name);
624 flags &= ~mnt_flags[i].m_flag;
629 printf("%sunknown_flags:%x", sep, flags);
637 loadvnodes(int *avnodes)
641 struct e_vnode *vnodebase;
647 return (kinfo_vnodes(avnodes));
651 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1)
652 err(1, "sysctl: KERN_VNODE");
653 if ((vnodebase = malloc(copysize)) == NULL)
655 if (sysctl(mib, 2, vnodebase, ©size, NULL, 0) == -1)
656 err(1, "sysctl: KERN_VNODE");
657 if (copysize % sizeof(struct e_vnode))
658 errx(1, "vnode size mismatch");
659 *avnodes = copysize / sizeof(struct e_vnode);
665 * simulate what a running kernel does in in kinfo_vnode
668 kinfo_vnodes(int *avnodes)
670 struct mntlist mountlist;
671 struct mount *mp, mounth, *mp_next;
672 struct vnode *vp, vnode, *vp_next;
673 char *vbuf, *evbuf, *bp;
676 #define VPTRSZ sizeof(struct vnode *)
677 #define VNODESZ sizeof(struct vnode)
679 KGET(NL_NUMVNODES, numvnodes);
680 if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
683 evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ);
684 KGET(NL_MOUNTLIST, mountlist);
685 for (num = 0, mp = TAILQ_FIRST(&mountlist); ; mp = mp_next) {
686 KGET2(mp, &mounth, sizeof(mounth), "mount entry");
687 mp_next = TAILQ_NEXT(&mounth, mnt_list);
688 for (vp = TAILQ_FIRST(&mounth.mnt_nvnodelist);
689 vp != NULL; vp = vp_next) {
690 KGET2(vp, &vnode, sizeof(vnode), "vnode");
691 vp_next = TAILQ_NEXT(&vnode, v_nmntvnodes);
692 if ((bp + VPTRSZ + VNODESZ) > evbuf)
693 /* XXX - should realloc */
694 errx(1, "no more room for vnodes");
695 memmove(bp, &vp, VPTRSZ);
697 memmove(bp, &vnode, VNODESZ);
701 if (mp == TAILQ_LAST(&mountlist, mntlist))
705 return ((struct e_vnode *)vbuf);
709 " LINE RAW CAN OUT IHIWT ILOWT OHWT LWT COL STATE SESS PGID DISC\n";
716 struct tty ttyb[1000];
722 error = sysctlbyname("kern.ttys", &ttyb, &len, 0, 0);
724 len /= sizeof(ttyb[0]);
725 for (i = 0; i < len; i++) {
729 if ((tty = malloc(ttyspace * sizeof(*tty))) == NULL)
731 if (nl[NL_NRC_TTY].n_type != 0)
732 ttytype(tty, "rc", NL_RC_TTY, NL_NRC_TTY, 0);
733 if (nl[NL_NCY_TTY].n_type != 0)
734 ttytype(tty, "cy", NL_CY_TTY, NL_NCY_TTY, 0);
735 if (nl[NL_SI_NPORTS].n_type != 0)
736 ttytype(tty, "si", NL_SI_TTY, NL_SI_NPORTS, 1);
741 ttytype(struct tty *tty, const char *name, int type, int number, int indir)
745 struct tty **ttyaddr;
750 printf("%d %s %s\n", ntty, name, (ntty == 1) ? "line" : "lines");
751 if (ntty > ttyspace) {
753 if ((tty = realloc(tty, ttyspace * sizeof(*tty))) == NULL)
758 KGET2(ttyaddr, tty, (ssize_t)(ntty * sizeof(struct tty)),
761 KGET1(type, tty, (ssize_t)(ntty * sizeof(struct tty)),
765 for (tp = tty; tp < &tty[ntty]; tp++)
766 ttyprt(tp, tp - tty);
779 { TS_CONNECTED, 'c'},
788 { TS_SO_OLOWAT, 'A'},
790 #ifdef TS_SO_OCOMPLETE
791 { TS_SO_OCOMPLETE, 'a'},
796 { TS_CAR_OFLOW, 'm'},
799 { TS_CTS_OFLOW, 'o'},
802 { TS_DSR_OFLOW, 'd'},
811 #ifdef TS_CAN_BYPASS_L_RINT
812 { TS_CAN_BYPASS_L_RINT, 'l'},
824 ttyprt(struct tty *tp, int line)
828 char *name, state[20];
830 if (usenumflag || tp->t_dev == 0 ||
831 (name = devname(tp->t_dev, S_IFCHR)) == NULL)
832 printf("%7d ", line);
834 printf("%7s ", name);
835 printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc);
836 printf("%3d %5d %5d %4d %3d %7d ", tp->t_outq.c_cc,
837 tp->t_ihiwat, tp->t_ilowat, tp->t_ohiwat, tp->t_olowat,
839 for (i = j = 0; ttystates[i].flag; i++)
840 if (tp->t_state&ttystates[i].flag)
841 state[j++] = ttystates[i].val;
845 printf("%-6s %8lx", state, (u_long)(void *)tp->t_session);
847 if (tp->t_pgrp != NULL)
848 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
849 printf("%6d ", pgid);
850 switch (tp->t_line) {
864 printf("%d\n", tp->t_line);
872 struct kinfo_file *fp, *ofp;
874 char flagbuf[16], *fbp;
876 static const char *dtypes[] = { "???", "inode", "socket" };
878 if (kinfo_get_maxfiles(&maxfile))
879 err(1, "kinfo_get_maxfiles");
881 if (kinfo_get_openfiles(&nfile))
882 err(1, "kinfo_get_openfiles");
883 printf("%3d/%3d files\n", nfile, maxfile);
886 if (kinfo_get_files(&fp, &len))
887 err(1, "kinfo_get_files");
890 printf("%d/%d open files\n", len, maxfile);
891 printf(" LOC TYPE FLG CNT MSG DATA OFFSET\n");
892 for (; len-- > 0; fp++) {
893 if ((unsigned)fp->f_type > DTYPE_SOCKET)
895 printf("%p ", fp->f_file);
896 printf("%-8.8s", dtypes[fp->f_type]);
898 if (fp->f_flag & FREAD)
900 if (fp->f_flag & FWRITE)
902 if (fp->f_flag & FAPPEND)
904 #ifdef FSHLOCK /* currently gone */
905 if (fp->f_flag & FSHLOCK)
907 if (fp->f_flag & FEXLOCK)
910 if (fp->f_flag & FASYNC)
913 printf("%6s %3d", flagbuf, fp->f_count);
914 printf(" %3d", fp->f_msgcount);
915 printf(" %8lx", (u_long)(void *)fp->f_data);
916 if (fp->f_offset < 0)
917 printf(" %qx\n", fp->f_offset);
919 printf(" %qd\n", fp->f_offset);
925 * swapmode is based on a program called swapinfo written
926 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
931 struct kvm_swap kswap[16];
934 int pagesize = getpagesize();
942 sizeof(kswap)/sizeof(kswap[0]),
943 ((swapflag > 1) ? SWIF_DUMP_TREE : 0) | SWIF_DEV_PREFIX
946 #define CONVERT(v) ((int)((quad_t)(v) * pagesize / blocksize))
948 header = getbsize(&hlen, &blocksize);
949 if (totalflag == 0) {
950 printf("%-15s %*s %8s %8s %8s %s\n",
951 "Device", hlen, header,
952 "Used", "Avail", "Capacity", "Type");
954 for (i = 0; i < n; ++i) {
957 kswap[i].ksw_devname,
959 CONVERT(kswap[i].ksw_total)
962 "%8d %8d %5.0f%% %s\n",
963 CONVERT(kswap[i].ksw_used),
964 CONVERT(kswap[i].ksw_total - kswap[i].ksw_used),
965 (double)kswap[i].ksw_used * 100.0 /
966 (double)kswap[i].ksw_total,
967 (kswap[i].ksw_flags & SW_SEQUENTIAL) ?
968 "Sequential" : "Interleaved"
974 blocksize = 1024 * 1024;
977 "%dM/%dM swap space\n",
978 CONVERT(kswap[n].ksw_used),
979 CONVERT(kswap[n].ksw_total)
983 "%-15s %*d %8d %8d %5.0f%%\n",
986 CONVERT(kswap[n].ksw_total),
987 CONVERT(kswap[n].ksw_used),
988 CONVERT(kswap[n].ksw_total - kswap[n].ksw_used),
989 (double)kswap[n].ksw_used * 100.0 /
990 (double)kswap[n].ksw_total