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