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