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