kgmon.c revision 50479
1/* 2 * Copyright (c) 1983, 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 20 * 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 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35static const char copyright[] = 36"@(#) Copyright (c) 1983, 1992, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41#if 0 42static char sccsid[] = "@(#)kgmon.c 8.1 (Berkeley) 6/6/93"; 43#endif 44static const char rcsid[] = 45 "$FreeBSD: head/usr.sbin/kgmon/kgmon.c 50479 1999-08-28 01:35:59Z peter $"; 46#endif /* not lint */ 47 48#include <sys/param.h> 49#include <sys/file.h> 50#include <sys/time.h> 51#include <sys/sysctl.h> 52#include <sys/gmon.h> 53#include <ctype.h> 54#include <err.h> 55#include <errno.h> 56#include <kvm.h> 57#include <limits.h> 58#include <nlist.h> 59#include <paths.h> 60#include <stdio.h> 61#include <stdlib.h> 62#include <string.h> 63#include <unistd.h> 64 65struct nlist nl[] = { 66#define N_GMONPARAM 0 67 { "__gmonparam" }, 68#define N_PROFHZ 1 69 { "_profhz" }, 70 { NULL }, 71}; 72 73struct kvmvars { 74 kvm_t *kd; 75 struct gmonparam gpm; 76}; 77 78int Bflag, bflag, hflag, kflag, rflag, pflag; 79int debug = 0; 80int getprof __P((struct kvmvars *)); 81int getprofhz __P((struct kvmvars *)); 82void kern_readonly __P((int)); 83int openfiles __P((char *, char *, struct kvmvars *)); 84void setprof __P((struct kvmvars *kvp, int state)); 85void dumpstate __P((struct kvmvars *kvp)); 86void reset __P((struct kvmvars *kvp)); 87static void usage __P((void)); 88 89int 90main(int argc, char **argv) 91{ 92 int ch, mode, disp, accessmode; 93 struct kvmvars kvmvars; 94 char *system, *kmemf; 95 96 seteuid(getuid()); 97 kmemf = NULL; 98 system = NULL; 99 while ((ch = getopt(argc, argv, "M:N:Bbhpr")) != -1) { 100 switch((char)ch) { 101 102 case 'M': 103 kmemf = optarg; 104 kflag = 1; 105 break; 106 107 case 'N': 108 system = optarg; 109 break; 110 111 case 'B': 112 Bflag = 1; 113 break; 114 115 case 'b': 116 bflag = 1; 117 break; 118 119 case 'h': 120 hflag = 1; 121 break; 122 123 case 'p': 124 pflag = 1; 125 break; 126 127 case 'r': 128 rflag = 1; 129 break; 130 131 default: 132 usage(); 133 } 134 } 135 argc -= optind; 136 argv += optind; 137 138#define BACKWARD_COMPATIBILITY 139#ifdef BACKWARD_COMPATIBILITY 140 if (*argv) { 141 system = *argv; 142 if (*++argv) { 143 kmemf = *argv; 144 ++kflag; 145 } 146 } 147#endif 148 if (system == NULL) 149 system = (char *)getbootfile(); 150 accessmode = openfiles(system, kmemf, &kvmvars); 151 mode = getprof(&kvmvars); 152 if (hflag) 153 disp = GMON_PROF_OFF; 154 else if (Bflag) 155 disp = GMON_PROF_HIRES; 156 else if (bflag) 157 disp = GMON_PROF_ON; 158 else 159 disp = mode; 160 if (pflag) 161 dumpstate(&kvmvars); 162 if (rflag) 163 reset(&kvmvars); 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"); 173 return (0); 174} 175 176static void 177usage() 178{ 179 fprintf(stderr, "usage: kgmon [-Bbhrp] [-M core] [-N system]\n"); 180 exit(1); 181} 182 183/* 184 * Check that profiling is enabled and open any ncessary files. 185 */ 186int 187openfiles(system, kmemf, kvp) 188 char *system; 189 char *kmemf; 190 struct kvmvars *kvp; 191{ 192 int mib[3], state, size, openmode; 193 char errbuf[_POSIX2_LINE_MAX]; 194 195 if (!kflag) { 196 mib[0] = CTL_KERN; 197 mib[1] = KERN_PROF; 198 mib[2] = GPROF_STATE; 199 size = sizeof 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 || 203 (pflag && 204 (state == GMON_PROF_HIRES || state == GMON_PROF_ON)))) 205 return (O_RDONLY); 206 (void)seteuid(0); 207 if (sysctl(mib, 3, NULL, NULL, &state, size) >= 0) 208 return (O_RDWR); 209 (void)seteuid(getuid()); 210 kern_readonly(state); 211 return (O_RDONLY); 212 } 213 openmode = (Bflag || bflag || hflag || pflag || rflag) 214 ? O_RDWR : O_RDONLY; 215 kvp->kd = kvm_openfiles(system, kmemf, NULL, openmode, errbuf); 216 if (kvp->kd == NULL) { 217 if (openmode == O_RDWR) { 218 openmode = O_RDONLY; 219 kvp->kd = kvm_openfiles(system, kmemf, NULL, O_RDONLY, 220 errbuf); 221 } 222 if (kvp->kd == NULL) 223 errx(2, "kvm_openfiles: %s", errbuf); 224 kern_readonly(GMON_PROF_ON); 225 } 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"); 230 return (openmode); 231} 232 233/* 234 * Suppress options that require a writable kernel. 235 */ 236void 237kern_readonly(mode) 238 int mode; 239{ 240 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"); 244 if (rflag) 245 (void)fprintf(stderr, "-r supressed\n"); 246 if (Bflag) 247 (void)fprintf(stderr, "-B supressed\n"); 248 if (bflag) 249 (void)fprintf(stderr, "-b supressed\n"); 250 if (hflag) 251 (void)fprintf(stderr, "-h supressed\n"); 252 rflag = Bflag = bflag = hflag = 0; 253} 254 255/* 256 * Get the state of kernel profiling. 257 */ 258int 259getprof(kvp) 260 struct kvmvars *kvp; 261{ 262 int mib[3], size; 263 264 if (kflag) { 265 size = kvm_read(kvp->kd, nl[N_GMONPARAM].n_value, &kvp->gpm, 266 sizeof kvp->gpm); 267 } else { 268 mib[0] = CTL_KERN; 269 mib[1] = KERN_PROF; 270 mib[2] = GPROF_GMONPARAM; 271 size = sizeof kvp->gpm; 272 if (sysctl(mib, 3, &kvp->gpm, &size, NULL, 0) < 0) 273 size = 0; 274 } 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); 279} 280 281/* 282 * Enable or disable kernel profiling according to the state variable. 283 */ 284void 285setprof(kvp, state) 286 struct kvmvars *kvp; 287 int state; 288{ 289 struct gmonparam *p = (struct gmonparam *)nl[N_GMONPARAM].n_value; 290 int mib[3], sz, oldstate; 291 292 sz = sizeof(state); 293 if (!kflag) { 294 mib[0] = CTL_KERN; 295 mib[1] = KERN_PROF; 296 mib[2] = GPROF_STATE; 297 if (sysctl(mib, 3, &oldstate, &sz, NULL, 0) < 0) 298 goto bad; 299 if (oldstate == state) 300 return; 301 (void)seteuid(0); 302 if (sysctl(mib, 3, NULL, NULL, &state, sz) >= 0) { 303 (void)seteuid(getuid()); 304 return; 305 } 306 (void)seteuid(getuid()); 307 } else if (kvm_write(kvp->kd, (u_long)&p->state, (void *)&state, sz) 308 == sz) 309 return; 310bad: 311 warnx("warning: cannot turn profiling %s", 312 state == GMON_PROF_OFF ? "off" : "on"); 313} 314 315/* 316 * Build the gmon.out file. 317 */ 318void 319dumpstate(kvp) 320 struct kvmvars *kvp; 321{ 322 register FILE *fp; 323 struct rawarc rawarc; 324 struct tostruct *tos; 325 u_long frompc; 326 u_short *froms, *tickbuf; 327 int mib[3], i; 328 struct gmonhdr h; 329 int fromindex, endfrom, toindex; 330 331 setprof(kvp, GMON_PROF_OFF); 332 fp = fopen("gmon.out", "w"); 333 if (fp == 0) { 334 warn("gmon.out"); 335 return; 336 } 337 338 /* 339 * Build the gmon header and write it to a file. 340 */ 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; 347 if (h.profrate == 0) 348 h.profrate = getprofhz(kvp); /* ancient kernel */ 349 fwrite((char *)&h, sizeof(h), 1, fp); 350 351 /* 352 * Write out the tick buffer. 353 */ 354 mib[0] = CTL_KERN; 355 mib[1] = KERN_PROF; 356 if ((tickbuf = (u_short *)malloc(kvp->gpm.kcountsize)) == NULL) 357 errx(5, "cannot allocate kcount space"); 358 if (kflag) { 359 i = kvm_read(kvp->kd, (u_long)kvp->gpm.kcount, (void *)tickbuf, 360 kvp->gpm.kcountsize); 361 } else { 362 mib[2] = GPROF_COUNT; 363 i = kvp->gpm.kcountsize; 364 if (sysctl(mib, 3, tickbuf, &i, NULL, 0) < 0) 365 i = 0; 366 } 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"); 373 free(tickbuf); 374 375 /* 376 * Write out the arc info. 377 */ 378 if ((froms = (u_short *)malloc(kvp->gpm.fromssize)) == NULL) 379 errx(8, "cannot allocate froms space"); 380 if (kflag) { 381 i = kvm_read(kvp->kd, (u_long)kvp->gpm.froms, (void *)froms, 382 kvp->gpm.fromssize); 383 } else { 384 mib[2] = GPROF_FROMS; 385 i = kvp->gpm.fromssize; 386 if (sysctl(mib, 3, froms, &i, NULL, 0) < 0) 387 i = 0; 388 } 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"); 395 if (kflag) { 396 i = kvm_read(kvp->kd, (u_long)kvp->gpm.tos, (void *)tos, 397 kvp->gpm.tossize); 398 } else { 399 mib[2] = GPROF_TOS; 400 i = kvp->gpm.tossize; 401 if (sysctl(mib, 3, tos, &i, NULL, 0) < 0) 402 i = 0; 403 } 404 if (i != kvp->gpm.tossize) 405 errx(11, "read tos: read %u, got %d: %s", 406 kvp->gpm.tossize, i, 407 kflag ? kvm_geterr(kvp->kd) : strerror(errno)); 408 if (debug) 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) 414 continue; 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) { 419 if (debug) 420 warnx("[mcleanup] frompc 0x%x selfpc 0x%x count %d", 421 frompc, tos[toindex].selfpc, 422 tos[toindex].count); 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); 427 } 428 } 429 fclose(fp); 430} 431 432/* 433 * Get the profiling rate. 434 */ 435int 436getprofhz(kvp) 437 struct kvmvars *kvp; 438{ 439 int mib[2], size, profrate; 440 struct clockinfo clockrate; 441 442 if (kflag) { 443 profrate = 1; 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)); 447 return (profrate); 448 } 449 mib[0] = CTL_KERN; 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); 456} 457 458/* 459 * Reset the kernel profiling date structures. 460 */ 461void 462reset(kvp) 463 struct kvmvars *kvp; 464{ 465 char *zbuf; 466 u_long biggest; 467 int mib[3]; 468 469 setprof(kvp, GMON_PROF_OFF); 470 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); 479 if (kflag) { 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)); 489 return; 490 } 491 (void)seteuid(0); 492 mib[0] = CTL_KERN; 493 mib[1] = KERN_PROF; 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"); 500 mib[2] = GPROF_TOS; 501 if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.tossize) < 0) 502 err(15, "tos zero"); 503 (void)seteuid(getuid()); 504 free(zbuf); 505} 506