print.c revision 1.45 1 /* $NetBSD: print.c,v 1.45 1999/10/15 19:31:24 jdolecek Exp $ */
2
3 /*-
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94";
40 #else
41 __RCSID("$NetBSD: print.c,v 1.45 1999/10/15 19:31:24 jdolecek Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/time.h>
47 #include <sys/resource.h>
48 #include <sys/proc.h>
49 #include <sys/stat.h>
50 #include <sys/ucred.h>
51 #include <sys/sysctl.h>
52
53 #include <vm/vm.h>
54
55 #include <err.h>
56 #include <kvm.h>
57 #include <math.h>
58 #include <nlist.h>
59 #include <pwd.h>
60 #include <stddef.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <time.h>
65 #include <tzfile.h>
66 #include <unistd.h>
67
68 #include "ps.h"
69
70 extern kvm_t *kd;
71 extern int needenv, needcomm, commandonly, dontuseprocfs;
72
73 static char *cmdpart __P((char *));
74 static void printval __P((char *, VAR *));
75 static int titlecmp __P((char *, char **));
76
77 #define min(a,b) ((a) <= (b) ? (a) : (b))
78 #define max(a,b) ((a) >= (b) ? (a) : (b))
79
80 static char *
81 cmdpart(arg0)
82 char *arg0;
83 {
84 char *cp;
85
86 return ((cp = strrchr(arg0, '/')) != NULL ? cp + 1 : arg0);
87 }
88
89 void
90 printheader()
91 {
92 VAR *v;
93 struct varent *vent;
94
95 for (vent = vhead; vent; vent = vent->next) {
96 v = vent->var;
97 if (v->flag & LJUST) {
98 if (vent->next == NULL) /* last one */
99 (void)printf("%s", v->header);
100 else
101 (void)printf("%-*s", v->width, v->header);
102 } else
103 (void)printf("%*s", v->width, v->header);
104 if (vent->next != NULL)
105 (void)putchar(' ');
106 }
107 (void)putchar('\n');
108 }
109
110 static int
111 titlecmp(name, argv)
112 char *name;
113 char **argv;
114 {
115 char *title;
116 int namelen;
117
118 if (argv == 0 || argv[0] == 0)
119 return (1);
120
121 title = cmdpart(argv[0]);
122
123 if (!strcmp(name, title))
124 return (0);
125
126 if (title[0] == '-' && !strcmp(name, title+1))
127 return (0);
128
129 namelen = strlen(name);
130
131 if (argv[1] == 0 &&
132 !strncmp(name, title, namelen) &&
133 title[namelen+0] == ':' &&
134 title[namelen+1] == ' ')
135 return (0);
136
137 return (1);
138 }
139
140 void
141 command(ki, ve)
142 KINFO *ki;
143 VARENT *ve;
144 {
145 VAR *v;
146 int left;
147 static int use_procfs=0;
148 char **argv, **p, *name;
149
150 v = ve->var;
151 if (ve->next != NULL || termwidth != UNLIMITED) {
152 if (ve->next == NULL) {
153 left = termwidth - (totwidth - v->width);
154 if (left < 1) /* already wrapped, just use std width */
155 left = v->width;
156 } else
157 left = v->width;
158 } else
159 left = -1;
160 if (needenv && kd) {
161 argv = kvm_getenvv(kd, ki->ki_p, termwidth);
162 if ((p = argv) != NULL) {
163 while (*p) {
164 fmt_puts(*p, &left);
165 p++;
166 fmt_putc(' ', &left);
167 }
168 }
169 }
170 if (needcomm) {
171 name = KI_PROC(ki)->p_comm;
172 if (!commandonly) {
173 argv = NULL;
174 if (kd && !use_procfs)
175 argv = kvm_getargv(kd, ki->ki_p, termwidth);
176 if (argv == NULL && !dontuseprocfs) {
177 argv = procfs_getargv(ki->ki_p, termwidth);
178 use_procfs = 1;
179 }
180 if ((p = argv) != NULL) {
181 while (*p) {
182 fmt_puts(*p, &left);
183 p++;
184 fmt_putc(' ', &left);
185 }
186 }
187 if (titlecmp(name, argv)) {
188 fmt_putc('(', &left);
189 fmt_puts(name, &left);
190 fmt_putc(')', &left);
191 }
192 if (use_procfs) {
193 free(argv[0]);
194 free(argv);
195 }
196 } else {
197 fmt_puts(name, &left);
198 }
199 }
200 if (ve->next && left > 0)
201 printf("%*s", left, "");
202 }
203
204 void
205 ucomm(k, ve)
206 KINFO *k;
207 VARENT *ve;
208 {
209 VAR *v;
210
211 v = ve->var;
212 (void)printf("%-*s", v->width, KI_PROC(k)->p_comm);
213 }
214
215 void
216 logname(k, ve)
217 KINFO *k;
218 VARENT *ve;
219 {
220 VAR *v;
221 int n;
222
223 v = ve->var;
224 n = min(v->width, MAXLOGNAME);
225 (void)printf("%-*.*s", n, n, KI_EPROC(k)->e_login);
226 if (v->width > n)
227 (void)printf("%*s", v->width - n, "");
228 }
229
230 void
231 state(k, ve)
232 KINFO *k;
233 VARENT *ve;
234 {
235 struct proc *p;
236 int flag;
237 char *cp;
238 VAR *v;
239 char buf[16];
240
241 v = ve->var;
242 p = KI_PROC(k);
243 flag = p->p_flag;
244 cp = buf;
245
246 switch (p->p_stat) {
247
248 case SSTOP:
249 *cp = 'T';
250 break;
251
252 case SSLEEP:
253 if (flag & P_SINTR) /* interuptable (long) */
254 *cp = p->p_slptime >= MAXSLP ? 'I' : 'S';
255 else
256 *cp = 'D';
257 break;
258
259 case SRUN:
260 case SIDL:
261 *cp = 'R';
262 break;
263
264 case SZOMB:
265 case SDEAD:
266 *cp = 'Z';
267 break;
268
269 default:
270 *cp = '?';
271 }
272 cp++;
273 if (flag & P_INMEM) {
274 } else
275 *cp++ = 'W';
276 if (p->p_nice < NZERO)
277 *cp++ = '<';
278 else if (p->p_nice > NZERO)
279 *cp++ = 'N';
280 if (flag & P_TRACED)
281 *cp++ = 'X';
282 if (flag & P_WEXIT && P_ZOMBIE(p) == 0)
283 *cp++ = 'E';
284 if (flag & P_PPWAIT)
285 *cp++ = 'V';
286 if ((flag & P_SYSTEM) || p->p_holdcnt)
287 *cp++ = 'L';
288 if (KI_EPROC(k)->e_flag & EPROC_SLEADER)
289 *cp++ = 's';
290 if ((flag & P_CONTROLT) && KI_EPROC(k)->e_pgid == KI_EPROC(k)->e_tpgid)
291 *cp++ = '+';
292 *cp = '\0';
293 (void)printf("%-*s", v->width, buf);
294 }
295
296 void
297 pnice(k, ve)
298 KINFO *k;
299 VARENT *ve;
300 {
301 VAR *v;
302
303 v = ve->var;
304 (void)printf("%*d", v->width, KI_PROC(k)->p_nice - NZERO);
305 }
306
307 void
308 pri(k, ve)
309 KINFO *k;
310 VARENT *ve;
311 {
312 VAR *v;
313
314 v = ve->var;
315 (void)printf("%*d", v->width, KI_PROC(k)->p_priority - PZERO);
316 }
317
318 void
319 uname(k, ve)
320 KINFO *k;
321 VARENT *ve;
322 {
323 VAR *v;
324
325 v = ve->var;
326 (void)printf("%-*s",
327 (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0));
328 }
329
330 void
331 runame(k, ve)
332 KINFO *k;
333 VARENT *ve;
334 {
335 VAR *v;
336
337 v = ve->var;
338 (void)printf("%-*s",
339 (int)v->width, user_from_uid(KI_EPROC(k)->e_pcred.p_ruid, 0));
340 }
341
342 void
343 tdev(k, ve)
344 KINFO *k;
345 VARENT *ve;
346 {
347 VAR *v;
348 dev_t dev;
349 char buff[16];
350
351 v = ve->var;
352 dev = KI_EPROC(k)->e_tdev;
353 if (dev == NODEV)
354 (void)printf("%*s", v->width, "??");
355 else {
356 (void)snprintf(buff, sizeof(buff),
357 "%d/%d", major(dev), minor(dev));
358 (void)printf("%*s", v->width, buff);
359 }
360 }
361
362 void
363 tname(k, ve)
364 KINFO *k;
365 VARENT *ve;
366 {
367 VAR *v;
368 dev_t dev;
369 const char *ttname;
370
371 v = ve->var;
372 dev = KI_EPROC(k)->e_tdev;
373 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
374 (void)printf("%-*s", v->width, "??");
375 else {
376 if (strncmp(ttname, "tty", 3) == 0 ||
377 strncmp(ttname, "dty", 3) == 0)
378 ttname += 3;
379 (void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
380 KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-');
381 }
382 }
383
384 void
385 longtname(k, ve)
386 KINFO *k;
387 VARENT *ve;
388 {
389 VAR *v;
390 dev_t dev;
391 const char *ttname;
392
393 v = ve->var;
394 dev = KI_EPROC(k)->e_tdev;
395 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
396 (void)printf("%-*s", v->width, "??");
397 else
398 (void)printf("%-*s", v->width, ttname);
399 }
400
401 void
402 started(k, ve)
403 KINFO *k;
404 VARENT *ve;
405 {
406 VAR *v;
407 static time_t now;
408 time_t startt;
409 struct tm *tp;
410 char buf[100];
411
412 v = ve->var;
413 if (!k->ki_u.u_valid) {
414 (void)printf("%-*s", v->width, "-");
415 return;
416 }
417
418 startt = k->ki_u.u_start.tv_sec;
419 tp = localtime(&startt);
420 if (!now)
421 (void)time(&now);
422 if (now - k->ki_u.u_start.tv_sec < 24 * SECSPERHOUR) {
423 /* I *hate* SCCS... */
424 static char fmt[] = __CONCAT("%l:%", "M%p");
425 (void)strftime(buf, sizeof(buf) - 1, fmt, tp);
426 } else if (now - k->ki_u.u_start.tv_sec < 7 * SECSPERDAY) {
427 /* I *hate* SCCS... */
428 static char fmt[] = __CONCAT("%a%", "I%p");
429 (void)strftime(buf, sizeof(buf) - 1, fmt, tp);
430 } else
431 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
432 (void)printf("%-*s", v->width, buf);
433 }
434
435 void
436 lstarted(k, ve)
437 KINFO *k;
438 VARENT *ve;
439 {
440 VAR *v;
441 time_t startt;
442 char buf[100];
443
444 v = ve->var;
445 if (!k->ki_u.u_valid) {
446 (void)printf("%-*s", v->width, "-");
447 return;
448 }
449 startt = k->ki_u.u_start.tv_sec;
450 (void)strftime(buf, sizeof(buf) -1, "%c",
451 localtime(&startt));
452 (void)printf("%-*s", v->width, buf);
453 }
454
455 void
456 wchan(k, ve)
457 KINFO *k;
458 VARENT *ve;
459 {
460 VAR *v;
461 int n;
462
463 v = ve->var;
464 if (KI_PROC(k)->p_wchan) {
465 if (KI_PROC(k)->p_wmesg) {
466 n = min(v->width, WMESGLEN);
467 (void)printf("%-*.*s", n, n, KI_EPROC(k)->e_wmesg);
468 if (v->width > n)
469 (void)printf("%*s", v->width - n, "");
470 } else
471 (void)printf("%-*lx", v->width,
472 (long)KI_PROC(k)->p_wchan);
473 } else
474 (void)printf("%-*s", v->width, "-");
475 }
476
477 #define pgtok(a) (((a)*getpagesize())/1024)
478
479 void
480 vsize(k, ve)
481 KINFO *k;
482 VARENT *ve;
483 {
484 VAR *v;
485
486 v = ve->var;
487 (void)printf("%*d", v->width,
488 pgtok(KI_EPROC(k)->e_vm.vm_dsize + KI_EPROC(k)->e_vm.vm_ssize +
489 KI_EPROC(k)->e_vm.vm_tsize));
490 }
491
492 void
493 rssize(k, ve)
494 KINFO *k;
495 VARENT *ve;
496 {
497 VAR *v;
498
499 v = ve->var;
500 /* XXX don't have info about shared */
501 (void)printf("%*d", v->width, pgtok(KI_EPROC(k)->e_vm.vm_rssize));
502 }
503
504 void
505 p_rssize(k, ve) /* doesn't account for text */
506 KINFO *k;
507 VARENT *ve;
508 {
509 VAR *v;
510
511 v = ve->var;
512 (void)printf("%*d", v->width, pgtok(KI_EPROC(k)->e_vm.vm_rssize));
513 }
514
515 void
516 cputime(k, ve)
517 KINFO *k;
518 VARENT *ve;
519 {
520 VAR *v;
521 long secs;
522 long psecs; /* "parts" of a second. first micro, then centi */
523 char obuff[128];
524
525 v = ve->var;
526 if (P_ZOMBIE(KI_PROC(k)) || k->ki_u.u_valid == 0) {
527 secs = 0;
528 psecs = 0;
529 } else {
530 /*
531 * This counts time spent handling interrupts. We could
532 * fix this, but it is not 100% trivial (and interrupt
533 * time fractions only work on the sparc anyway). XXX
534 */
535 secs = KI_PROC(k)->p_rtime.tv_sec;
536 psecs = KI_PROC(k)->p_rtime.tv_usec;
537 if (sumrusage) {
538 secs += k->ki_u.u_cru.ru_utime.tv_sec +
539 k->ki_u.u_cru.ru_stime.tv_sec;
540 psecs += k->ki_u.u_cru.ru_utime.tv_usec +
541 k->ki_u.u_cru.ru_stime.tv_usec;
542 }
543 /*
544 * round and scale to 100's
545 */
546 psecs = (psecs + 5000) / 10000;
547 secs += psecs / 100;
548 psecs = psecs % 100;
549 }
550 (void)snprintf(obuff, sizeof(obuff),
551 "%3ld:%02ld.%02ld", secs/60, secs%60, psecs);
552 (void)printf("%*s", v->width, obuff);
553 }
554
555 double
556 getpcpu(k)
557 KINFO *k;
558 {
559 struct proc *p;
560 static int failure;
561
562 if (!nlistread)
563 failure = (kd) ? donlist() : 1;
564 if (failure)
565 return (0.0);
566
567 p = KI_PROC(k);
568 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
569
570 /* XXX - I don't like this */
571 if (p->p_swtime == 0 || (p->p_flag & P_INMEM) == 0 ||
572 P_ZOMBIE(p))
573 return (0.0);
574 if (rawcpu)
575 return (100.0 * fxtofl(p->p_pctcpu));
576 return (100.0 * fxtofl(p->p_pctcpu) /
577 (1.0 - exp(p->p_swtime * log(fxtofl(ccpu)))));
578 }
579
580 void
581 pcpu(k, ve)
582 KINFO *k;
583 VARENT *ve;
584 {
585 VAR *v;
586
587 v = ve->var;
588 (void)printf("%*.1f", v->width, getpcpu(k));
589 }
590
591 double
592 getpmem(k)
593 KINFO *k;
594 {
595 static int failure;
596 struct proc *p;
597 struct eproc *e;
598 double fracmem;
599 int szptudot;
600
601 if (!nlistread)
602 failure = (kd) ? donlist() : 1;
603 if (failure)
604 return (0.0);
605
606 p = KI_PROC(k);
607 e = KI_EPROC(k);
608 if ((p->p_flag & P_INMEM) == 0)
609 return (0.0);
610 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
611 szptudot = USPACE/getpagesize();
612 /* XXX don't have info about shared */
613 fracmem = ((float)e->e_vm.vm_rssize + szptudot)/CLSIZE/mempages;
614 return (100.0 * fracmem);
615 }
616
617 void
618 pmem(k, ve)
619 KINFO *k;
620 VARENT *ve;
621 {
622 VAR *v;
623
624 v = ve->var;
625 (void)printf("%*.1f", v->width, getpmem(k));
626 }
627
628 void
629 pagein(k, ve)
630 KINFO *k;
631 VARENT *ve;
632 {
633 VAR *v;
634
635 v = ve->var;
636 (void)printf("%*ld", v->width,
637 k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0);
638 }
639
640 void
641 maxrss(k, ve)
642 KINFO *k;
643 VARENT *ve;
644 {
645 VAR *v;
646
647 v = ve->var;
648 (void)printf("%*s", v->width, "-");
649 }
650
651 void
652 tsize(k, ve)
653 KINFO *k;
654 VARENT *ve;
655 {
656 VAR *v;
657
658 v = ve->var;
659 (void)printf("%*d", v->width, pgtok(KI_EPROC(k)->e_vm.vm_tsize));
660 }
661
662 /*
663 * Generic output routines. Print fields from various prototype
664 * structures.
665 */
666 static void
667 printval(bp, v)
668 char *bp;
669 VAR *v;
670 {
671 static char ofmt[32] = "%";
672 char *fcp, *cp;
673 enum type type;
674
675 cp = ofmt + 1;
676 fcp = v->fmt;
677 if (v->flag & LJUST)
678 *cp++ = '-';
679 *cp++ = '*';
680 while ((*cp++ = *fcp++) != '\0')
681 continue;
682
683 /*
684 * Note that the "INF127" check is nonsensical for types
685 * that are or can be signed.
686 */
687 #define GET(type) (*(type *)bp)
688 #define CHK_INF127(n) (((n) > 127) && (v->flag & INF127) ? 127 : (n))
689
690 switch (v->type) {
691 case INT32:
692 if (sizeof(int32_t) == sizeof(int))
693 type = INT;
694 else if (sizeof(int32_t) == sizeof(long))
695 type = LONG;
696 else
697 errx(1, "unknown conversion for type %d", v->type);
698 break;
699 case UINT32:
700 if (sizeof(u_int32_t) == sizeof(u_int))
701 type = UINT;
702 else if (sizeof(u_int32_t) == sizeof(u_long))
703 type = ULONG;
704 else
705 errx(1, "unknown conversion for type %d", v->type);
706 break;
707 default:
708 type = v->type;
709 break;
710 }
711
712 switch (type) {
713 case CHAR:
714 (void)printf(ofmt, v->width, GET(char));
715 break;
716 case UCHAR:
717 (void)printf(ofmt, v->width, CHK_INF127(GET(u_char)));
718 break;
719 case SHORT:
720 (void)printf(ofmt, v->width, GET(short));
721 break;
722 case USHORT:
723 (void)printf(ofmt, v->width, CHK_INF127(GET(u_short)));
724 break;
725 case INT:
726 (void)printf(ofmt, v->width, GET(int));
727 break;
728 case UINT:
729 (void)printf(ofmt, v->width, CHK_INF127(GET(u_int)));
730 break;
731 case LONG:
732 (void)printf(ofmt, v->width, GET(long));
733 break;
734 case ULONG:
735 (void)printf(ofmt, v->width, CHK_INF127(GET(u_long)));
736 break;
737 case KPTR:
738 (void)printf(ofmt, v->width, GET(u_long));
739 break;
740 case KPTR24:
741 (void)printf(ofmt, v->width, GET(u_long) & 0xffffff);
742 break;
743 case SIGLIST:
744 {
745 sigset_t *s = (sigset_t *)(void *)bp;
746 size_t i;
747 #define SIGSETSIZE (sizeof(s->__bits) / sizeof(s->__bits[0]))
748 char buf[SIGSETSIZE * 8 + 1];
749
750 for (i = 0; i < SIGSETSIZE; i++)
751 (void)snprintf(&buf[i * 8], 9, "%.8x",
752 s->__bits[(SIGSETSIZE - 1) - i]);
753
754 /* Skip leading zeroes */
755 for (i = 0; buf[i]; i++)
756 if (buf[i] != '0')
757 break;
758
759 if (buf[i] == '\0')
760 i--;
761 (void)printf(ofmt, v->width, &buf[i]);
762 }
763 break;
764 default:
765 errx(1, "unknown type %d", v->type);
766 }
767 #undef GET
768 #undef CHK_INF127
769 }
770
771 void
772 pvar(k, ve)
773 KINFO *k;
774 VARENT *ve;
775 {
776 VAR *v;
777
778 v = ve->var;
779 printval((char *)KI_PROC(k) + v->off, v);
780 }
781
782 void
783 evar(k, ve)
784 KINFO *k;
785 VARENT *ve;
786 {
787 VAR *v;
788
789 v = ve->var;
790 printval((char *)KI_EPROC(k) + v->off, v);
791 }
792
793 void
794 uvar(k, ve)
795 KINFO *k;
796 VARENT *ve;
797 {
798 VAR *v;
799
800 v = ve->var;
801 if (k->ki_u.u_valid)
802 printval((char *)&k->ki_u + v->off, v);
803 else
804 (void)printf("%*s", v->width, "-");
805 }
806
807 void
808 rvar(k, ve)
809 KINFO *k;
810 VARENT *ve;
811 {
812 VAR *v;
813
814 v = ve->var;
815 if (k->ki_u.u_valid)
816 printval((char *)&k->ki_u.u_ru + v->off, v);
817 else
818 (void)printf("%*s", v->width, "-");
819 }
820