2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software developed by the Computer Systems
8 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
9 * BG 91-66 and contributed to Berkeley.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid[] = "@(#)kvm_proc.c 8.3 (Berkeley) 9/23/93";
39 #endif /* LIBC_SCCS and not lint */
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
46 * Proc traversal interface for kvm. ps and w are (probably) the exclusive
47 * users of this code, so we've factored it out into a separate module.
48 * Thus, we keep this grunge out of the other kvm applications (i.e.,
49 * most other applications are interested only in open/close/read/nlist).
52 #include <sys/param.h>
53 #define _WANT_UCRED /* make ucred.h give us 'struct ucred' */
54 #include <sys/ucred.h>
55 #include <sys/queue.h>
56 #include <sys/_lock.h>
57 #include <sys/_mutex.h>
58 #include <sys/_task.h>
59 #include <sys/cpuset.h>
62 #define _WANT_PRISON /* make jail.h give us 'struct prison' */
66 #include <sys/sysent.h>
67 #include <sys/ioctl.h>
71 #define _WANT_KW_EXITCODE
79 #include <sys/sysctl.h>
85 #include "kvm_private.h"
87 #define KREAD(kd, addr, obj) \
88 (kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj))
92 static uint64_t cpu_tick_frequency;
95 * From sys/kern/kern_tc.c. Depends on cpu_tick_frequency, which is
96 * read/initialized before this function is ever called.
99 cputick2usec(uint64_t tick)
102 if (cpu_tick_frequency == 0)
104 if (tick > 18446744073709551) /* floor(2^64 / 1000) */
105 return (tick / (cpu_tick_frequency / 1000000));
106 else if (tick > 18446744073709) /* floor(2^64 / 1000000) */
107 return ((tick * 1000) / (cpu_tick_frequency / 1000));
109 return ((tick * 1000000) / cpu_tick_frequency);
113 * Read proc's from memory file into buffer bp, which has space to hold
114 * at most maxcnt procs.
117 kvm_proclist(kvm_t *kd, int what, int arg, struct proc *p,
118 struct kinfo_proc *bp, int maxcnt)
121 struct kinfo_proc kinfo_proc, *kp;
126 struct vmspace vmspace;
127 struct sigacts sigacts;
129 struct pstats pstats;
136 struct sysentvec sysent;
137 char svname[KI_EMULNAMELEN];
140 kp->ki_structsize = sizeof(kinfo_proc);
142 * Loop on the processes. this is completely broken because we need to be
143 * able to loop on the threads and merge the ones that are the same process some how.
145 for (; cnt < maxcnt && p != NULL; p = LIST_NEXT(&proc, p_list)) {
146 memset(kp, 0, sizeof *kp);
147 if (KREAD(kd, (u_long)p, &proc)) {
148 _kvm_err(kd, kd->program, "can't read proc at %p", p);
151 if (proc.p_state == PRS_NEW)
153 if (proc.p_state != PRS_ZOMBIE) {
154 if (KREAD(kd, (u_long)TAILQ_FIRST(&proc.p_threads),
156 _kvm_err(kd, kd->program,
157 "can't read thread at %p",
158 TAILQ_FIRST(&proc.p_threads));
162 if (KREAD(kd, (u_long)proc.p_ucred, &ucred) == 0) {
163 kp->ki_ruid = ucred.cr_ruid;
164 kp->ki_svuid = ucred.cr_svuid;
165 kp->ki_rgid = ucred.cr_rgid;
166 kp->ki_svgid = ucred.cr_svgid;
167 kp->ki_cr_flags = ucred.cr_flags;
168 if (ucred.cr_ngroups > KI_NGROUPS) {
169 kp->ki_ngroups = KI_NGROUPS;
170 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
172 kp->ki_ngroups = ucred.cr_ngroups;
173 kvm_read(kd, (u_long)ucred.cr_groups, kp->ki_groups,
174 kp->ki_ngroups * sizeof(gid_t));
175 kp->ki_uid = ucred.cr_uid;
176 if (ucred.cr_prison != NULL) {
177 if (KREAD(kd, (u_long)ucred.cr_prison, &pr)) {
178 _kvm_err(kd, kd->program,
179 "can't read prison at %p",
183 kp->ki_jid = pr.pr_id;
187 switch(what & ~KERN_PROC_INC_THREAD) {
190 if (kp->ki_groups[0] != (gid_t)arg)
195 if (proc.p_pid != (pid_t)arg)
200 if (kp->ki_rgid != (gid_t)arg)
205 if (kp->ki_uid != (uid_t)arg)
210 if (kp->ki_ruid != (uid_t)arg)
215 * We're going to add another proc to the set. If this
216 * will overflow the buffer, assume the reason is because
217 * nprocs (or the proc list) is corrupt and declare an error.
220 _kvm_err(kd, kd->program, "nprocs corrupt");
227 kp->ki_addr = 0; /* XXX uarea */
228 /* kp->ki_kstack = proc.p_thread.td_kstack; XXXKSE */
229 kp->ki_args = proc.p_args;
230 kp->ki_tracep = proc.p_tracevp;
231 kp->ki_textvp = proc.p_textvp;
232 kp->ki_fd = proc.p_fd;
233 kp->ki_vmspace = proc.p_vmspace;
234 if (proc.p_sigacts != NULL) {
235 if (KREAD(kd, (u_long)proc.p_sigacts, &sigacts)) {
236 _kvm_err(kd, kd->program,
237 "can't read sigacts at %p", proc.p_sigacts);
240 kp->ki_sigignore = sigacts.ps_sigignore;
241 kp->ki_sigcatch = sigacts.ps_sigcatch;
244 if ((proc.p_flag & P_INMEM) && proc.p_stats != NULL) {
245 if (KREAD(kd, (u_long)proc.p_stats, &pstats)) {
246 _kvm_err(kd, kd->program,
247 "can't read stats at %x", proc.p_stats);
250 kp->ki_start = pstats.p_start;
253 * XXX: The times here are probably zero and need
254 * to be calculated from the raw data in p_rux and
257 kp->ki_rusage = pstats.p_ru;
258 kp->ki_childstime = pstats.p_cru.ru_stime;
259 kp->ki_childutime = pstats.p_cru.ru_utime;
260 /* Some callers want child-times in a single value */
261 timeradd(&kp->ki_childstime, &kp->ki_childutime,
266 kp->ki_ppid = proc.p_oppid;
267 else if (proc.p_pptr) {
268 if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) {
269 _kvm_err(kd, kd->program,
270 "can't read pproc at %p", proc.p_pptr);
273 kp->ki_ppid = pproc.p_pid;
276 if (proc.p_pgrp == NULL)
278 if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
279 _kvm_err(kd, kd->program, "can't read pgrp at %p",
283 kp->ki_pgid = pgrp.pg_id;
284 kp->ki_jobc = pgrp.pg_jobc;
285 if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
286 _kvm_err(kd, kd->program, "can't read session at %p",
290 kp->ki_sid = sess.s_sid;
291 (void)memcpy(kp->ki_login, sess.s_login,
292 sizeof(kp->ki_login));
293 kp->ki_kiflag = sess.s_ttyvp ? KI_CTTY : 0;
294 if (sess.s_leader == p)
295 kp->ki_kiflag |= KI_SLEADER;
296 if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) {
297 if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
298 _kvm_err(kd, kd->program,
299 "can't read tty at %p", sess.s_ttyp);
302 if (tty.t_dev != NULL) {
303 if (KREAD(kd, (u_long)tty.t_dev, &t_cdev)) {
304 _kvm_err(kd, kd->program,
305 "can't read cdev at %p",
310 kp->ki_tdev = t_cdev.si_udev;
315 if (tty.t_pgrp != NULL) {
316 if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
317 _kvm_err(kd, kd->program,
318 "can't read tpgrp at %p",
322 kp->ki_tpgid = pgrp.pg_id;
325 if (tty.t_session != NULL) {
326 if (KREAD(kd, (u_long)tty.t_session, &sess)) {
327 _kvm_err(kd, kd->program,
328 "can't read session at %p",
332 kp->ki_tsid = sess.s_sid;
338 if ((proc.p_state != PRS_ZOMBIE) && mtd.td_wmesg)
339 (void)kvm_read(kd, (u_long)mtd.td_wmesg,
340 kp->ki_wmesg, WMESGLEN);
342 (void)kvm_read(kd, (u_long)proc.p_vmspace,
343 (char *)&vmspace, sizeof(vmspace));
344 kp->ki_size = vmspace.vm_map.size;
346 * Approximate the kernel's method of calculating
349 #define pmap_resident_count(pm) ((pm)->pm_stats.resident_count)
350 kp->ki_rssize = pmap_resident_count(&vmspace.vm_pmap);
351 kp->ki_swrss = vmspace.vm_swrss;
352 kp->ki_tsize = vmspace.vm_tsize;
353 kp->ki_dsize = vmspace.vm_dsize;
354 kp->ki_ssize = vmspace.vm_ssize;
356 switch (what & ~KERN_PROC_INC_THREAD) {
359 if (kp->ki_pgid != (pid_t)arg)
363 case KERN_PROC_SESSION:
364 if (kp->ki_sid != (pid_t)arg)
369 if ((proc.p_flag & P_CONTROLT) == 0 ||
370 kp->ki_tdev != (dev_t)arg)
374 if (proc.p_comm[0] != 0)
375 strlcpy(kp->ki_comm, proc.p_comm, MAXCOMLEN);
376 (void)kvm_read(kd, (u_long)proc.p_sysent, (char *)&sysent,
378 (void)kvm_read(kd, (u_long)sysent.sv_name, (char *)&svname,
381 strlcpy(kp->ki_emul, svname, KI_EMULNAMELEN);
382 if ((proc.p_state != PRS_ZOMBIE) &&
383 (mtd.td_blocked != 0)) {
384 kp->ki_kiflag |= KI_LOCKBLOCK;
387 (u_long)mtd.td_lockname,
388 kp->ki_lockname, LOCKNAMELEN);
389 kp->ki_lockname[LOCKNAMELEN] = 0;
391 kp->ki_runtime = cputick2usec(proc.p_rux.rux_runtime);
392 kp->ki_pid = proc.p_pid;
393 kp->ki_siglist = proc.p_siglist;
394 SIGSETOR(kp->ki_siglist, mtd.td_siglist);
395 kp->ki_sigmask = mtd.td_sigmask;
396 kp->ki_xstat = KW_EXITCODE(proc.p_xexit, proc.p_xsig);
397 kp->ki_acflag = proc.p_acflag;
398 kp->ki_lock = proc.p_lock;
399 if (proc.p_state != PRS_ZOMBIE) {
400 kp->ki_swtime = (ticks - proc.p_swtick) / hz;
401 kp->ki_flag = proc.p_flag;
403 kp->ki_nice = proc.p_nice;
404 kp->ki_traceflag = proc.p_traceflag;
405 if (proc.p_state == PRS_NORMAL) {
406 if (TD_ON_RUNQ(&mtd) ||
408 TD_IS_RUNNING(&mtd)) {
410 } else if (mtd.td_state ==
412 if (P_SHOULDSTOP(&proc)) {
415 TD_IS_SLEEPING(&mtd)) {
416 kp->ki_stat = SSLEEP;
417 } else if (TD_ON_LOCK(&mtd)) {
426 /* Stuff from the thread */
427 kp->ki_pri.pri_level = mtd.td_priority;
428 kp->ki_pri.pri_native = mtd.td_base_pri;
429 kp->ki_lastcpu = mtd.td_lastcpu;
430 kp->ki_wchan = mtd.td_wchan;
431 kp->ki_oncpu = mtd.td_oncpu;
432 if (mtd.td_name[0] != '\0')
433 strlcpy(kp->ki_tdname, mtd.td_name, sizeof(kp->ki_tdname));
438 * Note: legacy fields; wraps at NO_CPU_OLD or the
439 * old max CPU value as appropriate
441 if (mtd.td_lastcpu == NOCPU)
442 kp->ki_lastcpu_old = NOCPU_OLD;
443 else if (mtd.td_lastcpu > MAXCPU_OLD)
444 kp->ki_lastcpu_old = MAXCPU_OLD;
446 kp->ki_lastcpu_old = mtd.td_lastcpu;
448 if (mtd.td_oncpu == NOCPU)
449 kp->ki_oncpu_old = NOCPU_OLD;
450 else if (mtd.td_oncpu > MAXCPU_OLD)
451 kp->ki_oncpu_old = MAXCPU_OLD;
453 kp->ki_oncpu_old = mtd.td_oncpu;
457 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
458 bcopy(&kinfo_proc, bp, sizeof(kinfo_proc));
466 * Build proc info array by reading in proc list from a crash dump.
467 * Return number of procs read. maxcnt is the max we will read.
470 kvm_deadprocs(kvm_t *kd, int what, int arg, u_long a_allproc,
471 u_long a_zombproc, int maxcnt)
473 struct kinfo_proc *bp = kd->procbase;
477 if (KREAD(kd, a_allproc, &p)) {
478 _kvm_err(kd, kd->program, "cannot read allproc");
481 acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
485 if (KREAD(kd, a_zombproc, &p)) {
486 _kvm_err(kd, kd->program, "cannot read zombproc");
489 zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt);
493 return (acnt + zcnt);
497 kvm_getprocs(kvm_t *kd, int op, int arg, int *cnt)
499 int mib[4], st, nprocs;
503 if (kd->procbase != 0) {
504 free((void *)kd->procbase);
506 * Clear this pointer in case this call fails. Otherwise,
507 * kvm_close() will free it again.
517 temp_op = op & ~KERN_PROC_INC_THREAD;
519 temp_op == KERN_PROC_ALL || temp_op == KERN_PROC_PROC ?
520 3 : 4, NULL, &size, NULL, 0);
522 _kvm_syserr(kd, kd->program, "kvm_getprocs");
526 * We can't continue with a size of 0 because we pass
527 * it to realloc() (via _kvm_realloc()), and passing 0
528 * to realloc() results in undefined behavior.
532 * XXX: We should probably return an invalid,
533 * but non-NULL, pointer here so any client
534 * program trying to dereference it will
535 * crash. However, _kvm_freeprocs() calls
536 * free() on kd->procbase if it isn't NULL,
537 * and free()'ing a junk pointer isn't good.
538 * Then again, _kvm_freeprocs() isn't used
541 kd->procbase = _kvm_malloc(kd, 1);
546 kd->procbase = (struct kinfo_proc *)
547 _kvm_realloc(kd, kd->procbase, size);
548 if (kd->procbase == NULL)
551 st = sysctl(mib, temp_op == KERN_PROC_ALL ||
552 temp_op == KERN_PROC_PROC ? 3 : 4,
553 kd->procbase, &size, NULL, 0);
554 } while (st == -1 && errno == ENOMEM && size == osize);
556 _kvm_syserr(kd, kd->program, "kvm_getprocs");
560 * We have to check the size again because sysctl()
561 * may "round up" oldlenp if oldp is NULL; hence it
562 * might've told us that there was data to get when
563 * there really isn't any.
566 kd->procbase->ki_structsize != sizeof(struct kinfo_proc)) {
567 _kvm_err(kd, kd->program,
568 "kinfo_proc size mismatch (expected %zu, got %d)",
569 sizeof(struct kinfo_proc),
570 kd->procbase->ki_structsize);
574 nprocs = size == 0 ? 0 : size / kd->procbase->ki_structsize;
576 struct nlist nl[7], *p;
578 nl[0].n_name = "_nprocs";
579 nl[1].n_name = "_allproc";
580 nl[2].n_name = "_zombproc";
581 nl[3].n_name = "_ticks";
582 nl[4].n_name = "_hz";
583 nl[5].n_name = "_cpu_tick_frequency";
586 if (!kd->arch->ka_native(kd)) {
587 _kvm_err(kd, kd->program,
588 "cannot read procs from non-native core");
592 if (kvm_nlist(kd, nl) != 0) {
593 for (p = nl; p->n_type != 0; ++p)
595 _kvm_err(kd, kd->program,
596 "%s: no such symbol", p->n_name);
599 if (KREAD(kd, nl[0].n_value, &nprocs)) {
600 _kvm_err(kd, kd->program, "can't read nprocs");
603 if (KREAD(kd, nl[3].n_value, &ticks)) {
604 _kvm_err(kd, kd->program, "can't read ticks");
607 if (KREAD(kd, nl[4].n_value, &hz)) {
608 _kvm_err(kd, kd->program, "can't read hz");
611 if (KREAD(kd, nl[5].n_value, &cpu_tick_frequency)) {
612 _kvm_err(kd, kd->program,
613 "can't read cpu_tick_frequency");
616 size = nprocs * sizeof(struct kinfo_proc);
617 kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
618 if (kd->procbase == NULL)
621 nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
622 nl[2].n_value, nprocs);
629 size = nprocs * sizeof(struct kinfo_proc);
630 kd->procbase = realloc(kd->procbase, size);
635 return (kd->procbase);
639 _kvm_freeprocs(kvm_t *kd)
647 _kvm_realloc(kvm_t *kd, void *p, size_t n)
653 _kvm_err(kd, kd->program, "out of memory");
658 * Get the command args or environment.
661 kvm_argv(kvm_t *kd, const struct kinfo_proc *kp, int env, int nchr)
667 static char *buf, *p;
673 _kvm_err(kd, kd->program,
674 "cannot read user space from dead kernel");
678 if (nchr == 0 || nchr > ARG_MAX)
683 _kvm_err(kd, kd->program, "cannot allocate memory");
687 bufp = malloc(sizeof(char *) * argc);
691 _kvm_err(kd, kd->program, "cannot allocate memory");
695 } else if (nchr > buflen) {
696 p = realloc(buf, nchr);
704 oid[2] = env ? KERN_PROC_ENV : KERN_PROC_ARGS;
707 if (sysctl(oid, 4, buf, &bufsz, 0, 0) == -1) {
709 * If the supplied buf is too short to hold the requested
710 * value the sysctl returns with ENOMEM. The buf is filled
711 * with the truncated value and the returned bufsz is equal
712 * to the requested len.
714 if (errno != ENOMEM || bufsz != (size_t)buflen)
716 buf[bufsz - 1] = '\0';
718 } else if (bufsz == 0)
727 nbufp = realloc(bufp, sizeof(char *) * argc);
732 } while (p < buf + bufsz);
738 kvm_getargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr)
740 return (kvm_argv(kd, kp, 0, nchr));
744 kvm_getenvv(kvm_t *kd, const struct kinfo_proc *kp, int nchr)
746 return (kvm_argv(kd, kp, 1, nchr));