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$");
37 #include "opt_compat.h"
39 #include "opt_ktrace.h"
40 #include "opt_kstack_pages.h"
41 #include "opt_stack.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
46 #include <sys/eventhandler.h>
49 #include <sys/kernel.h>
50 #include <sys/limits.h>
52 #include <sys/loginclass.h>
53 #include <sys/malloc.h>
55 #include <sys/mount.h>
56 #include <sys/mutex.h>
58 #include <sys/ptrace.h>
59 #include <sys/refcount.h>
60 #include <sys/resourcevar.h>
61 #include <sys/rwlock.h>
63 #include <sys/sysent.h>
64 #include <sys/sched.h>
66 #include <sys/stack.h>
68 #include <sys/sysctl.h>
69 #include <sys/filedesc.h>
71 #include <sys/signalvar.h>
75 #include <sys/vnode.h>
83 #include <vm/vm_param.h>
84 #include <vm/vm_extern.h>
86 #include <vm/vm_map.h>
87 #include <vm/vm_object.h>
88 #include <vm/vm_page.h>
91 #ifdef COMPAT_FREEBSD32
92 #include <compat/freebsd32/freebsd32.h>
93 #include <compat/freebsd32/freebsd32_util.h>
96 SDT_PROVIDER_DEFINE(proc);
97 SDT_PROBE_DEFINE4(proc, , ctor, entry, "struct proc *", "int", "void *",
99 SDT_PROBE_DEFINE4(proc, , ctor, return, "struct proc *", "int", "void *",
101 SDT_PROBE_DEFINE4(proc, , dtor, entry, "struct proc *", "int", "void *",
103 SDT_PROBE_DEFINE3(proc, , dtor, return, "struct proc *", "int", "void *");
104 SDT_PROBE_DEFINE3(proc, , init, entry, "struct proc *", "int", "int");
105 SDT_PROBE_DEFINE3(proc, , init, return, "struct proc *", "int", "int");
107 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
108 MALLOC_DEFINE(M_SESSION, "session", "session header");
109 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
110 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
112 static void doenterpgrp(struct proc *, struct pgrp *);
113 static void orphanpg(struct pgrp *pg);
114 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
115 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
116 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
118 static void pgadjustjobc(struct pgrp *pgrp, int entering);
119 static void pgdelete(struct pgrp *);
120 static int proc_ctor(void *mem, int size, void *arg, int flags);
121 static void proc_dtor(void *mem, int size, void *arg);
122 static int proc_init(void *mem, int size, int flags);
123 static void proc_fini(void *mem, int size);
124 static void pargs_free(struct pargs *pa);
125 static struct proc *zpfind_locked(pid_t pid);
128 * Other process lists
130 struct pidhashhead *pidhashtbl;
132 struct pgrphashhead *pgrphashtbl;
134 struct proclist allproc;
135 struct proclist zombproc;
136 struct sx __exclusive_cache_line allproc_lock;
137 struct sx __exclusive_cache_line proctree_lock;
138 struct mtx __exclusive_cache_line ppeers_lock;
139 uma_zone_t proc_zone;
142 * The offset of various fields in struct proc and struct thread.
143 * These are used by kernel debuggers to enumerate kernel threads and
146 const int proc_off_p_pid = offsetof(struct proc, p_pid);
147 const int proc_off_p_comm = offsetof(struct proc, p_comm);
148 const int proc_off_p_list = offsetof(struct proc, p_list);
149 const int proc_off_p_threads = offsetof(struct proc, p_threads);
150 const int thread_off_td_tid = offsetof(struct thread, td_tid);
151 const int thread_off_td_name = offsetof(struct thread, td_name);
152 const int thread_off_td_oncpu = offsetof(struct thread, td_oncpu);
153 const int thread_off_td_pcb = offsetof(struct thread, td_pcb);
154 const int thread_off_td_plist = offsetof(struct thread, td_plist);
156 EVENTHANDLER_LIST_DEFINE(process_ctor);
157 EVENTHANDLER_LIST_DEFINE(process_dtor);
158 EVENTHANDLER_LIST_DEFINE(process_init);
159 EVENTHANDLER_LIST_DEFINE(process_fini);
160 EVENTHANDLER_LIST_DEFINE(process_exit);
161 EVENTHANDLER_LIST_DEFINE(process_fork);
162 EVENTHANDLER_LIST_DEFINE(process_exec);
164 EVENTHANDLER_LIST_DECLARE(thread_ctor);
165 EVENTHANDLER_LIST_DECLARE(thread_dtor);
167 int kstack_pages = KSTACK_PAGES;
168 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
169 "Kernel stack size in pages");
170 static int vmmap_skip_res_cnt = 0;
171 SYSCTL_INT(_kern, OID_AUTO, proc_vmmap_skip_resident_count, CTLFLAG_RW,
172 &vmmap_skip_res_cnt, 0,
173 "Skip calculation of the pages resident count in kern.proc.vmmap");
175 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
176 #ifdef COMPAT_FREEBSD32
177 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
181 * Initialize global process hashing structures.
187 sx_init(&allproc_lock, "allproc");
188 sx_init(&proctree_lock, "proctree");
189 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
191 LIST_INIT(&zombproc);
192 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
193 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
194 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
195 proc_ctor, proc_dtor, proc_init, proc_fini,
196 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
201 * Prepare a proc for use.
204 proc_ctor(void *mem, int size, void *arg, int flags)
209 p = (struct proc *)mem;
210 SDT_PROBE4(proc, , ctor , entry, p, size, arg, flags);
211 EVENTHANDLER_DIRECT_INVOKE(process_ctor, p);
212 SDT_PROBE4(proc, , ctor , return, p, size, arg, flags);
213 td = FIRST_THREAD_IN_PROC(p);
215 /* Make sure all thread constructors are executed */
216 EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td);
222 * Reclaim a proc after use.
225 proc_dtor(void *mem, int size, void *arg)
230 /* INVARIANTS checks go here */
231 p = (struct proc *)mem;
232 td = FIRST_THREAD_IN_PROC(p);
233 SDT_PROBE4(proc, , dtor, entry, p, size, arg, td);
236 KASSERT((p->p_numthreads == 1),
237 ("bad number of threads in exiting process"));
238 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
240 /* Free all OSD associated to this thread. */
242 td_softdep_cleanup(td);
243 MPASS(td->td_su == NULL);
245 /* Make sure all thread destructors are executed */
246 EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td);
248 EVENTHANDLER_DIRECT_INVOKE(process_dtor, p);
249 if (p->p_ksi != NULL)
250 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
251 SDT_PROBE3(proc, , dtor, return, p, size, arg);
255 * Initialize type-stable parts of a proc (when newly created).
258 proc_init(void *mem, int size, int flags)
262 p = (struct proc *)mem;
263 SDT_PROBE3(proc, , init, entry, p, size, flags);
264 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK | MTX_NEW);
265 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_NEW);
266 mtx_init(&p->p_statmtx, "pstatl", NULL, MTX_SPIN | MTX_NEW);
267 mtx_init(&p->p_itimmtx, "pitiml", NULL, MTX_SPIN | MTX_NEW);
268 mtx_init(&p->p_profmtx, "pprofl", NULL, MTX_SPIN | MTX_NEW);
269 cv_init(&p->p_pwait, "ppwait");
270 cv_init(&p->p_dbgwait, "dbgwait");
271 TAILQ_INIT(&p->p_threads); /* all threads in proc */
272 EVENTHANDLER_DIRECT_INVOKE(process_init, p);
273 p->p_stats = pstats_alloc();
275 SDT_PROBE3(proc, , init, return, p, size, flags);
280 * UMA should ensure that this function is never called.
281 * Freeing a proc structure would violate type stability.
284 proc_fini(void *mem, int size)
289 p = (struct proc *)mem;
290 EVENTHANDLER_DIRECT_INVOKE(process_fini, p);
291 pstats_free(p->p_stats);
292 thread_free(FIRST_THREAD_IN_PROC(p));
293 mtx_destroy(&p->p_mtx);
294 if (p->p_ksi != NULL)
295 ksiginfo_free(p->p_ksi);
297 panic("proc reclaimed");
302 * Is p an inferior of the current process?
305 inferior(struct proc *p)
308 sx_assert(&proctree_lock, SX_LOCKED);
309 PROC_LOCK_ASSERT(p, MA_OWNED);
310 for (; p != curproc; p = proc_realparent(p)) {
318 pfind_locked(pid_t pid)
322 sx_assert(&allproc_lock, SX_LOCKED);
323 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
324 if (p->p_pid == pid) {
326 if (p->p_state == PRS_NEW) {
337 * Locate a process by number; return only "live" processes -- i.e., neither
338 * zombies nor newly born but incompletely initialized processes. By not
339 * returning processes in the PRS_NEW state, we allow callers to avoid
340 * testing for that condition to avoid dereferencing p_ucred, et al.
348 if (p->p_pid == pid) {
352 sx_slock(&allproc_lock);
353 p = pfind_locked(pid);
354 sx_sunlock(&allproc_lock);
359 * Same as pfind but allow zombies.
366 sx_slock(&allproc_lock);
367 p = pfind_locked(pid);
369 p = zpfind_locked(pid);
370 sx_sunlock(&allproc_lock);
376 pfind_tid_locked(pid_t tid)
381 sx_assert(&allproc_lock, SX_LOCKED);
382 FOREACH_PROC_IN_SYSTEM(p) {
384 if (p->p_state == PRS_NEW) {
388 FOREACH_THREAD_IN_PROC(p, td) {
389 if (td->td_tid == tid)
399 * Locate a process group by number.
400 * The caller must hold proctree_lock.
407 sx_assert(&proctree_lock, SX_LOCKED);
409 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
410 if (pgrp->pg_id == pgid) {
419 * Locate process and do additional manipulations, depending on flags.
422 pget(pid_t pid, int flags, struct proc **pp)
428 if (p->p_pid == pid) {
431 sx_slock(&allproc_lock);
432 if (pid <= PID_MAX) {
433 p = pfind_locked(pid);
434 if (p == NULL && (flags & PGET_NOTWEXIT) == 0)
435 p = zpfind_locked(pid);
436 } else if ((flags & PGET_NOTID) == 0) {
437 p = pfind_tid_locked(pid);
441 sx_sunlock(&allproc_lock);
444 if ((flags & PGET_CANSEE) != 0) {
445 error = p_cansee(curthread, p);
450 if ((flags & PGET_CANDEBUG) != 0) {
451 error = p_candebug(curthread, p);
455 if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
459 if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
463 if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
465 * XXXRW: Not clear ESRCH is the right error during proc
471 if ((flags & PGET_HOLD) != 0) {
483 * Create a new process group.
484 * pgid must be equal to the pid of p.
485 * Begin a new session if required.
488 enterpgrp(struct proc *p, pid_t pgid, struct pgrp *pgrp, struct session *sess)
491 sx_assert(&proctree_lock, SX_XLOCKED);
493 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
494 KASSERT(p->p_pid == pgid,
495 ("enterpgrp: new pgrp and pid != pgid"));
496 KASSERT(pgfind(pgid) == NULL,
497 ("enterpgrp: pgrp with pgid exists"));
498 KASSERT(!SESS_LEADER(p),
499 ("enterpgrp: session leader attempted setpgrp"));
501 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
507 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
509 p->p_flag &= ~P_CONTROLT;
513 sess->s_sid = p->p_pid;
514 refcount_init(&sess->s_count, 1);
515 sess->s_ttyvp = NULL;
516 sess->s_ttydp = NULL;
518 bcopy(p->p_session->s_login, sess->s_login,
519 sizeof(sess->s_login));
520 pgrp->pg_session = sess;
521 KASSERT(p == curproc,
522 ("enterpgrp: mksession and p != curproc"));
524 pgrp->pg_session = p->p_session;
525 sess_hold(pgrp->pg_session);
529 LIST_INIT(&pgrp->pg_members);
532 * As we have an exclusive lock of proctree_lock,
533 * this should not deadlock.
535 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
537 SLIST_INIT(&pgrp->pg_sigiolst);
540 doenterpgrp(p, pgrp);
546 * Move p to an existing process group
549 enterthispgrp(struct proc *p, struct pgrp *pgrp)
552 sx_assert(&proctree_lock, SX_XLOCKED);
553 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
554 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
555 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
556 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
557 KASSERT(pgrp->pg_session == p->p_session,
558 ("%s: pgrp's session %p, p->p_session %p.\n",
562 KASSERT(pgrp != p->p_pgrp,
563 ("%s: p belongs to pgrp.", __func__));
565 doenterpgrp(p, pgrp);
571 * Move p to a process group
574 doenterpgrp(struct proc *p, struct pgrp *pgrp)
576 struct pgrp *savepgrp;
578 sx_assert(&proctree_lock, SX_XLOCKED);
579 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
580 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
581 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
582 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
584 savepgrp = p->p_pgrp;
587 * Adjust eligibility of affected pgrps to participate in job control.
588 * Increment eligibility counts before decrementing, otherwise we
589 * could reach 0 spuriously during the first call.
592 fixjobc(p, p->p_pgrp, 0);
597 LIST_REMOVE(p, p_pglist);
600 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
601 PGRP_UNLOCK(savepgrp);
603 if (LIST_EMPTY(&savepgrp->pg_members))
608 * remove process from process group
611 leavepgrp(struct proc *p)
613 struct pgrp *savepgrp;
615 sx_assert(&proctree_lock, SX_XLOCKED);
616 savepgrp = p->p_pgrp;
619 LIST_REMOVE(p, p_pglist);
622 PGRP_UNLOCK(savepgrp);
623 if (LIST_EMPTY(&savepgrp->pg_members))
629 * delete a process group
632 pgdelete(struct pgrp *pgrp)
634 struct session *savesess;
637 sx_assert(&proctree_lock, SX_XLOCKED);
638 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
639 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
642 * Reset any sigio structures pointing to us as a result of
643 * F_SETOWN with our pgid.
645 funsetownlst(&pgrp->pg_sigiolst);
648 tp = pgrp->pg_session->s_ttyp;
649 LIST_REMOVE(pgrp, pg_hash);
650 savesess = pgrp->pg_session;
653 /* Remove the reference to the pgrp before deallocating it. */
656 tty_rel_pgrp(tp, pgrp);
659 mtx_destroy(&pgrp->pg_mtx);
661 sess_release(savesess);
665 pgadjustjobc(struct pgrp *pgrp, int entering)
673 if (pgrp->pg_jobc == 0)
680 * Adjust pgrp jobc counters when specified process changes process group.
681 * We count the number of processes in each process group that "qualify"
682 * the group for terminal job control (those with a parent in a different
683 * process group of the same session). If that count reaches zero, the
684 * process group becomes orphaned. Check both the specified process'
685 * process group and that of its children.
686 * entering == 0 => p is leaving specified group.
687 * entering == 1 => p is entering specified group.
690 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
692 struct pgrp *hispgrp;
693 struct session *mysession;
696 sx_assert(&proctree_lock, SX_LOCKED);
697 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
698 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
699 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
702 * Check p's parent to see whether p qualifies its own process
703 * group; if so, adjust count for p's process group.
705 mysession = pgrp->pg_session;
706 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
707 hispgrp->pg_session == mysession)
708 pgadjustjobc(pgrp, entering);
711 * Check this process' children to see whether they qualify
712 * their process groups; if so, adjust counts for children's
715 LIST_FOREACH(q, &p->p_children, p_sibling) {
717 if (hispgrp == pgrp ||
718 hispgrp->pg_session != mysession)
720 if (q->p_state == PRS_ZOMBIE)
722 pgadjustjobc(hispgrp, entering);
735 MPASS(p->p_flag & P_WEXIT);
737 * Do a quick check to see if there is anything to do with the
738 * proctree_lock held. pgrp and LIST_EMPTY checks are for fixjobc().
741 if (!SESS_LEADER(p) &&
742 (p->p_pgrp == p->p_pptr->p_pgrp) &&
743 LIST_EMPTY(&p->p_children)) {
749 sx_xlock(&proctree_lock);
750 if (SESS_LEADER(p)) {
754 * s_ttyp is not zero'd; we use this to indicate that
755 * the session once had a controlling terminal. (for
756 * logging and informational purposes)
767 * Signal foreground pgrp and revoke access to
768 * controlling terminal if it has not been revoked
771 * Because the TTY may have been revoked in the mean
772 * time and could already have a new session associated
773 * with it, make sure we don't send a SIGHUP to a
774 * foreground process group that does not belong to this
780 if (tp->t_session == sp)
781 tty_signal_pgrp(tp, SIGHUP);
786 sx_xunlock(&proctree_lock);
787 if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) {
788 VOP_REVOKE(ttyvp, REVOKEALL);
789 VOP_UNLOCK(ttyvp, 0);
792 sx_xlock(&proctree_lock);
795 fixjobc(p, p->p_pgrp, 0);
796 sx_xunlock(&proctree_lock);
800 * A process group has become orphaned;
801 * if there are any stopped processes in the group,
802 * hang-up all process in that group.
805 orphanpg(struct pgrp *pg)
809 PGRP_LOCK_ASSERT(pg, MA_OWNED);
811 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
813 if (P_SHOULDSTOP(p) == P_STOPPED_SIG) {
815 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
817 kern_psignal(p, SIGHUP);
818 kern_psignal(p, SIGCONT);
828 sess_hold(struct session *s)
831 refcount_acquire(&s->s_count);
835 sess_release(struct session *s)
838 if (refcount_release(&s->s_count)) {
839 if (s->s_ttyp != NULL) {
841 tty_rel_sess(s->s_ttyp, s);
843 mtx_destroy(&s->s_mtx);
850 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
856 for (i = 0; i <= pgrphash; i++) {
857 if (!LIST_EMPTY(&pgrphashtbl[i])) {
858 printf("\tindx %d\n", i);
859 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
861 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
862 (void *)pgrp, (long)pgrp->pg_id,
863 (void *)pgrp->pg_session,
864 pgrp->pg_session->s_count,
865 (void *)LIST_FIRST(&pgrp->pg_members));
866 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
867 printf("\t\tpid %ld addr %p pgrp %p\n",
868 (long)p->p_pid, (void *)p,
878 * Calculate the kinfo_proc members which contain process-wide
880 * Must be called with the target process locked.
883 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
887 PROC_LOCK_ASSERT(p, MA_OWNED);
891 FOREACH_THREAD_IN_PROC(p, td) {
893 kp->ki_pctcpu += sched_pctcpu(td);
894 kp->ki_estcpu += sched_estcpu(td);
900 * Clear kinfo_proc and fill in any information that is common
901 * to all threads in the process.
902 * Must be called with the target process locked.
905 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
912 struct timeval boottime;
914 /* For proc_realparent. */
915 sx_assert(&proctree_lock, SX_LOCKED);
916 PROC_LOCK_ASSERT(p, MA_OWNED);
917 bzero(kp, sizeof(*kp));
919 kp->ki_structsize = sizeof(*kp);
921 kp->ki_addr =/* p->p_addr; */0; /* XXX */
922 kp->ki_args = p->p_args;
923 kp->ki_textvp = p->p_textvp;
925 kp->ki_tracep = p->p_tracevp;
926 kp->ki_traceflag = p->p_traceflag;
929 kp->ki_vmspace = p->p_vmspace;
930 kp->ki_flag = p->p_flag;
931 kp->ki_flag2 = p->p_flag2;
934 kp->ki_uid = cred->cr_uid;
935 kp->ki_ruid = cred->cr_ruid;
936 kp->ki_svuid = cred->cr_svuid;
938 if (cred->cr_flags & CRED_FLAG_CAPMODE)
939 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
940 /* XXX bde doesn't like KI_NGROUPS */
941 if (cred->cr_ngroups > KI_NGROUPS) {
942 kp->ki_ngroups = KI_NGROUPS;
943 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
945 kp->ki_ngroups = cred->cr_ngroups;
946 bcopy(cred->cr_groups, kp->ki_groups,
947 kp->ki_ngroups * sizeof(gid_t));
948 kp->ki_rgid = cred->cr_rgid;
949 kp->ki_svgid = cred->cr_svgid;
950 /* If jailed(cred), emulate the old P_JAILED flag. */
952 kp->ki_flag |= P_JAILED;
953 /* If inside the jail, use 0 as a jail ID. */
954 if (cred->cr_prison != curthread->td_ucred->cr_prison)
955 kp->ki_jid = cred->cr_prison->pr_id;
957 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
958 sizeof(kp->ki_loginclass));
962 mtx_lock(&ps->ps_mtx);
963 kp->ki_sigignore = ps->ps_sigignore;
964 kp->ki_sigcatch = ps->ps_sigcatch;
965 mtx_unlock(&ps->ps_mtx);
967 if (p->p_state != PRS_NEW &&
968 p->p_state != PRS_ZOMBIE &&
969 p->p_vmspace != NULL) {
970 struct vmspace *vm = p->p_vmspace;
972 kp->ki_size = vm->vm_map.size;
973 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
974 FOREACH_THREAD_IN_PROC(p, td0) {
975 if (!TD_IS_SWAPPED(td0))
976 kp->ki_rssize += td0->td_kstack_pages;
978 kp->ki_swrss = vm->vm_swrss;
979 kp->ki_tsize = vm->vm_tsize;
980 kp->ki_dsize = vm->vm_dsize;
981 kp->ki_ssize = vm->vm_ssize;
982 } else if (p->p_state == PRS_ZOMBIE)
984 if (kp->ki_flag & P_INMEM)
985 kp->ki_sflag = PS_INMEM;
988 /* Calculate legacy swtime as seconds since 'swtick'. */
989 kp->ki_swtime = (ticks - p->p_swtick) / hz;
990 kp->ki_pid = p->p_pid;
991 kp->ki_nice = p->p_nice;
992 kp->ki_fibnum = p->p_fibnum;
993 kp->ki_start = p->p_stats->p_start;
994 getboottime(&boottime);
995 timevaladd(&kp->ki_start, &boottime);
997 rufetch(p, &kp->ki_rusage);
998 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
999 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
1001 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
1002 /* Some callers want child times in a single value. */
1003 kp->ki_childtime = kp->ki_childstime;
1004 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
1006 FOREACH_THREAD_IN_PROC(p, td0)
1007 kp->ki_cow += td0->td_cow;
1011 kp->ki_pgid = p->p_pgrp->pg_id;
1012 kp->ki_jobc = p->p_pgrp->pg_jobc;
1013 sp = p->p_pgrp->pg_session;
1016 kp->ki_sid = sp->s_sid;
1018 strlcpy(kp->ki_login, sp->s_login,
1019 sizeof(kp->ki_login));
1021 kp->ki_kiflag |= KI_CTTY;
1023 kp->ki_kiflag |= KI_SLEADER;
1024 /* XXX proctree_lock */
1029 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
1030 kp->ki_tdev = tty_udev(tp);
1031 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1032 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1034 kp->ki_tsid = tp->t_session->s_sid;
1036 kp->ki_tdev = NODEV;
1037 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1039 if (p->p_comm[0] != '\0')
1040 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
1041 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
1042 p->p_sysent->sv_name[0] != '\0')
1043 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
1044 kp->ki_siglist = p->p_siglist;
1045 kp->ki_xstat = KW_EXITCODE(p->p_xexit, p->p_xsig);
1046 kp->ki_acflag = p->p_acflag;
1047 kp->ki_lock = p->p_lock;
1049 kp->ki_ppid = proc_realparent(p)->p_pid;
1050 if (p->p_flag & P_TRACED)
1051 kp->ki_tracer = p->p_pptr->p_pid;
1056 * Fill in information that is thread specific. Must be called with
1057 * target process locked. If 'preferthread' is set, overwrite certain
1058 * process-related fields that are maintained for both threads and
1062 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
1068 PROC_LOCK_ASSERT(p, MA_OWNED);
1073 if (td->td_wmesg != NULL)
1074 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
1076 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
1077 if (strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)) >=
1078 sizeof(kp->ki_tdname)) {
1079 strlcpy(kp->ki_moretdname,
1080 td->td_name + sizeof(kp->ki_tdname) - 1,
1081 sizeof(kp->ki_moretdname));
1083 bzero(kp->ki_moretdname, sizeof(kp->ki_moretdname));
1085 if (TD_ON_LOCK(td)) {
1086 kp->ki_kiflag |= KI_LOCKBLOCK;
1087 strlcpy(kp->ki_lockname, td->td_lockname,
1088 sizeof(kp->ki_lockname));
1090 kp->ki_kiflag &= ~KI_LOCKBLOCK;
1091 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
1094 if (p->p_state == PRS_NORMAL) { /* approximate. */
1095 if (TD_ON_RUNQ(td) ||
1097 TD_IS_RUNNING(td)) {
1099 } else if (P_SHOULDSTOP(p)) {
1100 kp->ki_stat = SSTOP;
1101 } else if (TD_IS_SLEEPING(td)) {
1102 kp->ki_stat = SSLEEP;
1103 } else if (TD_ON_LOCK(td)) {
1104 kp->ki_stat = SLOCK;
1106 kp->ki_stat = SWAIT;
1108 } else if (p->p_state == PRS_ZOMBIE) {
1109 kp->ki_stat = SZOMB;
1114 /* Things in the thread */
1115 kp->ki_wchan = td->td_wchan;
1116 kp->ki_pri.pri_level = td->td_priority;
1117 kp->ki_pri.pri_native = td->td_base_pri;
1120 * Note: legacy fields; clamp at the old NOCPU value and/or
1121 * the maximum u_char CPU value.
1123 if (td->td_lastcpu == NOCPU)
1124 kp->ki_lastcpu_old = NOCPU_OLD;
1125 else if (td->td_lastcpu > MAXCPU_OLD)
1126 kp->ki_lastcpu_old = MAXCPU_OLD;
1128 kp->ki_lastcpu_old = td->td_lastcpu;
1130 if (td->td_oncpu == NOCPU)
1131 kp->ki_oncpu_old = NOCPU_OLD;
1132 else if (td->td_oncpu > MAXCPU_OLD)
1133 kp->ki_oncpu_old = MAXCPU_OLD;
1135 kp->ki_oncpu_old = td->td_oncpu;
1137 kp->ki_lastcpu = td->td_lastcpu;
1138 kp->ki_oncpu = td->td_oncpu;
1139 kp->ki_tdflags = td->td_flags;
1140 kp->ki_tid = td->td_tid;
1141 kp->ki_numthreads = p->p_numthreads;
1142 kp->ki_pcb = td->td_pcb;
1143 kp->ki_kstack = (void *)td->td_kstack;
1144 kp->ki_slptime = (ticks - td->td_slptick) / hz;
1145 kp->ki_pri.pri_class = td->td_pri_class;
1146 kp->ki_pri.pri_user = td->td_user_pri;
1149 rufetchtd(td, &kp->ki_rusage);
1150 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
1151 kp->ki_pctcpu = sched_pctcpu(td);
1152 kp->ki_estcpu = sched_estcpu(td);
1153 kp->ki_cow = td->td_cow;
1156 /* We can't get this anymore but ps etc never used it anyway. */
1160 kp->ki_siglist = td->td_siglist;
1161 kp->ki_sigmask = td->td_sigmask;
1168 * Fill in a kinfo_proc structure for the specified process.
1169 * Must be called with the target process locked.
1172 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1175 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1177 fill_kinfo_proc_only(p, kp);
1178 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1179 fill_kinfo_aggregate(p, kp);
1186 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1190 * Copy parts of p_stats; zero the rest of p_stats (statistics).
1193 pstats_fork(struct pstats *src, struct pstats *dst)
1196 bzero(&dst->pstat_startzero,
1197 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1198 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1199 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1203 pstats_free(struct pstats *ps)
1206 free(ps, M_SUBPROC);
1209 static struct proc *
1210 zpfind_locked(pid_t pid)
1214 sx_assert(&allproc_lock, SX_LOCKED);
1215 LIST_FOREACH(p, &zombproc, p_list) {
1216 if (p->p_pid == pid) {
1225 * Locate a zombie process by number
1232 sx_slock(&allproc_lock);
1233 p = zpfind_locked(pid);
1234 sx_sunlock(&allproc_lock);
1238 #ifdef COMPAT_FREEBSD32
1241 * This function is typically used to copy out the kernel address, so
1242 * it can be replaced by assignment of zero.
1244 static inline uint32_t
1245 ptr32_trim(void *ptr)
1249 uptr = (uintptr_t)ptr;
1250 return ((uptr > UINT_MAX) ? 0 : uptr);
1253 #define PTRTRIM_CP(src,dst,fld) \
1254 do { (dst).fld = ptr32_trim((src).fld); } while (0)
1257 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1261 bzero(ki32, sizeof(struct kinfo_proc32));
1262 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1263 CP(*ki, *ki32, ki_layout);
1264 PTRTRIM_CP(*ki, *ki32, ki_args);
1265 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1266 PTRTRIM_CP(*ki, *ki32, ki_addr);
1267 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1268 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1269 PTRTRIM_CP(*ki, *ki32, ki_fd);
1270 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1271 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1272 CP(*ki, *ki32, ki_pid);
1273 CP(*ki, *ki32, ki_ppid);
1274 CP(*ki, *ki32, ki_pgid);
1275 CP(*ki, *ki32, ki_tpgid);
1276 CP(*ki, *ki32, ki_sid);
1277 CP(*ki, *ki32, ki_tsid);
1278 CP(*ki, *ki32, ki_jobc);
1279 CP(*ki, *ki32, ki_tdev);
1280 CP(*ki, *ki32, ki_tdev_freebsd11);
1281 CP(*ki, *ki32, ki_siglist);
1282 CP(*ki, *ki32, ki_sigmask);
1283 CP(*ki, *ki32, ki_sigignore);
1284 CP(*ki, *ki32, ki_sigcatch);
1285 CP(*ki, *ki32, ki_uid);
1286 CP(*ki, *ki32, ki_ruid);
1287 CP(*ki, *ki32, ki_svuid);
1288 CP(*ki, *ki32, ki_rgid);
1289 CP(*ki, *ki32, ki_svgid);
1290 CP(*ki, *ki32, ki_ngroups);
1291 for (i = 0; i < KI_NGROUPS; i++)
1292 CP(*ki, *ki32, ki_groups[i]);
1293 CP(*ki, *ki32, ki_size);
1294 CP(*ki, *ki32, ki_rssize);
1295 CP(*ki, *ki32, ki_swrss);
1296 CP(*ki, *ki32, ki_tsize);
1297 CP(*ki, *ki32, ki_dsize);
1298 CP(*ki, *ki32, ki_ssize);
1299 CP(*ki, *ki32, ki_xstat);
1300 CP(*ki, *ki32, ki_acflag);
1301 CP(*ki, *ki32, ki_pctcpu);
1302 CP(*ki, *ki32, ki_estcpu);
1303 CP(*ki, *ki32, ki_slptime);
1304 CP(*ki, *ki32, ki_swtime);
1305 CP(*ki, *ki32, ki_cow);
1306 CP(*ki, *ki32, ki_runtime);
1307 TV_CP(*ki, *ki32, ki_start);
1308 TV_CP(*ki, *ki32, ki_childtime);
1309 CP(*ki, *ki32, ki_flag);
1310 CP(*ki, *ki32, ki_kiflag);
1311 CP(*ki, *ki32, ki_traceflag);
1312 CP(*ki, *ki32, ki_stat);
1313 CP(*ki, *ki32, ki_nice);
1314 CP(*ki, *ki32, ki_lock);
1315 CP(*ki, *ki32, ki_rqindex);
1316 CP(*ki, *ki32, ki_oncpu);
1317 CP(*ki, *ki32, ki_lastcpu);
1319 /* XXX TODO: wrap cpu value as appropriate */
1320 CP(*ki, *ki32, ki_oncpu_old);
1321 CP(*ki, *ki32, ki_lastcpu_old);
1323 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1324 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1325 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1326 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1327 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1328 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1329 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1330 bcopy(ki->ki_moretdname, ki32->ki_moretdname, MAXCOMLEN - TDNAMLEN + 1);
1331 CP(*ki, *ki32, ki_tracer);
1332 CP(*ki, *ki32, ki_flag2);
1333 CP(*ki, *ki32, ki_fibnum);
1334 CP(*ki, *ki32, ki_cr_flags);
1335 CP(*ki, *ki32, ki_jid);
1336 CP(*ki, *ki32, ki_numthreads);
1337 CP(*ki, *ki32, ki_tid);
1338 CP(*ki, *ki32, ki_pri);
1339 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1340 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1341 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1342 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1343 PTRTRIM_CP(*ki, *ki32, ki_udata);
1344 PTRTRIM_CP(*ki, *ki32, ki_tdaddr);
1345 CP(*ki, *ki32, ki_sflag);
1346 CP(*ki, *ki32, ki_tdflags);
1351 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
1354 struct kinfo_proc ki;
1355 #ifdef COMPAT_FREEBSD32
1356 struct kinfo_proc32 ki32;
1360 PROC_LOCK_ASSERT(p, MA_OWNED);
1361 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1364 fill_kinfo_proc(p, &ki);
1365 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1366 #ifdef COMPAT_FREEBSD32
1367 if ((flags & KERN_PROC_MASK32) != 0) {
1368 freebsd32_kinfo_proc_out(&ki, &ki32);
1369 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1373 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1376 FOREACH_THREAD_IN_PROC(p, td) {
1377 fill_kinfo_thread(td, &ki, 1);
1378 #ifdef COMPAT_FREEBSD32
1379 if ((flags & KERN_PROC_MASK32) != 0) {
1380 freebsd32_kinfo_proc_out(&ki, &ki32);
1381 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1385 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1396 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1399 struct kinfo_proc ki;
1402 sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
1403 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1404 error = kern_proc_out(p, &sb, flags);
1405 error2 = sbuf_finish(&sb);
1409 else if (error2 != 0)
1415 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1417 int *name = (int *)arg1;
1418 u_int namelen = arg2;
1420 int flags, doingzomb, oid_number;
1423 oid_number = oidp->oid_number;
1424 if (oid_number != KERN_PROC_ALL &&
1425 (oid_number & KERN_PROC_INC_THREAD) == 0)
1426 flags = KERN_PROC_NOTHREADS;
1429 oid_number &= ~KERN_PROC_INC_THREAD;
1431 #ifdef COMPAT_FREEBSD32
1432 if (req->flags & SCTL_MASK32)
1433 flags |= KERN_PROC_MASK32;
1435 if (oid_number == KERN_PROC_PID) {
1438 error = sysctl_wire_old_buffer(req, 0);
1441 sx_slock(&proctree_lock);
1442 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1444 error = sysctl_out_proc(p, req, flags);
1445 sx_sunlock(&proctree_lock);
1449 switch (oid_number) {
1454 case KERN_PROC_PROC:
1455 if (namelen != 0 && namelen != 1)
1465 /* overestimate by 5 procs */
1466 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1470 error = sysctl_wire_old_buffer(req, 0);
1473 sx_slock(&proctree_lock);
1474 sx_slock(&allproc_lock);
1475 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
1477 p = LIST_FIRST(&allproc);
1479 p = LIST_FIRST(&zombproc);
1480 for (; p != NULL; p = LIST_NEXT(p, p_list)) {
1482 * Skip embryonic processes.
1484 if (p->p_state == PRS_NEW)
1487 KASSERT(p->p_ucred != NULL,
1488 ("process credential is NULL for non-NEW proc"));
1490 * Show a user only appropriate processes.
1492 if (p_cansee(curthread, p)) {
1497 * TODO - make more efficient (see notes below).
1500 switch (oid_number) {
1503 if (p->p_ucred->cr_gid != (gid_t)name[0]) {
1509 case KERN_PROC_PGRP:
1510 /* could do this by traversing pgrp */
1511 if (p->p_pgrp == NULL ||
1512 p->p_pgrp->pg_id != (pid_t)name[0]) {
1518 case KERN_PROC_RGID:
1519 if (p->p_ucred->cr_rgid != (gid_t)name[0]) {
1525 case KERN_PROC_SESSION:
1526 if (p->p_session == NULL ||
1527 p->p_session->s_sid != (pid_t)name[0]) {
1534 if ((p->p_flag & P_CONTROLT) == 0 ||
1535 p->p_session == NULL) {
1539 /* XXX proctree_lock */
1540 SESS_LOCK(p->p_session);
1541 if (p->p_session->s_ttyp == NULL ||
1542 tty_udev(p->p_session->s_ttyp) !=
1544 SESS_UNLOCK(p->p_session);
1548 SESS_UNLOCK(p->p_session);
1552 if (p->p_ucred->cr_uid != (uid_t)name[0]) {
1558 case KERN_PROC_RUID:
1559 if (p->p_ucred->cr_ruid != (uid_t)name[0]) {
1565 case KERN_PROC_PROC:
1573 error = sysctl_out_proc(p, req, flags);
1575 sx_sunlock(&allproc_lock);
1576 sx_sunlock(&proctree_lock);
1581 sx_sunlock(&allproc_lock);
1582 sx_sunlock(&proctree_lock);
1587 pargs_alloc(int len)
1591 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1593 refcount_init(&pa->ar_ref, 1);
1594 pa->ar_length = len;
1599 pargs_free(struct pargs *pa)
1606 pargs_hold(struct pargs *pa)
1611 refcount_acquire(&pa->ar_ref);
1615 pargs_drop(struct pargs *pa)
1620 if (refcount_release(&pa->ar_ref))
1625 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1631 * This may return a short read if the string is shorter than the chunk
1632 * and is aligned at the end of the page, and the following page is not
1635 n = proc_readmem(td, p, (vm_offset_t)sptr, buf, len);
1641 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1643 enum proc_vector_type {
1649 #ifdef COMPAT_FREEBSD32
1651 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1652 size_t *vsizep, enum proc_vector_type type)
1654 struct freebsd32_ps_strings pss;
1656 vm_offset_t vptr, ptr;
1657 uint32_t *proc_vector32;
1663 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1664 sizeof(pss)) != sizeof(pss))
1668 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1669 vsize = pss.ps_nargvstr;
1670 if (vsize > ARG_MAX)
1672 size = vsize * sizeof(int32_t);
1675 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1676 vsize = pss.ps_nenvstr;
1677 if (vsize > ARG_MAX)
1679 size = vsize * sizeof(int32_t);
1682 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1683 (pss.ps_nenvstr + 1) * sizeof(int32_t);
1686 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1687 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1690 if (aux.a_type == AT_NULL)
1694 if (aux.a_type != AT_NULL)
1697 size = vsize * sizeof(aux);
1700 KASSERT(0, ("Wrong proc vector type: %d", type));
1703 proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1704 if (proc_readmem(td, p, vptr, proc_vector32, size) != size) {
1708 if (type == PROC_AUX) {
1709 *proc_vectorp = (char **)proc_vector32;
1713 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1714 for (i = 0; i < (int)vsize; i++)
1715 proc_vector[i] = PTRIN(proc_vector32[i]);
1716 *proc_vectorp = proc_vector;
1719 free(proc_vector32, M_TEMP);
1725 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1726 size_t *vsizep, enum proc_vector_type type)
1728 struct ps_strings pss;
1730 vm_offset_t vptr, ptr;
1735 #ifdef COMPAT_FREEBSD32
1736 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1737 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1739 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1740 sizeof(pss)) != sizeof(pss))
1744 vptr = (vm_offset_t)pss.ps_argvstr;
1745 vsize = pss.ps_nargvstr;
1746 if (vsize > ARG_MAX)
1748 size = vsize * sizeof(char *);
1751 vptr = (vm_offset_t)pss.ps_envstr;
1752 vsize = pss.ps_nenvstr;
1753 if (vsize > ARG_MAX)
1755 size = vsize * sizeof(char *);
1759 * The aux array is just above env array on the stack. Check
1760 * that the address is naturally aligned.
1762 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1764 #if __ELF_WORD_SIZE == 64
1765 if (vptr % sizeof(uint64_t) != 0)
1767 if (vptr % sizeof(uint32_t) != 0)
1771 * We count the array size reading the aux vectors from the
1772 * stack until AT_NULL vector is returned. So (to keep the code
1773 * simple) we read the process stack twice: the first time here
1774 * to find the size and the second time when copying the vectors
1775 * to the allocated proc_vector.
1777 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1778 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1781 if (aux.a_type == AT_NULL)
1786 * If the PROC_AUXV_MAX entries are iterated over, and we have
1787 * not reached AT_NULL, it is most likely we are reading wrong
1788 * data: either the process doesn't have auxv array or data has
1789 * been modified. Return the error in this case.
1791 if (aux.a_type != AT_NULL)
1794 size = vsize * sizeof(aux);
1797 KASSERT(0, ("Wrong proc vector type: %d", type));
1798 return (EINVAL); /* In case we are built without INVARIANTS. */
1800 proc_vector = malloc(size, M_TEMP, M_WAITOK);
1801 if (proc_readmem(td, p, vptr, proc_vector, size) != size) {
1802 free(proc_vector, M_TEMP);
1805 *proc_vectorp = proc_vector;
1811 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
1814 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
1815 enum proc_vector_type type)
1817 size_t done, len, nchr, vsize;
1819 char **proc_vector, *sptr;
1820 char pss_string[GET_PS_STRINGS_CHUNK_SZ];
1822 PROC_ASSERT_HELD(p);
1825 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
1827 nchr = 2 * (PATH_MAX + ARG_MAX);
1829 error = get_proc_vector(td, p, &proc_vector, &vsize, type);
1832 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
1834 * The program may have scribbled into its argv array, e.g. to
1835 * remove some arguments. If that has happened, break out
1836 * before trying to read from NULL.
1838 if (proc_vector[i] == NULL)
1840 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
1841 error = proc_read_string(td, p, sptr, pss_string,
1842 sizeof(pss_string));
1845 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
1846 if (done + len >= nchr)
1847 len = nchr - done - 1;
1848 sbuf_bcat(sb, pss_string, len);
1849 if (len != GET_PS_STRINGS_CHUNK_SZ)
1851 done += GET_PS_STRINGS_CHUNK_SZ;
1853 sbuf_bcat(sb, "", 1);
1857 free(proc_vector, M_TEMP);
1862 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
1865 return (get_ps_strings(curthread, p, sb, PROC_ARG));
1869 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
1872 return (get_ps_strings(curthread, p, sb, PROC_ENV));
1876 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
1882 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
1884 #ifdef COMPAT_FREEBSD32
1885 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1886 size = vsize * sizeof(Elf32_Auxinfo);
1889 size = vsize * sizeof(Elf_Auxinfo);
1890 if (sbuf_bcat(sb, auxv, size) != 0)
1898 * This sysctl allows a process to retrieve the argument list or process
1899 * title for another process without groping around in the address space
1900 * of the other process. It also allow a process to set its own "process
1901 * title to a string of its own choice.
1904 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1906 int *name = (int *)arg1;
1907 u_int namelen = arg2;
1908 struct pargs *newpa, *pa;
1911 int flags, error = 0, error2;
1917 pid = (pid_t)name[0];
1919 * If the query is for this process and it is single-threaded, there
1920 * is nobody to modify pargs, thus we can just read.
1923 if (pid == p->p_pid && p->p_numthreads == 1 && req->newptr == NULL &&
1924 (pa = p->p_args) != NULL)
1925 return (SYSCTL_OUT(req, pa->ar_args, pa->ar_length));
1927 flags = PGET_CANSEE;
1928 if (req->newptr != NULL)
1929 flags |= PGET_ISCURRENT;
1930 error = pget(pid, flags, &p);
1938 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1940 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
1943 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1944 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1945 error = proc_getargv(curthread, p, &sb);
1946 error2 = sbuf_finish(&sb);
1949 if (error == 0 && error2 != 0)
1954 if (error != 0 || req->newptr == NULL)
1957 if (req->newlen > ps_arg_cache_limit - sizeof(struct pargs))
1959 newpa = pargs_alloc(req->newlen);
1960 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
1974 * This sysctl allows a process to retrieve environment of another process.
1977 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
1979 int *name = (int *)arg1;
1980 u_int namelen = arg2;
1988 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1991 if ((p->p_flag & P_SYSTEM) != 0) {
1996 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1997 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1998 error = proc_getenvv(curthread, p, &sb);
1999 error2 = sbuf_finish(&sb);
2002 return (error != 0 ? error : error2);
2006 * This sysctl allows a process to retrieve ELF auxiliary vector of
2010 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
2012 int *name = (int *)arg1;
2013 u_int namelen = arg2;
2021 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2024 if ((p->p_flag & P_SYSTEM) != 0) {
2028 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2029 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2030 error = proc_getauxv(curthread, p, &sb);
2031 error2 = sbuf_finish(&sb);
2034 return (error != 0 ? error : error2);
2038 * This sysctl allows a process to retrieve the path of the executable for
2039 * itself or another process.
2042 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
2044 pid_t *pidp = (pid_t *)arg1;
2045 unsigned int arglen = arg2;
2048 char *retbuf, *freebuf;
2053 if (*pidp == -1) { /* -1 means this process */
2054 p = req->td->td_proc;
2056 error = pget(*pidp, PGET_CANSEE, &p);
2070 error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
2074 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
2075 free(freebuf, M_TEMP);
2080 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
2093 error = pget((pid_t)name[0], PGET_CANSEE, &p);
2096 sv_name = p->p_sysent->sv_name;
2098 return (sysctl_handle_string(oidp, sv_name, 0, req));
2101 #ifdef KINFO_OVMENTRY_SIZE
2102 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
2105 #ifdef COMPAT_FREEBSD7
2107 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2109 vm_map_entry_t entry, tmp_entry;
2110 unsigned int last_timestamp;
2111 char *fullpath, *freepath;
2112 struct kinfo_ovmentry *kve;
2122 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2125 vm = vmspace_acquire_ref(p);
2130 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2133 vm_map_lock_read(map);
2134 for (entry = map->header.next; entry != &map->header;
2135 entry = entry->next) {
2136 vm_object_t obj, tobj, lobj;
2139 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2142 bzero(kve, sizeof(*kve));
2143 kve->kve_structsize = sizeof(*kve);
2145 kve->kve_private_resident = 0;
2146 obj = entry->object.vm_object;
2148 VM_OBJECT_RLOCK(obj);
2149 if (obj->shadow_count == 1)
2150 kve->kve_private_resident =
2151 obj->resident_page_count;
2153 kve->kve_resident = 0;
2154 addr = entry->start;
2155 while (addr < entry->end) {
2156 if (pmap_extract(map->pmap, addr))
2157 kve->kve_resident++;
2161 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2163 VM_OBJECT_RLOCK(tobj);
2164 kve->kve_offset += tobj->backing_object_offset;
2167 VM_OBJECT_RUNLOCK(lobj);
2171 kve->kve_start = (void*)entry->start;
2172 kve->kve_end = (void*)entry->end;
2173 kve->kve_offset += (off_t)entry->offset;
2175 if (entry->protection & VM_PROT_READ)
2176 kve->kve_protection |= KVME_PROT_READ;
2177 if (entry->protection & VM_PROT_WRITE)
2178 kve->kve_protection |= KVME_PROT_WRITE;
2179 if (entry->protection & VM_PROT_EXECUTE)
2180 kve->kve_protection |= KVME_PROT_EXEC;
2182 if (entry->eflags & MAP_ENTRY_COW)
2183 kve->kve_flags |= KVME_FLAG_COW;
2184 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2185 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2186 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2187 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2189 last_timestamp = map->timestamp;
2190 vm_map_unlock_read(map);
2192 kve->kve_fileid = 0;
2198 switch (lobj->type) {
2200 kve->kve_type = KVME_TYPE_DEFAULT;
2203 kve->kve_type = KVME_TYPE_VNODE;
2208 if ((lobj->flags & OBJ_TMPFS_NODE) != 0) {
2209 kve->kve_type = KVME_TYPE_VNODE;
2210 if ((lobj->flags & OBJ_TMPFS) != 0) {
2211 vp = lobj->un_pager.swp.swp_tmpfs;
2215 kve->kve_type = KVME_TYPE_SWAP;
2219 kve->kve_type = KVME_TYPE_DEVICE;
2222 kve->kve_type = KVME_TYPE_PHYS;
2225 kve->kve_type = KVME_TYPE_DEAD;
2228 kve->kve_type = KVME_TYPE_SG;
2231 kve->kve_type = KVME_TYPE_UNKNOWN;
2235 VM_OBJECT_RUNLOCK(lobj);
2237 kve->kve_ref_count = obj->ref_count;
2238 kve->kve_shadow_count = obj->shadow_count;
2239 VM_OBJECT_RUNLOCK(obj);
2241 vn_fullpath(curthread, vp, &fullpath,
2243 cred = curthread->td_ucred;
2244 vn_lock(vp, LK_SHARED | LK_RETRY);
2245 if (VOP_GETATTR(vp, &va, cred) == 0) {
2246 kve->kve_fileid = va.va_fileid;
2248 kve->kve_fsid = va.va_fsid;
2253 kve->kve_type = KVME_TYPE_NONE;
2254 kve->kve_ref_count = 0;
2255 kve->kve_shadow_count = 0;
2258 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2259 if (freepath != NULL)
2260 free(freepath, M_TEMP);
2262 error = SYSCTL_OUT(req, kve, sizeof(*kve));
2263 vm_map_lock_read(map);
2266 if (last_timestamp != map->timestamp) {
2267 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2271 vm_map_unlock_read(map);
2277 #endif /* COMPAT_FREEBSD7 */
2279 #ifdef KINFO_VMENTRY_SIZE
2280 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2284 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2285 int *resident_count, bool *super)
2287 vm_object_t obj, tobj;
2290 vm_paddr_t locked_pa;
2291 vm_pindex_t pi, pi_adv, pindex;
2294 *resident_count = 0;
2295 if (vmmap_skip_res_cnt)
2299 obj = entry->object.vm_object;
2300 addr = entry->start;
2302 pi = OFF_TO_IDX(entry->offset);
2303 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2304 if (m_adv != NULL) {
2307 pi_adv = atop(entry->end - addr);
2309 for (tobj = obj;; tobj = tobj->backing_object) {
2310 m = vm_page_find_least(tobj, pindex);
2312 if (m->pindex == pindex)
2314 if (pi_adv > m->pindex - pindex) {
2315 pi_adv = m->pindex - pindex;
2319 if (tobj->backing_object == NULL)
2321 pindex += OFF_TO_IDX(tobj->
2322 backing_object_offset);
2326 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2327 (addr & (pagesizes[1] - 1)) == 0 &&
2328 (pmap_mincore(map->pmap, addr, &locked_pa) &
2329 MINCORE_SUPER) != 0) {
2331 pi_adv = atop(pagesizes[1]);
2334 * We do not test the found page on validity.
2335 * Either the page is busy and being paged in,
2336 * or it was invalidated. The first case
2337 * should be counted as resident, the second
2338 * is not so clear; we do account both.
2342 *resident_count += pi_adv;
2345 PA_UNLOCK_COND(locked_pa);
2349 * Must be called with the process locked and will return unlocked.
2352 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
2354 vm_map_entry_t entry, tmp_entry;
2357 vm_object_t obj, tobj, lobj;
2358 char *fullpath, *freepath;
2359 struct kinfo_vmentry *kve;
2364 unsigned int last_timestamp;
2368 PROC_LOCK_ASSERT(p, MA_OWNED);
2372 vm = vmspace_acquire_ref(p);
2377 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK | M_ZERO);
2381 vm_map_lock_read(map);
2382 for (entry = map->header.next; entry != &map->header;
2383 entry = entry->next) {
2384 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2388 bzero(kve, sizeof(*kve));
2389 obj = entry->object.vm_object;
2391 for (tobj = obj; tobj != NULL;
2392 tobj = tobj->backing_object) {
2393 VM_OBJECT_RLOCK(tobj);
2394 kve->kve_offset += tobj->backing_object_offset;
2397 if (obj->backing_object == NULL)
2398 kve->kve_private_resident =
2399 obj->resident_page_count;
2400 kern_proc_vmmap_resident(map, entry,
2401 &kve->kve_resident, &super);
2403 kve->kve_flags |= KVME_FLAG_SUPER;
2404 for (tobj = obj; tobj != NULL;
2405 tobj = tobj->backing_object) {
2406 if (tobj != obj && tobj != lobj)
2407 VM_OBJECT_RUNLOCK(tobj);
2413 kve->kve_start = entry->start;
2414 kve->kve_end = entry->end;
2415 kve->kve_offset += entry->offset;
2417 if (entry->protection & VM_PROT_READ)
2418 kve->kve_protection |= KVME_PROT_READ;
2419 if (entry->protection & VM_PROT_WRITE)
2420 kve->kve_protection |= KVME_PROT_WRITE;
2421 if (entry->protection & VM_PROT_EXECUTE)
2422 kve->kve_protection |= KVME_PROT_EXEC;
2424 if (entry->eflags & MAP_ENTRY_COW)
2425 kve->kve_flags |= KVME_FLAG_COW;
2426 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2427 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2428 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2429 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2430 if (entry->eflags & MAP_ENTRY_GROWS_UP)
2431 kve->kve_flags |= KVME_FLAG_GROWS_UP;
2432 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2433 kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2435 last_timestamp = map->timestamp;
2436 vm_map_unlock_read(map);
2442 switch (lobj->type) {
2444 kve->kve_type = KVME_TYPE_DEFAULT;
2447 kve->kve_type = KVME_TYPE_VNODE;
2452 if ((lobj->flags & OBJ_TMPFS_NODE) != 0) {
2453 kve->kve_type = KVME_TYPE_VNODE;
2454 if ((lobj->flags & OBJ_TMPFS) != 0) {
2455 vp = lobj->un_pager.swp.swp_tmpfs;
2459 kve->kve_type = KVME_TYPE_SWAP;
2463 kve->kve_type = KVME_TYPE_DEVICE;
2466 kve->kve_type = KVME_TYPE_PHYS;
2469 kve->kve_type = KVME_TYPE_DEAD;
2472 kve->kve_type = KVME_TYPE_SG;
2474 case OBJT_MGTDEVICE:
2475 kve->kve_type = KVME_TYPE_MGTDEVICE;
2478 kve->kve_type = KVME_TYPE_UNKNOWN;
2482 VM_OBJECT_RUNLOCK(lobj);
2484 kve->kve_ref_count = obj->ref_count;
2485 kve->kve_shadow_count = obj->shadow_count;
2486 VM_OBJECT_RUNLOCK(obj);
2488 vn_fullpath(curthread, vp, &fullpath,
2490 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2491 cred = curthread->td_ucred;
2492 vn_lock(vp, LK_SHARED | LK_RETRY);
2493 if (VOP_GETATTR(vp, &va, cred) == 0) {
2494 kve->kve_vn_fileid = va.va_fileid;
2495 kve->kve_vn_fsid = va.va_fsid;
2496 kve->kve_vn_fsid_freebsd11 =
2497 kve->kve_vn_fsid; /* truncate */
2499 MAKEIMODE(va.va_type, va.va_mode);
2500 kve->kve_vn_size = va.va_size;
2501 kve->kve_vn_rdev = va.va_rdev;
2502 kve->kve_vn_rdev_freebsd11 =
2503 kve->kve_vn_rdev; /* truncate */
2504 kve->kve_status = KF_ATTR_VALID;
2509 kve->kve_type = KVME_TYPE_NONE;
2510 kve->kve_ref_count = 0;
2511 kve->kve_shadow_count = 0;
2514 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2515 if (freepath != NULL)
2516 free(freepath, M_TEMP);
2518 /* Pack record size down */
2519 if ((flags & KERN_VMMAP_PACK_KINFO) != 0)
2520 kve->kve_structsize =
2521 offsetof(struct kinfo_vmentry, kve_path) +
2522 strlen(kve->kve_path) + 1;
2524 kve->kve_structsize = sizeof(*kve);
2525 kve->kve_structsize = roundup(kve->kve_structsize,
2528 /* Halt filling and truncate rather than exceeding maxlen */
2529 if (maxlen != -1 && maxlen < kve->kve_structsize) {
2531 vm_map_lock_read(map);
2533 } else if (maxlen != -1)
2534 maxlen -= kve->kve_structsize;
2536 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2538 vm_map_lock_read(map);
2541 if (last_timestamp != map->timestamp) {
2542 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2546 vm_map_unlock_read(map);
2554 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2558 int error, error2, *name;
2561 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2562 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2563 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2568 error = kern_proc_vmmap_out(p, &sb, -1, KERN_VMMAP_PACK_KINFO);
2569 error2 = sbuf_finish(&sb);
2571 return (error != 0 ? error : error2);
2574 #if defined(STACK) || defined(DDB)
2576 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2578 struct kinfo_kstack *kkstp;
2579 int error, i, *name, numthreads;
2580 lwpid_t *lwpidarray;
2587 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2591 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2592 st = stack_create(M_WAITOK);
2597 if (lwpidarray != NULL) {
2598 free(lwpidarray, M_TEMP);
2601 numthreads = p->p_numthreads;
2603 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2606 } while (numthreads < p->p_numthreads);
2609 * XXXRW: During the below loop, execve(2) and countless other sorts
2610 * of changes could have taken place. Should we check to see if the
2611 * vmspace has been replaced, or the like, in order to prevent
2612 * giving a snapshot that spans, say, execve(2), with some threads
2613 * before and some after? Among other things, the credentials could
2614 * have changed, in which case the right to extract debug info might
2615 * no longer be assured.
2618 FOREACH_THREAD_IN_PROC(p, td) {
2619 KASSERT(i < numthreads,
2620 ("sysctl_kern_proc_kstack: numthreads"));
2621 lwpidarray[i] = td->td_tid;
2625 for (i = 0; i < numthreads; i++) {
2626 td = thread_find(p, lwpidarray[i]);
2630 bzero(kkstp, sizeof(*kkstp));
2631 (void)sbuf_new(&sb, kkstp->kkst_trace,
2632 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2634 kkstp->kkst_tid = td->td_tid;
2635 if (TD_IS_SWAPPED(td)) {
2636 kkstp->kkst_state = KKST_STATE_SWAPPED;
2637 } else if (TD_IS_RUNNING(td)) {
2638 if (stack_save_td_running(st, td) == 0)
2639 kkstp->kkst_state = KKST_STATE_STACKOK;
2641 kkstp->kkst_state = KKST_STATE_RUNNING;
2643 kkstp->kkst_state = KKST_STATE_STACKOK;
2644 stack_save_td(st, td);
2648 stack_sbuf_print(&sb, st);
2651 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2658 if (lwpidarray != NULL)
2659 free(lwpidarray, M_TEMP);
2661 free(kkstp, M_TEMP);
2667 * This sysctl allows a process to retrieve the full list of groups from
2668 * itself or another process.
2671 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2673 pid_t *pidp = (pid_t *)arg1;
2674 unsigned int arglen = arg2;
2681 if (*pidp == -1) { /* -1 means this process */
2682 p = req->td->td_proc;
2685 error = pget(*pidp, PGET_CANSEE, &p);
2690 cred = crhold(p->p_ucred);
2693 error = SYSCTL_OUT(req, cred->cr_groups,
2694 cred->cr_ngroups * sizeof(gid_t));
2700 * This sysctl allows a process to retrieve or/and set the resource limit for
2704 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2706 int *name = (int *)arg1;
2707 u_int namelen = arg2;
2716 which = (u_int)name[1];
2717 if (which >= RLIM_NLIMITS)
2720 if (req->newptr != NULL && req->newlen != sizeof(rlim))
2723 flags = PGET_HOLD | PGET_NOTWEXIT;
2724 if (req->newptr != NULL)
2725 flags |= PGET_CANDEBUG;
2727 flags |= PGET_CANSEE;
2728 error = pget((pid_t)name[0], flags, &p);
2735 if (req->oldptr != NULL) {
2737 lim_rlimit_proc(p, which, &rlim);
2740 error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
2747 if (req->newptr != NULL) {
2748 error = SYSCTL_IN(req, &rlim, sizeof(rlim));
2750 error = kern_proc_setrlimit(curthread, p, which, &rlim);
2759 * This sysctl allows a process to retrieve ps_strings structure location of
2763 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2765 int *name = (int *)arg1;
2766 u_int namelen = arg2;
2768 vm_offset_t ps_strings;
2770 #ifdef COMPAT_FREEBSD32
2771 uint32_t ps_strings32;
2777 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2780 #ifdef COMPAT_FREEBSD32
2781 if ((req->flags & SCTL_MASK32) != 0) {
2783 * We return 0 if the 32 bit emulation request is for a 64 bit
2786 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
2787 PTROUT(p->p_sysent->sv_psstrings) : 0;
2789 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
2793 ps_strings = p->p_sysent->sv_psstrings;
2795 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
2800 * This sysctl allows a process to retrieve umask of another process.
2803 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
2805 int *name = (int *)arg1;
2806 u_int namelen = arg2;
2815 pid = (pid_t)name[0];
2817 if (pid == p->p_pid || pid == 0) {
2818 fd_cmask = p->p_fd->fd_cmask;
2822 error = pget(pid, PGET_WANTREAD, &p);
2826 fd_cmask = p->p_fd->fd_cmask;
2829 error = SYSCTL_OUT(req, &fd_cmask, sizeof(fd_cmask));
2834 * This sysctl allows a process to set and retrieve binary osreldate of
2838 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
2840 int *name = (int *)arg1;
2841 u_int namelen = arg2;
2843 int flags, error, osrel;
2848 if (req->newptr != NULL && req->newlen != sizeof(osrel))
2851 flags = PGET_HOLD | PGET_NOTWEXIT;
2852 if (req->newptr != NULL)
2853 flags |= PGET_CANDEBUG;
2855 flags |= PGET_CANSEE;
2856 error = pget((pid_t)name[0], flags, &p);
2860 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
2864 if (req->newptr != NULL) {
2865 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
2880 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
2882 int *name = (int *)arg1;
2883 u_int namelen = arg2;
2885 struct kinfo_sigtramp kst;
2886 const struct sysentvec *sv;
2888 #ifdef COMPAT_FREEBSD32
2889 struct kinfo_sigtramp32 kst32;
2895 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2899 #ifdef COMPAT_FREEBSD32
2900 if ((req->flags & SCTL_MASK32) != 0) {
2901 bzero(&kst32, sizeof(kst32));
2902 if (SV_PROC_FLAG(p, SV_ILP32)) {
2903 if (sv->sv_sigcode_base != 0) {
2904 kst32.ksigtramp_start = sv->sv_sigcode_base;
2905 kst32.ksigtramp_end = sv->sv_sigcode_base +
2908 kst32.ksigtramp_start = sv->sv_psstrings -
2910 kst32.ksigtramp_end = sv->sv_psstrings;
2914 error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
2918 bzero(&kst, sizeof(kst));
2919 if (sv->sv_sigcode_base != 0) {
2920 kst.ksigtramp_start = (char *)sv->sv_sigcode_base;
2921 kst.ksigtramp_end = (char *)sv->sv_sigcode_base +
2924 kst.ksigtramp_start = (char *)sv->sv_psstrings -
2926 kst.ksigtramp_end = (char *)sv->sv_psstrings;
2929 error = SYSCTL_OUT(req, &kst, sizeof(kst));
2933 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
2935 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
2936 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
2937 "Return entire process table");
2939 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2940 sysctl_kern_proc, "Process table");
2942 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
2943 sysctl_kern_proc, "Process table");
2945 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2946 sysctl_kern_proc, "Process table");
2948 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
2949 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2951 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
2952 sysctl_kern_proc, "Process table");
2954 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2955 sysctl_kern_proc, "Process table");
2957 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2958 sysctl_kern_proc, "Process table");
2960 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2961 sysctl_kern_proc, "Process table");
2963 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
2964 sysctl_kern_proc, "Return process table, no threads");
2966 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
2967 CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
2968 sysctl_kern_proc_args, "Process argument list");
2970 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
2971 sysctl_kern_proc_env, "Process environment");
2973 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
2974 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
2976 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
2977 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
2979 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
2980 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
2981 "Process syscall vector name (ABI type)");
2983 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
2984 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2986 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
2987 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2989 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
2990 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2992 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
2993 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2995 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
2996 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2998 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
2999 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3001 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
3002 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3004 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
3005 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3007 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
3008 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
3009 "Return process table, no threads");
3011 #ifdef COMPAT_FREEBSD7
3012 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
3013 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
3016 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
3017 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
3019 #if defined(STACK) || defined(DDB)
3020 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
3021 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
3024 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
3025 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
3027 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
3028 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
3029 "Process resource limits");
3031 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
3032 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
3033 "Process ps_strings location");
3035 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
3036 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
3038 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
3039 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
3040 "Process binary osreldate");
3042 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
3043 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
3044 "Process signal trampoline location");
3049 * stop_all_proc() purpose is to stop all process which have usermode,
3050 * except current process for obvious reasons. This makes it somewhat
3051 * unreliable when invoked from multithreaded process. The service
3052 * must not be user-callable anyway.
3057 struct proc *cp, *p;
3059 bool restart, seen_stopped, seen_exiting, stopped_some;
3063 sx_xlock(&allproc_lock);
3065 seen_exiting = seen_stopped = stopped_some = restart = false;
3066 LIST_REMOVE(cp, p_list);
3067 LIST_INSERT_HEAD(&allproc, cp, p_list);
3069 p = LIST_NEXT(cp, p_list);
3072 LIST_REMOVE(cp, p_list);
3073 LIST_INSERT_AFTER(p, cp, p_list);
3075 if ((p->p_flag & (P_KPROC | P_SYSTEM | P_TOTAL_STOP)) != 0) {
3079 if ((p->p_flag & P_WEXIT) != 0) {
3080 seen_exiting = true;
3084 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
3086 * Stopped processes are tolerated when there
3087 * are no other processes which might continue
3088 * them. P_STOPPED_SINGLE but not
3089 * P_TOTAL_STOP process still has at least one
3092 seen_stopped = true;
3097 sx_xunlock(&allproc_lock);
3098 r = thread_single(p, SINGLE_ALLPROC);
3102 stopped_some = true;
3105 sx_xlock(&allproc_lock);
3107 /* Catch forked children we did not see in iteration. */
3108 if (gen != allproc_gen)
3110 sx_xunlock(&allproc_lock);
3111 if (restart || stopped_some || seen_exiting || seen_stopped) {
3112 kern_yield(PRI_USER);
3118 resume_all_proc(void)
3120 struct proc *cp, *p;
3123 sx_xlock(&allproc_lock);
3125 LIST_REMOVE(cp, p_list);
3126 LIST_INSERT_HEAD(&allproc, cp, p_list);
3128 p = LIST_NEXT(cp, p_list);
3131 LIST_REMOVE(cp, p_list);
3132 LIST_INSERT_AFTER(p, cp, p_list);
3134 if ((p->p_flag & P_TOTAL_STOP) != 0) {
3135 sx_xunlock(&allproc_lock);
3137 thread_single_end(p, SINGLE_ALLPROC);
3140 sx_xlock(&allproc_lock);
3145 /* Did the loop above missed any stopped process ? */
3146 LIST_FOREACH(p, &allproc, p_list) {
3147 /* No need for proc lock. */
3148 if ((p->p_flag & P_TOTAL_STOP) != 0)
3151 sx_xunlock(&allproc_lock);
3154 /* #define TOTAL_STOP_DEBUG 1 */
3155 #ifdef TOTAL_STOP_DEBUG
3156 volatile static int ap_resume;
3157 #include <sys/mount.h>
3160 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
3166 error = sysctl_handle_int(oidp, &val, 0, req);
3167 if (error != 0 || req->newptr == NULL)
3172 while (ap_resume == 0)
3180 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
3181 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
3182 sysctl_debug_stop_all_proc, "I",