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