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