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