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