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