print.c revision 1.12 1 /*-
2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94";
36 #endif /* not lint */
37
38 #include <sys/param.h>
39 #include <sys/time.h>
40 #include <sys/resource.h>
41 #include <sys/proc.h>
42 #include <sys/stat.h>
43
44 #ifdef P_PPWAIT
45 #define NEWVM
46 #endif
47
48 #ifdef NEWVM
49 #include <sys/ucred.h>
50 #include <sys/sysctl.h>
51 #include <vm/vm.h>
52 #else
53 #include <machine/pte.h>
54 #include <sys/vmparam.h>
55 #include <sys/vm.h>
56 #endif
57
58 #include <err.h>
59 #include <math.h>
60 #include <nlist.h>
61 #include <stddef.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <vis.h>
66 #include <tzfile.h>
67
68 #include "ps.h"
69
70 void
71 printheader()
72 {
73 VAR *v;
74 struct varent *vent;
75
76 for (vent = vhead; vent; vent = vent->next) {
77 v = vent->var;
78 if (v->flag & LJUST) {
79 if (vent->next == NULL) /* last one */
80 (void)printf("%s", v->header);
81 else
82 (void)printf("%-*s", v->width, v->header);
83 } else
84 (void)printf("%*s", v->width, v->header);
85 if (vent->next != NULL)
86 (void)putchar(' ');
87 }
88 (void)putchar('\n');
89 }
90
91 void
92 command(k, ve)
93 KINFO *k;
94 VARENT *ve;
95 {
96 VAR *v;
97 int left;
98 char *cp, *vis_env, *vis_args;
99
100 if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
101 err(1, NULL);
102 strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
103 if (k->ki_env) {
104 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
105 err(1, NULL);
106 strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
107 } else
108 vis_env = NULL;
109
110 v = ve->var;
111 if (ve->next == NULL) {
112 /* last field */
113 if (termwidth == UNLIMITED) {
114 if (vis_env)
115 (void)printf("%s ", vis_env);
116 (void)printf("%s", vis_args);
117 } else {
118 left = termwidth - (totwidth - v->width);
119 if (left < 1) /* already wrapped, just use std width */
120 left = v->width;
121 if ((cp = vis_env) != NULL) {
122 while (--left >= 0 && *cp)
123 (void)putchar(*cp++);
124 if (--left >= 0)
125 putchar(' ');
126 }
127 for (cp = vis_args; --left >= 0 && *cp != '\0';)
128 (void)putchar(*cp++);
129 }
130 } else
131 /* XXX env? */
132 (void)printf("%-*.*s", v->width, v->width, vis_args);
133 free(vis_args);
134 if (vis_env != NULL)
135 free(vis_env);
136 }
137
138 void
139 ucomm(k, ve)
140 KINFO *k;
141 VARENT *ve;
142 {
143 VAR *v;
144
145 v = ve->var;
146 (void)printf("%-*s", v->width, KI_PROC(k)->p_comm);
147 }
148
149 void
150 logname(k, ve)
151 KINFO *k;
152 VARENT *ve;
153 {
154 VAR *v;
155
156 v = ve->var;
157 #ifndef NEWVM
158 (void)printf("%-*s", v->width, KI_PROC(k)->p_logname);
159 #else
160 (void)printf("%-*s", v->width, KI_EPROC(k)->e_login);
161 #endif
162 }
163
164 void
165 state(k, ve)
166 KINFO *k;
167 VARENT *ve;
168 {
169 struct proc *p;
170 int flag;
171 char *cp;
172 VAR *v;
173 char buf[16];
174
175 v = ve->var;
176 p = KI_PROC(k);
177 flag = p->p_flag;
178 cp = buf;
179
180 switch (p->p_stat) {
181
182 case SSTOP:
183 *cp = 'T';
184 break;
185
186 case SSLEEP:
187 if (flag & P_SINTR) /* interuptable (long) */
188 *cp = p->p_slptime >= MAXSLP ? 'I' : 'S';
189 else
190 *cp = 'D';
191 break;
192
193 case SRUN:
194 case SIDL:
195 *cp = 'R';
196 break;
197
198 case SZOMB:
199 *cp = 'Z';
200 break;
201
202 default:
203 *cp = '?';
204 }
205 cp++;
206 if (flag & P_INMEM) {
207 #ifndef NEWVM
208 if (p->p_rssize > p->p_maxrss)
209 *cp++ = '>';
210 #endif
211 } else
212 *cp++ = 'W';
213 if (p->p_nice < NZERO)
214 *cp++ = '<';
215 else if (p->p_nice > NZERO)
216 *cp++ = 'N';
217 #ifndef NEWVM
218 if (flag & SUANOM)
219 *cp++ = 'A';
220 else if (flag & SSEQL)
221 *cp++ = 'S';
222 #endif
223 if (flag & P_TRACED)
224 *cp++ = 'X';
225 if (flag & P_WEXIT && p->p_stat != SZOMB)
226 *cp++ = 'E';
227 #ifdef NEWVM
228 if (flag & P_PPWAIT)
229 #else
230 if (flag & SVFORK)
231 #endif
232 *cp++ = 'V';
233 #ifdef NEWVM
234 if (flag & (P_SYSTEM | P_NOSWAP | P_PHYSIO))
235 #else
236 if (flag & (SSYS|SLOCK|SULOCK|SKEEP|SPHYSIO))
237 #endif
238 *cp++ = 'L';
239 if (KI_EPROC(k)->e_flag & EPROC_SLEADER)
240 *cp++ = 's';
241 if ((flag & P_CONTROLT) && KI_EPROC(k)->e_pgid == KI_EPROC(k)->e_tpgid)
242 *cp++ = '+';
243 *cp = '\0';
244 (void)printf("%-*s", v->width, buf);
245 }
246
247 void
248 pri(k, ve)
249 KINFO *k;
250 VARENT *ve;
251 {
252 VAR *v;
253
254 v = ve->var;
255 (void)printf("%*d", v->width, KI_PROC(k)->p_priority - PZERO);
256 }
257
258 void
259 uname(k, ve)
260 KINFO *k;
261 VARENT *ve;
262 {
263 VAR *v;
264
265 v = ve->var;
266 #ifndef NEWVM
267 (void)printf("%-*s",
268 (int)v->width, user_from_uid(KI_PROC(k)->p_uid, 0));
269 #else
270 (void)printf("%-*s",
271 (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0));
272 #endif
273 }
274
275 void
276 runame(k, ve)
277 KINFO *k;
278 VARENT *ve;
279 {
280 VAR *v;
281
282 v = ve->var;
283 #ifndef NEWVM
284 (void)printf("%-*s",
285 (int)v->width, user_from_uid(KI_PROC(k)->p_ruid, 0));
286 #else
287 (void)printf("%-*s",
288 (int)v->width, user_from_uid(KI_EPROC(k)->e_pcred.p_ruid, 0));
289 #endif
290 }
291
292 void
293 tdev(k, ve)
294 KINFO *k;
295 VARENT *ve;
296 {
297 VAR *v;
298 dev_t dev;
299 char buff[16];
300
301 v = ve->var;
302 dev = KI_EPROC(k)->e_tdev;
303 if (dev == NODEV)
304 (void)printf("%*s", v->width, "??");
305 else {
306 (void)snprintf(buff, sizeof(buff),
307 "%d/%d", major(dev), minor(dev));
308 (void)printf("%*s", v->width, buff);
309 }
310 }
311
312 void
313 tname(k, ve)
314 KINFO *k;
315 VARENT *ve;
316 {
317 VAR *v;
318 dev_t dev;
319 char *ttname;
320
321 v = ve->var;
322 dev = KI_EPROC(k)->e_tdev;
323 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
324 (void)printf("%-*s", v->width, "??");
325 else {
326 if (strncmp(ttname, "tty", 3) == 0)
327 ttname += 3;
328 (void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
329 KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-');
330 }
331 }
332
333 void
334 longtname(k, ve)
335 KINFO *k;
336 VARENT *ve;
337 {
338 VAR *v;
339 dev_t dev;
340 char *ttname;
341
342 v = ve->var;
343 dev = KI_EPROC(k)->e_tdev;
344 if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
345 (void)printf("%-*s", v->width, "??");
346 else
347 (void)printf("%-*s", v->width, ttname);
348 }
349
350 void
351 started(k, ve)
352 KINFO *k;
353 VARENT *ve;
354 {
355 VAR *v;
356 static time_t now;
357 struct tm *tp;
358 char buf[100];
359
360 v = ve->var;
361 if (!k->ki_u.u_valid) {
362 (void)printf("%-*s", v->width, "-");
363 return;
364 }
365
366 tp = localtime(&k->ki_u.u_start.tv_sec);
367 if (!now)
368 (void)time(&now);
369 if (now - k->ki_u.u_start.tv_sec < 24 * SECSPERHOUR) {
370 /* I *hate* SCCS... */
371 static char fmt[] = __CONCAT("%l:%", "M%p");
372 (void)strftime(buf, sizeof(buf) - 1, fmt, tp);
373 } else if (now - k->ki_u.u_start.tv_sec < 7 * SECSPERDAY) {
374 /* I *hate* SCCS... */
375 static char fmt[] = __CONCAT("%a%", "I%p");
376 (void)strftime(buf, sizeof(buf) - 1, fmt, tp);
377 } else
378 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
379 (void)printf("%-*s", v->width, buf);
380 }
381
382 void
383 lstarted(k, ve)
384 KINFO *k;
385 VARENT *ve;
386 {
387 VAR *v;
388 char buf[100];
389
390 v = ve->var;
391 if (!k->ki_u.u_valid) {
392 (void)printf("%-*s", v->width, "-");
393 return;
394 }
395 (void)strftime(buf, sizeof(buf) -1, "%C",
396 localtime(&k->ki_u.u_start.tv_sec));
397 (void)printf("%-*s", v->width, buf);
398 }
399
400 void
401 wchan(k, ve)
402 KINFO *k;
403 VARENT *ve;
404 {
405 VAR *v;
406
407 v = ve->var;
408 if (KI_PROC(k)->p_wchan) {
409 if (KI_PROC(k)->p_wmesg)
410 (void)printf("%-*.*s", v->width, v->width,
411 KI_EPROC(k)->e_wmesg);
412 else
413 (void)printf("%-*x", v->width,
414 (int)KI_PROC(k)->p_wchan &~ KERNBASE);
415 } else
416 (void)printf("%-*s", v->width, "-");
417 }
418
419 #define pgtok(a) (((a)*NBPG)/1024)
420
421 void
422 vsize(k, ve)
423 KINFO *k;
424 VARENT *ve;
425 {
426 VAR *v;
427
428 v = ve->var;
429 (void)printf("%*d", v->width,
430 #ifndef NEWVM
431 pgtok(KI_PROC(k)->p_dsize +
432 KI_PROC(k)->p_ssize + KI_EPROC(k)->e_xsize));
433 #else
434 pgtok(KI_EPROC(k)->e_vm.vm_dsize + KI_EPROC(k)->e_vm.vm_ssize +
435 KI_EPROC(k)->e_vm.vm_tsize));
436 #endif
437 }
438
439 void
440 rssize(k, ve)
441 KINFO *k;
442 VARENT *ve;
443 {
444 VAR *v;
445
446 v = ve->var;
447 #ifndef NEWVM
448 (void)printf("%*d", v->width,
449 pgtok(KI_PROC(k)->p_rssize + (KI_EPROC(k)->e_xccount ?
450 (KI_EPROC(k)->e_xrssize / KI_EPROC(k)->e_xccount) : 0)));
451 #else
452 /* XXX don't have info about shared */
453 (void)printf("%*d", v->width, pgtok(KI_EPROC(k)->e_vm.vm_rssize));
454 #endif
455 }
456
457 void
458 p_rssize(k, ve) /* doesn't account for text */
459 KINFO *k;
460 VARENT *ve;
461 {
462 VAR *v;
463
464 v = ve->var;
465 #ifndef NEWVM
466 (void)printf("%*d", v->width, pgtok(KI_PROC(k)->p_rssize));
467 #else
468 (void)printf("%*d", v->width, pgtok(KI_EPROC(k)->e_vm.vm_rssize));
469 #endif
470 }
471
472 void
473 cputime(k, ve)
474 KINFO *k;
475 VARENT *ve;
476 {
477 VAR *v;
478 long secs;
479 long psecs; /* "parts" of a second. first micro, then centi */
480 char obuff[128];
481
482 v = ve->var;
483 if (KI_PROC(k)->p_stat == SZOMB || !k->ki_u.u_valid) {
484 secs = 0;
485 psecs = 0;
486 } else {
487 /*
488 * This counts time spent handling interrupts. We could
489 * fix this, but it is not 100% trivial (and interrupt
490 * time fractions only work on the sparc anyway). XXX
491 */
492 secs = KI_PROC(k)->p_rtime.tv_sec;
493 psecs = KI_PROC(k)->p_rtime.tv_usec;
494 if (sumrusage) {
495 secs += k->ki_u.u_cru.ru_utime.tv_sec +
496 k->ki_u.u_cru.ru_stime.tv_sec;
497 psecs += k->ki_u.u_cru.ru_utime.tv_usec +
498 k->ki_u.u_cru.ru_stime.tv_usec;
499 }
500 /*
501 * round and scale to 100's
502 */
503 psecs = (psecs + 5000) / 10000;
504 secs += psecs / 100;
505 psecs = psecs % 100;
506 }
507 (void)snprintf(obuff, sizeof(obuff),
508 "%3ld:%02ld.%02ld", secs/60, secs%60, psecs);
509 (void)printf("%*s", v->width, obuff);
510 }
511
512 double
513 getpcpu(k)
514 KINFO *k;
515 {
516 struct proc *p;
517 static int failure;
518
519 if (!nlistread)
520 failure = donlist();
521 if (failure)
522 return (0.0);
523
524 p = KI_PROC(k);
525 #define fxtofl(fixpt) ((double)(fixpt) / fscale)
526
527 /* XXX - I don't like this */
528 if (p->p_swtime == 0 || (p->p_flag & P_INMEM) == 0)
529 return (0.0);
530 if (rawcpu)
531 return (100.0 * fxtofl(p->p_pctcpu));
532 return (100.0 * fxtofl(p->p_pctcpu) /
533 (1.0 - exp(p->p_swtime * log(fxtofl(ccpu)))));
534 }
535
536 void
537 pcpu(k, ve)
538 KINFO *k;
539 VARENT *ve;
540 {
541 VAR *v;
542
543 v = ve->var;
544 (void)printf("%*.1f", v->width, getpcpu(k));
545 }
546
547 double
548 getpmem(k)
549 KINFO *k;
550 {
551 static int failure;
552 struct proc *p;
553 struct eproc *e;
554 double fracmem;
555 int szptudot;
556
557 if (!nlistread)
558 failure = donlist();
559 if (failure)
560 return (0.0);
561
562 p = KI_PROC(k);
563 e = KI_EPROC(k);
564 if ((p->p_flag & P_INMEM) == 0)
565 return (0.0);
566 #ifndef NEWVM
567 szptudot = UPAGES + clrnd(ctopt(p->p_dsize + p->p_ssize + e->e_xsize));
568 fracmem = ((float)p->p_rssize + szptudot)/CLSIZE/mempages;
569 if (p->p_textp && e->e_xccount)
570 fracmem += ((float)e->e_xrssize)/CLSIZE/e->e_xccount/mempages;
571 #else
572 /* XXX want pmap ptpages, segtab, etc. (per architecture) */
573 szptudot = UPAGES;
574 /* XXX don't have info about shared */
575 fracmem = ((float)e->e_vm.vm_rssize + szptudot)/CLSIZE/mempages;
576 #endif
577 return (100.0 * fracmem);
578 }
579
580 void
581 pmem(k, ve)
582 KINFO *k;
583 VARENT *ve;
584 {
585 VAR *v;
586
587 v = ve->var;
588 (void)printf("%*.1f", v->width, getpmem(k));
589 }
590
591 void
592 pagein(k, ve)
593 KINFO *k;
594 VARENT *ve;
595 {
596 VAR *v;
597
598 v = ve->var;
599 (void)printf("%*d", v->width,
600 k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0);
601 }
602
603 void
604 maxrss(k, ve)
605 KINFO *k;
606 VARENT *ve;
607 {
608 VAR *v;
609
610 v = ve->var;
611 #ifndef NEWVM /* not yet */
612 if (KI_PROC(k)->p_maxrss != (RLIM_INFINITY/NBPG))
613 (void)printf("%*d", v->width, pgtok(KI_PROC(k)->p_maxrss));
614 else
615 #endif
616 (void)printf("%*s", v->width, "-");
617 }
618
619 void
620 tsize(k, ve)
621 KINFO *k;
622 VARENT *ve;
623 {
624 VAR *v;
625
626 v = ve->var;
627 #ifndef NEWVM
628 (void)printf("%*d", v->width, pgtok(KI_EPROC(k)->e_xsize));
629 #else
630 (void)printf("%*d", v->width, pgtok(KI_EPROC(k)->e_vm.vm_tsize));
631 #endif
632 }
633
634 #ifndef NEWVM
635 void
636 trss(k, ve)
637 KINFO *k;
638 VARENT *ve;
639 {
640 VAR *v;
641
642 v = ve->var;
643 (void)printf("%*d", v->width, pgtok(KI_EPROC(k)->e_xrssize));
644 }
645 #endif
646
647 /*
648 * Generic output routines. Print fields from various prototype
649 * structures.
650 */
651 static void
652 printval(bp, v)
653 char *bp;
654 VAR *v;
655 {
656 static char ofmt[32] = "%";
657 char *fcp, *cp;
658
659 cp = ofmt + 1;
660 fcp = v->fmt;
661 if (v->flag & LJUST)
662 *cp++ = '-';
663 *cp++ = '*';
664 while (*cp++ = *fcp++);
665
666 switch (v->type) {
667 case CHAR:
668 (void)printf(ofmt, v->width, *(char *)bp);
669 break;
670 case UCHAR:
671 (void)printf(ofmt, v->width, *(u_char *)bp);
672 break;
673 case SHORT:
674 (void)printf(ofmt, v->width, *(short *)bp);
675 break;
676 case USHORT:
677 (void)printf(ofmt, v->width, *(u_short *)bp);
678 break;
679 case LONG:
680 (void)printf(ofmt, v->width, *(long *)bp);
681 break;
682 case ULONG:
683 (void)printf(ofmt, v->width, *(u_long *)bp);
684 break;
685 case KPTR:
686 (void)printf(ofmt, v->width, *(u_long *)bp &~ KERNBASE);
687 break;
688 default:
689 errx(1, "unknown type %d", v->type);
690 }
691 }
692
693 void
694 pvar(k, ve)
695 KINFO *k;
696 VARENT *ve;
697 {
698 VAR *v;
699
700 v = ve->var;
701 printval((char *)((char *)KI_PROC(k) + v->off), v);
702 }
703
704 void
705 evar(k, ve)
706 KINFO *k;
707 VARENT *ve;
708 {
709 VAR *v;
710
711 v = ve->var;
712 printval((char *)((char *)KI_EPROC(k) + v->off), v);
713 }
714
715 void
716 uvar(k, ve)
717 KINFO *k;
718 VARENT *ve;
719 {
720 VAR *v;
721
722 v = ve->var;
723 if (k->ki_u.u_valid)
724 printval((char *)((char *)&k->ki_u + v->off), v);
725 else
726 (void)printf("%*s", v->width, "-");
727 }
728
729 void
730 rvar(k, ve)
731 KINFO *k;
732 VARENT *ve;
733 {
734 VAR *v;
735
736 v = ve->var;
737 if (k->ki_u.u_valid)
738 printval((char *)((char *)(&k->ki_u.u_ru) + v->off), v);
739 else
740 (void)printf("%*s", v->width, "-");
741 }
742