2 * Copyright (c) 2007 Robert N. M. Watson
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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/types.h>
30 #include <sys/sysctl.h>
41 static int aflag, bflag, cflag, fflag, kflag, sflag, tflag, vflag;
48 fprintf(stderr, "usage: procstat [-h] [-w interval] [-b | -c | -f | "
49 "-k | -s | -t | -v]\n");
50 fprintf(stderr, " [-a | pid ...]\n");
55 procstat(pid_t pid, struct kinfo_proc *kipp)
59 procstat_bin(pid, kipp);
61 procstat_args(pid, kipp);
63 procstat_files(pid, kipp);
65 procstat_kstack(pid, kipp, kflag);
67 procstat_cred(pid, kipp);
69 procstat_threads(pid, kipp);
71 procstat_vm(pid, kipp);
73 procstat_basic(pid, kipp);
77 * Sort processes first by pid and then tid.
80 kinfo_proc_compare(const void *a, const void *b)
84 i = ((const struct kinfo_proc *)a)->ki_pid -
85 ((const struct kinfo_proc *)b)->ki_pid;
88 i = ((const struct kinfo_proc *)a)->ki_tid -
89 ((const struct kinfo_proc *)b)->ki_tid;
94 kinfo_proc_sort(struct kinfo_proc *kipp, int count)
97 qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare);
101 main(int argc, char *argv[])
103 int ch, interval, name[4], tmp;
105 struct kinfo_proc *kipp;
112 while ((ch = getopt(argc, argv, "abcfkhstvw:")) != -1) {
151 l = strtol(optarg, &dummy, 10);
154 if (l < 1 || l > INT_MAX)
168 /* We require that either 0 or 1 mode flags be set. */
169 tmp = bflag + cflag + fflag + (kflag ? 1 : 0) + sflag + tflag + vflag;
170 if (!(tmp == 0 || tmp == 1))
173 /* We allow -k to be specified up to twice, but not more. */
177 /* Must specify either the -a flag or a list of pids. */
178 if (!(aflag == 1 && argc == 0) && !(aflag == 0 && argc > 0))
185 name[2] = KERN_PROC_PROC;
188 if (sysctl(name, 3, NULL, &len, NULL, 0) < 0)
189 err(-1, "sysctl: kern.proc.all");
195 if (sysctl(name, 3, kipp, &len, NULL, 0) < 0) {
197 err(-1, "sysctl: kern.proc.all");
199 if (len % sizeof(*kipp) != 0)
200 err(-1, "kinfo_proc mismatch");
201 if (kipp->ki_structsize != sizeof(*kipp))
202 err(-1, "kinfo_proc structure mismatch");
203 kinfo_proc_sort(kipp, len / sizeof(*kipp));
204 for (i = 0; i < len / sizeof(*kipp); i++) {
205 procstat(kipp[i].ki_pid, &kipp[i]);
207 /* Suppress header after first process. */
212 for (i = 0; i < (unsigned int)argc; i++) {
213 l = strtol(argv[i], &dummy, 10);
222 name[2] = KERN_PROC_PID;
226 if (sysctl(name, 4, NULL, &len, NULL, 0) < 0)
227 err(-1, "sysctl: kern.proc.pid: %d", pid);
233 if (sysctl(name, 4, kipp, &len, NULL, 0) < 0) {
235 err(-1, "sysctl: kern.proc.pid: %d", pid);
237 if (len != sizeof(*kipp))
238 err(-1, "kinfo_proc mismatch");
239 if (kipp->ki_structsize != sizeof(*kipp))
240 errx(-1, "kinfo_proc structure mismatch");
241 if (kipp->ki_pid != pid)
242 errx(-1, "kinfo_proc pid mismatch");
246 /* Suppress header after first process. */