1 /* $OpenBSD: kgmon.c,v 1.26 2019/06/28 13:32:48 deraadt 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/types.h> 33 #include <sys/sysctl.h> 34 #include <sys/time.h> 35 #include <sys/gmon.h> 36 37 #include <errno.h> 38 #include <err.h> 39 #include <fcntl.h> 40 #include <kvm.h> 41 #include <limits.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <unistd.h> 45 #include <string.h> 46 #include <nlist.h> 47 #include <paths.h> 48 49 struct nlist nl[] = { 50 #define N_GMONPARAM 0 51 { "__gmonparam" }, 52 #define N_PROFHZ 1 53 { "_profhz" }, 54 { NULL } 55 }; 56 57 struct kvmvars { 58 kvm_t *kd; 59 struct gmonparam gpm; 60 }; 61 62 extern char *__progname; 63 64 int bflag, cflag, hflag, kflag, rflag, pflag; 65 int debug = 0; 66 void kgmon(char *, char *, struct kvmvars *, int); 67 void setprof(struct kvmvars *, int, int); 68 void dumpstate(struct kvmvars *, int); 69 void reset(struct kvmvars *, int); 70 void kern_readonly(int); 71 int getprof(struct kvmvars *, int); 72 int getprofhz(struct kvmvars *); 73 int openfiles(char *, char *, struct kvmvars *, int); 74 int getncpu(void); 75 76 int 77 main(int argc, char **argv) 78 { 79 int ch, ncpu, cpuid = -1; 80 struct kvmvars kvmvars; 81 char *sys, *kmemf; 82 const char *p; 83 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) == -1) 192 errx(20, "profiling not defined in kernel."); 193 if (!(bflag || hflag || rflag || 194 (pflag && state == GMON_PROF_ON))) 195 return (O_RDONLY); 196 if (sysctl(mib, 4, NULL, NULL, &state, size) >= 0) 197 return (O_RDWR); 198 kern_readonly(state); 199 return (O_RDONLY); 200 } 201 openmode = (bflag || hflag || pflag || rflag) ? O_RDWR : O_RDONLY; 202 kvp->kd = kvm_openfiles(sys, kmemf, NULL, openmode, errbuf); 203 if (kvp->kd == NULL) { 204 if (openmode == O_RDWR) { 205 openmode = O_RDONLY; 206 kvp->kd = kvm_openfiles(sys, kmemf, NULL, O_RDONLY, 207 errbuf); 208 } 209 if (kvp->kd == NULL) 210 errx(2, "kvm_openfiles: %s", errbuf); 211 kern_readonly(GMON_PROF_ON); 212 } 213 if (kvm_nlist(kvp->kd, nl) == -1) 214 errx(3, "%s: no namelist", sys ? sys : _PATH_UNIX); 215 if (!nl[N_GMONPARAM].n_value) 216 errx(20, "profiling not defined in kernel."); 217 return (openmode); 218 } 219 220 /* 221 * Suppress options that require a writable kernel. 222 */ 223 void 224 kern_readonly(int mode) 225 { 226 extern char *__progname; 227 228 (void)fprintf(stderr, "%s: kernel read-only: ", __progname); 229 if (pflag && mode == GMON_PROF_ON) 230 (void)fprintf(stderr, "data may be inconsistent\n"); 231 if (rflag) 232 (void)fprintf(stderr, "-r suppressed\n"); 233 if (bflag) 234 (void)fprintf(stderr, "-b suppressed\n"); 235 if (hflag) 236 (void)fprintf(stderr, "-h suppressed\n"); 237 rflag = bflag = hflag = 0; 238 } 239 240 /* 241 * Get the state of kernel profiling. 242 */ 243 int 244 getprof(struct kvmvars *kvp, int cpuid) 245 { 246 int mib[4]; 247 size_t size; 248 249 if (kflag) { 250 size = kvm_read(kvp->kd, nl[N_GMONPARAM].n_value, &kvp->gpm, 251 sizeof kvp->gpm); 252 } else { 253 mib[0] = CTL_KERN; 254 mib[1] = KERN_PROF; 255 mib[2] = GPROF_GMONPARAM; 256 mib[3] = cpuid; 257 size = sizeof kvp->gpm; 258 if (sysctl(mib, 4, &kvp->gpm, &size, NULL, 0) == -1) 259 size = 0; 260 } 261 if (size != sizeof kvp->gpm) 262 errx(4, "cannot get gmonparam: %s", 263 kflag ? kvm_geterr(kvp->kd) : strerror(errno)); 264 return (kvp->gpm.state); 265 } 266 267 /* 268 * Enable or disable kernel profiling according to the state variable. 269 */ 270 void 271 setprof(struct kvmvars *kvp, int cpuid, int state) 272 { 273 struct gmonparam *p = (struct gmonparam *)nl[N_GMONPARAM].n_value; 274 int mib[4], oldstate; 275 size_t sz; 276 277 sz = sizeof(state); 278 if (!kflag) { 279 mib[0] = CTL_KERN; 280 mib[1] = KERN_PROF; 281 mib[2] = GPROF_STATE; 282 mib[3] = cpuid; 283 if (sysctl(mib, 4, &oldstate, &sz, NULL, 0) == -1) 284 goto bad; 285 if (oldstate == state) 286 return; 287 if (sysctl(mib, 4, NULL, NULL, &state, sz) >= 0) 288 return; 289 } else if (kvm_write(kvp->kd, (u_long)&p->state, (void *)&state, sz) 290 == sz) 291 return; 292 bad: 293 warnx("warning: cannot turn profiling %s", 294 state == GMON_PROF_OFF ? "off" : "on"); 295 } 296 297 /* 298 * Build the gmon.out file. 299 */ 300 void 301 dumpstate(struct kvmvars *kvp, int cpuid) 302 { 303 FILE *fp; 304 struct rawarc rawarc; 305 struct tostruct *tos; 306 u_long frompc; 307 u_short *froms, *tickbuf; 308 int mib[4]; 309 size_t i; 310 struct gmonhdr h; 311 int fromindex, endfrom, toindex; 312 char buf[16]; 313 314 snprintf(buf, sizeof(buf), "gmon-%02d.out", cpuid); 315 316 setprof(kvp, cpuid, GMON_PROF_OFF); 317 fp = fopen(buf, "w"); 318 if (fp == 0) { 319 perror(buf); 320 return; 321 } 322 323 /* 324 * Build the gmon header and write it to a file. 325 */ 326 bzero(&h, sizeof(h)); 327 h.lpc = kvp->gpm.lowpc; 328 h.hpc = kvp->gpm.highpc; 329 h.ncnt = kvp->gpm.kcountsize + sizeof(h); 330 h.version = GMONVERSION; 331 h.profrate = getprofhz(kvp); 332 fwrite((char *)&h, sizeof(h), 1, fp); 333 334 /* 335 * Write out the tick buffer. 336 */ 337 mib[0] = CTL_KERN; 338 mib[1] = KERN_PROF; 339 if ((tickbuf = malloc(kvp->gpm.kcountsize)) == NULL) 340 errx(5, "cannot allocate kcount space"); 341 if (kflag) { 342 i = kvm_read(kvp->kd, (u_long)kvp->gpm.kcount, (void *)tickbuf, 343 kvp->gpm.kcountsize); 344 } else { 345 mib[2] = GPROF_COUNT; 346 mib[3] = cpuid; 347 i = kvp->gpm.kcountsize; 348 if (sysctl(mib, 4, tickbuf, &i, NULL, 0) == -1) 349 i = 0; 350 } 351 if (i != kvp->gpm.kcountsize) 352 errx(6, "read ticks: read %lu, got %zu: %s", 353 kvp->gpm.kcountsize, i, 354 kflag ? kvm_geterr(kvp->kd) : strerror(errno)); 355 if ((fwrite(tickbuf, kvp->gpm.kcountsize, 1, fp)) != 1) 356 err(7, "writing tocks to gmon.out"); 357 free(tickbuf); 358 359 /* 360 * Write out the arc info. 361 */ 362 if ((froms = malloc(kvp->gpm.fromssize)) == NULL) 363 errx(8, "cannot allocate froms space"); 364 if (kflag) { 365 i = kvm_read(kvp->kd, (u_long)kvp->gpm.froms, (void *)froms, 366 kvp->gpm.fromssize); 367 } else { 368 mib[2] = GPROF_FROMS; 369 mib[3] = cpuid; 370 i = kvp->gpm.fromssize; 371 if (sysctl(mib, 4, froms, &i, NULL, 0) == -1) 372 i = 0; 373 } 374 if (i != kvp->gpm.fromssize) 375 errx(9, "read froms: read %lu, got %zu: %s", 376 kvp->gpm.fromssize, i, 377 kflag ? kvm_geterr(kvp->kd) : strerror(errno)); 378 if ((tos = malloc(kvp->gpm.tossize)) == NULL) 379 errx(10, "cannot allocate tos space"); 380 if (kflag) { 381 i = kvm_read(kvp->kd, (u_long)kvp->gpm.tos, (void *)tos, 382 kvp->gpm.tossize); 383 } else { 384 mib[2] = GPROF_TOS; 385 mib[3] = cpuid; 386 i = kvp->gpm.tossize; 387 if (sysctl(mib, 4, tos, &i, NULL, 0) == -1) 388 i = 0; 389 } 390 if (i != kvp->gpm.tossize) 391 errx(11, "read tos: read %lu, got %zu: %s", 392 kvp->gpm.tossize, i, 393 kflag ? kvm_geterr(kvp->kd) : strerror(errno)); 394 if (debug) 395 warnx("lowpc 0x%lx, textsize 0x%lx", 396 kvp->gpm.lowpc, kvp->gpm.textsize); 397 endfrom = kvp->gpm.fromssize / sizeof(*froms); 398 for (fromindex = 0; fromindex < endfrom; ++fromindex) { 399 if (froms[fromindex] == 0) 400 continue; 401 frompc = (u_long)kvp->gpm.lowpc + 402 (fromindex * kvp->gpm.hashfraction * sizeof(*froms)); 403 for (toindex = froms[fromindex]; toindex != 0; 404 toindex = tos[toindex].link) { 405 if (debug) 406 warnx("[mcleanup] frompc 0x%lx selfpc 0x%lx count %ld", 407 frompc, tos[toindex].selfpc, tos[toindex].count); 408 rawarc.raw_frompc = frompc; 409 rawarc.raw_selfpc = (u_long)tos[toindex].selfpc; 410 rawarc.raw_count = tos[toindex].count; 411 fwrite((char *)&rawarc, sizeof(rawarc), 1, fp); 412 } 413 } 414 fclose(fp); 415 } 416 417 /* 418 * Get the profiling rate. 419 */ 420 int 421 getprofhz(struct kvmvars *kvp) 422 { 423 int mib[2], profrate; 424 size_t size; 425 struct clockinfo clockrate; 426 427 if (kflag) { 428 profrate = 1; 429 if (kvm_read(kvp->kd, nl[N_PROFHZ].n_value, &profrate, 430 sizeof profrate) != sizeof profrate) 431 warnx("get clockrate: %s", kvm_geterr(kvp->kd)); 432 return (profrate); 433 } 434 mib[0] = CTL_KERN; 435 mib[1] = KERN_CLOCKRATE; 436 clockrate.profhz = 1; 437 size = sizeof clockrate; 438 if (sysctl(mib, 2, &clockrate, &size, NULL, 0) == -1) 439 warn("get clockrate"); 440 return (clockrate.profhz); 441 } 442 443 /* 444 * Reset the kernel profiling date structures. 445 */ 446 void 447 reset(struct kvmvars *kvp, int cpuid) 448 { 449 char *zbuf; 450 u_long biggest; 451 int mib[4]; 452 453 setprof(kvp, cpuid, GMON_PROF_OFF); 454 455 biggest = kvp->gpm.kcountsize; 456 if (kvp->gpm.fromssize > biggest) 457 biggest = kvp->gpm.fromssize; 458 if (kvp->gpm.tossize > biggest) 459 biggest = kvp->gpm.tossize; 460 if ((zbuf = malloc(biggest)) == NULL) 461 errx(12, "cannot allocate zbuf space"); 462 bzero(zbuf, biggest); 463 if (kflag) { 464 if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf, 465 kvp->gpm.kcountsize) != kvp->gpm.kcountsize) 466 errx(13, "tickbuf zero: %s", kvm_geterr(kvp->kd)); 467 if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf, 468 kvp->gpm.fromssize) != kvp->gpm.fromssize) 469 errx(14, "froms zero: %s", kvm_geterr(kvp->kd)); 470 if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf, 471 kvp->gpm.tossize) != kvp->gpm.tossize) 472 errx(15, "tos zero: %s", kvm_geterr(kvp->kd)); 473 return; 474 } 475 mib[0] = CTL_KERN; 476 mib[1] = KERN_PROF; 477 mib[2] = GPROF_COUNT; 478 mib[3] = cpuid; 479 if (sysctl(mib, 4, NULL, NULL, zbuf, kvp->gpm.kcountsize) == -1) 480 err(13, "tickbuf zero"); 481 mib[2] = GPROF_FROMS; 482 if (sysctl(mib, 4, NULL, NULL, zbuf, kvp->gpm.fromssize) == -1) 483 err(14, "froms zero"); 484 mib[2] = GPROF_TOS; 485 if (sysctl(mib, 4, NULL, NULL, zbuf, kvp->gpm.tossize) == -1) 486 err(15, "tos zero"); 487 free(zbuf); 488 } 489 490 int 491 getncpu(void) 492 { 493 int mib[2] = { CTL_HW, HW_NCPU }; 494 size_t size; 495 int ncpu; 496 497 size = sizeof(ncpu); 498 if (sysctl(mib, 2, &ncpu, &size, NULL, 0) == -1) { 499 warnx("cannot read hw.ncpu"); 500 return (1); 501 } 502 503 return (ncpu); 504 } 505