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 * 4. 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
31 static const char copyright[] =
32 "@(#) Copyright (c) 1983, 1992, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)kgmon.c 8.1 (Berkeley) 6/6/93";
40 static const char rcsid[] =
44 #include <sys/param.h>
47 #include <sys/sysctl.h>
75 int Bflag, bflag, hflag, kflag, rflag, pflag;
77 int getprof(struct kvmvars *);
78 int getprofhz(struct kvmvars *);
79 void kern_readonly(int);
80 int openfiles(char *, char *, struct kvmvars *);
81 void setprof(struct kvmvars *kvp, int state);
82 void dumpstate(struct kvmvars *kvp);
83 void reset(struct kvmvars *kvp);
84 static void usage(void);
87 main(int argc, char **argv)
89 int ch, mode, disp, accessmode;
90 struct kvmvars kvmvars;
96 while ((ch = getopt(argc, argv, "M:N:Bbhpr")) != -1) {
135 #define BACKWARD_COMPATIBILITY
136 #ifdef BACKWARD_COMPATIBILITY
146 system = (char *)getbootfile();
147 accessmode = openfiles(system, kmemf, &kvmvars);
148 mode = getprof(&kvmvars);
150 disp = GMON_PROF_OFF;
152 disp = GMON_PROF_HIRES;
161 if (accessmode == O_RDWR)
162 setprof(&kvmvars, disp);
163 (void)fprintf(stdout, "kgmon: kernel profiling is %s.\n",
164 disp == GMON_PROF_OFF ? "off" :
165 disp == GMON_PROF_HIRES ? "running (high resolution)" :
166 disp == GMON_PROF_ON ? "running" :
167 disp == GMON_PROF_BUSY ? "busy" :
168 disp == GMON_PROF_ERROR ? "off (error)" :
169 "in an unknown state");
176 fprintf(stderr, "usage: kgmon [-Bbhrp] [-M core] [-N system]\n");
181 * Check that profiling is enabled and open any necessary files.
184 openfiles(system, kmemf, kvp)
190 int mib[3], state, openmode;
191 char errbuf[_POSIX2_LINE_MAX];
196 mib[2] = GPROF_STATE;
198 if (sysctl(mib, 3, &state, &size, NULL, 0) < 0)
199 errx(20, "profiling not defined in kernel");
200 if (!(Bflag || bflag || hflag || rflag ||
202 (state == GMON_PROF_HIRES || state == GMON_PROF_ON))))
205 if (sysctl(mib, 3, NULL, NULL, &state, size) >= 0)
207 (void)seteuid(getuid());
208 kern_readonly(state);
211 openmode = (Bflag || bflag || hflag || pflag || rflag)
213 kvp->kd = kvm_openfiles(system, kmemf, NULL, openmode, errbuf);
214 if (kvp->kd == NULL) {
215 if (openmode == O_RDWR) {
217 kvp->kd = kvm_openfiles(system, kmemf, NULL, O_RDONLY,
221 errx(2, "kvm_openfiles: %s", errbuf);
222 kern_readonly(GMON_PROF_ON);
224 if (kvm_nlist(kvp->kd, nl) < 0)
225 errx(3, "%s: no namelist", system);
226 if (!nl[N_GMONPARAM].n_value)
227 errx(20, "profiling not defined in kernel");
232 * Suppress options that require a writable kernel.
239 (void)fprintf(stderr, "kgmon: kernel read-only: ");
240 if (pflag && (mode == GMON_PROF_HIRES || mode == GMON_PROF_ON))
241 (void)fprintf(stderr, "data may be inconsistent\n");
243 (void)fprintf(stderr, "-r supressed\n");
245 (void)fprintf(stderr, "-B supressed\n");
247 (void)fprintf(stderr, "-b supressed\n");
249 (void)fprintf(stderr, "-h supressed\n");
250 rflag = Bflag = bflag = hflag = 0;
254 * Get the state of kernel profiling.
264 size = kvm_read(kvp->kd, nl[N_GMONPARAM].n_value, &kvp->gpm,
269 mib[2] = GPROF_GMONPARAM;
270 size = sizeof kvp->gpm;
271 if (sysctl(mib, 3, &kvp->gpm, &size, NULL, 0) < 0)
276 * Accept certain undersized "structs" from old kernels. We need
277 * everything up to hashfraction, and want profrate and
278 * histcounter_type. Assume that the kernel doesn't put garbage
279 * in any padding that is returned instead of profrate and
280 * histcounter_type. This is a bad assumption for dead kernels,
281 * since kvm_read() will normally return garbage for bytes beyond
282 * the end of the actual kernel struct, if any.
284 if (size < offsetof(struct gmonparam, hashfraction) +
285 sizeof(kvp->gpm.hashfraction) || size > sizeof(kvp->gpm))
286 errx(4, "cannot get gmonparam: %s",
287 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
288 bzero((char *)&kvp->gpm + size, sizeof(kvp->gpm) - size);
289 if (kvp->gpm.profrate == 0)
290 kvp->gpm.profrate = getprofhz(kvp);
292 if (kvp->gpm.histcounter_type == 0) {
294 * This fixup only works for not-so-old i386 kernels. The
295 * magic 16 is the kernel FUNCTION_ALIGNMENT. 64-bit
296 * counters are signed; smaller counters are unsigned.
298 kvp->gpm.histcounter_type = 16 /
299 (kvp->gpm.textsize / kvp->gpm.kcountsize) * CHAR_BIT;
300 if (kvp->gpm.histcounter_type == 64)
301 kvp->gpm.histcounter_type = -64;
305 return (kvp->gpm.state);
309 * Enable or disable kernel profiling according to the state variable.
316 struct gmonparam *p = (struct gmonparam *)nl[N_GMONPARAM].n_value;
318 int mib[3], oldstate;
324 mib[2] = GPROF_STATE;
325 if (sysctl(mib, 3, &oldstate, &sz, NULL, 0) < 0)
327 if (oldstate == state)
330 if (sysctl(mib, 3, NULL, NULL, &state, sz) >= 0) {
331 (void)seteuid(getuid());
334 (void)seteuid(getuid());
335 } else if (kvm_write(kvp->kd, (u_long)&p->state, (void *)&state, sz)
339 warnx("warning: cannot turn profiling %s",
340 state == GMON_PROF_OFF ? "off" : "on");
344 * Build the gmon.out file.
351 struct rawarc rawarc;
352 struct tostruct *tos;
354 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 %lu, got %ld: %s",
397 kvp->gpm.kcountsize, (long)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 %lu, got %ld: %s",
419 kvp->gpm.fromssize, (long)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 %lu, got %ld: %s",
434 kvp->gpm.tossize, (long)i,
435 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
437 warnx("lowpc 0x%lx, textsize 0x%lx",
438 (unsigned long)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%lx selfpc 0x%lx "
449 "count %ld", 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.
468 int mib[2], profrate;
469 struct clockinfo clockrate;
473 if (kvm_read(kvp->kd, nl[N_PROFHZ].n_value, &profrate,
474 sizeof profrate) != sizeof profrate)
475 warnx("get clockrate: %s", kvm_geterr(kvp->kd));
479 mib[1] = KERN_CLOCKRATE;
480 clockrate.profhz = 1;
481 size = sizeof clockrate;
482 if (sysctl(mib, 2, &clockrate, &size, NULL, 0) < 0)
483 warn("get clockrate");
484 return (clockrate.profhz);
488 * Reset the kernel profiling date structures.
498 setprof(kvp, GMON_PROF_OFF);
500 biggest = kvp->gpm.kcountsize;
501 if (kvp->gpm.fromssize > biggest)
502 biggest = kvp->gpm.fromssize;
503 if (kvp->gpm.tossize > biggest)
504 biggest = kvp->gpm.tossize;
505 if ((zbuf = (char *)malloc(biggest)) == NULL)
506 errx(12, "cannot allocate zbuf space");
507 bzero(zbuf, biggest);
509 if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf,
510 kvp->gpm.kcountsize) != kvp->gpm.kcountsize)
511 errx(13, "tickbuf zero: %s", kvm_geterr(kvp->kd));
512 if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf,
513 kvp->gpm.fromssize) != kvp->gpm.fromssize)
514 errx(14, "froms zero: %s", kvm_geterr(kvp->kd));
515 if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf,
516 kvp->gpm.tossize) != kvp->gpm.tossize)
517 errx(15, "tos zero: %s", kvm_geterr(kvp->kd));
523 mib[2] = GPROF_COUNT;
524 if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.kcountsize) < 0)
525 err(13, "tickbuf zero");
526 mib[2] = GPROF_FROMS;
527 if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.fromssize) < 0)
528 err(14, "froms zero");
530 if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.tossize) < 0)
532 (void)seteuid(getuid());