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