158f0484fSRodney W. Grimes /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 458f0484fSRodney W. Grimes * Copyright (c) 1989, 1992, 1993 558f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 658f0484fSRodney W. Grimes * 758f0484fSRodney W. Grimes * This code is derived from software developed by the Computer Systems 858f0484fSRodney W. Grimes * Engineering group at Lawrence Berkeley Laboratory under DARPA contract 958f0484fSRodney W. Grimes * BG 91-66 and contributed to Berkeley. 1058f0484fSRodney W. Grimes * 1158f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 1258f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 1358f0484fSRodney W. Grimes * are met: 1458f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 1558f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1658f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1758f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1858f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 19fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 2058f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 2158f0484fSRodney W. Grimes * without specific prior written permission. 2258f0484fSRodney W. Grimes * 2358f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2458f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2558f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2658f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2758f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2858f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2958f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3058f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3158f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3258f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3358f0484fSRodney W. Grimes * SUCH DAMAGE. 3458f0484fSRodney W. Grimes */ 3558f0484fSRodney W. Grimes 36f86e3350SBruce Evans #include <sys/cdefs.h> 37f86e3350SBruce Evans __FBSDID("$FreeBSD$"); 38993d074bSJohn Baldwin __SCCSID("@(#)kvm_proc.c 8.3 (Berkeley) 9/23/93"); 3958f0484fSRodney W. Grimes 4058f0484fSRodney W. Grimes /* 4158f0484fSRodney W. Grimes * Proc traversal interface for kvm. ps and w are (probably) the exclusive 4258f0484fSRodney W. Grimes * users of this code, so we've factored it out into a separate module. 4358f0484fSRodney W. Grimes * Thus, we keep this grunge out of the other kvm applications (i.e., 4458f0484fSRodney W. Grimes * most other applications are interested only in open/close/read/nlist). 4558f0484fSRodney W. Grimes */ 4658f0484fSRodney W. Grimes 4758f0484fSRodney W. Grimes #include <sys/param.h> 48b20ea179SAlfred Perlstein #define _WANT_UCRED /* make ucred.h give us 'struct ucred' */ 49aa22cbfeSAlfred Perlstein #include <sys/ucred.h> 50f8197bf0SPawel Jakub Dawidek #include <sys/queue.h> 51f8197bf0SPawel Jakub Dawidek #include <sys/_lock.h> 52f8197bf0SPawel Jakub Dawidek #include <sys/_mutex.h> 53f8197bf0SPawel Jakub Dawidek #include <sys/_task.h> 54413628a7SBjoern A. Zeeb #include <sys/cpuset.h> 5558f0484fSRodney W. Grimes #include <sys/user.h> 5658f0484fSRodney W. Grimes #include <sys/proc.h> 57413628a7SBjoern A. Zeeb #define _WANT_PRISON /* make jail.h give us 'struct prison' */ 58413628a7SBjoern A. Zeeb #include <sys/jail.h> 5958f0484fSRodney W. Grimes #include <sys/exec.h> 6058f0484fSRodney W. Grimes #include <sys/stat.h> 61276de18cSGarance A Drosehn #include <sys/sysent.h> 6258f0484fSRodney W. Grimes #include <sys/ioctl.h> 6358f0484fSRodney W. Grimes #include <sys/tty.h> 64338c7541SDavid Greenman #include <sys/file.h> 652bdd5609SPeter Wemm #include <sys/conf.h> 66b4490c6eSKonstantin Belousov #define _WANT_KW_EXITCODE 67b4490c6eSKonstantin Belousov #include <sys/wait.h> 6851295a4dSJordan K. Hubbard #include <stdio.h> 6951295a4dSJordan K. Hubbard #include <stdlib.h> 70789f4e26SMike Karels #include <stdbool.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 <sys/sysctl.h> 7658f0484fSRodney W. Grimes 7758f0484fSRodney W. Grimes #include <limits.h> 7877721f53SPeter Wemm #include <memory.h> 7958f0484fSRodney W. Grimes #include <paths.h> 8058f0484fSRodney W. Grimes 8158f0484fSRodney W. Grimes #include "kvm_private.h" 8258f0484fSRodney W. Grimes 8358f0484fSRodney W. Grimes #define KREAD(kd, addr, obj) \ 8458f0484fSRodney W. Grimes (kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj)) 8558f0484fSRodney W. Grimes 8684a0b303SJeff Roberson static int ticks; 8784a0b303SJeff Roberson static int hz; 88de788839SUlrich Spörlein static uint64_t cpu_tick_frequency; 89de788839SUlrich Spörlein 90de788839SUlrich Spörlein /* 91de788839SUlrich Spörlein * From sys/kern/kern_tc.c. Depends on cpu_tick_frequency, which is 92de788839SUlrich Spörlein * read/initialized before this function is ever called. 93de788839SUlrich Spörlein */ 94de788839SUlrich Spörlein static uint64_t 95de788839SUlrich Spörlein cputick2usec(uint64_t tick) 96de788839SUlrich Spörlein { 97de788839SUlrich Spörlein 98de788839SUlrich Spörlein if (cpu_tick_frequency == 0) 99de788839SUlrich Spörlein return (0); 100de788839SUlrich Spörlein if (tick > 18446744073709551) /* floor(2^64 / 1000) */ 101de788839SUlrich Spörlein return (tick / (cpu_tick_frequency / 1000000)); 102de788839SUlrich Spörlein else if (tick > 18446744073709) /* floor(2^64 / 1000000) */ 103de788839SUlrich Spörlein return ((tick * 1000) / (cpu_tick_frequency / 1000)); 104de788839SUlrich Spörlein else 105de788839SUlrich Spörlein return ((tick * 1000000) / cpu_tick_frequency); 106de788839SUlrich Spörlein } 10784a0b303SJeff Roberson 10858f0484fSRodney W. Grimes /* 10958f0484fSRodney W. Grimes * Read proc's from memory file into buffer bp, which has space to hold 11058f0484fSRodney W. Grimes * at most maxcnt procs. 11158f0484fSRodney W. Grimes */ 11258f0484fSRodney W. Grimes static int 113c10970ddSUlrich Spörlein kvm_proclist(kvm_t *kd, int what, int arg, struct proc *p, 114c10970ddSUlrich Spörlein struct kinfo_proc *bp, int maxcnt) 11558f0484fSRodney W. Grimes { 116be04b6d1SDavid E. O'Brien int cnt = 0; 1171f7d2501SKirk McKusick struct kinfo_proc kinfo_proc, *kp; 11858f0484fSRodney W. Grimes struct pgrp pgrp; 11958f0484fSRodney W. Grimes struct session sess; 1202bdd5609SPeter Wemm struct cdev t_cdev; 12158f0484fSRodney W. Grimes struct tty tty; 1221f7d2501SKirk McKusick struct vmspace vmspace; 123840558b9SJohn Baldwin struct sigacts sigacts; 124c10970ddSUlrich Spörlein #if 0 1251f7d2501SKirk McKusick struct pstats pstats; 126c10970ddSUlrich Spörlein #endif 1271f7d2501SKirk McKusick struct ucred ucred; 128f8197bf0SPawel Jakub Dawidek struct prison pr; 12971fad9fdSJulian Elischer struct thread mtd; 13058f0484fSRodney W. Grimes struct proc proc; 131a58930d8STor Egge struct proc pproc; 132276de18cSGarance A Drosehn struct sysentvec sysent; 133276de18cSGarance A Drosehn char svname[KI_EMULNAMELEN]; 134789f4e26SMike Karels struct thread *td = NULL; 135789f4e26SMike Karels bool first_thread; 13658f0484fSRodney W. Grimes 1371f7d2501SKirk McKusick kp = &kinfo_proc; 1381f7d2501SKirk McKusick kp->ki_structsize = sizeof(kinfo_proc); 1397ab24ea3SJulian Elischer /* 140789f4e26SMike Karels * Loop on the processes, then threads within the process if requested. 1417ab24ea3SJulian Elischer */ 142789f4e26SMike Karels if (what == KERN_PROC_ALL) 143789f4e26SMike Karels what |= KERN_PROC_INC_THREAD; 14442680b3aSBen Smithurst for (; cnt < maxcnt && p != NULL; p = LIST_NEXT(&proc, p_list)) { 1456f8c6a69SPeter Wemm memset(kp, 0, sizeof *kp); 14658f0484fSRodney W. Grimes if (KREAD(kd, (u_long)p, &proc)) { 147c10970ddSUlrich Spörlein _kvm_err(kd, kd->program, "can't read proc at %p", p); 14858f0484fSRodney W. Grimes return (-1); 14958f0484fSRodney W. Grimes } 1509fb9ea1cSAndriy Gapon if (proc.p_state == PRS_NEW) 1519fb9ea1cSAndriy Gapon continue; 152b1fc0ec1SRobert Watson if (KREAD(kd, (u_long)proc.p_ucred, &ucred) == 0) { 153b1fc0ec1SRobert Watson kp->ki_ruid = ucred.cr_ruid; 154b1fc0ec1SRobert Watson kp->ki_svuid = ucred.cr_svuid; 155b1fc0ec1SRobert Watson kp->ki_rgid = ucred.cr_rgid; 156b1fc0ec1SRobert Watson kp->ki_svgid = ucred.cr_svgid; 1571b5768beSBrooks Davis kp->ki_cr_flags = ucred.cr_flags; 1581b5768beSBrooks Davis if (ucred.cr_ngroups > KI_NGROUPS) { 1591b5768beSBrooks Davis kp->ki_ngroups = KI_NGROUPS; 1601b5768beSBrooks Davis kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW; 16168a5cc39SBrooks Davis } else 1621f7d2501SKirk McKusick kp->ki_ngroups = ucred.cr_ngroups; 163d534b0c2SBrooks Davis kvm_read(kd, (u_long)ucred.cr_groups, kp->ki_groups, 1641b5768beSBrooks Davis kp->ki_ngroups * sizeof(gid_t)); 1651f7d2501SKirk McKusick kp->ki_uid = ucred.cr_uid; 166f8197bf0SPawel Jakub Dawidek if (ucred.cr_prison != NULL) { 167f8197bf0SPawel Jakub Dawidek if (KREAD(kd, (u_long)ucred.cr_prison, &pr)) { 168f8197bf0SPawel Jakub Dawidek _kvm_err(kd, kd->program, 169c10970ddSUlrich Spörlein "can't read prison at %p", 170f8197bf0SPawel Jakub Dawidek ucred.cr_prison); 171f8197bf0SPawel Jakub Dawidek return (-1); 172f8197bf0SPawel Jakub Dawidek } 173f8197bf0SPawel Jakub Dawidek kp->ki_jid = pr.pr_id; 174f8197bf0SPawel Jakub Dawidek } 1751f7d2501SKirk McKusick } 17658f0484fSRodney W. Grimes 177694127f8SDaniel Eischen switch(what & ~KERN_PROC_INC_THREAD) { 17858f0484fSRodney W. Grimes 179276de18cSGarance A Drosehn case KERN_PROC_GID: 180276de18cSGarance A Drosehn if (kp->ki_groups[0] != (gid_t)arg) 181276de18cSGarance A Drosehn continue; 182276de18cSGarance A Drosehn break; 183276de18cSGarance A Drosehn 18458f0484fSRodney W. Grimes case KERN_PROC_PID: 18558f0484fSRodney W. Grimes if (proc.p_pid != (pid_t)arg) 18658f0484fSRodney W. Grimes continue; 18758f0484fSRodney W. Grimes break; 18858f0484fSRodney W. Grimes 189276de18cSGarance A Drosehn case KERN_PROC_RGID: 190276de18cSGarance A Drosehn if (kp->ki_rgid != (gid_t)arg) 191276de18cSGarance A Drosehn continue; 192276de18cSGarance A Drosehn break; 193276de18cSGarance A Drosehn 19458f0484fSRodney W. Grimes case KERN_PROC_UID: 1951f7d2501SKirk McKusick if (kp->ki_uid != (uid_t)arg) 19658f0484fSRodney W. Grimes continue; 19758f0484fSRodney W. Grimes break; 19858f0484fSRodney W. Grimes 19958f0484fSRodney W. Grimes case KERN_PROC_RUID: 2001f7d2501SKirk McKusick if (kp->ki_ruid != (uid_t)arg) 20158f0484fSRodney W. Grimes continue; 20258f0484fSRodney W. Grimes break; 20358f0484fSRodney W. Grimes } 20458f0484fSRodney W. Grimes /* 20558f0484fSRodney W. Grimes * We're going to add another proc to the set. If this 20658f0484fSRodney W. Grimes * will overflow the buffer, assume the reason is because 20758f0484fSRodney W. Grimes * nprocs (or the proc list) is corrupt and declare an error. 20858f0484fSRodney W. Grimes */ 20958f0484fSRodney W. Grimes if (cnt >= maxcnt) { 21058f0484fSRodney W. Grimes _kvm_err(kd, kd->program, "nprocs corrupt"); 21158f0484fSRodney W. Grimes return (-1); 21258f0484fSRodney W. Grimes } 21358f0484fSRodney W. Grimes /* 2141f7d2501SKirk McKusick * gather kinfo_proc 21558f0484fSRodney W. Grimes */ 2161f7d2501SKirk McKusick kp->ki_paddr = p; 2177a62aa8aSDavid Schultz kp->ki_addr = 0; /* XXX uarea */ 218b40ce416SJulian Elischer /* kp->ki_kstack = proc.p_thread.td_kstack; XXXKSE */ 2191f7d2501SKirk McKusick kp->ki_args = proc.p_args; 220789f4e26SMike Karels kp->ki_numthreads = proc.p_numthreads; 2218b7a975eSJohn Baldwin kp->ki_tracep = proc.p_tracevp; 2221f7d2501SKirk McKusick kp->ki_textvp = proc.p_textvp; 2231f7d2501SKirk McKusick kp->ki_fd = proc.p_fd; 22485078b85SConrad Meyer kp->ki_pd = proc.p_pd; 2251f7d2501SKirk McKusick kp->ki_vmspace = proc.p_vmspace; 226840558b9SJohn Baldwin if (proc.p_sigacts != NULL) { 227840558b9SJohn Baldwin if (KREAD(kd, (u_long)proc.p_sigacts, &sigacts)) { 2281f7d2501SKirk McKusick _kvm_err(kd, kd->program, 229c10970ddSUlrich Spörlein "can't read sigacts at %p", proc.p_sigacts); 2301f7d2501SKirk McKusick return (-1); 2311f7d2501SKirk McKusick } 232840558b9SJohn Baldwin kp->ki_sigignore = sigacts.ps_sigignore; 233840558b9SJohn Baldwin kp->ki_sigcatch = sigacts.ps_sigcatch; 2341f7d2501SKirk McKusick } 2358ef6b142SJeff Roberson #if 0 236b61ce5b0SJeff Roberson if ((proc.p_flag & P_INMEM) && proc.p_stats != NULL) { 2371f7d2501SKirk McKusick if (KREAD(kd, (u_long)proc.p_stats, &pstats)) { 2381f7d2501SKirk McKusick _kvm_err(kd, kd->program, 2391f7d2501SKirk McKusick "can't read stats at %x", proc.p_stats); 2401f7d2501SKirk McKusick return (-1); 2411f7d2501SKirk McKusick } 2421f7d2501SKirk McKusick kp->ki_start = pstats.p_start; 24309ff687cSJohn Baldwin 24409ff687cSJohn Baldwin /* 24509ff687cSJohn Baldwin * XXX: The times here are probably zero and need 24609ff687cSJohn Baldwin * to be calculated from the raw data in p_rux and 24709ff687cSJohn Baldwin * p_crux. 24809ff687cSJohn Baldwin */ 2491f7d2501SKirk McKusick kp->ki_rusage = pstats.p_ru; 250276de18cSGarance A Drosehn kp->ki_childstime = pstats.p_cru.ru_stime; 251276de18cSGarance A Drosehn kp->ki_childutime = pstats.p_cru.ru_utime; 252276de18cSGarance A Drosehn /* Some callers want child-times in a single value */ 253276de18cSGarance A Drosehn timeradd(&kp->ki_childstime, &kp->ki_childutime, 254276de18cSGarance A Drosehn &kp->ki_childtime); 2551f7d2501SKirk McKusick } 2568ef6b142SJeff Roberson #endif 257a58930d8STor Egge if (proc.p_oppid) 2581f7d2501SKirk McKusick kp->ki_ppid = proc.p_oppid; 259a58930d8STor Egge else if (proc.p_pptr) { 260a58930d8STor Egge if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) { 2611f7d2501SKirk McKusick _kvm_err(kd, kd->program, 262c10970ddSUlrich Spörlein "can't read pproc at %p", proc.p_pptr); 263a58930d8STor Egge return (-1); 264a58930d8STor Egge } 2651f7d2501SKirk McKusick kp->ki_ppid = pproc.p_pid; 266a58930d8STor Egge } else 2671f7d2501SKirk McKusick kp->ki_ppid = 0; 2686f8c6a69SPeter Wemm if (proc.p_pgrp == NULL) 2696f8c6a69SPeter Wemm goto nopgrp; 2706f8c6a69SPeter Wemm if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) { 271c10970ddSUlrich Spörlein _kvm_err(kd, kd->program, "can't read pgrp at %p", 2726f8c6a69SPeter Wemm proc.p_pgrp); 2736f8c6a69SPeter Wemm return (-1); 2746f8c6a69SPeter Wemm } 2751f7d2501SKirk McKusick kp->ki_pgid = pgrp.pg_id; 276*5844bd05SKonstantin Belousov kp->ki_jobc = -1; /* Or calculate? Arguably not. */ 27758f0484fSRodney W. Grimes if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) { 278c10970ddSUlrich Spörlein _kvm_err(kd, kd->program, "can't read session at %p", 27958f0484fSRodney W. Grimes pgrp.pg_session); 28058f0484fSRodney W. Grimes return (-1); 28158f0484fSRodney W. Grimes } 2821f7d2501SKirk McKusick kp->ki_sid = sess.s_sid; 2831f7d2501SKirk McKusick (void)memcpy(kp->ki_login, sess.s_login, 2841f7d2501SKirk McKusick sizeof(kp->ki_login)); 28558f0484fSRodney W. Grimes if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) { 28658f0484fSRodney W. Grimes if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) { 28758f0484fSRodney W. Grimes _kvm_err(kd, kd->program, 288c10970ddSUlrich Spörlein "can't read tty at %p", sess.s_ttyp); 28958f0484fSRodney W. Grimes return (-1); 29058f0484fSRodney W. Grimes } 2912bdd5609SPeter Wemm if (tty.t_dev != NULL) { 2922bdd5609SPeter Wemm if (KREAD(kd, (u_long)tty.t_dev, &t_cdev)) { 2932bdd5609SPeter Wemm _kvm_err(kd, kd->program, 294c10970ddSUlrich Spörlein "can't read cdev at %p", 2952bdd5609SPeter Wemm tty.t_dev); 2962bdd5609SPeter Wemm return (-1); 2972bdd5609SPeter Wemm } 2989c4fb661SPoul-Henning Kamp #if 0 2992bdd5609SPeter Wemm kp->ki_tdev = t_cdev.si_udev; 3009c4fb661SPoul-Henning Kamp #else 3012cfe3fdaSPeter Wemm kp->ki_tdev = NODEV; 3029c4fb661SPoul-Henning Kamp #endif 3032bdd5609SPeter Wemm } 30458f0484fSRodney W. Grimes if (tty.t_pgrp != NULL) { 30558f0484fSRodney W. Grimes if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) { 30658f0484fSRodney W. Grimes _kvm_err(kd, kd->program, 307c10970ddSUlrich Spörlein "can't read tpgrp at %p", 30858f0484fSRodney W. Grimes tty.t_pgrp); 30958f0484fSRodney W. Grimes return (-1); 31058f0484fSRodney W. Grimes } 3111f7d2501SKirk McKusick kp->ki_tpgid = pgrp.pg_id; 31258f0484fSRodney W. Grimes } else 3131f7d2501SKirk McKusick kp->ki_tpgid = -1; 3141f7d2501SKirk McKusick if (tty.t_session != NULL) { 3151f7d2501SKirk McKusick if (KREAD(kd, (u_long)tty.t_session, &sess)) { 3161f7d2501SKirk McKusick _kvm_err(kd, kd->program, 317c10970ddSUlrich Spörlein "can't read session at %p", 3181f7d2501SKirk McKusick tty.t_session); 3191f7d2501SKirk McKusick return (-1); 3201f7d2501SKirk McKusick } 3211f7d2501SKirk McKusick kp->ki_tsid = sess.s_sid; 3221f7d2501SKirk McKusick } 3236f8c6a69SPeter Wemm } else { 3246f8c6a69SPeter Wemm nopgrp: 3251f7d2501SKirk McKusick kp->ki_tdev = NODEV; 3266f8c6a69SPeter Wemm } 32758f0484fSRodney W. Grimes 32858f0484fSRodney W. Grimes (void)kvm_read(kd, (u_long)proc.p_vmspace, 3291f7d2501SKirk McKusick (char *)&vmspace, sizeof(vmspace)); 3301f7d2501SKirk McKusick kp->ki_size = vmspace.vm_map.size; 3315f494640SSean Bruno /* 3325f494640SSean Bruno * Approximate the kernel's method of calculating 3335f494640SSean Bruno * this field. 3345f494640SSean Bruno */ 3355f494640SSean Bruno #define pmap_resident_count(pm) ((pm)->pm_stats.resident_count) 3365f494640SSean Bruno kp->ki_rssize = pmap_resident_count(&vmspace.vm_pmap); 3371f7d2501SKirk McKusick kp->ki_swrss = vmspace.vm_swrss; 3381f7d2501SKirk McKusick kp->ki_tsize = vmspace.vm_tsize; 3391f7d2501SKirk McKusick kp->ki_dsize = vmspace.vm_dsize; 3401f7d2501SKirk McKusick kp->ki_ssize = vmspace.vm_ssize; 34158f0484fSRodney W. Grimes 342694127f8SDaniel Eischen switch (what & ~KERN_PROC_INC_THREAD) { 34358f0484fSRodney W. Grimes 34458f0484fSRodney W. Grimes case KERN_PROC_PGRP: 3451f7d2501SKirk McKusick if (kp->ki_pgid != (pid_t)arg) 34658f0484fSRodney W. Grimes continue; 34758f0484fSRodney W. Grimes break; 34858f0484fSRodney W. Grimes 349276de18cSGarance A Drosehn case KERN_PROC_SESSION: 350276de18cSGarance A Drosehn if (kp->ki_sid != (pid_t)arg) 351276de18cSGarance A Drosehn continue; 352276de18cSGarance A Drosehn break; 353276de18cSGarance A Drosehn 35458f0484fSRodney W. Grimes case KERN_PROC_TTY: 35558f0484fSRodney W. Grimes if ((proc.p_flag & P_CONTROLT) == 0 || 3561f7d2501SKirk McKusick kp->ki_tdev != (dev_t)arg) 35758f0484fSRodney W. Grimes continue; 35858f0484fSRodney W. Grimes break; 35958f0484fSRodney W. Grimes } 360b7e7c21aSGarance A Drosehn if (proc.p_comm[0] != 0) 361b7e7c21aSGarance A Drosehn strlcpy(kp->ki_comm, proc.p_comm, MAXCOMLEN); 362276de18cSGarance A Drosehn (void)kvm_read(kd, (u_long)proc.p_sysent, (char *)&sysent, 363276de18cSGarance A Drosehn sizeof(sysent)); 364276de18cSGarance A Drosehn (void)kvm_read(kd, (u_long)sysent.sv_name, (char *)&svname, 365276de18cSGarance A Drosehn sizeof(svname)); 366276de18cSGarance A Drosehn if (svname[0] != 0) 367276de18cSGarance A Drosehn strlcpy(kp->ki_emul, svname, KI_EMULNAMELEN); 368789f4e26SMike Karels kp->ki_runtime = cputick2usec(proc.p_rux.rux_runtime); 369789f4e26SMike Karels kp->ki_pid = proc.p_pid; 370789f4e26SMike Karels kp->ki_xstat = KW_EXITCODE(proc.p_xexit, proc.p_xsig); 371789f4e26SMike Karels kp->ki_acflag = proc.p_acflag; 372789f4e26SMike Karels kp->ki_lock = proc.p_lock; 373789f4e26SMike Karels kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */ 374789f4e26SMike Karels 375789f4e26SMike Karels /* Per-thread items; iterate as appropriate. */ 376789f4e26SMike Karels td = TAILQ_FIRST(&proc.p_threads); 377789f4e26SMike Karels for (first_thread = true; cnt < maxcnt && td != NULL && 378789f4e26SMike Karels (first_thread || (what & KERN_PROC_INC_THREAD)); 379789f4e26SMike Karels first_thread = false) { 380789f4e26SMike Karels if (proc.p_state != PRS_ZOMBIE) { 381789f4e26SMike Karels if (KREAD(kd, (u_long)td, &mtd)) { 382789f4e26SMike Karels _kvm_err(kd, kd->program, 383789f4e26SMike Karels "can't read thread at %p", td); 384789f4e26SMike Karels return (-1); 385789f4e26SMike Karels } 386789f4e26SMike Karels if (what & KERN_PROC_INC_THREAD) 387789f4e26SMike Karels td = TAILQ_NEXT(&mtd, td_plist); 388789f4e26SMike Karels } else 389789f4e26SMike Karels td = NULL; 390789f4e26SMike Karels if ((proc.p_state != PRS_ZOMBIE) && mtd.td_wmesg) 391789f4e26SMike Karels (void)kvm_read(kd, (u_long)mtd.td_wmesg, 392789f4e26SMike Karels kp->ki_wmesg, WMESGLEN); 393789f4e26SMike Karels else 394789f4e26SMike Karels memset(kp->ki_wmesg, 0, WMESGLEN); 395789f4e26SMike Karels if (proc.p_pgrp == NULL) { 396789f4e26SMike Karels kp->ki_kiflag = 0; 397789f4e26SMike Karels } else { 398789f4e26SMike Karels kp->ki_kiflag = sess.s_ttyvp ? KI_CTTY : 0; 399789f4e26SMike Karels if (sess.s_leader == p) 400789f4e26SMike Karels kp->ki_kiflag |= KI_SLEADER; 401789f4e26SMike Karels } 402bff4151cSJulian Elischer if ((proc.p_state != PRS_ZOMBIE) && 40371fad9fdSJulian Elischer (mtd.td_blocked != 0)) { 4040d632649SJohn Baldwin kp->ki_kiflag |= KI_LOCKBLOCK; 4050d632649SJohn Baldwin if (mtd.td_lockname) 406bff4151cSJulian Elischer (void)kvm_read(kd, 4070d632649SJohn Baldwin (u_long)mtd.td_lockname, 4080d632649SJohn Baldwin kp->ki_lockname, LOCKNAMELEN); 409789f4e26SMike Karels else 410789f4e26SMike Karels memset(kp->ki_lockname, 0, 411789f4e26SMike Karels LOCKNAMELEN); 4120d632649SJohn Baldwin kp->ki_lockname[LOCKNAMELEN] = 0; 413789f4e26SMike Karels } else 414789f4e26SMike Karels kp->ki_kiflag &= ~KI_LOCKBLOCK; 415e8a58a83SJuli Mallett kp->ki_siglist = proc.p_siglist; 416789f4e26SMike Karels if (proc.p_state != PRS_ZOMBIE) { 41742d3ad71SJeff Roberson SIGSETOR(kp->ki_siglist, mtd.td_siglist); 41831a9779eSJeff Roberson kp->ki_sigmask = mtd.td_sigmask; 41984a0b303SJeff Roberson kp->ki_swtime = (ticks - proc.p_swtick) / hz; 4206143c383SJulian Elischer kp->ki_flag = proc.p_flag; 421b61ce5b0SJeff Roberson kp->ki_sflag = 0; 42230105366SJulian Elischer kp->ki_nice = proc.p_nice; 4231f7d2501SKirk McKusick kp->ki_traceflag = proc.p_traceflag; 4246143c383SJulian Elischer if (proc.p_state == PRS_NORMAL) { 42571fad9fdSJulian Elischer if (TD_ON_RUNQ(&mtd) || 42671fad9fdSJulian Elischer TD_CAN_RUN(&mtd) || 42771fad9fdSJulian Elischer TD_IS_RUNNING(&mtd)) { 428e602ba25SJulian Elischer kp->ki_stat = SRUN; 4294f0db5e0SJulian Elischer } else if (mtd.td_state == 4304f0db5e0SJulian Elischer TDS_INHIBITED) { 43171fad9fdSJulian Elischer if (P_SHOULDSTOP(&proc)) { 432e602ba25SJulian Elischer kp->ki_stat = SSTOP; 4334f0db5e0SJulian Elischer } else if ( 4344f0db5e0SJulian Elischer TD_IS_SLEEPING(&mtd)) { 43571fad9fdSJulian Elischer kp->ki_stat = SSLEEP; 4360d632649SJohn Baldwin } else if (TD_ON_LOCK(&mtd)) { 4370d632649SJohn Baldwin kp->ki_stat = SLOCK; 438e602ba25SJulian Elischer } else { 439e602ba25SJulian Elischer kp->ki_stat = SWAIT; 440e602ba25SJulian Elischer } 44171fad9fdSJulian Elischer } 442e602ba25SJulian Elischer } else { 443e602ba25SJulian Elischer kp->ki_stat = SIDL; 444e602ba25SJulian Elischer } 4454f0db5e0SJulian Elischer /* Stuff from the thread */ 44671fad9fdSJulian Elischer kp->ki_pri.pri_level = mtd.td_priority; 44771fad9fdSJulian Elischer kp->ki_pri.pri_native = mtd.td_base_pri; 44871fad9fdSJulian Elischer kp->ki_lastcpu = mtd.td_lastcpu; 4494f0db5e0SJulian Elischer kp->ki_wchan = mtd.td_wchan; 450a0ddbf49SJulian Elischer kp->ki_oncpu = mtd.td_oncpu; 4517ab24ea3SJulian Elischer if (mtd.td_name[0] != '\0') 452789f4e26SMike Karels strlcpy(kp->ki_tdname, mtd.td_name, 453789f4e26SMike Karels sizeof(kp->ki_tdname)); 454789f4e26SMike Karels else 455789f4e26SMike Karels memset(kp->ki_tdname, 0, 456789f4e26SMike Karels sizeof(kp->ki_tdname)); 457ed062c8dSJulian Elischer kp->ki_pctcpu = 0; 458ed062c8dSJulian Elischer kp->ki_rqindex = 0; 459e77f9fedSAdrian Chadd 460e77f9fedSAdrian Chadd /* 461789f4e26SMike Karels * Note: legacy fields; wraps at NO_CPU_OLD 462789f4e26SMike Karels * or the old max CPU value as appropriate 463e77f9fedSAdrian Chadd */ 464e77f9fedSAdrian Chadd if (mtd.td_lastcpu == NOCPU) 465e77f9fedSAdrian Chadd kp->ki_lastcpu_old = NOCPU_OLD; 466e77f9fedSAdrian Chadd else if (mtd.td_lastcpu > MAXCPU_OLD) 467e77f9fedSAdrian Chadd kp->ki_lastcpu_old = MAXCPU_OLD; 468e77f9fedSAdrian Chadd else 469e77f9fedSAdrian Chadd kp->ki_lastcpu_old = mtd.td_lastcpu; 470e77f9fedSAdrian Chadd 471e77f9fedSAdrian Chadd if (mtd.td_oncpu == NOCPU) 472e77f9fedSAdrian Chadd kp->ki_oncpu_old = NOCPU_OLD; 473e77f9fedSAdrian Chadd else if (mtd.td_oncpu > MAXCPU_OLD) 474e77f9fedSAdrian Chadd kp->ki_oncpu_old = MAXCPU_OLD; 475e77f9fedSAdrian Chadd else 476e77f9fedSAdrian Chadd kp->ki_oncpu_old = mtd.td_oncpu; 477789f4e26SMike Karels kp->ki_tid = mtd.td_tid; 4784f0db5e0SJulian Elischer } else { 479789f4e26SMike Karels memset(&kp->ki_sigmask, 0, 480789f4e26SMike Karels sizeof(kp->ki_sigmask)); 4816143c383SJulian Elischer kp->ki_stat = SZOMB; 482789f4e26SMike Karels kp->ki_tid = 0; 4836143c383SJulian Elischer } 484789f4e26SMike Karels 4851f7d2501SKirk McKusick bcopy(&kinfo_proc, bp, sizeof(kinfo_proc)); 48658f0484fSRodney W. Grimes ++bp; 48758f0484fSRodney W. Grimes ++cnt; 48858f0484fSRodney W. Grimes } 489789f4e26SMike Karels } 49058f0484fSRodney W. Grimes return (cnt); 49158f0484fSRodney W. Grimes } 49258f0484fSRodney W. Grimes 49358f0484fSRodney W. Grimes /* 49458f0484fSRodney W. Grimes * Build proc info array by reading in proc list from a crash dump. 49558f0484fSRodney W. Grimes * Return number of procs read. maxcnt is the max we will read. 49658f0484fSRodney W. Grimes */ 49758f0484fSRodney W. Grimes static int 498c10970ddSUlrich Spörlein kvm_deadprocs(kvm_t *kd, int what, int arg, u_long a_allproc, 499c10970ddSUlrich Spörlein u_long a_zombproc, int maxcnt) 50058f0484fSRodney W. Grimes { 501be04b6d1SDavid E. O'Brien struct kinfo_proc *bp = kd->procbase; 502789f4e26SMike Karels int acnt, zcnt = 0; 50358f0484fSRodney W. Grimes struct proc *p; 50458f0484fSRodney W. Grimes 50558f0484fSRodney W. Grimes if (KREAD(kd, a_allproc, &p)) { 50658f0484fSRodney W. Grimes _kvm_err(kd, kd->program, "cannot read allproc"); 50758f0484fSRodney W. Grimes return (-1); 50858f0484fSRodney W. Grimes } 50958f0484fSRodney W. Grimes acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt); 51058f0484fSRodney W. Grimes if (acnt < 0) 51158f0484fSRodney W. Grimes return (acnt); 51258f0484fSRodney W. Grimes 513789f4e26SMike Karels if (a_zombproc != 0) { 51458f0484fSRodney W. Grimes if (KREAD(kd, a_zombproc, &p)) { 51558f0484fSRodney W. Grimes _kvm_err(kd, kd->program, "cannot read zombproc"); 51658f0484fSRodney W. Grimes return (-1); 51758f0484fSRodney W. Grimes } 51858f0484fSRodney W. Grimes zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt); 51958f0484fSRodney W. Grimes if (zcnt < 0) 52058f0484fSRodney W. Grimes zcnt = 0; 521789f4e26SMike Karels } 52258f0484fSRodney W. Grimes 52358f0484fSRodney W. Grimes return (acnt + zcnt); 52458f0484fSRodney W. Grimes } 52558f0484fSRodney W. Grimes 52658f0484fSRodney W. Grimes struct kinfo_proc * 527c10970ddSUlrich Spörlein kvm_getprocs(kvm_t *kd, int op, int arg, int *cnt) 52858f0484fSRodney W. Grimes { 529c2ac238cSDoug Rabson int mib[4], st, nprocs; 530767993ecSMikolaj Golub size_t size, osize; 531694127f8SDaniel Eischen int temp_op; 53258f0484fSRodney W. Grimes 53358f0484fSRodney W. Grimes if (kd->procbase != 0) { 53458f0484fSRodney W. Grimes free((void *)kd->procbase); 53558f0484fSRodney W. Grimes /* 53658f0484fSRodney W. Grimes * Clear this pointer in case this call fails. Otherwise, 53758f0484fSRodney W. Grimes * kvm_close() will free it again. 53858f0484fSRodney W. Grimes */ 53958f0484fSRodney W. Grimes kd->procbase = 0; 54058f0484fSRodney W. Grimes } 54158f0484fSRodney W. Grimes if (ISALIVE(kd)) { 54258f0484fSRodney W. Grimes size = 0; 54358f0484fSRodney W. Grimes mib[0] = CTL_KERN; 54458f0484fSRodney W. Grimes mib[1] = KERN_PROC; 54558f0484fSRodney W. Grimes mib[2] = op; 54658f0484fSRodney W. Grimes mib[3] = arg; 547694127f8SDaniel Eischen temp_op = op & ~KERN_PROC_INC_THREAD; 548694127f8SDaniel Eischen st = sysctl(mib, 549694127f8SDaniel Eischen temp_op == KERN_PROC_ALL || temp_op == KERN_PROC_PROC ? 550f2dd06abSTim J. Robbins 3 : 4, NULL, &size, NULL, 0); 55158f0484fSRodney W. Grimes if (st == -1) { 55258f0484fSRodney W. Grimes _kvm_syserr(kd, kd->program, "kvm_getprocs"); 55358f0484fSRodney W. Grimes return (0); 55458f0484fSRodney W. Grimes } 55521687047SDima Dorfman /* 55621687047SDima Dorfman * We can't continue with a size of 0 because we pass 55721687047SDima Dorfman * it to realloc() (via _kvm_realloc()), and passing 0 55821687047SDima Dorfman * to realloc() results in undefined behavior. 55921687047SDima Dorfman */ 56021687047SDima Dorfman if (size == 0) { 56121687047SDima Dorfman /* 56221687047SDima Dorfman * XXX: We should probably return an invalid, 56321687047SDima Dorfman * but non-NULL, pointer here so any client 56421687047SDima Dorfman * program trying to dereference it will 56521687047SDima Dorfman * crash. However, _kvm_freeprocs() calls 56621687047SDima Dorfman * free() on kd->procbase if it isn't NULL, 56721687047SDima Dorfman * and free()'ing a junk pointer isn't good. 56821687047SDima Dorfman * Then again, _kvm_freeprocs() isn't used 56921687047SDima Dorfman * anywhere . . . 57021687047SDima Dorfman */ 57121687047SDima Dorfman kd->procbase = _kvm_malloc(kd, 1); 57221687047SDima Dorfman goto liveout; 57321687047SDima Dorfman } 574b2d3d0f0SDag-Erling Smørgrav do { 575b2d3d0f0SDag-Erling Smørgrav size += size / 10; 576b2d3d0f0SDag-Erling Smørgrav kd->procbase = (struct kinfo_proc *) 577b2d3d0f0SDag-Erling Smørgrav _kvm_realloc(kd, kd->procbase, size); 578fb0e1892SEnji Cooper if (kd->procbase == NULL) 57958f0484fSRodney W. Grimes return (0); 580767993ecSMikolaj Golub osize = size; 581694127f8SDaniel Eischen st = sysctl(mib, temp_op == KERN_PROC_ALL || 582694127f8SDaniel Eischen temp_op == KERN_PROC_PROC ? 3 : 4, 583b2d3d0f0SDag-Erling Smørgrav kd->procbase, &size, NULL, 0); 584767993ecSMikolaj Golub } while (st == -1 && errno == ENOMEM && size == osize); 58558f0484fSRodney W. Grimes if (st == -1) { 58658f0484fSRodney W. Grimes _kvm_syserr(kd, kd->program, "kvm_getprocs"); 58758f0484fSRodney W. Grimes return (0); 58858f0484fSRodney W. Grimes } 58921687047SDima Dorfman /* 59021687047SDima Dorfman * We have to check the size again because sysctl() 59121687047SDima Dorfman * may "round up" oldlenp if oldp is NULL; hence it 59221687047SDima Dorfman * might've told us that there was data to get when 59321687047SDima Dorfman * there really isn't any. 59421687047SDima Dorfman */ 5950627f53bSDavid Malone if (size > 0 && 5960627f53bSDavid Malone kd->procbase->ki_structsize != sizeof(struct kinfo_proc)) { 59758f0484fSRodney W. Grimes _kvm_err(kd, kd->program, 598c10970ddSUlrich Spörlein "kinfo_proc size mismatch (expected %zu, got %d)", 5991f7d2501SKirk McKusick sizeof(struct kinfo_proc), 6001f7d2501SKirk McKusick kd->procbase->ki_structsize); 60158f0484fSRodney W. Grimes return (0); 60258f0484fSRodney W. Grimes } 60321687047SDima Dorfman liveout: 604d339edcfSDavid Malone nprocs = size == 0 ? 0 : size / kd->procbase->ki_structsize; 60558f0484fSRodney W. Grimes } else { 606789f4e26SMike Karels struct nlist nl[6], *p; 607789f4e26SMike Karels struct nlist nlz[2]; 60858f0484fSRodney W. Grimes 60958f0484fSRodney W. Grimes nl[0].n_name = "_nprocs"; 61058f0484fSRodney W. Grimes nl[1].n_name = "_allproc"; 611789f4e26SMike Karels nl[2].n_name = "_ticks"; 612789f4e26SMike Karels nl[3].n_name = "_hz"; 613789f4e26SMike Karels nl[4].n_name = "_cpu_tick_frequency"; 614789f4e26SMike Karels nl[5].n_name = 0; 615789f4e26SMike Karels 616789f4e26SMike Karels nlz[0].n_name = "_zombproc"; 617789f4e26SMike Karels nlz[1].n_name = 0; 61858f0484fSRodney W. Grimes 6197f911abeSJohn Baldwin if (!kd->arch->ka_native(kd)) { 6207f911abeSJohn Baldwin _kvm_err(kd, kd->program, 6217f911abeSJohn Baldwin "cannot read procs from non-native core"); 6227f911abeSJohn Baldwin return (0); 6237f911abeSJohn Baldwin } 6247f911abeSJohn Baldwin 62558f0484fSRodney W. Grimes if (kvm_nlist(kd, nl) != 0) { 62658f0484fSRodney W. Grimes for (p = nl; p->n_type != 0; ++p) 62758f0484fSRodney W. Grimes ; 62858f0484fSRodney W. Grimes _kvm_err(kd, kd->program, 62958f0484fSRodney W. Grimes "%s: no such symbol", p->n_name); 63058f0484fSRodney W. Grimes return (0); 63158f0484fSRodney W. Grimes } 632789f4e26SMike Karels (void) kvm_nlist(kd, nlz); /* attempt to get zombproc */ 63358f0484fSRodney W. Grimes if (KREAD(kd, nl[0].n_value, &nprocs)) { 63458f0484fSRodney W. Grimes _kvm_err(kd, kd->program, "can't read nprocs"); 63558f0484fSRodney W. Grimes return (0); 63658f0484fSRodney W. Grimes } 637789f4e26SMike Karels /* 638789f4e26SMike Karels * If returning all threads, we don't know how many that 639789f4e26SMike Karels * might be. Presume that there are, on average, no more 640789f4e26SMike Karels * than 10 threads per process. 641789f4e26SMike Karels */ 642789f4e26SMike Karels if (op == KERN_PROC_ALL || (op & KERN_PROC_INC_THREAD)) 643789f4e26SMike Karels nprocs *= 10; /* XXX */ 644789f4e26SMike Karels if (KREAD(kd, nl[2].n_value, &ticks)) { 64584a0b303SJeff Roberson _kvm_err(kd, kd->program, "can't read ticks"); 64684a0b303SJeff Roberson return (0); 64784a0b303SJeff Roberson } 648789f4e26SMike Karels if (KREAD(kd, nl[3].n_value, &hz)) { 64984a0b303SJeff Roberson _kvm_err(kd, kd->program, "can't read hz"); 65084a0b303SJeff Roberson return (0); 65184a0b303SJeff Roberson } 652789f4e26SMike Karels if (KREAD(kd, nl[4].n_value, &cpu_tick_frequency)) { 653de788839SUlrich Spörlein _kvm_err(kd, kd->program, 654de788839SUlrich Spörlein "can't read cpu_tick_frequency"); 655de788839SUlrich Spörlein return (0); 656de788839SUlrich Spörlein } 65758f0484fSRodney W. Grimes size = nprocs * sizeof(struct kinfo_proc); 65858f0484fSRodney W. Grimes kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size); 659fb0e1892SEnji Cooper if (kd->procbase == NULL) 66058f0484fSRodney W. Grimes return (0); 66158f0484fSRodney W. Grimes 66258f0484fSRodney W. Grimes nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value, 663789f4e26SMike Karels nlz[0].n_value, nprocs); 664ca895af3SAndriy Gapon if (nprocs <= 0) { 665ca895af3SAndriy Gapon _kvm_freeprocs(kd); 666ca895af3SAndriy Gapon nprocs = 0; 667ca895af3SAndriy Gapon } 66858f0484fSRodney W. Grimes #ifdef notdef 669ca895af3SAndriy Gapon else { 67058f0484fSRodney W. Grimes size = nprocs * sizeof(struct kinfo_proc); 671ca895af3SAndriy Gapon kd->procbase = realloc(kd->procbase, size); 672ca895af3SAndriy Gapon } 67358f0484fSRodney W. Grimes #endif 67458f0484fSRodney W. Grimes } 67558f0484fSRodney W. Grimes *cnt = nprocs; 67658f0484fSRodney W. Grimes return (kd->procbase); 67758f0484fSRodney W. Grimes } 67858f0484fSRodney W. Grimes 67958f0484fSRodney W. Grimes void 680c10970ddSUlrich Spörlein _kvm_freeprocs(kvm_t *kd) 68158f0484fSRodney W. Grimes { 682fb0e1892SEnji Cooper 68358f0484fSRodney W. Grimes free(kd->procbase); 684fb0e1892SEnji Cooper kd->procbase = NULL; 68558f0484fSRodney W. Grimes } 68658f0484fSRodney W. Grimes 68758f0484fSRodney W. Grimes void * 688c10970ddSUlrich Spörlein _kvm_realloc(kvm_t *kd, void *p, size_t n) 68958f0484fSRodney W. Grimes { 690fb0e1892SEnji Cooper void *np; 69158f0484fSRodney W. Grimes 692fb0e1892SEnji Cooper np = reallocf(p, n); 693fb0e1892SEnji Cooper if (np == NULL) 69458f0484fSRodney W. Grimes _kvm_err(kd, kd->program, "out of memory"); 69558f0484fSRodney W. Grimes return (np); 69658f0484fSRodney W. Grimes } 69758f0484fSRodney W. Grimes 69858f0484fSRodney W. Grimes /* 699ee5169dcSMikolaj Golub * Get the command args or environment. 70058f0484fSRodney W. Grimes */ 70158f0484fSRodney W. Grimes static char ** 702ee5169dcSMikolaj Golub kvm_argv(kvm_t *kd, const struct kinfo_proc *kp, int env, int nchr) 70358f0484fSRodney W. Grimes { 704b9df5231SPoul-Henning Kamp int oid[4]; 705b7875890SDavid E. O'Brien int i; 706b7875890SDavid E. O'Brien size_t bufsz; 707ee5169dcSMikolaj Golub static int buflen; 708b9df5231SPoul-Henning Kamp static char *buf, *p; 709b9df5231SPoul-Henning Kamp static char **bufp; 710b9df5231SPoul-Henning Kamp static int argc; 71168b68bf5SEnji Cooper char **nbufp; 712b9df5231SPoul-Henning Kamp 713c4a7cdb3SPeter Wemm if (!ISALIVE(kd)) { 714c4a7cdb3SPeter Wemm _kvm_err(kd, kd->program, 715c4a7cdb3SPeter Wemm "cannot read user space from dead kernel"); 716fb0e1892SEnji Cooper return (NULL); 717c4a7cdb3SPeter Wemm } 718c4a7cdb3SPeter Wemm 719ee5169dcSMikolaj Golub if (nchr == 0 || nchr > ARG_MAX) 720ee5169dcSMikolaj Golub nchr = ARG_MAX; 721ee5169dcSMikolaj Golub if (buflen == 0) { 722ee5169dcSMikolaj Golub buf = malloc(nchr); 723ee5169dcSMikolaj Golub if (buf == NULL) { 724ee5169dcSMikolaj Golub _kvm_err(kd, kd->program, "cannot allocate memory"); 725fb0e1892SEnji Cooper return (NULL); 726ee5169dcSMikolaj Golub } 727b9df5231SPoul-Henning Kamp argc = 32; 728b9df5231SPoul-Henning Kamp bufp = malloc(sizeof(char *) * argc); 72968b68bf5SEnji Cooper if (bufp == NULL) { 73068b68bf5SEnji Cooper free(buf); 73168b68bf5SEnji Cooper buf = NULL; 73268b68bf5SEnji Cooper _kvm_err(kd, kd->program, "cannot allocate memory"); 73368b68bf5SEnji Cooper return (NULL); 73468b68bf5SEnji Cooper } 73568b68bf5SEnji Cooper buflen = nchr; 736ee5169dcSMikolaj Golub } else if (nchr > buflen) { 737ee5169dcSMikolaj Golub p = realloc(buf, nchr); 738ee5169dcSMikolaj Golub if (p != NULL) { 739ee5169dcSMikolaj Golub buf = p; 740ee5169dcSMikolaj Golub buflen = nchr; 741b9df5231SPoul-Henning Kamp } 742b9df5231SPoul-Henning Kamp } 743b9df5231SPoul-Henning Kamp oid[0] = CTL_KERN; 744b9df5231SPoul-Henning Kamp oid[1] = KERN_PROC; 745ee5169dcSMikolaj Golub oid[2] = env ? KERN_PROC_ENV : KERN_PROC_ARGS; 7461f7d2501SKirk McKusick oid[3] = kp->ki_pid; 747b7875890SDavid E. O'Brien bufsz = buflen; 748952a0c3eSMikolaj Golub if (sysctl(oid, 4, buf, &bufsz, 0, 0) == -1) { 749952a0c3eSMikolaj Golub /* 750952a0c3eSMikolaj Golub * If the supplied buf is too short to hold the requested 751952a0c3eSMikolaj Golub * value the sysctl returns with ENOMEM. The buf is filled 752952a0c3eSMikolaj Golub * with the truncated value and the returned bufsz is equal 753952a0c3eSMikolaj Golub * to the requested len. 754952a0c3eSMikolaj Golub */ 755952a0c3eSMikolaj Golub if (errno != ENOMEM || bufsz != (size_t)buflen) 756fb0e1892SEnji Cooper return (NULL); 757952a0c3eSMikolaj Golub buf[bufsz - 1] = '\0'; 758952a0c3eSMikolaj Golub errno = 0; 759fb0e1892SEnji Cooper } else if (bufsz == 0) 760fb0e1892SEnji Cooper return (NULL); 761b9df5231SPoul-Henning Kamp i = 0; 762b9df5231SPoul-Henning Kamp p = buf; 763b9df5231SPoul-Henning Kamp do { 764b9df5231SPoul-Henning Kamp bufp[i++] = p; 765b9df5231SPoul-Henning Kamp p += strlen(p) + 1; 766b9df5231SPoul-Henning Kamp if (i >= argc) { 767b9df5231SPoul-Henning Kamp argc += argc; 76868b68bf5SEnji Cooper nbufp = realloc(bufp, sizeof(char *) * argc); 76968b68bf5SEnji Cooper if (nbufp == NULL) 77068b68bf5SEnji Cooper return (NULL); 77168b68bf5SEnji Cooper bufp = nbufp; 772b9df5231SPoul-Henning Kamp } 773b7875890SDavid E. O'Brien } while (p < buf + bufsz); 774b9df5231SPoul-Henning Kamp bufp[i++] = 0; 775b9df5231SPoul-Henning Kamp return (bufp); 776b9df5231SPoul-Henning Kamp } 777ee5169dcSMikolaj Golub 778ee5169dcSMikolaj Golub char ** 779ee5169dcSMikolaj Golub kvm_getargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr) 780ee5169dcSMikolaj Golub { 781ee5169dcSMikolaj Golub return (kvm_argv(kd, kp, 0, nchr)); 78258f0484fSRodney W. Grimes } 78358f0484fSRodney W. Grimes 78458f0484fSRodney W. Grimes char ** 785c10970ddSUlrich Spörlein kvm_getenvv(kvm_t *kd, const struct kinfo_proc *kp, int nchr) 78658f0484fSRodney W. Grimes { 787ee5169dcSMikolaj Golub return (kvm_argv(kd, kp, 1, nchr)); 78858f0484fSRodney W. Grimes } 789