1 /* $OpenBSD: kgmon.c,v 1.20 2013/11/27 13:32:02 okan Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/param.h> 33 #include <sys/file.h> 34 #include <sys/sysctl.h> 35 #include <sys/gmon.h> 36 #include <errno.h> 37 #include <err.h> 38 #include <kvm.h> 39 #include <limits.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <unistd.h> 43 #include <string.h> 44 #include <nlist.h> 45 #include <ctype.h> 46 #include <paths.h> 47 48 struct nlist nl[] = { 49 #define N_GMONPARAM 0 50 { "__gmonparam" }, 51 #define N_PROFHZ 1 52 { "_profhz" }, 53 { NULL } 54 }; 55 56 struct kvmvars { 57 kvm_t *kd; 58 struct gmonparam gpm; 59 }; 60 61 extern char *__progname; 62 63 int bflag, cflag, hflag, kflag, rflag, pflag; 64 int debug = 0; 65 void kgmon(char *, char *, struct kvmvars *, int); 66 void setprof(struct kvmvars *, int, int); 67 void dumpstate(struct kvmvars *, int); 68 void reset(struct kvmvars *, int); 69 void kern_readonly(int); 70 int getprof(struct kvmvars *, int); 71 int getprofhz(struct kvmvars *); 72 int openfiles(char *, char *, struct kvmvars *, int); 73 int getncpu(void); 74 75 int 76 main(int argc, char **argv) 77 { 78 int ch, ncpu, cpuid = -1; 79 struct kvmvars kvmvars; 80 char *sys, *kmemf; 81 const char *p; 82 83 seteuid(getuid()); 84 kmemf = NULL; 85 sys = NULL; 86 while ((ch = getopt(argc, argv, "M:N:bc:hpr")) != -1) { 87 switch(ch) { 88 89 case 'M': 90 kmemf = optarg; 91 kflag = 1; 92 break; 93 94 case 'N': 95 sys = optarg; 96 break; 97 98 case 'b': 99 bflag = 1; 100 break; 101 102 case 'c': 103 cflag = 1; 104 cpuid = strtonum(optarg, 0, 1024, &p); 105 if (p) 106 errx(1, "illegal CPU id %s: %s", optarg, p); 107 break; 108 109 case 'h': 110 hflag = 1; 111 break; 112 113 case 'p': 114 pflag = 1; 115 break; 116 117 case 'r': 118 rflag = 1; 119 break; 120 121 default: 122 fprintf(stderr, "usage: %s [-bhpr] " 123 "[-c cpuid] [-M core] [-N system]\n", __progname); 124 exit(1); 125 } 126 } 127 argc -= optind; 128 argv += optind; 129 130 #define BACKWARD_COMPATIBILITY 131 #ifdef BACKWARD_COMPATIBILITY 132 if (*argv) { 133 sys = *argv; 134 if (*++argv) { 135 kmemf = *argv; 136 ++kflag; 137 } 138 } 139 #endif 140 141 if (cflag) { 142 kgmon(sys, kmemf, &kvmvars, cpuid); 143 } else { 144 ncpu = getncpu(); 145 for (cpuid = 0; cpuid < ncpu; cpuid++) 146 kgmon(sys, kmemf, &kvmvars, cpuid); 147 } 148 149 return (0); 150 } 151 152 void 153 kgmon(char *sys, char *kmemf, struct kvmvars *kvp, int cpuid) 154 { 155 int mode, disp, accessmode; 156 157 accessmode = openfiles(sys, kmemf, kvp, cpuid); 158 mode = getprof(kvp, cpuid); 159 if (hflag) 160 disp = GMON_PROF_OFF; 161 else if (bflag) 162 disp = GMON_PROF_ON; 163 else 164 disp = mode; 165 if (pflag) 166 dumpstate(kvp, cpuid); 167 if (rflag) 168 reset(kvp, cpuid); 169 if (accessmode == O_RDWR) 170 setprof(kvp, cpuid, disp); 171 printf("%s: kernel profiling is %s for cpu %d.\n", __progname, 172 disp == GMON_PROF_OFF ? "off" : "running", cpuid); 173 } 174 175 /* 176 * Check that profiling is enabled and open any ncessary files. 177 */ 178 int 179 openfiles(char *sys, char *kmemf, struct kvmvars *kvp, int cpuid) 180 { 181 int mib[4], state, openmode; 182 size_t size; 183 char errbuf[_POSIX2_LINE_MAX]; 184 185 if (!kflag) { 186 mib[0] = CTL_KERN; 187 mib[1] = KERN_PROF; 188 mib[2] = GPROF_STATE; 189 mib[3] = cpuid; 190 size = sizeof state; 191 if (sysctl(mib, 4, &state, &size, NULL, 0) < 0) 192 errx(20, "profiling not defined in kernel."); 193 if (!(bflag || hflag || rflag || 194 (pflag && state == GMON_PROF_ON))) 195 return (O_RDONLY); 196 (void)seteuid(0); 197 if (sysctl(mib, 4, NULL, NULL, &state, size) >= 0) 198 return (O_RDWR); 199 (void)seteuid(getuid()); 200 kern_readonly(state); 201 return (O_RDONLY); 202 } 203 openmode = (bflag || hflag || pflag || rflag) ? O_RDWR : O_RDONLY; 204 kvp->kd = kvm_openfiles(sys, kmemf, NULL, openmode, errbuf); 205 if (kvp->kd == NULL) { 206 if (openmode == O_RDWR) { 207 openmode = O_RDONLY; 208 kvp->kd = kvm_openfiles(sys, kmemf, NULL, O_RDONLY, 209 errbuf); 210 } 211 if (kvp->kd == NULL) 212 errx(2, "kvm_openfiles: %s", errbuf); 213 kern_readonly(GMON_PROF_ON); 214 } 215 if (kvm_nlist(kvp->kd, nl) < 0) 216 errx(3, "%s: no namelist", sys ? sys : _PATH_UNIX); 217 if (!nl[N_GMONPARAM].n_value) 218 errx(20, "profiling not defined in kernel."); 219 return (openmode); 220 } 221 222 /* 223 * Suppress options that require a writable kernel. 224 */ 225 void 226 kern_readonly(int mode) 227 { 228 extern char *__progname; 229 230 (void)fprintf(stderr, "%s: kernel read-only: ", __progname); 231 if (pflag && mode == GMON_PROF_ON) 232 (void)fprintf(stderr, "data may be inconsistent\n"); 233 if (rflag) 234 (void)fprintf(stderr, "-r suppressed\n"); 235 if (bflag) 236 (void)fprintf(stderr, "-b suppressed\n"); 237 if (hflag) 238 (void)fprintf(stderr, "-h suppressed\n"); 239 rflag = bflag = hflag = 0; 240 } 241 242 /* 243 * Get the state of kernel profiling. 244 */ 245 int 246 getprof(struct kvmvars *kvp, int cpuid) 247 { 248 int mib[4]; 249 size_t size; 250 251 if (kflag) { 252 size = kvm_read(kvp->kd, nl[N_GMONPARAM].n_value, &kvp->gpm, 253 sizeof kvp->gpm); 254 } else { 255 mib[0] = CTL_KERN; 256 mib[1] = KERN_PROF; 257 mib[2] = GPROF_GMONPARAM; 258 mib[3] = cpuid; 259 size = sizeof kvp->gpm; 260 if (sysctl(mib, 4, &kvp->gpm, &size, NULL, 0) < 0) 261 size = 0; 262 } 263 if (size != sizeof kvp->gpm) 264 errx(4, "cannot get gmonparam: %s", 265 kflag ? kvm_geterr(kvp->kd) : strerror(errno)); 266 return (kvp->gpm.state); 267 } 268 269 /* 270 * Enable or disable kernel profiling according to the state variable. 271 */ 272 void 273 setprof(struct kvmvars *kvp, int cpuid, int state) 274 { 275 struct gmonparam *p = (struct gmonparam *)nl[N_GMONPARAM].n_value; 276 int mib[4], oldstate; 277 size_t sz; 278 279 sz = sizeof(state); 280 if (!kflag) { 281 mib[0] = CTL_KERN; 282 mib[1] = KERN_PROF; 283 mib[2] = GPROF_STATE; 284 mib[3] = cpuid; 285 if (sysctl(mib, 4, &oldstate, &sz, NULL, 0) < 0) 286 goto bad; 287 if (oldstate == state) 288 return; 289 (void)seteuid(0); 290 if (sysctl(mib, 4, NULL, NULL, &state, sz) >= 0) { 291 (void)seteuid(getuid()); 292 return; 293 } 294 (void)seteuid(getuid()); 295 } else if (kvm_write(kvp->kd, (u_long)&p->state, (void *)&state, sz) 296 == sz) 297 return; 298 bad: 299 warnx("warning: cannot turn profiling %s", 300 state == GMON_PROF_OFF ? "off" : "on"); 301 } 302 303 /* 304 * Build the gmon.out file. 305 */ 306 void 307 dumpstate(struct kvmvars *kvp, int cpuid) 308 { 309 FILE *fp; 310 struct rawarc rawarc; 311 struct tostruct *tos; 312 u_long frompc; 313 u_short *froms, *tickbuf; 314 int mib[4]; 315 size_t i; 316 struct gmonhdr h; 317 int fromindex, endfrom, toindex; 318 char buf[16]; 319 320 snprintf(buf, sizeof(buf), "gmon-%02d.out", cpuid); 321 322 setprof(kvp, cpuid, GMON_PROF_OFF); 323 fp = fopen(buf, "w"); 324 if (fp == 0) { 325 perror(buf); 326 return; 327 } 328 329 /* 330 * Build the gmon header and write it to a file. 331 */ 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 = getprofhz(kvp); 338 fwrite((char *)&h, sizeof(h), 1, fp); 339 340 /* 341 * Write out the tick buffer. 342 */ 343 mib[0] = CTL_KERN; 344 mib[1] = KERN_PROF; 345 if ((tickbuf = (u_short *)malloc(kvp->gpm.kcountsize)) == NULL) 346 errx(5, "cannot allocate kcount space"); 347 if (kflag) { 348 i = kvm_read(kvp->kd, (u_long)kvp->gpm.kcount, (void *)tickbuf, 349 kvp->gpm.kcountsize); 350 } else { 351 mib[2] = GPROF_COUNT; 352 mib[3] = cpuid; 353 i = kvp->gpm.kcountsize; 354 if (sysctl(mib, 4, tickbuf, &i, NULL, 0) < 0) 355 i = 0; 356 } 357 if (i != kvp->gpm.kcountsize) 358 errx(6, "read ticks: read %lu, got %zu: %s", 359 kvp->gpm.kcountsize, i, 360 kflag ? kvm_geterr(kvp->kd) : strerror(errno)); 361 if ((fwrite(tickbuf, kvp->gpm.kcountsize, 1, fp)) != 1) 362 err(7, "writing tocks to gmon.out"); 363 free(tickbuf); 364 365 /* 366 * Write out the arc info. 367 */ 368 if ((froms = (u_short *)malloc(kvp->gpm.fromssize)) == NULL) 369 errx(8, "cannot allocate froms space"); 370 if (kflag) { 371 i = kvm_read(kvp->kd, (u_long)kvp->gpm.froms, (void *)froms, 372 kvp->gpm.fromssize); 373 } else { 374 mib[2] = GPROF_FROMS; 375 mib[3] = cpuid; 376 i = kvp->gpm.fromssize; 377 if (sysctl(mib, 4, froms, &i, NULL, 0) < 0) 378 i = 0; 379 } 380 if (i != kvp->gpm.fromssize) 381 errx(9, "read froms: read %lu, got %zu: %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"); 386 if (kflag) { 387 i = kvm_read(kvp->kd, (u_long)kvp->gpm.tos, (void *)tos, 388 kvp->gpm.tossize); 389 } else { 390 mib[2] = GPROF_TOS; 391 mib[3] = cpuid; 392 i = kvp->gpm.tossize; 393 if (sysctl(mib, 4, tos, &i, NULL, 0) < 0) 394 i = 0; 395 } 396 if (i != kvp->gpm.tossize) 397 errx(11, "read tos: read %lu, got %zu: %s", 398 kvp->gpm.tossize, i, 399 kflag ? kvm_geterr(kvp->kd) : strerror(errno)); 400 if (debug) 401 warnx("lowpc 0x%lx, textsize 0x%lx", 402 kvp->gpm.lowpc, kvp->gpm.textsize); 403 endfrom = kvp->gpm.fromssize / sizeof(*froms); 404 for (fromindex = 0; fromindex < endfrom; ++fromindex) { 405 if (froms[fromindex] == 0) 406 continue; 407 frompc = (u_long)kvp->gpm.lowpc + 408 (fromindex * kvp->gpm.hashfraction * sizeof(*froms)); 409 for (toindex = froms[fromindex]; toindex != 0; 410 toindex = tos[toindex].link) { 411 if (debug) 412 warnx("[mcleanup] frompc 0x%lx selfpc 0x%lx count %ld", 413 frompc, tos[toindex].selfpc, tos[toindex].count); 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); 418 } 419 } 420 fclose(fp); 421 } 422 423 /* 424 * Get the profiling rate. 425 */ 426 int 427 getprofhz(struct kvmvars *kvp) 428 { 429 int mib[2], profrate; 430 size_t size; 431 struct clockinfo clockrate; 432 433 if (kflag) { 434 profrate = 1; 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)); 438 return (profrate); 439 } 440 mib[0] = CTL_KERN; 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); 447 } 448 449 /* 450 * Reset the kernel profiling date structures. 451 */ 452 void 453 reset(struct kvmvars *kvp, int cpuid) 454 { 455 char *zbuf; 456 u_long biggest; 457 int mib[4]; 458 459 setprof(kvp, cpuid, GMON_PROF_OFF); 460 461 biggest = kvp->gpm.kcountsize; 462 if (kvp->gpm.fromssize > biggest) 463 biggest = kvp->gpm.fromssize; 464 if (kvp->gpm.tossize > biggest) 465 biggest = kvp->gpm.tossize; 466 if ((zbuf = (char *)malloc(biggest)) == NULL) 467 errx(12, "cannot allocate zbuf space"); 468 bzero(zbuf, biggest); 469 if (kflag) { 470 if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf, 471 kvp->gpm.kcountsize) != kvp->gpm.kcountsize) 472 errx(13, "tickbuf zero: %s", kvm_geterr(kvp->kd)); 473 if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf, 474 kvp->gpm.fromssize) != kvp->gpm.fromssize) 475 errx(14, "froms zero: %s", kvm_geterr(kvp->kd)); 476 if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf, 477 kvp->gpm.tossize) != kvp->gpm.tossize) 478 errx(15, "tos zero: %s", kvm_geterr(kvp->kd)); 479 return; 480 } 481 (void)seteuid(0); 482 mib[0] = CTL_KERN; 483 mib[1] = KERN_PROF; 484 mib[2] = GPROF_COUNT; 485 mib[3] = cpuid; 486 if (sysctl(mib, 4, NULL, NULL, zbuf, kvp->gpm.kcountsize) < 0) 487 err(13, "tickbuf zero"); 488 mib[2] = GPROF_FROMS; 489 if (sysctl(mib, 4, NULL, NULL, zbuf, kvp->gpm.fromssize) < 0) 490 err(14, "froms zero"); 491 mib[2] = GPROF_TOS; 492 if (sysctl(mib, 4, NULL, NULL, zbuf, kvp->gpm.tossize) < 0) 493 err(15, "tos zero"); 494 (void)seteuid(getuid()); 495 free(zbuf); 496 } 497 498 int 499 getncpu(void) 500 { 501 int mib[2] = { CTL_HW, HW_NCPU }; 502 size_t size; 503 int ncpu; 504 505 size = sizeof(ncpu); 506 if (sysctl(mib, 2, &ncpu, &size, NULL, 0) < 0) { 507 warnx("cannot read hw.ncpu"); 508 return (1); 509 } 510 511 return (ncpu); 512 } 513