int main(int argc, char **argv) { int c, todo; u_int interval; /* milliseconds */ int reps; char *memf, *nlistf; char errbuf[_POSIX2_LINE_MAX]; memf = nlistf = NULL; interval = reps = todo = 0; maxshowdevs = 2; while ((c = getopt(argc, argv, "c:fiM:mN:n:p:stvw:z")) != -1) { switch (c) { case 'c': reps = atoi(optarg); break; case 'f': #ifdef notyet todo |= FORKSTAT; #else errx(EX_USAGE, "sorry, -f is not (re)implemented yet"); #endif break; case 'i': todo |= INTRSTAT; break; case 'M': memf = optarg; break; case 'm': todo |= MEMSTAT; break; case 'N': nlistf = optarg; break; case 'n': nflag = 1; maxshowdevs = atoi(optarg); if (maxshowdevs < 0) errx(1, "number of devices %d is < 0", maxshowdevs); break; case 'p': if (buildmatch(optarg, &matches, &num_matches) != 0) errx(1, "%s", devstat_errbuf); break; case 's': todo |= SUMSTAT; break; case 't': #ifdef notyet todo |= TIMESTAT; #else errx(EX_USAGE, "sorry, -t is not (re)implemented yet"); #endif break; case 'v': ++verbose; break; case 'w': interval = (u_int)(strtod(optarg, NULL) * 1000.0); break; case 'z': todo |= ZMEMSTAT; break; default: usage(); } } argc -= optind; argv += optind; if (todo == 0) todo = VMSTAT; /* * Discard setgid privileges if not the running kernel so that bad * guys can't print interesting stuff from kernel memory. */ if (nlistf != NULL || memf != NULL) setgid(getgid()); kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); if (kd == NULL) errx(1, "kvm_openfiles: %s", errbuf); if ((c = kvm_nlist(kd, namelist)) != 0) { if (c > 0) { warnx("undefined symbols:"); for (c = 0; c < (int)__arysize(namelist); c++) if (namelist[c].n_type == 0) fprintf(stderr, " %s", namelist[c].n_name); fputc('\n', stderr); } else warnx("kvm_nlist: %s", kvm_geterr(kd)); exit(1); } if (todo & VMSTAT) { struct winsize winsize; /* * Make sure that the userland devstat version matches the * kernel devstat version. If not, exit and print a * message informing the user of his mistake. */ if (checkversion() < 0) errx(1, "%s", devstat_errbuf); argv = getdrivedata(argv); winsize.ws_row = 0; ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&winsize); if (winsize.ws_row > 0) winlines = winsize.ws_row; } #define BACKWARD_COMPATIBILITY #ifdef BACKWARD_COMPATIBILITY if (*argv) { interval = (u_int)(strtod(*argv, NULL) * 1000.0); if (*++argv) reps = atoi(*argv); } #endif if (interval) { if (!reps) reps = -1; } else if (reps) { interval = 1000; } #ifdef notyet if (todo & FORKSTAT) doforkst(); #endif if (todo & MEMSTAT) domem(); if (todo & ZMEMSTAT) dozmem(); if (todo & SUMSTAT) dosum(); #ifdef notyet if (todo & TIMESTAT) dotimes(); #endif if (todo & INTRSTAT) dointr(); if (todo & VMSTAT) dovmstat(interval, reps); exit(0); }
int main(int argc, char *argv[]) { int c, todo; unsigned int interval; float f; int reps; char *memf, *nlistf; char errbuf[_POSIX2_LINE_MAX]; memf = nlistf = NULL; interval = reps = todo = 0; maxshowdevs = 2; hflag = isatty(1); while ((c = getopt(argc, argv, "ac:fhHiM:mN:n:Pp:stw:z")) != -1) { switch (c) { case 'a': aflag++; break; case 'c': reps = atoi(optarg); break; case 'P': Pflag++; break; case 'f': todo |= FORKSTAT; break; case 'h': hflag = 1; break; case 'H': hflag = 0; break; case 'i': todo |= INTRSTAT; break; case 'M': memf = optarg; break; case 'm': todo |= MEMSTAT; break; case 'N': nlistf = optarg; break; case 'n': nflag = 1; maxshowdevs = atoi(optarg); if (maxshowdevs < 0) errx(1, "number of devices %d is < 0", maxshowdevs); break; case 'p': if (devstat_buildmatch(optarg, &matches, &num_matches) != 0) errx(1, "%s", devstat_errbuf); break; case 's': todo |= SUMSTAT; break; case 't': #ifdef notyet todo |= TIMESTAT; #else errx(EX_USAGE, "sorry, -t is not (re)implemented yet"); #endif break; case 'w': /* Convert to milliseconds. */ f = atof(optarg); interval = f * 1000; break; case 'z': todo |= ZMEMSTAT; break; case '?': default: usage(); } } argc -= optind; argv += optind; if (todo == 0) todo = VMSTAT; if (memf != NULL) { kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); if (kd == NULL) errx(1, "kvm_openfiles: %s", errbuf); } retry_nlist: if (kd != NULL && (c = kvm_nlist(kd, namelist)) != 0) { if (c > 0) { /* * 'cnt' was renamed to 'vm_cnt'. If 'vm_cnt' is not * found try looking up older 'cnt' symbol. * */ if (namelist[X_SUM].n_type == 0 && strcmp(namelist[X_SUM].n_name, "_vm_cnt") == 0) { namelist[X_SUM].n_name = "_cnt"; goto retry_nlist; } warnx("undefined symbols:"); for (c = 0; c < (int)(sizeof(namelist)/sizeof(namelist[0])); c++) if (namelist[c].n_type == 0) (void)fprintf(stderr, " %s", namelist[c].n_name); (void)fputc('\n', stderr); } else warnx("kvm_nlist: %s", kvm_geterr(kd)); exit(1); } if (kd && Pflag) errx(1, "Cannot use -P with crash dumps"); if (todo & VMSTAT) { /* * Make sure that the userland devstat version matches the * kernel devstat version. If not, exit and print a * message informing the user of his mistake. */ if (devstat_checkversion(NULL) < 0) errx(1, "%s", devstat_errbuf); argv = getdrivedata(argv); } if (*argv) { f = atof(*argv); interval = f * 1000; if (*++argv) reps = atoi(*argv); } if (interval) { if (!reps) reps = -1; } else if (reps) interval = 1 * 1000; if (todo & FORKSTAT) doforkst(); if (todo & MEMSTAT) domemstat_malloc(); if (todo & ZMEMSTAT) domemstat_zone(); if (todo & SUMSTAT) dosum(); #ifdef notyet if (todo & TIMESTAT) dotimes(); #endif if (todo & INTRSTAT) dointr(interval, reps); if (todo & VMSTAT) dovmstat(interval, reps); exit(0); }
int main(int argc, char *argv[]) { char errbuf[_POSIX2_LINE_MAX]; int c, todo = 0, reps = 0; const char *errstr; u_int interval = 0; while ((c = getopt(argc, argv, "c:fiM:mN:stw:vz")) != -1) { switch (c) { case 'c': reps = atoi(optarg); break; case 'f': todo |= FORKSTAT; break; case 'i': todo |= INTRSTAT; break; case 'M': memf = optarg; break; case 'm': todo |= MEMSTAT; break; case 'N': nlistf = optarg; break; case 's': todo |= SUMSTAT; break; case 't': todo |= TIMESTAT; break; case 'w': interval = (u_int)strtonum(optarg, 0, 1000, &errstr); if (errstr) errx(1, "-w %s: %s", optarg, errstr); break; case 'v': verbose = 1; break; case 'z': zflag = 1; break; case '?': default: usage(); } } argc -= optind; argv += optind; if (todo == 0) todo = VMSTAT; if (nlistf != NULL || memf != NULL) { kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); if (kd == 0) errx(1, "kvm_openfiles: %s", errbuf); if ((c = kvm_nlist(kd, namelist)) != 0) { if (c > 0) { (void)fprintf(stderr, "%s: undefined symbols:", __progname); for (c = 0; c < sizeof(namelist)/sizeof(namelist[0]); c++) if (namelist[c].n_type == 0) fprintf(stderr, " %s", namelist[c].n_name); (void)fputc('\n', stderr); exit(1); } else errx(1, "kvm_nlist: %s", kvm_geterr(kd)); } } if (todo & VMSTAT) { struct winsize winsize; dkinit(0); /* Initialize disk stats, no disks selected. */ argv = choosedrives(argv); /* Select disks. */ winsize.ws_row = 0; (void) ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize); if (winsize.ws_row > 0) winlines = winsize.ws_row; } #define BACKWARD_COMPATIBILITY #ifdef BACKWARD_COMPATIBILITY if (*argv) { interval = (u_int)strtonum(*argv, 0, 1000, &errstr); if (errstr) errx(1, "%s: %s", *argv, errstr); if (*++argv) reps = atoi(*argv); } #endif if (interval) { if (!reps) reps = -1; } else if (reps) interval = 1; if (todo & FORKSTAT) doforkst(); if (todo & MEMSTAT) { domem(); dopool(); } if (todo & SUMSTAT) dosum(); if (todo & TIMESTAT) dotimes(); if (todo & INTRSTAT) dointr(); if (todo & VMSTAT) dovmstat(interval, reps); exit(0); }