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