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