2007-12-02 23:31:45 +00:00
|
|
|
/*-
|
2011-08-14 00:42:09 +00:00
|
|
|
* Copyright (c) 2007, 2011 Robert N. M. Watson
|
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>
|
|
|
|
#include <sysexits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "procstat.h"
|
|
|
|
|
2011-11-24 20:54:06 +00:00
|
|
|
static int aflag, bflag, cflag, eflag, fflag, iflag, jflag, kflag, lflag, sflag;
|
|
|
|
static int tflag, vflag, xflag;
|
2011-08-14 00:42:09 +00:00
|
|
|
int hflag, nflag, Cflag;
|
2007-12-02 23:31:45 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
|
2011-08-14 00:42:09 +00:00
|
|
|
fprintf(stderr, "usage: procstat [-h] [-C] [-M core] [-N system] "
|
2011-11-22 20:59:52 +00:00
|
|
|
"[-w interval] \n");
|
|
|
|
fprintf(stderr, " [-b | -c | -e | -f | -i | -j | -k | "
|
2013-04-20 08:22:09 +00:00
|
|
|
"-l | -s | -t | -v | -x] [-a | pid | core ...]\n");
|
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
|
|
|
{
|
|
|
|
|
|
|
|
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);
|
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);
|
2007-12-02 23:31:45 +00:00
|
|
|
else
|
2011-05-12 10:11:39 +00:00
|
|
|
procstat_basic(kipp);
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
int cnt;
|
2007-12-02 23:31:45 +00:00
|
|
|
|
|
|
|
interval = 0;
|
2011-05-12 10:11:39 +00:00
|
|
|
memf = nlistf = NULL;
|
2011-11-24 20:54:06 +00:00
|
|
|
while ((ch = getopt(argc, argv, "CN:M:abcefijklhstvw:x")) != -1) {
|
2007-12-02 23:31:45 +00:00
|
|
|
switch (ch) {
|
2011-08-14 00:42:09 +00:00
|
|
|
case 'C':
|
|
|
|
Cflag++;
|
|
|
|
break;
|
|
|
|
|
2011-05-12 10:11:39 +00:00
|
|
|
case 'M':
|
|
|
|
memf = optarg;
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
nlistf = optarg;
|
|
|
|
break;
|
2007-12-02 23:31:45 +00:00
|
|
|
case 'a':
|
|
|
|
aflag++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b':
|
|
|
|
bflag++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
cflag++;
|
|
|
|
break;
|
|
|
|
|
2011-11-22 20:59:52 +00:00
|
|
|
case 'e':
|
|
|
|
eflag++;
|
|
|
|
break;
|
|
|
|
|
2007-12-02 23:31:45 +00:00
|
|
|
case 'f':
|
|
|
|
fflag++;
|
|
|
|
break;
|
|
|
|
|
2010-03-08 20:44:22 +00:00
|
|
|
case 'i':
|
|
|
|
iflag++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'j':
|
|
|
|
jflag++;
|
|
|
|
break;
|
|
|
|
|
2007-12-02 23:31:45 +00:00
|
|
|
case 'k':
|
|
|
|
kflag++;
|
|
|
|
break;
|
|
|
|
|
2011-11-24 20:54:06 +00:00
|
|
|
case 'l':
|
|
|
|
lflag++;
|
|
|
|
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;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
sflag++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
tflag++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
vflag++;
|
|
|
|
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++;
|
|
|
|
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) +
|
|
|
|
lflag + sflag + tflag + vflag + xflag;
|
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)
|
|
|
|
errx(1, "procstat_open()");
|
2007-12-02 23:31:45 +00:00
|
|
|
do {
|
|
|
|
if (aflag) {
|
2011-05-12 10:11:39 +00:00
|
|
|
p = procstat_getprocs(prstat, KERN_PROC_PROC, 0, &cnt);
|
|
|
|
if (p == NULL)
|
|
|
|
errx(1, "procstat_getprocs()");
|
|
|
|
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;
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
p = procstat_getprocs(prstat, KERN_PROC_PID, pid, &cnt);
|
|
|
|
if (p == NULL)
|
|
|
|
errx(1, "procstat_getprocs()");
|
|
|
|
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)
|
|
|
|
errx(1, "procstat_getprocs()");
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (interval)
|
|
|
|
sleep(interval);
|
|
|
|
} while (interval);
|
2011-05-12 10:11:39 +00:00
|
|
|
procstat_close(prstat);
|
2007-12-02 23:31:45 +00:00
|
|
|
exit(0);
|
|
|
|
}
|