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.14 2005/02/03 17:28:40 joerg Exp $
39 #define _KERNEL_STRUCTURES
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/kinfo.h>
65 #include <sys/sysctl.h>
78 # define KCORE_KINFO_WRAPPER
95 const size_t NL_LAST_MANDATORY = NL_NUMVNODES;
98 { "_mountlist", 0, 0, 0, 0 }, /* address of head of mount list. */
99 { "_numvnodes", 0, 0, 0, 0 },
100 { "_rc_tty", 0, 0, 0, 0 },
101 { "_nrc_tty", 0, 0, 0, 0 },
102 { "_cy_tty", 0, 0, 0, 0 },
103 { "_ncy_tty", 0, 0, 0, 0 },
104 { "_si__tty", 0, 0, 0, 0 },
105 { "_si_Nports", 0, 0, 0, 0 },
116 const char *usagestr;
122 { MNT_RDONLY, "rdonly" },
123 { MNT_SYNCHRONOUS, "sync" },
124 { MNT_NOEXEC, "noexec" },
125 { MNT_NOSUID, "nosuid" },
126 { MNT_NODEV, "nodev" },
127 { MNT_UNION, "union" },
128 { MNT_ASYNC, "async" },
129 { MNT_SUIDDIR, "suiddir" },
130 { MNT_SOFTDEP, "softdep" },
131 { MNT_NOSYMFOLLOW, "nosymfollow" },
132 { MNT_NOATIME, "noatime" },
133 { MNT_NOCLUSTERR, "noclusterread" },
134 { MNT_NOCLUSTERW, "noclusterwrite" },
135 { MNT_EXRDONLY, "exrdonly" },
136 { MNT_EXPORTED, "exported" },
137 { MNT_DEFEXPORTED, "defexported" },
138 { MNT_EXPORTANON, "exportanon" },
139 { MNT_EXKERB, "exkerb" },
140 { MNT_EXPUBLIC, "public" },
141 { MNT_LOCAL, "local" },
142 { MNT_QUOTA, "quota" },
143 { MNT_ROOTFS, "rootfs" },
144 { MNT_USER, "user" },
145 { MNT_IGNORE, "ignore" },
146 { MNT_UPDATE, "update" },
147 { MNT_DELEXPORT, "delexport" },
148 { MNT_RELOAD, "reload" },
149 { MNT_FORCE, "force" },
154 #define SVAR(var) __STRING(var) /* to force expansion */
155 #define KGET(idx, var) \
156 KGET1(idx, &var, sizeof(var), SVAR(var))
157 #define KGET1(idx, p, s, msg) \
158 KGET2(nl[idx].n_value, p, s, msg)
159 #define KGET2(addr, p, s, msg) \
160 if (kvm_read(kd, (u_long)(addr), p, s) != s) \
161 warnx("cannot read %s: %s", msg, kvm_geterr(kd))
162 #define KGETN(idx, var) \
163 KGET1N(idx, &var, sizeof(var), SVAR(var))
164 #define KGET1N(idx, p, s, msg) \
165 KGET2N(nl[idx].n_value, p, s, msg)
166 #define KGET2N(addr, p, s, msg) \
167 ((kvm_read(kd, (u_long)(addr), p, s) == s) ? 1 : 0)
168 #define KGETRET(addr, p, s, msg) \
169 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \
170 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \
175 int getfiles(char **, int *);
177 getmnt(struct mount *);
182 void mount_print(struct mount *);
183 void nfs_header(void);
184 int nfs_print(struct vnode *);
187 void ttyprt(struct tty *, int);
188 void ttytype(struct tty *, const char *, int, int, int);
189 void ufs_header(void);
190 int ufs_print(struct vnode *);
191 void union_header(void);
192 int union_print(struct vnode *);
193 static void usage(void);
194 void vnode_header(void);
195 void vnode_print(struct vnode *, struct vnode *);
196 void vnodemode(void);
199 main(int argc, char **argv)
202 int fileflag, ttyflag, vnodeflag;
203 char buf[_POSIX2_LINE_MAX];
206 fileflag = swapflag = ttyflag = vnodeflag = 0;
208 /* We will behave like good old swapinfo if thus invoked */
209 opts = strrchr(argv[0],'/');
214 if (!strcmp(opts,"swapinfo")) {
217 usagestr = "swapinfo [-k] [-M core] [-N system]";
219 opts = "TM:N:fiknstv";
220 usagestr = "pstat [-Tfknst] [-M core] [-N system]";
223 while ((ch = getopt(argc, argv, opts)) != -1)
229 putenv("BLOCKSIZE=1K");
250 case 'i': /* Backward compatibility. */
251 errx(1, "vnode mode not supported");
263 * Discard setgid privileges if not the running kernel so that bad
264 * guys can't print interesting stuff from kernel memory.
266 if (nlistf != NULL || memf != NULL)
269 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0)
270 errx(1, "kvm_openfiles: %s", buf);
272 if (kcore_wrapper_open(nlistf, memf, buf))
273 errx(1, "kcore_open: %s", buf);
275 if ((ret = kvm_nlist(kd, nl)) != 0) {
280 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
281 for (i = 0; i < NL_LAST_MANDATORY; i++) {
282 if (!nl[i].n_value) {
284 warnx("undefined symbol: %s", nl[i].n_name);
290 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
292 if (fileflag || totalflag)
298 if (swapflag || totalflag)
306 fprintf(stderr, "usage: %s\n", usagestr);
311 struct vnode *avnode;
318 struct e_vnode *e_vnodebase, *endvnode, *evp;
320 struct mount *maddr, *mp = NULL;
323 e_vnodebase = loadvnodes(&numvnodes);
325 printf("%7d vnodes\n", numvnodes);
328 endvnode = e_vnodebase + numvnodes;
329 printf("%d active vnodes\n", numvnodes);
332 #define ST mp->mnt_stat
334 for (evp = e_vnodebase; evp < endvnode; evp++) {
336 if (vp->v_mount != maddr) {
340 if ((mp = getmnt(vp->v_mount)) == NULL)
345 if (!strcmp(ST.f_fstypename, "ufs") ||
346 !strcmp(ST.f_fstypename, "mfs"))
348 else if (!strcmp(ST.f_fstypename, "nfs"))
350 else if (!strcmp(ST.f_fstypename, "union"))
354 vnode_print(evp->avnode, vp);
355 if (!strcmp(ST.f_fstypename, "ufs") ||
356 !strcmp(ST.f_fstypename, "mfs"))
358 else if (!strcmp(ST.f_fstypename, "nfs"))
360 else if (!strcmp(ST.f_fstypename, "union"))
370 printf("ADDR TYP VFLAG USE HOLD");
374 vnode_print(struct vnode *avnode, struct vnode *vp)
384 switch (vp->v_type) {
440 if (flag & VONWORKLST)
445 if (flag & VINACTIVE)
447 if (flag & VRECLAIMED)
454 printf("%8lx %s %5s %4d %4d",
455 (u_long)(void *)avnode, type, flags, vp->v_usecount, vp->v_holdcnt);
461 printf(" FILEID IFLAG RDEV|SZ");
465 ufs_print(struct vnode *vp)
468 struct inode inode, *ip = &inode;
469 char flagbuf[16], *flags = flagbuf;
473 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
475 if (flag & IN_ACCESS)
477 if (flag & IN_CHANGE)
479 if (flag & IN_UPDATE)
481 if (flag & IN_MODIFIED)
483 if (flag & IN_RENAME)
485 if (flag & IN_SHLOCK)
487 if (flag & IN_EXLOCK)
489 if (flag & IN_HASHED)
491 if (flag & IN_LAZYMOD)
497 printf(" %6d %5s", ip->i_number, flagbuf);
498 type = ip->i_mode & S_IFMT;
499 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
500 if (usenumflag || ((name = devname(ip->i_rdev, type)) == NULL))
502 major(ip->i_rdev), minor(ip->i_rdev));
504 printf(" %7s", name);
506 printf(" %7qd", ip->i_size);
513 printf(" FILEID NFLAG RDEV|SZ");
517 nfs_print(struct vnode *vp)
519 struct nfsnode nfsnode, *np = &nfsnode;
520 char flagbuf[16], *flags = flagbuf;
525 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
527 if (flag & NFLUSHWANT)
529 if (flag & NFLUSHINPROG)
531 if (flag & NMODIFIED)
533 if (flag & NWRITEERR)
535 if (flag & NQNFSNONCACHE)
537 if (flag & NQNFSWRITE)
539 if (flag & NQNFSEVICTED)
555 #define VT np->n_vattr
556 printf(" %6ld %5s", VT.va_fileid, flagbuf);
557 type = VT.va_mode & S_IFMT;
558 if (S_ISCHR(VT.va_mode) || S_ISBLK(VT.va_mode))
559 if (usenumflag || ((name = devname(VT.va_rdev, type)) == NULL))
561 major(VT.va_rdev), minor(VT.va_rdev));
563 printf(" %7s", name);
565 printf(" %7qd", np->n_size);
572 printf(" UPPER LOWER");
576 union_print(struct vnode *vp)
578 struct union_node unode, *up = &unode;
580 KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode");
582 printf(" %8lx %8lx", (u_long)(void *)up->un_uppervp,
583 (u_long)(void *)up->un_lowervp);
588 * Given a pointer to a mount structure in kernel space,
589 * read it in and return a usable pointer to it.
592 getmnt(struct mount *maddr)
601 for (mt = mhead; mt != NULL; mt = mt->next)
602 if (maddr == mt->maddr)
604 if ((mt = malloc(sizeof(struct mtab))) == NULL)
606 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
614 mount_print(struct mount *mp)
618 #define ST mp->mnt_stat
619 printf("*** MOUNT %s %s on %s", ST.f_fstypename,
620 ST.f_mntfromname, ST.f_mntonname);
621 if ((flags = mp->mnt_flag)) {
623 const char *sep = " (";
625 for (i = 0; mnt_flags[i].m_flag; i++) {
626 if (flags & mnt_flags[i].m_flag) {
627 printf("%s%s", sep, mnt_flags[i].m_name);
628 flags &= ~mnt_flags[i].m_flag;
633 printf("%sunknown_flags:%x", sep, flags);
641 loadvnodes(int *avnodes)
645 struct e_vnode *vnodebase;
651 return (kinfo_vnodes(avnodes));
655 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1)
656 err(1, "sysctl: KERN_VNODE");
657 if ((vnodebase = malloc(copysize)) == NULL)
659 if (sysctl(mib, 2, vnodebase, ©size, NULL, 0) == -1)
660 err(1, "sysctl: KERN_VNODE");
661 if (copysize % sizeof(struct e_vnode))
662 errx(1, "vnode size mismatch");
663 *avnodes = copysize / sizeof(struct e_vnode);
669 * simulate what a running kernel does in in kinfo_vnode
672 kinfo_vnodes(int *avnodes)
674 struct mntlist mountlist;
675 struct mount *mp, mounth, *mp_next;
676 struct vnode *vp, vnode, *vp_next;
677 char *vbuf, *evbuf, *bp;
680 #define VPTRSZ sizeof(struct vnode *)
681 #define VNODESZ sizeof(struct vnode)
683 KGET(NL_NUMVNODES, numvnodes);
684 if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
687 evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ);
688 KGET(NL_MOUNTLIST, mountlist);
689 for (num = 0, mp = TAILQ_FIRST(&mountlist); ; mp = mp_next) {
690 KGET2(mp, &mounth, sizeof(mounth), "mount entry");
691 mp_next = TAILQ_NEXT(&mounth, mnt_list);
692 for (vp = TAILQ_FIRST(&mounth.mnt_nvnodelist);
693 vp != NULL; vp = vp_next) {
694 KGET2(vp, &vnode, sizeof(vnode), "vnode");
695 vp_next = TAILQ_NEXT(&vnode, v_nmntvnodes);
696 if ((bp + VPTRSZ + VNODESZ) > evbuf)
697 /* XXX - should realloc */
698 errx(1, "no more room for vnodes");
699 memmove(bp, &vp, VPTRSZ);
701 memmove(bp, &vnode, VNODESZ);
705 if (mp == TAILQ_LAST(&mountlist, mntlist))
709 return ((struct e_vnode *)vbuf);
713 " LINE RAW CAN OUT IHIWT ILOWT OHWT LWT COL STATE SESS PGID DISC\n";
720 struct tty ttyb[1000];
726 error = sysctlbyname("kern.ttys", &ttyb, &len, 0, 0);
728 len /= sizeof(ttyb[0]);
729 for (i = 0; i < len; i++) {
733 if ((tty = malloc(ttyspace * sizeof(*tty))) == NULL)
735 if (nl[NL_NRC_TTY].n_type != 0)
736 ttytype(tty, "rc", NL_RC_TTY, NL_NRC_TTY, 0);
737 if (nl[NL_NCY_TTY].n_type != 0)
738 ttytype(tty, "cy", NL_CY_TTY, NL_NCY_TTY, 0);
739 if (nl[NL_SI_NPORTS].n_type != 0)
740 ttytype(tty, "si", NL_SI_TTY, NL_SI_NPORTS, 1);
745 ttytype(struct tty *tty, const char *name, int type, int number, int indir)
749 struct tty **ttyaddr;
754 printf("%d %s %s\n", ntty, name, (ntty == 1) ? "line" : "lines");
755 if (ntty > ttyspace) {
757 if ((tty = realloc(tty, ttyspace * sizeof(*tty))) == 0)
762 KGET2(ttyaddr, tty, (ssize_t)(ntty * sizeof(struct tty)),
765 KGET1(type, tty, (ssize_t)(ntty * sizeof(struct tty)),
769 for (tp = tty; tp < &tty[ntty]; tp++)
770 ttyprt(tp, tp - tty);
783 { TS_CONNECTED, 'c'},
792 { TS_SO_OLOWAT, 'A'},
794 #ifdef TS_SO_OCOMPLETE
795 { TS_SO_OCOMPLETE, 'a'},
800 { TS_CAR_OFLOW, 'm'},
803 { TS_CTS_OFLOW, 'o'},
806 { TS_DSR_OFLOW, 'd'},
815 #ifdef TS_CAN_BYPASS_L_RINT
816 { TS_CAN_BYPASS_L_RINT, 'l'},
828 ttyprt(struct tty *tp, int line)
832 char *name, state[20];
834 if (usenumflag || tp->t_dev == 0 ||
835 (name = devname(tp->t_dev, S_IFCHR)) == NULL)
836 printf("%7d ", line);
838 printf("%7s ", name);
839 printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc);
840 printf("%3d %5d %5d %4d %3d %7d ", tp->t_outq.c_cc,
841 tp->t_ihiwat, tp->t_ilowat, tp->t_ohiwat, tp->t_olowat,
843 for (i = j = 0; ttystates[i].flag; i++)
844 if (tp->t_state&ttystates[i].flag)
845 state[j++] = ttystates[i].val;
849 printf("%-6s %8lx", state, (u_long)(void *)tp->t_session);
851 if (tp->t_pgrp != NULL)
852 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
853 printf("%6d ", pgid);
854 switch (tp->t_line) {
868 printf("%d\n", tp->t_line);
876 struct kinfo_file *fp, *ofp;
878 char flagbuf[16], *fbp;
880 static const char *dtypes[] = { "???", "inode", "socket" };
882 if (kinfo_get_maxfiles(&maxfile))
883 err(1, "kinfo_get_maxfiles");
885 if (kinfo_get_openfiles(&nfile))
886 err(1, "kinfo_get_openfiles");
887 printf("%3d/%3d files\n", nfile, maxfile);
890 if (kinfo_get_files(&fp, &len))
891 err(1, "kinfo_get_files");
894 printf("%d/%d open files\n", len, maxfile);
895 printf(" LOC TYPE FLG CNT MSG DATA OFFSET\n");
896 for (; len-- > 0; fp++) {
897 if ((unsigned)fp->f_type > DTYPE_SOCKET)
899 printf("%p ", fp->f_file);
900 printf("%-8.8s", dtypes[fp->f_type]);
902 if (fp->f_flag & FREAD)
904 if (fp->f_flag & FWRITE)
906 if (fp->f_flag & FAPPEND)
908 #ifdef FSHLOCK /* currently gone */
909 if (fp->f_flag & FSHLOCK)
911 if (fp->f_flag & FEXLOCK)
914 if (fp->f_flag & FASYNC)
917 printf("%6s %3d", flagbuf, fp->f_count);
918 printf(" %3d", fp->f_msgcount);
919 printf(" %8lx", (u_long)(void *)fp->f_data);
920 if (fp->f_offset < 0)
921 printf(" %qx\n", fp->f_offset);
923 printf(" %qd\n", fp->f_offset);
929 * swapmode is based on a program called swapinfo written
930 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
935 struct kvm_swap kswap[16];
938 int pagesize = getpagesize();
946 sizeof(kswap)/sizeof(kswap[0]),
947 ((swapflag > 1) ? SWIF_DUMP_TREE : 0) | SWIF_DEV_PREFIX
950 #define CONVERT(v) ((int)((quad_t)(v) * pagesize / blocksize))
952 header = getbsize(&hlen, &blocksize);
953 if (totalflag == 0) {
954 printf("%-15s %*s %8s %8s %8s %s\n",
955 "Device", hlen, header,
956 "Used", "Avail", "Capacity", "Type");
958 for (i = 0; i < n; ++i) {
961 kswap[i].ksw_devname,
963 CONVERT(kswap[i].ksw_total)
966 "%8d %8d %5.0f%% %s\n",
967 CONVERT(kswap[i].ksw_used),
968 CONVERT(kswap[i].ksw_total - kswap[i].ksw_used),
969 (double)kswap[i].ksw_used * 100.0 /
970 (double)kswap[i].ksw_total,
971 (kswap[i].ksw_flags & SW_SEQUENTIAL) ?
972 "Sequential" : "Interleaved"
978 blocksize = 1024 * 1024;
981 "%dM/%dM swap space\n",
982 CONVERT(kswap[n].ksw_used),
983 CONVERT(kswap[n].ksw_total)
987 "%-15s %*d %8d %8d %5.0f%%\n",
990 CONVERT(kswap[n].ksw_total),
991 CONVERT(kswap[n].ksw_used),
992 CONVERT(kswap[n].ksw_total - kswap[n].ksw_used),
993 (double)kswap[n].ksw_used * 100.0 /
994 (double)kswap[n].ksw_total