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