1999-07-02 19:28:49 +04:00
|
|
|
/* $NetBSD: kvm_proc.c,v 1.31 1999/07/02 15:28:50 simonb Exp $ */
|
1998-08-15 13:16:27 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1998 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Charles M. Hannum.
|
|
|
|
*
|
|
|
|
* 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 NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
|
|
|
|
*/
|
1996-03-19 01:33:07 +03:00
|
|
|
|
1994-05-09 07:15:36 +04:00
|
|
|
/*-
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1997-08-15 06:21:56 +04:00
|
|
|
#include <sys/cdefs.h>
|
1994-05-09 07:15:36 +04:00
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
1996-03-19 01:33:07 +03:00
|
|
|
#if 0
|
1994-05-09 07:15:36 +04:00
|
|
|
static char sccsid[] = "@(#)kvm_proc.c 8.3 (Berkeley) 9/23/93";
|
1996-03-19 01:33:07 +03:00
|
|
|
#else
|
1999-07-02 19:28:49 +04:00
|
|
|
__RCSID("$NetBSD: kvm_proc.c,v 1.31 1999/07/02 15:28:50 simonb Exp $");
|
1996-03-19 01:33:07 +03:00
|
|
|
#endif
|
1994-05-09 07:15:36 +04:00
|
|
|
#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>
|
1994-10-19 06:03:39 +03:00
|
|
|
#include <stdlib.h>
|
1995-05-16 18:21:08 +04:00
|
|
|
#include <string.h>
|
1994-05-09 07:15:36 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <nlist.h>
|
|
|
|
#include <kvm.h>
|
|
|
|
|
|
|
|
#include <vm/vm.h>
|
|
|
|
#include <vm/vm_param.h>
|
|
|
|
|
1998-02-12 09:55:29 +03:00
|
|
|
#include <uvm/uvm_extern.h>
|
1999-01-25 06:38:57 +03:00
|
|
|
#include <uvm/uvm_amap.h>
|
1998-02-12 09:55:29 +03:00
|
|
|
|
1994-05-09 07:15:36 +04:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <db.h>
|
|
|
|
#include <paths.h>
|
|
|
|
|
|
|
|
#include "kvm_private.h"
|
|
|
|
|
1994-05-24 02:22:24 +04:00
|
|
|
#define KREAD(kd, addr, obj) \
|
1998-09-27 22:15:58 +04:00
|
|
|
(kvm_read(kd, addr, (void *)(obj), sizeof(*obj)) != sizeof(*obj))
|
1994-05-24 02:22:24 +04:00
|
|
|
|
1997-08-15 21:52:45 +04:00
|
|
|
char *_kvm_uread __P((kvm_t *, const struct proc *, u_long, u_long *));
|
1995-07-04 07:03:30 +04:00
|
|
|
ssize_t kvm_uread __P((kvm_t *, const struct proc *, u_long, char *,
|
|
|
|
size_t));
|
|
|
|
|
|
|
|
static char **kvm_argv __P((kvm_t *, const struct proc *, u_long, int,
|
|
|
|
int));
|
1998-09-09 04:31:25 +04:00
|
|
|
static int kvm_deadprocs __P((kvm_t *, int, int, u_long, u_long, u_long,
|
|
|
|
int));
|
1995-07-04 07:03:30 +04:00
|
|
|
static char **kvm_doargv __P((kvm_t *, const struct kinfo_proc *, int,
|
|
|
|
void (*)(struct ps_strings *, u_long *, int *)));
|
|
|
|
static int kvm_proclist __P((kvm_t *, int, int, struct proc *,
|
|
|
|
struct kinfo_proc *, int));
|
|
|
|
static int proc_verify __P((kvm_t *, u_long, const struct proc *));
|
|
|
|
static void ps_str_a __P((struct ps_strings *, u_long *, int *));
|
|
|
|
static void ps_str_e __P((struct ps_strings *, u_long *, int *));
|
1994-05-24 02:22:24 +04:00
|
|
|
|
1995-01-09 11:59:25 +03:00
|
|
|
char *
|
|
|
|
_kvm_uread(kd, p, va, cnt)
|
1994-05-09 07:15:36 +04:00
|
|
|
kvm_t *kd;
|
|
|
|
const struct proc *p;
|
|
|
|
u_long va;
|
|
|
|
u_long *cnt;
|
|
|
|
{
|
1998-09-27 22:15:58 +04:00
|
|
|
int true = 1;
|
1998-02-03 22:12:13 +03:00
|
|
|
u_long addr, head;
|
|
|
|
u_long offset;
|
1994-05-09 07:15:36 +04:00
|
|
|
struct vm_map_entry vme;
|
1998-02-12 09:55:29 +03:00
|
|
|
struct vm_amap amap;
|
|
|
|
struct vm_anon *anonp, anon;
|
|
|
|
struct vm_page pg;
|
1998-09-27 22:15:58 +04:00
|
|
|
u_long slot;
|
1994-05-09 07:15:36 +04:00
|
|
|
|
1994-09-18 05:52:01 +04:00
|
|
|
if (kd->swapspc == 0) {
|
1998-09-27 22:15:58 +04:00
|
|
|
kd->swapspc = (char *)_kvm_malloc(kd, (size_t)kd->nbpg);
|
1994-09-18 05:52:01 +04:00
|
|
|
if (kd->swapspc == 0)
|
1994-09-18 04:22:22 +04:00
|
|
|
return (0);
|
|
|
|
}
|
1995-01-09 11:59:25 +03:00
|
|
|
|
1994-05-09 07:15:36 +04:00
|
|
|
/*
|
|
|
|
* Look through the address map for the memory object
|
|
|
|
* that corresponds to the given virtual address.
|
|
|
|
* The header just has the entire valid range.
|
|
|
|
*/
|
1995-01-09 11:59:25 +03:00
|
|
|
head = (u_long)&p->p_vmspace->vm_map.header;
|
1994-05-09 07:15:36 +04:00
|
|
|
addr = head;
|
1998-09-27 22:15:58 +04:00
|
|
|
while (true) {
|
1994-05-24 02:22:24 +04:00
|
|
|
if (KREAD(kd, addr, &vme))
|
1994-05-09 07:15:36 +04:00
|
|
|
return (0);
|
|
|
|
|
1998-02-12 09:55:29 +03:00
|
|
|
if (va >= vme.start && va < vme.end &&
|
|
|
|
vme.aref.ar_amap != NULL)
|
|
|
|
break;
|
|
|
|
|
1994-05-09 07:15:36 +04:00
|
|
|
addr = (u_long)vme.next;
|
1994-05-24 02:22:24 +04:00
|
|
|
if (addr == head)
|
1994-05-09 07:15:36 +04:00
|
|
|
return (0);
|
1998-02-12 09:55:29 +03:00
|
|
|
|
1994-05-09 07:15:36 +04:00
|
|
|
}
|
1998-02-12 09:55:29 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* we found the map entry, now to find the object...
|
|
|
|
*/
|
|
|
|
if (vme.aref.ar_amap == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
addr = (u_long)vme.aref.ar_amap;
|
|
|
|
if (KREAD(kd, addr, &amap))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
offset = va - vme.start;
|
1999-01-25 06:38:57 +03:00
|
|
|
slot = offset / kd->nbpg + vme.aref.ar_pageoff;
|
1998-02-12 09:55:29 +03:00
|
|
|
/* sanity-check slot number */
|
|
|
|
if (slot > amap.am_nslot)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
addr = (u_long)amap.am_anon + (offset / kd->nbpg) * sizeof(anonp);
|
|
|
|
if (KREAD(kd, addr, &anonp))
|
|
|
|
return NULL;
|
1994-05-24 02:22:24 +04:00
|
|
|
|
1998-02-12 09:55:29 +03:00
|
|
|
addr = (u_long)anonp;
|
|
|
|
if (KREAD(kd, addr, &anon))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
addr = (u_long)anon.u.an_page;
|
|
|
|
if (addr) {
|
|
|
|
if (KREAD(kd, addr, &pg))
|
|
|
|
return NULL;
|
|
|
|
|
1998-09-27 22:15:58 +04:00
|
|
|
if (pread(kd->pmfd, (void *)kd->swapspc, (size_t)kd->nbpg,
|
1998-07-01 00:29:39 +04:00
|
|
|
(off_t)pg.phys_addr) != kd->nbpg)
|
1998-02-12 09:55:29 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else {
|
1998-09-27 22:15:58 +04:00
|
|
|
if (pread(kd->swfd, (void *)kd->swapspc, (size_t)kd->nbpg,
|
1998-07-01 00:29:39 +04:00
|
|
|
(off_t)(anon.an_swslot * kd->nbpg)) != kd->nbpg)
|
1998-02-12 09:55:29 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
1995-01-09 11:59:25 +03:00
|
|
|
|
1994-05-24 02:22:24 +04:00
|
|
|
/* Found the page. */
|
1994-09-18 05:52:01 +04:00
|
|
|
offset %= kd->nbpg;
|
|
|
|
*cnt = kd->nbpg - offset;
|
1998-09-27 22:15:58 +04:00
|
|
|
return (&kd->swapspc[(size_t)offset]);
|
1994-05-24 02:22:24 +04:00
|
|
|
}
|
|
|
|
|
1994-05-09 07:15:36 +04:00
|
|
|
/*
|
|
|
|
* Read proc's from memory file into buffer bp, which has space to hold
|
|
|
|
* at most maxcnt procs.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
kvm_proclist(kd, what, arg, p, bp, maxcnt)
|
|
|
|
kvm_t *kd;
|
|
|
|
int what, arg;
|
|
|
|
struct proc *p;
|
|
|
|
struct kinfo_proc *bp;
|
|
|
|
int maxcnt;
|
|
|
|
{
|
1998-02-03 22:12:13 +03:00
|
|
|
int cnt = 0;
|
1994-05-09 07:15:36 +04:00
|
|
|
struct eproc eproc;
|
|
|
|
struct pgrp pgrp;
|
|
|
|
struct session sess;
|
|
|
|
struct tty tty;
|
|
|
|
struct proc proc;
|
|
|
|
|
1994-08-30 21:39:11 +04:00
|
|
|
for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) {
|
1994-05-09 07:15:36 +04:00
|
|
|
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)
|
1998-09-27 22:15:58 +04:00
|
|
|
if (KREAD(kd, (u_long)eproc.e_pcred.pc_ucred,
|
|
|
|
&eproc.e_ucred)) {
|
|
|
|
_kvm_err(kd, kd->program,
|
|
|
|
"can't read proc credentials at %x", p);
|
|
|
|
return -1;
|
|
|
|
}
|
1994-05-09 07:15:36 +04:00
|
|
|
|
|
|
|
switch(what) {
|
1999-07-02 19:28:49 +04:00
|
|
|
|
1994-05-09 07:15:36 +04:00
|
|
|
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)) {
|
1999-07-02 19:28:49 +04:00
|
|
|
_kvm_err(kd, kd->program, "can't read session at %x",
|
1994-05-09 07:15:36 +04:00
|
|
|
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,
|
1999-07-02 19:28:49 +04:00
|
|
|
"can't read tpgrp at &x",
|
1994-05-09 07:15:36 +04:00
|
|
|
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)
|
1999-07-02 19:28:49 +04:00
|
|
|
(void)kvm_read(kd, (u_long)proc.p_wmesg,
|
1994-05-09 07:15:36 +04:00
|
|
|
eproc.e_wmesg, WMESGLEN);
|
|
|
|
|
|
|
|
(void)kvm_read(kd, (u_long)proc.p_vmspace,
|
1998-09-27 22:15:58 +04:00
|
|
|
(void *)&eproc.e_vm, sizeof(eproc.e_vm));
|
1995-01-12 19:30:52 +03:00
|
|
|
|
1994-05-09 07:15:36 +04:00
|
|
|
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:
|
1999-07-02 19:28:49 +04:00
|
|
|
if ((proc.p_flag & P_CONTROLT) == 0 ||
|
1994-05-09 07:15:36 +04:00
|
|
|
eproc.e_tdev != (dev_t)arg)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
1998-08-10 06:43:08 +04:00
|
|
|
memcpy(&bp->kp_proc, &proc, sizeof(proc));
|
|
|
|
memcpy(&bp->kp_eproc, &eproc, sizeof(eproc));
|
1994-05-09 07:15:36 +04:00
|
|
|
++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 int
|
1998-09-09 04:31:25 +04:00
|
|
|
kvm_deadprocs(kd, what, arg, a_allproc, a_deadproc, a_zombproc, maxcnt)
|
1994-05-09 07:15:36 +04:00
|
|
|
kvm_t *kd;
|
|
|
|
int what, arg;
|
|
|
|
u_long a_allproc;
|
1998-09-09 04:31:25 +04:00
|
|
|
u_long a_deadproc;
|
1994-05-09 07:15:36 +04:00
|
|
|
u_long a_zombproc;
|
|
|
|
int maxcnt;
|
|
|
|
{
|
1998-02-03 22:12:13 +03:00
|
|
|
struct kinfo_proc *bp = kd->procbase;
|
1998-09-09 04:31:25 +04:00
|
|
|
int acnt, dcnt, zcnt;
|
1994-05-09 07:15:36 +04:00
|
|
|
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);
|
|
|
|
|
1998-09-09 04:31:25 +04:00
|
|
|
if (KREAD(kd, a_deadproc, &p)) {
|
|
|
|
_kvm_err(kd, kd->program, "cannot read deadproc");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
dcnt = kvm_proclist(kd, what, arg, p, bp, maxcnt - acnt);
|
|
|
|
if (dcnt < 0)
|
|
|
|
dcnt = 0;
|
|
|
|
|
1994-05-09 07:15:36 +04:00
|
|
|
if (KREAD(kd, a_zombproc, &p)) {
|
|
|
|
_kvm_err(kd, kd->program, "cannot read zombproc");
|
|
|
|
return (-1);
|
|
|
|
}
|
1998-09-09 04:31:25 +04:00
|
|
|
zcnt = kvm_proclist(kd, what, arg, p, bp + acnt,
|
|
|
|
maxcnt - (acnt + dcnt));
|
1994-05-09 07:15:36 +04:00
|
|
|
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;
|
|
|
|
{
|
1994-10-19 06:03:39 +03:00
|
|
|
size_t size;
|
|
|
|
int mib[4], st, nprocs;
|
1994-05-09 07:15:36 +04:00
|
|
|
|
|
|
|
if (kd->procbase != 0) {
|
|
|
|
free((void *)kd->procbase);
|
1999-07-02 19:28:49 +04:00
|
|
|
/*
|
1994-05-09 07:15:36 +04:00
|
|
|
* Clear this pointer in case this call fails. Otherwise,
|
|
|
|
* kvm_close() will free it again.
|
|
|
|
*/
|
|
|
|
kd->procbase = 0;
|
|
|
|
}
|
|
|
|
if (ISALIVE(kd)) {
|
|
|
|
size = 0;
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_PROC;
|
|
|
|
mib[2] = op;
|
|
|
|
mib[3] = arg;
|
|
|
|
st = sysctl(mib, 4, NULL, &size, NULL, 0);
|
|
|
|
if (st == -1) {
|
|
|
|
_kvm_syserr(kd, kd->program, "kvm_getprocs");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
|
|
|
|
if (kd->procbase == 0)
|
|
|
|
return (0);
|
|
|
|
st = sysctl(mib, 4, kd->procbase, &size, NULL, 0);
|
|
|
|
if (st == -1) {
|
|
|
|
_kvm_syserr(kd, kd->program, "kvm_getprocs");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (size % sizeof(struct kinfo_proc) != 0) {
|
|
|
|
_kvm_err(kd, kd->program,
|
|
|
|
"proc size mismatch (%d total, %d chunks)",
|
|
|
|
size, sizeof(struct kinfo_proc));
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
nprocs = size / sizeof(struct kinfo_proc);
|
|
|
|
} else {
|
1998-09-09 04:31:25 +04:00
|
|
|
struct nlist nl[5], *p;
|
1994-05-09 07:15:36 +04:00
|
|
|
|
|
|
|
nl[0].n_name = "_nprocs";
|
|
|
|
nl[1].n_name = "_allproc";
|
1998-09-09 04:31:25 +04:00
|
|
|
nl[2].n_name = "_deadproc";
|
|
|
|
nl[3].n_name = "_zombproc";
|
|
|
|
nl[4].n_name = 0;
|
1994-05-09 07:15:36 +04:00
|
|
|
|
|
|
|
if (kvm_nlist(kd, nl) != 0) {
|
|
|
|
for (p = nl; p->n_type != 0; ++p)
|
|
|
|
;
|
|
|
|
_kvm_err(kd, kd->program,
|
|
|
|
"%s: no such symbol", p->n_name);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (KREAD(kd, nl[0].n_value, &nprocs)) {
|
|
|
|
_kvm_err(kd, kd->program, "can't read nprocs");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
size = nprocs * sizeof(struct kinfo_proc);
|
|
|
|
kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
|
|
|
|
if (kd->procbase == 0)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
|
1998-09-09 04:31:25 +04:00
|
|
|
nl[2].n_value, nl[3].n_value, nprocs);
|
1994-05-09 07:15:36 +04:00
|
|
|
#ifdef notdef
|
|
|
|
size = nprocs * sizeof(struct kinfo_proc);
|
|
|
|
(void)realloc(kd->procbase, size);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
*cnt = nprocs;
|
|
|
|
return (kd->procbase);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_kvm_freeprocs(kd)
|
|
|
|
kvm_t *kd;
|
|
|
|
{
|
|
|
|
if (kd->procbase) {
|
|
|
|
free(kd->procbase);
|
|
|
|
kd->procbase = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
_kvm_realloc(kd, p, n)
|
|
|
|
kvm_t *kd;
|
|
|
|
void *p;
|
|
|
|
size_t n;
|
|
|
|
{
|
|
|
|
void *np = (void *)realloc(p, n);
|
|
|
|
|
|
|
|
if (np == 0)
|
|
|
|
_kvm_err(kd, kd->program, "out of memory");
|
|
|
|
return (np);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef MAX
|
|
|
|
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read in an argument vector from the user address space of process p.
|
1999-07-02 19:28:49 +04:00
|
|
|
* addr if the user-space base address of narg null-terminated contiguous
|
1994-05-09 07:15:36 +04:00
|
|
|
* strings. This is used to read in both the command arguments and
|
|
|
|
* environment strings. Read at most maxcnt characters of strings.
|
|
|
|
*/
|
|
|
|
static char **
|
|
|
|
kvm_argv(kd, p, addr, narg, maxcnt)
|
|
|
|
kvm_t *kd;
|
1995-07-04 07:03:30 +04:00
|
|
|
const struct proc *p;
|
1998-02-03 22:12:13 +03:00
|
|
|
u_long addr;
|
|
|
|
int narg;
|
|
|
|
int maxcnt;
|
1994-05-09 07:15:36 +04:00
|
|
|
{
|
1998-02-03 22:12:13 +03:00
|
|
|
char *np, *cp, *ep, *ap;
|
1998-09-27 22:15:58 +04:00
|
|
|
u_long oaddr = (u_long)~0L;
|
|
|
|
u_long len;
|
|
|
|
size_t cc;
|
1998-02-03 22:12:13 +03:00
|
|
|
char **argv;
|
1994-05-09 07:15:36 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that there aren't an unreasonable number of agruments,
|
|
|
|
* and that the address is in user space.
|
|
|
|
*/
|
1997-08-12 20:30:15 +04:00
|
|
|
if (narg > ARG_MAX || addr < kd->min_uva || addr >= kd->max_uva)
|
1994-05-09 07:15:36 +04:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
if (kd->argv == 0) {
|
|
|
|
/*
|
|
|
|
* Try to avoid reallocs.
|
|
|
|
*/
|
|
|
|
kd->argc = MAX(narg + 1, 32);
|
1999-07-02 19:28:49 +04:00
|
|
|
kd->argv = (char **)_kvm_malloc(kd, kd->argc *
|
1994-05-09 07:15:36 +04:00
|
|
|
sizeof(*kd->argv));
|
|
|
|
if (kd->argv == 0)
|
|
|
|
return (0);
|
|
|
|
} else if (narg + 1 > kd->argc) {
|
|
|
|
kd->argc = MAX(2 * kd->argc, narg + 1);
|
1999-07-02 19:28:49 +04:00
|
|
|
kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc *
|
1994-05-09 07:15:36 +04:00
|
|
|
sizeof(*kd->argv));
|
|
|
|
if (kd->argv == 0)
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (kd->argspc == 0) {
|
1998-09-27 22:15:58 +04:00
|
|
|
kd->argspc = (char *)_kvm_malloc(kd, (size_t)kd->nbpg);
|
1994-05-09 07:15:36 +04:00
|
|
|
if (kd->argspc == 0)
|
|
|
|
return (0);
|
1994-09-18 05:52:01 +04:00
|
|
|
kd->arglen = kd->nbpg;
|
1994-05-09 07:15:36 +04:00
|
|
|
}
|
1995-05-16 18:21:08 +04:00
|
|
|
if (kd->argbuf == 0) {
|
1998-09-27 22:15:58 +04:00
|
|
|
kd->argbuf = (char *)_kvm_malloc(kd, (size_t)kd->nbpg);
|
1995-05-16 18:21:08 +04:00
|
|
|
if (kd->argbuf == 0)
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
cc = sizeof(char *) * narg;
|
1998-09-27 22:15:58 +04:00
|
|
|
if (kvm_uread(kd, p, addr, (void *)kd->argv, cc) != cc)
|
1995-05-16 18:21:08 +04:00
|
|
|
return (0);
|
|
|
|
ap = np = kd->argspc;
|
1994-05-09 07:15:36 +04:00
|
|
|
argv = kd->argv;
|
|
|
|
len = 0;
|
|
|
|
/*
|
|
|
|
* Loop over pages, filling in the argument vector.
|
|
|
|
*/
|
1995-05-16 18:21:08 +04:00
|
|
|
while (argv < kd->argv + narg && *argv != 0) {
|
|
|
|
addr = (u_long)*argv & ~(kd->nbpg - 1);
|
|
|
|
if (addr != oaddr) {
|
1998-09-27 22:15:58 +04:00
|
|
|
if (kvm_uread(kd, p, addr, kd->argbuf,
|
|
|
|
(size_t)kd->nbpg) != kd->nbpg)
|
1995-05-16 18:21:08 +04:00
|
|
|
return (0);
|
|
|
|
oaddr = addr;
|
|
|
|
}
|
|
|
|
addr = (u_long)*argv & (kd->nbpg - 1);
|
1998-09-27 22:15:58 +04:00
|
|
|
cp = kd->argbuf + (size_t)addr;
|
|
|
|
cc = kd->nbpg - (size_t)addr;
|
|
|
|
if (maxcnt > 0 && cc > (size_t)(maxcnt - len))
|
|
|
|
cc = (size_t)(maxcnt - len);
|
1995-05-16 18:21:08 +04:00
|
|
|
ep = memchr(cp, '\0', cc);
|
|
|
|
if (ep != 0)
|
|
|
|
cc = ep - cp + 1;
|
1994-05-09 07:15:36 +04:00
|
|
|
if (len + cc > kd->arglen) {
|
1998-02-03 22:12:13 +03:00
|
|
|
int off;
|
|
|
|
char **pp;
|
|
|
|
char *op = kd->argspc;
|
1994-05-09 07:15:36 +04:00
|
|
|
|
|
|
|
kd->arglen *= 2;
|
|
|
|
kd->argspc = (char *)_kvm_realloc(kd, kd->argspc,
|
1998-09-27 22:15:58 +04:00
|
|
|
(size_t)kd->arglen);
|
1994-05-09 07:15:36 +04:00
|
|
|
if (kd->argspc == 0)
|
|
|
|
return (0);
|
|
|
|
/*
|
|
|
|
* Adjust argv pointers in case realloc moved
|
|
|
|
* the string space.
|
|
|
|
*/
|
|
|
|
off = kd->argspc - op;
|
1995-05-18 17:59:25 +04:00
|
|
|
for (pp = kd->argv; pp < argv; pp++)
|
1994-05-09 07:15:36 +04:00
|
|
|
*pp += off;
|
1995-05-18 16:21:54 +04:00
|
|
|
ap += off;
|
|
|
|
np += off;
|
1994-05-09 07:15:36 +04:00
|
|
|
}
|
1995-05-16 18:21:08 +04:00
|
|
|
memcpy(np, cp, cc);
|
|
|
|
np += cc;
|
1994-05-09 07:15:36 +04:00
|
|
|
len += cc;
|
1995-05-16 18:21:08 +04:00
|
|
|
if (ep != 0) {
|
|
|
|
*argv++ = ap;
|
|
|
|
ap = np;
|
|
|
|
} else
|
|
|
|
*argv += cc;
|
1994-05-09 07:15:36 +04:00
|
|
|
if (maxcnt > 0 && len >= maxcnt) {
|
|
|
|
/*
|
|
|
|
* We're stopping prematurely. Terminate the
|
1995-05-16 18:21:08 +04:00
|
|
|
* current string.
|
1994-05-09 07:15:36 +04:00
|
|
|
*/
|
1995-05-16 18:21:08 +04:00
|
|
|
if (ep == 0) {
|
|
|
|
*np = '\0';
|
1995-05-25 07:03:42 +04:00
|
|
|
*argv++ = ap;
|
1995-05-16 18:21:08 +04:00
|
|
|
}
|
|
|
|
break;
|
1994-05-09 07:15:36 +04:00
|
|
|
}
|
|
|
|
}
|
1995-05-16 18:21:08 +04:00
|
|
|
/* Make sure argv is terminated. */
|
|
|
|
*argv = 0;
|
|
|
|
return (kd->argv);
|
1994-05-09 07:15:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ps_str_a(p, addr, n)
|
|
|
|
struct ps_strings *p;
|
|
|
|
u_long *addr;
|
|
|
|
int *n;
|
|
|
|
{
|
|
|
|
*addr = (u_long)p->ps_argvstr;
|
|
|
|
*n = p->ps_nargvstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ps_str_e(p, addr, n)
|
|
|
|
struct ps_strings *p;
|
|
|
|
u_long *addr;
|
|
|
|
int *n;
|
|
|
|
{
|
|
|
|
*addr = (u_long)p->ps_envstr;
|
|
|
|
*n = p->ps_nenvstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine if the proc indicated by p is still active.
|
|
|
|
* This test is not 100% foolproof in theory, but chances of
|
|
|
|
* being wrong are very low.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
proc_verify(kd, kernp, p)
|
|
|
|
kvm_t *kd;
|
|
|
|
u_long kernp;
|
|
|
|
const struct proc *p;
|
|
|
|
{
|
|
|
|
struct proc kernproc;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Just read in the whole proc. It's not that big relative
|
|
|
|
* to the cost of the read system call.
|
|
|
|
*/
|
1999-07-02 19:28:49 +04:00
|
|
|
if (kvm_read(kd, kernp, (void *)&kernproc, sizeof(kernproc)) !=
|
1994-05-09 07:15:36 +04:00
|
|
|
sizeof(kernproc))
|
|
|
|
return (0);
|
|
|
|
return (p->p_pid == kernproc.p_pid &&
|
|
|
|
(kernproc.p_stat != SZOMB || p->p_stat == SZOMB));
|
|
|
|
}
|
|
|
|
|
|
|
|
static char **
|
|
|
|
kvm_doargv(kd, kp, nchr, info)
|
|
|
|
kvm_t *kd;
|
|
|
|
const struct kinfo_proc *kp;
|
|
|
|
int nchr;
|
1995-05-16 18:21:08 +04:00
|
|
|
void (*info)(struct ps_strings *, u_long *, int *);
|
1994-05-09 07:15:36 +04:00
|
|
|
{
|
1998-02-03 22:12:13 +03:00
|
|
|
const struct proc *p = &kp->kp_proc;
|
|
|
|
char **ap;
|
1994-05-09 07:15:36 +04:00
|
|
|
u_long addr;
|
|
|
|
int cnt;
|
|
|
|
struct ps_strings arginfo;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pointers are stored at the top of the user stack.
|
|
|
|
*/
|
1997-08-12 20:30:15 +04:00
|
|
|
if (p->p_stat == SZOMB)
|
|
|
|
return (0);
|
|
|
|
cnt = kvm_uread(kd, p, kd->usrstack - sizeof(arginfo),
|
1998-09-27 22:15:58 +04:00
|
|
|
(void *)&arginfo, sizeof(arginfo));
|
1997-08-12 20:30:15 +04:00
|
|
|
if (cnt != sizeof(arginfo))
|
1994-05-09 07:15:36 +04:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
(*info)(&arginfo, &addr, &cnt);
|
1994-08-13 14:15:02 +04:00
|
|
|
if (cnt == 0)
|
|
|
|
return (0);
|
1994-05-09 07:15:36 +04:00
|
|
|
ap = kvm_argv(kd, p, addr, cnt, nchr);
|
|
|
|
/*
|
|
|
|
* For live kernels, make sure this process didn't go away.
|
|
|
|
*/
|
|
|
|
if (ap != 0 && ISALIVE(kd) &&
|
|
|
|
!proc_verify(kd, (u_long)kp->kp_eproc.e_paddr, p))
|
|
|
|
ap = 0;
|
|
|
|
return (ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the command args. This code is now machine independent.
|
|
|
|
*/
|
|
|
|
char **
|
|
|
|
kvm_getargv(kd, kp, nchr)
|
|
|
|
kvm_t *kd;
|
|
|
|
const struct kinfo_proc *kp;
|
|
|
|
int nchr;
|
|
|
|
{
|
|
|
|
return (kvm_doargv(kd, kp, nchr, ps_str_a));
|
|
|
|
}
|
|
|
|
|
|
|
|
char **
|
|
|
|
kvm_getenvv(kd, kp, nchr)
|
|
|
|
kvm_t *kd;
|
|
|
|
const struct kinfo_proc *kp;
|
|
|
|
int nchr;
|
|
|
|
{
|
|
|
|
return (kvm_doargv(kd, kp, nchr, ps_str_e));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read from user space. The user context is given by p.
|
|
|
|
*/
|
|
|
|
ssize_t
|
|
|
|
kvm_uread(kd, p, uva, buf, len)
|
|
|
|
kvm_t *kd;
|
1998-02-03 22:12:13 +03:00
|
|
|
const struct proc *p;
|
|
|
|
u_long uva;
|
|
|
|
char *buf;
|
|
|
|
size_t len;
|
1994-05-09 07:15:36 +04:00
|
|
|
{
|
1998-02-03 22:12:13 +03:00
|
|
|
char *cp;
|
1994-05-09 07:15:36 +04:00
|
|
|
|
|
|
|
cp = buf;
|
|
|
|
while (len > 0) {
|
1998-09-27 22:15:58 +04:00
|
|
|
size_t cc;
|
1998-02-03 22:12:13 +03:00
|
|
|
char *dp;
|
1995-07-04 07:03:30 +04:00
|
|
|
u_long cnt;
|
1995-01-09 11:59:25 +03:00
|
|
|
|
|
|
|
dp = _kvm_uread(kd, p, uva, &cnt);
|
|
|
|
if (dp == 0) {
|
|
|
|
_kvm_err(kd, 0, "invalid address (%x)", uva);
|
|
|
|
return (0);
|
|
|
|
}
|
1998-09-27 22:15:58 +04:00
|
|
|
cc = (size_t)MIN(cnt, len);
|
1998-08-10 06:43:08 +04:00
|
|
|
memcpy(cp, dp, cc);
|
1994-05-09 07:15:36 +04:00
|
|
|
cp += cc;
|
|
|
|
uva += cc;
|
|
|
|
len -= cc;
|
|
|
|
}
|
|
|
|
return (ssize_t)(cp - buf);
|
|
|
|
}
|