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