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