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