xref: /netbsd-src/usr.sbin/kgmon/kgmon.c (revision ce2c90c7c172d95d2402a5b3d96d8f8e6d138a21)
1 /*	$NetBSD: kgmon.c,v 1.20 2006/08/13 00:20:25 dyoung 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.20 2006/08/13 00:20:25 dyoung 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" },
65 #define	N_PROFHZ	1
66 	{ "_profhz" },
67 	{ 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 *system, *kmemf;
91 
92 	setprogname(argv[0]);
93 	seteuid(getuid());
94 	kmemf = NULL;
95 	system = 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 			system = 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 		system = *argv;
142 		if (*++argv) {
143 			kmemf = *argv;
144 			++kflag;
145 		}
146 	}
147 #endif
148 	accessmode = openfiles(system, 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 *system, 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(system, kmemf, NULL, openmode, errbuf);
196 	if (kvp->kd == NULL) {
197 		if (openmode == O_RDWR) {
198 			openmode = O_RDONLY;
199 			kvp->kd = kvm_openfiles(system, 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", system);
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 
307 	setprof(kvp, GMON_PROF_OFF);
308 	fp = fopen("gmon.out", "w");
309 	if (fp == NULL) {
310 		warn("cannot open `gmon.out'");
311 		return;
312 	}
313 
314 	/*
315 	 * Build the gmon header and write it to a file.
316 	 */
317 	bzero(&h, sizeof(h));
318 	h.lpc = kvp->gpm.lowpc;
319 	h.hpc = kvp->gpm.highpc;
320 	h.ncnt = kvp->gpm.kcountsize + sizeof(h);
321 	h.version = GMONVERSION;
322 	h.profrate = getprofhz(kvp);
323 	if (fwrite((char *)&h, sizeof(h), 1, fp) != 1)
324 		err(EXIT_FAILURE, "writing header to gmon.out");
325 
326 	/*
327 	 * Write out the tick buffer.
328 	 */
329 	mib[0] = CTL_KERN;
330 	mib[1] = KERN_PROF;
331 	if ((tickbuf = malloc(kvp->gpm.kcountsize)) == NULL)
332 		err(EXIT_FAILURE, "cannot allocate kcount space");
333 	if (kflag) {
334 		i = kvm_read(kvp->kd, (u_long)kvp->gpm.kcount, (void *)tickbuf,
335 		    kvp->gpm.kcountsize);
336 	} else {
337 		mib[2] = GPROF_COUNT;
338 		i = kvp->gpm.kcountsize;
339 		if (sysctl(mib, 3, tickbuf, &i, NULL, 0) < 0)
340 			i = 0;
341 	}
342 	if (i != kvp->gpm.kcountsize)
343 		errx(EXIT_FAILURE, "read ticks: read %lu, got %lu: %s",
344 		    kvp->gpm.kcountsize, (u_long)i,
345 		    kflag ? kvm_geterr(kvp->kd) : strerror(errno));
346 	if ((fwrite(tickbuf, kvp->gpm.kcountsize, 1, fp)) != 1)
347 		err(EXIT_FAILURE, "writing tocks to gmon.out");
348 	free(tickbuf);
349 
350 	/*
351 	 * Write out the arc info.
352 	 */
353 	if ((froms = malloc(kvp->gpm.fromssize)) == NULL)
354 		err(EXIT_FAILURE, "cannot allocate froms space");
355 	if (kflag) {
356 		i = kvm_read(kvp->kd, (u_long)kvp->gpm.froms, (void *)froms,
357 		    kvp->gpm.fromssize);
358 	} else {
359 		mib[2] = GPROF_FROMS;
360 		i = kvp->gpm.fromssize;
361 		if (sysctl(mib, 3, froms, &i, NULL, 0) < 0)
362 			i = 0;
363 	}
364 	if (i != kvp->gpm.fromssize)
365 		errx(EXIT_FAILURE, "read froms: read %lu, got %lu: %s",
366 		    kvp->gpm.fromssize, (u_long)i,
367 		    kflag ? kvm_geterr(kvp->kd) : strerror(errno));
368 	if ((tos = malloc(kvp->gpm.tossize)) == NULL)
369 		err(EXIT_FAILURE, "cannot allocate tos space");
370 	if (kflag) {
371 		i = kvm_read(kvp->kd, (u_long)kvp->gpm.tos, (void *)tos,
372 		    kvp->gpm.tossize);
373 	} else {
374 		mib[2] = GPROF_TOS;
375 		i = kvp->gpm.tossize;
376 		if (sysctl(mib, 3, tos, &i, NULL, 0) < 0)
377 			i = 0;
378 	}
379 	if (i != kvp->gpm.tossize)
380 		errx(EXIT_FAILURE, "read tos: read %lu, got %lu: %s",
381 		    kvp->gpm.tossize, (u_long)i,
382 		    kflag ? kvm_geterr(kvp->kd) : strerror(errno));
383 	if (debug)
384 		(void)fprintf(stderr, "%s: lowpc 0x%lx, textsize 0x%lx\n",
385 		     getprogname(), kvp->gpm.lowpc, kvp->gpm.textsize);
386 	endfrom = kvp->gpm.fromssize / sizeof(*froms);
387 	for (fromindex = 0; fromindex < endfrom; ++fromindex) {
388 		if (froms[fromindex] == 0)
389 			continue;
390 		frompc = (u_long)kvp->gpm.lowpc +
391 		    (fromindex * kvp->gpm.hashfraction * sizeof(*froms));
392 		for (toindex = froms[fromindex]; toindex != 0;
393 		   toindex = tos[toindex].link) {
394 			if (debug)
395 			    (void)fprintf(stderr,
396 			    "%s: [mcleanup] frompc 0x%lx selfpc 0x%lx count %ld\n",
397 			    getprogname(), frompc, tos[toindex].selfpc,
398 			    tos[toindex].count);
399 			rawarc.raw_frompc = frompc;
400 			rawarc.raw_selfpc = (u_long)tos[toindex].selfpc;
401 			rawarc.raw_count = tos[toindex].count;
402 			if (fwrite((char *)&rawarc, sizeof(rawarc), 1,fp) != 1){
403 				err(EXIT_FAILURE,
404 				    "writing raw arc to gmon.out");
405 			}
406 		}
407 	}
408 	free(tos);
409 	fclose(fp);
410 }
411 
412 /*
413  * Get the profiling rate.
414  */
415 static int
416 getprofhz(struct kvmvars *kvp)
417 {
418 	int mib[2], profrate;
419 	size_t size;
420 	struct clockinfo clockrate;
421 
422 	if (kflag) {
423 		profrate = 1;
424 		if (kvm_read(kvp->kd, nl[N_PROFHZ].n_value, &profrate,
425 		    sizeof profrate) != sizeof profrate)
426 			warnx("get clockrate: %s", kvm_geterr(kvp->kd));
427 		return (profrate);
428 	}
429 	mib[0] = CTL_KERN;
430 	mib[1] = KERN_CLOCKRATE;
431 	clockrate.profhz = 1;
432 	size = sizeof clockrate;
433 	if (sysctl(mib, 2, &clockrate, &size, NULL, 0) < 0)
434 		warn("get clockrate");
435 	return (clockrate.profhz);
436 }
437 
438 /*
439  * Reset the kernel profiling date structures.
440  */
441 static void
442 reset(struct kvmvars *kvp)
443 {
444 	char *zbuf;
445 	u_long biggest;
446 	int mib[3];
447 
448 	setprof(kvp, GMON_PROF_OFF);
449 
450 	biggest = kvp->gpm.kcountsize;
451 	if (kvp->gpm.fromssize > biggest)
452 		biggest = kvp->gpm.fromssize;
453 	if (kvp->gpm.tossize > biggest)
454 		biggest = kvp->gpm.tossize;
455 	if ((zbuf = malloc(biggest)) == NULL)
456 		err(EXIT_FAILURE, "cannot allocate zbuf space");
457 	bzero(zbuf, biggest);
458 	if (kflag) {
459 		if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf,
460 		    kvp->gpm.kcountsize) != kvp->gpm.kcountsize)
461 			errx(EXIT_FAILURE, "tickbuf zero: %s",
462 			     kvm_geterr(kvp->kd));
463 		if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf,
464 		    kvp->gpm.fromssize) != kvp->gpm.fromssize)
465 			errx(EXIT_FAILURE, "froms zero: %s",
466 			     kvm_geterr(kvp->kd));
467 		if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf,
468 		    kvp->gpm.tossize) != kvp->gpm.tossize)
469 			errx(EXIT_FAILURE, "tos zero: %s", kvm_geterr(kvp->kd));
470 		free(zbuf);
471 		return;
472 	}
473 	(void)seteuid(0);
474 	mib[0] = CTL_KERN;
475 	mib[1] = KERN_PROF;
476 	mib[2] = GPROF_COUNT;
477 	if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.kcountsize) < 0)
478 		err(EXIT_FAILURE, "tickbuf zero");
479 	mib[2] = GPROF_FROMS;
480 	if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.fromssize) < 0)
481 		err(EXIT_FAILURE, "froms zero");
482 	mib[2] = GPROF_TOS;
483 	if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.tossize) < 0)
484 		err(EXIT_FAILURE, "tos zero");
485 	(void)seteuid(getuid());
486 	free(zbuf);
487 }
488