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