xref: /netbsd-src/lib/libkvm/kvm_proc.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: kvm_proc.c,v 1.62 2006/05/11 12:00:20 yamt Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*-
40  * Copyright (c) 1989, 1992, 1993
41  *	The Regents of the University of California.  All rights reserved.
42  *
43  * This code is derived from software developed by the Computer Systems
44  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
45  * BG 91-66 and contributed to Berkeley.
46  *
47  * Redistribution and use in source and binary forms, with or without
48  * modification, are permitted provided that the following conditions
49  * are met:
50  * 1. Redistributions of source code must retain the above copyright
51  *    notice, this list of conditions and the following disclaimer.
52  * 2. Redistributions in binary form must reproduce the above copyright
53  *    notice, this list of conditions and the following disclaimer in the
54  *    documentation and/or other materials provided with the distribution.
55  * 3. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  */
71 
72 #include <sys/cdefs.h>
73 #if defined(LIBC_SCCS) && !defined(lint)
74 #if 0
75 static char sccsid[] = "@(#)kvm_proc.c	8.3 (Berkeley) 9/23/93";
76 #else
77 __RCSID("$NetBSD: kvm_proc.c,v 1.62 2006/05/11 12:00:20 yamt Exp $");
78 #endif
79 #endif /* LIBC_SCCS and not lint */
80 
81 /*
82  * Proc traversal interface for kvm.  ps and w are (probably) the exclusive
83  * users of this code, so we've factored it out into a separate module.
84  * Thus, we keep this grunge out of the other kvm applications (i.e.,
85  * most other applications are interested only in open/close/read/nlist).
86  */
87 
88 #include <sys/param.h>
89 #include <sys/user.h>
90 #include <sys/lwp.h>
91 #include <sys/proc.h>
92 #include <sys/exec.h>
93 #include <sys/stat.h>
94 #include <sys/ioctl.h>
95 #include <sys/tty.h>
96 #include <sys/resourcevar.h>
97 #include <stdlib.h>
98 #include <stddef.h>
99 #include <string.h>
100 #include <unistd.h>
101 #include <nlist.h>
102 #include <kvm.h>
103 
104 #include <uvm/uvm_extern.h>
105 #include <uvm/uvm_amap.h>
106 
107 #include <sys/sysctl.h>
108 
109 #include <limits.h>
110 #include <db.h>
111 #include <paths.h>
112 
113 #include "kvm_private.h"
114 
115 /*
116  * Common info from kinfo_proc and kinfo_proc2 used by helper routines.
117  */
118 struct miniproc {
119 	struct	vmspace *p_vmspace;
120 	char	p_stat;
121 	struct	proc *p_paddr;
122 	pid_t	p_pid;
123 };
124 
125 /*
126  * Convert from struct proc and kinfo_proc{,2} to miniproc.
127  */
128 #define PTOMINI(kp, p) \
129 	do { \
130 		(p)->p_stat = (kp)->p_stat; \
131 		(p)->p_pid = (kp)->p_pid; \
132 		(p)->p_paddr = NULL; \
133 		(p)->p_vmspace = (kp)->p_vmspace; \
134 	} while (/*CONSTCOND*/0);
135 
136 #define KPTOMINI(kp, p) \
137 	do { \
138 		(p)->p_stat = (kp)->kp_proc.p_stat; \
139 		(p)->p_pid = (kp)->kp_proc.p_pid; \
140 		(p)->p_paddr = (kp)->kp_eproc.e_paddr; \
141 		(p)->p_vmspace = (kp)->kp_proc.p_vmspace; \
142 	} while (/*CONSTCOND*/0);
143 
144 #define KP2TOMINI(kp, p) \
145 	do { \
146 		(p)->p_stat = (kp)->p_stat; \
147 		(p)->p_pid = (kp)->p_pid; \
148 		(p)->p_paddr = (void *)(long)(kp)->p_paddr; \
149 		(p)->p_vmspace = (void *)(long)(kp)->p_vmspace; \
150 	} while (/*CONSTCOND*/0);
151 
152 
153 #define KREAD(kd, addr, obj) \
154 	(kvm_read(kd, addr, (obj), sizeof(*obj)) != sizeof(*obj))
155 
156 /* XXX: What uses these two functions? */
157 char		*_kvm_uread __P((kvm_t *, const struct proc *, u_long,
158 		    u_long *));
159 ssize_t		kvm_uread __P((kvm_t *, const struct proc *, u_long, char *,
160 		    size_t));
161 
162 static char	*_kvm_ureadm __P((kvm_t *, const struct miniproc *, u_long,
163 		    u_long *));
164 static ssize_t	kvm_ureadm __P((kvm_t *, const struct miniproc *, u_long,
165 		    char *, size_t));
166 
167 static char	**kvm_argv __P((kvm_t *, const struct miniproc *, u_long, int,
168 		    int));
169 static int	kvm_deadprocs __P((kvm_t *, int, int, u_long, u_long, int));
170 static char	**kvm_doargv __P((kvm_t *, const struct miniproc *, int,
171 		    void (*)(struct ps_strings *, u_long *, int *)));
172 static char	**kvm_doargv2 __P((kvm_t *, pid_t, int, int));
173 static int	kvm_proclist __P((kvm_t *, int, int, struct proc *,
174 		    struct kinfo_proc *, int));
175 static int	proc_verify __P((kvm_t *, u_long, const struct miniproc *));
176 static void	ps_str_a __P((struct ps_strings *, u_long *, int *));
177 static void	ps_str_e __P((struct ps_strings *, u_long *, int *));
178 
179 
180 static char *
181 _kvm_ureadm(kd, p, va, cnt)
182 	kvm_t *kd;
183 	const struct miniproc *p;
184 	u_long va;
185 	u_long *cnt;
186 {
187 	int true = 1;
188 	u_long addr, head;
189 	u_long offset;
190 	struct vm_map_entry vme;
191 	struct vm_amap amap;
192 	struct vm_anon *anonp, anon;
193 	struct vm_page pg;
194 	u_long slot;
195 
196 	if (kd->swapspc == NULL) {
197 		kd->swapspc = _kvm_malloc(kd, (size_t)kd->nbpg);
198 		if (kd->swapspc == NULL)
199 			return (NULL);
200 	}
201 
202 	/*
203 	 * Look through the address map for the memory object
204 	 * that corresponds to the given virtual address.
205 	 * The header just has the entire valid range.
206 	 */
207 	head = (u_long)&p->p_vmspace->vm_map.header;
208 	addr = head;
209 	while (true) {
210 		if (KREAD(kd, addr, &vme))
211 			return (NULL);
212 
213 		if (va >= vme.start && va < vme.end &&
214 		    vme.aref.ar_amap != NULL)
215 			break;
216 
217 		addr = (u_long)vme.next;
218 		if (addr == head)
219 			return (NULL);
220 	}
221 
222 	/*
223 	 * we found the map entry, now to find the object...
224 	 */
225 	if (vme.aref.ar_amap == NULL)
226 		return (NULL);
227 
228 	addr = (u_long)vme.aref.ar_amap;
229 	if (KREAD(kd, addr, &amap))
230 		return (NULL);
231 
232 	offset = va - vme.start;
233 	slot = offset / kd->nbpg + vme.aref.ar_pageoff;
234 	/* sanity-check slot number */
235 	if (slot > amap.am_nslot)
236 		return (NULL);
237 
238 	addr = (u_long)amap.am_anon + (offset / kd->nbpg) * sizeof(anonp);
239 	if (KREAD(kd, addr, &anonp))
240 		return (NULL);
241 
242 	addr = (u_long)anonp;
243 	if (KREAD(kd, addr, &anon))
244 		return (NULL);
245 
246 	addr = (u_long)anon.an_page;
247 	if (addr) {
248 		if (KREAD(kd, addr, &pg))
249 			return (NULL);
250 
251 		if (pread(kd->pmfd, kd->swapspc, (size_t)kd->nbpg,
252 		    (off_t)pg.phys_addr) != kd->nbpg)
253 			return (NULL);
254 	} else {
255 		if (kd->swfd < 0 ||
256 		    pread(kd->swfd, kd->swapspc, (size_t)kd->nbpg,
257 		    (off_t)(anon.an_swslot * kd->nbpg)) != kd->nbpg)
258 			return (NULL);
259 	}
260 
261 	/* Found the page. */
262 	offset %= kd->nbpg;
263 	*cnt = kd->nbpg - offset;
264 	return (&kd->swapspc[(size_t)offset]);
265 }
266 
267 char *
268 _kvm_uread(kd, p, va, cnt)
269 	kvm_t *kd;
270 	const struct proc *p;
271 	u_long va;
272 	u_long *cnt;
273 {
274 	struct miniproc mp;
275 
276 	PTOMINI(p, &mp);
277 	return (_kvm_ureadm(kd, &mp, va, cnt));
278 }
279 
280 /*
281  * Read proc's from memory file into buffer bp, which has space to hold
282  * at most maxcnt procs.
283  */
284 static int
285 kvm_proclist(kd, what, arg, p, bp, maxcnt)
286 	kvm_t *kd;
287 	int what, arg;
288 	struct proc *p;
289 	struct kinfo_proc *bp;
290 	int maxcnt;
291 {
292 	int cnt = 0;
293 	int nlwps;
294 	struct kinfo_lwp *kl;
295 	struct eproc eproc;
296 	struct pgrp pgrp;
297 	struct session sess;
298 	struct tty tty;
299 	struct proc proc;
300 
301 	for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) {
302 		if (KREAD(kd, (u_long)p, &proc)) {
303 			_kvm_err(kd, kd->program, "can't read proc at %p", p);
304 			return (-1);
305 		}
306 		if (KREAD(kd, (u_long)proc.p_cred, &eproc.e_pcred) == 0)
307 			if (KREAD(kd, (u_long)eproc.e_pcred.pc_ucred,
308 			    &eproc.e_ucred)) {
309 				_kvm_err(kd, kd->program,
310 				    "can't read proc credentials at %p", p);
311 				return (-1);
312 			}
313 
314 		switch (what) {
315 
316 		case KERN_PROC_PID:
317 			if (proc.p_pid != (pid_t)arg)
318 				continue;
319 			break;
320 
321 		case KERN_PROC_UID:
322 			if (eproc.e_ucred.cr_uid != (uid_t)arg)
323 				continue;
324 			break;
325 
326 		case KERN_PROC_RUID:
327 			if (eproc.e_pcred.p_ruid != (uid_t)arg)
328 				continue;
329 			break;
330 		}
331 		/*
332 		 * We're going to add another proc to the set.  If this
333 		 * will overflow the buffer, assume the reason is because
334 		 * nprocs (or the proc list) is corrupt and declare an error.
335 		 */
336 		if (cnt >= maxcnt) {
337 			_kvm_err(kd, kd->program, "nprocs corrupt");
338 			return (-1);
339 		}
340 		/*
341 		 * gather eproc
342 		 */
343 		eproc.e_paddr = p;
344 		if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
345 			_kvm_err(kd, kd->program, "can't read pgrp at %p",
346 			    proc.p_pgrp);
347 			return (-1);
348 		}
349 		eproc.e_sess = pgrp.pg_session;
350 		eproc.e_pgid = pgrp.pg_id;
351 		eproc.e_jobc = pgrp.pg_jobc;
352 		if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
353 			_kvm_err(kd, kd->program, "can't read session at %p",
354 			    pgrp.pg_session);
355 			return (-1);
356 		}
357 		if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) {
358 			if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
359 				_kvm_err(kd, kd->program,
360 				    "can't read tty at %p", sess.s_ttyp);
361 				return (-1);
362 			}
363 			eproc.e_tdev = tty.t_dev;
364 			eproc.e_tsess = tty.t_session;
365 			if (tty.t_pgrp != NULL) {
366 				if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
367 					_kvm_err(kd, kd->program,
368 					    "can't read tpgrp at %p",
369 					    tty.t_pgrp);
370 					return (-1);
371 				}
372 				eproc.e_tpgid = pgrp.pg_id;
373 			} else
374 				eproc.e_tpgid = -1;
375 		} else
376 			eproc.e_tdev = NODEV;
377 		eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0;
378 		eproc.e_sid = sess.s_sid;
379 		if (sess.s_leader == p)
380 			eproc.e_flag |= EPROC_SLEADER;
381 		/*
382 		 * Fill in the old-style proc.p_wmesg by copying the wmesg
383 		 * from the first available LWP.
384 		 */
385 		kl = kvm_getlwps(kd, proc.p_pid,
386 		    (u_long)PTRTOUINT64(eproc.e_paddr),
387 		    sizeof(struct kinfo_lwp), &nlwps);
388 		if (kl) {
389 			if (nlwps > 0) {
390 				strcpy(eproc.e_wmesg, kl[0].l_wmesg);
391 			}
392 		}
393 		(void)kvm_read(kd, (u_long)proc.p_vmspace, &eproc.e_vm,
394 		    sizeof(eproc.e_vm));
395 
396 		eproc.e_xsize = eproc.e_xrssize = 0;
397 		eproc.e_xccount = eproc.e_xswrss = 0;
398 
399 		switch (what) {
400 
401 		case KERN_PROC_PGRP:
402 			if (eproc.e_pgid != (pid_t)arg)
403 				continue;
404 			break;
405 
406 		case KERN_PROC_TTY:
407 			if ((proc.p_flag & P_CONTROLT) == 0 ||
408 			    eproc.e_tdev != (dev_t)arg)
409 				continue;
410 			break;
411 		}
412 		memcpy(&bp->kp_proc, &proc, sizeof(proc));
413 		memcpy(&bp->kp_eproc, &eproc, sizeof(eproc));
414 		++bp;
415 		++cnt;
416 	}
417 	return (cnt);
418 }
419 
420 /*
421  * Build proc info array by reading in proc list from a crash dump.
422  * Return number of procs read.  maxcnt is the max we will read.
423  */
424 static int
425 kvm_deadprocs(kd, what, arg, a_allproc, a_zombproc, maxcnt)
426 	kvm_t *kd;
427 	int what, arg;
428 	u_long a_allproc;
429 	u_long a_zombproc;
430 	int maxcnt;
431 {
432 	struct kinfo_proc *bp = kd->procbase;
433 	int acnt, zcnt;
434 	struct proc *p;
435 
436 	if (KREAD(kd, a_allproc, &p)) {
437 		_kvm_err(kd, kd->program, "cannot read allproc");
438 		return (-1);
439 	}
440 	acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
441 	if (acnt < 0)
442 		return (acnt);
443 
444 	if (KREAD(kd, a_zombproc, &p)) {
445 		_kvm_err(kd, kd->program, "cannot read zombproc");
446 		return (-1);
447 	}
448 	zcnt = kvm_proclist(kd, what, arg, p, bp + acnt,
449 	    maxcnt - acnt);
450 	if (zcnt < 0)
451 		zcnt = 0;
452 
453 	return (acnt + zcnt);
454 }
455 
456 struct kinfo_proc2 *
457 kvm_getproc2(kd, op, arg, esize, cnt)
458 	kvm_t *kd;
459 	int op, arg;
460 	size_t esize;
461 	int *cnt;
462 {
463 	size_t size;
464 	int mib[6], st, nprocs;
465 	struct pstats pstats;
466 
467 	if (ISSYSCTL(kd)) {
468 		size = 0;
469 		mib[0] = CTL_KERN;
470 		mib[1] = KERN_PROC2;
471 		mib[2] = op;
472 		mib[3] = arg;
473 		mib[4] = (int)esize;
474 		mib[5] = 0;
475 		st = sysctl(mib, 6, NULL, &size, NULL, (size_t)0);
476 		if (st == -1) {
477 			_kvm_syserr(kd, kd->program, "kvm_getproc2");
478 			return (NULL);
479 		}
480 
481 		mib[5] = (int) (size / esize);
482 		KVM_ALLOC(kd, procbase2, size);
483 		st = sysctl(mib, 6, kd->procbase2, &size, NULL, (size_t)0);
484 		if (st == -1) {
485 			_kvm_syserr(kd, kd->program, "kvm_getproc2");
486 			return (NULL);
487 		}
488 		nprocs = (int) (size / esize);
489 	} else {
490 		char *kp2c;
491 		struct kinfo_proc *kp;
492 		struct kinfo_proc2 kp2, *kp2p;
493 		struct kinfo_lwp *kl;
494 		int i, nlwps;
495 
496 		kp = kvm_getprocs(kd, op, arg, &nprocs);
497 		if (kp == NULL)
498 			return (NULL);
499 
500 		size = nprocs * esize;
501 		KVM_ALLOC(kd, procbase2, size);
502 		kp2c = (char *)(void *)kd->procbase2;
503 		kp2p = &kp2;
504 		for (i = 0; i < nprocs; i++, kp++) {
505 			kl = kvm_getlwps(kd, kp->kp_proc.p_pid,
506 			    (u_long)PTRTOUINT64(kp->kp_eproc.e_paddr),
507 			    sizeof(struct kinfo_lwp), &nlwps);
508 			/* We use kl[0] as the "representative" LWP */
509 			memset(kp2p, 0, sizeof(kp2));
510 			kp2p->p_forw = kl[0].l_forw;
511 			kp2p->p_back = kl[0].l_back;
512 			kp2p->p_paddr = PTRTOUINT64(kp->kp_eproc.e_paddr);
513 			kp2p->p_addr = kl[0].l_addr;
514 			kp2p->p_fd = PTRTOUINT64(kp->kp_proc.p_fd);
515 			kp2p->p_cwdi = PTRTOUINT64(kp->kp_proc.p_cwdi);
516 			kp2p->p_stats = PTRTOUINT64(kp->kp_proc.p_stats);
517 			kp2p->p_limit = PTRTOUINT64(kp->kp_proc.p_limit);
518 			kp2p->p_vmspace = PTRTOUINT64(kp->kp_proc.p_vmspace);
519 			kp2p->p_sigacts = PTRTOUINT64(kp->kp_proc.p_sigacts);
520 			kp2p->p_sess = PTRTOUINT64(kp->kp_eproc.e_sess);
521 			kp2p->p_tsess = 0;
522 			kp2p->p_ru = PTRTOUINT64(kp->kp_proc.p_ru);
523 
524 			kp2p->p_eflag = 0;
525 			kp2p->p_exitsig = kp->kp_proc.p_exitsig;
526 			kp2p->p_flag = kp->kp_proc.p_flag;
527 
528 			kp2p->p_pid = kp->kp_proc.p_pid;
529 
530 			kp2p->p_ppid = kp->kp_eproc.e_ppid;
531 			kp2p->p_sid = kp->kp_eproc.e_sid;
532 			kp2p->p__pgid = kp->kp_eproc.e_pgid;
533 
534 			kp2p->p_tpgid = -1 /* XXX NO_PGID! */;
535 
536 			kp2p->p_uid = kp->kp_eproc.e_ucred.cr_uid;
537 			kp2p->p_ruid = kp->kp_eproc.e_pcred.p_ruid;
538 			kp2p->p_svuid = kp->kp_eproc.e_pcred.p_svuid;
539 			kp2p->p_gid = kp->kp_eproc.e_ucred.cr_gid;
540 			kp2p->p_rgid = kp->kp_eproc.e_pcred.p_rgid;
541 			kp2p->p_svgid = kp->kp_eproc.e_pcred.p_svgid;
542 
543 			/*CONSTCOND*/
544 			memcpy(kp2p->p_groups, kp->kp_eproc.e_ucred.cr_groups,
545 			    MIN(sizeof(kp2p->p_groups),
546 			    sizeof(kp->kp_eproc.e_ucred.cr_groups)));
547 			kp2p->p_ngroups = kp->kp_eproc.e_ucred.cr_ngroups;
548 
549 			kp2p->p_jobc = kp->kp_eproc.e_jobc;
550 			kp2p->p_tdev = kp->kp_eproc.e_tdev;
551 			kp2p->p_tpgid = kp->kp_eproc.e_tpgid;
552 			kp2p->p_tsess = PTRTOUINT64(kp->kp_eproc.e_tsess);
553 
554 			kp2p->p_estcpu = kp->kp_proc.p_estcpu;
555 			kp2p->p_rtime_sec = kp->kp_proc.p_estcpu;
556 			kp2p->p_rtime_usec = kp->kp_proc.p_estcpu;
557 			kp2p->p_cpticks = kp->kp_proc.p_cpticks;
558 			kp2p->p_pctcpu = kp->kp_proc.p_pctcpu;
559 			kp2p->p_swtime = kl[0].l_swtime;
560 			kp2p->p_slptime = kl[0].l_slptime;
561 #if 0 /* XXX thorpej */
562 			kp2p->p_schedflags = kp->kp_proc.p_schedflags;
563 #else
564 			kp2p->p_schedflags = 0;
565 #endif
566 
567 			kp2p->p_uticks = kp->kp_proc.p_uticks;
568 			kp2p->p_sticks = kp->kp_proc.p_sticks;
569 			kp2p->p_iticks = kp->kp_proc.p_iticks;
570 
571 			kp2p->p_tracep = PTRTOUINT64(kp->kp_proc.p_tracep);
572 			kp2p->p_traceflag = kp->kp_proc.p_traceflag;
573 
574 			kp2p->p_holdcnt = kl[0].l_holdcnt;
575 
576 			memcpy(&kp2p->p_siglist,
577 			    &kp->kp_proc.p_sigctx.ps_siglist,
578 			    sizeof(ki_sigset_t));
579 			memcpy(&kp2p->p_sigmask,
580 			    &kp->kp_proc.p_sigctx.ps_sigmask,
581 			    sizeof(ki_sigset_t));
582 			memcpy(&kp2p->p_sigignore,
583 			    &kp->kp_proc.p_sigctx.ps_sigignore,
584 			    sizeof(ki_sigset_t));
585 			memcpy(&kp2p->p_sigcatch,
586 			    &kp->kp_proc.p_sigctx.ps_sigcatch,
587 			    sizeof(ki_sigset_t));
588 
589 			kp2p->p_stat = kp->kp_proc.p_stat;
590 			kp2p->p_priority = kl[0].l_priority;
591 			kp2p->p_usrpri = kl[0].l_usrpri;
592 			kp2p->p_nice = kp->kp_proc.p_nice;
593 
594 			kp2p->p_xstat = kp->kp_proc.p_xstat;
595 			kp2p->p_acflag = kp->kp_proc.p_acflag;
596 
597 			/*CONSTCOND*/
598 			strncpy(kp2p->p_comm, kp->kp_proc.p_comm,
599 			    MIN(sizeof(kp2p->p_comm),
600 			    sizeof(kp->kp_proc.p_comm)));
601 
602 			strncpy(kp2p->p_wmesg, kp->kp_eproc.e_wmesg,
603 			    sizeof(kp2p->p_wmesg));
604 			kp2p->p_wchan = kl[0].l_wchan;
605 			strncpy(kp2p->p_login, kp->kp_eproc.e_login,
606 			    sizeof(kp2p->p_login));
607 
608 			kp2p->p_vm_rssize = kp->kp_eproc.e_xrssize;
609 			kp2p->p_vm_tsize = kp->kp_eproc.e_vm.vm_tsize;
610 			kp2p->p_vm_dsize = kp->kp_eproc.e_vm.vm_dsize;
611 			kp2p->p_vm_ssize = kp->kp_eproc.e_vm.vm_ssize;
612 
613 			kp2p->p_eflag = (int32_t)kp->kp_eproc.e_flag;
614 
615 			kp2p->p_realflag = kp->kp_proc.p_flag;
616 			kp2p->p_nlwps = kp->kp_proc.p_nlwps;
617 			kp2p->p_nrlwps = kp->kp_proc.p_nrlwps;
618 			kp2p->p_realstat = kp->kp_proc.p_stat;
619 
620 			if (P_ZOMBIE(&kp->kp_proc) ||
621 			    kp->kp_proc.p_stats == NULL ||
622 			    KREAD(kd, (u_long)kp->kp_proc.p_stats, &pstats)) {
623 				kp2p->p_uvalid = 0;
624 			} else {
625 				kp2p->p_uvalid = 1;
626 
627 				kp2p->p_ustart_sec = (u_int32_t)
628 				    pstats.p_start.tv_sec;
629 				kp2p->p_ustart_usec = (u_int32_t)
630 				    pstats.p_start.tv_usec;
631 
632 				kp2p->p_uutime_sec = (u_int32_t)
633 				    pstats.p_ru.ru_utime.tv_sec;
634 				kp2p->p_uutime_usec = (u_int32_t)
635 				    pstats.p_ru.ru_utime.tv_usec;
636 				kp2p->p_ustime_sec = (u_int32_t)
637 				    pstats.p_ru.ru_stime.tv_sec;
638 				kp2p->p_ustime_usec = (u_int32_t)
639 				    pstats.p_ru.ru_stime.tv_usec;
640 
641 				kp2p->p_uru_maxrss = pstats.p_ru.ru_maxrss;
642 				kp2p->p_uru_ixrss = pstats.p_ru.ru_ixrss;
643 				kp2p->p_uru_idrss = pstats.p_ru.ru_idrss;
644 				kp2p->p_uru_isrss = pstats.p_ru.ru_isrss;
645 				kp2p->p_uru_minflt = pstats.p_ru.ru_minflt;
646 				kp2p->p_uru_majflt = pstats.p_ru.ru_majflt;
647 				kp2p->p_uru_nswap = pstats.p_ru.ru_nswap;
648 				kp2p->p_uru_inblock = pstats.p_ru.ru_inblock;
649 				kp2p->p_uru_oublock = pstats.p_ru.ru_oublock;
650 				kp2p->p_uru_msgsnd = pstats.p_ru.ru_msgsnd;
651 				kp2p->p_uru_msgrcv = pstats.p_ru.ru_msgrcv;
652 				kp2p->p_uru_nsignals = pstats.p_ru.ru_nsignals;
653 				kp2p->p_uru_nvcsw = pstats.p_ru.ru_nvcsw;
654 				kp2p->p_uru_nivcsw = pstats.p_ru.ru_nivcsw;
655 
656 				kp2p->p_uctime_sec = (u_int32_t)
657 				    (pstats.p_cru.ru_utime.tv_sec +
658 				    pstats.p_cru.ru_stime.tv_sec);
659 				kp2p->p_uctime_usec = (u_int32_t)
660 				    (pstats.p_cru.ru_utime.tv_usec +
661 				    pstats.p_cru.ru_stime.tv_usec);
662 			}
663 
664 			memcpy(kp2c, &kp2, esize);
665 			kp2c += esize;
666 		}
667 	}
668 	*cnt = nprocs;
669 	return (kd->procbase2);
670 }
671 
672 struct kinfo_lwp *
673 kvm_getlwps(kd, pid, paddr, esize, cnt)
674 	kvm_t *kd;
675 	int pid;
676 	u_long paddr;
677 	size_t esize;
678 	int *cnt;
679 {
680 	size_t size;
681 	int mib[5], nlwps;
682 	ssize_t st;
683 	struct kinfo_lwp *kl;
684 
685 	if (ISSYSCTL(kd)) {
686 		size = 0;
687 		mib[0] = CTL_KERN;
688 		mib[1] = KERN_LWP;
689 		mib[2] = pid;
690 		mib[3] = (int)esize;
691 		mib[4] = 0;
692 		st = sysctl(mib, 5, NULL, &size, NULL, (size_t)0);
693 		if (st == -1) {
694 			_kvm_syserr(kd, kd->program, "kvm_getlwps");
695 			return (NULL);
696 		}
697 
698 		mib[4] = (int) (size / esize);
699 		KVM_ALLOC(kd, lwpbase, size);
700 		st = sysctl(mib, 5, kd->lwpbase, &size, NULL, (size_t)0);
701 		if (st == -1) {
702 			_kvm_syserr(kd, kd->program, "kvm_getlwps");
703 			return (NULL);
704 		}
705 		nlwps = (int) (size / esize);
706 	} else {
707 		/* grovel through the memory image */
708 		struct proc p;
709 		struct lwp l;
710 		u_long laddr;
711 		int i;
712 
713 		st = kvm_read(kd, paddr, &p, sizeof(p));
714 		if (st == -1) {
715 			_kvm_syserr(kd, kd->program, "kvm_getlwps");
716 			return (NULL);
717 		}
718 
719 		nlwps = p.p_nlwps;
720 		size = nlwps * sizeof(*kd->lwpbase);
721 		KVM_ALLOC(kd, lwpbase, size);
722 		laddr = (u_long)PTRTOUINT64(p.p_lwps.lh_first);
723 		for (i = 0; (i < nlwps) && (laddr != 0); i++) {
724 			st = kvm_read(kd, laddr, &l, sizeof(l));
725 			if (st == -1) {
726 				_kvm_syserr(kd, kd->program, "kvm_getlwps");
727 				return (NULL);
728 			}
729 			kl = &kd->lwpbase[i];
730 			kl->l_laddr = laddr;
731 			kl->l_forw = PTRTOUINT64(l.l_forw);
732 			kl->l_back = PTRTOUINT64(l.l_back);
733 			kl->l_addr = PTRTOUINT64(l.l_addr);
734 			kl->l_lid = l.l_lid;
735 			kl->l_flag = l.l_flag;
736 			kl->l_swtime = l.l_swtime;
737 			kl->l_slptime = l.l_slptime;
738 			kl->l_schedflags = 0; /* XXX */
739 			kl->l_holdcnt = l.l_holdcnt;
740 			kl->l_priority = l.l_priority;
741 			kl->l_usrpri = l.l_usrpri;
742 			kl->l_stat = l.l_stat;
743 			kl->l_wchan = PTRTOUINT64(l.l_wchan);
744 			if (l.l_wmesg)
745 				(void)kvm_read(kd, (u_long)l.l_wmesg,
746 				    kl->l_wmesg, (size_t)WMESGLEN);
747 			kl->l_cpuid = KI_NOCPU;
748 			laddr = (u_long)PTRTOUINT64(l.l_sibling.le_next);
749 		}
750 	}
751 
752 	*cnt = nlwps;
753 	return (kd->lwpbase);
754 }
755 
756 struct kinfo_proc *
757 kvm_getprocs(kd, op, arg, cnt)
758 	kvm_t *kd;
759 	int op, arg;
760 	int *cnt;
761 {
762 	size_t size;
763 	int mib[4], st, nprocs;
764 
765 	if (ISKMEM(kd)) {
766 		size = 0;
767 		mib[0] = CTL_KERN;
768 		mib[1] = KERN_PROC;
769 		mib[2] = op;
770 		mib[3] = arg;
771 		st = sysctl(mib, 4, NULL, &size, NULL, (size_t)0);
772 		if (st == -1) {
773 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
774 			return (NULL);
775 		}
776 		KVM_ALLOC(kd, procbase, size);
777 		st = sysctl(mib, 4, kd->procbase, &size, NULL, (size_t)0);
778 		if (st == -1) {
779 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
780 			return (NULL);
781 		}
782 		if (size % sizeof(struct kinfo_proc) != 0) {
783 			_kvm_err(kd, kd->program,
784 			    "proc size mismatch (%lu total, %lu chunks)",
785 			    (u_long)size, (u_long)sizeof(struct kinfo_proc));
786 			return (NULL);
787 		}
788 		nprocs = (int) (size / sizeof(struct kinfo_proc));
789 	} else if (ISSYSCTL(kd)) {
790 		_kvm_err(kd, kd->program, "kvm_open called with KVM_NO_FILES, "
791 		    "can't use kvm_getprocs");
792 		return (NULL);
793 	} else {
794 		struct nlist nl[4], *p;
795 
796 		(void)memset(nl, 0, sizeof(nl));
797 		nl[0].n_name = "_nprocs";
798 		nl[1].n_name = "_allproc";
799 		nl[2].n_name = "_zombproc";
800 		nl[3].n_name = NULL;
801 
802 		if (kvm_nlist(kd, nl) != 0) {
803 			for (p = nl; p->n_type != 0; ++p)
804 				continue;
805 			_kvm_err(kd, kd->program,
806 			    "%s: no such symbol", p->n_name);
807 			return (NULL);
808 		}
809 		if (KREAD(kd, nl[0].n_value, &nprocs)) {
810 			_kvm_err(kd, kd->program, "can't read nprocs");
811 			return (NULL);
812 		}
813 		size = nprocs * sizeof(*kd->procbase);
814 		KVM_ALLOC(kd, procbase, size);
815 		nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
816 		    nl[2].n_value, nprocs);
817 		if (nprocs < 0)
818 			return (NULL);
819 #ifdef notdef
820 		size = nprocs * sizeof(struct kinfo_proc);
821 		(void)realloc(kd->procbase, size);
822 #endif
823 	}
824 	*cnt = nprocs;
825 	return (kd->procbase);
826 }
827 
828 void *
829 _kvm_realloc(kd, p, n)
830 	kvm_t *kd;
831 	void *p;
832 	size_t n;
833 {
834 	void *np = realloc(p, n);
835 
836 	if (np == NULL)
837 		_kvm_err(kd, kd->program, "out of memory");
838 	return (np);
839 }
840 
841 /*
842  * Read in an argument vector from the user address space of process p.
843  * addr if the user-space base address of narg null-terminated contiguous
844  * strings.  This is used to read in both the command arguments and
845  * environment strings.  Read at most maxcnt characters of strings.
846  */
847 static char **
848 kvm_argv(kd, p, addr, narg, maxcnt)
849 	kvm_t *kd;
850 	const struct miniproc *p;
851 	u_long addr;
852 	int narg;
853 	int maxcnt;
854 {
855 	char *np, *cp, *ep, *ap;
856 	u_long oaddr = (u_long)~0L;
857 	u_long len;
858 	size_t cc;
859 	char **argv;
860 
861 	/*
862 	 * Check that there aren't an unreasonable number of arguments,
863 	 * and that the address is in user space.
864 	 */
865 	if (narg > ARG_MAX || addr < kd->min_uva || addr >= kd->max_uva)
866 		return (NULL);
867 
868 	if (kd->argv == NULL) {
869 		/*
870 		 * Try to avoid reallocs.
871 		 */
872 		kd->argc = MAX(narg + 1, 32);
873 		kd->argv = _kvm_malloc(kd, kd->argc * sizeof(*kd->argv));
874 		if (kd->argv == NULL)
875 			return (NULL);
876 	} else if (narg + 1 > kd->argc) {
877 		kd->argc = MAX(2 * kd->argc, narg + 1);
878 		kd->argv = _kvm_realloc(kd, kd->argv, kd->argc *
879 		    sizeof(*kd->argv));
880 		if (kd->argv == NULL)
881 			return (NULL);
882 	}
883 	if (kd->argspc == NULL) {
884 		kd->argspc = _kvm_malloc(kd, (size_t)kd->nbpg);
885 		if (kd->argspc == NULL)
886 			return (NULL);
887 		kd->argspc_len = kd->nbpg;
888 	}
889 	if (kd->argbuf == NULL) {
890 		kd->argbuf = _kvm_malloc(kd, (size_t)kd->nbpg);
891 		if (kd->argbuf == NULL)
892 			return (NULL);
893 	}
894 	cc = sizeof(char *) * narg;
895 	if (kvm_ureadm(kd, p, addr, (void *)kd->argv, cc) != cc)
896 		return (NULL);
897 	ap = np = kd->argspc;
898 	argv = kd->argv;
899 	len = 0;
900 	/*
901 	 * Loop over pages, filling in the argument vector.
902 	 */
903 	while (argv < kd->argv + narg && *argv != NULL) {
904 		addr = (u_long)*argv & ~(kd->nbpg - 1);
905 		if (addr != oaddr) {
906 			if (kvm_ureadm(kd, p, addr, kd->argbuf,
907 			    (size_t)kd->nbpg) != kd->nbpg)
908 				return (NULL);
909 			oaddr = addr;
910 		}
911 		addr = (u_long)*argv & (kd->nbpg - 1);
912 		cp = kd->argbuf + (size_t)addr;
913 		cc = kd->nbpg - (size_t)addr;
914 		if (maxcnt > 0 && cc > (size_t)(maxcnt - len))
915 			cc = (size_t)(maxcnt - len);
916 		ep = memchr(cp, '\0', cc);
917 		if (ep != NULL)
918 			cc = ep - cp + 1;
919 		if (len + cc > kd->argspc_len) {
920 			ptrdiff_t off;
921 			char **pp;
922 			char *op = kd->argspc;
923 
924 			kd->argspc_len *= 2;
925 			kd->argspc = _kvm_realloc(kd, kd->argspc,
926 			    kd->argspc_len);
927 			if (kd->argspc == NULL)
928 				return (NULL);
929 			/*
930 			 * Adjust argv pointers in case realloc moved
931 			 * the string space.
932 			 */
933 			off = kd->argspc - op;
934 			for (pp = kd->argv; pp < argv; pp++)
935 				*pp += off;
936 			ap += off;
937 			np += off;
938 		}
939 		memcpy(np, cp, cc);
940 		np += cc;
941 		len += cc;
942 		if (ep != NULL) {
943 			*argv++ = ap;
944 			ap = np;
945 		} else
946 			*argv += cc;
947 		if (maxcnt > 0 && len >= maxcnt) {
948 			/*
949 			 * We're stopping prematurely.  Terminate the
950 			 * current string.
951 			 */
952 			if (ep == NULL) {
953 				*np = '\0';
954 				*argv++ = ap;
955 			}
956 			break;
957 		}
958 	}
959 	/* Make sure argv is terminated. */
960 	*argv = NULL;
961 	return (kd->argv);
962 }
963 
964 static void
965 ps_str_a(p, addr, n)
966 	struct ps_strings *p;
967 	u_long *addr;
968 	int *n;
969 {
970 
971 	*addr = (u_long)p->ps_argvstr;
972 	*n = p->ps_nargvstr;
973 }
974 
975 static void
976 ps_str_e(p, addr, n)
977 	struct ps_strings *p;
978 	u_long *addr;
979 	int *n;
980 {
981 
982 	*addr = (u_long)p->ps_envstr;
983 	*n = p->ps_nenvstr;
984 }
985 
986 /*
987  * Determine if the proc indicated by p is still active.
988  * This test is not 100% foolproof in theory, but chances of
989  * being wrong are very low.
990  */
991 static int
992 proc_verify(kd, kernp, p)
993 	kvm_t *kd;
994 	u_long kernp;
995 	const struct miniproc *p;
996 {
997 	struct proc kernproc;
998 
999 	/*
1000 	 * Just read in the whole proc.  It's not that big relative
1001 	 * to the cost of the read system call.
1002 	 */
1003 	if (kvm_read(kd, kernp, &kernproc, sizeof(kernproc)) !=
1004 	    sizeof(kernproc))
1005 		return (0);
1006 	return (p->p_pid == kernproc.p_pid &&
1007 	    (kernproc.p_stat != SZOMB || p->p_stat == SZOMB));
1008 }
1009 
1010 static char **
1011 kvm_doargv(kd, p, nchr, info)
1012 	kvm_t *kd;
1013 	const struct miniproc *p;
1014 	int nchr;
1015 	void (*info)(struct ps_strings *, u_long *, int *);
1016 {
1017 	char **ap;
1018 	u_long addr;
1019 	int cnt;
1020 	struct ps_strings arginfo;
1021 
1022 	/*
1023 	 * Pointers are stored at the top of the user stack.
1024 	 */
1025 	if (p->p_stat == SZOMB)
1026 		return (NULL);
1027 	cnt = (int)kvm_ureadm(kd, p, kd->usrstack - sizeof(arginfo),
1028 	    (void *)&arginfo, sizeof(arginfo));
1029 	if (cnt != sizeof(arginfo))
1030 		return (NULL);
1031 
1032 	(*info)(&arginfo, &addr, &cnt);
1033 	if (cnt == 0)
1034 		return (NULL);
1035 	ap = kvm_argv(kd, p, addr, cnt, nchr);
1036 	/*
1037 	 * For live kernels, make sure this process didn't go away.
1038 	 */
1039 	if (ap != NULL && ISALIVE(kd) &&
1040 	    !proc_verify(kd, (u_long)p->p_paddr, p))
1041 		ap = NULL;
1042 	return (ap);
1043 }
1044 
1045 /*
1046  * Get the command args.  This code is now machine independent.
1047  */
1048 char **
1049 kvm_getargv(kd, kp, nchr)
1050 	kvm_t *kd;
1051 	const struct kinfo_proc *kp;
1052 	int nchr;
1053 {
1054 	struct miniproc p;
1055 
1056 	KPTOMINI(kp, &p);
1057 	return (kvm_doargv(kd, &p, nchr, ps_str_a));
1058 }
1059 
1060 char **
1061 kvm_getenvv(kd, kp, nchr)
1062 	kvm_t *kd;
1063 	const struct kinfo_proc *kp;
1064 	int nchr;
1065 {
1066 	struct miniproc p;
1067 
1068 	KPTOMINI(kp, &p);
1069 	return (kvm_doargv(kd, &p, nchr, ps_str_e));
1070 }
1071 
1072 static char **
1073 kvm_doargv2(kd, pid, type, nchr)
1074 	kvm_t *kd;
1075 	pid_t pid;
1076 	int type;
1077 	int nchr;
1078 {
1079 	size_t bufs;
1080 	int narg, mib[4];
1081 	size_t newargspc_len;
1082 	char **ap, *bp, *endp;
1083 
1084 	/*
1085 	 * Check that there aren't an unreasonable number of arguments.
1086 	 */
1087 	if (nchr > ARG_MAX)
1088 		return (NULL);
1089 
1090 	if (nchr == 0)
1091 		nchr = ARG_MAX;
1092 
1093 	/* Get number of strings in argv */
1094 	mib[0] = CTL_KERN;
1095 	mib[1] = KERN_PROC_ARGS;
1096 	mib[2] = pid;
1097 	mib[3] = type == KERN_PROC_ARGV ? KERN_PROC_NARGV : KERN_PROC_NENV;
1098 	bufs = sizeof(narg);
1099 	if (sysctl(mib, 4, &narg, &bufs, NULL, (size_t)0) == -1)
1100 		return (NULL);
1101 
1102 	if (kd->argv == NULL) {
1103 		/*
1104 		 * Try to avoid reallocs.
1105 		 */
1106 		kd->argc = MAX(narg + 1, 32);
1107 		kd->argv = _kvm_malloc(kd, kd->argc * sizeof(*kd->argv));
1108 		if (kd->argv == NULL)
1109 			return (NULL);
1110 	} else if (narg + 1 > kd->argc) {
1111 		kd->argc = MAX(2 * kd->argc, narg + 1);
1112 		kd->argv = _kvm_realloc(kd, kd->argv, kd->argc *
1113 		    sizeof(*kd->argv));
1114 		if (kd->argv == NULL)
1115 			return (NULL);
1116 	}
1117 
1118 	newargspc_len = MIN(nchr, ARG_MAX);
1119 	KVM_ALLOC(kd, argspc, newargspc_len);
1120 	memset(kd->argspc, 0, (size_t)kd->argspc_len);	/* XXX necessary? */
1121 
1122 	mib[0] = CTL_KERN;
1123 	mib[1] = KERN_PROC_ARGS;
1124 	mib[2] = pid;
1125 	mib[3] = type;
1126 	bufs = kd->argspc_len;
1127 	if (sysctl(mib, 4, kd->argspc, &bufs, NULL, (size_t)0) == -1)
1128 		return (NULL);
1129 
1130 	bp = kd->argspc;
1131 	bp[kd->argspc_len-1] = '\0';	/* make sure the string ends with nul */
1132 	ap = kd->argv;
1133 	endp = bp + MIN(nchr, bufs);
1134 
1135 	while (bp < endp) {
1136 		*ap++ = bp;
1137 		/*
1138 		 * XXX: don't need following anymore, or stick check
1139 		 * for max argc in above while loop?
1140 		 */
1141 		if (ap >= kd->argv + kd->argc) {
1142 			kd->argc *= 2;
1143 			kd->argv = _kvm_realloc(kd, kd->argv,
1144 			    kd->argc * sizeof(*kd->argv));
1145 			ap = kd->argv;
1146 		}
1147 		bp += strlen(bp) + 1;
1148 	}
1149 	*ap = NULL;
1150 
1151 	return (kd->argv);
1152 }
1153 
1154 char **
1155 kvm_getargv2(kd, kp, nchr)
1156 	kvm_t *kd;
1157 	const struct kinfo_proc2 *kp;
1158 	int nchr;
1159 {
1160 
1161 	return (kvm_doargv2(kd, kp->p_pid, KERN_PROC_ARGV, nchr));
1162 }
1163 
1164 char **
1165 kvm_getenvv2(kd, kp, nchr)
1166 	kvm_t *kd;
1167 	const struct kinfo_proc2 *kp;
1168 	int nchr;
1169 {
1170 
1171 	return (kvm_doargv2(kd, kp->p_pid, KERN_PROC_ENV, nchr));
1172 }
1173 
1174 /*
1175  * Read from user space.  The user context is given by p.
1176  */
1177 static ssize_t
1178 kvm_ureadm(kd, p, uva, buf, len)
1179 	kvm_t *kd;
1180 	const struct miniproc *p;
1181 	u_long uva;
1182 	char *buf;
1183 	size_t len;
1184 {
1185 	char *cp;
1186 
1187 	cp = buf;
1188 	while (len > 0) {
1189 		size_t cc;
1190 		char *dp;
1191 		u_long cnt;
1192 
1193 		dp = _kvm_ureadm(kd, p, uva, &cnt);
1194 		if (dp == NULL) {
1195 			_kvm_err(kd, 0, "invalid address (%lx)", uva);
1196 			return (0);
1197 		}
1198 		cc = (size_t)MIN(cnt, len);
1199 		memcpy(cp, dp, cc);
1200 		cp += cc;
1201 		uva += cc;
1202 		len -= cc;
1203 	}
1204 	return (ssize_t)(cp - buf);
1205 }
1206 
1207 ssize_t
1208 kvm_uread(kd, p, uva, buf, len)
1209 	kvm_t *kd;
1210 	const struct proc *p;
1211 	u_long uva;
1212 	char *buf;
1213 	size_t len;
1214 {
1215 	struct miniproc mp;
1216 
1217 	PTOMINI(p, &mp);
1218 	return (kvm_ureadm(kd, &mp, uva, buf, len));
1219 }
1220