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