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