kvm_proc.c revision 1.34 1 /* $NetBSD: kvm_proc.c,v 1.34 2000/05/26 02:42:22 simonb 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.34 2000/05/26 02:42:22 simonb 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 kp2p->p_schedflags = kp->kp_proc.p_schedflags;
575
576 kp2p->p_uticks = kp->kp_proc.p_uticks;
577 kp2p->p_sticks = kp->kp_proc.p_sticks;
578 kp2p->p_iticks = kp->kp_proc.p_iticks;
579
580 kp2p->p_tracep = PTRTOINT64(kp->kp_proc.p_tracep);
581 kp2p->p_traceflag = kp->kp_proc.p_traceflag;
582
583 kp2p->p_holdcnt = kp->kp_proc.p_holdcnt;
584
585 memcpy(&kp2p->p_siglist, &kp->kp_proc.p_siglist, sizeof(ki_sigset_t));
586 memcpy(&kp2p->p_sigmask, &kp->kp_proc.p_sigmask, sizeof(ki_sigset_t));
587 memcpy(&kp2p->p_sigignore, &kp->kp_proc.p_sigignore, sizeof(ki_sigset_t));
588 memcpy(&kp2p->p_sigcatch, &kp->kp_proc.p_sigcatch, sizeof(ki_sigset_t));
589
590 kp2p->p_stat = kp->kp_proc.p_stat;
591 kp2p->p_priority = kp->kp_proc.p_priority;
592 kp2p->p_usrpri = kp->kp_proc.p_usrpri;
593 kp2p->p_nice = kp->kp_proc.p_nice;
594
595 kp2p->p_xstat = kp->kp_proc.p_xstat;
596 kp2p->p_acflag = kp->kp_proc.p_acflag;
597
598 strncpy(kp2p->p_comm, kp->kp_proc.p_comm,
599 MIN(sizeof(kp2p->p_comm), sizeof(kp->kp_proc.p_comm)));
600
601 strncpy(kp2p->p_wmesg, kp->kp_eproc.e_wmesg, sizeof(kp2p->p_wmesg));
602 kp2p->p_wchan = PTRTOINT64(kp->kp_proc.p_wchan);
603
604 strncpy(kp2p->p_login, kp->kp_eproc.e_login, sizeof(kp2p->p_login));
605
606 kp2p->p_vm_rssize = kp->kp_eproc.e_xrssize;
607 kp2p->p_vm_tsize = kp->kp_eproc.e_vm.vm_tsize;
608 kp2p->p_vm_dsize = kp->kp_eproc.e_vm.vm_dsize;
609 kp2p->p_vm_ssize = kp->kp_eproc.e_vm.vm_ssize;
610
611 kp2p->p_eflag = kp->kp_eproc.e_flag;
612
613 if (P_ZOMBIE(&kp->kp_proc) || kp->kp_proc.p_addr == NULL ||
614 KREAD(kd, (u_long)kp->kp_proc.p_addr, &user)) {
615 kp2p->p_uvalid = 0;
616 } else {
617 kp2p->p_uvalid = 1;
618
619 kp2p->p_ustart_sec = user.u_stats.p_start.tv_sec;
620 kp2p->p_ustart_usec = user.u_stats.p_start.tv_usec;
621
622 kp2p->p_uutime_sec = user.u_stats.p_ru.ru_utime.tv_sec;
623 kp2p->p_uutime_usec = user.u_stats.p_ru.ru_utime.tv_usec;
624 kp2p->p_ustime_sec = user.u_stats.p_ru.ru_stime.tv_sec;
625 kp2p->p_ustime_usec = user.u_stats.p_ru.ru_stime.tv_usec;
626
627 kp2p->p_uru_maxrss = user.u_stats.p_ru.ru_maxrss;
628 kp2p->p_uru_ixrss = user.u_stats.p_ru.ru_ixrss;
629 kp2p->p_uru_idrss = user.u_stats.p_ru.ru_idrss;
630 kp2p->p_uru_isrss = user.u_stats.p_ru.ru_isrss;
631 kp2p->p_uru_minflt = user.u_stats.p_ru.ru_minflt;
632 kp2p->p_uru_majflt = user.u_stats.p_ru.ru_majflt;
633 kp2p->p_uru_nswap = user.u_stats.p_ru.ru_nswap;
634 kp2p->p_uru_inblock = user.u_stats.p_ru.ru_inblock;
635 kp2p->p_uru_oublock = user.u_stats.p_ru.ru_oublock;
636 kp2p->p_uru_msgsnd = user.u_stats.p_ru.ru_msgsnd;
637 kp2p->p_uru_msgrcv = user.u_stats.p_ru.ru_msgrcv;
638 kp2p->p_uru_nsignals = user.u_stats.p_ru.ru_nsignals;
639 kp2p->p_uru_nvcsw = user.u_stats.p_ru.ru_nvcsw;
640 kp2p->p_uru_nivcsw = user.u_stats.p_ru.ru_nivcsw;
641
642 kp2p->p_uctime_sec = user.u_stats.p_cru.ru_utime.tv_sec +
643 user.u_stats.p_cru.ru_stime.tv_sec;
644 kp2p->p_uctime_usec = user.u_stats.p_cru.ru_utime.tv_usec +
645 user.u_stats.p_cru.ru_stime.tv_usec;
646 }
647
648 memcpy(kp2c, &kp2, esize);
649 kp2c += esize;
650 }
651
652 free(kd->procbase);
653 }
654 *cnt = nprocs;
655 return (kd->procbase2);
656 }
657
658 struct kinfo_proc *
659 kvm_getprocs(kd, op, arg, cnt)
660 kvm_t *kd;
661 int op, arg;
662 int *cnt;
663 {
664 size_t size;
665 int mib[4], st, nprocs;
666
667 if (kd->procbase != 0) {
668 free(kd->procbase);
669 /*
670 * Clear this pointer in case this call fails. Otherwise,
671 * kvm_close() will free it again.
672 */
673 kd->procbase = 0;
674 }
675 if (ISKMEM(kd)) {
676 size = 0;
677 mib[0] = CTL_KERN;
678 mib[1] = KERN_PROC;
679 mib[2] = op;
680 mib[3] = arg;
681 st = sysctl(mib, 4, NULL, &size, NULL, 0);
682 if (st == -1) {
683 _kvm_syserr(kd, kd->program, "kvm_getprocs");
684 return NULL;
685 }
686 kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
687 if (kd->procbase == 0)
688 return NULL;
689 st = sysctl(mib, 4, kd->procbase, &size, NULL, 0);
690 if (st == -1) {
691 _kvm_syserr(kd, kd->program, "kvm_getprocs");
692 return NULL;
693 }
694 if (size % sizeof(struct kinfo_proc) != 0) {
695 _kvm_err(kd, kd->program,
696 "proc size mismatch (%d total, %d chunks)",
697 size, sizeof(struct kinfo_proc));
698 return NULL;
699 }
700 nprocs = size / sizeof(struct kinfo_proc);
701 } else if (ISSYSCTL(kd)) {
702 _kvm_err(kd, kd->program, "kvm_open called with KVM_NO_FILES, "
703 "can't use kvm_getprocs");
704 return NULL;
705 } else {
706 struct nlist nl[5], *p;
707
708 nl[0].n_name = "_nprocs";
709 nl[1].n_name = "_allproc";
710 nl[2].n_name = "_deadproc";
711 nl[3].n_name = "_zombproc";
712 nl[4].n_name = 0;
713
714 if (kvm_nlist(kd, nl) != 0) {
715 for (p = nl; p->n_type != 0; ++p)
716 ;
717 _kvm_err(kd, kd->program,
718 "%s: no such symbol", p->n_name);
719 return NULL;
720 }
721 if (KREAD(kd, nl[0].n_value, &nprocs)) {
722 _kvm_err(kd, kd->program, "can't read nprocs");
723 return NULL;
724 }
725 size = nprocs * sizeof(struct kinfo_proc);
726 kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
727 if (kd->procbase == 0)
728 return NULL;
729
730 nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
731 nl[2].n_value, nl[3].n_value, nprocs);
732 if (nprocs < 0)
733 return NULL;
734 #ifdef notdef
735 size = nprocs * sizeof(struct kinfo_proc);
736 (void)realloc(kd->procbase, size);
737 #endif
738 }
739 *cnt = nprocs;
740 return (kd->procbase);
741 }
742
743 void
744 _kvm_freeprocs(kd)
745 kvm_t *kd;
746 {
747 if (kd->procbase) {
748 free(kd->procbase);
749 kd->procbase = 0;
750 }
751 }
752
753 void *
754 _kvm_realloc(kd, p, n)
755 kvm_t *kd;
756 void *p;
757 size_t n;
758 {
759 void *np = realloc(p, n);
760
761 if (np == 0)
762 _kvm_err(kd, kd->program, "out of memory");
763 return (np);
764 }
765
766 /*
767 * Read in an argument vector from the user address space of process p.
768 * addr if the user-space base address of narg null-terminated contiguous
769 * strings. This is used to read in both the command arguments and
770 * environment strings. Read at most maxcnt characters of strings.
771 */
772 static char **
773 kvm_argv(kd, p, addr, narg, maxcnt)
774 kvm_t *kd;
775 const struct miniproc *p;
776 u_long addr;
777 int narg;
778 int maxcnt;
779 {
780 char *np, *cp, *ep, *ap;
781 u_long oaddr = (u_long)~0L;
782 u_long len;
783 size_t cc;
784 char **argv;
785
786 /*
787 * Check that there aren't an unreasonable number of agruments,
788 * and that the address is in user space.
789 */
790 if (narg > ARG_MAX || addr < kd->min_uva || addr >= kd->max_uva)
791 return NULL;
792
793 if (kd->argv == 0) {
794 /*
795 * Try to avoid reallocs.
796 */
797 kd->argc = MAX(narg + 1, 32);
798 kd->argv = (char **)_kvm_malloc(kd, kd->argc *
799 sizeof(*kd->argv));
800 if (kd->argv == 0)
801 return NULL;
802 } else if (narg + 1 > kd->argc) {
803 kd->argc = MAX(2 * kd->argc, narg + 1);
804 kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc *
805 sizeof(*kd->argv));
806 if (kd->argv == 0)
807 return NULL;
808 }
809 if (kd->argspc == 0) {
810 kd->argspc = (char *)_kvm_malloc(kd, (size_t)kd->nbpg);
811 if (kd->argspc == 0)
812 return NULL;
813 kd->arglen = kd->nbpg;
814 }
815 if (kd->argbuf == 0) {
816 kd->argbuf = (char *)_kvm_malloc(kd, (size_t)kd->nbpg);
817 if (kd->argbuf == 0)
818 return NULL;
819 }
820 cc = sizeof(char *) * narg;
821 if (kvm_ureadm(kd, p, addr, (void *)kd->argv, cc) != cc)
822 return NULL;
823 ap = np = kd->argspc;
824 argv = kd->argv;
825 len = 0;
826 /*
827 * Loop over pages, filling in the argument vector.
828 */
829 while (argv < kd->argv + narg && *argv != 0) {
830 addr = (u_long)*argv & ~(kd->nbpg - 1);
831 if (addr != oaddr) {
832 if (kvm_ureadm(kd, p, addr, kd->argbuf,
833 (size_t)kd->nbpg) != kd->nbpg)
834 return NULL;
835 oaddr = addr;
836 }
837 addr = (u_long)*argv & (kd->nbpg - 1);
838 cp = kd->argbuf + (size_t)addr;
839 cc = kd->nbpg - (size_t)addr;
840 if (maxcnt > 0 && cc > (size_t)(maxcnt - len))
841 cc = (size_t)(maxcnt - len);
842 ep = memchr(cp, '\0', cc);
843 if (ep != 0)
844 cc = ep - cp + 1;
845 if (len + cc > kd->arglen) {
846 int off;
847 char **pp;
848 char *op = kd->argspc;
849
850 kd->arglen *= 2;
851 kd->argspc = (char *)_kvm_realloc(kd, kd->argspc,
852 (size_t)kd->arglen);
853 if (kd->argspc == 0)
854 return NULL;
855 /*
856 * Adjust argv pointers in case realloc moved
857 * the string space.
858 */
859 off = kd->argspc - op;
860 for (pp = kd->argv; pp < argv; pp++)
861 *pp += off;
862 ap += off;
863 np += off;
864 }
865 memcpy(np, cp, cc);
866 np += cc;
867 len += cc;
868 if (ep != 0) {
869 *argv++ = ap;
870 ap = np;
871 } else
872 *argv += cc;
873 if (maxcnt > 0 && len >= maxcnt) {
874 /*
875 * We're stopping prematurely. Terminate the
876 * current string.
877 */
878 if (ep == 0) {
879 *np = '\0';
880 *argv++ = ap;
881 }
882 break;
883 }
884 }
885 /* Make sure argv is terminated. */
886 *argv = 0;
887 return (kd->argv);
888 }
889
890 static void
891 ps_str_a(p, addr, n)
892 struct ps_strings *p;
893 u_long *addr;
894 int *n;
895 {
896 *addr = (u_long)p->ps_argvstr;
897 *n = p->ps_nargvstr;
898 }
899
900 static void
901 ps_str_e(p, addr, n)
902 struct ps_strings *p;
903 u_long *addr;
904 int *n;
905 {
906 *addr = (u_long)p->ps_envstr;
907 *n = p->ps_nenvstr;
908 }
909
910 /*
911 * Determine if the proc indicated by p is still active.
912 * This test is not 100% foolproof in theory, but chances of
913 * being wrong are very low.
914 */
915 static int
916 proc_verify(kd, kernp, p)
917 kvm_t *kd;
918 u_long kernp;
919 const struct miniproc *p;
920 {
921 struct proc kernproc;
922
923 /*
924 * Just read in the whole proc. It's not that big relative
925 * to the cost of the read system call.
926 */
927 if (kvm_read(kd, kernp, &kernproc, sizeof(kernproc)) !=
928 sizeof(kernproc))
929 return 0;
930 return (p->p_pid == kernproc.p_pid &&
931 (kernproc.p_stat != SZOMB || p->p_stat == SZOMB));
932 }
933
934 static char **
935 kvm_doargv(kd, p, nchr, info)
936 kvm_t *kd;
937 const struct miniproc *p;
938 int nchr;
939 void (*info)(struct ps_strings *, u_long *, int *);
940 {
941 char **ap;
942 u_long addr;
943 int cnt;
944 struct ps_strings arginfo;
945
946 /*
947 * Pointers are stored at the top of the user stack.
948 */
949 if (p->p_stat == SZOMB)
950 return NULL;
951 cnt = kvm_ureadm(kd, p, kd->usrstack - sizeof(arginfo),
952 (void *)&arginfo, sizeof(arginfo));
953 if (cnt != sizeof(arginfo))
954 return NULL;
955
956 (*info)(&arginfo, &addr, &cnt);
957 if (cnt == 0)
958 return NULL;
959 ap = kvm_argv(kd, p, addr, cnt, nchr);
960 /*
961 * For live kernels, make sure this process didn't go away.
962 */
963 if (ap != 0 && ISALIVE(kd) &&
964 !proc_verify(kd, (u_long)p->p_paddr, p))
965 ap = 0;
966 return (ap);
967 }
968
969 /*
970 * Get the command args. This code is now machine independent.
971 */
972 char **
973 kvm_getargv(kd, kp, nchr)
974 kvm_t *kd;
975 const struct kinfo_proc *kp;
976 int nchr;
977 {
978 struct miniproc p;
979
980 KPTOMINI(kp, &p);
981 return (kvm_doargv(kd, &p, nchr, ps_str_a));
982 }
983
984 char **
985 kvm_getenvv(kd, kp, nchr)
986 kvm_t *kd;
987 const struct kinfo_proc *kp;
988 int nchr;
989 {
990 struct miniproc p;
991
992 KPTOMINI(kp, &p);
993 return (kvm_doargv(kd, &p, nchr, ps_str_e));
994 }
995
996 static char **
997 kvm_doargv2(kd, pid, type, nchr)
998 kvm_t *kd;
999 pid_t pid;
1000 int type;
1001 int nchr;
1002 {
1003 size_t bufs;
1004 int narg, newarglen, mib[4];
1005 char **ap, *bp, *endp;
1006
1007 /*
1008 * Check that there aren't an unreasonable number of agruments.
1009 */
1010 if (nchr > ARG_MAX)
1011 return NULL;
1012
1013 if (nchr == 0)
1014 nchr = ARG_MAX;
1015
1016 /* Get number of strings in argv */
1017 mib[0] = CTL_KERN;
1018 mib[1] = KERN_PROC_ARGS;
1019 mib[2] = pid;
1020 mib[3] = type == KERN_PROC_ARGV ? KERN_PROC_NARGV : KERN_PROC_NENV;
1021 bufs = sizeof(narg);
1022 if (sysctl(mib, 4, &narg, &bufs, NULL, NULL) == -1)
1023 return NULL;
1024
1025 if (kd->argv == 0) {
1026 /*
1027 * Try to avoid reallocs.
1028 */
1029 kd->argc = MAX(narg + 1, 32);
1030 kd->argv = (char **)_kvm_malloc(kd, kd->argc *
1031 sizeof(*kd->argv));
1032 if (kd->argv == 0)
1033 return NULL;
1034 } else if (narg + 1 > kd->argc) {
1035 kd->argc = MAX(2 * kd->argc, narg + 1);
1036 kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc *
1037 sizeof(*kd->argv));
1038 if (kd->argv == 0)
1039 return NULL;
1040 }
1041
1042 newarglen = MIN(nchr, ARG_MAX);
1043 if (kd->arglen < newarglen) {
1044 if (kd->arglen == 0)
1045 kd->argspc = (char *)_kvm_malloc(kd, newarglen);
1046 else
1047 kd->argspc = (char *)_kvm_realloc(kd, kd->argspc,
1048 newarglen);
1049 if (kd->argspc == 0)
1050 return NULL;
1051 kd->arglen = newarglen;
1052 }
1053 memset(kd->argspc, 0, kd->arglen); /* XXX necessary? */
1054
1055 mib[0] = CTL_KERN;
1056 mib[1] = KERN_PROC_ARGS;
1057 mib[2] = pid;
1058 mib[3] = type;
1059 bufs = kd->arglen;
1060 if (sysctl(mib, 4, kd->argspc, &bufs, NULL, NULL) == -1)
1061 return NULL;
1062
1063 bp = kd->argspc;
1064 ap = kd->argv;
1065 endp = bp + MIN(nchr, bufs);
1066
1067 while (bp < endp) {
1068 *ap++ = bp;
1069 /* XXX: don't need following anymore, or stick check for max argc in above while loop? */
1070 if (ap >= kd->argv + kd->argc) {
1071 kd->argc *= 2;
1072 kd->argv = _kvm_realloc(kd, kd->argv,
1073 kd->argc * sizeof(*kd->argv));
1074 }
1075 bp += strlen(bp) + 1;
1076 }
1077 *ap = NULL;
1078
1079 return (kd->argv);
1080 }
1081
1082 char **
1083 kvm_getargv2(kd, kp, nchr)
1084 kvm_t *kd;
1085 const struct kinfo_proc2 *kp;
1086 int nchr;
1087 {
1088 return (kvm_doargv2(kd, kp->p_pid, KERN_PROC_ARGV, nchr));
1089 }
1090
1091 char **
1092 kvm_getenvv2(kd, kp, nchr)
1093 kvm_t *kd;
1094 const struct kinfo_proc2 *kp;
1095 int nchr;
1096 {
1097 return (kvm_doargv2(kd, kp->p_pid, KERN_PROC_ENV, nchr));
1098 }
1099
1100 /*
1101 * Read from user space. The user context is given by p.
1102 */
1103 static ssize_t
1104 kvm_ureadm(kd, p, uva, buf, len)
1105 kvm_t *kd;
1106 const struct miniproc *p;
1107 u_long uva;
1108 char *buf;
1109 size_t len;
1110 {
1111 char *cp;
1112
1113 cp = buf;
1114 while (len > 0) {
1115 size_t cc;
1116 char *dp;
1117 u_long cnt;
1118
1119 dp = _kvm_ureadm(kd, p, uva, &cnt);
1120 if (dp == 0) {
1121 _kvm_err(kd, 0, "invalid address (%x)", uva);
1122 return 0;
1123 }
1124 cc = (size_t)MIN(cnt, len);
1125 memcpy(cp, dp, cc);
1126 cp += cc;
1127 uva += cc;
1128 len -= cc;
1129 }
1130 return (ssize_t)(cp - buf);
1131 }
1132
1133 ssize_t
1134 kvm_uread(kd, p, uva, buf, len)
1135 kvm_t *kd;
1136 const struct proc *p;
1137 u_long uva;
1138 char *buf;
1139 size_t len;
1140 {
1141 struct miniproc mp;
1142
1143 PTOMINI(p, &mp);
1144 return (kvm_ureadm(kd, &mp, uva, buf, len));
1145 }
1146