2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_compat.h"
37 #include "opt_ktrace.h"
38 #include "opt_kstack_pages.h"
39 #include "opt_stack.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/eventhandler.h>
47 #include <sys/kernel.h>
48 #include <sys/limits.h>
50 #include <sys/loginclass.h>
51 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/mutex.h>
56 #include <sys/ptrace.h>
57 #include <sys/refcount.h>
58 #include <sys/resourcevar.h>
59 #include <sys/rwlock.h>
61 #include <sys/sysent.h>
62 #include <sys/sched.h>
64 #include <sys/stack.h>
66 #include <sys/sysctl.h>
67 #include <sys/filedesc.h>
69 #include <sys/signalvar.h>
73 #include <sys/vnode.h>
81 #include <vm/vm_param.h>
82 #include <vm/vm_extern.h>
84 #include <vm/vm_map.h>
85 #include <vm/vm_object.h>
86 #include <vm/vm_page.h>
89 #ifdef COMPAT_FREEBSD32
90 #include <compat/freebsd32/freebsd32.h>
91 #include <compat/freebsd32/freebsd32_util.h>
94 SDT_PROVIDER_DEFINE(proc);
95 SDT_PROBE_DEFINE4(proc, , ctor, entry, "struct proc *", "int", "void *",
97 SDT_PROBE_DEFINE4(proc, , ctor, return, "struct proc *", "int", "void *",
99 SDT_PROBE_DEFINE4(proc, , dtor, entry, "struct proc *", "int", "void *",
101 SDT_PROBE_DEFINE3(proc, , dtor, return, "struct proc *", "int", "void *");
102 SDT_PROBE_DEFINE3(proc, , init, entry, "struct proc *", "int", "int");
103 SDT_PROBE_DEFINE3(proc, , init, return, "struct proc *", "int", "int");
105 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
106 MALLOC_DEFINE(M_SESSION, "session", "session header");
107 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
108 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
110 static void doenterpgrp(struct proc *, struct pgrp *);
111 static void orphanpg(struct pgrp *pg);
112 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
113 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
114 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
116 static void pgadjustjobc(struct pgrp *pgrp, int entering);
117 static void pgdelete(struct pgrp *);
118 static int proc_ctor(void *mem, int size, void *arg, int flags);
119 static void proc_dtor(void *mem, int size, void *arg);
120 static int proc_init(void *mem, int size, int flags);
121 static void proc_fini(void *mem, int size);
122 static void pargs_free(struct pargs *pa);
123 static struct proc *zpfind_locked(pid_t pid);
126 * Other process lists
128 struct pidhashhead *pidhashtbl;
130 struct pgrphashhead *pgrphashtbl;
132 struct proclist allproc;
133 struct proclist zombproc;
134 struct sx __exclusive_cache_line allproc_lock;
135 struct sx __exclusive_cache_line proctree_lock;
136 struct mtx __exclusive_cache_line ppeers_lock;
137 uma_zone_t proc_zone;
140 * The offset of various fields in struct proc and struct thread.
141 * These are used by kernel debuggers to enumerate kernel threads and
144 const int proc_off_p_pid = offsetof(struct proc, p_pid);
145 const int proc_off_p_comm = offsetof(struct proc, p_comm);
146 const int proc_off_p_list = offsetof(struct proc, p_list);
147 const int proc_off_p_threads = offsetof(struct proc, p_threads);
148 const int thread_off_td_tid = offsetof(struct thread, td_tid);
149 const int thread_off_td_name = offsetof(struct thread, td_name);
150 const int thread_off_td_oncpu = offsetof(struct thread, td_oncpu);
151 const int thread_off_td_pcb = offsetof(struct thread, td_pcb);
152 const int thread_off_td_plist = offsetof(struct thread, td_plist);
154 EVENTHANDLER_LIST_DEFINE(process_ctor);
155 EVENTHANDLER_LIST_DEFINE(process_dtor);
156 EVENTHANDLER_LIST_DEFINE(process_init);
157 EVENTHANDLER_LIST_DEFINE(process_fini);
158 EVENTHANDLER_LIST_DEFINE(process_exit);
159 EVENTHANDLER_LIST_DEFINE(process_fork);
160 EVENTHANDLER_LIST_DEFINE(process_exec);
162 EVENTHANDLER_LIST_DECLARE(thread_ctor);
163 EVENTHANDLER_LIST_DECLARE(thread_dtor);
165 int kstack_pages = KSTACK_PAGES;
166 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
167 "Kernel stack size in pages");
168 static int vmmap_skip_res_cnt = 0;
169 SYSCTL_INT(_kern, OID_AUTO, proc_vmmap_skip_resident_count, CTLFLAG_RW,
170 &vmmap_skip_res_cnt, 0,
171 "Skip calculation of the pages resident count in kern.proc.vmmap");
173 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
174 #ifdef COMPAT_FREEBSD32
175 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
179 * Initialize global process hashing structures.
185 sx_init(&allproc_lock, "allproc");
186 sx_init(&proctree_lock, "proctree");
187 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
189 LIST_INIT(&zombproc);
190 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
191 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
192 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
193 proc_ctor, proc_dtor, proc_init, proc_fini,
194 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
199 * Prepare a proc for use.
202 proc_ctor(void *mem, int size, void *arg, int flags)
207 p = (struct proc *)mem;
208 SDT_PROBE4(proc, , ctor , entry, p, size, arg, flags);
209 EVENTHANDLER_DIRECT_INVOKE(process_ctor, p);
210 SDT_PROBE4(proc, , ctor , return, p, size, arg, flags);
211 td = FIRST_THREAD_IN_PROC(p);
213 /* Make sure all thread constructors are executed */
214 EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td);
220 * Reclaim a proc after use.
223 proc_dtor(void *mem, int size, void *arg)
228 /* INVARIANTS checks go here */
229 p = (struct proc *)mem;
230 td = FIRST_THREAD_IN_PROC(p);
231 SDT_PROBE4(proc, , dtor, entry, p, size, arg, td);
234 KASSERT((p->p_numthreads == 1),
235 ("bad number of threads in exiting process"));
236 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
238 /* Free all OSD associated to this thread. */
240 td_softdep_cleanup(td);
241 MPASS(td->td_su == NULL);
243 /* Make sure all thread destructors are executed */
244 EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td);
246 EVENTHANDLER_DIRECT_INVOKE(process_dtor, p);
247 if (p->p_ksi != NULL)
248 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
249 SDT_PROBE3(proc, , dtor, return, p, size, arg);
253 * Initialize type-stable parts of a proc (when newly created).
256 proc_init(void *mem, int size, int flags)
260 p = (struct proc *)mem;
261 SDT_PROBE3(proc, , init, entry, p, size, flags);
262 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK | MTX_NEW);
263 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_NEW);
264 mtx_init(&p->p_statmtx, "pstatl", NULL, MTX_SPIN | MTX_NEW);
265 mtx_init(&p->p_itimmtx, "pitiml", NULL, MTX_SPIN | MTX_NEW);
266 mtx_init(&p->p_profmtx, "pprofl", NULL, MTX_SPIN | MTX_NEW);
267 cv_init(&p->p_pwait, "ppwait");
268 cv_init(&p->p_dbgwait, "dbgwait");
269 TAILQ_INIT(&p->p_threads); /* all threads in proc */
270 EVENTHANDLER_DIRECT_INVOKE(process_init, p);
271 p->p_stats = pstats_alloc();
273 SDT_PROBE3(proc, , init, return, p, size, flags);
278 * UMA should ensure that this function is never called.
279 * Freeing a proc structure would violate type stability.
282 proc_fini(void *mem, int size)
287 p = (struct proc *)mem;
288 EVENTHANDLER_DIRECT_INVOKE(process_fini, p);
289 pstats_free(p->p_stats);
290 thread_free(FIRST_THREAD_IN_PROC(p));
291 mtx_destroy(&p->p_mtx);
292 if (p->p_ksi != NULL)
293 ksiginfo_free(p->p_ksi);
295 panic("proc reclaimed");
300 * Is p an inferior of the current process?
303 inferior(struct proc *p)
306 sx_assert(&proctree_lock, SX_LOCKED);
307 PROC_LOCK_ASSERT(p, MA_OWNED);
308 for (; p != curproc; p = proc_realparent(p)) {
316 pfind_locked(pid_t pid)
320 sx_assert(&allproc_lock, SX_LOCKED);
321 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
322 if (p->p_pid == pid) {
324 if (p->p_state == PRS_NEW) {
335 * Locate a process by number; return only "live" processes -- i.e., neither
336 * zombies nor newly born but incompletely initialized processes. By not
337 * returning processes in the PRS_NEW state, we allow callers to avoid
338 * testing for that condition to avoid dereferencing p_ucred, et al.
345 sx_slock(&allproc_lock);
346 p = pfind_locked(pid);
347 sx_sunlock(&allproc_lock);
352 pfind_tid_locked(pid_t tid)
357 sx_assert(&allproc_lock, SX_LOCKED);
358 FOREACH_PROC_IN_SYSTEM(p) {
360 if (p->p_state == PRS_NEW) {
364 FOREACH_THREAD_IN_PROC(p, td) {
365 if (td->td_tid == tid)
375 * Locate a process group by number.
376 * The caller must hold proctree_lock.
383 sx_assert(&proctree_lock, SX_LOCKED);
385 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
386 if (pgrp->pg_id == pgid) {
395 * Locate process and do additional manipulations, depending on flags.
398 pget(pid_t pid, int flags, struct proc **pp)
404 if (p->p_pid == pid) {
407 sx_slock(&allproc_lock);
408 if (pid <= PID_MAX) {
409 p = pfind_locked(pid);
410 if (p == NULL && (flags & PGET_NOTWEXIT) == 0)
411 p = zpfind_locked(pid);
412 } else if ((flags & PGET_NOTID) == 0) {
413 p = pfind_tid_locked(pid);
417 sx_sunlock(&allproc_lock);
420 if ((flags & PGET_CANSEE) != 0) {
421 error = p_cansee(curthread, p);
426 if ((flags & PGET_CANDEBUG) != 0) {
427 error = p_candebug(curthread, p);
431 if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
435 if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
439 if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
441 * XXXRW: Not clear ESRCH is the right error during proc
447 if ((flags & PGET_HOLD) != 0) {
459 * Create a new process group.
460 * pgid must be equal to the pid of p.
461 * Begin a new session if required.
464 enterpgrp(struct proc *p, pid_t pgid, struct pgrp *pgrp, struct session *sess)
467 sx_assert(&proctree_lock, SX_XLOCKED);
469 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
470 KASSERT(p->p_pid == pgid,
471 ("enterpgrp: new pgrp and pid != pgid"));
472 KASSERT(pgfind(pgid) == NULL,
473 ("enterpgrp: pgrp with pgid exists"));
474 KASSERT(!SESS_LEADER(p),
475 ("enterpgrp: session leader attempted setpgrp"));
477 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
483 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
485 p->p_flag &= ~P_CONTROLT;
489 sess->s_sid = p->p_pid;
490 refcount_init(&sess->s_count, 1);
491 sess->s_ttyvp = NULL;
492 sess->s_ttydp = NULL;
494 bcopy(p->p_session->s_login, sess->s_login,
495 sizeof(sess->s_login));
496 pgrp->pg_session = sess;
497 KASSERT(p == curproc,
498 ("enterpgrp: mksession and p != curproc"));
500 pgrp->pg_session = p->p_session;
501 sess_hold(pgrp->pg_session);
505 LIST_INIT(&pgrp->pg_members);
508 * As we have an exclusive lock of proctree_lock,
509 * this should not deadlock.
511 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
513 SLIST_INIT(&pgrp->pg_sigiolst);
516 doenterpgrp(p, pgrp);
522 * Move p to an existing process group
525 enterthispgrp(struct proc *p, struct pgrp *pgrp)
528 sx_assert(&proctree_lock, SX_XLOCKED);
529 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
530 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
531 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
532 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
533 KASSERT(pgrp->pg_session == p->p_session,
534 ("%s: pgrp's session %p, p->p_session %p.\n",
538 KASSERT(pgrp != p->p_pgrp,
539 ("%s: p belongs to pgrp.", __func__));
541 doenterpgrp(p, pgrp);
547 * Move p to a process group
550 doenterpgrp(struct proc *p, struct pgrp *pgrp)
552 struct pgrp *savepgrp;
554 sx_assert(&proctree_lock, SX_XLOCKED);
555 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
556 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
557 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
558 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
560 savepgrp = p->p_pgrp;
563 * Adjust eligibility of affected pgrps to participate in job control.
564 * Increment eligibility counts before decrementing, otherwise we
565 * could reach 0 spuriously during the first call.
568 fixjobc(p, p->p_pgrp, 0);
573 LIST_REMOVE(p, p_pglist);
576 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
577 PGRP_UNLOCK(savepgrp);
579 if (LIST_EMPTY(&savepgrp->pg_members))
584 * remove process from process group
587 leavepgrp(struct proc *p)
589 struct pgrp *savepgrp;
591 sx_assert(&proctree_lock, SX_XLOCKED);
592 savepgrp = p->p_pgrp;
595 LIST_REMOVE(p, p_pglist);
598 PGRP_UNLOCK(savepgrp);
599 if (LIST_EMPTY(&savepgrp->pg_members))
605 * delete a process group
608 pgdelete(struct pgrp *pgrp)
610 struct session *savesess;
613 sx_assert(&proctree_lock, SX_XLOCKED);
614 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
615 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
618 * Reset any sigio structures pointing to us as a result of
619 * F_SETOWN with our pgid.
621 funsetownlst(&pgrp->pg_sigiolst);
624 tp = pgrp->pg_session->s_ttyp;
625 LIST_REMOVE(pgrp, pg_hash);
626 savesess = pgrp->pg_session;
629 /* Remove the reference to the pgrp before deallocating it. */
632 tty_rel_pgrp(tp, pgrp);
635 mtx_destroy(&pgrp->pg_mtx);
637 sess_release(savesess);
641 pgadjustjobc(struct pgrp *pgrp, int entering)
649 if (pgrp->pg_jobc == 0)
656 * Adjust pgrp jobc counters when specified process changes process group.
657 * We count the number of processes in each process group that "qualify"
658 * the group for terminal job control (those with a parent in a different
659 * process group of the same session). If that count reaches zero, the
660 * process group becomes orphaned. Check both the specified process'
661 * process group and that of its children.
662 * entering == 0 => p is leaving specified group.
663 * entering == 1 => p is entering specified group.
666 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
668 struct pgrp *hispgrp;
669 struct session *mysession;
672 sx_assert(&proctree_lock, SX_LOCKED);
673 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
674 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
675 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
678 * Check p's parent to see whether p qualifies its own process
679 * group; if so, adjust count for p's process group.
681 mysession = pgrp->pg_session;
682 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
683 hispgrp->pg_session == mysession)
684 pgadjustjobc(pgrp, entering);
687 * Check this process' children to see whether they qualify
688 * their process groups; if so, adjust counts for children's
691 LIST_FOREACH(q, &p->p_children, p_sibling) {
693 if (hispgrp == pgrp ||
694 hispgrp->pg_session != mysession)
696 if (q->p_state == PRS_ZOMBIE)
698 pgadjustjobc(hispgrp, entering);
711 MPASS(p->p_flag & P_WEXIT);
713 * Do a quick check to see if there is anything to do with the
714 * proctree_lock held. pgrp and LIST_EMPTY checks are for fixjobc().
717 if (!SESS_LEADER(p) &&
718 (p->p_pgrp == p->p_pptr->p_pgrp) &&
719 LIST_EMPTY(&p->p_children)) {
725 sx_xlock(&proctree_lock);
726 if (SESS_LEADER(p)) {
730 * s_ttyp is not zero'd; we use this to indicate that
731 * the session once had a controlling terminal. (for
732 * logging and informational purposes)
743 * Signal foreground pgrp and revoke access to
744 * controlling terminal if it has not been revoked
747 * Because the TTY may have been revoked in the mean
748 * time and could already have a new session associated
749 * with it, make sure we don't send a SIGHUP to a
750 * foreground process group that does not belong to this
756 if (tp->t_session == sp)
757 tty_signal_pgrp(tp, SIGHUP);
762 sx_xunlock(&proctree_lock);
763 if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) {
764 VOP_REVOKE(ttyvp, REVOKEALL);
765 VOP_UNLOCK(ttyvp, 0);
768 sx_xlock(&proctree_lock);
771 fixjobc(p, p->p_pgrp, 0);
772 sx_xunlock(&proctree_lock);
776 * A process group has become orphaned;
777 * if there are any stopped processes in the group,
778 * hang-up all process in that group.
781 orphanpg(struct pgrp *pg)
785 PGRP_LOCK_ASSERT(pg, MA_OWNED);
787 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
789 if (P_SHOULDSTOP(p) == P_STOPPED_SIG) {
791 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
793 kern_psignal(p, SIGHUP);
794 kern_psignal(p, SIGCONT);
804 sess_hold(struct session *s)
807 refcount_acquire(&s->s_count);
811 sess_release(struct session *s)
814 if (refcount_release(&s->s_count)) {
815 if (s->s_ttyp != NULL) {
817 tty_rel_sess(s->s_ttyp, s);
819 mtx_destroy(&s->s_mtx);
826 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
832 for (i = 0; i <= pgrphash; i++) {
833 if (!LIST_EMPTY(&pgrphashtbl[i])) {
834 printf("\tindx %d\n", i);
835 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
837 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
838 (void *)pgrp, (long)pgrp->pg_id,
839 (void *)pgrp->pg_session,
840 pgrp->pg_session->s_count,
841 (void *)LIST_FIRST(&pgrp->pg_members));
842 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
843 printf("\t\tpid %ld addr %p pgrp %p\n",
844 (long)p->p_pid, (void *)p,
854 * Calculate the kinfo_proc members which contain process-wide
856 * Must be called with the target process locked.
859 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
863 PROC_LOCK_ASSERT(p, MA_OWNED);
867 FOREACH_THREAD_IN_PROC(p, td) {
869 kp->ki_pctcpu += sched_pctcpu(td);
870 kp->ki_estcpu += sched_estcpu(td);
876 * Clear kinfo_proc and fill in any information that is common
877 * to all threads in the process.
878 * Must be called with the target process locked.
881 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
888 struct timeval boottime;
890 /* For proc_realparent. */
891 sx_assert(&proctree_lock, SX_LOCKED);
892 PROC_LOCK_ASSERT(p, MA_OWNED);
893 bzero(kp, sizeof(*kp));
895 kp->ki_structsize = sizeof(*kp);
897 kp->ki_addr =/* p->p_addr; */0; /* XXX */
898 kp->ki_args = p->p_args;
899 kp->ki_textvp = p->p_textvp;
901 kp->ki_tracep = p->p_tracevp;
902 kp->ki_traceflag = p->p_traceflag;
905 kp->ki_vmspace = p->p_vmspace;
906 kp->ki_flag = p->p_flag;
907 kp->ki_flag2 = p->p_flag2;
910 kp->ki_uid = cred->cr_uid;
911 kp->ki_ruid = cred->cr_ruid;
912 kp->ki_svuid = cred->cr_svuid;
914 if (cred->cr_flags & CRED_FLAG_CAPMODE)
915 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
916 /* XXX bde doesn't like KI_NGROUPS */
917 if (cred->cr_ngroups > KI_NGROUPS) {
918 kp->ki_ngroups = KI_NGROUPS;
919 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
921 kp->ki_ngroups = cred->cr_ngroups;
922 bcopy(cred->cr_groups, kp->ki_groups,
923 kp->ki_ngroups * sizeof(gid_t));
924 kp->ki_rgid = cred->cr_rgid;
925 kp->ki_svgid = cred->cr_svgid;
926 /* If jailed(cred), emulate the old P_JAILED flag. */
928 kp->ki_flag |= P_JAILED;
929 /* If inside the jail, use 0 as a jail ID. */
930 if (cred->cr_prison != curthread->td_ucred->cr_prison)
931 kp->ki_jid = cred->cr_prison->pr_id;
933 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
934 sizeof(kp->ki_loginclass));
938 mtx_lock(&ps->ps_mtx);
939 kp->ki_sigignore = ps->ps_sigignore;
940 kp->ki_sigcatch = ps->ps_sigcatch;
941 mtx_unlock(&ps->ps_mtx);
943 if (p->p_state != PRS_NEW &&
944 p->p_state != PRS_ZOMBIE &&
945 p->p_vmspace != NULL) {
946 struct vmspace *vm = p->p_vmspace;
948 kp->ki_size = vm->vm_map.size;
949 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
950 FOREACH_THREAD_IN_PROC(p, td0) {
951 if (!TD_IS_SWAPPED(td0))
952 kp->ki_rssize += td0->td_kstack_pages;
954 kp->ki_swrss = vm->vm_swrss;
955 kp->ki_tsize = vm->vm_tsize;
956 kp->ki_dsize = vm->vm_dsize;
957 kp->ki_ssize = vm->vm_ssize;
958 } else if (p->p_state == PRS_ZOMBIE)
960 if (kp->ki_flag & P_INMEM)
961 kp->ki_sflag = PS_INMEM;
964 /* Calculate legacy swtime as seconds since 'swtick'. */
965 kp->ki_swtime = (ticks - p->p_swtick) / hz;
966 kp->ki_pid = p->p_pid;
967 kp->ki_nice = p->p_nice;
968 kp->ki_fibnum = p->p_fibnum;
969 kp->ki_start = p->p_stats->p_start;
970 getboottime(&boottime);
971 timevaladd(&kp->ki_start, &boottime);
973 rufetch(p, &kp->ki_rusage);
974 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
975 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
977 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
978 /* Some callers want child times in a single value. */
979 kp->ki_childtime = kp->ki_childstime;
980 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
982 FOREACH_THREAD_IN_PROC(p, td0)
983 kp->ki_cow += td0->td_cow;
987 kp->ki_pgid = p->p_pgrp->pg_id;
988 kp->ki_jobc = p->p_pgrp->pg_jobc;
989 sp = p->p_pgrp->pg_session;
992 kp->ki_sid = sp->s_sid;
994 strlcpy(kp->ki_login, sp->s_login,
995 sizeof(kp->ki_login));
997 kp->ki_kiflag |= KI_CTTY;
999 kp->ki_kiflag |= KI_SLEADER;
1000 /* XXX proctree_lock */
1005 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
1006 kp->ki_tdev = tty_udev(tp);
1007 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1008 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1010 kp->ki_tsid = tp->t_session->s_sid;
1012 kp->ki_tdev = NODEV;
1013 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1015 if (p->p_comm[0] != '\0')
1016 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
1017 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
1018 p->p_sysent->sv_name[0] != '\0')
1019 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
1020 kp->ki_siglist = p->p_siglist;
1021 kp->ki_xstat = KW_EXITCODE(p->p_xexit, p->p_xsig);
1022 kp->ki_acflag = p->p_acflag;
1023 kp->ki_lock = p->p_lock;
1025 kp->ki_ppid = proc_realparent(p)->p_pid;
1026 if (p->p_flag & P_TRACED)
1027 kp->ki_tracer = p->p_pptr->p_pid;
1032 * Fill in information that is thread specific. Must be called with
1033 * target process locked. If 'preferthread' is set, overwrite certain
1034 * process-related fields that are maintained for both threads and
1038 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
1044 PROC_LOCK_ASSERT(p, MA_OWNED);
1049 if (td->td_wmesg != NULL)
1050 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
1052 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
1053 if (strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)) >=
1054 sizeof(kp->ki_tdname)) {
1055 strlcpy(kp->ki_moretdname,
1056 td->td_name + sizeof(kp->ki_tdname) - 1,
1057 sizeof(kp->ki_moretdname));
1059 bzero(kp->ki_moretdname, sizeof(kp->ki_moretdname));
1061 if (TD_ON_LOCK(td)) {
1062 kp->ki_kiflag |= KI_LOCKBLOCK;
1063 strlcpy(kp->ki_lockname, td->td_lockname,
1064 sizeof(kp->ki_lockname));
1066 kp->ki_kiflag &= ~KI_LOCKBLOCK;
1067 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
1070 if (p->p_state == PRS_NORMAL) { /* approximate. */
1071 if (TD_ON_RUNQ(td) ||
1073 TD_IS_RUNNING(td)) {
1075 } else if (P_SHOULDSTOP(p)) {
1076 kp->ki_stat = SSTOP;
1077 } else if (TD_IS_SLEEPING(td)) {
1078 kp->ki_stat = SSLEEP;
1079 } else if (TD_ON_LOCK(td)) {
1080 kp->ki_stat = SLOCK;
1082 kp->ki_stat = SWAIT;
1084 } else if (p->p_state == PRS_ZOMBIE) {
1085 kp->ki_stat = SZOMB;
1090 /* Things in the thread */
1091 kp->ki_wchan = td->td_wchan;
1092 kp->ki_pri.pri_level = td->td_priority;
1093 kp->ki_pri.pri_native = td->td_base_pri;
1096 * Note: legacy fields; clamp at the old NOCPU value and/or
1097 * the maximum u_char CPU value.
1099 if (td->td_lastcpu == NOCPU)
1100 kp->ki_lastcpu_old = NOCPU_OLD;
1101 else if (td->td_lastcpu > MAXCPU_OLD)
1102 kp->ki_lastcpu_old = MAXCPU_OLD;
1104 kp->ki_lastcpu_old = td->td_lastcpu;
1106 if (td->td_oncpu == NOCPU)
1107 kp->ki_oncpu_old = NOCPU_OLD;
1108 else if (td->td_oncpu > MAXCPU_OLD)
1109 kp->ki_oncpu_old = MAXCPU_OLD;
1111 kp->ki_oncpu_old = td->td_oncpu;
1113 kp->ki_lastcpu = td->td_lastcpu;
1114 kp->ki_oncpu = td->td_oncpu;
1115 kp->ki_tdflags = td->td_flags;
1116 kp->ki_tid = td->td_tid;
1117 kp->ki_numthreads = p->p_numthreads;
1118 kp->ki_pcb = td->td_pcb;
1119 kp->ki_kstack = (void *)td->td_kstack;
1120 kp->ki_slptime = (ticks - td->td_slptick) / hz;
1121 kp->ki_pri.pri_class = td->td_pri_class;
1122 kp->ki_pri.pri_user = td->td_user_pri;
1125 rufetchtd(td, &kp->ki_rusage);
1126 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
1127 kp->ki_pctcpu = sched_pctcpu(td);
1128 kp->ki_estcpu = sched_estcpu(td);
1129 kp->ki_cow = td->td_cow;
1132 /* We can't get this anymore but ps etc never used it anyway. */
1136 kp->ki_siglist = td->td_siglist;
1137 kp->ki_sigmask = td->td_sigmask;
1144 * Fill in a kinfo_proc structure for the specified process.
1145 * Must be called with the target process locked.
1148 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1151 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1153 fill_kinfo_proc_only(p, kp);
1154 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1155 fill_kinfo_aggregate(p, kp);
1162 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1166 * Copy parts of p_stats; zero the rest of p_stats (statistics).
1169 pstats_fork(struct pstats *src, struct pstats *dst)
1172 bzero(&dst->pstat_startzero,
1173 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1174 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1175 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1179 pstats_free(struct pstats *ps)
1182 free(ps, M_SUBPROC);
1185 static struct proc *
1186 zpfind_locked(pid_t pid)
1190 sx_assert(&allproc_lock, SX_LOCKED);
1191 LIST_FOREACH(p, &zombproc, p_list) {
1192 if (p->p_pid == pid) {
1201 * Locate a zombie process by number
1208 sx_slock(&allproc_lock);
1209 p = zpfind_locked(pid);
1210 sx_sunlock(&allproc_lock);
1214 #ifdef COMPAT_FREEBSD32
1217 * This function is typically used to copy out the kernel address, so
1218 * it can be replaced by assignment of zero.
1220 static inline uint32_t
1221 ptr32_trim(void *ptr)
1225 uptr = (uintptr_t)ptr;
1226 return ((uptr > UINT_MAX) ? 0 : uptr);
1229 #define PTRTRIM_CP(src,dst,fld) \
1230 do { (dst).fld = ptr32_trim((src).fld); } while (0)
1233 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1237 bzero(ki32, sizeof(struct kinfo_proc32));
1238 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1239 CP(*ki, *ki32, ki_layout);
1240 PTRTRIM_CP(*ki, *ki32, ki_args);
1241 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1242 PTRTRIM_CP(*ki, *ki32, ki_addr);
1243 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1244 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1245 PTRTRIM_CP(*ki, *ki32, ki_fd);
1246 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1247 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1248 CP(*ki, *ki32, ki_pid);
1249 CP(*ki, *ki32, ki_ppid);
1250 CP(*ki, *ki32, ki_pgid);
1251 CP(*ki, *ki32, ki_tpgid);
1252 CP(*ki, *ki32, ki_sid);
1253 CP(*ki, *ki32, ki_tsid);
1254 CP(*ki, *ki32, ki_jobc);
1255 CP(*ki, *ki32, ki_tdev);
1256 CP(*ki, *ki32, ki_tdev_freebsd11);
1257 CP(*ki, *ki32, ki_siglist);
1258 CP(*ki, *ki32, ki_sigmask);
1259 CP(*ki, *ki32, ki_sigignore);
1260 CP(*ki, *ki32, ki_sigcatch);
1261 CP(*ki, *ki32, ki_uid);
1262 CP(*ki, *ki32, ki_ruid);
1263 CP(*ki, *ki32, ki_svuid);
1264 CP(*ki, *ki32, ki_rgid);
1265 CP(*ki, *ki32, ki_svgid);
1266 CP(*ki, *ki32, ki_ngroups);
1267 for (i = 0; i < KI_NGROUPS; i++)
1268 CP(*ki, *ki32, ki_groups[i]);
1269 CP(*ki, *ki32, ki_size);
1270 CP(*ki, *ki32, ki_rssize);
1271 CP(*ki, *ki32, ki_swrss);
1272 CP(*ki, *ki32, ki_tsize);
1273 CP(*ki, *ki32, ki_dsize);
1274 CP(*ki, *ki32, ki_ssize);
1275 CP(*ki, *ki32, ki_xstat);
1276 CP(*ki, *ki32, ki_acflag);
1277 CP(*ki, *ki32, ki_pctcpu);
1278 CP(*ki, *ki32, ki_estcpu);
1279 CP(*ki, *ki32, ki_slptime);
1280 CP(*ki, *ki32, ki_swtime);
1281 CP(*ki, *ki32, ki_cow);
1282 CP(*ki, *ki32, ki_runtime);
1283 TV_CP(*ki, *ki32, ki_start);
1284 TV_CP(*ki, *ki32, ki_childtime);
1285 CP(*ki, *ki32, ki_flag);
1286 CP(*ki, *ki32, ki_kiflag);
1287 CP(*ki, *ki32, ki_traceflag);
1288 CP(*ki, *ki32, ki_stat);
1289 CP(*ki, *ki32, ki_nice);
1290 CP(*ki, *ki32, ki_lock);
1291 CP(*ki, *ki32, ki_rqindex);
1292 CP(*ki, *ki32, ki_oncpu);
1293 CP(*ki, *ki32, ki_lastcpu);
1295 /* XXX TODO: wrap cpu value as appropriate */
1296 CP(*ki, *ki32, ki_oncpu_old);
1297 CP(*ki, *ki32, ki_lastcpu_old);
1299 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1300 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1301 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1302 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1303 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1304 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1305 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1306 bcopy(ki->ki_moretdname, ki32->ki_moretdname, MAXCOMLEN - TDNAMLEN + 1);
1307 CP(*ki, *ki32, ki_tracer);
1308 CP(*ki, *ki32, ki_flag2);
1309 CP(*ki, *ki32, ki_fibnum);
1310 CP(*ki, *ki32, ki_cr_flags);
1311 CP(*ki, *ki32, ki_jid);
1312 CP(*ki, *ki32, ki_numthreads);
1313 CP(*ki, *ki32, ki_tid);
1314 CP(*ki, *ki32, ki_pri);
1315 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1316 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1317 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1318 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1319 PTRTRIM_CP(*ki, *ki32, ki_udata);
1320 PTRTRIM_CP(*ki, *ki32, ki_tdaddr);
1321 CP(*ki, *ki32, ki_sflag);
1322 CP(*ki, *ki32, ki_tdflags);
1327 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
1330 struct kinfo_proc ki;
1331 #ifdef COMPAT_FREEBSD32
1332 struct kinfo_proc32 ki32;
1336 PROC_LOCK_ASSERT(p, MA_OWNED);
1337 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1340 fill_kinfo_proc(p, &ki);
1341 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1342 #ifdef COMPAT_FREEBSD32
1343 if ((flags & KERN_PROC_MASK32) != 0) {
1344 freebsd32_kinfo_proc_out(&ki, &ki32);
1345 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1349 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1352 FOREACH_THREAD_IN_PROC(p, td) {
1353 fill_kinfo_thread(td, &ki, 1);
1354 #ifdef COMPAT_FREEBSD32
1355 if ((flags & KERN_PROC_MASK32) != 0) {
1356 freebsd32_kinfo_proc_out(&ki, &ki32);
1357 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1361 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1372 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags,
1376 struct kinfo_proc ki;
1382 sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
1383 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1384 error = kern_proc_out(p, &sb, flags);
1385 error2 = sbuf_finish(&sb);
1389 else if (error2 != 0)
1409 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1411 int *name = (int *)arg1;
1412 u_int namelen = arg2;
1414 int flags, doingzomb, oid_number;
1417 oid_number = oidp->oid_number;
1418 if (oid_number != KERN_PROC_ALL &&
1419 (oid_number & KERN_PROC_INC_THREAD) == 0)
1420 flags = KERN_PROC_NOTHREADS;
1423 oid_number &= ~KERN_PROC_INC_THREAD;
1425 #ifdef COMPAT_FREEBSD32
1426 if (req->flags & SCTL_MASK32)
1427 flags |= KERN_PROC_MASK32;
1429 if (oid_number == KERN_PROC_PID) {
1432 error = sysctl_wire_old_buffer(req, 0);
1435 sx_slock(&proctree_lock);
1436 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1438 error = sysctl_out_proc(p, req, flags, 0);
1439 sx_sunlock(&proctree_lock);
1443 switch (oid_number) {
1448 case KERN_PROC_PROC:
1449 if (namelen != 0 && namelen != 1)
1459 /* overestimate by 5 procs */
1460 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1464 error = sysctl_wire_old_buffer(req, 0);
1467 sx_slock(&proctree_lock);
1468 sx_slock(&allproc_lock);
1469 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
1471 p = LIST_FIRST(&allproc);
1473 p = LIST_FIRST(&zombproc);
1474 for (; p != NULL; p = LIST_NEXT(p, p_list)) {
1476 * Skip embryonic processes.
1479 if (p->p_state == PRS_NEW) {
1483 KASSERT(p->p_ucred != NULL,
1484 ("process credential is NULL for non-NEW proc"));
1486 * Show a user only appropriate processes.
1488 if (p_cansee(curthread, p)) {
1493 * TODO - make more efficient (see notes below).
1496 switch (oid_number) {
1499 if (p->p_ucred->cr_gid != (gid_t)name[0]) {
1505 case KERN_PROC_PGRP:
1506 /* could do this by traversing pgrp */
1507 if (p->p_pgrp == NULL ||
1508 p->p_pgrp->pg_id != (pid_t)name[0]) {
1514 case KERN_PROC_RGID:
1515 if (p->p_ucred->cr_rgid != (gid_t)name[0]) {
1521 case KERN_PROC_SESSION:
1522 if (p->p_session == NULL ||
1523 p->p_session->s_sid != (pid_t)name[0]) {
1530 if ((p->p_flag & P_CONTROLT) == 0 ||
1531 p->p_session == NULL) {
1535 /* XXX proctree_lock */
1536 SESS_LOCK(p->p_session);
1537 if (p->p_session->s_ttyp == NULL ||
1538 tty_udev(p->p_session->s_ttyp) !=
1540 SESS_UNLOCK(p->p_session);
1544 SESS_UNLOCK(p->p_session);
1548 if (p->p_ucred->cr_uid != (uid_t)name[0]) {
1554 case KERN_PROC_RUID:
1555 if (p->p_ucred->cr_ruid != (uid_t)name[0]) {
1561 case KERN_PROC_PROC:
1569 error = sysctl_out_proc(p, req, flags, doingzomb);
1571 sx_sunlock(&allproc_lock);
1572 sx_sunlock(&proctree_lock);
1577 sx_sunlock(&allproc_lock);
1578 sx_sunlock(&proctree_lock);
1583 pargs_alloc(int len)
1587 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1589 refcount_init(&pa->ar_ref, 1);
1590 pa->ar_length = len;
1595 pargs_free(struct pargs *pa)
1602 pargs_hold(struct pargs *pa)
1607 refcount_acquire(&pa->ar_ref);
1611 pargs_drop(struct pargs *pa)
1616 if (refcount_release(&pa->ar_ref))
1621 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1627 * This may return a short read if the string is shorter than the chunk
1628 * and is aligned at the end of the page, and the following page is not
1631 n = proc_readmem(td, p, (vm_offset_t)sptr, buf, len);
1637 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1639 enum proc_vector_type {
1645 #ifdef COMPAT_FREEBSD32
1647 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1648 size_t *vsizep, enum proc_vector_type type)
1650 struct freebsd32_ps_strings pss;
1652 vm_offset_t vptr, ptr;
1653 uint32_t *proc_vector32;
1659 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1660 sizeof(pss)) != sizeof(pss))
1664 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1665 vsize = pss.ps_nargvstr;
1666 if (vsize > ARG_MAX)
1668 size = vsize * sizeof(int32_t);
1671 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1672 vsize = pss.ps_nenvstr;
1673 if (vsize > ARG_MAX)
1675 size = vsize * sizeof(int32_t);
1678 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1679 (pss.ps_nenvstr + 1) * sizeof(int32_t);
1682 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1683 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1686 if (aux.a_type == AT_NULL)
1690 if (aux.a_type != AT_NULL)
1693 size = vsize * sizeof(aux);
1696 KASSERT(0, ("Wrong proc vector type: %d", type));
1699 proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1700 if (proc_readmem(td, p, vptr, proc_vector32, size) != size) {
1704 if (type == PROC_AUX) {
1705 *proc_vectorp = (char **)proc_vector32;
1709 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1710 for (i = 0; i < (int)vsize; i++)
1711 proc_vector[i] = PTRIN(proc_vector32[i]);
1712 *proc_vectorp = proc_vector;
1715 free(proc_vector32, M_TEMP);
1721 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1722 size_t *vsizep, enum proc_vector_type type)
1724 struct ps_strings pss;
1726 vm_offset_t vptr, ptr;
1731 #ifdef COMPAT_FREEBSD32
1732 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1733 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1735 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1736 sizeof(pss)) != sizeof(pss))
1740 vptr = (vm_offset_t)pss.ps_argvstr;
1741 vsize = pss.ps_nargvstr;
1742 if (vsize > ARG_MAX)
1744 size = vsize * sizeof(char *);
1747 vptr = (vm_offset_t)pss.ps_envstr;
1748 vsize = pss.ps_nenvstr;
1749 if (vsize > ARG_MAX)
1751 size = vsize * sizeof(char *);
1755 * The aux array is just above env array on the stack. Check
1756 * that the address is naturally aligned.
1758 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1760 #if __ELF_WORD_SIZE == 64
1761 if (vptr % sizeof(uint64_t) != 0)
1763 if (vptr % sizeof(uint32_t) != 0)
1767 * We count the array size reading the aux vectors from the
1768 * stack until AT_NULL vector is returned. So (to keep the code
1769 * simple) we read the process stack twice: the first time here
1770 * to find the size and the second time when copying the vectors
1771 * to the allocated proc_vector.
1773 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1774 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1777 if (aux.a_type == AT_NULL)
1782 * If the PROC_AUXV_MAX entries are iterated over, and we have
1783 * not reached AT_NULL, it is most likely we are reading wrong
1784 * data: either the process doesn't have auxv array or data has
1785 * been modified. Return the error in this case.
1787 if (aux.a_type != AT_NULL)
1790 size = vsize * sizeof(aux);
1793 KASSERT(0, ("Wrong proc vector type: %d", type));
1794 return (EINVAL); /* In case we are built without INVARIANTS. */
1796 proc_vector = malloc(size, M_TEMP, M_WAITOK);
1797 if (proc_readmem(td, p, vptr, proc_vector, size) != size) {
1798 free(proc_vector, M_TEMP);
1801 *proc_vectorp = proc_vector;
1807 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
1810 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
1811 enum proc_vector_type type)
1813 size_t done, len, nchr, vsize;
1815 char **proc_vector, *sptr;
1816 char pss_string[GET_PS_STRINGS_CHUNK_SZ];
1818 PROC_ASSERT_HELD(p);
1821 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
1823 nchr = 2 * (PATH_MAX + ARG_MAX);
1825 error = get_proc_vector(td, p, &proc_vector, &vsize, type);
1828 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
1830 * The program may have scribbled into its argv array, e.g. to
1831 * remove some arguments. If that has happened, break out
1832 * before trying to read from NULL.
1834 if (proc_vector[i] == NULL)
1836 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
1837 error = proc_read_string(td, p, sptr, pss_string,
1838 sizeof(pss_string));
1841 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
1842 if (done + len >= nchr)
1843 len = nchr - done - 1;
1844 sbuf_bcat(sb, pss_string, len);
1845 if (len != GET_PS_STRINGS_CHUNK_SZ)
1847 done += GET_PS_STRINGS_CHUNK_SZ;
1849 sbuf_bcat(sb, "", 1);
1853 free(proc_vector, M_TEMP);
1858 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
1861 return (get_ps_strings(curthread, p, sb, PROC_ARG));
1865 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
1868 return (get_ps_strings(curthread, p, sb, PROC_ENV));
1872 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
1878 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
1880 #ifdef COMPAT_FREEBSD32
1881 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1882 size = vsize * sizeof(Elf32_Auxinfo);
1885 size = vsize * sizeof(Elf_Auxinfo);
1886 if (sbuf_bcat(sb, auxv, size) != 0)
1894 * This sysctl allows a process to retrieve the argument list or process
1895 * title for another process without groping around in the address space
1896 * of the other process. It also allow a process to set its own "process
1897 * title to a string of its own choice.
1900 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1902 int *name = (int *)arg1;
1903 u_int namelen = arg2;
1904 struct pargs *newpa, *pa;
1907 int flags, error = 0, error2;
1912 flags = PGET_CANSEE;
1913 if (req->newptr != NULL)
1914 flags |= PGET_ISCURRENT;
1915 error = pget((pid_t)name[0], flags, &p);
1923 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1925 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
1928 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1929 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1930 error = proc_getargv(curthread, p, &sb);
1931 error2 = sbuf_finish(&sb);
1934 if (error == 0 && error2 != 0)
1939 if (error != 0 || req->newptr == NULL)
1942 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
1944 newpa = pargs_alloc(req->newlen);
1945 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
1959 * This sysctl allows a process to retrieve environment of another process.
1962 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
1964 int *name = (int *)arg1;
1965 u_int namelen = arg2;
1973 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1976 if ((p->p_flag & P_SYSTEM) != 0) {
1981 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1982 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1983 error = proc_getenvv(curthread, p, &sb);
1984 error2 = sbuf_finish(&sb);
1987 return (error != 0 ? error : error2);
1991 * This sysctl allows a process to retrieve ELF auxiliary vector of
1995 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
1997 int *name = (int *)arg1;
1998 u_int namelen = arg2;
2006 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2009 if ((p->p_flag & P_SYSTEM) != 0) {
2013 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2014 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2015 error = proc_getauxv(curthread, p, &sb);
2016 error2 = sbuf_finish(&sb);
2019 return (error != 0 ? error : error2);
2023 * This sysctl allows a process to retrieve the path of the executable for
2024 * itself or another process.
2027 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
2029 pid_t *pidp = (pid_t *)arg1;
2030 unsigned int arglen = arg2;
2033 char *retbuf, *freebuf;
2038 if (*pidp == -1) { /* -1 means this process */
2039 p = req->td->td_proc;
2041 error = pget(*pidp, PGET_CANSEE, &p);
2055 error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
2059 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
2060 free(freebuf, M_TEMP);
2065 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
2078 error = pget((pid_t)name[0], PGET_CANSEE, &p);
2081 sv_name = p->p_sysent->sv_name;
2083 return (sysctl_handle_string(oidp, sv_name, 0, req));
2086 #ifdef KINFO_OVMENTRY_SIZE
2087 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
2090 #ifdef COMPAT_FREEBSD7
2092 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2094 vm_map_entry_t entry, tmp_entry;
2095 unsigned int last_timestamp;
2096 char *fullpath, *freepath;
2097 struct kinfo_ovmentry *kve;
2107 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2110 vm = vmspace_acquire_ref(p);
2115 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2118 vm_map_lock_read(map);
2119 for (entry = map->header.next; entry != &map->header;
2120 entry = entry->next) {
2121 vm_object_t obj, tobj, lobj;
2124 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2127 bzero(kve, sizeof(*kve));
2128 kve->kve_structsize = sizeof(*kve);
2130 kve->kve_private_resident = 0;
2131 obj = entry->object.vm_object;
2133 VM_OBJECT_RLOCK(obj);
2134 if (obj->shadow_count == 1)
2135 kve->kve_private_resident =
2136 obj->resident_page_count;
2138 kve->kve_resident = 0;
2139 addr = entry->start;
2140 while (addr < entry->end) {
2141 if (pmap_extract(map->pmap, addr))
2142 kve->kve_resident++;
2146 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2148 VM_OBJECT_RLOCK(tobj);
2150 VM_OBJECT_RUNLOCK(lobj);
2154 kve->kve_start = (void*)entry->start;
2155 kve->kve_end = (void*)entry->end;
2156 kve->kve_offset = (off_t)entry->offset;
2158 if (entry->protection & VM_PROT_READ)
2159 kve->kve_protection |= KVME_PROT_READ;
2160 if (entry->protection & VM_PROT_WRITE)
2161 kve->kve_protection |= KVME_PROT_WRITE;
2162 if (entry->protection & VM_PROT_EXECUTE)
2163 kve->kve_protection |= KVME_PROT_EXEC;
2165 if (entry->eflags & MAP_ENTRY_COW)
2166 kve->kve_flags |= KVME_FLAG_COW;
2167 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2168 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2169 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2170 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2172 last_timestamp = map->timestamp;
2173 vm_map_unlock_read(map);
2175 kve->kve_fileid = 0;
2181 switch (lobj->type) {
2183 kve->kve_type = KVME_TYPE_DEFAULT;
2186 kve->kve_type = KVME_TYPE_VNODE;
2191 if ((lobj->flags & OBJ_TMPFS_NODE) != 0) {
2192 kve->kve_type = KVME_TYPE_VNODE;
2193 if ((lobj->flags & OBJ_TMPFS) != 0) {
2194 vp = lobj->un_pager.swp.swp_tmpfs;
2198 kve->kve_type = KVME_TYPE_SWAP;
2202 kve->kve_type = KVME_TYPE_DEVICE;
2205 kve->kve_type = KVME_TYPE_PHYS;
2208 kve->kve_type = KVME_TYPE_DEAD;
2211 kve->kve_type = KVME_TYPE_SG;
2214 kve->kve_type = KVME_TYPE_UNKNOWN;
2218 VM_OBJECT_RUNLOCK(lobj);
2220 kve->kve_ref_count = obj->ref_count;
2221 kve->kve_shadow_count = obj->shadow_count;
2222 VM_OBJECT_RUNLOCK(obj);
2224 vn_fullpath(curthread, vp, &fullpath,
2226 cred = curthread->td_ucred;
2227 vn_lock(vp, LK_SHARED | LK_RETRY);
2228 if (VOP_GETATTR(vp, &va, cred) == 0) {
2229 kve->kve_fileid = va.va_fileid;
2231 kve->kve_fsid = va.va_fsid;
2236 kve->kve_type = KVME_TYPE_NONE;
2237 kve->kve_ref_count = 0;
2238 kve->kve_shadow_count = 0;
2241 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2242 if (freepath != NULL)
2243 free(freepath, M_TEMP);
2245 error = SYSCTL_OUT(req, kve, sizeof(*kve));
2246 vm_map_lock_read(map);
2249 if (last_timestamp != map->timestamp) {
2250 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2254 vm_map_unlock_read(map);
2260 #endif /* COMPAT_FREEBSD7 */
2262 #ifdef KINFO_VMENTRY_SIZE
2263 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2267 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2268 struct kinfo_vmentry *kve)
2270 vm_object_t obj, tobj;
2273 vm_paddr_t locked_pa;
2274 vm_pindex_t pi, pi_adv, pindex;
2277 obj = entry->object.vm_object;
2278 addr = entry->start;
2280 pi = OFF_TO_IDX(entry->offset);
2281 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2282 if (m_adv != NULL) {
2285 pi_adv = atop(entry->end - addr);
2287 for (tobj = obj;; tobj = tobj->backing_object) {
2288 m = vm_page_find_least(tobj, pindex);
2290 if (m->pindex == pindex)
2292 if (pi_adv > m->pindex - pindex) {
2293 pi_adv = m->pindex - pindex;
2297 if (tobj->backing_object == NULL)
2299 pindex += OFF_TO_IDX(tobj->
2300 backing_object_offset);
2304 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2305 (addr & (pagesizes[1] - 1)) == 0 &&
2306 (pmap_mincore(map->pmap, addr, &locked_pa) &
2307 MINCORE_SUPER) != 0) {
2308 kve->kve_flags |= KVME_FLAG_SUPER;
2309 pi_adv = atop(pagesizes[1]);
2312 * We do not test the found page on validity.
2313 * Either the page is busy and being paged in,
2314 * or it was invalidated. The first case
2315 * should be counted as resident, the second
2316 * is not so clear; we do account both.
2320 kve->kve_resident += pi_adv;
2323 PA_UNLOCK_COND(locked_pa);
2327 * Must be called with the process locked and will return unlocked.
2330 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
2332 vm_map_entry_t entry, tmp_entry;
2335 vm_object_t obj, tobj, lobj;
2336 char *fullpath, *freepath;
2337 struct kinfo_vmentry *kve;
2342 unsigned int last_timestamp;
2345 PROC_LOCK_ASSERT(p, MA_OWNED);
2349 vm = vmspace_acquire_ref(p);
2354 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK | M_ZERO);
2358 vm_map_lock_read(map);
2359 for (entry = map->header.next; entry != &map->header;
2360 entry = entry->next) {
2361 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2365 bzero(kve, sizeof(*kve));
2366 obj = entry->object.vm_object;
2368 for (tobj = obj; tobj != NULL;
2369 tobj = tobj->backing_object) {
2370 VM_OBJECT_RLOCK(tobj);
2373 if (obj->backing_object == NULL)
2374 kve->kve_private_resident =
2375 obj->resident_page_count;
2376 if (!vmmap_skip_res_cnt)
2377 kern_proc_vmmap_resident(map, entry, kve);
2378 for (tobj = obj; tobj != NULL;
2379 tobj = tobj->backing_object) {
2380 if (tobj != obj && tobj != lobj)
2381 VM_OBJECT_RUNLOCK(tobj);
2387 kve->kve_start = entry->start;
2388 kve->kve_end = entry->end;
2389 kve->kve_offset = entry->offset;
2391 if (entry->protection & VM_PROT_READ)
2392 kve->kve_protection |= KVME_PROT_READ;
2393 if (entry->protection & VM_PROT_WRITE)
2394 kve->kve_protection |= KVME_PROT_WRITE;
2395 if (entry->protection & VM_PROT_EXECUTE)
2396 kve->kve_protection |= KVME_PROT_EXEC;
2398 if (entry->eflags & MAP_ENTRY_COW)
2399 kve->kve_flags |= KVME_FLAG_COW;
2400 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2401 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2402 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2403 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2404 if (entry->eflags & MAP_ENTRY_GROWS_UP)
2405 kve->kve_flags |= KVME_FLAG_GROWS_UP;
2406 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2407 kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2409 last_timestamp = map->timestamp;
2410 vm_map_unlock_read(map);
2416 switch (lobj->type) {
2418 kve->kve_type = KVME_TYPE_DEFAULT;
2421 kve->kve_type = KVME_TYPE_VNODE;
2426 if ((lobj->flags & OBJ_TMPFS_NODE) != 0) {
2427 kve->kve_type = KVME_TYPE_VNODE;
2428 if ((lobj->flags & OBJ_TMPFS) != 0) {
2429 vp = lobj->un_pager.swp.swp_tmpfs;
2433 kve->kve_type = KVME_TYPE_SWAP;
2437 kve->kve_type = KVME_TYPE_DEVICE;
2440 kve->kve_type = KVME_TYPE_PHYS;
2443 kve->kve_type = KVME_TYPE_DEAD;
2446 kve->kve_type = KVME_TYPE_SG;
2448 case OBJT_MGTDEVICE:
2449 kve->kve_type = KVME_TYPE_MGTDEVICE;
2452 kve->kve_type = KVME_TYPE_UNKNOWN;
2456 VM_OBJECT_RUNLOCK(lobj);
2458 kve->kve_ref_count = obj->ref_count;
2459 kve->kve_shadow_count = obj->shadow_count;
2460 VM_OBJECT_RUNLOCK(obj);
2462 vn_fullpath(curthread, vp, &fullpath,
2464 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2465 cred = curthread->td_ucred;
2466 vn_lock(vp, LK_SHARED | LK_RETRY);
2467 if (VOP_GETATTR(vp, &va, cred) == 0) {
2468 kve->kve_vn_fileid = va.va_fileid;
2469 kve->kve_vn_fsid = va.va_fsid;
2470 kve->kve_vn_fsid_freebsd11 =
2471 kve->kve_vn_fsid; /* truncate */
2473 MAKEIMODE(va.va_type, va.va_mode);
2474 kve->kve_vn_size = va.va_size;
2475 kve->kve_vn_rdev = va.va_rdev;
2476 kve->kve_vn_rdev_freebsd11 =
2477 kve->kve_vn_rdev; /* truncate */
2478 kve->kve_status = KF_ATTR_VALID;
2483 kve->kve_type = KVME_TYPE_NONE;
2484 kve->kve_ref_count = 0;
2485 kve->kve_shadow_count = 0;
2488 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2489 if (freepath != NULL)
2490 free(freepath, M_TEMP);
2492 /* Pack record size down */
2493 if ((flags & KERN_VMMAP_PACK_KINFO) != 0)
2494 kve->kve_structsize =
2495 offsetof(struct kinfo_vmentry, kve_path) +
2496 strlen(kve->kve_path) + 1;
2498 kve->kve_structsize = sizeof(*kve);
2499 kve->kve_structsize = roundup(kve->kve_structsize,
2502 /* Halt filling and truncate rather than exceeding maxlen */
2503 if (maxlen != -1 && maxlen < kve->kve_structsize) {
2505 vm_map_lock_read(map);
2507 } else if (maxlen != -1)
2508 maxlen -= kve->kve_structsize;
2510 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2512 vm_map_lock_read(map);
2515 if (last_timestamp != map->timestamp) {
2516 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2520 vm_map_unlock_read(map);
2528 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2532 int error, error2, *name;
2535 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2536 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2537 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2542 error = kern_proc_vmmap_out(p, &sb, -1, KERN_VMMAP_PACK_KINFO);
2543 error2 = sbuf_finish(&sb);
2545 return (error != 0 ? error : error2);
2548 #if defined(STACK) || defined(DDB)
2550 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2552 struct kinfo_kstack *kkstp;
2553 int error, i, *name, numthreads;
2554 lwpid_t *lwpidarray;
2561 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2565 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2566 st = stack_create(M_WAITOK);
2571 if (lwpidarray != NULL) {
2572 free(lwpidarray, M_TEMP);
2575 numthreads = p->p_numthreads;
2577 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2580 } while (numthreads < p->p_numthreads);
2583 * XXXRW: During the below loop, execve(2) and countless other sorts
2584 * of changes could have taken place. Should we check to see if the
2585 * vmspace has been replaced, or the like, in order to prevent
2586 * giving a snapshot that spans, say, execve(2), with some threads
2587 * before and some after? Among other things, the credentials could
2588 * have changed, in which case the right to extract debug info might
2589 * no longer be assured.
2592 FOREACH_THREAD_IN_PROC(p, td) {
2593 KASSERT(i < numthreads,
2594 ("sysctl_kern_proc_kstack: numthreads"));
2595 lwpidarray[i] = td->td_tid;
2599 for (i = 0; i < numthreads; i++) {
2600 td = thread_find(p, lwpidarray[i]);
2604 bzero(kkstp, sizeof(*kkstp));
2605 (void)sbuf_new(&sb, kkstp->kkst_trace,
2606 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2608 kkstp->kkst_tid = td->td_tid;
2609 if (TD_IS_SWAPPED(td)) {
2610 kkstp->kkst_state = KKST_STATE_SWAPPED;
2611 } else if (TD_IS_RUNNING(td)) {
2612 if (stack_save_td_running(st, td) == 0)
2613 kkstp->kkst_state = KKST_STATE_STACKOK;
2615 kkstp->kkst_state = KKST_STATE_RUNNING;
2617 kkstp->kkst_state = KKST_STATE_STACKOK;
2618 stack_save_td(st, td);
2622 stack_sbuf_print(&sb, st);
2625 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2632 if (lwpidarray != NULL)
2633 free(lwpidarray, M_TEMP);
2635 free(kkstp, M_TEMP);
2641 * This sysctl allows a process to retrieve the full list of groups from
2642 * itself or another process.
2645 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2647 pid_t *pidp = (pid_t *)arg1;
2648 unsigned int arglen = arg2;
2655 if (*pidp == -1) { /* -1 means this process */
2656 p = req->td->td_proc;
2659 error = pget(*pidp, PGET_CANSEE, &p);
2664 cred = crhold(p->p_ucred);
2667 error = SYSCTL_OUT(req, cred->cr_groups,
2668 cred->cr_ngroups * sizeof(gid_t));
2674 * This sysctl allows a process to retrieve or/and set the resource limit for
2678 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2680 int *name = (int *)arg1;
2681 u_int namelen = arg2;
2690 which = (u_int)name[1];
2691 if (which >= RLIM_NLIMITS)
2694 if (req->newptr != NULL && req->newlen != sizeof(rlim))
2697 flags = PGET_HOLD | PGET_NOTWEXIT;
2698 if (req->newptr != NULL)
2699 flags |= PGET_CANDEBUG;
2701 flags |= PGET_CANSEE;
2702 error = pget((pid_t)name[0], flags, &p);
2709 if (req->oldptr != NULL) {
2711 lim_rlimit_proc(p, which, &rlim);
2714 error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
2721 if (req->newptr != NULL) {
2722 error = SYSCTL_IN(req, &rlim, sizeof(rlim));
2724 error = kern_proc_setrlimit(curthread, p, which, &rlim);
2733 * This sysctl allows a process to retrieve ps_strings structure location of
2737 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2739 int *name = (int *)arg1;
2740 u_int namelen = arg2;
2742 vm_offset_t ps_strings;
2744 #ifdef COMPAT_FREEBSD32
2745 uint32_t ps_strings32;
2751 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2754 #ifdef COMPAT_FREEBSD32
2755 if ((req->flags & SCTL_MASK32) != 0) {
2757 * We return 0 if the 32 bit emulation request is for a 64 bit
2760 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
2761 PTROUT(p->p_sysent->sv_psstrings) : 0;
2763 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
2767 ps_strings = p->p_sysent->sv_psstrings;
2769 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
2774 * This sysctl allows a process to retrieve umask of another process.
2777 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
2779 int *name = (int *)arg1;
2780 u_int namelen = arg2;
2789 pid = (pid_t)name[0];
2791 if (pid == p->p_pid || pid == 0) {
2792 fd_cmask = p->p_fd->fd_cmask;
2796 error = pget(pid, PGET_WANTREAD, &p);
2800 fd_cmask = p->p_fd->fd_cmask;
2803 error = SYSCTL_OUT(req, &fd_cmask, sizeof(fd_cmask));
2808 * This sysctl allows a process to set and retrieve binary osreldate of
2812 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
2814 int *name = (int *)arg1;
2815 u_int namelen = arg2;
2817 int flags, error, osrel;
2822 if (req->newptr != NULL && req->newlen != sizeof(osrel))
2825 flags = PGET_HOLD | PGET_NOTWEXIT;
2826 if (req->newptr != NULL)
2827 flags |= PGET_CANDEBUG;
2829 flags |= PGET_CANSEE;
2830 error = pget((pid_t)name[0], flags, &p);
2834 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
2838 if (req->newptr != NULL) {
2839 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
2854 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
2856 int *name = (int *)arg1;
2857 u_int namelen = arg2;
2859 struct kinfo_sigtramp kst;
2860 const struct sysentvec *sv;
2862 #ifdef COMPAT_FREEBSD32
2863 struct kinfo_sigtramp32 kst32;
2869 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2873 #ifdef COMPAT_FREEBSD32
2874 if ((req->flags & SCTL_MASK32) != 0) {
2875 bzero(&kst32, sizeof(kst32));
2876 if (SV_PROC_FLAG(p, SV_ILP32)) {
2877 if (sv->sv_sigcode_base != 0) {
2878 kst32.ksigtramp_start = sv->sv_sigcode_base;
2879 kst32.ksigtramp_end = sv->sv_sigcode_base +
2882 kst32.ksigtramp_start = sv->sv_psstrings -
2884 kst32.ksigtramp_end = sv->sv_psstrings;
2888 error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
2892 bzero(&kst, sizeof(kst));
2893 if (sv->sv_sigcode_base != 0) {
2894 kst.ksigtramp_start = (char *)sv->sv_sigcode_base;
2895 kst.ksigtramp_end = (char *)sv->sv_sigcode_base +
2898 kst.ksigtramp_start = (char *)sv->sv_psstrings -
2900 kst.ksigtramp_end = (char *)sv->sv_psstrings;
2903 error = SYSCTL_OUT(req, &kst, sizeof(kst));
2907 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
2909 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
2910 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
2911 "Return entire process table");
2913 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2914 sysctl_kern_proc, "Process table");
2916 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
2917 sysctl_kern_proc, "Process table");
2919 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2920 sysctl_kern_proc, "Process table");
2922 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
2923 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2925 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
2926 sysctl_kern_proc, "Process table");
2928 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2929 sysctl_kern_proc, "Process table");
2931 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2932 sysctl_kern_proc, "Process table");
2934 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2935 sysctl_kern_proc, "Process table");
2937 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
2938 sysctl_kern_proc, "Return process table, no threads");
2940 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
2941 CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
2942 sysctl_kern_proc_args, "Process argument list");
2944 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
2945 sysctl_kern_proc_env, "Process environment");
2947 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
2948 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
2950 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
2951 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
2953 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
2954 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
2955 "Process syscall vector name (ABI type)");
2957 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
2958 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2960 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
2961 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2963 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
2964 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2966 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
2967 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2969 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
2970 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2972 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
2973 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2975 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
2976 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2978 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
2979 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2981 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
2982 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
2983 "Return process table, no threads");
2985 #ifdef COMPAT_FREEBSD7
2986 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
2987 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
2990 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
2991 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
2993 #if defined(STACK) || defined(DDB)
2994 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
2995 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
2998 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
2999 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
3001 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
3002 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
3003 "Process resource limits");
3005 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
3006 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
3007 "Process ps_strings location");
3009 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
3010 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
3012 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
3013 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
3014 "Process binary osreldate");
3016 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
3017 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
3018 "Process signal trampoline location");
3023 * stop_all_proc() purpose is to stop all process which have usermode,
3024 * except current process for obvious reasons. This makes it somewhat
3025 * unreliable when invoked from multithreaded process. The service
3026 * must not be user-callable anyway.
3031 struct proc *cp, *p;
3033 bool restart, seen_stopped, seen_exiting, stopped_some;
3037 sx_xlock(&allproc_lock);
3039 seen_exiting = seen_stopped = stopped_some = restart = false;
3040 LIST_REMOVE(cp, p_list);
3041 LIST_INSERT_HEAD(&allproc, cp, p_list);
3043 p = LIST_NEXT(cp, p_list);
3046 LIST_REMOVE(cp, p_list);
3047 LIST_INSERT_AFTER(p, cp, p_list);
3049 if ((p->p_flag & (P_KPROC | P_SYSTEM | P_TOTAL_STOP)) != 0) {
3053 if ((p->p_flag & P_WEXIT) != 0) {
3054 seen_exiting = true;
3058 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
3060 * Stopped processes are tolerated when there
3061 * are no other processes which might continue
3062 * them. P_STOPPED_SINGLE but not
3063 * P_TOTAL_STOP process still has at least one
3066 seen_stopped = true;
3071 sx_xunlock(&allproc_lock);
3072 r = thread_single(p, SINGLE_ALLPROC);
3076 stopped_some = true;
3079 sx_xlock(&allproc_lock);
3081 /* Catch forked children we did not see in iteration. */
3082 if (gen != allproc_gen)
3084 sx_xunlock(&allproc_lock);
3085 if (restart || stopped_some || seen_exiting || seen_stopped) {
3086 kern_yield(PRI_USER);
3092 resume_all_proc(void)
3094 struct proc *cp, *p;
3097 sx_xlock(&allproc_lock);
3098 LIST_REMOVE(cp, p_list);
3099 LIST_INSERT_HEAD(&allproc, cp, p_list);
3101 p = LIST_NEXT(cp, p_list);
3104 LIST_REMOVE(cp, p_list);
3105 LIST_INSERT_AFTER(p, cp, p_list);
3107 if ((p->p_flag & P_TOTAL_STOP) != 0) {
3108 sx_xunlock(&allproc_lock);
3110 thread_single_end(p, SINGLE_ALLPROC);
3113 sx_xlock(&allproc_lock);
3118 sx_xunlock(&allproc_lock);
3121 /* #define TOTAL_STOP_DEBUG 1 */
3122 #ifdef TOTAL_STOP_DEBUG
3123 volatile static int ap_resume;
3124 #include <sys/mount.h>
3127 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
3133 error = sysctl_handle_int(oidp, &val, 0, req);
3134 if (error != 0 || req->newptr == NULL)
3139 while (ap_resume == 0)
3147 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
3148 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
3149 sysctl_debug_stop_all_proc, "I",