xref: /original-bsd/bin/ps/nlist.c (revision 4092c5cc)
1 /*-
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)nlist.c	8.1 (Berkeley) 05/31/93";
10 #endif /* not lint */
11 
12 #include <sys/param.h>
13 #include <sys/time.h>
14 #include <sys/proc.h>
15 #include <sys/resource.h>
16 
17 #include <err.h>
18 #include <errno.h>
19 #include <kvm.h>
20 #include <nlist.h>
21 #include <stdio.h>
22 #include <string.h>
23 
24 #include "ps.h"
25 
26 #ifdef SPPWAIT
27 #define NEWVM
28 #endif
29 
30 struct	nlist psnl[] = {
31 	{"_fscale"},
32 #define	X_FSCALE	0
33 	{"_ccpu"},
34 #define	X_CCPU		1
35 #ifdef NEWVM
36 	{"_avail_start"},
37 #define	X_AVAILSTART	2
38 	{"_avail_end"},
39 #define	X_AVAILEND	3
40 #else
41 	{"_ecmx"},
42 #define	X_ECMX		2
43 #endif
44 	{NULL}
45 };
46 
47 fixpt_t	ccpu;				/* kernel _ccpu variable */
48 int	nlistread;			/* if nlist already read. */
49 int	mempages;			/* number of pages of phys. memory */
50 int	fscale;				/* kernel _fscale variable */
51 
52 extern kvm_t *kd;
53 
54 #define kread(x, v) \
55 	kvm_read(kd, psnl[x].n_value, (char *)&v, sizeof v) != sizeof(v)
56 
57 int
58 donlist()
59 {
60 	extern int eval;
61 	int rval;
62 #ifdef NEWVM
63 	int tmp;
64 #endif
65 
66 	rval = 0;
67 	nlistread = 1;
68 	if (kvm_nlist(kd, psnl)) {
69 		nlisterr(psnl);
70 		eval = 1;
71 		return (1);
72 	}
73 	if (kread(X_FSCALE, fscale)) {
74 		warnx("fscale: %s", kvm_geterr(kd));
75 		eval = rval = 1;
76 	}
77 #ifdef NEWVM
78 	if (kread(X_AVAILEND, mempages)) {
79 		warnx("avail_start: %s", kvm_geterr(kd));
80 		eval = rval = 1;
81 	}
82 	if (kread(X_AVAILSTART, tmp)) {
83 		warnx("avail_end: %s", kvm_geterr(kd));
84 		eval = rval = 1;
85 	}
86 	mempages -= tmp;
87 #else
88 	if (kread(X_ECMX, mempages)) {
89 		warnx("ecmx: %s", kvm_geterr(kd));
90 		eval = rval = 1;
91 	}
92 #endif
93 	if (kread(X_CCPU, ccpu)) {
94 		warnx("ccpu: %s", kvm_geterr(kd));
95 		eval = rval = 1;
96 	}
97 	return (rval);
98 }
99 
100 void
101 nlisterr(nl)
102 	struct nlist nl[];
103 {
104 	int i;
105 
106 	(void)fprintf(stderr, "ps: nlist: can't find following symbols:");
107 	for (i = 0; nl[i].n_name != NULL; i++)
108 		if (nl[i].n_value == 0)
109 			(void)fprintf(stderr, " %s", nl[i].n_name);
110 	(void)fprintf(stderr, "\n");
111 }
112