xref: /openbsd-src/lib/libkvm/kvm_proc.c (revision 3a3fbb3f2e2521ab7c4a56b7ff7462ebd9095ec5)
1 /*	$OpenBSD: kvm_proc.c,v 1.12 2001/12/13 08:16:00 hugh 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. 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 #if defined(LIBC_SCCS) && !defined(lint)
77 #if 0
78 static char sccsid[] = "@(#)kvm_proc.c	8.3 (Berkeley) 9/23/93";
79 #else
80 static char *rcsid = "$OpenBSD: kvm_proc.c,v 1.12 2001/12/13 08:16:00 hugh Exp $";
81 #endif
82 #endif /* LIBC_SCCS and not lint */
83 
84 /*
85  * Proc traversal interface for kvm.  ps and w are (probably) the exclusive
86  * users of this code, so we've factored it out into a separate module.
87  * Thus, we keep this grunge out of the other kvm applications (i.e.,
88  * most other applications are interested only in open/close/read/nlist).
89  */
90 
91 #include <sys/param.h>
92 #include <sys/user.h>
93 #include <sys/proc.h>
94 #include <sys/exec.h>
95 #include <sys/stat.h>
96 #include <sys/ioctl.h>
97 #include <sys/tty.h>
98 #include <stdlib.h>
99 #include <string.h>
100 #include <unistd.h>
101 #include <nlist.h>
102 #include <kvm.h>
103 
104 #include <uvm/uvm_extern.h>
105 #include <uvm/uvm_amap.h>
106 #include <machine/vmparam.h>
107 #include <machine/pmap.h>
108 
109 #include <sys/sysctl.h>
110 
111 #include <limits.h>
112 #include <db.h>
113 #include <paths.h>
114 
115 #include "kvm_private.h"
116 
117 #define KREAD(kd, addr, obj) \
118 	(kvm_read(kd, addr, (void *)(obj), sizeof(*obj)) != sizeof(*obj))
119 
120 ssize_t		kvm_uread __P((kvm_t *, const struct proc *, u_long, char *,
121 		    size_t));
122 
123 static char	**kvm_argv __P((kvm_t *, const struct proc *, u_long, int,
124 		    int));
125 static int	kvm_deadprocs __P((kvm_t *, int, int, u_long, u_long, int));
126 static char	**kvm_doargv __P((kvm_t *, const struct kinfo_proc *, int,
127 		    void (*)(struct ps_strings *, u_long *, int *)));
128 static int	kvm_proclist __P((kvm_t *, int, int, struct proc *,
129 		    struct kinfo_proc *, int));
130 static int	proc_verify __P((kvm_t *, u_long, const struct proc *));
131 static void	ps_str_a __P((struct ps_strings *, u_long *, int *));
132 static void	ps_str_e __P((struct ps_strings *, u_long *, int *));
133 
134 char *
135 _kvm_uread(kd, p, va, cnt)
136 	kvm_t *kd;
137 	const struct proc *p;
138 	u_long va;
139 	u_long *cnt;
140 {
141 	u_long addr, head;
142 	u_long offset;
143 	struct vm_map_entry vme;
144 	struct vm_amap amap;
145 	struct vm_anon *anonp, anon;
146 	struct vm_page pg;
147 	u_long slot;
148 
149 	if (kd->swapspc == 0) {
150 		kd->swapspc = (char *)_kvm_malloc(kd, kd->nbpg);
151 		if (kd->swapspc == 0)
152 			return (0);
153 	}
154 
155 	/*
156 	 * Look through the address map for the memory object
157 	 * that corresponds to the given virtual address.
158 	 * The header just has the entire valid range.
159 	 */
160 	head = (u_long)&p->p_vmspace->vm_map.header;
161 	addr = head;
162 	while (1) {
163 		if (KREAD(kd, addr, &vme))
164 			return (0);
165 
166 		if (va >= vme.start && va < vme.end &&
167 		    vme.aref.ar_amap != NULL)
168 			break;
169 
170 		addr = (u_long)vme.next;
171 		if (addr == head)
172 			return (0);
173 	}
174 
175 	/*
176 	 * we found the map entry, now to find the object...
177 	 */
178 	if (vme.aref.ar_amap == NULL)
179 		return NULL;
180 
181 	addr = (u_long)vme.aref.ar_amap;
182 	if (KREAD(kd, addr, &amap))
183 		return NULL;
184 
185 	offset = va - vme.start;
186 	slot = offset / kd->nbpg + vme.aref.ar_pageoff;
187 	/* sanity-check slot number */
188 	if (slot > amap.am_nslot)
189 		return NULL;
190 
191 	addr = (u_long)amap.am_anon + (offset / kd->nbpg) * sizeof(anonp);
192 	if (KREAD(kd, addr, &anonp))
193 		return NULL;
194 
195 	addr = (u_long)anonp;
196 	if (KREAD(kd, addr, &anon))
197 		return NULL;
198 
199 	addr = (u_long)anon.u.an_page;
200 	if (addr) {
201 		if (KREAD(kd, addr, &pg))
202 			return NULL;
203 
204 		if (_kvm_pread(kd, kd->pmfd, (void *)kd->swapspc, (size_t)kd->nbpg, (off_t)pg.phys_addr) != kd->nbpg) {
205 			return NULL;
206 		}
207 	} else {
208 		if (_kvm_pread(kd, kd->swfd, (void *)kd->swapspc, (size_t)kd->nbpg, (off_t)(anon.an_swslot * kd->nbpg)) != kd->nbpg) {
209 			return NULL;
210 		}
211 	}
212 
213 	/* Found the page. */
214 	offset %= kd->nbpg;
215 	*cnt = kd->nbpg - offset;
216 	return (&kd->swapspc[offset]);
217 }
218 
219 /*
220  * Read proc's from memory file into buffer bp, which has space to hold
221  * at most maxcnt procs.
222  */
223 static int
224 kvm_proclist(kd, what, arg, p, bp, maxcnt)
225 	kvm_t *kd;
226 	int what, arg;
227 	struct proc *p;
228 	struct kinfo_proc *bp;
229 	int maxcnt;
230 {
231 	int cnt = 0;
232 	struct eproc eproc;
233 	struct pgrp pgrp;
234 	struct session sess;
235 	struct tty tty;
236 	struct proc proc;
237 
238 	for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) {
239 		if (KREAD(kd, (u_long)p, &proc)) {
240 			_kvm_err(kd, kd->program, "can't read proc at %x", p);
241 			return (-1);
242 		}
243 		if (KREAD(kd, (u_long)proc.p_cred, &eproc.e_pcred) == 0)
244 			KREAD(kd, (u_long)eproc.e_pcred.pc_ucred,
245 			      &eproc.e_ucred);
246 
247 		switch(what) {
248 
249 		case KERN_PROC_PID:
250 			if (proc.p_pid != (pid_t)arg)
251 				continue;
252 			break;
253 
254 		case KERN_PROC_UID:
255 			if (eproc.e_ucred.cr_uid != (uid_t)arg)
256 				continue;
257 			break;
258 
259 		case KERN_PROC_RUID:
260 			if (eproc.e_pcred.p_ruid != (uid_t)arg)
261 				continue;
262 			break;
263 
264 		case KERN_PROC_ALL:
265 			if (proc.p_flag & P_SYSTEM)
266 				continue;
267 			break;
268 		}
269 		/*
270 		 * We're going to add another proc to the set.  If this
271 		 * will overflow the buffer, assume the reason is because
272 		 * nprocs (or the proc list) is corrupt and declare an error.
273 		 */
274 		if (cnt >= maxcnt) {
275 			_kvm_err(kd, kd->program, "nprocs corrupt");
276 			return (-1);
277 		}
278 		/*
279 		 * gather eproc
280 		 */
281 		eproc.e_paddr = p;
282 		if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
283 			_kvm_err(kd, kd->program, "can't read pgrp at %x",
284 				 proc.p_pgrp);
285 			return (-1);
286 		}
287 		eproc.e_sess = pgrp.pg_session;
288 		eproc.e_pgid = pgrp.pg_id;
289 		eproc.e_jobc = pgrp.pg_jobc;
290 		if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
291 			_kvm_err(kd, kd->program, "can't read session at %x",
292 				pgrp.pg_session);
293 			return (-1);
294 		}
295 		if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) {
296 			if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
297 				_kvm_err(kd, kd->program,
298 					 "can't read tty at %x", sess.s_ttyp);
299 				return (-1);
300 			}
301 			eproc.e_tdev = tty.t_dev;
302 			eproc.e_tsess = tty.t_session;
303 			if (tty.t_pgrp != NULL) {
304 				if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
305 					_kvm_err(kd, kd->program,
306 						 "can't read tpgrp at &x",
307 						tty.t_pgrp);
308 					return (-1);
309 				}
310 				eproc.e_tpgid = pgrp.pg_id;
311 			} else
312 				eproc.e_tpgid = -1;
313 		} else
314 			eproc.e_tdev = NODEV;
315 		eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0;
316 		if (sess.s_leader == p)
317 			eproc.e_flag |= EPROC_SLEADER;
318 		if (proc.p_wmesg)
319 			(void)kvm_read(kd, (u_long)proc.p_wmesg,
320 			    eproc.e_wmesg, WMESGLEN);
321 
322 		(void)kvm_read(kd, (u_long)proc.p_vmspace,
323 		    &eproc.e_vm, sizeof(eproc.e_vm));
324 
325 		eproc.e_xsize = eproc.e_xrssize = 0;
326 		eproc.e_xccount = eproc.e_xswrss = 0;
327 
328 		switch (what) {
329 
330 		case KERN_PROC_PGRP:
331 			if (eproc.e_pgid != (pid_t)arg)
332 				continue;
333 			break;
334 
335 		case KERN_PROC_TTY:
336 			if ((proc.p_flag & P_CONTROLT) == 0 ||
337 			     eproc.e_tdev != (dev_t)arg)
338 				continue;
339 			break;
340 		}
341 		bcopy(&proc, &bp->kp_proc, sizeof(proc));
342 		bcopy(&eproc, &bp->kp_eproc, sizeof(eproc));
343 		++bp;
344 		++cnt;
345 	}
346 	return (cnt);
347 }
348 
349 /*
350  * Build proc info array by reading in proc list from a crash dump.
351  * Return number of procs read.  maxcnt is the max we will read.
352  */
353 static int
354 kvm_deadprocs(kd, what, arg, a_allproc, a_zombproc, maxcnt)
355 	kvm_t *kd;
356 	int what, arg;
357 	u_long a_allproc;
358 	u_long a_zombproc;
359 	int maxcnt;
360 {
361 	struct kinfo_proc *bp = kd->procbase;
362 	int acnt, zcnt;
363 	struct proc *p;
364 
365 	if (KREAD(kd, a_allproc, &p)) {
366 		_kvm_err(kd, kd->program, "cannot read allproc");
367 		return (-1);
368 	}
369 	acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
370 	if (acnt < 0)
371 		return (acnt);
372 
373 	if (KREAD(kd, a_zombproc, &p)) {
374 		_kvm_err(kd, kd->program, "cannot read zombproc");
375 		return (-1);
376 	}
377 	zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt);
378 	if (zcnt < 0)
379 		zcnt = 0;
380 
381 	return (acnt + zcnt);
382 }
383 
384 struct kinfo_proc *
385 kvm_getprocs(kd, op, arg, cnt)
386 	kvm_t *kd;
387 	int op, arg;
388 	int *cnt;
389 {
390 	size_t size;
391 	int mib[4], st, nprocs;
392 
393 	if (kd->procbase != 0) {
394 		free((void *)kd->procbase);
395 		/*
396 		 * Clear this pointer in case this call fails.  Otherwise,
397 		 * kvm_close() will free it again.
398 		 */
399 		kd->procbase = 0;
400 	}
401 	if (ISALIVE(kd)) {
402 		size = 0;
403 		mib[0] = CTL_KERN;
404 		mib[1] = KERN_PROC;
405 		mib[2] = op;
406 		mib[3] = arg;
407 		st = sysctl(mib, 4, NULL, &size, NULL, 0);
408 		if (st == -1) {
409 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
410 			return (0);
411 		}
412 		kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
413 		if (kd->procbase == 0)
414 			return (0);
415 		st = sysctl(mib, 4, kd->procbase, &size, NULL, 0);
416 		if (st == -1) {
417 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
418 			return (0);
419 		}
420 		if (size % sizeof(struct kinfo_proc) != 0) {
421 			_kvm_err(kd, kd->program,
422 				"proc size mismatch (%d total, %d chunks)",
423 				size, sizeof(struct kinfo_proc));
424 			return (0);
425 		}
426 		nprocs = size / sizeof(struct kinfo_proc);
427 	} else {
428 		struct nlist nl[4], *p;
429 
430 		nl[0].n_name = "_nprocs";
431 		nl[1].n_name = "_allproc";
432 		nl[2].n_name = "_zombproc";
433 		nl[3].n_name = 0;
434 
435 		if (kvm_nlist(kd, nl) != 0) {
436 			for (p = nl; p->n_type != 0; ++p)
437 				;
438 			_kvm_err(kd, kd->program,
439 				 "%s: no such symbol", p->n_name);
440 			return (0);
441 		}
442 		if (KREAD(kd, nl[0].n_value, &nprocs)) {
443 			_kvm_err(kd, kd->program, "can't read nprocs");
444 			return (0);
445 		}
446 		size = nprocs * sizeof(struct kinfo_proc);
447 		kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
448 		if (kd->procbase == 0)
449 			return (0);
450 
451 		nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
452 				      nl[2].n_value, nprocs);
453 #ifdef notdef
454 		size = nprocs * sizeof(struct kinfo_proc);
455 		(void)realloc(kd->procbase, size);
456 #endif
457 	}
458 	*cnt = nprocs;
459 	return (kd->procbase);
460 }
461 
462 void
463 _kvm_freeprocs(kd)
464 	kvm_t *kd;
465 {
466 	if (kd->procbase) {
467 		free(kd->procbase);
468 		kd->procbase = 0;
469 	}
470 }
471 
472 void *
473 _kvm_realloc(kd, p, n)
474 	kvm_t *kd;
475 	void *p;
476 	size_t n;
477 {
478 	void *np = (void *)realloc(p, n);
479 
480 	if (np == 0)
481 		_kvm_err(kd, kd->program, "out of memory");
482 	return (np);
483 }
484 
485 #ifndef MAX
486 #define MAX(a, b) ((a) > (b) ? (a) : (b))
487 #endif
488 
489 /*
490  * Read in an argument vector from the user address space of process p.
491  * addr if the user-space base address of narg null-terminated contiguous
492  * strings.  This is used to read in both the command arguments and
493  * environment strings.  Read at most maxcnt characters of strings.
494  */
495 static char **
496 kvm_argv(kd, p, addr, narg, maxcnt)
497 	kvm_t *kd;
498 	const struct proc *p;
499 	u_long addr;
500 	int narg;
501 	int maxcnt;
502 {
503 	char *np, *cp, *ep, *ap;
504 	u_long oaddr = -1;
505 	int len, cc;
506 	char **argv;
507 
508 	/*
509 	 * Check that there aren't an unreasonable number of agruments,
510 	 * and that the address is in user space.
511 	 */
512 	if (narg > ARG_MAX || addr < VM_MIN_ADDRESS || addr >= VM_MAXUSER_ADDRESS)
513 		return (0);
514 
515 	if (kd->argv == 0) {
516 		/*
517 		 * Try to avoid reallocs.
518 		 */
519 		kd->argc = MAX(narg + 1, 32);
520 		kd->argv = (char **)_kvm_malloc(kd, kd->argc *
521 						sizeof(*kd->argv));
522 		if (kd->argv == 0)
523 			return (0);
524 	} else if (narg + 1 > kd->argc) {
525 		kd->argc = MAX(2 * kd->argc, narg + 1);
526 		kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc *
527 						sizeof(*kd->argv));
528 		if (kd->argv == 0)
529 			return (0);
530 	}
531 	if (kd->argspc == 0) {
532 		kd->argspc = (char *)_kvm_malloc(kd, kd->nbpg);
533 		if (kd->argspc == 0)
534 			return (0);
535 		kd->arglen = kd->nbpg;
536 	}
537 	if (kd->argbuf == 0) {
538 		kd->argbuf = (char *)_kvm_malloc(kd, kd->nbpg);
539 		if (kd->argbuf == 0)
540 			return (0);
541 	}
542 	cc = sizeof(char *) * narg;
543 	if (kvm_uread(kd, p, addr, (char *)kd->argv, cc) != cc)
544 		return (0);
545 	ap = np = kd->argspc;
546 	argv = kd->argv;
547 	len = 0;
548 	/*
549 	 * Loop over pages, filling in the argument vector.
550 	 */
551 	while (argv < kd->argv + narg && *argv != 0) {
552 		addr = (u_long)*argv & ~(kd->nbpg - 1);
553 		if (addr != oaddr) {
554 			if (kvm_uread(kd, p, addr, kd->argbuf, kd->nbpg) !=
555 			    kd->nbpg)
556 				return (0);
557 			oaddr = addr;
558 		}
559 		addr = (u_long)*argv & (kd->nbpg - 1);
560 		cp = kd->argbuf + addr;
561 		cc = kd->nbpg - addr;
562 		if (maxcnt > 0 && cc > maxcnt - len)
563 			cc = maxcnt - len;;
564 		ep = memchr(cp, '\0', cc);
565 		if (ep != 0)
566 			cc = ep - cp + 1;
567 		if (len + cc > kd->arglen) {
568 			int off;
569 			char **pp;
570 			char *op = kd->argspc;
571 
572 			kd->arglen *= 2;
573 			kd->argspc = (char *)_kvm_realloc(kd, kd->argspc,
574 							  kd->arglen);
575 			if (kd->argspc == 0)
576 				return (0);
577 			/*
578 			 * Adjust argv pointers in case realloc moved
579 			 * the string space.
580 			 */
581 			off = kd->argspc - op;
582 			for (pp = kd->argv; pp < argv; pp++)
583 				*pp += off;
584 			ap += off;
585 			np += off;
586 		}
587 		memcpy(np, cp, cc);
588 		np += cc;
589 		len += cc;
590 		if (ep != 0) {
591 			*argv++ = ap;
592 			ap = np;
593 		} else
594 			*argv += cc;
595 		if (maxcnt > 0 && len >= maxcnt) {
596 			/*
597 			 * We're stopping prematurely.  Terminate the
598 			 * current string.
599 			 */
600 			if (ep == 0) {
601 				*np = '\0';
602 				*argv++ = ap;
603 			}
604 			break;
605 		}
606 	}
607 	/* Make sure argv is terminated. */
608 	*argv = 0;
609 	return (kd->argv);
610 }
611 
612 static void
613 ps_str_a(p, addr, n)
614 	struct ps_strings *p;
615 	u_long *addr;
616 	int *n;
617 {
618 	*addr = (u_long)p->ps_argvstr;
619 	*n = p->ps_nargvstr;
620 }
621 
622 static void
623 ps_str_e(p, addr, n)
624 	struct ps_strings *p;
625 	u_long *addr;
626 	int *n;
627 {
628 	*addr = (u_long)p->ps_envstr;
629 	*n = p->ps_nenvstr;
630 }
631 
632 /*
633  * Determine if the proc indicated by p is still active.
634  * This test is not 100% foolproof in theory, but chances of
635  * being wrong are very low.
636  */
637 static int
638 proc_verify(kd, kernp, p)
639 	kvm_t *kd;
640 	u_long kernp;
641 	const struct proc *p;
642 {
643 	struct proc kernproc;
644 
645 	/*
646 	 * Just read in the whole proc.  It's not that big relative
647 	 * to the cost of the read system call.
648 	 */
649 	if (kvm_read(kd, kernp, &kernproc, sizeof(kernproc)) !=
650 	    sizeof(kernproc))
651 		return (0);
652 	return (p->p_pid == kernproc.p_pid &&
653 		(kernproc.p_stat != SZOMB || p->p_stat == SZOMB));
654 }
655 
656 static char **
657 kvm_doargv(kd, kp, nchr, info)
658 	kvm_t *kd;
659 	const struct kinfo_proc *kp;
660 	int nchr;
661 	void (*info)(struct ps_strings *, u_long *, int *);
662 {
663 	const struct proc *p = &kp->kp_proc;
664 	char **ap;
665 	u_long addr;
666 	int cnt;
667 	struct ps_strings arginfo;
668 	static struct ps_strings *ps;
669 
670 	if (ps == NULL) {
671 		struct _ps_strings _ps;
672 		int mib[2];
673 		size_t len;
674 
675 		mib[0] = CTL_VM;
676 		mib[1] = VM_PSSTRINGS;
677 		len = sizeof(_ps);
678 		sysctl(mib, 2, &_ps, &len, NULL, 0);
679 		ps = (struct ps_strings *)_ps.val;
680 	}
681 
682 	/*
683 	 * Pointers are stored at the top of the user stack.
684 	 */
685 	if (p->p_stat == SZOMB ||
686 	    kvm_uread(kd, p, (u_long)ps, (char *)&arginfo,
687 		      sizeof(arginfo)) != sizeof(arginfo))
688 		return (0);
689 
690 	(*info)(&arginfo, &addr, &cnt);
691 	if (cnt == 0)
692 		return (0);
693 	ap = kvm_argv(kd, p, addr, cnt, nchr);
694 	/*
695 	 * For live kernels, make sure this process didn't go away.
696 	 */
697 	if (ap != 0 && ISALIVE(kd) &&
698 	    !proc_verify(kd, (u_long)kp->kp_eproc.e_paddr, p))
699 		ap = 0;
700 	return (ap);
701 }
702 
703 /*
704  * Get the command args.  This code is now machine independent.
705  */
706 char **
707 kvm_getargv(kd, kp, nchr)
708 	kvm_t *kd;
709 	const struct kinfo_proc *kp;
710 	int nchr;
711 {
712 	return (kvm_doargv(kd, kp, nchr, ps_str_a));
713 }
714 
715 char **
716 kvm_getenvv(kd, kp, nchr)
717 	kvm_t *kd;
718 	const struct kinfo_proc *kp;
719 	int nchr;
720 {
721 	return (kvm_doargv(kd, kp, nchr, ps_str_e));
722 }
723 
724 /*
725  * Read from user space.  The user context is given by p.
726  */
727 ssize_t
728 kvm_uread(kd, p, uva, buf, len)
729 	kvm_t *kd;
730 	const struct proc *p;
731 	u_long uva;
732 	char *buf;
733 	size_t len;
734 {
735 	char *cp;
736 
737 	cp = buf;
738 	while (len > 0) {
739 		int cc;
740 		char *dp;
741 		u_long cnt;
742 
743 		dp = _kvm_uread(kd, p, uva, &cnt);
744 		if (dp == 0) {
745 			_kvm_err(kd, 0, "invalid address (%lx)", uva);
746 			return (0);
747 		}
748 		cc = MIN(cnt, len);
749 		bcopy(dp, cp, cc);
750 
751 		cp += cc;
752 		uva += cc;
753 		len -= cc;
754 	}
755 	return (ssize_t)(cp - buf);
756 }
757