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