📄 kvm_proc.c
字号:
/*- * Copyright (c) 1989, 1992, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software developed by the Computer Systems * Engineering group at Lawrence Berkeley Laboratory under DARPA contract * BG 91-66 and contributed to Berkeley. * * 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. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University 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 REGENTS 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 REGENTS 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. */#if defined(LIBC_SCCS) && !defined(lint)static char sccsid[] = "@(#)kvm_proc.c 8.3 (Berkeley) 9/23/93";#endif /* LIBC_SCCS and not lint *//* * Proc traversal interface for kvm. ps and w are (probably) the exclusive * users of this code, so we've factored it out into a separate module. * Thus, we keep this grunge out of the other kvm applications (i.e., * most other applications are interested only in open/close/read/nlist). */#include <sys/param.h>#include <sys/user.h>#include <sys/proc.h>#include <sys/exec.h>#include <sys/stat.h>#include <sys/ioctl.h>#include <sys/tty.h>#include <unistd.h>#include <nlist.h>#include <kvm.h>#include <vm/vm.h>#include <vm/vm_param.h>#include <vm/swap_pager.h>#include <sys/sysctl.h>#include <limits.h>#include <db.h>#include <paths.h>#include "kvm_private.h"static char *kvm_readswap(kd, p, va, cnt) kvm_t *kd; const struct proc *p; u_long va; u_long *cnt;{ register int ix; register u_long addr, head; register u_long offset, pagestart, sbstart, pgoff; register off_t seekpoint; struct vm_map_entry vme; struct vm_object vmo; struct pager_struct pager; struct swpager swap; struct swblock swb; static char page[NBPG]; head = (u_long)&p->p_vmspace->vm_map.header; /* * Look through the address map for the memory object * that corresponds to the given virtual address. * The header just has the entire valid range. */ addr = head; while (1) { if (kvm_read(kd, addr, (char *)&vme, sizeof(vme)) != sizeof(vme)) return (0); if (va >= vme.start && va <= vme.end && vme.object.vm_object != 0) break; addr = (u_long)vme.next; if (addr == 0 || addr == head) return (0); } /* * We found the right object -- follow shadow links. */ offset = va - vme.start + vme.offset; addr = (u_long)vme.object.vm_object; while (1) { if (kvm_read(kd, addr, (char *)&vmo, sizeof(vmo)) != sizeof(vmo)) return (0); addr = (u_long)vmo.shadow; if (addr == 0) break; offset += vmo.shadow_offset; } if (vmo.pager == 0) return (0); offset += vmo.paging_offset; /* * Read in the pager info and make sure it's a swap device. */ addr = (u_long)vmo.pager; if (kvm_read(kd, addr, (char *)&pager, sizeof(pager)) != sizeof(pager) || pager.pg_type != PG_SWAP) return (0); /* * Read in the swap_pager private data, and compute the * swap offset. */ addr = (u_long)pager.pg_data; if (kvm_read(kd, addr, (char *)&swap, sizeof(swap)) != sizeof(swap)) return (0); ix = offset / dbtob(swap.sw_bsize); if (swap.sw_blocks == 0 || ix >= swap.sw_nblocks) return (0); addr = (u_long)&swap.sw_blocks[ix]; if (kvm_read(kd, addr, (char *)&swb, sizeof(swb)) != sizeof(swb)) return (0); sbstart = (offset / dbtob(swap.sw_bsize)) * dbtob(swap.sw_bsize); sbstart /= NBPG; pagestart = offset / NBPG; pgoff = pagestart - sbstart; if (swb.swb_block == 0 || (swb.swb_mask & (1 << pgoff)) == 0) return (0); seekpoint = dbtob(swb.swb_block) + ctob(pgoff); errno = 0; if (lseek(kd->swfd, seekpoint, 0) == -1 && errno != 0) return (0); if (read(kd->swfd, page, sizeof(page)) != sizeof(page)) return (0); offset %= NBPG; *cnt = NBPG - offset; return (&page[offset]);}#define KREAD(kd, addr, obj) \ (kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj))/* * Read proc's from memory file into buffer bp, which has space to hold * at most maxcnt procs. */static intkvm_proclist(kd, what, arg, p, bp, maxcnt) kvm_t *kd; int what, arg; struct proc *p; struct kinfo_proc *bp; int maxcnt;{ register int cnt = 0; struct eproc eproc; struct pgrp pgrp; struct session sess; struct tty tty; struct proc proc; for (; cnt < maxcnt && p != NULL; p = proc.p_next) { if (KREAD(kd, (u_long)p, &proc)) { _kvm_err(kd, kd->program, "can't read proc at %x", p); return (-1); } if (KREAD(kd, (u_long)proc.p_cred, &eproc.e_pcred) == 0) KREAD(kd, (u_long)eproc.e_pcred.pc_ucred, &eproc.e_ucred); switch(what) { case KERN_PROC_PID: if (proc.p_pid != (pid_t)arg) continue; break; case KERN_PROC_UID: if (eproc.e_ucred.cr_uid != (uid_t)arg) continue; break; case KERN_PROC_RUID: if (eproc.e_pcred.p_ruid != (uid_t)arg) continue; break; } /* * We're going to add another proc to the set. If this * will overflow the buffer, assume the reason is because * nprocs (or the proc list) is corrupt and declare an error. */ if (cnt >= maxcnt) { _kvm_err(kd, kd->program, "nprocs corrupt"); return (-1); } /* * gather eproc */ eproc.e_paddr = p; if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) { _kvm_err(kd, kd->program, "can't read pgrp at %x", proc.p_pgrp); return (-1); } eproc.e_sess = pgrp.pg_session; eproc.e_pgid = pgrp.pg_id; eproc.e_jobc = pgrp.pg_jobc; if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) { _kvm_err(kd, kd->program, "can't read session at %x", pgrp.pg_session); return (-1); } if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) { if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) { _kvm_err(kd, kd->program, "can't read tty at %x", sess.s_ttyp); return (-1); } eproc.e_tdev = tty.t_dev; eproc.e_tsess = tty.t_session; if (tty.t_pgrp != NULL) { if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) { _kvm_err(kd, kd->program, "can't read tpgrp at &x", tty.t_pgrp); return (-1); } eproc.e_tpgid = pgrp.pg_id; } else eproc.e_tpgid = -1; } else eproc.e_tdev = NODEV; eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0; if (sess.s_leader == p) eproc.e_flag |= EPROC_SLEADER; if (proc.p_wmesg) (void)kvm_read(kd, (u_long)proc.p_wmesg, eproc.e_wmesg, WMESGLEN);#ifdef sparc (void)kvm_read(kd, (u_long)&proc.p_vmspace->vm_rssize, (char *)&eproc.e_vm.vm_rssize, sizeof(eproc.e_vm.vm_rssize)); (void)kvm_read(kd, (u_long)&proc.p_vmspace->vm_tsize, (char *)&eproc.e_vm.vm_tsize, 3 * sizeof(eproc.e_vm.vm_rssize)); /* XXX */#else (void)kvm_read(kd, (u_long)proc.p_vmspace, (char *)&eproc.e_vm, sizeof(eproc.e_vm));#endif eproc.e_xsize = eproc.e_xrssize = 0; eproc.e_xccount = eproc.e_xswrss = 0; switch (what) { case KERN_PROC_PGRP: if (eproc.e_pgid != (pid_t)arg) continue; break; case KERN_PROC_TTY: if ((proc.p_flag & P_CONTROLT) == 0 || eproc.e_tdev != (dev_t)arg) continue; break; } bcopy(&proc, &bp->kp_proc, sizeof(proc)); bcopy(&eproc, &bp->kp_eproc, sizeof(eproc)); ++bp; ++cnt; } return (cnt);}/* * Build proc info array by reading in proc list from a crash dump. * Return number of procs read. maxcnt is the max we will read. */static intkvm_deadprocs(kd, what, arg, a_allproc, a_zombproc, maxcnt) kvm_t *kd; int what, arg; u_long a_allproc; u_long a_zombproc; int maxcnt;{ register struct kinfo_proc *bp = kd->procbase; register int acnt, zcnt; struct proc *p; if (KREAD(kd, a_allproc, &p)) { _kvm_err(kd, kd->program, "cannot read allproc"); return (-1); } acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt); if (acnt < 0) return (acnt); if (KREAD(kd, a_zombproc, &p)) { _kvm_err(kd, kd->program, "cannot read zombproc"); return (-1); } zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt); if (zcnt < 0) zcnt = 0; return (acnt + zcnt);}struct kinfo_proc *kvm_getprocs(kd, op, arg, cnt) kvm_t *kd; int op, arg; int *cnt;{ int mib[4], size, st, nprocs; if (kd->procbase != 0) { free((void *)kd->procbase); /* * Clear this pointer in case this call fails. Otherwise,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -