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