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