xref: /dragonfly/sys/kern/kern_kinfo.c (revision b7367ef6)
1 /*-
2  * Copyright (c) 2007 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Simon 'corecode' Schubert <corecode@fs.ei.tum.de>
6  * by Thomas E. Spanjaard <tgen@netphreax.net>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * $DragonFly: src/sys/kern/kern_kinfo.c,v 1.15 2007/10/17 23:30:31 dillon Exp $
36  */
37 
38 /*
39  * This is a source file used by both the kernel and libkvm.
40  */
41 
42 #ifndef _KERNEL
43 #define _KERNEL_STRUCTURES
44 #endif
45 
46 #include <sys/proc.h>
47 #include <vm/vm_map.h>
48 #include <sys/kinfo.h>
49 #include <sys/tty.h>
50 #include <sys/conf.h>
51 #include <sys/jail.h>
52 #include <sys/globaldata.h>
53 #ifdef _KERNEL
54 #include <sys/systm.h>
55 #else
56 #include <string.h>
57 #endif
58 
59 
60 /*
61  * Fill in a struct kinfo_proc.
62  */
63 void
64 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
65 {
66 	struct session *sess;
67 	struct pgrp *pgrp;
68 
69 	/*
70 	 * NOTE: pgrp and sess can wind up NULL if we caught a process
71 	 * in the middle of exiting.
72 	 */
73 	pgrp = p->p_pgrp;
74 	sess = pgrp ? pgrp->pg_session : NULL;
75 
76 	bzero(kp, sizeof(*kp));
77 
78 	kp->kp_paddr = (uintptr_t)p;
79 	kp->kp_fd = (uintptr_t)p->p_fd;
80 
81 	kp->kp_flags = p->p_flag;
82 	kp->kp_stat = p->p_stat;
83 	kp->kp_lock = p->p_lock;
84 	kp->kp_acflag = p->p_acflag;
85 	kp->kp_traceflag = p->p_traceflag;
86 	kp->kp_siglist = p->p_siglist;
87 	kp->kp_sigignore = p->p_sigignore;
88 	kp->kp_sigcatch = p->p_sigcatch;
89 	kp->kp_sigflag = p->p_sigacts->ps_flag;
90 	kp->kp_start = p->p_start;
91 
92 	strncpy(kp->kp_comm, p->p_comm, sizeof(kp->kp_comm) - 1);
93 	kp->kp_comm[sizeof(kp->kp_comm) - 1] = 0;
94 
95 	kp->kp_uid = p->p_ucred->cr_uid;
96 	kp->kp_ngroups = p->p_ucred->cr_ngroups;
97 	bcopy(p->p_ucred->cr_groups, kp->kp_groups,
98 	      NGROUPS * sizeof(kp->kp_groups[0]));
99 	kp->kp_ruid = p->p_ucred->cr_ruid;
100 	kp->kp_svuid = p->p_ucred->cr_svuid;
101 	kp->kp_rgid = p->p_ucred->cr_rgid;
102 	kp->kp_svgid = p->p_ucred->cr_svgid;
103 
104 	kp->kp_pid = p->p_pid;
105 	if (p->p_oppid != 0)
106 		kp->kp_ppid = p->p_oppid;
107 	else
108 		kp->kp_ppid = p->p_pptr != NULL ? p->p_pptr->p_pid : -1;
109 	if (pgrp) {
110 		kp->kp_pgid = pgrp->pg_id;
111 		kp->kp_jobc = pgrp->pg_jobc;
112 	}
113 	if (sess) {
114 		kp->kp_sid = sess->s_sid;
115 		bcopy(sess->s_login, kp->kp_login, MAXLOGNAME);
116 		if (sess->s_ttyvp != NULL)
117 			kp->kp_auxflags |= KI_CTTY;
118 		if (SESS_LEADER(p))
119 			kp->kp_auxflags |= KI_SLEADER;
120 	}
121 	if (sess && (p->p_flag & P_CONTROLT) != 0 && sess->s_ttyp != NULL) {
122 		kp->kp_tdev = dev2udev(sess->s_ttyp->t_dev);
123 		if (sess->s_ttyp->t_pgrp != NULL)
124 			kp->kp_tpgid = sess->s_ttyp->t_pgrp->pg_id;
125 		else
126 			kp->kp_tpgid = -1;
127 		if (sess->s_ttyp->t_session != NULL)
128 			kp->kp_tsid = sess->s_ttyp->t_session->s_sid;
129 		else
130 			kp->kp_tsid = -1;
131 	} else {
132 		kp->kp_tdev = NOUDEV;
133 	}
134 	kp->kp_exitstat = p->p_xstat;
135 	kp->kp_nthreads = p->p_nthreads;
136 	kp->kp_nice = p->p_nice;
137 	kp->kp_swtime = p->p_swtime;
138 
139 	kp->kp_vm_map_size = p->p_vmspace->vm_map.size;
140 	kp->kp_vm_rssize = vmspace_resident_count(p->p_vmspace);
141 	kp->kp_vm_swrss = p->p_vmspace->vm_swrss;
142 	kp->kp_vm_tsize = p->p_vmspace->vm_tsize;
143 	kp->kp_vm_dsize = p->p_vmspace->vm_dsize;
144 	kp->kp_vm_ssize = p->p_vmspace->vm_ssize;
145 
146 	if (jailed(p->p_ucred))
147 		kp->kp_jailid = p->p_ucred->cr_prison->pr_id;
148 
149 	kp->kp_ru = p->p_ru;
150 	kp->kp_ru = p->p_cru;
151 }
152 
153 /*
154  * Fill in a struct kinfo_lwp.
155  */
156 void
157 fill_kinfo_lwp(struct lwp *lwp, struct kinfo_lwp *kl)
158 {
159 	bzero(kl, sizeof(*kl));
160 
161 	kl->kl_pid = lwp->lwp_proc->p_pid;
162 	kl->kl_tid = lwp->lwp_tid;
163 
164 	kl->kl_flags = lwp->lwp_flag;
165 	kl->kl_stat = lwp->lwp_stat;
166 	kl->kl_lock = lwp->lwp_lock;
167 	kl->kl_tdflags = lwp->lwp_thread->td_flags;
168 
169 	/*
170 	 * The process/lwp stat may not reflect whether the process is
171 	 * actually sleeping or not if the related thread was directly
172 	 * descheduled by LWKT.  Adjust the stat if the thread is not
173 	 * runnable and not waiting to be scheduled on a cpu by the
174 	 * user process scheduler.
175 	 */
176 	if (kl->kl_stat == LSRUN) {
177 		if ((kl->kl_tdflags & TDF_RUNQ) == 0 &&
178 		    (lwp->lwp_flag & LWP_ONRUNQ) == 0) {
179 			kl->kl_stat = LSSLEEP;
180 		}
181 	}
182 #ifdef SMP
183 	kl->kl_mpcount = lwp->lwp_thread->td_mpcount;
184 #else
185 	kl->kl_mpcount = 0;
186 #endif
187 
188 	kl->kl_prio = lwp->lwp_usdata.bsd4.priority;	/* XXX TGEN dangerous assumption */
189 	kl->kl_tdprio = lwp->lwp_thread->td_pri;
190 	kl->kl_rtprio = lwp->lwp_rtprio;
191 
192 	kl->kl_uticks = lwp->lwp_thread->td_uticks;
193 	kl->kl_sticks = lwp->lwp_thread->td_sticks;
194 	kl->kl_iticks = lwp->lwp_thread->td_iticks;
195 	kl->kl_cpticks = lwp->lwp_cpticks;
196 	kl->kl_pctcpu = lwp->lwp_pctcpu;
197 	kl->kl_slptime = lwp->lwp_slptime;
198 	kl->kl_origcpu = lwp->lwp_usdata.bsd4.origcpu;	/* XXX TGEN same */
199 	kl->kl_estcpu = lwp->lwp_usdata.bsd4.estcpu;
200 	kl->kl_cpuid = lwp->lwp_thread->td_gd->gd_cpuid;
201 
202 	kl->kl_ru = lwp->lwp_ru;
203 
204 	kl->kl_siglist = lwp->lwp_siglist;
205 	kl->kl_sigmask = lwp->lwp_sigmask;
206 
207 	kl->kl_wchan = (uintptr_t)lwp->lwp_thread->td_wchan;
208 	if (lwp->lwp_thread->td_wmesg) {
209 		strncpy(kl->kl_wmesg, lwp->lwp_thread->td_wmesg, WMESGLEN);
210 		kl->kl_wmesg[WMESGLEN] = 0;
211 	}
212 }
213 
214 /*
215  * Fill in a struct kinfo_proc for kernel threads (i.e. those without proc).
216  */
217 void
218 fill_kinfo_proc_kthread(struct thread *td, struct kinfo_proc *kp)
219 {
220 	bzero(kp, sizeof(*kp));
221 
222 	/*
223 	 * Fill in fake proc information and semi-fake lwp info.
224 	 */
225 	kp->kp_pid = -1;
226 	kp->kp_tdev = NOUDEV;
227 	strncpy(kp->kp_comm, td->td_comm, sizeof(kp->kp_comm) - 1);
228 	kp->kp_comm[sizeof(kp->kp_comm) - 1] = 0;
229 	kp->kp_flags = P_SYSTEM;
230 	kp->kp_stat = SACTIVE;
231 
232 	kp->kp_lwp.kl_pid = -1;
233 	kp->kp_lwp.kl_tid = -1;
234 	kp->kp_lwp.kl_tdflags = td->td_flags;
235 #ifdef SMP
236 	kp->kp_lwp.kl_mpcount = td->td_mpcount;
237 #else /* !SMP */
238 	kp->kp_lwp.kl_mpcount = 0;
239 #endif /* SMP */
240 
241 	kp->kp_lwp.kl_tdprio = td->td_pri;
242 	kp->kp_lwp.kl_rtprio.type = RTP_PRIO_THREAD;
243 	kp->kp_lwp.kl_rtprio.prio = td->td_pri & TDPRI_MASK;
244 
245 	kp->kp_lwp.kl_uticks = td->td_uticks;
246 	kp->kp_lwp.kl_sticks = td->td_sticks;
247 	kp->kp_lwp.kl_iticks = td->td_iticks;
248 	kp->kp_lwp.kl_cpuid = td->td_gd->gd_cpuid;
249 
250 	kp->kp_lwp.kl_wchan = (uintptr_t)td->td_wchan;
251 	if (td->td_flags & TDF_RUNQ)
252 		kp->kp_lwp.kl_stat = LSRUN;
253 	else
254 		kp->kp_lwp.kl_stat = LSSLEEP;
255 	if (td->td_wmesg) {
256 		strncpy(kp->kp_lwp.kl_wmesg, td->td_wmesg, WMESGLEN);
257 		kp->kp_lwp.kl_wmesg[WMESGLEN] = 0;
258 	}
259 }
260