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