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