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