1 /*- 2 * Copyright (c) 1990, 1993, 1994 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 35 #if 0 36 static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94"; 37 #endif 38 static const char rcsid[] = 39 "$FreeBSD$"; 40 #endif /* not lint */ 41 42 #include <sys/param.h> 43 #include <sys/time.h> 44 #include <sys/resource.h> 45 #include <sys/proc.h> 46 #include <sys/stat.h> 47 48 #include <sys/ucred.h> 49 #include <sys/user.h> 50 #include <sys/sysctl.h> 51 #include <vm/vm.h> 52 53 #include <err.h> 54 #include <langinfo.h> 55 #include <locale.h> 56 #include <math.h> 57 #include <nlist.h> 58 #include <stddef.h> 59 #include <stdio.h> 60 #include <stdlib.h> 61 #include <unistd.h> 62 #include <string.h> 63 #include <vis.h> 64 65 #include "ps.h" 66 67 void 68 printheader() 69 { 70 VAR *v; 71 struct varent *vent; 72 73 for (vent = vhead; vent; vent = vent->next) { 74 v = vent->var; 75 if (v->flag & LJUST) { 76 if (vent->next == NULL) /* last one */ 77 (void)printf("%s", v->header); 78 else 79 (void)printf("%-*s", v->width, v->header); 80 } else 81 (void)printf("%*s", v->width, v->header); 82 if (vent->next != NULL) 83 (void)putchar(' '); 84 } 85 (void)putchar('\n'); 86 } 87 88 void 89 command(k, ve) 90 KINFO *k; 91 VARENT *ve; 92 { 93 VAR *v; 94 int left; 95 char *cp, *vis_env, *vis_args; 96 97 v = ve->var; 98 99 if (cflag) { 100 if (ve->next == NULL) /* last field, don't pad */ 101 (void)printf("%s", k->ki_p->ki_comm); 102 else 103 (void)printf("%-*s", v->width, k->ki_p->ki_comm); 104 return; 105 } 106 107 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL) 108 err(1, NULL); 109 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH); 110 if (k->ki_env) { 111 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL) 112 err(1, NULL); 113 strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH); 114 } else 115 vis_env = NULL; 116 117 if (ve->next == NULL) { 118 /* last field */ 119 if (termwidth == UNLIMITED) { 120 if (vis_env) 121 (void)printf("%s ", vis_env); 122 (void)printf("%s", vis_args); 123 } else { 124 left = termwidth - (totwidth - v->width); 125 if (left < 1) /* already wrapped, just use std width */ 126 left = v->width; 127 if ((cp = vis_env) != NULL) { 128 while (--left >= 0 && *cp) 129 (void)putchar(*cp++); 130 if (--left >= 0) 131 putchar(' '); 132 } 133 for (cp = vis_args; --left >= 0 && *cp != '\0';) 134 (void)putchar(*cp++); 135 } 136 } else 137 /* XXX env? */ 138 (void)printf("%-*.*s", v->width, v->width, vis_args); 139 free(vis_args); 140 if (vis_env != NULL) 141 free(vis_env); 142 } 143 144 void 145 ucomm(k, ve) 146 KINFO *k; 147 VARENT *ve; 148 { 149 VAR *v; 150 151 v = ve->var; 152 (void)printf("%-*s", v->width, k->ki_p->ki_comm); 153 } 154 155 void 156 logname(k, ve) 157 KINFO *k; 158 VARENT *ve; 159 { 160 VAR *v; 161 char *s; 162 163 v = ve->var; 164 (void)printf("%-*s", v->width, (s = k->ki_p->ki_login, *s) ? s : "-"); 165 } 166 167 void 168 state(k, ve) 169 KINFO *k; 170 VARENT *ve; 171 { 172 int flag, sflag; 173 char *cp; 174 VAR *v; 175 char buf[16]; 176 177 v = ve->var; 178 flag = k->ki_p->ki_flag; 179 sflag = k->ki_p->ki_sflag; 180 cp = buf; 181 182 switch (k->ki_p->ki_stat) { 183 184 case SSTOP: 185 *cp = 'T'; 186 break; 187 188 case SSLEEP: 189 if (sflag & PS_SINTR) /* interruptable (long) */ 190 *cp = k->ki_p->ki_slptime >= MAXSLP ? 'I' : 'S'; 191 else 192 *cp = 'D'; 193 break; 194 195 case SRUN: 196 case SIDL: 197 *cp = 'R'; 198 break; 199 200 case SWAIT: 201 *cp = 'W'; 202 break; 203 204 case SMTX: 205 *cp = 'M'; 206 break; 207 208 case SZOMB: 209 *cp = 'Z'; 210 break; 211 212 default: 213 *cp = '?'; 214 } 215 cp++; 216 if (!(sflag & PS_INMEM)) 217 *cp++ = 'W'; 218 if (k->ki_p->ki_nice < NZERO) 219 *cp++ = '<'; 220 else if (k->ki_p->ki_nice > NZERO) 221 *cp++ = 'N'; 222 if (flag & P_TRACED) 223 *cp++ = 'X'; 224 if (flag & P_WEXIT && k->ki_p->ki_stat != SZOMB) 225 *cp++ = 'E'; 226 if (flag & P_PPWAIT) 227 *cp++ = 'V'; 228 if ((flag & P_SYSTEM) || k->ki_p->ki_lock > 0) 229 *cp++ = 'L'; 230 if (k->ki_p->ki_kiflag & KI_SLEADER) 231 *cp++ = 's'; 232 if ((flag & P_CONTROLT) && k->ki_p->ki_pgid == k->ki_p->ki_tpgid) 233 *cp++ = '+'; 234 if (flag & P_JAILED) 235 *cp++ = 'J'; 236 *cp = '\0'; 237 (void)printf("%-*s", v->width, buf); 238 } 239 240 void 241 pri(k, ve) 242 KINFO *k; 243 VARENT *ve; 244 { 245 VAR *v; 246 247 v = ve->var; 248 (void)printf("%*d", v->width, k->ki_p->ki_pri.pri_level - PZERO); 249 } 250 251 void 252 uname(k, ve) 253 KINFO *k; 254 VARENT *ve; 255 { 256 VAR *v; 257 258 v = ve->var; 259 (void)printf("%-*s", 260 (int)v->width, user_from_uid(k->ki_p->ki_uid, 0)); 261 } 262 263 int 264 s_uname(k) 265 KINFO *k; 266 { 267 return (strlen(user_from_uid(k->ki_p->ki_uid, 0))); 268 } 269 270 void 271 runame(k, ve) 272 KINFO *k; 273 VARENT *ve; 274 { 275 VAR *v; 276 277 v = ve->var; 278 (void)printf("%-*s", 279 (int)v->width, user_from_uid(k->ki_p->ki_ruid, 0)); 280 } 281 282 int 283 s_runame(k) 284 KINFO *k; 285 { 286 return (strlen(user_from_uid(k->ki_p->ki_ruid, 0))); 287 } 288 289 void 290 tdev(k, ve) 291 KINFO *k; 292 VARENT *ve; 293 { 294 VAR *v; 295 dev_t dev; 296 char buff[16]; 297 298 v = ve->var; 299 dev = k->ki_p->ki_tdev; 300 if (dev == NODEV) 301 (void)printf("%*s", v->width, "??"); 302 else { 303 (void)snprintf(buff, sizeof(buff), 304 "%d/%d", major(dev), minor(dev)); 305 (void)printf("%*s", v->width, buff); 306 } 307 } 308 309 void 310 tname(k, ve) 311 KINFO *k; 312 VARENT *ve; 313 { 314 VAR *v; 315 dev_t dev; 316 char *ttname; 317 318 v = ve->var; 319 dev = k->ki_p->ki_tdev; 320 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) 321 (void)printf("%*s ", v->width-1, "??"); 322 else { 323 if (strncmp(ttname, "tty", 3) == 0 || 324 strncmp(ttname, "cua", 3) == 0) 325 ttname += 3; 326 (void)printf("%*.*s%c", v->width-1, v->width-1, ttname, 327 k->ki_p->ki_kiflag & KI_CTTY ? ' ' : '-'); 328 } 329 } 330 331 void 332 longtname(k, ve) 333 KINFO *k; 334 VARENT *ve; 335 { 336 VAR *v; 337 dev_t dev; 338 char *ttname; 339 340 v = ve->var; 341 dev = k->ki_p->ki_tdev; 342 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) 343 (void)printf("%-*s", v->width, "??"); 344 else 345 (void)printf("%-*s", v->width, ttname); 346 } 347 348 void 349 started(k, ve) 350 KINFO *k; 351 VARENT *ve; 352 { 353 VAR *v; 354 static time_t now; 355 time_t then; 356 struct tm *tp; 357 char buf[100]; 358 static int use_ampm = -1; 359 360 v = ve->var; 361 if (!k->ki_valid) { 362 (void)printf("%-*s", v->width, "-"); 363 return; 364 } 365 366 if (use_ampm < 0) 367 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0'); 368 369 then = k->ki_p->ki_start.tv_sec; 370 tp = localtime(&then); 371 if (!now) 372 (void)time(&now); 373 if (now - k->ki_p->ki_start.tv_sec < 24 * 3600) { 374 (void)strftime(buf, sizeof(buf) - 1, 375 use_ampm ? "%l:%M%p" : "%k:%M ", tp); 376 } else if (now - k->ki_p->ki_start.tv_sec < 7 * 86400) { 377 (void)strftime(buf, sizeof(buf) - 1, 378 use_ampm ? "%a%I%p" : "%a%H ", tp); 379 } else 380 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp); 381 (void)printf("%-*s", v->width, buf); 382 } 383 384 void 385 lstarted(k, ve) 386 KINFO *k; 387 VARENT *ve; 388 { 389 VAR *v; 390 time_t then; 391 char buf[100]; 392 393 v = ve->var; 394 if (!k->ki_valid) { 395 (void)printf("%-*s", v->width, "-"); 396 return; 397 } 398 then = k->ki_p->ki_start.tv_sec; 399 (void)strftime(buf, sizeof(buf) -1, "%c", localtime(&then)); 400 (void)printf("%-*s", v->width, buf); 401 } 402 403 void 404 mtxname(k, ve) 405 KINFO *k; 406 VARENT *ve; 407 { 408 VAR *v; 409 410 v = ve->var; 411 if (k->ki_p->ki_kiflag & KI_MTXBLOCK) { 412 if (k->ki_p->ki_mtxname[0] != 0) 413 (void)printf("%-*.*s", v->width, v->width, 414 k->ki_p->ki_mtxname); 415 else 416 (void)printf("%-*s", v->width, "???"); 417 } else 418 (void)printf("%-*s", v->width, "-"); 419 } 420 421 void 422 wchan(k, ve) 423 KINFO *k; 424 VARENT *ve; 425 { 426 VAR *v; 427 428 v = ve->var; 429 if (k->ki_p->ki_wchan) { 430 if (k->ki_p->ki_wmesg[0] != 0) 431 (void)printf("%-*.*s", v->width, v->width, 432 k->ki_p->ki_wmesg); 433 else 434 (void)printf("%-*lx", v->width, 435 (long)k->ki_p->ki_wchan &~ KERNBASE); 436 } else 437 (void)printf("%-*s", v->width, "-"); 438 } 439 440 #ifndef pgtok 441 #define pgtok(a) (((a)*getpagesize())/1024) 442 #endif 443 444 void 445 vsize(k, ve) 446 KINFO *k; 447 VARENT *ve; 448 { 449 VAR *v; 450 451 v = ve->var; 452 (void)printf("%*d", v->width, 453 (k->ki_p->ki_size/1024)); 454 } 455 456 void 457 rssize(k, ve) 458 KINFO *k; 459 VARENT *ve; 460 { 461 VAR *v; 462 463 v = ve->var; 464 /* XXX don't have info about shared */ 465 (void)printf("%*lu", v->width, 466 (u_long)pgtok(k->ki_p->ki_rssize)); 467 } 468 469 void 470 p_rssize(k, ve) /* doesn't account for text */ 471 KINFO *k; 472 VARENT *ve; 473 { 474 VAR *v; 475 476 v = ve->var; 477 (void)printf("%*ld", v->width, (long)pgtok(k->ki_p->ki_rssize)); 478 } 479 480 void 481 cputime(k, ve) 482 KINFO *k; 483 VARENT *ve; 484 { 485 VAR *v; 486 long secs; 487 long psecs; /* "parts" of a second. first micro, then centi */ 488 char obuff[128]; 489 static char decimal_point = 0; 490 491 if (!decimal_point) 492 decimal_point = localeconv()->decimal_point[0]; 493 v = ve->var; 494 if (k->ki_p->ki_stat == SZOMB || !k->ki_valid) { 495 secs = 0; 496 psecs = 0; 497 } else { 498 /* 499 * This counts time spent handling interrupts. We could 500 * fix this, but it is not 100% trivial (and interrupt 501 * time fractions only work on the sparc anyway). XXX 502 */ 503 secs = k->ki_p->ki_runtime / 1000000; 504 psecs = k->ki_p->ki_runtime % 1000000; 505 if (sumrusage) { 506 secs += k->ki_p->ki_childtime.tv_sec; 507 psecs += k->ki_p->ki_childtime.tv_usec; 508 } 509 /* 510 * round and scale to 100's 511 */ 512 psecs = (psecs + 5000) / 10000; 513 secs += psecs / 100; 514 psecs = psecs % 100; 515 } 516 (void)snprintf(obuff, sizeof(obuff), 517 "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs); 518 (void)printf("%*s", v->width, obuff); 519 } 520 521 double 522 getpcpu(k) 523 KINFO *k; 524 { 525 static int failure; 526 527 if (!nlistread) 528 failure = donlist(); 529 if (failure) 530 return (0.0); 531 532 #define fxtofl(fixpt) ((double)(fixpt) / fscale) 533 534 /* XXX - I don't like this */ 535 if (k->ki_p->ki_swtime == 0 || (k->ki_p->ki_sflag & PS_INMEM) == 0) 536 return (0.0); 537 if (rawcpu) 538 return (100.0 * fxtofl(k->ki_p->ki_pctcpu)); 539 return (100.0 * fxtofl(k->ki_p->ki_pctcpu) / 540 (1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu))))); 541 } 542 543 void 544 pcpu(k, ve) 545 KINFO *k; 546 VARENT *ve; 547 { 548 VAR *v; 549 550 v = ve->var; 551 (void)printf("%*.1f", v->width, getpcpu(k)); 552 } 553 554 double 555 getpmem(k) 556 KINFO *k; 557 { 558 static int failure; 559 double fracmem; 560 int szptudot; 561 562 if (!nlistread) 563 failure = donlist(); 564 if (failure) 565 return (0.0); 566 567 if ((k->ki_p->ki_sflag & PS_INMEM) == 0) 568 return (0.0); 569 /* XXX want pmap ptpages, segtab, etc. (per architecture) */ 570 szptudot = UPAGES; 571 /* XXX don't have info about shared */ 572 fracmem = ((float)k->ki_p->ki_rssize + szptudot)/mempages; 573 return (100.0 * fracmem); 574 } 575 576 void 577 pmem(k, ve) 578 KINFO *k; 579 VARENT *ve; 580 { 581 VAR *v; 582 583 v = ve->var; 584 (void)printf("%*.1f", v->width, getpmem(k)); 585 } 586 587 void 588 pagein(k, ve) 589 KINFO *k; 590 VARENT *ve; 591 { 592 VAR *v; 593 594 v = ve->var; 595 (void)printf("%*ld", v->width, 596 k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0); 597 } 598 599 void 600 maxrss(k, ve) 601 KINFO *k; 602 VARENT *ve; 603 { 604 VAR *v; 605 606 v = ve->var; 607 /* XXX not yet */ 608 (void)printf("%*s", v->width, "-"); 609 } 610 611 void 612 tsize(k, ve) 613 KINFO *k; 614 VARENT *ve; 615 { 616 VAR *v; 617 618 v = ve->var; 619 (void)printf("%*ld", v->width, (long)pgtok(k->ki_p->ki_tsize)); 620 } 621 622 void 623 priorityr(k, ve) 624 KINFO *k; 625 VARENT *ve; 626 { 627 VAR *v; 628 struct priority *pri; 629 char str[8]; 630 unsigned class, level; 631 632 v = ve->var; 633 pri = (struct priority *) ((char *)k + v->off); 634 class = pri->pri_class; 635 level = pri->pri_level; 636 switch (class) { 637 case PRI_REALTIME: 638 snprintf(str, sizeof(str), "real:%u", level); 639 break; 640 case PRI_TIMESHARE: 641 strncpy(str, "normal", sizeof(str)); 642 break; 643 case PRI_IDLE: 644 snprintf(str, sizeof(str), "idle:%u", level); 645 break; 646 default: 647 snprintf(str, sizeof(str), "%u:%u", class, level); 648 break; 649 } 650 str[sizeof(str) - 1] = '\0'; 651 (void)printf("%*s", v->width, str); 652 } 653 654 /* 655 * Generic output routines. Print fields from various prototype 656 * structures. 657 */ 658 static void 659 printval(bp, v) 660 char *bp; 661 VAR *v; 662 { 663 static char ofmt[32] = "%"; 664 char *fcp, *cp; 665 666 cp = ofmt + 1; 667 fcp = v->fmt; 668 if (v->flag & LJUST) 669 *cp++ = '-'; 670 *cp++ = '*'; 671 while ((*cp++ = *fcp++)); 672 673 switch (v->type) { 674 case CHAR: 675 (void)printf(ofmt, v->width, *(char *)bp); 676 break; 677 case UCHAR: 678 (void)printf(ofmt, v->width, *(u_char *)bp); 679 break; 680 case SHORT: 681 (void)printf(ofmt, v->width, *(short *)bp); 682 break; 683 case USHORT: 684 (void)printf(ofmt, v->width, *(u_short *)bp); 685 break; 686 case INT: 687 (void)printf(ofmt, v->width, *(int *)bp); 688 break; 689 case UINT: 690 (void)printf(ofmt, v->width, *(u_int *)bp); 691 break; 692 case LONG: 693 (void)printf(ofmt, v->width, *(long *)bp); 694 break; 695 case ULONG: 696 (void)printf(ofmt, v->width, *(u_long *)bp); 697 break; 698 case KPTR: 699 (void)printf(ofmt, v->width, *(u_long *)bp &~ KERNBASE); 700 break; 701 default: 702 errx(1, "unknown type %d", v->type); 703 } 704 } 705 706 void 707 kvar(k, ve) 708 KINFO *k; 709 VARENT *ve; 710 { 711 VAR *v; 712 713 v = ve->var; 714 printval((char *)((char *)k->ki_p + v->off), v); 715 } 716 717 void 718 rvar(k, ve) 719 KINFO *k; 720 VARENT *ve; 721 { 722 VAR *v; 723 724 v = ve->var; 725 if (k->ki_valid) 726 printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v); 727 else 728 (void)printf("%*s", v->width, "-"); 729 } 730