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, lflag, sflag;
43 static int tflag, 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 "-l | -s | -t | -v | -x] [-a | pid | core ...]\n");
58 procstat(struct procstat *prstat, struct kinfo_proc *kipp)
62 procstat_bin(prstat, kipp);
64 procstat_args(prstat, kipp);
66 procstat_env(prstat, kipp);
68 procstat_files(prstat, kipp);
70 procstat_sigs(prstat, kipp);
72 procstat_threads_sigs(prstat, kipp);
74 procstat_kstack(prstat, kipp, kflag);
76 procstat_rlimit(prstat, kipp);
78 procstat_cred(prstat, kipp);
80 procstat_threads(prstat, kipp);
82 procstat_vm(prstat, kipp);
84 procstat_auxv(prstat, kipp);
90 * Sort processes first by pid and then tid.
93 kinfo_proc_compare(const void *a, const void *b)
97 i = ((const struct kinfo_proc *)a)->ki_pid -
98 ((const struct kinfo_proc *)b)->ki_pid;
101 i = ((const struct kinfo_proc *)a)->ki_tid -
102 ((const struct kinfo_proc *)b)->ki_tid;
107 kinfo_proc_sort(struct kinfo_proc *kipp, int count)
110 qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare);
114 main(int argc, char *argv[])
116 int ch, interval, tmp;
118 struct kinfo_proc *p;
119 struct procstat *prstat, *cprstat;
127 memf = nlistf = NULL;
128 while ((ch = getopt(argc, argv, "CN:M:abcefijklhstvw:x")) != -1) {
197 l = strtol(optarg, &dummy, 10);
200 if (l < 1 || l > INT_MAX)
218 /* We require that either 0 or 1 mode flags be set. */
219 tmp = bflag + cflag + eflag + fflag + iflag + jflag + (kflag ? 1 : 0) +
220 lflag + sflag + tflag + vflag + xflag;
221 if (!(tmp == 0 || tmp == 1))
224 /* We allow -k to be specified up to twice, but not more. */
228 /* Must specify either the -a flag or a list of pids. */
229 if (!(aflag == 1 && argc == 0) && !(aflag == 0 && argc > 0))
232 /* Only allow -C with -f. */
237 prstat = procstat_open_kvm(nlistf, memf);
239 prstat = procstat_open_sysctl();
241 errx(1, "procstat_open()");
244 p = procstat_getprocs(prstat, KERN_PROC_PROC, 0, &cnt);
246 errx(1, "procstat_getprocs()");
247 kinfo_proc_sort(p, cnt);
248 for (i = 0; i < cnt; i++) {
249 procstat(prstat, &p[i]);
251 /* Suppress header after first process. */
254 procstat_freeprocs(prstat, p);
256 for (i = 0; i < argc; i++) {
257 l = strtol(argv[i], &dummy, 10);
258 if (*dummy == '\0') {
263 p = procstat_getprocs(prstat, KERN_PROC_PID, pid, &cnt);
265 errx(1, "procstat_getprocs()");
268 procstat_freeprocs(prstat, p);
270 cprstat = procstat_open_core(argv[i]);
271 if (cprstat == NULL) {
272 warnx("procstat_open()");
275 p = procstat_getprocs(cprstat, KERN_PROC_PID,
278 errx(1, "procstat_getprocs()");
280 procstat(cprstat, p);
281 procstat_freeprocs(cprstat, p);
282 procstat_close(cprstat);
284 /* Suppress header after first process. */
290 procstat_close(prstat);