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