2007-12-02 23:31:45 +00:00
|
|
|
/*-
|
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.
|
|
|
|
*
|
|
|
|
* $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[]);
|
|
|
|
|
|
|
|
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 },
|
|
|
|
{ "ptlwpinfo", "ptlwpinfo", NULL, &procstat_ptlwpinfo, &cmdopt_none,
|
|
|
|
PS_CMP_NORMAL },
|
|
|
|
{ "rlimit", "rlimit", NULL, &procstat_rlimit, &cmdopt_none,
|
|
|
|
PS_CMP_NORMAL },
|
|
|
|
{ "rusage", "rusage", "[-Ht]", &procstat_rusage, &cmdopt_rusage,
|
|
|
|
PS_CMP_NORMAL },
|
|
|
|
{ "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
|
|
|
|
usage(void)
|
|
|
|
{
|
2017-10-14 18:38:36 +00:00
|
|
|
size_t i, l;
|
|
|
|
int multi;
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
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[])
|
|
|
|
{
|
2017-10-14 18:38:36 +00:00
|
|
|
int ch, interval;
|
2011-05-12 10:11:39 +00:00
|
|
|
int i;
|
|
|
|
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;
|
2007-12-02 23:31:45 +00:00
|
|
|
long l;
|
|
|
|
pid_t pid;
|
|
|
|
char *dummy;
|
2011-05-12 10:11:39 +00:00
|
|
|
char *nlistf, *memf;
|
2017-10-14 18:38:36 +00:00
|
|
|
int aflag;
|
2011-05-12 10:11:39 +00:00
|
|
|
int cnt;
|
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);
|
|
|
|
|
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)
|
|
|
|
usage();
|
|
|
|
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)
|
|
|
|
usage();
|
|
|
|
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)
|
|
|
|
usage();
|
|
|
|
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)
|
|
|
|
usage();
|
|
|
|
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)
|
|
|
|
usage();
|
|
|
|
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)
|
|
|
|
usage();
|
|
|
|
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)
|
|
|
|
usage();
|
|
|
|
procstat_opts |= PS_OPT_VERBOSE;
|
|
|
|
} else {
|
|
|
|
if (cmd != NULL)
|
|
|
|
usage();
|
|
|
|
cmd = getcmd("kstack");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
if (cmd != NULL)
|
|
|
|
usage();
|
|
|
|
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)
|
|
|
|
usage();
|
|
|
|
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)
|
|
|
|
usage();
|
|
|
|
cmd = getcmd("rusage");
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
if (cmd != NULL)
|
|
|
|
usage();
|
|
|
|
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)
|
|
|
|
usage();
|
|
|
|
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)
|
|
|
|
usage();
|
|
|
|
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)
|
|
|
|
usage();
|
|
|
|
cmd = getcmd("vm");
|
2007-12-02 23:31:45 +00:00
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
l = strtol(optarg, &dummy, 10);
|
|
|
|
if (*dummy != '\0')
|
|
|
|
usage();
|
|
|
|
if (l < 1 || l > INT_MAX)
|
|
|
|
usage();
|
|
|
|
interval = l;
|
|
|
|
break;
|
2011-11-22 20:59:52 +00:00
|
|
|
case 'x':
|
2017-10-14 18:38:36 +00:00
|
|
|
if (cmd != NULL)
|
|
|
|
usage();
|
|
|
|
cmd = getcmd("auxv");
|
2011-11-22 20:59:52 +00:00
|
|
|
break;
|
2007-12-02 23:31:45 +00:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2017-10-14 18:38:36 +00:00
|
|
|
if (cmd == NULL && argv[0] != NULL && (cmd = getcmd(argv[0])) != NULL) {
|
|
|
|
if ((procstat_opts & PS_SUBCOMMAND_OPTS) != 0)
|
|
|
|
usage();
|
|
|
|
if (cmd->opt != NULL) {
|
|
|
|
optreset = 1;
|
|
|
|
optind = 1;
|
|
|
|
cmd->opt(argc, argv);
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
} else {
|
|
|
|
argc -= 1;
|
|
|
|
argv += 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (cmd == NULL)
|
|
|
|
cmd = getcmd("basic");
|
|
|
|
if (cmd->cmd != procstat_files &&
|
|
|
|
(procstat_opts & PS_OPT_CAPABILITIES) != 0)
|
|
|
|
usage();
|
|
|
|
}
|
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))
|
|
|
|
usage();
|
|
|
|
|
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 {
|
2015-09-05 17:02:01 +00:00
|
|
|
xo_set_version(PROCSTAT_XO_VERSION);
|
|
|
|
xo_open_container("procstat");
|
2017-10-14 18:38:36 +00:00
|
|
|
xo_open_container(cmd->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)
|
|
|
|
usage();
|
|
|
|
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 {
|
|
|
|
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)
|
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, cprstat, p);
|
2013-04-20 08:22:09 +00:00
|
|
|
procstat_freeprocs(cprstat, p);
|
|
|
|
procstat_close(cprstat);
|
|
|
|
}
|
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;
|
2007-12-02 23:31:45 +00:00
|
|
|
}
|
2015-09-05 17:02:01 +00:00
|
|
|
|
2017-10-14 18:38:36 +00:00
|
|
|
xo_close_container(cmd->xocontainer);
|
2015-09-05 17:02:01 +00:00
|
|
|
xo_close_container("procstat");
|
|
|
|
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:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cmdopt_cpuset(int argc, char * const argv[])
|
|
|
|
{
|
|
|
|
|
|
|
|
procstat_opts |= PS_OPT_PERTHREAD;
|
|
|
|
cmdopt_none(argc, argv);
|
|
|
|
}
|