Merge from vendor branch LESS:
[dragonfly.git] / bin / ps / print.c
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  * @(#)print.c  8.6 (Berkeley) 4/16/94
34  * $FreeBSD: src/bin/ps/print.c,v 1.36.2.4 2002/11/30 13:00:14 tjr Exp $
35  * $DragonFly: src/bin/ps/print.c,v 1.10 2004/03/19 17:47:49 cpressey Exp $
36  */
37
38 #include <sys/param.h>
39 #include <sys/time.h>
40 #include <sys/resource.h>
41 #include <sys/stat.h>
42
43 #include <sys/ucred.h>
44 #include <sys/user.h>
45 #include <sys/sysctl.h>
46 #include <sys/rtprio.h>
47 #include <vm/vm.h>
48
49 #include <err.h>
50 #include <langinfo.h>
51 #include <locale.h>
52 #include <math.h>
53 #include <nlist.h>
54 #include <stddef.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <unistd.h>
58 #include <string.h>
59 #include <vis.h>
60
61 #include "ps.h"
62
63 void
64 printheader(void)
65 {
66         const VAR *v;
67         struct varent *vent;
68         int allempty;
69
70         allempty = 1;
71         for (vent = vhead; vent; vent = vent->next)
72                 if (*vent->var->header != '\0') {
73                         allempty = 0;
74                         break;
75                 }
76         if (allempty)
77                 return;
78         for (vent = vhead; vent; vent = vent->next) {
79                 v = vent->var;
80                 if (v->flag & LJUST) {
81                         if (vent->next == NULL) /* last one */
82                                 (void)printf("%s", v->header);
83                         else
84                                 (void)printf("%-*s", v->width, v->header);
85                 } else
86                         (void)printf("%*s", v->width, v->header);
87                 if (vent->next != NULL)
88                         (void)putchar(' ');
89         }
90         (void)putchar('\n');
91 }
92
93 void
94 command(const KINFO *k, const VARENT *ve)
95 {
96         const VAR *v;
97         int left;
98         char *cp, *vis_env, *vis_args;
99
100         v = ve->var;
101
102         if (cflag) {
103                 if (ve->next == NULL)   /* last field, don't pad */
104                         (void)printf("%s", KI_THREAD(k)->td_comm);
105                 else
106                         (void)printf("%-*s", v->width, KI_THREAD(k)->td_comm);
107                 return;
108         }
109
110         if ((vis_args = malloc(strlen(k->ki_args) * 4 + 1)) == NULL)
111                 err(1, NULL);
112         strvis(vis_args, k->ki_args, VIS_TAB | VIS_NL | VIS_NOSLASH);
113         if (k->ki_env) {
114                 if ((vis_env = malloc(strlen(k->ki_env) * 4 + 1)) == NULL)
115                         err(1, NULL);
116                 strvis(vis_env, k->ki_env, VIS_TAB | VIS_NL | VIS_NOSLASH);
117         } else
118                 vis_env = NULL;
119
120         if (ve->next == NULL) {
121                 /* last field */
122                 if (termwidth == UNLIMITED) {
123                         if (vis_env)
124                                 (void)printf("%s ", vis_env);
125                         (void)printf("%s", vis_args);
126                 } else {
127                         left = termwidth - (totwidth - v->width);
128                         if (left < 1) /* already wrapped, just use std width */
129                                 left = v->width;
130                         if ((cp = vis_env) != NULL) {
131                                 while (--left >= 0 && *cp)
132                                         (void)putchar(*cp++);
133                                 if (--left >= 0)
134                                         putchar(' ');
135                         }
136                         for (cp = vis_args; --left >= 0 && *cp != '\0';)
137                                 (void)putchar(*cp++);
138                 }
139         } else
140                 /* XXX env? */
141                 (void)printf("%-*.*s", v->width, v->width, vis_args);
142         free(vis_args);
143         if (vis_env != NULL)
144                 free(vis_env);
145 }
146
147 void
148 ucomm(const KINFO *k, const VARENT *ve)
149 {
150         const VAR *v;
151
152         v = ve->var;
153         (void)printf("%-*s", v->width, KI_THREAD(k)->td_comm);
154 }
155
156 void
157 logname(const KINFO *k, const VARENT *ve)
158 {
159         const VAR *v;
160         char *s;
161
162         v = ve->var;
163         (void)printf("%-*s", v->width, (s = KI_EPROC(k)->e_login, *s) ? s : "-");
164 }
165
166 void
167 state(const KINFO *k, const VARENT *ve)
168 {
169         struct proc *p;
170         int flag;
171         char *cp;
172         const 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)     /* interruptable (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                 if (KI_THREAD(k)->td_flags & TDF_RUNNING) {
197                     ++cp;
198                     sprintf(cp, "%d", KI_EPROC(k)->e_cpuid);
199                     while (cp[1])
200                         ++cp;
201                 }
202                 break;
203
204         case SZOMB:
205                 *cp = 'Z';
206                 break;
207
208         default:
209                 *cp = '?';
210         }
211         cp++;
212         if (!(flag & P_INMEM))
213                 *cp++ = 'W';
214         if (p->p_nice < NZERO)
215                 *cp++ = '<';
216         else if (p->p_nice > NZERO)
217                 *cp++ = 'N';
218         if (flag & P_TRACED)
219                 *cp++ = 'X';
220         if (flag & P_WEXIT && p->p_stat != SZOMB)
221                 *cp++ = 'E';
222         if (flag & P_PPWAIT)
223                 *cp++ = 'V';
224         if ((flag & P_SYSTEM) || p->p_lock > 0)
225                 *cp++ = 'L';
226         if (KI_EPROC(k)->e_flag & EPROC_SLEADER)
227                 *cp++ = 's';
228         if ((flag & P_CONTROLT) && KI_EPROC(k)->e_pgid == KI_EPROC(k)->e_tpgid)
229                 *cp++ = '+';
230         if (flag & P_JAILED)
231                 *cp++ = 'J';
232         *cp = '\0';
233         (void)printf("%-*s", v->width, buf);
234 }
235
236 void
237 pri(const KINFO *k, const VARENT *ve)
238 {
239         const VAR *v;
240
241         v = ve->var;
242         (void)printf("%*d", v->width, KI_PROC(k)->p_priority);
243 }
244
245 void
246 uname(const KINFO *k, const VARENT *ve)
247 {
248         const VAR *v;
249
250         v = ve->var;
251         (void)printf("%-*s",
252             (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0));
253 }
254
255 int
256 s_uname(const KINFO *k)
257 {
258             return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0)));
259 }
260
261 void
262 runame(const KINFO *k, const VARENT *ve)
263 {
264         const VAR *v;
265
266         v = ve->var;
267         (void)printf("%-*s",
268             (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0));
269 }
270
271 int
272 s_runame(const KINFO *k)
273 {
274             return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0)));
275 }
276
277 void
278 tdev(const KINFO *k, const VARENT *ve)
279 {
280         const VAR *v;
281         dev_t dev;
282         char buff[16];
283
284         v = ve->var;
285         dev = KI_EPROC(k)->e_tdev;
286         if (dev == NODEV)
287                 (void)printf("%*s", v->width, "??");
288         else {
289                 (void)snprintf(buff, sizeof(buff),
290                     "%d/%d", major(dev), minor(dev));
291                 (void)printf("%*s", v->width, buff);
292         }
293 }
294
295 void
296 tname(const KINFO *k, const VARENT *ve)
297 {
298         const VAR *v;
299         dev_t dev;
300         char *ttname;
301
302         v = ve->var;
303         dev = KI_EPROC(k)->e_tdev;
304         if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
305                 (void)printf("%*s ", v->width-1, "??");
306         else {
307                 if (strncmp(ttname, "tty", 3) == 0 ||
308                     strncmp(ttname, "cua", 3) == 0)
309                         ttname += 3;
310                 (void)printf("%*.*s%c", v->width-1, v->width-1, ttname,
311                         KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-');
312         }
313 }
314
315 void
316 longtname(const KINFO *k, const VARENT *ve)
317 {
318         const VAR *v;
319         dev_t dev;
320         char *ttname;
321
322         v = ve->var;
323         dev = KI_EPROC(k)->e_tdev;
324         if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
325                 (void)printf("%-*s", v->width, "??");
326         else
327                 (void)printf("%-*s", v->width, ttname);
328 }
329
330 void
331 started(const KINFO *k, const VARENT *ve)
332 {
333         const VAR *v;
334         static time_t now;
335         time_t then;
336         struct tm *tp;
337         char buf[100];
338         static int  use_ampm = -1;
339
340         v = ve->var;
341         if (!k->ki_u.u_valid) {
342                 (void)printf("%-*s", v->width, "-");
343                 return;
344         }
345
346         if (use_ampm < 0)
347                 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
348
349         then = k->ki_u.u_start.tv_sec;
350         tp = localtime(&then);
351         if (!now)
352                 (void)time(&now);
353         if (now - k->ki_u.u_start.tv_sec < 24 * 3600) {
354                 (void)strftime(buf, sizeof(buf) - 1,
355                 use_ampm ? "%l:%M%p" : "%k:%M  ", tp);
356         } else if (now - k->ki_u.u_start.tv_sec < 7 * 86400) {
357                 (void)strftime(buf, sizeof(buf) - 1,
358                 use_ampm ? "%a%I%p" : "%a%H  ", tp);
359         } else
360                 (void)strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
361         (void)printf("%-*s", v->width, buf);
362 }
363
364 void
365 lstarted(const KINFO *k, const VARENT *ve)
366 {
367         const VAR *v;
368         time_t then;
369         char buf[100];
370
371         v = ve->var;
372         if (!k->ki_u.u_valid) {
373                 (void)printf("%-*s", v->width, "-");
374                 return;
375         }
376         then = k->ki_u.u_start.tv_sec;
377         (void)strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
378         (void)printf("%-*s", v->width, buf);
379 }
380
381 void
382 wchan(const KINFO *k, const VARENT *ve)
383 {
384         const VAR *v;
385
386         v = ve->var;
387         if (KI_THREAD(k)->td_wchan) {
388                 if (KI_THREAD(k)->td_wmesg)
389                         (void)printf("%-*.*s", v->width, v->width,
390                                       KI_EPROC(k)->e_wmesg);
391                 else
392                         (void)printf("%-*lx", v->width,
393                             (long)KI_THREAD(k)->td_wchan);
394         } else
395                 (void)printf("%-*s", v->width, "-");
396 }
397
398 #ifndef pgtok
399 #define pgtok(a)        (((a)*getpagesize())/1024)
400 #endif
401
402 void
403 vsize(const KINFO *k, const VARENT *ve)
404 {
405         const VAR *v;
406
407         v = ve->var;
408         (void)printf("%*d", v->width,
409             (KI_EPROC(k)->e_vm.vm_map.size/1024));
410 }
411
412 void
413 rssize(const KINFO *k, const VARENT *ve)
414 {
415         const VAR *v;
416
417         v = ve->var;
418         /* XXX don't have info about shared */
419         (void)printf("%*lu", v->width,
420             (u_long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
421 }
422
423 void
424 p_rssize(const KINFO *k, const VARENT *ve)      /* doesn't account for text */
425 {
426         const VAR *v;
427
428         v = ve->var;
429         (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
430 }
431
432 void
433 cputime(const KINFO *k, const VARENT *ve)
434 {
435         const VAR *v;
436         long secs;
437         long psecs;     /* "parts" of a second. first micro, then centi */
438         char obuff[128];
439         static char decimal_point = 0;
440
441         if (!decimal_point)
442                 decimal_point = localeconv()->decimal_point[0];
443         v = ve->var;
444         if (KI_PROC(k)->p_stat == SZOMB || !k->ki_u.u_valid) {
445                 secs = 0;
446                 psecs = 0;
447         } else {
448                 u_int64_t timeus;
449
450                 /*
451                  * This counts time spent handling interrupts.  We could
452                  * fix this, but it is not 100% trivial (and interrupt
453                  * time fractions only work on the sparc anyway).       XXX
454                  */
455                 timeus = KI_EPROC(k)->e_uticks + KI_EPROC(k)->e_sticks;
456                 secs = timeus / 1000000;
457                 psecs = timeus % 1000000;
458                 if (sumrusage) {
459                         secs += k->ki_u.u_cru.ru_utime.tv_sec +
460                                 k->ki_u.u_cru.ru_stime.tv_sec;
461                         psecs += k->ki_u.u_cru.ru_utime.tv_usec +
462                                 k->ki_u.u_cru.ru_stime.tv_usec;
463                 }
464                 /*
465                  * round and scale to 100's
466                  */
467                 psecs = (psecs + 5000) / 10000;
468                 secs += psecs / 100;
469                 psecs = psecs % 100;
470         }
471         (void)snprintf(obuff, sizeof(obuff),
472             "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs);
473         (void)printf("%*s", v->width, obuff);
474 }
475
476 double
477 getpcpu(const KINFO *k)
478 {
479         const struct proc *p;
480         static int failure;
481
482         if (!nlistread)
483                 failure = donlist();
484         if (failure)
485                 return (0.0);
486
487         p = KI_PROC(k);
488 #define fxtofl(fixpt)   ((double)(fixpt) / fscale)
489
490         /* XXX - I don't like this */
491         if (p->p_swtime == 0 || (p->p_flag & P_INMEM) == 0)
492                 return (0.0);
493         if (rawcpu)
494                 return (100.0 * fxtofl(p->p_pctcpu));
495         return (100.0 * fxtofl(p->p_pctcpu) /
496                 (1.0 - exp(p->p_swtime * log(fxtofl(ccpu)))));
497 }
498
499 void
500 pcpu(const KINFO *k, const VARENT *ve)
501 {
502         const VAR *v;
503
504         v = ve->var;
505         (void)printf("%*.1f", v->width, getpcpu(k));
506 }
507
508 void
509 pnice(const KINFO *k, const VARENT *ve)
510 {
511         const VAR *v;
512         int niceval;
513
514         switch (KI_PROC(k)->p_rtprio.type) {
515         case RTP_PRIO_REALTIME:
516                 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_PROC(k)->p_rtprio.prio;
517                 break;
518         case RTP_PRIO_IDLE:
519                 niceval = PRIO_MAX + 1 + KI_PROC(k)->p_rtprio.prio;
520                 break;
521         case RTP_PRIO_THREAD:
522                 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_PROC(k)->p_rtprio.prio;
523                 break;
524         default:
525                 niceval = KI_PROC(k)->p_nice - NZERO;
526                 break;
527         }
528         v = ve->var;
529         (void)printf("%*d", v->width, niceval);
530 }
531
532
533 double
534 getpmem(const KINFO *k)
535 {
536         static int failure;
537         struct proc *p;
538         struct eproc *e;
539         double fracmem;
540         int szptudot;
541
542         if (!nlistread)
543                 failure = donlist();
544         if (failure)
545                 return (0.0);
546
547         p = KI_PROC(k);
548         e = KI_EPROC(k);
549         if ((p->p_flag & P_INMEM) == 0)
550                 return (0.0);
551         /* XXX want pmap ptpages, segtab, etc. (per architecture) */
552         szptudot = UPAGES;
553         /* XXX don't have info about shared */
554         fracmem = ((float)e->e_vm.vm_rssize + szptudot)/mempages;
555         return (100.0 * fracmem);
556 }
557
558 void
559 pmem(const KINFO *k, const VARENT *ve)
560 {
561         const VAR *v;
562
563         v = ve->var;
564         (void)printf("%*.1f", v->width, getpmem(k));
565 }
566
567 void
568 pagein(const KINFO *k, const VARENT *ve)
569 {
570         const VAR *v;
571
572         v = ve->var;
573         (void)printf("%*ld", v->width,
574             k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0);
575 }
576
577 /* ARGSUSED */
578 void
579 maxrss(const KINFO *k, const VARENT *ve)
580 {
581         const VAR *v;
582
583         v = ve->var;
584         /* XXX not yet */
585         (void)printf("%*s", v->width, "-");
586 }
587
588 void
589 tsize(const KINFO *k, const VARENT *ve)
590 {
591         const VAR *v;
592
593         v = ve->var;
594         (void)printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_tsize));
595 }
596
597 void
598 rtprior(const KINFO *k, const VARENT *ve)
599 {
600         const VAR *v;
601         struct rtprio *prtp;
602         char str[8];
603         unsigned prio, type;
604  
605         v = ve->var;
606         prtp = (struct rtprio *) ((char *)KI_PROC(k) + v->off);
607         prio = prtp->prio;
608         type = prtp->type;
609         switch (type) {
610         case RTP_PRIO_REALTIME:
611                 snprintf(str, sizeof(str), "real:%u", prio);
612                 break;
613         case RTP_PRIO_NORMAL:
614                 strncpy(str, "normal", sizeof(str));
615                 break;
616         case RTP_PRIO_IDLE:
617                 snprintf(str, sizeof(str), "idle:%u", prio);
618                 break;
619         default:
620                 snprintf(str, sizeof(str), "%u:%u", type, prio);
621                 break;
622         }
623         str[sizeof(str) - 1] = '\0';
624         (void)printf("%*s", v->width, str);
625 }
626
627 /*
628  * Generic output routines.  Print fields from various prototype
629  * structures.
630  */
631 static void
632 printval(const char *bp, const VAR *v)
633 {
634         static char ofmt[32] = "%";
635         const char *fcp;
636         char *cp;
637
638         cp = ofmt + 1;
639         fcp = v->fmt;
640         if (v->flag & LJUST)
641                 *cp++ = '-';
642         *cp++ = '*';
643         while ((*cp++ = *fcp++));
644
645         switch (v->type) {
646         case CHAR:
647                 (void)printf(ofmt, v->width, *(const char *)bp);
648                 break;
649         case UCHAR:
650                 (void)printf(ofmt, v->width, *(const u_char *)bp);
651                 break;
652         case SHORT:
653                 (void)printf(ofmt, v->width, *(const short *)bp);
654                 break;
655         case USHORT:
656                 (void)printf(ofmt, v->width, *(const u_short *)bp);
657                 break;
658         case INT:
659                 (void)printf(ofmt, v->width, *(const int *)bp);
660                 break;
661         case UINT:
662                 (void)printf(ofmt, v->width, *(const u_int *)bp);
663                 break;
664         case LONG:
665                 (void)printf(ofmt, v->width, *(const long *)bp);
666                 break;
667         case ULONG:
668                 (void)printf(ofmt, v->width, *(const u_long *)bp);
669                 break;
670         case KPTR:
671                 (void)printf(ofmt, v->width, *(const u_long *)bp);
672                 break;
673         default:
674                 errx(1, "unknown type %d", v->type);
675         }
676 }
677
678 void
679 pvar(const KINFO *k, const VARENT *ve)
680 {
681         const VAR *v;
682
683         v = ve->var;
684         printval((char *)((char *)KI_PROC(k) + v->off), v);
685 }
686
687 void
688 tvar(const KINFO *k, const VARENT *ve)
689 {
690         const VAR *v;
691
692         v = ve->var;
693         printval((char *)((char *)KI_THREAD(k) + v->off), v);
694 }
695
696 void
697 evar(const KINFO *k, const VARENT *ve)
698 {
699         const VAR *v;
700
701         v = ve->var;
702         printval((char *)((char *)KI_EPROC(k) + v->off), v);
703 }
704
705 void
706 uvar(const KINFO *k, const VARENT *ve)
707 {
708         const VAR *v;
709
710         v = ve->var;
711         if (k->ki_u.u_valid)
712                 printval(((const char *)&k->ki_u + v->off), v);
713         else
714                 (void)printf("%*s", v->width, "-");
715 }
716
717 void
718 rvar(const KINFO *k, const VARENT *ve)
719 {
720         const VAR *v;
721
722         v = ve->var;
723         if (k->ki_u.u_valid)
724                 printval(((const char *)&k->ki_u.u_ru + v->off), v);
725         else
726                 (void)printf("%*s", v->width, "-");
727 }