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;
54 "usage: procstat [--libxo] [-Hhn] [-M core] "
55 "[-N system] [-w interval]\n"
56 " [-S | -b | -c | -e | -i | -j | -k | -kk | "
59 " [-a | pid ... | core ...]\n"
60 " procstat [--libxo] -Cf [-hn] [-M core] "
61 "[-N system] [-a | pid ... | core ...]\n"
62 " [-S | -b | -c | -e | -i | -j | -k | -kk | "
64 " procstat [--libxo] -L [-hn] [-M core] "
65 "[-N system] [-w interval]\n"
66 " [-S | -b | -c | -e | -i | -j | -k | -kk | "
75 procstat(struct procstat *prstat, struct kinfo_proc *kipp)
79 asprintf(&pidstr, "%d", kipp->ki_pid);
81 xo_errc(1, ENOMEM, "Failed to allocate memory in procstat()");
82 xo_open_container(pidstr);
85 procstat_bin(prstat, kipp);
87 procstat_args(prstat, kipp);
89 procstat_env(prstat, kipp);
91 procstat_files(prstat, kipp);
93 procstat_sigs(prstat, kipp);
95 procstat_threads_sigs(prstat, kipp);
97 procstat_kstack(prstat, kipp, kflag);
99 procstat_rlimit(prstat, kipp);
101 procstat_ptlwpinfo(prstat);
103 procstat_rusage(prstat, kipp);
105 procstat_cred(prstat, kipp);
107 procstat_threads(prstat, kipp);
109 procstat_vm(prstat, kipp);
111 procstat_auxv(prstat, kipp);
113 procstat_cs(prstat, kipp);
115 procstat_basic(kipp);
117 xo_close_container(pidstr);
122 * Sort processes first by pid and then tid.
125 kinfo_proc_compare(const void *a, const void *b)
129 i = ((const struct kinfo_proc *)a)->ki_pid -
130 ((const struct kinfo_proc *)b)->ki_pid;
133 i = ((const struct kinfo_proc *)a)->ki_tid -
134 ((const struct kinfo_proc *)b)->ki_tid;
139 kinfo_proc_sort(struct kinfo_proc *kipp, int count)
142 qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare);
146 kinfo_proc_thread_name(const struct kinfo_proc *kipp)
148 static char name[MAXCOMLEN+1];
150 strlcpy(name, kipp->ki_tdname, sizeof(name));
151 strlcat(name, kipp->ki_moretdname, sizeof(name));
152 if (name[0] == '\0' || strcmp(kipp->ki_comm, name) == 0) {
161 main(int argc, char *argv[])
163 int ch, interval, tmp;
165 struct kinfo_proc *p;
166 struct procstat *prstat, *cprstat;
171 const char *xocontainer;
175 memf = nlistf = NULL;
176 argc = xo_parse_args(argc, argv);
177 xocontainer = "basic";
179 while ((ch = getopt(argc, argv, "abCcefHhijkLlM:N:nrSstvw:x")) != -1) {
205 xocontainer = "binary";
210 xocontainer = "arguments";
215 xocontainer = "environment";
220 xocontainer = "files";
225 xocontainer = "signals";
230 xocontainer = "thread_signals";
235 xocontainer = "kstack";
240 xocontainer = "rlimit";
245 xocontainer = "ptlwpinfo";
258 xocontainer = "rusage";
263 xocontainer = "credentials";
268 xocontainer = "threads";
277 l = strtol(optarg, &dummy, 10);
280 if (l < 1 || l > INT_MAX)
287 xocontainer = "auxv";
299 /* We require that either 0 or 1 mode flags be set. */
300 tmp = bflag + cflag + eflag + fflag + iflag + jflag + (kflag ? 1 : 0) +
301 lflag + rflag + sflag + tflag + vflag + xflag + Sflag;
302 if (!(tmp == 0 || tmp == 1))
305 /* We allow -k to be specified up to twice, but not more. */
309 /* Must specify either the -a flag or a list of pids. */
310 if (!(aflag == 1 && argc == 0) && !(aflag == 0 && argc > 0))
313 /* Only allow -C with -f. */
318 prstat = procstat_open_kvm(nlistf, memf);
320 prstat = procstat_open_sysctl();
322 xo_errx(1, "procstat_open()");
324 xo_set_version(PROCSTAT_XO_VERSION);
325 xo_open_container("procstat");
326 xo_open_container(xocontainer);
329 p = procstat_getprocs(prstat, KERN_PROC_PROC, 0, &cnt);
331 xo_errx(1, "procstat_getprocs()");
332 kinfo_proc_sort(p, cnt);
333 for (i = 0; i < cnt; i++) {
334 procstat(prstat, &p[i]);
336 /* Suppress header after first process. */
340 procstat_freeprocs(prstat, p);
342 for (i = 0; i < argc; i++) {
343 l = strtol(argv[i], &dummy, 10);
344 if (*dummy == '\0') {
349 p = procstat_getprocs(prstat, KERN_PROC_PID,
352 xo_errx(1, "procstat_getprocs()");
355 procstat_freeprocs(prstat, p);
357 cprstat = procstat_open_core(argv[i]);
358 if (cprstat == NULL) {
359 warnx("procstat_open()");
362 p = procstat_getprocs(cprstat, KERN_PROC_PID,
365 xo_errx(1, "procstat_getprocs()");
367 procstat(cprstat, p);
368 procstat_freeprocs(cprstat, p);
369 procstat_close(cprstat);
371 /* Suppress header after first process. */
375 xo_close_container(xocontainer);
376 xo_close_container("procstat");
382 procstat_close(prstat);