etc/protocols - sync with NetBSD-8
[minix.git] / bin / ps / print.c
blob48c4ea1e1510d6985be69b7fa5c040cf651f7d96
1 /* $NetBSD: print.c,v 1.123 2014/11/15 01:58:34 joerg Exp $ */
3 /*
4 * Copyright (c) 2000, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Simon Burge.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1990, 1993, 1994
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
61 #include <sys/cdefs.h>
62 #ifndef lint
63 #if 0
64 static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94";
65 #else
66 __RCSID("$NetBSD: print.c,v 1.123 2014/11/15 01:58:34 joerg Exp $");
67 #endif
68 #endif /* not lint */
70 #include <sys/param.h>
71 #include <sys/time.h>
72 #include <sys/resource.h>
73 #include <sys/lwp.h>
74 #include <sys/proc.h>
75 #include <sys/stat.h>
76 #include <sys/ucred.h>
77 #include <sys/sysctl.h>
79 #include <err.h>
80 #include <grp.h>
81 #include <kvm.h>
82 #include <math.h>
83 #include <nlist.h>
84 #include <pwd.h>
85 #include <stddef.h>
86 #include <stdio.h>
87 #include <stdlib.h>
88 #include <string.h>
89 #include <time.h>
90 #include <tzfile.h>
91 #include <unistd.h>
93 #include "ps.h"
95 static char *cmdpart(char *);
96 static void printval(void *, VAR *, enum mode);
97 static int titlecmp(char *, char **);
99 static void doubleprintorsetwidth(VAR *, double, int, enum mode);
100 static void intprintorsetwidth(VAR *, int, enum mode);
101 static void strprintorsetwidth(VAR *, const char *, enum mode);
103 static time_t now;
105 #define min(a,b) ((a) <= (b) ? (a) : (b))
107 static int
108 iwidth(u_int64_t v)
110 u_int64_t nlim, lim;
111 int w = 1;
113 for (lim = 10; v >= lim; lim = nlim) {
114 nlim = lim * 10;
115 w++;
116 if (nlim < lim)
117 break;
119 return w;
122 static char *
123 cmdpart(char *arg0)
125 char *cp;
127 return ((cp = strrchr(arg0, '/')) != NULL ? cp + 1 : arg0);
130 void
131 printheader(void)
133 int len;
134 VAR *v;
135 struct varent *vent;
136 static int firsttime = 1;
137 static int noheader = 0;
140 * If all the columns have user-specified null headers,
141 * don't print the blank header line at all.
143 if (firsttime) {
144 SIMPLEQ_FOREACH(vent, &displaylist, next) {
145 if (vent->var->header[0])
146 break;
148 if (vent == NULL) {
149 noheader = 1;
150 firsttime = 0;
154 if (noheader)
155 return;
157 SIMPLEQ_FOREACH(vent, &displaylist, next) {
158 v = vent->var;
159 if (firsttime) {
160 len = strlen(v->header);
161 if (len > v->width)
162 v->width = len;
163 totwidth += v->width + 1; /* +1 for space */
165 if (v->flag & LJUST) {
166 if (SIMPLEQ_NEXT(vent, next) == NULL) /* last one */
167 (void)printf("%s", v->header);
168 else
169 (void)printf("%-*s", v->width,
170 v->header);
171 } else
172 (void)printf("%*s", v->width, v->header);
173 if (SIMPLEQ_NEXT(vent, next) != NULL)
174 (void)putchar(' ');
176 (void)putchar('\n');
177 if (firsttime) {
178 firsttime = 0;
179 totwidth--; /* take off last space */
184 * Return 1 if the command name in the argument vector (u-area) does
185 * not match the command name (p_comm)
187 static int
188 titlecmp(char *name, char **argv)
190 char *title;
191 int namelen;
194 /* no argument vector == no match; system processes/threads do that */
195 if (argv == 0 || argv[0] == 0)
196 return (1);
198 title = cmdpart(argv[0]);
200 /* the basename matches */
201 if (!strcmp(name, title))
202 return (0);
204 /* handle login shells, by skipping the leading - */
205 if (title[0] == '-' && !strcmp(name, title + 1))
206 return (0);
208 namelen = strlen(name);
210 /* handle daemons that report activity as daemonname: activity */
211 if (argv[1] == 0 &&
212 !strncmp(name, title, namelen) &&
213 title[namelen + 0] == ':' &&
214 title[namelen + 1] == ' ')
215 return (0);
217 return (1);
220 static void
221 doubleprintorsetwidth(VAR *v, double val, int prec, enum mode mode)
223 int fmtlen;
225 if (mode == WIDTHMODE) {
226 if (val < 0.0 && val < v->longestnd) {
227 fmtlen = (int)log10(-val) + prec + 2;
228 v->longestnd = val;
229 if (fmtlen > v->width)
230 v->width = fmtlen;
231 } else if (val > 0.0 && val > v->longestpd) {
232 fmtlen = (int)log10(val) + prec + 1;
233 v->longestpd = val;
234 if (fmtlen > v->width)
235 v->width = fmtlen;
237 } else {
238 (void)printf("%*.*f", v->width, prec, val);
242 static void
243 intprintorsetwidth(VAR *v, int val, enum mode mode)
245 int fmtlen;
247 if (mode == WIDTHMODE) {
248 if (val < 0 && val < v->longestn) {
249 v->longestn = val;
250 fmtlen = iwidth(-val) + 1;
251 if (fmtlen > v->width)
252 v->width = fmtlen;
253 } else if (val > 0 && val > v->longestp) {
254 v->longestp = val;
255 fmtlen = iwidth(val);
256 if (fmtlen > v->width)
257 v->width = fmtlen;
259 } else
260 (void)printf("%*d", v->width, val);
263 static void
264 strprintorsetwidth(VAR *v, const char *str, enum mode mode)
266 int len;
268 if (mode == WIDTHMODE) {
269 len = strlen(str);
270 if (len > v->width)
271 v->width = len;
272 } else {
273 if (v->flag & LJUST)
274 (void)printf("%-*.*s", v->width, v->width, str);
275 else
276 (void)printf("%*.*s", v->width, v->width, str);
280 void
281 command(void *arg, VARENT *ve, enum mode mode)
283 struct kinfo_proc2 *ki;
284 VAR *v;
285 int left;
286 char **argv, **p, *name;
288 if (mode == WIDTHMODE)
289 return;
291 ki = arg;
292 v = ve->var;
293 if (SIMPLEQ_NEXT(ve, next) != NULL || termwidth != UNLIMITED) {
294 if (SIMPLEQ_NEXT(ve, next) == NULL) {
295 left = termwidth - (totwidth - v->width);
296 if (left < 1) /* already wrapped, just use std width */
297 left = v->width;
298 } else
299 left = v->width;
300 } else
301 left = -1;
302 if (needenv && kd) {
303 argv = kvm_getenvv2(kd, ki, termwidth);
304 if ((p = argv) != NULL) {
305 while (*p) {
306 fmt_puts(*p, &left);
307 p++;
308 fmt_putc(' ', &left);
312 if (needcomm) {
313 name = ki->p_comm;
314 if (!commandonly) {
315 argv = kvm_getargv2(kd, ki, termwidth);
316 if ((p = argv) != NULL) {
317 while (*p) {
318 fmt_puts(*p, &left);
319 p++;
320 fmt_putc(' ', &left);
321 if (v->flag & ARGV0)
322 break;
324 if (!(v->flag & ARGV0) &&
325 titlecmp(name, argv)) {
327 * append the real command name within
328 * parentheses, if the command name
329 * does not match the one in the
330 * argument vector
332 fmt_putc('(', &left);
333 fmt_puts(name, &left);
334 fmt_putc(')', &left);
336 } else {
338 * Commands that don't set an argv vector
339 * are printed with square brackets if they
340 * are system commands. Otherwise they are
341 * printed within parentheses.
343 if (ki->p_flag & P_SYSTEM) {
344 fmt_putc('[', &left);
345 fmt_puts(name, &left);
346 fmt_putc(']', &left);
347 } else {
348 fmt_putc('(', &left);
349 fmt_puts(name, &left);
350 fmt_putc(')', &left);
353 } else {
354 fmt_puts(name, &left);
357 if (SIMPLEQ_NEXT(ve, next) != NULL && left > 0)
358 (void)printf("%*s", left, "");
361 void
362 groups(void *arg, VARENT *ve, enum mode mode)
364 struct kinfo_proc2 *ki;
365 VAR *v;
366 int left, i;
367 char buf[16], *p;
369 if (mode == WIDTHMODE)
370 return;
372 ki = arg;
373 v = ve->var;
374 if (SIMPLEQ_NEXT(ve, next) != NULL || termwidth != UNLIMITED) {
375 if (SIMPLEQ_NEXT(ve, next) == NULL) {
376 left = termwidth - (totwidth - v->width);
377 if (left < 1) /* already wrapped, just use std width */
378 left = v->width;
379 } else
380 left = v->width;
381 } else
382 left = -1;
384 if (ki->p_ngroups == 0)
385 fmt_putc('-', &left);
387 for (i = 0; i < ki->p_ngroups; i++) {
388 (void)snprintf(buf, sizeof(buf), "%d", ki->p_groups[i]);
389 if (i)
390 fmt_putc(' ', &left);
391 for (p = &buf[0]; *p; p++)
392 fmt_putc(*p, &left);
395 if (SIMPLEQ_NEXT(ve, next) != NULL && left > 0)
396 (void)printf("%*s", left, "");
399 void
400 groupnames(void *arg, VARENT *ve, enum mode mode)
402 struct kinfo_proc2 *ki;
403 VAR *v;
404 int left, i;
405 const char *p;
407 if (mode == WIDTHMODE)
408 return;
410 ki = arg;
411 v = ve->var;
412 if (SIMPLEQ_NEXT(ve, next) != NULL || termwidth != UNLIMITED) {
413 if (SIMPLEQ_NEXT(ve, next) == NULL) {
414 left = termwidth - (totwidth - v->width);
415 if (left < 1) /* already wrapped, just use std width */
416 left = v->width;
417 } else
418 left = v->width;
419 } else
420 left = -1;
422 if (ki->p_ngroups == 0)
423 fmt_putc('-', &left);
425 for (i = 0; i < ki->p_ngroups; i++) {
426 if (i)
427 fmt_putc(' ', &left);
428 for (p = group_from_gid(ki->p_groups[i], 0); *p; p++)
429 fmt_putc(*p, &left);
432 if (SIMPLEQ_NEXT(ve, next) != NULL && left > 0)
433 (void)printf("%*s", left, "");
436 void
437 ucomm(void *arg, VARENT *ve, enum mode mode)
439 struct kinfo_proc2 *k;
440 VAR *v;
442 k = arg;
443 v = ve->var;
444 strprintorsetwidth(v, k->p_comm, mode);
447 void
448 emul(void *arg, VARENT *ve, enum mode mode)
450 struct kinfo_proc2 *k;
451 VAR *v;
453 k = arg;
454 v = ve->var;
455 strprintorsetwidth(v, k->p_ename, mode);
458 void
459 logname(void *arg, VARENT *ve, enum mode mode)
461 struct kinfo_proc2 *k;
462 VAR *v;
464 k = arg;
465 v = ve->var;
466 strprintorsetwidth(v, k->p_login, mode);
469 void
470 state(void *arg, VARENT *ve, enum mode mode)
472 struct kinfo_proc2 *k;
473 int flag, is_zombie;
474 char *cp;
475 VAR *v;
476 char buf[16];
478 k = arg;
479 is_zombie = 0;
480 v = ve->var;
481 flag = k->p_flag;
482 cp = buf;
485 * NOTE: There are historical letters, which are no longer used:
487 * - W: indicated that process is swapped out.
488 * - L: indicated non-zero l_holdcnt (i.e. that process was
489 * prevented from swapping-out.
491 * These letters should not be used for new states to avoid
492 * conflicts with old applications which might depend on them.
494 switch (k->p_stat) {
496 case LSSTOP:
497 *cp = 'T';
498 break;
500 case LSSLEEP:
501 if (flag & L_SINTR) /* interruptable (long) */
502 *cp = (int)k->p_slptime >= maxslp ? 'I' : 'S';
503 else
504 *cp = 'D';
505 break;
507 case LSRUN:
508 case LSIDL:
509 *cp = 'R';
510 break;
512 case LSONPROC:
513 *cp = 'O';
514 break;
516 case LSZOMB:
517 *cp = 'Z';
518 is_zombie = 1;
519 break;
521 case LSSUSPENDED:
522 *cp = 'U';
523 break;
525 default:
526 *cp = '?';
528 cp++;
529 if (k->p_nice < NZERO)
530 *cp++ = '<';
531 else if (k->p_nice > NZERO)
532 *cp++ = 'N';
533 if (flag & P_TRACED)
534 *cp++ = 'X';
535 if (flag & P_WEXIT && !is_zombie)
536 *cp++ = 'E';
537 if (flag & P_PPWAIT)
538 *cp++ = 'V';
539 if (flag & P_SYSTEM)
540 *cp++ = 'K';
541 if (k->p_eflag & EPROC_SLEADER)
542 *cp++ = 's';
543 if (flag & P_SA)
544 *cp++ = 'a';
545 else if (k->p_nlwps > 1)
546 *cp++ = 'l';
547 if ((flag & P_CONTROLT) && k->p__pgid == k->p_tpgid)
548 *cp++ = '+';
549 *cp = '\0';
550 strprintorsetwidth(v, buf, mode);
553 void
554 lstate(void *arg, VARENT *ve, enum mode mode)
556 struct kinfo_lwp *k;
557 int flag;
558 char *cp;
559 VAR *v;
560 char buf[16];
562 k = arg;
563 v = ve->var;
564 flag = k->l_flag;
565 cp = buf;
567 switch (k->l_stat) {
569 case LSSTOP:
570 *cp = 'T';
571 break;
573 case LSSLEEP:
574 if (flag & L_SINTR) /* interruptible (long) */
575 *cp = (int)k->l_slptime >= maxslp ? 'I' : 'S';
576 else
577 *cp = 'D';
578 break;
580 case LSRUN:
581 case LSIDL:
582 *cp = 'R';
583 break;
585 case LSONPROC:
586 *cp = 'O';
587 break;
589 case LSZOMB:
590 case LSDEAD:
591 *cp = 'Z';
592 break;
594 case LSSUSPENDED:
595 *cp = 'U';
596 break;
598 default:
599 *cp = '?';
601 cp++;
602 if (flag & L_SYSTEM)
603 *cp++ = 'K';
604 if (flag & L_SA)
605 *cp++ = 'a';
606 if (flag & L_DETACHED)
607 *cp++ = '-';
608 *cp = '\0';
609 strprintorsetwidth(v, buf, mode);
612 void
613 pnice(void *arg, VARENT *ve, enum mode mode)
615 struct kinfo_proc2 *k;
616 VAR *v;
618 k = arg;
619 v = ve->var;
620 intprintorsetwidth(v, k->p_nice - NZERO, mode);
623 void
624 pri(void *arg, VARENT *ve, enum mode mode)
626 struct kinfo_lwp *l;
627 VAR *v;
629 l = arg;
630 v = ve->var;
631 intprintorsetwidth(v, l->l_priority, mode);
634 void
635 uname(void *arg, VARENT *ve, enum mode mode)
637 struct kinfo_proc2 *k;
638 VAR *v;
640 k = arg;
641 v = ve->var;
642 strprintorsetwidth(v, user_from_uid(k->p_uid, 0), mode);
645 void
646 runame(void *arg, VARENT *ve, enum mode mode)
648 struct kinfo_proc2 *k;
649 VAR *v;
651 k = arg;
652 v = ve->var;
653 strprintorsetwidth(v, user_from_uid(k->p_ruid, 0), mode);
656 void
657 svuname(void *arg, VARENT *ve, enum mode mode)
659 struct kinfo_proc2 *k;
660 VAR *v;
662 k = arg;
663 v = ve->var;
664 strprintorsetwidth(v, user_from_uid(k->p_svuid, 0), mode);
667 void
668 gname(void *arg, VARENT *ve, enum mode mode)
670 struct kinfo_proc2 *k;
671 VAR *v;
673 k = arg;
674 v = ve->var;
675 strprintorsetwidth(v, group_from_gid(k->p_gid, 0), mode);
678 void
679 rgname(void *arg, VARENT *ve, enum mode mode)
681 struct kinfo_proc2 *k;
682 VAR *v;
684 k = arg;
685 v = ve->var;
686 strprintorsetwidth(v, group_from_gid(k->p_rgid, 0), mode);
689 void
690 svgname(void *arg, VARENT *ve, enum mode mode)
692 struct kinfo_proc2 *k;
693 VAR *v;
695 k = arg;
696 v = ve->var;
697 strprintorsetwidth(v, group_from_gid(k->p_svgid, 0), mode);
700 void
701 tdev(void *arg, VARENT *ve, enum mode mode)
703 struct kinfo_proc2 *k;
704 VAR *v;
705 dev_t dev;
706 char buff[16];
708 k = arg;
709 v = ve->var;
710 dev = k->p_tdev;
711 if (dev == NODEV) {
712 if (mode == PRINTMODE)
713 (void)printf("%*s", v->width, "?");
714 else
715 if (v->width < 2)
716 v->width = 2;
717 } else {
718 (void)snprintf(buff, sizeof(buff),
719 "%lld/%lld", (long long)major(dev), (long long)minor(dev));
720 strprintorsetwidth(v, buff, mode);
724 void
725 tname(void *arg, VARENT *ve, enum mode mode)
727 struct kinfo_proc2 *k;
728 VAR *v;
729 dev_t dev;
730 const char *ttname;
731 int noctty;
733 k = arg;
734 v = ve->var;
735 dev = k->p_tdev;
736 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) {
737 if (mode == PRINTMODE)
738 (void)printf("%-*s", v->width, "?");
739 else
740 if (v->width < 2)
741 v->width = 2;
742 } else {
743 #ifdef __minix
744 /* Actually shorten TTY names. "console" is *really* long. */
745 if (strcmp(ttname, "console") == 0)
746 ttname = "co";
747 else if (strncmp(ttname, "tty", 3) == 0 && ttname[3] != '\0')
748 ttname += 3;
749 else if (strncmp(ttname, "pts/", 4) == 0 && ttname[4] != '\0')
750 ttname += 4; /* this is what FreeBSD does */
751 #endif /* __minix */
752 noctty = !(k->p_eflag & EPROC_CTTY) ? 1 : 0;
753 if (mode == WIDTHMODE) {
754 int fmtlen;
756 fmtlen = strlen(ttname) + noctty;
757 if (v->width < fmtlen)
758 v->width = fmtlen;
759 } else {
760 if (noctty)
761 (void)printf("%-*s-", v->width - 1, ttname);
762 else
763 (void)printf("%-*s", v->width, ttname);
768 void
769 longtname(void *arg, VARENT *ve, enum mode mode)
771 struct kinfo_proc2 *k;
772 VAR *v;
773 dev_t dev;
774 const char *ttname;
776 k = arg;
777 v = ve->var;
778 dev = k->p_tdev;
779 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) {
780 if (mode == PRINTMODE)
781 (void)printf("%-*s", v->width, "?");
782 else
783 if (v->width < 2)
784 v->width = 2;
785 } else {
786 strprintorsetwidth(v, ttname, mode);
790 void
791 started(void *arg, VARENT *ve, enum mode mode)
793 struct kinfo_proc2 *k;
794 VAR *v;
795 time_t startt;
796 struct tm *tp;
797 char buf[100], *cp;
799 k = arg;
800 v = ve->var;
801 if (!k->p_uvalid) {
802 if (mode == PRINTMODE)
803 (void)printf("%*s", v->width, "-");
804 return;
807 startt = k->p_ustart_sec;
808 tp = localtime(&startt);
809 if (now == 0)
810 (void)time(&now);
811 if (now - k->p_ustart_sec < SECSPERDAY)
812 /* I *hate* SCCS... */
813 (void)strftime(buf, sizeof(buf) - 1, "%l:%" "M%p", tp);
814 else if (now - k->p_ustart_sec < DAYSPERWEEK * SECSPERDAY)
815 /* I *hate* SCCS... */
816 (void)strftime(buf, sizeof(buf) - 1, "%a%" "I%p", tp);
817 else
818 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
819 /* %e and %l can start with a space. */
820 cp = buf;
821 if (*cp == ' ')
822 cp++;
823 strprintorsetwidth(v, cp, mode);
826 void
827 lstarted(void *arg, VARENT *ve, enum mode mode)
829 struct kinfo_proc2 *k;
830 VAR *v;
831 time_t startt;
832 char buf[100];
834 k = arg;
835 v = ve->var;
836 if (!k->p_uvalid) {
838 * Minimum width is less than header - we don't
839 * need to check it every time.
841 if (mode == PRINTMODE)
842 (void)printf("%*s", v->width, "-");
843 return;
845 startt = k->p_ustart_sec;
847 /* assume all times are the same length */
848 if (mode != WIDTHMODE || v->width == 0) {
849 (void)strftime(buf, sizeof(buf) -1, "%c",
850 localtime(&startt));
851 strprintorsetwidth(v, buf, mode);
855 void
856 elapsed(void *arg, VARENT *ve, enum mode mode)
858 struct kinfo_proc2 *k;
859 VAR *v;
860 int32_t origseconds, secs, mins, hours, days;
861 int fmtlen, printed_something;
863 k = arg;
864 v = ve->var;
865 if (k->p_uvalid == 0) {
866 origseconds = 0;
867 } else {
868 if (now == 0)
869 (void)time(&now);
870 origseconds = now - k->p_ustart_sec;
871 if (origseconds < 0) {
873 * Don't try to be fancy if the machine's
874 * clock has been rewound to before the
875 * process "started".
877 origseconds = 0;
881 secs = origseconds;
882 mins = secs / SECSPERMIN;
883 secs %= SECSPERMIN;
884 hours = mins / MINSPERHOUR;
885 mins %= MINSPERHOUR;
886 days = hours / HOURSPERDAY;
887 hours %= HOURSPERDAY;
889 if (mode == WIDTHMODE) {
890 if (origseconds == 0)
891 /* non-zero so fmtlen is calculated at least once */
892 origseconds = 1;
894 if (origseconds > v->longestp) {
895 v->longestp = origseconds;
897 if (days > 0) {
898 /* +9 for "-hh:mm:ss" */
899 fmtlen = iwidth(days) + 9;
900 } else if (hours > 0) {
901 /* +6 for "mm:ss" */
902 fmtlen = iwidth(hours) + 6;
903 } else {
904 /* +3 for ":ss" */
905 fmtlen = iwidth(mins) + 3;
908 if (fmtlen > v->width)
909 v->width = fmtlen;
911 } else {
912 printed_something = 0;
913 fmtlen = v->width;
915 if (days > 0) {
916 (void)printf("%*d", fmtlen - 9, days);
917 printed_something = 1;
918 } else if (fmtlen > 9) {
919 (void)printf("%*s", fmtlen - 9, "");
921 if (fmtlen > 9)
922 fmtlen = 9;
924 if (printed_something) {
925 (void)printf("-%.*d", fmtlen - 7, hours);
926 printed_something = 1;
927 } else if (hours > 0) {
928 (void)printf("%*d", fmtlen - 6, hours);
929 printed_something = 1;
930 } else if (fmtlen > 6) {
931 (void)printf("%*s", fmtlen - 6, "");
933 if (fmtlen > 6)
934 fmtlen = 6;
936 /* Don't need to set fmtlen or printed_something any more... */
937 if (printed_something) {
938 (void)printf(":%.*d", fmtlen - 4, mins);
939 } else if (mins > 0) {
940 (void)printf("%*d", fmtlen - 3, mins);
941 } else if (fmtlen > 3) {
942 (void)printf("%*s", fmtlen - 3, "0");
945 (void)printf(":%.2d", secs);
949 void
950 wchan(void *arg, VARENT *ve, enum mode mode)
952 struct kinfo_lwp *l;
953 VAR *v;
954 char *buf;
956 l = arg;
957 v = ve->var;
958 if (l->l_wchan) {
959 if (l->l_wmesg[0]) {
960 strprintorsetwidth(v, l->l_wmesg, mode);
961 v->width = min(v->width, KI_WMESGLEN);
962 } else {
963 (void)asprintf(&buf, "%-*" PRIx64, v->width,
964 l->l_wchan);
965 if (buf == NULL)
966 err(1, "%s", "");
967 strprintorsetwidth(v, buf, mode);
968 v->width = min(v->width, KI_WMESGLEN);
969 free(buf);
971 } else {
972 if (mode == PRINTMODE)
973 (void)printf("%-*s", v->width, "-");
977 #define pgtok(a) (((a)*(size_t)getpagesize())/1024)
979 void
980 vsize(void *arg, VARENT *ve, enum mode mode)
982 struct kinfo_proc2 *k;
983 VAR *v;
985 k = arg;
986 v = ve->var;
987 intprintorsetwidth(v, pgtok(k->p_vm_msize), mode);
990 void
991 rssize(void *arg, VARENT *ve, enum mode mode)
993 struct kinfo_proc2 *k;
994 VAR *v;
996 k = arg;
997 v = ve->var;
998 /* XXX don't have info about shared */
999 intprintorsetwidth(v, pgtok(k->p_vm_rssize), mode);
1002 void
1003 p_rssize(void *arg, VARENT *ve, enum mode mode) /* doesn't account for text */
1005 struct kinfo_proc2 *k;
1006 VAR *v;
1008 k = arg;
1009 v = ve->var;
1010 intprintorsetwidth(v, pgtok(k->p_vm_rssize), mode);
1013 void
1014 cpuid(void *arg, VARENT *ve, enum mode mode)
1016 struct kinfo_lwp *l;
1017 VAR *v;
1019 l = arg;
1020 v = ve->var;
1021 intprintorsetwidth(v, l->l_cpuid, mode);
1024 static void
1025 cputime1(int32_t secs, int32_t psecs, VAR *v, enum mode mode)
1027 int fmtlen;
1030 * round and scale to 100's
1032 psecs = (psecs + 5000) / 10000;
1033 secs += psecs / 100;
1034 psecs = psecs % 100;
1036 if (mode == WIDTHMODE) {
1038 * Ugg, this is the only field where a value of 0 is longer
1039 * than the column title.
1040 * Use SECSPERMIN, because secs is divided by that when
1041 * passed to iwidth().
1043 if (secs == 0)
1044 secs = SECSPERMIN;
1046 if (secs > v->longestp) {
1047 v->longestp = secs;
1048 /* "+6" for the ":%02ld.%02ld" in the printf() below */
1049 fmtlen = iwidth(secs / SECSPERMIN) + 6;
1050 if (fmtlen > v->width)
1051 v->width = fmtlen;
1053 } else {
1054 (void)printf("%*ld:%02ld.%02ld", v->width - 6,
1055 (long)(secs / SECSPERMIN), (long)(secs % SECSPERMIN),
1056 (long)psecs);
1060 void
1061 cputime(void *arg, VARENT *ve, enum mode mode)
1063 struct kinfo_proc2 *k;
1064 VAR *v;
1065 int32_t secs;
1066 int32_t psecs; /* "parts" of a second. first micro, then centi */
1068 k = arg;
1069 v = ve->var;
1072 * This counts time spent handling interrupts. We could
1073 * fix this, but it is not 100% trivial (and interrupt
1074 * time fractions only work on the sparc anyway). XXX
1076 secs = k->p_rtime_sec;
1077 psecs = k->p_rtime_usec;
1078 if (sumrusage) {
1079 secs += k->p_uctime_sec;
1080 psecs += k->p_uctime_usec;
1083 cputime1(secs, psecs, v, mode);
1086 void
1087 lcputime(void *arg, VARENT *ve, enum mode mode)
1089 struct kinfo_lwp *l;
1090 VAR *v;
1091 int32_t secs;
1092 int32_t psecs; /* "parts" of a second. first micro, then centi */
1094 l = arg;
1095 v = ve->var;
1097 secs = l->l_rtime_sec;
1098 psecs = l->l_rtime_usec;
1100 cputime1(secs, psecs, v, mode);
1103 double
1104 getpcpu(const struct kinfo_proc2 *k)
1106 static int failure;
1108 if (!nlistread)
1109 failure = (kd) ? donlist() : 1;
1110 if (failure)
1111 return (0.0);
1113 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
1115 if (k->p_swtime == 0 || k->p_realstat == SZOMB)
1116 return (0.0);
1117 if (rawcpu)
1118 return (100.0 * fxtofl(k->p_pctcpu));
1119 return (100.0 * fxtofl(k->p_pctcpu) /
1120 (1.0 - exp(k->p_swtime * log(ccpu))));
1123 void
1124 pcpu(void *arg, VARENT *ve, enum mode mode)
1126 struct kinfo_proc2 *k;
1127 VAR *v;
1128 double dbl;
1130 k = arg;
1131 v = ve->var;
1132 dbl = getpcpu(k);
1133 doubleprintorsetwidth(v, dbl, (dbl >= 99.95) ? 0 : 1, mode);
1136 double
1137 getpmem(const struct kinfo_proc2 *k)
1139 static int failure;
1140 double fracmem;
1141 int szptudot;
1143 if (!nlistread)
1144 failure = (kd) ? donlist() : 1;
1145 if (failure)
1146 return (0.0);
1148 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
1149 szptudot = uspace/getpagesize();
1150 /* XXX don't have info about shared */
1151 fracmem = ((float)k->p_vm_rssize + szptudot)/mempages;
1152 return (100.0 * fracmem);
1155 void
1156 pmem(void *arg, VARENT *ve, enum mode mode)
1158 struct kinfo_proc2 *k;
1159 VAR *v;
1161 k = arg;
1162 v = ve->var;
1163 doubleprintorsetwidth(v, getpmem(k), 1, mode);
1166 void
1167 pagein(void *arg, VARENT *ve, enum mode mode)
1169 struct kinfo_proc2 *k;
1170 VAR *v;
1172 k = arg;
1173 v = ve->var;
1174 intprintorsetwidth(v, k->p_uvalid ? k->p_uru_majflt : 0, mode);
1177 void
1178 maxrss(void *arg, VARENT *ve, enum mode mode)
1180 VAR *v;
1182 v = ve->var;
1183 /* No need to check width! */
1184 if (mode == PRINTMODE)
1185 (void)printf("%*s", v->width, "-");
1188 void
1189 tsize(void *arg, VARENT *ve, enum mode mode)
1191 struct kinfo_proc2 *k;
1192 VAR *v;
1194 k = arg;
1195 v = ve->var;
1196 intprintorsetwidth(v, pgtok(k->p_vm_tsize), mode);
1200 * Generic output routines. Print fields from various prototype
1201 * structures.
1203 static void
1204 printval(void *bp, VAR *v, enum mode mode)
1206 static char ofmt[32] = "%";
1207 int width, vok, fmtlen;
1208 const char *fcp;
1209 char *cp;
1210 int64_t val;
1211 u_int64_t uval;
1213 val = 0; /* XXXGCC -Wuninitialized [hpcarm] */
1214 uval = 0; /* XXXGCC -Wuninitialized [hpcarm] */
1217 * Note that the "INF127" check is nonsensical for types
1218 * that are or can be signed.
1220 #define GET(type) (*(type *)bp)
1221 #define CHK_INF127(n) (((n) > 127) && (v->flag & INF127) ? 127 : (n))
1223 #define VSIGN 1
1224 #define VUNSIGN 2
1225 #define VPTR 3
1227 if (mode == WIDTHMODE) {
1228 vok = 0;
1229 switch (v->type) {
1230 case CHAR:
1231 val = GET(char);
1232 vok = VSIGN;
1233 break;
1234 case UCHAR:
1235 uval = CHK_INF127(GET(u_char));
1236 vok = VUNSIGN;
1237 break;
1238 case SHORT:
1239 val = GET(short);
1240 vok = VSIGN;
1241 break;
1242 case USHORT:
1243 uval = CHK_INF127(GET(u_short));
1244 vok = VUNSIGN;
1245 break;
1246 case INT32:
1247 val = GET(int32_t);
1248 vok = VSIGN;
1249 break;
1250 case INT:
1251 val = GET(int);
1252 vok = VSIGN;
1253 break;
1254 case UINT:
1255 case UINT32:
1256 uval = CHK_INF127(GET(u_int));
1257 vok = VUNSIGN;
1258 break;
1259 case LONG:
1260 val = GET(long);
1261 vok = VSIGN;
1262 break;
1263 case ULONG:
1264 uval = CHK_INF127(GET(u_long));
1265 vok = VUNSIGN;
1266 break;
1267 case KPTR:
1268 uval = GET(u_int64_t);
1269 vok = VPTR;
1270 break;
1271 case KPTR24:
1272 uval = GET(u_int64_t);
1273 uval &= 0xffffff;
1274 vok = VPTR;
1275 break;
1276 case INT64:
1277 val = GET(int64_t);
1278 vok = VSIGN;
1279 break;
1280 case UINT64:
1281 uval = CHK_INF127(GET(u_int64_t));
1282 vok = VUNSIGN;
1283 break;
1285 case SIGLIST:
1286 default:
1287 /* nothing... */;
1289 switch (vok) {
1290 case VSIGN:
1291 if (val < 0 && val < v->longestn) {
1292 v->longestn = val;
1293 fmtlen = iwidth(-val) + 1;
1294 if (fmtlen > v->width)
1295 v->width = fmtlen;
1296 } else if (val > 0 && val > v->longestp) {
1297 v->longestp = val;
1298 fmtlen = iwidth(val);
1299 if (fmtlen > v->width)
1300 v->width = fmtlen;
1302 return;
1303 case VUNSIGN:
1304 if (uval > v->longestu) {
1305 v->longestu = uval;
1306 v->width = iwidth(uval);
1308 return;
1309 case VPTR:
1310 fmtlen = 0;
1311 while (uval > 0) {
1312 uval >>= 4;
1313 fmtlen++;
1315 if (fmtlen > v->width)
1316 v->width = fmtlen;
1317 return;
1321 width = v->width;
1322 cp = ofmt + 1;
1323 fcp = v->fmt;
1324 if (v->flag & LJUST)
1325 *cp++ = '-';
1326 *cp++ = '*';
1327 while ((*cp++ = *fcp++) != '\0')
1328 continue;
1330 switch (v->type) {
1331 case CHAR:
1332 (void)printf(ofmt, width, GET(char));
1333 return;
1334 case UCHAR:
1335 (void)printf(ofmt, width, CHK_INF127(GET(u_char)));
1336 return;
1337 case SHORT:
1338 (void)printf(ofmt, width, GET(short));
1339 return;
1340 case USHORT:
1341 (void)printf(ofmt, width, CHK_INF127(GET(u_short)));
1342 return;
1343 case INT:
1344 (void)printf(ofmt, width, GET(int));
1345 return;
1346 case UINT:
1347 (void)printf(ofmt, width, CHK_INF127(GET(u_int)));
1348 return;
1349 case LONG:
1350 (void)printf(ofmt, width, GET(long));
1351 return;
1352 case ULONG:
1353 (void)printf(ofmt, width, CHK_INF127(GET(u_long)));
1354 return;
1355 case KPTR:
1356 (void)printf(ofmt, width, GET(u_int64_t));
1357 return;
1358 case KPTR24:
1359 (void)printf(ofmt, width, GET(u_int64_t) & 0xffffff);
1360 return;
1361 case INT32:
1362 (void)printf(ofmt, width, GET(int32_t));
1363 return;
1364 case UINT32:
1365 (void)printf(ofmt, width, CHK_INF127(GET(u_int32_t)));
1366 return;
1367 case SIGLIST:
1369 sigset_t *s = (sigset_t *)(void *)bp;
1370 size_t i;
1371 #define SIGSETSIZE (sizeof(s->__bits) / sizeof(s->__bits[0]))
1372 char buf[SIGSETSIZE * 8 + 1];
1374 for (i = 0; i < SIGSETSIZE; i++)
1375 (void)snprintf(&buf[i * 8], 9, "%.8x",
1376 s->__bits[(SIGSETSIZE - 1) - i]);
1378 /* Skip leading zeroes */
1379 for (i = 0; buf[i] == '0'; i++)
1380 continue;
1382 if (buf[i] == '\0')
1383 i--;
1384 strprintorsetwidth(v, buf + i, mode);
1385 #undef SIGSETSIZE
1387 return;
1388 case INT64:
1389 (void)printf(ofmt, width, GET(int64_t));
1390 return;
1391 case UINT64:
1392 (void)printf(ofmt, width, CHK_INF127(GET(u_int64_t)));
1393 return;
1394 default:
1395 errx(1, "unknown type %d", v->type);
1397 #undef GET
1398 #undef CHK_INF127
1401 void
1402 pvar(void *arg, VARENT *ve, enum mode mode)
1404 VAR *v;
1406 v = ve->var;
1407 if (v->flag & UAREA && !((struct kinfo_proc2 *)arg)->p_uvalid) {
1408 if (mode == PRINTMODE)
1409 (void)printf("%*s", v->width, "-");
1410 return;
1413 (void)printval((char *)arg + v->off, v, mode);
1416 void
1417 putimeval(void *arg, VARENT *ve, enum mode mode)
1419 VAR *v = ve->var;
1420 struct kinfo_proc2 *k = arg;
1421 ulong secs = *(uint32_t *)((char *)arg + v->off);
1422 ulong usec = *(uint32_t *)((char *)arg + v->off + sizeof (uint32_t));
1423 int fmtlen;
1425 if (!k->p_uvalid) {
1426 if (mode == PRINTMODE)
1427 (void)printf("%*s", v->width, "-");
1428 return;
1431 if (mode == WIDTHMODE) {
1432 if (secs == 0)
1433 /* non-zero so fmtlen is calculated at least once */
1434 secs = 1;
1435 if (secs > v->longestu) {
1436 v->longestu = secs;
1437 if (secs <= 999)
1438 /* sss.ssssss */
1439 fmtlen = iwidth(secs) + 6 + 1;
1440 else
1441 /* hh:mm:ss.ss */
1442 fmtlen = iwidth((secs + 1) / SECSPERHOUR)
1443 + 2 + 1 + 2 + 1 + 2 + 1;
1444 if (fmtlen > v->width)
1445 v->width = fmtlen;
1447 return;
1450 if (secs < 999)
1451 (void)printf( "%*lu.%.6lu", v->width - 6 - 1, secs, usec);
1452 else {
1453 uint h, m;
1454 usec += 5000;
1455 if (usec >= 1000000) {
1456 usec -= 1000000;
1457 secs++;
1459 m = secs / SECSPERMIN;
1460 secs -= m * SECSPERMIN;
1461 h = m / MINSPERHOUR;
1462 m -= h * MINSPERHOUR;
1463 (void)printf( "%*u:%.2u:%.2lu.%.2lu", v->width - 9, h, m, secs,
1464 usec / 10000u );
1468 void
1469 lname(void *arg, VARENT *ve, enum mode mode)
1471 struct kinfo_lwp *l;
1472 VAR *v;
1474 l = arg;
1475 v = ve->var;
1476 if (l->l_name[0] != '\0') {
1477 strprintorsetwidth(v, l->l_name, mode);
1478 v->width = min(v->width, KI_LNAMELEN);
1479 } else {
1480 if (mode == PRINTMODE)
1481 (void)printf("%-*s", v->width, "-");