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