xref: /dragonfly/lib/libkvm/kvm_proc.c (revision 9bb2a92d)
1 /*-
2  * Copyright (c) 1989, 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software developed by the Computer Systems
6  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
7  * BG 91-66 and contributed to Berkeley.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  * $FreeBSD: src/lib/libkvm/kvm_proc.c,v 1.25.2.3 2002/08/24 07:27:46 kris Exp $
38  * $DragonFly: src/lib/libkvm/kvm_proc.c,v 1.5 2004/02/03 07:34:10 dillon Exp $
39  *
40  * @(#)kvm_proc.c	8.3 (Berkeley) 9/23/93
41  */
42 
43 /*
44  * Proc traversal interface for kvm.  ps and w are (probably) the exclusive
45  * users of this code, so we've factored it out into a separate module.
46  * Thus, we keep this grunge out of the other kvm applications (i.e.,
47  * most other applications are interested only in open/close/read/nlist).
48  */
49 
50 #include <sys/param.h>
51 #include <sys/user.h>
52 #include <sys/proc.h>
53 #include <sys/exec.h>
54 #include <sys/stat.h>
55 #include <sys/ioctl.h>
56 #include <sys/tty.h>
57 #include <sys/file.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <unistd.h>
61 #include <nlist.h>
62 #include <kvm.h>
63 
64 #include <vm/vm.h>
65 #include <vm/vm_param.h>
66 #include <vm/swap_pager.h>
67 
68 #include <sys/sysctl.h>
69 
70 #include <limits.h>
71 #include <memory.h>
72 #include <paths.h>
73 
74 #include "kvm_private.h"
75 
76 #if used
77 static char *
78 kvm_readswap(kd, p, va, cnt)
79 	kvm_t *kd;
80 	const struct proc *p;
81 	u_long va;
82 	u_long *cnt;
83 {
84 #if defined(__FreeBSD__) || defined(__DragonFly__)
85 	/* XXX Stubbed out, our vm system is differnet */
86 	_kvm_err(kd, kd->program, "kvm_readswap not implemented");
87 	return(0);
88 #endif
89 }
90 #endif
91 
92 #define KREAD(kd, addr, obj) \
93 	(kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj))
94 
95 /*
96  * Read proc's from memory file into buffer bp, which has space to hold
97  * at most maxcnt procs.
98  */
99 static int
100 kvm_proclist(kd, what, arg, p, bp, maxcnt)
101 	kvm_t *kd;
102 	int what, arg;
103 	struct proc *p;
104 	struct kinfo_proc *bp;
105 	int maxcnt;
106 {
107 	register int cnt = 0;
108 	struct eproc eproc;
109 	struct pgrp pgrp;
110 	struct session sess;
111 	struct tty tty;
112 	struct proc proc;
113 	struct thread thread;
114 	struct proc pproc;
115 
116 	for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) {
117 		if (KREAD(kd, (u_long)p, &proc)) {
118 			_kvm_err(kd, kd->program, "can't read proc at %x", p);
119 			return (-1);
120 		}
121 		if (KREAD(kd, (u_long)proc.p_thread, &thread)) {
122 			_kvm_err(kd, kd->program, "can't read thread at %x",
123 			    proc.p_thread);
124 			return (-1);
125 		}
126 		KREAD(kd, (u_long)proc.p_ucred, &eproc.e_ucred);
127 
128 		switch(what) {
129 
130 		case KERN_PROC_PID:
131 			if (proc.p_pid != (pid_t)arg)
132 				continue;
133 			break;
134 
135 		case KERN_PROC_UID:
136 			if (eproc.e_ucred.cr_uid != (uid_t)arg)
137 				continue;
138 			break;
139 
140 		case KERN_PROC_RUID:
141 			if (eproc.e_ucred.cr_ruid != (uid_t)arg)
142 				continue;
143 			break;
144 		}
145 		/*
146 		 * We're going to add another proc to the set.  If this
147 		 * will overflow the buffer, assume the reason is because
148 		 * nprocs (or the proc list) is corrupt and declare an error.
149 		 */
150 		if (cnt >= maxcnt) {
151 			_kvm_err(kd, kd->program, "nprocs corrupt");
152 			return (-1);
153 		}
154 		/*
155 		 * gather eproc
156 		 */
157 		eproc.e_paddr = p;
158 		if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
159 			_kvm_err(kd, kd->program, "can't read pgrp at %x",
160 				 proc.p_pgrp);
161 			return (-1);
162 		}
163 		if (proc.p_oppid)
164 		  eproc.e_ppid = proc.p_oppid;
165 		else if (proc.p_pptr) {
166 		  if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) {
167 			_kvm_err(kd, kd->program, "can't read pproc at %x",
168 				 proc.p_pptr);
169 			return (-1);
170 		  }
171 		  eproc.e_ppid = pproc.p_pid;
172 		} else
173 		  eproc.e_ppid = 0;
174 		eproc.e_sess = pgrp.pg_session;
175 		eproc.e_pgid = pgrp.pg_id;
176 		eproc.e_jobc = pgrp.pg_jobc;
177 		if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
178 			_kvm_err(kd, kd->program, "can't read session at %x",
179 				pgrp.pg_session);
180 			return (-1);
181 		}
182 		(void)memcpy(eproc.e_login, sess.s_login,
183 						sizeof(eproc.e_login));
184 		if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) {
185 			if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
186 				_kvm_err(kd, kd->program,
187 					 "can't read tty at %x", sess.s_ttyp);
188 				return (-1);
189 			}
190 			eproc.e_tdev = tty.t_dev;
191 			eproc.e_tsess = tty.t_session;
192 			if (tty.t_pgrp != NULL) {
193 				if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
194 					_kvm_err(kd, kd->program,
195 						 "can't read tpgrp at %x",
196 						tty.t_pgrp);
197 					return (-1);
198 				}
199 				eproc.e_tpgid = pgrp.pg_id;
200 			} else
201 				eproc.e_tpgid = -1;
202 		} else
203 			eproc.e_tdev = NODEV;
204 		eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0;
205 		if (sess.s_leader == p)
206 			eproc.e_flag |= EPROC_SLEADER;
207 		if (thread.td_wmesg)
208 			(void)kvm_read(kd, (u_long)thread.td_wmesg,
209 			    eproc.e_wmesg, WMESGLEN);
210 
211 #ifdef sparc
212 		(void)kvm_read(kd, (u_long)&proc.p_vmspace->vm_rssize,
213 		    (char *)&eproc.e_vm.vm_rssize,
214 		    sizeof(eproc.e_vm.vm_rssize));
215 		(void)kvm_read(kd, (u_long)&proc.p_vmspace->vm_tsize,
216 		    (char *)&eproc.e_vm.vm_tsize,
217 		    3 * sizeof(eproc.e_vm.vm_rssize));	/* XXX */
218 #else
219 		(void)kvm_read(kd, (u_long)proc.p_vmspace,
220 		    (char *)&eproc.e_vm, sizeof(eproc.e_vm));
221 #endif
222 		eproc.e_xsize = eproc.e_xrssize = 0;
223 		eproc.e_xccount = eproc.e_xswrss = 0;
224 
225 		switch (what) {
226 
227 		case KERN_PROC_PGRP:
228 			if (eproc.e_pgid != (pid_t)arg)
229 				continue;
230 			break;
231 
232 		case KERN_PROC_TTY:
233 			if ((proc.p_flag & P_CONTROLT) == 0 ||
234 			     eproc.e_tdev != (dev_t)arg)
235 				continue;
236 			break;
237 		}
238 		bcopy(&proc, &bp->kp_proc, sizeof(proc));
239 		bcopy(&eproc, &bp->kp_eproc, sizeof(eproc));
240 		++bp;
241 		++cnt;
242 	}
243 	return (cnt);
244 }
245 
246 /*
247  * Build proc info array by reading in proc list from a crash dump.
248  * Return number of procs read.  maxcnt is the max we will read.
249  */
250 static int
251 kvm_deadprocs(kd, what, arg, a_allproc, a_zombproc, maxcnt)
252 	kvm_t *kd;
253 	int what, arg;
254 	u_long a_allproc;
255 	u_long a_zombproc;
256 	int maxcnt;
257 {
258 	register struct kinfo_proc *bp = kd->procbase;
259 	register int acnt, zcnt;
260 	struct proc *p;
261 
262 	if (KREAD(kd, a_allproc, &p)) {
263 		_kvm_err(kd, kd->program, "cannot read allproc");
264 		return (-1);
265 	}
266 	acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
267 	if (acnt < 0)
268 		return (acnt);
269 
270 	if (KREAD(kd, a_zombproc, &p)) {
271 		_kvm_err(kd, kd->program, "cannot read zombproc");
272 		return (-1);
273 	}
274 	zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt);
275 	if (zcnt < 0)
276 		zcnt = 0;
277 
278 	return (acnt + zcnt);
279 }
280 
281 struct kinfo_proc *
282 kvm_getprocs(kd, op, arg, cnt)
283 	kvm_t *kd;
284 	int op, arg;
285 	int *cnt;
286 {
287 	int mib[4], st, nprocs;
288 	size_t size;
289 
290 	if (kd->procbase != 0) {
291 		free((void *)kd->procbase);
292 		/*
293 		 * Clear this pointer in case this call fails.  Otherwise,
294 		 * kvm_close() will free it again.
295 		 */
296 		kd->procbase = 0;
297 	}
298 	if (ISALIVE(kd)) {
299 		size = 0;
300 		mib[0] = CTL_KERN;
301 		mib[1] = KERN_PROC;
302 		mib[2] = op;
303 		mib[3] = arg;
304 		st = sysctl(mib, op == KERN_PROC_ALL ? 3 : 4, NULL, &size, NULL, 0);
305 		if (st == -1) {
306 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
307 			return (0);
308 		}
309 		do {
310 			size += size / 10;
311 			kd->procbase = (struct kinfo_proc *)
312 			    _kvm_realloc(kd, kd->procbase, size);
313 			if (kd->procbase == 0)
314 				return (0);
315 			st = sysctl(mib, op == KERN_PROC_ALL ? 3 : 4,
316 			    kd->procbase, &size, NULL, 0);
317 		} while (st == -1 && errno == ENOMEM);
318 		if (st == -1) {
319 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
320 			return (0);
321 		}
322 		if (size % sizeof(struct kinfo_proc) != 0) {
323 			_kvm_err(kd, kd->program,
324 				"proc size mismatch (%d total, %d chunks)",
325 				size, sizeof(struct kinfo_proc));
326 			return (0);
327 		}
328 		nprocs = size / sizeof(struct kinfo_proc);
329 	} else {
330 		struct nlist nl[4], *p;
331 
332 		nl[0].n_name = "_nprocs";
333 		nl[1].n_name = "_allproc";
334 		nl[2].n_name = "_zombproc";
335 		nl[3].n_name = 0;
336 
337 		if (kvm_nlist(kd, nl) != 0) {
338 			for (p = nl; p->n_type != 0; ++p)
339 				;
340 			_kvm_err(kd, kd->program,
341 				 "%s: no such symbol", p->n_name);
342 			return (0);
343 		}
344 		if (KREAD(kd, nl[0].n_value, &nprocs)) {
345 			_kvm_err(kd, kd->program, "can't read nprocs");
346 			return (0);
347 		}
348 		size = nprocs * sizeof(struct kinfo_proc);
349 		kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
350 		if (kd->procbase == 0)
351 			return (0);
352 
353 		nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
354 				      nl[2].n_value, nprocs);
355 #ifdef notdef
356 		size = nprocs * sizeof(struct kinfo_proc);
357 		(void)realloc(kd->procbase, size);
358 #endif
359 	}
360 	*cnt = nprocs;
361 	return (kd->procbase);
362 }
363 
364 void
365 _kvm_freeprocs(kd)
366 	kvm_t *kd;
367 {
368 	if (kd->procbase) {
369 		free(kd->procbase);
370 		kd->procbase = 0;
371 	}
372 }
373 
374 void *
375 _kvm_realloc(kd, p, n)
376 	kvm_t *kd;
377 	void *p;
378 	size_t n;
379 {
380 	void *np = (void *)realloc(p, n);
381 
382 	if (np == 0) {
383 		free(p);
384 		_kvm_err(kd, kd->program, "out of memory");
385 	}
386 	return (np);
387 }
388 
389 #ifndef MAX
390 #define MAX(a, b) ((a) > (b) ? (a) : (b))
391 #endif
392 
393 /*
394  * Read in an argument vector from the user address space of process p.
395  * addr if the user-space base address of narg null-terminated contiguous
396  * strings.  This is used to read in both the command arguments and
397  * environment strings.  Read at most maxcnt characters of strings.
398  */
399 static char **
400 kvm_argv(kd, p, addr, narg, maxcnt)
401 	kvm_t *kd;
402 	const struct proc *p;
403 	register u_long addr;
404 	register int narg;
405 	register int maxcnt;
406 {
407 	register char *np, *cp, *ep, *ap;
408 	register u_long oaddr = -1;
409 	register int len, cc;
410 	register char **argv;
411 
412 	/*
413 	 * Check that there aren't an unreasonable number of agruments,
414 	 * and that the address is in user space.
415 	 */
416 	if (narg > 512 || addr < VM_MIN_ADDRESS || addr >= VM_MAXUSER_ADDRESS)
417 		return (0);
418 
419 	/*
420 	 * kd->argv : work space for fetching the strings from the target
421 	 *            process's space, and is converted for returning to caller
422 	 */
423 	if (kd->argv == 0) {
424 		/*
425 		 * Try to avoid reallocs.
426 		 */
427 		kd->argc = MAX(narg + 1, 32);
428 		kd->argv = (char **)_kvm_malloc(kd, kd->argc *
429 						sizeof(*kd->argv));
430 		if (kd->argv == 0)
431 			return (0);
432 	} else if (narg + 1 > kd->argc) {
433 		kd->argc = MAX(2 * kd->argc, narg + 1);
434 		kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc *
435 						sizeof(*kd->argv));
436 		if (kd->argv == 0)
437 			return (0);
438 	}
439 	/*
440 	 * kd->argspc : returned to user, this is where the kd->argv
441 	 *              arrays are left pointing to the collected strings.
442 	 */
443 	if (kd->argspc == 0) {
444 		kd->argspc = (char *)_kvm_malloc(kd, PAGE_SIZE);
445 		if (kd->argspc == 0)
446 			return (0);
447 		kd->arglen = PAGE_SIZE;
448 	}
449 	/*
450 	 * kd->argbuf : used to pull in pages from the target process.
451 	 *              the strings are copied out of here.
452 	 */
453 	if (kd->argbuf == 0) {
454 		kd->argbuf = (char *)_kvm_malloc(kd, PAGE_SIZE);
455 		if (kd->argbuf == 0)
456 			return (0);
457 	}
458 
459 	/* Pull in the target process'es argv vector */
460 	cc = sizeof(char *) * narg;
461 	if (kvm_uread(kd, p, addr, (char *)kd->argv, cc) != cc)
462 		return (0);
463 	/*
464 	 * ap : saved start address of string we're working on in kd->argspc
465 	 * np : pointer to next place to write in kd->argspc
466 	 * len: length of data in kd->argspc
467 	 * argv: pointer to the argv vector that we are hunting around the
468 	 *       target process space for, and converting to addresses in
469 	 *       our address space (kd->argspc).
470 	 */
471 	ap = np = kd->argspc;
472 	argv = kd->argv;
473 	len = 0;
474 	/*
475 	 * Loop over pages, filling in the argument vector.
476 	 * Note that the argv strings could be pointing *anywhere* in
477 	 * the user address space and are no longer contiguous.
478 	 * Note that *argv is modified when we are going to fetch a string
479 	 * that crosses a page boundary.  We copy the next part of the string
480 	 * into to "np" and eventually convert the pointer.
481 	 */
482 	while (argv < kd->argv + narg && *argv != 0) {
483 
484 		/* get the address that the current argv string is on */
485 		addr = (u_long)*argv & ~(PAGE_SIZE - 1);
486 
487 		/* is it the same page as the last one? */
488 		if (addr != oaddr) {
489 			if (kvm_uread(kd, p, addr, kd->argbuf, PAGE_SIZE) !=
490 			    PAGE_SIZE)
491 				return (0);
492 			oaddr = addr;
493 		}
494 
495 		/* offset within the page... kd->argbuf */
496 		addr = (u_long)*argv & (PAGE_SIZE - 1);
497 
498 		/* cp = start of string, cc = count of chars in this chunk */
499 		cp = kd->argbuf + addr;
500 		cc = PAGE_SIZE - addr;
501 
502 		/* dont get more than asked for by user process */
503 		if (maxcnt > 0 && cc > maxcnt - len)
504 			cc = maxcnt - len;
505 
506 		/* pointer to end of string if we found it in this page */
507 		ep = memchr(cp, '\0', cc);
508 		if (ep != 0)
509 			cc = ep - cp + 1;
510 		/*
511 		 * at this point, cc is the count of the chars that we are
512 		 * going to retrieve this time. we may or may not have found
513 		 * the end of it.  (ep points to the null if the end is known)
514 		 */
515 
516 		/* will we exceed the malloc/realloced buffer? */
517 		if (len + cc > kd->arglen) {
518 			register int off;
519 			register char **pp;
520 			register char *op = kd->argspc;
521 
522 			kd->arglen *= 2;
523 			kd->argspc = (char *)_kvm_realloc(kd, kd->argspc,
524 							  kd->arglen);
525 			if (kd->argspc == 0)
526 				return (0);
527 			/*
528 			 * Adjust argv pointers in case realloc moved
529 			 * the string space.
530 			 */
531 			off = kd->argspc - op;
532 			for (pp = kd->argv; pp < argv; pp++)
533 				*pp += off;
534 			ap += off;
535 			np += off;
536 		}
537 		/* np = where to put the next part of the string in kd->argspc*/
538 		/* np is kinda redundant.. could use "kd->argspc + len" */
539 		memcpy(np, cp, cc);
540 		np += cc;	/* inc counters */
541 		len += cc;
542 
543 		/*
544 		 * if end of string found, set the *argv pointer to the
545 		 * saved beginning of string, and advance. argv points to
546 		 * somewhere in kd->argv..  This is initially relative
547 		 * to the target process, but when we close it off, we set
548 		 * it to point in our address space.
549 		 */
550 		if (ep != 0) {
551 			*argv++ = ap;
552 			ap = np;
553 		} else {
554 			/* update the address relative to the target process */
555 			*argv += cc;
556 		}
557 
558 		if (maxcnt > 0 && len >= maxcnt) {
559 			/*
560 			 * We're stopping prematurely.  Terminate the
561 			 * current string.
562 			 */
563 			if (ep == 0) {
564 				*np = '\0';
565 				*argv++ = ap;
566 			}
567 			break;
568 		}
569 	}
570 	/* Make sure argv is terminated. */
571 	*argv = 0;
572 	return (kd->argv);
573 }
574 
575 static void
576 ps_str_a(p, addr, n)
577 	struct ps_strings *p;
578 	u_long *addr;
579 	int *n;
580 {
581 	*addr = (u_long)p->ps_argvstr;
582 	*n = p->ps_nargvstr;
583 }
584 
585 static void
586 ps_str_e(p, addr, n)
587 	struct ps_strings *p;
588 	u_long *addr;
589 	int *n;
590 {
591 	*addr = (u_long)p->ps_envstr;
592 	*n = p->ps_nenvstr;
593 }
594 
595 /*
596  * Determine if the proc indicated by p is still active.
597  * This test is not 100% foolproof in theory, but chances of
598  * being wrong are very low.
599  */
600 static int
601 proc_verify(kd, kernp, p)
602 	kvm_t *kd;
603 	u_long kernp;
604 	const struct proc *p;
605 {
606 	struct kinfo_proc kp;
607 	int mib[4];
608 	size_t len;
609 
610 	mib[0] = CTL_KERN;
611 	mib[1] = KERN_PROC;
612 	mib[2] = KERN_PROC_PID;
613 	mib[3] = p->p_pid;
614 	len = sizeof(kp);
615 	if (sysctl(mib, 4, &kp, &len, NULL, 0) == -1)
616 		return (0);
617 	return (p->p_pid == kp.kp_proc.p_pid &&
618 	    (kp.kp_proc.p_stat != SZOMB || p->p_stat == SZOMB));
619 }
620 
621 static char **
622 kvm_doargv(kd, kp, nchr, info)
623 	kvm_t *kd;
624 	const struct kinfo_proc *kp;
625 	int nchr;
626 	void (*info)(struct ps_strings *, u_long *, int *);
627 {
628 	register const struct proc *p = &kp->kp_proc;
629 	register char **ap;
630 	u_long addr;
631 	int cnt;
632 	static struct ps_strings arginfo;
633 	static u_long ps_strings;
634 	size_t len;
635 
636 	if (ps_strings == NULL) {
637 		len = sizeof(ps_strings);
638 		if (sysctlbyname("kern.ps_strings", &ps_strings, &len, NULL,
639 		    0) == -1)
640 			ps_strings = PS_STRINGS;
641 	}
642 
643 	/*
644 	 * Pointers are stored at the top of the user stack.
645 	 */
646 	if (p->p_stat == SZOMB ||
647 	    kvm_uread(kd, p, ps_strings, (char *)&arginfo,
648 		      sizeof(arginfo)) != sizeof(arginfo))
649 		return (0);
650 
651 	(*info)(&arginfo, &addr, &cnt);
652 	if (cnt == 0)
653 		return (0);
654 	ap = kvm_argv(kd, p, addr, cnt, nchr);
655 	/*
656 	 * For live kernels, make sure this process didn't go away.
657 	 */
658 	if (ap != 0 && ISALIVE(kd) &&
659 	    !proc_verify(kd, (u_long)kp->kp_eproc.e_paddr, p))
660 		ap = 0;
661 	return (ap);
662 }
663 
664 /*
665  * Get the command args.  This code is now machine independent.
666  */
667 char **
668 kvm_getargv(kd, kp, nchr)
669 	kvm_t *kd;
670 	const struct kinfo_proc *kp;
671 	int nchr;
672 {
673 	int oid[4];
674 	int i;
675 	size_t bufsz;
676 	static unsigned long buflen;
677 	static char *buf, *p;
678 	static char **bufp;
679 	static int argc;
680 
681 	if (!ISALIVE(kd)) {
682 		_kvm_err(kd, kd->program,
683 		    "cannot read user space from dead kernel");
684 		return (0);
685 	}
686 
687 	if (!buflen) {
688 		bufsz = sizeof(buflen);
689 		i = sysctlbyname("kern.ps_arg_cache_limit",
690 		    &buflen, &bufsz, NULL, 0);
691 		if (i == -1) {
692 			buflen = 0;
693 		} else {
694 			buf = malloc(buflen);
695 			if (buf == NULL)
696 				buflen = 0;
697 			argc = 32;
698 			bufp = malloc(sizeof(char *) * argc);
699 		}
700 	}
701 	if (buf != NULL) {
702 		oid[0] = CTL_KERN;
703 		oid[1] = KERN_PROC;
704 		oid[2] = KERN_PROC_ARGS;
705 		oid[3] = kp->kp_proc.p_pid;
706 		bufsz = buflen;
707 		i = sysctl(oid, 4, buf, &bufsz, 0, 0);
708 		if (i == 0 && bufsz > 0) {
709 			i = 0;
710 			p = buf;
711 			do {
712 				bufp[i++] = p;
713 				p += strlen(p) + 1;
714 				if (i >= argc) {
715 					argc += argc;
716 					bufp = realloc(bufp,
717 					    sizeof(char *) * argc);
718 				}
719 			} while (p < buf + bufsz);
720 			bufp[i++] = 0;
721 			return (bufp);
722 		}
723 	}
724 	if (kp->kp_proc.p_flag & P_SYSTEM)
725 		return (NULL);
726 	return (kvm_doargv(kd, kp, nchr, ps_str_a));
727 }
728 
729 char **
730 kvm_getenvv(kd, kp, nchr)
731 	kvm_t *kd;
732 	const struct kinfo_proc *kp;
733 	int nchr;
734 {
735 	return (kvm_doargv(kd, kp, nchr, ps_str_e));
736 }
737 
738 /*
739  * Read from user space.  The user context is given by p.
740  */
741 ssize_t
742 kvm_uread(kd, p, uva, buf, len)
743 	kvm_t *kd;
744 	register const struct proc *p;
745 	register u_long uva;
746 	register char *buf;
747 	register size_t len;
748 {
749 	register char *cp;
750 	char procfile[MAXPATHLEN];
751 	ssize_t amount;
752 	int fd;
753 
754 	if (!ISALIVE(kd)) {
755 		_kvm_err(kd, kd->program,
756 		    "cannot read user space from dead kernel");
757 		return (0);
758 	}
759 
760 	sprintf(procfile, "/proc/%d/mem", p->p_pid);
761 	fd = open(procfile, O_RDONLY, 0);
762 	if (fd < 0) {
763 		_kvm_err(kd, kd->program, "cannot open %s", procfile);
764 		close(fd);
765 		return (0);
766 	}
767 
768 	cp = buf;
769 	while (len > 0) {
770 		errno = 0;
771 		if (lseek(fd, (off_t)uva, 0) == -1 && errno != 0) {
772 			_kvm_err(kd, kd->program, "invalid address (%x) in %s",
773 			    uva, procfile);
774 			break;
775 		}
776 		amount = read(fd, cp, len);
777 		if (amount < 0) {
778 			_kvm_syserr(kd, kd->program, "error reading %s",
779 			    procfile);
780 			break;
781 		}
782 		if (amount == 0) {
783 			_kvm_err(kd, kd->program, "EOF reading %s", procfile);
784 			break;
785 		}
786 		cp += amount;
787 		uva += amount;
788 		len -= amount;
789 	}
790 
791 	close(fd);
792 	return ((ssize_t)(cp - buf));
793 }
794