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