2 * Copyright (c) 2007, 2011 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/param.h>
30 #include <sys/sysctl.h>
34 #include <libprocstat.h>
42 static int aflag, bflag, cflag, eflag, fflag, iflag, jflag, kflag, sflag, tflag;
43 static int vflag, xflag;
44 int hflag, nflag, Cflag;
50 fprintf(stderr, "usage: procstat [-h] [-C] [-M core] [-N system] "
52 fprintf(stderr, " [-b | -c | -e | -f | -i | -j | -k | "
53 "-s | -t | -v | -x] [-a | pid ...]\n");
58 procstat(struct procstat *prstat, struct kinfo_proc *kipp)
68 procstat_files(prstat, kipp);
70 procstat_sigs(prstat, kipp);
72 procstat_threads_sigs(prstat, kipp);
74 procstat_kstack(kipp, kflag);
78 procstat_threads(kipp);
88 * Sort processes first by pid and then tid.
91 kinfo_proc_compare(const void *a, const void *b)
95 i = ((const struct kinfo_proc *)a)->ki_pid -
96 ((const struct kinfo_proc *)b)->ki_pid;
99 i = ((const struct kinfo_proc *)a)->ki_tid -
100 ((const struct kinfo_proc *)b)->ki_tid;
105 kinfo_proc_sort(struct kinfo_proc *kipp, int count)
108 qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare);
112 main(int argc, char *argv[])
114 int ch, interval, tmp;
116 struct kinfo_proc *p;
117 struct procstat *prstat;
125 memf = nlistf = NULL;
126 while ((ch = getopt(argc, argv, "CN:M:abcefijkhstvw:x")) != -1) {
191 l = strtol(optarg, &dummy, 10);
194 if (l < 1 || l > INT_MAX)
212 /* We require that either 0 or 1 mode flags be set. */
213 tmp = bflag + cflag + eflag + fflag + (kflag ? 1 : 0) + sflag + tflag +
215 if (!(tmp == 0 || tmp == 1))
218 /* We allow -k to be specified up to twice, but not more. */
222 /* Must specify either the -a flag or a list of pids. */
223 if (!(aflag == 1 && argc == 0) && !(aflag == 0 && argc > 0))
226 /* Only allow -C with -f. */
231 prstat = procstat_open_kvm(nlistf, memf);
233 prstat = procstat_open_sysctl();
235 errx(1, "procstat_open()");
238 p = procstat_getprocs(prstat, KERN_PROC_PROC, 0, &cnt);
240 errx(1, "procstat_getprocs()");
241 kinfo_proc_sort(p, cnt);
242 for (i = 0; i < cnt; i++) {
243 procstat(prstat, &p[i]);
245 /* Suppress header after first process. */
248 procstat_freeprocs(prstat, p);
250 for (i = 0; i < argc; i++) {
251 l = strtol(argv[i], &dummy, 10);
258 p = procstat_getprocs(prstat, KERN_PROC_PID, pid, &cnt);
260 errx(1, "procstat_getprocs()");
263 procstat_freeprocs(prstat, p);
265 /* Suppress header after first process. */
271 procstat_close(prstat);