Sprinkle static and use err/errx/warn instead of fprintf/perror

This commit is contained in:
christos 2006-05-25 00:05:45 +00:00
parent d62ef54061
commit aa0a3ef501

View File

@ -1,4 +1,4 @@
/* $NetBSD: kgmon.c,v 1.16 2005/02/06 04:48:58 perry Exp $ */ /* $NetBSD: kgmon.c,v 1.17 2006/05/25 00:05:45 christos Exp $ */
/* /*
* Copyright (c) 1983, 1992, 1993 * Copyright (c) 1983, 1992, 1993
@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1983, 1992, 1993\n\
#if 0 #if 0
static char sccsid[] = "from: @(#)kgmon.c 8.1 (Berkeley) 6/6/93"; static char sccsid[] = "from: @(#)kgmon.c 8.1 (Berkeley) 6/6/93";
#else #else
__RCSID("$NetBSD: kgmon.c,v 1.16 2005/02/06 04:48:58 perry Exp $"); __RCSID("$NetBSD: kgmon.c,v 1.17 2006/05/25 00:05:45 christos Exp $");
#endif #endif
#endif /* not lint */ #endif /* not lint */
@ -51,6 +51,7 @@ __RCSID("$NetBSD: kgmon.c,v 1.16 2005/02/06 04:48:58 perry Exp $");
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <kvm.h> #include <kvm.h>
#include <err.h>
#include <limits.h> #include <limits.h>
#include <nlist.h> #include <nlist.h>
#include <stdio.h> #include <stdio.h>
@ -58,7 +59,7 @@ __RCSID("$NetBSD: kgmon.c,v 1.16 2005/02/06 04:48:58 perry Exp $");
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
struct nlist nl[] = { static struct nlist nl[] = {
#define N_GMONPARAM 0 #define N_GMONPARAM 0
{ "__gmonparam" }, { "__gmonparam" },
#define N_PROFHZ 1 #define N_PROFHZ 1
@ -71,15 +72,15 @@ struct kvmvars {
struct gmonparam gpm; struct gmonparam gpm;
}; };
int bflag, hflag, kflag, rflag, pflag; static int bflag, hflag, kflag, rflag, pflag;
int debug = 0; static int debug = 0;
void setprof(struct kvmvars *kvp, int state); static void setprof(struct kvmvars *kvp, int state);
void dumpstate(struct kvmvars *kvp); static void dumpstate(struct kvmvars *kvp);
void reset(struct kvmvars *kvp); static void reset(struct kvmvars *kvp);
int openfiles(char *, char *, struct kvmvars *); static int openfiles(char *, char *, struct kvmvars *);
int getprof(struct kvmvars *); static int getprof(struct kvmvars *);
void kern_readonly(int); static void kern_readonly(int);
int getprofhz(struct kvmvars *); static int getprofhz(struct kvmvars *);
int int
main(int argc, char **argv) main(int argc, char **argv)
@ -88,6 +89,7 @@ main(int argc, char **argv)
struct kvmvars kvmvars; struct kvmvars kvmvars;
char *system, *kmemf; char *system, *kmemf;
setprogname(argv[0]);
seteuid(getuid()); seteuid(getuid());
kmemf = NULL; kmemf = NULL;
system = NULL; system = NULL;
@ -157,15 +159,15 @@ main(int argc, char **argv)
reset(&kvmvars); reset(&kvmvars);
if (accessmode == O_RDWR) if (accessmode == O_RDWR)
setprof(&kvmvars, disp); setprof(&kvmvars, disp);
(void)fprintf(stdout, "kgmon: kernel profiling is %s.\n", (void)fprintf(stdout, "%s: kernel profiling is %s.\n",
disp == GMON_PROF_OFF ? "off" : "running"); getprogname(), disp == GMON_PROF_OFF ? "off" : "running");
return (0); return (0);
} }
/* /*
* Check that profiling is enabled and open any ncessary files. * Check that profiling is enabled and open any ncessary files.
*/ */
int static int
openfiles(char *system, char *kmemf, struct kvmvars *kvp) openfiles(char *system, char *kmemf, struct kvmvars *kvp)
{ {
int mib[3], state, openmode; int mib[3], state, openmode;
@ -177,11 +179,8 @@ openfiles(char *system, char *kmemf, struct kvmvars *kvp)
mib[1] = KERN_PROF; mib[1] = KERN_PROF;
mib[2] = GPROF_STATE; mib[2] = GPROF_STATE;
size = sizeof state; size = sizeof state;
if (sysctl(mib, 3, &state, &size, NULL, 0) < 0) { if (sysctl(mib, 3, &state, &size, NULL, 0) < 0)
(void)fprintf(stderr, err(1, "profiling not defined in kernel");
"kgmon: profiling not defined in kernel.\n");
exit(20);
}
if (!(bflag || hflag || rflag || if (!(bflag || hflag || rflag ||
(pflag && state == GMON_PROF_ON))) (pflag && state == GMON_PROF_ON)))
return (O_RDONLY); return (O_RDONLY);
@ -200,33 +199,25 @@ openfiles(char *system, char *kmemf, struct kvmvars *kvp)
kvp->kd = kvm_openfiles(system, kmemf, NULL, O_RDONLY, kvp->kd = kvm_openfiles(system, kmemf, NULL, O_RDONLY,
errbuf); errbuf);
} }
if (kvp->kd == NULL) { if (kvp->kd == NULL)
(void)fprintf(stderr, "kgmon: kvm_openfiles: %s\n", errx(1, "kvm_openfiles: %s", errbuf);
errbuf);
exit(2);
}
kern_readonly(GMON_PROF_ON); kern_readonly(GMON_PROF_ON);
} }
if (kvm_nlist(kvp->kd, nl) < 0) { if (kvm_nlist(kvp->kd, nl) < 0)
(void)fprintf(stderr, "kgmon: %s: no namelist\n", system); errx(1, "%s: no namelist", system);
exit(3); if (!nl[N_GMONPARAM].n_value)
} errx(1, "profiling not defined in kernel");
if (!nl[N_GMONPARAM].n_value) {
(void)fprintf(stderr,
"kgmon: profiling not defined in kernel.\n");
exit(20);
}
return (openmode); return (openmode);
} }
/* /*
* Suppress options that require a writable kernel. * Suppress options that require a writable kernel.
*/ */
void static void
kern_readonly(int mode) kern_readonly(int mode)
{ {
(void)fprintf(stderr, "kgmon: kernel read-only: "); (void)fprintf(stderr, "%s: kernel read-only: ", getprogname());
if (pflag && mode == GMON_PROF_ON) if (pflag && mode == GMON_PROF_ON)
(void)fprintf(stderr, "data may be inconsistent\n"); (void)fprintf(stderr, "data may be inconsistent\n");
if (rflag) if (rflag)
@ -241,7 +232,7 @@ kern_readonly(int mode)
/* /*
* Get the state of kernel profiling. * Get the state of kernel profiling.
*/ */
int static int
getprof(struct kvmvars *kvp) getprof(struct kvmvars *kvp)
{ {
int mib[3]; int mib[3];
@ -258,18 +249,16 @@ getprof(struct kvmvars *kvp)
if (sysctl(mib, 3, &kvp->gpm, &size, NULL, 0) < 0) if (sysctl(mib, 3, &kvp->gpm, &size, NULL, 0) < 0)
size = 0; size = 0;
} }
if (size != sizeof kvp->gpm) { if (size != sizeof kvp->gpm)
(void)fprintf(stderr, "kgmon: cannot get gmonparam: %s\n", errx(1, "cannot get gmonparam: %s",
kflag ? kvm_geterr(kvp->kd) : strerror(errno)); kflag ? kvm_geterr(kvp->kd) : strerror(errno));
exit (4);
}
return (kvp->gpm.state); return (kvp->gpm.state);
} }
/* /*
* Enable or disable kernel profiling according to the state variable. * Enable or disable kernel profiling according to the state variable.
*/ */
void static void
setprof(struct kvmvars *kvp, int state) setprof(struct kvmvars *kvp, int state)
{ {
struct gmonparam *p = (struct gmonparam *)nl[N_GMONPARAM].n_value; struct gmonparam *p = (struct gmonparam *)nl[N_GMONPARAM].n_value;
@ -295,14 +284,14 @@ setprof(struct kvmvars *kvp, int state)
== sz) == sz)
return; return;
bad: bad:
(void)fprintf(stderr, "kgmon: warning: cannot turn profiling %s\n", warnx("cannot turn profiling %s", state == GMON_PROF_OFF ?
state == GMON_PROF_OFF ? "off" : "on"); "off" : "on");
} }
/* /*
* Build the gmon.out file. * Build the gmon.out file.
*/ */
void static void
dumpstate(struct kvmvars *kvp) dumpstate(struct kvmvars *kvp)
{ {
FILE *fp; FILE *fp;
@ -318,7 +307,7 @@ dumpstate(struct kvmvars *kvp)
setprof(kvp, GMON_PROF_OFF); setprof(kvp, GMON_PROF_OFF);
fp = fopen("gmon.out", "w"); fp = fopen("gmon.out", "w");
if (fp == 0) { if (fp == 0) {
perror("gmon.out"); warn("cannot open `gmon.out'");
return; return;
} }
@ -338,10 +327,8 @@ dumpstate(struct kvmvars *kvp)
*/ */
mib[0] = CTL_KERN; mib[0] = CTL_KERN;
mib[1] = KERN_PROF; mib[1] = KERN_PROF;
if ((tickbuf = (u_short *)malloc(kvp->gpm.kcountsize)) == NULL) { if ((tickbuf = malloc(kvp->gpm.kcountsize)) == NULL)
fprintf(stderr, "kgmon: cannot allocate kcount space\n"); err(1, "cannot allocate kcount space");
exit (5);
}
if (kflag) { if (kflag) {
i = kvm_read(kvp->kd, (u_long)kvp->gpm.kcount, (void *)tickbuf, i = kvm_read(kvp->kd, (u_long)kvp->gpm.kcount, (void *)tickbuf,
kvp->gpm.kcountsize); kvp->gpm.kcountsize);
@ -351,26 +338,19 @@ dumpstate(struct kvmvars *kvp)
if (sysctl(mib, 3, tickbuf, &i, NULL, 0) < 0) if (sysctl(mib, 3, tickbuf, &i, NULL, 0) < 0)
i = 0; i = 0;
} }
if (i != kvp->gpm.kcountsize) { if (i != kvp->gpm.kcountsize)
(void)fprintf(stderr, errx(1, "read ticks: read %lu, got %lu: %s",
"kgmon: read ticks: read %lu, got %lu: %s\n",
kvp->gpm.kcountsize, (u_long)i, kvp->gpm.kcountsize, (u_long)i,
kflag ? kvm_geterr(kvp->kd) : strerror(errno)); kflag ? kvm_geterr(kvp->kd) : strerror(errno));
exit(6); if ((fwrite(tickbuf, kvp->gpm.kcountsize, 1, fp)) != 1)
} err(1, "writing tocks to gmon.out");
if ((fwrite(tickbuf, kvp->gpm.kcountsize, 1, fp)) != 1) {
perror("kgmon: writing tocks to gmon.out");
exit(7);
}
free(tickbuf); free(tickbuf);
/* /*
* Write out the arc info. * Write out the arc info.
*/ */
if ((froms = (u_short *)malloc(kvp->gpm.fromssize)) == NULL) { if ((froms = malloc(kvp->gpm.fromssize)) == NULL)
fprintf(stderr, "kgmon: cannot allocate froms space\n"); err(1, "cannot allocate froms space");
exit (8);
}
if (kflag) { if (kflag) {
i = kvm_read(kvp->kd, (u_long)kvp->gpm.froms, (void *)froms, i = kvm_read(kvp->kd, (u_long)kvp->gpm.froms, (void *)froms,
kvp->gpm.fromssize); kvp->gpm.fromssize);
@ -380,17 +360,12 @@ dumpstate(struct kvmvars *kvp)
if (sysctl(mib, 3, froms, &i, NULL, 0) < 0) if (sysctl(mib, 3, froms, &i, NULL, 0) < 0)
i = 0; i = 0;
} }
if (i != kvp->gpm.fromssize) { if (i != kvp->gpm.fromssize)
(void)fprintf(stderr, errx(1, "read froms: read %lu, got %lu: %s",
"kgmon: read froms: read %lu, got %lu: %s\n",
kvp->gpm.fromssize, (u_long)i, kvp->gpm.fromssize, (u_long)i,
kflag ? kvm_geterr(kvp->kd) : strerror(errno)); kflag ? kvm_geterr(kvp->kd) : strerror(errno));
exit(9); if ((tos = malloc(kvp->gpm.tossize)) == NULL)
} err(1, "cannot allocate tos space");
if ((tos = (struct tostruct *)malloc(kvp->gpm.tossize)) == NULL) {
fprintf(stderr, "kgmon: cannot allocate tos space\n");
exit(10);
}
if (kflag) { if (kflag) {
i = kvm_read(kvp->kd, (u_long)kvp->gpm.tos, (void *)tos, i = kvm_read(kvp->kd, (u_long)kvp->gpm.tos, (void *)tos,
kvp->gpm.tossize); kvp->gpm.tossize);
@ -400,16 +375,13 @@ dumpstate(struct kvmvars *kvp)
if (sysctl(mib, 3, tos, &i, NULL, 0) < 0) if (sysctl(mib, 3, tos, &i, NULL, 0) < 0)
i = 0; i = 0;
} }
if (i != kvp->gpm.tossize) { if (i != kvp->gpm.tossize)
(void)fprintf(stderr, errx(1, "read tos: read %lu, got %lu: %s",
"kgmon: read tos: read %lu, got %lu: %s\n",
kvp->gpm.tossize, (u_long)i, kvp->gpm.tossize, (u_long)i,
kflag ? kvm_geterr(kvp->kd) : strerror(errno)); kflag ? kvm_geterr(kvp->kd) : strerror(errno));
exit(11);
}
if (debug) if (debug)
(void)fprintf(stderr, "kgmon: lowpc 0x%lx, textsize 0x%lx\n", (void)fprintf(stderr, "%s: lowpc 0x%lx, textsize 0x%lx\n",
kvp->gpm.lowpc, kvp->gpm.textsize); getprogname(), kvp->gpm.lowpc, kvp->gpm.textsize);
endfrom = kvp->gpm.fromssize / sizeof(*froms); endfrom = kvp->gpm.fromssize / sizeof(*froms);
for (fromindex = 0; fromindex < endfrom; ++fromindex) { for (fromindex = 0; fromindex < endfrom; ++fromindex) {
if (froms[fromindex] == 0) if (froms[fromindex] == 0)
@ -421,7 +393,7 @@ dumpstate(struct kvmvars *kvp)
if (debug) if (debug)
(void)fprintf(stderr, (void)fprintf(stderr,
"%s: [mcleanup] frompc 0x%lx selfpc 0x%lx count %ld\n", "%s: [mcleanup] frompc 0x%lx selfpc 0x%lx count %ld\n",
"kgmon", frompc, tos[toindex].selfpc, getprogname(), frompc, tos[toindex].selfpc,
tos[toindex].count); tos[toindex].count);
rawarc.raw_frompc = frompc; rawarc.raw_frompc = frompc;
rawarc.raw_selfpc = (u_long)tos[toindex].selfpc; rawarc.raw_selfpc = (u_long)tos[toindex].selfpc;
@ -435,7 +407,7 @@ dumpstate(struct kvmvars *kvp)
/* /*
* Get the profiling rate. * Get the profiling rate.
*/ */
int static int
getprofhz(struct kvmvars *kvp) getprofhz(struct kvmvars *kvp)
{ {
int mib[2], profrate; int mib[2], profrate;
@ -446,8 +418,7 @@ getprofhz(struct kvmvars *kvp)
profrate = 1; profrate = 1;
if (kvm_read(kvp->kd, nl[N_PROFHZ].n_value, &profrate, if (kvm_read(kvp->kd, nl[N_PROFHZ].n_value, &profrate,
sizeof profrate) != sizeof profrate) sizeof profrate) != sizeof profrate)
(void)fprintf(stderr, "kgmon: get clockrate: %s\n", warnx("get clockrate: %s", kvm_geterr(kvp->kd));
kvm_geterr(kvp->kd));
return (profrate); return (profrate);
} }
mib[0] = CTL_KERN; mib[0] = CTL_KERN;
@ -455,15 +426,14 @@ getprofhz(struct kvmvars *kvp)
clockrate.profhz = 1; clockrate.profhz = 1;
size = sizeof clockrate; size = sizeof clockrate;
if (sysctl(mib, 2, &clockrate, &size, NULL, 0) < 0) if (sysctl(mib, 2, &clockrate, &size, NULL, 0) < 0)
(void)fprintf(stderr, "kgmon: get clockrate: %s\n", warn("get clockrate");
strerror(errno));
return (clockrate.profhz); return (clockrate.profhz);
} }
/* /*
* Reset the kernel profiling date structures. * Reset the kernel profiling date structures.
*/ */
void static void
reset(struct kvmvars *kvp) reset(struct kvmvars *kvp)
{ {
char *zbuf; char *zbuf;
@ -477,53 +447,34 @@ reset(struct kvmvars *kvp)
biggest = kvp->gpm.fromssize; biggest = kvp->gpm.fromssize;
if (kvp->gpm.tossize > biggest) if (kvp->gpm.tossize > biggest)
biggest = kvp->gpm.tossize; biggest = kvp->gpm.tossize;
if ((zbuf = (char *)malloc(biggest)) == NULL) { if ((zbuf = malloc(biggest)) == NULL)
fprintf(stderr, "kgmon: cannot allocate zbuf space\n"); err(1, "cannot allocate zbuf space");
exit(12);
}
bzero(zbuf, biggest); bzero(zbuf, biggest);
if (kflag) { if (kflag) {
if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf, if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf,
kvp->gpm.kcountsize) != kvp->gpm.kcountsize) { kvp->gpm.kcountsize) != kvp->gpm.kcountsize)
(void)fprintf(stderr, "kgmon: tickbuf zero: %s\n", errx(1, "tickbuf zero: %s", kvm_geterr(kvp->kd));
kvm_geterr(kvp->kd));
exit(13);
}
if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf, if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf,
kvp->gpm.fromssize) != kvp->gpm.fromssize) { kvp->gpm.fromssize) != kvp->gpm.fromssize)
(void)fprintf(stderr, "kgmon: froms zero: %s\n", errx(1, "froms zero: %s", kvm_geterr(kvp->kd));
kvm_geterr(kvp->kd));
exit(14);
}
if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf, if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf,
kvp->gpm.tossize) != kvp->gpm.tossize) { kvp->gpm.tossize) != kvp->gpm.tossize)
(void)fprintf(stderr, "kgmon: tos zero: %s\n", errx(1, "tos zero: %s", kvm_geterr(kvp->kd));
kvm_geterr(kvp->kd)); free(zbuf);
exit(15);
}
return; return;
} }
(void)seteuid(0); (void)seteuid(0);
mib[0] = CTL_KERN; mib[0] = CTL_KERN;
mib[1] = KERN_PROF; mib[1] = KERN_PROF;
mib[2] = GPROF_COUNT; mib[2] = GPROF_COUNT;
if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.kcountsize) < 0) { if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.kcountsize) < 0)
(void)fprintf(stderr, "kgmon: tickbuf zero: %s\n", err(1, "tickbuf zero");
strerror(errno));
exit(13);
}
mib[2] = GPROF_FROMS; mib[2] = GPROF_FROMS;
if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.fromssize) < 0) { if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.fromssize) < 0)
(void)fprintf(stderr, "kgmon: froms zero: %s\n", err(1, "froms zero");
strerror(errno));
exit(14);
}
mib[2] = GPROF_TOS; mib[2] = GPROF_TOS;
if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.tossize) < 0) { if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.tossize) < 0)
(void)fprintf(stderr, "kgmon: tos zero: %s\n", err(1, "tos zero");
strerror(errno));
exit(15);
}
(void)seteuid(getuid()); (void)seteuid(getuid());
free(zbuf); free(zbuf);
} }