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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1980, 1991, 1993, 1994 The Regents of the University of California. All rights reserved.
34 * @(#)pstat.c 8.16 (Berkeley) 5/9/95
35 * $FreeBSD: src/usr.sbin/pstat/pstat.c,v 1.49.2.5 2002/07/12 09:12:49 des Exp $
36 * $DragonFly: src/usr.sbin/pstat/pstat.c,v 1.22 2008/03/04 18:10:44 swildner Exp $
40 #include <sys/kinfo.h>
41 #include <sys/param.h>
43 #include <sys/vnode.h>
44 #include <sys/ucred.h>
46 #include <vfs/ufs/quota.h>
47 #include <vfs/ufs/inode.h>
48 #include <sys/mount.h>
50 #include <sys/namei.h>
51 #include <vfs/union/union.h>
53 #include <nfs/rpcv2.h>
54 #include <nfs/nfsproto.h>
56 #include <nfs/nfsnode.h>
57 #include <sys/ioctl.h>
58 #include <sys/ioctl_compat.h> /* XXX NTTYDISC is too well hidden */
61 #include <sys/blist.h>
63 #include <sys/sysctl.h>
76 # define KCORE_KINFO_WRAPPER
93 const size_t NL_LAST_MANDATORY = NL_NUMVNODES;
96 { "_mountlist", 0, 0, 0, 0 }, /* address of head of mount list. */
97 { "_numvnodes", 0, 0, 0, 0 },
98 { "_rc_tty", 0, 0, 0, 0 },
99 { "_nrc_tty", 0, 0, 0, 0 },
100 { "_cy_tty", 0, 0, 0, 0 },
101 { "_ncy_tty", 0, 0, 0, 0 },
102 { "_si__tty", 0, 0, 0, 0 },
103 { "_si_Nports", 0, 0, 0, 0 },
114 const char *usagestr;
120 { MNT_RDONLY, "rdonly" },
121 { MNT_SYNCHRONOUS, "sync" },
122 { MNT_NOEXEC, "noexec" },
123 { MNT_NOSUID, "nosuid" },
124 { MNT_NODEV, "nodev" },
125 { MNT_UNION, "union" },
126 { MNT_ASYNC, "async" },
127 { MNT_SUIDDIR, "suiddir" },
128 { MNT_SOFTDEP, "softdep" },
129 { MNT_NOSYMFOLLOW, "nosymfollow" },
130 { MNT_NOATIME, "noatime" },
131 { MNT_NOCLUSTERR, "noclusterread" },
132 { MNT_NOCLUSTERW, "noclusterwrite" },
133 { MNT_EXRDONLY, "exrdonly" },
134 { MNT_EXPORTED, "exported" },
135 { MNT_DEFEXPORTED, "defexported" },
136 { MNT_EXPORTANON, "exportanon" },
137 { MNT_EXKERB, "exkerb" },
138 { MNT_EXPUBLIC, "public" },
139 { MNT_LOCAL, "local" },
140 { MNT_QUOTA, "quota" },
141 { MNT_ROOTFS, "rootfs" },
142 { MNT_USER, "user" },
143 { MNT_IGNORE, "ignore" },
144 { MNT_UPDATE, "update" },
145 { MNT_DELEXPORT, "delexport" },
146 { MNT_RELOAD, "reload" },
147 { MNT_FORCE, "force" },
152 #define SVAR(var) __STRING(var) /* to force expansion */
153 #define KGET(idx, var) \
154 KGET1(idx, &var, sizeof(var), SVAR(var))
155 #define KGET1(idx, p, s, msg) \
156 KGET2(nl[idx].n_value, p, s, msg)
157 #define KGET2(addr, p, s, msg) \
158 if (kvm_read(kd, (u_long)(addr), p, s) != s) \
159 warnx("cannot read %s: %s", msg, kvm_geterr(kd))
160 #define KGETN(idx, var) \
161 KGET1N(idx, &var, sizeof(var), SVAR(var))
162 #define KGET1N(idx, p, s, msg) \
163 KGET2N(nl[idx].n_value, p, s, msg)
164 #define KGET2N(addr, p, s, msg) \
165 ((kvm_read(kd, (u_long)(addr), p, s) == s) ? 1 : 0)
166 #define KGETRET(addr, p, s, msg) \
167 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \
168 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \
173 int getfiles(char **, int *);
175 getmnt(struct mount *);
180 void mount_print(struct mount *);
181 void nfs_header(void);
182 int nfs_print(struct vnode *);
185 void ttyprt(struct tty *, int);
186 void ttytype(struct tty *, const char *, int, int, int);
187 void ufs_header(void);
188 int ufs_print(struct vnode *);
189 void union_header(void);
190 int union_print(struct vnode *);
191 static void usage(void);
192 void vnode_header(void);
193 void vnode_print(struct vnode *, struct vnode *);
194 void vnodemode(void);
197 main(int argc, char **argv)
200 int fileflag, ttyflag, vnodeflag;
201 char buf[_POSIX2_LINE_MAX];
204 fileflag = swapflag = ttyflag = vnodeflag = 0;
206 /* We will behave like good old swapinfo if thus invoked */
207 opts = strrchr(argv[0],'/');
212 if (!strcmp(opts,"swapinfo")) {
215 usagestr = "swapinfo [-k] [-M core] [-N system]";
217 opts = "TM:N:fiknstv";
218 usagestr = "pstat [-Tfknst] [-M core] [-N system]";
221 while ((ch = getopt(argc, argv, opts)) != -1)
227 if (setenv("BLOCKSIZE", "1K", 1) == -1)
228 warn("setenv: cannot set BLOCKSIZE=1K");
249 case 'i': /* Backward compatibility. */
250 errx(1, "vnode mode not supported");
262 * Discard setgid privileges if not the running kernel so that bad
263 * guys can't print interesting stuff from kernel memory.
265 if (nlistf != NULL || memf != NULL)
268 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0)
269 errx(1, "kvm_openfiles: %s", buf);
271 if (kcore_wrapper_open(nlistf, memf, buf))
272 errx(1, "kcore_open: %s", buf);
274 if ((ret = kvm_nlist(kd, nl)) != 0) {
279 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
280 for (i = 0; i < NL_LAST_MANDATORY; i++) {
281 if (!nl[i].n_value) {
283 warnx("undefined symbol: %s", nl[i].n_name);
289 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
291 if (fileflag || totalflag)
297 if (swapflag || totalflag)
305 fprintf(stderr, "usage: %s\n", usagestr);
310 struct vnode *avnode;
317 struct e_vnode *e_vnodebase, *endvnode, *evp;
319 struct mount *maddr, *mp = NULL;
322 e_vnodebase = loadvnodes(&numvnodes);
324 printf("%7d vnodes\n", numvnodes);
327 endvnode = e_vnodebase + numvnodes;
328 printf("%d active vnodes\n", numvnodes);
331 #define ST mp->mnt_stat
333 for (evp = e_vnodebase; evp < endvnode; evp++) {
335 if (vp->v_mount != maddr) {
339 if ((mp = getmnt(vp->v_mount)) == NULL)
344 if (!strcmp(ST.f_fstypename, "ufs") ||
345 !strcmp(ST.f_fstypename, "mfs"))
347 else if (!strcmp(ST.f_fstypename, "nfs"))
349 else if (!strcmp(ST.f_fstypename, "union"))
353 vnode_print(evp->avnode, vp);
354 if (!strcmp(ST.f_fstypename, "ufs") ||
355 !strcmp(ST.f_fstypename, "mfs"))
357 else if (!strcmp(ST.f_fstypename, "nfs"))
359 else if (!strcmp(ST.f_fstypename, "union"))
369 printf("ADDR TYP VFLAG USE HOLD");
373 vnode_print(struct vnode *avnode, struct vnode *vp)
384 switch (vp->v_type) {
426 if (flag & (VAGE0 | VAGE1))
438 if (flag & VONWORKLST)
443 if (flag & VINACTIVE)
445 if (flag & VRECLAIMED)
454 * Convert SYSREF ref counts into something more
455 * human readable for display.
457 if ((refs = vp->v_sysref.refcnt) < 0)
458 refs = -(refs + 0x40000001);
459 printf("%8lx %s %5s %4d %4d",
460 (u_long)(void *)avnode, type, flags, refs, vp->v_auxrefs);
466 printf(" FILEID IFLAG RDEV|SZ");
470 ufs_print(struct vnode *vp)
473 struct inode inode, *ip = &inode;
474 char flagbuf[16], *flags = flagbuf;
478 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
480 if (flag & IN_ACCESS)
482 if (flag & IN_CHANGE)
484 if (flag & IN_UPDATE)
486 if (flag & IN_MODIFIED)
488 if (flag & IN_RENAME)
490 if (flag & IN_SHLOCK)
492 if (flag & IN_EXLOCK)
494 if (flag & IN_HASHED)
496 if (flag & IN_LAZYMOD)
502 printf(" %6ju %5s", (uintmax_t)ip->i_number, flagbuf);
503 type = ip->i_mode & S_IFMT;
504 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
505 if (usenumflag || ((name = devname(ip->i_rdev, type)) == NULL))
507 major(ip->i_rdev), minor(ip->i_rdev));
509 printf(" %7s", name);
511 printf(" %7qd", ip->i_size);
518 printf(" FILEID NFLAG RDEV|SZ");
522 nfs_print(struct vnode *vp)
524 struct nfsnode nfsnode, *np = &nfsnode;
525 char flagbuf[16], *flags = flagbuf;
530 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
532 if (flag & NFLUSHWANT)
534 if (flag & NFLUSHINPROG)
536 if (flag & NLMODIFIED)
538 if (flag & NRMODIFIED)
540 if (flag & NWRITEERR)
542 if (flag & NQNFSEVICTED)
558 #define VT np->n_vattr
559 printf(" %6ju %5s", (uintmax_t)VT.va_fileid, flagbuf);
560 type = VT.va_mode & S_IFMT;
561 if (S_ISCHR(VT.va_mode) || S_ISBLK(VT.va_mode))
562 if (usenumflag || ((name = devname((VT.va_rmajor << 8) | VT.va_rminor, type)) == NULL))
564 VT.va_rmajor, VT.va_rminor);
566 printf(" %7s", name);
568 printf(" %7qd", np->n_size);
575 printf(" UPPER LOWER");
579 union_print(struct vnode *vp)
581 struct union_node unode, *up = &unode;
583 KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode");
585 printf(" %8lx %8lx", (u_long)(void *)up->un_uppervp,
586 (u_long)(void *)up->un_lowervp);
591 * Given a pointer to a mount structure in kernel space,
592 * read it in and return a usable pointer to it.
595 getmnt(struct mount *maddr)
604 for (mt = mhead; mt != NULL; mt = mt->next)
605 if (maddr == mt->maddr)
607 if ((mt = malloc(sizeof(struct mtab))) == NULL)
609 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
617 mount_print(struct mount *mp)
621 #define ST mp->mnt_stat
622 printf("*** MOUNT %s %s on %s", ST.f_fstypename,
623 ST.f_mntfromname, ST.f_mntonname);
624 if ((flags = mp->mnt_flag)) {
626 const char *sep = " (";
628 for (i = 0; mnt_flags[i].m_flag; i++) {
629 if (flags & mnt_flags[i].m_flag) {
630 printf("%s%s", sep, mnt_flags[i].m_name);
631 flags &= ~mnt_flags[i].m_flag;
636 printf("%sunknown_flags:%x", sep, flags);
644 loadvnodes(int *avnodes)
648 struct e_vnode *vnodebase;
654 return (kinfo_vnodes(avnodes));
658 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1)
659 err(1, "sysctl: KERN_VNODE");
660 if ((vnodebase = malloc(copysize)) == NULL)
662 if (sysctl(mib, 2, vnodebase, ©size, NULL, 0) == -1)
663 err(1, "sysctl: KERN_VNODE");
664 if (copysize % sizeof(struct e_vnode))
665 errx(1, "vnode size mismatch");
666 *avnodes = copysize / sizeof(struct e_vnode);
672 * simulate what a running kernel does in in kinfo_vnode
675 kinfo_vnodes(int *avnodes)
677 struct mntlist mountlist;
678 struct mount *mp, mounth, *mp_next;
679 struct vnode *vp, vnode, *vp_next;
680 char *vbuf, *evbuf, *bp;
683 #define VPTRSZ sizeof(struct vnode *)
684 #define VNODESZ sizeof(struct vnode)
686 KGET(NL_NUMVNODES, numvnodes);
687 if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
690 evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ);
691 KGET(NL_MOUNTLIST, mountlist);
692 for (num = 0, mp = TAILQ_FIRST(&mountlist); ; mp = mp_next) {
693 KGET2(mp, &mounth, sizeof(mounth), "mount entry");
694 mp_next = TAILQ_NEXT(&mounth, mnt_list);
695 for (vp = TAILQ_FIRST(&mounth.mnt_nvnodelist);
696 vp != NULL; vp = vp_next) {
697 KGET2(vp, &vnode, sizeof(vnode), "vnode");
698 vp_next = TAILQ_NEXT(&vnode, v_nmntvnodes);
699 if ((bp + VPTRSZ + VNODESZ) > evbuf)
700 /* XXX - should realloc */
701 errx(1, "no more room for vnodes");
702 memmove(bp, &vp, VPTRSZ);
704 memmove(bp, &vnode, VNODESZ);
708 if (mp == TAILQ_LAST(&mountlist, mntlist))
712 return ((struct e_vnode *)vbuf);
716 " LINE RAW CAN OUT IHIWT ILOWT OHWT LWT COL STATE SESS PGID DISC\n";
723 struct tty ttyb[1000];
729 error = sysctlbyname("kern.ttys", &ttyb, &len, 0, 0);
731 len /= sizeof(ttyb[0]);
732 for (i = 0; i < len; i++) {
736 if ((tty = malloc(ttyspace * sizeof(*tty))) == NULL)
738 if (nl[NL_NRC_TTY].n_type != 0)
739 ttytype(tty, "rc", NL_RC_TTY, NL_NRC_TTY, 0);
740 if (nl[NL_NCY_TTY].n_type != 0)
741 ttytype(tty, "cy", NL_CY_TTY, NL_NCY_TTY, 0);
742 if (nl[NL_SI_NPORTS].n_type != 0)
743 ttytype(tty, "si", NL_SI_TTY, NL_SI_NPORTS, 1);
748 ttytype(struct tty *tty, const char *name, int type, int number, int indir)
752 struct tty **ttyaddr;
757 printf("%d %s %s\n", ntty, name, (ntty == 1) ? "line" : "lines");
758 if (ntty > ttyspace) {
760 if ((tty = realloc(tty, ttyspace * sizeof(*tty))) == 0)
765 KGET2(ttyaddr, tty, (ssize_t)(ntty * sizeof(struct tty)),
768 KGET1(type, tty, (ssize_t)(ntty * sizeof(struct tty)),
772 for (tp = tty; tp < &tty[ntty]; tp++)
773 ttyprt(tp, tp - tty);
786 { TS_CONNECTED, 'c'},
795 { TS_SO_OLOWAT, 'A'},
797 #ifdef TS_SO_OCOMPLETE
798 { TS_SO_OCOMPLETE, 'a'},
803 { TS_CAR_OFLOW, 'm'},
806 { TS_CTS_OFLOW, 'o'},
809 { TS_DSR_OFLOW, 'd'},
818 #ifdef TS_CAN_BYPASS_L_RINT
819 { TS_CAN_BYPASS_L_RINT, 'l'},
831 ttyprt(struct tty *tp, int line)
835 char *name, state[20];
837 if (usenumflag || tp->t_dev == 0 ||
838 (name = devname(tp->t_dev, S_IFCHR)) == NULL)
839 printf("%7d ", line);
841 printf("%7s ", name);
842 printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc);
843 printf("%3d %5d %5d %4d %3d %7d ", tp->t_outq.c_cc,
844 tp->t_ihiwat, tp->t_ilowat, tp->t_ohiwat, tp->t_olowat,
846 for (i = j = 0; ttystates[i].flag; i++)
847 if (tp->t_state&ttystates[i].flag)
848 state[j++] = ttystates[i].val;
852 printf("%-6s %8lx", state, (u_long)(void *)tp->t_session);
854 if (tp->t_pgrp != NULL)
855 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
856 printf("%6d ", pgid);
857 switch (tp->t_line) {
871 printf("%d\n", tp->t_line);
879 struct kinfo_file *fp, *ofp;
881 char flagbuf[16], *fbp;
883 static const char *dtypes[] = { "???", "inode", "socket" };
885 if (kinfo_get_maxfiles(&maxfile))
886 err(1, "kinfo_get_maxfiles");
888 if (kinfo_get_openfiles(&nfile))
889 err(1, "kinfo_get_openfiles");
890 printf("%3d/%3d files\n", nfile, maxfile);
893 if (kinfo_get_files(&fp, &len))
894 err(1, "kinfo_get_files");
897 printf("%d/%d open files\n", len, maxfile);
898 printf(" LOC TYPE FLG CNT MSG DATA OFFSET\n");
899 for (; len-- > 0; fp++) {
900 if ((unsigned)fp->f_type > DTYPE_SOCKET)
902 printf("%p ", fp->f_file);
903 printf("%-8.8s", dtypes[fp->f_type]);
905 if (fp->f_flag & FREAD)
907 if (fp->f_flag & FWRITE)
909 if (fp->f_flag & FAPPEND)
911 #ifdef FSHLOCK /* currently gone */
912 if (fp->f_flag & FSHLOCK)
914 if (fp->f_flag & FEXLOCK)
917 if (fp->f_flag & FASYNC)
920 printf("%6s %3d", flagbuf, fp->f_count);
921 printf(" %3d", fp->f_msgcount);
922 printf(" %8lx", (u_long)(void *)fp->f_data);
923 if (fp->f_offset < 0)
924 printf(" %qx\n", fp->f_offset);
926 printf(" %qd\n", fp->f_offset);
932 * swapmode is based on a program called swapinfo written
933 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
938 struct kvm_swap kswap[16];
941 int pagesize = getpagesize();
949 sizeof(kswap)/sizeof(kswap[0]),
950 ((swapflag > 1) ? SWIF_DUMP_TREE : 0) | SWIF_DEV_PREFIX
953 #define CONVERT(v) ((int)((quad_t)(v) * pagesize / blocksize))
955 header = getbsize(&hlen, &blocksize);
956 if (totalflag == 0) {
957 printf("%-15s %*s %8s %8s %8s %s\n",
958 "Device", hlen, header,
959 "Used", "Avail", "Capacity", "Type");
961 for (i = 0; i < n; ++i) {
964 kswap[i].ksw_devname,
966 CONVERT(kswap[i].ksw_total)
969 "%8d %8d %5.0f%% %s\n",
970 CONVERT(kswap[i].ksw_used),
971 CONVERT(kswap[i].ksw_total - kswap[i].ksw_used),
972 (double)kswap[i].ksw_used * 100.0 /
973 (double)kswap[i].ksw_total,
974 (kswap[i].ksw_flags & SW_SEQUENTIAL) ?
975 "Sequential" : "Interleaved"
981 blocksize = 1024 * 1024;
984 "%dM/%dM swap space\n",
985 CONVERT(kswap[n].ksw_used),
986 CONVERT(kswap[n].ksw_total)
990 "%-15s %*d %8d %8d %5.0f%%\n",
993 CONVERT(kswap[n].ksw_total),
994 CONVERT(kswap[n].ksw_used),
995 CONVERT(kswap[n].ksw_total - kswap[n].ksw_used),
996 (double)kswap[n].ksw_used * 100.0 /
997 (double)kswap[n].ksw_total