kvm_proc.c revision 1.28 1 1.28 christos /* $NetBSD: kvm_proc.c,v 1.28 1998/09/27 18:16:00 christos 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.28 christos __RCSID("$NetBSD: kvm_proc.c,v 1.28 1998/09/27 18:16:00 christos 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 #include <vm/swap_pager.h>
108 1.1 cgd
109 1.23 chs #if defined(UVM)
110 1.23 chs #include <uvm/uvm_extern.h>
111 1.23 chs #endif
112 1.23 chs
113 1.1 cgd #include <sys/sysctl.h>
114 1.1 cgd
115 1.1 cgd #include <limits.h>
116 1.1 cgd #include <db.h>
117 1.1 cgd #include <paths.h>
118 1.1 cgd
119 1.1 cgd #include "kvm_private.h"
120 1.1 cgd
121 1.2 mycroft #define KREAD(kd, addr, obj) \
122 1.28 christos (kvm_read(kd, addr, (void *)(obj), sizeof(*obj)) != sizeof(*obj))
123 1.2 mycroft
124 1.20 drochner char *_kvm_uread __P((kvm_t *, const struct proc *, u_long, u_long *));
125 1.23 chs #if !defined(UVM)
126 1.20 drochner int _kvm_coreinit __P((kvm_t *));
127 1.15 cgd int _kvm_readfromcore __P((kvm_t *, u_long, u_long));
128 1.15 cgd int _kvm_readfrompager __P((kvm_t *, struct vm_object *, u_long));
129 1.23 chs #endif
130 1.15 cgd ssize_t kvm_uread __P((kvm_t *, const struct proc *, u_long, char *,
131 1.15 cgd size_t));
132 1.15 cgd
133 1.15 cgd static char **kvm_argv __P((kvm_t *, const struct proc *, u_long, int,
134 1.15 cgd int));
135 1.27 thorpej static int kvm_deadprocs __P((kvm_t *, int, int, u_long, u_long, u_long,
136 1.27 thorpej int));
137 1.15 cgd static char **kvm_doargv __P((kvm_t *, const struct kinfo_proc *, int,
138 1.15 cgd void (*)(struct ps_strings *, u_long *, int *)));
139 1.15 cgd static int kvm_proclist __P((kvm_t *, int, int, struct proc *,
140 1.15 cgd struct kinfo_proc *, int));
141 1.15 cgd static int proc_verify __P((kvm_t *, u_long, const struct proc *));
142 1.15 cgd static void ps_str_a __P((struct ps_strings *, u_long *, int *));
143 1.15 cgd static void ps_str_e __P((struct ps_strings *, u_long *, int *));
144 1.2 mycroft
145 1.8 mycroft char *
146 1.8 mycroft _kvm_uread(kd, p, va, cnt)
147 1.1 cgd kvm_t *kd;
148 1.1 cgd const struct proc *p;
149 1.1 cgd u_long va;
150 1.1 cgd u_long *cnt;
151 1.1 cgd {
152 1.28 christos int true = 1;
153 1.21 perry u_long addr, head;
154 1.21 perry u_long offset;
155 1.1 cgd struct vm_map_entry vme;
156 1.23 chs #if defined(UVM)
157 1.23 chs struct vm_amap amap;
158 1.23 chs struct vm_anon *anonp, anon;
159 1.23 chs struct vm_page pg;
160 1.28 christos u_long slot;
161 1.23 chs #else
162 1.1 cgd struct vm_object vmo;
163 1.8 mycroft int rv;
164 1.23 chs #endif
165 1.1 cgd
166 1.6 mycroft if (kd->swapspc == 0) {
167 1.28 christos kd->swapspc = (char *)_kvm_malloc(kd, (size_t)kd->nbpg);
168 1.6 mycroft if (kd->swapspc == 0)
169 1.5 deraadt return (0);
170 1.5 deraadt }
171 1.8 mycroft
172 1.1 cgd /*
173 1.1 cgd * Look through the address map for the memory object
174 1.1 cgd * that corresponds to the given virtual address.
175 1.1 cgd * The header just has the entire valid range.
176 1.1 cgd */
177 1.8 mycroft head = (u_long)&p->p_vmspace->vm_map.header;
178 1.1 cgd addr = head;
179 1.28 christos while (true) {
180 1.2 mycroft if (KREAD(kd, addr, &vme))
181 1.1 cgd return (0);
182 1.1 cgd
183 1.23 chs #if defined(UVM)
184 1.23 chs if (va >= vme.start && va < vme.end &&
185 1.23 chs vme.aref.ar_amap != NULL)
186 1.23 chs break;
187 1.23 chs
188 1.23 chs #else
189 1.2 mycroft if (va >= vme.start && va < vme.end &&
190 1.1 cgd vme.object.vm_object != 0)
191 1.1 cgd break;
192 1.23 chs #endif
193 1.1 cgd
194 1.1 cgd addr = (u_long)vme.next;
195 1.2 mycroft if (addr == head)
196 1.1 cgd return (0);
197 1.23 chs
198 1.1 cgd }
199 1.23 chs #if defined(UVM)
200 1.2 mycroft
201 1.1 cgd /*
202 1.23 chs * we found the map entry, now to find the object...
203 1.23 chs */
204 1.23 chs if (vme.aref.ar_amap == NULL)
205 1.23 chs return NULL;
206 1.23 chs
207 1.23 chs addr = (u_long)vme.aref.ar_amap;
208 1.23 chs if (KREAD(kd, addr, &amap))
209 1.23 chs return NULL;
210 1.23 chs
211 1.23 chs offset = va - vme.start;
212 1.23 chs slot = offset / kd->nbpg + vme.aref.ar_slotoff;
213 1.23 chs /* sanity-check slot number */
214 1.23 chs if (slot > amap.am_nslot)
215 1.23 chs return NULL;
216 1.23 chs
217 1.23 chs addr = (u_long)amap.am_anon + (offset / kd->nbpg) * sizeof(anonp);
218 1.23 chs if (KREAD(kd, addr, &anonp))
219 1.23 chs return NULL;
220 1.23 chs
221 1.23 chs addr = (u_long)anonp;
222 1.23 chs if (KREAD(kd, addr, &anon))
223 1.23 chs return NULL;
224 1.23 chs
225 1.23 chs addr = (u_long)anon.u.an_page;
226 1.23 chs if (addr) {
227 1.23 chs if (KREAD(kd, addr, &pg))
228 1.23 chs return NULL;
229 1.23 chs
230 1.28 christos if (pread(kd->pmfd, (void *)kd->swapspc, (size_t)kd->nbpg,
231 1.24 thorpej (off_t)pg.phys_addr) != kd->nbpg)
232 1.23 chs return NULL;
233 1.23 chs }
234 1.23 chs else {
235 1.28 christos if (pread(kd->swfd, (void *)kd->swapspc, (size_t)kd->nbpg,
236 1.24 thorpej (off_t)(anon.an_swslot * kd->nbpg)) != kd->nbpg)
237 1.23 chs return NULL;
238 1.23 chs }
239 1.23 chs #else
240 1.23 chs /*
241 1.1 cgd * We found the right object -- follow shadow links.
242 1.1 cgd */
243 1.1 cgd offset = va - vme.start + vme.offset;
244 1.1 cgd addr = (u_long)vme.object.vm_object;
245 1.8 mycroft
246 1.1 cgd while (1) {
247 1.8 mycroft /* Try reading the page from core first. */
248 1.8 mycroft if ((rv = _kvm_readfromcore(kd, addr, offset)))
249 1.8 mycroft break;
250 1.8 mycroft
251 1.2 mycroft if (KREAD(kd, addr, &vmo))
252 1.1 cgd return (0);
253 1.2 mycroft
254 1.2 mycroft /* If there is a pager here, see if it has the page. */
255 1.2 mycroft if (vmo.pager != 0 &&
256 1.8 mycroft (rv = _kvm_readfrompager(kd, &vmo, offset)))
257 1.2 mycroft break;
258 1.2 mycroft
259 1.2 mycroft /* Move down the shadow chain. */
260 1.1 cgd addr = (u_long)vmo.shadow;
261 1.1 cgd if (addr == 0)
262 1.2 mycroft return (0);
263 1.1 cgd offset += vmo.shadow_offset;
264 1.1 cgd }
265 1.2 mycroft
266 1.8 mycroft if (rv == -1)
267 1.8 mycroft return (0);
268 1.23 chs #endif
269 1.8 mycroft
270 1.2 mycroft /* Found the page. */
271 1.6 mycroft offset %= kd->nbpg;
272 1.6 mycroft *cnt = kd->nbpg - offset;
273 1.28 christos return (&kd->swapspc[(size_t)offset]);
274 1.2 mycroft }
275 1.2 mycroft
276 1.23 chs #if !defined(UVM)
277 1.23 chs
278 1.8 mycroft #define vm_page_hash(kd, object, offset) \
279 1.8 mycroft (((u_long)object + (u_long)(offset / kd->nbpg)) & kd->vm_page_hash_mask)
280 1.8 mycroft
281 1.8 mycroft int
282 1.8 mycroft _kvm_coreinit(kd)
283 1.8 mycroft kvm_t *kd;
284 1.8 mycroft {
285 1.8 mycroft struct nlist nlist[3];
286 1.8 mycroft
287 1.8 mycroft nlist[0].n_name = "_vm_page_buckets";
288 1.8 mycroft nlist[1].n_name = "_vm_page_hash_mask";
289 1.8 mycroft nlist[2].n_name = 0;
290 1.8 mycroft if (kvm_nlist(kd, nlist) != 0)
291 1.8 mycroft return (-1);
292 1.8 mycroft
293 1.8 mycroft if (KREAD(kd, nlist[0].n_value, &kd->vm_page_buckets) ||
294 1.8 mycroft KREAD(kd, nlist[1].n_value, &kd->vm_page_hash_mask))
295 1.8 mycroft return (-1);
296 1.8 mycroft
297 1.8 mycroft return (0);
298 1.8 mycroft }
299 1.8 mycroft
300 1.8 mycroft int
301 1.8 mycroft _kvm_readfromcore(kd, object, offset)
302 1.8 mycroft kvm_t *kd;
303 1.8 mycroft u_long object, offset;
304 1.8 mycroft {
305 1.8 mycroft u_long addr;
306 1.8 mycroft struct pglist bucket;
307 1.8 mycroft struct vm_page mem;
308 1.8 mycroft off_t seekpoint;
309 1.8 mycroft
310 1.8 mycroft if (kd->vm_page_buckets == 0 &&
311 1.8 mycroft _kvm_coreinit(kd))
312 1.8 mycroft return (-1);
313 1.8 mycroft
314 1.8 mycroft addr = (u_long)&kd->vm_page_buckets[vm_page_hash(kd, object, offset)];
315 1.8 mycroft if (KREAD(kd, addr, &bucket))
316 1.8 mycroft return (-1);
317 1.8 mycroft
318 1.8 mycroft addr = (u_long)bucket.tqh_first;
319 1.8 mycroft offset &= ~(kd->nbpg -1);
320 1.8 mycroft while (1) {
321 1.8 mycroft if (addr == 0)
322 1.8 mycroft return (0);
323 1.8 mycroft
324 1.8 mycroft if (KREAD(kd, addr, &mem))
325 1.8 mycroft return (-1);
326 1.8 mycroft
327 1.8 mycroft if ((u_long)mem.object == object &&
328 1.8 mycroft (u_long)mem.offset == offset)
329 1.8 mycroft break;
330 1.8 mycroft
331 1.8 mycroft addr = (u_long)mem.hashq.tqe_next;
332 1.8 mycroft }
333 1.8 mycroft
334 1.8 mycroft seekpoint = mem.phys_addr;
335 1.8 mycroft
336 1.24 thorpej if (pread(kd->pmfd, kd->swapspc, kd->nbpg, seekpoint) != kd->nbpg)
337 1.8 mycroft return (-1);
338 1.8 mycroft
339 1.8 mycroft return (1);
340 1.8 mycroft }
341 1.8 mycroft
342 1.2 mycroft int
343 1.6 mycroft _kvm_readfrompager(kd, vmop, offset)
344 1.2 mycroft kvm_t *kd;
345 1.2 mycroft struct vm_object *vmop;
346 1.2 mycroft u_long offset;
347 1.8 mycroft {
348 1.2 mycroft u_long addr;
349 1.2 mycroft struct pager_struct pager;
350 1.2 mycroft struct swpager swap;
351 1.2 mycroft int ix;
352 1.2 mycroft struct swblock swb;
353 1.8 mycroft off_t seekpoint;
354 1.2 mycroft
355 1.2 mycroft /* Read in the pager info and make sure it's a swap device. */
356 1.2 mycroft addr = (u_long)vmop->pager;
357 1.2 mycroft if (KREAD(kd, addr, &pager) || pager.pg_type != PG_SWAP)
358 1.8 mycroft return (-1);
359 1.1 cgd
360 1.2 mycroft /* Read in the swap_pager private data. */
361 1.2 mycroft addr = (u_long)pager.pg_data;
362 1.2 mycroft if (KREAD(kd, addr, &swap))
363 1.8 mycroft return (-1);
364 1.1 cgd
365 1.1 cgd /*
366 1.2 mycroft * Calculate the paging offset, and make sure it's within the
367 1.2 mycroft * bounds of the pager.
368 1.1 cgd */
369 1.2 mycroft offset += vmop->paging_offset;
370 1.1 cgd ix = offset / dbtob(swap.sw_bsize);
371 1.2 mycroft #if 0
372 1.1 cgd if (swap.sw_blocks == 0 || ix >= swap.sw_nblocks)
373 1.8 mycroft return (-1);
374 1.2 mycroft #else
375 1.2 mycroft if (swap.sw_blocks == 0 || ix >= swap.sw_nblocks) {
376 1.2 mycroft int i;
377 1.2 mycroft printf("BUG BUG BUG BUG:\n");
378 1.17 mikel printf("object %p offset %lx pgoffset %lx ",
379 1.17 mikel vmop, offset - vmop->paging_offset,
380 1.17 mikel (u_long)vmop->paging_offset);
381 1.17 mikel printf("pager %p swpager %p\n",
382 1.2 mycroft vmop->pager, pager.pg_data);
383 1.17 mikel printf("osize %lx bsize %x blocks %p nblocks %x\n",
384 1.17 mikel (u_long)swap.sw_osize, swap.sw_bsize, swap.sw_blocks,
385 1.2 mycroft swap.sw_nblocks);
386 1.20 drochner for (i = 0; i < swap.sw_nblocks; i++) {
387 1.20 drochner addr = (u_long)&swap.sw_blocks[i];
388 1.2 mycroft if (KREAD(kd, addr, &swb))
389 1.2 mycroft return (0);
390 1.20 drochner printf("sw_blocks[%d]: block %x mask %x\n", i,
391 1.2 mycroft swb.swb_block, swb.swb_mask);
392 1.2 mycroft }
393 1.8 mycroft return (-1);
394 1.2 mycroft }
395 1.2 mycroft #endif
396 1.1 cgd
397 1.2 mycroft /* Read in the swap records. */
398 1.1 cgd addr = (u_long)&swap.sw_blocks[ix];
399 1.2 mycroft if (KREAD(kd, addr, &swb))
400 1.8 mycroft return (-1);
401 1.1 cgd
402 1.2 mycroft /* Calculate offset within pager. */
403 1.2 mycroft offset %= dbtob(swap.sw_bsize);
404 1.1 cgd
405 1.2 mycroft /* Check that the page is actually present. */
406 1.6 mycroft if ((swb.swb_mask & (1 << (offset / kd->nbpg))) == 0)
407 1.1 cgd return (0);
408 1.1 cgd
409 1.8 mycroft if (!ISALIVE(kd))
410 1.8 mycroft return (-1);
411 1.8 mycroft
412 1.2 mycroft /* Calculate the physical address and read the page. */
413 1.6 mycroft seekpoint = dbtob(swb.swb_block) + (offset & ~(kd->nbpg -1));
414 1.8 mycroft
415 1.24 thorpej if (pread(kd->swfd, kd->swapspc, kd->nbpg, seekpoint) != kd->nbpg)
416 1.8 mycroft return (-1);
417 1.1 cgd
418 1.2 mycroft return (1);
419 1.1 cgd }
420 1.23 chs #endif /* !defined(UVM) */
421 1.1 cgd
422 1.1 cgd /*
423 1.1 cgd * Read proc's from memory file into buffer bp, which has space to hold
424 1.1 cgd * at most maxcnt procs.
425 1.1 cgd */
426 1.1 cgd static int
427 1.1 cgd kvm_proclist(kd, what, arg, p, bp, maxcnt)
428 1.1 cgd kvm_t *kd;
429 1.1 cgd int what, arg;
430 1.1 cgd struct proc *p;
431 1.1 cgd struct kinfo_proc *bp;
432 1.1 cgd int maxcnt;
433 1.1 cgd {
434 1.21 perry int cnt = 0;
435 1.1 cgd struct eproc eproc;
436 1.1 cgd struct pgrp pgrp;
437 1.1 cgd struct session sess;
438 1.1 cgd struct tty tty;
439 1.1 cgd struct proc proc;
440 1.1 cgd
441 1.4 mycroft for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) {
442 1.1 cgd if (KREAD(kd, (u_long)p, &proc)) {
443 1.1 cgd _kvm_err(kd, kd->program, "can't read proc at %x", p);
444 1.1 cgd return (-1);
445 1.1 cgd }
446 1.1 cgd if (KREAD(kd, (u_long)proc.p_cred, &eproc.e_pcred) == 0)
447 1.28 christos if (KREAD(kd, (u_long)eproc.e_pcred.pc_ucred,
448 1.28 christos &eproc.e_ucred)) {
449 1.28 christos _kvm_err(kd, kd->program,
450 1.28 christos "can't read proc credentials at %x", p);
451 1.28 christos return -1;
452 1.28 christos }
453 1.1 cgd
454 1.1 cgd switch(what) {
455 1.1 cgd
456 1.1 cgd case KERN_PROC_PID:
457 1.1 cgd if (proc.p_pid != (pid_t)arg)
458 1.1 cgd continue;
459 1.1 cgd break;
460 1.1 cgd
461 1.1 cgd case KERN_PROC_UID:
462 1.1 cgd if (eproc.e_ucred.cr_uid != (uid_t)arg)
463 1.1 cgd continue;
464 1.1 cgd break;
465 1.1 cgd
466 1.1 cgd case KERN_PROC_RUID:
467 1.1 cgd if (eproc.e_pcred.p_ruid != (uid_t)arg)
468 1.1 cgd continue;
469 1.1 cgd break;
470 1.1 cgd }
471 1.1 cgd /*
472 1.1 cgd * We're going to add another proc to the set. If this
473 1.1 cgd * will overflow the buffer, assume the reason is because
474 1.1 cgd * nprocs (or the proc list) is corrupt and declare an error.
475 1.1 cgd */
476 1.1 cgd if (cnt >= maxcnt) {
477 1.1 cgd _kvm_err(kd, kd->program, "nprocs corrupt");
478 1.1 cgd return (-1);
479 1.1 cgd }
480 1.1 cgd /*
481 1.1 cgd * gather eproc
482 1.1 cgd */
483 1.1 cgd eproc.e_paddr = p;
484 1.1 cgd if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
485 1.1 cgd _kvm_err(kd, kd->program, "can't read pgrp at %x",
486 1.1 cgd proc.p_pgrp);
487 1.1 cgd return (-1);
488 1.1 cgd }
489 1.1 cgd eproc.e_sess = pgrp.pg_session;
490 1.1 cgd eproc.e_pgid = pgrp.pg_id;
491 1.1 cgd eproc.e_jobc = pgrp.pg_jobc;
492 1.1 cgd if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
493 1.1 cgd _kvm_err(kd, kd->program, "can't read session at %x",
494 1.1 cgd pgrp.pg_session);
495 1.1 cgd return (-1);
496 1.1 cgd }
497 1.1 cgd if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) {
498 1.1 cgd if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
499 1.1 cgd _kvm_err(kd, kd->program,
500 1.1 cgd "can't read tty at %x", sess.s_ttyp);
501 1.1 cgd return (-1);
502 1.1 cgd }
503 1.1 cgd eproc.e_tdev = tty.t_dev;
504 1.1 cgd eproc.e_tsess = tty.t_session;
505 1.1 cgd if (tty.t_pgrp != NULL) {
506 1.1 cgd if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
507 1.1 cgd _kvm_err(kd, kd->program,
508 1.1 cgd "can't read tpgrp at &x",
509 1.1 cgd tty.t_pgrp);
510 1.1 cgd return (-1);
511 1.1 cgd }
512 1.1 cgd eproc.e_tpgid = pgrp.pg_id;
513 1.1 cgd } else
514 1.1 cgd eproc.e_tpgid = -1;
515 1.1 cgd } else
516 1.1 cgd eproc.e_tdev = NODEV;
517 1.1 cgd eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0;
518 1.1 cgd if (sess.s_leader == p)
519 1.1 cgd eproc.e_flag |= EPROC_SLEADER;
520 1.1 cgd if (proc.p_wmesg)
521 1.1 cgd (void)kvm_read(kd, (u_long)proc.p_wmesg,
522 1.1 cgd eproc.e_wmesg, WMESGLEN);
523 1.1 cgd
524 1.1 cgd (void)kvm_read(kd, (u_long)proc.p_vmspace,
525 1.28 christos (void *)&eproc.e_vm, sizeof(eproc.e_vm));
526 1.9 pk
527 1.1 cgd eproc.e_xsize = eproc.e_xrssize = 0;
528 1.1 cgd eproc.e_xccount = eproc.e_xswrss = 0;
529 1.1 cgd
530 1.1 cgd switch (what) {
531 1.1 cgd
532 1.1 cgd case KERN_PROC_PGRP:
533 1.1 cgd if (eproc.e_pgid != (pid_t)arg)
534 1.1 cgd continue;
535 1.1 cgd break;
536 1.1 cgd
537 1.1 cgd case KERN_PROC_TTY:
538 1.1 cgd if ((proc.p_flag & P_CONTROLT) == 0 ||
539 1.1 cgd eproc.e_tdev != (dev_t)arg)
540 1.1 cgd continue;
541 1.1 cgd break;
542 1.1 cgd }
543 1.25 perry memcpy(&bp->kp_proc, &proc, sizeof(proc));
544 1.25 perry memcpy(&bp->kp_eproc, &eproc, sizeof(eproc));
545 1.1 cgd ++bp;
546 1.1 cgd ++cnt;
547 1.1 cgd }
548 1.1 cgd return (cnt);
549 1.1 cgd }
550 1.1 cgd
551 1.1 cgd /*
552 1.1 cgd * Build proc info array by reading in proc list from a crash dump.
553 1.1 cgd * Return number of procs read. maxcnt is the max we will read.
554 1.1 cgd */
555 1.1 cgd static int
556 1.27 thorpej kvm_deadprocs(kd, what, arg, a_allproc, a_deadproc, a_zombproc, maxcnt)
557 1.1 cgd kvm_t *kd;
558 1.1 cgd int what, arg;
559 1.1 cgd u_long a_allproc;
560 1.27 thorpej u_long a_deadproc;
561 1.1 cgd u_long a_zombproc;
562 1.1 cgd int maxcnt;
563 1.1 cgd {
564 1.21 perry struct kinfo_proc *bp = kd->procbase;
565 1.27 thorpej int acnt, dcnt, zcnt;
566 1.1 cgd struct proc *p;
567 1.1 cgd
568 1.1 cgd if (KREAD(kd, a_allproc, &p)) {
569 1.1 cgd _kvm_err(kd, kd->program, "cannot read allproc");
570 1.1 cgd return (-1);
571 1.1 cgd }
572 1.1 cgd acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
573 1.1 cgd if (acnt < 0)
574 1.1 cgd return (acnt);
575 1.1 cgd
576 1.27 thorpej if (KREAD(kd, a_deadproc, &p)) {
577 1.27 thorpej _kvm_err(kd, kd->program, "cannot read deadproc");
578 1.27 thorpej return (-1);
579 1.27 thorpej }
580 1.27 thorpej
581 1.27 thorpej dcnt = kvm_proclist(kd, what, arg, p, bp, maxcnt - acnt);
582 1.27 thorpej if (dcnt < 0)
583 1.27 thorpej dcnt = 0;
584 1.27 thorpej
585 1.1 cgd if (KREAD(kd, a_zombproc, &p)) {
586 1.1 cgd _kvm_err(kd, kd->program, "cannot read zombproc");
587 1.1 cgd return (-1);
588 1.1 cgd }
589 1.27 thorpej zcnt = kvm_proclist(kd, what, arg, p, bp + acnt,
590 1.27 thorpej maxcnt - (acnt + dcnt));
591 1.1 cgd if (zcnt < 0)
592 1.1 cgd zcnt = 0;
593 1.1 cgd
594 1.1 cgd return (acnt + zcnt);
595 1.1 cgd }
596 1.1 cgd
597 1.1 cgd struct kinfo_proc *
598 1.1 cgd kvm_getprocs(kd, op, arg, cnt)
599 1.1 cgd kvm_t *kd;
600 1.1 cgd int op, arg;
601 1.1 cgd int *cnt;
602 1.1 cgd {
603 1.7 cgd size_t size;
604 1.7 cgd int mib[4], st, nprocs;
605 1.1 cgd
606 1.1 cgd if (kd->procbase != 0) {
607 1.1 cgd free((void *)kd->procbase);
608 1.1 cgd /*
609 1.1 cgd * Clear this pointer in case this call fails. Otherwise,
610 1.1 cgd * kvm_close() will free it again.
611 1.1 cgd */
612 1.1 cgd kd->procbase = 0;
613 1.1 cgd }
614 1.1 cgd if (ISALIVE(kd)) {
615 1.1 cgd size = 0;
616 1.1 cgd mib[0] = CTL_KERN;
617 1.1 cgd mib[1] = KERN_PROC;
618 1.1 cgd mib[2] = op;
619 1.1 cgd mib[3] = arg;
620 1.1 cgd st = sysctl(mib, 4, NULL, &size, NULL, 0);
621 1.1 cgd if (st == -1) {
622 1.1 cgd _kvm_syserr(kd, kd->program, "kvm_getprocs");
623 1.1 cgd return (0);
624 1.1 cgd }
625 1.1 cgd kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
626 1.1 cgd if (kd->procbase == 0)
627 1.1 cgd return (0);
628 1.1 cgd st = sysctl(mib, 4, kd->procbase, &size, NULL, 0);
629 1.1 cgd if (st == -1) {
630 1.1 cgd _kvm_syserr(kd, kd->program, "kvm_getprocs");
631 1.1 cgd return (0);
632 1.1 cgd }
633 1.1 cgd if (size % sizeof(struct kinfo_proc) != 0) {
634 1.1 cgd _kvm_err(kd, kd->program,
635 1.1 cgd "proc size mismatch (%d total, %d chunks)",
636 1.1 cgd size, sizeof(struct kinfo_proc));
637 1.1 cgd return (0);
638 1.1 cgd }
639 1.1 cgd nprocs = size / sizeof(struct kinfo_proc);
640 1.1 cgd } else {
641 1.27 thorpej struct nlist nl[5], *p;
642 1.1 cgd
643 1.1 cgd nl[0].n_name = "_nprocs";
644 1.1 cgd nl[1].n_name = "_allproc";
645 1.27 thorpej nl[2].n_name = "_deadproc";
646 1.27 thorpej nl[3].n_name = "_zombproc";
647 1.27 thorpej nl[4].n_name = 0;
648 1.1 cgd
649 1.1 cgd if (kvm_nlist(kd, nl) != 0) {
650 1.1 cgd for (p = nl; p->n_type != 0; ++p)
651 1.1 cgd ;
652 1.1 cgd _kvm_err(kd, kd->program,
653 1.1 cgd "%s: no such symbol", p->n_name);
654 1.1 cgd return (0);
655 1.1 cgd }
656 1.1 cgd if (KREAD(kd, nl[0].n_value, &nprocs)) {
657 1.1 cgd _kvm_err(kd, kd->program, "can't read nprocs");
658 1.1 cgd return (0);
659 1.1 cgd }
660 1.1 cgd size = nprocs * sizeof(struct kinfo_proc);
661 1.1 cgd kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
662 1.1 cgd if (kd->procbase == 0)
663 1.1 cgd return (0);
664 1.1 cgd
665 1.1 cgd nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
666 1.27 thorpej nl[2].n_value, nl[3].n_value, nprocs);
667 1.1 cgd #ifdef notdef
668 1.1 cgd size = nprocs * sizeof(struct kinfo_proc);
669 1.1 cgd (void)realloc(kd->procbase, size);
670 1.1 cgd #endif
671 1.1 cgd }
672 1.1 cgd *cnt = nprocs;
673 1.1 cgd return (kd->procbase);
674 1.1 cgd }
675 1.1 cgd
676 1.1 cgd void
677 1.1 cgd _kvm_freeprocs(kd)
678 1.1 cgd kvm_t *kd;
679 1.1 cgd {
680 1.1 cgd if (kd->procbase) {
681 1.1 cgd free(kd->procbase);
682 1.1 cgd kd->procbase = 0;
683 1.1 cgd }
684 1.1 cgd }
685 1.1 cgd
686 1.1 cgd void *
687 1.1 cgd _kvm_realloc(kd, p, n)
688 1.1 cgd kvm_t *kd;
689 1.1 cgd void *p;
690 1.1 cgd size_t n;
691 1.1 cgd {
692 1.1 cgd void *np = (void *)realloc(p, n);
693 1.1 cgd
694 1.1 cgd if (np == 0)
695 1.1 cgd _kvm_err(kd, kd->program, "out of memory");
696 1.1 cgd return (np);
697 1.1 cgd }
698 1.1 cgd
699 1.1 cgd #ifndef MAX
700 1.1 cgd #define MAX(a, b) ((a) > (b) ? (a) : (b))
701 1.1 cgd #endif
702 1.1 cgd
703 1.1 cgd /*
704 1.1 cgd * Read in an argument vector from the user address space of process p.
705 1.1 cgd * addr if the user-space base address of narg null-terminated contiguous
706 1.1 cgd * strings. This is used to read in both the command arguments and
707 1.1 cgd * environment strings. Read at most maxcnt characters of strings.
708 1.1 cgd */
709 1.1 cgd static char **
710 1.1 cgd kvm_argv(kd, p, addr, narg, maxcnt)
711 1.1 cgd kvm_t *kd;
712 1.15 cgd const struct proc *p;
713 1.21 perry u_long addr;
714 1.21 perry int narg;
715 1.21 perry int maxcnt;
716 1.21 perry {
717 1.21 perry char *np, *cp, *ep, *ap;
718 1.28 christos u_long oaddr = (u_long)~0L;
719 1.28 christos u_long len;
720 1.28 christos size_t cc;
721 1.21 perry char **argv;
722 1.1 cgd
723 1.1 cgd /*
724 1.1 cgd * Check that there aren't an unreasonable number of agruments,
725 1.1 cgd * and that the address is in user space.
726 1.1 cgd */
727 1.18 gwr if (narg > ARG_MAX || addr < kd->min_uva || addr >= kd->max_uva)
728 1.1 cgd return (0);
729 1.1 cgd
730 1.1 cgd if (kd->argv == 0) {
731 1.1 cgd /*
732 1.1 cgd * Try to avoid reallocs.
733 1.1 cgd */
734 1.1 cgd kd->argc = MAX(narg + 1, 32);
735 1.1 cgd kd->argv = (char **)_kvm_malloc(kd, kd->argc *
736 1.1 cgd sizeof(*kd->argv));
737 1.1 cgd if (kd->argv == 0)
738 1.1 cgd return (0);
739 1.1 cgd } else if (narg + 1 > kd->argc) {
740 1.1 cgd kd->argc = MAX(2 * kd->argc, narg + 1);
741 1.1 cgd kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc *
742 1.1 cgd sizeof(*kd->argv));
743 1.1 cgd if (kd->argv == 0)
744 1.1 cgd return (0);
745 1.1 cgd }
746 1.1 cgd if (kd->argspc == 0) {
747 1.28 christos kd->argspc = (char *)_kvm_malloc(kd, (size_t)kd->nbpg);
748 1.1 cgd if (kd->argspc == 0)
749 1.1 cgd return (0);
750 1.6 mycroft kd->arglen = kd->nbpg;
751 1.1 cgd }
752 1.10 mycroft if (kd->argbuf == 0) {
753 1.28 christos kd->argbuf = (char *)_kvm_malloc(kd, (size_t)kd->nbpg);
754 1.10 mycroft if (kd->argbuf == 0)
755 1.10 mycroft return (0);
756 1.10 mycroft }
757 1.10 mycroft cc = sizeof(char *) * narg;
758 1.28 christos if (kvm_uread(kd, p, addr, (void *)kd->argv, cc) != cc)
759 1.10 mycroft return (0);
760 1.10 mycroft ap = np = kd->argspc;
761 1.1 cgd argv = kd->argv;
762 1.1 cgd len = 0;
763 1.1 cgd /*
764 1.1 cgd * Loop over pages, filling in the argument vector.
765 1.1 cgd */
766 1.10 mycroft while (argv < kd->argv + narg && *argv != 0) {
767 1.10 mycroft addr = (u_long)*argv & ~(kd->nbpg - 1);
768 1.10 mycroft if (addr != oaddr) {
769 1.28 christos if (kvm_uread(kd, p, addr, kd->argbuf,
770 1.28 christos (size_t)kd->nbpg) != kd->nbpg)
771 1.10 mycroft return (0);
772 1.10 mycroft oaddr = addr;
773 1.10 mycroft }
774 1.10 mycroft addr = (u_long)*argv & (kd->nbpg - 1);
775 1.28 christos cp = kd->argbuf + (size_t)addr;
776 1.28 christos cc = kd->nbpg - (size_t)addr;
777 1.28 christos if (maxcnt > 0 && cc > (size_t)(maxcnt - len))
778 1.28 christos cc = (size_t)(maxcnt - len);
779 1.10 mycroft ep = memchr(cp, '\0', cc);
780 1.10 mycroft if (ep != 0)
781 1.10 mycroft cc = ep - cp + 1;
782 1.1 cgd if (len + cc > kd->arglen) {
783 1.21 perry int off;
784 1.21 perry char **pp;
785 1.21 perry char *op = kd->argspc;
786 1.1 cgd
787 1.1 cgd kd->arglen *= 2;
788 1.1 cgd kd->argspc = (char *)_kvm_realloc(kd, kd->argspc,
789 1.28 christos (size_t)kd->arglen);
790 1.1 cgd if (kd->argspc == 0)
791 1.1 cgd return (0);
792 1.1 cgd /*
793 1.1 cgd * Adjust argv pointers in case realloc moved
794 1.1 cgd * the string space.
795 1.1 cgd */
796 1.1 cgd off = kd->argspc - op;
797 1.13 mycroft for (pp = kd->argv; pp < argv; pp++)
798 1.1 cgd *pp += off;
799 1.12 mycroft ap += off;
800 1.12 mycroft np += off;
801 1.1 cgd }
802 1.10 mycroft memcpy(np, cp, cc);
803 1.10 mycroft np += cc;
804 1.1 cgd len += cc;
805 1.10 mycroft if (ep != 0) {
806 1.10 mycroft *argv++ = ap;
807 1.10 mycroft ap = np;
808 1.10 mycroft } else
809 1.10 mycroft *argv += cc;
810 1.1 cgd if (maxcnt > 0 && len >= maxcnt) {
811 1.1 cgd /*
812 1.1 cgd * We're stopping prematurely. Terminate the
813 1.10 mycroft * current string.
814 1.1 cgd */
815 1.10 mycroft if (ep == 0) {
816 1.10 mycroft *np = '\0';
817 1.14 mycroft *argv++ = ap;
818 1.10 mycroft }
819 1.10 mycroft break;
820 1.1 cgd }
821 1.1 cgd }
822 1.10 mycroft /* Make sure argv is terminated. */
823 1.10 mycroft *argv = 0;
824 1.10 mycroft return (kd->argv);
825 1.1 cgd }
826 1.1 cgd
827 1.1 cgd static void
828 1.1 cgd ps_str_a(p, addr, n)
829 1.1 cgd struct ps_strings *p;
830 1.1 cgd u_long *addr;
831 1.1 cgd int *n;
832 1.1 cgd {
833 1.1 cgd *addr = (u_long)p->ps_argvstr;
834 1.1 cgd *n = p->ps_nargvstr;
835 1.1 cgd }
836 1.1 cgd
837 1.1 cgd static void
838 1.1 cgd ps_str_e(p, addr, n)
839 1.1 cgd struct ps_strings *p;
840 1.1 cgd u_long *addr;
841 1.1 cgd int *n;
842 1.1 cgd {
843 1.1 cgd *addr = (u_long)p->ps_envstr;
844 1.1 cgd *n = p->ps_nenvstr;
845 1.1 cgd }
846 1.1 cgd
847 1.1 cgd /*
848 1.1 cgd * Determine if the proc indicated by p is still active.
849 1.1 cgd * This test is not 100% foolproof in theory, but chances of
850 1.1 cgd * being wrong are very low.
851 1.1 cgd */
852 1.1 cgd static int
853 1.1 cgd proc_verify(kd, kernp, p)
854 1.1 cgd kvm_t *kd;
855 1.1 cgd u_long kernp;
856 1.1 cgd const struct proc *p;
857 1.1 cgd {
858 1.1 cgd struct proc kernproc;
859 1.1 cgd
860 1.1 cgd /*
861 1.1 cgd * Just read in the whole proc. It's not that big relative
862 1.1 cgd * to the cost of the read system call.
863 1.1 cgd */
864 1.28 christos if (kvm_read(kd, kernp, (void *)&kernproc, sizeof(kernproc)) !=
865 1.1 cgd sizeof(kernproc))
866 1.1 cgd return (0);
867 1.1 cgd return (p->p_pid == kernproc.p_pid &&
868 1.1 cgd (kernproc.p_stat != SZOMB || p->p_stat == SZOMB));
869 1.1 cgd }
870 1.1 cgd
871 1.1 cgd static char **
872 1.1 cgd kvm_doargv(kd, kp, nchr, info)
873 1.1 cgd kvm_t *kd;
874 1.1 cgd const struct kinfo_proc *kp;
875 1.1 cgd int nchr;
876 1.10 mycroft void (*info)(struct ps_strings *, u_long *, int *);
877 1.1 cgd {
878 1.21 perry const struct proc *p = &kp->kp_proc;
879 1.21 perry char **ap;
880 1.1 cgd u_long addr;
881 1.1 cgd int cnt;
882 1.1 cgd struct ps_strings arginfo;
883 1.1 cgd
884 1.1 cgd /*
885 1.1 cgd * Pointers are stored at the top of the user stack.
886 1.1 cgd */
887 1.18 gwr if (p->p_stat == SZOMB)
888 1.18 gwr return (0);
889 1.18 gwr cnt = kvm_uread(kd, p, kd->usrstack - sizeof(arginfo),
890 1.28 christos (void *)&arginfo, sizeof(arginfo));
891 1.18 gwr if (cnt != sizeof(arginfo))
892 1.1 cgd return (0);
893 1.1 cgd
894 1.1 cgd (*info)(&arginfo, &addr, &cnt);
895 1.3 mycroft if (cnt == 0)
896 1.3 mycroft return (0);
897 1.1 cgd ap = kvm_argv(kd, p, addr, cnt, nchr);
898 1.1 cgd /*
899 1.1 cgd * For live kernels, make sure this process didn't go away.
900 1.1 cgd */
901 1.1 cgd if (ap != 0 && ISALIVE(kd) &&
902 1.1 cgd !proc_verify(kd, (u_long)kp->kp_eproc.e_paddr, p))
903 1.1 cgd ap = 0;
904 1.1 cgd return (ap);
905 1.1 cgd }
906 1.1 cgd
907 1.1 cgd /*
908 1.1 cgd * Get the command args. This code is now machine independent.
909 1.1 cgd */
910 1.1 cgd char **
911 1.1 cgd kvm_getargv(kd, kp, nchr)
912 1.1 cgd kvm_t *kd;
913 1.1 cgd const struct kinfo_proc *kp;
914 1.1 cgd int nchr;
915 1.1 cgd {
916 1.1 cgd return (kvm_doargv(kd, kp, nchr, ps_str_a));
917 1.1 cgd }
918 1.1 cgd
919 1.1 cgd char **
920 1.1 cgd kvm_getenvv(kd, kp, nchr)
921 1.1 cgd kvm_t *kd;
922 1.1 cgd const struct kinfo_proc *kp;
923 1.1 cgd int nchr;
924 1.1 cgd {
925 1.1 cgd return (kvm_doargv(kd, kp, nchr, ps_str_e));
926 1.1 cgd }
927 1.1 cgd
928 1.1 cgd /*
929 1.1 cgd * Read from user space. The user context is given by p.
930 1.1 cgd */
931 1.1 cgd ssize_t
932 1.1 cgd kvm_uread(kd, p, uva, buf, len)
933 1.1 cgd kvm_t *kd;
934 1.21 perry const struct proc *p;
935 1.21 perry u_long uva;
936 1.21 perry char *buf;
937 1.21 perry size_t len;
938 1.1 cgd {
939 1.21 perry char *cp;
940 1.1 cgd
941 1.1 cgd cp = buf;
942 1.1 cgd while (len > 0) {
943 1.28 christos size_t cc;
944 1.21 perry char *dp;
945 1.15 cgd u_long cnt;
946 1.8 mycroft
947 1.8 mycroft dp = _kvm_uread(kd, p, uva, &cnt);
948 1.8 mycroft if (dp == 0) {
949 1.8 mycroft _kvm_err(kd, 0, "invalid address (%x)", uva);
950 1.8 mycroft return (0);
951 1.8 mycroft }
952 1.28 christos cc = (size_t)MIN(cnt, len);
953 1.25 perry memcpy(cp, dp, cc);
954 1.1 cgd cp += cc;
955 1.1 cgd uva += cc;
956 1.1 cgd len -= cc;
957 1.1 cgd }
958 1.1 cgd return (ssize_t)(cp - buf);
959 1.1 cgd }
960