2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 #include "opt_ktrace.h"
39 #include "opt_kstack_pages.h"
40 #include "opt_stack.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/eventhandler.h>
48 #include <sys/kernel.h>
49 #include <sys/limits.h>
51 #include <sys/loginclass.h>
52 #include <sys/malloc.h>
54 #include <sys/mount.h>
55 #include <sys/mutex.h>
57 #include <sys/ptrace.h>
58 #include <sys/refcount.h>
59 #include <sys/resourcevar.h>
60 #include <sys/rwlock.h>
62 #include <sys/sysent.h>
63 #include <sys/sched.h>
65 #include <sys/stack.h>
67 #include <sys/sysctl.h>
68 #include <sys/filedesc.h>
70 #include <sys/signalvar.h>
74 #include <sys/vnode.h>
82 #include <vm/vm_param.h>
83 #include <vm/vm_extern.h>
85 #include <vm/vm_map.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
90 #ifdef COMPAT_FREEBSD32
91 #include <compat/freebsd32/freebsd32.h>
92 #include <compat/freebsd32/freebsd32_util.h>
95 SDT_PROVIDER_DEFINE(proc);
97 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
98 MALLOC_DEFINE(M_SESSION, "session", "session header");
99 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
100 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
102 static void doenterpgrp(struct proc *, struct pgrp *);
103 static void orphanpg(struct pgrp *pg);
104 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
105 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
106 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
108 static void pgadjustjobc(struct pgrp *pgrp, int entering);
109 static void pgdelete(struct pgrp *);
110 static int proc_ctor(void *mem, int size, void *arg, int flags);
111 static void proc_dtor(void *mem, int size, void *arg);
112 static int proc_init(void *mem, int size, int flags);
113 static void proc_fini(void *mem, int size);
114 static void pargs_free(struct pargs *pa);
115 static struct proc *zpfind_locked(pid_t pid);
118 * Other process lists
120 struct pidhashhead *pidhashtbl;
122 struct pgrphashhead *pgrphashtbl;
124 struct proclist allproc;
125 struct proclist zombproc;
126 struct sx __exclusive_cache_line allproc_lock;
127 struct sx __exclusive_cache_line proctree_lock;
128 struct mtx __exclusive_cache_line ppeers_lock;
129 uma_zone_t proc_zone;
132 * The offset of various fields in struct proc and struct thread.
133 * These are used by kernel debuggers to enumerate kernel threads and
136 const int proc_off_p_pid = offsetof(struct proc, p_pid);
137 const int proc_off_p_comm = offsetof(struct proc, p_comm);
138 const int proc_off_p_list = offsetof(struct proc, p_list);
139 const int proc_off_p_threads = offsetof(struct proc, p_threads);
140 const int thread_off_td_tid = offsetof(struct thread, td_tid);
141 const int thread_off_td_name = offsetof(struct thread, td_name);
142 const int thread_off_td_oncpu = offsetof(struct thread, td_oncpu);
143 const int thread_off_td_pcb = offsetof(struct thread, td_pcb);
144 const int thread_off_td_plist = offsetof(struct thread, td_plist);
146 EVENTHANDLER_LIST_DEFINE(process_ctor);
147 EVENTHANDLER_LIST_DEFINE(process_dtor);
148 EVENTHANDLER_LIST_DEFINE(process_init);
149 EVENTHANDLER_LIST_DEFINE(process_fini);
150 EVENTHANDLER_LIST_DEFINE(process_exit);
151 EVENTHANDLER_LIST_DEFINE(process_fork);
152 EVENTHANDLER_LIST_DEFINE(process_exec);
154 EVENTHANDLER_LIST_DECLARE(thread_ctor);
155 EVENTHANDLER_LIST_DECLARE(thread_dtor);
157 int kstack_pages = KSTACK_PAGES;
158 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
159 "Kernel stack size in pages");
160 static int vmmap_skip_res_cnt = 0;
161 SYSCTL_INT(_kern, OID_AUTO, proc_vmmap_skip_resident_count, CTLFLAG_RW,
162 &vmmap_skip_res_cnt, 0,
163 "Skip calculation of the pages resident count in kern.proc.vmmap");
165 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
166 #ifdef COMPAT_FREEBSD32
167 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
171 * Initialize global process hashing structures.
177 sx_init(&allproc_lock, "allproc");
178 sx_init(&proctree_lock, "proctree");
179 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
181 LIST_INIT(&zombproc);
182 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
183 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
184 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
185 proc_ctor, proc_dtor, proc_init, proc_fini,
186 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
191 * Prepare a proc for use.
194 proc_ctor(void *mem, int size, void *arg, int flags)
199 p = (struct proc *)mem;
200 EVENTHANDLER_DIRECT_INVOKE(process_ctor, p);
201 td = FIRST_THREAD_IN_PROC(p);
203 /* Make sure all thread constructors are executed */
204 EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td);
210 * Reclaim a proc after use.
213 proc_dtor(void *mem, int size, void *arg)
218 /* INVARIANTS checks go here */
219 p = (struct proc *)mem;
220 td = FIRST_THREAD_IN_PROC(p);
223 KASSERT((p->p_numthreads == 1),
224 ("bad number of threads in exiting process"));
225 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
227 /* Free all OSD associated to this thread. */
229 td_softdep_cleanup(td);
230 MPASS(td->td_su == NULL);
232 /* Make sure all thread destructors are executed */
233 EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td);
235 EVENTHANDLER_DIRECT_INVOKE(process_dtor, p);
236 if (p->p_ksi != NULL)
237 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
241 * Initialize type-stable parts of a proc (when newly created).
244 proc_init(void *mem, int size, int flags)
248 p = (struct proc *)mem;
249 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK | MTX_NEW);
250 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_NEW);
251 mtx_init(&p->p_statmtx, "pstatl", NULL, MTX_SPIN | MTX_NEW);
252 mtx_init(&p->p_itimmtx, "pitiml", NULL, MTX_SPIN | MTX_NEW);
253 mtx_init(&p->p_profmtx, "pprofl", NULL, MTX_SPIN | MTX_NEW);
254 cv_init(&p->p_pwait, "ppwait");
255 TAILQ_INIT(&p->p_threads); /* all threads in proc */
256 EVENTHANDLER_DIRECT_INVOKE(process_init, p);
257 p->p_stats = pstats_alloc();
263 * UMA should ensure that this function is never called.
264 * Freeing a proc structure would violate type stability.
267 proc_fini(void *mem, int size)
272 p = (struct proc *)mem;
273 EVENTHANDLER_DIRECT_INVOKE(process_fini, p);
274 pstats_free(p->p_stats);
275 thread_free(FIRST_THREAD_IN_PROC(p));
276 mtx_destroy(&p->p_mtx);
277 if (p->p_ksi != NULL)
278 ksiginfo_free(p->p_ksi);
280 panic("proc reclaimed");
285 * Is p an inferior of the current process?
288 inferior(struct proc *p)
291 sx_assert(&proctree_lock, SX_LOCKED);
292 PROC_LOCK_ASSERT(p, MA_OWNED);
293 for (; p != curproc; p = proc_realparent(p)) {
301 pfind_locked(pid_t pid)
305 sx_assert(&allproc_lock, SX_LOCKED);
306 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
307 if (p->p_pid == pid) {
309 if (p->p_state == PRS_NEW) {
320 * Locate a process by number; return only "live" processes -- i.e., neither
321 * zombies nor newly born but incompletely initialized processes. By not
322 * returning processes in the PRS_NEW state, we allow callers to avoid
323 * testing for that condition to avoid dereferencing p_ucred, et al.
331 if (p->p_pid == pid) {
335 sx_slock(&allproc_lock);
336 p = pfind_locked(pid);
337 sx_sunlock(&allproc_lock);
342 * Same as pfind but allow zombies.
349 sx_slock(&allproc_lock);
350 p = pfind_locked(pid);
352 p = zpfind_locked(pid);
353 sx_sunlock(&allproc_lock);
359 pfind_tid_locked(pid_t tid)
364 sx_assert(&allproc_lock, SX_LOCKED);
365 FOREACH_PROC_IN_SYSTEM(p) {
367 if (p->p_state == PRS_NEW) {
371 FOREACH_THREAD_IN_PROC(p, td) {
372 if (td->td_tid == tid)
382 * Locate a process group by number.
383 * The caller must hold proctree_lock.
390 sx_assert(&proctree_lock, SX_LOCKED);
392 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
393 if (pgrp->pg_id == pgid) {
402 * Locate process and do additional manipulations, depending on flags.
405 pget(pid_t pid, int flags, struct proc **pp)
411 if (p->p_pid == pid) {
414 sx_slock(&allproc_lock);
415 if (pid <= PID_MAX) {
416 p = pfind_locked(pid);
417 if (p == NULL && (flags & PGET_NOTWEXIT) == 0)
418 p = zpfind_locked(pid);
419 } else if ((flags & PGET_NOTID) == 0) {
420 p = pfind_tid_locked(pid);
424 sx_sunlock(&allproc_lock);
427 if ((flags & PGET_CANSEE) != 0) {
428 error = p_cansee(curthread, p);
433 if ((flags & PGET_CANDEBUG) != 0) {
434 error = p_candebug(curthread, p);
438 if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
442 if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
446 if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
448 * XXXRW: Not clear ESRCH is the right error during proc
454 if ((flags & PGET_HOLD) != 0) {
466 * Create a new process group.
467 * pgid must be equal to the pid of p.
468 * Begin a new session if required.
471 enterpgrp(struct proc *p, pid_t pgid, struct pgrp *pgrp, struct session *sess)
474 sx_assert(&proctree_lock, SX_XLOCKED);
476 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
477 KASSERT(p->p_pid == pgid,
478 ("enterpgrp: new pgrp and pid != pgid"));
479 KASSERT(pgfind(pgid) == NULL,
480 ("enterpgrp: pgrp with pgid exists"));
481 KASSERT(!SESS_LEADER(p),
482 ("enterpgrp: session leader attempted setpgrp"));
484 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
490 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
492 p->p_flag &= ~P_CONTROLT;
496 sess->s_sid = p->p_pid;
497 refcount_init(&sess->s_count, 1);
498 sess->s_ttyvp = NULL;
499 sess->s_ttydp = NULL;
501 bcopy(p->p_session->s_login, sess->s_login,
502 sizeof(sess->s_login));
503 pgrp->pg_session = sess;
504 KASSERT(p == curproc,
505 ("enterpgrp: mksession and p != curproc"));
507 pgrp->pg_session = p->p_session;
508 sess_hold(pgrp->pg_session);
512 LIST_INIT(&pgrp->pg_members);
515 * As we have an exclusive lock of proctree_lock,
516 * this should not deadlock.
518 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
520 SLIST_INIT(&pgrp->pg_sigiolst);
523 doenterpgrp(p, pgrp);
529 * Move p to an existing process group
532 enterthispgrp(struct proc *p, struct pgrp *pgrp)
535 sx_assert(&proctree_lock, SX_XLOCKED);
536 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
537 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
538 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
539 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
540 KASSERT(pgrp->pg_session == p->p_session,
541 ("%s: pgrp's session %p, p->p_session %p.\n",
545 KASSERT(pgrp != p->p_pgrp,
546 ("%s: p belongs to pgrp.", __func__));
548 doenterpgrp(p, pgrp);
554 * Move p to a process group
557 doenterpgrp(struct proc *p, struct pgrp *pgrp)
559 struct pgrp *savepgrp;
561 sx_assert(&proctree_lock, SX_XLOCKED);
562 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
563 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
564 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
565 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
567 savepgrp = p->p_pgrp;
570 * Adjust eligibility of affected pgrps to participate in job control.
571 * Increment eligibility counts before decrementing, otherwise we
572 * could reach 0 spuriously during the first call.
575 fixjobc(p, p->p_pgrp, 0);
580 LIST_REMOVE(p, p_pglist);
583 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
584 PGRP_UNLOCK(savepgrp);
586 if (LIST_EMPTY(&savepgrp->pg_members))
591 * remove process from process group
594 leavepgrp(struct proc *p)
596 struct pgrp *savepgrp;
598 sx_assert(&proctree_lock, SX_XLOCKED);
599 savepgrp = p->p_pgrp;
602 LIST_REMOVE(p, p_pglist);
605 PGRP_UNLOCK(savepgrp);
606 if (LIST_EMPTY(&savepgrp->pg_members))
612 * delete a process group
615 pgdelete(struct pgrp *pgrp)
617 struct session *savesess;
620 sx_assert(&proctree_lock, SX_XLOCKED);
621 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
622 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
625 * Reset any sigio structures pointing to us as a result of
626 * F_SETOWN with our pgid.
628 funsetownlst(&pgrp->pg_sigiolst);
631 tp = pgrp->pg_session->s_ttyp;
632 LIST_REMOVE(pgrp, pg_hash);
633 savesess = pgrp->pg_session;
636 /* Remove the reference to the pgrp before deallocating it. */
639 tty_rel_pgrp(tp, pgrp);
642 mtx_destroy(&pgrp->pg_mtx);
644 sess_release(savesess);
648 pgadjustjobc(struct pgrp *pgrp, int entering)
656 if (pgrp->pg_jobc == 0)
663 * Adjust pgrp jobc counters when specified process changes process group.
664 * We count the number of processes in each process group that "qualify"
665 * the group for terminal job control (those with a parent in a different
666 * process group of the same session). If that count reaches zero, the
667 * process group becomes orphaned. Check both the specified process'
668 * process group and that of its children.
669 * entering == 0 => p is leaving specified group.
670 * entering == 1 => p is entering specified group.
673 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
675 struct pgrp *hispgrp;
676 struct session *mysession;
679 sx_assert(&proctree_lock, SX_LOCKED);
680 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
681 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
682 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
685 * Check p's parent to see whether p qualifies its own process
686 * group; if so, adjust count for p's process group.
688 mysession = pgrp->pg_session;
689 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
690 hispgrp->pg_session == mysession)
691 pgadjustjobc(pgrp, entering);
694 * Check this process' children to see whether they qualify
695 * their process groups; if so, adjust counts for children's
698 LIST_FOREACH(q, &p->p_children, p_sibling) {
700 if (hispgrp == pgrp ||
701 hispgrp->pg_session != mysession)
703 if (q->p_state == PRS_ZOMBIE)
705 pgadjustjobc(hispgrp, entering);
718 MPASS(p->p_flag & P_WEXIT);
720 * Do a quick check to see if there is anything to do with the
721 * proctree_lock held. pgrp and LIST_EMPTY checks are for fixjobc().
724 if (!SESS_LEADER(p) &&
725 (p->p_pgrp == p->p_pptr->p_pgrp) &&
726 LIST_EMPTY(&p->p_children)) {
732 sx_xlock(&proctree_lock);
733 if (SESS_LEADER(p)) {
737 * s_ttyp is not zero'd; we use this to indicate that
738 * the session once had a controlling terminal. (for
739 * logging and informational purposes)
750 * Signal foreground pgrp and revoke access to
751 * controlling terminal if it has not been revoked
754 * Because the TTY may have been revoked in the mean
755 * time and could already have a new session associated
756 * with it, make sure we don't send a SIGHUP to a
757 * foreground process group that does not belong to this
763 if (tp->t_session == sp)
764 tty_signal_pgrp(tp, SIGHUP);
769 sx_xunlock(&proctree_lock);
770 if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) {
771 VOP_REVOKE(ttyvp, REVOKEALL);
772 VOP_UNLOCK(ttyvp, 0);
775 sx_xlock(&proctree_lock);
778 fixjobc(p, p->p_pgrp, 0);
779 sx_xunlock(&proctree_lock);
783 * A process group has become orphaned;
784 * if there are any stopped processes in the group,
785 * hang-up all process in that group.
788 orphanpg(struct pgrp *pg)
792 PGRP_LOCK_ASSERT(pg, MA_OWNED);
794 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
796 if (P_SHOULDSTOP(p) == P_STOPPED_SIG) {
798 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
800 kern_psignal(p, SIGHUP);
801 kern_psignal(p, SIGCONT);
811 sess_hold(struct session *s)
814 refcount_acquire(&s->s_count);
818 sess_release(struct session *s)
821 if (refcount_release(&s->s_count)) {
822 if (s->s_ttyp != NULL) {
824 tty_rel_sess(s->s_ttyp, s);
826 mtx_destroy(&s->s_mtx);
833 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
839 for (i = 0; i <= pgrphash; i++) {
840 if (!LIST_EMPTY(&pgrphashtbl[i])) {
841 printf("\tindx %d\n", i);
842 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
844 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
845 (void *)pgrp, (long)pgrp->pg_id,
846 (void *)pgrp->pg_session,
847 pgrp->pg_session->s_count,
848 (void *)LIST_FIRST(&pgrp->pg_members));
849 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
850 printf("\t\tpid %ld addr %p pgrp %p\n",
851 (long)p->p_pid, (void *)p,
861 * Calculate the kinfo_proc members which contain process-wide
863 * Must be called with the target process locked.
866 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
870 PROC_LOCK_ASSERT(p, MA_OWNED);
874 FOREACH_THREAD_IN_PROC(p, td) {
876 kp->ki_pctcpu += sched_pctcpu(td);
877 kp->ki_estcpu += sched_estcpu(td);
883 * Clear kinfo_proc and fill in any information that is common
884 * to all threads in the process.
885 * Must be called with the target process locked.
888 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
895 struct timeval boottime;
897 /* For proc_realparent. */
898 sx_assert(&proctree_lock, SX_LOCKED);
899 PROC_LOCK_ASSERT(p, MA_OWNED);
900 bzero(kp, sizeof(*kp));
902 kp->ki_structsize = sizeof(*kp);
904 kp->ki_addr =/* p->p_addr; */0; /* XXX */
905 kp->ki_args = p->p_args;
906 kp->ki_textvp = p->p_textvp;
908 kp->ki_tracep = p->p_tracevp;
909 kp->ki_traceflag = p->p_traceflag;
912 kp->ki_vmspace = p->p_vmspace;
913 kp->ki_flag = p->p_flag;
914 kp->ki_flag2 = p->p_flag2;
917 kp->ki_uid = cred->cr_uid;
918 kp->ki_ruid = cred->cr_ruid;
919 kp->ki_svuid = cred->cr_svuid;
921 if (cred->cr_flags & CRED_FLAG_CAPMODE)
922 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
923 /* XXX bde doesn't like KI_NGROUPS */
924 if (cred->cr_ngroups > KI_NGROUPS) {
925 kp->ki_ngroups = KI_NGROUPS;
926 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
928 kp->ki_ngroups = cred->cr_ngroups;
929 bcopy(cred->cr_groups, kp->ki_groups,
930 kp->ki_ngroups * sizeof(gid_t));
931 kp->ki_rgid = cred->cr_rgid;
932 kp->ki_svgid = cred->cr_svgid;
933 /* If jailed(cred), emulate the old P_JAILED flag. */
935 kp->ki_flag |= P_JAILED;
936 /* If inside the jail, use 0 as a jail ID. */
937 if (cred->cr_prison != curthread->td_ucred->cr_prison)
938 kp->ki_jid = cred->cr_prison->pr_id;
940 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
941 sizeof(kp->ki_loginclass));
945 mtx_lock(&ps->ps_mtx);
946 kp->ki_sigignore = ps->ps_sigignore;
947 kp->ki_sigcatch = ps->ps_sigcatch;
948 mtx_unlock(&ps->ps_mtx);
950 if (p->p_state != PRS_NEW &&
951 p->p_state != PRS_ZOMBIE &&
952 p->p_vmspace != NULL) {
953 struct vmspace *vm = p->p_vmspace;
955 kp->ki_size = vm->vm_map.size;
956 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
957 FOREACH_THREAD_IN_PROC(p, td0) {
958 if (!TD_IS_SWAPPED(td0))
959 kp->ki_rssize += td0->td_kstack_pages;
961 kp->ki_swrss = vm->vm_swrss;
962 kp->ki_tsize = vm->vm_tsize;
963 kp->ki_dsize = vm->vm_dsize;
964 kp->ki_ssize = vm->vm_ssize;
965 } else if (p->p_state == PRS_ZOMBIE)
967 if (kp->ki_flag & P_INMEM)
968 kp->ki_sflag = PS_INMEM;
971 /* Calculate legacy swtime as seconds since 'swtick'. */
972 kp->ki_swtime = (ticks - p->p_swtick) / hz;
973 kp->ki_pid = p->p_pid;
974 kp->ki_nice = p->p_nice;
975 kp->ki_fibnum = p->p_fibnum;
976 kp->ki_start = p->p_stats->p_start;
977 getboottime(&boottime);
978 timevaladd(&kp->ki_start, &boottime);
980 rufetch(p, &kp->ki_rusage);
981 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
982 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
984 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
985 /* Some callers want child times in a single value. */
986 kp->ki_childtime = kp->ki_childstime;
987 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
989 FOREACH_THREAD_IN_PROC(p, td0)
990 kp->ki_cow += td0->td_cow;
994 kp->ki_pgid = p->p_pgrp->pg_id;
995 kp->ki_jobc = p->p_pgrp->pg_jobc;
996 sp = p->p_pgrp->pg_session;
999 kp->ki_sid = sp->s_sid;
1001 strlcpy(kp->ki_login, sp->s_login,
1002 sizeof(kp->ki_login));
1004 kp->ki_kiflag |= KI_CTTY;
1006 kp->ki_kiflag |= KI_SLEADER;
1007 /* XXX proctree_lock */
1012 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
1013 kp->ki_tdev = tty_udev(tp);
1014 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1015 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1017 kp->ki_tsid = tp->t_session->s_sid;
1019 kp->ki_tdev = NODEV;
1020 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1022 if (p->p_comm[0] != '\0')
1023 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
1024 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
1025 p->p_sysent->sv_name[0] != '\0')
1026 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
1027 kp->ki_siglist = p->p_siglist;
1028 kp->ki_xstat = KW_EXITCODE(p->p_xexit, p->p_xsig);
1029 kp->ki_acflag = p->p_acflag;
1030 kp->ki_lock = p->p_lock;
1032 kp->ki_ppid = proc_realparent(p)->p_pid;
1033 if (p->p_flag & P_TRACED)
1034 kp->ki_tracer = p->p_pptr->p_pid;
1039 * Fill in information that is thread specific. Must be called with
1040 * target process locked. If 'preferthread' is set, overwrite certain
1041 * process-related fields that are maintained for both threads and
1045 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
1051 PROC_LOCK_ASSERT(p, MA_OWNED);
1056 if (td->td_wmesg != NULL)
1057 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
1059 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
1060 if (strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)) >=
1061 sizeof(kp->ki_tdname)) {
1062 strlcpy(kp->ki_moretdname,
1063 td->td_name + sizeof(kp->ki_tdname) - 1,
1064 sizeof(kp->ki_moretdname));
1066 bzero(kp->ki_moretdname, sizeof(kp->ki_moretdname));
1068 if (TD_ON_LOCK(td)) {
1069 kp->ki_kiflag |= KI_LOCKBLOCK;
1070 strlcpy(kp->ki_lockname, td->td_lockname,
1071 sizeof(kp->ki_lockname));
1073 kp->ki_kiflag &= ~KI_LOCKBLOCK;
1074 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
1077 if (p->p_state == PRS_NORMAL) { /* approximate. */
1078 if (TD_ON_RUNQ(td) ||
1080 TD_IS_RUNNING(td)) {
1082 } else if (P_SHOULDSTOP(p)) {
1083 kp->ki_stat = SSTOP;
1084 } else if (TD_IS_SLEEPING(td)) {
1085 kp->ki_stat = SSLEEP;
1086 } else if (TD_ON_LOCK(td)) {
1087 kp->ki_stat = SLOCK;
1089 kp->ki_stat = SWAIT;
1091 } else if (p->p_state == PRS_ZOMBIE) {
1092 kp->ki_stat = SZOMB;
1097 /* Things in the thread */
1098 kp->ki_wchan = td->td_wchan;
1099 kp->ki_pri.pri_level = td->td_priority;
1100 kp->ki_pri.pri_native = td->td_base_pri;
1103 * Note: legacy fields; clamp at the old NOCPU value and/or
1104 * the maximum u_char CPU value.
1106 if (td->td_lastcpu == NOCPU)
1107 kp->ki_lastcpu_old = NOCPU_OLD;
1108 else if (td->td_lastcpu > MAXCPU_OLD)
1109 kp->ki_lastcpu_old = MAXCPU_OLD;
1111 kp->ki_lastcpu_old = td->td_lastcpu;
1113 if (td->td_oncpu == NOCPU)
1114 kp->ki_oncpu_old = NOCPU_OLD;
1115 else if (td->td_oncpu > MAXCPU_OLD)
1116 kp->ki_oncpu_old = MAXCPU_OLD;
1118 kp->ki_oncpu_old = td->td_oncpu;
1120 kp->ki_lastcpu = td->td_lastcpu;
1121 kp->ki_oncpu = td->td_oncpu;
1122 kp->ki_tdflags = td->td_flags;
1123 kp->ki_tid = td->td_tid;
1124 kp->ki_numthreads = p->p_numthreads;
1125 kp->ki_pcb = td->td_pcb;
1126 kp->ki_kstack = (void *)td->td_kstack;
1127 kp->ki_slptime = (ticks - td->td_slptick) / hz;
1128 kp->ki_pri.pri_class = td->td_pri_class;
1129 kp->ki_pri.pri_user = td->td_user_pri;
1132 rufetchtd(td, &kp->ki_rusage);
1133 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
1134 kp->ki_pctcpu = sched_pctcpu(td);
1135 kp->ki_estcpu = sched_estcpu(td);
1136 kp->ki_cow = td->td_cow;
1139 /* We can't get this anymore but ps etc never used it anyway. */
1143 kp->ki_siglist = td->td_siglist;
1144 kp->ki_sigmask = td->td_sigmask;
1151 * Fill in a kinfo_proc structure for the specified process.
1152 * Must be called with the target process locked.
1155 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1158 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1160 fill_kinfo_proc_only(p, kp);
1161 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1162 fill_kinfo_aggregate(p, kp);
1169 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1173 * Copy parts of p_stats; zero the rest of p_stats (statistics).
1176 pstats_fork(struct pstats *src, struct pstats *dst)
1179 bzero(&dst->pstat_startzero,
1180 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1181 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1182 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1186 pstats_free(struct pstats *ps)
1189 free(ps, M_SUBPROC);
1192 static struct proc *
1193 zpfind_locked(pid_t pid)
1197 sx_assert(&allproc_lock, SX_LOCKED);
1198 LIST_FOREACH(p, &zombproc, p_list) {
1199 if (p->p_pid == pid) {
1208 * Locate a zombie process by number
1215 sx_slock(&allproc_lock);
1216 p = zpfind_locked(pid);
1217 sx_sunlock(&allproc_lock);
1221 #ifdef COMPAT_FREEBSD32
1224 * This function is typically used to copy out the kernel address, so
1225 * it can be replaced by assignment of zero.
1227 static inline uint32_t
1228 ptr32_trim(void *ptr)
1232 uptr = (uintptr_t)ptr;
1233 return ((uptr > UINT_MAX) ? 0 : uptr);
1236 #define PTRTRIM_CP(src,dst,fld) \
1237 do { (dst).fld = ptr32_trim((src).fld); } while (0)
1240 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1244 bzero(ki32, sizeof(struct kinfo_proc32));
1245 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1246 CP(*ki, *ki32, ki_layout);
1247 PTRTRIM_CP(*ki, *ki32, ki_args);
1248 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1249 PTRTRIM_CP(*ki, *ki32, ki_addr);
1250 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1251 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1252 PTRTRIM_CP(*ki, *ki32, ki_fd);
1253 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1254 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1255 CP(*ki, *ki32, ki_pid);
1256 CP(*ki, *ki32, ki_ppid);
1257 CP(*ki, *ki32, ki_pgid);
1258 CP(*ki, *ki32, ki_tpgid);
1259 CP(*ki, *ki32, ki_sid);
1260 CP(*ki, *ki32, ki_tsid);
1261 CP(*ki, *ki32, ki_jobc);
1262 CP(*ki, *ki32, ki_tdev);
1263 CP(*ki, *ki32, ki_tdev_freebsd11);
1264 CP(*ki, *ki32, ki_siglist);
1265 CP(*ki, *ki32, ki_sigmask);
1266 CP(*ki, *ki32, ki_sigignore);
1267 CP(*ki, *ki32, ki_sigcatch);
1268 CP(*ki, *ki32, ki_uid);
1269 CP(*ki, *ki32, ki_ruid);
1270 CP(*ki, *ki32, ki_svuid);
1271 CP(*ki, *ki32, ki_rgid);
1272 CP(*ki, *ki32, ki_svgid);
1273 CP(*ki, *ki32, ki_ngroups);
1274 for (i = 0; i < KI_NGROUPS; i++)
1275 CP(*ki, *ki32, ki_groups[i]);
1276 CP(*ki, *ki32, ki_size);
1277 CP(*ki, *ki32, ki_rssize);
1278 CP(*ki, *ki32, ki_swrss);
1279 CP(*ki, *ki32, ki_tsize);
1280 CP(*ki, *ki32, ki_dsize);
1281 CP(*ki, *ki32, ki_ssize);
1282 CP(*ki, *ki32, ki_xstat);
1283 CP(*ki, *ki32, ki_acflag);
1284 CP(*ki, *ki32, ki_pctcpu);
1285 CP(*ki, *ki32, ki_estcpu);
1286 CP(*ki, *ki32, ki_slptime);
1287 CP(*ki, *ki32, ki_swtime);
1288 CP(*ki, *ki32, ki_cow);
1289 CP(*ki, *ki32, ki_runtime);
1290 TV_CP(*ki, *ki32, ki_start);
1291 TV_CP(*ki, *ki32, ki_childtime);
1292 CP(*ki, *ki32, ki_flag);
1293 CP(*ki, *ki32, ki_kiflag);
1294 CP(*ki, *ki32, ki_traceflag);
1295 CP(*ki, *ki32, ki_stat);
1296 CP(*ki, *ki32, ki_nice);
1297 CP(*ki, *ki32, ki_lock);
1298 CP(*ki, *ki32, ki_rqindex);
1299 CP(*ki, *ki32, ki_oncpu);
1300 CP(*ki, *ki32, ki_lastcpu);
1302 /* XXX TODO: wrap cpu value as appropriate */
1303 CP(*ki, *ki32, ki_oncpu_old);
1304 CP(*ki, *ki32, ki_lastcpu_old);
1306 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1307 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1308 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1309 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1310 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1311 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1312 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1313 bcopy(ki->ki_moretdname, ki32->ki_moretdname, MAXCOMLEN - TDNAMLEN + 1);
1314 CP(*ki, *ki32, ki_tracer);
1315 CP(*ki, *ki32, ki_flag2);
1316 CP(*ki, *ki32, ki_fibnum);
1317 CP(*ki, *ki32, ki_cr_flags);
1318 CP(*ki, *ki32, ki_jid);
1319 CP(*ki, *ki32, ki_numthreads);
1320 CP(*ki, *ki32, ki_tid);
1321 CP(*ki, *ki32, ki_pri);
1322 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1323 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1324 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1325 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1326 PTRTRIM_CP(*ki, *ki32, ki_udata);
1327 PTRTRIM_CP(*ki, *ki32, ki_tdaddr);
1328 CP(*ki, *ki32, ki_sflag);
1329 CP(*ki, *ki32, ki_tdflags);
1334 kern_proc_out_size(struct proc *p, int flags)
1338 PROC_LOCK_ASSERT(p, MA_OWNED);
1340 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1341 #ifdef COMPAT_FREEBSD32
1342 if ((flags & KERN_PROC_MASK32) != 0) {
1343 size += sizeof(struct kinfo_proc32);
1346 size += sizeof(struct kinfo_proc);
1348 #ifdef COMPAT_FREEBSD32
1349 if ((flags & KERN_PROC_MASK32) != 0)
1350 size += sizeof(struct kinfo_proc32) * p->p_numthreads;
1353 size += sizeof(struct kinfo_proc) * p->p_numthreads;
1360 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
1363 struct kinfo_proc ki;
1364 #ifdef COMPAT_FREEBSD32
1365 struct kinfo_proc32 ki32;
1369 PROC_LOCK_ASSERT(p, MA_OWNED);
1370 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1373 fill_kinfo_proc(p, &ki);
1374 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1375 #ifdef COMPAT_FREEBSD32
1376 if ((flags & KERN_PROC_MASK32) != 0) {
1377 freebsd32_kinfo_proc_out(&ki, &ki32);
1378 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1382 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1385 FOREACH_THREAD_IN_PROC(p, td) {
1386 fill_kinfo_thread(td, &ki, 1);
1387 #ifdef COMPAT_FREEBSD32
1388 if ((flags & KERN_PROC_MASK32) != 0) {
1389 freebsd32_kinfo_proc_out(&ki, &ki32);
1390 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1394 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1405 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1408 struct kinfo_proc ki;
1411 if (req->oldptr == NULL)
1412 return (SYSCTL_OUT(req, 0, kern_proc_out_size(p, flags)));
1414 sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
1415 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1416 error = kern_proc_out(p, &sb, flags);
1417 error2 = sbuf_finish(&sb);
1421 else if (error2 != 0)
1427 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1429 int *name = (int *)arg1;
1430 u_int namelen = arg2;
1432 int flags, doingzomb, oid_number;
1435 oid_number = oidp->oid_number;
1436 if (oid_number != KERN_PROC_ALL &&
1437 (oid_number & KERN_PROC_INC_THREAD) == 0)
1438 flags = KERN_PROC_NOTHREADS;
1441 oid_number &= ~KERN_PROC_INC_THREAD;
1443 #ifdef COMPAT_FREEBSD32
1444 if (req->flags & SCTL_MASK32)
1445 flags |= KERN_PROC_MASK32;
1447 if (oid_number == KERN_PROC_PID) {
1450 error = sysctl_wire_old_buffer(req, 0);
1453 sx_slock(&proctree_lock);
1454 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1456 error = sysctl_out_proc(p, req, flags);
1457 sx_sunlock(&proctree_lock);
1461 switch (oid_number) {
1466 case KERN_PROC_PROC:
1467 if (namelen != 0 && namelen != 1)
1476 if (req->oldptr == NULL) {
1477 /* overestimate by 5 procs */
1478 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1482 error = sysctl_wire_old_buffer(req, 0);
1486 * This lock is only needed to safely grab the parent of a
1487 * traced process. Only grab it if we are producing any
1488 * data to begin with.
1490 sx_slock(&proctree_lock);
1492 sx_slock(&allproc_lock);
1493 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
1495 p = LIST_FIRST(&allproc);
1497 p = LIST_FIRST(&zombproc);
1498 for (; p != NULL; p = LIST_NEXT(p, p_list)) {
1500 * Skip embryonic processes.
1502 if (p->p_state == PRS_NEW)
1505 KASSERT(p->p_ucred != NULL,
1506 ("process credential is NULL for non-NEW proc"));
1508 * Show a user only appropriate processes.
1510 if (p_cansee(curthread, p)) {
1515 * TODO - make more efficient (see notes below).
1518 switch (oid_number) {
1521 if (p->p_ucred->cr_gid != (gid_t)name[0]) {
1527 case KERN_PROC_PGRP:
1528 /* could do this by traversing pgrp */
1529 if (p->p_pgrp == NULL ||
1530 p->p_pgrp->pg_id != (pid_t)name[0]) {
1536 case KERN_PROC_RGID:
1537 if (p->p_ucred->cr_rgid != (gid_t)name[0]) {
1543 case KERN_PROC_SESSION:
1544 if (p->p_session == NULL ||
1545 p->p_session->s_sid != (pid_t)name[0]) {
1552 if ((p->p_flag & P_CONTROLT) == 0 ||
1553 p->p_session == NULL) {
1557 /* XXX proctree_lock */
1558 SESS_LOCK(p->p_session);
1559 if (p->p_session->s_ttyp == NULL ||
1560 tty_udev(p->p_session->s_ttyp) !=
1562 SESS_UNLOCK(p->p_session);
1566 SESS_UNLOCK(p->p_session);
1570 if (p->p_ucred->cr_uid != (uid_t)name[0]) {
1576 case KERN_PROC_RUID:
1577 if (p->p_ucred->cr_ruid != (uid_t)name[0]) {
1583 case KERN_PROC_PROC:
1591 error = sysctl_out_proc(p, req, flags);
1597 sx_sunlock(&allproc_lock);
1598 if (req->oldptr != NULL)
1599 sx_sunlock(&proctree_lock);
1604 pargs_alloc(int len)
1608 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1610 refcount_init(&pa->ar_ref, 1);
1611 pa->ar_length = len;
1616 pargs_free(struct pargs *pa)
1623 pargs_hold(struct pargs *pa)
1628 refcount_acquire(&pa->ar_ref);
1632 pargs_drop(struct pargs *pa)
1637 if (refcount_release(&pa->ar_ref))
1642 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1648 * This may return a short read if the string is shorter than the chunk
1649 * and is aligned at the end of the page, and the following page is not
1652 n = proc_readmem(td, p, (vm_offset_t)sptr, buf, len);
1658 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1660 enum proc_vector_type {
1666 #ifdef COMPAT_FREEBSD32
1668 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1669 size_t *vsizep, enum proc_vector_type type)
1671 struct freebsd32_ps_strings pss;
1673 vm_offset_t vptr, ptr;
1674 uint32_t *proc_vector32;
1680 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1681 sizeof(pss)) != sizeof(pss))
1685 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1686 vsize = pss.ps_nargvstr;
1687 if (vsize > ARG_MAX)
1689 size = vsize * sizeof(int32_t);
1692 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1693 vsize = pss.ps_nenvstr;
1694 if (vsize > ARG_MAX)
1696 size = vsize * sizeof(int32_t);
1699 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1700 (pss.ps_nenvstr + 1) * sizeof(int32_t);
1703 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1704 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1707 if (aux.a_type == AT_NULL)
1711 if (aux.a_type != AT_NULL)
1714 size = vsize * sizeof(aux);
1717 KASSERT(0, ("Wrong proc vector type: %d", type));
1720 proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1721 if (proc_readmem(td, p, vptr, proc_vector32, size) != size) {
1725 if (type == PROC_AUX) {
1726 *proc_vectorp = (char **)proc_vector32;
1730 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1731 for (i = 0; i < (int)vsize; i++)
1732 proc_vector[i] = PTRIN(proc_vector32[i]);
1733 *proc_vectorp = proc_vector;
1736 free(proc_vector32, M_TEMP);
1742 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1743 size_t *vsizep, enum proc_vector_type type)
1745 struct ps_strings pss;
1747 vm_offset_t vptr, ptr;
1752 #ifdef COMPAT_FREEBSD32
1753 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1754 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1756 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1757 sizeof(pss)) != sizeof(pss))
1761 vptr = (vm_offset_t)pss.ps_argvstr;
1762 vsize = pss.ps_nargvstr;
1763 if (vsize > ARG_MAX)
1765 size = vsize * sizeof(char *);
1768 vptr = (vm_offset_t)pss.ps_envstr;
1769 vsize = pss.ps_nenvstr;
1770 if (vsize > ARG_MAX)
1772 size = vsize * sizeof(char *);
1776 * The aux array is just above env array on the stack. Check
1777 * that the address is naturally aligned.
1779 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1781 #if __ELF_WORD_SIZE == 64
1782 if (vptr % sizeof(uint64_t) != 0)
1784 if (vptr % sizeof(uint32_t) != 0)
1788 * We count the array size reading the aux vectors from the
1789 * stack until AT_NULL vector is returned. So (to keep the code
1790 * simple) we read the process stack twice: the first time here
1791 * to find the size and the second time when copying the vectors
1792 * to the allocated proc_vector.
1794 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1795 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1798 if (aux.a_type == AT_NULL)
1803 * If the PROC_AUXV_MAX entries are iterated over, and we have
1804 * not reached AT_NULL, it is most likely we are reading wrong
1805 * data: either the process doesn't have auxv array or data has
1806 * been modified. Return the error in this case.
1808 if (aux.a_type != AT_NULL)
1811 size = vsize * sizeof(aux);
1814 KASSERT(0, ("Wrong proc vector type: %d", type));
1815 return (EINVAL); /* In case we are built without INVARIANTS. */
1817 proc_vector = malloc(size, M_TEMP, M_WAITOK);
1818 if (proc_readmem(td, p, vptr, proc_vector, size) != size) {
1819 free(proc_vector, M_TEMP);
1822 *proc_vectorp = proc_vector;
1828 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
1831 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
1832 enum proc_vector_type type)
1834 size_t done, len, nchr, vsize;
1836 char **proc_vector, *sptr;
1837 char pss_string[GET_PS_STRINGS_CHUNK_SZ];
1839 PROC_ASSERT_HELD(p);
1842 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
1844 nchr = 2 * (PATH_MAX + ARG_MAX);
1846 error = get_proc_vector(td, p, &proc_vector, &vsize, type);
1849 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
1851 * The program may have scribbled into its argv array, e.g. to
1852 * remove some arguments. If that has happened, break out
1853 * before trying to read from NULL.
1855 if (proc_vector[i] == NULL)
1857 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
1858 error = proc_read_string(td, p, sptr, pss_string,
1859 sizeof(pss_string));
1862 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
1863 if (done + len >= nchr)
1864 len = nchr - done - 1;
1865 sbuf_bcat(sb, pss_string, len);
1866 if (len != GET_PS_STRINGS_CHUNK_SZ)
1868 done += GET_PS_STRINGS_CHUNK_SZ;
1870 sbuf_bcat(sb, "", 1);
1874 free(proc_vector, M_TEMP);
1879 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
1882 return (get_ps_strings(curthread, p, sb, PROC_ARG));
1886 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
1889 return (get_ps_strings(curthread, p, sb, PROC_ENV));
1893 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
1899 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
1901 #ifdef COMPAT_FREEBSD32
1902 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1903 size = vsize * sizeof(Elf32_Auxinfo);
1906 size = vsize * sizeof(Elf_Auxinfo);
1907 if (sbuf_bcat(sb, auxv, size) != 0)
1915 * This sysctl allows a process to retrieve the argument list or process
1916 * title for another process without groping around in the address space
1917 * of the other process. It also allow a process to set its own "process
1918 * title to a string of its own choice.
1921 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1923 int *name = (int *)arg1;
1924 u_int namelen = arg2;
1925 struct pargs *newpa, *pa;
1928 int flags, error = 0, error2;
1934 pid = (pid_t)name[0];
1936 * If the query is for this process and it is single-threaded, there
1937 * is nobody to modify pargs, thus we can just read.
1940 if (pid == p->p_pid && p->p_numthreads == 1 && req->newptr == NULL &&
1941 (pa = p->p_args) != NULL)
1942 return (SYSCTL_OUT(req, pa->ar_args, pa->ar_length));
1944 flags = PGET_CANSEE;
1945 if (req->newptr != NULL)
1946 flags |= PGET_ISCURRENT;
1947 error = pget(pid, flags, &p);
1955 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1957 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
1960 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1961 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1962 error = proc_getargv(curthread, p, &sb);
1963 error2 = sbuf_finish(&sb);
1966 if (error == 0 && error2 != 0)
1971 if (error != 0 || req->newptr == NULL)
1974 if (req->newlen > ps_arg_cache_limit - sizeof(struct pargs))
1977 if (req->newlen == 0) {
1979 * Clear the argument pointer, so that we'll fetch arguments
1980 * with proc_getargv() until further notice.
1984 newpa = pargs_alloc(req->newlen);
1985 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
2000 * This sysctl allows a process to retrieve environment of another process.
2003 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
2005 int *name = (int *)arg1;
2006 u_int namelen = arg2;
2014 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2017 if ((p->p_flag & P_SYSTEM) != 0) {
2022 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2023 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2024 error = proc_getenvv(curthread, p, &sb);
2025 error2 = sbuf_finish(&sb);
2028 return (error != 0 ? error : error2);
2032 * This sysctl allows a process to retrieve ELF auxiliary vector of
2036 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
2038 int *name = (int *)arg1;
2039 u_int namelen = arg2;
2047 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2050 if ((p->p_flag & P_SYSTEM) != 0) {
2054 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2055 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2056 error = proc_getauxv(curthread, p, &sb);
2057 error2 = sbuf_finish(&sb);
2060 return (error != 0 ? error : error2);
2064 * This sysctl allows a process to retrieve the path of the executable for
2065 * itself or another process.
2068 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
2070 pid_t *pidp = (pid_t *)arg1;
2071 unsigned int arglen = arg2;
2074 char *retbuf, *freebuf;
2079 if (*pidp == -1) { /* -1 means this process */
2080 p = req->td->td_proc;
2082 error = pget(*pidp, PGET_CANSEE, &p);
2096 error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
2100 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
2101 free(freebuf, M_TEMP);
2106 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
2119 error = pget((pid_t)name[0], PGET_CANSEE, &p);
2122 sv_name = p->p_sysent->sv_name;
2124 return (sysctl_handle_string(oidp, sv_name, 0, req));
2127 #ifdef KINFO_OVMENTRY_SIZE
2128 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
2131 #ifdef COMPAT_FREEBSD7
2133 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2135 vm_map_entry_t entry, tmp_entry;
2136 unsigned int last_timestamp;
2137 char *fullpath, *freepath;
2138 struct kinfo_ovmentry *kve;
2148 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2151 vm = vmspace_acquire_ref(p);
2156 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2159 vm_map_lock_read(map);
2160 for (entry = map->header.next; entry != &map->header;
2161 entry = entry->next) {
2162 vm_object_t obj, tobj, lobj;
2165 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2168 bzero(kve, sizeof(*kve));
2169 kve->kve_structsize = sizeof(*kve);
2171 kve->kve_private_resident = 0;
2172 obj = entry->object.vm_object;
2174 VM_OBJECT_RLOCK(obj);
2175 if (obj->shadow_count == 1)
2176 kve->kve_private_resident =
2177 obj->resident_page_count;
2179 kve->kve_resident = 0;
2180 addr = entry->start;
2181 while (addr < entry->end) {
2182 if (pmap_extract(map->pmap, addr))
2183 kve->kve_resident++;
2187 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2189 VM_OBJECT_RLOCK(tobj);
2190 kve->kve_offset += tobj->backing_object_offset;
2193 VM_OBJECT_RUNLOCK(lobj);
2197 kve->kve_start = (void*)entry->start;
2198 kve->kve_end = (void*)entry->end;
2199 kve->kve_offset += (off_t)entry->offset;
2201 if (entry->protection & VM_PROT_READ)
2202 kve->kve_protection |= KVME_PROT_READ;
2203 if (entry->protection & VM_PROT_WRITE)
2204 kve->kve_protection |= KVME_PROT_WRITE;
2205 if (entry->protection & VM_PROT_EXECUTE)
2206 kve->kve_protection |= KVME_PROT_EXEC;
2208 if (entry->eflags & MAP_ENTRY_COW)
2209 kve->kve_flags |= KVME_FLAG_COW;
2210 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2211 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2212 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2213 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2215 last_timestamp = map->timestamp;
2216 vm_map_unlock_read(map);
2218 kve->kve_fileid = 0;
2224 switch (lobj->type) {
2226 kve->kve_type = KVME_TYPE_DEFAULT;
2229 kve->kve_type = KVME_TYPE_VNODE;
2234 if ((lobj->flags & OBJ_TMPFS_NODE) != 0) {
2235 kve->kve_type = KVME_TYPE_VNODE;
2236 if ((lobj->flags & OBJ_TMPFS) != 0) {
2237 vp = lobj->un_pager.swp.swp_tmpfs;
2241 kve->kve_type = KVME_TYPE_SWAP;
2245 kve->kve_type = KVME_TYPE_DEVICE;
2248 kve->kve_type = KVME_TYPE_PHYS;
2251 kve->kve_type = KVME_TYPE_DEAD;
2254 kve->kve_type = KVME_TYPE_SG;
2257 kve->kve_type = KVME_TYPE_UNKNOWN;
2261 VM_OBJECT_RUNLOCK(lobj);
2263 kve->kve_ref_count = obj->ref_count;
2264 kve->kve_shadow_count = obj->shadow_count;
2265 VM_OBJECT_RUNLOCK(obj);
2267 vn_fullpath(curthread, vp, &fullpath,
2269 cred = curthread->td_ucred;
2270 vn_lock(vp, LK_SHARED | LK_RETRY);
2271 if (VOP_GETATTR(vp, &va, cred) == 0) {
2272 kve->kve_fileid = va.va_fileid;
2274 kve->kve_fsid = va.va_fsid;
2279 kve->kve_type = KVME_TYPE_NONE;
2280 kve->kve_ref_count = 0;
2281 kve->kve_shadow_count = 0;
2284 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2285 if (freepath != NULL)
2286 free(freepath, M_TEMP);
2288 error = SYSCTL_OUT(req, kve, sizeof(*kve));
2289 vm_map_lock_read(map);
2292 if (last_timestamp != map->timestamp) {
2293 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2297 vm_map_unlock_read(map);
2303 #endif /* COMPAT_FREEBSD7 */
2305 #ifdef KINFO_VMENTRY_SIZE
2306 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2310 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2311 int *resident_count, bool *super)
2313 vm_object_t obj, tobj;
2316 vm_paddr_t locked_pa;
2317 vm_pindex_t pi, pi_adv, pindex;
2320 *resident_count = 0;
2321 if (vmmap_skip_res_cnt)
2325 obj = entry->object.vm_object;
2326 addr = entry->start;
2328 pi = OFF_TO_IDX(entry->offset);
2329 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2330 if (m_adv != NULL) {
2333 pi_adv = atop(entry->end - addr);
2335 for (tobj = obj;; tobj = tobj->backing_object) {
2336 m = vm_page_find_least(tobj, pindex);
2338 if (m->pindex == pindex)
2340 if (pi_adv > m->pindex - pindex) {
2341 pi_adv = m->pindex - pindex;
2345 if (tobj->backing_object == NULL)
2347 pindex += OFF_TO_IDX(tobj->
2348 backing_object_offset);
2352 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2353 (addr & (pagesizes[1] - 1)) == 0 &&
2354 (pmap_mincore(map->pmap, addr, &locked_pa) &
2355 MINCORE_SUPER) != 0) {
2357 pi_adv = atop(pagesizes[1]);
2360 * We do not test the found page on validity.
2361 * Either the page is busy and being paged in,
2362 * or it was invalidated. The first case
2363 * should be counted as resident, the second
2364 * is not so clear; we do account both.
2368 *resident_count += pi_adv;
2371 PA_UNLOCK_COND(locked_pa);
2375 * Must be called with the process locked and will return unlocked.
2378 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
2380 vm_map_entry_t entry, tmp_entry;
2383 vm_object_t obj, tobj, lobj;
2384 char *fullpath, *freepath;
2385 struct kinfo_vmentry *kve;
2390 unsigned int last_timestamp;
2394 PROC_LOCK_ASSERT(p, MA_OWNED);
2398 vm = vmspace_acquire_ref(p);
2403 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK | M_ZERO);
2407 vm_map_lock_read(map);
2408 for (entry = map->header.next; entry != &map->header;
2409 entry = entry->next) {
2410 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2414 bzero(kve, sizeof(*kve));
2415 obj = entry->object.vm_object;
2417 for (tobj = obj; tobj != NULL;
2418 tobj = tobj->backing_object) {
2419 VM_OBJECT_RLOCK(tobj);
2420 kve->kve_offset += tobj->backing_object_offset;
2423 if (obj->backing_object == NULL)
2424 kve->kve_private_resident =
2425 obj->resident_page_count;
2426 kern_proc_vmmap_resident(map, entry,
2427 &kve->kve_resident, &super);
2429 kve->kve_flags |= KVME_FLAG_SUPER;
2430 for (tobj = obj; tobj != NULL;
2431 tobj = tobj->backing_object) {
2432 if (tobj != obj && tobj != lobj)
2433 VM_OBJECT_RUNLOCK(tobj);
2439 kve->kve_start = entry->start;
2440 kve->kve_end = entry->end;
2441 kve->kve_offset += entry->offset;
2443 if (entry->protection & VM_PROT_READ)
2444 kve->kve_protection |= KVME_PROT_READ;
2445 if (entry->protection & VM_PROT_WRITE)
2446 kve->kve_protection |= KVME_PROT_WRITE;
2447 if (entry->protection & VM_PROT_EXECUTE)
2448 kve->kve_protection |= KVME_PROT_EXEC;
2450 if (entry->eflags & MAP_ENTRY_COW)
2451 kve->kve_flags |= KVME_FLAG_COW;
2452 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2453 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2454 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2455 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2456 if (entry->eflags & MAP_ENTRY_GROWS_UP)
2457 kve->kve_flags |= KVME_FLAG_GROWS_UP;
2458 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2459 kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2461 last_timestamp = map->timestamp;
2462 vm_map_unlock_read(map);
2468 switch (lobj->type) {
2470 kve->kve_type = KVME_TYPE_DEFAULT;
2473 kve->kve_type = KVME_TYPE_VNODE;
2478 if ((lobj->flags & OBJ_TMPFS_NODE) != 0) {
2479 kve->kve_type = KVME_TYPE_VNODE;
2480 if ((lobj->flags & OBJ_TMPFS) != 0) {
2481 vp = lobj->un_pager.swp.swp_tmpfs;
2485 kve->kve_type = KVME_TYPE_SWAP;
2489 kve->kve_type = KVME_TYPE_DEVICE;
2492 kve->kve_type = KVME_TYPE_PHYS;
2495 kve->kve_type = KVME_TYPE_DEAD;
2498 kve->kve_type = KVME_TYPE_SG;
2500 case OBJT_MGTDEVICE:
2501 kve->kve_type = KVME_TYPE_MGTDEVICE;
2504 kve->kve_type = KVME_TYPE_UNKNOWN;
2508 VM_OBJECT_RUNLOCK(lobj);
2510 kve->kve_ref_count = obj->ref_count;
2511 kve->kve_shadow_count = obj->shadow_count;
2512 VM_OBJECT_RUNLOCK(obj);
2514 vn_fullpath(curthread, vp, &fullpath,
2516 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2517 cred = curthread->td_ucred;
2518 vn_lock(vp, LK_SHARED | LK_RETRY);
2519 if (VOP_GETATTR(vp, &va, cred) == 0) {
2520 kve->kve_vn_fileid = va.va_fileid;
2521 kve->kve_vn_fsid = va.va_fsid;
2522 kve->kve_vn_fsid_freebsd11 =
2523 kve->kve_vn_fsid; /* truncate */
2525 MAKEIMODE(va.va_type, va.va_mode);
2526 kve->kve_vn_size = va.va_size;
2527 kve->kve_vn_rdev = va.va_rdev;
2528 kve->kve_vn_rdev_freebsd11 =
2529 kve->kve_vn_rdev; /* truncate */
2530 kve->kve_status = KF_ATTR_VALID;
2535 kve->kve_type = KVME_TYPE_NONE;
2536 kve->kve_ref_count = 0;
2537 kve->kve_shadow_count = 0;
2540 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2541 if (freepath != NULL)
2542 free(freepath, M_TEMP);
2544 /* Pack record size down */
2545 if ((flags & KERN_VMMAP_PACK_KINFO) != 0)
2546 kve->kve_structsize =
2547 offsetof(struct kinfo_vmentry, kve_path) +
2548 strlen(kve->kve_path) + 1;
2550 kve->kve_structsize = sizeof(*kve);
2551 kve->kve_structsize = roundup(kve->kve_structsize,
2554 /* Halt filling and truncate rather than exceeding maxlen */
2555 if (maxlen != -1 && maxlen < kve->kve_structsize) {
2557 vm_map_lock_read(map);
2559 } else if (maxlen != -1)
2560 maxlen -= kve->kve_structsize;
2562 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2564 vm_map_lock_read(map);
2567 if (last_timestamp != map->timestamp) {
2568 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2572 vm_map_unlock_read(map);
2580 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2584 int error, error2, *name;
2587 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2588 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2589 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2594 error = kern_proc_vmmap_out(p, &sb, -1, KERN_VMMAP_PACK_KINFO);
2595 error2 = sbuf_finish(&sb);
2597 return (error != 0 ? error : error2);
2600 #if defined(STACK) || defined(DDB)
2602 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2604 struct kinfo_kstack *kkstp;
2605 int error, i, *name, numthreads;
2606 lwpid_t *lwpidarray;
2613 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2617 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2618 st = stack_create(M_WAITOK);
2623 if (lwpidarray != NULL) {
2624 free(lwpidarray, M_TEMP);
2627 numthreads = p->p_numthreads;
2629 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2632 } while (numthreads < p->p_numthreads);
2635 * XXXRW: During the below loop, execve(2) and countless other sorts
2636 * of changes could have taken place. Should we check to see if the
2637 * vmspace has been replaced, or the like, in order to prevent
2638 * giving a snapshot that spans, say, execve(2), with some threads
2639 * before and some after? Among other things, the credentials could
2640 * have changed, in which case the right to extract debug info might
2641 * no longer be assured.
2644 FOREACH_THREAD_IN_PROC(p, td) {
2645 KASSERT(i < numthreads,
2646 ("sysctl_kern_proc_kstack: numthreads"));
2647 lwpidarray[i] = td->td_tid;
2651 for (i = 0; i < numthreads; i++) {
2652 td = thread_find(p, lwpidarray[i]);
2656 bzero(kkstp, sizeof(*kkstp));
2657 (void)sbuf_new(&sb, kkstp->kkst_trace,
2658 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2660 kkstp->kkst_tid = td->td_tid;
2661 if (TD_IS_SWAPPED(td)) {
2662 kkstp->kkst_state = KKST_STATE_SWAPPED;
2663 } else if (TD_IS_RUNNING(td)) {
2664 if (stack_save_td_running(st, td) == 0)
2665 kkstp->kkst_state = KKST_STATE_STACKOK;
2667 kkstp->kkst_state = KKST_STATE_RUNNING;
2669 kkstp->kkst_state = KKST_STATE_STACKOK;
2670 stack_save_td(st, td);
2674 stack_sbuf_print(&sb, st);
2677 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2684 if (lwpidarray != NULL)
2685 free(lwpidarray, M_TEMP);
2687 free(kkstp, M_TEMP);
2693 * This sysctl allows a process to retrieve the full list of groups from
2694 * itself or another process.
2697 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2699 pid_t *pidp = (pid_t *)arg1;
2700 unsigned int arglen = arg2;
2707 if (*pidp == -1) { /* -1 means this process */
2708 p = req->td->td_proc;
2711 error = pget(*pidp, PGET_CANSEE, &p);
2716 cred = crhold(p->p_ucred);
2719 error = SYSCTL_OUT(req, cred->cr_groups,
2720 cred->cr_ngroups * sizeof(gid_t));
2726 * This sysctl allows a process to retrieve or/and set the resource limit for
2730 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2732 int *name = (int *)arg1;
2733 u_int namelen = arg2;
2742 which = (u_int)name[1];
2743 if (which >= RLIM_NLIMITS)
2746 if (req->newptr != NULL && req->newlen != sizeof(rlim))
2749 flags = PGET_HOLD | PGET_NOTWEXIT;
2750 if (req->newptr != NULL)
2751 flags |= PGET_CANDEBUG;
2753 flags |= PGET_CANSEE;
2754 error = pget((pid_t)name[0], flags, &p);
2761 if (req->oldptr != NULL) {
2763 lim_rlimit_proc(p, which, &rlim);
2766 error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
2773 if (req->newptr != NULL) {
2774 error = SYSCTL_IN(req, &rlim, sizeof(rlim));
2776 error = kern_proc_setrlimit(curthread, p, which, &rlim);
2785 * This sysctl allows a process to retrieve ps_strings structure location of
2789 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2791 int *name = (int *)arg1;
2792 u_int namelen = arg2;
2794 vm_offset_t ps_strings;
2796 #ifdef COMPAT_FREEBSD32
2797 uint32_t ps_strings32;
2803 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2806 #ifdef COMPAT_FREEBSD32
2807 if ((req->flags & SCTL_MASK32) != 0) {
2809 * We return 0 if the 32 bit emulation request is for a 64 bit
2812 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
2813 PTROUT(p->p_sysent->sv_psstrings) : 0;
2815 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
2819 ps_strings = p->p_sysent->sv_psstrings;
2821 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
2826 * This sysctl allows a process to retrieve umask of another process.
2829 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
2831 int *name = (int *)arg1;
2832 u_int namelen = arg2;
2841 pid = (pid_t)name[0];
2843 if (pid == p->p_pid || pid == 0) {
2844 fd_cmask = p->p_fd->fd_cmask;
2848 error = pget(pid, PGET_WANTREAD, &p);
2852 fd_cmask = p->p_fd->fd_cmask;
2855 error = SYSCTL_OUT(req, &fd_cmask, sizeof(fd_cmask));
2860 * This sysctl allows a process to set and retrieve binary osreldate of
2864 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
2866 int *name = (int *)arg1;
2867 u_int namelen = arg2;
2869 int flags, error, osrel;
2874 if (req->newptr != NULL && req->newlen != sizeof(osrel))
2877 flags = PGET_HOLD | PGET_NOTWEXIT;
2878 if (req->newptr != NULL)
2879 flags |= PGET_CANDEBUG;
2881 flags |= PGET_CANSEE;
2882 error = pget((pid_t)name[0], flags, &p);
2886 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
2890 if (req->newptr != NULL) {
2891 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
2906 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
2908 int *name = (int *)arg1;
2909 u_int namelen = arg2;
2911 struct kinfo_sigtramp kst;
2912 const struct sysentvec *sv;
2914 #ifdef COMPAT_FREEBSD32
2915 struct kinfo_sigtramp32 kst32;
2921 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2925 #ifdef COMPAT_FREEBSD32
2926 if ((req->flags & SCTL_MASK32) != 0) {
2927 bzero(&kst32, sizeof(kst32));
2928 if (SV_PROC_FLAG(p, SV_ILP32)) {
2929 if (sv->sv_sigcode_base != 0) {
2930 kst32.ksigtramp_start = sv->sv_sigcode_base;
2931 kst32.ksigtramp_end = sv->sv_sigcode_base +
2934 kst32.ksigtramp_start = sv->sv_psstrings -
2936 kst32.ksigtramp_end = sv->sv_psstrings;
2940 error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
2944 bzero(&kst, sizeof(kst));
2945 if (sv->sv_sigcode_base != 0) {
2946 kst.ksigtramp_start = (char *)sv->sv_sigcode_base;
2947 kst.ksigtramp_end = (char *)sv->sv_sigcode_base +
2950 kst.ksigtramp_start = (char *)sv->sv_psstrings -
2952 kst.ksigtramp_end = (char *)sv->sv_psstrings;
2955 error = SYSCTL_OUT(req, &kst, sizeof(kst));
2959 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
2961 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
2962 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
2963 "Return entire process table");
2965 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2966 sysctl_kern_proc, "Process table");
2968 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
2969 sysctl_kern_proc, "Process table");
2971 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2972 sysctl_kern_proc, "Process table");
2974 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
2975 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2977 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
2978 sysctl_kern_proc, "Process table");
2980 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2981 sysctl_kern_proc, "Process table");
2983 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2984 sysctl_kern_proc, "Process table");
2986 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2987 sysctl_kern_proc, "Process table");
2989 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
2990 sysctl_kern_proc, "Return process table, no threads");
2992 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
2993 CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
2994 sysctl_kern_proc_args, "Process argument list");
2996 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
2997 sysctl_kern_proc_env, "Process environment");
2999 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
3000 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
3002 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
3003 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
3005 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
3006 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
3007 "Process syscall vector name (ABI type)");
3009 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
3010 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3012 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
3013 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3015 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
3016 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3018 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
3019 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3021 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
3022 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3024 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
3025 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3027 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
3028 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3030 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
3031 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3033 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
3034 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
3035 "Return process table, no threads");
3037 #ifdef COMPAT_FREEBSD7
3038 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
3039 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
3042 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
3043 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
3045 #if defined(STACK) || defined(DDB)
3046 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
3047 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
3050 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
3051 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
3053 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
3054 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
3055 "Process resource limits");
3057 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
3058 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
3059 "Process ps_strings location");
3061 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
3062 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
3064 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
3065 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
3066 "Process binary osreldate");
3068 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
3069 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
3070 "Process signal trampoline location");
3075 * stop_all_proc() purpose is to stop all process which have usermode,
3076 * except current process for obvious reasons. This makes it somewhat
3077 * unreliable when invoked from multithreaded process. The service
3078 * must not be user-callable anyway.
3083 struct proc *cp, *p;
3085 bool restart, seen_stopped, seen_exiting, stopped_some;
3089 sx_xlock(&allproc_lock);
3091 seen_exiting = seen_stopped = stopped_some = restart = false;
3092 LIST_REMOVE(cp, p_list);
3093 LIST_INSERT_HEAD(&allproc, cp, p_list);
3095 p = LIST_NEXT(cp, p_list);
3098 LIST_REMOVE(cp, p_list);
3099 LIST_INSERT_AFTER(p, cp, p_list);
3101 if ((p->p_flag & (P_KPROC | P_SYSTEM | P_TOTAL_STOP)) != 0) {
3105 if ((p->p_flag & P_WEXIT) != 0) {
3106 seen_exiting = true;
3110 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
3112 * Stopped processes are tolerated when there
3113 * are no other processes which might continue
3114 * them. P_STOPPED_SINGLE but not
3115 * P_TOTAL_STOP process still has at least one
3118 seen_stopped = true;
3123 sx_xunlock(&allproc_lock);
3124 r = thread_single(p, SINGLE_ALLPROC);
3128 stopped_some = true;
3131 sx_xlock(&allproc_lock);
3133 /* Catch forked children we did not see in iteration. */
3134 if (gen != allproc_gen)
3136 sx_xunlock(&allproc_lock);
3137 if (restart || stopped_some || seen_exiting || seen_stopped) {
3138 kern_yield(PRI_USER);
3144 resume_all_proc(void)
3146 struct proc *cp, *p;
3149 sx_xlock(&allproc_lock);
3151 LIST_REMOVE(cp, p_list);
3152 LIST_INSERT_HEAD(&allproc, cp, p_list);
3154 p = LIST_NEXT(cp, p_list);
3157 LIST_REMOVE(cp, p_list);
3158 LIST_INSERT_AFTER(p, cp, p_list);
3160 if ((p->p_flag & P_TOTAL_STOP) != 0) {
3161 sx_xunlock(&allproc_lock);
3163 thread_single_end(p, SINGLE_ALLPROC);
3166 sx_xlock(&allproc_lock);
3171 /* Did the loop above missed any stopped process ? */
3172 FOREACH_PROC_IN_SYSTEM(p) {
3173 /* No need for proc lock. */
3174 if ((p->p_flag & P_TOTAL_STOP) != 0)
3177 sx_xunlock(&allproc_lock);
3180 /* #define TOTAL_STOP_DEBUG 1 */
3181 #ifdef TOTAL_STOP_DEBUG
3182 volatile static int ap_resume;
3183 #include <sys/mount.h>
3186 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
3192 error = sysctl_handle_int(oidp, &val, 0, req);
3193 if (error != 0 || req->newptr == NULL)
3198 while (ap_resume == 0)
3206 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
3207 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
3208 sysctl_debug_stop_all_proc, "I",