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