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