print.c revision 1.39.2.2 1 /* $NetBSD: print.c,v 1.39.2.2 1999/11/08 06:40:14 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 #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.39.2.2 1999/11/08 06:40:14 cgd 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 *cp = 'Z';
263 break;
264
265 default:
266 *cp = '?';
267 }
268 cp++;
269 if (flag & P_INMEM) {
270 } else
271 *cp++ = 'W';
272 if (p->p_nice < NZERO)
273 *cp++ = '<';
274 else if (p->p_nice > NZERO)
275 *cp++ = 'N';
276 if (flag & P_TRACED)
277 *cp++ = 'X';
278 if (flag & P_WEXIT && p->p_stat != SZOMB)
279 *cp++ = 'E';
280 if (flag & P_PPWAIT)
281 *cp++ = 'V';
282 if ((flag & P_SYSTEM) || p->p_holdcnt)
283 *cp++ = 'L';
284 if (KI_EPROC(k)->e_flag & EPROC_SLEADER)
285 *cp++ = 's';
286 if ((flag & P_CONTROLT) && KI_EPROC(k)->e_pgid == KI_EPROC(k)->e_tpgid)
287 *cp++ = '+';
288 *cp = '\0';
289 (void)printf("%-*s", v->width, buf);
290 }
291
292 void
293 pnice(k, ve)
294 KINFO *k;
295 VARENT *ve;
296 {
297 VAR *v;
298
299 v = ve->var;
300 (void)printf("%*d", v->width, KI_PROC(k)->p_nice - NZERO);
301 }
302
303 void
304 pri(k, ve)
305 KINFO *k;
306 VARENT *ve;
307 {
308 VAR *v;
309
310 v = ve->var;
311 (void)printf("%*d", v->width, KI_PROC(k)->p_priority - PZERO);
312 }
313
314 void
315 uname(k, ve)
316 KINFO *k;
317 VARENT *ve;
318 {
319 VAR *v;
320
321 v = ve->var;
322 (void)printf("%-*s",
323 (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0));
324 }
325
326 void
327 runame(k, ve)
328 KINFO *k;
329 VARENT *ve;
330 {
331 VAR *v;
332
333 v = ve->var;
334 (void)printf("%-*s",
335 (int)v->width, user_from_uid(KI_EPROC(k)->e_pcred.p_ruid, 0));
336 }
337
338 void
339 tdev(k, ve)
340 KINFO *k;
341 VARENT *ve;
342 {
343 VAR *v;
344 dev_t dev;
345 char buff[16];
346
347 v = ve->var;
348 dev = KI_EPROC(k)->e_tdev;
349 if (dev == NODEV)
350 (void)printf("%*s", v->width, "??");
351 else {
352 (void)snprintf(buff, sizeof(buff),
353 "%d/%d", major(dev), minor(dev));
354 (void)printf("%*s", v->width, buff);
355 }
356 }
357
358 void
359 tname(k, ve)
360 KINFO *k;
361 VARENT *ve;
362 {
363 VAR *v;
364 dev_t dev;
365 const char *ttname;
366
367 v = ve->var;
368 dev = KI_EPROC(k)->e_tdev;
369 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
370 (void)printf("%-*s", v->width, "??");
371 else {
372 if (strncmp(ttname, "tty", 3) == 0 ||
373 strncmp(ttname, "dty", 3) == 0)
374 ttname += 3;
375 (void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
376 KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-');
377 }
378 }
379
380 void
381 longtname(k, ve)
382 KINFO *k;
383 VARENT *ve;
384 {
385 VAR *v;
386 dev_t dev;
387 const char *ttname;
388
389 v = ve->var;
390 dev = KI_EPROC(k)->e_tdev;
391 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
392 (void)printf("%-*s", v->width, "??");
393 else
394 (void)printf("%-*s", v->width, ttname);
395 }
396
397 void
398 started(k, ve)
399 KINFO *k;
400 VARENT *ve;
401 {
402 VAR *v;
403 static time_t now;
404 time_t startt;
405 struct tm *tp;
406 char buf[100];
407
408 v = ve->var;
409 if (!k->ki_u.u_valid) {
410 (void)printf("%-*s", v->width, "-");
411 return;
412 }
413
414 startt = k->ki_u.u_start.tv_sec;
415 tp = localtime(&startt);
416 if (!now)
417 (void)time(&now);
418 if (now - k->ki_u.u_start.tv_sec < 24 * SECSPERHOUR) {
419 /* I *hate* SCCS... */
420 static char fmt[] = __CONCAT("%l:%", "M%p");
421 (void)strftime(buf, sizeof(buf) - 1, fmt, tp);
422 } else if (now - k->ki_u.u_start.tv_sec < 7 * SECSPERDAY) {
423 /* I *hate* SCCS... */
424 static char fmt[] = __CONCAT("%a%", "I%p");
425 (void)strftime(buf, sizeof(buf) - 1, fmt, tp);
426 } else
427 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
428 (void)printf("%-*s", v->width, buf);
429 }
430
431 void
432 lstarted(k, ve)
433 KINFO *k;
434 VARENT *ve;
435 {
436 VAR *v;
437 time_t startt;
438 char buf[100];
439
440 v = ve->var;
441 if (!k->ki_u.u_valid) {
442 (void)printf("%-*s", v->width, "-");
443 return;
444 }
445 startt = k->ki_u.u_start.tv_sec;
446 (void)strftime(buf, sizeof(buf) -1, "%c",
447 localtime(&startt));
448 (void)printf("%-*s", v->width, buf);
449 }
450
451 void
452 wchan(k, ve)
453 KINFO *k;
454 VARENT *ve;
455 {
456 VAR *v;
457 int n;
458
459 v = ve->var;
460 if (KI_PROC(k)->p_wchan) {
461 if (KI_PROC(k)->p_wmesg) {
462 n = min(v->width, WMESGLEN);
463 (void)printf("%-*.*s", n, n, KI_EPROC(k)->e_wmesg);
464 if (v->width > n)
465 (void)printf("%*s", v->width - n, "");
466 } else
467 (void)printf("%-*lx", v->width,
468 (long)KI_PROC(k)->p_wchan);
469 } else
470 (void)printf("%-*s", v->width, "-");
471 }
472
473 #define pgtok(a) (((a)*getpagesize())/1024)
474
475 void
476 vsize(k, ve)
477 KINFO *k;
478 VARENT *ve;
479 {
480 VAR *v;
481
482 v = ve->var;
483 (void)printf("%*d", v->width,
484 pgtok(KI_EPROC(k)->e_vm.vm_dsize + KI_EPROC(k)->e_vm.vm_ssize +
485 KI_EPROC(k)->e_vm.vm_tsize));
486 }
487
488 void
489 rssize(k, ve)
490 KINFO *k;
491 VARENT *ve;
492 {
493 VAR *v;
494
495 v = ve->var;
496 /* XXX don't have info about shared */
497 (void)printf("%*d", v->width, pgtok(KI_EPROC(k)->e_vm.vm_rssize));
498 }
499
500 void
501 p_rssize(k, ve) /* doesn't account for text */
502 KINFO *k;
503 VARENT *ve;
504 {
505 VAR *v;
506
507 v = ve->var;
508 (void)printf("%*d", v->width, pgtok(KI_EPROC(k)->e_vm.vm_rssize));
509 }
510
511 void
512 cputime(k, ve)
513 KINFO *k;
514 VARENT *ve;
515 {
516 VAR *v;
517 long secs;
518 long psecs; /* "parts" of a second. first micro, then centi */
519 char obuff[128];
520
521 v = ve->var;
522 if (KI_PROC(k)->p_stat == SZOMB || !k->ki_u.u_valid) {
523 secs = 0;
524 psecs = 0;
525 } else {
526 /*
527 * This counts time spent handling interrupts. We could
528 * fix this, but it is not 100% trivial (and interrupt
529 * time fractions only work on the sparc anyway). XXX
530 */
531 secs = KI_PROC(k)->p_rtime.tv_sec;
532 psecs = KI_PROC(k)->p_rtime.tv_usec;
533 if (sumrusage) {
534 secs += k->ki_u.u_cru.ru_utime.tv_sec +
535 k->ki_u.u_cru.ru_stime.tv_sec;
536 psecs += k->ki_u.u_cru.ru_utime.tv_usec +
537 k->ki_u.u_cru.ru_stime.tv_usec;
538 }
539 /*
540 * round and scale to 100's
541 */
542 psecs = (psecs + 5000) / 10000;
543 secs += psecs / 100;
544 psecs = psecs % 100;
545 }
546 (void)snprintf(obuff, sizeof(obuff),
547 "%3ld:%02ld.%02ld", secs/60, secs%60, psecs);
548 (void)printf("%*s", v->width, obuff);
549 }
550
551 double
552 getpcpu(k)
553 KINFO *k;
554 {
555 struct proc *p;
556 static int failure;
557
558 if (!nlistread)
559 failure = (kd) ? donlist() : 1;
560 if (failure)
561 return (0.0);
562
563 p = KI_PROC(k);
564 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
565
566 /* XXX - I don't like this */
567 if (p->p_swtime == 0 || (p->p_flag & P_INMEM) == 0
568 || p->p_stat == SZOMB)
569 return (0.0);
570 if (rawcpu)
571 return (100.0 * fxtofl(p->p_pctcpu));
572 return (100.0 * fxtofl(p->p_pctcpu) /
573 (1.0 - exp(p->p_swtime * log(fxtofl(ccpu)))));
574 }
575
576 void
577 pcpu(k, ve)
578 KINFO *k;
579 VARENT *ve;
580 {
581 VAR *v;
582
583 v = ve->var;
584 (void)printf("%*.1f", v->width, getpcpu(k));
585 }
586
587 double
588 getpmem(k)
589 KINFO *k;
590 {
591 static int failure;
592 struct proc *p;
593 struct eproc *e;
594 double fracmem;
595 int szptudot;
596
597 if (!nlistread)
598 failure = (kd) ? donlist() : 1;
599 if (failure)
600 return (0.0);
601
602 p = KI_PROC(k);
603 e = KI_EPROC(k);
604 if ((p->p_flag & P_INMEM) == 0)
605 return (0.0);
606 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
607 szptudot = USPACE/getpagesize();
608 /* XXX don't have info about shared */
609 fracmem = ((float)e->e_vm.vm_rssize + szptudot)/CLSIZE/mempages;
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 (void)printf("%*s", v->width, "-");
645 }
646
647 void
648 tsize(k, ve)
649 KINFO *k;
650 VARENT *ve;
651 {
652 VAR *v;
653
654 v = ve->var;
655 (void)printf("%*d", v->width, pgtok(KI_EPROC(k)->e_vm.vm_tsize));
656 }
657
658 /*
659 * Generic output routines. Print fields from various prototype
660 * structures.
661 */
662 static void
663 printval(bp, v)
664 char *bp;
665 VAR *v;
666 {
667 static char ofmt[32] = "%";
668 char *fcp, *cp;
669 enum type type;
670
671 cp = ofmt + 1;
672 fcp = v->fmt;
673 if (v->flag & LJUST)
674 *cp++ = '-';
675 *cp++ = '*';
676 while ((*cp++ = *fcp++) != '\0')
677 continue;
678
679 /*
680 * Note that the "INF127" check is nonsensical for types
681 * that are or can be signed.
682 */
683 #define GET(type) (*(type *)bp)
684 #define CHK_INF127(n) (((n) > 127) && (v->flag & INF127) ? 127 : (n))
685
686 switch (v->type) {
687 case INT32:
688 if (sizeof(int32_t) == sizeof(int))
689 type = INT;
690 else if (sizeof(int32_t) == sizeof(long))
691 type = LONG;
692 else
693 errx(1, "unknown conversion for type %d", v->type);
694 break;
695 case UINT32:
696 if (sizeof(u_int32_t) == sizeof(u_int))
697 type = UINT;
698 else if (sizeof(u_int32_t) == sizeof(u_long))
699 type = ULONG;
700 else
701 errx(1, "unknown conversion for type %d", v->type);
702 break;
703 default:
704 type = v->type;
705 break;
706 }
707
708 switch (type) {
709 case CHAR:
710 (void)printf(ofmt, v->width, GET(char));
711 break;
712 case UCHAR:
713 (void)printf(ofmt, v->width, CHK_INF127(GET(u_char)));
714 break;
715 case SHORT:
716 (void)printf(ofmt, v->width, GET(short));
717 break;
718 case USHORT:
719 (void)printf(ofmt, v->width, CHK_INF127(GET(u_short)));
720 break;
721 case INT:
722 (void)printf(ofmt, v->width, GET(int));
723 break;
724 case UINT:
725 (void)printf(ofmt, v->width, CHK_INF127(GET(u_int)));
726 break;
727 case LONG:
728 (void)printf(ofmt, v->width, GET(long));
729 break;
730 case ULONG:
731 (void)printf(ofmt, v->width, CHK_INF127(GET(u_long)));
732 break;
733 case KPTR:
734 (void)printf(ofmt, v->width, GET(u_long));
735 break;
736 default:
737 errx(1, "unknown type %d", v->type);
738 }
739 #undef GET
740 #undef CHK_INF127
741 }
742
743 void
744 pvar(k, ve)
745 KINFO *k;
746 VARENT *ve;
747 {
748 VAR *v;
749
750 v = ve->var;
751 printval((char *)KI_PROC(k) + v->off, v);
752 }
753
754 void
755 evar(k, ve)
756 KINFO *k;
757 VARENT *ve;
758 {
759 VAR *v;
760
761 v = ve->var;
762 printval((char *)KI_EPROC(k) + v->off, v);
763 }
764
765 void
766 uvar(k, ve)
767 KINFO *k;
768 VARENT *ve;
769 {
770 VAR *v;
771
772 v = ve->var;
773 if (k->ki_u.u_valid)
774 printval((char *)&k->ki_u + v->off, v);
775 else
776 (void)printf("%*s", v->width, "-");
777 }
778
779 void
780 rvar(k, ve)
781 KINFO *k;
782 VARENT *ve;
783 {
784 VAR *v;
785
786 v = ve->var;
787 if (k->ki_u.u_valid)
788 printval((char *)&k->ki_u.u_ru + v->off, v);
789 else
790 (void)printf("%*s", v->width, "-");
791 }
792