Use err(3). Add usage() and prototypes.
This commit is contained in:
parent
ecdf56e7d9
commit
fa6990870e
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=29736
@ -46,7 +46,7 @@
|
||||
.Nm Kgmon
|
||||
is a tool used when profiling the operating system.
|
||||
When no arguments are supplied,
|
||||
.Nm kgmon
|
||||
.Nm
|
||||
indicates the state of operating system profiling as running,
|
||||
off, or not configured.
|
||||
(see
|
||||
@ -54,7 +54,7 @@ off, or not configured.
|
||||
If the
|
||||
.Fl p
|
||||
flag is specified,
|
||||
.Nm kgmon
|
||||
.Nm
|
||||
extracts profile data from the operating system and produces a
|
||||
.Pa gmon.out
|
||||
file suitable for later analysis by
|
||||
|
@ -32,13 +32,17 @@
|
||||
*/
|
||||
|
||||
#ifndef lint
|
||||
static char copyright[] =
|
||||
static const char copyright[] =
|
||||
"@(#) Copyright (c) 1983, 1992, 1993\n\
|
||||
The Regents of the University of California. All rights reserved.\n";
|
||||
#endif /* not lint */
|
||||
|
||||
#ifndef lint
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)kgmon.c 8.1 (Berkeley) 6/6/93";
|
||||
#endif
|
||||
static const char rcsid[] =
|
||||
"$Id$";
|
||||
#endif /* not lint */
|
||||
|
||||
#include <sys/param.h>
|
||||
@ -46,22 +50,24 @@ static char sccsid[] = "@(#)kgmon.c 8.1 (Berkeley) 6/6/93";
|
||||
#include <sys/time.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <sys/gmon.h>
|
||||
#include <ctype.h>
|
||||
#include <err.h>
|
||||
#include <errno.h>
|
||||
#include <kvm.h>
|
||||
#include <limits.h>
|
||||
#include <nlist.h>
|
||||
#include <paths.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <nlist.h>
|
||||
#include <ctype.h>
|
||||
#include <paths.h>
|
||||
#include <unistd.h>
|
||||
|
||||
struct nlist nl[] = {
|
||||
#define N_GMONPARAM 0
|
||||
{ "__gmonparam" },
|
||||
#define N_PROFHZ 1
|
||||
{ "_profhz" },
|
||||
0,
|
||||
{ NULL },
|
||||
};
|
||||
|
||||
struct kvmvars {
|
||||
@ -71,15 +77,18 @@ struct kvmvars {
|
||||
|
||||
int Bflag, bflag, hflag, kflag, rflag, pflag;
|
||||
int debug = 0;
|
||||
int getprof __P((struct kvmvars *));
|
||||
int getprofhz __P((struct kvmvars *));
|
||||
void kern_readonly __P((int));
|
||||
int openfiles __P((char *, char *, struct kvmvars *));
|
||||
void setprof __P((struct kvmvars *kvp, int state));
|
||||
void dumpstate __P((struct kvmvars *kvp));
|
||||
void reset __P((struct kvmvars *kvp));
|
||||
static void usage __P((void));
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
extern char *optarg;
|
||||
extern int optind;
|
||||
int ch, mode, disp, accessmode;
|
||||
struct kvmvars kvmvars;
|
||||
char *system, *kmemf;
|
||||
@ -120,9 +129,7 @@ main(int argc, char **argv)
|
||||
break;
|
||||
|
||||
default:
|
||||
(void)fprintf(stderr,
|
||||
"usage: kgmon [-Bbhrp] [-M core] [-N system]\n");
|
||||
exit(1);
|
||||
usage();
|
||||
}
|
||||
}
|
||||
argc -= optind;
|
||||
@ -166,9 +173,17 @@ main(int argc, char **argv)
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void
|
||||
usage()
|
||||
{
|
||||
fprintf(stderr, "usage: kgmon [-Bbhrp] [-M core] [-N system]\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/*
|
||||
* Check that profiling is enabled and open any ncessary files.
|
||||
*/
|
||||
int
|
||||
openfiles(system, kmemf, kvp)
|
||||
char *system;
|
||||
char *kmemf;
|
||||
@ -182,11 +197,8 @@ openfiles(system, kmemf, kvp)
|
||||
mib[1] = KERN_PROF;
|
||||
mib[2] = GPROF_STATE;
|
||||
size = sizeof state;
|
||||
if (sysctl(mib, 3, &state, &size, NULL, 0) < 0) {
|
||||
(void)fprintf(stderr,
|
||||
"kgmon: profiling not defined in kernel.\n");
|
||||
exit(20);
|
||||
}
|
||||
if (sysctl(mib, 3, &state, &size, NULL, 0) < 0)
|
||||
errx(20, "profiling not defined in kernel");
|
||||
if (!(Bflag || bflag || hflag || rflag ||
|
||||
(pflag &&
|
||||
(state == GMON_PROF_HIRES || state == GMON_PROF_ON))))
|
||||
@ -207,28 +219,21 @@ openfiles(system, kmemf, kvp)
|
||||
kvp->kd = kvm_openfiles(system, kmemf, NULL, O_RDONLY,
|
||||
errbuf);
|
||||
}
|
||||
if (kvp->kd == NULL) {
|
||||
(void)fprintf(stderr, "kgmon: kvm_openfiles: %s\n",
|
||||
errbuf);
|
||||
exit(2);
|
||||
}
|
||||
if (kvp->kd == NULL)
|
||||
errx(2, "kvm_openfiles: %s", errbuf);
|
||||
kern_readonly(GMON_PROF_ON);
|
||||
}
|
||||
if (kvm_nlist(kvp->kd, nl) < 0) {
|
||||
(void)fprintf(stderr, "kgmon: %s: no namelist\n", system);
|
||||
exit(3);
|
||||
}
|
||||
if (!nl[N_GMONPARAM].n_value) {
|
||||
(void)fprintf(stderr,
|
||||
"kgmon: profiling not defined in kernel.\n");
|
||||
exit(20);
|
||||
}
|
||||
if (kvm_nlist(kvp->kd, nl) < 0)
|
||||
errx(3, "%s: no namelist", system);
|
||||
if (!nl[N_GMONPARAM].n_value)
|
||||
errx(20, "profiling not defined in kernel");
|
||||
return (openmode);
|
||||
}
|
||||
|
||||
/*
|
||||
* Suppress options that require a writable kernel.
|
||||
*/
|
||||
void
|
||||
kern_readonly(mode)
|
||||
int mode;
|
||||
{
|
||||
@ -250,6 +255,7 @@ kern_readonly(mode)
|
||||
/*
|
||||
* Get the state of kernel profiling.
|
||||
*/
|
||||
int
|
||||
getprof(kvp)
|
||||
struct kvmvars *kvp;
|
||||
{
|
||||
@ -266,11 +272,9 @@ getprof(kvp)
|
||||
if (sysctl(mib, 3, &kvp->gpm, &size, NULL, 0) < 0)
|
||||
size = 0;
|
||||
}
|
||||
if (size != sizeof kvp->gpm) {
|
||||
(void)fprintf(stderr, "kgmon: cannot get gmonparam: %s\n",
|
||||
if (size != sizeof kvp->gpm)
|
||||
errx(4, "cannot get gmonparam: %s",
|
||||
kflag ? kvm_geterr(kvp->kd) : strerror(errno));
|
||||
exit (4);
|
||||
}
|
||||
return (kvp->gpm.state);
|
||||
}
|
||||
|
||||
@ -304,7 +308,7 @@ setprof(kvp, state)
|
||||
== sz)
|
||||
return;
|
||||
bad:
|
||||
(void)fprintf(stderr, "kgmon: warning: cannot turn profiling %s\n",
|
||||
warnx("warning: cannot turn profiling %s",
|
||||
state == GMON_PROF_OFF ? "off" : "on");
|
||||
}
|
||||
|
||||
@ -318,7 +322,7 @@ dumpstate(kvp)
|
||||
register FILE *fp;
|
||||
struct rawarc rawarc;
|
||||
struct tostruct *tos;
|
||||
u_long frompc, addr;
|
||||
u_long frompc;
|
||||
u_short *froms, *tickbuf;
|
||||
int mib[3], i;
|
||||
struct gmonhdr h;
|
||||
@ -327,7 +331,7 @@ dumpstate(kvp)
|
||||
setprof(kvp, GMON_PROF_OFF);
|
||||
fp = fopen("gmon.out", "w");
|
||||
if (fp == 0) {
|
||||
perror("gmon.out");
|
||||
warn("gmon.out");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -349,10 +353,8 @@ dumpstate(kvp)
|
||||
*/
|
||||
mib[0] = CTL_KERN;
|
||||
mib[1] = KERN_PROF;
|
||||
if ((tickbuf = (u_short *)malloc(kvp->gpm.kcountsize)) == NULL) {
|
||||
fprintf(stderr, "kgmon: cannot allocate kcount space\n");
|
||||
exit (5);
|
||||
}
|
||||
if ((tickbuf = (u_short *)malloc(kvp->gpm.kcountsize)) == NULL)
|
||||
errx(5, "cannot allocate kcount space");
|
||||
if (kflag) {
|
||||
i = kvm_read(kvp->kd, (u_long)kvp->gpm.kcount, (void *)tickbuf,
|
||||
kvp->gpm.kcountsize);
|
||||
@ -362,25 +364,19 @@ dumpstate(kvp)
|
||||
if (sysctl(mib, 3, tickbuf, &i, NULL, 0) < 0)
|
||||
i = 0;
|
||||
}
|
||||
if (i != kvp->gpm.kcountsize) {
|
||||
(void)fprintf(stderr, "kgmon: read ticks: read %u, got %d: %s",
|
||||
if (i != kvp->gpm.kcountsize)
|
||||
errx(6, "read ticks: read %u, got %d: %s",
|
||||
kvp->gpm.kcountsize, i,
|
||||
kflag ? kvm_geterr(kvp->kd) : strerror(errno));
|
||||
exit(6);
|
||||
}
|
||||
if ((fwrite(tickbuf, kvp->gpm.kcountsize, 1, fp)) != 1) {
|
||||
perror("kgmon: writing tocks to gmon.out");
|
||||
exit(7);
|
||||
}
|
||||
if ((fwrite(tickbuf, kvp->gpm.kcountsize, 1, fp)) != 1)
|
||||
err(7, "writing tocks to gmon.out");
|
||||
free(tickbuf);
|
||||
|
||||
/*
|
||||
* Write out the arc info.
|
||||
*/
|
||||
if ((froms = (u_short *)malloc(kvp->gpm.fromssize)) == NULL) {
|
||||
fprintf(stderr, "kgmon: cannot allocate froms space\n");
|
||||
exit (8);
|
||||
}
|
||||
if ((froms = (u_short *)malloc(kvp->gpm.fromssize)) == NULL)
|
||||
errx(8, "cannot allocate froms space");
|
||||
if (kflag) {
|
||||
i = kvm_read(kvp->kd, (u_long)kvp->gpm.froms, (void *)froms,
|
||||
kvp->gpm.fromssize);
|
||||
@ -390,16 +386,12 @@ dumpstate(kvp)
|
||||
if (sysctl(mib, 3, froms, &i, NULL, 0) < 0)
|
||||
i = 0;
|
||||
}
|
||||
if (i != kvp->gpm.fromssize) {
|
||||
(void)fprintf(stderr, "kgmon: read froms: read %u, got %d: %s",
|
||||
if (i != kvp->gpm.fromssize)
|
||||
errx(9, "read froms: read %u, got %d: %s",
|
||||
kvp->gpm.fromssize, i,
|
||||
kflag ? kvm_geterr(kvp->kd) : strerror(errno));
|
||||
exit(9);
|
||||
}
|
||||
if ((tos = (struct tostruct *)malloc(kvp->gpm.tossize)) == NULL) {
|
||||
fprintf(stderr, "kgmon: cannot allocate tos space\n");
|
||||
exit(10);
|
||||
}
|
||||
if ((tos = (struct tostruct *)malloc(kvp->gpm.tossize)) == NULL)
|
||||
errx(10, "cannot allocate tos space");
|
||||
if (kflag) {
|
||||
i = kvm_read(kvp->kd, (u_long)kvp->gpm.tos, (void *)tos,
|
||||
kvp->gpm.tossize);
|
||||
@ -409,14 +401,12 @@ dumpstate(kvp)
|
||||
if (sysctl(mib, 3, tos, &i, NULL, 0) < 0)
|
||||
i = 0;
|
||||
}
|
||||
if (i != kvp->gpm.tossize) {
|
||||
(void)fprintf(stderr, "kgmon: read tos: read %u, got %d: %s",
|
||||
if (i != kvp->gpm.tossize)
|
||||
errx(11, "read tos: read %u, got %d: %s",
|
||||
kvp->gpm.tossize, i,
|
||||
kflag ? kvm_geterr(kvp->kd) : strerror(errno));
|
||||
exit(11);
|
||||
}
|
||||
if (debug)
|
||||
(void)fprintf(stderr, "kgmon: lowpc 0x%x, textsize 0x%x\n",
|
||||
warnx("lowpc 0x%x, textsize 0x%x",
|
||||
kvp->gpm.lowpc, kvp->gpm.textsize);
|
||||
endfrom = kvp->gpm.fromssize / sizeof(*froms);
|
||||
for (fromindex = 0; fromindex < endfrom; ++fromindex) {
|
||||
@ -427,9 +417,8 @@ dumpstate(kvp)
|
||||
for (toindex = froms[fromindex]; toindex != 0;
|
||||
toindex = tos[toindex].link) {
|
||||
if (debug)
|
||||
(void)fprintf(stderr,
|
||||
"%s: [mcleanup] frompc 0x%x selfpc 0x%x count %d\n",
|
||||
"kgmon", frompc, tos[toindex].selfpc,
|
||||
warnx("[mcleanup] frompc 0x%x selfpc 0x%x count %d",
|
||||
frompc, tos[toindex].selfpc,
|
||||
tos[toindex].count);
|
||||
rawarc.raw_frompc = frompc;
|
||||
rawarc.raw_selfpc = (u_long)tos[toindex].selfpc;
|
||||
@ -454,8 +443,7 @@ getprofhz(kvp)
|
||||
profrate = 1;
|
||||
if (kvm_read(kvp->kd, nl[N_PROFHZ].n_value, &profrate,
|
||||
sizeof profrate) != sizeof profrate)
|
||||
(void)fprintf(stderr, "kgmon: get clockrate: %s\n",
|
||||
kvm_geterr(kvp->kd));
|
||||
warnx("get clockrate: %s", kvm_geterr(kvp->kd));
|
||||
return (profrate);
|
||||
}
|
||||
mib[0] = CTL_KERN;
|
||||
@ -463,8 +451,7 @@ getprofhz(kvp)
|
||||
clockrate.profhz = 1;
|
||||
size = sizeof clockrate;
|
||||
if (sysctl(mib, 2, &clockrate, &size, NULL, 0) < 0)
|
||||
(void)fprintf(stderr, "kgmon: get clockrate: %s\n",
|
||||
strerror(errno));
|
||||
warn("get clockrate");
|
||||
return (clockrate.profhz);
|
||||
}
|
||||
|
||||
@ -486,53 +473,33 @@ reset(kvp)
|
||||
biggest = kvp->gpm.fromssize;
|
||||
if (kvp->gpm.tossize > biggest)
|
||||
biggest = kvp->gpm.tossize;
|
||||
if ((zbuf = (char *)malloc(biggest)) == NULL) {
|
||||
fprintf(stderr, "kgmon: cannot allocate zbuf space\n");
|
||||
exit(12);
|
||||
}
|
||||
if ((zbuf = (char *)malloc(biggest)) == NULL)
|
||||
errx(12, "cannot allocate zbuf space");
|
||||
bzero(zbuf, biggest);
|
||||
if (kflag) {
|
||||
if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf,
|
||||
kvp->gpm.kcountsize) != kvp->gpm.kcountsize) {
|
||||
(void)fprintf(stderr, "kgmon: tickbuf zero: %s\n",
|
||||
kvm_geterr(kvp->kd));
|
||||
exit(13);
|
||||
}
|
||||
kvp->gpm.kcountsize) != kvp->gpm.kcountsize)
|
||||
errx(13, "tickbuf zero: %s", kvm_geterr(kvp->kd));
|
||||
if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf,
|
||||
kvp->gpm.fromssize) != kvp->gpm.fromssize) {
|
||||
(void)fprintf(stderr, "kgmon: froms zero: %s\n",
|
||||
kvm_geterr(kvp->kd));
|
||||
exit(14);
|
||||
}
|
||||
kvp->gpm.fromssize) != kvp->gpm.fromssize)
|
||||
errx(14, "froms zero: %s", kvm_geterr(kvp->kd));
|
||||
if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf,
|
||||
kvp->gpm.tossize) != kvp->gpm.tossize) {
|
||||
(void)fprintf(stderr, "kgmon: tos zero: %s\n",
|
||||
kvm_geterr(kvp->kd));
|
||||
exit(15);
|
||||
}
|
||||
kvp->gpm.tossize) != kvp->gpm.tossize)
|
||||
errx(15, "tos zero: %s", kvm_geterr(kvp->kd));
|
||||
return;
|
||||
}
|
||||
(void)seteuid(0);
|
||||
mib[0] = CTL_KERN;
|
||||
mib[1] = KERN_PROF;
|
||||
mib[2] = GPROF_COUNT;
|
||||
if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.kcountsize) < 0) {
|
||||
(void)fprintf(stderr, "kgmon: tickbuf zero: %s\n",
|
||||
strerror(errno));
|
||||
exit(13);
|
||||
}
|
||||
if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.kcountsize) < 0)
|
||||
err(13, "tickbuf zero");
|
||||
mib[2] = GPROF_FROMS;
|
||||
if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.fromssize) < 0) {
|
||||
(void)fprintf(stderr, "kgmon: froms zero: %s\n",
|
||||
strerror(errno));
|
||||
exit(14);
|
||||
}
|
||||
if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.fromssize) < 0)
|
||||
err(14, "froms zero");
|
||||
mib[2] = GPROF_TOS;
|
||||
if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.tossize) < 0) {
|
||||
(void)fprintf(stderr, "kgmon: tos zero: %s\n",
|
||||
strerror(errno));
|
||||
exit(15);
|
||||
}
|
||||
if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.tossize) < 0)
|
||||
err(15, "tos zero");
|
||||
(void)seteuid(getuid());
|
||||
free(zbuf);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user