2 * Copyright (c) 2007, 2011 Robert N. M. Watson
3 * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/param.h>
31 #include <sys/sysctl.h>
35 #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 xo_error("usage: procstat [-CHhn] [-M core] [-N system] "
53 " [-b | -c | -e | -f | -i | -j | -k | "
54 "-l | -r | -s | -S | -t | -v | -x]\n"
55 " [-a | pid | core ...]\n");
61 procstat(struct procstat *prstat, struct kinfo_proc *kipp)
65 asprintf(&pidstr, "%d", kipp->ki_pid);
67 xo_errc(1, ENOMEM, "Failed to allocate memory in procstat()");
68 xo_open_container(pidstr);
71 procstat_bin(prstat, kipp);
73 procstat_args(prstat, kipp);
75 procstat_env(prstat, kipp);
77 procstat_files(prstat, kipp);
79 procstat_sigs(prstat, kipp);
81 procstat_threads_sigs(prstat, kipp);
83 procstat_kstack(prstat, kipp, kflag);
85 procstat_rlimit(prstat, kipp);
87 procstat_rusage(prstat, kipp);
89 procstat_cred(prstat, kipp);
91 procstat_threads(prstat, kipp);
93 procstat_vm(prstat, kipp);
95 procstat_auxv(prstat, kipp);
97 procstat_cs(prstat, kipp);
101 xo_close_container(pidstr);
106 * Sort processes first by pid and then tid.
109 kinfo_proc_compare(const void *a, const void *b)
113 i = ((const struct kinfo_proc *)a)->ki_pid -
114 ((const struct kinfo_proc *)b)->ki_pid;
117 i = ((const struct kinfo_proc *)a)->ki_tid -
118 ((const struct kinfo_proc *)b)->ki_tid;
123 kinfo_proc_sort(struct kinfo_proc *kipp, int count)
126 qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare);
130 main(int argc, char *argv[])
132 int ch, interval, tmp;
134 struct kinfo_proc *p;
135 struct procstat *prstat, *cprstat;
140 const char *xocontainer;
144 memf = nlistf = NULL;
145 argc = xo_parse_args(argc, argv);
146 xocontainer = "basic";
148 while ((ch = getopt(argc, argv, "CHN:M:abcefijklhrsStvw:x")) != -1) {
174 xocontainer = "binary";
179 xocontainer = "arguments";
184 xocontainer = "environment";
189 xocontainer = "files";
194 xocontainer = "signals";
199 xocontainer = "thread_signals";
204 xocontainer = "kstack";
209 xocontainer = "rlimit";
222 xocontainer = "rusage";
227 xocontainer = "credentials";
232 xocontainer = "threads";
241 l = strtol(optarg, &dummy, 10);
244 if (l < 1 || l > INT_MAX)
251 xocontainer = "auxv";
263 /* We require that either 0 or 1 mode flags be set. */
264 tmp = bflag + cflag + eflag + fflag + iflag + jflag + (kflag ? 1 : 0) +
265 lflag + rflag + sflag + tflag + vflag + xflag + Sflag;
266 if (!(tmp == 0 || tmp == 1))
269 /* We allow -k to be specified up to twice, but not more. */
273 /* Must specify either the -a flag or a list of pids. */
274 if (!(aflag == 1 && argc == 0) && !(aflag == 0 && argc > 0))
277 /* Only allow -C with -f. */
282 prstat = procstat_open_kvm(nlistf, memf);
284 prstat = procstat_open_sysctl();
286 xo_errx(1, "procstat_open()");
288 xo_set_version(PROCSTAT_XO_VERSION);
289 xo_open_container("procstat");
290 xo_open_container(xocontainer);
293 p = procstat_getprocs(prstat, KERN_PROC_PROC, 0, &cnt);
295 xo_errx(1, "procstat_getprocs()");
296 kinfo_proc_sort(p, cnt);
297 for (i = 0; i < cnt; i++) {
298 procstat(prstat, &p[i]);
300 /* Suppress header after first process. */
304 procstat_freeprocs(prstat, p);
306 for (i = 0; i < argc; i++) {
307 l = strtol(argv[i], &dummy, 10);
308 if (*dummy == '\0') {
313 p = procstat_getprocs(prstat, KERN_PROC_PID,
316 xo_errx(1, "procstat_getprocs()");
319 procstat_freeprocs(prstat, p);
321 cprstat = procstat_open_core(argv[i]);
322 if (cprstat == NULL) {
323 warnx("procstat_open()");
326 p = procstat_getprocs(cprstat, KERN_PROC_PID,
329 xo_errx(1, "procstat_getprocs()");
331 procstat(cprstat, p);
332 procstat_freeprocs(cprstat, p);
333 procstat_close(cprstat);
335 /* Suppress header after first process. */
339 xo_close_container(xocontainer);
340 xo_close_container("procstat");
346 procstat_close(prstat);