kvm_proc.c revision 1.33 1 1.33 simonb /* $NetBSD: kvm_proc.c,v 1.33 2000/04/15 15:52:52 simonb Exp $ */
2 1.26 mycroft
3 1.26 mycroft /*-
4 1.26 mycroft * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.26 mycroft * All rights reserved.
6 1.26 mycroft *
7 1.26 mycroft * This code is derived from software contributed to The NetBSD Foundation
8 1.26 mycroft * by Charles M. Hannum.
9 1.26 mycroft *
10 1.26 mycroft * Redistribution and use in source and binary forms, with or without
11 1.26 mycroft * modification, are permitted provided that the following conditions
12 1.26 mycroft * are met:
13 1.26 mycroft * 1. Redistributions of source code must retain the above copyright
14 1.26 mycroft * notice, this list of conditions and the following disclaimer.
15 1.26 mycroft * 2. Redistributions in binary form must reproduce the above copyright
16 1.26 mycroft * notice, this list of conditions and the following disclaimer in the
17 1.26 mycroft * documentation and/or other materials provided with the distribution.
18 1.26 mycroft * 3. All advertising materials mentioning features or use of this software
19 1.26 mycroft * must display the following acknowledgement:
20 1.26 mycroft * This product includes software developed by the NetBSD
21 1.26 mycroft * Foundation, Inc. and its contributors.
22 1.26 mycroft * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.26 mycroft * contributors may be used to endorse or promote products derived
24 1.26 mycroft * from this software without specific prior written permission.
25 1.26 mycroft *
26 1.26 mycroft * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.26 mycroft * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.26 mycroft * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.26 mycroft * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.26 mycroft * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.26 mycroft * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.26 mycroft * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.26 mycroft * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.26 mycroft * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.26 mycroft * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.26 mycroft * POSSIBILITY OF SUCH DAMAGE.
37 1.26 mycroft */
38 1.16 thorpej
39 1.1 cgd /*-
40 1.1 cgd * Copyright (c) 1989, 1992, 1993
41 1.1 cgd * The Regents of the University of California. All rights reserved.
42 1.1 cgd *
43 1.1 cgd * This code is derived from software developed by the Computer Systems
44 1.1 cgd * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
45 1.1 cgd * BG 91-66 and contributed to Berkeley.
46 1.1 cgd *
47 1.1 cgd * Redistribution and use in source and binary forms, with or without
48 1.1 cgd * modification, are permitted provided that the following conditions
49 1.1 cgd * are met:
50 1.1 cgd * 1. Redistributions of source code must retain the above copyright
51 1.1 cgd * notice, this list of conditions and the following disclaimer.
52 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
53 1.1 cgd * notice, this list of conditions and the following disclaimer in the
54 1.1 cgd * documentation and/or other materials provided with the distribution.
55 1.1 cgd * 3. All advertising materials mentioning features or use of this software
56 1.1 cgd * must display the following acknowledgement:
57 1.1 cgd * This product includes software developed by the University of
58 1.1 cgd * California, Berkeley and its contributors.
59 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
60 1.1 cgd * may be used to endorse or promote products derived from this software
61 1.1 cgd * without specific prior written permission.
62 1.1 cgd *
63 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 1.1 cgd * SUCH DAMAGE.
74 1.1 cgd */
75 1.1 cgd
76 1.19 mikel #include <sys/cdefs.h>
77 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
78 1.16 thorpej #if 0
79 1.1 cgd static char sccsid[] = "@(#)kvm_proc.c 8.3 (Berkeley) 9/23/93";
80 1.16 thorpej #else
81 1.33 simonb __RCSID("$NetBSD: kvm_proc.c,v 1.33 2000/04/15 15:52:52 simonb Exp $");
82 1.16 thorpej #endif
83 1.1 cgd #endif /* LIBC_SCCS and not lint */
84 1.1 cgd
85 1.1 cgd /*
86 1.1 cgd * Proc traversal interface for kvm. ps and w are (probably) the exclusive
87 1.1 cgd * users of this code, so we've factored it out into a separate module.
88 1.1 cgd * Thus, we keep this grunge out of the other kvm applications (i.e.,
89 1.1 cgd * most other applications are interested only in open/close/read/nlist).
90 1.1 cgd */
91 1.1 cgd
92 1.1 cgd #include <sys/param.h>
93 1.1 cgd #include <sys/user.h>
94 1.1 cgd #include <sys/proc.h>
95 1.1 cgd #include <sys/exec.h>
96 1.1 cgd #include <sys/stat.h>
97 1.1 cgd #include <sys/ioctl.h>
98 1.1 cgd #include <sys/tty.h>
99 1.7 cgd #include <stdlib.h>
100 1.10 mycroft #include <string.h>
101 1.1 cgd #include <unistd.h>
102 1.1 cgd #include <nlist.h>
103 1.1 cgd #include <kvm.h>
104 1.1 cgd
105 1.1 cgd #include <vm/vm.h>
106 1.1 cgd #include <vm/vm_param.h>
107 1.1 cgd
108 1.23 chs #include <uvm/uvm_extern.h>
109 1.29 mrg #include <uvm/uvm_amap.h>
110 1.23 chs
111 1.1 cgd #include <sys/sysctl.h>
112 1.1 cgd
113 1.1 cgd #include <limits.h>
114 1.1 cgd #include <db.h>
115 1.1 cgd #include <paths.h>
116 1.1 cgd
117 1.1 cgd #include "kvm_private.h"
118 1.1 cgd
119 1.2 mycroft #define KREAD(kd, addr, obj) \
120 1.28 christos (kvm_read(kd, addr, (void *)(obj), sizeof(*obj)) != sizeof(*obj))
121 1.2 mycroft
122 1.20 drochner char *_kvm_uread __P((kvm_t *, const struct proc *, u_long, u_long *));
123 1.15 cgd ssize_t kvm_uread __P((kvm_t *, const struct proc *, u_long, char *,
124 1.15 cgd size_t));
125 1.15 cgd
126 1.15 cgd static char **kvm_argv __P((kvm_t *, const struct proc *, u_long, int,
127 1.15 cgd int));
128 1.27 thorpej static int kvm_deadprocs __P((kvm_t *, int, int, u_long, u_long, u_long,
129 1.27 thorpej int));
130 1.15 cgd static char **kvm_doargv __P((kvm_t *, const struct kinfo_proc *, int,
131 1.15 cgd void (*)(struct ps_strings *, u_long *, int *)));
132 1.15 cgd static int kvm_proclist __P((kvm_t *, int, int, struct proc *,
133 1.15 cgd struct kinfo_proc *, int));
134 1.15 cgd static int proc_verify __P((kvm_t *, u_long, const struct proc *));
135 1.15 cgd static void ps_str_a __P((struct ps_strings *, u_long *, int *));
136 1.15 cgd static void ps_str_e __P((struct ps_strings *, u_long *, int *));
137 1.2 mycroft
138 1.8 mycroft char *
139 1.8 mycroft _kvm_uread(kd, p, va, cnt)
140 1.1 cgd kvm_t *kd;
141 1.1 cgd const struct proc *p;
142 1.1 cgd u_long va;
143 1.1 cgd u_long *cnt;
144 1.1 cgd {
145 1.28 christos int true = 1;
146 1.21 perry u_long addr, head;
147 1.21 perry u_long offset;
148 1.1 cgd struct vm_map_entry vme;
149 1.23 chs struct vm_amap amap;
150 1.23 chs struct vm_anon *anonp, anon;
151 1.23 chs struct vm_page pg;
152 1.28 christos u_long slot;
153 1.1 cgd
154 1.6 mycroft if (kd->swapspc == 0) {
155 1.28 christos kd->swapspc = (char *)_kvm_malloc(kd, (size_t)kd->nbpg);
156 1.6 mycroft if (kd->swapspc == 0)
157 1.5 deraadt return (0);
158 1.5 deraadt }
159 1.8 mycroft
160 1.1 cgd /*
161 1.1 cgd * Look through the address map for the memory object
162 1.1 cgd * that corresponds to the given virtual address.
163 1.1 cgd * The header just has the entire valid range.
164 1.1 cgd */
165 1.8 mycroft head = (u_long)&p->p_vmspace->vm_map.header;
166 1.1 cgd addr = head;
167 1.28 christos while (true) {
168 1.2 mycroft if (KREAD(kd, addr, &vme))
169 1.1 cgd return (0);
170 1.1 cgd
171 1.23 chs if (va >= vme.start && va < vme.end &&
172 1.23 chs vme.aref.ar_amap != NULL)
173 1.23 chs break;
174 1.23 chs
175 1.1 cgd addr = (u_long)vme.next;
176 1.2 mycroft if (addr == head)
177 1.1 cgd return (0);
178 1.23 chs
179 1.1 cgd }
180 1.2 mycroft
181 1.1 cgd /*
182 1.23 chs * we found the map entry, now to find the object...
183 1.23 chs */
184 1.23 chs if (vme.aref.ar_amap == NULL)
185 1.23 chs return NULL;
186 1.23 chs
187 1.23 chs addr = (u_long)vme.aref.ar_amap;
188 1.23 chs if (KREAD(kd, addr, &amap))
189 1.23 chs return NULL;
190 1.23 chs
191 1.23 chs offset = va - vme.start;
192 1.29 mrg slot = offset / kd->nbpg + vme.aref.ar_pageoff;
193 1.23 chs /* sanity-check slot number */
194 1.23 chs if (slot > amap.am_nslot)
195 1.23 chs return NULL;
196 1.23 chs
197 1.23 chs addr = (u_long)amap.am_anon + (offset / kd->nbpg) * sizeof(anonp);
198 1.23 chs if (KREAD(kd, addr, &anonp))
199 1.23 chs return NULL;
200 1.23 chs
201 1.23 chs addr = (u_long)anonp;
202 1.23 chs if (KREAD(kd, addr, &anon))
203 1.23 chs return NULL;
204 1.23 chs
205 1.23 chs addr = (u_long)anon.u.an_page;
206 1.23 chs if (addr) {
207 1.23 chs if (KREAD(kd, addr, &pg))
208 1.23 chs return NULL;
209 1.23 chs
210 1.28 christos if (pread(kd->pmfd, (void *)kd->swapspc, (size_t)kd->nbpg,
211 1.24 thorpej (off_t)pg.phys_addr) != kd->nbpg)
212 1.23 chs return NULL;
213 1.23 chs }
214 1.23 chs else {
215 1.28 christos if (pread(kd->swfd, (void *)kd->swapspc, (size_t)kd->nbpg,
216 1.24 thorpej (off_t)(anon.an_swslot * kd->nbpg)) != kd->nbpg)
217 1.23 chs return NULL;
218 1.23 chs }
219 1.8 mycroft
220 1.2 mycroft /* Found the page. */
221 1.6 mycroft offset %= kd->nbpg;
222 1.6 mycroft *cnt = kd->nbpg - offset;
223 1.28 christos return (&kd->swapspc[(size_t)offset]);
224 1.2 mycroft }
225 1.1 cgd
226 1.1 cgd /*
227 1.1 cgd * Read proc's from memory file into buffer bp, which has space to hold
228 1.1 cgd * at most maxcnt procs.
229 1.1 cgd */
230 1.1 cgd static int
231 1.1 cgd kvm_proclist(kd, what, arg, p, bp, maxcnt)
232 1.1 cgd kvm_t *kd;
233 1.1 cgd int what, arg;
234 1.1 cgd struct proc *p;
235 1.1 cgd struct kinfo_proc *bp;
236 1.1 cgd int maxcnt;
237 1.1 cgd {
238 1.21 perry int cnt = 0;
239 1.1 cgd struct eproc eproc;
240 1.1 cgd struct pgrp pgrp;
241 1.1 cgd struct session sess;
242 1.1 cgd struct tty tty;
243 1.1 cgd struct proc proc;
244 1.1 cgd
245 1.4 mycroft for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) {
246 1.1 cgd if (KREAD(kd, (u_long)p, &proc)) {
247 1.1 cgd _kvm_err(kd, kd->program, "can't read proc at %x", p);
248 1.1 cgd return (-1);
249 1.1 cgd }
250 1.1 cgd if (KREAD(kd, (u_long)proc.p_cred, &eproc.e_pcred) == 0)
251 1.28 christos if (KREAD(kd, (u_long)eproc.e_pcred.pc_ucred,
252 1.28 christos &eproc.e_ucred)) {
253 1.28 christos _kvm_err(kd, kd->program,
254 1.28 christos "can't read proc credentials at %x", p);
255 1.28 christos return -1;
256 1.28 christos }
257 1.1 cgd
258 1.1 cgd switch(what) {
259 1.31 simonb
260 1.1 cgd case KERN_PROC_PID:
261 1.1 cgd if (proc.p_pid != (pid_t)arg)
262 1.1 cgd continue;
263 1.1 cgd break;
264 1.1 cgd
265 1.1 cgd case KERN_PROC_UID:
266 1.1 cgd if (eproc.e_ucred.cr_uid != (uid_t)arg)
267 1.1 cgd continue;
268 1.1 cgd break;
269 1.1 cgd
270 1.1 cgd case KERN_PROC_RUID:
271 1.1 cgd if (eproc.e_pcred.p_ruid != (uid_t)arg)
272 1.1 cgd continue;
273 1.1 cgd break;
274 1.1 cgd }
275 1.1 cgd /*
276 1.1 cgd * We're going to add another proc to the set. If this
277 1.1 cgd * will overflow the buffer, assume the reason is because
278 1.1 cgd * nprocs (or the proc list) is corrupt and declare an error.
279 1.1 cgd */
280 1.1 cgd if (cnt >= maxcnt) {
281 1.1 cgd _kvm_err(kd, kd->program, "nprocs corrupt");
282 1.1 cgd return (-1);
283 1.1 cgd }
284 1.1 cgd /*
285 1.1 cgd * gather eproc
286 1.1 cgd */
287 1.1 cgd eproc.e_paddr = p;
288 1.1 cgd if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
289 1.1 cgd _kvm_err(kd, kd->program, "can't read pgrp at %x",
290 1.1 cgd proc.p_pgrp);
291 1.1 cgd return (-1);
292 1.1 cgd }
293 1.1 cgd eproc.e_sess = pgrp.pg_session;
294 1.1 cgd eproc.e_pgid = pgrp.pg_id;
295 1.1 cgd eproc.e_jobc = pgrp.pg_jobc;
296 1.1 cgd if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
297 1.31 simonb _kvm_err(kd, kd->program, "can't read session at %x",
298 1.1 cgd pgrp.pg_session);
299 1.1 cgd return (-1);
300 1.1 cgd }
301 1.1 cgd if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) {
302 1.1 cgd if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
303 1.1 cgd _kvm_err(kd, kd->program,
304 1.1 cgd "can't read tty at %x", sess.s_ttyp);
305 1.1 cgd return (-1);
306 1.1 cgd }
307 1.1 cgd eproc.e_tdev = tty.t_dev;
308 1.1 cgd eproc.e_tsess = tty.t_session;
309 1.1 cgd if (tty.t_pgrp != NULL) {
310 1.1 cgd if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
311 1.1 cgd _kvm_err(kd, kd->program,
312 1.31 simonb "can't read tpgrp at &x",
313 1.1 cgd tty.t_pgrp);
314 1.1 cgd return (-1);
315 1.1 cgd }
316 1.1 cgd eproc.e_tpgid = pgrp.pg_id;
317 1.1 cgd } else
318 1.1 cgd eproc.e_tpgid = -1;
319 1.1 cgd } else
320 1.1 cgd eproc.e_tdev = NODEV;
321 1.1 cgd eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0;
322 1.33 simonb eproc.e_sid = sess.s_sid;
323 1.1 cgd if (sess.s_leader == p)
324 1.1 cgd eproc.e_flag |= EPROC_SLEADER;
325 1.1 cgd if (proc.p_wmesg)
326 1.31 simonb (void)kvm_read(kd, (u_long)proc.p_wmesg,
327 1.1 cgd eproc.e_wmesg, WMESGLEN);
328 1.1 cgd
329 1.1 cgd (void)kvm_read(kd, (u_long)proc.p_vmspace,
330 1.28 christos (void *)&eproc.e_vm, sizeof(eproc.e_vm));
331 1.9 pk
332 1.1 cgd eproc.e_xsize = eproc.e_xrssize = 0;
333 1.1 cgd eproc.e_xccount = eproc.e_xswrss = 0;
334 1.1 cgd
335 1.1 cgd switch (what) {
336 1.1 cgd
337 1.1 cgd case KERN_PROC_PGRP:
338 1.1 cgd if (eproc.e_pgid != (pid_t)arg)
339 1.1 cgd continue;
340 1.1 cgd break;
341 1.1 cgd
342 1.1 cgd case KERN_PROC_TTY:
343 1.31 simonb if ((proc.p_flag & P_CONTROLT) == 0 ||
344 1.1 cgd eproc.e_tdev != (dev_t)arg)
345 1.1 cgd continue;
346 1.1 cgd break;
347 1.1 cgd }
348 1.25 perry memcpy(&bp->kp_proc, &proc, sizeof(proc));
349 1.25 perry memcpy(&bp->kp_eproc, &eproc, sizeof(eproc));
350 1.1 cgd ++bp;
351 1.1 cgd ++cnt;
352 1.1 cgd }
353 1.1 cgd return (cnt);
354 1.1 cgd }
355 1.1 cgd
356 1.1 cgd /*
357 1.1 cgd * Build proc info array by reading in proc list from a crash dump.
358 1.1 cgd * Return number of procs read. maxcnt is the max we will read.
359 1.1 cgd */
360 1.1 cgd static int
361 1.27 thorpej kvm_deadprocs(kd, what, arg, a_allproc, a_deadproc, a_zombproc, maxcnt)
362 1.1 cgd kvm_t *kd;
363 1.1 cgd int what, arg;
364 1.1 cgd u_long a_allproc;
365 1.27 thorpej u_long a_deadproc;
366 1.1 cgd u_long a_zombproc;
367 1.1 cgd int maxcnt;
368 1.1 cgd {
369 1.21 perry struct kinfo_proc *bp = kd->procbase;
370 1.27 thorpej int acnt, dcnt, zcnt;
371 1.1 cgd struct proc *p;
372 1.1 cgd
373 1.1 cgd if (KREAD(kd, a_allproc, &p)) {
374 1.1 cgd _kvm_err(kd, kd->program, "cannot read allproc");
375 1.1 cgd return (-1);
376 1.1 cgd }
377 1.1 cgd acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
378 1.1 cgd if (acnt < 0)
379 1.1 cgd return (acnt);
380 1.1 cgd
381 1.27 thorpej if (KREAD(kd, a_deadproc, &p)) {
382 1.27 thorpej _kvm_err(kd, kd->program, "cannot read deadproc");
383 1.27 thorpej return (-1);
384 1.27 thorpej }
385 1.27 thorpej
386 1.27 thorpej dcnt = kvm_proclist(kd, what, arg, p, bp, maxcnt - acnt);
387 1.27 thorpej if (dcnt < 0)
388 1.27 thorpej dcnt = 0;
389 1.27 thorpej
390 1.1 cgd if (KREAD(kd, a_zombproc, &p)) {
391 1.1 cgd _kvm_err(kd, kd->program, "cannot read zombproc");
392 1.1 cgd return (-1);
393 1.1 cgd }
394 1.27 thorpej zcnt = kvm_proclist(kd, what, arg, p, bp + acnt,
395 1.27 thorpej maxcnt - (acnt + dcnt));
396 1.1 cgd if (zcnt < 0)
397 1.1 cgd zcnt = 0;
398 1.1 cgd
399 1.1 cgd return (acnt + zcnt);
400 1.1 cgd }
401 1.1 cgd
402 1.1 cgd struct kinfo_proc *
403 1.1 cgd kvm_getprocs(kd, op, arg, cnt)
404 1.1 cgd kvm_t *kd;
405 1.1 cgd int op, arg;
406 1.1 cgd int *cnt;
407 1.1 cgd {
408 1.7 cgd size_t size;
409 1.7 cgd int mib[4], st, nprocs;
410 1.1 cgd
411 1.1 cgd if (kd->procbase != 0) {
412 1.1 cgd free((void *)kd->procbase);
413 1.31 simonb /*
414 1.1 cgd * Clear this pointer in case this call fails. Otherwise,
415 1.1 cgd * kvm_close() will free it again.
416 1.1 cgd */
417 1.1 cgd kd->procbase = 0;
418 1.1 cgd }
419 1.1 cgd if (ISALIVE(kd)) {
420 1.1 cgd size = 0;
421 1.1 cgd mib[0] = CTL_KERN;
422 1.1 cgd mib[1] = KERN_PROC;
423 1.1 cgd mib[2] = op;
424 1.1 cgd mib[3] = arg;
425 1.1 cgd st = sysctl(mib, 4, NULL, &size, NULL, 0);
426 1.1 cgd if (st == -1) {
427 1.1 cgd _kvm_syserr(kd, kd->program, "kvm_getprocs");
428 1.1 cgd return (0);
429 1.1 cgd }
430 1.1 cgd kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
431 1.1 cgd if (kd->procbase == 0)
432 1.1 cgd return (0);
433 1.1 cgd st = sysctl(mib, 4, kd->procbase, &size, NULL, 0);
434 1.1 cgd if (st == -1) {
435 1.1 cgd _kvm_syserr(kd, kd->program, "kvm_getprocs");
436 1.1 cgd return (0);
437 1.1 cgd }
438 1.1 cgd if (size % sizeof(struct kinfo_proc) != 0) {
439 1.1 cgd _kvm_err(kd, kd->program,
440 1.1 cgd "proc size mismatch (%d total, %d chunks)",
441 1.1 cgd size, sizeof(struct kinfo_proc));
442 1.1 cgd return (0);
443 1.1 cgd }
444 1.1 cgd nprocs = size / sizeof(struct kinfo_proc);
445 1.1 cgd } else {
446 1.27 thorpej struct nlist nl[5], *p;
447 1.1 cgd
448 1.1 cgd nl[0].n_name = "_nprocs";
449 1.1 cgd nl[1].n_name = "_allproc";
450 1.27 thorpej nl[2].n_name = "_deadproc";
451 1.27 thorpej nl[3].n_name = "_zombproc";
452 1.27 thorpej nl[4].n_name = 0;
453 1.1 cgd
454 1.1 cgd if (kvm_nlist(kd, nl) != 0) {
455 1.1 cgd for (p = nl; p->n_type != 0; ++p)
456 1.1 cgd ;
457 1.1 cgd _kvm_err(kd, kd->program,
458 1.1 cgd "%s: no such symbol", p->n_name);
459 1.1 cgd return (0);
460 1.1 cgd }
461 1.1 cgd if (KREAD(kd, nl[0].n_value, &nprocs)) {
462 1.1 cgd _kvm_err(kd, kd->program, "can't read nprocs");
463 1.1 cgd return (0);
464 1.1 cgd }
465 1.1 cgd size = nprocs * sizeof(struct kinfo_proc);
466 1.1 cgd kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
467 1.1 cgd if (kd->procbase == 0)
468 1.1 cgd return (0);
469 1.1 cgd
470 1.1 cgd nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
471 1.27 thorpej nl[2].n_value, nl[3].n_value, nprocs);
472 1.32 chs if (nprocs < 0)
473 1.32 chs return (0);
474 1.1 cgd #ifdef notdef
475 1.1 cgd size = nprocs * sizeof(struct kinfo_proc);
476 1.1 cgd (void)realloc(kd->procbase, size);
477 1.1 cgd #endif
478 1.1 cgd }
479 1.1 cgd *cnt = nprocs;
480 1.1 cgd return (kd->procbase);
481 1.1 cgd }
482 1.1 cgd
483 1.1 cgd void
484 1.1 cgd _kvm_freeprocs(kd)
485 1.1 cgd kvm_t *kd;
486 1.1 cgd {
487 1.1 cgd if (kd->procbase) {
488 1.1 cgd free(kd->procbase);
489 1.1 cgd kd->procbase = 0;
490 1.1 cgd }
491 1.1 cgd }
492 1.1 cgd
493 1.1 cgd void *
494 1.1 cgd _kvm_realloc(kd, p, n)
495 1.1 cgd kvm_t *kd;
496 1.1 cgd void *p;
497 1.1 cgd size_t n;
498 1.1 cgd {
499 1.1 cgd void *np = (void *)realloc(p, n);
500 1.1 cgd
501 1.1 cgd if (np == 0)
502 1.1 cgd _kvm_err(kd, kd->program, "out of memory");
503 1.1 cgd return (np);
504 1.1 cgd }
505 1.1 cgd
506 1.1 cgd #ifndef MAX
507 1.1 cgd #define MAX(a, b) ((a) > (b) ? (a) : (b))
508 1.1 cgd #endif
509 1.1 cgd
510 1.1 cgd /*
511 1.1 cgd * Read in an argument vector from the user address space of process p.
512 1.31 simonb * addr if the user-space base address of narg null-terminated contiguous
513 1.1 cgd * strings. This is used to read in both the command arguments and
514 1.1 cgd * environment strings. Read at most maxcnt characters of strings.
515 1.1 cgd */
516 1.1 cgd static char **
517 1.1 cgd kvm_argv(kd, p, addr, narg, maxcnt)
518 1.1 cgd kvm_t *kd;
519 1.15 cgd const struct proc *p;
520 1.21 perry u_long addr;
521 1.21 perry int narg;
522 1.21 perry int maxcnt;
523 1.21 perry {
524 1.21 perry char *np, *cp, *ep, *ap;
525 1.28 christos u_long oaddr = (u_long)~0L;
526 1.28 christos u_long len;
527 1.28 christos size_t cc;
528 1.21 perry char **argv;
529 1.1 cgd
530 1.1 cgd /*
531 1.1 cgd * Check that there aren't an unreasonable number of agruments,
532 1.1 cgd * and that the address is in user space.
533 1.1 cgd */
534 1.18 gwr if (narg > ARG_MAX || addr < kd->min_uva || addr >= kd->max_uva)
535 1.1 cgd return (0);
536 1.1 cgd
537 1.1 cgd if (kd->argv == 0) {
538 1.1 cgd /*
539 1.1 cgd * Try to avoid reallocs.
540 1.1 cgd */
541 1.1 cgd kd->argc = MAX(narg + 1, 32);
542 1.31 simonb kd->argv = (char **)_kvm_malloc(kd, kd->argc *
543 1.1 cgd sizeof(*kd->argv));
544 1.1 cgd if (kd->argv == 0)
545 1.1 cgd return (0);
546 1.1 cgd } else if (narg + 1 > kd->argc) {
547 1.1 cgd kd->argc = MAX(2 * kd->argc, narg + 1);
548 1.31 simonb kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc *
549 1.1 cgd sizeof(*kd->argv));
550 1.1 cgd if (kd->argv == 0)
551 1.1 cgd return (0);
552 1.1 cgd }
553 1.1 cgd if (kd->argspc == 0) {
554 1.28 christos kd->argspc = (char *)_kvm_malloc(kd, (size_t)kd->nbpg);
555 1.1 cgd if (kd->argspc == 0)
556 1.1 cgd return (0);
557 1.6 mycroft kd->arglen = kd->nbpg;
558 1.1 cgd }
559 1.10 mycroft if (kd->argbuf == 0) {
560 1.28 christos kd->argbuf = (char *)_kvm_malloc(kd, (size_t)kd->nbpg);
561 1.10 mycroft if (kd->argbuf == 0)
562 1.10 mycroft return (0);
563 1.10 mycroft }
564 1.10 mycroft cc = sizeof(char *) * narg;
565 1.28 christos if (kvm_uread(kd, p, addr, (void *)kd->argv, cc) != cc)
566 1.10 mycroft return (0);
567 1.10 mycroft ap = np = kd->argspc;
568 1.1 cgd argv = kd->argv;
569 1.1 cgd len = 0;
570 1.1 cgd /*
571 1.1 cgd * Loop over pages, filling in the argument vector.
572 1.1 cgd */
573 1.10 mycroft while (argv < kd->argv + narg && *argv != 0) {
574 1.10 mycroft addr = (u_long)*argv & ~(kd->nbpg - 1);
575 1.10 mycroft if (addr != oaddr) {
576 1.28 christos if (kvm_uread(kd, p, addr, kd->argbuf,
577 1.28 christos (size_t)kd->nbpg) != kd->nbpg)
578 1.10 mycroft return (0);
579 1.10 mycroft oaddr = addr;
580 1.10 mycroft }
581 1.10 mycroft addr = (u_long)*argv & (kd->nbpg - 1);
582 1.28 christos cp = kd->argbuf + (size_t)addr;
583 1.28 christos cc = kd->nbpg - (size_t)addr;
584 1.28 christos if (maxcnt > 0 && cc > (size_t)(maxcnt - len))
585 1.28 christos cc = (size_t)(maxcnt - len);
586 1.10 mycroft ep = memchr(cp, '\0', cc);
587 1.10 mycroft if (ep != 0)
588 1.10 mycroft cc = ep - cp + 1;
589 1.1 cgd if (len + cc > kd->arglen) {
590 1.21 perry int off;
591 1.21 perry char **pp;
592 1.21 perry char *op = kd->argspc;
593 1.1 cgd
594 1.1 cgd kd->arglen *= 2;
595 1.1 cgd kd->argspc = (char *)_kvm_realloc(kd, kd->argspc,
596 1.28 christos (size_t)kd->arglen);
597 1.1 cgd if (kd->argspc == 0)
598 1.1 cgd return (0);
599 1.1 cgd /*
600 1.1 cgd * Adjust argv pointers in case realloc moved
601 1.1 cgd * the string space.
602 1.1 cgd */
603 1.1 cgd off = kd->argspc - op;
604 1.13 mycroft for (pp = kd->argv; pp < argv; pp++)
605 1.1 cgd *pp += off;
606 1.12 mycroft ap += off;
607 1.12 mycroft np += off;
608 1.1 cgd }
609 1.10 mycroft memcpy(np, cp, cc);
610 1.10 mycroft np += cc;
611 1.1 cgd len += cc;
612 1.10 mycroft if (ep != 0) {
613 1.10 mycroft *argv++ = ap;
614 1.10 mycroft ap = np;
615 1.10 mycroft } else
616 1.10 mycroft *argv += cc;
617 1.1 cgd if (maxcnt > 0 && len >= maxcnt) {
618 1.1 cgd /*
619 1.1 cgd * We're stopping prematurely. Terminate the
620 1.10 mycroft * current string.
621 1.1 cgd */
622 1.10 mycroft if (ep == 0) {
623 1.10 mycroft *np = '\0';
624 1.14 mycroft *argv++ = ap;
625 1.10 mycroft }
626 1.10 mycroft break;
627 1.1 cgd }
628 1.1 cgd }
629 1.10 mycroft /* Make sure argv is terminated. */
630 1.10 mycroft *argv = 0;
631 1.10 mycroft return (kd->argv);
632 1.1 cgd }
633 1.1 cgd
634 1.1 cgd static void
635 1.1 cgd ps_str_a(p, addr, n)
636 1.1 cgd struct ps_strings *p;
637 1.1 cgd u_long *addr;
638 1.1 cgd int *n;
639 1.1 cgd {
640 1.1 cgd *addr = (u_long)p->ps_argvstr;
641 1.1 cgd *n = p->ps_nargvstr;
642 1.1 cgd }
643 1.1 cgd
644 1.1 cgd static void
645 1.1 cgd ps_str_e(p, addr, n)
646 1.1 cgd struct ps_strings *p;
647 1.1 cgd u_long *addr;
648 1.1 cgd int *n;
649 1.1 cgd {
650 1.1 cgd *addr = (u_long)p->ps_envstr;
651 1.1 cgd *n = p->ps_nenvstr;
652 1.1 cgd }
653 1.1 cgd
654 1.1 cgd /*
655 1.1 cgd * Determine if the proc indicated by p is still active.
656 1.1 cgd * This test is not 100% foolproof in theory, but chances of
657 1.1 cgd * being wrong are very low.
658 1.1 cgd */
659 1.1 cgd static int
660 1.1 cgd proc_verify(kd, kernp, p)
661 1.1 cgd kvm_t *kd;
662 1.1 cgd u_long kernp;
663 1.1 cgd const struct proc *p;
664 1.1 cgd {
665 1.1 cgd struct proc kernproc;
666 1.1 cgd
667 1.1 cgd /*
668 1.1 cgd * Just read in the whole proc. It's not that big relative
669 1.1 cgd * to the cost of the read system call.
670 1.1 cgd */
671 1.31 simonb if (kvm_read(kd, kernp, (void *)&kernproc, sizeof(kernproc)) !=
672 1.1 cgd sizeof(kernproc))
673 1.1 cgd return (0);
674 1.1 cgd return (p->p_pid == kernproc.p_pid &&
675 1.1 cgd (kernproc.p_stat != SZOMB || p->p_stat == SZOMB));
676 1.1 cgd }
677 1.1 cgd
678 1.1 cgd static char **
679 1.1 cgd kvm_doargv(kd, kp, nchr, info)
680 1.1 cgd kvm_t *kd;
681 1.1 cgd const struct kinfo_proc *kp;
682 1.1 cgd int nchr;
683 1.10 mycroft void (*info)(struct ps_strings *, u_long *, int *);
684 1.1 cgd {
685 1.21 perry const struct proc *p = &kp->kp_proc;
686 1.21 perry char **ap;
687 1.1 cgd u_long addr;
688 1.1 cgd int cnt;
689 1.1 cgd struct ps_strings arginfo;
690 1.1 cgd
691 1.1 cgd /*
692 1.1 cgd * Pointers are stored at the top of the user stack.
693 1.1 cgd */
694 1.18 gwr if (p->p_stat == SZOMB)
695 1.18 gwr return (0);
696 1.18 gwr cnt = kvm_uread(kd, p, kd->usrstack - sizeof(arginfo),
697 1.28 christos (void *)&arginfo, sizeof(arginfo));
698 1.18 gwr if (cnt != sizeof(arginfo))
699 1.1 cgd return (0);
700 1.1 cgd
701 1.1 cgd (*info)(&arginfo, &addr, &cnt);
702 1.3 mycroft if (cnt == 0)
703 1.3 mycroft return (0);
704 1.1 cgd ap = kvm_argv(kd, p, addr, cnt, nchr);
705 1.1 cgd /*
706 1.1 cgd * For live kernels, make sure this process didn't go away.
707 1.1 cgd */
708 1.1 cgd if (ap != 0 && ISALIVE(kd) &&
709 1.1 cgd !proc_verify(kd, (u_long)kp->kp_eproc.e_paddr, p))
710 1.1 cgd ap = 0;
711 1.1 cgd return (ap);
712 1.1 cgd }
713 1.1 cgd
714 1.1 cgd /*
715 1.1 cgd * Get the command args. This code is now machine independent.
716 1.1 cgd */
717 1.1 cgd char **
718 1.1 cgd kvm_getargv(kd, kp, nchr)
719 1.1 cgd kvm_t *kd;
720 1.1 cgd const struct kinfo_proc *kp;
721 1.1 cgd int nchr;
722 1.1 cgd {
723 1.1 cgd return (kvm_doargv(kd, kp, nchr, ps_str_a));
724 1.1 cgd }
725 1.1 cgd
726 1.1 cgd char **
727 1.1 cgd kvm_getenvv(kd, kp, nchr)
728 1.1 cgd kvm_t *kd;
729 1.1 cgd const struct kinfo_proc *kp;
730 1.1 cgd int nchr;
731 1.1 cgd {
732 1.1 cgd return (kvm_doargv(kd, kp, nchr, ps_str_e));
733 1.1 cgd }
734 1.1 cgd
735 1.1 cgd /*
736 1.1 cgd * Read from user space. The user context is given by p.
737 1.1 cgd */
738 1.1 cgd ssize_t
739 1.1 cgd kvm_uread(kd, p, uva, buf, len)
740 1.1 cgd kvm_t *kd;
741 1.21 perry const struct proc *p;
742 1.21 perry u_long uva;
743 1.21 perry char *buf;
744 1.21 perry size_t len;
745 1.1 cgd {
746 1.21 perry char *cp;
747 1.1 cgd
748 1.1 cgd cp = buf;
749 1.1 cgd while (len > 0) {
750 1.28 christos size_t cc;
751 1.21 perry char *dp;
752 1.15 cgd u_long cnt;
753 1.8 mycroft
754 1.8 mycroft dp = _kvm_uread(kd, p, uva, &cnt);
755 1.8 mycroft if (dp == 0) {
756 1.8 mycroft _kvm_err(kd, 0, "invalid address (%x)", uva);
757 1.8 mycroft return (0);
758 1.8 mycroft }
759 1.28 christos cc = (size_t)MIN(cnt, len);
760 1.25 perry memcpy(cp, dp, cc);
761 1.1 cgd cp += cc;
762 1.1 cgd uva += cc;
763 1.1 cgd len -= cc;
764 1.1 cgd }
765 1.1 cgd return (ssize_t)(cp - buf);
766 1.1 cgd }
767