xref: /freebsd/bin/ps/print.c (revision 7304f61f9e409b45a8d69036ef22f351fbd680d8)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1990, 1993, 1994
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
64b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
74b88c807SRodney W. Grimes  * are met:
84b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
94b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
104b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
114b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
124b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
134b88c807SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
144b88c807SRodney W. Grimes  *    must display the following acknowledgement:
154b88c807SRodney W. Grimes  *	This product includes software developed by the University of
164b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
174b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
184b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
194b88c807SRodney W. Grimes  *    without specific prior written permission.
204b88c807SRodney W. Grimes  *
214b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
224b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
234b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
244b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
254b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
264b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
274b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
284b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
294b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
304b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
314b88c807SRodney W. Grimes  * SUCH DAMAGE.
324b88c807SRodney W. Grimes  */
334b88c807SRodney W. Grimes 
344b88c807SRodney W. Grimes #ifndef lint
35c9a8d1f4SPhilippe Charnier #if 0
36c9a8d1f4SPhilippe Charnier static char sccsid[] = "@(#)print.c	8.6 (Berkeley) 4/16/94";
37c9a8d1f4SPhilippe Charnier #endif
38c9a8d1f4SPhilippe Charnier static const char rcsid[] =
392a456239SPeter Wemm   "$FreeBSD$";
404b88c807SRodney W. Grimes #endif /* not lint */
414b88c807SRodney W. Grimes 
424b88c807SRodney W. Grimes #include <sys/param.h>
434b88c807SRodney W. Grimes #include <sys/time.h>
444b88c807SRodney W. Grimes #include <sys/resource.h>
454b88c807SRodney W. Grimes #include <sys/proc.h>
464b88c807SRodney W. Grimes #include <sys/stat.h>
474b88c807SRodney W. Grimes 
484b88c807SRodney W. Grimes #include <sys/ucred.h>
498b9b0e39SPoul-Henning Kamp #include <sys/user.h>
504b88c807SRodney W. Grimes #include <sys/sysctl.h>
514b88c807SRodney W. Grimes #include <vm/vm.h>
524b88c807SRodney W. Grimes 
534b88c807SRodney W. Grimes #include <err.h>
54f59105eeSAndrey A. Chernov #include <langinfo.h>
558073a93cSAndrey A. Chernov #include <locale.h>
564b88c807SRodney W. Grimes #include <math.h>
574b88c807SRodney W. Grimes #include <nlist.h>
584b88c807SRodney W. Grimes #include <stddef.h>
594b88c807SRodney W. Grimes #include <stdio.h>
604b88c807SRodney W. Grimes #include <stdlib.h>
610164b6d6SPeter Wemm #include <unistd.h>
624b88c807SRodney W. Grimes #include <string.h>
634b88c807SRodney W. Grimes #include <vis.h>
644b88c807SRodney W. Grimes 
654b88c807SRodney W. Grimes #include "ps.h"
664b88c807SRodney W. Grimes 
674b88c807SRodney W. Grimes void
684b88c807SRodney W. Grimes printheader()
694b88c807SRodney W. Grimes {
704b88c807SRodney W. Grimes 	VAR *v;
714b88c807SRodney W. Grimes 	struct varent *vent;
724b88c807SRodney W. Grimes 
734b88c807SRodney W. Grimes 	for (vent = vhead; vent; vent = vent->next) {
744b88c807SRodney W. Grimes 		v = vent->var;
754b88c807SRodney W. Grimes 		if (v->flag & LJUST) {
764b88c807SRodney W. Grimes 			if (vent->next == NULL)	/* last one */
774b88c807SRodney W. Grimes 				(void)printf("%s", v->header);
784b88c807SRodney W. Grimes 			else
794b88c807SRodney W. Grimes 				(void)printf("%-*s", v->width, v->header);
804b88c807SRodney W. Grimes 		} else
814b88c807SRodney W. Grimes 			(void)printf("%*s", v->width, v->header);
824b88c807SRodney W. Grimes 		if (vent->next != NULL)
834b88c807SRodney W. Grimes 			(void)putchar(' ');
844b88c807SRodney W. Grimes 	}
854b88c807SRodney W. Grimes 	(void)putchar('\n');
864b88c807SRodney W. Grimes }
874b88c807SRodney W. Grimes 
884b88c807SRodney W. Grimes void
894b88c807SRodney W. Grimes command(k, ve)
904b88c807SRodney W. Grimes 	KINFO *k;
914b88c807SRodney W. Grimes 	VARENT *ve;
924b88c807SRodney W. Grimes {
934b88c807SRodney W. Grimes 	VAR *v;
944b88c807SRodney W. Grimes 	int left;
954b88c807SRodney W. Grimes 	char *cp, *vis_env, *vis_args;
964b88c807SRodney W. Grimes 
97db91faacSPeter Wemm 	v = ve->var;
98db91faacSPeter Wemm 
99db91faacSPeter Wemm 	if (cflag) {
1003cc273e0SJohn Polstra 		if (ve->next == NULL)	/* last field, don't pad */
1011f7d2501SKirk McKusick 			(void)printf("%s", k->ki_p->ki_comm);
1023cc273e0SJohn Polstra 		else
1031f7d2501SKirk McKusick 			(void)printf("%-*s", v->width, k->ki_p->ki_comm);
104db91faacSPeter Wemm 		return;
105db91faacSPeter Wemm 	}
106db91faacSPeter Wemm 
1074b88c807SRodney W. Grimes 	if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
1084b88c807SRodney W. Grimes 		err(1, NULL);
1094b88c807SRodney W. Grimes 	strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
1104b88c807SRodney W. Grimes 	if (k->ki_env) {
1114b88c807SRodney W. Grimes 		if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
1124b88c807SRodney W. Grimes 			err(1, NULL);
1134b88c807SRodney W. Grimes 		strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
1144b88c807SRodney W. Grimes 	} else
1154b88c807SRodney W. Grimes 		vis_env = NULL;
1164b88c807SRodney W. Grimes 
1174b88c807SRodney W. Grimes 	if (ve->next == NULL) {
1184b88c807SRodney W. Grimes 		/* last field */
1194b88c807SRodney W. Grimes 		if (termwidth == UNLIMITED) {
1204b88c807SRodney W. Grimes 			if (vis_env)
1214b88c807SRodney W. Grimes 				(void)printf("%s ", vis_env);
1224b88c807SRodney W. Grimes 			(void)printf("%s", vis_args);
1234b88c807SRodney W. Grimes 		} else {
1244b88c807SRodney W. Grimes 			left = termwidth - (totwidth - v->width);
1254b88c807SRodney W. Grimes 			if (left < 1) /* already wrapped, just use std width */
1264b88c807SRodney W. Grimes 				left = v->width;
1274b88c807SRodney W. Grimes 			if ((cp = vis_env) != NULL) {
1284b88c807SRodney W. Grimes 				while (--left >= 0 && *cp)
1294b88c807SRodney W. Grimes 					(void)putchar(*cp++);
1304b88c807SRodney W. Grimes 				if (--left >= 0)
1314b88c807SRodney W. Grimes 					putchar(' ');
1324b88c807SRodney W. Grimes 			}
1334b88c807SRodney W. Grimes 			for (cp = vis_args; --left >= 0 && *cp != '\0';)
1344b88c807SRodney W. Grimes 				(void)putchar(*cp++);
1354b88c807SRodney W. Grimes 		}
1364b88c807SRodney W. Grimes 	} else
1374b88c807SRodney W. Grimes 		/* XXX env? */
1384b88c807SRodney W. Grimes 		(void)printf("%-*.*s", v->width, v->width, vis_args);
1394b88c807SRodney W. Grimes 	free(vis_args);
1404b88c807SRodney W. Grimes 	if (vis_env != NULL)
1414b88c807SRodney W. Grimes 		free(vis_env);
1424b88c807SRodney W. Grimes }
1434b88c807SRodney W. Grimes 
1444b88c807SRodney W. Grimes void
1454b88c807SRodney W. Grimes ucomm(k, ve)
1464b88c807SRodney W. Grimes 	KINFO *k;
1474b88c807SRodney W. Grimes 	VARENT *ve;
1484b88c807SRodney W. Grimes {
1494b88c807SRodney W. Grimes 	VAR *v;
1504b88c807SRodney W. Grimes 
1514b88c807SRodney W. Grimes 	v = ve->var;
1521f7d2501SKirk McKusick 	(void)printf("%-*s", v->width, k->ki_p->ki_comm);
1534b88c807SRodney W. Grimes }
1544b88c807SRodney W. Grimes 
1554b88c807SRodney W. Grimes void
1564b88c807SRodney W. Grimes logname(k, ve)
1574b88c807SRodney W. Grimes 	KINFO *k;
1584b88c807SRodney W. Grimes 	VARENT *ve;
1594b88c807SRodney W. Grimes {
1604b88c807SRodney W. Grimes 	VAR *v;
161ad863cacSSteve Price 	char *s;
1624b88c807SRodney W. Grimes 
1634b88c807SRodney W. Grimes 	v = ve->var;
1641f7d2501SKirk McKusick 	(void)printf("%-*s", v->width, (s = k->ki_p->ki_login, *s) ? s : "-");
1654b88c807SRodney W. Grimes }
1664b88c807SRodney W. Grimes 
1674b88c807SRodney W. Grimes void
1684b88c807SRodney W. Grimes state(k, ve)
1694b88c807SRodney W. Grimes 	KINFO *k;
1704b88c807SRodney W. Grimes 	VARENT *ve;
1714b88c807SRodney W. Grimes {
172b40ce416SJulian Elischer 	int flag, sflag, tdflags;
1734b88c807SRodney W. Grimes 	char *cp;
1744b88c807SRodney W. Grimes 	VAR *v;
1754b88c807SRodney W. Grimes 	char buf[16];
1764b88c807SRodney W. Grimes 
1774b88c807SRodney W. Grimes 	v = ve->var;
1781f7d2501SKirk McKusick 	flag = k->ki_p->ki_flag;
179564efb8fSTor Egge 	sflag = k->ki_p->ki_sflag;
180b40ce416SJulian Elischer 	tdflags = k->ki_p->ki_tdflags;	/* XXXKSE */
1814b88c807SRodney W. Grimes 	cp = buf;
1824b88c807SRodney W. Grimes 
1831f7d2501SKirk McKusick 	switch (k->ki_p->ki_stat) {
1844b88c807SRodney W. Grimes 
1854b88c807SRodney W. Grimes 	case SSTOP:
1864b88c807SRodney W. Grimes 		*cp = 'T';
1874b88c807SRodney W. Grimes 		break;
1884b88c807SRodney W. Grimes 
1894b88c807SRodney W. Grimes 	case SSLEEP:
190b40ce416SJulian Elischer 		if (tdflags & TDF_SINTR)	/* interruptable (long) */
1911f7d2501SKirk McKusick 			*cp = k->ki_p->ki_slptime >= MAXSLP ? 'I' : 'S';
1924b88c807SRodney W. Grimes 		else
1934b88c807SRodney W. Grimes 			*cp = 'D';
1944b88c807SRodney W. Grimes 		break;
1954b88c807SRodney W. Grimes 
1964b88c807SRodney W. Grimes 	case SRUN:
1974b88c807SRodney W. Grimes 	case SIDL:
1984b88c807SRodney W. Grimes 		*cp = 'R';
1994b88c807SRodney W. Grimes 		break;
2004b88c807SRodney W. Grimes 
2010384fff8SJason Evans 	case SWAIT:
2020384fff8SJason Evans 		*cp = 'W';
2030384fff8SJason Evans 		break;
2040384fff8SJason Evans 
2050384fff8SJason Evans 	case SMTX:
2060384fff8SJason Evans 		*cp = 'M';
2070384fff8SJason Evans 		break;
2080384fff8SJason Evans 
2094b88c807SRodney W. Grimes 	case SZOMB:
2104b88c807SRodney W. Grimes 		*cp = 'Z';
2114b88c807SRodney W. Grimes 		break;
2124b88c807SRodney W. Grimes 
2134b88c807SRodney W. Grimes 	default:
2144b88c807SRodney W. Grimes 		*cp = '?';
2154b88c807SRodney W. Grimes 	}
2164b88c807SRodney W. Grimes 	cp++;
217e0aa5ab7SJohn Baldwin 	if (!(sflag & PS_INMEM))
2184b88c807SRodney W. Grimes 		*cp++ = 'W';
2191f7d2501SKirk McKusick 	if (k->ki_p->ki_nice < NZERO)
2204b88c807SRodney W. Grimes 		*cp++ = '<';
2211f7d2501SKirk McKusick 	else if (k->ki_p->ki_nice > NZERO)
2224b88c807SRodney W. Grimes 		*cp++ = 'N';
2234b88c807SRodney W. Grimes 	if (flag & P_TRACED)
2244b88c807SRodney W. Grimes 		*cp++ = 'X';
2251f7d2501SKirk McKusick 	if (flag & P_WEXIT && k->ki_p->ki_stat != SZOMB)
2264b88c807SRodney W. Grimes 		*cp++ = 'E';
2274b88c807SRodney W. Grimes 	if (flag & P_PPWAIT)
2284b88c807SRodney W. Grimes 		*cp++ = 'V';
2291f7d2501SKirk McKusick 	if ((flag & P_SYSTEM) || k->ki_p->ki_lock > 0)
2304b88c807SRodney W. Grimes 		*cp++ = 'L';
2311f7d2501SKirk McKusick 	if (k->ki_p->ki_kiflag & KI_SLEADER)
2324b88c807SRodney W. Grimes 		*cp++ = 's';
2331f7d2501SKirk McKusick 	if ((flag & P_CONTROLT) && k->ki_p->ki_pgid == k->ki_p->ki_tpgid)
2344b88c807SRodney W. Grimes 		*cp++ = '+';
23575c13541SPoul-Henning Kamp 	if (flag & P_JAILED)
23675c13541SPoul-Henning Kamp 		*cp++ = 'J';
2374b88c807SRodney W. Grimes 	*cp = '\0';
2384b88c807SRodney W. Grimes 	(void)printf("%-*s", v->width, buf);
2394b88c807SRodney W. Grimes }
2404b88c807SRodney W. Grimes 
2414b88c807SRodney W. Grimes void
2424b88c807SRodney W. Grimes pri(k, ve)
2434b88c807SRodney W. Grimes 	KINFO *k;
2444b88c807SRodney W. Grimes 	VARENT *ve;
2454b88c807SRodney W. Grimes {
2464b88c807SRodney W. Grimes 	VAR *v;
2474b88c807SRodney W. Grimes 
2484b88c807SRodney W. Grimes 	v = ve->var;
2494c85452bSJake Burkholder 	(void)printf("%*d", v->width, k->ki_p->ki_pri.pri_level - PZERO);
2504b88c807SRodney W. Grimes }
2514b88c807SRodney W. Grimes 
2524b88c807SRodney W. Grimes void
2534b88c807SRodney W. Grimes uname(k, ve)
2544b88c807SRodney W. Grimes 	KINFO *k;
2554b88c807SRodney W. Grimes 	VARENT *ve;
2564b88c807SRodney W. Grimes {
2574b88c807SRodney W. Grimes 	VAR *v;
2584b88c807SRodney W. Grimes 
2594b88c807SRodney W. Grimes 	v = ve->var;
2604b88c807SRodney W. Grimes 	(void)printf("%-*s",
2611f7d2501SKirk McKusick 	    (int)v->width, user_from_uid(k->ki_p->ki_uid, 0));
2624b88c807SRodney W. Grimes }
2634b88c807SRodney W. Grimes 
2646a2d726bSJordan K. Hubbard int
2656a2d726bSJordan K. Hubbard s_uname(k)
2666a2d726bSJordan K. Hubbard 	KINFO *k;
2676a2d726bSJordan K. Hubbard {
2681f7d2501SKirk McKusick 	    return (strlen(user_from_uid(k->ki_p->ki_uid, 0)));
2696a2d726bSJordan K. Hubbard }
2706a2d726bSJordan K. Hubbard 
2714b88c807SRodney W. Grimes void
2724b88c807SRodney W. Grimes runame(k, ve)
2734b88c807SRodney W. Grimes 	KINFO *k;
2744b88c807SRodney W. Grimes 	VARENT *ve;
2754b88c807SRodney W. Grimes {
2764b88c807SRodney W. Grimes 	VAR *v;
2774b88c807SRodney W. Grimes 
2784b88c807SRodney W. Grimes 	v = ve->var;
2794b88c807SRodney W. Grimes 	(void)printf("%-*s",
2801f7d2501SKirk McKusick 	    (int)v->width, user_from_uid(k->ki_p->ki_ruid, 0));
2814b88c807SRodney W. Grimes }
2824b88c807SRodney W. Grimes 
2836a2d726bSJordan K. Hubbard int
2846a2d726bSJordan K. Hubbard s_runame(k)
2856a2d726bSJordan K. Hubbard 	KINFO *k;
2866a2d726bSJordan K. Hubbard {
2871f7d2501SKirk McKusick 	    return (strlen(user_from_uid(k->ki_p->ki_ruid, 0)));
2886a2d726bSJordan K. Hubbard }
2896a2d726bSJordan K. Hubbard 
2904b88c807SRodney W. Grimes void
2914b88c807SRodney W. Grimes tdev(k, ve)
2924b88c807SRodney W. Grimes 	KINFO *k;
2934b88c807SRodney W. Grimes 	VARENT *ve;
2944b88c807SRodney W. Grimes {
2954b88c807SRodney W. Grimes 	VAR *v;
2964b88c807SRodney W. Grimes 	dev_t dev;
2974b88c807SRodney W. Grimes 	char buff[16];
2984b88c807SRodney W. Grimes 
2994b88c807SRodney W. Grimes 	v = ve->var;
3001f7d2501SKirk McKusick 	dev = k->ki_p->ki_tdev;
3014b88c807SRodney W. Grimes 	if (dev == NODEV)
3024b88c807SRodney W. Grimes 		(void)printf("%*s", v->width, "??");
3034b88c807SRodney W. Grimes 	else {
3044b88c807SRodney W. Grimes 		(void)snprintf(buff, sizeof(buff),
3054b88c807SRodney W. Grimes 		    "%d/%d", major(dev), minor(dev));
3064b88c807SRodney W. Grimes 		(void)printf("%*s", v->width, buff);
3074b88c807SRodney W. Grimes 	}
3084b88c807SRodney W. Grimes }
3094b88c807SRodney W. Grimes 
3104b88c807SRodney W. Grimes void
3114b88c807SRodney W. Grimes tname(k, ve)
3124b88c807SRodney W. Grimes 	KINFO *k;
3134b88c807SRodney W. Grimes 	VARENT *ve;
3144b88c807SRodney W. Grimes {
3154b88c807SRodney W. Grimes 	VAR *v;
3164b88c807SRodney W. Grimes 	dev_t dev;
3174b88c807SRodney W. Grimes 	char *ttname;
3184b88c807SRodney W. Grimes 
3194b88c807SRodney W. Grimes 	v = ve->var;
3201f7d2501SKirk McKusick 	dev = k->ki_p->ki_tdev;
3214b88c807SRodney W. Grimes 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
322c55931c7SPeter Wemm 		(void)printf("%*s ", v->width-1, "??");
3234b88c807SRodney W. Grimes 	else {
324efc18e2cSAndrey A. Chernov 		if (strncmp(ttname, "tty", 3) == 0 ||
325efc18e2cSAndrey A. Chernov 		    strncmp(ttname, "cua", 3) == 0)
3264b88c807SRodney W. Grimes 			ttname += 3;
327c55931c7SPeter Wemm 		(void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
3281f7d2501SKirk McKusick 			k->ki_p->ki_kiflag & KI_CTTY ? ' ' : '-');
3294b88c807SRodney W. Grimes 	}
3304b88c807SRodney W. Grimes }
3314b88c807SRodney W. Grimes 
3324b88c807SRodney W. Grimes void
3334b88c807SRodney W. Grimes longtname(k, ve)
3344b88c807SRodney W. Grimes 	KINFO *k;
3354b88c807SRodney W. Grimes 	VARENT *ve;
3364b88c807SRodney W. Grimes {
3374b88c807SRodney W. Grimes 	VAR *v;
3384b88c807SRodney W. Grimes 	dev_t dev;
3394b88c807SRodney W. Grimes 	char *ttname;
3404b88c807SRodney W. Grimes 
3414b88c807SRodney W. Grimes 	v = ve->var;
3421f7d2501SKirk McKusick 	dev = k->ki_p->ki_tdev;
3434b88c807SRodney W. Grimes 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
3444b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, "??");
3454b88c807SRodney W. Grimes 	else
3464b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, ttname);
3474b88c807SRodney W. Grimes }
3484b88c807SRodney W. Grimes 
3494b88c807SRodney W. Grimes void
3504b88c807SRodney W. Grimes started(k, ve)
3514b88c807SRodney W. Grimes 	KINFO *k;
3524b88c807SRodney W. Grimes 	VARENT *ve;
3534b88c807SRodney W. Grimes {
3544b88c807SRodney W. Grimes 	VAR *v;
3554b88c807SRodney W. Grimes 	static time_t now;
356f8ec5fe2SBruce Evans 	time_t then;
3574b88c807SRodney W. Grimes 	struct tm *tp;
3584b88c807SRodney W. Grimes 	char buf[100];
359f59105eeSAndrey A. Chernov 	static int  use_ampm = -1;
3604b88c807SRodney W. Grimes 
3614b88c807SRodney W. Grimes 	v = ve->var;
3621f7d2501SKirk McKusick 	if (!k->ki_valid) {
3634b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, "-");
3644b88c807SRodney W. Grimes 		return;
3654b88c807SRodney W. Grimes 	}
3664b88c807SRodney W. Grimes 
367f59105eeSAndrey A. Chernov 	if (use_ampm < 0)
368f59105eeSAndrey A. Chernov 		use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
369f59105eeSAndrey A. Chernov 
3701f7d2501SKirk McKusick 	then = k->ki_p->ki_start.tv_sec;
371f8ec5fe2SBruce Evans 	tp = localtime(&then);
3724b88c807SRodney W. Grimes 	if (!now)
3734b88c807SRodney W. Grimes 		(void)time(&now);
3741f7d2501SKirk McKusick 	if (now - k->ki_p->ki_start.tv_sec < 24 * 3600) {
37508017519SAndrey A. Chernov 		(void)strftime(buf, sizeof(buf) - 1,
37608017519SAndrey A. Chernov 		use_ampm ? "%l:%M%p" : "%k:%M  ", tp);
3771f7d2501SKirk McKusick 	} else if (now - k->ki_p->ki_start.tv_sec < 7 * 86400) {
37808017519SAndrey A. Chernov 		(void)strftime(buf, sizeof(buf) - 1,
37908017519SAndrey A. Chernov 		use_ampm ? "%a%I%p" : "%a%H  ", tp);
3804b88c807SRodney W. Grimes 	} else
3814b88c807SRodney W. Grimes 		(void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
3824b88c807SRodney W. Grimes 	(void)printf("%-*s", v->width, buf);
3834b88c807SRodney W. Grimes }
3844b88c807SRodney W. Grimes 
3854b88c807SRodney W. Grimes void
3864b88c807SRodney W. Grimes lstarted(k, ve)
3874b88c807SRodney W. Grimes 	KINFO *k;
3884b88c807SRodney W. Grimes 	VARENT *ve;
3894b88c807SRodney W. Grimes {
3904b88c807SRodney W. Grimes 	VAR *v;
391f8ec5fe2SBruce Evans 	time_t then;
3924b88c807SRodney W. Grimes 	char buf[100];
3934b88c807SRodney W. Grimes 
3944b88c807SRodney W. Grimes 	v = ve->var;
3951f7d2501SKirk McKusick 	if (!k->ki_valid) {
3964b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, "-");
3974b88c807SRodney W. Grimes 		return;
3984b88c807SRodney W. Grimes 	}
3991f7d2501SKirk McKusick 	then = k->ki_p->ki_start.tv_sec;
400f8ec5fe2SBruce Evans 	(void)strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
4014b88c807SRodney W. Grimes 	(void)printf("%-*s", v->width, buf);
4024b88c807SRodney W. Grimes }
4034b88c807SRodney W. Grimes 
4044b88c807SRodney W. Grimes void
405fd5f30bfSJohn Baldwin mtxname(k, ve)
406fd5f30bfSJohn Baldwin 	KINFO *k;
407fd5f30bfSJohn Baldwin 	VARENT *ve;
408fd5f30bfSJohn Baldwin {
409fd5f30bfSJohn Baldwin 	VAR *v;
410fd5f30bfSJohn Baldwin 
411fd5f30bfSJohn Baldwin 	v = ve->var;
4121f7d2501SKirk McKusick 	if (k->ki_p->ki_kiflag & KI_MTXBLOCK) {
4131f7d2501SKirk McKusick 		if (k->ki_p->ki_mtxname[0] != 0)
414fd5f30bfSJohn Baldwin 			(void)printf("%-*.*s", v->width, v->width,
4151f7d2501SKirk McKusick 				      k->ki_p->ki_mtxname);
416fd5f30bfSJohn Baldwin 		else
417fd5f30bfSJohn Baldwin 			(void)printf("%-*s", v->width, "???");
418fd5f30bfSJohn Baldwin 	} else
419fd5f30bfSJohn Baldwin 		(void)printf("%-*s", v->width, "-");
420fd5f30bfSJohn Baldwin }
421fd5f30bfSJohn Baldwin 
422fd5f30bfSJohn Baldwin void
4234b88c807SRodney W. Grimes wchan(k, ve)
4244b88c807SRodney W. Grimes 	KINFO *k;
4254b88c807SRodney W. Grimes 	VARENT *ve;
4264b88c807SRodney W. Grimes {
4274b88c807SRodney W. Grimes 	VAR *v;
4284b88c807SRodney W. Grimes 
4294b88c807SRodney W. Grimes 	v = ve->var;
4301f7d2501SKirk McKusick 	if (k->ki_p->ki_wchan) {
4311f7d2501SKirk McKusick 		if (k->ki_p->ki_wmesg[0] != 0)
4324b88c807SRodney W. Grimes 			(void)printf("%-*.*s", v->width, v->width,
4331f7d2501SKirk McKusick 				      k->ki_p->ki_wmesg);
4344b88c807SRodney W. Grimes 		else
4353929d518SDoug Rabson 			(void)printf("%-*lx", v->width,
4367d1192a7SPeter Wemm 			    (long)k->ki_p->ki_wchan);
4374b88c807SRodney W. Grimes 	} else
4384b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, "-");
4394b88c807SRodney W. Grimes }
4404b88c807SRodney W. Grimes 
4414a40c5e7SPeter Wemm #ifndef pgtok
4423a91de06SPoul-Henning Kamp #define pgtok(a)        (((a)*getpagesize())/1024)
4434a40c5e7SPeter Wemm #endif
4444b88c807SRodney W. Grimes 
4454b88c807SRodney W. Grimes void
4464b88c807SRodney W. Grimes vsize(k, ve)
4474b88c807SRodney W. Grimes 	KINFO *k;
4484b88c807SRodney W. Grimes 	VARENT *ve;
4494b88c807SRodney W. Grimes {
4504b88c807SRodney W. Grimes 	VAR *v;
4514b88c807SRodney W. Grimes 
4524b88c807SRodney W. Grimes 	v = ve->var;
4534b88c807SRodney W. Grimes 	(void)printf("%*d", v->width,
4541f7d2501SKirk McKusick 	    (k->ki_p->ki_size/1024));
4554b88c807SRodney W. Grimes }
4564b88c807SRodney W. Grimes 
4574b88c807SRodney W. Grimes void
4584b88c807SRodney W. Grimes rssize(k, ve)
4594b88c807SRodney W. Grimes 	KINFO *k;
4604b88c807SRodney W. Grimes 	VARENT *ve;
4614b88c807SRodney W. Grimes {
4624b88c807SRodney W. Grimes 	VAR *v;
4634b88c807SRodney W. Grimes 
4644b88c807SRodney W. Grimes 	v = ve->var;
4654b88c807SRodney W. Grimes 	/* XXX don't have info about shared */
466ce0cab88SBruce Evans 	(void)printf("%*lu", v->width,
4671f7d2501SKirk McKusick 	    (u_long)pgtok(k->ki_p->ki_rssize));
4684b88c807SRodney W. Grimes }
4694b88c807SRodney W. Grimes 
4704b88c807SRodney W. Grimes void
4714b88c807SRodney W. Grimes p_rssize(k, ve)		/* doesn't account for text */
4724b88c807SRodney W. Grimes 	KINFO *k;
4734b88c807SRodney W. Grimes 	VARENT *ve;
4744b88c807SRodney W. Grimes {
4754b88c807SRodney W. Grimes 	VAR *v;
4764b88c807SRodney W. Grimes 
4774b88c807SRodney W. Grimes 	v = ve->var;
4781f7d2501SKirk McKusick 	(void)printf("%*ld", v->width, (long)pgtok(k->ki_p->ki_rssize));
4794b88c807SRodney W. Grimes }
4804b88c807SRodney W. Grimes 
4814b88c807SRodney W. Grimes void
4824b88c807SRodney W. Grimes cputime(k, ve)
4834b88c807SRodney W. Grimes 	KINFO *k;
4844b88c807SRodney W. Grimes 	VARENT *ve;
4854b88c807SRodney W. Grimes {
4864b88c807SRodney W. Grimes 	VAR *v;
4874b88c807SRodney W. Grimes 	long secs;
4884b88c807SRodney W. Grimes 	long psecs;	/* "parts" of a second. first micro, then centi */
4894b88c807SRodney W. Grimes 	char obuff[128];
4908073a93cSAndrey A. Chernov 	static char decimal_point = 0;
4914b88c807SRodney W. Grimes 
4928073a93cSAndrey A. Chernov 	if (!decimal_point)
4938073a93cSAndrey A. Chernov 		decimal_point = localeconv()->decimal_point[0];
4944b88c807SRodney W. Grimes 	v = ve->var;
4951f7d2501SKirk McKusick 	if (k->ki_p->ki_stat == SZOMB || !k->ki_valid) {
4965832a752SBruce Evans 		secs = 0;
4975832a752SBruce Evans 		psecs = 0;
4984b88c807SRodney W. Grimes 	} else {
4994b88c807SRodney W. Grimes 		/*
5004b88c807SRodney W. Grimes 		 * This counts time spent handling interrupts.  We could
5014b88c807SRodney W. Grimes 		 * fix this, but it is not 100% trivial (and interrupt
5024b88c807SRodney W. Grimes 		 * time fractions only work on the sparc anyway).	XXX
5034b88c807SRodney W. Grimes 		 */
5041f7d2501SKirk McKusick 		secs = k->ki_p->ki_runtime / 1000000;
5051f7d2501SKirk McKusick 		psecs = k->ki_p->ki_runtime % 1000000;
5064b88c807SRodney W. Grimes 		if (sumrusage) {
5071f7d2501SKirk McKusick 			secs += k->ki_p->ki_childtime.tv_sec;
5081f7d2501SKirk McKusick 			psecs += k->ki_p->ki_childtime.tv_usec;
5094b88c807SRodney W. Grimes 		}
5104b88c807SRodney W. Grimes 		/*
5114b88c807SRodney W. Grimes 		 * round and scale to 100's
5124b88c807SRodney W. Grimes 		 */
5135832a752SBruce Evans 		psecs = (psecs + 5000) / 10000;
5145832a752SBruce Evans 		secs += psecs / 100;
5155832a752SBruce Evans 		psecs = psecs % 100;
5165832a752SBruce Evans 	}
5174b88c807SRodney W. Grimes 	(void)snprintf(obuff, sizeof(obuff),
5188073a93cSAndrey A. Chernov 	    "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs);
5194b88c807SRodney W. Grimes 	(void)printf("%*s", v->width, obuff);
5204b88c807SRodney W. Grimes }
5214b88c807SRodney W. Grimes 
5224b88c807SRodney W. Grimes double
5234b88c807SRodney W. Grimes getpcpu(k)
5244b88c807SRodney W. Grimes 	KINFO *k;
5254b88c807SRodney W. Grimes {
5264b88c807SRodney W. Grimes 	static int failure;
5274b88c807SRodney W. Grimes 
5284b88c807SRodney W. Grimes 	if (!nlistread)
5294b88c807SRodney W. Grimes 		failure = donlist();
5304b88c807SRodney W. Grimes 	if (failure)
5314b88c807SRodney W. Grimes 		return (0.0);
5324b88c807SRodney W. Grimes 
5334b88c807SRodney W. Grimes #define	fxtofl(fixpt)	((double)(fixpt) / fscale)
5344b88c807SRodney W. Grimes 
5354b88c807SRodney W. Grimes 	/* XXX - I don't like this */
536e0aa5ab7SJohn Baldwin 	if (k->ki_p->ki_swtime == 0 || (k->ki_p->ki_sflag & PS_INMEM) == 0)
5374b88c807SRodney W. Grimes 		return (0.0);
5384b88c807SRodney W. Grimes 	if (rawcpu)
5391f7d2501SKirk McKusick 		return (100.0 * fxtofl(k->ki_p->ki_pctcpu));
5401f7d2501SKirk McKusick 	return (100.0 * fxtofl(k->ki_p->ki_pctcpu) /
5411f7d2501SKirk McKusick 		(1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu)))));
5424b88c807SRodney W. Grimes }
5434b88c807SRodney W. Grimes 
5444b88c807SRodney W. Grimes void
5454b88c807SRodney W. Grimes pcpu(k, ve)
5464b88c807SRodney W. Grimes 	KINFO *k;
5474b88c807SRodney W. Grimes 	VARENT *ve;
5484b88c807SRodney W. Grimes {
5494b88c807SRodney W. Grimes 	VAR *v;
5504b88c807SRodney W. Grimes 
5514b88c807SRodney W. Grimes 	v = ve->var;
5524b88c807SRodney W. Grimes 	(void)printf("%*.1f", v->width, getpcpu(k));
5534b88c807SRodney W. Grimes }
5544b88c807SRodney W. Grimes 
5554b88c807SRodney W. Grimes double
5564b88c807SRodney W. Grimes getpmem(k)
5574b88c807SRodney W. Grimes 	KINFO *k;
5584b88c807SRodney W. Grimes {
5594b88c807SRodney W. Grimes 	static int failure;
5604b88c807SRodney W. Grimes 	double fracmem;
5614b88c807SRodney W. Grimes 
5624b88c807SRodney W. Grimes 	if (!nlistread)
5634b88c807SRodney W. Grimes 		failure = donlist();
5644b88c807SRodney W. Grimes 	if (failure)
5654b88c807SRodney W. Grimes 		return (0.0);
5664b88c807SRodney W. Grimes 
567e0aa5ab7SJohn Baldwin 	if ((k->ki_p->ki_sflag & PS_INMEM) == 0)
5684b88c807SRodney W. Grimes 		return (0.0);
5694b88c807SRodney W. Grimes 	/* XXX want pmap ptpages, segtab, etc. (per architecture) */
5704b88c807SRodney W. Grimes 	/* XXX don't have info about shared */
57185012e7cSPeter Wemm 	fracmem = ((float)k->ki_p->ki_rssize)/mempages;
5724b88c807SRodney W. Grimes 	return (100.0 * fracmem);
5734b88c807SRodney W. Grimes }
5744b88c807SRodney W. Grimes 
5754b88c807SRodney W. Grimes void
5764b88c807SRodney W. Grimes pmem(k, ve)
5774b88c807SRodney W. Grimes 	KINFO *k;
5784b88c807SRodney W. Grimes 	VARENT *ve;
5794b88c807SRodney W. Grimes {
5804b88c807SRodney W. Grimes 	VAR *v;
5814b88c807SRodney W. Grimes 
5824b88c807SRodney W. Grimes 	v = ve->var;
5834b88c807SRodney W. Grimes 	(void)printf("%*.1f", v->width, getpmem(k));
5844b88c807SRodney W. Grimes }
5854b88c807SRodney W. Grimes 
5864b88c807SRodney W. Grimes void
5874b88c807SRodney W. Grimes pagein(k, ve)
5884b88c807SRodney W. Grimes 	KINFO *k;
5894b88c807SRodney W. Grimes 	VARENT *ve;
5904b88c807SRodney W. Grimes {
5914b88c807SRodney W. Grimes 	VAR *v;
5924b88c807SRodney W. Grimes 
5934b88c807SRodney W. Grimes 	v = ve->var;
5940fd510b7SJoerg Wunsch 	(void)printf("%*ld", v->width,
5951f7d2501SKirk McKusick 	    k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0);
5964b88c807SRodney W. Grimes }
5974b88c807SRodney W. Grimes 
5984b88c807SRodney W. Grimes void
5994b88c807SRodney W. Grimes maxrss(k, ve)
6004b88c807SRodney W. Grimes 	KINFO *k;
6014b88c807SRodney W. Grimes 	VARENT *ve;
6024b88c807SRodney W. Grimes {
6034b88c807SRodney W. Grimes 	VAR *v;
6044b88c807SRodney W. Grimes 
6054b88c807SRodney W. Grimes 	v = ve->var;
606940cca66SPeter Wemm 	/* XXX not yet */
6074b88c807SRodney W. Grimes 	(void)printf("%*s", v->width, "-");
6084b88c807SRodney W. Grimes }
6094b88c807SRodney W. Grimes 
6104b88c807SRodney W. Grimes void
6114b88c807SRodney W. Grimes tsize(k, ve)
6124b88c807SRodney W. Grimes 	KINFO *k;
6134b88c807SRodney W. Grimes 	VARENT *ve;
6144b88c807SRodney W. Grimes {
6154b88c807SRodney W. Grimes 	VAR *v;
6164b88c807SRodney W. Grimes 
6174b88c807SRodney W. Grimes 	v = ve->var;
6181f7d2501SKirk McKusick 	(void)printf("%*ld", v->width, (long)pgtok(k->ki_p->ki_tsize));
6194b88c807SRodney W. Grimes }
6204b88c807SRodney W. Grimes 
621ad863cacSSteve Price void
6224c85452bSJake Burkholder priorityr(k, ve)
623ad863cacSSteve Price 	KINFO *k;
624ad863cacSSteve Price 	VARENT *ve;
625ad863cacSSteve Price {
626ad863cacSSteve Price 	VAR *v;
6274c85452bSJake Burkholder 	struct priority *pri;
628ad863cacSSteve Price 	char str[8];
6294c85452bSJake Burkholder 	unsigned class, level;
630ad863cacSSteve Price 
631ad863cacSSteve Price 	v = ve->var;
6324c85452bSJake Burkholder 	pri = (struct priority *) ((char *)k + v->off);
6334c85452bSJake Burkholder 	class = pri->pri_class;
6344c85452bSJake Burkholder 	level = pri->pri_level;
6354c85452bSJake Burkholder 	switch (class) {
6364c85452bSJake Burkholder 	case PRI_REALTIME:
6374c85452bSJake Burkholder 		snprintf(str, sizeof(str), "real:%u", level);
638ad863cacSSteve Price 		break;
6394c85452bSJake Burkholder 	case PRI_TIMESHARE:
640ad863cacSSteve Price 		strncpy(str, "normal", sizeof(str));
641ad863cacSSteve Price 		break;
6424c85452bSJake Burkholder 	case PRI_IDLE:
6434c85452bSJake Burkholder 		snprintf(str, sizeof(str), "idle:%u", level);
644ad863cacSSteve Price 		break;
645ad863cacSSteve Price 	default:
6464c85452bSJake Burkholder 		snprintf(str, sizeof(str), "%u:%u", class, level);
647ad863cacSSteve Price 		break;
648ad863cacSSteve Price 	}
649ad863cacSSteve Price 	str[sizeof(str) - 1] = '\0';
650ad863cacSSteve Price 	(void)printf("%*s", v->width, str);
651ad863cacSSteve Price }
652ad863cacSSteve Price 
6534b88c807SRodney W. Grimes /*
6544b88c807SRodney W. Grimes  * Generic output routines.  Print fields from various prototype
6554b88c807SRodney W. Grimes  * structures.
6564b88c807SRodney W. Grimes  */
6574b88c807SRodney W. Grimes static void
6584b88c807SRodney W. Grimes printval(bp, v)
6594b88c807SRodney W. Grimes 	char *bp;
6604b88c807SRodney W. Grimes 	VAR *v;
6614b88c807SRodney W. Grimes {
6624b88c807SRodney W. Grimes 	static char ofmt[32] = "%";
6634b88c807SRodney W. Grimes 	char *fcp, *cp;
6644b88c807SRodney W. Grimes 
6654b88c807SRodney W. Grimes 	cp = ofmt + 1;
6664b88c807SRodney W. Grimes 	fcp = v->fmt;
6674b88c807SRodney W. Grimes 	if (v->flag & LJUST)
6684b88c807SRodney W. Grimes 		*cp++ = '-';
6694b88c807SRodney W. Grimes 	*cp++ = '*';
6700fd510b7SJoerg Wunsch 	while ((*cp++ = *fcp++));
6714b88c807SRodney W. Grimes 
6724b88c807SRodney W. Grimes 	switch (v->type) {
6734b88c807SRodney W. Grimes 	case CHAR:
6744b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(char *)bp);
6754b88c807SRodney W. Grimes 		break;
6764b88c807SRodney W. Grimes 	case UCHAR:
6774b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(u_char *)bp);
6784b88c807SRodney W. Grimes 		break;
6794b88c807SRodney W. Grimes 	case SHORT:
6804b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(short *)bp);
6814b88c807SRodney W. Grimes 		break;
6824b88c807SRodney W. Grimes 	case USHORT:
6834b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(u_short *)bp);
6844b88c807SRodney W. Grimes 		break;
6853929d518SDoug Rabson 	case INT:
6863929d518SDoug Rabson 		(void)printf(ofmt, v->width, *(int *)bp);
6873929d518SDoug Rabson 		break;
6883929d518SDoug Rabson 	case UINT:
6893929d518SDoug Rabson 		(void)printf(ofmt, v->width, *(u_int *)bp);
6903929d518SDoug Rabson 		break;
6914b88c807SRodney W. Grimes 	case LONG:
6924b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(long *)bp);
6934b88c807SRodney W. Grimes 		break;
6944b88c807SRodney W. Grimes 	case ULONG:
6954b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(u_long *)bp);
6964b88c807SRodney W. Grimes 		break;
6974b88c807SRodney W. Grimes 	case KPTR:
6987d1192a7SPeter Wemm 		(void)printf(ofmt, v->width, *(u_long *)bp);
6994b88c807SRodney W. Grimes 		break;
7004b88c807SRodney W. Grimes 	default:
7014b88c807SRodney W. Grimes 		errx(1, "unknown type %d", v->type);
7024b88c807SRodney W. Grimes 	}
7034b88c807SRodney W. Grimes }
7044b88c807SRodney W. Grimes 
7054b88c807SRodney W. Grimes void
7061f7d2501SKirk McKusick kvar(k, ve)
7074b88c807SRodney W. Grimes 	KINFO *k;
7084b88c807SRodney W. Grimes 	VARENT *ve;
7094b88c807SRodney W. Grimes {
7104b88c807SRodney W. Grimes 	VAR *v;
7114b88c807SRodney W. Grimes 
7124b88c807SRodney W. Grimes 	v = ve->var;
7131f7d2501SKirk McKusick 	printval((char *)((char *)k->ki_p + v->off), v);
7144b88c807SRodney W. Grimes }
7154b88c807SRodney W. Grimes 
7164b88c807SRodney W. Grimes void
7174b88c807SRodney W. Grimes rvar(k, ve)
7184b88c807SRodney W. Grimes 	KINFO *k;
7194b88c807SRodney W. Grimes 	VARENT *ve;
7204b88c807SRodney W. Grimes {
7214b88c807SRodney W. Grimes 	VAR *v;
7224b88c807SRodney W. Grimes 
7234b88c807SRodney W. Grimes 	v = ve->var;
7241f7d2501SKirk McKusick 	if (k->ki_valid)
7251f7d2501SKirk McKusick 		printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v);
7264b88c807SRodney W. Grimes 	else
7274b88c807SRodney W. Grimes 		(void)printf("%*s", v->width, "-");
7284b88c807SRodney W. Grimes }
7297304f61fSBrian Feldman 
7307304f61fSBrian Feldman void
7317304f61fSBrian Feldman lattr(k, ve)
7327304f61fSBrian Feldman 	KINFO *k;
7337304f61fSBrian Feldman 	VARENT *ve;
7347304f61fSBrian Feldman {
7357304f61fSBrian Feldman 	VAR *v;
7367304f61fSBrian Feldman 
7377304f61fSBrian Feldman 	v = ve->var;
7387304f61fSBrian Feldman 	(void)printf("%-*d", (int)v->width, get_lattr(k->ki_p->ki_pid));
7397304f61fSBrian Feldman }
740