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