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.24 2005/11/14 18:49:48 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_SWAPPEDOUT) 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 then = k->ki_u.u_start.tv_sec; 353 if (then < btime.tv_sec) { 354 then = btime.tv_sec; 355 } 356 357 tp = localtime(&then); 358 if (!now) 359 time(&now); 360 if (now - then < 24 * 3600) { 361 strftime(buf, sizeof(buf) - 1, 362 use_ampm ? "%l:%M%p" : "%k:%M ", tp); 363 } else if (now - then < 7 * 86400) { 364 strftime(buf, sizeof(buf) - 1, 365 use_ampm ? "%a%I%p" : "%a%H ", tp); 366 } else 367 strftime(buf, sizeof(buf) - 1, "%e%b%y", tp); 368 printf("%-*s", vent->width, buf); 369 } 370 371 void 372 lstarted(const KINFO *k, const struct varent *vent) 373 { 374 time_t then; 375 char buf[100]; 376 377 if (!k->ki_u.u_valid) { 378 printf("%-*s", vent->width, "-"); 379 return; 380 } 381 then = k->ki_u.u_start.tv_sec; 382 strftime(buf, sizeof(buf) -1, "%c", localtime(&then)); 383 printf("%-*s", vent->width, buf); 384 } 385 386 void 387 wchan(const KINFO *k, const struct varent *vent) 388 { 389 if (KI_THREAD(k)->td_wchan) { 390 if (KI_THREAD(k)->td_wmesg) 391 printf("%-*.*s", vent->width, vent->width, 392 KI_EPROC(k)->e_wmesg); 393 else 394 printf("%-*lx", vent->width, 395 (long)KI_THREAD(k)->td_wchan); 396 } else 397 printf("%-*s", vent->width, "-"); 398 } 399 400 #ifndef pgtok 401 #define pgtok(a) (((a)*getpagesize())/1024) 402 #endif 403 404 void 405 vsize(const KINFO *k, const struct varent *vent) 406 { 407 printf("%*d", vent->width, (KI_EPROC(k)->e_vm.vm_map.size/1024)); 408 } 409 410 void 411 rssize(const KINFO *k, const struct varent *vent) 412 { 413 /* XXX don't have info about shared */ 414 printf("%*lu", vent->width, (u_long)pgtok(KI_EPROC(k)->e_vm.vm_rssize)); 415 } 416 417 void 418 p_rssize(const KINFO *k, const struct varent *vent) /* doesn't account for text */ 419 { 420 printf("%*ld", vent->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_rssize)); 421 } 422 423 void 424 cputime(const KINFO *k, const struct varent *vent) 425 { 426 long secs; 427 long psecs; /* "parts" of a second. first micro, then centi */ 428 char obuff[128]; 429 static char decimal_point = '\0'; 430 431 if (decimal_point == '\0') 432 decimal_point = localeconv()->decimal_point[0]; 433 434 if (KI_PROC(k)->p_stat == SZOMB || !k->ki_u.u_valid) { 435 secs = 0; 436 psecs = 0; 437 } else { 438 u_int64_t timeus; 439 440 /* 441 * This counts time spent handling interrupts. We could 442 * fix this, but it is not 100% trivial (and interrupt 443 * time fractions only work on the sparc anyway). XXX 444 */ 445 timeus = KI_EPROC(k)->e_uticks + KI_EPROC(k)->e_sticks + 446 KI_EPROC(k)->e_iticks; 447 secs = timeus / 1000000; 448 psecs = timeus % 1000000; 449 if (sumrusage) { 450 secs += k->ki_u.u_cru.ru_utime.tv_sec + 451 k->ki_u.u_cru.ru_stime.tv_sec; 452 psecs += k->ki_u.u_cru.ru_utime.tv_usec + 453 k->ki_u.u_cru.ru_stime.tv_usec; 454 } 455 /* 456 * round and scale to 100's 457 */ 458 psecs = (psecs + 5000) / 10000; 459 secs += psecs / 100; 460 psecs = psecs % 100; 461 } 462 snprintf(obuff, sizeof(obuff), 463 "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs); 464 printf("%*s", vent->width, obuff); 465 } 466 467 double 468 getpcpu(const KINFO *k) 469 { 470 const struct proc *p; 471 static int failure; 472 473 if (!nlistread) 474 failure = donlist(); 475 if (failure) 476 return (0.0); 477 478 p = KI_PROC(k); 479 #define fxtofl(fixpt) ((double)(fixpt) / fscale) 480 481 /* XXX - I don't like this */ 482 if (p->p_swtime == 0 || (p->p_flag & P_SWAPPEDOUT)) 483 return (0.0); 484 if (rawcpu) 485 return (100.0 * fxtofl(p->p_pctcpu)); 486 return (100.0 * fxtofl(p->p_pctcpu) / 487 (1.0 - exp(p->p_swtime * log(fxtofl(ccpu))))); 488 } 489 490 void 491 pcpu(const KINFO *k, const struct varent *vent) 492 { 493 printf("%*.1f", vent->width, getpcpu(k)); 494 } 495 496 void 497 pnice(const KINFO *k, const struct varent *vent) 498 { 499 int niceval; 500 501 switch (KI_PROC(k)->p_rtprio.type) { 502 case RTP_PRIO_REALTIME: 503 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_PROC(k)->p_rtprio.prio; 504 break; 505 case RTP_PRIO_IDLE: 506 niceval = PRIO_MAX + 1 + KI_PROC(k)->p_rtprio.prio; 507 break; 508 case RTP_PRIO_THREAD: 509 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_PROC(k)->p_rtprio.prio; 510 break; 511 default: 512 niceval = KI_PROC(k)->p_nice - NZERO; 513 break; 514 } 515 printf("%*d", vent->width, niceval); 516 } 517 518 519 double 520 getpmem(const KINFO *k) 521 { 522 static int failure; 523 struct proc *p; 524 struct eproc *e; 525 double fracmem; 526 int szptudot; 527 528 if (!nlistread) 529 failure = donlist(); 530 if (failure) 531 return (0.0); 532 533 p = KI_PROC(k); 534 e = KI_EPROC(k); 535 if (p->p_flag & P_SWAPPEDOUT) 536 return (0.0); 537 /* XXX want pmap ptpages, segtab, etc. (per architecture) */ 538 szptudot = UPAGES; 539 /* XXX don't have info about shared */ 540 fracmem = ((float)e->e_vm.vm_rssize + szptudot)/mempages; 541 return (100.0 * fracmem); 542 } 543 544 void 545 pmem(const KINFO *k, const struct varent *vent) 546 { 547 printf("%*.1f", vent->width, getpmem(k)); 548 } 549 550 void 551 pagein(const KINFO *k, const struct varent *vent) 552 { 553 printf("%*ld", vent->width, 554 k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0); 555 } 556 557 /* ARGSUSED */ 558 void 559 maxrss(const KINFO *k __unused, const struct varent *vent) 560 { 561 /* XXX not yet */ 562 printf("%*s", vent->width, "-"); 563 } 564 565 void 566 tsize(const KINFO *k, const struct varent *vent) 567 { 568 printf("%*ld", vent->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_tsize)); 569 } 570 571 void 572 rtprior(const KINFO *k, const struct varent *vent) 573 { 574 struct rtprio *prtp; 575 char str[8]; 576 unsigned prio, type; 577 578 prtp = (struct rtprio *) ((char *)KI_PROC(k) + vent->var->off); 579 prio = prtp->prio; 580 type = prtp->type; 581 switch (type) { 582 case RTP_PRIO_REALTIME: 583 snprintf(str, sizeof(str), "real:%u", prio); 584 break; 585 case RTP_PRIO_NORMAL: 586 strncpy(str, "normal", sizeof(str)); 587 break; 588 case RTP_PRIO_IDLE: 589 snprintf(str, sizeof(str), "idle:%u", prio); 590 break; 591 default: 592 snprintf(str, sizeof(str), "%u:%u", type, prio); 593 break; 594 } 595 str[sizeof(str) - 1] = '\0'; 596 printf("%*s", vent->width, str); 597 } 598 599 /* 600 * Generic output routines. Print fields from various prototype 601 * structures. 602 */ 603 static void 604 printval(const char *bp, const struct varent *vent) 605 { 606 static char ofmt[32] = "%"; 607 const char *fcp; 608 char *cp; 609 610 cp = ofmt + 1; 611 fcp = vent->var->fmt; 612 if (vent->var->flag & LJUST) 613 *cp++ = '-'; 614 *cp++ = '*'; 615 while ((*cp++ = *fcp++)); 616 617 switch (vent->var->type) { 618 case CHAR: 619 printf(ofmt, vent->width, *(const char *)bp); 620 break; 621 case UCHAR: 622 printf(ofmt, vent->width, *(const u_char *)bp); 623 break; 624 case SHORT: 625 printf(ofmt, vent->width, *(const short *)bp); 626 break; 627 case USHORT: 628 printf(ofmt, vent->width, *(const u_short *)bp); 629 break; 630 case INT: 631 printf(ofmt, vent->width, *(const int *)bp); 632 break; 633 case UINT: 634 printf(ofmt, vent->width, *(const u_int *)bp); 635 break; 636 case LONG: 637 printf(ofmt, vent->width, *(const long *)bp); 638 break; 639 case ULONG: 640 printf(ofmt, vent->width, *(const u_long *)bp); 641 break; 642 case KPTR: 643 printf(ofmt, vent->width, *(const u_long *)bp); 644 break; 645 default: 646 errx(1, "unknown type %d", vent->var->type); 647 } 648 } 649 650 void 651 pvar(const KINFO *k, const struct varent *vent) 652 { 653 printval((char *)((char *)KI_PROC(k) + vent->var->off), vent); 654 } 655 656 void 657 pest(const KINFO *k, const struct varent *vent) 658 { 659 int val; 660 661 val = *(int *)((char *)KI_PROC(k) + vent->var->off); 662 val = val / 128; 663 printval((char *)&val, vent); 664 } 665 666 667 void 668 tvar(const KINFO *k, const struct varent *vent) 669 { 670 printval((char *)((char *)KI_THREAD(k) + vent->var->off), vent); 671 } 672 673 void 674 evar(const KINFO *k, const struct varent *vent) 675 { 676 printval((char *)((char *)KI_EPROC(k) + vent->var->off), vent); 677 } 678 679 void 680 uvar(const KINFO *k, const struct varent *vent) 681 { 682 if (k->ki_u.u_valid) 683 printval(((const char *)&k->ki_u + vent->var->off), vent); 684 else 685 printf("%*s", vent->width, "-"); 686 } 687 688 void 689 rvar(const KINFO *k, const struct varent *vent) 690 { 691 if (k->ki_u.u_valid) 692 printval(((const char *)&k->ki_u.u_ru + vent->var->off), vent); 693 else 694 printf("%*s", vent->width, "-"); 695 } 696 697 static const char * 698 make_printable(const char *str) 699 { 700 static char *cpy; 701 int len; 702 703 if (cpy) 704 free(cpy); 705 len = strlen(str); 706 if ((cpy = malloc(len * 4 + 1)) == NULL) 707 err(1, NULL); 708 strvis(cpy, str, VIS_TAB | VIS_NL | VIS_NOSLASH); 709 return(cpy); 710 } 711