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