xref: /netbsd-src/usr.sbin/kgmon/kgmon.c (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /*	$NetBSD: kgmon.c,v 1.18 2006/05/25 00:07:59 christos 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.18 2006/05/25 00:07:59 christos 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(1, "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(1, "kvm_openfiles: %s", errbuf);
204 		kern_readonly(GMON_PROF_ON);
205 	}
206 	if (kvm_nlist(kvp->kd, nl) < 0)
207 		errx(1, "%s: no namelist", system);
208 	if (!nl[N_GMONPARAM].n_value)
209 		errx(1, "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(1, "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 == 0) {
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 	fwrite((char *)&h, sizeof(h), 1, fp);
324 
325 	/*
326 	 * Write out the tick buffer.
327 	 */
328 	mib[0] = CTL_KERN;
329 	mib[1] = KERN_PROF;
330 	if ((tickbuf = malloc(kvp->gpm.kcountsize)) == NULL)
331 		err(1, "cannot allocate kcount space");
332 	if (kflag) {
333 		i = kvm_read(kvp->kd, (u_long)kvp->gpm.kcount, (void *)tickbuf,
334 		    kvp->gpm.kcountsize);
335 	} else {
336 		mib[2] = GPROF_COUNT;
337 		i = kvp->gpm.kcountsize;
338 		if (sysctl(mib, 3, tickbuf, &i, NULL, 0) < 0)
339 			i = 0;
340 	}
341 	if (i != kvp->gpm.kcountsize)
342 		errx(1, "read ticks: read %lu, got %lu: %s",
343 		    kvp->gpm.kcountsize, (u_long)i,
344 		    kflag ? kvm_geterr(kvp->kd) : strerror(errno));
345 	if ((fwrite(tickbuf, kvp->gpm.kcountsize, 1, fp)) != 1)
346 		err(1, "writing tocks to gmon.out");
347 	free(tickbuf);
348 
349 	/*
350 	 * Write out the arc info.
351 	 */
352 	if ((froms = malloc(kvp->gpm.fromssize)) == NULL)
353 		err(1, "cannot allocate froms space");
354 	if (kflag) {
355 		i = kvm_read(kvp->kd, (u_long)kvp->gpm.froms, (void *)froms,
356 		    kvp->gpm.fromssize);
357 	} else {
358 		mib[2] = GPROF_FROMS;
359 		i = kvp->gpm.fromssize;
360 		if (sysctl(mib, 3, froms, &i, NULL, 0) < 0)
361 			i = 0;
362 	}
363 	if (i != kvp->gpm.fromssize)
364 		errx(1, "read froms: read %lu, got %lu: %s",
365 		    kvp->gpm.fromssize, (u_long)i,
366 		    kflag ? kvm_geterr(kvp->kd) : strerror(errno));
367 	if ((tos = malloc(kvp->gpm.tossize)) == NULL)
368 		err(1, "cannot allocate tos space");
369 	if (kflag) {
370 		i = kvm_read(kvp->kd, (u_long)kvp->gpm.tos, (void *)tos,
371 		    kvp->gpm.tossize);
372 	} else {
373 		mib[2] = GPROF_TOS;
374 		i = kvp->gpm.tossize;
375 		if (sysctl(mib, 3, tos, &i, NULL, 0) < 0)
376 			i = 0;
377 	}
378 	if (i != kvp->gpm.tossize)
379 		errx(1, "read tos: read %lu, got %lu: %s",
380 		    kvp->gpm.tossize, (u_long)i,
381 		    kflag ? kvm_geterr(kvp->kd) : strerror(errno));
382 	if (debug)
383 		(void)fprintf(stderr, "%s: lowpc 0x%lx, textsize 0x%lx\n",
384 		     getprogname(), kvp->gpm.lowpc, kvp->gpm.textsize);
385 	endfrom = kvp->gpm.fromssize / sizeof(*froms);
386 	for (fromindex = 0; fromindex < endfrom; ++fromindex) {
387 		if (froms[fromindex] == 0)
388 			continue;
389 		frompc = (u_long)kvp->gpm.lowpc +
390 		    (fromindex * kvp->gpm.hashfraction * sizeof(*froms));
391 		for (toindex = froms[fromindex]; toindex != 0;
392 		   toindex = tos[toindex].link) {
393 			if (debug)
394 			    (void)fprintf(stderr,
395 			    "%s: [mcleanup] frompc 0x%lx selfpc 0x%lx count %ld\n",
396 			    getprogname(), frompc, tos[toindex].selfpc,
397 			    tos[toindex].count);
398 			rawarc.raw_frompc = frompc;
399 			rawarc.raw_selfpc = (u_long)tos[toindex].selfpc;
400 			rawarc.raw_count = tos[toindex].count;
401 			fwrite((char *)&rawarc, sizeof(rawarc), 1, fp);
402 		}
403 	}
404 	free(tos);
405 	fclose(fp);
406 }
407 
408 /*
409  * Get the profiling rate.
410  */
411 static int
412 getprofhz(struct kvmvars *kvp)
413 {
414 	int mib[2], profrate;
415 	size_t size;
416 	struct clockinfo clockrate;
417 
418 	if (kflag) {
419 		profrate = 1;
420 		if (kvm_read(kvp->kd, nl[N_PROFHZ].n_value, &profrate,
421 		    sizeof profrate) != sizeof profrate)
422 			warnx("get clockrate: %s", kvm_geterr(kvp->kd));
423 		return (profrate);
424 	}
425 	mib[0] = CTL_KERN;
426 	mib[1] = KERN_CLOCKRATE;
427 	clockrate.profhz = 1;
428 	size = sizeof clockrate;
429 	if (sysctl(mib, 2, &clockrate, &size, NULL, 0) < 0)
430 		warn("get clockrate");
431 	return (clockrate.profhz);
432 }
433 
434 /*
435  * Reset the kernel profiling date structures.
436  */
437 static void
438 reset(struct kvmvars *kvp)
439 {
440 	char *zbuf;
441 	u_long biggest;
442 	int mib[3];
443 
444 	setprof(kvp, GMON_PROF_OFF);
445 
446 	biggest = kvp->gpm.kcountsize;
447 	if (kvp->gpm.fromssize > biggest)
448 		biggest = kvp->gpm.fromssize;
449 	if (kvp->gpm.tossize > biggest)
450 		biggest = kvp->gpm.tossize;
451 	if ((zbuf = malloc(biggest)) == NULL)
452 		err(1, "cannot allocate zbuf space");
453 	bzero(zbuf, biggest);
454 	if (kflag) {
455 		if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf,
456 		    kvp->gpm.kcountsize) != kvp->gpm.kcountsize)
457 			errx(1, "tickbuf zero: %s", kvm_geterr(kvp->kd));
458 		if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf,
459 		    kvp->gpm.fromssize) != kvp->gpm.fromssize)
460 			errx(1, "froms zero: %s", kvm_geterr(kvp->kd));
461 		if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf,
462 		    kvp->gpm.tossize) != kvp->gpm.tossize)
463 			errx(1, "tos zero: %s", kvm_geterr(kvp->kd));
464 		free(zbuf);
465 		return;
466 	}
467 	(void)seteuid(0);
468 	mib[0] = CTL_KERN;
469 	mib[1] = KERN_PROF;
470 	mib[2] = GPROF_COUNT;
471 	if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.kcountsize) < 0)
472 		err(1, "tickbuf zero");
473 	mib[2] = GPROF_FROMS;
474 	if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.fromssize) < 0)
475 		err(1, "froms zero");
476 	mib[2] = GPROF_TOS;
477 	if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.tossize) < 0)
478 		err(1, "tos zero");
479 	(void)seteuid(getuid());
480 	free(zbuf);
481 }
482