2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2007, 2011 Robert N. M. Watson
5 * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
6 * Copyright (c) 2017 Dell EMC
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/param.h>
34 #include <sys/sysctl.h>
38 #include <libprocstat.h>
55 const char *xocontainer;
57 void (*cmd)(struct procstat *, struct kinfo_proc *);
58 void (*opt)(int, char * const *);
62 int procstat_opts = 0;
64 static void cmdopt_none(int argc, char * const argv[]);
65 static void cmdopt_verbose(int argc, char * const argv[]);
66 static void cmdopt_signals(int argc, char * const argv[]);
67 static void cmdopt_rusage(int argc, char * const argv[]);
68 static void cmdopt_files(int argc, char * const argv[]);
69 static void cmdopt_cpuset(int argc, char * const argv[]);
71 static const struct procstat_cmd cmd_table[] = {
72 { "argument", "arguments", NULL, &procstat_args, &cmdopt_none,
73 PS_CMP_PLURAL | PS_CMP_SUBSTR },
74 { "auxv", "auxv", NULL, &procstat_auxv, &cmdopt_none, PS_CMP_NORMAL },
75 { "basic", "basic", NULL, &procstat_basic, &cmdopt_none,
77 { "binary", "binary", NULL, &procstat_bin, &cmdopt_none,
79 { "cpuset", "cs", NULL, &procstat_cs, &cmdopt_cpuset, PS_CMP_NORMAL },
80 { "cs", "cs", NULL, &procstat_cs, &cmdopt_cpuset, PS_CMP_NORMAL },
81 { "credential", "credentials", NULL, &procstat_cred, &cmdopt_none,
82 PS_CMP_PLURAL | PS_CMP_SUBSTR },
83 { "environment", "environment", NULL, &procstat_env, &cmdopt_none,
85 { "fd", "files", "[-C]", &procstat_files, &cmdopt_files,
87 { "file", "files", "[-C]", &procstat_files, &cmdopt_files,
89 { "kstack", "kstack", "[-v]", &procstat_kstack, &cmdopt_verbose,
91 { "ptlwpinfo", "ptlwpinfo", NULL, &procstat_ptlwpinfo, &cmdopt_none,
93 { "rlimit", "rlimit", NULL, &procstat_rlimit, &cmdopt_none,
95 { "rusage", "rusage", "[-Ht]", &procstat_rusage, &cmdopt_rusage,
97 { "sigfastblock", "sigfastblock", NULL, &procstat_sigfastblock,
98 &cmdopt_none, PS_CMP_NORMAL },
99 { "signal", "signals", "[-n]", &procstat_sigs, &cmdopt_signals,
100 PS_CMP_PLURAL | PS_CMP_SUBSTR },
101 { "thread", "threads", NULL, &procstat_threads, &cmdopt_none,
103 { "tsignal", "thread_signals", "[-n]", &procstat_threads_sigs,
104 &cmdopt_signals, PS_CMP_PLURAL | PS_CMP_SUBSTR },
105 { "vm", "vm", NULL, &procstat_vm, &cmdopt_none, PS_CMP_NORMAL }
114 xo_error("usage: procstat [--libxo] [-h] [-M core] [-N system]"
115 " [-w interval] command\n"
116 " [pid ... | core ...]\n"
117 " procstat [--libxo] -a [-h] [-M core] [-N system] "
118 " [-w interval] command\n"
119 " procstat [--libxo] [-h] [-M core] [-N system]"
121 " [-S | -b | -c | -e | -f [-C] | -i [-n] | "
122 "-j [-n] | -k [-k] |\n"
123 " -l | -r [-H] | -s | -t | -v | -x] "
124 "[pid ... | core ...]\n"
125 " procstat [--libxo] -a [-h] [-M core] [-N system]"
127 " [-S | -b | -c | -e | -f [-C] | -i [-n] | "
128 "-j [-n] | -k [-k] |\n"
129 " -l | -r [-H] | -s | -t | -v | -x]\n"
130 " procstat [--libxo] -L [-h] [-M core] [-N system] core ...\n"
131 "Available commands:\n");
132 for (i = 0, l = nitems(cmd_table); i < l; i++) {
133 multi = i + 1 < l && cmd_table[i].cmd == cmd_table[i + 1].cmd;
134 xo_error(" %s%s%s", multi ? "[" : "",
135 cmd_table[i].command, (cmd_table[i].cmp & PS_CMP_PLURAL) ?
137 for (; i + 1 < l && cmd_table[i].cmd == cmd_table[i + 1].cmd;
139 xo_error(" | %s%s", cmd_table[i + 1].command,
140 (cmd_table[i].cmp & PS_CMP_PLURAL) ? "(s)" : "");
143 if (cmd_table[i].usage != NULL)
144 xo_error(" %s", cmd_table[i].usage);
152 procstat(const struct procstat_cmd *cmd, struct procstat *prstat,
153 struct kinfo_proc *kipp)
157 asprintf(&pidstr, "%d", kipp->ki_pid);
159 xo_errc(1, ENOMEM, "Failed to allocate memory in procstat()");
160 xo_open_container(pidstr);
161 cmd->cmd(prstat, kipp);
162 xo_close_container(pidstr);
167 * Sort processes first by pid and then tid.
170 kinfo_proc_compare(const void *a, const void *b)
174 i = ((const struct kinfo_proc *)a)->ki_pid -
175 ((const struct kinfo_proc *)b)->ki_pid;
178 i = ((const struct kinfo_proc *)a)->ki_tid -
179 ((const struct kinfo_proc *)b)->ki_tid;
184 kinfo_proc_sort(struct kinfo_proc *kipp, int count)
187 qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare);
191 kinfo_proc_thread_name(const struct kinfo_proc *kipp)
193 static char name[MAXCOMLEN+1];
195 strlcpy(name, kipp->ki_tdname, sizeof(name));
196 strlcat(name, kipp->ki_moretdname, sizeof(name));
197 if (name[0] == '\0' || strcmp(kipp->ki_comm, name) == 0) {
205 static const struct procstat_cmd *
206 getcmd(const char *str)
208 const struct procstat_cmd *cmd;
215 if ((l = strlen(str)) == 0)
216 return (getcmd("basic"));
217 s = l > 1 && strcasecmp(str + l - 1, "s") == 0;
218 for (i = 0; i < nitems(cmd_table); i++) {
220 * After the first match substring matches are disabled,
221 * allowing subsequent full matches to take precedence.
223 if (cmd == NULL && (cmd_table[i].cmp & PS_CMP_SUBSTR))
224 cmp = strncasecmp(str, cmd_table[i].command, l -
225 ((cmd_table[i].cmp & PS_CMP_PLURAL) && s ? 1 : 0));
226 else if ((cmd_table[i].cmp & PS_CMP_PLURAL) && s &&
227 l == strlen(cmd_table[i].command) + 1)
228 cmp = strncasecmp(str, cmd_table[i].command, l - 1);
230 cmp = strcasecmp(str, cmd_table[i].command);
238 main(int argc, char *argv[])
242 struct kinfo_proc *p;
243 const struct procstat_cmd *cmd;
244 struct procstat *prstat, *cprstat;
254 memf = nlistf = NULL;
256 argc = xo_parse_args(argc, argv);
258 while ((ch = getopt(argc, argv, "abCcefHhijkLlM:N:nrSstvw:x")) != -1) {
266 cmd = getcmd("binary");
269 procstat_opts |= PS_OPT_CAPABILITIES;
274 cmd = getcmd("arguments");
279 cmd = getcmd("environment");
284 cmd = getcmd("files");
287 procstat_opts |= PS_OPT_PERTHREAD;
290 procstat_opts |= PS_OPT_NOHEADER;
295 cmd = getcmd("signals");
300 cmd = getcmd("tsignals");
303 if (cmd != NULL && cmd->cmd == procstat_kstack) {
304 if ((procstat_opts & PS_OPT_VERBOSE) != 0)
306 procstat_opts |= PS_OPT_VERBOSE;
310 cmd = getcmd("kstack");
316 cmd = getcmd("ptlwpinfo");
321 cmd = getcmd("rlimit");
330 procstat_opts |= PS_OPT_SIGNUM;
335 cmd = getcmd("rusage");
340 cmd = getcmd("cpuset");
345 cmd = getcmd("credentials");
350 cmd = getcmd("threads");
358 l = strtol(optarg, &dummy, 10);
361 if (l < 1 || l > INT_MAX)
368 cmd = getcmd("auxv");
379 if (cmd == NULL && argv[0] != NULL && (cmd = getcmd(argv[0])) != NULL) {
380 if ((procstat_opts & PS_SUBCOMMAND_OPTS) != 0)
382 if (cmd->opt != NULL) {
385 cmd->opt(argc, argv);
394 cmd = getcmd("basic");
395 if (cmd->cmd != procstat_files &&
396 (procstat_opts & PS_OPT_CAPABILITIES) != 0)
400 /* Must specify either the -a flag or a list of pids. */
401 if (!(aflag == 1 && argc == 0) && !(aflag == 0 && argc > 0))
405 prstat = procstat_open_kvm(nlistf, memf);
407 prstat = procstat_open_sysctl();
409 xo_errx(1, "procstat_open()");
411 xo_set_version(PROCSTAT_XO_VERSION);
412 xo_open_container("procstat");
413 xo_open_container(cmd->xocontainer);
416 p = procstat_getprocs(prstat, KERN_PROC_PROC, 0, &cnt);
418 xo_errx(1, "procstat_getprocs()");
419 kinfo_proc_sort(p, cnt);
420 for (i = 0; i < cnt; i++) {
421 procstat(cmd, prstat, &p[i]);
423 /* Suppress header after first process. */
424 procstat_opts |= PS_OPT_NOHEADER;
427 procstat_freeprocs(prstat, p);
429 for (i = 0; i < argc; i++) {
430 l = strtol(argv[i], &dummy, 10);
431 if (*dummy == '\0') {
436 p = procstat_getprocs(prstat, KERN_PROC_PID,
439 xo_errx(1, "procstat_getprocs()");
441 procstat(cmd, prstat, p);
442 procstat_freeprocs(prstat, p);
444 cprstat = procstat_open_core(argv[i]);
445 if (cprstat == NULL) {
446 warnx("procstat_open()");
449 p = procstat_getprocs(cprstat, KERN_PROC_PID,
452 xo_errx(1, "procstat_getprocs()");
454 procstat(cmd, cprstat, p);
455 procstat_freeprocs(cprstat, p);
456 procstat_close(cprstat);
458 /* Suppress header after first process. */
459 procstat_opts |= PS_OPT_NOHEADER;
462 xo_close_container(cmd->xocontainer);
463 xo_close_container("procstat");
469 procstat_close(prstat);
475 cmdopt_none(int argc, char * const argv[])
479 while ((ch = getopt(argc, argv, "")) != -1) {
489 cmdopt_verbose(int argc, char * const argv[])
493 while ((ch = getopt(argc, argv, "v")) != -1) {
496 procstat_opts |= PS_OPT_VERBOSE;
506 cmdopt_signals(int argc, char * const argv[])
510 while ((ch = getopt(argc, argv, "n")) != -1) {
513 procstat_opts |= PS_OPT_SIGNUM;
523 cmdopt_rusage(int argc, char * const argv[])
527 while ((ch = getopt(argc, argv, "Ht")) != -1) {
532 procstat_opts |= PS_OPT_PERTHREAD;
542 cmdopt_files(int argc, char * const argv[])
546 while ((ch = getopt(argc, argv, "C")) != -1) {
549 procstat_opts |= PS_OPT_CAPABILITIES;
559 cmdopt_cpuset(int argc, char * const argv[])
562 procstat_opts |= PS_OPT_PERTHREAD;
563 cmdopt_none(argc, argv);