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