kvm_proc.c revision 1.26 1 /* $NetBSD: kvm_proc.c,v 1.26 1998/08/15 09:16:28 mycroft 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.26 1998/08/15 09:16:28 mycroft 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, int));
136 static char **kvm_doargv __P((kvm_t *, const struct kinfo_proc *, int,
137 void (*)(struct ps_strings *, u_long *, int *)));
138 static int kvm_proclist __P((kvm_t *, int, int, struct proc *,
139 struct kinfo_proc *, int));
140 static int proc_verify __P((kvm_t *, u_long, const struct proc *));
141 static void ps_str_a __P((struct ps_strings *, u_long *, int *));
142 static void ps_str_e __P((struct ps_strings *, u_long *, int *));
143
144 char *
145 _kvm_uread(kd, p, va, cnt)
146 kvm_t *kd;
147 const struct proc *p;
148 u_long va;
149 u_long *cnt;
150 {
151 u_long addr, head;
152 u_long offset;
153 struct vm_map_entry vme;
154 #if defined(UVM)
155 struct vm_amap amap;
156 struct vm_anon *anonp, anon;
157 struct vm_page pg;
158 int slot;
159 #else
160 struct vm_object vmo;
161 int rv;
162 #endif
163
164 if (kd->swapspc == 0) {
165 kd->swapspc = (char *)_kvm_malloc(kd, kd->nbpg);
166 if (kd->swapspc == 0)
167 return (0);
168 }
169
170 /*
171 * Look through the address map for the memory object
172 * that corresponds to the given virtual address.
173 * The header just has the entire valid range.
174 */
175 head = (u_long)&p->p_vmspace->vm_map.header;
176 addr = head;
177 while (1) {
178 if (KREAD(kd, addr, &vme))
179 return (0);
180
181 #if defined(UVM)
182 if (va >= vme.start && va < vme.end &&
183 vme.aref.ar_amap != NULL)
184 break;
185
186 #else
187 if (va >= vme.start && va < vme.end &&
188 vme.object.vm_object != 0)
189 break;
190 #endif
191
192 addr = (u_long)vme.next;
193 if (addr == head)
194 return (0);
195
196 }
197 #if defined(UVM)
198
199 /*
200 * we found the map entry, now to find the object...
201 */
202 if (vme.aref.ar_amap == NULL)
203 return NULL;
204
205 addr = (u_long)vme.aref.ar_amap;
206 if (KREAD(kd, addr, &amap))
207 return NULL;
208
209 offset = va - vme.start;
210 slot = offset / kd->nbpg + vme.aref.ar_slotoff;
211 /* sanity-check slot number */
212 if (slot > amap.am_nslot)
213 return NULL;
214
215 addr = (u_long)amap.am_anon + (offset / kd->nbpg) * sizeof(anonp);
216 if (KREAD(kd, addr, &anonp))
217 return NULL;
218
219 addr = (u_long)anonp;
220 if (KREAD(kd, addr, &anon))
221 return NULL;
222
223 addr = (u_long)anon.u.an_page;
224 if (addr) {
225 if (KREAD(kd, addr, &pg))
226 return NULL;
227
228 if (pread(kd->pmfd, kd->swapspc, kd->nbpg,
229 (off_t)pg.phys_addr) != kd->nbpg)
230 return NULL;
231 }
232 else {
233 if (pread(kd->swfd, kd->swapspc, kd->nbpg,
234 (off_t)(anon.an_swslot * kd->nbpg)) != kd->nbpg)
235 return NULL;
236 }
237 #else
238 /*
239 * We found the right object -- follow shadow links.
240 */
241 offset = va - vme.start + vme.offset;
242 addr = (u_long)vme.object.vm_object;
243
244 while (1) {
245 /* Try reading the page from core first. */
246 if ((rv = _kvm_readfromcore(kd, addr, offset)))
247 break;
248
249 if (KREAD(kd, addr, &vmo))
250 return (0);
251
252 /* If there is a pager here, see if it has the page. */
253 if (vmo.pager != 0 &&
254 (rv = _kvm_readfrompager(kd, &vmo, offset)))
255 break;
256
257 /* Move down the shadow chain. */
258 addr = (u_long)vmo.shadow;
259 if (addr == 0)
260 return (0);
261 offset += vmo.shadow_offset;
262 }
263
264 if (rv == -1)
265 return (0);
266 #endif
267
268 /* Found the page. */
269 offset %= kd->nbpg;
270 *cnt = kd->nbpg - offset;
271 return (&kd->swapspc[offset]);
272 }
273
274 #if !defined(UVM)
275
276 #define vm_page_hash(kd, object, offset) \
277 (((u_long)object + (u_long)(offset / kd->nbpg)) & kd->vm_page_hash_mask)
278
279 int
280 _kvm_coreinit(kd)
281 kvm_t *kd;
282 {
283 struct nlist nlist[3];
284
285 nlist[0].n_name = "_vm_page_buckets";
286 nlist[1].n_name = "_vm_page_hash_mask";
287 nlist[2].n_name = 0;
288 if (kvm_nlist(kd, nlist) != 0)
289 return (-1);
290
291 if (KREAD(kd, nlist[0].n_value, &kd->vm_page_buckets) ||
292 KREAD(kd, nlist[1].n_value, &kd->vm_page_hash_mask))
293 return (-1);
294
295 return (0);
296 }
297
298 int
299 _kvm_readfromcore(kd, object, offset)
300 kvm_t *kd;
301 u_long object, offset;
302 {
303 u_long addr;
304 struct pglist bucket;
305 struct vm_page mem;
306 off_t seekpoint;
307
308 if (kd->vm_page_buckets == 0 &&
309 _kvm_coreinit(kd))
310 return (-1);
311
312 addr = (u_long)&kd->vm_page_buckets[vm_page_hash(kd, object, offset)];
313 if (KREAD(kd, addr, &bucket))
314 return (-1);
315
316 addr = (u_long)bucket.tqh_first;
317 offset &= ~(kd->nbpg -1);
318 while (1) {
319 if (addr == 0)
320 return (0);
321
322 if (KREAD(kd, addr, &mem))
323 return (-1);
324
325 if ((u_long)mem.object == object &&
326 (u_long)mem.offset == offset)
327 break;
328
329 addr = (u_long)mem.hashq.tqe_next;
330 }
331
332 seekpoint = mem.phys_addr;
333
334 if (pread(kd->pmfd, kd->swapspc, kd->nbpg, seekpoint) != kd->nbpg)
335 return (-1);
336
337 return (1);
338 }
339
340 int
341 _kvm_readfrompager(kd, vmop, offset)
342 kvm_t *kd;
343 struct vm_object *vmop;
344 u_long offset;
345 {
346 u_long addr;
347 struct pager_struct pager;
348 struct swpager swap;
349 int ix;
350 struct swblock swb;
351 off_t seekpoint;
352
353 /* Read in the pager info and make sure it's a swap device. */
354 addr = (u_long)vmop->pager;
355 if (KREAD(kd, addr, &pager) || pager.pg_type != PG_SWAP)
356 return (-1);
357
358 /* Read in the swap_pager private data. */
359 addr = (u_long)pager.pg_data;
360 if (KREAD(kd, addr, &swap))
361 return (-1);
362
363 /*
364 * Calculate the paging offset, and make sure it's within the
365 * bounds of the pager.
366 */
367 offset += vmop->paging_offset;
368 ix = offset / dbtob(swap.sw_bsize);
369 #if 0
370 if (swap.sw_blocks == 0 || ix >= swap.sw_nblocks)
371 return (-1);
372 #else
373 if (swap.sw_blocks == 0 || ix >= swap.sw_nblocks) {
374 int i;
375 printf("BUG BUG BUG BUG:\n");
376 printf("object %p offset %lx pgoffset %lx ",
377 vmop, offset - vmop->paging_offset,
378 (u_long)vmop->paging_offset);
379 printf("pager %p swpager %p\n",
380 vmop->pager, pager.pg_data);
381 printf("osize %lx bsize %x blocks %p nblocks %x\n",
382 (u_long)swap.sw_osize, swap.sw_bsize, swap.sw_blocks,
383 swap.sw_nblocks);
384 for (i = 0; i < swap.sw_nblocks; i++) {
385 addr = (u_long)&swap.sw_blocks[i];
386 if (KREAD(kd, addr, &swb))
387 return (0);
388 printf("sw_blocks[%d]: block %x mask %x\n", i,
389 swb.swb_block, swb.swb_mask);
390 }
391 return (-1);
392 }
393 #endif
394
395 /* Read in the swap records. */
396 addr = (u_long)&swap.sw_blocks[ix];
397 if (KREAD(kd, addr, &swb))
398 return (-1);
399
400 /* Calculate offset within pager. */
401 offset %= dbtob(swap.sw_bsize);
402
403 /* Check that the page is actually present. */
404 if ((swb.swb_mask & (1 << (offset / kd->nbpg))) == 0)
405 return (0);
406
407 if (!ISALIVE(kd))
408 return (-1);
409
410 /* Calculate the physical address and read the page. */
411 seekpoint = dbtob(swb.swb_block) + (offset & ~(kd->nbpg -1));
412
413 if (pread(kd->swfd, kd->swapspc, kd->nbpg, seekpoint) != kd->nbpg)
414 return (-1);
415
416 return (1);
417 }
418 #endif /* !defined(UVM) */
419
420 /*
421 * Read proc's from memory file into buffer bp, which has space to hold
422 * at most maxcnt procs.
423 */
424 static int
425 kvm_proclist(kd, what, arg, p, bp, maxcnt)
426 kvm_t *kd;
427 int what, arg;
428 struct proc *p;
429 struct kinfo_proc *bp;
430 int maxcnt;
431 {
432 int cnt = 0;
433 struct eproc eproc;
434 struct pgrp pgrp;
435 struct session sess;
436 struct tty tty;
437 struct proc proc;
438
439 for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) {
440 if (KREAD(kd, (u_long)p, &proc)) {
441 _kvm_err(kd, kd->program, "can't read proc at %x", p);
442 return (-1);
443 }
444 if (KREAD(kd, (u_long)proc.p_cred, &eproc.e_pcred) == 0)
445 (void)KREAD(kd, (u_long)eproc.e_pcred.pc_ucred,
446 &eproc.e_ucred);
447
448 switch(what) {
449
450 case KERN_PROC_PID:
451 if (proc.p_pid != (pid_t)arg)
452 continue;
453 break;
454
455 case KERN_PROC_UID:
456 if (eproc.e_ucred.cr_uid != (uid_t)arg)
457 continue;
458 break;
459
460 case KERN_PROC_RUID:
461 if (eproc.e_pcred.p_ruid != (uid_t)arg)
462 continue;
463 break;
464 }
465 /*
466 * We're going to add another proc to the set. If this
467 * will overflow the buffer, assume the reason is because
468 * nprocs (or the proc list) is corrupt and declare an error.
469 */
470 if (cnt >= maxcnt) {
471 _kvm_err(kd, kd->program, "nprocs corrupt");
472 return (-1);
473 }
474 /*
475 * gather eproc
476 */
477 eproc.e_paddr = p;
478 if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
479 _kvm_err(kd, kd->program, "can't read pgrp at %x",
480 proc.p_pgrp);
481 return (-1);
482 }
483 eproc.e_sess = pgrp.pg_session;
484 eproc.e_pgid = pgrp.pg_id;
485 eproc.e_jobc = pgrp.pg_jobc;
486 if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
487 _kvm_err(kd, kd->program, "can't read session at %x",
488 pgrp.pg_session);
489 return (-1);
490 }
491 if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) {
492 if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
493 _kvm_err(kd, kd->program,
494 "can't read tty at %x", sess.s_ttyp);
495 return (-1);
496 }
497 eproc.e_tdev = tty.t_dev;
498 eproc.e_tsess = tty.t_session;
499 if (tty.t_pgrp != NULL) {
500 if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
501 _kvm_err(kd, kd->program,
502 "can't read tpgrp at &x",
503 tty.t_pgrp);
504 return (-1);
505 }
506 eproc.e_tpgid = pgrp.pg_id;
507 } else
508 eproc.e_tpgid = -1;
509 } else
510 eproc.e_tdev = NODEV;
511 eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0;
512 if (sess.s_leader == p)
513 eproc.e_flag |= EPROC_SLEADER;
514 if (proc.p_wmesg)
515 (void)kvm_read(kd, (u_long)proc.p_wmesg,
516 eproc.e_wmesg, WMESGLEN);
517
518 (void)kvm_read(kd, (u_long)proc.p_vmspace,
519 (char *)&eproc.e_vm, sizeof(eproc.e_vm));
520
521 eproc.e_xsize = eproc.e_xrssize = 0;
522 eproc.e_xccount = eproc.e_xswrss = 0;
523
524 switch (what) {
525
526 case KERN_PROC_PGRP:
527 if (eproc.e_pgid != (pid_t)arg)
528 continue;
529 break;
530
531 case KERN_PROC_TTY:
532 if ((proc.p_flag & P_CONTROLT) == 0 ||
533 eproc.e_tdev != (dev_t)arg)
534 continue;
535 break;
536 }
537 memcpy(&bp->kp_proc, &proc, sizeof(proc));
538 memcpy(&bp->kp_eproc, &eproc, sizeof(eproc));
539 ++bp;
540 ++cnt;
541 }
542 return (cnt);
543 }
544
545 /*
546 * Build proc info array by reading in proc list from a crash dump.
547 * Return number of procs read. maxcnt is the max we will read.
548 */
549 static int
550 kvm_deadprocs(kd, what, arg, a_allproc, a_zombproc, maxcnt)
551 kvm_t *kd;
552 int what, arg;
553 u_long a_allproc;
554 u_long a_zombproc;
555 int maxcnt;
556 {
557 struct kinfo_proc *bp = kd->procbase;
558 int acnt, zcnt;
559 struct proc *p;
560
561 if (KREAD(kd, a_allproc, &p)) {
562 _kvm_err(kd, kd->program, "cannot read allproc");
563 return (-1);
564 }
565 acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
566 if (acnt < 0)
567 return (acnt);
568
569 if (KREAD(kd, a_zombproc, &p)) {
570 _kvm_err(kd, kd->program, "cannot read zombproc");
571 return (-1);
572 }
573 zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt);
574 if (zcnt < 0)
575 zcnt = 0;
576
577 return (acnt + zcnt);
578 }
579
580 struct kinfo_proc *
581 kvm_getprocs(kd, op, arg, cnt)
582 kvm_t *kd;
583 int op, arg;
584 int *cnt;
585 {
586 size_t size;
587 int mib[4], st, nprocs;
588
589 if (kd->procbase != 0) {
590 free((void *)kd->procbase);
591 /*
592 * Clear this pointer in case this call fails. Otherwise,
593 * kvm_close() will free it again.
594 */
595 kd->procbase = 0;
596 }
597 if (ISALIVE(kd)) {
598 size = 0;
599 mib[0] = CTL_KERN;
600 mib[1] = KERN_PROC;
601 mib[2] = op;
602 mib[3] = arg;
603 st = sysctl(mib, 4, NULL, &size, NULL, 0);
604 if (st == -1) {
605 _kvm_syserr(kd, kd->program, "kvm_getprocs");
606 return (0);
607 }
608 kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
609 if (kd->procbase == 0)
610 return (0);
611 st = sysctl(mib, 4, kd->procbase, &size, NULL, 0);
612 if (st == -1) {
613 _kvm_syserr(kd, kd->program, "kvm_getprocs");
614 return (0);
615 }
616 if (size % sizeof(struct kinfo_proc) != 0) {
617 _kvm_err(kd, kd->program,
618 "proc size mismatch (%d total, %d chunks)",
619 size, sizeof(struct kinfo_proc));
620 return (0);
621 }
622 nprocs = size / sizeof(struct kinfo_proc);
623 } else {
624 struct nlist nl[4], *p;
625
626 nl[0].n_name = "_nprocs";
627 nl[1].n_name = "_allproc";
628 nl[2].n_name = "_zombproc";
629 nl[3].n_name = 0;
630
631 if (kvm_nlist(kd, nl) != 0) {
632 for (p = nl; p->n_type != 0; ++p)
633 ;
634 _kvm_err(kd, kd->program,
635 "%s: no such symbol", p->n_name);
636 return (0);
637 }
638 if (KREAD(kd, nl[0].n_value, &nprocs)) {
639 _kvm_err(kd, kd->program, "can't read nprocs");
640 return (0);
641 }
642 size = nprocs * sizeof(struct kinfo_proc);
643 kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
644 if (kd->procbase == 0)
645 return (0);
646
647 nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
648 nl[2].n_value, nprocs);
649 #ifdef notdef
650 size = nprocs * sizeof(struct kinfo_proc);
651 (void)realloc(kd->procbase, size);
652 #endif
653 }
654 *cnt = nprocs;
655 return (kd->procbase);
656 }
657
658 void
659 _kvm_freeprocs(kd)
660 kvm_t *kd;
661 {
662 if (kd->procbase) {
663 free(kd->procbase);
664 kd->procbase = 0;
665 }
666 }
667
668 void *
669 _kvm_realloc(kd, p, n)
670 kvm_t *kd;
671 void *p;
672 size_t n;
673 {
674 void *np = (void *)realloc(p, n);
675
676 if (np == 0)
677 _kvm_err(kd, kd->program, "out of memory");
678 return (np);
679 }
680
681 #ifndef MAX
682 #define MAX(a, b) ((a) > (b) ? (a) : (b))
683 #endif
684
685 /*
686 * Read in an argument vector from the user address space of process p.
687 * addr if the user-space base address of narg null-terminated contiguous
688 * strings. This is used to read in both the command arguments and
689 * environment strings. Read at most maxcnt characters of strings.
690 */
691 static char **
692 kvm_argv(kd, p, addr, narg, maxcnt)
693 kvm_t *kd;
694 const struct proc *p;
695 u_long addr;
696 int narg;
697 int maxcnt;
698 {
699 char *np, *cp, *ep, *ap;
700 u_long oaddr = -1;
701 int len, cc;
702 char **argv;
703
704 /*
705 * Check that there aren't an unreasonable number of agruments,
706 * and that the address is in user space.
707 */
708 if (narg > ARG_MAX || addr < kd->min_uva || addr >= kd->max_uva)
709 return (0);
710
711 if (kd->argv == 0) {
712 /*
713 * Try to avoid reallocs.
714 */
715 kd->argc = MAX(narg + 1, 32);
716 kd->argv = (char **)_kvm_malloc(kd, kd->argc *
717 sizeof(*kd->argv));
718 if (kd->argv == 0)
719 return (0);
720 } else if (narg + 1 > kd->argc) {
721 kd->argc = MAX(2 * kd->argc, narg + 1);
722 kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc *
723 sizeof(*kd->argv));
724 if (kd->argv == 0)
725 return (0);
726 }
727 if (kd->argspc == 0) {
728 kd->argspc = (char *)_kvm_malloc(kd, kd->nbpg);
729 if (kd->argspc == 0)
730 return (0);
731 kd->arglen = kd->nbpg;
732 }
733 if (kd->argbuf == 0) {
734 kd->argbuf = (char *)_kvm_malloc(kd, kd->nbpg);
735 if (kd->argbuf == 0)
736 return (0);
737 }
738 cc = sizeof(char *) * narg;
739 if (kvm_uread(kd, p, addr, (char *)kd->argv, cc) != cc)
740 return (0);
741 ap = np = kd->argspc;
742 argv = kd->argv;
743 len = 0;
744 /*
745 * Loop over pages, filling in the argument vector.
746 */
747 while (argv < kd->argv + narg && *argv != 0) {
748 addr = (u_long)*argv & ~(kd->nbpg - 1);
749 if (addr != oaddr) {
750 if (kvm_uread(kd, p, addr, kd->argbuf, kd->nbpg) !=
751 kd->nbpg)
752 return (0);
753 oaddr = addr;
754 }
755 addr = (u_long)*argv & (kd->nbpg - 1);
756 cp = kd->argbuf + addr;
757 cc = kd->nbpg - addr;
758 if (maxcnt > 0 && cc > maxcnt - len)
759 cc = maxcnt - len;;
760 ep = memchr(cp, '\0', cc);
761 if (ep != 0)
762 cc = ep - cp + 1;
763 if (len + cc > kd->arglen) {
764 int off;
765 char **pp;
766 char *op = kd->argspc;
767
768 kd->arglen *= 2;
769 kd->argspc = (char *)_kvm_realloc(kd, kd->argspc,
770 kd->arglen);
771 if (kd->argspc == 0)
772 return (0);
773 /*
774 * Adjust argv pointers in case realloc moved
775 * the string space.
776 */
777 off = kd->argspc - op;
778 for (pp = kd->argv; pp < argv; pp++)
779 *pp += off;
780 ap += off;
781 np += off;
782 }
783 memcpy(np, cp, cc);
784 np += cc;
785 len += cc;
786 if (ep != 0) {
787 *argv++ = ap;
788 ap = np;
789 } else
790 *argv += cc;
791 if (maxcnt > 0 && len >= maxcnt) {
792 /*
793 * We're stopping prematurely. Terminate the
794 * current string.
795 */
796 if (ep == 0) {
797 *np = '\0';
798 *argv++ = ap;
799 }
800 break;
801 }
802 }
803 /* Make sure argv is terminated. */
804 *argv = 0;
805 return (kd->argv);
806 }
807
808 static void
809 ps_str_a(p, addr, n)
810 struct ps_strings *p;
811 u_long *addr;
812 int *n;
813 {
814 *addr = (u_long)p->ps_argvstr;
815 *n = p->ps_nargvstr;
816 }
817
818 static void
819 ps_str_e(p, addr, n)
820 struct ps_strings *p;
821 u_long *addr;
822 int *n;
823 {
824 *addr = (u_long)p->ps_envstr;
825 *n = p->ps_nenvstr;
826 }
827
828 /*
829 * Determine if the proc indicated by p is still active.
830 * This test is not 100% foolproof in theory, but chances of
831 * being wrong are very low.
832 */
833 static int
834 proc_verify(kd, kernp, p)
835 kvm_t *kd;
836 u_long kernp;
837 const struct proc *p;
838 {
839 struct proc kernproc;
840
841 /*
842 * Just read in the whole proc. It's not that big relative
843 * to the cost of the read system call.
844 */
845 if (kvm_read(kd, kernp, (char *)&kernproc, sizeof(kernproc)) !=
846 sizeof(kernproc))
847 return (0);
848 return (p->p_pid == kernproc.p_pid &&
849 (kernproc.p_stat != SZOMB || p->p_stat == SZOMB));
850 }
851
852 static char **
853 kvm_doargv(kd, kp, nchr, info)
854 kvm_t *kd;
855 const struct kinfo_proc *kp;
856 int nchr;
857 void (*info)(struct ps_strings *, u_long *, int *);
858 {
859 const struct proc *p = &kp->kp_proc;
860 char **ap;
861 u_long addr;
862 int cnt;
863 struct ps_strings arginfo;
864
865 /*
866 * Pointers are stored at the top of the user stack.
867 */
868 if (p->p_stat == SZOMB)
869 return (0);
870 cnt = kvm_uread(kd, p, kd->usrstack - sizeof(arginfo),
871 (char *)&arginfo, sizeof(arginfo));
872 if (cnt != sizeof(arginfo))
873 return (0);
874
875 (*info)(&arginfo, &addr, &cnt);
876 if (cnt == 0)
877 return (0);
878 ap = kvm_argv(kd, p, addr, cnt, nchr);
879 /*
880 * For live kernels, make sure this process didn't go away.
881 */
882 if (ap != 0 && ISALIVE(kd) &&
883 !proc_verify(kd, (u_long)kp->kp_eproc.e_paddr, p))
884 ap = 0;
885 return (ap);
886 }
887
888 /*
889 * Get the command args. This code is now machine independent.
890 */
891 char **
892 kvm_getargv(kd, kp, nchr)
893 kvm_t *kd;
894 const struct kinfo_proc *kp;
895 int nchr;
896 {
897 return (kvm_doargv(kd, kp, nchr, ps_str_a));
898 }
899
900 char **
901 kvm_getenvv(kd, kp, nchr)
902 kvm_t *kd;
903 const struct kinfo_proc *kp;
904 int nchr;
905 {
906 return (kvm_doargv(kd, kp, nchr, ps_str_e));
907 }
908
909 /*
910 * Read from user space. The user context is given by p.
911 */
912 ssize_t
913 kvm_uread(kd, p, uva, buf, len)
914 kvm_t *kd;
915 const struct proc *p;
916 u_long uva;
917 char *buf;
918 size_t len;
919 {
920 char *cp;
921
922 cp = buf;
923 while (len > 0) {
924 int cc;
925 char *dp;
926 u_long cnt;
927
928 dp = _kvm_uread(kd, p, uva, &cnt);
929 if (dp == 0) {
930 _kvm_err(kd, 0, "invalid address (%x)", uva);
931 return (0);
932 }
933 cc = MIN(cnt, len);
934 memcpy(cp, dp, cc);
935
936 cp += cc;
937 uva += cc;
938 len -= cc;
939 }
940 return (ssize_t)(cp - buf);
941 }
942