11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1989, 1992, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * This code is derived from software developed by the Computer Systems
61573Srgrimes * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
71573Srgrimes * BG 91-66 and contributed to Berkeley.
81573Srgrimes *
91573Srgrimes * Redistribution and use in source and binary forms, with or without
101573Srgrimes * modification, are permitted provided that the following conditions
111573Srgrimes * are met:
121573Srgrimes * 1. Redistributions of source code must retain the above copyright
131573Srgrimes *    notice, this list of conditions and the following disclaimer.
141573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151573Srgrimes *    notice, this list of conditions and the following disclaimer in the
161573Srgrimes *    documentation and/or other materials provided with the distribution.
171573Srgrimes * 4. Neither the name of the University nor the names of its contributors
181573Srgrimes *    may be used to endorse or promote products derived from this software
191573Srgrimes *    without specific prior written permission.
201573Srgrimes *
211573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311573Srgrimes * SUCH DAMAGE.
321573Srgrimes */
331573Srgrimes
34103385Sbde#if 0
351573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
361573Srgrimesstatic char sccsid[] = "@(#)kvm_proc.c	8.3 (Berkeley) 9/23/93";
371573Srgrimes#endif /* LIBC_SCCS and not lint */
38103385Sbde#endif
391573Srgrimes
40103385Sbde#include <sys/cdefs.h>
41103385Sbde__FBSDID("$FreeBSD: stable/11/lib/libkvm/kvm_proc.c 310120 2016-12-15 16:51:33Z vangyzen $");
42103385Sbde
431573Srgrimes/*
441573Srgrimes * Proc traversal interface for kvm.  ps and w are (probably) the exclusive
451573Srgrimes * users of this code, so we've factored it out into a separate module.
461573Srgrimes * Thus, we keep this grunge out of the other kvm applications (i.e.,
471573Srgrimes * most other applications are interested only in open/close/read/nlist).
481573Srgrimes */
491573Srgrimes
501573Srgrimes#include <sys/param.h>
51143871Spjd#define	_WANT_UCRED	/* make ucred.h give us 'struct ucred' */
52101968Salfred#include <sys/ucred.h>
53143871Spjd#include <sys/queue.h>
54143871Spjd#include <sys/_lock.h>
55143871Spjd#include <sys/_mutex.h>
56143871Spjd#include <sys/_task.h>
57185435Sbz#include <sys/cpuset.h>
58185435Sbz#include <sys/user.h>
59185435Sbz#include <sys/proc.h>
60143871Spjd#define	_WANT_PRISON	/* make jail.h give us 'struct prison' */
61143871Spjd#include <sys/jail.h>
621573Srgrimes#include <sys/exec.h>
631573Srgrimes#include <sys/stat.h>
64130728Sgad#include <sys/sysent.h>
651573Srgrimes#include <sys/ioctl.h>
661573Srgrimes#include <sys/tty.h>
672029Sdg#include <sys/file.h>
68136402Speter#include <sys/conf.h>
69285670Skib#define	_WANT_KW_EXITCODE
70285670Skib#include <sys/wait.h>
7117141Sjkh#include <stdio.h>
7217141Sjkh#include <stdlib.h>
731573Srgrimes#include <unistd.h>
741573Srgrimes#include <nlist.h>
751573Srgrimes#include <kvm.h>
761573Srgrimes
771573Srgrimes#include <sys/sysctl.h>
781573Srgrimes
791573Srgrimes#include <limits.h>
8012682Speter#include <memory.h>
811573Srgrimes#include <paths.h>
821573Srgrimes
831573Srgrimes#include "kvm_private.h"
841573Srgrimes
851573Srgrimes#define KREAD(kd, addr, obj) \
861573Srgrimes	(kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj))
871573Srgrimes
88172265Sjeffstatic int ticks;
89172265Sjeffstatic int hz;
90217745Suqsstatic uint64_t cpu_tick_frequency;
91172265Sjeff
921573Srgrimes/*
93217745Suqs * From sys/kern/kern_tc.c. Depends on cpu_tick_frequency, which is
94217745Suqs * read/initialized before this function is ever called.
95217745Suqs */
96217745Suqsstatic uint64_t
97217745Suqscputick2usec(uint64_t tick)
98217745Suqs{
99217745Suqs
100217745Suqs	if (cpu_tick_frequency == 0)
101217745Suqs		return (0);
102217745Suqs	if (tick > 18446744073709551)		/* floor(2^64 / 1000) */
103217745Suqs		return (tick / (cpu_tick_frequency / 1000000));
104217745Suqs	else if (tick > 18446744073709)	/* floor(2^64 / 1000000) */
105217745Suqs		return ((tick * 1000) / (cpu_tick_frequency / 1000));
106217745Suqs	else
107217745Suqs		return ((tick * 1000000) / cpu_tick_frequency);
108217745Suqs}
109217745Suqs
110217745Suqs/*
1111573Srgrimes * Read proc's from memory file into buffer bp, which has space to hold
1121573Srgrimes * at most maxcnt procs.
1131573Srgrimes */
1141573Srgrimesstatic int
115217744Suqskvm_proclist(kvm_t *kd, int what, int arg, struct proc *p,
116217744Suqs    struct kinfo_proc *bp, int maxcnt)
1171573Srgrimes{
11892913Sobrien	int cnt = 0;
11969896Smckusick	struct kinfo_proc kinfo_proc, *kp;
1201573Srgrimes	struct pgrp pgrp;
1211573Srgrimes	struct session sess;
122136402Speter	struct cdev t_cdev;
1231573Srgrimes	struct tty tty;
12469896Smckusick	struct vmspace vmspace;
125114996Sjhb	struct sigacts sigacts;
126217744Suqs#if 0
12769896Smckusick	struct pstats pstats;
128217744Suqs#endif
12969896Smckusick	struct ucred ucred;
130143871Spjd	struct prison pr;
131103216Sjulian	struct thread mtd;
1321573Srgrimes	struct proc proc;
13326947Stegge	struct proc pproc;
134130728Sgad	struct sysentvec sysent;
135130728Sgad	char svname[KI_EMULNAMELEN];
1361573Srgrimes
13769896Smckusick	kp = &kinfo_proc;
13869896Smckusick	kp->ki_structsize = sizeof(kinfo_proc);
139173004Sjulian	/*
140173004Sjulian	 * Loop on the processes. this is completely broken because we need to be
141173004Sjulian	 * able to loop on the threads and merge the ones that are the same process some how.
142173004Sjulian	 */
14370525Sben	for (; cnt < maxcnt && p != NULL; p = LIST_NEXT(&proc, p_list)) {
14482266Speter		memset(kp, 0, sizeof *kp);
1451573Srgrimes		if (KREAD(kd, (u_long)p, &proc)) {
146217744Suqs			_kvm_err(kd, kd->program, "can't read proc at %p", p);
1471573Srgrimes			return (-1);
1481573Srgrimes		}
149241302Savg		if (proc.p_state == PRS_NEW)
150241302Savg			continue;
15199157Sjulian		if (proc.p_state != PRS_ZOMBIE) {
15299157Sjulian			if (KREAD(kd, (u_long)TAILQ_FIRST(&proc.p_threads),
153103216Sjulian			    &mtd)) {
15499157Sjulian				_kvm_err(kd, kd->program,
155217744Suqs				    "can't read thread at %p",
15699157Sjulian				    TAILQ_FIRST(&proc.p_threads));
15799157Sjulian				return (-1);
15899157Sjulian			}
15990360Sjulian		}
16077183Srwatson		if (KREAD(kd, (u_long)proc.p_ucred, &ucred) == 0) {
16177183Srwatson			kp->ki_ruid = ucred.cr_ruid;
16277183Srwatson			kp->ki_svuid = ucred.cr_svuid;
16377183Srwatson			kp->ki_rgid = ucred.cr_rgid;
16477183Srwatson			kp->ki_svgid = ucred.cr_svgid;
165195843Sbrooks			kp->ki_cr_flags = ucred.cr_flags;
166195843Sbrooks			if (ucred.cr_ngroups > KI_NGROUPS) {
167195843Sbrooks				kp->ki_ngroups = KI_NGROUPS;
168195843Sbrooks				kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
169200366Sbrooks			} else
170195843Sbrooks				kp->ki_ngroups = ucred.cr_ngroups;
171196990Sbrooks			kvm_read(kd, (u_long)ucred.cr_groups, kp->ki_groups,
172195843Sbrooks			    kp->ki_ngroups * sizeof(gid_t));
17369896Smckusick			kp->ki_uid = ucred.cr_uid;
174143871Spjd			if (ucred.cr_prison != NULL) {
175143871Spjd				if (KREAD(kd, (u_long)ucred.cr_prison, &pr)) {
176143871Spjd					_kvm_err(kd, kd->program,
177217744Suqs					    "can't read prison at %p",
178143871Spjd					    ucred.cr_prison);
179143871Spjd					return (-1);
180143871Spjd				}
181143871Spjd				kp->ki_jid = pr.pr_id;
182143871Spjd			}
18369896Smckusick		}
1841573Srgrimes
185126126Sdeischen		switch(what & ~KERN_PROC_INC_THREAD) {
1868870Srgrimes
187130728Sgad		case KERN_PROC_GID:
188130728Sgad			if (kp->ki_groups[0] != (gid_t)arg)
189130728Sgad				continue;
190130728Sgad			break;
191130728Sgad
1921573Srgrimes		case KERN_PROC_PID:
1931573Srgrimes			if (proc.p_pid != (pid_t)arg)
1941573Srgrimes				continue;
1951573Srgrimes			break;
1961573Srgrimes
197130728Sgad		case KERN_PROC_RGID:
198130728Sgad			if (kp->ki_rgid != (gid_t)arg)
199130728Sgad				continue;
200130728Sgad			break;
201130728Sgad
2021573Srgrimes		case KERN_PROC_UID:
20369896Smckusick			if (kp->ki_uid != (uid_t)arg)
2041573Srgrimes				continue;
2051573Srgrimes			break;
2061573Srgrimes
2071573Srgrimes		case KERN_PROC_RUID:
20869896Smckusick			if (kp->ki_ruid != (uid_t)arg)
2091573Srgrimes				continue;
2101573Srgrimes			break;
2111573Srgrimes		}
2121573Srgrimes		/*
2131573Srgrimes		 * We're going to add another proc to the set.  If this
2141573Srgrimes		 * will overflow the buffer, assume the reason is because
2151573Srgrimes		 * nprocs (or the proc list) is corrupt and declare an error.
2161573Srgrimes		 */
2171573Srgrimes		if (cnt >= maxcnt) {
2181573Srgrimes			_kvm_err(kd, kd->program, "nprocs corrupt");
2191573Srgrimes			return (-1);
2201573Srgrimes		}
2211573Srgrimes		/*
22269896Smckusick		 * gather kinfo_proc
2231573Srgrimes		 */
22469896Smckusick		kp->ki_paddr = p;
225137907Sdas		kp->ki_addr = 0;	/* XXX uarea */
22683366Sjulian		/* kp->ki_kstack = proc.p_thread.td_kstack; XXXKSE */
22769896Smckusick		kp->ki_args = proc.p_args;
228112204Sjhb		kp->ki_tracep = proc.p_tracevp;
22969896Smckusick		kp->ki_textvp = proc.p_textvp;
23069896Smckusick		kp->ki_fd = proc.p_fd;
23169896Smckusick		kp->ki_vmspace = proc.p_vmspace;
232114996Sjhb		if (proc.p_sigacts != NULL) {
233114996Sjhb			if (KREAD(kd, (u_long)proc.p_sigacts, &sigacts)) {
23469896Smckusick				_kvm_err(kd, kd->program,
235217744Suqs				    "can't read sigacts at %p", proc.p_sigacts);
23669896Smckusick				return (-1);
23769896Smckusick			}
238114996Sjhb			kp->ki_sigignore = sigacts.ps_sigignore;
239114996Sjhb			kp->ki_sigcatch = sigacts.ps_sigcatch;
24069896Smckusick		}
241170178Sjeff#if 0
242172207Sjeff		if ((proc.p_flag & P_INMEM) && proc.p_stats != NULL) {
24369896Smckusick			if (KREAD(kd, (u_long)proc.p_stats, &pstats)) {
24469896Smckusick				_kvm_err(kd, kd->program,
24569896Smckusick				    "can't read stats at %x", proc.p_stats);
24669896Smckusick				return (-1);
24769896Smckusick			}
24869896Smckusick			kp->ki_start = pstats.p_start;
249136195Sjhb
250136195Sjhb			/*
251136195Sjhb			 * XXX: The times here are probably zero and need
252136195Sjhb			 * to be calculated from the raw data in p_rux and
253136195Sjhb			 * p_crux.
254136195Sjhb			 */
25569896Smckusick			kp->ki_rusage = pstats.p_ru;
256130728Sgad			kp->ki_childstime = pstats.p_cru.ru_stime;
257130728Sgad			kp->ki_childutime = pstats.p_cru.ru_utime;
258130728Sgad			/* Some callers want child-times in a single value */
259130728Sgad			timeradd(&kp->ki_childstime, &kp->ki_childutime,
260130728Sgad			    &kp->ki_childtime);
26169896Smckusick		}
262170178Sjeff#endif
26326947Stegge		if (proc.p_oppid)
26469896Smckusick			kp->ki_ppid = proc.p_oppid;
26526947Stegge		else if (proc.p_pptr) {
26669896Smckusick			if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) {
26769896Smckusick				_kvm_err(kd, kd->program,
268217744Suqs				    "can't read pproc at %p", proc.p_pptr);
26969896Smckusick				return (-1);
27069896Smckusick			}
27169896Smckusick			kp->ki_ppid = pproc.p_pid;
272291372Sjhb		} else
27369896Smckusick			kp->ki_ppid = 0;
27482266Speter		if (proc.p_pgrp == NULL)
27582266Speter			goto nopgrp;
27682266Speter		if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
277217744Suqs			_kvm_err(kd, kd->program, "can't read pgrp at %p",
27882266Speter				 proc.p_pgrp);
27982266Speter			return (-1);
28082266Speter		}
28169896Smckusick		kp->ki_pgid = pgrp.pg_id;
28269896Smckusick		kp->ki_jobc = pgrp.pg_jobc;
2831573Srgrimes		if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
284217744Suqs			_kvm_err(kd, kd->program, "can't read session at %p",
2851573Srgrimes				pgrp.pg_session);
2861573Srgrimes			return (-1);
2871573Srgrimes		}
28869896Smckusick		kp->ki_sid = sess.s_sid;
28969896Smckusick		(void)memcpy(kp->ki_login, sess.s_login,
29069896Smckusick						sizeof(kp->ki_login));
29169896Smckusick		kp->ki_kiflag = sess.s_ttyvp ? KI_CTTY : 0;
29269896Smckusick		if (sess.s_leader == p)
29369896Smckusick			kp->ki_kiflag |= KI_SLEADER;
2941573Srgrimes		if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) {
2951573Srgrimes			if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
2961573Srgrimes				_kvm_err(kd, kd->program,
297217744Suqs					 "can't read tty at %p", sess.s_ttyp);
2981573Srgrimes				return (-1);
2991573Srgrimes			}
300136402Speter			if (tty.t_dev != NULL) {
301136402Speter				if (KREAD(kd, (u_long)tty.t_dev, &t_cdev)) {
302136402Speter					_kvm_err(kd, kd->program,
303217744Suqs						 "can't read cdev at %p",
304136402Speter						tty.t_dev);
305136402Speter					return (-1);
306136402Speter				}
307143648Sphk#if 0
308136402Speter				kp->ki_tdev = t_cdev.si_udev;
309143648Sphk#else
310147567Speter				kp->ki_tdev = NODEV;
311143648Sphk#endif
312136402Speter			}
3131573Srgrimes			if (tty.t_pgrp != NULL) {
3141573Srgrimes				if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
3151573Srgrimes					_kvm_err(kd, kd->program,
316217744Suqs						 "can't read tpgrp at %p",
3171573Srgrimes						tty.t_pgrp);
3181573Srgrimes					return (-1);
3191573Srgrimes				}
32069896Smckusick				kp->ki_tpgid = pgrp.pg_id;
3211573Srgrimes			} else
32269896Smckusick				kp->ki_tpgid = -1;
32369896Smckusick			if (tty.t_session != NULL) {
32469896Smckusick				if (KREAD(kd, (u_long)tty.t_session, &sess)) {
32569896Smckusick					_kvm_err(kd, kd->program,
326217744Suqs					    "can't read session at %p",
32769896Smckusick					    tty.t_session);
32869896Smckusick					return (-1);
32969896Smckusick				}
33069896Smckusick				kp->ki_tsid = sess.s_sid;
33169896Smckusick			}
33282266Speter		} else {
33382266Speternopgrp:
33469896Smckusick			kp->ki_tdev = NODEV;
33582266Speter		}
336103216Sjulian		if ((proc.p_state != PRS_ZOMBIE) && mtd.td_wmesg)
337103216Sjulian			(void)kvm_read(kd, (u_long)mtd.td_wmesg,
33869896Smckusick			    kp->ki_wmesg, WMESGLEN);
3391573Srgrimes
3401573Srgrimes		(void)kvm_read(kd, (u_long)proc.p_vmspace,
34169896Smckusick		    (char *)&vmspace, sizeof(vmspace));
34269896Smckusick		kp->ki_size = vmspace.vm_map.size;
343209276Ssbruno		/*
344209276Ssbruno		 * Approximate the kernel's method of calculating
345209276Ssbruno		 * this field.
346209276Ssbruno		 */
347209276Ssbruno#define		pmap_resident_count(pm) ((pm)->pm_stats.resident_count)
348291372Sjhb		kp->ki_rssize = pmap_resident_count(&vmspace.vm_pmap);
34969896Smckusick		kp->ki_swrss = vmspace.vm_swrss;
35069896Smckusick		kp->ki_tsize = vmspace.vm_tsize;
35169896Smckusick		kp->ki_dsize = vmspace.vm_dsize;
35269896Smckusick		kp->ki_ssize = vmspace.vm_ssize;
3531573Srgrimes
354126126Sdeischen		switch (what & ~KERN_PROC_INC_THREAD) {
3551573Srgrimes
3561573Srgrimes		case KERN_PROC_PGRP:
35769896Smckusick			if (kp->ki_pgid != (pid_t)arg)
3581573Srgrimes				continue;
3591573Srgrimes			break;
3601573Srgrimes
361130728Sgad		case KERN_PROC_SESSION:
362130728Sgad			if (kp->ki_sid != (pid_t)arg)
363130728Sgad				continue;
364130728Sgad			break;
365130728Sgad
3661573Srgrimes		case KERN_PROC_TTY:
3678870Srgrimes			if ((proc.p_flag & P_CONTROLT) == 0 ||
36869896Smckusick			     kp->ki_tdev != (dev_t)arg)
3691573Srgrimes				continue;
3701573Srgrimes			break;
3711573Srgrimes		}
372130992Sgad		if (proc.p_comm[0] != 0)
373130992Sgad			strlcpy(kp->ki_comm, proc.p_comm, MAXCOMLEN);
374130728Sgad		(void)kvm_read(kd, (u_long)proc.p_sysent, (char *)&sysent,
375130728Sgad		    sizeof(sysent));
376130728Sgad		(void)kvm_read(kd, (u_long)sysent.sv_name, (char *)&svname,
377130728Sgad		    sizeof(svname));
378130728Sgad		if (svname[0] != 0)
379130728Sgad			strlcpy(kp->ki_emul, svname, KI_EMULNAMELEN);
38099142Sjulian		if ((proc.p_state != PRS_ZOMBIE) &&
381103216Sjulian		    (mtd.td_blocked != 0)) {
382104388Sjhb			kp->ki_kiflag |= KI_LOCKBLOCK;
383104388Sjhb			if (mtd.td_lockname)
38499142Sjulian				(void)kvm_read(kd,
385104388Sjhb				    (u_long)mtd.td_lockname,
386104388Sjhb				    kp->ki_lockname, LOCKNAMELEN);
387104388Sjhb			kp->ki_lockname[LOCKNAMELEN] = 0;
38869896Smckusick		}
389217745Suqs		kp->ki_runtime = cputick2usec(proc.p_rux.rux_runtime);
39069896Smckusick		kp->ki_pid = proc.p_pid;
391104307Sjmallett		kp->ki_siglist = proc.p_siglist;
392112923Sjeff		SIGSETOR(kp->ki_siglist, mtd.td_siglist);
393112891Sjeff		kp->ki_sigmask = mtd.td_sigmask;
394285670Skib		kp->ki_xstat = KW_EXITCODE(proc.p_xexit, proc.p_xsig);
39569896Smckusick		kp->ki_acflag = proc.p_acflag;
396103367Sjulian		kp->ki_lock = proc.p_lock;
39799142Sjulian		if (proc.p_state != PRS_ZOMBIE) {
398172265Sjeff			kp->ki_swtime = (ticks - proc.p_swtick) / hz;
39999128Sjulian			kp->ki_flag = proc.p_flag;
400172207Sjeff			kp->ki_sflag = 0;
401130552Sjulian			kp->ki_nice = proc.p_nice;
40299128Sjulian			kp->ki_traceflag = proc.p_traceflag;
403291372Sjhb			if (proc.p_state == PRS_NORMAL) {
404103216Sjulian				if (TD_ON_RUNQ(&mtd) ||
405103216Sjulian				    TD_CAN_RUN(&mtd) ||
406103216Sjulian				    TD_IS_RUNNING(&mtd)) {
40799128Sjulian					kp->ki_stat = SRUN;
408291372Sjhb				} else if (mtd.td_state ==
409103367Sjulian				    TDS_INHIBITED) {
410103216Sjulian					if (P_SHOULDSTOP(&proc)) {
411103216Sjulian						kp->ki_stat = SSTOP;
412103367Sjulian					} else if (
413103367Sjulian					    TD_IS_SLEEPING(&mtd)) {
414103216Sjulian						kp->ki_stat = SSLEEP;
415104388Sjhb					} else if (TD_ON_LOCK(&mtd)) {
416104388Sjhb						kp->ki_stat = SLOCK;
417103216Sjulian					} else {
418103216Sjulian						kp->ki_stat = SWAIT;
419103216Sjulian					}
42099128Sjulian				}
42199072Sjulian			} else {
42299128Sjulian				kp->ki_stat = SIDL;
42399072Sjulian			}
424103367Sjulian			/* Stuff from the thread */
425103216Sjulian			kp->ki_pri.pri_level = mtd.td_priority;
426103216Sjulian			kp->ki_pri.pri_native = mtd.td_base_pri;
427103216Sjulian			kp->ki_lastcpu = mtd.td_lastcpu;
428103367Sjulian			kp->ki_wchan = mtd.td_wchan;
429113340Sjulian			kp->ki_oncpu = mtd.td_oncpu;
430173004Sjulian			if (mtd.td_name[0] != '\0')
431224199Sbz				strlcpy(kp->ki_tdname, mtd.td_name, sizeof(kp->ki_tdname));
432177089Sjeff			kp->ki_pctcpu = 0;
433177089Sjeff			kp->ki_rqindex = 0;
434273266Sadrian
435273266Sadrian			/*
436273266Sadrian			 * Note: legacy fields; wraps at NO_CPU_OLD or the
437273266Sadrian			 * old max CPU value as appropriate
438273266Sadrian			 */
439273266Sadrian			if (mtd.td_lastcpu == NOCPU)
440273266Sadrian				kp->ki_lastcpu_old = NOCPU_OLD;
441273266Sadrian			else if (mtd.td_lastcpu > MAXCPU_OLD)
442273266Sadrian				kp->ki_lastcpu_old = MAXCPU_OLD;
443273266Sadrian			else
444273266Sadrian				kp->ki_lastcpu_old = mtd.td_lastcpu;
445273266Sadrian
446273266Sadrian			if (mtd.td_oncpu == NOCPU)
447273266Sadrian				kp->ki_oncpu_old = NOCPU_OLD;
448273266Sadrian			else if (mtd.td_oncpu > MAXCPU_OLD)
449273266Sadrian				kp->ki_oncpu_old = MAXCPU_OLD;
450273266Sadrian			else
451273266Sadrian				kp->ki_oncpu_old = mtd.td_oncpu;
45299128Sjulian		} else {
45399072Sjulian			kp->ki_stat = SZOMB;
45499072Sjulian		}
45569896Smckusick		bcopy(&kinfo_proc, bp, sizeof(kinfo_proc));
4561573Srgrimes		++bp;
4571573Srgrimes		++cnt;
4581573Srgrimes	}
4591573Srgrimes	return (cnt);
4601573Srgrimes}
4611573Srgrimes
4621573Srgrimes/*
4631573Srgrimes * Build proc info array by reading in proc list from a crash dump.
4641573Srgrimes * Return number of procs read.  maxcnt is the max we will read.
4651573Srgrimes */
4661573Srgrimesstatic int
467217744Suqskvm_deadprocs(kvm_t *kd, int what, int arg, u_long a_allproc,
468217744Suqs    u_long a_zombproc, int maxcnt)
4691573Srgrimes{
47092913Sobrien	struct kinfo_proc *bp = kd->procbase;
47192913Sobrien	int acnt, zcnt;
4721573Srgrimes	struct proc *p;
4731573Srgrimes
4741573Srgrimes	if (KREAD(kd, a_allproc, &p)) {
4751573Srgrimes		_kvm_err(kd, kd->program, "cannot read allproc");
4761573Srgrimes		return (-1);
4771573Srgrimes	}
4781573Srgrimes	acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
4791573Srgrimes	if (acnt < 0)
4801573Srgrimes		return (acnt);
4811573Srgrimes
4821573Srgrimes	if (KREAD(kd, a_zombproc, &p)) {
4831573Srgrimes		_kvm_err(kd, kd->program, "cannot read zombproc");
4841573Srgrimes		return (-1);
4851573Srgrimes	}
4861573Srgrimes	zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt);
4871573Srgrimes	if (zcnt < 0)
4881573Srgrimes		zcnt = 0;
4891573Srgrimes
4901573Srgrimes	return (acnt + zcnt);
4911573Srgrimes}
4921573Srgrimes
4931573Srgrimesstruct kinfo_proc *
494217744Suqskvm_getprocs(kvm_t *kd, int op, int arg, int *cnt)
4951573Srgrimes{
49638534Sdfr	int mib[4], st, nprocs;
497230873Strociny	size_t size, osize;
498126126Sdeischen	int temp_op;
4991573Srgrimes
5001573Srgrimes	if (kd->procbase != 0) {
5011573Srgrimes		free((void *)kd->procbase);
5028870Srgrimes		/*
5031573Srgrimes		 * Clear this pointer in case this call fails.  Otherwise,
5041573Srgrimes		 * kvm_close() will free it again.
5051573Srgrimes		 */
5061573Srgrimes		kd->procbase = 0;
5071573Srgrimes	}
5081573Srgrimes	if (ISALIVE(kd)) {
5091573Srgrimes		size = 0;
5101573Srgrimes		mib[0] = CTL_KERN;
5111573Srgrimes		mib[1] = KERN_PROC;
5121573Srgrimes		mib[2] = op;
5131573Srgrimes		mib[3] = arg;
514126126Sdeischen		temp_op = op & ~KERN_PROC_INC_THREAD;
515126126Sdeischen		st = sysctl(mib,
516126126Sdeischen		    temp_op == KERN_PROC_ALL || temp_op == KERN_PROC_PROC ?
517120500Stjr		    3 : 4, NULL, &size, NULL, 0);
5181573Srgrimes		if (st == -1) {
5191573Srgrimes			_kvm_syserr(kd, kd->program, "kvm_getprocs");
5201573Srgrimes			return (0);
5211573Srgrimes		}
52294028Sdd		/*
52394028Sdd		 * We can't continue with a size of 0 because we pass
52494028Sdd		 * it to realloc() (via _kvm_realloc()), and passing 0
52594028Sdd		 * to realloc() results in undefined behavior.
52694028Sdd		 */
52794028Sdd		if (size == 0) {
52894028Sdd			/*
52994028Sdd			 * XXX: We should probably return an invalid,
53094028Sdd			 * but non-NULL, pointer here so any client
53194028Sdd			 * program trying to dereference it will
53294028Sdd			 * crash.  However, _kvm_freeprocs() calls
53394028Sdd			 * free() on kd->procbase if it isn't NULL,
53494028Sdd			 * and free()'ing a junk pointer isn't good.
53594028Sdd			 * Then again, _kvm_freeprocs() isn't used
53694028Sdd			 * anywhere . . .
53794028Sdd			 */
53894028Sdd			kd->procbase = _kvm_malloc(kd, 1);
53994028Sdd			goto liveout;
54094028Sdd		}
54140268Sdes		do {
54240268Sdes			size += size / 10;
54340268Sdes			kd->procbase = (struct kinfo_proc *)
54441880Sbde			    _kvm_realloc(kd, kd->procbase, size);
545298485Sngie			if (kd->procbase == NULL)
54640268Sdes				return (0);
547230873Strociny			osize = size;
548126126Sdeischen			st = sysctl(mib, temp_op == KERN_PROC_ALL ||
549126126Sdeischen			    temp_op == KERN_PROC_PROC ? 3 : 4,
55041880Sbde			    kd->procbase, &size, NULL, 0);
551230873Strociny		} while (st == -1 && errno == ENOMEM && size == osize);
5521573Srgrimes		if (st == -1) {
5531573Srgrimes			_kvm_syserr(kd, kd->program, "kvm_getprocs");
5541573Srgrimes			return (0);
5551573Srgrimes		}
55694028Sdd		/*
55794028Sdd		 * We have to check the size again because sysctl()
55894028Sdd		 * may "round up" oldlenp if oldp is NULL; hence it
55994028Sdd		 * might've told us that there was data to get when
56094028Sdd		 * there really isn't any.
56194028Sdd		 */
56276182Sdwmalone		if (size > 0 &&
56376182Sdwmalone		    kd->procbase->ki_structsize != sizeof(struct kinfo_proc)) {
5641573Srgrimes			_kvm_err(kd, kd->program,
565217744Suqs			    "kinfo_proc size mismatch (expected %zu, got %d)",
56669896Smckusick			    sizeof(struct kinfo_proc),
56769896Smckusick			    kd->procbase->ki_structsize);
5681573Srgrimes			return (0);
5691573Srgrimes		}
57094028Sddliveout:
57176243Sdwmalone		nprocs = size == 0 ? 0 : size / kd->procbase->ki_structsize;
5721573Srgrimes	} else {
573217745Suqs		struct nlist nl[7], *p;
5741573Srgrimes
5751573Srgrimes		nl[0].n_name = "_nprocs";
5761573Srgrimes		nl[1].n_name = "_allproc";
5771573Srgrimes		nl[2].n_name = "_zombproc";
578172265Sjeff		nl[3].n_name = "_ticks";
579172265Sjeff		nl[4].n_name = "_hz";
580217745Suqs		nl[5].n_name = "_cpu_tick_frequency";
581217745Suqs		nl[6].n_name = 0;
5821573Srgrimes
583291406Sjhb		if (!kd->arch->ka_native(kd)) {
584291406Sjhb			_kvm_err(kd, kd->program,
585291406Sjhb			    "cannot read procs from non-native core");
586291406Sjhb			return (0);
587291406Sjhb		}
588291406Sjhb
5891573Srgrimes		if (kvm_nlist(kd, nl) != 0) {
5901573Srgrimes			for (p = nl; p->n_type != 0; ++p)
5911573Srgrimes				;
5921573Srgrimes			_kvm_err(kd, kd->program,
5931573Srgrimes				 "%s: no such symbol", p->n_name);
5941573Srgrimes			return (0);
5951573Srgrimes		}
5961573Srgrimes		if (KREAD(kd, nl[0].n_value, &nprocs)) {
5971573Srgrimes			_kvm_err(kd, kd->program, "can't read nprocs");
5981573Srgrimes			return (0);
5991573Srgrimes		}
600172265Sjeff		if (KREAD(kd, nl[3].n_value, &ticks)) {
601172265Sjeff			_kvm_err(kd, kd->program, "can't read ticks");
602172265Sjeff			return (0);
603172265Sjeff		}
604172265Sjeff		if (KREAD(kd, nl[4].n_value, &hz)) {
605172265Sjeff			_kvm_err(kd, kd->program, "can't read hz");
606172265Sjeff			return (0);
607172265Sjeff		}
608217745Suqs		if (KREAD(kd, nl[5].n_value, &cpu_tick_frequency)) {
609217745Suqs			_kvm_err(kd, kd->program,
610217745Suqs			    "can't read cpu_tick_frequency");
611217745Suqs			return (0);
612217745Suqs		}
6131573Srgrimes		size = nprocs * sizeof(struct kinfo_proc);
6141573Srgrimes		kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
615298485Sngie		if (kd->procbase == NULL)
6161573Srgrimes			return (0);
6171573Srgrimes
6181573Srgrimes		nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
6191573Srgrimes				      nl[2].n_value, nprocs);
620241303Savg		if (nprocs <= 0) {
621241303Savg			_kvm_freeprocs(kd);
622241303Savg			nprocs = 0;
623241303Savg		}
6241573Srgrimes#ifdef notdef
625241303Savg		else {
626241303Savg			size = nprocs * sizeof(struct kinfo_proc);
627241303Savg			kd->procbase = realloc(kd->procbase, size);
628241303Savg		}
6291573Srgrimes#endif
6301573Srgrimes	}
6311573Srgrimes	*cnt = nprocs;
6321573Srgrimes	return (kd->procbase);
6331573Srgrimes}
6341573Srgrimes
6351573Srgrimesvoid
636217744Suqs_kvm_freeprocs(kvm_t *kd)
6371573Srgrimes{
638298485Sngie
639298485Sngie	free(kd->procbase);
640298485Sngie	kd->procbase = NULL;
6411573Srgrimes}
6421573Srgrimes
6431573Srgrimesvoid *
644217744Suqs_kvm_realloc(kvm_t *kd, void *p, size_t n)
6451573Srgrimes{
646298485Sngie	void *np;
6471573Srgrimes
648298485Sngie	np = reallocf(p, n);
649298485Sngie	if (np == NULL)
6501573Srgrimes		_kvm_err(kd, kd->program, "out of memory");
6511573Srgrimes	return (np);
6521573Srgrimes}
6531573Srgrimes
6541573Srgrimes/*
655227839Strociny * Get the command args or environment.
6561573Srgrimes */
6571573Srgrimesstatic char **
658227839Strocinykvm_argv(kvm_t *kd, const struct kinfo_proc *kp, int env, int nchr)
6591573Srgrimes{
66053239Sphk	int oid[4];
66158642Sobrien	int i;
66258642Sobrien	size_t bufsz;
663227839Strociny	static int buflen;
66453239Sphk	static char *buf, *p;
66553239Sphk	static char **bufp;
66653239Sphk	static int argc;
667298839Sngie	char **nbufp;
66853239Sphk
66955127Speter	if (!ISALIVE(kd)) {
67055127Speter		_kvm_err(kd, kd->program,
67155127Speter		    "cannot read user space from dead kernel");
672298485Sngie		return (NULL);
67355127Speter	}
67455127Speter
675227839Strociny	if (nchr == 0 || nchr > ARG_MAX)
676227839Strociny		nchr = ARG_MAX;
677227839Strociny	if (buflen == 0) {
678227839Strociny		buf = malloc(nchr);
679227839Strociny		if (buf == NULL) {
680227839Strociny			_kvm_err(kd, kd->program, "cannot allocate memory");
681298485Sngie			return (NULL);
68253239Sphk		}
683227839Strociny		argc = 32;
684227839Strociny		bufp = malloc(sizeof(char *) * argc);
685298839Sngie		if (bufp == NULL) {
686298839Sngie			free(buf);
687298839Sngie			buf = NULL;
688298839Sngie			_kvm_err(kd, kd->program, "cannot allocate memory");
689298839Sngie			return (NULL);
690298839Sngie		}
691298839Sngie		buflen = nchr;
692227839Strociny	} else if (nchr > buflen) {
693227839Strociny		p = realloc(buf, nchr);
694227839Strociny		if (p != NULL) {
695227839Strociny			buf = p;
696227839Strociny			buflen = nchr;
697227839Strociny		}
69853239Sphk	}
699230146Strociny	oid[0] = CTL_KERN;
700230146Strociny	oid[1] = KERN_PROC;
701230146Strociny	oid[2] = env ? KERN_PROC_ENV : KERN_PROC_ARGS;
702230146Strociny	oid[3] = kp->ki_pid;
703230146Strociny	bufsz = buflen;
704230146Strociny	if (sysctl(oid, 4, buf, &bufsz, 0, 0) == -1) {
705230146Strociny		/*
706230146Strociny		 * If the supplied buf is too short to hold the requested
707230146Strociny		 * value the sysctl returns with ENOMEM. The buf is filled
708230146Strociny		 * with the truncated value and the returned bufsz is equal
709230146Strociny		 * to the requested len.
710230146Strociny		 */
711230146Strociny		if (errno != ENOMEM || bufsz != (size_t)buflen)
712298485Sngie			return (NULL);
713230146Strociny		buf[bufsz - 1] = '\0';
714230146Strociny		errno = 0;
715298485Sngie	} else if (bufsz == 0)
716298485Sngie		return (NULL);
717230146Strociny	i = 0;
718230146Strociny	p = buf;
719230146Strociny	do {
720230146Strociny		bufp[i++] = p;
721230146Strociny		p += strlen(p) + 1;
722230146Strociny		if (i >= argc) {
723230146Strociny			argc += argc;
724298839Sngie			nbufp = realloc(bufp, sizeof(char *) * argc);
725298839Sngie			if (nbufp == NULL)
726298839Sngie				return (NULL);
727298839Sngie			bufp = nbufp;
72853239Sphk		}
729230146Strociny	} while (p < buf + bufsz);
730230146Strociny	bufp[i++] = 0;
731230146Strociny	return (bufp);
7321573Srgrimes}
7331573Srgrimes
7341573Srgrimeschar **
735227839Strocinykvm_getargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr)
7361573Srgrimes{
737227839Strociny	return (kvm_argv(kd, kp, 0, nchr));
7381573Srgrimes}
7391573Srgrimes
740227839Strocinychar **
741227839Strocinykvm_getenvv(kvm_t *kd, const struct kinfo_proc *kp, int nchr)
7421573Srgrimes{
743227839Strociny	return (kvm_argv(kd, kp, 1, nchr));
7441573Srgrimes}
745