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