print.c revision 1.134 1 1.134 christos /* $NetBSD: print.c,v 1.134 2021/04/06 13:35:52 christos Exp $ */
2 1.18 cgd
3 1.55 simonb /*
4 1.98 ad * Copyright (c) 2000, 2007 The NetBSD Foundation, Inc.
5 1.55 simonb * All rights reserved.
6 1.55 simonb *
7 1.55 simonb * This code is derived from software contributed to The NetBSD Foundation
8 1.55 simonb * by Simon Burge.
9 1.55 simonb *
10 1.55 simonb * Redistribution and use in source and binary forms, with or without
11 1.55 simonb * modification, are permitted provided that the following conditions
12 1.55 simonb * are met:
13 1.55 simonb * 1. Redistributions of source code must retain the above copyright
14 1.55 simonb * notice, this list of conditions and the following disclaimer.
15 1.55 simonb * 2. Redistributions in binary form must reproduce the above copyright
16 1.55 simonb * notice, this list of conditions and the following disclaimer in the
17 1.55 simonb * documentation and/or other materials provided with the distribution.
18 1.55 simonb *
19 1.55 simonb * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.55 simonb * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.55 simonb * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.55 simonb * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.55 simonb * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.55 simonb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.55 simonb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.55 simonb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.55 simonb * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.55 simonb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.55 simonb * POSSIBILITY OF SUCH DAMAGE.
30 1.55 simonb */
31 1.55 simonb
32 1.55 simonb /*
33 1.12 cgd * Copyright (c) 1990, 1993, 1994
34 1.12 cgd * The Regents of the University of California. All rights reserved.
35 1.1 cgd *
36 1.1 cgd * Redistribution and use in source and binary forms, with or without
37 1.1 cgd * modification, are permitted provided that the following conditions
38 1.1 cgd * are met:
39 1.1 cgd * 1. Redistributions of source code must retain the above copyright
40 1.1 cgd * notice, this list of conditions and the following disclaimer.
41 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 cgd * notice, this list of conditions and the following disclaimer in the
43 1.1 cgd * documentation and/or other materials provided with the distribution.
44 1.79 agc * 3. Neither the name of the University nor the names of its contributors
45 1.1 cgd * may be used to endorse or promote products derived from this software
46 1.1 cgd * without specific prior written permission.
47 1.1 cgd *
48 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 1.1 cgd * SUCH DAMAGE.
59 1.1 cgd */
60 1.1 cgd
61 1.33 christos #include <sys/cdefs.h>
62 1.1 cgd #ifndef lint
63 1.18 cgd #if 0
64 1.12 cgd static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94";
65 1.18 cgd #else
66 1.134 christos __RCSID("$NetBSD: print.c,v 1.134 2021/04/06 13:35:52 christos Exp $");
67 1.18 cgd #endif
68 1.1 cgd #endif /* not lint */
69 1.1 cgd
70 1.1 cgd #include <sys/param.h>
71 1.1 cgd #include <sys/time.h>
72 1.1 cgd #include <sys/resource.h>
73 1.76 thorpej #include <sys/lwp.h>
74 1.1 cgd #include <sys/proc.h>
75 1.1 cgd #include <sys/stat.h>
76 1.12 cgd #include <sys/ucred.h>
77 1.12 cgd #include <sys/sysctl.h>
78 1.1 cgd
79 1.12 cgd #include <err.h>
80 1.77 atatat #include <grp.h>
81 1.21 mycroft #include <kvm.h>
82 1.12 cgd #include <math.h>
83 1.12 cgd #include <nlist.h>
84 1.28 explorer #include <pwd.h>
85 1.12 cgd #include <stddef.h>
86 1.12 cgd #include <stdio.h>
87 1.12 cgd #include <stdlib.h>
88 1.12 cgd #include <string.h>
89 1.37 kleink #include <time.h>
90 1.12 cgd #include <tzfile.h>
91 1.16 cgd #include <unistd.h>
92 1.12 cgd
93 1.12 cgd #include "ps.h"
94 1.21 mycroft
95 1.82 simonb static char *cmdpart(char *);
96 1.122 dholland static void printval(void *, VAR *, enum mode);
97 1.82 simonb static int titlecmp(char *, char **);
98 1.82 simonb
99 1.122 dholland static void doubleprintorsetwidth(VAR *, double, int, enum mode);
100 1.122 dholland static void intprintorsetwidth(VAR *, int, enum mode);
101 1.122 dholland static void strprintorsetwidth(VAR *, const char *, enum mode);
102 1.53 simonb
103 1.87 simonb static time_t now;
104 1.87 simonb
105 1.32 mycroft #define min(a,b) ((a) <= (b) ? (a) : (b))
106 1.78 dsl
107 1.132 kamil /* pre-NetBSD 5.x support. */
108 1.132 kamil #ifndef LSDEAD
109 1.132 kamil #define LSDEAD 6
110 1.132 kamil #endif
111 1.132 kamil
112 1.134 christos static void __attribute__((__format__(__strftime__, 3, 0)))
113 1.134 christos safe_strftime(char *buf, size_t bufsiz, const char *fmt,
114 1.134 christos const struct tm *tp)
115 1.134 christos {
116 1.134 christos if (tp == NULL || strftime(buf, bufsiz, fmt, tp) == 0)
117 1.134 christos strlcpy(buf, "-", sizeof(buf));
118 1.134 christos }
119 1.134 christos
120 1.78 dsl static int
121 1.78 dsl iwidth(u_int64_t v)
122 1.78 dsl {
123 1.78 dsl u_int64_t nlim, lim;
124 1.78 dsl int w = 1;
125 1.78 dsl
126 1.78 dsl for (lim = 10; v >= lim; lim = nlim) {
127 1.78 dsl nlim = lim * 10;
128 1.78 dsl w++;
129 1.78 dsl if (nlim < lim)
130 1.78 dsl break;
131 1.78 dsl }
132 1.78 dsl return w;
133 1.78 dsl }
134 1.32 mycroft
135 1.21 mycroft static char *
136 1.82 simonb cmdpart(char *arg0)
137 1.21 mycroft {
138 1.21 mycroft char *cp;
139 1.21 mycroft
140 1.21 mycroft return ((cp = strrchr(arg0, '/')) != NULL ? cp + 1 : arg0);
141 1.21 mycroft }
142 1.21 mycroft
143 1.12 cgd void
144 1.82 simonb printheader(void)
145 1.1 cgd {
146 1.53 simonb int len;
147 1.12 cgd VAR *v;
148 1.12 cgd struct varent *vent;
149 1.53 simonb static int firsttime = 1;
150 1.95 apb static int noheader = 0;
151 1.1 cgd
152 1.95 apb /*
153 1.95 apb * If all the columns have user-specified null headers,
154 1.95 apb * don't print the blank header line at all.
155 1.95 apb */
156 1.95 apb if (firsttime) {
157 1.95 apb SIMPLEQ_FOREACH(vent, &displaylist, next) {
158 1.95 apb if (vent->var->header[0])
159 1.95 apb break;
160 1.95 apb }
161 1.95 apb if (vent == NULL) {
162 1.95 apb noheader = 1;
163 1.95 apb firsttime = 0;
164 1.95 apb }
165 1.95 apb
166 1.95 apb }
167 1.95 apb if (noheader)
168 1.95 apb return;
169 1.95 apb
170 1.95 apb SIMPLEQ_FOREACH(vent, &displaylist, next) {
171 1.1 cgd v = vent->var;
172 1.55 simonb if (firsttime) {
173 1.53 simonb len = strlen(v->header);
174 1.53 simonb if (len > v->width)
175 1.53 simonb v->width = len;
176 1.53 simonb totwidth += v->width + 1; /* +1 for space */
177 1.53 simonb }
178 1.1 cgd if (v->flag & LJUST) {
179 1.95 apb if (SIMPLEQ_NEXT(vent, next) == NULL) /* last one */
180 1.12 cgd (void)printf("%s", v->header);
181 1.1 cgd else
182 1.53 simonb (void)printf("%-*s", v->width,
183 1.53 simonb v->header);
184 1.1 cgd } else
185 1.12 cgd (void)printf("%*s", v->width, v->header);
186 1.95 apb if (SIMPLEQ_NEXT(vent, next) != NULL)
187 1.12 cgd (void)putchar(' ');
188 1.1 cgd }
189 1.12 cgd (void)putchar('\n');
190 1.55 simonb if (firsttime) {
191 1.53 simonb firsttime = 0;
192 1.55 simonb totwidth--; /* take off last space */
193 1.55 simonb }
194 1.21 mycroft }
195 1.21 mycroft
196 1.82 simonb /*
197 1.90 simonb * Return 1 if the command name in the argument vector (u-area) does
198 1.65 christos * not match the command name (p_comm)
199 1.63 hubertf */
200 1.39 mycroft static int
201 1.82 simonb titlecmp(char *name, char **argv)
202 1.39 mycroft {
203 1.39 mycroft char *title;
204 1.39 mycroft int namelen;
205 1.39 mycroft
206 1.63 hubertf
207 1.64 christos /* no argument vector == no match; system processes/threads do that */
208 1.39 mycroft if (argv == 0 || argv[0] == 0)
209 1.39 mycroft return (1);
210 1.39 mycroft
211 1.39 mycroft title = cmdpart(argv[0]);
212 1.39 mycroft
213 1.64 christos /* the basename matches */
214 1.39 mycroft if (!strcmp(name, title))
215 1.39 mycroft return (0);
216 1.39 mycroft
217 1.64 christos /* handle login shells, by skipping the leading - */
218 1.84 simonb if (title[0] == '-' && !strcmp(name, title + 1))
219 1.39 mycroft return (0);
220 1.39 mycroft
221 1.39 mycroft namelen = strlen(name);
222 1.39 mycroft
223 1.64 christos /* handle daemons that report activity as daemonname: activity */
224 1.39 mycroft if (argv[1] == 0 &&
225 1.39 mycroft !strncmp(name, title, namelen) &&
226 1.55 simonb title[namelen + 0] == ':' &&
227 1.55 simonb title[namelen + 1] == ' ')
228 1.39 mycroft return (0);
229 1.39 mycroft
230 1.39 mycroft return (1);
231 1.39 mycroft }
232 1.39 mycroft
233 1.53 simonb static void
234 1.122 dholland doubleprintorsetwidth(VAR *v, double val, int prec, enum mode mode)
235 1.53 simonb {
236 1.53 simonb int fmtlen;
237 1.53 simonb
238 1.53 simonb if (mode == WIDTHMODE) {
239 1.53 simonb if (val < 0.0 && val < v->longestnd) {
240 1.53 simonb fmtlen = (int)log10(-val) + prec + 2;
241 1.53 simonb v->longestnd = val;
242 1.53 simonb if (fmtlen > v->width)
243 1.53 simonb v->width = fmtlen;
244 1.53 simonb } else if (val > 0.0 && val > v->longestpd) {
245 1.53 simonb fmtlen = (int)log10(val) + prec + 1;
246 1.53 simonb v->longestpd = val;
247 1.53 simonb if (fmtlen > v->width)
248 1.53 simonb v->width = fmtlen;
249 1.53 simonb }
250 1.53 simonb } else {
251 1.84 simonb (void)printf("%*.*f", v->width, prec, val);
252 1.53 simonb }
253 1.53 simonb }
254 1.53 simonb
255 1.53 simonb static void
256 1.122 dholland intprintorsetwidth(VAR *v, int val, enum mode mode)
257 1.53 simonb {
258 1.53 simonb int fmtlen;
259 1.53 simonb
260 1.53 simonb if (mode == WIDTHMODE) {
261 1.53 simonb if (val < 0 && val < v->longestn) {
262 1.53 simonb v->longestn = val;
263 1.78 dsl fmtlen = iwidth(-val) + 1;
264 1.53 simonb if (fmtlen > v->width)
265 1.53 simonb v->width = fmtlen;
266 1.53 simonb } else if (val > 0 && val > v->longestp) {
267 1.53 simonb v->longestp = val;
268 1.78 dsl fmtlen = iwidth(val);
269 1.53 simonb if (fmtlen > v->width)
270 1.53 simonb v->width = fmtlen;
271 1.53 simonb }
272 1.53 simonb } else
273 1.84 simonb (void)printf("%*d", v->width, val);
274 1.53 simonb }
275 1.53 simonb
276 1.53 simonb static void
277 1.122 dholland strprintorsetwidth(VAR *v, const char *str, enum mode mode)
278 1.53 simonb {
279 1.53 simonb int len;
280 1.53 simonb
281 1.53 simonb if (mode == WIDTHMODE) {
282 1.53 simonb len = strlen(str);
283 1.53 simonb if (len > v->width)
284 1.53 simonb v->width = len;
285 1.53 simonb } else {
286 1.53 simonb if (v->flag & LJUST)
287 1.84 simonb (void)printf("%-*.*s", v->width, v->width, str);
288 1.53 simonb else
289 1.84 simonb (void)printf("%*.*s", v->width, v->width, str);
290 1.53 simonb }
291 1.53 simonb }
292 1.53 simonb
293 1.12 cgd void
294 1.126 christos command(struct pinfo *pi, VARENT *ve, enum mode mode)
295 1.12 cgd {
296 1.126 christos struct kinfo_proc2 *ki = pi->ki;
297 1.1 cgd VAR *v;
298 1.12 cgd int left;
299 1.39 mycroft char **argv, **p, *name;
300 1.7 cgd
301 1.55 simonb if (mode == WIDTHMODE)
302 1.53 simonb return;
303 1.55 simonb
304 1.53 simonb v = ve->var;
305 1.95 apb if (SIMPLEQ_NEXT(ve, next) != NULL || termwidth != UNLIMITED) {
306 1.95 apb if (SIMPLEQ_NEXT(ve, next) == NULL) {
307 1.55 simonb left = termwidth - (totwidth - v->width);
308 1.1 cgd if (left < 1) /* already wrapped, just use std width */
309 1.1 cgd left = v->width;
310 1.21 mycroft } else
311 1.21 mycroft left = v->width;
312 1.21 mycroft } else
313 1.21 mycroft left = -1;
314 1.51 simonb if (needenv && kd) {
315 1.51 simonb argv = kvm_getenvv2(kd, ki, termwidth);
316 1.33 christos if ((p = argv) != NULL) {
317 1.21 mycroft while (*p) {
318 1.21 mycroft fmt_puts(*p, &left);
319 1.21 mycroft p++;
320 1.21 mycroft fmt_putc(' ', &left);
321 1.4 cgd }
322 1.1 cgd }
323 1.21 mycroft }
324 1.21 mycroft if (needcomm) {
325 1.126 christos if (pi->prefix)
326 1.126 christos (void)fmt_puts(pi->prefix, &left);
327 1.51 simonb name = ki->p_comm;
328 1.21 mycroft if (!commandonly) {
329 1.74 jdolecek argv = kvm_getargv2(kd, ki, termwidth);
330 1.33 christos if ((p = argv) != NULL) {
331 1.21 mycroft while (*p) {
332 1.21 mycroft fmt_puts(*p, &left);
333 1.21 mycroft p++;
334 1.21 mycroft fmt_putc(' ', &left);
335 1.88 simonb if (v->flag & ARGV0)
336 1.88 simonb break;
337 1.21 mycroft }
338 1.88 simonb if (!(v->flag & ARGV0) &&
339 1.88 simonb titlecmp(name, argv)) {
340 1.67 christos /*
341 1.67 christos * append the real command name within
342 1.82 simonb * parentheses, if the command name
343 1.67 christos * does not match the one in the
344 1.67 christos * argument vector
345 1.67 christos */
346 1.67 christos fmt_putc('(', &left);
347 1.67 christos fmt_puts(name, &left);
348 1.67 christos fmt_putc(')', &left);
349 1.67 christos }
350 1.67 christos } else {
351 1.67 christos /*
352 1.67 christos * Commands that don't set an argv vector
353 1.69 lukem * are printed with square brackets if they
354 1.68 enami * are system commands. Otherwise they are
355 1.68 enami * printed within parentheses.
356 1.67 christos */
357 1.99 pavel if (ki->p_flag & P_SYSTEM) {
358 1.68 enami fmt_putc('[', &left);
359 1.68 enami fmt_puts(name, &left);
360 1.68 enami fmt_putc(']', &left);
361 1.68 enami } else {
362 1.68 enami fmt_putc('(', &left);
363 1.68 enami fmt_puts(name, &left);
364 1.68 enami fmt_putc(')', &left);
365 1.68 enami }
366 1.45 jdolecek }
367 1.21 mycroft } else {
368 1.39 mycroft fmt_puts(name, &left);
369 1.21 mycroft }
370 1.21 mycroft }
371 1.95 apb if (SIMPLEQ_NEXT(ve, next) != NULL && left > 0)
372 1.84 simonb (void)printf("%*s", left, "");
373 1.1 cgd }
374 1.1 cgd
375 1.12 cgd void
376 1.126 christos groups(struct pinfo *pi, VARENT *ve, enum mode mode)
377 1.77 atatat {
378 1.126 christos struct kinfo_proc2 *ki = pi->ki;
379 1.77 atatat VAR *v;
380 1.77 atatat int left, i;
381 1.77 atatat char buf[16], *p;
382 1.77 atatat
383 1.77 atatat if (mode == WIDTHMODE)
384 1.77 atatat return;
385 1.77 atatat
386 1.77 atatat v = ve->var;
387 1.95 apb if (SIMPLEQ_NEXT(ve, next) != NULL || termwidth != UNLIMITED) {
388 1.95 apb if (SIMPLEQ_NEXT(ve, next) == NULL) {
389 1.77 atatat left = termwidth - (totwidth - v->width);
390 1.77 atatat if (left < 1) /* already wrapped, just use std width */
391 1.77 atatat left = v->width;
392 1.77 atatat } else
393 1.77 atatat left = v->width;
394 1.77 atatat } else
395 1.77 atatat left = -1;
396 1.77 atatat
397 1.108 drochner if (ki->p_ngroups == 0)
398 1.77 atatat fmt_putc('-', &left);
399 1.77 atatat
400 1.77 atatat for (i = 0; i < ki->p_ngroups; i++) {
401 1.77 atatat (void)snprintf(buf, sizeof(buf), "%d", ki->p_groups[i]);
402 1.77 atatat if (i)
403 1.77 atatat fmt_putc(' ', &left);
404 1.77 atatat for (p = &buf[0]; *p; p++)
405 1.77 atatat fmt_putc(*p, &left);
406 1.77 atatat }
407 1.77 atatat
408 1.95 apb if (SIMPLEQ_NEXT(ve, next) != NULL && left > 0)
409 1.84 simonb (void)printf("%*s", left, "");
410 1.77 atatat }
411 1.77 atatat
412 1.77 atatat void
413 1.126 christos groupnames(struct pinfo *pi, VARENT *ve, enum mode mode)
414 1.77 atatat {
415 1.126 christos struct kinfo_proc2 *ki = pi->ki;
416 1.77 atatat VAR *v;
417 1.77 atatat int left, i;
418 1.77 atatat const char *p;
419 1.77 atatat
420 1.77 atatat if (mode == WIDTHMODE)
421 1.77 atatat return;
422 1.77 atatat
423 1.77 atatat v = ve->var;
424 1.95 apb if (SIMPLEQ_NEXT(ve, next) != NULL || termwidth != UNLIMITED) {
425 1.95 apb if (SIMPLEQ_NEXT(ve, next) == NULL) {
426 1.77 atatat left = termwidth - (totwidth - v->width);
427 1.77 atatat if (left < 1) /* already wrapped, just use std width */
428 1.77 atatat left = v->width;
429 1.77 atatat } else
430 1.77 atatat left = v->width;
431 1.77 atatat } else
432 1.77 atatat left = -1;
433 1.77 atatat
434 1.109 drochner if (ki->p_ngroups == 0)
435 1.77 atatat fmt_putc('-', &left);
436 1.77 atatat
437 1.77 atatat for (i = 0; i < ki->p_ngroups; i++) {
438 1.77 atatat if (i)
439 1.77 atatat fmt_putc(' ', &left);
440 1.77 atatat for (p = group_from_gid(ki->p_groups[i], 0); *p; p++)
441 1.77 atatat fmt_putc(*p, &left);
442 1.77 atatat }
443 1.77 atatat
444 1.95 apb if (SIMPLEQ_NEXT(ve, next) != NULL && left > 0)
445 1.84 simonb (void)printf("%*s", left, "");
446 1.77 atatat }
447 1.77 atatat
448 1.77 atatat void
449 1.126 christos ucomm(struct pinfo *pi, VARENT *ve, enum mode mode)
450 1.12 cgd {
451 1.126 christos struct kinfo_proc2 *k = pi->ki;
452 1.126 christos char buf[MAXPATHLEN], *p;
453 1.1 cgd VAR *v;
454 1.12 cgd
455 1.12 cgd v = ve->var;
456 1.126 christos if (pi->prefix)
457 1.126 christos snprintf(p = buf, sizeof(buf), "%s%s", pi->prefix, k->p_comm);
458 1.126 christos else
459 1.126 christos p = k->p_comm;
460 1.126 christos strprintorsetwidth(v, p, mode);
461 1.1 cgd }
462 1.1 cgd
463 1.12 cgd void
464 1.126 christos emul(struct pinfo *pi, VARENT *ve, enum mode mode)
465 1.96 christos {
466 1.126 christos struct kinfo_proc2 *k = pi->ki;
467 1.96 christos VAR *v;
468 1.96 christos
469 1.96 christos v = ve->var;
470 1.96 christos strprintorsetwidth(v, k->p_ename, mode);
471 1.96 christos }
472 1.96 christos
473 1.96 christos void
474 1.126 christos logname(struct pinfo *pi, VARENT *ve, enum mode mode)
475 1.12 cgd {
476 1.126 christos struct kinfo_proc2 *k = pi->ki;
477 1.1 cgd VAR *v;
478 1.12 cgd
479 1.12 cgd v = ve->var;
480 1.53 simonb strprintorsetwidth(v, k->p_login, mode);
481 1.1 cgd }
482 1.1 cgd
483 1.12 cgd void
484 1.126 christos state(struct pinfo *pi, VARENT *ve, enum mode mode)
485 1.12 cgd {
486 1.126 christos struct kinfo_proc2 *k = pi->ki;
487 1.51 simonb int flag, is_zombie;
488 1.12 cgd char *cp;
489 1.1 cgd VAR *v;
490 1.1 cgd char buf[16];
491 1.12 cgd
492 1.51 simonb is_zombie = 0;
493 1.12 cgd v = ve->var;
494 1.51 simonb flag = k->p_flag;
495 1.12 cgd cp = buf;
496 1.1 cgd
497 1.112 rmind /*
498 1.112 rmind * NOTE: There are historical letters, which are no longer used:
499 1.112 rmind *
500 1.112 rmind * - W: indicated that process is swapped out.
501 1.112 rmind * - L: indicated non-zero l_holdcnt (i.e. that process was
502 1.112 rmind * prevented from swapping-out.
503 1.112 rmind *
504 1.112 rmind * These letters should not be used for new states to avoid
505 1.112 rmind * conflicts with old applications which might depend on them.
506 1.112 rmind */
507 1.51 simonb switch (k->p_stat) {
508 1.1 cgd
509 1.76 thorpej case LSSTOP:
510 1.1 cgd *cp = 'T';
511 1.1 cgd break;
512 1.1 cgd
513 1.76 thorpej case LSSLEEP:
514 1.99 pavel if (flag & L_SINTR) /* interruptable (long) */
515 1.110 lukem *cp = (int)k->p_slptime >= maxslp ? 'I' : 'S';
516 1.1 cgd else
517 1.10 cgd *cp = 'D';
518 1.1 cgd break;
519 1.1 cgd
520 1.76 thorpej case LSRUN:
521 1.76 thorpej case LSIDL:
522 1.106 christos *cp = 'R';
523 1.106 christos break;
524 1.106 christos
525 1.76 thorpej case LSONPROC:
526 1.106 christos *cp = 'O';
527 1.1 cgd break;
528 1.1 cgd
529 1.76 thorpej case LSZOMB:
530 1.1 cgd *cp = 'Z';
531 1.51 simonb is_zombie = 1;
532 1.1 cgd break;
533 1.1 cgd
534 1.76 thorpej case LSSUSPENDED:
535 1.76 thorpej *cp = 'U';
536 1.76 thorpej break;
537 1.76 thorpej
538 1.1 cgd default:
539 1.1 cgd *cp = '?';
540 1.1 cgd }
541 1.1 cgd cp++;
542 1.51 simonb if (k->p_nice < NZERO)
543 1.1 cgd *cp++ = '<';
544 1.51 simonb else if (k->p_nice > NZERO)
545 1.1 cgd *cp++ = 'N';
546 1.99 pavel if (flag & P_TRACED)
547 1.1 cgd *cp++ = 'X';
548 1.99 pavel if (flag & P_WEXIT && !is_zombie)
549 1.1 cgd *cp++ = 'E';
550 1.99 pavel if (flag & P_PPWAIT)
551 1.1 cgd *cp++ = 'V';
552 1.99 pavel if (flag & P_SYSTEM)
553 1.57 simonb *cp++ = 'K';
554 1.51 simonb if (k->p_eflag & EPROC_SLEADER)
555 1.1 cgd *cp++ = 's';
556 1.99 pavel if (flag & P_SA)
557 1.76 thorpej *cp++ = 'a';
558 1.76 thorpej else if (k->p_nlwps > 1)
559 1.76 thorpej *cp++ = 'l';
560 1.99 pavel if ((flag & P_CONTROLT) && k->p__pgid == k->p_tpgid)
561 1.1 cgd *cp++ = '+';
562 1.1 cgd *cp = '\0';
563 1.53 simonb strprintorsetwidth(v, buf, mode);
564 1.1 cgd }
565 1.1 cgd
566 1.12 cgd void
567 1.126 christos lstate(struct pinfo *pi, VARENT *ve, enum mode mode)
568 1.76 thorpej {
569 1.126 christos struct kinfo_lwp *k = pi->li;
570 1.119 wiz int flag;
571 1.76 thorpej char *cp;
572 1.76 thorpej VAR *v;
573 1.76 thorpej char buf[16];
574 1.76 thorpej
575 1.76 thorpej v = ve->var;
576 1.76 thorpej flag = k->l_flag;
577 1.76 thorpej cp = buf;
578 1.76 thorpej
579 1.76 thorpej switch (k->l_stat) {
580 1.76 thorpej
581 1.76 thorpej case LSSTOP:
582 1.76 thorpej *cp = 'T';
583 1.76 thorpej break;
584 1.76 thorpej
585 1.76 thorpej case LSSLEEP:
586 1.81 wiz if (flag & L_SINTR) /* interruptible (long) */
587 1.110 lukem *cp = (int)k->l_slptime >= maxslp ? 'I' : 'S';
588 1.76 thorpej else
589 1.76 thorpej *cp = 'D';
590 1.76 thorpej break;
591 1.76 thorpej
592 1.76 thorpej case LSRUN:
593 1.76 thorpej case LSIDL:
594 1.117 christos *cp = 'R';
595 1.117 christos break;
596 1.117 christos
597 1.76 thorpej case LSONPROC:
598 1.117 christos *cp = 'O';
599 1.76 thorpej break;
600 1.76 thorpej
601 1.76 thorpej case LSZOMB:
602 1.99 pavel case LSDEAD:
603 1.76 thorpej *cp = 'Z';
604 1.76 thorpej break;
605 1.76 thorpej
606 1.76 thorpej case LSSUSPENDED:
607 1.76 thorpej *cp = 'U';
608 1.76 thorpej break;
609 1.76 thorpej
610 1.76 thorpej default:
611 1.76 thorpej *cp = '?';
612 1.76 thorpej }
613 1.76 thorpej cp++;
614 1.117 christos if (flag & L_SYSTEM)
615 1.116 christos *cp++ = 'K';
616 1.117 christos if (flag & L_SA)
617 1.117 christos *cp++ = 'a';
618 1.99 pavel if (flag & L_DETACHED)
619 1.76 thorpej *cp++ = '-';
620 1.76 thorpej *cp = '\0';
621 1.76 thorpej strprintorsetwidth(v, buf, mode);
622 1.76 thorpej }
623 1.76 thorpej
624 1.76 thorpej void
625 1.126 christos pnice(struct pinfo *pi, VARENT *ve, enum mode mode)
626 1.30 ws {
627 1.126 christos struct kinfo_proc2 *k = pi->ki;
628 1.30 ws VAR *v;
629 1.30 ws
630 1.30 ws v = ve->var;
631 1.53 simonb intprintorsetwidth(v, k->p_nice - NZERO, mode);
632 1.30 ws }
633 1.30 ws
634 1.30 ws void
635 1.126 christos pri(struct pinfo *pi, VARENT *ve, enum mode mode)
636 1.12 cgd {
637 1.126 christos struct kinfo_lwp *l = pi->li;
638 1.1 cgd VAR *v;
639 1.82 simonb
640 1.12 cgd v = ve->var;
641 1.102 ad intprintorsetwidth(v, l->l_priority, mode);
642 1.1 cgd }
643 1.1 cgd
644 1.12 cgd void
645 1.128 kamil usrname(struct pinfo *pi, VARENT *ve, enum mode mode)
646 1.12 cgd {
647 1.126 christos struct kinfo_proc2 *k = pi->ki;
648 1.1 cgd VAR *v;
649 1.12 cgd
650 1.12 cgd v = ve->var;
651 1.53 simonb strprintorsetwidth(v, user_from_uid(k->p_uid, 0), mode);
652 1.1 cgd }
653 1.1 cgd
654 1.12 cgd void
655 1.126 christos runame(struct pinfo *pi, VARENT *ve, enum mode mode)
656 1.12 cgd {
657 1.126 christos struct kinfo_proc2 *k = pi->ki;
658 1.1 cgd VAR *v;
659 1.12 cgd
660 1.12 cgd v = ve->var;
661 1.53 simonb strprintorsetwidth(v, user_from_uid(k->p_ruid, 0), mode);
662 1.77 atatat }
663 1.77 atatat
664 1.77 atatat void
665 1.126 christos svuname(struct pinfo *pi, VARENT *ve, enum mode mode)
666 1.77 atatat {
667 1.126 christos struct kinfo_proc2 *k = pi->ki;
668 1.77 atatat VAR *v;
669 1.77 atatat
670 1.77 atatat v = ve->var;
671 1.77 atatat strprintorsetwidth(v, user_from_uid(k->p_svuid, 0), mode);
672 1.77 atatat }
673 1.77 atatat
674 1.77 atatat void
675 1.126 christos gname(struct pinfo *pi, VARENT *ve, enum mode mode)
676 1.77 atatat {
677 1.126 christos struct kinfo_proc2 *k = pi->ki;
678 1.77 atatat VAR *v;
679 1.77 atatat
680 1.77 atatat v = ve->var;
681 1.77 atatat strprintorsetwidth(v, group_from_gid(k->p_gid, 0), mode);
682 1.77 atatat }
683 1.77 atatat
684 1.77 atatat void
685 1.126 christos rgname(struct pinfo *pi, VARENT *ve, enum mode mode)
686 1.77 atatat {
687 1.126 christos struct kinfo_proc2 *k = pi->ki;
688 1.77 atatat VAR *v;
689 1.77 atatat
690 1.77 atatat v = ve->var;
691 1.77 atatat strprintorsetwidth(v, group_from_gid(k->p_rgid, 0), mode);
692 1.77 atatat }
693 1.77 atatat
694 1.77 atatat void
695 1.126 christos svgname(struct pinfo *pi, VARENT *ve, enum mode mode)
696 1.77 atatat {
697 1.126 christos struct kinfo_proc2 *k = pi->ki;
698 1.77 atatat VAR *v;
699 1.77 atatat
700 1.77 atatat v = ve->var;
701 1.77 atatat strprintorsetwidth(v, group_from_gid(k->p_svgid, 0), mode);
702 1.1 cgd }
703 1.1 cgd
704 1.12 cgd void
705 1.126 christos tdev(struct pinfo *pi, VARENT *ve, enum mode mode)
706 1.12 cgd {
707 1.126 christos struct kinfo_proc2 *k = pi->ki;
708 1.1 cgd VAR *v;
709 1.12 cgd dev_t dev;
710 1.12 cgd char buff[16];
711 1.1 cgd
712 1.12 cgd v = ve->var;
713 1.51 simonb dev = k->p_tdev;
714 1.53 simonb if (dev == NODEV) {
715 1.53 simonb if (mode == PRINTMODE)
716 1.91 christos (void)printf("%*s", v->width, "?");
717 1.78 dsl else
718 1.78 dsl if (v->width < 2)
719 1.78 dsl v->width = 2;
720 1.53 simonb } else {
721 1.12 cgd (void)snprintf(buff, sizeof(buff),
722 1.107 christos "%lld/%lld", (long long)major(dev), (long long)minor(dev));
723 1.53 simonb strprintorsetwidth(v, buff, mode);
724 1.1 cgd }
725 1.1 cgd }
726 1.1 cgd
727 1.12 cgd void
728 1.126 christos tname(struct pinfo *pi, VARENT *ve, enum mode mode)
729 1.12 cgd {
730 1.126 christos struct kinfo_proc2 *k = pi->ki;
731 1.1 cgd VAR *v;
732 1.1 cgd dev_t dev;
733 1.38 mycroft const char *ttname;
734 1.53 simonb int noctty;
735 1.82 simonb
736 1.12 cgd v = ve->var;
737 1.51 simonb dev = k->p_tdev;
738 1.53 simonb if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) {
739 1.53 simonb if (mode == PRINTMODE)
740 1.91 christos (void)printf("%-*s", v->width, "?");
741 1.78 dsl else
742 1.78 dsl if (v->width < 2)
743 1.78 dsl v->width = 2;
744 1.53 simonb } else {
745 1.53 simonb noctty = !(k->p_eflag & EPROC_CTTY) ? 1 : 0;
746 1.53 simonb if (mode == WIDTHMODE) {
747 1.53 simonb int fmtlen;
748 1.53 simonb
749 1.53 simonb fmtlen = strlen(ttname) + noctty;
750 1.53 simonb if (v->width < fmtlen)
751 1.54 simonb v->width = fmtlen;
752 1.53 simonb } else {
753 1.53 simonb if (noctty)
754 1.84 simonb (void)printf("%-*s-", v->width - 1, ttname);
755 1.53 simonb else
756 1.84 simonb (void)printf("%-*s", v->width, ttname);
757 1.53 simonb }
758 1.1 cgd }
759 1.1 cgd }
760 1.1 cgd
761 1.12 cgd void
762 1.126 christos longtname(struct pinfo *pi, VARENT *ve, enum mode mode)
763 1.12 cgd {
764 1.126 christos struct kinfo_proc2 *k = pi->ki;
765 1.1 cgd VAR *v;
766 1.1 cgd dev_t dev;
767 1.38 mycroft const char *ttname;
768 1.1 cgd
769 1.12 cgd v = ve->var;
770 1.51 simonb dev = k->p_tdev;
771 1.53 simonb if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) {
772 1.53 simonb if (mode == PRINTMODE)
773 1.91 christos (void)printf("%-*s", v->width, "?");
774 1.78 dsl else
775 1.78 dsl if (v->width < 2)
776 1.78 dsl v->width = 2;
777 1.78 dsl } else {
778 1.53 simonb strprintorsetwidth(v, ttname, mode);
779 1.53 simonb }
780 1.1 cgd }
781 1.1 cgd
782 1.12 cgd void
783 1.126 christos started(struct pinfo *pi, VARENT *ve, enum mode mode)
784 1.12 cgd {
785 1.126 christos struct kinfo_proc2 *k = pi->ki;
786 1.1 cgd VAR *v;
787 1.24 cgd time_t startt;
788 1.1 cgd struct tm *tp;
789 1.53 simonb char buf[100], *cp;
790 1.1 cgd
791 1.12 cgd v = ve->var;
792 1.51 simonb if (!k->p_uvalid) {
793 1.53 simonb if (mode == PRINTMODE)
794 1.53 simonb (void)printf("%*s", v->width, "-");
795 1.1 cgd return;
796 1.1 cgd }
797 1.1 cgd
798 1.51 simonb startt = k->p_ustart_sec;
799 1.24 cgd tp = localtime(&startt);
800 1.87 simonb if (now == 0)
801 1.1 cgd (void)time(&now);
802 1.53 simonb if (now - k->p_ustart_sec < SECSPERDAY)
803 1.12 cgd /* I *hate* SCCS... */
804 1.134 christos safe_strftime(buf, sizeof(buf) - 1, "%l:%" "M%p", tp);
805 1.53 simonb else if (now - k->p_ustart_sec < DAYSPERWEEK * SECSPERDAY)
806 1.12 cgd /* I *hate* SCCS... */
807 1.134 christos safe_strftime(buf, sizeof(buf) - 1, "%a%" "I%p", tp);
808 1.53 simonb else
809 1.134 christos safe_strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
810 1.53 simonb /* %e and %l can start with a space. */
811 1.53 simonb cp = buf;
812 1.53 simonb if (*cp == ' ')
813 1.53 simonb cp++;
814 1.53 simonb strprintorsetwidth(v, cp, mode);
815 1.1 cgd }
816 1.1 cgd
817 1.12 cgd void
818 1.126 christos lstarted(struct pinfo *pi, VARENT *ve, enum mode mode)
819 1.82 simonb {
820 1.126 christos struct kinfo_proc2 *k = pi->ki;
821 1.1 cgd VAR *v;
822 1.24 cgd time_t startt;
823 1.1 cgd char buf[100];
824 1.1 cgd
825 1.12 cgd v = ve->var;
826 1.133 simonb startt = k->p_ustart_sec;
827 1.133 simonb
828 1.133 simonb if (mode == WIDTHMODE) {
829 1.53 simonb /*
830 1.133 simonb * We only need to set the width once, as we assume
831 1.133 simonb * that all times are the same length. We do need to
832 1.133 simonb * check against the header length as well, as "no
833 1.133 simonb * header" mode for this variable will set the field
834 1.133 simonb * width to the length of the header anyway (ref: the
835 1.133 simonb * P1003.1-2004 comment in findvar()).
836 1.133 simonb *
837 1.133 simonb * XXX: The hardcoded "STARTED" string. Better or
838 1.133 simonb * worse than a "<= 7" or some other arbitary number?
839 1.53 simonb */
840 1.134 christos if (v->width > (int)sizeof("STARTED") - 1) {
841 1.134 christos return;
842 1.133 simonb }
843 1.133 simonb } else {
844 1.133 simonb if (!k->p_uvalid) {
845 1.53 simonb (void)printf("%*s", v->width, "-");
846 1.134 christos return;
847 1.133 simonb }
848 1.53 simonb }
849 1.134 christos safe_strftime(buf, sizeof(buf) - 1, "%c", localtime(&startt));
850 1.134 christos strprintorsetwidth(v, buf, mode);
851 1.1 cgd }
852 1.1 cgd
853 1.12 cgd void
854 1.126 christos elapsed(struct pinfo *pi, VARENT *ve, enum mode mode)
855 1.87 simonb {
856 1.126 christos struct kinfo_proc2 *k = pi->ki;
857 1.87 simonb VAR *v;
858 1.87 simonb int32_t origseconds, secs, mins, hours, days;
859 1.87 simonb int fmtlen, printed_something;
860 1.87 simonb
861 1.87 simonb v = ve->var;
862 1.87 simonb if (k->p_uvalid == 0) {
863 1.87 simonb origseconds = 0;
864 1.87 simonb } else {
865 1.87 simonb if (now == 0)
866 1.87 simonb (void)time(&now);
867 1.87 simonb origseconds = now - k->p_ustart_sec;
868 1.87 simonb if (origseconds < 0) {
869 1.87 simonb /*
870 1.87 simonb * Don't try to be fancy if the machine's
871 1.87 simonb * clock has been rewound to before the
872 1.87 simonb * process "started".
873 1.87 simonb */
874 1.87 simonb origseconds = 0;
875 1.87 simonb }
876 1.87 simonb }
877 1.87 simonb
878 1.87 simonb secs = origseconds;
879 1.87 simonb mins = secs / SECSPERMIN;
880 1.87 simonb secs %= SECSPERMIN;
881 1.87 simonb hours = mins / MINSPERHOUR;
882 1.87 simonb mins %= MINSPERHOUR;
883 1.87 simonb days = hours / HOURSPERDAY;
884 1.87 simonb hours %= HOURSPERDAY;
885 1.87 simonb
886 1.87 simonb if (mode == WIDTHMODE) {
887 1.87 simonb if (origseconds == 0)
888 1.87 simonb /* non-zero so fmtlen is calculated at least once */
889 1.87 simonb origseconds = 1;
890 1.87 simonb
891 1.87 simonb if (origseconds > v->longestp) {
892 1.87 simonb v->longestp = origseconds;
893 1.87 simonb
894 1.87 simonb if (days > 0) {
895 1.87 simonb /* +9 for "-hh:mm:ss" */
896 1.87 simonb fmtlen = iwidth(days) + 9;
897 1.87 simonb } else if (hours > 0) {
898 1.87 simonb /* +6 for "mm:ss" */
899 1.87 simonb fmtlen = iwidth(hours) + 6;
900 1.87 simonb } else {
901 1.87 simonb /* +3 for ":ss" */
902 1.87 simonb fmtlen = iwidth(mins) + 3;
903 1.87 simonb }
904 1.87 simonb
905 1.87 simonb if (fmtlen > v->width)
906 1.87 simonb v->width = fmtlen;
907 1.87 simonb }
908 1.87 simonb } else {
909 1.89 simonb printed_something = 0;
910 1.87 simonb fmtlen = v->width;
911 1.87 simonb
912 1.87 simonb if (days > 0) {
913 1.87 simonb (void)printf("%*d", fmtlen - 9, days);
914 1.87 simonb printed_something = 1;
915 1.87 simonb } else if (fmtlen > 9) {
916 1.87 simonb (void)printf("%*s", fmtlen - 9, "");
917 1.87 simonb }
918 1.87 simonb if (fmtlen > 9)
919 1.87 simonb fmtlen = 9;
920 1.87 simonb
921 1.87 simonb if (printed_something) {
922 1.87 simonb (void)printf("-%.*d", fmtlen - 7, hours);
923 1.87 simonb printed_something = 1;
924 1.87 simonb } else if (hours > 0) {
925 1.87 simonb (void)printf("%*d", fmtlen - 6, hours);
926 1.87 simonb printed_something = 1;
927 1.87 simonb } else if (fmtlen > 6) {
928 1.87 simonb (void)printf("%*s", fmtlen - 6, "");
929 1.87 simonb }
930 1.87 simonb if (fmtlen > 6)
931 1.87 simonb fmtlen = 6;
932 1.87 simonb
933 1.87 simonb /* Don't need to set fmtlen or printed_something any more... */
934 1.87 simonb if (printed_something) {
935 1.87 simonb (void)printf(":%.*d", fmtlen - 4, mins);
936 1.87 simonb } else if (mins > 0) {
937 1.87 simonb (void)printf("%*d", fmtlen - 3, mins);
938 1.87 simonb } else if (fmtlen > 3) {
939 1.87 simonb (void)printf("%*s", fmtlen - 3, "0");
940 1.87 simonb }
941 1.87 simonb
942 1.87 simonb (void)printf(":%.2d", secs);
943 1.87 simonb }
944 1.87 simonb }
945 1.87 simonb
946 1.87 simonb void
947 1.126 christos wchan(struct pinfo *pi, VARENT *ve, enum mode mode)
948 1.12 cgd {
949 1.126 christos struct kinfo_lwp *l = pi->li;
950 1.1 cgd VAR *v;
951 1.12 cgd
952 1.12 cgd v = ve->var;
953 1.130 maxv if (l->l_wmesg[0]) {
954 1.130 maxv strprintorsetwidth(v, l->l_wmesg, mode);
955 1.130 maxv v->width = min(v->width, KI_WMESGLEN);
956 1.53 simonb } else {
957 1.53 simonb if (mode == PRINTMODE)
958 1.53 simonb (void)printf("%-*s", v->width, "-");
959 1.53 simonb }
960 1.1 cgd }
961 1.1 cgd
962 1.118 dholland #define pgtok(a) (((a)*(size_t)getpagesize())/1024)
963 1.1 cgd
964 1.12 cgd void
965 1.126 christos vsize(struct pinfo *pi, VARENT *ve, enum mode mode)
966 1.12 cgd {
967 1.126 christos struct kinfo_proc2 *k = pi->ki;
968 1.1 cgd VAR *v;
969 1.12 cgd
970 1.12 cgd v = ve->var;
971 1.111 mrg intprintorsetwidth(v, pgtok(k->p_vm_msize), mode);
972 1.1 cgd }
973 1.1 cgd
974 1.12 cgd void
975 1.126 christos rssize(struct pinfo *pi, VARENT *ve, enum mode mode)
976 1.12 cgd {
977 1.126 christos struct kinfo_proc2 *k = pi->ki;
978 1.1 cgd VAR *v;
979 1.12 cgd
980 1.12 cgd v = ve->var;
981 1.1 cgd /* XXX don't have info about shared */
982 1.53 simonb intprintorsetwidth(v, pgtok(k->p_vm_rssize), mode);
983 1.1 cgd }
984 1.1 cgd
985 1.12 cgd void
986 1.126 christos p_rssize(struct pinfo *pi, VARENT *ve, enum mode mode) /* doesn't account for text */
987 1.12 cgd {
988 1.126 christos struct kinfo_proc2 *k = pi->ki;
989 1.1 cgd VAR *v;
990 1.12 cgd
991 1.12 cgd v = ve->var;
992 1.53 simonb intprintorsetwidth(v, pgtok(k->p_vm_rssize), mode);
993 1.1 cgd }
994 1.1 cgd
995 1.12 cgd void
996 1.126 christos cpuid(struct pinfo *pi, VARENT *ve, enum mode mode)
997 1.104 christos {
998 1.126 christos struct kinfo_lwp *l = pi->li;
999 1.104 christos VAR *v;
1000 1.104 christos
1001 1.104 christos v = ve->var;
1002 1.113 rmind intprintorsetwidth(v, l->l_cpuid, mode);
1003 1.104 christos }
1004 1.104 christos
1005 1.121 mlelstv static void
1006 1.122 dholland cputime1(int32_t secs, int32_t psecs, VAR *v, enum mode mode)
1007 1.12 cgd {
1008 1.53 simonb int fmtlen;
1009 1.1 cgd
1010 1.100 dsl /*
1011 1.100 dsl * round and scale to 100's
1012 1.100 dsl */
1013 1.100 dsl psecs = (psecs + 5000) / 10000;
1014 1.100 dsl secs += psecs / 100;
1015 1.100 dsl psecs = psecs % 100;
1016 1.100 dsl
1017 1.53 simonb if (mode == WIDTHMODE) {
1018 1.53 simonb /*
1019 1.83 simonb * Ugg, this is the only field where a value of 0 is longer
1020 1.78 dsl * than the column title.
1021 1.53 simonb * Use SECSPERMIN, because secs is divided by that when
1022 1.78 dsl * passed to iwidth().
1023 1.53 simonb */
1024 1.78 dsl if (secs == 0)
1025 1.53 simonb secs = SECSPERMIN;
1026 1.78 dsl
1027 1.53 simonb if (secs > v->longestp) {
1028 1.53 simonb v->longestp = secs;
1029 1.78 dsl /* "+6" for the ":%02ld.%02ld" in the printf() below */
1030 1.78 dsl fmtlen = iwidth(secs / SECSPERMIN) + 6;
1031 1.53 simonb if (fmtlen > v->width)
1032 1.53 simonb v->width = fmtlen;
1033 1.53 simonb }
1034 1.53 simonb } else {
1035 1.84 simonb (void)printf("%*ld:%02ld.%02ld", v->width - 6,
1036 1.84 simonb (long)(secs / SECSPERMIN), (long)(secs % SECSPERMIN),
1037 1.84 simonb (long)psecs);
1038 1.53 simonb }
1039 1.1 cgd }
1040 1.1 cgd
1041 1.121 mlelstv void
1042 1.126 christos cputime(struct pinfo *pi, VARENT *ve, enum mode mode)
1043 1.121 mlelstv {
1044 1.126 christos struct kinfo_proc2 *k = pi->ki;
1045 1.121 mlelstv VAR *v;
1046 1.121 mlelstv int32_t secs;
1047 1.121 mlelstv int32_t psecs; /* "parts" of a second. first micro, then centi */
1048 1.121 mlelstv
1049 1.121 mlelstv v = ve->var;
1050 1.121 mlelstv
1051 1.121 mlelstv /*
1052 1.121 mlelstv * This counts time spent handling interrupts. We could
1053 1.121 mlelstv * fix this, but it is not 100% trivial (and interrupt
1054 1.121 mlelstv * time fractions only work on the sparc anyway). XXX
1055 1.121 mlelstv */
1056 1.121 mlelstv secs = k->p_rtime_sec;
1057 1.121 mlelstv psecs = k->p_rtime_usec;
1058 1.121 mlelstv if (sumrusage) {
1059 1.121 mlelstv secs += k->p_uctime_sec;
1060 1.121 mlelstv psecs += k->p_uctime_usec;
1061 1.121 mlelstv }
1062 1.121 mlelstv
1063 1.121 mlelstv cputime1(secs, psecs, v, mode);
1064 1.121 mlelstv }
1065 1.121 mlelstv
1066 1.121 mlelstv void
1067 1.126 christos lcputime(struct pinfo *pi, VARENT *ve, enum mode mode)
1068 1.121 mlelstv {
1069 1.126 christos struct kinfo_lwp *l = pi->li;
1070 1.121 mlelstv VAR *v;
1071 1.121 mlelstv int32_t secs;
1072 1.121 mlelstv int32_t psecs; /* "parts" of a second. first micro, then centi */
1073 1.121 mlelstv
1074 1.121 mlelstv v = ve->var;
1075 1.121 mlelstv
1076 1.121 mlelstv secs = l->l_rtime_sec;
1077 1.121 mlelstv psecs = l->l_rtime_usec;
1078 1.121 mlelstv
1079 1.121 mlelstv cputime1(secs, psecs, v, mode);
1080 1.121 mlelstv }
1081 1.121 mlelstv
1082 1.12 cgd void
1083 1.126 christos pcpu(struct pinfo *pi, VARENT *ve, enum mode mode)
1084 1.12 cgd {
1085 1.1 cgd VAR *v;
1086 1.114 njoly double dbl;
1087 1.12 cgd
1088 1.12 cgd v = ve->var;
1089 1.126 christos dbl = pi->pcpu;
1090 1.115 njoly doubleprintorsetwidth(v, dbl, (dbl >= 99.95) ? 0 : 1, mode);
1091 1.1 cgd }
1092 1.1 cgd
1093 1.1 cgd double
1094 1.120 matt getpmem(const struct kinfo_proc2 *k)
1095 1.1 cgd {
1096 1.1 cgd double fracmem;
1097 1.1 cgd int szptudot;
1098 1.1 cgd
1099 1.1 cgd if (!nlistread)
1100 1.124 rin donlist();
1101 1.1 cgd
1102 1.1 cgd /* XXX want pmap ptpages, segtab, etc. (per architecture) */
1103 1.66 matt szptudot = uspace/getpagesize();
1104 1.1 cgd /* XXX don't have info about shared */
1105 1.51 simonb fracmem = ((float)k->p_vm_rssize + szptudot)/mempages;
1106 1.1 cgd return (100.0 * fracmem);
1107 1.1 cgd }
1108 1.1 cgd
1109 1.12 cgd void
1110 1.126 christos pmem(struct pinfo *pi, VARENT *ve, enum mode mode)
1111 1.12 cgd {
1112 1.126 christos struct kinfo_proc2 *k = pi->ki;
1113 1.1 cgd VAR *v;
1114 1.12 cgd
1115 1.12 cgd v = ve->var;
1116 1.53 simonb doubleprintorsetwidth(v, getpmem(k), 1, mode);
1117 1.1 cgd }
1118 1.1 cgd
1119 1.12 cgd void
1120 1.126 christos pagein(struct pinfo *pi, VARENT *ve, enum mode mode)
1121 1.12 cgd {
1122 1.126 christos struct kinfo_proc2 *k = pi->ki;
1123 1.1 cgd VAR *v;
1124 1.12 cgd
1125 1.12 cgd v = ve->var;
1126 1.53 simonb intprintorsetwidth(v, k->p_uvalid ? k->p_uru_majflt : 0, mode);
1127 1.1 cgd }
1128 1.1 cgd
1129 1.12 cgd void
1130 1.126 christos maxrss(struct pinfo *pi, VARENT *ve, enum mode mode)
1131 1.12 cgd {
1132 1.1 cgd VAR *v;
1133 1.12 cgd
1134 1.12 cgd v = ve->var;
1135 1.53 simonb /* No need to check width! */
1136 1.53 simonb if (mode == PRINTMODE)
1137 1.53 simonb (void)printf("%*s", v->width, "-");
1138 1.1 cgd }
1139 1.1 cgd
1140 1.12 cgd void
1141 1.126 christos tsize(struct pinfo *pi, VARENT *ve, enum mode mode)
1142 1.12 cgd {
1143 1.126 christos struct kinfo_proc2 *k = pi->ki;
1144 1.1 cgd VAR *v;
1145 1.12 cgd
1146 1.12 cgd v = ve->var;
1147 1.53 simonb intprintorsetwidth(v, pgtok(k->p_vm_tsize), mode);
1148 1.1 cgd }
1149 1.1 cgd
1150 1.1 cgd /*
1151 1.1 cgd * Generic output routines. Print fields from various prototype
1152 1.1 cgd * structures.
1153 1.1 cgd */
1154 1.12 cgd static void
1155 1.122 dholland printval(void *bp, VAR *v, enum mode mode)
1156 1.1 cgd {
1157 1.1 cgd static char ofmt[32] = "%";
1158 1.53 simonb int width, vok, fmtlen;
1159 1.93 christos const char *fcp;
1160 1.93 christos char *cp;
1161 1.78 dsl int64_t val;
1162 1.78 dsl u_int64_t uval;
1163 1.53 simonb
1164 1.92 he val = 0; /* XXXGCC -Wuninitialized [hpcarm] */
1165 1.92 he uval = 0; /* XXXGCC -Wuninitialized [hpcarm] */
1166 1.92 he
1167 1.53 simonb /*
1168 1.53 simonb * Note that the "INF127" check is nonsensical for types
1169 1.53 simonb * that are or can be signed.
1170 1.53 simonb */
1171 1.53 simonb #define GET(type) (*(type *)bp)
1172 1.53 simonb #define CHK_INF127(n) (((n) > 127) && (v->flag & INF127) ? 127 : (n))
1173 1.53 simonb
1174 1.53 simonb #define VSIGN 1
1175 1.53 simonb #define VUNSIGN 2
1176 1.53 simonb #define VPTR 3
1177 1.53 simonb
1178 1.53 simonb if (mode == WIDTHMODE) {
1179 1.53 simonb vok = 0;
1180 1.53 simonb switch (v->type) {
1181 1.53 simonb case CHAR:
1182 1.53 simonb val = GET(char);
1183 1.53 simonb vok = VSIGN;
1184 1.53 simonb break;
1185 1.53 simonb case UCHAR:
1186 1.53 simonb uval = CHK_INF127(GET(u_char));
1187 1.53 simonb vok = VUNSIGN;
1188 1.53 simonb break;
1189 1.53 simonb case SHORT:
1190 1.53 simonb val = GET(short);
1191 1.53 simonb vok = VSIGN;
1192 1.53 simonb break;
1193 1.53 simonb case USHORT:
1194 1.53 simonb uval = CHK_INF127(GET(u_short));
1195 1.53 simonb vok = VUNSIGN;
1196 1.53 simonb break;
1197 1.53 simonb case INT32:
1198 1.53 simonb val = GET(int32_t);
1199 1.53 simonb vok = VSIGN;
1200 1.53 simonb break;
1201 1.53 simonb case INT:
1202 1.53 simonb val = GET(int);
1203 1.53 simonb vok = VSIGN;
1204 1.53 simonb break;
1205 1.53 simonb case UINT:
1206 1.53 simonb case UINT32:
1207 1.53 simonb uval = CHK_INF127(GET(u_int));
1208 1.53 simonb vok = VUNSIGN;
1209 1.53 simonb break;
1210 1.53 simonb case LONG:
1211 1.53 simonb val = GET(long);
1212 1.53 simonb vok = VSIGN;
1213 1.53 simonb break;
1214 1.53 simonb case ULONG:
1215 1.53 simonb uval = CHK_INF127(GET(u_long));
1216 1.53 simonb vok = VUNSIGN;
1217 1.53 simonb break;
1218 1.53 simonb case KPTR:
1219 1.78 dsl uval = GET(u_int64_t);
1220 1.53 simonb vok = VPTR;
1221 1.53 simonb break;
1222 1.53 simonb case KPTR24:
1223 1.78 dsl uval = GET(u_int64_t);
1224 1.58 itojun uval &= 0xffffff;
1225 1.53 simonb vok = VPTR;
1226 1.53 simonb break;
1227 1.72 nathanw case INT64:
1228 1.78 dsl val = GET(int64_t);
1229 1.72 nathanw vok = VSIGN;
1230 1.72 nathanw break;
1231 1.72 nathanw case UINT64:
1232 1.78 dsl uval = CHK_INF127(GET(u_int64_t));
1233 1.72 nathanw vok = VUNSIGN;
1234 1.72 nathanw break;
1235 1.72 nathanw
1236 1.78 dsl case SIGLIST:
1237 1.53 simonb default:
1238 1.53 simonb /* nothing... */;
1239 1.53 simonb }
1240 1.53 simonb switch (vok) {
1241 1.53 simonb case VSIGN:
1242 1.78 dsl if (val < 0 && val < v->longestn) {
1243 1.53 simonb v->longestn = val;
1244 1.78 dsl fmtlen = iwidth(-val) + 1;
1245 1.53 simonb if (fmtlen > v->width)
1246 1.53 simonb v->width = fmtlen;
1247 1.53 simonb } else if (val > 0 && val > v->longestp) {
1248 1.53 simonb v->longestp = val;
1249 1.78 dsl fmtlen = iwidth(val);
1250 1.53 simonb if (fmtlen > v->width)
1251 1.53 simonb v->width = fmtlen;
1252 1.53 simonb }
1253 1.53 simonb return;
1254 1.53 simonb case VUNSIGN:
1255 1.53 simonb if (uval > v->longestu) {
1256 1.53 simonb v->longestu = uval;
1257 1.78 dsl v->width = iwidth(uval);
1258 1.53 simonb }
1259 1.53 simonb return;
1260 1.53 simonb case VPTR:
1261 1.53 simonb fmtlen = 0;
1262 1.53 simonb while (uval > 0) {
1263 1.53 simonb uval >>= 4;
1264 1.53 simonb fmtlen++;
1265 1.53 simonb }
1266 1.53 simonb if (fmtlen > v->width)
1267 1.53 simonb v->width = fmtlen;
1268 1.53 simonb return;
1269 1.53 simonb }
1270 1.53 simonb }
1271 1.1 cgd
1272 1.53 simonb width = v->width;
1273 1.12 cgd cp = ofmt + 1;
1274 1.12 cgd fcp = v->fmt;
1275 1.1 cgd if (v->flag & LJUST)
1276 1.1 cgd *cp++ = '-';
1277 1.1 cgd *cp++ = '*';
1278 1.33 christos while ((*cp++ = *fcp++) != '\0')
1279 1.33 christos continue;
1280 1.1 cgd
1281 1.1 cgd switch (v->type) {
1282 1.1 cgd case CHAR:
1283 1.53 simonb (void)printf(ofmt, width, GET(char));
1284 1.53 simonb return;
1285 1.1 cgd case UCHAR:
1286 1.53 simonb (void)printf(ofmt, width, CHK_INF127(GET(u_char)));
1287 1.53 simonb return;
1288 1.1 cgd case SHORT:
1289 1.53 simonb (void)printf(ofmt, width, GET(short));
1290 1.53 simonb return;
1291 1.1 cgd case USHORT:
1292 1.53 simonb (void)printf(ofmt, width, CHK_INF127(GET(u_short)));
1293 1.53 simonb return;
1294 1.19 cgd case INT:
1295 1.53 simonb (void)printf(ofmt, width, GET(int));
1296 1.53 simonb return;
1297 1.19 cgd case UINT:
1298 1.53 simonb (void)printf(ofmt, width, CHK_INF127(GET(u_int)));
1299 1.53 simonb return;
1300 1.1 cgd case LONG:
1301 1.53 simonb (void)printf(ofmt, width, GET(long));
1302 1.53 simonb return;
1303 1.1 cgd case ULONG:
1304 1.53 simonb (void)printf(ofmt, width, CHK_INF127(GET(u_long)));
1305 1.53 simonb return;
1306 1.1 cgd case KPTR:
1307 1.78 dsl (void)printf(ofmt, width, GET(u_int64_t));
1308 1.53 simonb return;
1309 1.41 mrg case KPTR24:
1310 1.78 dsl (void)printf(ofmt, width, GET(u_int64_t) & 0xffffff);
1311 1.78 dsl return;
1312 1.78 dsl case INT32:
1313 1.78 dsl (void)printf(ofmt, width, GET(int32_t));
1314 1.78 dsl return;
1315 1.78 dsl case UINT32:
1316 1.78 dsl (void)printf(ofmt, width, CHK_INF127(GET(u_int32_t)));
1317 1.53 simonb return;
1318 1.40 christos case SIGLIST:
1319 1.40 christos {
1320 1.40 christos sigset_t *s = (sigset_t *)(void *)bp;
1321 1.40 christos size_t i;
1322 1.86 simonb #define SIGSETSIZE (sizeof(s->__bits) / sizeof(s->__bits[0]))
1323 1.40 christos char buf[SIGSETSIZE * 8 + 1];
1324 1.40 christos
1325 1.40 christos for (i = 0; i < SIGSETSIZE; i++)
1326 1.40 christos (void)snprintf(&buf[i * 8], 9, "%.8x",
1327 1.40 christos s->__bits[(SIGSETSIZE - 1) - i]);
1328 1.40 christos
1329 1.40 christos /* Skip leading zeroes */
1330 1.78 dsl for (i = 0; buf[i] == '0'; i++)
1331 1.78 dsl continue;
1332 1.40 christos
1333 1.40 christos if (buf[i] == '\0')
1334 1.40 christos i--;
1335 1.78 dsl strprintorsetwidth(v, buf + i, mode);
1336 1.78 dsl #undef SIGSETSIZE
1337 1.40 christos }
1338 1.78 dsl return;
1339 1.72 nathanw case INT64:
1340 1.78 dsl (void)printf(ofmt, width, GET(int64_t));
1341 1.72 nathanw return;
1342 1.72 nathanw case UINT64:
1343 1.78 dsl (void)printf(ofmt, width, CHK_INF127(GET(u_int64_t)));
1344 1.72 nathanw return;
1345 1.1 cgd default:
1346 1.129 christos errx(EXIT_FAILURE, "unknown type %d", v->type);
1347 1.1 cgd }
1348 1.25 cgd #undef GET
1349 1.26 cgd #undef CHK_INF127
1350 1.12 cgd }
1351 1.12 cgd
1352 1.12 cgd void
1353 1.126 christos pvar(struct pinfo *pi, VARENT *ve, enum mode mode)
1354 1.12 cgd {
1355 1.127 christos VAR *v = ve->var;
1356 1.127 christos char *b = (v->flag & LWP) ? (char *)pi->li : (char *)pi->ki;
1357 1.12 cgd
1358 1.127 christos if ((v->flag & UAREA) && !pi->ki->p_uvalid) {
1359 1.78 dsl if (mode == PRINTMODE)
1360 1.78 dsl (void)printf("%*s", v->width, "-");
1361 1.78 dsl return;
1362 1.78 dsl }
1363 1.78 dsl
1364 1.127 christos (void)printval(b + v->off, v, mode);
1365 1.78 dsl }
1366 1.78 dsl
1367 1.78 dsl void
1368 1.126 christos putimeval(struct pinfo *pi, VARENT *ve, enum mode mode)
1369 1.78 dsl {
1370 1.78 dsl VAR *v = ve->var;
1371 1.126 christos struct kinfo_proc2 *k = pi->ki;
1372 1.127 christos char *b = (v->flag & LWP) ? (char *)pi->li : (char *)pi->ki;
1373 1.127 christos ulong secs = *(uint32_t *)(b + v->off);
1374 1.127 christos ulong usec = *(uint32_t *)(b + v->off + sizeof (uint32_t));
1375 1.78 dsl int fmtlen;
1376 1.78 dsl
1377 1.78 dsl if (!k->p_uvalid) {
1378 1.78 dsl if (mode == PRINTMODE)
1379 1.78 dsl (void)printf("%*s", v->width, "-");
1380 1.78 dsl return;
1381 1.78 dsl }
1382 1.78 dsl
1383 1.78 dsl if (mode == WIDTHMODE) {
1384 1.87 simonb if (secs == 0)
1385 1.87 simonb /* non-zero so fmtlen is calculated at least once */
1386 1.78 dsl secs = 1;
1387 1.78 dsl if (secs > v->longestu) {
1388 1.78 dsl v->longestu = secs;
1389 1.78 dsl if (secs <= 999)
1390 1.78 dsl /* sss.ssssss */
1391 1.78 dsl fmtlen = iwidth(secs) + 6 + 1;
1392 1.78 dsl else
1393 1.78 dsl /* hh:mm:ss.ss */
1394 1.85 simonb fmtlen = iwidth((secs + 1) / SECSPERHOUR)
1395 1.78 dsl + 2 + 1 + 2 + 1 + 2 + 1;
1396 1.78 dsl if (fmtlen > v->width)
1397 1.78 dsl v->width = fmtlen;
1398 1.78 dsl }
1399 1.78 dsl return;
1400 1.78 dsl }
1401 1.78 dsl
1402 1.78 dsl if (secs < 999)
1403 1.78 dsl (void)printf( "%*lu.%.6lu", v->width - 6 - 1, secs, usec);
1404 1.78 dsl else {
1405 1.78 dsl uint h, m;
1406 1.78 dsl usec += 5000;
1407 1.78 dsl if (usec >= 1000000) {
1408 1.78 dsl usec -= 1000000;
1409 1.78 dsl secs++;
1410 1.78 dsl }
1411 1.85 simonb m = secs / SECSPERMIN;
1412 1.85 simonb secs -= m * SECSPERMIN;
1413 1.85 simonb h = m / MINSPERHOUR;
1414 1.85 simonb m -= h * MINSPERHOUR;
1415 1.84 simonb (void)printf( "%*u:%.2u:%.2lu.%.2lu", v->width - 9, h, m, secs,
1416 1.84 simonb usec / 10000u );
1417 1.78 dsl }
1418 1.1 cgd }
1419 1.101 yamt
1420 1.101 yamt void
1421 1.126 christos lname(struct pinfo *pi, VARENT *ve, enum mode mode)
1422 1.101 yamt {
1423 1.126 christos struct kinfo_lwp *l = pi->li;
1424 1.101 yamt VAR *v;
1425 1.101 yamt
1426 1.101 yamt v = ve->var;
1427 1.123 joerg if (l->l_name[0] != '\0') {
1428 1.101 yamt strprintorsetwidth(v, l->l_name, mode);
1429 1.101 yamt v->width = min(v->width, KI_LNAMELEN);
1430 1.101 yamt } else {
1431 1.101 yamt if (mode == PRINTMODE)
1432 1.101 yamt (void)printf("%-*s", v->width, "-");
1433 1.101 yamt }
1434 1.101 yamt }
1435