kvm_proc.c revision 1.27 1 /* $NetBSD: kvm_proc.c,v 1.27 1998/09/09 00:31:25 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*-
40 * Copyright (c) 1989, 1992, 1993
41 * The Regents of the University of California. All rights reserved.
42 *
43 * This code is derived from software developed by the Computer Systems
44 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
45 * BG 91-66 and contributed to Berkeley.
46 *
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. All advertising materials mentioning features or use of this software
56 * must display the following acknowledgement:
57 * This product includes software developed by the University of
58 * California, Berkeley and its contributors.
59 * 4. Neither the name of the University nor the names of its contributors
60 * may be used to endorse or promote products derived from this software
61 * without specific prior written permission.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * SUCH DAMAGE.
74 */
75
76 #include <sys/cdefs.h>
77 #if defined(LIBC_SCCS) && !defined(lint)
78 #if 0
79 static char sccsid[] = "@(#)kvm_proc.c 8.3 (Berkeley) 9/23/93";
80 #else
81 __RCSID("$NetBSD: kvm_proc.c,v 1.27 1998/09/09 00:31:25 thorpej Exp $");
82 #endif
83 #endif /* LIBC_SCCS and not lint */
84
85 /*
86 * Proc traversal interface for kvm. ps and w are (probably) the exclusive
87 * users of this code, so we've factored it out into a separate module.
88 * Thus, we keep this grunge out of the other kvm applications (i.e.,
89 * most other applications are interested only in open/close/read/nlist).
90 */
91
92 #include <sys/param.h>
93 #include <sys/user.h>
94 #include <sys/proc.h>
95 #include <sys/exec.h>
96 #include <sys/stat.h>
97 #include <sys/ioctl.h>
98 #include <sys/tty.h>
99 #include <stdlib.h>
100 #include <string.h>
101 #include <unistd.h>
102 #include <nlist.h>
103 #include <kvm.h>
104
105 #include <vm/vm.h>
106 #include <vm/vm_param.h>
107 #include <vm/swap_pager.h>
108
109 #if defined(UVM)
110 #include <uvm/uvm_extern.h>
111 #endif
112
113 #include <sys/sysctl.h>
114
115 #include <limits.h>
116 #include <db.h>
117 #include <paths.h>
118
119 #include "kvm_private.h"
120
121 #define KREAD(kd, addr, obj) \
122 (kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj))
123
124 char *_kvm_uread __P((kvm_t *, const struct proc *, u_long, u_long *));
125 #if !defined(UVM)
126 int _kvm_coreinit __P((kvm_t *));
127 int _kvm_readfromcore __P((kvm_t *, u_long, u_long));
128 int _kvm_readfrompager __P((kvm_t *, struct vm_object *, u_long));
129 #endif
130 ssize_t kvm_uread __P((kvm_t *, const struct proc *, u_long, char *,
131 size_t));
132
133 static char **kvm_argv __P((kvm_t *, const struct proc *, u_long, int,
134 int));
135 static int kvm_deadprocs __P((kvm_t *, int, int, u_long, u_long, u_long,
136 int));
137 static char **kvm_doargv __P((kvm_t *, const struct kinfo_proc *, int,
138 void (*)(struct ps_strings *, u_long *, int *)));
139 static int kvm_proclist __P((kvm_t *, int, int, struct proc *,
140 struct kinfo_proc *, int));
141 static int proc_verify __P((kvm_t *, u_long, const struct proc *));
142 static void ps_str_a __P((struct ps_strings *, u_long *, int *));
143 static void ps_str_e __P((struct ps_strings *, u_long *, int *));
144
145 char *
146 _kvm_uread(kd, p, va, cnt)
147 kvm_t *kd;
148 const struct proc *p;
149 u_long va;
150 u_long *cnt;
151 {
152 u_long addr, head;
153 u_long offset;
154 struct vm_map_entry vme;
155 #if defined(UVM)
156 struct vm_amap amap;
157 struct vm_anon *anonp, anon;
158 struct vm_page pg;
159 int slot;
160 #else
161 struct vm_object vmo;
162 int rv;
163 #endif
164
165 if (kd->swapspc == 0) {
166 kd->swapspc = (char *)_kvm_malloc(kd, kd->nbpg);
167 if (kd->swapspc == 0)
168 return (0);
169 }
170
171 /*
172 * Look through the address map for the memory object
173 * that corresponds to the given virtual address.
174 * The header just has the entire valid range.
175 */
176 head = (u_long)&p->p_vmspace->vm_map.header;
177 addr = head;
178 while (1) {
179 if (KREAD(kd, addr, &vme))
180 return (0);
181
182 #if defined(UVM)
183 if (va >= vme.start && va < vme.end &&
184 vme.aref.ar_amap != NULL)
185 break;
186
187 #else
188 if (va >= vme.start && va < vme.end &&
189 vme.object.vm_object != 0)
190 break;
191 #endif
192
193 addr = (u_long)vme.next;
194 if (addr == head)
195 return (0);
196
197 }
198 #if defined(UVM)
199
200 /*
201 * we found the map entry, now to find the object...
202 */
203 if (vme.aref.ar_amap == NULL)
204 return NULL;
205
206 addr = (u_long)vme.aref.ar_amap;
207 if (KREAD(kd, addr, &amap))
208 return NULL;
209
210 offset = va - vme.start;
211 slot = offset / kd->nbpg + vme.aref.ar_slotoff;
212 /* sanity-check slot number */
213 if (slot > amap.am_nslot)
214 return NULL;
215
216 addr = (u_long)amap.am_anon + (offset / kd->nbpg) * sizeof(anonp);
217 if (KREAD(kd, addr, &anonp))
218 return NULL;
219
220 addr = (u_long)anonp;
221 if (KREAD(kd, addr, &anon))
222 return NULL;
223
224 addr = (u_long)anon.u.an_page;
225 if (addr) {
226 if (KREAD(kd, addr, &pg))
227 return NULL;
228
229 if (pread(kd->pmfd, kd->swapspc, kd->nbpg,
230 (off_t)pg.phys_addr) != kd->nbpg)
231 return NULL;
232 }
233 else {
234 if (pread(kd->swfd, kd->swapspc, kd->nbpg,
235 (off_t)(anon.an_swslot * kd->nbpg)) != kd->nbpg)
236 return NULL;
237 }
238 #else
239 /*
240 * We found the right object -- follow shadow links.
241 */
242 offset = va - vme.start + vme.offset;
243 addr = (u_long)vme.object.vm_object;
244
245 while (1) {
246 /* Try reading the page from core first. */
247 if ((rv = _kvm_readfromcore(kd, addr, offset)))
248 break;
249
250 if (KREAD(kd, addr, &vmo))
251 return (0);
252
253 /* If there is a pager here, see if it has the page. */
254 if (vmo.pager != 0 &&
255 (rv = _kvm_readfrompager(kd, &vmo, offset)))
256 break;
257
258 /* Move down the shadow chain. */
259 addr = (u_long)vmo.shadow;
260 if (addr == 0)
261 return (0);
262 offset += vmo.shadow_offset;
263 }
264
265 if (rv == -1)
266 return (0);
267 #endif
268
269 /* Found the page. */
270 offset %= kd->nbpg;
271 *cnt = kd->nbpg - offset;
272 return (&kd->swapspc[offset]);
273 }
274
275 #if !defined(UVM)
276
277 #define vm_page_hash(kd, object, offset) \
278 (((u_long)object + (u_long)(offset / kd->nbpg)) & kd->vm_page_hash_mask)
279
280 int
281 _kvm_coreinit(kd)
282 kvm_t *kd;
283 {
284 struct nlist nlist[3];
285
286 nlist[0].n_name = "_vm_page_buckets";
287 nlist[1].n_name = "_vm_page_hash_mask";
288 nlist[2].n_name = 0;
289 if (kvm_nlist(kd, nlist) != 0)
290 return (-1);
291
292 if (KREAD(kd, nlist[0].n_value, &kd->vm_page_buckets) ||
293 KREAD(kd, nlist[1].n_value, &kd->vm_page_hash_mask))
294 return (-1);
295
296 return (0);
297 }
298
299 int
300 _kvm_readfromcore(kd, object, offset)
301 kvm_t *kd;
302 u_long object, offset;
303 {
304 u_long addr;
305 struct pglist bucket;
306 struct vm_page mem;
307 off_t seekpoint;
308
309 if (kd->vm_page_buckets == 0 &&
310 _kvm_coreinit(kd))
311 return (-1);
312
313 addr = (u_long)&kd->vm_page_buckets[vm_page_hash(kd, object, offset)];
314 if (KREAD(kd, addr, &bucket))
315 return (-1);
316
317 addr = (u_long)bucket.tqh_first;
318 offset &= ~(kd->nbpg -1);
319 while (1) {
320 if (addr == 0)
321 return (0);
322
323 if (KREAD(kd, addr, &mem))
324 return (-1);
325
326 if ((u_long)mem.object == object &&
327 (u_long)mem.offset == offset)
328 break;
329
330 addr = (u_long)mem.hashq.tqe_next;
331 }
332
333 seekpoint = mem.phys_addr;
334
335 if (pread(kd->pmfd, kd->swapspc, kd->nbpg, seekpoint) != kd->nbpg)
336 return (-1);
337
338 return (1);
339 }
340
341 int
342 _kvm_readfrompager(kd, vmop, offset)
343 kvm_t *kd;
344 struct vm_object *vmop;
345 u_long offset;
346 {
347 u_long addr;
348 struct pager_struct pager;
349 struct swpager swap;
350 int ix;
351 struct swblock swb;
352 off_t seekpoint;
353
354 /* Read in the pager info and make sure it's a swap device. */
355 addr = (u_long)vmop->pager;
356 if (KREAD(kd, addr, &pager) || pager.pg_type != PG_SWAP)
357 return (-1);
358
359 /* Read in the swap_pager private data. */
360 addr = (u_long)pager.pg_data;
361 if (KREAD(kd, addr, &swap))
362 return (-1);
363
364 /*
365 * Calculate the paging offset, and make sure it's within the
366 * bounds of the pager.
367 */
368 offset += vmop->paging_offset;
369 ix = offset / dbtob(swap.sw_bsize);
370 #if 0
371 if (swap.sw_blocks == 0 || ix >= swap.sw_nblocks)
372 return (-1);
373 #else
374 if (swap.sw_blocks == 0 || ix >= swap.sw_nblocks) {
375 int i;
376 printf("BUG BUG BUG BUG:\n");
377 printf("object %p offset %lx pgoffset %lx ",
378 vmop, offset - vmop->paging_offset,
379 (u_long)vmop->paging_offset);
380 printf("pager %p swpager %p\n",
381 vmop->pager, pager.pg_data);
382 printf("osize %lx bsize %x blocks %p nblocks %x\n",
383 (u_long)swap.sw_osize, swap.sw_bsize, swap.sw_blocks,
384 swap.sw_nblocks);
385 for (i = 0; i < swap.sw_nblocks; i++) {
386 addr = (u_long)&swap.sw_blocks[i];
387 if (KREAD(kd, addr, &swb))
388 return (0);
389 printf("sw_blocks[%d]: block %x mask %x\n", i,
390 swb.swb_block, swb.swb_mask);
391 }
392 return (-1);
393 }
394 #endif
395
396 /* Read in the swap records. */
397 addr = (u_long)&swap.sw_blocks[ix];
398 if (KREAD(kd, addr, &swb))
399 return (-1);
400
401 /* Calculate offset within pager. */
402 offset %= dbtob(swap.sw_bsize);
403
404 /* Check that the page is actually present. */
405 if ((swb.swb_mask & (1 << (offset / kd->nbpg))) == 0)
406 return (0);
407
408 if (!ISALIVE(kd))
409 return (-1);
410
411 /* Calculate the physical address and read the page. */
412 seekpoint = dbtob(swb.swb_block) + (offset & ~(kd->nbpg -1));
413
414 if (pread(kd->swfd, kd->swapspc, kd->nbpg, seekpoint) != kd->nbpg)
415 return (-1);
416
417 return (1);
418 }
419 #endif /* !defined(UVM) */
420
421 /*
422 * Read proc's from memory file into buffer bp, which has space to hold
423 * at most maxcnt procs.
424 */
425 static int
426 kvm_proclist(kd, what, arg, p, bp, maxcnt)
427 kvm_t *kd;
428 int what, arg;
429 struct proc *p;
430 struct kinfo_proc *bp;
431 int maxcnt;
432 {
433 int cnt = 0;
434 struct eproc eproc;
435 struct pgrp pgrp;
436 struct session sess;
437 struct tty tty;
438 struct proc proc;
439
440 for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) {
441 if (KREAD(kd, (u_long)p, &proc)) {
442 _kvm_err(kd, kd->program, "can't read proc at %x", p);
443 return (-1);
444 }
445 if (KREAD(kd, (u_long)proc.p_cred, &eproc.e_pcred) == 0)
446 (void)KREAD(kd, (u_long)eproc.e_pcred.pc_ucred,
447 &eproc.e_ucred);
448
449 switch(what) {
450
451 case KERN_PROC_PID:
452 if (proc.p_pid != (pid_t)arg)
453 continue;
454 break;
455
456 case KERN_PROC_UID:
457 if (eproc.e_ucred.cr_uid != (uid_t)arg)
458 continue;
459 break;
460
461 case KERN_PROC_RUID:
462 if (eproc.e_pcred.p_ruid != (uid_t)arg)
463 continue;
464 break;
465 }
466 /*
467 * We're going to add another proc to the set. If this
468 * will overflow the buffer, assume the reason is because
469 * nprocs (or the proc list) is corrupt and declare an error.
470 */
471 if (cnt >= maxcnt) {
472 _kvm_err(kd, kd->program, "nprocs corrupt");
473 return (-1);
474 }
475 /*
476 * gather eproc
477 */
478 eproc.e_paddr = p;
479 if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
480 _kvm_err(kd, kd->program, "can't read pgrp at %x",
481 proc.p_pgrp);
482 return (-1);
483 }
484 eproc.e_sess = pgrp.pg_session;
485 eproc.e_pgid = pgrp.pg_id;
486 eproc.e_jobc = pgrp.pg_jobc;
487 if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
488 _kvm_err(kd, kd->program, "can't read session at %x",
489 pgrp.pg_session);
490 return (-1);
491 }
492 if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) {
493 if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
494 _kvm_err(kd, kd->program,
495 "can't read tty at %x", sess.s_ttyp);
496 return (-1);
497 }
498 eproc.e_tdev = tty.t_dev;
499 eproc.e_tsess = tty.t_session;
500 if (tty.t_pgrp != NULL) {
501 if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
502 _kvm_err(kd, kd->program,
503 "can't read tpgrp at &x",
504 tty.t_pgrp);
505 return (-1);
506 }
507 eproc.e_tpgid = pgrp.pg_id;
508 } else
509 eproc.e_tpgid = -1;
510 } else
511 eproc.e_tdev = NODEV;
512 eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0;
513 if (sess.s_leader == p)
514 eproc.e_flag |= EPROC_SLEADER;
515 if (proc.p_wmesg)
516 (void)kvm_read(kd, (u_long)proc.p_wmesg,
517 eproc.e_wmesg, WMESGLEN);
518
519 (void)kvm_read(kd, (u_long)proc.p_vmspace,
520 (char *)&eproc.e_vm, sizeof(eproc.e_vm));
521
522 eproc.e_xsize = eproc.e_xrssize = 0;
523 eproc.e_xccount = eproc.e_xswrss = 0;
524
525 switch (what) {
526
527 case KERN_PROC_PGRP:
528 if (eproc.e_pgid != (pid_t)arg)
529 continue;
530 break;
531
532 case KERN_PROC_TTY:
533 if ((proc.p_flag & P_CONTROLT) == 0 ||
534 eproc.e_tdev != (dev_t)arg)
535 continue;
536 break;
537 }
538 memcpy(&bp->kp_proc, &proc, sizeof(proc));
539 memcpy(&bp->kp_eproc, &eproc, sizeof(eproc));
540 ++bp;
541 ++cnt;
542 }
543 return (cnt);
544 }
545
546 /*
547 * Build proc info array by reading in proc list from a crash dump.
548 * Return number of procs read. maxcnt is the max we will read.
549 */
550 static int
551 kvm_deadprocs(kd, what, arg, a_allproc, a_deadproc, a_zombproc, maxcnt)
552 kvm_t *kd;
553 int what, arg;
554 u_long a_allproc;
555 u_long a_deadproc;
556 u_long a_zombproc;
557 int maxcnt;
558 {
559 struct kinfo_proc *bp = kd->procbase;
560 int acnt, dcnt, zcnt;
561 struct proc *p;
562
563 if (KREAD(kd, a_allproc, &p)) {
564 _kvm_err(kd, kd->program, "cannot read allproc");
565 return (-1);
566 }
567 acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
568 if (acnt < 0)
569 return (acnt);
570
571 if (KREAD(kd, a_deadproc, &p)) {
572 _kvm_err(kd, kd->program, "cannot read deadproc");
573 return (-1);
574 }
575
576 dcnt = kvm_proclist(kd, what, arg, p, bp, maxcnt - acnt);
577 if (dcnt < 0)
578 dcnt = 0;
579
580 if (KREAD(kd, a_zombproc, &p)) {
581 _kvm_err(kd, kd->program, "cannot read zombproc");
582 return (-1);
583 }
584 zcnt = kvm_proclist(kd, what, arg, p, bp + acnt,
585 maxcnt - (acnt + dcnt));
586 if (zcnt < 0)
587 zcnt = 0;
588
589 return (acnt + zcnt);
590 }
591
592 struct kinfo_proc *
593 kvm_getprocs(kd, op, arg, cnt)
594 kvm_t *kd;
595 int op, arg;
596 int *cnt;
597 {
598 size_t size;
599 int mib[4], st, nprocs;
600
601 if (kd->procbase != 0) {
602 free((void *)kd->procbase);
603 /*
604 * Clear this pointer in case this call fails. Otherwise,
605 * kvm_close() will free it again.
606 */
607 kd->procbase = 0;
608 }
609 if (ISALIVE(kd)) {
610 size = 0;
611 mib[0] = CTL_KERN;
612 mib[1] = KERN_PROC;
613 mib[2] = op;
614 mib[3] = arg;
615 st = sysctl(mib, 4, NULL, &size, NULL, 0);
616 if (st == -1) {
617 _kvm_syserr(kd, kd->program, "kvm_getprocs");
618 return (0);
619 }
620 kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
621 if (kd->procbase == 0)
622 return (0);
623 st = sysctl(mib, 4, kd->procbase, &size, NULL, 0);
624 if (st == -1) {
625 _kvm_syserr(kd, kd->program, "kvm_getprocs");
626 return (0);
627 }
628 if (size % sizeof(struct kinfo_proc) != 0) {
629 _kvm_err(kd, kd->program,
630 "proc size mismatch (%d total, %d chunks)",
631 size, sizeof(struct kinfo_proc));
632 return (0);
633 }
634 nprocs = size / sizeof(struct kinfo_proc);
635 } else {
636 struct nlist nl[5], *p;
637
638 nl[0].n_name = "_nprocs";
639 nl[1].n_name = "_allproc";
640 nl[2].n_name = "_deadproc";
641 nl[3].n_name = "_zombproc";
642 nl[4].n_name = 0;
643
644 if (kvm_nlist(kd, nl) != 0) {
645 for (p = nl; p->n_type != 0; ++p)
646 ;
647 _kvm_err(kd, kd->program,
648 "%s: no such symbol", p->n_name);
649 return (0);
650 }
651 if (KREAD(kd, nl[0].n_value, &nprocs)) {
652 _kvm_err(kd, kd->program, "can't read nprocs");
653 return (0);
654 }
655 size = nprocs * sizeof(struct kinfo_proc);
656 kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
657 if (kd->procbase == 0)
658 return (0);
659
660 nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
661 nl[2].n_value, nl[3].n_value, nprocs);
662 #ifdef notdef
663 size = nprocs * sizeof(struct kinfo_proc);
664 (void)realloc(kd->procbase, size);
665 #endif
666 }
667 *cnt = nprocs;
668 return (kd->procbase);
669 }
670
671 void
672 _kvm_freeprocs(kd)
673 kvm_t *kd;
674 {
675 if (kd->procbase) {
676 free(kd->procbase);
677 kd->procbase = 0;
678 }
679 }
680
681 void *
682 _kvm_realloc(kd, p, n)
683 kvm_t *kd;
684 void *p;
685 size_t n;
686 {
687 void *np = (void *)realloc(p, n);
688
689 if (np == 0)
690 _kvm_err(kd, kd->program, "out of memory");
691 return (np);
692 }
693
694 #ifndef MAX
695 #define MAX(a, b) ((a) > (b) ? (a) : (b))
696 #endif
697
698 /*
699 * Read in an argument vector from the user address space of process p.
700 * addr if the user-space base address of narg null-terminated contiguous
701 * strings. This is used to read in both the command arguments and
702 * environment strings. Read at most maxcnt characters of strings.
703 */
704 static char **
705 kvm_argv(kd, p, addr, narg, maxcnt)
706 kvm_t *kd;
707 const struct proc *p;
708 u_long addr;
709 int narg;
710 int maxcnt;
711 {
712 char *np, *cp, *ep, *ap;
713 u_long oaddr = -1;
714 int len, cc;
715 char **argv;
716
717 /*
718 * Check that there aren't an unreasonable number of agruments,
719 * and that the address is in user space.
720 */
721 if (narg > ARG_MAX || addr < kd->min_uva || addr >= kd->max_uva)
722 return (0);
723
724 if (kd->argv == 0) {
725 /*
726 * Try to avoid reallocs.
727 */
728 kd->argc = MAX(narg + 1, 32);
729 kd->argv = (char **)_kvm_malloc(kd, kd->argc *
730 sizeof(*kd->argv));
731 if (kd->argv == 0)
732 return (0);
733 } else if (narg + 1 > kd->argc) {
734 kd->argc = MAX(2 * kd->argc, narg + 1);
735 kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc *
736 sizeof(*kd->argv));
737 if (kd->argv == 0)
738 return (0);
739 }
740 if (kd->argspc == 0) {
741 kd->argspc = (char *)_kvm_malloc(kd, kd->nbpg);
742 if (kd->argspc == 0)
743 return (0);
744 kd->arglen = kd->nbpg;
745 }
746 if (kd->argbuf == 0) {
747 kd->argbuf = (char *)_kvm_malloc(kd, kd->nbpg);
748 if (kd->argbuf == 0)
749 return (0);
750 }
751 cc = sizeof(char *) * narg;
752 if (kvm_uread(kd, p, addr, (char *)kd->argv, cc) != cc)
753 return (0);
754 ap = np = kd->argspc;
755 argv = kd->argv;
756 len = 0;
757 /*
758 * Loop over pages, filling in the argument vector.
759 */
760 while (argv < kd->argv + narg && *argv != 0) {
761 addr = (u_long)*argv & ~(kd->nbpg - 1);
762 if (addr != oaddr) {
763 if (kvm_uread(kd, p, addr, kd->argbuf, kd->nbpg) !=
764 kd->nbpg)
765 return (0);
766 oaddr = addr;
767 }
768 addr = (u_long)*argv & (kd->nbpg - 1);
769 cp = kd->argbuf + addr;
770 cc = kd->nbpg - addr;
771 if (maxcnt > 0 && cc > maxcnt - len)
772 cc = maxcnt - len;;
773 ep = memchr(cp, '\0', cc);
774 if (ep != 0)
775 cc = ep - cp + 1;
776 if (len + cc > kd->arglen) {
777 int off;
778 char **pp;
779 char *op = kd->argspc;
780
781 kd->arglen *= 2;
782 kd->argspc = (char *)_kvm_realloc(kd, kd->argspc,
783 kd->arglen);
784 if (kd->argspc == 0)
785 return (0);
786 /*
787 * Adjust argv pointers in case realloc moved
788 * the string space.
789 */
790 off = kd->argspc - op;
791 for (pp = kd->argv; pp < argv; pp++)
792 *pp += off;
793 ap += off;
794 np += off;
795 }
796 memcpy(np, cp, cc);
797 np += cc;
798 len += cc;
799 if (ep != 0) {
800 *argv++ = ap;
801 ap = np;
802 } else
803 *argv += cc;
804 if (maxcnt > 0 && len >= maxcnt) {
805 /*
806 * We're stopping prematurely. Terminate the
807 * current string.
808 */
809 if (ep == 0) {
810 *np = '\0';
811 *argv++ = ap;
812 }
813 break;
814 }
815 }
816 /* Make sure argv is terminated. */
817 *argv = 0;
818 return (kd->argv);
819 }
820
821 static void
822 ps_str_a(p, addr, n)
823 struct ps_strings *p;
824 u_long *addr;
825 int *n;
826 {
827 *addr = (u_long)p->ps_argvstr;
828 *n = p->ps_nargvstr;
829 }
830
831 static void
832 ps_str_e(p, addr, n)
833 struct ps_strings *p;
834 u_long *addr;
835 int *n;
836 {
837 *addr = (u_long)p->ps_envstr;
838 *n = p->ps_nenvstr;
839 }
840
841 /*
842 * Determine if the proc indicated by p is still active.
843 * This test is not 100% foolproof in theory, but chances of
844 * being wrong are very low.
845 */
846 static int
847 proc_verify(kd, kernp, p)
848 kvm_t *kd;
849 u_long kernp;
850 const struct proc *p;
851 {
852 struct proc kernproc;
853
854 /*
855 * Just read in the whole proc. It's not that big relative
856 * to the cost of the read system call.
857 */
858 if (kvm_read(kd, kernp, (char *)&kernproc, sizeof(kernproc)) !=
859 sizeof(kernproc))
860 return (0);
861 return (p->p_pid == kernproc.p_pid &&
862 (kernproc.p_stat != SZOMB || p->p_stat == SZOMB));
863 }
864
865 static char **
866 kvm_doargv(kd, kp, nchr, info)
867 kvm_t *kd;
868 const struct kinfo_proc *kp;
869 int nchr;
870 void (*info)(struct ps_strings *, u_long *, int *);
871 {
872 const struct proc *p = &kp->kp_proc;
873 char **ap;
874 u_long addr;
875 int cnt;
876 struct ps_strings arginfo;
877
878 /*
879 * Pointers are stored at the top of the user stack.
880 */
881 if (p->p_stat == SZOMB)
882 return (0);
883 cnt = kvm_uread(kd, p, kd->usrstack - sizeof(arginfo),
884 (char *)&arginfo, sizeof(arginfo));
885 if (cnt != sizeof(arginfo))
886 return (0);
887
888 (*info)(&arginfo, &addr, &cnt);
889 if (cnt == 0)
890 return (0);
891 ap = kvm_argv(kd, p, addr, cnt, nchr);
892 /*
893 * For live kernels, make sure this process didn't go away.
894 */
895 if (ap != 0 && ISALIVE(kd) &&
896 !proc_verify(kd, (u_long)kp->kp_eproc.e_paddr, p))
897 ap = 0;
898 return (ap);
899 }
900
901 /*
902 * Get the command args. This code is now machine independent.
903 */
904 char **
905 kvm_getargv(kd, kp, nchr)
906 kvm_t *kd;
907 const struct kinfo_proc *kp;
908 int nchr;
909 {
910 return (kvm_doargv(kd, kp, nchr, ps_str_a));
911 }
912
913 char **
914 kvm_getenvv(kd, kp, nchr)
915 kvm_t *kd;
916 const struct kinfo_proc *kp;
917 int nchr;
918 {
919 return (kvm_doargv(kd, kp, nchr, ps_str_e));
920 }
921
922 /*
923 * Read from user space. The user context is given by p.
924 */
925 ssize_t
926 kvm_uread(kd, p, uva, buf, len)
927 kvm_t *kd;
928 const struct proc *p;
929 u_long uva;
930 char *buf;
931 size_t len;
932 {
933 char *cp;
934
935 cp = buf;
936 while (len > 0) {
937 int cc;
938 char *dp;
939 u_long cnt;
940
941 dp = _kvm_uread(kd, p, uva, &cnt);
942 if (dp == 0) {
943 _kvm_err(kd, 0, "invalid address (%x)", uva);
944 return (0);
945 }
946 cc = MIN(cnt, len);
947 memcpy(cp, dp, cc);
948
949 cp += cc;
950 uva += cc;
951 len -= cc;
952 }
953 return (ssize_t)(cp - buf);
954 }
955