2 * Copyright (c) 2007, 2011 Robert N. M. Watson
3 * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
4 * Copyright (c) 2017 Dell EMC
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
32 #include <sys/sysctl.h>
36 #include <libprocstat.h>
45 static int aflag, bflag, cflag, eflag, fflag, iflag, jflag, kflag;
46 static int lflag, Lflag, rflag, sflag, tflag, vflag, xflag, Sflag;
47 int hflag, nflag, Cflag, Hflag;
53 xo_error("usage: procstat [--libxo] [-CHhn] [-M core] "
54 "[-N system] [-w interval]\n"
55 " [-b | -c | -e | -f | -i | -j | -k | "
56 "-l | -L | -r | -s | \n"
57 " -S | -t | -v | -x]\n"
58 " [-a | pid | core ...]\n");
64 procstat(struct procstat *prstat, struct kinfo_proc *kipp)
68 asprintf(&pidstr, "%d", kipp->ki_pid);
70 xo_errc(1, ENOMEM, "Failed to allocate memory in procstat()");
71 xo_open_container(pidstr);
74 procstat_bin(prstat, kipp);
76 procstat_args(prstat, kipp);
78 procstat_env(prstat, kipp);
80 procstat_files(prstat, kipp);
82 procstat_sigs(prstat, kipp);
84 procstat_threads_sigs(prstat, kipp);
86 procstat_kstack(prstat, kipp, kflag);
88 procstat_rlimit(prstat, kipp);
90 procstat_ptlwpinfo(prstat);
92 procstat_rusage(prstat, kipp);
94 procstat_cred(prstat, kipp);
96 procstat_threads(prstat, kipp);
98 procstat_vm(prstat, kipp);
100 procstat_auxv(prstat, kipp);
102 procstat_cs(prstat, kipp);
104 procstat_basic(kipp);
106 xo_close_container(pidstr);
111 * Sort processes first by pid and then tid.
114 kinfo_proc_compare(const void *a, const void *b)
118 i = ((const struct kinfo_proc *)a)->ki_pid -
119 ((const struct kinfo_proc *)b)->ki_pid;
122 i = ((const struct kinfo_proc *)a)->ki_tid -
123 ((const struct kinfo_proc *)b)->ki_tid;
128 kinfo_proc_sort(struct kinfo_proc *kipp, int count)
131 qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare);
135 kinfo_proc_thread_name(const struct kinfo_proc *kipp)
137 static char name[MAXCOMLEN+1];
139 strlcpy(name, kipp->ki_tdname, sizeof(name));
140 strlcat(name, kipp->ki_moretdname, sizeof(name));
141 if (name[0] == '\0' || strcmp(kipp->ki_comm, name) == 0) {
150 main(int argc, char *argv[])
152 int ch, interval, tmp;
154 struct kinfo_proc *p;
155 struct procstat *prstat, *cprstat;
160 const char *xocontainer;
164 memf = nlistf = NULL;
165 argc = xo_parse_args(argc, argv);
166 xocontainer = "basic";
168 while ((ch = getopt(argc, argv, "abCcefHhijkLlM:N:nrSstvw:x")) != -1) {
194 xocontainer = "binary";
199 xocontainer = "arguments";
204 xocontainer = "environment";
209 xocontainer = "files";
214 xocontainer = "signals";
219 xocontainer = "thread_signals";
224 xocontainer = "kstack";
229 xocontainer = "rlimit";
234 xocontainer = "ptlwpinfo";
247 xocontainer = "rusage";
252 xocontainer = "credentials";
257 xocontainer = "threads";
266 l = strtol(optarg, &dummy, 10);
269 if (l < 1 || l > INT_MAX)
276 xocontainer = "auxv";
288 /* We require that either 0 or 1 mode flags be set. */
289 tmp = bflag + cflag + eflag + fflag + iflag + jflag + (kflag ? 1 : 0) +
290 lflag + rflag + sflag + tflag + vflag + xflag + Sflag;
291 if (!(tmp == 0 || tmp == 1))
294 /* We allow -k to be specified up to twice, but not more. */
298 /* Must specify either the -a flag or a list of pids. */
299 if (!(aflag == 1 && argc == 0) && !(aflag == 0 && argc > 0))
302 /* Only allow -C with -f. */
307 prstat = procstat_open_kvm(nlistf, memf);
309 prstat = procstat_open_sysctl();
311 xo_errx(1, "procstat_open()");
313 xo_set_version(PROCSTAT_XO_VERSION);
314 xo_open_container("procstat");
315 xo_open_container(xocontainer);
318 p = procstat_getprocs(prstat, KERN_PROC_PROC, 0, &cnt);
320 xo_errx(1, "procstat_getprocs()");
321 kinfo_proc_sort(p, cnt);
322 for (i = 0; i < cnt; i++) {
323 procstat(prstat, &p[i]);
325 /* Suppress header after first process. */
329 procstat_freeprocs(prstat, p);
331 for (i = 0; i < argc; i++) {
332 l = strtol(argv[i], &dummy, 10);
333 if (*dummy == '\0') {
338 p = procstat_getprocs(prstat, KERN_PROC_PID,
341 xo_errx(1, "procstat_getprocs()");
344 procstat_freeprocs(prstat, p);
346 cprstat = procstat_open_core(argv[i]);
347 if (cprstat == NULL) {
348 warnx("procstat_open()");
351 p = procstat_getprocs(cprstat, KERN_PROC_PID,
354 xo_errx(1, "procstat_getprocs()");
356 procstat(cprstat, p);
357 procstat_freeprocs(cprstat, p);
358 procstat_close(cprstat);
360 /* Suppress header after first process. */
364 xo_close_container(xocontainer);
365 xo_close_container("procstat");
371 procstat_close(prstat);