kvm_proc.c revision 1.81 1 /* $NetBSD: kvm_proc.c,v 1.81 2008/12/28 19:49:26 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c) 1989, 1992, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * This code is derived from software developed by the Computer Systems
37 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
38 * BG 91-66 and contributed to Berkeley.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 */
64
65 #include <sys/cdefs.h>
66 #if defined(LIBC_SCCS) && !defined(lint)
67 #if 0
68 static char sccsid[] = "@(#)kvm_proc.c 8.3 (Berkeley) 9/23/93";
69 #else
70 __RCSID("$NetBSD: kvm_proc.c,v 1.81 2008/12/28 19:49:26 christos Exp $");
71 #endif
72 #endif /* LIBC_SCCS and not lint */
73
74 /*
75 * Proc traversal interface for kvm. ps and w are (probably) the exclusive
76 * users of this code, so we've factored it out into a separate module.
77 * Thus, we keep this grunge out of the other kvm applications (i.e.,
78 * most other applications are interested only in open/close/read/nlist).
79 */
80
81 #include <sys/param.h>
82 #include <sys/user.h>
83 #include <sys/lwp.h>
84 #include <sys/proc.h>
85 #include <sys/exec.h>
86 #include <sys/stat.h>
87 #include <sys/ioctl.h>
88 #include <sys/tty.h>
89 #include <sys/resourcevar.h>
90 #include <sys/mutex.h>
91 #include <sys/specificdata.h>
92
93 #include <errno.h>
94 #include <stdlib.h>
95 #include <stddef.h>
96 #include <string.h>
97 #include <unistd.h>
98 #include <nlist.h>
99 #include <kvm.h>
100
101 #include <uvm/uvm_extern.h>
102 #include <uvm/uvm_amap.h>
103
104 #include <sys/sysctl.h>
105
106 #include <limits.h>
107 #include <db.h>
108 #include <paths.h>
109
110 #include "kvm_private.h"
111
112 /*
113 * Common info from kinfo_proc and kinfo_proc2 used by helper routines.
114 */
115 struct miniproc {
116 struct vmspace *p_vmspace;
117 char p_stat;
118 struct proc *p_paddr;
119 pid_t p_pid;
120 };
121
122 /*
123 * Convert from struct proc and kinfo_proc{,2} to miniproc.
124 */
125 #define PTOMINI(kp, p) \
126 do { \
127 (p)->p_stat = (kp)->p_stat; \
128 (p)->p_pid = (kp)->p_pid; \
129 (p)->p_paddr = NULL; \
130 (p)->p_vmspace = (kp)->p_vmspace; \
131 } while (/*CONSTCOND*/0);
132
133 #define KPTOMINI(kp, p) \
134 do { \
135 (p)->p_stat = (kp)->kp_proc.p_stat; \
136 (p)->p_pid = (kp)->kp_proc.p_pid; \
137 (p)->p_paddr = (kp)->kp_eproc.e_paddr; \
138 (p)->p_vmspace = (kp)->kp_proc.p_vmspace; \
139 } while (/*CONSTCOND*/0);
140
141 #define KP2TOMINI(kp, p) \
142 do { \
143 (p)->p_stat = (kp)->p_stat; \
144 (p)->p_pid = (kp)->p_pid; \
145 (p)->p_paddr = (void *)(long)(kp)->p_paddr; \
146 (p)->p_vmspace = (void *)(long)(kp)->p_vmspace; \
147 } while (/*CONSTCOND*/0);
148
149 /*
150 * NetBSD uses kauth(9) to manage credentials, which are stored in kauth_cred_t,
151 * a kernel-only opaque type. This is an embedded version which is *INTERNAL* to
152 * kvm(3) so dumps can be read properly.
153 *
154 * Whenever NetBSD starts exporting credentials to userland consistently (using
155 * 'struct uucred', or something) this will have to be updated again.
156 */
157 struct kvm_kauth_cred {
158 u_int cr_refcnt; /* reference count */
159 uint8_t cr_pad[CACHE_LINE_SIZE - sizeof(u_int)];
160 uid_t cr_uid; /* user id */
161 uid_t cr_euid; /* effective user id */
162 uid_t cr_svuid; /* saved effective user id */
163 gid_t cr_gid; /* group id */
164 gid_t cr_egid; /* effective group id */
165 gid_t cr_svgid; /* saved effective group id */
166 u_int cr_ngroups; /* number of groups */
167 gid_t cr_groups[NGROUPS]; /* group memberships */
168 specificdata_reference cr_sd; /* specific data */
169 };
170
171 #define KREAD(kd, addr, obj) \
172 (kvm_read(kd, addr, (obj), sizeof(*obj)) != sizeof(*obj))
173
174 /* XXX: What uses these two functions? */
175 char *_kvm_uread __P((kvm_t *, const struct proc *, u_long,
176 u_long *));
177 ssize_t kvm_uread __P((kvm_t *, const struct proc *, u_long, char *,
178 size_t));
179
180 static char *_kvm_ureadm __P((kvm_t *, const struct miniproc *, u_long,
181 u_long *));
182 static ssize_t kvm_ureadm __P((kvm_t *, const struct miniproc *, u_long,
183 char *, size_t));
184
185 static char **kvm_argv __P((kvm_t *, const struct miniproc *, u_long, int,
186 int));
187 static int kvm_deadprocs __P((kvm_t *, int, int, u_long, u_long, int));
188 static char **kvm_doargv __P((kvm_t *, const struct miniproc *, int,
189 void (*)(struct ps_strings *, u_long *, int *)));
190 static char **kvm_doargv2 __P((kvm_t *, pid_t, int, int));
191 static int kvm_proclist __P((kvm_t *, int, int, struct proc *,
192 struct kinfo_proc *, int));
193 static int proc_verify __P((kvm_t *, u_long, const struct miniproc *));
194 static void ps_str_a __P((struct ps_strings *, u_long *, int *));
195 static void ps_str_e __P((struct ps_strings *, u_long *, int *));
196
197
198 static char *
199 _kvm_ureadm(kd, p, va, cnt)
200 kvm_t *kd;
201 const struct miniproc *p;
202 u_long va;
203 u_long *cnt;
204 {
205 u_long addr, head;
206 u_long offset;
207 struct vm_map_entry vme;
208 struct vm_amap amap;
209 struct vm_anon *anonp, anon;
210 struct vm_page pg;
211 u_long slot;
212
213 if (kd->swapspc == NULL) {
214 kd->swapspc = _kvm_malloc(kd, (size_t)kd->nbpg);
215 if (kd->swapspc == NULL)
216 return (NULL);
217 }
218
219 /*
220 * Look through the address map for the memory object
221 * that corresponds to the given virtual address.
222 * The header just has the entire valid range.
223 */
224 head = (u_long)&p->p_vmspace->vm_map.header;
225 addr = head;
226 for (;;) {
227 if (KREAD(kd, addr, &vme))
228 return (NULL);
229
230 if (va >= vme.start && va < vme.end &&
231 vme.aref.ar_amap != NULL)
232 break;
233
234 addr = (u_long)vme.next;
235 if (addr == head)
236 return (NULL);
237 }
238
239 /*
240 * we found the map entry, now to find the object...
241 */
242 if (vme.aref.ar_amap == NULL)
243 return (NULL);
244
245 addr = (u_long)vme.aref.ar_amap;
246 if (KREAD(kd, addr, &amap))
247 return (NULL);
248
249 offset = va - vme.start;
250 slot = offset / kd->nbpg + vme.aref.ar_pageoff;
251 /* sanity-check slot number */
252 if (slot > amap.am_nslot)
253 return (NULL);
254
255 addr = (u_long)amap.am_anon + (offset / kd->nbpg) * sizeof(anonp);
256 if (KREAD(kd, addr, &anonp))
257 return (NULL);
258
259 addr = (u_long)anonp;
260 if (KREAD(kd, addr, &anon))
261 return (NULL);
262
263 addr = (u_long)anon.an_page;
264 if (addr) {
265 if (KREAD(kd, addr, &pg))
266 return (NULL);
267
268 if (_kvm_pread(kd, kd->pmfd, kd->swapspc, (size_t)kd->nbpg,
269 (off_t)pg.phys_addr) != kd->nbpg)
270 return (NULL);
271 } else {
272 if (kd->swfd < 0 ||
273 _kvm_pread(kd, kd->swfd, kd->swapspc, (size_t)kd->nbpg,
274 (off_t)(anon.an_swslot * kd->nbpg)) != kd->nbpg)
275 return (NULL);
276 }
277
278 /* Found the page. */
279 offset %= kd->nbpg;
280 *cnt = kd->nbpg - offset;
281 return (&kd->swapspc[(size_t)offset]);
282 }
283
284 char *
285 _kvm_uread(kd, p, va, cnt)
286 kvm_t *kd;
287 const struct proc *p;
288 u_long va;
289 u_long *cnt;
290 {
291 struct miniproc mp;
292
293 PTOMINI(p, &mp);
294 return (_kvm_ureadm(kd, &mp, va, cnt));
295 }
296
297 /*
298 * Convert credentials located in kernel space address 'cred' and store
299 * them in the appropriate members of 'eproc'.
300 */
301 static int
302 _kvm_convertcred(kvm_t *kd, u_long cred, struct eproc *eproc)
303 {
304 struct kvm_kauth_cred kauthcred;
305 struct ki_pcred *pc = &eproc->e_pcred;
306 struct ki_ucred *uc = &eproc->e_ucred;
307
308 if (KREAD(kd, cred, &kauthcred) != 0)
309 return (-1);
310
311 /* inlined version of kauth_cred_to_pcred, see kauth(9). */
312 pc->p_ruid = kauthcred.cr_uid;
313 pc->p_svuid = kauthcred.cr_svuid;
314 pc->p_rgid = kauthcred.cr_gid;
315 pc->p_svgid = kauthcred.cr_svgid;
316 pc->p_refcnt = kauthcred.cr_refcnt;
317 pc->p_pad = NULL;
318
319 /* inlined version of kauth_cred_to_ucred(), see kauth(9). */
320 uc->cr_ref = kauthcred.cr_refcnt;
321 uc->cr_uid = kauthcred.cr_euid;
322 uc->cr_gid = kauthcred.cr_egid;
323 uc->cr_ngroups = (uint32_t)MIN(kauthcred.cr_ngroups,
324 sizeof(uc->cr_groups) / sizeof(uc->cr_groups[0]));
325 memcpy(uc->cr_groups, kauthcred.cr_groups,
326 uc->cr_ngroups * sizeof(uc->cr_groups[0]));
327
328 return (0);
329 }
330
331 /*
332 * Read proc's from memory file into buffer bp, which has space to hold
333 * at most maxcnt procs.
334 */
335 static int
336 kvm_proclist(kd, what, arg, p, bp, maxcnt)
337 kvm_t *kd;
338 int what, arg;
339 struct proc *p;
340 struct kinfo_proc *bp;
341 int maxcnt;
342 {
343 int cnt = 0;
344 int nlwps;
345 struct kinfo_lwp *kl;
346 struct eproc eproc;
347 struct pgrp pgrp;
348 struct session sess;
349 struct tty tty;
350 struct proc proc;
351
352 for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) {
353 if (KREAD(kd, (u_long)p, &proc)) {
354 _kvm_err(kd, kd->program, "can't read proc at %p", p);
355 return (-1);
356 }
357 if (_kvm_convertcred(kd, (u_long)proc.p_cred, &eproc) != 0) {
358 _kvm_err(kd, kd->program,
359 "can't read proc credentials at %p", p);
360 return (-1);
361 }
362
363 switch (what) {
364
365 case KERN_PROC_PID:
366 if (proc.p_pid != (pid_t)arg)
367 continue;
368 break;
369
370 case KERN_PROC_UID:
371 if (eproc.e_ucred.cr_uid != (uid_t)arg)
372 continue;
373 break;
374
375 case KERN_PROC_RUID:
376 if (eproc.e_pcred.p_ruid != (uid_t)arg)
377 continue;
378 break;
379 }
380 /*
381 * We're going to add another proc to the set. If this
382 * will overflow the buffer, assume the reason is because
383 * nprocs (or the proc list) is corrupt and declare an error.
384 */
385 if (cnt >= maxcnt) {
386 _kvm_err(kd, kd->program, "nprocs corrupt");
387 return (-1);
388 }
389 /*
390 * gather eproc
391 */
392 eproc.e_paddr = p;
393 if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
394 _kvm_err(kd, kd->program, "can't read pgrp at %p",
395 proc.p_pgrp);
396 return (-1);
397 }
398 eproc.e_sess = pgrp.pg_session;
399 eproc.e_pgid = pgrp.pg_id;
400 eproc.e_jobc = pgrp.pg_jobc;
401 if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
402 _kvm_err(kd, kd->program, "can't read session at %p",
403 pgrp.pg_session);
404 return (-1);
405 }
406 if ((proc.p_lflag & PL_CONTROLT) && sess.s_ttyp != NULL) {
407 if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
408 _kvm_err(kd, kd->program,
409 "can't read tty at %p", sess.s_ttyp);
410 return (-1);
411 }
412 eproc.e_tdev = (uint32_t)tty.t_dev;
413 eproc.e_tsess = tty.t_session;
414 if (tty.t_pgrp != NULL) {
415 if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
416 _kvm_err(kd, kd->program,
417 "can't read tpgrp at %p",
418 tty.t_pgrp);
419 return (-1);
420 }
421 eproc.e_tpgid = pgrp.pg_id;
422 } else
423 eproc.e_tpgid = -1;
424 } else
425 eproc.e_tdev = (uint32_t)NODEV;
426 eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0;
427 eproc.e_sid = sess.s_sid;
428 if (sess.s_leader == p)
429 eproc.e_flag |= EPROC_SLEADER;
430 /*
431 * Fill in the old-style proc.p_wmesg by copying the wmesg
432 * from the first available LWP.
433 */
434 kl = kvm_getlwps(kd, proc.p_pid,
435 (u_long)PTRTOUINT64(eproc.e_paddr),
436 sizeof(struct kinfo_lwp), &nlwps);
437 if (kl) {
438 if (nlwps > 0) {
439 strcpy(eproc.e_wmesg, kl[0].l_wmesg);
440 }
441 }
442 (void)kvm_read(kd, (u_long)proc.p_vmspace, &eproc.e_vm,
443 sizeof(eproc.e_vm));
444
445 eproc.e_xsize = eproc.e_xrssize = 0;
446 eproc.e_xccount = eproc.e_xswrss = 0;
447
448 switch (what) {
449
450 case KERN_PROC_PGRP:
451 if (eproc.e_pgid != (pid_t)arg)
452 continue;
453 break;
454
455 case KERN_PROC_TTY:
456 if ((proc.p_lflag & PL_CONTROLT) == 0 ||
457 eproc.e_tdev != (dev_t)arg)
458 continue;
459 break;
460 }
461 memcpy(&bp->kp_proc, &proc, sizeof(proc));
462 memcpy(&bp->kp_eproc, &eproc, sizeof(eproc));
463 ++bp;
464 ++cnt;
465 }
466 return (cnt);
467 }
468
469 /*
470 * Build proc info array by reading in proc list from a crash dump.
471 * Return number of procs read. maxcnt is the max we will read.
472 */
473 static int
474 kvm_deadprocs(kd, what, arg, a_allproc, a_zombproc, maxcnt)
475 kvm_t *kd;
476 int what, arg;
477 u_long a_allproc;
478 u_long a_zombproc;
479 int maxcnt;
480 {
481 struct kinfo_proc *bp = kd->procbase;
482 int acnt, zcnt;
483 struct proc *p;
484
485 if (KREAD(kd, a_allproc, &p)) {
486 _kvm_err(kd, kd->program, "cannot read allproc");
487 return (-1);
488 }
489 acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
490 if (acnt < 0)
491 return (acnt);
492
493 if (KREAD(kd, a_zombproc, &p)) {
494 _kvm_err(kd, kd->program, "cannot read zombproc");
495 return (-1);
496 }
497 zcnt = kvm_proclist(kd, what, arg, p, bp + acnt,
498 maxcnt - acnt);
499 if (zcnt < 0)
500 zcnt = 0;
501
502 return (acnt + zcnt);
503 }
504
505 struct kinfo_proc2 *
506 kvm_getproc2(kd, op, arg, esize, cnt)
507 kvm_t *kd;
508 int op, arg;
509 size_t esize;
510 int *cnt;
511 {
512 size_t size;
513 int mib[6], st, nprocs;
514 struct pstats pstats;
515
516 if (ISSYSCTL(kd)) {
517 size = 0;
518 mib[0] = CTL_KERN;
519 mib[1] = KERN_PROC2;
520 mib[2] = op;
521 mib[3] = arg;
522 mib[4] = (int)esize;
523 again:
524 mib[5] = 0;
525 st = sysctl(mib, 6, NULL, &size, NULL, (size_t)0);
526 if (st == -1) {
527 _kvm_syserr(kd, kd->program, "kvm_getproc2");
528 return (NULL);
529 }
530
531 mib[5] = (int) (size / esize);
532 KVM_ALLOC(kd, procbase2, size);
533 st = sysctl(mib, 6, kd->procbase2, &size, NULL, (size_t)0);
534 if (st == -1) {
535 if (errno == ENOMEM) {
536 goto again;
537 }
538 _kvm_syserr(kd, kd->program, "kvm_getproc2");
539 return (NULL);
540 }
541 nprocs = (int) (size / esize);
542 } else {
543 char *kp2c;
544 struct kinfo_proc *kp;
545 struct kinfo_proc2 kp2, *kp2p;
546 struct kinfo_lwp *kl;
547 int i, nlwps;
548
549 kp = kvm_getprocs(kd, op, arg, &nprocs);
550 if (kp == NULL)
551 return (NULL);
552
553 size = nprocs * esize;
554 KVM_ALLOC(kd, procbase2, size);
555 kp2c = (char *)(void *)kd->procbase2;
556 kp2p = &kp2;
557 for (i = 0; i < nprocs; i++, kp++) {
558 struct timeval tv;
559
560 kl = kvm_getlwps(kd, kp->kp_proc.p_pid,
561 (u_long)PTRTOUINT64(kp->kp_eproc.e_paddr),
562 sizeof(struct kinfo_lwp), &nlwps);
563
564 if (kl == NULL) {
565 _kvm_syserr(kd, NULL,
566 "kvm_getlwps() failed on process %u\n",
567 kp->kp_proc.p_pid);
568 if (nlwps == 0)
569 return NULL;
570 else
571 continue;
572 }
573
574 /* We use kl[0] as the "representative" LWP */
575 memset(kp2p, 0, sizeof(kp2));
576 kp2p->p_forw = kl[0].l_forw;
577 kp2p->p_back = kl[0].l_back;
578 kp2p->p_paddr = PTRTOUINT64(kp->kp_eproc.e_paddr);
579 kp2p->p_addr = kl[0].l_addr;
580 kp2p->p_fd = PTRTOUINT64(kp->kp_proc.p_fd);
581 kp2p->p_cwdi = PTRTOUINT64(kp->kp_proc.p_cwdi);
582 kp2p->p_stats = PTRTOUINT64(kp->kp_proc.p_stats);
583 kp2p->p_limit = PTRTOUINT64(kp->kp_proc.p_limit);
584 kp2p->p_vmspace = PTRTOUINT64(kp->kp_proc.p_vmspace);
585 kp2p->p_sigacts = PTRTOUINT64(kp->kp_proc.p_sigacts);
586 kp2p->p_sess = PTRTOUINT64(kp->kp_eproc.e_sess);
587 kp2p->p_tsess = 0;
588 #if 1 /* XXX: dsl - p_ru was only ever non-zero for zombies */
589 kp2p->p_ru = 0;
590 #else
591 kp2p->p_ru = PTRTOUINT64(pstats.p_ru);
592 #endif
593
594 kp2p->p_eflag = 0;
595 kp2p->p_exitsig = kp->kp_proc.p_exitsig;
596 kp2p->p_flag = kp->kp_proc.p_flag;
597
598 kp2p->p_pid = kp->kp_proc.p_pid;
599
600 kp2p->p_ppid = kp->kp_eproc.e_ppid;
601 kp2p->p_sid = kp->kp_eproc.e_sid;
602 kp2p->p__pgid = kp->kp_eproc.e_pgid;
603
604 kp2p->p_tpgid = -1 /* XXX NO_PGID! */;
605
606 kp2p->p_uid = kp->kp_eproc.e_ucred.cr_uid;
607 kp2p->p_ruid = kp->kp_eproc.e_pcred.p_ruid;
608 kp2p->p_svuid = kp->kp_eproc.e_pcred.p_svuid;
609 kp2p->p_gid = kp->kp_eproc.e_ucred.cr_gid;
610 kp2p->p_rgid = kp->kp_eproc.e_pcred.p_rgid;
611 kp2p->p_svgid = kp->kp_eproc.e_pcred.p_svgid;
612
613 /*CONSTCOND*/
614 memcpy(kp2p->p_groups, kp->kp_eproc.e_ucred.cr_groups,
615 MIN(sizeof(kp2p->p_groups),
616 sizeof(kp->kp_eproc.e_ucred.cr_groups)));
617 kp2p->p_ngroups = kp->kp_eproc.e_ucred.cr_ngroups;
618
619 kp2p->p_jobc = kp->kp_eproc.e_jobc;
620 kp2p->p_tdev = kp->kp_eproc.e_tdev;
621 kp2p->p_tpgid = kp->kp_eproc.e_tpgid;
622 kp2p->p_tsess = PTRTOUINT64(kp->kp_eproc.e_tsess);
623
624 kp2p->p_estcpu = 0;
625 bintime2timeval(&kp->kp_proc.p_rtime, &tv);
626 kp2p->p_rtime_sec = (uint32_t)tv.tv_sec;
627 kp2p->p_rtime_usec = (uint32_t)tv.tv_usec;
628 kp2p->p_cpticks = kl[0].l_cpticks;
629 kp2p->p_pctcpu = kp->kp_proc.p_pctcpu;
630 kp2p->p_swtime = kl[0].l_swtime;
631 kp2p->p_slptime = kl[0].l_slptime;
632 #if 0 /* XXX thorpej */
633 kp2p->p_schedflags = kp->kp_proc.p_schedflags;
634 #else
635 kp2p->p_schedflags = 0;
636 #endif
637
638 kp2p->p_uticks = kp->kp_proc.p_uticks;
639 kp2p->p_sticks = kp->kp_proc.p_sticks;
640 kp2p->p_iticks = kp->kp_proc.p_iticks;
641
642 kp2p->p_tracep = PTRTOUINT64(kp->kp_proc.p_tracep);
643 kp2p->p_traceflag = kp->kp_proc.p_traceflag;
644
645 kp2p->p_holdcnt = kl[0].l_holdcnt;
646
647 memcpy(&kp2p->p_siglist,
648 &kp->kp_proc.p_sigpend.sp_set,
649 sizeof(ki_sigset_t));
650 memset(&kp2p->p_sigmask, 0,
651 sizeof(ki_sigset_t));
652 memcpy(&kp2p->p_sigignore,
653 &kp->kp_proc.p_sigctx.ps_sigignore,
654 sizeof(ki_sigset_t));
655 memcpy(&kp2p->p_sigcatch,
656 &kp->kp_proc.p_sigctx.ps_sigcatch,
657 sizeof(ki_sigset_t));
658
659 kp2p->p_stat = kl[0].l_stat;
660 kp2p->p_priority = kl[0].l_priority;
661 kp2p->p_usrpri = kl[0].l_priority;
662 kp2p->p_nice = kp->kp_proc.p_nice;
663
664 kp2p->p_xstat = kp->kp_proc.p_xstat;
665 kp2p->p_acflag = kp->kp_proc.p_acflag;
666
667 /*CONSTCOND*/
668 strncpy(kp2p->p_comm, kp->kp_proc.p_comm,
669 MIN(sizeof(kp2p->p_comm),
670 sizeof(kp->kp_proc.p_comm)));
671
672 strncpy(kp2p->p_wmesg, kp->kp_eproc.e_wmesg,
673 sizeof(kp2p->p_wmesg));
674 kp2p->p_wchan = kl[0].l_wchan;
675 strncpy(kp2p->p_login, kp->kp_eproc.e_login,
676 sizeof(kp2p->p_login));
677
678 kp2p->p_vm_rssize = kp->kp_eproc.e_xrssize;
679 kp2p->p_vm_tsize = kp->kp_eproc.e_vm.vm_tsize;
680 kp2p->p_vm_dsize = kp->kp_eproc.e_vm.vm_dsize;
681 kp2p->p_vm_ssize = kp->kp_eproc.e_vm.vm_ssize;
682
683 kp2p->p_eflag = (int32_t)kp->kp_eproc.e_flag;
684
685 kp2p->p_realflag = kp->kp_proc.p_flag;
686 kp2p->p_nlwps = kp->kp_proc.p_nlwps;
687 kp2p->p_nrlwps = kp->kp_proc.p_nrlwps;
688 kp2p->p_realstat = kp->kp_proc.p_stat;
689
690 if (P_ZOMBIE(&kp->kp_proc) ||
691 kp->kp_proc.p_stats == NULL ||
692 KREAD(kd, (u_long)kp->kp_proc.p_stats, &pstats)) {
693 kp2p->p_uvalid = 0;
694 } else {
695 kp2p->p_uvalid = 1;
696
697 kp2p->p_ustart_sec = (u_int32_t)
698 pstats.p_start.tv_sec;
699 kp2p->p_ustart_usec = (u_int32_t)
700 pstats.p_start.tv_usec;
701
702 kp2p->p_uutime_sec = (u_int32_t)
703 pstats.p_ru.ru_utime.tv_sec;
704 kp2p->p_uutime_usec = (u_int32_t)
705 pstats.p_ru.ru_utime.tv_usec;
706 kp2p->p_ustime_sec = (u_int32_t)
707 pstats.p_ru.ru_stime.tv_sec;
708 kp2p->p_ustime_usec = (u_int32_t)
709 pstats.p_ru.ru_stime.tv_usec;
710
711 kp2p->p_uru_maxrss = pstats.p_ru.ru_maxrss;
712 kp2p->p_uru_ixrss = pstats.p_ru.ru_ixrss;
713 kp2p->p_uru_idrss = pstats.p_ru.ru_idrss;
714 kp2p->p_uru_isrss = pstats.p_ru.ru_isrss;
715 kp2p->p_uru_minflt = pstats.p_ru.ru_minflt;
716 kp2p->p_uru_majflt = pstats.p_ru.ru_majflt;
717 kp2p->p_uru_nswap = pstats.p_ru.ru_nswap;
718 kp2p->p_uru_inblock = pstats.p_ru.ru_inblock;
719 kp2p->p_uru_oublock = pstats.p_ru.ru_oublock;
720 kp2p->p_uru_msgsnd = pstats.p_ru.ru_msgsnd;
721 kp2p->p_uru_msgrcv = pstats.p_ru.ru_msgrcv;
722 kp2p->p_uru_nsignals = pstats.p_ru.ru_nsignals;
723 kp2p->p_uru_nvcsw = pstats.p_ru.ru_nvcsw;
724 kp2p->p_uru_nivcsw = pstats.p_ru.ru_nivcsw;
725
726 kp2p->p_uctime_sec = (u_int32_t)
727 (pstats.p_cru.ru_utime.tv_sec +
728 pstats.p_cru.ru_stime.tv_sec);
729 kp2p->p_uctime_usec = (u_int32_t)
730 (pstats.p_cru.ru_utime.tv_usec +
731 pstats.p_cru.ru_stime.tv_usec);
732 }
733
734 memcpy(kp2c, &kp2, esize);
735 kp2c += esize;
736 }
737 }
738 *cnt = nprocs;
739 return (kd->procbase2);
740 }
741
742 struct kinfo_lwp *
743 kvm_getlwps(kd, pid, paddr, esize, cnt)
744 kvm_t *kd;
745 int pid;
746 u_long paddr;
747 size_t esize;
748 int *cnt;
749 {
750 size_t size;
751 int mib[5], nlwps;
752 ssize_t st;
753 struct kinfo_lwp *kl;
754
755 if (ISSYSCTL(kd)) {
756 size = 0;
757 mib[0] = CTL_KERN;
758 mib[1] = KERN_LWP;
759 mib[2] = pid;
760 mib[3] = (int)esize;
761 mib[4] = 0;
762 again:
763 st = sysctl(mib, 5, NULL, &size, NULL, (size_t)0);
764 if (st == -1) {
765 switch (errno) {
766 case ESRCH: /* Treat this as a soft error; see kvm.c */
767 _kvm_syserr(kd, NULL, "kvm_getlwps");
768 return NULL;
769 default:
770 _kvm_syserr(kd, kd->program, "kvm_getlwps");
771 return NULL;
772 }
773 }
774 mib[4] = (int) (size / esize);
775 KVM_ALLOC(kd, lwpbase, size);
776 st = sysctl(mib, 5, kd->lwpbase, &size, NULL, (size_t)0);
777 if (st == -1) {
778 switch (errno) {
779 case ESRCH: /* Treat this as a soft error; see kvm.c */
780 _kvm_syserr(kd, NULL, "kvm_getlwps");
781 return NULL;
782 case ENOMEM:
783 goto again;
784 default:
785 _kvm_syserr(kd, kd->program, "kvm_getlwps");
786 return NULL;
787 }
788 }
789 nlwps = (int) (size / esize);
790 } else {
791 /* grovel through the memory image */
792 struct proc p;
793 struct lwp l;
794 u_long laddr;
795 void *back;
796 int i;
797
798 st = kvm_read(kd, paddr, &p, sizeof(p));
799 if (st == -1) {
800 _kvm_syserr(kd, kd->program, "kvm_getlwps");
801 return (NULL);
802 }
803
804 nlwps = p.p_nlwps;
805 size = nlwps * sizeof(*kd->lwpbase);
806 KVM_ALLOC(kd, lwpbase, size);
807 laddr = (u_long)PTRTOUINT64(p.p_lwps.lh_first);
808 for (i = 0; (i < nlwps) && (laddr != 0); i++) {
809 st = kvm_read(kd, laddr, &l, sizeof(l));
810 if (st == -1) {
811 _kvm_syserr(kd, kd->program, "kvm_getlwps");
812 return (NULL);
813 }
814 kl = &kd->lwpbase[i];
815 kl->l_laddr = laddr;
816 kl->l_forw = PTRTOUINT64(l.l_runq.tqe_next);
817 laddr = (u_long)PTRTOUINT64(l.l_runq.tqe_prev);
818 st = kvm_read(kd, laddr, &back, sizeof(back));
819 if (st == -1) {
820 _kvm_syserr(kd, kd->program, "kvm_getlwps");
821 return (NULL);
822 }
823 kl->l_back = PTRTOUINT64(back);
824 kl->l_addr = PTRTOUINT64(l.l_addr);
825 kl->l_lid = l.l_lid;
826 kl->l_flag = l.l_flag;
827 kl->l_swtime = l.l_swtime;
828 kl->l_slptime = l.l_slptime;
829 kl->l_schedflags = 0; /* XXX */
830 kl->l_holdcnt = l.l_holdcnt;
831 kl->l_priority = l.l_priority;
832 kl->l_usrpri = l.l_priority;
833 kl->l_stat = l.l_stat;
834 kl->l_wchan = PTRTOUINT64(l.l_wchan);
835 if (l.l_wmesg)
836 (void)kvm_read(kd, (u_long)l.l_wmesg,
837 kl->l_wmesg, (size_t)WMESGLEN);
838 kl->l_cpuid = KI_NOCPU;
839 laddr = (u_long)PTRTOUINT64(l.l_sibling.le_next);
840 }
841 }
842
843 *cnt = nlwps;
844 return (kd->lwpbase);
845 }
846
847 struct kinfo_proc *
848 kvm_getprocs(kd, op, arg, cnt)
849 kvm_t *kd;
850 int op, arg;
851 int *cnt;
852 {
853 size_t size;
854 int mib[4], st, nprocs;
855
856 if (ISKMEM(kd)) {
857 size = 0;
858 mib[0] = CTL_KERN;
859 mib[1] = KERN_PROC;
860 mib[2] = op;
861 mib[3] = arg;
862 st = sysctl(mib, 4, NULL, &size, NULL, (size_t)0);
863 if (st == -1) {
864 _kvm_syserr(kd, kd->program, "kvm_getprocs");
865 return (NULL);
866 }
867 KVM_ALLOC(kd, procbase, size);
868 st = sysctl(mib, 4, kd->procbase, &size, NULL, (size_t)0);
869 if (st == -1) {
870 _kvm_syserr(kd, kd->program, "kvm_getprocs");
871 return (NULL);
872 }
873 if (size % sizeof(struct kinfo_proc) != 0) {
874 _kvm_err(kd, kd->program,
875 "proc size mismatch (%lu total, %lu chunks)",
876 (u_long)size, (u_long)sizeof(struct kinfo_proc));
877 return (NULL);
878 }
879 nprocs = (int) (size / sizeof(struct kinfo_proc));
880 } else if (ISSYSCTL(kd)) {
881 _kvm_err(kd, kd->program, "kvm_open called with KVM_NO_FILES, "
882 "can't use kvm_getprocs");
883 return (NULL);
884 } else {
885 struct nlist nl[4], *p;
886
887 (void)memset(nl, 0, sizeof(nl));
888 nl[0].n_name = "_nprocs";
889 nl[1].n_name = "_allproc";
890 nl[2].n_name = "_zombproc";
891 nl[3].n_name = NULL;
892
893 if (kvm_nlist(kd, nl) != 0) {
894 for (p = nl; p->n_type != 0; ++p)
895 continue;
896 _kvm_err(kd, kd->program,
897 "%s: no such symbol", p->n_name);
898 return (NULL);
899 }
900 if (KREAD(kd, nl[0].n_value, &nprocs)) {
901 _kvm_err(kd, kd->program, "can't read nprocs");
902 return (NULL);
903 }
904 size = nprocs * sizeof(*kd->procbase);
905 KVM_ALLOC(kd, procbase, size);
906 nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
907 nl[2].n_value, nprocs);
908 if (nprocs < 0)
909 return (NULL);
910 #ifdef notdef
911 size = nprocs * sizeof(struct kinfo_proc);
912 (void)realloc(kd->procbase, size);
913 #endif
914 }
915 *cnt = nprocs;
916 return (kd->procbase);
917 }
918
919 void *
920 _kvm_realloc(kd, p, n)
921 kvm_t *kd;
922 void *p;
923 size_t n;
924 {
925 void *np = realloc(p, n);
926
927 if (np == NULL)
928 _kvm_err(kd, kd->program, "out of memory");
929 return (np);
930 }
931
932 /*
933 * Read in an argument vector from the user address space of process p.
934 * addr if the user-space base address of narg null-terminated contiguous
935 * strings. This is used to read in both the command arguments and
936 * environment strings. Read at most maxcnt characters of strings.
937 */
938 static char **
939 kvm_argv(kd, p, addr, narg, maxcnt)
940 kvm_t *kd;
941 const struct miniproc *p;
942 u_long addr;
943 int narg;
944 int maxcnt;
945 {
946 char *np, *cp, *ep, *ap;
947 u_long oaddr = (u_long)~0L;
948 u_long len;
949 size_t cc;
950 char **argv;
951
952 /*
953 * Check that there aren't an unreasonable number of arguments,
954 * and that the address is in user space.
955 */
956 if (narg > ARG_MAX || addr < kd->min_uva || addr >= kd->max_uva)
957 return (NULL);
958
959 if (kd->argv == NULL) {
960 /*
961 * Try to avoid reallocs.
962 */
963 kd->argc = MAX(narg + 1, 32);
964 kd->argv = _kvm_malloc(kd, kd->argc * sizeof(*kd->argv));
965 if (kd->argv == NULL)
966 return (NULL);
967 } else if (narg + 1 > kd->argc) {
968 kd->argc = MAX(2 * kd->argc, narg + 1);
969 kd->argv = _kvm_realloc(kd, kd->argv, kd->argc *
970 sizeof(*kd->argv));
971 if (kd->argv == NULL)
972 return (NULL);
973 }
974 if (kd->argspc == NULL) {
975 kd->argspc = _kvm_malloc(kd, (size_t)kd->nbpg);
976 if (kd->argspc == NULL)
977 return (NULL);
978 kd->argspc_len = kd->nbpg;
979 }
980 if (kd->argbuf == NULL) {
981 kd->argbuf = _kvm_malloc(kd, (size_t)kd->nbpg);
982 if (kd->argbuf == NULL)
983 return (NULL);
984 }
985 cc = sizeof(char *) * narg;
986 if (kvm_ureadm(kd, p, addr, (void *)kd->argv, cc) != cc)
987 return (NULL);
988 ap = np = kd->argspc;
989 argv = kd->argv;
990 len = 0;
991 /*
992 * Loop over pages, filling in the argument vector.
993 */
994 while (argv < kd->argv + narg && *argv != NULL) {
995 addr = (u_long)*argv & ~(kd->nbpg - 1);
996 if (addr != oaddr) {
997 if (kvm_ureadm(kd, p, addr, kd->argbuf,
998 (size_t)kd->nbpg) != kd->nbpg)
999 return (NULL);
1000 oaddr = addr;
1001 }
1002 addr = (u_long)*argv & (kd->nbpg - 1);
1003 cp = kd->argbuf + (size_t)addr;
1004 cc = kd->nbpg - (size_t)addr;
1005 if (maxcnt > 0 && cc > (size_t)(maxcnt - len))
1006 cc = (size_t)(maxcnt - len);
1007 ep = memchr(cp, '\0', cc);
1008 if (ep != NULL)
1009 cc = ep - cp + 1;
1010 if (len + cc > kd->argspc_len) {
1011 ptrdiff_t off;
1012 char **pp;
1013 char *op = kd->argspc;
1014
1015 kd->argspc_len *= 2;
1016 kd->argspc = _kvm_realloc(kd, kd->argspc,
1017 kd->argspc_len);
1018 if (kd->argspc == NULL)
1019 return (NULL);
1020 /*
1021 * Adjust argv pointers in case realloc moved
1022 * the string space.
1023 */
1024 off = kd->argspc - op;
1025 for (pp = kd->argv; pp < argv; pp++)
1026 *pp += off;
1027 ap += off;
1028 np += off;
1029 }
1030 memcpy(np, cp, cc);
1031 np += cc;
1032 len += cc;
1033 if (ep != NULL) {
1034 *argv++ = ap;
1035 ap = np;
1036 } else
1037 *argv += cc;
1038 if (maxcnt > 0 && len >= maxcnt) {
1039 /*
1040 * We're stopping prematurely. Terminate the
1041 * current string.
1042 */
1043 if (ep == NULL) {
1044 *np = '\0';
1045 *argv++ = ap;
1046 }
1047 break;
1048 }
1049 }
1050 /* Make sure argv is terminated. */
1051 *argv = NULL;
1052 return (kd->argv);
1053 }
1054
1055 static void
1056 ps_str_a(p, addr, n)
1057 struct ps_strings *p;
1058 u_long *addr;
1059 int *n;
1060 {
1061
1062 *addr = (u_long)p->ps_argvstr;
1063 *n = p->ps_nargvstr;
1064 }
1065
1066 static void
1067 ps_str_e(p, addr, n)
1068 struct ps_strings *p;
1069 u_long *addr;
1070 int *n;
1071 {
1072
1073 *addr = (u_long)p->ps_envstr;
1074 *n = p->ps_nenvstr;
1075 }
1076
1077 /*
1078 * Determine if the proc indicated by p is still active.
1079 * This test is not 100% foolproof in theory, but chances of
1080 * being wrong are very low.
1081 */
1082 static int
1083 proc_verify(kd, kernp, p)
1084 kvm_t *kd;
1085 u_long kernp;
1086 const struct miniproc *p;
1087 {
1088 struct proc kernproc;
1089
1090 /*
1091 * Just read in the whole proc. It's not that big relative
1092 * to the cost of the read system call.
1093 */
1094 if (kvm_read(kd, kernp, &kernproc, sizeof(kernproc)) !=
1095 sizeof(kernproc))
1096 return (0);
1097 return (p->p_pid == kernproc.p_pid &&
1098 (kernproc.p_stat != SZOMB || p->p_stat == SZOMB));
1099 }
1100
1101 static char **
1102 kvm_doargv(kd, p, nchr, info)
1103 kvm_t *kd;
1104 const struct miniproc *p;
1105 int nchr;
1106 void (*info)(struct ps_strings *, u_long *, int *);
1107 {
1108 char **ap;
1109 u_long addr;
1110 int cnt;
1111 struct ps_strings arginfo;
1112
1113 /*
1114 * Pointers are stored at the top of the user stack.
1115 */
1116 if (p->p_stat == SZOMB)
1117 return (NULL);
1118 cnt = (int)kvm_ureadm(kd, p, kd->usrstack - sizeof(arginfo),
1119 (void *)&arginfo, sizeof(arginfo));
1120 if (cnt != sizeof(arginfo))
1121 return (NULL);
1122
1123 (*info)(&arginfo, &addr, &cnt);
1124 if (cnt == 0)
1125 return (NULL);
1126 ap = kvm_argv(kd, p, addr, cnt, nchr);
1127 /*
1128 * For live kernels, make sure this process didn't go away.
1129 */
1130 if (ap != NULL && ISALIVE(kd) &&
1131 !proc_verify(kd, (u_long)p->p_paddr, p))
1132 ap = NULL;
1133 return (ap);
1134 }
1135
1136 /*
1137 * Get the command args. This code is now machine independent.
1138 */
1139 char **
1140 kvm_getargv(kd, kp, nchr)
1141 kvm_t *kd;
1142 const struct kinfo_proc *kp;
1143 int nchr;
1144 {
1145 struct miniproc p;
1146
1147 KPTOMINI(kp, &p);
1148 return (kvm_doargv(kd, &p, nchr, ps_str_a));
1149 }
1150
1151 char **
1152 kvm_getenvv(kd, kp, nchr)
1153 kvm_t *kd;
1154 const struct kinfo_proc *kp;
1155 int nchr;
1156 {
1157 struct miniproc p;
1158
1159 KPTOMINI(kp, &p);
1160 return (kvm_doargv(kd, &p, nchr, ps_str_e));
1161 }
1162
1163 static char **
1164 kvm_doargv2(kd, pid, type, nchr)
1165 kvm_t *kd;
1166 pid_t pid;
1167 int type;
1168 int nchr;
1169 {
1170 size_t bufs;
1171 int narg, mib[4];
1172 size_t newargspc_len;
1173 char **ap, *bp, *endp;
1174
1175 /*
1176 * Check that there aren't an unreasonable number of arguments.
1177 */
1178 if (nchr > ARG_MAX)
1179 return (NULL);
1180
1181 if (nchr == 0)
1182 nchr = ARG_MAX;
1183
1184 /* Get number of strings in argv */
1185 mib[0] = CTL_KERN;
1186 mib[1] = KERN_PROC_ARGS;
1187 mib[2] = pid;
1188 mib[3] = type == KERN_PROC_ARGV ? KERN_PROC_NARGV : KERN_PROC_NENV;
1189 bufs = sizeof(narg);
1190 if (sysctl(mib, 4, &narg, &bufs, NULL, (size_t)0) == -1)
1191 return (NULL);
1192
1193 if (kd->argv == NULL) {
1194 /*
1195 * Try to avoid reallocs.
1196 */
1197 kd->argc = MAX(narg + 1, 32);
1198 kd->argv = _kvm_malloc(kd, kd->argc * sizeof(*kd->argv));
1199 if (kd->argv == NULL)
1200 return (NULL);
1201 } else if (narg + 1 > kd->argc) {
1202 kd->argc = MAX(2 * kd->argc, narg + 1);
1203 kd->argv = _kvm_realloc(kd, kd->argv, kd->argc *
1204 sizeof(*kd->argv));
1205 if (kd->argv == NULL)
1206 return (NULL);
1207 }
1208
1209 newargspc_len = MIN(nchr, ARG_MAX);
1210 KVM_ALLOC(kd, argspc, newargspc_len);
1211 memset(kd->argspc, 0, (size_t)kd->argspc_len); /* XXX necessary? */
1212
1213 mib[0] = CTL_KERN;
1214 mib[1] = KERN_PROC_ARGS;
1215 mib[2] = pid;
1216 mib[3] = type;
1217 bufs = kd->argspc_len;
1218 if (sysctl(mib, 4, kd->argspc, &bufs, NULL, (size_t)0) == -1)
1219 return (NULL);
1220
1221 bp = kd->argspc;
1222 bp[kd->argspc_len-1] = '\0'; /* make sure the string ends with nul */
1223 ap = kd->argv;
1224 endp = bp + MIN(nchr, bufs);
1225
1226 while (bp < endp) {
1227 *ap++ = bp;
1228 /*
1229 * XXX: don't need following anymore, or stick check
1230 * for max argc in above while loop?
1231 */
1232 if (ap >= kd->argv + kd->argc) {
1233 kd->argc *= 2;
1234 kd->argv = _kvm_realloc(kd, kd->argv,
1235 kd->argc * sizeof(*kd->argv));
1236 ap = kd->argv;
1237 }
1238 bp += strlen(bp) + 1;
1239 }
1240 *ap = NULL;
1241
1242 return (kd->argv);
1243 }
1244
1245 char **
1246 kvm_getargv2(kd, kp, nchr)
1247 kvm_t *kd;
1248 const struct kinfo_proc2 *kp;
1249 int nchr;
1250 {
1251
1252 return (kvm_doargv2(kd, kp->p_pid, KERN_PROC_ARGV, nchr));
1253 }
1254
1255 char **
1256 kvm_getenvv2(kd, kp, nchr)
1257 kvm_t *kd;
1258 const struct kinfo_proc2 *kp;
1259 int nchr;
1260 {
1261
1262 return (kvm_doargv2(kd, kp->p_pid, KERN_PROC_ENV, nchr));
1263 }
1264
1265 /*
1266 * Read from user space. The user context is given by p.
1267 */
1268 static ssize_t
1269 kvm_ureadm(kd, p, uva, buf, len)
1270 kvm_t *kd;
1271 const struct miniproc *p;
1272 u_long uva;
1273 char *buf;
1274 size_t len;
1275 {
1276 char *cp;
1277
1278 cp = buf;
1279 while (len > 0) {
1280 size_t cc;
1281 char *dp;
1282 u_long cnt;
1283
1284 dp = _kvm_ureadm(kd, p, uva, &cnt);
1285 if (dp == NULL) {
1286 _kvm_err(kd, 0, "invalid address (%lx)", uva);
1287 return (0);
1288 }
1289 cc = (size_t)MIN(cnt, len);
1290 memcpy(cp, dp, cc);
1291 cp += cc;
1292 uva += cc;
1293 len -= cc;
1294 }
1295 return (ssize_t)(cp - buf);
1296 }
1297
1298 ssize_t
1299 kvm_uread(kd, p, uva, buf, len)
1300 kvm_t *kd;
1301 const struct proc *p;
1302 u_long uva;
1303 char *buf;
1304 size_t len;
1305 {
1306 struct miniproc mp;
1307
1308 PTOMINI(p, &mp);
1309 return (kvm_ureadm(kd, &mp, uva, buf, len));
1310 }
1311