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/param.h>
30 #include <sys/sysctl.h>
34 #include <libprocstat.h>
42 static int aflag, bflag, cflag, fflag, iflag, jflag, kflag, sflag, tflag, vflag;
49 fprintf(stderr, "usage: procstat [-h] [-M core] [-N system] "
50 "[-w interval] [-b | -c | -f | -i | -j | -k | -s | -t | -v]\n");
51 fprintf(stderr, " [-a | pid ...]\n");
56 procstat(struct procstat *prstat, struct kinfo_proc *kipp)
64 procstat_files(prstat, kipp);
66 procstat_sigs(prstat, kipp);
68 procstat_threads_sigs(prstat, kipp);
70 procstat_kstack(kipp, kflag);
74 procstat_threads(kipp);
82 * Sort processes first by pid and then tid.
85 kinfo_proc_compare(const void *a, const void *b)
89 i = ((const struct kinfo_proc *)a)->ki_pid -
90 ((const struct kinfo_proc *)b)->ki_pid;
93 i = ((const struct kinfo_proc *)a)->ki_tid -
94 ((const struct kinfo_proc *)b)->ki_tid;
99 kinfo_proc_sort(struct kinfo_proc *kipp, int count)
102 qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare);
106 main(int argc, char *argv[])
108 int ch, interval, tmp;
110 struct kinfo_proc *p;
111 struct procstat *prstat;
119 memf = nlistf = NULL;
120 while ((ch = getopt(argc, argv, "N:M:abcfijkhstvw:")) != -1) {
177 l = strtol(optarg, &dummy, 10);
180 if (l < 1 || l > INT_MAX)
194 /* We require that either 0 or 1 mode flags be set. */
195 tmp = bflag + cflag + fflag + (kflag ? 1 : 0) + sflag + tflag + vflag;
196 if (!(tmp == 0 || tmp == 1))
199 /* We allow -k to be specified up to twice, but not more. */
203 /* Must specify either the -a flag or a list of pids. */
204 if (!(aflag == 1 && argc == 0) && !(aflag == 0 && argc > 0))
208 prstat = procstat_open_kvm(nlistf, memf);
210 prstat = procstat_open_sysctl();
212 errx(1, "procstat_open()");
215 p = procstat_getprocs(prstat, KERN_PROC_PROC, 0, &cnt);
217 errx(1, "procstat_getprocs()");
218 kinfo_proc_sort(p, cnt);
219 for (i = 0; i < cnt; i++) {
220 procstat(prstat, &p[i]);
222 /* Suppress header after first process. */
225 procstat_freeprocs(prstat, p);
227 for (i = 0; i < argc; i++) {
228 l = strtol(argv[i], &dummy, 10);
235 p = procstat_getprocs(prstat, KERN_PROC_PID, pid, &cnt);
237 errx(1, "procstat_getprocs()");
240 procstat_freeprocs(prstat, p);
242 /* Suppress header after first process. */
248 procstat_close(prstat);