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>
68 #define COMMAND_WIDTH 16
69 #define ARGUMENTS_WIDTH 16
71 #define ps_pgtok(a) (((a) * getpagesize()) / 1024)
79 STAILQ_FOREACH(vent, &varlist, next_ve)
80 if (*vent->header != '\0')
85 STAILQ_FOREACH(vent, &varlist, next_ve) {
87 if (v->flag & LJUST) {
88 if (STAILQ_NEXT(vent, next_ve) == NULL) /* last one */
89 xo_emit("{T:/%s}", vent->header);
91 xo_emit("{T:/%-*s}", v->width, vent->header);
93 xo_emit("{T:/%*s}", v->width, vent->header);
94 if (STAILQ_NEXT(vent, next_ve) != NULL)
101 arguments(KINFO *k, VARENT *ve)
105 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
106 xo_errx(1, "malloc failed");
107 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
109 if (STAILQ_NEXT(ve, next_ve) != NULL && strlen(vis_args) > ARGUMENTS_WIDTH)
110 vis_args[ARGUMENTS_WIDTH] = '\0';
116 command(KINFO *k, VARENT *ve)
118 char *vis_args, *vis_env, *str;
121 /* If it is the last field, then don't pad */
122 if (STAILQ_NEXT(ve, next_ve) == NULL) {
123 asprintf(&str, "%s%s%s%s",
124 k->ki_d.prefix ? k->ki_d.prefix : "",
126 (showthreads && k->ki_p->ki_numthreads > 1) ? "/" : "",
127 (showthreads && k->ki_p->ki_numthreads > 1) ? k->ki_p->ki_tdname : "");
129 str = strdup(k->ki_p->ki_comm);
133 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
134 xo_errx(1, "malloc failed");
135 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
137 if (STAILQ_NEXT(ve, next_ve) == NULL) {
141 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1))
143 xo_errx(1, "malloc failed");
144 strvis(vis_env, k->ki_env,
145 VIS_TAB | VIS_NL | VIS_NOSLASH);
149 asprintf(&str, "%s%s%s%s",
150 k->ki_d.prefix ? k->ki_d.prefix : "",
151 vis_env ? vis_env : "",
159 /* ki_d.prefix & ki_env aren't shown for interim fields */
162 if (strlen(str) > COMMAND_WIDTH)
163 str[COMMAND_WIDTH] = '\0';
170 ucomm(KINFO *k, VARENT *ve)
174 if (STAILQ_NEXT(ve, next_ve) == NULL) { /* last field, don't pad */
175 asprintf(&str, "%s%s%s%s",
176 k->ki_d.prefix ? k->ki_d.prefix : "",
178 (showthreads && k->ki_p->ki_numthreads > 1) ? "/" : "",
179 (showthreads && k->ki_p->ki_numthreads > 1) ? k->ki_p->ki_tdname : "");
181 if (showthreads && k->ki_p->ki_numthreads > 1)
182 asprintf(&str, "%s/%s", k->ki_p->ki_comm, k->ki_p->ki_tdname);
184 str = strdup(k->ki_p->ki_comm);
190 tdnam(KINFO *k, VARENT *ve __unused)
194 if (showthreads && k->ki_p->ki_numthreads > 1)
195 str = strdup(k->ki_p->ki_tdname);
203 logname(KINFO *k, VARENT *ve __unused)
206 if (*k->ki_p->ki_login == '\0')
208 return (strdup(k->ki_p->ki_login));
212 state(KINFO *k, VARENT *ve __unused)
219 xo_errx(1, "malloc failed");
221 flag = k->ki_p->ki_flag;
222 tdflags = k->ki_p->ki_tdflags; /* XXXKSE */
225 switch (k->ki_p->ki_stat) {
232 if (tdflags & TDF_SINTR) /* interruptable (long) */
233 *cp = k->ki_p->ki_slptime >= MAXSLP ? 'I' : 'S';
259 if (!(flag & P_INMEM))
261 if (k->ki_p->ki_nice < NZERO)
263 else if (k->ki_p->ki_nice > NZERO)
267 if (flag & P_WEXIT && k->ki_p->ki_stat != SZOMB)
271 if ((flag & P_SYSTEM) || k->ki_p->ki_lock > 0)
273 if (k->ki_p->ki_kiflag & KI_SLEADER)
275 if ((flag & P_CONTROLT) && k->ki_p->ki_pgid == k->ki_p->ki_tpgid)
283 #define scalepri(x) ((x) - PZERO)
286 pri(KINFO *k, VARENT *ve __unused)
290 asprintf(&str, "%d", scalepri(k->ki_p->ki_pri.pri_level));
295 upr(KINFO *k, VARENT *ve __unused)
299 asprintf(&str, "%d", scalepri(k->ki_p->ki_pri.pri_user));
305 uname(KINFO *k, VARENT *ve __unused)
308 return (strdup(user_from_uid(k->ki_p->ki_uid, 0)));
312 egroupname(KINFO *k, VARENT *ve __unused)
315 return (strdup(group_from_gid(k->ki_p->ki_groups[0], 0)));
319 rgroupname(KINFO *k, VARENT *ve __unused)
322 return (strdup(group_from_gid(k->ki_p->ki_rgid, 0)));
326 runame(KINFO *k, VARENT *ve __unused)
329 return (strdup(user_from_uid(k->ki_p->ki_ruid, 0)));
333 tdev(KINFO *k, VARENT *ve __unused)
338 dev = k->ki_p->ki_tdev;
342 asprintf(&str, "%#jx", (uintmax_t)dev);
348 tname(KINFO *k, VARENT *ve __unused)
353 dev = k->ki_p->ki_tdev;
354 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
357 if (strncmp(ttname, "tty", 3) == 0 ||
358 strncmp(ttname, "cua", 3) == 0)
360 if (strncmp(ttname, "pts/", 4) == 0)
362 asprintf(&str, "%s%c", ttname,
363 k->ki_p->ki_kiflag & KI_CTTY ? ' ' : '-');
370 longtname(KINFO *k, VARENT *ve __unused)
375 dev = k->ki_p->ki_tdev;
376 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
379 return (strdup(ttname));
383 started(KINFO *k, VARENT *ve __unused)
393 buf = malloc(buflen);
395 xo_errx(1, "malloc failed");
397 then = k->ki_p->ki_start.tv_sec;
398 tp = localtime(&then);
399 if (now - k->ki_p->ki_start.tv_sec < 24 * 3600) {
400 (void)strftime(buf, buflen, "%H:%M ", tp);
401 } else if (now - k->ki_p->ki_start.tv_sec < 7 * 86400) {
402 (void)strftime(buf, buflen, "%a%H ", tp);
404 (void)strftime(buf, buflen, "%e%b%y", tp);
409 lstarted(KINFO *k, VARENT *ve __unused)
418 buf = malloc(buflen);
420 xo_errx(1, "malloc failed");
422 then = k->ki_p->ki_start.tv_sec;
423 (void)strftime(buf, buflen, "%c", localtime(&then));
428 lockname(KINFO *k, VARENT *ve __unused)
432 if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
433 if (k->ki_p->ki_lockname[0] != 0)
434 str = strdup(k->ki_p->ki_lockname);
444 wchan(KINFO *k, VARENT *ve __unused)
448 if (k->ki_p->ki_wchan) {
449 if (k->ki_p->ki_wmesg[0] != 0)
450 str = strdup(k->ki_p->ki_wmesg);
452 asprintf(&str, "%lx", (long)k->ki_p->ki_wchan);
460 nwchan(KINFO *k, VARENT *ve __unused)
464 if (k->ki_p->ki_wchan)
465 asprintf(&str, "%0lx", (long)k->ki_p->ki_wchan);
473 mwchan(KINFO *k, VARENT *ve __unused)
477 if (k->ki_p->ki_wchan) {
478 if (k->ki_p->ki_wmesg[0] != 0)
479 str = strdup(k->ki_p->ki_wmesg);
481 asprintf(&str, "%lx", (long)k->ki_p->ki_wchan);
482 } else if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
483 if (k->ki_p->ki_lockname[0]) {
484 str = strdup(k->ki_p->ki_lockname);
494 vsize(KINFO *k, VARENT *ve __unused)
498 asprintf(&str, "%lu", (u_long)(k->ki_p->ki_size / 1024));
503 printtime(KINFO *k, VARENT *ve __unused, long secs, long psecs)
504 /* psecs is "parts" of a second. first micro, then centi */
506 static char decimal_point;
509 if (decimal_point == '\0')
510 decimal_point = localeconv()->decimal_point[0];
515 /* round and scale to 100's */
516 psecs = (psecs + 5000) / 10000;
520 asprintf(&str, "%ld:%02ld%c%02ld",
521 secs / 60, secs % 60, decimal_point, psecs);
526 cputime(KINFO *k, VARENT *ve)
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;
541 return (printtime(k, ve, secs, psecs));
545 systime(KINFO *k, VARENT *ve)
549 secs = k->ki_p->ki_rusage.ru_stime.tv_sec;
550 psecs = k->ki_p->ki_rusage.ru_stime.tv_usec;
552 secs += k->ki_p->ki_childstime.tv_sec;
553 psecs += k->ki_p->ki_childstime.tv_usec;
555 return (printtime(k, ve, secs, psecs));
559 usertime(KINFO *k, VARENT *ve)
563 secs = k->ki_p->ki_rusage.ru_utime.tv_sec;
564 psecs = k->ki_p->ki_rusage.ru_utime.tv_usec;
566 secs += k->ki_p->ki_childutime.tv_sec;
567 psecs += k->ki_p->ki_childutime.tv_usec;
569 return (printtime(k, ve, secs, psecs));
573 elapsed(KINFO *k, VARENT *ve __unused)
576 int days, hours, mins, secs;
581 val = now - k->ki_p->ki_start.tv_sec;
582 days = val / (24 * 60 * 60);
584 hours = val / (60 * 60);
589 asprintf(&str, "%3d-%02d:%02d:%02d", days, hours, mins, secs);
591 asprintf(&str, "%02d:%02d:%02d", hours, mins, secs);
593 asprintf(&str, "%02d:%02d", mins, secs);
599 elapseds(KINFO *k, VARENT *ve __unused)
606 val = now - k->ki_p->ki_start.tv_sec;
607 asprintf(&str, "%jd", (intmax_t)val);
612 getpcpu(const KINFO *k)
621 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
623 /* XXX - I don't like this */
624 if (k->ki_p->ki_swtime == 0 || (k->ki_p->ki_flag & P_INMEM) == 0)
627 return (100.0 * fxtofl(k->ki_p->ki_pctcpu));
628 return (100.0 * fxtofl(k->ki_p->ki_pctcpu) /
629 (1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu)))));
633 pcpu(KINFO *k, VARENT *ve __unused)
637 asprintf(&str, "%.1f", getpcpu(k));
652 if ((k->ki_p->ki_flag & P_INMEM) == 0)
654 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
655 /* XXX don't have info about shared */
656 fracmem = ((float)k->ki_p->ki_rssize) / mempages;
657 return (100.0 * fracmem);
661 pmem(KINFO *k, VARENT *ve __unused)
665 asprintf(&str, "%.1f", getpmem(k));
670 pagein(KINFO *k, VARENT *ve __unused)
674 asprintf(&str, "%ld", k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0);
680 maxrss(KINFO *k __unused, VARENT *ve __unused)
688 priorityr(KINFO *k, VARENT *ve __unused)
690 struct priority *lpri;
692 unsigned class, level;
694 lpri = &k->ki_p->ki_pri;
695 class = lpri->pri_class;
696 level = lpri->pri_level;
699 asprintf(&str, "intr:%u", level);
702 asprintf(&str, "real:%u", level);
705 asprintf(&str, "normal");
708 asprintf(&str, "idle:%u", level);
711 asprintf(&str, "%u:%u", class, level);
718 * Generic output routines. Print fields from various prototype
722 printval(void *bp, VAR *v)
724 static char ofmt[32] = "%";
730 while ((*cp++ = *fcp++));
732 #define CHKINF127(n) (((n) > 127) && (v->flag & INF127) ? 127 : (n))
736 (void)asprintf(&str, ofmt, *(char *)bp);
739 (void)asprintf(&str, ofmt, *(u_char *)bp);
742 (void)asprintf(&str, ofmt, *(short *)bp);
745 (void)asprintf(&str, ofmt, *(u_short *)bp);
748 (void)asprintf(&str, ofmt, *(int *)bp);
751 (void)asprintf(&str, ofmt, CHKINF127(*(u_int *)bp));
754 (void)asprintf(&str, ofmt, *(long *)bp);
757 (void)asprintf(&str, ofmt, *(u_long *)bp);
760 (void)asprintf(&str, ofmt, *(u_long *)bp);
763 (void)asprintf(&str, ofmt, ps_pgtok(*(u_long *)bp));
766 xo_errx(1, "unknown type %d", v->type);
773 kvar(KINFO *k, VARENT *ve)
778 return (printval((char *)((char *)k->ki_p + v->off), v));
782 rvar(KINFO *k, VARENT *ve)
789 return (printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v));
793 emulname(KINFO *k, VARENT *ve __unused)
796 return (strdup(k->ki_p->ki_emul));
800 label(KINFO *k, VARENT *ve __unused)
807 if (mac_prepare_process_label(&proclabel) == -1) {
808 xo_warn("mac_prepare_process_label");
811 error = mac_get_pid(k->ki_p->ki_pid, proclabel);
813 if (mac_to_text(proclabel, &string) == -1)
822 loginclass(KINFO *k, VARENT *ve __unused)
826 * Don't display login class for system processes;
827 * login classes are used for resource limits,
828 * and limits don't apply to system processes.
830 if (k->ki_p->ki_flag & P_SYSTEM) {
831 return (strdup("-"));
833 return (strdup(k->ki_p->ki_loginclass));