print.c revision 1.26 1 /* $NetBSD: print.c,v 1.26 1995/08/15 14:01:01 cgd 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 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94";
39 #else
40 static char rcsid[] = "$NetBSD: print.c,v 1.26 1995/08/15 14:01:01 cgd Exp $";
41 #endif
42 #endif /* not lint */
43
44 #include <sys/param.h>
45 #include <sys/time.h>
46 #include <sys/resource.h>
47 #include <sys/proc.h>
48 #include <sys/stat.h>
49
50 #ifdef P_PPWAIT
51 #define NEWVM
52 #endif
53
54 #ifdef NEWVM
55 #include <sys/ucred.h>
56 #include <sys/sysctl.h>
57 #include <vm/vm.h>
58 #else
59 #include <machine/pte.h>
60 #include <sys/vmparam.h>
61 #include <sys/vm.h>
62 #endif
63
64 #include <err.h>
65 #include <kvm.h>
66 #include <math.h>
67 #include <nlist.h>
68 #include <stddef.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <string.h>
72 #include <tzfile.h>
73 #include <unistd.h>
74
75 #include "ps.h"
76
77 extern kvm_t *kd;
78 extern int needenv, needcomm, commandonly;
79
80 static char *cmdpart __P((char *));
81
82 static char *
83 cmdpart(arg0)
84 char *arg0;
85 {
86 char *cp;
87
88 return ((cp = strrchr(arg0, '/')) != NULL ? cp + 1 : arg0);
89 }
90
91 void
92 printheader()
93 {
94 VAR *v;
95 struct varent *vent;
96
97 for (vent = vhead; vent; vent = vent->next) {
98 v = vent->var;
99 if (v->flag & LJUST) {
100 if (vent->next == NULL) /* last one */
101 (void)printf("%s", v->header);
102 else
103 (void)printf("%-*s", v->width, v->header);
104 } else
105 (void)printf("%*s", v->width, v->header);
106 if (vent->next != NULL)
107 (void)putchar(' ');
108 }
109 (void)putchar('\n');
110 }
111
112 void
113 command(ki, ve)
114 KINFO *ki;
115 VARENT *ve;
116 {
117 VAR *v;
118 int left;
119 char **argv, **p;
120
121 v = ve->var;
122 if (ve->next != NULL || termwidth != UNLIMITED) {
123 if (ve->next == NULL) {
124 left = termwidth - (totwidth - v->width);
125 if (left < 1) /* already wrapped, just use std width */
126 left = v->width;
127 } else
128 left = v->width;
129 } else
130 left = -1;
131 if (needenv) {
132 argv = kvm_getenvv(kd, ki->ki_p, termwidth);
133 if (p = argv) {
134 while (*p) {
135 fmt_puts(*p, &left);
136 p++;
137 fmt_putc(' ', &left);
138 }
139 }
140 }
141 if (needcomm) {
142 if (!commandonly) {
143 argv = kvm_getargv(kd, ki->ki_p, termwidth);
144 if (p = argv) {
145 while (*p) {
146 fmt_puts(*p, &left);
147 p++;
148 fmt_putc(' ', &left);
149 }
150 }
151 if (argv == 0 || argv[0] == 0 ||
152 strcmp(cmdpart(argv[0]), KI_PROC(ki)->p_comm)) {
153 fmt_putc('(', &left);
154 fmt_puts(KI_PROC(ki)->p_comm, &left);
155 fmt_putc(')', &left);
156 }
157 } else {
158 fmt_puts(KI_PROC(ki)->p_comm, &left);
159 }
160 }
161 if (ve->next && left > 0)
162 printf("%*s", left, "");
163 }
164
165 void
166 ucomm(k, ve)
167 KINFO *k;
168 VARENT *ve;
169 {
170 VAR *v;
171
172 v = ve->var;
173 (void)printf("%-*s", v->width, KI_PROC(k)->p_comm);
174 }
175
176 void
177 logname(k, ve)
178 KINFO *k;
179 VARENT *ve;
180 {
181 VAR *v;
182
183 v = ve->var;
184 #ifndef NEWVM
185 (void)printf("%-*s", v->width, KI_PROC(k)->p_logname);
186 #else
187 (void)printf("%-*s", v->width, KI_EPROC(k)->e_login);
188 #endif
189 }
190
191 void
192 state(k, ve)
193 KINFO *k;
194 VARENT *ve;
195 {
196 struct proc *p;
197 int flag;
198 char *cp;
199 VAR *v;
200 char buf[16];
201
202 v = ve->var;
203 p = KI_PROC(k);
204 flag = p->p_flag;
205 cp = buf;
206
207 switch (p->p_stat) {
208
209 case SSTOP:
210 *cp = 'T';
211 break;
212
213 case SSLEEP:
214 if (flag & P_SINTR) /* interuptable (long) */
215 *cp = p->p_slptime >= MAXSLP ? 'I' : 'S';
216 else
217 *cp = 'D';
218 break;
219
220 case SRUN:
221 case SIDL:
222 *cp = 'R';
223 break;
224
225 case SZOMB:
226 *cp = 'Z';
227 break;
228
229 default:
230 *cp = '?';
231 }
232 cp++;
233 if (flag & P_INMEM) {
234 #ifndef NEWVM
235 if (p->p_rssize > p->p_maxrss)
236 *cp++ = '>';
237 #endif
238 } else
239 *cp++ = 'W';
240 if (p->p_nice < NZERO)
241 *cp++ = '<';
242 else if (p->p_nice > NZERO)
243 *cp++ = 'N';
244 #ifndef NEWVM
245 if (flag & SUANOM)
246 *cp++ = 'A';
247 else if (flag & SSEQL)
248 *cp++ = 'S';
249 #endif
250 if (flag & P_TRACED)
251 *cp++ = 'X';
252 if (flag & P_WEXIT && p->p_stat != SZOMB)
253 *cp++ = 'E';
254 #ifdef NEWVM
255 if (flag & P_PPWAIT)
256 #else
257 if (flag & SVFORK)
258 #endif
259 *cp++ = 'V';
260 #ifdef NEWVM
261 if ((flag & P_SYSTEM) || p->p_holdcnt)
262 #else
263 if (flag & (SSYS|SLOCK|SULOCK|SKEEP|SPHYSIO))
264 #endif
265 *cp++ = 'L';
266 if (KI_EPROC(k)->e_flag & EPROC_SLEADER)
267 *cp++ = 's';
268 if ((flag & P_CONTROLT) && KI_EPROC(k)->e_pgid == KI_EPROC(k)->e_tpgid)
269 *cp++ = '+';
270 *cp = '\0';
271 (void)printf("%-*s", v->width, buf);
272 }
273
274 void
275 pri(k, ve)
276 KINFO *k;
277 VARENT *ve;
278 {
279 VAR *v;
280
281 v = ve->var;
282 (void)printf("%*d", v->width, KI_PROC(k)->p_priority - PZERO);
283 }
284
285 void
286 uname(k, ve)
287 KINFO *k;
288 VARENT *ve;
289 {
290 VAR *v;
291
292 v = ve->var;
293 #ifndef NEWVM
294 (void)printf("%-*s",
295 (int)v->width, user_from_uid(KI_PROC(k)->p_uid, 0));
296 #else
297 (void)printf("%-*s",
298 (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0));
299 #endif
300 }
301
302 void
303 runame(k, ve)
304 KINFO *k;
305 VARENT *ve;
306 {
307 VAR *v;
308
309 v = ve->var;
310 #ifndef NEWVM
311 (void)printf("%-*s",
312 (int)v->width, user_from_uid(KI_PROC(k)->p_ruid, 0));
313 #else
314 (void)printf("%-*s",
315 (int)v->width, user_from_uid(KI_EPROC(k)->e_pcred.p_ruid, 0));
316 #endif
317 }
318
319 void
320 tdev(k, ve)
321 KINFO *k;
322 VARENT *ve;
323 {
324 VAR *v;
325 dev_t dev;
326 char buff[16];
327
328 v = ve->var;
329 dev = KI_EPROC(k)->e_tdev;
330 if (dev == NODEV)
331 (void)printf("%*s", v->width, "??");
332 else {
333 (void)snprintf(buff, sizeof(buff),
334 "%d/%d", major(dev), minor(dev));
335 (void)printf("%*s", v->width, buff);
336 }
337 }
338
339 void
340 tname(k, ve)
341 KINFO *k;
342 VARENT *ve;
343 {
344 VAR *v;
345 dev_t dev;
346 char *ttname;
347
348 v = ve->var;
349 dev = KI_EPROC(k)->e_tdev;
350 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
351 (void)printf("%-*s", v->width, "??");
352 else {
353 if (strncmp(ttname, "tty", 3) == 0)
354 ttname += 3;
355 (void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
356 KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-');
357 }
358 }
359
360 void
361 longtname(k, ve)
362 KINFO *k;
363 VARENT *ve;
364 {
365 VAR *v;
366 dev_t dev;
367 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 (void)printf("%-*s", v->width, ttname);
375 }
376
377 void
378 started(k, ve)
379 KINFO *k;
380 VARENT *ve;
381 {
382 VAR *v;
383 static time_t now;
384 time_t startt;
385 struct tm *tp;
386 char buf[100];
387
388 v = ve->var;
389 if (!k->ki_u.u_valid) {
390 (void)printf("%-*s", v->width, "-");
391 return;
392 }
393
394 startt = k->ki_u.u_start.tv_sec;
395 tp = localtime(&startt);
396 if (!now)
397 (void)time(&now);
398 if (now - k->ki_u.u_start.tv_sec < 24 * SECSPERHOUR) {
399 /* I *hate* SCCS... */
400 static char fmt[] = __CONCAT("%l:%", "M%p");
401 (void)strftime(buf, sizeof(buf) - 1, fmt, tp);
402 } else if (now - k->ki_u.u_start.tv_sec < 7 * SECSPERDAY) {
403 /* I *hate* SCCS... */
404 static char fmt[] = __CONCAT("%a%", "I%p");
405 (void)strftime(buf, sizeof(buf) - 1, fmt, tp);
406 } else
407 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
408 (void)printf("%-*s", v->width, buf);
409 }
410
411 void
412 lstarted(k, ve)
413 KINFO *k;
414 VARENT *ve;
415 {
416 VAR *v;
417 time_t startt;
418 char buf[100];
419
420 v = ve->var;
421 if (!k->ki_u.u_valid) {
422 (void)printf("%-*s", v->width, "-");
423 return;
424 }
425 startt = k->ki_u.u_start.tv_sec;
426 (void)strftime(buf, sizeof(buf) -1, "%C",
427 localtime(&startt));
428 (void)printf("%-*s", v->width, buf);
429 }
430
431 void
432 wchan(k, ve)
433 KINFO *k;
434 VARENT *ve;
435 {
436 VAR *v;
437
438 v = ve->var;
439 if (KI_PROC(k)->p_wchan) {
440 if (KI_PROC(k)->p_wmesg)
441 (void)printf("%-*.*s", v->width, v->width,
442 KI_EPROC(k)->e_wmesg);
443 else
444 (void)printf("%-*lx", v->width,
445 (long)KI_PROC(k)->p_wchan &~ KERNBASE);
446 } else
447 (void)printf("%-*s", v->width, "-");
448 }
449
450 #define pgtok(a) (((a)*getpagesize())/1024)
451
452 void
453 vsize(k, ve)
454 KINFO *k;
455 VARENT *ve;
456 {
457 VAR *v;
458
459 v = ve->var;
460 (void)printf("%*d", v->width,
461 #ifndef NEWVM
462 pgtok(KI_PROC(k)->p_dsize +
463 KI_PROC(k)->p_ssize + KI_EPROC(k)->e_xsize));
464 #else
465 pgtok(KI_EPROC(k)->e_vm.vm_dsize + KI_EPROC(k)->e_vm.vm_ssize +
466 KI_EPROC(k)->e_vm.vm_tsize));
467 #endif
468 }
469
470 void
471 rssize(k, ve)
472 KINFO *k;
473 VARENT *ve;
474 {
475 VAR *v;
476
477 v = ve->var;
478 #ifndef NEWVM
479 (void)printf("%*d", v->width,
480 pgtok(KI_PROC(k)->p_rssize + (KI_EPROC(k)->e_xccount ?
481 (KI_EPROC(k)->e_xrssize / KI_EPROC(k)->e_xccount) : 0)));
482 #else
483 /* XXX don't have info about shared */
484 (void)printf("%*d", v->width, pgtok(KI_EPROC(k)->e_vm.vm_rssize));
485 #endif
486 }
487
488 void
489 p_rssize(k, ve) /* doesn't account for text */
490 KINFO *k;
491 VARENT *ve;
492 {
493 VAR *v;
494
495 v = ve->var;
496 #ifndef NEWVM
497 (void)printf("%*d", v->width, pgtok(KI_PROC(k)->p_rssize));
498 #else
499 (void)printf("%*d", v->width, pgtok(KI_EPROC(k)->e_vm.vm_rssize));
500 #endif
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 (KI_PROC(k)->p_stat == SZOMB || !k->ki_u.u_valid) {
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 return (0.0);
561 if (rawcpu)
562 return (100.0 * fxtofl(p->p_pctcpu));
563 return (100.0 * fxtofl(p->p_pctcpu) /
564 (1.0 - exp(p->p_swtime * log(fxtofl(ccpu)))));
565 }
566
567 void
568 pcpu(k, ve)
569 KINFO *k;
570 VARENT *ve;
571 {
572 VAR *v;
573
574 v = ve->var;
575 (void)printf("%*.1f", v->width, getpcpu(k));
576 }
577
578 double
579 getpmem(k)
580 KINFO *k;
581 {
582 static int failure;
583 struct proc *p;
584 struct eproc *e;
585 double fracmem;
586 int szptudot;
587
588 if (!nlistread)
589 failure = donlist();
590 if (failure)
591 return (0.0);
592
593 p = KI_PROC(k);
594 e = KI_EPROC(k);
595 if ((p->p_flag & P_INMEM) == 0)
596 return (0.0);
597 #ifndef NEWVM
598 szptudot = USPACE/getpagesize() +
599 clrnd(ctopt(p->p_dsize + p->p_ssize + e->e_xsize));
600 fracmem = ((float)p->p_rssize + szptudot)/CLSIZE/mempages;
601 if (p->p_textp && e->e_xccount)
602 fracmem += ((float)e->e_xrssize)/CLSIZE/e->e_xccount/mempages;
603 #else
604 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
605 szptudot = USPACE/getpagesize();
606 /* XXX don't have info about shared */
607 fracmem = ((float)e->e_vm.vm_rssize + szptudot)/CLSIZE/mempages;
608 #endif
609 return (100.0 * fracmem);
610 }
611
612 void
613 pmem(k, ve)
614 KINFO *k;
615 VARENT *ve;
616 {
617 VAR *v;
618
619 v = ve->var;
620 (void)printf("%*.1f", v->width, getpmem(k));
621 }
622
623 void
624 pagein(k, ve)
625 KINFO *k;
626 VARENT *ve;
627 {
628 VAR *v;
629
630 v = ve->var;
631 (void)printf("%*d", v->width,
632 k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0);
633 }
634
635 void
636 maxrss(k, ve)
637 KINFO *k;
638 VARENT *ve;
639 {
640 VAR *v;
641
642 v = ve->var;
643 #ifndef NEWVM /* not yet */
644 if (KI_PROC(k)->p_maxrss != (RLIM_INFINITY/getpagesize()))
645 (void)printf("%*d", v->width, pgtok(KI_PROC(k)->p_maxrss));
646 else
647 #endif
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 #ifndef NEWVM
660 (void)printf("%*d", v->width, pgtok(KI_EPROC(k)->e_xsize));
661 #else
662 (void)printf("%*d", v->width, pgtok(KI_EPROC(k)->e_vm.vm_tsize));
663 #endif
664 }
665
666 #ifndef NEWVM
667 void
668 trss(k, ve)
669 KINFO *k;
670 VARENT *ve;
671 {
672 VAR *v;
673
674 v = ve->var;
675 (void)printf("%*d", v->width, pgtok(KI_EPROC(k)->e_xrssize));
676 }
677 #endif
678
679 /*
680 * Generic output routines. Print fields from various prototype
681 * structures.
682 */
683 static void
684 printval(bp, v)
685 char *bp;
686 VAR *v;
687 {
688 static char ofmt[32] = "%";
689 char *fcp, *cp;
690
691 cp = ofmt + 1;
692 fcp = v->fmt;
693 if (v->flag & LJUST)
694 *cp++ = '-';
695 *cp++ = '*';
696 while (*cp++ = *fcp++);
697
698 /*
699 * Note that the "INF127" check is nonsensical for types
700 * that are or can be signed.
701 */
702 #define GET(type) (*(type *)bp)
703 #define CHK_INF127(n) (((n) > 127) && (v->flag & INF127) ? 127 : (n))
704
705 switch (v->type) {
706 case CHAR:
707 (void)printf(ofmt, v->width, GET(char));
708 break;
709 case UCHAR:
710 (void)printf(ofmt, v->width, CHK_INF127(GET(u_char)));
711 break;
712 case SHORT:
713 (void)printf(ofmt, v->width, GET(short));
714 break;
715 case USHORT:
716 (void)printf(ofmt, v->width, CHK_INF127(GET(u_short)));
717 break;
718 case INT:
719 (void)printf(ofmt, v->width, GET(int));
720 break;
721 case UINT:
722 (void)printf(ofmt, v->width, CHK_INF127(GET(u_int)));
723 break;
724 case LONG:
725 (void)printf(ofmt, v->width, GET(long));
726 break;
727 case ULONG:
728 (void)printf(ofmt, v->width, CHK_INF127(GET(u_long)));
729 break;
730 case KPTR:
731 (void)printf(ofmt, v->width, GET(u_long) &~ KERNBASE);
732 break;
733 default:
734 errx(1, "unknown type %d", v->type);
735 }
736 #undef GET
737 #undef CHK_INF127
738 }
739
740 void
741 pvar(k, ve)
742 KINFO *k;
743 VARENT *ve;
744 {
745 VAR *v;
746
747 v = ve->var;
748 printval((char *)((char *)KI_PROC(k) + v->off), v);
749 }
750
751 void
752 evar(k, ve)
753 KINFO *k;
754 VARENT *ve;
755 {
756 VAR *v;
757
758 v = ve->var;
759 printval((char *)((char *)KI_EPROC(k) + v->off), v);
760 }
761
762 void
763 uvar(k, ve)
764 KINFO *k;
765 VARENT *ve;
766 {
767 VAR *v;
768
769 v = ve->var;
770 if (k->ki_u.u_valid)
771 printval((char *)((char *)&k->ki_u + v->off), v);
772 else
773 (void)printf("%*s", v->width, "-");
774 }
775
776 void
777 rvar(k, ve)
778 KINFO *k;
779 VARENT *ve;
780 {
781 VAR *v;
782
783 v = ve->var;
784 if (k->ki_u.u_valid)
785 printval((char *)((char *)(&k->ki_u.u_ru) + v->off), v);
786 else
787 (void)printf("%*s", v->width, "-");
788 }
789