sys/kern/kern_kinfo.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | /*- * Copyright (c) 2007 The DragonFly Project. All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Simon 'corecode' Schubert <corecode@fs.ei.tum.de> * by Thomas E. Spanjaard <tgen@netphreax.net> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. Neither the name of The DragonFly Project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific, prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * This is a source file used by both the kernel and libkvm. */ #ifndef _KERNEL #define _KERNEL_STRUCTURES #endif #include <sys/proc.h> #include <vm/vm_map.h> #include <sys/kinfo.h> #include <sys/tty.h> #include <sys/conf.h> #include <sys/jail.h> #include <sys/mplock2.h> #include <sys/globaldata.h> #ifdef _KERNEL #include <sys/systm.h> #else #include <string.h> dev_t devid_from_dev(cdev_t dev); /* kvm_proc.c */ #endif #ifndef _KERNEL /* * This is a temporary hack for when libkvm compiles in this file * from userland. These functions don't belong here. */ static void timevalfix(struct timeval *t1) { if (t1->tv_usec < 0) { t1->tv_sec--; t1->tv_usec += 1000000; } if (t1->tv_usec >= 1000000) { t1->tv_sec++; t1->tv_usec -= 1000000; } } static void timevaladd(struct timeval *t1, const struct timeval *t2) { t1->tv_sec += t2->tv_sec; t1->tv_usec += t2->tv_usec; timevalfix(t1); } static void ruadd(struct rusage *ru, struct rusage *ru2) { long *ip, *ip2; int i; timevaladd(&ru->ru_utime, &ru2->ru_utime); timevaladd(&ru->ru_stime, &ru2->ru_stime); if (ru->ru_maxrss < ru2->ru_maxrss) ru->ru_maxrss = ru2->ru_maxrss; ip = &ru->ru_first; ip2 = &ru2->ru_first; for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--) *ip++ += *ip2++; } #endif /* * Fill in a struct kinfo_proc, and zero the lwp fields for a possible * fill_kinfo_lwp() aggregation. * * NOTE! We may be asked to fill in kinfo_proc for a zombied process, and * the process may be in the middle of being deallocated. Check all pointers * for NULL. * * Caller must hold p->p_token */ void fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp) { struct session *sess; struct pgrp *pgrp; struct vmspace *vm; pgrp = p->p_pgrp; sess = pgrp ? pgrp->pg_session : NULL; bzero(kp, sizeof(*kp)); kp->kp_paddr = (uintptr_t)p; kp->kp_fd = (uintptr_t)p->p_fd; kp->kp_flags = p->p_flags; kp->kp_stat = p->p_stat; kp->kp_lock = p->p_lock; kp->kp_acflag = p->p_acflag; kp->kp_traceflag = p->p_traceflag; kp->kp_siglist = p->p_siglist; if (p->p_sigacts) { kp->kp_sigignore = p->p_sigignore; /* p_sigacts-> */ kp->kp_sigcatch = p->p_sigcatch; /* p_sigacts-> */ kp->kp_sigflag = p->p_sigacts->ps_flag; } kp->kp_start = p->p_start; strncpy(kp->kp_comm, p->p_comm, sizeof(kp->kp_comm) - 1); kp->kp_comm[sizeof(kp->kp_comm) - 1] = 0; if (p->p_ucred) { kp->kp_uid = p->p_ucred->cr_uid; kp->kp_ngroups = p->p_ucred->cr_ngroups; bcopy(p->p_ucred->cr_groups, kp->kp_groups, NGROUPS * sizeof(kp->kp_groups[0])); kp->kp_ruid = p->p_ucred->cr_ruid; kp->kp_svuid = p->p_ucred->cr_svuid; kp->kp_rgid = p->p_ucred->cr_rgid; kp->kp_svgid = p->p_ucred->cr_svgid; /* * Copy just some of the capabilities for now to avoid * changing the size of the kp structure. */ bcopy(p->p_ucred->cr_caps.caps, kp->kp_syscaps, sizeof(kp->kp_syscaps)); } kp->kp_pid = p->p_pid; if (p->p_oppid != 0) kp->kp_ppid = p->p_oppid; else kp->kp_ppid = p->p_pptr != NULL ? p->p_pptr->p_pid : -1; if (pgrp) { kp->kp_pgid = pgrp->pg_id; kp->kp_jobc = pgrp->pg_jobc; } if (sess) { kp->kp_sid = sess->s_sid; bcopy(sess->s_login, kp->kp_login, MAXLOGNAME); if (sess->s_ttyvp != NULL) kp->kp_auxflags |= KI_CTTY; if ((p->p_session != NULL) && SESS_LEADER(p)) kp->kp_auxflags |= KI_SLEADER; } if (sess && (p->p_flags & P_CONTROLT) != 0 && sess->s_ttyp != NULL) { kp->kp_tdev = devid_from_dev(sess->s_ttyp->t_dev); if (sess->s_ttyp->t_pgrp != NULL) kp->kp_tpgid = sess->s_ttyp->t_pgrp->pg_id; else kp->kp_tpgid = -1; if (sess->s_ttyp->t_session != NULL) kp->kp_tsid = sess->s_ttyp->t_session->s_sid; else kp->kp_tsid = -1; } else { kp->kp_tdev = NOUDEV; } kp->kp_exitstat = p->p_xstat; kp->kp_nthreads = p->p_nthreads; kp->kp_nice = p->p_nice; kp->kp_swtime = p->p_swtime; if ((vm = p->p_vmspace) != NULL) { kp->kp_vm_map_size = vm->vm_map.size; kp->kp_vm_rssize = vmspace_resident_count(vm); kp->kp_vm_swrss = vm->vm_swrss; kp->kp_vm_tsize = btoc(vm->vm_tsize); kp->kp_vm_dsize = btoc(vm->vm_dsize); kp->kp_vm_ssize = btoc(vm->vm_ssize); } if (p->p_ucred && jailed(p->p_ucred)) kp->kp_jailid = p->p_ucred->cr_prison->pr_id; kp->kp_ru = p->p_ru; kp->kp_cru = p->p_cru; } /* * Fill in a struct kinfo_lwp. This routine also doubles as an aggregator * of lwps for the proc. * * The kl structure must be initially zerod by the caller. Note that * fill_kinfo_proc() will do this for us. */ void fill_kinfo_lwp(struct lwp *lwp, struct kinfo_lwp *kl) { kl->kl_pid = lwp->lwp_proc->p_pid; kl->kl_tid = lwp->lwp_tid; kl->kl_flags = lwp->lwp_flags; kl->kl_stat = lwp->lwp_stat; kl->kl_lock = lwp->lwp_lock; kl->kl_tdflags = lwp->lwp_thread->td_flags; /* * The process/lwp stat may not reflect whether the process is * actually sleeping or not if the related thread was directly * descheduled by LWKT. Adjust the stat if the thread is not * runnable and not waiting to be scheduled on a cpu by the * user process scheduler. */ if (kl->kl_stat == LSRUN) { if ((kl->kl_tdflags & TDF_RUNQ) == 0 && (lwp->lwp_mpflags & LWP_MP_ONRUNQ) == 0) { kl->kl_stat = LSSLEEP; } } #ifdef _KERNEL kl->kl_mpcount = get_mplock_count(lwp->lwp_thread); #else kl->kl_mpcount = 0; #endif kl->kl_prio = lwp->lwp_usdata.bsd4.priority; /* XXX TGEN dangerous assumption */ kl->kl_tdprio = lwp->lwp_thread->td_pri; kl->kl_rtprio = lwp->lwp_rtprio; kl->kl_uticks += lwp->lwp_thread->td_uticks; kl->kl_sticks += lwp->lwp_thread->td_sticks; kl->kl_iticks += lwp->lwp_thread->td_iticks; kl->kl_cpticks += lwp->lwp_cpticks; kl->kl_pctcpu += lwp->lwp_proc->p_stat == SZOMB ? 0 : lwp->lwp_pctcpu; kl->kl_slptime += lwp->lwp_slptime; kl->kl_origcpu = lwp->lwp_usdata.bsd4.batch; kl->kl_estcpu = lwp->lwp_usdata.bsd4.estcpu; kl->kl_cpuid = lwp->lwp_thread->td_gd->gd_cpuid; ruadd(&kl->kl_ru, &lwp->lwp_ru); kl->kl_siglist = lwp->lwp_siglist; kl->kl_sigmask = lwp->lwp_sigmask; kl->kl_wchan = (uintptr_t)lwp->lwp_thread->td_wchan; if (lwp->lwp_thread->td_wmesg) { strncpy(kl->kl_wmesg, lwp->lwp_thread->td_wmesg, WMESGLEN); kl->kl_wmesg[WMESGLEN] = 0; } strlcpy(kl->kl_comm, lwp->lwp_thread->td_comm, sizeof(kl->kl_comm)); } /* * Fill in a struct kinfo_proc for kernel threads (i.e. those without proc). */ void fill_kinfo_proc_kthread(struct thread *td, struct kinfo_proc *kp) { bzero(kp, sizeof(*kp)); /* * Fill in fake proc information and semi-fake lwp info. */ kp->kp_pid = -1; kp->kp_tdev = NOUDEV; strncpy(kp->kp_comm, td->td_comm, sizeof(kp->kp_comm) - 1); kp->kp_comm[sizeof(kp->kp_comm) - 1] = 0; kp->kp_flags = P_SYSTEM; if (td != &td->td_gd->gd_idlethread) kp->kp_stat = SACTIVE; else kp->kp_stat = SIDL; kp->kp_nthreads = 1; kp->kp_ktaddr = (uintptr_t)td; kp->kp_lwp.kl_pid = -1; kp->kp_lwp.kl_tid = -1; kp->kp_lwp.kl_tdflags = td->td_flags; #ifdef _KERNEL kp->kp_lwp.kl_mpcount = get_mplock_count(td); #else kp->kp_lwp.kl_mpcount = 0; #endif kp->kp_lwp.kl_tdprio = td->td_pri; kp->kp_lwp.kl_rtprio.type = RTP_PRIO_THREAD; kp->kp_lwp.kl_rtprio.prio = td->td_pri; kp->kp_lwp.kl_uticks = td->td_uticks; kp->kp_lwp.kl_sticks = td->td_sticks; kp->kp_lwp.kl_iticks = td->td_iticks; kp->kp_lwp.kl_cpuid = td->td_gd->gd_cpuid; kp->kp_lwp.kl_wchan = (uintptr_t)td->td_wchan; if (td->td_flags & TDF_RUNQ) kp->kp_lwp.kl_stat = LSRUN; else kp->kp_lwp.kl_stat = LSSLEEP; if (td->td_wmesg) { strncpy(kp->kp_lwp.kl_wmesg, td->td_wmesg, WMESGLEN); kp->kp_lwp.kl_wmesg[WMESGLEN] = 0; } strlcpy(kp->kp_lwp.kl_comm, td->td_comm, sizeof(kp->kp_lwp.kl_comm)); } |