kernel - Fix races created by a comedy of circumstansces (3)
[dragonfly.git] / usr.sbin / kgmon / kgmon.c
blob03eacecd283fc3ed73a774bb0c669767caaf1e4b
1 /*
2 * Copyright (c) 1983, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#) Copyright (c) 1983, 1992, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)kgmon.c 8.1 (Berkeley) 6/6/93
31 * $FreeBSD: src/usr.sbin/kgmon/kgmon.c,v 1.9 1999/08/28 01:16:42 peter Exp $
34 #include <sys/param.h>
35 #include <sys/file.h>
36 #include <sys/time.h>
37 #include <sys/sysctl.h>
38 #include <sys/gmon.h>
39 #include <ctype.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <kvm.h>
43 #include <limits.h>
44 #include <nlist.h>
45 #include <paths.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
51 struct nlist nl[] = {
52 #define N_GMONPARAM 0
53 { "__gmonparam" },
54 { NULL },
57 struct kvmvars {
58 kvm_t *kd;
59 struct gmonparam gpm;
62 int Bflag, bflag, hflag, kflag, rflag, pflag;
63 int debug = 0;
64 int getprof(struct kvmvars *);
65 void kern_readonly(int);
66 int openfiles(char *, char *, struct kvmvars *);
67 void setprof(struct kvmvars *kvp, int state);
68 void dumpstate(struct kvmvars *kvp);
69 void reset(struct kvmvars *kvp);
70 static void usage(void);
72 int
73 main(int argc, char **argv)
75 int ch, mode, disp, accessmode;
76 struct kvmvars kvmvars;
77 char *system, *kmemf;
79 seteuid(getuid());
80 kmemf = NULL;
81 system = NULL;
82 while ((ch = getopt(argc, argv, "M:N:Bbhpr")) != -1) {
83 switch((char)ch) {
85 case 'M':
86 kmemf = optarg;
87 kflag = 1;
88 break;
90 case 'N':
91 system = optarg;
92 break;
94 case 'B':
95 Bflag = 1;
96 break;
98 case 'b':
99 bflag = 1;
100 break;
102 case 'h':
103 hflag = 1;
104 break;
106 case 'p':
107 pflag = 1;
108 break;
110 case 'r':
111 rflag = 1;
112 break;
114 default:
115 usage();
118 argc -= optind;
119 argv += optind;
121 #define BACKWARD_COMPATIBILITY
122 #ifdef BACKWARD_COMPATIBILITY
123 if (*argv) {
124 system = *argv;
125 if (*++argv) {
126 kmemf = *argv;
127 ++kflag;
130 #endif
131 if (system == NULL)
132 system = (char *)getbootfile();
133 accessmode = openfiles(system, kmemf, &kvmvars);
134 mode = getprof(&kvmvars);
135 if (hflag)
136 disp = GMON_PROF_OFF;
137 else if (Bflag)
138 disp = GMON_PROF_HIRES;
139 else if (bflag)
140 disp = GMON_PROF_ON;
141 else
142 disp = mode;
143 if (pflag)
144 dumpstate(&kvmvars);
145 if (rflag)
146 reset(&kvmvars);
147 if (accessmode == O_RDWR)
148 setprof(&kvmvars, disp);
149 fprintf(stdout, "kgmon: kernel profiling is %s.\n",
150 disp == GMON_PROF_OFF ? "off" :
151 disp == GMON_PROF_HIRES ? "running (high resolution)" :
152 disp == GMON_PROF_ON ? "running" :
153 disp == GMON_PROF_BUSY ? "busy" :
154 disp == GMON_PROF_ERROR ? "off (error)" :
155 "in an unknown state");
156 return (0);
159 static void
160 usage(void)
162 fprintf(stderr, "usage: kgmon [-Bbhrp] [-M core] [-N system]\n");
163 exit(1);
167 * Check that profiling is enabled and open any ncessary files.
170 openfiles(char *system, char *kmemf, struct kvmvars *kvp)
172 int mib[3], state, openmode;
173 size_t size;
174 char errbuf[_POSIX2_LINE_MAX];
176 if (!kflag) {
177 mib[0] = CTL_KERN;
178 mib[1] = KERN_PROF;
179 mib[2] = GPROF_STATE;
180 size = sizeof state;
181 if (sysctl(mib, 3, &state, &size, NULL, 0) < 0)
182 errx(20, "profiling not defined in kernel");
183 if (!(Bflag || bflag || hflag || rflag ||
184 (pflag &&
185 (state == GMON_PROF_HIRES || state == GMON_PROF_ON))))
186 return (O_RDONLY);
187 seteuid(0);
188 if (sysctl(mib, 3, NULL, NULL, &state, size) >= 0)
189 return (O_RDWR);
190 seteuid(getuid());
191 kern_readonly(state);
192 return (O_RDONLY);
194 openmode = (Bflag || bflag || hflag || pflag || rflag)
195 ? O_RDWR : O_RDONLY;
196 kvp->kd = kvm_openfiles(system, kmemf, NULL, openmode, errbuf);
197 if (kvp->kd == NULL) {
198 if (openmode == O_RDWR) {
199 openmode = O_RDONLY;
200 kvp->kd = kvm_openfiles(system, kmemf, NULL, O_RDONLY,
201 errbuf);
203 if (kvp->kd == NULL)
204 errx(2, "kvm_openfiles: %s", errbuf);
205 kern_readonly(GMON_PROF_ON);
207 if (kvm_nlist(kvp->kd, nl) < 0)
208 errx(3, "%s: no namelist", system);
209 if (!nl[N_GMONPARAM].n_value)
210 errx(20, "profiling not defined in kernel");
211 return (openmode);
215 * Suppress options that require a writable kernel.
217 void
218 kern_readonly(int mode)
220 fprintf(stderr, "kgmon: kernel read-only: ");
221 if (pflag && (mode == GMON_PROF_HIRES || mode == GMON_PROF_ON))
222 fprintf(stderr, "data may be inconsistent\n");
223 if (rflag)
224 fprintf(stderr, "-r suppressed\n");
225 if (Bflag)
226 fprintf(stderr, "-B suppressed\n");
227 if (bflag)
228 fprintf(stderr, "-b suppressed\n");
229 if (hflag)
230 fprintf(stderr, "-h suppressed\n");
231 rflag = Bflag = bflag = hflag = 0;
235 * Get the state of kernel profiling.
238 getprof(struct kvmvars *kvp)
240 int mib[3];
241 size_t size;
243 if (kflag) {
244 size = kvm_read(kvp->kd, nl[N_GMONPARAM].n_value, &kvp->gpm,
245 sizeof kvp->gpm);
246 } else {
247 mib[0] = CTL_KERN;
248 mib[1] = KERN_PROF;
249 mib[2] = GPROF_GMONPARAM;
250 size = sizeof kvp->gpm;
251 if (sysctl(mib, 3, &kvp->gpm, &size, NULL, 0) < 0)
252 size = 0;
254 if (size != sizeof kvp->gpm)
255 errx(4, "cannot get gmonparam: %s",
256 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
257 return (kvp->gpm.state);
261 * Enable or disable kernel profiling according to the state variable.
263 void
264 setprof(struct kvmvars *kvp, int state)
266 struct gmonparam *p = (struct gmonparam *)nl[N_GMONPARAM].n_value;
267 int mib[3], oldstate;
268 size_t size;
270 size = sizeof(state);
271 if (!kflag) {
272 mib[0] = CTL_KERN;
273 mib[1] = KERN_PROF;
274 mib[2] = GPROF_STATE;
275 if (sysctl(mib, 3, &oldstate, &size, NULL, 0) < 0)
276 goto bad;
277 if (oldstate == state)
278 return;
279 seteuid(0);
280 if (sysctl(mib, 3, NULL, NULL, &state, size) >= 0) {
281 seteuid(getuid());
282 return;
284 seteuid(getuid());
285 } else if (kvm_write(kvp->kd, (u_long)&p->state, (void *)&state, size)
286 == size) {
287 return;
289 bad:
290 warnx("warning: cannot turn profiling %s",
291 state == GMON_PROF_OFF ? "off" : "on");
295 * Build the gmon.out file.
297 void
298 dumpstate(struct kvmvars *kvp)
300 FILE *fp;
301 struct rawarc rawarc;
302 struct tostruct *tos;
303 u_long frompc;
304 u_short *froms, *tickbuf;
305 int mib[3];
306 size_t i;
307 struct gmonhdr h;
308 int fromindex, endfrom, toindex;
310 setprof(kvp, GMON_PROF_OFF);
311 fp = fopen("gmon.out", "w");
312 if (fp == NULL) {
313 warn("gmon.out");
314 return;
318 * Build the gmon header and write it to a file.
320 bzero(&h, sizeof(h));
321 h.lpc = kvp->gpm.lowpc;
322 h.hpc = kvp->gpm.highpc;
323 h.ncnt = kvp->gpm.kcountsize + sizeof(h);
324 h.version = GMONVERSION;
325 h.profrate = kvp->gpm.profrate;
326 fwrite((char *)&h, sizeof(h), 1, fp);
329 * Write out the tick buffer.
331 mib[0] = CTL_KERN;
332 mib[1] = KERN_PROF;
333 if ((tickbuf = (u_short *)malloc(kvp->gpm.kcountsize)) == NULL)
334 errx(5, "cannot allocate kcount space");
335 if (kflag) {
336 i = kvm_read(kvp->kd, (u_long)kvp->gpm.kcount, (void *)tickbuf,
337 kvp->gpm.kcountsize);
338 } else {
339 mib[2] = GPROF_COUNT;
340 i = kvp->gpm.kcountsize;
341 if (sysctl(mib, 3, tickbuf, &i, NULL, 0) < 0)
342 i = 0;
344 if (i != kvp->gpm.kcountsize)
345 errx(6, "read ticks: read %lu, got %zd: %s",
346 kvp->gpm.kcountsize, i,
347 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
348 if ((fwrite(tickbuf, kvp->gpm.kcountsize, 1, fp)) != 1)
349 err(7, "writing tocks to gmon.out");
350 free(tickbuf);
353 * Write out the arc info.
355 if ((froms = (u_short *)malloc(kvp->gpm.fromssize)) == NULL)
356 errx(8, "cannot allocate froms space");
357 if (kflag) {
358 i = kvm_read(kvp->kd, (u_long)kvp->gpm.froms, (void *)froms,
359 kvp->gpm.fromssize);
360 } else {
361 mib[2] = GPROF_FROMS;
362 i = kvp->gpm.fromssize;
363 if (sysctl(mib, 3, froms, &i, NULL, 0) < 0)
364 i = 0;
366 if (i != kvp->gpm.fromssize)
367 errx(9, "read froms: read %lu, got %zd: %s",
368 kvp->gpm.fromssize, i,
369 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
370 if ((tos = (struct tostruct *)malloc(kvp->gpm.tossize)) == NULL)
371 errx(10, "cannot allocate tos space");
372 if (kflag) {
373 i = kvm_read(kvp->kd, (u_long)kvp->gpm.tos, (void *)tos,
374 kvp->gpm.tossize);
375 } else {
376 mib[2] = GPROF_TOS;
377 i = kvp->gpm.tossize;
378 if (sysctl(mib, 3, tos, &i, NULL, 0) < 0)
379 i = 0;
381 if (i != kvp->gpm.tossize)
382 errx(11, "read tos: read %lu, got %zd: %s",
383 kvp->gpm.tossize, i,
384 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
385 if (debug)
386 warnx("lowpc 0x%tx, textsize 0x%lx",
387 kvp->gpm.lowpc, kvp->gpm.textsize);
388 endfrom = kvp->gpm.fromssize / sizeof(*froms);
389 for (fromindex = 0; fromindex < endfrom; ++fromindex) {
390 if (froms[fromindex] == 0)
391 continue;
392 frompc = (u_long)kvp->gpm.lowpc +
393 (fromindex * kvp->gpm.hashfraction * sizeof(*froms));
394 for (toindex = froms[fromindex]; toindex != 0;
395 toindex = tos[toindex].link) {
396 if (debug)
397 warnx("[mcleanup] frompc 0x%lx selfpc 0x%lx count %ld",
398 frompc, tos[toindex].selfpc,
399 tos[toindex].count);
400 rawarc.raw_frompc = frompc;
401 rawarc.raw_selfpc = (u_long)tos[toindex].selfpc;
402 rawarc.raw_count = tos[toindex].count;
403 fwrite((char *)&rawarc, sizeof(rawarc), 1, fp);
406 fclose(fp);
410 * Reset the kernel profiling date structures.
412 void
413 reset(struct kvmvars *kvp)
415 char *zbuf;
416 u_long biggest;
417 int mib[3];
419 setprof(kvp, GMON_PROF_OFF);
421 biggest = kvp->gpm.kcountsize;
422 if (kvp->gpm.fromssize > biggest)
423 biggest = kvp->gpm.fromssize;
424 if (kvp->gpm.tossize > biggest)
425 biggest = kvp->gpm.tossize;
426 if ((zbuf = (char *)malloc(biggest)) == NULL)
427 errx(12, "cannot allocate zbuf space");
428 bzero(zbuf, biggest);
429 if (kflag) {
430 if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf,
431 kvp->gpm.kcountsize) != kvp->gpm.kcountsize)
432 errx(13, "tickbuf zero: %s", kvm_geterr(kvp->kd));
433 if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf,
434 kvp->gpm.fromssize) != kvp->gpm.fromssize)
435 errx(14, "froms zero: %s", kvm_geterr(kvp->kd));
436 if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf,
437 kvp->gpm.tossize) != kvp->gpm.tossize)
438 errx(15, "tos zero: %s", kvm_geterr(kvp->kd));
439 return;
441 seteuid(0);
442 mib[0] = CTL_KERN;
443 mib[1] = KERN_PROF;
444 mib[2] = GPROF_COUNT;
445 if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.kcountsize) < 0)
446 err(13, "tickbuf zero");
447 mib[2] = GPROF_FROMS;
448 if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.fromssize) < 0)
449 err(14, "froms zero");
450 mib[2] = GPROF_TOS;
451 if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.tossize) < 0)
452 err(15, "tos zero");
453 seteuid(getuid());
454 free(zbuf);