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 $
38 #include <sys/param.h>
41 #include <sys/sysctl.h>
66 int Bflag, bflag, hflag, kflag, rflag, pflag;
68 int getprof(struct kvmvars *);
69 void kern_readonly(int);
70 int openfiles(char *, char *, struct kvmvars *);
71 void setprof(struct kvmvars *kvp, int state);
72 void dumpstate(struct kvmvars *kvp);
73 void reset(struct kvmvars *kvp);
74 static void usage(void);
77 main(int argc, char **argv)
79 int ch, mode, disp, accessmode;
80 struct kvmvars kvmvars;
86 while ((ch = getopt(argc, argv, "M:N:Bbhpr")) != -1) {
125 #define BACKWARD_COMPATIBILITY
126 #ifdef BACKWARD_COMPATIBILITY
136 system = (char *)getbootfile();
137 accessmode = openfiles(system, kmemf, &kvmvars);
138 mode = getprof(&kvmvars);
140 disp = GMON_PROF_OFF;
142 disp = GMON_PROF_HIRES;
151 if (accessmode == O_RDWR)
152 setprof(&kvmvars, disp);
153 fprintf(stdout, "kgmon: kernel profiling is %s.\n",
154 disp == GMON_PROF_OFF ? "off" :
155 disp == GMON_PROF_HIRES ? "running (high resolution)" :
156 disp == GMON_PROF_ON ? "running" :
157 disp == GMON_PROF_BUSY ? "busy" :
158 disp == GMON_PROF_ERROR ? "off (error)" :
159 "in an unknown state");
166 fprintf(stderr, "usage: kgmon [-Bbhrp] [-M core] [-N system]\n");
171 * Check that profiling is enabled and open any ncessary files.
174 openfiles(char *system, char *kmemf, struct kvmvars *kvp)
176 int mib[3], state, openmode;
178 char errbuf[_POSIX2_LINE_MAX];
183 mib[2] = GPROF_STATE;
185 if (sysctl(mib, 3, &state, &size, NULL, 0) < 0)
186 errx(20, "profiling not defined in kernel");
187 if (!(Bflag || bflag || hflag || rflag ||
189 (state == GMON_PROF_HIRES || state == GMON_PROF_ON))))
192 if (sysctl(mib, 3, NULL, NULL, &state, size) >= 0)
195 kern_readonly(state);
198 openmode = (Bflag || bflag || hflag || pflag || rflag)
200 kvp->kd = kvm_openfiles(system, kmemf, NULL, openmode, errbuf);
201 if (kvp->kd == NULL) {
202 if (openmode == O_RDWR) {
204 kvp->kd = kvm_openfiles(system, kmemf, NULL, O_RDONLY,
208 errx(2, "kvm_openfiles: %s", errbuf);
209 kern_readonly(GMON_PROF_ON);
211 if (kvm_nlist(kvp->kd, nl) < 0)
212 errx(3, "%s: no namelist", system);
213 if (!nl[N_GMONPARAM].n_value)
214 errx(20, "profiling not defined in kernel");
219 * Suppress options that require a writable kernel.
222 kern_readonly(int mode)
224 fprintf(stderr, "kgmon: kernel read-only: ");
225 if (pflag && (mode == GMON_PROF_HIRES || mode == GMON_PROF_ON))
226 fprintf(stderr, "data may be inconsistent\n");
228 fprintf(stderr, "-r suppressed\n");
230 fprintf(stderr, "-B suppressed\n");
232 fprintf(stderr, "-b suppressed\n");
234 fprintf(stderr, "-h suppressed\n");
235 rflag = Bflag = bflag = hflag = 0;
239 * Get the state of kernel profiling.
242 getprof(struct kvmvars *kvp)
248 size = kvm_read(kvp->kd, nl[N_GMONPARAM].n_value, &kvp->gpm,
253 mib[2] = GPROF_GMONPARAM;
254 size = sizeof kvp->gpm;
255 if (sysctl(mib, 3, &kvp->gpm, &size, NULL, 0) < 0)
258 if (size != sizeof kvp->gpm)
259 errx(4, "cannot get gmonparam: %s",
260 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
261 return (kvp->gpm.state);
265 * Enable or disable kernel profiling according to the state variable.
268 setprof(struct kvmvars *kvp, int state)
270 struct gmonparam *p = (struct gmonparam *)nl[N_GMONPARAM].n_value;
271 int mib[3], oldstate;
274 size = sizeof(state);
278 mib[2] = GPROF_STATE;
279 if (sysctl(mib, 3, &oldstate, &size, NULL, 0) < 0)
281 if (oldstate == state)
284 if (sysctl(mib, 3, NULL, NULL, &state, size) >= 0) {
289 } else if (kvm_write(kvp->kd, (u_long)&p->state, (void *)&state, size)
294 warnx("warning: cannot turn profiling %s",
295 state == GMON_PROF_OFF ? "off" : "on");
299 * Build the gmon.out file.
302 dumpstate(struct kvmvars *kvp)
305 struct rawarc rawarc;
306 struct tostruct *tos;
308 u_short *froms, *tickbuf;
312 int fromindex, endfrom, toindex;
314 setprof(kvp, GMON_PROF_OFF);
315 fp = fopen("gmon.out", "w");
322 * Build the gmon header and write it to a file.
324 bzero(&h, sizeof(h));
325 h.lpc = kvp->gpm.lowpc;
326 h.hpc = kvp->gpm.highpc;
327 h.ncnt = kvp->gpm.kcountsize + sizeof(h);
328 h.version = GMONVERSION;
329 h.profrate = kvp->gpm.profrate;
330 fwrite((char *)&h, sizeof(h), 1, fp);
333 * Write out the tick buffer.
337 if ((tickbuf = (u_short *)malloc(kvp->gpm.kcountsize)) == NULL)
338 errx(5, "cannot allocate kcount space");
340 i = kvm_read(kvp->kd, (u_long)kvp->gpm.kcount, (void *)tickbuf,
341 kvp->gpm.kcountsize);
343 mib[2] = GPROF_COUNT;
344 i = kvp->gpm.kcountsize;
345 if (sysctl(mib, 3, tickbuf, &i, NULL, 0) < 0)
348 if (i != kvp->gpm.kcountsize)
349 errx(6, "read ticks: read %lu, got %zd: %s",
350 kvp->gpm.kcountsize, i,
351 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
352 if ((fwrite(tickbuf, kvp->gpm.kcountsize, 1, fp)) != 1)
353 err(7, "writing tocks to gmon.out");
357 * Write out the arc info.
359 if ((froms = (u_short *)malloc(kvp->gpm.fromssize)) == NULL)
360 errx(8, "cannot allocate froms space");
362 i = kvm_read(kvp->kd, (u_long)kvp->gpm.froms, (void *)froms,
365 mib[2] = GPROF_FROMS;
366 i = kvp->gpm.fromssize;
367 if (sysctl(mib, 3, froms, &i, NULL, 0) < 0)
370 if (i != kvp->gpm.fromssize)
371 errx(9, "read froms: read %lu, got %zd: %s",
372 kvp->gpm.fromssize, i,
373 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
374 if ((tos = (struct tostruct *)malloc(kvp->gpm.tossize)) == NULL)
375 errx(10, "cannot allocate tos space");
377 i = kvm_read(kvp->kd, (u_long)kvp->gpm.tos, (void *)tos,
381 i = kvp->gpm.tossize;
382 if (sysctl(mib, 3, tos, &i, NULL, 0) < 0)
385 if (i != kvp->gpm.tossize)
386 errx(11, "read tos: read %lu, got %zd: %s",
388 kflag ? kvm_geterr(kvp->kd) : strerror(errno));
390 warnx("lowpc 0x%tx, textsize 0x%lx",
391 kvp->gpm.lowpc, kvp->gpm.textsize);
392 endfrom = kvp->gpm.fromssize / sizeof(*froms);
393 for (fromindex = 0; fromindex < endfrom; ++fromindex) {
394 if (froms[fromindex] == 0)
396 frompc = (u_long)kvp->gpm.lowpc +
397 (fromindex * kvp->gpm.hashfraction * sizeof(*froms));
398 for (toindex = froms[fromindex]; toindex != 0;
399 toindex = tos[toindex].link) {
401 warnx("[mcleanup] frompc 0x%lx selfpc 0x%lx count %ld",
402 frompc, tos[toindex].selfpc,
404 rawarc.raw_frompc = frompc;
405 rawarc.raw_selfpc = (u_long)tos[toindex].selfpc;
406 rawarc.raw_count = tos[toindex].count;
407 fwrite((char *)&rawarc, sizeof(rawarc), 1, fp);
414 * Reset the kernel profiling date structures.
417 reset(struct kvmvars *kvp)
423 setprof(kvp, GMON_PROF_OFF);
425 biggest = kvp->gpm.kcountsize;
426 if (kvp->gpm.fromssize > biggest)
427 biggest = kvp->gpm.fromssize;
428 if (kvp->gpm.tossize > biggest)
429 biggest = kvp->gpm.tossize;
430 if ((zbuf = (char *)malloc(biggest)) == NULL)
431 errx(12, "cannot allocate zbuf space");
432 bzero(zbuf, biggest);
434 if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf,
435 kvp->gpm.kcountsize) != kvp->gpm.kcountsize)
436 errx(13, "tickbuf zero: %s", kvm_geterr(kvp->kd));
437 if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf,
438 kvp->gpm.fromssize) != kvp->gpm.fromssize)
439 errx(14, "froms zero: %s", kvm_geterr(kvp->kd));
440 if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf,
441 kvp->gpm.tossize) != kvp->gpm.tossize)
442 errx(15, "tos zero: %s", kvm_geterr(kvp->kd));
448 mib[2] = GPROF_COUNT;
449 if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.kcountsize) < 0)
450 err(13, "tickbuf zero");
451 mib[2] = GPROF_FROMS;
452 if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.fromssize) < 0)
453 err(14, "froms zero");
455 if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.tossize) < 0)