1 /* $NetBSD: print.c,v 1.123 2014/11/15 01:58:34 joerg Exp $ */ 2 3 /* 4 * Copyright (c) 2000, 2007 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Simon Burge. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1990, 1993, 1994 34 * The Regents of the University of California. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. Neither the name of the University nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 */ 60 61 #include <sys/cdefs.h> 62 #ifndef lint 63 #if 0 64 static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94"; 65 #else 66 __RCSID("$NetBSD: print.c,v 1.123 2014/11/15 01:58:34 joerg Exp $"); 67 #endif 68 #endif /* not lint */ 69 70 #include <sys/param.h> 71 #include <sys/time.h> 72 #include <sys/resource.h> 73 #include <sys/lwp.h> 74 #include <sys/proc.h> 75 #include <sys/stat.h> 76 #include <sys/ucred.h> 77 #include <sys/sysctl.h> 78 79 #include <err.h> 80 #include <grp.h> 81 #include <kvm.h> 82 #include <math.h> 83 #include <nlist.h> 84 #include <pwd.h> 85 #include <stddef.h> 86 #include <stdio.h> 87 #include <stdlib.h> 88 #include <string.h> 89 #include <time.h> 90 #include <tzfile.h> 91 #include <unistd.h> 92 93 #include "ps.h" 94 95 static char *cmdpart(char *); 96 static void printval(void *, VAR *, enum mode); 97 static int titlecmp(char *, char **); 98 99 static void doubleprintorsetwidth(VAR *, double, int, enum mode); 100 static void intprintorsetwidth(VAR *, int, enum mode); 101 static void strprintorsetwidth(VAR *, const char *, enum mode); 102 103 static time_t now; 104 105 #define min(a,b) ((a) <= (b) ? (a) : (b)) 106 107 static int 108 iwidth(u_int64_t v) 109 { 110 u_int64_t nlim, lim; 111 int w = 1; 112 113 for (lim = 10; v >= lim; lim = nlim) { 114 nlim = lim * 10; 115 w++; 116 if (nlim < lim) 117 break; 118 } 119 return w; 120 } 121 122 static char * 123 cmdpart(char *arg0) 124 { 125 char *cp; 126 127 return ((cp = strrchr(arg0, '/')) != NULL ? cp + 1 : arg0); 128 } 129 130 void 131 printheader(void) 132 { 133 int len; 134 VAR *v; 135 struct varent *vent; 136 static int firsttime = 1; 137 static int noheader = 0; 138 139 /* 140 * If all the columns have user-specified null headers, 141 * don't print the blank header line at all. 142 */ 143 if (firsttime) { 144 SIMPLEQ_FOREACH(vent, &displaylist, next) { 145 if (vent->var->header[0]) 146 break; 147 } 148 if (vent == NULL) { 149 noheader = 1; 150 firsttime = 0; 151 } 152 153 } 154 if (noheader) 155 return; 156 157 SIMPLEQ_FOREACH(vent, &displaylist, next) { 158 v = vent->var; 159 if (firsttime) { 160 len = strlen(v->header); 161 if (len > v->width) 162 v->width = len; 163 totwidth += v->width + 1; /* +1 for space */ 164 } 165 if (v->flag & LJUST) { 166 if (SIMPLEQ_NEXT(vent, next) == NULL) /* last one */ 167 (void)printf("%s", v->header); 168 else 169 (void)printf("%-*s", v->width, 170 v->header); 171 } else 172 (void)printf("%*s", v->width, v->header); 173 if (SIMPLEQ_NEXT(vent, next) != NULL) 174 (void)putchar(' '); 175 } 176 (void)putchar('\n'); 177 if (firsttime) { 178 firsttime = 0; 179 totwidth--; /* take off last space */ 180 } 181 } 182 183 /* 184 * Return 1 if the command name in the argument vector (u-area) does 185 * not match the command name (p_comm) 186 */ 187 static int 188 titlecmp(char *name, char **argv) 189 { 190 char *title; 191 int namelen; 192 193 194 /* no argument vector == no match; system processes/threads do that */ 195 if (argv == 0 || argv[0] == 0) 196 return (1); 197 198 title = cmdpart(argv[0]); 199 200 /* the basename matches */ 201 if (!strcmp(name, title)) 202 return (0); 203 204 /* handle login shells, by skipping the leading - */ 205 if (title[0] == '-' && !strcmp(name, title + 1)) 206 return (0); 207 208 namelen = strlen(name); 209 210 /* handle daemons that report activity as daemonname: activity */ 211 if (argv[1] == 0 && 212 !strncmp(name, title, namelen) && 213 title[namelen + 0] == ':' && 214 title[namelen + 1] == ' ') 215 return (0); 216 217 return (1); 218 } 219 220 static void 221 doubleprintorsetwidth(VAR *v, double val, int prec, enum mode mode) 222 { 223 int fmtlen; 224 225 if (mode == WIDTHMODE) { 226 if (val < 0.0 && val < v->longestnd) { 227 fmtlen = (int)log10(-val) + prec + 2; 228 v->longestnd = val; 229 if (fmtlen > v->width) 230 v->width = fmtlen; 231 } else if (val > 0.0 && val > v->longestpd) { 232 fmtlen = (int)log10(val) + prec + 1; 233 v->longestpd = val; 234 if (fmtlen > v->width) 235 v->width = fmtlen; 236 } 237 } else { 238 (void)printf("%*.*f", v->width, prec, val); 239 } 240 } 241 242 static void 243 intprintorsetwidth(VAR *v, int val, enum mode mode) 244 { 245 int fmtlen; 246 247 if (mode == WIDTHMODE) { 248 if (val < 0 && val < v->longestn) { 249 v->longestn = val; 250 fmtlen = iwidth(-val) + 1; 251 if (fmtlen > v->width) 252 v->width = fmtlen; 253 } else if (val > 0 && val > v->longestp) { 254 v->longestp = val; 255 fmtlen = iwidth(val); 256 if (fmtlen > v->width) 257 v->width = fmtlen; 258 } 259 } else 260 (void)printf("%*d", v->width, val); 261 } 262 263 static void 264 strprintorsetwidth(VAR *v, const char *str, enum mode mode) 265 { 266 int len; 267 268 if (mode == WIDTHMODE) { 269 len = strlen(str); 270 if (len > v->width) 271 v->width = len; 272 } else { 273 if (v->flag & LJUST) 274 (void)printf("%-*.*s", v->width, v->width, str); 275 else 276 (void)printf("%*.*s", v->width, v->width, str); 277 } 278 } 279 280 void 281 command(void *arg, VARENT *ve, enum mode mode) 282 { 283 struct kinfo_proc2 *ki; 284 VAR *v; 285 int left; 286 char **argv, **p, *name; 287 288 if (mode == WIDTHMODE) 289 return; 290 291 ki = arg; 292 v = ve->var; 293 if (SIMPLEQ_NEXT(ve, next) != NULL || termwidth != UNLIMITED) { 294 if (SIMPLEQ_NEXT(ve, next) == NULL) { 295 left = termwidth - (totwidth - v->width); 296 if (left < 1) /* already wrapped, just use std width */ 297 left = v->width; 298 } else 299 left = v->width; 300 } else 301 left = -1; 302 if (needenv && kd) { 303 argv = kvm_getenvv2(kd, ki, termwidth); 304 if ((p = argv) != NULL) { 305 while (*p) { 306 fmt_puts(*p, &left); 307 p++; 308 fmt_putc(' ', &left); 309 } 310 } 311 } 312 if (needcomm) { 313 name = ki->p_comm; 314 if (!commandonly) { 315 argv = kvm_getargv2(kd, ki, termwidth); 316 if ((p = argv) != NULL) { 317 while (*p) { 318 fmt_puts(*p, &left); 319 p++; 320 fmt_putc(' ', &left); 321 if (v->flag & ARGV0) 322 break; 323 } 324 if (!(v->flag & ARGV0) && 325 titlecmp(name, argv)) { 326 /* 327 * append the real command name within 328 * parentheses, if the command name 329 * does not match the one in the 330 * argument vector 331 */ 332 fmt_putc('(', &left); 333 fmt_puts(name, &left); 334 fmt_putc(')', &left); 335 } 336 } else { 337 /* 338 * Commands that don't set an argv vector 339 * are printed with square brackets if they 340 * are system commands. Otherwise they are 341 * printed within parentheses. 342 */ 343 if (ki->p_flag & P_SYSTEM) { 344 fmt_putc('[', &left); 345 fmt_puts(name, &left); 346 fmt_putc(']', &left); 347 } else { 348 fmt_putc('(', &left); 349 fmt_puts(name, &left); 350 fmt_putc(')', &left); 351 } 352 } 353 } else { 354 fmt_puts(name, &left); 355 } 356 } 357 if (SIMPLEQ_NEXT(ve, next) != NULL && left > 0) 358 (void)printf("%*s", left, ""); 359 } 360 361 void 362 groups(void *arg, VARENT *ve, enum mode mode) 363 { 364 struct kinfo_proc2 *ki; 365 VAR *v; 366 int left, i; 367 char buf[16], *p; 368 369 if (mode == WIDTHMODE) 370 return; 371 372 ki = arg; 373 v = ve->var; 374 if (SIMPLEQ_NEXT(ve, next) != NULL || termwidth != UNLIMITED) { 375 if (SIMPLEQ_NEXT(ve, next) == NULL) { 376 left = termwidth - (totwidth - v->width); 377 if (left < 1) /* already wrapped, just use std width */ 378 left = v->width; 379 } else 380 left = v->width; 381 } else 382 left = -1; 383 384 if (ki->p_ngroups == 0) 385 fmt_putc('-', &left); 386 387 for (i = 0; i < ki->p_ngroups; i++) { 388 (void)snprintf(buf, sizeof(buf), "%d", ki->p_groups[i]); 389 if (i) 390 fmt_putc(' ', &left); 391 for (p = &buf[0]; *p; p++) 392 fmt_putc(*p, &left); 393 } 394 395 if (SIMPLEQ_NEXT(ve, next) != NULL && left > 0) 396 (void)printf("%*s", left, ""); 397 } 398 399 void 400 groupnames(void *arg, VARENT *ve, enum mode mode) 401 { 402 struct kinfo_proc2 *ki; 403 VAR *v; 404 int left, i; 405 const char *p; 406 407 if (mode == WIDTHMODE) 408 return; 409 410 ki = arg; 411 v = ve->var; 412 if (SIMPLEQ_NEXT(ve, next) != NULL || termwidth != UNLIMITED) { 413 if (SIMPLEQ_NEXT(ve, next) == NULL) { 414 left = termwidth - (totwidth - v->width); 415 if (left < 1) /* already wrapped, just use std width */ 416 left = v->width; 417 } else 418 left = v->width; 419 } else 420 left = -1; 421 422 if (ki->p_ngroups == 0) 423 fmt_putc('-', &left); 424 425 for (i = 0; i < ki->p_ngroups; i++) { 426 if (i) 427 fmt_putc(' ', &left); 428 for (p = group_from_gid(ki->p_groups[i], 0); *p; p++) 429 fmt_putc(*p, &left); 430 } 431 432 if (SIMPLEQ_NEXT(ve, next) != NULL && left > 0) 433 (void)printf("%*s", left, ""); 434 } 435 436 void 437 ucomm(void *arg, VARENT *ve, enum mode mode) 438 { 439 struct kinfo_proc2 *k; 440 VAR *v; 441 442 k = arg; 443 v = ve->var; 444 strprintorsetwidth(v, k->p_comm, mode); 445 } 446 447 void 448 emul(void *arg, VARENT *ve, enum mode mode) 449 { 450 struct kinfo_proc2 *k; 451 VAR *v; 452 453 k = arg; 454 v = ve->var; 455 strprintorsetwidth(v, k->p_ename, mode); 456 } 457 458 void 459 logname(void *arg, VARENT *ve, enum mode mode) 460 { 461 struct kinfo_proc2 *k; 462 VAR *v; 463 464 k = arg; 465 v = ve->var; 466 strprintorsetwidth(v, k->p_login, mode); 467 } 468 469 void 470 state(void *arg, VARENT *ve, enum mode mode) 471 { 472 struct kinfo_proc2 *k; 473 int flag, is_zombie; 474 char *cp; 475 VAR *v; 476 char buf[16]; 477 478 k = arg; 479 is_zombie = 0; 480 v = ve->var; 481 flag = k->p_flag; 482 cp = buf; 483 484 /* 485 * NOTE: There are historical letters, which are no longer used: 486 * 487 * - W: indicated that process is swapped out. 488 * - L: indicated non-zero l_holdcnt (i.e. that process was 489 * prevented from swapping-out. 490 * 491 * These letters should not be used for new states to avoid 492 * conflicts with old applications which might depend on them. 493 */ 494 switch (k->p_stat) { 495 496 case LSSTOP: 497 *cp = 'T'; 498 break; 499 500 case LSSLEEP: 501 if (flag & L_SINTR) /* interruptable (long) */ 502 *cp = (int)k->p_slptime >= maxslp ? 'I' : 'S'; 503 else 504 *cp = 'D'; 505 break; 506 507 case LSRUN: 508 case LSIDL: 509 *cp = 'R'; 510 break; 511 512 case LSONPROC: 513 *cp = 'O'; 514 break; 515 516 case LSZOMB: 517 *cp = 'Z'; 518 is_zombie = 1; 519 break; 520 521 case LSSUSPENDED: 522 *cp = 'U'; 523 break; 524 525 default: 526 *cp = '?'; 527 } 528 cp++; 529 if (k->p_nice < NZERO) 530 *cp++ = '<'; 531 else if (k->p_nice > NZERO) 532 *cp++ = 'N'; 533 if (flag & P_TRACED) 534 *cp++ = 'X'; 535 if (flag & P_WEXIT && !is_zombie) 536 *cp++ = 'E'; 537 if (flag & P_PPWAIT) 538 *cp++ = 'V'; 539 if (flag & P_SYSTEM) 540 *cp++ = 'K'; 541 if (k->p_eflag & EPROC_SLEADER) 542 *cp++ = 's'; 543 if (flag & P_SA) 544 *cp++ = 'a'; 545 else if (k->p_nlwps > 1) 546 *cp++ = 'l'; 547 if ((flag & P_CONTROLT) && k->p__pgid == k->p_tpgid) 548 *cp++ = '+'; 549 *cp = '\0'; 550 strprintorsetwidth(v, buf, mode); 551 } 552 553 void 554 lstate(void *arg, VARENT *ve, enum mode mode) 555 { 556 struct kinfo_lwp *k; 557 int flag; 558 char *cp; 559 VAR *v; 560 char buf[16]; 561 562 k = arg; 563 v = ve->var; 564 flag = k->l_flag; 565 cp = buf; 566 567 switch (k->l_stat) { 568 569 case LSSTOP: 570 *cp = 'T'; 571 break; 572 573 case LSSLEEP: 574 if (flag & L_SINTR) /* interruptible (long) */ 575 *cp = (int)k->l_slptime >= maxslp ? 'I' : 'S'; 576 else 577 *cp = 'D'; 578 break; 579 580 case LSRUN: 581 case LSIDL: 582 *cp = 'R'; 583 break; 584 585 case LSONPROC: 586 *cp = 'O'; 587 break; 588 589 case LSZOMB: 590 case LSDEAD: 591 *cp = 'Z'; 592 break; 593 594 case LSSUSPENDED: 595 *cp = 'U'; 596 break; 597 598 default: 599 *cp = '?'; 600 } 601 cp++; 602 if (flag & L_SYSTEM) 603 *cp++ = 'K'; 604 if (flag & L_SA) 605 *cp++ = 'a'; 606 if (flag & L_DETACHED) 607 *cp++ = '-'; 608 *cp = '\0'; 609 strprintorsetwidth(v, buf, mode); 610 } 611 612 void 613 pnice(void *arg, VARENT *ve, enum mode mode) 614 { 615 struct kinfo_proc2 *k; 616 VAR *v; 617 618 k = arg; 619 v = ve->var; 620 intprintorsetwidth(v, k->p_nice - NZERO, mode); 621 } 622 623 void 624 pri(void *arg, VARENT *ve, enum mode mode) 625 { 626 struct kinfo_lwp *l; 627 VAR *v; 628 629 l = arg; 630 v = ve->var; 631 intprintorsetwidth(v, l->l_priority, mode); 632 } 633 634 void 635 uname(void *arg, VARENT *ve, enum mode mode) 636 { 637 struct kinfo_proc2 *k; 638 VAR *v; 639 640 k = arg; 641 v = ve->var; 642 strprintorsetwidth(v, user_from_uid(k->p_uid, 0), mode); 643 } 644 645 void 646 runame(void *arg, VARENT *ve, enum mode mode) 647 { 648 struct kinfo_proc2 *k; 649 VAR *v; 650 651 k = arg; 652 v = ve->var; 653 strprintorsetwidth(v, user_from_uid(k->p_ruid, 0), mode); 654 } 655 656 void 657 svuname(void *arg, VARENT *ve, enum mode mode) 658 { 659 struct kinfo_proc2 *k; 660 VAR *v; 661 662 k = arg; 663 v = ve->var; 664 strprintorsetwidth(v, user_from_uid(k->p_svuid, 0), mode); 665 } 666 667 void 668 gname(void *arg, VARENT *ve, enum mode mode) 669 { 670 struct kinfo_proc2 *k; 671 VAR *v; 672 673 k = arg; 674 v = ve->var; 675 strprintorsetwidth(v, group_from_gid(k->p_gid, 0), mode); 676 } 677 678 void 679 rgname(void *arg, VARENT *ve, enum mode mode) 680 { 681 struct kinfo_proc2 *k; 682 VAR *v; 683 684 k = arg; 685 v = ve->var; 686 strprintorsetwidth(v, group_from_gid(k->p_rgid, 0), mode); 687 } 688 689 void 690 svgname(void *arg, VARENT *ve, enum mode mode) 691 { 692 struct kinfo_proc2 *k; 693 VAR *v; 694 695 k = arg; 696 v = ve->var; 697 strprintorsetwidth(v, group_from_gid(k->p_svgid, 0), mode); 698 } 699 700 void 701 tdev(void *arg, VARENT *ve, enum mode mode) 702 { 703 struct kinfo_proc2 *k; 704 VAR *v; 705 dev_t dev; 706 char buff[16]; 707 708 k = arg; 709 v = ve->var; 710 dev = k->p_tdev; 711 if (dev == NODEV) { 712 if (mode == PRINTMODE) 713 (void)printf("%*s", v->width, "?"); 714 else 715 if (v->width < 2) 716 v->width = 2; 717 } else { 718 (void)snprintf(buff, sizeof(buff), 719 "%lld/%lld", (long long)major(dev), (long long)minor(dev)); 720 strprintorsetwidth(v, buff, mode); 721 } 722 } 723 724 void 725 tname(void *arg, VARENT *ve, enum mode mode) 726 { 727 struct kinfo_proc2 *k; 728 VAR *v; 729 dev_t dev; 730 const char *ttname; 731 int noctty; 732 733 k = arg; 734 v = ve->var; 735 dev = k->p_tdev; 736 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) { 737 if (mode == PRINTMODE) 738 (void)printf("%-*s", v->width, "?"); 739 else 740 if (v->width < 2) 741 v->width = 2; 742 } else { 743 #ifdef __minix 744 /* Actually shorten TTY names. "console" is *really* long. */ 745 if (strcmp(ttname, "console") == 0) 746 ttname = "co"; 747 else if (strncmp(ttname, "tty", 3) == 0 && ttname[3] != '\0') 748 ttname += 3; 749 else if (strncmp(ttname, "pts/", 4) == 0 && ttname[4] != '\0') 750 ttname += 4; /* this is what FreeBSD does */ 751 #endif /* __minix */ 752 noctty = !(k->p_eflag & EPROC_CTTY) ? 1 : 0; 753 if (mode == WIDTHMODE) { 754 int fmtlen; 755 756 fmtlen = strlen(ttname) + noctty; 757 if (v->width < fmtlen) 758 v->width = fmtlen; 759 } else { 760 if (noctty) 761 (void)printf("%-*s-", v->width - 1, ttname); 762 else 763 (void)printf("%-*s", v->width, ttname); 764 } 765 } 766 } 767 768 void 769 longtname(void *arg, VARENT *ve, enum mode mode) 770 { 771 struct kinfo_proc2 *k; 772 VAR *v; 773 dev_t dev; 774 const char *ttname; 775 776 k = arg; 777 v = ve->var; 778 dev = k->p_tdev; 779 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) { 780 if (mode == PRINTMODE) 781 (void)printf("%-*s", v->width, "?"); 782 else 783 if (v->width < 2) 784 v->width = 2; 785 } else { 786 strprintorsetwidth(v, ttname, mode); 787 } 788 } 789 790 void 791 started(void *arg, VARENT *ve, enum mode mode) 792 { 793 struct kinfo_proc2 *k; 794 VAR *v; 795 time_t startt; 796 struct tm *tp; 797 char buf[100], *cp; 798 799 k = arg; 800 v = ve->var; 801 if (!k->p_uvalid) { 802 if (mode == PRINTMODE) 803 (void)printf("%*s", v->width, "-"); 804 return; 805 } 806 807 startt = k->p_ustart_sec; 808 tp = localtime(&startt); 809 if (now == 0) 810 (void)time(&now); 811 if (now - k->p_ustart_sec < SECSPERDAY) 812 /* I *hate* SCCS... */ 813 (void)strftime(buf, sizeof(buf) - 1, "%l:%" "M%p", tp); 814 else if (now - k->p_ustart_sec < DAYSPERWEEK * SECSPERDAY) 815 /* I *hate* SCCS... */ 816 (void)strftime(buf, sizeof(buf) - 1, "%a%" "I%p", tp); 817 else 818 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp); 819 /* %e and %l can start with a space. */ 820 cp = buf; 821 if (*cp == ' ') 822 cp++; 823 strprintorsetwidth(v, cp, mode); 824 } 825 826 void 827 lstarted(void *arg, VARENT *ve, enum mode mode) 828 { 829 struct kinfo_proc2 *k; 830 VAR *v; 831 time_t startt; 832 char buf[100]; 833 834 k = arg; 835 v = ve->var; 836 if (!k->p_uvalid) { 837 /* 838 * Minimum width is less than header - we don't 839 * need to check it every time. 840 */ 841 if (mode == PRINTMODE) 842 (void)printf("%*s", v->width, "-"); 843 return; 844 } 845 startt = k->p_ustart_sec; 846 847 /* assume all times are the same length */ 848 if (mode != WIDTHMODE || v->width == 0) { 849 (void)strftime(buf, sizeof(buf) -1, "%c", 850 localtime(&startt)); 851 strprintorsetwidth(v, buf, mode); 852 } 853 } 854 855 void 856 elapsed(void *arg, VARENT *ve, enum mode mode) 857 { 858 struct kinfo_proc2 *k; 859 VAR *v; 860 int32_t origseconds, secs, mins, hours, days; 861 int fmtlen, printed_something; 862 863 k = arg; 864 v = ve->var; 865 if (k->p_uvalid == 0) { 866 origseconds = 0; 867 } else { 868 if (now == 0) 869 (void)time(&now); 870 origseconds = now - k->p_ustart_sec; 871 if (origseconds < 0) { 872 /* 873 * Don't try to be fancy if the machine's 874 * clock has been rewound to before the 875 * process "started". 876 */ 877 origseconds = 0; 878 } 879 } 880 881 secs = origseconds; 882 mins = secs / SECSPERMIN; 883 secs %= SECSPERMIN; 884 hours = mins / MINSPERHOUR; 885 mins %= MINSPERHOUR; 886 days = hours / HOURSPERDAY; 887 hours %= HOURSPERDAY; 888 889 if (mode == WIDTHMODE) { 890 if (origseconds == 0) 891 /* non-zero so fmtlen is calculated at least once */ 892 origseconds = 1; 893 894 if (origseconds > v->longestp) { 895 v->longestp = origseconds; 896 897 if (days > 0) { 898 /* +9 for "-hh:mm:ss" */ 899 fmtlen = iwidth(days) + 9; 900 } else if (hours > 0) { 901 /* +6 for "mm:ss" */ 902 fmtlen = iwidth(hours) + 6; 903 } else { 904 /* +3 for ":ss" */ 905 fmtlen = iwidth(mins) + 3; 906 } 907 908 if (fmtlen > v->width) 909 v->width = fmtlen; 910 } 911 } else { 912 printed_something = 0; 913 fmtlen = v->width; 914 915 if (days > 0) { 916 (void)printf("%*d", fmtlen - 9, days); 917 printed_something = 1; 918 } else if (fmtlen > 9) { 919 (void)printf("%*s", fmtlen - 9, ""); 920 } 921 if (fmtlen > 9) 922 fmtlen = 9; 923 924 if (printed_something) { 925 (void)printf("-%.*d", fmtlen - 7, hours); 926 printed_something = 1; 927 } else if (hours > 0) { 928 (void)printf("%*d", fmtlen - 6, hours); 929 printed_something = 1; 930 } else if (fmtlen > 6) { 931 (void)printf("%*s", fmtlen - 6, ""); 932 } 933 if (fmtlen > 6) 934 fmtlen = 6; 935 936 /* Don't need to set fmtlen or printed_something any more... */ 937 if (printed_something) { 938 (void)printf(":%.*d", fmtlen - 4, mins); 939 } else if (mins > 0) { 940 (void)printf("%*d", fmtlen - 3, mins); 941 } else if (fmtlen > 3) { 942 (void)printf("%*s", fmtlen - 3, "0"); 943 } 944 945 (void)printf(":%.2d", secs); 946 } 947 } 948 949 void 950 wchan(void *arg, VARENT *ve, enum mode mode) 951 { 952 struct kinfo_lwp *l; 953 VAR *v; 954 char *buf; 955 956 l = arg; 957 v = ve->var; 958 if (l->l_wchan) { 959 if (l->l_wmesg[0]) { 960 strprintorsetwidth(v, l->l_wmesg, mode); 961 v->width = min(v->width, KI_WMESGLEN); 962 } else { 963 (void)asprintf(&buf, "%-*" PRIx64, v->width, 964 l->l_wchan); 965 if (buf == NULL) 966 err(1, "%s", ""); 967 strprintorsetwidth(v, buf, mode); 968 v->width = min(v->width, KI_WMESGLEN); 969 free(buf); 970 } 971 } else { 972 if (mode == PRINTMODE) 973 (void)printf("%-*s", v->width, "-"); 974 } 975 } 976 977 #define pgtok(a) (((a)*(size_t)getpagesize())/1024) 978 979 void 980 vsize(void *arg, VARENT *ve, enum mode mode) 981 { 982 struct kinfo_proc2 *k; 983 VAR *v; 984 985 k = arg; 986 v = ve->var; 987 intprintorsetwidth(v, pgtok(k->p_vm_msize), mode); 988 } 989 990 void 991 rssize(void *arg, VARENT *ve, enum mode mode) 992 { 993 struct kinfo_proc2 *k; 994 VAR *v; 995 996 k = arg; 997 v = ve->var; 998 /* XXX don't have info about shared */ 999 intprintorsetwidth(v, pgtok(k->p_vm_rssize), mode); 1000 } 1001 1002 void 1003 p_rssize(void *arg, VARENT *ve, enum mode mode) /* doesn't account for text */ 1004 { 1005 struct kinfo_proc2 *k; 1006 VAR *v; 1007 1008 k = arg; 1009 v = ve->var; 1010 intprintorsetwidth(v, pgtok(k->p_vm_rssize), mode); 1011 } 1012 1013 void 1014 cpuid(void *arg, VARENT *ve, enum mode mode) 1015 { 1016 struct kinfo_lwp *l; 1017 VAR *v; 1018 1019 l = arg; 1020 v = ve->var; 1021 intprintorsetwidth(v, l->l_cpuid, mode); 1022 } 1023 1024 static void 1025 cputime1(int32_t secs, int32_t psecs, VAR *v, enum mode mode) 1026 { 1027 int fmtlen; 1028 1029 /* 1030 * round and scale to 100's 1031 */ 1032 psecs = (psecs + 5000) / 10000; 1033 secs += psecs / 100; 1034 psecs = psecs % 100; 1035 1036 if (mode == WIDTHMODE) { 1037 /* 1038 * Ugg, this is the only field where a value of 0 is longer 1039 * than the column title. 1040 * Use SECSPERMIN, because secs is divided by that when 1041 * passed to iwidth(). 1042 */ 1043 if (secs == 0) 1044 secs = SECSPERMIN; 1045 1046 if (secs > v->longestp) { 1047 v->longestp = secs; 1048 /* "+6" for the ":%02ld.%02ld" in the printf() below */ 1049 fmtlen = iwidth(secs / SECSPERMIN) + 6; 1050 if (fmtlen > v->width) 1051 v->width = fmtlen; 1052 } 1053 } else { 1054 (void)printf("%*ld:%02ld.%02ld", v->width - 6, 1055 (long)(secs / SECSPERMIN), (long)(secs % SECSPERMIN), 1056 (long)psecs); 1057 } 1058 } 1059 1060 void 1061 cputime(void *arg, VARENT *ve, enum mode mode) 1062 { 1063 struct kinfo_proc2 *k; 1064 VAR *v; 1065 int32_t secs; 1066 int32_t psecs; /* "parts" of a second. first micro, then centi */ 1067 1068 k = arg; 1069 v = ve->var; 1070 1071 /* 1072 * This counts time spent handling interrupts. We could 1073 * fix this, but it is not 100% trivial (and interrupt 1074 * time fractions only work on the sparc anyway). XXX 1075 */ 1076 secs = k->p_rtime_sec; 1077 psecs = k->p_rtime_usec; 1078 if (sumrusage) { 1079 secs += k->p_uctime_sec; 1080 psecs += k->p_uctime_usec; 1081 } 1082 1083 cputime1(secs, psecs, v, mode); 1084 } 1085 1086 void 1087 lcputime(void *arg, VARENT *ve, enum mode mode) 1088 { 1089 struct kinfo_lwp *l; 1090 VAR *v; 1091 int32_t secs; 1092 int32_t psecs; /* "parts" of a second. first micro, then centi */ 1093 1094 l = arg; 1095 v = ve->var; 1096 1097 secs = l->l_rtime_sec; 1098 psecs = l->l_rtime_usec; 1099 1100 cputime1(secs, psecs, v, mode); 1101 } 1102 1103 double 1104 getpcpu(const struct kinfo_proc2 *k) 1105 { 1106 static int failure; 1107 1108 if (!nlistread) 1109 failure = (kd) ? donlist() : 1; 1110 if (failure) 1111 return (0.0); 1112 1113 #define fxtofl(fixpt) ((double)(fixpt) / fscale) 1114 1115 if (k->p_swtime == 0 || k->p_realstat == SZOMB) 1116 return (0.0); 1117 if (rawcpu) 1118 return (100.0 * fxtofl(k->p_pctcpu)); 1119 return (100.0 * fxtofl(k->p_pctcpu) / 1120 (1.0 - exp(k->p_swtime * log(ccpu)))); 1121 } 1122 1123 void 1124 pcpu(void *arg, VARENT *ve, enum mode mode) 1125 { 1126 struct kinfo_proc2 *k; 1127 VAR *v; 1128 double dbl; 1129 1130 k = arg; 1131 v = ve->var; 1132 dbl = getpcpu(k); 1133 doubleprintorsetwidth(v, dbl, (dbl >= 99.95) ? 0 : 1, mode); 1134 } 1135 1136 double 1137 getpmem(const struct kinfo_proc2 *k) 1138 { 1139 static int failure; 1140 double fracmem; 1141 int szptudot; 1142 1143 if (!nlistread) 1144 failure = (kd) ? donlist() : 1; 1145 if (failure) 1146 return (0.0); 1147 1148 /* XXX want pmap ptpages, segtab, etc. (per architecture) */ 1149 szptudot = uspace/getpagesize(); 1150 /* XXX don't have info about shared */ 1151 fracmem = ((float)k->p_vm_rssize + szptudot)/mempages; 1152 return (100.0 * fracmem); 1153 } 1154 1155 void 1156 pmem(void *arg, VARENT *ve, enum mode mode) 1157 { 1158 struct kinfo_proc2 *k; 1159 VAR *v; 1160 1161 k = arg; 1162 v = ve->var; 1163 doubleprintorsetwidth(v, getpmem(k), 1, mode); 1164 } 1165 1166 void 1167 pagein(void *arg, VARENT *ve, enum mode mode) 1168 { 1169 struct kinfo_proc2 *k; 1170 VAR *v; 1171 1172 k = arg; 1173 v = ve->var; 1174 intprintorsetwidth(v, k->p_uvalid ? k->p_uru_majflt : 0, mode); 1175 } 1176 1177 void 1178 maxrss(void *arg, VARENT *ve, enum mode mode) 1179 { 1180 VAR *v; 1181 1182 v = ve->var; 1183 /* No need to check width! */ 1184 if (mode == PRINTMODE) 1185 (void)printf("%*s", v->width, "-"); 1186 } 1187 1188 void 1189 tsize(void *arg, VARENT *ve, enum mode mode) 1190 { 1191 struct kinfo_proc2 *k; 1192 VAR *v; 1193 1194 k = arg; 1195 v = ve->var; 1196 intprintorsetwidth(v, pgtok(k->p_vm_tsize), mode); 1197 } 1198 1199 /* 1200 * Generic output routines. Print fields from various prototype 1201 * structures. 1202 */ 1203 static void 1204 printval(void *bp, VAR *v, enum mode mode) 1205 { 1206 static char ofmt[32] = "%"; 1207 int width, vok, fmtlen; 1208 const char *fcp; 1209 char *cp; 1210 int64_t val; 1211 u_int64_t uval; 1212 1213 val = 0; /* XXXGCC -Wuninitialized [hpcarm] */ 1214 uval = 0; /* XXXGCC -Wuninitialized [hpcarm] */ 1215 1216 /* 1217 * Note that the "INF127" check is nonsensical for types 1218 * that are or can be signed. 1219 */ 1220 #define GET(type) (*(type *)bp) 1221 #define CHK_INF127(n) (((n) > 127) && (v->flag & INF127) ? 127 : (n)) 1222 1223 #define VSIGN 1 1224 #define VUNSIGN 2 1225 #define VPTR 3 1226 1227 if (mode == WIDTHMODE) { 1228 vok = 0; 1229 switch (v->type) { 1230 case CHAR: 1231 val = GET(char); 1232 vok = VSIGN; 1233 break; 1234 case UCHAR: 1235 uval = CHK_INF127(GET(u_char)); 1236 vok = VUNSIGN; 1237 break; 1238 case SHORT: 1239 val = GET(short); 1240 vok = VSIGN; 1241 break; 1242 case USHORT: 1243 uval = CHK_INF127(GET(u_short)); 1244 vok = VUNSIGN; 1245 break; 1246 case INT32: 1247 val = GET(int32_t); 1248 vok = VSIGN; 1249 break; 1250 case INT: 1251 val = GET(int); 1252 vok = VSIGN; 1253 break; 1254 case UINT: 1255 case UINT32: 1256 uval = CHK_INF127(GET(u_int)); 1257 vok = VUNSIGN; 1258 break; 1259 case LONG: 1260 val = GET(long); 1261 vok = VSIGN; 1262 break; 1263 case ULONG: 1264 uval = CHK_INF127(GET(u_long)); 1265 vok = VUNSIGN; 1266 break; 1267 case KPTR: 1268 uval = GET(u_int64_t); 1269 vok = VPTR; 1270 break; 1271 case KPTR24: 1272 uval = GET(u_int64_t); 1273 uval &= 0xffffff; 1274 vok = VPTR; 1275 break; 1276 case INT64: 1277 val = GET(int64_t); 1278 vok = VSIGN; 1279 break; 1280 case UINT64: 1281 uval = CHK_INF127(GET(u_int64_t)); 1282 vok = VUNSIGN; 1283 break; 1284 1285 case SIGLIST: 1286 default: 1287 /* nothing... */; 1288 } 1289 switch (vok) { 1290 case VSIGN: 1291 if (val < 0 && val < v->longestn) { 1292 v->longestn = val; 1293 fmtlen = iwidth(-val) + 1; 1294 if (fmtlen > v->width) 1295 v->width = fmtlen; 1296 } else if (val > 0 && val > v->longestp) { 1297 v->longestp = val; 1298 fmtlen = iwidth(val); 1299 if (fmtlen > v->width) 1300 v->width = fmtlen; 1301 } 1302 return; 1303 case VUNSIGN: 1304 if (uval > v->longestu) { 1305 v->longestu = uval; 1306 v->width = iwidth(uval); 1307 } 1308 return; 1309 case VPTR: 1310 fmtlen = 0; 1311 while (uval > 0) { 1312 uval >>= 4; 1313 fmtlen++; 1314 } 1315 if (fmtlen > v->width) 1316 v->width = fmtlen; 1317 return; 1318 } 1319 } 1320 1321 width = v->width; 1322 cp = ofmt + 1; 1323 fcp = v->fmt; 1324 if (v->flag & LJUST) 1325 *cp++ = '-'; 1326 *cp++ = '*'; 1327 while ((*cp++ = *fcp++) != '\0') 1328 continue; 1329 1330 switch (v->type) { 1331 case CHAR: 1332 (void)printf(ofmt, width, GET(char)); 1333 return; 1334 case UCHAR: 1335 (void)printf(ofmt, width, CHK_INF127(GET(u_char))); 1336 return; 1337 case SHORT: 1338 (void)printf(ofmt, width, GET(short)); 1339 return; 1340 case USHORT: 1341 (void)printf(ofmt, width, CHK_INF127(GET(u_short))); 1342 return; 1343 case INT: 1344 (void)printf(ofmt, width, GET(int)); 1345 return; 1346 case UINT: 1347 (void)printf(ofmt, width, CHK_INF127(GET(u_int))); 1348 return; 1349 case LONG: 1350 (void)printf(ofmt, width, GET(long)); 1351 return; 1352 case ULONG: 1353 (void)printf(ofmt, width, CHK_INF127(GET(u_long))); 1354 return; 1355 case KPTR: 1356 (void)printf(ofmt, width, GET(u_int64_t)); 1357 return; 1358 case KPTR24: 1359 (void)printf(ofmt, width, GET(u_int64_t) & 0xffffff); 1360 return; 1361 case INT32: 1362 (void)printf(ofmt, width, GET(int32_t)); 1363 return; 1364 case UINT32: 1365 (void)printf(ofmt, width, CHK_INF127(GET(u_int32_t))); 1366 return; 1367 case SIGLIST: 1368 { 1369 sigset_t *s = (sigset_t *)(void *)bp; 1370 size_t i; 1371 #define SIGSETSIZE (sizeof(s->__bits) / sizeof(s->__bits[0])) 1372 char buf[SIGSETSIZE * 8 + 1]; 1373 1374 for (i = 0; i < SIGSETSIZE; i++) 1375 (void)snprintf(&buf[i * 8], 9, "%.8x", 1376 s->__bits[(SIGSETSIZE - 1) - i]); 1377 1378 /* Skip leading zeroes */ 1379 for (i = 0; buf[i] == '0'; i++) 1380 continue; 1381 1382 if (buf[i] == '\0') 1383 i--; 1384 strprintorsetwidth(v, buf + i, mode); 1385 #undef SIGSETSIZE 1386 } 1387 return; 1388 case INT64: 1389 (void)printf(ofmt, width, GET(int64_t)); 1390 return; 1391 case UINT64: 1392 (void)printf(ofmt, width, CHK_INF127(GET(u_int64_t))); 1393 return; 1394 default: 1395 errx(1, "unknown type %d", v->type); 1396 } 1397 #undef GET 1398 #undef CHK_INF127 1399 } 1400 1401 void 1402 pvar(void *arg, VARENT *ve, enum mode mode) 1403 { 1404 VAR *v; 1405 1406 v = ve->var; 1407 if (v->flag & UAREA && !((struct kinfo_proc2 *)arg)->p_uvalid) { 1408 if (mode == PRINTMODE) 1409 (void)printf("%*s", v->width, "-"); 1410 return; 1411 } 1412 1413 (void)printval((char *)arg + v->off, v, mode); 1414 } 1415 1416 void 1417 putimeval(void *arg, VARENT *ve, enum mode mode) 1418 { 1419 VAR *v = ve->var; 1420 struct kinfo_proc2 *k = arg; 1421 ulong secs = *(uint32_t *)((char *)arg + v->off); 1422 ulong usec = *(uint32_t *)((char *)arg + v->off + sizeof (uint32_t)); 1423 int fmtlen; 1424 1425 if (!k->p_uvalid) { 1426 if (mode == PRINTMODE) 1427 (void)printf("%*s", v->width, "-"); 1428 return; 1429 } 1430 1431 if (mode == WIDTHMODE) { 1432 if (secs == 0) 1433 /* non-zero so fmtlen is calculated at least once */ 1434 secs = 1; 1435 if (secs > v->longestu) { 1436 v->longestu = secs; 1437 if (secs <= 999) 1438 /* sss.ssssss */ 1439 fmtlen = iwidth(secs) + 6 + 1; 1440 else 1441 /* hh:mm:ss.ss */ 1442 fmtlen = iwidth((secs + 1) / SECSPERHOUR) 1443 + 2 + 1 + 2 + 1 + 2 + 1; 1444 if (fmtlen > v->width) 1445 v->width = fmtlen; 1446 } 1447 return; 1448 } 1449 1450 if (secs < 999) 1451 (void)printf( "%*lu.%.6lu", v->width - 6 - 1, secs, usec); 1452 else { 1453 uint h, m; 1454 usec += 5000; 1455 if (usec >= 1000000) { 1456 usec -= 1000000; 1457 secs++; 1458 } 1459 m = secs / SECSPERMIN; 1460 secs -= m * SECSPERMIN; 1461 h = m / MINSPERHOUR; 1462 m -= h * MINSPERHOUR; 1463 (void)printf( "%*u:%.2u:%.2lu.%.2lu", v->width - 9, h, m, secs, 1464 usec / 10000u ); 1465 } 1466 } 1467 1468 void 1469 lname(void *arg, VARENT *ve, enum mode mode) 1470 { 1471 struct kinfo_lwp *l; 1472 VAR *v; 1473 1474 l = arg; 1475 v = ve->var; 1476 if (l->l_name[0] != '\0') { 1477 strprintorsetwidth(v, l->l_name, mode); 1478 v->width = min(v->width, KI_LNAMELEN); 1479 } else { 1480 if (mode == PRINTMODE) 1481 (void)printf("%-*s", v->width, "-"); 1482 } 1483 } 1484