xref: /openbsd-src/lib/libkvm/kvm_proc.c (revision fd9b881ab0b4634eb50073b79bfe69dbb92c620c)
1 /*	$OpenBSD: kvm_proc.c,v 1.30 2006/07/13 22:51:24 deraadt Exp $	*/
2 /*	$NetBSD: kvm_proc.c,v 1.30 1999/03/24 05:50:50 mrg Exp $	*/
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 /*-
39  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
40  * Copyright (c) 1989, 1992, 1993
41  *	The Regents of the University of California.  All rights reserved.
42  *
43  * This code is derived from software developed by the Computer Systems
44  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
45  * BG 91-66 and contributed to Berkeley.
46  *
47  * Redistribution and use in source and binary forms, with or without
48  * modification, are permitted provided that the following conditions
49  * are met:
50  * 1. Redistributions of source code must retain the above copyright
51  *    notice, this list of conditions and the following disclaimer.
52  * 2. Redistributions in binary form must reproduce the above copyright
53  *    notice, this list of conditions and the following disclaimer in the
54  *    documentation and/or other materials provided with the distribution.
55  * 3. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  */
71 
72 #if defined(LIBC_SCCS) && !defined(lint)
73 #if 0
74 static char sccsid[] = "@(#)kvm_proc.c	8.3 (Berkeley) 9/23/93";
75 #else
76 static char *rcsid = "$OpenBSD: kvm_proc.c,v 1.30 2006/07/13 22:51:24 deraadt Exp $";
77 #endif
78 #endif /* LIBC_SCCS and not lint */
79 
80 /*
81  * Proc traversal interface for kvm.  ps and w are (probably) the exclusive
82  * users of this code, so we've factored it out into a separate module.
83  * Thus, we keep this grunge out of the other kvm applications (i.e.,
84  * most other applications are interested only in open/close/read/nlist).
85  */
86 
87 #include <sys/param.h>
88 #include <sys/user.h>
89 #include <sys/proc.h>
90 #include <sys/exec.h>
91 #include <sys/stat.h>
92 #include <sys/ioctl.h>
93 #include <sys/tty.h>
94 #include <stdlib.h>
95 #include <string.h>
96 #include <unistd.h>
97 #include <nlist.h>
98 #include <kvm.h>
99 
100 #include <uvm/uvm_extern.h>
101 #include <uvm/uvm_amap.h>
102 #include <machine/vmparam.h>
103 #include <machine/pmap.h>
104 
105 #include <sys/sysctl.h>
106 
107 #include <limits.h>
108 #include <db.h>
109 #include <paths.h>
110 
111 #include "kvm_private.h"
112 
113 /*
114  * Common info from kinfo_proc and kinfo_proc2 used by helper routines.
115  */
116 struct miniproc {
117 	struct	vmspace *p_vmspace;
118 	char	p_stat;
119 	struct	proc *p_paddr;
120 	pid_t	p_pid;
121 };
122 
123 /*
124  * Convert from struct proc and kinfo_proc{,2} to miniproc.
125  */
126 #define PTOMINI(kp, p) \
127 	do { \
128 		(p)->p_stat = (kp)->p_stat; \
129 		(p)->p_pid = (kp)->p_pid; \
130 		(p)->p_paddr = NULL; \
131 		(p)->p_vmspace = (kp)->p_vmspace; \
132 	} while (/*CONSTCOND*/0);
133 
134 #define KPTOMINI(kp, p) \
135 	do { \
136 		(p)->p_stat = (kp)->kp_proc.p_stat; \
137 		(p)->p_pid = (kp)->kp_proc.p_pid; \
138 		(p)->p_paddr = (kp)->kp_eproc.e_paddr; \
139 		(p)->p_vmspace = (kp)->kp_proc.p_vmspace; \
140 	} while (/*CONSTCOND*/0);
141 
142 #define KP2TOMINI(kp, p) \
143 	do { \
144 		(p)->p_stat = (kp)->p_stat; \
145 		(p)->p_pid = (kp)->p_pid; \
146 		(p)->p_paddr = (void *)(long)(kp)->p_paddr; \
147 		(p)->p_vmspace = (void *)(long)(kp)->p_vmspace; \
148 	} while (/*CONSTCOND*/0);
149 
150 
151 #define	PTRTOINT64(foo)	((u_int64_t)(u_long)(foo))
152 
153 #define KREAD(kd, addr, obj) \
154 	(kvm_read(kd, addr, (void *)(obj), sizeof(*obj)) != sizeof(*obj))
155 
156 ssize_t		kvm_uread(kvm_t *, const struct proc *, u_long, char *, size_t);
157 
158 static char	*_kvm_ureadm(kvm_t *, const struct miniproc *, u_long, u_long *);
159 static ssize_t	kvm_ureadm(kvm_t *, const struct miniproc *, u_long, char *, size_t);
160 
161 static char	**kvm_argv(kvm_t *, const struct miniproc *, u_long, int, int);
162 
163 static int	kvm_deadprocs(kvm_t *, int, int, u_long, u_long, int);
164 static char	**kvm_doargv(kvm_t *, const struct miniproc *, int,
165 		    void (*)(struct ps_strings *, u_long *, int *));
166 static int	kvm_proclist(kvm_t *, int, int, struct proc *,
167 		    struct kinfo_proc *, int);
168 static int	proc_verify(kvm_t *, const struct miniproc *);
169 static void	ps_str_a(struct ps_strings *, u_long *, int *);
170 static void	ps_str_e(struct ps_strings *, u_long *, int *);
171 
172 static char *
173 _kvm_ureadm(kvm_t *kd, const struct miniproc *p, u_long va, u_long *cnt)
174 {
175 	u_long addr, head, offset, slot;
176 	struct vm_anon *anonp, anon;
177 	struct vm_map_entry vme;
178 	struct vm_amap amap;
179 	struct vm_page pg;
180 
181 	if (kd->swapspc == 0) {
182 		kd->swapspc = _kvm_malloc(kd, kd->nbpg);
183 		if (kd->swapspc == 0)
184 			return (0);
185 	}
186 
187 	/*
188 	 * Look through the address map for the memory object
189 	 * that corresponds to the given virtual address.
190 	 * The header just has the entire valid range.
191 	 */
192 	head = (u_long)&p->p_vmspace->vm_map.header;
193 	addr = head;
194 	while (1) {
195 		if (KREAD(kd, addr, &vme))
196 			return (0);
197 
198 		if (va >= vme.start && va < vme.end &&
199 		    vme.aref.ar_amap != NULL)
200 			break;
201 
202 		addr = (u_long)vme.next;
203 		if (addr == head)
204 			return (0);
205 	}
206 
207 	/*
208 	 * we found the map entry, now to find the object...
209 	 */
210 	if (vme.aref.ar_amap == NULL)
211 		return (NULL);
212 
213 	addr = (u_long)vme.aref.ar_amap;
214 	if (KREAD(kd, addr, &amap))
215 		return (NULL);
216 
217 	offset = va - vme.start;
218 	slot = offset / kd->nbpg + vme.aref.ar_pageoff;
219 	/* sanity-check slot number */
220 	if (slot > amap.am_nslot)
221 		return (NULL);
222 
223 	addr = (u_long)amap.am_anon + (offset / kd->nbpg) * sizeof(anonp);
224 	if (KREAD(kd, addr, &anonp))
225 		return (NULL);
226 
227 	addr = (u_long)anonp;
228 	if (KREAD(kd, addr, &anon))
229 		return (NULL);
230 
231 	addr = (u_long)anon.u.an_page;
232 	if (addr) {
233 		if (KREAD(kd, addr, &pg))
234 			return (NULL);
235 
236 		if (_kvm_pread(kd, kd->pmfd, (void *)kd->swapspc,
237 		    (size_t)kd->nbpg, (off_t)pg.phys_addr) != kd->nbpg)
238 			return (NULL);
239 	} else {
240 		if (_kvm_pread(kd, kd->swfd, (void *)kd->swapspc,
241 		    (size_t)kd->nbpg,
242 		    (off_t)(anon.an_swslot * kd->nbpg)) != kd->nbpg)
243 			return (NULL);
244 	}
245 
246 	/* Found the page. */
247 	offset %= kd->nbpg;
248 	*cnt = kd->nbpg - offset;
249 	return (&kd->swapspc[offset]);
250 }
251 
252 char *
253 _kvm_uread(kvm_t *kd, const struct proc *p, u_long va, u_long *cnt)
254 {
255 	struct miniproc mp;
256 
257 	PTOMINI(p, &mp);
258 	return (_kvm_ureadm(kd, &mp, va, cnt));
259 }
260 
261 /*
262  * Read proc's from memory file into buffer bp, which has space to hold
263  * at most maxcnt procs.
264  */
265 static int
266 kvm_proclist(kvm_t *kd, int what, int arg, struct proc *p,
267     struct kinfo_proc *bp, int maxcnt)
268 {
269 	struct session sess;
270 	struct eproc eproc;
271 	struct proc proc;
272 	struct pgrp pgrp;
273 	struct tty tty;
274 	int cnt = 0;
275 
276 	for (; cnt < maxcnt && p != NULL; p = LIST_NEXT(&proc, p_list)) {
277 		if (KREAD(kd, (u_long)p, &proc)) {
278 			_kvm_err(kd, kd->program, "can't read proc at %x", p);
279 			return (-1);
280 		}
281 		if (KREAD(kd, (u_long)proc.p_cred, &eproc.e_pcred) == 0)
282 			KREAD(kd, (u_long)eproc.e_pcred.pc_ucred,
283 			    &eproc.e_ucred);
284 
285 		switch (what) {
286 		case KERN_PROC_PID:
287 			if (proc.p_pid != (pid_t)arg)
288 				continue;
289 			break;
290 
291 		case KERN_PROC_UID:
292 			if (eproc.e_ucred.cr_uid != (uid_t)arg)
293 				continue;
294 			break;
295 
296 		case KERN_PROC_RUID:
297 			if (eproc.e_pcred.p_ruid != (uid_t)arg)
298 				continue;
299 			break;
300 
301 		case KERN_PROC_ALL:
302 			if (proc.p_flag & P_SYSTEM)
303 				continue;
304 			break;
305 		}
306 		/*
307 		 * We're going to add another proc to the set.  If this
308 		 * will overflow the buffer, assume the reason is because
309 		 * nprocs (or the proc list) is corrupt and declare an error.
310 		 */
311 		if (cnt >= maxcnt) {
312 			_kvm_err(kd, kd->program, "nprocs corrupt");
313 			return (-1);
314 		}
315 		/*
316 		 * gather eproc
317 		 */
318 		eproc.e_paddr = p;
319 		if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
320 			_kvm_err(kd, kd->program, "can't read pgrp at %x",
321 			    proc.p_pgrp);
322 			return (-1);
323 		}
324 		eproc.e_sess = pgrp.pg_session;
325 		eproc.e_pgid = pgrp.pg_id;
326 		eproc.e_jobc = pgrp.pg_jobc;
327 		if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
328 			_kvm_err(kd, kd->program, "can't read session at %x",
329 			    pgrp.pg_session);
330 			return (-1);
331 		}
332 		if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) {
333 			if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
334 				_kvm_err(kd, kd->program,
335 				    "can't read tty at %x", sess.s_ttyp);
336 				return (-1);
337 			}
338 			eproc.e_tdev = tty.t_dev;
339 			eproc.e_tsess = tty.t_session;
340 			if (tty.t_pgrp != NULL) {
341 				if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
342 					_kvm_err(kd, kd->program,
343 					    "can't read tpgrp at &x",
344 					    tty.t_pgrp);
345 					return (-1);
346 				}
347 				eproc.e_tpgid = pgrp.pg_id;
348 			} else
349 				eproc.e_tpgid = -1;
350 		} else
351 			eproc.e_tdev = NODEV;
352 		eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0;
353 		if (sess.s_leader == p)
354 			eproc.e_flag |= EPROC_SLEADER;
355 		if (proc.p_wmesg)
356 			(void)kvm_read(kd, (u_long)proc.p_wmesg,
357 			    eproc.e_wmesg, WMESGLEN);
358 
359 		(void)kvm_read(kd, (u_long)proc.p_vmspace,
360 		    &eproc.e_vm, sizeof(eproc.e_vm));
361 
362 		eproc.e_xsize = eproc.e_xrssize = 0;
363 		eproc.e_xccount = eproc.e_xswrss = 0;
364 
365 		switch (what) {
366 		case KERN_PROC_PGRP:
367 			if (eproc.e_pgid != (pid_t)arg)
368 				continue;
369 			break;
370 
371 		case KERN_PROC_TTY:
372 			if ((proc.p_flag & P_CONTROLT) == 0 ||
373 			    eproc.e_tdev != (dev_t)arg)
374 				continue;
375 			break;
376 		}
377 		bcopy(&proc, &bp->kp_proc, sizeof(proc));
378 		bcopy(&eproc, &bp->kp_eproc, sizeof(eproc));
379 		++bp;
380 		++cnt;
381 	}
382 	return (cnt);
383 }
384 
385 /*
386  * Build proc info array by reading in proc list from a crash dump.
387  * Return number of procs read.  maxcnt is the max we will read.
388  */
389 static int
390 kvm_deadprocs(kvm_t *kd, int what, int arg, u_long a_allproc,
391     u_long a_zombproc, int maxcnt)
392 {
393 	struct kinfo_proc *bp = kd->procbase;
394 	struct proc *p;
395 	int acnt, zcnt;
396 
397 	if (KREAD(kd, a_allproc, &p)) {
398 		_kvm_err(kd, kd->program, "cannot read allproc");
399 		return (-1);
400 	}
401 	acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
402 	if (acnt < 0)
403 		return (acnt);
404 
405 	if (KREAD(kd, a_zombproc, &p)) {
406 		_kvm_err(kd, kd->program, "cannot read zombproc");
407 		return (-1);
408 	}
409 	zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt);
410 	if (zcnt < 0)
411 		zcnt = 0;
412 
413 	return (acnt + zcnt);
414 }
415 
416 struct kinfo_proc2 *
417 kvm_getproc2(kvm_t *kd, int op, int arg, size_t esize, int *cnt)
418 {
419 	int mib[6], st, nprocs;
420 	struct user user;
421 	size_t size;
422 
423 	if ((ssize_t)esize < 0)
424 		return (NULL);
425 
426 	if (kd->procbase2 != NULL) {
427 		free(kd->procbase2);
428 		/*
429 		 * Clear this pointer in case this call fails.  Otherwise,
430 		 * kvm_close() will free it again.
431 		 */
432 		kd->procbase2 = 0;
433 	}
434 
435 	if (ISALIVE(kd)) {
436 		size = 0;
437 		mib[0] = CTL_KERN;
438 		mib[1] = KERN_PROC2;
439 		mib[2] = op;
440 		mib[3] = arg;
441 		mib[4] = esize;
442 		mib[5] = 0;
443 		st = sysctl(mib, 6, NULL, &size, NULL, 0);
444 		if (st == -1) {
445 			_kvm_syserr(kd, kd->program, "kvm_getproc2");
446 			return (NULL);
447 		}
448 
449 		mib[5] = size / esize;
450 		kd->procbase2 = _kvm_malloc(kd, size);
451 		if (kd->procbase2 == 0)
452 			return (NULL);
453 		st = sysctl(mib, 6, kd->procbase2, &size, NULL, 0);
454 		if (st == -1) {
455 			_kvm_syserr(kd, kd->program, "kvm_getproc2");
456 			return (NULL);
457 		}
458 		nprocs = size / esize;
459 	} else {
460 		struct kinfo_proc2 kp2, *kp2p;
461 		struct kinfo_proc *kp;
462 		char *kp2c;
463 		int i;
464 
465 		kp = kvm_getprocs(kd, op, arg, &nprocs);
466 		if (kp == NULL)
467 			return (NULL);
468 
469 		kd->procbase2 = _kvm_malloc(kd, nprocs * esize);
470 		kp2c = (char *)kd->procbase2;
471 		kp2p = &kp2;
472 		for (i = 0; i < nprocs; i++, kp++) {
473 			memset(kp2p, 0, sizeof(kp2));
474 			kp2p->p_forw = PTRTOINT64(kp->kp_proc.p_forw);
475 			kp2p->p_back = PTRTOINT64(kp->kp_proc.p_back);
476 			kp2p->p_paddr = PTRTOINT64(kp->kp_eproc.e_paddr);
477 
478 			kp2p->p_addr = PTRTOINT64(kp->kp_proc.p_addr);
479 			kp2p->p_fd = PTRTOINT64(kp->kp_proc.p_fd);
480 			kp2p->p_stats = PTRTOINT64(kp->kp_proc.p_stats);
481 			kp2p->p_limit = PTRTOINT64(kp->kp_proc.p_limit);
482 			kp2p->p_vmspace = PTRTOINT64(kp->kp_proc.p_vmspace);
483 			kp2p->p_sigacts = PTRTOINT64(kp->kp_proc.p_sigacts);
484 			kp2p->p_sess = PTRTOINT64(kp->kp_eproc.e_sess);
485 			kp2p->p_tsess = 0;
486 			kp2p->p_ru = PTRTOINT64(kp->kp_proc.p_ru);
487 
488 			kp2p->p_eflag = 0;
489 			kp2p->p_exitsig = kp->kp_proc.p_exitsig;
490 			kp2p->p_flag = kp->kp_proc.p_flag;
491 
492 			kp2p->p_pid = kp->kp_proc.p_pid;
493 
494 			kp2p->p_ppid = kp->kp_eproc.e_ppid;
495 #if 0
496 			kp2p->p_sid = kp->kp_eproc.e_sid;
497 #else
498 			kp2p->p_sid = -1; /* XXX */
499 #endif
500 			kp2p->p__pgid = kp->kp_eproc.e_pgid;
501 
502 			kp2p->p_tpgid = -1;
503 
504 			kp2p->p_uid = kp->kp_eproc.e_ucred.cr_uid;
505 			kp2p->p_ruid = kp->kp_eproc.e_pcred.p_ruid;
506 			kp2p->p_gid = kp->kp_eproc.e_ucred.cr_gid;
507 			kp2p->p_rgid = kp->kp_eproc.e_pcred.p_rgid;
508 
509 			memcpy(kp2p->p_groups, kp->kp_eproc.e_ucred.cr_groups,
510 			    MIN(sizeof(kp2p->p_groups),
511 			    sizeof(kp->kp_eproc.e_ucred.cr_groups)));
512 			kp2p->p_ngroups = kp->kp_eproc.e_ucred.cr_ngroups;
513 
514 			kp2p->p_jobc = kp->kp_eproc.e_jobc;
515 			kp2p->p_tdev = kp->kp_eproc.e_tdev;
516 			kp2p->p_tpgid = kp->kp_eproc.e_tpgid;
517 			kp2p->p_tsess = PTRTOINT64(kp->kp_eproc.e_tsess);
518 
519 			kp2p->p_estcpu = kp->kp_proc.p_estcpu;
520 			kp2p->p_rtime_sec = kp->kp_proc.p_estcpu;
521 			kp2p->p_rtime_usec = kp->kp_proc.p_estcpu;
522 			kp2p->p_cpticks = kp->kp_proc.p_cpticks;
523 			kp2p->p_pctcpu = kp->kp_proc.p_pctcpu;
524 			kp2p->p_swtime = kp->kp_proc.p_swtime;
525 			kp2p->p_slptime = kp->kp_proc.p_slptime;
526 			kp2p->p_schedflags = 0;
527 
528 			kp2p->p_uticks = kp->kp_proc.p_uticks;
529 			kp2p->p_sticks = kp->kp_proc.p_sticks;
530 			kp2p->p_iticks = kp->kp_proc.p_iticks;
531 
532 			kp2p->p_tracep = PTRTOINT64(kp->kp_proc.p_tracep);
533 			kp2p->p_traceflag = kp->kp_proc.p_traceflag;
534 
535 			kp2p->p_holdcnt = kp->kp_proc.p_holdcnt;
536 
537 			kp2p->p_siglist = kp->kp_proc.p_siglist;
538 			kp2p->p_sigmask = kp->kp_proc.p_sigmask;
539 			kp2p->p_sigignore = kp->kp_proc.p_sigignore;
540 			kp2p->p_sigcatch = kp->kp_proc.p_sigcatch;
541 
542 			kp2p->p_stat = kp->kp_proc.p_stat;
543 			kp2p->p_priority = kp->kp_proc.p_priority;
544 			kp2p->p_usrpri = kp->kp_proc.p_usrpri;
545 			kp2p->p_nice = kp->kp_proc.p_nice;
546 
547 			kp2p->p_xstat = kp->kp_proc.p_xstat;
548 			kp2p->p_acflag = kp->kp_proc.p_acflag;
549 
550 			strncpy(kp2p->p_comm, kp->kp_proc.p_comm,
551 			    MIN(sizeof(kp2p->p_comm), sizeof(kp->kp_proc.p_comm)));
552 
553 			strncpy(kp2p->p_wmesg, kp->kp_eproc.e_wmesg,
554 			    sizeof(kp2p->p_wmesg));
555 			kp2p->p_wchan = PTRTOINT64(kp->kp_proc.p_wchan);
556 
557 			strncpy(kp2p->p_login, kp->kp_eproc.e_login,
558 			    sizeof(kp2p->p_login));
559 
560 			kp2p->p_vm_rssize = kp->kp_eproc.e_xrssize;
561 			kp2p->p_vm_tsize = kp->kp_eproc.e_vm.vm_tsize;
562 			kp2p->p_vm_dsize = kp->kp_eproc.e_vm.vm_dsize;
563 			kp2p->p_vm_ssize = kp->kp_eproc.e_vm.vm_ssize;
564 
565 			kp2p->p_eflag = kp->kp_eproc.e_flag;
566 
567 			if (P_ZOMBIE(&kp->kp_proc) || kp->kp_proc.p_addr == NULL ||
568 			    KREAD(kd, (u_long)kp->kp_proc.p_addr, &user)) {
569 				kp2p->p_uvalid = 0;
570 			} else {
571 				kp2p->p_uvalid = 1;
572 
573 				kp2p->p_ustart_sec = user.u_stats.p_start.tv_sec;
574 				kp2p->p_ustart_usec = user.u_stats.p_start.tv_usec;
575 
576 				kp2p->p_uutime_sec = user.u_stats.p_ru.ru_utime.tv_sec;
577 				kp2p->p_uutime_usec = user.u_stats.p_ru.ru_utime.tv_usec;
578 				kp2p->p_ustime_sec = user.u_stats.p_ru.ru_stime.tv_sec;
579 				kp2p->p_ustime_usec = user.u_stats.p_ru.ru_stime.tv_usec;
580 
581 				kp2p->p_uru_maxrss = user.u_stats.p_ru.ru_maxrss;
582 				kp2p->p_uru_ixrss = user.u_stats.p_ru.ru_ixrss;
583 				kp2p->p_uru_idrss = user.u_stats.p_ru.ru_idrss;
584 				kp2p->p_uru_isrss = user.u_stats.p_ru.ru_isrss;
585 				kp2p->p_uru_minflt = user.u_stats.p_ru.ru_minflt;
586 				kp2p->p_uru_majflt = user.u_stats.p_ru.ru_majflt;
587 				kp2p->p_uru_nswap = user.u_stats.p_ru.ru_nswap;
588 				kp2p->p_uru_inblock = user.u_stats.p_ru.ru_inblock;
589 				kp2p->p_uru_oublock = user.u_stats.p_ru.ru_oublock;
590 				kp2p->p_uru_msgsnd = user.u_stats.p_ru.ru_msgsnd;
591 				kp2p->p_uru_msgrcv = user.u_stats.p_ru.ru_msgrcv;
592 				kp2p->p_uru_nsignals = user.u_stats.p_ru.ru_nsignals;
593 				kp2p->p_uru_nvcsw = user.u_stats.p_ru.ru_nvcsw;
594 				kp2p->p_uru_nivcsw = user.u_stats.p_ru.ru_nivcsw;
595 
596 				kp2p->p_uctime_sec =
597 				    user.u_stats.p_cru.ru_utime.tv_sec +
598 				    user.u_stats.p_cru.ru_stime.tv_sec;
599 				kp2p->p_uctime_usec =
600 				    user.u_stats.p_cru.ru_utime.tv_usec +
601 				    user.u_stats.p_cru.ru_stime.tv_usec;
602 			}
603 
604 			memcpy(kp2c, &kp2, esize);
605 			kp2c += esize;
606 		}
607 
608 		free(kd->procbase);
609 	}
610 	*cnt = nprocs;
611 	return (kd->procbase2);
612 }
613 
614 struct kinfo_proc *
615 kvm_getprocs(kvm_t *kd, int op, int arg, int *cnt)
616 {
617 	int mib[4], st, nprocs;
618 	size_t size;
619 
620 	if (kd->procbase != 0) {
621 		free((void *)kd->procbase);
622 		/*
623 		 * Clear this pointer in case this call fails.  Otherwise,
624 		 * kvm_close() will free it again.
625 		 */
626 		kd->procbase = 0;
627 	}
628 	if (ISALIVE(kd)) {
629 		size = 0;
630 		mib[0] = CTL_KERN;
631 		mib[1] = KERN_PROC;
632 		mib[2] = op;
633 		mib[3] = arg;
634 		st = sysctl(mib, 4, NULL, &size, NULL, 0);
635 		if (st == -1) {
636 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
637 			return (0);
638 		}
639 		kd->procbase = _kvm_malloc(kd, size);
640 		if (kd->procbase == 0)
641 			return (0);
642 		st = sysctl(mib, 4, kd->procbase, &size, NULL, 0);
643 		if (st == -1) {
644 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
645 			return (0);
646 		}
647 		if (size % sizeof(struct kinfo_proc) != 0) {
648 			_kvm_err(kd, kd->program,
649 			    "proc size mismatch (%d total, %d chunks)",
650 			    size, sizeof(struct kinfo_proc));
651 			return (0);
652 		}
653 		nprocs = size / sizeof(struct kinfo_proc);
654 	} else {
655 		struct nlist nl[4], *p;
656 
657 		memset(nl, 0, sizeof(nl));
658 		nl[0].n_name = "_nprocs";
659 		nl[1].n_name = "_allproc";
660 		nl[2].n_name = "_zombproc";
661 		nl[3].n_name = NULL;
662 
663 		if (kvm_nlist(kd, nl) != 0) {
664 			for (p = nl; p->n_type != 0; ++p)
665 				;
666 			_kvm_err(kd, kd->program,
667 			    "%s: no such symbol", p->n_name);
668 			return (0);
669 		}
670 		if (KREAD(kd, nl[0].n_value, &nprocs)) {
671 			_kvm_err(kd, kd->program, "can't read nprocs");
672 			return (0);
673 		}
674 		size = nprocs * sizeof(struct kinfo_proc);
675 		kd->procbase = _kvm_malloc(kd, size);
676 		if (kd->procbase == 0)
677 			return (0);
678 
679 		nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
680 		    nl[2].n_value, nprocs);
681 #ifdef notdef
682 		size = nprocs * sizeof(struct kinfo_proc);
683 		(void)realloc(kd->procbase, size);
684 #endif
685 	}
686 	*cnt = nprocs;
687 	return (kd->procbase);
688 }
689 
690 void
691 _kvm_freeprocs(kvm_t *kd)
692 {
693 	if (kd->procbase) {
694 		free(kd->procbase);
695 		kd->procbase = 0;
696 	}
697 }
698 
699 void *
700 _kvm_realloc(kvm_t *kd, void *p, size_t n)
701 {
702 	void *np = (void *)realloc(p, n);
703 
704 	if (np == 0)
705 		_kvm_err(kd, kd->program, "out of memory");
706 	return (np);
707 }
708 
709 /*
710  * Read in an argument vector from the user address space of process p.
711  * addr if the user-space base address of narg null-terminated contiguous
712  * strings.  This is used to read in both the command arguments and
713  * environment strings.  Read at most maxcnt characters of strings.
714  */
715 static char **
716 kvm_argv(kvm_t *kd, const struct miniproc *p, u_long addr, int narg,
717     int maxcnt)
718 {
719 	char *np, *cp, *ep, *ap, **argv;
720 	u_long oaddr = -1;
721 	int len, cc;
722 
723 	/*
724 	 * Check that there aren't an unreasonable number of agruments,
725 	 * and that the address is in user space.
726 	 */
727 	if (narg > ARG_MAX || addr < VM_MIN_ADDRESS || addr >= VM_MAXUSER_ADDRESS)
728 		return (0);
729 
730 	if (kd->argv == 0) {
731 		/*
732 		 * Try to avoid reallocs.
733 		 */
734 		kd->argc = MAX(narg + 1, 32);
735 		kd->argv = _kvm_malloc(kd, kd->argc *
736 		    sizeof(*kd->argv));
737 		if (kd->argv == 0)
738 			return (0);
739 	} else if (narg + 1 > kd->argc) {
740 		kd->argc = MAX(2 * kd->argc, narg + 1);
741 		kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc *
742 		    sizeof(*kd->argv));
743 		if (kd->argv == 0)
744 			return (0);
745 	}
746 	if (kd->argspc == 0) {
747 		kd->argspc = _kvm_malloc(kd, kd->nbpg);
748 		if (kd->argspc == 0)
749 			return (0);
750 		kd->arglen = kd->nbpg;
751 	}
752 	if (kd->argbuf == 0) {
753 		kd->argbuf = _kvm_malloc(kd, kd->nbpg);
754 		if (kd->argbuf == 0)
755 			return (0);
756 	}
757 	cc = sizeof(char *) * narg;
758 	if (kvm_ureadm(kd, p, addr, (char *)kd->argv, cc) != cc)
759 		return (0);
760 	ap = np = kd->argspc;
761 	argv = kd->argv;
762 	len = 0;
763 
764 	/*
765 	 * Loop over pages, filling in the argument vector.
766 	 */
767 	while (argv < kd->argv + narg && *argv != 0) {
768 		addr = (u_long)*argv & ~(kd->nbpg - 1);
769 		if (addr != oaddr) {
770 			if (kvm_ureadm(kd, p, addr, kd->argbuf, kd->nbpg) !=
771 			    kd->nbpg)
772 				return (0);
773 			oaddr = addr;
774 		}
775 		addr = (u_long)*argv & (kd->nbpg - 1);
776 		cp = kd->argbuf + addr;
777 		cc = kd->nbpg - addr;
778 		if (maxcnt > 0 && cc > maxcnt - len)
779 			cc = maxcnt - len;
780 		ep = memchr(cp, '\0', cc);
781 		if (ep != 0)
782 			cc = ep - cp + 1;
783 		if (len + cc > kd->arglen) {
784 			int off;
785 			char **pp;
786 			char *op = kd->argspc;
787 
788 			kd->arglen *= 2;
789 			kd->argspc = (char *)_kvm_realloc(kd, kd->argspc,
790 			    kd->arglen);
791 			if (kd->argspc == 0)
792 				return (0);
793 			/*
794 			 * Adjust argv pointers in case realloc moved
795 			 * the string space.
796 			 */
797 			off = kd->argspc - op;
798 			for (pp = kd->argv; pp < argv; pp++)
799 				*pp += off;
800 			ap += off;
801 			np += off;
802 		}
803 		memcpy(np, cp, cc);
804 		np += cc;
805 		len += cc;
806 		if (ep != 0) {
807 			*argv++ = ap;
808 			ap = np;
809 		} else
810 			*argv += cc;
811 		if (maxcnt > 0 && len >= maxcnt) {
812 			/*
813 			 * We're stopping prematurely.  Terminate the
814 			 * current string.
815 			 */
816 			if (ep == 0) {
817 				*np = '\0';
818 				*argv++ = ap;
819 			}
820 			break;
821 		}
822 	}
823 	/* Make sure argv is terminated. */
824 	*argv = 0;
825 	return (kd->argv);
826 }
827 
828 static void
829 ps_str_a(struct ps_strings *p, u_long *addr, int *n)
830 {
831 	*addr = (u_long)p->ps_argvstr;
832 	*n = p->ps_nargvstr;
833 }
834 
835 static void
836 ps_str_e(struct ps_strings *p, u_long *addr, int *n)
837 {
838 	*addr = (u_long)p->ps_envstr;
839 	*n = p->ps_nenvstr;
840 }
841 
842 /*
843  * Determine if the proc indicated by p is still active.
844  * This test is not 100% foolproof in theory, but chances of
845  * being wrong are very low.
846  */
847 static int
848 proc_verify(kvm_t *kd, const struct miniproc *p)
849 {
850 	struct proc kernproc;
851 
852 	/*
853 	 * Just read in the whole proc.  It's not that big relative
854 	 * to the cost of the read system call.
855 	 */
856 	if (kvm_read(kd, (u_long)p->p_paddr, &kernproc, sizeof(kernproc)) !=
857 	    sizeof(kernproc))
858 		return (0);
859 	return (p->p_pid == kernproc.p_pid &&
860 	    (kernproc.p_stat != SZOMB || p->p_stat == SZOMB));
861 }
862 
863 static char **
864 kvm_doargv(kvm_t *kd, const struct miniproc *p, int nchr,
865     void (*info)(struct ps_strings *, u_long *, int *))
866 {
867 	static struct ps_strings *ps;
868 	struct ps_strings arginfo;
869 	u_long addr;
870 	char **ap;
871 	int cnt;
872 
873 	if (ps == NULL) {
874 		struct _ps_strings _ps;
875 		int mib[2];
876 		size_t len;
877 
878 		mib[0] = CTL_VM;
879 		mib[1] = VM_PSSTRINGS;
880 		len = sizeof(_ps);
881 		sysctl(mib, 2, &_ps, &len, NULL, 0);
882 		ps = (struct ps_strings *)_ps.val;
883 	}
884 
885 	/*
886 	 * Pointers are stored at the top of the user stack.
887 	 */
888 	if (p->p_stat == SZOMB ||
889 	    kvm_ureadm(kd, p, (u_long)ps, (char *)&arginfo,
890 	    sizeof(arginfo)) != sizeof(arginfo))
891 		return (0);
892 
893 	(*info)(&arginfo, &addr, &cnt);
894 	if (cnt == 0)
895 		return (0);
896 	ap = kvm_argv(kd, p, addr, cnt, nchr);
897 	/*
898 	 * For live kernels, make sure this process didn't go away.
899 	 */
900 	if (ap != 0 && ISALIVE(kd) && !proc_verify(kd, p))
901 		ap = 0;
902 	return (ap);
903 }
904 
905 static char **
906 kvm_arg_sysctl(kvm_t *kd, pid_t pid, int nchr, int env)
907 {
908 	size_t len, orglen;
909 	int mib[4], ret;
910 	char *buf;
911 
912 	orglen = env ? kd->nbpg : 8 * kd->nbpg;	/* XXX - should be ARG_MAX */
913 	if (kd->argbuf == NULL &&
914 	    (kd->argbuf = _kvm_malloc(kd, orglen)) == NULL)
915 		return (NULL);
916 
917 again:
918 	mib[0] = CTL_KERN;
919 	mib[1] = KERN_PROC_ARGS;
920 	mib[2] = (int)pid;
921 	mib[3] = env ? KERN_PROC_ENV : KERN_PROC_ARGV;
922 
923 	len = orglen;
924 	ret = (sysctl(mib, 4, kd->argbuf, &len, NULL, 0) < 0);
925 	if (ret && errno == ENOMEM) {
926 		orglen *= 2;
927 		buf = _kvm_realloc(kd, kd->argbuf, orglen);
928 		if (buf == NULL)
929 			return (NULL);
930 		kd->argbuf = buf;
931 		goto again;
932 	}
933 
934 	if (ret) {
935 		free(kd->argbuf);
936 		kd->argbuf = NULL;
937 		_kvm_syserr(kd, kd->program, "kvm_arg_sysctl");
938 		return (NULL);
939 	}
940 #if 0
941 	for (argv = (char **)kd->argbuf; *argv != NULL; argv++)
942 		if (strlen(*argv) > nchr)
943 			*argv[nchr] = '\0';
944 #endif
945 
946 	return (char **)(kd->argbuf);
947 }
948 
949 /*
950  * Get the command args.  This code is now machine independent.
951  */
952 char **
953 kvm_getargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr)
954 {
955 	struct miniproc p;
956 
957 	if (ISALIVE(kd))
958 		return (kvm_arg_sysctl(kd, kp->kp_proc.p_pid, nchr, 0));
959 	KPTOMINI(kp, &p);
960 	return (kvm_doargv(kd, &p, nchr, ps_str_a));
961 }
962 
963 char **
964 kvm_getenvv(kvm_t *kd, const struct kinfo_proc *kp, int nchr)
965 {
966 	struct miniproc p;
967 
968 	if (ISALIVE(kd))
969 		return (kvm_arg_sysctl(kd, kp->kp_proc.p_pid, nchr, 1));
970 	KPTOMINI(kp, &p);
971 	return (kvm_doargv(kd, &p, nchr, ps_str_e));
972 }
973 
974 char **
975 kvm_getargv2(kvm_t *kd, const struct kinfo_proc2 *kp, int nchr)
976 {
977 	struct miniproc p;
978 
979 	if (ISALIVE(kd))
980 		return (kvm_arg_sysctl(kd, kp->p_pid, nchr, 0));
981 	KP2TOMINI(kp, &p);
982 	return (kvm_doargv(kd, &p, nchr, ps_str_a));
983 }
984 
985 char **
986 kvm_getenvv2(kvm_t *kd, const struct kinfo_proc2 *kp, int nchr)
987 {
988 	struct miniproc p;
989 
990 	if (ISALIVE(kd))
991 		return (kvm_arg_sysctl(kd, kp->p_pid, nchr, 1));
992 	KP2TOMINI(kp, &p);
993 	return (kvm_doargv(kd, &p, nchr, ps_str_e));
994 }
995 
996 /*
997  * Read from user space.  The user context is given by p.
998  */
999 static ssize_t
1000 kvm_ureadm(kvm_t *kd, const struct miniproc *p, u_long uva, char *buf,
1001     size_t len)
1002 {
1003 	char *cp = buf;
1004 
1005 	while (len > 0) {
1006 		u_long cnt;
1007 		size_t cc;
1008 		char *dp;
1009 
1010 		dp = _kvm_ureadm(kd, p, uva, &cnt);
1011 		if (dp == 0) {
1012 			_kvm_err(kd, 0, "invalid address (%lx)", uva);
1013 			return (0);
1014 		}
1015 		cc = (size_t)MIN(cnt, len);
1016 		bcopy(dp, cp, cc);
1017 		cp += cc;
1018 		uva += cc;
1019 		len -= cc;
1020 	}
1021 	return (ssize_t)(cp - buf);
1022 }
1023 
1024 ssize_t
1025 kvm_uread(kvm_t *kd, const struct proc *p, u_long uva, char *buf,
1026     size_t len)
1027 {
1028 	struct miniproc mp;
1029 
1030 	PTOMINI(p, &mp);
1031 	return (kvm_ureadm(kd, &mp, uva, buf, len));
1032 }
1033