kgmon.c revision 1.1 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.1 cgd static char sccsid[] = "@(#)kgmon.c 5.12 (Berkeley) 7/1/91";
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd #include <sys/param.h>
45 1.1 cgd #if BSD >= 199103
46 1.1 cgd #define NEWVM
47 1.1 cgd #endif
48 1.1 cgd #include <sys/file.h>
49 1.1 cgd #ifndef NEWVM
50 1.1 cgd #include <machine/pte.h>
51 1.1 cgd #include <sys/vm.h>
52 1.1 cgd #endif
53 1.1 cgd #include <sys/gprof.h>
54 1.1 cgd #include <stdio.h>
55 1.1 cgd #include <nlist.h>
56 1.1 cgd #include <ctype.h>
57 1.1 cgd #include <paths.h>
58 1.1 cgd
59 1.1 cgd #define PROFILING_ON 0
60 1.1 cgd #define PROFILING_OFF 3
61 1.1 cgd
62 1.1 cgd u_long s_textsize;
63 1.1 cgd off_t sbuf, klseek(), lseek();
64 1.1 cgd int ssiz;
65 1.1 cgd
66 1.1 cgd struct nlist nl[] = {
67 1.1 cgd #define N_SYSMAP 0
68 1.1 cgd { "_Sysmap" },
69 1.1 cgd #define N_SYSSIZE 1
70 1.1 cgd { "_Syssize" },
71 1.1 cgd #define N_FROMS 2
72 1.1 cgd { "_froms" },
73 1.1 cgd #define N_PROFILING 3
74 1.1 cgd { "_profiling" },
75 1.1 cgd #define N_S_LOWPC 4
76 1.1 cgd { "_s_lowpc" },
77 1.1 cgd #define N_S_TEXTSIZE 5
78 1.1 cgd { "_s_textsize" },
79 1.1 cgd #define N_SBUF 6
80 1.1 cgd { "_sbuf" },
81 1.1 cgd #define N_SSIZ 7
82 1.1 cgd { "_ssiz" },
83 1.1 cgd #define N_TOS 8
84 1.1 cgd { "_tos" },
85 1.1 cgd 0,
86 1.1 cgd };
87 1.1 cgd
88 1.1 cgd struct pte *Sysmap;
89 1.1 cgd
90 1.1 cgd int kmem;
91 1.1 cgd int bflag, hflag, kflag, rflag, pflag;
92 1.1 cgd int debug = 0;
93 1.1 cgd
94 1.1 cgd main(argc, argv)
95 1.1 cgd int argc;
96 1.1 cgd char **argv;
97 1.1 cgd {
98 1.1 cgd extern char *optarg;
99 1.1 cgd extern int optind;
100 1.1 cgd int ch, mode, disp, openmode;
101 1.1 cgd char *system, *kmemf, *malloc();
102 1.1 cgd
103 1.1 cgd kmemf = _PATH_KMEM;
104 1.1 cgd system = _PATH_UNIX;
105 1.1 cgd while ((ch = getopt(argc, argv, "M:N:bhpr")) != EOF)
106 1.1 cgd switch((char)ch) {
107 1.1 cgd case 'M':
108 1.1 cgd kmemf = optarg;
109 1.1 cgd kflag = 1;
110 1.1 cgd break;
111 1.1 cgd case 'N':
112 1.1 cgd system = optarg;
113 1.1 cgd break;
114 1.1 cgd case 'b':
115 1.1 cgd bflag = 1;
116 1.1 cgd break;
117 1.1 cgd case 'h':
118 1.1 cgd hflag = 1;
119 1.1 cgd break;
120 1.1 cgd case 'p':
121 1.1 cgd pflag = 1;
122 1.1 cgd break;
123 1.1 cgd case 'r':
124 1.1 cgd rflag = 1;
125 1.1 cgd break;
126 1.1 cgd default:
127 1.1 cgd (void)fprintf(stderr,
128 1.1 cgd "usage: kgmon [-bhrp] [-M core] [-N system]\n");
129 1.1 cgd exit(1);
130 1.1 cgd }
131 1.1 cgd argc -= optind;
132 1.1 cgd argv += optind;
133 1.1 cgd
134 1.1 cgd #define BACKWARD_COMPATIBILITY
135 1.1 cgd #ifdef BACKWARD_COMPATIBILITY
136 1.1 cgd if (*argv) {
137 1.1 cgd system = *argv;
138 1.1 cgd if (*++argv) {
139 1.1 cgd kmemf = *argv;
140 1.1 cgd ++kflag;
141 1.1 cgd }
142 1.1 cgd }
143 1.1 cgd #endif
144 1.1 cgd
145 1.1 cgd if (nlist(system, nl) < 0 || nl[0].n_type == 0) {
146 1.1 cgd (void)fprintf(stderr, "kgmon: %s: no namelist\n", system);
147 1.1 cgd exit(2);
148 1.1 cgd }
149 1.1 cgd if (!nl[N_PROFILING].n_value) {
150 1.1 cgd (void)fprintf(stderr,
151 1.1 cgd "kgmon: profiling: not defined in kernel.\n");
152 1.1 cgd exit(10);
153 1.1 cgd }
154 1.1 cgd
155 1.1 cgd openmode = (bflag || hflag || pflag || rflag) ? O_RDWR : O_RDONLY;
156 1.1 cgd kmem = open(kmemf, openmode);
157 1.1 cgd if (kmem < 0) {
158 1.1 cgd openmode = O_RDONLY;
159 1.1 cgd kmem = open(kmemf, openmode);
160 1.1 cgd if (kmem < 0) {
161 1.1 cgd perror(kmemf);
162 1.1 cgd exit(3);
163 1.1 cgd }
164 1.1 cgd (void)fprintf(stderr, "%s opened read-only\n", kmemf);
165 1.1 cgd if (rflag)
166 1.1 cgd (void)fprintf(stderr, "-r supressed\n");
167 1.1 cgd if (bflag)
168 1.1 cgd (void)fprintf(stderr, "-b supressed\n");
169 1.1 cgd if (hflag)
170 1.1 cgd (void)fprintf(stderr, "-h supressed\n");
171 1.1 cgd rflag = bflag = hflag = 0;
172 1.1 cgd }
173 1.1 cgd if (kflag) {
174 1.1 cgd #ifdef NEWVM
175 1.1 cgd (void)fprintf(stderr,
176 1.1 cgd "kgmon: can't do core files yet\n");
177 1.1 cgd exit(1);
178 1.1 cgd #else
179 1.1 cgd off_t off;
180 1.1 cgd
181 1.1 cgd Sysmap = (struct pte *)
182 1.1 cgd malloc((u_int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
183 1.1 cgd if (!Sysmap) {
184 1.1 cgd (void)fprintf(stderr,
185 1.1 cgd "kgmon: can't get memory for Sysmap.\n");
186 1.1 cgd exit(1);
187 1.1 cgd }
188 1.1 cgd off = nl[N_SYSMAP].n_value & ~KERNBASE;
189 1.1 cgd (void)lseek(kmem, off, L_SET);
190 1.1 cgd (void)read(kmem, (char *)Sysmap,
191 1.1 cgd (int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
192 1.1 cgd #endif
193 1.1 cgd }
194 1.1 cgd mode = kfetch(N_PROFILING);
195 1.1 cgd if (hflag)
196 1.1 cgd disp = PROFILING_OFF;
197 1.1 cgd else if (bflag)
198 1.1 cgd disp = PROFILING_ON;
199 1.1 cgd else
200 1.1 cgd disp = mode;
201 1.1 cgd if (pflag) {
202 1.1 cgd if (openmode == O_RDONLY && mode == PROFILING_ON)
203 1.1 cgd (void)fprintf(stderr, "data may be inconsistent\n");
204 1.1 cgd dumpstate();
205 1.1 cgd }
206 1.1 cgd if (rflag)
207 1.1 cgd resetstate();
208 1.1 cgd turnonoff(disp);
209 1.1 cgd (void)fprintf(stdout,
210 1.1 cgd "kernel profiling is %s.\n", disp ? "off" : "running");
211 1.1 cgd exit(0);
212 1.1 cgd }
213 1.1 cgd
214 1.1 cgd dumpstate()
215 1.1 cgd {
216 1.1 cgd extern int errno;
217 1.1 cgd struct rawarc rawarc;
218 1.1 cgd struct tostruct *tos;
219 1.1 cgd u_long frompc;
220 1.1 cgd off_t kfroms, ktos;
221 1.1 cgd u_short *froms; /* froms is a bunch of u_shorts indexing tos */
222 1.1 cgd int i, fd, fromindex, endfrom, fromssize, tossize, toindex;
223 1.1 cgd char buf[BUFSIZ], *s_lowpc, *malloc(), *strerror();
224 1.1 cgd
225 1.1 cgd turnonoff(PROFILING_OFF);
226 1.1 cgd fd = creat("gmon.out", 0666);
227 1.1 cgd if (fd < 0) {
228 1.1 cgd perror("gmon.out");
229 1.1 cgd return;
230 1.1 cgd }
231 1.1 cgd ssiz = kfetch(N_SSIZ);
232 1.1 cgd sbuf = kfetch(N_SBUF);
233 1.1 cgd (void)klseek(kmem, (off_t)sbuf, L_SET);
234 1.1 cgd for (i = ssiz; i > 0; i -= BUFSIZ) {
235 1.1 cgd read(kmem, buf, i < BUFSIZ ? i : BUFSIZ);
236 1.1 cgd write(fd, buf, i < BUFSIZ ? i : BUFSIZ);
237 1.1 cgd }
238 1.1 cgd s_textsize = kfetch(N_S_TEXTSIZE);
239 1.1 cgd fromssize = s_textsize / HASHFRACTION;
240 1.1 cgd froms = (u_short *)malloc((u_int)fromssize);
241 1.1 cgd kfroms = kfetch(N_FROMS);
242 1.1 cgd (void)klseek(kmem, kfroms, L_SET);
243 1.1 cgd i = read(kmem, ((char *)(froms)), fromssize);
244 1.1 cgd if (i != fromssize) {
245 1.1 cgd (void)fprintf(stderr, "read kmem: request %d, got %d: %s",
246 1.1 cgd fromssize, i, strerror(errno));
247 1.1 cgd exit(5);
248 1.1 cgd }
249 1.1 cgd tossize = (s_textsize * ARCDENSITY / 100) * sizeof(struct tostruct);
250 1.1 cgd tos = (struct tostruct *)malloc((u_int)tossize);
251 1.1 cgd ktos = kfetch(N_TOS);
252 1.1 cgd (void)klseek(kmem, ktos, L_SET);
253 1.1 cgd i = read(kmem, ((char *)(tos)), tossize);
254 1.1 cgd if (i != tossize) {
255 1.1 cgd (void)fprintf(stderr, "read kmem: request %d, got %d: %s",
256 1.1 cgd tossize, i, strerror(errno));
257 1.1 cgd exit(6);
258 1.1 cgd }
259 1.1 cgd s_lowpc = (char *)kfetch(N_S_LOWPC);
260 1.1 cgd if (debug)
261 1.1 cgd (void)fprintf(stderr, "s_lowpc 0x%x, s_textsize 0x%x\n",
262 1.1 cgd s_lowpc, s_textsize);
263 1.1 cgd endfrom = fromssize / sizeof(*froms);
264 1.1 cgd for (fromindex = 0; fromindex < endfrom; fromindex++) {
265 1.1 cgd if (froms[fromindex] == 0)
266 1.1 cgd continue;
267 1.1 cgd frompc = (u_long)s_lowpc +
268 1.1 cgd (fromindex * HASHFRACTION * sizeof(*froms));
269 1.1 cgd for (toindex = froms[fromindex]; toindex != 0;
270 1.1 cgd toindex = tos[toindex].link) {
271 1.1 cgd if (debug)
272 1.1 cgd (void)fprintf(stderr,
273 1.1 cgd "[mcleanup] frompc 0x%x selfpc 0x%x count %d\n" ,
274 1.1 cgd frompc, tos[toindex].selfpc, tos[toindex].count);
275 1.1 cgd rawarc.raw_frompc = frompc;
276 1.1 cgd rawarc.raw_selfpc = (u_long)tos[toindex].selfpc;
277 1.1 cgd rawarc.raw_count = tos[toindex].count;
278 1.1 cgd write(fd, (char *)&rawarc, sizeof (rawarc));
279 1.1 cgd }
280 1.1 cgd }
281 1.1 cgd close(fd);
282 1.1 cgd }
283 1.1 cgd
284 1.1 cgd resetstate()
285 1.1 cgd {
286 1.1 cgd off_t kfroms, ktos;
287 1.1 cgd int i, fromssize, tossize;
288 1.1 cgd char buf[BUFSIZ];
289 1.1 cgd
290 1.1 cgd turnonoff(PROFILING_OFF);
291 1.1 cgd bzero(buf, BUFSIZ);
292 1.1 cgd ssiz = kfetch(N_SSIZ);
293 1.1 cgd sbuf = kfetch(N_SBUF);
294 1.1 cgd ssiz -= sizeof(struct phdr);
295 1.1 cgd sbuf += sizeof(struct phdr);
296 1.1 cgd (void)klseek(kmem, (off_t)sbuf, L_SET);
297 1.1 cgd for (i = ssiz; i > 0; i -= BUFSIZ)
298 1.1 cgd if (write(kmem, buf, i < BUFSIZ ? i : BUFSIZ) < 0) {
299 1.1 cgd perror("sbuf write");
300 1.1 cgd exit(7);
301 1.1 cgd }
302 1.1 cgd s_textsize = kfetch(N_S_TEXTSIZE);
303 1.1 cgd fromssize = s_textsize / HASHFRACTION;
304 1.1 cgd kfroms = kfetch(N_FROMS);
305 1.1 cgd (void)klseek(kmem, kfroms, L_SET);
306 1.1 cgd for (i = fromssize; i > 0; i -= BUFSIZ)
307 1.1 cgd if (write(kmem, buf, i < BUFSIZ ? i : BUFSIZ) < 0) {
308 1.1 cgd perror("kforms write");
309 1.1 cgd exit(8);
310 1.1 cgd }
311 1.1 cgd tossize = (s_textsize * ARCDENSITY / 100) * sizeof(struct tostruct);
312 1.1 cgd ktos = kfetch(N_TOS);
313 1.1 cgd (void)klseek(kmem, ktos, L_SET);
314 1.1 cgd for (i = tossize; i > 0; i -= BUFSIZ)
315 1.1 cgd if (write(kmem, buf, i < BUFSIZ ? i : BUFSIZ) < 0) {
316 1.1 cgd perror("ktos write");
317 1.1 cgd exit(9);
318 1.1 cgd }
319 1.1 cgd }
320 1.1 cgd
321 1.1 cgd turnonoff(onoff)
322 1.1 cgd int onoff;
323 1.1 cgd {
324 1.1 cgd (void)klseek(kmem, (long)nl[N_PROFILING].n_value, L_SET);
325 1.1 cgd (void)write(kmem, (char *)&onoff, sizeof (onoff));
326 1.1 cgd }
327 1.1 cgd
328 1.1 cgd kfetch(index)
329 1.1 cgd int index;
330 1.1 cgd {
331 1.1 cgd off_t off;
332 1.1 cgd int value;
333 1.1 cgd
334 1.1 cgd if ((off = nl[index].n_value) == 0) {
335 1.1 cgd printf("%s: not defined in kernel\n", nl[index].n_name);
336 1.1 cgd exit(11);
337 1.1 cgd }
338 1.1 cgd if (klseek(kmem, off, L_SET) == -1) {
339 1.1 cgd perror("lseek");
340 1.1 cgd exit(12);
341 1.1 cgd }
342 1.1 cgd if (read(kmem, (char *)&value, sizeof (value)) != sizeof (value)) {
343 1.1 cgd perror("read");
344 1.1 cgd exit(13);
345 1.1 cgd }
346 1.1 cgd return (value);
347 1.1 cgd }
348 1.1 cgd
349 1.1 cgd off_t
350 1.1 cgd klseek(fd, base, off)
351 1.1 cgd int fd, off;
352 1.1 cgd off_t base;
353 1.1 cgd {
354 1.1 cgd
355 1.1 cgd #ifndef NEWVM
356 1.1 cgd if (kflag) { /* get kernel pte */
357 1.1 cgd base &= ~KERNBASE;
358 1.1 cgd base = ctob(Sysmap[btop(base)].pg_pfnum) + (base & PGOFSET);
359 1.1 cgd }
360 1.1 cgd #endif
361 1.1 cgd return (lseek(fd, base, off));
362 1.1 cgd }
363