kvm.c revision 1.1 1 /*-
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)kvm.c 5.18 (Berkeley) 5/7/91";
36 #endif /* LIBC_SCCS and not lint */
37
38 #include <sys/param.h>
39 #include <sys/user.h>
40 #include <sys/proc.h>
41 #include <sys/ioctl.h>
42 #include <sys/kinfo.h>
43 #include <sys/tty.h>
44 #include <machine/vmparam.h>
45 #include <fcntl.h>
46 #include <nlist.h>
47 #include <kvm.h>
48 #include <ndbm.h>
49 #include <limits.h>
50 #include <paths.h>
51 #include <stdio.h>
52 #include <string.h>
53
54 #ifdef SPPWAIT
55 #define NEWVM
56 #endif
57
58 #ifdef NEWVM
59 #define btop(x) (((unsigned)(x)) >> PGSHIFT) /* XXX */
60 #define ptob(x) ((caddr_t)((x) << PGSHIFT)) /* XXX */
61 #include <vm/vm.h> /* ??? kinfo_proc currently includes this*/
62 #include <sys/kinfo_proc.h>
63 #ifdef hp300
64 #include <hp300/hp300/pte.h>
65 #endif
66 #else /* NEWVM */
67 #include <machine/pte.h>
68 #include <sys/vmmac.h>
69 #include <sys/text.h>
70 #endif /* NEWVM */
71
72 /*
73 * files
74 */
75 static const char *unixf, *memf, *kmemf, *swapf;
76 static int unixx, mem, kmem, swap;
77 static DBM *db;
78 /*
79 * flags
80 */
81 static int deadkernel;
82 static int kvminit = 0;
83 static int kvmfilesopen = 0;
84 /*
85 * state
86 */
87 static struct kinfo_proc *kvmprocbase, *kvmprocptr;
88 static int kvmnprocs;
89 /*
90 * u. buffer
91 */
92 static union {
93 struct user user;
94 char upages[UPAGES][NBPG];
95 } user;
96 /*
97 * random other stuff
98 */
99 #ifndef NEWVM
100 static struct pte *Usrptmap, *usrpt;
101 static struct pte *Sysmap;
102 static int Syssize;
103 #endif
104 static int dmmin, dmmax;
105 static int pcbpf;
106 static int argaddr0; /* XXX */
107 static int argaddr1;
108 static int nswap;
109 static char *tmp;
110 #if defined(hp300)
111 static int lowram;
112 static struct ste *Sysseg;
113 #endif
114 #if defined(i386)
115 static struct pde *PTD;
116 #endif
117
118 #define basename(cp) ((tmp=rindex((cp), '/')) ? tmp+1 : (cp))
119 #define MAXSYMSIZE 256
120
121 #if defined(hp300)
122 #define pftoc(f) ((f) - lowram)
123 #define iskva(v) (1)
124 #endif
125
126 #ifndef pftoc
127 #define pftoc(f) (f)
128 #endif
129 #ifndef iskva
130 #define iskva(v) ((u_long)(v) & KERNBASE)
131 #endif
132
133 static struct nlist nl[] = {
134 { "_Usrptmap" },
135 #define X_USRPTMAP 0
136 { "_usrpt" },
137 #define X_USRPT 1
138 { "_nswap" },
139 #define X_NSWAP 2
140 { "_dmmin" },
141 #define X_DMMIN 3
142 { "_dmmax" },
143 #define X_DMMAX 4
144 /*
145 * everything here and down, only if a dead kernel
146 */
147 { "_Sysmap" },
148 #define X_SYSMAP 5
149 #define X_DEADKERNEL X_SYSMAP
150 { "_Syssize" },
151 #define X_SYSSIZE 6
152 { "_allproc" },
153 #define X_ALLPROC 7
154 { "_zombproc" },
155 #define X_ZOMBPROC 8
156 { "_nproc" },
157 #define X_NPROC 9
158 #define X_LAST 9
159 #if defined(hp300)
160 { "_Sysseg" },
161 #define X_SYSSEG (X_LAST+1)
162 { "_lowram" },
163 #define X_LOWRAM (X_LAST+2)
164 #endif
165 #if defined(i386)
166 { "_IdlePTD" },
167 #define X_IdlePTD (X_LAST+1)
168 #endif
169 { "" },
170 };
171
172 static off_t Vtophys();
173 static void klseek(), seterr(), setsyserr(), vstodb();
174 static int getkvars(), kvm_doprocs(), kvm_init();
175
176 /*
177 * returns 0 if files were opened now,
178 * 1 if files were already opened,
179 * -1 if files could not be opened.
180 */
181 kvm_openfiles(uf, mf, sf)
182 const char *uf, *mf, *sf;
183 {
184 if (kvmfilesopen)
185 return (1);
186 unixx = mem = kmem = swap = -1;
187 unixf = (uf == NULL) ? _PATH_UNIX : uf;
188 memf = (mf == NULL) ? _PATH_MEM : mf;
189
190 if ((unixx = open(unixf, O_RDONLY, 0)) == -1) {
191 setsyserr("can't open %s", unixf);
192 goto failed;
193 }
194 if ((mem = open(memf, O_RDONLY, 0)) == -1) {
195 setsyserr("can't open %s", memf);
196 goto failed;
197 }
198 if (sf != NULL)
199 swapf = sf;
200 if (mf != NULL) {
201 deadkernel++;
202 kmemf = mf;
203 kmem = mem;
204 swap = -1;
205 } else {
206 kmemf = _PATH_KMEM;
207 if ((kmem = open(kmemf, O_RDONLY, 0)) == -1) {
208 setsyserr("can't open %s", kmemf);
209 goto failed;
210 }
211 swapf = (sf == NULL) ? _PATH_DRUM : sf;
212 /*
213 * live kernel - avoid looking up nlist entries
214 * past X_DEADKERNEL.
215 */
216 nl[X_DEADKERNEL].n_name = "";
217 }
218 if (swapf != NULL && ((swap = open(swapf, O_RDONLY, 0)) == -1)) {
219 seterr("can't open %s", swapf);
220 goto failed;
221 }
222 kvmfilesopen++;
223 if (kvminit == 0 && kvm_init(NULL, NULL, NULL, 0) == -1) /*XXX*/
224 return (-1);
225 return (0);
226 failed:
227 kvm_close();
228 return (-1);
229 }
230
231 static
232 kvm_init(uf, mf, sf)
233 char *uf, *mf, *sf;
234 {
235 if (kvmfilesopen == 0 && kvm_openfiles(NULL, NULL, NULL) == -1)
236 return (-1);
237 if (getkvars() == -1)
238 return (-1);
239 kvminit = 1;
240
241 return (0);
242 }
243
244 kvm_close()
245 {
246 if (unixx != -1) {
247 close(unixx);
248 unixx = -1;
249 }
250 if (kmem != -1) {
251 if (kmem != mem)
252 close(kmem);
253 /* otherwise kmem is a copy of mem, and will be closed below */
254 kmem = -1;
255 }
256 if (mem != -1) {
257 close(mem);
258 mem = -1;
259 }
260 if (swap != -1) {
261 close(swap);
262 swap = -1;
263 }
264 if (db != NULL) {
265 dbm_close(db);
266 db = NULL;
267 }
268 kvminit = 0;
269 kvmfilesopen = 0;
270 deadkernel = 0;
271 #ifndef NEWVM
272 if (Sysmap) {
273 free(Sysmap);
274 Sysmap = NULL;
275 }
276 #endif
277 }
278
279 kvm_nlist(nl)
280 struct nlist *nl;
281 {
282 datum key, data;
283 char dbname[MAXPATHLEN];
284 char dbversion[_POSIX2_LINE_MAX];
285 char kversion[_POSIX2_LINE_MAX];
286 int dbversionlen;
287 char symbuf[MAXSYMSIZE];
288 struct nlist nbuf, *n;
289 int num, did;
290
291 if (kvmfilesopen == 0 && kvm_openfiles(NULL, NULL, NULL) == -1)
292 return (-1);
293 if (deadkernel)
294 goto hard2;
295 /*
296 * initialize key datum
297 */
298 key.dptr = symbuf;
299
300 if (db != NULL)
301 goto win; /* off to the races */
302 /*
303 * open database
304 */
305 sprintf(dbname, "%s/kvm_%s", _PATH_VARRUN, basename(unixf));
306 if ((db = dbm_open(dbname, O_RDONLY, 0)) == NULL)
307 goto hard2;
308 /*
309 * read version out of database
310 */
311 bcopy("VERSION", symbuf, sizeof ("VERSION")-1);
312 key.dsize = (sizeof ("VERSION") - 1) + 1;
313 data = dbm_fetch(db, key);
314 if (data.dptr == NULL)
315 goto hard1;
316 bcopy(data.dptr, dbversion, data.dsize);
317 dbversionlen = data.dsize;
318 /*
319 * read version string from kernel memory
320 */
321 bcopy("_version", symbuf, sizeof ("_version")-1);
322 key.dsize = (sizeof ("_version")-1) + 1;
323 data = dbm_fetch(db, key);
324 if (data.dptr == NULL)
325 goto hard1;
326 if (data.dsize != sizeof (struct nlist))
327 goto hard1;
328 bcopy(data.dptr, &nbuf, sizeof (struct nlist));
329 lseek(kmem, nbuf.n_value, 0);
330 if (read(kmem, kversion, dbversionlen) != dbversionlen)
331 goto hard1;
332 /*
333 * if they match, we win - otherwise do it the hard way
334 */
335 if (bcmp(dbversion, kversion, dbversionlen) != 0)
336 goto hard1;
337 /*
338 * getem from the database.
339 */
340 win:
341 num = did = 0;
342 for (n = nl; n->n_name && n->n_name[0]; n++, num++) {
343 int len;
344 /*
345 * clear out fields from users buffer
346 */
347 n->n_type = 0;
348 n->n_other = 0;
349 n->n_desc = 0;
350 n->n_value = 0;
351 /*
352 * query db
353 */
354 if ((len = strlen(n->n_name)) > MAXSYMSIZE) {
355 seterr("symbol too large");
356 return (-1);
357 }
358 (void)strcpy(symbuf, n->n_name);
359 key.dsize = len + 1;
360 data = dbm_fetch(db, key);
361 if (data.dptr == NULL || data.dsize != sizeof (struct nlist))
362 continue;
363 bcopy(data.dptr, &nbuf, sizeof (struct nlist));
364 n->n_value = nbuf.n_value;
365 n->n_type = nbuf.n_type;
366 n->n_desc = nbuf.n_desc;
367 n->n_other = nbuf.n_other;
368 did++;
369 }
370 return (num - did);
371 hard1:
372 dbm_close(db);
373 db = NULL;
374 hard2:
375 num = nlist(unixf, nl);
376 if (num == -1)
377 seterr("nlist (hard way) failed");
378 return (num);
379 }
380
381 kvm_getprocs(what, arg)
382 int what, arg;
383 {
384 if (kvminit == 0 && kvm_init(NULL, NULL, NULL, 0) == -1)
385 return (NULL);
386 if (!deadkernel) {
387 int ret, copysize;
388
389 if ((ret = getkerninfo(what, NULL, NULL, arg)) == -1) {
390 setsyserr("can't get estimate for kerninfo");
391 return (-1);
392 }
393 copysize = ret;
394 if ((kvmprocbase = (struct kinfo_proc *)malloc(copysize))
395 == NULL) {
396 seterr("out of memory");
397 return (-1);
398 }
399 if ((ret = getkerninfo(what, kvmprocbase, ©size,
400 arg)) == -1) {
401 setsyserr("can't get proc list");
402 return (-1);
403 }
404 if (copysize % sizeof (struct kinfo_proc)) {
405 seterr("proc size mismatch (got %d total, kinfo_proc: %d)",
406 copysize, sizeof (struct kinfo_proc));
407 return (-1);
408 }
409 kvmnprocs = copysize / sizeof (struct kinfo_proc);
410 } else {
411 int nproc;
412
413 if (kvm_read((void *) nl[X_NPROC].n_value, &nproc,
414 sizeof (int)) != sizeof (int)) {
415 seterr("can't read nproc");
416 return (-1);
417 }
418 if ((kvmprocbase = (struct kinfo_proc *)
419 malloc(nproc * sizeof (struct kinfo_proc))) == NULL) {
420 seterr("out of memory (addr: %x nproc = %d)",
421 nl[X_NPROC].n_value, nproc);
422 return (-1);
423 }
424 kvmnprocs = kvm_doprocs(what, arg, kvmprocbase);
425 realloc(kvmprocbase, kvmnprocs * sizeof (struct kinfo_proc));
426 }
427 kvmprocptr = kvmprocbase;
428
429 return (kvmnprocs);
430 }
431
432 /*
433 * XXX - should NOT give up so easily - especially since the kernel
434 * may be corrupt (it died). Should gather as much information as possible.
435 * Follows proc ptrs instead of reading table since table may go
436 * away soon.
437 */
438 static
439 kvm_doprocs(what, arg, buff)
440 int what, arg;
441 char *buff;
442 {
443 struct proc *p, proc;
444 register char *bp = buff;
445 int i = 0;
446 int doingzomb = 0;
447 struct eproc eproc;
448 struct pgrp pgrp;
449 struct session sess;
450 struct tty tty;
451 #ifndef NEWVM
452 struct text text;
453 #endif
454
455 /* allproc */
456 if (kvm_read((void *) nl[X_ALLPROC].n_value, &p,
457 sizeof (struct proc *)) != sizeof (struct proc *)) {
458 seterr("can't read allproc");
459 return (-1);
460 }
461
462 again:
463 for (; p; p = proc.p_nxt) {
464 if (kvm_read(p, &proc, sizeof (struct proc)) !=
465 sizeof (struct proc)) {
466 seterr("can't read proc at %x", p);
467 return (-1);
468 }
469 #ifdef NEWVM
470 if (kvm_read(proc.p_cred, &eproc.e_pcred,
471 sizeof (struct pcred)) == sizeof (struct pcred))
472 (void) kvm_read(eproc.e_pcred.pc_ucred, &eproc.e_ucred,
473 sizeof (struct ucred));
474 switch(ki_op(what)) {
475
476 case KINFO_PROC_PID:
477 if (proc.p_pid != (pid_t)arg)
478 continue;
479 break;
480
481
482 case KINFO_PROC_UID:
483 if (eproc.e_ucred.cr_uid != (uid_t)arg)
484 continue;
485 break;
486
487 case KINFO_PROC_RUID:
488 if (eproc.e_pcred.p_ruid != (uid_t)arg)
489 continue;
490 break;
491 }
492 #else
493 switch(ki_op(what)) {
494
495 case KINFO_PROC_PID:
496 if (proc.p_pid != (pid_t)arg)
497 continue;
498 break;
499
500
501 case KINFO_PROC_UID:
502 if (proc.p_uid != (uid_t)arg)
503 continue;
504 break;
505
506 case KINFO_PROC_RUID:
507 if (proc.p_ruid != (uid_t)arg)
508 continue;
509 break;
510 }
511 #endif
512 /*
513 * gather eproc
514 */
515 eproc.e_paddr = p;
516 if (kvm_read(proc.p_pgrp, &pgrp, sizeof (struct pgrp)) !=
517 sizeof (struct pgrp)) {
518 seterr("can't read pgrp at %x", proc.p_pgrp);
519 return (-1);
520 }
521 eproc.e_sess = pgrp.pg_session;
522 eproc.e_pgid = pgrp.pg_id;
523 eproc.e_jobc = pgrp.pg_jobc;
524 if (kvm_read(pgrp.pg_session, &sess, sizeof (struct session))
525 != sizeof (struct session)) {
526 seterr("can't read session at %x", pgrp.pg_session);
527 return (-1);
528 }
529 if ((proc.p_flag&SCTTY) && sess.s_ttyp != NULL) {
530 if (kvm_read(sess.s_ttyp, &tty, sizeof (struct tty))
531 != sizeof (struct tty)) {
532 seterr("can't read tty at %x", sess.s_ttyp);
533 return (-1);
534 }
535 eproc.e_tdev = tty.t_dev;
536 eproc.e_tsess = tty.t_session;
537 if (tty.t_pgrp != NULL) {
538 if (kvm_read(tty.t_pgrp, &pgrp, sizeof (struct
539 pgrp)) != sizeof (struct pgrp)) {
540 seterr("can't read tpgrp at &x",
541 tty.t_pgrp);
542 return (-1);
543 }
544 eproc.e_tpgid = pgrp.pg_id;
545 } else
546 eproc.e_tpgid = -1;
547 } else
548 eproc.e_tdev = NODEV;
549 if (proc.p_wmesg)
550 kvm_read(proc.p_wmesg, eproc.e_wmesg, WMESGLEN);
551 #ifdef NEWVM
552 (void) kvm_read(proc.p_vmspace, &eproc.e_vm,
553 sizeof (struct vmspace));
554 eproc.e_xsize = eproc.e_xrssize =
555 eproc.e_xccount = eproc.e_xswrss = 0;
556 #else
557 if (proc.p_textp) {
558 kvm_read(proc.p_textp, &text, sizeof (text));
559 eproc.e_xsize = text.x_size;
560 eproc.e_xrssize = text.x_rssize;
561 eproc.e_xccount = text.x_ccount;
562 eproc.e_xswrss = text.x_swrss;
563 } else {
564 eproc.e_xsize = eproc.e_xrssize =
565 eproc.e_xccount = eproc.e_xswrss = 0;
566 }
567 #endif
568
569 switch(ki_op(what)) {
570
571 case KINFO_PROC_PGRP:
572 if (eproc.e_pgid != (pid_t)arg)
573 continue;
574 break;
575
576 case KINFO_PROC_TTY:
577 if ((proc.p_flag&SCTTY) == 0 ||
578 eproc.e_tdev != (dev_t)arg)
579 continue;
580 break;
581 }
582
583 i++;
584 bcopy(&proc, bp, sizeof (struct proc));
585 bp += sizeof (struct proc);
586 bcopy(&eproc, bp, sizeof (struct eproc));
587 bp+= sizeof (struct eproc);
588 }
589 if (!doingzomb) {
590 /* zombproc */
591 if (kvm_read((void *) nl[X_ZOMBPROC].n_value, &p,
592 sizeof (struct proc *)) != sizeof (struct proc *)) {
593 seterr("can't read zombproc");
594 return (-1);
595 }
596 doingzomb = 1;
597 goto again;
598 }
599
600 return (i);
601 }
602
603 struct proc *
604 kvm_nextproc()
605 {
606
607 if (!kvmprocbase && kvm_getprocs(0, 0) == -1)
608 return (NULL);
609 if (kvmprocptr >= (kvmprocbase + kvmnprocs)) {
610 seterr("end of proc list");
611 return (NULL);
612 }
613 return((struct proc *)(kvmprocptr++));
614 }
615
616 struct eproc *
617 kvm_geteproc(p)
618 const struct proc *p;
619 {
620 return ((struct eproc *)(((char *)p) + sizeof (struct proc)));
621 }
622
623 kvm_setproc()
624 {
625 kvmprocptr = kvmprocbase;
626 }
627
628 kvm_freeprocs()
629 {
630
631 if (kvmprocbase) {
632 free(kvmprocbase);
633 kvmprocbase = NULL;
634 }
635 }
636
637 #ifdef NEWVM
638 struct user *
639 kvm_getu(p)
640 const struct proc *p;
641 {
642 register struct kinfo_proc *kp = (struct kinfo_proc *)p;
643 register int i;
644 register char *up;
645
646 if (kvminit == 0 && kvm_init(NULL, NULL, NULL, 0) == -1)
647 return (NULL);
648 if (p->p_stat == SZOMB) {
649 seterr("zombie process");
650 return (NULL);
651 }
652 /*
653 * Reading from swap is too complicated right now.
654 */
655 if ((p->p_flag & SLOAD) == 0)
656 return(NULL);
657 /*
658 * Read u-area one page at a time for the benefit of post-mortems
659 */
660 up = (char *) p->p_addr;
661 for (i = 0; i < UPAGES; i++) {
662 klseek(kmem, (long)up, 0);
663 if (read(kmem, user.upages[i], CLBYTES) != CLBYTES) {
664 seterr("cant read page %x of u of pid %d from %s",
665 up, p->p_pid, kmemf);
666 return(NULL);
667 }
668 up += CLBYTES;
669 }
670 pcbpf = (int) btop(p->p_addr); /* what should this be really? */
671 /*
672 * Conjure up a physical address for the arguments.
673 */
674 argaddr0 = argaddr1 = 0;
675 #ifdef hp300
676 if (kp->kp_eproc.e_vm.vm_pmap.pm_ptab) {
677 struct pte pte[CLSIZE*2];
678
679 klseek(kmem,
680 (long)&kp->kp_eproc.e_vm.vm_pmap.pm_ptab
681 [btoc(USRSTACK-CLBYTES*2)], 0);
682 if (read(kmem, (char *)&pte, sizeof(pte)) == sizeof(pte)) {
683 #if CLBYTES < 2048
684 argaddr0 = ctob(pftoc(pte[CLSIZE*0].pg_pfnum));
685 #endif
686 argaddr1 = ctob(pftoc(pte[CLSIZE*1].pg_pfnum));
687 }
688 }
689 kp->kp_eproc.e_vm.vm_rssize =
690 kp->kp_eproc.e_vm.vm_pmap.pm_stats.resident_count; /* XXX */
691 #endif
692
693 #ifdef i386
694 if (kp->kp_eproc.e_vm.vm_pmap.pm_pdir) {
695 struct pde pde;
696
697 klseek(kmem,
698 (long)(kp->kp_eproc.e_vm.vm_pmap.pm_pdir + UPTDI), 0);
699 if (read(kmem, (char *)&pde, sizeof pde) == sizeof pde &&
700 pde.pd_v) {
701
702 struct pte pte;
703
704 lseek(mem, (long)ctob(pde.pd_pfnum) +
705 (ptei(USRSTACK-CLBYTES) * sizeof pte), 0);
706 if (read(mem, (char *)&pte, sizeof pte) == sizeof pte && + pte.pg_v) {
707 argaddr1 = (long)ctob(pte.pg_pfnum);
708 }
709 }
710 }
711 #endif
712 return(&user.user);
713 }
714 #else
715 struct user *
716 kvm_getu(p)
717 const struct proc *p;
718 {
719 struct pte *pteaddr, apte;
720 struct pte arguutl[HIGHPAGES+(CLSIZE*2)];
721 register int i;
722 int ncl;
723
724 if (kvminit == 0 && kvm_init(NULL, NULL, NULL, 0) == -1)
725 return (NULL);
726 if (p->p_stat == SZOMB) {
727 seterr("zombie process");
728 return (NULL);
729 }
730 if ((p->p_flag & SLOAD) == 0) {
731 if (swap < 0) {
732 seterr("no swap");
733 return (NULL);
734 }
735 (void) lseek(swap, (long)dtob(p->p_swaddr), 0);
736 if (read(swap, (char *)&user.user, sizeof (struct user)) !=
737 sizeof (struct user)) {
738 seterr("can't read u for pid %d from %s",
739 p->p_pid, swapf);
740 return (NULL);
741 }
742 pcbpf = 0;
743 argaddr0 = 0;
744 argaddr1 = 0;
745 return (&user.user);
746 }
747 pteaddr = &Usrptmap[btokmx(p->p_p0br) + p->p_szpt - 1];
748 klseek(kmem, (long)pteaddr, 0);
749 if (read(kmem, (char *)&apte, sizeof(apte)) != sizeof(apte)) {
750 seterr("can't read indir pte to get u for pid %d from %s",
751 p->p_pid, kmemf);
752 return (NULL);
753 }
754 lseek(mem, (long)ctob(pftoc(apte.pg_pfnum+1)) - sizeof(arguutl), 0);
755 if (read(mem, (char *)arguutl, sizeof(arguutl)) != sizeof(arguutl)) {
756 seterr("can't read page table for u of pid %d from %s",
757 p->p_pid, memf);
758 return (NULL);
759 }
760 if (arguutl[0].pg_fod == 0 && arguutl[0].pg_pfnum)
761 argaddr0 = ctob(pftoc(arguutl[0].pg_pfnum));
762 else
763 argaddr0 = 0;
764 if (arguutl[CLSIZE*1].pg_fod == 0 && arguutl[CLSIZE*1].pg_pfnum)
765 argaddr1 = ctob(pftoc(arguutl[CLSIZE*1].pg_pfnum));
766 else
767 argaddr1 = 0;
768 pcbpf = arguutl[CLSIZE*2].pg_pfnum;
769 ncl = (sizeof (struct user) + CLBYTES - 1) / CLBYTES;
770 while (--ncl >= 0) {
771 i = ncl * CLSIZE;
772 lseek(mem,
773 (long)ctob(pftoc(arguutl[(CLSIZE*2)+i].pg_pfnum)), 0);
774 if (read(mem, user.upages[i], CLBYTES) != CLBYTES) {
775 seterr("can't read page %d of u of pid %d from %s",
776 arguutl[(CLSIZE*2)+i].pg_pfnum, p->p_pid, memf);
777 return(NULL);
778 }
779 }
780 return (&user.user);
781 }
782 #endif
783
784 char *
785 kvm_getargs(p, up)
786 const struct proc *p;
787 const struct user *up;
788 {
789 static char cmdbuf[CLBYTES*2];
790 static union {
791 char argc[CLBYTES*2];
792 int argi[CLBYTES*2/sizeof (int)];
793 } argspac;
794 register char *cp;
795 register int *ip;
796 char c;
797 int nbad;
798 #ifndef NEWVM
799 struct dblock db;
800 #endif
801 const char *file;
802 int stkoff = 0;
803
804 #if defined(NEWVM) && defined(hp300)
805 stkoff = 20; /* XXX for sigcode */
806 #endif
807 if (up == NULL || p->p_pid == 0 || p->p_pid == 2)
808 goto retucomm;
809 if ((p->p_flag & SLOAD) == 0 || argaddr1 == 0) {
810 #ifdef NEWVM
811 goto retucomm; /* XXX for now */
812 #else
813 if (swap < 0 || p->p_ssize == 0)
814 goto retucomm;
815 vstodb(0, CLSIZE, &up->u_smap, &db, 1);
816 (void) lseek(swap, (long)dtob(db.db_base), 0);
817 if (read(swap, (char *)&argspac.argc[CLBYTES], CLBYTES)
818 != CLBYTES)
819 goto bad;
820 vstodb(1, CLSIZE, &up->u_smap, &db, 1);
821 (void) lseek(swap, (long)dtob(db.db_base), 0);
822 if (read(swap, (char *)&argspac.argc[0], CLBYTES) != CLBYTES)
823 goto bad;
824 file = swapf;
825 #endif
826 } else {
827 if (argaddr0) {
828 lseek(mem, (long)argaddr0, 0);
829 if (read(mem, (char *)&argspac, CLBYTES) != CLBYTES)
830 goto bad;
831 } else
832 bzero(&argspac, CLBYTES);
833 lseek(mem, (long)argaddr1, 0);
834 if (read(mem, &argspac.argc[CLBYTES], CLBYTES) != CLBYTES)
835 goto bad;
836 file = (char *) memf;
837 }
838 #ifdef i386
839 ip = &argspac.argi[(CLBYTES + CLBYTES/2)/sizeof (int)];
840 #else
841 ip = &argspac.argi[CLBYTES*2/sizeof (int)];
842 ip -= 2; /* last arg word and .long 0 */
843 ip -= stkoff / sizeof (int);
844 while (*--ip) {
845 if (ip == argspac.argi)
846 goto retucomm;
847 }
848 *(char *)ip = ' ';
849 ip++;
850 nbad = 0;
851 #endif
852 for (cp = (char *)ip; cp < &argspac.argc[CLBYTES*2-stkoff]; cp++) {
853 c = *cp & 0177;
854 if (c == 0)
855 *cp = ' ';
856 else if (c < ' ' || c > 0176) {
857 if (++nbad >= 5*(0+1)) { /* eflg -> 0 XXX */
858 *cp++ = ' ';
859 break;
860 }
861 *cp = '?';
862 } else if (0 == 0 && c == '=') { /* eflg -> 0 XXX */
863 while (*--cp != ' ')
864 if (cp <= (char *)ip)
865 break;
866 break;
867 }
868 }
869 *cp = 0;
870 while (*--cp == ' ')
871 *cp = 0;
872 cp = (char *)ip;
873 (void) strncpy(cmdbuf, cp, &argspac.argc[CLBYTES*2] - cp);
874 if (cp[0] == '-' || cp[0] == '?' || cp[0] <= ' ') {
875 (void) strcat(cmdbuf, " (");
876 (void) strncat(cmdbuf, p->p_comm, sizeof(p->p_comm));
877 (void) strcat(cmdbuf, ")");
878 }
879 return (cmdbuf);
880
881 bad:
882 seterr("error locating command name for pid %d from %s",
883 p->p_pid, file);
884 retucomm:
885 (void) strcpy(cmdbuf, " (");
886 (void) strncat(cmdbuf, p->p_comm, sizeof (p->p_comm));
887 (void) strcat(cmdbuf, ")");
888 return (cmdbuf);
889 }
890
891
892 static
893 getkvars()
894 {
895 if (kvm_nlist(nl) == -1)
896 return (-1);
897 if (deadkernel) {
898 /* We must do the sys map first because klseek uses it */
899 long addr;
900
901 #ifndef NEWVM
902 Syssize = nl[X_SYSSIZE].n_value;
903 Sysmap = (struct pte *)
904 calloc((unsigned) Syssize, sizeof (struct pte));
905 if (Sysmap == NULL) {
906 seterr("out of space for Sysmap");
907 return (-1);
908 }
909 addr = (long) nl[X_SYSMAP].n_value;
910 addr &= ~KERNBASE;
911 (void) lseek(kmem, addr, 0);
912 if (read(kmem, (char *) Sysmap, Syssize * sizeof (struct pte))
913 != Syssize * sizeof (struct pte)) {
914 seterr("can't read Sysmap");
915 return (-1);
916 }
917 #endif
918 #if defined(hp300)
919 addr = (long) nl[X_LOWRAM].n_value;
920 (void) lseek(kmem, addr, 0);
921 if (read(kmem, (char *) &lowram, sizeof (lowram))
922 != sizeof (lowram)) {
923 seterr("can't read lowram");
924 return (-1);
925 }
926 lowram = btop(lowram);
927 Sysseg = (struct ste *) malloc(NBPG);
928 if (Sysseg == NULL) {
929 seterr("out of space for Sysseg");
930 return (-1);
931 }
932 addr = (long) nl[X_SYSSEG].n_value;
933 (void) lseek(kmem, addr, 0);
934 read(kmem, (char *)&addr, sizeof(addr));
935 (void) lseek(kmem, (long)addr, 0);
936 if (read(kmem, (char *) Sysseg, NBPG) != NBPG) {
937 seterr("can't read Sysseg");
938 return (-1);
939 }
940 #endif
941 #if defined(i386)
942 PTD = (struct pde *) malloc(NBPG);
943 if (PTD == NULL) {
944 seterr("out of space for PTD");
945 return (-1);
946 }
947 addr = (long) nl[X_IdlePTD].n_value;
948 (void) lseek(kmem, addr, 0);
949 read(kmem, (char *)&addr, sizeof(addr));
950 (void) lseek(kmem, (long)addr, 0);
951 if (read(kmem, (char *) PTD, NBPG) != NBPG) {
952 seterr("can't read PTD");
953 return (-1);
954 }
955 #endif
956 }
957 #ifndef NEWVM
958 usrpt = (struct pte *)nl[X_USRPT].n_value;
959 Usrptmap = (struct pte *)nl[X_USRPTMAP].n_value;
960 #endif
961 if (kvm_read((void *) nl[X_NSWAP].n_value, &nswap, sizeof (long)) !=
962 sizeof (long)) {
963 seterr("can't read nswap");
964 return (-1);
965 }
966 if (kvm_read((void *) nl[X_DMMIN].n_value, &dmmin, sizeof (long)) !=
967 sizeof (long)) {
968 seterr("can't read dmmin");
969 return (-1);
970 }
971 if (kvm_read((void *) nl[X_DMMAX].n_value, &dmmax, sizeof (long)) !=
972 sizeof (long)) {
973 seterr("can't read dmmax");
974 return (-1);
975 }
976 return (0);
977 }
978
979 kvm_read(loc, buf, len)
980 void *loc;
981 void *buf;
982 {
983 if (kvmfilesopen == 0 && kvm_openfiles(NULL, NULL, NULL) == -1)
984 return (-1);
985 if (iskva(loc)) {
986 klseek(kmem, (off_t) loc, 0);
987 if (read(kmem, buf, len) != len) {
988 seterr("error reading kmem at %x", loc);
989 return (-1);
990 }
991 } else {
992 lseek(mem, (off_t) loc, 0);
993 if (read(mem, buf, len) != len) {
994 seterr("error reading mem at %x", loc);
995 return (-1);
996 }
997 }
998 return (len);
999 }
1000
1001 static void
1002 klseek(fd, loc, off)
1003 int fd;
1004 off_t loc;
1005 int off;
1006 {
1007
1008 if (deadkernel) {
1009 if ((loc = Vtophys(loc)) == -1)
1010 return;
1011 }
1012 (void) lseek(fd, (off_t)loc, off);
1013 }
1014
1015 #ifndef NEWVM
1016 /*
1017 * Given a base/size pair in virtual swap area,
1018 * return a physical base/size pair which is the
1019 * (largest) initial, physically contiguous block.
1020 */
1021 static void
1022 vstodb(vsbase, vssize, dmp, dbp, rev)
1023 register int vsbase;
1024 int vssize;
1025 struct dmap *dmp;
1026 register struct dblock *dbp;
1027 {
1028 register int blk = dmmin;
1029 register swblk_t *ip = dmp->dm_map;
1030
1031 vsbase = ctod(vsbase);
1032 vssize = ctod(vssize);
1033 if (vsbase < 0 || vsbase + vssize > dmp->dm_size)
1034 /*panic("vstodb")*/;
1035 while (vsbase >= blk) {
1036 vsbase -= blk;
1037 if (blk < dmmax)
1038 blk *= 2;
1039 ip++;
1040 }
1041 if (*ip <= 0 || *ip + blk > nswap)
1042 /*panic("vstodb")*/;
1043 dbp->db_size = MIN(vssize, blk - vsbase);
1044 dbp->db_base = *ip + (rev ? blk - (vsbase + dbp->db_size) : vsbase);
1045 }
1046 #endif
1047
1048 #ifdef NEWVM
1049 static off_t
1050 Vtophys(loc)
1051 u_long loc;
1052 {
1053 off_t newloc = (off_t) -1;
1054 #ifdef hp300
1055 int p, ste, pte;
1056
1057 ste = *(int *)&Sysseg[loc >> SG_ISHIFT];
1058 if ((ste & SG_V) == 0) {
1059 seterr("vtophys: segment not valid");
1060 return((off_t) -1);
1061 }
1062 p = btop(loc & SG_PMASK);
1063 newloc = (ste & SG_FRAME) + (p * sizeof(struct pte));
1064 (void) lseek(kmem, (long)(newloc-(off_t)ptob(lowram)), 0);
1065 if (read(kmem, (char *)&pte, sizeof pte) != sizeof pte) {
1066 seterr("vtophys: cannot locate pte");
1067 return((off_t) -1);
1068 }
1069 newloc = pte & PG_FRAME;
1070 if (pte == PG_NV || newloc < (off_t)ptob(lowram)) {
1071 seterr("vtophys: page not valid");
1072 return((off_t) -1);
1073 }
1074 newloc = (newloc - (off_t)ptob(lowram)) + (loc & PGOFSET);
1075 #endif
1076 #ifdef i386
1077 struct pde pde;
1078 struct pte pte;
1079 int p;
1080
1081 pde = PTD[loc >> PD_SHIFT];
1082 if (pde.pd_v == 0) {
1083 seterr("vtophys: page directory entry not valid");
1084 return((off_t) -1);
1085 }
1086 p = btop(loc & PT_MASK);
1087 newloc = pde.pd_pfnum + (p * sizeof(struct pte));
1088 (void) lseek(kmem, (long)newloc, 0);
1089 if (read(kmem, (char *)&pte, sizeof pte) != sizeof pte) {
1090 seterr("vtophys: cannot obtain desired pte");
1091 return((off_t) -1);
1092 }
1093 newloc = pte.pg_pfnum;
1094 if (pte.pg_v == 0) {
1095 seterr("vtophys: page table entry not valid");
1096 return((off_t) -1);
1097 }
1098 newloc += (loc & PGOFSET);
1099 #endif
1100 return((off_t) newloc);
1101 }
1102 #else
1103 static off_t
1104 vtophys(loc)
1105 long loc;
1106 {
1107 int p;
1108 off_t newloc;
1109 register struct pte *pte;
1110
1111 newloc = loc & ~KERNBASE;
1112 p = btop(newloc);
1113 #if defined(vax) || defined(tahoe)
1114 if ((loc & KERNBASE) == 0) {
1115 seterr("vtophys: translating non-kernel address");
1116 return((off_t) -1);
1117 }
1118 #endif
1119 if (p >= Syssize) {
1120 seterr("vtophys: page out of bound (%d>=%d)", p, Syssize);
1121 return((off_t) -1);
1122 }
1123 pte = &Sysmap[p];
1124 if (pte->pg_v == 0 && (pte->pg_fod || pte->pg_pfnum == 0)) {
1125 seterr("vtophys: page not valid");
1126 return((off_t) -1);
1127 }
1128 #if defined(hp300)
1129 if (pte->pg_pfnum < lowram) {
1130 seterr("vtophys: non-RAM page (%d<%d)", pte->pg_pfnum, lowram);
1131 return((off_t) -1);
1132 }
1133 #endif
1134 loc = (long) (ptob(pftoc(pte->pg_pfnum)) + (loc & PGOFSET));
1135 return(loc);
1136 }
1137 #endif
1138
1139 #include <varargs.h>
1140 static char errbuf[_POSIX2_LINE_MAX];
1141
1142 static void
1143 seterr(va_alist)
1144 va_dcl
1145 {
1146 char *fmt;
1147 va_list ap;
1148
1149 va_start(ap);
1150 fmt = va_arg(ap, char *);
1151 (void) vsnprintf(errbuf, _POSIX2_LINE_MAX, fmt, ap);
1152 va_end(ap);
1153 }
1154
1155 static void
1156 setsyserr(va_alist)
1157 va_dcl
1158 {
1159 char *fmt, *cp;
1160 va_list ap;
1161 extern int errno;
1162
1163 va_start(ap);
1164 fmt = va_arg(ap, char *);
1165 (void) vsnprintf(errbuf, _POSIX2_LINE_MAX, fmt, ap);
1166 for (cp=errbuf; *cp; cp++)
1167 ;
1168 snprintf(cp, _POSIX2_LINE_MAX - (cp - errbuf), ": %s", strerror(errno));
1169 va_end(ap);
1170 }
1171
1172 char *
1173 kvm_geterr()
1174 {
1175 return (errbuf);
1176 }
1177