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>
79 int Bflag, bflag, hflag, kflag, rflag, pflag;
81 int getprof __P((struct kvmvars *));
82 int getprofhz __P((struct kvmvars *));
83 void kern_readonly __P((int));
84 int openfiles __P((char *, char *, struct kvmvars *));
85 void setprof __P((struct kvmvars *kvp, int state));
86 void dumpstate __P((struct kvmvars *kvp));
87 void reset __P((struct kvmvars *kvp));
88 static void usage __P((void));
91 main(int argc, char **argv)
93 int ch, mode, disp, accessmode;
94 struct kvmvars kvmvars;
100 while ((ch = getopt(argc, argv, "M:N:Bbhpr")) != -1) {
139 #define BACKWARD_COMPATIBILITY
140 #ifdef BACKWARD_COMPATIBILITY
150 system = (char *)getbootfile();
151 accessmode = openfiles(system, kmemf, &kvmvars);
152 mode = getprof(&kvmvars);
154 disp = GMON_PROF_OFF;
156 disp = GMON_PROF_HIRES;
165 if (accessmode == O_RDWR)
166 setprof(&kvmvars, disp);
167 (void)fprintf(stdout, "kgmon: kernel profiling is %s.\n",
168 disp == GMON_PROF_OFF ? "off" :
169 disp == GMON_PROF_HIRES ? "running (high resolution)" :
170 disp == GMON_PROF_ON ? "running" :
171 disp == GMON_PROF_BUSY ? "busy" :
172 disp == GMON_PROF_ERROR ? "off (error)" :
173 "in an unknown state");
180 fprintf(stderr, "usage: kgmon [-Bbhrp] [-M core] [-N system]\n");
185 * Check that profiling is enabled and open any ncessary files.
188 openfiles(system, kmemf, kvp)
193 int mib[3], state, size, openmode;
194 char errbuf[_POSIX2_LINE_MAX];
199 mib[2] = GPROF_STATE;
201 if (sysctl(mib, 3, &state, &size, NULL, 0) < 0)
202 errx(20, "profiling not defined in kernel");
203 if (!(Bflag || bflag || hflag || rflag ||
205 (state == GMON_PROF_HIRES || state == GMON_PROF_ON))))
208 if (sysctl(mib, 3, NULL, NULL, &state, size) >= 0)
210 (void)seteuid(getuid());
211 kern_readonly(state);
214 openmode = (Bflag || bflag || hflag || pflag || rflag)
216 kvp->kd = kvm_openfiles(system, kmemf, NULL, openmode, errbuf);
217 if (kvp->kd == NULL) {
218 if (openmode == O_RDWR) {
220 kvp->kd = kvm_openfiles(system, kmemf, NULL, O_RDONLY,
224 errx(2, "kvm_openfiles: %s", errbuf);
225 kern_readonly(GMON_PROF_ON);
227 if (kvm_nlist(kvp->kd, nl) < 0)
228 errx(3, "%s: no namelist", system);
229 if (!nl[N_GMONPARAM].n_value)
230 errx(20, "profiling not defined in kernel");
235 * Suppress options that require a writable kernel.
242 (void)fprintf(stderr, "kgmon: kernel read-only: ");
243 if (pflag && (mode == GMON_PROF_HIRES || mode == GMON_PROF_ON))
244 (void)fprintf(stderr, "data may be inconsistent\n");
246 (void)fprintf(stderr, "-r supressed\n");
248 (void)fprintf(stderr, "-B supressed\n");
250 (void)fprintf(stderr, "-b supressed\n");
252 (void)fprintf(stderr, "-h supressed\n");
253 rflag = Bflag = bflag = hflag = 0;
257 * Get the state of kernel profiling.
266 size = kvm_read(kvp->kd, nl[N_GMONPARAM].n_value, &kvp->gpm,
271 mib[2] = GPROF_GMONPARAM;
272 size = sizeof kvp->gpm;
273 if (sysctl(mib, 3, &kvp->gpm, &size, NULL, 0) < 0)
278 * Accept certain undersized "structs" from old kernels. We need
279 * everything up to hashfraction, and want profrate and
280 * histcounter_type. Assume that the kernel doesn't put garbage
281 * in any padding that is returned instead of profrate and
282 * histcounter_type. This is a bad assumption for dead kernels,
283 * since kvm_read() will normally return garbage for bytes beyond
284 * the end of the actual kernel struct, if any.
286 if (size < offsetof(struct gmonparam, hashfraction) +
287 sizeof(kvp->gpm.hashfraction) || size > sizeof(kvp->gpm))
288 errx(4, "cannot get gmonparam: %s",
289 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
290 bzero((char *)&kvp->gpm + size, sizeof(kvp->gpm) - size);
291 if (kvp->gpm.profrate == 0)
292 kvp->gpm.profrate = getprofhz(kvp);
294 if (kvp->gpm.histcounter_type == 0) {
296 * This fixup only works for not-so-old i386 kernels. The
297 * magic 16 is the kernel FUNCTION_ALIGNMENT. 64-bit
298 * counters are signed; smaller counters are unsigned.
300 kvp->gpm.histcounter_type = 16 /
301 (kvp->gpm.textsize / kvp->gpm.kcountsize) * CHAR_BIT;
302 if (kvp->gpm.histcounter_type == 64)
303 kvp->gpm.histcounter_type = -64;
307 return (kvp->gpm.state);
311 * Enable or disable kernel profiling according to the state variable.
318 struct gmonparam *p = (struct gmonparam *)nl[N_GMONPARAM].n_value;
319 int mib[3], sz, oldstate;
325 mib[2] = GPROF_STATE;
326 if (sysctl(mib, 3, &oldstate, &sz, NULL, 0) < 0)
328 if (oldstate == state)
331 if (sysctl(mib, 3, NULL, NULL, &state, sz) >= 0) {
332 (void)seteuid(getuid());
335 (void)seteuid(getuid());
336 } else if (kvm_write(kvp->kd, (u_long)&p->state, (void *)&state, sz)
340 warnx("warning: cannot turn profiling %s",
341 state == GMON_PROF_OFF ? "off" : "on");
345 * Build the gmon.out file.
352 struct rawarc rawarc;
353 struct tostruct *tos;
355 u_short *froms, *tickbuf;
358 int fromindex, endfrom, toindex;
360 setprof(kvp, GMON_PROF_OFF);
361 fp = fopen("gmon.out", "w");
368 * Build the gmon header and write it to a file.
370 bzero(&h, sizeof(h));
371 h.lpc = kvp->gpm.lowpc;
372 h.hpc = kvp->gpm.highpc;
373 h.ncnt = kvp->gpm.kcountsize + sizeof(h);
374 h.version = GMONVERSION;
375 h.profrate = kvp->gpm.profrate;
376 h.histcounter_type = kvp->gpm.histcounter_type;
377 fwrite((char *)&h, sizeof(h), 1, fp);
380 * Write out the tick buffer.
384 if ((tickbuf = (u_short *)malloc(kvp->gpm.kcountsize)) == NULL)
385 errx(5, "cannot allocate kcount space");
387 i = kvm_read(kvp->kd, (u_long)kvp->gpm.kcount, (void *)tickbuf,
388 kvp->gpm.kcountsize);
390 mib[2] = GPROF_COUNT;
391 i = kvp->gpm.kcountsize;
392 if (sysctl(mib, 3, tickbuf, &i, NULL, 0) < 0)
395 if (i != kvp->gpm.kcountsize)
396 errx(6, "read ticks: read %u, got %d: %s",
397 kvp->gpm.kcountsize, i,
398 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
399 if ((fwrite(tickbuf, kvp->gpm.kcountsize, 1, fp)) != 1)
400 err(7, "writing tocks to gmon.out");
404 * Write out the arc info.
406 if ((froms = (u_short *)malloc(kvp->gpm.fromssize)) == NULL)
407 errx(8, "cannot allocate froms space");
409 i = kvm_read(kvp->kd, (u_long)kvp->gpm.froms, (void *)froms,
412 mib[2] = GPROF_FROMS;
413 i = kvp->gpm.fromssize;
414 if (sysctl(mib, 3, froms, &i, NULL, 0) < 0)
417 if (i != kvp->gpm.fromssize)
418 errx(9, "read froms: read %u, got %d: %s",
419 kvp->gpm.fromssize, i,
420 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
421 if ((tos = (struct tostruct *)malloc(kvp->gpm.tossize)) == NULL)
422 errx(10, "cannot allocate tos space");
424 i = kvm_read(kvp->kd, (u_long)kvp->gpm.tos, (void *)tos,
428 i = kvp->gpm.tossize;
429 if (sysctl(mib, 3, tos, &i, NULL, 0) < 0)
432 if (i != kvp->gpm.tossize)
433 errx(11, "read tos: read %u, got %d: %s",
435 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
437 warnx("lowpc 0x%x, textsize 0x%x",
438 kvp->gpm.lowpc, kvp->gpm.textsize);
439 endfrom = kvp->gpm.fromssize / sizeof(*froms);
440 for (fromindex = 0; fromindex < endfrom; ++fromindex) {
441 if (froms[fromindex] == 0)
443 frompc = (u_long)kvp->gpm.lowpc +
444 (fromindex * kvp->gpm.hashfraction * sizeof(*froms));
445 for (toindex = froms[fromindex]; toindex != 0;
446 toindex = tos[toindex].link) {
448 warnx("[mcleanup] frompc 0x%x selfpc 0x%x count %d",
449 frompc, tos[toindex].selfpc,
451 rawarc.raw_frompc = frompc;
452 rawarc.raw_selfpc = (u_long)tos[toindex].selfpc;
453 rawarc.raw_count = tos[toindex].count;
454 fwrite((char *)&rawarc, sizeof(rawarc), 1, fp);
461 * Get the profiling rate.
467 int mib[2], size, profrate;
468 struct clockinfo clockrate;
472 if (kvm_read(kvp->kd, nl[N_PROFHZ].n_value, &profrate,
473 sizeof profrate) != sizeof profrate)
474 warnx("get clockrate: %s", kvm_geterr(kvp->kd));
478 mib[1] = KERN_CLOCKRATE;
479 clockrate.profhz = 1;
480 size = sizeof clockrate;
481 if (sysctl(mib, 2, &clockrate, &size, NULL, 0) < 0)
482 warn("get clockrate");
483 return (clockrate.profhz);
487 * Reset the kernel profiling date structures.
497 setprof(kvp, GMON_PROF_OFF);
499 biggest = kvp->gpm.kcountsize;
500 if (kvp->gpm.fromssize > biggest)
501 biggest = kvp->gpm.fromssize;
502 if (kvp->gpm.tossize > biggest)
503 biggest = kvp->gpm.tossize;
504 if ((zbuf = (char *)malloc(biggest)) == NULL)
505 errx(12, "cannot allocate zbuf space");
506 bzero(zbuf, biggest);
508 if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf,
509 kvp->gpm.kcountsize) != kvp->gpm.kcountsize)
510 errx(13, "tickbuf zero: %s", kvm_geterr(kvp->kd));
511 if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf,
512 kvp->gpm.fromssize) != kvp->gpm.fromssize)
513 errx(14, "froms zero: %s", kvm_geterr(kvp->kd));
514 if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf,
515 kvp->gpm.tossize) != kvp->gpm.tossize)
516 errx(15, "tos zero: %s", kvm_geterr(kvp->kd));
522 mib[2] = GPROF_COUNT;
523 if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.kcountsize) < 0)
524 err(13, "tickbuf zero");
525 mib[2] = GPROF_FROMS;
526 if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.fromssize) < 0)
527 err(14, "froms zero");
529 if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.tossize) < 0)
531 (void)seteuid(getuid());