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>
65 #define ps_pgtok(a) (((a) * getpagesize()) / 1024)
73 STAILQ_FOREACH(vent, &varlist, next_ve)
74 if (*vent->header != '\0')
79 STAILQ_FOREACH(vent, &varlist, next_ve) {
81 if (v->flag & LJUST) {
82 if (STAILQ_NEXT(vent, next_ve) == NULL) /* last one */
83 (void)printf("%s", vent->header);
85 (void)printf("%-*s", v->width, vent->header);
87 (void)printf("%*s", v->width, vent->header);
88 if (STAILQ_NEXT(vent, next_ve) != NULL)
95 arguments(KINFO *k, VARENT *ve)
102 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
103 errx(1, "malloc failed");
104 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
105 if (STAILQ_NEXT(ve, next_ve) == NULL) {
107 if (termwidth == UNLIMITED) {
108 (void)printf("%s", vis_args);
110 left = termwidth - (totwidth - v->width);
111 if (left < 1) /* already wrapped, just use std width */
113 for (cp = vis_args; --left >= 0 && *cp != '\0';)
114 (void)putchar(*cp++);
117 (void)printf("%-*.*s", v->width, v->width, vis_args);
123 command(KINFO *k, VARENT *ve)
127 char *cp, *vis_env, *vis_args;
131 /* If it is the last field, then don't pad */
132 if (STAILQ_NEXT(ve, next_ve) == NULL)
133 (void)printf("%s", k->ki_p->ki_comm);
135 (void)printf("%-*s", v->width, k->ki_p->ki_comm);
138 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
139 errx(1, "malloc failed");
140 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
142 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
143 errx(1, "malloc failed");
144 strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
148 if (STAILQ_NEXT(ve, next_ve) == NULL) {
150 if (termwidth == UNLIMITED) {
152 (void)printf("%s ", vis_env);
153 (void)printf("%s", vis_args);
155 left = termwidth - (totwidth - v->width);
156 if (left < 1) /* already wrapped, just use std width */
158 if ((cp = vis_env) != NULL) {
159 while (--left >= 0 && *cp)
160 (void)putchar(*cp++);
164 for (cp = vis_args; --left >= 0 && *cp != '\0';)
165 (void)putchar(*cp++);
169 (void)printf("%-*.*s", v->width, v->width, vis_args);
176 ucomm(KINFO *k, VARENT *ve)
181 if (STAILQ_NEXT(ve, next_ve) == NULL) /* last field, don't pad */
182 (void)printf("%s", k->ki_p->ki_comm);
184 (void)printf("%-*s", v->width, k->ki_p->ki_comm);
188 logname(KINFO *k, VARENT *ve)
194 (void)printf("%-*s", v->width, (s = k->ki_p->ki_login, *s) ? s : "-");
198 state(KINFO *k, VARENT *ve)
206 flag = k->ki_p->ki_flag;
207 tdflags = k->ki_p->ki_tdflags; /* XXXKSE */
210 switch (k->ki_p->ki_stat) {
217 if (tdflags & TDF_SINTR) /* interruptable (long) */
218 *cp = k->ki_p->ki_slptime >= MAXSLP ? 'I' : 'S';
244 if (!(flag & P_INMEM))
246 if (k->ki_p->ki_nice < NZERO)
248 else if (k->ki_p->ki_nice > NZERO)
252 if (flag & P_WEXIT && k->ki_p->ki_stat != SZOMB)
256 if ((flag & P_SYSTEM) || k->ki_p->ki_lock > 0)
258 if (k->ki_p->ki_kiflag & KI_SLEADER)
260 if ((flag & P_CONTROLT) && k->ki_p->ki_pgid == k->ki_p->ki_tpgid)
265 (void)printf("%-*s", v->width, buf);
268 #define scalepri(x) ((x) - PZERO)
271 pri(KINFO *k, VARENT *ve)
276 (void)printf("%*d", v->width, scalepri(k->ki_p->ki_pri.pri_level));
280 upr(KINFO *k, VARENT *ve)
285 (void)printf("%*d", v->width, scalepri(k->ki_p->ki_pri.pri_user));
290 uname(KINFO *k, VARENT *ve)
295 (void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_uid, 0));
301 return (strlen(user_from_uid(k->ki_p->ki_uid, 0)));
305 rgroupname(KINFO *k, VARENT *ve)
310 (void)printf("%-*s", v->width, group_from_gid(k->ki_p->ki_rgid, 0));
314 s_rgroupname(KINFO *k)
316 return (strlen(group_from_gid(k->ki_p->ki_rgid, 0)));
320 runame(KINFO *k, VARENT *ve)
325 (void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_ruid, 0));
331 return (strlen(user_from_uid(k->ki_p->ki_ruid, 0)));
336 tdev(KINFO *k, VARENT *ve)
343 dev = k->ki_p->ki_tdev;
345 (void)printf("%*s", v->width, "??");
347 (void)snprintf(buff, sizeof(buff),
348 "%d/%d", major(dev), minor(dev));
349 (void)printf("%*s", v->width, buff);
354 tname(KINFO *k, VARENT *ve)
361 dev = k->ki_p->ki_tdev;
362 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
363 (void)printf("%*s ", v->width - 1, "??");
365 if (strncmp(ttname, "tty", 3) == 0 ||
366 strncmp(ttname, "cua", 3) == 0)
368 if (strncmp(ttname, "pts/", 4) == 0)
370 (void)printf("%*.*s%c", v->width - 1, v->width - 1, ttname,
371 k->ki_p->ki_kiflag & KI_CTTY ? ' ' : '-');
376 longtname(KINFO *k, VARENT *ve)
383 dev = k->ki_p->ki_tdev;
384 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
385 (void)printf("%-*s", v->width, "??");
387 (void)printf("%-*s", v->width, ttname);
391 started(KINFO *k, VARENT *ve)
396 static int use_ampm = -1;
401 (void)printf("%-*s", v->width, "-");
405 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
406 then = k->ki_p->ki_start.tv_sec;
407 tp = localtime(&then);
408 if (now - k->ki_p->ki_start.tv_sec < 24 * 3600) {
409 (void)strftime(buf, sizeof(buf),
410 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
411 } else if (now - k->ki_p->ki_start.tv_sec < 7 * 86400) {
412 (void)strftime(buf, sizeof(buf),
413 use_ampm ? "%a%I%p" : "%a%H ", tp);
415 (void)strftime(buf, sizeof(buf), "%e%b%y", tp);
416 (void)printf("%-*s", v->width, buf);
420 lstarted(KINFO *k, VARENT *ve)
428 (void)printf("%-*s", v->width, "-");
431 then = k->ki_p->ki_start.tv_sec;
432 (void)strftime(buf, sizeof(buf), "%c", localtime(&then));
433 (void)printf("%-*s", v->width, buf);
437 lockname(KINFO *k, VARENT *ve)
442 if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
443 if (k->ki_p->ki_lockname[0] != 0)
444 (void)printf("%-*.*s", v->width, v->width,
445 k->ki_p->ki_lockname);
447 (void)printf("%-*s", v->width, "???");
449 (void)printf("%-*s", v->width, "-");
453 wchan(KINFO *k, VARENT *ve)
458 if (k->ki_p->ki_wchan) {
459 if (k->ki_p->ki_wmesg[0] != 0)
460 (void)printf("%-*.*s", v->width, v->width,
463 (void)printf("%-*lx", v->width,
464 (long)k->ki_p->ki_wchan);
466 (void)printf("%-*s", v->width, "-");
470 nwchan(KINFO *k, VARENT *ve)
475 if (k->ki_p->ki_wchan) {
476 (void)printf("%0*lx", v->width,
477 (long)k->ki_p->ki_wchan);
479 (void)printf("%-*s", v->width, "-");
483 mwchan(KINFO *k, VARENT *ve)
488 if (k->ki_p->ki_wchan) {
489 if (k->ki_p->ki_wmesg[0] != 0)
490 (void)printf("%-*.*s", v->width, v->width,
493 (void)printf("%-*lx", v->width,
494 (long)k->ki_p->ki_wchan);
495 } else if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
496 if (k->ki_p->ki_lockname[0]) {
497 (void)printf("%-*.*s", v->width, v->width,
498 k->ki_p->ki_lockname);
500 (void)printf("%-*s", v->width, "???");
502 (void)printf("%-*s", v->width, "-");
506 vsize(KINFO *k, VARENT *ve)
511 (void)printf("%*lu", v->width, (u_long)(k->ki_p->ki_size / 1024));
515 cputime(KINFO *k, VARENT *ve)
519 long psecs; /* "parts" of a second. first micro, then centi */
521 static char decimal_point;
523 if (decimal_point == '\0')
524 decimal_point = localeconv()->decimal_point[0];
531 * This counts time spent handling interrupts. We could
532 * fix this, but it is not 100% trivial (and interrupt
533 * time fractions only work on the sparc anyway). XXX
535 secs = k->ki_p->ki_runtime / 1000000;
536 psecs = k->ki_p->ki_runtime % 1000000;
538 secs += k->ki_p->ki_childtime.tv_sec;
539 psecs += k->ki_p->ki_childtime.tv_usec;
542 * round and scale to 100's
544 psecs = (psecs + 5000) / 10000;
548 (void)snprintf(obuff, sizeof(obuff), "%3ld:%02ld%c%02ld",
549 secs / 60, secs % 60, decimal_point, psecs);
550 (void)printf("%*s", v->width, obuff);
554 elapsed(KINFO *k, VARENT *ve)
558 int days, hours, mins, secs;
562 val = now - k->ki_p->ki_start.tv_sec;
563 days = val / (24 * 60 * 60);
565 hours = val / (60 * 60);
570 (void)snprintf(obuff, sizeof(obuff), "%3d-%02d:%02d:%02d",
571 days, hours, mins, secs);
573 (void)snprintf(obuff, sizeof(obuff), "%02d:%02d:%02d",
576 (void)snprintf(obuff, sizeof(obuff), "%02d:%02d", mins, secs);
577 (void)printf("%*s", v->width, obuff);
581 getpcpu(const KINFO *k)
590 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
592 /* XXX - I don't like this */
593 if (k->ki_p->ki_swtime == 0 || (k->ki_p->ki_flag & P_INMEM) == 0)
596 return (100.0 * fxtofl(k->ki_p->ki_pctcpu));
597 return (100.0 * fxtofl(k->ki_p->ki_pctcpu) /
598 (1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu)))));
602 pcpu(KINFO *k, VARENT *ve)
607 (void)printf("%*.1f", v->width, getpcpu(k));
621 if ((k->ki_p->ki_flag & P_INMEM) == 0)
623 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
624 /* XXX don't have info about shared */
625 fracmem = ((float)k->ki_p->ki_rssize) / mempages;
626 return (100.0 * fracmem);
630 pmem(KINFO *k, VARENT *ve)
635 (void)printf("%*.1f", v->width, getpmem(k));
639 pagein(KINFO *k, VARENT *ve)
644 (void)printf("%*ld", v->width,
645 k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0);
650 maxrss(KINFO *k __unused, VARENT *ve)
656 (void)printf("%*s", v->width, "-");
660 priorityr(KINFO *k, VARENT *ve)
663 struct priority *lpri;
665 unsigned class, level;
668 lpri = &k->ki_p->ki_pri;
669 class = lpri->pri_class;
670 level = lpri->pri_level;
673 snprintf(str, sizeof(str), "intr:%u", level);
676 snprintf(str, sizeof(str), "real:%u", level);
679 strncpy(str, "normal", sizeof(str));
682 snprintf(str, sizeof(str), "idle:%u", level);
685 snprintf(str, sizeof(str), "%u:%u", class, level);
688 str[sizeof(str) - 1] = '\0';
689 (void)printf("%*s", v->width, str);
693 * Generic output routines. Print fields from various prototype
697 printval(void *bp, VAR *v)
699 static char ofmt[32] = "%";
708 while ((*cp++ = *fcp++));
710 #define CHKINF127(n) (((n) > 127) && (v->flag & INF127) ? 127 : (n))
714 (void)printf(ofmt, v->width, *(char *)bp);
717 (void)printf(ofmt, v->width, *(u_char *)bp);
720 (void)printf(ofmt, v->width, *(short *)bp);
723 (void)printf(ofmt, v->width, *(u_short *)bp);
726 (void)printf(ofmt, v->width, *(int *)bp);
729 (void)printf(ofmt, v->width, CHKINF127(*(u_int *)bp));
732 (void)printf(ofmt, v->width, *(long *)bp);
735 (void)printf(ofmt, v->width, *(u_long *)bp);
738 (void)printf(ofmt, v->width, *(u_long *)bp);
741 (void)printf(ofmt, v->width, ps_pgtok(*(u_long *)bp));
744 errx(1, "unknown type %d", v->type);
749 kvar(KINFO *k, VARENT *ve)
754 printval((char *)((char *)k->ki_p + v->off), v);
758 rvar(KINFO *k, VARENT *ve)
764 printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v);
766 (void)printf("%*s", v->width, "-");
770 emulname(KINFO *k, VARENT *ve)
775 printf("%-*s", v->width, *k->ki_p->ki_emul ? k->ki_p->ki_emul : "-");
779 label(KINFO *k, VARENT *ve)
788 if (mac_prepare_process_label(&proclabel) == -1) {
789 warn("mac_prepare_process_label");
792 error = mac_get_pid(k->ki_p->ki_pid, proclabel);
794 if (mac_to_text(proclabel, &string) == -1)
799 if (string != NULL) {
800 (void)printf("%-*s", v->width, string);
803 (void)printf("%-*s", v->width, " -");
814 if (mac_prepare_process_label(&proclabel) == -1) {
815 warn("mac_prepare_process_label");
818 error = mac_get_pid(k->ki_p->ki_pid, proclabel);
819 if (error == 0 && mac_to_text(proclabel, &string) == 0) {
820 size = strlen(string);