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