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