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
33 * @(#) Copyright (c) 1983, 1992, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)kgmon.c 8.1 (Berkeley) 6/6/93
35 * $FreeBSD: src/usr.sbin/kgmon/kgmon.c,v 1.9 1999/08/28 01:16:42 peter Exp $
36 * $DragonFly: src/usr.sbin/kgmon/kgmon.c,v 1.3 2003/11/03 19:31:38 eirikn Exp $
39 #include <sys/param.h>
42 #include <sys/sysctl.h>
69 int Bflag, bflag, hflag, kflag, rflag, pflag;
71 int getprof(struct kvmvars *);
72 int getprofhz(struct kvmvars *);
73 void kern_readonly(int);
74 int openfiles(char *, char *, struct kvmvars *);
75 void setprof(struct kvmvars *kvp, int state);
76 void dumpstate(struct kvmvars *kvp);
77 void reset(struct kvmvars *kvp);
78 static void usage(void);
81 main(int argc, char **argv)
83 int ch, mode, disp, accessmode;
84 struct kvmvars kvmvars;
90 while ((ch = getopt(argc, argv, "M:N:Bbhpr")) != -1) {
129 #define BACKWARD_COMPATIBILITY
130 #ifdef BACKWARD_COMPATIBILITY
140 system = (char *)getbootfile();
141 accessmode = openfiles(system, kmemf, &kvmvars);
142 mode = getprof(&kvmvars);
144 disp = GMON_PROF_OFF;
146 disp = GMON_PROF_HIRES;
155 if (accessmode == O_RDWR)
156 setprof(&kvmvars, disp);
157 (void)fprintf(stdout, "kgmon: kernel profiling is %s.\n",
158 disp == GMON_PROF_OFF ? "off" :
159 disp == GMON_PROF_HIRES ? "running (high resolution)" :
160 disp == GMON_PROF_ON ? "running" :
161 disp == GMON_PROF_BUSY ? "busy" :
162 disp == GMON_PROF_ERROR ? "off (error)" :
163 "in an unknown state");
170 fprintf(stderr, "usage: kgmon [-Bbhrp] [-M core] [-N system]\n");
175 * Check that profiling is enabled and open any ncessary files.
178 openfiles(system, kmemf, kvp)
183 int mib[3], state, size, openmode;
184 char errbuf[_POSIX2_LINE_MAX];
189 mib[2] = GPROF_STATE;
191 if (sysctl(mib, 3, &state, &size, NULL, 0) < 0)
192 errx(20, "profiling not defined in kernel");
193 if (!(Bflag || bflag || hflag || rflag ||
195 (state == GMON_PROF_HIRES || state == GMON_PROF_ON))))
198 if (sysctl(mib, 3, NULL, NULL, &state, size) >= 0)
200 (void)seteuid(getuid());
201 kern_readonly(state);
204 openmode = (Bflag || bflag || hflag || pflag || rflag)
206 kvp->kd = kvm_openfiles(system, kmemf, NULL, openmode, errbuf);
207 if (kvp->kd == NULL) {
208 if (openmode == O_RDWR) {
210 kvp->kd = kvm_openfiles(system, kmemf, NULL, O_RDONLY,
214 errx(2, "kvm_openfiles: %s", errbuf);
215 kern_readonly(GMON_PROF_ON);
217 if (kvm_nlist(kvp->kd, nl) < 0)
218 errx(3, "%s: no namelist", system);
219 if (!nl[N_GMONPARAM].n_value)
220 errx(20, "profiling not defined in kernel");
225 * Suppress options that require a writable kernel.
232 (void)fprintf(stderr, "kgmon: kernel read-only: ");
233 if (pflag && (mode == GMON_PROF_HIRES || mode == GMON_PROF_ON))
234 (void)fprintf(stderr, "data may be inconsistent\n");
236 (void)fprintf(stderr, "-r supressed\n");
238 (void)fprintf(stderr, "-B supressed\n");
240 (void)fprintf(stderr, "-b supressed\n");
242 (void)fprintf(stderr, "-h supressed\n");
243 rflag = Bflag = bflag = hflag = 0;
247 * Get the state of kernel profiling.
256 size = kvm_read(kvp->kd, nl[N_GMONPARAM].n_value, &kvp->gpm,
261 mib[2] = GPROF_GMONPARAM;
262 size = sizeof kvp->gpm;
263 if (sysctl(mib, 3, &kvp->gpm, &size, NULL, 0) < 0)
266 if (size != sizeof kvp->gpm)
267 errx(4, "cannot get gmonparam: %s",
268 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
269 return (kvp->gpm.state);
273 * Enable or disable kernel profiling according to the state variable.
280 struct gmonparam *p = (struct gmonparam *)nl[N_GMONPARAM].n_value;
281 int mib[3], sz, oldstate;
287 mib[2] = GPROF_STATE;
288 if (sysctl(mib, 3, &oldstate, &sz, NULL, 0) < 0)
290 if (oldstate == state)
293 if (sysctl(mib, 3, NULL, NULL, &state, sz) >= 0) {
294 (void)seteuid(getuid());
297 (void)seteuid(getuid());
298 } else if (kvm_write(kvp->kd, (u_long)&p->state, (void *)&state, sz)
302 warnx("warning: cannot turn profiling %s",
303 state == GMON_PROF_OFF ? "off" : "on");
307 * Build the gmon.out file.
314 struct rawarc rawarc;
315 struct tostruct *tos;
317 u_short *froms, *tickbuf;
320 int fromindex, endfrom, toindex;
322 setprof(kvp, GMON_PROF_OFF);
323 fp = fopen("gmon.out", "w");
330 * Build the gmon header and write it to a file.
332 bzero(&h, sizeof(h));
333 h.lpc = kvp->gpm.lowpc;
334 h.hpc = kvp->gpm.highpc;
335 h.ncnt = kvp->gpm.kcountsize + sizeof(h);
336 h.version = GMONVERSION;
337 h.profrate = kvp->gpm.profrate;
339 h.profrate = getprofhz(kvp); /* ancient kernel */
340 fwrite((char *)&h, sizeof(h), 1, fp);
343 * Write out the tick buffer.
347 if ((tickbuf = (u_short *)malloc(kvp->gpm.kcountsize)) == NULL)
348 errx(5, "cannot allocate kcount space");
350 i = kvm_read(kvp->kd, (u_long)kvp->gpm.kcount, (void *)tickbuf,
351 kvp->gpm.kcountsize);
353 mib[2] = GPROF_COUNT;
354 i = kvp->gpm.kcountsize;
355 if (sysctl(mib, 3, tickbuf, &i, NULL, 0) < 0)
358 if (i != kvp->gpm.kcountsize)
359 errx(6, "read ticks: read %u, got %d: %s",
360 kvp->gpm.kcountsize, i,
361 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
362 if ((fwrite(tickbuf, kvp->gpm.kcountsize, 1, fp)) != 1)
363 err(7, "writing tocks to gmon.out");
367 * Write out the arc info.
369 if ((froms = (u_short *)malloc(kvp->gpm.fromssize)) == NULL)
370 errx(8, "cannot allocate froms space");
372 i = kvm_read(kvp->kd, (u_long)kvp->gpm.froms, (void *)froms,
375 mib[2] = GPROF_FROMS;
376 i = kvp->gpm.fromssize;
377 if (sysctl(mib, 3, froms, &i, NULL, 0) < 0)
380 if (i != kvp->gpm.fromssize)
381 errx(9, "read froms: read %u, got %d: %s",
382 kvp->gpm.fromssize, i,
383 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
384 if ((tos = (struct tostruct *)malloc(kvp->gpm.tossize)) == NULL)
385 errx(10, "cannot allocate tos space");
387 i = kvm_read(kvp->kd, (u_long)kvp->gpm.tos, (void *)tos,
391 i = kvp->gpm.tossize;
392 if (sysctl(mib, 3, tos, &i, NULL, 0) < 0)
395 if (i != kvp->gpm.tossize)
396 errx(11, "read tos: read %u, got %d: %s",
398 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
400 warnx("lowpc 0x%x, textsize 0x%x",
401 kvp->gpm.lowpc, kvp->gpm.textsize);
402 endfrom = kvp->gpm.fromssize / sizeof(*froms);
403 for (fromindex = 0; fromindex < endfrom; ++fromindex) {
404 if (froms[fromindex] == 0)
406 frompc = (u_long)kvp->gpm.lowpc +
407 (fromindex * kvp->gpm.hashfraction * sizeof(*froms));
408 for (toindex = froms[fromindex]; toindex != 0;
409 toindex = tos[toindex].link) {
411 warnx("[mcleanup] frompc 0x%x selfpc 0x%x count %d",
412 frompc, tos[toindex].selfpc,
414 rawarc.raw_frompc = frompc;
415 rawarc.raw_selfpc = (u_long)tos[toindex].selfpc;
416 rawarc.raw_count = tos[toindex].count;
417 fwrite((char *)&rawarc, sizeof(rawarc), 1, fp);
424 * Get the profiling rate.
430 int mib[2], size, profrate;
431 struct clockinfo clockrate;
435 if (kvm_read(kvp->kd, nl[N_PROFHZ].n_value, &profrate,
436 sizeof profrate) != sizeof profrate)
437 warnx("get clockrate: %s", kvm_geterr(kvp->kd));
441 mib[1] = KERN_CLOCKRATE;
442 clockrate.profhz = 1;
443 size = sizeof clockrate;
444 if (sysctl(mib, 2, &clockrate, &size, NULL, 0) < 0)
445 warn("get clockrate");
446 return (clockrate.profhz);
450 * Reset the kernel profiling date structures.
460 setprof(kvp, GMON_PROF_OFF);
462 biggest = kvp->gpm.kcountsize;
463 if (kvp->gpm.fromssize > biggest)
464 biggest = kvp->gpm.fromssize;
465 if (kvp->gpm.tossize > biggest)
466 biggest = kvp->gpm.tossize;
467 if ((zbuf = (char *)malloc(biggest)) == NULL)
468 errx(12, "cannot allocate zbuf space");
469 bzero(zbuf, biggest);
471 if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf,
472 kvp->gpm.kcountsize) != kvp->gpm.kcountsize)
473 errx(13, "tickbuf zero: %s", kvm_geterr(kvp->kd));
474 if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf,
475 kvp->gpm.fromssize) != kvp->gpm.fromssize)
476 errx(14, "froms zero: %s", kvm_geterr(kvp->kd));
477 if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf,
478 kvp->gpm.tossize) != kvp->gpm.tossize)
479 errx(15, "tos zero: %s", kvm_geterr(kvp->kd));
485 mib[2] = GPROF_COUNT;
486 if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.kcountsize) < 0)
487 err(13, "tickbuf zero");
488 mib[2] = GPROF_FROMS;
489 if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.fromssize) < 0)
490 err(14, "froms zero");
492 if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.tossize) < 0)
494 (void)seteuid(getuid());