-W is included in CFLAGS already.
[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.16 2004/11/14 13:58:42 eirikn 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                                 printf("%s", v->header);
83                         else
84                                 printf("%-*s", v->width, v->header);
85                 } else
86                         printf("%*s", v->width, v->header);
87                 if (vent->next != NULL)
88                         putchar(' ');
89         }
90         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                         printf("%s", KI_THREAD(k)->td_comm);
105                 else
106                         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                                 printf("%s ", vis_env);
125                         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                                         putchar(*cp++);
133                                 if (--left >= 0)
134                                         putchar(' ');
135                         }
136                         for (cp = vis_args; --left >= 0 && *cp != '\0';)
137                                 putchar(*cp++);
138                 }
139         } else
140                 /* XXX env? */
141                 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         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         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 if (KI_THREAD(k)->td_flags & TDF_SINTR)
190                         *cp = 'S';
191                 else
192                         *cp = 'D';
193                 break;
194
195         case SRUN:
196         case SIDL:
197                 *cp = 'R';
198                 if (KI_THREAD(k)->td_flags & TDF_RUNNING) {
199                     ++cp;
200                     sprintf(cp, "%d", KI_EPROC(k)->e_cpuid);
201                     while (cp[1])
202                         ++cp;
203                 }
204                 break;
205
206         case SZOMB:
207                 *cp = 'Z';
208                 break;
209
210         default:
211                 *cp = '?';
212         }
213         cp++;
214         if (!(flag & P_INMEM))
215                 *cp++ = 'W';
216         if (p->p_nice < NZERO)
217                 *cp++ = '<';
218         else if (p->p_nice > NZERO)
219                 *cp++ = 'N';
220         if (flag & P_TRACED)
221                 *cp++ = 'X';
222         if (flag & P_WEXIT && p->p_stat != SZOMB)
223                 *cp++ = 'E';
224         if (flag & P_PPWAIT)
225                 *cp++ = 'V';
226         if ((flag & P_SYSTEM) || p->p_lock > 0)
227                 *cp++ = 'L';
228         if (numcpus > 1 && KI_THREAD(k)->td_mpcount_unused == 0)
229                 *cp++ = 'M';
230         if (flag & P_JAILED)
231                 *cp++ = 'J';
232         if (KI_EPROC(k)->e_flag & EPROC_SLEADER)
233                 *cp++ = 's';
234         if ((flag & P_CONTROLT) && KI_EPROC(k)->e_pgid == KI_EPROC(k)->e_tpgid)
235                 *cp++ = '+';
236         *cp = '\0';
237         printf("%-*s", v->width, buf);
238 }
239
240 void
241 pri(const KINFO *k, const VARENT *ve)
242 {
243         const VAR *v;
244
245         v = ve->var;
246         printf("%*d", v->width, KI_PROC(k)->p_priority);
247 }
248
249 void
250 uname(const KINFO *k, const VARENT *ve)
251 {
252         const VAR *v;
253
254         v = ve->var;
255         printf("%-*s",
256             (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0));
257 }
258
259 int
260 s_uname(const KINFO *k)
261 {
262             return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_uid, 0)));
263 }
264
265 void
266 runame(const KINFO *k, const VARENT *ve)
267 {
268         const VAR *v;
269
270         v = ve->var;
271         printf("%-*s",
272             (int)v->width, user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0));
273 }
274
275 int
276 s_runame(const KINFO *k)
277 {
278             return (strlen(user_from_uid(KI_EPROC(k)->e_ucred.cr_ruid, 0)));
279 }
280
281 void
282 tdev(const KINFO *k, const VARENT *ve)
283 {
284         const VAR *v;
285         dev_t dev;
286         char buff[16];
287
288         v = ve->var;
289         dev = KI_EPROC(k)->e_tdev;
290         if (dev == NODEV)
291                 printf("%*s", v->width, "??");
292         else {
293                 snprintf(buff, sizeof(buff),
294                     "%d/%d", major(dev), minor(dev));
295                 printf("%*s", v->width, buff);
296         }
297 }
298
299 void
300 tname(const KINFO *k, const VARENT *ve)
301 {
302         const VAR *v;
303         dev_t dev;
304         char *ttname;
305
306         v = ve->var;
307         dev = KI_EPROC(k)->e_tdev;
308         if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
309                 printf("%*s ", v->width-1, "??");
310         else {
311                 if (strncmp(ttname, "tty", 3) == 0 ||
312                     strncmp(ttname, "cua", 3) == 0)
313                         ttname += 3;
314                 printf("%*.*s%c", v->width-1, v->width-1, ttname,
315                         KI_EPROC(k)->e_flag & EPROC_CTTY ? ' ' : '-');
316         }
317 }
318
319 void
320 longtname(const KINFO *k, const VARENT *ve)
321 {
322         const VAR *v;
323         dev_t dev;
324         char *ttname;
325
326         v = ve->var;
327         dev = KI_EPROC(k)->e_tdev;
328         if (dev == NODEV || (ttname = devname(dev, S_IFCHR)) == NULL)
329                 printf("%-*s", v->width, "??");
330         else
331                 printf("%-*s", v->width, ttname);
332 }
333
334 void
335 started(const KINFO *k, const VARENT *ve)
336 {
337         const VAR *v;
338         static time_t now;
339         time_t then;
340         struct tm *tp;
341         char buf[100];
342         static int  use_ampm = -1;
343
344         v = ve->var;
345         if (!k->ki_u.u_valid) {
346                 printf("%-*s", v->width, "-");
347                 return;
348         }
349
350         if (use_ampm < 0)
351                 use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
352
353         if (KI_THREAD(k)->td_proc != NULL) {
354                 then = k->ki_u.u_start.tv_sec;
355         } else {
356                 then = KI_THREAD(k)->td_start.tv_sec;
357                 if (then < btime.tv_sec) {
358                         then = btime.tv_sec;
359                 }
360         }
361
362         tp = localtime(&then);
363         if (!now)
364                 time(&now);
365         if (now - k->ki_u.u_start.tv_sec < 24 * 3600) {
366                 strftime(buf, sizeof(buf) - 1,
367                 use_ampm ? "%l:%M%p" : "%k:%M  ", tp);
368         } else if (now - k->ki_u.u_start.tv_sec < 7 * 86400) {
369                 strftime(buf, sizeof(buf) - 1,
370                 use_ampm ? "%a%I%p" : "%a%H  ", tp);
371         } else
372                 strftime(buf, sizeof(buf) - 1, "%e%b%y", tp);
373         printf("%-*s", v->width, buf);
374 }
375
376 void
377 lstarted(const KINFO *k, const VARENT *ve)
378 {
379         const VAR *v;
380         time_t then;
381         char buf[100];
382
383         v = ve->var;
384         if (!k->ki_u.u_valid) {
385                 printf("%-*s", v->width, "-");
386                 return;
387         }
388         then = k->ki_u.u_start.tv_sec;
389         strftime(buf, sizeof(buf) -1, "%c", localtime(&then));
390         printf("%-*s", v->width, buf);
391 }
392
393 void
394 wchan(const KINFO *k, const VARENT *ve)
395 {
396         const VAR *v;
397
398         v = ve->var;
399         if (KI_THREAD(k)->td_wchan) {
400                 if (KI_THREAD(k)->td_wmesg)
401                         printf("%-*.*s", v->width, v->width,
402                                       KI_EPROC(k)->e_wmesg);
403                 else
404                         printf("%-*lx", v->width,
405                             (long)KI_THREAD(k)->td_wchan);
406         } else
407                 printf("%-*s", v->width, "-");
408 }
409
410 #ifndef pgtok
411 #define pgtok(a)        (((a)*getpagesize())/1024)
412 #endif
413
414 void
415 vsize(const KINFO *k, const VARENT *ve)
416 {
417         const VAR *v;
418
419         v = ve->var;
420         printf("%*d", v->width,
421             (KI_EPROC(k)->e_vm.vm_map.size/1024));
422 }
423
424 void
425 rssize(const KINFO *k, const VARENT *ve)
426 {
427         const VAR *v;
428
429         v = ve->var;
430         /* XXX don't have info about shared */
431         printf("%*lu", v->width,
432             (u_long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
433 }
434
435 void
436 p_rssize(const KINFO *k, const VARENT *ve)      /* doesn't account for text */
437 {
438         const VAR *v;
439
440         v = ve->var;
441         printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_rssize));
442 }
443
444 void
445 cputime(const KINFO *k, const VARENT *ve)
446 {
447         const VAR *v;
448         long secs;
449         long psecs;     /* "parts" of a second. first micro, then centi */
450         char obuff[128];
451         static char decimal_point = 0;
452
453         if (!decimal_point)
454                 decimal_point = localeconv()->decimal_point[0];
455         v = ve->var;
456         if (KI_PROC(k)->p_stat == SZOMB || !k->ki_u.u_valid) {
457                 secs = 0;
458                 psecs = 0;
459         } else {
460                 u_int64_t timeus;
461
462                 /*
463                  * This counts time spent handling interrupts.  We could
464                  * fix this, but it is not 100% trivial (and interrupt
465                  * time fractions only work on the sparc anyway).       XXX
466                  */
467                 timeus = KI_EPROC(k)->e_uticks + KI_EPROC(k)->e_sticks;
468                 secs = timeus / 1000000;
469                 psecs = timeus % 1000000;
470                 if (sumrusage) {
471                         secs += k->ki_u.u_cru.ru_utime.tv_sec +
472                                 k->ki_u.u_cru.ru_stime.tv_sec;
473                         psecs += k->ki_u.u_cru.ru_utime.tv_usec +
474                                 k->ki_u.u_cru.ru_stime.tv_usec;
475                 }
476                 /*
477                  * round and scale to 100's
478                  */
479                 psecs = (psecs + 5000) / 10000;
480                 secs += psecs / 100;
481                 psecs = psecs % 100;
482         }
483         snprintf(obuff, sizeof(obuff),
484             "%3ld:%02ld%c%02ld", secs/60, secs%60, decimal_point, psecs);
485         printf("%*s", v->width, obuff);
486 }
487
488 double
489 getpcpu(const KINFO *k)
490 {
491         const struct proc *p;
492         static int failure;
493
494         if (!nlistread)
495                 failure = donlist();
496         if (failure)
497                 return (0.0);
498
499         p = KI_PROC(k);
500 #define fxtofl(fixpt)   ((double)(fixpt) / fscale)
501
502         /* XXX - I don't like this */
503         if (p->p_swtime == 0 || (p->p_flag & P_INMEM) == 0)
504                 return (0.0);
505         if (rawcpu)
506                 return (100.0 * fxtofl(p->p_pctcpu));
507         return (100.0 * fxtofl(p->p_pctcpu) /
508                 (1.0 - exp(p->p_swtime * log(fxtofl(ccpu)))));
509 }
510
511 void
512 pcpu(const KINFO *k, const VARENT *ve)
513 {
514         const VAR *v;
515
516         v = ve->var;
517         printf("%*.1f", v->width, getpcpu(k));
518 }
519
520 void
521 pnice(const KINFO *k, const VARENT *ve)
522 {
523         const VAR *v;
524         int niceval;
525
526         switch (KI_PROC(k)->p_rtprio.type) {
527         case RTP_PRIO_REALTIME:
528                 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX + KI_PROC(k)->p_rtprio.prio;
529                 break;
530         case RTP_PRIO_IDLE:
531                 niceval = PRIO_MAX + 1 + KI_PROC(k)->p_rtprio.prio;
532                 break;
533         case RTP_PRIO_THREAD:
534                 niceval = PRIO_MIN - 1 - RTP_PRIO_MAX - KI_PROC(k)->p_rtprio.prio;
535                 break;
536         default:
537                 niceval = KI_PROC(k)->p_nice - NZERO;
538                 break;
539         }
540         v = ve->var;
541         printf("%*d", v->width, niceval);
542 }
543
544
545 double
546 getpmem(const KINFO *k)
547 {
548         static int failure;
549         struct proc *p;
550         struct eproc *e;
551         double fracmem;
552         int szptudot;
553
554         if (!nlistread)
555                 failure = donlist();
556         if (failure)
557                 return (0.0);
558
559         p = KI_PROC(k);
560         e = KI_EPROC(k);
561         if ((p->p_flag & P_INMEM) == 0)
562                 return (0.0);
563         /* XXX want pmap ptpages, segtab, etc. (per architecture) */
564         szptudot = UPAGES;
565         /* XXX don't have info about shared */
566         fracmem = ((float)e->e_vm.vm_rssize + szptudot)/mempages;
567         return (100.0 * fracmem);
568 }
569
570 void
571 pmem(const KINFO *k, const VARENT *ve)
572 {
573         const VAR *v;
574
575         v = ve->var;
576         printf("%*.1f", v->width, getpmem(k));
577 }
578
579 void
580 pagein(const KINFO *k, const VARENT *ve)
581 {
582         const VAR *v;
583
584         v = ve->var;
585         printf("%*ld", v->width,
586             k->ki_u.u_valid ? k->ki_u.u_ru.ru_majflt : 0);
587 }
588
589 /* ARGSUSED */
590 void
591 maxrss(const KINFO *k __unused, const VARENT *ve)
592 {
593         const VAR *v;
594
595         v = ve->var;
596         /* XXX not yet */
597         printf("%*s", v->width, "-");
598 }
599
600 void
601 tsize(const KINFO *k, const VARENT *ve)
602 {
603         const VAR *v;
604
605         v = ve->var;
606         printf("%*ld", v->width, (long)pgtok(KI_EPROC(k)->e_vm.vm_tsize));
607 }
608
609 void
610 rtprior(const KINFO *k, const VARENT *ve)
611 {
612         const VAR *v;
613         struct rtprio *prtp;
614         char str[8];
615         unsigned prio, type;
616  
617         v = ve->var;
618         prtp = (struct rtprio *) ((char *)KI_PROC(k) + v->off);
619         prio = prtp->prio;
620         type = prtp->type;
621         switch (type) {
622         case RTP_PRIO_REALTIME:
623                 snprintf(str, sizeof(str), "real:%u", prio);
624                 break;
625         case RTP_PRIO_NORMAL:
626                 strncpy(str, "normal", sizeof(str));
627                 break;
628         case RTP_PRIO_IDLE:
629                 snprintf(str, sizeof(str), "idle:%u", prio);
630                 break;
631         default:
632                 snprintf(str, sizeof(str), "%u:%u", type, prio);
633                 break;
634         }
635         str[sizeof(str) - 1] = '\0';
636         printf("%*s", v->width, str);
637 }
638
639 /*
640  * Generic output routines.  Print fields from various prototype
641  * structures.
642  */
643 static void
644 printval(const char *bp, const VAR *v)
645 {
646         static char ofmt[32] = "%";
647         const char *fcp;
648         char *cp;
649
650         cp = ofmt + 1;
651         fcp = v->fmt;
652         if (v->flag & LJUST)
653                 *cp++ = '-';
654         *cp++ = '*';
655         while ((*cp++ = *fcp++));
656
657         switch (v->type) {
658         case CHAR:
659                 printf(ofmt, v->width, *(const char *)bp);
660                 break;
661         case UCHAR:
662                 printf(ofmt, v->width, *(const u_char *)bp);
663                 break;
664         case SHORT:
665                 printf(ofmt, v->width, *(const short *)bp);
666                 break;
667         case USHORT:
668                 printf(ofmt, v->width, *(const u_short *)bp);
669                 break;
670         case INT:
671                 printf(ofmt, v->width, *(const int *)bp);
672                 break;
673         case UINT:
674                 printf(ofmt, v->width, *(const u_int *)bp);
675                 break;
676         case LONG:
677                 printf(ofmt, v->width, *(const long *)bp);
678                 break;
679         case ULONG:
680                 printf(ofmt, v->width, *(const u_long *)bp);
681                 break;
682         case KPTR:
683                 printf(ofmt, v->width, *(const u_long *)bp);
684                 break;
685         default:
686                 errx(1, "unknown type %d", v->type);
687         }
688 }
689
690 void
691 pvar(const KINFO *k, const VARENT *ve)
692 {
693         const VAR *v;
694
695         v = ve->var;
696         printval((char *)((char *)KI_PROC(k) + v->off), v);
697 }
698
699 void
700 tvar(const KINFO *k, const VARENT *ve)
701 {
702         const VAR *v;
703
704         v = ve->var;
705         printval((char *)((char *)KI_THREAD(k) + v->off), v);
706 }
707
708 void
709 evar(const KINFO *k, const VARENT *ve)
710 {
711         const VAR *v;
712
713         v = ve->var;
714         printval((char *)((char *)KI_EPROC(k) + v->off), v);
715 }
716
717 void
718 uvar(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 + v->off), v);
725         else
726                 printf("%*s", v->width, "-");
727 }
728
729 void
730 rvar(const KINFO *k, const VARENT *ve)
731 {
732         const VAR *v;
733
734         v = ve->var;
735         if (k->ki_u.u_valid)
736                 printval(((const char *)&k->ki_u.u_ru + v->off), v);
737         else
738                 printf("%*s", v->width, "-");
739 }