2 * Copyright (c) 1989, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software developed by the Computer Systems
6 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
7 * BG 91-66 and contributed to Berkeley.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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 the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $FreeBSD: src/lib/libkvm/kvm_proc.c,v 1.25.2.3 2002/08/24 07:27:46 kris Exp $
35 * @(#)kvm_proc.c 8.3 (Berkeley) 9/23/93
39 * Proc traversal interface for kvm. ps and w are (probably) the exclusive
40 * users of this code, so we've factored it out into a separate module.
41 * Thus, we keep this grunge out of the other kvm applications (i.e.,
42 * most other applications are interested only in open/close/read/nlist).
45 #include <sys/user.h> /* MUST BE FIRST */
47 #include <sys/param.h>
51 #include <sys/globaldata.h>
52 #include <sys/ioctl.h>
63 #include <vm/vm_param.h>
64 #include <vm/swap_pager.h>
66 #include <sys/sysctl.h>
73 #include "kvm_private.h"
75 dev_t dev2udev(cdev_t dev);
77 #define KREAD(kd, addr, obj) \
78 (kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj))
79 #define KREADSTR(kd, addr) \
80 kvm_readstr(kd, (u_long)addr, NULL, NULL)
82 static struct kinfo_proc *
83 kinfo_resize_proc(kvm_t *kd, struct kinfo_proc *bp)
88 size_t pos = bp - kd->procend;
89 size_t size = kd->procend - kd->procbase;
95 kd->procbase = _kvm_realloc(kd, kd->procbase, sizeof(*bp) * size);
96 if (kd->procbase == NULL)
98 kd->procend = kd->procbase + size;
99 bp = kd->procbase + pos;
104 * note: this function is also used by /usr/src/sys/kern/kern_kinfo.c as
105 * compiled by userland.
112 if ((dev->si_umajor & 0xffffff00) ||
113 (dev->si_uminor & 0x0000ff00)) {
116 return((dev->si_umajor << 8) | dev->si_uminor);
120 * Helper routine which traverses the left hand side of a red-black sub-tree.
123 kvm_lwptraverse(kvm_t *kd, struct lwp *lwp, uintptr_t lwppos)
126 if (KREAD(kd, lwppos, lwp)) {
127 _kvm_err(kd, kd->program, "can't read lwp at %p",
129 return ((uintptr_t)-1);
131 if (lwp->u.lwp_rbnode.rbe_left == NULL)
133 lwppos = (uintptr_t)lwp->u.lwp_rbnode.rbe_left;
139 * Iterate LWPs in a process.
141 * The first lwp in a red-black tree is a left-side traversal of the tree.
144 kvm_firstlwp(kvm_t *kd, struct lwp *lwp, struct proc *proc)
146 return(kvm_lwptraverse(kd, lwp, (uintptr_t)proc->p_lwp_tree.rbh_root));
150 * If the current element is the left side of the parent the next element
151 * will be a left side traversal of the parent's right side. If the parent
152 * has no right side the next element will be the parent.
154 * If the current element is the right side of the parent the next element
157 * If the parent is NULL we are done.
160 kvm_nextlwp(kvm_t *kd, uintptr_t lwppos, struct lwp *lwp)
164 nextpos = (uintptr_t)lwp->u.lwp_rbnode.rbe_parent;
166 if (KREAD(kd, nextpos, lwp)) {
167 _kvm_err(kd, kd->program, "can't read lwp at %p",
169 return ((uintptr_t)-1);
171 if (lwppos == (uintptr_t)lwp->u.lwp_rbnode.rbe_left) {
173 * If we had gone down the left side the next element
174 * is a left hand traversal of the parent's right
175 * side, or the parent itself if there is no right
178 lwppos = (uintptr_t)lwp->u.lwp_rbnode.rbe_right;
180 nextpos = kvm_lwptraverse(kd, lwp, lwppos);
183 * If we had gone down the right side the next
184 * element is the parent.
186 /* nextpos = nextpos */
193 * Read proc's from memory file into buffer bp, which has space to hold
194 * at most maxcnt procs.
197 kvm_proclist(kvm_t *kd, int what, int arg, struct proc *p,
198 struct kinfo_proc *bp)
202 struct globaldata gdata;
204 struct session tsess;
208 struct thread thread;
211 struct vmspace vmspace;
212 struct prison prison;
213 struct sigacts sigacts;
221 for (; p != NULL; p = proc.p_list.le_next) {
222 if (KREAD(kd, (u_long)p, &proc)) {
223 _kvm_err(kd, kd->program, "can't read proc at %p", p);
226 if (KREAD(kd, (u_long)proc.p_ucred, &ucred)) {
227 _kvm_err(kd, kd->program, "can't read ucred at %p",
231 proc.p_ucred = &ucred;
233 switch(what & ~KERN_PROC_FLAGMASK) {
236 if (proc.p_pid != (pid_t)arg)
241 if (ucred.cr_uid != (uid_t)arg)
246 if (ucred.cr_ruid != (uid_t)arg)
251 if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
252 _kvm_err(kd, kd->program, "can't read pgrp at %p",
258 if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) {
259 _kvm_err(kd, kd->program, "can't read pproc at %p",
263 proc.p_pptr = &pproc;
266 if (proc.p_sigacts) {
267 if (KREAD(kd, (u_long)proc.p_sigacts, &sigacts)) {
268 _kvm_err(kd, kd->program,
269 "can't read sigacts at %p",
273 proc.p_sigacts = &sigacts;
276 if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
277 _kvm_err(kd, kd->program, "can't read session at %p",
281 pgrp.pg_session = &sess;
283 if ((proc.p_flags & P_CONTROLT) && sess.s_ttyp != NULL) {
284 if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
285 _kvm_err(kd, kd->program,
286 "can't read tty at %p", sess.s_ttyp);
290 if (tty.t_dev != NULL) {
291 if (KREAD(kd, (u_long)tty.t_dev, &cdev))
296 if (tty.t_pgrp != NULL) {
297 if (KREAD(kd, (u_long)tty.t_pgrp, &tpgrp)) {
298 _kvm_err(kd, kd->program,
299 "can't read tpgrp at %p",
305 if (tty.t_session != NULL) {
306 if (KREAD(kd, (u_long)tty.t_session, &tsess)) {
307 _kvm_err(kd, kd->program,
308 "can't read tsess at %p",
312 tty.t_session = &tsess;
316 if (KREAD(kd, (u_long)proc.p_vmspace, &vmspace)) {
317 _kvm_err(kd, kd->program, "can't read vmspace at %p",
321 proc.p_vmspace = &vmspace;
323 if (ucred.cr_prison != NULL) {
324 if (KREAD(kd, (u_long)ucred.cr_prison, &prison)) {
325 _kvm_err(kd, kd->program, "can't read prison at %p",
329 ucred.cr_prison = &prison;
332 switch (what & ~KERN_PROC_FLAGMASK) {
335 if (proc.p_pgrp->pg_id != (pid_t)arg)
340 if ((proc.p_flags & P_CONTROLT) == 0 ||
341 dev2udev(proc.p_pgrp->pg_session->s_ttyp->t_dev)
347 if ((bp = kinfo_resize_proc(kd, bp)) == NULL)
349 fill_kinfo_proc(&proc, bp);
350 bp->kp_paddr = (uintptr_t)p;
352 lwppos = kvm_firstlwp(kd, &lwp, &proc);
354 bp++; /* Just export the proc then */
357 while (lwppos && lwppos != (uintptr_t)-1) {
358 if (p != lwp.lwp_proc) {
359 _kvm_err(kd, kd->program, "lwp has wrong parent");
362 lwp.lwp_proc = &proc;
363 if (KREAD(kd, (u_long)lwp.lwp_thread, &thread)) {
364 _kvm_err(kd, kd->program, "can't read thread at %p",
368 lwp.lwp_thread = &thread;
371 if (KREAD(kd, (u_long)thread.td_gd, &gdata)) {
372 _kvm_err(kd, kd->program, "can't read"
377 thread.td_gd = &gdata;
379 if (thread.td_wmesg) {
380 wmesg = (void *)KREADSTR(kd, thread.td_wmesg);
382 _kvm_err(kd, kd->program, "can't read"
387 thread.td_wmesg = wmesg;
392 if ((bp = kinfo_resize_proc(kd, bp)) == NULL)
394 fill_kinfo_proc(&proc, bp);
395 fill_kinfo_lwp(&lwp, &bp->kp_lwp);
396 bp->kp_paddr = (uintptr_t)p;
401 if ((what & KERN_PROC_FLAG_LWP) == 0)
403 lwppos = kvm_nextlwp(kd, lwppos, &lwp);
405 if (lwppos == (uintptr_t)-1)
412 * Build proc info array by reading in proc list from a crash dump.
413 * We reallocate kd->procbase as necessary.
416 kvm_deadprocs(kvm_t *kd, int what, int arg, int allproc_hsize)
418 struct kinfo_proc *bp;
420 struct proclist **pl;
429 * Dynamically allocate space for all the elements of the
430 * allprocs array and KREAD() them.
432 pl = _kvm_malloc(kd, allproc_hsize * sizeof(struct proclist *));
433 for (n = 0; n < allproc_hsize; n++) {
434 pl[n] = _kvm_malloc(kd, sizeof(struct proclist));
435 a_allproc = sizeof(struct procglob) * n +
436 offsetof(struct procglob, allproc);
438 if (KREAD(kd, (u_long)nextoff, pl[n])) {
439 _kvm_err(kd, kd->program, "can't read proclist at 0x%lx",
444 /* Ignore empty proclists */
445 if (LIST_EMPTY(pl[n]))
448 bp = kd->procbase + cnt;
450 partcnt = kvm_proclist(kd, what, arg, p, bp);
464 kvm_getprocs(kvm_t *kd, int op, int arg, int *cnt)
466 int mib[4], st, nprocs, allproc_hsize;
467 int miblen = ((op & ~KERN_PROC_FLAGMASK) == KERN_PROC_ALL) ? 3 : 4;
470 if (kd->procbase != 0) {
471 free((void *)kd->procbase);
473 * Clear this pointer in case this call fails. Otherwise,
474 * kvm_close() will free it again.
478 if (kvm_ishost(kd)) {
484 st = sysctl(mib, miblen, NULL, &size, NULL, 0);
486 _kvm_syserr(kd, kd->program, "kvm_getprocs");
491 kd->procbase = (struct kinfo_proc *)
492 _kvm_realloc(kd, kd->procbase, size);
493 if (kd->procbase == 0)
495 st = sysctl(mib, miblen, kd->procbase, &size, NULL, 0);
496 } while (st == -1 && errno == ENOMEM);
498 _kvm_syserr(kd, kd->program, "kvm_getprocs");
501 if (size % sizeof(struct kinfo_proc) != 0) {
502 _kvm_err(kd, kd->program,
503 "proc size mismatch (%zd total, %zd chunks)",
504 size, sizeof(struct kinfo_proc));
507 nprocs = size / sizeof(struct kinfo_proc);
509 struct nlist nl[4], *p;
511 nl[0].n_name = "_nprocs";
512 nl[1].n_name = "_procglob";
513 nl[2].n_name = "_allproc_hsize";
516 if (kvm_nlist(kd, nl) != 0) {
517 for (p = nl; p->n_type != 0; ++p)
519 _kvm_err(kd, kd->program,
520 "%s: no such symbol", p->n_name);
523 if (KREAD(kd, nl[0].n_value, &nprocs)) {
524 _kvm_err(kd, kd->program, "can't read nprocs");
527 if (KREAD(kd, nl[2].n_value, &allproc_hsize)) {
528 _kvm_err(kd, kd->program, "can't read allproc_hsize");
531 nprocs = kvm_deadprocs(kd, op, arg, allproc_hsize);
533 size = nprocs * sizeof(struct kinfo_proc);
534 (void)realloc(kd->procbase, size);
538 return (kd->procbase);
542 _kvm_freeprocs(kvm_t *kd)
551 _kvm_realloc(kvm_t *kd, void *p, size_t n)
553 void *np = (void *)realloc(p, n);
557 _kvm_err(kd, kd->program, "out of memory");
563 #define MAX(a, b) ((a) > (b) ? (a) : (b))
567 * Read in an argument vector from the user address space of process pid.
568 * addr if the user-space base address of narg null-terminated contiguous
569 * strings. This is used to read in both the command arguments and
570 * environment strings. Read at most maxcnt characters of strings.
573 kvm_argv(kvm_t *kd, pid_t pid, u_long addr, int narg, int maxcnt)
575 char *np, *cp, *ep, *ap;
577 u_long addr_min = VM_MIN_USER_ADDRESS;
578 u_long addr_max = VM_MAX_USER_ADDRESS;
583 * Check that there aren't an unreasonable number of agruments,
584 * and that the address is in user space.
586 if (narg > 512 || addr < addr_min || addr >= addr_max)
590 * kd->argv : work space for fetching the strings from the target
591 * process's space, and is converted for returning to caller
595 * Try to avoid reallocs.
597 kd->argc = MAX(narg + 1, 32);
598 kd->argv = (char **)_kvm_malloc(kd, kd->argc *
602 } else if (narg + 1 > kd->argc) {
603 kd->argc = MAX(2 * kd->argc, narg + 1);
604 kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc *
610 * kd->argspc : returned to user, this is where the kd->argv
611 * arrays are left pointing to the collected strings.
613 if (kd->argspc == 0) {
614 kd->argspc = (char *)_kvm_malloc(kd, PAGE_SIZE);
617 kd->arglen = PAGE_SIZE;
620 * kd->argbuf : used to pull in pages from the target process.
621 * the strings are copied out of here.
623 if (kd->argbuf == 0) {
624 kd->argbuf = (char *)_kvm_malloc(kd, PAGE_SIZE);
629 /* Pull in the target process'es argv vector */
630 cc = sizeof(char *) * narg;
631 if (kvm_uread(kd, pid, addr, (char *)kd->argv, cc) != cc)
634 * ap : saved start address of string we're working on in kd->argspc
635 * np : pointer to next place to write in kd->argspc
636 * len: length of data in kd->argspc
637 * argv: pointer to the argv vector that we are hunting around the
638 * target process space for, and converting to addresses in
639 * our address space (kd->argspc).
641 ap = np = kd->argspc;
645 * Loop over pages, filling in the argument vector.
646 * Note that the argv strings could be pointing *anywhere* in
647 * the user address space and are no longer contiguous.
648 * Note that *argv is modified when we are going to fetch a string
649 * that crosses a page boundary. We copy the next part of the string
650 * into to "np" and eventually convert the pointer.
652 while (argv < kd->argv + narg && *argv != NULL) {
654 /* get the address that the current argv string is on */
655 addr = (u_long)*argv & ~(PAGE_SIZE - 1);
657 /* is it the same page as the last one? */
659 if (kvm_uread(kd, pid, addr, kd->argbuf, PAGE_SIZE) !=
665 /* offset within the page... kd->argbuf */
666 addr = (u_long)*argv & (PAGE_SIZE - 1);
668 /* cp = start of string, cc = count of chars in this chunk */
669 cp = kd->argbuf + addr;
670 cc = PAGE_SIZE - addr;
672 /* dont get more than asked for by user process */
673 if (maxcnt > 0 && cc > maxcnt - len)
676 /* pointer to end of string if we found it in this page */
677 ep = memchr(cp, '\0', cc);
681 * at this point, cc is the count of the chars that we are
682 * going to retrieve this time. we may or may not have found
683 * the end of it. (ep points to the null if the end is known)
686 /* will we exceed the malloc/realloced buffer? */
687 if (len + cc > kd->arglen) {
690 char *op = kd->argspc;
693 kd->argspc = (char *)_kvm_realloc(kd, kd->argspc,
698 * Adjust argv pointers in case realloc moved
701 off = kd->argspc - op;
702 for (pp = kd->argv; pp < argv; pp++)
707 /* np = where to put the next part of the string in kd->argspc*/
708 /* np is kinda redundant.. could use "kd->argspc + len" */
710 np += cc; /* inc counters */
714 * if end of string found, set the *argv pointer to the
715 * saved beginning of string, and advance. argv points to
716 * somewhere in kd->argv.. This is initially relative
717 * to the target process, but when we close it off, we set
718 * it to point in our address space.
724 /* update the address relative to the target process */
728 if (maxcnt > 0 && len >= maxcnt) {
730 * We're stopping prematurely. Terminate the
740 /* Make sure argv is terminated. */
746 ps_str_a(struct ps_strings *p, u_long *addr, int *n)
748 *addr = (u_long)p->ps_argvstr;
753 ps_str_e(struct ps_strings *p, u_long *addr, int *n)
755 *addr = (u_long)p->ps_envstr;
760 * Determine if the proc indicated by p is still active.
761 * This test is not 100% foolproof in theory, but chances of
762 * being wrong are very low.
765 proc_verify(const struct kinfo_proc *p)
767 struct kinfo_proc kp;
774 mib[2] = KERN_PROC_PID;
778 error = sysctl(mib, 4, &kp, &len, NULL, 0);
782 error = (p->kp_pid == kp.kp_pid &&
783 (kp.kp_stat != SZOMB || p->kp_stat == SZOMB));
788 kvm_doargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr,
789 void (*info)(struct ps_strings *, u_long *, int *))
794 static struct ps_strings arginfo;
795 static u_long ps_strings;
798 if (ps_strings == 0) {
799 len = sizeof(ps_strings);
800 if (sysctlbyname("kern.ps_strings", &ps_strings, &len, NULL,
802 ps_strings = PS_STRINGS;
806 * Pointers are stored at the top of the user stack.
808 if (kp->kp_stat == SZOMB ||
809 kvm_uread(kd, kp->kp_pid, ps_strings, (char *)&arginfo,
810 sizeof(arginfo)) != sizeof(arginfo))
813 (*info)(&arginfo, &addr, &cnt);
816 ap = kvm_argv(kd, kp->kp_pid, addr, cnt, nchr);
818 * For live kernels, make sure this process didn't go away.
820 if (ap != NULL && (kvm_ishost(kd) || kvm_isvkernel(kd)) &&
827 * Get the command args. This code is now machine independent.
830 kvm_getargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr)
835 static unsigned long buflen;
836 static char *buf, *p;
840 if (!kvm_ishost(kd)) { /* XXX: vkernels */
841 _kvm_err(kd, kd->program,
842 "cannot read user space from dead kernel");
847 bufsz = sizeof(buflen);
848 i = sysctlbyname("kern.ps_arg_cache_limit",
849 &buflen, &bufsz, NULL, 0);
853 buf = malloc(buflen);
857 bufp = malloc(sizeof(char *) * argc);
863 oid[2] = KERN_PROC_ARGS;
866 i = sysctl(oid, 4, buf, &bufsz, 0, 0);
867 if (i == 0 && bufsz > 0) {
876 sizeof(char *) * argc);
878 } while (p < buf + bufsz);
883 if (kp->kp_flags & P_SYSTEM)
885 return (kvm_doargv(kd, kp, nchr, ps_str_a));
889 kvm_getenvv(kvm_t *kd, const struct kinfo_proc *kp, int nchr)
891 return (kvm_doargv(kd, kp, nchr, ps_str_e));
895 * Read from user space. The user context is given by pid.
898 kvm_uread(kvm_t *kd, pid_t pid, u_long uva, char *buf, size_t len)
901 char procfile[MAXPATHLEN];
905 if (!kvm_ishost(kd)) { /* XXX: vkernels */
906 _kvm_err(kd, kd->program,
907 "cannot read user space from dead kernel");
911 sprintf(procfile, "/proc/%d/mem", pid);
912 fd = open(procfile, O_RDONLY, 0);
914 _kvm_err(kd, kd->program, "cannot open %s", procfile);
922 if (lseek(fd, (off_t)uva, 0) == -1 && errno != 0) {
923 _kvm_err(kd, kd->program, "invalid address (%lx) in %s",
927 amount = read(fd, cp, len);
929 _kvm_syserr(kd, kd->program, "error reading %s",
934 _kvm_err(kd, kd->program, "EOF reading %s", procfile);
943 return ((ssize_t)(cp - buf));