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 if (cpu_tick_frequency == 0) 98de788839SUlrich Spörlein return (0); 99*bb53dd56Sfirk return ((tick / cpu_tick_frequency) * 1000000ULL) + 100*bb53dd56Sfirk ((tick % cpu_tick_frequency) * 1000000ULL) / cpu_tick_frequency; 101de788839SUlrich Spörlein } 10284a0b303SJeff Roberson 10358f0484fSRodney W. Grimes /* 10458f0484fSRodney W. Grimes * Read proc's from memory file into buffer bp, which has space to hold 10558f0484fSRodney W. Grimes * at most maxcnt procs. 10658f0484fSRodney W. Grimes */ 10758f0484fSRodney W. Grimes static int 108c10970ddSUlrich Spörlein kvm_proclist(kvm_t *kd, int what, int arg, struct proc *p, 109c10970ddSUlrich Spörlein struct kinfo_proc *bp, int maxcnt) 11058f0484fSRodney W. Grimes { 111be04b6d1SDavid E. O'Brien int cnt = 0; 1121f7d2501SKirk McKusick struct kinfo_proc kinfo_proc, *kp; 11358f0484fSRodney W. Grimes struct pgrp pgrp; 11458f0484fSRodney W. Grimes struct session sess; 1152bdd5609SPeter Wemm struct cdev t_cdev; 11658f0484fSRodney W. Grimes struct tty tty; 1171f7d2501SKirk McKusick struct vmspace vmspace; 118840558b9SJohn Baldwin struct sigacts sigacts; 119c10970ddSUlrich Spörlein #if 0 1201f7d2501SKirk McKusick struct pstats pstats; 121c10970ddSUlrich Spörlein #endif 1221f7d2501SKirk McKusick struct ucred ucred; 123f8197bf0SPawel Jakub Dawidek struct prison pr; 12471fad9fdSJulian Elischer struct thread mtd; 12558f0484fSRodney W. Grimes struct proc proc; 126a58930d8STor Egge struct proc pproc; 127276de18cSGarance A Drosehn struct sysentvec sysent; 128276de18cSGarance A Drosehn char svname[KI_EMULNAMELEN]; 129789f4e26SMike Karels struct thread *td = NULL; 130789f4e26SMike Karels bool first_thread; 13158f0484fSRodney W. Grimes 1321f7d2501SKirk McKusick kp = &kinfo_proc; 1331f7d2501SKirk McKusick kp->ki_structsize = sizeof(kinfo_proc); 1347ab24ea3SJulian Elischer /* 135789f4e26SMike Karels * Loop on the processes, then threads within the process if requested. 1367ab24ea3SJulian Elischer */ 137789f4e26SMike Karels if (what == KERN_PROC_ALL) 138789f4e26SMike Karels what |= KERN_PROC_INC_THREAD; 13942680b3aSBen Smithurst for (; cnt < maxcnt && p != NULL; p = LIST_NEXT(&proc, p_list)) { 1406f8c6a69SPeter Wemm memset(kp, 0, sizeof *kp); 14158f0484fSRodney W. Grimes if (KREAD(kd, (u_long)p, &proc)) { 142c10970ddSUlrich Spörlein _kvm_err(kd, kd->program, "can't read proc at %p", p); 14358f0484fSRodney W. Grimes return (-1); 14458f0484fSRodney W. Grimes } 1459fb9ea1cSAndriy Gapon if (proc.p_state == PRS_NEW) 1469fb9ea1cSAndriy Gapon continue; 147b1fc0ec1SRobert Watson if (KREAD(kd, (u_long)proc.p_ucred, &ucred) == 0) { 148b1fc0ec1SRobert Watson kp->ki_ruid = ucred.cr_ruid; 149b1fc0ec1SRobert Watson kp->ki_svuid = ucred.cr_svuid; 150b1fc0ec1SRobert Watson kp->ki_rgid = ucred.cr_rgid; 151b1fc0ec1SRobert Watson kp->ki_svgid = ucred.cr_svgid; 1521b5768beSBrooks Davis kp->ki_cr_flags = ucred.cr_flags; 1531b5768beSBrooks Davis if (ucred.cr_ngroups > KI_NGROUPS) { 1541b5768beSBrooks Davis kp->ki_ngroups = KI_NGROUPS; 1551b5768beSBrooks Davis kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW; 15668a5cc39SBrooks Davis } else 1571f7d2501SKirk McKusick kp->ki_ngroups = ucred.cr_ngroups; 158d534b0c2SBrooks Davis kvm_read(kd, (u_long)ucred.cr_groups, kp->ki_groups, 1591b5768beSBrooks Davis kp->ki_ngroups * sizeof(gid_t)); 1601f7d2501SKirk McKusick kp->ki_uid = ucred.cr_uid; 161f8197bf0SPawel Jakub Dawidek if (ucred.cr_prison != NULL) { 162f8197bf0SPawel Jakub Dawidek if (KREAD(kd, (u_long)ucred.cr_prison, &pr)) { 163f8197bf0SPawel Jakub Dawidek _kvm_err(kd, kd->program, 164c10970ddSUlrich Spörlein "can't read prison at %p", 165f8197bf0SPawel Jakub Dawidek ucred.cr_prison); 166f8197bf0SPawel Jakub Dawidek return (-1); 167f8197bf0SPawel Jakub Dawidek } 168f8197bf0SPawel Jakub Dawidek kp->ki_jid = pr.pr_id; 169f8197bf0SPawel Jakub Dawidek } 1701f7d2501SKirk McKusick } 17158f0484fSRodney W. Grimes 172694127f8SDaniel Eischen switch(what & ~KERN_PROC_INC_THREAD) { 17358f0484fSRodney W. Grimes 174276de18cSGarance A Drosehn case KERN_PROC_GID: 175276de18cSGarance A Drosehn if (kp->ki_groups[0] != (gid_t)arg) 176276de18cSGarance A Drosehn continue; 177276de18cSGarance A Drosehn break; 178276de18cSGarance A Drosehn 17958f0484fSRodney W. Grimes case KERN_PROC_PID: 18058f0484fSRodney W. Grimes if (proc.p_pid != (pid_t)arg) 18158f0484fSRodney W. Grimes continue; 18258f0484fSRodney W. Grimes break; 18358f0484fSRodney W. Grimes 184276de18cSGarance A Drosehn case KERN_PROC_RGID: 185276de18cSGarance A Drosehn if (kp->ki_rgid != (gid_t)arg) 186276de18cSGarance A Drosehn continue; 187276de18cSGarance A Drosehn break; 188276de18cSGarance A Drosehn 18958f0484fSRodney W. Grimes case KERN_PROC_UID: 1901f7d2501SKirk McKusick if (kp->ki_uid != (uid_t)arg) 19158f0484fSRodney W. Grimes continue; 19258f0484fSRodney W. Grimes break; 19358f0484fSRodney W. Grimes 19458f0484fSRodney W. Grimes case KERN_PROC_RUID: 1951f7d2501SKirk McKusick if (kp->ki_ruid != (uid_t)arg) 19658f0484fSRodney W. Grimes continue; 19758f0484fSRodney W. Grimes break; 19858f0484fSRodney W. Grimes } 19958f0484fSRodney W. Grimes /* 20058f0484fSRodney W. Grimes * We're going to add another proc to the set. If this 20158f0484fSRodney W. Grimes * will overflow the buffer, assume the reason is because 20258f0484fSRodney W. Grimes * nprocs (or the proc list) is corrupt and declare an error. 20358f0484fSRodney W. Grimes */ 20458f0484fSRodney W. Grimes if (cnt >= maxcnt) { 20558f0484fSRodney W. Grimes _kvm_err(kd, kd->program, "nprocs corrupt"); 20658f0484fSRodney W. Grimes return (-1); 20758f0484fSRodney W. Grimes } 20858f0484fSRodney W. Grimes /* 2091f7d2501SKirk McKusick * gather kinfo_proc 21058f0484fSRodney W. Grimes */ 2111f7d2501SKirk McKusick kp->ki_paddr = p; 2127a62aa8aSDavid Schultz kp->ki_addr = 0; /* XXX uarea */ 213b40ce416SJulian Elischer /* kp->ki_kstack = proc.p_thread.td_kstack; XXXKSE */ 2141f7d2501SKirk McKusick kp->ki_args = proc.p_args; 215789f4e26SMike Karels kp->ki_numthreads = proc.p_numthreads; 216e67ef6ceSKonstantin Belousov kp->ki_tracep = NULL; /* XXXKIB do not expose ktr_io_params */ 2171f7d2501SKirk McKusick kp->ki_textvp = proc.p_textvp; 2181f7d2501SKirk McKusick kp->ki_fd = proc.p_fd; 21985078b85SConrad Meyer kp->ki_pd = proc.p_pd; 2201f7d2501SKirk McKusick kp->ki_vmspace = proc.p_vmspace; 221840558b9SJohn Baldwin if (proc.p_sigacts != NULL) { 222840558b9SJohn Baldwin if (KREAD(kd, (u_long)proc.p_sigacts, &sigacts)) { 2231f7d2501SKirk McKusick _kvm_err(kd, kd->program, 224c10970ddSUlrich Spörlein "can't read sigacts at %p", proc.p_sigacts); 2251f7d2501SKirk McKusick return (-1); 2261f7d2501SKirk McKusick } 227840558b9SJohn Baldwin kp->ki_sigignore = sigacts.ps_sigignore; 228840558b9SJohn Baldwin kp->ki_sigcatch = sigacts.ps_sigcatch; 2291f7d2501SKirk McKusick } 2308ef6b142SJeff Roberson #if 0 231b61ce5b0SJeff Roberson if ((proc.p_flag & P_INMEM) && proc.p_stats != NULL) { 2321f7d2501SKirk McKusick if (KREAD(kd, (u_long)proc.p_stats, &pstats)) { 2331f7d2501SKirk McKusick _kvm_err(kd, kd->program, 2341f7d2501SKirk McKusick "can't read stats at %x", proc.p_stats); 2351f7d2501SKirk McKusick return (-1); 2361f7d2501SKirk McKusick } 2371f7d2501SKirk McKusick kp->ki_start = pstats.p_start; 23809ff687cSJohn Baldwin 23909ff687cSJohn Baldwin /* 24009ff687cSJohn Baldwin * XXX: The times here are probably zero and need 24109ff687cSJohn Baldwin * to be calculated from the raw data in p_rux and 24209ff687cSJohn Baldwin * p_crux. 24309ff687cSJohn Baldwin */ 2441f7d2501SKirk McKusick kp->ki_rusage = pstats.p_ru; 245276de18cSGarance A Drosehn kp->ki_childstime = pstats.p_cru.ru_stime; 246276de18cSGarance A Drosehn kp->ki_childutime = pstats.p_cru.ru_utime; 247276de18cSGarance A Drosehn /* Some callers want child-times in a single value */ 248276de18cSGarance A Drosehn timeradd(&kp->ki_childstime, &kp->ki_childutime, 249276de18cSGarance A Drosehn &kp->ki_childtime); 2501f7d2501SKirk McKusick } 2518ef6b142SJeff Roberson #endif 252a58930d8STor Egge if (proc.p_oppid) 2531f7d2501SKirk McKusick kp->ki_ppid = proc.p_oppid; 254a58930d8STor Egge else if (proc.p_pptr) { 255a58930d8STor Egge if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) { 2561f7d2501SKirk McKusick _kvm_err(kd, kd->program, 257c10970ddSUlrich Spörlein "can't read pproc at %p", proc.p_pptr); 258a58930d8STor Egge return (-1); 259a58930d8STor Egge } 2601f7d2501SKirk McKusick kp->ki_ppid = pproc.p_pid; 261a58930d8STor Egge } else 2621f7d2501SKirk McKusick kp->ki_ppid = 0; 2636f8c6a69SPeter Wemm if (proc.p_pgrp == NULL) 2646f8c6a69SPeter Wemm goto nopgrp; 2656f8c6a69SPeter Wemm if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) { 266c10970ddSUlrich Spörlein _kvm_err(kd, kd->program, "can't read pgrp at %p", 2676f8c6a69SPeter Wemm proc.p_pgrp); 2686f8c6a69SPeter Wemm return (-1); 2696f8c6a69SPeter Wemm } 2701f7d2501SKirk McKusick kp->ki_pgid = pgrp.pg_id; 2715844bd05SKonstantin Belousov kp->ki_jobc = -1; /* Or calculate? Arguably not. */ 27258f0484fSRodney W. Grimes if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) { 273c10970ddSUlrich Spörlein _kvm_err(kd, kd->program, "can't read session at %p", 27458f0484fSRodney W. Grimes pgrp.pg_session); 27558f0484fSRodney W. Grimes return (-1); 27658f0484fSRodney W. Grimes } 2771f7d2501SKirk McKusick kp->ki_sid = sess.s_sid; 2781f7d2501SKirk McKusick (void)memcpy(kp->ki_login, sess.s_login, 2791f7d2501SKirk McKusick sizeof(kp->ki_login)); 28058f0484fSRodney W. Grimes if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) { 28158f0484fSRodney W. Grimes if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) { 28258f0484fSRodney W. Grimes _kvm_err(kd, kd->program, 283c10970ddSUlrich Spörlein "can't read tty at %p", sess.s_ttyp); 28458f0484fSRodney W. Grimes return (-1); 28558f0484fSRodney W. Grimes } 2862bdd5609SPeter Wemm if (tty.t_dev != NULL) { 2872bdd5609SPeter Wemm if (KREAD(kd, (u_long)tty.t_dev, &t_cdev)) { 2882bdd5609SPeter Wemm _kvm_err(kd, kd->program, 289c10970ddSUlrich Spörlein "can't read cdev at %p", 2902bdd5609SPeter Wemm tty.t_dev); 2912bdd5609SPeter Wemm return (-1); 2922bdd5609SPeter Wemm } 2939c4fb661SPoul-Henning Kamp #if 0 2942bdd5609SPeter Wemm kp->ki_tdev = t_cdev.si_udev; 2959c4fb661SPoul-Henning Kamp #else 2962cfe3fdaSPeter Wemm kp->ki_tdev = NODEV; 2979c4fb661SPoul-Henning Kamp #endif 2982bdd5609SPeter Wemm } 29958f0484fSRodney W. Grimes if (tty.t_pgrp != NULL) { 30058f0484fSRodney W. Grimes if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) { 30158f0484fSRodney W. Grimes _kvm_err(kd, kd->program, 302c10970ddSUlrich Spörlein "can't read tpgrp at %p", 30358f0484fSRodney W. Grimes tty.t_pgrp); 30458f0484fSRodney W. Grimes return (-1); 30558f0484fSRodney W. Grimes } 3061f7d2501SKirk McKusick kp->ki_tpgid = pgrp.pg_id; 30758f0484fSRodney W. Grimes } else 3081f7d2501SKirk McKusick kp->ki_tpgid = -1; 3091f7d2501SKirk McKusick if (tty.t_session != NULL) { 3101f7d2501SKirk McKusick if (KREAD(kd, (u_long)tty.t_session, &sess)) { 3111f7d2501SKirk McKusick _kvm_err(kd, kd->program, 312c10970ddSUlrich Spörlein "can't read session at %p", 3131f7d2501SKirk McKusick tty.t_session); 3141f7d2501SKirk McKusick return (-1); 3151f7d2501SKirk McKusick } 3161f7d2501SKirk McKusick kp->ki_tsid = sess.s_sid; 3171f7d2501SKirk McKusick } 3186f8c6a69SPeter Wemm } else { 3196f8c6a69SPeter Wemm nopgrp: 3201f7d2501SKirk McKusick kp->ki_tdev = NODEV; 3216f8c6a69SPeter Wemm } 32258f0484fSRodney W. Grimes 32358f0484fSRodney W. Grimes (void)kvm_read(kd, (u_long)proc.p_vmspace, 3241f7d2501SKirk McKusick (char *)&vmspace, sizeof(vmspace)); 3251f7d2501SKirk McKusick kp->ki_size = vmspace.vm_map.size; 3265f494640SSean Bruno /* 3275f494640SSean Bruno * Approximate the kernel's method of calculating 3285f494640SSean Bruno * this field. 3295f494640SSean Bruno */ 3305f494640SSean Bruno #define pmap_resident_count(pm) ((pm)->pm_stats.resident_count) 3315f494640SSean Bruno kp->ki_rssize = pmap_resident_count(&vmspace.vm_pmap); 3321f7d2501SKirk McKusick kp->ki_swrss = vmspace.vm_swrss; 3331f7d2501SKirk McKusick kp->ki_tsize = vmspace.vm_tsize; 3341f7d2501SKirk McKusick kp->ki_dsize = vmspace.vm_dsize; 3351f7d2501SKirk McKusick kp->ki_ssize = vmspace.vm_ssize; 33658f0484fSRodney W. Grimes 337694127f8SDaniel Eischen switch (what & ~KERN_PROC_INC_THREAD) { 33858f0484fSRodney W. Grimes 33958f0484fSRodney W. Grimes case KERN_PROC_PGRP: 3401f7d2501SKirk McKusick if (kp->ki_pgid != (pid_t)arg) 34158f0484fSRodney W. Grimes continue; 34258f0484fSRodney W. Grimes break; 34358f0484fSRodney W. Grimes 344276de18cSGarance A Drosehn case KERN_PROC_SESSION: 345276de18cSGarance A Drosehn if (kp->ki_sid != (pid_t)arg) 346276de18cSGarance A Drosehn continue; 347276de18cSGarance A Drosehn break; 348276de18cSGarance A Drosehn 34958f0484fSRodney W. Grimes case KERN_PROC_TTY: 35058f0484fSRodney W. Grimes if ((proc.p_flag & P_CONTROLT) == 0 || 3511f7d2501SKirk McKusick kp->ki_tdev != (dev_t)arg) 35258f0484fSRodney W. Grimes continue; 35358f0484fSRodney W. Grimes break; 35458f0484fSRodney W. Grimes } 355b7e7c21aSGarance A Drosehn if (proc.p_comm[0] != 0) 356b7e7c21aSGarance A Drosehn strlcpy(kp->ki_comm, proc.p_comm, MAXCOMLEN); 357276de18cSGarance A Drosehn (void)kvm_read(kd, (u_long)proc.p_sysent, (char *)&sysent, 358276de18cSGarance A Drosehn sizeof(sysent)); 359276de18cSGarance A Drosehn (void)kvm_read(kd, (u_long)sysent.sv_name, (char *)&svname, 360276de18cSGarance A Drosehn sizeof(svname)); 361276de18cSGarance A Drosehn if (svname[0] != 0) 362276de18cSGarance A Drosehn strlcpy(kp->ki_emul, svname, KI_EMULNAMELEN); 363789f4e26SMike Karels kp->ki_runtime = cputick2usec(proc.p_rux.rux_runtime); 364789f4e26SMike Karels kp->ki_pid = proc.p_pid; 365789f4e26SMike Karels kp->ki_xstat = KW_EXITCODE(proc.p_xexit, proc.p_xsig); 366789f4e26SMike Karels kp->ki_acflag = proc.p_acflag; 367789f4e26SMike Karels kp->ki_lock = proc.p_lock; 368789f4e26SMike Karels kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */ 369789f4e26SMike Karels 370789f4e26SMike Karels /* Per-thread items; iterate as appropriate. */ 371789f4e26SMike Karels td = TAILQ_FIRST(&proc.p_threads); 372789f4e26SMike Karels for (first_thread = true; cnt < maxcnt && td != NULL && 373789f4e26SMike Karels (first_thread || (what & KERN_PROC_INC_THREAD)); 374789f4e26SMike Karels first_thread = false) { 375789f4e26SMike Karels if (proc.p_state != PRS_ZOMBIE) { 376789f4e26SMike Karels if (KREAD(kd, (u_long)td, &mtd)) { 377789f4e26SMike Karels _kvm_err(kd, kd->program, 378789f4e26SMike Karels "can't read thread at %p", td); 379789f4e26SMike Karels return (-1); 380789f4e26SMike Karels } 381789f4e26SMike Karels if (what & KERN_PROC_INC_THREAD) 382789f4e26SMike Karels td = TAILQ_NEXT(&mtd, td_plist); 383789f4e26SMike Karels } else 384789f4e26SMike Karels td = NULL; 385789f4e26SMike Karels if ((proc.p_state != PRS_ZOMBIE) && mtd.td_wmesg) 386789f4e26SMike Karels (void)kvm_read(kd, (u_long)mtd.td_wmesg, 387789f4e26SMike Karels kp->ki_wmesg, WMESGLEN); 388789f4e26SMike Karels else 389789f4e26SMike Karels memset(kp->ki_wmesg, 0, WMESGLEN); 390789f4e26SMike Karels if (proc.p_pgrp == NULL) { 391789f4e26SMike Karels kp->ki_kiflag = 0; 392789f4e26SMike Karels } else { 393789f4e26SMike Karels kp->ki_kiflag = sess.s_ttyvp ? KI_CTTY : 0; 394789f4e26SMike Karels if (sess.s_leader == p) 395789f4e26SMike Karels kp->ki_kiflag |= KI_SLEADER; 396789f4e26SMike Karels } 397bff4151cSJulian Elischer if ((proc.p_state != PRS_ZOMBIE) && 39871fad9fdSJulian Elischer (mtd.td_blocked != 0)) { 3990d632649SJohn Baldwin kp->ki_kiflag |= KI_LOCKBLOCK; 4000d632649SJohn Baldwin if (mtd.td_lockname) 401bff4151cSJulian Elischer (void)kvm_read(kd, 4020d632649SJohn Baldwin (u_long)mtd.td_lockname, 4030d632649SJohn Baldwin kp->ki_lockname, LOCKNAMELEN); 404789f4e26SMike Karels else 405789f4e26SMike Karels memset(kp->ki_lockname, 0, 406789f4e26SMike Karels LOCKNAMELEN); 4070d632649SJohn Baldwin kp->ki_lockname[LOCKNAMELEN] = 0; 408789f4e26SMike Karels } else 409789f4e26SMike Karels kp->ki_kiflag &= ~KI_LOCKBLOCK; 410e8a58a83SJuli Mallett kp->ki_siglist = proc.p_siglist; 411789f4e26SMike Karels if (proc.p_state != PRS_ZOMBIE) { 41242d3ad71SJeff Roberson SIGSETOR(kp->ki_siglist, mtd.td_siglist); 41331a9779eSJeff Roberson kp->ki_sigmask = mtd.td_sigmask; 41484a0b303SJeff Roberson kp->ki_swtime = (ticks - proc.p_swtick) / hz; 4156143c383SJulian Elischer kp->ki_flag = proc.p_flag; 416b61ce5b0SJeff Roberson kp->ki_sflag = 0; 41730105366SJulian Elischer kp->ki_nice = proc.p_nice; 4181f7d2501SKirk McKusick kp->ki_traceflag = proc.p_traceflag; 4196143c383SJulian Elischer if (proc.p_state == PRS_NORMAL) { 42071fad9fdSJulian Elischer if (TD_ON_RUNQ(&mtd) || 42171fad9fdSJulian Elischer TD_CAN_RUN(&mtd) || 42271fad9fdSJulian Elischer TD_IS_RUNNING(&mtd)) { 423e602ba25SJulian Elischer kp->ki_stat = SRUN; 424fa2528acSAlex Richardson } else if (TD_GET_STATE(&mtd) == 4254f0db5e0SJulian Elischer TDS_INHIBITED) { 42671fad9fdSJulian Elischer if (P_SHOULDSTOP(&proc)) { 427e602ba25SJulian Elischer kp->ki_stat = SSTOP; 4284f0db5e0SJulian Elischer } else if ( 4294f0db5e0SJulian Elischer TD_IS_SLEEPING(&mtd)) { 43071fad9fdSJulian Elischer kp->ki_stat = SSLEEP; 4310d632649SJohn Baldwin } else if (TD_ON_LOCK(&mtd)) { 4320d632649SJohn Baldwin kp->ki_stat = SLOCK; 433e602ba25SJulian Elischer } else { 434e602ba25SJulian Elischer kp->ki_stat = SWAIT; 435e602ba25SJulian Elischer } 43671fad9fdSJulian Elischer } 437e602ba25SJulian Elischer } else { 438e602ba25SJulian Elischer kp->ki_stat = SIDL; 439e602ba25SJulian Elischer } 4404f0db5e0SJulian Elischer /* Stuff from the thread */ 44171fad9fdSJulian Elischer kp->ki_pri.pri_level = mtd.td_priority; 44271fad9fdSJulian Elischer kp->ki_pri.pri_native = mtd.td_base_pri; 44371fad9fdSJulian Elischer kp->ki_lastcpu = mtd.td_lastcpu; 4444f0db5e0SJulian Elischer kp->ki_wchan = mtd.td_wchan; 445a0ddbf49SJulian Elischer kp->ki_oncpu = mtd.td_oncpu; 4467ab24ea3SJulian Elischer if (mtd.td_name[0] != '\0') 447789f4e26SMike Karels strlcpy(kp->ki_tdname, mtd.td_name, 448789f4e26SMike Karels sizeof(kp->ki_tdname)); 449789f4e26SMike Karels else 450789f4e26SMike Karels memset(kp->ki_tdname, 0, 451789f4e26SMike Karels sizeof(kp->ki_tdname)); 452ed062c8dSJulian Elischer kp->ki_pctcpu = 0; 453ed062c8dSJulian Elischer kp->ki_rqindex = 0; 454e77f9fedSAdrian Chadd 455e77f9fedSAdrian Chadd /* 456789f4e26SMike Karels * Note: legacy fields; wraps at NO_CPU_OLD 457789f4e26SMike Karels * or the old max CPU value as appropriate 458e77f9fedSAdrian Chadd */ 459e77f9fedSAdrian Chadd if (mtd.td_lastcpu == NOCPU) 460e77f9fedSAdrian Chadd kp->ki_lastcpu_old = NOCPU_OLD; 461e77f9fedSAdrian Chadd else if (mtd.td_lastcpu > MAXCPU_OLD) 462e77f9fedSAdrian Chadd kp->ki_lastcpu_old = MAXCPU_OLD; 463e77f9fedSAdrian Chadd else 464e77f9fedSAdrian Chadd kp->ki_lastcpu_old = mtd.td_lastcpu; 465e77f9fedSAdrian Chadd 466e77f9fedSAdrian Chadd if (mtd.td_oncpu == NOCPU) 467e77f9fedSAdrian Chadd kp->ki_oncpu_old = NOCPU_OLD; 468e77f9fedSAdrian Chadd else if (mtd.td_oncpu > MAXCPU_OLD) 469e77f9fedSAdrian Chadd kp->ki_oncpu_old = MAXCPU_OLD; 470e77f9fedSAdrian Chadd else 471e77f9fedSAdrian Chadd kp->ki_oncpu_old = mtd.td_oncpu; 472789f4e26SMike Karels kp->ki_tid = mtd.td_tid; 4734f0db5e0SJulian Elischer } else { 474789f4e26SMike Karels memset(&kp->ki_sigmask, 0, 475789f4e26SMike Karels sizeof(kp->ki_sigmask)); 4766143c383SJulian Elischer kp->ki_stat = SZOMB; 477789f4e26SMike Karels kp->ki_tid = 0; 4786143c383SJulian Elischer } 479789f4e26SMike Karels 4801f7d2501SKirk McKusick bcopy(&kinfo_proc, bp, sizeof(kinfo_proc)); 48158f0484fSRodney W. Grimes ++bp; 48258f0484fSRodney W. Grimes ++cnt; 48358f0484fSRodney W. Grimes } 484789f4e26SMike Karels } 48558f0484fSRodney W. Grimes return (cnt); 48658f0484fSRodney W. Grimes } 48758f0484fSRodney W. Grimes 48858f0484fSRodney W. Grimes /* 48958f0484fSRodney W. Grimes * Build proc info array by reading in proc list from a crash dump. 49058f0484fSRodney W. Grimes * Return number of procs read. maxcnt is the max we will read. 49158f0484fSRodney W. Grimes */ 49258f0484fSRodney W. Grimes static int 493c10970ddSUlrich Spörlein kvm_deadprocs(kvm_t *kd, int what, int arg, u_long a_allproc, 494c10970ddSUlrich Spörlein u_long a_zombproc, int maxcnt) 49558f0484fSRodney W. Grimes { 496be04b6d1SDavid E. O'Brien struct kinfo_proc *bp = kd->procbase; 497789f4e26SMike Karels int acnt, zcnt = 0; 49858f0484fSRodney W. Grimes struct proc *p; 49958f0484fSRodney W. Grimes 50058f0484fSRodney W. Grimes if (KREAD(kd, a_allproc, &p)) { 50158f0484fSRodney W. Grimes _kvm_err(kd, kd->program, "cannot read allproc"); 50258f0484fSRodney W. Grimes return (-1); 50358f0484fSRodney W. Grimes } 50458f0484fSRodney W. Grimes acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt); 50558f0484fSRodney W. Grimes if (acnt < 0) 50658f0484fSRodney W. Grimes return (acnt); 50758f0484fSRodney W. Grimes 508789f4e26SMike Karels if (a_zombproc != 0) { 50958f0484fSRodney W. Grimes if (KREAD(kd, a_zombproc, &p)) { 51058f0484fSRodney W. Grimes _kvm_err(kd, kd->program, "cannot read zombproc"); 51158f0484fSRodney W. Grimes return (-1); 51258f0484fSRodney W. Grimes } 51358f0484fSRodney W. Grimes zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt); 51458f0484fSRodney W. Grimes if (zcnt < 0) 51558f0484fSRodney W. Grimes zcnt = 0; 516789f4e26SMike Karels } 51758f0484fSRodney W. Grimes 51858f0484fSRodney W. Grimes return (acnt + zcnt); 51958f0484fSRodney W. Grimes } 52058f0484fSRodney W. Grimes 52158f0484fSRodney W. Grimes struct kinfo_proc * 522c10970ddSUlrich Spörlein kvm_getprocs(kvm_t *kd, int op, int arg, int *cnt) 52358f0484fSRodney W. Grimes { 524c2ac238cSDoug Rabson int mib[4], st, nprocs; 525767993ecSMikolaj Golub size_t size, osize; 526694127f8SDaniel Eischen int temp_op; 52758f0484fSRodney W. Grimes 52858f0484fSRodney W. Grimes if (kd->procbase != 0) { 52958f0484fSRodney W. Grimes free((void *)kd->procbase); 53058f0484fSRodney W. Grimes /* 53158f0484fSRodney W. Grimes * Clear this pointer in case this call fails. Otherwise, 53258f0484fSRodney W. Grimes * kvm_close() will free it again. 53358f0484fSRodney W. Grimes */ 53458f0484fSRodney W. Grimes kd->procbase = 0; 53558f0484fSRodney W. Grimes } 53658f0484fSRodney W. Grimes if (ISALIVE(kd)) { 53758f0484fSRodney W. Grimes size = 0; 53858f0484fSRodney W. Grimes mib[0] = CTL_KERN; 53958f0484fSRodney W. Grimes mib[1] = KERN_PROC; 54058f0484fSRodney W. Grimes mib[2] = op; 54158f0484fSRodney W. Grimes mib[3] = arg; 542694127f8SDaniel Eischen temp_op = op & ~KERN_PROC_INC_THREAD; 543694127f8SDaniel Eischen st = sysctl(mib, 544694127f8SDaniel Eischen temp_op == KERN_PROC_ALL || temp_op == KERN_PROC_PROC ? 545f2dd06abSTim J. Robbins 3 : 4, NULL, &size, NULL, 0); 54658f0484fSRodney W. Grimes if (st == -1) { 54758f0484fSRodney W. Grimes _kvm_syserr(kd, kd->program, "kvm_getprocs"); 54858f0484fSRodney W. Grimes return (0); 54958f0484fSRodney W. Grimes } 55021687047SDima Dorfman /* 55121687047SDima Dorfman * We can't continue with a size of 0 because we pass 55221687047SDima Dorfman * it to realloc() (via _kvm_realloc()), and passing 0 55321687047SDima Dorfman * to realloc() results in undefined behavior. 55421687047SDima Dorfman */ 55521687047SDima Dorfman if (size == 0) { 55621687047SDima Dorfman /* 55721687047SDima Dorfman * XXX: We should probably return an invalid, 55821687047SDima Dorfman * but non-NULL, pointer here so any client 55921687047SDima Dorfman * program trying to dereference it will 56021687047SDima Dorfman * crash. However, _kvm_freeprocs() calls 56121687047SDima Dorfman * free() on kd->procbase if it isn't NULL, 56221687047SDima Dorfman * and free()'ing a junk pointer isn't good. 56321687047SDima Dorfman * Then again, _kvm_freeprocs() isn't used 56421687047SDima Dorfman * anywhere . . . 56521687047SDima Dorfman */ 56621687047SDima Dorfman kd->procbase = _kvm_malloc(kd, 1); 56721687047SDima Dorfman goto liveout; 56821687047SDima Dorfman } 569b2d3d0f0SDag-Erling Smørgrav do { 570b2d3d0f0SDag-Erling Smørgrav size += size / 10; 571b2d3d0f0SDag-Erling Smørgrav kd->procbase = (struct kinfo_proc *) 572b2d3d0f0SDag-Erling Smørgrav _kvm_realloc(kd, kd->procbase, size); 573fb0e1892SEnji Cooper if (kd->procbase == NULL) 57458f0484fSRodney W. Grimes return (0); 575767993ecSMikolaj Golub osize = size; 576694127f8SDaniel Eischen st = sysctl(mib, temp_op == KERN_PROC_ALL || 577694127f8SDaniel Eischen temp_op == KERN_PROC_PROC ? 3 : 4, 578b2d3d0f0SDag-Erling Smørgrav kd->procbase, &size, NULL, 0); 579767993ecSMikolaj Golub } while (st == -1 && errno == ENOMEM && size == osize); 58058f0484fSRodney W. Grimes if (st == -1) { 58158f0484fSRodney W. Grimes _kvm_syserr(kd, kd->program, "kvm_getprocs"); 58258f0484fSRodney W. Grimes return (0); 58358f0484fSRodney W. Grimes } 58421687047SDima Dorfman /* 58521687047SDima Dorfman * We have to check the size again because sysctl() 58621687047SDima Dorfman * may "round up" oldlenp if oldp is NULL; hence it 58721687047SDima Dorfman * might've told us that there was data to get when 58821687047SDima Dorfman * there really isn't any. 58921687047SDima Dorfman */ 5900627f53bSDavid Malone if (size > 0 && 5910627f53bSDavid Malone kd->procbase->ki_structsize != sizeof(struct kinfo_proc)) { 59258f0484fSRodney W. Grimes _kvm_err(kd, kd->program, 593c10970ddSUlrich Spörlein "kinfo_proc size mismatch (expected %zu, got %d)", 5941f7d2501SKirk McKusick sizeof(struct kinfo_proc), 5951f7d2501SKirk McKusick kd->procbase->ki_structsize); 59658f0484fSRodney W. Grimes return (0); 59758f0484fSRodney W. Grimes } 59821687047SDima Dorfman liveout: 599d339edcfSDavid Malone nprocs = size == 0 ? 0 : size / kd->procbase->ki_structsize; 60058f0484fSRodney W. Grimes } else { 601789f4e26SMike Karels struct nlist nl[6], *p; 602789f4e26SMike Karels struct nlist nlz[2]; 60358f0484fSRodney W. Grimes 60458f0484fSRodney W. Grimes nl[0].n_name = "_nprocs"; 60558f0484fSRodney W. Grimes nl[1].n_name = "_allproc"; 606789f4e26SMike Karels nl[2].n_name = "_ticks"; 607789f4e26SMike Karels nl[3].n_name = "_hz"; 608789f4e26SMike Karels nl[4].n_name = "_cpu_tick_frequency"; 609789f4e26SMike Karels nl[5].n_name = 0; 610789f4e26SMike Karels 611789f4e26SMike Karels nlz[0].n_name = "_zombproc"; 612789f4e26SMike Karels nlz[1].n_name = 0; 61358f0484fSRodney W. Grimes 6147f911abeSJohn Baldwin if (!kd->arch->ka_native(kd)) { 6157f911abeSJohn Baldwin _kvm_err(kd, kd->program, 6167f911abeSJohn Baldwin "cannot read procs from non-native core"); 6177f911abeSJohn Baldwin return (0); 6187f911abeSJohn Baldwin } 6197f911abeSJohn Baldwin 62058f0484fSRodney W. Grimes if (kvm_nlist(kd, nl) != 0) { 62158f0484fSRodney W. Grimes for (p = nl; p->n_type != 0; ++p) 62258f0484fSRodney W. Grimes ; 62358f0484fSRodney W. Grimes _kvm_err(kd, kd->program, 62458f0484fSRodney W. Grimes "%s: no such symbol", p->n_name); 62558f0484fSRodney W. Grimes return (0); 62658f0484fSRodney W. Grimes } 627789f4e26SMike Karels (void) kvm_nlist(kd, nlz); /* attempt to get zombproc */ 62858f0484fSRodney W. Grimes if (KREAD(kd, nl[0].n_value, &nprocs)) { 62958f0484fSRodney W. Grimes _kvm_err(kd, kd->program, "can't read nprocs"); 63058f0484fSRodney W. Grimes return (0); 63158f0484fSRodney W. Grimes } 632789f4e26SMike Karels /* 633789f4e26SMike Karels * If returning all threads, we don't know how many that 634789f4e26SMike Karels * might be. Presume that there are, on average, no more 635789f4e26SMike Karels * than 10 threads per process. 636789f4e26SMike Karels */ 637789f4e26SMike Karels if (op == KERN_PROC_ALL || (op & KERN_PROC_INC_THREAD)) 638789f4e26SMike Karels nprocs *= 10; /* XXX */ 639789f4e26SMike Karels if (KREAD(kd, nl[2].n_value, &ticks)) { 64084a0b303SJeff Roberson _kvm_err(kd, kd->program, "can't read ticks"); 64184a0b303SJeff Roberson return (0); 64284a0b303SJeff Roberson } 643789f4e26SMike Karels if (KREAD(kd, nl[3].n_value, &hz)) { 64484a0b303SJeff Roberson _kvm_err(kd, kd->program, "can't read hz"); 64584a0b303SJeff Roberson return (0); 64684a0b303SJeff Roberson } 647789f4e26SMike Karels if (KREAD(kd, nl[4].n_value, &cpu_tick_frequency)) { 648de788839SUlrich Spörlein _kvm_err(kd, kd->program, 649de788839SUlrich Spörlein "can't read cpu_tick_frequency"); 650de788839SUlrich Spörlein return (0); 651de788839SUlrich Spörlein } 65258f0484fSRodney W. Grimes size = nprocs * sizeof(struct kinfo_proc); 65358f0484fSRodney W. Grimes kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size); 654fb0e1892SEnji Cooper if (kd->procbase == NULL) 65558f0484fSRodney W. Grimes return (0); 65658f0484fSRodney W. Grimes 65758f0484fSRodney W. Grimes nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value, 658789f4e26SMike Karels nlz[0].n_value, nprocs); 659ca895af3SAndriy Gapon if (nprocs <= 0) { 660ca895af3SAndriy Gapon _kvm_freeprocs(kd); 661ca895af3SAndriy Gapon nprocs = 0; 662ca895af3SAndriy Gapon } 66358f0484fSRodney W. Grimes #ifdef notdef 664ca895af3SAndriy Gapon else { 66558f0484fSRodney W. Grimes size = nprocs * sizeof(struct kinfo_proc); 666ca895af3SAndriy Gapon kd->procbase = realloc(kd->procbase, size); 667ca895af3SAndriy Gapon } 66858f0484fSRodney W. Grimes #endif 66958f0484fSRodney W. Grimes } 67058f0484fSRodney W. Grimes *cnt = nprocs; 67158f0484fSRodney W. Grimes return (kd->procbase); 67258f0484fSRodney W. Grimes } 67358f0484fSRodney W. Grimes 67458f0484fSRodney W. Grimes void 675c10970ddSUlrich Spörlein _kvm_freeprocs(kvm_t *kd) 67658f0484fSRodney W. Grimes { 677fb0e1892SEnji Cooper 67858f0484fSRodney W. Grimes free(kd->procbase); 679fb0e1892SEnji Cooper kd->procbase = NULL; 68058f0484fSRodney W. Grimes } 68158f0484fSRodney W. Grimes 68258f0484fSRodney W. Grimes void * 683c10970ddSUlrich Spörlein _kvm_realloc(kvm_t *kd, void *p, size_t n) 68458f0484fSRodney W. Grimes { 685fb0e1892SEnji Cooper void *np; 68658f0484fSRodney W. Grimes 687fb0e1892SEnji Cooper np = reallocf(p, n); 688fb0e1892SEnji Cooper if (np == NULL) 68958f0484fSRodney W. Grimes _kvm_err(kd, kd->program, "out of memory"); 69058f0484fSRodney W. Grimes return (np); 69158f0484fSRodney W. Grimes } 69258f0484fSRodney W. Grimes 69358f0484fSRodney W. Grimes /* 694ee5169dcSMikolaj Golub * Get the command args or environment. 69558f0484fSRodney W. Grimes */ 69658f0484fSRodney W. Grimes static char ** 697ee5169dcSMikolaj Golub kvm_argv(kvm_t *kd, const struct kinfo_proc *kp, int env, int nchr) 69858f0484fSRodney W. Grimes { 699b9df5231SPoul-Henning Kamp int oid[4]; 700b7875890SDavid E. O'Brien int i; 701b7875890SDavid E. O'Brien size_t bufsz; 702ee5169dcSMikolaj Golub static int buflen; 703b9df5231SPoul-Henning Kamp static char *buf, *p; 704b9df5231SPoul-Henning Kamp static char **bufp; 705b9df5231SPoul-Henning Kamp static int argc; 70668b68bf5SEnji Cooper char **nbufp; 707b9df5231SPoul-Henning Kamp 708c4a7cdb3SPeter Wemm if (!ISALIVE(kd)) { 709c4a7cdb3SPeter Wemm _kvm_err(kd, kd->program, 710c4a7cdb3SPeter Wemm "cannot read user space from dead kernel"); 711fb0e1892SEnji Cooper return (NULL); 712c4a7cdb3SPeter Wemm } 713c4a7cdb3SPeter Wemm 714ee5169dcSMikolaj Golub if (nchr == 0 || nchr > ARG_MAX) 715ee5169dcSMikolaj Golub nchr = ARG_MAX; 716ee5169dcSMikolaj Golub if (buflen == 0) { 717ee5169dcSMikolaj Golub buf = malloc(nchr); 718ee5169dcSMikolaj Golub if (buf == NULL) { 719ee5169dcSMikolaj Golub _kvm_err(kd, kd->program, "cannot allocate memory"); 720fb0e1892SEnji Cooper return (NULL); 721ee5169dcSMikolaj Golub } 722b9df5231SPoul-Henning Kamp argc = 32; 723b9df5231SPoul-Henning Kamp bufp = malloc(sizeof(char *) * argc); 72468b68bf5SEnji Cooper if (bufp == NULL) { 72568b68bf5SEnji Cooper free(buf); 72668b68bf5SEnji Cooper buf = NULL; 72768b68bf5SEnji Cooper _kvm_err(kd, kd->program, "cannot allocate memory"); 72868b68bf5SEnji Cooper return (NULL); 72968b68bf5SEnji Cooper } 73068b68bf5SEnji Cooper buflen = nchr; 731ee5169dcSMikolaj Golub } else if (nchr > buflen) { 732ee5169dcSMikolaj Golub p = realloc(buf, nchr); 733ee5169dcSMikolaj Golub if (p != NULL) { 734ee5169dcSMikolaj Golub buf = p; 735ee5169dcSMikolaj Golub buflen = nchr; 736b9df5231SPoul-Henning Kamp } 737b9df5231SPoul-Henning Kamp } 738b9df5231SPoul-Henning Kamp oid[0] = CTL_KERN; 739b9df5231SPoul-Henning Kamp oid[1] = KERN_PROC; 740ee5169dcSMikolaj Golub oid[2] = env ? KERN_PROC_ENV : KERN_PROC_ARGS; 7411f7d2501SKirk McKusick oid[3] = kp->ki_pid; 742b7875890SDavid E. O'Brien bufsz = buflen; 743952a0c3eSMikolaj Golub if (sysctl(oid, 4, buf, &bufsz, 0, 0) == -1) { 744952a0c3eSMikolaj Golub /* 745952a0c3eSMikolaj Golub * If the supplied buf is too short to hold the requested 746952a0c3eSMikolaj Golub * value the sysctl returns with ENOMEM. The buf is filled 747952a0c3eSMikolaj Golub * with the truncated value and the returned bufsz is equal 748952a0c3eSMikolaj Golub * to the requested len. 749952a0c3eSMikolaj Golub */ 750952a0c3eSMikolaj Golub if (errno != ENOMEM || bufsz != (size_t)buflen) 751fb0e1892SEnji Cooper return (NULL); 752952a0c3eSMikolaj Golub buf[bufsz - 1] = '\0'; 753952a0c3eSMikolaj Golub errno = 0; 754fb0e1892SEnji Cooper } else if (bufsz == 0) 755fb0e1892SEnji Cooper return (NULL); 756b9df5231SPoul-Henning Kamp i = 0; 757b9df5231SPoul-Henning Kamp p = buf; 758b9df5231SPoul-Henning Kamp do { 759b9df5231SPoul-Henning Kamp bufp[i++] = p; 760b9df5231SPoul-Henning Kamp p += strlen(p) + 1; 761b9df5231SPoul-Henning Kamp if (i >= argc) { 762b9df5231SPoul-Henning Kamp argc += argc; 76368b68bf5SEnji Cooper nbufp = realloc(bufp, sizeof(char *) * argc); 76468b68bf5SEnji Cooper if (nbufp == NULL) 76568b68bf5SEnji Cooper return (NULL); 76668b68bf5SEnji Cooper bufp = nbufp; 767b9df5231SPoul-Henning Kamp } 768b7875890SDavid E. O'Brien } while (p < buf + bufsz); 769b9df5231SPoul-Henning Kamp bufp[i++] = 0; 770b9df5231SPoul-Henning Kamp return (bufp); 771b9df5231SPoul-Henning Kamp } 772ee5169dcSMikolaj Golub 773ee5169dcSMikolaj Golub char ** 774ee5169dcSMikolaj Golub kvm_getargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr) 775ee5169dcSMikolaj Golub { 776ee5169dcSMikolaj Golub return (kvm_argv(kd, kp, 0, nchr)); 77758f0484fSRodney W. Grimes } 77858f0484fSRodney W. Grimes 77958f0484fSRodney W. Grimes char ** 780c10970ddSUlrich Spörlein kvm_getenvv(kvm_t *kd, const struct kinfo_proc *kp, int nchr) 78158f0484fSRodney W. Grimes { 782ee5169dcSMikolaj Golub return (kvm_argv(kd, kp, 1, nchr)); 78358f0484fSRodney W. Grimes } 784