kgmon.c revision 91009
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 3529736Scharnierstatic const 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 4129736Scharnier#if 0 421553Srgrimesstatic char sccsid[] = "@(#)kgmon.c 8.1 (Berkeley) 6/6/93"; 4329736Scharnier#endif 4429736Scharnierstatic const char rcsid[] = 4550479Speter "$FreeBSD: head/usr.sbin/kgmon/kgmon.c 91009 2002-02-21 05:52:49Z bde $"; 461553Srgrimes#endif /* not lint */ 471553Srgrimes 481553Srgrimes#include <sys/param.h> 491553Srgrimes#include <sys/file.h> 5011937Sphk#include <sys/time.h> 511553Srgrimes#include <sys/sysctl.h> 521553Srgrimes#include <sys/gmon.h> 5329736Scharnier#include <ctype.h> 5429736Scharnier#include <err.h> 551553Srgrimes#include <errno.h> 561553Srgrimes#include <kvm.h> 571553Srgrimes#include <limits.h> 5829736Scharnier#include <nlist.h> 5929736Scharnier#include <paths.h> 6091009Sbde#include <stddef.h> 611553Srgrimes#include <stdio.h> 621553Srgrimes#include <stdlib.h> 631553Srgrimes#include <string.h> 6429736Scharnier#include <unistd.h> 651553Srgrimes 661553Srgrimesstruct nlist nl[] = { 671553Srgrimes#define N_GMONPARAM 0 681553Srgrimes { "__gmonparam" }, 691553Srgrimes#define N_PROFHZ 1 701553Srgrimes { "_profhz" }, 7129736Scharnier { NULL }, 721553Srgrimes}; 731553Srgrimes 741553Srgrimesstruct kvmvars { 751553Srgrimes kvm_t *kd; 761553Srgrimes struct gmonparam gpm; 771553Srgrimes}; 781553Srgrimes 7913107Sbdeint Bflag, bflag, hflag, kflag, rflag, pflag; 801553Srgrimesint debug = 0; 8129736Scharnierint getprof __P((struct kvmvars *)); 8229736Scharnierint getprofhz __P((struct kvmvars *)); 8329736Scharniervoid kern_readonly __P((int)); 8429736Scharnierint openfiles __P((char *, char *, struct kvmvars *)); 851553Srgrimesvoid setprof __P((struct kvmvars *kvp, int state)); 861553Srgrimesvoid dumpstate __P((struct kvmvars *kvp)); 871553Srgrimesvoid reset __P((struct kvmvars *kvp)); 8829736Scharnierstatic void usage __P((void)); 891553Srgrimes 901553Srgrimesint 911553Srgrimesmain(int argc, char **argv) 921553Srgrimes{ 931553Srgrimes int ch, mode, disp, accessmode; 941553Srgrimes struct kvmvars kvmvars; 951553Srgrimes char *system, *kmemf; 961553Srgrimes 971553Srgrimes seteuid(getuid()); 981553Srgrimes kmemf = NULL; 991553Srgrimes system = 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': 1091553Srgrimes system = 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) { 1421553Srgrimes system = *argv; 1431553Srgrimes if (*++argv) { 1441553Srgrimes kmemf = *argv; 1451553Srgrimes ++kflag; 1461553Srgrimes } 1471553Srgrimes } 1481553Srgrimes#endif 1491553Srgrimes if (system == NULL) 1503041Swollman system = (char *)getbootfile(); 1511553Srgrimes accessmode = openfiles(system, 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 17829736Scharnierusage() 17929736Scharnier{ 18029736Scharnier fprintf(stderr, "usage: kgmon [-Bbhrp] [-M core] [-N system]\n"); 18129736Scharnier exit(1); 18229736Scharnier} 18329736Scharnier 1841553Srgrimes/* 1851553Srgrimes * Check that profiling is enabled and open any ncessary files. 1861553Srgrimes */ 18729736Scharnierint 1881553Srgrimesopenfiles(system, kmemf, kvp) 1891553Srgrimes char *system; 1901553Srgrimes char *kmemf; 1911553Srgrimes struct kvmvars *kvp; 1921553Srgrimes{ 1931553Srgrimes int mib[3], state, size, openmode; 1941553Srgrimes char errbuf[_POSIX2_LINE_MAX]; 1951553Srgrimes 1961553Srgrimes if (!kflag) { 1971553Srgrimes mib[0] = CTL_KERN; 1981553Srgrimes mib[1] = KERN_PROF; 1991553Srgrimes mib[2] = GPROF_STATE; 2001553Srgrimes size = sizeof state; 20129736Scharnier if (sysctl(mib, 3, &state, &size, NULL, 0) < 0) 20229736Scharnier errx(20, "profiling not defined in kernel"); 20313107Sbde if (!(Bflag || bflag || hflag || rflag || 20413107Sbde (pflag && 20513107Sbde (state == GMON_PROF_HIRES || state == GMON_PROF_ON)))) 2061553Srgrimes return (O_RDONLY); 2071553Srgrimes (void)seteuid(0); 2081553Srgrimes if (sysctl(mib, 3, NULL, NULL, &state, size) >= 0) 2091553Srgrimes return (O_RDWR); 2101553Srgrimes (void)seteuid(getuid()); 2111553Srgrimes kern_readonly(state); 2121553Srgrimes return (O_RDONLY); 2131553Srgrimes } 21413107Sbde openmode = (Bflag || bflag || hflag || pflag || rflag) 21513107Sbde ? O_RDWR : O_RDONLY; 2161553Srgrimes kvp->kd = kvm_openfiles(system, kmemf, NULL, openmode, errbuf); 2171553Srgrimes if (kvp->kd == NULL) { 2181553Srgrimes if (openmode == O_RDWR) { 2191553Srgrimes openmode = O_RDONLY; 2201553Srgrimes kvp->kd = kvm_openfiles(system, kmemf, NULL, O_RDONLY, 2211553Srgrimes errbuf); 2221553Srgrimes } 22329736Scharnier if (kvp->kd == NULL) 22429736Scharnier errx(2, "kvm_openfiles: %s", errbuf); 2251553Srgrimes kern_readonly(GMON_PROF_ON); 2261553Srgrimes } 22729736Scharnier if (kvm_nlist(kvp->kd, nl) < 0) 22829736Scharnier errx(3, "%s: no namelist", system); 22929736Scharnier if (!nl[N_GMONPARAM].n_value) 23029736Scharnier errx(20, "profiling not defined in kernel"); 2311553Srgrimes return (openmode); 2321553Srgrimes} 2331553Srgrimes 2341553Srgrimes/* 2351553Srgrimes * Suppress options that require a writable kernel. 2361553Srgrimes */ 23729736Scharniervoid 2381553Srgrimeskern_readonly(mode) 2391553Srgrimes int mode; 2401553Srgrimes{ 2411553Srgrimes 2421553Srgrimes (void)fprintf(stderr, "kgmon: kernel read-only: "); 24313107Sbde if (pflag && (mode == GMON_PROF_HIRES || mode == GMON_PROF_ON)) 2441553Srgrimes (void)fprintf(stderr, "data may be inconsistent\n"); 2451553Srgrimes if (rflag) 2461553Srgrimes (void)fprintf(stderr, "-r supressed\n"); 24713107Sbde if (Bflag) 24813107Sbde (void)fprintf(stderr, "-B supressed\n"); 2491553Srgrimes if (bflag) 2501553Srgrimes (void)fprintf(stderr, "-b supressed\n"); 2511553Srgrimes if (hflag) 2521553Srgrimes (void)fprintf(stderr, "-h supressed\n"); 25313107Sbde rflag = Bflag = bflag = hflag = 0; 2541553Srgrimes} 2551553Srgrimes 2561553Srgrimes/* 2571553Srgrimes * Get the state of kernel profiling. 2581553Srgrimes */ 25929736Scharnierint 2601553Srgrimesgetprof(kvp) 2611553Srgrimes struct kvmvars *kvp; 2621553Srgrimes{ 2631553Srgrimes int mib[3], size; 2641553Srgrimes 2651553Srgrimes if (kflag) { 2661553Srgrimes size = kvm_read(kvp->kd, nl[N_GMONPARAM].n_value, &kvp->gpm, 2671553Srgrimes sizeof kvp->gpm); 2681553Srgrimes } else { 2691553Srgrimes mib[0] = CTL_KERN; 2701553Srgrimes mib[1] = KERN_PROF; 2711553Srgrimes mib[2] = GPROF_GMONPARAM; 2721553Srgrimes size = sizeof kvp->gpm; 2731553Srgrimes if (sysctl(mib, 3, &kvp->gpm, &size, NULL, 0) < 0) 2741553Srgrimes size = 0; 2751553Srgrimes } 27691009Sbde 27791009Sbde /* 27891009Sbde * Accept certain undersized "structs" from old kernels. We need 27991009Sbde * everything up to hashfraction, and want profrate and 28091009Sbde * histcounter_type. Assume that the kernel doesn't put garbage 28191009Sbde * in any padding that is returned instead of profrate and 28291009Sbde * histcounter_type. This is a bad assumption for dead kernels, 28391009Sbde * since kvm_read() will normally return garbage for bytes beyond 28491009Sbde * the end of the actual kernel struct, if any. 28591009Sbde */ 28691009Sbde if (size < offsetof(struct gmonparam, hashfraction) + 28791009Sbde sizeof(kvp->gpm.hashfraction) || size > sizeof(kvp->gpm)) 28829736Scharnier errx(4, "cannot get gmonparam: %s", 2891553Srgrimes kflag ? kvm_geterr(kvp->kd) : strerror(errno)); 29091009Sbde bzero((char *)&kvp->gpm + size, sizeof(kvp->gpm) - size); 29191009Sbde if (kvp->gpm.profrate == 0) 29291009Sbde kvp->gpm.profrate = getprofhz(kvp); 29391009Sbde#ifdef __i386__ 29491009Sbde if (kvp->gpm.histcounter_type == 0) { 29591009Sbde /* 29691009Sbde * This fixup only works for not-so-old i386 kernels. The 29791009Sbde * magic 16 is the kernel FUNCTION_ALIGNMENT. 64-bit 29891009Sbde * counters are signed; smaller counters are unsigned. 29991009Sbde */ 30091009Sbde kvp->gpm.histcounter_type = 16 / 30191009Sbde (kvp->gpm.textsize / kvp->gpm.kcountsize) * CHAR_BIT; 30291009Sbde if (kvp->gpm.histcounter_type == 64) 30391009Sbde kvp->gpm.histcounter_type = -64; 30491009Sbde } 30591009Sbde#endif 30691009Sbde 3071553Srgrimes return (kvp->gpm.state); 3081553Srgrimes} 3091553Srgrimes 3101553Srgrimes/* 3111553Srgrimes * Enable or disable kernel profiling according to the state variable. 3121553Srgrimes */ 3131553Srgrimesvoid 3141553Srgrimessetprof(kvp, state) 3151553Srgrimes struct kvmvars *kvp; 3161553Srgrimes int state; 3171553Srgrimes{ 3181553Srgrimes struct gmonparam *p = (struct gmonparam *)nl[N_GMONPARAM].n_value; 3191553Srgrimes int mib[3], sz, oldstate; 3201553Srgrimes 3211553Srgrimes sz = sizeof(state); 3221553Srgrimes if (!kflag) { 3231553Srgrimes mib[0] = CTL_KERN; 3241553Srgrimes mib[1] = KERN_PROF; 3251553Srgrimes mib[2] = GPROF_STATE; 3261553Srgrimes if (sysctl(mib, 3, &oldstate, &sz, NULL, 0) < 0) 3271553Srgrimes goto bad; 3281553Srgrimes if (oldstate == state) 3291553Srgrimes return; 3301553Srgrimes (void)seteuid(0); 3311553Srgrimes if (sysctl(mib, 3, NULL, NULL, &state, sz) >= 0) { 3321553Srgrimes (void)seteuid(getuid()); 3331553Srgrimes return; 3341553Srgrimes } 3351553Srgrimes (void)seteuid(getuid()); 3368857Srgrimes } else if (kvm_write(kvp->kd, (u_long)&p->state, (void *)&state, sz) 3371553Srgrimes == sz) 3381553Srgrimes return; 3391553Srgrimesbad: 34029736Scharnier warnx("warning: cannot turn profiling %s", 3411553Srgrimes state == GMON_PROF_OFF ? "off" : "on"); 3421553Srgrimes} 3431553Srgrimes 3441553Srgrimes/* 3451553Srgrimes * Build the gmon.out file. 3461553Srgrimes */ 3471553Srgrimesvoid 3481553Srgrimesdumpstate(kvp) 3491553Srgrimes struct kvmvars *kvp; 3501553Srgrimes{ 3511553Srgrimes register FILE *fp; 3521553Srgrimes struct rawarc rawarc; 3531553Srgrimes struct tostruct *tos; 35429736Scharnier u_long frompc; 3551553Srgrimes u_short *froms, *tickbuf; 3561553Srgrimes int mib[3], i; 3571553Srgrimes struct gmonhdr h; 3581553Srgrimes int fromindex, endfrom, toindex; 3591553Srgrimes 3601553Srgrimes setprof(kvp, GMON_PROF_OFF); 3611553Srgrimes fp = fopen("gmon.out", "w"); 3621553Srgrimes if (fp == 0) { 36329736Scharnier warn("gmon.out"); 3641553Srgrimes return; 3651553Srgrimes } 3661553Srgrimes 3671553Srgrimes /* 3681553Srgrimes * Build the gmon header and write it to a file. 3691553Srgrimes */ 3701553Srgrimes bzero(&h, sizeof(h)); 3711553Srgrimes h.lpc = kvp->gpm.lowpc; 3721553Srgrimes h.hpc = kvp->gpm.highpc; 3731553Srgrimes h.ncnt = kvp->gpm.kcountsize + sizeof(h); 3741553Srgrimes h.version = GMONVERSION; 37513107Sbde h.profrate = kvp->gpm.profrate; 37691009Sbde h.histcounter_type = kvp->gpm.histcounter_type; 3771553Srgrimes fwrite((char *)&h, sizeof(h), 1, fp); 3781553Srgrimes 3791553Srgrimes /* 3801553Srgrimes * Write out the tick buffer. 3811553Srgrimes */ 3821553Srgrimes mib[0] = CTL_KERN; 3831553Srgrimes mib[1] = KERN_PROF; 38429736Scharnier if ((tickbuf = (u_short *)malloc(kvp->gpm.kcountsize)) == NULL) 38529736Scharnier errx(5, "cannot allocate kcount space"); 3861553Srgrimes if (kflag) { 3871553Srgrimes i = kvm_read(kvp->kd, (u_long)kvp->gpm.kcount, (void *)tickbuf, 3881553Srgrimes kvp->gpm.kcountsize); 3891553Srgrimes } else { 3901553Srgrimes mib[2] = GPROF_COUNT; 3911553Srgrimes i = kvp->gpm.kcountsize; 3921553Srgrimes if (sysctl(mib, 3, tickbuf, &i, NULL, 0) < 0) 3931553Srgrimes i = 0; 3941553Srgrimes } 39529736Scharnier if (i != kvp->gpm.kcountsize) 39629736Scharnier errx(6, "read ticks: read %u, got %d: %s", 3971553Srgrimes kvp->gpm.kcountsize, i, 3981553Srgrimes kflag ? kvm_geterr(kvp->kd) : strerror(errno)); 39929736Scharnier if ((fwrite(tickbuf, kvp->gpm.kcountsize, 1, fp)) != 1) 40029736Scharnier err(7, "writing tocks to gmon.out"); 4011553Srgrimes free(tickbuf); 4021553Srgrimes 4031553Srgrimes /* 4041553Srgrimes * Write out the arc info. 4051553Srgrimes */ 40629736Scharnier if ((froms = (u_short *)malloc(kvp->gpm.fromssize)) == NULL) 40729736Scharnier errx(8, "cannot allocate froms space"); 4081553Srgrimes if (kflag) { 4091553Srgrimes i = kvm_read(kvp->kd, (u_long)kvp->gpm.froms, (void *)froms, 4101553Srgrimes kvp->gpm.fromssize); 4111553Srgrimes } else { 4121553Srgrimes mib[2] = GPROF_FROMS; 4131553Srgrimes i = kvp->gpm.fromssize; 4141553Srgrimes if (sysctl(mib, 3, froms, &i, NULL, 0) < 0) 4151553Srgrimes i = 0; 4161553Srgrimes } 41729736Scharnier if (i != kvp->gpm.fromssize) 41829736Scharnier errx(9, "read froms: read %u, got %d: %s", 4191553Srgrimes kvp->gpm.fromssize, i, 4201553Srgrimes kflag ? kvm_geterr(kvp->kd) : strerror(errno)); 42129736Scharnier if ((tos = (struct tostruct *)malloc(kvp->gpm.tossize)) == NULL) 42229736Scharnier errx(10, "cannot allocate tos space"); 4231553Srgrimes if (kflag) { 4241553Srgrimes i = kvm_read(kvp->kd, (u_long)kvp->gpm.tos, (void *)tos, 4251553Srgrimes kvp->gpm.tossize); 4261553Srgrimes } else { 4271553Srgrimes mib[2] = GPROF_TOS; 4281553Srgrimes i = kvp->gpm.tossize; 4291553Srgrimes if (sysctl(mib, 3, tos, &i, NULL, 0) < 0) 4301553Srgrimes i = 0; 4311553Srgrimes } 43229736Scharnier if (i != kvp->gpm.tossize) 43329736Scharnier errx(11, "read tos: read %u, got %d: %s", 4341553Srgrimes kvp->gpm.tossize, i, 4351553Srgrimes kflag ? kvm_geterr(kvp->kd) : strerror(errno)); 4361553Srgrimes if (debug) 43729736Scharnier warnx("lowpc 0x%x, textsize 0x%x", 4381553Srgrimes kvp->gpm.lowpc, kvp->gpm.textsize); 4391553Srgrimes endfrom = kvp->gpm.fromssize / sizeof(*froms); 4401553Srgrimes for (fromindex = 0; fromindex < endfrom; ++fromindex) { 4411553Srgrimes if (froms[fromindex] == 0) 4421553Srgrimes continue; 4431553Srgrimes frompc = (u_long)kvp->gpm.lowpc + 4441553Srgrimes (fromindex * kvp->gpm.hashfraction * sizeof(*froms)); 4451553Srgrimes for (toindex = froms[fromindex]; toindex != 0; 4461553Srgrimes toindex = tos[toindex].link) { 4471553Srgrimes if (debug) 44829736Scharnier warnx("[mcleanup] frompc 0x%x selfpc 0x%x count %d", 44929736Scharnier frompc, tos[toindex].selfpc, 4501553Srgrimes tos[toindex].count); 4511553Srgrimes rawarc.raw_frompc = frompc; 4521553Srgrimes rawarc.raw_selfpc = (u_long)tos[toindex].selfpc; 4531553Srgrimes rawarc.raw_count = tos[toindex].count; 4541553Srgrimes fwrite((char *)&rawarc, sizeof(rawarc), 1, fp); 4551553Srgrimes } 4561553Srgrimes } 4571553Srgrimes fclose(fp); 4581553Srgrimes} 4591553Srgrimes 4601553Srgrimes/* 4611553Srgrimes * Get the profiling rate. 4621553Srgrimes */ 4631553Srgrimesint 4641553Srgrimesgetprofhz(kvp) 4651553Srgrimes struct kvmvars *kvp; 4661553Srgrimes{ 4671553Srgrimes int mib[2], size, profrate; 4681553Srgrimes struct clockinfo clockrate; 4691553Srgrimes 4701553Srgrimes if (kflag) { 4711553Srgrimes profrate = 1; 4721553Srgrimes if (kvm_read(kvp->kd, nl[N_PROFHZ].n_value, &profrate, 4731553Srgrimes sizeof profrate) != sizeof profrate) 47429736Scharnier warnx("get clockrate: %s", kvm_geterr(kvp->kd)); 4751553Srgrimes return (profrate); 4761553Srgrimes } 4771553Srgrimes mib[0] = CTL_KERN; 4781553Srgrimes mib[1] = KERN_CLOCKRATE; 4791553Srgrimes clockrate.profhz = 1; 4801553Srgrimes size = sizeof clockrate; 4811553Srgrimes if (sysctl(mib, 2, &clockrate, &size, NULL, 0) < 0) 48229736Scharnier warn("get clockrate"); 4831553Srgrimes return (clockrate.profhz); 4841553Srgrimes} 4851553Srgrimes 4861553Srgrimes/* 4871553Srgrimes * Reset the kernel profiling date structures. 4881553Srgrimes */ 4891553Srgrimesvoid 4901553Srgrimesreset(kvp) 4911553Srgrimes struct kvmvars *kvp; 4921553Srgrimes{ 4931553Srgrimes char *zbuf; 4941553Srgrimes u_long biggest; 4951553Srgrimes int mib[3]; 4961553Srgrimes 4971553Srgrimes setprof(kvp, GMON_PROF_OFF); 4981553Srgrimes 4991553Srgrimes biggest = kvp->gpm.kcountsize; 5001553Srgrimes if (kvp->gpm.fromssize > biggest) 5011553Srgrimes biggest = kvp->gpm.fromssize; 5021553Srgrimes if (kvp->gpm.tossize > biggest) 5031553Srgrimes biggest = kvp->gpm.tossize; 50429736Scharnier if ((zbuf = (char *)malloc(biggest)) == NULL) 50529736Scharnier errx(12, "cannot allocate zbuf space"); 5061553Srgrimes bzero(zbuf, biggest); 5071553Srgrimes if (kflag) { 5081553Srgrimes if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf, 50929736Scharnier kvp->gpm.kcountsize) != kvp->gpm.kcountsize) 51029736Scharnier errx(13, "tickbuf zero: %s", kvm_geterr(kvp->kd)); 5111553Srgrimes if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf, 51229736Scharnier kvp->gpm.fromssize) != kvp->gpm.fromssize) 51329736Scharnier errx(14, "froms zero: %s", kvm_geterr(kvp->kd)); 5141553Srgrimes if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf, 51529736Scharnier kvp->gpm.tossize) != kvp->gpm.tossize) 51629736Scharnier errx(15, "tos zero: %s", kvm_geterr(kvp->kd)); 5171553Srgrimes return; 5181553Srgrimes } 5191553Srgrimes (void)seteuid(0); 5201553Srgrimes mib[0] = CTL_KERN; 5211553Srgrimes mib[1] = KERN_PROF; 5221553Srgrimes mib[2] = GPROF_COUNT; 52329736Scharnier if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.kcountsize) < 0) 52429736Scharnier err(13, "tickbuf zero"); 5251553Srgrimes mib[2] = GPROF_FROMS; 52629736Scharnier if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.fromssize) < 0) 52729736Scharnier err(14, "froms zero"); 5281553Srgrimes mib[2] = GPROF_TOS; 52929736Scharnier if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.tossize) < 0) 53029736Scharnier err(15, "tos zero"); 5311553Srgrimes (void)seteuid(getuid()); 5321553Srgrimes free(zbuf); 5331553Srgrimes} 534