xref: /freebsd/bin/ps/print.c (revision 953a3198a35204535cc9d450f04da982a4fea59b)
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  *	$Id: print.c,v 1.9 1995/09/04 01:22:41 peter Exp $
34  */
35 
36 #ifndef lint
37 static char sccsid[] = "@(#)print.c	8.6 (Berkeley) 4/16/94";
38 #endif /* not lint */
39 
40 #include <sys/param.h>
41 #include <sys/time.h>
42 #include <sys/resource.h>
43 #include <sys/proc.h>
44 #include <sys/stat.h>
45 
46 #ifdef P_PPWAIT
47 #define NEWVM
48 #endif
49 
50 #ifdef NEWVM
51 #include <sys/ucred.h>
52 #include <sys/sysctl.h>
53 #include <vm/vm.h>
54 #else
55 #include <machine/pte.h>
56 #include <sys/vmparam.h>
57 #include <sys/vm.h>
58 #endif
59 
60 #include <err.h>
61 #include <math.h>
62 #include <nlist.h>
63 #include <stddef.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <vis.h>
68 
69 #include "ps.h"
70 
71 void
72 printheader()
73 {
74 	VAR *v;
75 	struct varent *vent;
76 
77 	for (vent = vhead; vent; vent = vent->next) {
78 		v = vent->var;
79 		if (v->flag & LJUST) {
80 			if (vent->next == NULL)	/* last one */
81 				(void)printf("%s", v->header);
82 			else
83 				(void)printf("%-*s", v->width, v->header);
84 		} else
85 			(void)printf("%*s", v->width, v->header);
86 		if (vent->next != NULL)
87 			(void)putchar(' ');
88 	}
89 	(void)putchar('\n');
90 }
91 
92 void
93 command(k, ve)
94 	KINFO *k;
95 	VARENT *ve;
96 {
97 	VAR *v;
98 	int left;
99 	char *cp, *vis_env, *vis_args;
100 
101 	if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
102 		err(1, NULL);
103 	strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
104 	if (k->ki_env) {
105 		if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
106 			err(1, NULL);
107 		strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
108 	} else
109 		vis_env = NULL;
110 
111 	v = ve->var;
112 	if (ve->next == NULL) {
113 		/* last field */
114 		if (termwidth == UNLIMITED) {
115 			if (vis_env)
116 				(void)printf("%s ", vis_env);
117 			(void)printf("%s", vis_args);
118 		} else {
119 			left = termwidth - (totwidth - v->width);
120 			if (left < 1) /* already wrapped, just use std width */
121 				left = v->width;
122 			if ((cp = vis_env) != NULL) {
123 				while (--left >= 0 && *cp)
124 					(void)putchar(*cp++);
125 				if (--left >= 0)
126 					putchar(' ');
127 			}
128 			for (cp = vis_args; --left >= 0 && *cp != '\0';)
129 				(void)putchar(*cp++);
130 		}
131 	} else
132 		/* XXX env? */
133 		(void)printf("%-*.*s", v->width, v->width, vis_args);
134 	free(vis_args);
135 	if (vis_env != NULL)
136 		free(vis_env);
137 }
138 
139 void
140 ucomm(k, ve)
141 	KINFO *k;
142 	VARENT *ve;
143 {
144 	VAR *v;
145 
146 	v = ve->var;
147 	(void)printf("%-*s", v->width, KI_PROC(k)->p_comm);
148 }
149 
150 void
151 logname(k, ve)
152 	KINFO *k;
153 	VARENT *ve;
154 {
155 	VAR *v;
156 
157 	v = ve->var;
158 #ifndef NEWVM
159 	(void)printf("%-*s", v->width, KI_PROC(k)->p_logname);
160 #else
161 	(void)printf("%-*s", v->width, KI_EPROC(k)->e_login);
162 #endif
163 }
164 
165 void
166 state(k, ve)
167 	KINFO *k;
168 	VARENT *ve;
169 {
170 	struct proc *p;
171 	int flag;
172 	char *cp;
173 	VAR *v;
174 	char buf[16];
175 
176 	v = ve->var;
177 	p = KI_PROC(k);
178 	flag = p->p_flag;
179 	cp = buf;
180 
181 	switch (p->p_stat) {
182 
183 	case SSTOP:
184 		*cp = 'T';
185 		break;
186 
187 	case SSLEEP:
188 		if (flag & P_SINTR)	/* interuptable (long) */
189 			*cp = p->p_slptime >= MAXSLP ? 'I' : 'S';
190 		else
191 			*cp = 'D';
192 		break;
193 
194 	case SRUN:
195 	case SIDL:
196 		*cp = 'R';
197 		break;
198 
199 	case SZOMB:
200 		*cp = 'Z';
201 		break;
202 
203 	default:
204 		*cp = '?';
205 	}
206 	cp++;
207 	if (flag & P_INMEM) {
208 #ifndef NEWVM
209 		if (p->p_rssize > p->p_maxrss)
210 			*cp++ = '>';
211 #endif
212 	} else
213 		*cp++ = 'W';
214 	if (p->p_nice < NZERO)
215 		*cp++ = '<';
216 	else if (p->p_nice > NZERO)
217 		*cp++ = 'N';
218 #ifndef NEWVM
219 	if (flag & SUANOM)
220 		*cp++ = 'A';
221 	else if (flag & SSEQL)
222 		*cp++ = 'S';
223 #endif
224 	if (flag & P_TRACED)
225 		*cp++ = 'X';
226 	if (flag & P_WEXIT && p->p_stat != SZOMB)
227 		*cp++ = 'E';
228 #ifdef NEWVM
229 	if (flag & P_PPWAIT)
230 #else
231 	if (flag & SVFORK)
232 #endif
233 		*cp++ = 'V';
234 #ifdef NEWVM
235 	if (flag & (P_SYSTEM | P_NOSWAP | P_PHYSIO))
236 #else
237 	if (flag & (SSYS|SLOCK|SULOCK|SKEEP|SPHYSIO))
238 #endif
239 		*cp++ = 'L';
240 	if (KI_EPROC(k)->e_flag & EPROC_SLEADER)
241 		*cp++ = 's';
242 	if ((flag & P_CONTROLT) && KI_EPROC(k)->e_pgid == KI_EPROC(k)->e_tpgid)
243 		*cp++ = '+';
244 	*cp = '\0';
245 	(void)printf("%-*s", v->width, buf);
246 }
247 
248 void
249 pri(k, ve)
250 	KINFO *k;
251 	VARENT *ve;
252 {
253 	VAR *v;
254 
255 	v = ve->var;
256 	(void)printf("%*d", v->width, KI_PROC(k)->p_priority - PZERO);
257 }
258 
259 void
260 uname(k, ve)
261 	KINFO *k;
262 	VARENT *ve;
263 {
264 	VAR *v;
265 
266 	v = ve->var;
267 #ifndef NEWVM
268 	(void)printf("%-*s",
269 	    (int)v->width, user_from_uid(KI_PROC(k)->p_uid, 0));
270 #else
271 	(void)printf("%-*s",
272 	    (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0));
273 #endif
274 }
275 
276 void
277 runame(k, ve)
278 	KINFO *k;
279 	VARENT *ve;
280 {
281 	VAR *v;
282 
283 	v = ve->var;
284 #ifndef NEWVM
285 	(void)printf("%-*s",
286 	    (int)v->width, user_from_uid(KI_PROC(k)->p_ruid, 0));
287 #else
288 	(void)printf("%-*s",
289 	    (int)v->width, user_from_uid(KI_EPROC(k)->e_pcred.p_ruid, 0));
290 #endif
291 }
292 
293 void
294 tdev(k, ve)
295 	KINFO *k;
296 	VARENT *ve;
297 {
298 	VAR *v;
299 	dev_t dev;
300 	char buff[16];
301 
302 	v = ve->var;
303 	dev = KI_EPROC(k)->e_tdev;
304 	if (dev == NODEV)
305 		(void)printf("%*s", v->width, "??");
306 	else {
307 		(void)snprintf(buff, sizeof(buff),
308 		    "%d/%d", major(dev), minor(dev));
309 		(void)printf("%*s", v->width, buff);
310 	}
311 }
312 
313 void
314 tname(k, ve)
315 	KINFO *k;
316 	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-1, "??");
326 	else {
327 		if (strncmp(ttname, "tty", 3) == 0 ||
328 		    strncmp(ttname, "cua", 3) == 0)
329 			ttname += 3;
330 		(void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
331 			KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-');
332 	}
333 }
334 
335 void
336 longtname(k, ve)
337 	KINFO *k;
338 	VARENT *ve;
339 {
340 	VAR *v;
341 	dev_t dev;
342 	char *ttname;
343 
344 	v = ve->var;
345 	dev = KI_EPROC(k)->e_tdev;
346 	if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
347 		(void)printf("%-*s", v->width, "??");
348 	else
349 		(void)printf("%-*s", v->width, ttname);
350 }
351 
352 void
353 started(k, ve)
354 	KINFO *k;
355 	VARENT *ve;
356 {
357 	VAR *v;
358 	static time_t now;
359 	struct tm *tp;
360 	char buf[100];
361 
362 	v = ve->var;
363 	if (!k->ki_u.u_valid) {
364 		(void)printf("%-*s", v->width, "-");
365 		return;
366 	}
367 
368 	tp = localtime(&k->ki_u.u_start.tv_sec);
369 	if (!now)
370 		(void)time(&now);
371 	if (now - k->ki_u.u_start.tv_sec < 24 * 3600) {
372 		/* I *hate* SCCS... */
373 		static char fmt[] = __CONCAT("%l:%", "M%p");
374 		(void)strftime(buf, sizeof(buf) - 1, fmt, tp);
375 	} else if (now - k->ki_u.u_start.tv_sec < 7 * 86400) {
376 		/* I *hate* SCCS... */
377 		static char fmt[] = __CONCAT("%a%", "I%p");
378 		(void)strftime(buf, sizeof(buf) - 1, fmt, tp);
379 	} else
380 		(void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
381 	(void)printf("%-*s", v->width, buf);
382 }
383 
384 void
385 lstarted(k, ve)
386 	KINFO *k;
387 	VARENT *ve;
388 {
389 	VAR *v;
390 	char buf[100];
391 
392 	v = ve->var;
393 	if (!k->ki_u.u_valid) {
394 		(void)printf("%-*s", v->width, "-");
395 		return;
396 	}
397 	(void)strftime(buf, sizeof(buf) -1, "%C",
398 	    localtime(&k->ki_u.u_start.tv_sec));
399 	(void)printf("%-*s", v->width, buf);
400 }
401 
402 void
403 wchan(k, ve)
404 	KINFO *k;
405 	VARENT *ve;
406 {
407 	VAR *v;
408 
409 	v = ve->var;
410 	if (KI_PROC(k)->p_wchan) {
411 		if (KI_PROC(k)->p_wmesg)
412 			(void)printf("%-*.*s", v->width, v->width,
413 				      KI_EPROC(k)->e_wmesg);
414 		else
415 			(void)printf("%-*x", v->width,
416 			    (int)KI_PROC(k)->p_wchan &~ KERNBASE);
417 	} else
418 		(void)printf("%-*s", v->width, "-");
419 }
420 
421 #define pgtok(a)        (((a)*NBPG)/1024)
422 
423 void
424 vsize(k, ve)
425 	KINFO *k;
426 	VARENT *ve;
427 {
428 	VAR *v;
429 
430 	v = ve->var;
431 	(void)printf("%*d", v->width,
432 #ifndef NEWVM
433 	    pgtok(KI_PROC(k)->p_dsize +
434 	        KI_PROC(k)->p_ssize + KI_EPROC(k)->e_xsize));
435 #else
436 	    pgtok(KI_EPROC(k)->e_vm.vm_dsize + KI_EPROC(k)->e_vm.vm_ssize +
437 		KI_EPROC(k)->e_vm.vm_tsize));
438 #endif
439 }
440 
441 void
442 rssize(k, ve)
443 	KINFO *k;
444 	VARENT *ve;
445 {
446 	VAR *v;
447 
448 	v = ve->var;
449 #ifndef NEWVM
450 	(void)printf("%*d", v->width,
451 	    pgtok(KI_PROC(k)->p_rssize + (KI_EPROC(k)->e_xccount ?
452 	    (KI_EPROC(k)->e_xrssize / KI_EPROC(k)->e_xccount) : 0)));
453 #else
454 	/* XXX don't have info about shared */
455 	(void)printf("%*d", v->width, pgtok(KI_EPROC(k)->e_vm.vm_rssize));
456 #endif
457 }
458 
459 void
460 p_rssize(k, ve)		/* doesn't account for text */
461 	KINFO *k;
462 	VARENT *ve;
463 {
464 	VAR *v;
465 
466 	v = ve->var;
467 #ifndef NEWVM
468 	(void)printf("%*d", v->width, pgtok(KI_PROC(k)->p_rssize));
469 #else
470 	(void)printf("%*ld", v->width, pgtok(KI_EPROC(k)->e_vm.vm_rssize));
471 #endif
472 }
473 
474 void
475 cputime(k, ve)
476 	KINFO *k;
477 	VARENT *ve;
478 {
479 	VAR *v;
480 	long secs;
481 	long psecs;	/* "parts" of a second. first micro, then centi */
482 	char obuff[128];
483 
484 	v = ve->var;
485 	if (KI_PROC(k)->p_stat == SZOMB || !k->ki_u.u_valid) {
486 		secs = 0;
487 		psecs = 0;
488 	} else {
489 		/*
490 		 * This counts time spent handling interrupts.  We could
491 		 * fix this, but it is not 100% trivial (and interrupt
492 		 * time fractions only work on the sparc anyway).	XXX
493 		 */
494 		secs = KI_PROC(k)->p_rtime.tv_sec;
495 		psecs = KI_PROC(k)->p_rtime.tv_usec;
496 		if (sumrusage) {
497 			secs += k->ki_u.u_cru.ru_utime.tv_sec +
498 				k->ki_u.u_cru.ru_stime.tv_sec;
499 			psecs += k->ki_u.u_cru.ru_utime.tv_usec +
500 				k->ki_u.u_cru.ru_stime.tv_usec;
501 		}
502 		/*
503 		 * round and scale to 100's
504 		 */
505 		psecs = (psecs + 5000) / 10000;
506 		secs += psecs / 100;
507 		psecs = psecs % 100;
508 	}
509 	(void)snprintf(obuff, sizeof(obuff),
510 	    "%3ld:%02ld.%02ld", secs/60, secs%60, psecs);
511 	(void)printf("%*s", v->width, obuff);
512 }
513 
514 double
515 getpcpu(k)
516 	KINFO *k;
517 {
518 	struct proc *p;
519 	static int failure;
520 
521 	if (!nlistread)
522 		failure = donlist();
523 	if (failure)
524 		return (0.0);
525 
526 	p = KI_PROC(k);
527 #define	fxtofl(fixpt)	((double)(fixpt) / fscale)
528 
529 	/* XXX - I don't like this */
530 	if (p->p_swtime == 0 || (p->p_flag & P_INMEM) == 0)
531 		return (0.0);
532 	if (rawcpu)
533 		return (100.0 * fxtofl(p->p_pctcpu));
534 	return (100.0 * fxtofl(p->p_pctcpu) /
535 		(1.0 - exp(p->p_swtime * log(fxtofl(ccpu)))));
536 }
537 
538 void
539 pcpu(k, ve)
540 	KINFO *k;
541 	VARENT *ve;
542 {
543 	VAR *v;
544 
545 	v = ve->var;
546 	(void)printf("%*.1f", v->width, getpcpu(k));
547 }
548 
549 double
550 getpmem(k)
551 	KINFO *k;
552 {
553 	static int failure;
554 	struct proc *p;
555 	struct eproc *e;
556 	double fracmem;
557 	int szptudot;
558 
559 	if (!nlistread)
560 		failure = donlist();
561 	if (failure)
562 		return (0.0);
563 
564 	p = KI_PROC(k);
565 	e = KI_EPROC(k);
566 	if ((p->p_flag & P_INMEM) == 0)
567 		return (0.0);
568 #ifndef NEWVM
569 	szptudot = UPAGES + clrnd(ctopt(p->p_dsize + p->p_ssize + e->e_xsize));
570 	fracmem = ((float)p->p_rssize + szptudot)/CLSIZE/mempages;
571 	if (p->p_textp && e->e_xccount)
572 		fracmem += ((float)e->e_xrssize)/CLSIZE/e->e_xccount/mempages;
573 #else
574 	/* XXX want pmap ptpages, segtab, etc. (per architecture) */
575 	szptudot = UPAGES;
576 	/* XXX don't have info about shared */
577 	fracmem = ((float)e->e_vm.vm_rssize + szptudot)/CLSIZE/mempages;
578 #endif
579 	return (100.0 * fracmem);
580 }
581 
582 void
583 pmem(k, ve)
584 	KINFO *k;
585 	VARENT *ve;
586 {
587 	VAR *v;
588 
589 	v = ve->var;
590 	(void)printf("%*.1f", v->width, getpmem(k));
591 }
592 
593 void
594 pagein(k, ve)
595 	KINFO *k;
596 	VARENT *ve;
597 {
598 	VAR *v;
599 
600 	v = ve->var;
601 	(void)printf("%*ld", v->width,
602 	    k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0);
603 }
604 
605 void
606 maxrss(k, ve)
607 	KINFO *k;
608 	VARENT *ve;
609 {
610 	VAR *v;
611 
612 	v = ve->var;
613 #ifndef NEWVM	/* not yet */
614 	if (KI_PROC(k)->p_maxrss != (RLIM_INFINITY/NBPG))
615 		(void)printf("%*d", v->width, pgtok(KI_PROC(k)->p_maxrss));
616 	else
617 #endif
618 		(void)printf("%*s", v->width, "-");
619 }
620 
621 void
622 tsize(k, ve)
623 	KINFO *k;
624 	VARENT *ve;
625 {
626 	VAR *v;
627 
628 	v = ve->var;
629 #ifndef NEWVM
630 	(void)printf("%*d", v->width, pgtok(KI_EPROC(k)->e_xsize));
631 #else
632 	(void)printf("%*ld", v->width, pgtok(KI_EPROC(k)->e_vm.vm_tsize));
633 #endif
634 }
635 
636 #ifndef NEWVM
637 void
638 trss(k, ve)
639 	KINFO *k;
640 	VARENT *ve;
641 {
642 	VAR *v;
643 
644 	v = ve->var;
645 	(void)printf("%*d", v->width, pgtok(KI_EPROC(k)->e_xrssize));
646 }
647 #endif
648 
649 /*
650  * Generic output routines.  Print fields from various prototype
651  * structures.
652  */
653 static void
654 printval(bp, v)
655 	char *bp;
656 	VAR *v;
657 {
658 	static char ofmt[32] = "%";
659 	char *fcp, *cp;
660 
661 	cp = ofmt + 1;
662 	fcp = v->fmt;
663 	if (v->flag & LJUST)
664 		*cp++ = '-';
665 	*cp++ = '*';
666 	while ((*cp++ = *fcp++));
667 
668 	switch (v->type) {
669 	case CHAR:
670 		(void)printf(ofmt, v->width, *(char *)bp);
671 		break;
672 	case UCHAR:
673 		(void)printf(ofmt, v->width, *(u_char *)bp);
674 		break;
675 	case SHORT:
676 		(void)printf(ofmt, v->width, *(short *)bp);
677 		break;
678 	case USHORT:
679 		(void)printf(ofmt, v->width, *(u_short *)bp);
680 		break;
681 	case LONG:
682 		(void)printf(ofmt, v->width, *(long *)bp);
683 		break;
684 	case ULONG:
685 		(void)printf(ofmt, v->width, *(u_long *)bp);
686 		break;
687 	case KPTR:
688 		(void)printf(ofmt, v->width, *(u_long *)bp &~ KERNBASE);
689 		break;
690 	default:
691 		errx(1, "unknown type %d", v->type);
692 	}
693 }
694 
695 void
696 pvar(k, ve)
697 	KINFO *k;
698 	VARENT *ve;
699 {
700 	VAR *v;
701 
702 	v = ve->var;
703 	printval((char *)((char *)KI_PROC(k) + v->off), v);
704 }
705 
706 void
707 evar(k, ve)
708 	KINFO *k;
709 	VARENT *ve;
710 {
711 	VAR *v;
712 
713 	v = ve->var;
714 	printval((char *)((char *)KI_EPROC(k) + v->off), v);
715 }
716 
717 void
718 uvar(k, ve)
719 	KINFO *k;
720 	VARENT *ve;
721 {
722 	VAR *v;
723 
724 	v = ve->var;
725 	if (k->ki_u.u_valid)
726 		printval((char *)((char *)&k->ki_u + v->off), v);
727 	else
728 		(void)printf("%*s", v->width, "-");
729 }
730 
731 void
732 rvar(k, ve)
733 	KINFO *k;
734 	VARENT *ve;
735 {
736 	VAR *v;
737 
738 	v = ve->var;
739 	if (k->ki_u.u_valid)
740 		printval((char *)((char *)(&k->ki_u.u_ru) + v->off), v);
741 	else
742 		(void)printf("%*s", v->width, "-");
743 }
744