xref: /freebsd/lib/libkvm/kvm_proc.c (revision b787589098ac28fb0612b60dc4da43a730356b33)
158f0484fSRodney W. Grimes /*-
258f0484fSRodney W. Grimes  * Copyright (c) 1989, 1992, 1993
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * This code is derived from software developed by the Computer Systems
658f0484fSRodney W. Grimes  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
758f0484fSRodney W. Grimes  * BG 91-66 and contributed to Berkeley.
858f0484fSRodney W. Grimes  *
958f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1058f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1158f0484fSRodney W. Grimes  * are met:
1258f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1358f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1458f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1558f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1658f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1758f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1858f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1958f0484fSRodney W. Grimes  *	This product includes software developed by the University of
2058f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
2158f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
2258f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2358f0484fSRodney W. Grimes  *    without specific prior written permission.
2458f0484fSRodney W. Grimes  *
2558f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2658f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2758f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2858f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2958f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3058f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3158f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3258f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3358f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3458f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3558f0484fSRodney W. Grimes  * SUCH DAMAGE.
36b9df5231SPoul-Henning Kamp  *
37b9df5231SPoul-Henning Kamp  * $FreeBSD$
3858f0484fSRodney W. Grimes  */
3958f0484fSRodney W. Grimes 
4058f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
4158f0484fSRodney W. Grimes static char sccsid[] = "@(#)kvm_proc.c	8.3 (Berkeley) 9/23/93";
4258f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
4358f0484fSRodney W. Grimes 
4458f0484fSRodney W. Grimes /*
4558f0484fSRodney W. Grimes  * Proc traversal interface for kvm.  ps and w are (probably) the exclusive
4658f0484fSRodney W. Grimes  * users of this code, so we've factored it out into a separate module.
4758f0484fSRodney W. Grimes  * Thus, we keep this grunge out of the other kvm applications (i.e.,
4858f0484fSRodney W. Grimes  * most other applications are interested only in open/close/read/nlist).
4958f0484fSRodney W. Grimes  */
5058f0484fSRodney W. Grimes 
5158f0484fSRodney W. Grimes #include <sys/param.h>
5258f0484fSRodney W. Grimes #include <sys/user.h>
5358f0484fSRodney W. Grimes #include <sys/proc.h>
5458f0484fSRodney W. Grimes #include <sys/exec.h>
5558f0484fSRodney W. Grimes #include <sys/stat.h>
5658f0484fSRodney W. Grimes #include <sys/ioctl.h>
5758f0484fSRodney W. Grimes #include <sys/tty.h>
58338c7541SDavid Greenman #include <sys/file.h>
5951295a4dSJordan K. Hubbard #include <stdio.h>
6051295a4dSJordan K. Hubbard #include <stdlib.h>
6158f0484fSRodney W. Grimes #include <unistd.h>
6258f0484fSRodney W. Grimes #include <nlist.h>
6358f0484fSRodney W. Grimes #include <kvm.h>
6458f0484fSRodney W. Grimes 
6558f0484fSRodney W. Grimes #include <vm/vm.h>
6658f0484fSRodney W. Grimes #include <vm/vm_param.h>
6758f0484fSRodney W. Grimes #include <vm/swap_pager.h>
6858f0484fSRodney W. Grimes 
6958f0484fSRodney W. Grimes #include <sys/sysctl.h>
7058f0484fSRodney W. Grimes 
7158f0484fSRodney W. Grimes #include <limits.h>
7277721f53SPeter Wemm #include <memory.h>
7358f0484fSRodney W. Grimes #include <paths.h>
7458f0484fSRodney W. Grimes 
7558f0484fSRodney W. Grimes #include "kvm_private.h"
7658f0484fSRodney W. Grimes 
7751295a4dSJordan K. Hubbard #if used
7858f0484fSRodney W. Grimes static char *
7958f0484fSRodney W. Grimes kvm_readswap(kd, p, va, cnt)
8058f0484fSRodney W. Grimes 	kvm_t *kd;
8158f0484fSRodney W. Grimes 	const struct proc *p;
8258f0484fSRodney W. Grimes 	u_long va;
8358f0484fSRodney W. Grimes 	u_long *cnt;
8458f0484fSRodney W. Grimes {
8521d54b07SRodney W. Grimes #ifdef __FreeBSD__
8621d54b07SRodney W. Grimes 	/* XXX Stubbed out, our vm system is differnet */
8721d54b07SRodney W. Grimes 	_kvm_err(kd, kd->program, "kvm_readswap not implemented");
8821d54b07SRodney W. Grimes 	return(0);
8921d54b07SRodney W. Grimes #endif	/* __FreeBSD__ */
9058f0484fSRodney W. Grimes }
9151295a4dSJordan K. Hubbard #endif
9258f0484fSRodney W. Grimes 
9358f0484fSRodney W. Grimes #define KREAD(kd, addr, obj) \
9458f0484fSRodney W. Grimes 	(kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj))
9558f0484fSRodney W. Grimes 
9658f0484fSRodney W. Grimes /*
9758f0484fSRodney W. Grimes  * Read proc's from memory file into buffer bp, which has space to hold
9858f0484fSRodney W. Grimes  * at most maxcnt procs.
9958f0484fSRodney W. Grimes  */
10058f0484fSRodney W. Grimes static int
10158f0484fSRodney W. Grimes kvm_proclist(kd, what, arg, p, bp, maxcnt)
10258f0484fSRodney W. Grimes 	kvm_t *kd;
10358f0484fSRodney W. Grimes 	int what, arg;
10458f0484fSRodney W. Grimes 	struct proc *p;
10558f0484fSRodney W. Grimes 	struct kinfo_proc *bp;
10658f0484fSRodney W. Grimes 	int maxcnt;
10758f0484fSRodney W. Grimes {
10858f0484fSRodney W. Grimes 	register int cnt = 0;
10958f0484fSRodney W. Grimes 	struct eproc eproc;
11058f0484fSRodney W. Grimes 	struct pgrp pgrp;
11158f0484fSRodney W. Grimes 	struct session sess;
11258f0484fSRodney W. Grimes 	struct tty tty;
11358f0484fSRodney W. Grimes 	struct proc proc;
114a58930d8STor Egge 	struct proc pproc;
11558f0484fSRodney W. Grimes 
11637e4fbc4SJeffrey Hsu 	for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) {
11758f0484fSRodney W. Grimes 		if (KREAD(kd, (u_long)p, &proc)) {
11858f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "can't read proc at %x", p);
11958f0484fSRodney W. Grimes 			return (-1);
12058f0484fSRodney W. Grimes 		}
12158f0484fSRodney W. Grimes 		if (KREAD(kd, (u_long)proc.p_cred, &eproc.e_pcred) == 0)
12251295a4dSJordan K. Hubbard 			(void)(KREAD(kd, (u_long)eproc.e_pcred.pc_ucred,
12351295a4dSJordan K. Hubbard 			             &eproc.e_ucred));
12458f0484fSRodney W. Grimes 
12558f0484fSRodney W. Grimes 		switch(what) {
12658f0484fSRodney W. Grimes 
12758f0484fSRodney W. Grimes 		case KERN_PROC_PID:
12858f0484fSRodney W. Grimes 			if (proc.p_pid != (pid_t)arg)
12958f0484fSRodney W. Grimes 				continue;
13058f0484fSRodney W. Grimes 			break;
13158f0484fSRodney W. Grimes 
13258f0484fSRodney W. Grimes 		case KERN_PROC_UID:
13358f0484fSRodney W. Grimes 			if (eproc.e_ucred.cr_uid != (uid_t)arg)
13458f0484fSRodney W. Grimes 				continue;
13558f0484fSRodney W. Grimes 			break;
13658f0484fSRodney W. Grimes 
13758f0484fSRodney W. Grimes 		case KERN_PROC_RUID:
13858f0484fSRodney W. Grimes 			if (eproc.e_pcred.p_ruid != (uid_t)arg)
13958f0484fSRodney W. Grimes 				continue;
14058f0484fSRodney W. Grimes 			break;
14158f0484fSRodney W. Grimes 		}
14258f0484fSRodney W. Grimes 		/*
14358f0484fSRodney W. Grimes 		 * We're going to add another proc to the set.  If this
14458f0484fSRodney W. Grimes 		 * will overflow the buffer, assume the reason is because
14558f0484fSRodney W. Grimes 		 * nprocs (or the proc list) is corrupt and declare an error.
14658f0484fSRodney W. Grimes 		 */
14758f0484fSRodney W. Grimes 		if (cnt >= maxcnt) {
14858f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "nprocs corrupt");
14958f0484fSRodney W. Grimes 			return (-1);
15058f0484fSRodney W. Grimes 		}
15158f0484fSRodney W. Grimes 		/*
15258f0484fSRodney W. Grimes 		 * gather eproc
15358f0484fSRodney W. Grimes 		 */
15458f0484fSRodney W. Grimes 		eproc.e_paddr = p;
15558f0484fSRodney W. Grimes 		if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
15658f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "can't read pgrp at %x",
15758f0484fSRodney W. Grimes 				 proc.p_pgrp);
15858f0484fSRodney W. Grimes 			return (-1);
15958f0484fSRodney W. Grimes 		}
160a58930d8STor Egge 		if (proc.p_oppid)
161a58930d8STor Egge 		  eproc.e_ppid = proc.p_oppid;
162a58930d8STor Egge 		else if (proc.p_pptr) {
163a58930d8STor Egge 		  if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) {
164a58930d8STor Egge 			_kvm_err(kd, kd->program, "can't read pproc at %x",
165a58930d8STor Egge 				 proc.p_pptr);
166a58930d8STor Egge 			return (-1);
167a58930d8STor Egge 		  }
168a58930d8STor Egge 		  eproc.e_ppid = pproc.p_pid;
169a58930d8STor Egge 		} else
170a58930d8STor Egge 		  eproc.e_ppid = 0;
17158f0484fSRodney W. Grimes 		eproc.e_sess = pgrp.pg_session;
17258f0484fSRodney W. Grimes 		eproc.e_pgid = pgrp.pg_id;
17358f0484fSRodney W. Grimes 		eproc.e_jobc = pgrp.pg_jobc;
17458f0484fSRodney W. Grimes 		if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
17558f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "can't read session at %x",
17658f0484fSRodney W. Grimes 				pgrp.pg_session);
17758f0484fSRodney W. Grimes 			return (-1);
17858f0484fSRodney W. Grimes 		}
179b8321444SPeter Wemm 		(void)memcpy(eproc.e_login, sess.s_login,
180b8321444SPeter Wemm 						sizeof(eproc.e_login));
18158f0484fSRodney W. Grimes 		if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) {
18258f0484fSRodney W. Grimes 			if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
18358f0484fSRodney W. Grimes 				_kvm_err(kd, kd->program,
18458f0484fSRodney W. Grimes 					 "can't read tty at %x", sess.s_ttyp);
18558f0484fSRodney W. Grimes 				return (-1);
18658f0484fSRodney W. Grimes 			}
18758f0484fSRodney W. Grimes 			eproc.e_tdev = tty.t_dev;
18858f0484fSRodney W. Grimes 			eproc.e_tsess = tty.t_session;
18958f0484fSRodney W. Grimes 			if (tty.t_pgrp != NULL) {
19058f0484fSRodney W. Grimes 				if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
19158f0484fSRodney W. Grimes 					_kvm_err(kd, kd->program,
19258f0484fSRodney W. Grimes 						 "can't read tpgrp at &x",
19358f0484fSRodney W. Grimes 						tty.t_pgrp);
19458f0484fSRodney W. Grimes 					return (-1);
19558f0484fSRodney W. Grimes 				}
19658f0484fSRodney W. Grimes 				eproc.e_tpgid = pgrp.pg_id;
19758f0484fSRodney W. Grimes 			} else
19858f0484fSRodney W. Grimes 				eproc.e_tpgid = -1;
19958f0484fSRodney W. Grimes 		} else
20058f0484fSRodney W. Grimes 			eproc.e_tdev = NODEV;
20158f0484fSRodney W. Grimes 		eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0;
20258f0484fSRodney W. Grimes 		if (sess.s_leader == p)
20358f0484fSRodney W. Grimes 			eproc.e_flag |= EPROC_SLEADER;
20458f0484fSRodney W. Grimes 		if (proc.p_wmesg)
20558f0484fSRodney W. Grimes 			(void)kvm_read(kd, (u_long)proc.p_wmesg,
20658f0484fSRodney W. Grimes 			    eproc.e_wmesg, WMESGLEN);
20758f0484fSRodney W. Grimes 
20858f0484fSRodney W. Grimes #ifdef sparc
20958f0484fSRodney W. Grimes 		(void)kvm_read(kd, (u_long)&proc.p_vmspace->vm_rssize,
21058f0484fSRodney W. Grimes 		    (char *)&eproc.e_vm.vm_rssize,
21158f0484fSRodney W. Grimes 		    sizeof(eproc.e_vm.vm_rssize));
21258f0484fSRodney W. Grimes 		(void)kvm_read(kd, (u_long)&proc.p_vmspace->vm_tsize,
21358f0484fSRodney W. Grimes 		    (char *)&eproc.e_vm.vm_tsize,
21458f0484fSRodney W. Grimes 		    3 * sizeof(eproc.e_vm.vm_rssize));	/* XXX */
21558f0484fSRodney W. Grimes #else
21658f0484fSRodney W. Grimes 		(void)kvm_read(kd, (u_long)proc.p_vmspace,
21758f0484fSRodney W. Grimes 		    (char *)&eproc.e_vm, sizeof(eproc.e_vm));
21858f0484fSRodney W. Grimes #endif
21958f0484fSRodney W. Grimes 		eproc.e_xsize = eproc.e_xrssize = 0;
22058f0484fSRodney W. Grimes 		eproc.e_xccount = eproc.e_xswrss = 0;
22158f0484fSRodney W. Grimes 
22258f0484fSRodney W. Grimes 		switch (what) {
22358f0484fSRodney W. Grimes 
22458f0484fSRodney W. Grimes 		case KERN_PROC_PGRP:
22558f0484fSRodney W. Grimes 			if (eproc.e_pgid != (pid_t)arg)
22658f0484fSRodney W. Grimes 				continue;
22758f0484fSRodney W. Grimes 			break;
22858f0484fSRodney W. Grimes 
22958f0484fSRodney W. Grimes 		case KERN_PROC_TTY:
23058f0484fSRodney W. Grimes 			if ((proc.p_flag & P_CONTROLT) == 0 ||
23158f0484fSRodney W. Grimes 			     eproc.e_tdev != (dev_t)arg)
23258f0484fSRodney W. Grimes 				continue;
23358f0484fSRodney W. Grimes 			break;
23458f0484fSRodney W. Grimes 		}
23558f0484fSRodney W. Grimes 		bcopy(&proc, &bp->kp_proc, sizeof(proc));
23658f0484fSRodney W. Grimes 		bcopy(&eproc, &bp->kp_eproc, sizeof(eproc));
23758f0484fSRodney W. Grimes 		++bp;
23858f0484fSRodney W. Grimes 		++cnt;
23958f0484fSRodney W. Grimes 	}
24058f0484fSRodney W. Grimes 	return (cnt);
24158f0484fSRodney W. Grimes }
24258f0484fSRodney W. Grimes 
24358f0484fSRodney W. Grimes /*
24458f0484fSRodney W. Grimes  * Build proc info array by reading in proc list from a crash dump.
24558f0484fSRodney W. Grimes  * Return number of procs read.  maxcnt is the max we will read.
24658f0484fSRodney W. Grimes  */
24758f0484fSRodney W. Grimes static int
24858f0484fSRodney W. Grimes kvm_deadprocs(kd, what, arg, a_allproc, a_zombproc, maxcnt)
24958f0484fSRodney W. Grimes 	kvm_t *kd;
25058f0484fSRodney W. Grimes 	int what, arg;
25158f0484fSRodney W. Grimes 	u_long a_allproc;
25258f0484fSRodney W. Grimes 	u_long a_zombproc;
25358f0484fSRodney W. Grimes 	int maxcnt;
25458f0484fSRodney W. Grimes {
25558f0484fSRodney W. Grimes 	register struct kinfo_proc *bp = kd->procbase;
25658f0484fSRodney W. Grimes 	register int acnt, zcnt;
25758f0484fSRodney W. Grimes 	struct proc *p;
25858f0484fSRodney W. Grimes 
25958f0484fSRodney W. Grimes 	if (KREAD(kd, a_allproc, &p)) {
26058f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "cannot read allproc");
26158f0484fSRodney W. Grimes 		return (-1);
26258f0484fSRodney W. Grimes 	}
26358f0484fSRodney W. Grimes 	acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
26458f0484fSRodney W. Grimes 	if (acnt < 0)
26558f0484fSRodney W. Grimes 		return (acnt);
26658f0484fSRodney W. Grimes 
26758f0484fSRodney W. Grimes 	if (KREAD(kd, a_zombproc, &p)) {
26858f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "cannot read zombproc");
26958f0484fSRodney W. Grimes 		return (-1);
27058f0484fSRodney W. Grimes 	}
27158f0484fSRodney W. Grimes 	zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt);
27258f0484fSRodney W. Grimes 	if (zcnt < 0)
27358f0484fSRodney W. Grimes 		zcnt = 0;
27458f0484fSRodney W. Grimes 
27558f0484fSRodney W. Grimes 	return (acnt + zcnt);
27658f0484fSRodney W. Grimes }
27758f0484fSRodney W. Grimes 
27858f0484fSRodney W. Grimes struct kinfo_proc *
27958f0484fSRodney W. Grimes kvm_getprocs(kd, op, arg, cnt)
28058f0484fSRodney W. Grimes 	kvm_t *kd;
28158f0484fSRodney W. Grimes 	int op, arg;
28258f0484fSRodney W. Grimes 	int *cnt;
28358f0484fSRodney W. Grimes {
284c2ac238cSDoug Rabson 	int mib[4], st, nprocs;
285c2ac238cSDoug Rabson 	size_t size;
28658f0484fSRodney W. Grimes 
28758f0484fSRodney W. Grimes 	if (kd->procbase != 0) {
28858f0484fSRodney W. Grimes 		free((void *)kd->procbase);
28958f0484fSRodney W. Grimes 		/*
29058f0484fSRodney W. Grimes 		 * Clear this pointer in case this call fails.  Otherwise,
29158f0484fSRodney W. Grimes 		 * kvm_close() will free it again.
29258f0484fSRodney W. Grimes 		 */
29358f0484fSRodney W. Grimes 		kd->procbase = 0;
29458f0484fSRodney W. Grimes 	}
29558f0484fSRodney W. Grimes 	if (ISALIVE(kd)) {
29658f0484fSRodney W. Grimes 		size = 0;
29758f0484fSRodney W. Grimes 		mib[0] = CTL_KERN;
29858f0484fSRodney W. Grimes 		mib[1] = KERN_PROC;
29958f0484fSRodney W. Grimes 		mib[2] = op;
30058f0484fSRodney W. Grimes 		mib[3] = arg;
30144ffb5f5SPoul-Henning Kamp 		st = sysctl(mib, op == KERN_PROC_ALL ? 3 : 4, NULL, &size, NULL, 0);
30258f0484fSRodney W. Grimes 		if (st == -1) {
30358f0484fSRodney W. Grimes 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
30458f0484fSRodney W. Grimes 			return (0);
30558f0484fSRodney W. Grimes 		}
306b2d3d0f0SDag-Erling Smørgrav 		do {
307b2d3d0f0SDag-Erling Smørgrav 			size += size / 10;
308b2d3d0f0SDag-Erling Smørgrav 			kd->procbase = (struct kinfo_proc *)
309b2d3d0f0SDag-Erling Smørgrav 			    _kvm_realloc(kd, kd->procbase, size);
31058f0484fSRodney W. Grimes 			if (kd->procbase == 0)
31158f0484fSRodney W. Grimes 				return (0);
312b2d3d0f0SDag-Erling Smørgrav 			st = sysctl(mib, op == KERN_PROC_ALL ? 3 : 4,
313b2d3d0f0SDag-Erling Smørgrav 			    kd->procbase, &size, NULL, 0);
314b2d3d0f0SDag-Erling Smørgrav 		} while (st == -1 && errno == ENOMEM);
31558f0484fSRodney W. Grimes 		if (st == -1) {
31658f0484fSRodney W. Grimes 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
31758f0484fSRodney W. Grimes 			return (0);
31858f0484fSRodney W. Grimes 		}
31958f0484fSRodney W. Grimes 		if (size % sizeof(struct kinfo_proc) != 0) {
32058f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program,
32158f0484fSRodney W. Grimes 				"proc size mismatch (%d total, %d chunks)",
32258f0484fSRodney W. Grimes 				size, sizeof(struct kinfo_proc));
32358f0484fSRodney W. Grimes 			return (0);
32458f0484fSRodney W. Grimes 		}
32558f0484fSRodney W. Grimes 		nprocs = size / sizeof(struct kinfo_proc);
32658f0484fSRodney W. Grimes 	} else {
32758f0484fSRodney W. Grimes 		struct nlist nl[4], *p;
32858f0484fSRodney W. Grimes 
32958f0484fSRodney W. Grimes 		nl[0].n_name = "_nprocs";
33058f0484fSRodney W. Grimes 		nl[1].n_name = "_allproc";
33158f0484fSRodney W. Grimes 		nl[2].n_name = "_zombproc";
33258f0484fSRodney W. Grimes 		nl[3].n_name = 0;
33358f0484fSRodney W. Grimes 
33458f0484fSRodney W. Grimes 		if (kvm_nlist(kd, nl) != 0) {
33558f0484fSRodney W. Grimes 			for (p = nl; p->n_type != 0; ++p)
33658f0484fSRodney W. Grimes 				;
33758f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program,
33858f0484fSRodney W. Grimes 				 "%s: no such symbol", p->n_name);
33958f0484fSRodney W. Grimes 			return (0);
34058f0484fSRodney W. Grimes 		}
34158f0484fSRodney W. Grimes 		if (KREAD(kd, nl[0].n_value, &nprocs)) {
34258f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "can't read nprocs");
34358f0484fSRodney W. Grimes 			return (0);
34458f0484fSRodney W. Grimes 		}
34558f0484fSRodney W. Grimes 		size = nprocs * sizeof(struct kinfo_proc);
34658f0484fSRodney W. Grimes 		kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
34758f0484fSRodney W. Grimes 		if (kd->procbase == 0)
34858f0484fSRodney W. Grimes 			return (0);
34958f0484fSRodney W. Grimes 
35058f0484fSRodney W. Grimes 		nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
35158f0484fSRodney W. Grimes 				      nl[2].n_value, nprocs);
35258f0484fSRodney W. Grimes #ifdef notdef
35358f0484fSRodney W. Grimes 		size = nprocs * sizeof(struct kinfo_proc);
35458f0484fSRodney W. Grimes 		(void)realloc(kd->procbase, size);
35558f0484fSRodney W. Grimes #endif
35658f0484fSRodney W. Grimes 	}
35758f0484fSRodney W. Grimes 	*cnt = nprocs;
35858f0484fSRodney W. Grimes 	return (kd->procbase);
35958f0484fSRodney W. Grimes }
36058f0484fSRodney W. Grimes 
36158f0484fSRodney W. Grimes void
36258f0484fSRodney W. Grimes _kvm_freeprocs(kd)
36358f0484fSRodney W. Grimes 	kvm_t *kd;
36458f0484fSRodney W. Grimes {
36558f0484fSRodney W. Grimes 	if (kd->procbase) {
36658f0484fSRodney W. Grimes 		free(kd->procbase);
36758f0484fSRodney W. Grimes 		kd->procbase = 0;
36858f0484fSRodney W. Grimes 	}
36958f0484fSRodney W. Grimes }
37058f0484fSRodney W. Grimes 
37158f0484fSRodney W. Grimes void *
37258f0484fSRodney W. Grimes _kvm_realloc(kd, p, n)
37358f0484fSRodney W. Grimes 	kvm_t *kd;
37458f0484fSRodney W. Grimes 	void *p;
37558f0484fSRodney W. Grimes 	size_t n;
37658f0484fSRodney W. Grimes {
37758f0484fSRodney W. Grimes 	void *np = (void *)realloc(p, n);
37858f0484fSRodney W. Grimes 
379e8420087SWarner Losh 	if (np == 0) {
380e8420087SWarner Losh 		free(p);
38158f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "out of memory");
382e8420087SWarner Losh 	}
38358f0484fSRodney W. Grimes 	return (np);
38458f0484fSRodney W. Grimes }
38558f0484fSRodney W. Grimes 
38658f0484fSRodney W. Grimes #ifndef MAX
38758f0484fSRodney W. Grimes #define MAX(a, b) ((a) > (b) ? (a) : (b))
38858f0484fSRodney W. Grimes #endif
38958f0484fSRodney W. Grimes 
39058f0484fSRodney W. Grimes /*
39158f0484fSRodney W. Grimes  * Read in an argument vector from the user address space of process p.
39277721f53SPeter Wemm  * addr if the user-space base address of narg null-terminated contiguous
39358f0484fSRodney W. Grimes  * strings.  This is used to read in both the command arguments and
39458f0484fSRodney W. Grimes  * environment strings.  Read at most maxcnt characters of strings.
39558f0484fSRodney W. Grimes  */
39658f0484fSRodney W. Grimes static char **
39758f0484fSRodney W. Grimes kvm_argv(kd, p, addr, narg, maxcnt)
39858f0484fSRodney W. Grimes 	kvm_t *kd;
39977721f53SPeter Wemm 	const struct proc *p;
40058f0484fSRodney W. Grimes 	register u_long addr;
40158f0484fSRodney W. Grimes 	register int narg;
40258f0484fSRodney W. Grimes 	register int maxcnt;
40358f0484fSRodney W. Grimes {
40477721f53SPeter Wemm 	register char *np, *cp, *ep, *ap;
40577721f53SPeter Wemm 	register u_long oaddr = -1;
40658f0484fSRodney W. Grimes 	register int len, cc;
40758f0484fSRodney W. Grimes 	register char **argv;
40858f0484fSRodney W. Grimes 
40958f0484fSRodney W. Grimes 	/*
41058f0484fSRodney W. Grimes 	 * Check that there aren't an unreasonable number of agruments,
41158f0484fSRodney W. Grimes 	 * and that the address is in user space.
41258f0484fSRodney W. Grimes 	 */
4136b492ae4SPeter Wemm 	if (narg > 512 || addr < VM_MIN_ADDRESS || addr >= VM_MAXUSER_ADDRESS)
41458f0484fSRodney W. Grimes 		return (0);
41558f0484fSRodney W. Grimes 
4166b492ae4SPeter Wemm 	/*
4176b492ae4SPeter Wemm 	 * kd->argv : work space for fetching the strings from the target
4186b492ae4SPeter Wemm 	 *            process's space, and is converted for returning to caller
4196b492ae4SPeter Wemm 	 */
42058f0484fSRodney W. Grimes 	if (kd->argv == 0) {
42158f0484fSRodney W. Grimes 		/*
42258f0484fSRodney W. Grimes 		 * Try to avoid reallocs.
42358f0484fSRodney W. Grimes 		 */
42458f0484fSRodney W. Grimes 		kd->argc = MAX(narg + 1, 32);
42558f0484fSRodney W. Grimes 		kd->argv = (char **)_kvm_malloc(kd, kd->argc *
42658f0484fSRodney W. Grimes 						sizeof(*kd->argv));
42758f0484fSRodney W. Grimes 		if (kd->argv == 0)
42858f0484fSRodney W. Grimes 			return (0);
42958f0484fSRodney W. Grimes 	} else if (narg + 1 > kd->argc) {
43058f0484fSRodney W. Grimes 		kd->argc = MAX(2 * kd->argc, narg + 1);
43158f0484fSRodney W. Grimes 		kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc *
43258f0484fSRodney W. Grimes 						sizeof(*kd->argv));
43358f0484fSRodney W. Grimes 		if (kd->argv == 0)
43458f0484fSRodney W. Grimes 			return (0);
43558f0484fSRodney W. Grimes 	}
4366b492ae4SPeter Wemm 	/*
4376b492ae4SPeter Wemm 	 * kd->argspc : returned to user, this is where the kd->argv
4386b492ae4SPeter Wemm 	 *              arrays are left pointing to the collected strings.
4396b492ae4SPeter Wemm 	 */
44058f0484fSRodney W. Grimes 	if (kd->argspc == 0) {
4412572133eSPoul-Henning Kamp 		kd->argspc = (char *)_kvm_malloc(kd, PAGE_SIZE);
44258f0484fSRodney W. Grimes 		if (kd->argspc == 0)
44358f0484fSRodney W. Grimes 			return (0);
4442572133eSPoul-Henning Kamp 		kd->arglen = PAGE_SIZE;
44558f0484fSRodney W. Grimes 	}
4466b492ae4SPeter Wemm 	/*
4476b492ae4SPeter Wemm 	 * kd->argbuf : used to pull in pages from the target process.
4486b492ae4SPeter Wemm 	 *              the strings are copied out of here.
4496b492ae4SPeter Wemm 	 */
45077721f53SPeter Wemm 	if (kd->argbuf == 0) {
4512572133eSPoul-Henning Kamp 		kd->argbuf = (char *)_kvm_malloc(kd, PAGE_SIZE);
45277721f53SPeter Wemm 		if (kd->argbuf == 0)
45377721f53SPeter Wemm 			return (0);
45477721f53SPeter Wemm 	}
4556b492ae4SPeter Wemm 
4566b492ae4SPeter Wemm 	/* Pull in the target process'es argv vector */
45777721f53SPeter Wemm 	cc = sizeof(char *) * narg;
45877721f53SPeter Wemm 	if (kvm_uread(kd, p, addr, (char *)kd->argv, cc) != cc)
45977721f53SPeter Wemm 		return (0);
4606b492ae4SPeter Wemm 	/*
4616b492ae4SPeter Wemm 	 * ap : saved start address of string we're working on in kd->argspc
4626b492ae4SPeter Wemm 	 * np : pointer to next place to write in kd->argspc
4636b492ae4SPeter Wemm 	 * len: length of data in kd->argspc
4646b492ae4SPeter Wemm 	 * argv: pointer to the argv vector that we are hunting around the
4656b492ae4SPeter Wemm 	 *       target process space for, and converting to addresses in
4666b492ae4SPeter Wemm 	 *       our address space (kd->argspc).
4676b492ae4SPeter Wemm 	 */
46877721f53SPeter Wemm 	ap = np = kd->argspc;
46958f0484fSRodney W. Grimes 	argv = kd->argv;
47058f0484fSRodney W. Grimes 	len = 0;
47158f0484fSRodney W. Grimes 	/*
47258f0484fSRodney W. Grimes 	 * Loop over pages, filling in the argument vector.
4736b492ae4SPeter Wemm 	 * Note that the argv strings could be pointing *anywhere* in
4746b492ae4SPeter Wemm 	 * the user address space and are no longer contiguous.
4756b492ae4SPeter Wemm 	 * Note that *argv is modified when we are going to fetch a string
4766b492ae4SPeter Wemm 	 * that crosses a page boundary.  We copy the next part of the string
4776b492ae4SPeter Wemm 	 * into to "np" and eventually convert the pointer.
47858f0484fSRodney W. Grimes 	 */
47977721f53SPeter Wemm 	while (argv < kd->argv + narg && *argv != 0) {
4806b492ae4SPeter Wemm 
4816b492ae4SPeter Wemm 		/* get the address that the current argv string is on */
4822572133eSPoul-Henning Kamp 		addr = (u_long)*argv & ~(PAGE_SIZE - 1);
4836b492ae4SPeter Wemm 
4846b492ae4SPeter Wemm 		/* is it the same page as the last one? */
48577721f53SPeter Wemm 		if (addr != oaddr) {
4862572133eSPoul-Henning Kamp 			if (kvm_uread(kd, p, addr, kd->argbuf, PAGE_SIZE) !=
4872572133eSPoul-Henning Kamp 			    PAGE_SIZE)
48877721f53SPeter Wemm 				return (0);
48977721f53SPeter Wemm 			oaddr = addr;
49077721f53SPeter Wemm 		}
4916b492ae4SPeter Wemm 
4926b492ae4SPeter Wemm 		/* offset within the page... kd->argbuf */
4932572133eSPoul-Henning Kamp 		addr = (u_long)*argv & (PAGE_SIZE - 1);
4946b492ae4SPeter Wemm 
4956b492ae4SPeter Wemm 		/* cp = start of string, cc = count of chars in this chunk */
49677721f53SPeter Wemm 		cp = kd->argbuf + addr;
4972572133eSPoul-Henning Kamp 		cc = PAGE_SIZE - addr;
4986b492ae4SPeter Wemm 
4996b492ae4SPeter Wemm 		/* dont get more than asked for by user process */
50058f0484fSRodney W. Grimes 		if (maxcnt > 0 && cc > maxcnt - len)
5016b492ae4SPeter Wemm 			cc = maxcnt - len;
5026b492ae4SPeter Wemm 
5036b492ae4SPeter Wemm 		/* pointer to end of string if we found it in this page */
50477721f53SPeter Wemm 		ep = memchr(cp, '\0', cc);
50577721f53SPeter Wemm 		if (ep != 0)
50677721f53SPeter Wemm 			cc = ep - cp + 1;
5076b492ae4SPeter Wemm 		/*
5086b492ae4SPeter Wemm 		 * at this point, cc is the count of the chars that we are
5096b492ae4SPeter Wemm 		 * going to retrieve this time. we may or may not have found
5106b492ae4SPeter Wemm 		 * the end of it.  (ep points to the null if the end is known)
5116b492ae4SPeter Wemm 		 */
5126b492ae4SPeter Wemm 
5136b492ae4SPeter Wemm 		/* will we exceed the malloc/realloced buffer? */
51458f0484fSRodney W. Grimes 		if (len + cc > kd->arglen) {
51558f0484fSRodney W. Grimes 			register int off;
51658f0484fSRodney W. Grimes 			register char **pp;
51758f0484fSRodney W. Grimes 			register char *op = kd->argspc;
51858f0484fSRodney W. Grimes 
51958f0484fSRodney W. Grimes 			kd->arglen *= 2;
52058f0484fSRodney W. Grimes 			kd->argspc = (char *)_kvm_realloc(kd, kd->argspc,
52158f0484fSRodney W. Grimes 							  kd->arglen);
52258f0484fSRodney W. Grimes 			if (kd->argspc == 0)
52358f0484fSRodney W. Grimes 				return (0);
52458f0484fSRodney W. Grimes 			/*
52558f0484fSRodney W. Grimes 			 * Adjust argv pointers in case realloc moved
52658f0484fSRodney W. Grimes 			 * the string space.
52758f0484fSRodney W. Grimes 			 */
52858f0484fSRodney W. Grimes 			off = kd->argspc - op;
52977721f53SPeter Wemm 			for (pp = kd->argv; pp < argv; pp++)
53058f0484fSRodney W. Grimes 				*pp += off;
53177721f53SPeter Wemm 			ap += off;
53277721f53SPeter Wemm 			np += off;
53358f0484fSRodney W. Grimes 		}
5346b492ae4SPeter Wemm 		/* np = where to put the next part of the string in kd->argspc*/
5356b492ae4SPeter Wemm 		/* np is kinda redundant.. could use "kd->argspc + len" */
53677721f53SPeter Wemm 		memcpy(np, cp, cc);
5376b492ae4SPeter Wemm 		np += cc;	/* inc counters */
53858f0484fSRodney W. Grimes 		len += cc;
5396b492ae4SPeter Wemm 
5406b492ae4SPeter Wemm 		/*
5416b492ae4SPeter Wemm 		 * if end of string found, set the *argv pointer to the
5426b492ae4SPeter Wemm 		 * saved beginning of string, and advance. argv points to
5436b492ae4SPeter Wemm 		 * somewhere in kd->argv..  This is initially relative
5446b492ae4SPeter Wemm 		 * to the target process, but when we close it off, we set
5456b492ae4SPeter Wemm 		 * it to point in our address space.
5466b492ae4SPeter Wemm 		 */
54777721f53SPeter Wemm 		if (ep != 0) {
54877721f53SPeter Wemm 			*argv++ = ap;
54977721f53SPeter Wemm 			ap = np;
5506b492ae4SPeter Wemm 		} else {
5516b492ae4SPeter Wemm 			/* update the address relative to the target process */
55277721f53SPeter Wemm 			*argv += cc;
5536b492ae4SPeter Wemm 		}
5546b492ae4SPeter Wemm 
55558f0484fSRodney W. Grimes 		if (maxcnt > 0 && len >= maxcnt) {
55658f0484fSRodney W. Grimes 			/*
55758f0484fSRodney W. Grimes 			 * We're stopping prematurely.  Terminate the
55877721f53SPeter Wemm 			 * current string.
55958f0484fSRodney W. Grimes 			 */
56077721f53SPeter Wemm 			if (ep == 0) {
56177721f53SPeter Wemm 				*np = '\0';
56277721f53SPeter Wemm 				*argv++ = ap;
56377721f53SPeter Wemm 			}
56477721f53SPeter Wemm 			break;
56577721f53SPeter Wemm 		}
56677721f53SPeter Wemm 	}
56777721f53SPeter Wemm 	/* Make sure argv is terminated. */
56877721f53SPeter Wemm 	*argv = 0;
56958f0484fSRodney W. Grimes 	return (kd->argv);
57058f0484fSRodney W. Grimes }
57158f0484fSRodney W. Grimes 
57258f0484fSRodney W. Grimes static void
57358f0484fSRodney W. Grimes ps_str_a(p, addr, n)
57458f0484fSRodney W. Grimes 	struct ps_strings *p;
57558f0484fSRodney W. Grimes 	u_long *addr;
57658f0484fSRodney W. Grimes 	int *n;
57758f0484fSRodney W. Grimes {
57858f0484fSRodney W. Grimes 	*addr = (u_long)p->ps_argvstr;
57958f0484fSRodney W. Grimes 	*n = p->ps_nargvstr;
58058f0484fSRodney W. Grimes }
58158f0484fSRodney W. Grimes 
58258f0484fSRodney W. Grimes static void
58358f0484fSRodney W. Grimes ps_str_e(p, addr, n)
58458f0484fSRodney W. Grimes 	struct ps_strings *p;
58558f0484fSRodney W. Grimes 	u_long *addr;
58658f0484fSRodney W. Grimes 	int *n;
58758f0484fSRodney W. Grimes {
58858f0484fSRodney W. Grimes 	*addr = (u_long)p->ps_envstr;
58958f0484fSRodney W. Grimes 	*n = p->ps_nenvstr;
59058f0484fSRodney W. Grimes }
59158f0484fSRodney W. Grimes 
59258f0484fSRodney W. Grimes /*
59358f0484fSRodney W. Grimes  * Determine if the proc indicated by p is still active.
59458f0484fSRodney W. Grimes  * This test is not 100% foolproof in theory, but chances of
59558f0484fSRodney W. Grimes  * being wrong are very low.
59658f0484fSRodney W. Grimes  */
59758f0484fSRodney W. Grimes static int
59858f0484fSRodney W. Grimes proc_verify(kd, kernp, p)
59958f0484fSRodney W. Grimes 	kvm_t *kd;
60058f0484fSRodney W. Grimes 	u_long kernp;
60158f0484fSRodney W. Grimes 	const struct proc *p;
60258f0484fSRodney W. Grimes {
60335e6b695SPoul-Henning Kamp 	struct kinfo_proc kp;
60460160c5eSBruce Evans 	int mib[4];
605c2ac238cSDoug Rabson 	size_t len;
60658f0484fSRodney W. Grimes 
60735e6b695SPoul-Henning Kamp 	mib[0] = CTL_KERN;
60835e6b695SPoul-Henning Kamp 	mib[1] = KERN_PROC;
60935e6b695SPoul-Henning Kamp 	mib[2] = KERN_PROC_PID;
61035e6b695SPoul-Henning Kamp 	mib[3] = p->p_pid;
61160160c5eSBruce Evans 	len = sizeof(kp);
61260160c5eSBruce Evans 	if (sysctl(mib, 4, &kp, &len, NULL, 0) == -1)
61358f0484fSRodney W. Grimes 		return (0);
61435e6b695SPoul-Henning Kamp 	return (p->p_pid == kp.kp_proc.p_pid &&
61535e6b695SPoul-Henning Kamp 	    (kp.kp_proc.p_stat != SZOMB || p->p_stat == SZOMB));
61658f0484fSRodney W. Grimes }
61758f0484fSRodney W. Grimes 
61858f0484fSRodney W. Grimes static char **
61958f0484fSRodney W. Grimes kvm_doargv(kd, kp, nchr, info)
62058f0484fSRodney W. Grimes 	kvm_t *kd;
62158f0484fSRodney W. Grimes 	const struct kinfo_proc *kp;
62258f0484fSRodney W. Grimes 	int nchr;
62377721f53SPeter Wemm 	void (*info)(struct ps_strings *, u_long *, int *);
62458f0484fSRodney W. Grimes {
62558f0484fSRodney W. Grimes 	register const struct proc *p = &kp->kp_proc;
62658f0484fSRodney W. Grimes 	register char **ap;
62758f0484fSRodney W. Grimes 	u_long addr;
62858f0484fSRodney W. Grimes 	int cnt;
62960160c5eSBruce Evans 	static struct ps_strings arginfo;
63060160c5eSBruce Evans 	static u_long ps_strings;
6317350dd84SPeter Wemm 	size_t len;
6327350dd84SPeter Wemm 
63335e6b695SPoul-Henning Kamp 	if (ps_strings == NULL) {
63460160c5eSBruce Evans 		len = sizeof(ps_strings);
63560160c5eSBruce Evans 		if (sysctlbyname("kern.ps_strings", &ps_strings, &len, NULL,
63660160c5eSBruce Evans 		    0) == -1)
6377350dd84SPeter Wemm 			ps_strings = PS_STRINGS;
63835e6b695SPoul-Henning Kamp 	}
63958f0484fSRodney W. Grimes 
64058f0484fSRodney W. Grimes 	/*
64158f0484fSRodney W. Grimes 	 * Pointers are stored at the top of the user stack.
64258f0484fSRodney W. Grimes 	 */
64358f0484fSRodney W. Grimes 	if (p->p_stat == SZOMB ||
6447350dd84SPeter Wemm 	    kvm_uread(kd, p, ps_strings, (char *)&arginfo,
64577721f53SPeter Wemm 		      sizeof(arginfo)) != sizeof(arginfo))
64658f0484fSRodney W. Grimes 		return (0);
64758f0484fSRodney W. Grimes 
64858f0484fSRodney W. Grimes 	(*info)(&arginfo, &addr, &cnt);
64977721f53SPeter Wemm 	if (cnt == 0)
65077721f53SPeter Wemm 		return (0);
65158f0484fSRodney W. Grimes 	ap = kvm_argv(kd, p, addr, cnt, nchr);
65258f0484fSRodney W. Grimes 	/*
65358f0484fSRodney W. Grimes 	 * For live kernels, make sure this process didn't go away.
65458f0484fSRodney W. Grimes 	 */
65558f0484fSRodney W. Grimes 	if (ap != 0 && ISALIVE(kd) &&
65658f0484fSRodney W. Grimes 	    !proc_verify(kd, (u_long)kp->kp_eproc.e_paddr, p))
65758f0484fSRodney W. Grimes 		ap = 0;
65858f0484fSRodney W. Grimes 	return (ap);
65958f0484fSRodney W. Grimes }
66058f0484fSRodney W. Grimes 
66158f0484fSRodney W. Grimes /*
66258f0484fSRodney W. Grimes  * Get the command args.  This code is now machine independent.
66358f0484fSRodney W. Grimes  */
66458f0484fSRodney W. Grimes char **
66558f0484fSRodney W. Grimes kvm_getargv(kd, kp, nchr)
66658f0484fSRodney W. Grimes 	kvm_t *kd;
66758f0484fSRodney W. Grimes 	const struct kinfo_proc *kp;
66858f0484fSRodney W. Grimes 	int nchr;
66958f0484fSRodney W. Grimes {
670b9df5231SPoul-Henning Kamp 	int oid[4];
671b7875890SDavid E. O'Brien 	int i;
672b7875890SDavid E. O'Brien 	size_t bufsz;
673b9df5231SPoul-Henning Kamp 	static int buflen;
674b9df5231SPoul-Henning Kamp 	static char *buf, *p;
675b9df5231SPoul-Henning Kamp 	static char **bufp;
676b9df5231SPoul-Henning Kamp 	static int argc;
677b9df5231SPoul-Henning Kamp 
678c4a7cdb3SPeter Wemm 	if (!ISALIVE(kd)) {
679c4a7cdb3SPeter Wemm 		_kvm_err(kd, kd->program,
680c4a7cdb3SPeter Wemm 		    "cannot read user space from dead kernel");
681c4a7cdb3SPeter Wemm 		return (0);
682c4a7cdb3SPeter Wemm 	}
683c4a7cdb3SPeter Wemm 
684b9df5231SPoul-Henning Kamp 	if (!buflen) {
685b7875890SDavid E. O'Brien 		bufsz = sizeof(buflen);
686b9df5231SPoul-Henning Kamp 		i = sysctlbyname("kern.ps_arg_cache_limit",
687b7875890SDavid E. O'Brien 		    &buflen, &bufsz, NULL, 0);
688b9df5231SPoul-Henning Kamp 		if (i == -1) {
689b7875890SDavid E. O'Brien 			buflen = 0;
690b9df5231SPoul-Henning Kamp 		} else {
691b9df5231SPoul-Henning Kamp 			buf = malloc(buflen);
692b9df5231SPoul-Henning Kamp 			if (buf == NULL)
693b9df5231SPoul-Henning Kamp 				buflen = 0;
694b9df5231SPoul-Henning Kamp 			argc = 32;
695b9df5231SPoul-Henning Kamp 			bufp = malloc(sizeof(char *) * argc);
696b9df5231SPoul-Henning Kamp 		}
697b9df5231SPoul-Henning Kamp 	}
698b9df5231SPoul-Henning Kamp 	if (buf != NULL) {
699b9df5231SPoul-Henning Kamp 		oid[0] = CTL_KERN;
700b9df5231SPoul-Henning Kamp 		oid[1] = KERN_PROC;
701b9df5231SPoul-Henning Kamp 		oid[2] = KERN_PROC_ARGS;
702b9df5231SPoul-Henning Kamp 		oid[3] = kp->kp_proc.p_pid;
703b7875890SDavid E. O'Brien 		bufsz = buflen;
704b7875890SDavid E. O'Brien 		i = sysctl(oid, 4, buf, &bufsz, 0, 0);
705b7875890SDavid E. O'Brien 		if (i == 0 && bufsz > 0) {
706b9df5231SPoul-Henning Kamp 			i = 0;
707b9df5231SPoul-Henning Kamp 			p = buf;
708b9df5231SPoul-Henning Kamp 			do {
709b9df5231SPoul-Henning Kamp 				bufp[i++] = p;
710b9df5231SPoul-Henning Kamp 				p += strlen(p) + 1;
711b9df5231SPoul-Henning Kamp 				if (i >= argc) {
712b9df5231SPoul-Henning Kamp 					argc += argc;
713b9df5231SPoul-Henning Kamp 					bufp = realloc(bufp,
714b9df5231SPoul-Henning Kamp 					    sizeof(char *) * argc);
715b9df5231SPoul-Henning Kamp 				}
716b7875890SDavid E. O'Brien 			} while (p < buf + bufsz);
717b9df5231SPoul-Henning Kamp 			bufp[i++] = 0;
718b9df5231SPoul-Henning Kamp 			return (bufp);
719b9df5231SPoul-Henning Kamp 		}
720b9df5231SPoul-Henning Kamp 	}
721b9df5231SPoul-Henning Kamp 	if (kp->kp_proc.p_flag & P_SYSTEM)
722b9df5231SPoul-Henning Kamp 		return (NULL);
72358f0484fSRodney W. Grimes 	return (kvm_doargv(kd, kp, nchr, ps_str_a));
72458f0484fSRodney W. Grimes }
72558f0484fSRodney W. Grimes 
72658f0484fSRodney W. Grimes char **
72758f0484fSRodney W. Grimes kvm_getenvv(kd, kp, nchr)
72858f0484fSRodney W. Grimes 	kvm_t *kd;
72958f0484fSRodney W. Grimes 	const struct kinfo_proc *kp;
73058f0484fSRodney W. Grimes 	int nchr;
73158f0484fSRodney W. Grimes {
73258f0484fSRodney W. Grimes 	return (kvm_doargv(kd, kp, nchr, ps_str_e));
73358f0484fSRodney W. Grimes }
73458f0484fSRodney W. Grimes 
73558f0484fSRodney W. Grimes /*
73658f0484fSRodney W. Grimes  * Read from user space.  The user context is given by p.
73758f0484fSRodney W. Grimes  */
73858f0484fSRodney W. Grimes ssize_t
73958f0484fSRodney W. Grimes kvm_uread(kd, p, uva, buf, len)
74058f0484fSRodney W. Grimes 	kvm_t *kd;
74151295a4dSJordan K. Hubbard 	register const struct proc *p;
74258f0484fSRodney W. Grimes 	register u_long uva;
74358f0484fSRodney W. Grimes 	register char *buf;
74458f0484fSRodney W. Grimes 	register size_t len;
74558f0484fSRodney W. Grimes {
74658f0484fSRodney W. Grimes 	register char *cp;
747338c7541SDavid Greenman 	char procfile[MAXPATHLEN];
748338c7541SDavid Greenman 	ssize_t amount;
749338c7541SDavid Greenman 	int fd;
75058f0484fSRodney W. Grimes 
7517350dd84SPeter Wemm 	if (!ISALIVE(kd)) {
752d7fb4b13SBruce Evans 		_kvm_err(kd, kd->program,
753d7fb4b13SBruce Evans 		    "cannot read user space from dead kernel");
7547350dd84SPeter Wemm 		return (0);
7557350dd84SPeter Wemm 	}
7567350dd84SPeter Wemm 
757338c7541SDavid Greenman 	sprintf(procfile, "/proc/%d/mem", p->p_pid);
758338c7541SDavid Greenman 	fd = open(procfile, O_RDONLY, 0);
759338c7541SDavid Greenman 	if (fd < 0) {
760338c7541SDavid Greenman 		_kvm_err(kd, kd->program, "cannot open %s", procfile);
761338c7541SDavid Greenman 		close(fd);
76258f0484fSRodney W. Grimes 		return (0);
76358f0484fSRodney W. Grimes 	}
764338c7541SDavid Greenman 
765d7fb4b13SBruce Evans 	cp = buf;
766338c7541SDavid Greenman 	while (len > 0) {
767d7fb4b13SBruce Evans 		errno = 0;
768ec4f2251SJoerg Wunsch 		if (lseek(fd, (off_t)uva, 0) == -1 && errno != 0) {
769d7fb4b13SBruce Evans 			_kvm_err(kd, kd->program, "invalid address (%x) in %s",
770d7fb4b13SBruce Evans 			    uva, procfile);
77158f0484fSRodney W. Grimes 			break;
77258f0484fSRodney W. Grimes 		}
773567127faSDavid Greenman 		amount = read(fd, cp, len);
774338c7541SDavid Greenman 		if (amount < 0) {
775d7fb4b13SBruce Evans 			_kvm_syserr(kd, kd->program, "error reading %s",
776d7fb4b13SBruce Evans 			    procfile);
777d7fb4b13SBruce Evans 			break;
778d7fb4b13SBruce Evans 		}
779d7fb4b13SBruce Evans 		if (amount == 0) {
780d7fb4b13SBruce Evans 			_kvm_err(kd, kd->program, "EOF reading %s", procfile);
781338c7541SDavid Greenman 			break;
782338c7541SDavid Greenman 		}
783338c7541SDavid Greenman 		cp += amount;
784338c7541SDavid Greenman 		uva += amount;
785338c7541SDavid Greenman 		len -= amount;
786338c7541SDavid Greenman 	}
787338c7541SDavid Greenman 
788338c7541SDavid Greenman 	close(fd);
789d7fb4b13SBruce Evans 	return ((ssize_t)(cp - buf));
79058f0484fSRodney W. Grimes }
791