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