2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
41 #include <sys/resource.h>
47 #include <sys/sysctl.h>
48 #include <sys/vmmeter.h>
67 #define ps_pgtok(a) (((a) * getpagesize()) / 1024)
75 STAILQ_FOREACH(vent, &varlist, next_ve)
76 if (*vent->header != '\0')
81 STAILQ_FOREACH(vent, &varlist, next_ve) {
83 if (v->flag & LJUST) {
84 if (STAILQ_NEXT(vent, next_ve) == NULL) /* last one */
85 (void)printf("%s", vent->header);
87 (void)printf("%-*s", v->width, vent->header);
89 (void)printf("%*s", v->width, vent->header);
90 if (STAILQ_NEXT(vent, next_ve) != NULL)
97 arguments(KINFO *k, VARENT *ve)
104 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
105 errx(1, "malloc failed");
106 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
107 if (STAILQ_NEXT(ve, next_ve) == NULL) {
109 if (termwidth == UNLIMITED) {
110 (void)printf("%s", vis_args);
112 left = termwidth - (totwidth - v->width);
113 if (left < 1) /* already wrapped, just use std width */
115 for (cp = vis_args; --left >= 0 && *cp != '\0';)
116 (void)putchar(*cp++);
119 (void)printf("%-*.*s", v->width, v->width, vis_args);
125 command(KINFO *k, VARENT *ve)
129 char *cp, *vis_env, *vis_args;
133 /* If it is the last field, then don't pad */
134 if (STAILQ_NEXT(ve, next_ve) == NULL) {
136 (void)printf("%s", k->ki_d.prefix);
137 (void)printf("%s", k->ki_p->ki_comm);
138 if (showthreads && k->ki_p->ki_numthreads > 1)
139 (void)printf("/%s", k->ki_p->ki_tdname);
141 (void)printf("%-*s", v->width, k->ki_p->ki_comm);
144 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
145 errx(1, "malloc failed");
146 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
148 if (STAILQ_NEXT(ve, next_ve) == NULL) {
152 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1))
154 errx(1, "malloc failed");
155 strvis(vis_env, k->ki_env,
156 VIS_TAB | VIS_NL | VIS_NOSLASH);
160 if (termwidth == UNLIMITED) {
162 (void)printf("%s", k->ki_d.prefix);
164 (void)printf("%s ", vis_env);
165 (void)printf("%s", vis_args);
167 left = termwidth - (totwidth - v->width);
168 if (left < 1) /* already wrapped, just use std width */
170 if ((cp = k->ki_d.prefix) != NULL)
171 while (--left >= 0 && *cp)
172 (void)putchar(*cp++);
173 if ((cp = vis_env) != NULL) {
174 while (--left >= 0 && *cp)
175 (void)putchar(*cp++);
179 for (cp = vis_args; --left >= 0 && *cp != '\0';)
180 (void)putchar(*cp++);
185 /* ki_d.prefix & ki_env aren't shown for interim fields */
186 (void)printf("%-*.*s", v->width, v->width, vis_args);
191 ucomm(KINFO *k, VARENT *ve)
193 char tmpbuff[COMMLEN + TDNAMLEN + 2];
197 if (STAILQ_NEXT(ve, next_ve) == NULL) { /* last field, don't pad */
199 (void)printf("%s", k->ki_d.prefix);
200 (void)printf("%s", k->ki_p->ki_comm);
201 if (showthreads && k->ki_p->ki_numthreads > 1)
202 printf("/%s", k->ki_p->ki_tdname);
204 bzero(tmpbuff, sizeof(tmpbuff));
205 if (showthreads && k->ki_p->ki_numthreads > 1)
206 sprintf(tmpbuff, "%s/%s", k->ki_p->ki_comm,
209 sprintf(tmpbuff, "%s", k->ki_p->ki_comm);
210 (void)printf("%-*s", v->width, tmpbuff);
215 tdnam(KINFO *k, VARENT *ve)
220 if (showthreads && k->ki_p->ki_numthreads > 1)
221 (void)printf("%-*s", v->width, k->ki_p->ki_tdname);
223 (void)printf("%-*s", v->width, " ");
227 logname(KINFO *k, VARENT *ve)
233 (void)printf("%-*s", v->width, (s = k->ki_p->ki_login, *s) ? s : "-");
237 state(KINFO *k, VARENT *ve)
245 flag = k->ki_p->ki_flag;
246 tdflags = k->ki_p->ki_tdflags; /* XXXKSE */
249 switch (k->ki_p->ki_stat) {
256 if (tdflags & TDF_SINTR) /* interruptable (long) */
257 *cp = k->ki_p->ki_slptime >= MAXSLP ? 'I' : 'S';
283 if (!(flag & P_INMEM))
285 if (k->ki_p->ki_nice < NZERO)
287 else if (k->ki_p->ki_nice > NZERO)
291 if (flag & P_WEXIT && k->ki_p->ki_stat != SZOMB)
295 if ((flag & P_SYSTEM) || k->ki_p->ki_lock > 0)
297 if (k->ki_p->ki_kiflag & KI_SLEADER)
299 if ((flag & P_CONTROLT) && k->ki_p->ki_pgid == k->ki_p->ki_tpgid)
304 (void)printf("%-*s", v->width, buf);
307 #define scalepri(x) ((x) - PZERO)
310 pri(KINFO *k, VARENT *ve)
315 (void)printf("%*d", v->width, scalepri(k->ki_p->ki_pri.pri_level));
319 upr(KINFO *k, VARENT *ve)
324 (void)printf("%*d", v->width, scalepri(k->ki_p->ki_pri.pri_user));
329 uname(KINFO *k, VARENT *ve)
334 (void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_uid, 0));
340 return (strlen(user_from_uid(k->ki_p->ki_uid, 0)));
344 egroupname(KINFO *k, VARENT *ve)
349 (void)printf("%-*s", v->width,
350 group_from_gid(k->ki_p->ki_groups[0], 0));
354 s_egroupname(KINFO *k)
356 return (strlen(group_from_gid(k->ki_p->ki_groups[0], 0)));
360 rgroupname(KINFO *k, VARENT *ve)
365 (void)printf("%-*s", v->width, group_from_gid(k->ki_p->ki_rgid, 0));
369 s_rgroupname(KINFO *k)
371 return (strlen(group_from_gid(k->ki_p->ki_rgid, 0)));
375 runame(KINFO *k, VARENT *ve)
380 (void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_ruid, 0));
386 return (strlen(user_from_uid(k->ki_p->ki_ruid, 0)));
391 tdev(KINFO *k, VARENT *ve)
398 dev = k->ki_p->ki_tdev;
400 (void)printf("%*s", v->width, "??");
402 (void)snprintf(buff, sizeof(buff),
403 "%d/%d", major(dev), minor(dev));
404 (void)printf("%*s", v->width, buff);
409 tname(KINFO *k, VARENT *ve)
416 dev = k->ki_p->ki_tdev;
417 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
418 (void)printf("%*s ", v->width - 1, "??");
420 if (strncmp(ttname, "tty", 3) == 0 ||
421 strncmp(ttname, "cua", 3) == 0)
423 if (strncmp(ttname, "pts/", 4) == 0)
425 (void)printf("%*.*s%c", v->width - 1, v->width - 1, ttname,
426 k->ki_p->ki_kiflag & KI_CTTY ? ' ' : '-');
431 longtname(KINFO *k, VARENT *ve)
438 dev = k->ki_p->ki_tdev;
439 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
440 (void)printf("%-*s", v->width, "??");
442 (void)printf("%-*s", v->width, ttname);
446 started(KINFO *k, VARENT *ve)
451 static int use_ampm = -1;
456 (void)printf("%-*s", v->width, "-");
460 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
461 then = k->ki_p->ki_start.tv_sec;
462 tp = localtime(&then);
463 if (now - k->ki_p->ki_start.tv_sec < 24 * 3600) {
464 (void)strftime(buf, sizeof(buf),
465 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
466 } else if (now - k->ki_p->ki_start.tv_sec < 7 * 86400) {
467 (void)strftime(buf, sizeof(buf),
468 use_ampm ? "%a%I%p" : "%a%H ", tp);
470 (void)strftime(buf, sizeof(buf), "%e%b%y", tp);
471 (void)printf("%-*s", v->width, buf);
475 lstarted(KINFO *k, VARENT *ve)
483 (void)printf("%-*s", v->width, "-");
486 then = k->ki_p->ki_start.tv_sec;
487 (void)strftime(buf, sizeof(buf), "%c", localtime(&then));
488 (void)printf("%-*s", v->width, buf);
492 lockname(KINFO *k, VARENT *ve)
497 if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
498 if (k->ki_p->ki_lockname[0] != 0)
499 (void)printf("%-*.*s", v->width, v->width,
500 k->ki_p->ki_lockname);
502 (void)printf("%-*s", v->width, "???");
504 (void)printf("%-*s", v->width, "-");
508 wchan(KINFO *k, VARENT *ve)
513 if (k->ki_p->ki_wchan) {
514 if (k->ki_p->ki_wmesg[0] != 0)
515 (void)printf("%-*.*s", v->width, v->width,
518 (void)printf("%-*lx", v->width,
519 (long)k->ki_p->ki_wchan);
521 (void)printf("%-*s", v->width, "-");
525 nwchan(KINFO *k, VARENT *ve)
530 if (k->ki_p->ki_wchan) {
531 (void)printf("%0*lx", v->width,
532 (long)k->ki_p->ki_wchan);
534 (void)printf("%-*s", v->width, "-");
538 mwchan(KINFO *k, VARENT *ve)
543 if (k->ki_p->ki_wchan) {
544 if (k->ki_p->ki_wmesg[0] != 0)
545 (void)printf("%-*.*s", v->width, v->width,
548 (void)printf("%-*lx", v->width,
549 (long)k->ki_p->ki_wchan);
550 } else if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
551 if (k->ki_p->ki_lockname[0]) {
552 (void)printf("%-*.*s", v->width, v->width,
553 k->ki_p->ki_lockname);
555 (void)printf("%-*s", v->width, "???");
557 (void)printf("%-*s", v->width, "-");
561 vsize(KINFO *k, VARENT *ve)
566 (void)printf("%*lu", v->width, (u_long)(k->ki_p->ki_size / 1024));
570 printtime(KINFO *k, VARENT *ve, long secs, long psecs)
571 /* psecs is "parts" of a second. first micro, then centi */
575 static char decimal_point;
577 if (decimal_point == '\0')
578 decimal_point = localeconv()->decimal_point[0];
584 /* round and scale to 100's */
585 psecs = (psecs + 5000) / 10000;
589 (void)snprintf(obuff, sizeof(obuff), "%ld:%02ld%c%02ld",
590 secs / 60, secs % 60, decimal_point, psecs);
591 (void)printf("%*s", v->width, obuff);
600 return (log10(secs / 60) + 7);
604 cputime(KINFO *k, VARENT *ve)
609 * This counts time spent handling interrupts. We could
610 * fix this, but it is not 100% trivial (and interrupt
611 * time fractions only work on the sparc anyway). XXX
613 secs = k->ki_p->ki_runtime / 1000000;
614 psecs = k->ki_p->ki_runtime % 1000000;
616 secs += k->ki_p->ki_childtime.tv_sec;
617 psecs += k->ki_p->ki_childtime.tv_usec;
619 printtime(k, ve, secs, psecs);
623 systime(KINFO *k, VARENT *ve)
627 secs = k->ki_p->ki_rusage.ru_stime.tv_sec;
628 psecs = k->ki_p->ki_rusage.ru_stime.tv_usec;
630 secs += k->ki_p->ki_childstime.tv_sec;
631 psecs += k->ki_p->ki_childstime.tv_usec;
633 printtime(k, ve, secs, psecs);
637 usertime(KINFO *k, VARENT *ve)
641 secs = k->ki_p->ki_rusage.ru_utime.tv_sec;
642 psecs = k->ki_p->ki_rusage.ru_utime.tv_usec;
644 secs += k->ki_p->ki_childutime.tv_sec;
645 psecs += k->ki_p->ki_childutime.tv_usec;
647 printtime(k, ve, secs, psecs);
651 elapsed(KINFO *k, VARENT *ve)
655 int days, hours, mins, secs;
660 (void)printf("%-*s", v->width, "-");
663 val = now - k->ki_p->ki_start.tv_sec;
664 days = val / (24 * 60 * 60);
666 hours = val / (60 * 60);
671 (void)snprintf(obuff, sizeof(obuff), "%3d-%02d:%02d:%02d",
672 days, hours, mins, secs);
674 (void)snprintf(obuff, sizeof(obuff), "%02d:%02d:%02d",
677 (void)snprintf(obuff, sizeof(obuff), "%02d:%02d", mins, secs);
678 (void)printf("%*s", v->width, obuff);
682 elapseds(KINFO *k, VARENT *ve)
689 (void)printf("%-*s", v->width, "-");
692 val = now - k->ki_p->ki_start.tv_sec;
693 (void)printf("%*jd", v->width, (intmax_t)val);
697 getpcpu(const KINFO *k)
706 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
708 /* XXX - I don't like this */
709 if (k->ki_p->ki_swtime == 0 || (k->ki_p->ki_flag & P_INMEM) == 0)
712 return (100.0 * fxtofl(k->ki_p->ki_pctcpu));
713 return (100.0 * fxtofl(k->ki_p->ki_pctcpu) /
714 (1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu)))));
718 pcpu(KINFO *k, VARENT *ve)
723 (void)printf("%*.1f", v->width, getpcpu(k));
737 if ((k->ki_p->ki_flag & P_INMEM) == 0)
739 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
740 /* XXX don't have info about shared */
741 fracmem = ((float)k->ki_p->ki_rssize) / mempages;
742 return (100.0 * fracmem);
746 pmem(KINFO *k, VARENT *ve)
751 (void)printf("%*.1f", v->width, getpmem(k));
755 pagein(KINFO *k, VARENT *ve)
760 (void)printf("%*ld", v->width,
761 k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0);
766 maxrss(KINFO *k __unused, VARENT *ve)
772 (void)printf("%*s", v->width, "-");
776 priorityr(KINFO *k, VARENT *ve)
779 struct priority *lpri;
781 unsigned class, level;
784 lpri = &k->ki_p->ki_pri;
785 class = lpri->pri_class;
786 level = lpri->pri_level;
789 snprintf(str, sizeof(str), "intr:%u", level);
792 snprintf(str, sizeof(str), "real:%u", level);
795 strncpy(str, "normal", sizeof(str));
798 snprintf(str, sizeof(str), "idle:%u", level);
801 snprintf(str, sizeof(str), "%u:%u", class, level);
804 str[sizeof(str) - 1] = '\0';
805 (void)printf("%*s", v->width, str);
809 * Generic output routines. Print fields from various prototype
813 printval(void *bp, VAR *v)
815 static char ofmt[32] = "%";
824 while ((*cp++ = *fcp++));
826 #define CHKINF127(n) (((n) > 127) && (v->flag & INF127) ? 127 : (n))
830 (void)printf(ofmt, v->width, *(char *)bp);
833 (void)printf(ofmt, v->width, *(u_char *)bp);
836 (void)printf(ofmt, v->width, *(short *)bp);
839 (void)printf(ofmt, v->width, *(u_short *)bp);
842 (void)printf(ofmt, v->width, *(int *)bp);
845 (void)printf(ofmt, v->width, CHKINF127(*(u_int *)bp));
848 (void)printf(ofmt, v->width, *(long *)bp);
851 (void)printf(ofmt, v->width, *(u_long *)bp);
854 (void)printf(ofmt, v->width, *(u_long *)bp);
857 (void)printf(ofmt, v->width, ps_pgtok(*(u_long *)bp));
860 errx(1, "unknown type %d", v->type);
865 kvar(KINFO *k, VARENT *ve)
870 printval((char *)((char *)k->ki_p + v->off), v);
874 rvar(KINFO *k, VARENT *ve)
880 printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v);
882 (void)printf("%*s", v->width, "-");
886 emulname(KINFO *k, VARENT *ve)
891 printf("%-*s", v->width, *k->ki_p->ki_emul ? k->ki_p->ki_emul : "-");
895 label(KINFO *k, VARENT *ve)
904 if (mac_prepare_process_label(&proclabel) == -1) {
905 warn("mac_prepare_process_label");
908 error = mac_get_pid(k->ki_p->ki_pid, proclabel);
910 if (mac_to_text(proclabel, &string) == -1)
915 if (string != NULL) {
916 (void)printf("%-*s", v->width, string);
919 (void)printf("%-*s", v->width, " -");
924 loginclass(KINFO *k, VARENT *ve)
931 * Don't display login class for system processes;
932 * login classes are used for resource limits,
933 * and limits don't apply to system processes.
935 if (k->ki_p->ki_flag & P_SYSTEM) {
936 (void)printf("%-*s", v->width, "-");
939 s = k->ki_p->ki_loginclass;
940 (void)printf("%-*s", v->width, *s ? s : "-");
946 char tmpbuff[COMMLEN + TDNAMLEN + 2];
948 bzero(tmpbuff, sizeof(tmpbuff));
949 if (showthreads && k->ki_p->ki_numthreads > 1)
950 sprintf(tmpbuff, "%s/%s", k->ki_p->ki_comm,
953 sprintf(tmpbuff, "%s", k->ki_p->ki_comm);
954 return (strlen(tmpbuff));
962 secs = k->ki_p->ki_runtime / 1000000;
964 secs += k->ki_p->ki_childtime.tv_sec;
965 return (sizetime(secs));
975 if (mac_prepare_process_label(&proclabel) == -1) {
976 warn("mac_prepare_process_label");
979 error = mac_get_pid(k->ki_p->ki_pid, proclabel);
980 if (error == 0 && mac_to_text(proclabel, &string) == 0) {
981 size = strlen(string);
989 s_loginclass(KINFO *k)
993 if (k->ki_p->ki_flag & P_SYSTEM)
996 s = k->ki_p->ki_loginclass;
1008 s = k->ki_p->ki_login;
1020 secs = k->ki_p->ki_rusage.ru_stime.tv_sec;
1022 secs += k->ki_p->ki_childstime.tv_sec;
1023 return (sizetime(secs));
1027 s_usertime(KINFO *k)
1031 secs = k->ki_p->ki_rusage.ru_utime.tv_sec;
1033 secs += k->ki_p->ki_childutime.tv_sec;
1034 return (sizetime(secs));