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 <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) /* interruptable (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 SWAIT: 200 *cp = 'W'; 201 break; 202 203 case SMTX: 204 *cp = 'M'; 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 *cp++ = 'W'; 217 if (p->p_nice < NZERO) 218 *cp++ = '<'; 219 else if (p->p_nice > NZERO) 220 *cp++ = 'N'; 221 if (flag & P_TRACED) 222 *cp++ = 'X'; 223 if (flag & P_WEXIT && p->p_stat != SZOMB) 224 *cp++ = 'E'; 225 if (flag & P_PPWAIT) 226 *cp++ = 'V'; 227 if ((flag & P_SYSTEM) || p->p_lock > 0) 228 *cp++ = 'L'; 229 if (KI_EPROC(k)->e_flag & EPROC_SLEADER) 230 *cp++ = 's'; 231 if ((flag & P_CONTROLT) && KI_EPROC(k)->e_pgid == KI_EPROC(k)->e_tpgid) 232 *cp++ = '+'; 233 if (flag & P_JAILED) 234 *cp++ = 'J'; 235 *cp = '\0'; 236 (void)printf("%-*s", v->width, buf); 237 } 238 239 void 240 pri(k, ve) 241 KINFO *k; 242 VARENT *ve; 243 { 244 VAR *v; 245 246 v = ve->var; 247 (void)printf("%*d", v->width, KI_PROC(k)->p_priority - PZERO); 248 } 249 250 void 251 uname(k, ve) 252 KINFO *k; 253 VARENT *ve; 254 { 255 VAR *v; 256 257 v = ve->var; 258 (void)printf("%-*s", 259 (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0)); 260 } 261 262 int 263 s_uname(k) 264 KINFO *k; 265 { 266 return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0))); 267 } 268 269 void 270 runame(k, ve) 271 KINFO *k; 272 VARENT *ve; 273 { 274 VAR *v; 275 276 v = ve->var; 277 (void)printf("%-*s", 278 (int)v->width, user_from_uid(KI_EPROC(k)->e_pcred.p_ruid, 0)); 279 } 280 281 int 282 s_runame(k) 283 KINFO *k; 284 { 285 return (strlen(user_from_uid(KI_EPROC(k)->e_pcred.p_ruid, 0))); 286 } 287 288 void 289 tdev(k, ve) 290 KINFO *k; 291 VARENT *ve; 292 { 293 VAR *v; 294 dev_t dev; 295 char buff[16]; 296 297 v = ve->var; 298 dev = KI_EPROC(k)->e_tdev; 299 if (dev == NODEV) 300 (void)printf("%*s", v->width, "??"); 301 else { 302 (void)snprintf(buff, sizeof(buff), 303 "%d/%d", major(dev), minor(dev)); 304 (void)printf("%*s", v->width, buff); 305 } 306 } 307 308 void 309 tname(k, ve) 310 KINFO *k; 311 VARENT *ve; 312 { 313 VAR *v; 314 dev_t dev; 315 char *ttname; 316 317 v = ve->var; 318 dev = KI_EPROC(k)->e_tdev; 319 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) 320 (void)printf("%*s ", v->width-1, "??"); 321 else { 322 if (strncmp(ttname, "tty", 3) == 0 || 323 strncmp(ttname, "cua", 3) == 0) 324 ttname += 3; 325 (void)printf("%*.*s%c", v->width-1, v->width-1, ttname, 326 KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-'); 327 } 328 } 329 330 void 331 longtname(k, ve) 332 KINFO *k; 333 VARENT *ve; 334 { 335 VAR *v; 336 dev_t dev; 337 char *ttname; 338 339 v = ve->var; 340 dev = KI_EPROC(k)->e_tdev; 341 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) 342 (void)printf("%-*s", v->width, "??"); 343 else 344 (void)printf("%-*s", v->width, ttname); 345 } 346 347 void 348 started(k, ve) 349 KINFO *k; 350 VARENT *ve; 351 { 352 VAR *v; 353 static time_t now; 354 time_t then; 355 struct tm *tp; 356 char buf[100]; 357 358 v = ve->var; 359 if (!k->ki_u.u_valid) { 360 (void)printf("%-*s", v->width, "-"); 361 return; 362 } 363 364 then = k->ki_u.u_start.tv_sec; 365 tp = localtime(&then); 366 if (!now) 367 (void)time(&now); 368 if (now - k->ki_u.u_start.tv_sec < 24 * 3600) { 369 (void)strftime(buf, sizeof(buf) - 1, "%l:%M%p", tp); 370 } else if (now - k->ki_u.u_start.tv_sec < 7 * 86400) { 371 (void)strftime(buf, sizeof(buf) - 1, "%a%I%p", tp); 372 } else 373 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp); 374 (void)printf("%-*s", v->width, buf); 375 } 376 377 void 378 lstarted(k, ve) 379 KINFO *k; 380 VARENT *ve; 381 { 382 VAR *v; 383 time_t then; 384 char buf[100]; 385 386 v = ve->var; 387 if (!k->ki_u.u_valid) { 388 (void)printf("%-*s", v->width, "-"); 389 return; 390 } 391 then = k->ki_u.u_start.tv_sec; 392 (void)strftime(buf, sizeof(buf) -1, "%c", localtime(&then)); 393 (void)printf("%-*s", v->width, buf); 394 } 395 396 void 397 wchan(k, ve) 398 KINFO *k; 399 VARENT *ve; 400 { 401 VAR *v; 402 403 v = ve->var; 404 if (KI_PROC(k)->p_wchan) { 405 if (KI_PROC(k)->p_wmesg) 406 (void)printf("%-*.*s", v->width, v->width, 407 KI_EPROC(k)->e_wmesg); 408 else 409 (void)printf("%-*lx", v->width, 410 (long)KI_PROC(k)->p_wchan &~ KERNBASE); 411 } else 412 (void)printf("%-*s", v->width, "-"); 413 } 414 415 #ifndef pgtok 416 #define pgtok(a) (((a)*getpagesize())/1024) 417 #endif 418 419 void 420 vsize(k, ve) 421 KINFO *k; 422 VARENT *ve; 423 { 424 VAR *v; 425 426 v = ve->var; 427 (void)printf("%*d", v->width, 428 (KI_EPROC(k)->e_vm.vm_map.size/1024)); 429 } 430 431 void 432 rssize(k, ve) 433 KINFO *k; 434 VARENT *ve; 435 { 436 VAR *v; 437 438 v = ve->var; 439 /* XXX don't have info about shared */ 440 (void)printf("%*lu", v->width, 441 (u_long)pgtok(KI_EPROC(k)->e_vm.vm_rssize)); 442 } 443 444 void 445 p_rssize(k, ve) /* doesn't account for text */ 446 KINFO *k; 447 VARENT *ve; 448 { 449 VAR *v; 450 451 v = ve->var; 452 (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_rssize)); 453 } 454 455 void 456 cputime(k, ve) 457 KINFO *k; 458 VARENT *ve; 459 { 460 VAR *v; 461 long secs; 462 long psecs; /* "parts" of a second. first micro, then centi */ 463 char obuff[128]; 464 465 v = ve->var; 466 if (KI_PROC(k)->p_stat == SZOMB || !k->ki_u.u_valid) { 467 secs = 0; 468 psecs = 0; 469 } else { 470 /* 471 * This counts time spent handling interrupts. We could 472 * fix this, but it is not 100% trivial (and interrupt 473 * time fractions only work on the sparc anyway). XXX 474 */ 475 secs = KI_PROC(k)->p_runtime / 1000000; 476 psecs = KI_PROC(k)->p_runtime % 1000000; 477 if (sumrusage) { 478 secs += k->ki_u.u_cru.ru_utime.tv_sec + 479 k->ki_u.u_cru.ru_stime.tv_sec; 480 psecs += k->ki_u.u_cru.ru_utime.tv_usec + 481 k->ki_u.u_cru.ru_stime.tv_usec; 482 } 483 /* 484 * round and scale to 100's 485 */ 486 psecs = (psecs + 5000) / 10000; 487 secs += psecs / 100; 488 psecs = psecs % 100; 489 } 490 (void)snprintf(obuff, sizeof(obuff), 491 "%3ld:%02ld.%02ld", secs/60, secs%60, psecs); 492 (void)printf("%*s", v->width, obuff); 493 } 494 495 double 496 getpcpu(k) 497 KINFO *k; 498 { 499 struct proc *p; 500 static int failure; 501 502 if (!nlistread) 503 failure = donlist(); 504 if (failure) 505 return (0.0); 506 507 p = KI_PROC(k); 508 #define fxtofl(fixpt) ((double)(fixpt) / fscale) 509 510 /* XXX - I don't like this */ 511 if (p->p_swtime == 0 || (p->p_flag & P_INMEM) == 0) 512 return (0.0); 513 if (rawcpu) 514 return (100.0 * fxtofl(p->p_pctcpu)); 515 return (100.0 * fxtofl(p->p_pctcpu) / 516 (1.0 - exp(p->p_swtime * log(fxtofl(ccpu))))); 517 } 518 519 void 520 pcpu(k, ve) 521 KINFO *k; 522 VARENT *ve; 523 { 524 VAR *v; 525 526 v = ve->var; 527 (void)printf("%*.1f", v->width, getpcpu(k)); 528 } 529 530 double 531 getpmem(k) 532 KINFO *k; 533 { 534 static int failure; 535 struct proc *p; 536 struct eproc *e; 537 double fracmem; 538 int szptudot; 539 540 if (!nlistread) 541 failure = donlist(); 542 if (failure) 543 return (0.0); 544 545 p = KI_PROC(k); 546 e = KI_EPROC(k); 547 if ((p->p_flag & P_INMEM) == 0) 548 return (0.0); 549 /* XXX want pmap ptpages, segtab, etc. (per architecture) */ 550 szptudot = UPAGES; 551 /* XXX don't have info about shared */ 552 fracmem = ((float)e->e_vm.vm_rssize + szptudot)/mempages; 553 return (100.0 * fracmem); 554 } 555 556 void 557 pmem(k, ve) 558 KINFO *k; 559 VARENT *ve; 560 { 561 VAR *v; 562 563 v = ve->var; 564 (void)printf("%*.1f", v->width, getpmem(k)); 565 } 566 567 void 568 pagein(k, ve) 569 KINFO *k; 570 VARENT *ve; 571 { 572 VAR *v; 573 574 v = ve->var; 575 (void)printf("%*ld", v->width, 576 k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0); 577 } 578 579 void 580 maxrss(k, ve) 581 KINFO *k; 582 VARENT *ve; 583 { 584 VAR *v; 585 586 v = ve->var; 587 /* XXX not yet */ 588 (void)printf("%*s", v->width, "-"); 589 } 590 591 void 592 tsize(k, ve) 593 KINFO *k; 594 VARENT *ve; 595 { 596 VAR *v; 597 598 v = ve->var; 599 (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_tsize)); 600 } 601 602 void 603 rtprior(k, ve) 604 KINFO *k; 605 VARENT *ve; 606 { 607 VAR *v; 608 struct rtprio *prtp; 609 char str[8]; 610 unsigned prio, type; 611 612 v = ve->var; 613 prtp = (struct rtprio *) ((char *)KI_PROC(k) + v->off); 614 prio = prtp->prio; 615 type = prtp->type; 616 switch (type) { 617 case RTP_PRIO_REALTIME: 618 snprintf(str, sizeof(str), "real:%u", prio); 619 break; 620 case RTP_PRIO_NORMAL: 621 strncpy(str, "normal", sizeof(str)); 622 break; 623 case RTP_PRIO_IDLE: 624 snprintf(str, sizeof(str), "idle:%u", prio); 625 break; 626 default: 627 snprintf(str, sizeof(str), "%u:%u", type, prio); 628 break; 629 } 630 str[sizeof(str) - 1] = '\0'; 631 (void)printf("%*s", v->width, str); 632 } 633 634 /* 635 * Generic output routines. Print fields from various prototype 636 * structures. 637 */ 638 static void 639 printval(bp, v) 640 char *bp; 641 VAR *v; 642 { 643 static char ofmt[32] = "%"; 644 char *fcp, *cp; 645 646 cp = ofmt + 1; 647 fcp = v->fmt; 648 if (v->flag & LJUST) 649 *cp++ = '-'; 650 *cp++ = '*'; 651 while ((*cp++ = *fcp++)); 652 653 switch (v->type) { 654 case CHAR: 655 (void)printf(ofmt, v->width, *(char *)bp); 656 break; 657 case UCHAR: 658 (void)printf(ofmt, v->width, *(u_char *)bp); 659 break; 660 case SHORT: 661 (void)printf(ofmt, v->width, *(short *)bp); 662 break; 663 case USHORT: 664 (void)printf(ofmt, v->width, *(u_short *)bp); 665 break; 666 case INT: 667 (void)printf(ofmt, v->width, *(int *)bp); 668 break; 669 case UINT: 670 (void)printf(ofmt, v->width, *(u_int *)bp); 671 break; 672 case LONG: 673 (void)printf(ofmt, v->width, *(long *)bp); 674 break; 675 case ULONG: 676 (void)printf(ofmt, v->width, *(u_long *)bp); 677 break; 678 case KPTR: 679 (void)printf(ofmt, v->width, *(u_long *)bp &~ KERNBASE); 680 break; 681 default: 682 errx(1, "unknown type %d", v->type); 683 } 684 } 685 686 void 687 pvar(k, ve) 688 KINFO *k; 689 VARENT *ve; 690 { 691 VAR *v; 692 693 v = ve->var; 694 printval((char *)((char *)KI_PROC(k) + v->off), v); 695 } 696 697 void 698 evar(k, ve) 699 KINFO *k; 700 VARENT *ve; 701 { 702 VAR *v; 703 704 v = ve->var; 705 printval((char *)((char *)KI_EPROC(k) + v->off), v); 706 } 707 708 void 709 uvar(k, ve) 710 KINFO *k; 711 VARENT *ve; 712 { 713 VAR *v; 714 715 v = ve->var; 716 if (k->ki_u.u_valid) 717 printval((char *)((char *)&k->ki_u + v->off), v); 718 else 719 (void)printf("%*s", v->width, "-"); 720 } 721 722 void 723 rvar(k, ve) 724 KINFO *k; 725 VARENT *ve; 726 { 727 VAR *v; 728 729 v = ve->var; 730 if (k->ki_u.u_valid) 731 printval((char *)((char *)(&k->ki_u.u_ru) + v->off), v); 732 else 733 (void)printf("%*s", v->width, "-"); 734 } 735