xref: /freebsd/bin/ps/nlist.c (revision ef5d438ed4bc17ad7ece3e40fe4d1f9baf3aadf7)
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  *	$Id: nlist.c,v 1.4 1994/10/02 08:33:29 davidg Exp $
34  */
35 
36 #ifndef lint
37 static char sccsid[] = "@(#)nlist.c	8.4 (Berkeley) 4/2/94";
38 #endif /* not lint */
39 
40 #include <sys/param.h>
41 #include <sys/time.h>
42 #include <sys/proc.h>
43 #include <sys/resource.h>
44 
45 #include <err.h>
46 #include <errno.h>
47 #include <kvm.h>
48 #include <nlist.h>
49 #include <stdio.h>
50 #include <string.h>
51 
52 #include "ps.h"
53 
54 #ifdef P_PPWAIT
55 #define NEWVM
56 #endif
57 
58 struct	nlist psnl[] = {
59 	{"_fscale"},
60 #define	X_FSCALE	0
61 	{"_ccpu"},
62 #define	X_CCPU		1
63 #ifdef NEWVM
64 	{"_avail_start"},
65 #define	X_AVAILSTART	2
66 	{"_avail_end"},
67 #define	X_AVAILEND	3
68 #else
69 	{"_ecmx"},
70 #define	X_ECMX		2
71 #endif
72 	{NULL}
73 };
74 
75 fixpt_t	ccpu;				/* kernel _ccpu variable */
76 int	nlistread;			/* if nlist already read. */
77 int	mempages;			/* number of pages of phys. memory */
78 int	fscale;				/* kernel _fscale variable */
79 
80 extern kvm_t *kd;
81 
82 #define kread(x, v) \
83 	kvm_read(kd, psnl[x].n_value, (char *)&v, sizeof v) != sizeof(v)
84 
85 int
86 donlist()
87 {
88 	int rval;
89 #ifdef NEWVM
90 	int tmp;
91 #endif
92 
93 	rval = 0;
94 	nlistread = 1;
95 	if (kvm_nlist(kd, psnl)) {
96 		nlisterr(psnl);
97 		eval = 1;
98 		return (1);
99 	}
100 	if (kread(X_FSCALE, fscale)) {
101 		warnx("fscale: %s", kvm_geterr(kd));
102 		eval = rval = 1;
103 	}
104 #ifdef NEWVM
105 	if (kread(X_AVAILEND, mempages)) {
106 		warnx("avail_start: %s", kvm_geterr(kd));
107 		eval = rval = 1;
108 	}
109 	if (kread(X_AVAILSTART, tmp)) {
110 		warnx("avail_end: %s", kvm_geterr(kd));
111 		eval = rval = 1;
112 	}
113 	mempages -= tmp;
114 	mempages /= PAGE_SIZE;
115 #else
116 	if (kread(X_ECMX, mempages)) {
117 		warnx("ecmx: %s", kvm_geterr(kd));
118 		eval = rval = 1;
119 	}
120 #endif
121 	if (kread(X_CCPU, ccpu)) {
122 		warnx("ccpu: %s", kvm_geterr(kd));
123 		eval = rval = 1;
124 	}
125 	return (rval);
126 }
127 
128 void
129 nlisterr(nl)
130 	struct nlist nl[];
131 {
132 	int i;
133 
134 	(void)fprintf(stderr, "ps: nlist: can't find following symbols:");
135 	for (i = 0; nl[i].n_name != NULL; i++)
136 		if (nl[i].n_value == 0)
137 			(void)fprintf(stderr, " %s", nl[i].n_name);
138 	(void)fprintf(stderr, "\n");
139 }
140