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) {
134 (void)printf("%s", k->ki_d.prefix);
135 (void)printf("%s", k->ki_p->ki_comm);
136 if (showthreads && k->ki_p->ki_numthreads > 1)
137 (void)printf("/%s", k->ki_p->ki_ocomm);
139 (void)printf("%-*s", v->width, k->ki_p->ki_comm);
142 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
143 errx(1, "malloc failed");
144 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
146 if (STAILQ_NEXT(ve, next_ve) == NULL) {
150 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1))
152 errx(1, "malloc failed");
153 strvis(vis_env, k->ki_env,
154 VIS_TAB | VIS_NL | VIS_NOSLASH);
158 if (termwidth == UNLIMITED) {
160 (void)printf("%s", k->ki_d.prefix);
162 (void)printf("%s ", vis_env);
163 (void)printf("%s", vis_args);
165 left = termwidth - (totwidth - v->width);
166 if (left < 1) /* already wrapped, just use std width */
168 if ((cp = k->ki_d.prefix) != NULL)
169 while (--left >= 0 && *cp)
170 (void)putchar(*cp++);
171 if ((cp = vis_env) != NULL) {
172 while (--left >= 0 && *cp)
173 (void)putchar(*cp++);
177 for (cp = vis_args; --left >= 0 && *cp != '\0';)
178 (void)putchar(*cp++);
183 /* ki_d.prefix & ki_env aren't shown for interim fields */
184 (void)printf("%-*.*s", v->width, v->width, vis_args);
189 ucomm(KINFO *k, VARENT *ve)
191 char tmpbuff[COMMLEN + OCOMMLEN + 2];
195 if (STAILQ_NEXT(ve, next_ve) == NULL) { /* last field, don't pad */
197 (void)printf("%s", k->ki_d.prefix);
198 (void)printf("%s", k->ki_p->ki_comm);
199 if (showthreads && k->ki_p->ki_numthreads > 1)
200 printf("/%s", k->ki_p->ki_ocomm);
202 bzero(tmpbuff, sizeof(tmpbuff));
203 if (showthreads && k->ki_p->ki_numthreads > 1)
204 sprintf(tmpbuff, "%s/%s", k->ki_p->ki_comm,
207 sprintf(tmpbuff, "%s", k->ki_p->ki_comm);
208 (void)printf("%-*s", v->width, tmpbuff);
213 tdnam(KINFO *k, VARENT *ve)
218 if (showthreads && k->ki_p->ki_numthreads > 1)
219 (void)printf("%-*s", v->width, k->ki_p->ki_ocomm);
221 (void)printf("%-*s", v->width, " ");
225 logname(KINFO *k, VARENT *ve)
231 (void)printf("%-*s", v->width, (s = k->ki_p->ki_login, *s) ? s : "-");
235 state(KINFO *k, VARENT *ve)
243 flag = k->ki_p->ki_flag;
244 tdflags = k->ki_p->ki_tdflags; /* XXXKSE */
247 switch (k->ki_p->ki_stat) {
254 if (tdflags & TDF_SINTR) /* interruptable (long) */
255 *cp = k->ki_p->ki_slptime >= MAXSLP ? 'I' : 'S';
281 if (!(flag & P_INMEM))
283 if (k->ki_p->ki_nice < NZERO)
285 else if (k->ki_p->ki_nice > NZERO)
289 if (flag & P_WEXIT && k->ki_p->ki_stat != SZOMB)
293 if ((flag & P_SYSTEM) || k->ki_p->ki_lock > 0)
295 if (k->ki_p->ki_kiflag & KI_SLEADER)
297 if ((flag & P_CONTROLT) && k->ki_p->ki_pgid == k->ki_p->ki_tpgid)
302 (void)printf("%-*s", v->width, buf);
305 #define scalepri(x) ((x) - PZERO)
308 pri(KINFO *k, VARENT *ve)
313 (void)printf("%*d", v->width, scalepri(k->ki_p->ki_pri.pri_level));
317 upr(KINFO *k, VARENT *ve)
322 (void)printf("%*d", v->width, scalepri(k->ki_p->ki_pri.pri_user));
327 uname(KINFO *k, VARENT *ve)
332 (void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_uid, 0));
338 return (strlen(user_from_uid(k->ki_p->ki_uid, 0)));
342 rgroupname(KINFO *k, VARENT *ve)
347 (void)printf("%-*s", v->width, group_from_gid(k->ki_p->ki_rgid, 0));
351 s_rgroupname(KINFO *k)
353 return (strlen(group_from_gid(k->ki_p->ki_rgid, 0)));
357 runame(KINFO *k, VARENT *ve)
362 (void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_ruid, 0));
368 return (strlen(user_from_uid(k->ki_p->ki_ruid, 0)));
373 tdev(KINFO *k, VARENT *ve)
380 dev = k->ki_p->ki_tdev;
382 (void)printf("%*s", v->width, "??");
384 (void)snprintf(buff, sizeof(buff),
385 "%d/%d", major(dev), minor(dev));
386 (void)printf("%*s", v->width, buff);
391 tname(KINFO *k, VARENT *ve)
398 dev = k->ki_p->ki_tdev;
399 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
400 (void)printf("%*s ", v->width - 1, "??");
402 if (strncmp(ttname, "tty", 3) == 0 ||
403 strncmp(ttname, "cua", 3) == 0)
405 if (strncmp(ttname, "pts/", 4) == 0)
407 (void)printf("%*.*s%c", v->width - 1, v->width - 1, ttname,
408 k->ki_p->ki_kiflag & KI_CTTY ? ' ' : '-');
413 longtname(KINFO *k, VARENT *ve)
420 dev = k->ki_p->ki_tdev;
421 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
422 (void)printf("%-*s", v->width, "??");
424 (void)printf("%-*s", v->width, ttname);
428 started(KINFO *k, VARENT *ve)
433 static int use_ampm = -1;
438 (void)printf("%-*s", v->width, "-");
442 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
443 then = k->ki_p->ki_start.tv_sec;
444 tp = localtime(&then);
445 if (now - k->ki_p->ki_start.tv_sec < 24 * 3600) {
446 (void)strftime(buf, sizeof(buf),
447 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
448 } else if (now - k->ki_p->ki_start.tv_sec < 7 * 86400) {
449 (void)strftime(buf, sizeof(buf),
450 use_ampm ? "%a%I%p" : "%a%H ", tp);
452 (void)strftime(buf, sizeof(buf), "%e%b%y", tp);
453 (void)printf("%-*s", v->width, buf);
457 lstarted(KINFO *k, VARENT *ve)
465 (void)printf("%-*s", v->width, "-");
468 then = k->ki_p->ki_start.tv_sec;
469 (void)strftime(buf, sizeof(buf), "%c", localtime(&then));
470 (void)printf("%-*s", v->width, buf);
474 lockname(KINFO *k, VARENT *ve)
479 if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
480 if (k->ki_p->ki_lockname[0] != 0)
481 (void)printf("%-*.*s", v->width, v->width,
482 k->ki_p->ki_lockname);
484 (void)printf("%-*s", v->width, "???");
486 (void)printf("%-*s", v->width, "-");
490 wchan(KINFO *k, VARENT *ve)
495 if (k->ki_p->ki_wchan) {
496 if (k->ki_p->ki_wmesg[0] != 0)
497 (void)printf("%-*.*s", v->width, v->width,
500 (void)printf("%-*lx", v->width,
501 (long)k->ki_p->ki_wchan);
503 (void)printf("%-*s", v->width, "-");
507 nwchan(KINFO *k, VARENT *ve)
512 if (k->ki_p->ki_wchan) {
513 (void)printf("%0*lx", v->width,
514 (long)k->ki_p->ki_wchan);
516 (void)printf("%-*s", v->width, "-");
520 mwchan(KINFO *k, VARENT *ve)
525 if (k->ki_p->ki_wchan) {
526 if (k->ki_p->ki_wmesg[0] != 0)
527 (void)printf("%-*.*s", v->width, v->width,
530 (void)printf("%-*lx", v->width,
531 (long)k->ki_p->ki_wchan);
532 } else if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
533 if (k->ki_p->ki_lockname[0]) {
534 (void)printf("%-*.*s", v->width, v->width,
535 k->ki_p->ki_lockname);
537 (void)printf("%-*s", v->width, "???");
539 (void)printf("%-*s", v->width, "-");
543 vsize(KINFO *k, VARENT *ve)
548 (void)printf("%*lu", v->width, (u_long)(k->ki_p->ki_size / 1024));
552 cputime(KINFO *k, VARENT *ve)
556 long psecs; /* "parts" of a second. first micro, then centi */
558 static char decimal_point;
560 if (decimal_point == '\0')
561 decimal_point = localeconv()->decimal_point[0];
568 * This counts time spent handling interrupts. We could
569 * fix this, but it is not 100% trivial (and interrupt
570 * time fractions only work on the sparc anyway). XXX
572 secs = k->ki_p->ki_runtime / 1000000;
573 psecs = k->ki_p->ki_runtime % 1000000;
575 secs += k->ki_p->ki_childtime.tv_sec;
576 psecs += k->ki_p->ki_childtime.tv_usec;
579 * round and scale to 100's
581 psecs = (psecs + 5000) / 10000;
585 (void)snprintf(obuff, sizeof(obuff), "%3ld:%02ld%c%02ld",
586 secs / 60, secs % 60, decimal_point, psecs);
587 (void)printf("%*s", v->width, obuff);
591 elapsed(KINFO *k, VARENT *ve)
595 int days, hours, mins, secs;
600 (void)printf("%-*s", v->width, "-");
603 val = now - k->ki_p->ki_start.tv_sec;
604 days = val / (24 * 60 * 60);
606 hours = val / (60 * 60);
611 (void)snprintf(obuff, sizeof(obuff), "%3d-%02d:%02d:%02d",
612 days, hours, mins, secs);
614 (void)snprintf(obuff, sizeof(obuff), "%02d:%02d:%02d",
617 (void)snprintf(obuff, sizeof(obuff), "%02d:%02d", mins, secs);
618 (void)printf("%*s", v->width, obuff);
622 getpcpu(const KINFO *k)
631 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
633 /* XXX - I don't like this */
634 if (k->ki_p->ki_swtime == 0 || (k->ki_p->ki_flag & P_INMEM) == 0)
637 return (100.0 * fxtofl(k->ki_p->ki_pctcpu));
638 return (100.0 * fxtofl(k->ki_p->ki_pctcpu) /
639 (1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu)))));
643 pcpu(KINFO *k, VARENT *ve)
648 (void)printf("%*.1f", v->width, getpcpu(k));
662 if ((k->ki_p->ki_flag & P_INMEM) == 0)
664 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
665 /* XXX don't have info about shared */
666 fracmem = ((float)k->ki_p->ki_rssize) / mempages;
667 return (100.0 * fracmem);
671 pmem(KINFO *k, VARENT *ve)
676 (void)printf("%*.1f", v->width, getpmem(k));
680 pagein(KINFO *k, VARENT *ve)
685 (void)printf("%*ld", v->width,
686 k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0);
691 maxrss(KINFO *k __unused, VARENT *ve)
697 (void)printf("%*s", v->width, "-");
701 priorityr(KINFO *k, VARENT *ve)
704 struct priority *lpri;
706 unsigned class, level;
709 lpri = &k->ki_p->ki_pri;
710 class = lpri->pri_class;
711 level = lpri->pri_level;
714 snprintf(str, sizeof(str), "intr:%u", level);
717 snprintf(str, sizeof(str), "real:%u", level);
720 strncpy(str, "normal", sizeof(str));
723 snprintf(str, sizeof(str), "idle:%u", level);
726 snprintf(str, sizeof(str), "%u:%u", class, level);
729 str[sizeof(str) - 1] = '\0';
730 (void)printf("%*s", v->width, str);
734 * Generic output routines. Print fields from various prototype
738 printval(void *bp, VAR *v)
740 static char ofmt[32] = "%";
749 while ((*cp++ = *fcp++));
751 #define CHKINF127(n) (((n) > 127) && (v->flag & INF127) ? 127 : (n))
755 (void)printf(ofmt, v->width, *(char *)bp);
758 (void)printf(ofmt, v->width, *(u_char *)bp);
761 (void)printf(ofmt, v->width, *(short *)bp);
764 (void)printf(ofmt, v->width, *(u_short *)bp);
767 (void)printf(ofmt, v->width, *(int *)bp);
770 (void)printf(ofmt, v->width, CHKINF127(*(u_int *)bp));
773 (void)printf(ofmt, v->width, *(long *)bp);
776 (void)printf(ofmt, v->width, *(u_long *)bp);
779 (void)printf(ofmt, v->width, *(u_long *)bp);
782 (void)printf(ofmt, v->width, ps_pgtok(*(u_long *)bp));
785 errx(1, "unknown type %d", v->type);
790 kvar(KINFO *k, VARENT *ve)
795 printval((char *)((char *)k->ki_p + v->off), v);
799 rvar(KINFO *k, VARENT *ve)
805 printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v);
807 (void)printf("%*s", v->width, "-");
811 emulname(KINFO *k, VARENT *ve)
816 printf("%-*s", v->width, *k->ki_p->ki_emul ? k->ki_p->ki_emul : "-");
820 label(KINFO *k, VARENT *ve)
829 if (mac_prepare_process_label(&proclabel) == -1) {
830 warn("mac_prepare_process_label");
833 error = mac_get_pid(k->ki_p->ki_pid, proclabel);
835 if (mac_to_text(proclabel, &string) == -1)
840 if (string != NULL) {
841 (void)printf("%-*s", v->width, string);
844 (void)printf("%-*s", v->width, " -");
851 char tmpbuff[COMMLEN + OCOMMLEN + 2];
853 bzero(tmpbuff, sizeof(tmpbuff));
854 if (showthreads && k->ki_p->ki_numthreads > 1)
855 sprintf(tmpbuff, "%s/%s", k->ki_p->ki_comm,
858 sprintf(tmpbuff, "%s", k->ki_p->ki_comm);
859 return (strlen(tmpbuff));
869 if (mac_prepare_process_label(&proclabel) == -1) {
870 warn("mac_prepare_process_label");
873 error = mac_get_pid(k->ki_p->ki_pid, proclabel);
874 if (error == 0 && mac_to_text(proclabel, &string) == 0) {
875 size = strlen(string);