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