xref: /freebsd/lib/libkvm/kvm_proc.c (revision d339edcf66f366e4a6a97e163b434f185e8013d0)
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>
5282633431SMark Murray #include <sys/lock.h>
5382633431SMark Murray #include <sys/mutex.h>
5458f0484fSRodney W. Grimes #include <sys/user.h>
5558f0484fSRodney W. Grimes #include <sys/proc.h>
5658f0484fSRodney W. Grimes #include <sys/exec.h>
5758f0484fSRodney W. Grimes #include <sys/stat.h>
5858f0484fSRodney W. Grimes #include <sys/ioctl.h>
5958f0484fSRodney W. Grimes #include <sys/tty.h>
60338c7541SDavid Greenman #include <sys/file.h>
6151295a4dSJordan K. Hubbard #include <stdio.h>
6251295a4dSJordan K. Hubbard #include <stdlib.h>
6358f0484fSRodney W. Grimes #include <unistd.h>
6458f0484fSRodney W. Grimes #include <nlist.h>
6558f0484fSRodney W. Grimes #include <kvm.h>
6658f0484fSRodney W. Grimes 
6758f0484fSRodney W. Grimes #include <vm/vm.h>
6858f0484fSRodney W. Grimes #include <vm/vm_param.h>
6958f0484fSRodney W. Grimes #include <vm/swap_pager.h>
7058f0484fSRodney W. Grimes 
7158f0484fSRodney W. Grimes #include <sys/sysctl.h>
7258f0484fSRodney W. Grimes 
7358f0484fSRodney W. Grimes #include <limits.h>
7477721f53SPeter Wemm #include <memory.h>
7558f0484fSRodney W. Grimes #include <paths.h>
7658f0484fSRodney W. Grimes 
7758f0484fSRodney W. Grimes #include "kvm_private.h"
7858f0484fSRodney W. Grimes 
7951295a4dSJordan K. Hubbard #if used
8058f0484fSRodney W. Grimes static char *
8158f0484fSRodney W. Grimes kvm_readswap(kd, p, va, cnt)
8258f0484fSRodney W. Grimes 	kvm_t *kd;
8358f0484fSRodney W. Grimes 	const struct proc *p;
8458f0484fSRodney W. Grimes 	u_long va;
8558f0484fSRodney W. Grimes 	u_long *cnt;
8658f0484fSRodney W. Grimes {
8721d54b07SRodney W. Grimes #ifdef __FreeBSD__
8821d54b07SRodney W. Grimes 	/* XXX Stubbed out, our vm system is differnet */
8921d54b07SRodney W. Grimes 	_kvm_err(kd, kd->program, "kvm_readswap not implemented");
9021d54b07SRodney W. Grimes 	return(0);
9121d54b07SRodney W. Grimes #endif	/* __FreeBSD__ */
9258f0484fSRodney W. Grimes }
9351295a4dSJordan K. Hubbard #endif
9458f0484fSRodney W. Grimes 
9558f0484fSRodney W. Grimes #define KREAD(kd, addr, obj) \
9658f0484fSRodney W. Grimes 	(kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj))
9758f0484fSRodney W. Grimes 
9858f0484fSRodney W. Grimes /*
9958f0484fSRodney W. Grimes  * Read proc's from memory file into buffer bp, which has space to hold
10058f0484fSRodney W. Grimes  * at most maxcnt procs.
10158f0484fSRodney W. Grimes  */
10258f0484fSRodney W. Grimes static int
10358f0484fSRodney W. Grimes kvm_proclist(kd, what, arg, p, bp, maxcnt)
10458f0484fSRodney W. Grimes 	kvm_t *kd;
10558f0484fSRodney W. Grimes 	int what, arg;
10658f0484fSRodney W. Grimes 	struct proc *p;
10758f0484fSRodney W. Grimes 	struct kinfo_proc *bp;
10858f0484fSRodney W. Grimes 	int maxcnt;
10958f0484fSRodney W. Grimes {
11058f0484fSRodney W. Grimes 	register int cnt = 0;
1111f7d2501SKirk McKusick 	struct kinfo_proc kinfo_proc, *kp;
11258f0484fSRodney W. Grimes 	struct pgrp pgrp;
11358f0484fSRodney W. Grimes 	struct session sess;
11458f0484fSRodney W. Grimes 	struct tty tty;
1151f7d2501SKirk McKusick 	struct vmspace vmspace;
1161f7d2501SKirk McKusick 	struct procsig procsig;
1171f7d2501SKirk McKusick 	struct pcred pcred;
1181f7d2501SKirk McKusick 	struct pstats pstats;
1191f7d2501SKirk McKusick 	struct ucred ucred;
12058f0484fSRodney W. Grimes 	struct proc proc;
121a58930d8STor Egge 	struct proc pproc;
12258f0484fSRodney W. Grimes 
1231f7d2501SKirk McKusick 	kp = &kinfo_proc;
1241f7d2501SKirk McKusick 	kp->ki_structsize = sizeof(kinfo_proc);
12542680b3aSBen Smithurst 	for (; cnt < maxcnt && p != NULL; p = LIST_NEXT(&proc, p_list)) {
12658f0484fSRodney W. Grimes 		if (KREAD(kd, (u_long)p, &proc)) {
12758f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "can't read proc at %x", p);
12858f0484fSRodney W. Grimes 			return (-1);
12958f0484fSRodney W. Grimes 		}
1301f7d2501SKirk McKusick 		if (KREAD(kd, (u_long)proc.p_cred, &pcred) == 0) {
1311f7d2501SKirk McKusick 			kp->ki_ruid = pcred.p_ruid;
1321f7d2501SKirk McKusick 			kp->ki_svuid = pcred.p_svuid;
1331f7d2501SKirk McKusick 			kp->ki_rgid = pcred.p_rgid;
1341f7d2501SKirk McKusick 			kp->ki_svgid = pcred.p_svgid;
1351f7d2501SKirk McKusick 			(void)(KREAD(kd, (u_long)pcred.pc_ucred, &ucred));
1361f7d2501SKirk McKusick 			kp->ki_ngroups = ucred.cr_ngroups;
1371f7d2501SKirk McKusick 			bcopy(ucred.cr_groups, kp->ki_groups,
1381f7d2501SKirk McKusick 			    NGROUPS * sizeof(gid_t));
1391f7d2501SKirk McKusick 			kp->ki_uid = ucred.cr_uid;
1401f7d2501SKirk McKusick 		}
14158f0484fSRodney W. Grimes 
14258f0484fSRodney W. Grimes 		switch(what) {
14358f0484fSRodney W. Grimes 
14458f0484fSRodney W. Grimes 		case KERN_PROC_PID:
14558f0484fSRodney W. Grimes 			if (proc.p_pid != (pid_t)arg)
14658f0484fSRodney W. Grimes 				continue;
14758f0484fSRodney W. Grimes 			break;
14858f0484fSRodney W. Grimes 
14958f0484fSRodney W. Grimes 		case KERN_PROC_UID:
1501f7d2501SKirk McKusick 			if (kp->ki_uid != (uid_t)arg)
15158f0484fSRodney W. Grimes 				continue;
15258f0484fSRodney W. Grimes 			break;
15358f0484fSRodney W. Grimes 
15458f0484fSRodney W. Grimes 		case KERN_PROC_RUID:
1551f7d2501SKirk McKusick 			if (kp->ki_ruid != (uid_t)arg)
15658f0484fSRodney W. Grimes 				continue;
15758f0484fSRodney W. Grimes 			break;
15858f0484fSRodney W. Grimes 		}
15958f0484fSRodney W. Grimes 		/*
16058f0484fSRodney W. Grimes 		 * We're going to add another proc to the set.  If this
16158f0484fSRodney W. Grimes 		 * will overflow the buffer, assume the reason is because
16258f0484fSRodney W. Grimes 		 * nprocs (or the proc list) is corrupt and declare an error.
16358f0484fSRodney W. Grimes 		 */
16458f0484fSRodney W. Grimes 		if (cnt >= maxcnt) {
16558f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "nprocs corrupt");
16658f0484fSRodney W. Grimes 			return (-1);
16758f0484fSRodney W. Grimes 		}
16858f0484fSRodney W. Grimes 		/*
1691f7d2501SKirk McKusick 		 * gather kinfo_proc
17058f0484fSRodney W. Grimes 		 */
1711f7d2501SKirk McKusick 		kp->ki_paddr = p;
1721f7d2501SKirk McKusick 		kp->ki_addr = proc.p_addr;
1731f7d2501SKirk McKusick 		kp->ki_args = proc.p_args;
1741f7d2501SKirk McKusick 		kp->ki_tracep = proc.p_tracep;
1751f7d2501SKirk McKusick 		kp->ki_textvp = proc.p_textvp;
1761f7d2501SKirk McKusick 		kp->ki_fd = proc.p_fd;
1771f7d2501SKirk McKusick 		kp->ki_vmspace = proc.p_vmspace;
1781f7d2501SKirk McKusick 		if (proc.p_procsig != NULL) {
1791f7d2501SKirk McKusick 			if (KREAD(kd, (u_long)proc.p_procsig, &procsig)) {
1801f7d2501SKirk McKusick 				_kvm_err(kd, kd->program,
1811f7d2501SKirk McKusick 				    "can't read procsig at %x", proc.p_procsig);
1821f7d2501SKirk McKusick 				return (-1);
1831f7d2501SKirk McKusick 			}
1841f7d2501SKirk McKusick 			kp->ki_sigignore = procsig.ps_sigignore;
1851f7d2501SKirk McKusick 			kp->ki_sigcatch = procsig.ps_sigcatch;
1861f7d2501SKirk McKusick 		}
1876d3e7b9bSJohn Baldwin 		if ((proc.p_sflag & PS_INMEM) && proc.p_stats != NULL) {
1881f7d2501SKirk McKusick 			if (KREAD(kd, (u_long)proc.p_stats, &pstats)) {
1891f7d2501SKirk McKusick 				_kvm_err(kd, kd->program,
1901f7d2501SKirk McKusick 				    "can't read stats at %x", proc.p_stats);
1911f7d2501SKirk McKusick 				return (-1);
1921f7d2501SKirk McKusick 			}
1931f7d2501SKirk McKusick 			kp->ki_start = pstats.p_start;
1941f7d2501SKirk McKusick 			kp->ki_rusage = pstats.p_ru;
1951f7d2501SKirk McKusick 			kp->ki_childtime.tv_sec = pstats.p_cru.ru_utime.tv_sec +
1961f7d2501SKirk McKusick 			    pstats.p_cru.ru_stime.tv_sec;
1971f7d2501SKirk McKusick 			kp->ki_childtime.tv_usec =
1981f7d2501SKirk McKusick 			    pstats.p_cru.ru_utime.tv_usec +
1991f7d2501SKirk McKusick 			    pstats.p_cru.ru_stime.tv_usec;
2001f7d2501SKirk McKusick 		}
20158f0484fSRodney W. Grimes 		if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
20258f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "can't read pgrp at %x",
20358f0484fSRodney W. Grimes 				 proc.p_pgrp);
20458f0484fSRodney W. Grimes 			return (-1);
20558f0484fSRodney W. Grimes 		}
206a58930d8STor Egge 		if (proc.p_oppid)
2071f7d2501SKirk McKusick 			kp->ki_ppid = proc.p_oppid;
208a58930d8STor Egge 		else if (proc.p_pptr) {
209a58930d8STor Egge 			if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) {
2101f7d2501SKirk McKusick 				_kvm_err(kd, kd->program,
2111f7d2501SKirk McKusick 				    "can't read pproc at %x", proc.p_pptr);
212a58930d8STor Egge 				return (-1);
213a58930d8STor Egge 			}
2141f7d2501SKirk McKusick 			kp->ki_ppid = pproc.p_pid;
215a58930d8STor Egge 		} else
2161f7d2501SKirk McKusick 			kp->ki_ppid = 0;
2171f7d2501SKirk McKusick 		kp->ki_pgid = pgrp.pg_id;
2181f7d2501SKirk McKusick 		kp->ki_jobc = pgrp.pg_jobc;
21958f0484fSRodney W. Grimes 		if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
22058f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "can't read session at %x",
22158f0484fSRodney W. Grimes 				pgrp.pg_session);
22258f0484fSRodney W. Grimes 			return (-1);
22358f0484fSRodney W. Grimes 		}
2241f7d2501SKirk McKusick 		kp->ki_sid = sess.s_sid;
2251f7d2501SKirk McKusick 		(void)memcpy(kp->ki_login, sess.s_login,
2261f7d2501SKirk McKusick 						sizeof(kp->ki_login));
2271f7d2501SKirk McKusick 		kp->ki_kiflag = sess.s_ttyvp ? KI_CTTY : 0;
2281f7d2501SKirk McKusick 		if (sess.s_leader == p)
2291f7d2501SKirk McKusick 			kp->ki_kiflag |= KI_SLEADER;
23058f0484fSRodney W. Grimes 		if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) {
23158f0484fSRodney W. Grimes 			if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
23258f0484fSRodney W. Grimes 				_kvm_err(kd, kd->program,
23358f0484fSRodney W. Grimes 					 "can't read tty at %x", sess.s_ttyp);
23458f0484fSRodney W. Grimes 				return (-1);
23558f0484fSRodney W. Grimes 			}
2361f7d2501SKirk McKusick 			kp->ki_tdev = tty.t_dev;
23758f0484fSRodney W. Grimes 			if (tty.t_pgrp != NULL) {
23858f0484fSRodney W. Grimes 				if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
23958f0484fSRodney W. Grimes 					_kvm_err(kd, kd->program,
24058f0484fSRodney W. Grimes 						 "can't read tpgrp at &x",
24158f0484fSRodney W. Grimes 						tty.t_pgrp);
24258f0484fSRodney W. Grimes 					return (-1);
24358f0484fSRodney W. Grimes 				}
2441f7d2501SKirk McKusick 				kp->ki_tpgid = pgrp.pg_id;
24558f0484fSRodney W. Grimes 			} else
2461f7d2501SKirk McKusick 				kp->ki_tpgid = -1;
2471f7d2501SKirk McKusick 			if (tty.t_session != NULL) {
2481f7d2501SKirk McKusick 				if (KREAD(kd, (u_long)tty.t_session, &sess)) {
2491f7d2501SKirk McKusick 					_kvm_err(kd, kd->program,
2501f7d2501SKirk McKusick 					    "can't read session at %x",
2511f7d2501SKirk McKusick 					    tty.t_session);
2521f7d2501SKirk McKusick 					return (-1);
2531f7d2501SKirk McKusick 				}
2541f7d2501SKirk McKusick 				kp->ki_tsid = sess.s_sid;
2551f7d2501SKirk McKusick 			}
25658f0484fSRodney W. Grimes 		} else
2571f7d2501SKirk McKusick 			kp->ki_tdev = NODEV;
25858f0484fSRodney W. Grimes 		if (proc.p_wmesg)
25958f0484fSRodney W. Grimes 			(void)kvm_read(kd, (u_long)proc.p_wmesg,
2601f7d2501SKirk McKusick 			    kp->ki_wmesg, WMESGLEN);
26158f0484fSRodney W. Grimes 
26258f0484fSRodney W. Grimes #ifdef sparc
26358f0484fSRodney W. Grimes 		(void)kvm_read(kd, (u_long)&proc.p_vmspace->vm_rssize,
2641f7d2501SKirk McKusick 		    (char *)&kp->ki_rssize,
2651f7d2501SKirk McKusick 		    sizeof(kp->ki_rssize));
26658f0484fSRodney W. Grimes 		(void)kvm_read(kd, (u_long)&proc.p_vmspace->vm_tsize,
2671f7d2501SKirk McKusick 		    (char *)&kp->ki_tsize,
2681f7d2501SKirk McKusick 		    3 * sizeof(kp->ki_rssize));	/* XXX */
26958f0484fSRodney W. Grimes #else
27058f0484fSRodney W. Grimes 		(void)kvm_read(kd, (u_long)proc.p_vmspace,
2711f7d2501SKirk McKusick 		    (char *)&vmspace, sizeof(vmspace));
2721f7d2501SKirk McKusick 		kp->ki_size = vmspace.vm_map.size;
2731f7d2501SKirk McKusick 		kp->ki_rssize = vmspace.vm_swrss; /* XXX */
2741f7d2501SKirk McKusick 		kp->ki_swrss = vmspace.vm_swrss;
2751f7d2501SKirk McKusick 		kp->ki_tsize = vmspace.vm_tsize;
2761f7d2501SKirk McKusick 		kp->ki_dsize = vmspace.vm_dsize;
2771f7d2501SKirk McKusick 		kp->ki_ssize = vmspace.vm_ssize;
27858f0484fSRodney W. Grimes #endif
27958f0484fSRodney W. Grimes 
28058f0484fSRodney W. Grimes 		switch (what) {
28158f0484fSRodney W. Grimes 
28258f0484fSRodney W. Grimes 		case KERN_PROC_PGRP:
2831f7d2501SKirk McKusick 			if (kp->ki_pgid != (pid_t)arg)
28458f0484fSRodney W. Grimes 				continue;
28558f0484fSRodney W. Grimes 			break;
28658f0484fSRodney W. Grimes 
28758f0484fSRodney W. Grimes 		case KERN_PROC_TTY:
28858f0484fSRodney W. Grimes 			if ((proc.p_flag & P_CONTROLT) == 0 ||
2891f7d2501SKirk McKusick 			     kp->ki_tdev != (dev_t)arg)
29058f0484fSRodney W. Grimes 				continue;
29158f0484fSRodney W. Grimes 			break;
29258f0484fSRodney W. Grimes 		}
2931f7d2501SKirk McKusick 		if (proc.p_comm[0] != 0) {
2941f7d2501SKirk McKusick 			strncpy(kp->ki_comm, proc.p_comm, MAXCOMLEN);
2951f7d2501SKirk McKusick 			kp->ki_comm[MAXCOMLEN] = 0;
2961f7d2501SKirk McKusick 		}
2971f7d2501SKirk McKusick 		if (proc.p_blocked != 0) {
2981f7d2501SKirk McKusick 			kp->ki_kiflag |= KI_MTXBLOCK;
2991f7d2501SKirk McKusick 			if (proc.p_mtxname)
3001f7d2501SKirk McKusick 				(void)kvm_read(kd, (u_long)proc.p_mtxname,
3011f7d2501SKirk McKusick 				    kp->ki_mtxname, MTXNAMELEN);
3021f7d2501SKirk McKusick 			kp->ki_mtxname[MTXNAMELEN] = 0;
3031f7d2501SKirk McKusick 		}
3041f7d2501SKirk McKusick 		kp->ki_runtime = proc.p_runtime;
3051f7d2501SKirk McKusick 		kp->ki_pid = proc.p_pid;
3061f7d2501SKirk McKusick 		kp->ki_siglist = proc.p_siglist;
3071f7d2501SKirk McKusick 		kp->ki_sigmask = proc.p_sigmask;
3081f7d2501SKirk McKusick 		kp->ki_xstat = proc.p_xstat;
3091f7d2501SKirk McKusick 		kp->ki_acflag = proc.p_acflag;
3101f7d2501SKirk McKusick 		kp->ki_pctcpu = proc.p_pctcpu;
3111f7d2501SKirk McKusick 		kp->ki_estcpu = proc.p_estcpu;
3121f7d2501SKirk McKusick 		kp->ki_slptime = proc.p_slptime;
3131f7d2501SKirk McKusick 		kp->ki_swtime = proc.p_swtime;
3141f7d2501SKirk McKusick 		kp->ki_flag = proc.p_flag;
3156d3e7b9bSJohn Baldwin 		kp->ki_sflag = proc.p_sflag;
3161f7d2501SKirk McKusick 		kp->ki_wchan = proc.p_wchan;
3171f7d2501SKirk McKusick 		kp->ki_traceflag = proc.p_traceflag;
3181f7d2501SKirk McKusick 		kp->ki_stat = proc.p_stat;
3194c85452bSJake Burkholder 		kp->ki_pri = proc.p_pri;
3201f7d2501SKirk McKusick 		kp->ki_nice = proc.p_nice;
3211f7d2501SKirk McKusick 		kp->ki_lock = proc.p_lock;
3221f7d2501SKirk McKusick 		kp->ki_rqindex = proc.p_rqindex;
3231f7d2501SKirk McKusick 		kp->ki_oncpu = proc.p_oncpu;
3241f7d2501SKirk McKusick 		kp->ki_lastcpu = proc.p_lastcpu;
3251f7d2501SKirk McKusick 		bcopy(&kinfo_proc, bp, sizeof(kinfo_proc));
32658f0484fSRodney W. Grimes 		++bp;
32758f0484fSRodney W. Grimes 		++cnt;
32858f0484fSRodney W. Grimes 	}
32958f0484fSRodney W. Grimes 	return (cnt);
33058f0484fSRodney W. Grimes }
33158f0484fSRodney W. Grimes 
33258f0484fSRodney W. Grimes /*
33358f0484fSRodney W. Grimes  * Build proc info array by reading in proc list from a crash dump.
33458f0484fSRodney W. Grimes  * Return number of procs read.  maxcnt is the max we will read.
33558f0484fSRodney W. Grimes  */
33658f0484fSRodney W. Grimes static int
33758f0484fSRodney W. Grimes kvm_deadprocs(kd, what, arg, a_allproc, a_zombproc, maxcnt)
33858f0484fSRodney W. Grimes 	kvm_t *kd;
33958f0484fSRodney W. Grimes 	int what, arg;
34058f0484fSRodney W. Grimes 	u_long a_allproc;
34158f0484fSRodney W. Grimes 	u_long a_zombproc;
34258f0484fSRodney W. Grimes 	int maxcnt;
34358f0484fSRodney W. Grimes {
34458f0484fSRodney W. Grimes 	register struct kinfo_proc *bp = kd->procbase;
34558f0484fSRodney W. Grimes 	register int acnt, zcnt;
34658f0484fSRodney W. Grimes 	struct proc *p;
34758f0484fSRodney W. Grimes 
34858f0484fSRodney W. Grimes 	if (KREAD(kd, a_allproc, &p)) {
34958f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "cannot read allproc");
35058f0484fSRodney W. Grimes 		return (-1);
35158f0484fSRodney W. Grimes 	}
35258f0484fSRodney W. Grimes 	acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
35358f0484fSRodney W. Grimes 	if (acnt < 0)
35458f0484fSRodney W. Grimes 		return (acnt);
35558f0484fSRodney W. Grimes 
35658f0484fSRodney W. Grimes 	if (KREAD(kd, a_zombproc, &p)) {
35758f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "cannot read zombproc");
35858f0484fSRodney W. Grimes 		return (-1);
35958f0484fSRodney W. Grimes 	}
36058f0484fSRodney W. Grimes 	zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt);
36158f0484fSRodney W. Grimes 	if (zcnt < 0)
36258f0484fSRodney W. Grimes 		zcnt = 0;
36358f0484fSRodney W. Grimes 
36458f0484fSRodney W. Grimes 	return (acnt + zcnt);
36558f0484fSRodney W. Grimes }
36658f0484fSRodney W. Grimes 
36758f0484fSRodney W. Grimes struct kinfo_proc *
36858f0484fSRodney W. Grimes kvm_getprocs(kd, op, arg, cnt)
36958f0484fSRodney W. Grimes 	kvm_t *kd;
37058f0484fSRodney W. Grimes 	int op, arg;
37158f0484fSRodney W. Grimes 	int *cnt;
37258f0484fSRodney W. Grimes {
373c2ac238cSDoug Rabson 	int mib[4], st, nprocs;
374c2ac238cSDoug Rabson 	size_t size;
37558f0484fSRodney W. Grimes 
37658f0484fSRodney W. Grimes 	if (kd->procbase != 0) {
37758f0484fSRodney W. Grimes 		free((void *)kd->procbase);
37858f0484fSRodney W. Grimes 		/*
37958f0484fSRodney W. Grimes 		 * Clear this pointer in case this call fails.  Otherwise,
38058f0484fSRodney W. Grimes 		 * kvm_close() will free it again.
38158f0484fSRodney W. Grimes 		 */
38258f0484fSRodney W. Grimes 		kd->procbase = 0;
38358f0484fSRodney W. Grimes 	}
38458f0484fSRodney W. Grimes 	if (ISALIVE(kd)) {
38558f0484fSRodney W. Grimes 		size = 0;
38658f0484fSRodney W. Grimes 		mib[0] = CTL_KERN;
38758f0484fSRodney W. Grimes 		mib[1] = KERN_PROC;
38858f0484fSRodney W. Grimes 		mib[2] = op;
38958f0484fSRodney W. Grimes 		mib[3] = arg;
39044ffb5f5SPoul-Henning Kamp 		st = sysctl(mib, op == KERN_PROC_ALL ? 3 : 4, NULL, &size, NULL, 0);
39158f0484fSRodney W. Grimes 		if (st == -1) {
39258f0484fSRodney W. Grimes 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
39358f0484fSRodney W. Grimes 			return (0);
39458f0484fSRodney W. Grimes 		}
395b2d3d0f0SDag-Erling Smørgrav 		do {
396b2d3d0f0SDag-Erling Smørgrav 			size += size / 10;
397b2d3d0f0SDag-Erling Smørgrav 			kd->procbase = (struct kinfo_proc *)
398b2d3d0f0SDag-Erling Smørgrav 			    _kvm_realloc(kd, kd->procbase, size);
39958f0484fSRodney W. Grimes 			if (kd->procbase == 0)
40058f0484fSRodney W. Grimes 				return (0);
401b2d3d0f0SDag-Erling Smørgrav 			st = sysctl(mib, op == KERN_PROC_ALL ? 3 : 4,
402b2d3d0f0SDag-Erling Smørgrav 			    kd->procbase, &size, NULL, 0);
403b2d3d0f0SDag-Erling Smørgrav 		} while (st == -1 && errno == ENOMEM);
40458f0484fSRodney W. Grimes 		if (st == -1) {
40558f0484fSRodney W. Grimes 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
40658f0484fSRodney W. Grimes 			return (0);
40758f0484fSRodney W. Grimes 		}
4080627f53bSDavid Malone 		if (size > 0 &&
4090627f53bSDavid Malone 		    kd->procbase->ki_structsize != sizeof(struct kinfo_proc)) {
41058f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program,
4111f7d2501SKirk McKusick 			    "kinfo_proc size mismatch (expected %d, got %d)",
4121f7d2501SKirk McKusick 			    sizeof(struct kinfo_proc),
4131f7d2501SKirk McKusick 			    kd->procbase->ki_structsize);
41458f0484fSRodney W. Grimes 			return (0);
41558f0484fSRodney W. Grimes 		}
416d339edcfSDavid Malone 		nprocs = size == 0 ? 0 : size / kd->procbase->ki_structsize;
41758f0484fSRodney W. Grimes 	} else {
41858f0484fSRodney W. Grimes 		struct nlist nl[4], *p;
41958f0484fSRodney W. Grimes 
42058f0484fSRodney W. Grimes 		nl[0].n_name = "_nprocs";
42158f0484fSRodney W. Grimes 		nl[1].n_name = "_allproc";
42258f0484fSRodney W. Grimes 		nl[2].n_name = "_zombproc";
42358f0484fSRodney W. Grimes 		nl[3].n_name = 0;
42458f0484fSRodney W. Grimes 
42558f0484fSRodney W. Grimes 		if (kvm_nlist(kd, nl) != 0) {
42658f0484fSRodney W. Grimes 			for (p = nl; p->n_type != 0; ++p)
42758f0484fSRodney W. Grimes 				;
42858f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program,
42958f0484fSRodney W. Grimes 				 "%s: no such symbol", p->n_name);
43058f0484fSRodney W. Grimes 			return (0);
43158f0484fSRodney W. Grimes 		}
43258f0484fSRodney W. Grimes 		if (KREAD(kd, nl[0].n_value, &nprocs)) {
43358f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "can't read nprocs");
43458f0484fSRodney W. Grimes 			return (0);
43558f0484fSRodney W. Grimes 		}
43658f0484fSRodney W. Grimes 		size = nprocs * sizeof(struct kinfo_proc);
43758f0484fSRodney W. Grimes 		kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
43858f0484fSRodney W. Grimes 		if (kd->procbase == 0)
43958f0484fSRodney W. Grimes 			return (0);
44058f0484fSRodney W. Grimes 
44158f0484fSRodney W. Grimes 		nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
44258f0484fSRodney W. Grimes 				      nl[2].n_value, nprocs);
44358f0484fSRodney W. Grimes #ifdef notdef
44458f0484fSRodney W. Grimes 		size = nprocs * sizeof(struct kinfo_proc);
44558f0484fSRodney W. Grimes 		(void)realloc(kd->procbase, size);
44658f0484fSRodney W. Grimes #endif
44758f0484fSRodney W. Grimes 	}
44858f0484fSRodney W. Grimes 	*cnt = nprocs;
44958f0484fSRodney W. Grimes 	return (kd->procbase);
45058f0484fSRodney W. Grimes }
45158f0484fSRodney W. Grimes 
45258f0484fSRodney W. Grimes void
45358f0484fSRodney W. Grimes _kvm_freeprocs(kd)
45458f0484fSRodney W. Grimes 	kvm_t *kd;
45558f0484fSRodney W. Grimes {
45658f0484fSRodney W. Grimes 	if (kd->procbase) {
45758f0484fSRodney W. Grimes 		free(kd->procbase);
45858f0484fSRodney W. Grimes 		kd->procbase = 0;
45958f0484fSRodney W. Grimes 	}
46058f0484fSRodney W. Grimes }
46158f0484fSRodney W. Grimes 
46258f0484fSRodney W. Grimes void *
46358f0484fSRodney W. Grimes _kvm_realloc(kd, p, n)
46458f0484fSRodney W. Grimes 	kvm_t *kd;
46558f0484fSRodney W. Grimes 	void *p;
46658f0484fSRodney W. Grimes 	size_t n;
46758f0484fSRodney W. Grimes {
46858f0484fSRodney W. Grimes 	void *np = (void *)realloc(p, n);
46958f0484fSRodney W. Grimes 
470e8420087SWarner Losh 	if (np == 0) {
471e8420087SWarner Losh 		free(p);
47258f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "out of memory");
473e8420087SWarner Losh 	}
47458f0484fSRodney W. Grimes 	return (np);
47558f0484fSRodney W. Grimes }
47658f0484fSRodney W. Grimes 
47758f0484fSRodney W. Grimes #ifndef MAX
47858f0484fSRodney W. Grimes #define MAX(a, b) ((a) > (b) ? (a) : (b))
47958f0484fSRodney W. Grimes #endif
48058f0484fSRodney W. Grimes 
48158f0484fSRodney W. Grimes /*
4821f7d2501SKirk McKusick  * Read in an argument vector from the user address space of process kp.
48377721f53SPeter Wemm  * addr if the user-space base address of narg null-terminated contiguous
48458f0484fSRodney W. Grimes  * strings.  This is used to read in both the command arguments and
48558f0484fSRodney W. Grimes  * environment strings.  Read at most maxcnt characters of strings.
48658f0484fSRodney W. Grimes  */
48758f0484fSRodney W. Grimes static char **
4881f7d2501SKirk McKusick kvm_argv(kd, kp, addr, narg, maxcnt)
48958f0484fSRodney W. Grimes 	kvm_t *kd;
4901f7d2501SKirk McKusick 	struct kinfo_proc *kp;
49158f0484fSRodney W. Grimes 	register u_long addr;
49258f0484fSRodney W. Grimes 	register int narg;
49358f0484fSRodney W. Grimes 	register int maxcnt;
49458f0484fSRodney W. Grimes {
49577721f53SPeter Wemm 	register char *np, *cp, *ep, *ap;
49677721f53SPeter Wemm 	register u_long oaddr = -1;
49758f0484fSRodney W. Grimes 	register int len, cc;
49858f0484fSRodney W. Grimes 	register char **argv;
49958f0484fSRodney W. Grimes 
50058f0484fSRodney W. Grimes 	/*
50158f0484fSRodney W. Grimes 	 * Check that there aren't an unreasonable number of agruments,
50258f0484fSRodney W. Grimes 	 * and that the address is in user space.
50358f0484fSRodney W. Grimes 	 */
5046b492ae4SPeter Wemm 	if (narg > 512 || addr < VM_MIN_ADDRESS || addr >= VM_MAXUSER_ADDRESS)
50558f0484fSRodney W. Grimes 		return (0);
50658f0484fSRodney W. Grimes 
5076b492ae4SPeter Wemm 	/*
5086b492ae4SPeter Wemm 	 * kd->argv : work space for fetching the strings from the target
5096b492ae4SPeter Wemm 	 *            process's space, and is converted for returning to caller
5106b492ae4SPeter Wemm 	 */
51158f0484fSRodney W. Grimes 	if (kd->argv == 0) {
51258f0484fSRodney W. Grimes 		/*
51358f0484fSRodney W. Grimes 		 * Try to avoid reallocs.
51458f0484fSRodney W. Grimes 		 */
51558f0484fSRodney W. Grimes 		kd->argc = MAX(narg + 1, 32);
51658f0484fSRodney W. Grimes 		kd->argv = (char **)_kvm_malloc(kd, kd->argc *
51758f0484fSRodney W. Grimes 						sizeof(*kd->argv));
51858f0484fSRodney W. Grimes 		if (kd->argv == 0)
51958f0484fSRodney W. Grimes 			return (0);
52058f0484fSRodney W. Grimes 	} else if (narg + 1 > kd->argc) {
52158f0484fSRodney W. Grimes 		kd->argc = MAX(2 * kd->argc, narg + 1);
52258f0484fSRodney W. Grimes 		kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc *
52358f0484fSRodney W. Grimes 						sizeof(*kd->argv));
52458f0484fSRodney W. Grimes 		if (kd->argv == 0)
52558f0484fSRodney W. Grimes 			return (0);
52658f0484fSRodney W. Grimes 	}
5276b492ae4SPeter Wemm 	/*
5286b492ae4SPeter Wemm 	 * kd->argspc : returned to user, this is where the kd->argv
5296b492ae4SPeter Wemm 	 *              arrays are left pointing to the collected strings.
5306b492ae4SPeter Wemm 	 */
53158f0484fSRodney W. Grimes 	if (kd->argspc == 0) {
5322572133eSPoul-Henning Kamp 		kd->argspc = (char *)_kvm_malloc(kd, PAGE_SIZE);
53358f0484fSRodney W. Grimes 		if (kd->argspc == 0)
53458f0484fSRodney W. Grimes 			return (0);
5352572133eSPoul-Henning Kamp 		kd->arglen = PAGE_SIZE;
53658f0484fSRodney W. Grimes 	}
5376b492ae4SPeter Wemm 	/*
5386b492ae4SPeter Wemm 	 * kd->argbuf : used to pull in pages from the target process.
5396b492ae4SPeter Wemm 	 *              the strings are copied out of here.
5406b492ae4SPeter Wemm 	 */
54177721f53SPeter Wemm 	if (kd->argbuf == 0) {
5422572133eSPoul-Henning Kamp 		kd->argbuf = (char *)_kvm_malloc(kd, PAGE_SIZE);
54377721f53SPeter Wemm 		if (kd->argbuf == 0)
54477721f53SPeter Wemm 			return (0);
54577721f53SPeter Wemm 	}
5466b492ae4SPeter Wemm 
5476b492ae4SPeter Wemm 	/* Pull in the target process'es argv vector */
54877721f53SPeter Wemm 	cc = sizeof(char *) * narg;
5491f7d2501SKirk McKusick 	if (kvm_uread(kd, kp, addr, (char *)kd->argv, cc) != cc)
55077721f53SPeter Wemm 		return (0);
5516b492ae4SPeter Wemm 	/*
5526b492ae4SPeter Wemm 	 * ap : saved start address of string we're working on in kd->argspc
5536b492ae4SPeter Wemm 	 * np : pointer to next place to write in kd->argspc
5546b492ae4SPeter Wemm 	 * len: length of data in kd->argspc
5556b492ae4SPeter Wemm 	 * argv: pointer to the argv vector that we are hunting around the
5566b492ae4SPeter Wemm 	 *       target process space for, and converting to addresses in
5576b492ae4SPeter Wemm 	 *       our address space (kd->argspc).
5586b492ae4SPeter Wemm 	 */
55977721f53SPeter Wemm 	ap = np = kd->argspc;
56058f0484fSRodney W. Grimes 	argv = kd->argv;
56158f0484fSRodney W. Grimes 	len = 0;
56258f0484fSRodney W. Grimes 	/*
56358f0484fSRodney W. Grimes 	 * Loop over pages, filling in the argument vector.
5646b492ae4SPeter Wemm 	 * Note that the argv strings could be pointing *anywhere* in
5656b492ae4SPeter Wemm 	 * the user address space and are no longer contiguous.
5666b492ae4SPeter Wemm 	 * Note that *argv is modified when we are going to fetch a string
5676b492ae4SPeter Wemm 	 * that crosses a page boundary.  We copy the next part of the string
5686b492ae4SPeter Wemm 	 * into to "np" and eventually convert the pointer.
56958f0484fSRodney W. Grimes 	 */
57077721f53SPeter Wemm 	while (argv < kd->argv + narg && *argv != 0) {
5716b492ae4SPeter Wemm 
5726b492ae4SPeter Wemm 		/* get the address that the current argv string is on */
5732572133eSPoul-Henning Kamp 		addr = (u_long)*argv & ~(PAGE_SIZE - 1);
5746b492ae4SPeter Wemm 
5756b492ae4SPeter Wemm 		/* is it the same page as the last one? */
57677721f53SPeter Wemm 		if (addr != oaddr) {
5771f7d2501SKirk McKusick 			if (kvm_uread(kd, kp, addr, kd->argbuf, PAGE_SIZE) !=
5782572133eSPoul-Henning Kamp 			    PAGE_SIZE)
57977721f53SPeter Wemm 				return (0);
58077721f53SPeter Wemm 			oaddr = addr;
58177721f53SPeter Wemm 		}
5826b492ae4SPeter Wemm 
5836b492ae4SPeter Wemm 		/* offset within the page... kd->argbuf */
5842572133eSPoul-Henning Kamp 		addr = (u_long)*argv & (PAGE_SIZE - 1);
5856b492ae4SPeter Wemm 
5866b492ae4SPeter Wemm 		/* cp = start of string, cc = count of chars in this chunk */
58777721f53SPeter Wemm 		cp = kd->argbuf + addr;
5882572133eSPoul-Henning Kamp 		cc = PAGE_SIZE - addr;
5896b492ae4SPeter Wemm 
5906b492ae4SPeter Wemm 		/* dont get more than asked for by user process */
59158f0484fSRodney W. Grimes 		if (maxcnt > 0 && cc > maxcnt - len)
5926b492ae4SPeter Wemm 			cc = maxcnt - len;
5936b492ae4SPeter Wemm 
5946b492ae4SPeter Wemm 		/* pointer to end of string if we found it in this page */
59577721f53SPeter Wemm 		ep = memchr(cp, '\0', cc);
59677721f53SPeter Wemm 		if (ep != 0)
59777721f53SPeter Wemm 			cc = ep - cp + 1;
5986b492ae4SPeter Wemm 		/*
5996b492ae4SPeter Wemm 		 * at this point, cc is the count of the chars that we are
6006b492ae4SPeter Wemm 		 * going to retrieve this time. we may or may not have found
6016b492ae4SPeter Wemm 		 * the end of it.  (ep points to the null if the end is known)
6026b492ae4SPeter Wemm 		 */
6036b492ae4SPeter Wemm 
6046b492ae4SPeter Wemm 		/* will we exceed the malloc/realloced buffer? */
60558f0484fSRodney W. Grimes 		if (len + cc > kd->arglen) {
60658f0484fSRodney W. Grimes 			register int off;
60758f0484fSRodney W. Grimes 			register char **pp;
60858f0484fSRodney W. Grimes 			register char *op = kd->argspc;
60958f0484fSRodney W. Grimes 
61058f0484fSRodney W. Grimes 			kd->arglen *= 2;
61158f0484fSRodney W. Grimes 			kd->argspc = (char *)_kvm_realloc(kd, kd->argspc,
61258f0484fSRodney W. Grimes 							  kd->arglen);
61358f0484fSRodney W. Grimes 			if (kd->argspc == 0)
61458f0484fSRodney W. Grimes 				return (0);
61558f0484fSRodney W. Grimes 			/*
61658f0484fSRodney W. Grimes 			 * Adjust argv pointers in case realloc moved
61758f0484fSRodney W. Grimes 			 * the string space.
61858f0484fSRodney W. Grimes 			 */
61958f0484fSRodney W. Grimes 			off = kd->argspc - op;
62077721f53SPeter Wemm 			for (pp = kd->argv; pp < argv; pp++)
62158f0484fSRodney W. Grimes 				*pp += off;
62277721f53SPeter Wemm 			ap += off;
62377721f53SPeter Wemm 			np += off;
62458f0484fSRodney W. Grimes 		}
6256b492ae4SPeter Wemm 		/* np = where to put the next part of the string in kd->argspc*/
6266b492ae4SPeter Wemm 		/* np is kinda redundant.. could use "kd->argspc + len" */
62777721f53SPeter Wemm 		memcpy(np, cp, cc);
6286b492ae4SPeter Wemm 		np += cc;	/* inc counters */
62958f0484fSRodney W. Grimes 		len += cc;
6306b492ae4SPeter Wemm 
6316b492ae4SPeter Wemm 		/*
6326b492ae4SPeter Wemm 		 * if end of string found, set the *argv pointer to the
6336b492ae4SPeter Wemm 		 * saved beginning of string, and advance. argv points to
6346b492ae4SPeter Wemm 		 * somewhere in kd->argv..  This is initially relative
6356b492ae4SPeter Wemm 		 * to the target process, but when we close it off, we set
6366b492ae4SPeter Wemm 		 * it to point in our address space.
6376b492ae4SPeter Wemm 		 */
63877721f53SPeter Wemm 		if (ep != 0) {
63977721f53SPeter Wemm 			*argv++ = ap;
64077721f53SPeter Wemm 			ap = np;
6416b492ae4SPeter Wemm 		} else {
6426b492ae4SPeter Wemm 			/* update the address relative to the target process */
64377721f53SPeter Wemm 			*argv += cc;
6446b492ae4SPeter Wemm 		}
6456b492ae4SPeter Wemm 
64658f0484fSRodney W. Grimes 		if (maxcnt > 0 && len >= maxcnt) {
64758f0484fSRodney W. Grimes 			/*
64858f0484fSRodney W. Grimes 			 * We're stopping prematurely.  Terminate the
64977721f53SPeter Wemm 			 * current string.
65058f0484fSRodney W. Grimes 			 */
65177721f53SPeter Wemm 			if (ep == 0) {
65277721f53SPeter Wemm 				*np = '\0';
65377721f53SPeter Wemm 				*argv++ = ap;
65477721f53SPeter Wemm 			}
65577721f53SPeter Wemm 			break;
65677721f53SPeter Wemm 		}
65777721f53SPeter Wemm 	}
65877721f53SPeter Wemm 	/* Make sure argv is terminated. */
65977721f53SPeter Wemm 	*argv = 0;
66058f0484fSRodney W. Grimes 	return (kd->argv);
66158f0484fSRodney W. Grimes }
66258f0484fSRodney W. Grimes 
66358f0484fSRodney W. Grimes static void
66458f0484fSRodney W. Grimes ps_str_a(p, addr, n)
66558f0484fSRodney W. Grimes 	struct ps_strings *p;
66658f0484fSRodney W. Grimes 	u_long *addr;
66758f0484fSRodney W. Grimes 	int *n;
66858f0484fSRodney W. Grimes {
66958f0484fSRodney W. Grimes 	*addr = (u_long)p->ps_argvstr;
67058f0484fSRodney W. Grimes 	*n = p->ps_nargvstr;
67158f0484fSRodney W. Grimes }
67258f0484fSRodney W. Grimes 
67358f0484fSRodney W. Grimes static void
67458f0484fSRodney W. Grimes ps_str_e(p, addr, n)
67558f0484fSRodney W. Grimes 	struct ps_strings *p;
67658f0484fSRodney W. Grimes 	u_long *addr;
67758f0484fSRodney W. Grimes 	int *n;
67858f0484fSRodney W. Grimes {
67958f0484fSRodney W. Grimes 	*addr = (u_long)p->ps_envstr;
68058f0484fSRodney W. Grimes 	*n = p->ps_nenvstr;
68158f0484fSRodney W. Grimes }
68258f0484fSRodney W. Grimes 
68358f0484fSRodney W. Grimes /*
68458f0484fSRodney W. Grimes  * Determine if the proc indicated by p is still active.
68558f0484fSRodney W. Grimes  * This test is not 100% foolproof in theory, but chances of
68658f0484fSRodney W. Grimes  * being wrong are very low.
68758f0484fSRodney W. Grimes  */
68858f0484fSRodney W. Grimes static int
6891f7d2501SKirk McKusick proc_verify(curkp)
6901f7d2501SKirk McKusick 	struct kinfo_proc *curkp;
69158f0484fSRodney W. Grimes {
6921f7d2501SKirk McKusick 	struct kinfo_proc newkp;
69360160c5eSBruce Evans 	int mib[4];
694c2ac238cSDoug Rabson 	size_t len;
69558f0484fSRodney W. Grimes 
69635e6b695SPoul-Henning Kamp 	mib[0] = CTL_KERN;
69735e6b695SPoul-Henning Kamp 	mib[1] = KERN_PROC;
69835e6b695SPoul-Henning Kamp 	mib[2] = KERN_PROC_PID;
6991f7d2501SKirk McKusick 	mib[3] = curkp->ki_pid;
7001f7d2501SKirk McKusick 	len = sizeof(newkp);
7011f7d2501SKirk McKusick 	if (sysctl(mib, 4, &newkp, &len, NULL, 0) == -1)
70258f0484fSRodney W. Grimes 		return (0);
7031f7d2501SKirk McKusick 	return (curkp->ki_pid == newkp.ki_pid &&
7041f7d2501SKirk McKusick 	    (newkp.ki_stat != SZOMB || curkp->ki_stat == SZOMB));
70558f0484fSRodney W. Grimes }
70658f0484fSRodney W. Grimes 
70758f0484fSRodney W. Grimes static char **
70858f0484fSRodney W. Grimes kvm_doargv(kd, kp, nchr, info)
70958f0484fSRodney W. Grimes 	kvm_t *kd;
7101f7d2501SKirk McKusick 	struct kinfo_proc *kp;
71158f0484fSRodney W. Grimes 	int nchr;
71277721f53SPeter Wemm 	void (*info)(struct ps_strings *, u_long *, int *);
71358f0484fSRodney W. Grimes {
7141f7d2501SKirk McKusick 	char **ap;
71558f0484fSRodney W. Grimes 	u_long addr;
71658f0484fSRodney W. Grimes 	int cnt;
71760160c5eSBruce Evans 	static struct ps_strings arginfo;
71860160c5eSBruce Evans 	static u_long ps_strings;
7197350dd84SPeter Wemm 	size_t len;
7207350dd84SPeter Wemm 
72135e6b695SPoul-Henning Kamp 	if (ps_strings == NULL) {
72260160c5eSBruce Evans 		len = sizeof(ps_strings);
72360160c5eSBruce Evans 		if (sysctlbyname("kern.ps_strings", &ps_strings, &len, NULL,
72460160c5eSBruce Evans 		    0) == -1)
7257350dd84SPeter Wemm 			ps_strings = PS_STRINGS;
72635e6b695SPoul-Henning Kamp 	}
72758f0484fSRodney W. Grimes 
72858f0484fSRodney W. Grimes 	/*
72958f0484fSRodney W. Grimes 	 * Pointers are stored at the top of the user stack.
73058f0484fSRodney W. Grimes 	 */
7311f7d2501SKirk McKusick 	if (kp->ki_stat == SZOMB ||
7321f7d2501SKirk McKusick 	    kvm_uread(kd, kp, ps_strings, (char *)&arginfo,
73377721f53SPeter Wemm 		      sizeof(arginfo)) != sizeof(arginfo))
73458f0484fSRodney W. Grimes 		return (0);
73558f0484fSRodney W. Grimes 
73658f0484fSRodney W. Grimes 	(*info)(&arginfo, &addr, &cnt);
73777721f53SPeter Wemm 	if (cnt == 0)
73877721f53SPeter Wemm 		return (0);
7391f7d2501SKirk McKusick 	ap = kvm_argv(kd, kp, addr, cnt, nchr);
74058f0484fSRodney W. Grimes 	/*
74158f0484fSRodney W. Grimes 	 * For live kernels, make sure this process didn't go away.
74258f0484fSRodney W. Grimes 	 */
7431f7d2501SKirk McKusick 	if (ap != 0 && ISALIVE(kd) && !proc_verify(kp))
74458f0484fSRodney W. Grimes 		ap = 0;
74558f0484fSRodney W. Grimes 	return (ap);
74658f0484fSRodney W. Grimes }
74758f0484fSRodney W. Grimes 
74858f0484fSRodney W. Grimes /*
74958f0484fSRodney W. Grimes  * Get the command args.  This code is now machine independent.
75058f0484fSRodney W. Grimes  */
75158f0484fSRodney W. Grimes char **
75258f0484fSRodney W. Grimes kvm_getargv(kd, kp, nchr)
75358f0484fSRodney W. Grimes 	kvm_t *kd;
75458f0484fSRodney W. Grimes 	const struct kinfo_proc *kp;
75558f0484fSRodney W. Grimes 	int nchr;
75658f0484fSRodney W. Grimes {
757b9df5231SPoul-Henning Kamp 	int oid[4];
758b7875890SDavid E. O'Brien 	int i;
759b7875890SDavid E. O'Brien 	size_t bufsz;
760b9df5231SPoul-Henning Kamp 	static int buflen;
761b9df5231SPoul-Henning Kamp 	static char *buf, *p;
762b9df5231SPoul-Henning Kamp 	static char **bufp;
763b9df5231SPoul-Henning Kamp 	static int argc;
764b9df5231SPoul-Henning Kamp 
765c4a7cdb3SPeter Wemm 	if (!ISALIVE(kd)) {
766c4a7cdb3SPeter Wemm 		_kvm_err(kd, kd->program,
767c4a7cdb3SPeter Wemm 		    "cannot read user space from dead kernel");
768c4a7cdb3SPeter Wemm 		return (0);
769c4a7cdb3SPeter Wemm 	}
770c4a7cdb3SPeter Wemm 
771b9df5231SPoul-Henning Kamp 	if (!buflen) {
772b7875890SDavid E. O'Brien 		bufsz = sizeof(buflen);
773b9df5231SPoul-Henning Kamp 		i = sysctlbyname("kern.ps_arg_cache_limit",
774b7875890SDavid E. O'Brien 		    &buflen, &bufsz, NULL, 0);
775b9df5231SPoul-Henning Kamp 		if (i == -1) {
776b7875890SDavid E. O'Brien 			buflen = 0;
777b9df5231SPoul-Henning Kamp 		} else {
778b9df5231SPoul-Henning Kamp 			buf = malloc(buflen);
779b9df5231SPoul-Henning Kamp 			if (buf == NULL)
780b9df5231SPoul-Henning Kamp 				buflen = 0;
781b9df5231SPoul-Henning Kamp 			argc = 32;
782b9df5231SPoul-Henning Kamp 			bufp = malloc(sizeof(char *) * argc);
783b9df5231SPoul-Henning Kamp 		}
784b9df5231SPoul-Henning Kamp 	}
785b9df5231SPoul-Henning Kamp 	if (buf != NULL) {
786b9df5231SPoul-Henning Kamp 		oid[0] = CTL_KERN;
787b9df5231SPoul-Henning Kamp 		oid[1] = KERN_PROC;
788b9df5231SPoul-Henning Kamp 		oid[2] = KERN_PROC_ARGS;
7891f7d2501SKirk McKusick 		oid[3] = kp->ki_pid;
790b7875890SDavid E. O'Brien 		bufsz = buflen;
791b7875890SDavid E. O'Brien 		i = sysctl(oid, 4, buf, &bufsz, 0, 0);
792b7875890SDavid E. O'Brien 		if (i == 0 && bufsz > 0) {
793b9df5231SPoul-Henning Kamp 			i = 0;
794b9df5231SPoul-Henning Kamp 			p = buf;
795b9df5231SPoul-Henning Kamp 			do {
796b9df5231SPoul-Henning Kamp 				bufp[i++] = p;
797b9df5231SPoul-Henning Kamp 				p += strlen(p) + 1;
798b9df5231SPoul-Henning Kamp 				if (i >= argc) {
799b9df5231SPoul-Henning Kamp 					argc += argc;
800b9df5231SPoul-Henning Kamp 					bufp = realloc(bufp,
801b9df5231SPoul-Henning Kamp 					    sizeof(char *) * argc);
802b9df5231SPoul-Henning Kamp 				}
803b7875890SDavid E. O'Brien 			} while (p < buf + bufsz);
804b9df5231SPoul-Henning Kamp 			bufp[i++] = 0;
805b9df5231SPoul-Henning Kamp 			return (bufp);
806b9df5231SPoul-Henning Kamp 		}
807b9df5231SPoul-Henning Kamp 	}
8081f7d2501SKirk McKusick 	if (kp->ki_flag & P_SYSTEM)
809b9df5231SPoul-Henning Kamp 		return (NULL);
81058f0484fSRodney W. Grimes 	return (kvm_doargv(kd, kp, nchr, ps_str_a));
81158f0484fSRodney W. Grimes }
81258f0484fSRodney W. Grimes 
81358f0484fSRodney W. Grimes char **
81458f0484fSRodney W. Grimes kvm_getenvv(kd, kp, nchr)
81558f0484fSRodney W. Grimes 	kvm_t *kd;
81658f0484fSRodney W. Grimes 	const struct kinfo_proc *kp;
81758f0484fSRodney W. Grimes 	int nchr;
81858f0484fSRodney W. Grimes {
81958f0484fSRodney W. Grimes 	return (kvm_doargv(kd, kp, nchr, ps_str_e));
82058f0484fSRodney W. Grimes }
82158f0484fSRodney W. Grimes 
82258f0484fSRodney W. Grimes /*
82358f0484fSRodney W. Grimes  * Read from user space.  The user context is given by p.
82458f0484fSRodney W. Grimes  */
82558f0484fSRodney W. Grimes ssize_t
8261f7d2501SKirk McKusick kvm_uread(kd, kp, uva, buf, len)
82758f0484fSRodney W. Grimes 	kvm_t *kd;
8281f7d2501SKirk McKusick 	struct kinfo_proc *kp;
82958f0484fSRodney W. Grimes 	register u_long uva;
83058f0484fSRodney W. Grimes 	register char *buf;
83158f0484fSRodney W. Grimes 	register size_t len;
83258f0484fSRodney W. Grimes {
83358f0484fSRodney W. Grimes 	register char *cp;
834338c7541SDavid Greenman 	char procfile[MAXPATHLEN];
835338c7541SDavid Greenman 	ssize_t amount;
836338c7541SDavid Greenman 	int fd;
83758f0484fSRodney W. Grimes 
8387350dd84SPeter Wemm 	if (!ISALIVE(kd)) {
839d7fb4b13SBruce Evans 		_kvm_err(kd, kd->program,
840d7fb4b13SBruce Evans 		    "cannot read user space from dead kernel");
8417350dd84SPeter Wemm 		return (0);
8427350dd84SPeter Wemm 	}
8437350dd84SPeter Wemm 
8441f7d2501SKirk McKusick 	sprintf(procfile, "/proc/%d/mem", kp->ki_pid);
845338c7541SDavid Greenman 	fd = open(procfile, O_RDONLY, 0);
846338c7541SDavid Greenman 	if (fd < 0) {
847338c7541SDavid Greenman 		_kvm_err(kd, kd->program, "cannot open %s", procfile);
848338c7541SDavid Greenman 		close(fd);
84958f0484fSRodney W. Grimes 		return (0);
85058f0484fSRodney W. Grimes 	}
851338c7541SDavid Greenman 
852d7fb4b13SBruce Evans 	cp = buf;
853338c7541SDavid Greenman 	while (len > 0) {
854d7fb4b13SBruce Evans 		errno = 0;
855ec4f2251SJoerg Wunsch 		if (lseek(fd, (off_t)uva, 0) == -1 && errno != 0) {
856d7fb4b13SBruce Evans 			_kvm_err(kd, kd->program, "invalid address (%x) in %s",
857d7fb4b13SBruce Evans 			    uva, procfile);
85858f0484fSRodney W. Grimes 			break;
85958f0484fSRodney W. Grimes 		}
860567127faSDavid Greenman 		amount = read(fd, cp, len);
861338c7541SDavid Greenman 		if (amount < 0) {
862d7fb4b13SBruce Evans 			_kvm_syserr(kd, kd->program, "error reading %s",
863d7fb4b13SBruce Evans 			    procfile);
864d7fb4b13SBruce Evans 			break;
865d7fb4b13SBruce Evans 		}
866d7fb4b13SBruce Evans 		if (amount == 0) {
867d7fb4b13SBruce Evans 			_kvm_err(kd, kd->program, "EOF reading %s", procfile);
868338c7541SDavid Greenman 			break;
869338c7541SDavid Greenman 		}
870338c7541SDavid Greenman 		cp += amount;
871338c7541SDavid Greenman 		uva += amount;
872338c7541SDavid Greenman 		len -= amount;
873338c7541SDavid Greenman 	}
874338c7541SDavid Greenman 
875338c7541SDavid Greenman 	close(fd);
876d7fb4b13SBruce Evans 	return ((ssize_t)(cp - buf));
87758f0484fSRodney W. Grimes }
878