xref: /freebsd/lib/libkvm/kvm_proc.c (revision 84a0b303a9fb7483d021535d40f57424967a71ce)
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  * 4. Neither the name of the University nor the names of its contributors
1858f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1958f0484fSRodney W. Grimes  *    without specific prior written permission.
2058f0484fSRodney W. Grimes  *
2158f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2258f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2358f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2458f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2558f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2658f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2758f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2858f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2958f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3058f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3158f0484fSRodney W. Grimes  * SUCH DAMAGE.
3258f0484fSRodney W. Grimes  */
3358f0484fSRodney W. Grimes 
34f86e3350SBruce Evans #if 0
3558f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
3658f0484fSRodney W. Grimes static char sccsid[] = "@(#)kvm_proc.c	8.3 (Berkeley) 9/23/93";
3758f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
38f86e3350SBruce Evans #endif
39f86e3350SBruce Evans 
40f86e3350SBruce Evans #include <sys/cdefs.h>
41f86e3350SBruce Evans __FBSDID("$FreeBSD$");
4258f0484fSRodney W. Grimes 
4358f0484fSRodney W. Grimes /*
4458f0484fSRodney W. Grimes  * Proc traversal interface for kvm.  ps and w are (probably) the exclusive
4558f0484fSRodney W. Grimes  * users of this code, so we've factored it out into a separate module.
4658f0484fSRodney W. Grimes  * Thus, we keep this grunge out of the other kvm applications (i.e.,
4758f0484fSRodney W. Grimes  * most other applications are interested only in open/close/read/nlist).
4858f0484fSRodney W. Grimes  */
4958f0484fSRodney W. Grimes 
5058f0484fSRodney W. Grimes #include <sys/param.h>
51b20ea179SAlfred Perlstein #define	_WANT_UCRED	/* make ucred.h give us 'struct ucred' */
52aa22cbfeSAlfred Perlstein #include <sys/ucred.h>
53f8197bf0SPawel Jakub Dawidek #include <sys/queue.h>
54f8197bf0SPawel Jakub Dawidek #include <sys/_lock.h>
55f8197bf0SPawel Jakub Dawidek #include <sys/_mutex.h>
56f8197bf0SPawel Jakub Dawidek #include <sys/_task.h>
57f8197bf0SPawel Jakub Dawidek #define	_WANT_PRISON	/* make jail.h give us 'struct prison' */
58f8197bf0SPawel Jakub Dawidek #include <sys/jail.h>
5958f0484fSRodney W. Grimes #include <sys/user.h>
6058f0484fSRodney W. Grimes #include <sys/proc.h>
6158f0484fSRodney W. Grimes #include <sys/exec.h>
6258f0484fSRodney W. Grimes #include <sys/stat.h>
63276de18cSGarance A Drosehn #include <sys/sysent.h>
6458f0484fSRodney W. Grimes #include <sys/ioctl.h>
6558f0484fSRodney W. Grimes #include <sys/tty.h>
66338c7541SDavid Greenman #include <sys/file.h>
672bdd5609SPeter Wemm #include <sys/conf.h>
6851295a4dSJordan K. Hubbard #include <stdio.h>
6951295a4dSJordan K. Hubbard #include <stdlib.h>
7058f0484fSRodney W. Grimes #include <unistd.h>
7158f0484fSRodney W. Grimes #include <nlist.h>
7258f0484fSRodney W. Grimes #include <kvm.h>
7358f0484fSRodney W. Grimes 
7458f0484fSRodney W. Grimes #include <vm/vm.h>
7558f0484fSRodney W. Grimes #include <vm/vm_param.h>
7658f0484fSRodney W. Grimes 
7758f0484fSRodney W. Grimes #include <sys/sysctl.h>
7858f0484fSRodney W. Grimes 
7958f0484fSRodney W. Grimes #include <limits.h>
8077721f53SPeter Wemm #include <memory.h>
8158f0484fSRodney W. Grimes #include <paths.h>
8258f0484fSRodney W. Grimes 
8358f0484fSRodney W. Grimes #include "kvm_private.h"
8458f0484fSRodney W. Grimes 
8558f0484fSRodney W. Grimes #define KREAD(kd, addr, obj) \
8658f0484fSRodney W. Grimes 	(kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj))
8758f0484fSRodney W. Grimes 
8884a0b303SJeff Roberson static int ticks;
8984a0b303SJeff Roberson static int hz;
9084a0b303SJeff Roberson 
9158f0484fSRodney W. Grimes /*
9258f0484fSRodney W. Grimes  * Read proc's from memory file into buffer bp, which has space to hold
9358f0484fSRodney W. Grimes  * at most maxcnt procs.
9458f0484fSRodney W. Grimes  */
9558f0484fSRodney W. Grimes static int
9658f0484fSRodney W. Grimes kvm_proclist(kd, what, arg, p, bp, maxcnt)
9758f0484fSRodney W. Grimes 	kvm_t *kd;
9858f0484fSRodney W. Grimes 	int what, arg;
9958f0484fSRodney W. Grimes 	struct proc *p;
10058f0484fSRodney W. Grimes 	struct kinfo_proc *bp;
10158f0484fSRodney W. Grimes 	int maxcnt;
10258f0484fSRodney W. Grimes {
103be04b6d1SDavid E. O'Brien 	int cnt = 0;
1041f7d2501SKirk McKusick 	struct kinfo_proc kinfo_proc, *kp;
10558f0484fSRodney W. Grimes 	struct pgrp pgrp;
10658f0484fSRodney W. Grimes 	struct session sess;
1072bdd5609SPeter Wemm 	struct cdev t_cdev;
10858f0484fSRodney W. Grimes 	struct tty tty;
1091f7d2501SKirk McKusick 	struct vmspace vmspace;
110840558b9SJohn Baldwin 	struct sigacts sigacts;
1111f7d2501SKirk McKusick 	struct pstats pstats;
1121f7d2501SKirk McKusick 	struct ucred ucred;
113f8197bf0SPawel Jakub Dawidek 	struct prison pr;
11471fad9fdSJulian Elischer 	struct thread mtd;
11558f0484fSRodney W. Grimes 	struct proc proc;
116a58930d8STor Egge 	struct proc pproc;
11749b33de8SBrian Feldman 	struct timeval tv;
118276de18cSGarance A Drosehn 	struct sysentvec sysent;
119276de18cSGarance A Drosehn 	char svname[KI_EMULNAMELEN];
12058f0484fSRodney W. Grimes 
1211f7d2501SKirk McKusick 	kp = &kinfo_proc;
1221f7d2501SKirk McKusick 	kp->ki_structsize = sizeof(kinfo_proc);
12342680b3aSBen Smithurst 	for (; cnt < maxcnt && p != NULL; p = LIST_NEXT(&proc, p_list)) {
1246f8c6a69SPeter Wemm 		memset(kp, 0, sizeof *kp);
12558f0484fSRodney W. Grimes 		if (KREAD(kd, (u_long)p, &proc)) {
12658f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "can't read proc at %x", p);
12758f0484fSRodney W. Grimes 			return (-1);
12858f0484fSRodney W. Grimes 		}
12958551c03SJulian Elischer 		if (proc.p_state != PRS_ZOMBIE) {
1303daf63fcSJulian Elischer 			if (KREAD(kd, (u_long)TAILQ_FIRST(&proc.p_threads),
13171fad9fdSJulian Elischer 			    &mtd)) {
13258551c03SJulian Elischer 				_kvm_err(kd, kd->program,
13358551c03SJulian Elischer 				    "can't read thread at %x",
1343daf63fcSJulian Elischer 				    TAILQ_FIRST(&proc.p_threads));
1353daf63fcSJulian Elischer 				return (-1);
1363daf63fcSJulian Elischer 			}
13758551c03SJulian Elischer 		}
138b1fc0ec1SRobert Watson 		if (KREAD(kd, (u_long)proc.p_ucred, &ucred) == 0) {
139b1fc0ec1SRobert Watson 			kp->ki_ruid = ucred.cr_ruid;
140b1fc0ec1SRobert Watson 			kp->ki_svuid = ucred.cr_svuid;
141b1fc0ec1SRobert Watson 			kp->ki_rgid = ucred.cr_rgid;
142b1fc0ec1SRobert Watson 			kp->ki_svgid = ucred.cr_svgid;
1431f7d2501SKirk McKusick 			kp->ki_ngroups = ucred.cr_ngroups;
1441f7d2501SKirk McKusick 			bcopy(ucred.cr_groups, kp->ki_groups,
1451f7d2501SKirk McKusick 			    NGROUPS * sizeof(gid_t));
1461f7d2501SKirk McKusick 			kp->ki_uid = ucred.cr_uid;
147f8197bf0SPawel Jakub Dawidek 			if (ucred.cr_prison != NULL) {
148f8197bf0SPawel Jakub Dawidek 				if (KREAD(kd, (u_long)ucred.cr_prison, &pr)) {
149f8197bf0SPawel Jakub Dawidek 					_kvm_err(kd, kd->program,
150f8197bf0SPawel Jakub Dawidek 					    "can't read prison at %x",
151f8197bf0SPawel Jakub Dawidek 					    ucred.cr_prison);
152f8197bf0SPawel Jakub Dawidek 					return (-1);
153f8197bf0SPawel Jakub Dawidek 				}
154f8197bf0SPawel Jakub Dawidek 				kp->ki_jid = pr.pr_id;
155f8197bf0SPawel Jakub Dawidek 			}
1561f7d2501SKirk McKusick 		}
15758f0484fSRodney W. Grimes 
158694127f8SDaniel Eischen 		switch(what & ~KERN_PROC_INC_THREAD) {
15958f0484fSRodney W. Grimes 
160276de18cSGarance A Drosehn 		case KERN_PROC_GID:
161276de18cSGarance A Drosehn 			if (kp->ki_groups[0] != (gid_t)arg)
162276de18cSGarance A Drosehn 				continue;
163276de18cSGarance A Drosehn 			break;
164276de18cSGarance A Drosehn 
16558f0484fSRodney W. Grimes 		case KERN_PROC_PID:
16658f0484fSRodney W. Grimes 			if (proc.p_pid != (pid_t)arg)
16758f0484fSRodney W. Grimes 				continue;
16858f0484fSRodney W. Grimes 			break;
16958f0484fSRodney W. Grimes 
170276de18cSGarance A Drosehn 		case KERN_PROC_RGID:
171276de18cSGarance A Drosehn 			if (kp->ki_rgid != (gid_t)arg)
172276de18cSGarance A Drosehn 				continue;
173276de18cSGarance A Drosehn 			break;
174276de18cSGarance A Drosehn 
17558f0484fSRodney W. Grimes 		case KERN_PROC_UID:
1761f7d2501SKirk McKusick 			if (kp->ki_uid != (uid_t)arg)
17758f0484fSRodney W. Grimes 				continue;
17858f0484fSRodney W. Grimes 			break;
17958f0484fSRodney W. Grimes 
18058f0484fSRodney W. Grimes 		case KERN_PROC_RUID:
1811f7d2501SKirk McKusick 			if (kp->ki_ruid != (uid_t)arg)
18258f0484fSRodney W. Grimes 				continue;
18358f0484fSRodney W. Grimes 			break;
18458f0484fSRodney W. Grimes 		}
18558f0484fSRodney W. Grimes 		/*
18658f0484fSRodney W. Grimes 		 * We're going to add another proc to the set.  If this
18758f0484fSRodney W. Grimes 		 * will overflow the buffer, assume the reason is because
18858f0484fSRodney W. Grimes 		 * nprocs (or the proc list) is corrupt and declare an error.
18958f0484fSRodney W. Grimes 		 */
19058f0484fSRodney W. Grimes 		if (cnt >= maxcnt) {
19158f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "nprocs corrupt");
19258f0484fSRodney W. Grimes 			return (-1);
19358f0484fSRodney W. Grimes 		}
19458f0484fSRodney W. Grimes 		/*
1951f7d2501SKirk McKusick 		 * gather kinfo_proc
19658f0484fSRodney W. Grimes 		 */
1971f7d2501SKirk McKusick 		kp->ki_paddr = p;
1987a62aa8aSDavid Schultz 		kp->ki_addr = 0;	/* XXX uarea */
199b40ce416SJulian Elischer 		/* kp->ki_kstack = proc.p_thread.td_kstack; XXXKSE */
2001f7d2501SKirk McKusick 		kp->ki_args = proc.p_args;
2018b7a975eSJohn Baldwin 		kp->ki_tracep = proc.p_tracevp;
2021f7d2501SKirk McKusick 		kp->ki_textvp = proc.p_textvp;
2031f7d2501SKirk McKusick 		kp->ki_fd = proc.p_fd;
2041f7d2501SKirk McKusick 		kp->ki_vmspace = proc.p_vmspace;
205840558b9SJohn Baldwin 		if (proc.p_sigacts != NULL) {
206840558b9SJohn Baldwin 			if (KREAD(kd, (u_long)proc.p_sigacts, &sigacts)) {
2071f7d2501SKirk McKusick 				_kvm_err(kd, kd->program,
208840558b9SJohn Baldwin 				    "can't read sigacts at %x", proc.p_sigacts);
2091f7d2501SKirk McKusick 				return (-1);
2101f7d2501SKirk McKusick 			}
211840558b9SJohn Baldwin 			kp->ki_sigignore = sigacts.ps_sigignore;
212840558b9SJohn Baldwin 			kp->ki_sigcatch = sigacts.ps_sigcatch;
2131f7d2501SKirk McKusick 		}
2148ef6b142SJeff Roberson #if 0
215b61ce5b0SJeff Roberson 		if ((proc.p_flag & P_INMEM) && proc.p_stats != NULL) {
2161f7d2501SKirk McKusick 			if (KREAD(kd, (u_long)proc.p_stats, &pstats)) {
2171f7d2501SKirk McKusick 				_kvm_err(kd, kd->program,
2181f7d2501SKirk McKusick 				    "can't read stats at %x", proc.p_stats);
2191f7d2501SKirk McKusick 				return (-1);
2201f7d2501SKirk McKusick 			}
2211f7d2501SKirk McKusick 			kp->ki_start = pstats.p_start;
22209ff687cSJohn Baldwin 
22309ff687cSJohn Baldwin 			/*
22409ff687cSJohn Baldwin 			 * XXX: The times here are probably zero and need
22509ff687cSJohn Baldwin 			 * to be calculated from the raw data in p_rux and
22609ff687cSJohn Baldwin 			 * p_crux.
22709ff687cSJohn Baldwin 			 */
2281f7d2501SKirk McKusick 			kp->ki_rusage = pstats.p_ru;
229276de18cSGarance A Drosehn 			kp->ki_childstime = pstats.p_cru.ru_stime;
230276de18cSGarance A Drosehn 			kp->ki_childutime = pstats.p_cru.ru_utime;
231276de18cSGarance A Drosehn 			/* Some callers want child-times in a single value */
232276de18cSGarance A Drosehn 			timeradd(&kp->ki_childstime, &kp->ki_childutime,
233276de18cSGarance A Drosehn 			    &kp->ki_childtime);
2341f7d2501SKirk McKusick 		}
2358ef6b142SJeff Roberson #endif
236a58930d8STor Egge 		if (proc.p_oppid)
2371f7d2501SKirk McKusick 			kp->ki_ppid = proc.p_oppid;
238a58930d8STor Egge 		else if (proc.p_pptr) {
239a58930d8STor Egge 			if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) {
2401f7d2501SKirk McKusick 				_kvm_err(kd, kd->program,
2411f7d2501SKirk McKusick 				    "can't read pproc at %x", proc.p_pptr);
242a58930d8STor Egge 				return (-1);
243a58930d8STor Egge 			}
2441f7d2501SKirk McKusick 			kp->ki_ppid = pproc.p_pid;
245a58930d8STor Egge 		} else
2461f7d2501SKirk McKusick 			kp->ki_ppid = 0;
2476f8c6a69SPeter Wemm 		if (proc.p_pgrp == NULL)
2486f8c6a69SPeter Wemm 			goto nopgrp;
2496f8c6a69SPeter Wemm 		if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
2506f8c6a69SPeter Wemm 			_kvm_err(kd, kd->program, "can't read pgrp at %x",
2516f8c6a69SPeter Wemm 				 proc.p_pgrp);
2526f8c6a69SPeter Wemm 			return (-1);
2536f8c6a69SPeter Wemm 		}
2541f7d2501SKirk McKusick 		kp->ki_pgid = pgrp.pg_id;
2551f7d2501SKirk McKusick 		kp->ki_jobc = pgrp.pg_jobc;
25658f0484fSRodney W. Grimes 		if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
25758f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "can't read session at %x",
25858f0484fSRodney W. Grimes 				pgrp.pg_session);
25958f0484fSRodney W. Grimes 			return (-1);
26058f0484fSRodney W. Grimes 		}
2611f7d2501SKirk McKusick 		kp->ki_sid = sess.s_sid;
2621f7d2501SKirk McKusick 		(void)memcpy(kp->ki_login, sess.s_login,
2631f7d2501SKirk McKusick 						sizeof(kp->ki_login));
2641f7d2501SKirk McKusick 		kp->ki_kiflag = sess.s_ttyvp ? KI_CTTY : 0;
2651f7d2501SKirk McKusick 		if (sess.s_leader == p)
2661f7d2501SKirk McKusick 			kp->ki_kiflag |= KI_SLEADER;
26758f0484fSRodney W. Grimes 		if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) {
26858f0484fSRodney W. Grimes 			if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
26958f0484fSRodney W. Grimes 				_kvm_err(kd, kd->program,
27058f0484fSRodney W. Grimes 					 "can't read tty at %x", sess.s_ttyp);
27158f0484fSRodney W. Grimes 				return (-1);
27258f0484fSRodney W. Grimes 			}
2732bdd5609SPeter Wemm 			if (tty.t_dev != NULL) {
2742bdd5609SPeter Wemm 				if (KREAD(kd, (u_long)tty.t_dev, &t_cdev)) {
2752bdd5609SPeter Wemm 					_kvm_err(kd, kd->program,
2762bdd5609SPeter Wemm 						 "can't read cdev at %x",
2772bdd5609SPeter Wemm 						tty.t_dev);
2782bdd5609SPeter Wemm 					return (-1);
2792bdd5609SPeter Wemm 				}
2809c4fb661SPoul-Henning Kamp #if 0
2812bdd5609SPeter Wemm 				kp->ki_tdev = t_cdev.si_udev;
2829c4fb661SPoul-Henning Kamp #else
2832cfe3fdaSPeter Wemm 				kp->ki_tdev = NODEV;
2849c4fb661SPoul-Henning Kamp #endif
2852bdd5609SPeter Wemm 			}
28658f0484fSRodney W. Grimes 			if (tty.t_pgrp != NULL) {
28758f0484fSRodney W. Grimes 				if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
28858f0484fSRodney W. Grimes 					_kvm_err(kd, kd->program,
289d3cf4981SKris Kennaway 						 "can't read tpgrp at %x",
29058f0484fSRodney W. Grimes 						tty.t_pgrp);
29158f0484fSRodney W. Grimes 					return (-1);
29258f0484fSRodney W. Grimes 				}
2931f7d2501SKirk McKusick 				kp->ki_tpgid = pgrp.pg_id;
29458f0484fSRodney W. Grimes 			} else
2951f7d2501SKirk McKusick 				kp->ki_tpgid = -1;
2961f7d2501SKirk McKusick 			if (tty.t_session != NULL) {
2971f7d2501SKirk McKusick 				if (KREAD(kd, (u_long)tty.t_session, &sess)) {
2981f7d2501SKirk McKusick 					_kvm_err(kd, kd->program,
2991f7d2501SKirk McKusick 					    "can't read session at %x",
3001f7d2501SKirk McKusick 					    tty.t_session);
3011f7d2501SKirk McKusick 					return (-1);
3021f7d2501SKirk McKusick 				}
3031f7d2501SKirk McKusick 				kp->ki_tsid = sess.s_sid;
3041f7d2501SKirk McKusick 			}
3056f8c6a69SPeter Wemm 		} else {
3066f8c6a69SPeter Wemm nopgrp:
3071f7d2501SKirk McKusick 			kp->ki_tdev = NODEV;
3086f8c6a69SPeter Wemm 		}
30971fad9fdSJulian Elischer 		if ((proc.p_state != PRS_ZOMBIE) && mtd.td_wmesg)
31071fad9fdSJulian Elischer 			(void)kvm_read(kd, (u_long)mtd.td_wmesg,
3111f7d2501SKirk McKusick 			    kp->ki_wmesg, WMESGLEN);
31258f0484fSRodney W. Grimes 
31358f0484fSRodney W. Grimes 		(void)kvm_read(kd, (u_long)proc.p_vmspace,
3141f7d2501SKirk McKusick 		    (char *)&vmspace, sizeof(vmspace));
3151f7d2501SKirk McKusick 		kp->ki_size = vmspace.vm_map.size;
3161f7d2501SKirk McKusick 		kp->ki_rssize = vmspace.vm_swrss; /* XXX */
3171f7d2501SKirk McKusick 		kp->ki_swrss = vmspace.vm_swrss;
3181f7d2501SKirk McKusick 		kp->ki_tsize = vmspace.vm_tsize;
3191f7d2501SKirk McKusick 		kp->ki_dsize = vmspace.vm_dsize;
3201f7d2501SKirk McKusick 		kp->ki_ssize = vmspace.vm_ssize;
32158f0484fSRodney W. Grimes 
322694127f8SDaniel Eischen 		switch (what & ~KERN_PROC_INC_THREAD) {
32358f0484fSRodney W. Grimes 
32458f0484fSRodney W. Grimes 		case KERN_PROC_PGRP:
3251f7d2501SKirk McKusick 			if (kp->ki_pgid != (pid_t)arg)
32658f0484fSRodney W. Grimes 				continue;
32758f0484fSRodney W. Grimes 			break;
32858f0484fSRodney W. Grimes 
329276de18cSGarance A Drosehn 		case KERN_PROC_SESSION:
330276de18cSGarance A Drosehn 			if (kp->ki_sid != (pid_t)arg)
331276de18cSGarance A Drosehn 				continue;
332276de18cSGarance A Drosehn 			break;
333276de18cSGarance A Drosehn 
33458f0484fSRodney W. Grimes 		case KERN_PROC_TTY:
33558f0484fSRodney W. Grimes 			if ((proc.p_flag & P_CONTROLT) == 0 ||
3361f7d2501SKirk McKusick 			     kp->ki_tdev != (dev_t)arg)
33758f0484fSRodney W. Grimes 				continue;
33858f0484fSRodney W. Grimes 			break;
33958f0484fSRodney W. Grimes 		}
340b7e7c21aSGarance A Drosehn 		if (proc.p_comm[0] != 0)
341b7e7c21aSGarance A Drosehn 			strlcpy(kp->ki_comm, proc.p_comm, MAXCOMLEN);
342276de18cSGarance A Drosehn 		(void)kvm_read(kd, (u_long)proc.p_sysent, (char *)&sysent,
343276de18cSGarance A Drosehn 		    sizeof(sysent));
344276de18cSGarance A Drosehn 		(void)kvm_read(kd, (u_long)sysent.sv_name, (char *)&svname,
345276de18cSGarance A Drosehn 		    sizeof(svname));
346276de18cSGarance A Drosehn 		if (svname[0] != 0)
347276de18cSGarance A Drosehn 			strlcpy(kp->ki_emul, svname, KI_EMULNAMELEN);
348bff4151cSJulian Elischer 		if ((proc.p_state != PRS_ZOMBIE) &&
34971fad9fdSJulian Elischer 		    (mtd.td_blocked != 0)) {
3500d632649SJohn Baldwin 			kp->ki_kiflag |= KI_LOCKBLOCK;
3510d632649SJohn Baldwin 			if (mtd.td_lockname)
352bff4151cSJulian Elischer 				(void)kvm_read(kd,
3530d632649SJohn Baldwin 				    (u_long)mtd.td_lockname,
3540d632649SJohn Baldwin 				    kp->ki_lockname, LOCKNAMELEN);
3550d632649SJohn Baldwin 			kp->ki_lockname[LOCKNAMELEN] = 0;
3561f7d2501SKirk McKusick 		}
35742e43591SYaroslav Tykhiy 		/*
35842e43591SYaroslav Tykhiy 		 * XXX: This is plain wrong, rux_runtime has nothing
35942e43591SYaroslav Tykhiy 		 * to do with struct bintime, rux_runtime is just a 64-bit
36042e43591SYaroslav Tykhiy 		 * integer counter of cputicks.  What we need here is a way
36142e43591SYaroslav Tykhiy 		 * to convert cputicks to usecs.  The kernel does it in
36242e43591SYaroslav Tykhiy 		 * kern/kern_tc.c, but the function can't be just copied.
36342e43591SYaroslav Tykhiy 		 */
36409ff687cSJohn Baldwin 		bintime2timeval(&proc.p_rux.rux_runtime, &tv);
36549b33de8SBrian Feldman 		kp->ki_runtime = (u_int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
3661f7d2501SKirk McKusick 		kp->ki_pid = proc.p_pid;
367e8a58a83SJuli Mallett 		kp->ki_siglist = proc.p_siglist;
36842d3ad71SJeff Roberson 		SIGSETOR(kp->ki_siglist, mtd.td_siglist);
36931a9779eSJeff Roberson 		kp->ki_sigmask = mtd.td_sigmask;
3701f7d2501SKirk McKusick 		kp->ki_xstat = proc.p_xstat;
3711f7d2501SKirk McKusick 		kp->ki_acflag = proc.p_acflag;
3724f0db5e0SJulian Elischer 		kp->ki_lock = proc.p_lock;
373bff4151cSJulian Elischer 		if (proc.p_state != PRS_ZOMBIE) {
37484a0b303SJeff Roberson 			kp->ki_swtime = (ticks - proc.p_swtick) / hz;
3756143c383SJulian Elischer 			kp->ki_flag = proc.p_flag;
376b61ce5b0SJeff Roberson 			kp->ki_sflag = 0;
37730105366SJulian Elischer 			kp->ki_nice = proc.p_nice;
3781f7d2501SKirk McKusick 			kp->ki_traceflag = proc.p_traceflag;
3796143c383SJulian Elischer 			if (proc.p_state == PRS_NORMAL) {
38071fad9fdSJulian Elischer 				if (TD_ON_RUNQ(&mtd) ||
38171fad9fdSJulian Elischer 				    TD_CAN_RUN(&mtd) ||
38271fad9fdSJulian Elischer 				    TD_IS_RUNNING(&mtd)) {
383e602ba25SJulian Elischer 					kp->ki_stat = SRUN;
3844f0db5e0SJulian Elischer 				} else if (mtd.td_state ==
3854f0db5e0SJulian Elischer 				    TDS_INHIBITED) {
38671fad9fdSJulian Elischer 					if (P_SHOULDSTOP(&proc)) {
387e602ba25SJulian Elischer 						kp->ki_stat = SSTOP;
3884f0db5e0SJulian Elischer 					} else if (
3894f0db5e0SJulian Elischer 					    TD_IS_SLEEPING(&mtd)) {
39071fad9fdSJulian Elischer 						kp->ki_stat = SSLEEP;
3910d632649SJohn Baldwin 					} else if (TD_ON_LOCK(&mtd)) {
3920d632649SJohn Baldwin 						kp->ki_stat = SLOCK;
393e602ba25SJulian Elischer 					} else {
394e602ba25SJulian Elischer 						kp->ki_stat = SWAIT;
395e602ba25SJulian Elischer 					}
39671fad9fdSJulian Elischer 				}
397e602ba25SJulian Elischer 			} else {
398e602ba25SJulian Elischer 				kp->ki_stat = SIDL;
399e602ba25SJulian Elischer 			}
4004f0db5e0SJulian Elischer 			/* Stuff from the thread */
40171fad9fdSJulian Elischer 			kp->ki_pri.pri_level = mtd.td_priority;
40271fad9fdSJulian Elischer 			kp->ki_pri.pri_native = mtd.td_base_pri;
40371fad9fdSJulian Elischer 			kp->ki_lastcpu = mtd.td_lastcpu;
4044f0db5e0SJulian Elischer 			kp->ki_wchan = mtd.td_wchan;
405a0ddbf49SJulian Elischer 			kp->ki_oncpu = mtd.td_oncpu;
4064f0db5e0SJulian Elischer 
407d8406adaSDavid Xu 			if (!(proc.p_flag & P_SA)) {
408ed062c8dSJulian Elischer 				kp->ki_pctcpu = 0;
409ed062c8dSJulian Elischer 				kp->ki_rqindex = 0;
4104f0db5e0SJulian Elischer 			} else {
4114f0db5e0SJulian Elischer 				kp->ki_tdflags = -1;
4124f0db5e0SJulian Elischer 				/* All the rest are 0 for now */
4134f0db5e0SJulian Elischer 			}
4146143c383SJulian Elischer 		} else {
4156143c383SJulian Elischer 			kp->ki_stat = SZOMB;
4166143c383SJulian Elischer 		}
4171f7d2501SKirk McKusick 		bcopy(&kinfo_proc, bp, sizeof(kinfo_proc));
41858f0484fSRodney W. Grimes 		++bp;
41958f0484fSRodney W. Grimes 		++cnt;
42058f0484fSRodney W. Grimes 	}
42158f0484fSRodney W. Grimes 	return (cnt);
42258f0484fSRodney W. Grimes }
42358f0484fSRodney W. Grimes 
42458f0484fSRodney W. Grimes /*
42558f0484fSRodney W. Grimes  * Build proc info array by reading in proc list from a crash dump.
42658f0484fSRodney W. Grimes  * Return number of procs read.  maxcnt is the max we will read.
42758f0484fSRodney W. Grimes  */
42858f0484fSRodney W. Grimes static int
42958f0484fSRodney W. Grimes kvm_deadprocs(kd, what, arg, a_allproc, a_zombproc, maxcnt)
43058f0484fSRodney W. Grimes 	kvm_t *kd;
43158f0484fSRodney W. Grimes 	int what, arg;
43258f0484fSRodney W. Grimes 	u_long a_allproc;
43358f0484fSRodney W. Grimes 	u_long a_zombproc;
43458f0484fSRodney W. Grimes 	int maxcnt;
43558f0484fSRodney W. Grimes {
436be04b6d1SDavid E. O'Brien 	struct kinfo_proc *bp = kd->procbase;
437be04b6d1SDavid E. O'Brien 	int acnt, zcnt;
43858f0484fSRodney W. Grimes 	struct proc *p;
43958f0484fSRodney W. Grimes 
44058f0484fSRodney W. Grimes 	if (KREAD(kd, a_allproc, &p)) {
44158f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "cannot read allproc");
44258f0484fSRodney W. Grimes 		return (-1);
44358f0484fSRodney W. Grimes 	}
44458f0484fSRodney W. Grimes 	acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
44558f0484fSRodney W. Grimes 	if (acnt < 0)
44658f0484fSRodney W. Grimes 		return (acnt);
44758f0484fSRodney W. Grimes 
44858f0484fSRodney W. Grimes 	if (KREAD(kd, a_zombproc, &p)) {
44958f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "cannot read zombproc");
45058f0484fSRodney W. Grimes 		return (-1);
45158f0484fSRodney W. Grimes 	}
45258f0484fSRodney W. Grimes 	zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt);
45358f0484fSRodney W. Grimes 	if (zcnt < 0)
45458f0484fSRodney W. Grimes 		zcnt = 0;
45558f0484fSRodney W. Grimes 
45658f0484fSRodney W. Grimes 	return (acnt + zcnt);
45758f0484fSRodney W. Grimes }
45858f0484fSRodney W. Grimes 
45958f0484fSRodney W. Grimes struct kinfo_proc *
46058f0484fSRodney W. Grimes kvm_getprocs(kd, op, arg, cnt)
46158f0484fSRodney W. Grimes 	kvm_t *kd;
46258f0484fSRodney W. Grimes 	int op, arg;
46358f0484fSRodney W. Grimes 	int *cnt;
46458f0484fSRodney W. Grimes {
465c2ac238cSDoug Rabson 	int mib[4], st, nprocs;
466c2ac238cSDoug Rabson 	size_t size;
467694127f8SDaniel Eischen 	int temp_op;
46858f0484fSRodney W. Grimes 
46958f0484fSRodney W. Grimes 	if (kd->procbase != 0) {
47058f0484fSRodney W. Grimes 		free((void *)kd->procbase);
47158f0484fSRodney W. Grimes 		/*
47258f0484fSRodney W. Grimes 		 * Clear this pointer in case this call fails.  Otherwise,
47358f0484fSRodney W. Grimes 		 * kvm_close() will free it again.
47458f0484fSRodney W. Grimes 		 */
47558f0484fSRodney W. Grimes 		kd->procbase = 0;
47658f0484fSRodney W. Grimes 	}
47758f0484fSRodney W. Grimes 	if (ISALIVE(kd)) {
47858f0484fSRodney W. Grimes 		size = 0;
47958f0484fSRodney W. Grimes 		mib[0] = CTL_KERN;
48058f0484fSRodney W. Grimes 		mib[1] = KERN_PROC;
48158f0484fSRodney W. Grimes 		mib[2] = op;
48258f0484fSRodney W. Grimes 		mib[3] = arg;
483694127f8SDaniel Eischen 		temp_op = op & ~KERN_PROC_INC_THREAD;
484694127f8SDaniel Eischen 		st = sysctl(mib,
485694127f8SDaniel Eischen 		    temp_op == KERN_PROC_ALL || temp_op == KERN_PROC_PROC ?
486f2dd06abSTim J. Robbins 		    3 : 4, NULL, &size, NULL, 0);
48758f0484fSRodney W. Grimes 		if (st == -1) {
48858f0484fSRodney W. Grimes 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
48958f0484fSRodney W. Grimes 			return (0);
49058f0484fSRodney W. Grimes 		}
49121687047SDima Dorfman 		/*
49221687047SDima Dorfman 		 * We can't continue with a size of 0 because we pass
49321687047SDima Dorfman 		 * it to realloc() (via _kvm_realloc()), and passing 0
49421687047SDima Dorfman 		 * to realloc() results in undefined behavior.
49521687047SDima Dorfman 		 */
49621687047SDima Dorfman 		if (size == 0) {
49721687047SDima Dorfman 			/*
49821687047SDima Dorfman 			 * XXX: We should probably return an invalid,
49921687047SDima Dorfman 			 * but non-NULL, pointer here so any client
50021687047SDima Dorfman 			 * program trying to dereference it will
50121687047SDima Dorfman 			 * crash.  However, _kvm_freeprocs() calls
50221687047SDima Dorfman 			 * free() on kd->procbase if it isn't NULL,
50321687047SDima Dorfman 			 * and free()'ing a junk pointer isn't good.
50421687047SDima Dorfman 			 * Then again, _kvm_freeprocs() isn't used
50521687047SDima Dorfman 			 * anywhere . . .
50621687047SDima Dorfman 			 */
50721687047SDima Dorfman 			kd->procbase = _kvm_malloc(kd, 1);
50821687047SDima Dorfman 			goto liveout;
50921687047SDima Dorfman 		}
510b2d3d0f0SDag-Erling Smørgrav 		do {
511b2d3d0f0SDag-Erling Smørgrav 			size += size / 10;
512b2d3d0f0SDag-Erling Smørgrav 			kd->procbase = (struct kinfo_proc *)
513b2d3d0f0SDag-Erling Smørgrav 			    _kvm_realloc(kd, kd->procbase, size);
51458f0484fSRodney W. Grimes 			if (kd->procbase == 0)
51558f0484fSRodney W. Grimes 				return (0);
516694127f8SDaniel Eischen 			st = sysctl(mib, temp_op == KERN_PROC_ALL ||
517694127f8SDaniel Eischen 			    temp_op == KERN_PROC_PROC ? 3 : 4,
518b2d3d0f0SDag-Erling Smørgrav 			    kd->procbase, &size, NULL, 0);
519b2d3d0f0SDag-Erling Smørgrav 		} while (st == -1 && errno == ENOMEM);
52058f0484fSRodney W. Grimes 		if (st == -1) {
52158f0484fSRodney W. Grimes 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
52258f0484fSRodney W. Grimes 			return (0);
52358f0484fSRodney W. Grimes 		}
52421687047SDima Dorfman 		/*
52521687047SDima Dorfman 		 * We have to check the size again because sysctl()
52621687047SDima Dorfman 		 * may "round up" oldlenp if oldp is NULL; hence it
52721687047SDima Dorfman 		 * might've told us that there was data to get when
52821687047SDima Dorfman 		 * there really isn't any.
52921687047SDima Dorfman 		 */
5300627f53bSDavid Malone 		if (size > 0 &&
5310627f53bSDavid Malone 		    kd->procbase->ki_structsize != sizeof(struct kinfo_proc)) {
53258f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program,
5331f7d2501SKirk McKusick 			    "kinfo_proc size mismatch (expected %d, got %d)",
5341f7d2501SKirk McKusick 			    sizeof(struct kinfo_proc),
5351f7d2501SKirk McKusick 			    kd->procbase->ki_structsize);
53658f0484fSRodney W. Grimes 			return (0);
53758f0484fSRodney W. Grimes 		}
53821687047SDima Dorfman liveout:
539d339edcfSDavid Malone 		nprocs = size == 0 ? 0 : size / kd->procbase->ki_structsize;
54058f0484fSRodney W. Grimes 	} else {
54184a0b303SJeff Roberson 		struct nlist nl[6], *p;
54258f0484fSRodney W. Grimes 
54358f0484fSRodney W. Grimes 		nl[0].n_name = "_nprocs";
54458f0484fSRodney W. Grimes 		nl[1].n_name = "_allproc";
54558f0484fSRodney W. Grimes 		nl[2].n_name = "_zombproc";
54684a0b303SJeff Roberson 		nl[3].n_name = "_ticks";
54784a0b303SJeff Roberson 		nl[4].n_name = "_hz";
54884a0b303SJeff Roberson 		nl[5].n_name = 0;
54958f0484fSRodney W. Grimes 
55058f0484fSRodney W. Grimes 		if (kvm_nlist(kd, nl) != 0) {
55158f0484fSRodney W. Grimes 			for (p = nl; p->n_type != 0; ++p)
55258f0484fSRodney W. Grimes 				;
55358f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program,
55458f0484fSRodney W. Grimes 				 "%s: no such symbol", p->n_name);
55558f0484fSRodney W. Grimes 			return (0);
55658f0484fSRodney W. Grimes 		}
55758f0484fSRodney W. Grimes 		if (KREAD(kd, nl[0].n_value, &nprocs)) {
55858f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "can't read nprocs");
55958f0484fSRodney W. Grimes 			return (0);
56058f0484fSRodney W. Grimes 		}
56184a0b303SJeff Roberson 		if (KREAD(kd, nl[3].n_value, &ticks)) {
56284a0b303SJeff Roberson 			_kvm_err(kd, kd->program, "can't read ticks");
56384a0b303SJeff Roberson 			return (0);
56484a0b303SJeff Roberson 		}
56584a0b303SJeff Roberson 		if (KREAD(kd, nl[4].n_value, &hz)) {
56684a0b303SJeff Roberson 			_kvm_err(kd, kd->program, "can't read hz");
56784a0b303SJeff Roberson 			return (0);
56884a0b303SJeff Roberson 		}
56958f0484fSRodney W. Grimes 		size = nprocs * sizeof(struct kinfo_proc);
57058f0484fSRodney W. Grimes 		kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
57158f0484fSRodney W. Grimes 		if (kd->procbase == 0)
57258f0484fSRodney W. Grimes 			return (0);
57358f0484fSRodney W. Grimes 
57458f0484fSRodney W. Grimes 		nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
57558f0484fSRodney W. Grimes 				      nl[2].n_value, nprocs);
57658f0484fSRodney W. Grimes #ifdef notdef
57758f0484fSRodney W. Grimes 		size = nprocs * sizeof(struct kinfo_proc);
57858f0484fSRodney W. Grimes 		(void)realloc(kd->procbase, size);
57958f0484fSRodney W. Grimes #endif
58058f0484fSRodney W. Grimes 	}
58158f0484fSRodney W. Grimes 	*cnt = nprocs;
58258f0484fSRodney W. Grimes 	return (kd->procbase);
58358f0484fSRodney W. Grimes }
58458f0484fSRodney W. Grimes 
58558f0484fSRodney W. Grimes void
58658f0484fSRodney W. Grimes _kvm_freeprocs(kd)
58758f0484fSRodney W. Grimes 	kvm_t *kd;
58858f0484fSRodney W. Grimes {
58958f0484fSRodney W. Grimes 	if (kd->procbase) {
59058f0484fSRodney W. Grimes 		free(kd->procbase);
59158f0484fSRodney W. Grimes 		kd->procbase = 0;
59258f0484fSRodney W. Grimes 	}
59358f0484fSRodney W. Grimes }
59458f0484fSRodney W. Grimes 
59558f0484fSRodney W. Grimes void *
59658f0484fSRodney W. Grimes _kvm_realloc(kd, p, n)
59758f0484fSRodney W. Grimes 	kvm_t *kd;
59858f0484fSRodney W. Grimes 	void *p;
59958f0484fSRodney W. Grimes 	size_t n;
60058f0484fSRodney W. Grimes {
60158f0484fSRodney W. Grimes 	void *np = (void *)realloc(p, n);
60258f0484fSRodney W. Grimes 
603e8420087SWarner Losh 	if (np == 0) {
604e8420087SWarner Losh 		free(p);
60558f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "out of memory");
606e8420087SWarner Losh 	}
60758f0484fSRodney W. Grimes 	return (np);
60858f0484fSRodney W. Grimes }
60958f0484fSRodney W. Grimes 
61058f0484fSRodney W. Grimes #ifndef MAX
61158f0484fSRodney W. Grimes #define MAX(a, b) ((a) > (b) ? (a) : (b))
61258f0484fSRodney W. Grimes #endif
61358f0484fSRodney W. Grimes 
61458f0484fSRodney W. Grimes /*
6151f7d2501SKirk McKusick  * Read in an argument vector from the user address space of process kp.
61677721f53SPeter Wemm  * addr if the user-space base address of narg null-terminated contiguous
61758f0484fSRodney W. Grimes  * strings.  This is used to read in both the command arguments and
61858f0484fSRodney W. Grimes  * environment strings.  Read at most maxcnt characters of strings.
61958f0484fSRodney W. Grimes  */
62058f0484fSRodney W. Grimes static char **
6211f7d2501SKirk McKusick kvm_argv(kd, kp, addr, narg, maxcnt)
62258f0484fSRodney W. Grimes 	kvm_t *kd;
6231f7d2501SKirk McKusick 	struct kinfo_proc *kp;
624be04b6d1SDavid E. O'Brien 	u_long addr;
625be04b6d1SDavid E. O'Brien 	int narg;
626be04b6d1SDavid E. O'Brien 	int maxcnt;
62758f0484fSRodney W. Grimes {
628be04b6d1SDavid E. O'Brien 	char *np, *cp, *ep, *ap;
629be04b6d1SDavid E. O'Brien 	u_long oaddr = -1;
630be04b6d1SDavid E. O'Brien 	int len, cc;
631be04b6d1SDavid E. O'Brien 	char **argv;
63258f0484fSRodney W. Grimes 
63358f0484fSRodney W. Grimes 	/*
63458f0484fSRodney W. Grimes 	 * Check that there aren't an unreasonable number of agruments,
63558f0484fSRodney W. Grimes 	 * and that the address is in user space.
63658f0484fSRodney W. Grimes 	 */
6376b492ae4SPeter Wemm 	if (narg > 512 || addr < VM_MIN_ADDRESS || addr >= VM_MAXUSER_ADDRESS)
63858f0484fSRodney W. Grimes 		return (0);
63958f0484fSRodney W. Grimes 
6406b492ae4SPeter Wemm 	/*
6416b492ae4SPeter Wemm 	 * kd->argv : work space for fetching the strings from the target
6426b492ae4SPeter Wemm 	 *            process's space, and is converted for returning to caller
6436b492ae4SPeter Wemm 	 */
64458f0484fSRodney W. Grimes 	if (kd->argv == 0) {
64558f0484fSRodney W. Grimes 		/*
64658f0484fSRodney W. Grimes 		 * Try to avoid reallocs.
64758f0484fSRodney W. Grimes 		 */
64858f0484fSRodney W. Grimes 		kd->argc = MAX(narg + 1, 32);
64958f0484fSRodney W. Grimes 		kd->argv = (char **)_kvm_malloc(kd, kd->argc *
65058f0484fSRodney W. Grimes 						sizeof(*kd->argv));
65158f0484fSRodney W. Grimes 		if (kd->argv == 0)
65258f0484fSRodney W. Grimes 			return (0);
65358f0484fSRodney W. Grimes 	} else if (narg + 1 > kd->argc) {
65458f0484fSRodney W. Grimes 		kd->argc = MAX(2 * kd->argc, narg + 1);
65558f0484fSRodney W. Grimes 		kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc *
65658f0484fSRodney W. Grimes 						sizeof(*kd->argv));
65758f0484fSRodney W. Grimes 		if (kd->argv == 0)
65858f0484fSRodney W. Grimes 			return (0);
65958f0484fSRodney W. Grimes 	}
6606b492ae4SPeter Wemm 	/*
6616b492ae4SPeter Wemm 	 * kd->argspc : returned to user, this is where the kd->argv
6626b492ae4SPeter Wemm 	 *              arrays are left pointing to the collected strings.
6636b492ae4SPeter Wemm 	 */
66458f0484fSRodney W. Grimes 	if (kd->argspc == 0) {
6652572133eSPoul-Henning Kamp 		kd->argspc = (char *)_kvm_malloc(kd, PAGE_SIZE);
66658f0484fSRodney W. Grimes 		if (kd->argspc == 0)
66758f0484fSRodney W. Grimes 			return (0);
6682572133eSPoul-Henning Kamp 		kd->arglen = PAGE_SIZE;
66958f0484fSRodney W. Grimes 	}
6706b492ae4SPeter Wemm 	/*
6716b492ae4SPeter Wemm 	 * kd->argbuf : used to pull in pages from the target process.
6726b492ae4SPeter Wemm 	 *              the strings are copied out of here.
6736b492ae4SPeter Wemm 	 */
67477721f53SPeter Wemm 	if (kd->argbuf == 0) {
6752572133eSPoul-Henning Kamp 		kd->argbuf = (char *)_kvm_malloc(kd, PAGE_SIZE);
67677721f53SPeter Wemm 		if (kd->argbuf == 0)
67777721f53SPeter Wemm 			return (0);
67877721f53SPeter Wemm 	}
6796b492ae4SPeter Wemm 
6806b492ae4SPeter Wemm 	/* Pull in the target process'es argv vector */
68177721f53SPeter Wemm 	cc = sizeof(char *) * narg;
6821f7d2501SKirk McKusick 	if (kvm_uread(kd, kp, addr, (char *)kd->argv, cc) != cc)
68377721f53SPeter Wemm 		return (0);
6846b492ae4SPeter Wemm 	/*
6856b492ae4SPeter Wemm 	 * ap : saved start address of string we're working on in kd->argspc
6866b492ae4SPeter Wemm 	 * np : pointer to next place to write in kd->argspc
6876b492ae4SPeter Wemm 	 * len: length of data in kd->argspc
6886b492ae4SPeter Wemm 	 * argv: pointer to the argv vector that we are hunting around the
6896b492ae4SPeter Wemm 	 *       target process space for, and converting to addresses in
6906b492ae4SPeter Wemm 	 *       our address space (kd->argspc).
6916b492ae4SPeter Wemm 	 */
69277721f53SPeter Wemm 	ap = np = kd->argspc;
69358f0484fSRodney W. Grimes 	argv = kd->argv;
69458f0484fSRodney W. Grimes 	len = 0;
69558f0484fSRodney W. Grimes 	/*
69658f0484fSRodney W. Grimes 	 * Loop over pages, filling in the argument vector.
6976b492ae4SPeter Wemm 	 * Note that the argv strings could be pointing *anywhere* in
6986b492ae4SPeter Wemm 	 * the user address space and are no longer contiguous.
6996b492ae4SPeter Wemm 	 * Note that *argv is modified when we are going to fetch a string
7006b492ae4SPeter Wemm 	 * that crosses a page boundary.  We copy the next part of the string
7016b492ae4SPeter Wemm 	 * into to "np" and eventually convert the pointer.
70258f0484fSRodney W. Grimes 	 */
70377721f53SPeter Wemm 	while (argv < kd->argv + narg && *argv != 0) {
7046b492ae4SPeter Wemm 
7056b492ae4SPeter Wemm 		/* get the address that the current argv string is on */
7062572133eSPoul-Henning Kamp 		addr = (u_long)*argv & ~(PAGE_SIZE - 1);
7076b492ae4SPeter Wemm 
7086b492ae4SPeter Wemm 		/* is it the same page as the last one? */
70977721f53SPeter Wemm 		if (addr != oaddr) {
7101f7d2501SKirk McKusick 			if (kvm_uread(kd, kp, addr, kd->argbuf, PAGE_SIZE) !=
7112572133eSPoul-Henning Kamp 			    PAGE_SIZE)
71277721f53SPeter Wemm 				return (0);
71377721f53SPeter Wemm 			oaddr = addr;
71477721f53SPeter Wemm 		}
7156b492ae4SPeter Wemm 
7166b492ae4SPeter Wemm 		/* offset within the page... kd->argbuf */
7172572133eSPoul-Henning Kamp 		addr = (u_long)*argv & (PAGE_SIZE - 1);
7186b492ae4SPeter Wemm 
7196b492ae4SPeter Wemm 		/* cp = start of string, cc = count of chars in this chunk */
72077721f53SPeter Wemm 		cp = kd->argbuf + addr;
7212572133eSPoul-Henning Kamp 		cc = PAGE_SIZE - addr;
7226b492ae4SPeter Wemm 
7236b492ae4SPeter Wemm 		/* dont get more than asked for by user process */
72458f0484fSRodney W. Grimes 		if (maxcnt > 0 && cc > maxcnt - len)
7256b492ae4SPeter Wemm 			cc = maxcnt - len;
7266b492ae4SPeter Wemm 
7276b492ae4SPeter Wemm 		/* pointer to end of string if we found it in this page */
72877721f53SPeter Wemm 		ep = memchr(cp, '\0', cc);
72977721f53SPeter Wemm 		if (ep != 0)
73077721f53SPeter Wemm 			cc = ep - cp + 1;
7316b492ae4SPeter Wemm 		/*
7326b492ae4SPeter Wemm 		 * at this point, cc is the count of the chars that we are
7336b492ae4SPeter Wemm 		 * going to retrieve this time. we may or may not have found
7346b492ae4SPeter Wemm 		 * the end of it.  (ep points to the null if the end is known)
7356b492ae4SPeter Wemm 		 */
7366b492ae4SPeter Wemm 
7376b492ae4SPeter Wemm 		/* will we exceed the malloc/realloced buffer? */
73858f0484fSRodney W. Grimes 		if (len + cc > kd->arglen) {
739be04b6d1SDavid E. O'Brien 			int off;
740be04b6d1SDavid E. O'Brien 			char **pp;
741be04b6d1SDavid E. O'Brien 			char *op = kd->argspc;
74258f0484fSRodney W. Grimes 
74358f0484fSRodney W. Grimes 			kd->arglen *= 2;
74458f0484fSRodney W. Grimes 			kd->argspc = (char *)_kvm_realloc(kd, kd->argspc,
74558f0484fSRodney W. Grimes 							  kd->arglen);
74658f0484fSRodney W. Grimes 			if (kd->argspc == 0)
74758f0484fSRodney W. Grimes 				return (0);
74858f0484fSRodney W. Grimes 			/*
74958f0484fSRodney W. Grimes 			 * Adjust argv pointers in case realloc moved
75058f0484fSRodney W. Grimes 			 * the string space.
75158f0484fSRodney W. Grimes 			 */
75258f0484fSRodney W. Grimes 			off = kd->argspc - op;
75377721f53SPeter Wemm 			for (pp = kd->argv; pp < argv; pp++)
75458f0484fSRodney W. Grimes 				*pp += off;
75577721f53SPeter Wemm 			ap += off;
75677721f53SPeter Wemm 			np += off;
75758f0484fSRodney W. Grimes 		}
7586b492ae4SPeter Wemm 		/* np = where to put the next part of the string in kd->argspc*/
7596b492ae4SPeter Wemm 		/* np is kinda redundant.. could use "kd->argspc + len" */
76077721f53SPeter Wemm 		memcpy(np, cp, cc);
7616b492ae4SPeter Wemm 		np += cc;	/* inc counters */
76258f0484fSRodney W. Grimes 		len += cc;
7636b492ae4SPeter Wemm 
7646b492ae4SPeter Wemm 		/*
7656b492ae4SPeter Wemm 		 * if end of string found, set the *argv pointer to the
7666b492ae4SPeter Wemm 		 * saved beginning of string, and advance. argv points to
7676b492ae4SPeter Wemm 		 * somewhere in kd->argv..  This is initially relative
7686b492ae4SPeter Wemm 		 * to the target process, but when we close it off, we set
7696b492ae4SPeter Wemm 		 * it to point in our address space.
7706b492ae4SPeter Wemm 		 */
77177721f53SPeter Wemm 		if (ep != 0) {
77277721f53SPeter Wemm 			*argv++ = ap;
77377721f53SPeter Wemm 			ap = np;
7746b492ae4SPeter Wemm 		} else {
7756b492ae4SPeter Wemm 			/* update the address relative to the target process */
77677721f53SPeter Wemm 			*argv += cc;
7776b492ae4SPeter Wemm 		}
7786b492ae4SPeter Wemm 
77958f0484fSRodney W. Grimes 		if (maxcnt > 0 && len >= maxcnt) {
78058f0484fSRodney W. Grimes 			/*
78158f0484fSRodney W. Grimes 			 * We're stopping prematurely.  Terminate the
78277721f53SPeter Wemm 			 * current string.
78358f0484fSRodney W. Grimes 			 */
78477721f53SPeter Wemm 			if (ep == 0) {
78577721f53SPeter Wemm 				*np = '\0';
78677721f53SPeter Wemm 				*argv++ = ap;
78777721f53SPeter Wemm 			}
78877721f53SPeter Wemm 			break;
78977721f53SPeter Wemm 		}
79077721f53SPeter Wemm 	}
79177721f53SPeter Wemm 	/* Make sure argv is terminated. */
79277721f53SPeter Wemm 	*argv = 0;
79358f0484fSRodney W. Grimes 	return (kd->argv);
79458f0484fSRodney W. Grimes }
79558f0484fSRodney W. Grimes 
79658f0484fSRodney W. Grimes static void
79758f0484fSRodney W. Grimes ps_str_a(p, addr, n)
79858f0484fSRodney W. Grimes 	struct ps_strings *p;
79958f0484fSRodney W. Grimes 	u_long *addr;
80058f0484fSRodney W. Grimes 	int *n;
80158f0484fSRodney W. Grimes {
80258f0484fSRodney W. Grimes 	*addr = (u_long)p->ps_argvstr;
80358f0484fSRodney W. Grimes 	*n = p->ps_nargvstr;
80458f0484fSRodney W. Grimes }
80558f0484fSRodney W. Grimes 
80658f0484fSRodney W. Grimes static void
80758f0484fSRodney W. Grimes ps_str_e(p, addr, n)
80858f0484fSRodney W. Grimes 	struct ps_strings *p;
80958f0484fSRodney W. Grimes 	u_long *addr;
81058f0484fSRodney W. Grimes 	int *n;
81158f0484fSRodney W. Grimes {
81258f0484fSRodney W. Grimes 	*addr = (u_long)p->ps_envstr;
81358f0484fSRodney W. Grimes 	*n = p->ps_nenvstr;
81458f0484fSRodney W. Grimes }
81558f0484fSRodney W. Grimes 
81658f0484fSRodney W. Grimes /*
81758f0484fSRodney W. Grimes  * Determine if the proc indicated by p is still active.
81858f0484fSRodney W. Grimes  * This test is not 100% foolproof in theory, but chances of
81958f0484fSRodney W. Grimes  * being wrong are very low.
82058f0484fSRodney W. Grimes  */
82158f0484fSRodney W. Grimes static int
8221f7d2501SKirk McKusick proc_verify(curkp)
8231f7d2501SKirk McKusick 	struct kinfo_proc *curkp;
82458f0484fSRodney W. Grimes {
8251f7d2501SKirk McKusick 	struct kinfo_proc newkp;
82660160c5eSBruce Evans 	int mib[4];
827c2ac238cSDoug Rabson 	size_t len;
82858f0484fSRodney W. Grimes 
82935e6b695SPoul-Henning Kamp 	mib[0] = CTL_KERN;
83035e6b695SPoul-Henning Kamp 	mib[1] = KERN_PROC;
83135e6b695SPoul-Henning Kamp 	mib[2] = KERN_PROC_PID;
8321f7d2501SKirk McKusick 	mib[3] = curkp->ki_pid;
8331f7d2501SKirk McKusick 	len = sizeof(newkp);
8341f7d2501SKirk McKusick 	if (sysctl(mib, 4, &newkp, &len, NULL, 0) == -1)
83558f0484fSRodney W. Grimes 		return (0);
8361f7d2501SKirk McKusick 	return (curkp->ki_pid == newkp.ki_pid &&
8371f7d2501SKirk McKusick 	    (newkp.ki_stat != SZOMB || curkp->ki_stat == SZOMB));
83858f0484fSRodney W. Grimes }
83958f0484fSRodney W. Grimes 
84058f0484fSRodney W. Grimes static char **
84158f0484fSRodney W. Grimes kvm_doargv(kd, kp, nchr, info)
84258f0484fSRodney W. Grimes 	kvm_t *kd;
8431f7d2501SKirk McKusick 	struct kinfo_proc *kp;
84458f0484fSRodney W. Grimes 	int nchr;
84577721f53SPeter Wemm 	void (*info)(struct ps_strings *, u_long *, int *);
84658f0484fSRodney W. Grimes {
8471f7d2501SKirk McKusick 	char **ap;
84858f0484fSRodney W. Grimes 	u_long addr;
84958f0484fSRodney W. Grimes 	int cnt;
85060160c5eSBruce Evans 	static struct ps_strings arginfo;
85160160c5eSBruce Evans 	static u_long ps_strings;
8527350dd84SPeter Wemm 	size_t len;
8537350dd84SPeter Wemm 
8545aaa432dSJens Schweikhardt 	if (ps_strings == 0) {
85560160c5eSBruce Evans 		len = sizeof(ps_strings);
85660160c5eSBruce Evans 		if (sysctlbyname("kern.ps_strings", &ps_strings, &len, NULL,
85760160c5eSBruce Evans 		    0) == -1)
8587350dd84SPeter Wemm 			ps_strings = PS_STRINGS;
85935e6b695SPoul-Henning Kamp 	}
86058f0484fSRodney W. Grimes 
86158f0484fSRodney W. Grimes 	/*
86258f0484fSRodney W. Grimes 	 * Pointers are stored at the top of the user stack.
86358f0484fSRodney W. Grimes 	 */
8641f7d2501SKirk McKusick 	if (kp->ki_stat == SZOMB ||
8651f7d2501SKirk McKusick 	    kvm_uread(kd, kp, ps_strings, (char *)&arginfo,
86677721f53SPeter Wemm 		      sizeof(arginfo)) != sizeof(arginfo))
86758f0484fSRodney W. Grimes 		return (0);
86858f0484fSRodney W. Grimes 
86958f0484fSRodney W. Grimes 	(*info)(&arginfo, &addr, &cnt);
87077721f53SPeter Wemm 	if (cnt == 0)
87177721f53SPeter Wemm 		return (0);
8721f7d2501SKirk McKusick 	ap = kvm_argv(kd, kp, addr, cnt, nchr);
87358f0484fSRodney W. Grimes 	/*
87458f0484fSRodney W. Grimes 	 * For live kernels, make sure this process didn't go away.
87558f0484fSRodney W. Grimes 	 */
8761f7d2501SKirk McKusick 	if (ap != 0 && ISALIVE(kd) && !proc_verify(kp))
87758f0484fSRodney W. Grimes 		ap = 0;
87858f0484fSRodney W. Grimes 	return (ap);
87958f0484fSRodney W. Grimes }
88058f0484fSRodney W. Grimes 
88158f0484fSRodney W. Grimes /*
88258f0484fSRodney W. Grimes  * Get the command args.  This code is now machine independent.
88358f0484fSRodney W. Grimes  */
88458f0484fSRodney W. Grimes char **
88558f0484fSRodney W. Grimes kvm_getargv(kd, kp, nchr)
88658f0484fSRodney W. Grimes 	kvm_t *kd;
88758f0484fSRodney W. Grimes 	const struct kinfo_proc *kp;
88858f0484fSRodney W. Grimes 	int nchr;
88958f0484fSRodney W. Grimes {
890b9df5231SPoul-Henning Kamp 	int oid[4];
891b7875890SDavid E. O'Brien 	int i;
892b7875890SDavid E. O'Brien 	size_t bufsz;
89315d2f5f0SPeter Wemm 	static unsigned long buflen;
894b9df5231SPoul-Henning Kamp 	static char *buf, *p;
895b9df5231SPoul-Henning Kamp 	static char **bufp;
896b9df5231SPoul-Henning Kamp 	static int argc;
897b9df5231SPoul-Henning Kamp 
898c4a7cdb3SPeter Wemm 	if (!ISALIVE(kd)) {
899c4a7cdb3SPeter Wemm 		_kvm_err(kd, kd->program,
900c4a7cdb3SPeter Wemm 		    "cannot read user space from dead kernel");
901c4a7cdb3SPeter Wemm 		return (0);
902c4a7cdb3SPeter Wemm 	}
903c4a7cdb3SPeter Wemm 
904b9df5231SPoul-Henning Kamp 	if (!buflen) {
905b7875890SDavid E. O'Brien 		bufsz = sizeof(buflen);
906b9df5231SPoul-Henning Kamp 		i = sysctlbyname("kern.ps_arg_cache_limit",
907b7875890SDavid E. O'Brien 		    &buflen, &bufsz, NULL, 0);
908b9df5231SPoul-Henning Kamp 		if (i == -1) {
909b7875890SDavid E. O'Brien 			buflen = 0;
910b9df5231SPoul-Henning Kamp 		} else {
911b9df5231SPoul-Henning Kamp 			buf = malloc(buflen);
912b9df5231SPoul-Henning Kamp 			if (buf == NULL)
913b9df5231SPoul-Henning Kamp 				buflen = 0;
914b9df5231SPoul-Henning Kamp 			argc = 32;
915b9df5231SPoul-Henning Kamp 			bufp = malloc(sizeof(char *) * argc);
916b9df5231SPoul-Henning Kamp 		}
917b9df5231SPoul-Henning Kamp 	}
918b9df5231SPoul-Henning Kamp 	if (buf != NULL) {
919b9df5231SPoul-Henning Kamp 		oid[0] = CTL_KERN;
920b9df5231SPoul-Henning Kamp 		oid[1] = KERN_PROC;
921b9df5231SPoul-Henning Kamp 		oid[2] = KERN_PROC_ARGS;
9221f7d2501SKirk McKusick 		oid[3] = kp->ki_pid;
923b7875890SDavid E. O'Brien 		bufsz = buflen;
924b7875890SDavid E. O'Brien 		i = sysctl(oid, 4, buf, &bufsz, 0, 0);
925b7875890SDavid E. O'Brien 		if (i == 0 && bufsz > 0) {
926b9df5231SPoul-Henning Kamp 			i = 0;
927b9df5231SPoul-Henning Kamp 			p = buf;
928b9df5231SPoul-Henning Kamp 			do {
929b9df5231SPoul-Henning Kamp 				bufp[i++] = p;
930b9df5231SPoul-Henning Kamp 				p += strlen(p) + 1;
931b9df5231SPoul-Henning Kamp 				if (i >= argc) {
932b9df5231SPoul-Henning Kamp 					argc += argc;
933b9df5231SPoul-Henning Kamp 					bufp = realloc(bufp,
934b9df5231SPoul-Henning Kamp 					    sizeof(char *) * argc);
935b9df5231SPoul-Henning Kamp 				}
936b7875890SDavid E. O'Brien 			} while (p < buf + bufsz);
937b9df5231SPoul-Henning Kamp 			bufp[i++] = 0;
938b9df5231SPoul-Henning Kamp 			return (bufp);
939b9df5231SPoul-Henning Kamp 		}
940b9df5231SPoul-Henning Kamp 	}
9411f7d2501SKirk McKusick 	if (kp->ki_flag & P_SYSTEM)
942b9df5231SPoul-Henning Kamp 		return (NULL);
94358f0484fSRodney W. Grimes 	return (kvm_doargv(kd, kp, nchr, ps_str_a));
94458f0484fSRodney W. Grimes }
94558f0484fSRodney W. Grimes 
94658f0484fSRodney W. Grimes char **
94758f0484fSRodney W. Grimes kvm_getenvv(kd, kp, nchr)
94858f0484fSRodney W. Grimes 	kvm_t *kd;
94958f0484fSRodney W. Grimes 	const struct kinfo_proc *kp;
95058f0484fSRodney W. Grimes 	int nchr;
95158f0484fSRodney W. Grimes {
95258f0484fSRodney W. Grimes 	return (kvm_doargv(kd, kp, nchr, ps_str_e));
95358f0484fSRodney W. Grimes }
95458f0484fSRodney W. Grimes 
95558f0484fSRodney W. Grimes /*
95658f0484fSRodney W. Grimes  * Read from user space.  The user context is given by p.
95758f0484fSRodney W. Grimes  */
95858f0484fSRodney W. Grimes ssize_t
9591f7d2501SKirk McKusick kvm_uread(kd, kp, uva, buf, len)
96058f0484fSRodney W. Grimes 	kvm_t *kd;
9611f7d2501SKirk McKusick 	struct kinfo_proc *kp;
962be04b6d1SDavid E. O'Brien 	u_long uva;
963be04b6d1SDavid E. O'Brien 	char *buf;
964be04b6d1SDavid E. O'Brien 	size_t len;
96558f0484fSRodney W. Grimes {
966be04b6d1SDavid E. O'Brien 	char *cp;
967338c7541SDavid Greenman 	char procfile[MAXPATHLEN];
968338c7541SDavid Greenman 	ssize_t amount;
969338c7541SDavid Greenman 	int fd;
97058f0484fSRodney W. Grimes 
9717350dd84SPeter Wemm 	if (!ISALIVE(kd)) {
972d7fb4b13SBruce Evans 		_kvm_err(kd, kd->program,
973d7fb4b13SBruce Evans 		    "cannot read user space from dead kernel");
9747350dd84SPeter Wemm 		return (0);
9757350dd84SPeter Wemm 	}
9767350dd84SPeter Wemm 
9771f7d2501SKirk McKusick 	sprintf(procfile, "/proc/%d/mem", kp->ki_pid);
978338c7541SDavid Greenman 	fd = open(procfile, O_RDONLY, 0);
979338c7541SDavid Greenman 	if (fd < 0) {
980338c7541SDavid Greenman 		_kvm_err(kd, kd->program, "cannot open %s", procfile);
98158f0484fSRodney W. Grimes 		return (0);
98258f0484fSRodney W. Grimes 	}
983338c7541SDavid Greenman 
984d7fb4b13SBruce Evans 	cp = buf;
985338c7541SDavid Greenman 	while (len > 0) {
986d7fb4b13SBruce Evans 		errno = 0;
987ec4f2251SJoerg Wunsch 		if (lseek(fd, (off_t)uva, 0) == -1 && errno != 0) {
988d7fb4b13SBruce Evans 			_kvm_err(kd, kd->program, "invalid address (%x) in %s",
989d7fb4b13SBruce Evans 			    uva, procfile);
99058f0484fSRodney W. Grimes 			break;
99158f0484fSRodney W. Grimes 		}
992567127faSDavid Greenman 		amount = read(fd, cp, len);
993338c7541SDavid Greenman 		if (amount < 0) {
994d7fb4b13SBruce Evans 			_kvm_syserr(kd, kd->program, "error reading %s",
995d7fb4b13SBruce Evans 			    procfile);
996d7fb4b13SBruce Evans 			break;
997d7fb4b13SBruce Evans 		}
998d7fb4b13SBruce Evans 		if (amount == 0) {
999d7fb4b13SBruce Evans 			_kvm_err(kd, kd->program, "EOF reading %s", procfile);
1000338c7541SDavid Greenman 			break;
1001338c7541SDavid Greenman 		}
1002338c7541SDavid Greenman 		cp += amount;
1003338c7541SDavid Greenman 		uva += amount;
1004338c7541SDavid Greenman 		len -= amount;
1005338c7541SDavid Greenman 	}
1006338c7541SDavid Greenman 
1007338c7541SDavid Greenman 	close(fd);
1008d7fb4b13SBruce Evans 	return ((ssize_t)(cp - buf));
100958f0484fSRodney W. Grimes }
1010