print.c revision 1.55.2.1 1 /* $NetBSD: print.c,v 1.55.2.1 2001/01/27 07:06:45 jhawk Exp $ */
2
3 /*
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Simon Burge.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1990, 1993, 1994
41 * The Regents of the University of California. All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 */
71
72 #include <sys/cdefs.h>
73 #ifndef lint
74 #if 0
75 static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94";
76 #else
77 __RCSID("$NetBSD: print.c,v 1.55.2.1 2001/01/27 07:06:45 jhawk Exp $");
78 #endif
79 #endif /* not lint */
80
81 #include <sys/param.h>
82 #include <sys/time.h>
83 #include <sys/resource.h>
84 #include <sys/proc.h>
85 #include <sys/stat.h>
86 #include <sys/ucred.h>
87 #include <sys/sysctl.h>
88
89 #include <vm/vm.h>
90
91 #include <err.h>
92 #include <kvm.h>
93 #include <math.h>
94 #include <nlist.h>
95 #include <pwd.h>
96 #include <stddef.h>
97 #include <stdio.h>
98 #include <stdlib.h>
99 #include <string.h>
100 #include <time.h>
101 #include <tzfile.h>
102 #include <unistd.h>
103
104 #include "ps.h"
105
106 static char *cmdpart __P((char *));
107 static void printval __P((void *, VAR *, int));
108 static int titlecmp __P((char *, char **));
109
110 static void doubleprintorsetwidth __P((VAR *, double, int, int));
111 static void intprintorsetwidth __P((VAR *, int, int));
112 static void strprintorsetwidth __P((VAR *, const char *, int));
113
114 #define min(a,b) ((a) <= (b) ? (a) : (b))
115 #define max(a,b) ((a) >= (b) ? (a) : (b))
116
117 static char *
118 cmdpart(arg0)
119 char *arg0;
120 {
121 char *cp;
122
123 return ((cp = strrchr(arg0, '/')) != NULL ? cp + 1 : arg0);
124 }
125
126 void
127 printheader()
128 {
129 int len;
130 VAR *v;
131 struct varent *vent;
132 static int firsttime = 1;
133
134 for (vent = vhead; vent; vent = vent->next) {
135 v = vent->var;
136 if (firsttime) {
137 len = strlen(v->header);
138 if (len > v->width)
139 v->width = len;
140 totwidth += v->width + 1; /* +1 for space */
141 }
142 if (v->flag & LJUST) {
143 if (vent->next == NULL) /* last one */
144 (void)printf("%s", v->header);
145 else
146 (void)printf("%-*s", v->width,
147 v->header);
148 } else
149 (void)printf("%*s", v->width, v->header);
150 if (vent->next != NULL)
151 (void)putchar(' ');
152 }
153 (void)putchar('\n');
154 if (firsttime) {
155 firsttime = 0;
156 totwidth--; /* take off last space */
157 }
158 }
159
160 /*
161 * Return 1 if the the command name in the argument vector (u-area) does
162 * not match the command name (p_comm)
163 */
164 static int
165 titlecmp(name, argv)
166 char *name;
167 char **argv;
168 {
169 char *title;
170 int namelen;
171
172
173 /* no argument vector == no match; system processes/threads do that */
174 if (argv == 0 || argv[0] == 0)
175 return (1);
176
177 title = cmdpart(argv[0]);
178
179 /* the basename matches */
180 if (!strcmp(name, title))
181 return (0);
182
183 /* handle login shells, by skipping the leading - */
184 if (title[0] == '-' && !strcmp(name, title+1))
185 return (0);
186
187 namelen = strlen(name);
188
189 /* handle daemons that report activity as daemonname: activity */
190 if (argv[1] == 0 &&
191 !strncmp(name, title, namelen) &&
192 title[namelen + 0] == ':' &&
193 title[namelen + 1] == ' ')
194 return (0);
195
196 return (1);
197 }
198
199 static void
200 doubleprintorsetwidth(v, val, prec, mode)
201 VAR *v;
202 double val;
203 int prec;
204 int mode;
205 {
206 int fmtlen;
207
208 if (mode == WIDTHMODE) {
209 if (val < 0.0 && val < v->longestnd) {
210 fmtlen = (int)log10(-val) + prec + 2;
211 v->longestnd = val;
212 if (fmtlen > v->width)
213 v->width = fmtlen;
214 } else if (val > 0.0 && val > v->longestpd) {
215 fmtlen = (int)log10(val) + prec + 1;
216 v->longestpd = val;
217 if (fmtlen > v->width)
218 v->width = fmtlen;
219 }
220 } else {
221 printf("%*.*f", v->width, prec, val);
222 }
223 }
224
225 static void
226 intprintorsetwidth(v, val, mode)
227 VAR *v;
228 int val;
229 int mode;
230 {
231 int fmtlen;
232
233 if (mode == WIDTHMODE) {
234 if (val < 0 && val < v->longestn) {
235 fmtlen = (int)log10((double)-val) + 2;
236 v->longestn = val;
237 if (fmtlen > v->width)
238 v->width = fmtlen;
239 } else if (val > 0 && val > v->longestp) {
240 fmtlen = (int)log10((double)val) + 1;
241 v->longestp = val;
242 if (fmtlen > v->width)
243 v->width = fmtlen;
244 }
245 } else
246 printf("%*d", v->width, val);
247 }
248
249 static void
250 strprintorsetwidth(v, str, mode)
251 VAR *v;
252 const char *str;
253 {
254 int len;
255
256 if (mode == WIDTHMODE) {
257 len = strlen(str);
258 if (len > v->width)
259 v->width = len;
260 } else {
261 if (v->flag & LJUST)
262 printf("%-*.*s", v->width, v->width, str);
263 else
264 printf("%*.*s", v->width, v->width, str);
265 }
266 }
267
268 void
269 command(ki, ve, mode)
270 struct kinfo_proc2 *ki;
271 VARENT *ve;
272 int mode;
273 {
274 VAR *v;
275 int left;
276 char **argv, **p, *name;
277
278 if (mode == WIDTHMODE)
279 return;
280
281 v = ve->var;
282 if (ve->next != NULL || termwidth != UNLIMITED) {
283 if (ve->next == NULL) {
284 left = termwidth - (totwidth - v->width);
285 if (left < 1) /* already wrapped, just use std width */
286 left = v->width;
287 } else
288 left = v->width;
289 } else
290 left = -1;
291 if (needenv && kd) {
292 argv = kvm_getenvv2(kd, ki, termwidth);
293 if ((p = argv) != NULL) {
294 while (*p) {
295 fmt_puts(*p, &left);
296 p++;
297 fmt_putc(' ', &left);
298 }
299 }
300 }
301 if (needcomm) {
302 name = ki->p_comm;
303 if (!commandonly) {
304 argv = NULL;
305 if (!use_procfs)
306 argv = kvm_getargv2(kd, ki, termwidth);
307 else
308 argv = procfs_getargv(ki, termwidth);
309 if ((p = argv) != NULL) {
310 while (*p) {
311 fmt_puts(*p, &left);
312 p++;
313 fmt_putc(' ', &left);
314 }
315 }
316 /*
317 * append the real command name within
318 * parentheses, if the command name does
319 * not match the one in the argument vector
320 */
321 if (titlecmp(name, argv)) {
322 fmt_putc('(', &left);
323 fmt_puts(name, &left);
324 fmt_putc(')', &left);
325 }
326 if (use_procfs && argv) {
327 free(argv[0]);
328 free(argv);
329 }
330 } else {
331 fmt_puts(name, &left);
332 }
333 }
334 if (ve->next && left > 0)
335 printf("%*s", left, "");
336 }
337
338 void
339 ucomm(k, ve, mode)
340 struct kinfo_proc2 *k;
341 VARENT *ve;
342 int mode;
343 {
344 VAR *v;
345
346 v = ve->var;
347 strprintorsetwidth(v, k->p_comm, mode);
348 }
349
350 void
351 logname(k, ve, mode)
352 struct kinfo_proc2 *k;
353 VARENT *ve;
354 int mode;
355 {
356 VAR *v;
357
358 v = ve->var;
359 strprintorsetwidth(v, k->p_login, mode);
360 }
361
362 void
363 state(k, ve, mode)
364 struct kinfo_proc2 *k;
365 VARENT *ve;
366 int mode;
367 {
368 int flag, is_zombie;
369 char *cp;
370 VAR *v;
371 char buf[16];
372
373 is_zombie = 0;
374 v = ve->var;
375 flag = k->p_flag;
376 cp = buf;
377
378 switch (k->p_stat) {
379
380 case SSTOP:
381 *cp = 'T';
382 break;
383
384 case SSLEEP:
385 if (flag & P_SINTR) /* interuptable (long) */
386 *cp = k->p_slptime >= MAXSLP ? 'I' : 'S';
387 else
388 *cp = 'D';
389 break;
390
391 case SRUN:
392 case SIDL:
393 case SONPROC:
394 *cp = 'R';
395 break;
396
397 case SZOMB:
398 case SDEAD:
399 *cp = 'Z';
400 is_zombie = 1;
401 break;
402
403 default:
404 *cp = '?';
405 }
406 cp++;
407 if (flag & P_INMEM) {
408 } else
409 *cp++ = 'W';
410 if (k->p_nice < NZERO)
411 *cp++ = '<';
412 else if (k->p_nice > NZERO)
413 *cp++ = 'N';
414 if (flag & P_TRACED)
415 *cp++ = 'X';
416 if (flag & P_WEXIT && !is_zombie)
417 *cp++ = 'E';
418 if (flag & P_PPWAIT)
419 *cp++ = 'V';
420 if ((flag & P_SYSTEM) || k->p_holdcnt)
421 *cp++ = 'L';
422 if (k->p_eflag & EPROC_SLEADER)
423 *cp++ = 's';
424 if ((flag & P_CONTROLT) && k->p__pgid == k->p_tpgid)
425 *cp++ = '+';
426 *cp = '\0';
427 strprintorsetwidth(v, buf, mode);
428 }
429
430 void
431 pnice(k, ve, mode)
432 struct kinfo_proc2 *k;
433 VARENT *ve;
434 int mode;
435 {
436 VAR *v;
437
438 v = ve->var;
439 intprintorsetwidth(v, k->p_nice - NZERO, mode);
440 }
441
442 void
443 pri(k, ve, mode)
444 struct kinfo_proc2 *k;
445 VARENT *ve;
446 int mode;
447 {
448 VAR *v;
449
450 v = ve->var;
451 intprintorsetwidth(v, k->p_priority - PZERO, mode);
452 }
453
454 void
455 uname(k, ve, mode)
456 struct kinfo_proc2 *k;
457 VARENT *ve;
458 int mode;
459 {
460 VAR *v;
461
462 v = ve->var;
463 strprintorsetwidth(v, user_from_uid(k->p_uid, 0), mode);
464 }
465
466 void
467 runame(k, ve, mode)
468 struct kinfo_proc2 *k;
469 VARENT *ve;
470 int mode;
471 {
472 VAR *v;
473
474 v = ve->var;
475 strprintorsetwidth(v, user_from_uid(k->p_ruid, 0), mode);
476 }
477
478 void
479 tdev(k, ve, mode)
480 struct kinfo_proc2 *k;
481 VARENT *ve;
482 int mode;
483 {
484 VAR *v;
485 dev_t dev;
486 char buff[16];
487
488 v = ve->var;
489 dev = k->p_tdev;
490 if (dev == NODEV) {
491 /*
492 * Minimum width is width of header - we don't
493 * need to check it every time.
494 */
495 if (mode == PRINTMODE)
496 (void)printf("%*s", v->width, "??");
497 } else {
498 (void)snprintf(buff, sizeof(buff),
499 "%d/%d", major(dev), minor(dev));
500 strprintorsetwidth(v, buff, mode);
501 }
502 }
503
504 void
505 tname(k, ve, mode)
506 struct kinfo_proc2 *k;
507 VARENT *ve;
508 int mode;
509 {
510 VAR *v;
511 dev_t dev;
512 const char *ttname;
513 int noctty;
514
515 v = ve->var;
516 dev = k->p_tdev;
517 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) {
518 /*
519 * Minimum width is width of header - we don't
520 * need to check it every time.
521 */
522 if (mode == PRINTMODE)
523 (void)printf("%-*s", v->width, "??");
524 } else {
525 if (strncmp(ttname, "tty", 3) == 0 ||
526 strncmp(ttname, "dty", 3) == 0)
527 ttname += 3;
528 noctty = !(k->p_eflag & EPROC_CTTY) ? 1 : 0;
529 if (mode == WIDTHMODE) {
530 int fmtlen;
531
532 fmtlen = strlen(ttname) + noctty;
533 if (v->width < fmtlen)
534 v->width = fmtlen;
535 } else {
536 if (noctty)
537 printf("%-*s-", v->width - 1, ttname);
538 else
539 printf("%-*s", v->width, ttname);
540 }
541 }
542 }
543
544 void
545 longtname(k, ve, mode)
546 struct kinfo_proc2 *k;
547 VARENT *ve;
548 int mode;
549 {
550 VAR *v;
551 dev_t dev;
552 const char *ttname;
553
554 v = ve->var;
555 dev = k->p_tdev;
556 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL) {
557 /*
558 * Minimum width is width of header - we don't
559 * need to check it every time.
560 */
561 if (mode == PRINTMODE)
562 (void)printf("%-*s", v->width, "??");
563 }
564 else {
565 strprintorsetwidth(v, ttname, mode);
566 }
567 }
568
569 void
570 started(k, ve, mode)
571 struct kinfo_proc2 *k;
572 VARENT *ve;
573 int mode;
574 {
575 VAR *v;
576 static time_t now;
577 time_t startt;
578 struct tm *tp;
579 char buf[100], *cp;
580
581 /*
582 * XXX: The maximum width of this field is the same as the header
583 * "STARTED" for locales that have 3 letter abbreviated month
584 * names and 2 letter am/pm descriptions.
585 */
586 if (mode == WIDTHMODE)
587 return;
588
589 v = ve->var;
590 if (!k->p_uvalid) {
591 if (mode == PRINTMODE)
592 (void)printf("%*s", v->width, "-");
593 return;
594 }
595
596 startt = k->p_ustart_sec;
597 tp = localtime(&startt);
598 if (!now)
599 (void)time(&now);
600 if (now - k->p_ustart_sec < SECSPERDAY)
601 /* I *hate* SCCS... */
602 (void)strftime(buf, sizeof(buf) - 1, "%l:%" "M%p", tp);
603 else if (now - k->p_ustart_sec < DAYSPERWEEK * SECSPERDAY)
604 /* I *hate* SCCS... */
605 (void)strftime(buf, sizeof(buf) - 1, "%a%" "I%p", tp);
606 else
607 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
608 /* %e and %l can start with a space. */
609 cp = buf;
610 if (*cp == ' ')
611 cp++;
612 strprintorsetwidth(v, cp, mode);
613 }
614
615 void
616 lstarted(k, ve, mode)
617 struct kinfo_proc2 *k;
618 VARENT *ve;
619 int mode;
620 {
621 VAR *v;
622 time_t startt;
623 char buf[100];
624
625 v = ve->var;
626 if (!k->p_uvalid) {
627 /*
628 * Minimum width is less than header - we don't
629 * need to check it every time.
630 */
631 if (mode == PRINTMODE)
632 (void)printf("%*s", v->width, "-");
633 return;
634 }
635 startt = k->p_ustart_sec;
636
637 /* assume all times are the same length */
638 if (mode != WIDTHMODE || v->width == 0) {
639 (void)strftime(buf, sizeof(buf) -1, "%c",
640 localtime(&startt));
641 strprintorsetwidth(v, buf, mode);
642 }
643 }
644
645 void
646 wchan(k, ve, mode)
647 struct kinfo_proc2 *k;
648 VARENT *ve;
649 int mode;
650 {
651 VAR *v;
652 char *buf;
653
654 v = ve->var;
655 if (k->p_wchan) {
656 if (k->p_wmesg) {
657 strprintorsetwidth(v, k->p_wmesg, mode);
658 v->width = min(v->width, WMESGLEN);
659 } else {
660 (void)asprintf(&buf, "%-*llx", v->width,
661 (long long)k->p_wchan);
662 if (buf == NULL)
663 err(1, "%s", "");
664 strprintorsetwidth(v, buf, mode);
665 v->width = min(v->width, WMESGLEN);
666 free(buf);
667 }
668 } else {
669 if (mode == PRINTMODE)
670 (void)printf("%-*s", v->width, "-");
671 }
672 }
673
674 #define pgtok(a) (((a)*getpagesize())/1024)
675
676 void
677 vsize(k, ve, mode)
678 struct kinfo_proc2 *k;
679 VARENT *ve;
680 int mode;
681 {
682 VAR *v;
683
684 v = ve->var;
685 intprintorsetwidth(v,
686 pgtok(k->p_vm_dsize + k->p_vm_ssize + k->p_vm_tsize), mode);
687 }
688
689 void
690 rssize(k, ve, mode)
691 struct kinfo_proc2 *k;
692 VARENT *ve;
693 int mode;
694 {
695 VAR *v;
696
697 v = ve->var;
698 /* XXX don't have info about shared */
699 intprintorsetwidth(v, pgtok(k->p_vm_rssize), mode);
700 }
701
702 void
703 p_rssize(k, ve, mode) /* doesn't account for text */
704 struct kinfo_proc2 *k;
705 VARENT *ve;
706 int mode;
707 {
708 VAR *v;
709
710 v = ve->var;
711 intprintorsetwidth(v, pgtok(k->p_vm_rssize), mode);
712 }
713
714 void
715 cputime(k, ve, mode)
716 struct kinfo_proc2 *k;
717 VARENT *ve;
718 int mode;
719 {
720 VAR *v;
721 long secs;
722 long psecs; /* "parts" of a second. first micro, then centi */
723 int fmtlen;
724
725 v = ve->var;
726 if (P_ZOMBIE(k) || k->p_uvalid == 0) {
727 secs = 0;
728 psecs = 0;
729 } else {
730 /*
731 * This counts time spent handling interrupts. We could
732 * fix this, but it is not 100% trivial (and interrupt
733 * time fractions only work on the sparc anyway). XXX
734 */
735 secs = k->p_rtime_sec;
736 psecs = k->p_rtime_usec;
737 if (sumrusage) {
738 secs += k->p_uctime_sec;
739 psecs += k->p_uctime_usec;
740 }
741 /*
742 * round and scale to 100's
743 */
744 psecs = (psecs + 5000) / 10000;
745 secs += psecs / 100;
746 psecs = psecs % 100;
747 }
748 if (mode == WIDTHMODE) {
749 /*
750 * Ugg, this is the only field where a value of 0 longer
751 * than the column title, and log10(0) isn't good enough.
752 * Use SECSPERMIN, because secs is divided by that when
753 * passed to log10().
754 */
755 if (secs == 0 && v->longestp == 0)
756 secs = SECSPERMIN;
757 if (secs > v->longestp) {
758 /* "+6" for the "%02ld.%02ld" in the printf() below */
759 fmtlen = (int)log10((double)secs / SECSPERMIN) + 1 + 6;
760 v->longestp = secs;
761 if (fmtlen > v->width)
762 v->width = fmtlen;
763 }
764 } else {
765 printf("%*ld:%02ld.%02ld", v->width - 6, secs / SECSPERMIN,
766 secs % SECSPERMIN, psecs);
767 }
768 }
769
770 double
771 getpcpu(k)
772 struct kinfo_proc2 *k;
773 {
774 static int failure;
775
776 if (!nlistread)
777 failure = (kd) ? donlist() : 1;
778 if (failure)
779 return (0.0);
780
781 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
782
783 /* XXX - I don't like this */
784 if (k->p_swtime == 0 || (k->p_flag & P_INMEM) == 0 ||
785 k->p_stat == SZOMB || k->p_stat == SDEAD)
786 return (0.0);
787 if (rawcpu)
788 return (100.0 * fxtofl(k->p_pctcpu));
789 return (100.0 * fxtofl(k->p_pctcpu) /
790 (1.0 - exp(k->p_swtime * log(ccpu))));
791 }
792
793 void
794 pcpu(k, ve, mode)
795 struct kinfo_proc2 *k;
796 VARENT *ve;
797 int mode;
798 {
799 VAR *v;
800
801 v = ve->var;
802 doubleprintorsetwidth(v, getpcpu(k), 1, mode);
803 }
804
805 double
806 getpmem(k)
807 struct kinfo_proc2 *k;
808 {
809 static int failure;
810 double fracmem;
811 int szptudot;
812
813 if (!nlistread)
814 failure = (kd) ? donlist() : 1;
815 if (failure)
816 return (0.0);
817
818 if ((k->p_flag & P_INMEM) == 0)
819 return (0.0);
820 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
821 szptudot = USPACE/getpagesize();
822 /* XXX don't have info about shared */
823 fracmem = ((float)k->p_vm_rssize + szptudot)/mempages;
824 return (100.0 * fracmem);
825 }
826
827 void
828 pmem(k, ve, mode)
829 struct kinfo_proc2 *k;
830 VARENT *ve;
831 int mode;
832 {
833 VAR *v;
834
835 v = ve->var;
836 doubleprintorsetwidth(v, getpmem(k), 1, mode);
837 }
838
839 void
840 pagein(k, ve, mode)
841 struct kinfo_proc2 *k;
842 VARENT *ve;
843 int mode;
844 {
845 VAR *v;
846
847 v = ve->var;
848 intprintorsetwidth(v, k->p_uvalid ? k->p_uru_majflt : 0, mode);
849 }
850
851 void
852 maxrss(k, ve, mode)
853 struct kinfo_proc2 *k;
854 VARENT *ve;
855 int mode;
856 {
857 VAR *v;
858
859 v = ve->var;
860 /* No need to check width! */
861 if (mode == PRINTMODE)
862 (void)printf("%*s", v->width, "-");
863 }
864
865 void
866 tsize(k, ve, mode)
867 struct kinfo_proc2 *k;
868 VARENT *ve;
869 int mode;
870 {
871 VAR *v;
872
873 v = ve->var;
874 intprintorsetwidth(v, pgtok(k->p_vm_tsize), mode);
875 }
876
877 /*
878 * Generic output routines. Print fields from various prototype
879 * structures.
880 */
881 static void
882 printval(bp, v, mode)
883 void *bp;
884 VAR *v;
885 int mode;
886 {
887 static char ofmt[32] = "%";
888 int width, vok, fmtlen;
889 char *fcp, *cp, *obuf;
890 enum type type;
891 long long val;
892 unsigned long long uval;
893
894 /*
895 * Note that the "INF127" check is nonsensical for types
896 * that are or can be signed.
897 */
898 #define GET(type) (*(type *)bp)
899 #define CHK_INF127(n) (((n) > 127) && (v->flag & INF127) ? 127 : (n))
900
901 #define VSIGN 1
902 #define VUNSIGN 2
903 #define VPTR 3
904
905 if (mode == WIDTHMODE) {
906 vok = 0;
907 switch (v->type) {
908 case CHAR:
909 val = GET(char);
910 vok = VSIGN;
911 break;
912 case UCHAR:
913 uval = CHK_INF127(GET(u_char));
914 vok = VUNSIGN;
915 break;
916 case SHORT:
917 val = GET(short);
918 vok = VSIGN;
919 break;
920 case USHORT:
921 uval = CHK_INF127(GET(u_short));
922 vok = VUNSIGN;
923 break;
924 case INT32:
925 val = GET(int32_t);
926 vok = VSIGN;
927 break;
928 case INT:
929 val = GET(int);
930 vok = VSIGN;
931 break;
932 case UINT:
933 case UINT32:
934 uval = CHK_INF127(GET(u_int));
935 vok = VUNSIGN;
936 break;
937 case LONG:
938 val = GET(long);
939 vok = VSIGN;
940 break;
941 case ULONG:
942 uval = CHK_INF127(GET(u_long));
943 vok = VUNSIGN;
944 break;
945 case KPTR:
946 uval = GET(u_long);
947 vok = VPTR;
948 break;
949 case KPTR24:
950 uval = GET(u_long) & 0xffffff;
951 vok = VPTR;
952 break;
953 default:
954 /* nothing... */;
955 }
956 switch (vok) {
957 case VSIGN:
958 if (val < 0 && val < v->longestn) {
959 fmtlen = (int)log10((double)-val) + 2;
960 v->longestn = val;
961 if (fmtlen > v->width)
962 v->width = fmtlen;
963 } else if (val > 0 && val > v->longestp) {
964 fmtlen = (int)log10((double)val) + 1;
965 v->longestp = val;
966 if (fmtlen > v->width)
967 v->width = fmtlen;
968 }
969 return;
970 case VUNSIGN:
971 if (uval > v->longestu) {
972 fmtlen = (int)log10((double)uval) + 1;
973 v->longestu = uval;
974 v->width = fmtlen;
975 }
976 return;
977 case VPTR:
978 fmtlen = 0;
979 while (uval > 0) {
980 uval >>= 4;
981 fmtlen++;
982 }
983 if (fmtlen > v->width)
984 v->width = fmtlen;
985 return;
986 }
987 }
988
989 width = v->width;
990 cp = ofmt + 1;
991 fcp = v->fmt;
992 if (v->flag & LJUST)
993 *cp++ = '-';
994 *cp++ = '*';
995 while ((*cp++ = *fcp++) != '\0')
996 continue;
997
998 switch (v->type) {
999 case INT32:
1000 if (sizeof(int32_t) == sizeof(int))
1001 type = INT;
1002 else if (sizeof(int32_t) == sizeof(long))
1003 type = LONG;
1004 else
1005 errx(1, "unknown conversion for type %d", v->type);
1006 break;
1007 case UINT32:
1008 if (sizeof(u_int32_t) == sizeof(u_int))
1009 type = UINT;
1010 else if (sizeof(u_int32_t) == sizeof(u_long))
1011 type = ULONG;
1012 else
1013 errx(1, "unknown conversion for type %d", v->type);
1014 break;
1015 default:
1016 type = v->type;
1017 break;
1018 }
1019
1020 switch (type) {
1021 case CHAR:
1022 (void)printf(ofmt, width, GET(char));
1023 return;
1024 case UCHAR:
1025 (void)printf(ofmt, width, CHK_INF127(GET(u_char)));
1026 return;
1027 case SHORT:
1028 (void)printf(ofmt, width, GET(short));
1029 return;
1030 case USHORT:
1031 (void)printf(ofmt, width, CHK_INF127(GET(u_short)));
1032 return;
1033 case INT:
1034 (void)printf(ofmt, width, GET(int));
1035 return;
1036 case UINT:
1037 (void)printf(ofmt, width, CHK_INF127(GET(u_int)));
1038 return;
1039 case LONG:
1040 (void)printf(ofmt, width, GET(long));
1041 return;
1042 case ULONG:
1043 (void)printf(ofmt, width, CHK_INF127(GET(u_long)));
1044 return;
1045 case KPTR:
1046 (void)printf(ofmt, width, GET(u_long));
1047 return;
1048 case KPTR24:
1049 (void)printf(ofmt, width, GET(u_long) & 0xffffff);
1050 return;
1051 case SIGLIST:
1052 {
1053 sigset_t *s = (sigset_t *)(void *)bp;
1054 size_t i;
1055 #define SIGSETSIZE (sizeof(s->__bits) / sizeof(s->__bits[0]))
1056 char buf[SIGSETSIZE * 8 + 1];
1057
1058 for (i = 0; i < SIGSETSIZE; i++)
1059 (void)snprintf(&buf[i * 8], 9, "%.8x",
1060 s->__bits[(SIGSETSIZE - 1) - i]);
1061
1062 /* Skip leading zeroes */
1063 for (i = 0; buf[i]; i++)
1064 if (buf[i] != '0')
1065 break;
1066
1067 if (buf[i] == '\0')
1068 i--;
1069 (void)asprintf(&obuf, ofmt, width, &buf[i]);
1070 }
1071 break;
1072 default:
1073 errx(1, "unknown type %d", v->type);
1074 }
1075 if (obuf == NULL)
1076 err(1, "%s", "");
1077 if (mode == WIDTHMODE) {
1078 /* Skip leading spaces. */
1079 cp = strrchr(obuf, ' ');
1080 if (cp == NULL)
1081 cp = obuf;
1082 else
1083 cp++; /* skip last space */
1084 }
1085 else
1086 cp = obuf;
1087 strprintorsetwidth(v, cp, mode);
1088 free(obuf);
1089 #undef GET
1090 #undef CHK_INF127
1091 }
1092
1093 void
1094 pvar(k, ve, mode)
1095 struct kinfo_proc2 *k;
1096 VARENT *ve;
1097 int mode;
1098 {
1099 VAR *v;
1100
1101 v = ve->var;
1102 printval((char *)k + v->off, v, mode);
1103 }
1104