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