Home | History | Annotate | Line # | Download | only in libkvm
kvm_proc.c revision 1.19
      1 /*	$NetBSD: kvm_proc.c,v 1.19 1997/08/15 02:22:02 mikel 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.19 1997/08/15 02:22:02 mikel 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 int		_kvm_readfromcore __P((kvm_t *, u_long, u_long));
     86 int		_kvm_readfrompager __P((kvm_t *, struct vm_object *, u_long));
     87 ssize_t		kvm_uread __P((kvm_t *, const struct proc *, u_long, char *,
     88 		    size_t));
     89 
     90 static char	**kvm_argv __P((kvm_t *, const struct proc *, u_long, int,
     91 		    int));
     92 static int	kvm_deadprocs __P((kvm_t *, int, int, u_long, u_long, int));
     93 static char	**kvm_doargv __P((kvm_t *, const struct kinfo_proc *, int,
     94 		    void (*)(struct ps_strings *, u_long *, int *)));
     95 static int	kvm_proclist __P((kvm_t *, int, int, struct proc *,
     96 		    struct kinfo_proc *, int));
     97 static int	proc_verify __P((kvm_t *, u_long, const struct proc *));
     98 static void	ps_str_a __P((struct ps_strings *, u_long *, int *));
     99 static void	ps_str_e __P((struct ps_strings *, u_long *, int *));
    100 
    101 char *
    102 _kvm_uread(kd, p, va, cnt)
    103 	kvm_t *kd;
    104 	const struct proc *p;
    105 	u_long va;
    106 	u_long *cnt;
    107 {
    108 	register u_long addr, head;
    109 	register u_long offset;
    110 	struct vm_map_entry vme;
    111 	struct vm_object vmo;
    112 	int rv;
    113 
    114 	if (kd->swapspc == 0) {
    115 		kd->swapspc = (char *)_kvm_malloc(kd, kd->nbpg);
    116 		if (kd->swapspc == 0)
    117 			return (0);
    118 	}
    119 
    120 	/*
    121 	 * Look through the address map for the memory object
    122 	 * that corresponds to the given virtual address.
    123 	 * The header just has the entire valid range.
    124 	 */
    125 	head = (u_long)&p->p_vmspace->vm_map.header;
    126 	addr = head;
    127 	while (1) {
    128 		if (KREAD(kd, addr, &vme))
    129 			return (0);
    130 
    131 		if (va >= vme.start && va < vme.end &&
    132 		    vme.object.vm_object != 0)
    133 			break;
    134 
    135 		addr = (u_long)vme.next;
    136 		if (addr == head)
    137 			return (0);
    138 	}
    139 
    140 	/*
    141 	 * We found the right object -- follow shadow links.
    142 	 */
    143 	offset = va - vme.start + vme.offset;
    144 	addr = (u_long)vme.object.vm_object;
    145 
    146 	while (1) {
    147 		/* Try reading the page from core first. */
    148 		if ((rv = _kvm_readfromcore(kd, addr, offset)))
    149 			break;
    150 
    151 		if (KREAD(kd, addr, &vmo))
    152 			return (0);
    153 
    154 		/* If there is a pager here, see if it has the page. */
    155 		if (vmo.pager != 0 &&
    156 		    (rv = _kvm_readfrompager(kd, &vmo, offset)))
    157 			break;
    158 
    159 		/* Move down the shadow chain. */
    160 		addr = (u_long)vmo.shadow;
    161 		if (addr == 0)
    162 			return (0);
    163 		offset += vmo.shadow_offset;
    164 	}
    165 
    166 	if (rv == -1)
    167 		return (0);
    168 
    169 	/* Found the page. */
    170 	offset %= kd->nbpg;
    171 	*cnt = kd->nbpg - offset;
    172 	return (&kd->swapspc[offset]);
    173 }
    174 
    175 #define	vm_page_hash(kd, object, offset) \
    176 	(((u_long)object + (u_long)(offset / kd->nbpg)) & kd->vm_page_hash_mask)
    177 
    178 int
    179 _kvm_coreinit(kd)
    180 	kvm_t *kd;
    181 {
    182 	struct nlist nlist[3];
    183 
    184 	nlist[0].n_name = "_vm_page_buckets";
    185 	nlist[1].n_name = "_vm_page_hash_mask";
    186 	nlist[2].n_name = 0;
    187 	if (kvm_nlist(kd, nlist) != 0)
    188 		return (-1);
    189 
    190 	if (KREAD(kd, nlist[0].n_value, &kd->vm_page_buckets) ||
    191 	    KREAD(kd, nlist[1].n_value, &kd->vm_page_hash_mask))
    192 		return (-1);
    193 
    194 	return (0);
    195 }
    196 
    197 int
    198 _kvm_readfromcore(kd, object, offset)
    199 	kvm_t *kd;
    200 	u_long object, offset;
    201 {
    202 	u_long addr;
    203 	struct pglist bucket;
    204 	struct vm_page mem;
    205 	off_t seekpoint;
    206 
    207 	if (kd->vm_page_buckets == 0 &&
    208 	    _kvm_coreinit(kd))
    209 		return (-1);
    210 
    211 	addr = (u_long)&kd->vm_page_buckets[vm_page_hash(kd, object, offset)];
    212 	if (KREAD(kd, addr, &bucket))
    213 		return (-1);
    214 
    215 	addr = (u_long)bucket.tqh_first;
    216 	offset &= ~(kd->nbpg -1);
    217 	while (1) {
    218 		if (addr == 0)
    219 			return (0);
    220 
    221 		if (KREAD(kd, addr, &mem))
    222 			return (-1);
    223 
    224 		if ((u_long)mem.object == object &&
    225 		    (u_long)mem.offset == offset)
    226 			break;
    227 
    228 		addr = (u_long)mem.hashq.tqe_next;
    229 	}
    230 
    231 	seekpoint = mem.phys_addr;
    232 
    233 	if (lseek(kd->pmfd, seekpoint, 0) == -1)
    234 		return (-1);
    235 	if (read(kd->pmfd, kd->swapspc, kd->nbpg) != kd->nbpg)
    236 		return (-1);
    237 
    238 	return (1);
    239 }
    240 
    241 int
    242 _kvm_readfrompager(kd, vmop, offset)
    243 	kvm_t *kd;
    244 	struct vm_object *vmop;
    245 	u_long offset;
    246 {
    247 	u_long addr;
    248 	struct pager_struct pager;
    249 	struct swpager swap;
    250 	int ix;
    251 	struct swblock swb;
    252 	off_t seekpoint;
    253 
    254 	/* Read in the pager info and make sure it's a swap device. */
    255 	addr = (u_long)vmop->pager;
    256 	if (KREAD(kd, addr, &pager) || pager.pg_type != PG_SWAP)
    257 		return (-1);
    258 
    259 	/* Read in the swap_pager private data. */
    260 	addr = (u_long)pager.pg_data;
    261 	if (KREAD(kd, addr, &swap))
    262 		return (-1);
    263 
    264 	/*
    265 	 * Calculate the paging offset, and make sure it's within the
    266 	 * bounds of the pager.
    267 	 */
    268 	offset += vmop->paging_offset;
    269 	ix = offset / dbtob(swap.sw_bsize);
    270 #if 0
    271 	if (swap.sw_blocks == 0 || ix >= swap.sw_nblocks)
    272 		return (-1);
    273 #else
    274 	if (swap.sw_blocks == 0 || ix >= swap.sw_nblocks) {
    275 		int i;
    276 		printf("BUG BUG BUG BUG:\n");
    277 		printf("object %p offset %lx pgoffset %lx ",
    278 		    vmop, offset - vmop->paging_offset,
    279 		    (u_long)vmop->paging_offset);
    280 		printf("pager %p swpager %p\n",
    281 		    vmop->pager, pager.pg_data);
    282 		printf("osize %lx bsize %x blocks %p nblocks %x\n",
    283 		    (u_long)swap.sw_osize, swap.sw_bsize, swap.sw_blocks,
    284 		    swap.sw_nblocks);
    285 		for (ix = 0; ix < swap.sw_nblocks; ix++) {
    286 			addr = (u_long)&swap.sw_blocks[ix];
    287 			if (KREAD(kd, addr, &swb))
    288 				return (0);
    289 			printf("sw_blocks[%d]: block %x mask %x\n", ix,
    290 			    swb.swb_block, swb.swb_mask);
    291 		}
    292 		return (-1);
    293 	}
    294 #endif
    295 
    296 	/* Read in the swap records. */
    297 	addr = (u_long)&swap.sw_blocks[ix];
    298 	if (KREAD(kd, addr, &swb))
    299 		return (-1);
    300 
    301 	/* Calculate offset within pager. */
    302 	offset %= dbtob(swap.sw_bsize);
    303 
    304 	/* Check that the page is actually present. */
    305 	if ((swb.swb_mask & (1 << (offset / kd->nbpg))) == 0)
    306 		return (0);
    307 
    308 	if (!ISALIVE(kd))
    309 		return (-1);
    310 
    311 	/* Calculate the physical address and read the page. */
    312 	seekpoint = dbtob(swb.swb_block) + (offset & ~(kd->nbpg -1));
    313 
    314 	if (lseek(kd->swfd, seekpoint, 0) == -1)
    315 		return (-1);
    316 	if (read(kd->swfd, kd->swapspc, kd->nbpg) != kd->nbpg)
    317 		return (-1);
    318 
    319 	return (1);
    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(kd, what, arg, p, bp, maxcnt)
    328 	kvm_t *kd;
    329 	int what, arg;
    330 	struct proc *p;
    331 	struct kinfo_proc *bp;
    332 	int maxcnt;
    333 {
    334 	register int cnt = 0;
    335 	struct eproc eproc;
    336 	struct pgrp pgrp;
    337 	struct session sess;
    338 	struct tty tty;
    339 	struct proc proc;
    340 
    341 	for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) {
    342 		if (KREAD(kd, (u_long)p, &proc)) {
    343 			_kvm_err(kd, kd->program, "can't read proc at %x", p);
    344 			return (-1);
    345 		}
    346 		if (KREAD(kd, (u_long)proc.p_cred, &eproc.e_pcred) == 0)
    347 			KREAD(kd, (u_long)eproc.e_pcred.pc_ucred,
    348 			      &eproc.e_ucred);
    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 %x",
    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 %x",
    390 				pgrp.pg_session);
    391 			return (-1);
    392 		}
    393 		if ((proc.p_flag & P_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 %x", sess.s_ttyp);
    397 				return (-1);
    398 			}
    399 			eproc.e_tdev = 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 &x",
    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 = NODEV;
    413 		eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0;
    414 		if (sess.s_leader == p)
    415 			eproc.e_flag |= EPROC_SLEADER;
    416 		if (proc.p_wmesg)
    417 			(void)kvm_read(kd, (u_long)proc.p_wmesg,
    418 			    eproc.e_wmesg, WMESGLEN);
    419 
    420 		(void)kvm_read(kd, (u_long)proc.p_vmspace,
    421 		    (char *)&eproc.e_vm, sizeof(eproc.e_vm));
    422 
    423 		eproc.e_xsize = eproc.e_xrssize = 0;
    424 		eproc.e_xccount = eproc.e_xswrss = 0;
    425 
    426 		switch (what) {
    427 
    428 		case KERN_PROC_PGRP:
    429 			if (eproc.e_pgid != (pid_t)arg)
    430 				continue;
    431 			break;
    432 
    433 		case KERN_PROC_TTY:
    434 			if ((proc.p_flag & P_CONTROLT) == 0 ||
    435 			     eproc.e_tdev != (dev_t)arg)
    436 				continue;
    437 			break;
    438 		}
    439 		bcopy(&proc, &bp->kp_proc, sizeof(proc));
    440 		bcopy(&eproc, &bp->kp_eproc, sizeof(eproc));
    441 		++bp;
    442 		++cnt;
    443 	}
    444 	return (cnt);
    445 }
    446 
    447 /*
    448  * Build proc info array by reading in proc list from a crash dump.
    449  * Return number of procs read.  maxcnt is the max we will read.
    450  */
    451 static int
    452 kvm_deadprocs(kd, what, arg, a_allproc, a_zombproc, maxcnt)
    453 	kvm_t *kd;
    454 	int what, arg;
    455 	u_long a_allproc;
    456 	u_long a_zombproc;
    457 	int maxcnt;
    458 {
    459 	register struct kinfo_proc *bp = kd->procbase;
    460 	register int acnt, zcnt;
    461 	struct proc *p;
    462 
    463 	if (KREAD(kd, a_allproc, &p)) {
    464 		_kvm_err(kd, kd->program, "cannot read allproc");
    465 		return (-1);
    466 	}
    467 	acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
    468 	if (acnt < 0)
    469 		return (acnt);
    470 
    471 	if (KREAD(kd, a_zombproc, &p)) {
    472 		_kvm_err(kd, kd->program, "cannot read zombproc");
    473 		return (-1);
    474 	}
    475 	zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt);
    476 	if (zcnt < 0)
    477 		zcnt = 0;
    478 
    479 	return (acnt + zcnt);
    480 }
    481 
    482 struct kinfo_proc *
    483 kvm_getprocs(kd, op, arg, cnt)
    484 	kvm_t *kd;
    485 	int op, arg;
    486 	int *cnt;
    487 {
    488 	size_t size;
    489 	int mib[4], st, nprocs;
    490 
    491 	if (kd->procbase != 0) {
    492 		free((void *)kd->procbase);
    493 		/*
    494 		 * Clear this pointer in case this call fails.  Otherwise,
    495 		 * kvm_close() will free it again.
    496 		 */
    497 		kd->procbase = 0;
    498 	}
    499 	if (ISALIVE(kd)) {
    500 		size = 0;
    501 		mib[0] = CTL_KERN;
    502 		mib[1] = KERN_PROC;
    503 		mib[2] = op;
    504 		mib[3] = arg;
    505 		st = sysctl(mib, 4, NULL, &size, NULL, 0);
    506 		if (st == -1) {
    507 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
    508 			return (0);
    509 		}
    510 		kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
    511 		if (kd->procbase == 0)
    512 			return (0);
    513 		st = sysctl(mib, 4, kd->procbase, &size, NULL, 0);
    514 		if (st == -1) {
    515 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
    516 			return (0);
    517 		}
    518 		if (size % sizeof(struct kinfo_proc) != 0) {
    519 			_kvm_err(kd, kd->program,
    520 				"proc size mismatch (%d total, %d chunks)",
    521 				size, sizeof(struct kinfo_proc));
    522 			return (0);
    523 		}
    524 		nprocs = size / sizeof(struct kinfo_proc);
    525 	} else {
    526 		struct nlist nl[4], *p;
    527 
    528 		nl[0].n_name = "_nprocs";
    529 		nl[1].n_name = "_allproc";
    530 		nl[2].n_name = "_zombproc";
    531 		nl[3].n_name = 0;
    532 
    533 		if (kvm_nlist(kd, nl) != 0) {
    534 			for (p = nl; p->n_type != 0; ++p)
    535 				;
    536 			_kvm_err(kd, kd->program,
    537 				 "%s: no such symbol", p->n_name);
    538 			return (0);
    539 		}
    540 		if (KREAD(kd, nl[0].n_value, &nprocs)) {
    541 			_kvm_err(kd, kd->program, "can't read nprocs");
    542 			return (0);
    543 		}
    544 		size = nprocs * sizeof(struct kinfo_proc);
    545 		kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
    546 		if (kd->procbase == 0)
    547 			return (0);
    548 
    549 		nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
    550 				      nl[2].n_value, nprocs);
    551 #ifdef notdef
    552 		size = nprocs * sizeof(struct kinfo_proc);
    553 		(void)realloc(kd->procbase, size);
    554 #endif
    555 	}
    556 	*cnt = nprocs;
    557 	return (kd->procbase);
    558 }
    559 
    560 void
    561 _kvm_freeprocs(kd)
    562 	kvm_t *kd;
    563 {
    564 	if (kd->procbase) {
    565 		free(kd->procbase);
    566 		kd->procbase = 0;
    567 	}
    568 }
    569 
    570 void *
    571 _kvm_realloc(kd, p, n)
    572 	kvm_t *kd;
    573 	void *p;
    574 	size_t n;
    575 {
    576 	void *np = (void *)realloc(p, n);
    577 
    578 	if (np == 0)
    579 		_kvm_err(kd, kd->program, "out of memory");
    580 	return (np);
    581 }
    582 
    583 #ifndef MAX
    584 #define MAX(a, b) ((a) > (b) ? (a) : (b))
    585 #endif
    586 
    587 /*
    588  * Read in an argument vector from the user address space of process p.
    589  * addr if the user-space base address of narg null-terminated contiguous
    590  * strings.  This is used to read in both the command arguments and
    591  * environment strings.  Read at most maxcnt characters of strings.
    592  */
    593 static char **
    594 kvm_argv(kd, p, addr, narg, maxcnt)
    595 	kvm_t *kd;
    596 	const struct proc *p;
    597 	register u_long addr;
    598 	register int narg;
    599 	register int maxcnt;
    600 {
    601 	register char *np, *cp, *ep, *ap;
    602 	register u_long oaddr = -1;
    603 	register int len, cc;
    604 	register char **argv;
    605 
    606 	/*
    607 	 * Check that there aren't an unreasonable number of agruments,
    608 	 * and that the address is in user space.
    609 	 */
    610 	if (narg > ARG_MAX || addr < kd->min_uva || addr >= kd->max_uva)
    611 		return (0);
    612 
    613 	if (kd->argv == 0) {
    614 		/*
    615 		 * Try to avoid reallocs.
    616 		 */
    617 		kd->argc = MAX(narg + 1, 32);
    618 		kd->argv = (char **)_kvm_malloc(kd, kd->argc *
    619 						sizeof(*kd->argv));
    620 		if (kd->argv == 0)
    621 			return (0);
    622 	} else if (narg + 1 > kd->argc) {
    623 		kd->argc = MAX(2 * kd->argc, narg + 1);
    624 		kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc *
    625 						sizeof(*kd->argv));
    626 		if (kd->argv == 0)
    627 			return (0);
    628 	}
    629 	if (kd->argspc == 0) {
    630 		kd->argspc = (char *)_kvm_malloc(kd, kd->nbpg);
    631 		if (kd->argspc == 0)
    632 			return (0);
    633 		kd->arglen = kd->nbpg;
    634 	}
    635 	if (kd->argbuf == 0) {
    636 		kd->argbuf = (char *)_kvm_malloc(kd, kd->nbpg);
    637 		if (kd->argbuf == 0)
    638 			return (0);
    639 	}
    640 	cc = sizeof(char *) * narg;
    641 	if (kvm_uread(kd, p, addr, (char *)kd->argv, cc) != cc)
    642 		return (0);
    643 	ap = np = kd->argspc;
    644 	argv = kd->argv;
    645 	len = 0;
    646 	/*
    647 	 * Loop over pages, filling in the argument vector.
    648 	 */
    649 	while (argv < kd->argv + narg && *argv != 0) {
    650 		addr = (u_long)*argv & ~(kd->nbpg - 1);
    651 		if (addr != oaddr) {
    652 			if (kvm_uread(kd, p, addr, kd->argbuf, kd->nbpg) !=
    653 			    kd->nbpg)
    654 				return (0);
    655 			oaddr = addr;
    656 		}
    657 		addr = (u_long)*argv & (kd->nbpg - 1);
    658 		cp = kd->argbuf + addr;
    659 		cc = kd->nbpg - addr;
    660 		if (maxcnt > 0 && cc > maxcnt - len)
    661 			cc = maxcnt - len;;
    662 		ep = memchr(cp, '\0', cc);
    663 		if (ep != 0)
    664 			cc = ep - cp + 1;
    665 		if (len + cc > kd->arglen) {
    666 			register int off;
    667 			register char **pp;
    668 			register char *op = kd->argspc;
    669 
    670 			kd->arglen *= 2;
    671 			kd->argspc = (char *)_kvm_realloc(kd, kd->argspc,
    672 							  kd->arglen);
    673 			if (kd->argspc == 0)
    674 				return (0);
    675 			/*
    676 			 * Adjust argv pointers in case realloc moved
    677 			 * the string space.
    678 			 */
    679 			off = kd->argspc - op;
    680 			for (pp = kd->argv; pp < argv; pp++)
    681 				*pp += off;
    682 			ap += off;
    683 			np += off;
    684 		}
    685 		memcpy(np, cp, cc);
    686 		np += cc;
    687 		len += cc;
    688 		if (ep != 0) {
    689 			*argv++ = ap;
    690 			ap = np;
    691 		} else
    692 			*argv += cc;
    693 		if (maxcnt > 0 && len >= maxcnt) {
    694 			/*
    695 			 * We're stopping prematurely.  Terminate the
    696 			 * current string.
    697 			 */
    698 			if (ep == 0) {
    699 				*np = '\0';
    700 				*argv++ = ap;
    701 			}
    702 			break;
    703 		}
    704 	}
    705 	/* Make sure argv is terminated. */
    706 	*argv = 0;
    707 	return (kd->argv);
    708 }
    709 
    710 static void
    711 ps_str_a(p, addr, n)
    712 	struct ps_strings *p;
    713 	u_long *addr;
    714 	int *n;
    715 {
    716 	*addr = (u_long)p->ps_argvstr;
    717 	*n = p->ps_nargvstr;
    718 }
    719 
    720 static void
    721 ps_str_e(p, addr, n)
    722 	struct ps_strings *p;
    723 	u_long *addr;
    724 	int *n;
    725 {
    726 	*addr = (u_long)p->ps_envstr;
    727 	*n = p->ps_nenvstr;
    728 }
    729 
    730 /*
    731  * Determine if the proc indicated by p is still active.
    732  * This test is not 100% foolproof in theory, but chances of
    733  * being wrong are very low.
    734  */
    735 static int
    736 proc_verify(kd, kernp, p)
    737 	kvm_t *kd;
    738 	u_long kernp;
    739 	const struct proc *p;
    740 {
    741 	struct proc kernproc;
    742 
    743 	/*
    744 	 * Just read in the whole proc.  It's not that big relative
    745 	 * to the cost of the read system call.
    746 	 */
    747 	if (kvm_read(kd, kernp, (char *)&kernproc, sizeof(kernproc)) !=
    748 	    sizeof(kernproc))
    749 		return (0);
    750 	return (p->p_pid == kernproc.p_pid &&
    751 		(kernproc.p_stat != SZOMB || p->p_stat == SZOMB));
    752 }
    753 
    754 static char **
    755 kvm_doargv(kd, kp, nchr, info)
    756 	kvm_t *kd;
    757 	const struct kinfo_proc *kp;
    758 	int nchr;
    759 	void (*info)(struct ps_strings *, u_long *, int *);
    760 {
    761 	register const struct proc *p = &kp->kp_proc;
    762 	register char **ap;
    763 	u_long addr;
    764 	int cnt;
    765 	struct ps_strings arginfo;
    766 
    767 	/*
    768 	 * Pointers are stored at the top of the user stack.
    769 	 */
    770 	if (p->p_stat == SZOMB)
    771 		return (0);
    772 	cnt = kvm_uread(kd, p, kd->usrstack - sizeof(arginfo),
    773 	    (char *)&arginfo, sizeof(arginfo));
    774 	if (cnt != sizeof(arginfo))
    775 		return (0);
    776 
    777 	(*info)(&arginfo, &addr, &cnt);
    778 	if (cnt == 0)
    779 		return (0);
    780 	ap = kvm_argv(kd, p, addr, cnt, nchr);
    781 	/*
    782 	 * For live kernels, make sure this process didn't go away.
    783 	 */
    784 	if (ap != 0 && ISALIVE(kd) &&
    785 	    !proc_verify(kd, (u_long)kp->kp_eproc.e_paddr, p))
    786 		ap = 0;
    787 	return (ap);
    788 }
    789 
    790 /*
    791  * Get the command args.  This code is now machine independent.
    792  */
    793 char **
    794 kvm_getargv(kd, kp, nchr)
    795 	kvm_t *kd;
    796 	const struct kinfo_proc *kp;
    797 	int nchr;
    798 {
    799 	return (kvm_doargv(kd, kp, nchr, ps_str_a));
    800 }
    801 
    802 char **
    803 kvm_getenvv(kd, kp, nchr)
    804 	kvm_t *kd;
    805 	const struct kinfo_proc *kp;
    806 	int nchr;
    807 {
    808 	return (kvm_doargv(kd, kp, nchr, ps_str_e));
    809 }
    810 
    811 /*
    812  * Read from user space.  The user context is given by p.
    813  */
    814 ssize_t
    815 kvm_uread(kd, p, uva, buf, len)
    816 	kvm_t *kd;
    817 	register const struct proc *p;
    818 	register u_long uva;
    819 	register char *buf;
    820 	register size_t len;
    821 {
    822 	register char *cp;
    823 
    824 	cp = buf;
    825 	while (len > 0) {
    826 		register int cc;
    827 		register char *dp;
    828 		u_long cnt;
    829 
    830 		dp = _kvm_uread(kd, p, uva, &cnt);
    831 		if (dp == 0) {
    832 			_kvm_err(kd, 0, "invalid address (%x)", uva);
    833 			return (0);
    834 		}
    835 		cc = MIN(cnt, len);
    836 		bcopy(dp, cp, cc);
    837 
    838 		cp += cc;
    839 		uva += cc;
    840 		len -= cc;
    841 	}
    842 	return (ssize_t)(cp - buf);
    843 }
    844