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