xref: /freebsd/bin/ps/print.c (revision ad863cac06b4376b11d869fb252daa1a83f3a722)
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[] =
39ad863cacSSteve Price 	"$Id: print.c,v 1.25 1998/05/15 06:29:16 charnier Exp $";
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>
544b88c807SRodney W. Grimes #include <math.h>
554b88c807SRodney W. Grimes #include <nlist.h>
564b88c807SRodney W. Grimes #include <stddef.h>
574b88c807SRodney W. Grimes #include <stdio.h>
584b88c807SRodney W. Grimes #include <stdlib.h>
590164b6d6SPeter Wemm #include <unistd.h>
604b88c807SRodney W. Grimes #include <string.h>
614b88c807SRodney W. Grimes #include <vis.h>
624b88c807SRodney W. Grimes 
634b88c807SRodney W. Grimes #include "ps.h"
644b88c807SRodney W. Grimes 
654b88c807SRodney W. Grimes void
664b88c807SRodney W. Grimes printheader()
674b88c807SRodney W. Grimes {
684b88c807SRodney W. Grimes 	VAR *v;
694b88c807SRodney W. Grimes 	struct varent *vent;
704b88c807SRodney W. Grimes 
714b88c807SRodney W. Grimes 	for (vent = vhead; vent; vent = vent->next) {
724b88c807SRodney W. Grimes 		v = vent->var;
734b88c807SRodney W. Grimes 		if (v->flag & LJUST) {
744b88c807SRodney W. Grimes 			if (vent->next == NULL)	/* last one */
754b88c807SRodney W. Grimes 				(void)printf("%s", v->header);
764b88c807SRodney W. Grimes 			else
774b88c807SRodney W. Grimes 				(void)printf("%-*s", v->width, v->header);
784b88c807SRodney W. Grimes 		} else
794b88c807SRodney W. Grimes 			(void)printf("%*s", v->width, v->header);
804b88c807SRodney W. Grimes 		if (vent->next != NULL)
814b88c807SRodney W. Grimes 			(void)putchar(' ');
824b88c807SRodney W. Grimes 	}
834b88c807SRodney W. Grimes 	(void)putchar('\n');
844b88c807SRodney W. Grimes }
854b88c807SRodney W. Grimes 
864b88c807SRodney W. Grimes void
874b88c807SRodney W. Grimes command(k, ve)
884b88c807SRodney W. Grimes 	KINFO *k;
894b88c807SRodney W. Grimes 	VARENT *ve;
904b88c807SRodney W. Grimes {
914b88c807SRodney W. Grimes 	VAR *v;
924b88c807SRodney W. Grimes 	int left;
934b88c807SRodney W. Grimes 	char *cp, *vis_env, *vis_args;
944b88c807SRodney W. Grimes 
95db91faacSPeter Wemm 	v = ve->var;
96db91faacSPeter Wemm 
97db91faacSPeter Wemm 	if (cflag) {
983cc273e0SJohn Polstra 		if (ve->next == NULL)	/* last field, don't pad */
993cc273e0SJohn Polstra 			(void)printf("%s", KI_PROC(k)->p_comm);
1003cc273e0SJohn Polstra 		else
101db91faacSPeter Wemm 			(void)printf("%-*s", v->width, KI_PROC(k)->p_comm);
102db91faacSPeter Wemm 		return;
103db91faacSPeter Wemm 	}
104db91faacSPeter Wemm 
1054b88c807SRodney W. Grimes 	if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
1064b88c807SRodney W. Grimes 		err(1, NULL);
1074b88c807SRodney W. Grimes 	strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
1084b88c807SRodney W. Grimes 	if (k->ki_env) {
1094b88c807SRodney W. Grimes 		if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
1104b88c807SRodney W. Grimes 			err(1, NULL);
1114b88c807SRodney W. Grimes 		strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
1124b88c807SRodney W. Grimes 	} else
1134b88c807SRodney W. Grimes 		vis_env = NULL;
1144b88c807SRodney W. Grimes 
1154b88c807SRodney W. Grimes 	if (ve->next == NULL) {
1164b88c807SRodney W. Grimes 		/* last field */
1174b88c807SRodney W. Grimes 		if (termwidth == UNLIMITED) {
1184b88c807SRodney W. Grimes 			if (vis_env)
1194b88c807SRodney W. Grimes 				(void)printf("%s ", vis_env);
1204b88c807SRodney W. Grimes 			(void)printf("%s", vis_args);
1214b88c807SRodney W. Grimes 		} else {
1224b88c807SRodney W. Grimes 			left = termwidth - (totwidth - v->width);
1234b88c807SRodney W. Grimes 			if (left < 1) /* already wrapped, just use std width */
1244b88c807SRodney W. Grimes 				left = v->width;
1254b88c807SRodney W. Grimes 			if ((cp = vis_env) != NULL) {
1264b88c807SRodney W. Grimes 				while (--left >= 0 && *cp)
1274b88c807SRodney W. Grimes 					(void)putchar(*cp++);
1284b88c807SRodney W. Grimes 				if (--left >= 0)
1294b88c807SRodney W. Grimes 					putchar(' ');
1304b88c807SRodney W. Grimes 			}
1314b88c807SRodney W. Grimes 			for (cp = vis_args; --left >= 0 && *cp != '\0';)
1324b88c807SRodney W. Grimes 				(void)putchar(*cp++);
1334b88c807SRodney W. Grimes 		}
1344b88c807SRodney W. Grimes 	} else
1354b88c807SRodney W. Grimes 		/* XXX env? */
1364b88c807SRodney W. Grimes 		(void)printf("%-*.*s", v->width, v->width, vis_args);
1374b88c807SRodney W. Grimes 	free(vis_args);
1384b88c807SRodney W. Grimes 	if (vis_env != NULL)
1394b88c807SRodney W. Grimes 		free(vis_env);
1404b88c807SRodney W. Grimes }
1414b88c807SRodney W. Grimes 
1424b88c807SRodney W. Grimes void
1434b88c807SRodney W. Grimes ucomm(k, ve)
1444b88c807SRodney W. Grimes 	KINFO *k;
1454b88c807SRodney W. Grimes 	VARENT *ve;
1464b88c807SRodney W. Grimes {
1474b88c807SRodney W. Grimes 	VAR *v;
1484b88c807SRodney W. Grimes 
1494b88c807SRodney W. Grimes 	v = ve->var;
1504b88c807SRodney W. Grimes 	(void)printf("%-*s", v->width, KI_PROC(k)->p_comm);
1514b88c807SRodney W. Grimes }
1524b88c807SRodney W. Grimes 
1534b88c807SRodney W. Grimes void
1544b88c807SRodney W. Grimes logname(k, ve)
1554b88c807SRodney W. Grimes 	KINFO *k;
1564b88c807SRodney W. Grimes 	VARENT *ve;
1574b88c807SRodney W. Grimes {
1584b88c807SRodney W. Grimes 	VAR *v;
159ad863cacSSteve Price 	char *s;
1604b88c807SRodney W. Grimes 
1614b88c807SRodney W. Grimes 	v = ve->var;
162ad863cacSSteve Price 	(void)printf("%-*s", v->width, (s = KI_EPROC(k)->e_login, *s) ? s : "-");
1634b88c807SRodney W. Grimes }
1644b88c807SRodney W. Grimes 
1654b88c807SRodney W. Grimes void
1664b88c807SRodney W. Grimes state(k, ve)
1674b88c807SRodney W. Grimes 	KINFO *k;
1684b88c807SRodney W. Grimes 	VARENT *ve;
1694b88c807SRodney W. Grimes {
1704b88c807SRodney W. Grimes 	struct proc *p;
1714b88c807SRodney W. Grimes 	int flag;
1724b88c807SRodney W. Grimes 	char *cp;
1734b88c807SRodney W. Grimes 	VAR *v;
1744b88c807SRodney W. Grimes 	char buf[16];
1754b88c807SRodney W. Grimes 
1764b88c807SRodney W. Grimes 	v = ve->var;
1774b88c807SRodney W. Grimes 	p = KI_PROC(k);
1784b88c807SRodney W. Grimes 	flag = p->p_flag;
1794b88c807SRodney W. Grimes 	cp = buf;
1804b88c807SRodney W. Grimes 
1814b88c807SRodney W. Grimes 	switch (p->p_stat) {
1824b88c807SRodney W. Grimes 
1834b88c807SRodney W. Grimes 	case SSTOP:
1844b88c807SRodney W. Grimes 		*cp = 'T';
1854b88c807SRodney W. Grimes 		break;
1864b88c807SRodney W. Grimes 
1874b88c807SRodney W. Grimes 	case SSLEEP:
1884b88c807SRodney W. Grimes 		if (flag & P_SINTR)	/* interuptable (long) */
1894b88c807SRodney W. Grimes 			*cp = p->p_slptime >= MAXSLP ? 'I' : 'S';
1904b88c807SRodney W. Grimes 		else
1914b88c807SRodney W. Grimes 			*cp = 'D';
1924b88c807SRodney W. Grimes 		break;
1934b88c807SRodney W. Grimes 
1944b88c807SRodney W. Grimes 	case SRUN:
1954b88c807SRodney W. Grimes 	case SIDL:
1964b88c807SRodney W. Grimes 		*cp = 'R';
1974b88c807SRodney W. Grimes 		break;
1984b88c807SRodney W. Grimes 
1994b88c807SRodney W. Grimes 	case SZOMB:
2004b88c807SRodney W. Grimes 		*cp = 'Z';
2014b88c807SRodney W. Grimes 		break;
2024b88c807SRodney W. Grimes 
2034b88c807SRodney W. Grimes 	default:
2044b88c807SRodney W. Grimes 		*cp = '?';
2054b88c807SRodney W. Grimes 	}
2064b88c807SRodney W. Grimes 	cp++;
207940cca66SPeter Wemm 	if (!(flag & P_INMEM))
2084b88c807SRodney W. Grimes 		*cp++ = 'W';
2094b88c807SRodney W. Grimes 	if (p->p_nice < NZERO)
2104b88c807SRodney W. Grimes 		*cp++ = '<';
2114b88c807SRodney W. Grimes 	else if (p->p_nice > NZERO)
2124b88c807SRodney W. Grimes 		*cp++ = 'N';
2134b88c807SRodney W. Grimes 	if (flag & P_TRACED)
2144b88c807SRodney W. Grimes 		*cp++ = 'X';
2154b88c807SRodney W. Grimes 	if (flag & P_WEXIT && p->p_stat != SZOMB)
2164b88c807SRodney W. Grimes 		*cp++ = 'E';
2174b88c807SRodney W. Grimes 	if (flag & P_PPWAIT)
2184b88c807SRodney W. Grimes 		*cp++ = 'V';
2194b88c807SRodney W. Grimes 	if (flag & (P_SYSTEM | P_NOSWAP | P_PHYSIO))
2204b88c807SRodney W. Grimes 		*cp++ = 'L';
2214b88c807SRodney W. Grimes 	if (KI_EPROC(k)->e_flag & EPROC_SLEADER)
2224b88c807SRodney W. Grimes 		*cp++ = 's';
2234b88c807SRodney W. Grimes 	if ((flag & P_CONTROLT) && KI_EPROC(k)->e_pgid == KI_EPROC(k)->e_tpgid)
2244b88c807SRodney W. Grimes 		*cp++ = '+';
2254b88c807SRodney W. Grimes 	*cp = '\0';
2264b88c807SRodney W. Grimes 	(void)printf("%-*s", v->width, buf);
2274b88c807SRodney W. Grimes }
2284b88c807SRodney W. Grimes 
2294b88c807SRodney W. Grimes void
2304b88c807SRodney W. Grimes pri(k, ve)
2314b88c807SRodney W. Grimes 	KINFO *k;
2324b88c807SRodney W. Grimes 	VARENT *ve;
2334b88c807SRodney W. Grimes {
2344b88c807SRodney W. Grimes 	VAR *v;
2354b88c807SRodney W. Grimes 
2364b88c807SRodney W. Grimes 	v = ve->var;
2374b88c807SRodney W. Grimes 	(void)printf("%*d", v->width, KI_PROC(k)->p_priority - PZERO);
2384b88c807SRodney W. Grimes }
2394b88c807SRodney W. Grimes 
2404b88c807SRodney W. Grimes void
2414b88c807SRodney W. Grimes uname(k, ve)
2424b88c807SRodney W. Grimes 	KINFO *k;
2434b88c807SRodney W. Grimes 	VARENT *ve;
2444b88c807SRodney W. Grimes {
2454b88c807SRodney W. Grimes 	VAR *v;
2464b88c807SRodney W. Grimes 
2474b88c807SRodney W. Grimes 	v = ve->var;
2484b88c807SRodney W. Grimes 	(void)printf("%-*s",
2494b88c807SRodney W. Grimes 	    (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0));
2504b88c807SRodney W. Grimes }
2514b88c807SRodney W. Grimes 
2526a2d726bSJordan K. Hubbard int
2536a2d726bSJordan K. Hubbard s_uname(k)
2546a2d726bSJordan K. Hubbard 	KINFO *k;
2556a2d726bSJordan K. Hubbard {
2566a2d726bSJordan K. Hubbard 	    return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0)));
2576a2d726bSJordan K. Hubbard }
2586a2d726bSJordan K. Hubbard 
2594b88c807SRodney W. Grimes void
2604b88c807SRodney W. Grimes runame(k, ve)
2614b88c807SRodney W. Grimes 	KINFO *k;
2624b88c807SRodney W. Grimes 	VARENT *ve;
2634b88c807SRodney W. Grimes {
2644b88c807SRodney W. Grimes 	VAR *v;
2654b88c807SRodney W. Grimes 
2664b88c807SRodney W. Grimes 	v = ve->var;
2674b88c807SRodney W. Grimes 	(void)printf("%-*s",
2684b88c807SRodney W. Grimes 	    (int)v->width, user_from_uid(KI_EPROC(k)->e_pcred.p_ruid, 0));
2694b88c807SRodney W. Grimes }
2704b88c807SRodney W. Grimes 
2716a2d726bSJordan K. Hubbard int
2726a2d726bSJordan K. Hubbard s_runame(k)
2736a2d726bSJordan K. Hubbard 	KINFO *k;
2746a2d726bSJordan K. Hubbard {
2756a2d726bSJordan K. Hubbard 	    return (strlen(user_from_uid(KI_EPROC(k)->e_pcred.p_ruid, 0)));
2766a2d726bSJordan K. Hubbard }
2776a2d726bSJordan K. Hubbard 
2784b88c807SRodney W. Grimes void
2794b88c807SRodney W. Grimes tdev(k, ve)
2804b88c807SRodney W. Grimes 	KINFO *k;
2814b88c807SRodney W. Grimes 	VARENT *ve;
2824b88c807SRodney W. Grimes {
2834b88c807SRodney W. Grimes 	VAR *v;
2844b88c807SRodney W. Grimes 	dev_t dev;
2854b88c807SRodney W. Grimes 	char buff[16];
2864b88c807SRodney W. Grimes 
2874b88c807SRodney W. Grimes 	v = ve->var;
2884b88c807SRodney W. Grimes 	dev = KI_EPROC(k)->e_tdev;
2894b88c807SRodney W. Grimes 	if (dev == NODEV)
2904b88c807SRodney W. Grimes 		(void)printf("%*s", v->width, "??");
2914b88c807SRodney W. Grimes 	else {
2924b88c807SRodney W. Grimes 		(void)snprintf(buff, sizeof(buff),
2934b88c807SRodney W. Grimes 		    "%d/%d", major(dev), minor(dev));
2944b88c807SRodney W. Grimes 		(void)printf("%*s", v->width, buff);
2954b88c807SRodney W. Grimes 	}
2964b88c807SRodney W. Grimes }
2974b88c807SRodney W. Grimes 
2984b88c807SRodney W. Grimes void
2994b88c807SRodney W. Grimes tname(k, ve)
3004b88c807SRodney W. Grimes 	KINFO *k;
3014b88c807SRodney W. Grimes 	VARENT *ve;
3024b88c807SRodney W. Grimes {
3034b88c807SRodney W. Grimes 	VAR *v;
3044b88c807SRodney W. Grimes 	dev_t dev;
3054b88c807SRodney W. Grimes 	char *ttname;
3064b88c807SRodney W. Grimes 
3074b88c807SRodney W. Grimes 	v = ve->var;
3084b88c807SRodney W. Grimes 	dev = KI_EPROC(k)->e_tdev;
3094b88c807SRodney W. Grimes 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
310c55931c7SPeter Wemm 		(void)printf("%*s ", v->width-1, "??");
3114b88c807SRodney W. Grimes 	else {
312efc18e2cSAndrey A. Chernov 		if (strncmp(ttname, "tty", 3) == 0 ||
313efc18e2cSAndrey A. Chernov 		    strncmp(ttname, "cua", 3) == 0)
3144b88c807SRodney W. Grimes 			ttname += 3;
315c55931c7SPeter Wemm 		(void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
3164b88c807SRodney W. Grimes 			KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-');
3174b88c807SRodney W. Grimes 	}
3184b88c807SRodney W. Grimes }
3194b88c807SRodney W. Grimes 
3204b88c807SRodney W. Grimes void
3214b88c807SRodney W. Grimes longtname(k, ve)
3224b88c807SRodney W. Grimes 	KINFO *k;
3234b88c807SRodney W. Grimes 	VARENT *ve;
3244b88c807SRodney W. Grimes {
3254b88c807SRodney W. Grimes 	VAR *v;
3264b88c807SRodney W. Grimes 	dev_t dev;
3274b88c807SRodney W. Grimes 	char *ttname;
3284b88c807SRodney W. Grimes 
3294b88c807SRodney W. Grimes 	v = ve->var;
3304b88c807SRodney W. Grimes 	dev = KI_EPROC(k)->e_tdev;
3314b88c807SRodney W. Grimes 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
3324b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, "??");
3334b88c807SRodney W. Grimes 	else
3344b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, ttname);
3354b88c807SRodney W. Grimes }
3364b88c807SRodney W. Grimes 
3374b88c807SRodney W. Grimes void
3384b88c807SRodney W. Grimes started(k, ve)
3394b88c807SRodney W. Grimes 	KINFO *k;
3404b88c807SRodney W. Grimes 	VARENT *ve;
3414b88c807SRodney W. Grimes {
3424b88c807SRodney W. Grimes 	VAR *v;
3434b88c807SRodney W. Grimes 	static time_t now;
3444b88c807SRodney W. Grimes 	struct tm *tp;
3454b88c807SRodney W. Grimes 	char buf[100];
3464b88c807SRodney W. Grimes 
3474b88c807SRodney W. Grimes 	v = ve->var;
3484b88c807SRodney W. Grimes 	if (!k->ki_u.u_valid) {
3494b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, "-");
3504b88c807SRodney W. Grimes 		return;
3514b88c807SRodney W. Grimes 	}
3524b88c807SRodney W. Grimes 
3534b88c807SRodney W. Grimes 	tp = localtime(&k->ki_u.u_start.tv_sec);
3544b88c807SRodney W. Grimes 	if (!now)
3554b88c807SRodney W. Grimes 		(void)time(&now);
356656dcd43SGarrett Wollman 	if (now - k->ki_u.u_start.tv_sec < 24 * 3600) {
3574b88c807SRodney W. Grimes 		/* I *hate* SCCS... */
3584b88c807SRodney W. Grimes 		static char fmt[] = __CONCAT("%l:%", "M%p");
3594b88c807SRodney W. Grimes 		(void)strftime(buf, sizeof(buf) - 1, fmt, tp);
360656dcd43SGarrett Wollman 	} else if (now - k->ki_u.u_start.tv_sec < 7 * 86400) {
3614b88c807SRodney W. Grimes 		/* I *hate* SCCS... */
3624b88c807SRodney W. Grimes 		static char fmt[] = __CONCAT("%a%", "I%p");
3634b88c807SRodney W. Grimes 		(void)strftime(buf, sizeof(buf) - 1, fmt, tp);
3644b88c807SRodney W. Grimes 	} else
3654b88c807SRodney W. Grimes 		(void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
3664b88c807SRodney W. Grimes 	(void)printf("%-*s", v->width, buf);
3674b88c807SRodney W. Grimes }
3684b88c807SRodney W. Grimes 
3694b88c807SRodney W. Grimes void
3704b88c807SRodney W. Grimes lstarted(k, ve)
3714b88c807SRodney W. Grimes 	KINFO *k;
3724b88c807SRodney W. Grimes 	VARENT *ve;
3734b88c807SRodney W. Grimes {
3744b88c807SRodney W. Grimes 	VAR *v;
3754b88c807SRodney W. Grimes 	char buf[100];
3764b88c807SRodney W. Grimes 
3774b88c807SRodney W. Grimes 	v = ve->var;
3784b88c807SRodney W. Grimes 	if (!k->ki_u.u_valid) {
3794b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, "-");
3804b88c807SRodney W. Grimes 		return;
3814b88c807SRodney W. Grimes 	}
382e410bf23SPeter Wemm 	(void)strftime(buf, sizeof(buf) -1, "%c",
3834b88c807SRodney W. Grimes 	    localtime(&k->ki_u.u_start.tv_sec));
3844b88c807SRodney W. Grimes 	(void)printf("%-*s", v->width, buf);
3854b88c807SRodney W. Grimes }
3864b88c807SRodney W. Grimes 
3874b88c807SRodney W. Grimes void
3884b88c807SRodney W. Grimes wchan(k, ve)
3894b88c807SRodney W. Grimes 	KINFO *k;
3904b88c807SRodney W. Grimes 	VARENT *ve;
3914b88c807SRodney W. Grimes {
3924b88c807SRodney W. Grimes 	VAR *v;
3934b88c807SRodney W. Grimes 
3944b88c807SRodney W. Grimes 	v = ve->var;
3954b88c807SRodney W. Grimes 	if (KI_PROC(k)->p_wchan) {
3964b88c807SRodney W. Grimes 		if (KI_PROC(k)->p_wmesg)
3974b88c807SRodney W. Grimes 			(void)printf("%-*.*s", v->width, v->width,
3984b88c807SRodney W. Grimes 				      KI_EPROC(k)->e_wmesg);
3994b88c807SRodney W. Grimes 		else
4004b88c807SRodney W. Grimes 			(void)printf("%-*x", v->width,
4014b88c807SRodney W. Grimes 			    (int)KI_PROC(k)->p_wchan &~ KERNBASE);
4024b88c807SRodney W. Grimes 	} else
4034b88c807SRodney W. Grimes 		(void)printf("%-*s", v->width, "-");
4044b88c807SRodney W. Grimes }
4054b88c807SRodney W. Grimes 
4063a91de06SPoul-Henning Kamp #define pgtok(a)        (((a)*getpagesize())/1024)
4074b88c807SRodney W. Grimes 
4084b88c807SRodney W. Grimes void
4094b88c807SRodney W. Grimes vsize(k, ve)
4104b88c807SRodney W. Grimes 	KINFO *k;
4114b88c807SRodney W. Grimes 	VARENT *ve;
4124b88c807SRodney W. Grimes {
4134b88c807SRodney W. Grimes 	VAR *v;
4144b88c807SRodney W. Grimes 
4154b88c807SRodney W. Grimes 	v = ve->var;
4164b88c807SRodney W. Grimes 	(void)printf("%*d", v->width,
417dc8ab2b6SJohn Dyson 	    (KI_EPROC(k)->e_vm.vm_map.size/1024));
4184b88c807SRodney W. Grimes }
4194b88c807SRodney W. Grimes 
4204b88c807SRodney W. Grimes void
4214b88c807SRodney W. Grimes rssize(k, ve)
4224b88c807SRodney W. Grimes 	KINFO *k;
4234b88c807SRodney W. Grimes 	VARENT *ve;
4244b88c807SRodney W. Grimes {
4254b88c807SRodney W. Grimes 	VAR *v;
4264b88c807SRodney W. Grimes 
4274b88c807SRodney W. Grimes 	v = ve->var;
4284b88c807SRodney W. Grimes 	/* XXX don't have info about shared */
4294b88c807SRodney W. Grimes 	(void)printf("%*d", v->width, pgtok(KI_EPROC(k)->e_vm.vm_rssize));
4304b88c807SRodney W. Grimes }
4314b88c807SRodney W. Grimes 
4324b88c807SRodney W. Grimes void
4334b88c807SRodney W. Grimes p_rssize(k, ve)		/* doesn't account for text */
4344b88c807SRodney W. Grimes 	KINFO *k;
4354b88c807SRodney W. Grimes 	VARENT *ve;
4364b88c807SRodney W. Grimes {
4374b88c807SRodney W. Grimes 	VAR *v;
4384b88c807SRodney W. Grimes 
4394b88c807SRodney W. Grimes 	v = ve->var;
44078b09ffeSSteve Price 	(void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
4414b88c807SRodney W. Grimes }
4424b88c807SRodney W. Grimes 
4434b88c807SRodney W. Grimes void
4444b88c807SRodney W. Grimes cputime(k, ve)
4454b88c807SRodney W. Grimes 	KINFO *k;
4464b88c807SRodney W. Grimes 	VARENT *ve;
4474b88c807SRodney W. Grimes {
4484b88c807SRodney W. Grimes 	VAR *v;
4494b88c807SRodney W. Grimes 	long secs;
4504b88c807SRodney W. Grimes 	long psecs;	/* "parts" of a second. first micro, then centi */
4514b88c807SRodney W. Grimes 	char obuff[128];
4524b88c807SRodney W. Grimes 
4534b88c807SRodney W. Grimes 	v = ve->var;
4544b88c807SRodney W. Grimes 	if (KI_PROC(k)->p_stat == SZOMB || !k->ki_u.u_valid) {
4554b88c807SRodney W. Grimes 		secs = 0;
4564b88c807SRodney W. Grimes 		psecs = 0;
4574b88c807SRodney W. Grimes 	} else {
4584b88c807SRodney W. Grimes 		/*
4594b88c807SRodney W. Grimes 		 * This counts time spent handling interrupts.  We could
4604b88c807SRodney W. Grimes 		 * fix this, but it is not 100% trivial (and interrupt
4614b88c807SRodney W. Grimes 		 * time fractions only work on the sparc anyway).	XXX
4624b88c807SRodney W. Grimes 		 */
4634b88c807SRodney W. Grimes 		secs = KI_PROC(k)->p_rtime.tv_sec;
4644b88c807SRodney W. Grimes 		psecs = KI_PROC(k)->p_rtime.tv_usec;
4654b88c807SRodney W. Grimes 		if (sumrusage) {
4664b88c807SRodney W. Grimes 			secs += k->ki_u.u_cru.ru_utime.tv_sec +
4674b88c807SRodney W. Grimes 				k->ki_u.u_cru.ru_stime.tv_sec;
4684b88c807SRodney W. Grimes 			psecs += k->ki_u.u_cru.ru_utime.tv_usec +
4694b88c807SRodney W. Grimes 				k->ki_u.u_cru.ru_stime.tv_usec;
4704b88c807SRodney W. Grimes 		}
4714b88c807SRodney W. Grimes 		/*
4724b88c807SRodney W. Grimes 		 * round and scale to 100's
4734b88c807SRodney W. Grimes 		 */
4744b88c807SRodney W. Grimes 		psecs = (psecs + 5000) / 10000;
4754b88c807SRodney W. Grimes 		secs += psecs / 100;
4764b88c807SRodney W. Grimes 		psecs = psecs % 100;
4774b88c807SRodney W. Grimes 	}
4784b88c807SRodney W. Grimes 	(void)snprintf(obuff, sizeof(obuff),
4794b88c807SRodney W. Grimes 	    "%3ld:%02ld.%02ld", secs/60, secs%60, psecs);
4804b88c807SRodney W. Grimes 	(void)printf("%*s", v->width, obuff);
4814b88c807SRodney W. Grimes }
4824b88c807SRodney W. Grimes 
4834b88c807SRodney W. Grimes double
4844b88c807SRodney W. Grimes getpcpu(k)
4854b88c807SRodney W. Grimes 	KINFO *k;
4864b88c807SRodney W. Grimes {
4874b88c807SRodney W. Grimes 	struct proc *p;
4884b88c807SRodney W. Grimes 	static int failure;
4894b88c807SRodney W. Grimes 
4904b88c807SRodney W. Grimes 	if (!nlistread)
4914b88c807SRodney W. Grimes 		failure = donlist();
4924b88c807SRodney W. Grimes 	if (failure)
4934b88c807SRodney W. Grimes 		return (0.0);
4944b88c807SRodney W. Grimes 
4954b88c807SRodney W. Grimes 	p = KI_PROC(k);
4964b88c807SRodney W. Grimes #define	fxtofl(fixpt)	((double)(fixpt) / fscale)
4974b88c807SRodney W. Grimes 
4984b88c807SRodney W. Grimes 	/* XXX - I don't like this */
4994b88c807SRodney W. Grimes 	if (p->p_swtime == 0 || (p->p_flag & P_INMEM) == 0)
5004b88c807SRodney W. Grimes 		return (0.0);
5014b88c807SRodney W. Grimes 	if (rawcpu)
5020164b6d6SPeter Wemm 		return (10000.0 / sysconf(_SC_CLK_TCK) * fxtofl(p->p_pctcpu));
5030164b6d6SPeter Wemm 	return (10000.0 / sysconf(_SC_CLK_TCK) * fxtofl(p->p_pctcpu) /
5044b88c807SRodney W. Grimes 		(1.0 - exp(p->p_swtime * log(fxtofl(ccpu)))));
5054b88c807SRodney W. Grimes }
5064b88c807SRodney W. Grimes 
5074b88c807SRodney W. Grimes void
5084b88c807SRodney W. Grimes pcpu(k, ve)
5094b88c807SRodney W. Grimes 	KINFO *k;
5104b88c807SRodney W. Grimes 	VARENT *ve;
5114b88c807SRodney W. Grimes {
5124b88c807SRodney W. Grimes 	VAR *v;
5134b88c807SRodney W. Grimes 
5144b88c807SRodney W. Grimes 	v = ve->var;
5154b88c807SRodney W. Grimes 	(void)printf("%*.1f", v->width, getpcpu(k));
5164b88c807SRodney W. Grimes }
5174b88c807SRodney W. Grimes 
5184b88c807SRodney W. Grimes double
5194b88c807SRodney W. Grimes getpmem(k)
5204b88c807SRodney W. Grimes 	KINFO *k;
5214b88c807SRodney W. Grimes {
5224b88c807SRodney W. Grimes 	static int failure;
5234b88c807SRodney W. Grimes 	struct proc *p;
5244b88c807SRodney W. Grimes 	struct eproc *e;
5254b88c807SRodney W. Grimes 	double fracmem;
5264b88c807SRodney W. Grimes 	int szptudot;
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 	p = KI_PROC(k);
5344b88c807SRodney W. Grimes 	e = KI_EPROC(k);
5354b88c807SRodney W. Grimes 	if ((p->p_flag & P_INMEM) == 0)
5364b88c807SRodney W. Grimes 		return (0.0);
5374b88c807SRodney W. Grimes 	/* XXX want pmap ptpages, segtab, etc. (per architecture) */
5384b88c807SRodney W. Grimes 	szptudot = UPAGES;
5394b88c807SRodney W. Grimes 	/* XXX don't have info about shared */
5400cd02d20SPeter Wemm 	fracmem = ((float)e->e_vm.vm_rssize + szptudot)/mempages;
5414b88c807SRodney W. Grimes 	return (100.0 * fracmem);
5424b88c807SRodney W. Grimes }
5434b88c807SRodney W. Grimes 
5444b88c807SRodney W. Grimes void
5454b88c807SRodney W. Grimes pmem(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, getpmem(k));
5534b88c807SRodney W. Grimes }
5544b88c807SRodney W. Grimes 
5554b88c807SRodney W. Grimes void
5564b88c807SRodney W. Grimes pagein(k, ve)
5574b88c807SRodney W. Grimes 	KINFO *k;
5584b88c807SRodney W. Grimes 	VARENT *ve;
5594b88c807SRodney W. Grimes {
5604b88c807SRodney W. Grimes 	VAR *v;
5614b88c807SRodney W. Grimes 
5624b88c807SRodney W. Grimes 	v = ve->var;
5630fd510b7SJoerg Wunsch 	(void)printf("%*ld", v->width,
5644b88c807SRodney W. Grimes 	    k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0);
5654b88c807SRodney W. Grimes }
5664b88c807SRodney W. Grimes 
5674b88c807SRodney W. Grimes void
5684b88c807SRodney W. Grimes maxrss(k, ve)
5694b88c807SRodney W. Grimes 	KINFO *k;
5704b88c807SRodney W. Grimes 	VARENT *ve;
5714b88c807SRodney W. Grimes {
5724b88c807SRodney W. Grimes 	VAR *v;
5734b88c807SRodney W. Grimes 
5744b88c807SRodney W. Grimes 	v = ve->var;
575940cca66SPeter Wemm 	/* XXX not yet */
5764b88c807SRodney W. Grimes 	(void)printf("%*s", v->width, "-");
5774b88c807SRodney W. Grimes }
5784b88c807SRodney W. Grimes 
5794b88c807SRodney W. Grimes void
5804b88c807SRodney W. Grimes tsize(k, ve)
5814b88c807SRodney W. Grimes 	KINFO *k;
5824b88c807SRodney W. Grimes 	VARENT *ve;
5834b88c807SRodney W. Grimes {
5844b88c807SRodney W. Grimes 	VAR *v;
5854b88c807SRodney W. Grimes 
5864b88c807SRodney W. Grimes 	v = ve->var;
58778b09ffeSSteve Price 	(void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_tsize));
5884b88c807SRodney W. Grimes }
5894b88c807SRodney W. Grimes 
590ad863cacSSteve Price void
591ad863cacSSteve Price rtprior(k, ve)
592ad863cacSSteve Price 	KINFO *k;
593ad863cacSSteve Price 	VARENT *ve;
594ad863cacSSteve Price {
595ad863cacSSteve Price 	VAR *v;
596ad863cacSSteve Price 	struct rtprio *prtp;
597ad863cacSSteve Price 	char str[8];
598ad863cacSSteve Price 	unsigned prio, type;
599ad863cacSSteve Price 
600ad863cacSSteve Price 	v = ve->var;
601ad863cacSSteve Price 	prtp = (struct rtprio *) ((char *)KI_PROC(k) + v->off);
602ad863cacSSteve Price 	prio = prtp->prio;
603ad863cacSSteve Price 	type = prtp->type;
604ad863cacSSteve Price 	switch (type) {
605ad863cacSSteve Price 	case RTP_PRIO_REALTIME:
606ad863cacSSteve Price 		snprintf(str, sizeof(str), "real:%u", prio);
607ad863cacSSteve Price 		break;
608ad863cacSSteve Price 	case RTP_PRIO_NORMAL:
609ad863cacSSteve Price 		strncpy(str, "normal", sizeof(str));
610ad863cacSSteve Price 		break;
611ad863cacSSteve Price 	case RTP_PRIO_IDLE:
612ad863cacSSteve Price 		snprintf(str, sizeof(str), "idle:%u", prio);
613ad863cacSSteve Price 		break;
614ad863cacSSteve Price 	default:
615ad863cacSSteve Price 		snprintf(str, sizeof(str), "%u:%u", type, prio);
616ad863cacSSteve Price 		break;
617ad863cacSSteve Price 	}
618ad863cacSSteve Price 	str[sizeof(str) - 1] = '\0';
619ad863cacSSteve Price 	(void)printf("%*s", v->width, str);
620ad863cacSSteve Price }
621ad863cacSSteve Price 
6224b88c807SRodney W. Grimes /*
6234b88c807SRodney W. Grimes  * Generic output routines.  Print fields from various prototype
6244b88c807SRodney W. Grimes  * structures.
6254b88c807SRodney W. Grimes  */
6264b88c807SRodney W. Grimes static void
6274b88c807SRodney W. Grimes printval(bp, v)
6284b88c807SRodney W. Grimes 	char *bp;
6294b88c807SRodney W. Grimes 	VAR *v;
6304b88c807SRodney W. Grimes {
6314b88c807SRodney W. Grimes 	static char ofmt[32] = "%";
6324b88c807SRodney W. Grimes 	char *fcp, *cp;
6334b88c807SRodney W. Grimes 
6344b88c807SRodney W. Grimes 	cp = ofmt + 1;
6354b88c807SRodney W. Grimes 	fcp = v->fmt;
6364b88c807SRodney W. Grimes 	if (v->flag & LJUST)
6374b88c807SRodney W. Grimes 		*cp++ = '-';
6384b88c807SRodney W. Grimes 	*cp++ = '*';
6390fd510b7SJoerg Wunsch 	while ((*cp++ = *fcp++));
6404b88c807SRodney W. Grimes 
6414b88c807SRodney W. Grimes 	switch (v->type) {
6424b88c807SRodney W. Grimes 	case CHAR:
6434b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(char *)bp);
6444b88c807SRodney W. Grimes 		break;
6454b88c807SRodney W. Grimes 	case UCHAR:
6464b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(u_char *)bp);
6474b88c807SRodney W. Grimes 		break;
6484b88c807SRodney W. Grimes 	case SHORT:
6494b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(short *)bp);
6504b88c807SRodney W. Grimes 		break;
6514b88c807SRodney W. Grimes 	case USHORT:
6524b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(u_short *)bp);
6534b88c807SRodney W. Grimes 		break;
6544b88c807SRodney W. Grimes 	case LONG:
6554b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(long *)bp);
6564b88c807SRodney W. Grimes 		break;
6574b88c807SRodney W. Grimes 	case ULONG:
6584b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(u_long *)bp);
6594b88c807SRodney W. Grimes 		break;
6604b88c807SRodney W. Grimes 	case KPTR:
6614b88c807SRodney W. Grimes 		(void)printf(ofmt, v->width, *(u_long *)bp &~ KERNBASE);
6624b88c807SRodney W. Grimes 		break;
6634b88c807SRodney W. Grimes 	default:
6644b88c807SRodney W. Grimes 		errx(1, "unknown type %d", v->type);
6654b88c807SRodney W. Grimes 	}
6664b88c807SRodney W. Grimes }
6674b88c807SRodney W. Grimes 
6684b88c807SRodney W. Grimes void
6694b88c807SRodney W. Grimes pvar(k, ve)
6704b88c807SRodney W. Grimes 	KINFO *k;
6714b88c807SRodney W. Grimes 	VARENT *ve;
6724b88c807SRodney W. Grimes {
6734b88c807SRodney W. Grimes 	VAR *v;
6744b88c807SRodney W. Grimes 
6754b88c807SRodney W. Grimes 	v = ve->var;
6764b88c807SRodney W. Grimes 	printval((char *)((char *)KI_PROC(k) + v->off), v);
6774b88c807SRodney W. Grimes }
6784b88c807SRodney W. Grimes 
6794b88c807SRodney W. Grimes void
6804b88c807SRodney W. Grimes evar(k, ve)
6814b88c807SRodney W. Grimes 	KINFO *k;
6824b88c807SRodney W. Grimes 	VARENT *ve;
6834b88c807SRodney W. Grimes {
6844b88c807SRodney W. Grimes 	VAR *v;
6854b88c807SRodney W. Grimes 
6864b88c807SRodney W. Grimes 	v = ve->var;
6874b88c807SRodney W. Grimes 	printval((char *)((char *)KI_EPROC(k) + v->off), v);
6884b88c807SRodney W. Grimes }
6894b88c807SRodney W. Grimes 
6904b88c807SRodney W. Grimes void
6914b88c807SRodney W. Grimes uvar(k, ve)
6924b88c807SRodney W. Grimes 	KINFO *k;
6934b88c807SRodney W. Grimes 	VARENT *ve;
6944b88c807SRodney W. Grimes {
6954b88c807SRodney W. Grimes 	VAR *v;
6964b88c807SRodney W. Grimes 
6974b88c807SRodney W. Grimes 	v = ve->var;
6984b88c807SRodney W. Grimes 	if (k->ki_u.u_valid)
6994b88c807SRodney W. Grimes 		printval((char *)((char *)&k->ki_u + v->off), v);
7004b88c807SRodney W. Grimes 	else
7014b88c807SRodney W. Grimes 		(void)printf("%*s", v->width, "-");
7024b88c807SRodney W. Grimes }
7034b88c807SRodney W. Grimes 
7044b88c807SRodney W. Grimes void
7054b88c807SRodney W. Grimes rvar(k, ve)
7064b88c807SRodney W. Grimes 	KINFO *k;
7074b88c807SRodney W. Grimes 	VARENT *ve;
7084b88c807SRodney W. Grimes {
7094b88c807SRodney W. Grimes 	VAR *v;
7104b88c807SRodney W. Grimes 
7114b88c807SRodney W. Grimes 	v = ve->var;
7124b88c807SRodney W. Grimes 	if (k->ki_u.u_valid)
7134b88c807SRodney W. Grimes 		printval((char *)((char *)(&k->ki_u.u_ru) + v->off), v);
7144b88c807SRodney W. Grimes 	else
7154b88c807SRodney W. Grimes 		(void)printf("%*s", v->width, "-");
7164b88c807SRodney W. Grimes }
717