xref: /freebsd/bin/ps/print.c (revision 74bf4e164ba5851606a27d4feff27717452583e5)
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #if 0
31 #ifndef lint
32 static char sccsid[] = "@(#)print.c	8.6 (Berkeley) 4/16/94";
33 #endif /* not lint */
34 #endif
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <sys/param.h>
40 #include <sys/time.h>
41 #include <sys/resource.h>
42 #include <sys/proc.h>
43 #include <sys/stat.h>
44 
45 #include <sys/mac.h>
46 #include <sys/user.h>
47 #include <sys/sysctl.h>
48 
49 #include <err.h>
50 #include <grp.h>
51 #include <langinfo.h>
52 #include <locale.h>
53 #include <math.h>
54 #include <nlist.h>
55 #include <pwd.h>
56 #include <stddef.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 #include <vis.h>
62 
63 #include "ps.h"
64 
65 #define	ps_pgtok(a)	(((a) * getpagesize()) / 1024)
66 
67 void
68 printheader(void)
69 {
70 	VAR *v;
71 	struct varent *vent;
72 
73 	STAILQ_FOREACH(vent, &varlist, next_ve)
74 		if (*vent->header != '\0')
75 			break;
76 	if (!vent)
77 		return;
78 
79 	STAILQ_FOREACH(vent, &varlist, next_ve) {
80 		v = vent->var;
81 		if (v->flag & LJUST) {
82 			if (STAILQ_NEXT(vent, next_ve) == NULL)	/* last one */
83 				(void)printf("%s", vent->header);
84 			else
85 				(void)printf("%-*s", v->width, vent->header);
86 		} else
87 			(void)printf("%*s", v->width, vent->header);
88 		if (STAILQ_NEXT(vent, next_ve) != NULL)
89 			(void)putchar(' ');
90 	}
91 	(void)putchar('\n');
92 }
93 
94 void
95 arguments(KINFO *k, VARENT *ve)
96 {
97 	VAR *v;
98 	int left;
99 	char *cp, *vis_args;
100 
101 	v = ve->var;
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) {
106 		/* last field */
107 		if (termwidth == UNLIMITED) {
108 			(void)printf("%s", vis_args);
109 		} else {
110 			left = termwidth - (totwidth - v->width);
111 			if (left < 1) /* already wrapped, just use std width */
112 				left = v->width;
113 			for (cp = vis_args; --left >= 0 && *cp != '\0';)
114 				(void)putchar(*cp++);
115 		}
116 	} else {
117 		(void)printf("%-*.*s", v->width, v->width, vis_args);
118 	}
119 	free(vis_args);
120 }
121 
122 void
123 command(KINFO *k, VARENT *ve)
124 {
125 	VAR *v;
126 	int left;
127 	char *cp, *vis_env, *vis_args;
128 
129 	v = ve->var;
130 	if (cflag) {
131 		/* If it is the last field, then don't pad */
132 		if (STAILQ_NEXT(ve, next_ve) == NULL)
133 			(void)printf("%s", k->ki_p->ki_comm);
134 		else
135 			(void)printf("%-*s", v->width, k->ki_p->ki_comm);
136 		return;
137 	}
138 	if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
139 		errx(1, "malloc failed");
140 	strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
141 	if (k->ki_env) {
142 		if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
143 			errx(1, "malloc failed");
144 		strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
145 	} else
146 		vis_env = NULL;
147 
148 	if (STAILQ_NEXT(ve, next_ve) == NULL) {
149 		/* last field */
150 		if (termwidth == UNLIMITED) {
151 			if (vis_env)
152 				(void)printf("%s ", vis_env);
153 			(void)printf("%s", vis_args);
154 		} else {
155 			left = termwidth - (totwidth - v->width);
156 			if (left < 1) /* already wrapped, just use std width */
157 				left = v->width;
158 			if ((cp = vis_env) != NULL) {
159 				while (--left >= 0 && *cp)
160 					(void)putchar(*cp++);
161 				if (--left >= 0)
162 					putchar(' ');
163 			}
164 			for (cp = vis_args; --left >= 0 && *cp != '\0';)
165 				(void)putchar(*cp++);
166 		}
167 	} else
168 		/* XXX env? */
169 		(void)printf("%-*.*s", v->width, v->width, vis_args);
170 	free(vis_args);
171 	if (vis_env != NULL)
172 		free(vis_env);
173 }
174 
175 void
176 ucomm(KINFO *k, VARENT *ve)
177 {
178 	VAR *v;
179 
180 	v = ve->var;
181 	if (STAILQ_NEXT(ve, next_ve) == NULL)	/* last field, don't pad */
182 		(void)printf("%s", k->ki_p->ki_comm);
183 	else
184 		(void)printf("%-*s", v->width, k->ki_p->ki_comm);
185 }
186 
187 void
188 logname(KINFO *k, VARENT *ve)
189 {
190 	VAR *v;
191 	char *s;
192 
193 	v = ve->var;
194 	(void)printf("%-*s", v->width, (s = k->ki_p->ki_login, *s) ? s : "-");
195 }
196 
197 void
198 state(KINFO *k, VARENT *ve)
199 {
200 	int flag, sflag, tdflags;
201 	char *cp;
202 	VAR *v;
203 	char buf[16];
204 
205 	v = ve->var;
206 	flag = k->ki_p->ki_flag;
207 	sflag = k->ki_p->ki_sflag;
208 	tdflags = k->ki_p->ki_tdflags;	/* XXXKSE */
209 	cp = buf;
210 
211 	switch (k->ki_p->ki_stat) {
212 
213 	case SSTOP:
214 		*cp = 'T';
215 		break;
216 
217 	case SSLEEP:
218 		if (tdflags & TDF_SINTR)	/* interruptable (long) */
219 			*cp = k->ki_p->ki_slptime >= MAXSLP ? 'I' : 'S';
220 		else
221 			*cp = 'D';
222 		break;
223 
224 	case SRUN:
225 	case SIDL:
226 		*cp = 'R';
227 		break;
228 
229 	case SWAIT:
230 		*cp = 'W';
231 		break;
232 
233 	case SLOCK:
234 		*cp = 'L';
235 		break;
236 
237 	case SZOMB:
238 		*cp = 'Z';
239 		break;
240 
241 	default:
242 		*cp = '?';
243 	}
244 	cp++;
245 	if (!(sflag & PS_INMEM))
246 		*cp++ = 'W';
247 	if (k->ki_p->ki_nice < NZERO)
248 		*cp++ = '<';
249 	else if (k->ki_p->ki_nice > NZERO)
250 		*cp++ = 'N';
251 	if (flag & P_TRACED)
252 		*cp++ = 'X';
253 	if (flag & P_WEXIT && k->ki_p->ki_stat != SZOMB)
254 		*cp++ = 'E';
255 	if (flag & P_PPWAIT)
256 		*cp++ = 'V';
257 	if ((flag & P_SYSTEM) || k->ki_p->ki_lock > 0)
258 		*cp++ = 'L';
259 	if (k->ki_p->ki_kiflag & KI_SLEADER)
260 		*cp++ = 's';
261 	if ((flag & P_CONTROLT) && k->ki_p->ki_pgid == k->ki_p->ki_tpgid)
262 		*cp++ = '+';
263 	if (flag & P_JAILED)
264 		*cp++ = 'J';
265 	*cp = '\0';
266 	(void)printf("%-*s", v->width, buf);
267 }
268 
269 #define	scalepri(x)	((x) - PZERO)
270 
271 void
272 pri(KINFO *k, VARENT *ve)
273 {
274 	VAR *v;
275 
276 	v = ve->var;
277 	(void)printf("%*d", v->width, scalepri(k->ki_p->ki_pri.pri_level));
278 }
279 
280 void
281 upr(KINFO *k, VARENT *ve)
282 {
283 	VAR *v;
284 
285 	v = ve->var;
286 	(void)printf("%*d", v->width, scalepri(k->ki_p->ki_pri.pri_user));
287 }
288 #undef scalepri
289 
290 void
291 uname(KINFO *k, VARENT *ve)
292 {
293 	VAR *v;
294 
295 	v = ve->var;
296 	(void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_uid, 0));
297 }
298 
299 int
300 s_uname(KINFO *k)
301 {
302 	return (strlen(user_from_uid(k->ki_p->ki_uid, 0)));
303 }
304 
305 void
306 rgroupname(KINFO *k, VARENT *ve)
307 {
308 	VAR *v;
309 
310 	v = ve->var;
311 	(void)printf("%-*s", v->width, group_from_gid(k->ki_p->ki_rgid, 0));
312 }
313 
314 int
315 s_rgroupname(KINFO *k)
316 {
317 	return (strlen(group_from_gid(k->ki_p->ki_rgid, 0)));
318 }
319 
320 void
321 runame(KINFO *k, VARENT *ve)
322 {
323 	VAR *v;
324 
325 	v = ve->var;
326 	(void)printf("%-*s", v->width, user_from_uid(k->ki_p->ki_ruid, 0));
327 }
328 
329 int
330 s_runame(KINFO *k)
331 {
332 	return (strlen(user_from_uid(k->ki_p->ki_ruid, 0)));
333 }
334 
335 
336 void
337 tdev(KINFO *k, VARENT *ve)
338 {
339 	VAR *v;
340 	dev_t dev;
341 	char buff[16];
342 
343 	v = ve->var;
344 	dev = k->ki_p->ki_tdev;
345 	if (dev == NODEV)
346 		(void)printf("%*s", v->width, "??");
347 	else {
348 		(void)snprintf(buff, sizeof(buff),
349 		    "%d/%d", major(dev), minor(dev));
350 		(void)printf("%*s", v->width, buff);
351 	}
352 }
353 
354 void
355 tname(KINFO *k, VARENT *ve)
356 {
357 	VAR *v;
358 	dev_t dev;
359 	char *ttname;
360 
361 	v = ve->var;
362 	dev = k->ki_p->ki_tdev;
363 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
364 		(void)printf("%*s ", v->width - 1, "??");
365 	else {
366 		if (strncmp(ttname, "tty", 3) == 0 ||
367 		    strncmp(ttname, "cua", 3) == 0)
368 			ttname += 3;
369 		(void)printf("%*.*s%c", v->width - 1, v->width - 1, ttname,
370 		    k->ki_p->ki_kiflag & KI_CTTY ? ' ' : '-');
371 	}
372 }
373 
374 void
375 longtname(KINFO *k, VARENT *ve)
376 {
377 	VAR *v;
378 	dev_t dev;
379 	char *ttname;
380 
381 	v = ve->var;
382 	dev = k->ki_p->ki_tdev;
383 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
384 		(void)printf("%-*s", v->width, "??");
385 	else
386 		(void)printf("%-*s", v->width, ttname);
387 }
388 
389 void
390 started(KINFO *k, VARENT *ve)
391 {
392 	VAR *v;
393 	time_t then;
394 	struct tm *tp;
395 	static int use_ampm = -1;
396 	char buf[100];
397 
398 	v = ve->var;
399 	if (!k->ki_valid) {
400 		(void)printf("%-*s", v->width, "-");
401 		return;
402 	}
403 	if (use_ampm < 0)
404 		use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
405 	then = k->ki_p->ki_start.tv_sec;
406 	tp = localtime(&then);
407 	if (now - k->ki_p->ki_start.tv_sec < 24 * 3600) {
408 		(void)strftime(buf, sizeof(buf),
409 		    use_ampm ? "%l:%M%p" : "%k:%M  ", tp);
410 	} else if (now - k->ki_p->ki_start.tv_sec < 7 * 86400) {
411 		(void)strftime(buf, sizeof(buf),
412 		    use_ampm ? "%a%I%p" : "%a%H  ", tp);
413 	} else
414 		(void)strftime(buf, sizeof(buf), "%e%b%y", tp);
415 	(void)printf("%-*s", v->width, buf);
416 }
417 
418 void
419 lstarted(KINFO *k, VARENT *ve)
420 {
421 	VAR *v;
422 	time_t then;
423 	char buf[100];
424 
425 	v = ve->var;
426 	if (!k->ki_valid) {
427 		(void)printf("%-*s", v->width, "-");
428 		return;
429 	}
430 	then = k->ki_p->ki_start.tv_sec;
431 	(void)strftime(buf, sizeof(buf), "%c", localtime(&then));
432 	(void)printf("%-*s", v->width, buf);
433 }
434 
435 void
436 lockname(KINFO *k, VARENT *ve)
437 {
438 	VAR *v;
439 
440 	v = ve->var;
441 	if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
442 		if (k->ki_p->ki_lockname[0] != 0)
443 			(void)printf("%-*.*s", v->width, v->width,
444 			    k->ki_p->ki_lockname);
445 		else
446 			(void)printf("%-*s", v->width, "???");
447 	} else
448 		(void)printf("%-*s", v->width, "-");
449 }
450 
451 void
452 wchan(KINFO *k, VARENT *ve)
453 {
454 	VAR *v;
455 
456 	v = ve->var;
457 	if (k->ki_p->ki_wchan) {
458 		if (k->ki_p->ki_wmesg[0] != 0)
459 			(void)printf("%-*.*s", v->width, v->width,
460 			    k->ki_p->ki_wmesg);
461 		else
462 			(void)printf("%-*lx", v->width,
463 			    (long)k->ki_p->ki_wchan);
464 	} else
465 		(void)printf("%-*s", v->width, "-");
466 }
467 
468 void
469 nwchan(KINFO *k, VARENT *ve)
470 {
471 	VAR *v;
472 
473 	v = ve->var;
474 	if (k->ki_p->ki_wchan) {
475 		(void)printf("%0*lx", v->width,
476 		    (long)k->ki_p->ki_wchan);
477 	} else
478 		(void)printf("%-*s", v->width, "-");
479 }
480 
481 void
482 mwchan(KINFO *k, VARENT *ve)
483 {
484 	VAR *v;
485 
486 	v = ve->var;
487 	if (k->ki_p->ki_wchan) {
488 		if (k->ki_p->ki_wmesg[0] != 0)
489 			(void)printf("%-*.*s", v->width, v->width,
490 			    k->ki_p->ki_wmesg);
491 		else
492 			(void)printf("%-*lx", v->width,
493 			    (long)k->ki_p->ki_wchan);
494 	} else if (k->ki_p->ki_kiflag & KI_LOCKBLOCK) {
495 		if (k->ki_p->ki_lockname[0]) {
496 			(void)printf("%-*.*s", v->width, v->width,
497 			    k->ki_p->ki_lockname);
498 		} else
499 			(void)printf("%-*s", v->width, "???");
500 	} else
501 		(void)printf("%-*s", v->width, "-");
502 }
503 
504 void
505 vsize(KINFO *k, VARENT *ve)
506 {
507 	VAR *v;
508 
509 	v = ve->var;
510 	(void)printf("%*lu", v->width, (u_long)(k->ki_p->ki_size / 1024));
511 }
512 
513 void
514 cputime(KINFO *k, VARENT *ve)
515 {
516 	VAR *v;
517 	long secs;
518 	long psecs;	/* "parts" of a second. first micro, then centi */
519 	char obuff[128];
520 	static char decimal_point;
521 
522 	if (decimal_point == '\0')
523 		decimal_point = localeconv()->decimal_point[0];
524 	v = ve->var;
525 	if (!k->ki_valid) {
526 		secs = 0;
527 		psecs = 0;
528 	} else {
529 		/*
530 		 * This counts time spent handling interrupts.  We could
531 		 * fix this, but it is not 100% trivial (and interrupt
532 		 * time fractions only work on the sparc anyway).	XXX
533 		 */
534 		secs = k->ki_p->ki_runtime / 1000000;
535 		psecs = k->ki_p->ki_runtime % 1000000;
536 		if (sumrusage) {
537 			secs += k->ki_p->ki_childtime.tv_sec;
538 			psecs += k->ki_p->ki_childtime.tv_usec;
539 		}
540 		/*
541 		 * round and scale to 100's
542 		 */
543 		psecs = (psecs + 5000) / 10000;
544 		secs += psecs / 100;
545 		psecs = psecs % 100;
546 	}
547 	(void)snprintf(obuff, sizeof(obuff), "%3ld:%02ld%c%02ld",
548 	    secs / 60, secs % 60, decimal_point, psecs);
549 	(void)printf("%*s", v->width, obuff);
550 }
551 
552 void
553 elapsed(KINFO *k, VARENT *ve)
554 {
555 	VAR *v;
556 	time_t val;
557 	int days, hours, mins, secs;
558 	char obuff[128];
559 
560 	v = ve->var;
561 	val = now - k->ki_p->ki_start.tv_sec;
562 	days = val / (24 * 60 * 60);
563 	val %= 24 * 60 * 60;
564 	hours = val / (60 * 60);
565 	val %= 60 * 60;
566 	mins = val / 60;
567 	secs = val % 60;
568 	if (days != 0)
569 		(void)snprintf(obuff, sizeof(obuff), "%3d-%02d:%02d:%02d",
570 		    days, hours, mins, secs);
571 	else if (hours != 0)
572 		(void)snprintf(obuff, sizeof(obuff), "%02d:%02d:%02d",
573 		    hours, mins, secs);
574 	else
575 		(void)snprintf(obuff, sizeof(obuff), "%02d:%02d", mins, secs);
576 	(void)printf("%*s", v->width, obuff);
577 }
578 
579 double
580 getpcpu(const KINFO *k)
581 {
582 	static int failure;
583 
584 	if (!nlistread)
585 		failure = donlist();
586 	if (failure)
587 		return (0.0);
588 
589 #define	fxtofl(fixpt)	((double)(fixpt) / fscale)
590 
591 	/* XXX - I don't like this */
592 	if (k->ki_p->ki_swtime == 0 || (k->ki_p->ki_sflag & PS_INMEM) == 0)
593 		return (0.0);
594 	if (rawcpu)
595 		return (100.0 * fxtofl(k->ki_p->ki_pctcpu));
596 	return (100.0 * fxtofl(k->ki_p->ki_pctcpu) /
597 		(1.0 - exp(k->ki_p->ki_swtime * log(fxtofl(ccpu)))));
598 }
599 
600 void
601 pcpu(KINFO *k, VARENT *ve)
602 {
603 	VAR *v;
604 
605 	v = ve->var;
606 	(void)printf("%*.1f", v->width, getpcpu(k));
607 }
608 
609 static double
610 getpmem(KINFO *k)
611 {
612 	static int failure;
613 	double fracmem;
614 
615 	if (!nlistread)
616 		failure = donlist();
617 	if (failure)
618 		return (0.0);
619 
620 	if ((k->ki_p->ki_sflag & PS_INMEM) == 0)
621 		return (0.0);
622 	/* XXX want pmap ptpages, segtab, etc. (per architecture) */
623 	/* XXX don't have info about shared */
624 	fracmem = ((float)k->ki_p->ki_rssize) / mempages;
625 	return (100.0 * fracmem);
626 }
627 
628 void
629 pmem(KINFO *k, VARENT *ve)
630 {
631 	VAR *v;
632 
633 	v = ve->var;
634 	(void)printf("%*.1f", v->width, getpmem(k));
635 }
636 
637 void
638 pagein(KINFO *k, VARENT *ve)
639 {
640 	VAR *v;
641 
642 	v = ve->var;
643 	(void)printf("%*ld", v->width,
644 	    k->ki_valid ? k->ki_p->ki_rusage.ru_majflt : 0);
645 }
646 
647 /* ARGSUSED */
648 void
649 maxrss(KINFO *k __unused, VARENT *ve)
650 {
651 	VAR *v;
652 
653 	v = ve->var;
654 	/* XXX not yet */
655 	(void)printf("%*s", v->width, "-");
656 }
657 
658 void
659 priorityr(KINFO *k, VARENT *ve)
660 {
661 	VAR *v;
662 	struct priority *lpri;
663 	char str[8];
664 	unsigned class, level;
665 
666 	v = ve->var;
667 	lpri = &k->ki_p->ki_pri;
668 	class = lpri->pri_class;
669 	level = lpri->pri_level;
670 	switch (class) {
671 	case PRI_ITHD:
672 		snprintf(str, sizeof(str), "intr:%u", level);
673 		break;
674 	case PRI_REALTIME:
675 		snprintf(str, sizeof(str), "real:%u", level);
676 		break;
677 	case PRI_TIMESHARE:
678 		strncpy(str, "normal", sizeof(str));
679 		break;
680 	case PRI_IDLE:
681 		snprintf(str, sizeof(str), "idle:%u", level);
682 		break;
683 	default:
684 		snprintf(str, sizeof(str), "%u:%u", class, level);
685 		break;
686 	}
687 	str[sizeof(str) - 1] = '\0';
688 	(void)printf("%*s", v->width, str);
689 }
690 
691 /*
692  * Generic output routines.  Print fields from various prototype
693  * structures.
694  */
695 static void
696 printval(void *bp, VAR *v)
697 {
698 	static char ofmt[32] = "%";
699 	const char *fcp;
700 	char *cp;
701 
702 	cp = ofmt + 1;
703 	fcp = v->fmt;
704 	if (v->flag & LJUST)
705 		*cp++ = '-';
706 	*cp++ = '*';
707 	while ((*cp++ = *fcp++));
708 
709 #define	CHKINF127(n)	(((n) > 127) && (v->flag & INF127) ? 127 : (n))
710 
711 	switch (v->type) {
712 	case CHAR:
713 		(void)printf(ofmt, v->width, *(char *)bp);
714 		break;
715 	case UCHAR:
716 		(void)printf(ofmt, v->width, *(u_char *)bp);
717 		break;
718 	case SHORT:
719 		(void)printf(ofmt, v->width, *(short *)bp);
720 		break;
721 	case USHORT:
722 		(void)printf(ofmt, v->width, *(u_short *)bp);
723 		break;
724 	case INT:
725 		(void)printf(ofmt, v->width, *(int *)bp);
726 		break;
727 	case UINT:
728 		(void)printf(ofmt, v->width, CHKINF127(*(u_int *)bp));
729 		break;
730 	case LONG:
731 		(void)printf(ofmt, v->width, *(long *)bp);
732 		break;
733 	case ULONG:
734 		(void)printf(ofmt, v->width, *(u_long *)bp);
735 		break;
736 	case KPTR:
737 		(void)printf(ofmt, v->width, *(u_long *)bp);
738 		break;
739 	case PGTOK:
740 		(void)printf(ofmt, v->width, ps_pgtok(*(u_long *)bp));
741 		break;
742 	default:
743 		errx(1, "unknown type %d", v->type);
744 	}
745 }
746 
747 void
748 kvar(KINFO *k, VARENT *ve)
749 {
750 	VAR *v;
751 
752 	v = ve->var;
753 	printval((char *)((char *)k->ki_p + v->off), v);
754 }
755 
756 void
757 rvar(KINFO *k, VARENT *ve)
758 {
759 	VAR *v;
760 
761 	v = ve->var;
762 	if (k->ki_valid)
763 		printval((char *)((char *)(&k->ki_p->ki_rusage) + v->off), v);
764 	else
765 		(void)printf("%*s", v->width, "-");
766 }
767 
768 void
769 emulname(KINFO *k, VARENT *ve)
770 {
771 	VAR *v;
772 
773 	v = ve->var;
774 	printf("%-*s", v->width, *k->ki_p->ki_emul ? k->ki_p->ki_emul : "-");
775 }
776 
777 void
778 label(KINFO *k, VARENT *ve)
779 {
780 	char *string;
781 	VAR *v;
782 	mac_t proclabel;
783 	int error;
784 
785 	v = ve->var;
786 	string = NULL;
787 	if (mac_prepare_process_label(&proclabel) == -1) {
788 		warn("mac_prepare_process_label");
789 		goto out;
790 	}
791 	error = mac_get_pid(k->ki_p->ki_pid, proclabel);
792 	if (error == 0) {
793 		if (mac_to_text(proclabel, &string) == -1)
794 			string = NULL;
795 	}
796 	mac_free(proclabel);
797 out:
798 	if (string != NULL) {
799 		(void)printf("%-*s", v->width, string);
800 		free(string);
801 	} else
802 		(void)printf("%-*s", v->width, "  -");
803 	return;
804 }
805 
806 int
807 s_label(KINFO *k)
808 {
809 	char *string = NULL;
810 	mac_t proclabel;
811 	int error, size = 0;
812 
813 	if (mac_prepare_process_label(&proclabel) == -1) {
814 		warn("mac_prepare_process_label");
815 		return (0);
816 	}
817 	error = mac_get_pid(k->ki_p->ki_pid, proclabel);
818 	if (error == 0 && mac_to_text(proclabel, &string) == 0) {
819 		size = strlen(string);
820 		free(string);
821 	}
822 	mac_free(proclabel);
823 	return (size);
824 }
825