158f0484fSRodney W. Grimes /*- 258f0484fSRodney W. Grimes * Copyright (c) 1989, 1992, 1993 358f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 458f0484fSRodney W. Grimes * 558f0484fSRodney W. Grimes * This code is derived from software developed by the Computer Systems 658f0484fSRodney W. Grimes * Engineering group at Lawrence Berkeley Laboratory under DARPA contract 758f0484fSRodney W. Grimes * BG 91-66 and contributed to Berkeley. 858f0484fSRodney W. Grimes * 958f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 1058f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 1158f0484fSRodney W. Grimes * are met: 1258f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 1358f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1458f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1558f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1658f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1758f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 1858f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 1958f0484fSRodney W. Grimes * without specific prior written permission. 2058f0484fSRodney W. Grimes * 2158f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2258f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2358f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2458f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2558f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2658f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2758f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2858f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2958f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3058f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3158f0484fSRodney W. Grimes * SUCH DAMAGE. 3258f0484fSRodney W. Grimes */ 3358f0484fSRodney W. Grimes 34f86e3350SBruce Evans #if 0 3558f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 3658f0484fSRodney W. Grimes static char sccsid[] = "@(#)kvm_proc.c 8.3 (Berkeley) 9/23/93"; 3758f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 38f86e3350SBruce Evans #endif 39f86e3350SBruce Evans 40f86e3350SBruce Evans #include <sys/cdefs.h> 41f86e3350SBruce Evans __FBSDID("$FreeBSD$"); 4258f0484fSRodney W. Grimes 4358f0484fSRodney W. Grimes /* 4458f0484fSRodney W. Grimes * Proc traversal interface for kvm. ps and w are (probably) the exclusive 4558f0484fSRodney W. Grimes * users of this code, so we've factored it out into a separate module. 4658f0484fSRodney W. Grimes * Thus, we keep this grunge out of the other kvm applications (i.e., 4758f0484fSRodney W. Grimes * most other applications are interested only in open/close/read/nlist). 4858f0484fSRodney W. Grimes */ 4958f0484fSRodney W. Grimes 5058f0484fSRodney W. Grimes #include <sys/param.h> 51b20ea179SAlfred Perlstein #define _WANT_UCRED /* make ucred.h give us 'struct ucred' */ 52aa22cbfeSAlfred Perlstein #include <sys/ucred.h> 53f8197bf0SPawel Jakub Dawidek #include <sys/queue.h> 54f8197bf0SPawel Jakub Dawidek #include <sys/_lock.h> 55f8197bf0SPawel Jakub Dawidek #include <sys/_mutex.h> 56f8197bf0SPawel Jakub Dawidek #include <sys/_task.h> 57413628a7SBjoern A. Zeeb #include <sys/cpuset.h> 5858f0484fSRodney W. Grimes #include <sys/user.h> 5958f0484fSRodney W. Grimes #include <sys/proc.h> 60413628a7SBjoern A. Zeeb #define _WANT_PRISON /* make jail.h give us 'struct prison' */ 61413628a7SBjoern A. Zeeb #include <sys/jail.h> 6258f0484fSRodney W. Grimes #include <sys/exec.h> 6358f0484fSRodney W. Grimes #include <sys/stat.h> 64276de18cSGarance A Drosehn #include <sys/sysent.h> 6558f0484fSRodney W. Grimes #include <sys/ioctl.h> 6658f0484fSRodney W. Grimes #include <sys/tty.h> 67338c7541SDavid Greenman #include <sys/file.h> 682bdd5609SPeter Wemm #include <sys/conf.h> 6951295a4dSJordan K. Hubbard #include <stdio.h> 7051295a4dSJordan K. Hubbard #include <stdlib.h> 7158f0484fSRodney W. Grimes #include <unistd.h> 7258f0484fSRodney W. Grimes #include <nlist.h> 7358f0484fSRodney W. Grimes #include <kvm.h> 7458f0484fSRodney W. Grimes 7558f0484fSRodney W. Grimes #include <vm/vm.h> 7658f0484fSRodney W. Grimes #include <vm/vm_param.h> 7758f0484fSRodney W. Grimes 7858f0484fSRodney W. Grimes #include <sys/sysctl.h> 7958f0484fSRodney W. Grimes 8058f0484fSRodney W. Grimes #include <limits.h> 8177721f53SPeter Wemm #include <memory.h> 8258f0484fSRodney W. Grimes #include <paths.h> 8358f0484fSRodney W. Grimes 8458f0484fSRodney W. Grimes #include "kvm_private.h" 8558f0484fSRodney W. Grimes 8658f0484fSRodney W. Grimes #define KREAD(kd, addr, obj) \ 8758f0484fSRodney W. Grimes (kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj)) 8858f0484fSRodney W. Grimes 8984a0b303SJeff Roberson static int ticks; 9084a0b303SJeff Roberson static int hz; 9184a0b303SJeff Roberson 9258f0484fSRodney W. Grimes /* 9358f0484fSRodney W. Grimes * Read proc's from memory file into buffer bp, which has space to hold 9458f0484fSRodney W. Grimes * at most maxcnt procs. 9558f0484fSRodney W. Grimes */ 9658f0484fSRodney W. Grimes static int 97*c10970ddSUlrich Spörlein kvm_proclist(kvm_t *kd, int what, int arg, struct proc *p, 98*c10970ddSUlrich Spörlein struct kinfo_proc *bp, int maxcnt) 9958f0484fSRodney W. Grimes { 100be04b6d1SDavid E. O'Brien int cnt = 0; 1011f7d2501SKirk McKusick struct kinfo_proc kinfo_proc, *kp; 10258f0484fSRodney W. Grimes struct pgrp pgrp; 10358f0484fSRodney W. Grimes struct session sess; 1042bdd5609SPeter Wemm struct cdev t_cdev; 10558f0484fSRodney W. Grimes struct tty tty; 1061f7d2501SKirk McKusick struct vmspace vmspace; 107840558b9SJohn Baldwin struct sigacts sigacts; 108*c10970ddSUlrich Spörlein #if 0 1091f7d2501SKirk McKusick struct pstats pstats; 110*c10970ddSUlrich Spörlein #endif 1111f7d2501SKirk McKusick struct ucred ucred; 112f8197bf0SPawel Jakub Dawidek struct prison pr; 11371fad9fdSJulian Elischer struct thread mtd; 11458f0484fSRodney W. Grimes struct proc proc; 115a58930d8STor Egge struct proc pproc; 11649b33de8SBrian Feldman struct timeval tv; 117276de18cSGarance A Drosehn struct sysentvec sysent; 118276de18cSGarance A Drosehn char svname[KI_EMULNAMELEN]; 11958f0484fSRodney W. Grimes 1201f7d2501SKirk McKusick kp = &kinfo_proc; 1211f7d2501SKirk McKusick kp->ki_structsize = sizeof(kinfo_proc); 1227ab24ea3SJulian Elischer /* 1237ab24ea3SJulian Elischer * Loop on the processes. this is completely broken because we need to be 1247ab24ea3SJulian Elischer * able to loop on the threads and merge the ones that are the same process some how. 1257ab24ea3SJulian Elischer */ 12642680b3aSBen Smithurst for (; cnt < maxcnt && p != NULL; p = LIST_NEXT(&proc, p_list)) { 1276f8c6a69SPeter Wemm memset(kp, 0, sizeof *kp); 12858f0484fSRodney W. Grimes if (KREAD(kd, (u_long)p, &proc)) { 129*c10970ddSUlrich Spörlein _kvm_err(kd, kd->program, "can't read proc at %p", p); 13058f0484fSRodney W. Grimes return (-1); 13158f0484fSRodney W. Grimes } 13258551c03SJulian Elischer if (proc.p_state != PRS_ZOMBIE) { 1333daf63fcSJulian Elischer if (KREAD(kd, (u_long)TAILQ_FIRST(&proc.p_threads), 13471fad9fdSJulian Elischer &mtd)) { 13558551c03SJulian Elischer _kvm_err(kd, kd->program, 136*c10970ddSUlrich Spörlein "can't read thread at %p", 1373daf63fcSJulian Elischer TAILQ_FIRST(&proc.p_threads)); 1383daf63fcSJulian Elischer return (-1); 1393daf63fcSJulian Elischer } 14058551c03SJulian Elischer } 141b1fc0ec1SRobert Watson if (KREAD(kd, (u_long)proc.p_ucred, &ucred) == 0) { 142b1fc0ec1SRobert Watson kp->ki_ruid = ucred.cr_ruid; 143b1fc0ec1SRobert Watson kp->ki_svuid = ucred.cr_svuid; 144b1fc0ec1SRobert Watson kp->ki_rgid = ucred.cr_rgid; 145b1fc0ec1SRobert Watson kp->ki_svgid = ucred.cr_svgid; 1461b5768beSBrooks Davis kp->ki_cr_flags = ucred.cr_flags; 1471b5768beSBrooks Davis if (ucred.cr_ngroups > KI_NGROUPS) { 1481b5768beSBrooks Davis kp->ki_ngroups = KI_NGROUPS; 1491b5768beSBrooks Davis kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW; 15068a5cc39SBrooks Davis } else 1511f7d2501SKirk McKusick kp->ki_ngroups = ucred.cr_ngroups; 152d534b0c2SBrooks Davis kvm_read(kd, (u_long)ucred.cr_groups, kp->ki_groups, 1531b5768beSBrooks Davis kp->ki_ngroups * sizeof(gid_t)); 1541f7d2501SKirk McKusick kp->ki_uid = ucred.cr_uid; 155f8197bf0SPawel Jakub Dawidek if (ucred.cr_prison != NULL) { 156f8197bf0SPawel Jakub Dawidek if (KREAD(kd, (u_long)ucred.cr_prison, &pr)) { 157f8197bf0SPawel Jakub Dawidek _kvm_err(kd, kd->program, 158*c10970ddSUlrich Spörlein "can't read prison at %p", 159f8197bf0SPawel Jakub Dawidek ucred.cr_prison); 160f8197bf0SPawel Jakub Dawidek return (-1); 161f8197bf0SPawel Jakub Dawidek } 162f8197bf0SPawel Jakub Dawidek kp->ki_jid = pr.pr_id; 163f8197bf0SPawel Jakub Dawidek } 1641f7d2501SKirk McKusick } 16558f0484fSRodney W. Grimes 166694127f8SDaniel Eischen switch(what & ~KERN_PROC_INC_THREAD) { 16758f0484fSRodney W. Grimes 168276de18cSGarance A Drosehn case KERN_PROC_GID: 169276de18cSGarance A Drosehn if (kp->ki_groups[0] != (gid_t)arg) 170276de18cSGarance A Drosehn continue; 171276de18cSGarance A Drosehn break; 172276de18cSGarance A Drosehn 17358f0484fSRodney W. Grimes case KERN_PROC_PID: 17458f0484fSRodney W. Grimes if (proc.p_pid != (pid_t)arg) 17558f0484fSRodney W. Grimes continue; 17658f0484fSRodney W. Grimes break; 17758f0484fSRodney W. Grimes 178276de18cSGarance A Drosehn case KERN_PROC_RGID: 179276de18cSGarance A Drosehn if (kp->ki_rgid != (gid_t)arg) 180276de18cSGarance A Drosehn continue; 181276de18cSGarance A Drosehn break; 182276de18cSGarance A Drosehn 18358f0484fSRodney W. Grimes case KERN_PROC_UID: 1841f7d2501SKirk McKusick if (kp->ki_uid != (uid_t)arg) 18558f0484fSRodney W. Grimes continue; 18658f0484fSRodney W. Grimes break; 18758f0484fSRodney W. Grimes 18858f0484fSRodney W. Grimes case KERN_PROC_RUID: 1891f7d2501SKirk McKusick if (kp->ki_ruid != (uid_t)arg) 19058f0484fSRodney W. Grimes continue; 19158f0484fSRodney W. Grimes break; 19258f0484fSRodney W. Grimes } 19358f0484fSRodney W. Grimes /* 19458f0484fSRodney W. Grimes * We're going to add another proc to the set. If this 19558f0484fSRodney W. Grimes * will overflow the buffer, assume the reason is because 19658f0484fSRodney W. Grimes * nprocs (or the proc list) is corrupt and declare an error. 19758f0484fSRodney W. Grimes */ 19858f0484fSRodney W. Grimes if (cnt >= maxcnt) { 19958f0484fSRodney W. Grimes _kvm_err(kd, kd->program, "nprocs corrupt"); 20058f0484fSRodney W. Grimes return (-1); 20158f0484fSRodney W. Grimes } 20258f0484fSRodney W. Grimes /* 2031f7d2501SKirk McKusick * gather kinfo_proc 20458f0484fSRodney W. Grimes */ 2051f7d2501SKirk McKusick kp->ki_paddr = p; 2067a62aa8aSDavid Schultz kp->ki_addr = 0; /* XXX uarea */ 207b40ce416SJulian Elischer /* kp->ki_kstack = proc.p_thread.td_kstack; XXXKSE */ 2081f7d2501SKirk McKusick kp->ki_args = proc.p_args; 2098b7a975eSJohn Baldwin kp->ki_tracep = proc.p_tracevp; 2101f7d2501SKirk McKusick kp->ki_textvp = proc.p_textvp; 2111f7d2501SKirk McKusick kp->ki_fd = proc.p_fd; 2121f7d2501SKirk McKusick kp->ki_vmspace = proc.p_vmspace; 213840558b9SJohn Baldwin if (proc.p_sigacts != NULL) { 214840558b9SJohn Baldwin if (KREAD(kd, (u_long)proc.p_sigacts, &sigacts)) { 2151f7d2501SKirk McKusick _kvm_err(kd, kd->program, 216*c10970ddSUlrich Spörlein "can't read sigacts at %p", proc.p_sigacts); 2171f7d2501SKirk McKusick return (-1); 2181f7d2501SKirk McKusick } 219840558b9SJohn Baldwin kp->ki_sigignore = sigacts.ps_sigignore; 220840558b9SJohn Baldwin kp->ki_sigcatch = sigacts.ps_sigcatch; 2211f7d2501SKirk McKusick } 2228ef6b142SJeff Roberson #if 0 223b61ce5b0SJeff Roberson if ((proc.p_flag & P_INMEM) && proc.p_stats != NULL) { 2241f7d2501SKirk McKusick if (KREAD(kd, (u_long)proc.p_stats, &pstats)) { 2251f7d2501SKirk McKusick _kvm_err(kd, kd->program, 2261f7d2501SKirk McKusick "can't read stats at %x", proc.p_stats); 2271f7d2501SKirk McKusick return (-1); 2281f7d2501SKirk McKusick } 2291f7d2501SKirk McKusick kp->ki_start = pstats.p_start; 23009ff687cSJohn Baldwin 23109ff687cSJohn Baldwin /* 23209ff687cSJohn Baldwin * XXX: The times here are probably zero and need 23309ff687cSJohn Baldwin * to be calculated from the raw data in p_rux and 23409ff687cSJohn Baldwin * p_crux. 23509ff687cSJohn Baldwin */ 2361f7d2501SKirk McKusick kp->ki_rusage = pstats.p_ru; 237276de18cSGarance A Drosehn kp->ki_childstime = pstats.p_cru.ru_stime; 238276de18cSGarance A Drosehn kp->ki_childutime = pstats.p_cru.ru_utime; 239276de18cSGarance A Drosehn /* Some callers want child-times in a single value */ 240276de18cSGarance A Drosehn timeradd(&kp->ki_childstime, &kp->ki_childutime, 241276de18cSGarance A Drosehn &kp->ki_childtime); 2421f7d2501SKirk McKusick } 2438ef6b142SJeff Roberson #endif 244a58930d8STor Egge if (proc.p_oppid) 2451f7d2501SKirk McKusick kp->ki_ppid = proc.p_oppid; 246a58930d8STor Egge else if (proc.p_pptr) { 247a58930d8STor Egge if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) { 2481f7d2501SKirk McKusick _kvm_err(kd, kd->program, 249*c10970ddSUlrich Spörlein "can't read pproc at %p", proc.p_pptr); 250a58930d8STor Egge return (-1); 251a58930d8STor Egge } 2521f7d2501SKirk McKusick kp->ki_ppid = pproc.p_pid; 253a58930d8STor Egge } else 2541f7d2501SKirk McKusick kp->ki_ppid = 0; 2556f8c6a69SPeter Wemm if (proc.p_pgrp == NULL) 2566f8c6a69SPeter Wemm goto nopgrp; 2576f8c6a69SPeter Wemm if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) { 258*c10970ddSUlrich Spörlein _kvm_err(kd, kd->program, "can't read pgrp at %p", 2596f8c6a69SPeter Wemm proc.p_pgrp); 2606f8c6a69SPeter Wemm return (-1); 2616f8c6a69SPeter Wemm } 2621f7d2501SKirk McKusick kp->ki_pgid = pgrp.pg_id; 2631f7d2501SKirk McKusick kp->ki_jobc = pgrp.pg_jobc; 26458f0484fSRodney W. Grimes if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) { 265*c10970ddSUlrich Spörlein _kvm_err(kd, kd->program, "can't read session at %p", 26658f0484fSRodney W. Grimes pgrp.pg_session); 26758f0484fSRodney W. Grimes return (-1); 26858f0484fSRodney W. Grimes } 2691f7d2501SKirk McKusick kp->ki_sid = sess.s_sid; 2701f7d2501SKirk McKusick (void)memcpy(kp->ki_login, sess.s_login, 2711f7d2501SKirk McKusick sizeof(kp->ki_login)); 2721f7d2501SKirk McKusick kp->ki_kiflag = sess.s_ttyvp ? KI_CTTY : 0; 2731f7d2501SKirk McKusick if (sess.s_leader == p) 2741f7d2501SKirk McKusick kp->ki_kiflag |= KI_SLEADER; 27558f0484fSRodney W. Grimes if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) { 27658f0484fSRodney W. Grimes if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) { 27758f0484fSRodney W. Grimes _kvm_err(kd, kd->program, 278*c10970ddSUlrich Spörlein "can't read tty at %p", sess.s_ttyp); 27958f0484fSRodney W. Grimes return (-1); 28058f0484fSRodney W. Grimes } 2812bdd5609SPeter Wemm if (tty.t_dev != NULL) { 2822bdd5609SPeter Wemm if (KREAD(kd, (u_long)tty.t_dev, &t_cdev)) { 2832bdd5609SPeter Wemm _kvm_err(kd, kd->program, 284*c10970ddSUlrich Spörlein "can't read cdev at %p", 2852bdd5609SPeter Wemm tty.t_dev); 2862bdd5609SPeter Wemm return (-1); 2872bdd5609SPeter Wemm } 2889c4fb661SPoul-Henning Kamp #if 0 2892bdd5609SPeter Wemm kp->ki_tdev = t_cdev.si_udev; 2909c4fb661SPoul-Henning Kamp #else 2912cfe3fdaSPeter Wemm kp->ki_tdev = NODEV; 2929c4fb661SPoul-Henning Kamp #endif 2932bdd5609SPeter Wemm } 29458f0484fSRodney W. Grimes if (tty.t_pgrp != NULL) { 29558f0484fSRodney W. Grimes if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) { 29658f0484fSRodney W. Grimes _kvm_err(kd, kd->program, 297*c10970ddSUlrich Spörlein "can't read tpgrp at %p", 29858f0484fSRodney W. Grimes tty.t_pgrp); 29958f0484fSRodney W. Grimes return (-1); 30058f0484fSRodney W. Grimes } 3011f7d2501SKirk McKusick kp->ki_tpgid = pgrp.pg_id; 30258f0484fSRodney W. Grimes } else 3031f7d2501SKirk McKusick kp->ki_tpgid = -1; 3041f7d2501SKirk McKusick if (tty.t_session != NULL) { 3051f7d2501SKirk McKusick if (KREAD(kd, (u_long)tty.t_session, &sess)) { 3061f7d2501SKirk McKusick _kvm_err(kd, kd->program, 307*c10970ddSUlrich Spörlein "can't read session at %p", 3081f7d2501SKirk McKusick tty.t_session); 3091f7d2501SKirk McKusick return (-1); 3101f7d2501SKirk McKusick } 3111f7d2501SKirk McKusick kp->ki_tsid = sess.s_sid; 3121f7d2501SKirk McKusick } 3136f8c6a69SPeter Wemm } else { 3146f8c6a69SPeter Wemm nopgrp: 3151f7d2501SKirk McKusick kp->ki_tdev = NODEV; 3166f8c6a69SPeter Wemm } 31771fad9fdSJulian Elischer if ((proc.p_state != PRS_ZOMBIE) && mtd.td_wmesg) 31871fad9fdSJulian Elischer (void)kvm_read(kd, (u_long)mtd.td_wmesg, 3191f7d2501SKirk McKusick kp->ki_wmesg, WMESGLEN); 32058f0484fSRodney W. Grimes 32158f0484fSRodney W. Grimes (void)kvm_read(kd, (u_long)proc.p_vmspace, 3221f7d2501SKirk McKusick (char *)&vmspace, sizeof(vmspace)); 3231f7d2501SKirk McKusick kp->ki_size = vmspace.vm_map.size; 3245f494640SSean Bruno /* 3255f494640SSean Bruno * Approximate the kernel's method of calculating 3265f494640SSean Bruno * this field. 3275f494640SSean Bruno */ 3285f494640SSean Bruno #define pmap_resident_count(pm) ((pm)->pm_stats.resident_count) 3295f494640SSean Bruno kp->ki_rssize = pmap_resident_count(&vmspace.vm_pmap); 3301f7d2501SKirk McKusick kp->ki_swrss = vmspace.vm_swrss; 3311f7d2501SKirk McKusick kp->ki_tsize = vmspace.vm_tsize; 3321f7d2501SKirk McKusick kp->ki_dsize = vmspace.vm_dsize; 3331f7d2501SKirk McKusick kp->ki_ssize = vmspace.vm_ssize; 33458f0484fSRodney W. Grimes 335694127f8SDaniel Eischen switch (what & ~KERN_PROC_INC_THREAD) { 33658f0484fSRodney W. Grimes 33758f0484fSRodney W. Grimes case KERN_PROC_PGRP: 3381f7d2501SKirk McKusick if (kp->ki_pgid != (pid_t)arg) 33958f0484fSRodney W. Grimes continue; 34058f0484fSRodney W. Grimes break; 34158f0484fSRodney W. Grimes 342276de18cSGarance A Drosehn case KERN_PROC_SESSION: 343276de18cSGarance A Drosehn if (kp->ki_sid != (pid_t)arg) 344276de18cSGarance A Drosehn continue; 345276de18cSGarance A Drosehn break; 346276de18cSGarance A Drosehn 34758f0484fSRodney W. Grimes case KERN_PROC_TTY: 34858f0484fSRodney W. Grimes if ((proc.p_flag & P_CONTROLT) == 0 || 3491f7d2501SKirk McKusick kp->ki_tdev != (dev_t)arg) 35058f0484fSRodney W. Grimes continue; 35158f0484fSRodney W. Grimes break; 35258f0484fSRodney W. Grimes } 353b7e7c21aSGarance A Drosehn if (proc.p_comm[0] != 0) 354b7e7c21aSGarance A Drosehn strlcpy(kp->ki_comm, proc.p_comm, MAXCOMLEN); 355276de18cSGarance A Drosehn (void)kvm_read(kd, (u_long)proc.p_sysent, (char *)&sysent, 356276de18cSGarance A Drosehn sizeof(sysent)); 357276de18cSGarance A Drosehn (void)kvm_read(kd, (u_long)sysent.sv_name, (char *)&svname, 358276de18cSGarance A Drosehn sizeof(svname)); 359276de18cSGarance A Drosehn if (svname[0] != 0) 360276de18cSGarance A Drosehn strlcpy(kp->ki_emul, svname, KI_EMULNAMELEN); 361bff4151cSJulian Elischer if ((proc.p_state != PRS_ZOMBIE) && 36271fad9fdSJulian Elischer (mtd.td_blocked != 0)) { 3630d632649SJohn Baldwin kp->ki_kiflag |= KI_LOCKBLOCK; 3640d632649SJohn Baldwin if (mtd.td_lockname) 365bff4151cSJulian Elischer (void)kvm_read(kd, 3660d632649SJohn Baldwin (u_long)mtd.td_lockname, 3670d632649SJohn Baldwin kp->ki_lockname, LOCKNAMELEN); 3680d632649SJohn Baldwin kp->ki_lockname[LOCKNAMELEN] = 0; 3691f7d2501SKirk McKusick } 37042e43591SYaroslav Tykhiy /* 37142e43591SYaroslav Tykhiy * XXX: This is plain wrong, rux_runtime has nothing 37242e43591SYaroslav Tykhiy * to do with struct bintime, rux_runtime is just a 64-bit 37342e43591SYaroslav Tykhiy * integer counter of cputicks. What we need here is a way 37442e43591SYaroslav Tykhiy * to convert cputicks to usecs. The kernel does it in 37542e43591SYaroslav Tykhiy * kern/kern_tc.c, but the function can't be just copied. 37642e43591SYaroslav Tykhiy */ 37709ff687cSJohn Baldwin bintime2timeval(&proc.p_rux.rux_runtime, &tv); 37849b33de8SBrian Feldman kp->ki_runtime = (u_int64_t)tv.tv_sec * 1000000 + tv.tv_usec; 3791f7d2501SKirk McKusick kp->ki_pid = proc.p_pid; 380e8a58a83SJuli Mallett kp->ki_siglist = proc.p_siglist; 38142d3ad71SJeff Roberson SIGSETOR(kp->ki_siglist, mtd.td_siglist); 38231a9779eSJeff Roberson kp->ki_sigmask = mtd.td_sigmask; 3831f7d2501SKirk McKusick kp->ki_xstat = proc.p_xstat; 3841f7d2501SKirk McKusick kp->ki_acflag = proc.p_acflag; 3854f0db5e0SJulian Elischer kp->ki_lock = proc.p_lock; 386bff4151cSJulian Elischer if (proc.p_state != PRS_ZOMBIE) { 38784a0b303SJeff Roberson kp->ki_swtime = (ticks - proc.p_swtick) / hz; 3886143c383SJulian Elischer kp->ki_flag = proc.p_flag; 389b61ce5b0SJeff Roberson kp->ki_sflag = 0; 39030105366SJulian Elischer kp->ki_nice = proc.p_nice; 3911f7d2501SKirk McKusick kp->ki_traceflag = proc.p_traceflag; 3926143c383SJulian Elischer if (proc.p_state == PRS_NORMAL) { 39371fad9fdSJulian Elischer if (TD_ON_RUNQ(&mtd) || 39471fad9fdSJulian Elischer TD_CAN_RUN(&mtd) || 39571fad9fdSJulian Elischer TD_IS_RUNNING(&mtd)) { 396e602ba25SJulian Elischer kp->ki_stat = SRUN; 3974f0db5e0SJulian Elischer } else if (mtd.td_state == 3984f0db5e0SJulian Elischer TDS_INHIBITED) { 39971fad9fdSJulian Elischer if (P_SHOULDSTOP(&proc)) { 400e602ba25SJulian Elischer kp->ki_stat = SSTOP; 4014f0db5e0SJulian Elischer } else if ( 4024f0db5e0SJulian Elischer TD_IS_SLEEPING(&mtd)) { 40371fad9fdSJulian Elischer kp->ki_stat = SSLEEP; 4040d632649SJohn Baldwin } else if (TD_ON_LOCK(&mtd)) { 4050d632649SJohn Baldwin kp->ki_stat = SLOCK; 406e602ba25SJulian Elischer } else { 407e602ba25SJulian Elischer kp->ki_stat = SWAIT; 408e602ba25SJulian Elischer } 40971fad9fdSJulian Elischer } 410e602ba25SJulian Elischer } else { 411e602ba25SJulian Elischer kp->ki_stat = SIDL; 412e602ba25SJulian Elischer } 4134f0db5e0SJulian Elischer /* Stuff from the thread */ 41471fad9fdSJulian Elischer kp->ki_pri.pri_level = mtd.td_priority; 41571fad9fdSJulian Elischer kp->ki_pri.pri_native = mtd.td_base_pri; 41671fad9fdSJulian Elischer kp->ki_lastcpu = mtd.td_lastcpu; 4174f0db5e0SJulian Elischer kp->ki_wchan = mtd.td_wchan; 4187ab24ea3SJulian Elischer if (mtd.td_name[0] != 0) 419cfe127f5SJulian Elischer strlcpy(kp->ki_ocomm, mtd.td_name, MAXCOMLEN); 420a0ddbf49SJulian Elischer kp->ki_oncpu = mtd.td_oncpu; 4217ab24ea3SJulian Elischer if (mtd.td_name[0] != '\0') 4227ab24ea3SJulian Elischer strlcpy(kp->ki_ocomm, mtd.td_name, sizeof(kp->ki_ocomm)); 423ed062c8dSJulian Elischer kp->ki_pctcpu = 0; 424ed062c8dSJulian Elischer kp->ki_rqindex = 0; 4254f0db5e0SJulian Elischer } else { 4266143c383SJulian Elischer kp->ki_stat = SZOMB; 4276143c383SJulian Elischer } 4281f7d2501SKirk McKusick bcopy(&kinfo_proc, bp, sizeof(kinfo_proc)); 42958f0484fSRodney W. Grimes ++bp; 43058f0484fSRodney W. Grimes ++cnt; 43158f0484fSRodney W. Grimes } 43258f0484fSRodney W. Grimes return (cnt); 43358f0484fSRodney W. Grimes } 43458f0484fSRodney W. Grimes 43558f0484fSRodney W. Grimes /* 43658f0484fSRodney W. Grimes * Build proc info array by reading in proc list from a crash dump. 43758f0484fSRodney W. Grimes * Return number of procs read. maxcnt is the max we will read. 43858f0484fSRodney W. Grimes */ 43958f0484fSRodney W. Grimes static int 440*c10970ddSUlrich Spörlein kvm_deadprocs(kvm_t *kd, int what, int arg, u_long a_allproc, 441*c10970ddSUlrich Spörlein u_long a_zombproc, int maxcnt) 44258f0484fSRodney W. Grimes { 443be04b6d1SDavid E. O'Brien struct kinfo_proc *bp = kd->procbase; 444be04b6d1SDavid E. O'Brien int acnt, zcnt; 44558f0484fSRodney W. Grimes struct proc *p; 44658f0484fSRodney W. Grimes 44758f0484fSRodney W. Grimes if (KREAD(kd, a_allproc, &p)) { 44858f0484fSRodney W. Grimes _kvm_err(kd, kd->program, "cannot read allproc"); 44958f0484fSRodney W. Grimes return (-1); 45058f0484fSRodney W. Grimes } 45158f0484fSRodney W. Grimes acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt); 45258f0484fSRodney W. Grimes if (acnt < 0) 45358f0484fSRodney W. Grimes return (acnt); 45458f0484fSRodney W. Grimes 45558f0484fSRodney W. Grimes if (KREAD(kd, a_zombproc, &p)) { 45658f0484fSRodney W. Grimes _kvm_err(kd, kd->program, "cannot read zombproc"); 45758f0484fSRodney W. Grimes return (-1); 45858f0484fSRodney W. Grimes } 45958f0484fSRodney W. Grimes zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt); 46058f0484fSRodney W. Grimes if (zcnt < 0) 46158f0484fSRodney W. Grimes zcnt = 0; 46258f0484fSRodney W. Grimes 46358f0484fSRodney W. Grimes return (acnt + zcnt); 46458f0484fSRodney W. Grimes } 46558f0484fSRodney W. Grimes 46658f0484fSRodney W. Grimes struct kinfo_proc * 467*c10970ddSUlrich Spörlein kvm_getprocs(kvm_t *kd, int op, int arg, int *cnt) 46858f0484fSRodney W. Grimes { 469c2ac238cSDoug Rabson int mib[4], st, nprocs; 470c2ac238cSDoug Rabson size_t size; 471694127f8SDaniel Eischen int temp_op; 47258f0484fSRodney W. Grimes 47358f0484fSRodney W. Grimes if (kd->procbase != 0) { 47458f0484fSRodney W. Grimes free((void *)kd->procbase); 47558f0484fSRodney W. Grimes /* 47658f0484fSRodney W. Grimes * Clear this pointer in case this call fails. Otherwise, 47758f0484fSRodney W. Grimes * kvm_close() will free it again. 47858f0484fSRodney W. Grimes */ 47958f0484fSRodney W. Grimes kd->procbase = 0; 48058f0484fSRodney W. Grimes } 48158f0484fSRodney W. Grimes if (ISALIVE(kd)) { 48258f0484fSRodney W. Grimes size = 0; 48358f0484fSRodney W. Grimes mib[0] = CTL_KERN; 48458f0484fSRodney W. Grimes mib[1] = KERN_PROC; 48558f0484fSRodney W. Grimes mib[2] = op; 48658f0484fSRodney W. Grimes mib[3] = arg; 487694127f8SDaniel Eischen temp_op = op & ~KERN_PROC_INC_THREAD; 488694127f8SDaniel Eischen st = sysctl(mib, 489694127f8SDaniel Eischen temp_op == KERN_PROC_ALL || temp_op == KERN_PROC_PROC ? 490f2dd06abSTim J. Robbins 3 : 4, NULL, &size, NULL, 0); 49158f0484fSRodney W. Grimes if (st == -1) { 49258f0484fSRodney W. Grimes _kvm_syserr(kd, kd->program, "kvm_getprocs"); 49358f0484fSRodney W. Grimes return (0); 49458f0484fSRodney W. Grimes } 49521687047SDima Dorfman /* 49621687047SDima Dorfman * We can't continue with a size of 0 because we pass 49721687047SDima Dorfman * it to realloc() (via _kvm_realloc()), and passing 0 49821687047SDima Dorfman * to realloc() results in undefined behavior. 49921687047SDima Dorfman */ 50021687047SDima Dorfman if (size == 0) { 50121687047SDima Dorfman /* 50221687047SDima Dorfman * XXX: We should probably return an invalid, 50321687047SDima Dorfman * but non-NULL, pointer here so any client 50421687047SDima Dorfman * program trying to dereference it will 50521687047SDima Dorfman * crash. However, _kvm_freeprocs() calls 50621687047SDima Dorfman * free() on kd->procbase if it isn't NULL, 50721687047SDima Dorfman * and free()'ing a junk pointer isn't good. 50821687047SDima Dorfman * Then again, _kvm_freeprocs() isn't used 50921687047SDima Dorfman * anywhere . . . 51021687047SDima Dorfman */ 51121687047SDima Dorfman kd->procbase = _kvm_malloc(kd, 1); 51221687047SDima Dorfman goto liveout; 51321687047SDima Dorfman } 514b2d3d0f0SDag-Erling Smørgrav do { 515b2d3d0f0SDag-Erling Smørgrav size += size / 10; 516b2d3d0f0SDag-Erling Smørgrav kd->procbase = (struct kinfo_proc *) 517b2d3d0f0SDag-Erling Smørgrav _kvm_realloc(kd, kd->procbase, size); 51858f0484fSRodney W. Grimes if (kd->procbase == 0) 51958f0484fSRodney W. Grimes return (0); 520694127f8SDaniel Eischen st = sysctl(mib, temp_op == KERN_PROC_ALL || 521694127f8SDaniel Eischen temp_op == KERN_PROC_PROC ? 3 : 4, 522b2d3d0f0SDag-Erling Smørgrav kd->procbase, &size, NULL, 0); 523b2d3d0f0SDag-Erling Smørgrav } while (st == -1 && errno == ENOMEM); 52458f0484fSRodney W. Grimes if (st == -1) { 52558f0484fSRodney W. Grimes _kvm_syserr(kd, kd->program, "kvm_getprocs"); 52658f0484fSRodney W. Grimes return (0); 52758f0484fSRodney W. Grimes } 52821687047SDima Dorfman /* 52921687047SDima Dorfman * We have to check the size again because sysctl() 53021687047SDima Dorfman * may "round up" oldlenp if oldp is NULL; hence it 53121687047SDima Dorfman * might've told us that there was data to get when 53221687047SDima Dorfman * there really isn't any. 53321687047SDima Dorfman */ 5340627f53bSDavid Malone if (size > 0 && 5350627f53bSDavid Malone kd->procbase->ki_structsize != sizeof(struct kinfo_proc)) { 53658f0484fSRodney W. Grimes _kvm_err(kd, kd->program, 537*c10970ddSUlrich Spörlein "kinfo_proc size mismatch (expected %zu, got %d)", 5381f7d2501SKirk McKusick sizeof(struct kinfo_proc), 5391f7d2501SKirk McKusick kd->procbase->ki_structsize); 54058f0484fSRodney W. Grimes return (0); 54158f0484fSRodney W. Grimes } 54221687047SDima Dorfman liveout: 543d339edcfSDavid Malone nprocs = size == 0 ? 0 : size / kd->procbase->ki_structsize; 54458f0484fSRodney W. Grimes } else { 54584a0b303SJeff Roberson struct nlist nl[6], *p; 54658f0484fSRodney W. Grimes 54758f0484fSRodney W. Grimes nl[0].n_name = "_nprocs"; 54858f0484fSRodney W. Grimes nl[1].n_name = "_allproc"; 54958f0484fSRodney W. Grimes nl[2].n_name = "_zombproc"; 55084a0b303SJeff Roberson nl[3].n_name = "_ticks"; 55184a0b303SJeff Roberson nl[4].n_name = "_hz"; 55284a0b303SJeff Roberson nl[5].n_name = 0; 55358f0484fSRodney W. Grimes 55458f0484fSRodney W. Grimes if (kvm_nlist(kd, nl) != 0) { 55558f0484fSRodney W. Grimes for (p = nl; p->n_type != 0; ++p) 55658f0484fSRodney W. Grimes ; 55758f0484fSRodney W. Grimes _kvm_err(kd, kd->program, 55858f0484fSRodney W. Grimes "%s: no such symbol", p->n_name); 55958f0484fSRodney W. Grimes return (0); 56058f0484fSRodney W. Grimes } 56158f0484fSRodney W. Grimes if (KREAD(kd, nl[0].n_value, &nprocs)) { 56258f0484fSRodney W. Grimes _kvm_err(kd, kd->program, "can't read nprocs"); 56358f0484fSRodney W. Grimes return (0); 56458f0484fSRodney W. Grimes } 56584a0b303SJeff Roberson if (KREAD(kd, nl[3].n_value, &ticks)) { 56684a0b303SJeff Roberson _kvm_err(kd, kd->program, "can't read ticks"); 56784a0b303SJeff Roberson return (0); 56884a0b303SJeff Roberson } 56984a0b303SJeff Roberson if (KREAD(kd, nl[4].n_value, &hz)) { 57084a0b303SJeff Roberson _kvm_err(kd, kd->program, "can't read hz"); 57184a0b303SJeff Roberson return (0); 57284a0b303SJeff Roberson } 57358f0484fSRodney W. Grimes size = nprocs * sizeof(struct kinfo_proc); 57458f0484fSRodney W. Grimes kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size); 57558f0484fSRodney W. Grimes if (kd->procbase == 0) 57658f0484fSRodney W. Grimes return (0); 57758f0484fSRodney W. Grimes 57858f0484fSRodney W. Grimes nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value, 57958f0484fSRodney W. Grimes nl[2].n_value, nprocs); 58058f0484fSRodney W. Grimes #ifdef notdef 58158f0484fSRodney W. Grimes size = nprocs * sizeof(struct kinfo_proc); 58258f0484fSRodney W. Grimes (void)realloc(kd->procbase, size); 58358f0484fSRodney W. Grimes #endif 58458f0484fSRodney W. Grimes } 58558f0484fSRodney W. Grimes *cnt = nprocs; 58658f0484fSRodney W. Grimes return (kd->procbase); 58758f0484fSRodney W. Grimes } 58858f0484fSRodney W. Grimes 58958f0484fSRodney W. Grimes void 590*c10970ddSUlrich Spörlein _kvm_freeprocs(kvm_t *kd) 59158f0484fSRodney W. Grimes { 59258f0484fSRodney W. Grimes if (kd->procbase) { 59358f0484fSRodney W. Grimes free(kd->procbase); 59458f0484fSRodney W. Grimes kd->procbase = 0; 59558f0484fSRodney W. Grimes } 59658f0484fSRodney W. Grimes } 59758f0484fSRodney W. Grimes 59858f0484fSRodney W. Grimes void * 599*c10970ddSUlrich Spörlein _kvm_realloc(kvm_t *kd, void *p, size_t n) 60058f0484fSRodney W. Grimes { 60158f0484fSRodney W. Grimes void *np = (void *)realloc(p, n); 60258f0484fSRodney W. Grimes 603e8420087SWarner Losh if (np == 0) { 604e8420087SWarner Losh free(p); 60558f0484fSRodney W. Grimes _kvm_err(kd, kd->program, "out of memory"); 606e8420087SWarner Losh } 60758f0484fSRodney W. Grimes return (np); 60858f0484fSRodney W. Grimes } 60958f0484fSRodney W. Grimes 61058f0484fSRodney W. Grimes #ifndef MAX 61158f0484fSRodney W. Grimes #define MAX(a, b) ((a) > (b) ? (a) : (b)) 61258f0484fSRodney W. Grimes #endif 61358f0484fSRodney W. Grimes 61458f0484fSRodney W. Grimes /* 6151f7d2501SKirk McKusick * Read in an argument vector from the user address space of process kp. 61677721f53SPeter Wemm * addr if the user-space base address of narg null-terminated contiguous 61758f0484fSRodney W. Grimes * strings. This is used to read in both the command arguments and 61858f0484fSRodney W. Grimes * environment strings. Read at most maxcnt characters of strings. 61958f0484fSRodney W. Grimes */ 62058f0484fSRodney W. Grimes static char ** 621*c10970ddSUlrich Spörlein kvm_argv(kvm_t *kd, const struct kinfo_proc *kp, u_long addr, int narg, 622*c10970ddSUlrich Spörlein int maxcnt) 62358f0484fSRodney W. Grimes { 624be04b6d1SDavid E. O'Brien char *np, *cp, *ep, *ap; 625be04b6d1SDavid E. O'Brien u_long oaddr = -1; 626be04b6d1SDavid E. O'Brien int len, cc; 627be04b6d1SDavid E. O'Brien char **argv; 62858f0484fSRodney W. Grimes 62958f0484fSRodney W. Grimes /* 630*c10970ddSUlrich Spörlein * Check that there aren't an unreasonable number of arguments, 631*c10970ddSUlrich Spörlein * and that the address is in user space. Special test for 632*c10970ddSUlrich Spörlein * VM_MIN_ADDRESS as it evaluates to zero, but is not a simple zero 633*c10970ddSUlrich Spörlein * constant for some archs. We cannot use the pre-processor here and 634*c10970ddSUlrich Spörlein * for some archs the compiler would trigger a signedness warning. 63558f0484fSRodney W. Grimes */ 636*c10970ddSUlrich Spörlein if (narg > 512 || addr + 1 < VM_MIN_ADDRESS + 1 || addr >= VM_MAXUSER_ADDRESS) 63758f0484fSRodney W. Grimes return (0); 63858f0484fSRodney W. Grimes 6396b492ae4SPeter Wemm /* 6406b492ae4SPeter Wemm * kd->argv : work space for fetching the strings from the target 6416b492ae4SPeter Wemm * process's space, and is converted for returning to caller 6426b492ae4SPeter Wemm */ 64358f0484fSRodney W. Grimes if (kd->argv == 0) { 64458f0484fSRodney W. Grimes /* 64558f0484fSRodney W. Grimes * Try to avoid reallocs. 64658f0484fSRodney W. Grimes */ 64758f0484fSRodney W. Grimes kd->argc = MAX(narg + 1, 32); 64858f0484fSRodney W. Grimes kd->argv = (char **)_kvm_malloc(kd, kd->argc * 64958f0484fSRodney W. Grimes sizeof(*kd->argv)); 65058f0484fSRodney W. Grimes if (kd->argv == 0) 65158f0484fSRodney W. Grimes return (0); 65258f0484fSRodney W. Grimes } else if (narg + 1 > kd->argc) { 65358f0484fSRodney W. Grimes kd->argc = MAX(2 * kd->argc, narg + 1); 65458f0484fSRodney W. Grimes kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc * 65558f0484fSRodney W. Grimes sizeof(*kd->argv)); 65658f0484fSRodney W. Grimes if (kd->argv == 0) 65758f0484fSRodney W. Grimes return (0); 65858f0484fSRodney W. Grimes } 6596b492ae4SPeter Wemm /* 6606b492ae4SPeter Wemm * kd->argspc : returned to user, this is where the kd->argv 6616b492ae4SPeter Wemm * arrays are left pointing to the collected strings. 6626b492ae4SPeter Wemm */ 66358f0484fSRodney W. Grimes if (kd->argspc == 0) { 6642572133eSPoul-Henning Kamp kd->argspc = (char *)_kvm_malloc(kd, PAGE_SIZE); 66558f0484fSRodney W. Grimes if (kd->argspc == 0) 66658f0484fSRodney W. Grimes return (0); 6672572133eSPoul-Henning Kamp kd->arglen = PAGE_SIZE; 66858f0484fSRodney W. Grimes } 6696b492ae4SPeter Wemm /* 6706b492ae4SPeter Wemm * kd->argbuf : used to pull in pages from the target process. 6716b492ae4SPeter Wemm * the strings are copied out of here. 6726b492ae4SPeter Wemm */ 67377721f53SPeter Wemm if (kd->argbuf == 0) { 6742572133eSPoul-Henning Kamp kd->argbuf = (char *)_kvm_malloc(kd, PAGE_SIZE); 67577721f53SPeter Wemm if (kd->argbuf == 0) 67677721f53SPeter Wemm return (0); 67777721f53SPeter Wemm } 6786b492ae4SPeter Wemm 6796b492ae4SPeter Wemm /* Pull in the target process'es argv vector */ 68077721f53SPeter Wemm cc = sizeof(char *) * narg; 6811f7d2501SKirk McKusick if (kvm_uread(kd, kp, addr, (char *)kd->argv, cc) != cc) 68277721f53SPeter Wemm return (0); 6836b492ae4SPeter Wemm /* 6846b492ae4SPeter Wemm * ap : saved start address of string we're working on in kd->argspc 6856b492ae4SPeter Wemm * np : pointer to next place to write in kd->argspc 6866b492ae4SPeter Wemm * len: length of data in kd->argspc 6876b492ae4SPeter Wemm * argv: pointer to the argv vector that we are hunting around the 6886b492ae4SPeter Wemm * target process space for, and converting to addresses in 6896b492ae4SPeter Wemm * our address space (kd->argspc). 6906b492ae4SPeter Wemm */ 69177721f53SPeter Wemm ap = np = kd->argspc; 69258f0484fSRodney W. Grimes argv = kd->argv; 69358f0484fSRodney W. Grimes len = 0; 69458f0484fSRodney W. Grimes /* 69558f0484fSRodney W. Grimes * Loop over pages, filling in the argument vector. 6966b492ae4SPeter Wemm * Note that the argv strings could be pointing *anywhere* in 6976b492ae4SPeter Wemm * the user address space and are no longer contiguous. 6986b492ae4SPeter Wemm * Note that *argv is modified when we are going to fetch a string 6996b492ae4SPeter Wemm * that crosses a page boundary. We copy the next part of the string 7006b492ae4SPeter Wemm * into to "np" and eventually convert the pointer. 70158f0484fSRodney W. Grimes */ 70277721f53SPeter Wemm while (argv < kd->argv + narg && *argv != 0) { 7036b492ae4SPeter Wemm 7046b492ae4SPeter Wemm /* get the address that the current argv string is on */ 7052572133eSPoul-Henning Kamp addr = (u_long)*argv & ~(PAGE_SIZE - 1); 7066b492ae4SPeter Wemm 7076b492ae4SPeter Wemm /* is it the same page as the last one? */ 70877721f53SPeter Wemm if (addr != oaddr) { 7091f7d2501SKirk McKusick if (kvm_uread(kd, kp, addr, kd->argbuf, PAGE_SIZE) != 7102572133eSPoul-Henning Kamp PAGE_SIZE) 71177721f53SPeter Wemm return (0); 71277721f53SPeter Wemm oaddr = addr; 71377721f53SPeter Wemm } 7146b492ae4SPeter Wemm 7156b492ae4SPeter Wemm /* offset within the page... kd->argbuf */ 7162572133eSPoul-Henning Kamp addr = (u_long)*argv & (PAGE_SIZE - 1); 7176b492ae4SPeter Wemm 7186b492ae4SPeter Wemm /* cp = start of string, cc = count of chars in this chunk */ 71977721f53SPeter Wemm cp = kd->argbuf + addr; 7202572133eSPoul-Henning Kamp cc = PAGE_SIZE - addr; 7216b492ae4SPeter Wemm 7226b492ae4SPeter Wemm /* dont get more than asked for by user process */ 72358f0484fSRodney W. Grimes if (maxcnt > 0 && cc > maxcnt - len) 7246b492ae4SPeter Wemm cc = maxcnt - len; 7256b492ae4SPeter Wemm 7266b492ae4SPeter Wemm /* pointer to end of string if we found it in this page */ 72777721f53SPeter Wemm ep = memchr(cp, '\0', cc); 72877721f53SPeter Wemm if (ep != 0) 72977721f53SPeter Wemm cc = ep - cp + 1; 7306b492ae4SPeter Wemm /* 7316b492ae4SPeter Wemm * at this point, cc is the count of the chars that we are 7326b492ae4SPeter Wemm * going to retrieve this time. we may or may not have found 7336b492ae4SPeter Wemm * the end of it. (ep points to the null if the end is known) 7346b492ae4SPeter Wemm */ 7356b492ae4SPeter Wemm 7366b492ae4SPeter Wemm /* will we exceed the malloc/realloced buffer? */ 73758f0484fSRodney W. Grimes if (len + cc > kd->arglen) { 738be04b6d1SDavid E. O'Brien int off; 739be04b6d1SDavid E. O'Brien char **pp; 740be04b6d1SDavid E. O'Brien char *op = kd->argspc; 74158f0484fSRodney W. Grimes 74258f0484fSRodney W. Grimes kd->arglen *= 2; 74358f0484fSRodney W. Grimes kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, 74458f0484fSRodney W. Grimes kd->arglen); 74558f0484fSRodney W. Grimes if (kd->argspc == 0) 74658f0484fSRodney W. Grimes return (0); 74758f0484fSRodney W. Grimes /* 74858f0484fSRodney W. Grimes * Adjust argv pointers in case realloc moved 74958f0484fSRodney W. Grimes * the string space. 75058f0484fSRodney W. Grimes */ 75158f0484fSRodney W. Grimes off = kd->argspc - op; 75277721f53SPeter Wemm for (pp = kd->argv; pp < argv; pp++) 75358f0484fSRodney W. Grimes *pp += off; 75477721f53SPeter Wemm ap += off; 75577721f53SPeter Wemm np += off; 75658f0484fSRodney W. Grimes } 7576b492ae4SPeter Wemm /* np = where to put the next part of the string in kd->argspc*/ 7586b492ae4SPeter Wemm /* np is kinda redundant.. could use "kd->argspc + len" */ 75977721f53SPeter Wemm memcpy(np, cp, cc); 7606b492ae4SPeter Wemm np += cc; /* inc counters */ 76158f0484fSRodney W. Grimes len += cc; 7626b492ae4SPeter Wemm 7636b492ae4SPeter Wemm /* 7646b492ae4SPeter Wemm * if end of string found, set the *argv pointer to the 7656b492ae4SPeter Wemm * saved beginning of string, and advance. argv points to 7666b492ae4SPeter Wemm * somewhere in kd->argv.. This is initially relative 7676b492ae4SPeter Wemm * to the target process, but when we close it off, we set 7686b492ae4SPeter Wemm * it to point in our address space. 7696b492ae4SPeter Wemm */ 77077721f53SPeter Wemm if (ep != 0) { 77177721f53SPeter Wemm *argv++ = ap; 77277721f53SPeter Wemm ap = np; 7736b492ae4SPeter Wemm } else { 7746b492ae4SPeter Wemm /* update the address relative to the target process */ 77577721f53SPeter Wemm *argv += cc; 7766b492ae4SPeter Wemm } 7776b492ae4SPeter Wemm 77858f0484fSRodney W. Grimes if (maxcnt > 0 && len >= maxcnt) { 77958f0484fSRodney W. Grimes /* 78058f0484fSRodney W. Grimes * We're stopping prematurely. Terminate the 78177721f53SPeter Wemm * current string. 78258f0484fSRodney W. Grimes */ 78377721f53SPeter Wemm if (ep == 0) { 78477721f53SPeter Wemm *np = '\0'; 78577721f53SPeter Wemm *argv++ = ap; 78677721f53SPeter Wemm } 78777721f53SPeter Wemm break; 78877721f53SPeter Wemm } 78977721f53SPeter Wemm } 79077721f53SPeter Wemm /* Make sure argv is terminated. */ 79177721f53SPeter Wemm *argv = 0; 79258f0484fSRodney W. Grimes return (kd->argv); 79358f0484fSRodney W. Grimes } 79458f0484fSRodney W. Grimes 79558f0484fSRodney W. Grimes static void 796*c10970ddSUlrich Spörlein ps_str_a(struct ps_strings *p, u_long *addr, int *n) 79758f0484fSRodney W. Grimes { 79858f0484fSRodney W. Grimes *addr = (u_long)p->ps_argvstr; 79958f0484fSRodney W. Grimes *n = p->ps_nargvstr; 80058f0484fSRodney W. Grimes } 80158f0484fSRodney W. Grimes 80258f0484fSRodney W. Grimes static void 803*c10970ddSUlrich Spörlein ps_str_e (struct ps_strings *p, u_long *addr, int *n) 80458f0484fSRodney W. Grimes { 80558f0484fSRodney W. Grimes *addr = (u_long)p->ps_envstr; 80658f0484fSRodney W. Grimes *n = p->ps_nenvstr; 80758f0484fSRodney W. Grimes } 80858f0484fSRodney W. Grimes 80958f0484fSRodney W. Grimes /* 81058f0484fSRodney W. Grimes * Determine if the proc indicated by p is still active. 81158f0484fSRodney W. Grimes * This test is not 100% foolproof in theory, but chances of 81258f0484fSRodney W. Grimes * being wrong are very low. 81358f0484fSRodney W. Grimes */ 81458f0484fSRodney W. Grimes static int 815*c10970ddSUlrich Spörlein proc_verify(const struct kinfo_proc *curkp) 81658f0484fSRodney W. Grimes { 8171f7d2501SKirk McKusick struct kinfo_proc newkp; 81860160c5eSBruce Evans int mib[4]; 819c2ac238cSDoug Rabson size_t len; 82058f0484fSRodney W. Grimes 82135e6b695SPoul-Henning Kamp mib[0] = CTL_KERN; 82235e6b695SPoul-Henning Kamp mib[1] = KERN_PROC; 82335e6b695SPoul-Henning Kamp mib[2] = KERN_PROC_PID; 8241f7d2501SKirk McKusick mib[3] = curkp->ki_pid; 8251f7d2501SKirk McKusick len = sizeof(newkp); 8261f7d2501SKirk McKusick if (sysctl(mib, 4, &newkp, &len, NULL, 0) == -1) 82758f0484fSRodney W. Grimes return (0); 8281f7d2501SKirk McKusick return (curkp->ki_pid == newkp.ki_pid && 8291f7d2501SKirk McKusick (newkp.ki_stat != SZOMB || curkp->ki_stat == SZOMB)); 83058f0484fSRodney W. Grimes } 83158f0484fSRodney W. Grimes 83258f0484fSRodney W. Grimes static char ** 833*c10970ddSUlrich Spörlein kvm_doargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr, 834*c10970ddSUlrich Spörlein void (*info)(struct ps_strings *, u_long *, int *)) 83558f0484fSRodney W. Grimes { 8361f7d2501SKirk McKusick char **ap; 83758f0484fSRodney W. Grimes u_long addr; 83858f0484fSRodney W. Grimes int cnt; 83960160c5eSBruce Evans static struct ps_strings arginfo; 84060160c5eSBruce Evans static u_long ps_strings; 8417350dd84SPeter Wemm size_t len; 8427350dd84SPeter Wemm 8435aaa432dSJens Schweikhardt if (ps_strings == 0) { 84460160c5eSBruce Evans len = sizeof(ps_strings); 84560160c5eSBruce Evans if (sysctlbyname("kern.ps_strings", &ps_strings, &len, NULL, 84660160c5eSBruce Evans 0) == -1) 8477350dd84SPeter Wemm ps_strings = PS_STRINGS; 84835e6b695SPoul-Henning Kamp } 84958f0484fSRodney W. Grimes 85058f0484fSRodney W. Grimes /* 85158f0484fSRodney W. Grimes * Pointers are stored at the top of the user stack. 85258f0484fSRodney W. Grimes */ 8531f7d2501SKirk McKusick if (kp->ki_stat == SZOMB || 8541f7d2501SKirk McKusick kvm_uread(kd, kp, ps_strings, (char *)&arginfo, 85577721f53SPeter Wemm sizeof(arginfo)) != sizeof(arginfo)) 85658f0484fSRodney W. Grimes return (0); 85758f0484fSRodney W. Grimes 85858f0484fSRodney W. Grimes (*info)(&arginfo, &addr, &cnt); 85977721f53SPeter Wemm if (cnt == 0) 86077721f53SPeter Wemm return (0); 8611f7d2501SKirk McKusick ap = kvm_argv(kd, kp, addr, cnt, nchr); 86258f0484fSRodney W. Grimes /* 86358f0484fSRodney W. Grimes * For live kernels, make sure this process didn't go away. 86458f0484fSRodney W. Grimes */ 8651f7d2501SKirk McKusick if (ap != 0 && ISALIVE(kd) && !proc_verify(kp)) 86658f0484fSRodney W. Grimes ap = 0; 86758f0484fSRodney W. Grimes return (ap); 86858f0484fSRodney W. Grimes } 86958f0484fSRodney W. Grimes 87058f0484fSRodney W. Grimes /* 87158f0484fSRodney W. Grimes * Get the command args. This code is now machine independent. 87258f0484fSRodney W. Grimes */ 87358f0484fSRodney W. Grimes char ** 874*c10970ddSUlrich Spörlein kvm_getargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr) 87558f0484fSRodney W. Grimes { 876b9df5231SPoul-Henning Kamp int oid[4]; 877b7875890SDavid E. O'Brien int i; 878b7875890SDavid E. O'Brien size_t bufsz; 87915d2f5f0SPeter Wemm static unsigned long buflen; 880b9df5231SPoul-Henning Kamp static char *buf, *p; 881b9df5231SPoul-Henning Kamp static char **bufp; 882b9df5231SPoul-Henning Kamp static int argc; 883b9df5231SPoul-Henning Kamp 884c4a7cdb3SPeter Wemm if (!ISALIVE(kd)) { 885c4a7cdb3SPeter Wemm _kvm_err(kd, kd->program, 886c4a7cdb3SPeter Wemm "cannot read user space from dead kernel"); 887c4a7cdb3SPeter Wemm return (0); 888c4a7cdb3SPeter Wemm } 889c4a7cdb3SPeter Wemm 890b9df5231SPoul-Henning Kamp if (!buflen) { 891b7875890SDavid E. O'Brien bufsz = sizeof(buflen); 892b9df5231SPoul-Henning Kamp i = sysctlbyname("kern.ps_arg_cache_limit", 893b7875890SDavid E. O'Brien &buflen, &bufsz, NULL, 0); 894b9df5231SPoul-Henning Kamp if (i == -1) { 895b7875890SDavid E. O'Brien buflen = 0; 896b9df5231SPoul-Henning Kamp } else { 897b9df5231SPoul-Henning Kamp buf = malloc(buflen); 898b9df5231SPoul-Henning Kamp if (buf == NULL) 899b9df5231SPoul-Henning Kamp buflen = 0; 900b9df5231SPoul-Henning Kamp argc = 32; 901b9df5231SPoul-Henning Kamp bufp = malloc(sizeof(char *) * argc); 902b9df5231SPoul-Henning Kamp } 903b9df5231SPoul-Henning Kamp } 904b9df5231SPoul-Henning Kamp if (buf != NULL) { 905b9df5231SPoul-Henning Kamp oid[0] = CTL_KERN; 906b9df5231SPoul-Henning Kamp oid[1] = KERN_PROC; 907b9df5231SPoul-Henning Kamp oid[2] = KERN_PROC_ARGS; 9081f7d2501SKirk McKusick oid[3] = kp->ki_pid; 909b7875890SDavid E. O'Brien bufsz = buflen; 910b7875890SDavid E. O'Brien i = sysctl(oid, 4, buf, &bufsz, 0, 0); 911b7875890SDavid E. O'Brien if (i == 0 && bufsz > 0) { 912b9df5231SPoul-Henning Kamp i = 0; 913b9df5231SPoul-Henning Kamp p = buf; 914b9df5231SPoul-Henning Kamp do { 915b9df5231SPoul-Henning Kamp bufp[i++] = p; 916b9df5231SPoul-Henning Kamp p += strlen(p) + 1; 917b9df5231SPoul-Henning Kamp if (i >= argc) { 918b9df5231SPoul-Henning Kamp argc += argc; 919b9df5231SPoul-Henning Kamp bufp = realloc(bufp, 920b9df5231SPoul-Henning Kamp sizeof(char *) * argc); 921b9df5231SPoul-Henning Kamp } 922b7875890SDavid E. O'Brien } while (p < buf + bufsz); 923b9df5231SPoul-Henning Kamp bufp[i++] = 0; 924b9df5231SPoul-Henning Kamp return (bufp); 925b9df5231SPoul-Henning Kamp } 926b9df5231SPoul-Henning Kamp } 9271f7d2501SKirk McKusick if (kp->ki_flag & P_SYSTEM) 928b9df5231SPoul-Henning Kamp return (NULL); 92958f0484fSRodney W. Grimes return (kvm_doargv(kd, kp, nchr, ps_str_a)); 93058f0484fSRodney W. Grimes } 93158f0484fSRodney W. Grimes 93258f0484fSRodney W. Grimes char ** 933*c10970ddSUlrich Spörlein kvm_getenvv(kvm_t *kd, const struct kinfo_proc *kp, int nchr) 93458f0484fSRodney W. Grimes { 93558f0484fSRodney W. Grimes return (kvm_doargv(kd, kp, nchr, ps_str_e)); 93658f0484fSRodney W. Grimes } 93758f0484fSRodney W. Grimes 93858f0484fSRodney W. Grimes /* 93958f0484fSRodney W. Grimes * Read from user space. The user context is given by p. 94058f0484fSRodney W. Grimes */ 94158f0484fSRodney W. Grimes ssize_t 942*c10970ddSUlrich Spörlein kvm_uread(kvm_t *kd, const struct kinfo_proc *kp, u_long uva, char *buf, 943*c10970ddSUlrich Spörlein size_t len) 94458f0484fSRodney W. Grimes { 945be04b6d1SDavid E. O'Brien char *cp; 946338c7541SDavid Greenman char procfile[MAXPATHLEN]; 947338c7541SDavid Greenman ssize_t amount; 948338c7541SDavid Greenman int fd; 94958f0484fSRodney W. Grimes 9507350dd84SPeter Wemm if (!ISALIVE(kd)) { 951d7fb4b13SBruce Evans _kvm_err(kd, kd->program, 952d7fb4b13SBruce Evans "cannot read user space from dead kernel"); 9537350dd84SPeter Wemm return (0); 9547350dd84SPeter Wemm } 9557350dd84SPeter Wemm 9561f7d2501SKirk McKusick sprintf(procfile, "/proc/%d/mem", kp->ki_pid); 957338c7541SDavid Greenman fd = open(procfile, O_RDONLY, 0); 958338c7541SDavid Greenman if (fd < 0) { 959338c7541SDavid Greenman _kvm_err(kd, kd->program, "cannot open %s", procfile); 96058f0484fSRodney W. Grimes return (0); 96158f0484fSRodney W. Grimes } 962338c7541SDavid Greenman 963d7fb4b13SBruce Evans cp = buf; 964338c7541SDavid Greenman while (len > 0) { 965d7fb4b13SBruce Evans errno = 0; 966ec4f2251SJoerg Wunsch if (lseek(fd, (off_t)uva, 0) == -1 && errno != 0) { 967*c10970ddSUlrich Spörlein _kvm_err(kd, kd->program, "invalid address (%lx) in %s", 968d7fb4b13SBruce Evans uva, procfile); 96958f0484fSRodney W. Grimes break; 97058f0484fSRodney W. Grimes } 971567127faSDavid Greenman amount = read(fd, cp, len); 972338c7541SDavid Greenman if (amount < 0) { 973d7fb4b13SBruce Evans _kvm_syserr(kd, kd->program, "error reading %s", 974d7fb4b13SBruce Evans procfile); 975d7fb4b13SBruce Evans break; 976d7fb4b13SBruce Evans } 977d7fb4b13SBruce Evans if (amount == 0) { 978d7fb4b13SBruce Evans _kvm_err(kd, kd->program, "EOF reading %s", procfile); 979338c7541SDavid Greenman break; 980338c7541SDavid Greenman } 981338c7541SDavid Greenman cp += amount; 982338c7541SDavid Greenman uva += amount; 983338c7541SDavid Greenman len -= amount; 984338c7541SDavid Greenman } 985338c7541SDavid Greenman 986338c7541SDavid Greenman close(fd); 987d7fb4b13SBruce Evans return ((ssize_t)(cp - buf)); 98858f0484fSRodney W. Grimes } 989