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>
43 static int aflag, bflag, cflag, eflag, fflag, iflag, jflag, kflag, lflag, rflag;
44 static int sflag, tflag, vflag, xflag, Sflag;
45 int hflag, nflag, Cflag, Hflag;
51 fprintf(stderr, "usage: procstat [-CHhn] [-M core] [-N system] "
53 fprintf(stderr, " [-b | -c | -e | -f | -i | -j | -k | "
54 "-l | -r | -s | -S | -t | -v | -x]\n");
55 fprintf(stderr, " [-a | pid | core ...]\n");
60 procstat(struct procstat *prstat, struct kinfo_proc *kipp)
64 procstat_bin(prstat, kipp);
66 procstat_args(prstat, kipp);
68 procstat_env(prstat, kipp);
70 procstat_files(prstat, kipp);
72 procstat_sigs(prstat, kipp);
74 procstat_threads_sigs(prstat, kipp);
76 procstat_kstack(prstat, kipp, kflag);
78 procstat_rlimit(prstat, kipp);
80 procstat_rusage(prstat, kipp);
82 procstat_cred(prstat, kipp);
84 procstat_threads(prstat, kipp);
86 procstat_vm(prstat, kipp);
88 procstat_auxv(prstat, kipp);
90 procstat_cs(prstat, kipp);
96 * Sort processes first by pid and then tid.
99 kinfo_proc_compare(const void *a, const void *b)
103 i = ((const struct kinfo_proc *)a)->ki_pid -
104 ((const struct kinfo_proc *)b)->ki_pid;
107 i = ((const struct kinfo_proc *)a)->ki_tid -
108 ((const struct kinfo_proc *)b)->ki_tid;
113 kinfo_proc_sort(struct kinfo_proc *kipp, int count)
116 qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare);
120 kinfo_proc_thread_name(const struct kinfo_proc *kipp)
122 static char name[MAXCOMLEN+1];
124 strlcpy(name, kipp->ki_tdname, sizeof(name));
125 strlcat(name, kipp->ki_moretdname, sizeof(name));
126 if (name[0] == '\0' || strcmp(kipp->ki_comm, name) == 0) {
135 main(int argc, char *argv[])
137 int ch, interval, tmp;
139 struct kinfo_proc *p;
140 struct procstat *prstat, *cprstat;
148 memf = nlistf = NULL;
149 while ((ch = getopt(argc, argv, "CHN:M:abcefijklhrsStvw:x")) != -1) {
229 l = strtol(optarg, &dummy, 10);
232 if (l < 1 || l > INT_MAX)
250 /* We require that either 0 or 1 mode flags be set. */
251 tmp = bflag + cflag + eflag + fflag + iflag + jflag + (kflag ? 1 : 0) +
252 lflag + rflag + sflag + tflag + vflag + xflag + Sflag;
253 if (!(tmp == 0 || tmp == 1))
256 /* We allow -k to be specified up to twice, but not more. */
260 /* Must specify either the -a flag or a list of pids. */
261 if (!(aflag == 1 && argc == 0) && !(aflag == 0 && argc > 0))
264 /* Only allow -C with -f. */
269 prstat = procstat_open_kvm(nlistf, memf);
271 prstat = procstat_open_sysctl();
273 errx(1, "procstat_open()");
276 p = procstat_getprocs(prstat, KERN_PROC_PROC, 0, &cnt);
278 errx(1, "procstat_getprocs()");
279 kinfo_proc_sort(p, cnt);
280 for (i = 0; i < cnt; i++) {
281 procstat(prstat, &p[i]);
283 /* Suppress header after first process. */
286 procstat_freeprocs(prstat, p);
288 for (i = 0; i < argc; i++) {
289 l = strtol(argv[i], &dummy, 10);
290 if (*dummy == '\0') {
295 p = procstat_getprocs(prstat, KERN_PROC_PID, pid, &cnt);
297 errx(1, "procstat_getprocs()");
300 procstat_freeprocs(prstat, p);
302 cprstat = procstat_open_core(argv[i]);
303 if (cprstat == NULL) {
304 warnx("procstat_open()");
307 p = procstat_getprocs(cprstat, KERN_PROC_PID,
310 errx(1, "procstat_getprocs()");
312 procstat(cprstat, p);
313 procstat_freeprocs(cprstat, p);
314 procstat_close(cprstat);
316 /* Suppress header after first process. */
322 procstat_close(prstat);