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>
66 #define ps_pgtok(a) (((a) * getpagesize()) / 1024)
74 STAILQ_FOREACH(vent, &varlist, next_ve)
75 if (*vent->header != '\0')
80 STAILQ_FOREACH(vent, &varlist, next_ve) {
82 if (v->flag & LJUST) {
83 if (STAILQ_NEXT(vent, next_ve) == NULL) /* last one */
84 (void)printf("%s", vent->header);
86 (void)printf("%-*s", v->width, vent->header);
88 (void)printf("%*s", v->width, vent->header);
89 if (STAILQ_NEXT(vent, next_ve) != NULL)
96 arguments(KINFO *k, VARENT *ve)
103 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
104 errx(1, "malloc failed");
105 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
106 if (STAILQ_NEXT(ve, next_ve) == NULL) {
108 if (termwidth == UNLIMITED) {
109 (void)printf("%s", vis_args);
111 left = termwidth - (totwidth - v->width);
112 if (left < 1) /* already wrapped, just use std width */
114 for (cp = vis_args; --left >= 0 && *cp != '\0';)
115 (void)putchar(*cp++);
118 (void)printf("%-*.*s", v->width, v->width, vis_args);
124 command(KINFO *k, VARENT *ve)
128 char *cp, *vis_env, *vis_args;
132 /* If it is the last field, then don't pad */
133 if (STAILQ_NEXT(ve, next_ve) == NULL) {
135 (void)printf("%s", k->ki_d.prefix);
136 (void)printf("%s", k->ki_p->ki_comm);
137 if (showthreads && k->ki_p->ki_numthreads > 1)
138 (void)printf("/%s", k->ki_p->ki_ocomm);
140 (void)printf("%-*s", v->width, k->ki_p->ki_comm);
143 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
144 errx(1, "malloc failed");
145 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
147 if (STAILQ_NEXT(ve, next_ve) == NULL) {
151 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1))
153 errx(1, "malloc failed");
154 strvis(vis_env, k->ki_env,
155 VIS_TAB | VIS_NL | VIS_NOSLASH);
159 if (termwidth == UNLIMITED) {
161 (void)printf("%s", k->ki_d.prefix);
163 (void)printf("%s ", vis_env);
164 (void)printf("%s", vis_args);
166 left = termwidth - (totwidth - v->width);
167 if (left < 1) /* already wrapped, just use std width */
169 if ((cp = k->ki_d.prefix) != NULL)
170 while (--left >= 0 && *cp)
171 (void)putchar(*cp++);
172 if ((cp = vis_env) != NULL) {
173 while (--left >= 0 && *cp)
174 (void)putchar(*cp++);
178 for (cp = vis_args; --left >= 0 && *cp != '\0';)
179 (void)putchar(*cp++);
184 /* ki_d.prefix & ki_env aren't shown for interim fields */
185 (void)printf("%-*.*s", v->width, v->width, vis_args);
190 ucomm(KINFO *k, VARENT *ve)
192 char tmpbuff[COMMLEN + OCOMMLEN + 2];
196 if (STAILQ_NEXT(ve, next_ve) == NULL) { /* last field, don't pad */
198 (void)printf("%s", k->ki_d.prefix);
199 (void)printf("%s", k->ki_p->ki_comm);
200 if (showthreads && k->ki_p->ki_numthreads > 1)
201 printf("/%s", k->ki_p->ki_ocomm);
203 bzero(tmpbuff, sizeof(tmpbuff));
204 if (showthreads && k->ki_p->ki_numthreads > 1)
205 sprintf(tmpbuff, "%s/%s", k->ki_p->ki_comm,
208 sprintf(tmpbuff, "%s", k->ki_p->ki_comm);
209 (void)printf("%-*s", v->width, tmpbuff);
214 tdnam(KINFO *k, VARENT *ve)
219 if (showthreads && k->ki_p->ki_numthreads > 1)
220 (void)printf("%-*s", v->width, k->ki_p->ki_ocomm);
222 (void)printf("%-*s", v->width, " ");
226 logname(KINFO *k, VARENT *ve)
232 (void)printf("%-*s", v->width, (s = k->ki_p->ki_login, *s) ? s : "-");
236 state(KINFO *k, VARENT *ve)
244 flag = k->ki_p->ki_flag;
245 tdflags = k->ki_p->ki_tdflags; /* XXXKSE */
248 switch (k->ki_p->ki_stat) {
255 if (tdflags & TDF_SINTR) /* interruptable (long) */
256 *cp = k->ki_p->ki_slptime >= MAXSLP ? 'I' : 'S';
282 if (!(flag & P_INMEM))
284 if (k->ki_p->ki_nice < NZERO)
286 else if (k->ki_p->ki_nice > NZERO)
290 if (flag & P_WEXIT && k->ki_p->ki_stat != SZOMB)
294 if ((flag & P_SYSTEM) || k->ki_p->ki_lock > 0)
296 if (k->ki_p->ki_kiflag & KI_SLEADER)
298 if ((flag & P_CONTROLT) && k->ki_p->ki_pgid == k->ki_p->ki_tpgid)
303 (void)printf("%-*s", v->width, buf);
306 #define scalepri(x) ((x) - PZERO)
309 pri(KINFO *k, VARENT *ve)
314 (void)printf("%*d", v->width, scalepri(k->ki_p->ki_pri.pri_level));
318 upr(KINFO *k, VARENT *ve)
323 (void)printf("%*d", v->width, scalepri(k->ki_p->ki_pri.pri_user));
328 uname(KINFO *k, VARENT *ve)
333 (void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_uid, 0));
339 return (strlen(user_from_uid(k->ki_p->ki_uid, 0)));
343 rgroupname(KINFO *k, VARENT *ve)
348 (void)printf("%-*s", v->width, group_from_gid(k->ki_p->ki_rgid, 0));
352 s_rgroupname(KINFO *k)
354 return (strlen(group_from_gid(k->ki_p->ki_rgid, 0)));
358 runame(KINFO *k, VARENT *ve)
363 (void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_ruid, 0));
369 return (strlen(user_from_uid(k->ki_p->ki_ruid, 0)));
374 tdev(KINFO *k, VARENT *ve)
381 dev = k->ki_p->ki_tdev;
383 (void)printf("%*s", v->width, "??");
385 (void)snprintf(buff, sizeof(buff),
386 "%d/%d", major(dev), minor(dev));
387 (void)printf("%*s", v->width, buff);
392 tname(KINFO *k, VARENT *ve)
399 dev = k->ki_p->ki_tdev;
400 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
401 (void)printf("%*s ", v->width - 1, "??");
403 if (strncmp(ttname, "tty", 3) == 0 ||
404 strncmp(ttname, "cua", 3) == 0)
406 if (strncmp(ttname, "pts/", 4) == 0)
408 (void)printf("%*.*s%c", v->width - 1, v->width - 1, ttname,
409 k->ki_p->ki_kiflag & KI_CTTY ? ' ' : '-');
414 longtname(KINFO *k, VARENT *ve)
421 dev = k->ki_p->ki_tdev;
422 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
423 (void)printf("%-*s", v->width, "??");
425 (void)printf("%-*s", v->width, ttname);
429 started(KINFO *k, VARENT *ve)
434 static int use_ampm = -1;
439 (void)printf("%-*s", v->width, "-");
443 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
444 then = k->ki_p->ki_start.tv_sec;
445 tp = localtime(&then);
446 if (now - k->ki_p->ki_start.tv_sec < 24 * 3600) {
447 (void)strftime(buf, sizeof(buf),
448 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
449 } else if (now - k->ki_p->ki_start.tv_sec < 7 * 86400) {
450 (void)strftime(buf, sizeof(buf),
451 use_ampm ? "%a%I%p" : "%a%H ", tp);
453 (void)strftime(buf, sizeof(buf), "%e%b%y", tp);
454 (void)printf("%-*s", v->width, buf);
458 lstarted(KINFO *k, VARENT *ve)
466 (void)printf("%-*s", v->width, "-");
469 then = k->ki_p->ki_start.tv_sec;
470 (void)strftime(buf, sizeof(buf), "%c", localtime(&then));
471 (void)printf("%-*s", v->width, buf);
475 lockname(KINFO *k, VARENT *ve)
480 if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
481 if (k->ki_p->ki_lockname[0] != 0)
482 (void)printf("%-*.*s", v->width, v->width,
483 k->ki_p->ki_lockname);
485 (void)printf("%-*s", v->width, "???");
487 (void)printf("%-*s", v->width, "-");
491 wchan(KINFO *k, VARENT *ve)
496 if (k->ki_p->ki_wchan) {
497 if (k->ki_p->ki_wmesg[0] != 0)
498 (void)printf("%-*.*s", v->width, v->width,
501 (void)printf("%-*lx", v->width,
502 (long)k->ki_p->ki_wchan);
504 (void)printf("%-*s", v->width, "-");
508 nwchan(KINFO *k, VARENT *ve)
513 if (k->ki_p->ki_wchan) {
514 (void)printf("%0*lx", v->width,
515 (long)k->ki_p->ki_wchan);
517 (void)printf("%-*s", v->width, "-");
521 mwchan(KINFO *k, VARENT *ve)
526 if (k->ki_p->ki_wchan) {
527 if (k->ki_p->ki_wmesg[0] != 0)
528 (void)printf("%-*.*s", v->width, v->width,
531 (void)printf("%-*lx", v->width,
532 (long)k->ki_p->ki_wchan);
533 } else if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
534 if (k->ki_p->ki_lockname[0]) {
535 (void)printf("%-*.*s", v->width, v->width,
536 k->ki_p->ki_lockname);
538 (void)printf("%-*s", v->width, "???");
540 (void)printf("%-*s", v->width, "-");
544 vsize(KINFO *k, VARENT *ve)
549 (void)printf("%*lu", v->width, (u_long)(k->ki_p->ki_size / 1024));
553 cputime(KINFO *k, VARENT *ve)
557 long psecs; /* "parts" of a second. first micro, then centi */
559 static char decimal_point;
561 if (decimal_point == '\0')
562 decimal_point = localeconv()->decimal_point[0];
569 * This counts time spent handling interrupts. We could
570 * fix this, but it is not 100% trivial (and interrupt
571 * time fractions only work on the sparc anyway). XXX
573 secs = k->ki_p->ki_runtime / 1000000;
574 psecs = k->ki_p->ki_runtime % 1000000;
576 secs += k->ki_p->ki_childtime.tv_sec;
577 psecs += k->ki_p->ki_childtime.tv_usec;
580 * round and scale to 100's
582 psecs = (psecs + 5000) / 10000;
586 (void)snprintf(obuff, sizeof(obuff), "%3ld:%02ld%c%02ld",
587 secs / 60, secs % 60, decimal_point, psecs);
588 (void)printf("%*s", v->width, obuff);
592 elapsed(KINFO *k, VARENT *ve)
596 int days, hours, mins, secs;
601 (void)printf("%-*s", v->width, "-");
604 val = now - k->ki_p->ki_start.tv_sec;
605 days = val / (24 * 60 * 60);
607 hours = val / (60 * 60);
612 (void)snprintf(obuff, sizeof(obuff), "%3d-%02d:%02d:%02d",
613 days, hours, mins, secs);
615 (void)snprintf(obuff, sizeof(obuff), "%02d:%02d:%02d",
618 (void)snprintf(obuff, sizeof(obuff), "%02d:%02d", mins, secs);
619 (void)printf("%*s", v->width, obuff);
623 elapseds(KINFO *k, VARENT *ve)
630 (void)printf("%-*s", v->width, "-");
633 val = now - k->ki_p->ki_start.tv_sec;
634 (void)printf("%*jd", v->width, (intmax_t)val);
638 getpcpu(const KINFO *k)
647 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
649 /* XXX - I don't like this */
650 if (k->ki_p->ki_swtime == 0 || (k->ki_p->ki_flag & P_INMEM) == 0)
653 return (100.0 * fxtofl(k->ki_p->ki_pctcpu));
654 return (100.0 * fxtofl(k->ki_p->ki_pctcpu) /
655 (1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu)))));
659 pcpu(KINFO *k, VARENT *ve)
664 (void)printf("%*.1f", v->width, getpcpu(k));
678 if ((k->ki_p->ki_flag & P_INMEM) == 0)
680 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
681 /* XXX don't have info about shared */
682 fracmem = ((float)k->ki_p->ki_rssize) / mempages;
683 return (100.0 * fracmem);
687 pmem(KINFO *k, VARENT *ve)
692 (void)printf("%*.1f", v->width, getpmem(k));
696 pagein(KINFO *k, VARENT *ve)
701 (void)printf("%*ld", v->width,
702 k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0);
707 maxrss(KINFO *k __unused, VARENT *ve)
713 (void)printf("%*s", v->width, "-");
717 priorityr(KINFO *k, VARENT *ve)
720 struct priority *lpri;
722 unsigned class, level;
725 lpri = &k->ki_p->ki_pri;
726 class = lpri->pri_class;
727 level = lpri->pri_level;
730 snprintf(str, sizeof(str), "intr:%u", level);
733 snprintf(str, sizeof(str), "real:%u", level);
736 strncpy(str, "normal", sizeof(str));
739 snprintf(str, sizeof(str), "idle:%u", level);
742 snprintf(str, sizeof(str), "%u:%u", class, level);
745 str[sizeof(str) - 1] = '\0';
746 (void)printf("%*s", v->width, str);
750 * Generic output routines. Print fields from various prototype
754 printval(void *bp, VAR *v)
756 static char ofmt[32] = "%";
765 while ((*cp++ = *fcp++));
767 #define CHKINF127(n) (((n) > 127) && (v->flag & INF127) ? 127 : (n))
771 (void)printf(ofmt, v->width, *(char *)bp);
774 (void)printf(ofmt, v->width, *(u_char *)bp);
777 (void)printf(ofmt, v->width, *(short *)bp);
780 (void)printf(ofmt, v->width, *(u_short *)bp);
783 (void)printf(ofmt, v->width, *(int *)bp);
786 (void)printf(ofmt, v->width, CHKINF127(*(u_int *)bp));
789 (void)printf(ofmt, v->width, *(long *)bp);
792 (void)printf(ofmt, v->width, *(u_long *)bp);
795 (void)printf(ofmt, v->width, *(u_long *)bp);
798 (void)printf(ofmt, v->width, ps_pgtok(*(u_long *)bp));
801 errx(1, "unknown type %d", v->type);
806 kvar(KINFO *k, VARENT *ve)
811 printval((char *)((char *)k->ki_p + v->off), v);
815 rvar(KINFO *k, VARENT *ve)
821 printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v);
823 (void)printf("%*s", v->width, "-");
827 emulname(KINFO *k, VARENT *ve)
832 printf("%-*s", v->width, *k->ki_p->ki_emul ? k->ki_p->ki_emul : "-");
836 label(KINFO *k, VARENT *ve)
845 if (mac_prepare_process_label(&proclabel) == -1) {
846 warn("mac_prepare_process_label");
849 error = mac_get_pid(k->ki_p->ki_pid, proclabel);
851 if (mac_to_text(proclabel, &string) == -1)
856 if (string != NULL) {
857 (void)printf("%-*s", v->width, string);
860 (void)printf("%-*s", v->width, " -");
867 char tmpbuff[COMMLEN + OCOMMLEN + 2];
869 bzero(tmpbuff, sizeof(tmpbuff));
870 if (showthreads && k->ki_p->ki_numthreads > 1)
871 sprintf(tmpbuff, "%s/%s", k->ki_p->ki_comm,
874 sprintf(tmpbuff, "%s", k->ki_p->ki_comm);
875 return (strlen(tmpbuff));
885 if (mac_prepare_process_label(&proclabel) == -1) {
886 warn("mac_prepare_process_label");
889 error = mac_get_pid(k->ki_p->ki_pid, proclabel);
890 if (error == 0 && mac_to_text(proclabel, &string) == 0) {
891 size = strlen(string);