xref: /freebsd/bin/ps/print.c (revision b9c36cc755002809a7d7c7109e3425fdfca036d2)
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 #include <libxo/xo.h>
65 
66 #include "ps.h"
67 
68 #define	COMMAND_WIDTH	16
69 #define	ARGUMENTS_WIDTH	16
70 
71 #define	ps_pgtok(a)	(((a) * getpagesize()) / 1024)
72 
73 void
74 printheader(void)
75 {
76 	VAR *v;
77 	struct varent *vent;
78 
79 	STAILQ_FOREACH(vent, &varlist, next_ve)
80 		if (*vent->header != '\0')
81 			break;
82 	if (!vent)
83 		return;
84 
85 	STAILQ_FOREACH(vent, &varlist, next_ve) {
86 		v = vent->var;
87 		if (v->flag & LJUST) {
88 			if (STAILQ_NEXT(vent, next_ve) == NULL)	/* last one */
89 				xo_emit("{T:/%s}", vent->header);
90 			else
91 				xo_emit("{T:/%-*s}", v->width, vent->header);
92 		} else
93 			xo_emit("{T:/%*s}", v->width, vent->header);
94 		if (STAILQ_NEXT(vent, next_ve) != NULL)
95 			xo_emit("{P: }");
96 	}
97 	xo_emit("\n");
98 }
99 
100 char *
101 arguments(KINFO *k, VARENT *ve)
102 {
103 	char *vis_args;
104 
105 	if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
106 		xo_errx(1, "malloc failed");
107 	strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
108 
109 	if (STAILQ_NEXT(ve, next_ve) != NULL && strlen(vis_args) > ARGUMENTS_WIDTH)
110 		vis_args[ARGUMENTS_WIDTH] = '\0';
111 
112 	return (vis_args);
113 }
114 
115 char *
116 command(KINFO *k, VARENT *ve)
117 {
118 	char *vis_args, *vis_env, *str;
119 
120 	if (cflag) {
121 		/* If it is the last field, then don't pad */
122 		if (STAILQ_NEXT(ve, next_ve) == NULL) {
123 			asprintf(&str, "%s%s%s%s%s",
124 			    k->ki_d.prefix ? k->ki_d.prefix : "",
125 			    k->ki_p->ki_comm,
126 			    (showthreads && k->ki_p->ki_numthreads > 1) ? "/" : "",
127 			    (showthreads && k->ki_p->ki_numthreads > 1) ? k->ki_p->ki_tdname : "",
128 			    (showthreads && k->ki_p->ki_numthreads > 1) ? k->ki_p->ki_moretdname : "");
129 		} else
130 			str = strdup(k->ki_p->ki_comm);
131 
132 		return (str);
133 	}
134 	if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
135 		xo_errx(1, "malloc failed");
136 	strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
137 
138 	if (STAILQ_NEXT(ve, next_ve) == NULL) {
139 		/* last field */
140 
141 		if (k->ki_env) {
142 			if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1))
143 			    == NULL)
144 				xo_errx(1, "malloc failed");
145 			strvis(vis_env, k->ki_env,
146 			    VIS_TAB | VIS_NL | VIS_NOSLASH);
147 		} else
148 			vis_env = NULL;
149 
150 		asprintf(&str, "%s%s%s%s",
151 		    k->ki_d.prefix ? k->ki_d.prefix : "",
152 		    vis_env ? vis_env : "",
153 		    vis_env ? " " : "",
154 		    vis_args);
155 
156 		if (vis_env != NULL)
157 			free(vis_env);
158 		free(vis_args);
159 	} else {
160 		/* ki_d.prefix & ki_env aren't shown for interim fields */
161 		str = vis_args;
162 
163 		if (strlen(str) > COMMAND_WIDTH)
164 			str[COMMAND_WIDTH] = '\0';
165 	}
166 
167 	return (str);
168 }
169 
170 char *
171 ucomm(KINFO *k, VARENT *ve)
172 {
173 	char *str;
174 
175 	if (STAILQ_NEXT(ve, next_ve) == NULL) {	/* last field, don't pad */
176 		asprintf(&str, "%s%s%s%s%s",
177 		    k->ki_d.prefix ? k->ki_d.prefix : "",
178 		    k->ki_p->ki_comm,
179 		    (showthreads && k->ki_p->ki_numthreads > 1) ? "/" : "",
180 		    (showthreads && k->ki_p->ki_numthreads > 1) ? k->ki_p->ki_tdname : "",
181 		    (showthreads && k->ki_p->ki_numthreads > 1) ? k->ki_p->ki_moretdname : "");
182 	} else {
183 		if (showthreads && k->ki_p->ki_numthreads > 1)
184 			asprintf(&str, "%s/%s%s", k->ki_p->ki_comm,
185 			    k->ki_p->ki_tdname, k->ki_p->ki_moretdname);
186 		else
187 			str = strdup(k->ki_p->ki_comm);
188 	}
189 	return (str);
190 }
191 
192 char *
193 tdnam(KINFO *k, VARENT *ve __unused)
194 {
195 	char *str;
196 
197 	if (showthreads && k->ki_p->ki_numthreads > 1)
198 		asprintf(&str, "%s%s", k->ki_p->ki_tdname,
199 		    k->ki_p->ki_moretdname);
200 	else
201 		str = strdup("      ");
202 
203 	return (str);
204 }
205 
206 char *
207 logname(KINFO *k, VARENT *ve __unused)
208 {
209 
210 	if (*k->ki_p->ki_login == '\0')
211 		return (NULL);
212 	return (strdup(k->ki_p->ki_login));
213 }
214 
215 char *
216 state(KINFO *k, VARENT *ve __unused)
217 {
218 	long flag, tdflags;
219 	char *cp, *buf;
220 
221 	buf = malloc(16);
222 	if (buf == NULL)
223 		xo_errx(1, "malloc failed");
224 
225 	flag = k->ki_p->ki_flag;
226 	tdflags = k->ki_p->ki_tdflags;	/* XXXKSE */
227 	cp = buf;
228 
229 	switch (k->ki_p->ki_stat) {
230 
231 	case SSTOP:
232 		*cp = 'T';
233 		break;
234 
235 	case SSLEEP:
236 		if (tdflags & TDF_SINTR)	/* interruptable (long) */
237 			*cp = k->ki_p->ki_slptime >= MAXSLP ? 'I' : 'S';
238 		else
239 			*cp = 'D';
240 		break;
241 
242 	case SRUN:
243 	case SIDL:
244 		*cp = 'R';
245 		break;
246 
247 	case SWAIT:
248 		*cp = 'W';
249 		break;
250 
251 	case SLOCK:
252 		*cp = 'L';
253 		break;
254 
255 	case SZOMB:
256 		*cp = 'Z';
257 		break;
258 
259 	default:
260 		*cp = '?';
261 	}
262 	cp++;
263 	if (!(flag & P_INMEM))
264 		*cp++ = 'W';
265 	if (k->ki_p->ki_nice < NZERO)
266 		*cp++ = '<';
267 	else if (k->ki_p->ki_nice > NZERO)
268 		*cp++ = 'N';
269 	if (flag & P_TRACED)
270 		*cp++ = 'X';
271 	if (flag & P_WEXIT && k->ki_p->ki_stat != SZOMB)
272 		*cp++ = 'E';
273 	if (flag & P_PPWAIT)
274 		*cp++ = 'V';
275 	if ((flag & P_SYSTEM) || k->ki_p->ki_lock > 0)
276 		*cp++ = 'L';
277 	if (k->ki_p->ki_kiflag & KI_SLEADER)
278 		*cp++ = 's';
279 	if ((flag & P_CONTROLT) && k->ki_p->ki_pgid == k->ki_p->ki_tpgid)
280 		*cp++ = '+';
281 	if (flag & P_JAILED)
282 		*cp++ = 'J';
283 	*cp = '\0';
284 	return (buf);
285 }
286 
287 #define	scalepri(x)	((x) - PZERO)
288 
289 char *
290 pri(KINFO *k, VARENT *ve __unused)
291 {
292 	char *str;
293 
294 	asprintf(&str, "%d", scalepri(k->ki_p->ki_pri.pri_level));
295 	return (str);
296 }
297 
298 char *
299 upr(KINFO *k, VARENT *ve __unused)
300 {
301 	char *str;
302 
303 	asprintf(&str, "%d", scalepri(k->ki_p->ki_pri.pri_user));
304 	return (str);
305 }
306 #undef scalepri
307 
308 char *
309 uname(KINFO *k, VARENT *ve __unused)
310 {
311 
312 	return (strdup(user_from_uid(k->ki_p->ki_uid, 0)));
313 }
314 
315 char *
316 egroupname(KINFO *k, VARENT *ve __unused)
317 {
318 
319 	return (strdup(group_from_gid(k->ki_p->ki_groups[0], 0)));
320 }
321 
322 char *
323 rgroupname(KINFO *k, VARENT *ve __unused)
324 {
325 
326 	return (strdup(group_from_gid(k->ki_p->ki_rgid, 0)));
327 }
328 
329 char *
330 runame(KINFO *k, VARENT *ve __unused)
331 {
332 
333 	return (strdup(user_from_uid(k->ki_p->ki_ruid, 0)));
334 }
335 
336 char *
337 tdev(KINFO *k, VARENT *ve __unused)
338 {
339 	dev_t dev;
340 	char *str;
341 
342 	dev = k->ki_p->ki_tdev;
343 	if (dev == NODEV)
344 		str = strdup("-");
345 	else
346 		asprintf(&str, "%#jx", (uintmax_t)dev);
347 
348 	return (str);
349 }
350 
351 char *
352 tname(KINFO *k, VARENT *ve __unused)
353 {
354 	dev_t dev;
355 	char *ttname, *str;
356 
357 	dev = k->ki_p->ki_tdev;
358 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
359 		str = strdup("- ");
360 	else {
361 		if (strncmp(ttname, "tty", 3) == 0 ||
362 		    strncmp(ttname, "cua", 3) == 0)
363 			ttname += 3;
364 		if (strncmp(ttname, "pts/", 4) == 0)
365 			ttname += 4;
366 		asprintf(&str, "%s%c", ttname,
367 		    k->ki_p->ki_kiflag & KI_CTTY ? ' ' : '-');
368 	}
369 
370 	return (str);
371 }
372 
373 char *
374 longtname(KINFO *k, VARENT *ve __unused)
375 {
376 	dev_t dev;
377 	const char *ttname;
378 
379 	dev = k->ki_p->ki_tdev;
380 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
381 		ttname = "-";
382 
383 	return (strdup(ttname));
384 }
385 
386 char *
387 started(KINFO *k, VARENT *ve __unused)
388 {
389 	time_t then;
390 	struct tm *tp;
391 	size_t buflen = 100;
392 	char *buf;
393 
394 	if (!k->ki_valid)
395 		return (NULL);
396 
397 	buf = malloc(buflen);
398 	if (buf == NULL)
399 		xo_errx(1, "malloc failed");
400 
401 	then = k->ki_p->ki_start.tv_sec;
402 	tp = localtime(&then);
403 	if (now - k->ki_p->ki_start.tv_sec < 24 * 3600) {
404 		(void)strftime(buf, buflen, "%H:%M  ", tp);
405 	} else if (now - k->ki_p->ki_start.tv_sec < 7 * 86400) {
406 		(void)strftime(buf, buflen, "%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 		xo_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 		xo_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 	return (strdup(k->ki_p->ki_emul));
801 }
802 
803 char *
804 label(KINFO *k, VARENT *ve __unused)
805 {
806 	char *string;
807 	mac_t proclabel;
808 	int error;
809 
810 	string = NULL;
811 	if (mac_prepare_process_label(&proclabel) == -1) {
812 		xo_warn("mac_prepare_process_label");
813 		goto out;
814 	}
815 	error = mac_get_pid(k->ki_p->ki_pid, proclabel);
816 	if (error == 0) {
817 		if (mac_to_text(proclabel, &string) == -1)
818 			string = NULL;
819 	}
820 	mac_free(proclabel);
821 out:
822 	return (string);
823 }
824 
825 char *
826 loginclass(KINFO *k, VARENT *ve __unused)
827 {
828 
829 	/*
830 	 * Don't display login class for system processes;
831 	 * login classes are used for resource limits,
832 	 * and limits don't apply to system processes.
833 	 */
834 	if (k->ki_p->ki_flag & P_SYSTEM) {
835 		return (strdup("-"));
836 	}
837 	return (strdup(k->ki_p->ki_loginclass));
838 }
839