1994-05-26 05:23:31 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1983, 1992, 1993
|
|
|
|
* The Regents of the University of California. 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.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-09-23 06:38:54 +00:00
|
|
|
static const char copyright[] =
|
1994-05-26 05:23:31 +00:00
|
|
|
"@(#) Copyright (c) 1983, 1992, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-09-23 06:38:54 +00:00
|
|
|
#if 0
|
1994-05-26 05:23:31 +00:00
|
|
|
static char sccsid[] = "@(#)kgmon.c 8.1 (Berkeley) 6/6/93";
|
1997-09-23 06:38:54 +00:00
|
|
|
#endif
|
1994-05-26 05:23:31 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
2013-02-14 14:44:08 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1994-05-26 05:23:31 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/file.h>
|
1995-10-30 15:45:21 +00:00
|
|
|
#include <sys/time.h>
|
1994-05-26 05:23:31 +00:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/gmon.h>
|
1997-09-23 06:38:54 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
1994-05-26 05:23:31 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <kvm.h>
|
|
|
|
#include <limits.h>
|
1997-09-23 06:38:54 +00:00
|
|
|
#include <nlist.h>
|
|
|
|
#include <paths.h>
|
2002-02-21 05:52:49 +00:00
|
|
|
#include <stddef.h>
|
1994-05-26 05:23:31 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1997-09-23 06:38:54 +00:00
|
|
|
#include <unistd.h>
|
1994-05-26 05:23:31 +00:00
|
|
|
|
|
|
|
struct nlist nl[] = {
|
|
|
|
#define N_GMONPARAM 0
|
|
|
|
{ "__gmonparam" },
|
|
|
|
#define N_PROFHZ 1
|
|
|
|
{ "_profhz" },
|
1997-09-23 06:38:54 +00:00
|
|
|
{ NULL },
|
1994-05-26 05:23:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct kvmvars {
|
|
|
|
kvm_t *kd;
|
|
|
|
struct gmonparam gpm;
|
|
|
|
};
|
|
|
|
|
1995-12-29 15:30:05 +00:00
|
|
|
int Bflag, bflag, hflag, kflag, rflag, pflag;
|
1994-05-26 05:23:31 +00:00
|
|
|
int debug = 0;
|
2002-07-11 18:31:16 +00:00
|
|
|
int getprof(struct kvmvars *);
|
|
|
|
int getprofhz(struct kvmvars *);
|
|
|
|
void kern_readonly(int);
|
2013-02-14 14:44:08 +00:00
|
|
|
int openfiles(const char *, char *, struct kvmvars *);
|
2002-07-11 18:31:16 +00:00
|
|
|
void setprof(struct kvmvars *kvp, int state);
|
|
|
|
void dumpstate(struct kvmvars *kvp);
|
|
|
|
void reset(struct kvmvars *kvp);
|
|
|
|
static void usage(void);
|
1994-05-26 05:23:31 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int ch, mode, disp, accessmode;
|
|
|
|
struct kvmvars kvmvars;
|
2013-02-14 14:44:08 +00:00
|
|
|
const char *systemname;
|
|
|
|
char *kmemf;
|
1994-05-26 05:23:31 +00:00
|
|
|
|
2012-10-22 03:07:05 +00:00
|
|
|
if (seteuid(getuid()) != 0) {
|
|
|
|
err(1, "seteuid failed\n");
|
|
|
|
}
|
1994-05-26 05:23:31 +00:00
|
|
|
kmemf = NULL;
|
2013-02-14 14:44:08 +00:00
|
|
|
systemname = NULL;
|
1997-03-31 05:11:47 +00:00
|
|
|
while ((ch = getopt(argc, argv, "M:N:Bbhpr")) != -1) {
|
1994-05-26 05:23:31 +00:00
|
|
|
switch((char)ch) {
|
|
|
|
|
|
|
|
case 'M':
|
|
|
|
kmemf = optarg;
|
|
|
|
kflag = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'N':
|
2013-02-14 14:44:08 +00:00
|
|
|
systemname = optarg;
|
1994-05-26 05:23:31 +00:00
|
|
|
break;
|
|
|
|
|
1995-12-29 15:30:05 +00:00
|
|
|
case 'B':
|
|
|
|
Bflag = 1;
|
|
|
|
break;
|
|
|
|
|
1994-05-26 05:23:31 +00:00
|
|
|
case 'b':
|
|
|
|
bflag = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
hflag = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
pflag = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
rflag = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1997-09-23 06:38:54 +00:00
|
|
|
usage();
|
1994-05-26 05:23:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
#define BACKWARD_COMPATIBILITY
|
|
|
|
#ifdef BACKWARD_COMPATIBILITY
|
|
|
|
if (*argv) {
|
2013-02-14 14:44:08 +00:00
|
|
|
systemname = *argv;
|
1994-05-26 05:23:31 +00:00
|
|
|
if (*++argv) {
|
|
|
|
kmemf = *argv;
|
|
|
|
++kflag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2013-02-14 14:44:08 +00:00
|
|
|
if (systemname == NULL)
|
|
|
|
systemname = getbootfile();
|
|
|
|
accessmode = openfiles(systemname, kmemf, &kvmvars);
|
1994-05-26 05:23:31 +00:00
|
|
|
mode = getprof(&kvmvars);
|
|
|
|
if (hflag)
|
|
|
|
disp = GMON_PROF_OFF;
|
1995-12-29 15:30:05 +00:00
|
|
|
else if (Bflag)
|
|
|
|
disp = GMON_PROF_HIRES;
|
1994-05-26 05:23:31 +00:00
|
|
|
else if (bflag)
|
|
|
|
disp = GMON_PROF_ON;
|
|
|
|
else
|
|
|
|
disp = mode;
|
|
|
|
if (pflag)
|
|
|
|
dumpstate(&kvmvars);
|
|
|
|
if (rflag)
|
|
|
|
reset(&kvmvars);
|
|
|
|
if (accessmode == O_RDWR)
|
|
|
|
setprof(&kvmvars, disp);
|
|
|
|
(void)fprintf(stdout, "kgmon: kernel profiling is %s.\n",
|
1995-12-29 15:30:05 +00:00
|
|
|
disp == GMON_PROF_OFF ? "off" :
|
|
|
|
disp == GMON_PROF_HIRES ? "running (high resolution)" :
|
|
|
|
disp == GMON_PROF_ON ? "running" :
|
|
|
|
disp == GMON_PROF_BUSY ? "busy" :
|
|
|
|
disp == GMON_PROF_ERROR ? "off (error)" :
|
|
|
|
"in an unknown state");
|
1994-05-26 05:23:31 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1997-09-23 06:38:54 +00:00
|
|
|
static void
|
2013-02-14 14:44:08 +00:00
|
|
|
usage(void)
|
1997-09-23 06:38:54 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "usage: kgmon [-Bbhrp] [-M core] [-N system]\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1994-05-26 05:23:31 +00:00
|
|
|
/*
|
2006-06-29 09:18:16 +00:00
|
|
|
* Check that profiling is enabled and open any necessary files.
|
1994-05-26 05:23:31 +00:00
|
|
|
*/
|
1997-09-23 06:38:54 +00:00
|
|
|
int
|
2013-02-14 14:44:08 +00:00
|
|
|
openfiles(const char *systemname, char *kmemf, struct kvmvars *kvp)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
2002-07-30 04:45:14 +00:00
|
|
|
size_t size;
|
|
|
|
int mib[3], state, openmode;
|
1994-05-26 05:23:31 +00:00
|
|
|
char errbuf[_POSIX2_LINE_MAX];
|
|
|
|
|
|
|
|
if (!kflag) {
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_PROF;
|
|
|
|
mib[2] = GPROF_STATE;
|
|
|
|
size = sizeof state;
|
1997-09-23 06:38:54 +00:00
|
|
|
if (sysctl(mib, 3, &state, &size, NULL, 0) < 0)
|
|
|
|
errx(20, "profiling not defined in kernel");
|
1995-12-29 15:30:05 +00:00
|
|
|
if (!(Bflag || bflag || hflag || rflag ||
|
|
|
|
(pflag &&
|
|
|
|
(state == GMON_PROF_HIRES || state == GMON_PROF_ON))))
|
1994-05-26 05:23:31 +00:00
|
|
|
return (O_RDONLY);
|
|
|
|
(void)seteuid(0);
|
|
|
|
if (sysctl(mib, 3, NULL, NULL, &state, size) >= 0)
|
|
|
|
return (O_RDWR);
|
|
|
|
(void)seteuid(getuid());
|
|
|
|
kern_readonly(state);
|
|
|
|
return (O_RDONLY);
|
|
|
|
}
|
1995-12-29 15:30:05 +00:00
|
|
|
openmode = (Bflag || bflag || hflag || pflag || rflag)
|
|
|
|
? O_RDWR : O_RDONLY;
|
2013-02-14 14:44:08 +00:00
|
|
|
kvp->kd = kvm_openfiles(systemname, kmemf, NULL, openmode, errbuf);
|
1994-05-26 05:23:31 +00:00
|
|
|
if (kvp->kd == NULL) {
|
|
|
|
if (openmode == O_RDWR) {
|
|
|
|
openmode = O_RDONLY;
|
2013-02-14 14:44:08 +00:00
|
|
|
kvp->kd = kvm_openfiles(systemname, kmemf, NULL, O_RDONLY,
|
1994-05-26 05:23:31 +00:00
|
|
|
errbuf);
|
|
|
|
}
|
1997-09-23 06:38:54 +00:00
|
|
|
if (kvp->kd == NULL)
|
|
|
|
errx(2, "kvm_openfiles: %s", errbuf);
|
1994-05-26 05:23:31 +00:00
|
|
|
kern_readonly(GMON_PROF_ON);
|
|
|
|
}
|
1997-09-23 06:38:54 +00:00
|
|
|
if (kvm_nlist(kvp->kd, nl) < 0)
|
2013-02-14 14:44:08 +00:00
|
|
|
errx(3, "%s: no namelist", systemname);
|
1997-09-23 06:38:54 +00:00
|
|
|
if (!nl[N_GMONPARAM].n_value)
|
|
|
|
errx(20, "profiling not defined in kernel");
|
1994-05-26 05:23:31 +00:00
|
|
|
return (openmode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Suppress options that require a writable kernel.
|
|
|
|
*/
|
1997-09-23 06:38:54 +00:00
|
|
|
void
|
2013-02-14 14:44:08 +00:00
|
|
|
kern_readonly(int mode)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
(void)fprintf(stderr, "kgmon: kernel read-only: ");
|
1995-12-29 15:30:05 +00:00
|
|
|
if (pflag && (mode == GMON_PROF_HIRES || mode == GMON_PROF_ON))
|
1994-05-26 05:23:31 +00:00
|
|
|
(void)fprintf(stderr, "data may be inconsistent\n");
|
|
|
|
if (rflag)
|
2011-12-30 10:58:14 +00:00
|
|
|
(void)fprintf(stderr, "-r suppressed\n");
|
1995-12-29 15:30:05 +00:00
|
|
|
if (Bflag)
|
2011-12-30 10:58:14 +00:00
|
|
|
(void)fprintf(stderr, "-B suppressed\n");
|
1994-05-26 05:23:31 +00:00
|
|
|
if (bflag)
|
2011-12-30 10:58:14 +00:00
|
|
|
(void)fprintf(stderr, "-b suppressed\n");
|
1994-05-26 05:23:31 +00:00
|
|
|
if (hflag)
|
2011-12-30 10:58:14 +00:00
|
|
|
(void)fprintf(stderr, "-h suppressed\n");
|
1995-12-29 15:30:05 +00:00
|
|
|
rflag = Bflag = bflag = hflag = 0;
|
1994-05-26 05:23:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the state of kernel profiling.
|
|
|
|
*/
|
1997-09-23 06:38:54 +00:00
|
|
|
int
|
2013-02-14 14:44:08 +00:00
|
|
|
getprof(struct kvmvars *kvp)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
2002-07-30 04:45:14 +00:00
|
|
|
size_t size;
|
|
|
|
int mib[3];
|
1994-05-26 05:23:31 +00:00
|
|
|
|
|
|
|
if (kflag) {
|
|
|
|
size = kvm_read(kvp->kd, nl[N_GMONPARAM].n_value, &kvp->gpm,
|
|
|
|
sizeof kvp->gpm);
|
|
|
|
} else {
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_PROF;
|
|
|
|
mib[2] = GPROF_GMONPARAM;
|
|
|
|
size = sizeof kvp->gpm;
|
|
|
|
if (sysctl(mib, 3, &kvp->gpm, &size, NULL, 0) < 0)
|
|
|
|
size = 0;
|
|
|
|
}
|
2002-02-21 05:52:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Accept certain undersized "structs" from old kernels. We need
|
|
|
|
* everything up to hashfraction, and want profrate and
|
|
|
|
* histcounter_type. Assume that the kernel doesn't put garbage
|
|
|
|
* in any padding that is returned instead of profrate and
|
|
|
|
* histcounter_type. This is a bad assumption for dead kernels,
|
|
|
|
* since kvm_read() will normally return garbage for bytes beyond
|
|
|
|
* the end of the actual kernel struct, if any.
|
|
|
|
*/
|
|
|
|
if (size < offsetof(struct gmonparam, hashfraction) +
|
|
|
|
sizeof(kvp->gpm.hashfraction) || size > sizeof(kvp->gpm))
|
1997-09-23 06:38:54 +00:00
|
|
|
errx(4, "cannot get gmonparam: %s",
|
1994-05-26 05:23:31 +00:00
|
|
|
kflag ? kvm_geterr(kvp->kd) : strerror(errno));
|
2002-02-21 05:52:49 +00:00
|
|
|
bzero((char *)&kvp->gpm + size, sizeof(kvp->gpm) - size);
|
|
|
|
if (kvp->gpm.profrate == 0)
|
|
|
|
kvp->gpm.profrate = getprofhz(kvp);
|
|
|
|
#ifdef __i386__
|
|
|
|
if (kvp->gpm.histcounter_type == 0) {
|
|
|
|
/*
|
|
|
|
* This fixup only works for not-so-old i386 kernels. The
|
|
|
|
* magic 16 is the kernel FUNCTION_ALIGNMENT. 64-bit
|
|
|
|
* counters are signed; smaller counters are unsigned.
|
|
|
|
*/
|
|
|
|
kvp->gpm.histcounter_type = 16 /
|
|
|
|
(kvp->gpm.textsize / kvp->gpm.kcountsize) * CHAR_BIT;
|
|
|
|
if (kvp->gpm.histcounter_type == 64)
|
|
|
|
kvp->gpm.histcounter_type = -64;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1994-05-26 05:23:31 +00:00
|
|
|
return (kvp->gpm.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enable or disable kernel profiling according to the state variable.
|
|
|
|
*/
|
|
|
|
void
|
2013-02-14 14:44:08 +00:00
|
|
|
setprof(struct kvmvars *kvp, int state)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
|
|
|
struct gmonparam *p = (struct gmonparam *)nl[N_GMONPARAM].n_value;
|
2002-07-30 04:45:14 +00:00
|
|
|
size_t sz;
|
|
|
|
int mib[3], oldstate;
|
1994-05-26 05:23:31 +00:00
|
|
|
|
|
|
|
sz = sizeof(state);
|
|
|
|
if (!kflag) {
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_PROF;
|
|
|
|
mib[2] = GPROF_STATE;
|
|
|
|
if (sysctl(mib, 3, &oldstate, &sz, NULL, 0) < 0)
|
|
|
|
goto bad;
|
|
|
|
if (oldstate == state)
|
|
|
|
return;
|
|
|
|
(void)seteuid(0);
|
|
|
|
if (sysctl(mib, 3, NULL, NULL, &state, sz) >= 0) {
|
|
|
|
(void)seteuid(getuid());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
(void)seteuid(getuid());
|
1995-05-30 03:57:47 +00:00
|
|
|
} else if (kvm_write(kvp->kd, (u_long)&p->state, (void *)&state, sz)
|
2013-02-14 14:44:08 +00:00
|
|
|
== (ssize_t)sz)
|
1994-05-26 05:23:31 +00:00
|
|
|
return;
|
|
|
|
bad:
|
1997-09-23 06:38:54 +00:00
|
|
|
warnx("warning: cannot turn profiling %s",
|
1994-05-26 05:23:31 +00:00
|
|
|
state == GMON_PROF_OFF ? "off" : "on");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build the gmon.out file.
|
|
|
|
*/
|
|
|
|
void
|
2013-02-14 14:44:08 +00:00
|
|
|
dumpstate(struct kvmvars *kvp)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
|
|
|
register FILE *fp;
|
|
|
|
struct rawarc rawarc;
|
|
|
|
struct tostruct *tos;
|
1997-09-23 06:38:54 +00:00
|
|
|
u_long frompc;
|
1994-05-26 05:23:31 +00:00
|
|
|
u_short *froms, *tickbuf;
|
2002-07-30 04:45:14 +00:00
|
|
|
size_t i;
|
|
|
|
int mib[3];
|
1994-05-26 05:23:31 +00:00
|
|
|
struct gmonhdr h;
|
|
|
|
int fromindex, endfrom, toindex;
|
|
|
|
|
|
|
|
setprof(kvp, GMON_PROF_OFF);
|
|
|
|
fp = fopen("gmon.out", "w");
|
|
|
|
if (fp == 0) {
|
1997-09-23 06:38:54 +00:00
|
|
|
warn("gmon.out");
|
1994-05-26 05:23:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build the gmon header and write it to a file.
|
|
|
|
*/
|
|
|
|
bzero(&h, sizeof(h));
|
|
|
|
h.lpc = kvp->gpm.lowpc;
|
|
|
|
h.hpc = kvp->gpm.highpc;
|
|
|
|
h.ncnt = kvp->gpm.kcountsize + sizeof(h);
|
|
|
|
h.version = GMONVERSION;
|
1995-12-29 15:30:05 +00:00
|
|
|
h.profrate = kvp->gpm.profrate;
|
2002-02-21 05:52:49 +00:00
|
|
|
h.histcounter_type = kvp->gpm.histcounter_type;
|
1994-05-26 05:23:31 +00:00
|
|
|
fwrite((char *)&h, sizeof(h), 1, fp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write out the tick buffer.
|
|
|
|
*/
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_PROF;
|
1997-09-23 06:38:54 +00:00
|
|
|
if ((tickbuf = (u_short *)malloc(kvp->gpm.kcountsize)) == NULL)
|
|
|
|
errx(5, "cannot allocate kcount space");
|
1994-05-26 05:23:31 +00:00
|
|
|
if (kflag) {
|
|
|
|
i = kvm_read(kvp->kd, (u_long)kvp->gpm.kcount, (void *)tickbuf,
|
|
|
|
kvp->gpm.kcountsize);
|
|
|
|
} else {
|
|
|
|
mib[2] = GPROF_COUNT;
|
|
|
|
i = kvp->gpm.kcountsize;
|
|
|
|
if (sysctl(mib, 3, tickbuf, &i, NULL, 0) < 0)
|
|
|
|
i = 0;
|
|
|
|
}
|
1997-09-23 06:38:54 +00:00
|
|
|
if (i != kvp->gpm.kcountsize)
|
2004-08-30 03:11:46 +00:00
|
|
|
errx(6, "read ticks: read %lu, got %ld: %s",
|
|
|
|
kvp->gpm.kcountsize, (long)i,
|
1994-05-26 05:23:31 +00:00
|
|
|
kflag ? kvm_geterr(kvp->kd) : strerror(errno));
|
1997-09-23 06:38:54 +00:00
|
|
|
if ((fwrite(tickbuf, kvp->gpm.kcountsize, 1, fp)) != 1)
|
|
|
|
err(7, "writing tocks to gmon.out");
|
1994-05-26 05:23:31 +00:00
|
|
|
free(tickbuf);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write out the arc info.
|
|
|
|
*/
|
1997-09-23 06:38:54 +00:00
|
|
|
if ((froms = (u_short *)malloc(kvp->gpm.fromssize)) == NULL)
|
|
|
|
errx(8, "cannot allocate froms space");
|
1994-05-26 05:23:31 +00:00
|
|
|
if (kflag) {
|
|
|
|
i = kvm_read(kvp->kd, (u_long)kvp->gpm.froms, (void *)froms,
|
|
|
|
kvp->gpm.fromssize);
|
|
|
|
} else {
|
|
|
|
mib[2] = GPROF_FROMS;
|
|
|
|
i = kvp->gpm.fromssize;
|
|
|
|
if (sysctl(mib, 3, froms, &i, NULL, 0) < 0)
|
|
|
|
i = 0;
|
|
|
|
}
|
1997-09-23 06:38:54 +00:00
|
|
|
if (i != kvp->gpm.fromssize)
|
2004-08-30 03:11:46 +00:00
|
|
|
errx(9, "read froms: read %lu, got %ld: %s",
|
|
|
|
kvp->gpm.fromssize, (long)i,
|
1994-05-26 05:23:31 +00:00
|
|
|
kflag ? kvm_geterr(kvp->kd) : strerror(errno));
|
1997-09-23 06:38:54 +00:00
|
|
|
if ((tos = (struct tostruct *)malloc(kvp->gpm.tossize)) == NULL)
|
|
|
|
errx(10, "cannot allocate tos space");
|
1994-05-26 05:23:31 +00:00
|
|
|
if (kflag) {
|
|
|
|
i = kvm_read(kvp->kd, (u_long)kvp->gpm.tos, (void *)tos,
|
|
|
|
kvp->gpm.tossize);
|
|
|
|
} else {
|
|
|
|
mib[2] = GPROF_TOS;
|
|
|
|
i = kvp->gpm.tossize;
|
|
|
|
if (sysctl(mib, 3, tos, &i, NULL, 0) < 0)
|
|
|
|
i = 0;
|
|
|
|
}
|
1997-09-23 06:38:54 +00:00
|
|
|
if (i != kvp->gpm.tossize)
|
2004-08-30 03:11:46 +00:00
|
|
|
errx(11, "read tos: read %lu, got %ld: %s",
|
|
|
|
kvp->gpm.tossize, (long)i,
|
1994-05-26 05:23:31 +00:00
|
|
|
kflag ? kvm_geterr(kvp->kd) : strerror(errno));
|
|
|
|
if (debug)
|
2004-08-30 03:11:46 +00:00
|
|
|
warnx("lowpc 0x%lx, textsize 0x%lx",
|
|
|
|
(unsigned long)kvp->gpm.lowpc, kvp->gpm.textsize);
|
1994-05-26 05:23:31 +00:00
|
|
|
endfrom = kvp->gpm.fromssize / sizeof(*froms);
|
|
|
|
for (fromindex = 0; fromindex < endfrom; ++fromindex) {
|
|
|
|
if (froms[fromindex] == 0)
|
|
|
|
continue;
|
|
|
|
frompc = (u_long)kvp->gpm.lowpc +
|
|
|
|
(fromindex * kvp->gpm.hashfraction * sizeof(*froms));
|
|
|
|
for (toindex = froms[fromindex]; toindex != 0;
|
2004-08-30 03:11:46 +00:00
|
|
|
toindex = tos[toindex].link) {
|
1994-05-26 05:23:31 +00:00
|
|
|
if (debug)
|
2004-08-30 03:11:46 +00:00
|
|
|
warnx("[mcleanup] frompc 0x%lx selfpc 0x%lx "
|
|
|
|
"count %ld", frompc, tos[toindex].selfpc,
|
|
|
|
tos[toindex].count);
|
1994-05-26 05:23:31 +00:00
|
|
|
rawarc.raw_frompc = frompc;
|
|
|
|
rawarc.raw_selfpc = (u_long)tos[toindex].selfpc;
|
|
|
|
rawarc.raw_count = tos[toindex].count;
|
|
|
|
fwrite((char *)&rawarc, sizeof(rawarc), 1, fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the profiling rate.
|
|
|
|
*/
|
|
|
|
int
|
2013-02-14 14:44:08 +00:00
|
|
|
getprofhz(struct kvmvars *kvp)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
2002-07-30 04:45:14 +00:00
|
|
|
size_t size;
|
|
|
|
int mib[2], profrate;
|
1994-05-26 05:23:31 +00:00
|
|
|
struct clockinfo clockrate;
|
|
|
|
|
|
|
|
if (kflag) {
|
|
|
|
profrate = 1;
|
|
|
|
if (kvm_read(kvp->kd, nl[N_PROFHZ].n_value, &profrate,
|
|
|
|
sizeof profrate) != sizeof profrate)
|
1997-09-23 06:38:54 +00:00
|
|
|
warnx("get clockrate: %s", kvm_geterr(kvp->kd));
|
1994-05-26 05:23:31 +00:00
|
|
|
return (profrate);
|
|
|
|
}
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_CLOCKRATE;
|
|
|
|
clockrate.profhz = 1;
|
|
|
|
size = sizeof clockrate;
|
|
|
|
if (sysctl(mib, 2, &clockrate, &size, NULL, 0) < 0)
|
1997-09-23 06:38:54 +00:00
|
|
|
warn("get clockrate");
|
1994-05-26 05:23:31 +00:00
|
|
|
return (clockrate.profhz);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reset the kernel profiling date structures.
|
|
|
|
*/
|
|
|
|
void
|
2013-02-14 14:44:08 +00:00
|
|
|
reset(struct kvmvars *kvp)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
|
|
|
char *zbuf;
|
|
|
|
u_long biggest;
|
|
|
|
int mib[3];
|
|
|
|
|
|
|
|
setprof(kvp, GMON_PROF_OFF);
|
|
|
|
|
|
|
|
biggest = kvp->gpm.kcountsize;
|
|
|
|
if (kvp->gpm.fromssize > biggest)
|
|
|
|
biggest = kvp->gpm.fromssize;
|
|
|
|
if (kvp->gpm.tossize > biggest)
|
|
|
|
biggest = kvp->gpm.tossize;
|
1997-09-23 06:38:54 +00:00
|
|
|
if ((zbuf = (char *)malloc(biggest)) == NULL)
|
|
|
|
errx(12, "cannot allocate zbuf space");
|
1994-05-26 05:23:31 +00:00
|
|
|
bzero(zbuf, biggest);
|
|
|
|
if (kflag) {
|
|
|
|
if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf,
|
2013-02-14 14:44:08 +00:00
|
|
|
kvp->gpm.kcountsize) != (ssize_t)kvp->gpm.kcountsize)
|
1997-09-23 06:38:54 +00:00
|
|
|
errx(13, "tickbuf zero: %s", kvm_geterr(kvp->kd));
|
1994-05-26 05:23:31 +00:00
|
|
|
if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf,
|
2013-02-14 14:44:08 +00:00
|
|
|
kvp->gpm.fromssize) != (ssize_t)kvp->gpm.fromssize)
|
1997-09-23 06:38:54 +00:00
|
|
|
errx(14, "froms zero: %s", kvm_geterr(kvp->kd));
|
1994-05-26 05:23:31 +00:00
|
|
|
if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf,
|
2013-02-14 14:44:08 +00:00
|
|
|
kvp->gpm.tossize) != (ssize_t)kvp->gpm.tossize)
|
1997-09-23 06:38:54 +00:00
|
|
|
errx(15, "tos zero: %s", kvm_geterr(kvp->kd));
|
1994-05-26 05:23:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
(void)seteuid(0);
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_PROF;
|
|
|
|
mib[2] = GPROF_COUNT;
|
1997-09-23 06:38:54 +00:00
|
|
|
if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.kcountsize) < 0)
|
|
|
|
err(13, "tickbuf zero");
|
1994-05-26 05:23:31 +00:00
|
|
|
mib[2] = GPROF_FROMS;
|
1997-09-23 06:38:54 +00:00
|
|
|
if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.fromssize) < 0)
|
|
|
|
err(14, "froms zero");
|
1994-05-26 05:23:31 +00:00
|
|
|
mib[2] = GPROF_TOS;
|
1997-09-23 06:38:54 +00:00
|
|
|
if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.tossize) < 0)
|
|
|
|
err(15, "tos zero");
|
1994-05-26 05:23:31 +00:00
|
|
|
(void)seteuid(getuid());
|
|
|
|
free(zbuf);
|
|
|
|
}
|