kvm.c revision 1.42 1 /* $NetBSD: kvm.c,v 1.42 1996/03/18 22:33:13 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1989, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software developed by the Computer Systems
8 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
9 * BG 91-66 and contributed to Berkeley.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40 #if defined(LIBC_SCCS) && !defined(lint)
41 #if 0
42 static char sccsid[] = "@(#)kvm.c 8.2 (Berkeley) 2/13/94";
43 #else
44 static char *rcsid = "$NetBSD: kvm.c,v 1.42 1996/03/18 22:33:13 thorpej Exp $";
45 #endif
46 #endif /* LIBC_SCCS and not lint */
47
48 #include <sys/param.h>
49 #include <sys/user.h>
50 #include <sys/proc.h>
51 #include <sys/ioctl.h>
52 #include <sys/stat.h>
53 #include <sys/sysctl.h>
54
55 #include <sys/core.h>
56 #include <sys/exec_aout.h>
57 #include <sys/kcore.h>
58
59 #include <vm/vm.h>
60 #include <vm/vm_param.h>
61 #include <vm/swap_pager.h>
62
63 #include <machine/vmparam.h>
64 #include <machine/kcore.h>
65
66 #include <ctype.h>
67 #include <db.h>
68 #include <fcntl.h>
69 #include <limits.h>
70 #include <nlist.h>
71 #include <paths.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
75 #include <unistd.h>
76 #include <kvm.h>
77
78 #include "kvm_private.h"
79
80 static int kvm_dbopen __P((kvm_t *, const char *));
81 static int _kvm_get_header __P((kvm_t *));
82 static kvm_t *_kvm_open __P((kvm_t *, const char *, const char *,
83 const char *, int, char *));
84 static int clear_gap __P((kvm_t *, FILE *, int));
85 static off_t Lseek __P((kvm_t *, int, off_t, int));
86 static ssize_t Read __P(( kvm_t *, int, void *, size_t));
87
88 char *
89 kvm_geterr(kd)
90 kvm_t *kd;
91 {
92 return (kd->errbuf);
93 }
94
95 #if __STDC__
96 #include <stdarg.h>
97 #else
98 #include <varargs.h>
99 #endif
100
101 /*
102 * Report an error using printf style arguments. "program" is kd->program
103 * on hard errors, and 0 on soft errors, so that under sun error emulation,
104 * only hard errors are printed out (otherwise, programs like gdb will
105 * generate tons of error messages when trying to access bogus pointers).
106 */
107 void
108 #if __STDC__
109 _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
110 #else
111 _kvm_err(kd, program, fmt, va_alist)
112 kvm_t *kd;
113 char *program, *fmt;
114 va_dcl
115 #endif
116 {
117 va_list ap;
118
119 #ifdef __STDC__
120 va_start(ap, fmt);
121 #else
122 va_start(ap);
123 #endif
124 if (program != NULL) {
125 (void)fprintf(stderr, "%s: ", program);
126 (void)vfprintf(stderr, fmt, ap);
127 (void)fputc('\n', stderr);
128 } else
129 (void)vsnprintf(kd->errbuf,
130 sizeof(kd->errbuf), (char *)fmt, ap);
131
132 va_end(ap);
133 }
134
135 void
136 #if __STDC__
137 _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
138 #else
139 _kvm_syserr(kd, program, fmt, va_alist)
140 kvm_t *kd;
141 char *program, *fmt;
142 va_dcl
143 #endif
144 {
145 va_list ap;
146 register int n;
147
148 #if __STDC__
149 va_start(ap, fmt);
150 #else
151 va_start(ap);
152 #endif
153 if (program != NULL) {
154 (void)fprintf(stderr, "%s: ", program);
155 (void)vfprintf(stderr, fmt, ap);
156 (void)fprintf(stderr, ": %s\n", strerror(errno));
157 } else {
158 register char *cp = kd->errbuf;
159
160 (void)vsnprintf(cp, sizeof(kd->errbuf), (char *)fmt, ap);
161 n = strlen(cp);
162 (void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
163 strerror(errno));
164 }
165 va_end(ap);
166 }
167
168 void *
169 _kvm_malloc(kd, n)
170 register kvm_t *kd;
171 register size_t n;
172 {
173 void *p;
174
175 if ((p = malloc(n)) == NULL)
176 _kvm_err(kd, kd->program, strerror(errno));
177 return (p);
178 }
179
180 /*
181 * Wrappers for Lseek/Read system calls. They check for errors and
182 * call _kvm_syserr() if appropriate.
183 */
184 static off_t
185 Lseek(kd, fd, offset, whence)
186 kvm_t *kd;
187 int fd, whence;
188 off_t offset;
189 {
190 off_t off;
191
192 errno = 0;
193 if ((off = lseek(fd, offset, whence)) == -1 && errno != 0) {
194 _kvm_syserr(kd, kd->program, "Lseek");
195 return (-1);
196 }
197 return (off);
198 }
199
200 static ssize_t
201 Read(kd, fd, buf, nbytes)
202 kvm_t *kd;
203 int fd;
204 void *buf;
205 size_t nbytes;
206 {
207 ssize_t rv;
208
209 errno = 0;
210
211 if ((rv = read(fd, buf, nbytes)) != nbytes && errno != 0)
212 _kvm_syserr(kd, kd->program, "Read");
213 return (rv);
214 }
215
216 static kvm_t *
217 _kvm_open(kd, uf, mf, sf, flag, errout)
218 register kvm_t *kd;
219 const char *uf;
220 const char *mf;
221 const char *sf;
222 int flag;
223 char *errout;
224 {
225 struct stat st;
226
227 kd->db = 0;
228 kd->pmfd = -1;
229 kd->vmfd = -1;
230 kd->swfd = -1;
231 kd->nlfd = -1;
232 kd->procbase = 0;
233 kd->nbpg = getpagesize();
234 kd->swapspc = 0;
235 kd->argspc = 0;
236 kd->argbuf = 0;
237 kd->argv = 0;
238 kd->vmst = 0;
239 kd->vm_page_buckets = 0;
240 kd->kcore_hdr = 0;
241 kd->cpu_hdr = 0;
242 kd->dump_off = 0;
243
244 if (uf == 0)
245 uf = _PATH_UNIX;
246 else if (strlen(uf) >= MAXPATHLEN) {
247 _kvm_err(kd, kd->program, "exec file name too long");
248 goto failed;
249 }
250 if (flag & ~O_RDWR) {
251 _kvm_err(kd, kd->program, "bad flags arg");
252 goto failed;
253 }
254 if (mf == 0)
255 mf = _PATH_MEM;
256 if (sf == 0)
257 sf = _PATH_DRUM;
258
259 if ((kd->pmfd = open(mf, flag, 0)) < 0) {
260 _kvm_syserr(kd, kd->program, "%s", mf);
261 goto failed;
262 }
263 if (fstat(kd->pmfd, &st) < 0) {
264 _kvm_syserr(kd, kd->program, "%s", mf);
265 goto failed;
266 }
267 if (S_ISCHR(st.st_mode)) {
268 /*
269 * If this is a character special device, then check that
270 * it's /dev/mem. If so, open kmem too. (Maybe we should
271 * make it work for either /dev/mem or /dev/kmem -- in either
272 * case you're working with a live kernel.)
273 */
274 if (strcmp(mf, _PATH_MEM) != 0) { /* XXX */
275 _kvm_err(kd, kd->program,
276 "%s: not physical memory device", mf);
277 goto failed;
278 }
279 if ((kd->vmfd = open(_PATH_KMEM, flag)) < 0) {
280 _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
281 goto failed;
282 }
283 if ((kd->swfd = open(sf, flag, 0)) < 0) {
284 _kvm_syserr(kd, kd->program, "%s", sf);
285 goto failed;
286 }
287 /*
288 * Open kvm nlist database. We go ahead and do this
289 * here so that we don't have to hold on to the vmunix
290 * path name. Since a kvm application will surely do
291 * a kvm_nlist(), this probably won't be a wasted effort.
292 * If the database cannot be opened, open the namelist
293 * argument so we revert to slow nlist() calls.
294 */
295 if (kvm_dbopen(kd, uf) < 0 &&
296 (kd->nlfd = open(uf, O_RDONLY, 0)) < 0) {
297 _kvm_syserr(kd, kd->program, "%s", uf);
298 goto failed;
299 }
300 } else {
301 /*
302 * This is a crash dump.
303 * Initalize the virtual address translation machinery,
304 * but first setup the namelist fd.
305 */
306 if ((kd->nlfd = open(uf, O_RDONLY, 0)) < 0) {
307 _kvm_syserr(kd, kd->program, "%s", uf);
308 goto failed;
309 }
310
311 /*
312 * If there is no valid core header, fail silently here.
313 * The address translations however will fail without
314 * header. Things can be made to run by calling
315 * kvm_dump_mkheader() before doing any translation.
316 */
317 if (_kvm_get_header(kd) == 0) {
318 if (_kvm_initvtop(kd) < 0)
319 goto failed;
320 }
321 }
322 return (kd);
323 failed:
324 /*
325 * Copy out the error if doing sane error semantics.
326 */
327 if (errout != 0)
328 strcpy(errout, kd->errbuf);
329 (void)kvm_close(kd);
330 return (0);
331 }
332
333 static int
334 _kvm_get_header(kd)
335 kvm_t *kd;
336 {
337 cpu_kcore_hdr_t ckhdr;
338 kcore_hdr_t khdr;
339 kcore_seg_t seghdr;
340 off_t offset;
341
342 if (Lseek(kd, kd->pmfd, (off_t)0, SEEK_SET) == -1)
343 return (-1);
344
345 if (Read(kd, kd->pmfd, &khdr, sizeof(khdr)) != sizeof(khdr))
346 return (-1);
347 offset = khdr.c_hdrsize;
348
349 /*
350 * Currently, we only support dump-files made by the current
351 * architecture...
352 */
353 if ((CORE_GETMAGIC(khdr) != KCORE_MAGIC)
354 || ((CORE_GETMID(khdr) != MID_MACHINE)))
355 return (-1);
356
357 /*
358 * Currently, we only support exactly 2 segments: cpu-segment
359 * and data-segment in exactly that order.
360 */
361 if (khdr.c_nseg != 2)
362 return (-1);
363
364 /*
365 * Read the next segment header: cpu segment
366 */
367 if (Lseek(kd, kd->pmfd, (off_t)offset, SEEK_SET) == -1)
368 return (-1);
369 if (Read(kd, kd->pmfd, &seghdr, sizeof(seghdr)) != sizeof(seghdr))
370 return (-1);
371 if (CORE_GETMAGIC(seghdr) != KCORESEG_MAGIC
372 || CORE_GETFLAG(seghdr) != CORE_CPU)
373 return (-1);
374 offset += khdr.c_seghdrsize;
375 if (Lseek(kd, kd->pmfd, (off_t)offset, SEEK_SET) == -1)
376 return (-1);
377 if (Read(kd, kd->pmfd, &ckhdr, sizeof(ckhdr)) != sizeof(ckhdr))
378 return (-1);
379 offset += seghdr.c_size;
380
381 /*
382 * Read the next segment header: data segment
383 */
384 if (Lseek(kd, kd->pmfd, (off_t)offset, SEEK_SET) == -1)
385 return (-1);
386 if (Read(kd, kd->pmfd, &seghdr, sizeof(seghdr)) != sizeof(seghdr))
387 return (-1);
388 offset += khdr.c_seghdrsize;
389
390 if (CORE_GETMAGIC(seghdr) != KCORESEG_MAGIC
391 || CORE_GETFLAG(seghdr) != CORE_DATA)
392 return (-1);
393
394 kd->kcore_hdr = (kcore_hdr_t *)_kvm_malloc(kd, sizeof(*kd->kcore_hdr));
395 if (kd->kcore_hdr == NULL)
396 return (-1);
397 kd->cpu_hdr = (cpu_kcore_hdr_t *)_kvm_malloc(kd, sizeof(*kd->cpu_hdr));
398 if (kd->cpu_hdr == NULL) {
399 free((void *)kd->kcore_hdr);
400 kd->kcore_hdr = NULL;
401 return (-1);
402 }
403
404 *kd->kcore_hdr = khdr;
405 *kd->cpu_hdr = ckhdr;
406 kd->dump_off = offset;
407 return (0);
408 }
409
410 /*
411 * Translate a physical address to a file-offset in the crash-dump.
412 */
413 off_t
414 _kvm_pa2off(kd, pa)
415 kvm_t *kd;
416 u_long pa;
417 {
418 off_t off;
419 phys_ram_seg_t *rsp;
420
421 off = 0;
422 for (rsp = kd->cpu_hdr->ram_segs; rsp->size; rsp++) {
423 if (pa >= rsp->start && pa < rsp->start + rsp->size) {
424 pa -= rsp->start;
425 break;
426 }
427 off += rsp->size;
428 }
429 return(pa + off + kd->dump_off);
430 }
431
432 int
433 kvm_dump_mkheader(kd, dump_off)
434 kvm_t *kd;
435 off_t dump_off;
436 {
437 kcore_hdr_t kch;
438 kcore_seg_t kseg;
439 cpu_kcore_hdr_t ckhdr;
440 int hdr_size;
441
442 hdr_size = 0;
443 if (kd->kcore_hdr != NULL) {
444 _kvm_err(kd, kd->program, "already has a dump header");
445 return (-1);
446 }
447 if (ISALIVE(kd)) {
448 _kvm_err(kd, kd->program, "don't use on live kernel");
449 return (-1);
450 }
451
452 /*
453 * Check for new format crash dump
454 */
455 if (Lseek(kd, kd->pmfd, dump_off, SEEK_SET) == -1)
456 return (-1);
457 if (Read(kd, kd->pmfd, &kseg, sizeof(kseg)) != sizeof(kseg))
458 return (-1);
459 if ((CORE_GETMAGIC(kseg) == KCORE_MAGIC)
460 && ((CORE_GETMID(kseg) == MID_MACHINE))) {
461 hdr_size += ALIGN(sizeof(kcore_seg_t));
462 if (Lseek(kd, kd->pmfd, dump_off+hdr_size, SEEK_SET) == -1)
463 return (-1);
464 if (Read(kd, kd->pmfd, &ckhdr, sizeof(ckhdr)) != sizeof(ckhdr))
465 return (-1);
466 hdr_size += kseg.c_size;
467 if (Lseek(kd, kd->pmfd, dump_off+hdr_size, SEEK_SET) == -1)
468 return (-1);
469 kd->cpu_hdr = (cpu_kcore_hdr_t *)
470 _kvm_malloc(kd, sizeof(cpu_kcore_hdr_t));
471 *kd->cpu_hdr = ckhdr;
472 }
473
474 /*
475 * Create a kcore_hdr.
476 */
477 kd->kcore_hdr = (kcore_hdr_t *) _kvm_malloc(kd, sizeof(kcore_hdr_t));
478 if (kd->kcore_hdr == NULL) {
479 if (kd->cpu_hdr != NULL) {
480 free((void *)kd->cpu_hdr);
481 kd->cpu_hdr = NULL;
482 }
483 return (-1);
484 }
485
486 kd->kcore_hdr->c_hdrsize = ALIGN(sizeof(kcore_hdr_t));
487 kd->kcore_hdr->c_seghdrsize = ALIGN(sizeof(kcore_seg_t));
488 kd->kcore_hdr->c_nseg = 2;
489 CORE_SETMAGIC(*(kd->kcore_hdr), KCORE_MAGIC, MID_MACHINE,0);
490
491 /*
492 * If there is no cpu_hdr at this point, we probably have an
493 * old format crash dump.....bail out
494 */
495 if (kd->cpu_hdr == NULL) {
496 free((void *)kd->kcore_hdr);
497 kd->kcore_hdr = NULL;
498 _kvm_err(kd, kd->program, "invalid dump");
499 }
500
501 kd->dump_off = dump_off + hdr_size;
502
503 /*
504 * Now that we have a valid header, enable translations.
505 */
506 _kvm_initvtop(kd);
507
508 return(hdr_size);
509 }
510
511 static int
512 clear_gap(kd, fp, size)
513 kvm_t *kd;
514 FILE *fp;
515 int size;
516 {
517 if (size <= 0) /* XXX - < 0 should never happen */
518 return (0);
519 while (size-- > 0) {
520 if (fputc(0, fp) == EOF) {
521 _kvm_syserr(kd, kd->program, "clear_gap");
522 return (-1);
523 }
524 }
525 return (0);
526 }
527
528 /*
529 * Write the dump header info to 'fp'. Note that we can't use fseek(3) here
530 * because 'fp' might be a file pointer obtained by zopen().
531 */
532 int
533 kvm_dump_wrtheader(kd, fp, dumpsize)
534 kvm_t *kd;
535 FILE *fp;
536 int dumpsize;
537 {
538 kcore_seg_t seghdr;
539 long offset;
540 int gap;
541
542 if (kd->kcore_hdr == NULL || kd->cpu_hdr == NULL) {
543 _kvm_err(kd, kd->program, "no valid dump header(s)");
544 return (-1);
545 }
546
547 /*
548 * Write the generic header
549 */
550 offset = 0;
551 if (fwrite((void*)kd->kcore_hdr, sizeof(kcore_hdr_t), 1, fp) <= 0) {
552 _kvm_syserr(kd, kd->program, "kvm_dump_wrtheader");
553 return (-1);
554 }
555 offset += kd->kcore_hdr->c_hdrsize;
556 gap = kd->kcore_hdr->c_hdrsize - sizeof(kcore_hdr_t);
557 if (clear_gap(kd, fp, gap) == -1)
558 return (-1);
559
560 /*
561 * Write the cpu header
562 */
563 CORE_SETMAGIC(seghdr, KCORESEG_MAGIC, 0, CORE_CPU);
564 seghdr.c_size = ALIGN(sizeof(cpu_kcore_hdr_t));
565 if (fwrite((void*)&seghdr, sizeof(seghdr), 1, fp) <= 0) {
566 _kvm_syserr(kd, kd->program, "kvm_dump_wrtheader");
567 return (-1);
568 }
569 offset += kd->kcore_hdr->c_seghdrsize;
570 gap = kd->kcore_hdr->c_seghdrsize - sizeof(seghdr);
571 if (clear_gap(kd, fp, gap) == -1)
572 return (-1);
573
574 if (fwrite((void*)kd->cpu_hdr, sizeof(cpu_kcore_hdr_t), 1, fp) <= 0) {
575 _kvm_syserr(kd, kd->program, "kvm_dump_wrtheader");
576 return (-1);
577 }
578 offset += seghdr.c_size;
579 gap = seghdr.c_size - sizeof(cpu_kcore_hdr_t);
580 if (clear_gap(kd, fp, gap) == -1)
581 return (-1);
582
583 /*
584 * Write the actual dump data segment header
585 */
586 CORE_SETMAGIC(seghdr, KCORESEG_MAGIC, 0, CORE_DATA);
587 seghdr.c_size = dumpsize;
588 if (fwrite((void*)&seghdr, sizeof(seghdr), 1, fp) <= 0) {
589 _kvm_syserr(kd, kd->program, "kvm_dump_wrtheader");
590 return (-1);
591 }
592 offset += kd->kcore_hdr->c_seghdrsize;
593 gap = kd->kcore_hdr->c_seghdrsize - sizeof(seghdr);
594 if (clear_gap(kd, fp, gap) == -1)
595 return (-1);
596
597 return (offset);
598 }
599
600 kvm_t *
601 kvm_openfiles(uf, mf, sf, flag, errout)
602 const char *uf;
603 const char *mf;
604 const char *sf;
605 int flag;
606 char *errout;
607 {
608 register kvm_t *kd;
609
610 if ((kd = malloc(sizeof(*kd))) == NULL) {
611 (void)strcpy(errout, strerror(errno));
612 return (0);
613 }
614 kd->program = 0;
615 return (_kvm_open(kd, uf, mf, sf, flag, errout));
616 }
617
618 kvm_t *
619 kvm_open(uf, mf, sf, flag, program)
620 const char *uf;
621 const char *mf;
622 const char *sf;
623 int flag;
624 const char *program;
625 {
626 register kvm_t *kd;
627
628 if ((kd = malloc(sizeof(*kd))) == NULL && program != NULL) {
629 (void)fprintf(stderr, "%s: %s\n", strerror(errno));
630 return (0);
631 }
632 kd->program = program;
633 return (_kvm_open(kd, uf, mf, sf, flag, NULL));
634 }
635
636 int
637 kvm_close(kd)
638 kvm_t *kd;
639 {
640 register int error = 0;
641
642 if (kd->pmfd >= 0)
643 error |= close(kd->pmfd);
644 if (kd->vmfd >= 0)
645 error |= close(kd->vmfd);
646 if (kd->nlfd >= 0)
647 error |= close(kd->nlfd);
648 if (kd->swfd >= 0)
649 error |= close(kd->swfd);
650 if (kd->db != 0)
651 error |= (kd->db->close)(kd->db);
652 if (kd->vmst)
653 _kvm_freevtop(kd);
654 if (kd->cpu_hdr != NULL)
655 free((void *)kd->cpu_hdr);
656 if (kd->kcore_hdr != NULL)
657 free((void *)kd->kcore_hdr);
658 if (kd->procbase != 0)
659 free((void *)kd->procbase);
660 if (kd->swapspc != 0)
661 free((void *)kd->swapspc);
662 if (kd->argspc != 0)
663 free((void *)kd->argspc);
664 if (kd->argbuf != 0)
665 free((void *)kd->argbuf);
666 if (kd->argv != 0)
667 free((void *)kd->argv);
668 free((void *)kd);
669
670 return (0);
671 }
672
673 /*
674 * Set up state necessary to do queries on the kernel namelist
675 * data base. If the data base is out-of-data/incompatible with
676 * given executable, set up things so we revert to standard nlist call.
677 * Only called for live kernels. Return 0 on success, -1 on failure.
678 */
679 static int
680 kvm_dbopen(kd, uf)
681 kvm_t *kd;
682 const char *uf;
683 {
684 char *cp;
685 DBT rec;
686 int dbversionlen;
687 struct nlist nitem;
688 char dbversion[_POSIX2_LINE_MAX];
689 char kversion[_POSIX2_LINE_MAX];
690 char dbname[MAXPATHLEN];
691
692 if ((cp = rindex(uf, '/')) != 0)
693 uf = cp + 1;
694
695 (void)snprintf(dbname, sizeof(dbname), "%skvm_%s.db", _PATH_VARDB, uf);
696 kd->db = dbopen(dbname, O_RDONLY, 0, DB_HASH, NULL);
697 if (kd->db == 0)
698 return (-1);
699 /*
700 * read version out of database
701 */
702 rec.data = VRS_KEY;
703 rec.size = sizeof(VRS_KEY) - 1;
704 if ((kd->db->get)(kd->db, (DBT *)&rec, (DBT *)&rec, 0))
705 goto close;
706 if (rec.data == 0 || rec.size > sizeof(dbversion))
707 goto close;
708
709 bcopy(rec.data, dbversion, rec.size);
710 dbversionlen = rec.size;
711 /*
712 * Read version string from kernel memory.
713 * Since we are dealing with a live kernel, we can call kvm_read()
714 * at this point.
715 */
716 rec.data = VRS_SYM;
717 rec.size = sizeof(VRS_SYM) - 1;
718 if ((kd->db->get)(kd->db, (DBT *)&rec, (DBT *)&rec, 0))
719 goto close;
720 if (rec.data == 0 || rec.size != sizeof(struct nlist))
721 goto close;
722 bcopy((char *)rec.data, (char *)&nitem, sizeof(nitem));
723 if (kvm_read(kd, (u_long)nitem.n_value, kversion, dbversionlen) !=
724 dbversionlen)
725 goto close;
726 /*
727 * If they match, we win - otherwise clear out kd->db so
728 * we revert to slow nlist().
729 */
730 if (bcmp(dbversion, kversion, dbversionlen) == 0)
731 return (0);
732 close:
733 (void)(kd->db->close)(kd->db);
734 kd->db = 0;
735
736 return (-1);
737 }
738
739 int
740 kvm_nlist(kd, nl)
741 kvm_t *kd;
742 struct nlist *nl;
743 {
744 register struct nlist *p;
745 register int nvalid;
746
747 /*
748 * If we can't use the data base, revert to the
749 * slow library call.
750 */
751 if (kd->db == 0)
752 return (__fdnlist(kd->nlfd, nl));
753
754 /*
755 * We can use the kvm data base. Go through each nlist entry
756 * and look it up with a db query.
757 */
758 nvalid = 0;
759 for (p = nl; p->n_name && p->n_name[0]; ++p) {
760 register int len;
761 DBT rec;
762
763 if ((len = strlen(p->n_name)) > 4096) {
764 /* sanity */
765 _kvm_err(kd, kd->program, "symbol too large");
766 return (-1);
767 }
768 rec.data = p->n_name;
769 rec.size = len;
770
771 /*
772 * Make sure that n_value = 0 when the symbol isn't found
773 */
774 p->n_value = 0;
775
776 if ((kd->db->get)(kd->db, (DBT *)&rec, (DBT *)&rec, 0))
777 continue;
778 if (rec.data == 0 || rec.size != sizeof(struct nlist))
779 continue;
780 ++nvalid;
781 /*
782 * Avoid alignment issues.
783 */
784 bcopy((char *)&((struct nlist *)rec.data)->n_type,
785 (char *)&p->n_type,
786 sizeof(p->n_type));
787 bcopy((char *)&((struct nlist *)rec.data)->n_value,
788 (char *)&p->n_value,
789 sizeof(p->n_value));
790 }
791 /*
792 * Return the number of entries that weren't found.
793 */
794 return ((p - nl) - nvalid);
795 }
796
797 int kvm_dump_inval(kd)
798 kvm_t *kd;
799 {
800 struct nlist nlist[2];
801 u_long pa;
802
803 if (ISALIVE(kd)) {
804 _kvm_err(kd, kd->program, "clearing dump on live kernel");
805 return (-1);
806 }
807 nlist[0].n_name = "_dumpmag";
808 nlist[1].n_name = NULL;
809
810 if (kvm_nlist(kd, nlist) == -1) {
811 _kvm_err(kd, 0, "bad namelist");
812 return (-1);
813 }
814 if (_kvm_kvatop(kd, (u_long)nlist[0].n_value, &pa) == 0)
815 return (-1);
816
817 errno = 0;
818 if (lseek(kd->pmfd, _kvm_pa2off(kd, pa), SEEK_SET) == -1
819 && errno != 0) {
820 _kvm_err(kd, 0, "cannot invalidate dump - lseek");
821 return (-1);
822 }
823 pa = 0;
824 if (write(kd->pmfd, &pa, sizeof(pa)) != sizeof(pa)) {
825 _kvm_err(kd, 0, "cannot invalidate dump - write");
826 return (-1);
827 }
828 return (0);
829 }
830
831 ssize_t
832 kvm_read(kd, kva, buf, len)
833 kvm_t *kd;
834 register u_long kva;
835 register void *buf;
836 register size_t len;
837 {
838 register int cc;
839 register void *cp;
840
841 if (ISALIVE(kd)) {
842 /*
843 * We're using /dev/kmem. Just read straight from the
844 * device and let the active kernel do the address translation.
845 */
846 errno = 0;
847 if (lseek(kd->vmfd, (off_t)kva, SEEK_SET) == -1
848 && errno != 0) {
849 _kvm_err(kd, 0, "invalid address (%x)", kva);
850 return (0);
851 }
852 cc = read(kd->vmfd, buf, len);
853 if (cc < 0) {
854 _kvm_syserr(kd, 0, "kvm_read");
855 return (0);
856 } else if (cc < len)
857 _kvm_err(kd, kd->program, "short read");
858 return (cc);
859 } else {
860 if ((kd->kcore_hdr == NULL) || (kd->cpu_hdr == NULL)) {
861 _kvm_err(kd, kd->program, "no valid dump header");
862 return (0);
863 }
864 cp = buf;
865 while (len > 0) {
866 u_long pa;
867 off_t foff;
868
869 cc = _kvm_kvatop(kd, kva, &pa);
870 if (cc == 0)
871 return (0);
872 if (cc > len)
873 cc = len;
874 foff = _kvm_pa2off(kd, pa);
875 errno = 0;
876 if (lseek(kd->pmfd, foff, SEEK_SET) == -1
877 && errno != 0) {
878 _kvm_syserr(kd, 0, _PATH_MEM);
879 break;
880 }
881 cc = read(kd->pmfd, cp, cc);
882 if (cc < 0) {
883 _kvm_syserr(kd, kd->program, "kvm_read");
884 break;
885 }
886 /*
887 * If kvm_kvatop returns a bogus value or our core
888 * file is truncated, we might wind up seeking beyond
889 * the end of the core file in which case the read will
890 * return 0 (EOF).
891 */
892 if (cc == 0)
893 break;
894 cp = (char *)cp + cc;
895 kva += cc;
896 len -= cc;
897 }
898 return ((char *)cp - (char *)buf);
899 }
900 /* NOTREACHED */
901 }
902
903 ssize_t
904 kvm_write(kd, kva, buf, len)
905 kvm_t *kd;
906 register u_long kva;
907 register const void *buf;
908 register size_t len;
909 {
910 register int cc;
911
912 if (ISALIVE(kd)) {
913 /*
914 * Just like kvm_read, only we write.
915 */
916 errno = 0;
917 if (lseek(kd->vmfd, (off_t)kva, SEEK_SET) == -1
918 && errno != 0) {
919 _kvm_err(kd, 0, "invalid address (%x)", kva);
920 return (0);
921 }
922 cc = write(kd->vmfd, buf, len);
923 if (cc < 0) {
924 _kvm_syserr(kd, 0, "kvm_write");
925 return (0);
926 } else if (cc < len)
927 _kvm_err(kd, kd->program, "short write");
928 return (cc);
929 } else {
930 _kvm_err(kd, kd->program,
931 "kvm_write not implemented for dead kernels");
932 return (0);
933 }
934 /* NOTREACHED */
935 }
936