1 /* $OpenBSD: kgmon.c,v 1.24 2016/08/27 01:50:07 guenther 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/file.h> 33 #include <sys/sysctl.h> 34 #include <sys/time.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 kmemf = NULL; 84 sys = NULL; 85 while ((ch = getopt(argc, argv, "M:N:bc:hpr")) != -1) { 86 switch(ch) { 87 88 case 'M': 89 kmemf = optarg; 90 kflag = 1; 91 break; 92 93 case 'N': 94 sys = optarg; 95 break; 96 97 case 'b': 98 bflag = 1; 99 break; 100 101 case 'c': 102 cflag = 1; 103 cpuid = strtonum(optarg, 0, 1024, &p); 104 if (p) 105 errx(1, "illegal CPU id %s: %s", optarg, p); 106 break; 107 108 case 'h': 109 hflag = 1; 110 break; 111 112 case 'p': 113 pflag = 1; 114 break; 115 116 case 'r': 117 rflag = 1; 118 break; 119 120 default: 121 fprintf(stderr, "usage: %s [-bhpr] " 122 "[-c cpuid] [-M core] [-N system]\n", __progname); 123 exit(1); 124 } 125 } 126 argc -= optind; 127 argv += optind; 128 129 #define BACKWARD_COMPATIBILITY 130 #ifdef BACKWARD_COMPATIBILITY 131 if (*argv) { 132 sys = *argv; 133 if (*++argv) { 134 kmemf = *argv; 135 ++kflag; 136 } 137 } 138 #endif 139 140 if (cflag) { 141 kgmon(sys, kmemf, &kvmvars, cpuid); 142 } else { 143 ncpu = getncpu(); 144 for (cpuid = 0; cpuid < ncpu; cpuid++) 145 kgmon(sys, kmemf, &kvmvars, cpuid); 146 } 147 148 return (0); 149 } 150 151 void 152 kgmon(char *sys, char *kmemf, struct kvmvars *kvp, int cpuid) 153 { 154 int mode, disp, accessmode; 155 156 accessmode = openfiles(sys, kmemf, kvp, cpuid); 157 mode = getprof(kvp, cpuid); 158 if (hflag) 159 disp = GMON_PROF_OFF; 160 else if (bflag) 161 disp = GMON_PROF_ON; 162 else 163 disp = mode; 164 if (pflag) 165 dumpstate(kvp, cpuid); 166 if (rflag) 167 reset(kvp, cpuid); 168 if (accessmode == O_RDWR) 169 setprof(kvp, cpuid, disp); 170 printf("%s: kernel profiling is %s for cpu %d.\n", __progname, 171 disp == GMON_PROF_OFF ? "off" : "running", cpuid); 172 } 173 174 /* 175 * Check that profiling is enabled and open any ncessary files. 176 */ 177 int 178 openfiles(char *sys, char *kmemf, struct kvmvars *kvp, int cpuid) 179 { 180 int mib[4], state, openmode; 181 size_t size; 182 char errbuf[_POSIX2_LINE_MAX]; 183 184 if (!kflag) { 185 mib[0] = CTL_KERN; 186 mib[1] = KERN_PROF; 187 mib[2] = GPROF_STATE; 188 mib[3] = cpuid; 189 size = sizeof state; 190 if (sysctl(mib, 4, &state, &size, NULL, 0) < 0) 191 errx(20, "profiling not defined in kernel."); 192 if (!(bflag || hflag || rflag || 193 (pflag && state == GMON_PROF_ON))) 194 return (O_RDONLY); 195 if (sysctl(mib, 4, NULL, NULL, &state, size) >= 0) 196 return (O_RDWR); 197 kern_readonly(state); 198 return (O_RDONLY); 199 } 200 openmode = (bflag || hflag || pflag || rflag) ? O_RDWR : O_RDONLY; 201 kvp->kd = kvm_openfiles(sys, kmemf, NULL, openmode, errbuf); 202 if (kvp->kd == NULL) { 203 if (openmode == O_RDWR) { 204 openmode = O_RDONLY; 205 kvp->kd = kvm_openfiles(sys, kmemf, NULL, O_RDONLY, 206 errbuf); 207 } 208 if (kvp->kd == NULL) 209 errx(2, "kvm_openfiles: %s", errbuf); 210 kern_readonly(GMON_PROF_ON); 211 } 212 if (kvm_nlist(kvp->kd, nl) < 0) 213 errx(3, "%s: no namelist", sys ? sys : _PATH_UNIX); 214 if (!nl[N_GMONPARAM].n_value) 215 errx(20, "profiling not defined in kernel."); 216 return (openmode); 217 } 218 219 /* 220 * Suppress options that require a writable kernel. 221 */ 222 void 223 kern_readonly(int mode) 224 { 225 extern char *__progname; 226 227 (void)fprintf(stderr, "%s: kernel read-only: ", __progname); 228 if (pflag && mode == GMON_PROF_ON) 229 (void)fprintf(stderr, "data may be inconsistent\n"); 230 if (rflag) 231 (void)fprintf(stderr, "-r suppressed\n"); 232 if (bflag) 233 (void)fprintf(stderr, "-b suppressed\n"); 234 if (hflag) 235 (void)fprintf(stderr, "-h suppressed\n"); 236 rflag = bflag = hflag = 0; 237 } 238 239 /* 240 * Get the state of kernel profiling. 241 */ 242 int 243 getprof(struct kvmvars *kvp, int cpuid) 244 { 245 int mib[4]; 246 size_t size; 247 248 if (kflag) { 249 size = kvm_read(kvp->kd, nl[N_GMONPARAM].n_value, &kvp->gpm, 250 sizeof kvp->gpm); 251 } else { 252 mib[0] = CTL_KERN; 253 mib[1] = KERN_PROF; 254 mib[2] = GPROF_GMONPARAM; 255 mib[3] = cpuid; 256 size = sizeof kvp->gpm; 257 if (sysctl(mib, 4, &kvp->gpm, &size, NULL, 0) < 0) 258 size = 0; 259 } 260 if (size != sizeof kvp->gpm) 261 errx(4, "cannot get gmonparam: %s", 262 kflag ? kvm_geterr(kvp->kd) : strerror(errno)); 263 return (kvp->gpm.state); 264 } 265 266 /* 267 * Enable or disable kernel profiling according to the state variable. 268 */ 269 void 270 setprof(struct kvmvars *kvp, int cpuid, int state) 271 { 272 struct gmonparam *p = (struct gmonparam *)nl[N_GMONPARAM].n_value; 273 int mib[4], oldstate; 274 size_t sz; 275 276 sz = sizeof(state); 277 if (!kflag) { 278 mib[0] = CTL_KERN; 279 mib[1] = KERN_PROF; 280 mib[2] = GPROF_STATE; 281 mib[3] = cpuid; 282 if (sysctl(mib, 4, &oldstate, &sz, NULL, 0) < 0) 283 goto bad; 284 if (oldstate == state) 285 return; 286 if (sysctl(mib, 4, NULL, NULL, &state, sz) >= 0) 287 return; 288 } else if (kvm_write(kvp->kd, (u_long)&p->state, (void *)&state, sz) 289 == sz) 290 return; 291 bad: 292 warnx("warning: cannot turn profiling %s", 293 state == GMON_PROF_OFF ? "off" : "on"); 294 } 295 296 /* 297 * Build the gmon.out file. 298 */ 299 void 300 dumpstate(struct kvmvars *kvp, int cpuid) 301 { 302 FILE *fp; 303 struct rawarc rawarc; 304 struct tostruct *tos; 305 u_long frompc; 306 u_short *froms, *tickbuf; 307 int mib[4]; 308 size_t i; 309 struct gmonhdr h; 310 int fromindex, endfrom, toindex; 311 char buf[16]; 312 313 snprintf(buf, sizeof(buf), "gmon-%02d.out", cpuid); 314 315 setprof(kvp, cpuid, GMON_PROF_OFF); 316 fp = fopen(buf, "w"); 317 if (fp == 0) { 318 perror(buf); 319 return; 320 } 321 322 /* 323 * Build the gmon header and write it to a file. 324 */ 325 bzero(&h, sizeof(h)); 326 h.lpc = kvp->gpm.lowpc; 327 h.hpc = kvp->gpm.highpc; 328 h.ncnt = kvp->gpm.kcountsize + sizeof(h); 329 h.version = GMONVERSION; 330 h.profrate = getprofhz(kvp); 331 fwrite((char *)&h, sizeof(h), 1, fp); 332 333 /* 334 * Write out the tick buffer. 335 */ 336 mib[0] = CTL_KERN; 337 mib[1] = KERN_PROF; 338 if ((tickbuf = malloc(kvp->gpm.kcountsize)) == NULL) 339 errx(5, "cannot allocate kcount space"); 340 if (kflag) { 341 i = kvm_read(kvp->kd, (u_long)kvp->gpm.kcount, (void *)tickbuf, 342 kvp->gpm.kcountsize); 343 } else { 344 mib[2] = GPROF_COUNT; 345 mib[3] = cpuid; 346 i = kvp->gpm.kcountsize; 347 if (sysctl(mib, 4, tickbuf, &i, NULL, 0) < 0) 348 i = 0; 349 } 350 if (i != kvp->gpm.kcountsize) 351 errx(6, "read ticks: read %lu, got %zu: %s", 352 kvp->gpm.kcountsize, i, 353 kflag ? kvm_geterr(kvp->kd) : strerror(errno)); 354 if ((fwrite(tickbuf, kvp->gpm.kcountsize, 1, fp)) != 1) 355 err(7, "writing tocks to gmon.out"); 356 free(tickbuf); 357 358 /* 359 * Write out the arc info. 360 */ 361 if ((froms = malloc(kvp->gpm.fromssize)) == NULL) 362 errx(8, "cannot allocate froms space"); 363 if (kflag) { 364 i = kvm_read(kvp->kd, (u_long)kvp->gpm.froms, (void *)froms, 365 kvp->gpm.fromssize); 366 } else { 367 mib[2] = GPROF_FROMS; 368 mib[3] = cpuid; 369 i = kvp->gpm.fromssize; 370 if (sysctl(mib, 4, froms, &i, NULL, 0) < 0) 371 i = 0; 372 } 373 if (i != kvp->gpm.fromssize) 374 errx(9, "read froms: read %lu, got %zu: %s", 375 kvp->gpm.fromssize, i, 376 kflag ? kvm_geterr(kvp->kd) : strerror(errno)); 377 if ((tos = malloc(kvp->gpm.tossize)) == NULL) 378 errx(10, "cannot allocate tos space"); 379 if (kflag) { 380 i = kvm_read(kvp->kd, (u_long)kvp->gpm.tos, (void *)tos, 381 kvp->gpm.tossize); 382 } else { 383 mib[2] = GPROF_TOS; 384 mib[3] = cpuid; 385 i = kvp->gpm.tossize; 386 if (sysctl(mib, 4, tos, &i, NULL, 0) < 0) 387 i = 0; 388 } 389 if (i != kvp->gpm.tossize) 390 errx(11, "read tos: read %lu, got %zu: %s", 391 kvp->gpm.tossize, i, 392 kflag ? kvm_geterr(kvp->kd) : strerror(errno)); 393 if (debug) 394 warnx("lowpc 0x%lx, textsize 0x%lx", 395 kvp->gpm.lowpc, kvp->gpm.textsize); 396 endfrom = kvp->gpm.fromssize / sizeof(*froms); 397 for (fromindex = 0; fromindex < endfrom; ++fromindex) { 398 if (froms[fromindex] == 0) 399 continue; 400 frompc = (u_long)kvp->gpm.lowpc + 401 (fromindex * kvp->gpm.hashfraction * sizeof(*froms)); 402 for (toindex = froms[fromindex]; toindex != 0; 403 toindex = tos[toindex].link) { 404 if (debug) 405 warnx("[mcleanup] frompc 0x%lx selfpc 0x%lx count %ld", 406 frompc, tos[toindex].selfpc, tos[toindex].count); 407 rawarc.raw_frompc = frompc; 408 rawarc.raw_selfpc = (u_long)tos[toindex].selfpc; 409 rawarc.raw_count = tos[toindex].count; 410 fwrite((char *)&rawarc, sizeof(rawarc), 1, fp); 411 } 412 } 413 fclose(fp); 414 } 415 416 /* 417 * Get the profiling rate. 418 */ 419 int 420 getprofhz(struct kvmvars *kvp) 421 { 422 int mib[2], profrate; 423 size_t size; 424 struct clockinfo clockrate; 425 426 if (kflag) { 427 profrate = 1; 428 if (kvm_read(kvp->kd, nl[N_PROFHZ].n_value, &profrate, 429 sizeof profrate) != sizeof profrate) 430 warnx("get clockrate: %s", kvm_geterr(kvp->kd)); 431 return (profrate); 432 } 433 mib[0] = CTL_KERN; 434 mib[1] = KERN_CLOCKRATE; 435 clockrate.profhz = 1; 436 size = sizeof clockrate; 437 if (sysctl(mib, 2, &clockrate, &size, NULL, 0) < 0) 438 warn("get clockrate"); 439 return (clockrate.profhz); 440 } 441 442 /* 443 * Reset the kernel profiling date structures. 444 */ 445 void 446 reset(struct kvmvars *kvp, int cpuid) 447 { 448 char *zbuf; 449 u_long biggest; 450 int mib[4]; 451 452 setprof(kvp, cpuid, GMON_PROF_OFF); 453 454 biggest = kvp->gpm.kcountsize; 455 if (kvp->gpm.fromssize > biggest) 456 biggest = kvp->gpm.fromssize; 457 if (kvp->gpm.tossize > biggest) 458 biggest = kvp->gpm.tossize; 459 if ((zbuf = malloc(biggest)) == NULL) 460 errx(12, "cannot allocate zbuf space"); 461 bzero(zbuf, biggest); 462 if (kflag) { 463 if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf, 464 kvp->gpm.kcountsize) != kvp->gpm.kcountsize) 465 errx(13, "tickbuf zero: %s", kvm_geterr(kvp->kd)); 466 if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf, 467 kvp->gpm.fromssize) != kvp->gpm.fromssize) 468 errx(14, "froms zero: %s", kvm_geterr(kvp->kd)); 469 if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf, 470 kvp->gpm.tossize) != kvp->gpm.tossize) 471 errx(15, "tos zero: %s", kvm_geterr(kvp->kd)); 472 return; 473 } 474 mib[0] = CTL_KERN; 475 mib[1] = KERN_PROF; 476 mib[2] = GPROF_COUNT; 477 mib[3] = cpuid; 478 if (sysctl(mib, 4, NULL, NULL, zbuf, kvp->gpm.kcountsize) < 0) 479 err(13, "tickbuf zero"); 480 mib[2] = GPROF_FROMS; 481 if (sysctl(mib, 4, NULL, NULL, zbuf, kvp->gpm.fromssize) < 0) 482 err(14, "froms zero"); 483 mib[2] = GPROF_TOS; 484 if (sysctl(mib, 4, NULL, NULL, zbuf, kvp->gpm.tossize) < 0) 485 err(15, "tos zero"); 486 free(zbuf); 487 } 488 489 int 490 getncpu(void) 491 { 492 int mib[2] = { CTL_HW, HW_NCPU }; 493 size_t size; 494 int ncpu; 495 496 size = sizeof(ncpu); 497 if (sysctl(mib, 2, &ncpu, &size, NULL, 0) < 0) { 498 warnx("cannot read hw.ncpu"); 499 return (1); 500 } 501 502 return (ncpu); 503 } 504