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