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 * 4. Neither the name of the University nor the names of its contributors 141553Srgrimes * may be used to endorse or promote products derived from this software 151553Srgrimes * without specific prior written permission. 161553Srgrimes * 171553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201553Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271553Srgrimes * SUCH DAMAGE. 281553Srgrimes */ 291553Srgrimes 301553Srgrimes#ifndef lint 3129736Scharnierstatic const char copyright[] = 321553Srgrimes"@(#) Copyright (c) 1983, 1992, 1993\n\ 331553Srgrimes The Regents of the University of California. All rights reserved.\n"; 341553Srgrimes#endif /* not lint */ 351553Srgrimes 361553Srgrimes#ifndef lint 3729736Scharnier#if 0 381553Srgrimesstatic char sccsid[] = "@(#)kgmon.c 8.1 (Berkeley) 6/6/93"; 3929736Scharnier#endif 401553Srgrimes#endif /* not lint */ 411553Srgrimes 42246792Scharnier#include <sys/cdefs.h> 43246792Scharnier__FBSDID("$FreeBSD$"); 44246792Scharnier 451553Srgrimes#include <sys/param.h> 461553Srgrimes#include <sys/file.h> 4711937Sphk#include <sys/time.h> 481553Srgrimes#include <sys/sysctl.h> 491553Srgrimes#include <sys/gmon.h> 5029736Scharnier#include <ctype.h> 5129736Scharnier#include <err.h> 521553Srgrimes#include <errno.h> 531553Srgrimes#include <kvm.h> 541553Srgrimes#include <limits.h> 5529736Scharnier#include <nlist.h> 5629736Scharnier#include <paths.h> 5791009Sbde#include <stddef.h> 581553Srgrimes#include <stdio.h> 591553Srgrimes#include <stdlib.h> 601553Srgrimes#include <string.h> 6129736Scharnier#include <unistd.h> 621553Srgrimes 631553Srgrimesstruct nlist nl[] = { 641553Srgrimes#define N_GMONPARAM 0 651553Srgrimes { "__gmonparam" }, 661553Srgrimes#define N_PROFHZ 1 671553Srgrimes { "_profhz" }, 6829736Scharnier { NULL }, 691553Srgrimes}; 701553Srgrimes 711553Srgrimesstruct kvmvars { 721553Srgrimes kvm_t *kd; 731553Srgrimes struct gmonparam gpm; 741553Srgrimes}; 751553Srgrimes 7613107Sbdeint Bflag, bflag, hflag, kflag, rflag, pflag; 771553Srgrimesint debug = 0; 7899800Salfredint getprof(struct kvmvars *); 7999800Salfredint getprofhz(struct kvmvars *); 8099800Salfredvoid kern_readonly(int); 81246792Scharnierint openfiles(const char *, char *, struct kvmvars *); 8299800Salfredvoid setprof(struct kvmvars *kvp, int state); 8399800Salfredvoid dumpstate(struct kvmvars *kvp); 8499800Salfredvoid reset(struct kvmvars *kvp); 8599800Salfredstatic void usage(void); 861553Srgrimes 871553Srgrimesint 881553Srgrimesmain(int argc, char **argv) 891553Srgrimes{ 901553Srgrimes int ch, mode, disp, accessmode; 911553Srgrimes struct kvmvars kvmvars; 92246792Scharnier const char *systemname; 93246792Scharnier char *kmemf; 941553Srgrimes 95241848Seadler if (seteuid(getuid()) != 0) { 96241848Seadler err(1, "seteuid failed\n"); 97241848Seadler } 981553Srgrimes kmemf = NULL; 99246792Scharnier systemname = NULL; 10024428Simp while ((ch = getopt(argc, argv, "M:N:Bbhpr")) != -1) { 1011553Srgrimes switch((char)ch) { 1021553Srgrimes 1031553Srgrimes case 'M': 1041553Srgrimes kmemf = optarg; 1051553Srgrimes kflag = 1; 1061553Srgrimes break; 1071553Srgrimes 1081553Srgrimes case 'N': 109246792Scharnier systemname = optarg; 1101553Srgrimes break; 1111553Srgrimes 11213107Sbde case 'B': 11313107Sbde Bflag = 1; 11413107Sbde break; 11513107Sbde 1161553Srgrimes case 'b': 1171553Srgrimes bflag = 1; 1181553Srgrimes break; 1191553Srgrimes 1201553Srgrimes case 'h': 1211553Srgrimes hflag = 1; 1221553Srgrimes break; 1231553Srgrimes 1241553Srgrimes case 'p': 1251553Srgrimes pflag = 1; 1261553Srgrimes break; 1271553Srgrimes 1281553Srgrimes case 'r': 1291553Srgrimes rflag = 1; 1301553Srgrimes break; 1311553Srgrimes 1321553Srgrimes default: 13329736Scharnier usage(); 1341553Srgrimes } 1351553Srgrimes } 1361553Srgrimes argc -= optind; 1371553Srgrimes argv += optind; 1381553Srgrimes 1391553Srgrimes#define BACKWARD_COMPATIBILITY 1401553Srgrimes#ifdef BACKWARD_COMPATIBILITY 1411553Srgrimes if (*argv) { 142246792Scharnier systemname = *argv; 1431553Srgrimes if (*++argv) { 1441553Srgrimes kmemf = *argv; 1451553Srgrimes ++kflag; 1461553Srgrimes } 1471553Srgrimes } 1481553Srgrimes#endif 149246792Scharnier if (systemname == NULL) 150246792Scharnier systemname = getbootfile(); 151246792Scharnier accessmode = openfiles(systemname, kmemf, &kvmvars); 1521553Srgrimes mode = getprof(&kvmvars); 1531553Srgrimes if (hflag) 1541553Srgrimes disp = GMON_PROF_OFF; 15513107Sbde else if (Bflag) 15613107Sbde disp = GMON_PROF_HIRES; 1571553Srgrimes else if (bflag) 1581553Srgrimes disp = GMON_PROF_ON; 1591553Srgrimes else 1601553Srgrimes disp = mode; 1611553Srgrimes if (pflag) 1621553Srgrimes dumpstate(&kvmvars); 1631553Srgrimes if (rflag) 1641553Srgrimes reset(&kvmvars); 1651553Srgrimes if (accessmode == O_RDWR) 1661553Srgrimes setprof(&kvmvars, disp); 1671553Srgrimes (void)fprintf(stdout, "kgmon: kernel profiling is %s.\n", 16813107Sbde disp == GMON_PROF_OFF ? "off" : 16913107Sbde disp == GMON_PROF_HIRES ? "running (high resolution)" : 17013107Sbde disp == GMON_PROF_ON ? "running" : 17113107Sbde disp == GMON_PROF_BUSY ? "busy" : 17213107Sbde disp == GMON_PROF_ERROR ? "off (error)" : 17313107Sbde "in an unknown state"); 1741553Srgrimes return (0); 1751553Srgrimes} 1761553Srgrimes 17729736Scharnierstatic void 178246792Scharnierusage(void) 17929736Scharnier{ 18029736Scharnier fprintf(stderr, "usage: kgmon [-Bbhrp] [-M core] [-N system]\n"); 18129736Scharnier exit(1); 18229736Scharnier} 18329736Scharnier 1841553Srgrimes/* 185160022Sdelphij * Check that profiling is enabled and open any necessary files. 1861553Srgrimes */ 18729736Scharnierint 188246792Scharnieropenfiles(const char *systemname, char *kmemf, struct kvmvars *kvp) 1891553Srgrimes{ 190100904Sjake size_t size; 191100904Sjake int mib[3], state, openmode; 1921553Srgrimes char errbuf[_POSIX2_LINE_MAX]; 1931553Srgrimes 1941553Srgrimes if (!kflag) { 1951553Srgrimes mib[0] = CTL_KERN; 1961553Srgrimes mib[1] = KERN_PROF; 1971553Srgrimes mib[2] = GPROF_STATE; 1981553Srgrimes size = sizeof state; 19929736Scharnier if (sysctl(mib, 3, &state, &size, NULL, 0) < 0) 20029736Scharnier errx(20, "profiling not defined in kernel"); 20113107Sbde if (!(Bflag || bflag || hflag || rflag || 20213107Sbde (pflag && 20313107Sbde (state == GMON_PROF_HIRES || state == GMON_PROF_ON)))) 2041553Srgrimes return (O_RDONLY); 2051553Srgrimes (void)seteuid(0); 2061553Srgrimes if (sysctl(mib, 3, NULL, NULL, &state, size) >= 0) 2071553Srgrimes return (O_RDWR); 2081553Srgrimes (void)seteuid(getuid()); 2091553Srgrimes kern_readonly(state); 2101553Srgrimes return (O_RDONLY); 2111553Srgrimes } 21213107Sbde openmode = (Bflag || bflag || hflag || pflag || rflag) 21313107Sbde ? O_RDWR : O_RDONLY; 214246792Scharnier kvp->kd = kvm_openfiles(systemname, kmemf, NULL, openmode, errbuf); 2151553Srgrimes if (kvp->kd == NULL) { 2161553Srgrimes if (openmode == O_RDWR) { 2171553Srgrimes openmode = O_RDONLY; 218246792Scharnier kvp->kd = kvm_openfiles(systemname, kmemf, NULL, O_RDONLY, 2191553Srgrimes errbuf); 2201553Srgrimes } 22129736Scharnier if (kvp->kd == NULL) 22229736Scharnier errx(2, "kvm_openfiles: %s", errbuf); 2231553Srgrimes kern_readonly(GMON_PROF_ON); 2241553Srgrimes } 22529736Scharnier if (kvm_nlist(kvp->kd, nl) < 0) 226246792Scharnier errx(3, "%s: no namelist", systemname); 22729736Scharnier if (!nl[N_GMONPARAM].n_value) 22829736Scharnier errx(20, "profiling not defined in kernel"); 2291553Srgrimes return (openmode); 2301553Srgrimes} 2311553Srgrimes 2321553Srgrimes/* 2331553Srgrimes * Suppress options that require a writable kernel. 2341553Srgrimes */ 23529736Scharniervoid 236246792Scharnierkern_readonly(int mode) 2371553Srgrimes{ 2381553Srgrimes 2391553Srgrimes (void)fprintf(stderr, "kgmon: kernel read-only: "); 24013107Sbde if (pflag && (mode == GMON_PROF_HIRES || mode == GMON_PROF_ON)) 2411553Srgrimes (void)fprintf(stderr, "data may be inconsistent\n"); 2421553Srgrimes if (rflag) 243228990Suqs (void)fprintf(stderr, "-r suppressed\n"); 24413107Sbde if (Bflag) 245228990Suqs (void)fprintf(stderr, "-B suppressed\n"); 2461553Srgrimes if (bflag) 247228990Suqs (void)fprintf(stderr, "-b suppressed\n"); 2481553Srgrimes if (hflag) 249228990Suqs (void)fprintf(stderr, "-h suppressed\n"); 25013107Sbde rflag = Bflag = bflag = hflag = 0; 2511553Srgrimes} 2521553Srgrimes 2531553Srgrimes/* 2541553Srgrimes * Get the state of kernel profiling. 2551553Srgrimes */ 25629736Scharnierint 257246792Scharniergetprof(struct kvmvars *kvp) 2581553Srgrimes{ 259100904Sjake size_t size; 260100904Sjake int mib[3]; 2611553Srgrimes 2621553Srgrimes if (kflag) { 2631553Srgrimes size = kvm_read(kvp->kd, nl[N_GMONPARAM].n_value, &kvp->gpm, 2641553Srgrimes sizeof kvp->gpm); 2651553Srgrimes } else { 2661553Srgrimes mib[0] = CTL_KERN; 2671553Srgrimes mib[1] = KERN_PROF; 2681553Srgrimes mib[2] = GPROF_GMONPARAM; 2691553Srgrimes size = sizeof kvp->gpm; 2701553Srgrimes if (sysctl(mib, 3, &kvp->gpm, &size, NULL, 0) < 0) 2711553Srgrimes size = 0; 2721553Srgrimes } 27391009Sbde 27491009Sbde /* 27591009Sbde * Accept certain undersized "structs" from old kernels. We need 27691009Sbde * everything up to hashfraction, and want profrate and 27791009Sbde * histcounter_type. Assume that the kernel doesn't put garbage 27891009Sbde * in any padding that is returned instead of profrate and 27991009Sbde * histcounter_type. This is a bad assumption for dead kernels, 28091009Sbde * since kvm_read() will normally return garbage for bytes beyond 28191009Sbde * the end of the actual kernel struct, if any. 28291009Sbde */ 28391009Sbde if (size < offsetof(struct gmonparam, hashfraction) + 28491009Sbde sizeof(kvp->gpm.hashfraction) || size > sizeof(kvp->gpm)) 28529736Scharnier errx(4, "cannot get gmonparam: %s", 2861553Srgrimes kflag ? kvm_geterr(kvp->kd) : strerror(errno)); 28791009Sbde bzero((char *)&kvp->gpm + size, sizeof(kvp->gpm) - size); 28891009Sbde if (kvp->gpm.profrate == 0) 28991009Sbde kvp->gpm.profrate = getprofhz(kvp); 29091009Sbde#ifdef __i386__ 29191009Sbde if (kvp->gpm.histcounter_type == 0) { 29291009Sbde /* 29391009Sbde * This fixup only works for not-so-old i386 kernels. The 29491009Sbde * magic 16 is the kernel FUNCTION_ALIGNMENT. 64-bit 29591009Sbde * counters are signed; smaller counters are unsigned. 29691009Sbde */ 29791009Sbde kvp->gpm.histcounter_type = 16 / 29891009Sbde (kvp->gpm.textsize / kvp->gpm.kcountsize) * CHAR_BIT; 29991009Sbde if (kvp->gpm.histcounter_type == 64) 30091009Sbde kvp->gpm.histcounter_type = -64; 30191009Sbde } 30291009Sbde#endif 30391009Sbde 3041553Srgrimes return (kvp->gpm.state); 3051553Srgrimes} 3061553Srgrimes 3071553Srgrimes/* 3081553Srgrimes * Enable or disable kernel profiling according to the state variable. 3091553Srgrimes */ 3101553Srgrimesvoid 311246792Scharniersetprof(struct kvmvars *kvp, int state) 3121553Srgrimes{ 3131553Srgrimes struct gmonparam *p = (struct gmonparam *)nl[N_GMONPARAM].n_value; 314100904Sjake size_t sz; 315100904Sjake int mib[3], oldstate; 3161553Srgrimes 3171553Srgrimes sz = sizeof(state); 3181553Srgrimes if (!kflag) { 3191553Srgrimes mib[0] = CTL_KERN; 3201553Srgrimes mib[1] = KERN_PROF; 3211553Srgrimes mib[2] = GPROF_STATE; 3221553Srgrimes if (sysctl(mib, 3, &oldstate, &sz, NULL, 0) < 0) 3231553Srgrimes goto bad; 3241553Srgrimes if (oldstate == state) 3251553Srgrimes return; 3261553Srgrimes (void)seteuid(0); 3271553Srgrimes if (sysctl(mib, 3, NULL, NULL, &state, sz) >= 0) { 3281553Srgrimes (void)seteuid(getuid()); 3291553Srgrimes return; 3301553Srgrimes } 3311553Srgrimes (void)seteuid(getuid()); 3328857Srgrimes } else if (kvm_write(kvp->kd, (u_long)&p->state, (void *)&state, sz) 333246792Scharnier == (ssize_t)sz) 3341553Srgrimes return; 3351553Srgrimesbad: 33629736Scharnier warnx("warning: cannot turn profiling %s", 3371553Srgrimes state == GMON_PROF_OFF ? "off" : "on"); 3381553Srgrimes} 3391553Srgrimes 3401553Srgrimes/* 3411553Srgrimes * Build the gmon.out file. 3421553Srgrimes */ 3431553Srgrimesvoid 344246792Scharnierdumpstate(struct kvmvars *kvp) 3451553Srgrimes{ 3461553Srgrimes register FILE *fp; 3471553Srgrimes struct rawarc rawarc; 3481553Srgrimes struct tostruct *tos; 34929736Scharnier u_long frompc; 3501553Srgrimes u_short *froms, *tickbuf; 351100904Sjake size_t i; 352100904Sjake int mib[3]; 3531553Srgrimes struct gmonhdr h; 3541553Srgrimes int fromindex, endfrom, toindex; 3551553Srgrimes 3561553Srgrimes setprof(kvp, GMON_PROF_OFF); 3571553Srgrimes fp = fopen("gmon.out", "w"); 3581553Srgrimes if (fp == 0) { 35929736Scharnier warn("gmon.out"); 3601553Srgrimes return; 3611553Srgrimes } 3621553Srgrimes 3631553Srgrimes /* 3641553Srgrimes * Build the gmon header and write it to a file. 3651553Srgrimes */ 3661553Srgrimes bzero(&h, sizeof(h)); 3671553Srgrimes h.lpc = kvp->gpm.lowpc; 3681553Srgrimes h.hpc = kvp->gpm.highpc; 3691553Srgrimes h.ncnt = kvp->gpm.kcountsize + sizeof(h); 3701553Srgrimes h.version = GMONVERSION; 37113107Sbde h.profrate = kvp->gpm.profrate; 37291009Sbde h.histcounter_type = kvp->gpm.histcounter_type; 3731553Srgrimes fwrite((char *)&h, sizeof(h), 1, fp); 3741553Srgrimes 3751553Srgrimes /* 3761553Srgrimes * Write out the tick buffer. 3771553Srgrimes */ 3781553Srgrimes mib[0] = CTL_KERN; 3791553Srgrimes mib[1] = KERN_PROF; 38029736Scharnier if ((tickbuf = (u_short *)malloc(kvp->gpm.kcountsize)) == NULL) 38129736Scharnier errx(5, "cannot allocate kcount space"); 3821553Srgrimes if (kflag) { 3831553Srgrimes i = kvm_read(kvp->kd, (u_long)kvp->gpm.kcount, (void *)tickbuf, 3841553Srgrimes kvp->gpm.kcountsize); 3851553Srgrimes } else { 3861553Srgrimes mib[2] = GPROF_COUNT; 3871553Srgrimes i = kvp->gpm.kcountsize; 3881553Srgrimes if (sysctl(mib, 3, tickbuf, &i, NULL, 0) < 0) 3891553Srgrimes i = 0; 3901553Srgrimes } 39129736Scharnier if (i != kvp->gpm.kcountsize) 392134505Smarcel errx(6, "read ticks: read %lu, got %ld: %s", 393134505Smarcel kvp->gpm.kcountsize, (long)i, 3941553Srgrimes kflag ? kvm_geterr(kvp->kd) : strerror(errno)); 39529736Scharnier if ((fwrite(tickbuf, kvp->gpm.kcountsize, 1, fp)) != 1) 39629736Scharnier err(7, "writing tocks to gmon.out"); 3971553Srgrimes free(tickbuf); 3981553Srgrimes 3991553Srgrimes /* 4001553Srgrimes * Write out the arc info. 4011553Srgrimes */ 40229736Scharnier if ((froms = (u_short *)malloc(kvp->gpm.fromssize)) == NULL) 40329736Scharnier errx(8, "cannot allocate froms space"); 4041553Srgrimes if (kflag) { 4051553Srgrimes i = kvm_read(kvp->kd, (u_long)kvp->gpm.froms, (void *)froms, 4061553Srgrimes kvp->gpm.fromssize); 4071553Srgrimes } else { 4081553Srgrimes mib[2] = GPROF_FROMS; 4091553Srgrimes i = kvp->gpm.fromssize; 4101553Srgrimes if (sysctl(mib, 3, froms, &i, NULL, 0) < 0) 4111553Srgrimes i = 0; 4121553Srgrimes } 41329736Scharnier if (i != kvp->gpm.fromssize) 414134505Smarcel errx(9, "read froms: read %lu, got %ld: %s", 415134505Smarcel kvp->gpm.fromssize, (long)i, 4161553Srgrimes kflag ? kvm_geterr(kvp->kd) : strerror(errno)); 41729736Scharnier if ((tos = (struct tostruct *)malloc(kvp->gpm.tossize)) == NULL) 41829736Scharnier errx(10, "cannot allocate tos space"); 4191553Srgrimes if (kflag) { 4201553Srgrimes i = kvm_read(kvp->kd, (u_long)kvp->gpm.tos, (void *)tos, 4211553Srgrimes kvp->gpm.tossize); 4221553Srgrimes } else { 4231553Srgrimes mib[2] = GPROF_TOS; 4241553Srgrimes i = kvp->gpm.tossize; 4251553Srgrimes if (sysctl(mib, 3, tos, &i, NULL, 0) < 0) 4261553Srgrimes i = 0; 4271553Srgrimes } 42829736Scharnier if (i != kvp->gpm.tossize) 429134505Smarcel errx(11, "read tos: read %lu, got %ld: %s", 430134505Smarcel kvp->gpm.tossize, (long)i, 4311553Srgrimes kflag ? kvm_geterr(kvp->kd) : strerror(errno)); 4321553Srgrimes if (debug) 433134505Smarcel warnx("lowpc 0x%lx, textsize 0x%lx", 434134505Smarcel (unsigned long)kvp->gpm.lowpc, kvp->gpm.textsize); 4351553Srgrimes endfrom = kvp->gpm.fromssize / sizeof(*froms); 4361553Srgrimes for (fromindex = 0; fromindex < endfrom; ++fromindex) { 4371553Srgrimes if (froms[fromindex] == 0) 4381553Srgrimes continue; 4391553Srgrimes frompc = (u_long)kvp->gpm.lowpc + 4401553Srgrimes (fromindex * kvp->gpm.hashfraction * sizeof(*froms)); 4411553Srgrimes for (toindex = froms[fromindex]; toindex != 0; 442134505Smarcel toindex = tos[toindex].link) { 4431553Srgrimes if (debug) 444134505Smarcel warnx("[mcleanup] frompc 0x%lx selfpc 0x%lx " 445134505Smarcel "count %ld", frompc, tos[toindex].selfpc, 446134505Smarcel tos[toindex].count); 4471553Srgrimes rawarc.raw_frompc = frompc; 4481553Srgrimes rawarc.raw_selfpc = (u_long)tos[toindex].selfpc; 4491553Srgrimes rawarc.raw_count = tos[toindex].count; 4501553Srgrimes fwrite((char *)&rawarc, sizeof(rawarc), 1, fp); 4511553Srgrimes } 4521553Srgrimes } 4531553Srgrimes fclose(fp); 4541553Srgrimes} 4551553Srgrimes 4561553Srgrimes/* 4571553Srgrimes * Get the profiling rate. 4581553Srgrimes */ 4591553Srgrimesint 460246792Scharniergetprofhz(struct kvmvars *kvp) 4611553Srgrimes{ 462100904Sjake size_t size; 463100904Sjake int mib[2], profrate; 4641553Srgrimes struct clockinfo clockrate; 4651553Srgrimes 4661553Srgrimes if (kflag) { 4671553Srgrimes profrate = 1; 4681553Srgrimes if (kvm_read(kvp->kd, nl[N_PROFHZ].n_value, &profrate, 4691553Srgrimes sizeof profrate) != sizeof profrate) 47029736Scharnier warnx("get clockrate: %s", kvm_geterr(kvp->kd)); 4711553Srgrimes return (profrate); 4721553Srgrimes } 4731553Srgrimes mib[0] = CTL_KERN; 4741553Srgrimes mib[1] = KERN_CLOCKRATE; 4751553Srgrimes clockrate.profhz = 1; 4761553Srgrimes size = sizeof clockrate; 4771553Srgrimes if (sysctl(mib, 2, &clockrate, &size, NULL, 0) < 0) 47829736Scharnier warn("get clockrate"); 4791553Srgrimes return (clockrate.profhz); 4801553Srgrimes} 4811553Srgrimes 4821553Srgrimes/* 4831553Srgrimes * Reset the kernel profiling date structures. 4841553Srgrimes */ 4851553Srgrimesvoid 486246792Scharnierreset(struct kvmvars *kvp) 4871553Srgrimes{ 4881553Srgrimes char *zbuf; 4891553Srgrimes u_long biggest; 4901553Srgrimes int mib[3]; 4911553Srgrimes 4921553Srgrimes setprof(kvp, GMON_PROF_OFF); 4931553Srgrimes 4941553Srgrimes biggest = kvp->gpm.kcountsize; 4951553Srgrimes if (kvp->gpm.fromssize > biggest) 4961553Srgrimes biggest = kvp->gpm.fromssize; 4971553Srgrimes if (kvp->gpm.tossize > biggest) 4981553Srgrimes biggest = kvp->gpm.tossize; 49929736Scharnier if ((zbuf = (char *)malloc(biggest)) == NULL) 50029736Scharnier errx(12, "cannot allocate zbuf space"); 5011553Srgrimes bzero(zbuf, biggest); 5021553Srgrimes if (kflag) { 5031553Srgrimes if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf, 504246792Scharnier kvp->gpm.kcountsize) != (ssize_t)kvp->gpm.kcountsize) 50529736Scharnier errx(13, "tickbuf zero: %s", kvm_geterr(kvp->kd)); 5061553Srgrimes if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf, 507246792Scharnier kvp->gpm.fromssize) != (ssize_t)kvp->gpm.fromssize) 50829736Scharnier errx(14, "froms zero: %s", kvm_geterr(kvp->kd)); 5091553Srgrimes if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf, 510246792Scharnier kvp->gpm.tossize) != (ssize_t)kvp->gpm.tossize) 51129736Scharnier errx(15, "tos zero: %s", kvm_geterr(kvp->kd)); 5121553Srgrimes return; 5131553Srgrimes } 5141553Srgrimes (void)seteuid(0); 5151553Srgrimes mib[0] = CTL_KERN; 5161553Srgrimes mib[1] = KERN_PROF; 5171553Srgrimes mib[2] = GPROF_COUNT; 51829736Scharnier if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.kcountsize) < 0) 51929736Scharnier err(13, "tickbuf zero"); 5201553Srgrimes mib[2] = GPROF_FROMS; 52129736Scharnier if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.fromssize) < 0) 52229736Scharnier err(14, "froms zero"); 5231553Srgrimes mib[2] = GPROF_TOS; 52429736Scharnier if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.tossize) < 0) 52529736Scharnier err(15, "tos zero"); 5261553Srgrimes (void)seteuid(getuid()); 5271553Srgrimes free(zbuf); 5281553Srgrimes} 529