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