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-03-30 18:21:36 +00:00
|
|
|
static int aflag, bflag, cflag, eflag, fflag, iflag, jflag, kflag;
|
|
|
|
static int lflag, Lflag, rflag, sflag, tflag, vflag, xflag, Sflag;
|
2014-05-17 03:54:51 +00:00
|
|
|
int hflag, nflag, Cflag, Hflag;
|
2007-12-02 23:31:45 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
|
2015-09-05 17:02:01 +00:00
|
|
|
xo_error("usage: procstat [-CHhn] [-M core] [-N system] "
|
|
|
|
"[-w interval]\n"
|
|
|
|
" [-b | -c | -e | -f | -i | -j | -k | "
|
|
|
|
"-l | -r | -s | -S | -t | -v | -x]\n"
|
|
|
|
" [-a | pid | core ...]\n");
|
|
|
|
xo_finish();
|
2007-12-02 23:31:45 +00:00
|
|
|
exit(EX_USAGE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-05-12 10:11:39 +00:00
|
|
|
procstat(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);
|
2007-12-02 23:31:45 +00:00
|
|
|
|
|
|
|
if (bflag)
|
2013-04-20 08:05:04 +00:00
|
|
|
procstat_bin(prstat, kipp);
|
2007-12-02 23:31:45 +00:00
|
|
|
else if (cflag)
|
2013-04-20 08:08:29 +00:00
|
|
|
procstat_args(prstat, kipp);
|
2011-11-22 20:59:52 +00:00
|
|
|
else if (eflag)
|
2013-04-20 08:08:29 +00:00
|
|
|
procstat_env(prstat, kipp);
|
2007-12-02 23:31:45 +00:00
|
|
|
else if (fflag)
|
2011-05-12 10:11:39 +00:00
|
|
|
procstat_files(prstat, kipp);
|
2010-03-08 20:44:22 +00:00
|
|
|
else if (iflag)
|
2011-05-12 10:11:39 +00:00
|
|
|
procstat_sigs(prstat, kipp);
|
2010-03-08 20:44:22 +00:00
|
|
|
else if (jflag)
|
2011-05-12 10:11:39 +00:00
|
|
|
procstat_threads_sigs(prstat, kipp);
|
2007-12-02 23:31:45 +00:00
|
|
|
else if (kflag)
|
2013-04-20 08:19:06 +00:00
|
|
|
procstat_kstack(prstat, kipp, kflag);
|
2011-11-24 20:54:06 +00:00
|
|
|
else if (lflag)
|
2013-04-20 08:01:00 +00:00
|
|
|
procstat_rlimit(prstat, kipp);
|
2017-03-30 18:21:36 +00:00
|
|
|
else if (Lflag)
|
|
|
|
procstat_ptlwpinfo(prstat);
|
2014-05-17 03:54:51 +00:00
|
|
|
else if (rflag)
|
|
|
|
procstat_rusage(prstat, kipp);
|
2007-12-02 23:31:45 +00:00
|
|
|
else if (sflag)
|
2013-04-20 07:55:31 +00:00
|
|
|
procstat_cred(prstat, kipp);
|
2007-12-02 23:31:45 +00:00
|
|
|
else if (tflag)
|
2013-04-20 07:50:59 +00:00
|
|
|
procstat_threads(prstat, kipp);
|
2007-12-02 23:31:45 +00:00
|
|
|
else if (vflag)
|
2013-04-20 07:52:23 +00:00
|
|
|
procstat_vm(prstat, kipp);
|
2011-11-22 20:59:52 +00:00
|
|
|
else if (xflag)
|
2013-04-20 08:15:43 +00:00
|
|
|
procstat_auxv(prstat, kipp);
|
2015-03-10 09:41:48 +00:00
|
|
|
else if (Sflag)
|
|
|
|
procstat_cs(prstat, kipp);
|
2007-12-02 23:31:45 +00:00
|
|
|
else
|
2011-05-12 10:11:39 +00:00
|
|
|
procstat_basic(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);
|
|
|
|
}
|
|
|
|
|
2007-12-02 23:31:45 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2011-05-12 10:11:39 +00:00
|
|
|
int ch, interval, tmp;
|
|
|
|
int i;
|
|
|
|
struct kinfo_proc *p;
|
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;
|
2015-09-05 17:02:01 +00:00
|
|
|
const char *xocontainer;
|
2011-05-12 10:11:39 +00:00
|
|
|
int cnt;
|
2007-12-02 23:31:45 +00:00
|
|
|
|
|
|
|
interval = 0;
|
2011-05-12 10:11:39 +00:00
|
|
|
memf = nlistf = NULL;
|
2015-09-05 17:02:01 +00:00
|
|
|
argc = xo_parse_args(argc, argv);
|
|
|
|
xocontainer = "basic";
|
|
|
|
|
2017-03-30 18:21:36 +00:00
|
|
|
while ((ch = getopt(argc, argv, "CHN:M:abcefijklLhrsStvw:x")) != -1) {
|
2007-12-02 23:31:45 +00:00
|
|
|
switch (ch) {
|
2011-08-14 00:42:09 +00:00
|
|
|
case 'C':
|
|
|
|
Cflag++;
|
|
|
|
break;
|
|
|
|
|
2014-05-17 03:54:51 +00:00
|
|
|
case 'H':
|
|
|
|
Hflag++;
|
|
|
|
break;
|
|
|
|
|
2011-05-12 10:11:39 +00:00
|
|
|
case 'M':
|
|
|
|
memf = optarg;
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
nlistf = optarg;
|
|
|
|
break;
|
2015-03-10 09:41:48 +00:00
|
|
|
case 'S':
|
|
|
|
Sflag++;
|
2015-09-05 17:02:01 +00:00
|
|
|
xocontainer = "cs";
|
2015-03-10 09:41:48 +00:00
|
|
|
break;
|
2007-12-02 23:31:45 +00:00
|
|
|
case 'a':
|
|
|
|
aflag++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b':
|
|
|
|
bflag++;
|
2015-09-05 17:02:01 +00:00
|
|
|
xocontainer = "binary";
|
2007-12-02 23:31:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
cflag++;
|
2015-09-05 17:02:01 +00:00
|
|
|
xocontainer = "arguments";
|
2007-12-02 23:31:45 +00:00
|
|
|
break;
|
|
|
|
|
2011-11-22 20:59:52 +00:00
|
|
|
case 'e':
|
|
|
|
eflag++;
|
2015-09-05 17:02:01 +00:00
|
|
|
xocontainer = "environment";
|
2011-11-22 20:59:52 +00:00
|
|
|
break;
|
|
|
|
|
2007-12-02 23:31:45 +00:00
|
|
|
case 'f':
|
|
|
|
fflag++;
|
2015-09-05 17:02:01 +00:00
|
|
|
xocontainer = "files";
|
2007-12-02 23:31:45 +00:00
|
|
|
break;
|
|
|
|
|
2010-03-08 20:44:22 +00:00
|
|
|
case 'i':
|
|
|
|
iflag++;
|
2015-09-05 17:02:01 +00:00
|
|
|
xocontainer = "signals";
|
2010-03-08 20:44:22 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'j':
|
|
|
|
jflag++;
|
2015-09-05 17:02:01 +00:00
|
|
|
xocontainer = "thread_signals";
|
2010-03-08 20:44:22 +00:00
|
|
|
break;
|
|
|
|
|
2007-12-02 23:31:45 +00:00
|
|
|
case 'k':
|
|
|
|
kflag++;
|
2015-09-05 17:02:01 +00:00
|
|
|
xocontainer = "kstack";
|
2007-12-02 23:31:45 +00:00
|
|
|
break;
|
|
|
|
|
2011-11-24 20:54:06 +00:00
|
|
|
case 'l':
|
|
|
|
lflag++;
|
2015-09-05 17:02:01 +00:00
|
|
|
xocontainer = "rlimit";
|
2011-11-24 20:54:06 +00:00
|
|
|
break;
|
|
|
|
|
2017-03-30 18:21:36 +00:00
|
|
|
case 'L':
|
|
|
|
Lflag++;
|
|
|
|
xocontainer = "ptlwpinfo";
|
|
|
|
break;
|
|
|
|
|
2010-03-08 20:44:22 +00:00
|
|
|
case 'n':
|
|
|
|
nflag++;
|
|
|
|
break;
|
|
|
|
|
2007-12-02 23:31:45 +00:00
|
|
|
case 'h':
|
|
|
|
hflag++;
|
|
|
|
break;
|
|
|
|
|
2014-05-17 03:54:51 +00:00
|
|
|
case 'r':
|
|
|
|
rflag++;
|
2015-09-05 17:02:01 +00:00
|
|
|
xocontainer = "rusage";
|
2014-05-17 03:54:51 +00:00
|
|
|
break;
|
|
|
|
|
2007-12-02 23:31:45 +00:00
|
|
|
case 's':
|
|
|
|
sflag++;
|
2015-09-05 17:02:01 +00:00
|
|
|
xocontainer = "credentials";
|
2007-12-02 23:31:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
tflag++;
|
2015-09-05 17:02:01 +00:00
|
|
|
xocontainer = "threads";
|
2007-12-02 23:31:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
vflag++;
|
2015-09-05 17:02:01 +00:00
|
|
|
xocontainer = "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':
|
|
|
|
xflag++;
|
2015-09-05 17:02:01 +00:00
|
|
|
xocontainer = "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;
|
|
|
|
|
|
|
|
/* We require that either 0 or 1 mode flags be set. */
|
2013-01-12 22:20:37 +00:00
|
|
|
tmp = bflag + cflag + eflag + fflag + iflag + jflag + (kflag ? 1 : 0) +
|
2015-03-10 09:41:48 +00:00
|
|
|
lflag + rflag + sflag + tflag + vflag + xflag + Sflag;
|
2007-12-02 23:31:45 +00:00
|
|
|
if (!(tmp == 0 || tmp == 1))
|
|
|
|
usage();
|
|
|
|
|
|
|
|
/* We allow -k to be specified up to twice, but not more. */
|
|
|
|
if (kflag > 2)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
/* Must specify either the -a flag or a list of pids. */
|
|
|
|
if (!(aflag == 1 && argc == 0) && !(aflag == 0 && argc > 0))
|
|
|
|
usage();
|
|
|
|
|
2011-08-14 00:42:09 +00:00
|
|
|
/* Only allow -C with -f. */
|
|
|
|
if (Cflag && !fflag)
|
|
|
|
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");
|
|
|
|
xo_open_container(xocontainer);
|
|
|
|
|
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++) {
|
|
|
|
procstat(prstat, &p[i]);
|
2007-12-02 23:31:45 +00:00
|
|
|
|
|
|
|
/* Suppress header after first process. */
|
|
|
|
hflag = 1;
|
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)
|
|
|
|
procstat(prstat, p);
|
|
|
|
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)
|
|
|
|
procstat(cprstat, p);
|
|
|
|
procstat_freeprocs(cprstat, p);
|
|
|
|
procstat_close(cprstat);
|
|
|
|
}
|
2007-12-02 23:31:45 +00:00
|
|
|
/* Suppress header after first process. */
|
|
|
|
hflag = 1;
|
|
|
|
}
|
2015-09-05 17:02:01 +00:00
|
|
|
|
|
|
|
xo_close_container(xocontainer);
|
|
|
|
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);
|
|
|
|
}
|