kgmon.c revision 8857
11553Srgrimes/*
21553Srgrimes * Copyright (c) 1983, 1992, 1993
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes * Redistribution and use in source and binary forms, with or without
61553Srgrimes * modification, are permitted provided that the following conditions
71553Srgrimes * are met:
81553Srgrimes * 1. Redistributions of source code must retain the above copyright
91553Srgrimes *    notice, this list of conditions and the following disclaimer.
101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111553Srgrimes *    notice, this list of conditions and the following disclaimer in the
121553Srgrimes *    documentation and/or other materials provided with the distribution.
131553Srgrimes * 3. All advertising materials mentioning features or use of this software
141553Srgrimes *    must display the following acknowledgement:
151553Srgrimes *	This product includes software developed by the University of
161553Srgrimes *	California, Berkeley and its contributors.
171553Srgrimes * 4. Neither the name of the University nor the names of its contributors
181553Srgrimes *    may be used to endorse or promote products derived from this software
191553Srgrimes *    without specific prior written permission.
201553Srgrimes *
211553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311553Srgrimes * SUCH DAMAGE.
321553Srgrimes */
331553Srgrimes
341553Srgrimes#ifndef lint
351553Srgrimesstatic char copyright[] =
361553Srgrimes"@(#) Copyright (c) 1983, 1992, 1993\n\
371553Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381553Srgrimes#endif /* not lint */
391553Srgrimes
401553Srgrimes#ifndef lint
411553Srgrimesstatic char sccsid[] = "@(#)kgmon.c	8.1 (Berkeley) 6/6/93";
421553Srgrimes#endif /* not lint */
431553Srgrimes
441553Srgrimes#include <sys/param.h>
451553Srgrimes#include <sys/file.h>
461553Srgrimes#include <sys/sysctl.h>
471553Srgrimes#include <sys/gmon.h>
481553Srgrimes#include <errno.h>
491553Srgrimes#include <kvm.h>
501553Srgrimes#include <limits.h>
511553Srgrimes#include <stdio.h>
521553Srgrimes#include <stdlib.h>
531553Srgrimes#include <string.h>
541553Srgrimes#include <nlist.h>
551553Srgrimes#include <ctype.h>
561553Srgrimes#include <paths.h>
571553Srgrimes
581553Srgrimesstruct nlist nl[] = {
591553Srgrimes#define	N_GMONPARAM	0
601553Srgrimes	{ "__gmonparam" },
611553Srgrimes#define	N_PROFHZ	1
621553Srgrimes	{ "_profhz" },
631553Srgrimes	0,
641553Srgrimes};
651553Srgrimes
661553Srgrimesstruct kvmvars {
671553Srgrimes	kvm_t	*kd;
681553Srgrimes	struct gmonparam gpm;
691553Srgrimes};
701553Srgrimes
711553Srgrimesint	bflag, hflag, kflag, rflag, pflag;
721553Srgrimesint	debug = 0;
731553Srgrimesvoid	setprof __P((struct kvmvars *kvp, int state));
741553Srgrimesvoid	dumpstate __P((struct kvmvars *kvp));
751553Srgrimesvoid	reset __P((struct kvmvars *kvp));
761553Srgrimes
771553Srgrimesint
781553Srgrimesmain(int argc, char **argv)
791553Srgrimes{
801553Srgrimes	extern char *optarg;
811553Srgrimes	extern int optind;
821553Srgrimes	int ch, mode, disp, accessmode;
831553Srgrimes	struct kvmvars kvmvars;
841553Srgrimes	char *system, *kmemf;
851553Srgrimes
861553Srgrimes	seteuid(getuid());
871553Srgrimes	kmemf = NULL;
881553Srgrimes	system = NULL;
891553Srgrimes	while ((ch = getopt(argc, argv, "M:N:bhpr")) != EOF) {
901553Srgrimes		switch((char)ch) {
911553Srgrimes
921553Srgrimes		case 'M':
931553Srgrimes			kmemf = optarg;
941553Srgrimes			kflag = 1;
951553Srgrimes			break;
961553Srgrimes
971553Srgrimes		case 'N':
981553Srgrimes			system = optarg;
991553Srgrimes			break;
1001553Srgrimes
1011553Srgrimes		case 'b':
1021553Srgrimes			bflag = 1;
1031553Srgrimes			break;
1041553Srgrimes
1051553Srgrimes		case 'h':
1061553Srgrimes			hflag = 1;
1071553Srgrimes			break;
1081553Srgrimes
1091553Srgrimes		case 'p':
1101553Srgrimes			pflag = 1;
1111553Srgrimes			break;
1121553Srgrimes
1131553Srgrimes		case 'r':
1141553Srgrimes			rflag = 1;
1151553Srgrimes			break;
1161553Srgrimes
1171553Srgrimes		default:
1181553Srgrimes			(void)fprintf(stderr,
1191553Srgrimes			    "usage: kgmon [-bhrp] [-M core] [-N system]\n");
1201553Srgrimes			exit(1);
1211553Srgrimes		}
1221553Srgrimes	}
1231553Srgrimes	argc -= optind;
1241553Srgrimes	argv += optind;
1251553Srgrimes
1261553Srgrimes#define BACKWARD_COMPATIBILITY
1271553Srgrimes#ifdef	BACKWARD_COMPATIBILITY
1281553Srgrimes	if (*argv) {
1291553Srgrimes		system = *argv;
1301553Srgrimes		if (*++argv) {
1311553Srgrimes			kmemf = *argv;
1321553Srgrimes			++kflag;
1331553Srgrimes		}
1341553Srgrimes	}
1351553Srgrimes#endif
1361553Srgrimes	if (system == NULL)
1373041Swollman		system = (char *)getbootfile();
1381553Srgrimes	accessmode = openfiles(system, kmemf, &kvmvars);
1391553Srgrimes	mode = getprof(&kvmvars);
1401553Srgrimes	if (hflag)
1411553Srgrimes		disp = GMON_PROF_OFF;
1421553Srgrimes	else if (bflag)
1431553Srgrimes		disp = GMON_PROF_ON;
1441553Srgrimes	else
1451553Srgrimes		disp = mode;
1461553Srgrimes	if (pflag)
1471553Srgrimes		dumpstate(&kvmvars);
1481553Srgrimes	if (rflag)
1491553Srgrimes		reset(&kvmvars);
1501553Srgrimes	if (accessmode == O_RDWR)
1511553Srgrimes		setprof(&kvmvars, disp);
1521553Srgrimes	(void)fprintf(stdout, "kgmon: kernel profiling is %s.\n",
1531553Srgrimes		      disp == GMON_PROF_OFF ? "off" : "running");
1541553Srgrimes	return (0);
1551553Srgrimes}
1561553Srgrimes
1571553Srgrimes/*
1581553Srgrimes * Check that profiling is enabled and open any ncessary files.
1591553Srgrimes */
1601553Srgrimesopenfiles(system, kmemf, kvp)
1611553Srgrimes	char *system;
1621553Srgrimes	char *kmemf;
1631553Srgrimes	struct kvmvars *kvp;
1641553Srgrimes{
1651553Srgrimes	int mib[3], state, size, openmode;
1661553Srgrimes	char errbuf[_POSIX2_LINE_MAX];
1671553Srgrimes
1681553Srgrimes	if (!kflag) {
1691553Srgrimes		mib[0] = CTL_KERN;
1701553Srgrimes		mib[1] = KERN_PROF;
1711553Srgrimes		mib[2] = GPROF_STATE;
1721553Srgrimes		size = sizeof state;
1731553Srgrimes		if (sysctl(mib, 3, &state, &size, NULL, 0) < 0) {
1741553Srgrimes			(void)fprintf(stderr,
1751553Srgrimes			    "kgmon: profiling not defined in kernel.\n");
1761553Srgrimes			exit(20);
1771553Srgrimes		}
1781553Srgrimes		if (!(bflag || hflag || rflag ||
1791553Srgrimes		    (pflag && state == GMON_PROF_ON)))
1801553Srgrimes			return (O_RDONLY);
1811553Srgrimes		(void)seteuid(0);
1821553Srgrimes		if (sysctl(mib, 3, NULL, NULL, &state, size) >= 0)
1831553Srgrimes			return (O_RDWR);
1841553Srgrimes		(void)seteuid(getuid());
1851553Srgrimes		kern_readonly(state);
1861553Srgrimes		return (O_RDONLY);
1871553Srgrimes	}
1881553Srgrimes	openmode = (bflag || hflag || pflag || rflag) ? O_RDWR : O_RDONLY;
1891553Srgrimes	kvp->kd = kvm_openfiles(system, kmemf, NULL, openmode, errbuf);
1901553Srgrimes	if (kvp->kd == NULL) {
1911553Srgrimes		if (openmode == O_RDWR) {
1921553Srgrimes			openmode = O_RDONLY;
1931553Srgrimes			kvp->kd = kvm_openfiles(system, kmemf, NULL, O_RDONLY,
1941553Srgrimes			    errbuf);
1951553Srgrimes		}
1961553Srgrimes		if (kvp->kd == NULL) {
1971553Srgrimes			(void)fprintf(stderr, "kgmon: kvm_openfiles: %s\n",
1981553Srgrimes			    errbuf);
1991553Srgrimes			exit(2);
2001553Srgrimes		}
2011553Srgrimes		kern_readonly(GMON_PROF_ON);
2021553Srgrimes	}
2031553Srgrimes	if (kvm_nlist(kvp->kd, nl) < 0) {
2041553Srgrimes		(void)fprintf(stderr, "kgmon: %s: no namelist\n", system);
2051553Srgrimes		exit(3);
2061553Srgrimes	}
2071553Srgrimes	if (!nl[N_GMONPARAM].n_value) {
2081553Srgrimes		(void)fprintf(stderr,
2091553Srgrimes		    "kgmon: profiling not defined in kernel.\n");
2101553Srgrimes		exit(20);
2111553Srgrimes	}
2121553Srgrimes	return (openmode);
2131553Srgrimes}
2141553Srgrimes
2151553Srgrimes/*
2161553Srgrimes * Suppress options that require a writable kernel.
2171553Srgrimes */
2181553Srgrimeskern_readonly(mode)
2191553Srgrimes	int mode;
2201553Srgrimes{
2211553Srgrimes
2221553Srgrimes	(void)fprintf(stderr, "kgmon: kernel read-only: ");
2231553Srgrimes	if (pflag && mode == GMON_PROF_ON)
2241553Srgrimes		(void)fprintf(stderr, "data may be inconsistent\n");
2251553Srgrimes	if (rflag)
2261553Srgrimes		(void)fprintf(stderr, "-r supressed\n");
2271553Srgrimes	if (bflag)
2281553Srgrimes		(void)fprintf(stderr, "-b supressed\n");
2291553Srgrimes	if (hflag)
2301553Srgrimes		(void)fprintf(stderr, "-h supressed\n");
2311553Srgrimes	rflag = bflag = hflag = 0;
2321553Srgrimes}
2331553Srgrimes
2341553Srgrimes/*
2351553Srgrimes * Get the state of kernel profiling.
2361553Srgrimes */
2371553Srgrimesgetprof(kvp)
2381553Srgrimes	struct kvmvars *kvp;
2391553Srgrimes{
2401553Srgrimes	int mib[3], size;
2411553Srgrimes
2421553Srgrimes	if (kflag) {
2431553Srgrimes		size = kvm_read(kvp->kd, nl[N_GMONPARAM].n_value, &kvp->gpm,
2441553Srgrimes		    sizeof kvp->gpm);
2451553Srgrimes	} else {
2461553Srgrimes		mib[0] = CTL_KERN;
2471553Srgrimes		mib[1] = KERN_PROF;
2481553Srgrimes		mib[2] = GPROF_GMONPARAM;
2491553Srgrimes		size = sizeof kvp->gpm;
2501553Srgrimes		if (sysctl(mib, 3, &kvp->gpm, &size, NULL, 0) < 0)
2511553Srgrimes			size = 0;
2521553Srgrimes	}
2531553Srgrimes	if (size != sizeof kvp->gpm) {
2541553Srgrimes		(void)fprintf(stderr, "kgmon: cannot get gmonparam: %s\n",
2551553Srgrimes		    kflag ? kvm_geterr(kvp->kd) : strerror(errno));
2561553Srgrimes		exit (4);
2571553Srgrimes	}
2581553Srgrimes	return (kvp->gpm.state);
2591553Srgrimes}
2601553Srgrimes
2611553Srgrimes/*
2621553Srgrimes * Enable or disable kernel profiling according to the state variable.
2631553Srgrimes */
2641553Srgrimesvoid
2651553Srgrimessetprof(kvp, state)
2661553Srgrimes	struct kvmvars *kvp;
2671553Srgrimes	int state;
2681553Srgrimes{
2691553Srgrimes	struct gmonparam *p = (struct gmonparam *)nl[N_GMONPARAM].n_value;
2701553Srgrimes	int mib[3], sz, oldstate;
2711553Srgrimes
2721553Srgrimes	sz = sizeof(state);
2731553Srgrimes	if (!kflag) {
2741553Srgrimes		mib[0] = CTL_KERN;
2751553Srgrimes		mib[1] = KERN_PROF;
2761553Srgrimes		mib[2] = GPROF_STATE;
2771553Srgrimes		if (sysctl(mib, 3, &oldstate, &sz, NULL, 0) < 0)
2781553Srgrimes			goto bad;
2791553Srgrimes		if (oldstate == state)
2801553Srgrimes			return;
2811553Srgrimes		(void)seteuid(0);
2821553Srgrimes		if (sysctl(mib, 3, NULL, NULL, &state, sz) >= 0) {
2831553Srgrimes			(void)seteuid(getuid());
2841553Srgrimes			return;
2851553Srgrimes		}
2861553Srgrimes		(void)seteuid(getuid());
2878857Srgrimes	} else if (kvm_write(kvp->kd, (u_long)&p->state, (void *)&state, sz)
2881553Srgrimes	    == sz)
2891553Srgrimes		return;
2901553Srgrimesbad:
2911553Srgrimes	(void)fprintf(stderr, "kgmon: warning: cannot turn profiling %s\n",
2921553Srgrimes	    state == GMON_PROF_OFF ? "off" : "on");
2931553Srgrimes}
2941553Srgrimes
2951553Srgrimes/*
2961553Srgrimes * Build the gmon.out file.
2971553Srgrimes */
2981553Srgrimesvoid
2991553Srgrimesdumpstate(kvp)
3001553Srgrimes	struct kvmvars *kvp;
3011553Srgrimes{
3021553Srgrimes	register FILE *fp;
3031553Srgrimes	struct rawarc rawarc;
3041553Srgrimes	struct tostruct *tos;
3051553Srgrimes	u_long frompc, addr;
3061553Srgrimes	u_short *froms, *tickbuf;
3071553Srgrimes	int mib[3], i;
3081553Srgrimes	struct gmonhdr h;
3091553Srgrimes	int fromindex, endfrom, toindex;
3101553Srgrimes
3111553Srgrimes	setprof(kvp, GMON_PROF_OFF);
3121553Srgrimes	fp = fopen("gmon.out", "w");
3131553Srgrimes	if (fp == 0) {
3141553Srgrimes		perror("gmon.out");
3151553Srgrimes		return;
3161553Srgrimes	}
3171553Srgrimes
3181553Srgrimes	/*
3191553Srgrimes	 * Build the gmon header and write it to a file.
3201553Srgrimes	 */
3211553Srgrimes	bzero(&h, sizeof(h));
3221553Srgrimes	h.lpc = kvp->gpm.lowpc;
3231553Srgrimes	h.hpc = kvp->gpm.highpc;
3241553Srgrimes	h.ncnt = kvp->gpm.kcountsize + sizeof(h);
3251553Srgrimes	h.version = GMONVERSION;
3261553Srgrimes	h.profrate = getprofhz(kvp);
3271553Srgrimes	fwrite((char *)&h, sizeof(h), 1, fp);
3281553Srgrimes
3291553Srgrimes	/*
3301553Srgrimes	 * Write out the tick buffer.
3311553Srgrimes	 */
3321553Srgrimes	mib[0] = CTL_KERN;
3331553Srgrimes	mib[1] = KERN_PROF;
3341553Srgrimes	if ((tickbuf = (u_short *)malloc(kvp->gpm.kcountsize)) == NULL) {
3351553Srgrimes		fprintf(stderr, "kgmon: cannot allocate kcount space\n");
3361553Srgrimes		exit (5);
3371553Srgrimes	}
3381553Srgrimes	if (kflag) {
3391553Srgrimes		i = kvm_read(kvp->kd, (u_long)kvp->gpm.kcount, (void *)tickbuf,
3401553Srgrimes		    kvp->gpm.kcountsize);
3411553Srgrimes	} else {
3421553Srgrimes		mib[2] = GPROF_COUNT;
3431553Srgrimes		i = kvp->gpm.kcountsize;
3441553Srgrimes		if (sysctl(mib, 3, tickbuf, &i, NULL, 0) < 0)
3451553Srgrimes			i = 0;
3461553Srgrimes	}
3471553Srgrimes	if (i != kvp->gpm.kcountsize) {
3481553Srgrimes		(void)fprintf(stderr, "kgmon: read ticks: read %u, got %d: %s",
3491553Srgrimes		    kvp->gpm.kcountsize, i,
3501553Srgrimes		    kflag ? kvm_geterr(kvp->kd) : strerror(errno));
3511553Srgrimes		exit(6);
3521553Srgrimes	}
3531553Srgrimes	if ((fwrite(tickbuf, kvp->gpm.kcountsize, 1, fp)) != 1) {
3541553Srgrimes		perror("kgmon: writing tocks to gmon.out");
3551553Srgrimes		exit(7);
3561553Srgrimes	}
3571553Srgrimes	free(tickbuf);
3581553Srgrimes
3591553Srgrimes	/*
3601553Srgrimes	 * Write out the arc info.
3611553Srgrimes	 */
3621553Srgrimes	if ((froms = (u_short *)malloc(kvp->gpm.fromssize)) == NULL) {
3631553Srgrimes		fprintf(stderr, "kgmon: cannot allocate froms space\n");
3641553Srgrimes		exit (8);
3651553Srgrimes	}
3661553Srgrimes	if (kflag) {
3671553Srgrimes		i = kvm_read(kvp->kd, (u_long)kvp->gpm.froms, (void *)froms,
3681553Srgrimes		    kvp->gpm.fromssize);
3691553Srgrimes	} else {
3701553Srgrimes		mib[2] = GPROF_FROMS;
3711553Srgrimes		i = kvp->gpm.fromssize;
3721553Srgrimes		if (sysctl(mib, 3, froms, &i, NULL, 0) < 0)
3731553Srgrimes			i = 0;
3741553Srgrimes	}
3751553Srgrimes	if (i != kvp->gpm.fromssize) {
3761553Srgrimes		(void)fprintf(stderr, "kgmon: read froms: read %u, got %d: %s",
3771553Srgrimes		    kvp->gpm.fromssize, i,
3781553Srgrimes		    kflag ? kvm_geterr(kvp->kd) : strerror(errno));
3791553Srgrimes		exit(9);
3801553Srgrimes	}
3811553Srgrimes	if ((tos = (struct tostruct *)malloc(kvp->gpm.tossize)) == NULL) {
3821553Srgrimes		fprintf(stderr, "kgmon: cannot allocate tos space\n");
3831553Srgrimes		exit(10);
3841553Srgrimes	}
3851553Srgrimes	if (kflag) {
3861553Srgrimes		i = kvm_read(kvp->kd, (u_long)kvp->gpm.tos, (void *)tos,
3871553Srgrimes		    kvp->gpm.tossize);
3881553Srgrimes	} else {
3891553Srgrimes		mib[2] = GPROF_TOS;
3901553Srgrimes		i = kvp->gpm.tossize;
3911553Srgrimes		if (sysctl(mib, 3, tos, &i, NULL, 0) < 0)
3921553Srgrimes			i = 0;
3931553Srgrimes	}
3941553Srgrimes	if (i != kvp->gpm.tossize) {
3951553Srgrimes		(void)fprintf(stderr, "kgmon: read tos: read %u, got %d: %s",
3961553Srgrimes		    kvp->gpm.tossize, i,
3971553Srgrimes		    kflag ? kvm_geterr(kvp->kd) : strerror(errno));
3981553Srgrimes		exit(11);
3991553Srgrimes	}
4001553Srgrimes	if (debug)
4011553Srgrimes		(void)fprintf(stderr, "kgmon: lowpc 0x%x, textsize 0x%x\n",
4021553Srgrimes			      kvp->gpm.lowpc, kvp->gpm.textsize);
4031553Srgrimes	endfrom = kvp->gpm.fromssize / sizeof(*froms);
4041553Srgrimes	for (fromindex = 0; fromindex < endfrom; ++fromindex) {
4051553Srgrimes		if (froms[fromindex] == 0)
4061553Srgrimes			continue;
4071553Srgrimes		frompc = (u_long)kvp->gpm.lowpc +
4081553Srgrimes		    (fromindex * kvp->gpm.hashfraction * sizeof(*froms));
4091553Srgrimes		for (toindex = froms[fromindex]; toindex != 0;
4101553Srgrimes		   toindex = tos[toindex].link) {
4111553Srgrimes			if (debug)
4121553Srgrimes			    (void)fprintf(stderr,
4131553Srgrimes			    "%s: [mcleanup] frompc 0x%x selfpc 0x%x count %d\n",
4141553Srgrimes			    "kgmon", frompc, tos[toindex].selfpc,
4151553Srgrimes			    tos[toindex].count);
4161553Srgrimes			rawarc.raw_frompc = frompc;
4171553Srgrimes			rawarc.raw_selfpc = (u_long)tos[toindex].selfpc;
4181553Srgrimes			rawarc.raw_count = tos[toindex].count;
4191553Srgrimes			fwrite((char *)&rawarc, sizeof(rawarc), 1, fp);
4201553Srgrimes		}
4211553Srgrimes	}
4221553Srgrimes	fclose(fp);
4231553Srgrimes}
4241553Srgrimes
4251553Srgrimes/*
4261553Srgrimes * Get the profiling rate.
4271553Srgrimes */
4281553Srgrimesint
4291553Srgrimesgetprofhz(kvp)
4301553Srgrimes	struct kvmvars *kvp;
4311553Srgrimes{
4321553Srgrimes	int mib[2], size, profrate;
4331553Srgrimes	struct clockinfo clockrate;
4341553Srgrimes
4351553Srgrimes	if (kflag) {
4361553Srgrimes		profrate = 1;
4371553Srgrimes		if (kvm_read(kvp->kd, nl[N_PROFHZ].n_value, &profrate,
4381553Srgrimes		    sizeof profrate) != sizeof profrate)
4391553Srgrimes			(void)fprintf(stderr, "kgmon: get clockrate: %s\n",
4401553Srgrimes				kvm_geterr(kvp->kd));
4411553Srgrimes		return (profrate);
4421553Srgrimes	}
4431553Srgrimes	mib[0] = CTL_KERN;
4441553Srgrimes	mib[1] = KERN_CLOCKRATE;
4451553Srgrimes	clockrate.profhz = 1;
4461553Srgrimes	size = sizeof clockrate;
4471553Srgrimes	if (sysctl(mib, 2, &clockrate, &size, NULL, 0) < 0)
4481553Srgrimes		(void)fprintf(stderr, "kgmon: get clockrate: %s\n",
4491553Srgrimes			strerror(errno));
4501553Srgrimes	return (clockrate.profhz);
4511553Srgrimes}
4521553Srgrimes
4531553Srgrimes/*
4541553Srgrimes * Reset the kernel profiling date structures.
4551553Srgrimes */
4561553Srgrimesvoid
4571553Srgrimesreset(kvp)
4581553Srgrimes	struct kvmvars *kvp;
4591553Srgrimes{
4601553Srgrimes	char *zbuf;
4611553Srgrimes	u_long biggest;
4621553Srgrimes	int mib[3];
4631553Srgrimes
4641553Srgrimes	setprof(kvp, GMON_PROF_OFF);
4651553Srgrimes
4661553Srgrimes	biggest = kvp->gpm.kcountsize;
4671553Srgrimes	if (kvp->gpm.fromssize > biggest)
4681553Srgrimes		biggest = kvp->gpm.fromssize;
4691553Srgrimes	if (kvp->gpm.tossize > biggest)
4701553Srgrimes		biggest = kvp->gpm.tossize;
4711553Srgrimes	if ((zbuf = (char *)malloc(biggest)) == NULL) {
4721553Srgrimes		fprintf(stderr, "kgmon: cannot allocate zbuf space\n");
4731553Srgrimes		exit(12);
4741553Srgrimes	}
4751553Srgrimes	bzero(zbuf, biggest);
4761553Srgrimes	if (kflag) {
4771553Srgrimes		if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf,
4781553Srgrimes		    kvp->gpm.kcountsize) != kvp->gpm.kcountsize) {
4791553Srgrimes			(void)fprintf(stderr, "kgmon: tickbuf zero: %s\n",
4801553Srgrimes			    kvm_geterr(kvp->kd));
4811553Srgrimes			exit(13);
4821553Srgrimes		}
4831553Srgrimes		if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf,
4841553Srgrimes		    kvp->gpm.fromssize) != kvp->gpm.fromssize) {
4851553Srgrimes			(void)fprintf(stderr, "kgmon: froms zero: %s\n",
4861553Srgrimes			    kvm_geterr(kvp->kd));
4871553Srgrimes			exit(14);
4881553Srgrimes		}
4891553Srgrimes		if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf,
4901553Srgrimes		    kvp->gpm.tossize) != kvp->gpm.tossize) {
4911553Srgrimes			(void)fprintf(stderr, "kgmon: tos zero: %s\n",
4921553Srgrimes			    kvm_geterr(kvp->kd));
4931553Srgrimes			exit(15);
4941553Srgrimes		}
4951553Srgrimes		return;
4961553Srgrimes	}
4971553Srgrimes	(void)seteuid(0);
4981553Srgrimes	mib[0] = CTL_KERN;
4991553Srgrimes	mib[1] = KERN_PROF;
5001553Srgrimes	mib[2] = GPROF_COUNT;
5011553Srgrimes	if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.kcountsize) < 0) {
5021553Srgrimes		(void)fprintf(stderr, "kgmon: tickbuf zero: %s\n",
5031553Srgrimes		    strerror(errno));
5041553Srgrimes		exit(13);
5051553Srgrimes	}
5061553Srgrimes	mib[2] = GPROF_FROMS;
5071553Srgrimes	if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.fromssize) < 0) {
5081553Srgrimes		(void)fprintf(stderr, "kgmon: froms zero: %s\n",
5091553Srgrimes		    strerror(errno));
5101553Srgrimes		exit(14);
5111553Srgrimes	}
5121553Srgrimes	mib[2] = GPROF_TOS;
5131553Srgrimes	if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.tossize) < 0) {
5141553Srgrimes		(void)fprintf(stderr, "kgmon: tos zero: %s\n",
5151553Srgrimes		    strerror(errno));
5161553Srgrimes		exit(15);
5171553Srgrimes	}
5181553Srgrimes	(void)seteuid(getuid());
5191553Srgrimes	free(zbuf);
5201553Srgrimes}
521