xref: /dragonfly/bin/ps/print.c (revision 4e7eb5cc)
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)print.c	8.6 (Berkeley) 4/16/94
34  * $FreeBSD: src/bin/ps/print.c,v 1.36.2.4 2002/11/30 13:00:14 tjr Exp $
35  * $DragonFly: src/bin/ps/print.c,v 1.9 2003/11/21 22:46:10 dillon Exp $
36  */
37 
38 #include <sys/param.h>
39 #include <sys/time.h>
40 #include <sys/resource.h>
41 #include <sys/stat.h>
42 
43 #include <sys/ucred.h>
44 #include <sys/user.h>
45 #include <sys/sysctl.h>
46 #include <sys/rtprio.h>
47 #include <vm/vm.h>
48 
49 #include <err.h>
50 #include <langinfo.h>
51 #include <locale.h>
52 #include <math.h>
53 #include <nlist.h>
54 #include <stddef.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <unistd.h>
58 #include <string.h>
59 #include <vis.h>
60 
61 #include "ps.h"
62 
63 void
64 printheader(void)
65 {
66 	VAR *v;
67 	struct varent *vent;
68 	int allempty;
69 
70 	allempty = 1;
71 	for (vent = vhead; vent; vent = vent->next)
72 		if (*vent->var->header != '\0') {
73 			allempty = 0;
74 			break;
75 		}
76 	if (allempty)
77 		return;
78 	for (vent = vhead; vent; vent = vent->next) {
79 		v = vent->var;
80 		if (v->flag & LJUST) {
81 			if (vent->next == NULL)	/* last one */
82 				(void)printf("%s", v->header);
83 			else
84 				(void)printf("%-*s", v->width, v->header);
85 		} else
86 			(void)printf("%*s", v->width, v->header);
87 		if (vent->next != NULL)
88 			(void)putchar(' ');
89 	}
90 	(void)putchar('\n');
91 }
92 
93 void
94 command(KINFO *k, VARENT *ve)
95 {
96 	VAR *v;
97 	int left;
98 	char *cp, *vis_env, *vis_args;
99 
100 	v = ve->var;
101 
102 	if (cflag) {
103 		if (ve->next == NULL)	/* last field, don't pad */
104 			(void)printf("%s", KI_THREAD(k)->td_comm);
105 		else
106 			(void)printf("%-*s", v->width, KI_THREAD(k)->td_comm);
107 		return;
108 	}
109 
110 	if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
111 		err(1, NULL);
112 	strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
113 	if (k->ki_env) {
114 		if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
115 			err(1, NULL);
116 		strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
117 	} else
118 		vis_env = NULL;
119 
120 	if (ve->next == NULL) {
121 		/* last field */
122 		if (termwidth == UNLIMITED) {
123 			if (vis_env)
124 				(void)printf("%s ", vis_env);
125 			(void)printf("%s", vis_args);
126 		} else {
127 			left = termwidth - (totwidth - v->width);
128 			if (left < 1) /* already wrapped, just use std width */
129 				left = v->width;
130 			if ((cp = vis_env) != NULL) {
131 				while (--left >= 0 && *cp)
132 					(void)putchar(*cp++);
133 				if (--left >= 0)
134 					putchar(' ');
135 			}
136 			for (cp = vis_args; --left >= 0 && *cp != '\0';)
137 				(void)putchar(*cp++);
138 		}
139 	} else
140 		/* XXX env? */
141 		(void)printf("%-*.*s", v->width, v->width, vis_args);
142 	free(vis_args);
143 	if (vis_env != NULL)
144 		free(vis_env);
145 }
146 
147 void
148 ucomm(KINFO *k, VARENT *ve)
149 {
150 	VAR *v;
151 
152 	v = ve->var;
153 	(void)printf("%-*s", v->width, KI_THREAD(k)->td_comm);
154 }
155 
156 void
157 logname(KINFO *k, VARENT *ve)
158 {
159 	VAR *v;
160 	char *s;
161 
162 	v = ve->var;
163 	(void)printf("%-*s", v->width, (s = KI_EPROC(k)->e_login, *s) ? s : "-");
164 }
165 
166 void
167 state(KINFO *k, VARENT *ve)
168 {
169 	struct proc *p;
170 	int flag;
171 	char *cp;
172 	VAR *v;
173 	char buf[16];
174 
175 	v = ve->var;
176 	p = KI_PROC(k);
177 	flag = p->p_flag;
178 	cp = buf;
179 
180 	switch (p->p_stat) {
181 
182 	case SSTOP:
183 		*cp = 'T';
184 		break;
185 
186 	case SSLEEP:
187 		if (flag & P_SINTR)	/* interruptable (long) */
188 			*cp = p->p_slptime >= MAXSLP ? 'I' : 'S';
189 		else
190 			*cp = 'D';
191 		break;
192 
193 	case SRUN:
194 	case SIDL:
195 		*cp = 'R';
196 		if (KI_THREAD(k)->td_flags & TDF_RUNNING) {
197 		    ++cp;
198 		    sprintf(cp, "%d", KI_EPROC(k)->e_cpuid);
199 		    while (cp[1])
200 			++cp;
201 		}
202 		break;
203 
204 	case SZOMB:
205 		*cp = 'Z';
206 		break;
207 
208 	default:
209 		*cp = '?';
210 	}
211 	cp++;
212 	if (!(flag & P_INMEM))
213 		*cp++ = 'W';
214 	if (p->p_nice < NZERO)
215 		*cp++ = '<';
216 	else if (p->p_nice > NZERO)
217 		*cp++ = 'N';
218 	if (flag & P_TRACED)
219 		*cp++ = 'X';
220 	if (flag & P_WEXIT && p->p_stat != SZOMB)
221 		*cp++ = 'E';
222 	if (flag & P_PPWAIT)
223 		*cp++ = 'V';
224 	if ((flag & P_SYSTEM) || p->p_lock > 0)
225 		*cp++ = 'L';
226 	if (KI_EPROC(k)->e_flag & EPROC_SLEADER)
227 		*cp++ = 's';
228 	if ((flag & P_CONTROLT) && KI_EPROC(k)->e_pgid == KI_EPROC(k)->e_tpgid)
229 		*cp++ = '+';
230 	if (flag & P_JAILED)
231 		*cp++ = 'J';
232 	*cp = '\0';
233 	(void)printf("%-*s", v->width, buf);
234 }
235 
236 void
237 pri(KINFO *k, VARENT *ve)
238 {
239 	VAR *v;
240 
241 	v = ve->var;
242 	(void)printf("%*d", v->width, KI_PROC(k)->p_priority);
243 }
244 
245 void
246 uname(KINFO *k, VARENT *ve)
247 {
248 	VAR *v;
249 
250 	v = ve->var;
251 	(void)printf("%-*s",
252 	    (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0));
253 }
254 
255 int
256 s_uname(KINFO *k)
257 {
258 	    return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0)));
259 }
260 
261 void
262 runame(KINFO *k, VARENT *ve)
263 {
264 	VAR *v;
265 
266 	v = ve->var;
267 	(void)printf("%-*s",
268 	    (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0));
269 }
270 
271 int
272 s_runame(KINFO *k)
273 {
274 	    return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0)));
275 }
276 
277 void
278 tdev(KINFO *k, VARENT *ve)
279 {
280 	VAR *v;
281 	dev_t dev;
282 	char buff[16];
283 
284 	v = ve->var;
285 	dev = KI_EPROC(k)->e_tdev;
286 	if (dev == NODEV)
287 		(void)printf("%*s", v->width, "??");
288 	else {
289 		(void)snprintf(buff, sizeof(buff),
290 		    "%d/%d", major(dev), minor(dev));
291 		(void)printf("%*s", v->width, buff);
292 	}
293 }
294 
295 void
296 tname(KINFO *k, VARENT *ve)
297 {
298 	VAR *v;
299 	dev_t dev;
300 	char *ttname;
301 
302 	v = ve->var;
303 	dev = KI_EPROC(k)->e_tdev;
304 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
305 		(void)printf("%*s ", v->width-1, "??");
306 	else {
307 		if (strncmp(ttname, "tty", 3) == 0 ||
308 		    strncmp(ttname, "cua", 3) == 0)
309 			ttname += 3;
310 		(void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
311 			KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-');
312 	}
313 }
314 
315 void
316 longtname(KINFO *k, VARENT *ve)
317 {
318 	VAR *v;
319 	dev_t dev;
320 	char *ttname;
321 
322 	v = ve->var;
323 	dev = KI_EPROC(k)->e_tdev;
324 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
325 		(void)printf("%-*s", v->width, "??");
326 	else
327 		(void)printf("%-*s", v->width, ttname);
328 }
329 
330 void
331 started(KINFO *k, VARENT *ve)
332 {
333 	VAR *v;
334 	static time_t now;
335 	time_t then;
336 	struct tm *tp;
337 	char buf[100];
338 	static int  use_ampm = -1;
339 
340 	v = ve->var;
341 	if (!k->ki_u.u_valid) {
342 		(void)printf("%-*s", v->width, "-");
343 		return;
344 	}
345 
346 	if (use_ampm < 0)
347 		use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
348 
349 	then = k->ki_u.u_start.tv_sec;
350 	tp = localtime(&then);
351 	if (!now)
352 		(void)time(&now);
353 	if (now - k->ki_u.u_start.tv_sec < 24 * 3600) {
354 		(void)strftime(buf, sizeof(buf) - 1,
355 		use_ampm ? "%l:%M%p" : "%k:%M  ", tp);
356 	} else if (now - k->ki_u.u_start.tv_sec < 7 * 86400) {
357 		(void)strftime(buf, sizeof(buf) - 1,
358 		use_ampm ? "%a%I%p" : "%a%H  ", tp);
359 	} else
360 		(void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
361 	(void)printf("%-*s", v->width, buf);
362 }
363 
364 void
365 lstarted(KINFO *k, VARENT *ve)
366 {
367 	VAR *v;
368 	time_t then;
369 	char buf[100];
370 
371 	v = ve->var;
372 	if (!k->ki_u.u_valid) {
373 		(void)printf("%-*s", v->width, "-");
374 		return;
375 	}
376 	then = k->ki_u.u_start.tv_sec;
377 	(void)strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
378 	(void)printf("%-*s", v->width, buf);
379 }
380 
381 void
382 wchan(KINFO *k, VARENT *ve)
383 {
384 	VAR *v;
385 
386 	v = ve->var;
387 	if (KI_THREAD(k)->td_wchan) {
388 		if (KI_THREAD(k)->td_wmesg)
389 			(void)printf("%-*.*s", v->width, v->width,
390 				      KI_EPROC(k)->e_wmesg);
391 		else
392 			(void)printf("%-*lx", v->width,
393 			    (long)KI_THREAD(k)->td_wchan);
394 	} else
395 		(void)printf("%-*s", v->width, "-");
396 }
397 
398 #ifndef pgtok
399 #define pgtok(a)        (((a)*getpagesize())/1024)
400 #endif
401 
402 void
403 vsize(KINFO *k, VARENT *ve)
404 {
405 	VAR *v;
406 
407 	v = ve->var;
408 	(void)printf("%*d", v->width,
409 	    (KI_EPROC(k)->e_vm.vm_map.size/1024));
410 }
411 
412 void
413 rssize(KINFO *k, VARENT *ve)
414 {
415 	VAR *v;
416 
417 	v = ve->var;
418 	/* XXX don't have info about shared */
419 	(void)printf("%*lu", v->width,
420 	    (u_long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
421 }
422 
423 void
424 p_rssize(KINFO *k, VARENT *ve)		/* doesn't account for text */
425 {
426 	VAR *v;
427 
428 	v = ve->var;
429 	(void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
430 }
431 
432 void
433 cputime(KINFO *k, VARENT *ve)
434 {
435 	VAR *v;
436 	long secs;
437 	long psecs;	/* "parts" of a second. first micro, then centi */
438 	char obuff[128];
439 	static char decimal_point = 0;
440 
441 	if (!decimal_point)
442 		decimal_point = localeconv()->decimal_point[0];
443 	v = ve->var;
444 	if (KI_PROC(k)->p_stat == SZOMB || !k->ki_u.u_valid) {
445 		secs = 0;
446 		psecs = 0;
447 	} else {
448 		u_int64_t timeus;
449 
450 		/*
451 		 * This counts time spent handling interrupts.  We could
452 		 * fix this, but it is not 100% trivial (and interrupt
453 		 * time fractions only work on the sparc anyway).	XXX
454 		 */
455 		timeus = KI_EPROC(k)->e_uticks + KI_EPROC(k)->e_sticks;
456 		secs = timeus / 1000000;
457 		psecs = timeus % 1000000;
458 		if (sumrusage) {
459 			secs += k->ki_u.u_cru.ru_utime.tv_sec +
460 				k->ki_u.u_cru.ru_stime.tv_sec;
461 			psecs += k->ki_u.u_cru.ru_utime.tv_usec +
462 				k->ki_u.u_cru.ru_stime.tv_usec;
463 		}
464 		/*
465 		 * round and scale to 100's
466 		 */
467 		psecs = (psecs + 5000) / 10000;
468 		secs += psecs / 100;
469 		psecs = psecs % 100;
470 	}
471 	(void)snprintf(obuff, sizeof(obuff),
472 	    "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs);
473 	(void)printf("%*s", v->width, obuff);
474 }
475 
476 double
477 getpcpu(KINFO *k)
478 {
479 	struct proc *p;
480 	static int failure;
481 
482 	if (!nlistread)
483 		failure = donlist();
484 	if (failure)
485 		return (0.0);
486 
487 	p = KI_PROC(k);
488 #define	fxtofl(fixpt)	((double)(fixpt) / fscale)
489 
490 	/* XXX - I don't like this */
491 	if (p->p_swtime == 0 || (p->p_flag & P_INMEM) == 0)
492 		return (0.0);
493 	if (rawcpu)
494 		return (100.0 * fxtofl(p->p_pctcpu));
495 	return (100.0 * fxtofl(p->p_pctcpu) /
496 		(1.0 - exp(p->p_swtime * log(fxtofl(ccpu)))));
497 }
498 
499 void
500 pcpu(KINFO *k, VARENT *ve)
501 {
502 	VAR *v;
503 
504 	v = ve->var;
505 	(void)printf("%*.1f", v->width, getpcpu(k));
506 }
507 
508 void
509 pnice(KINFO *k, VARENT *ve)
510 {
511 	VAR *v;
512 	int nice;
513 
514 	switch (KI_PROC(k)->p_rtprio.type) {
515 	case RTP_PRIO_REALTIME:
516 		nice = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_PROC(k)->p_rtprio.prio;
517 		break;
518 	case RTP_PRIO_IDLE:
519 		nice = PRIO_MAX + 1 + KI_PROC(k)->p_rtprio.prio;
520 		break;
521 	case RTP_PRIO_THREAD:
522 		nice = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_PROC(k)->p_rtprio.prio;
523 		break;
524 	default:
525 		nice = KI_PROC(k)->p_nice - NZERO;
526 		break;
527 	}
528 	v = ve->var;
529 	(void)printf("%*d", v->width, nice);
530 }
531 
532 
533 double
534 getpmem(KINFO *k)
535 {
536 	static int failure;
537 	struct proc *p;
538 	struct eproc *e;
539 	double fracmem;
540 	int szptudot;
541 
542 	if (!nlistread)
543 		failure = donlist();
544 	if (failure)
545 		return (0.0);
546 
547 	p = KI_PROC(k);
548 	e = KI_EPROC(k);
549 	if ((p->p_flag & P_INMEM) == 0)
550 		return (0.0);
551 	/* XXX want pmap ptpages, segtab, etc. (per architecture) */
552 	szptudot = UPAGES;
553 	/* XXX don't have info about shared */
554 	fracmem = ((float)e->e_vm.vm_rssize + szptudot)/mempages;
555 	return (100.0 * fracmem);
556 }
557 
558 void
559 pmem(KINFO *k, VARENT *ve)
560 {
561 	VAR *v;
562 
563 	v = ve->var;
564 	(void)printf("%*.1f", v->width, getpmem(k));
565 }
566 
567 void
568 pagein(KINFO *k, VARENT *ve)
569 {
570 	VAR *v;
571 
572 	v = ve->var;
573 	(void)printf("%*ld", v->width,
574 	    k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0);
575 }
576 
577 void
578 maxrss(KINFO *k, VARENT *ve)
579 {
580 	VAR *v;
581 
582 	v = ve->var;
583 	/* XXX not yet */
584 	(void)printf("%*s", v->width, "-");
585 }
586 
587 void
588 tsize(KINFO *k, VARENT *ve)
589 {
590 	VAR *v;
591 
592 	v = ve->var;
593 	(void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_tsize));
594 }
595 
596 void
597 rtprior(KINFO *k, VARENT *ve)
598 {
599 	VAR *v;
600 	struct rtprio *prtp;
601 	char str[8];
602 	unsigned prio, type;
603 
604 	v = ve->var;
605 	prtp = (struct rtprio *) ((char *)KI_PROC(k) + v->off);
606 	prio = prtp->prio;
607 	type = prtp->type;
608 	switch (type) {
609 	case RTP_PRIO_REALTIME:
610 		snprintf(str, sizeof(str), "real:%u", prio);
611 		break;
612 	case RTP_PRIO_NORMAL:
613 		strncpy(str, "normal", sizeof(str));
614 		break;
615 	case RTP_PRIO_IDLE:
616 		snprintf(str, sizeof(str), "idle:%u", prio);
617 		break;
618 	default:
619 		snprintf(str, sizeof(str), "%u:%u", type, prio);
620 		break;
621 	}
622 	str[sizeof(str) - 1] = '\0';
623 	(void)printf("%*s", v->width, str);
624 }
625 
626 /*
627  * Generic output routines.  Print fields from various prototype
628  * structures.
629  */
630 static void
631 printval(char *bp, VAR *v)
632 {
633 	static char ofmt[32] = "%";
634 	char *fcp, *cp;
635 
636 	cp = ofmt + 1;
637 	fcp = v->fmt;
638 	if (v->flag & LJUST)
639 		*cp++ = '-';
640 	*cp++ = '*';
641 	while ((*cp++ = *fcp++));
642 
643 	switch (v->type) {
644 	case CHAR:
645 		(void)printf(ofmt, v->width, *(char *)bp);
646 		break;
647 	case UCHAR:
648 		(void)printf(ofmt, v->width, *(u_char *)bp);
649 		break;
650 	case SHORT:
651 		(void)printf(ofmt, v->width, *(short *)bp);
652 		break;
653 	case USHORT:
654 		(void)printf(ofmt, v->width, *(u_short *)bp);
655 		break;
656 	case INT:
657 		(void)printf(ofmt, v->width, *(int *)bp);
658 		break;
659 	case UINT:
660 		(void)printf(ofmt, v->width, *(u_int *)bp);
661 		break;
662 	case LONG:
663 		(void)printf(ofmt, v->width, *(long *)bp);
664 		break;
665 	case ULONG:
666 		(void)printf(ofmt, v->width, *(u_long *)bp);
667 		break;
668 	case KPTR:
669 		(void)printf(ofmt, v->width, *(u_long *)bp);
670 		break;
671 	default:
672 		errx(1, "unknown type %d", v->type);
673 	}
674 }
675 
676 void
677 pvar(KINFO *k, VARENT *ve)
678 {
679 	VAR *v;
680 
681 	v = ve->var;
682 	printval((char *)((char *)KI_PROC(k) + v->off), v);
683 }
684 
685 void
686 tvar(KINFO *k, VARENT *ve)
687 {
688 	VAR *v;
689 
690 	v = ve->var;
691 	printval((char *)((char *)KI_THREAD(k) + v->off), v);
692 }
693 
694 void
695 evar(KINFO *k, VARENT *ve)
696 {
697 	VAR *v;
698 
699 	v = ve->var;
700 	printval((char *)((char *)KI_EPROC(k) + v->off), v);
701 }
702 
703 void
704 uvar(KINFO *k, VARENT *ve)
705 {
706 	VAR *v;
707 
708 	v = ve->var;
709 	if (k->ki_u.u_valid)
710 		printval((char *)((char *)&k->ki_u + v->off), v);
711 	else
712 		(void)printf("%*s", v->width, "-");
713 }
714 
715 void
716 rvar(KINFO *k, VARENT *ve)
717 {
718 	VAR *v;
719 
720 	v = ve->var;
721 	if (k->ki_u.u_valid)
722 		printval((char *)((char *)(&k->ki_u.u_ru) + v->off), v);
723 	else
724 		(void)printf("%*s", v->width, "-");
725 }
726