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