print.c revision 1.46 1 /* $NetBSD: print.c,v 1.46 1999/10/15 20:39:52 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.46 1999/10/15 20:39:52 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, use_procfs;
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 char **argv, **p, *name;
148
149 v = ve->var;
150 if (ve->next != NULL || termwidth != UNLIMITED) {
151 if (ve->next == NULL) {
152 left = termwidth - (totwidth - v->width);
153 if (left < 1) /* already wrapped, just use std width */
154 left = v->width;
155 } else
156 left = v->width;
157 } else
158 left = -1;
159 if (needenv && kd) {
160 argv = kvm_getenvv(kd, ki->ki_p, termwidth);
161 if ((p = argv) != NULL) {
162 while (*p) {
163 fmt_puts(*p, &left);
164 p++;
165 fmt_putc(' ', &left);
166 }
167 }
168 }
169 if (needcomm) {
170 name = KI_PROC(ki)->p_comm;
171 if (!commandonly) {
172 argv = NULL;
173 if (!use_procfs)
174 argv = kvm_getargv(kd, ki->ki_p, termwidth);
175 else
176 argv = procfs_getargv(ki->ki_p, termwidth);
177 if ((p = argv) != NULL) {
178 while (*p) {
179 fmt_puts(*p, &left);
180 p++;
181 fmt_putc(' ', &left);
182 }
183 }
184 if (titlecmp(name, argv)) {
185 fmt_putc('(', &left);
186 fmt_puts(name, &left);
187 fmt_putc(')', &left);
188 }
189 if (use_procfs && argv) {
190 free(argv[0]);
191 free(argv);
192 }
193 } else {
194 fmt_puts(name, &left);
195 }
196 }
197 if (ve->next && left > 0)
198 printf("%*s", left, "");
199 }
200
201 void
202 ucomm(k, ve)
203 KINFO *k;
204 VARENT *ve;
205 {
206 VAR *v;
207
208 v = ve->var;
209 (void)printf("%-*s", v->width, KI_PROC(k)->p_comm);
210 }
211
212 void
213 logname(k, ve)
214 KINFO *k;
215 VARENT *ve;
216 {
217 VAR *v;
218 int n;
219
220 v = ve->var;
221 n = min(v->width, MAXLOGNAME);
222 (void)printf("%-*.*s", n, n, KI_EPROC(k)->e_login);
223 if (v->width > n)
224 (void)printf("%*s", v->width - n, "");
225 }
226
227 void
228 state(k, ve)
229 KINFO *k;
230 VARENT *ve;
231 {
232 struct proc *p;
233 int flag;
234 char *cp;
235 VAR *v;
236 char buf[16];
237
238 v = ve->var;
239 p = KI_PROC(k);
240 flag = p->p_flag;
241 cp = buf;
242
243 switch (p->p_stat) {
244
245 case SSTOP:
246 *cp = 'T';
247 break;
248
249 case SSLEEP:
250 if (flag & P_SINTR) /* interuptable (long) */
251 *cp = p->p_slptime >= MAXSLP ? 'I' : 'S';
252 else
253 *cp = 'D';
254 break;
255
256 case SRUN:
257 case SIDL:
258 *cp = 'R';
259 break;
260
261 case SZOMB:
262 case SDEAD:
263 *cp = 'Z';
264 break;
265
266 default:
267 *cp = '?';
268 }
269 cp++;
270 if (flag & P_INMEM) {
271 } else
272 *cp++ = 'W';
273 if (p->p_nice < NZERO)
274 *cp++ = '<';
275 else if (p->p_nice > NZERO)
276 *cp++ = 'N';
277 if (flag & P_TRACED)
278 *cp++ = 'X';
279 if (flag & P_WEXIT && P_ZOMBIE(p) == 0)
280 *cp++ = 'E';
281 if (flag & P_PPWAIT)
282 *cp++ = 'V';
283 if ((flag & P_SYSTEM) || p->p_holdcnt)
284 *cp++ = 'L';
285 if (KI_EPROC(k)->e_flag & EPROC_SLEADER)
286 *cp++ = 's';
287 if ((flag & P_CONTROLT) && KI_EPROC(k)->e_pgid == KI_EPROC(k)->e_tpgid)
288 *cp++ = '+';
289 *cp = '\0';
290 (void)printf("%-*s", v->width, buf);
291 }
292
293 void
294 pnice(k, ve)
295 KINFO *k;
296 VARENT *ve;
297 {
298 VAR *v;
299
300 v = ve->var;
301 (void)printf("%*d", v->width, KI_PROC(k)->p_nice - NZERO);
302 }
303
304 void
305 pri(k, ve)
306 KINFO *k;
307 VARENT *ve;
308 {
309 VAR *v;
310
311 v = ve->var;
312 (void)printf("%*d", v->width, KI_PROC(k)->p_priority - PZERO);
313 }
314
315 void
316 uname(k, ve)
317 KINFO *k;
318 VARENT *ve;
319 {
320 VAR *v;
321
322 v = ve->var;
323 (void)printf("%-*s",
324 (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0));
325 }
326
327 void
328 runame(k, ve)
329 KINFO *k;
330 VARENT *ve;
331 {
332 VAR *v;
333
334 v = ve->var;
335 (void)printf("%-*s",
336 (int)v->width, user_from_uid(KI_EPROC(k)->e_pcred.p_ruid, 0));
337 }
338
339 void
340 tdev(k, ve)
341 KINFO *k;
342 VARENT *ve;
343 {
344 VAR *v;
345 dev_t dev;
346 char buff[16];
347
348 v = ve->var;
349 dev = KI_EPROC(k)->e_tdev;
350 if (dev == NODEV)
351 (void)printf("%*s", v->width, "??");
352 else {
353 (void)snprintf(buff, sizeof(buff),
354 "%d/%d", major(dev), minor(dev));
355 (void)printf("%*s", v->width, buff);
356 }
357 }
358
359 void
360 tname(k, ve)
361 KINFO *k;
362 VARENT *ve;
363 {
364 VAR *v;
365 dev_t dev;
366 const char *ttname;
367
368 v = ve->var;
369 dev = KI_EPROC(k)->e_tdev;
370 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
371 (void)printf("%-*s", v->width, "??");
372 else {
373 if (strncmp(ttname, "tty", 3) == 0 ||
374 strncmp(ttname, "dty", 3) == 0)
375 ttname += 3;
376 (void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
377 KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-');
378 }
379 }
380
381 void
382 longtname(k, ve)
383 KINFO *k;
384 VARENT *ve;
385 {
386 VAR *v;
387 dev_t dev;
388 const char *ttname;
389
390 v = ve->var;
391 dev = KI_EPROC(k)->e_tdev;
392 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
393 (void)printf("%-*s", v->width, "??");
394 else
395 (void)printf("%-*s", v->width, ttname);
396 }
397
398 void
399 started(k, ve)
400 KINFO *k;
401 VARENT *ve;
402 {
403 VAR *v;
404 static time_t now;
405 time_t startt;
406 struct tm *tp;
407 char buf[100];
408
409 v = ve->var;
410 if (!k->ki_u.u_valid) {
411 (void)printf("%-*s", v->width, "-");
412 return;
413 }
414
415 startt = k->ki_u.u_start.tv_sec;
416 tp = localtime(&startt);
417 if (!now)
418 (void)time(&now);
419 if (now - k->ki_u.u_start.tv_sec < 24 * SECSPERHOUR) {
420 /* I *hate* SCCS... */
421 static char fmt[] = __CONCAT("%l:%", "M%p");
422 (void)strftime(buf, sizeof(buf) - 1, fmt, tp);
423 } else if (now - k->ki_u.u_start.tv_sec < 7 * SECSPERDAY) {
424 /* I *hate* SCCS... */
425 static char fmt[] = __CONCAT("%a%", "I%p");
426 (void)strftime(buf, sizeof(buf) - 1, fmt, tp);
427 } else
428 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
429 (void)printf("%-*s", v->width, buf);
430 }
431
432 void
433 lstarted(k, ve)
434 KINFO *k;
435 VARENT *ve;
436 {
437 VAR *v;
438 time_t startt;
439 char buf[100];
440
441 v = ve->var;
442 if (!k->ki_u.u_valid) {
443 (void)printf("%-*s", v->width, "-");
444 return;
445 }
446 startt = k->ki_u.u_start.tv_sec;
447 (void)strftime(buf, sizeof(buf) -1, "%c",
448 localtime(&startt));
449 (void)printf("%-*s", v->width, buf);
450 }
451
452 void
453 wchan(k, ve)
454 KINFO *k;
455 VARENT *ve;
456 {
457 VAR *v;
458 int n;
459
460 v = ve->var;
461 if (KI_PROC(k)->p_wchan) {
462 if (KI_PROC(k)->p_wmesg) {
463 n = min(v->width, WMESGLEN);
464 (void)printf("%-*.*s", n, n, KI_EPROC(k)->e_wmesg);
465 if (v->width > n)
466 (void)printf("%*s", v->width - n, "");
467 } else
468 (void)printf("%-*lx", v->width,
469 (long)KI_PROC(k)->p_wchan);
470 } else
471 (void)printf("%-*s", v->width, "-");
472 }
473
474 #define pgtok(a) (((a)*getpagesize())/1024)
475
476 void
477 vsize(k, ve)
478 KINFO *k;
479 VARENT *ve;
480 {
481 VAR *v;
482
483 v = ve->var;
484 (void)printf("%*d", v->width,
485 pgtok(KI_EPROC(k)->e_vm.vm_dsize + KI_EPROC(k)->e_vm.vm_ssize +
486 KI_EPROC(k)->e_vm.vm_tsize));
487 }
488
489 void
490 rssize(k, ve)
491 KINFO *k;
492 VARENT *ve;
493 {
494 VAR *v;
495
496 v = ve->var;
497 /* XXX don't have info about shared */
498 (void)printf("%*d", v->width, pgtok(KI_EPROC(k)->e_vm.vm_rssize));
499 }
500
501 void
502 p_rssize(k, ve) /* doesn't account for text */
503 KINFO *k;
504 VARENT *ve;
505 {
506 VAR *v;
507
508 v = ve->var;
509 (void)printf("%*d", v->width, pgtok(KI_EPROC(k)->e_vm.vm_rssize));
510 }
511
512 void
513 cputime(k, ve)
514 KINFO *k;
515 VARENT *ve;
516 {
517 VAR *v;
518 long secs;
519 long psecs; /* "parts" of a second. first micro, then centi */
520 char obuff[128];
521
522 v = ve->var;
523 if (P_ZOMBIE(KI_PROC(k)) || k->ki_u.u_valid == 0) {
524 secs = 0;
525 psecs = 0;
526 } else {
527 /*
528 * This counts time spent handling interrupts. We could
529 * fix this, but it is not 100% trivial (and interrupt
530 * time fractions only work on the sparc anyway). XXX
531 */
532 secs = KI_PROC(k)->p_rtime.tv_sec;
533 psecs = KI_PROC(k)->p_rtime.tv_usec;
534 if (sumrusage) {
535 secs += k->ki_u.u_cru.ru_utime.tv_sec +
536 k->ki_u.u_cru.ru_stime.tv_sec;
537 psecs += k->ki_u.u_cru.ru_utime.tv_usec +
538 k->ki_u.u_cru.ru_stime.tv_usec;
539 }
540 /*
541 * round and scale to 100's
542 */
543 psecs = (psecs + 5000) / 10000;
544 secs += psecs / 100;
545 psecs = psecs % 100;
546 }
547 (void)snprintf(obuff, sizeof(obuff),
548 "%3ld:%02ld.%02ld", secs/60, secs%60, psecs);
549 (void)printf("%*s", v->width, obuff);
550 }
551
552 double
553 getpcpu(k)
554 KINFO *k;
555 {
556 struct proc *p;
557 static int failure;
558
559 if (!nlistread)
560 failure = (kd) ? donlist() : 1;
561 if (failure)
562 return (0.0);
563
564 p = KI_PROC(k);
565 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
566
567 /* XXX - I don't like this */
568 if (p->p_swtime == 0 || (p->p_flag & P_INMEM) == 0 ||
569 P_ZOMBIE(p))
570 return (0.0);
571 if (rawcpu)
572 return (100.0 * fxtofl(p->p_pctcpu));
573 return (100.0 * fxtofl(p->p_pctcpu) /
574 (1.0 - exp(p->p_swtime * log(fxtofl(ccpu)))));
575 }
576
577 void
578 pcpu(k, ve)
579 KINFO *k;
580 VARENT *ve;
581 {
582 VAR *v;
583
584 v = ve->var;
585 (void)printf("%*.1f", v->width, getpcpu(k));
586 }
587
588 double
589 getpmem(k)
590 KINFO *k;
591 {
592 static int failure;
593 struct proc *p;
594 struct eproc *e;
595 double fracmem;
596 int szptudot;
597
598 if (!nlistread)
599 failure = (kd) ? donlist() : 1;
600 if (failure)
601 return (0.0);
602
603 p = KI_PROC(k);
604 e = KI_EPROC(k);
605 if ((p->p_flag & P_INMEM) == 0)
606 return (0.0);
607 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
608 szptudot = USPACE/getpagesize();
609 /* XXX don't have info about shared */
610 fracmem = ((float)e->e_vm.vm_rssize + szptudot)/CLSIZE/mempages;
611 return (100.0 * fracmem);
612 }
613
614 void
615 pmem(k, ve)
616 KINFO *k;
617 VARENT *ve;
618 {
619 VAR *v;
620
621 v = ve->var;
622 (void)printf("%*.1f", v->width, getpmem(k));
623 }
624
625 void
626 pagein(k, ve)
627 KINFO *k;
628 VARENT *ve;
629 {
630 VAR *v;
631
632 v = ve->var;
633 (void)printf("%*ld", v->width,
634 k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0);
635 }
636
637 void
638 maxrss(k, ve)
639 KINFO *k;
640 VARENT *ve;
641 {
642 VAR *v;
643
644 v = ve->var;
645 (void)printf("%*s", v->width, "-");
646 }
647
648 void
649 tsize(k, ve)
650 KINFO *k;
651 VARENT *ve;
652 {
653 VAR *v;
654
655 v = ve->var;
656 (void)printf("%*d", v->width, pgtok(KI_EPROC(k)->e_vm.vm_tsize));
657 }
658
659 /*
660 * Generic output routines. Print fields from various prototype
661 * structures.
662 */
663 static void
664 printval(bp, v)
665 char *bp;
666 VAR *v;
667 {
668 static char ofmt[32] = "%";
669 char *fcp, *cp;
670 enum type type;
671
672 cp = ofmt + 1;
673 fcp = v->fmt;
674 if (v->flag & LJUST)
675 *cp++ = '-';
676 *cp++ = '*';
677 while ((*cp++ = *fcp++) != '\0')
678 continue;
679
680 /*
681 * Note that the "INF127" check is nonsensical for types
682 * that are or can be signed.
683 */
684 #define GET(type) (*(type *)bp)
685 #define CHK_INF127(n) (((n) > 127) && (v->flag & INF127) ? 127 : (n))
686
687 switch (v->type) {
688 case INT32:
689 if (sizeof(int32_t) == sizeof(int))
690 type = INT;
691 else if (sizeof(int32_t) == sizeof(long))
692 type = LONG;
693 else
694 errx(1, "unknown conversion for type %d", v->type);
695 break;
696 case UINT32:
697 if (sizeof(u_int32_t) == sizeof(u_int))
698 type = UINT;
699 else if (sizeof(u_int32_t) == sizeof(u_long))
700 type = ULONG;
701 else
702 errx(1, "unknown conversion for type %d", v->type);
703 break;
704 default:
705 type = v->type;
706 break;
707 }
708
709 switch (type) {
710 case CHAR:
711 (void)printf(ofmt, v->width, GET(char));
712 break;
713 case UCHAR:
714 (void)printf(ofmt, v->width, CHK_INF127(GET(u_char)));
715 break;
716 case SHORT:
717 (void)printf(ofmt, v->width, GET(short));
718 break;
719 case USHORT:
720 (void)printf(ofmt, v->width, CHK_INF127(GET(u_short)));
721 break;
722 case INT:
723 (void)printf(ofmt, v->width, GET(int));
724 break;
725 case UINT:
726 (void)printf(ofmt, v->width, CHK_INF127(GET(u_int)));
727 break;
728 case LONG:
729 (void)printf(ofmt, v->width, GET(long));
730 break;
731 case ULONG:
732 (void)printf(ofmt, v->width, CHK_INF127(GET(u_long)));
733 break;
734 case KPTR:
735 (void)printf(ofmt, v->width, GET(u_long));
736 break;
737 case KPTR24:
738 (void)printf(ofmt, v->width, GET(u_long) & 0xffffff);
739 break;
740 case SIGLIST:
741 {
742 sigset_t *s = (sigset_t *)(void *)bp;
743 size_t i;
744 #define SIGSETSIZE (sizeof(s->__bits) / sizeof(s->__bits[0]))
745 char buf[SIGSETSIZE * 8 + 1];
746
747 for (i = 0; i < SIGSETSIZE; i++)
748 (void)snprintf(&buf[i * 8], 9, "%.8x",
749 s->__bits[(SIGSETSIZE - 1) - i]);
750
751 /* Skip leading zeroes */
752 for (i = 0; buf[i]; i++)
753 if (buf[i] != '0')
754 break;
755
756 if (buf[i] == '\0')
757 i--;
758 (void)printf(ofmt, v->width, &buf[i]);
759 }
760 break;
761 default:
762 errx(1, "unknown type %d", v->type);
763 }
764 #undef GET
765 #undef CHK_INF127
766 }
767
768 void
769 pvar(k, ve)
770 KINFO *k;
771 VARENT *ve;
772 {
773 VAR *v;
774
775 v = ve->var;
776 printval((char *)KI_PROC(k) + v->off, v);
777 }
778
779 void
780 evar(k, ve)
781 KINFO *k;
782 VARENT *ve;
783 {
784 VAR *v;
785
786 v = ve->var;
787 printval((char *)KI_EPROC(k) + v->off, v);
788 }
789
790 void
791 uvar(k, ve)
792 KINFO *k;
793 VARENT *ve;
794 {
795 VAR *v;
796
797 v = ve->var;
798 if (k->ki_u.u_valid)
799 printval((char *)&k->ki_u + v->off, v);
800 else
801 (void)printf("%*s", v->width, "-");
802 }
803
804 void
805 rvar(k, ve)
806 KINFO *k;
807 VARENT *ve;
808 {
809 VAR *v;
810
811 v = ve->var;
812 if (k->ki_u.u_valid)
813 printval((char *)&k->ki_u.u_ru + v->off, v);
814 else
815 (void)printf("%*s", v->width, "-");
816 }
817