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>
44 static int aflag, bflag, cflag, eflag, fflag, iflag, jflag, kflag, lflag, rflag;
45 static int sflag, tflag, vflag, xflag, Sflag;
46 int hflag, nflag, Cflag, Hflag;
52 xo_error("usage: procstat [-CHhn] [-M core] [-N system] "
54 " [-b | -c | -e | -f | -i | -j | -k | "
55 "-l | -r | -s | -S | -t | -v | -x]\n"
56 " [-a | pid | core ...]\n");
62 procstat(struct procstat *prstat, struct kinfo_proc *kipp)
66 asprintf(&pidstr, "%d", kipp->ki_pid);
68 xo_errc(1, ENOMEM, "Failed to allocate memory in procstat()");
69 xo_open_container(pidstr);
72 procstat_bin(prstat, kipp);
74 procstat_args(prstat, kipp);
76 procstat_env(prstat, kipp);
78 procstat_files(prstat, kipp);
80 procstat_sigs(prstat, kipp);
82 procstat_threads_sigs(prstat, kipp);
84 procstat_kstack(prstat, kipp, kflag);
86 procstat_rlimit(prstat, kipp);
88 procstat_rusage(prstat, kipp);
90 procstat_cred(prstat, kipp);
92 procstat_threads(prstat, kipp);
94 procstat_vm(prstat, kipp);
96 procstat_auxv(prstat, kipp);
98 procstat_cs(prstat, kipp);
100 procstat_basic(kipp);
102 xo_close_container(pidstr);
107 * Sort processes first by pid and then tid.
110 kinfo_proc_compare(const void *a, const void *b)
114 i = ((const struct kinfo_proc *)a)->ki_pid -
115 ((const struct kinfo_proc *)b)->ki_pid;
118 i = ((const struct kinfo_proc *)a)->ki_tid -
119 ((const struct kinfo_proc *)b)->ki_tid;
124 kinfo_proc_sort(struct kinfo_proc *kipp, int count)
127 qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare);
131 kinfo_proc_thread_name(const struct kinfo_proc *kipp)
133 static char name[MAXCOMLEN+1];
135 strlcpy(name, kipp->ki_tdname, sizeof(name));
136 strlcat(name, kipp->ki_moretdname, sizeof(name));
137 if (name[0] == '\0' || strcmp(kipp->ki_comm, name) == 0) {
146 main(int argc, char *argv[])
148 int ch, interval, tmp;
150 struct kinfo_proc *p;
151 struct procstat *prstat, *cprstat;
156 const char *xocontainer;
160 memf = nlistf = NULL;
161 argc = xo_parse_args(argc, argv);
162 xocontainer = "basic";
164 while ((ch = getopt(argc, argv, "CHN:M:abcefijklhrsStvw:x")) != -1) {
190 xocontainer = "binary";
195 xocontainer = "arguments";
200 xocontainer = "environment";
205 xocontainer = "files";
210 xocontainer = "signals";
215 xocontainer = "thread_signals";
220 xocontainer = "kstack";
225 xocontainer = "rlimit";
238 xocontainer = "rusage";
243 xocontainer = "credentials";
248 xocontainer = "threads";
257 l = strtol(optarg, &dummy, 10);
260 if (l < 1 || l > INT_MAX)
267 xocontainer = "auxv";
279 /* We require that either 0 or 1 mode flags be set. */
280 tmp = bflag + cflag + eflag + fflag + iflag + jflag + (kflag ? 1 : 0) +
281 lflag + rflag + sflag + tflag + vflag + xflag + Sflag;
282 if (!(tmp == 0 || tmp == 1))
285 /* We allow -k to be specified up to twice, but not more. */
289 /* Must specify either the -a flag or a list of pids. */
290 if (!(aflag == 1 && argc == 0) && !(aflag == 0 && argc > 0))
293 /* Only allow -C with -f. */
298 prstat = procstat_open_kvm(nlistf, memf);
300 prstat = procstat_open_sysctl();
302 xo_errx(1, "procstat_open()");
304 xo_set_version(PROCSTAT_XO_VERSION);
305 xo_open_container("procstat");
306 xo_open_container(xocontainer);
309 p = procstat_getprocs(prstat, KERN_PROC_PROC, 0, &cnt);
311 xo_errx(1, "procstat_getprocs()");
312 kinfo_proc_sort(p, cnt);
313 for (i = 0; i < cnt; i++) {
314 procstat(prstat, &p[i]);
316 /* Suppress header after first process. */
320 procstat_freeprocs(prstat, p);
322 for (i = 0; i < argc; i++) {
323 l = strtol(argv[i], &dummy, 10);
324 if (*dummy == '\0') {
329 p = procstat_getprocs(prstat, KERN_PROC_PID,
332 xo_errx(1, "procstat_getprocs()");
335 procstat_freeprocs(prstat, p);
337 cprstat = procstat_open_core(argv[i]);
338 if (cprstat == NULL) {
339 warnx("procstat_open()");
342 p = procstat_getprocs(cprstat, KERN_PROC_PID,
345 xo_errx(1, "procstat_getprocs()");
347 procstat(cprstat, p);
348 procstat_freeprocs(cprstat, p);
349 procstat_close(cprstat);
351 /* Suppress header after first process. */
355 xo_close_container(xocontainer);
356 xo_close_container("procstat");
362 procstat_close(prstat);