xref: /dragonfly/lib/libkvm/kvm_proc.c (revision 1de703da)
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.2 2003/06/17 04:26:49 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 #ifdef __FreeBSD__
85 	/* XXX Stubbed out, our vm system is differnet */
86 	_kvm_err(kd, kd->program, "kvm_readswap not implemented");
87 	return(0);
88 #endif	/* __FreeBSD__ */
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 proc pproc;
114 
115 	for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) {
116 		if (KREAD(kd, (u_long)p, &proc)) {
117 			_kvm_err(kd, kd->program, "can't read proc at %x", p);
118 			return (-1);
119 		}
120 		if (KREAD(kd, (u_long)proc.p_cred, &eproc.e_pcred) == 0)
121 			(void)(KREAD(kd, (u_long)eproc.e_pcred.pc_ucred,
122 			             &eproc.e_ucred));
123 
124 		switch(what) {
125 
126 		case KERN_PROC_PID:
127 			if (proc.p_pid != (pid_t)arg)
128 				continue;
129 			break;
130 
131 		case KERN_PROC_UID:
132 			if (eproc.e_ucred.cr_uid != (uid_t)arg)
133 				continue;
134 			break;
135 
136 		case KERN_PROC_RUID:
137 			if (eproc.e_pcred.p_ruid != (uid_t)arg)
138 				continue;
139 			break;
140 		}
141 		/*
142 		 * We're going to add another proc to the set.  If this
143 		 * will overflow the buffer, assume the reason is because
144 		 * nprocs (or the proc list) is corrupt and declare an error.
145 		 */
146 		if (cnt >= maxcnt) {
147 			_kvm_err(kd, kd->program, "nprocs corrupt");
148 			return (-1);
149 		}
150 		/*
151 		 * gather eproc
152 		 */
153 		eproc.e_paddr = p;
154 		if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
155 			_kvm_err(kd, kd->program, "can't read pgrp at %x",
156 				 proc.p_pgrp);
157 			return (-1);
158 		}
159 		if (proc.p_oppid)
160 		  eproc.e_ppid = proc.p_oppid;
161 		else if (proc.p_pptr) {
162 		  if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) {
163 			_kvm_err(kd, kd->program, "can't read pproc at %x",
164 				 proc.p_pptr);
165 			return (-1);
166 		  }
167 		  eproc.e_ppid = pproc.p_pid;
168 		} else
169 		  eproc.e_ppid = 0;
170 		eproc.e_sess = pgrp.pg_session;
171 		eproc.e_pgid = pgrp.pg_id;
172 		eproc.e_jobc = pgrp.pg_jobc;
173 		if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
174 			_kvm_err(kd, kd->program, "can't read session at %x",
175 				pgrp.pg_session);
176 			return (-1);
177 		}
178 		(void)memcpy(eproc.e_login, sess.s_login,
179 						sizeof(eproc.e_login));
180 		if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) {
181 			if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
182 				_kvm_err(kd, kd->program,
183 					 "can't read tty at %x", sess.s_ttyp);
184 				return (-1);
185 			}
186 			eproc.e_tdev = tty.t_dev;
187 			eproc.e_tsess = tty.t_session;
188 			if (tty.t_pgrp != NULL) {
189 				if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
190 					_kvm_err(kd, kd->program,
191 						 "can't read tpgrp at %x",
192 						tty.t_pgrp);
193 					return (-1);
194 				}
195 				eproc.e_tpgid = pgrp.pg_id;
196 			} else
197 				eproc.e_tpgid = -1;
198 		} else
199 			eproc.e_tdev = NODEV;
200 		eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0;
201 		if (sess.s_leader == p)
202 			eproc.e_flag |= EPROC_SLEADER;
203 		if (proc.p_wmesg)
204 			(void)kvm_read(kd, (u_long)proc.p_wmesg,
205 			    eproc.e_wmesg, WMESGLEN);
206 
207 #ifdef sparc
208 		(void)kvm_read(kd, (u_long)&proc.p_vmspace->vm_rssize,
209 		    (char *)&eproc.e_vm.vm_rssize,
210 		    sizeof(eproc.e_vm.vm_rssize));
211 		(void)kvm_read(kd, (u_long)&proc.p_vmspace->vm_tsize,
212 		    (char *)&eproc.e_vm.vm_tsize,
213 		    3 * sizeof(eproc.e_vm.vm_rssize));	/* XXX */
214 #else
215 		(void)kvm_read(kd, (u_long)proc.p_vmspace,
216 		    (char *)&eproc.e_vm, sizeof(eproc.e_vm));
217 #endif
218 		eproc.e_xsize = eproc.e_xrssize = 0;
219 		eproc.e_xccount = eproc.e_xswrss = 0;
220 
221 		switch (what) {
222 
223 		case KERN_PROC_PGRP:
224 			if (eproc.e_pgid != (pid_t)arg)
225 				continue;
226 			break;
227 
228 		case KERN_PROC_TTY:
229 			if ((proc.p_flag & P_CONTROLT) == 0 ||
230 			     eproc.e_tdev != (dev_t)arg)
231 				continue;
232 			break;
233 		}
234 		bcopy(&proc, &bp->kp_proc, sizeof(proc));
235 		bcopy(&eproc, &bp->kp_eproc, sizeof(eproc));
236 		++bp;
237 		++cnt;
238 	}
239 	return (cnt);
240 }
241 
242 /*
243  * Build proc info array by reading in proc list from a crash dump.
244  * Return number of procs read.  maxcnt is the max we will read.
245  */
246 static int
247 kvm_deadprocs(kd, what, arg, a_allproc, a_zombproc, maxcnt)
248 	kvm_t *kd;
249 	int what, arg;
250 	u_long a_allproc;
251 	u_long a_zombproc;
252 	int maxcnt;
253 {
254 	register struct kinfo_proc *bp = kd->procbase;
255 	register int acnt, zcnt;
256 	struct proc *p;
257 
258 	if (KREAD(kd, a_allproc, &p)) {
259 		_kvm_err(kd, kd->program, "cannot read allproc");
260 		return (-1);
261 	}
262 	acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
263 	if (acnt < 0)
264 		return (acnt);
265 
266 	if (KREAD(kd, a_zombproc, &p)) {
267 		_kvm_err(kd, kd->program, "cannot read zombproc");
268 		return (-1);
269 	}
270 	zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt);
271 	if (zcnt < 0)
272 		zcnt = 0;
273 
274 	return (acnt + zcnt);
275 }
276 
277 struct kinfo_proc *
278 kvm_getprocs(kd, op, arg, cnt)
279 	kvm_t *kd;
280 	int op, arg;
281 	int *cnt;
282 {
283 	int mib[4], st, nprocs;
284 	size_t size;
285 
286 	if (kd->procbase != 0) {
287 		free((void *)kd->procbase);
288 		/*
289 		 * Clear this pointer in case this call fails.  Otherwise,
290 		 * kvm_close() will free it again.
291 		 */
292 		kd->procbase = 0;
293 	}
294 	if (ISALIVE(kd)) {
295 		size = 0;
296 		mib[0] = CTL_KERN;
297 		mib[1] = KERN_PROC;
298 		mib[2] = op;
299 		mib[3] = arg;
300 		st = sysctl(mib, op == KERN_PROC_ALL ? 3 : 4, NULL, &size, NULL, 0);
301 		if (st == -1) {
302 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
303 			return (0);
304 		}
305 		do {
306 			size += size / 10;
307 			kd->procbase = (struct kinfo_proc *)
308 			    _kvm_realloc(kd, kd->procbase, size);
309 			if (kd->procbase == 0)
310 				return (0);
311 			st = sysctl(mib, op == KERN_PROC_ALL ? 3 : 4,
312 			    kd->procbase, &size, NULL, 0);
313 		} while (st == -1 && errno == ENOMEM);
314 		if (st == -1) {
315 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
316 			return (0);
317 		}
318 		if (size % sizeof(struct kinfo_proc) != 0) {
319 			_kvm_err(kd, kd->program,
320 				"proc size mismatch (%d total, %d chunks)",
321 				size, sizeof(struct kinfo_proc));
322 			return (0);
323 		}
324 		nprocs = size / sizeof(struct kinfo_proc);
325 	} else {
326 		struct nlist nl[4], *p;
327 
328 		nl[0].n_name = "_nprocs";
329 		nl[1].n_name = "_allproc";
330 		nl[2].n_name = "_zombproc";
331 		nl[3].n_name = 0;
332 
333 		if (kvm_nlist(kd, nl) != 0) {
334 			for (p = nl; p->n_type != 0; ++p)
335 				;
336 			_kvm_err(kd, kd->program,
337 				 "%s: no such symbol", p->n_name);
338 			return (0);
339 		}
340 		if (KREAD(kd, nl[0].n_value, &nprocs)) {
341 			_kvm_err(kd, kd->program, "can't read nprocs");
342 			return (0);
343 		}
344 		size = nprocs * sizeof(struct kinfo_proc);
345 		kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
346 		if (kd->procbase == 0)
347 			return (0);
348 
349 		nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
350 				      nl[2].n_value, nprocs);
351 #ifdef notdef
352 		size = nprocs * sizeof(struct kinfo_proc);
353 		(void)realloc(kd->procbase, size);
354 #endif
355 	}
356 	*cnt = nprocs;
357 	return (kd->procbase);
358 }
359 
360 void
361 _kvm_freeprocs(kd)
362 	kvm_t *kd;
363 {
364 	if (kd->procbase) {
365 		free(kd->procbase);
366 		kd->procbase = 0;
367 	}
368 }
369 
370 void *
371 _kvm_realloc(kd, p, n)
372 	kvm_t *kd;
373 	void *p;
374 	size_t n;
375 {
376 	void *np = (void *)realloc(p, n);
377 
378 	if (np == 0) {
379 		free(p);
380 		_kvm_err(kd, kd->program, "out of memory");
381 	}
382 	return (np);
383 }
384 
385 #ifndef MAX
386 #define MAX(a, b) ((a) > (b) ? (a) : (b))
387 #endif
388 
389 /*
390  * Read in an argument vector from the user address space of process p.
391  * addr if the user-space base address of narg null-terminated contiguous
392  * strings.  This is used to read in both the command arguments and
393  * environment strings.  Read at most maxcnt characters of strings.
394  */
395 static char **
396 kvm_argv(kd, p, addr, narg, maxcnt)
397 	kvm_t *kd;
398 	const struct proc *p;
399 	register u_long addr;
400 	register int narg;
401 	register int maxcnt;
402 {
403 	register char *np, *cp, *ep, *ap;
404 	register u_long oaddr = -1;
405 	register int len, cc;
406 	register char **argv;
407 
408 	/*
409 	 * Check that there aren't an unreasonable number of agruments,
410 	 * and that the address is in user space.
411 	 */
412 	if (narg > 512 || addr < VM_MIN_ADDRESS || addr >= VM_MAXUSER_ADDRESS)
413 		return (0);
414 
415 	/*
416 	 * kd->argv : work space for fetching the strings from the target
417 	 *            process's space, and is converted for returning to caller
418 	 */
419 	if (kd->argv == 0) {
420 		/*
421 		 * Try to avoid reallocs.
422 		 */
423 		kd->argc = MAX(narg + 1, 32);
424 		kd->argv = (char **)_kvm_malloc(kd, kd->argc *
425 						sizeof(*kd->argv));
426 		if (kd->argv == 0)
427 			return (0);
428 	} else if (narg + 1 > kd->argc) {
429 		kd->argc = MAX(2 * kd->argc, narg + 1);
430 		kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc *
431 						sizeof(*kd->argv));
432 		if (kd->argv == 0)
433 			return (0);
434 	}
435 	/*
436 	 * kd->argspc : returned to user, this is where the kd->argv
437 	 *              arrays are left pointing to the collected strings.
438 	 */
439 	if (kd->argspc == 0) {
440 		kd->argspc = (char *)_kvm_malloc(kd, PAGE_SIZE);
441 		if (kd->argspc == 0)
442 			return (0);
443 		kd->arglen = PAGE_SIZE;
444 	}
445 	/*
446 	 * kd->argbuf : used to pull in pages from the target process.
447 	 *              the strings are copied out of here.
448 	 */
449 	if (kd->argbuf == 0) {
450 		kd->argbuf = (char *)_kvm_malloc(kd, PAGE_SIZE);
451 		if (kd->argbuf == 0)
452 			return (0);
453 	}
454 
455 	/* Pull in the target process'es argv vector */
456 	cc = sizeof(char *) * narg;
457 	if (kvm_uread(kd, p, addr, (char *)kd->argv, cc) != cc)
458 		return (0);
459 	/*
460 	 * ap : saved start address of string we're working on in kd->argspc
461 	 * np : pointer to next place to write in kd->argspc
462 	 * len: length of data in kd->argspc
463 	 * argv: pointer to the argv vector that we are hunting around the
464 	 *       target process space for, and converting to addresses in
465 	 *       our address space (kd->argspc).
466 	 */
467 	ap = np = kd->argspc;
468 	argv = kd->argv;
469 	len = 0;
470 	/*
471 	 * Loop over pages, filling in the argument vector.
472 	 * Note that the argv strings could be pointing *anywhere* in
473 	 * the user address space and are no longer contiguous.
474 	 * Note that *argv is modified when we are going to fetch a string
475 	 * that crosses a page boundary.  We copy the next part of the string
476 	 * into to "np" and eventually convert the pointer.
477 	 */
478 	while (argv < kd->argv + narg && *argv != 0) {
479 
480 		/* get the address that the current argv string is on */
481 		addr = (u_long)*argv & ~(PAGE_SIZE - 1);
482 
483 		/* is it the same page as the last one? */
484 		if (addr != oaddr) {
485 			if (kvm_uread(kd, p, addr, kd->argbuf, PAGE_SIZE) !=
486 			    PAGE_SIZE)
487 				return (0);
488 			oaddr = addr;
489 		}
490 
491 		/* offset within the page... kd->argbuf */
492 		addr = (u_long)*argv & (PAGE_SIZE - 1);
493 
494 		/* cp = start of string, cc = count of chars in this chunk */
495 		cp = kd->argbuf + addr;
496 		cc = PAGE_SIZE - addr;
497 
498 		/* dont get more than asked for by user process */
499 		if (maxcnt > 0 && cc > maxcnt - len)
500 			cc = maxcnt - len;
501 
502 		/* pointer to end of string if we found it in this page */
503 		ep = memchr(cp, '\0', cc);
504 		if (ep != 0)
505 			cc = ep - cp + 1;
506 		/*
507 		 * at this point, cc is the count of the chars that we are
508 		 * going to retrieve this time. we may or may not have found
509 		 * the end of it.  (ep points to the null if the end is known)
510 		 */
511 
512 		/* will we exceed the malloc/realloced buffer? */
513 		if (len + cc > kd->arglen) {
514 			register int off;
515 			register char **pp;
516 			register char *op = kd->argspc;
517 
518 			kd->arglen *= 2;
519 			kd->argspc = (char *)_kvm_realloc(kd, kd->argspc,
520 							  kd->arglen);
521 			if (kd->argspc == 0)
522 				return (0);
523 			/*
524 			 * Adjust argv pointers in case realloc moved
525 			 * the string space.
526 			 */
527 			off = kd->argspc - op;
528 			for (pp = kd->argv; pp < argv; pp++)
529 				*pp += off;
530 			ap += off;
531 			np += off;
532 		}
533 		/* np = where to put the next part of the string in kd->argspc*/
534 		/* np is kinda redundant.. could use "kd->argspc + len" */
535 		memcpy(np, cp, cc);
536 		np += cc;	/* inc counters */
537 		len += cc;
538 
539 		/*
540 		 * if end of string found, set the *argv pointer to the
541 		 * saved beginning of string, and advance. argv points to
542 		 * somewhere in kd->argv..  This is initially relative
543 		 * to the target process, but when we close it off, we set
544 		 * it to point in our address space.
545 		 */
546 		if (ep != 0) {
547 			*argv++ = ap;
548 			ap = np;
549 		} else {
550 			/* update the address relative to the target process */
551 			*argv += cc;
552 		}
553 
554 		if (maxcnt > 0 && len >= maxcnt) {
555 			/*
556 			 * We're stopping prematurely.  Terminate the
557 			 * current string.
558 			 */
559 			if (ep == 0) {
560 				*np = '\0';
561 				*argv++ = ap;
562 			}
563 			break;
564 		}
565 	}
566 	/* Make sure argv is terminated. */
567 	*argv = 0;
568 	return (kd->argv);
569 }
570 
571 static void
572 ps_str_a(p, addr, n)
573 	struct ps_strings *p;
574 	u_long *addr;
575 	int *n;
576 {
577 	*addr = (u_long)p->ps_argvstr;
578 	*n = p->ps_nargvstr;
579 }
580 
581 static void
582 ps_str_e(p, addr, n)
583 	struct ps_strings *p;
584 	u_long *addr;
585 	int *n;
586 {
587 	*addr = (u_long)p->ps_envstr;
588 	*n = p->ps_nenvstr;
589 }
590 
591 /*
592  * Determine if the proc indicated by p is still active.
593  * This test is not 100% foolproof in theory, but chances of
594  * being wrong are very low.
595  */
596 static int
597 proc_verify(kd, kernp, p)
598 	kvm_t *kd;
599 	u_long kernp;
600 	const struct proc *p;
601 {
602 	struct kinfo_proc kp;
603 	int mib[4];
604 	size_t len;
605 
606 	mib[0] = CTL_KERN;
607 	mib[1] = KERN_PROC;
608 	mib[2] = KERN_PROC_PID;
609 	mib[3] = p->p_pid;
610 	len = sizeof(kp);
611 	if (sysctl(mib, 4, &kp, &len, NULL, 0) == -1)
612 		return (0);
613 	return (p->p_pid == kp.kp_proc.p_pid &&
614 	    (kp.kp_proc.p_stat != SZOMB || p->p_stat == SZOMB));
615 }
616 
617 static char **
618 kvm_doargv(kd, kp, nchr, info)
619 	kvm_t *kd;
620 	const struct kinfo_proc *kp;
621 	int nchr;
622 	void (*info)(struct ps_strings *, u_long *, int *);
623 {
624 	register const struct proc *p = &kp->kp_proc;
625 	register char **ap;
626 	u_long addr;
627 	int cnt;
628 	static struct ps_strings arginfo;
629 	static u_long ps_strings;
630 	size_t len;
631 
632 	if (ps_strings == NULL) {
633 		len = sizeof(ps_strings);
634 		if (sysctlbyname("kern.ps_strings", &ps_strings, &len, NULL,
635 		    0) == -1)
636 			ps_strings = PS_STRINGS;
637 	}
638 
639 	/*
640 	 * Pointers are stored at the top of the user stack.
641 	 */
642 	if (p->p_stat == SZOMB ||
643 	    kvm_uread(kd, p, ps_strings, (char *)&arginfo,
644 		      sizeof(arginfo)) != sizeof(arginfo))
645 		return (0);
646 
647 	(*info)(&arginfo, &addr, &cnt);
648 	if (cnt == 0)
649 		return (0);
650 	ap = kvm_argv(kd, p, addr, cnt, nchr);
651 	/*
652 	 * For live kernels, make sure this process didn't go away.
653 	 */
654 	if (ap != 0 && ISALIVE(kd) &&
655 	    !proc_verify(kd, (u_long)kp->kp_eproc.e_paddr, p))
656 		ap = 0;
657 	return (ap);
658 }
659 
660 /*
661  * Get the command args.  This code is now machine independent.
662  */
663 char **
664 kvm_getargv(kd, kp, nchr)
665 	kvm_t *kd;
666 	const struct kinfo_proc *kp;
667 	int nchr;
668 {
669 	int oid[4];
670 	int i;
671 	size_t bufsz;
672 	static unsigned long buflen;
673 	static char *buf, *p;
674 	static char **bufp;
675 	static int argc;
676 
677 	if (!ISALIVE(kd)) {
678 		_kvm_err(kd, kd->program,
679 		    "cannot read user space from dead kernel");
680 		return (0);
681 	}
682 
683 	if (!buflen) {
684 		bufsz = sizeof(buflen);
685 		i = sysctlbyname("kern.ps_arg_cache_limit",
686 		    &buflen, &bufsz, NULL, 0);
687 		if (i == -1) {
688 			buflen = 0;
689 		} else {
690 			buf = malloc(buflen);
691 			if (buf == NULL)
692 				buflen = 0;
693 			argc = 32;
694 			bufp = malloc(sizeof(char *) * argc);
695 		}
696 	}
697 	if (buf != NULL) {
698 		oid[0] = CTL_KERN;
699 		oid[1] = KERN_PROC;
700 		oid[2] = KERN_PROC_ARGS;
701 		oid[3] = kp->kp_proc.p_pid;
702 		bufsz = buflen;
703 		i = sysctl(oid, 4, buf, &bufsz, 0, 0);
704 		if (i == 0 && bufsz > 0) {
705 			i = 0;
706 			p = buf;
707 			do {
708 				bufp[i++] = p;
709 				p += strlen(p) + 1;
710 				if (i >= argc) {
711 					argc += argc;
712 					bufp = realloc(bufp,
713 					    sizeof(char *) * argc);
714 				}
715 			} while (p < buf + bufsz);
716 			bufp[i++] = 0;
717 			return (bufp);
718 		}
719 	}
720 	if (kp->kp_proc.p_flag & P_SYSTEM)
721 		return (NULL);
722 	return (kvm_doargv(kd, kp, nchr, ps_str_a));
723 }
724 
725 char **
726 kvm_getenvv(kd, kp, nchr)
727 	kvm_t *kd;
728 	const struct kinfo_proc *kp;
729 	int nchr;
730 {
731 	return (kvm_doargv(kd, kp, nchr, ps_str_e));
732 }
733 
734 /*
735  * Read from user space.  The user context is given by p.
736  */
737 ssize_t
738 kvm_uread(kd, p, uva, buf, len)
739 	kvm_t *kd;
740 	register const struct proc *p;
741 	register u_long uva;
742 	register char *buf;
743 	register size_t len;
744 {
745 	register char *cp;
746 	char procfile[MAXPATHLEN];
747 	ssize_t amount;
748 	int fd;
749 
750 	if (!ISALIVE(kd)) {
751 		_kvm_err(kd, kd->program,
752 		    "cannot read user space from dead kernel");
753 		return (0);
754 	}
755 
756 	sprintf(procfile, "/proc/%d/mem", p->p_pid);
757 	fd = open(procfile, O_RDONLY, 0);
758 	if (fd < 0) {
759 		_kvm_err(kd, kd->program, "cannot open %s", procfile);
760 		close(fd);
761 		return (0);
762 	}
763 
764 	cp = buf;
765 	while (len > 0) {
766 		errno = 0;
767 		if (lseek(fd, (off_t)uva, 0) == -1 && errno != 0) {
768 			_kvm_err(kd, kd->program, "invalid address (%x) in %s",
769 			    uva, procfile);
770 			break;
771 		}
772 		amount = read(fd, cp, len);
773 		if (amount < 0) {
774 			_kvm_syserr(kd, kd->program, "error reading %s",
775 			    procfile);
776 			break;
777 		}
778 		if (amount == 0) {
779 			_kvm_err(kd, kd->program, "EOF reading %s", procfile);
780 			break;
781 		}
782 		cp += amount;
783 		uva += amount;
784 		len -= amount;
785 	}
786 
787 	close(fd);
788 	return ((ssize_t)(cp - buf));
789 }
790