1 /* $NetBSD: print.c,v 1.111 2009/03/29 01:02:49 mrg Exp $ */
4 * Copyright (c) 2000, 2007 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
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
61 #include <sys/cdefs.h>
64 static char sccsid
[] = "@(#)print.c 8.6 (Berkeley) 4/16/94";
66 __RCSID("$NetBSD: print.c,v 1.111 2009/03/29 01:02:49 mrg Exp $");
70 #include <sys/param.h>
72 #include <sys/resource.h>
76 #include <sys/ucred.h>
77 #include <sys/sysctl.h>
95 static char *cmdpart(char *);
96 static void printval(void *, VAR
*, int);
97 static int titlecmp(char *, char **);
99 static void doubleprintorsetwidth(VAR
*, double, int, int);
100 static void intprintorsetwidth(VAR
*, int, int);
101 static void strprintorsetwidth(VAR
*, const char *, int);
105 static u_int64_t
*cp_id
;
107 #define min(a,b) ((a) <= (b) ? (a) : (b))
115 for (lim
= 10; v
>= lim
; lim
= nlim
) {
129 return ((cp
= strrchr(arg0
, '/')) != NULL
? cp
+ 1 : arg0
);
138 static int firsttime
= 1;
139 static int noheader
= 0;
142 * If all the columns have user-specified null headers,
143 * don't print the blank header line at all.
146 SIMPLEQ_FOREACH(vent
, &displaylist
, next
) {
147 if (vent
->var
->header
[0])
159 SIMPLEQ_FOREACH(vent
, &displaylist
, next
) {
162 len
= strlen(v
->header
);
165 totwidth
+= v
->width
+ 1; /* +1 for space */
167 if (v
->flag
& LJUST
) {
168 if (SIMPLEQ_NEXT(vent
, next
) == NULL
) /* last one */
169 (void)printf("%s", v
->header
);
171 (void)printf("%-*s", v
->width
,
174 (void)printf("%*s", v
->width
, v
->header
);
175 if (SIMPLEQ_NEXT(vent
, next
) != NULL
)
181 totwidth
--; /* take off last space */
186 * Return 1 if the command name in the argument vector (u-area) does
187 * not match the command name (p_comm)
190 titlecmp(char *name
, char **argv
)
196 /* no argument vector == no match; system processes/threads do that */
197 if (argv
== 0 || argv
[0] == 0)
200 title
= cmdpart(argv
[0]);
202 /* the basename matches */
203 if (!strcmp(name
, title
))
206 /* handle login shells, by skipping the leading - */
207 if (title
[0] == '-' && !strcmp(name
, title
+ 1))
210 namelen
= strlen(name
);
212 /* handle daemons that report activity as daemonname: activity */
214 !strncmp(name
, title
, namelen
) &&
215 title
[namelen
+ 0] == ':' &&
216 title
[namelen
+ 1] == ' ')
223 doubleprintorsetwidth(VAR
*v
, double val
, int prec
, int mode
)
227 if (mode
== WIDTHMODE
) {
228 if (val
< 0.0 && val
< v
->longestnd
) {
229 fmtlen
= (int)log10(-val
) + prec
+ 2;
231 if (fmtlen
> v
->width
)
233 } else if (val
> 0.0 && val
> v
->longestpd
) {
234 fmtlen
= (int)log10(val
) + prec
+ 1;
236 if (fmtlen
> v
->width
)
240 (void)printf("%*.*f", v
->width
, prec
, val
);
245 intprintorsetwidth(VAR
*v
, int val
, int mode
)
249 if (mode
== WIDTHMODE
) {
250 if (val
< 0 && val
< v
->longestn
) {
252 fmtlen
= iwidth(-val
) + 1;
253 if (fmtlen
> v
->width
)
255 } else if (val
> 0 && val
> v
->longestp
) {
257 fmtlen
= iwidth(val
);
258 if (fmtlen
> v
->width
)
262 (void)printf("%*d", v
->width
, val
);
266 strprintorsetwidth(VAR
*v
, const char *str
, int mode
)
270 if (mode
== WIDTHMODE
) {
276 (void)printf("%-*.*s", v
->width
, v
->width
, str
);
278 (void)printf("%*.*s", v
->width
, v
->width
, str
);
283 command(void *arg
, VARENT
*ve
, int mode
)
285 struct kinfo_proc2
*ki
;
288 char **argv
, **p
, *name
;
290 if (mode
== WIDTHMODE
)
295 if (SIMPLEQ_NEXT(ve
, next
) != NULL
|| termwidth
!= UNLIMITED
) {
296 if (SIMPLEQ_NEXT(ve
, next
) == NULL
) {
297 left
= termwidth
- (totwidth
- v
->width
);
298 if (left
< 1) /* already wrapped, just use std width */
305 argv
= kvm_getenvv2(kd
, ki
, termwidth
);
306 if ((p
= argv
) != NULL
) {
310 fmt_putc(' ', &left
);
317 argv
= kvm_getargv2(kd
, ki
, termwidth
);
318 if ((p
= argv
) != NULL
) {
322 fmt_putc(' ', &left
);
326 if (!(v
->flag
& ARGV0
) &&
327 titlecmp(name
, argv
)) {
329 * append the real command name within
330 * parentheses, if the command name
331 * does not match the one in the
334 fmt_putc('(', &left
);
335 fmt_puts(name
, &left
);
336 fmt_putc(')', &left
);
340 * Commands that don't set an argv vector
341 * are printed with square brackets if they
342 * are system commands. Otherwise they are
343 * printed within parentheses.
345 if (ki
->p_flag
& P_SYSTEM
) {
346 fmt_putc('[', &left
);
347 fmt_puts(name
, &left
);
348 fmt_putc(']', &left
);
350 fmt_putc('(', &left
);
351 fmt_puts(name
, &left
);
352 fmt_putc(')', &left
);
356 fmt_puts(name
, &left
);
359 if (SIMPLEQ_NEXT(ve
, next
) != NULL
&& left
> 0)
360 (void)printf("%*s", left
, "");
364 groups(void *arg
, VARENT
*ve
, int mode
)
366 struct kinfo_proc2
*ki
;
371 if (mode
== WIDTHMODE
)
376 if (SIMPLEQ_NEXT(ve
, next
) != NULL
|| termwidth
!= UNLIMITED
) {
377 if (SIMPLEQ_NEXT(ve
, next
) == NULL
) {
378 left
= termwidth
- (totwidth
- v
->width
);
379 if (left
< 1) /* already wrapped, just use std width */
386 if (ki
->p_ngroups
== 0)
387 fmt_putc('-', &left
);
389 for (i
= 0; i
< ki
->p_ngroups
; i
++) {
390 (void)snprintf(buf
, sizeof(buf
), "%d", ki
->p_groups
[i
]);
392 fmt_putc(' ', &left
);
393 for (p
= &buf
[0]; *p
; p
++)
397 if (SIMPLEQ_NEXT(ve
, next
) != NULL
&& left
> 0)
398 (void)printf("%*s", left
, "");
402 groupnames(void *arg
, VARENT
*ve
, int mode
)
404 struct kinfo_proc2
*ki
;
409 if (mode
== WIDTHMODE
)
414 if (SIMPLEQ_NEXT(ve
, next
) != NULL
|| termwidth
!= UNLIMITED
) {
415 if (SIMPLEQ_NEXT(ve
, next
) == NULL
) {
416 left
= termwidth
- (totwidth
- v
->width
);
417 if (left
< 1) /* already wrapped, just use std width */
424 if (ki
->p_ngroups
== 0)
425 fmt_putc('-', &left
);
427 for (i
= 0; i
< ki
->p_ngroups
; i
++) {
429 fmt_putc(' ', &left
);
430 for (p
= group_from_gid(ki
->p_groups
[i
], 0); *p
; p
++)
434 if (SIMPLEQ_NEXT(ve
, next
) != NULL
&& left
> 0)
435 (void)printf("%*s", left
, "");
439 ucomm(void *arg
, VARENT
*ve
, int mode
)
441 struct kinfo_proc2
*k
;
446 strprintorsetwidth(v
, k
->p_comm
, mode
);
450 emul(void *arg
, VARENT
*ve
, int mode
)
452 struct kinfo_proc2
*k
;
457 strprintorsetwidth(v
, k
->p_ename
, mode
);
461 logname(void *arg
, VARENT
*ve
, int mode
)
463 struct kinfo_proc2
*k
;
468 strprintorsetwidth(v
, k
->p_login
, mode
);
472 state(void *arg
, VARENT
*ve
, int mode
)
474 struct kinfo_proc2
*k
;
487 * NOTE: There are historical letters, which are no longer used:
489 * - W: indicated that process is swapped out.
490 * - L: indicated non-zero l_holdcnt (i.e. that process was
491 * prevented from swapping-out.
493 * These letters should not be used for new states to avoid
494 * conflicts with old applications which might depend on them.
503 if (flag
& L_SINTR
) /* interruptable (long) */
504 *cp
= (int)k
->p_slptime
>= maxslp
? 'I' : 'S';
531 if (k
->p_nice
< NZERO
)
533 else if (k
->p_nice
> NZERO
)
537 if (flag
& P_WEXIT
&& !is_zombie
)
543 if (k
->p_eflag
& EPROC_SLEADER
)
547 else if (k
->p_nlwps
> 1)
549 if ((flag
& P_CONTROLT
) && k
->p__pgid
== k
->p_tpgid
)
552 strprintorsetwidth(v
, buf
, mode
);
556 lstate(void *arg
, VARENT
*ve
, int mode
)
577 if (flag
& L_SINTR
) /* interruptible (long) */
578 *cp
= (int)k
->l_slptime
>= maxslp
? 'I' : 'S';
603 if (flag
& L_DETACHED
)
606 strprintorsetwidth(v
, buf
, mode
);
610 pnice(void *arg
, VARENT
*ve
, int mode
)
612 struct kinfo_proc2
*k
;
617 intprintorsetwidth(v
, k
->p_nice
- NZERO
, mode
);
621 pri(void *arg
, VARENT
*ve
, int mode
)
628 intprintorsetwidth(v
, l
->l_priority
, mode
);
632 uname(void *arg
, VARENT
*ve
, int mode
)
634 struct kinfo_proc2
*k
;
639 strprintorsetwidth(v
, user_from_uid(k
->p_uid
, 0), mode
);
643 runame(void *arg
, VARENT
*ve
, int mode
)
645 struct kinfo_proc2
*k
;
650 strprintorsetwidth(v
, user_from_uid(k
->p_ruid
, 0), mode
);
654 svuname(void *arg
, VARENT
*ve
, int mode
)
656 struct kinfo_proc2
*k
;
661 strprintorsetwidth(v
, user_from_uid(k
->p_svuid
, 0), mode
);
665 gname(void *arg
, VARENT
*ve
, int mode
)
667 struct kinfo_proc2
*k
;
672 strprintorsetwidth(v
, group_from_gid(k
->p_gid
, 0), mode
);
676 rgname(void *arg
, VARENT
*ve
, int mode
)
678 struct kinfo_proc2
*k
;
683 strprintorsetwidth(v
, group_from_gid(k
->p_rgid
, 0), mode
);
687 svgname(void *arg
, VARENT
*ve
, int mode
)
689 struct kinfo_proc2
*k
;
694 strprintorsetwidth(v
, group_from_gid(k
->p_svgid
, 0), mode
);
698 tdev(void *arg
, VARENT
*ve
, int mode
)
700 struct kinfo_proc2
*k
;
709 if (mode
== PRINTMODE
)
710 (void)printf("%*s", v
->width
, "?");
715 (void)snprintf(buff
, sizeof(buff
),
716 "%lld/%lld", (long long)major(dev
), (long long)minor(dev
));
717 strprintorsetwidth(v
, buff
, mode
);
722 tname(void *arg
, VARENT
*ve
, int mode
)
724 struct kinfo_proc2
*k
;
733 if (dev
== NODEV
|| (ttname
= devname(dev
, S_IFCHR
)) == NULL
) {
734 if (mode
== PRINTMODE
)
735 (void)printf("%-*s", v
->width
, "?");
740 noctty
= !(k
->p_eflag
& EPROC_CTTY
) ? 1 : 0;
741 if (mode
== WIDTHMODE
) {
744 fmtlen
= strlen(ttname
) + noctty
;
745 if (v
->width
< fmtlen
)
749 (void)printf("%-*s-", v
->width
- 1, ttname
);
751 (void)printf("%-*s", v
->width
, ttname
);
757 longtname(void *arg
, VARENT
*ve
, int mode
)
759 struct kinfo_proc2
*k
;
767 if (dev
== NODEV
|| (ttname
= devname(dev
, S_IFCHR
)) == NULL
) {
768 if (mode
== PRINTMODE
)
769 (void)printf("%-*s", v
->width
, "?");
774 strprintorsetwidth(v
, ttname
, mode
);
779 started(void *arg
, VARENT
*ve
, int mode
)
781 struct kinfo_proc2
*k
;
790 if (mode
== PRINTMODE
)
791 (void)printf("%*s", v
->width
, "-");
795 startt
= k
->p_ustart_sec
;
796 tp
= localtime(&startt
);
799 if (now
- k
->p_ustart_sec
< SECSPERDAY
)
800 /* I *hate* SCCS... */
801 (void)strftime(buf
, sizeof(buf
) - 1, "%l:%" "M%p", tp
);
802 else if (now
- k
->p_ustart_sec
< DAYSPERWEEK
* SECSPERDAY
)
803 /* I *hate* SCCS... */
804 (void)strftime(buf
, sizeof(buf
) - 1, "%a%" "I%p", tp
);
806 (void)strftime(buf
, sizeof(buf
) - 1, "%e%b%y", tp
);
807 /* %e and %l can start with a space. */
811 strprintorsetwidth(v
, cp
, mode
);
815 lstarted(void *arg
, VARENT
*ve
, int mode
)
817 struct kinfo_proc2
*k
;
826 * Minimum width is less than header - we don't
827 * need to check it every time.
829 if (mode
== PRINTMODE
)
830 (void)printf("%*s", v
->width
, "-");
833 startt
= k
->p_ustart_sec
;
835 /* assume all times are the same length */
836 if (mode
!= WIDTHMODE
|| v
->width
== 0) {
837 (void)strftime(buf
, sizeof(buf
) -1, "%c",
839 strprintorsetwidth(v
, buf
, mode
);
844 elapsed(void *arg
, VARENT
*ve
, int mode
)
846 struct kinfo_proc2
*k
;
848 int32_t origseconds
, secs
, mins
, hours
, days
;
849 int fmtlen
, printed_something
;
853 if (k
->p_uvalid
== 0) {
858 origseconds
= now
- k
->p_ustart_sec
;
859 if (origseconds
< 0) {
861 * Don't try to be fancy if the machine's
862 * clock has been rewound to before the
870 mins
= secs
/ SECSPERMIN
;
872 hours
= mins
/ MINSPERHOUR
;
874 days
= hours
/ HOURSPERDAY
;
875 hours
%= HOURSPERDAY
;
877 if (mode
== WIDTHMODE
) {
878 if (origseconds
== 0)
879 /* non-zero so fmtlen is calculated at least once */
882 if (origseconds
> v
->longestp
) {
883 v
->longestp
= origseconds
;
886 /* +9 for "-hh:mm:ss" */
887 fmtlen
= iwidth(days
) + 9;
888 } else if (hours
> 0) {
890 fmtlen
= iwidth(hours
) + 6;
893 fmtlen
= iwidth(mins
) + 3;
896 if (fmtlen
> v
->width
)
900 printed_something
= 0;
904 (void)printf("%*d", fmtlen
- 9, days
);
905 printed_something
= 1;
906 } else if (fmtlen
> 9) {
907 (void)printf("%*s", fmtlen
- 9, "");
912 if (printed_something
) {
913 (void)printf("-%.*d", fmtlen
- 7, hours
);
914 printed_something
= 1;
915 } else if (hours
> 0) {
916 (void)printf("%*d", fmtlen
- 6, hours
);
917 printed_something
= 1;
918 } else if (fmtlen
> 6) {
919 (void)printf("%*s", fmtlen
- 6, "");
924 /* Don't need to set fmtlen or printed_something any more... */
925 if (printed_something
) {
926 (void)printf(":%.*d", fmtlen
- 4, mins
);
927 } else if (mins
> 0) {
928 (void)printf("%*d", fmtlen
- 3, mins
);
929 } else if (fmtlen
> 3) {
930 (void)printf("%*s", fmtlen
- 3, "0");
933 (void)printf(":%.2d", secs
);
938 wchan(void *arg
, VARENT
*ve
, int mode
)
948 strprintorsetwidth(v
, l
->l_wmesg
, mode
);
949 v
->width
= min(v
->width
, KI_WMESGLEN
);
951 (void)asprintf(&buf
, "%-*" PRIx64
, v
->width
,
955 strprintorsetwidth(v
, buf
, mode
);
956 v
->width
= min(v
->width
, KI_WMESGLEN
);
960 if (mode
== PRINTMODE
)
961 (void)printf("%-*s", v
->width
, "-");
965 #define pgtok(a) (((a)*getpagesize())/1024)
968 vsize(void *arg
, VARENT
*ve
, int mode
)
970 struct kinfo_proc2
*k
;
975 intprintorsetwidth(v
, pgtok(k
->p_vm_msize
), mode
);
979 rssize(void *arg
, VARENT
*ve
, int mode
)
981 struct kinfo_proc2
*k
;
986 /* XXX don't have info about shared */
987 intprintorsetwidth(v
, pgtok(k
->p_vm_rssize
), mode
);
991 p_rssize(void *arg
, VARENT
*ve
, int mode
) /* doesn't account for text */
993 struct kinfo_proc2
*k
;
998 intprintorsetwidth(v
, pgtok(k
->p_vm_rssize
), mode
);
1009 size
= sizeof(ncpu
);
1010 if (sysctl(mib
, 2, &ncpu
, &size
, NULL
, 0) == -1) {
1014 cp_id
= malloc(sizeof(cp_id
[0]) * ncpu
);
1018 mib
[1] = KERN_CP_ID
;
1019 size
= sizeof(cp_id
[0]) * ncpu
;
1020 if (sysctl(mib
, 2, cp_id
, &size
, NULL
, 0) == -1)
1025 get_cpunum(u_int64_t id
)
1028 for (i
= 0; i
< ncpu
; i
++)
1035 cpuid(void *arg
, VARENT
*ve
, int mode
)
1037 struct kinfo_lwp
*l
;
1042 intprintorsetwidth(v
, get_cpunum(l
->l_cpuid
), mode
);
1046 cputime(void *arg
, VARENT
*ve
, int mode
)
1048 struct kinfo_proc2
*k
;
1051 int32_t psecs
; /* "parts" of a second. first micro, then centi */
1058 * This counts time spent handling interrupts. We could
1059 * fix this, but it is not 100% trivial (and interrupt
1060 * time fractions only work on the sparc anyway). XXX
1062 secs
= k
->p_rtime_sec
;
1063 psecs
= k
->p_rtime_usec
;
1065 secs
+= k
->p_uctime_sec
;
1066 psecs
+= k
->p_uctime_usec
;
1069 * round and scale to 100's
1071 psecs
= (psecs
+ 5000) / 10000;
1072 secs
+= psecs
/ 100;
1073 psecs
= psecs
% 100;
1075 if (mode
== WIDTHMODE
) {
1077 * Ugg, this is the only field where a value of 0 is longer
1078 * than the column title.
1079 * Use SECSPERMIN, because secs is divided by that when
1080 * passed to iwidth().
1085 if (secs
> v
->longestp
) {
1087 /* "+6" for the ":%02ld.%02ld" in the printf() below */
1088 fmtlen
= iwidth(secs
/ SECSPERMIN
) + 6;
1089 if (fmtlen
> v
->width
)
1093 (void)printf("%*ld:%02ld.%02ld", v
->width
- 6,
1094 (long)(secs
/ SECSPERMIN
), (long)(secs
% SECSPERMIN
),
1101 const struct kinfo_proc2
*k
;
1106 failure
= (kd
) ? donlist() : 1;
1110 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
1112 if (k
->p_swtime
== 0 || k
->p_realstat
== SZOMB
)
1115 return (100.0 * fxtofl(k
->p_pctcpu
));
1116 return (100.0 * fxtofl(k
->p_pctcpu
) /
1117 (1.0 - exp(k
->p_swtime
* log(ccpu
))));
1121 pcpu(void *arg
, VARENT
*ve
, int mode
)
1123 struct kinfo_proc2
*k
;
1128 doubleprintorsetwidth(v
, getpcpu(k
), 1, mode
);
1133 const struct kinfo_proc2
*k
;
1140 failure
= (kd
) ? donlist() : 1;
1144 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
1145 szptudot
= uspace
/getpagesize();
1146 /* XXX don't have info about shared */
1147 fracmem
= ((float)k
->p_vm_rssize
+ szptudot
)/mempages
;
1148 return (100.0 * fracmem
);
1152 pmem(void *arg
, VARENT
*ve
, int mode
)
1154 struct kinfo_proc2
*k
;
1159 doubleprintorsetwidth(v
, getpmem(k
), 1, mode
);
1163 pagein(void *arg
, VARENT
*ve
, int mode
)
1165 struct kinfo_proc2
*k
;
1170 intprintorsetwidth(v
, k
->p_uvalid
? k
->p_uru_majflt
: 0, mode
);
1174 maxrss(void *arg
, VARENT
*ve
, int mode
)
1179 /* No need to check width! */
1180 if (mode
== PRINTMODE
)
1181 (void)printf("%*s", v
->width
, "-");
1185 tsize(void *arg
, VARENT
*ve
, int mode
)
1187 struct kinfo_proc2
*k
;
1192 intprintorsetwidth(v
, pgtok(k
->p_vm_tsize
), mode
);
1196 * Generic output routines. Print fields from various prototype
1200 printval(bp
, v
, mode
)
1205 static char ofmt
[32] = "%";
1206 int width
, vok
, fmtlen
;
1212 val
= 0; /* XXXGCC -Wuninitialized [hpcarm] */
1213 uval
= 0; /* XXXGCC -Wuninitialized [hpcarm] */
1216 * Note that the "INF127" check is nonsensical for types
1217 * that are or can be signed.
1219 #define GET(type) (*(type *)bp)
1220 #define CHK_INF127(n) (((n) > 127) && (v->flag & INF127) ? 127 : (n))
1226 if (mode
== WIDTHMODE
) {
1234 uval
= CHK_INF127(GET(u_char
));
1242 uval
= CHK_INF127(GET(u_short
));
1255 uval
= CHK_INF127(GET(u_int
));
1263 uval
= CHK_INF127(GET(u_long
));
1267 uval
= GET(u_int64_t
);
1271 uval
= GET(u_int64_t
);
1280 uval
= CHK_INF127(GET(u_int64_t
));
1290 if (val
< 0 && val
< v
->longestn
) {
1292 fmtlen
= iwidth(-val
) + 1;
1293 if (fmtlen
> v
->width
)
1295 } else if (val
> 0 && val
> v
->longestp
) {
1297 fmtlen
= iwidth(val
);
1298 if (fmtlen
> v
->width
)
1303 if (uval
> v
->longestu
) {
1305 v
->width
= iwidth(uval
);
1314 if (fmtlen
> v
->width
)
1323 if (v
->flag
& LJUST
)
1326 while ((*cp
++ = *fcp
++) != '\0')
1331 (void)printf(ofmt
, width
, GET(char));
1334 (void)printf(ofmt
, width
, CHK_INF127(GET(u_char
)));
1337 (void)printf(ofmt
, width
, GET(short));
1340 (void)printf(ofmt
, width
, CHK_INF127(GET(u_short
)));
1343 (void)printf(ofmt
, width
, GET(int));
1346 (void)printf(ofmt
, width
, CHK_INF127(GET(u_int
)));
1349 (void)printf(ofmt
, width
, GET(long));
1352 (void)printf(ofmt
, width
, CHK_INF127(GET(u_long
)));
1355 (void)printf(ofmt
, width
, GET(u_int64_t
));
1358 (void)printf(ofmt
, width
, GET(u_int64_t
) & 0xffffff);
1361 (void)printf(ofmt
, width
, GET(int32_t));
1364 (void)printf(ofmt
, width
, CHK_INF127(GET(u_int32_t
)));
1368 sigset_t
*s
= (sigset_t
*)(void *)bp
;
1370 #define SIGSETSIZE (sizeof(s->__bits) / sizeof(s->__bits[0]))
1371 char buf
[SIGSETSIZE
* 8 + 1];
1373 for (i
= 0; i
< SIGSETSIZE
; i
++)
1374 (void)snprintf(&buf
[i
* 8], 9, "%.8x",
1375 s
->__bits
[(SIGSETSIZE
- 1) - i
]);
1377 /* Skip leading zeroes */
1378 for (i
= 0; buf
[i
] == '0'; i
++)
1383 strprintorsetwidth(v
, buf
+ i
, mode
);
1388 (void)printf(ofmt
, width
, GET(int64_t));
1391 (void)printf(ofmt
, width
, CHK_INF127(GET(u_int64_t
)));
1394 errx(1, "unknown type %d", v
->type
);
1401 pvar(void *arg
, VARENT
*ve
, int mode
)
1406 if (v
->flag
& UAREA
&& !((struct kinfo_proc2
*)arg
)->p_uvalid
) {
1407 if (mode
== PRINTMODE
)
1408 (void)printf("%*s", v
->width
, "-");
1412 (void)printval((char *)arg
+ v
->off
, v
, mode
);
1416 putimeval(void *arg
, VARENT
*ve
, int mode
)
1419 struct kinfo_proc2
*k
= arg
;
1420 ulong secs
= *(uint32_t *)((char *)arg
+ v
->off
);
1421 ulong usec
= *(uint32_t *)((char *)arg
+ v
->off
+ sizeof (uint32_t));
1425 if (mode
== PRINTMODE
)
1426 (void)printf("%*s", v
->width
, "-");
1430 if (mode
== WIDTHMODE
) {
1432 /* non-zero so fmtlen is calculated at least once */
1434 if (secs
> v
->longestu
) {
1438 fmtlen
= iwidth(secs
) + 6 + 1;
1441 fmtlen
= iwidth((secs
+ 1) / SECSPERHOUR
)
1442 + 2 + 1 + 2 + 1 + 2 + 1;
1443 if (fmtlen
> v
->width
)
1450 (void)printf( "%*lu.%.6lu", v
->width
- 6 - 1, secs
, usec
);
1454 if (usec
>= 1000000) {
1458 m
= secs
/ SECSPERMIN
;
1459 secs
-= m
* SECSPERMIN
;
1460 h
= m
/ MINSPERHOUR
;
1461 m
-= h
* MINSPERHOUR
;
1462 (void)printf( "%*u:%.2u:%.2lu.%.2lu", v
->width
- 9, h
, m
, secs
,
1468 lname(void *arg
, VARENT
*ve
, int mode
)
1470 struct kinfo_lwp
*l
;
1475 if (l
->l_name
&& l
->l_name
[0] != '\0') {
1476 strprintorsetwidth(v
, l
->l_name
, mode
);
1477 v
->width
= min(v
->width
, KI_LNAMELEN
);
1479 if (mode
== PRINTMODE
)
1480 (void)printf("%-*s", v
->width
, "-");