xref: /freebsd/lib/libkvm/kvm_proc.c (revision fb0e1892d9e31cdba013c27b24c47c86838924a8)
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>
57413628a7SBjoern A. Zeeb #include <sys/cpuset.h>
5858f0484fSRodney W. Grimes #include <sys/user.h>
5958f0484fSRodney W. Grimes #include <sys/proc.h>
60413628a7SBjoern A. Zeeb #define	_WANT_PRISON	/* make jail.h give us 'struct prison' */
61413628a7SBjoern A. Zeeb #include <sys/jail.h>
6258f0484fSRodney W. Grimes #include <sys/exec.h>
6358f0484fSRodney W. Grimes #include <sys/stat.h>
64276de18cSGarance A Drosehn #include <sys/sysent.h>
6558f0484fSRodney W. Grimes #include <sys/ioctl.h>
6658f0484fSRodney W. Grimes #include <sys/tty.h>
67338c7541SDavid Greenman #include <sys/file.h>
682bdd5609SPeter Wemm #include <sys/conf.h>
69b4490c6eSKonstantin Belousov #define	_WANT_KW_EXITCODE
70b4490c6eSKonstantin Belousov #include <sys/wait.h>
7151295a4dSJordan K. Hubbard #include <stdio.h>
7251295a4dSJordan K. Hubbard #include <stdlib.h>
7358f0484fSRodney W. Grimes #include <unistd.h>
7458f0484fSRodney W. Grimes #include <nlist.h>
7558f0484fSRodney W. Grimes #include <kvm.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;
90de788839SUlrich Spörlein static uint64_t cpu_tick_frequency;
91de788839SUlrich Spörlein 
92de788839SUlrich Spörlein /*
93de788839SUlrich Spörlein  * From sys/kern/kern_tc.c. Depends on cpu_tick_frequency, which is
94de788839SUlrich Spörlein  * read/initialized before this function is ever called.
95de788839SUlrich Spörlein  */
96de788839SUlrich Spörlein static uint64_t
97de788839SUlrich Spörlein cputick2usec(uint64_t tick)
98de788839SUlrich Spörlein {
99de788839SUlrich Spörlein 
100de788839SUlrich Spörlein 	if (cpu_tick_frequency == 0)
101de788839SUlrich Spörlein 		return (0);
102de788839SUlrich Spörlein 	if (tick > 18446744073709551)		/* floor(2^64 / 1000) */
103de788839SUlrich Spörlein 		return (tick / (cpu_tick_frequency / 1000000));
104de788839SUlrich Spörlein 	else if (tick > 18446744073709)	/* floor(2^64 / 1000000) */
105de788839SUlrich Spörlein 		return ((tick * 1000) / (cpu_tick_frequency / 1000));
106de788839SUlrich Spörlein 	else
107de788839SUlrich Spörlein 		return ((tick * 1000000) / cpu_tick_frequency);
108de788839SUlrich Spörlein }
10984a0b303SJeff Roberson 
11058f0484fSRodney W. Grimes /*
11158f0484fSRodney W. Grimes  * Read proc's from memory file into buffer bp, which has space to hold
11258f0484fSRodney W. Grimes  * at most maxcnt procs.
11358f0484fSRodney W. Grimes  */
11458f0484fSRodney W. Grimes static int
115c10970ddSUlrich Spörlein kvm_proclist(kvm_t *kd, int what, int arg, struct proc *p,
116c10970ddSUlrich Spörlein     struct kinfo_proc *bp, int maxcnt)
11758f0484fSRodney W. Grimes {
118be04b6d1SDavid E. O'Brien 	int cnt = 0;
1191f7d2501SKirk McKusick 	struct kinfo_proc kinfo_proc, *kp;
12058f0484fSRodney W. Grimes 	struct pgrp pgrp;
12158f0484fSRodney W. Grimes 	struct session sess;
1222bdd5609SPeter Wemm 	struct cdev t_cdev;
12358f0484fSRodney W. Grimes 	struct tty tty;
1241f7d2501SKirk McKusick 	struct vmspace vmspace;
125840558b9SJohn Baldwin 	struct sigacts sigacts;
126c10970ddSUlrich Spörlein #if 0
1271f7d2501SKirk McKusick 	struct pstats pstats;
128c10970ddSUlrich Spörlein #endif
1291f7d2501SKirk McKusick 	struct ucred ucred;
130f8197bf0SPawel Jakub Dawidek 	struct prison pr;
13171fad9fdSJulian Elischer 	struct thread mtd;
13258f0484fSRodney W. Grimes 	struct proc proc;
133a58930d8STor Egge 	struct proc pproc;
134276de18cSGarance A Drosehn 	struct sysentvec sysent;
135276de18cSGarance A Drosehn 	char svname[KI_EMULNAMELEN];
13658f0484fSRodney W. Grimes 
1371f7d2501SKirk McKusick 	kp = &kinfo_proc;
1381f7d2501SKirk McKusick 	kp->ki_structsize = sizeof(kinfo_proc);
1397ab24ea3SJulian Elischer 	/*
1407ab24ea3SJulian Elischer 	 * Loop on the processes. this is completely broken because we need to be
1417ab24ea3SJulian Elischer 	 * able to loop on the threads and merge the ones that are the same process some how.
1427ab24ea3SJulian Elischer 	 */
14342680b3aSBen Smithurst 	for (; cnt < maxcnt && p != NULL; p = LIST_NEXT(&proc, p_list)) {
1446f8c6a69SPeter Wemm 		memset(kp, 0, sizeof *kp);
14558f0484fSRodney W. Grimes 		if (KREAD(kd, (u_long)p, &proc)) {
146c10970ddSUlrich Spörlein 			_kvm_err(kd, kd->program, "can't read proc at %p", p);
14758f0484fSRodney W. Grimes 			return (-1);
14858f0484fSRodney W. Grimes 		}
1499fb9ea1cSAndriy Gapon 		if (proc.p_state == PRS_NEW)
1509fb9ea1cSAndriy Gapon 			continue;
15158551c03SJulian Elischer 		if (proc.p_state != PRS_ZOMBIE) {
1523daf63fcSJulian Elischer 			if (KREAD(kd, (u_long)TAILQ_FIRST(&proc.p_threads),
15371fad9fdSJulian Elischer 			    &mtd)) {
15458551c03SJulian Elischer 				_kvm_err(kd, kd->program,
155c10970ddSUlrich Spörlein 				    "can't read thread at %p",
1563daf63fcSJulian Elischer 				    TAILQ_FIRST(&proc.p_threads));
1573daf63fcSJulian Elischer 				return (-1);
1583daf63fcSJulian Elischer 			}
15958551c03SJulian Elischer 		}
160b1fc0ec1SRobert Watson 		if (KREAD(kd, (u_long)proc.p_ucred, &ucred) == 0) {
161b1fc0ec1SRobert Watson 			kp->ki_ruid = ucred.cr_ruid;
162b1fc0ec1SRobert Watson 			kp->ki_svuid = ucred.cr_svuid;
163b1fc0ec1SRobert Watson 			kp->ki_rgid = ucred.cr_rgid;
164b1fc0ec1SRobert Watson 			kp->ki_svgid = ucred.cr_svgid;
1651b5768beSBrooks Davis 			kp->ki_cr_flags = ucred.cr_flags;
1661b5768beSBrooks Davis 			if (ucred.cr_ngroups > KI_NGROUPS) {
1671b5768beSBrooks Davis 				kp->ki_ngroups = KI_NGROUPS;
1681b5768beSBrooks Davis 				kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
16968a5cc39SBrooks Davis 			} else
1701f7d2501SKirk McKusick 				kp->ki_ngroups = ucred.cr_ngroups;
171d534b0c2SBrooks Davis 			kvm_read(kd, (u_long)ucred.cr_groups, kp->ki_groups,
1721b5768beSBrooks Davis 			    kp->ki_ngroups * sizeof(gid_t));
1731f7d2501SKirk McKusick 			kp->ki_uid = ucred.cr_uid;
174f8197bf0SPawel Jakub Dawidek 			if (ucred.cr_prison != NULL) {
175f8197bf0SPawel Jakub Dawidek 				if (KREAD(kd, (u_long)ucred.cr_prison, &pr)) {
176f8197bf0SPawel Jakub Dawidek 					_kvm_err(kd, kd->program,
177c10970ddSUlrich Spörlein 					    "can't read prison at %p",
178f8197bf0SPawel Jakub Dawidek 					    ucred.cr_prison);
179f8197bf0SPawel Jakub Dawidek 					return (-1);
180f8197bf0SPawel Jakub Dawidek 				}
181f8197bf0SPawel Jakub Dawidek 				kp->ki_jid = pr.pr_id;
182f8197bf0SPawel Jakub Dawidek 			}
1831f7d2501SKirk McKusick 		}
18458f0484fSRodney W. Grimes 
185694127f8SDaniel Eischen 		switch(what & ~KERN_PROC_INC_THREAD) {
18658f0484fSRodney W. Grimes 
187276de18cSGarance A Drosehn 		case KERN_PROC_GID:
188276de18cSGarance A Drosehn 			if (kp->ki_groups[0] != (gid_t)arg)
189276de18cSGarance A Drosehn 				continue;
190276de18cSGarance A Drosehn 			break;
191276de18cSGarance A Drosehn 
19258f0484fSRodney W. Grimes 		case KERN_PROC_PID:
19358f0484fSRodney W. Grimes 			if (proc.p_pid != (pid_t)arg)
19458f0484fSRodney W. Grimes 				continue;
19558f0484fSRodney W. Grimes 			break;
19658f0484fSRodney W. Grimes 
197276de18cSGarance A Drosehn 		case KERN_PROC_RGID:
198276de18cSGarance A Drosehn 			if (kp->ki_rgid != (gid_t)arg)
199276de18cSGarance A Drosehn 				continue;
200276de18cSGarance A Drosehn 			break;
201276de18cSGarance A Drosehn 
20258f0484fSRodney W. Grimes 		case KERN_PROC_UID:
2031f7d2501SKirk McKusick 			if (kp->ki_uid != (uid_t)arg)
20458f0484fSRodney W. Grimes 				continue;
20558f0484fSRodney W. Grimes 			break;
20658f0484fSRodney W. Grimes 
20758f0484fSRodney W. Grimes 		case KERN_PROC_RUID:
2081f7d2501SKirk McKusick 			if (kp->ki_ruid != (uid_t)arg)
20958f0484fSRodney W. Grimes 				continue;
21058f0484fSRodney W. Grimes 			break;
21158f0484fSRodney W. Grimes 		}
21258f0484fSRodney W. Grimes 		/*
21358f0484fSRodney W. Grimes 		 * We're going to add another proc to the set.  If this
21458f0484fSRodney W. Grimes 		 * will overflow the buffer, assume the reason is because
21558f0484fSRodney W. Grimes 		 * nprocs (or the proc list) is corrupt and declare an error.
21658f0484fSRodney W. Grimes 		 */
21758f0484fSRodney W. Grimes 		if (cnt >= maxcnt) {
21858f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "nprocs corrupt");
21958f0484fSRodney W. Grimes 			return (-1);
22058f0484fSRodney W. Grimes 		}
22158f0484fSRodney W. Grimes 		/*
2221f7d2501SKirk McKusick 		 * gather kinfo_proc
22358f0484fSRodney W. Grimes 		 */
2241f7d2501SKirk McKusick 		kp->ki_paddr = p;
2257a62aa8aSDavid Schultz 		kp->ki_addr = 0;	/* XXX uarea */
226b40ce416SJulian Elischer 		/* kp->ki_kstack = proc.p_thread.td_kstack; XXXKSE */
2271f7d2501SKirk McKusick 		kp->ki_args = proc.p_args;
2288b7a975eSJohn Baldwin 		kp->ki_tracep = proc.p_tracevp;
2291f7d2501SKirk McKusick 		kp->ki_textvp = proc.p_textvp;
2301f7d2501SKirk McKusick 		kp->ki_fd = proc.p_fd;
2311f7d2501SKirk McKusick 		kp->ki_vmspace = proc.p_vmspace;
232840558b9SJohn Baldwin 		if (proc.p_sigacts != NULL) {
233840558b9SJohn Baldwin 			if (KREAD(kd, (u_long)proc.p_sigacts, &sigacts)) {
2341f7d2501SKirk McKusick 				_kvm_err(kd, kd->program,
235c10970ddSUlrich Spörlein 				    "can't read sigacts at %p", proc.p_sigacts);
2361f7d2501SKirk McKusick 				return (-1);
2371f7d2501SKirk McKusick 			}
238840558b9SJohn Baldwin 			kp->ki_sigignore = sigacts.ps_sigignore;
239840558b9SJohn Baldwin 			kp->ki_sigcatch = sigacts.ps_sigcatch;
2401f7d2501SKirk McKusick 		}
2418ef6b142SJeff Roberson #if 0
242b61ce5b0SJeff Roberson 		if ((proc.p_flag & P_INMEM) && proc.p_stats != NULL) {
2431f7d2501SKirk McKusick 			if (KREAD(kd, (u_long)proc.p_stats, &pstats)) {
2441f7d2501SKirk McKusick 				_kvm_err(kd, kd->program,
2451f7d2501SKirk McKusick 				    "can't read stats at %x", proc.p_stats);
2461f7d2501SKirk McKusick 				return (-1);
2471f7d2501SKirk McKusick 			}
2481f7d2501SKirk McKusick 			kp->ki_start = pstats.p_start;
24909ff687cSJohn Baldwin 
25009ff687cSJohn Baldwin 			/*
25109ff687cSJohn Baldwin 			 * XXX: The times here are probably zero and need
25209ff687cSJohn Baldwin 			 * to be calculated from the raw data in p_rux and
25309ff687cSJohn Baldwin 			 * p_crux.
25409ff687cSJohn Baldwin 			 */
2551f7d2501SKirk McKusick 			kp->ki_rusage = pstats.p_ru;
256276de18cSGarance A Drosehn 			kp->ki_childstime = pstats.p_cru.ru_stime;
257276de18cSGarance A Drosehn 			kp->ki_childutime = pstats.p_cru.ru_utime;
258276de18cSGarance A Drosehn 			/* Some callers want child-times in a single value */
259276de18cSGarance A Drosehn 			timeradd(&kp->ki_childstime, &kp->ki_childutime,
260276de18cSGarance A Drosehn 			    &kp->ki_childtime);
2611f7d2501SKirk McKusick 		}
2628ef6b142SJeff Roberson #endif
263a58930d8STor Egge 		if (proc.p_oppid)
2641f7d2501SKirk McKusick 			kp->ki_ppid = proc.p_oppid;
265a58930d8STor Egge 		else if (proc.p_pptr) {
266a58930d8STor Egge 			if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) {
2671f7d2501SKirk McKusick 				_kvm_err(kd, kd->program,
268c10970ddSUlrich Spörlein 				    "can't read pproc at %p", proc.p_pptr);
269a58930d8STor Egge 				return (-1);
270a58930d8STor Egge 			}
2711f7d2501SKirk McKusick 			kp->ki_ppid = pproc.p_pid;
272a58930d8STor Egge 		} else
2731f7d2501SKirk McKusick 			kp->ki_ppid = 0;
2746f8c6a69SPeter Wemm 		if (proc.p_pgrp == NULL)
2756f8c6a69SPeter Wemm 			goto nopgrp;
2766f8c6a69SPeter Wemm 		if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
277c10970ddSUlrich Spörlein 			_kvm_err(kd, kd->program, "can't read pgrp at %p",
2786f8c6a69SPeter Wemm 				 proc.p_pgrp);
2796f8c6a69SPeter Wemm 			return (-1);
2806f8c6a69SPeter Wemm 		}
2811f7d2501SKirk McKusick 		kp->ki_pgid = pgrp.pg_id;
2821f7d2501SKirk McKusick 		kp->ki_jobc = pgrp.pg_jobc;
28358f0484fSRodney W. Grimes 		if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
284c10970ddSUlrich Spörlein 			_kvm_err(kd, kd->program, "can't read session at %p",
28558f0484fSRodney W. Grimes 				pgrp.pg_session);
28658f0484fSRodney W. Grimes 			return (-1);
28758f0484fSRodney W. Grimes 		}
2881f7d2501SKirk McKusick 		kp->ki_sid = sess.s_sid;
2891f7d2501SKirk McKusick 		(void)memcpy(kp->ki_login, sess.s_login,
2901f7d2501SKirk McKusick 						sizeof(kp->ki_login));
2911f7d2501SKirk McKusick 		kp->ki_kiflag = sess.s_ttyvp ? KI_CTTY : 0;
2921f7d2501SKirk McKusick 		if (sess.s_leader == p)
2931f7d2501SKirk McKusick 			kp->ki_kiflag |= KI_SLEADER;
29458f0484fSRodney W. Grimes 		if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) {
29558f0484fSRodney W. Grimes 			if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
29658f0484fSRodney W. Grimes 				_kvm_err(kd, kd->program,
297c10970ddSUlrich Spörlein 					 "can't read tty at %p", sess.s_ttyp);
29858f0484fSRodney W. Grimes 				return (-1);
29958f0484fSRodney W. Grimes 			}
3002bdd5609SPeter Wemm 			if (tty.t_dev != NULL) {
3012bdd5609SPeter Wemm 				if (KREAD(kd, (u_long)tty.t_dev, &t_cdev)) {
3022bdd5609SPeter Wemm 					_kvm_err(kd, kd->program,
303c10970ddSUlrich Spörlein 						 "can't read cdev at %p",
3042bdd5609SPeter Wemm 						tty.t_dev);
3052bdd5609SPeter Wemm 					return (-1);
3062bdd5609SPeter Wemm 				}
3079c4fb661SPoul-Henning Kamp #if 0
3082bdd5609SPeter Wemm 				kp->ki_tdev = t_cdev.si_udev;
3099c4fb661SPoul-Henning Kamp #else
3102cfe3fdaSPeter Wemm 				kp->ki_tdev = NODEV;
3119c4fb661SPoul-Henning Kamp #endif
3122bdd5609SPeter Wemm 			}
31358f0484fSRodney W. Grimes 			if (tty.t_pgrp != NULL) {
31458f0484fSRodney W. Grimes 				if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
31558f0484fSRodney W. Grimes 					_kvm_err(kd, kd->program,
316c10970ddSUlrich Spörlein 						 "can't read tpgrp at %p",
31758f0484fSRodney W. Grimes 						tty.t_pgrp);
31858f0484fSRodney W. Grimes 					return (-1);
31958f0484fSRodney W. Grimes 				}
3201f7d2501SKirk McKusick 				kp->ki_tpgid = pgrp.pg_id;
32158f0484fSRodney W. Grimes 			} else
3221f7d2501SKirk McKusick 				kp->ki_tpgid = -1;
3231f7d2501SKirk McKusick 			if (tty.t_session != NULL) {
3241f7d2501SKirk McKusick 				if (KREAD(kd, (u_long)tty.t_session, &sess)) {
3251f7d2501SKirk McKusick 					_kvm_err(kd, kd->program,
326c10970ddSUlrich Spörlein 					    "can't read session at %p",
3271f7d2501SKirk McKusick 					    tty.t_session);
3281f7d2501SKirk McKusick 					return (-1);
3291f7d2501SKirk McKusick 				}
3301f7d2501SKirk McKusick 				kp->ki_tsid = sess.s_sid;
3311f7d2501SKirk McKusick 			}
3326f8c6a69SPeter Wemm 		} else {
3336f8c6a69SPeter Wemm nopgrp:
3341f7d2501SKirk McKusick 			kp->ki_tdev = NODEV;
3356f8c6a69SPeter Wemm 		}
33671fad9fdSJulian Elischer 		if ((proc.p_state != PRS_ZOMBIE) && mtd.td_wmesg)
33771fad9fdSJulian Elischer 			(void)kvm_read(kd, (u_long)mtd.td_wmesg,
3381f7d2501SKirk McKusick 			    kp->ki_wmesg, WMESGLEN);
33958f0484fSRodney W. Grimes 
34058f0484fSRodney W. Grimes 		(void)kvm_read(kd, (u_long)proc.p_vmspace,
3411f7d2501SKirk McKusick 		    (char *)&vmspace, sizeof(vmspace));
3421f7d2501SKirk McKusick 		kp->ki_size = vmspace.vm_map.size;
3435f494640SSean Bruno 		/*
3445f494640SSean Bruno 		 * Approximate the kernel's method of calculating
3455f494640SSean Bruno 		 * this field.
3465f494640SSean Bruno 		 */
3475f494640SSean Bruno #define		pmap_resident_count(pm) ((pm)->pm_stats.resident_count)
3485f494640SSean Bruno 		kp->ki_rssize = pmap_resident_count(&vmspace.vm_pmap);
3491f7d2501SKirk McKusick 		kp->ki_swrss = vmspace.vm_swrss;
3501f7d2501SKirk McKusick 		kp->ki_tsize = vmspace.vm_tsize;
3511f7d2501SKirk McKusick 		kp->ki_dsize = vmspace.vm_dsize;
3521f7d2501SKirk McKusick 		kp->ki_ssize = vmspace.vm_ssize;
35358f0484fSRodney W. Grimes 
354694127f8SDaniel Eischen 		switch (what & ~KERN_PROC_INC_THREAD) {
35558f0484fSRodney W. Grimes 
35658f0484fSRodney W. Grimes 		case KERN_PROC_PGRP:
3571f7d2501SKirk McKusick 			if (kp->ki_pgid != (pid_t)arg)
35858f0484fSRodney W. Grimes 				continue;
35958f0484fSRodney W. Grimes 			break;
36058f0484fSRodney W. Grimes 
361276de18cSGarance A Drosehn 		case KERN_PROC_SESSION:
362276de18cSGarance A Drosehn 			if (kp->ki_sid != (pid_t)arg)
363276de18cSGarance A Drosehn 				continue;
364276de18cSGarance A Drosehn 			break;
365276de18cSGarance A Drosehn 
36658f0484fSRodney W. Grimes 		case KERN_PROC_TTY:
36758f0484fSRodney W. Grimes 			if ((proc.p_flag & P_CONTROLT) == 0 ||
3681f7d2501SKirk McKusick 			     kp->ki_tdev != (dev_t)arg)
36958f0484fSRodney W. Grimes 				continue;
37058f0484fSRodney W. Grimes 			break;
37158f0484fSRodney W. Grimes 		}
372b7e7c21aSGarance A Drosehn 		if (proc.p_comm[0] != 0)
373b7e7c21aSGarance A Drosehn 			strlcpy(kp->ki_comm, proc.p_comm, MAXCOMLEN);
374276de18cSGarance A Drosehn 		(void)kvm_read(kd, (u_long)proc.p_sysent, (char *)&sysent,
375276de18cSGarance A Drosehn 		    sizeof(sysent));
376276de18cSGarance A Drosehn 		(void)kvm_read(kd, (u_long)sysent.sv_name, (char *)&svname,
377276de18cSGarance A Drosehn 		    sizeof(svname));
378276de18cSGarance A Drosehn 		if (svname[0] != 0)
379276de18cSGarance A Drosehn 			strlcpy(kp->ki_emul, svname, KI_EMULNAMELEN);
380bff4151cSJulian Elischer 		if ((proc.p_state != PRS_ZOMBIE) &&
38171fad9fdSJulian Elischer 		    (mtd.td_blocked != 0)) {
3820d632649SJohn Baldwin 			kp->ki_kiflag |= KI_LOCKBLOCK;
3830d632649SJohn Baldwin 			if (mtd.td_lockname)
384bff4151cSJulian Elischer 				(void)kvm_read(kd,
3850d632649SJohn Baldwin 				    (u_long)mtd.td_lockname,
3860d632649SJohn Baldwin 				    kp->ki_lockname, LOCKNAMELEN);
3870d632649SJohn Baldwin 			kp->ki_lockname[LOCKNAMELEN] = 0;
3881f7d2501SKirk McKusick 		}
389de788839SUlrich Spörlein 		kp->ki_runtime = cputick2usec(proc.p_rux.rux_runtime);
3901f7d2501SKirk McKusick 		kp->ki_pid = proc.p_pid;
391e8a58a83SJuli Mallett 		kp->ki_siglist = proc.p_siglist;
39242d3ad71SJeff Roberson 		SIGSETOR(kp->ki_siglist, mtd.td_siglist);
39331a9779eSJeff Roberson 		kp->ki_sigmask = mtd.td_sigmask;
394b4490c6eSKonstantin Belousov 		kp->ki_xstat = KW_EXITCODE(proc.p_xexit, proc.p_xsig);
3951f7d2501SKirk McKusick 		kp->ki_acflag = proc.p_acflag;
3964f0db5e0SJulian Elischer 		kp->ki_lock = proc.p_lock;
397bff4151cSJulian Elischer 		if (proc.p_state != PRS_ZOMBIE) {
39884a0b303SJeff Roberson 			kp->ki_swtime = (ticks - proc.p_swtick) / hz;
3996143c383SJulian Elischer 			kp->ki_flag = proc.p_flag;
400b61ce5b0SJeff Roberson 			kp->ki_sflag = 0;
40130105366SJulian Elischer 			kp->ki_nice = proc.p_nice;
4021f7d2501SKirk McKusick 			kp->ki_traceflag = proc.p_traceflag;
4036143c383SJulian Elischer 			if (proc.p_state == PRS_NORMAL) {
40471fad9fdSJulian Elischer 				if (TD_ON_RUNQ(&mtd) ||
40571fad9fdSJulian Elischer 				    TD_CAN_RUN(&mtd) ||
40671fad9fdSJulian Elischer 				    TD_IS_RUNNING(&mtd)) {
407e602ba25SJulian Elischer 					kp->ki_stat = SRUN;
4084f0db5e0SJulian Elischer 				} else if (mtd.td_state ==
4094f0db5e0SJulian Elischer 				    TDS_INHIBITED) {
41071fad9fdSJulian Elischer 					if (P_SHOULDSTOP(&proc)) {
411e602ba25SJulian Elischer 						kp->ki_stat = SSTOP;
4124f0db5e0SJulian Elischer 					} else if (
4134f0db5e0SJulian Elischer 					    TD_IS_SLEEPING(&mtd)) {
41471fad9fdSJulian Elischer 						kp->ki_stat = SSLEEP;
4150d632649SJohn Baldwin 					} else if (TD_ON_LOCK(&mtd)) {
4160d632649SJohn Baldwin 						kp->ki_stat = SLOCK;
417e602ba25SJulian Elischer 					} else {
418e602ba25SJulian Elischer 						kp->ki_stat = SWAIT;
419e602ba25SJulian Elischer 					}
42071fad9fdSJulian Elischer 				}
421e602ba25SJulian Elischer 			} else {
422e602ba25SJulian Elischer 				kp->ki_stat = SIDL;
423e602ba25SJulian Elischer 			}
4244f0db5e0SJulian Elischer 			/* Stuff from the thread */
42571fad9fdSJulian Elischer 			kp->ki_pri.pri_level = mtd.td_priority;
42671fad9fdSJulian Elischer 			kp->ki_pri.pri_native = mtd.td_base_pri;
42771fad9fdSJulian Elischer 			kp->ki_lastcpu = mtd.td_lastcpu;
4284f0db5e0SJulian Elischer 			kp->ki_wchan = mtd.td_wchan;
4297ab24ea3SJulian Elischer 			if (mtd.td_name[0] != 0)
430925af544SBjoern A. Zeeb 				strlcpy(kp->ki_tdname, mtd.td_name, MAXCOMLEN);
431a0ddbf49SJulian Elischer 			kp->ki_oncpu = mtd.td_oncpu;
4327ab24ea3SJulian Elischer 			if (mtd.td_name[0] != '\0')
433925af544SBjoern A. Zeeb 				strlcpy(kp->ki_tdname, mtd.td_name, sizeof(kp->ki_tdname));
434ed062c8dSJulian Elischer 			kp->ki_pctcpu = 0;
435ed062c8dSJulian Elischer 			kp->ki_rqindex = 0;
436e77f9fedSAdrian Chadd 
437e77f9fedSAdrian Chadd 			/*
438e77f9fedSAdrian Chadd 			 * Note: legacy fields; wraps at NO_CPU_OLD or the
439e77f9fedSAdrian Chadd 			 * old max CPU value as appropriate
440e77f9fedSAdrian Chadd 			 */
441e77f9fedSAdrian Chadd 			if (mtd.td_lastcpu == NOCPU)
442e77f9fedSAdrian Chadd 				kp->ki_lastcpu_old = NOCPU_OLD;
443e77f9fedSAdrian Chadd 			else if (mtd.td_lastcpu > MAXCPU_OLD)
444e77f9fedSAdrian Chadd 				kp->ki_lastcpu_old = MAXCPU_OLD;
445e77f9fedSAdrian Chadd 			else
446e77f9fedSAdrian Chadd 				kp->ki_lastcpu_old = mtd.td_lastcpu;
447e77f9fedSAdrian Chadd 
448e77f9fedSAdrian Chadd 			if (mtd.td_oncpu == NOCPU)
449e77f9fedSAdrian Chadd 				kp->ki_oncpu_old = NOCPU_OLD;
450e77f9fedSAdrian Chadd 			else if (mtd.td_oncpu > MAXCPU_OLD)
451e77f9fedSAdrian Chadd 				kp->ki_oncpu_old = MAXCPU_OLD;
452e77f9fedSAdrian Chadd 			else
453e77f9fedSAdrian Chadd 				kp->ki_oncpu_old = mtd.td_oncpu;
4544f0db5e0SJulian Elischer 		} else {
4556143c383SJulian Elischer 			kp->ki_stat = SZOMB;
4566143c383SJulian Elischer 		}
4571f7d2501SKirk McKusick 		bcopy(&kinfo_proc, bp, sizeof(kinfo_proc));
45858f0484fSRodney W. Grimes 		++bp;
45958f0484fSRodney W. Grimes 		++cnt;
46058f0484fSRodney W. Grimes 	}
46158f0484fSRodney W. Grimes 	return (cnt);
46258f0484fSRodney W. Grimes }
46358f0484fSRodney W. Grimes 
46458f0484fSRodney W. Grimes /*
46558f0484fSRodney W. Grimes  * Build proc info array by reading in proc list from a crash dump.
46658f0484fSRodney W. Grimes  * Return number of procs read.  maxcnt is the max we will read.
46758f0484fSRodney W. Grimes  */
46858f0484fSRodney W. Grimes static int
469c10970ddSUlrich Spörlein kvm_deadprocs(kvm_t *kd, int what, int arg, u_long a_allproc,
470c10970ddSUlrich Spörlein     u_long a_zombproc, int maxcnt)
47158f0484fSRodney W. Grimes {
472be04b6d1SDavid E. O'Brien 	struct kinfo_proc *bp = kd->procbase;
473be04b6d1SDavid E. O'Brien 	int acnt, zcnt;
47458f0484fSRodney W. Grimes 	struct proc *p;
47558f0484fSRodney W. Grimes 
47658f0484fSRodney W. Grimes 	if (KREAD(kd, a_allproc, &p)) {
47758f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "cannot read allproc");
47858f0484fSRodney W. Grimes 		return (-1);
47958f0484fSRodney W. Grimes 	}
48058f0484fSRodney W. Grimes 	acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
48158f0484fSRodney W. Grimes 	if (acnt < 0)
48258f0484fSRodney W. Grimes 		return (acnt);
48358f0484fSRodney W. Grimes 
48458f0484fSRodney W. Grimes 	if (KREAD(kd, a_zombproc, &p)) {
48558f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "cannot read zombproc");
48658f0484fSRodney W. Grimes 		return (-1);
48758f0484fSRodney W. Grimes 	}
48858f0484fSRodney W. Grimes 	zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt);
48958f0484fSRodney W. Grimes 	if (zcnt < 0)
49058f0484fSRodney W. Grimes 		zcnt = 0;
49158f0484fSRodney W. Grimes 
49258f0484fSRodney W. Grimes 	return (acnt + zcnt);
49358f0484fSRodney W. Grimes }
49458f0484fSRodney W. Grimes 
49558f0484fSRodney W. Grimes struct kinfo_proc *
496c10970ddSUlrich Spörlein kvm_getprocs(kvm_t *kd, int op, int arg, int *cnt)
49758f0484fSRodney W. Grimes {
498c2ac238cSDoug Rabson 	int mib[4], st, nprocs;
499767993ecSMikolaj Golub 	size_t size, osize;
500694127f8SDaniel Eischen 	int temp_op;
50158f0484fSRodney W. Grimes 
50258f0484fSRodney W. Grimes 	if (kd->procbase != 0) {
50358f0484fSRodney W. Grimes 		free((void *)kd->procbase);
50458f0484fSRodney W. Grimes 		/*
50558f0484fSRodney W. Grimes 		 * Clear this pointer in case this call fails.  Otherwise,
50658f0484fSRodney W. Grimes 		 * kvm_close() will free it again.
50758f0484fSRodney W. Grimes 		 */
50858f0484fSRodney W. Grimes 		kd->procbase = 0;
50958f0484fSRodney W. Grimes 	}
51058f0484fSRodney W. Grimes 	if (ISALIVE(kd)) {
51158f0484fSRodney W. Grimes 		size = 0;
51258f0484fSRodney W. Grimes 		mib[0] = CTL_KERN;
51358f0484fSRodney W. Grimes 		mib[1] = KERN_PROC;
51458f0484fSRodney W. Grimes 		mib[2] = op;
51558f0484fSRodney W. Grimes 		mib[3] = arg;
516694127f8SDaniel Eischen 		temp_op = op & ~KERN_PROC_INC_THREAD;
517694127f8SDaniel Eischen 		st = sysctl(mib,
518694127f8SDaniel Eischen 		    temp_op == KERN_PROC_ALL || temp_op == KERN_PROC_PROC ?
519f2dd06abSTim J. Robbins 		    3 : 4, NULL, &size, NULL, 0);
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 can't continue with a size of 0 because we pass
52621687047SDima Dorfman 		 * it to realloc() (via _kvm_realloc()), and passing 0
52721687047SDima Dorfman 		 * to realloc() results in undefined behavior.
52821687047SDima Dorfman 		 */
52921687047SDima Dorfman 		if (size == 0) {
53021687047SDima Dorfman 			/*
53121687047SDima Dorfman 			 * XXX: We should probably return an invalid,
53221687047SDima Dorfman 			 * but non-NULL, pointer here so any client
53321687047SDima Dorfman 			 * program trying to dereference it will
53421687047SDima Dorfman 			 * crash.  However, _kvm_freeprocs() calls
53521687047SDima Dorfman 			 * free() on kd->procbase if it isn't NULL,
53621687047SDima Dorfman 			 * and free()'ing a junk pointer isn't good.
53721687047SDima Dorfman 			 * Then again, _kvm_freeprocs() isn't used
53821687047SDima Dorfman 			 * anywhere . . .
53921687047SDima Dorfman 			 */
54021687047SDima Dorfman 			kd->procbase = _kvm_malloc(kd, 1);
54121687047SDima Dorfman 			goto liveout;
54221687047SDima Dorfman 		}
543b2d3d0f0SDag-Erling Smørgrav 		do {
544b2d3d0f0SDag-Erling Smørgrav 			size += size / 10;
545b2d3d0f0SDag-Erling Smørgrav 			kd->procbase = (struct kinfo_proc *)
546b2d3d0f0SDag-Erling Smørgrav 			    _kvm_realloc(kd, kd->procbase, size);
547*fb0e1892SEnji Cooper 			if (kd->procbase == NULL)
54858f0484fSRodney W. Grimes 				return (0);
549767993ecSMikolaj Golub 			osize = size;
550694127f8SDaniel Eischen 			st = sysctl(mib, temp_op == KERN_PROC_ALL ||
551694127f8SDaniel Eischen 			    temp_op == KERN_PROC_PROC ? 3 : 4,
552b2d3d0f0SDag-Erling Smørgrav 			    kd->procbase, &size, NULL, 0);
553767993ecSMikolaj Golub 		} while (st == -1 && errno == ENOMEM && size == osize);
55458f0484fSRodney W. Grimes 		if (st == -1) {
55558f0484fSRodney W. Grimes 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
55658f0484fSRodney W. Grimes 			return (0);
55758f0484fSRodney W. Grimes 		}
55821687047SDima Dorfman 		/*
55921687047SDima Dorfman 		 * We have to check the size again because sysctl()
56021687047SDima Dorfman 		 * may "round up" oldlenp if oldp is NULL; hence it
56121687047SDima Dorfman 		 * might've told us that there was data to get when
56221687047SDima Dorfman 		 * there really isn't any.
56321687047SDima Dorfman 		 */
5640627f53bSDavid Malone 		if (size > 0 &&
5650627f53bSDavid Malone 		    kd->procbase->ki_structsize != sizeof(struct kinfo_proc)) {
56658f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program,
567c10970ddSUlrich Spörlein 			    "kinfo_proc size mismatch (expected %zu, got %d)",
5681f7d2501SKirk McKusick 			    sizeof(struct kinfo_proc),
5691f7d2501SKirk McKusick 			    kd->procbase->ki_structsize);
57058f0484fSRodney W. Grimes 			return (0);
57158f0484fSRodney W. Grimes 		}
57221687047SDima Dorfman liveout:
573d339edcfSDavid Malone 		nprocs = size == 0 ? 0 : size / kd->procbase->ki_structsize;
57458f0484fSRodney W. Grimes 	} else {
575de788839SUlrich Spörlein 		struct nlist nl[7], *p;
57658f0484fSRodney W. Grimes 
57758f0484fSRodney W. Grimes 		nl[0].n_name = "_nprocs";
57858f0484fSRodney W. Grimes 		nl[1].n_name = "_allproc";
57958f0484fSRodney W. Grimes 		nl[2].n_name = "_zombproc";
58084a0b303SJeff Roberson 		nl[3].n_name = "_ticks";
58184a0b303SJeff Roberson 		nl[4].n_name = "_hz";
582de788839SUlrich Spörlein 		nl[5].n_name = "_cpu_tick_frequency";
583de788839SUlrich Spörlein 		nl[6].n_name = 0;
58458f0484fSRodney W. Grimes 
5857f911abeSJohn Baldwin 		if (!kd->arch->ka_native(kd)) {
5867f911abeSJohn Baldwin 			_kvm_err(kd, kd->program,
5877f911abeSJohn Baldwin 			    "cannot read procs from non-native core");
5887f911abeSJohn Baldwin 			return (0);
5897f911abeSJohn Baldwin 		}
5907f911abeSJohn Baldwin 
59158f0484fSRodney W. Grimes 		if (kvm_nlist(kd, nl) != 0) {
59258f0484fSRodney W. Grimes 			for (p = nl; p->n_type != 0; ++p)
59358f0484fSRodney W. Grimes 				;
59458f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program,
59558f0484fSRodney W. Grimes 				 "%s: no such symbol", p->n_name);
59658f0484fSRodney W. Grimes 			return (0);
59758f0484fSRodney W. Grimes 		}
59858f0484fSRodney W. Grimes 		if (KREAD(kd, nl[0].n_value, &nprocs)) {
59958f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "can't read nprocs");
60058f0484fSRodney W. Grimes 			return (0);
60158f0484fSRodney W. Grimes 		}
60284a0b303SJeff Roberson 		if (KREAD(kd, nl[3].n_value, &ticks)) {
60384a0b303SJeff Roberson 			_kvm_err(kd, kd->program, "can't read ticks");
60484a0b303SJeff Roberson 			return (0);
60584a0b303SJeff Roberson 		}
60684a0b303SJeff Roberson 		if (KREAD(kd, nl[4].n_value, &hz)) {
60784a0b303SJeff Roberson 			_kvm_err(kd, kd->program, "can't read hz");
60884a0b303SJeff Roberson 			return (0);
60984a0b303SJeff Roberson 		}
610de788839SUlrich Spörlein 		if (KREAD(kd, nl[5].n_value, &cpu_tick_frequency)) {
611de788839SUlrich Spörlein 			_kvm_err(kd, kd->program,
612de788839SUlrich Spörlein 			    "can't read cpu_tick_frequency");
613de788839SUlrich Spörlein 			return (0);
614de788839SUlrich Spörlein 		}
61558f0484fSRodney W. Grimes 		size = nprocs * sizeof(struct kinfo_proc);
61658f0484fSRodney W. Grimes 		kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
617*fb0e1892SEnji Cooper 		if (kd->procbase == NULL)
61858f0484fSRodney W. Grimes 			return (0);
61958f0484fSRodney W. Grimes 
62058f0484fSRodney W. Grimes 		nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
62158f0484fSRodney W. Grimes 				      nl[2].n_value, nprocs);
622ca895af3SAndriy Gapon 		if (nprocs <= 0) {
623ca895af3SAndriy Gapon 			_kvm_freeprocs(kd);
624ca895af3SAndriy Gapon 			nprocs = 0;
625ca895af3SAndriy Gapon 		}
62658f0484fSRodney W. Grimes #ifdef notdef
627ca895af3SAndriy Gapon 		else {
62858f0484fSRodney W. Grimes 			size = nprocs * sizeof(struct kinfo_proc);
629ca895af3SAndriy Gapon 			kd->procbase = realloc(kd->procbase, size);
630ca895af3SAndriy Gapon 		}
63158f0484fSRodney W. Grimes #endif
63258f0484fSRodney W. Grimes 	}
63358f0484fSRodney W. Grimes 	*cnt = nprocs;
63458f0484fSRodney W. Grimes 	return (kd->procbase);
63558f0484fSRodney W. Grimes }
63658f0484fSRodney W. Grimes 
63758f0484fSRodney W. Grimes void
638c10970ddSUlrich Spörlein _kvm_freeprocs(kvm_t *kd)
63958f0484fSRodney W. Grimes {
640*fb0e1892SEnji Cooper 
64158f0484fSRodney W. Grimes 	free(kd->procbase);
642*fb0e1892SEnji Cooper 	kd->procbase = NULL;
64358f0484fSRodney W. Grimes }
64458f0484fSRodney W. Grimes 
64558f0484fSRodney W. Grimes void *
646c10970ddSUlrich Spörlein _kvm_realloc(kvm_t *kd, void *p, size_t n)
64758f0484fSRodney W. Grimes {
648*fb0e1892SEnji Cooper 	void *np;
64958f0484fSRodney W. Grimes 
650*fb0e1892SEnji Cooper 	np = reallocf(p, n);
651*fb0e1892SEnji Cooper 	if (np == NULL)
65258f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "out of memory");
65358f0484fSRodney W. Grimes 	return (np);
65458f0484fSRodney W. Grimes }
65558f0484fSRodney W. Grimes 
65658f0484fSRodney W. Grimes /*
657ee5169dcSMikolaj Golub  * Get the command args or environment.
65858f0484fSRodney W. Grimes  */
65958f0484fSRodney W. Grimes static char **
660ee5169dcSMikolaj Golub kvm_argv(kvm_t *kd, const struct kinfo_proc *kp, int env, int nchr)
66158f0484fSRodney W. Grimes {
662b9df5231SPoul-Henning Kamp 	int oid[4];
663b7875890SDavid E. O'Brien 	int i;
664b7875890SDavid E. O'Brien 	size_t bufsz;
665ee5169dcSMikolaj Golub 	static int buflen;
666b9df5231SPoul-Henning Kamp 	static char *buf, *p;
667b9df5231SPoul-Henning Kamp 	static char **bufp;
668b9df5231SPoul-Henning Kamp 	static int argc;
669b9df5231SPoul-Henning Kamp 
670c4a7cdb3SPeter Wemm 	if (!ISALIVE(kd)) {
671c4a7cdb3SPeter Wemm 		_kvm_err(kd, kd->program,
672c4a7cdb3SPeter Wemm 		    "cannot read user space from dead kernel");
673*fb0e1892SEnji Cooper 		return (NULL);
674c4a7cdb3SPeter Wemm 	}
675c4a7cdb3SPeter Wemm 
676ee5169dcSMikolaj Golub 	if (nchr == 0 || nchr > ARG_MAX)
677ee5169dcSMikolaj Golub 		nchr = ARG_MAX;
678ee5169dcSMikolaj Golub 	if (buflen == 0) {
679ee5169dcSMikolaj Golub 		buf = malloc(nchr);
680ee5169dcSMikolaj Golub 		if (buf == NULL) {
681ee5169dcSMikolaj Golub 			_kvm_err(kd, kd->program, "cannot allocate memory");
682*fb0e1892SEnji Cooper 			return (NULL);
683ee5169dcSMikolaj Golub 		}
684ee5169dcSMikolaj Golub 		buflen = nchr;
685b9df5231SPoul-Henning Kamp 		argc = 32;
686b9df5231SPoul-Henning Kamp 		bufp = malloc(sizeof(char *) * argc);
687ee5169dcSMikolaj Golub 	} else if (nchr > buflen) {
688ee5169dcSMikolaj Golub 		p = realloc(buf, nchr);
689ee5169dcSMikolaj Golub 		if (p != NULL) {
690ee5169dcSMikolaj Golub 			buf = p;
691ee5169dcSMikolaj Golub 			buflen = nchr;
692b9df5231SPoul-Henning Kamp 		}
693b9df5231SPoul-Henning Kamp 	}
694b9df5231SPoul-Henning Kamp 	oid[0] = CTL_KERN;
695b9df5231SPoul-Henning Kamp 	oid[1] = KERN_PROC;
696ee5169dcSMikolaj Golub 	oid[2] = env ? KERN_PROC_ENV : KERN_PROC_ARGS;
6971f7d2501SKirk McKusick 	oid[3] = kp->ki_pid;
698b7875890SDavid E. O'Brien 	bufsz = buflen;
699952a0c3eSMikolaj Golub 	if (sysctl(oid, 4, buf, &bufsz, 0, 0) == -1) {
700952a0c3eSMikolaj Golub 		/*
701952a0c3eSMikolaj Golub 		 * If the supplied buf is too short to hold the requested
702952a0c3eSMikolaj Golub 		 * value the sysctl returns with ENOMEM. The buf is filled
703952a0c3eSMikolaj Golub 		 * with the truncated value and the returned bufsz is equal
704952a0c3eSMikolaj Golub 		 * to the requested len.
705952a0c3eSMikolaj Golub 		 */
706952a0c3eSMikolaj Golub 		if (errno != ENOMEM || bufsz != (size_t)buflen)
707*fb0e1892SEnji Cooper 			return (NULL);
708952a0c3eSMikolaj Golub 		buf[bufsz - 1] = '\0';
709952a0c3eSMikolaj Golub 		errno = 0;
710*fb0e1892SEnji Cooper 	} else if (bufsz == 0)
711*fb0e1892SEnji Cooper 		return (NULL);
712b9df5231SPoul-Henning Kamp 	i = 0;
713b9df5231SPoul-Henning Kamp 	p = buf;
714b9df5231SPoul-Henning Kamp 	do {
715b9df5231SPoul-Henning Kamp 		bufp[i++] = p;
716b9df5231SPoul-Henning Kamp 		p += strlen(p) + 1;
717b9df5231SPoul-Henning Kamp 		if (i >= argc) {
718b9df5231SPoul-Henning Kamp 			argc += argc;
719b9df5231SPoul-Henning Kamp 			bufp = realloc(bufp,
720b9df5231SPoul-Henning Kamp 			    sizeof(char *) * argc);
721b9df5231SPoul-Henning Kamp 		}
722b7875890SDavid E. O'Brien 	} while (p < buf + bufsz);
723b9df5231SPoul-Henning Kamp 	bufp[i++] = 0;
724b9df5231SPoul-Henning Kamp 	return (bufp);
725b9df5231SPoul-Henning Kamp }
726ee5169dcSMikolaj Golub 
727ee5169dcSMikolaj Golub char **
728ee5169dcSMikolaj Golub kvm_getargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr)
729ee5169dcSMikolaj Golub {
730ee5169dcSMikolaj Golub 	return (kvm_argv(kd, kp, 0, nchr));
73158f0484fSRodney W. Grimes }
73258f0484fSRodney W. Grimes 
73358f0484fSRodney W. Grimes char **
734c10970ddSUlrich Spörlein kvm_getenvv(kvm_t *kd, const struct kinfo_proc *kp, int nchr)
73558f0484fSRodney W. Grimes {
736ee5169dcSMikolaj Golub 	return (kvm_argv(kd, kp, 1, nchr));
73758f0484fSRodney W. Grimes }
738