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