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