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