Home | History | Annotate | Line # | Download | only in libkvm
kvm_proc.c revision 1.22
      1 /*	$NetBSD: kvm_proc.c,v 1.22 1998/02/11 12:00:37 mrg Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
      5  * Copyright (c) 1989, 1992, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * This code is derived from software developed by the Computer Systems
      9  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
     10  * BG 91-66 and contributed to Berkeley.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  */
     40 
     41 #include <sys/cdefs.h>
     42 #if defined(LIBC_SCCS) && !defined(lint)
     43 #if 0
     44 static char sccsid[] = "@(#)kvm_proc.c	8.3 (Berkeley) 9/23/93";
     45 #else
     46 __RCSID("$NetBSD: kvm_proc.c,v 1.22 1998/02/11 12:00:37 mrg Exp $");
     47 #endif
     48 #endif /* LIBC_SCCS and not lint */
     49 
     50 /*
     51  * Proc traversal interface for kvm.  ps and w are (probably) the exclusive
     52  * users of this code, so we've factored it out into a separate module.
     53  * Thus, we keep this grunge out of the other kvm applications (i.e.,
     54  * most other applications are interested only in open/close/read/nlist).
     55  */
     56 
     57 #include <sys/param.h>
     58 #include <sys/user.h>
     59 #include <sys/proc.h>
     60 #include <sys/exec.h>
     61 #include <sys/stat.h>
     62 #include <sys/ioctl.h>
     63 #include <sys/tty.h>
     64 #include <stdlib.h>
     65 #include <string.h>
     66 #include <unistd.h>
     67 #include <nlist.h>
     68 #include <kvm.h>
     69 
     70 #include <vm/vm.h>
     71 #include <vm/vm_param.h>
     72 #include <vm/swap_pager.h>
     73 
     74 #include <sys/sysctl.h>
     75 
     76 #include <limits.h>
     77 #include <db.h>
     78 #include <paths.h>
     79 
     80 #include "kvm_private.h"
     81 
     82 #define KREAD(kd, addr, obj) \
     83 	(kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj))
     84 
     85 char		*_kvm_uread __P((kvm_t *, const struct proc *, u_long, u_long *));
     86 int		_kvm_coreinit __P((kvm_t *));
     87 int		_kvm_readfromcore __P((kvm_t *, u_long, u_long));
     88 int		_kvm_readfrompager __P((kvm_t *, struct vm_object *, u_long));
     89 ssize_t		kvm_uread __P((kvm_t *, const struct proc *, u_long, char *,
     90 		    size_t));
     91 
     92 static char	**kvm_argv __P((kvm_t *, const struct proc *, u_long, int,
     93 		    int));
     94 static int	kvm_deadprocs __P((kvm_t *, int, int, u_long, u_long, int));
     95 static char	**kvm_doargv __P((kvm_t *, const struct kinfo_proc *, int,
     96 		    void (*)(struct ps_strings *, u_long *, int *)));
     97 static int	kvm_proclist __P((kvm_t *, int, int, struct proc *,
     98 		    struct kinfo_proc *, int));
     99 static int	proc_verify __P((kvm_t *, u_long, const struct proc *));
    100 static void	ps_str_a __P((struct ps_strings *, u_long *, int *));
    101 static void	ps_str_e __P((struct ps_strings *, u_long *, int *));
    102 
    103 char *
    104 _kvm_uread(kd, p, va, cnt)
    105 	kvm_t *kd;
    106 	const struct proc *p;
    107 	u_long va;
    108 	u_long *cnt;
    109 {
    110 	u_long addr, head;
    111 	u_long offset;
    112 	struct vm_map_entry vme;
    113 	struct vm_object vmo;
    114 	int rv;
    115 
    116 	if (kd->swapspc == 0) {
    117 		kd->swapspc = (char *)_kvm_malloc(kd, kd->nbpg);
    118 		if (kd->swapspc == 0)
    119 			return (0);
    120 	}
    121 
    122 	/*
    123 	 * Look through the address map for the memory object
    124 	 * that corresponds to the given virtual address.
    125 	 * The header just has the entire valid range.
    126 	 */
    127 	head = (u_long)&p->p_vmspace->vm_map.header;
    128 	addr = head;
    129 	while (1) {
    130 		if (KREAD(kd, addr, &vme))
    131 			return (0);
    132 
    133 		if (va >= vme.start && va < vme.end &&
    134 		    vme.object.vm_object != 0)
    135 			break;
    136 
    137 		addr = (u_long)vme.next;
    138 		if (addr == head)
    139 			return (0);
    140 	}
    141 
    142 	/*
    143 	 * We found the right object -- follow shadow links.
    144 	 */
    145 	offset = va - vme.start + vme.offset;
    146 	addr = (u_long)vme.object.vm_object;
    147 
    148 	while (1) {
    149 		/* Try reading the page from core first. */
    150 		if ((rv = _kvm_readfromcore(kd, addr, offset)))
    151 			break;
    152 
    153 		if (KREAD(kd, addr, &vmo))
    154 			return (0);
    155 
    156 		/* If there is a pager here, see if it has the page. */
    157 		if (vmo.pager != 0 &&
    158 		    (rv = _kvm_readfrompager(kd, &vmo, offset)))
    159 			break;
    160 
    161 		/* Move down the shadow chain. */
    162 		addr = (u_long)vmo.shadow;
    163 		if (addr == 0)
    164 			return (0);
    165 		offset += vmo.shadow_offset;
    166 	}
    167 
    168 	if (rv == -1)
    169 		return (0);
    170 
    171 	/* Found the page. */
    172 	offset %= kd->nbpg;
    173 	*cnt = kd->nbpg - offset;
    174 	return (&kd->swapspc[offset]);
    175 }
    176 
    177 #define	vm_page_hash(kd, object, offset) \
    178 	(((u_long)object + (u_long)(offset / kd->nbpg)) & kd->vm_page_hash_mask)
    179 
    180 int
    181 _kvm_coreinit(kd)
    182 	kvm_t *kd;
    183 {
    184 	struct nlist nlist[3];
    185 
    186 	nlist[0].n_name = "_vm_page_buckets";
    187 	nlist[1].n_name = "_vm_page_hash_mask";
    188 	nlist[2].n_name = 0;
    189 	if (kvm_nlist(kd, nlist) != 0)
    190 		return (-1);
    191 
    192 	if (KREAD(kd, nlist[0].n_value, &kd->vm_page_buckets) ||
    193 	    KREAD(kd, nlist[1].n_value, &kd->vm_page_hash_mask))
    194 		return (-1);
    195 
    196 	return (0);
    197 }
    198 
    199 int
    200 _kvm_readfromcore(kd, object, offset)
    201 	kvm_t *kd;
    202 	u_long object, offset;
    203 {
    204 	u_long addr;
    205 	struct pglist bucket;
    206 	struct vm_page mem;
    207 	off_t seekpoint;
    208 
    209 	if (kd->vm_page_buckets == 0 &&
    210 	    _kvm_coreinit(kd))
    211 		return (-1);
    212 
    213 	addr = (u_long)&kd->vm_page_buckets[vm_page_hash(kd, object, offset)];
    214 	if (KREAD(kd, addr, &bucket))
    215 		return (-1);
    216 
    217 	addr = (u_long)bucket.tqh_first;
    218 	offset &= ~(kd->nbpg -1);
    219 	while (1) {
    220 		if (addr == 0)
    221 			return (0);
    222 
    223 		if (KREAD(kd, addr, &mem))
    224 			return (-1);
    225 
    226 #if defined(UVM)
    227 		if ((u_long)mem.uobject == object &&
    228 		    (u_long)mem.offset == offset)
    229 #else
    230 		if ((u_long)mem.object == object &&
    231 		    (u_long)mem.offset == offset)
    232 #endif
    233 			break;
    234 
    235 		addr = (u_long)mem.hashq.tqe_next;
    236 	}
    237 
    238 	seekpoint = mem.phys_addr;
    239 
    240 	if (lseek(kd->pmfd, seekpoint, 0) == -1)
    241 		return (-1);
    242 	if (read(kd->pmfd, kd->swapspc, kd->nbpg) != kd->nbpg)
    243 		return (-1);
    244 
    245 	return (1);
    246 }
    247 
    248 int
    249 _kvm_readfrompager(kd, vmop, offset)
    250 	kvm_t *kd;
    251 	struct vm_object *vmop;
    252 	u_long offset;
    253 {
    254 	u_long addr;
    255 	struct pager_struct pager;
    256 	struct swpager swap;
    257 	int ix;
    258 	struct swblock swb;
    259 	off_t seekpoint;
    260 
    261 	/* Read in the pager info and make sure it's a swap device. */
    262 	addr = (u_long)vmop->pager;
    263 	if (KREAD(kd, addr, &pager) || pager.pg_type != PG_SWAP)
    264 		return (-1);
    265 
    266 	/* Read in the swap_pager private data. */
    267 	addr = (u_long)pager.pg_data;
    268 	if (KREAD(kd, addr, &swap))
    269 		return (-1);
    270 
    271 	/*
    272 	 * Calculate the paging offset, and make sure it's within the
    273 	 * bounds of the pager.
    274 	 */
    275 	offset += vmop->paging_offset;
    276 	ix = offset / dbtob(swap.sw_bsize);
    277 #if 0
    278 	if (swap.sw_blocks == 0 || ix >= swap.sw_nblocks)
    279 		return (-1);
    280 #else
    281 	if (swap.sw_blocks == 0 || ix >= swap.sw_nblocks) {
    282 		int i;
    283 		printf("BUG BUG BUG BUG:\n");
    284 		printf("object %p offset %lx pgoffset %lx ",
    285 		    vmop, offset - vmop->paging_offset,
    286 		    (u_long)vmop->paging_offset);
    287 		printf("pager %p swpager %p\n",
    288 		    vmop->pager, pager.pg_data);
    289 		printf("osize %lx bsize %x blocks %p nblocks %x\n",
    290 		    (u_long)swap.sw_osize, swap.sw_bsize, swap.sw_blocks,
    291 		    swap.sw_nblocks);
    292 		for (i = 0; i < swap.sw_nblocks; i++) {
    293 			addr = (u_long)&swap.sw_blocks[i];
    294 			if (KREAD(kd, addr, &swb))
    295 				return (0);
    296 			printf("sw_blocks[%d]: block %x mask %x\n", i,
    297 			    swb.swb_block, swb.swb_mask);
    298 		}
    299 		return (-1);
    300 	}
    301 #endif
    302 
    303 	/* Read in the swap records. */
    304 	addr = (u_long)&swap.sw_blocks[ix];
    305 	if (KREAD(kd, addr, &swb))
    306 		return (-1);
    307 
    308 	/* Calculate offset within pager. */
    309 	offset %= dbtob(swap.sw_bsize);
    310 
    311 	/* Check that the page is actually present. */
    312 	if ((swb.swb_mask & (1 << (offset / kd->nbpg))) == 0)
    313 		return (0);
    314 
    315 	if (!ISALIVE(kd))
    316 		return (-1);
    317 
    318 	/* Calculate the physical address and read the page. */
    319 	seekpoint = dbtob(swb.swb_block) + (offset & ~(kd->nbpg -1));
    320 
    321 	if (lseek(kd->swfd, seekpoint, 0) == -1)
    322 		return (-1);
    323 	if (read(kd->swfd, kd->swapspc, kd->nbpg) != kd->nbpg)
    324 		return (-1);
    325 
    326 	return (1);
    327 }
    328 
    329 /*
    330  * Read proc's from memory file into buffer bp, which has space to hold
    331  * at most maxcnt procs.
    332  */
    333 static int
    334 kvm_proclist(kd, what, arg, p, bp, maxcnt)
    335 	kvm_t *kd;
    336 	int what, arg;
    337 	struct proc *p;
    338 	struct kinfo_proc *bp;
    339 	int maxcnt;
    340 {
    341 	int cnt = 0;
    342 	struct eproc eproc;
    343 	struct pgrp pgrp;
    344 	struct session sess;
    345 	struct tty tty;
    346 	struct proc proc;
    347 
    348 	for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) {
    349 		if (KREAD(kd, (u_long)p, &proc)) {
    350 			_kvm_err(kd, kd->program, "can't read proc at %x", p);
    351 			return (-1);
    352 		}
    353 		if (KREAD(kd, (u_long)proc.p_cred, &eproc.e_pcred) == 0)
    354 			(void)KREAD(kd, (u_long)eproc.e_pcred.pc_ucred,
    355 			      &eproc.e_ucred);
    356 
    357 		switch(what) {
    358 
    359 		case KERN_PROC_PID:
    360 			if (proc.p_pid != (pid_t)arg)
    361 				continue;
    362 			break;
    363 
    364 		case KERN_PROC_UID:
    365 			if (eproc.e_ucred.cr_uid != (uid_t)arg)
    366 				continue;
    367 			break;
    368 
    369 		case KERN_PROC_RUID:
    370 			if (eproc.e_pcred.p_ruid != (uid_t)arg)
    371 				continue;
    372 			break;
    373 		}
    374 		/*
    375 		 * We're going to add another proc to the set.  If this
    376 		 * will overflow the buffer, assume the reason is because
    377 		 * nprocs (or the proc list) is corrupt and declare an error.
    378 		 */
    379 		if (cnt >= maxcnt) {
    380 			_kvm_err(kd, kd->program, "nprocs corrupt");
    381 			return (-1);
    382 		}
    383 		/*
    384 		 * gather eproc
    385 		 */
    386 		eproc.e_paddr = p;
    387 		if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
    388 			_kvm_err(kd, kd->program, "can't read pgrp at %x",
    389 				 proc.p_pgrp);
    390 			return (-1);
    391 		}
    392 		eproc.e_sess = pgrp.pg_session;
    393 		eproc.e_pgid = pgrp.pg_id;
    394 		eproc.e_jobc = pgrp.pg_jobc;
    395 		if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
    396 			_kvm_err(kd, kd->program, "can't read session at %x",
    397 				pgrp.pg_session);
    398 			return (-1);
    399 		}
    400 		if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) {
    401 			if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
    402 				_kvm_err(kd, kd->program,
    403 					 "can't read tty at %x", sess.s_ttyp);
    404 				return (-1);
    405 			}
    406 			eproc.e_tdev = tty.t_dev;
    407 			eproc.e_tsess = tty.t_session;
    408 			if (tty.t_pgrp != NULL) {
    409 				if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
    410 					_kvm_err(kd, kd->program,
    411 						 "can't read tpgrp at &x",
    412 						tty.t_pgrp);
    413 					return (-1);
    414 				}
    415 				eproc.e_tpgid = pgrp.pg_id;
    416 			} else
    417 				eproc.e_tpgid = -1;
    418 		} else
    419 			eproc.e_tdev = NODEV;
    420 		eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0;
    421 		if (sess.s_leader == p)
    422 			eproc.e_flag |= EPROC_SLEADER;
    423 		if (proc.p_wmesg)
    424 			(void)kvm_read(kd, (u_long)proc.p_wmesg,
    425 			    eproc.e_wmesg, WMESGLEN);
    426 
    427 		(void)kvm_read(kd, (u_long)proc.p_vmspace,
    428 		    (char *)&eproc.e_vm, sizeof(eproc.e_vm));
    429 
    430 		eproc.e_xsize = eproc.e_xrssize = 0;
    431 		eproc.e_xccount = eproc.e_xswrss = 0;
    432 
    433 		switch (what) {
    434 
    435 		case KERN_PROC_PGRP:
    436 			if (eproc.e_pgid != (pid_t)arg)
    437 				continue;
    438 			break;
    439 
    440 		case KERN_PROC_TTY:
    441 			if ((proc.p_flag & P_CONTROLT) == 0 ||
    442 			     eproc.e_tdev != (dev_t)arg)
    443 				continue;
    444 			break;
    445 		}
    446 		bcopy(&proc, &bp->kp_proc, sizeof(proc));
    447 		bcopy(&eproc, &bp->kp_eproc, sizeof(eproc));
    448 		++bp;
    449 		++cnt;
    450 	}
    451 	return (cnt);
    452 }
    453 
    454 /*
    455  * Build proc info array by reading in proc list from a crash dump.
    456  * Return number of procs read.  maxcnt is the max we will read.
    457  */
    458 static int
    459 kvm_deadprocs(kd, what, arg, a_allproc, a_zombproc, maxcnt)
    460 	kvm_t *kd;
    461 	int what, arg;
    462 	u_long a_allproc;
    463 	u_long a_zombproc;
    464 	int maxcnt;
    465 {
    466 	struct kinfo_proc *bp = kd->procbase;
    467 	int acnt, zcnt;
    468 	struct proc *p;
    469 
    470 	if (KREAD(kd, a_allproc, &p)) {
    471 		_kvm_err(kd, kd->program, "cannot read allproc");
    472 		return (-1);
    473 	}
    474 	acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
    475 	if (acnt < 0)
    476 		return (acnt);
    477 
    478 	if (KREAD(kd, a_zombproc, &p)) {
    479 		_kvm_err(kd, kd->program, "cannot read zombproc");
    480 		return (-1);
    481 	}
    482 	zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt);
    483 	if (zcnt < 0)
    484 		zcnt = 0;
    485 
    486 	return (acnt + zcnt);
    487 }
    488 
    489 struct kinfo_proc *
    490 kvm_getprocs(kd, op, arg, cnt)
    491 	kvm_t *kd;
    492 	int op, arg;
    493 	int *cnt;
    494 {
    495 	size_t size;
    496 	int mib[4], st, nprocs;
    497 
    498 	if (kd->procbase != 0) {
    499 		free((void *)kd->procbase);
    500 		/*
    501 		 * Clear this pointer in case this call fails.  Otherwise,
    502 		 * kvm_close() will free it again.
    503 		 */
    504 		kd->procbase = 0;
    505 	}
    506 	if (ISALIVE(kd)) {
    507 		size = 0;
    508 		mib[0] = CTL_KERN;
    509 		mib[1] = KERN_PROC;
    510 		mib[2] = op;
    511 		mib[3] = arg;
    512 		st = sysctl(mib, 4, NULL, &size, NULL, 0);
    513 		if (st == -1) {
    514 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
    515 			return (0);
    516 		}
    517 		kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
    518 		if (kd->procbase == 0)
    519 			return (0);
    520 		st = sysctl(mib, 4, kd->procbase, &size, NULL, 0);
    521 		if (st == -1) {
    522 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
    523 			return (0);
    524 		}
    525 		if (size % sizeof(struct kinfo_proc) != 0) {
    526 			_kvm_err(kd, kd->program,
    527 				"proc size mismatch (%d total, %d chunks)",
    528 				size, sizeof(struct kinfo_proc));
    529 			return (0);
    530 		}
    531 		nprocs = size / sizeof(struct kinfo_proc);
    532 	} else {
    533 		struct nlist nl[4], *p;
    534 
    535 		nl[0].n_name = "_nprocs";
    536 		nl[1].n_name = "_allproc";
    537 		nl[2].n_name = "_zombproc";
    538 		nl[3].n_name = 0;
    539 
    540 		if (kvm_nlist(kd, nl) != 0) {
    541 			for (p = nl; p->n_type != 0; ++p)
    542 				;
    543 			_kvm_err(kd, kd->program,
    544 				 "%s: no such symbol", p->n_name);
    545 			return (0);
    546 		}
    547 		if (KREAD(kd, nl[0].n_value, &nprocs)) {
    548 			_kvm_err(kd, kd->program, "can't read nprocs");
    549 			return (0);
    550 		}
    551 		size = nprocs * sizeof(struct kinfo_proc);
    552 		kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
    553 		if (kd->procbase == 0)
    554 			return (0);
    555 
    556 		nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
    557 				      nl[2].n_value, nprocs);
    558 #ifdef notdef
    559 		size = nprocs * sizeof(struct kinfo_proc);
    560 		(void)realloc(kd->procbase, size);
    561 #endif
    562 	}
    563 	*cnt = nprocs;
    564 	return (kd->procbase);
    565 }
    566 
    567 void
    568 _kvm_freeprocs(kd)
    569 	kvm_t *kd;
    570 {
    571 	if (kd->procbase) {
    572 		free(kd->procbase);
    573 		kd->procbase = 0;
    574 	}
    575 }
    576 
    577 void *
    578 _kvm_realloc(kd, p, n)
    579 	kvm_t *kd;
    580 	void *p;
    581 	size_t n;
    582 {
    583 	void *np = (void *)realloc(p, n);
    584 
    585 	if (np == 0)
    586 		_kvm_err(kd, kd->program, "out of memory");
    587 	return (np);
    588 }
    589 
    590 #ifndef MAX
    591 #define MAX(a, b) ((a) > (b) ? (a) : (b))
    592 #endif
    593 
    594 /*
    595  * Read in an argument vector from the user address space of process p.
    596  * addr if the user-space base address of narg null-terminated contiguous
    597  * strings.  This is used to read in both the command arguments and
    598  * environment strings.  Read at most maxcnt characters of strings.
    599  */
    600 static char **
    601 kvm_argv(kd, p, addr, narg, maxcnt)
    602 	kvm_t *kd;
    603 	const struct proc *p;
    604 	u_long addr;
    605 	int narg;
    606 	int maxcnt;
    607 {
    608 	char *np, *cp, *ep, *ap;
    609 	u_long oaddr = -1;
    610 	int len, cc;
    611 	char **argv;
    612 
    613 	/*
    614 	 * Check that there aren't an unreasonable number of agruments,
    615 	 * and that the address is in user space.
    616 	 */
    617 	if (narg > ARG_MAX || addr < kd->min_uva || addr >= kd->max_uva)
    618 		return (0);
    619 
    620 	if (kd->argv == 0) {
    621 		/*
    622 		 * Try to avoid reallocs.
    623 		 */
    624 		kd->argc = MAX(narg + 1, 32);
    625 		kd->argv = (char **)_kvm_malloc(kd, kd->argc *
    626 						sizeof(*kd->argv));
    627 		if (kd->argv == 0)
    628 			return (0);
    629 	} else if (narg + 1 > kd->argc) {
    630 		kd->argc = MAX(2 * kd->argc, narg + 1);
    631 		kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc *
    632 						sizeof(*kd->argv));
    633 		if (kd->argv == 0)
    634 			return (0);
    635 	}
    636 	if (kd->argspc == 0) {
    637 		kd->argspc = (char *)_kvm_malloc(kd, kd->nbpg);
    638 		if (kd->argspc == 0)
    639 			return (0);
    640 		kd->arglen = kd->nbpg;
    641 	}
    642 	if (kd->argbuf == 0) {
    643 		kd->argbuf = (char *)_kvm_malloc(kd, kd->nbpg);
    644 		if (kd->argbuf == 0)
    645 			return (0);
    646 	}
    647 	cc = sizeof(char *) * narg;
    648 	if (kvm_uread(kd, p, addr, (char *)kd->argv, cc) != cc)
    649 		return (0);
    650 	ap = np = kd->argspc;
    651 	argv = kd->argv;
    652 	len = 0;
    653 	/*
    654 	 * Loop over pages, filling in the argument vector.
    655 	 */
    656 	while (argv < kd->argv + narg && *argv != 0) {
    657 		addr = (u_long)*argv & ~(kd->nbpg - 1);
    658 		if (addr != oaddr) {
    659 			if (kvm_uread(kd, p, addr, kd->argbuf, kd->nbpg) !=
    660 			    kd->nbpg)
    661 				return (0);
    662 			oaddr = addr;
    663 		}
    664 		addr = (u_long)*argv & (kd->nbpg - 1);
    665 		cp = kd->argbuf + addr;
    666 		cc = kd->nbpg - addr;
    667 		if (maxcnt > 0 && cc > maxcnt - len)
    668 			cc = maxcnt - len;;
    669 		ep = memchr(cp, '\0', cc);
    670 		if (ep != 0)
    671 			cc = ep - cp + 1;
    672 		if (len + cc > kd->arglen) {
    673 			int off;
    674 			char **pp;
    675 			char *op = kd->argspc;
    676 
    677 			kd->arglen *= 2;
    678 			kd->argspc = (char *)_kvm_realloc(kd, kd->argspc,
    679 							  kd->arglen);
    680 			if (kd->argspc == 0)
    681 				return (0);
    682 			/*
    683 			 * Adjust argv pointers in case realloc moved
    684 			 * the string space.
    685 			 */
    686 			off = kd->argspc - op;
    687 			for (pp = kd->argv; pp < argv; pp++)
    688 				*pp += off;
    689 			ap += off;
    690 			np += off;
    691 		}
    692 		memcpy(np, cp, cc);
    693 		np += cc;
    694 		len += cc;
    695 		if (ep != 0) {
    696 			*argv++ = ap;
    697 			ap = np;
    698 		} else
    699 			*argv += cc;
    700 		if (maxcnt > 0 && len >= maxcnt) {
    701 			/*
    702 			 * We're stopping prematurely.  Terminate the
    703 			 * current string.
    704 			 */
    705 			if (ep == 0) {
    706 				*np = '\0';
    707 				*argv++ = ap;
    708 			}
    709 			break;
    710 		}
    711 	}
    712 	/* Make sure argv is terminated. */
    713 	*argv = 0;
    714 	return (kd->argv);
    715 }
    716 
    717 static void
    718 ps_str_a(p, addr, n)
    719 	struct ps_strings *p;
    720 	u_long *addr;
    721 	int *n;
    722 {
    723 	*addr = (u_long)p->ps_argvstr;
    724 	*n = p->ps_nargvstr;
    725 }
    726 
    727 static void
    728 ps_str_e(p, addr, n)
    729 	struct ps_strings *p;
    730 	u_long *addr;
    731 	int *n;
    732 {
    733 	*addr = (u_long)p->ps_envstr;
    734 	*n = p->ps_nenvstr;
    735 }
    736 
    737 /*
    738  * Determine if the proc indicated by p is still active.
    739  * This test is not 100% foolproof in theory, but chances of
    740  * being wrong are very low.
    741  */
    742 static int
    743 proc_verify(kd, kernp, p)
    744 	kvm_t *kd;
    745 	u_long kernp;
    746 	const struct proc *p;
    747 {
    748 	struct proc kernproc;
    749 
    750 	/*
    751 	 * Just read in the whole proc.  It's not that big relative
    752 	 * to the cost of the read system call.
    753 	 */
    754 	if (kvm_read(kd, kernp, (char *)&kernproc, sizeof(kernproc)) !=
    755 	    sizeof(kernproc))
    756 		return (0);
    757 	return (p->p_pid == kernproc.p_pid &&
    758 		(kernproc.p_stat != SZOMB || p->p_stat == SZOMB));
    759 }
    760 
    761 static char **
    762 kvm_doargv(kd, kp, nchr, info)
    763 	kvm_t *kd;
    764 	const struct kinfo_proc *kp;
    765 	int nchr;
    766 	void (*info)(struct ps_strings *, u_long *, int *);
    767 {
    768 	const struct proc *p = &kp->kp_proc;
    769 	char **ap;
    770 	u_long addr;
    771 	int cnt;
    772 	struct ps_strings arginfo;
    773 
    774 	/*
    775 	 * Pointers are stored at the top of the user stack.
    776 	 */
    777 	if (p->p_stat == SZOMB)
    778 		return (0);
    779 	cnt = kvm_uread(kd, p, kd->usrstack - sizeof(arginfo),
    780 	    (char *)&arginfo, sizeof(arginfo));
    781 	if (cnt != sizeof(arginfo))
    782 		return (0);
    783 
    784 	(*info)(&arginfo, &addr, &cnt);
    785 	if (cnt == 0)
    786 		return (0);
    787 	ap = kvm_argv(kd, p, addr, cnt, nchr);
    788 	/*
    789 	 * For live kernels, make sure this process didn't go away.
    790 	 */
    791 	if (ap != 0 && ISALIVE(kd) &&
    792 	    !proc_verify(kd, (u_long)kp->kp_eproc.e_paddr, p))
    793 		ap = 0;
    794 	return (ap);
    795 }
    796 
    797 /*
    798  * Get the command args.  This code is now machine independent.
    799  */
    800 char **
    801 kvm_getargv(kd, kp, nchr)
    802 	kvm_t *kd;
    803 	const struct kinfo_proc *kp;
    804 	int nchr;
    805 {
    806 	return (kvm_doargv(kd, kp, nchr, ps_str_a));
    807 }
    808 
    809 char **
    810 kvm_getenvv(kd, kp, nchr)
    811 	kvm_t *kd;
    812 	const struct kinfo_proc *kp;
    813 	int nchr;
    814 {
    815 	return (kvm_doargv(kd, kp, nchr, ps_str_e));
    816 }
    817 
    818 /*
    819  * Read from user space.  The user context is given by p.
    820  */
    821 ssize_t
    822 kvm_uread(kd, p, uva, buf, len)
    823 	kvm_t *kd;
    824 	const struct proc *p;
    825 	u_long uva;
    826 	char *buf;
    827 	size_t len;
    828 {
    829 	char *cp;
    830 
    831 	cp = buf;
    832 	while (len > 0) {
    833 		int cc;
    834 		char *dp;
    835 		u_long cnt;
    836 
    837 		dp = _kvm_uread(kd, p, uva, &cnt);
    838 		if (dp == 0) {
    839 			_kvm_err(kd, 0, "invalid address (%x)", uva);
    840 			return (0);
    841 		}
    842 		cc = MIN(cnt, len);
    843 		bcopy(dp, cp, cc);
    844 
    845 		cp += cc;
    846 		uva += cc;
    847 		len -= cc;
    848 	}
    849 	return (ssize_t)(cp - buf);
    850 }
    851