xref: /386bsd/usr/src/usr.sbin/kgmon/kgmon.c (revision a2142627)
1 /*
2  * Copyright (c) 1983 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
37  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 static char sccsid[] = "@(#)kgmon.c	5.12 (Berkeley) 7/1/91";
42 #endif /* not lint */
43 
44 #include <sys/param.h>
45 #include <sys/file.h>
46 #include <sys/gprof.h>
47 #include <stdio.h>
48 #include <nlist.h>
49 #include <ctype.h>
50 #include <paths.h>
51 
52 #define	PROFILING_ON	0
53 #define	PROFILING_OFF	3
54 
55 u_long	s_textsize;
56 off_t	sbuf, klseek(), lseek();
57 int	ssiz;
58 
59 struct nlist nl[] = {
60 #define	N_SYSMAP	0
61 	{ "_Sysmap" },
62 #define	N_SYSSIZE	1
63 	{ "_Syssize" },
64 #define N_FROMS		2
65 	{ "_froms" },
66 #define	N_PROFILING	3
67 	{ "_profiling" },
68 #define	N_S_LOWPC	4
69 	{ "_s_lowpc" },
70 #define	N_S_TEXTSIZE	5
71 	{ "_s_textsize" },
72 #define	N_SBUF		6
73 	{ "_sbuf" },
74 #define N_SSIZ		7
75 	{ "_ssiz" },
76 #define	N_TOS		8
77 	{ "_tos" },
78 	0,
79 };
80 
81 struct	pte *Sysmap;
82 
83 int	kmem;
84 int	bflag, hflag, kflag, rflag, pflag;
85 int	debug = 0;
86 
main(argc,argv)87 main(argc, argv)
88 	int argc;
89 	char **argv;
90 {
91 	extern char *optarg;
92 	extern int optind;
93 	int ch, mode, disp, openmode;
94 	char *system, *kmemf, *malloc();
95 
96 	kmemf = _PATH_KMEM;
97 	system = _PATH_UNIX;
98 	while ((ch = getopt(argc, argv, "M:N:bhpr")) != EOF)
99 		switch((char)ch) {
100 		case 'M':
101 			kmemf = optarg;
102 			kflag = 1;
103 			break;
104 		case 'N':
105 			system = optarg;
106 			break;
107 		case 'b':
108 			bflag = 1;
109 			break;
110 		case 'h':
111 			hflag = 1;
112 			break;
113 		case 'p':
114 			pflag = 1;
115 			break;
116 		case 'r':
117 			rflag = 1;
118 			break;
119 		default:
120 			(void)fprintf(stderr,
121 			    "usage: kgmon [-bhrp] [-M core] [-N system]\n");
122 			exit(1);
123 		}
124 	argc -= optind;
125 	argv += optind;
126 
127 #define BACKWARD_COMPATIBILITY
128 #ifdef	BACKWARD_COMPATIBILITY
129 	if (*argv) {
130 		system = *argv;
131 		if (*++argv) {
132 			kmemf = *argv;
133 			++kflag;
134 		}
135 	}
136 #endif
137 
138 	if (nlist(system, nl) < 0 || nl[0].n_type == 0) {
139 		(void)fprintf(stderr, "kgmon: %s: no namelist\n", system);
140 		exit(2);
141 	}
142 	if (!nl[N_PROFILING].n_value) {
143 		(void)fprintf(stderr,
144 		    "kgmon: profiling: not defined in kernel.\n");
145 		exit(10);
146 	}
147 
148 	openmode = (bflag || hflag || pflag || rflag) ? O_RDWR : O_RDONLY;
149 	kmem = open(kmemf, openmode);
150 	if (kmem < 0) {
151 		openmode = O_RDONLY;
152 		kmem = open(kmemf, openmode);
153 		if (kmem < 0) {
154 			perror(kmemf);
155 			exit(3);
156 		}
157 		(void)fprintf(stderr, "%s opened read-only\n", kmemf);
158 		if (rflag)
159 			(void)fprintf(stderr, "-r supressed\n");
160 		if (bflag)
161 			(void)fprintf(stderr, "-b supressed\n");
162 		if (hflag)
163 			(void)fprintf(stderr, "-h supressed\n");
164 		rflag = bflag = hflag = 0;
165 	}
166 	if (kflag) {
167 #define NEWVM
168 #ifdef NEWVM
169 		(void)fprintf(stderr,
170 		    "kgmon: can't do core files yet\n");
171 		exit(1);
172 #else
173 		off_t off;
174 
175 		Sysmap = (struct pte *)
176 		   malloc((u_int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
177 		if (!Sysmap) {
178 			(void)fprintf(stderr,
179 			    "kgmon: can't get memory for Sysmap.\n");
180 			exit(1);
181 		}
182 		off = nl[N_SYSMAP].n_value & ~KERNBASE;
183 		(void)lseek(kmem, off, L_SET);
184 		(void)read(kmem, (char *)Sysmap,
185 		    (int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
186 #endif
187 	}
188 	mode = kfetch(N_PROFILING);
189 	if (hflag)
190 		disp = PROFILING_OFF;
191 	else if (bflag)
192 		disp = PROFILING_ON;
193 	else
194 		disp = mode;
195 	if (pflag) {
196 		if (openmode == O_RDONLY && mode == PROFILING_ON)
197 			(void)fprintf(stderr, "data may be inconsistent\n");
198 		dumpstate();
199 	}
200 	if (rflag)
201 		resetstate();
202 	turnonoff(disp);
203 	(void)fprintf(stdout,
204 	    "kernel profiling is %s.\n", disp ? "off" : "running");
205 	exit(0);
206 }
207 
dumpstate()208 dumpstate()
209 {
210 	extern int errno;
211 	struct rawarc rawarc;
212 	struct tostruct *tos;
213 	u_long frompc;
214 	off_t kfroms, ktos;
215 	u_short *froms;		/* froms is a bunch of u_shorts indexing tos */
216 	int i, fd, fromindex, endfrom, fromssize, tossize, toindex;
217 	char buf[BUFSIZ], *s_lowpc, *malloc(), *strerror();
218 
219 	turnonoff(PROFILING_OFF);
220 	fd = creat("gmon.out", 0666);
221 	if (fd < 0) {
222 		perror("gmon.out");
223 		return;
224 	}
225 	ssiz = kfetch(N_SSIZ);
226 	sbuf = kfetch(N_SBUF);
227 	(void)klseek(kmem, (off_t)sbuf, L_SET);
228 	for (i = ssiz; i > 0; i -= BUFSIZ) {
229 		read(kmem, buf, i < BUFSIZ ? i : BUFSIZ);
230 		write(fd, buf, i < BUFSIZ ? i : BUFSIZ);
231 	}
232 	s_textsize = kfetch(N_S_TEXTSIZE);
233 	fromssize = s_textsize / HASHFRACTION;
234 	froms = (u_short *)malloc((u_int)fromssize);
235 	kfroms = kfetch(N_FROMS);
236 	(void)klseek(kmem, kfroms, L_SET);
237 	i = read(kmem, ((char *)(froms)), fromssize);
238 	if (i != fromssize) {
239 		(void)fprintf(stderr, "read kmem: request %d, got %d: %s",
240 		    fromssize, i, strerror(errno));
241 		exit(5);
242 	}
243 	tossize = (s_textsize * ARCDENSITY / 100) * sizeof(struct tostruct);
244 	tos = (struct tostruct *)malloc((u_int)tossize);
245 	ktos = kfetch(N_TOS);
246 	(void)klseek(kmem, ktos, L_SET);
247 	i = read(kmem, ((char *)(tos)), tossize);
248 	if (i != tossize) {
249 		(void)fprintf(stderr, "read kmem: request %d, got %d: %s",
250 		    tossize, i, strerror(errno));
251 		exit(6);
252 	}
253 	s_lowpc = (char *)kfetch(N_S_LOWPC);
254 	if (debug)
255 		(void)fprintf(stderr, "s_lowpc 0x%x, s_textsize 0x%x\n",
256 		    s_lowpc, s_textsize);
257 	endfrom = fromssize / sizeof(*froms);
258 	for (fromindex = 0; fromindex < endfrom; fromindex++) {
259 		if (froms[fromindex] == 0)
260 			continue;
261 		frompc = (u_long)s_lowpc +
262 		    (fromindex * HASHFRACTION * sizeof(*froms));
263 		for (toindex = froms[fromindex]; toindex != 0;
264 		   toindex = tos[toindex].link) {
265 			if (debug)
266 			    (void)fprintf(stderr,
267 			    "[mcleanup] frompc 0x%x selfpc 0x%x count %d\n" ,
268 			    frompc, tos[toindex].selfpc, tos[toindex].count);
269 			rawarc.raw_frompc = frompc;
270 			rawarc.raw_selfpc = (u_long)tos[toindex].selfpc;
271 			rawarc.raw_count = tos[toindex].count;
272 			write(fd, (char *)&rawarc, sizeof (rawarc));
273 		}
274 	}
275 	close(fd);
276 }
277 
resetstate()278 resetstate()
279 {
280 	off_t kfroms, ktos;
281 	int i, fromssize, tossize;
282 	char buf[BUFSIZ];
283 
284 	turnonoff(PROFILING_OFF);
285 	bzero(buf, BUFSIZ);
286 	ssiz = kfetch(N_SSIZ);
287 	sbuf = kfetch(N_SBUF);
288 	ssiz -= sizeof(struct phdr);
289 	sbuf += sizeof(struct phdr);
290 	(void)klseek(kmem, (off_t)sbuf, L_SET);
291 	for (i = ssiz; i > 0; i -= BUFSIZ)
292 		if (write(kmem, buf, i < BUFSIZ ? i : BUFSIZ) < 0) {
293 			perror("sbuf write");
294 			exit(7);
295 		}
296 	s_textsize = kfetch(N_S_TEXTSIZE);
297 	fromssize = s_textsize / HASHFRACTION;
298 	kfroms = kfetch(N_FROMS);
299 	(void)klseek(kmem, kfroms, L_SET);
300 	for (i = fromssize; i > 0; i -= BUFSIZ)
301 		if (write(kmem, buf, i < BUFSIZ ? i : BUFSIZ) < 0) {
302 			perror("kforms write");
303 			exit(8);
304 		}
305 	tossize = (s_textsize * ARCDENSITY / 100) * sizeof(struct tostruct);
306 	ktos = kfetch(N_TOS);
307 	(void)klseek(kmem, ktos, L_SET);
308 	for (i = tossize; i > 0; i -= BUFSIZ)
309 		if (write(kmem, buf, i < BUFSIZ ? i : BUFSIZ) < 0) {
310 			perror("ktos write");
311 			exit(9);
312 		}
313 }
314 
turnonoff(onoff)315 turnonoff(onoff)
316 	int onoff;
317 {
318 	(void)klseek(kmem, (long)nl[N_PROFILING].n_value, L_SET);
319 	(void)write(kmem, (char *)&onoff, sizeof (onoff));
320 }
321 
kfetch(index)322 kfetch(index)
323 	int index;
324 {
325 	off_t off;
326 	int value;
327 
328 	if ((off = nl[index].n_value) == 0) {
329 		printf("%s: not defined in kernel\n", nl[index].n_name);
330 		exit(11);
331 	}
332 	if (klseek(kmem, off, L_SET) == -1) {
333 		perror("lseek");
334 		exit(12);
335 	}
336 	if (read(kmem, (char *)&value, sizeof (value)) != sizeof (value)) {
337 		perror("read");
338 		exit(13);
339 	}
340 	return (value);
341 }
342 
343 off_t
klseek(fd,base,off)344 klseek(fd, base, off)
345 	int fd, off;
346 	off_t base;
347 {
348 
349 #ifndef NEWVM
350 	if (kflag) {	/* get kernel pte */
351 		base &= ~KERNBASE;
352 		base = ctob(Sysmap[btop(base)].pg_pfnum) + (base & PGOFSET);
353 	}
354 #endif
355 	return (lseek(fd, base, off));
356 }
357