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