install correct aliases file
[unix-history] / usr / src / usr.sbin / kgmon / kgmon.c
CommitLineData
528b0614 1/*
c82fa61b
KB
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 are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
528b0614
DF
16 */
17
18#ifndef lint
19char copyright[] =
c82fa61b 20"@(#) Copyright (c) 1983 The Regents of the University of California.\n\
528b0614 21 All rights reserved.\n";
4d32c205 22#endif /* not lint */
528b0614 23
d841a9ba 24#ifndef lint
c82fa61b 25static char sccsid[] = "@(#)kgmon.c 5.5 (Berkeley) %G%";
4d32c205 26#endif /* not lint */
d841a9ba
KM
27
28#include <sys/param.h>
01538592 29#include <machine/pte.h>
769cbf71 30#include <sys/file.h>
fa7eedc6 31#include <sys/vm.h>
d841a9ba
KM
32#include <stdio.h>
33#include <nlist.h>
34#include <ctype.h>
78e8943b 35#include <sys/gprof.h>
d841a9ba
KM
36
37#define PROFILING_ON 0
4d32c205 38#define PROFILING_OFF 3
d841a9ba 39
d841a9ba 40u_long s_textsize;
4d32c205 41off_t sbuf, klseek(), lseek();
d841a9ba 42int ssiz;
d841a9ba
KM
43
44struct nlist nl[] = {
45#define N_SYSMAP 0
46 { "_Sysmap" },
47#define N_SYSSIZE 1
48 { "_Syssize" },
49#define N_FROMS 2
50 { "_froms" },
51#define N_PROFILING 3
52 { "_profiling" },
53#define N_S_LOWPC 4
54 { "_s_lowpc" },
55#define N_S_TEXTSIZE 5
56 { "_s_textsize" },
57#define N_SBUF 6
58 { "_sbuf" },
59#define N_SSIZ 7
60 { "_ssiz" },
61#define N_TOS 8
62 { "_tos" },
63 0,
64};
65
66struct pte *Sysmap;
67
d841a9ba 68int kmem;
ecc6febd 69int bflag, hflag, kflag, rflag, pflag;
78e8943b 70int debug = 0;
d841a9ba
KM
71
72main(argc, argv)
73 int argc;
74 char *argv[];
75{
4d32c205
KB
76 extern char *optarg;
77 extern int optind;
78 int ch, mode, disp, openmode;
79 char *system, *kmemf, *malloc();
d841a9ba 80
4d32c205
KB
81 while ((ch = getopt(argc, argv, "bhpr")) != EOF)
82 switch((char)ch) {
78e8943b
KM
83 case 'b':
84 bflag++;
85 break;
86 case 'h':
87 hflag++;
78e8943b 88 break;
ecc6febd
KM
89 case 'p':
90 pflag++;
4d32c205
KB
91 break;
92 case 'r':
93 rflag++;
78e8943b
KM
94 break;
95 default:
4d32c205 96 fputs("usage: kgmon [-bhrp] [system [core]]\n", stderr);
78e8943b
KM
97 exit(1);
98 }
4d32c205
KB
99
100 openmode = (bflag || hflag || pflag || rflag) ? O_RDWR : O_RDONLY;
101
102 kmemf = "/dev/kmem";
d841a9ba
KM
103 if (argc > 0) {
104 system = *argv;
105 argv++, argc--;
4d32c205
KB
106 if (argc > 0) {
107 kmemf = *argv;
108 kflag++;
109 }
d841a9ba 110 }
4d32c205
KB
111 else
112 system = "/vmunix";
113
114 if (nlist(system, nl) < 0 || nl[0].n_type == 0) {
d841a9ba 115 fprintf(stderr, "%s: no namelist\n", system);
01538592 116 exit(2);
d841a9ba 117 }
4d32c205
KB
118 if (!nl[N_PROFILING].n_value) {
119 fputs("profiling: not defined in kernel.\n", stderr);
120 exit(10);
d841a9ba 121 }
ecc6febd 122 kmem = open(kmemf, openmode);
d841a9ba 123 if (kmem < 0) {
769cbf71 124 openmode = O_RDONLY;
ecc6febd 125 kmem = open(kmemf, openmode);
d841a9ba 126 if (kmem < 0) {
d841a9ba 127 perror(kmemf);
01538592 128 exit(3);
d841a9ba 129 }
78e8943b 130 fprintf(stderr, "%s opened read-only\n", kmemf);
78e8943b
KM
131 if (rflag)
132 fprintf(stderr, "-r supressed\n");
133 if (bflag)
134 fprintf(stderr, "-b supressed\n");
135 if (hflag)
136 fprintf(stderr, "-h supressed\n");
4d32c205 137 rflag = bflag = hflag = 0;
d841a9ba 138 }
78e8943b 139 if (kflag) {
d841a9ba
KM
140 off_t off;
141
4d32c205
KB
142 Sysmap = (struct pte *)
143 malloc((u_int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
144 if (!Sysmap) {
145 fputs("arp: can't get memory for Sysmap.\n", stderr);
146 exit(1);
d841a9ba 147 }
4d32c205
KB
148 off = nl[N_SYSMAP].n_value & ~KERNBASE;
149 (void)lseek(kmem, off, L_SET);
150 (void)read(kmem, (char *)Sysmap,
151 (int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
d841a9ba 152 }
79ec4952 153 mode = kfetch(N_PROFILING);
78e8943b
KM
154 if (hflag)
155 disp = PROFILING_OFF;
156 else if (bflag)
157 disp = PROFILING_ON;
158 else
79ec4952 159 disp = mode;
ecc6febd 160 if (pflag) {
769cbf71 161 if (openmode == O_RDONLY && mode == PROFILING_ON)
79ec4952 162 fprintf(stderr, "data may be inconsistent\n");
78e8943b 163 dumpstate();
79ec4952 164 }
78e8943b
KM
165 if (rflag)
166 resetstate();
167 turnonoff(disp);
79ec4952 168 fprintf(stdout, "kernel profiling is %s.\n", disp ? "off" : "running");
78e8943b
KM
169}
170
171dumpstate()
172{
78e8943b 173 struct rawarc rawarc;
4d32c205
KB
174 struct tostruct *tos;
175 u_long frompc;
176 off_t kfroms, ktos;
177 u_short *froms; /* froms is a bunch of u_shorts indexing tos */
178 int i, fd, fromindex, endfrom, fromssize, tossize, toindex;
179 char buf[BUFSIZ], *s_lowpc, *malloc();
78e8943b 180
d841a9ba
KM
181 turnonoff(PROFILING_OFF);
182 fd = creat("gmon.out", 0666);
183 if (fd < 0) {
184 perror("gmon.out");
185 return;
186 }
187 ssiz = kfetch(N_SSIZ);
188 sbuf = kfetch(N_SBUF);
4d32c205 189 (void)klseek(kmem, (off_t)sbuf, L_SET);
d841a9ba
KM
190 for (i = ssiz; i > 0; i -= BUFSIZ) {
191 read(kmem, buf, i < BUFSIZ ? i : BUFSIZ);
192 write(fd, buf, i < BUFSIZ ? i : BUFSIZ);
193 }
194 s_textsize = kfetch(N_S_TEXTSIZE);
78e8943b 195 fromssize = s_textsize / HASHFRACTION;
4d32c205 196 froms = (u_short *)malloc((u_int)fromssize);
d841a9ba 197 kfroms = kfetch(N_FROMS);
4d32c205 198 (void)klseek(kmem, kfroms, L_SET);
78e8943b
KM
199 i = read(kmem, ((char *)(froms)), fromssize);
200 if (i != fromssize) {
201 fprintf(stderr, "read froms: request %d, got %d", fromssize, i);
4d32c205 202 perror((char *)NULL);
78e8943b 203 exit(5);
d841a9ba 204 }
78e8943b 205 tossize = (s_textsize * ARCDENSITY / 100) * sizeof(struct tostruct);
4d32c205 206 tos = (struct tostruct *)malloc((u_int)tossize);
d841a9ba 207 ktos = kfetch(N_TOS);
4d32c205 208 (void)klseek(kmem, ktos, L_SET);
78e8943b
KM
209 i = read(kmem, ((char *)(tos)), tossize);
210 if (i != tossize) {
211 fprintf(stderr, "read tos: request %d, got %d", tossize, i);
4d32c205 212 perror((char *)NULL);
78e8943b 213 exit(6);
d841a9ba
KM
214 }
215 s_lowpc = (char *)kfetch(N_S_LOWPC);
216 if (debug)
217 fprintf(stderr, "s_lowpc 0x%x, s_textsize 0x%x\n",
218 s_lowpc, s_textsize);
78e8943b
KM
219 endfrom = fromssize / sizeof(*froms);
220 for (fromindex = 0; fromindex < endfrom; fromindex++) {
d841a9ba
KM
221 if (froms[fromindex] == 0)
222 continue;
78e8943b
KM
223 frompc = (u_long)s_lowpc +
224 (fromindex * HASHFRACTION * sizeof(*froms));
d841a9ba
KM
225 for (toindex = froms[fromindex]; toindex != 0;
226 toindex = tos[toindex].link) {
227 if (debug)
228 fprintf(stderr,
229 "[mcleanup] frompc 0x%x selfpc 0x%x count %d\n" ,
230 frompc, tos[toindex].selfpc, tos[toindex].count);
231 rawarc.raw_frompc = frompc;
232 rawarc.raw_selfpc = (u_long)tos[toindex].selfpc;
233 rawarc.raw_count = tos[toindex].count;
4d32c205 234 write(fd, (char *)&rawarc, sizeof (rawarc));
d841a9ba
KM
235 }
236 }
237 close(fd);
78e8943b
KM
238}
239
240resetstate()
241{
78e8943b 242 off_t kfroms, ktos;
4d32c205 243 int i, fromssize, tossize;
78e8943b
KM
244 char buf[BUFSIZ];
245
246 turnonoff(PROFILING_OFF);
247 bzero(buf, BUFSIZ);
248 ssiz = kfetch(N_SSIZ);
249 sbuf = kfetch(N_SBUF);
250 ssiz -= sizeof(struct phdr);
251 sbuf += sizeof(struct phdr);
4d32c205 252 (void)klseek(kmem, (off_t)sbuf, L_SET);
78e8943b
KM
253 for (i = ssiz; i > 0; i -= BUFSIZ)
254 if (write(kmem, buf, i < BUFSIZ ? i : BUFSIZ) < 0) {
255 perror("sbuf write");
01538592 256 exit(7);
78e8943b
KM
257 }
258 s_textsize = kfetch(N_S_TEXTSIZE);
259 fromssize = s_textsize / HASHFRACTION;
260 kfroms = kfetch(N_FROMS);
4d32c205 261 (void)klseek(kmem, kfroms, L_SET);
78e8943b
KM
262 for (i = fromssize; i > 0; i -= BUFSIZ)
263 if (write(kmem, buf, i < BUFSIZ ? i : BUFSIZ) < 0) {
264 perror("kforms write");
01538592 265 exit(8);
78e8943b
KM
266 }
267 tossize = (s_textsize * ARCDENSITY / 100) * sizeof(struct tostruct);
268 ktos = kfetch(N_TOS);
4d32c205 269 (void)klseek(kmem, ktos, L_SET);
78e8943b
KM
270 for (i = tossize; i > 0; i -= BUFSIZ)
271 if (write(kmem, buf, i < BUFSIZ ? i : BUFSIZ) < 0) {
272 perror("ktos write");
01538592 273 exit(9);
78e8943b 274 }
d841a9ba
KM
275}
276
277turnonoff(onoff)
278 int onoff;
279{
4d32c205
KB
280 (void)klseek(kmem, (long)nl[N_PROFILING].n_value, L_SET);
281 (void)write(kmem, (char *)&onoff, sizeof (onoff));
d841a9ba
KM
282}
283
284kfetch(index)
285 int index;
286{
287 off_t off;
288 int value;
289
290 if ((off = nl[index].n_value) == 0) {
291 printf("%s: not defined in kernel\n", nl[index].n_name);
01538592 292 exit(11);
d841a9ba 293 }
769cbf71 294 if (klseek(kmem, off, L_SET) == -1) {
d841a9ba 295 perror("lseek");
01538592 296 exit(12);
d841a9ba
KM
297 }
298 if (read(kmem, (char *)&value, sizeof (value)) != sizeof (value)) {
299 perror("read");
01538592 300 exit(13);
d841a9ba
KM
301 }
302 return (value);
303}
304
4d32c205 305off_t
d841a9ba 306klseek(fd, base, off)
4d32c205
KB
307 int fd, off;
308 off_t base;
d841a9ba 309{
4d32c205
KB
310 if (kflag) { /* get kernel pte */
311 base &= ~KERNBASE;
312 base = ctob(Sysmap[btop(base)].pg_pfnum) + (base & PGOFSET);
d841a9ba
KM
313 }
314 return (lseek(fd, base, off));
315}