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