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
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.
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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1992, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)kgmon.c 8.1 (Berkeley) 6/6/93";
44 static const char rcsid[] =
48 #include <sys/param.h>
51 #include <sys/sysctl.h>
78 int Bflag, bflag, hflag, kflag, rflag, pflag;
80 int getprof __P((struct kvmvars *));
81 int getprofhz __P((struct kvmvars *));
82 void kern_readonly __P((int));
83 int openfiles __P((char *, char *, struct kvmvars *));
84 void setprof __P((struct kvmvars *kvp, int state));
85 void dumpstate __P((struct kvmvars *kvp));
86 void reset __P((struct kvmvars *kvp));
87 static void usage __P((void));
90 main(int argc, char **argv)
92 int ch, mode, disp, accessmode;
93 struct kvmvars kvmvars;
99 while ((ch = getopt(argc, argv, "M:N:Bbhpr")) != -1) {
138 #define BACKWARD_COMPATIBILITY
139 #ifdef BACKWARD_COMPATIBILITY
149 system = (char *)getbootfile();
150 accessmode = openfiles(system, kmemf, &kvmvars);
151 mode = getprof(&kvmvars);
153 disp = GMON_PROF_OFF;
155 disp = GMON_PROF_HIRES;
164 if (accessmode == O_RDWR)
165 setprof(&kvmvars, disp);
166 (void)fprintf(stdout, "kgmon: kernel profiling is %s.\n",
167 disp == GMON_PROF_OFF ? "off" :
168 disp == GMON_PROF_HIRES ? "running (high resolution)" :
169 disp == GMON_PROF_ON ? "running" :
170 disp == GMON_PROF_BUSY ? "busy" :
171 disp == GMON_PROF_ERROR ? "off (error)" :
172 "in an unknown state");
179 fprintf(stderr, "usage: kgmon [-Bbhrp] [-M core] [-N system]\n");
184 * Check that profiling is enabled and open any ncessary files.
187 openfiles(system, kmemf, kvp)
192 int mib[3], state, size, openmode;
193 char errbuf[_POSIX2_LINE_MAX];
198 mib[2] = GPROF_STATE;
200 if (sysctl(mib, 3, &state, &size, NULL, 0) < 0)
201 errx(20, "profiling not defined in kernel");
202 if (!(Bflag || bflag || hflag || rflag ||
204 (state == GMON_PROF_HIRES || state == GMON_PROF_ON))))
207 if (sysctl(mib, 3, NULL, NULL, &state, size) >= 0)
209 (void)seteuid(getuid());
210 kern_readonly(state);
213 openmode = (Bflag || bflag || hflag || pflag || rflag)
215 kvp->kd = kvm_openfiles(system, kmemf, NULL, openmode, errbuf);
216 if (kvp->kd == NULL) {
217 if (openmode == O_RDWR) {
219 kvp->kd = kvm_openfiles(system, kmemf, NULL, O_RDONLY,
223 errx(2, "kvm_openfiles: %s", errbuf);
224 kern_readonly(GMON_PROF_ON);
226 if (kvm_nlist(kvp->kd, nl) < 0)
227 errx(3, "%s: no namelist", system);
228 if (!nl[N_GMONPARAM].n_value)
229 errx(20, "profiling not defined in kernel");
234 * Suppress options that require a writable kernel.
241 (void)fprintf(stderr, "kgmon: kernel read-only: ");
242 if (pflag && (mode == GMON_PROF_HIRES || mode == GMON_PROF_ON))
243 (void)fprintf(stderr, "data may be inconsistent\n");
245 (void)fprintf(stderr, "-r supressed\n");
247 (void)fprintf(stderr, "-B supressed\n");
249 (void)fprintf(stderr, "-b supressed\n");
251 (void)fprintf(stderr, "-h supressed\n");
252 rflag = Bflag = bflag = hflag = 0;
256 * Get the state of kernel profiling.
265 size = kvm_read(kvp->kd, nl[N_GMONPARAM].n_value, &kvp->gpm,
270 mib[2] = GPROF_GMONPARAM;
271 size = sizeof kvp->gpm;
272 if (sysctl(mib, 3, &kvp->gpm, &size, NULL, 0) < 0)
275 if (size != sizeof kvp->gpm)
276 errx(4, "cannot get gmonparam: %s",
277 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
278 return (kvp->gpm.state);
282 * Enable or disable kernel profiling according to the state variable.
289 struct gmonparam *p = (struct gmonparam *)nl[N_GMONPARAM].n_value;
290 int mib[3], sz, oldstate;
296 mib[2] = GPROF_STATE;
297 if (sysctl(mib, 3, &oldstate, &sz, NULL, 0) < 0)
299 if (oldstate == state)
302 if (sysctl(mib, 3, NULL, NULL, &state, sz) >= 0) {
303 (void)seteuid(getuid());
306 (void)seteuid(getuid());
307 } else if (kvm_write(kvp->kd, (u_long)&p->state, (void *)&state, sz)
311 warnx("warning: cannot turn profiling %s",
312 state == GMON_PROF_OFF ? "off" : "on");
316 * Build the gmon.out file.
323 struct rawarc rawarc;
324 struct tostruct *tos;
326 u_short *froms, *tickbuf;
329 int fromindex, endfrom, toindex;
331 setprof(kvp, GMON_PROF_OFF);
332 fp = fopen("gmon.out", "w");
339 * Build the gmon header and write it to a file.
341 bzero(&h, sizeof(h));
342 h.lpc = kvp->gpm.lowpc;
343 h.hpc = kvp->gpm.highpc;
344 h.ncnt = kvp->gpm.kcountsize + sizeof(h);
345 h.version = GMONVERSION;
346 h.profrate = kvp->gpm.profrate;
348 h.profrate = getprofhz(kvp); /* ancient kernel */
349 fwrite((char *)&h, sizeof(h), 1, fp);
352 * Write out the tick buffer.
356 if ((tickbuf = (u_short *)malloc(kvp->gpm.kcountsize)) == NULL)
357 errx(5, "cannot allocate kcount space");
359 i = kvm_read(kvp->kd, (u_long)kvp->gpm.kcount, (void *)tickbuf,
360 kvp->gpm.kcountsize);
362 mib[2] = GPROF_COUNT;
363 i = kvp->gpm.kcountsize;
364 if (sysctl(mib, 3, tickbuf, &i, NULL, 0) < 0)
367 if (i != kvp->gpm.kcountsize)
368 errx(6, "read ticks: read %u, got %d: %s",
369 kvp->gpm.kcountsize, i,
370 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
371 if ((fwrite(tickbuf, kvp->gpm.kcountsize, 1, fp)) != 1)
372 err(7, "writing tocks to gmon.out");
376 * Write out the arc info.
378 if ((froms = (u_short *)malloc(kvp->gpm.fromssize)) == NULL)
379 errx(8, "cannot allocate froms space");
381 i = kvm_read(kvp->kd, (u_long)kvp->gpm.froms, (void *)froms,
384 mib[2] = GPROF_FROMS;
385 i = kvp->gpm.fromssize;
386 if (sysctl(mib, 3, froms, &i, NULL, 0) < 0)
389 if (i != kvp->gpm.fromssize)
390 errx(9, "read froms: read %u, got %d: %s",
391 kvp->gpm.fromssize, i,
392 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
393 if ((tos = (struct tostruct *)malloc(kvp->gpm.tossize)) == NULL)
394 errx(10, "cannot allocate tos space");
396 i = kvm_read(kvp->kd, (u_long)kvp->gpm.tos, (void *)tos,
400 i = kvp->gpm.tossize;
401 if (sysctl(mib, 3, tos, &i, NULL, 0) < 0)
404 if (i != kvp->gpm.tossize)
405 errx(11, "read tos: read %u, got %d: %s",
407 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
409 warnx("lowpc 0x%x, textsize 0x%x",
410 kvp->gpm.lowpc, kvp->gpm.textsize);
411 endfrom = kvp->gpm.fromssize / sizeof(*froms);
412 for (fromindex = 0; fromindex < endfrom; ++fromindex) {
413 if (froms[fromindex] == 0)
415 frompc = (u_long)kvp->gpm.lowpc +
416 (fromindex * kvp->gpm.hashfraction * sizeof(*froms));
417 for (toindex = froms[fromindex]; toindex != 0;
418 toindex = tos[toindex].link) {
420 warnx("[mcleanup] frompc 0x%x selfpc 0x%x count %d",
421 frompc, tos[toindex].selfpc,
423 rawarc.raw_frompc = frompc;
424 rawarc.raw_selfpc = (u_long)tos[toindex].selfpc;
425 rawarc.raw_count = tos[toindex].count;
426 fwrite((char *)&rawarc, sizeof(rawarc), 1, fp);
433 * Get the profiling rate.
439 int mib[2], size, profrate;
440 struct clockinfo clockrate;
444 if (kvm_read(kvp->kd, nl[N_PROFHZ].n_value, &profrate,
445 sizeof profrate) != sizeof profrate)
446 warnx("get clockrate: %s", kvm_geterr(kvp->kd));
450 mib[1] = KERN_CLOCKRATE;
451 clockrate.profhz = 1;
452 size = sizeof clockrate;
453 if (sysctl(mib, 2, &clockrate, &size, NULL, 0) < 0)
454 warn("get clockrate");
455 return (clockrate.profhz);
459 * Reset the kernel profiling date structures.
469 setprof(kvp, GMON_PROF_OFF);
471 biggest = kvp->gpm.kcountsize;
472 if (kvp->gpm.fromssize > biggest)
473 biggest = kvp->gpm.fromssize;
474 if (kvp->gpm.tossize > biggest)
475 biggest = kvp->gpm.tossize;
476 if ((zbuf = (char *)malloc(biggest)) == NULL)
477 errx(12, "cannot allocate zbuf space");
478 bzero(zbuf, biggest);
480 if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf,
481 kvp->gpm.kcountsize) != kvp->gpm.kcountsize)
482 errx(13, "tickbuf zero: %s", kvm_geterr(kvp->kd));
483 if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf,
484 kvp->gpm.fromssize) != kvp->gpm.fromssize)
485 errx(14, "froms zero: %s", kvm_geterr(kvp->kd));
486 if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf,
487 kvp->gpm.tossize) != kvp->gpm.tossize)
488 errx(15, "tos zero: %s", kvm_geterr(kvp->kd));
494 mib[2] = GPROF_COUNT;
495 if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.kcountsize) < 0)
496 err(13, "tickbuf zero");
497 mib[2] = GPROF_FROMS;
498 if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.fromssize) < 0)
499 err(14, "froms zero");
501 if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.tossize) < 0)
503 (void)seteuid(getuid());