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"
|
|
|
|
|
2010-03-08 20:44:22 +00:00
|
|
|
static int aflag, bflag, cflag, fflag, iflag, jflag, kflag, sflag, tflag, vflag;
|
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-05-12 10:11:39 +00:00
|
|
|
"[-w interval] [-b | -c | -f | -i | -j | -k | -s | -t | -v]\n");
|
2007-12-02 23:31:45 +00:00
|
|
|
fprintf(stderr, " [-a | pid ...]\n");
|
|
|
|
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)
|
2011-05-12 10:11:39 +00:00
|
|
|
procstat_bin(kipp);
|
2007-12-02 23:31:45 +00:00
|
|
|
else if (cflag)
|
2011-05-12 10:11:39 +00:00
|
|
|
procstat_args(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)
|
2011-05-12 10:11:39 +00:00
|
|
|
procstat_kstack(kipp, kflag);
|
2007-12-02 23:31:45 +00:00
|
|
|
else if (sflag)
|
2011-05-12 10:11:39 +00:00
|
|
|
procstat_cred(kipp);
|
2007-12-02 23:31:45 +00:00
|
|
|
else if (tflag)
|
2011-05-12 10:11:39 +00:00
|
|
|
procstat_threads(kipp);
|
2007-12-02 23:31:45 +00:00
|
|
|
else if (vflag)
|
2011-05-12 10:11:39 +00:00
|
|
|
procstat_vm(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;
|
|
|
|
struct procstat *prstat;
|
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-08-14 00:42:09 +00:00
|
|
|
while ((ch = getopt(argc, argv, "CN:M:abcfijkhstvw:")) != -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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
/* We require that either 0 or 1 mode flags be set. */
|
|
|
|
tmp = bflag + cflag + fflag + (kflag ? 1 : 0) + sflag + tflag + vflag;
|
|
|
|
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);
|
|
|
|
if (*dummy != '\0')
|
|
|
|
usage();
|
|
|
|
if (l < 0)
|
|
|
|
usage();
|
|
|
|
pid = l;
|
|
|
|
|
2011-05-12 10:11:39 +00:00
|
|
|
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);
|
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);
|
|
|
|
}
|