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 [-CHhn] [-M core] [-N system] "
55 " [-b | -c | -e | -f | -i | -j | -k | "
56 "-l | -r | -s | -S | -t | -v | -x]\n"
57 " [-a | pid | core ...]\n");
63 procstat(struct procstat *prstat, struct kinfo_proc *kipp)
67 asprintf(&pidstr, "%d", kipp->ki_pid);
69 xo_errc(1, ENOMEM, "Failed to allocate memory in procstat()");
70 xo_open_container(pidstr);
73 procstat_bin(prstat, kipp);
75 procstat_args(prstat, kipp);
77 procstat_env(prstat, kipp);
79 procstat_files(prstat, kipp);
81 procstat_sigs(prstat, kipp);
83 procstat_threads_sigs(prstat, kipp);
85 procstat_kstack(prstat, kipp, kflag);
87 procstat_rlimit(prstat, kipp);
89 procstat_ptlwpinfo(prstat);
91 procstat_rusage(prstat, kipp);
93 procstat_cred(prstat, kipp);
95 procstat_threads(prstat, kipp);
97 procstat_vm(prstat, kipp);
99 procstat_auxv(prstat, kipp);
101 procstat_cs(prstat, kipp);
103 procstat_basic(kipp);
105 xo_close_container(pidstr);
110 * Sort processes first by pid and then tid.
113 kinfo_proc_compare(const void *a, const void *b)
117 i = ((const struct kinfo_proc *)a)->ki_pid -
118 ((const struct kinfo_proc *)b)->ki_pid;
121 i = ((const struct kinfo_proc *)a)->ki_tid -
122 ((const struct kinfo_proc *)b)->ki_tid;
127 kinfo_proc_sort(struct kinfo_proc *kipp, int count)
130 qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare);
134 kinfo_proc_thread_name(const struct kinfo_proc *kipp)
136 static char name[MAXCOMLEN+1];
138 strlcpy(name, kipp->ki_tdname, sizeof(name));
139 strlcat(name, kipp->ki_moretdname, sizeof(name));
140 if (name[0] == '\0' || strcmp(kipp->ki_comm, name) == 0) {
149 main(int argc, char *argv[])
151 int ch, interval, tmp;
153 struct kinfo_proc *p;
154 struct procstat *prstat, *cprstat;
159 const char *xocontainer;
163 memf = nlistf = NULL;
164 argc = xo_parse_args(argc, argv);
165 xocontainer = "basic";
167 while ((ch = getopt(argc, argv, "CHN:M:abcefijklLhrsStvw:x")) != -1) {
193 xocontainer = "binary";
198 xocontainer = "arguments";
203 xocontainer = "environment";
208 xocontainer = "files";
213 xocontainer = "signals";
218 xocontainer = "thread_signals";
223 xocontainer = "kstack";
228 xocontainer = "rlimit";
233 xocontainer = "ptlwpinfo";
246 xocontainer = "rusage";
251 xocontainer = "credentials";
256 xocontainer = "threads";
265 l = strtol(optarg, &dummy, 10);
268 if (l < 1 || l > INT_MAX)
275 xocontainer = "auxv";
287 /* We require that either 0 or 1 mode flags be set. */
288 tmp = bflag + cflag + eflag + fflag + iflag + jflag + (kflag ? 1 : 0) +
289 lflag + rflag + sflag + tflag + vflag + xflag + Sflag;
290 if (!(tmp == 0 || tmp == 1))
293 /* We allow -k to be specified up to twice, but not more. */
297 /* Must specify either the -a flag or a list of pids. */
298 if (!(aflag == 1 && argc == 0) && !(aflag == 0 && argc > 0))
301 /* Only allow -C with -f. */
306 prstat = procstat_open_kvm(nlistf, memf);
308 prstat = procstat_open_sysctl();
310 xo_errx(1, "procstat_open()");
312 xo_set_version(PROCSTAT_XO_VERSION);
313 xo_open_container("procstat");
314 xo_open_container(xocontainer);
317 p = procstat_getprocs(prstat, KERN_PROC_PROC, 0, &cnt);
319 xo_errx(1, "procstat_getprocs()");
320 kinfo_proc_sort(p, cnt);
321 for (i = 0; i < cnt; i++) {
322 procstat(prstat, &p[i]);
324 /* Suppress header after first process. */
328 procstat_freeprocs(prstat, p);
330 for (i = 0; i < argc; i++) {
331 l = strtol(argv[i], &dummy, 10);
332 if (*dummy == '\0') {
337 p = procstat_getprocs(prstat, KERN_PROC_PID,
340 xo_errx(1, "procstat_getprocs()");
343 procstat_freeprocs(prstat, p);
345 cprstat = procstat_open_core(argv[i]);
346 if (cprstat == NULL) {
347 warnx("procstat_open()");
350 p = procstat_getprocs(cprstat, KERN_PROC_PID,
353 xo_errx(1, "procstat_getprocs()");
355 procstat(cprstat, p);
356 procstat_freeprocs(cprstat, p);
357 procstat_close(cprstat);
359 /* Suppress header after first process. */
363 xo_close_container(xocontainer);
364 xo_close_container("procstat");
370 procstat_close(prstat);