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