2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
36 static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94";
38 static const char rcsid[] =
39 "$FreeBSD: src/bin/ps/print.c,v 1.36.2.4 2002/11/30 13:00:14 tjr Exp $";
42 #include <sys/param.h>
44 #include <sys/resource.h>
48 #include <sys/ucred.h>
50 #include <sys/sysctl.h>
75 for (vent = vhead; vent; vent = vent->next)
76 if (*vent->var->header != '\0') {
82 for (vent = vhead; vent; vent = vent->next) {
84 if (v->flag & LJUST) {
85 if (vent->next == NULL) /* last one */
86 (void)printf("%s", v->header);
88 (void)printf("%-*s", v->width, v->header);
90 (void)printf("%*s", v->width, v->header);
91 if (vent->next != NULL)
104 char *cp, *vis_env, *vis_args;
109 if (ve->next == NULL) /* last field, don't pad */
110 (void)printf("%s", KI_PROC(k)->p_comm);
112 (void)printf("%-*s", v->width, KI_PROC(k)->p_comm);
116 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
118 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
120 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
122 strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
126 if (ve->next == NULL) {
128 if (termwidth == UNLIMITED) {
130 (void)printf("%s ", vis_env);
131 (void)printf("%s", vis_args);
133 left = termwidth - (totwidth - v->width);
134 if (left < 1) /* already wrapped, just use std width */
136 if ((cp = vis_env) != NULL) {
137 while (--left >= 0 && *cp)
138 (void)putchar(*cp++);
142 for (cp = vis_args; --left >= 0 && *cp != '\0';)
143 (void)putchar(*cp++);
147 (void)printf("%-*.*s", v->width, v->width, vis_args);
161 (void)printf("%-*s", v->width, KI_PROC(k)->p_comm);
173 (void)printf("%-*s", v->width, (s = KI_EPROC(k)->e_login, *s) ? s : "-");
199 if (flag & P_SINTR) /* interruptable (long) */
200 *cp = p->p_slptime >= MAXSLP ? 'I' : 'S';
218 if (!(flag & P_INMEM))
220 if (p->p_nice < NZERO)
222 else if (p->p_nice > NZERO)
226 if (flag & P_WEXIT && p->p_stat != SZOMB)
230 if ((flag & P_SYSTEM) || p->p_lock > 0)
232 if (KI_EPROC(k)->e_flag & EPROC_SLEADER)
234 if ((flag & P_CONTROLT) && KI_EPROC(k)->e_pgid == KI_EPROC(k)->e_tpgid)
239 (void)printf("%-*s", v->width, buf);
250 (void)printf("%*d", v->width, KI_PROC(k)->p_priority - PZERO);
262 (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0));
269 return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0)));
281 (int)v->width, user_from_uid(KI_EPROC(k)->e_pcred.p_ruid, 0));
288 return (strlen(user_from_uid(KI_EPROC(k)->e_pcred.p_ruid, 0)));
301 dev = KI_EPROC(k)->e_tdev;
303 (void)printf("%*s", v->width, "??");
305 (void)snprintf(buff, sizeof(buff),
306 "%d/%d", major(dev), minor(dev));
307 (void)printf("%*s", v->width, buff);
321 dev = KI_EPROC(k)->e_tdev;
322 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
323 (void)printf("%*s ", v->width-1, "??");
325 if (strncmp(ttname, "tty", 3) == 0 ||
326 strncmp(ttname, "cua", 3) == 0)
328 (void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
329 KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-');
343 dev = KI_EPROC(k)->e_tdev;
344 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
345 (void)printf("%-*s", v->width, "??");
347 (void)printf("%-*s", v->width, ttname);
360 static int use_ampm = -1;
363 if (!k->ki_u.u_valid) {
364 (void)printf("%-*s", v->width, "-");
369 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
371 then = k->ki_u.u_start.tv_sec;
372 tp = localtime(&then);
375 if (now - k->ki_u.u_start.tv_sec < 24 * 3600) {
376 (void)strftime(buf, sizeof(buf) - 1,
377 use_ampm ? "%l:%M%p" : "%k:%M ", tp);
378 } else if (now - k->ki_u.u_start.tv_sec < 7 * 86400) {
379 (void)strftime(buf, sizeof(buf) - 1,
380 use_ampm ? "%a%I%p" : "%a%H ", tp);
382 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
383 (void)printf("%-*s", v->width, buf);
396 if (!k->ki_u.u_valid) {
397 (void)printf("%-*s", v->width, "-");
400 then = k->ki_u.u_start.tv_sec;
401 (void)strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
402 (void)printf("%-*s", v->width, buf);
413 if (KI_PROC(k)->p_wchan) {
414 if (KI_PROC(k)->p_wmesg)
415 (void)printf("%-*.*s", v->width, v->width,
416 KI_EPROC(k)->e_wmesg);
418 (void)printf("%-*lx", v->width,
419 (long)KI_PROC(k)->p_wchan);
421 (void)printf("%-*s", v->width, "-");
425 #define pgtok(a) (((a)*getpagesize())/1024)
436 (void)printf("%*d", v->width,
437 (KI_EPROC(k)->e_vm.vm_map.size/1024));
448 /* XXX don't have info about shared */
449 (void)printf("%*lu", v->width,
450 (u_long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
454 p_rssize(k, ve) /* doesn't account for text */
461 (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
471 long psecs; /* "parts" of a second. first micro, then centi */
473 static char decimal_point = 0;
476 decimal_point = localeconv()->decimal_point[0];
478 if (KI_PROC(k)->p_stat == SZOMB || !k->ki_u.u_valid) {
483 * This counts time spent handling interrupts. We could
484 * fix this, but it is not 100% trivial (and interrupt
485 * time fractions only work on the sparc anyway). XXX
487 secs = KI_PROC(k)->p_runtime / 1000000;
488 psecs = KI_PROC(k)->p_runtime % 1000000;
490 secs += k->ki_u.u_cru.ru_utime.tv_sec +
491 k->ki_u.u_cru.ru_stime.tv_sec;
492 psecs += k->ki_u.u_cru.ru_utime.tv_usec +
493 k->ki_u.u_cru.ru_stime.tv_usec;
496 * round and scale to 100's
498 psecs = (psecs + 5000) / 10000;
502 (void)snprintf(obuff, sizeof(obuff),
503 "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs);
504 (void)printf("%*s", v->width, obuff);
520 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
522 /* XXX - I don't like this */
523 if (p->p_swtime == 0 || (p->p_flag & P_INMEM) == 0)
526 return (100.0 * fxtofl(p->p_pctcpu));
527 return (100.0 * fxtofl(p->p_pctcpu) /
528 (1.0 - exp(p->p_swtime * log(fxtofl(ccpu)))));
539 (void)printf("%*.1f", v->width, getpcpu(k));
559 if ((p->p_flag & P_INMEM) == 0)
561 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
563 /* XXX don't have info about shared */
564 fracmem = ((float)e->e_vm.vm_rssize + szptudot)/mempages;
565 return (100.0 * fracmem);
576 (void)printf("%*.1f", v->width, getpmem(k));
587 (void)printf("%*ld", v->width,
588 k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0);
600 (void)printf("%*s", v->width, "-");
611 (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_tsize));
625 prtp = (struct rtprio *) ((char *)KI_PROC(k) + v->off);
629 case RTP_PRIO_REALTIME:
630 snprintf(str, sizeof(str), "real:%u", prio);
632 case RTP_PRIO_NORMAL:
633 strncpy(str, "normal", sizeof(str));
636 snprintf(str, sizeof(str), "idle:%u", prio);
639 snprintf(str, sizeof(str), "%u:%u", type, prio);
642 str[sizeof(str) - 1] = '\0';
643 (void)printf("%*s", v->width, str);
647 * Generic output routines. Print fields from various prototype
655 static char ofmt[32] = "%";
663 while ((*cp++ = *fcp++));
667 (void)printf(ofmt, v->width, *(char *)bp);
670 (void)printf(ofmt, v->width, *(u_char *)bp);
673 (void)printf(ofmt, v->width, *(short *)bp);
676 (void)printf(ofmt, v->width, *(u_short *)bp);
679 (void)printf(ofmt, v->width, *(int *)bp);
682 (void)printf(ofmt, v->width, *(u_int *)bp);
685 (void)printf(ofmt, v->width, *(long *)bp);
688 (void)printf(ofmt, v->width, *(u_long *)bp);
691 (void)printf(ofmt, v->width, *(u_long *)bp);
694 errx(1, "unknown type %d", v->type);
706 printval((char *)((char *)KI_PROC(k) + v->off), v);
717 printval((char *)((char *)KI_EPROC(k) + v->off), v);
729 printval((char *)((char *)&k->ki_u + v->off), v);
731 (void)printf("%*s", v->width, "-");
743 printval((char *)((char *)(&k->ki_u.u_ru) + v->off), v);
745 (void)printf("%*s", v->width, "-");