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