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 4029736Scharnierstatic const char rcsid[] = 4150479Speter "$FreeBSD$"; 421553Srgrimes#endif /* not lint */ 431553Srgrimes 441553Srgrimes#include <sys/param.h> 451553Srgrimes#include <sys/file.h> 4611937Sphk#include <sys/time.h> 471553Srgrimes#include <sys/sysctl.h> 481553Srgrimes#include <sys/gmon.h> 4929736Scharnier#include <ctype.h> 5029736Scharnier#include <err.h> 511553Srgrimes#include <errno.h> 521553Srgrimes#include <kvm.h> 531553Srgrimes#include <limits.h> 5429736Scharnier#include <nlist.h> 5529736Scharnier#include <paths.h> 5691009Sbde#include <stddef.h> 571553Srgrimes#include <stdio.h> 581553Srgrimes#include <stdlib.h> 591553Srgrimes#include <string.h> 6029736Scharnier#include <unistd.h> 611553Srgrimes 621553Srgrimesstruct nlist nl[] = { 631553Srgrimes#define N_GMONPARAM 0 641553Srgrimes { "__gmonparam" }, 651553Srgrimes#define N_PROFHZ 1 661553Srgrimes { "_profhz" }, 6729736Scharnier { NULL }, 681553Srgrimes}; 691553Srgrimes 701553Srgrimesstruct kvmvars { 711553Srgrimes kvm_t *kd; 721553Srgrimes struct gmonparam gpm; 731553Srgrimes}; 741553Srgrimes 7513107Sbdeint Bflag, bflag, hflag, kflag, rflag, pflag; 761553Srgrimesint debug = 0; 7799800Salfredint getprof(struct kvmvars *); 7899800Salfredint getprofhz(struct kvmvars *); 7999800Salfredvoid kern_readonly(int); 8099800Salfredint openfiles(char *, char *, struct kvmvars *); 8199800Salfredvoid setprof(struct kvmvars *kvp, int state); 8299800Salfredvoid dumpstate(struct kvmvars *kvp); 8399800Salfredvoid reset(struct kvmvars *kvp); 8499800Salfredstatic void usage(void); 851553Srgrimes 861553Srgrimesint 871553Srgrimesmain(int argc, char **argv) 881553Srgrimes{ 891553Srgrimes int ch, mode, disp, accessmode; 901553Srgrimes struct kvmvars kvmvars; 911553Srgrimes char *system, *kmemf; 921553Srgrimes 93242166Seadler if (seteuid(getuid()) != 0) { 94242166Seadler err(1, "seteuid failed\n"); 95242166Seadler } 961553Srgrimes kmemf = NULL; 971553Srgrimes system = NULL; 9824428Simp while ((ch = getopt(argc, argv, "M:N:Bbhpr")) != -1) { 991553Srgrimes switch((char)ch) { 1001553Srgrimes 1011553Srgrimes case 'M': 1021553Srgrimes kmemf = optarg; 1031553Srgrimes kflag = 1; 1041553Srgrimes break; 1051553Srgrimes 1061553Srgrimes case 'N': 1071553Srgrimes system = optarg; 1081553Srgrimes break; 1091553Srgrimes 11013107Sbde case 'B': 11113107Sbde Bflag = 1; 11213107Sbde break; 11313107Sbde 1141553Srgrimes case 'b': 1151553Srgrimes bflag = 1; 1161553Srgrimes break; 1171553Srgrimes 1181553Srgrimes case 'h': 1191553Srgrimes hflag = 1; 1201553Srgrimes break; 1211553Srgrimes 1221553Srgrimes case 'p': 1231553Srgrimes pflag = 1; 1241553Srgrimes break; 1251553Srgrimes 1261553Srgrimes case 'r': 1271553Srgrimes rflag = 1; 1281553Srgrimes break; 1291553Srgrimes 1301553Srgrimes default: 13129736Scharnier usage(); 1321553Srgrimes } 1331553Srgrimes } 1341553Srgrimes argc -= optind; 1351553Srgrimes argv += optind; 1361553Srgrimes 1371553Srgrimes#define BACKWARD_COMPATIBILITY 1381553Srgrimes#ifdef BACKWARD_COMPATIBILITY 1391553Srgrimes if (*argv) { 1401553Srgrimes system = *argv; 1411553Srgrimes if (*++argv) { 1421553Srgrimes kmemf = *argv; 1431553Srgrimes ++kflag; 1441553Srgrimes } 1451553Srgrimes } 1461553Srgrimes#endif 1471553Srgrimes if (system == NULL) 1483041Swollman system = (char *)getbootfile(); 1491553Srgrimes accessmode = openfiles(system, kmemf, &kvmvars); 1501553Srgrimes mode = getprof(&kvmvars); 1511553Srgrimes if (hflag) 1521553Srgrimes disp = GMON_PROF_OFF; 15313107Sbde else if (Bflag) 15413107Sbde disp = GMON_PROF_HIRES; 1551553Srgrimes else if (bflag) 1561553Srgrimes disp = GMON_PROF_ON; 1571553Srgrimes else 1581553Srgrimes disp = mode; 1591553Srgrimes if (pflag) 1601553Srgrimes dumpstate(&kvmvars); 1611553Srgrimes if (rflag) 1621553Srgrimes reset(&kvmvars); 1631553Srgrimes if (accessmode == O_RDWR) 1641553Srgrimes setprof(&kvmvars, disp); 1651553Srgrimes (void)fprintf(stdout, "kgmon: kernel profiling is %s.\n", 16613107Sbde disp == GMON_PROF_OFF ? "off" : 16713107Sbde disp == GMON_PROF_HIRES ? "running (high resolution)" : 16813107Sbde disp == GMON_PROF_ON ? "running" : 16913107Sbde disp == GMON_PROF_BUSY ? "busy" : 17013107Sbde disp == GMON_PROF_ERROR ? "off (error)" : 17113107Sbde "in an unknown state"); 1721553Srgrimes return (0); 1731553Srgrimes} 1741553Srgrimes 17529736Scharnierstatic void 17629736Scharnierusage() 17729736Scharnier{ 17829736Scharnier fprintf(stderr, "usage: kgmon [-Bbhrp] [-M core] [-N system]\n"); 17929736Scharnier exit(1); 18029736Scharnier} 18129736Scharnier 1821553Srgrimes/* 183160022Sdelphij * Check that profiling is enabled and open any necessary files. 1841553Srgrimes */ 18529736Scharnierint 1861553Srgrimesopenfiles(system, kmemf, kvp) 1871553Srgrimes char *system; 1881553Srgrimes char *kmemf; 1891553Srgrimes struct kvmvars *kvp; 1901553Srgrimes{ 191100904Sjake size_t size; 192100904Sjake int mib[3], state, openmode; 1931553Srgrimes char errbuf[_POSIX2_LINE_MAX]; 1941553Srgrimes 1951553Srgrimes if (!kflag) { 1961553Srgrimes mib[0] = CTL_KERN; 1971553Srgrimes mib[1] = KERN_PROF; 1981553Srgrimes mib[2] = GPROF_STATE; 1991553Srgrimes size = sizeof state; 20029736Scharnier if (sysctl(mib, 3, &state, &size, NULL, 0) < 0) 20129736Scharnier errx(20, "profiling not defined in kernel"); 20213107Sbde if (!(Bflag || bflag || hflag || rflag || 20313107Sbde (pflag && 20413107Sbde (state == GMON_PROF_HIRES || state == GMON_PROF_ON)))) 2051553Srgrimes return (O_RDONLY); 2061553Srgrimes (void)seteuid(0); 2071553Srgrimes if (sysctl(mib, 3, NULL, NULL, &state, size) >= 0) 2081553Srgrimes return (O_RDWR); 2091553Srgrimes (void)seteuid(getuid()); 2101553Srgrimes kern_readonly(state); 2111553Srgrimes return (O_RDONLY); 2121553Srgrimes } 21313107Sbde openmode = (Bflag || bflag || hflag || pflag || rflag) 21413107Sbde ? O_RDWR : O_RDONLY; 2151553Srgrimes kvp->kd = kvm_openfiles(system, kmemf, NULL, openmode, errbuf); 2161553Srgrimes if (kvp->kd == NULL) { 2171553Srgrimes if (openmode == O_RDWR) { 2181553Srgrimes openmode = O_RDONLY; 2191553Srgrimes kvp->kd = kvm_openfiles(system, kmemf, NULL, O_RDONLY, 2201553Srgrimes errbuf); 2211553Srgrimes } 22229736Scharnier if (kvp->kd == NULL) 22329736Scharnier errx(2, "kvm_openfiles: %s", errbuf); 2241553Srgrimes kern_readonly(GMON_PROF_ON); 2251553Srgrimes } 22629736Scharnier if (kvm_nlist(kvp->kd, nl) < 0) 22729736Scharnier errx(3, "%s: no namelist", system); 22829736Scharnier if (!nl[N_GMONPARAM].n_value) 22929736Scharnier errx(20, "profiling not defined in kernel"); 2301553Srgrimes return (openmode); 2311553Srgrimes} 2321553Srgrimes 2331553Srgrimes/* 2341553Srgrimes * Suppress options that require a writable kernel. 2351553Srgrimes */ 23629736Scharniervoid 2371553Srgrimeskern_readonly(mode) 2381553Srgrimes int mode; 2391553Srgrimes{ 2401553Srgrimes 2411553Srgrimes (void)fprintf(stderr, "kgmon: kernel read-only: "); 24213107Sbde if (pflag && (mode == GMON_PROF_HIRES || mode == GMON_PROF_ON)) 2431553Srgrimes (void)fprintf(stderr, "data may be inconsistent\n"); 2441553Srgrimes if (rflag) 2451553Srgrimes (void)fprintf(stderr, "-r supressed\n"); 24613107Sbde if (Bflag) 24713107Sbde (void)fprintf(stderr, "-B supressed\n"); 2481553Srgrimes if (bflag) 2491553Srgrimes (void)fprintf(stderr, "-b supressed\n"); 2501553Srgrimes if (hflag) 2511553Srgrimes (void)fprintf(stderr, "-h supressed\n"); 25213107Sbde rflag = Bflag = bflag = hflag = 0; 2531553Srgrimes} 2541553Srgrimes 2551553Srgrimes/* 2561553Srgrimes * Get the state of kernel profiling. 2571553Srgrimes */ 25829736Scharnierint 2591553Srgrimesgetprof(kvp) 2601553Srgrimes struct kvmvars *kvp; 2611553Srgrimes{ 262100904Sjake size_t size; 263100904Sjake int mib[3]; 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; 319100904Sjake size_t sz; 320100904Sjake int mib[3], oldstate; 3211553Srgrimes 3221553Srgrimes sz = sizeof(state); 3231553Srgrimes if (!kflag) { 3241553Srgrimes mib[0] = CTL_KERN; 3251553Srgrimes mib[1] = KERN_PROF; 3261553Srgrimes mib[2] = GPROF_STATE; 3271553Srgrimes if (sysctl(mib, 3, &oldstate, &sz, NULL, 0) < 0) 3281553Srgrimes goto bad; 3291553Srgrimes if (oldstate == state) 3301553Srgrimes return; 3311553Srgrimes (void)seteuid(0); 3321553Srgrimes if (sysctl(mib, 3, NULL, NULL, &state, sz) >= 0) { 3331553Srgrimes (void)seteuid(getuid()); 3341553Srgrimes return; 3351553Srgrimes } 3361553Srgrimes (void)seteuid(getuid()); 3378857Srgrimes } else if (kvm_write(kvp->kd, (u_long)&p->state, (void *)&state, sz) 3381553Srgrimes == sz) 3391553Srgrimes return; 3401553Srgrimesbad: 34129736Scharnier warnx("warning: cannot turn profiling %s", 3421553Srgrimes state == GMON_PROF_OFF ? "off" : "on"); 3431553Srgrimes} 3441553Srgrimes 3451553Srgrimes/* 3461553Srgrimes * Build the gmon.out file. 3471553Srgrimes */ 3481553Srgrimesvoid 3491553Srgrimesdumpstate(kvp) 3501553Srgrimes struct kvmvars *kvp; 3511553Srgrimes{ 3521553Srgrimes register FILE *fp; 3531553Srgrimes struct rawarc rawarc; 3541553Srgrimes struct tostruct *tos; 35529736Scharnier u_long frompc; 3561553Srgrimes u_short *froms, *tickbuf; 357100904Sjake size_t i; 358100904Sjake int mib[3]; 3591553Srgrimes struct gmonhdr h; 3601553Srgrimes int fromindex, endfrom, toindex; 3611553Srgrimes 3621553Srgrimes setprof(kvp, GMON_PROF_OFF); 3631553Srgrimes fp = fopen("gmon.out", "w"); 3641553Srgrimes if (fp == 0) { 36529736Scharnier warn("gmon.out"); 3661553Srgrimes return; 3671553Srgrimes } 3681553Srgrimes 3691553Srgrimes /* 3701553Srgrimes * Build the gmon header and write it to a file. 3711553Srgrimes */ 3721553Srgrimes bzero(&h, sizeof(h)); 3731553Srgrimes h.lpc = kvp->gpm.lowpc; 3741553Srgrimes h.hpc = kvp->gpm.highpc; 3751553Srgrimes h.ncnt = kvp->gpm.kcountsize + sizeof(h); 3761553Srgrimes h.version = GMONVERSION; 37713107Sbde h.profrate = kvp->gpm.profrate; 37891009Sbde h.histcounter_type = kvp->gpm.histcounter_type; 3791553Srgrimes fwrite((char *)&h, sizeof(h), 1, fp); 3801553Srgrimes 3811553Srgrimes /* 3821553Srgrimes * Write out the tick buffer. 3831553Srgrimes */ 3841553Srgrimes mib[0] = CTL_KERN; 3851553Srgrimes mib[1] = KERN_PROF; 38629736Scharnier if ((tickbuf = (u_short *)malloc(kvp->gpm.kcountsize)) == NULL) 38729736Scharnier errx(5, "cannot allocate kcount space"); 3881553Srgrimes if (kflag) { 3891553Srgrimes i = kvm_read(kvp->kd, (u_long)kvp->gpm.kcount, (void *)tickbuf, 3901553Srgrimes kvp->gpm.kcountsize); 3911553Srgrimes } else { 3921553Srgrimes mib[2] = GPROF_COUNT; 3931553Srgrimes i = kvp->gpm.kcountsize; 3941553Srgrimes if (sysctl(mib, 3, tickbuf, &i, NULL, 0) < 0) 3951553Srgrimes i = 0; 3961553Srgrimes } 39729736Scharnier if (i != kvp->gpm.kcountsize) 398134505Smarcel errx(6, "read ticks: read %lu, got %ld: %s", 399134505Smarcel kvp->gpm.kcountsize, (long)i, 4001553Srgrimes kflag ? kvm_geterr(kvp->kd) : strerror(errno)); 40129736Scharnier if ((fwrite(tickbuf, kvp->gpm.kcountsize, 1, fp)) != 1) 40229736Scharnier err(7, "writing tocks to gmon.out"); 4031553Srgrimes free(tickbuf); 4041553Srgrimes 4051553Srgrimes /* 4061553Srgrimes * Write out the arc info. 4071553Srgrimes */ 40829736Scharnier if ((froms = (u_short *)malloc(kvp->gpm.fromssize)) == NULL) 40929736Scharnier errx(8, "cannot allocate froms space"); 4101553Srgrimes if (kflag) { 4111553Srgrimes i = kvm_read(kvp->kd, (u_long)kvp->gpm.froms, (void *)froms, 4121553Srgrimes kvp->gpm.fromssize); 4131553Srgrimes } else { 4141553Srgrimes mib[2] = GPROF_FROMS; 4151553Srgrimes i = kvp->gpm.fromssize; 4161553Srgrimes if (sysctl(mib, 3, froms, &i, NULL, 0) < 0) 4171553Srgrimes i = 0; 4181553Srgrimes } 41929736Scharnier if (i != kvp->gpm.fromssize) 420134505Smarcel errx(9, "read froms: read %lu, got %ld: %s", 421134505Smarcel kvp->gpm.fromssize, (long)i, 4221553Srgrimes kflag ? kvm_geterr(kvp->kd) : strerror(errno)); 42329736Scharnier if ((tos = (struct tostruct *)malloc(kvp->gpm.tossize)) == NULL) 42429736Scharnier errx(10, "cannot allocate tos space"); 4251553Srgrimes if (kflag) { 4261553Srgrimes i = kvm_read(kvp->kd, (u_long)kvp->gpm.tos, (void *)tos, 4271553Srgrimes kvp->gpm.tossize); 4281553Srgrimes } else { 4291553Srgrimes mib[2] = GPROF_TOS; 4301553Srgrimes i = kvp->gpm.tossize; 4311553Srgrimes if (sysctl(mib, 3, tos, &i, NULL, 0) < 0) 4321553Srgrimes i = 0; 4331553Srgrimes } 43429736Scharnier if (i != kvp->gpm.tossize) 435134505Smarcel errx(11, "read tos: read %lu, got %ld: %s", 436134505Smarcel kvp->gpm.tossize, (long)i, 4371553Srgrimes kflag ? kvm_geterr(kvp->kd) : strerror(errno)); 4381553Srgrimes if (debug) 439134505Smarcel warnx("lowpc 0x%lx, textsize 0x%lx", 440134505Smarcel (unsigned long)kvp->gpm.lowpc, kvp->gpm.textsize); 4411553Srgrimes endfrom = kvp->gpm.fromssize / sizeof(*froms); 4421553Srgrimes for (fromindex = 0; fromindex < endfrom; ++fromindex) { 4431553Srgrimes if (froms[fromindex] == 0) 4441553Srgrimes continue; 4451553Srgrimes frompc = (u_long)kvp->gpm.lowpc + 4461553Srgrimes (fromindex * kvp->gpm.hashfraction * sizeof(*froms)); 4471553Srgrimes for (toindex = froms[fromindex]; toindex != 0; 448134505Smarcel toindex = tos[toindex].link) { 4491553Srgrimes if (debug) 450134505Smarcel warnx("[mcleanup] frompc 0x%lx selfpc 0x%lx " 451134505Smarcel "count %ld", frompc, tos[toindex].selfpc, 452134505Smarcel tos[toindex].count); 4531553Srgrimes rawarc.raw_frompc = frompc; 4541553Srgrimes rawarc.raw_selfpc = (u_long)tos[toindex].selfpc; 4551553Srgrimes rawarc.raw_count = tos[toindex].count; 4561553Srgrimes fwrite((char *)&rawarc, sizeof(rawarc), 1, fp); 4571553Srgrimes } 4581553Srgrimes } 4591553Srgrimes fclose(fp); 4601553Srgrimes} 4611553Srgrimes 4621553Srgrimes/* 4631553Srgrimes * Get the profiling rate. 4641553Srgrimes */ 4651553Srgrimesint 4661553Srgrimesgetprofhz(kvp) 4671553Srgrimes struct kvmvars *kvp; 4681553Srgrimes{ 469100904Sjake size_t size; 470100904Sjake int mib[2], profrate; 4711553Srgrimes struct clockinfo clockrate; 4721553Srgrimes 4731553Srgrimes if (kflag) { 4741553Srgrimes profrate = 1; 4751553Srgrimes if (kvm_read(kvp->kd, nl[N_PROFHZ].n_value, &profrate, 4761553Srgrimes sizeof profrate) != sizeof profrate) 47729736Scharnier warnx("get clockrate: %s", kvm_geterr(kvp->kd)); 4781553Srgrimes return (profrate); 4791553Srgrimes } 4801553Srgrimes mib[0] = CTL_KERN; 4811553Srgrimes mib[1] = KERN_CLOCKRATE; 4821553Srgrimes clockrate.profhz = 1; 4831553Srgrimes size = sizeof clockrate; 4841553Srgrimes if (sysctl(mib, 2, &clockrate, &size, NULL, 0) < 0) 48529736Scharnier warn("get clockrate"); 4861553Srgrimes return (clockrate.profhz); 4871553Srgrimes} 4881553Srgrimes 4891553Srgrimes/* 4901553Srgrimes * Reset the kernel profiling date structures. 4911553Srgrimes */ 4921553Srgrimesvoid 4931553Srgrimesreset(kvp) 4941553Srgrimes struct kvmvars *kvp; 4951553Srgrimes{ 4961553Srgrimes char *zbuf; 4971553Srgrimes u_long biggest; 4981553Srgrimes int mib[3]; 4991553Srgrimes 5001553Srgrimes setprof(kvp, GMON_PROF_OFF); 5011553Srgrimes 5021553Srgrimes biggest = kvp->gpm.kcountsize; 5031553Srgrimes if (kvp->gpm.fromssize > biggest) 5041553Srgrimes biggest = kvp->gpm.fromssize; 5051553Srgrimes if (kvp->gpm.tossize > biggest) 5061553Srgrimes biggest = kvp->gpm.tossize; 50729736Scharnier if ((zbuf = (char *)malloc(biggest)) == NULL) 50829736Scharnier errx(12, "cannot allocate zbuf space"); 5091553Srgrimes bzero(zbuf, biggest); 5101553Srgrimes if (kflag) { 5111553Srgrimes if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf, 51229736Scharnier kvp->gpm.kcountsize) != kvp->gpm.kcountsize) 51329736Scharnier errx(13, "tickbuf zero: %s", kvm_geterr(kvp->kd)); 5141553Srgrimes if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf, 51529736Scharnier kvp->gpm.fromssize) != kvp->gpm.fromssize) 51629736Scharnier errx(14, "froms zero: %s", kvm_geterr(kvp->kd)); 5171553Srgrimes if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf, 51829736Scharnier kvp->gpm.tossize) != kvp->gpm.tossize) 51929736Scharnier errx(15, "tos zero: %s", kvm_geterr(kvp->kd)); 5201553Srgrimes return; 5211553Srgrimes } 5221553Srgrimes (void)seteuid(0); 5231553Srgrimes mib[0] = CTL_KERN; 5241553Srgrimes mib[1] = KERN_PROF; 5251553Srgrimes mib[2] = GPROF_COUNT; 52629736Scharnier if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.kcountsize) < 0) 52729736Scharnier err(13, "tickbuf zero"); 5281553Srgrimes mib[2] = GPROF_FROMS; 52929736Scharnier if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.fromssize) < 0) 53029736Scharnier err(14, "froms zero"); 5311553Srgrimes mib[2] = GPROF_TOS; 53229736Scharnier if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.tossize) < 0) 53329736Scharnier err(15, "tos zero"); 5341553Srgrimes (void)seteuid(getuid()); 5351553Srgrimes free(zbuf); 5361553Srgrimes} 537