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