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;
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 | -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);
93 * Sort processes first by pid and then tid.
96 kinfo_proc_compare(const void *a, const void *b)
100 i = ((const struct kinfo_proc *)a)->ki_pid -
101 ((const struct kinfo_proc *)b)->ki_pid;
104 i = ((const struct kinfo_proc *)a)->ki_tid -
105 ((const struct kinfo_proc *)b)->ki_tid;
110 kinfo_proc_sort(struct kinfo_proc *kipp, int count)
113 qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare);
117 main(int argc, char *argv[])
119 int ch, interval, tmp;
121 struct kinfo_proc *p;
122 struct procstat *prstat, *cprstat;
130 memf = nlistf = NULL;
131 while ((ch = getopt(argc, argv, "CHN:M:abcefijklhrstvw:x")) != -1) {
208 l = strtol(optarg, &dummy, 10);
211 if (l < 1 || l > INT_MAX)
229 /* We require that either 0 or 1 mode flags be set. */
230 tmp = bflag + cflag + eflag + fflag + iflag + jflag + (kflag ? 1 : 0) +
231 lflag + rflag + sflag + tflag + vflag + xflag;
232 if (!(tmp == 0 || tmp == 1))
235 /* We allow -k to be specified up to twice, but not more. */
239 /* Must specify either the -a flag or a list of pids. */
240 if (!(aflag == 1 && argc == 0) && !(aflag == 0 && argc > 0))
243 /* Only allow -C with -f. */
248 prstat = procstat_open_kvm(nlistf, memf);
250 prstat = procstat_open_sysctl();
252 errx(1, "procstat_open()");
255 p = procstat_getprocs(prstat, KERN_PROC_PROC, 0, &cnt);
257 errx(1, "procstat_getprocs()");
258 kinfo_proc_sort(p, cnt);
259 for (i = 0; i < cnt; i++) {
260 procstat(prstat, &p[i]);
262 /* Suppress header after first process. */
265 procstat_freeprocs(prstat, p);
267 for (i = 0; i < argc; i++) {
268 l = strtol(argv[i], &dummy, 10);
269 if (*dummy == '\0') {
274 p = procstat_getprocs(prstat, KERN_PROC_PID, pid, &cnt);
276 errx(1, "procstat_getprocs()");
279 procstat_freeprocs(prstat, p);
281 cprstat = procstat_open_core(argv[i]);
282 if (cprstat == NULL) {
283 warnx("procstat_open()");
286 p = procstat_getprocs(cprstat, KERN_PROC_PID,
289 errx(1, "procstat_getprocs()");
291 procstat(cprstat, p);
292 procstat_freeprocs(cprstat, p);
293 procstat_close(cprstat);
295 /* Suppress header after first process. */
301 procstat_close(prstat);