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 { "signal", "signals", "[-n]", &procstat_sigs, &cmdopt_signals,
98 PS_CMP_PLURAL | PS_CMP_SUBSTR },
99 { "thread", "threads", NULL, &procstat_threads, &cmdopt_none,
101 { "tsignal", "thread_signals", "[-n]", &procstat_threads_sigs,
102 &cmdopt_signals, PS_CMP_PLURAL | PS_CMP_SUBSTR },
103 { "vm", "vm", NULL, &procstat_vm, &cmdopt_none, PS_CMP_NORMAL }
112 xo_error("usage: procstat [--libxo] [-h] [-M core] [-N system]"
113 " [-w interval] command\n"
114 " [pid ... | core ...]\n"
115 " procstat [--libxo] -a [-h] [-M core] [-N system] "
116 " [-w interval] command\n"
117 " procstat [--libxo] [-h] [-M core] [-N system]"
119 " [-S | -b | -c | -e | -f [-C] | -i [-n] | "
120 "-j [-n] | -k [-k] |\n"
121 " -l | -r [-H] | -s | -t | -v | -x] "
122 "[pid ... | core ...]\n"
123 " procstat [--libxo] -a [-h] [-M core] [-N system]"
125 " [-S | -b | -c | -e | -f [-C] | -i [-n] | "
126 "-j [-n] | -k [-k] |\n"
127 " -l | -r [-H] | -s | -t | -v | -x]\n"
128 " procstat [--libxo] -L [-h] [-M core] [-N system] core ...\n"
129 "Available commands:\n");
130 for (i = 0, l = nitems(cmd_table); i < l; i++) {
131 multi = i + 1 < l && cmd_table[i].cmd == cmd_table[i + 1].cmd;
132 xo_error(" %s%s%s", multi ? "[" : "",
133 cmd_table[i].command, (cmd_table[i].cmp & PS_CMP_PLURAL) ?
135 for (; i + 1 < l && cmd_table[i].cmd == cmd_table[i + 1].cmd;
137 xo_error(" | %s%s", cmd_table[i + 1].command,
138 (cmd_table[i].cmp & PS_CMP_PLURAL) ? "(s)" : "");
141 if (cmd_table[i].usage != NULL)
142 xo_error(" %s", cmd_table[i].usage);
150 procstat(const struct procstat_cmd *cmd, struct procstat *prstat,
151 struct kinfo_proc *kipp)
155 asprintf(&pidstr, "%d", kipp->ki_pid);
157 xo_errc(1, ENOMEM, "Failed to allocate memory in procstat()");
158 xo_open_container(pidstr);
159 cmd->cmd(prstat, kipp);
160 xo_close_container(pidstr);
165 * Sort processes first by pid and then tid.
168 kinfo_proc_compare(const void *a, const void *b)
172 i = ((const struct kinfo_proc *)a)->ki_pid -
173 ((const struct kinfo_proc *)b)->ki_pid;
176 i = ((const struct kinfo_proc *)a)->ki_tid -
177 ((const struct kinfo_proc *)b)->ki_tid;
182 kinfo_proc_sort(struct kinfo_proc *kipp, int count)
185 qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare);
189 kinfo_proc_thread_name(const struct kinfo_proc *kipp)
191 static char name[MAXCOMLEN+1];
193 strlcpy(name, kipp->ki_tdname, sizeof(name));
194 strlcat(name, kipp->ki_moretdname, sizeof(name));
195 if (name[0] == '\0' || strcmp(kipp->ki_comm, name) == 0) {
203 static const struct procstat_cmd *
204 getcmd(const char *str)
206 const struct procstat_cmd *cmd;
213 if ((l = strlen(str)) == 0)
214 return (getcmd("basic"));
215 s = l > 1 && strcasecmp(str + l - 1, "s") == 0;
216 for (i = 0; i < nitems(cmd_table); i++) {
218 * After the first match substring matches are disabled,
219 * allowing subsequent full matches to take precedence.
221 if (cmd == NULL && (cmd_table[i].cmp & PS_CMP_SUBSTR))
222 cmp = strncasecmp(str, cmd_table[i].command, l -
223 ((cmd_table[i].cmp & PS_CMP_PLURAL) && s ? 1 : 0));
224 else if ((cmd_table[i].cmp & PS_CMP_PLURAL) && s &&
225 l == strlen(cmd_table[i].command) + 1)
226 cmp = strncasecmp(str, cmd_table[i].command, l - 1);
228 cmp = strcasecmp(str, cmd_table[i].command);
236 main(int argc, char *argv[])
240 struct kinfo_proc *p;
241 const struct procstat_cmd *cmd;
242 struct procstat *prstat, *cprstat;
252 memf = nlistf = NULL;
254 argc = xo_parse_args(argc, argv);
256 while ((ch = getopt(argc, argv, "abCcefHhijkLlM:N:nrSstvw:x")) != -1) {
264 cmd = getcmd("binary");
267 procstat_opts |= PS_OPT_CAPABILITIES;
272 cmd = getcmd("arguments");
277 cmd = getcmd("environment");
282 cmd = getcmd("files");
285 procstat_opts |= PS_OPT_PERTHREAD;
288 procstat_opts |= PS_OPT_NOHEADER;
293 cmd = getcmd("signals");
298 cmd = getcmd("tsignals");
301 if (cmd != NULL && cmd->cmd == procstat_kstack) {
302 if ((procstat_opts & PS_OPT_VERBOSE) != 0)
304 procstat_opts |= PS_OPT_VERBOSE;
308 cmd = getcmd("kstack");
314 cmd = getcmd("ptlwpinfo");
319 cmd = getcmd("rlimit");
328 procstat_opts |= PS_OPT_SIGNUM;
333 cmd = getcmd("rusage");
338 cmd = getcmd("cpuset");
343 cmd = getcmd("credentials");
348 cmd = getcmd("threads");
356 l = strtol(optarg, &dummy, 10);
359 if (l < 1 || l > INT_MAX)
366 cmd = getcmd("auxv");
377 if (cmd == NULL && argv[0] != NULL && (cmd = getcmd(argv[0])) != NULL) {
378 if ((procstat_opts & PS_SUBCOMMAND_OPTS) != 0)
380 if (cmd->opt != NULL) {
383 cmd->opt(argc, argv);
392 cmd = getcmd("basic");
393 if (cmd->cmd != procstat_files &&
394 (procstat_opts & PS_OPT_CAPABILITIES) != 0)
398 /* Must specify either the -a flag or a list of pids. */
399 if (!(aflag == 1 && argc == 0) && !(aflag == 0 && argc > 0))
403 prstat = procstat_open_kvm(nlistf, memf);
405 prstat = procstat_open_sysctl();
407 xo_errx(1, "procstat_open()");
409 xo_set_version(PROCSTAT_XO_VERSION);
410 xo_open_container("procstat");
411 xo_open_container(cmd->xocontainer);
414 p = procstat_getprocs(prstat, KERN_PROC_PROC, 0, &cnt);
416 xo_errx(1, "procstat_getprocs()");
417 kinfo_proc_sort(p, cnt);
418 for (i = 0; i < cnt; i++) {
419 procstat(cmd, prstat, &p[i]);
421 /* Suppress header after first process. */
422 procstat_opts |= PS_OPT_NOHEADER;
425 procstat_freeprocs(prstat, p);
427 for (i = 0; i < argc; i++) {
428 l = strtol(argv[i], &dummy, 10);
429 if (*dummy == '\0') {
434 p = procstat_getprocs(prstat, KERN_PROC_PID,
437 xo_errx(1, "procstat_getprocs()");
439 procstat(cmd, prstat, p);
440 procstat_freeprocs(prstat, p);
442 cprstat = procstat_open_core(argv[i]);
443 if (cprstat == NULL) {
444 warnx("procstat_open()");
447 p = procstat_getprocs(cprstat, KERN_PROC_PID,
450 xo_errx(1, "procstat_getprocs()");
452 procstat(cmd, cprstat, p);
453 procstat_freeprocs(cprstat, p);
454 procstat_close(cprstat);
456 /* Suppress header after first process. */
457 procstat_opts |= PS_OPT_NOHEADER;
460 xo_close_container(cmd->xocontainer);
461 xo_close_container("procstat");
467 procstat_close(prstat);
473 cmdopt_none(int argc, char * const argv[])
477 while ((ch = getopt(argc, argv, "")) != -1) {
487 cmdopt_verbose(int argc, char * const argv[])
491 while ((ch = getopt(argc, argv, "v")) != -1) {
494 procstat_opts |= PS_OPT_VERBOSE;
504 cmdopt_signals(int argc, char * const argv[])
508 while ((ch = getopt(argc, argv, "n")) != -1) {
511 procstat_opts |= PS_OPT_SIGNUM;
521 cmdopt_rusage(int argc, char * const argv[])
525 while ((ch = getopt(argc, argv, "Ht")) != -1) {
530 procstat_opts |= PS_OPT_PERTHREAD;
540 cmdopt_files(int argc, char * const argv[])
544 while ((ch = getopt(argc, argv, "C")) != -1) {
547 procstat_opts |= PS_OPT_CAPABILITIES;
557 cmdopt_cpuset(int argc, char * const argv[])
560 procstat_opts |= PS_OPT_PERTHREAD;
561 cmdopt_none(argc, argv);