1 /* $NetBSD: kvm_proc.c,v 1.90 2014/02/19 20:21:22 dsl Exp $ */
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1989, 1992, 1993
34 * The Regents of the University of California. All rights reserved.
36 * This code is derived from software developed by the Computer Systems
37 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
38 * BG 91-66 and contributed to Berkeley.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 #include <sys/cdefs.h>
66 #if defined(LIBC_SCCS) && !defined(lint)
68 static char sccsid
[] = "@(#)kvm_proc.c 8.3 (Berkeley) 9/23/93";
70 __RCSID("$NetBSD: kvm_proc.c,v 1.90 2014/02/19 20:21:22 dsl Exp $");
72 #endif /* LIBC_SCCS and not lint */
75 * Proc traversal interface for kvm. ps and w are (probably) the exclusive
76 * users of this code, so we've factored it out into a separate module.
77 * Thus, we keep this grunge out of the other kvm applications (i.e.,
78 * most other applications are interested only in open/close/read/nlist).
81 #include <sys/param.h>
86 #include <sys/ioctl.h>
88 #include <sys/resourcevar.h>
89 #include <sys/mutex.h>
90 #include <sys/specificdata.h>
91 #include <sys/types.h>
101 #include <uvm/uvm_extern.h>
102 #include <uvm/uvm_param.h>
103 #include <uvm/uvm_amap.h>
104 #include <uvm/uvm_page.h>
106 #include <sys/sysctl.h>
112 #include "kvm_private.h"
115 * Common info from kinfo_proc and kinfo_proc2 used by helper routines.
118 struct vmspace
*p_vmspace
;
120 struct proc
*p_paddr
;
125 * Convert from struct proc and kinfo_proc{,2} to miniproc.
127 #define PTOMINI(kp, p) \
129 (p)->p_stat = (kp)->p_stat; \
130 (p)->p_pid = (kp)->p_pid; \
131 (p)->p_paddr = NULL; \
132 (p)->p_vmspace = (kp)->p_vmspace; \
133 } while (/*CONSTCOND*/0);
135 #define KPTOMINI(kp, p) \
137 (p)->p_stat = (kp)->kp_proc.p_stat; \
138 (p)->p_pid = (kp)->kp_proc.p_pid; \
139 (p)->p_paddr = (kp)->kp_eproc.e_paddr; \
140 (p)->p_vmspace = (kp)->kp_proc.p_vmspace; \
141 } while (/*CONSTCOND*/0);
143 #define KP2TOMINI(kp, p) \
145 (p)->p_stat = (kp)->p_stat; \
146 (p)->p_pid = (kp)->p_pid; \
147 (p)->p_paddr = (void *)(long)(kp)->p_paddr; \
148 (p)->p_vmspace = (void *)(long)(kp)->p_vmspace; \
149 } while (/*CONSTCOND*/0);
152 * NetBSD uses kauth(9) to manage credentials, which are stored in kauth_cred_t,
153 * a kernel-only opaque type. This is an embedded version which is *INTERNAL* to
154 * kvm(3) so dumps can be read properly.
156 * Whenever NetBSD starts exporting credentials to userland consistently (using
157 * 'struct uucred', or something) this will have to be updated again.
159 struct kvm_kauth_cred
{
160 u_int cr_refcnt
; /* reference count */
161 uint8_t cr_pad
[CACHE_LINE_SIZE
- sizeof(u_int
)];
162 uid_t cr_uid
; /* user id */
163 uid_t cr_euid
; /* effective user id */
164 uid_t cr_svuid
; /* saved effective user id */
165 gid_t cr_gid
; /* group id */
166 gid_t cr_egid
; /* effective group id */
167 gid_t cr_svgid
; /* saved effective group id */
168 u_int cr_ngroups
; /* number of groups */
169 gid_t cr_groups
[NGROUPS
]; /* group memberships */
170 specificdata_reference cr_sd
; /* specific data */
173 /* XXX: What uses these two functions? */
174 char *_kvm_uread(kvm_t
*, const struct proc
*, u_long
, u_long
*);
175 ssize_t
kvm_uread(kvm_t
*, const struct proc
*, u_long
, char *,
178 static char *_kvm_ureadm(kvm_t
*, const struct miniproc
*, u_long
,
180 static ssize_t
kvm_ureadm(kvm_t
*, const struct miniproc
*, u_long
,
183 static char **kvm_argv(kvm_t
*, const struct miniproc
*, u_long
, int, int);
184 static int kvm_deadprocs(kvm_t
*, int, int, u_long
, u_long
, int);
185 static char **kvm_doargv(kvm_t
*, const struct miniproc
*, int,
186 void (*)(struct ps_strings
*, u_long
*, int *));
187 static char **kvm_doargv2(kvm_t
*, pid_t
, int, int);
188 static int kvm_proclist(kvm_t
*, int, int, struct proc
*,
189 struct kinfo_proc
*, int);
190 static int proc_verify(kvm_t
*, u_long
, const struct miniproc
*);
191 static void ps_str_a(struct ps_strings
*, u_long
*, int *);
192 static void ps_str_e(struct ps_strings
*, u_long
*, int *);
196 _kvm_ureadm(kvm_t
*kd
, const struct miniproc
*p
, u_long va
, u_long
*cnt
)
200 struct vm_map_entry vme
;
202 struct vm_anon
*anonp
, anon
;
206 if (kd
->swapspc
== NULL
) {
207 kd
->swapspc
= _kvm_malloc(kd
, (size_t)kd
->nbpg
);
208 if (kd
->swapspc
== NULL
)
213 * Look through the address map for the memory object
214 * that corresponds to the given virtual address.
215 * The header just has the entire valid range.
217 head
= (u_long
)&p
->p_vmspace
->vm_map
.header
;
220 if (KREAD(kd
, addr
, &vme
))
223 if (va
>= vme
.start
&& va
< vme
.end
&&
224 vme
.aref
.ar_amap
!= NULL
)
227 addr
= (u_long
)vme
.next
;
233 * we found the map entry, now to find the object...
235 if (vme
.aref
.ar_amap
== NULL
)
238 addr
= (u_long
)vme
.aref
.ar_amap
;
239 if (KREAD(kd
, addr
, &amap
))
242 offset
= va
- vme
.start
;
243 slot
= offset
/ kd
->nbpg
+ vme
.aref
.ar_pageoff
;
244 /* sanity-check slot number */
245 if (slot
> amap
.am_nslot
)
248 addr
= (u_long
)amap
.am_anon
+ (offset
/ kd
->nbpg
) * sizeof(anonp
);
249 if (KREAD(kd
, addr
, &anonp
))
252 addr
= (u_long
)anonp
;
253 if (KREAD(kd
, addr
, &anon
))
256 addr
= (u_long
)anon
.an_page
;
258 if (KREAD(kd
, addr
, &pg
))
261 if (_kvm_pread(kd
, kd
->pmfd
, kd
->swapspc
, (size_t)kd
->nbpg
,
262 (off_t
)pg
.phys_addr
) != kd
->nbpg
)
266 _kvm_pread(kd
, kd
->swfd
, kd
->swapspc
, (size_t)kd
->nbpg
,
267 (off_t
)(anon
.an_swslot
* kd
->nbpg
)) != kd
->nbpg
)
271 /* Found the page. */
273 *cnt
= kd
->nbpg
- offset
;
274 return (&kd
->swapspc
[(size_t)offset
]);
278 _kvm_uread(kvm_t
*kd
, const struct proc
*p
, u_long va
, u_long
*cnt
)
283 return (_kvm_ureadm(kd
, &mp
, va
, cnt
));
287 * Convert credentials located in kernel space address 'cred' and store
288 * them in the appropriate members of 'eproc'.
291 _kvm_convertcred(kvm_t
*kd
, u_long cred
, struct eproc
*eproc
)
293 struct kvm_kauth_cred kauthcred
;
294 struct ki_pcred
*pc
= &eproc
->e_pcred
;
295 struct ki_ucred
*uc
= &eproc
->e_ucred
;
297 if (KREAD(kd
, cred
, &kauthcred
) != 0)
300 /* inlined version of kauth_cred_to_pcred, see kauth(9). */
301 pc
->p_ruid
= kauthcred
.cr_uid
;
302 pc
->p_svuid
= kauthcred
.cr_svuid
;
303 pc
->p_rgid
= kauthcred
.cr_gid
;
304 pc
->p_svgid
= kauthcred
.cr_svgid
;
305 pc
->p_refcnt
= kauthcred
.cr_refcnt
;
308 /* inlined version of kauth_cred_to_ucred(), see kauth(9). */
309 uc
->cr_ref
= kauthcred
.cr_refcnt
;
310 uc
->cr_uid
= kauthcred
.cr_euid
;
311 uc
->cr_gid
= kauthcred
.cr_egid
;
312 uc
->cr_ngroups
= (uint32_t)MIN(kauthcred
.cr_ngroups
,
313 sizeof(uc
->cr_groups
) / sizeof(uc
->cr_groups
[0]));
314 memcpy(uc
->cr_groups
, kauthcred
.cr_groups
,
315 uc
->cr_ngroups
* sizeof(uc
->cr_groups
[0]));
321 * Read proc's from memory file into buffer bp, which has space to hold
322 * at most maxcnt procs.
325 kvm_proclist(kvm_t
*kd
, int what
, int arg
, struct proc
*p
,
326 struct kinfo_proc
*bp
, int maxcnt
)
330 struct kinfo_lwp
*kl
;
337 for (; cnt
< maxcnt
&& p
!= NULL
; p
= proc
.p_list
.le_next
) {
338 if (KREAD(kd
, (u_long
)p
, &proc
)) {
339 _kvm_err(kd
, kd
->program
, "can't read proc at %p", p
);
342 if (_kvm_convertcred(kd
, (u_long
)proc
.p_cred
, &eproc
) != 0) {
343 _kvm_err(kd
, kd
->program
,
344 "can't read proc credentials at %p", p
);
351 if (proc
.p_pid
!= (pid_t
)arg
)
356 if (eproc
.e_ucred
.cr_uid
!= (uid_t
)arg
)
361 if (eproc
.e_pcred
.p_ruid
!= (uid_t
)arg
)
366 * We're going to add another proc to the set. If this
367 * will overflow the buffer, assume the reason is because
368 * nprocs (or the proc list) is corrupt and declare an error.
371 _kvm_err(kd
, kd
->program
, "nprocs corrupt");
378 if (KREAD(kd
, (u_long
)proc
.p_pgrp
, &pgrp
)) {
379 _kvm_err(kd
, kd
->program
, "can't read pgrp at %p",
383 eproc
.e_sess
= pgrp
.pg_session
;
384 eproc
.e_pgid
= pgrp
.pg_id
;
385 eproc
.e_jobc
= pgrp
.pg_jobc
;
386 if (KREAD(kd
, (u_long
)pgrp
.pg_session
, &sess
)) {
387 _kvm_err(kd
, kd
->program
, "can't read session at %p",
391 if ((proc
.p_lflag
& PL_CONTROLT
) && sess
.s_ttyp
!= NULL
) {
392 if (KREAD(kd
, (u_long
)sess
.s_ttyp
, &tty
)) {
393 _kvm_err(kd
, kd
->program
,
394 "can't read tty at %p", sess
.s_ttyp
);
397 eproc
.e_tdev
= (uint32_t)tty
.t_dev
;
398 eproc
.e_tsess
= tty
.t_session
;
399 if (tty
.t_pgrp
!= NULL
) {
400 if (KREAD(kd
, (u_long
)tty
.t_pgrp
, &pgrp
)) {
401 _kvm_err(kd
, kd
->program
,
402 "can't read tpgrp at %p",
406 eproc
.e_tpgid
= pgrp
.pg_id
;
410 eproc
.e_tdev
= (uint32_t)NODEV
;
411 eproc
.e_flag
= sess
.s_ttyvp
? EPROC_CTTY
: 0;
412 eproc
.e_sid
= sess
.s_sid
;
413 if (sess
.s_leader
== p
)
414 eproc
.e_flag
|= EPROC_SLEADER
;
416 * Fill in the old-style proc.p_wmesg by copying the wmesg
417 * from the first available LWP.
419 kl
= kvm_getlwps(kd
, proc
.p_pid
,
420 (u_long
)PTRTOUINT64(eproc
.e_paddr
),
421 sizeof(struct kinfo_lwp
), &nlwps
);
424 strcpy(eproc
.e_wmesg
, kl
[0].l_wmesg
);
427 (void)kvm_read(kd
, (u_long
)proc
.p_vmspace
, &eproc
.e_vm
,
430 eproc
.e_xsize
= eproc
.e_xrssize
= 0;
431 eproc
.e_xccount
= eproc
.e_xswrss
= 0;
436 if (eproc
.e_pgid
!= (pid_t
)arg
)
441 if ((proc
.p_lflag
& PL_CONTROLT
) == 0 ||
442 eproc
.e_tdev
!= (dev_t
)arg
)
446 memcpy(&bp
->kp_proc
, &proc
, sizeof(proc
));
447 memcpy(&bp
->kp_eproc
, &eproc
, sizeof(eproc
));
455 * Build proc info array by reading in proc list from a crash dump.
456 * Return number of procs read. maxcnt is the max we will read.
459 kvm_deadprocs(kvm_t
*kd
, int what
, int arg
, u_long a_allproc
,
460 u_long a_zombproc
, int maxcnt
)
462 struct kinfo_proc
*bp
= kd
->procbase
;
466 if (KREAD(kd
, a_allproc
, &p
)) {
467 _kvm_err(kd
, kd
->program
, "cannot read allproc");
470 acnt
= kvm_proclist(kd
, what
, arg
, p
, bp
, maxcnt
);
474 if (KREAD(kd
, a_zombproc
, &p
)) {
475 _kvm_err(kd
, kd
->program
, "cannot read zombproc");
478 zcnt
= kvm_proclist(kd
, what
, arg
, p
, bp
+ acnt
,
483 return (acnt
+ zcnt
);
487 kvm_getproc2(kvm_t
*kd
, int op
, int arg
, size_t esize
, int *cnt
)
490 int mib
[6], st
, nprocs
;
491 struct pstats pstats
;
502 st
= sysctl(mib
, 6, NULL
, &size
, NULL
, (size_t)0);
504 _kvm_syserr(kd
, kd
->program
, "kvm_getproc2");
508 mib
[5] = (int) (size
/ esize
);
509 KVM_ALLOC(kd
, procbase2
, size
);
510 st
= sysctl(mib
, 6, kd
->procbase2
, &size
, NULL
, (size_t)0);
512 if (errno
== ENOMEM
) {
515 _kvm_syserr(kd
, kd
->program
, "kvm_getproc2");
518 nprocs
= (int) (size
/ esize
);
521 struct kinfo_proc
*kp
;
522 struct kinfo_proc2 kp2
, *kp2p
;
523 struct kinfo_lwp
*kl
;
526 kp
= kvm_getprocs(kd
, op
, arg
, &nprocs
);
530 size
= nprocs
* esize
;
531 KVM_ALLOC(kd
, procbase2
, size
);
532 kp2c
= (char *)(void *)kd
->procbase2
;
534 for (i
= 0; i
< nprocs
; i
++, kp
++) {
537 kl
= kvm_getlwps(kd
, kp
->kp_proc
.p_pid
,
538 (u_long
)PTRTOUINT64(kp
->kp_eproc
.e_paddr
),
539 sizeof(struct kinfo_lwp
), &nlwps
);
542 _kvm_syserr(kd
, NULL
,
543 "kvm_getlwps() failed on process %u\n",
551 /* We use kl[0] as the "representative" LWP */
552 memset(kp2p
, 0, sizeof(kp2
));
553 kp2p
->p_forw
= kl
[0].l_forw
;
554 kp2p
->p_back
= kl
[0].l_back
;
555 kp2p
->p_paddr
= PTRTOUINT64(kp
->kp_eproc
.e_paddr
);
556 kp2p
->p_addr
= kl
[0].l_addr
;
557 kp2p
->p_fd
= PTRTOUINT64(kp
->kp_proc
.p_fd
);
558 kp2p
->p_cwdi
= PTRTOUINT64(kp
->kp_proc
.p_cwdi
);
559 kp2p
->p_stats
= PTRTOUINT64(kp
->kp_proc
.p_stats
);
560 kp2p
->p_limit
= PTRTOUINT64(kp
->kp_proc
.p_limit
);
561 kp2p
->p_vmspace
= PTRTOUINT64(kp
->kp_proc
.p_vmspace
);
562 kp2p
->p_sigacts
= PTRTOUINT64(kp
->kp_proc
.p_sigacts
);
563 kp2p
->p_sess
= PTRTOUINT64(kp
->kp_eproc
.e_sess
);
565 #if 1 /* XXX: dsl - p_ru was only ever non-zero for zombies */
568 kp2p
->p_ru
= PTRTOUINT64(pstats
.p_ru
);
572 kp2p
->p_exitsig
= kp
->kp_proc
.p_exitsig
;
573 kp2p
->p_flag
= kp
->kp_proc
.p_flag
;
575 kp2p
->p_pid
= kp
->kp_proc
.p_pid
;
577 kp2p
->p_ppid
= kp
->kp_eproc
.e_ppid
;
578 kp2p
->p_sid
= kp
->kp_eproc
.e_sid
;
579 kp2p
->p__pgid
= kp
->kp_eproc
.e_pgid
;
581 kp2p
->p_tpgid
= -1 /* XXX NO_PGID! */;
583 kp2p
->p_uid
= kp
->kp_eproc
.e_ucred
.cr_uid
;
584 kp2p
->p_ruid
= kp
->kp_eproc
.e_pcred
.p_ruid
;
585 kp2p
->p_svuid
= kp
->kp_eproc
.e_pcred
.p_svuid
;
586 kp2p
->p_gid
= kp
->kp_eproc
.e_ucred
.cr_gid
;
587 kp2p
->p_rgid
= kp
->kp_eproc
.e_pcred
.p_rgid
;
588 kp2p
->p_svgid
= kp
->kp_eproc
.e_pcred
.p_svgid
;
591 memcpy(kp2p
->p_groups
, kp
->kp_eproc
.e_ucred
.cr_groups
,
592 MIN(sizeof(kp2p
->p_groups
),
593 sizeof(kp
->kp_eproc
.e_ucred
.cr_groups
)));
594 kp2p
->p_ngroups
= kp
->kp_eproc
.e_ucred
.cr_ngroups
;
596 kp2p
->p_jobc
= kp
->kp_eproc
.e_jobc
;
597 kp2p
->p_tdev
= kp
->kp_eproc
.e_tdev
;
598 kp2p
->p_tpgid
= kp
->kp_eproc
.e_tpgid
;
599 kp2p
->p_tsess
= PTRTOUINT64(kp
->kp_eproc
.e_tsess
);
602 bintime2timeval(&kp
->kp_proc
.p_rtime
, &tv
);
603 kp2p
->p_rtime_sec
= (uint32_t)tv
.tv_sec
;
604 kp2p
->p_rtime_usec
= (uint32_t)tv
.tv_usec
;
605 kp2p
->p_cpticks
= kl
[0].l_cpticks
;
606 kp2p
->p_pctcpu
= kp
->kp_proc
.p_pctcpu
;
607 kp2p
->p_swtime
= kl
[0].l_swtime
;
608 kp2p
->p_slptime
= kl
[0].l_slptime
;
609 #if 0 /* XXX thorpej */
610 kp2p
->p_schedflags
= kp
->kp_proc
.p_schedflags
;
612 kp2p
->p_schedflags
= 0;
615 kp2p
->p_uticks
= kp
->kp_proc
.p_uticks
;
616 kp2p
->p_sticks
= kp
->kp_proc
.p_sticks
;
617 kp2p
->p_iticks
= kp
->kp_proc
.p_iticks
;
619 kp2p
->p_tracep
= PTRTOUINT64(kp
->kp_proc
.p_tracep
);
620 kp2p
->p_traceflag
= kp
->kp_proc
.p_traceflag
;
622 kp2p
->p_holdcnt
= kl
[0].l_holdcnt
;
624 memcpy(&kp2p
->p_siglist
,
625 &kp
->kp_proc
.p_sigpend
.sp_set
,
626 sizeof(ki_sigset_t
));
627 memset(&kp2p
->p_sigmask
, 0,
628 sizeof(ki_sigset_t
));
629 memcpy(&kp2p
->p_sigignore
,
630 &kp
->kp_proc
.p_sigctx
.ps_sigignore
,
631 sizeof(ki_sigset_t
));
632 memcpy(&kp2p
->p_sigcatch
,
633 &kp
->kp_proc
.p_sigctx
.ps_sigcatch
,
634 sizeof(ki_sigset_t
));
636 kp2p
->p_stat
= kl
[0].l_stat
;
637 kp2p
->p_priority
= kl
[0].l_priority
;
638 kp2p
->p_usrpri
= kl
[0].l_priority
;
639 kp2p
->p_nice
= kp
->kp_proc
.p_nice
;
641 kp2p
->p_xstat
= kp
->kp_proc
.p_xstat
;
642 kp2p
->p_acflag
= kp
->kp_proc
.p_acflag
;
645 strncpy(kp2p
->p_comm
, kp
->kp_proc
.p_comm
,
646 MIN(sizeof(kp2p
->p_comm
),
647 sizeof(kp
->kp_proc
.p_comm
)));
649 strncpy(kp2p
->p_wmesg
, kp
->kp_eproc
.e_wmesg
,
650 sizeof(kp2p
->p_wmesg
));
651 kp2p
->p_wchan
= kl
[0].l_wchan
;
652 strncpy(kp2p
->p_login
, kp
->kp_eproc
.e_login
,
653 sizeof(kp2p
->p_login
));
655 kp2p
->p_vm_rssize
= kp
->kp_eproc
.e_xrssize
;
656 kp2p
->p_vm_tsize
= kp
->kp_eproc
.e_vm
.vm_tsize
;
657 kp2p
->p_vm_dsize
= kp
->kp_eproc
.e_vm
.vm_dsize
;
658 kp2p
->p_vm_ssize
= kp
->kp_eproc
.e_vm
.vm_ssize
;
659 kp2p
->p_vm_vsize
= kp
->kp_eproc
.e_vm
.vm_map
.size
661 /* Adjust mapped size */
663 (kp
->kp_eproc
.e_vm
.vm_map
.size
/ kd
->nbpg
) -
664 kp
->kp_eproc
.e_vm
.vm_issize
+
665 kp
->kp_eproc
.e_vm
.vm_ssize
;
667 kp2p
->p_eflag
= (int32_t)kp
->kp_eproc
.e_flag
;
669 kp2p
->p_realflag
= kp
->kp_proc
.p_flag
;
670 kp2p
->p_nlwps
= kp
->kp_proc
.p_nlwps
;
671 kp2p
->p_nrlwps
= kp
->kp_proc
.p_nrlwps
;
672 kp2p
->p_realstat
= kp
->kp_proc
.p_stat
;
674 if (P_ZOMBIE(&kp
->kp_proc
) ||
675 kp
->kp_proc
.p_stats
== NULL
||
676 KREAD(kd
, (u_long
)kp
->kp_proc
.p_stats
, &pstats
)) {
681 kp2p
->p_ustart_sec
= (u_int32_t
)
682 pstats
.p_start
.tv_sec
;
683 kp2p
->p_ustart_usec
= (u_int32_t
)
684 pstats
.p_start
.tv_usec
;
686 kp2p
->p_uutime_sec
= (u_int32_t
)
687 pstats
.p_ru
.ru_utime
.tv_sec
;
688 kp2p
->p_uutime_usec
= (u_int32_t
)
689 pstats
.p_ru
.ru_utime
.tv_usec
;
690 kp2p
->p_ustime_sec
= (u_int32_t
)
691 pstats
.p_ru
.ru_stime
.tv_sec
;
692 kp2p
->p_ustime_usec
= (u_int32_t
)
693 pstats
.p_ru
.ru_stime
.tv_usec
;
695 kp2p
->p_uru_maxrss
= pstats
.p_ru
.ru_maxrss
;
696 kp2p
->p_uru_ixrss
= pstats
.p_ru
.ru_ixrss
;
697 kp2p
->p_uru_idrss
= pstats
.p_ru
.ru_idrss
;
698 kp2p
->p_uru_isrss
= pstats
.p_ru
.ru_isrss
;
699 kp2p
->p_uru_minflt
= pstats
.p_ru
.ru_minflt
;
700 kp2p
->p_uru_majflt
= pstats
.p_ru
.ru_majflt
;
701 kp2p
->p_uru_nswap
= pstats
.p_ru
.ru_nswap
;
702 kp2p
->p_uru_inblock
= pstats
.p_ru
.ru_inblock
;
703 kp2p
->p_uru_oublock
= pstats
.p_ru
.ru_oublock
;
704 kp2p
->p_uru_msgsnd
= pstats
.p_ru
.ru_msgsnd
;
705 kp2p
->p_uru_msgrcv
= pstats
.p_ru
.ru_msgrcv
;
706 kp2p
->p_uru_nsignals
= pstats
.p_ru
.ru_nsignals
;
707 kp2p
->p_uru_nvcsw
= pstats
.p_ru
.ru_nvcsw
;
708 kp2p
->p_uru_nivcsw
= pstats
.p_ru
.ru_nivcsw
;
710 kp2p
->p_uctime_sec
= (u_int32_t
)
711 (pstats
.p_cru
.ru_utime
.tv_sec
+
712 pstats
.p_cru
.ru_stime
.tv_sec
);
713 kp2p
->p_uctime_usec
= (u_int32_t
)
714 (pstats
.p_cru
.ru_utime
.tv_usec
+
715 pstats
.p_cru
.ru_stime
.tv_usec
);
718 memcpy(kp2c
, &kp2
, esize
);
723 return (kd
->procbase2
);
727 kvm_getlwps(kvm_t
*kd
, int pid
, u_long paddr
, size_t esize
, int *cnt
)
732 struct kinfo_lwp
*kl
;
742 st
= sysctl(mib
, 5, NULL
, &size
, NULL
, (size_t)0);
745 case ESRCH
: /* Treat this as a soft error; see kvm.c */
746 _kvm_syserr(kd
, NULL
, "kvm_getlwps");
749 _kvm_syserr(kd
, kd
->program
, "kvm_getlwps");
753 mib
[4] = (int) (size
/ esize
);
754 KVM_ALLOC(kd
, lwpbase
, size
);
755 st
= sysctl(mib
, 5, kd
->lwpbase
, &size
, NULL
, (size_t)0);
758 case ESRCH
: /* Treat this as a soft error; see kvm.c */
759 _kvm_syserr(kd
, NULL
, "kvm_getlwps");
764 _kvm_syserr(kd
, kd
->program
, "kvm_getlwps");
768 nlwps
= (int) (size
/ esize
);
770 /* grovel through the memory image */
777 st
= kvm_read(kd
, paddr
, &p
, sizeof(p
));
779 _kvm_syserr(kd
, kd
->program
, "kvm_getlwps");
784 size
= nlwps
* sizeof(*kd
->lwpbase
);
785 KVM_ALLOC(kd
, lwpbase
, size
);
786 laddr
= (u_long
)PTRTOUINT64(p
.p_lwps
.lh_first
);
787 for (i
= 0; (i
< nlwps
) && (laddr
!= 0); i
++) {
788 st
= kvm_read(kd
, laddr
, &l
, sizeof(l
));
790 _kvm_syserr(kd
, kd
->program
, "kvm_getlwps");
793 kl
= &kd
->lwpbase
[i
];
795 kl
->l_forw
= PTRTOUINT64(l
.l_runq
.tqe_next
);
796 laddr
= (u_long
)PTRTOUINT64(l
.l_runq
.tqe_prev
);
797 st
= kvm_read(kd
, laddr
, &back
, sizeof(back
));
799 _kvm_syserr(kd
, kd
->program
, "kvm_getlwps");
802 kl
->l_back
= PTRTOUINT64(back
);
803 kl
->l_addr
= PTRTOUINT64(l
.l_addr
);
805 kl
->l_flag
= l
.l_flag
;
806 kl
->l_swtime
= l
.l_swtime
;
807 kl
->l_slptime
= l
.l_slptime
;
808 kl
->l_schedflags
= 0; /* XXX */
810 kl
->l_priority
= l
.l_priority
;
811 kl
->l_usrpri
= l
.l_priority
;
812 kl
->l_stat
= l
.l_stat
;
813 kl
->l_wchan
= PTRTOUINT64(l
.l_wchan
);
815 (void)kvm_read(kd
, (u_long
)l
.l_wmesg
,
816 kl
->l_wmesg
, (size_t)WMESGLEN
);
817 kl
->l_cpuid
= KI_NOCPU
;
818 laddr
= (u_long
)PTRTOUINT64(l
.l_sibling
.le_next
);
823 return (kd
->lwpbase
);
827 kvm_getprocs(kvm_t
*kd
, int op
, int arg
, int *cnt
)
830 int mib
[4], st
, nprocs
;
838 st
= sysctl(mib
, 4, NULL
, &size
, NULL
, (size_t)0);
840 _kvm_syserr(kd
, kd
->program
, "kvm_getprocs");
843 KVM_ALLOC(kd
, procbase
, size
);
844 st
= sysctl(mib
, 4, kd
->procbase
, &size
, NULL
, (size_t)0);
846 _kvm_syserr(kd
, kd
->program
, "kvm_getprocs");
849 if (size
% sizeof(struct kinfo_proc
) != 0) {
850 _kvm_err(kd
, kd
->program
,
851 "proc size mismatch (%lu total, %lu chunks)",
852 (u_long
)size
, (u_long
)sizeof(struct kinfo_proc
));
855 nprocs
= (int) (size
/ sizeof(struct kinfo_proc
));
857 struct nlist nl
[4], *p
;
859 (void)memset(nl
, 0, sizeof(nl
));
860 nl
[0].n_name
= "_nprocs";
861 nl
[1].n_name
= "_allproc";
862 nl
[2].n_name
= "_zombproc";
865 if (kvm_nlist(kd
, nl
) != 0) {
866 for (p
= nl
; p
->n_type
!= 0; ++p
)
868 _kvm_err(kd
, kd
->program
,
869 "%s: no such symbol", p
->n_name
);
872 if (KREAD(kd
, nl
[0].n_value
, &nprocs
)) {
873 _kvm_err(kd
, kd
->program
, "can't read nprocs");
876 size
= nprocs
* sizeof(*kd
->procbase
);
877 KVM_ALLOC(kd
, procbase
, size
);
878 nprocs
= kvm_deadprocs(kd
, op
, arg
, nl
[1].n_value
,
879 nl
[2].n_value
, nprocs
);
883 size
= nprocs
* sizeof(struct kinfo_proc
);
884 (void)realloc(kd
->procbase
, size
);
888 return (kd
->procbase
);
892 _kvm_realloc(kvm_t
*kd
, void *p
, size_t n
)
894 void *np
= realloc(p
, n
);
897 _kvm_err(kd
, kd
->program
, "out of memory");
902 * Read in an argument vector from the user address space of process p.
903 * addr if the user-space base address of narg null-terminated contiguous
904 * strings. This is used to read in both the command arguments and
905 * environment strings. Read at most maxcnt characters of strings.
908 kvm_argv(kvm_t
*kd
, const struct miniproc
*p
, u_long addr
, int narg
,
911 char *np
, *cp
, *ep
, *ap
;
912 u_long oaddr
= (u_long
)~0L;
918 * Check that there aren't an unreasonable number of arguments,
919 * and that the address is in user space.
921 if (narg
> ARG_MAX
|| addr
< kd
->min_uva
|| addr
>= kd
->max_uva
)
924 if (kd
->argv
== NULL
) {
926 * Try to avoid reallocs.
928 kd
->argc
= MAX(narg
+ 1, 32);
929 kd
->argv
= _kvm_malloc(kd
, kd
->argc
* sizeof(*kd
->argv
));
930 if (kd
->argv
== NULL
)
932 } else if (narg
+ 1 > kd
->argc
) {
933 kd
->argc
= MAX(2 * kd
->argc
, narg
+ 1);
934 kd
->argv
= _kvm_realloc(kd
, kd
->argv
, kd
->argc
*
936 if (kd
->argv
== NULL
)
939 if (kd
->argspc
== NULL
) {
940 kd
->argspc
= _kvm_malloc(kd
, (size_t)kd
->nbpg
);
941 if (kd
->argspc
== NULL
)
943 kd
->argspc_len
= kd
->nbpg
;
945 if (kd
->argbuf
== NULL
) {
946 kd
->argbuf
= _kvm_malloc(kd
, (size_t)kd
->nbpg
);
947 if (kd
->argbuf
== NULL
)
950 cc
= sizeof(char *) * narg
;
951 if (kvm_ureadm(kd
, p
, addr
, (void *)kd
->argv
, cc
) != cc
)
953 ap
= np
= kd
->argspc
;
957 * Loop over pages, filling in the argument vector.
959 while (argv
< kd
->argv
+ narg
&& *argv
!= NULL
) {
960 addr
= (u_long
)*argv
& ~(kd
->nbpg
- 1);
962 if (kvm_ureadm(kd
, p
, addr
, kd
->argbuf
,
963 (size_t)kd
->nbpg
) != kd
->nbpg
)
967 addr
= (u_long
)*argv
& (kd
->nbpg
- 1);
968 cp
= kd
->argbuf
+ (size_t)addr
;
969 cc
= kd
->nbpg
- (size_t)addr
;
970 if (maxcnt
> 0 && cc
> (size_t)(maxcnt
- len
))
971 cc
= (size_t)(maxcnt
- len
);
972 ep
= memchr(cp
, '\0', cc
);
975 if (len
+ cc
> kd
->argspc_len
) {
978 char *op
= kd
->argspc
;
981 kd
->argspc
= _kvm_realloc(kd
, kd
->argspc
,
983 if (kd
->argspc
== NULL
)
986 * Adjust argv pointers in case realloc moved
989 off
= kd
->argspc
- op
;
990 for (pp
= kd
->argv
; pp
< argv
; pp
++)
1003 if (maxcnt
> 0 && len
>= maxcnt
) {
1005 * We're stopping prematurely. Terminate the
1015 /* Make sure argv is terminated. */
1021 ps_str_a(struct ps_strings
*p
, u_long
*addr
, int *n
)
1024 *addr
= (u_long
)p
->ps_argvstr
;
1025 *n
= p
->ps_nargvstr
;
1029 ps_str_e(struct ps_strings
*p
, u_long
*addr
, int *n
)
1032 *addr
= (u_long
)p
->ps_envstr
;
1037 * Determine if the proc indicated by p is still active.
1038 * This test is not 100% foolproof in theory, but chances of
1039 * being wrong are very low.
1042 proc_verify(kvm_t
*kd
, u_long kernp
, const struct miniproc
*p
)
1044 struct proc kernproc
;
1047 * Just read in the whole proc. It's not that big relative
1048 * to the cost of the read system call.
1050 if (kvm_read(kd
, kernp
, &kernproc
, sizeof(kernproc
)) !=
1053 return (p
->p_pid
== kernproc
.p_pid
&&
1054 (kernproc
.p_stat
!= SZOMB
|| p
->p_stat
== SZOMB
));
1058 kvm_doargv(kvm_t
*kd
, const struct miniproc
*p
, int nchr
,
1059 void (*info
)(struct ps_strings
*, u_long
*, int *))
1064 struct ps_strings arginfo
;
1067 * Pointers are stored at the top of the user stack.
1069 if (p
->p_stat
== SZOMB
)
1071 cnt
= (int)kvm_ureadm(kd
, p
, kd
->usrstack
- sizeof(arginfo
),
1072 (void *)&arginfo
, sizeof(arginfo
));
1073 if (cnt
!= sizeof(arginfo
))
1076 (*info
)(&arginfo
, &addr
, &cnt
);
1079 ap
= kvm_argv(kd
, p
, addr
, cnt
, nchr
);
1081 * For live kernels, make sure this process didn't go away.
1083 if (ap
!= NULL
&& ISALIVE(kd
) &&
1084 !proc_verify(kd
, (u_long
)p
->p_paddr
, p
))
1090 * Get the command args. This code is now machine independent.
1093 kvm_getargv(kvm_t
*kd
, const struct kinfo_proc
*kp
, int nchr
)
1098 return (kvm_doargv(kd
, &p
, nchr
, ps_str_a
));
1102 kvm_getenvv(kvm_t
*kd
, const struct kinfo_proc
*kp
, int nchr
)
1107 return (kvm_doargv(kd
, &p
, nchr
, ps_str_e
));
1111 kvm_doargv2(kvm_t
*kd
, pid_t pid
, int type
, int nchr
)
1115 size_t newargspc_len
;
1116 char **ap
, *bp
, *endp
;
1119 * Check that there aren't an unreasonable number of arguments.
1127 /* Get number of strings in argv */
1129 mib
[1] = KERN_PROC_ARGS
;
1131 mib
[3] = type
== KERN_PROC_ARGV
? KERN_PROC_NARGV
: KERN_PROC_NENV
;
1132 bufs
= sizeof(narg
);
1133 if (sysctl(mib
, 4, &narg
, &bufs
, NULL
, (size_t)0) == -1)
1136 if (kd
->argv
== NULL
) {
1138 * Try to avoid reallocs.
1140 kd
->argc
= MAX(narg
+ 1, 32);
1141 kd
->argv
= _kvm_malloc(kd
, kd
->argc
* sizeof(*kd
->argv
));
1142 if (kd
->argv
== NULL
)
1144 } else if (narg
+ 1 > kd
->argc
) {
1145 kd
->argc
= MAX(2 * kd
->argc
, narg
+ 1);
1146 kd
->argv
= _kvm_realloc(kd
, kd
->argv
, kd
->argc
*
1148 if (kd
->argv
== NULL
)
1152 newargspc_len
= MIN(nchr
, ARG_MAX
);
1153 KVM_ALLOC(kd
, argspc
, newargspc_len
);
1154 memset(kd
->argspc
, 0, (size_t)kd
->argspc_len
); /* XXX necessary? */
1157 mib
[1] = KERN_PROC_ARGS
;
1160 bufs
= kd
->argspc_len
;
1161 if (sysctl(mib
, 4, kd
->argspc
, &bufs
, NULL
, (size_t)0) == -1)
1165 bp
[kd
->argspc_len
-1] = '\0'; /* make sure the string ends with nul */
1167 endp
= bp
+ MIN(nchr
, bufs
);
1172 * XXX: don't need following anymore, or stick check
1173 * for max argc in above while loop?
1175 if (ap
>= kd
->argv
+ kd
->argc
) {
1177 kd
->argv
= _kvm_realloc(kd
, kd
->argv
,
1178 kd
->argc
* sizeof(*kd
->argv
));
1181 bp
+= strlen(bp
) + 1;
1189 kvm_getargv2(kvm_t
*kd
, const struct kinfo_proc2
*kp
, int nchr
)
1192 return (kvm_doargv2(kd
, kp
->p_pid
, KERN_PROC_ARGV
, nchr
));
1196 kvm_getenvv2(kvm_t
*kd
, const struct kinfo_proc2
*kp
, int nchr
)
1199 return (kvm_doargv2(kd
, kp
->p_pid
, KERN_PROC_ENV
, nchr
));
1203 * Read from user space. The user context is given by p.
1206 kvm_ureadm(kvm_t
*kd
, const struct miniproc
*p
, u_long uva
,
1207 char *buf
, size_t len
)
1217 dp
= _kvm_ureadm(kd
, p
, uva
, &cnt
);
1219 _kvm_err(kd
, 0, "invalid address (%lx)", uva
);
1222 cc
= (size_t)MIN(cnt
, len
);
1228 return (ssize_t
)(cp
- buf
);
1232 kvm_uread(kvm_t
*kd
, const struct proc
*p
, u_long uva
, char *buf
, size_t len
)
1237 return (kvm_ureadm(kd
, &mp
, uva
, buf
, len
));