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