2007-12-02 23:31:45 +00:00
|
|
|
/*-
|
2017-11-27 15:37:16 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2011-08-14 00:42:09 +00:00
|
|
|
* Copyright (c) 2007, 2011 Robert N. M. Watson
|
2015-09-05 17:02:01 +00:00
|
|
|
* Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
|
2017-03-30 18:21:36 +00:00
|
|
|
* Copyright (c) 2017 Dell EMC
|
2007-12-02 23:31:45 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-09-27 23:01:54 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2008-12-29 18:58:22 +00:00
|
|
|
#include <sys/param.h>
|
2007-12-02 23:31:45 +00:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/user.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
2011-05-12 10:11:39 +00:00
|
|
|
#include <libprocstat.h>
|
2007-12-02 23:31:45 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-12-07 15:04:22 +00:00
|
|
|
#include <string.h>
|
2007-12-02 23:31:45 +00:00
|
|
|
#include <sysexits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "procstat.h"
|
|
|
|
|
2017-10-14 18:38:36 +00:00
|
|
|
enum {
|
|
|
|
PS_CMP_NORMAL = 0x00,
|
|
|
|
PS_CMP_PLURAL = 0x01,
|
|
|
|
PS_CMP_SUBSTR = 0x02
|
|
|
|
};
|
|
|
|
|
|
|
|
struct procstat_cmd {
|
|
|
|
const char *command;
|
|
|
|
const char *xocontainer;
|
|
|
|
const char *usage;
|
|
|
|
void (*cmd)(struct procstat *, struct kinfo_proc *);
|
|
|
|
void (*opt)(int, char * const *);
|
|
|
|
int cmp;
|
|
|
|
};
|
|
|
|
|
|
|
|
int procstat_opts = 0;
|
|
|
|
|
|
|
|
static void cmdopt_none(int argc, char * const argv[]);
|
|
|
|
static void cmdopt_verbose(int argc, char * const argv[]);
|
|
|
|
static void cmdopt_signals(int argc, char * const argv[]);
|
|
|
|
static void cmdopt_rusage(int argc, char * const argv[]);
|
|
|
|
static void cmdopt_files(int argc, char * const argv[]);
|
|
|
|
static void cmdopt_cpuset(int argc, char * const argv[]);
|
|
|
|
|
2020-09-18 12:59:27 +00:00
|
|
|
static const char *progname;
|
|
|
|
|
|
|
|
/* aliased program parameters and arguments
|
|
|
|
* - usage field is abused to hold the pointer to the function
|
|
|
|
* displaying program usage
|
|
|
|
*/
|
|
|
|
static const struct procstat_cmd pacmd_table[] = {
|
|
|
|
/* arguments are the same as for pwdx: pid or core file */
|
|
|
|
{ "pargs", "args", NULL, &procstat_pargs, &cmdopt_none,
|
|
|
|
PS_CMP_NORMAL | PS_MODE_COMPAT },
|
|
|
|
{ "penv", "env", NULL, &procstat_penv, &cmdopt_none,
|
|
|
|
PS_CMP_NORMAL | PS_MODE_COMPAT },
|
|
|
|
{ "pwdx", "pwd", NULL, &procstat_pwdx, &cmdopt_none,
|
|
|
|
PS_CMP_NORMAL | PS_MODE_COMPAT }
|
|
|
|
};
|
|
|
|
|
|
|
|
/* procstat parameters and arguments */
|
2017-10-14 18:38:36 +00:00
|
|
|
static const struct procstat_cmd cmd_table[] = {
|
|
|
|
{ "argument", "arguments", NULL, &procstat_args, &cmdopt_none,
|
|
|
|
PS_CMP_PLURAL | PS_CMP_SUBSTR },
|
|
|
|
{ "auxv", "auxv", NULL, &procstat_auxv, &cmdopt_none, PS_CMP_NORMAL },
|
|
|
|
{ "basic", "basic", NULL, &procstat_basic, &cmdopt_none,
|
|
|
|
PS_CMP_NORMAL },
|
|
|
|
{ "binary", "binary", NULL, &procstat_bin, &cmdopt_none,
|
|
|
|
PS_CMP_SUBSTR },
|
|
|
|
{ "cpuset", "cs", NULL, &procstat_cs, &cmdopt_cpuset, PS_CMP_NORMAL },
|
|
|
|
{ "cs", "cs", NULL, &procstat_cs, &cmdopt_cpuset, PS_CMP_NORMAL },
|
|
|
|
{ "credential", "credentials", NULL, &procstat_cred, &cmdopt_none,
|
|
|
|
PS_CMP_PLURAL | PS_CMP_SUBSTR },
|
|
|
|
{ "environment", "environment", NULL, &procstat_env, &cmdopt_none,
|
|
|
|
PS_CMP_SUBSTR },
|
|
|
|
{ "fd", "files", "[-C]", &procstat_files, &cmdopt_files,
|
|
|
|
PS_CMP_PLURAL },
|
|
|
|
{ "file", "files", "[-C]", &procstat_files, &cmdopt_files,
|
|
|
|
PS_CMP_PLURAL },
|
|
|
|
{ "kstack", "kstack", "[-v]", &procstat_kstack, &cmdopt_verbose,
|
|
|
|
PS_CMP_NORMAL },
|
2020-09-18 12:59:27 +00:00
|
|
|
{ "pargs", "args", NULL, &procstat_pargs, &cmdopt_none,
|
|
|
|
PS_CMP_NORMAL },
|
|
|
|
{ "penv", "env", NULL, &procstat_penv, &cmdopt_none,
|
|
|
|
PS_CMP_NORMAL },
|
2017-10-14 18:38:36 +00:00
|
|
|
{ "ptlwpinfo", "ptlwpinfo", NULL, &procstat_ptlwpinfo, &cmdopt_none,
|
|
|
|
PS_CMP_NORMAL },
|
2020-09-18 12:59:27 +00:00
|
|
|
{ "pwdx", "pwd", NULL, &procstat_pwdx, &cmdopt_none,
|
|
|
|
PS_CMP_NORMAL },
|
2017-10-14 18:38:36 +00:00
|
|
|
{ "rlimit", "rlimit", NULL, &procstat_rlimit, &cmdopt_none,
|
|
|
|
PS_CMP_NORMAL },
|
|
|
|
{ "rusage", "rusage", "[-Ht]", &procstat_rusage, &cmdopt_rusage,
|
|
|
|
PS_CMP_NORMAL },
|
2020-02-09 12:30:50 +00:00
|
|
|
{ "sigfastblock", "sigfastblock", NULL, &procstat_sigfastblock,
|
|
|
|
&cmdopt_none, PS_CMP_NORMAL },
|
2017-10-14 18:38:36 +00:00
|
|
|
{ "signal", "signals", "[-n]", &procstat_sigs, &cmdopt_signals,
|
|
|
|
PS_CMP_PLURAL | PS_CMP_SUBSTR },
|
|
|
|
{ "thread", "threads", NULL, &procstat_threads, &cmdopt_none,
|
|
|
|
PS_CMP_PLURAL },
|
|
|
|
{ "tsignal", "thread_signals", "[-n]", &procstat_threads_sigs,
|
|
|
|
&cmdopt_signals, PS_CMP_PLURAL | PS_CMP_SUBSTR },
|
|
|
|
{ "vm", "vm", NULL, &procstat_vm, &cmdopt_none, PS_CMP_NORMAL }
|
|
|
|
};
|
2007-12-02 23:31:45 +00:00
|
|
|
|
|
|
|
static void
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(const struct procstat_cmd *cmd)
|
2007-12-02 23:31:45 +00:00
|
|
|
{
|
2017-10-14 18:38:36 +00:00
|
|
|
size_t i, l;
|
|
|
|
int multi;
|
|
|
|
|
2020-09-18 12:59:27 +00:00
|
|
|
if (cmd == NULL || (cmd->cmp & PS_MODE_COMPAT) == 0) {
|
|
|
|
xo_error("usage: procstat [--libxo] [-h] [-M core] [-N system]"
|
|
|
|
" [-w interval] command\n"
|
|
|
|
" [pid ... | core ...]\n"
|
|
|
|
" procstat [--libxo] -a [-h] [-M core] [-N system] "
|
|
|
|
" [-w interval] command\n"
|
|
|
|
" procstat [--libxo] [-h] [-M core] [-N system]"
|
|
|
|
" [-w interval]\n"
|
|
|
|
" [-S | -b | -c | -e | -f [-C] | -i [-n] | "
|
|
|
|
"-j [-n] | -k [-k] |\n"
|
|
|
|
" -l | -r [-H] | -s | -t | -v | -x] "
|
|
|
|
"[pid ... | core ...]\n"
|
|
|
|
" procstat [--libxo] -a [-h] [-M core] [-N system]"
|
|
|
|
" [-w interval]\n"
|
|
|
|
" [-S | -b | -c | -e | -f [-C] | -i [-n] | "
|
|
|
|
"-j [-n] | -k [-k] |\n"
|
|
|
|
" -l | -r [-H] | -s | -t | -v | -x]\n"
|
|
|
|
" procstat [--libxo] -L [-h] [-M core] [-N system] core ...\n"
|
|
|
|
"Available commands:\n");
|
|
|
|
for (i = 0, l = nitems(cmd_table); i < l; i++) {
|
|
|
|
multi = i + 1 < l && cmd_table[i].cmd ==
|
|
|
|
cmd_table[i + 1].cmd;
|
|
|
|
xo_error(" %s%s%s", multi ? "[" : "",
|
|
|
|
cmd_table[i].command, (cmd_table[i].cmp &
|
|
|
|
PS_CMP_PLURAL) ? "(s)" : "");
|
|
|
|
for (; i + 1 < l && cmd_table[i].cmd ==
|
|
|
|
cmd_table[i + 1].cmd; i++)
|
|
|
|
xo_error(" | %s%s", cmd_table[i + 1].command,
|
|
|
|
(cmd_table[i].cmp & PS_CMP_PLURAL) ?
|
|
|
|
"(s)" : "");
|
|
|
|
if (multi)
|
|
|
|
xo_error("]");
|
|
|
|
if (cmd_table[i].usage != NULL)
|
|
|
|
xo_error(" %s", cmd_table[i].usage);
|
|
|
|
xo_error("\n");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
xo_error("usage: %s [--libxo] pid ...\n", progname);
|
2017-10-14 18:38:36 +00:00
|
|
|
}
|
2015-09-05 17:02:01 +00:00
|
|
|
xo_finish();
|
2007-12-02 23:31:45 +00:00
|
|
|
exit(EX_USAGE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-10-14 18:38:36 +00:00
|
|
|
procstat(const struct procstat_cmd *cmd, struct procstat *prstat,
|
|
|
|
struct kinfo_proc *kipp)
|
2007-12-02 23:31:45 +00:00
|
|
|
{
|
2015-09-05 17:02:01 +00:00
|
|
|
char *pidstr = NULL;
|
|
|
|
|
|
|
|
asprintf(&pidstr, "%d", kipp->ki_pid);
|
|
|
|
if (pidstr == NULL)
|
|
|
|
xo_errc(1, ENOMEM, "Failed to allocate memory in procstat()");
|
|
|
|
xo_open_container(pidstr);
|
2017-10-14 18:38:36 +00:00
|
|
|
cmd->cmd(prstat, kipp);
|
2015-09-05 17:02:01 +00:00
|
|
|
xo_close_container(pidstr);
|
|
|
|
free(pidstr);
|
2007-12-02 23:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sort processes first by pid and then tid.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
kinfo_proc_compare(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2008-02-08 11:03:05 +00:00
|
|
|
i = ((const struct kinfo_proc *)a)->ki_pid -
|
|
|
|
((const struct kinfo_proc *)b)->ki_pid;
|
2007-12-02 23:31:45 +00:00
|
|
|
if (i != 0)
|
|
|
|
return (i);
|
2008-02-08 11:03:05 +00:00
|
|
|
i = ((const struct kinfo_proc *)a)->ki_tid -
|
|
|
|
((const struct kinfo_proc *)b)->ki_tid;
|
2007-12-02 23:31:45 +00:00
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
kinfo_proc_sort(struct kinfo_proc *kipp, int count)
|
|
|
|
{
|
|
|
|
|
|
|
|
qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare);
|
|
|
|
}
|
|
|
|
|
2016-12-07 15:04:22 +00:00
|
|
|
const char *
|
|
|
|
kinfo_proc_thread_name(const struct kinfo_proc *kipp)
|
|
|
|
{
|
|
|
|
static char name[MAXCOMLEN+1];
|
|
|
|
|
|
|
|
strlcpy(name, kipp->ki_tdname, sizeof(name));
|
|
|
|
strlcat(name, kipp->ki_moretdname, sizeof(name));
|
|
|
|
if (name[0] == '\0' || strcmp(kipp->ki_comm, name) == 0) {
|
|
|
|
name[0] = '-';
|
|
|
|
name[1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return (name);
|
|
|
|
}
|
|
|
|
|
2020-09-18 12:59:27 +00:00
|
|
|
static const struct procstat_cmd *
|
|
|
|
getcmdbyprogname(const char *pprogname)
|
|
|
|
{
|
|
|
|
const char *ca;
|
|
|
|
size_t i, len;
|
|
|
|
|
|
|
|
if (pprogname == NULL)
|
|
|
|
return (NULL);
|
|
|
|
len = strlen(pprogname);
|
|
|
|
|
|
|
|
for (i = 0; i < nitems(pacmd_table); i++) {
|
|
|
|
ca = pacmd_table[i].command;
|
|
|
|
if (ca != NULL && strcmp(ca, pprogname) == 0)
|
|
|
|
return (&pacmd_table[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2017-10-14 18:38:36 +00:00
|
|
|
static const struct procstat_cmd *
|
|
|
|
getcmd(const char *str)
|
|
|
|
{
|
|
|
|
const struct procstat_cmd *cmd;
|
|
|
|
size_t i, l;
|
|
|
|
int cmp, s;
|
|
|
|
|
|
|
|
if (str == NULL)
|
|
|
|
return (NULL);
|
|
|
|
cmd = NULL;
|
|
|
|
if ((l = strlen(str)) == 0)
|
|
|
|
return (getcmd("basic"));
|
|
|
|
s = l > 1 && strcasecmp(str + l - 1, "s") == 0;
|
|
|
|
for (i = 0; i < nitems(cmd_table); i++) {
|
|
|
|
/*
|
|
|
|
* After the first match substring matches are disabled,
|
|
|
|
* allowing subsequent full matches to take precedence.
|
|
|
|
*/
|
|
|
|
if (cmd == NULL && (cmd_table[i].cmp & PS_CMP_SUBSTR))
|
|
|
|
cmp = strncasecmp(str, cmd_table[i].command, l -
|
|
|
|
((cmd_table[i].cmp & PS_CMP_PLURAL) && s ? 1 : 0));
|
|
|
|
else if ((cmd_table[i].cmp & PS_CMP_PLURAL) && s &&
|
|
|
|
l == strlen(cmd_table[i].command) + 1)
|
|
|
|
cmp = strncasecmp(str, cmd_table[i].command, l - 1);
|
|
|
|
else
|
|
|
|
cmp = strcasecmp(str, cmd_table[i].command);
|
|
|
|
if (cmp == 0)
|
|
|
|
cmd = &cmd_table[i];
|
|
|
|
}
|
|
|
|
return (cmd);
|
|
|
|
}
|
|
|
|
|
2007-12-02 23:31:45 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2011-05-12 10:11:39 +00:00
|
|
|
struct kinfo_proc *p;
|
2017-10-14 18:38:36 +00:00
|
|
|
const struct procstat_cmd *cmd;
|
2013-04-20 08:22:09 +00:00
|
|
|
struct procstat *prstat, *cprstat;
|
2020-09-18 12:59:27 +00:00
|
|
|
char *dummy, *nlistf, *memf;
|
|
|
|
const char *xocontainer;
|
2007-12-02 23:31:45 +00:00
|
|
|
long l;
|
|
|
|
pid_t pid;
|
2020-09-18 12:59:27 +00:00
|
|
|
int aflag, ch, cnt, i, interval;
|
2007-12-02 23:31:45 +00:00
|
|
|
|
|
|
|
interval = 0;
|
2017-10-14 18:38:36 +00:00
|
|
|
cmd = NULL;
|
2011-05-12 10:11:39 +00:00
|
|
|
memf = nlistf = NULL;
|
2017-10-14 18:38:36 +00:00
|
|
|
aflag = 0;
|
2015-09-05 17:02:01 +00:00
|
|
|
argc = xo_parse_args(argc, argv);
|
|
|
|
|
2020-09-18 12:59:27 +00:00
|
|
|
progname = getprogname();
|
|
|
|
cmd = getcmdbyprogname(progname);
|
|
|
|
|
2017-05-26 21:10:01 +00:00
|
|
|
while ((ch = getopt(argc, argv, "abCcefHhijkLlM:N:nrSstvw:x")) != -1) {
|
2007-12-02 23:31:45 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'a':
|
|
|
|
aflag++;
|
|
|
|
break;
|
|
|
|
case 'b':
|
2017-10-14 18:38:36 +00:00
|
|
|
if (cmd != NULL)
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(cmd);
|
2017-10-14 18:38:36 +00:00
|
|
|
cmd = getcmd("binary");
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
procstat_opts |= PS_OPT_CAPABILITIES;
|
2007-12-02 23:31:45 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
2017-10-14 18:38:36 +00:00
|
|
|
if (cmd != NULL)
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(cmd);
|
2017-10-14 18:38:36 +00:00
|
|
|
cmd = getcmd("arguments");
|
2007-12-02 23:31:45 +00:00
|
|
|
break;
|
2011-11-22 20:59:52 +00:00
|
|
|
case 'e':
|
2017-10-14 18:38:36 +00:00
|
|
|
if (cmd != NULL)
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(cmd);
|
2017-10-14 18:38:36 +00:00
|
|
|
cmd = getcmd("environment");
|
2011-11-22 20:59:52 +00:00
|
|
|
break;
|
2007-12-02 23:31:45 +00:00
|
|
|
case 'f':
|
2017-10-14 18:38:36 +00:00
|
|
|
if (cmd != NULL)
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(cmd);
|
2017-10-14 18:38:36 +00:00
|
|
|
cmd = getcmd("files");
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
procstat_opts |= PS_OPT_PERTHREAD;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
procstat_opts |= PS_OPT_NOHEADER;
|
2007-12-02 23:31:45 +00:00
|
|
|
break;
|
2010-03-08 20:44:22 +00:00
|
|
|
case 'i':
|
2017-10-14 18:38:36 +00:00
|
|
|
if (cmd != NULL)
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(cmd);
|
2017-10-14 18:38:36 +00:00
|
|
|
cmd = getcmd("signals");
|
2010-03-08 20:44:22 +00:00
|
|
|
break;
|
|
|
|
case 'j':
|
2017-10-14 18:38:36 +00:00
|
|
|
if (cmd != NULL)
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(cmd);
|
2017-10-14 18:38:36 +00:00
|
|
|
cmd = getcmd("tsignals");
|
2010-03-08 20:44:22 +00:00
|
|
|
break;
|
2007-12-02 23:31:45 +00:00
|
|
|
case 'k':
|
2017-11-16 22:14:49 +00:00
|
|
|
if (cmd != NULL && cmd->cmd == procstat_kstack) {
|
2017-10-14 18:38:36 +00:00
|
|
|
if ((procstat_opts & PS_OPT_VERBOSE) != 0)
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(cmd);
|
2017-10-14 18:38:36 +00:00
|
|
|
procstat_opts |= PS_OPT_VERBOSE;
|
|
|
|
} else {
|
|
|
|
if (cmd != NULL)
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(cmd);
|
2017-10-14 18:38:36 +00:00
|
|
|
cmd = getcmd("kstack");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
if (cmd != NULL)
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(cmd);
|
2017-10-14 18:38:36 +00:00
|
|
|
cmd = getcmd("ptlwpinfo");
|
2007-12-02 23:31:45 +00:00
|
|
|
break;
|
2011-11-24 20:54:06 +00:00
|
|
|
case 'l':
|
2017-10-14 18:38:36 +00:00
|
|
|
if (cmd != NULL)
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(cmd);
|
2017-10-14 18:38:36 +00:00
|
|
|
cmd = getcmd("rlimit");
|
2011-11-24 20:54:06 +00:00
|
|
|
break;
|
2017-10-14 18:38:36 +00:00
|
|
|
case 'M':
|
|
|
|
memf = optarg;
|
2017-03-30 18:21:36 +00:00
|
|
|
break;
|
2017-10-14 18:38:36 +00:00
|
|
|
case 'N':
|
|
|
|
nlistf = optarg;
|
2010-03-08 20:44:22 +00:00
|
|
|
break;
|
2017-10-14 18:38:36 +00:00
|
|
|
case 'n':
|
|
|
|
procstat_opts |= PS_OPT_SIGNUM;
|
2007-12-02 23:31:45 +00:00
|
|
|
break;
|
2014-05-17 03:54:51 +00:00
|
|
|
case 'r':
|
2017-10-14 18:38:36 +00:00
|
|
|
if (cmd != NULL)
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(cmd);
|
2017-10-14 18:38:36 +00:00
|
|
|
cmd = getcmd("rusage");
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
if (cmd != NULL)
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(cmd);
|
2017-10-14 18:38:36 +00:00
|
|
|
cmd = getcmd("cpuset");
|
2014-05-17 03:54:51 +00:00
|
|
|
break;
|
2007-12-02 23:31:45 +00:00
|
|
|
case 's':
|
2017-10-14 18:38:36 +00:00
|
|
|
if (cmd != NULL)
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(cmd);
|
2017-10-14 18:38:36 +00:00
|
|
|
cmd = getcmd("credentials");
|
2007-12-02 23:31:45 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
2017-10-14 18:38:36 +00:00
|
|
|
if (cmd != NULL)
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(cmd);
|
2017-10-14 18:38:36 +00:00
|
|
|
cmd = getcmd("threads");
|
2007-12-02 23:31:45 +00:00
|
|
|
break;
|
|
|
|
case 'v':
|
2017-10-14 18:38:36 +00:00
|
|
|
if (cmd != NULL)
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(cmd);
|
2017-10-14 18:38:36 +00:00
|
|
|
cmd = getcmd("vm");
|
2007-12-02 23:31:45 +00:00
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
l = strtol(optarg, &dummy, 10);
|
|
|
|
if (*dummy != '\0')
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(cmd);
|
2007-12-02 23:31:45 +00:00
|
|
|
if (l < 1 || l > INT_MAX)
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(cmd);
|
2007-12-02 23:31:45 +00:00
|
|
|
interval = l;
|
|
|
|
break;
|
2011-11-22 20:59:52 +00:00
|
|
|
case 'x':
|
2017-10-14 18:38:36 +00:00
|
|
|
if (cmd != NULL)
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(cmd);
|
2017-10-14 18:38:36 +00:00
|
|
|
cmd = getcmd("auxv");
|
2011-11-22 20:59:52 +00:00
|
|
|
break;
|
2007-12-02 23:31:45 +00:00
|
|
|
case '?':
|
|
|
|
default:
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(cmd);
|
2007-12-02 23:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2020-09-29 00:20:58 +00:00
|
|
|
if (cmd == NULL && argv[0] != NULL && (cmd = getcmd(argv[0])) != NULL) {
|
2017-10-14 18:38:36 +00:00
|
|
|
if ((procstat_opts & PS_SUBCOMMAND_OPTS) != 0)
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(cmd);
|
2017-10-14 18:38:36 +00:00
|
|
|
if (cmd->opt != NULL) {
|
|
|
|
optreset = 1;
|
|
|
|
optind = 1;
|
|
|
|
cmd->opt(argc, argv);
|
2020-09-18 12:59:27 +00:00
|
|
|
if ((cmd->cmp & PS_MODE_COMPAT) == 0) {
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
}
|
2017-10-14 18:38:36 +00:00
|
|
|
} else {
|
|
|
|
argc -= 1;
|
|
|
|
argv += 1;
|
|
|
|
}
|
|
|
|
} else {
|
2020-09-29 00:20:58 +00:00
|
|
|
if (cmd == NULL)
|
|
|
|
cmd = getcmd("basic");
|
|
|
|
if (cmd->cmd != procstat_files &&
|
|
|
|
(procstat_opts & PS_OPT_CAPABILITIES) != 0 &&
|
|
|
|
(cmd->cmp & PS_MODE_COMPAT) == 0)
|
|
|
|
usage(cmd);
|
2017-10-14 18:38:36 +00:00
|
|
|
}
|
2007-12-02 23:31:45 +00:00
|
|
|
|
|
|
|
/* Must specify either the -a flag or a list of pids. */
|
|
|
|
if (!(aflag == 1 && argc == 0) && !(aflag == 0 && argc > 0))
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(cmd);
|
2007-12-02 23:31:45 +00:00
|
|
|
|
2011-05-12 10:11:39 +00:00
|
|
|
if (memf != NULL)
|
|
|
|
prstat = procstat_open_kvm(nlistf, memf);
|
|
|
|
else
|
|
|
|
prstat = procstat_open_sysctl();
|
|
|
|
if (prstat == NULL)
|
2015-09-05 17:02:01 +00:00
|
|
|
xo_errx(1, "procstat_open()");
|
2007-12-02 23:31:45 +00:00
|
|
|
do {
|
2020-09-18 12:59:27 +00:00
|
|
|
xocontainer = cmd->xocontainer != NULL ? cmd->xocontainer :
|
|
|
|
cmd->command;
|
2015-09-05 17:02:01 +00:00
|
|
|
xo_set_version(PROCSTAT_XO_VERSION);
|
2020-09-18 12:59:27 +00:00
|
|
|
xo_open_container(progname);
|
|
|
|
xo_open_container(xocontainer);
|
2015-09-05 17:02:01 +00:00
|
|
|
|
2007-12-02 23:31:45 +00:00
|
|
|
if (aflag) {
|
2011-05-12 10:11:39 +00:00
|
|
|
p = procstat_getprocs(prstat, KERN_PROC_PROC, 0, &cnt);
|
|
|
|
if (p == NULL)
|
2015-09-05 17:02:01 +00:00
|
|
|
xo_errx(1, "procstat_getprocs()");
|
2011-05-12 10:11:39 +00:00
|
|
|
kinfo_proc_sort(p, cnt);
|
|
|
|
for (i = 0; i < cnt; i++) {
|
2017-10-14 18:38:36 +00:00
|
|
|
procstat(cmd, prstat, &p[i]);
|
2007-12-02 23:31:45 +00:00
|
|
|
|
|
|
|
/* Suppress header after first process. */
|
2017-10-14 18:38:36 +00:00
|
|
|
procstat_opts |= PS_OPT_NOHEADER;
|
2015-09-05 17:02:01 +00:00
|
|
|
xo_flush();
|
2007-12-02 23:31:45 +00:00
|
|
|
}
|
2011-05-12 10:11:39 +00:00
|
|
|
procstat_freeprocs(prstat, p);
|
2007-12-02 23:31:45 +00:00
|
|
|
}
|
2011-05-12 10:11:39 +00:00
|
|
|
for (i = 0; i < argc; i++) {
|
2007-12-02 23:31:45 +00:00
|
|
|
l = strtol(argv[i], &dummy, 10);
|
2013-04-20 08:22:09 +00:00
|
|
|
if (*dummy == '\0') {
|
|
|
|
if (l < 0)
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(cmd);
|
2013-04-20 08:22:09 +00:00
|
|
|
pid = l;
|
|
|
|
|
2015-09-05 17:02:01 +00:00
|
|
|
p = procstat_getprocs(prstat, KERN_PROC_PID,
|
|
|
|
pid, &cnt);
|
2013-04-20 08:22:09 +00:00
|
|
|
if (p == NULL)
|
2015-09-05 17:02:01 +00:00
|
|
|
xo_errx(1, "procstat_getprocs()");
|
2013-04-20 08:22:09 +00:00
|
|
|
if (cnt != 0)
|
2017-10-14 18:38:36 +00:00
|
|
|
procstat(cmd, prstat, p);
|
2013-04-20 08:22:09 +00:00
|
|
|
procstat_freeprocs(prstat, p);
|
|
|
|
} else {
|
2020-09-18 12:59:27 +00:00
|
|
|
if ((cmd->cmp & PS_MODE_COMPAT) == 0) {
|
|
|
|
cprstat = procstat_open_core(argv[i]);
|
|
|
|
if (cprstat == NULL) {
|
|
|
|
warnx("procstat_open()");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
p = procstat_getprocs(cprstat,
|
|
|
|
KERN_PROC_PID, -1, &cnt);
|
|
|
|
if (p == NULL) {
|
|
|
|
xo_errx(1,
|
|
|
|
"procstat_getprocs()");
|
|
|
|
}
|
|
|
|
if (cnt != 0)
|
|
|
|
procstat(cmd, cprstat, p);
|
|
|
|
procstat_freeprocs(cprstat, p);
|
|
|
|
procstat_close(cprstat);
|
|
|
|
} else {
|
|
|
|
usage(cmd);
|
2013-04-20 08:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-18 12:59:27 +00:00
|
|
|
if ((cmd->cmp & PS_MODE_COMPAT) == 0) {
|
|
|
|
/* Suppress header after first process. */
|
|
|
|
procstat_opts |= PS_OPT_NOHEADER;
|
|
|
|
}
|
2007-12-02 23:31:45 +00:00
|
|
|
}
|
2015-09-05 17:02:01 +00:00
|
|
|
|
2020-09-18 12:59:27 +00:00
|
|
|
xo_close_container(xocontainer);
|
|
|
|
xo_close_container(progname);
|
2015-09-05 17:02:01 +00:00
|
|
|
xo_finish();
|
2007-12-02 23:31:45 +00:00
|
|
|
if (interval)
|
|
|
|
sleep(interval);
|
|
|
|
} while (interval);
|
2015-09-05 17:02:01 +00:00
|
|
|
|
2011-05-12 10:11:39 +00:00
|
|
|
procstat_close(prstat);
|
2015-09-05 17:02:01 +00:00
|
|
|
|
2007-12-02 23:31:45 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
2017-10-14 18:38:36 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
cmdopt_none(int argc, char * const argv[])
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "")) != -1) {
|
|
|
|
switch (ch) {
|
|
|
|
case '?':
|
|
|
|
default:
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(NULL);
|
2017-10-14 18:38:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cmdopt_verbose(int argc, char * const argv[])
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "v")) != -1) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'v':
|
|
|
|
procstat_opts |= PS_OPT_VERBOSE;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(NULL);
|
2017-10-14 18:38:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cmdopt_signals(int argc, char * const argv[])
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "n")) != -1) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'n':
|
|
|
|
procstat_opts |= PS_OPT_SIGNUM;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(NULL);
|
2017-10-14 18:38:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cmdopt_rusage(int argc, char * const argv[])
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "Ht")) != -1) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'H':
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 't':
|
|
|
|
procstat_opts |= PS_OPT_PERTHREAD;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(NULL);
|
2017-10-14 18:38:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cmdopt_files(int argc, char * const argv[])
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "C")) != -1) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'C':
|
|
|
|
procstat_opts |= PS_OPT_CAPABILITIES;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
2020-09-18 12:59:27 +00:00
|
|
|
usage(NULL);
|
2017-10-14 18:38:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cmdopt_cpuset(int argc, char * const argv[])
|
|
|
|
{
|
|
|
|
|
|
|
|
procstat_opts |= PS_OPT_PERTHREAD;
|
|
|
|
cmdopt_none(argc, argv);
|
|
|
|
}
|