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)
200 int flag, sflag, tdflags;
206 flag = k->ki_p->ki_flag;
207 sflag = k->ki_p->ki_sflag;
208 tdflags = k->ki_p->ki_tdflags; /* XXXKSE */
211 switch (k->ki_p->ki_stat) {
218 if (tdflags & TDF_SINTR) /* interruptable (long) */
219 *cp = k->ki_p->ki_slptime >= MAXSLP ? 'I' : 'S';
245 if (!(sflag & PS_INMEM))
247 if (k->ki_p->ki_nice < NZERO)
249 else if (k->ki_p->ki_nice > NZERO)
253 if (flag & P_WEXIT && k->ki_p->ki_stat != SZOMB)
257 if ((flag & P_SYSTEM) || k->ki_p->ki_lock > 0)
259 if (k->ki_p->ki_kiflag & KI_SLEADER)
261 if ((flag & P_CONTROLT) && k->ki_p->ki_pgid == k->ki_p->ki_tpgid)
266 (void)printf("%-*s", v->width, buf);
269 #define scalepri(x) ((x) - PZERO)
272 pri(KINFO *k, VARENT *ve)
277 (void)printf("%*d", v->width, scalepri(k->ki_p->ki_pri.pri_level));
281 upr(KINFO *k, VARENT *ve)
286 (void)printf("%*d", v->width, scalepri(k->ki_p->ki_pri.pri_user));
291 uname(KINFO *k, VARENT *ve)
296 (void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_uid, 0));
302 return (strlen(user_from_uid(k->ki_p->ki_uid, 0)));
306 rgroupname(KINFO *k, VARENT *ve)
311 (void)printf("%-*s", v->width, group_from_gid(k->ki_p->ki_rgid, 0));
315 s_rgroupname(KINFO *k)
317 return (strlen(group_from_gid(k->ki_p->ki_rgid, 0)));
321 runame(KINFO *k, VARENT *ve)
326 (void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_ruid, 0));
332 return (strlen(user_from_uid(k->ki_p->ki_ruid, 0)));
337 tdev(KINFO *k, VARENT *ve)
344 dev = k->ki_p->ki_tdev;
346 (void)printf("%*s", v->width, "??");
348 (void)snprintf(buff, sizeof(buff),
349 "%d/%d", major(dev), minor(dev));
350 (void)printf("%*s", v->width, buff);
355 tname(KINFO *k, VARENT *ve)
362 dev = k->ki_p->ki_tdev;
363 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
364 (void)printf("%*s ", v->width - 1, "??");
366 if (strncmp(ttname, "tty", 3) == 0 ||
367 strncmp(ttname, "cua", 3) == 0)
369 (void)printf("%*.*s%c", v->width - 1, v->width - 1, ttname,
370 k->ki_p->ki_kiflag & KI_CTTY ? ' ' : '-');
375 longtname(KINFO *k, VARENT *ve)
382 dev = k->ki_p->ki_tdev;
383 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
384 (void)printf("%-*s", v->width, "??");
386 (void)printf("%-*s", v->width, ttname);
390 started(KINFO *k, VARENT *ve)
395 static int use_ampm = -1;
400 (void)printf("%-*s", v->width, "-");
404 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
405 then = k->ki_p->ki_start.tv_sec;
406 tp = localtime(&then);
407 if (now - k->ki_p->ki_start.tv_sec < 24 * 3600) {
408 (void)strftime(buf, sizeof(buf),
409 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
410 } else if (now - k->ki_p->ki_start.tv_sec < 7 * 86400) {
411 (void)strftime(buf, sizeof(buf),
412 use_ampm ? "%a%I%p" : "%a%H ", tp);
414 (void)strftime(buf, sizeof(buf), "%e%b%y", tp);
415 (void)printf("%-*s", v->width, buf);
419 lstarted(KINFO *k, VARENT *ve)
427 (void)printf("%-*s", v->width, "-");
430 then = k->ki_p->ki_start.tv_sec;
431 (void)strftime(buf, sizeof(buf), "%c", localtime(&then));
432 (void)printf("%-*s", v->width, buf);
436 lockname(KINFO *k, VARENT *ve)
441 if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
442 if (k->ki_p->ki_lockname[0] != 0)
443 (void)printf("%-*.*s", v->width, v->width,
444 k->ki_p->ki_lockname);
446 (void)printf("%-*s", v->width, "???");
448 (void)printf("%-*s", v->width, "-");
452 wchan(KINFO *k, VARENT *ve)
457 if (k->ki_p->ki_wchan) {
458 if (k->ki_p->ki_wmesg[0] != 0)
459 (void)printf("%-*.*s", v->width, v->width,
462 (void)printf("%-*lx", v->width,
463 (long)k->ki_p->ki_wchan);
465 (void)printf("%-*s", v->width, "-");
469 nwchan(KINFO *k, VARENT *ve)
474 if (k->ki_p->ki_wchan) {
475 (void)printf("%0*lx", v->width,
476 (long)k->ki_p->ki_wchan);
478 (void)printf("%-*s", v->width, "-");
482 mwchan(KINFO *k, VARENT *ve)
487 if (k->ki_p->ki_wchan) {
488 if (k->ki_p->ki_wmesg[0] != 0)
489 (void)printf("%-*.*s", v->width, v->width,
492 (void)printf("%-*lx", v->width,
493 (long)k->ki_p->ki_wchan);
494 } else if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
495 if (k->ki_p->ki_lockname[0]) {
496 (void)printf("%-*.*s", v->width, v->width,
497 k->ki_p->ki_lockname);
499 (void)printf("%-*s", v->width, "???");
501 (void)printf("%-*s", v->width, "-");
505 vsize(KINFO *k, VARENT *ve)
510 (void)printf("%*lu", v->width, (u_long)(k->ki_p->ki_size / 1024));
514 cputime(KINFO *k, VARENT *ve)
518 long psecs; /* "parts" of a second. first micro, then centi */
520 static char decimal_point;
522 if (decimal_point == '\0')
523 decimal_point = localeconv()->decimal_point[0];
530 * This counts time spent handling interrupts. We could
531 * fix this, but it is not 100% trivial (and interrupt
532 * time fractions only work on the sparc anyway). XXX
534 secs = k->ki_p->ki_runtime / 1000000;
535 psecs = k->ki_p->ki_runtime % 1000000;
537 secs += k->ki_p->ki_childtime.tv_sec;
538 psecs += k->ki_p->ki_childtime.tv_usec;
541 * round and scale to 100's
543 psecs = (psecs + 5000) / 10000;
547 (void)snprintf(obuff, sizeof(obuff), "%3ld:%02ld%c%02ld",
548 secs / 60, secs % 60, decimal_point, psecs);
549 (void)printf("%*s", v->width, obuff);
553 elapsed(KINFO *k, VARENT *ve)
557 int days, hours, mins, secs;
561 val = now - k->ki_p->ki_start.tv_sec;
562 days = val / (24 * 60 * 60);
564 hours = val / (60 * 60);
569 (void)snprintf(obuff, sizeof(obuff), "%3d-%02d:%02d:%02d",
570 days, hours, mins, secs);
572 (void)snprintf(obuff, sizeof(obuff), "%02d:%02d:%02d",
575 (void)snprintf(obuff, sizeof(obuff), "%02d:%02d", mins, secs);
576 (void)printf("%*s", v->width, obuff);
580 getpcpu(const KINFO *k)
589 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
591 /* XXX - I don't like this */
592 if (k->ki_p->ki_swtime == 0 || (k->ki_p->ki_sflag & PS_INMEM) == 0)
595 return (100.0 * fxtofl(k->ki_p->ki_pctcpu));
596 return (100.0 * fxtofl(k->ki_p->ki_pctcpu) /
597 (1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu)))));
601 pcpu(KINFO *k, VARENT *ve)
606 (void)printf("%*.1f", v->width, getpcpu(k));
620 if ((k->ki_p->ki_sflag & PS_INMEM) == 0)
622 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
623 /* XXX don't have info about shared */
624 fracmem = ((float)k->ki_p->ki_rssize) / mempages;
625 return (100.0 * fracmem);
629 pmem(KINFO *k, VARENT *ve)
634 (void)printf("%*.1f", v->width, getpmem(k));
638 pagein(KINFO *k, VARENT *ve)
643 (void)printf("%*ld", v->width,
644 k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0);
649 maxrss(KINFO *k __unused, VARENT *ve)
655 (void)printf("%*s", v->width, "-");
659 priorityr(KINFO *k, VARENT *ve)
662 struct priority *lpri;
664 unsigned class, level;
667 lpri = &k->ki_p->ki_pri;
668 class = lpri->pri_class;
669 level = lpri->pri_level;
672 snprintf(str, sizeof(str), "intr:%u", level);
675 snprintf(str, sizeof(str), "real:%u", level);
678 strncpy(str, "normal", sizeof(str));
681 snprintf(str, sizeof(str), "idle:%u", level);
684 snprintf(str, sizeof(str), "%u:%u", class, level);
687 str[sizeof(str) - 1] = '\0';
688 (void)printf("%*s", v->width, str);
692 * Generic output routines. Print fields from various prototype
696 printval(void *bp, VAR *v)
698 static char ofmt[32] = "%";
707 while ((*cp++ = *fcp++));
709 #define CHKINF127(n) (((n) > 127) && (v->flag & INF127) ? 127 : (n))
713 (void)printf(ofmt, v->width, *(char *)bp);
716 (void)printf(ofmt, v->width, *(u_char *)bp);
719 (void)printf(ofmt, v->width, *(short *)bp);
722 (void)printf(ofmt, v->width, *(u_short *)bp);
725 (void)printf(ofmt, v->width, *(int *)bp);
728 (void)printf(ofmt, v->width, CHKINF127(*(u_int *)bp));
731 (void)printf(ofmt, v->width, *(long *)bp);
734 (void)printf(ofmt, v->width, *(u_long *)bp);
737 (void)printf(ofmt, v->width, *(u_long *)bp);
740 (void)printf(ofmt, v->width, ps_pgtok(*(u_long *)bp));
743 errx(1, "unknown type %d", v->type);
748 kvar(KINFO *k, VARENT *ve)
753 printval((char *)((char *)k->ki_p + v->off), v);
757 rvar(KINFO *k, VARENT *ve)
763 printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v);
765 (void)printf("%*s", v->width, "-");
769 emulname(KINFO *k, VARENT *ve)
774 printf("%-*s", v->width, *k->ki_p->ki_emul ? k->ki_p->ki_emul : "-");
778 label(KINFO *k, VARENT *ve)
787 if (mac_prepare_process_label(&proclabel) == -1) {
788 warn("mac_prepare_process_label");
791 error = mac_get_pid(k->ki_p->ki_pid, proclabel);
793 if (mac_to_text(proclabel, &string) == -1)
798 if (string != NULL) {
799 (void)printf("%-*s", v->width, string);
802 (void)printf("%-*s", v->width, " -");
813 if (mac_prepare_process_label(&proclabel) == -1) {
814 warn("mac_prepare_process_label");
817 error = mac_get_pid(k->ki_p->ki_pid, proclabel);
818 if (error == 0 && mac_to_text(proclabel, &string) == 0) {
819 size = strlen(string);