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