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, rflag;
43 static int sflag, tflag, vflag, xflag, Sflag;
44 int hflag, nflag, Cflag, Hflag;
50 fprintf(stderr, "usage: procstat [-CHhn] [-M core] [-N system] "
52 fprintf(stderr, " [-b | -c | -e | -f | -i | -j | -k | "
53 "-l | -r | -s | -S | -t | -v | -x]\n");
54 fprintf(stderr, " [-a | pid | core ...]\n");
59 procstat(struct procstat *prstat, struct kinfo_proc *kipp)
63 procstat_bin(prstat, kipp);
65 procstat_args(prstat, kipp);
67 procstat_env(prstat, kipp);
69 procstat_files(prstat, kipp);
71 procstat_sigs(prstat, kipp);
73 procstat_threads_sigs(prstat, kipp);
75 procstat_kstack(prstat, kipp, kflag);
77 procstat_rlimit(prstat, kipp);
79 procstat_rusage(prstat, kipp);
81 procstat_cred(prstat, kipp);
83 procstat_threads(prstat, kipp);
85 procstat_vm(prstat, kipp);
87 procstat_auxv(prstat, kipp);
89 procstat_cs(prstat, kipp);
95 * Sort processes first by pid and then tid.
98 kinfo_proc_compare(const void *a, const void *b)
102 i = ((const struct kinfo_proc *)a)->ki_pid -
103 ((const struct kinfo_proc *)b)->ki_pid;
106 i = ((const struct kinfo_proc *)a)->ki_tid -
107 ((const struct kinfo_proc *)b)->ki_tid;
112 kinfo_proc_sort(struct kinfo_proc *kipp, int count)
115 qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare);
119 main(int argc, char *argv[])
121 int ch, interval, tmp;
123 struct kinfo_proc *p;
124 struct procstat *prstat, *cprstat;
132 memf = nlistf = NULL;
133 while ((ch = getopt(argc, argv, "CHN:M:abcefijklhrsStvw:x")) != -1) {
213 l = strtol(optarg, &dummy, 10);
216 if (l < 1 || l > INT_MAX)
234 /* We require that either 0 or 1 mode flags be set. */
235 tmp = bflag + cflag + eflag + fflag + iflag + jflag + (kflag ? 1 : 0) +
236 lflag + rflag + sflag + tflag + vflag + xflag + Sflag;
237 if (!(tmp == 0 || tmp == 1))
240 /* We allow -k to be specified up to twice, but not more. */
244 /* Must specify either the -a flag or a list of pids. */
245 if (!(aflag == 1 && argc == 0) && !(aflag == 0 && argc > 0))
248 /* Only allow -C with -f. */
253 prstat = procstat_open_kvm(nlistf, memf);
255 prstat = procstat_open_sysctl();
257 errx(1, "procstat_open()");
260 p = procstat_getprocs(prstat, KERN_PROC_PROC, 0, &cnt);
262 errx(1, "procstat_getprocs()");
263 kinfo_proc_sort(p, cnt);
264 for (i = 0; i < cnt; i++) {
265 procstat(prstat, &p[i]);
267 /* Suppress header after first process. */
270 procstat_freeprocs(prstat, p);
272 for (i = 0; i < argc; i++) {
273 l = strtol(argv[i], &dummy, 10);
274 if (*dummy == '\0') {
279 p = procstat_getprocs(prstat, KERN_PROC_PID, pid, &cnt);
281 errx(1, "procstat_getprocs()");
284 procstat_freeprocs(prstat, p);
286 cprstat = procstat_open_core(argv[i]);
287 if (cprstat == NULL) {
288 warnx("procstat_open()");
291 p = procstat_getprocs(cprstat, KERN_PROC_PID,
294 errx(1, "procstat_getprocs()");
296 procstat(cprstat, p);
297 procstat_freeprocs(cprstat, p);
298 procstat_close(cprstat);
300 /* Suppress header after first process. */
306 procstat_close(prstat);