Home | History | Annotate | Line # | Download | only in kgmon
kgmon.c revision 1.22
      1 /*	$NetBSD: kgmon.c,v 1.22 2007/05/04 22:07:16 dogcow Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1983, 1992, 1993\n\
     35 	The Regents of the University of California.  All rights reserved.\n");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "from: @(#)kgmon.c	8.1 (Berkeley) 6/6/93";
     41 #else
     42 __RCSID("$NetBSD: kgmon.c,v 1.22 2007/05/04 22:07:16 dogcow Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 #include <sys/param.h>
     47 #include <sys/file.h>
     48 #include <sys/sysctl.h>
     49 #include <sys/gmon.h>
     50 
     51 #include <ctype.h>
     52 #include <errno.h>
     53 #include <kvm.h>
     54 #include <err.h>
     55 #include <limits.h>
     56 #include <nlist.h>
     57 #include <stdio.h>
     58 #include <stdlib.h>
     59 #include <string.h>
     60 #include <unistd.h>
     61 
     62 static struct nlist nl[] = {
     63 #define	N_GMONPARAM	0
     64 	{ "__gmonparam", 0, 0, 0, 0 },
     65 #define	N_PROFHZ	1
     66 	{ "_profhz", 0, 0, 0, 0 },
     67 	{ 0, 0, 0, 0, 0 }
     68 };
     69 
     70 struct kvmvars {
     71 	kvm_t	*kd;
     72 	struct gmonparam gpm;
     73 };
     74 
     75 static int	bflag, hflag, kflag, rflag, pflag;
     76 static int	debug = 0;
     77 static void	setprof(struct kvmvars *kvp, int state);
     78 static void	dumpstate(struct kvmvars *kvp);
     79 static void	reset(struct kvmvars *kvp);
     80 static int	openfiles(char *, char *, struct kvmvars *);
     81 static int	getprof(struct kvmvars *);
     82 static void	kern_readonly(int);
     83 static int	getprofhz(struct kvmvars *);
     84 
     85 int
     86 main(int argc, char **argv)
     87 {
     88 	int ch, mode, disp, accessmode;
     89 	struct kvmvars kvmvars;
     90 	char *sys, *kmemf;
     91 
     92 	setprogname(argv[0]);
     93 	(void)seteuid(getuid());
     94 	kmemf = NULL;
     95 	sys = NULL;
     96 	while ((ch = getopt(argc, argv, "M:N:bdhpr")) != -1) {
     97 		switch((char)ch) {
     98 
     99 		case 'M':
    100 			kmemf = optarg;
    101 			kflag = 1;
    102 			break;
    103 
    104 		case 'N':
    105 			sys = optarg;
    106 			break;
    107 
    108 		case 'b':
    109 			bflag = 1;
    110 			break;
    111 
    112 		case 'h':
    113 			hflag = 1;
    114 			break;
    115 
    116 		case 'p':
    117 			pflag = 1;
    118 			break;
    119 
    120 		case 'r':
    121 			rflag = 1;
    122 			break;
    123 
    124 		case 'd':
    125 			debug = 1;
    126 			break;
    127 
    128 		default:
    129 			(void)fprintf(stderr,
    130 			    "usage: %s [-bdhrp] [-M core] [-N system]\n",
    131 			    getprogname());
    132 			exit(1);
    133 		}
    134 	}
    135 	argc -= optind;
    136 	argv += optind;
    137 
    138 #define BACKWARD_COMPATIBILITY
    139 #ifdef	BACKWARD_COMPATIBILITY
    140 	if (*argv) {
    141 		sys = *argv;
    142 		if (*++argv) {
    143 			kmemf = *argv;
    144 			++kflag;
    145 		}
    146 	}
    147 #endif
    148 	accessmode = openfiles(sys, kmemf, &kvmvars);
    149 	mode = getprof(&kvmvars);
    150 	if (hflag)
    151 		disp = GMON_PROF_OFF;
    152 	else if (bflag)
    153 		disp = GMON_PROF_ON;
    154 	else
    155 		disp = mode;
    156 	if (pflag)
    157 		dumpstate(&kvmvars);
    158 	if (rflag)
    159 		reset(&kvmvars);
    160 	if (accessmode == O_RDWR)
    161 		setprof(&kvmvars, disp);
    162 	(void)fprintf(stdout, "%s: kernel profiling is %s.\n",
    163 	     getprogname(), disp == GMON_PROF_OFF ? "off" : "running");
    164 	return (0);
    165 }
    166 
    167 /*
    168  * Check that profiling is enabled and open any ncessary files.
    169  */
    170 static int
    171 openfiles(char *sys, char *kmemf, struct kvmvars *kvp)
    172 {
    173 	int mib[3], state, openmode;
    174 	size_t size;
    175 	char errbuf[_POSIX2_LINE_MAX];
    176 
    177 	if (!kflag) {
    178 		mib[0] = CTL_KERN;
    179 		mib[1] = KERN_PROF;
    180 		mib[2] = GPROF_STATE;
    181 		size = sizeof state;
    182 		if (sysctl(mib, 3, &state, &size, NULL, 0) < 0)
    183 			err(EXIT_FAILURE, "profiling not defined in kernel");
    184 		if (!(bflag || hflag || rflag ||
    185 		    (pflag && state == GMON_PROF_ON)))
    186 			return (O_RDONLY);
    187 		(void)seteuid(0);
    188 		if (sysctl(mib, 3, NULL, NULL, &state, size) >= 0)
    189 			return (O_RDWR);
    190 		(void)seteuid(getuid());
    191 		kern_readonly(state);
    192 		return (O_RDONLY);
    193 	}
    194 	openmode = (bflag || hflag || pflag || rflag) ? O_RDWR : O_RDONLY;
    195 	kvp->kd = kvm_openfiles(sys, kmemf, NULL, openmode, errbuf);
    196 	if (kvp->kd == NULL) {
    197 		if (openmode == O_RDWR) {
    198 			openmode = O_RDONLY;
    199 			kvp->kd = kvm_openfiles(sys, kmemf, NULL, O_RDONLY,
    200 			    errbuf);
    201 		}
    202 		if (kvp->kd == NULL)
    203 			errx(EXIT_FAILURE, "kvm_openfiles: %s", errbuf);
    204 		kern_readonly(GMON_PROF_ON);
    205 	}
    206 	if (kvm_nlist(kvp->kd, nl) < 0)
    207 		errx(EXIT_FAILURE, "%s: no namelist", sys);
    208 	if (!nl[N_GMONPARAM].n_value)
    209 		errx(EXIT_FAILURE, "profiling not defined in kernel");
    210 	return (openmode);
    211 }
    212 
    213 /*
    214  * Suppress options that require a writable kernel.
    215  */
    216 static void
    217 kern_readonly(int mode)
    218 {
    219 
    220 	(void)fprintf(stderr, "%s: kernel read-only: ", getprogname());
    221 	if (pflag && mode == GMON_PROF_ON)
    222 		(void)fprintf(stderr, "data may be inconsistent\n");
    223 	if (rflag)
    224 		(void)fprintf(stderr, "-r supressed\n");
    225 	if (bflag)
    226 		(void)fprintf(stderr, "-b supressed\n");
    227 	if (hflag)
    228 		(void)fprintf(stderr, "-h supressed\n");
    229 	rflag = bflag = hflag = 0;
    230 }
    231 
    232 /*
    233  * Get the state of kernel profiling.
    234  */
    235 static int
    236 getprof(struct kvmvars *kvp)
    237 {
    238 	int mib[3];
    239 	size_t size;
    240 
    241 	if (kflag) {
    242 		size = kvm_read(kvp->kd, nl[N_GMONPARAM].n_value, &kvp->gpm,
    243 		    sizeof kvp->gpm);
    244 	} else {
    245 		mib[0] = CTL_KERN;
    246 		mib[1] = KERN_PROF;
    247 		mib[2] = GPROF_GMONPARAM;
    248 		size = sizeof kvp->gpm;
    249 		if (sysctl(mib, 3, &kvp->gpm, &size, NULL, 0) < 0)
    250 			size = 0;
    251 	}
    252 	if (size != sizeof kvp->gpm)
    253 		errx(EXIT_FAILURE, "cannot get gmonparam: %s",
    254 		    kflag ? kvm_geterr(kvp->kd) : strerror(errno));
    255 	return (kvp->gpm.state);
    256 }
    257 
    258 /*
    259  * Enable or disable kernel profiling according to the state variable.
    260  */
    261 static void
    262 setprof(struct kvmvars *kvp, int state)
    263 {
    264 	struct gmonparam *p = (struct gmonparam *)nl[N_GMONPARAM].n_value;
    265 	int mib[3], oldstate;
    266 	size_t sz;
    267 
    268 	sz = sizeof(state);
    269 	if (!kflag) {
    270 		mib[0] = CTL_KERN;
    271 		mib[1] = KERN_PROF;
    272 		mib[2] = GPROF_STATE;
    273 		if (sysctl(mib, 3, &oldstate, &sz, NULL, 0) < 0)
    274 			goto bad;
    275 		if (oldstate == state)
    276 			return;
    277 		(void)seteuid(0);
    278 		if (sysctl(mib, 3, NULL, NULL, &state, sz) >= 0) {
    279 			(void)seteuid(getuid());
    280 			return;
    281 		}
    282 		(void)seteuid(getuid());
    283 	} else if (kvm_write(kvp->kd, (u_long)&p->state, (void *)&state, sz)
    284 	    == sz)
    285 		return;
    286 bad:
    287 	warnx("cannot turn profiling %s", state == GMON_PROF_OFF ?
    288 	    "off" : "on");
    289 }
    290 
    291 /*
    292  * Build the gmon.out file.
    293  */
    294 static void
    295 dumpstate(struct kvmvars *kvp)
    296 {
    297 	FILE *fp;
    298 	struct rawarc rawarc;
    299 	struct tostruct *tos;
    300 	u_long frompc;
    301 	u_short *froms, *tickbuf;
    302 	int mib[3];
    303 	size_t i;
    304 	struct gmonhdr h;
    305 	int fromindex, endfrom, toindex;
    306 	size_t kcountsize;
    307 
    308 	setprof(kvp, GMON_PROF_OFF);
    309 	fp = fopen("gmon.out", "w");
    310 	if (fp == NULL) {
    311 		warn("cannot open `gmon.out'");
    312 		return;
    313 	}
    314 
    315 	/*
    316 	 * Build the gmon header and write it to a file.
    317 	 */
    318 	bzero(&h, sizeof(h));
    319 	h.lpc = kvp->gpm.lowpc;
    320 	h.hpc = kvp->gpm.highpc;
    321 	h.ncnt = kvp->gpm.kcountsize + sizeof(h);
    322 	h.version = GMONVERSION;
    323 	h.profrate = getprofhz(kvp);
    324 	if (fwrite(&h, sizeof(h), 1, fp) != 1)
    325 		err(EXIT_FAILURE, "writing header to gmon.out");
    326 
    327 	kcountsize = (size_t)kvp->gpm.kcountsize;
    328 
    329 	/*
    330 	 * Write out the tick buffer.
    331 	 */
    332 	mib[0] = CTL_KERN;
    333 	mib[1] = KERN_PROF;
    334 	if ((tickbuf = malloc(kcountsize)) == NULL)
    335 		err(EXIT_FAILURE, "Cannot allocate %zu kcount space",
    336 		    kcountsize);
    337 	if (kflag) {
    338 		i = kvm_read(kvp->kd, (u_long)kvp->gpm.kcount, tickbuf,
    339 		    kcountsize);
    340 	} else {
    341 		mib[2] = GPROF_COUNT;
    342 		i = kcountsize;
    343 		if (sysctl(mib, 3, tickbuf, &i, NULL, 0) < 0)
    344 			i = 0;
    345 	}
    346 	if (i != kcountsize)
    347 		errx(EXIT_FAILURE, "read ticks: read %zu, got %zu: %s",
    348 		    kcountsize, i,
    349 		    kflag ? kvm_geterr(kvp->kd) : strerror(errno));
    350 	if ((fwrite(tickbuf, kcountsize, 1, fp)) != 1)
    351 		err(EXIT_FAILURE, "writing tocks to gmon.out");
    352 	free(tickbuf);
    353 
    354 	/*
    355 	 * Write out the arc info.
    356 	 */
    357 	if ((froms = malloc((size_t)kvp->gpm.fromssize)) == NULL)
    358 		err(EXIT_FAILURE, "cannot allocate %zu froms space",
    359 		    (size_t)kvp->gpm.fromssize);
    360 	if (kflag) {
    361 		i = kvm_read(kvp->kd, (u_long)kvp->gpm.froms, froms,
    362 		    (size_t)kvp->gpm.fromssize);
    363 	} else {
    364 		mib[2] = GPROF_FROMS;
    365 		i = kvp->gpm.fromssize;
    366 		if (sysctl(mib, 3, froms, &i, NULL, 0) < 0)
    367 			i = 0;
    368 	}
    369 	if (i != kvp->gpm.fromssize)
    370 		errx(EXIT_FAILURE, "read froms: read %lu, got %lu: %s",
    371 		    kvp->gpm.fromssize, (u_long)i,
    372 		    kflag ? kvm_geterr(kvp->kd) : strerror(errno));
    373 	if ((tos = malloc((size_t)kvp->gpm.tossize)) == NULL)
    374 		err(EXIT_FAILURE, "cannot allocate %zu tos space",
    375 		    (size_t)kvp->gpm.tossize);
    376 	if (kflag) {
    377 		i = kvm_read(kvp->kd, (u_long)kvp->gpm.tos, (void *)tos,
    378 		    (size_t)kvp->gpm.tossize);
    379 	} else {
    380 		mib[2] = GPROF_TOS;
    381 		i = kvp->gpm.tossize;
    382 		if (sysctl(mib, 3, tos, &i, NULL, 0) < 0)
    383 			i = 0;
    384 	}
    385 	if (i != kvp->gpm.tossize)
    386 		errx(EXIT_FAILURE, "read tos: read %zu, got %zu: %s",
    387 		    (size_t)kvp->gpm.tossize, i,
    388 		    kflag ? kvm_geterr(kvp->kd) : strerror(errno));
    389 	if (debug)
    390 		(void)fprintf(stderr, "%s: lowpc 0x%lx, textsize 0x%lx\n",
    391 		     getprogname(), kvp->gpm.lowpc, kvp->gpm.textsize);
    392 	endfrom = kvp->gpm.fromssize / sizeof(*froms);
    393 	for (fromindex = 0; fromindex < endfrom; ++fromindex) {
    394 		if (froms[fromindex] == 0)
    395 			continue;
    396 		frompc = (u_long)kvp->gpm.lowpc +
    397 		    (fromindex * kvp->gpm.hashfraction * sizeof(*froms));
    398 		for (toindex = froms[fromindex]; toindex != 0;
    399 		   toindex = tos[toindex].link) {
    400 			if (debug)
    401 			    (void)fprintf(stderr,
    402 			    "%s: [mcleanup] frompc 0x%lx selfpc 0x%lx count %ld\n",
    403 			    getprogname(), frompc, tos[toindex].selfpc,
    404 			    tos[toindex].count);
    405 			rawarc.raw_frompc = frompc;
    406 			rawarc.raw_selfpc = (u_long)tos[toindex].selfpc;
    407 			rawarc.raw_count = tos[toindex].count;
    408 			if (fwrite(&rawarc, sizeof(rawarc), 1,fp) != 1){
    409 				err(EXIT_FAILURE,
    410 				    "writing raw arc to gmon.out");
    411 			}
    412 		}
    413 	}
    414 	free(tos);
    415 	(void)fclose(fp);
    416 }
    417 
    418 /*
    419  * Get the profiling rate.
    420  */
    421 static int
    422 getprofhz(struct kvmvars *kvp)
    423 {
    424 	int mib[2], profrate;
    425 	size_t size;
    426 	struct clockinfo clockrate;
    427 
    428 	if (kflag) {
    429 		profrate = 1;
    430 		if (kvm_read(kvp->kd, nl[N_PROFHZ].n_value, &profrate,
    431 		    sizeof profrate) != sizeof profrate)
    432 			warnx("get clockrate: %s", kvm_geterr(kvp->kd));
    433 		return (profrate);
    434 	}
    435 	mib[0] = CTL_KERN;
    436 	mib[1] = KERN_CLOCKRATE;
    437 	clockrate.profhz = 1;
    438 	size = sizeof clockrate;
    439 	if (sysctl(mib, 2, &clockrate, &size, NULL, 0) < 0)
    440 		warn("get clockrate");
    441 	return (clockrate.profhz);
    442 }
    443 
    444 /*
    445  * Reset the kernel profiling date structures.
    446  */
    447 static void
    448 reset(struct kvmvars *kvp)
    449 {
    450 	char *zbuf;
    451 	size_t biggest;
    452 	int mib[3];
    453 
    454 	setprof(kvp, GMON_PROF_OFF);
    455 
    456 	biggest = (size_t)kvp->gpm.kcountsize;
    457 	if ((size_t)kvp->gpm.fromssize > biggest)
    458 		biggest = (size_t)kvp->gpm.fromssize;
    459 	if ((size_t)kvp->gpm.tossize > biggest)
    460 		biggest = (size_t)kvp->gpm.tossize;
    461 	if ((zbuf = malloc(biggest)) == NULL)
    462 		err(EXIT_FAILURE, "cannot allocate zbuf space");
    463 	(void)memset(zbuf, 0, biggest);
    464 	if (kflag) {
    465 		if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf,
    466 		    (size_t)kvp->gpm.kcountsize) != kvp->gpm.kcountsize)
    467 			errx(EXIT_FAILURE, "tickbuf zero: %s",
    468 			     kvm_geterr(kvp->kd));
    469 		if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf,
    470 		    (size_t)kvp->gpm.fromssize) != kvp->gpm.fromssize)
    471 			errx(EXIT_FAILURE, "froms zero: %s",
    472 			     kvm_geterr(kvp->kd));
    473 		if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf,
    474 		    (size_t)kvp->gpm.tossize) != kvp->gpm.tossize)
    475 			errx(EXIT_FAILURE, "tos zero: %s", kvm_geterr(kvp->kd));
    476 		free(zbuf);
    477 		return;
    478 	}
    479 	(void)seteuid(0);
    480 	mib[0] = CTL_KERN;
    481 	mib[1] = KERN_PROF;
    482 	mib[2] = GPROF_COUNT;
    483 	if (sysctl(mib, 3, NULL, NULL, zbuf, (size_t)kvp->gpm.kcountsize) < 0)
    484 		err(EXIT_FAILURE, "tickbuf zero");
    485 	mib[2] = GPROF_FROMS;
    486 	if (sysctl(mib, 3, NULL, NULL, zbuf, (size_t)kvp->gpm.fromssize) < 0)
    487 		err(EXIT_FAILURE, "froms zero");
    488 	mib[2] = GPROF_TOS;
    489 	if (sysctl(mib, 3, NULL, NULL, zbuf, (size_t)kvp->gpm.tossize) < 0)
    490 		err(EXIT_FAILURE, "tos zero");
    491 	(void)seteuid(getuid());
    492 	free(zbuf);
    493 }
    494