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