xref: /freebsd/lib/libkvm/kvm_proc.c (revision e602ba25fd1f9a7ea2215c01f470c08f140de809)
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 
40e67f5b9fSMatthew Dillon #include <sys/cdefs.h>
41e67f5b9fSMatthew Dillon __FBSDID("$FreeBSD$");
42e67f5b9fSMatthew Dillon 
4358f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
4458f0484fSRodney W. Grimes static char sccsid[] = "@(#)kvm_proc.c	8.3 (Berkeley) 9/23/93";
4558f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
4658f0484fSRodney W. Grimes 
4758f0484fSRodney W. Grimes /*
4858f0484fSRodney W. Grimes  * Proc traversal interface for kvm.  ps and w are (probably) the exclusive
4958f0484fSRodney W. Grimes  * users of this code, so we've factored it out into a separate module.
5058f0484fSRodney W. Grimes  * Thus, we keep this grunge out of the other kvm applications (i.e.,
5158f0484fSRodney W. Grimes  * most other applications are interested only in open/close/read/nlist).
5258f0484fSRodney W. Grimes  */
5358f0484fSRodney W. Grimes 
5458f0484fSRodney W. Grimes #include <sys/param.h>
5558f0484fSRodney W. Grimes #include <sys/user.h>
5658f0484fSRodney W. Grimes #include <sys/proc.h>
5758f0484fSRodney W. Grimes #include <sys/exec.h>
5858f0484fSRodney W. Grimes #include <sys/stat.h>
5958f0484fSRodney W. Grimes #include <sys/ioctl.h>
6058f0484fSRodney W. Grimes #include <sys/tty.h>
61338c7541SDavid Greenman #include <sys/file.h>
6251295a4dSJordan K. Hubbard #include <stdio.h>
6351295a4dSJordan K. Hubbard #include <stdlib.h>
6458f0484fSRodney W. Grimes #include <unistd.h>
6558f0484fSRodney W. Grimes #include <nlist.h>
6658f0484fSRodney W. Grimes #include <kvm.h>
6758f0484fSRodney W. Grimes 
6858f0484fSRodney W. Grimes #include <vm/vm.h>
6958f0484fSRodney W. Grimes #include <vm/vm_param.h>
7058f0484fSRodney W. Grimes #include <vm/swap_pager.h>
7158f0484fSRodney W. Grimes 
7258f0484fSRodney W. Grimes #include <sys/sysctl.h>
7358f0484fSRodney W. Grimes 
7458f0484fSRodney W. Grimes #include <limits.h>
7577721f53SPeter Wemm #include <memory.h>
7658f0484fSRodney W. Grimes #include <paths.h>
7758f0484fSRodney W. Grimes 
7858f0484fSRodney W. Grimes #include "kvm_private.h"
7958f0484fSRodney W. Grimes 
8051295a4dSJordan K. Hubbard #if used
8158f0484fSRodney W. Grimes static char *
8258f0484fSRodney W. Grimes kvm_readswap(kd, p, va, cnt)
8358f0484fSRodney W. Grimes 	kvm_t *kd;
8458f0484fSRodney W. Grimes 	const struct proc *p;
8558f0484fSRodney W. Grimes 	u_long va;
8658f0484fSRodney W. Grimes 	u_long *cnt;
8758f0484fSRodney W. Grimes {
8821d54b07SRodney W. Grimes #ifdef __FreeBSD__
8921d54b07SRodney W. Grimes 	/* XXX Stubbed out, our vm system is differnet */
9021d54b07SRodney W. Grimes 	_kvm_err(kd, kd->program, "kvm_readswap not implemented");
9121d54b07SRodney W. Grimes 	return(0);
9221d54b07SRodney W. Grimes #endif	/* __FreeBSD__ */
9358f0484fSRodney W. Grimes }
9451295a4dSJordan K. Hubbard #endif
9558f0484fSRodney W. Grimes 
9658f0484fSRodney W. Grimes #define KREAD(kd, addr, obj) \
9758f0484fSRodney W. Grimes 	(kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj))
9858f0484fSRodney W. Grimes 
9958f0484fSRodney W. Grimes /*
10058f0484fSRodney W. Grimes  * Read proc's from memory file into buffer bp, which has space to hold
10158f0484fSRodney W. Grimes  * at most maxcnt procs.
10258f0484fSRodney W. Grimes  */
10358f0484fSRodney W. Grimes static int
10458f0484fSRodney W. Grimes kvm_proclist(kd, what, arg, p, bp, maxcnt)
10558f0484fSRodney W. Grimes 	kvm_t *kd;
10658f0484fSRodney W. Grimes 	int what, arg;
10758f0484fSRodney W. Grimes 	struct proc *p;
10858f0484fSRodney W. Grimes 	struct kinfo_proc *bp;
10958f0484fSRodney W. Grimes 	int maxcnt;
11058f0484fSRodney W. Grimes {
111be04b6d1SDavid E. O'Brien 	int cnt = 0;
1121f7d2501SKirk McKusick 	struct kinfo_proc kinfo_proc, *kp;
11358f0484fSRodney W. Grimes 	struct pgrp pgrp;
11458f0484fSRodney W. Grimes 	struct session sess;
11558f0484fSRodney W. Grimes 	struct tty tty;
1161f7d2501SKirk McKusick 	struct vmspace vmspace;
1171f7d2501SKirk McKusick 	struct procsig procsig;
1181f7d2501SKirk McKusick 	struct pstats pstats;
1191f7d2501SKirk McKusick 	struct ucred ucred;
1203daf63fcSJulian Elischer 	struct thread mainthread;
12158f0484fSRodney W. Grimes 	struct proc proc;
122a58930d8STor Egge 	struct proc pproc;
12349b33de8SBrian Feldman 	struct timeval tv;
12458f0484fSRodney W. Grimes 
1251f7d2501SKirk McKusick 	kp = &kinfo_proc;
1261f7d2501SKirk McKusick 	kp->ki_structsize = sizeof(kinfo_proc);
12742680b3aSBen Smithurst 	for (; cnt < maxcnt && p != NULL; p = LIST_NEXT(&proc, p_list)) {
1286f8c6a69SPeter Wemm 		memset(kp, 0, sizeof *kp);
12958f0484fSRodney W. Grimes 		if (KREAD(kd, (u_long)p, &proc)) {
13058f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "can't read proc at %x", p);
13158f0484fSRodney W. Grimes 			return (-1);
13258f0484fSRodney W. Grimes 		}
1333daf63fcSJulian Elischer 		if (KREAD(kd, (u_long)TAILQ_FIRST(&proc.p_threads),
1343daf63fcSJulian Elischer 		    &mainthread)) {
1353daf63fcSJulian Elischer 			_kvm_err(kd, kd->program, "can't read thread at %x",
1363daf63fcSJulian Elischer 			    TAILQ_FIRST(&proc.p_threads));
1373daf63fcSJulian Elischer 			return (-1);
1383daf63fcSJulian Elischer 		}
139b1fc0ec1SRobert Watson 		if (KREAD(kd, (u_long)proc.p_ucred, &ucred) == 0) {
140b1fc0ec1SRobert Watson 			kp->ki_ruid = ucred.cr_ruid;
141b1fc0ec1SRobert Watson 			kp->ki_svuid = ucred.cr_svuid;
142b1fc0ec1SRobert Watson 			kp->ki_rgid = ucred.cr_rgid;
143b1fc0ec1SRobert Watson 			kp->ki_svgid = ucred.cr_svgid;
1441f7d2501SKirk McKusick 			kp->ki_ngroups = ucred.cr_ngroups;
1451f7d2501SKirk McKusick 			bcopy(ucred.cr_groups, kp->ki_groups,
1461f7d2501SKirk McKusick 			    NGROUPS * sizeof(gid_t));
1471f7d2501SKirk McKusick 			kp->ki_uid = ucred.cr_uid;
1481f7d2501SKirk McKusick 		}
14958f0484fSRodney W. Grimes 
15058f0484fSRodney W. Grimes 		switch(what) {
15158f0484fSRodney W. Grimes 
15258f0484fSRodney W. Grimes 		case KERN_PROC_PID:
15358f0484fSRodney W. Grimes 			if (proc.p_pid != (pid_t)arg)
15458f0484fSRodney W. Grimes 				continue;
15558f0484fSRodney W. Grimes 			break;
15658f0484fSRodney W. Grimes 
15758f0484fSRodney W. Grimes 		case KERN_PROC_UID:
1581f7d2501SKirk McKusick 			if (kp->ki_uid != (uid_t)arg)
15958f0484fSRodney W. Grimes 				continue;
16058f0484fSRodney W. Grimes 			break;
16158f0484fSRodney W. Grimes 
16258f0484fSRodney W. Grimes 		case KERN_PROC_RUID:
1631f7d2501SKirk McKusick 			if (kp->ki_ruid != (uid_t)arg)
16458f0484fSRodney W. Grimes 				continue;
16558f0484fSRodney W. Grimes 			break;
16658f0484fSRodney W. Grimes 		}
16758f0484fSRodney W. Grimes 		/*
16858f0484fSRodney W. Grimes 		 * We're going to add another proc to the set.  If this
16958f0484fSRodney W. Grimes 		 * will overflow the buffer, assume the reason is because
17058f0484fSRodney W. Grimes 		 * nprocs (or the proc list) is corrupt and declare an error.
17158f0484fSRodney W. Grimes 		 */
17258f0484fSRodney W. Grimes 		if (cnt >= maxcnt) {
17358f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "nprocs corrupt");
17458f0484fSRodney W. Grimes 			return (-1);
17558f0484fSRodney W. Grimes 		}
17658f0484fSRodney W. Grimes 		/*
1771f7d2501SKirk McKusick 		 * gather kinfo_proc
17858f0484fSRodney W. Grimes 		 */
1791f7d2501SKirk McKusick 		kp->ki_paddr = p;
180b40ce416SJulian Elischer 		kp->ki_addr = proc.p_uarea;
181b40ce416SJulian Elischer 		/* kp->ki_kstack = proc.p_thread.td_kstack; XXXKSE */
1821f7d2501SKirk McKusick 		kp->ki_args = proc.p_args;
1831f7d2501SKirk McKusick 		kp->ki_tracep = proc.p_tracep;
1841f7d2501SKirk McKusick 		kp->ki_textvp = proc.p_textvp;
1851f7d2501SKirk McKusick 		kp->ki_fd = proc.p_fd;
1861f7d2501SKirk McKusick 		kp->ki_vmspace = proc.p_vmspace;
1871f7d2501SKirk McKusick 		if (proc.p_procsig != NULL) {
1881f7d2501SKirk McKusick 			if (KREAD(kd, (u_long)proc.p_procsig, &procsig)) {
1891f7d2501SKirk McKusick 				_kvm_err(kd, kd->program,
1901f7d2501SKirk McKusick 				    "can't read procsig at %x", proc.p_procsig);
1911f7d2501SKirk McKusick 				return (-1);
1921f7d2501SKirk McKusick 			}
1931f7d2501SKirk McKusick 			kp->ki_sigignore = procsig.ps_sigignore;
1941f7d2501SKirk McKusick 			kp->ki_sigcatch = procsig.ps_sigcatch;
1951f7d2501SKirk McKusick 		}
1966d3e7b9bSJohn Baldwin 		if ((proc.p_sflag & PS_INMEM) && proc.p_stats != NULL) {
1971f7d2501SKirk McKusick 			if (KREAD(kd, (u_long)proc.p_stats, &pstats)) {
1981f7d2501SKirk McKusick 				_kvm_err(kd, kd->program,
1991f7d2501SKirk McKusick 				    "can't read stats at %x", proc.p_stats);
2001f7d2501SKirk McKusick 				return (-1);
2011f7d2501SKirk McKusick 			}
2021f7d2501SKirk McKusick 			kp->ki_start = pstats.p_start;
2031f7d2501SKirk McKusick 			kp->ki_rusage = pstats.p_ru;
2041f7d2501SKirk McKusick 			kp->ki_childtime.tv_sec = pstats.p_cru.ru_utime.tv_sec +
2051f7d2501SKirk McKusick 			    pstats.p_cru.ru_stime.tv_sec;
2061f7d2501SKirk McKusick 			kp->ki_childtime.tv_usec =
2071f7d2501SKirk McKusick 			    pstats.p_cru.ru_utime.tv_usec +
2081f7d2501SKirk McKusick 			    pstats.p_cru.ru_stime.tv_usec;
2091f7d2501SKirk McKusick 		}
210a58930d8STor Egge 		if (proc.p_oppid)
2111f7d2501SKirk McKusick 			kp->ki_ppid = proc.p_oppid;
212a58930d8STor Egge 		else if (proc.p_pptr) {
213a58930d8STor Egge 			if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) {
2141f7d2501SKirk McKusick 				_kvm_err(kd, kd->program,
2151f7d2501SKirk McKusick 				    "can't read pproc at %x", proc.p_pptr);
216a58930d8STor Egge 				return (-1);
217a58930d8STor Egge 			}
2181f7d2501SKirk McKusick 			kp->ki_ppid = pproc.p_pid;
219a58930d8STor Egge 		} else
2201f7d2501SKirk McKusick 			kp->ki_ppid = 0;
2216f8c6a69SPeter Wemm 		if (proc.p_pgrp == NULL)
2226f8c6a69SPeter Wemm 			goto nopgrp;
2236f8c6a69SPeter Wemm 		if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
2246f8c6a69SPeter Wemm 			_kvm_err(kd, kd->program, "can't read pgrp at %x",
2256f8c6a69SPeter Wemm 				 proc.p_pgrp);
2266f8c6a69SPeter Wemm 			return (-1);
2276f8c6a69SPeter Wemm 		}
2281f7d2501SKirk McKusick 		kp->ki_pgid = pgrp.pg_id;
2291f7d2501SKirk McKusick 		kp->ki_jobc = pgrp.pg_jobc;
23058f0484fSRodney W. Grimes 		if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
23158f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "can't read session at %x",
23258f0484fSRodney W. Grimes 				pgrp.pg_session);
23358f0484fSRodney W. Grimes 			return (-1);
23458f0484fSRodney W. Grimes 		}
2351f7d2501SKirk McKusick 		kp->ki_sid = sess.s_sid;
2361f7d2501SKirk McKusick 		(void)memcpy(kp->ki_login, sess.s_login,
2371f7d2501SKirk McKusick 						sizeof(kp->ki_login));
2381f7d2501SKirk McKusick 		kp->ki_kiflag = sess.s_ttyvp ? KI_CTTY : 0;
2391f7d2501SKirk McKusick 		if (sess.s_leader == p)
2401f7d2501SKirk McKusick 			kp->ki_kiflag |= KI_SLEADER;
24158f0484fSRodney W. Grimes 		if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) {
24258f0484fSRodney W. Grimes 			if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
24358f0484fSRodney W. Grimes 				_kvm_err(kd, kd->program,
24458f0484fSRodney W. Grimes 					 "can't read tty at %x", sess.s_ttyp);
24558f0484fSRodney W. Grimes 				return (-1);
24658f0484fSRodney W. Grimes 			}
2471f7d2501SKirk McKusick 			kp->ki_tdev = tty.t_dev;
24858f0484fSRodney W. Grimes 			if (tty.t_pgrp != NULL) {
24958f0484fSRodney W. Grimes 				if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
25058f0484fSRodney W. Grimes 					_kvm_err(kd, kd->program,
25158f0484fSRodney W. Grimes 						 "can't read tpgrp at &x",
25258f0484fSRodney W. Grimes 						tty.t_pgrp);
25358f0484fSRodney W. Grimes 					return (-1);
25458f0484fSRodney W. Grimes 				}
2551f7d2501SKirk McKusick 				kp->ki_tpgid = pgrp.pg_id;
25658f0484fSRodney W. Grimes 			} else
2571f7d2501SKirk McKusick 				kp->ki_tpgid = -1;
2581f7d2501SKirk McKusick 			if (tty.t_session != NULL) {
2591f7d2501SKirk McKusick 				if (KREAD(kd, (u_long)tty.t_session, &sess)) {
2601f7d2501SKirk McKusick 					_kvm_err(kd, kd->program,
2611f7d2501SKirk McKusick 					    "can't read session at %x",
2621f7d2501SKirk McKusick 					    tty.t_session);
2631f7d2501SKirk McKusick 					return (-1);
2641f7d2501SKirk McKusick 				}
2651f7d2501SKirk McKusick 				kp->ki_tsid = sess.s_sid;
2661f7d2501SKirk McKusick 			}
2676f8c6a69SPeter Wemm 		} else {
2686f8c6a69SPeter Wemm nopgrp:
2691f7d2501SKirk McKusick 			kp->ki_tdev = NODEV;
2706f8c6a69SPeter Wemm 		}
2713daf63fcSJulian Elischer 		if (mainthread.td_wmesg)	/* XXXKSE */
2723daf63fcSJulian Elischer 			(void)kvm_read(kd, (u_long)mainthread.td_wmesg,
2731f7d2501SKirk McKusick 			    kp->ki_wmesg, WMESGLEN);
27458f0484fSRodney W. Grimes 
27558f0484fSRodney W. Grimes #ifdef sparc
27658f0484fSRodney W. Grimes 		(void)kvm_read(kd, (u_long)&proc.p_vmspace->vm_rssize,
2771f7d2501SKirk McKusick 		    (char *)&kp->ki_rssize,
2781f7d2501SKirk McKusick 		    sizeof(kp->ki_rssize));
27958f0484fSRodney W. Grimes 		(void)kvm_read(kd, (u_long)&proc.p_vmspace->vm_tsize,
2801f7d2501SKirk McKusick 		    (char *)&kp->ki_tsize,
2811f7d2501SKirk McKusick 		    3 * sizeof(kp->ki_rssize));	/* XXX */
28258f0484fSRodney W. Grimes #else
28358f0484fSRodney W. Grimes 		(void)kvm_read(kd, (u_long)proc.p_vmspace,
2841f7d2501SKirk McKusick 		    (char *)&vmspace, sizeof(vmspace));
2851f7d2501SKirk McKusick 		kp->ki_size = vmspace.vm_map.size;
2861f7d2501SKirk McKusick 		kp->ki_rssize = vmspace.vm_swrss; /* XXX */
2871f7d2501SKirk McKusick 		kp->ki_swrss = vmspace.vm_swrss;
2881f7d2501SKirk McKusick 		kp->ki_tsize = vmspace.vm_tsize;
2891f7d2501SKirk McKusick 		kp->ki_dsize = vmspace.vm_dsize;
2901f7d2501SKirk McKusick 		kp->ki_ssize = vmspace.vm_ssize;
29158f0484fSRodney W. Grimes #endif
29258f0484fSRodney W. Grimes 
29358f0484fSRodney W. Grimes 		switch (what) {
29458f0484fSRodney W. Grimes 
29558f0484fSRodney W. Grimes 		case KERN_PROC_PGRP:
2961f7d2501SKirk McKusick 			if (kp->ki_pgid != (pid_t)arg)
29758f0484fSRodney W. Grimes 				continue;
29858f0484fSRodney W. Grimes 			break;
29958f0484fSRodney W. Grimes 
30058f0484fSRodney W. Grimes 		case KERN_PROC_TTY:
30158f0484fSRodney W. Grimes 			if ((proc.p_flag & P_CONTROLT) == 0 ||
3021f7d2501SKirk McKusick 			     kp->ki_tdev != (dev_t)arg)
30358f0484fSRodney W. Grimes 				continue;
30458f0484fSRodney W. Grimes 			break;
30558f0484fSRodney W. Grimes 		}
3061f7d2501SKirk McKusick 		if (proc.p_comm[0] != 0) {
3071f7d2501SKirk McKusick 			strncpy(kp->ki_comm, proc.p_comm, MAXCOMLEN);
3081f7d2501SKirk McKusick 			kp->ki_comm[MAXCOMLEN] = 0;
3091f7d2501SKirk McKusick 		}
3103daf63fcSJulian Elischer 		if (mainthread.td_blocked != 0) {	/* XXXKSE */
3111f7d2501SKirk McKusick 			kp->ki_kiflag |= KI_MTXBLOCK;
3123daf63fcSJulian Elischer 			if (mainthread.td_mtxname)	/* XXXKSE */
3133daf63fcSJulian Elischer 				(void)kvm_read(kd, (u_long)mainthread.td_mtxname,
3141f7d2501SKirk McKusick 				    kp->ki_mtxname, MTXNAMELEN);
3151f7d2501SKirk McKusick 			kp->ki_mtxname[MTXNAMELEN] = 0;
3161f7d2501SKirk McKusick 		}
31749b33de8SBrian Feldman 		bintime2timeval(&proc.p_runtime, &tv);
31849b33de8SBrian Feldman 		kp->ki_runtime = (u_int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
3191f7d2501SKirk McKusick 		kp->ki_pid = proc.p_pid;
3201f7d2501SKirk McKusick 		kp->ki_siglist = proc.p_siglist;
3211f7d2501SKirk McKusick 		kp->ki_sigmask = proc.p_sigmask;
3221f7d2501SKirk McKusick 		kp->ki_xstat = proc.p_xstat;
3231f7d2501SKirk McKusick 		kp->ki_acflag = proc.p_acflag;
324b40ce416SJulian Elischer 		kp->ki_pctcpu = proc.p_kse.ke_pctcpu;		/* XXXKSE */
325b40ce416SJulian Elischer 		kp->ki_estcpu = proc.p_ksegrp.kg_estcpu;	/* XXXKSE */
326b40ce416SJulian Elischer 		kp->ki_slptime = proc.p_kse.ke_slptime;		/* XXXKSE */
3271f7d2501SKirk McKusick 		kp->ki_swtime = proc.p_swtime;
328e602ba25SJulian Elischer 		kp->ki_flag = proc.p_flag;	/* WILDLY INNACURATE XXXKSE */
3296d3e7b9bSJohn Baldwin 		kp->ki_sflag = proc.p_sflag;
3303daf63fcSJulian Elischer 		kp->ki_wchan = mainthread.td_wchan;		/* XXXKSE */
3311f7d2501SKirk McKusick 		kp->ki_traceflag = proc.p_traceflag;
332e602ba25SJulian Elischer 		if (proc.p_state == PRS_NORMAL) { /*  XXXKSE very aproximate */
333e602ba25SJulian Elischer 			if ((mainthread.td_state == TDS_RUNQ) ||
334e602ba25SJulian Elischer 			    (mainthread.td_state == TDS_RUNNING)) {
335e602ba25SJulian Elischer 				kp->ki_stat = SRUN;
336e602ba25SJulian Elischer 			} else if (mainthread.td_state == TDS_SLP) {
337e602ba25SJulian Elischer 				kp->ki_stat = SSLEEP;
338e602ba25SJulian Elischer 			} else if (P_SHOULDSTOP(&proc)) {
339e602ba25SJulian Elischer 				kp->ki_stat = SSTOP;
340e602ba25SJulian Elischer 			} else if (mainthread.td_state == TDS_MTX) {
341e602ba25SJulian Elischer 				kp->ki_stat = SMTX;
342e602ba25SJulian Elischer 			} else {
343e602ba25SJulian Elischer 				kp->ki_stat = SWAIT;
344e602ba25SJulian Elischer 			}
345e602ba25SJulian Elischer 		} else if (proc.p_state == PRS_ZOMBIE) {
346e602ba25SJulian Elischer 			kp->ki_stat = SZOMB;
347e602ba25SJulian Elischer 		} else {
348e602ba25SJulian Elischer 			kp->ki_stat = SIDL;
349e602ba25SJulian Elischer 		}
35065e4542fSJulian Elischer 		kp->ki_pri.pri_class = proc.p_ksegrp.kg_pri_class; /* XXXKSE */
35165e4542fSJulian Elischer 		kp->ki_pri.pri_user = proc.p_ksegrp.kg_user_pri; /* XXXKSE */
35265e4542fSJulian Elischer 		kp->ki_pri.pri_level = mainthread.td_priority;	/* XXXKSE */
35365e4542fSJulian Elischer 		kp->ki_pri.pri_native = mainthread.td_base_pri; /* XXXKSE */
354b40ce416SJulian Elischer 		kp->ki_nice = proc.p_ksegrp.kg_nice;		/* XXXKSE */
3551f7d2501SKirk McKusick 		kp->ki_lock = proc.p_lock;
356b40ce416SJulian Elischer 		kp->ki_rqindex = proc.p_kse.ke_rqindex;		/* XXXKSE */
357b40ce416SJulian Elischer 		kp->ki_oncpu = proc.p_kse.ke_oncpu;		/* XXXKSE */
3583daf63fcSJulian Elischer 		kp->ki_lastcpu = mainthread.td_lastcpu;	/* XXXKSE */
3591f7d2501SKirk McKusick 		bcopy(&kinfo_proc, bp, sizeof(kinfo_proc));
36058f0484fSRodney W. Grimes 		++bp;
36158f0484fSRodney W. Grimes 		++cnt;
36258f0484fSRodney W. Grimes 	}
36358f0484fSRodney W. Grimes 	return (cnt);
36458f0484fSRodney W. Grimes }
36558f0484fSRodney W. Grimes 
36658f0484fSRodney W. Grimes /*
36758f0484fSRodney W. Grimes  * Build proc info array by reading in proc list from a crash dump.
36858f0484fSRodney W. Grimes  * Return number of procs read.  maxcnt is the max we will read.
36958f0484fSRodney W. Grimes  */
37058f0484fSRodney W. Grimes static int
37158f0484fSRodney W. Grimes kvm_deadprocs(kd, what, arg, a_allproc, a_zombproc, maxcnt)
37258f0484fSRodney W. Grimes 	kvm_t *kd;
37358f0484fSRodney W. Grimes 	int what, arg;
37458f0484fSRodney W. Grimes 	u_long a_allproc;
37558f0484fSRodney W. Grimes 	u_long a_zombproc;
37658f0484fSRodney W. Grimes 	int maxcnt;
37758f0484fSRodney W. Grimes {
378be04b6d1SDavid E. O'Brien 	struct kinfo_proc *bp = kd->procbase;
379be04b6d1SDavid E. O'Brien 	int acnt, zcnt;
38058f0484fSRodney W. Grimes 	struct proc *p;
38158f0484fSRodney W. Grimes 
38258f0484fSRodney W. Grimes 	if (KREAD(kd, a_allproc, &p)) {
38358f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "cannot read allproc");
38458f0484fSRodney W. Grimes 		return (-1);
38558f0484fSRodney W. Grimes 	}
38658f0484fSRodney W. Grimes 	acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
38758f0484fSRodney W. Grimes 	if (acnt < 0)
38858f0484fSRodney W. Grimes 		return (acnt);
38958f0484fSRodney W. Grimes 
39058f0484fSRodney W. Grimes 	if (KREAD(kd, a_zombproc, &p)) {
39158f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "cannot read zombproc");
39258f0484fSRodney W. Grimes 		return (-1);
39358f0484fSRodney W. Grimes 	}
39458f0484fSRodney W. Grimes 	zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt);
39558f0484fSRodney W. Grimes 	if (zcnt < 0)
39658f0484fSRodney W. Grimes 		zcnt = 0;
39758f0484fSRodney W. Grimes 
39858f0484fSRodney W. Grimes 	return (acnt + zcnt);
39958f0484fSRodney W. Grimes }
40058f0484fSRodney W. Grimes 
40158f0484fSRodney W. Grimes struct kinfo_proc *
40258f0484fSRodney W. Grimes kvm_getprocs(kd, op, arg, cnt)
40358f0484fSRodney W. Grimes 	kvm_t *kd;
40458f0484fSRodney W. Grimes 	int op, arg;
40558f0484fSRodney W. Grimes 	int *cnt;
40658f0484fSRodney W. Grimes {
407c2ac238cSDoug Rabson 	int mib[4], st, nprocs;
408c2ac238cSDoug Rabson 	size_t size;
40958f0484fSRodney W. Grimes 
41058f0484fSRodney W. Grimes 	if (kd->procbase != 0) {
41158f0484fSRodney W. Grimes 		free((void *)kd->procbase);
41258f0484fSRodney W. Grimes 		/*
41358f0484fSRodney W. Grimes 		 * Clear this pointer in case this call fails.  Otherwise,
41458f0484fSRodney W. Grimes 		 * kvm_close() will free it again.
41558f0484fSRodney W. Grimes 		 */
41658f0484fSRodney W. Grimes 		kd->procbase = 0;
41758f0484fSRodney W. Grimes 	}
41858f0484fSRodney W. Grimes 	if (ISALIVE(kd)) {
41958f0484fSRodney W. Grimes 		size = 0;
42058f0484fSRodney W. Grimes 		mib[0] = CTL_KERN;
42158f0484fSRodney W. Grimes 		mib[1] = KERN_PROC;
42258f0484fSRodney W. Grimes 		mib[2] = op;
42358f0484fSRodney W. Grimes 		mib[3] = arg;
42444ffb5f5SPoul-Henning Kamp 		st = sysctl(mib, op == KERN_PROC_ALL ? 3 : 4, NULL, &size, NULL, 0);
42558f0484fSRodney W. Grimes 		if (st == -1) {
42658f0484fSRodney W. Grimes 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
42758f0484fSRodney W. Grimes 			return (0);
42858f0484fSRodney W. Grimes 		}
42921687047SDima Dorfman 		/*
43021687047SDima Dorfman 		 * We can't continue with a size of 0 because we pass
43121687047SDima Dorfman 		 * it to realloc() (via _kvm_realloc()), and passing 0
43221687047SDima Dorfman 		 * to realloc() results in undefined behavior.
43321687047SDima Dorfman 		 */
43421687047SDima Dorfman 		if (size == 0) {
43521687047SDima Dorfman 			/*
43621687047SDima Dorfman 			 * XXX: We should probably return an invalid,
43721687047SDima Dorfman 			 * but non-NULL, pointer here so any client
43821687047SDima Dorfman 			 * program trying to dereference it will
43921687047SDima Dorfman 			 * crash.  However, _kvm_freeprocs() calls
44021687047SDima Dorfman 			 * free() on kd->procbase if it isn't NULL,
44121687047SDima Dorfman 			 * and free()'ing a junk pointer isn't good.
44221687047SDima Dorfman 			 * Then again, _kvm_freeprocs() isn't used
44321687047SDima Dorfman 			 * anywhere . . .
44421687047SDima Dorfman 			 */
44521687047SDima Dorfman 			kd->procbase = _kvm_malloc(kd, 1);
44621687047SDima Dorfman 			goto liveout;
44721687047SDima Dorfman 		}
448b2d3d0f0SDag-Erling Smørgrav 		do {
449b2d3d0f0SDag-Erling Smørgrav 			size += size / 10;
450b2d3d0f0SDag-Erling Smørgrav 			kd->procbase = (struct kinfo_proc *)
451b2d3d0f0SDag-Erling Smørgrav 			    _kvm_realloc(kd, kd->procbase, size);
45258f0484fSRodney W. Grimes 			if (kd->procbase == 0)
45358f0484fSRodney W. Grimes 				return (0);
454b2d3d0f0SDag-Erling Smørgrav 			st = sysctl(mib, op == KERN_PROC_ALL ? 3 : 4,
455b2d3d0f0SDag-Erling Smørgrav 			    kd->procbase, &size, NULL, 0);
456b2d3d0f0SDag-Erling Smørgrav 		} while (st == -1 && errno == ENOMEM);
45758f0484fSRodney W. Grimes 		if (st == -1) {
45858f0484fSRodney W. Grimes 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
45958f0484fSRodney W. Grimes 			return (0);
46058f0484fSRodney W. Grimes 		}
46121687047SDima Dorfman 		/*
46221687047SDima Dorfman 		 * We have to check the size again because sysctl()
46321687047SDima Dorfman 		 * may "round up" oldlenp if oldp is NULL; hence it
46421687047SDima Dorfman 		 * might've told us that there was data to get when
46521687047SDima Dorfman 		 * there really isn't any.
46621687047SDima Dorfman 		 */
4670627f53bSDavid Malone 		if (size > 0 &&
4680627f53bSDavid Malone 		    kd->procbase->ki_structsize != sizeof(struct kinfo_proc)) {
46958f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program,
4701f7d2501SKirk McKusick 			    "kinfo_proc size mismatch (expected %d, got %d)",
4711f7d2501SKirk McKusick 			    sizeof(struct kinfo_proc),
4721f7d2501SKirk McKusick 			    kd->procbase->ki_structsize);
47358f0484fSRodney W. Grimes 			return (0);
47458f0484fSRodney W. Grimes 		}
47521687047SDima Dorfman liveout:
476d339edcfSDavid Malone 		nprocs = size == 0 ? 0 : size / kd->procbase->ki_structsize;
47758f0484fSRodney W. Grimes 	} else {
47858f0484fSRodney W. Grimes 		struct nlist nl[4], *p;
47958f0484fSRodney W. Grimes 
48058f0484fSRodney W. Grimes 		nl[0].n_name = "_nprocs";
48158f0484fSRodney W. Grimes 		nl[1].n_name = "_allproc";
48258f0484fSRodney W. Grimes 		nl[2].n_name = "_zombproc";
48358f0484fSRodney W. Grimes 		nl[3].n_name = 0;
48458f0484fSRodney W. Grimes 
48558f0484fSRodney W. Grimes 		if (kvm_nlist(kd, nl) != 0) {
48658f0484fSRodney W. Grimes 			for (p = nl; p->n_type != 0; ++p)
48758f0484fSRodney W. Grimes 				;
48858f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program,
48958f0484fSRodney W. Grimes 				 "%s: no such symbol", p->n_name);
49058f0484fSRodney W. Grimes 			return (0);
49158f0484fSRodney W. Grimes 		}
49258f0484fSRodney W. Grimes 		if (KREAD(kd, nl[0].n_value, &nprocs)) {
49358f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "can't read nprocs");
49458f0484fSRodney W. Grimes 			return (0);
49558f0484fSRodney W. Grimes 		}
49658f0484fSRodney W. Grimes 		size = nprocs * sizeof(struct kinfo_proc);
49758f0484fSRodney W. Grimes 		kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
49858f0484fSRodney W. Grimes 		if (kd->procbase == 0)
49958f0484fSRodney W. Grimes 			return (0);
50058f0484fSRodney W. Grimes 
50158f0484fSRodney W. Grimes 		nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
50258f0484fSRodney W. Grimes 				      nl[2].n_value, nprocs);
50358f0484fSRodney W. Grimes #ifdef notdef
50458f0484fSRodney W. Grimes 		size = nprocs * sizeof(struct kinfo_proc);
50558f0484fSRodney W. Grimes 		(void)realloc(kd->procbase, size);
50658f0484fSRodney W. Grimes #endif
50758f0484fSRodney W. Grimes 	}
50858f0484fSRodney W. Grimes 	*cnt = nprocs;
50958f0484fSRodney W. Grimes 	return (kd->procbase);
51058f0484fSRodney W. Grimes }
51158f0484fSRodney W. Grimes 
51258f0484fSRodney W. Grimes void
51358f0484fSRodney W. Grimes _kvm_freeprocs(kd)
51458f0484fSRodney W. Grimes 	kvm_t *kd;
51558f0484fSRodney W. Grimes {
51658f0484fSRodney W. Grimes 	if (kd->procbase) {
51758f0484fSRodney W. Grimes 		free(kd->procbase);
51858f0484fSRodney W. Grimes 		kd->procbase = 0;
51958f0484fSRodney W. Grimes 	}
52058f0484fSRodney W. Grimes }
52158f0484fSRodney W. Grimes 
52258f0484fSRodney W. Grimes void *
52358f0484fSRodney W. Grimes _kvm_realloc(kd, p, n)
52458f0484fSRodney W. Grimes 	kvm_t *kd;
52558f0484fSRodney W. Grimes 	void *p;
52658f0484fSRodney W. Grimes 	size_t n;
52758f0484fSRodney W. Grimes {
52858f0484fSRodney W. Grimes 	void *np = (void *)realloc(p, n);
52958f0484fSRodney W. Grimes 
530e8420087SWarner Losh 	if (np == 0) {
531e8420087SWarner Losh 		free(p);
53258f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "out of memory");
533e8420087SWarner Losh 	}
53458f0484fSRodney W. Grimes 	return (np);
53558f0484fSRodney W. Grimes }
53658f0484fSRodney W. Grimes 
53758f0484fSRodney W. Grimes #ifndef MAX
53858f0484fSRodney W. Grimes #define MAX(a, b) ((a) > (b) ? (a) : (b))
53958f0484fSRodney W. Grimes #endif
54058f0484fSRodney W. Grimes 
54158f0484fSRodney W. Grimes /*
5421f7d2501SKirk McKusick  * Read in an argument vector from the user address space of process kp.
54377721f53SPeter Wemm  * addr if the user-space base address of narg null-terminated contiguous
54458f0484fSRodney W. Grimes  * strings.  This is used to read in both the command arguments and
54558f0484fSRodney W. Grimes  * environment strings.  Read at most maxcnt characters of strings.
54658f0484fSRodney W. Grimes  */
54758f0484fSRodney W. Grimes static char **
5481f7d2501SKirk McKusick kvm_argv(kd, kp, addr, narg, maxcnt)
54958f0484fSRodney W. Grimes 	kvm_t *kd;
5501f7d2501SKirk McKusick 	struct kinfo_proc *kp;
551be04b6d1SDavid E. O'Brien 	u_long addr;
552be04b6d1SDavid E. O'Brien 	int narg;
553be04b6d1SDavid E. O'Brien 	int maxcnt;
55458f0484fSRodney W. Grimes {
555be04b6d1SDavid E. O'Brien 	char *np, *cp, *ep, *ap;
556be04b6d1SDavid E. O'Brien 	u_long oaddr = -1;
557be04b6d1SDavid E. O'Brien 	int len, cc;
558be04b6d1SDavid E. O'Brien 	char **argv;
55958f0484fSRodney W. Grimes 
56058f0484fSRodney W. Grimes 	/*
56158f0484fSRodney W. Grimes 	 * Check that there aren't an unreasonable number of agruments,
56258f0484fSRodney W. Grimes 	 * and that the address is in user space.
56358f0484fSRodney W. Grimes 	 */
5646b492ae4SPeter Wemm 	if (narg > 512 || addr < VM_MIN_ADDRESS || addr >= VM_MAXUSER_ADDRESS)
56558f0484fSRodney W. Grimes 		return (0);
56658f0484fSRodney W. Grimes 
5676b492ae4SPeter Wemm 	/*
5686b492ae4SPeter Wemm 	 * kd->argv : work space for fetching the strings from the target
5696b492ae4SPeter Wemm 	 *            process's space, and is converted for returning to caller
5706b492ae4SPeter Wemm 	 */
57158f0484fSRodney W. Grimes 	if (kd->argv == 0) {
57258f0484fSRodney W. Grimes 		/*
57358f0484fSRodney W. Grimes 		 * Try to avoid reallocs.
57458f0484fSRodney W. Grimes 		 */
57558f0484fSRodney W. Grimes 		kd->argc = MAX(narg + 1, 32);
57658f0484fSRodney W. Grimes 		kd->argv = (char **)_kvm_malloc(kd, kd->argc *
57758f0484fSRodney W. Grimes 						sizeof(*kd->argv));
57858f0484fSRodney W. Grimes 		if (kd->argv == 0)
57958f0484fSRodney W. Grimes 			return (0);
58058f0484fSRodney W. Grimes 	} else if (narg + 1 > kd->argc) {
58158f0484fSRodney W. Grimes 		kd->argc = MAX(2 * kd->argc, narg + 1);
58258f0484fSRodney W. Grimes 		kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc *
58358f0484fSRodney W. Grimes 						sizeof(*kd->argv));
58458f0484fSRodney W. Grimes 		if (kd->argv == 0)
58558f0484fSRodney W. Grimes 			return (0);
58658f0484fSRodney W. Grimes 	}
5876b492ae4SPeter Wemm 	/*
5886b492ae4SPeter Wemm 	 * kd->argspc : returned to user, this is where the kd->argv
5896b492ae4SPeter Wemm 	 *              arrays are left pointing to the collected strings.
5906b492ae4SPeter Wemm 	 */
59158f0484fSRodney W. Grimes 	if (kd->argspc == 0) {
5922572133eSPoul-Henning Kamp 		kd->argspc = (char *)_kvm_malloc(kd, PAGE_SIZE);
59358f0484fSRodney W. Grimes 		if (kd->argspc == 0)
59458f0484fSRodney W. Grimes 			return (0);
5952572133eSPoul-Henning Kamp 		kd->arglen = PAGE_SIZE;
59658f0484fSRodney W. Grimes 	}
5976b492ae4SPeter Wemm 	/*
5986b492ae4SPeter Wemm 	 * kd->argbuf : used to pull in pages from the target process.
5996b492ae4SPeter Wemm 	 *              the strings are copied out of here.
6006b492ae4SPeter Wemm 	 */
60177721f53SPeter Wemm 	if (kd->argbuf == 0) {
6022572133eSPoul-Henning Kamp 		kd->argbuf = (char *)_kvm_malloc(kd, PAGE_SIZE);
60377721f53SPeter Wemm 		if (kd->argbuf == 0)
60477721f53SPeter Wemm 			return (0);
60577721f53SPeter Wemm 	}
6066b492ae4SPeter Wemm 
6076b492ae4SPeter Wemm 	/* Pull in the target process'es argv vector */
60877721f53SPeter Wemm 	cc = sizeof(char *) * narg;
6091f7d2501SKirk McKusick 	if (kvm_uread(kd, kp, addr, (char *)kd->argv, cc) != cc)
61077721f53SPeter Wemm 		return (0);
6116b492ae4SPeter Wemm 	/*
6126b492ae4SPeter Wemm 	 * ap : saved start address of string we're working on in kd->argspc
6136b492ae4SPeter Wemm 	 * np : pointer to next place to write in kd->argspc
6146b492ae4SPeter Wemm 	 * len: length of data in kd->argspc
6156b492ae4SPeter Wemm 	 * argv: pointer to the argv vector that we are hunting around the
6166b492ae4SPeter Wemm 	 *       target process space for, and converting to addresses in
6176b492ae4SPeter Wemm 	 *       our address space (kd->argspc).
6186b492ae4SPeter Wemm 	 */
61977721f53SPeter Wemm 	ap = np = kd->argspc;
62058f0484fSRodney W. Grimes 	argv = kd->argv;
62158f0484fSRodney W. Grimes 	len = 0;
62258f0484fSRodney W. Grimes 	/*
62358f0484fSRodney W. Grimes 	 * Loop over pages, filling in the argument vector.
6246b492ae4SPeter Wemm 	 * Note that the argv strings could be pointing *anywhere* in
6256b492ae4SPeter Wemm 	 * the user address space and are no longer contiguous.
6266b492ae4SPeter Wemm 	 * Note that *argv is modified when we are going to fetch a string
6276b492ae4SPeter Wemm 	 * that crosses a page boundary.  We copy the next part of the string
6286b492ae4SPeter Wemm 	 * into to "np" and eventually convert the pointer.
62958f0484fSRodney W. Grimes 	 */
63077721f53SPeter Wemm 	while (argv < kd->argv + narg && *argv != 0) {
6316b492ae4SPeter Wemm 
6326b492ae4SPeter Wemm 		/* get the address that the current argv string is on */
6332572133eSPoul-Henning Kamp 		addr = (u_long)*argv & ~(PAGE_SIZE - 1);
6346b492ae4SPeter Wemm 
6356b492ae4SPeter Wemm 		/* is it the same page as the last one? */
63677721f53SPeter Wemm 		if (addr != oaddr) {
6371f7d2501SKirk McKusick 			if (kvm_uread(kd, kp, addr, kd->argbuf, PAGE_SIZE) !=
6382572133eSPoul-Henning Kamp 			    PAGE_SIZE)
63977721f53SPeter Wemm 				return (0);
64077721f53SPeter Wemm 			oaddr = addr;
64177721f53SPeter Wemm 		}
6426b492ae4SPeter Wemm 
6436b492ae4SPeter Wemm 		/* offset within the page... kd->argbuf */
6442572133eSPoul-Henning Kamp 		addr = (u_long)*argv & (PAGE_SIZE - 1);
6456b492ae4SPeter Wemm 
6466b492ae4SPeter Wemm 		/* cp = start of string, cc = count of chars in this chunk */
64777721f53SPeter Wemm 		cp = kd->argbuf + addr;
6482572133eSPoul-Henning Kamp 		cc = PAGE_SIZE - addr;
6496b492ae4SPeter Wemm 
6506b492ae4SPeter Wemm 		/* dont get more than asked for by user process */
65158f0484fSRodney W. Grimes 		if (maxcnt > 0 && cc > maxcnt - len)
6526b492ae4SPeter Wemm 			cc = maxcnt - len;
6536b492ae4SPeter Wemm 
6546b492ae4SPeter Wemm 		/* pointer to end of string if we found it in this page */
65577721f53SPeter Wemm 		ep = memchr(cp, '\0', cc);
65677721f53SPeter Wemm 		if (ep != 0)
65777721f53SPeter Wemm 			cc = ep - cp + 1;
6586b492ae4SPeter Wemm 		/*
6596b492ae4SPeter Wemm 		 * at this point, cc is the count of the chars that we are
6606b492ae4SPeter Wemm 		 * going to retrieve this time. we may or may not have found
6616b492ae4SPeter Wemm 		 * the end of it.  (ep points to the null if the end is known)
6626b492ae4SPeter Wemm 		 */
6636b492ae4SPeter Wemm 
6646b492ae4SPeter Wemm 		/* will we exceed the malloc/realloced buffer? */
66558f0484fSRodney W. Grimes 		if (len + cc > kd->arglen) {
666be04b6d1SDavid E. O'Brien 			int off;
667be04b6d1SDavid E. O'Brien 			char **pp;
668be04b6d1SDavid E. O'Brien 			char *op = kd->argspc;
66958f0484fSRodney W. Grimes 
67058f0484fSRodney W. Grimes 			kd->arglen *= 2;
67158f0484fSRodney W. Grimes 			kd->argspc = (char *)_kvm_realloc(kd, kd->argspc,
67258f0484fSRodney W. Grimes 							  kd->arglen);
67358f0484fSRodney W. Grimes 			if (kd->argspc == 0)
67458f0484fSRodney W. Grimes 				return (0);
67558f0484fSRodney W. Grimes 			/*
67658f0484fSRodney W. Grimes 			 * Adjust argv pointers in case realloc moved
67758f0484fSRodney W. Grimes 			 * the string space.
67858f0484fSRodney W. Grimes 			 */
67958f0484fSRodney W. Grimes 			off = kd->argspc - op;
68077721f53SPeter Wemm 			for (pp = kd->argv; pp < argv; pp++)
68158f0484fSRodney W. Grimes 				*pp += off;
68277721f53SPeter Wemm 			ap += off;
68377721f53SPeter Wemm 			np += off;
68458f0484fSRodney W. Grimes 		}
6856b492ae4SPeter Wemm 		/* np = where to put the next part of the string in kd->argspc*/
6866b492ae4SPeter Wemm 		/* np is kinda redundant.. could use "kd->argspc + len" */
68777721f53SPeter Wemm 		memcpy(np, cp, cc);
6886b492ae4SPeter Wemm 		np += cc;	/* inc counters */
68958f0484fSRodney W. Grimes 		len += cc;
6906b492ae4SPeter Wemm 
6916b492ae4SPeter Wemm 		/*
6926b492ae4SPeter Wemm 		 * if end of string found, set the *argv pointer to the
6936b492ae4SPeter Wemm 		 * saved beginning of string, and advance. argv points to
6946b492ae4SPeter Wemm 		 * somewhere in kd->argv..  This is initially relative
6956b492ae4SPeter Wemm 		 * to the target process, but when we close it off, we set
6966b492ae4SPeter Wemm 		 * it to point in our address space.
6976b492ae4SPeter Wemm 		 */
69877721f53SPeter Wemm 		if (ep != 0) {
69977721f53SPeter Wemm 			*argv++ = ap;
70077721f53SPeter Wemm 			ap = np;
7016b492ae4SPeter Wemm 		} else {
7026b492ae4SPeter Wemm 			/* update the address relative to the target process */
70377721f53SPeter Wemm 			*argv += cc;
7046b492ae4SPeter Wemm 		}
7056b492ae4SPeter Wemm 
70658f0484fSRodney W. Grimes 		if (maxcnt > 0 && len >= maxcnt) {
70758f0484fSRodney W. Grimes 			/*
70858f0484fSRodney W. Grimes 			 * We're stopping prematurely.  Terminate the
70977721f53SPeter Wemm 			 * current string.
71058f0484fSRodney W. Grimes 			 */
71177721f53SPeter Wemm 			if (ep == 0) {
71277721f53SPeter Wemm 				*np = '\0';
71377721f53SPeter Wemm 				*argv++ = ap;
71477721f53SPeter Wemm 			}
71577721f53SPeter Wemm 			break;
71677721f53SPeter Wemm 		}
71777721f53SPeter Wemm 	}
71877721f53SPeter Wemm 	/* Make sure argv is terminated. */
71977721f53SPeter Wemm 	*argv = 0;
72058f0484fSRodney W. Grimes 	return (kd->argv);
72158f0484fSRodney W. Grimes }
72258f0484fSRodney W. Grimes 
72358f0484fSRodney W. Grimes static void
72458f0484fSRodney W. Grimes ps_str_a(p, addr, n)
72558f0484fSRodney W. Grimes 	struct ps_strings *p;
72658f0484fSRodney W. Grimes 	u_long *addr;
72758f0484fSRodney W. Grimes 	int *n;
72858f0484fSRodney W. Grimes {
72958f0484fSRodney W. Grimes 	*addr = (u_long)p->ps_argvstr;
73058f0484fSRodney W. Grimes 	*n = p->ps_nargvstr;
73158f0484fSRodney W. Grimes }
73258f0484fSRodney W. Grimes 
73358f0484fSRodney W. Grimes static void
73458f0484fSRodney W. Grimes ps_str_e(p, addr, n)
73558f0484fSRodney W. Grimes 	struct ps_strings *p;
73658f0484fSRodney W. Grimes 	u_long *addr;
73758f0484fSRodney W. Grimes 	int *n;
73858f0484fSRodney W. Grimes {
73958f0484fSRodney W. Grimes 	*addr = (u_long)p->ps_envstr;
74058f0484fSRodney W. Grimes 	*n = p->ps_nenvstr;
74158f0484fSRodney W. Grimes }
74258f0484fSRodney W. Grimes 
74358f0484fSRodney W. Grimes /*
74458f0484fSRodney W. Grimes  * Determine if the proc indicated by p is still active.
74558f0484fSRodney W. Grimes  * This test is not 100% foolproof in theory, but chances of
74658f0484fSRodney W. Grimes  * being wrong are very low.
74758f0484fSRodney W. Grimes  */
74858f0484fSRodney W. Grimes static int
7491f7d2501SKirk McKusick proc_verify(curkp)
7501f7d2501SKirk McKusick 	struct kinfo_proc *curkp;
75158f0484fSRodney W. Grimes {
7521f7d2501SKirk McKusick 	struct kinfo_proc newkp;
75360160c5eSBruce Evans 	int mib[4];
754c2ac238cSDoug Rabson 	size_t len;
75558f0484fSRodney W. Grimes 
75635e6b695SPoul-Henning Kamp 	mib[0] = CTL_KERN;
75735e6b695SPoul-Henning Kamp 	mib[1] = KERN_PROC;
75835e6b695SPoul-Henning Kamp 	mib[2] = KERN_PROC_PID;
7591f7d2501SKirk McKusick 	mib[3] = curkp->ki_pid;
7601f7d2501SKirk McKusick 	len = sizeof(newkp);
7611f7d2501SKirk McKusick 	if (sysctl(mib, 4, &newkp, &len, NULL, 0) == -1)
76258f0484fSRodney W. Grimes 		return (0);
7631f7d2501SKirk McKusick 	return (curkp->ki_pid == newkp.ki_pid &&
7641f7d2501SKirk McKusick 	    (newkp.ki_stat != SZOMB || curkp->ki_stat == SZOMB));
76558f0484fSRodney W. Grimes }
76658f0484fSRodney W. Grimes 
76758f0484fSRodney W. Grimes static char **
76858f0484fSRodney W. Grimes kvm_doargv(kd, kp, nchr, info)
76958f0484fSRodney W. Grimes 	kvm_t *kd;
7701f7d2501SKirk McKusick 	struct kinfo_proc *kp;
77158f0484fSRodney W. Grimes 	int nchr;
77277721f53SPeter Wemm 	void (*info)(struct ps_strings *, u_long *, int *);
77358f0484fSRodney W. Grimes {
7741f7d2501SKirk McKusick 	char **ap;
77558f0484fSRodney W. Grimes 	u_long addr;
77658f0484fSRodney W. Grimes 	int cnt;
77760160c5eSBruce Evans 	static struct ps_strings arginfo;
77860160c5eSBruce Evans 	static u_long ps_strings;
7797350dd84SPeter Wemm 	size_t len;
7807350dd84SPeter Wemm 
78135e6b695SPoul-Henning Kamp 	if (ps_strings == NULL) {
78260160c5eSBruce Evans 		len = sizeof(ps_strings);
78360160c5eSBruce Evans 		if (sysctlbyname("kern.ps_strings", &ps_strings, &len, NULL,
78460160c5eSBruce Evans 		    0) == -1)
7857350dd84SPeter Wemm 			ps_strings = PS_STRINGS;
78635e6b695SPoul-Henning Kamp 	}
78758f0484fSRodney W. Grimes 
78858f0484fSRodney W. Grimes 	/*
78958f0484fSRodney W. Grimes 	 * Pointers are stored at the top of the user stack.
79058f0484fSRodney W. Grimes 	 */
7911f7d2501SKirk McKusick 	if (kp->ki_stat == SZOMB ||
7921f7d2501SKirk McKusick 	    kvm_uread(kd, kp, ps_strings, (char *)&arginfo,
79377721f53SPeter Wemm 		      sizeof(arginfo)) != sizeof(arginfo))
79458f0484fSRodney W. Grimes 		return (0);
79558f0484fSRodney W. Grimes 
79658f0484fSRodney W. Grimes 	(*info)(&arginfo, &addr, &cnt);
79777721f53SPeter Wemm 	if (cnt == 0)
79877721f53SPeter Wemm 		return (0);
7991f7d2501SKirk McKusick 	ap = kvm_argv(kd, kp, addr, cnt, nchr);
80058f0484fSRodney W. Grimes 	/*
80158f0484fSRodney W. Grimes 	 * For live kernels, make sure this process didn't go away.
80258f0484fSRodney W. Grimes 	 */
8031f7d2501SKirk McKusick 	if (ap != 0 && ISALIVE(kd) && !proc_verify(kp))
80458f0484fSRodney W. Grimes 		ap = 0;
80558f0484fSRodney W. Grimes 	return (ap);
80658f0484fSRodney W. Grimes }
80758f0484fSRodney W. Grimes 
80858f0484fSRodney W. Grimes /*
80958f0484fSRodney W. Grimes  * Get the command args.  This code is now machine independent.
81058f0484fSRodney W. Grimes  */
81158f0484fSRodney W. Grimes char **
81258f0484fSRodney W. Grimes kvm_getargv(kd, kp, nchr)
81358f0484fSRodney W. Grimes 	kvm_t *kd;
81458f0484fSRodney W. Grimes 	const struct kinfo_proc *kp;
81558f0484fSRodney W. Grimes 	int nchr;
81658f0484fSRodney W. Grimes {
817b9df5231SPoul-Henning Kamp 	int oid[4];
818b7875890SDavid E. O'Brien 	int i;
819b7875890SDavid E. O'Brien 	size_t bufsz;
82015d2f5f0SPeter Wemm 	static unsigned long buflen;
821b9df5231SPoul-Henning Kamp 	static char *buf, *p;
822b9df5231SPoul-Henning Kamp 	static char **bufp;
823b9df5231SPoul-Henning Kamp 	static int argc;
824b9df5231SPoul-Henning Kamp 
825c4a7cdb3SPeter Wemm 	if (!ISALIVE(kd)) {
826c4a7cdb3SPeter Wemm 		_kvm_err(kd, kd->program,
827c4a7cdb3SPeter Wemm 		    "cannot read user space from dead kernel");
828c4a7cdb3SPeter Wemm 		return (0);
829c4a7cdb3SPeter Wemm 	}
830c4a7cdb3SPeter Wemm 
831b9df5231SPoul-Henning Kamp 	if (!buflen) {
832b7875890SDavid E. O'Brien 		bufsz = sizeof(buflen);
833b9df5231SPoul-Henning Kamp 		i = sysctlbyname("kern.ps_arg_cache_limit",
834b7875890SDavid E. O'Brien 		    &buflen, &bufsz, NULL, 0);
835b9df5231SPoul-Henning Kamp 		if (i == -1) {
836b7875890SDavid E. O'Brien 			buflen = 0;
837b9df5231SPoul-Henning Kamp 		} else {
838b9df5231SPoul-Henning Kamp 			buf = malloc(buflen);
839b9df5231SPoul-Henning Kamp 			if (buf == NULL)
840b9df5231SPoul-Henning Kamp 				buflen = 0;
841b9df5231SPoul-Henning Kamp 			argc = 32;
842b9df5231SPoul-Henning Kamp 			bufp = malloc(sizeof(char *) * argc);
843b9df5231SPoul-Henning Kamp 		}
844b9df5231SPoul-Henning Kamp 	}
845b9df5231SPoul-Henning Kamp 	if (buf != NULL) {
846b9df5231SPoul-Henning Kamp 		oid[0] = CTL_KERN;
847b9df5231SPoul-Henning Kamp 		oid[1] = KERN_PROC;
848b9df5231SPoul-Henning Kamp 		oid[2] = KERN_PROC_ARGS;
8491f7d2501SKirk McKusick 		oid[3] = kp->ki_pid;
850b7875890SDavid E. O'Brien 		bufsz = buflen;
851b7875890SDavid E. O'Brien 		i = sysctl(oid, 4, buf, &bufsz, 0, 0);
852b7875890SDavid E. O'Brien 		if (i == 0 && bufsz > 0) {
853b9df5231SPoul-Henning Kamp 			i = 0;
854b9df5231SPoul-Henning Kamp 			p = buf;
855b9df5231SPoul-Henning Kamp 			do {
856b9df5231SPoul-Henning Kamp 				bufp[i++] = p;
857b9df5231SPoul-Henning Kamp 				p += strlen(p) + 1;
858b9df5231SPoul-Henning Kamp 				if (i >= argc) {
859b9df5231SPoul-Henning Kamp 					argc += argc;
860b9df5231SPoul-Henning Kamp 					bufp = realloc(bufp,
861b9df5231SPoul-Henning Kamp 					    sizeof(char *) * argc);
862b9df5231SPoul-Henning Kamp 				}
863b7875890SDavid E. O'Brien 			} while (p < buf + bufsz);
864b9df5231SPoul-Henning Kamp 			bufp[i++] = 0;
865b9df5231SPoul-Henning Kamp 			return (bufp);
866b9df5231SPoul-Henning Kamp 		}
867b9df5231SPoul-Henning Kamp 	}
8681f7d2501SKirk McKusick 	if (kp->ki_flag & P_SYSTEM)
869b9df5231SPoul-Henning Kamp 		return (NULL);
87058f0484fSRodney W. Grimes 	return (kvm_doargv(kd, kp, nchr, ps_str_a));
87158f0484fSRodney W. Grimes }
87258f0484fSRodney W. Grimes 
87358f0484fSRodney W. Grimes char **
87458f0484fSRodney W. Grimes kvm_getenvv(kd, kp, nchr)
87558f0484fSRodney W. Grimes 	kvm_t *kd;
87658f0484fSRodney W. Grimes 	const struct kinfo_proc *kp;
87758f0484fSRodney W. Grimes 	int nchr;
87858f0484fSRodney W. Grimes {
87958f0484fSRodney W. Grimes 	return (kvm_doargv(kd, kp, nchr, ps_str_e));
88058f0484fSRodney W. Grimes }
88158f0484fSRodney W. Grimes 
88258f0484fSRodney W. Grimes /*
88358f0484fSRodney W. Grimes  * Read from user space.  The user context is given by p.
88458f0484fSRodney W. Grimes  */
88558f0484fSRodney W. Grimes ssize_t
8861f7d2501SKirk McKusick kvm_uread(kd, kp, uva, buf, len)
88758f0484fSRodney W. Grimes 	kvm_t *kd;
8881f7d2501SKirk McKusick 	struct kinfo_proc *kp;
889be04b6d1SDavid E. O'Brien 	u_long uva;
890be04b6d1SDavid E. O'Brien 	char *buf;
891be04b6d1SDavid E. O'Brien 	size_t len;
89258f0484fSRodney W. Grimes {
893be04b6d1SDavid E. O'Brien 	char *cp;
894338c7541SDavid Greenman 	char procfile[MAXPATHLEN];
895338c7541SDavid Greenman 	ssize_t amount;
896338c7541SDavid Greenman 	int fd;
89758f0484fSRodney W. Grimes 
8987350dd84SPeter Wemm 	if (!ISALIVE(kd)) {
899d7fb4b13SBruce Evans 		_kvm_err(kd, kd->program,
900d7fb4b13SBruce Evans 		    "cannot read user space from dead kernel");
9017350dd84SPeter Wemm 		return (0);
9027350dd84SPeter Wemm 	}
9037350dd84SPeter Wemm 
9041f7d2501SKirk McKusick 	sprintf(procfile, "/proc/%d/mem", kp->ki_pid);
905338c7541SDavid Greenman 	fd = open(procfile, O_RDONLY, 0);
906338c7541SDavid Greenman 	if (fd < 0) {
907338c7541SDavid Greenman 		_kvm_err(kd, kd->program, "cannot open %s", procfile);
908338c7541SDavid Greenman 		close(fd);
90958f0484fSRodney W. Grimes 		return (0);
91058f0484fSRodney W. Grimes 	}
911338c7541SDavid Greenman 
912d7fb4b13SBruce Evans 	cp = buf;
913338c7541SDavid Greenman 	while (len > 0) {
914d7fb4b13SBruce Evans 		errno = 0;
915ec4f2251SJoerg Wunsch 		if (lseek(fd, (off_t)uva, 0) == -1 && errno != 0) {
916d7fb4b13SBruce Evans 			_kvm_err(kd, kd->program, "invalid address (%x) in %s",
917d7fb4b13SBruce Evans 			    uva, procfile);
91858f0484fSRodney W. Grimes 			break;
91958f0484fSRodney W. Grimes 		}
920567127faSDavid Greenman 		amount = read(fd, cp, len);
921338c7541SDavid Greenman 		if (amount < 0) {
922d7fb4b13SBruce Evans 			_kvm_syserr(kd, kd->program, "error reading %s",
923d7fb4b13SBruce Evans 			    procfile);
924d7fb4b13SBruce Evans 			break;
925d7fb4b13SBruce Evans 		}
926d7fb4b13SBruce Evans 		if (amount == 0) {
927d7fb4b13SBruce Evans 			_kvm_err(kd, kd->program, "EOF reading %s", procfile);
928338c7541SDavid Greenman 			break;
929338c7541SDavid Greenman 		}
930338c7541SDavid Greenman 		cp += amount;
931338c7541SDavid Greenman 		uva += amount;
932338c7541SDavid Greenman 		len -= amount;
933338c7541SDavid Greenman 	}
934338c7541SDavid Greenman 
935338c7541SDavid Greenman 	close(fd);
936d7fb4b13SBruce Evans 	return ((ssize_t)(cp - buf));
93758f0484fSRodney W. Grimes }
938