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