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